123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- /* <!-- copyright */
- /*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL. If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so. If you
- * do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
- /* copyright --> */
- #ifndef _D_OPTION_HANDLER_IMPL_H_
- #define _D_OPTION_HANDLER_IMPL_H_
- #include "OptionHandler.h"
- #include <cstdio>
- #include <utility>
- #include <algorithm>
- #include <numeric>
- #include <sstream>
- #include <iterator>
- #include <vector>
- #include "NameMatchOptionHandler.h"
- #include "util.h"
- #include "DlAbortEx.h"
- #include "prefs.h"
- #include "Option.h"
- #include "StringFormat.h"
- #include "A2STR.h"
- #include "Request.h"
- #include "a2functional.h"
- #include "message.h"
- #include "File.h"
- #include "FileEntry.h"
- #include "a2io.h"
- namespace aria2 {
- class NullOptionHandler : public OptionHandler {
- private:
- int _id;
- public:
- virtual ~NullOptionHandler() {}
- virtual bool canHandle(const std::string& optName) { return true; }
- virtual void parse(Option& option, const std::string& arg) {}
- virtual bool hasTag(const std::string& tag) const { return false; }
- virtual void addTag(const std::string& tag) {}
- virtual std::string toTagString() const { return A2STR::NIL; }
- virtual const std::string& getName() const { return A2STR::NIL; }
- virtual const std::string& getDescription() const { return A2STR::NIL; }
- virtual const std::string& getDefaultValue() const { return A2STR::NIL; }
- virtual std::string createPossibleValuesString() const { return A2STR::NIL; }
- virtual bool isHidden() const { return true; }
- virtual void hide() {}
- virtual OptionHandler::ARG_TYPE getArgType() const
- {
- return OptionHandler::NO_ARG;
- }
- virtual int getOptionID() const
- {
- return _id;
- }
- virtual void setOptionID(int id)
- {
- _id = id;
- }
- virtual char getShortName() const
- {
- return 0;
- }
- };
- class BooleanOptionHandler : public NameMatchOptionHandler {
- public:
- BooleanOptionHandler(const std::string& optName,
- const std::string& description = NO_DESCRIPTION,
- const std::string& defaultValue = NO_DEFAULT_VALUE,
- OptionHandler::ARG_TYPE argType = OptionHandler::REQ_ARG,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue,
- argType, shortName) {}
- virtual ~BooleanOptionHandler() {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- if(optarg == "true" ||
- ((_argType == OptionHandler::OPT_ARG ||
- _argType == OptionHandler::NO_ARG)
- && optarg.empty())) {
- option.put(_optName, V_TRUE);
- } else if(optarg == "false") {
- option.put(_optName, V_FALSE);
- } else {
- std::string msg = _optName;
- strappend(msg, " ", _("must be either 'true' or 'false'."));
- throw DL_ABORT_EX(msg);
- }
- }
- virtual std::string createPossibleValuesString() const
- {
- return "true,false";
- }
- };
- class IntegerRangeOptionHandler : public NameMatchOptionHandler {
- private:
- int32_t _min;
- int32_t _max;
- public:
- IntegerRangeOptionHandler(const std::string& optName,
- const std::string& description,
- const std::string& defaultValue,
- int32_t min, int32_t max,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue,
- OptionHandler::REQ_ARG, shortName),
- _min(min), _max(max) {}
- virtual ~IntegerRangeOptionHandler() {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- IntSequence seq = util::parseIntRange(optarg);
- while(seq.hasNext()) {
- int32_t v = seq.next();
- if(v < _min || _max < v) {
- std::string msg = _optName;
- strappend(msg, " ", _("must be between %s and %s."));
- throw DL_ABORT_EX
- (StringFormat(msg.c_str(), util::itos(_min).c_str(),
- util::itos(_max).c_str()).str());
- }
- option.put(_optName, optarg);
- }
- }
- virtual std::string createPossibleValuesString() const
- {
- return util::itos(_min)+"-"+util::itos(_max);
- }
- };
- class NumberOptionHandler : public NameMatchOptionHandler {
- private:
- int64_t _min;
- int64_t _max;
- public:
- NumberOptionHandler(const std::string& optName,
- const std::string& description = NO_DESCRIPTION,
- const std::string& defaultValue = NO_DEFAULT_VALUE,
- int64_t min = -1,
- int64_t max = -1,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue,
- OptionHandler::REQ_ARG, shortName),
- _min(min), _max(max) {}
- virtual ~NumberOptionHandler() {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- int64_t num = util::parseLLInt(optarg);
- parseArg(option, num);
- }
- void parseArg(Option& option, int64_t number)
- {
- if((_min == -1 || _min <= number) && (_max == -1 || number <= _max)) {
- option.put(_optName, util::itos(number));
- } else {
- std::string msg = _optName;
- msg += " ";
- if(_min == -1 && _max != -1) {
- msg += StringFormat(_("must be smaller than or equal to %s."),
- util::itos(_max).c_str()).str();
- } else if(_min != -1 && _max != -1) {
- msg += StringFormat(_("must be between %s and %s."),
- util::itos(_min).c_str(), util::itos(_max).c_str()).str();
- } else if(_min != -1 && _max == -1) {
- msg += StringFormat(_("must be greater than or equal to %s."),
- util::itos(_min).c_str()).str();
- } else {
- msg += _("must be a number.");
- }
- throw DL_ABORT_EX(msg);
- }
- }
- virtual std::string createPossibleValuesString() const
- {
- std::string values;
- if(_min == -1) {
- values += "*";
- } else {
- values += util::itos(_min);
- }
- values += "-";
- if(_max == -1) {
- values += "*";
- } else {
- values += util::itos(_max);
- }
- return values;
- }
- };
- class UnitNumberOptionHandler : public NumberOptionHandler {
- public:
- UnitNumberOptionHandler(const std::string& optName,
- const std::string& description = NO_DESCRIPTION,
- const std::string& defaultValue = NO_DEFAULT_VALUE,
- int64_t min = -1,
- int64_t max = -1,
- char shortName = 0):
- NumberOptionHandler(optName, description, defaultValue, min, max,
- shortName) {}
- virtual ~UnitNumberOptionHandler() {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- int64_t num = util::getRealSize(optarg);
- NumberOptionHandler::parseArg(option, num);
- }
- };
- class FloatNumberOptionHandler : public NameMatchOptionHandler {
- private:
- double _min;
- double _max;
- public:
- FloatNumberOptionHandler(const std::string& optName,
- const std::string& description = NO_DESCRIPTION,
- const std::string& defaultValue = NO_DEFAULT_VALUE,
- double min = -1, double max = -1,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue,
- OptionHandler::REQ_ARG, shortName),
- _min(min), _max(max) {}
- virtual ~FloatNumberOptionHandler() {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- double number = strtod(optarg.c_str(), 0);
- if((_min < 0 || _min <= number) && (_max < 0 || number <= _max)) {
- option.put(_optName, optarg);
- } else {
- std::string msg = _optName;
- msg += " ";
- if(_min < 0 && _max >= 0) {
- msg += StringFormat(_("must be smaller than or equal to %.1f."),
- _max).str();
- } else if(_min >= 0 && _max >= 0) {
- msg += StringFormat(_("must be between %.1f and %.1f."),
- _min, _max).str();
- } else if(_min >= 0 && _max < 0) {
- msg += StringFormat(_("must be greater than or equal to %.1f."),
- _min).str();
- } else {
- msg += _("must be a number.");
- }
- throw DL_ABORT_EX(msg);
- }
- }
- virtual std::string createPossibleValuesString() const
- {
- std::string valuesString;
- if(_min < 0) {
- valuesString += "*";
- } else {
- char buf[11];
- snprintf(buf, sizeof(buf), "%.1f", _min);
- valuesString += buf;
- }
- valuesString += "-";
- if(_max < 0) {
- valuesString += "*";
- } else {
- char buf[11];
- snprintf(buf, sizeof(buf), "%.1f", _max);
- valuesString += buf;
- }
- return valuesString;
- }
- };
- class DefaultOptionHandler : public NameMatchOptionHandler {
- private:
- std::string _possibleValuesString;
- public:
- DefaultOptionHandler(const std::string& optName,
- const std::string& description = NO_DESCRIPTION,
- const std::string& defaultValue = NO_DEFAULT_VALUE,
- const std::string& possibleValuesString = A2STR::NIL,
- OptionHandler::ARG_TYPE argType = OptionHandler::REQ_ARG,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue, argType,
- shortName),
- _possibleValuesString(possibleValuesString) {}
- virtual ~DefaultOptionHandler() {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- option.put(_optName, optarg);
- }
- virtual std::string createPossibleValuesString() const
- {
- return _possibleValuesString;
- }
- };
- class CumulativeOptionHandler : public NameMatchOptionHandler {
- private:
- std::string _delim;
- std::string _possibleValuesString;
- public:
- CumulativeOptionHandler(const std::string& optName,
- const std::string& description,
- const std::string& defaultValue,
- const std::string& delim,
- const std::string& possibleValuesString = A2STR::NIL,
- OptionHandler::ARG_TYPE argType =
- OptionHandler::REQ_ARG,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue, argType,
- shortName),
- _delim(delim),
- _possibleValuesString(possibleValuesString) {}
- virtual ~CumulativeOptionHandler() {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- std::string value = option.get(_optName);
- strappend(value, optarg, _delim);
- option.put(_optName, value);
- }
- virtual std::string createPossibleValuesString() const
- {
- return _possibleValuesString;
- }
- };
- class IndexOutOptionHandler : public NameMatchOptionHandler {
- private:
- public:
- IndexOutOptionHandler(const std::string& optName,
- const std::string& description,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, NO_DEFAULT_VALUE,
- OptionHandler::REQ_ARG, shortName) {}
- virtual ~IndexOutOptionHandler() {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- // See optarg is in the fomrat of "INDEX=PATH"
- util::parseIndexPath(optarg);
- std::string value = option.get(_optName);
- strappend(value, optarg, "\n");
- option.put(_optName, value);
- }
- virtual std::string createPossibleValuesString() const
- {
- return "INDEX=PATH";
- }
- };
- class ParameterOptionHandler : public NameMatchOptionHandler {
- private:
- std::vector<std::string> _validParamValues;
- public:
- ParameterOptionHandler(const std::string& optName,
- const std::string& description,
- const std::string& defaultValue,
- const std::vector<std::string>& validParamValues,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue,
- OptionHandler::REQ_ARG, shortName),
- _validParamValues(validParamValues) {}
- ParameterOptionHandler(const std::string& optName,
- const std::string& description,
- const std::string& defaultValue,
- const std::string& validParamValue,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue,
- OptionHandler::REQ_ARG, shortName)
- {
- _validParamValues.push_back(validParamValue);
- }
- ParameterOptionHandler(const std::string& optName,
- const std::string& description,
- const std::string& defaultValue,
- const std::string& validParamValue1,
- const std::string& validParamValue2,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue,
- OptionHandler::REQ_ARG, shortName)
- {
- _validParamValues.push_back(validParamValue1);
- _validParamValues.push_back(validParamValue2);
- }
- ParameterOptionHandler(const std::string& optName,
- const std::string& description,
- const std::string& defaultValue,
- const std::string& validParamValue1,
- const std::string& validParamValue2,
- const std::string& validParamValue3,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue,
- OptionHandler::REQ_ARG, shortName)
- {
- _validParamValues.push_back(validParamValue1);
- _validParamValues.push_back(validParamValue2);
- _validParamValues.push_back(validParamValue3);
- }
-
- virtual ~ParameterOptionHandler() {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- std::vector<std::string>::const_iterator itr =
- std::find(_validParamValues.begin(), _validParamValues.end(), optarg);
- if(itr == _validParamValues.end()) {
- std::string msg = _optName;
- strappend(msg, " ", _("must be one of the following:"));
- if(_validParamValues.size() == 0) {
- msg += "''";
- } else {
- for(std::vector<std::string>::const_iterator itr =
- _validParamValues.begin(), eoi = _validParamValues.end();
- itr != eoi; ++itr) {
- strappend(msg, "'", *itr, "' ");
- }
- }
- throw DL_ABORT_EX(msg);
- } else {
- option.put(_optName, optarg);
- }
- }
- virtual std::string createPossibleValuesString() const
- {
- std::stringstream s;
- std::copy(_validParamValues.begin(), _validParamValues.end(),
- std::ostream_iterator<std::string>(s, ","));
- return util::trim(s.str(), ", ");
- }
- };
- class HostPortOptionHandler : public NameMatchOptionHandler {
- private:
- std::string _hostOptionName;
-
- std::string _portOptionName;
- public:
- HostPortOptionHandler(const std::string& optName,
- const std::string& description,
- const std::string& defaultValue,
- const std::string& hostOptionName,
- const std::string& portOptionName,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue,
- OptionHandler::REQ_ARG, shortName),
- _hostOptionName(hostOptionName),
- _portOptionName(portOptionName) {}
- virtual ~HostPortOptionHandler() {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- std::pair<std::string, std::string> proxy = util::split(optarg, ":");
- int32_t port = util::parseInt(proxy.second);
- if(proxy.first.empty() || proxy.second.empty() ||
- port <= 0 || 65535 < port) {
- throw DL_ABORT_EX(_("unrecognized proxy format"));
- }
- option.put(_optName, optarg);
- setHostAndPort(option, proxy.first, port);
- }
- void setHostAndPort(Option& option, const std::string& hostname, uint16_t port)
- {
- option.put(_hostOptionName, hostname);
- option.put(_portOptionName, util::uitos(port));
- }
- virtual std::string createPossibleValuesString() const
- {
- return "HOST:PORT";
- }
- };
- class HttpProxyOptionHandler : public NameMatchOptionHandler {
- public:
- HttpProxyOptionHandler(const std::string& optName,
- const std::string& description,
- const std::string& defaultValue,
- char shortName = 0)
- :
- NameMatchOptionHandler(optName, description, defaultValue,
- OptionHandler::REQ_ARG, shortName)
- {}
- virtual ~HttpProxyOptionHandler() {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- if(optarg.empty()) {
- option.put(_optName, optarg);
- } else {
- Request req;
- std::string uri;
- if(util::startsWith(optarg, "http://")) {
- uri = optarg;
- } else {
- uri = "http://"+optarg;
- }
- if(req.setUri(uri)) {
- option.put(_optName, uri);
- } else {
- throw DL_ABORT_EX(_("unrecognized proxy format"));
- }
- }
- }
- virtual std::string createPossibleValuesString() const
- {
- return "[http://][USER:PASSWORD@]HOST[:PORT]";
- }
- };
- class LocalFilePathOptionHandler : public NameMatchOptionHandler {
- private:
- bool _acceptStdin;
- public:
- LocalFilePathOptionHandler
- (const std::string& optName,
- const std::string& description = NO_DESCRIPTION,
- const std::string& defaultValue = NO_DEFAULT_VALUE,
- bool acceptStdin = false,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue,
- OptionHandler::REQ_ARG,
- shortName),
- _acceptStdin(acceptStdin) {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- if(_acceptStdin && optarg == "-") {
- option.put(_optName, DEV_STDIN);
- } else {
- File f(optarg);
- if(!f.exists() || f.isDir()) {
- throw DL_ABORT_EX
- (StringFormat(MSG_NOT_FILE, optarg.c_str()).str());
- }
- option.put(_optName, optarg);
- }
- }
-
- virtual std::string createPossibleValuesString() const
- {
- return "/path/to/file";
- }
- };
- class PrioritizePieceOptionHandler:public NameMatchOptionHandler {
- public:
- PrioritizePieceOptionHandler
- (const std::string& optName,
- const std::string& description = NO_DESCRIPTION,
- const std::string& defaultValue = NO_DEFAULT_VALUE,
- char shortName = 0):
- NameMatchOptionHandler(optName, description, defaultValue,
- OptionHandler::REQ_ARG, shortName) {}
- virtual void parseArg(Option& option, const std::string& optarg)
- {
- // Parse optarg against empty FileEntry list to detect syntax
- // error.
- std::vector<size_t> result;
- util::parsePrioritizePieceRange
- (result, optarg, std::vector<SharedHandle<FileEntry> >(), 1024);
- option.put(_optName, optarg);
- }
- virtual std::string createPossibleValuesString() const
- {
- return "head[=SIZE],tail[=SIZE]";
- }
- };
- } // namespace aria2
- #endif // _D_OPTION_HANDLER_IMPL_H_
|