mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-08-14 14:12:31 +00:00
* Moved the rest of the helper functions from VariableMap to
parse_commands.h. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@912 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
@@ -6,8 +6,6 @@ libsub_utils_a_SOURCES = \
|
||||
list_focus.h \
|
||||
lockfile.cc \
|
||||
lockfile.h \
|
||||
parse.cc \
|
||||
parse.h \
|
||||
socket_fd.cc \
|
||||
socket_fd.h \
|
||||
variable_map.cc \
|
||||
|
||||
@@ -1,338 +0,0 @@
|
||||
// rTorrent - BitTorrent client
|
||||
// Copyright (C) 2006, Jari Sundell
|
||||
//
|
||||
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
|
||||
//
|
||||
// Contact: Jari Sundell <jaris@ifi.uio.no>
|
||||
//
|
||||
// Skomakerveien 33
|
||||
// 3185 Skoppum, NORWAY
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <locale>
|
||||
#include <torrent/exceptions.h>
|
||||
|
||||
#include "parse.h"
|
||||
|
||||
namespace utils {
|
||||
|
||||
const char*
|
||||
parse_skip_wspace(const char* first, const char* last) {
|
||||
while (first != last && std::isspace(*first))
|
||||
first++;
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_skip_wspace(const char* first) {
|
||||
// Assume isspace('\0') == false.
|
||||
while (std::isspace(*first))
|
||||
first++;
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_string(const char* first, const char* last, std::string* dest) {
|
||||
if (first == last)
|
||||
return first;
|
||||
|
||||
bool quoted = parse_is_quote(*first);
|
||||
|
||||
if (quoted)
|
||||
first++;
|
||||
|
||||
while (first != last) {
|
||||
if (quoted) {
|
||||
if (parse_is_quote(*first))
|
||||
return ++first;
|
||||
|
||||
} else {
|
||||
if (parse_is_seperator(*first) || std::isspace(*first))
|
||||
return first;
|
||||
}
|
||||
|
||||
if (parse_is_escape(*first))
|
||||
if (++first == last)
|
||||
throw torrent::input_error("Escape character at end of input.");
|
||||
|
||||
dest->push_back(*first);
|
||||
first++;
|
||||
}
|
||||
|
||||
if (quoted)
|
||||
throw torrent::input_error("Missing closing quote.");
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
void
|
||||
parse_whole_string(const char* first, const char* last, std::string* dest) {
|
||||
first = parse_skip_wspace(first, last);
|
||||
first = parse_string(first, last, dest);
|
||||
first = parse_skip_wspace(first, last);
|
||||
|
||||
if (first != last)
|
||||
throw torrent::input_error("Junk at end of input.");
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_value(const char* src, int64_t* value, int base, int unit) {
|
||||
const char* last = parse_value_nothrow(src, value, base, unit);
|
||||
|
||||
if (last == src)
|
||||
throw torrent::input_error("Could not convert string to value.");
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
void
|
||||
parse_whole_value(const char* src, int64_t* value, int base, int unit) {
|
||||
const char* last = parse_value_nothrow(src, value, base, unit);
|
||||
|
||||
if (last == src || *parse_skip_wspace(last) != '\0')
|
||||
throw torrent::input_error("Could not convert string to value.");
|
||||
}
|
||||
|
||||
bool
|
||||
parse_whole_value_nothrow(const char* src, int64_t* value, int base, int unit) {
|
||||
const char* last = parse_value_nothrow(src, value, base, unit);
|
||||
|
||||
if (last == src || *parse_skip_wspace(last) != '\0')
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_value_nothrow(const char* src, int64_t* value, int base, int unit) {
|
||||
if (unit <= 0)
|
||||
throw torrent::input_error("Command::string_to_value_unit(...) received unit <= 0.");
|
||||
|
||||
char* last;
|
||||
*value = strtoll(src, &last, base);
|
||||
|
||||
if (last == src) {
|
||||
if (strcasecmp(src, "no") == 0) { *value = 0; return src + strlen("no"); }
|
||||
if (strcasecmp(src, "yes") == 0) { *value = 1; return src + strlen("yes"); }
|
||||
if (strcasecmp(src, "true") == 0) { *value = 1; return src + strlen("true"); }
|
||||
if (strcasecmp(src, "false") == 0) { *value = 0; return src + strlen("false"); }
|
||||
|
||||
return src;
|
||||
}
|
||||
|
||||
switch (*last) {
|
||||
case 'b':
|
||||
case 'B': ++last; break;
|
||||
case 'k':
|
||||
case 'K': *value = *value << 10; ++last; break;
|
||||
case 'm':
|
||||
case 'M': *value = *value << 20; ++last; break;
|
||||
case 'g':
|
||||
case 'G': *value = *value << 30; ++last; break;
|
||||
// case ' ':
|
||||
// case '\0': *value = *value * unit; break;
|
||||
// default: throw torrent::input_error("Could not parse value.");
|
||||
default: *value = *value * unit; break;
|
||||
}
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_list(const char* first, const char* last, torrent::Object* dest) {
|
||||
if (!dest->is_list())
|
||||
throw torrent::internal_error("parse_list(...) !dest->is_list().");
|
||||
|
||||
while (true) {
|
||||
std::string str;
|
||||
|
||||
first = parse_skip_wspace(first, last);
|
||||
first = parse_string(first, last, &str);
|
||||
first = parse_skip_wspace(first, last);
|
||||
|
||||
dest->as_list().push_back(str);
|
||||
|
||||
if (first == last || !parse_is_seperator(*first))
|
||||
break;
|
||||
|
||||
first++;
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
void
|
||||
parse_whole_list(const char* first, const char* last, torrent::Object* dest) {
|
||||
std::string str;
|
||||
|
||||
first = parse_skip_wspace(first, last);
|
||||
first = parse_string(first, last, &str);
|
||||
first = parse_skip_wspace(first, last);
|
||||
|
||||
if (first != last && parse_is_seperator(*first)) {
|
||||
*dest = torrent::Object(torrent::Object::TYPE_LIST);
|
||||
|
||||
dest->as_list().push_back(str);
|
||||
first = parse_list(++first, last, dest);
|
||||
|
||||
} else {
|
||||
*dest = str;
|
||||
}
|
||||
|
||||
if (first != last)
|
||||
throw torrent::input_error("Junk at end of input.");
|
||||
}
|
||||
|
||||
std::string
|
||||
convert_list_to_string(const torrent::Object& src) {
|
||||
if (!src.is_list())
|
||||
throw torrent::internal_error("convert_list_to_string(...) !src->is_list().");
|
||||
|
||||
return convert_list_to_string(src.as_list().begin(), src.as_list().end());
|
||||
}
|
||||
|
||||
std::string
|
||||
convert_list_to_string(torrent::Object::list_type::const_iterator first,
|
||||
torrent::Object::list_type::const_iterator last) {
|
||||
std::string dest;
|
||||
|
||||
while (first != last) {
|
||||
if (!first->is_string())
|
||||
throw torrent::input_error("Could not convert non-string list element to string.");
|
||||
|
||||
// Meh.
|
||||
if (!dest.empty())
|
||||
dest += ",\"";
|
||||
else
|
||||
dest += '"';
|
||||
|
||||
std::string::size_type quoteItr = dest.size();
|
||||
dest += first->as_string();
|
||||
|
||||
// Finding a quote inside the string should be relatively rare, so
|
||||
// use something that is fast in the general case and ignore the
|
||||
// cost of the unusual one.
|
||||
while (quoteItr != dest.size()) {
|
||||
if (dest[quoteItr] == '"' || dest[quoteItr] == '\\')
|
||||
dest.insert(quoteItr++, 1, '\\');
|
||||
|
||||
quoteItr++;
|
||||
}
|
||||
|
||||
dest += '"';
|
||||
first++;
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
std::string
|
||||
convert_list_to_command(torrent::Object::list_type::const_iterator first,
|
||||
torrent::Object::list_type::const_iterator last) {
|
||||
if (first == last)
|
||||
throw torrent::input_error("Too few arguments.");
|
||||
|
||||
std::string dest = (first++)->as_string();
|
||||
std::string::size_type quoteItr = dest.find('=');
|
||||
|
||||
if (quoteItr == std::string::npos)
|
||||
throw torrent::input_error("Could not find '=' in command.");
|
||||
|
||||
// We should only escape backslash, not quote here as the string
|
||||
// will start with the command name which isn't quoted.
|
||||
while ((quoteItr = dest.find('\\', quoteItr + 1)) != std::string::npos)
|
||||
dest.insert(quoteItr++, 1, '\\');
|
||||
|
||||
while (first != last) {
|
||||
if (!first->is_string())
|
||||
throw torrent::input_error("Could not convert non-string list element to string.");
|
||||
|
||||
dest += ",\"";
|
||||
|
||||
std::string::size_type quoteItr = dest.size();
|
||||
dest += first->as_string();
|
||||
|
||||
// Finding a quote inside the string should be relatively rare, so
|
||||
// use something that is fast in the general case and ignore the
|
||||
// cost of the unusual one.
|
||||
while (quoteItr != dest.size()) {
|
||||
if (dest[quoteItr] == '"' || dest[quoteItr] == '\\')
|
||||
dest.insert(quoteItr++, 1, '\\');
|
||||
|
||||
quoteItr++;
|
||||
}
|
||||
|
||||
dest += '"';
|
||||
first++;
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
int64_t
|
||||
convert_to_value(const torrent::Object& src, int base, int unit) {
|
||||
int64_t value;
|
||||
|
||||
if (!convert_to_value_nothrow(src, &value, base, unit))
|
||||
throw torrent::input_error("Not convertible to a value.");
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
bool
|
||||
convert_to_value_nothrow(const torrent::Object& src, int64_t* value, int base, int unit) {
|
||||
const torrent::Object& unpacked = (src.is_list() && src.as_list().size() == 1) ? src.as_list().front() : src;
|
||||
|
||||
switch (unpacked.type()) {
|
||||
case torrent::Object::TYPE_VALUE:
|
||||
*value = unpacked.as_value();
|
||||
break;
|
||||
|
||||
case torrent::Object::TYPE_STRING:
|
||||
if (parse_skip_wspace(parse_value(unpacked.as_string().c_str(), value, base, unit),
|
||||
unpacked.as_string().c_str() + unpacked.as_string().size()) != unpacked.as_string().c_str() + unpacked.as_string().size())
|
||||
return false;
|
||||
|
||||
break;
|
||||
|
||||
case torrent::Object::TYPE_NONE:
|
||||
*value = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
// rTorrent - BitTorrent client
|
||||
// Copyright (C) 2006, Jari Sundell
|
||||
//
|
||||
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
|
||||
//
|
||||
// Contact: Jari Sundell <jaris@ifi.uio.no>
|
||||
//
|
||||
// Skomakerveien 33
|
||||
// 3185 Skoppum, NORWAY
|
||||
|
||||
#include <string>
|
||||
#include <torrent/object.h>
|
||||
|
||||
namespace utils {
|
||||
|
||||
// parse_* functions do the bare minimum necessary to parse what was
|
||||
// asked for. If a whitespace is found, it will be treated as empty
|
||||
// input rather than skipped.
|
||||
//
|
||||
// parse_whole_* functions allow for whitespaces and throw an
|
||||
// exception if there is any garbage at the end of the input.
|
||||
|
||||
inline bool parse_is_quote(const char c) { return c == '"'; }
|
||||
inline bool parse_is_escape(const char c) { return c == '\\'; }
|
||||
inline bool parse_is_seperator(const char c) { return c == ','; }
|
||||
|
||||
const char* parse_skip_wspace(const char* first);
|
||||
const char* parse_skip_wspace(const char* first, const char* last);
|
||||
|
||||
const char* parse_string(const char* first, const char* last, std::string* dest);
|
||||
void parse_whole_string(const char* first, const char* last, std::string* dest);
|
||||
|
||||
const char* parse_value(const char* src, int64_t* value, int base = 0, int unit = 1);
|
||||
const char* parse_value_nothrow(const char* src, int64_t* value, int base = 0, int unit = 1);
|
||||
|
||||
void parse_whole_value(const char* src, int64_t* value, int base = 0, int unit = 1);
|
||||
bool parse_whole_value_nothrow(const char* src, int64_t* value, int base = 0, int unit = 1);
|
||||
|
||||
const char* parse_list(const char* first, const char* last, torrent::Object* dest);
|
||||
void parse_whole_list(const char* first, const char* last, torrent::Object* dest);
|
||||
|
||||
std::string convert_list_to_string(const torrent::Object& src);
|
||||
std::string convert_list_to_string(torrent::Object::list_type::const_iterator first, torrent::Object::list_type::const_iterator last);
|
||||
std::string convert_list_to_command(torrent::Object::list_type::const_iterator first, torrent::Object::list_type::const_iterator last);
|
||||
|
||||
int64_t convert_to_value(const torrent::Object& src, int base = 0, int unit = 1);
|
||||
bool convert_to_value_nothrow(const torrent::Object& src, int64_t* value, int base = 0, int unit = 1);
|
||||
|
||||
inline const torrent::Object&
|
||||
convert_to_single_argument(const torrent::Object& args) {
|
||||
if (args.type() == torrent::Object::TYPE_LIST && args.as_list().size() == 1)
|
||||
return args.as_list().front();
|
||||
else
|
||||
return args;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,7 +45,6 @@
|
||||
#include <torrent/exceptions.h>
|
||||
#include <torrent/object.h>
|
||||
|
||||
#include "parse.h"
|
||||
#include "rpc/command.h"
|
||||
#include "variable_map.h"
|
||||
|
||||
@@ -83,133 +82,6 @@ VariableMap::insert(key_type key, const variable_map_data_type src) {
|
||||
src.m_flags | flag_dont_delete, src.m_parm, src.m_doc)));
|
||||
}
|
||||
|
||||
struct variable_map_is_space : std::unary_function<char, bool> {
|
||||
bool operator () (char c) const {
|
||||
return std::isspace(c);
|
||||
}
|
||||
};
|
||||
|
||||
struct variable_map_is_newline : std::unary_function<char, bool> {
|
||||
bool operator () (char c) const {
|
||||
return c == '\n' || c == '\0';
|
||||
}
|
||||
};
|
||||
|
||||
// Use a static length buffer for dest.
|
||||
const char*
|
||||
parse_name(const char* first, const char* last, std::string* dest) {
|
||||
if (first == last || !std::isalpha(*first))
|
||||
throw torrent::input_error("Invalid start of name.");
|
||||
|
||||
for ( ; first != last && (std::isalnum(*first) || *first == '_'); ++first)
|
||||
dest->push_back(*first);
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
const char*
|
||||
VariableMap::process_single(const char* first) {
|
||||
// This could be optimized, but dunno if there's any point in doing
|
||||
// it.
|
||||
return process_single(first, first + std::strlen(first));
|
||||
}
|
||||
|
||||
const char*
|
||||
VariableMap::process_single(const char* first, const char* last) {
|
||||
first = std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
|
||||
if (first == last || *first == '#')
|
||||
return last;
|
||||
|
||||
std::string key;
|
||||
first = parse_name(first, last, &key);
|
||||
first = std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
|
||||
if (first == last || *first != '=')
|
||||
throw torrent::input_error("Could not find '='.");
|
||||
|
||||
mapped_type args;
|
||||
parse_whole_list(first + 1, last, &args);
|
||||
|
||||
call_command(key.c_str(), args);
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
const char*
|
||||
VariableMap::process_d_single(core::Download* download, const char* first, const char* last) {
|
||||
first = std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
|
||||
if (first == last || *first == '#')
|
||||
return last;
|
||||
|
||||
std::string key;
|
||||
first = parse_name(first, last, &key);
|
||||
first = std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
|
||||
if (first == last || *first != '=')
|
||||
throw torrent::input_error("Could not find '='.");
|
||||
|
||||
mapped_type args;
|
||||
parse_whole_list(first + 1, last, &args);
|
||||
|
||||
call_command_d(key.c_str(), download, args);
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
// Consider what the command scheduler should do.
|
||||
|
||||
void
|
||||
VariableMap::process_multiple(const char* first) {
|
||||
try {
|
||||
while (first != '\0') {
|
||||
const char* last = first;
|
||||
|
||||
while (*last != '\n' && *last != '\0') last++;
|
||||
|
||||
// Should we check the return value? Probably not necessary as
|
||||
// parse_args throws on unquoted multi-word input.
|
||||
process_single(first, last);
|
||||
|
||||
if (*last == '\0')
|
||||
return;
|
||||
|
||||
first = last + 1;
|
||||
}
|
||||
|
||||
} catch (torrent::input_error& e) {
|
||||
throw torrent::input_error(std::string("Error parsing multi-line option: ") + e.what());
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
VariableMap::process_file(key_type path) {
|
||||
std::fstream file(rak::path_expand(path).c_str(), std::ios::in);
|
||||
|
||||
if (!file.is_open())
|
||||
return false;
|
||||
|
||||
int lineNumber = 0;
|
||||
char buffer[max_size_line];
|
||||
|
||||
try {
|
||||
|
||||
while (file.getline(buffer, max_size_line).good()) {
|
||||
lineNumber++;
|
||||
// Would be nice to make this zero-copy.
|
||||
process_single(buffer, buffer + std::strlen(buffer));
|
||||
}
|
||||
|
||||
} catch (torrent::input_error& e) {
|
||||
snprintf(buffer, max_size_line, "Error in option file: %s:%i: %s", path, lineNumber, e.what());
|
||||
|
||||
throw torrent::input_error(buffer);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const VariableMap::mapped_type
|
||||
VariableMap::call_command(key_type key, const mapped_type& arg) {
|
||||
const_iterator itr = base_type::find(key);
|
||||
|
||||
@@ -70,7 +70,7 @@ struct variable_map_data_type {
|
||||
const char* parm, const char* doc) :
|
||||
m_variable(variable), m_genericSlot(genericSlot), m_downloadSlot(downloadSlot), m_flags(flags), m_parm(parm), m_doc(doc) {}
|
||||
|
||||
Command* m_variable;
|
||||
Command* m_variable;
|
||||
generic_slot m_genericSlot;
|
||||
download_slot m_downloadSlot;
|
||||
|
||||
@@ -111,27 +111,11 @@ public:
|
||||
bool has(const char* key) const { return base_type::find(key) != base_type::end(); }
|
||||
bool has(const std::string& key) const { return has(key.c_str()); }
|
||||
|
||||
// Allow NULL slot as a temporary compatibility hack.
|
||||
|
||||
void insert(key_type key, Command* variable, generic_slot genericSlot, download_slot downloadSlot, int flags,
|
||||
const char* parm, const char* doc);
|
||||
|
||||
void insert(key_type key, const variable_map_data_type src);
|
||||
|
||||
const char* process_single(const char* first);
|
||||
const char* process_single(const char* first, const char* last);
|
||||
void process_std_single(const std::string& cmd) { process_single(cmd.c_str(), cmd.c_str() + cmd.size()); }
|
||||
|
||||
const char* process_d_single(core::Download* download, const char* first, const char* last);
|
||||
void process_d_std_single(core::Download* download, const std::string& cmd) { process_d_single(download, cmd.c_str(), cmd.c_str() + cmd.size()); }
|
||||
|
||||
void process_multiple(const char* first);
|
||||
|
||||
void process_stream(std::istream* str);
|
||||
bool process_file(key_type path);
|
||||
|
||||
// The new API, which is atm just a wrapper over the old and
|
||||
// requires seperate calls to get and set. These will be merged.
|
||||
const mapped_type call_command(key_type key, const mapped_type& arg);
|
||||
const mapped_type call_command_void(key_type key) { return call_command(key, torrent::Object()); }
|
||||
const std::string call_command_string(key_type key) { return call_command(key, torrent::Object()).as_string(); }
|
||||
|
||||
Reference in New Issue
Block a user