-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRadLine.lua
314 lines (275 loc) · 9.02 KB
/
RadLine.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
local win32 = require "lrwin32"
require "Utils"
-- function FindRegKey(s)
function DebugOutLn(s)
win32.OutputDebugString(s .. "\n")
end
function CaseInsensitiveLess(s1, s2)
return s1:lower() < s2:lower()
end
DebugOutLn("RadLine ".._VERSION)
internal = {
"assoc", "call", "cd", "chdir", "cls", "color", "copy", "date", "del", "dir", "echo", "endlocal", "erase", "exit", "for",
"ftype", "goto", "if", "md", "mkdir", "mklink", "move", "path", "popd", "prompt", "pushd", "rem", "ren", "rd", "rmdir",
"set", "setlocal", "shift", "start", "time", "title", "type", "ver", "verify", "vol"
}
function FileNameIsDir(s)
return s:sub(-1) == "\\";
end
function FileNameLess(s1, s2)
if FileNameIsDir(s1) and not FileNameIsDir(s2) then
return true;
elseif not FileNameIsDir(s1) and FileNameIsDir(s2) then
return false;
else
return CaseInsensitiveLess(s1, s2)
end
end
FindFilesE = { All = 0, DirOnly = 1, FileOnly = 2 }
function FindFiles(s, e)
assert(table.contains(FindFilesE, e), "e=" .. tostring(e) .. " is not in FindFilesE")
s = s:gsub('"', '')
if s:beginswith("~\\") and tonumber(win32.GetEnvironmentVariable("RADLINE_TILDE") or 0) ~= 0 then
s = "%USERPROFILE%" .. s:sub(2, -1)
end
win32.SetEnvironmentVariable("CD", win32.GetCurrentDirectory())
s = win32.ExpandEnvironmentStrings(s)
win32.SetEnvironmentVariable("CD", nil)
local files = {}
local FindFileData = {}
local hFind = win32.FindFirstFile(s, FindFileData)
if hFind ~= win32.INVALID_HANDLE_VALUE then
repeat
if FindFileData.cFileName ~= "." and FindFileData.cFileName ~= ".." then
--if FindFileData.cFileName ~= "." and FindFileData.cFileName ~= ".." and (FindFileData.dwFileAttributes & win32.FILE_ATTRIBUTE.HIDDEN) == 0 then
local add = true;
if e == FindFilesE.FileOnly and (FindFileData.dwFileAttributes & win32.FILE_ATTRIBUTE.DIRECTORY) ~= 0 then add = false;
elseif e == FindFilesE.DirOnly and (FindFileData.dwFileAttributes & win32.FILE_ATTRIBUTE.DIRECTORY) == 0 then add = false;
end
if add then
if (FindFileData.dwFileAttributes & win32.FILE_ATTRIBUTE.DIRECTORY) ~= 0 then
FindFileData.cFileName = FindFileData.cFileName.."\\"
end
files[#files+1] = FindFileData.cFileName
end
end
until not win32.FindNextFile(hFind, FindFileData)
win32.FindClose(hFind);
end
table.sort(files, FileNameLess)
return files;
end
function FindExeFiles(s)
local pathext = win32.GetEnvironmentVariable("PATHEXT"):split(";")
local dot = s:match'^.*()%.' -- Find last of '.'
local slash = s:match'^.*()\\' or 0 -- Find last of '\'
local ext = (dot and dot > slash) and s:sub(dot):upper() or nil
local f = {}
if not ext then
for _,i in ipairs(pathext) do
table.concat(f, FindFiles(s.."*"..i, FindFilesE.FileOnly))
end
else
for _,i in ipairs(pathext) do
local dot = i:match'^.*()%.' -- Find last of '.'
local iext = (dot and dot > 1) and i:sub(dot) or nil
if i:upper():beginswith(ext) then
table.concat(f, FindFiles(s..i:sub(#ext + 1), FindFilesE.FileOnly))
end
if iext and iext:upper():beginswith(ext) then
table.concat(f, FindFiles(s..i:sub(dot + #ext), FindFilesE.FileOnly))
end
--table.concat(f, FindFiles(s.."*"..i, FindFilesE.FileOnly))
end
end
return f
end
function FindPathExeFiles(s)
local path = win32.GetEnvironmentVariable("PATH"):split(";")
local f = {}
for _,i in ipairs(path) do
table.concat(f, FindExeFiles(i.."\\"..s))
end
return f
end
function FindEnv(s, enclose)
s = s:upper()
local e = {}
local env = win32.GetEnvironmentStrings();
for k,v in pairs(env) do
if k:upper():beginswith(s) then
if enclose then
e[#e+1] = "%"..k.."%"
else
e[#e+1] = k
end
end
end
return e;
end
function FindEnvBegin(s)
local count = 0
for c in s:gmatch("%%") do
count = count + 1
end
if count%2 == 1 then
return s:match'^.*()%%' -- Find last of '%'
else
return nil
end
end
function FindAlias(s)
s = s:lower()
local a = {}
local aliases = win32.GetConsoleAliases("cmd.exe")
for k,v in pairs(aliases) do
if k:lower():beginswith(s) then
a[#a+1] = k
end
end
return a;
end
command_sep = {
["&"] = true;
["|"] = true;
["&&"] = true;
["||"] = true;
}
redirect_sep = {
[">"] = true;
[">>"] = true;
["<"] = true;
["2>"] = true;
}
function GetParam(params, p)
local s = params[p] or ""
local i = s:match'^.*()[\\/]' -- Find last of '\' or '/'
i = i and i + 1 or 1
if #s > 0 and s:sub(1, 1) == '"' then
s = s:sub(2)
if i == 1 then
i = i + 1
end
end
if #s > 0 and s:sub(#s, #s) == '"' then
s = s:sub(1, -2)
end
return s, i
end
function FindPotentialDefault(params, p)
local s,i = GetParam(params, p)
return FindFiles(s.."*", FindFilesE.All), i
end
function FindPotentialExe(params, p) -- checks path, no alias
local s,i = GetParam(params, p)
local f = {}
table.concat_if(f, s:lower(), internal)
table.concat(f, FindFiles(s.."*", FindFilesE.DirOnly))
table.concat(f, FindExeFiles(s))
table.concat(f, FindPathExeFiles(s))
return f, i
end
function FindPotentialDirs(params, p)
local s,i = GetParam(params, p)
return FindFiles(s.."*", FindFilesE.DirOnly), i
end
function FindPotentialDlls(params, p)
local s,i = GetParam(params, p)
return FindFiles(s.."*.dll", FindFilesE.FileOnly), i
end
function FindPotentialAlias(params, p)
local s,i = GetParam(params, p)
return FindAlias(s)
end
function FindPotentialEnv(params, p)
local s,i = GetParam(params, p)
return FindEnv(s, false)
end
function LookUpExt(t, key)
local pathext = win32.GetEnvironmentVariable("PATHEXT"):split(";")
for _,i in ipairs(pathext) do
local f = rawget(t, (key..i):lower())
if f then
return f
end
end
return rawget(t, "_default")
end
command_fn = setmetatable({
_default = FindPotentialDefault,
cd = FindPotentialDirs,
chdir = FindPotentialDirs,
md = FindPotentialDirs,
mkdir = FindPotentialDirs,
pushd = FindPotentialDirs,
}, { __index = LookUpExt })
command_options = setmetatable({}, { __index = LookUpExt })
command_command = setmetatable({}, { __index = LookUpExt }) -- first command line argument
other_env = {
"%CD%", "%CMDCMDLINE%", "%CMDEXTVERSION%", "%DATE%", "%ERRORLEVEL%", "%RANDOM%", "%TIME%",
"%FIRMWARE_TYPE%", "%HighestNumaNodeNumber%",
"%__APPDIR__%", "%__CD__%", "%__PID__%", "%__TICK__%", "%__USERCD__%", "%RADLINE_LOADED%", "%RADLINE_DIR%",
"%=ExitCode%", "%=ExitCodeAscii%", "%=C:%", "%=D:%", "%=E:%", "%=F:%"
}
function GetCmdOutput(cmd)
local d = {}
local f1 = io.popen(cmd, 'r')
for line in f1:lines() do
d[#d+1] = line
end
f1:close()
return d
end
command_output = setmetatable({}, { __index = LookUpExt })
function FindPotentialCmdOutput(params, p)
local s,i = GetParam(params, p)
local command = params[1]
local f = {}
table.concat_if(f, s:lower(), GetCmdOutput(command_output[command]))
return f
end
function FindPotential(params, p)
local s,i = GetParam(params, p)
local e = FindEnvBegin(s)
if e then
local f = {}
table.concat(f, FindEnv(s:sub(e + 1), true))
table.concat_if(f, s:sub(e):lower(), other_env)
return f, (e + i - 1)
elseif p == 1 or command_sep[params[p - 1]] then
local f = {}
if i == 1 then
table.concat_if(f, s:lower(), internal)
table.concat(f, FindFiles(s.."*", FindFilesE.DirOnly))
table.concat(f, FindExeFiles(s))
table.concat(f, FindPathExeFiles(s))
table.concat(f, FindAlias(s))
else
table.concat(f, FindFiles(s.."*", FindFilesE.DirOnly))
table.concat(f, FindExeFiles(s))
end
return f, i
elseif p > 1 and redirect_sep[params[p - 1]] then
return FindFiles(s.."*", FindFilesE.All), i
else
local command = params[1]
local cmd = command_command[command]
if s:sub(1, 1) == "/" or s:sub(1, 1) == "-" then
local f = {}
table.concat_if(f, s:lower(), command_options[command])
return f
elseif p == 2 and cmd then
if type(cmd) == "function" then
return cmd(params, p)
else
local f = {}
table.concat_if(f, s:lower(), cmd)
return f
end
else
-- TODO Unquote and remove path ???
local fn = command_fn[command]
return fn(params, p)
end
end
end