summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/luat-exe.lua
blob: ca3b751623b9470a3645e55d3201471b038d80e5 (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
if not modules then modules = { } end modules ['luat-exe'] = {
    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 match, find = string.match, string.find
local concat = table.concat

if not executer then executer = { } end

executer.permitted = { }
executer.execute   = os.execute

function executer.register(...)
    local ep = executer.permitted
    local t = { ... }
    for k=1,#t do
        local v = t[k]
        ep[#ep+1] = (v == "*" and ".*") or v
    end
end

function executer.finalize() -- todo: os.exec, todo: report ipv print
    local execute = os.execute
    function executer.execute(...)
        local t, name, arguments = {...}, "", ""
        if #t == 1 then
            if type(t[1]) == 'table' then
                name, arguments = t[1], concat(t," ",2,#t)
            else
                name, arguments = match(t[1],"^(.-)%s+(.+)$")
                if not (name and arguments) then
                    name, arguments = t[1], ""
                end
            end
        else
            name, arguments = t[1], concat(t," ",2,#t)
        end
        local permitted = executer.permitted
        for k=1,#permitted do
            local v = permitted[k]
            if find(name,v) then
                execute(name .. " " .. arguments)
            --  print("executed: " .. name .. " " .. arguments)
            else
                print("not permitted: " .. name .. " " .. arguments)
            end
        end
    end
    function executer.finalize()
        print("executer is already finalized")
    end
    function executer.register(name)
        print("executer is already finalized")
    end
    os.execute = executer.execute
end

--~ executer.register('.*')
--~ executer.register('*')
--~ executer.register('dir','ls')
--~ executer.register('dir')

--~ executer.finalize()
--~ executer.execute('dir',"*.tex")
--~ executer.execute("dir *.tex")
--~ executer.execute("ls *.tex")
--~ os.execute('ls')