option_processing.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "Option.h"
  40. #include "prefs.h"
  41. #include "OptionParser.h"
  42. #include "OptionHandlerFactory.h"
  43. #include "OptionHandler.h"
  44. #include "util.h"
  45. #include "message.h"
  46. #include "Exception.h"
  47. #include "a2io.h"
  48. #include "help_tags.h"
  49. #include "File.h"
  50. #include "fmt.h"
  51. #include "OptionHandlerException.h"
  52. #include "UnknownOptionException.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,
  66. const SharedHandle<OptionParser>& oparser,
  67. const Console& out);
  68. namespace {
  69. void overrideWithEnv
  70. (Option& op,
  71. const SharedHandle<OptionParser>& optionParser,
  72. const Pref* pref,
  73. const std::string& envName)
  74. {
  75. char* value = getenv(envName.c_str());
  76. if(value) {
  77. try {
  78. optionParser->find(pref)->parse(op, value);
  79. } catch(Exception& e) {
  80. global::cerr()->printf
  81. (_("Caught Error while parsing environment variable '%s'"),
  82. envName.c_str());
  83. global::cerr()->printf("\n%s\n", e.stackTrace().c_str());
  84. }
  85. }
  86. }
  87. } // namespace
  88. namespace {
  89. // Calculates Damerau–Levenshtein distance between c-string a and b
  90. // with given costs. swapcost, subcost, addcost and delcost are cost
  91. // to swap 2 adjacent characters, substitute characters, add character
  92. // and delete character respectively.
  93. int levenshtein
  94. (const char* a,
  95. const char* b,
  96. int swapcost,
  97. int subcost,
  98. int addcost,
  99. int delcost)
  100. {
  101. int alen = strlen(a);
  102. int blen = strlen(b);
  103. std::vector<std::vector<int> > dp(3, std::vector<int>(blen+1));
  104. for(int i = 0; i <= blen; ++i) {
  105. dp[1][i] = i;
  106. }
  107. for(int i = 1; i <= alen; ++i) {
  108. dp[0][0] = i;
  109. for(int j = 1; j <= blen; ++j) {
  110. dp[0][j] = dp[1][j-1]+(a[i-1] == b[j-1] ? 0 : subcost);
  111. if(i >= 2 && j >= 2 && a[i-1] != b[j-1] &&
  112. a[i-2] == b[j-1] && a[i-1] == b[j-2]) {
  113. dp[0][j] = std::min(dp[0][j], dp[2][j-2]+swapcost);
  114. }
  115. dp[0][j] = std::min(dp[0][j],
  116. std::min(dp[1][j]+delcost, dp[0][j-1]+addcost));
  117. }
  118. std::rotate(dp.begin(), dp.begin()+2, dp.end());
  119. }
  120. return dp[1][blen];
  121. }
  122. } // namespace
  123. namespace {
  124. void showCandidates
  125. (const std::string& unknownOption, const SharedHandle<OptionParser>& parser)
  126. {
  127. const char* optstr = unknownOption.c_str();
  128. for(; *optstr == '-'; ++optstr);
  129. if(*optstr == '\0') {
  130. return;
  131. }
  132. int optstrlen = strlen(optstr);
  133. std::vector<std::pair<int, const Pref*> > cands;
  134. for(int i = 1, len = option::countOption(); i < len; ++i) {
  135. const Pref* pref = option::i2p(i);
  136. const SharedHandle<OptionHandler>& h = parser->find(pref);
  137. if(!h || h->isHidden()) {
  138. continue;
  139. }
  140. // Use cost 0 for prefix match
  141. if(util::startsWith(pref->k, pref->k+strlen(pref->k),
  142. optstr, optstr+optstrlen)) {
  143. cands.push_back(std::make_pair(0, pref));
  144. continue;
  145. }
  146. // cost values are borrowed from git, help.c.
  147. int sim = levenshtein(optstr, pref->k, 0, 2, 1, 4);
  148. cands.push_back(std::make_pair(sim, pref));
  149. }
  150. if(cands.empty()) {
  151. return;
  152. }
  153. std::sort(cands.begin(), cands.end());
  154. int threshold = cands[0].first;
  155. // threshold value 7 is borrowed from git, help.c.
  156. if(threshold >= 7) {
  157. return;
  158. }
  159. global::cerr()->printf("\n");
  160. global::cerr()->printf(_("Did you mean:"));
  161. global::cerr()->printf("\n");
  162. for(std::vector<std::pair<int, const Pref*> >::iterator i = cands.begin(),
  163. eoi = cands.end(); i != eoi && (*i).first <= threshold; ++i) {
  164. global::cerr()->printf("\t--%s\n", (*i).second->k);
  165. }
  166. }
  167. } // namespace
  168. void option_processing(Option& op, std::vector<std::string>& uris,
  169. int argc, char* argv[])
  170. {
  171. const SharedHandle<OptionParser>& oparser = OptionParser::getInstance();
  172. try {
  173. bool noConf = false;
  174. std::string ucfname;
  175. std::stringstream cmdstream;
  176. oparser->parseArg(cmdstream, uris, argc, argv);
  177. {
  178. // first evaluate --no-conf and --conf-path options.
  179. Option op;
  180. oparser->parse(op, cmdstream);
  181. noConf = op.getAsBool(PREF_NO_CONF);
  182. ucfname = op.get(PREF_CONF_PATH);
  183. if(op.defined(PREF_VERSION)) {
  184. showVersion();
  185. exit(error_code::FINISHED);
  186. }
  187. if(op.defined(PREF_HELP)) {
  188. std::string keyword;
  189. if(op.get(PREF_HELP).empty()) {
  190. keyword = TAG_BASIC;
  191. } else {
  192. keyword = op.get(PREF_HELP);
  193. if(util::startsWith(keyword, "--")) {
  194. keyword.erase(keyword.begin(), keyword.begin()+2);
  195. }
  196. std::string::size_type eqpos = keyword.find("=");
  197. if(eqpos != std::string::npos) {
  198. keyword.erase(keyword.begin()+eqpos, keyword.end());
  199. }
  200. }
  201. showUsage(keyword, oparser, global::cout());
  202. exit(error_code::FINISHED);
  203. }
  204. }
  205. oparser->parseDefaultValues(op);
  206. if(!noConf) {
  207. std::string cfname =
  208. ucfname.empty() ?
  209. oparser->find(PREF_CONF_PATH)->getDefaultValue() : ucfname;
  210. if(File(cfname).isFile()) {
  211. std::stringstream ss;
  212. {
  213. BufferedFile fp(cfname, BufferedFile::READ);
  214. if(fp) {
  215. fp.transfer(ss);
  216. }
  217. }
  218. try {
  219. oparser->parse(op, ss);
  220. } catch(OptionHandlerException& e) {
  221. global::cerr()->printf(_("Parse error in %s"), cfname.c_str());
  222. global::cerr()->printf("\n%s", e.stackTrace().c_str());
  223. const SharedHandle<OptionHandler>& h = oparser->find(e.getPref());
  224. if(h) {
  225. global::cerr()->printf(_("Usage:"));
  226. global::cerr()->printf("\n%s\n", h->getDescription());
  227. }
  228. exit(e.getErrorCode());
  229. } catch(Exception& e) {
  230. global::cerr()->printf(_("Parse error in %s"), cfname.c_str());
  231. global::cerr()->printf("\n%s", e.stackTrace().c_str());
  232. exit(e.getErrorCode());
  233. }
  234. } else if(!ucfname.empty()) {
  235. global::cerr()->printf(_("Configuration file %s is not found."),
  236. cfname.c_str());
  237. global::cerr()->printf("\n");
  238. showUsage(TAG_HELP, oparser, global::cerr());
  239. exit(error_code::UNKNOWN_ERROR);
  240. }
  241. }
  242. // Override configuration with environment variables.
  243. overrideWithEnv(op, oparser, PREF_HTTP_PROXY, "http_proxy");
  244. overrideWithEnv(op, oparser, PREF_HTTPS_PROXY, "https_proxy");
  245. overrideWithEnv(op, oparser, PREF_FTP_PROXY, "ftp_proxy");
  246. overrideWithEnv(op, oparser, PREF_ALL_PROXY, "all_proxy");
  247. overrideWithEnv(op, oparser, PREF_NO_PROXY, "no_proxy");
  248. // we must clear eof bit and seek to the beginning of the buffer.
  249. cmdstream.clear();
  250. cmdstream.seekg(0, std::ios::beg);
  251. // finaly let's parse and store command-iine options.
  252. oparser->parse(op, cmdstream);
  253. #ifdef __MINGW32__
  254. for(size_t i = 1, len = option::countOption(); i < len; ++i) {
  255. const Pref* pref = option::i2p(i);
  256. if(op.defined(pref) && !util::isUtf8(op.get(pref))) {
  257. op.put(pref, nativeToUtf8(op.get(pref)));
  258. }
  259. }
  260. #endif // __MINGW32__
  261. } catch(OptionHandlerException& e) {
  262. global::cerr()->printf("%s", e.stackTrace().c_str());
  263. const SharedHandle<OptionHandler>& h = oparser->find(e.getPref());
  264. if(h) {
  265. global::cerr()->printf(_("Usage:"));
  266. global::cerr()->printf("\n");
  267. write(global::cerr(), *h);
  268. }
  269. exit(e.getErrorCode());
  270. } catch(UnknownOptionException& e) {
  271. showUsage("", oparser, global::cerr());
  272. showCandidates(e.getUnknownOption(), oparser);
  273. exit(e.getErrorCode());
  274. } catch(Exception& e) {
  275. global::cerr()->printf("%s", e.stackTrace().c_str());
  276. showUsage("", oparser, global::cerr());
  277. exit(e.getErrorCode());
  278. }
  279. if(!op.getAsBool(PREF_ENABLE_RPC) &&
  280. #ifdef ENABLE_BITTORRENT
  281. op.blank(PREF_TORRENT_FILE) &&
  282. #endif // ENABLE_BITTORRENT
  283. #ifdef ENABLE_METALINK
  284. op.blank(PREF_METALINK_FILE) &&
  285. #endif // ENABLE_METALINK
  286. op.blank(PREF_INPUT_FILE)) {
  287. if(uris.empty()) {
  288. global::cerr()->printf(MSG_URI_REQUIRED);
  289. global::cerr()->printf("\n");
  290. showUsage("", oparser, global::cerr());
  291. exit(error_code::UNKNOWN_ERROR);
  292. }
  293. }
  294. if(op.getAsBool(PREF_DAEMON)) {
  295. if(daemon(0, 0) < 0) {
  296. perror(MSG_DAEMON_FAILED);
  297. exit(error_code::UNKNOWN_ERROR);
  298. }
  299. }
  300. }
  301. } // namespace aria2