-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextensions.lua
446 lines (383 loc) · 12 KB
/
extensions.lua
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
-- os
function os.dirname(path)
return string.match(path, "(.*)[\\/]") or '.'
end
function os.basename(path)
return string.match(path, "([^\\/]*)$") or ''
end
if os.platform == 'windows' then
local socket = require'socket'
function os.pipe()
local p1, p2, lsock
lsock = assert(socket.bind("localhost", 0, 1))
local laddr, lport = lsock:getsockname()
p1 = assert(socket.connect(laddr, lport))
p2 = assert(lsock:accept())
local addr1, port1 = p1:getsockname()
local paddr2, pport2 = p2:getpeername()
assert(addr1 == paddr2 and tonumber(port1) == pport2, "address mismatch")
lsock:close()
p1:settimeout(0)
p2:settimeout(0)
return p1, p2
end
end
-- table
function table.index (t, v)
for i,x in ipairs (t) do
if x == v then
return i
end
end
end
-- string
do
local matches =
{
["^"] = "%^";
["$"] = "%$";
["("] = "%(";
[")"] = "%)";
["%"] = "%%";
["."] = "%.";
["["] = "%[";
["]"] = "%]";
["*"] = "%*";
["+"] = "%+";
["-"] = "%-";
["?"] = "%?";
["\0"]= "%z";
}
function string.quote_patterns (s)
return (s:gsub(".", matches))
end
end
function string.strip (str, chars)
if not str then return nil end
if chars then
chars = "["..string.quote_patterns(chars).."]"
else
chars = "[ \t\r\n]"
end
return string.match(str, "^"..chars.."*(.-)"..chars.."*$")
end
local ssub = string.sub
local sfind = string.find
function string.startswith(s, prefix)
return ssub(s, 1, #prefix) == prefix
end
function string.endswith(s, suffix)
return ssub(s, -#suffix) == suffix
end
function string.split (str, pat, n)
-- FIXME: transform into a closure based iterator?
pat = pat or "[ \t\r\n]+"
n = n or #str
local r = {}
local s, e = sfind (str, pat, 1)
if not s then return {str} end
if s ~= 1 then r[#r+1] = ssub(str, 1, s - 1) end
while true do
if e == #str then return r end
local ne
s, ne = sfind (str, pat, e + 1)
if not s or #r >= n then r[#r+1] = ssub(str, e + 1, #str) return r end
r[#r+1] = ssub(str, e + 1, s - 1)
e = ne
end
end
function string.splitv (str, pat, n)
return unpack(string.split (str, pat, n))
end
--[[ tests
do
local split = function (str, pat, n) return yd('split', string.split (str, pat, n)) end
split('foo/bar/baz/test','/')
--> {'foo','bar','baz','test'}
split('/foo/bar/baz/test','/')
--> {'foo','bar','baz','test'}
split('/foo/bar/baz/test/','/')
--> {'foo','bar','baz','test'}
split('/foo/bar//baz/test///','/')
--> {'foo','bar','','baz','test','',''}
split('//foo////bar/baz///test///','/+')
--> {'foo','bar','baz','test'}
split('foo','/+')
--> {'foo'}
split('','/+')
--> {}
split('foo','') -- splits a zero-sized string 3 (#str) times
--> {'','','',''}
split('a|b|c|d','|',2)
--> {'a','b','c|d'}
split('|a|b|c|d|','|',2)
--> {'a','b','c|d|')
end
--]]
function string.splitall (str, pat, n)
-- FIXME: transform into a closure based iterator?
pat = pat or "[ \t\r\n]+"
n = n or #str
local r = {}
local s = 0
local e = 0
while true do
local ne
s, ne = sfind (str, pat, e + 1)
if not s or #r >= n then r[#r+1] = ssub(str, e + 1, #str) return r end
r[#r+1] = ssub(str, e + 1, s - 1)
e = ne
end
end
function string.splitallv (str, pat, n)
return unpack(string.splitall (str, pat, n))
end
--[[ tests
do
local split = function (str, pat, n) local t = string.splitall (str, pat, n) yd('splitall', pat, str == table.concat (t, pat), t) return t end
split('foo/bar/baz/test','/')
--> {'foo','bar','baz','test'}
split('/foo/bar/baz/test','/')
--> {'','foo','bar','baz','test'}
split('/foo/bar/baz/test/','/')
--> {'','foo','bar','baz','test',''}
split('/foo/bar//baz/test///','/')
--> {'','foo','bar','','baz','test','','',''}
split('//foo////bar/baz///test///','/+')
--> {'','foo','bar','baz','test',''}
split('foo','/+')
--> {'foo'}
split('','/+')
--> {}
split('foo','') -- splits a zero-sized string 3 (#str) times
--> {'','','',''}
split('a|b|c|d','|',2)
--> {'a','b','c|d'}
end
--]]
--[[
START
The getopt function by Attractive Chaos <[email protected]>
--]]
--[[
The MIT License
Copyright (c) 2011, Attractive Chaos <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
-- Description: getopt() translated from the BSD getopt(); compatible with the default Unix getopt()
--[[ Example:
for o, a in os.getopt(arg, 'a:b') do
print(o, a)
end
]]--
function os.getopt(args, ostr)
local arg, place = nil, 0;
return function ()
if place == 0 then -- update scanning pointer
place = 1
if #args == 0 or args[1]:sub(1, 1) ~= '-' then place = 0; return nil end
if #args[1] >= 2 then
place = place + 1
if args[1]:sub(2, 2) == '-' then -- found "--"
place = 0
table.remove(args, 1);
return nil;
end
end
end
local optopt = args[1]:sub(place, place);
place = place + 1;
local oli = ostr:find(optopt);
if optopt == ':' or oli == nil then -- unknown option
if optopt == '-' then return nil end
if place > #args[1] then
table.remove(args, 1);
place = 0;
end
return '?';
end
oli = oli + 1;
if ostr:sub(oli, oli) ~= ':' then -- do not need argument
arg = nil;
if place > #args[1] then
table.remove(args, 1);
place = 0;
end
else -- need an argument
if place <= #args[1] then -- no white space
arg = args[1]:sub(place);
else
table.remove(args, 1);
if #args == 0 then -- an option requiring argument is the last one
place = 0;
if ostr:sub(1, 1) == ':' then return ':' end
return '?';
else arg = args[1] end
end
table.remove(args, 1);
place = 0;
end
return optopt, arg;
end
end
--[[
The getopt function by Attractive Chaos <[email protected]>
END
--]]
do
-- Copyright (c) 2012 Rob Hoelz <[email protected]>, 2020 LoEE – Jakub Piotr Cłapa <[email protected]>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
-- the Software, and to permit persons to whom the Software is furnished to do so,
-- subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
-- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
-- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
-- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
local sformat = string.format
local sgmatch = string.gmatch
local sgsub = string.gsub
local smatch = string.match
local tconcat = table.concat
local tinsert = table.insert
local setmetatable = setmetatable
local ploadlib = package.loadlib
local meta = {}
local _M = setmetatable({}, meta)
_M.VERSION = '0.01'
-- XXX assert(type(package.preload[name]) == 'function')?
local function preload_loader(name)
if package.preload[name] then
return package.preload[name]
else
return sformat("no field package.preload['%s']\n", name)
end
end
local function path_loader(name, paths, loader_func)
local errors = {}
local loader
name = sgsub(name, '%.', '/')
for path in sgmatch(paths, '[^;]+') do
path = sgsub(path, '%?', name)
local errmsg
loader, errmsg = loader_func(path)
if loader then
break
else
-- XXX error for when file isn't readable?
-- XXX error for when file isn't valid Lua (or loadable?)
tinsert(errors, sformat("no file '%s'", path))
end
end
if loader then
return loader
else
return tconcat(errors, '\n') .. '\n'
end
end
local function lua_loader(name)
return path_loader(name, package.path, function (path)
local fd = io.open(path, 'r')
if not fd then return nil end
local code = fd:read'*a'
fd:close()
local chunks = {string.format(
"local __SRC_DIR = %q; local function rrequire(name) return require(%q..name) end;",
os.dirname(path), name
), code}
local i = 0
local fun, err = load(function () i = i + 1 return chunks[i] end, '@'..path)
if not fun then error(err, 7) end
return fun
end)
end
local function get_init_function_name(name)
name = sgsub(name, '^.*%-', '', 1)
name = sgsub(name, '%.', '_')
return 'luaopen_' .. name
end
local function c_loader(name)
local init_func_name = get_init_function_name(name)
return path_loader(name, package.cpath, function(path)
return ploadlib(path, init_func_name)
end)
end
local function all_in_one_loader(name)
local init_func_name = get_init_function_name(name)
local base_name = smatch(name, '^[^.]+')
return path_loader(base_name, package.cpath, function(path)
return ploadlib(path, init_func_name)
end)
end
local function findchunk(name)
local errors = { string.format("module '%s' not found\n", name) }
local found
for _, loader in ipairs(_M.loaders) do
local chunk = loader(name)
if type(chunk) == 'function' then
return chunk
elseif type(chunk) == 'string' then
errors[#errors + 1] = chunk
end
end
return nil, table.concat(errors, '')
end
local function require(name)
if package.loaded[name] == nil then
local chunk, errors = findchunk(name)
if not chunk then
error(errors, 2)
end
local result = chunk(name)
if result ~= nil then
package.loaded[name] = result
elseif package.loaded[name] == nil then
package.loaded[name] = true
end
end
return package.loaded[name]
end
local loadermeta = {}
function loadermeta:__call(...)
return self.impl(...)
end
local function makeloader(loader_func, name)
return setmetatable({ impl = loader_func, name = name }, loadermeta)
end
-- XXX make sure that any added loaders are preserved (esp. luarocks)
_M.loaders = {
makeloader(preload_loader, 'preload'),
makeloader(lua_loader, 'lua'),
makeloader(c_loader, 'c'),
makeloader(all_in_one_loader, 'all_in_one'),
}
if package.loaded['luarocks.require'] then
local luarocks_loader = require('luarocks.require').luarocks_loader
table.insert(_M.loaders, 1, makeloader(luarocks_loader, 'luarocks'))
end
-- XXX sugar for adding/removing loaders
_G.require = require
_M.findchunk = findchunk
end