option_processing.cc 22 KB

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