option_processing.cc 6.7 KB

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