option_processing.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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 <cstdlib>
  37. #include <cstring>
  38. #include <fstream>
  39. #include <sstream>
  40. #include <iostream>
  41. #include "Option.h"
  42. #include "prefs.h"
  43. #include "OptionParser.h"
  44. #include "OptionHandlerFactory.h"
  45. #include "OptionHandler.h"
  46. #include "Util.h"
  47. #include "message.h"
  48. #include "Exception.h"
  49. #include "a2io.h"
  50. #include "help_tags.h"
  51. #include "File.h"
  52. #include "StringFormat.h"
  53. #include "OptionHandlerException.h"
  54. #include "DownloadResult.h"
  55. extern char* optarg;
  56. extern int optind, opterr, optopt;
  57. #include <getopt.h>
  58. namespace aria2 {
  59. extern void showVersion();
  60. extern void showUsage(const std::string& keyword, const OptionParser& oparser);
  61. static std::string toBoolArg(const char* optarg)
  62. {
  63. std::string arg;
  64. if(!optarg || strlen(optarg) == 0) {
  65. arg = V_TRUE;
  66. } else {
  67. arg = optarg;
  68. }
  69. return arg;
  70. }
  71. static void overrideWithEnv(Option* op, const OptionParser& optionParser,
  72. const std::string& pref,
  73. const std::string& envName)
  74. {
  75. char* value = getenv(envName.c_str());
  76. if(value) {
  77. try {
  78. optionParser.findByName(pref)->parse(op, value);
  79. } catch(Exception& e) {
  80. std::cerr << "Caught Error while parsing environment variable"
  81. << " '" << envName << "'"
  82. << "\n"
  83. << e.stackTrace();
  84. }
  85. }
  86. }
  87. Option* option_processing(int argc, char* const argv[])
  88. {
  89. std::stringstream cmdstream;
  90. int32_t c;
  91. Option* op = new Option();
  92. // following options are not parsed by OptionHandler and not stored in Option.
  93. bool noConf = false;
  94. std::string ucfname;
  95. OptionParser oparser;
  96. oparser.setOptionHandlers(OptionHandlerFactory::createOptionHandlers());
  97. try {
  98. oparser.parseDefaultValues(op);
  99. } catch(Exception& e) {
  100. std::cerr << e.stackTrace();
  101. exit(DownloadResult::UNKNOWN_ERROR);
  102. }
  103. while(1) {
  104. int optIndex = 0;
  105. int lopt;
  106. static struct option longOpts[] = {
  107. #ifdef HAVE_DAEMON
  108. { PREF_DAEMON.c_str(), no_argument, NULL, 'D' },
  109. #endif // HAVE_DAEMON
  110. { PREF_DIR.c_str(), required_argument, NULL, 'd' },
  111. { PREF_OUT.c_str(), required_argument, NULL, 'o' },
  112. { PREF_LOG.c_str(), required_argument, NULL, 'l' },
  113. { PREF_SPLIT.c_str(), required_argument, NULL, 's' },
  114. { PREF_TIMEOUT.c_str(), required_argument, NULL, 't' },
  115. { PREF_MAX_TRIES.c_str(), required_argument, NULL, 'm' },
  116. { PREF_HTTP_PROXY.c_str(), required_argument, &lopt, 1 },
  117. { PREF_HTTP_USER.c_str(), required_argument, &lopt, 2 },
  118. { PREF_HTTP_PASSWD.c_str(), required_argument, &lopt, 3 },
  119. { "http-proxy-user", required_argument, &lopt, 4 },
  120. { "http-proxy-passwd", required_argument, &lopt, 5 },
  121. { PREF_HTTP_AUTH_SCHEME.c_str(), required_argument, &lopt, 6 },
  122. { PREF_REFERER.c_str(), required_argument, &lopt, 7 },
  123. { PREF_RETRY_WAIT.c_str(), required_argument, &lopt, 8 },
  124. { PREF_FTP_USER.c_str(), required_argument, &lopt, 9 },
  125. { PREF_FTP_PASSWD.c_str(), required_argument, &lopt, 10 },
  126. { PREF_FTP_TYPE.c_str(), required_argument, &lopt, 11 },
  127. { PREF_FTP_PASV.c_str(), optional_argument, 0, 'p' },
  128. { "ftp-via-http-proxy", required_argument, &lopt, 12 },
  129. { "http-proxy-method", required_argument, &lopt, 14 },
  130. { PREF_LOWEST_SPEED_LIMIT.c_str(), required_argument, &lopt, 200 },
  131. { PREF_MAX_DOWNLOAD_LIMIT.c_str(), required_argument, &lopt, 201 },
  132. { PREF_FILE_ALLOCATION.c_str(), required_argument, 0, 'a' },
  133. { PREF_ALLOW_OVERWRITE.c_str(), required_argument, &lopt, 202 },
  134. #ifdef ENABLE_MESSAGE_DIGEST
  135. { PREF_CHECK_INTEGRITY.c_str(), optional_argument, 0, 'V' },
  136. { PREF_REALTIME_CHUNK_CHECKSUM.c_str(), required_argument, &lopt, 204 },
  137. #endif // ENABLE_MESSAGE_DIGEST
  138. { PREF_CONTINUE.c_str(), no_argument, 0, 'c' },
  139. { PREF_USER_AGENT.c_str(), required_argument, 0, 'U' },
  140. { PREF_NO_NETRC.c_str(), no_argument, 0, 'n' },
  141. { PREF_INPUT_FILE.c_str(), required_argument, 0, 'i' },
  142. { PREF_MAX_CONCURRENT_DOWNLOADS.c_str(), required_argument, 0, 'j' },
  143. { PREF_LOAD_COOKIES.c_str(), required_argument, &lopt, 205 },
  144. { PREF_FORCE_SEQUENTIAL.c_str(), optional_argument, 0, 'Z' },
  145. { PREF_AUTO_FILE_RENAMING.c_str(), optional_argument, &lopt, 206 },
  146. { PREF_PARAMETERIZED_URI.c_str(), optional_argument, 0, 'P' },
  147. { PREF_ENABLE_HTTP_KEEP_ALIVE.c_str(), optional_argument, &lopt, 207 },
  148. { PREF_ENABLE_HTTP_PIPELINING.c_str(), optional_argument, &lopt, 208 },
  149. { PREF_NO_FILE_ALLOCATION_LIMIT.c_str(), required_argument, &lopt, 209 },
  150. #ifdef ENABLE_DIRECT_IO
  151. { PREF_ENABLE_DIRECT_IO.c_str(), optional_argument, &lopt, 210 },
  152. #endif // ENABLE_DIRECT_IO
  153. { PREF_ALLOW_PIECE_LENGTH_CHANGE.c_str(), required_argument, &lopt, 211 },
  154. { PREF_NO_CONF.c_str(), no_argument, &lopt, 212 },
  155. { PREF_CONF_PATH.c_str(), required_argument, &lopt, 213 },
  156. { PREF_STOP.c_str(), required_argument, &lopt, 214 },
  157. { PREF_HEADER.c_str(), required_argument, &lopt, 215 },
  158. { PREF_QUIET.c_str(), optional_argument, 0, 'q' },
  159. #ifdef ENABLE_ASYNC_DNS
  160. { PREF_ASYNC_DNS.c_str(), optional_argument, &lopt, 216 },
  161. #endif // ENABLE_ASYNC_DNS
  162. { PREF_FTP_REUSE_CONNECTION.c_str(), optional_argument, &lopt, 217 },
  163. { PREF_SUMMARY_INTERVAL.c_str(), required_argument, &lopt, 218 },
  164. { PREF_LOG_LEVEL.c_str(), required_argument, &lopt, 219 },
  165. { PREF_URI_SELECTOR.c_str(), required_argument, &lopt, 220 },
  166. { PREF_SERVER_STAT_IF.c_str(), required_argument, &lopt, 221 },
  167. { PREF_SERVER_STAT_OF.c_str(), required_argument, &lopt, 222 },
  168. { PREF_SERVER_STAT_TIMEOUT.c_str(), required_argument, &lopt, 223 },
  169. { PREF_REMOTE_TIME.c_str(), optional_argument, 0, 'R' },
  170. { PREF_CONNECT_TIMEOUT.c_str(), required_argument, &lopt, 224 },
  171. { PREF_MAX_FILE_NOT_FOUND.c_str(), required_argument, &lopt, 225 },
  172. { PREF_AUTO_SAVE_INTERVAL.c_str(), required_argument, &lopt, 226 },
  173. { PREF_HTTPS_PROXY.c_str(), required_argument, &lopt, 227 },
  174. { PREF_FTP_PROXY.c_str(), required_argument, &lopt, 228 },
  175. { PREF_ALL_PROXY.c_str(), required_argument, &lopt, 229 },
  176. { PREF_PROXY_METHOD.c_str(), required_argument, &lopt, 230 },
  177. { PREF_CERTIFICATE.c_str(), required_argument, &lopt, 231 },
  178. { PREF_PRIVATE_KEY.c_str(), required_argument, &lopt, 232 },
  179. { PREF_CA_CERTIFICATE.c_str(), optional_argument, &lopt, 233 },
  180. { PREF_CHECK_CERTIFICATE.c_str(), optional_argument, &lopt, 234 },
  181. { PREF_NO_PROXY.c_str(), required_argument, &lopt, 235 },
  182. { PREF_USE_HEAD.c_str(), optional_argument, &lopt, 236 },
  183. { PREF_EVENT_POLL.c_str(), required_argument, &lopt, 237 },
  184. #if defined ENABLE_BITTORRENT || defined ENABLE_METALINK
  185. { PREF_SHOW_FILES.c_str(), no_argument, NULL, 'S' },
  186. { PREF_SELECT_FILE.c_str(), required_argument, &lopt, 21 },
  187. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  188. #ifdef ENABLE_BITTORRENT
  189. { PREF_TORRENT_FILE.c_str(), required_argument, NULL, 'T' },
  190. { PREF_LISTEN_PORT.c_str(), required_argument, &lopt, 15 },
  191. { PREF_FOLLOW_TORRENT.c_str(), required_argument, &lopt, 16 },
  192. { PREF_DIRECT_FILE_MAPPING.c_str(), required_argument, &lopt, 19 },
  193. // TODO remove upload-limit.
  194. //{ "upload-limit".c_str(), required_argument, &lopt, 20 },
  195. { PREF_SEED_TIME.c_str(), required_argument, &lopt, 22 },
  196. { PREF_SEED_RATIO.c_str(), required_argument, &lopt, 23 },
  197. { PREF_MAX_UPLOAD_LIMIT.c_str(), required_argument, 0, 'u' },
  198. { PREF_PEER_ID_PREFIX.c_str(), required_argument, &lopt, 25 },
  199. { PREF_ENABLE_PEER_EXCHANGE.c_str(), optional_argument, &lopt, 26 },
  200. { PREF_ENABLE_DHT.c_str(), optional_argument, &lopt, 27 },
  201. { PREF_DHT_LISTEN_PORT.c_str(), required_argument, &lopt, 28 },
  202. { PREF_DHT_ENTRY_POINT.c_str(), required_argument, &lopt, 29 },
  203. { PREF_BT_MIN_CRYPTO_LEVEL.c_str(), required_argument, &lopt, 30 },
  204. { PREF_BT_REQUIRE_CRYPTO.c_str(), required_argument, &lopt, 31 },
  205. { PREF_BT_REQUEST_PEER_SPEED_LIMIT.c_str(), required_argument, &lopt, 32 },
  206. { PREF_BT_MAX_OPEN_FILES.c_str(), required_argument, &lopt, 33 },
  207. { PREF_BT_SEED_UNVERIFIED.c_str(), optional_argument, &lopt, 34 },
  208. { PREF_DHT_FILE_PATH.c_str(), required_argument, &lopt, 35 },
  209. { PREF_MAX_OVERALL_UPLOAD_LIMIT.c_str(), required_argument, &lopt, 36 },
  210. { PREF_BT_HASH_CHECK_SEED.c_str(), optional_argument, &lopt, 37 },
  211. { PREF_BT_MAX_PEERS.c_str(), required_argument, &lopt, 38 },
  212. #endif // ENABLE_BITTORRENT
  213. #ifdef ENABLE_METALINK
  214. { PREF_METALINK_FILE.c_str(), required_argument, NULL, 'M' },
  215. { PREF_METALINK_SERVERS.c_str(), required_argument, NULL, 'C' },
  216. { PREF_METALINK_VERSION.c_str(), required_argument, &lopt, 100 },
  217. { PREF_METALINK_LANGUAGE.c_str(), required_argument, &lopt, 101 },
  218. { PREF_METALINK_OS.c_str(), required_argument, &lopt, 102 },
  219. { PREF_FOLLOW_METALINK.c_str(), required_argument, &lopt, 103 },
  220. { PREF_METALINK_LOCATION.c_str(), required_argument, &lopt, 104 },
  221. { PREF_METALINK_PREFERRED_PROTOCOL.c_str(), required_argument, &lopt, 105 },
  222. { PREF_METALINK_ENABLE_UNIQUE_PROTOCOL.c_str(), optional_argument, &lopt, 106 },
  223. #endif // ENABLE_METALINK
  224. { "version", no_argument, NULL, 'v' },
  225. { "help", optional_argument, NULL, 'h' },
  226. { 0, 0, 0, 0 }
  227. };
  228. c = getopt_long(argc, argv,
  229. "Dd:o:l:s:p::t:m:vh::ST:M:C:a:cU:ni:j:Z::P::q::R::V::u:",
  230. longOpts, &optIndex);
  231. if(c == -1) {
  232. break;
  233. }
  234. switch(c) {
  235. case 0:{
  236. switch(lopt) {
  237. case 1:
  238. cmdstream << PREF_HTTP_PROXY << "=" << optarg << "\n";
  239. break;
  240. case 2:
  241. cmdstream << PREF_HTTP_USER << "=" << optarg << "\n";
  242. break;
  243. case 3:
  244. cmdstream << PREF_HTTP_PASSWD << "=" << optarg << "\n";
  245. break;
  246. case 4:
  247. std::cout << "--http-proxy-user was deprecated. See --http-proxy,"
  248. << " --https-proxy, --ftp-proxy, --all-proxy options."
  249. << std::endl;
  250. exit(DownloadResult::UNKNOWN_ERROR);
  251. case 5:
  252. std::cout << "--http-proxy-passwd was deprecated. See --http-proxy,"
  253. << " --https-proxy, --ftp-proxy, --all-proxy options."
  254. << std::endl;
  255. exit(DownloadResult::UNKNOWN_ERROR);
  256. case 6:
  257. cmdstream << PREF_HTTP_AUTH_SCHEME << "=" << optarg << "\n";
  258. break;
  259. case 7:
  260. cmdstream << PREF_REFERER << "=" << optarg << "\n";
  261. break;
  262. case 8:
  263. cmdstream << PREF_RETRY_WAIT << "=" << optarg << "\n";
  264. break;
  265. case 9:
  266. cmdstream << PREF_FTP_USER << "=" << optarg << "\n";
  267. break;
  268. case 10:
  269. cmdstream << PREF_FTP_PASSWD << "=" << optarg << "\n";
  270. break;
  271. case 11:
  272. cmdstream << PREF_FTP_TYPE << "=" << optarg << "\n";
  273. break;
  274. case 12:
  275. std::cout << "--ftp-via-http-proxy was deprecated."
  276. << " Use --http-proxy-method option instead."
  277. << std::endl;
  278. exit(DownloadResult::UNKNOWN_ERROR);
  279. case 14:
  280. std::cout << "--http-proxy-method was deprecated."
  281. << " Use --proxy-method option instead."
  282. << std::endl;
  283. exit(DownloadResult::UNKNOWN_ERROR);
  284. case 15:
  285. cmdstream << PREF_LISTEN_PORT << "=" << optarg << "\n";
  286. break;
  287. case 16:
  288. cmdstream << PREF_FOLLOW_TORRENT << "=" << optarg << "\n";
  289. break;
  290. case 19:
  291. cmdstream << PREF_DIRECT_FILE_MAPPING << "=" << optarg << "\n";
  292. break;
  293. case 21:
  294. cmdstream << PREF_SELECT_FILE << "=" << optarg << "\n";
  295. break;
  296. case 22:
  297. cmdstream << PREF_SEED_TIME << "=" << optarg << "\n";
  298. break;
  299. case 23:
  300. cmdstream << PREF_SEED_RATIO << "=" << optarg << "\n";
  301. break;
  302. case 25:
  303. cmdstream << PREF_PEER_ID_PREFIX << "=" << optarg << "\n";
  304. break;
  305. case 26:
  306. cmdstream << PREF_ENABLE_PEER_EXCHANGE << "=" << toBoolArg(optarg) << "\n";
  307. break;
  308. case 27:
  309. cmdstream << PREF_ENABLE_DHT << "=" << toBoolArg(optarg) << "\n";
  310. break;
  311. case 28:
  312. cmdstream << PREF_DHT_LISTEN_PORT << "=" << optarg << "\n";
  313. break;
  314. case 29:
  315. cmdstream << PREF_DHT_ENTRY_POINT << "=" << optarg << "\n";
  316. break;
  317. case 30:
  318. cmdstream << PREF_BT_MIN_CRYPTO_LEVEL << "=" << optarg << "\n";
  319. break;
  320. case 31:
  321. cmdstream << PREF_BT_REQUIRE_CRYPTO << "=" << optarg << "\n";
  322. break;
  323. case 32:
  324. cmdstream << PREF_BT_REQUEST_PEER_SPEED_LIMIT << "=" << optarg << "\n";
  325. break;
  326. case 33:
  327. cmdstream << PREF_BT_MAX_OPEN_FILES << "=" << optarg << "\n";
  328. break;
  329. case 34:
  330. cmdstream << PREF_BT_SEED_UNVERIFIED << "=" << toBoolArg(optarg)
  331. << "\n";
  332. break;
  333. case 35:
  334. cmdstream << PREF_DHT_FILE_PATH << "=" << optarg << "\n";
  335. break;
  336. case 36:
  337. cmdstream << PREF_MAX_OVERALL_UPLOAD_LIMIT << "=" << optarg << "\n";
  338. break;
  339. case 37:
  340. cmdstream << PREF_BT_HASH_CHECK_SEED << "=" << optarg << "\n";
  341. break;
  342. case 38:
  343. cmdstream << PREF_BT_MAX_PEERS << "=" << optarg << "\n";
  344. break;
  345. case 100:
  346. cmdstream << PREF_METALINK_VERSION << "=" << optarg << "\n";
  347. break;
  348. case 101:
  349. cmdstream << PREF_METALINK_LANGUAGE << "=" << optarg << "\n";
  350. break;
  351. case 102:
  352. cmdstream << PREF_METALINK_OS << "=" << optarg << "\n";
  353. break;
  354. case 103:
  355. cmdstream << PREF_FOLLOW_METALINK << "=" << optarg << "\n";
  356. break;
  357. case 104:
  358. cmdstream << PREF_METALINK_LOCATION << "=" << optarg << "\n";
  359. break;
  360. case 105:
  361. cmdstream << PREF_METALINK_PREFERRED_PROTOCOL << "=" << optarg << "\n";
  362. break;
  363. case 106:
  364. cmdstream << PREF_METALINK_ENABLE_UNIQUE_PROTOCOL << "=" << toBoolArg(optarg) << "\n";
  365. break;
  366. case 200:
  367. cmdstream << PREF_LOWEST_SPEED_LIMIT << "=" << optarg << "\n";
  368. break;
  369. case 201:
  370. cmdstream << PREF_MAX_DOWNLOAD_LIMIT << "=" << optarg << "\n";
  371. break;
  372. case 202:
  373. cmdstream << PREF_ALLOW_OVERWRITE << "=" << optarg << "\n";
  374. break;
  375. case 204:
  376. cmdstream << PREF_REALTIME_CHUNK_CHECKSUM << "=" << optarg << "\n";
  377. break;
  378. case 205:
  379. cmdstream << PREF_LOAD_COOKIES << "=" << optarg << "\n";
  380. break;
  381. case 206:
  382. cmdstream << PREF_AUTO_FILE_RENAMING << "=" << toBoolArg(optarg) << "\n";
  383. break;
  384. case 207:
  385. cmdstream << PREF_ENABLE_HTTP_KEEP_ALIVE << "=" << toBoolArg(optarg) << "\n";
  386. break;
  387. case 208:
  388. cmdstream << PREF_ENABLE_HTTP_PIPELINING << "=" << toBoolArg(optarg) << "\n";
  389. break;
  390. case 209:
  391. cmdstream << PREF_NO_FILE_ALLOCATION_LIMIT << "=" << optarg << "\n";
  392. break;
  393. case 210:
  394. cmdstream << PREF_ENABLE_DIRECT_IO << "=" << toBoolArg(optarg) << "\n";
  395. break;
  396. case 211:
  397. cmdstream << PREF_ALLOW_PIECE_LENGTH_CHANGE << "=" << optarg << "\n";
  398. break;
  399. case 212:
  400. noConf = true;
  401. break;
  402. case 213:
  403. ucfname = optarg;
  404. break;
  405. case 214:
  406. cmdstream << PREF_STOP << "=" << optarg << "\n";
  407. break;
  408. case 215:
  409. cmdstream << PREF_HEADER << "=" << optarg << "\n";
  410. break;
  411. #ifdef ENABLE_ASYNC_DNS
  412. case 216:
  413. cmdstream << PREF_ASYNC_DNS << "=" << toBoolArg(optarg) << "\n";
  414. break;
  415. #endif // ENABLE_ASYNC_DNS
  416. case 217:
  417. cmdstream << PREF_FTP_REUSE_CONNECTION << "=" << toBoolArg(optarg) << "\n";
  418. break;
  419. case 218:
  420. cmdstream << PREF_SUMMARY_INTERVAL << "=" << optarg << "\n";
  421. break;
  422. case 219:
  423. cmdstream << PREF_LOG_LEVEL << "=" << optarg << "\n";
  424. break;
  425. case 220:
  426. cmdstream << PREF_URI_SELECTOR << "=" << optarg << "\n";
  427. break;
  428. case 221:
  429. cmdstream << PREF_SERVER_STAT_IF << "=" << optarg << "\n";
  430. break;
  431. case 222:
  432. cmdstream << PREF_SERVER_STAT_OF << "=" << optarg << "\n";
  433. break;
  434. case 223:
  435. cmdstream << PREF_SERVER_STAT_TIMEOUT << "=" << optarg << "\n";
  436. break;
  437. case 224:
  438. cmdstream << PREF_CONNECT_TIMEOUT << "=" << optarg << "\n";
  439. break;
  440. case 225:
  441. cmdstream << PREF_MAX_FILE_NOT_FOUND << "=" << optarg << "\n";
  442. break;
  443. case 226:
  444. cmdstream << PREF_AUTO_SAVE_INTERVAL << "=" << optarg << "\n";
  445. break;
  446. case 227:
  447. cmdstream << PREF_HTTPS_PROXY << "=" << optarg << "\n";
  448. break;
  449. case 228:
  450. cmdstream << PREF_FTP_PROXY << "=" << optarg << "\n";
  451. break;
  452. case 229:
  453. cmdstream << PREF_ALL_PROXY << "=" << optarg << "\n";
  454. break;
  455. case 230:
  456. cmdstream << PREF_PROXY_METHOD << "=" << optarg << "\n";
  457. break;
  458. case 231:
  459. cmdstream << PREF_CERTIFICATE << "=" << optarg << "\n";
  460. break;
  461. case 232:
  462. cmdstream << PREF_PRIVATE_KEY << "=" << optarg << "\n";
  463. break;
  464. case 233:
  465. cmdstream << PREF_CA_CERTIFICATE << "=" << optarg << "\n";
  466. break;
  467. case 234:
  468. cmdstream << PREF_CHECK_CERTIFICATE << "=" << toBoolArg(optarg) << "\n";
  469. break;
  470. case 235:
  471. cmdstream << PREF_NO_PROXY << "=" << optarg << "\n";
  472. break;
  473. case 236:
  474. cmdstream << PREF_USE_HEAD << "=" << toBoolArg(optarg) << "\n";
  475. break;
  476. case 237:
  477. cmdstream << PREF_EVENT_POLL << "=" << optarg << "\n";
  478. break;
  479. }
  480. break;
  481. }
  482. #ifdef HAVE_DAEMON
  483. case 'D':
  484. cmdstream << PREF_DAEMON << "=" << V_TRUE << "\n";
  485. break;
  486. #endif // HAVE_DAEMON
  487. case 'd':
  488. cmdstream << PREF_DIR << "=" << optarg << "\n";
  489. break;
  490. case 'o':
  491. cmdstream << PREF_OUT << "=" << optarg << "\n";
  492. break;
  493. case 'l':
  494. cmdstream << PREF_LOG << "=" << optarg << "\n";
  495. break;
  496. case 's':
  497. cmdstream << PREF_SPLIT << "=" << optarg << "\n";
  498. break;
  499. case 't':
  500. cmdstream << PREF_TIMEOUT << "=" << optarg << "\n";
  501. break;
  502. case 'm':
  503. cmdstream << PREF_MAX_TRIES << "=" << optarg << "\n";
  504. break;
  505. case 'p':
  506. cmdstream << PREF_FTP_PASV << "=" << toBoolArg(optarg) << "\n";
  507. break;
  508. case 'S':
  509. cmdstream << PREF_SHOW_FILES << "=" << V_TRUE << "\n";
  510. break;
  511. case 'T':
  512. cmdstream << PREF_TORRENT_FILE << "=" << optarg << "\n";
  513. break;
  514. case 'M':
  515. cmdstream << PREF_METALINK_FILE << "=" << optarg << "\n";
  516. break;
  517. case 'C':
  518. cmdstream << PREF_METALINK_SERVERS << "=" << optarg << "\n";
  519. break;
  520. case 'a':
  521. cmdstream << PREF_FILE_ALLOCATION << "=" << optarg << "\n";
  522. break;
  523. case 'c':
  524. cmdstream << PREF_CONTINUE << "=" << V_TRUE << "\n";
  525. break;
  526. case 'U':
  527. cmdstream << PREF_USER_AGENT << "=" << optarg << "\n";
  528. break;
  529. case 'n':
  530. cmdstream << PREF_NO_NETRC << "=" << V_TRUE << "\n";
  531. break;
  532. case 'i':
  533. cmdstream << PREF_INPUT_FILE << "=" << optarg << "\n";
  534. break;
  535. case 'j':
  536. cmdstream << PREF_MAX_CONCURRENT_DOWNLOADS << "=" << optarg << "\n";
  537. break;
  538. case 'Z':
  539. cmdstream << PREF_FORCE_SEQUENTIAL << "=" << toBoolArg(optarg) << "\n";
  540. break;
  541. case 'P':
  542. cmdstream << PREF_PARAMETERIZED_URI << "=" << toBoolArg(optarg) << "\n";
  543. break;
  544. case 'q':
  545. cmdstream << PREF_QUIET << "=" << toBoolArg(optarg) << "\n";
  546. break;
  547. case 'R':
  548. cmdstream << PREF_REMOTE_TIME << "=" << toBoolArg(optarg) << "\n";
  549. break;
  550. case 'V':
  551. cmdstream << PREF_CHECK_INTEGRITY << "=" << toBoolArg(optarg) << "\n";
  552. break;
  553. case 'u':
  554. cmdstream << PREF_MAX_UPLOAD_LIMIT << "=" << optarg << "\n";
  555. break;
  556. case 'v':
  557. showVersion();
  558. exit(DownloadResult::FINISHED);
  559. case 'h':
  560. {
  561. std::string category;
  562. if(optarg == 0 || strlen(optarg) == 0) {
  563. category = TAG_BASIC;
  564. } else {
  565. category = optarg;
  566. }
  567. showUsage(category, oparser);
  568. exit(DownloadResult::FINISHED);
  569. }
  570. default:
  571. showUsage(TAG_HELP, oparser);
  572. exit(DownloadResult::UNKNOWN_ERROR);
  573. }
  574. }
  575. {
  576. if(!noConf) {
  577. std::string cfname =
  578. ucfname.empty() ?
  579. oparser.findByName(PREF_CONF_PATH)->getDefaultValue():
  580. ucfname;
  581. if(File(cfname).isFile()) {
  582. std::ifstream cfstream(cfname.c_str());
  583. try {
  584. oparser.parse(op, cfstream);
  585. } catch(OptionHandlerException& e) {
  586. std::cerr << "Parse error in " << cfname << "\n"
  587. << e.stackTrace() << "\n"
  588. << "Usage:" << "\n"
  589. << oparser.findByName(e.getOptionName())->getDescription()
  590. << std::endl;
  591. exit(DownloadResult::UNKNOWN_ERROR);
  592. } catch(Exception& e) {
  593. std::cerr << "Parse error in " << cfname << "\n"
  594. << e.stackTrace() << std::endl;
  595. exit(DownloadResult::UNKNOWN_ERROR);
  596. }
  597. } else if(!ucfname.empty()) {
  598. std::cerr << StringFormat("Configuration file %s is not found.",
  599. cfname.c_str())
  600. << "\n";
  601. showUsage(TAG_HELP, oparser);
  602. exit(DownloadResult::UNKNOWN_ERROR);
  603. }
  604. }
  605. // Override configuration with environment variables.
  606. overrideWithEnv(op, oparser, PREF_HTTP_PROXY, "http_proxy");
  607. overrideWithEnv(op, oparser, PREF_HTTPS_PROXY, "https_proxy");
  608. overrideWithEnv(op, oparser, PREF_FTP_PROXY, "ftp_proxy");
  609. overrideWithEnv(op, oparser, PREF_ALL_PROXY, "all_proxy");
  610. overrideWithEnv(op, oparser, PREF_NO_PROXY, "no_proxy");
  611. try {
  612. oparser.parse(op, cmdstream);
  613. } catch(OptionHandlerException& e) {
  614. std::cerr << e.stackTrace() << "\n"
  615. << "Usage:" << "\n"
  616. << oparser.findByName(e.getOptionName())->getDescription()
  617. << std::endl;
  618. exit(DownloadResult::UNKNOWN_ERROR);
  619. } catch(Exception& e) {
  620. std::cerr << e.stackTrace() << std::endl;
  621. showUsage(TAG_HELP, oparser);
  622. exit(DownloadResult::UNKNOWN_ERROR);
  623. }
  624. }
  625. if(
  626. #ifdef ENABLE_BITTORRENT
  627. op->blank(PREF_TORRENT_FILE) &&
  628. #endif // ENABLE_BITTORRENT
  629. #ifdef ENABLE_METALINK
  630. op->blank(PREF_METALINK_FILE) &&
  631. #endif // ENABLE_METALINK
  632. op->blank(PREF_INPUT_FILE)) {
  633. if(optind == argc) {
  634. std::cerr << MSG_URI_REQUIRED << std::endl;
  635. showUsage(TAG_HELP, oparser);
  636. exit(DownloadResult::UNKNOWN_ERROR);
  637. }
  638. }
  639. #ifdef HAVE_DAEMON
  640. if(op->getAsBool(PREF_DAEMON)) {
  641. if(daemon(1, 1) < 0) {
  642. perror(MSG_DAEMON_FAILED);
  643. exit(DownloadResult::UNKNOWN_ERROR);
  644. }
  645. }
  646. #endif // HAVE_DAEMON
  647. return op;
  648. }
  649. } // namespace aria2