option_processing.cc 6.8 KB

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