summaryrefslogtreecommitdiff
path: root/commands.cpp
diff options
context:
space:
mode:
authorBossCode45 <human.cyborg42@gmail.com>2023-06-21 20:38:24 +1200
committerBossCode45 <human.cyborg42@gmail.com>2023-06-21 20:38:24 +1200
commit4f090897faae6c6d8e451281dc862d58bf9f05ba (patch)
tree16124789c04fbe52b2b51266e299f7d26259955f /commands.cpp
parent8b4b487e685c72a478baf74aea2bf756988fe550 (diff)
parent5c799ffa098f6c9b6d88513c2662968a9f5aaa41 (diff)
downloadYATwm-4f090897faae6c6d8e451281dc862d58bf9f05ba.tar.gz
YATwm-4f090897faae6c6d8e451281dc862d58bf9f05ba.zip
Merge branch 'config-refactor'
Added a proper arg parser and made "\" an escape character
Diffstat (limited to 'commands.cpp')
-rw-r--r--commands.cpp62
1 files changed, 52 insertions, 10 deletions
diff --git a/commands.cpp b/commands.cpp
index 4e8e01b..de7d81e 100644
--- a/commands.cpp
+++ b/commands.cpp
@@ -72,18 +72,60 @@ Command* CommandsModule::lookupCommand(string name)
vector<string> CommandsModule::splitCommand(string command)
{
vector<string> v;
- string regexString = "((?:\"[^\"\n]+\")|(?:'[^'\n]+')|(?:[^ \n]+))+";
- std::regex regex(regexString, std::regex_constants::_S_icase);
- auto begin = std::sregex_iterator(command.begin(), command.end(), regex);
- auto end = std::sregex_iterator();
- for (std::sregex_iterator i = begin; i != end; i++)
+ string arg = "";
+ bool inQuotes = false;
+ bool escapeNext = true;
+ char quoteType;
+ for(int i = 0; i < command.size(); i++)
{
- std::smatch match = *i;
- std::string str = match.str();
- if(str[0] == '\'' || str[0] == '"')
- str = str.substr(1, str.size() - 2);
- v.push_back(str);
+ if(escapeNext)
+ {
+ arg += command[i];
+ escapeNext = false;
+ }
+ else if(command[i] == '\\')
+ {
+ escapeNext = true;
+ }
+ else if(inQuotes)
+ {
+ if(command[i] == quoteType)
+ {
+ if(arg != "")
+ {
+ v.push_back(arg);
+ arg = "";
+ }
+ inQuotes = false;
+ }
+ else
+ {
+ arg += command[i];
+ }
+ }
+ else
+ {
+ if(command[i] == ' ')
+ {
+ if(arg != "")
+ {
+ v.push_back(arg);
+ arg = "";
+ }
+ }
+ else if(command[i] == '"' || command[i] == '\'')
+ {
+ inQuotes = true;
+ quoteType = command[i];
+ }
+ else
+ {
+ arg += command[i];
+ }
+ }
}
+ if(arg != "")
+ v.push_back(arg);
return v;
}