2019-07-17 21:14:34 +02:00
|
|
|
local format = os.selfname -- or os.selfcore, I couldn't find a difference yet
|
|
|
|
local ourname = arg[0] -- This might not be os.selfarg[0]
|
2020-07-10 21:57:43 +02:00
|
|
|
-- We want to drop arg[0] aka. ourname from the arguments to avoid recursion
|
2019-07-17 21:14:34 +02:00
|
|
|
if os.selfarg[0] == ourname then
|
|
|
|
ourname = nil
|
|
|
|
end
|
2020-07-17 03:59:44 +02:00
|
|
|
local run
|
2020-07-19 14:03:57 +02:00
|
|
|
|
|
|
|
local arg_pattern, late_arg_pattern do
|
|
|
|
local l = lpeg or require'lpeg'
|
|
|
|
-- We intepret two arguments directly
|
|
|
|
local early_args = 'run' * l.Cmt(l.P'=' + -1, function() run = true return true end)
|
|
|
|
+ 'progname=' * l.Cmt(0, function(s, off) format = s:sub(off) return true end)
|
|
|
|
-- LuaMetaTeX needs -- to introduce parameters,
|
|
|
|
-- but fmtutil uses just -. Let's rewrite this on the fly:
|
|
|
|
local maybe_option = ('-' * ('-' + l.Cc'-') * #(early_args^-1))^-1
|
2020-07-19 19:29:54 +02:00
|
|
|
local quote = l.Cc(os.type == 'windows' and '"' or "'")
|
2020-07-19 14:03:57 +02:00
|
|
|
local escape
|
2020-07-19 19:29:54 +02:00
|
|
|
if os.type == 'windows' then
|
2020-07-19 14:03:57 +02:00
|
|
|
-- Windows: " -> "^"" The ^ is for cmd escaping, the """ is for command line splitting escaping,
|
|
|
|
-- backslashes still have to be escaped, but only in front of " or \
|
2020-07-23 09:01:03 +02:00
|
|
|
-- Additionally, "%" always as to be escaped for some variable substitution
|
|
|
|
-- pass before everything else.
|
2020-07-19 14:03:57 +02:00
|
|
|
-- WARNING: This works with luametatex's argument splitting, but don't generally rely
|
|
|
|
-- on it for other Windows programs. There are two standard Windows ways which are incompatible...
|
2020-07-23 09:01:03 +02:00
|
|
|
escape = '\\' * #l.S'\\"' * l.Cc'\\' + '"' * l.Cc'^""' + '%' * l.Cc'%' + 1
|
2020-07-19 14:03:57 +02:00
|
|
|
else
|
|
|
|
-- POSIX: We escape with single quotes, so only single quotes need escaping
|
|
|
|
escape = "'" * l.Cc"\\''" + 1
|
|
|
|
end
|
|
|
|
arg_pattern = l.Cs(l.Cc' ' * maybe_option * quote * escape^0 * quote)
|
|
|
|
late_arg_pattern = l.Cs(l.Cc' ' * quote * escape^0 * quote)
|
|
|
|
end
|
|
|
|
|
2019-07-17 21:14:34 +02:00
|
|
|
for i, a in ipairs(os.selfarg) do
|
|
|
|
if a == ourname then -- Avoid recursion
|
|
|
|
table.remove(os.selfarg, i)
|
|
|
|
ourname = nil
|
|
|
|
a = os.selfarg[i]
|
|
|
|
end
|
2020-07-19 14:03:57 +02:00
|
|
|
if a == "--" then arg_pattern = late_arg_pattern end -- This convention is not respected by luametatex itself
|
|
|
|
os.selfarg[i] = arg_pattern:match(a)
|
2019-07-17 21:14:34 +02:00
|
|
|
end
|
|
|
|
os.setenv("engine", status.luatex_engine)
|
2020-07-19 14:03:57 +02:00
|
|
|
|
|
|
|
local kpse_call = io.popen(string.format("kpsewhich -progname%s -format lua -all -must-exist%s-init.lua", late_arg_pattern:match(format), late_arg_pattern:match(format)))
|
2020-07-11 19:44:46 +02:00
|
|
|
local file
|
|
|
|
repeat
|
|
|
|
file = kpse_call:read()
|
2020-07-23 09:01:03 +02:00
|
|
|
until not assert(file, "Unable to find initialization script. Aborting."):match('^%.')
|
|
|
|
|
2020-07-10 21:57:43 +02:00
|
|
|
if not kpse_call:close() then
|
|
|
|
error(file)
|
|
|
|
end
|
2020-07-17 03:59:44 +02:00
|
|
|
|
|
|
|
local geterrorcode
|
|
|
|
if os.type == 'windows' then
|
|
|
|
function geterrorcode(ec) return ec end
|
|
|
|
else
|
|
|
|
function geterrorcode(ec) return ec & 0xFF == 0 and ec >> 8 or 0xFF end
|
|
|
|
end
|
|
|
|
|
2021-03-20 19:00:02 +01:00
|
|
|
local firstargs = string.format("luametatex --permitloadlib%s%s", late_arg_pattern:match('--lua=' .. file), late_arg_pattern:match('--arg0=' .. os.selfarg[0]))
|
2020-07-19 14:03:57 +02:00
|
|
|
local args = table.concat(os.selfarg)
|
|
|
|
if run then -- The user wants to take care of everything
|
|
|
|
os.exit(geterrorcode(os.execute(firstargs .. args)))
|
2020-07-17 03:59:44 +02:00
|
|
|
else
|
|
|
|
for i = 1, 5 do
|
2020-07-19 14:03:57 +02:00
|
|
|
local status = geterrorcode(os.execute(string.format("%s --run=%i%s", firstargs, i, args)))
|
2020-07-17 03:59:44 +02:00
|
|
|
if status ~= 75 then
|
|
|
|
os.exit(status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
os.exit(75)
|
|
|
|
end
|