From 8dd9bc2815347435c8f92bb329a0209b50660618 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 9 Sep 2008 04:40:34 +0200 Subject: Kommandos in einzelne Argumente aufteilen --- src/Common/Util.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/madc.cpp | 10 ++++++++-- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/Common/Util.h b/src/Common/Util.h index 04b666c..71d7b69 100644 --- a/src/Common/Util.h +++ b/src/Common/Util.h @@ -22,6 +22,7 @@ #include #include +#include namespace Mad { namespace Common { @@ -54,6 +55,64 @@ class Util { return str.substr(beg, end); } + + static std::vector split(const std::string &str) { + std::vector ret; + std::string temp; + bool quoteSingle = false, quoteDouble = false, escape = false; + + size_t beg = 0; + + for(size_t cur = 0; cur < str.length(); ++cur) { + if(!escape) { + if(str[cur] == ' ' && !quoteSingle && !quoteDouble) { + if(cur == beg && temp.empty()) { + ++beg; + } + else { + temp += str.substr(beg, cur-beg); + ret.push_back(temp); + temp.clear(); + beg = cur+1; + } + + continue; + } + + if(str[cur] == '"' && !quoteSingle) { + temp += str.substr(beg, cur-beg); + beg = cur+1; + + quoteDouble = !quoteDouble; + continue; + } + if(str[cur] == '\'' && !quoteDouble) { + temp += str.substr(beg, cur-beg); + beg = cur+1; + + quoteSingle = !quoteSingle; + continue; + } + + if(str[cur] == '\\') { + escape = true; + continue; + } + } + + if(escape && ((!quoteSingle && !quoteDouble) || (quoteSingle && str[cur] == '\'') || (quoteDouble && (str[cur] == '"' || str[cur] == '\\')))) { + temp += str.substr(beg, cur-beg-1); + beg = cur; + } + + escape = false; + } + + temp += str.substr(beg, std::string::npos); + ret.push_back(temp); + + return ret; + } }; } diff --git a/src/madc.cpp b/src/madc.cpp index 06d65af..af39f2d 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -20,6 +20,7 @@ #include "Net/ClientConnection.h" #include "Net/IPAddress.h" #include "Common/RequestManager.h" +#include "Common/Util.h" #include "Client/RequestProcessor.h" #include @@ -48,13 +49,18 @@ static void handleCommand(char *cmd) { if(!*cmd) return; - if(std::strcmp(cmd, "quit") == 0) { + std::vector splitCmd = Mad::Common::Util::split(cmd); + + if(splitCmd.empty()) + return; + + if(splitCmd[0] == "quit") { processor->requestDisconnect(); rl_callback_handler_remove(); } else { - std::cerr << "Unknown command \"" << cmd << "\"." << std::endl; + std::cerr << "Unknown command \"" << splitCmd[0] << "\"." << std::endl; } add_history(cmd); -- cgit v1.2.3