diff options
Diffstat (limited to 'scripts/context/lua/mtxrun.lua')
| -rw-r--r-- | scripts/context/lua/mtxrun.lua | 238 | 
1 files changed, 150 insertions, 88 deletions
| diff --git a/scripts/context/lua/mtxrun.lua b/scripts/context/lua/mtxrun.lua index 727099027..d3e50e00e 100644 --- a/scripts/context/lua/mtxrun.lua +++ b/scripts/context/lua/mtxrun.lua @@ -278,7 +278,7 @@ function string:totable()      return lpegmatch(pattern,self)  end ---~ for _, str in ipairs { +--~ local t = {  --~     "1234567123456712345671234567",  --~     "a\tb\tc",  --~     "aa\tbb\tcc", @@ -286,7 +286,10 @@ end  --~     "aaaa\tbbbb\tcccc",  --~     "aaaaa\tbbbbb\tccccc",  --~     "aaaaaa\tbbbbbb\tcccccc", ---~ } do print(string.tabtospace(str)) end +--~ } +--~ for k,v do +--~     print(string.tabtospace(t[k])) +--~ end  function string.tabtospace(str,tab)      -- we don't handle embedded newlines @@ -455,6 +458,8 @@ function string:split(separator)      return match(c,self)  end +lpeg.splitters = cache +  local cache = { }  function lpeg.checkedsplit(separator,str) @@ -519,7 +524,7 @@ table.join = table.concat  local concat, sort, insert, remove = table.concat, table.sort, table.insert, table.remove  local format, find, gsub, lower, dump, match = string.format, string.find, string.gsub, string.lower, string.dump, string.match  local getmetatable, setmetatable = getmetatable, setmetatable -local type, next, tostring, tonumber, ipairs, pairs = type, next, tostring, tonumber, ipairs, pairs +local type, next, tostring, tonumber, ipairs = type, next, tostring, tonumber, ipairs  local unpack = unpack or table.unpack  function table.strip(tab) @@ -586,7 +591,7 @@ end  table.sortedkeys     = sortedkeys  table.sortedhashkeys = sortedhashkeys -function table.sortedpairs(t) +function table.sortedhash(t)      local s = sortedhashkeys(t) -- maybe just sortedkeys      local n = 0      local function kv(s) @@ -597,6 +602,8 @@ function table.sortedpairs(t)      return kv, s  end +table.sortedpairs = table.sortedhash +  function table.append(t, list)      for _,v in next, list do          insert(t,v) @@ -719,18 +726,18 @@ end  -- slower than #t on indexed tables (#t only returns the size of the numerically indexed slice) -function table.is_empty(t) +function table.is_empty(t) -- obolete, use inline code instead      return not t or not next(t)  end -function table.one_entry(t) +function table.one_entry(t) -- obolete, use inline code instead      local n = next(t)      return n and not next(t,n)  end -function table.starts_at(t) -    return ipairs(t,1)(t,0) -end +--~ function table.starts_at(t) -- obsolete, not nice +--~     return ipairs(t,1)(t,0) +--~ end  function table.tohash(t,value)      local h = { } @@ -834,7 +841,7 @@ local function do_serialize(root,name,depth,level,indexed)      end      -- we could check for k (index) being number (cardinal)      if root and next(root) then -        local first, last = nil, 0 -- #root cannot be trusted here +        local first, last = nil, 0 -- #root cannot be trusted here (will be ok in 5.2 when ipairs is gone)          if compact then              -- NOT: for k=1,#root do (we need to quit at nil)              for k,v in ipairs(root) do -- can we use next? @@ -1543,13 +1550,14 @@ function io.ask(question,default,options)          elseif not options then              return answer          else -            for _,v in pairs(options) do -                if v == answer then +            for k=1,#options do +                if options[k] == answer then                      return answer                  end              end              local pattern = "^" .. answer -            for _,v in pairs(options) do +            for k=1,#options do +                local v = options[k]                  if find(v,pattern) then                      return v                  end @@ -2323,7 +2331,7 @@ function file.splitname(str) -- returns drive, path, base, suffix      return lpegmatch(pattern,str)  end --- function test(t) for k, v in pairs(t) do print(v, "=>", file.splitname(v)) end end +-- function test(t) for k, v in next, t do print(v, "=>", file.splitname(v)) end end  --  -- test { "c:", "c:/aa", "c:/aa/bb", "c:/aa/bb/cc", "c:/aa/bb/cc.dd", "c:/aa/bb/cc.dd.ee" }  -- test { "c:", "c:aa", "c:aa/bb", "c:aa/bb/cc", "c:aa/bb/cc.dd", "c:aa/bb/cc.dd.ee" } @@ -2768,8 +2776,9 @@ local make_indeed = true -- false  if string.find(os.getenv("PATH"),";") then -- os.type == "windows"      function dir.mkdirs(...) -        local str, pth = "", "" -        for _, s in ipairs({...}) do +        local str, pth, t = "", "", { ... } +        for i=1,#t do +            local s = t[i]              if s ~= "" then                  if str ~= "" then                      str = str .. "/" .. s @@ -2867,8 +2876,9 @@ if string.find(os.getenv("PATH"),";") then -- os.type == "windows"  else      function dir.mkdirs(...) -        local str, pth = "", "" -        for _, s in ipairs({...}) do +        local str, pth, t = "", "", { ... } +        for i=1,#t do +            local s = t[i]              if s ~= "" then                  if str ~= "" then                      str = str .. "/" .. s @@ -3049,6 +3059,7 @@ if not modules then modules = { } end modules ['l-utils'] = {  local gsub = string.gsub  local concat = table.concat +local type, next = type, next  if not utils        then utils        = { } end  if not utils.merger then utils.merger = { } end @@ -3124,9 +3135,10 @@ function utils.merger._self_libs_(libs,list)      if type(libs) == 'string' then libs = { libs } end      if type(list) == 'string' then list = { list } end      local foundpath = nil -    for _, lib in ipairs(libs) do -        for _, pth in ipairs(list) do -            pth = gsub(pth,"\\","/") -- file.clean_path +    for i=1,#libs do +        local lib = libs[i] +        for j=1,#list do +            local pth = gsub(list[j],"\\","/") -- file.clean_path              utils.report("checking library path %s",pth)              local name = pth .. "/" .. lib              if lfs.isfile(name) then @@ -3138,7 +3150,8 @@ function utils.merger._self_libs_(libs,list)      if foundpath then          utils.report("using library path %s",foundpath)          local right, wrong = { }, { } -        for _, lib in ipairs(libs) do +        for i=1,#libs do +            local lib = libs[i]              local fullname = foundpath .. "/" .. lib              if lfs.isfile(fullname) then              --  right[#right+1] = lib @@ -3491,6 +3504,8 @@ if not modules then modules = { } end modules ['trac-tra'] = {  -- bound to a variable, like node.new, node.copy etc (contrary to for instance  -- node.has_attribute which is bound to a has_attribute local variable in mkiv) +local debug = require "debug" +  local getinfo = debug.getinfo  local type, next = type, next  local concat = table.concat @@ -3538,7 +3553,7 @@ function debugger.showstats(printer,threshold)      local total, grandtotal, functions = 0, 0, 0      printer("\n") -- ugly but ok   -- table.sort(counters) -    for func, count in pairs(counters) do +    for func, count in next, counters do          if count > threshold then              local name = getname(func)              if not find(name,"for generator") then @@ -3573,7 +3588,7 @@ end  --~     local total, grandtotal, functions = 0, 0, 0  --~     printer("\n") -- ugly but ok  --~  -- table.sort(counters) ---~     for func, count in pairs(counters) do +--~     for func, count in next, counters do  --~         if count > threshold then  --~             printer(format("%8i  %s", count, func))  --~             total = total + count @@ -3749,8 +3764,9 @@ end  function setters.show(t)      commands.writestatus("","") -    for k,v in ipairs(setters.list(t)) do -        commands.writestatus(t.name,v) +    local list = setters.list(t) +    for k=1,#list do +        commands.writestatus(t.name,list[k])      end      commands.writestatus("","")  end @@ -4550,7 +4566,7 @@ local function copy(old,tables)          if not tables[old] then              tables[old] = new          end -        for k,v in pairs(old) do +        for k,v in next, old do              new[k] = (type(v) == "table" and (tables[v] or copy(v, tables))) or v          end          local mt = getmetatable(old) @@ -6316,7 +6332,7 @@ function xml.strip_leading_spaces(dk,d,k) -- cosmetic, for manual  end  --~ xml.escapes   = { ['&'] = '&', ['<'] = '<', ['>'] = '>', ['"'] = '"' } ---~ xml.unescapes = { } for k,v in pairs(xml.escapes) do xml.unescapes[v] = k end +--~ xml.unescapes = { } for k,v in next, xml.escapes do xml.unescapes[v] = k end  --~ function xml.escaped  (str) return (gsub(str,"(.)"   , xml.escapes  )) end  --~ function xml.unescaped(str) return (gsub(str,"(&.-;)", xml.unescapes)) end @@ -7265,7 +7281,8 @@ if not environment.jobname                              then             environ  function environment.initialize_arguments(arg)      local arguments, files = { }, { }      environment.arguments, environment.files, environment.sortedflags = arguments, files, nil -    for index, argument in pairs(arg) do +    for index=1,#arg do +        local argument = arg[index]          if index > 0 then              local flag, value = match(argument,"^%-+(.-)=(.-)$")              if flag then @@ -7298,14 +7315,15 @@ function environment.argument(name,partial)          return arguments[name]      elseif partial then          if not sortedflags then -            sortedflags = { } -            for _,v in pairs(table.sortedkeys(arguments)) do -                sortedflags[#sortedflags+1] = "^" .. v +            sortedflags = table.sortedkeys(arguments) +            for k=1,#sortedflags do +                sortedflags[k] = "^" .. sortedflags[k]              end              environment.sortedflags = sortedflags          end          -- example of potential clash: ^mode ^modefile -        for _,v in ipairs(sortedflags) do +        for k=1,#sortedflags do +            local v = sortedflags[k]              if find(name,v) then                  return arguments[sub(v,2,#v)]              end @@ -7314,9 +7332,13 @@ function environment.argument(name,partial)      return nil  end +environment.argument("x",true) +  function environment.split_arguments(separator) -- rather special, cut-off before separator      local done, before, after = false, { }, { } -    for _,v in ipairs(environment.original_arguments) do +    local original_arguments = environment.original_arguments +    for k=1,#original_arguments do +        local v = original_arguments[k]          if not done and v == separator then              done = true          elseif done then @@ -7335,9 +7357,10 @@ function environment.reconstruct_commandline(arg,noquote)          a = resolvers.resolve(a)          a = unquote(a)          return a -    elseif next(arg) then +    elseif #arg > 0 then          local result = { } -        for _,a in ipairs(arg) do -- ipairs 1 .. #n +        for i=1,#arg do +            local a = arg[i]              a = resolvers.resolve(a)              a = unquote(a)              a = gsub(a,'"','\\"') -- tricky @@ -7358,7 +7381,8 @@ if arg then      -- new, reconstruct quoted snippets (maybe better just remove the " then and add them later)      local newarg, instring = { }, false -    for index, argument in ipairs(arg) do +    for index=1,#arg do +        local argument = arg[index]          if find(argument,"^\"") then              newarg[#newarg+1] = gsub(argument,"^\"","")              if not find(argument,"\"$") then @@ -8041,7 +8065,7 @@ if not modules then modules = { } end modules ['data-inp'] = {  -- * some public auxiliary functions were made private  --  -- TODO: os.getenv -> os.env[] --- TODO: instances.[hashes,cnffiles,configurations,522] -> ipairs (alles check, sneller) +-- TODO: instances.[hashes,cnffiles,configurations,522]  -- TODO: check escaping in find etc, too much, too slow  -- This lib is multi-purpose and can be loaded again later on so that @@ -8452,8 +8476,8 @@ local function splitpathexpr(str, t, validate)          end      end      if trace_expansions then -        for k,v in ipairs(t) do -            logs.report("fileio","% 4i: %s",k,v) +        for k=1,#t do +            logs.report("fileio","% 4i: %s",k,t[k])          end      end      return t @@ -8658,7 +8682,9 @@ local function load_cnf_file(fname)  end  local function collapse_cnf_data() -- potential optimization: pass start index (setup and configuration are shared) -    for _,c in ipairs(instance.order) do +    local order = instance.order +    for i=1,#order do +        local c = order[i]          for k,v in next, c do              if not instance.variables[k] then                  if instance.environment[k] then @@ -8674,8 +8700,9 @@ end  function resolvers.load_cnf()      local function loadoldconfigdata() -        for _, fname in ipairs(instance.cnffiles) do -            load_cnf_file(fname) +        local cnffiles = instance.cnffiles +        for i=1,#cnffiles do +            load_cnf_file(cnffiles[i])          end      end      -- instance.cnffiles contain complete names now ! @@ -8687,9 +8714,10 @@ function resolvers.load_cnf()              logs.report("fileio","no cnf files found (TEXMFCNF may not be set/known)")          end      else -        instance.rootpath = instance.cnffiles[1] -        for k,fname in ipairs(instance.cnffiles) do -            instance.cnffiles[k] = file.collapse_path(fname) +        local cnffiles = instance.cnffiles +        instance.rootpath = cnffiles[1] +        for k=1,#cnffiles do +            instance.cnffiles[k] = file.collapse_path(cnffiles[k])          end          for i=1,3 do              instance.rootpath = file.dirname(instance.rootpath) @@ -8717,8 +8745,9 @@ function resolvers.load_lua()          -- yet harmless      else          instance.rootpath = instance.luafiles[1] -        for k,fname in ipairs(instance.luafiles) do -            instance.luafiles[k] = file.collapse_path(fname) +        local luafiles = instance.luafiles +        for k=1,#luafiles do +            instance.luafiles[k] = file.collapse_path(luafiles[k])          end          for i=1,3 do              instance.rootpath = file.dirname(instance.rootpath) @@ -8781,7 +8810,9 @@ end  -- locators  function resolvers.locatelists() -    for _, path in ipairs(resolvers.clean_path_list('TEXMF')) do +    local texmfpaths = resolvers.clean_path_list('TEXMF') +    for i=1,#texmfpaths do +        local path = texmfpaths[i]          if trace_locating then              logs.report("fileio","locating list of '%s'",path)          end @@ -8814,7 +8845,9 @@ function resolvers.loadfiles()      instance.loaderror = false      instance.files = { }      if not instance.renewcache then -        for _, hash in ipairs(instance.hashes) do +        local hashes = instance.hashes +        for k=1,#hashes do +            local hash = hashes[k]              resolvers.hashdatabase(hash.tag,hash.name)              if instance.loaderror then break end          end @@ -8828,8 +8861,9 @@ end  -- generators:  function resolvers.loadlists() -    for _, hash in ipairs(instance.hashes) do -        resolvers.generatedatabase(hash.tag) +    local hashes = instance.hashes +    for i=1,#hashes do +        resolvers.generatedatabase(hashes[i].tag)      end  end @@ -8926,8 +8960,7 @@ end  -- we join them and split them after the expansion has taken place. This  -- is more convenient. -local checkedsplit = string.checkedsplit -local normalsplit  = string.split +--~ local checkedsplit = string.checkedsplit  local cache = { } @@ -8951,8 +8984,8 @@ local split = lpegmatch(splitter,str)              end              if trace_expansions then                  logs.report("fileio","splitting path specification '%s'",str) -                for k,v in ipairs(found) do -                    logs.report("fileio","% 4i: %s",k,v) +                for k=1,#found do +                    logs.report("fileio","% 4i: %s",k,found[k])                  end              end              cache[str] = found @@ -8964,8 +8997,9 @@ end  resolvers.split_kpse_path = split_kpse_path  function resolvers.splitconfig() -    for i,c in ipairs(instance) do -        for k,v in pairs(c) do +    for i=1,#instance do +        local c = instance[i] +        for k,v in next, c do              if type(v) == 'string' then                  local t = split_kpse_path(v)                  if #t > 1 then @@ -8977,8 +9011,10 @@ function resolvers.splitconfig()  end  function resolvers.joinconfig() -    for i,c in ipairs(instance.order) do -        for k,v in pairs(c) do -- ipairs? +    local order = instance.order +    for i=1,#order do +        local c = order[i] +        for k,v in next, c do -- indexed?              if type(v) == 'table' then                  c[k] = file.join_path(v)              end @@ -9005,8 +9041,9 @@ end  function resolvers.splitexpansions()      local ie = instance.expansions      for k,v in next, ie do -        local t, h = { }, { } -        for _,vv in ipairs(split_kpse_path(v)) do +        local t, h, p = { }, { }, split_kpse_path(v) +        for kk=1,#p do +            local vv = p[kk]              if vv ~= "" and not h[vv] then                  t[#t+1] = vv                  h[vv] = true @@ -9053,11 +9090,15 @@ function resolvers.serialize(files)      end      t[#t+1] = "return {"      if instance.sortdata then -        for _, k in pairs(sortedkeys(files)) do -- ipairs +	local sortedfiles = sortedkeys(files) +	for i=1,#sortedfiles do +	    local k = sortedfiles[i]              local fk  = files[k]              if type(fk) == 'table' then                  t[#t+1] = "\t['" .. k .. "']={" -                for _, kk in pairs(sortedkeys(fk)) do -- ipairs +		local sortedfk = sortedkeys(fk) +        	for j=1,#sortedfk do +                    local kk = sortedfk[j]                      t[#t+1] = dump(kk,fk[kk],"\t\t")                  end                  t[#t+1] = "\t}," @@ -9169,7 +9210,9 @@ function resolvers.resetconfig()  end  function resolvers.loadnewconfig() -    for _, cnf in ipairs(instance.luafiles) do +    local luafiles = instance.luafiles +    for i=1,#luafiles do +        local cnf = luafiles[i]          local pathname = file.dirname(cnf)          local filename = file.join(pathname,resolvers.luaname)          local blob = loadfile(filename) @@ -9214,7 +9257,9 @@ end  function resolvers.loadoldconfig()      if not instance.renewcache then -        for _, cnf in ipairs(instance.cnffiles) do +        local cnffiles = instance.cnffiles +        for i=1,#cnffiles do +            local cnf = cnffiles[i]              local dname = file.dirname(cnf)              resolvers.load_data(dname,'configuration')              instance.order[#instance.order+1] = instance.configuration[dname] @@ -10018,13 +10063,14 @@ function resolvers.for_files(command, files, filetype, mustexist)          if trace_locating then              report('') -- ?          end -        for _, file in ipairs(files) do +        for f=1,#files do +            local file = files[f]              local result = command(file,filetype,mustexist)              if type(result) == 'string' then                  report(result)              else -                for _,v in ipairs(result) do -                    report(v) +                for i=1,#result do +                    report(result[i]) -- could be unpack                  end              end          end @@ -10071,7 +10117,7 @@ end  function table.sequenced(t,sep) -- temp here      local s = { } -    for k, v in pairs(t) do -- pairs? +    for k, v in next, t do -- indexed?          s[#s+1] = k .. "=" .. tostring(v)      end      return concat(s, sep or " | ") @@ -10103,8 +10149,9 @@ function resolvers.clean_path(str)  end  function resolvers.do_with_path(name,func) -    for _, v in pairs(resolvers.expanded_path_list(name)) do -- pairs? -        func("^"..resolvers.clean_path(v)) +    local pathlist = resolvers.expanded_path_list(name) +    for i=1,#pathlist do +        func("^"..resolvers.clean_path(pathlist[i]))      end  end @@ -10113,7 +10160,9 @@ function resolvers.do_with_var(name,func)  end  function resolvers.with_files(pattern,handle) -    for _, hash in ipairs(instance.hashes) do +    local hashes = instance.hashes +    for i=1,#hashes do +        local hash = hashes[i]          local blobpath = hash.tag          local blobtype = hash.type          if blobpath then @@ -10128,7 +10177,7 @@ function resolvers.with_files(pattern,handle)                          if type(v) == "string" then                              handle(blobtype,blobpath,v,k)                          else -                            for _,vv in pairs(v) do -- ipairs? +                            for _,vv in next, v do -- indexed                                  handle(blobtype,blobpath,vv,k)                              end                          end @@ -10299,7 +10348,8 @@ function caches.setpath(...)          caches.path = '.'      end      caches.path = resolvers.clean_path(caches.path) -    if not table.is_empty({...}) then +    local dirs = { ... } +    if #dirs > 0 then          local pth = dir.mkdirs(caches.path,...)          return pth      end @@ -10457,7 +10507,8 @@ end  local function resolve(str)      if type(str) == "table" then -        for k, v in pairs(str) do -- ipairs +        for k=1,#str do +            local v = str[k]              str[k] = resolve(v) or v          end      elseif str and str ~= "" then @@ -10470,7 +10521,7 @@ resolvers.resolve = resolve  if os.uname then -    for k, v in pairs(os.uname()) do +    for k, v in next, os.uname() do          if not prefixes[k] then              prefixes[k] = function() return v end          end @@ -10596,7 +10647,7 @@ end  function containers.is_valid(container, name)      if name and name ~= "" then          local storage = container.storage[name] -        return storage and not table.is_empty(storage) and storage.cache_version == container.version +        return storage and storage.cache_version == container.version      else          return false      end @@ -10700,12 +10751,13 @@ resolvers.automounted = resolvers.automounted or { }  function resolvers.automount(usecache)      local mountpaths = resolvers.clean_path_list(resolvers.expansion('TEXMFMOUNT')) -    if table.is_empty(mountpaths) and usecache then +    if (not mountpaths or #mountpaths == 0) and usecache then          mountpaths = { caches.setpath("mount") }      end -    if not table.is_empty(mountpaths) then +    if mountpaths and #mountpaths > 0 then          statistics.starttiming(resolvers.instance) -        for k, root in pairs(mountpaths) do +        for k=1,#mountpaths do +            local root = mountpaths[k]              local f = io.open(root.."/url.tmi")              if f then                  for line in f:lines() do @@ -11549,6 +11601,10 @@ function states.set_by_tag(tag,key,value,default,persistent)                      if not dk then                          dk = { }                          d[k] = dk +                    elseif type(dk) == "string" then +                        -- invalid table, unable to upgrade structure +                        -- hope for the best or delete the state file +                        break                      end                      d = dk                  end @@ -12142,7 +12198,8 @@ end  function runners.save_script_session(filename, list)      local t = { } -    for _, key in ipairs(list) do +    for i=1,#list do +        local key = list[i]          t[key] = environment.arguments[key]      end      io.savedata(filename,table.serialize(t,true)) @@ -12298,10 +12355,13 @@ function runners.execute_ctx_script(filename)              environment.ownscript = fullname              dofile(fullname)              local savename = environment.arguments['save'] -            if savename and runners.save_list and not table.is_empty(runners.save_list or { }) then -                if type(savename) ~= "string" then savename = file.basename(fullname) end -                savename = file.replacesuffix(savename,"cfg") -                runners.save_script_session(savename, runners.save_list) +            if savename then +                local save_list = runners.save_list +                if save_list and next(save_list) then +                    if type(savename) ~= "string" then savename = file.basename(fullname) end +                    savename = file.replacesuffix(savename,"cfg") +                    runners.save_script_session(savename,save_list) +                end              end              return true          end @@ -12314,7 +12374,8 @@ function runners.execute_ctx_script(filename)                  local result = dir.glob((string.gsub(context,"mtx%-context","mtx-*"))) -- () needed                  local valid = { }                  table.sort(result) -                for _, scriptname in ipairs(result) do +                for i=1,#result do +                    local scriptname = result[i]                      local scriptbase = string.match(scriptname,".*mtx%-([^%-]-)%.lua")                      if scriptbase then                          local data = io.loaddata(scriptname) @@ -12329,7 +12390,8 @@ function runners.execute_ctx_script(filename)                      logs.reportline()                      logs.simple("no script name given, known scripts:")                      logs.simple() -                    for k, v in ipairs(valid) do +                    for k=1,#valid do +                        local v = valid[k]                          logs.simple("%-12s  %4s  %s",v[1],v[2],v[3])                      end                  end | 
