option_processing.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. #endif // ENABLE_BITTORRENT
  211. #ifdef ENABLE_METALINK
  212. { PREF_METALINK_FILE.c_str(), required_argument, NULL, 'M' },
  213. { PREF_METALINK_SERVERS.c_str(), required_argument, NULL, 'C' },
  214. { PREF_METALINK_VERSION.c_str(), required_argument, &lopt, 100 },
  215. { PREF_METALINK_LANGUAGE.c_str(), required_argument, &lopt, 101 },
  216. { PREF_METALINK_OS.c_str(), required_argument, &lopt, 102 },
  217. { PREF_FOLLOW_METALINK.c_str(), required_argument, &lopt, 103 },
  218. { PREF_METALINK_LOCATION.c_str(), required_argument, &lopt, 104 },
  219. { PREF_METALINK_PREFERRED_PROTOCOL.c_str(), required_argument, &lopt, 105 },
  220. { PREF_METALINK_ENABLE_UNIQUE_PROTOCOL.c_str(), optional_argument, &lopt, 106 },
  221. #endif // ENABLE_METALINK
  222. { "version", no_argument, NULL, 'v' },
  223. { "help", optional_argument, NULL, 'h' },
  224. { 0, 0, 0, 0 }
  225. };
  226. c = getopt_long(argc, argv,
  227. "Dd:o:l:s:p::t:m:vh::ST:M:C:a:cU:ni:j:Z::P::q::R::V::u:",
  228. longOpts, &optIndex);
  229. if(c == -1) {
  230. break;
  231. }
  232. switch(c) {
  233. case 0:{
  234. switch(lopt) {
  235. case 1:
  236. cmdstream << PREF_HTTP_PROXY << "=" << optarg << "\n";
  237. break;
  238. case 2:
  239. cmdstream << PREF_HTTP_USER << "=" << optarg << "\n";
  240. break;
  241. case 3:
  242. cmdstream << PREF_HTTP_PASSWD << "=" << optarg << "\n";
  243. break;
  244. case 4:
  245. std::cout << "--http-proxy-user was deprecated. See --http-proxy,"
  246. << " --https-proxy, --ftp-proxy, --all-proxy options."
  247. << std::endl;
  248. exit(DownloadResult::UNKNOWN_ERROR);
  249. case 5:
  250. std::cout << "--http-proxy-passwd was deprecated. See --http-proxy,"
  251. << " --https-proxy, --ftp-proxy, --all-proxy options."
  252. << std::endl;
  253. exit(DownloadResult::UNKNOWN_ERROR);
  254. case 6:
  255. cmdstream << PREF_HTTP_AUTH_SCHEME << "=" << optarg << "\n";
  256. break;
  257. case 7:
  258. cmdstream << PREF_REFERER << "=" << optarg << "\n";
  259. break;
  260. case 8:
  261. cmdstream << PREF_RETRY_WAIT << "=" << optarg << "\n";
  262. break;
  263. case 9:
  264. cmdstream << PREF_FTP_USER << "=" << optarg << "\n";
  265. break;
  266. case 10:
  267. cmdstream << PREF_FTP_PASSWD << "=" << optarg << "\n";
  268. break;
  269. case 11:
  270. cmdstream << PREF_FTP_TYPE << "=" << optarg << "\n";
  271. break;
  272. case 12:
  273. std::cout << "--ftp-via-http-proxy was deprecated."
  274. << " Use --http-proxy-method option instead."
  275. << std::endl;
  276. exit(DownloadResult::UNKNOWN_ERROR);
  277. case 14:
  278. std::cout << "--http-proxy-method was deprecated."
  279. << " Use --proxy-method option instead."
  280. << std::endl;
  281. exit(DownloadResult::UNKNOWN_ERROR);
  282. case 15:
  283. cmdstream << PREF_LISTEN_PORT << "=" << optarg << "\n";
  284. break;
  285. case 16:
  286. cmdstream << PREF_FOLLOW_TORRENT << "=" << optarg << "\n";
  287. break;
  288. case 19:
  289. cmdstream << PREF_DIRECT_FILE_MAPPING << "=" << optarg << "\n";
  290. break;
  291. case 21:
  292. cmdstream << PREF_SELECT_FILE << "=" << optarg << "\n";
  293. break;
  294. case 22:
  295. cmdstream << PREF_SEED_TIME << "=" << optarg << "\n";
  296. break;
  297. case 23:
  298. cmdstream << PREF_SEED_RATIO << "=" << optarg << "\n";
  299. break;
  300. case 25:
  301. cmdstream << PREF_PEER_ID_PREFIX << "=" << optarg << "\n";
  302. break;
  303. case 26:
  304. cmdstream << PREF_ENABLE_PEER_EXCHANGE << "=" << toBoolArg(optarg) << "\n";
  305. break;
  306. case 27:
  307. cmdstream << PREF_ENABLE_DHT << "=" << toBoolArg(optarg) << "\n";
  308. break;
  309. case 28:
  310. cmdstream << PREF_DHT_LISTEN_PORT << "=" << optarg << "\n";
  311. break;
  312. case 29:
  313. cmdstream << PREF_DHT_ENTRY_POINT << "=" << optarg << "\n";
  314. break;
  315. case 30:
  316. cmdstream << PREF_BT_MIN_CRYPTO_LEVEL << "=" << optarg << "\n";
  317. break;
  318. case 31:
  319. cmdstream << PREF_BT_REQUIRE_CRYPTO << "=" << optarg << "\n";
  320. break;
  321. case 32:
  322. cmdstream << PREF_BT_REQUEST_PEER_SPEED_LIMIT << "=" << optarg << "\n";
  323. break;
  324. case 33:
  325. cmdstream << PREF_BT_MAX_OPEN_FILES << "=" << optarg << "\n";
  326. break;
  327. case 34:
  328. cmdstream << PREF_BT_SEED_UNVERIFIED << "=" << toBoolArg(optarg)
  329. << "\n";
  330. break;
  331. case 35:
  332. cmdstream << PREF_DHT_FILE_PATH << "=" << optarg << "\n";
  333. break;
  334. case 36:
  335. cmdstream << PREF_MAX_OVERALL_UPLOAD_LIMIT << "=" << optarg << "\n";
  336. break;
  337. case 100:
  338. cmdstream << PREF_METALINK_VERSION << "=" << optarg << "\n";
  339. break;
  340. case 101:
  341. cmdstream << PREF_METALINK_LANGUAGE << "=" << optarg << "\n";
  342. break;
  343. case 102:
  344. cmdstream << PREF_METALINK_OS << "=" << optarg << "\n";
  345. break;
  346. case 103:
  347. cmdstream << PREF_FOLLOW_METALINK << "=" << optarg << "\n";
  348. break;
  349. case 104:
  350. cmdstream << PREF_METALINK_LOCATION << "=" << optarg << "\n";
  351. break;
  352. case 105:
  353. cmdstream << PREF_METALINK_PREFERRED_PROTOCOL << "=" << optarg << "\n";
  354. break;
  355. case 106:
  356. cmdstream << PREF_METALINK_ENABLE_UNIQUE_PROTOCOL << "=" << toBoolArg(optarg) << "\n";
  357. break;
  358. case 200:
  359. cmdstream << PREF_LOWEST_SPEED_LIMIT << "=" << optarg << "\n";
  360. break;
  361. case 201:
  362. cmdstream << PREF_MAX_DOWNLOAD_LIMIT << "=" << optarg << "\n";
  363. break;
  364. case 202:
  365. cmdstream << PREF_ALLOW_OVERWRITE << "=" << optarg << "\n";
  366. break;
  367. case 204:
  368. cmdstream << PREF_REALTIME_CHUNK_CHECKSUM << "=" << optarg << "\n";
  369. break;
  370. case 205:
  371. cmdstream << PREF_LOAD_COOKIES << "=" << optarg << "\n";
  372. break;
  373. case 206:
  374. cmdstream << PREF_AUTO_FILE_RENAMING << "=" << toBoolArg(optarg) << "\n";
  375. break;
  376. case 207:
  377. cmdstream << PREF_ENABLE_HTTP_KEEP_ALIVE << "=" << toBoolArg(optarg) << "\n";
  378. break;
  379. case 208:
  380. cmdstream << PREF_ENABLE_HTTP_PIPELINING << "=" << toBoolArg(optarg) << "\n";
  381. break;
  382. case 209:
  383. cmdstream << PREF_NO_FILE_ALLOCATION_LIMIT << "=" << optarg << "\n";
  384. break;
  385. case 210:
  386. cmdstream << PREF_ENABLE_DIRECT_IO << "=" << toBoolArg(optarg) << "\n";
  387. break;
  388. case 211:
  389. cmdstream << PREF_ALLOW_PIECE_LENGTH_CHANGE << "=" << optarg << "\n";
  390. break;
  391. case 212:
  392. noConf = true;
  393. break;
  394. case 213:
  395. ucfname = optarg;
  396. break;
  397. case 214:
  398. cmdstream << PREF_STOP << "=" << optarg << "\n";
  399. break;
  400. case 215:
  401. cmdstream << PREF_HEADER << "=" << optarg << "\n";
  402. break;
  403. #ifdef ENABLE_ASYNC_DNS
  404. case 216:
  405. cmdstream << PREF_ASYNC_DNS << "=" << toBoolArg(optarg) << "\n";
  406. break;
  407. #endif // ENABLE_ASYNC_DNS
  408. case 217:
  409. cmdstream << PREF_FTP_REUSE_CONNECTION << "=" << toBoolArg(optarg) << "\n";
  410. break;
  411. case 218:
  412. cmdstream << PREF_SUMMARY_INTERVAL << "=" << optarg << "\n";
  413. break;
  414. case 219:
  415. cmdstream << PREF_LOG_LEVEL << "=" << optarg << "\n";
  416. break;
  417. case 220:
  418. cmdstream << PREF_URI_SELECTOR << "=" << optarg << "\n";
  419. break;
  420. case 221:
  421. cmdstream << PREF_SERVER_STAT_IF << "=" << optarg << "\n";
  422. break;
  423. case 222:
  424. cmdstream << PREF_SERVER_STAT_OF << "=" << optarg << "\n";
  425. break;
  426. case 223:
  427. cmdstream << PREF_SERVER_STAT_TIMEOUT << "=" << optarg << "\n";
  428. break;
  429. case 224:
  430. cmdstream << PREF_CONNECT_TIMEOUT << "=" << optarg << "\n";
  431. break;
  432. case 225:
  433. cmdstream << PREF_MAX_FILE_NOT_FOUND << "=" << optarg << "\n";
  434. break;
  435. case 226:
  436. cmdstream << PREF_AUTO_SAVE_INTERVAL << "=" << optarg << "\n";
  437. break;
  438. case 227:
  439. cmdstream << PREF_HTTPS_PROXY << "=" << optarg << "\n";
  440. break;
  441. case 228:
  442. cmdstream << PREF_FTP_PROXY << "=" << optarg << "\n";
  443. break;
  444. case 229:
  445. cmdstream << PREF_ALL_PROXY << "=" << optarg << "\n";
  446. break;
  447. case 230:
  448. cmdstream << PREF_PROXY_METHOD << "=" << optarg << "\n";
  449. break;
  450. case 231:
  451. cmdstream << PREF_CERTIFICATE << "=" << optarg << "\n";
  452. break;
  453. case 232:
  454. cmdstream << PREF_PRIVATE_KEY << "=" << optarg << "\n";
  455. break;
  456. case 233:
  457. cmdstream << PREF_CA_CERTIFICATE << "=" << optarg << "\n";
  458. break;
  459. case 234:
  460. cmdstream << PREF_CHECK_CERTIFICATE << "=" << toBoolArg(optarg) << "\n";
  461. break;
  462. case 235:
  463. cmdstream << PREF_NO_PROXY << "=" << optarg << "\n";
  464. break;
  465. case 236:
  466. cmdstream << PREF_USE_HEAD << "=" << toBoolArg(optarg) << "\n";
  467. break;
  468. case 237:
  469. cmdstream << PREF_EVENT_POLL << "=" << optarg << "\n";
  470. break;
  471. }
  472. break;
  473. }
  474. #ifdef HAVE_DAEMON
  475. case 'D':
  476. cmdstream << PREF_DAEMON << "=" << V_TRUE << "\n";
  477. break;
  478. #endif // HAVE_DAEMON
  479. case 'd':
  480. cmdstream << PREF_DIR << "=" << optarg << "\n";
  481. break;
  482. case 'o':
  483. cmdstream << PREF_OUT << "=" << optarg << "\n";
  484. break;
  485. case 'l':
  486. cmdstream << PREF_LOG << "=" << optarg << "\n";
  487. break;
  488. case 's':
  489. cmdstream << PREF_SPLIT << "=" << optarg << "\n";
  490. break;
  491. case 't':
  492. cmdstream << PREF_TIMEOUT << "=" << optarg << "\n";
  493. break;
  494. case 'm':
  495. cmdstream << PREF_MAX_TRIES << "=" << optarg << "\n";
  496. break;
  497. case 'p':
  498. cmdstream << PREF_FTP_PASV << "=" << toBoolArg(optarg) << "\n";
  499. break;
  500. case 'S':
  501. cmdstream << PREF_SHOW_FILES << "=" << V_TRUE << "\n";
  502. break;
  503. case 'T':
  504. cmdstream << PREF_TORRENT_FILE << "=" << optarg << "\n";
  505. break;
  506. case 'M':
  507. cmdstream << PREF_METALINK_FILE << "=" << optarg << "\n";
  508. break;
  509. case 'C':
  510. cmdstream << PREF_METALINK_SERVERS << "=" << optarg << "\n";
  511. break;
  512. case 'a':
  513. cmdstream << PREF_FILE_ALLOCATION << "=" << optarg << "\n";
  514. break;
  515. case 'c':
  516. cmdstream << PREF_CONTINUE << "=" << V_TRUE << "\n";
  517. break;
  518. case 'U':
  519. cmdstream << PREF_USER_AGENT << "=" << optarg << "\n";
  520. break;
  521. case 'n':
  522. cmdstream << PREF_NO_NETRC << "=" << V_TRUE << "\n";
  523. break;
  524. case 'i':
  525. cmdstream << PREF_INPUT_FILE << "=" << optarg << "\n";
  526. break;
  527. case 'j':
  528. cmdstream << PREF_MAX_CONCURRENT_DOWNLOADS << "=" << optarg << "\n";
  529. break;
  530. case 'Z':
  531. cmdstream << PREF_FORCE_SEQUENTIAL << "=" << toBoolArg(optarg) << "\n";
  532. break;
  533. case 'P':
  534. cmdstream << PREF_PARAMETERIZED_URI << "=" << toBoolArg(optarg) << "\n";
  535. break;
  536. case 'q':
  537. cmdstream << PREF_QUIET << "=" << toBoolArg(optarg) << "\n";
  538. break;
  539. case 'R':
  540. cmdstream << PREF_REMOTE_TIME << "=" << toBoolArg(optarg) << "\n";
  541. break;
  542. case 'V':
  543. cmdstream << PREF_CHECK_INTEGRITY << "=" << toBoolArg(optarg) << "\n";
  544. break;
  545. case 'u':
  546. cmdstream << PREF_MAX_UPLOAD_LIMIT << "=" << optarg << "\n";
  547. break;
  548. case 'v':
  549. showVersion();
  550. exit(DownloadResult::FINISHED);
  551. case 'h':
  552. {
  553. std::string category;
  554. if(optarg == 0 || strlen(optarg) == 0) {
  555. category = TAG_BASIC;
  556. } else {
  557. category = optarg;
  558. }
  559. showUsage(category, oparser);
  560. exit(DownloadResult::FINISHED);
  561. }
  562. default:
  563. showUsage(TAG_HELP, oparser);
  564. exit(DownloadResult::UNKNOWN_ERROR);
  565. }
  566. }
  567. {
  568. if(!noConf) {
  569. std::string cfname =
  570. ucfname.empty() ?
  571. oparser.findByName(PREF_CONF_PATH)->getDefaultValue():
  572. ucfname;
  573. if(File(cfname).isFile()) {
  574. std::ifstream cfstream(cfname.c_str());
  575. try {
  576. oparser.parse(op, cfstream);
  577. } catch(OptionHandlerException& e) {
  578. std::cerr << "Parse error in " << cfname << "\n"
  579. << e.stackTrace() << "\n"
  580. << "Usage:" << "\n"
  581. << oparser.findByName(e.getOptionName())->getDescription()
  582. << std::endl;
  583. exit(DownloadResult::UNKNOWN_ERROR);
  584. } catch(Exception& e) {
  585. std::cerr << "Parse error in " << cfname << "\n"
  586. << e.stackTrace() << std::endl;
  587. exit(DownloadResult::UNKNOWN_ERROR);
  588. }
  589. } else if(!ucfname.empty()) {
  590. std::cerr << StringFormat("Configuration file %s is not found.",
  591. cfname.c_str())
  592. << "\n";
  593. showUsage(TAG_HELP, oparser);
  594. exit(DownloadResult::UNKNOWN_ERROR);
  595. }
  596. }
  597. // Override configuration with environment variables.
  598. overrideWithEnv(op, oparser, PREF_HTTP_PROXY, "http_proxy");
  599. overrideWithEnv(op, oparser, PREF_HTTPS_PROXY, "https_proxy");
  600. overrideWithEnv(op, oparser, PREF_FTP_PROXY, "ftp_proxy");
  601. overrideWithEnv(op, oparser, PREF_ALL_PROXY, "all_proxy");
  602. overrideWithEnv(op, oparser, PREF_NO_PROXY, "no_proxy");
  603. try {
  604. oparser.parse(op, cmdstream);
  605. } catch(OptionHandlerException& e) {
  606. std::cerr << e.stackTrace() << "\n"
  607. << "Usage:" << "\n"
  608. << oparser.findByName(e.getOptionName())->getDescription()
  609. << std::endl;
  610. exit(DownloadResult::UNKNOWN_ERROR);
  611. } catch(Exception& e) {
  612. std::cerr << e.stackTrace() << std::endl;
  613. showUsage(TAG_HELP, oparser);
  614. exit(DownloadResult::UNKNOWN_ERROR);
  615. }
  616. }
  617. if(
  618. #ifdef ENABLE_BITTORRENT
  619. op->blank(PREF_TORRENT_FILE) &&
  620. #endif // ENABLE_BITTORRENT
  621. #ifdef ENABLE_METALINK
  622. op->blank(PREF_METALINK_FILE) &&
  623. #endif // ENABLE_METALINK
  624. op->blank(PREF_INPUT_FILE)) {
  625. if(optind == argc) {
  626. std::cerr << MSG_URI_REQUIRED << std::endl;
  627. showUsage(TAG_HELP, oparser);
  628. exit(DownloadResult::UNKNOWN_ERROR);
  629. }
  630. }
  631. #ifdef HAVE_DAEMON
  632. if(op->getAsBool(PREF_DAEMON)) {
  633. if(daemon(1, 1) < 0) {
  634. perror(MSG_DAEMON_FAILED);
  635. exit(DownloadResult::UNKNOWN_ERROR);
  636. }
  637. }
  638. #endif // HAVE_DAEMON
  639. return op;
  640. }
  641. } // namespace aria2