texlua.lua /size: 4023 b    last modification: 2024-01-16 10:21
1-- version   = 1.000,
2-- comment   = "companion to luametatex",
3-- author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
4-- copyright = "PRAGMA ADE / ConTeXt Development Team",
5-- license   = "see context related readme files"
6
7-- If you copy or link 'luametatex' to 'texlua' and put this script in the same path
8-- we have a more pure Lua runner (as with luatex and texlua).
9
10-- todo: error trace
11-- todo: protect these locals
12
13local texlua_load     = load
14local texlua_loadfile = loadfile
15local texlua_type     = type
16local texlua_xpcall   = xpcall
17local texlua_find     = string.find
18local texlua_dump     = string.dump
19local texlua_open     = io.open
20local texlua_print    = print
21local texlua_show     = luac.print
22
23function texlua_inspect(v)
24    if texlua_type(v) == "function" then
25        local ok, str = texlua_xpcall(texlua_dump,function() end,v)
26        if ok then
27            v = str
28        end
29    end
30    if type(v) == "string" then
31        texlua_show(v,true)
32    else
33        texlua_print(v)
34    end
35end
36
37local function main_execute(str,loader)
38    if str and str ~= "" then
39        local str = loader(str)
40        if texlua_type(str) == "function" then
41            str()
42        end
43    end
44end
45
46local function main_compile(str,loader,out,strip)
47    if str and str ~= "" then
48        local str = loader(str)
49        if texlua_type(str) == "function" then
50            str = texlua_dump(str,strip)
51            if type(out) == "string" and out ~= "" then
52                local f = texlua_open(out,"wb")
53                if f then
54                    f:write(str)
55                    f:close()
56                end
57            elseif out == true then
58                texlua_inspect(str)
59            else
60                texlua_print(str)
61            end
62        end
63    end
64end
65
66local function main_help()
67    texlua_print("LuaMetaTeX in Lua mode:")
68    texlua_print("")
69    texlua_print("-o  'filename'  output filename")
70    texlua_print("-e  'string'    execute loaded string")
71    texlua_print("-f  'filename'  execute loaded file")
72    texlua_print("-d  'string'    dump bytecode of loaded string")
73    texlua_print("-c  'filename'  dump bytecode of loaded file")
74    texlua_print("-i  'string'    list bytecode of loaded string")
75    texlua_print("-l  'filename'  list bytecode of loaded file")
76    texlua_print("-s              strip byte code")
77    texlua_print("    'filename'  execute loaded file")
78end
79
80local function main()
81    local i = 1
82    local o = ""
83    local s = false
84    while true do
85        local option = arg[i] or ""
86        if option == "" then
87            if i == 1 then
88                main_help()
89            end
90            break
91        elseif option == "-e" then
92            i = i + 1
93            main_execute(arg[i],texlua_load)
94            o = ""
95            s = false
96        elseif option == "-f" then
97            i = i + 1
98            main_execute(arg[i],texlua_loadfile)
99            o = ""
100            s = false
101        elseif option == "-d" then
102            i = i + 1
103            main_compile(arg[i],texlua_load,o,s)
104            o = ""
105            s = false
106        elseif option == "-c" then
107            i = i + 1
108            main_compile(arg[i],texlua_loadfile,o,s)
109            o = ""
110            s = false
111        elseif option == "-i" then
112            i = i + 1
113            main_compile(arg[i],texlua_load,true)
114            o = ""
115            s = false
116        elseif option == "-l" then
117            i = i + 1
118            main_compile(arg[i],texlua_loadfile,true)
119            o = ""
120            s = false
121        elseif option == "-s" then
122            s = true
123        elseif option == "-o" then
124            i = i + 1
125            o = arg[i] or ""
126            if texlua_find(o,"^%-") then
127                help()
128                break
129            end
130        elseif not texlua_find(option,"^%-") then
131            main_execute(option,texlua_loadfile)
132            break
133        else
134            main_help()
135            break
136        end
137        i = i + 1
138    end
139end
140
141main()
142