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
14
15environment = environment or { }
16local environment = environment
17
18
19
20local setlocale = os.setlocale
21
22setlocale(nil,nil)
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
56local report = logs.reporter("system")
57
58function os.setlocale(a,b)
59 if a or b then
60 if report then
61 report()
62 report("You're messing with os.locale in a supposedly locale neutral enviroment. From")
63 report("now on are on your own and without support. Crashes or unexpected side effects")
64 report("can happen but don't bother the luatex and context developer team with it.")
65 report()
66 report = nil
67 end
68 setlocale(a,b)
69 end
70end
71
72
73
74local validengines = allocate {
75 ["luatex"] = true,
76 ["luajittex"] = true,
77}
78
79local basicengines = allocate {
80 ["luatex"] = "luatex",
81 ["texlua"] = "luatex",
82 ["texluac"] = "luatex",
83 ["luajittex"] = "luajittex",
84 ["texluajit"] = "luajittex",
85}
86
87local luaengines = allocate {
88 ["lua"] = true,
89 ["luajit"] = true,
90}
91
92environment.validengines = validengines
93environment.basicengines = basicengines
94
95
96
97
98
99
100
101if not arg then
102 environment.used_as_library = true
103
104elseif luaengines[file.removesuffix(arg[-1])] then
105
106
107
108
109
110
111
112
113elseif validengines[file.removesuffix(arg[0])] then
114 if arg[1] == "--luaonly" then
115 arg[-1] = arg[0]
116 arg[ 0] = arg[2]
117 for k=3,#arg do
118 arg[k-2] = arg[k]
119 end
120 remove(arg)
121 remove(arg)
122 else
123
124 end
125
126
127
128
129
130
131
132
133 local originalzero = file.basename(arg[0])
134 local specialmapping = { luatools == "base" }
135
136 if originalzero ~= "mtxrun" and originalzero ~= "mtxrun.lua" then
137 arg[0] = specialmapping[originalzero] or originalzero
138 insert(arg,0,"--script")
139 insert(arg,0,"mtxrun")
140 end
141
142end
143
144
145
146environment.arguments = allocate()
147environment.files = allocate()
148environment.sortedflags = nil
149
150
151
152function environment.initializearguments(arg)
153 local arguments = { }
154 local files = { }
155 environment.arguments = arguments
156 environment.files = files
157 environment.sortedflags = nil
158 for index=1,#arg do
159 local argument = arg[index]
160 if index > 0 then
161 local flag, value = match(argument,"^%-+(.-)=(.-)$")
162 if flag then
163 flag = gsub(flag,"^c:","")
164 arguments[flag] = unquoted(value or "")
165 else
166 flag = match(argument,"^%-+(.+)")
167 if flag then
168 flag = gsub(flag,"^c:","")
169 arguments[flag] = true
170 else
171 files[#files+1] = argument
172 end
173 end
174 end
175 end
176 if not environment.ownname then
177 if os.selfpath and os.selfname then
178 environment.ownname = file.addsuffix(file.join(os.selfpath,os.selfname),"lua")
179 end
180 end
181 environment.ownname = file.reslash(environment.ownname or arg[0] or 'unknown.lua')
182end
183
184function environment.setargument(name,value)
185 environment.arguments[name] = value
186end
187
188
189
190
191
192
193function environment.getargument(name,partial)
194 local arguments = environment.arguments
195 local sortedflags = environment.sortedflags
196 if arguments[name] then
197 return arguments[name]
198 elseif partial then
199 if not sortedflags then
200 sortedflags = allocate(table.sortedkeys(arguments))
201 for k=1,#sortedflags do
202 sortedflags[k] = "^" .. sortedflags[k]
203 end
204 environment.sortedflags = sortedflags
205 end
206
207 for k=1,#sortedflags do
208 local v = sortedflags[k]
209 if find(name,v) then
210 return arguments[sub(v,2,#v)]
211 end
212 end
213 end
214 return nil
215end
216
217environment.argument = environment.getargument
218
219function environment.splitarguments(separator)
220 local done, before, after = false, { }, { }
221 local originalarguments = environment.originalarguments
222 for k=1,#originalarguments do
223 local v = originalarguments[k]
224 if not done and v == separator then
225 done = true
226 elseif done then
227 after[#after+1] = v
228 else
229 before[#before+1] = v
230 end
231 end
232 return before, after
233end
234
235function environment.reconstructcommandline(arg,noquote)
236 local resolveprefix = resolvers.resolve
237 arg = arg or environment.originalarguments
238 if noquote and #arg == 1 then
239 return unquoted(resolveprefix and resolveprefix(arg[1]) or arg[1])
240 elseif #arg > 0 then
241 local result = { }
242 for i=1,#arg do
243 result[i] = optionalquoted(resolveprefix and resolveprefix(arg[i]) or resolveprefix)
244 end
245 return concat(result," ")
246 else
247 return ""
248 end
249end
250
251
252
253function environment.relativepath(path,root)
254 if not path then
255 path = ""
256 end
257 if not file.is_rootbased_path(path) then
258 if not root then
259 root = file.pathpart(environment.ownscript or environment.ownname or ".")
260 end
261 if root == "" then
262 root = "."
263 end
264 path = root .. "/" .. path
265 end
266 return file.collapsepath(path,true)
267end
268
269
270
271
272
273
274
275
276
277
278
279
280if arg then
281
282
283
284 local newarg, instring = { }, false
285
286 for index=1,#arg do
287 local argument = arg[index]
288 if find(argument,"^\"") then
289 if find(argument,"\"$") then
290 newarg[#newarg+1] = gsub(argument,"^\"(.-)\"$","%1")
291 instring = false
292 else
293 newarg[#newarg+1] = gsub(argument,"^\"","")
294 instring = true
295 end
296 elseif find(argument,"\"$") then
297 if instring then
298 newarg[#newarg] = newarg[#newarg] .. " " .. gsub(argument,"\"$","")
299 instring = false
300 else
301 newarg[#newarg+1] = argument
302 end
303 elseif instring then
304 newarg[#newarg] = newarg[#newarg] .. " " .. argument
305 else
306 newarg[#newarg+1] = argument
307 end
308 end
309 for i=1,-5,-1 do
310 newarg[i] = arg[i]
311 end
312
313 environment.initializearguments(newarg)
314
315 environment.originalarguments = mark(newarg)
316 environment.rawarguments = mark(arg)
317
318 arg = { }
319
320end
321 |