option_processing.cc 7.2 KB

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