code / nomsu

Lines6.6K Lua5.1K PEG1.3K make117
2 others 83
Markdown60 Bourne Again Shell23
(220 lines)
1 local lpeg = require('lpeg')
2 local re = require('re')
3 local Files = { }
4 local run_cmd
5 run_cmd = function(cmd)
6 local f = io.popen(cmd .. ' 2>/dev/null')
7 local lines
8 do
9 local _accum_0 = { }
10 local _len_0 = 1
11 for line in f:lines() do
12 _accum_0[_len_0] = line
13 _len_0 = _len_0 + 1
14 end
15 lines = _accum_0
16 end
17 if not (f:close()) then
18 return nil
19 end
20 return lines
21 end
22 local _SPOOFED_FILES = { }
23 local _BROWSE_CACHE = { }
24 local _anon_number = 0
25 Files.spoof = function(filename, contents)
26 if not contents then
27 filename, contents = "<anonymous file #" .. tostring(_anon_number) .. ">", filename
28 _anon_number = _anon_number + 1
29 end
30 _SPOOFED_FILES[filename] = contents
31 return filename
32 end
33 Files.read = function(filename)
34 do
35 local contents = _SPOOFED_FILES[filename]
36 if contents then
37 return contents
38 end
39 end
40 if filename == 'stdin' or filename == '-' then
41 local contents = io.read('*a')
42 Files.spoof('stdin', contents)
43 Files.spoof('-', contents)
44 return contents
45 end
46 local file = io.open(filename)
47 if not (file) then
48 return nil
49 end
50 local contents = file:read("*a")
51 file:close()
52 return contents or nil
53 end
54 local match, gsub
55 do
56 local _obj_0 = string
57 match, gsub = _obj_0.match, _obj_0.gsub
58 end
59 local sanitize
60 sanitize = function(path)
61 path = gsub(path, "\\", "\\\\")
62 path = gsub(path, "`", "")
63 path = gsub(path, '"', '\\"')
64 path = gsub(path, "$", "")
65 return path
66 end
67 Files.exists = function(path)
68 if _SPOOFED_FILES[path] then
69 return true
70 end
71 if path == 'stdin' or path == '-' then
72 return true
73 end
74 if run_cmd("ls " .. tostring(sanitize(path))) then
75 return true
76 end
77 return false
78 end
79 Files.list = function(path)
80 if not (_BROWSE_CACHE[path]) then
81 local files
82 if _SPOOFED_FILES[path] or path == 'stdin' or path == '-' then
83 _BROWSE_CACHE[path] = {
84 path
86 else
87 _BROWSE_CACHE[path] = run_cmd('find -L "' .. path .. '" -not -path "*/\\.*" -type f') or false
88 end
89 end
90 return _BROWSE_CACHE[path]
91 end
92 Files.make_directory = function(path)
93 return run_cmd('mkdir ' .. path)
94 end
95 local ok, lfs = pcall(require, "lfs")
96 if ok then
97 local raw_file_exists
98 raw_file_exists = function(filename)
99 local mode = lfs.attributes(filename, 'mode')
100 if mode == 'file' or mode == 'directory' or mode == 'link' then
101 return true
102 else
103 return false
104 end
105 end
106 Files.exists = function(path)
107 if _SPOOFED_FILES[path] then
108 return true
109 end
110 if path == 'stdin' or path == '-' or raw_file_exists(path) then
111 return true
112 end
113 return false
114 end
115 Files.list = function(path)
116 if not (_BROWSE_CACHE[path]) then
117 if _SPOOFED_FILES[path] or path == 'stdin' or path == '-' then
118 _BROWSE_CACHE[path] = {
119 path
121 else
122 local file_type, err = lfs.attributes(path, 'mode')
123 local _exp_0 = file_type
124 if "file" == _exp_0 or "char device" == _exp_0 then
125 _BROWSE_CACHE[path] = {
126 path
128 elseif "directory" == _exp_0 or "link" == _exp_0 then
129 local files = { }
130 for subfile in lfs.dir(path) do
131 local _continue_0 = false
132 repeat
133 if subfile == "." or subfile == ".." then
134 _continue_0 = true
135 break
136 end
137 local _list_0 = (Files.list(path .. "/" .. subfile) or { })
138 for _index_0 = 1, #_list_0 do
139 local f = _list_0[_index_0]
140 files[#files + 1] = f
141 end
142 _continue_0 = true
143 until true
144 if not _continue_0 then
145 break
146 end
147 end
148 _BROWSE_CACHE[path] = files
149 else
150 _BROWSE_CACHE[path] = false
151 end
152 end
153 if _BROWSE_CACHE[path] then
154 for i, f in ipairs(_BROWSE_CACHE[path]) do
155 if f:match("^%./") then
156 _BROWSE_CACHE[path][i] = f:sub(3)
157 end
158 end
159 end
160 end
161 return _BROWSE_CACHE[path]
162 end
163 Files.make_directory = lfs.mkdir
164 else
165 if not (run_cmd('find . -maxdepth 0')) then
166 local url
167 if jit then
168 url = 'https://github.com/spacewander/luafilesystem'
169 else
170 url = 'https://github.com/keplerproject/luafilesystem'
171 end
172 error("Could not find 'luafilesystem' module and couldn't run system command `find` (this might happen on Windows). Please install `luafilesystem` (which can be found at: " .. tostring(url) .. " or `luarocks install luafilesystem`)\n" .. tostring(lfs) .. "\npackage.cpath: " .. tostring(package.cpath), 0)
173 end
174 end
175 local line_counter = re.compile([[ lines <- {| line (%nl line)* |}
176 line <- {} (!%nl .)*
177 ]], {
178 nl = lpeg.P("\r") ^ -1 * lpeg.P("\n")
180 local _LINE_STARTS = { }
181 Files.get_line_starts = function(str)
182 if type(str) ~= 'string' then
183 str = tostring(str)
184 end
186 local starts = _LINE_STARTS[str]
187 if starts then
188 return starts
189 end
190 end
191 local line_starts = line_counter:match(str)
192 _LINE_STARTS[str] = line_starts
193 return line_starts
194 end
195 Files.get_line_number = function(str, pos)
196 local line_starts = Files.get_line_starts(str)
197 local lo, hi = 1, #line_starts
198 while lo <= hi do
199 local mid = math.floor((lo + hi) / 2)
200 if line_starts[mid] > pos then
201 hi = mid - 1
202 else
203 lo = mid + 1
204 end
205 end
206 return hi
207 end
208 Files.get_line = function(str, line_no)
209 local line_starts = Files.get_line_starts(str)
210 local start = line_starts[line_no]
211 if not (start) then
212 return
213 end
214 local stop = line_starts[line_no + 1]
215 if not (stop) then
216 return
217 end
218 return (str:sub(start, stop - 2))
219 end
220 return Files