luat-cnf.lua /size: 7324 b    last modification: 2024-01-16 09:02
1if not modules then modules = { } end modules ['luat-cnf'] = {
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 type, next, tostring, tonumber =  type, next, tostring, tonumber
10local format, concat, find, lower, gsub = string.format, table.concat, string.find, string.lower, string.gsub
11
12local report = logs.reporter("system")
13
14local allocate = utilities.storage.allocate
15
16texconfig.kpse_init    = false
17texconfig.shell_escape = 't'
18
19luatex       = luatex or { }
20local luatex = luatex
21
22texconfig.error_line      =      250
23texconfig.expand_depth    =    10000
24texconfig.half_error_line =      125
25texconfig.max_print_line  =   100000
26texconfig.max_strings     =   500000
27texconfig.hash_extra      =   250000
28texconfig.function_size   =    32768
29texconfig.properties_size =    10000
30texconfig.max_in_open     =     1000
31texconfig.nest_size       =     1000
32texconfig.param_size      =    25000
33texconfig.save_size       =   100000
34texconfig.stack_size      =    10000
35texconfig.buf_size        = 10000000
36texconfig.fix_mem_init    =  1000000
37
38local variablenames = {
39    error_line      = false,
40    half_error_line = false,
41    max_print_line  = false,
42    max_in_open     = false,
43    expand_depth    = true,
44    hash_extra      = true,
45    nest_size       = true,
46    max_strings     = true,
47    param_size      = true,
48    save_size       = true,
49    stack_size      = true,
50    function_size   = true,
51    properties_size = true,
52    fix_mem_init    = true,
53}
54
55local stub = [[
56
57-- checking
58
59storage = storage or { }
60luatex  = luatex  or { }
61
62-- we provide our own file handling
63
64texconfig.kpse_init    = false
65texconfig.shell_escape = 't'
66---------.start_time   = tonumber(os.getenv("SOURCE_DATE_EPOCH")) -- not used in context
67
68-- as soon as possible
69
70luatex.starttime = os.gettimeofday()
71
72-- this will happen after the format is loaded
73
74function texconfig.init()
75
76    -- development
77
78    local builtin, globals = { }, { }
79
80    libraries = { -- we set it here as we want libraries also 'indexed'
81        basiclua = {
82            -- always
83            "string", "table", "coroutine", "debug", "file", "io", "lpeg", "math", "os", "package",
84            -- bonus
85            "bit32", "utf8",
86        },
87        basictex = {
88            -- always
89            "callback", "font", "lua", "node", "status", "tex", "texconfig", "texio", "token",
90            "img", "pdf", "lang",
91        },
92        extralua = {
93            "unicode", "utf", "gzip",  "zip", "zlib",
94            "lz4", "lzo",
95            "lfs","socket", "mime", "md5", "sha2", "fio", "sio",
96        },
97        extratex = {
98            "kpse",
99            "pdfe", "mplib",
100        },
101        obsolete = {
102            "epdf",
103            "fontloader", -- can be filled by luat-log
104            "kpse",
105        },
106        functions = {
107            "assert", "pcall", "xpcall", "error", "collectgarbage",
108            "dofile", "load","loadfile", "require", "module",
109            "getmetatable", "setmetatable",
110            "ipairs", "pairs", "rawequal", "rawget", "rawset", "next",
111            "tonumber", "tostring",
112            "type", "unpack", "select", "print",
113        },
114        builtin = builtin, -- to be filled
115        globals = globals, -- to be filled
116    }
117
118    for k, v in next, _G do
119        globals[k] = tostring(v)
120    end
121
122    local function collect(t,fnc)
123        local lib = { }
124        for k, v in next, t do
125            if fnc then
126                lib[v] = _G[v]
127            else
128                local keys = { }
129                local gv = _G[v]
130                local tv = type(gv)
131                if tv == "table" then
132                    for k, v in next, gv do
133                        keys[k] = tostring(v) -- true -- by tostring we cannot call overloads functions (security)
134                    end
135                end
136                lib[v] = keys
137                builtin[v] = keys
138            end
139        end
140        return lib
141    end
142
143    libraries.basiclua  = collect(libraries.basiclua)
144    libraries.basictex  = collect(libraries.basictex)
145    libraries.extralua  = collect(libraries.extralua)
146    libraries.extratex  = collect(libraries.extratex)
147    libraries.functions = collect(libraries.functions,true)
148    libraries.obsolete  = collect(libraries.obsolete)
149
150    -- shortcut and helper
151
152    local setbytecode  = lua.setbytecode
153    local getbytecode  = lua.getbytecode
154    local callbytecode = lua.callbytecode or function(i)
155        local b = getbytecode(i)
156        if type(b) == "function" then
157            b()
158            return true
159        else
160            return false
161        end
162    end
163
164    local function init(start)
165        local i = start
166        local t = os.clock()
167        while true do
168         -- local b = callbytecode(i)
169            local e, b = pcall(callbytecode,i)
170            if not e then
171                print(string.format("fatal error : unable to load bytecode register %%i, maybe wipe the cache first\n",i))
172                os.exit()
173            end
174            if b then
175                setbytecode(i,nil) ;
176                i = i + 1
177            else
178                break
179            end
180        end
181        return i - start, os.clock() - t
182    end
183
184    -- the stored tables and modules
185
186    storage.noftables , storage.toftables  = init(0)
187    storage.nofmodules, storage.tofmodules = init(%s)
188
189    if modules then
190        local loaded = package.loaded
191        for module, _ in next, modules do
192            loaded[module] = true
193        end
194    end
195
196    texconfig.init = function() end
197
198end
199
200CONTEXTLMTXMODE = 0
201
202-- we provide a qualified path
203
204callback.register('find_format_file',function(name)
205    texconfig.formatname = name
206    return name
207end)
208
209-- done, from now on input and callbacks are internal
210]]
211
212local function makestub()
213    name = name or (environment.jobname .. ".lui")
214    report("creating stub file %a using directives:",name)
215    report()
216    firsttable = firsttable or lua.firstbytecode
217    local t = {
218        "-- this file is generated, don't change it\n",
219        "-- configuration (can be overloaded later)\n"
220    }
221    for v, permitted in table.sortedhash(variablenames) do
222        local d = "luatex." .. gsub(lower(v),"[^%a]","")
223        local dv = directives.value(d)
224        local tv = texconfig[v]
225        if dv then
226            if not tv then
227                report("  %s = %s (%s)",d,dv,"configured")
228                tv = dv
229            elseif not permitted then
230                report("  %s = %s (%s)",d,tv,"frozen")
231            elseif tonumber(dv) >= tonumber(tv) then
232                report("  %s = %s (%s)",d,dv,"overloaded")
233                tv = dv
234            else
235                report("  %s = %s (%s)",d,tv,"preset kept")
236            end
237        elseif tv then
238            report("  %s = %s (%s)",d,tv,permitted and "preset" or "frozen")
239        else
240            report("  %s = <unset>",d)
241        end
242        if tv then
243            t[#t+1] = format("texconfig.%s=%s",v,tv)
244        end
245    end
246    t[#t+1] = ""
247    t[#t+1] = format(stub,firsttable)
248    io.savedata(name,concat(t,"\n"))
249    logs.newline()
250end
251
252lua.registerinitexfinalizer(makestub,"create stub file")
253