option_processing.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <sstream>
  39. #include <iostream>
  40. #include "Option.h"
  41. #include "prefs.h"
  42. #include "OptionParser.h"
  43. #include "OptionHandlerFactory.h"
  44. #include "OptionHandler.h"
  45. #include "util.h"
  46. #include "message.h"
  47. #include "Exception.h"
  48. #include "a2io.h"
  49. #include "help_tags.h"
  50. #include "File.h"
  51. #include "fmt.h"
  52. #include "OptionHandlerException.h"
  53. #include "error_code.h"
  54. #include "SimpleRandomizer.h"
  55. #include "bittorrent_helper.h"
  56. #include "BufferedFile.h"
  57. #include "console.h"
  58. #include "array_fun.h"
  59. #ifndef HAVE_DAEMON
  60. #include "daemon.h"
  61. #endif // !HAVE_DAEMON
  62. namespace aria2 {
  63. extern void showVersion();
  64. extern void showUsage
  65. (const std::string& keyword, const SharedHandle<OptionParser>& oparser);
  66. namespace {
  67. void overrideWithEnv
  68. (Option& op,
  69. const SharedHandle<OptionParser>& optionParser,
  70. const Pref* pref,
  71. const std::string& envName)
  72. {
  73. char* value = getenv(envName.c_str());
  74. if(value) {
  75. try {
  76. optionParser->find(pref)->parse(op, value);
  77. } catch(Exception& e) {
  78. global::cerr()->printf
  79. ("Caught Error while parsing environment variable '%s'\n%s\n",
  80. envName.c_str(),
  81. e.stackTrace().c_str());
  82. }
  83. }
  84. }
  85. } // namespace
  86. void option_processing(Option& op, std::vector<std::string>& uris,
  87. int argc, char* argv[])
  88. {
  89. const SharedHandle<OptionParser>& oparser = OptionParser::getInstance();
  90. try {
  91. bool noConf = false;
  92. std::string ucfname;
  93. std::stringstream cmdstream;
  94. oparser->parseArg(cmdstream, uris, argc, argv);
  95. {
  96. // first evaluate --no-conf and --conf-path options.
  97. Option op;
  98. oparser->parse(op, cmdstream);
  99. noConf = op.getAsBool(PREF_NO_CONF);
  100. ucfname = op.get(PREF_CONF_PATH);
  101. if(op.defined(PREF_VERSION)) {
  102. showVersion();
  103. exit(error_code::FINISHED);
  104. }
  105. if(op.defined(PREF_HELP)) {
  106. std::string keyword;
  107. if(op.get(PREF_HELP).empty()) {
  108. keyword = TAG_BASIC;
  109. } else {
  110. keyword = op.get(PREF_HELP);
  111. static const char A2_HH[] = "--";
  112. if(util::startsWith(keyword.begin(), keyword.end(),
  113. A2_HH, vend(A2_HH)-1)) {
  114. keyword.erase(keyword.begin(), keyword.begin()+2);
  115. }
  116. std::string::size_type eqpos = keyword.find("=");
  117. if(eqpos != std::string::npos) {
  118. keyword.erase(keyword.begin()+eqpos, keyword.end());
  119. }
  120. }
  121. showUsage(keyword, oparser);
  122. exit(error_code::FINISHED);
  123. }
  124. }
  125. oparser->parseDefaultValues(op);
  126. if(!noConf) {
  127. std::string cfname =
  128. ucfname.empty() ?
  129. oparser->find(PREF_CONF_PATH)->getDefaultValue() : ucfname;
  130. if(File(cfname).isFile()) {
  131. std::stringstream ss;
  132. {
  133. BufferedFile fp(cfname, BufferedFile::READ);
  134. if(fp) {
  135. fp.transfer(ss);
  136. }
  137. }
  138. try {
  139. oparser->parse(op, ss);
  140. } catch(OptionHandlerException& e) {
  141. global::cerr()->printf("Parse error in %s\n%s\n",
  142. cfname.c_str(),
  143. e.stackTrace().c_str());
  144. const SharedHandle<OptionHandler>& h = oparser->find(e.getPref());
  145. if(h) {
  146. global::cerr()->printf("Usage:\n%s\n", h->getDescription());
  147. }
  148. exit(e.getErrorCode());
  149. } catch(Exception& e) {
  150. global::cerr()->printf("Parse error in %s\n%s\n",
  151. cfname.c_str(),
  152. e.stackTrace().c_str());
  153. exit(e.getErrorCode());
  154. }
  155. } else if(!ucfname.empty()) {
  156. global::cerr()->printf("Configuration file %s is not found.\n",
  157. cfname.c_str());
  158. showUsage(TAG_HELP, oparser);
  159. exit(error_code::UNKNOWN_ERROR);
  160. }
  161. }
  162. // Override configuration with environment variables.
  163. overrideWithEnv(op, oparser, PREF_HTTP_PROXY, "http_proxy");
  164. overrideWithEnv(op, oparser, PREF_HTTPS_PROXY, "https_proxy");
  165. overrideWithEnv(op, oparser, PREF_FTP_PROXY, "ftp_proxy");
  166. overrideWithEnv(op, oparser, PREF_ALL_PROXY, "all_proxy");
  167. overrideWithEnv(op, oparser, PREF_NO_PROXY, "no_proxy");
  168. // we must clear eof bit and seek to the beginning of the buffer.
  169. cmdstream.clear();
  170. cmdstream.seekg(0, std::ios::beg);
  171. // finaly let's parse and store command-iine options.
  172. oparser->parse(op, cmdstream);
  173. #ifdef __MINGW32__
  174. for(size_t i = 1, len = option::countOption(); i < len; ++i) {
  175. const Pref* pref = option::i2p(i);
  176. if(op.defined(pref) && !util::isUtf8(op.get(pref))) {
  177. op.put(pref, nativeToUtf8(op.get(pref)));
  178. }
  179. }
  180. #endif // __MINGW32__
  181. } catch(OptionHandlerException& e) {
  182. global::cerr()->printf("%s\n", e.stackTrace().c_str());
  183. const SharedHandle<OptionHandler>& h = oparser->find(e.getPref());
  184. if(h) {
  185. std::ostringstream ss;
  186. ss << *h;
  187. global::cerr()->printf("Usage:\n%s\n", ss.str().c_str());
  188. }
  189. exit(e.getErrorCode());
  190. } catch(Exception& e) {
  191. global::cerr()->printf("%s\n", e.stackTrace().c_str());
  192. showUsage(TAG_HELP, oparser);
  193. exit(e.getErrorCode());
  194. }
  195. if(!op.getAsBool(PREF_ENABLE_RPC) &&
  196. #ifdef ENABLE_BITTORRENT
  197. op.blank(PREF_TORRENT_FILE) &&
  198. #endif // ENABLE_BITTORRENT
  199. #ifdef ENABLE_METALINK
  200. op.blank(PREF_METALINK_FILE) &&
  201. #endif // ENABLE_METALINK
  202. op.blank(PREF_INPUT_FILE)) {
  203. if(uris.empty()) {
  204. global::cerr()->printf("%s\n", MSG_URI_REQUIRED);
  205. showUsage(TAG_HELP, oparser);
  206. exit(error_code::UNKNOWN_ERROR);
  207. }
  208. }
  209. if(op.getAsBool(PREF_DAEMON)) {
  210. if(daemon(0, 0) < 0) {
  211. perror(MSG_DAEMON_FAILED);
  212. exit(error_code::UNKNOWN_ERROR);
  213. }
  214. }
  215. }
  216. } // namespace aria2