option_processing.cc 17 KB

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