summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/luat-env.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/luat-env.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/luat-env.lua147
1 files changed, 95 insertions, 52 deletions
diff --git a/Master/texmf-dist/tex/context/base/luat-env.lua b/Master/texmf-dist/tex/context/base/luat-env.lua
index 0e21fca31e9..4f1b661c218 100644
--- a/Master/texmf-dist/tex/context/base/luat-env.lua
+++ b/Master/texmf-dist/tex/context/base/luat-env.lua
@@ -14,8 +14,13 @@ if not modules then modules = { } end modules ['luat-env'] = {
local trace_locating = false trackers.register("resolvers.locating", function(v) trace_locating = v end)
+local report_lua = logs.reporter("resolvers","lua")
+
+local allocate, mark = utilities.storage.allocate, utilities.storage.mark
+
local format, sub, match, gsub, find = string.format, string.sub, string.match, string.gsub, string.find
-local unquote, quote = string.unquote, string.quote
+local unquoted, quoted = string.unquoted, string.quoted
+local concat = table.concat
-- precautions
@@ -28,25 +33,53 @@ end
-- dirty tricks
if arg and (arg[0] == 'luatex' or arg[0] == 'luatex.exe') and arg[1] == "--luaonly" then
- arg[-1]=arg[0] arg[0]=arg[2] for k=3,#arg do arg[k-2]=arg[k] end arg[#arg]=nil arg[#arg]=nil
-end
-
-if profiler and os.env["MTX_PROFILE_RUN"] == "YES" then
- profiler.start("luatex-profile.log")
+ arg[-1] = arg[0]
+ arg[ 0] = arg[2]
+ for k=3,#arg do
+ arg[k-2] = arg[k]
+ end
+ arg[#arg] = nil -- last
+ arg[#arg] = nil -- pre-last
end
-- environment
environment = environment or { }
-environment.arguments = { }
-environment.files = { }
+local environment = environment
+
+environment.arguments = allocate()
+environment.files = allocate()
environment.sortedflags = nil
-if not environment.jobname or environment.jobname == "" then if tex then environment.jobname = tex.jobname end end
-if not environment.version or environment.version == "" then environment.version = "unknown" end
-if not environment.jobname then environment.jobname = "unknown" end
+local mt = {
+ __index = function(_,k)
+ if k == "version" then
+ local version = tex.toks and tex.toks.contextversiontoks
+ if version and version ~= "" then
+ rawset(environment,"version",version)
+ return version
+ else
+ return "unknown"
+ end
+ elseif k == "jobname" or k == "formatname" then
+ local name = tex and tex[k]
+ if name or name== "" then
+ rawset(environment,k,name)
+ return name
+ else
+ return "unknown"
+ end
+ elseif k == "outputfilename" then
+ local name = environment.jobname
+ rawset(environment,k,name)
+ return name
+ end
+ end
+}
+
+setmetatable(environment,mt)
-function environment.initialize_arguments(arg)
+function environment.initializearguments(arg)
local arguments, files = { }, { }
environment.arguments, environment.files, environment.sortedflags = arguments, files, nil
for index=1,#arg do
@@ -54,7 +87,7 @@ function environment.initialize_arguments(arg)
if index > 0 then
local flag, value = match(argument,"^%-+(.-)=(.-)$")
if flag then
- arguments[flag] = unquote(value or "")
+ arguments[flag] = unquoted(value or "")
else
flag = match(argument,"^%-+(.+)")
if flag then
@@ -83,7 +116,7 @@ function environment.argument(name,partial)
return arguments[name]
elseif partial then
if not sortedflags then
- sortedflags = table.sortedkeys(arguments)
+ sortedflags = allocate(table.sortedkeys(arguments))
for k=1,#sortedflags do
sortedflags[k] = "^" .. sortedflags[k]
end
@@ -100,13 +133,11 @@ function environment.argument(name,partial)
return nil
end
-environment.argument("x",true)
-
-function environment.split_arguments(separator) -- rather special, cut-off before separator
+function environment.splitarguments(separator) -- rather special, cut-off before separator
local done, before, after = false, { }, { }
- local original_arguments = environment.original_arguments
- for k=1,#original_arguments do
- local v = original_arguments[k]
+ local originalarguments = environment.originalarguments
+ for k=1,#originalarguments do
+ local v = originalarguments[k]
if not done and v == separator then
done = true
elseif done then
@@ -118,32 +149,51 @@ function environment.split_arguments(separator) -- rather special, cut-off befor
return before, after
end
-function environment.reconstruct_commandline(arg,noquote)
- arg = arg or environment.original_arguments
+function environment.reconstructcommandline(arg,noquote)
+ arg = arg or environment.originalarguments
if noquote and #arg == 1 then
+ -- we could just do: return unquoted(resolvers.resolve(arg[i]))
local a = arg[1]
a = resolvers.resolve(a)
- a = unquote(a)
+ a = unquoted(a)
return a
elseif #arg > 0 then
local result = { }
for i=1,#arg do
+ -- we could just do: result[#result+1] = format("%q",unquoted(resolvers.resolve(arg[i])))
local a = arg[i]
a = resolvers.resolve(a)
- a = unquote(a)
+ a = unquoted(a)
a = gsub(a,'"','\\"') -- tricky
if find(a," ") then
- result[#result+1] = quote(a)
+ result[#result+1] = quoted(a)
else
result[#result+1] = a
end
end
- return table.join(result," ")
+ return concat(result," ")
else
return ""
end
end
+--~ -- to be tested:
+--~
+--~ function environment.reconstructcommandline(arg,noquote)
+--~ arg = arg or environment.originalarguments
+--~ if noquote and #arg == 1 then
+--~ return unquoted(resolvers.resolve(arg[1]))
+--~ elseif #arg > 0 then
+--~ local result = { }
+--~ for i=1,#arg do
+--~ result[#result+1] = format("%q",unquoted(resolvers.resolve(arg[i]))) -- always quote
+--~ end
+--~ return concat(result," ")
+--~ else
+--~ return ""
+--~ end
+--~ end
+
if arg then
-- new, reconstruct quoted snippets (maybe better just remove the " then and add them later)
@@ -169,9 +219,10 @@ if arg then
newarg[i] = arg[i]
end
- environment.initialize_arguments(newarg)
- environment.original_arguments = newarg
- environment.raw_arguments = arg
+ environment.initializearguments(newarg)
+
+ environment.originalarguments = mark(newarg)
+ environment.rawarguments = mark(arg)
arg = { } -- prevent duplicate handling
@@ -180,45 +231,37 @@ end
-- weird place ... depends on a not yet loaded module
function environment.texfile(filename)
- return resolvers.find_file(filename,'tex')
+ return resolvers.findfile(filename,'tex')
end
function environment.luafile(filename)
- local resolved = resolvers.find_file(filename,'tex') or ""
+ local resolved = resolvers.findfile(filename,'tex') or ""
if resolved ~= "" then
return resolved
end
- resolved = resolvers.find_file(filename,'texmfscripts') or ""
+ resolved = resolvers.findfile(filename,'texmfscripts') or ""
if resolved ~= "" then
return resolved
end
- return resolvers.find_file(filename,'luatexlibs') or ""
+ return resolvers.findfile(filename,'luatexlibs') or ""
end
environment.loadedluacode = loadfile -- can be overloaded
---~ function environment.loadedluacode(name)
---~ if os.spawn("texluac -s -o texluac.luc " .. name) == 0 then
---~ local chunk = loadstring(io.loaddata("texluac.luc"))
---~ os.remove("texluac.luc")
---~ return chunk
---~ else
---~ environment.loadedluacode = loadfile -- can be overloaded
---~ return loadfile(name)
---~ end
---~ end
-
-function environment.luafilechunk(filename) -- used for loading lua bytecode in the format
+function environment.luafilechunk(filename,silent) -- used for loading lua bytecode in the format
filename = file.replacesuffix(filename, "lua")
local fullname = environment.luafile(filename)
if fullname and fullname ~= "" then
+ local data = environment.loadedluacode(fullname)
if trace_locating then
- logs.report("fileio","loading file %s", fullname)
+ report_lua("loading file %s%s", fullname, not data and " failed" or "")
+ elseif not silent then
+ texio.write("<",data and "+ " or "- ",fullname,">")
end
- return environment.loadedluacode(fullname)
+ return data
else
if trace_locating then
- logs.report("fileio","unknown file %s", filename)
+ report_lua("unknown file %s", filename)
end
return nil
end
@@ -238,7 +281,7 @@ function environment.loadluafile(filename, version)
local fullname = (lucname and environment.luafile(lucname)) or ""
if fullname ~= "" then
if trace_locating then
- logs.report("fileio","loading %s", fullname)
+ report_lua("loading %s", fullname)
end
chunk = loadfile(fullname) -- this way we don't need a file exists check
end
@@ -256,7 +299,7 @@ function environment.loadluafile(filename, version)
return true
else
if trace_locating then
- logs.report("fileio","version mismatch for %s: lua=%s, luc=%s", filename, v, version)
+ report_lua("version mismatch for %s: lua=%s, luc=%s", filename, v, version)
end
environment.loadluafile(filename)
end
@@ -267,12 +310,12 @@ function environment.loadluafile(filename, version)
fullname = (luaname and environment.luafile(luaname)) or ""
if fullname ~= "" then
if trace_locating then
- logs.report("fileio","loading %s", fullname)
+ report_lua("loading %s", fullname)
end
chunk = loadfile(fullname) -- this way we don't need a file exists check
if not chunk then
if trace_locating then
- logs.report("fileio","unknown file %s", filename)
+ report_lua("unknown file %s", filename)
end
else
assert(chunk)()