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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
if not modules then modules = { } end modules ['data-ini'] = {
version = 1.001,
comment = "companion to luat-lib.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files",
}
local next, type, getmetatable, rawset = next, type, getmetatable, rawset
local gsub, find, gmatch, char = string.gsub, string.find, string.gmatch, string.char
local filedirname, filebasename, filejoin = file.dirname, file.basename, file.join
local ostype, osname, osuname, ossetenv, osgetenv = os.type, os.name, os.uname, os.setenv, os.getenv
local sortedpairs = table.sortedpairs
local P, S, R, C, Cs, Cc, lpegmatch = lpeg.P, lpeg.S, lpeg.R, lpeg.C, lpeg.Cs, lpeg.Cc, lpeg.match
local trace_locating = false trackers.register("resolvers.locating", function(v) trace_locating = v end)
local trace_expansions = false trackers.register("resolvers.expansions", function(v) trace_expansions = v end)
local report_initialization = logs.reporter("resolvers","initialization")
-- The code here used to be part of a data-res but for convenience we now split it over multiple
-- files. As this file is now the starting point we introduce resolvers here. We also put some
-- helpers here that later can be reimplemented of extended.
resolvers = resolvers or { }
local resolvers = resolvers
-- We don't want the kpse library to kick in. Also, we want to be able to
-- execute programs. Control over execution is implemented later.
texconfig.kpse_init = false
texconfig.shell_escape = 't'
if not (environment and environment.default_texmfcnf) and kpse and kpse.default_texmfcnf then
local default_texmfcnf = kpse.default_texmfcnf()
-- looks more like context
default_texmfcnf = gsub(default_texmfcnf,"$SELFAUTOLOC","selfautoloc:")
default_texmfcnf = gsub(default_texmfcnf,"$SELFAUTODIR","selfautodir:")
default_texmfcnf = gsub(default_texmfcnf,"$SELFAUTOPARENT","selfautoparent:")
default_texmfcnf = gsub(default_texmfcnf,"$HOME","home:")
--
environment.default_texmfcnf = default_texmfcnf
end
kpse = { original = kpse }
setmetatable(kpse, {
__index = function(kp,name)
report_initialization("fatal error: kpse library is accessed (key: %s)",name)
os.exit()
end
} )
-- First we check a couple of environment variables. Some might be
-- set already but we need then later on. We start with the system
-- font path.
do
local osfontdir = osgetenv("OSFONTDIR")
if osfontdir and osfontdir ~= "" then
-- ok
elseif osname == "windows" then
ossetenv("OSFONTDIR","c:/windows/fonts//")
elseif osname == "macosx" then
ossetenv("OSFONTDIR","$HOME/Library/Fonts//;/Library/Fonts//;/System/Library/Fonts//")
end
end
-- Next comes the user's home path. We need this as later on we have
-- to replace ~ with its value.
if not environment.homedir then
local oldhome = osgetenv('HOME')
local homedir = osgetenv(ostype == "windows" and 'USERPROFILE' or 'HOME') or ''
if not homedir or homedir == "" then
homedir = char(127) -- we need a value, later we wil trigger on it
end
homedir = file.collapsepath(homedir)
ossetenv("HOME", homedir) -- can be used in unix cnf files
ossetenv("USERPROFILE",homedir) -- can be used in windows cnf files
environment.oldhome = oldhome
environment.homedir = homedir
end
-- The following code sets the name of the own binary and its
-- path. This is fallback code as we have os.selfdir now.
do
local args = environment.originalarguments or arg -- this needs a cleanup
if not environment.ownmain then
environment.ownmain = status and string.match(string.lower(status.banner),"this is ([%a]+)") or "luatex"
end
local ownbin = environment.ownbin or args[-2] or arg[-2] or args[-1] or arg[-1] or arg[0] or "luatex"
local ownpath = environment.ownpath or os.selfdir
ownbin = file.collapsepath(ownbin)
ownpath = file.collapsepath(ownpath)
if not ownpath or ownpath == "" or ownpath == "unset" then
ownpath = args[-1] or arg[-1]
ownpath = ownpath and filedirname(gsub(ownpath,"\\","/"))
if not ownpath or ownpath == "" then
ownpath = args[-0] or arg[-0]
ownpath = ownpath and filedirname(gsub(ownpath,"\\","/"))
end
local binary = ownbin
if not ownpath or ownpath == "" then
ownpath = ownpath and filedirname(binary)
end
if not ownpath or ownpath == "" then
if os.binsuffix ~= "" then
binary = file.replacesuffix(binary,os.binsuffix)
end
local path = osgetenv("PATH")
if path then
for p in gmatch(path,"[^"..io.pathseparator.."]+") do
local b = filejoin(p,binary)
if lfs.isfile(b) then
-- we assume that after changing to the path the currentdir function
-- resolves to the real location and use this side effect here; this
-- trick is needed because on the mac installations use symlinks in the
-- path instead of real locations
local olddir = lfs.currentdir()
if lfs.chdir(p) then
local pp = lfs.currentdir()
if trace_locating and p ~= pp then
report_initialization("following symlink %a to %a",p,pp)
end
ownpath = pp
lfs.chdir(olddir)
else
if trace_locating then
report_initialization("unable to check path %a",p)
end
ownpath = p
end
break
end
end
end
end
if not ownpath or ownpath == "" then
ownpath = "."
report_initialization("forcing fallback to ownpath %a",ownpath)
elseif trace_locating then
report_initialization("using ownpath %a",ownpath)
end
end
environment.ownbin = ownbin
environment.ownpath = ownpath
end
resolvers.ownpath = environment.ownpath
function resolvers.getownpath()
return environment.ownpath
end
-- The self variables permit us to use only a few (or even no)
-- environment variables.
do
local ownpath = environment.ownpath or dir.current()
if ownpath then
ossetenv('SELFAUTOLOC', file.collapsepath(ownpath))
ossetenv('SELFAUTODIR', file.collapsepath(ownpath .. "/.."))
ossetenv('SELFAUTOPARENT', file.collapsepath(ownpath .. "/../.."))
else
report_initialization("error: unable to locate ownpath")
os.exit()
end
end
-- The running os:
-- todo: check is context sits here os.platform is more trustworthy
-- that the bin check as mtx-update runs from another path
local texos = environment.texos or osgetenv("TEXOS")
local texmfos = environment.texmfos or osgetenv('SELFAUTODIR')
if not texos or texos == "" then
texos = file.basename(texmfos)
end
ossetenv('TEXMFOS', texmfos) -- full bin path
ossetenv('TEXOS', texos) -- partial bin parent
ossetenv('SELFAUTOSYSTEM',os.platform) -- bonus
environment.texos = texos
environment.texmfos = texmfos
-- The current root:
local texroot = environment.texroot or osgetenv("TEXROOT")
if not texroot or texroot == "" then
texroot = osgetenv('SELFAUTOPARENT')
ossetenv('TEXROOT',texroot)
end
environment.texroot = file.collapsepath(texroot)
-- a forward definition
-- Because we use resolvers.resolve a lot later on, we will implement the basics here and
-- add more later.
local prefixes = utilities.storage.allocate()
resolvers.prefixes = prefixes
local resolved = { }
local abstract = { }
local dynamic = { }
function resolvers.resetresolve(str)
resolved, abstract = { }, { }
end
function resolvers.allprefixes(separator)
local all = table.sortedkeys(prefixes)
if separator then
for i=1,#all do
all[i] = all[i] .. ":"
end
end
return all
end
local function _resolve_(method,target)
local action = prefixes[method]
if action then
return action(target)
else
return method .. ":" .. target
end
end
function resolvers.unresolve(str)
return abstract[str] or str
end
function resolvers.setdynamic(str)
dynamic[str] = true
end
-- home:xx;selfautoparent:xx;
local pattern = Cs((C(R("az")^2) * P(":") * C((1-S(" \"\';,"))^1) / _resolve_ + P(1))^0)
local prefix = C(R("az")^2) * P(":")
local target = C((1-S(" \"\';,"))^1)
local notarget = (#S(";,") + P(-1)) * Cc("")
local p_resolve = Cs(((prefix * (target + notarget)) / _resolve_ + P(1))^0)
local p_simple = prefix * P(-1)
local function resolve(str) -- use schemes, this one is then for the commandline only
if type(str) == "table" then
local res = { }
for i=1,#str do
res[i] = resolve(str[i])
end
return res
end
-- already resolved
local res = resolved[str]
if res then
return res
end
-- simple resolving of (dynamic) methods
local simple = lpegmatch(p_simple,str)
local action = prefixes[simple]
if action then
local res = action(res)
if not dynamic[simple] then
resolved[simple] = res
abstract[res] = simple
end
return res
end
-- more extensive resolving (multiple too)
res = lpegmatch(p_resolve,str)
resolved[str] = res
abstract[res] = str
return res
end
resolvers.resolve = resolve
if type(osuname) == "function" then
for k, v in next, osuname() do
if not prefixes[k] then
prefixes[k] = function() return v end
end
end
end
if ostype == "unix" then
-- We need to distringuish between a prefix and something else : so we
-- have a special repath variant for linux. Also, when a new prefix is
-- defined, we need to remake the matcher.
local pattern
local function makepattern(t,k,v)
if t then
rawset(t,k,v)
end
local colon = P(":")
for k, v in sortedpairs(prefixes) do
if p then
p = P(k) + p
else
p = P(k)
end
end
pattern = Cs((p * colon + colon/";" + P(1))^0)
end
makepattern()
table.setmetatablenewindex(prefixes,makepattern)
function resolvers.repath(str)
return lpegmatch(pattern,str)
end
else -- already the default:
function resolvers.repath(str)
return str
end
end
|