summaryrefslogtreecommitdiff
path: root/lib/gitano/util.lua
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-03-28 14:06:12 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-03-28 14:06:12 +0100
commita33232f15be993eb7439a8a449beee36f1acbf36 (patch)
tree30a249e5a6ca8f9ce41350274f606a652746db67 /lib/gitano/util.lua
parent61ba85e9b1b778f16600b2463548ffd2e7ded3c5 (diff)
downloadgitano-a33232f15be993eb7439a8a449beee36f1acbf36.tar.gz
GITANO: Simple filename replace
Diffstat (limited to 'lib/gitano/util.lua')
-rw-r--r--lib/gitano/util.lua95
1 files changed, 95 insertions, 0 deletions
diff --git a/lib/gitano/util.lua b/lib/gitano/util.lua
new file mode 100644
index 0000000..8d594d7
--- /dev/null
+++ b/lib/gitano/util.lua
@@ -0,0 +1,95 @@
+-- legit.util
+--
+-- Low level utility routines for Legit
+--
+-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
+--
+--
+
+local function _deep_copy(t, memo)
+ if not memo then memo = {} end
+ if memo[t] then return memo[t] end
+ local ret = {}
+ local kk, vv
+ for k, v in pairs(t) do
+ kk, vv = k, v
+ if type(k) == "table" then
+ kk = _deep_copy(k)
+ end
+ if type(v) == "table" then
+ vv = _deep_copy(v)
+ end
+ ret[kk] = vv
+ end
+ return ret
+end
+
+local function _parse_cmdline(cmdline)
+ local r = {}
+ local acc = ""
+ local c
+ local escaping = false
+ local quoting = false
+ while #cmdline > 0 do
+ c, cmdline = cmdline:match("^(.)(.*)$")
+ if escaping then
+ if c == "n" then
+ acc = acc .. "\n"
+ elseif c == "t" then
+ acc = acc .. "\t"
+ else
+ acc = acc .. c
+ end
+ escaping = false
+ else
+ if c == "'" and quoting == false then
+ -- Start single quotes
+ quoting = c
+ elseif c == '"' and quoting == false then
+ -- Start double quotes
+ quoting = c
+ elseif c == "'" and quoting == c then
+ -- End single quotes
+ quoting = false
+ elseif c == '"' and quoting == c then
+ -- End double quotes
+ quoting = false
+ elseif c == "\\" then
+ -- A backslash, entering escaping mode
+ escaping = true
+ elseif quoting then
+ -- Within quotes, so accumulate
+ acc = acc .. c
+ elseif c == " " then
+ -- A space and not quoting, so clear the accumulator
+ if acc ~= "" then
+ r[#r+1] = acc
+ end
+ acc = ""
+ else
+ acc = acc .. c
+ end
+ end
+ end
+ if acc ~= "" then
+ r[#r+1] = acc
+ end
+
+ local warnings = {}
+ if quoting then
+ warnings[#warnings+1] = "Un-terminated quoted string"
+ end
+ if escaping then
+ warnings[#warnings+1] = "Un-used escape at end"
+ end
+ if #r == 0 then
+ warnings[#warnings+1] = "No command found?"
+ end
+
+ return r, warnings
+end
+
+return {
+ parse_cmdline = _parse_cmdline,
+ deep_copy = _deep_copy
+}