option_processing.cc 20 KB

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