option_processing.cc 18 KB

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