option_processing.cc 21 KB

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