option_processing.cc 21 KB

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