option_processing.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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, "60");
  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, "15");
  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. while(1) {
  127. int optIndex = 0;
  128. int lopt;
  129. static struct option longOpts[] = {
  130. #ifdef HAVE_DAEMON
  131. { "daemon", no_argument, NULL, 'D' },
  132. #endif // HAVE_DAEMON
  133. { "dir", required_argument, NULL, 'd' },
  134. { "out", required_argument, NULL, 'o' },
  135. { "log", required_argument, NULL, 'l' },
  136. { "split", required_argument, NULL, 's' },
  137. { "timeout", required_argument, NULL, 't' },
  138. { "max-tries", required_argument, NULL, 'm' },
  139. { "http-proxy", required_argument, &lopt, 1 },
  140. { "http-user", required_argument, &lopt, 2 },
  141. { "http-passwd", required_argument, &lopt, 3 },
  142. { "http-proxy-user", required_argument, &lopt, 4 },
  143. { "http-proxy-passwd", required_argument, &lopt, 5 },
  144. { "http-auth-scheme", required_argument, &lopt, 6 },
  145. { "referer", required_argument, &lopt, 7 },
  146. { "retry-wait", required_argument, &lopt, 8 },
  147. { "ftp-user", required_argument, &lopt, 9 },
  148. { "ftp-passwd", required_argument, &lopt, 10 },
  149. { "ftp-type", required_argument, &lopt, 11 },
  150. { "ftp-pasv", no_argument, NULL, 'p' },
  151. { "ftp-via-http-proxy", required_argument, &lopt, 12 },
  152. //{ "min-segment-size", required_argument, &lopt, 13 },
  153. { "http-proxy-method", required_argument, &lopt, 14 },
  154. { "lowest-speed-limit", required_argument, &lopt, 200 },
  155. { "max-download-limit", required_argument, &lopt, 201 },
  156. { "file-allocation", required_argument, 0, 'a' },
  157. { "allow-overwrite", required_argument, &lopt, 202 },
  158. #ifdef ENABLE_MESSAGE_DIGEST
  159. { "check-integrity", required_argument, &lopt, 203 },
  160. { "realtime-chunk-checksum", required_argument, &lopt, 204 },
  161. #endif // ENABLE_MESSAGE_DIGEST
  162. { "continue", no_argument, 0, 'c' },
  163. { "user-agent", required_argument, 0, 'U' },
  164. { "no-netrc", no_argument, 0, 'n' },
  165. { "input-file", required_argument, 0, 'i' },
  166. { "max-concurrent-downloads", required_argument, 0, 'j' },
  167. { "load-cookies", required_argument, &lopt, 205 },
  168. { "force-sequential", optional_argument, 0, 'Z' },
  169. { "auto-file-renaming", optional_argument, &lopt, 206 },
  170. { "parameterized-uri", optional_argument, 0, 'P' },
  171. { "enable-http-keep-alive", optional_argument, &lopt, 207 },
  172. { "enable-http-pipelining", optional_argument, &lopt, 208 },
  173. { "no-file-allocation-limit", required_argument, &lopt, 209 },
  174. #ifdef ENABLE_DIRECT_IO
  175. { PREF_ENABLE_DIRECT_IO, optional_argument, &lopt, 210 },
  176. #endif // ENABLE_DIRECT_IO
  177. #if defined ENABLE_BITTORRENT || ENABLE_METALINK
  178. { "show-files", no_argument, NULL, 'S' },
  179. { "select-file", required_argument, &lopt, 21 },
  180. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  181. #ifdef ENABLE_BITTORRENT
  182. { "torrent-file", required_argument, NULL, 'T' },
  183. { "listen-port", required_argument, &lopt, 15 },
  184. { "follow-torrent", required_argument, &lopt, 16 },
  185. { "no-preallocation", no_argument, &lopt, 18 },
  186. { "direct-file-mapping", required_argument, &lopt, 19 },
  187. // TODO remove upload-limit.
  188. //{ "upload-limit", required_argument, &lopt, 20 },
  189. { "seed-time", required_argument, &lopt, 22 },
  190. { "seed-ratio", required_argument, &lopt, 23 },
  191. { "max-upload-limit", required_argument, &lopt, 24 },
  192. { "peer-id-prefix", required_argument, &lopt, 25 },
  193. #endif // ENABLE_BITTORRENT
  194. #ifdef ENABLE_METALINK
  195. { "metalink-file", required_argument, NULL, 'M' },
  196. { "metalink-servers", required_argument, NULL, 'C' },
  197. { "metalink-version", required_argument, &lopt, 100 },
  198. { "metalink-language", required_argument, &lopt, 101 },
  199. { "metalink-os", required_argument, &lopt, 102 },
  200. { "follow-metalink", required_argument, &lopt, 103 },
  201. { "metalink-location", required_argument, &lopt, 104 },
  202. #endif // ENABLE_METALINK
  203. { "version", no_argument, NULL, 'v' },
  204. { "help", no_argument, NULL, 'h' },
  205. { 0, 0, 0, 0 }
  206. };
  207. c = getopt_long(argc, argv, "Dd:o:l:s:pt:m:vhST:M:C:a:cU:ni:j:Z::P::", longOpts, &optIndex);
  208. if(c == -1) {
  209. break;
  210. }
  211. switch(c) {
  212. case 0:{
  213. switch(lopt) {
  214. case 1:
  215. cmdstream << PREF_HTTP_PROXY << "=" << optarg << "\n";
  216. break;
  217. case 2:
  218. cmdstream << PREF_HTTP_USER << "=" << optarg << "\n";
  219. break;
  220. case 3:
  221. cmdstream << PREF_HTTP_PASSWD << "=" << optarg << "\n";
  222. break;
  223. case 4:
  224. cmdstream << PREF_HTTP_PROXY_USER << "=" << optarg << "\n";
  225. break;
  226. case 5:
  227. cmdstream << PREF_HTTP_PROXY_PASSWD << "=" << optarg << "\n";
  228. break;
  229. case 6:
  230. cmdstream << PREF_HTTP_AUTH_SCHEME << "=" << optarg << "\n";
  231. break;
  232. case 7:
  233. cmdstream << PREF_REFERER << "=" << optarg << "\n";
  234. break;
  235. case 8:
  236. cmdstream << PREF_RETRY_WAIT << "=" << optarg << "\n";
  237. break;
  238. case 9:
  239. cmdstream << PREF_FTP_USER << "=" << optarg << "\n";
  240. break;
  241. case 10:
  242. cmdstream << PREF_FTP_PASSWD << "=" << optarg << "\n";
  243. break;
  244. case 11:
  245. cmdstream << PREF_FTP_TYPE << "=" << optarg << "\n";
  246. break;
  247. case 12:
  248. cmdstream << PREF_FTP_VIA_HTTP_PROXY << "=" << optarg << "\n";
  249. break;
  250. case 13:
  251. cmdstream << PREF_MIN_SEGMENT_SIZE << "=" << optarg << "\n";
  252. break;
  253. case 14:
  254. cmdstream << PREF_HTTP_PROXY_METHOD << "=" << optarg << "\n";
  255. break;
  256. case 15:
  257. cmdstream << PREF_LISTEN_PORT << "=" << optarg << "\n";
  258. break;
  259. case 16:
  260. cmdstream << PREF_FOLLOW_TORRENT << "=" << optarg << "\n";
  261. break;
  262. case 18:
  263. cmdstream << PREF_NO_PREALLOCATION << "=" << V_TRUE << "\n";
  264. break;
  265. case 19:
  266. cmdstream << PREF_DIRECT_FILE_MAPPING << "=" << optarg << "\n";
  267. break;
  268. case 21:
  269. cmdstream << PREF_SELECT_FILE << "=" << optarg << "\n";
  270. break;
  271. case 22:
  272. cmdstream << PREF_SEED_TIME << "=" << optarg << "\n";
  273. break;
  274. case 23:
  275. cmdstream << PREF_SEED_RATIO << "=" << optarg << "\n";
  276. break;
  277. case 24:
  278. cmdstream << PREF_MAX_UPLOAD_LIMIT << "=" << optarg << "\n";
  279. break;
  280. case 25:
  281. cmdstream << PREF_PEER_ID_PREFIX << "=" << optarg << "\n";
  282. case 100:
  283. cmdstream << PREF_METALINK_VERSION << "=" << optarg << "\n";
  284. break;
  285. case 101:
  286. cmdstream << PREF_METALINK_LANGUAGE << "=" << optarg << "\n";
  287. break;
  288. case 102:
  289. cmdstream << PREF_METALINK_OS << "=" << optarg << "\n";
  290. break;
  291. case 103:
  292. cmdstream << PREF_FOLLOW_METALINK << "=" << optarg << "\n";
  293. break;
  294. case 104:
  295. cmdstream << PREF_METALINK_LOCATION << "=" << optarg << "\n";
  296. break;
  297. case 200:
  298. cmdstream << PREF_LOWEST_SPEED_LIMIT << "=" << optarg << "\n";
  299. break;
  300. case 201:
  301. cmdstream << PREF_MAX_DOWNLOAD_LIMIT << "=" << optarg << "\n";
  302. break;
  303. case 202:
  304. cmdstream << PREF_ALLOW_OVERWRITE << "=" << optarg << "\n";
  305. break;
  306. case 203:
  307. cmdstream << PREF_CHECK_INTEGRITY << "=" << optarg << "\n";
  308. break;
  309. case 204:
  310. cmdstream << PREF_REALTIME_CHUNK_CHECKSUM << "=" << optarg << "\n";
  311. break;
  312. case 205:
  313. cmdstream << PREF_LOAD_COOKIES << "=" << optarg << "\n";
  314. break;
  315. case 206:
  316. cmdstream << PREF_AUTO_FILE_RENAMING << "=" << toBoolArg(optarg) << "\n";
  317. break;
  318. case 207:
  319. cmdstream << PREF_ENABLE_HTTP_KEEP_ALIVE << "=" << toBoolArg(optarg) << "\n";
  320. break;
  321. case 208:
  322. cmdstream << PREF_ENABLE_HTTP_PIPELINING << "=" << toBoolArg(optarg) << "\n";
  323. break;
  324. case 209:
  325. cmdstream << PREF_NO_FILE_ALLOCATION_LIMIT << "=" << optarg << "\n";
  326. break;
  327. case 210:
  328. cmdstream << PREF_ENABLE_DIRECT_IO << "=" << toBoolArg(optarg) << "\n";
  329. break;
  330. }
  331. break;
  332. }
  333. #ifdef HAVE_DAEMON
  334. case 'D':
  335. cmdstream << PREF_DAEMON << "=" << V_TRUE << "\n";
  336. break;
  337. #endif // HAVE_DAEMON
  338. case 'd':
  339. cmdstream << PREF_DIR << "=" << optarg << "\n";
  340. break;
  341. case 'o':
  342. cmdstream << PREF_OUT << "=" << optarg << "\n";
  343. break;
  344. case 'l':
  345. cmdstream << PREF_LOG << "=" << optarg << "\n";
  346. break;
  347. case 's':
  348. cmdstream << PREF_SPLIT << "=" << optarg << "\n";
  349. break;
  350. case 't':
  351. cmdstream << PREF_TIMEOUT << "=" << optarg << "\n";
  352. break;
  353. case 'm':
  354. cmdstream << PREF_MAX_TRIES << "=" << optarg << "\n";
  355. break;
  356. case 'p':
  357. cmdstream << PREF_FTP_PASV << "=" << V_TRUE << "\n";
  358. break;
  359. case 'S':
  360. cmdstream << PREF_SHOW_FILES << "=" << V_TRUE << "\n";
  361. break;
  362. case 'T':
  363. cmdstream << PREF_TORRENT_FILE << "=" << optarg << "\n";
  364. break;
  365. case 'M':
  366. cmdstream << PREF_METALINK_FILE << "=" << optarg << "\n";
  367. break;
  368. case 'C':
  369. cmdstream << PREF_METALINK_SERVERS << "=" << optarg << "\n";
  370. break;
  371. case 'a':
  372. cmdstream << PREF_FILE_ALLOCATION << "=" << optarg << "\n";
  373. break;
  374. case 'c':
  375. cmdstream << PREF_CONTINUE << "=" << V_TRUE << "\n";
  376. break;
  377. case 'U':
  378. cmdstream << PREF_USER_AGENT << "=" << optarg << "\n";
  379. break;
  380. case 'n':
  381. cmdstream << PREF_NO_NETRC << "=" << V_TRUE << "\n";
  382. break;
  383. case 'i':
  384. cmdstream << PREF_INPUT_FILE << "=" << optarg << "\n";
  385. break;
  386. case 'j':
  387. cmdstream << PREF_MAX_CONCURRENT_DOWNLOADS << "=" << optarg << "\n";
  388. break;
  389. case 'Z':
  390. cmdstream << PREF_FORCE_SEQUENTIAL << "=" << toBoolArg(optarg) << "\n";
  391. break;
  392. case 'P':
  393. cmdstream << PREF_PARAMETERIZED_URI << "=" << toBoolArg(optarg) << "\n";
  394. break;
  395. case 'v':
  396. showVersion();
  397. exit(EXIT_SUCCESS);
  398. case 'h':
  399. showUsage();
  400. exit(EXIT_SUCCESS);
  401. default:
  402. exit(EXIT_FAILURE);
  403. }
  404. }
  405. {
  406. OptionParser oparser;
  407. oparser.setOptionHandlers(OptionHandlerFactory::createOptionHandlers());
  408. string cfname = Util::getHomeDir()+"/.aria2/aria2.conf";
  409. ifstream cfstream(cfname.c_str());
  410. try {
  411. oparser.parse(op, cfstream);
  412. } catch(Exception* e) {
  413. cerr << "Parse error in " << cfname << endl;
  414. cerr << *e << endl;
  415. delete e;
  416. exit(EXIT_FAILURE);
  417. }
  418. try {
  419. oparser.parse(op, cmdstream);
  420. } catch(Exception* e) {
  421. cerr << *e << endl;
  422. delete e;
  423. exit(EXIT_FAILURE);
  424. }
  425. }
  426. if(op->defined(PREF_HTTP_USER)) {
  427. op->put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  428. }
  429. if(op->defined(PREF_HTTP_PROXY_USER)) {
  430. op->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  431. }
  432. if(
  433. #ifdef ENABLE_BITTORRENT
  434. !op->defined(PREF_TORRENT_FILE) &&
  435. #endif // ENABLE_BITTORRENT
  436. #ifdef ENABLE_METALINK
  437. !op->defined(PREF_METALINK_FILE) &&
  438. #endif // ENABLE_METALINK
  439. !op->defined(PREF_INPUT_FILE)) {
  440. if(optind == argc) {
  441. cerr << MSG_URI_REQUIRED << endl;
  442. exit(EXIT_FAILURE);
  443. }
  444. }
  445. #ifdef HAVE_DAEMON
  446. if(op->getAsBool(PREF_DAEMON)) {
  447. if(daemon(1, 1) < 0) {
  448. perror(MSG_DAEMON_FAILED);
  449. exit(EXIT_FAILURE);
  450. }
  451. }
  452. #endif // HAVE_DAEMON
  453. return op;
  454. }