* 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:
rakshasa
2007-03-25 18:03:07 +00:00
parent 0ab5bae22e
commit ee6eae6c84
10 changed files with 506 additions and 248 deletions
+11 -11
View File
@@ -116,21 +116,21 @@ CommandScheduler::call_item(value_type item) {
}
void
CommandScheduler::parse(const std::string& arg) {
char key[21];
char bufAbsolute[21];
char bufInterval[21];
char command[2048];
CommandScheduler::parse(const std::string& key, const std::string& bufAbsolute, const std::string& bufInterval, const std::string& command) {
// char key[21];
// char bufAbsolute[21];
// char bufInterval[21];
// char command[2048];
if (std::sscanf(arg.c_str(), "%20[^,],%20[^,],%20[^,],%2047[^\n]", key, bufAbsolute, bufInterval, command) != 4)
throw torrent::input_error("Invalid arguments to command.");
// if (std::sscanf(arg.c_str(), "%20[^,],%20[^,],%20[^,],%2047[^\n]", key, bufAbsolute, bufInterval, command) != 4)
// throw torrent::input_error("Invalid arguments to command.");
uint32_t absolute = parse_absolute(bufAbsolute);
uint32_t interval = parse_interval(bufInterval);
uint32_t absolute = parse_absolute(bufAbsolute.c_str());
uint32_t interval = parse_interval(bufInterval.c_str());
CommandSchedulerItem* item = *insert(rak::trim(std::string(key)));
CommandSchedulerItem* item = *insert(key);
item->set_command(rak::trim(std::string(command)));
item->set_command(command);
item->set_interval(interval);
item->enable((cachedTime + rak::timer::from_seconds(absolute)).round_seconds());
+1 -1
View File
@@ -70,7 +70,7 @@ public:
void erase(iterator itr);
void erase(const std::string& key) { erase(find(key)); }
void parse(const std::string& arg);
void parse(const std::string& key, const std::string& bufAbsolute, const std::string& bufInterval, const std::string& command);
static uint32_t parse_absolute(const char* str);
static uint32_t parse_interval(const char* str);
+2 -2
View File
@@ -212,8 +212,8 @@ main(int argc, char** argv) {
"view_sort_new = seeding,state_changed\n"
"view_sort_current = seeding,state_changed_reverse\n"
"schedule = view_main,10,10,view_sort=main,20\n"
"schedule = view_name,10,10,view_sort=name,20\n"
"schedule = view_main,10,10,\"view_sort=main,20\"\n"
"schedule = view_name,10,10,\"view_sort=name,20\"\n"
// "schedule = view_started,10,10,view_sort=started,5\n"
// "schedule = view_stopped,10,10,view_sort=stopped,5\n"
// "schedule = view_complete,10,10,view_sort=complete,5\n"
+114 -137
View File
@@ -66,6 +66,7 @@
#include "core/view_manager.h"
#include "ui/root.h"
#include "utils/directory.h"
#include "utils/parse.h"
#include "utils/variable_generic.h"
#include "utils/variable_map.h"
@@ -90,15 +91,8 @@ namespace core {
// variables->insert("tracker_dump", new utils::VariableAny(std::string()));
// }
void
apply_hash_read_ahead(int arg) {
torrent::set_hash_read_ahead(arg << 20);
}
void
apply_hash_interval(int arg) {
torrent::set_hash_interval(arg * 1000);
}
void apply_hash_read_ahead(int arg) { torrent::set_hash_read_ahead(arg << 20); }
void apply_hash_interval(int arg) { torrent::set_hash_interval(arg * 1000); }
// The arg string *must* have been checked with validate_port_range
// first.
@@ -111,25 +105,10 @@ apply_port_range(const std::string& arg) {
control->core()->set_port_range(a, b);
}
void
apply_load(const std::string& arg) {
control->core()->try_create_download_expand(arg, false, false, true);
}
void
apply_load_verbose(const std::string& arg) {
control->core()->try_create_download_expand(arg, false, true, true);
}
void
apply_load_start(const std::string& arg) {
control->core()->try_create_download_expand(arg, true, false, true);
}
void
apply_load_start_verbose(const std::string& arg) {
control->core()->try_create_download_expand(arg, true, true, true);
}
void apply_load(const std::string& arg) { control->core()->try_create_download_expand(arg, false, false, true); }
void apply_load_verbose(const std::string& arg) { control->core()->try_create_download_expand(arg, false, true, true); }
void apply_load_start(const std::string& arg) { control->core()->try_create_download_expand(arg, true, false, true); }
void apply_load_start_verbose(const std::string& arg) { control->core()->try_create_download_expand(arg, true, true, true); }
void
apply_start_tied() {
@@ -200,20 +179,18 @@ apply_close_low_diskspace(int64_t arg) {
}
void
apply_stop_on_ratio(const std::string& arg) {
int64_t minRatio = 0; // first argument: minimum ratio to reach
int64_t minUpload = 0; // second argument: minimum upload amount to reach [optional]
int64_t maxRatio = 0; // third argument: maximum ratio to reach [optional]
apply_stop_on_ratio(const torrent::Object::list_type& args) {
if (args.empty())
throw torrent::input_error("Too few arguments.");
rak::split_iterator_t<std::string> sitr = rak::split_iterator(arg, ',');
torrent::Object::list_type::const_iterator argItr = args.begin();
utils::Variable::string_to_value_unit(rak::trim(*sitr).c_str(), &minRatio, 0, 1);
if (++sitr != rak::split_iterator(arg))
utils::Variable::string_to_value_unit(rak::trim(*sitr).c_str(), &minUpload, 0, 1);
if (++sitr != rak::split_iterator(arg))
utils::Variable::string_to_value_unit(rak::trim(*sitr).c_str(), &maxRatio, 0, 1);
// first argument: minimum ratio to reach
// second argument: minimum upload amount to reach [optional]
// third argument: maximum ratio to reach [optional]
int64_t minRatio = utils::convert_to_value(*argItr++);
int64_t minUpload = argItr != args.end() ? utils::convert_to_value(*argItr++) : 0;
int64_t maxRatio = argItr != args.end() ? utils::convert_to_value(*argItr++) : 0;
core::Manager::DListItr itr = control->core()->download_list()->begin();
@@ -221,7 +198,8 @@ apply_stop_on_ratio(const std::string& arg) {
int64_t totalUpload = (*itr)->download()->up_rate()->total();
int64_t totalDone = (*itr)->download()->bytes_done();
if ((totalUpload >= minUpload && totalUpload * 100 >= totalDone * minRatio) || (maxRatio > 0 && totalUpload * 100 > totalDone * maxRatio)) {
if ((totalUpload >= minUpload && totalUpload * 100 >= totalDone * minRatio) ||
(maxRatio > 0 && totalUpload * 100 > totalDone * maxRatio)) {
control->core()->download_list()->stop_try(*itr);
(*itr)->set("ignore_commands", (int64_t)1);
}
@@ -231,19 +209,19 @@ apply_stop_on_ratio(const std::string& arg) {
}
void
apply_on_state_change(core::DownloadList::slot_map* slotMap, const std::string& arg) {
std::string::const_iterator itr = std::find(arg.begin(), arg.end(), ',');
apply_on_state_change(core::DownloadList::slot_map* slotMap, const torrent::Object::list_type& args) {
if (args.size() != 2)
throw torrent::input_error("Too few arguments.");
std::string key = rak::trim(std::string(arg.begin(), itr));
std::string value = rak::trim(std::string(itr != arg.end() ? itr + 1 : itr, arg.end()));
if (key.empty())
if (args.front().as_string().empty())
throw torrent::input_error("Empty key.");
if (value.empty())
slotMap->erase("1_start_" + key);
std::string key = "1_state_" + args.front().as_string();
if (args.back().as_string().empty())
slotMap->erase(key);
else
(*slotMap)["1_start_" + key] = sigc::bind(sigc::mem_fun(control->download_variables(), &utils::VariableMap::process_d_std_single), value);
(*slotMap)[key] = sigc::bind(sigc::mem_fun(control->download_variables(), &utils::VariableMap::process_d_std_single), args.back().as_string());
}
void
@@ -252,13 +230,11 @@ apply_encoding_list(const std::string& arg) {
}
void
apply_encryption(const std::string& arg) {
rak::split_iterator_t<std::string> sitr = rak::split_iterator(arg, ',');
apply_encryption(const torrent::Object::list_type& args) {
uint32_t options_mask = torrent::ConnectionManager::encryption_none;
while (sitr != rak::split_iterator(arg)) {
std::string opt = rak::trim(*sitr);
++sitr;
for (torrent::Object::list_type::const_iterator itr = args.begin(), last = args.end(); itr != last; itr++) {
const std::string& opt = itr->as_string();
if (opt == "none")
options_mask = torrent::ConnectionManager::encryption_none;
@@ -282,7 +258,7 @@ apply_encryption(const std::string& arg) {
}
void
apply_enable_trackers(__UNUSED const std::string& arg) {
apply_enable_trackers(const std::string& arg) {
bool state = (arg != "no");
for (core::Manager::DListItr itr = control->core()->download_list()->begin(), last = control->core()->download_list()->end(); itr != last; ++itr) {
@@ -327,110 +303,91 @@ apply_tos(const std::string& arg) {
}
void
apply_view_filter(const std::string& arg) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(arg, ',');
apply_view_filter(const torrent::Object::list_type& args) {
if (args.size() < 1)
throw torrent::input_error("Too few arguments.");
std::string name = rak::trim(*itr);
const std::string& name = args.front().as_string();
if (name.empty())
throw torrent::input_error("First argument must be a string.");
core::ViewManager::filter_args filterArgs;
while (++itr != rak::split_iterator(arg)) {
filterArgs.push_back(rak::trim(*itr));
if (filterArgs.back().empty())
throw torrent::input_error("One of the arguments is empty.");
}
for (torrent::Object::list_type::const_iterator itr = ++args.begin(), last = args.end(); itr != last; itr++)
filterArgs.push_back(itr->as_string());
control->view_manager()->set_filter(name, filterArgs);
}
void
apply_view_filter_on(const std::string& arg) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(arg, ',');
apply_view_filter_on(const torrent::Object::list_type& args) {
if (args.size() < 1)
throw torrent::input_error("Too few arguments.");
std::string name = rak::trim(*itr);
const std::string& name = args.front().as_string();
if (name.empty())
throw torrent::input_error("First argument must be a string.");
core::ViewManager::filter_args filterArgs;
while (++itr != rak::split_iterator(arg)) {
filterArgs.push_back(rak::trim(*itr));
if (filterArgs.back().empty())
throw torrent::input_error("One of the arguments is empty.");
}
for (torrent::Object::list_type::const_iterator itr = ++args.begin(), last = args.end(); itr != last; itr++)
filterArgs.push_back(itr->as_string());
control->view_manager()->set_filter_on(name, filterArgs);
}
void
apply_view_sort(const std::string& arg) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(arg, ',');
apply_view_sort(const torrent::Object::list_type& args) {
if (args.size() <= 0 || args.size() > 2)
throw torrent::input_error("Wrong argument count.");
std::string name = rak::trim(*itr);
++itr;
const std::string& name = args.front().as_string();
if (name.empty())
throw torrent::input_error("First argument must be a string.");
// Need some generic tools for this, rather than hacking up
// something every time...
std::string arg1;
int32_t value = 0;
if (itr != rak::split_iterator(arg) && !(arg1 = *itr).empty()) {
char* endPtr;
if (args.size() == 2)
value = utils::convert_to_value(args.back());
if ((value = strtol(arg1.c_str(), &endPtr, 0)) < 0 || *endPtr != '\0')
throw torrent::input_error("Second argument must be a value.");
}
control->view_manager()->sort(name, value);
}
void
apply_view_sort_current(const std::string& arg) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(arg, ',');
apply_view_sort_current(const torrent::Object::list_type& args) {
if (args.size() < 1)
throw torrent::input_error("Too few arguments.");
std::string name = rak::trim(*itr);
const std::string& name = args.front().as_string();
if (name.empty())
throw torrent::input_error("First argument must be a string.");
core::ViewManager::sort_args sortArgs;
while (++itr != rak::split_iterator(arg)) {
sortArgs.push_back(rak::trim(*itr));
if (sortArgs.back().empty())
throw torrent::input_error("One of the arguments is empty.");
}
for (torrent::Object::list_type::const_iterator itr = ++args.begin(), last = args.end(); itr != last; itr++)
sortArgs.push_back(itr->as_string());
control->view_manager()->set_sort_current(name, sortArgs);
}
void
apply_view_sort_new(const std::string& arg) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(arg, ',');
apply_view_sort_new(const torrent::Object::list_type& args) {
if (args.size() < 1)
throw torrent::input_error("Too few arguments.");
std::string name = rak::trim(*itr);
const std::string& name = args.front().as_string();
if (name.empty())
throw torrent::input_error("First argument must be a string.");
core::ViewManager::sort_args sortArgs;
while (++itr != rak::split_iterator(arg)) {
sortArgs.push_back(rak::trim(*itr));
if (sortArgs.back().empty())
throw torrent::input_error("One of the arguments is empty.");
}
for (torrent::Object::list_type::const_iterator itr = ++args.begin(), last = args.end(); itr != last; itr++)
sortArgs.push_back(itr->as_string());
control->view_manager()->set_sort_new(name, sortArgs);
}
@@ -447,6 +404,21 @@ apply_try_import(const std::string& path) {
control->core()->push_log("Could not read resource file: " + path);
}
void
apply_schedule(const torrent::Object::list_type& args) {
if (args.size() != 4)
throw torrent::input_error("Wrong argument count.");
torrent::Object::list_type::const_iterator itr = args.begin();
const std::string& arg1 = (itr++)->as_string();
const std::string& arg2 = (itr++)->as_string();
const std::string& arg3 = (itr++)->as_string();
const std::string& arg4 = (itr++)->as_string();
control->command_scheduler()->parse(arg1, arg2, arg3, arg4);
}
void
initialize_variables() {
utils::VariableMap* variables = control->variable();
@@ -514,18 +486,17 @@ initialize_variables() {
variables->insert("try_import", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_try_import)));
variables->insert("view_add", new utils::VariableStringSlot(NULL, rak::mem_fn(control->view_manager(), &core::ViewManager::insert_throw)));
variables->insert("view_filter", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_view_filter)));
variables->insert("view_filter_on", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_view_filter_on)));
variables->insert("view_filter", new utils::VariableListSlot(rak::ptr_fn(&apply_view_filter)));
variables->insert("view_filter_on", new utils::VariableListSlot(rak::ptr_fn(&apply_view_filter_on)));
variables->insert("view_sort", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_view_sort)));
variables->insert("view_sort_new", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_view_sort_new)));
variables->insert("view_sort_current", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_view_sort_current)));
variables->insert("view_sort", new utils::VariableListSlot(rak::ptr_fn(&apply_view_sort)));
variables->insert("view_sort_new", new utils::VariableListSlot(rak::ptr_fn(&apply_view_sort_new)));
variables->insert("view_sort_current", new utils::VariableListSlot(rak::ptr_fn(&apply_view_sort_current)));
variables->insert("key_layout", new utils::VariableAny(std::string("qwerty")));
variables->insert("schedule", new utils::VariableStringSlot(NULL, rak::mem_fn(control->command_scheduler(), &CommandScheduler::parse)));
variables->insert("schedule_remove", new utils::VariableStringSlot(NULL,
rak::mem_fn<const std::string&>(control->command_scheduler(), &CommandScheduler::erase)));
variables->insert("schedule", new utils::VariableListSlot(rak::ptr_fn(&apply_schedule)));
variables->insert("schedule_remove", new utils::VariableStringSlot(NULL, rak::mem_fn<const std::string&>(control->command_scheduler(), &CommandScheduler::erase)));
variables->insert("download_scheduler", new utils::VariableVoidSlot(rak::mem_fn(control->scheduler(), &core::Scheduler::update)));
@@ -581,23 +552,23 @@ initialize_variables() {
variables->insert("remove_untied", new utils::VariableVoidSlot(rak::ptr_fn(&apply_remove_untied)));
variables->insert("close_low_diskspace", new utils::VariableValueSlot(rak::value_fn(int64_t()), rak::ptr_fn(&apply_close_low_diskspace)));
variables->insert("stop_on_ratio", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_stop_on_ratio)));
variables->insert("stop_on_ratio", new utils::VariableListSlot(rak::ptr_fn(&apply_stop_on_ratio)));
variables->insert("on_insert", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_insert())));
variables->insert("on_erase", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_erase())));
variables->insert("on_open", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_open())));
variables->insert("on_close", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_close())));
variables->insert("on_start", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_start())));
variables->insert("on_stop", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_stop())));
variables->insert("on_hash_queued", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_hash_queued())));
variables->insert("on_hash_removed", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_hash_removed())));
variables->insert("on_hash_done", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_hash_done())));
variables->insert("on_finished", new utils::VariableStringSlot(NULL, rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_finished())));
variables->insert("on_insert", new utils::VariableListSlot(rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_insert())));
variables->insert("on_erase", new utils::VariableListSlot(rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_erase())));
variables->insert("on_open", new utils::VariableListSlot(rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_open())));
variables->insert("on_close", new utils::VariableListSlot(rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_close())));
variables->insert("on_start", new utils::VariableListSlot(rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_start())));
variables->insert("on_stop", new utils::VariableListSlot(rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_stop())));
variables->insert("on_hash_queued", new utils::VariableListSlot(rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_hash_queued())));
variables->insert("on_hash_removed", new utils::VariableListSlot(rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_hash_removed())));
variables->insert("on_hash_done", new utils::VariableListSlot(rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_hash_done())));
variables->insert("on_finished", new utils::VariableListSlot(rak::bind_ptr_fn(&apply_on_state_change, &control->core()->download_list()->slot_map_finished())));
variables->insert("enable_trackers", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_enable_trackers)));
variables->insert("encoding_list", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_encoding_list)));
variables->insert("encryption", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_encryption)));
variables->insert("encryption", new utils::VariableListSlot(rak::ptr_fn(&apply_encryption)));
variables->insert("handshake_log", new utils::VariableBool(false));
}
@@ -622,12 +593,15 @@ var_d2_get_value(Target target, GetFunc getFunc) {
}
void
apply_d_create_link(core::Download* download, const std::string& args) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(args, ',');
apply_d_create_link(core::Download* download, const torrent::Object::list_type& args) {
if (args.size() != 3)
throw torrent::input_error("Wrong argument count.");
std::string type = rak::trim(*itr); ++itr;
std::string prefix = rak::trim(*itr); ++itr;
std::string postfix = rak::trim(*itr); ++itr;
torrent::Object::list_type::const_iterator itr = args.begin();
const std::string& type = (itr++)->as_string();
const std::string& prefix = (itr++)->as_string();
const std::string& postfix = (itr++)->as_string();
if (type.empty())
throw torrent::input_error("Invalid arguments.");
@@ -663,12 +637,15 @@ apply_d_create_link(core::Download* download, const std::string& args) {
}
void
apply_d_delete_link(core::Download* download, const std::string& args) {
rak::split_iterator_t<std::string> itr = rak::split_iterator(args, ',');
apply_d_delete_link(core::Download* download, const torrent::Object::list_type& args) {
if (args.size() != 3)
throw torrent::input_error("Wrong argument count.");
std::string type = rak::trim(*itr); ++itr;
std::string prefix = rak::trim(*itr); ++itr;
std::string postfix = rak::trim(*itr); ++itr;
torrent::Object::list_type::const_iterator itr = args.begin();
const std::string& type = (itr++)->as_string();
const std::string& prefix = (itr++)->as_string();
const std::string& postfix = (itr++)->as_string();
if (type.empty())
throw torrent::input_error("Invalid arguments.");
@@ -782,6 +759,6 @@ initialize_download_variables() {
// Hmm... do we need dupicates?
variables->insert("print", new utils::VariableStringSlot(NULL, rak::mem_fn(control->core(), &core::Manager::push_log)));
variables->insert("create_link", new utils::VariableDownloadStringSlot(NULL, rak::ptr_fn(&apply_d_create_link)));
variables->insert("delete_link", new utils::VariableDownloadStringSlot(NULL, rak::ptr_fn(&apply_d_delete_link)));
variables->insert("create_link", new utils::VariableDownloadListSlot(rak::ptr_fn(&apply_d_create_link)));
variables->insert("delete_link", new utils::VariableDownloadListSlot(rak::ptr_fn(&apply_d_delete_link)));
}
+2
View File
@@ -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 \
+227
View File
@@ -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.");
}
}
}
+66
View File
@@ -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);
}
+30
View File
@@ -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.");
}
}
}
+30
View File
@@ -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
View File
@@ -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());
}
}