1if not modules then modules = { } end modules ['util-env'] = {
2 version = 1.001,
3 comment = "companion to luat-lib.mkiv",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files"
7}
8
9local allocate, mark = utilities.storage.allocate, utilities.storage.mark
10
11local format, sub, match, gsub, find = string.format, string.sub, string.match, string.gsub, string.find
12local unquoted, quoted, optionalquoted = string.unquoted, string.quoted, string.optionalquoted
13local concat, insert, remove = table.concat, table.insert, table.remove
14local globfiles = dir.glob
15
16environment = environment or { }
17local environment = environment
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
77os.setlocale(nil,nil) function os.setlocale() end
78
79
80
81local validengines = allocate {
82 ["luatex"] = true,
83 ["luajittex"] = true,
84}
85
86local basicengines = allocate {
87 ["luatex"] = "luatex",
88 ["texlua"] = "luatex",
89 ["texluac"] = "luatex",
90 ["luajittex"] = "luajittex",
91 ["texluajit"] = "luajittex",
92}
93
94local luaengines = allocate {
95 ["lua"] = true,
96 ["luajit"] = true,
97}
98
99environment.validengines = validengines
100environment.basicengines = basicengines
101
102
103
104
105
106
107
108if not arg then
109 environment.used_as_library = true
110
111elseif luaengines[file.removesuffix(arg[-1])] then
112
113
114
115
116
117
118
119
120elseif validengines[file.removesuffix(arg[0])] then
121 if arg[1] == "--luaonly" then
122 arg[-1] = arg[0]
123 arg[ 0] = arg[2]
124 for k=3,#arg do
125 arg[k-2] = arg[k]
126 end
127 remove(arg)
128 remove(arg)
129 else
130
131 end
132
133
134
135
136
137
138
139
140 local originalzero = file.basename(arg[0])
141 local specialmapping = { luatools == "base" }
142
143 if originalzero ~= "mtxrun" and originalzero ~= "mtxrun.lua" then
144 arg[0] = specialmapping[originalzero] or originalzero
145 insert(arg,0,"--script")
146 insert(arg,0,"mtxrun")
147 end
148
149end
150
151
152
153environment.arguments = allocate()
154environment.files = allocate()
155environment.sortedflags = nil
156
157
158
159function environment.initializearguments(arg)
160 local arguments = { }
161 local files = { }
162 environment.arguments = arguments
163 environment.files = files
164 environment.sortedflags = nil
165 for index=1,#arg do
166 local argument = arg[index]
167 if index > 0 then
168 local flag, value = match(argument,"^%-+(.-)=(.-)$")
169 if flag then
170 flag = gsub(flag,"^c:","")
171 arguments[flag] = unquoted(value or "")
172 else
173 flag = match(argument,"^%-+(.+)")
174 if flag then
175 flag = gsub(flag,"^c:","")
176 arguments[flag] = true
177 else
178 files[#files+1] = argument
179 end
180 end
181 end
182 end
183 if not environment.ownname then
184 if os.selfpath and os.selfname then
185 environment.ownname = file.addsuffix(file.join(os.selfpath,os.selfname),"lua")
186 end
187 end
188 environment.ownname = file.reslash(environment.ownname or arg[0] or 'unknown.lua')
189end
190
191function environment.setargument(name,value)
192 environment.arguments[name] = value
193end
194
195
196
197
198
199
200function environment.getargument(name,partial)
201 local arguments = environment.arguments
202 local sortedflags = environment.sortedflags
203 if arguments[name] then
204 return arguments[name]
205 elseif partial then
206 if not sortedflags then
207 sortedflags = allocate(table.sortedkeys(arguments))
208 for k=1,#sortedflags do
209 sortedflags[k] = "^" .. sortedflags[k]
210 end
211 environment.sortedflags = sortedflags
212 end
213
214 for k=1,#sortedflags do
215 local v = sortedflags[k]
216 if find(name,v) then
217 return arguments[sub(v,2,#v)]
218 end
219 end
220 end
221 return nil
222end
223
224environment.argument = environment.getargument
225
226function environment.splitarguments(separator)
227 local done, before, after = false, { }, { }
228 local originalarguments = environment.originalarguments
229 for k=1,#originalarguments do
230 local v = originalarguments[k]
231 if not done and v == separator then
232 done = true
233 elseif done then
234 after[#after+1] = v
235 else
236 before[#before+1] = v
237 end
238 end
239 return before, after
240end
241
242function environment.reconstructcommandline(arg,noquote)
243 local resolveprefix = resolvers.resolve
244 arg = arg or environment.originalarguments
245 if noquote and #arg == 1 then
246 return unquoted(resolveprefix and resolveprefix(arg[1]) or arg[1])
247 elseif #arg > 0 then
248 local result = { }
249 for i=1,#arg do
250 result[i] = optionalquoted(resolveprefix and resolveprefix(arg[i]) or resolveprefix)
251 end
252 return concat(result," ")
253 else
254 return ""
255 end
256end
257
258
259
260function environment.relativepath(path,root)
261 if not path then
262 path = ""
263 end
264 if not file.is_rootbased_path(path) then
265 if not root then
266 root = file.pathpart(environment.ownscript or environment.ownname or ".")
267 end
268 if root == "" then
269 root = "."
270 end
271 path = root .. "/" .. path
272 end
273 return file.collapsepath(path,true)
274end
275
276
277
278
279
280
281
282
283
284
285
286
287if arg then
288
289
290
291 local newarg, instring = { }, false
292
293 for index=1,#arg do
294 local argument = arg[index]
295 if find(argument,"^\"") then
296 if find(argument,"\"$") then
297 newarg[#newarg+1] = gsub(argument,"^\"(.-)\"$","%1")
298 instring = false
299 else
300 newarg[#newarg+1] = gsub(argument,"^\"","")
301 instring = true
302 end
303 elseif find(argument,"\"$") then
304 if instring then
305 newarg[#newarg] = newarg[#newarg] .. " " .. gsub(argument,"\"$","")
306 instring = false
307 else
308 newarg[#newarg+1] = argument
309 end
310 elseif instring then
311 newarg[#newarg] = newarg[#newarg] .. " " .. argument
312 else
313 newarg[#newarg+1] = argument
314 end
315 end
316 for i=1,-5,-1 do
317 newarg[i] = arg[i]
318 end
319
320 environment.initializearguments(newarg)
321
322 environment.originalarguments = mark(newarg)
323 environment.rawarguments = mark(arg)
324
325 arg = { }
326
327end
328
329function environment.globfiles(files)
330 if not files then
331 files = environment.files
332 end
333 if files then
334 local globbed = { }
335 for i=1,#files do
336 local f = files[i]
337 if find(f,"%*") then
338 local g = globfiles(f)
339 if g then
340 for i=1,#g do
341 globbed[#globbed+1] = g[i]
342 end
343 end
344 else
345 globbed[#globbed+1] = f
346 end
347 end
348 return globbed
349 end
350end
351 |