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
|
if not modules then modules = { } end modules ['luat-cod'] = {
version = 1.001,
comment = "companion to luat-cod.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local match, gsub, find = string.match, string.gsub, string.find
-- some basic housekeeping
texconfig.kpse_init = false
texconfig.shell_escape = 't'
texconfig.max_print_line = 100000
texconfig.max_in_open = 127
-- registering bytecode chunks
lua.bytecode = lua.bytecode or { } -- built in anyway
lua.bytedata = lua.bytedata or { }
lua.bytedone = lua.bytedone or { }
local bytecode, bytedata, bytedone = lua.bytecode, lua.bytedata, lua.bytedone
lua.firstbytecode = 501
lua.lastbytecode = lua.lastbytecode or (lua.firstbytecode - 1) -- as we load ourselves again ... maybe return earlier
function lua.registeredcodes()
return lua.lastbytecode - lua.firstbytecode + 1
end
function lua.registercode(filename,version)
local barename = gsub(filename,"%.[%a%d]+$","")
if barename == filename then filename = filename .. ".lua" end
local basename = match(barename,"^.+[/\\](.-)$") or barename
if not bytedone[barename] then
local code = environment.luafilechunk(filename)
if code then
assert(code)()
bytedone[barename] = true
if environment.initex then
local n = lua.lastbytecode + 1
bytedata[n] = { barename, version }
bytecode[n] = code
lua.lastbytecode = n
end
end
end
end
local finalizers = { }
function lua.registerfinalizer(f,comment)
if type(f) == "function"then
finalizers[#finalizers+1] = { action = f, comment = comment }
end
end
function lua.finalize(logger)
for i=1,#finalizers do
local finalizer = finalizers[i]
finalizer.action()
if logger then
logger("finalizing lua", "action: %s",finalizer.comment)
end
end
end
-- A first start with environments. This will be overloaded later.
local sourcefile = arg and arg[1] or ""
local sourcepath = find(sourcefile,"/") and gsub(sourcefile,"/[^/]+$","") or ""
local targetpath = "."
environment = environment or { }
-- delayed (via metatable):
--
-- environment.jobname = tex.jobname
-- environment.version = tostring(tex.toks.contextversiontoks)
environment.initex = tex.formatname == ""
if not environment.luafilechunk then
function environment.luafilechunk(filename)
if sourcepath ~= "" then
filename = sourcepath .. "/" .. filename
end
local data = loadfile(filename)
texio.write("<",data and "+ " or "- ",filename,">")
return data
end
end
-- We need a few premature callbacks in the format generator. We
-- also do this when the format is loaded as otherwise we get
-- a kpse error when disabled. Thi sis en angine issue that will
-- be sorted out in due time.
local function source_file(name)
local fullname = sourcepath .. "/" .. name
if lfs.isfile(fullname) then
return fullname
end
fullname = fullname .. ".tex"
if lfs.isfile(fullname) then
return fullname
end
if lfs.isfile(name) then
return name
end
name = name .. ".tex"
if lfs.isfile(name) then
return name
end
return nil
end
local function target_file(name)
return targetpath .. "/" .. name
end
local function find_read_file (id,name)
return source_file(name)
end
local function find_write_file(id,name)
return target_file(name)
end
local function open_read_file(name)
local f = io.open(name,'rb')
return {
reader = function()
return f:read("*line")
end
}
end
callback.register('find_read_file' , find_read_file )
callback.register('open_read_file' , open_read_file)
callback.register('find_write_file', find_write_file)
|