From 7c1a1b89fbaa2bfad94ca825c4b734e459993654 Mon Sep 17 00:00:00 2001 From: Hans Hagen Date: Fri, 11 Jan 2008 12:09:00 +0100 Subject: stable 2008.01.11 12:09 --- scripts/context/lua/context | 3 + scripts/context/lua/context.cmd | 5 + scripts/context/lua/luatools.lua | 2 +- scripts/context/lua/mtx-context.lua | 78 ++++++++++----- scripts/context/lua/mtxrun.lua | 194 +++++++++++++++++++++++++----------- 5 files changed, 196 insertions(+), 86 deletions(-) create mode 100644 scripts/context/lua/context create mode 100644 scripts/context/lua/context.cmd (limited to 'scripts') diff --git a/scripts/context/lua/context b/scripts/context/lua/context new file mode 100644 index 000000000..a9d71ce9c --- /dev/null +++ b/scripts/context/lua/context @@ -0,0 +1,3 @@ +#!/bin/sh + +mtxrun --script context $@ diff --git a/scripts/context/lua/context.cmd b/scripts/context/lua/context.cmd new file mode 100644 index 000000000..64ac1ea91 --- /dev/null +++ b/scripts/context/lua/context.cmd @@ -0,0 +1,5 @@ +@echo off +setlocal +set ownpath=%~dp0% +texlua "%ownpath%mtxrun.lua" --script context %* +endlocal diff --git a/scripts/context/lua/luatools.lua b/scripts/context/lua/luatools.lua index d6af50fb6..099f254e5 100644 --- a/scripts/context/lua/luatools.lua +++ b/scripts/context/lua/luatools.lua @@ -1057,7 +1057,7 @@ else end function io.loaddata(filename) - local f = io.open(filename) + local f = io.open(filename,'rb') if f then local data = f:read('*all') f:close() diff --git a/scripts/context/lua/mtx-context.lua b/scripts/context/lua/mtx-context.lua index 2a4e25071..6d01938ef 100644 --- a/scripts/context/lua/mtx-context.lua +++ b/scripts/context/lua/mtx-context.lua @@ -11,7 +11,7 @@ texmf.instance = instance -- we need to get rid of this / maybe current instance scripts = scripts or { } scripts.context = scripts.context or { } --- l-file +-- l-file / todo function file.needsupdate(oldfile,newfile) return true @@ -228,12 +228,12 @@ do ctxdata.variables['job'] = ctxdata.jobname - ctxdata.flags = xml.all_texts(ctxdata.xmldata,"/ctx:job/ctx:flags/ctx:flag",true) - ctxdata.environments = xml.all_texts(ctxdata.xmldata,"/ctx:job/ctx:process/ctx:resources/ctx:environment",true) - ctxdata.modules = xml.all_texts(ctxdata.xmldata,"/ctx:job/ctx:process/ctx:resources/ctx:module",true) - ctxdata.filters = xml.all_texts(ctxdata.xmldata,"/ctx:job/ctx:process/ctx:resources/ctx:filter",true) - ctxdata.modes = xml.all_texts(ctxdata.xmldata,"/ctx:job/ctx:process/ctx:resources/ctx:mode",true) - ctxdata.messages = xml.all_texts(ctxdata.xmldata,"ctx:message",true) + ctxdata.flags = xml.collect_texts(ctxdata.xmldata,"/ctx:job/ctx:flags/ctx:flag",true) + ctxdata.environments = xml.collect_texts(ctxdata.xmldata,"/ctx:job/ctx:process/ctx:resources/ctx:environment",true) + ctxdata.modules = xml.collect_texts(ctxdata.xmldata,"/ctx:job/ctx:process/ctx:resources/ctx:module",true) + ctxdata.filters = xml.collect_texts(ctxdata.xmldata,"/ctx:job/ctx:process/ctx:resources/ctx:filter",true) + ctxdata.modes = xml.collect_texts(ctxdata.xmldata,"/ctx:job/ctx:process/ctx:resources/ctx:mode",true) + ctxdata.messages = xml.collect_texts(ctxdata.xmldata,"ctx:message",true) ctxdata.flags = ctxrunner.reflag(ctxdata.flags) @@ -405,10 +405,10 @@ function scripts.context.multipass.makeoptionfile(jobname,ctxdata) local f = io.open(jobname..".top","w") if f then local finalrun, kindofrun, currentrun = false, 0, 0 ---~ local function someflag(flag) ---~ return (ctxdata and ctxdata.flags[flag]) or environment.argument(flag) ---~ end - local someflag = environment.argument + local function someflag(flag) + return (ctxdata and ctxdata.flags[flag]) or environment.argument(flag) + end +--~ local someflag = environment.argument local function setvalue(flag,format,hash,default) local a = someflag(flag) or default if a and a ~= "" then @@ -422,8 +422,8 @@ function scripts.context.multipass.makeoptionfile(jobname,ctxdata) end end local function setvalues(flag,format) - if type(flag) == "table" then - for _, v in pairs(flag) do + if type(flag) == "table" then + for k, v in pairs(flag) do f:write(format:format(v),"\n") end else @@ -496,15 +496,32 @@ function scripts.context.multipass.copytuifile(jobname) g:write("% traditional utility file, only commands written by mtxrun/context\n%\n") for line in f:lines() do if line:find("^c ") then - g:write((line:gsub("^c ","")),"\n") + g:write((line:gsub("^c ","")),"%\n") end end + g:write("\\endinput\n") f:close() g:close() end end +scripts.context.xmlsuffixes = table.tohash { + "xml", +} + function scripts.context.run(ctxdata) + local function makestub(format,filename) + local stubname = file.replacesuffix(file.basename(filename),'run') + local f = io.open(stubname,'w') + if f then + f:write("\\starttext\n") + f:write(string.format(format,filename),"\n") + f:write("\\stoptext\n") + f:close() + filename = stubname + end + return filename + end if ctxdata then -- todo: interface for k,v in pairs(ctxdata.flags) do @@ -526,16 +543,10 @@ function scripts.context.run(ctxdata) filename = "./" .. filename end -- also other stubs - if environment.argument("forcexml") then - local stubname = file.replacesuffix(file.basename(filename),'run') - local f = io.open(stubname,'w') - if f then - f:write("\\starttext\n") - f:write(string.format("\\processXMLfilegrouped{%s}\n",filename)) - f:write("\\stoptext\n") - f:close() - filename = stubname - end + if environment.argument("forcexml") or scripts.context.xmlsuffixes[file.extname(filename) or "?"] then -- mkii + filename = makestub("\\processXMLfilegrouped{%s}",filename) + elseif environment.argument("processxml") then -- mkiv + filename = makestub("\\xmlprocess{%s}",filename) end -- local command = "luatex --fmt=" .. string.quote(formatfile) .. " --lua=" .. string.quote(scriptfile) .. " " .. string.quote(filename) @@ -575,6 +586,13 @@ function scripts.context.make() end end +function scripts.context.generate() + -- hack, should also be a shared function + local command = "luatools --generate " .. name + input.report("running command: " .. command) + os.execute(command) +end + function scripts.context.ctx() local ctxdata = ctxrunner.new() ctxdata.jobname = environment.files[1] @@ -582,15 +600,17 @@ function scripts.context.ctx() scripts.context.run(ctxdata) end +input.verbose = false + banner = banner .. " | context tools " messages.help = [[ ---run process (one or more) files ---make generate formats +--run process (one or more) files (default action) +--make create context formats formats +--generate generate file database etc. --ctx=name use ctx file ]] -input.verbose = true input.starttiming(scripts.context) if environment.argument("run") then @@ -599,6 +619,10 @@ elseif environment.argument("make") then scripts.context.make() elseif environment.argument("ctx") then scripts.context.ctx() +elseif environment.argument("help") then + input.help(banner,messages.help) +elseif environment.filename then + scripts.context.run() else input.help(banner,messages.help) end diff --git a/scripts/context/lua/mtxrun.lua b/scripts/context/lua/mtxrun.lua index 5a19d7960..96077d4f8 100644 --- a/scripts/context/lua/mtxrun.lua +++ b/scripts/context/lua/mtxrun.lua @@ -8,7 +8,6 @@ if not modules then modules = { } end modules ['mtxrun'] = { license = "see context related readme files" } - -- one can make a stub: -- -- #!/bin/sh @@ -1072,7 +1071,7 @@ else end function io.loaddata(filename) - local f = io.open(filename) + local f = io.open(filename,'rb') if f then local data = f:read('*all') f:close() @@ -1987,6 +1986,12 @@ do return "" end + local cleanup = false + + function xml.set_text_cleanup(fnc) + cleanup = fnc + end + local function add_attribute(namespace,tag,value) if tag == "xmlns" then xmlns[#xmlns+1] = xml.resolvens(value) @@ -2041,7 +2046,11 @@ do end end local function add_text(text) - dt[#dt+1] = text + if cleanup and #text > 0 then + dt[#dt+1] = cleanup(text) + else + dt[#dt+1] = text + end end local function add_special(what, spacing, text) if #spacing > 0 then @@ -2344,7 +2353,7 @@ do else if ats then -- handle(format("<%s:%s %s/>",ens,etg,table.concat(ats," "))) - handle("<" .. ens .. ":" .. etg .. table.concat(ats," ") .. "/>") + handle("<" .. ens .. ":" .. etg .. " " .. table.concat(ats," ") .. "/>") else -- handle(format("<%s:%s/>",ens,etg)) handle("<" .. ens .. ":" .. "/>") @@ -2368,7 +2377,7 @@ do else if ats then -- handle(format("<%s %s/>",etg,table.concat(ats," "))) - handle("<" .. etg .. table.concat(ats," ") .. "/>") + handle("<" .. etg .. " " .. table.concat(ats," ") .. "/>") else -- handle(format("<%s/>",etg)) handle("<" .. etg .. "/>") @@ -3669,15 +3678,14 @@ do if unicode and unicode.utf8 then end end - local entities = xml.entities local function resolve(e) - local e = entities[e] - if e then - return e + local ee = entities[e] + if ee then + return ee elseif e:find("#x") then - return char(tonumber(s:sub(3),16)) + return char(tonumber(e:sub(3),16)) else local h = entities.handler return (h and h(e)) or "&" .. e .. ";" @@ -3698,6 +3706,33 @@ do if unicode and unicode.utf8 then end end + function xml.utfize_text(str) + if str:find("&#") then + return (str:gsub("&#x(.-);",toutf)) + else + return str + end + end + + function xml.resolve_text_entities(str) + if str:find("&") then + return (str:gsub("&(.-);",resolve)) + else + return str + end + end + + function xml.show_text_entities(str) + if str:find("&") then + return (str:gsub("&(.-);","[%1]")) + else + return str + end + end + +-- xml.set_text_cleanup(xml.show_text_entities) +-- xml.set_text_cleanup(xml.resolve_text_entities) + end end --~ xml.lshow("/../../../a/(b|c)[@d='e']/f") @@ -6046,6 +6081,76 @@ end --~ print(table.serialize(input.aux.splitpathexpr("/usr/share/texmf-{texlive,tetex}", {}))) +-- command line resolver: + +--~ print(input.resolve("abc env:tmp file:cont-en.tex path:cont-en.tex full:cont-en.tex rel:zapf/one/p-chars.tex")) + +do + + local resolvers = { } + + resolvers.environment = function(instance,str) + return input.clean_path(os.getenv(str) or os.getenv(str:upper()) or os.getenv(str:lower()) or "") + end + resolvers.relative = function(instance,str,n) + if io.exists(str) then + -- nothing + elseif io.exists("./" .. str) then + str = "./" .. str + else + local p = "../" + for i=1,n or 2 do + if io.exists(p .. str) then + str = p .. str + break + else + p = p .. "../" + end + end + end + return input.clean_path(str) + end + resolvers.locate = function(instance,str) + local fullname = input.find_given_file(instance,str) or "" + return input.clean_path((fullname ~= "" and fullname) or str) + end + resolvers.filename = function(instance,str) + local fullname = input.find_given_file(instance,str) or "" + return input.clean_path(file.basename((fullname ~= "" and fullname) or str)) + end + resolvers.pathname = function(instance,str) + local fullname = input.find_given_file(instance,str) or "" + return input.clean_path(file.dirname((fullname ~= "" and fullname) or str)) + end + + resolvers.env = resolvers.environment + resolvers.rel = resolvers.relative + resolvers.loc = resolvers.locate + resolvers.kpse = resolvers.locate + resolvers.full = resolvers.locate + resolvers.file = resolvers.filename + resolvers.path = resolvers.pathname + + function resolve(instance,str) + if type(str) == "table" then + for k, v in pairs(str) do + str[k] = resolve(instance,v) or v + end + elseif str and str ~= "" then + str = str:gsub("([a-z]+):([^ ]+)", function(method,target) + if resolvers[method] then + return resolvers[method](instance,target) + else + return method .. ":" .. target + end + end) + end + return str + end + + input.resolve = resolve + +end if not modules then modules = { } end modules ['luat-tmp'] = { @@ -7114,7 +7219,7 @@ function input.runners.execute_program(instance,fullname) fullname = fullname:gsub("^bin:","") local command = fullname .. " " .. environment.reconstruct_commandline(environment.original_arguments) input.report("") - input.report("executing: " .. command) +--~ input.report("executing: " .. command) input.report("\n \n") local code = os.exec(command) return code == 0 @@ -7161,38 +7266,16 @@ function input.runners.handle_stubs(instance,create) end end -function input.resolve_prefixes(instance,str) - -- [env|environment|rel|relative|loc|locate|kpse|path|file]: - return (str:gsub("(%a+)%:(%S+)", function(k,v) - if k == "env" or k == "environment" then - v = os.getenv(k) or "" - elseif k == "rel" or k == "relative" then - for _,p in pairs({".","..","../.."}) do - local f = p .. "/" .. v - if file.exist(v) then break end - end - elseif k == "kpse" or k == "loc" or k == "locate" or k == "file" or k == "path" then -instance.progname = environment.argument("progname") or instance.progname -instance.format = environment.argument("format") or instance.format - v = input.find_given_file(instance, v) - if k == "path" then v = file.dirname(v) end - else - v = "" - end - return v - end)) -end - -function input.runners.resolve_string(instance) - instance.progname = environment.argument("progname") or environment.argument("program") or instance.progname - instance.format = environment.argument("format") or instance.format - input.runners.report_location(instance,input.resolve_prefixes(instance,table.join(environment.files, " "))) +function input.runners.resolve_string(instance,filename) + if filename and filename ~= "" then + input.runners.report_location(instance,input.resolve(instance,filename)) + end end -function input.runners.locate_file(instance) - instance.progname = environment.argument("progname") or instance.progname - instance.format = environment.argument("format") or instance.format - input.runners.report_location(instance,input.find_given_file(instance, environment.files[1] or "")) +function input.runners.locate_file(instance,filename) + if filename and filename ~= "" then + input.runners.report_location(instance,input.find_given_file(instance,filename)) + end end function input.runners.report_location(instance,result) @@ -7210,7 +7293,7 @@ end function input.runners.edit_script(instance,filename) local editor = os.getenv("MTXRUN_EDITOR") or os.getenv("TEXMFSTART_EDITOR") or os.getenv("EDITOR") or 'scite' - local rest = input.resolve_prefixes(instance,filename) + local rest = input.resolve(instance,filename) if rest ~= "" then os.launch(editor .. " " .. rest) end @@ -7303,17 +7386,17 @@ function input.runners.execute_ctx_script(instance,filename) -- mtx- if not fullname or fullname == "" then fullname = "mtx-" .. filename .. suffix - fullname = found(fullname) and input.find_file(instance,fullname) + fullname = found(fullname) or input.find_file(instance,fullname) end -- mtx-s if not fullname or fullname == "" then fullname = "mtx-" .. filename .. "s" .. suffix - fullname = found(fullname) and input.find_file(instance,fullname) + fullname = found(fullname) or input.find_file(instance,fullname) end -- mtx- if not fullname or fullname == "" then fullname = "mtx-" .. filename:gsub("s$","") .. suffix - fullname = found(fullname) and input.find_file(instance,fullname) + fullname = found(fullname) or input.find_file(instance,fullname) end -- that should do it if fullname and fullname ~= "" then @@ -7369,6 +7452,11 @@ local filename = environment.files[1] or "" local ok = true local before, after = environment.split_arguments(filename) + +input.runners.my_prepare_b(instance) +before = input.resolve(instance,before) -- experimental here +after = input.resolve(instance,after) -- experimental here + environment.initialize_arguments(before) if environment.argument("selfmerge") then @@ -7377,32 +7465,25 @@ if environment.argument("selfmerge") then elseif environment.argument("selfclean") then -- remove embedded libraries utils.merger.selfclean(own.name) -elseif environment.arguments["selfupdate"] then - input.runners.my_prepare_b(instance) +elseif environment.argument("selfupdate") then input.verbose = true input.update_script(instance,own.name,"mtxrun") elseif environment.argument("ctxlua") or environment.argument("internal") then -- run a script by loading it (using libs) - input.runners.my_prepare_b(instance) ok = input.runners.execute_script(instance,filename,true) elseif environment.argument("script") then -- run a script by loading it (using libs), pass args - input.runners.my_prepare_b(instance) ok = input.runners.execute_ctx_script(instance,filename) elseif environment.argument("execute") then -- execute script - input.runners.my_prepare_b(instance) ok = input.runners.execute_script(instance,filename) elseif environment.argument("direct") then -- equals bin: - input.runners.my_prepare_b(instance) ok = input.runners.execute_program(instance,filename) elseif environment.argument("edit") then -- edit file - input.runners.my_prepare_b(instance) input.runners.edit_script(instance,filename) elseif environment.argument("launch") then - input.runners.my_prepare_b(instance) input.runners.launch_file(instance,filename) elseif environment.argument("make") then -- make stubs @@ -7412,17 +7493,14 @@ elseif environment.argument("remove") then input.runners.handle_stubs(instance,false) elseif environment.argument("resolve") then -- resolve string - input.runners.my_prepare_b(instance) - input.runners.resolve_string(instance) + input.runners.resolve_string(instance,filename) elseif environment.argument("locate") then -- locate file - input.runners.my_prepare_b(instance) - input.runners.locate_file(instance) + input.runners.locate_file(instance,filename) elseif environment.argument("help") or filename=='help' or filename == "" then input.help(banner,messages.help) else -- execute script - input.runners.my_prepare_b(instance) if filename:find("^bin:") then ok = input.runners.execute_program(instance,filename) else -- cgit v1.2.3