option_processing.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "common.h"
  36. #include "Option.h"
  37. #include "prefs.h"
  38. #include "OptionParser.h"
  39. #include "OptionHandlerFactory.h"
  40. #include "Util.h"
  41. #include "message.h"
  42. #include "Exception.h"
  43. #include "a2io.h"
  44. #include <fstream>
  45. #include <sstream>
  46. extern char* optarg;
  47. extern int optind, opterr, optopt;
  48. #include <getopt.h>
  49. extern void showVersion();
  50. extern void showUsage();
  51. static string toBoolArg(const char* optarg)
  52. {
  53. string arg;
  54. if(!optarg || string(optarg) == "") {
  55. arg = V_TRUE;
  56. } else {
  57. arg = optarg;
  58. }
  59. return arg;
  60. }
  61. Option* option_processing(int argc, char* const argv[])
  62. {
  63. stringstream cmdstream;
  64. int32_t c;
  65. Option* op = new Option();
  66. op->put(PREF_STDOUT_LOG, V_FALSE);
  67. op->put(PREF_DIR, ".");
  68. op->put(PREF_SPLIT, "1");
  69. op->put(PREF_DAEMON, V_FALSE);
  70. op->put(PREF_SEGMENT_SIZE, Util::itos((int32_t)(1024*1024)));
  71. op->put(PREF_LISTEN_PORT, "6881-6999");
  72. op->put(PREF_METALINK_SERVERS, "5");
  73. op->put(PREF_FOLLOW_TORRENT,
  74. #ifdef ENABLE_BITTORRENT
  75. V_TRUE
  76. #else
  77. V_FALSE
  78. #endif // ENABLE_BITTORRENT
  79. );
  80. op->put(PREF_FOLLOW_METALINK,
  81. #ifdef ENABLE_METALINK
  82. V_TRUE
  83. #else
  84. V_FALSE
  85. #endif // ENABLE_METALINK
  86. );
  87. op->put(PREF_RETRY_WAIT, "5");
  88. op->put(PREF_TIMEOUT, "60");
  89. op->put(PREF_DNS_TIMEOUT, "10");
  90. op->put(PREF_PEER_CONNECTION_TIMEOUT, "20");
  91. op->put(PREF_BT_TIMEOUT, "180");
  92. op->put(PREF_BT_REQUEST_TIMEOUT, "60");
  93. op->put(PREF_BT_KEEP_ALIVE_INTERVAL, "120");
  94. op->put(PREF_MIN_SEGMENT_SIZE, "1048576");// 1M
  95. op->put(PREF_MAX_TRIES, "5");
  96. op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
  97. op->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  98. op->put(PREF_FTP_TYPE, V_BINARY);
  99. op->put(PREF_FTP_VIA_HTTP_PROXY, V_TUNNEL);
  100. op->put(PREF_AUTO_SAVE_INTERVAL, "60");
  101. op->put(PREF_DIRECT_FILE_MAPPING, V_TRUE);
  102. op->put(PREF_LOWEST_SPEED_LIMIT, "0");
  103. op->put(PREF_MAX_DOWNLOAD_LIMIT, "0");
  104. op->put(PREF_MAX_UPLOAD_LIMIT, "0");
  105. op->put(PREF_STARTUP_IDLE_TIME, "10");
  106. op->put(PREF_TRACKER_MAX_TRIES, "10");
  107. op->put(PREF_FILE_ALLOCATION, V_PREALLOC);
  108. op->put(PREF_NO_FILE_ALLOCATION_LIMIT, "5242880"); // 5MiB
  109. op->put(PREF_ALLOW_OVERWRITE, V_FALSE);
  110. op->put(PREF_REALTIME_CHUNK_CHECKSUM, V_TRUE);
  111. op->put(PREF_CHECK_INTEGRITY, V_FALSE);
  112. op->put(PREF_NETRC_PATH, Util::getHomeDir()+"/.netrc");
  113. op->put(PREF_CONTINUE, V_FALSE);
  114. op->put(PREF_USER_AGENT, "aria2");
  115. op->put(PREF_NO_NETRC, V_FALSE);
  116. op->put(PREF_MAX_CONCURRENT_DOWNLOADS, "5");
  117. op->put(PREF_DIRECT_DOWNLOAD_TIMEOUT, "300");
  118. op->put(PREF_FORCE_SEQUENTIAL, V_FALSE);
  119. op->put(PREF_AUTO_FILE_RENAMING, V_TRUE);
  120. op->put(PREF_PARAMETERIZED_URI, V_FALSE);
  121. op->put(PREF_ENABLE_HTTP_KEEP_ALIVE, V_FALSE);
  122. op->put(PREF_ENABLE_HTTP_PIPELINING, V_FALSE);
  123. op->put(PREF_MAX_HTTP_PIPELINING, "2");
  124. op->put(PREF_SEED_RATIO, "1.0");
  125. op->put(PREF_ENABLE_DIRECT_IO, V_FALSE);
  126. op->put(PREF_ALLOW_PIECE_LENGTH_CHANGE, V_FALSE);
  127. op->put(PREF_METALINK_PREFERRED_PROTOCOL, V_NONE);
  128. op->put(PREF_ENABLE_PEER_EXCHANGE, V_TRUE);
  129. op->put(PREF_METALINK_ENABLE_UNIQUE_PROTOCOL, V_TRUE);
  130. while(1) {
  131. int optIndex = 0;
  132. int lopt;
  133. static struct option longOpts[] = {
  134. #ifdef HAVE_DAEMON
  135. { "daemon", no_argument, NULL, 'D' },
  136. #endif // HAVE_DAEMON
  137. { "dir", required_argument, NULL, 'd' },
  138. { "out", required_argument, NULL, 'o' },
  139. { "log", required_argument, NULL, 'l' },
  140. { "split", required_argument, NULL, 's' },
  141. { "timeout", required_argument, NULL, 't' },
  142. { "max-tries", required_argument, NULL, 'm' },
  143. { "http-proxy", required_argument, &lopt, 1 },
  144. { "http-user", required_argument, &lopt, 2 },
  145. { "http-passwd", required_argument, &lopt, 3 },
  146. { "http-proxy-user", required_argument, &lopt, 4 },
  147. { "http-proxy-passwd", required_argument, &lopt, 5 },
  148. { "http-auth-scheme", required_argument, &lopt, 6 },
  149. { "referer", required_argument, &lopt, 7 },
  150. { "retry-wait", required_argument, &lopt, 8 },
  151. { "ftp-user", required_argument, &lopt, 9 },
  152. { "ftp-passwd", required_argument, &lopt, 10 },
  153. { "ftp-type", required_argument, &lopt, 11 },
  154. { "ftp-pasv", no_argument, NULL, 'p' },
  155. { "ftp-via-http-proxy", required_argument, &lopt, 12 },
  156. //{ "min-segment-size", required_argument, &lopt, 13 },
  157. { "http-proxy-method", required_argument, &lopt, 14 },
  158. { "lowest-speed-limit", required_argument, &lopt, 200 },
  159. { "max-download-limit", required_argument, &lopt, 201 },
  160. { "file-allocation", required_argument, 0, 'a' },
  161. { "allow-overwrite", required_argument, &lopt, 202 },
  162. #ifdef ENABLE_MESSAGE_DIGEST
  163. { "check-integrity", required_argument, &lopt, 203 },
  164. { "realtime-chunk-checksum", required_argument, &lopt, 204 },
  165. #endif // ENABLE_MESSAGE_DIGEST
  166. { "continue", no_argument, 0, 'c' },
  167. { "user-agent", required_argument, 0, 'U' },
  168. { "no-netrc", no_argument, 0, 'n' },
  169. { "input-file", required_argument, 0, 'i' },
  170. { "max-concurrent-downloads", required_argument, 0, 'j' },
  171. { "load-cookies", required_argument, &lopt, 205 },
  172. { "force-sequential", optional_argument, 0, 'Z' },
  173. { "auto-file-renaming", optional_argument, &lopt, 206 },
  174. { "parameterized-uri", optional_argument, 0, 'P' },
  175. { "enable-http-keep-alive", optional_argument, &lopt, 207 },
  176. { "enable-http-pipelining", optional_argument, &lopt, 208 },
  177. { "no-file-allocation-limit", required_argument, &lopt, 209 },
  178. #ifdef ENABLE_DIRECT_IO
  179. { PREF_ENABLE_DIRECT_IO, optional_argument, &lopt, 210 },
  180. #endif // ENABLE_DIRECT_IO
  181. { PREF_ALLOW_PIECE_LENGTH_CHANGE, required_argument, &lopt, 211 },
  182. #if defined ENABLE_BITTORRENT || ENABLE_METALINK
  183. { "show-files", no_argument, NULL, 'S' },
  184. { "select-file", required_argument, &lopt, 21 },
  185. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  186. #ifdef ENABLE_BITTORRENT
  187. { "torrent-file", required_argument, NULL, 'T' },
  188. { "listen-port", required_argument, &lopt, 15 },
  189. { "follow-torrent", required_argument, &lopt, 16 },
  190. { "no-preallocation", no_argument, &lopt, 18 },
  191. { "direct-file-mapping", required_argument, &lopt, 19 },
  192. // TODO remove upload-limit.
  193. //{ "upload-limit", required_argument, &lopt, 20 },
  194. { "seed-time", required_argument, &lopt, 22 },
  195. { "seed-ratio", required_argument, &lopt, 23 },
  196. { "max-upload-limit", required_argument, &lopt, 24 },
  197. { "peer-id-prefix", required_argument, &lopt, 25 },
  198. #endif // ENABLE_BITTORRENT
  199. #ifdef ENABLE_METALINK
  200. { "metalink-file", required_argument, NULL, 'M' },
  201. { "metalink-servers", required_argument, NULL, 'C' },
  202. { "metalink-version", required_argument, &lopt, 100 },
  203. { "metalink-language", required_argument, &lopt, 101 },
  204. { "metalink-os", required_argument, &lopt, 102 },
  205. { "follow-metalink", required_argument, &lopt, 103 },
  206. { "metalink-location", required_argument, &lopt, 104 },
  207. { "metalink-preferred-protocol", required_argument, &lopt, 105 },
  208. { "metalink-enable-unique-protocol", optional_argument, &lopt, 106 },
  209. #endif // ENABLE_METALINK
  210. { "version", no_argument, NULL, 'v' },
  211. { "help", no_argument, NULL, 'h' },
  212. { 0, 0, 0, 0 }
  213. };
  214. c = getopt_long(argc, argv, "Dd:o:l:s:pt:m:vhST:M:C:a:cU:ni:j:Z::P::", longOpts, &optIndex);
  215. if(c == -1) {
  216. break;
  217. }
  218. switch(c) {
  219. case 0:{
  220. switch(lopt) {
  221. case 1:
  222. cmdstream << PREF_HTTP_PROXY << "=" << optarg << "\n";
  223. break;
  224. case 2:
  225. cmdstream << PREF_HTTP_USER << "=" << optarg << "\n";
  226. break;
  227. case 3:
  228. cmdstream << PREF_HTTP_PASSWD << "=" << optarg << "\n";
  229. break;
  230. case 4:
  231. cmdstream << PREF_HTTP_PROXY_USER << "=" << optarg << "\n";
  232. break;
  233. case 5:
  234. cmdstream << PREF_HTTP_PROXY_PASSWD << "=" << optarg << "\n";
  235. break;
  236. case 6:
  237. cmdstream << PREF_HTTP_AUTH_SCHEME << "=" << optarg << "\n";
  238. break;
  239. case 7:
  240. cmdstream << PREF_REFERER << "=" << optarg << "\n";
  241. break;
  242. case 8:
  243. cmdstream << PREF_RETRY_WAIT << "=" << optarg << "\n";
  244. break;
  245. case 9:
  246. cmdstream << PREF_FTP_USER << "=" << optarg << "\n";
  247. break;
  248. case 10:
  249. cmdstream << PREF_FTP_PASSWD << "=" << optarg << "\n";
  250. break;
  251. case 11:
  252. cmdstream << PREF_FTP_TYPE << "=" << optarg << "\n";
  253. break;
  254. case 12:
  255. cmdstream << PREF_FTP_VIA_HTTP_PROXY << "=" << optarg << "\n";
  256. break;
  257. case 13:
  258. cmdstream << PREF_MIN_SEGMENT_SIZE << "=" << optarg << "\n";
  259. break;
  260. case 14:
  261. cmdstream << PREF_HTTP_PROXY_METHOD << "=" << optarg << "\n";
  262. break;
  263. case 15:
  264. cmdstream << PREF_LISTEN_PORT << "=" << optarg << "\n";
  265. break;
  266. case 16:
  267. cmdstream << PREF_FOLLOW_TORRENT << "=" << optarg << "\n";
  268. break;
  269. case 18:
  270. cmdstream << PREF_NO_PREALLOCATION << "=" << V_TRUE << "\n";
  271. break;
  272. case 19:
  273. cmdstream << PREF_DIRECT_FILE_MAPPING << "=" << optarg << "\n";
  274. break;
  275. case 21:
  276. cmdstream << PREF_SELECT_FILE << "=" << optarg << "\n";
  277. break;
  278. case 22:
  279. cmdstream << PREF_SEED_TIME << "=" << optarg << "\n";
  280. break;
  281. case 23:
  282. cmdstream << PREF_SEED_RATIO << "=" << optarg << "\n";
  283. break;
  284. case 24:
  285. cmdstream << PREF_MAX_UPLOAD_LIMIT << "=" << optarg << "\n";
  286. break;
  287. case 25:
  288. cmdstream << PREF_PEER_ID_PREFIX << "=" << optarg << "\n";
  289. case 100:
  290. cmdstream << PREF_METALINK_VERSION << "=" << optarg << "\n";
  291. break;
  292. case 101:
  293. cmdstream << PREF_METALINK_LANGUAGE << "=" << optarg << "\n";
  294. break;
  295. case 102:
  296. cmdstream << PREF_METALINK_OS << "=" << optarg << "\n";
  297. break;
  298. case 103:
  299. cmdstream << PREF_FOLLOW_METALINK << "=" << optarg << "\n";
  300. break;
  301. case 104:
  302. cmdstream << PREF_METALINK_LOCATION << "=" << optarg << "\n";
  303. break;
  304. case 105:
  305. cmdstream << PREF_METALINK_PREFERRED_PROTOCOL << "=" << optarg << "\n";
  306. break;
  307. case 106:
  308. cmdstream << PREF_METALINK_ENABLE_UNIQUE_PROTOCOL << "=" << toBoolArg(optarg) << "\n";
  309. break;
  310. case 200:
  311. cmdstream << PREF_LOWEST_SPEED_LIMIT << "=" << optarg << "\n";
  312. break;
  313. case 201:
  314. cmdstream << PREF_MAX_DOWNLOAD_LIMIT << "=" << optarg << "\n";
  315. break;
  316. case 202:
  317. cmdstream << PREF_ALLOW_OVERWRITE << "=" << optarg << "\n";
  318. break;
  319. case 203:
  320. cmdstream << PREF_CHECK_INTEGRITY << "=" << optarg << "\n";
  321. break;
  322. case 204:
  323. cmdstream << PREF_REALTIME_CHUNK_CHECKSUM << "=" << optarg << "\n";
  324. break;
  325. case 205:
  326. cmdstream << PREF_LOAD_COOKIES << "=" << optarg << "\n";
  327. break;
  328. case 206:
  329. cmdstream << PREF_AUTO_FILE_RENAMING << "=" << toBoolArg(optarg) << "\n";
  330. break;
  331. case 207:
  332. cmdstream << PREF_ENABLE_HTTP_KEEP_ALIVE << "=" << toBoolArg(optarg) << "\n";
  333. break;
  334. case 208:
  335. cmdstream << PREF_ENABLE_HTTP_PIPELINING << "=" << toBoolArg(optarg) << "\n";
  336. break;
  337. case 209:
  338. cmdstream << PREF_NO_FILE_ALLOCATION_LIMIT << "=" << optarg << "\n";
  339. break;
  340. case 210:
  341. cmdstream << PREF_ENABLE_DIRECT_IO << "=" << toBoolArg(optarg) << "\n";
  342. break;
  343. case 211:
  344. cmdstream << PREF_ALLOW_PIECE_LENGTH_CHANGE << "=" << optarg << "\n";
  345. break;
  346. }
  347. break;
  348. }
  349. #ifdef HAVE_DAEMON
  350. case 'D':
  351. cmdstream << PREF_DAEMON << "=" << V_TRUE << "\n";
  352. break;
  353. #endif // HAVE_DAEMON
  354. case 'd':
  355. cmdstream << PREF_DIR << "=" << optarg << "\n";
  356. break;
  357. case 'o':
  358. cmdstream << PREF_OUT << "=" << optarg << "\n";
  359. break;
  360. case 'l':
  361. cmdstream << PREF_LOG << "=" << optarg << "\n";
  362. break;
  363. case 's':
  364. cmdstream << PREF_SPLIT << "=" << optarg << "\n";
  365. break;
  366. case 't':
  367. cmdstream << PREF_TIMEOUT << "=" << optarg << "\n";
  368. break;
  369. case 'm':
  370. cmdstream << PREF_MAX_TRIES << "=" << optarg << "\n";
  371. break;
  372. case 'p':
  373. cmdstream << PREF_FTP_PASV << "=" << V_TRUE << "\n";
  374. break;
  375. case 'S':
  376. cmdstream << PREF_SHOW_FILES << "=" << V_TRUE << "\n";
  377. break;
  378. case 'T':
  379. cmdstream << PREF_TORRENT_FILE << "=" << optarg << "\n";
  380. break;
  381. case 'M':
  382. cmdstream << PREF_METALINK_FILE << "=" << optarg << "\n";
  383. break;
  384. case 'C':
  385. cmdstream << PREF_METALINK_SERVERS << "=" << optarg << "\n";
  386. break;
  387. case 'a':
  388. cmdstream << PREF_FILE_ALLOCATION << "=" << optarg << "\n";
  389. break;
  390. case 'c':
  391. cmdstream << PREF_CONTINUE << "=" << V_TRUE << "\n";
  392. break;
  393. case 'U':
  394. cmdstream << PREF_USER_AGENT << "=" << optarg << "\n";
  395. break;
  396. case 'n':
  397. cmdstream << PREF_NO_NETRC << "=" << V_TRUE << "\n";
  398. break;
  399. case 'i':
  400. cmdstream << PREF_INPUT_FILE << "=" << optarg << "\n";
  401. break;
  402. case 'j':
  403. cmdstream << PREF_MAX_CONCURRENT_DOWNLOADS << "=" << optarg << "\n";
  404. break;
  405. case 'Z':
  406. cmdstream << PREF_FORCE_SEQUENTIAL << "=" << toBoolArg(optarg) << "\n";
  407. break;
  408. case 'P':
  409. cmdstream << PREF_PARAMETERIZED_URI << "=" << toBoolArg(optarg) << "\n";
  410. break;
  411. case 'v':
  412. showVersion();
  413. exit(EXIT_SUCCESS);
  414. case 'h':
  415. showUsage();
  416. exit(EXIT_SUCCESS);
  417. default:
  418. exit(EXIT_FAILURE);
  419. }
  420. }
  421. {
  422. OptionParser oparser;
  423. oparser.setOptionHandlers(OptionHandlerFactory::createOptionHandlers());
  424. string cfname = Util::getHomeDir()+"/.aria2/aria2.conf";
  425. ifstream cfstream(cfname.c_str());
  426. try {
  427. oparser.parse(op, cfstream);
  428. } catch(Exception* e) {
  429. cerr << "Parse error in " << cfname << endl;
  430. cerr << *e << endl;
  431. delete e;
  432. exit(EXIT_FAILURE);
  433. }
  434. try {
  435. oparser.parse(op, cmdstream);
  436. } catch(Exception* e) {
  437. cerr << *e << endl;
  438. delete e;
  439. exit(EXIT_FAILURE);
  440. }
  441. }
  442. if(op->defined(PREF_HTTP_USER)) {
  443. op->put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  444. }
  445. if(op->defined(PREF_HTTP_PROXY_USER)) {
  446. op->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  447. }
  448. if(
  449. #ifdef ENABLE_BITTORRENT
  450. !op->defined(PREF_TORRENT_FILE) &&
  451. #endif // ENABLE_BITTORRENT
  452. #ifdef ENABLE_METALINK
  453. !op->defined(PREF_METALINK_FILE) &&
  454. #endif // ENABLE_METALINK
  455. !op->defined(PREF_INPUT_FILE)) {
  456. if(optind == argc) {
  457. cerr << MSG_URI_REQUIRED << endl;
  458. exit(EXIT_FAILURE);
  459. }
  460. }
  461. #ifdef HAVE_DAEMON
  462. if(op->getAsBool(PREF_DAEMON)) {
  463. if(daemon(1, 1) < 0) {
  464. perror(MSG_DAEMON_FAILED);
  465. exit(EXIT_FAILURE);
  466. }
  467. }
  468. #endif // HAVE_DAEMON
  469. return op;
  470. }