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, sortedflags = environment.arguments, environment.sortedflags
195 if arguments[name] then
196 return arguments[name]
197 elseif partial then
198 if not sortedflags then
199 sortedflags = allocate(table.sortedkeys(arguments))
200 for k=1,#sortedflags do
201 sortedflags[k] = "^" .. sortedflags[k]
202 end
203 environment.sortedflags = sortedflags
204 end
205
206 for k=1,#sortedflags do
207 local v = sortedflags[k]
208 if find(name,v) then
209 return arguments[sub(v,2,#v)]
210 end
211 end
212 end
213 return nil
214end
215
216environment.argument = environment.getargument
217
218function environment.splitarguments(separator)
219 local done, before, after = false, { }, { }
220 local originalarguments = environment.originalarguments
221 for k=1,#originalarguments do
222 local v = originalarguments[k]
223 if not done and v == separator then
224 done = true
225 elseif done then
226 after[#after+1] = v
227 else
228 before[#before+1] = v
229 end
230 end
231 return before, after
232end
233
234function environment.reconstructcommandline(arg,noquote)
235 local resolveprefix = resolvers.resolve
236 arg = arg or environment.originalarguments
237 if noquote and #arg == 1 then
238 return unquoted(resolveprefix and resolveprefix(arg[1]) or arg[1])
239 elseif #arg > 0 then
240 local result = { }
241 for i=1,#arg do
242 result[i] = optionalquoted(resolveprefix and resolveprefix(arg[i]) or resolveprefix)
243 end
244 return concat(result," ")
245 else
246 return ""
247 end
248end
249
250
251
252function environment.relativepath(path,root)
253 if not path then
254 path = ""
255 end
256 if not file.is_rootbased_path(path) then
257 if not root then
258 root = file.pathpart(environment.ownscript or environment.ownname or ".")
259 end
260 if root == "" then
261 root = "."
262 end
263 path = root .. "/" .. path
264 end
265 return file.collapsepath(path,true)
266end
267
268
269
270
271
272
273
274
275
276
277
278
279if arg then
280
281
282
283 local newarg, instring = { }, false
284
285 for index=1,#arg do
286 local argument = arg[index]
287 if find(argument,"^\"") then
288 if find(argument,"\"$") then
289 newarg[#newarg+1] = gsub(argument,"^\"(.-)\"$","%1")
290 instring = false
291 else
292 newarg[#newarg+1] = gsub(argument,"^\"","")
293 instring = true
294 end
295 elseif find(argument,"\"$") then
296 if instring then
297 newarg[#newarg] = newarg[#newarg] .. " " .. gsub(argument,"\"$","")
298 instring = false
299 else
300 newarg[#newarg+1] = argument
301 end
302 elseif instring then
303 newarg[#newarg] = newarg[#newarg] .. " " .. argument
304 else
305 newarg[#newarg+1] = argument
306 end
307 end
308 for i=1,-5,-1 do
309 newarg[i] = arg[i]
310 end
311
312 environment.initializearguments(newarg)
313
314 environment.originalarguments = mark(newarg)
315 environment.rawarguments = mark(arg)
316
317 arg = { }
318
319end
320 |