summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/cont-run.lua
blob: 2ffbd24dd7ad622479b29bb311534da7374e1b9b (plain)
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
if not modules then modules = { } end modules ['cont-run'] = {
    version   = 1.001,
    comment   = "companion to cont-yes.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- When a style is loaded there is a good change that we never enter
-- this code.

local report = logs.reporter("system")

local type, tostring = type, tostring

local report        = logs.reporter("sandbox","call")
local fastserialize = table.fastserialize
local quoted        = string.quoted
local possiblepath  = sandbox.possiblepath

local context       = context
local implement     = interfaces.implement

local qualified     = { }
local writeable     = { }
local readable      = { }
local blocked       = { }
local trace_files   = false
local trace_calls   = false
local nofcalls      = 0
local nofrejected   = 0
local logfilename   = "sandbox.log"

local function registerstats()
    statistics.register("sandboxing", function()
        if trace_files then
            return string.format("%i calls, %i rejected, logdata in '%s'",nofcalls,nofrejected,logfilename)
        else
            return string.format("%i calls, %i rejected",nofcalls,nofrejected)
        end
    end)
    registerstats = false
end

local function logsandbox(details)
    local comment   = details.comment
    local result    = details.result
    local arguments = details.arguments
    for i=1,#arguments do
        local argument = arguments[i]
        local t = type(argument)
        if t == "string" then
            arguments[i] = quoted(argument)
            if trace_files and possiblepath(argument) then
                local q = qualified[argument]
                if q then
                    local c = q[comment]
                    if c then
                        local r = c[result]
                        if r then
                            c[result] = r + 1
                        else
                            c[result] = r
                        end
                    else
                        q[comment] = {
                            [result] = 1
                        }
                    end
                else
                    qualified[argument] = {
                        [comment] = {
                            [result] = 1
                        }
                    }
                end
            end
        elseif t == "table" then
            arguments[i] = fastserialize(argument)
        else
            arguments[i] = tostring(argument)
        end
    end
    if trace_calls then
        report("%s(%,t) => %l",details.comment,arguments,result)
    end
    nofcalls = nofcalls + 1
    if not result then
        nofrejected = nofrejected + 1
    end
end

local ioopen = sandbox.original(io.open) -- dummy call

local function logsandboxfiles(name,what,asked,okay)
    -- we're only interested in permitted access
    if not okay then
        blocked  [asked] = blocked  [asked] or 0 + 1
    elseif what == "*" or what == "w" then
        writeable[asked] = writeable[asked] or 0 + 1
    else
        readable [asked] = readable [asked] or 0 + 1
    end
end

function sandbox.logcalls()
    if not trace_calls then
        trace_calls = true
        sandbox.setlogger(logsandbox)
        if registerstats then
            registerstats()
        end
    end
end

function sandbox.logfiles()
    if not trace_files then
        trace_files = true
        sandbox.setlogger(logsandbox)
        sandbox.setfilenamelogger(logsandboxfiles)
        luatex.registerstopactions(function()
            table.save(logfilename,{
                calls = {
                    nofcalls    = nofcalls,
                    nofrejected = nofrejected,
                    filenames   = qualified,
                },
                checkednames = {
                    readable  = readable,
                    writeable = writeable,
                    blocked   = blocked,
                },
            })
        end)
        if registerstats then
            registerstats()
        end
    end
end

trackers.register("sandbox.tracecalls",sandbox.logcalls)
trackers.register("sandbox.tracefiles",sandbox.logfiles)

local sandboxing = environment.arguments.sandbox
local debugging  = environment.arguments.debug

if sandboxing then

    report("enabling sandbox")

    sandbox.enable()

    if type(sandboxing) == "string" then
        sandboxing = utilities.parsers.settings_to_hash(sandboxing)
        if sandboxing.calls then
            sandbox.logcalls()
        end
        if sandboxing.files then
            sandbox.logfiles()
        end
    end

    -- Nicer would be if we could just disable write 18 and keep os.execute
    -- which in fact we can do by defining write18 as macro instead of
    -- primitive ... todo ... well, it has been done now.

    -- We block some potential escapes from protection.

    context [[
        \let\primitive      \relax
        \let\normalprimitive\relax
    ]]

    debug = {
        traceback = traceback,
    }

    package.loaded.debug = debug

elseif debugging then

    -- we keep debug

else

    debug = {
        traceback = traceback,
        getinfo   = getinfo,
        sethook   = sethook,
    }

    package.loaded.debug = debug

end

local preparejob  preparejob = function() -- tricky: we need a hook for this

    local arguments = environment.arguments

    if arguments.lmtx or CONTEXTLMTXMODE then
        report("enabling lmtx mode")
        context.enablelmtx()
        environment.lmtxmode = true
    end

    if arguments.nosynctex then
        luatex.synctex.setup {
            state  = interfaces.variables.never,
        }
    elseif arguments.synctex then
        luatex.synctex.setup {
            state  = interfaces.variables.start,
            method = interfaces.variables.max,
        }
    end

 -- -- todo: move from mtx-context to here:
 --
 -- local timing = arguments.timing
 -- if type(timing) == "string" then
 --     context.usemodule { timing }
 -- end
 -- local nodates = arguments.nodates
 -- if nodates then
 --     context.enabledirectives { "backend.date=" .. (type(nodates) == "string" and nodates or "no") }
 -- end
 -- local trailerid = arguments.trailerid
 -- if type(trailerid) == "string" then
 --     context.enabledirectives { "backend.trailerid=" .. trailerid }
 -- end
 -- local profile = arguments.profile
 -- if profile then
 --     context.enabledirectives { "system.profile=" .. tonumber(profile) or 0 }
 -- end

 -- -- already done in mtxrun / mtx-context, has to happen very early
 --
 -- if arguments.silent then
 --     directives.enable("logs.blocked",arguments.silent)
 -- end
 --
 -- -- already done in mtxrun / mtx-context, can as well happen here
 --
 -- if arguments.errors then
 --     directives.enable("logs.errors",arguments.errors)
 -- end

    preparejob = function() end

    job.prepare = preparejob

end

job.prepare = preparejob

local function processjob()

    environment.initializefilenames() -- todo: check if we really need to pre-prep the filename

    local arguments = environment.arguments
    local suffix    = environment.suffix
    local filename  = environment.filename -- hm, not inputfilename !

    preparejob()

    if not filename or filename == "" then
        -- skip
    elseif suffix == "xml" or arguments.forcexml then

        -- Maybe we should move the preamble parsing here as it
        -- can be part of (any) loaded (sub) file. The \starttext
        -- wrapping might go away.

        report("processing as xml: %s",filename)

        context.starttext()
            context.xmlprocess("main",filename,"")
        context.stoptext()

    elseif suffix == "cld" or arguments.forcecld then

        report("processing as cld: %s",filename)

        context.runfile(filename)

    elseif suffix == "lua" or arguments.forcelua then

        -- The wrapping might go away. Why is is it there in the
        -- first place.

        report("processing as lua: %s",filename)

        context.starttext()
            context.ctxlua(string.format('dofile("%s")',filename))
        context.stoptext()

    elseif suffix == "mp" or arguments.forcemp then

        report("processing as metapost: %s",filename)

        context.starttext()
            context.processMPfigurefile(filename)
        context.stoptext()

    -- elseif suffix == "prep" then
    --
    --     -- Why do we wrap here. Because it can be xml? Let's get rid
    --     -- of prepping in general.
    --
    --     context.starttext()
    --     context.input(filename)
    --     context.stoptext()

    elseif suffix == "mps" or arguments.forcemps then

        report("processing metapost output: %s",filename)

        context.starttext()
            context.startTEXpage()
                context.externalfigure { filename }
            context.stopTEXpage()
        context.stoptext()

    else

     -- \writestatus{system}{processing as tex}
        -- We have a regular tex file so no \starttext yet as we can
        -- load fonts.
     -- context.enabletrackers { "resolvers.*" }
        context.input(filename)
     -- context.disabletrackers { "resolvers.*" }

    end

    context.finishjob()

end

implement {
    name     = "processjob",
    onlyonce = true,
    actions  = processjob,
}