luat-exe.lua /size: 3839 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['luat-exe'] = {
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
9if not sandbox then require("l-sandbox") require("util-sbx") end -- for testing
10
11-- Ok, as usual, after finishing some code, I rewarded myself with searching youtube for
12-- new music ... this time I ran into the swedisch group 'wintergatan' (search for: marble
13-- machine) ... mechanical computers are so much more fun than the ones needed for running
14-- the code below. Nice videos (and shows) too ...
15
16local type = type
17
18local executers        = resolvers.executers or { }
19resolvers.executers    = executers
20
21local disablerunners   = sandbox.disablerunners
22local disablelibraries = sandbox.disablelibraries
23local registerbinary   = sandbox.registerbinary
24local registerlibrary  = sandbox.registerlibrary
25local registerroot     = sandbox.registerroot
26
27local lpegmatch        = lpeg.match
28
29local sc_splitter      = lpeg.tsplitat(";")
30local cm_splitter      = lpeg.tsplitat(",")
31
32local execution_mode  directives.register("system.executionmode", function(v) execution_mode = v end)
33local execution_list  directives.register("system.executionlist", function(v) execution_list = v end)
34local root_list       directives.register("system.rootlist",      function(v) root_list      = v end)
35local library_mode    directives.register("system.librarymode",   function(v) library_mode   = v end)
36local library_list    directives.register("system.librarylist",   function(v) library_list   = v end)
37
38sandbox.initializer {
39    category = "binaries",
40    action   = function()
41        if execution_mode == "none" then
42            -- will be done later
43        elseif execution_mode == "list" then
44            if type(execution_list) == "string" then
45                execution_list = lpegmatch(cm_splitter,execution_list)
46            end
47            if type(execution_list) == "table" then
48                for i=1,#execution_list do
49                    registerbinary(execution_list[i])
50                end
51            end
52        else
53            registerbinary(true) -- all
54        end
55    end
56}
57
58sandbox.finalizer {
59    category = "binaries",
60    action   = function()
61        if execution_mode == "none" then
62            disablerunners()
63        end
64    end
65}
66
67sandbox.initializer {
68    category = "libraries",
69    action   = function()
70        if library_mode == "none" then
71            -- will be done later
72        elseif library_mode == "list" then
73            if type(library_list) == "string" then
74                library_list = lpegmatch(cm_splitter,library_list)
75            end
76            if type(library_list) == "table" then
77                for i=1,#library_list do
78                    registerlibrary(library_list[i])
79                end
80            end
81        else
82            registerlibrary(true) -- all
83        end
84    end
85}
86
87sandbox.finalizer {
88    category = "libraries",
89    action   = function()
90        if library_mode == "none" then
91            disablelibraries()
92        end
93    end
94}
95
96-- A bit of file system protection.
97
98sandbox.initializer{
99    category = "files",
100    action   = function ()
101        if type(root_list) == "string" then
102            root_list = lpegmatch(sc_splitter,root_list)
103        end
104        if type(root_list) == "table" then
105            for i=1,#root_list do
106                registerroot(root_list[i])
107            end
108        end
109    end
110}
111
112-- Let's prevent abuse of these libraries (built-in support still works).
113
114sandbox.finalizer {
115    category = "functions",
116    action   = function()
117        mplib      = nil
118        epdf       = nil
119        zip        = nil
120        fontloader = nil
121    end
122}
123