summaryrefslogtreecommitdiff
path: root/lib/gitano/util.lua
blob: c8834d3e2a9a3221dff0e580b0985709893cb33c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
-- gitano.util
--
-- Low level utility routines for Gitano
--
-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
--
--

local luxio = require 'luxio'
local sio = require 'luxio.simple'

local tconcat = table.concat

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, memo)
      end
      if type(v) == "table" then
	 vv = _deep_copy(v, memo)
      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

local function patesc(s)
   return ((s:gsub("[%%%*%-%?%.%+%[%]%(%)]", "%%%0"):gsub("%z", "%%z")))
end

local function path_components(path)
   local ret = {}
   for elem in path:gmatch("([^/]*)/*") do
      ret[#ret+1] = elem
   end
   ret[#ret] = nil
   return ret
end

local function dirname(path)
   local t = path_components(path)
   t[#t] = nil
   return table.concat(t, "/")
end

local function basename(path, ext)
   local t = path_components(path)
   local ret = t[#t]
   if ext then
      local pat = patesc(ext) .. "$"
      if ret:find(pat) then
	 ret = ret:sub(1, -(#ext+1))
      end
   end
   return ret
end

local function mkdir_p(path, mode)
   if not mode then
      mode = sio.tomode("0755")
   end

   if path == "" or path == "/" then
      return true
   end

   if path:find("/") then
      local ok, msg = mkdir_p(dirname(path))
      if not ok then
	 return ok, msg
      end
   end

   local r, err = luxio.mkdir(path, mode)
   if r < 0 then
      if err == luxio.EEXIST then
	 return true
      end
      return nil, "mkdir(" .. path .. "): " .. luxio.strerror(err)
   end
   return true
end

local function rm_rf(path)
   local ret, err, dirp
   dirp, err = luxio.opendir(path)
   if not dirp then
      return false, luxio.strerror(err)
   end
   local e, i
   repeat
      e, i = luxio.readdir(dirp)
      if e == 0 then
	 if i.d_name ~= "." and i.d_name ~= ".." then
	    local elem = path .. "/" .. i.d_name
	    ret, err = luxio.unlink(elem)
	    if ret ~= 0 and err == luxio.EISDIR then
	       ret, err = rm_rf(elem)
	       if not ret then
		  return ret, err
	       end
	    elseif ret ~= 0 then
	       return false, luxio.strerror(err)
	    end
	 end
      end
   until not e

   -- explicitly close the dir so we can remove it
   luxio.closedir(dirp)
   ret, err = luxio.rmdir(path)
   return (ret == 0), luxio.strerror(err)
end

local function html_escape(s)
   return (s:gsub("&", "&amp;"):
	   gsub("<", "&lt;"):
	   gsub(">", "&gt;"):
	   gsub('"', "&quot;"))
end

local tagname_pattern = "^[a-z0-9_%-/]*[a-z0-9_%-]*$"

local function prep_expansion(str)
   -- Parse 'str' and return a table representing a sequence of
   -- operations required to evaluate the expansion of the string.
   -- in the simple case, it's merely the string in a table
   -- if the entry in ret is a string, it's copied.  If it's a table
   -- then that table's [1] is a string which is a tag name to expand.
   local ret = {}
   local acc = ""
   local c
   local seen = false
   while #str > 0 do
      c, str = str:match("^(.)(.*)$")
      if seen == false then
	 if c == "$" then
	    seen = c
	 else
	    acc = acc .. c
	 end
      elseif seen == "$" then
	 if c == "{" then
	    seen = c
	    if acc ~= "" then
	       ret[#ret+1] = acc
	       acc = ""
	    end
	 else
	    acc = acc .. c
	    seen = false
	 end
      elseif seen == "{" then
	 if c == "}" then
	    seen = false
	    assert(acc:match(tagname_pattern), "Expected tag name in string expansion of ${...}")
	    ret[#ret+1] = { acc }
	    acc = ""
	 else
	    acc = acc .. c
	 end
      end
   end
   if seen == "$" then
      acc = acc .. seen
   elseif seen ~= false then
      error("Unterminated tag expansion in string.")
   end
   if acc ~= "" then
      ret[#ret+1] = acc
   end

   return ret
end

local function process_expansion(tags, expn, tagsactive)
   -- Expand expn with tags and return a single string
   if type(expn) == "string" then
      return expn
   end
   if not tagsactive then
      tagsactive = {}
   end
   local r = {}
   for i = 1, #expn do
      local elem = expn[i]
      if type(elem) == "string" then
	 r[#r+1] = elem
      else
	 elem = elem[1]
	 if tagsactive[elem] then
	    return do_deny(tags, "Loop detected in tag expansion")
	 end
	 local tag = tags[elem]
	 if type(tag) == "function" then
	    tags[elem] = tag(tags)
	    tag = tags[elem]
	 end
	 if type(tag) == "string" then
	    tagsactive[elem] = true
	    tag = process_expansion(tags, tag, tagsactive)
	    tagsactive[elem] = nil
	 else
	    -- Can't implicitly expand lists etc.
	    tag = ""
	 end
	 r[#r+1] = tag
      end
   end
   return tconcat(r)
end

local function set(t)
   local ret = {}
   for i, v in ipairs(t) do
      ret[i] = v
      ret[v] = i
   end
   return ret
end

local function add_splitable(context, key, value, splitter,
			     prefix_name, suffix_name)
   if not value or value == "" then
      return
   end
   local function _(k, v)
      context[k] = v
   end
   _(key, value)
   local prefix, suffix = value:match("^(.*%" .. splitter .. ")" ..
				      "([^%" .. splitter .. "]+)$")

   if prefix then
      _(key .. "/" .. prefix_name, prefix:sub(1, -2))
      _(key .. "/" .. suffix_name, suffix)
   else
      _(key .. "/" .. suffix_name, value)
   end

   local i = 1
   for section in value:gmatch("([^%" .. splitter .. "]+)") do
      _(key .. "/" .. tostring(i), section)
      i = i + 1
   end
end

return {
   parse_cmdline = _parse_cmdline,

   patesc = patesc,

   path_components = path_components,
   dirname = dirname,
   basename = basename,

   mkdir_p = mkdir_p,
   rm_rf = rm_rf,

   html_escape = html_escape,

   deep_copy = _deep_copy,

   prep_expansion = prep_expansion,

   add_splitable = add_splitable,
   process_expansion = process_expansion,

   set = set,
}