option_processing.cc 18 KB

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