From fefafd4923e824cb5a7f762c7e65d24cdc1f8d87 Mon Sep 17 00:00:00 2001 From: Er2 Date: Sun, 13 Feb 2022 15:22:12 +0300 Subject: [PATCH] quoted args are optional --- core.lua | 37 +++++-------------------------------- tools.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 32 deletions(-) diff --git a/core.lua b/core.lua index 65877ef..14971ce 100644 --- a/core.lua +++ b/core.lua @@ -7,6 +7,9 @@ local tools = require 'api.tools' local api = require 'api.api' api.__index = api +api.parseArgs = tools.parseArgs +api.unparseArgs = tools.unparseArgs + function api:_ev(t, i, name, ...) local v = t[i] if v.name == name then @@ -37,38 +40,8 @@ local function receiveUpdate(self, update) -- remove needn't text msg.text = msg.text:sub(#cmd + #(to or '') + 3) - - -- "quoted" and not quoted args support - -- inside quotes, buffer and final args - local iq, buf, args = false, '', {} - local qt, esc = nil, false -- quote type and escape, addition - - -- helper function, add args and clean buffer - local function psh() - if #buf > 0 then table.insert(args, buf) end - buf = '' - qt = nil - end - - for i = 1, #msg.text do - local v = msg.text:sub(i, i) - print(v, buf, iq) - -- if space and not inside quotes - if (v == ' ' or v == '\n' or v == '\t') - and not iq then psh() - - elseif esc then esc, buf = false, buf.. v - elseif v == '\\' then esc = true - - elseif (not qt or qt == v) - and (v == '"' or v == "'") then - psh() - qt, iq = v, not iq -- quote type, inside quote - - else buf = buf.. v -- append buffer - end - end - psh() -- if has something + local args = {} + for v in msg.text:gmatch '%S+' do table.insert(args, v) end msg.cmd = cmd msg.args = args diff --git a/tools.lua b/tools.lua index 749e176..c6d80c5 100644 --- a/tools.lua +++ b/tools.lua @@ -18,6 +18,57 @@ function tools.fetchCmd(text) text:match '/[%w_]+@([%w_]+)' -- to end +function tools.parseArgs(text) + -- inside quotes, buffer and final args + local iq, buf, args = false, '', {} + local qt, esc = nil, false -- quote type and escape, addition + + -- helper functin, add args and clear buffer + local function psh() + if #buf > 0 then table.insert(args, buf) end + buf, qt = '', nil + end + + for i = 1, #text + 1 do + local v = text:sub(i, i) + -- if space and inside quotes, or end of string + if ((v == ' ' or v == '\n' or v == '\t') and not iq) + or v == '' + then psh() + + elseif esc then esc, buf = false, buf.. v + elseif v == '\\' then esc = true + + -- if (no current quote or same quote) and quote is supported + elseif (not qt or qt == v) + and (v == '"' or v == "'") + then psh() + qt, iq = v, not iq -- quote type, inside quote + + else buf = buf.. v -- append buffer + end + end + + return args +end + +function tools.unparseArgs(args) + -- args to string + local text = '' + + for i = 1, #args do + local v = args[i] + if type(v) == 'string' then + if v:match '%s' + then text = text.. '"'.. v ..'" ' + else text = text.. v ..' ' + end + end + end + + return text +end + function tools._req(url, meth, data, ctype) assert(url, 'Provide URL!') assert(meth, 'Provide method!')