option_processing.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 "OptionHandler.h"
  41. #include "Util.h"
  42. #include "message.h"
  43. #include "Exception.h"
  44. #include "a2io.h"
  45. #include "help_tags.h"
  46. #include "File.h"
  47. #include <cstdlib>
  48. #include <fstream>
  49. #include <sstream>
  50. #include <iostream>
  51. extern char* optarg;
  52. extern int optind, opterr, optopt;
  53. #include <getopt.h>
  54. namespace aria2 {
  55. extern void showVersion();
  56. extern void showUsage(const std::string& category, const Option* option);
  57. static std::string toBoolArg(const char* optarg)
  58. {
  59. std::string arg;
  60. if(!optarg || std::string(optarg) == "") {
  61. arg = V_TRUE;
  62. } else {
  63. arg = optarg;
  64. }
  65. return arg;
  66. }
  67. Option* createDefaultOption()
  68. {
  69. Option* op = new Option();
  70. op->put(PREF_STDOUT_LOG, V_FALSE);
  71. op->put(PREF_DIR, ".");
  72. op->put(PREF_SPLIT, "1");
  73. op->put(PREF_DAEMON, V_FALSE);
  74. op->put(PREF_SEGMENT_SIZE, Util::itos((int32_t)(1024*1024)));
  75. op->put(PREF_LISTEN_PORT, "6881-6999");
  76. op->put(PREF_METALINK_SERVERS, "5");
  77. op->put(PREF_FOLLOW_TORRENT,
  78. #ifdef ENABLE_BITTORRENT
  79. V_TRUE
  80. #else
  81. V_FALSE
  82. #endif // ENABLE_BITTORRENT
  83. );
  84. op->put(PREF_FOLLOW_METALINK,
  85. #ifdef ENABLE_METALINK
  86. V_TRUE
  87. #else
  88. V_FALSE
  89. #endif // ENABLE_METALINK
  90. );
  91. op->put(PREF_RETRY_WAIT, "5");
  92. op->put(PREF_TIMEOUT, "60");
  93. op->put(PREF_DNS_TIMEOUT, "30");
  94. op->put(PREF_PEER_CONNECTION_TIMEOUT, "20");
  95. op->put(PREF_BT_TIMEOUT, "180");
  96. op->put(PREF_BT_REQUEST_TIMEOUT, "60");
  97. op->put(PREF_BT_KEEP_ALIVE_INTERVAL, "120");
  98. op->put(PREF_MIN_SEGMENT_SIZE, "1048576");// 1M
  99. op->put(PREF_MAX_TRIES, "5");
  100. op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
  101. op->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  102. op->put(PREF_FTP_TYPE, V_BINARY);
  103. op->put(PREF_FTP_VIA_HTTP_PROXY, V_TUNNEL);
  104. op->put(PREF_AUTO_SAVE_INTERVAL, "60");
  105. op->put(PREF_DIRECT_FILE_MAPPING, V_TRUE);
  106. op->put(PREF_LOWEST_SPEED_LIMIT, "0");
  107. op->put(PREF_MAX_DOWNLOAD_LIMIT, "0");
  108. op->put(PREF_MAX_UPLOAD_LIMIT, "0");
  109. op->put(PREF_STARTUP_IDLE_TIME, "10");
  110. op->put(PREF_TRACKER_MAX_TRIES, "10");
  111. op->put(PREF_FILE_ALLOCATION, V_PREALLOC);
  112. op->put(PREF_NO_FILE_ALLOCATION_LIMIT, "5242880"); // 5MiB
  113. op->put(PREF_ALLOW_OVERWRITE, V_FALSE);
  114. op->put(PREF_REALTIME_CHUNK_CHECKSUM, V_TRUE);
  115. op->put(PREF_CHECK_INTEGRITY, V_FALSE);
  116. op->put(PREF_NETRC_PATH, Util::getHomeDir()+"/.netrc");
  117. op->put(PREF_CONTINUE, V_FALSE);
  118. op->put(PREF_USER_AGENT, "aria2");
  119. op->put(PREF_NO_NETRC, V_FALSE);
  120. op->put(PREF_MAX_CONCURRENT_DOWNLOADS, "5");
  121. op->put(PREF_DIRECT_DOWNLOAD_TIMEOUT, "300");
  122. op->put(PREF_FORCE_SEQUENTIAL, V_FALSE);
  123. op->put(PREF_AUTO_FILE_RENAMING, V_TRUE);
  124. op->put(PREF_PARAMETERIZED_URI, V_FALSE);
  125. op->put(PREF_ENABLE_HTTP_KEEP_ALIVE, V_FALSE);
  126. op->put(PREF_ENABLE_HTTP_PIPELINING, V_FALSE);
  127. op->put(PREF_MAX_HTTP_PIPELINING, "2");
  128. op->put(PREF_SEED_RATIO, "1.0");
  129. op->put(PREF_ENABLE_DIRECT_IO, V_FALSE);
  130. op->put(PREF_ALLOW_PIECE_LENGTH_CHANGE, V_FALSE);
  131. op->put(PREF_METALINK_PREFERRED_PROTOCOL, V_NONE);
  132. op->put(PREF_METALINK_ENABLE_UNIQUE_PROTOCOL, V_TRUE);
  133. op->put(PREF_ENABLE_PEER_EXCHANGE, V_TRUE);
  134. op->put(PREF_ENABLE_DHT, V_FALSE);
  135. op->put(PREF_DHT_LISTEN_PORT, "6881-6999");
  136. op->put(PREF_DHT_FILE_PATH, Util::getHomeDir()+"/.aria2/dht.dat");
  137. op->put(PREF_BT_MIN_CRYPTO_LEVEL, V_PLAIN);
  138. op->put(PREF_BT_REQUIRE_CRYPTO, V_FALSE);
  139. op->put(PREF_QUIET, V_FALSE);
  140. op->put(PREF_STOP, "0");
  141. return op;
  142. }
  143. Option* option_processing(int argc, char* const argv[])
  144. {
  145. std::stringstream cmdstream;
  146. int32_t c;
  147. Option* op = createDefaultOption();
  148. // following options are not parsed by OptionHandler and not stored in Option.
  149. bool noConf = false;
  150. std::string defaultCfname = Util::getHomeDir()+"/.aria2/aria2.conf";
  151. std::string ucfname;
  152. while(1) {
  153. int optIndex = 0;
  154. int lopt;
  155. static struct option longOpts[] = {
  156. #ifdef HAVE_DAEMON
  157. { PREF_DAEMON, no_argument, NULL, 'D' },
  158. #endif // HAVE_DAEMON
  159. { PREF_DIR, required_argument, NULL, 'd' },
  160. { PREF_OUT, required_argument, NULL, 'o' },
  161. { PREF_LOG, required_argument, NULL, 'l' },
  162. { PREF_SPLIT, required_argument, NULL, 's' },
  163. { PREF_TIMEOUT, required_argument, NULL, 't' },
  164. { PREF_MAX_TRIES, required_argument, NULL, 'm' },
  165. { PREF_HTTP_PROXY, required_argument, &lopt, 1 },
  166. { PREF_HTTP_USER, required_argument, &lopt, 2 },
  167. { PREF_HTTP_PASSWD, required_argument, &lopt, 3 },
  168. { PREF_HTTP_PROXY_USER, required_argument, &lopt, 4 },
  169. { PREF_HTTP_PROXY_PASSWD, required_argument, &lopt, 5 },
  170. { PREF_HTTP_AUTH_SCHEME, required_argument, &lopt, 6 },
  171. { PREF_REFERER, required_argument, &lopt, 7 },
  172. { PREF_RETRY_WAIT, required_argument, &lopt, 8 },
  173. { PREF_FTP_USER, required_argument, &lopt, 9 },
  174. { PREF_FTP_PASSWD, required_argument, &lopt, 10 },
  175. { PREF_FTP_TYPE, required_argument, &lopt, 11 },
  176. { PREF_FTP_PASV, no_argument, NULL, 'p' },
  177. { PREF_FTP_VIA_HTTP_PROXY, required_argument, &lopt, 12 },
  178. //{ PREF_MIN_SEGMENT_SIZE, required_argument, &lopt, 13 },
  179. { PREF_HTTP_PROXY_METHOD, required_argument, &lopt, 14 },
  180. { PREF_LOWEST_SPEED_LIMIT, required_argument, &lopt, 200 },
  181. { PREF_MAX_DOWNLOAD_LIMIT, required_argument, &lopt, 201 },
  182. { PREF_FILE_ALLOCATION, required_argument, 0, 'a' },
  183. { PREF_ALLOW_OVERWRITE, required_argument, &lopt, 202 },
  184. #ifdef ENABLE_MESSAGE_DIGEST
  185. { PREF_CHECK_INTEGRITY, required_argument, &lopt, 203 },
  186. { PREF_REALTIME_CHUNK_CHECKSUM, required_argument, &lopt, 204 },
  187. #endif // ENABLE_MESSAGE_DIGEST
  188. { PREF_CONTINUE, no_argument, 0, 'c' },
  189. { PREF_USER_AGENT, required_argument, 0, 'U' },
  190. { PREF_NO_NETRC, no_argument, 0, 'n' },
  191. { PREF_INPUT_FILE, required_argument, 0, 'i' },
  192. { PREF_MAX_CONCURRENT_DOWNLOADS, required_argument, 0, 'j' },
  193. { PREF_LOAD_COOKIES, required_argument, &lopt, 205 },
  194. { PREF_FORCE_SEQUENTIAL, optional_argument, 0, 'Z' },
  195. { PREF_AUTO_FILE_RENAMING, optional_argument, &lopt, 206 },
  196. { PREF_PARAMETERIZED_URI, optional_argument, 0, 'P' },
  197. { PREF_ENABLE_HTTP_KEEP_ALIVE, optional_argument, &lopt, 207 },
  198. { PREF_ENABLE_HTTP_PIPELINING, optional_argument, &lopt, 208 },
  199. { PREF_NO_FILE_ALLOCATION_LIMIT, required_argument, &lopt, 209 },
  200. #ifdef ENABLE_DIRECT_IO
  201. { PREF_ENABLE_DIRECT_IO, optional_argument, &lopt, 210 },
  202. #endif // ENABLE_DIRECT_IO
  203. { PREF_ALLOW_PIECE_LENGTH_CHANGE, required_argument, &lopt, 211 },
  204. { PREF_NO_CONF, no_argument, &lopt, 212 },
  205. { PREF_CONF_PATH, required_argument, &lopt, 213 },
  206. { PREF_STOP, required_argument, &lopt, 214 },
  207. { PREF_HEADER, required_argument, &lopt, 215 },
  208. { PREF_QUIET, optional_argument, 0, 'q' },
  209. #if defined ENABLE_BITTORRENT || ENABLE_METALINK
  210. { PREF_SHOW_FILES, no_argument, NULL, 'S' },
  211. { PREF_SELECT_FILE, required_argument, &lopt, 21 },
  212. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  213. #ifdef ENABLE_BITTORRENT
  214. { PREF_TORRENT_FILE, required_argument, NULL, 'T' },
  215. { PREF_LISTEN_PORT, required_argument, &lopt, 15 },
  216. { PREF_FOLLOW_TORRENT, required_argument, &lopt, 16 },
  217. { PREF_NO_PREALLOCATION, no_argument, &lopt, 18 },
  218. { PREF_DIRECT_FILE_MAPPING, required_argument, &lopt, 19 },
  219. // TODO remove upload-limit.
  220. //{ "upload-limit", required_argument, &lopt, 20 },
  221. { PREF_SEED_TIME, required_argument, &lopt, 22 },
  222. { PREF_SEED_RATIO, required_argument, &lopt, 23 },
  223. { PREF_MAX_UPLOAD_LIMIT, required_argument, &lopt, 24 },
  224. { PREF_PEER_ID_PREFIX, required_argument, &lopt, 25 },
  225. { PREF_ENABLE_PEER_EXCHANGE, optional_argument, &lopt, 26 },
  226. { PREF_ENABLE_DHT, optional_argument, &lopt, 27 },
  227. { PREF_DHT_LISTEN_PORT, required_argument, &lopt, 28 },
  228. { PREF_DHT_ENTRY_POINT, required_argument, &lopt, 29 },
  229. { PREF_BT_MIN_CRYPTO_LEVEL, required_argument, &lopt, 30 },
  230. { PREF_BT_REQUIRE_CRYPTO, required_argument, &lopt, 31 },
  231. #endif // ENABLE_BITTORRENT
  232. #ifdef ENABLE_METALINK
  233. { PREF_METALINK_FILE, required_argument, NULL, 'M' },
  234. { PREF_METALINK_SERVERS, required_argument, NULL, 'C' },
  235. { PREF_METALINK_VERSION, required_argument, &lopt, 100 },
  236. { PREF_METALINK_LANGUAGE, required_argument, &lopt, 101 },
  237. { PREF_METALINK_OS, required_argument, &lopt, 102 },
  238. { PREF_FOLLOW_METALINK, required_argument, &lopt, 103 },
  239. { PREF_METALINK_LOCATION, required_argument, &lopt, 104 },
  240. { PREF_METALINK_PREFERRED_PROTOCOL, required_argument, &lopt, 105 },
  241. { PREF_METALINK_ENABLE_UNIQUE_PROTOCOL, optional_argument, &lopt, 106 },
  242. #endif // ENABLE_METALINK
  243. { "version", no_argument, NULL, 'v' },
  244. { "help", optional_argument, NULL, 'h' },
  245. { 0, 0, 0, 0 }
  246. };
  247. c = getopt_long(argc, argv, "Dd:o:l:s:pt:m:vh::ST:M:C:a:cU:ni:j:Z::P::q::", longOpts, &optIndex);
  248. if(c == -1) {
  249. break;
  250. }
  251. switch(c) {
  252. case 0:{
  253. switch(lopt) {
  254. case 1:
  255. cmdstream << PREF_HTTP_PROXY << "=" << optarg << "\n";
  256. break;
  257. case 2:
  258. cmdstream << PREF_HTTP_USER << "=" << optarg << "\n";
  259. break;
  260. case 3:
  261. cmdstream << PREF_HTTP_PASSWD << "=" << optarg << "\n";
  262. break;
  263. case 4:
  264. cmdstream << PREF_HTTP_PROXY_USER << "=" << optarg << "\n";
  265. break;
  266. case 5:
  267. cmdstream << PREF_HTTP_PROXY_PASSWD << "=" << optarg << "\n";
  268. break;
  269. case 6:
  270. cmdstream << PREF_HTTP_AUTH_SCHEME << "=" << optarg << "\n";
  271. break;
  272. case 7:
  273. cmdstream << PREF_REFERER << "=" << optarg << "\n";
  274. break;
  275. case 8:
  276. cmdstream << PREF_RETRY_WAIT << "=" << optarg << "\n";
  277. break;
  278. case 9:
  279. cmdstream << PREF_FTP_USER << "=" << optarg << "\n";
  280. break;
  281. case 10:
  282. cmdstream << PREF_FTP_PASSWD << "=" << optarg << "\n";
  283. break;
  284. case 11:
  285. cmdstream << PREF_FTP_TYPE << "=" << optarg << "\n";
  286. break;
  287. case 12:
  288. cmdstream << PREF_FTP_VIA_HTTP_PROXY << "=" << optarg << "\n";
  289. break;
  290. case 13:
  291. cmdstream << PREF_MIN_SEGMENT_SIZE << "=" << optarg << "\n";
  292. break;
  293. case 14:
  294. cmdstream << PREF_HTTP_PROXY_METHOD << "=" << optarg << "\n";
  295. break;
  296. case 15:
  297. cmdstream << PREF_LISTEN_PORT << "=" << optarg << "\n";
  298. break;
  299. case 16:
  300. cmdstream << PREF_FOLLOW_TORRENT << "=" << optarg << "\n";
  301. break;
  302. case 18:
  303. cmdstream << PREF_NO_PREALLOCATION << "=" << V_TRUE << "\n";
  304. break;
  305. case 19:
  306. cmdstream << PREF_DIRECT_FILE_MAPPING << "=" << optarg << "\n";
  307. break;
  308. case 21:
  309. cmdstream << PREF_SELECT_FILE << "=" << optarg << "\n";
  310. break;
  311. case 22:
  312. cmdstream << PREF_SEED_TIME << "=" << optarg << "\n";
  313. break;
  314. case 23:
  315. cmdstream << PREF_SEED_RATIO << "=" << optarg << "\n";
  316. break;
  317. case 24:
  318. cmdstream << PREF_MAX_UPLOAD_LIMIT << "=" << optarg << "\n";
  319. break;
  320. case 25:
  321. cmdstream << PREF_PEER_ID_PREFIX << "=" << optarg << "\n";
  322. break;
  323. case 26:
  324. cmdstream << PREF_ENABLE_PEER_EXCHANGE << "=" << toBoolArg(optarg) << "\n";
  325. break;
  326. case 27:
  327. cmdstream << PREF_ENABLE_DHT << "=" << toBoolArg(optarg) << "\n";
  328. break;
  329. case 28:
  330. cmdstream << PREF_DHT_LISTEN_PORT << "=" << optarg << "\n";
  331. break;
  332. case 29:
  333. cmdstream << PREF_DHT_ENTRY_POINT << "=" << optarg << "\n";
  334. break;
  335. case 30:
  336. cmdstream << PREF_BT_MIN_CRYPTO_LEVEL << "=" << optarg << "\n";
  337. break;
  338. case 31:
  339. cmdstream << PREF_BT_REQUIRE_CRYPTO << "=" << optarg << "\n";
  340. break;
  341. case 100:
  342. cmdstream << PREF_METALINK_VERSION << "=" << optarg << "\n";
  343. break;
  344. case 101:
  345. cmdstream << PREF_METALINK_LANGUAGE << "=" << optarg << "\n";
  346. break;
  347. case 102:
  348. cmdstream << PREF_METALINK_OS << "=" << optarg << "\n";
  349. break;
  350. case 103:
  351. cmdstream << PREF_FOLLOW_METALINK << "=" << optarg << "\n";
  352. break;
  353. case 104:
  354. cmdstream << PREF_METALINK_LOCATION << "=" << optarg << "\n";
  355. break;
  356. case 105:
  357. cmdstream << PREF_METALINK_PREFERRED_PROTOCOL << "=" << optarg << "\n";
  358. break;
  359. case 106:
  360. cmdstream << PREF_METALINK_ENABLE_UNIQUE_PROTOCOL << "=" << toBoolArg(optarg) << "\n";
  361. break;
  362. case 200:
  363. cmdstream << PREF_LOWEST_SPEED_LIMIT << "=" << optarg << "\n";
  364. break;
  365. case 201:
  366. cmdstream << PREF_MAX_DOWNLOAD_LIMIT << "=" << optarg << "\n";
  367. break;
  368. case 202:
  369. cmdstream << PREF_ALLOW_OVERWRITE << "=" << optarg << "\n";
  370. break;
  371. case 203:
  372. cmdstream << PREF_CHECK_INTEGRITY << "=" << optarg << "\n";
  373. break;
  374. case 204:
  375. cmdstream << PREF_REALTIME_CHUNK_CHECKSUM << "=" << optarg << "\n";
  376. break;
  377. case 205:
  378. cmdstream << PREF_LOAD_COOKIES << "=" << optarg << "\n";
  379. break;
  380. case 206:
  381. cmdstream << PREF_AUTO_FILE_RENAMING << "=" << toBoolArg(optarg) << "\n";
  382. break;
  383. case 207:
  384. cmdstream << PREF_ENABLE_HTTP_KEEP_ALIVE << "=" << toBoolArg(optarg) << "\n";
  385. break;
  386. case 208:
  387. cmdstream << PREF_ENABLE_HTTP_PIPELINING << "=" << toBoolArg(optarg) << "\n";
  388. break;
  389. case 209:
  390. cmdstream << PREF_NO_FILE_ALLOCATION_LIMIT << "=" << optarg << "\n";
  391. break;
  392. case 210:
  393. cmdstream << PREF_ENABLE_DIRECT_IO << "=" << toBoolArg(optarg) << "\n";
  394. break;
  395. case 211:
  396. cmdstream << PREF_ALLOW_PIECE_LENGTH_CHANGE << "=" << optarg << "\n";
  397. break;
  398. case 212:
  399. noConf = true;
  400. break;
  401. case 213:
  402. ucfname = optarg;
  403. break;
  404. case 214:
  405. cmdstream << PREF_STOP << "=" << optarg << "\n";
  406. break;
  407. case 215:
  408. cmdstream << PREF_HEADER << "=" << optarg << "\n";
  409. break;
  410. }
  411. break;
  412. }
  413. #ifdef HAVE_DAEMON
  414. case 'D':
  415. cmdstream << PREF_DAEMON << "=" << V_TRUE << "\n";
  416. break;
  417. #endif // HAVE_DAEMON
  418. case 'd':
  419. cmdstream << PREF_DIR << "=" << optarg << "\n";
  420. break;
  421. case 'o':
  422. cmdstream << PREF_OUT << "=" << optarg << "\n";
  423. break;
  424. case 'l':
  425. cmdstream << PREF_LOG << "=" << optarg << "\n";
  426. break;
  427. case 's':
  428. cmdstream << PREF_SPLIT << "=" << optarg << "\n";
  429. break;
  430. case 't':
  431. cmdstream << PREF_TIMEOUT << "=" << optarg << "\n";
  432. break;
  433. case 'm':
  434. cmdstream << PREF_MAX_TRIES << "=" << optarg << "\n";
  435. break;
  436. case 'p':
  437. cmdstream << PREF_FTP_PASV << "=" << V_TRUE << "\n";
  438. break;
  439. case 'S':
  440. cmdstream << PREF_SHOW_FILES << "=" << V_TRUE << "\n";
  441. break;
  442. case 'T':
  443. cmdstream << PREF_TORRENT_FILE << "=" << optarg << "\n";
  444. break;
  445. case 'M':
  446. cmdstream << PREF_METALINK_FILE << "=" << optarg << "\n";
  447. break;
  448. case 'C':
  449. cmdstream << PREF_METALINK_SERVERS << "=" << optarg << "\n";
  450. break;
  451. case 'a':
  452. cmdstream << PREF_FILE_ALLOCATION << "=" << optarg << "\n";
  453. break;
  454. case 'c':
  455. cmdstream << PREF_CONTINUE << "=" << V_TRUE << "\n";
  456. break;
  457. case 'U':
  458. cmdstream << PREF_USER_AGENT << "=" << optarg << "\n";
  459. break;
  460. case 'n':
  461. cmdstream << PREF_NO_NETRC << "=" << V_TRUE << "\n";
  462. break;
  463. case 'i':
  464. cmdstream << PREF_INPUT_FILE << "=" << optarg << "\n";
  465. break;
  466. case 'j':
  467. cmdstream << PREF_MAX_CONCURRENT_DOWNLOADS << "=" << optarg << "\n";
  468. break;
  469. case 'Z':
  470. cmdstream << PREF_FORCE_SEQUENTIAL << "=" << toBoolArg(optarg) << "\n";
  471. break;
  472. case 'P':
  473. cmdstream << PREF_PARAMETERIZED_URI << "=" << toBoolArg(optarg) << "\n";
  474. break;
  475. case 'q':
  476. cmdstream << PREF_QUIET << "=" << toBoolArg(optarg) << "\n";
  477. break;
  478. case 'v':
  479. showVersion();
  480. exit(EXIT_SUCCESS);
  481. case 'h':
  482. {
  483. std::string category;
  484. if(optarg == 0 || std::string(optarg) == "") {
  485. category = TAG_BASIC;
  486. } else {
  487. category = optarg;
  488. }
  489. showUsage(category, createDefaultOption());
  490. exit(EXIT_SUCCESS);
  491. }
  492. default:
  493. exit(EXIT_FAILURE);
  494. }
  495. }
  496. {
  497. OptionParser oparser;
  498. oparser.setOptionHandlers(OptionHandlerFactory::createOptionHandlers());
  499. if(!noConf) {
  500. std::string cfname;
  501. if(ucfname.size()) {
  502. cfname = ucfname;
  503. } else {
  504. cfname = defaultCfname;
  505. }
  506. if(File(cfname).isFile()) {
  507. std::ifstream cfstream(cfname.c_str());
  508. try {
  509. oparser.parse(op, cfstream);
  510. } catch(Exception* e) {
  511. std::cerr << "Parse error in " << cfname << "\n"
  512. << *e << std::endl;
  513. delete e;
  514. exit(EXIT_FAILURE);
  515. }
  516. } else if(ucfname.size()) {
  517. printf("Configuration file %s is not found.", cfname.c_str());
  518. std::cout << "\n";
  519. }
  520. }
  521. try {
  522. oparser.parse(op, cmdstream);
  523. } catch(Exception* e) {
  524. std::cerr << *e << std::endl;
  525. delete e;
  526. exit(EXIT_FAILURE);
  527. }
  528. }
  529. if(op->defined(PREF_HTTP_USER)) {
  530. op->put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  531. }
  532. if(op->defined(PREF_HTTP_PROXY_USER)) {
  533. op->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  534. }
  535. if(
  536. #ifdef ENABLE_BITTORRENT
  537. !op->defined(PREF_TORRENT_FILE) &&
  538. #endif // ENABLE_BITTORRENT
  539. #ifdef ENABLE_METALINK
  540. !op->defined(PREF_METALINK_FILE) &&
  541. #endif // ENABLE_METALINK
  542. !op->defined(PREF_INPUT_FILE)) {
  543. if(optind == argc) {
  544. std::cerr << MSG_URI_REQUIRED << std::endl;
  545. exit(EXIT_FAILURE);
  546. }
  547. }
  548. #ifdef HAVE_DAEMON
  549. if(op->getAsBool(PREF_DAEMON)) {
  550. if(daemon(1, 1) < 0) {
  551. perror(MSG_DAEMON_FAILED);
  552. exit(EXIT_FAILURE);
  553. }
  554. }
  555. #endif // HAVE_DAEMON
  556. return op;
  557. }
  558. } // namespace aria2