option_processing.cc 22 KB

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