mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-08-09 11:42:33 +00:00
* Working on new infrastructure for parseing commands. It now passes a
list instead of a string when calling a multi-parameter command, which handles escaped spaces, commas and quotes correctly. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@877 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
@@ -6,6 +6,8 @@ libsub_utils_a_SOURCES = \
|
||||
list_focus.h \
|
||||
lockfile.cc \
|
||||
lockfile.h \
|
||||
parse.cc \
|
||||
parse.h \
|
||||
variable.cc \
|
||||
variable.h \
|
||||
variable_generic.cc \
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
// 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::iswspace(*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::iswspace(*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;
|
||||
}
|
||||
|
||||
const char*
|
||||
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.");
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_value(const char* src, int64_t* value, int base, int unit) {
|
||||
if (unit <= 0)
|
||||
throw torrent::input_error("Variable::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"); }
|
||||
|
||||
throw torrent::input_error("Could not convert string to value.");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
const char*
|
||||
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.");
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
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().");
|
||||
|
||||
std::string dest;
|
||||
|
||||
for (torrent::Object::list_type::const_iterator itr = src.as_list().begin(), last = src.as_list().end(); itr != last; itr++) {
|
||||
if (!itr->is_string())
|
||||
throw torrent::input_error("Could not convert non-string list element to string.");
|
||||
|
||||
// Meh.
|
||||
if (!dest.empty())
|
||||
dest += ",";
|
||||
|
||||
dest += itr->as_string();
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
int64_t
|
||||
convert_to_value(const torrent::Object& src, 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:
|
||||
return unpacked.as_value();
|
||||
|
||||
case torrent::Object::TYPE_STRING:
|
||||
int64_t tmp;
|
||||
|
||||
if (parse_skip_wspace(parse_value(unpacked.as_string().c_str(), &tmp, base, unit),
|
||||
unpacked.as_string().c_str() + unpacked.as_string().size()) != unpacked.as_string().c_str() + unpacked.as_string().size())
|
||||
throw torrent::input_error("Junk at end of value.");
|
||||
|
||||
return tmp;
|
||||
|
||||
case torrent::Object::TYPE_NONE:
|
||||
return 0;
|
||||
default:
|
||||
throw torrent::input_error("Not convertible to a value.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// 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* last);
|
||||
|
||||
const char* parse_string(const char* first, const char* last, std::string* dest);
|
||||
const char* 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_list(const char* first, const char* last, torrent::Object* dest);
|
||||
const char* parse_whole_list(const char* first, const char* last, torrent::Object* dest);
|
||||
|
||||
std::string convert_list_to_string(const torrent::Object& src);
|
||||
int64_t convert_to_value(const torrent::Object& src, int base = 0, int unit = 1);
|
||||
|
||||
}
|
||||
@@ -38,6 +38,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "parse.h"
|
||||
#include "variable_generic.h"
|
||||
|
||||
namespace utils {
|
||||
@@ -249,6 +250,9 @@ VariableStringSlot::set(const torrent::Object& arg) {
|
||||
case torrent::Object::TYPE_NONE:
|
||||
m_slotSet(std::string());
|
||||
break;
|
||||
case torrent::Object::TYPE_LIST:
|
||||
m_slotSet(convert_list_to_string(arg));
|
||||
break;
|
||||
default:
|
||||
throw torrent::input_error("Not a string.");
|
||||
}
|
||||
@@ -280,4 +284,30 @@ VariableDownloadStringSlot::set_d(core::Download* download, const torrent::Objec
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
VariableListSlot::set(const torrent::Object& arg) {
|
||||
switch (arg.type()) {
|
||||
// case torrent::Object::TYPE_STRING:
|
||||
// break;
|
||||
case torrent::Object::TYPE_LIST:
|
||||
m_slotSet(arg.as_list());
|
||||
break;
|
||||
default:
|
||||
throw torrent::input_error("Not a list.");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
VariableDownloadListSlot::set_d(core::Download* download, const torrent::Object& arg) {
|
||||
switch (arg.type()) {
|
||||
// case torrent::Object::TYPE_STRING:
|
||||
// break;
|
||||
case torrent::Object::TYPE_LIST:
|
||||
m_slotSet(download, arg.as_list());
|
||||
break;
|
||||
default:
|
||||
throw torrent::input_error("Not a list.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -295,6 +295,36 @@ private:
|
||||
torrent::Object m_cache;
|
||||
};
|
||||
|
||||
class VariableListSlot : public Variable {
|
||||
public:
|
||||
typedef rak::function1<void, const list_type&> slot_set_type;
|
||||
|
||||
template <typename SlotSet>
|
||||
VariableListSlot(SlotSet* slotSet) {
|
||||
m_slotSet.set(slotSet);
|
||||
}
|
||||
|
||||
virtual void set(const torrent::Object& arg);
|
||||
|
||||
private:
|
||||
slot_set_type m_slotSet;
|
||||
};
|
||||
|
||||
class VariableDownloadListSlot : public Variable {
|
||||
public:
|
||||
typedef rak::function2<void, core::Download*, const list_type&> slot_set_type;
|
||||
|
||||
template <typename SlotSet>
|
||||
VariableDownloadListSlot(SlotSet* slotSet) {
|
||||
m_slotSet.set(slotSet);
|
||||
}
|
||||
|
||||
virtual void set_d(core::Download* download, const torrent::Object& arg);
|
||||
|
||||
private:
|
||||
slot_set_type m_slotSet;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+23
-97
@@ -45,6 +45,7 @@
|
||||
#include <torrent/exceptions.h>
|
||||
#include <torrent/object.h>
|
||||
|
||||
#include "parse.h"
|
||||
#include "variable.h"
|
||||
#include "variable_map.h"
|
||||
|
||||
@@ -132,43 +133,6 @@ parse_name(const char* first, const char* last, std::string* dest) {
|
||||
return first;
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_unknown(const char* first, const char* last, VariableMap::mapped_type* dest) {
|
||||
if (*first == '"') {
|
||||
const char* next = std::find_if(++first, last, std::bind2nd(std::equal_to<char>(), '"'));
|
||||
|
||||
if (first == last || first == next || next == last)
|
||||
throw torrent::input_error("Could not find closing '\"'.");
|
||||
|
||||
*dest = std::string(first, next);
|
||||
return ++next;
|
||||
|
||||
} else {
|
||||
// Add rak::or and check for ','.
|
||||
const char* next = std::find_if(first, last, variable_map_is_space());
|
||||
|
||||
*dest = std::string(first, next);
|
||||
return next;
|
||||
}
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_args(const char* first, const char* last, VariableMap::mapped_type::list_type* dest) {
|
||||
first = std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
|
||||
while (first != last) {
|
||||
dest->push_back(VariableMap::mapped_type());
|
||||
|
||||
first = parse_unknown(first, last, &dest->back());
|
||||
first = std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
|
||||
if (first != last && *first != ',')
|
||||
throw torrent::input_error("A string with blanks must be quoted.");
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
const char*
|
||||
VariableMap::process_single(const char* first) {
|
||||
// This could be optimized, but dunno if there's any point in doing
|
||||
@@ -190,19 +154,12 @@ VariableMap::process_single(const char* first, const char* last) {
|
||||
if (first == last || *first != '=')
|
||||
throw torrent::input_error("Could not find '='.");
|
||||
|
||||
mapped_type args(mapped_type::TYPE_LIST);
|
||||
first = parse_args(first + 1, last, &args.as_list());
|
||||
mapped_type args;
|
||||
first = parse_whole_list(first + 1, last, &args);
|
||||
|
||||
if (args.as_list().empty())
|
||||
set(key.c_str(), mapped_type());
|
||||
set(key.c_str(), args);
|
||||
|
||||
else if (++args.as_list().begin() == args.as_list().end())
|
||||
set(key.c_str(), *args.as_list().begin());
|
||||
|
||||
else
|
||||
set(key.c_str(), args);
|
||||
|
||||
return std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
return first;
|
||||
}
|
||||
|
||||
const char*
|
||||
@@ -219,67 +176,36 @@ VariableMap::process_d_single(core::Download* download, const char* first, const
|
||||
if (first == last || *first != '=')
|
||||
throw torrent::input_error("Could not find '='.");
|
||||
|
||||
mapped_type args(mapped_type::TYPE_LIST);
|
||||
first = parse_args(first + 1, last, &args.as_list());
|
||||
mapped_type args;
|
||||
first = parse_whole_list(first + 1, last, &args);
|
||||
|
||||
if (args.as_list().empty())
|
||||
set_d(download, key.c_str(), mapped_type());
|
||||
set_d(download, key.c_str(), args);
|
||||
|
||||
else if (++args.as_list().begin() == args.as_list().end())
|
||||
set_d(download, key.c_str(), *args.as_list().begin());
|
||||
|
||||
else
|
||||
set_d(download, key.c_str(), args);
|
||||
|
||||
return std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
return first;
|
||||
}
|
||||
|
||||
// void
|
||||
// VariableMap::process_command(const std::string& command) {
|
||||
// std::string::const_iterator pos = command.begin();
|
||||
// pos = std::find_if(pos, command.end(), std::not1(variable_map_is_space()));
|
||||
|
||||
// if (pos == command.end() || *pos == '#')
|
||||
// return;
|
||||
|
||||
// // Replace with parse_unknown?
|
||||
// std::string key;
|
||||
// pos = parse_name(pos, command.end(), &key);
|
||||
// pos = std::find_if(pos, command.end(), std::not1(variable_map_is_space()));
|
||||
|
||||
// if (pos == command.end() || *pos != '=')
|
||||
// throw torrent::input_error("Could not find '='.");
|
||||
|
||||
// mapped_type args(mapped_type::TYPE_LIST);
|
||||
// parse_args(pos + 1, command.end(), &args.as_list());
|
||||
|
||||
// if (args.as_list().empty())
|
||||
// set(key.c_str(), mapped_type());
|
||||
|
||||
// else if (++args.as_list().begin() == args.as_list().end())
|
||||
// set(key.c_str(), *args.as_list().begin());
|
||||
|
||||
// else
|
||||
// set(key.c_str(), args);
|
||||
|
||||
|
||||
// Consider what the command scheduler should do.
|
||||
|
||||
void
|
||||
VariableMap::process_multiple(const char* first) {
|
||||
while (first != '\0') {
|
||||
const char* last = first;
|
||||
try {
|
||||
while (first != '\0') {
|
||||
const char* last = first;
|
||||
|
||||
while (*last != '\n' && *last != '\0') last++;
|
||||
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);
|
||||
// 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;
|
||||
if (*last == '\0')
|
||||
return;
|
||||
|
||||
first = last + 1;
|
||||
first = last + 1;
|
||||
}
|
||||
|
||||
} catch (torrent::input_error& e) {
|
||||
throw torrent::input_error(std::string("Error parsing multi-line option: ") + e.what());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user