diff options
Diffstat (limited to 'macros/luatex/generic/lualibs/lualibs-basic-merged.lua')
-rw-r--r-- | macros/luatex/generic/lualibs/lualibs-basic-merged.lua | 641 |
1 files changed, 280 insertions, 361 deletions
diff --git a/macros/luatex/generic/lualibs/lualibs-basic-merged.lua b/macros/luatex/generic/lualibs/lualibs-basic-merged.lua index ac98df379a..690a3160ac 100644 --- a/macros/luatex/generic/lualibs/lualibs-basic-merged.lua +++ b/macros/luatex/generic/lualibs/lualibs-basic-merged.lua @@ -1,6 +1,6 @@ -- merged file : lualibs-basic-merged.lua -- parent file : lualibs-basic.lua --- merge date : 2021-05-20 23:14 +-- merge date : 2022-10-04 17:16 do -- begin closure to overcome local limits and interference @@ -2737,8 +2737,14 @@ if not math.ceiling then math.ceiling=math.ceil end if not math.round then - local floor=math.floor - function math.round(x) return floor(x+0.5) end + if xmath then + math.round=xmath.round + else + local floor=math.floor + function math.round(x) + return x<0 and -floor(-x+0.5) or floor(x+0.5) + end + end end if not math.div then local floor=math.floor @@ -2808,7 +2814,7 @@ if not math.tointeger then end if not math.ult then local floor=math.floor - function math.tointeger(m,n) + function math.ult(m,n) return floor(m)<floor(n) end end @@ -2882,9 +2888,12 @@ function io.copydata(source,target,action) flush() end end -function io.savedata(filename,data,joiner) - local f=open(filename,"wb") +function io.savedata(filename,data,joiner,append) + local f=open(filename,append and "ab" or "wb") if f then + if append and joiner and f:seek("end")>0 then + f:write(joiner) + end if type(data)=="table" then f:write(concat(data,joiner or "")) elseif type(data)=="function" then @@ -3172,10 +3181,10 @@ if not modules then modules={} end modules ['l-os']={ license="see context related readme files" } local os=os -local date,time=os.date,os.time +local date,time,difftime=os.date,os.time,os.difftime local find,format,gsub,upper,gmatch=string.find,string.format,string.gsub,string.upper,string.gmatch local concat=table.concat -local random,ceil,randomseed=math.random,math.ceil,math.randomseed +local random,ceil,randomseed,modf=math.random,math.ceil,math.randomseed,math.modf local type,setmetatable,tonumber,tostring=type,setmetatable,tonumber,tostring do local selfdir=os.selfdir @@ -3273,7 +3282,7 @@ if not os.__getenv__ then osenv[K]=v end function os.getenv(k) - local K=upper(k) + local K=upper(k) local v=osenv[K] or osgetenv(K) or osgetenv(k) if v=="" then return nil @@ -3291,22 +3300,6 @@ if not os.__getenv__ then setmetatable(os.env,{ __index=__index,__newindex=__newindex } ) end end -local execute=os.execute -local iopopen=io.popen -local function resultof(command) - local handle=iopopen(command,"r") - if handle then - local result=handle:read("*all") or "" - handle:close() - return result - else - return "" - end -end -os.resultof=resultof -function os.pipeto(command) - return iopopen(command,"w") -end if not io.fileseparator then if find(os.getenv("PATH"),";",1,true) then io.fileseparator,io.pathseparator,os.type="\\",";",os.type or "windows" @@ -3321,228 +3314,231 @@ if os.type=="windows" then else os.libsuffix,os.binsuffix,os.binsuffixes='so','',{ '' } end -local launchers={ - windows="start %s", - macosx="open %s", - unix="xdg-open %s &> /dev/null &", -} -function os.launch(str) - local command=format(launchers[os.name] or launchers.unix,str) - execute(command) -end -local gettimeofday=os.gettimeofday or os.clock -os.gettimeofday=gettimeofday -local startuptime=gettimeofday() -function os.runtime() - return gettimeofday()-startuptime -end -local resolvers=os.resolvers or {} -os.resolvers=resolvers -setmetatable(os,{ __index=function(t,k) - local r=resolvers[k] - return r and r(t,k) or nil -end }) -local name,platform=os.name or "linux",os.getenv("MTX_PLATFORM") or "" -if platform~="" then - os.platform=platform -elseif os.type=="windows" then - function resolvers.platform(t,k) - local architecture=os.getenv("PROCESSOR_ARCHITECTURE") or "" - local platform="" - if find(architecture,"AMD64",1,true) then - platform="win64" +do + local execute=os.execute + local iopopen=io.popen + local ostype=os.type + local function resultof(command) + local handle=iopopen(command,ostype=="windows" and "rb" or "r") + if handle then + local result=handle:read("*all") or "" + handle:close() + return result else - platform="mswin" + return "" end - os.setenv("MTX_PLATFORM",platform) - os.platform=platform - return platform end -elseif name=="linux" then - function resolvers.platform(t,k) - local architecture=os.getenv("HOSTTYPE") or resultof("uname -m") or "" - local platform=os.getenv("MTX_PLATFORM") or "" + os.resultof=resultof + function os.pipeto(command) + return iopopen(command,"w") + end + local launchers={ + windows="start %s", + macosx="open %s", + unix="xdg-open %s &> /dev/null &", + } + function os.launch(str) + local command=format(launchers[os.name] or launchers.unix,str) + execute(command) + end +end +do + local gettimeofday=os.gettimeofday or os.clock + os.gettimeofday=gettimeofday + local startuptime=gettimeofday() + function os.runtime() + return gettimeofday()-startuptime + end +end +do + local name=os.name or "linux" + local platform=os.getenv("MTX_PLATFORM") or "" + local architecture=os.uname and os.uname().machine + local bits=os.getenv("MTX_BITS") or find(platform,"64") and 64 or 32 + if platform~="" then + elseif os.type=="windows" then + architecture=string.lower(architecture or os.getenv("PROCESSOR_ARCHITECTURE") or "") + if architecture=="x86_64" then + bits,platform=64,"win64" + elseif find(architecture,"amd64") then + bits,platform=64,"win64" + elseif find(architecture,"arm64") then + bits,platform=64,"windows-arm64" + elseif find(architecture,"arm32") then + bits,platform=32,"windows-arm32" + else + bits,platform=32,"mswin" + end + elseif name=="linux" then + architecture=architecture or os.getenv("HOSTTYPE") or resultof("uname -m") or "" local musl=find(os.selfdir or "","linuxmusl") - if platform~="" then - elseif find(architecture,"x86_64",1,true) then - platform=musl and "linuxmusl" or "linux-64" - elseif find(architecture,"ppc",1,true) then - platform="linux-ppc" + if find(architecture,"x86_64") then + bits,platform=64,musl and "linuxmusl" or "linux-64" + elseif find(architecture,"ppc") then + bits,platform=32,"linux-ppc" else - platform=musl and "linuxmusl" or "linux" + bits,platform=32,musl and "linuxmusl" or "linux" end - os.setenv("MTX_PLATFORM",platform) - os.platform=platform - return platform - end -elseif name=="macosx" then - function resolvers.platform(t,k) - local architecture=resultof("echo $HOSTTYPE") or "" - local platform="" + elseif name=="macosx" then + architecture=architecture or resultof("echo $HOSTTYPE") or "" if architecture=="" then - platform="osx-intel" - elseif find(architecture,"i386",1,true) then - platform="osx-intel" - elseif find(architecture,"x86_64",1,true) then - platform="osx-64" - elseif find(architecture,"arm64",1,true) then - platform="osx-arm" + bits,platform=64,"osx-intel" + elseif find(architecture,"i386") then + bits,platform=64,"osx-intel" + elseif find(architecture,"x86_64") then + bits,platform=64,"osx-64" + elseif find(architecture,"arm64") then + bits,platform=64,"osx-arm" else - platform="osx-ppc" - end - os.setenv("MTX_PLATFORM",platform) - os.platform=platform - return platform - end -elseif name=="sunos" then - function resolvers.platform(t,k) - local architecture=resultof("uname -m") or "" - local platform="" - if find(architecture,"sparc",1,true) then - platform="solaris-sparc" + bits,platform=32,"osx-ppc" + end + elseif name=="sunos" then + architecture=architecture or resultof("uname -m") or "" + if find(architecture,"sparc") then + bits,platform=32,"solaris-sparc" else - platform="solaris-intel" - end - os.setenv("MTX_PLATFORM",platform) - os.platform=platform - return platform - end -elseif name=="freebsd" then - function resolvers.platform(t,k) - local architecture=resultof("uname -m") or "" - local platform="" - if find(architecture,"amd64",1,true) then - platform="freebsd-amd64" + bits,platform=32,"solaris-intel" + end + elseif name=="freebsd" then + architecture=architecture or os.getenv("MACHTYPE") or resultof("uname -m") or "" + if find(architecture,"amd64") or find(architecture,"AMD64") then + bits,platform=64,"freebsd-amd64" else - platform="freebsd" - end - os.setenv("MTX_PLATFORM",platform) - os.platform=platform - return platform - end -elseif name=="kfreebsd" then - function resolvers.platform(t,k) - local architecture=os.getenv("HOSTTYPE") or resultof("uname -m") or "" - local platform="" - if find(architecture,"x86_64",1,true) then - platform="kfreebsd-amd64" + bits,platform=32,"freebsd" + end + elseif name=="kfreebsd" then + architecture=architecture or os.getenv("HOSTTYPE") or resultof("uname -m") or "" + if architecture=="x86_64" then + bits,platform=64,"kfreebsd-amd64" + else + bits,platform=32,"kfreebsd-i386" + end + else + architecture=architecture or resultof("uname -m") or "" + if find(architecture,"aarch64") then + bits,platform="linux-aarch64" + elseif find(architecture,"armv7l") then + bits,platform=32,"linux-armhf" + elseif find(architecture,"mips64") or find(architecture,"mips64el") then + bits,platform=64,"linux-mipsel" + elseif find(architecture,"mipsel") or find(architecture,"mips") then + bits,platform=32,"linux-mipsel" else - platform="kfreebsd-i386" + bits,platform=64,"linux-64" end - os.setenv("MTX_PLATFORM",platform) - os.platform=platform - return platform end -else - function resolvers.platform(t,k) - local platform="linux" - os.setenv("MTX_PLATFORM",platform) - os.platform=platform - return platform + os.setenv("MTX_PLATFORM",platform) + os.setenv("MTX_BITS",bits) + os.platform=platform + os.bits=bits + os.newline=name=="windows" and "\013\010" or "\010" +end +do + local t={ 8,9,"a","b" } + function os.uuid() + return format("%04x%04x-4%03x-%s%03x-%04x-%04x%04x%04x", + random(0xFFFF),random(0xFFFF), + random(0x0FFF), + t[ceil(random(4))] or 8,random(0x0FFF), + random(0xFFFF), + random(0xFFFF),random(0xFFFF),random(0xFFFF) + ) end end -os.newline=name=="windows" and "\013\010" or "\010" -function resolvers.bits(t,k) - local bits=find(os.platform,"64",1,true) and 64 or 32 - os.bits=bits - return bits -end -local t={ 8,9,"a","b" } -function os.uuid() - return format("%04x%04x-4%03x-%s%03x-%04x-%04x%04x%04x", - random(0xFFFF),random(0xFFFF), - random(0x0FFF), - t[ceil(random(4))] or 8,random(0x0FFF), - random(0xFFFF), - random(0xFFFF),random(0xFFFF),random(0xFFFF) - ) -end -local d -function os.timezone(delta) - d=d or ((tonumber(date("%H")) or 0)-(tonumber(date("!%H")) or 0)) - if delta then - if d>0 then - return format("+%02i:00",d) +do + local hour,min + function os.timezone(difference) + if not hour then + local current=time() + local utcdate=date("!*t",current) + local localdate=date("*t",current) + localdate.isdst=false + local timediff=difftime(time(localdate),time(utcdate)) + hour,min=modf(timediff/3600) + min=min*60 + end + if difference then + return hour,min else - return format("-%02i:00",-d) + return format("%+03d:%02d",hour,min) end - else - return 1 - end -end -local timeformat=format("%%s%s",os.timezone(true)) -local dateformat="!%Y-%m-%d %H:%M:%S" -local lasttime=nil -local lastdate=nil -function os.fulltime(t,default) - t=t and tonumber(t) or 0 - if t>0 then - elseif default then - return default - else - t=time() - end - if t~=lasttime then - lasttime=t - lastdate=format(timeformat,date(dateformat)) - end - return lastdate -end -local dateformat="%Y-%m-%d %H:%M:%S" -local lasttime=nil -local lastdate=nil -function os.localtime(t,default) - t=t and tonumber(t) or 0 - if t>0 then - elseif default then - return default - else - t=time() end - if t~=lasttime then - lasttime=t - lastdate=date(dateformat,t) + local timeformat=format("%%s%s",os.timezone()) + local dateformat="%Y-%m-%d %H:%M:%S" + local lasttime=nil + local lastdate=nil + function os.fulltime(t,default) + t=t and tonumber(t) or 0 + if t>0 then + elseif default then + return default + else + t=time() + end + if t~=lasttime then + lasttime=t + lastdate=format(timeformat,date(dateformat)) + end + return lastdate + end + local dateformat="%Y-%m-%d %H:%M:%S" + local lasttime=nil + local lastdate=nil + function os.localtime(t,default) + t=t and tonumber(t) or 0 + if t>0 then + elseif default then + return default + else + t=time() + end + if t~=lasttime then + lasttime=t + lastdate=date(dateformat,t) + end + return lastdate + end + function os.converttime(t,default) + local t=tonumber(t) + if t and t>0 then + return date(dateformat,t) + else + return default or "-" + end + end + function os.today() + return date("!*t") + end + function os.now() + return date("!%Y-%m-%d %H:%M:%S") end - return lastdate end -function os.converttime(t,default) - local t=tonumber(t) - if t and t>0 then - return date(dateformat,t) - else - return default or "-" - end -end -local memory={} -local function which(filename) - local fullname=memory[filename] - if fullname==nil then - local suffix=file.suffix(filename) - local suffixes=suffix=="" and os.binsuffixes or { suffix } - for directory in gmatch(os.getenv("PATH"),"[^"..io.pathseparator.."]+") do - local df=file.join(directory,filename) - for i=1,#suffixes do - local dfs=file.addsuffix(df,suffixes[i]) - if io.exists(dfs) then - fullname=dfs - break +do + local cache={} + local function which(filename) + local fullname=cache[filename] + if fullname==nil then + local suffix=file.suffix(filename) + local suffixes=suffix=="" and os.binsuffixes or { suffix } + for directory in gmatch(os.getenv("PATH"),"[^"..io.pathseparator.."]+") do + local df=file.join(directory,filename) + for i=1,#suffixes do + local dfs=file.addsuffix(df,suffixes[i]) + if io.exists(dfs) then + fullname=dfs + break + end end end + if not fullname then + fullname=false + end + cache[filename]=fullname end - if not fullname then - fullname=false - end - memory[filename]=fullname + return fullname end - return fullname -end -os.which=which -os.where=which -function os.today() - return date("!*t") -end -function os.now() - return date("!%Y-%m-%d %H:%M:%S") + os.which=which + os.where=which end if not os.sleep then local socket=socket @@ -3553,65 +3549,69 @@ if not os.sleep then socket.sleep(n) end end -local function isleapyear(year) - return (year%4==0) and (year%100~=0 or year%400==0) -end -os.isleapyear=isleapyear -local days={ 31,28,31,30,31,30,31,31,30,31,30,31 } -local function nofdays(year,month,day) - if not month then - return isleapyear(year) and 365 or 364 - elseif not day then - return month==2 and isleapyear(year) and 29 or days[month] - else - for i=1,month-1 do - day=day+days[i] - end - if month>2 and isleapyear(year) then - day=day+1 +do + local function isleapyear(year) + return (year%4==0) and (year%100~=0 or year%400==0) + end + os.isleapyear=isleapyear + local days={ 31,28,31,30,31,30,31,31,30,31,30,31 } + local function nofdays(year,month,day) + if not month then + return isleapyear(year) and 365 or 364 + elseif not day then + return month==2 and isleapyear(year) and 29 or days[month] + else + for i=1,month-1 do + day=day+days[i] + end + if month>2 and isleapyear(year) then + day=day+1 + end + return day end - return day end -end -os.nofdays=nofdays -function os.weekday(day,month,year) - return date("%w",time { year=year,month=month,day=day })+1 -end -function os.validdate(year,month,day) - if month<1 then - month=1 - elseif month>12 then - month=12 + os.nofdays=nofdays + function os.weekday(day,month,year) + return date("%w",time { year=year,month=month,day=day })+1 end - if day<1 then - day=1 - else - local max=nofdays(year,month) - if day>max then - day=max + function os.validdate(year,month,day) + if month<1 then + month=1 + elseif month>12 then + month=12 + end + if day<1 then + day=1 + else + local max=nofdays(year,month) + if day>max then + day=max + end end + return year,month,day end - return year,month,day -end -function os.date(fmt,...) - if not fmt then - fmt="%Y-%m-%d %H:%M" + function os.date(fmt,...) + if not fmt then + fmt="%Y-%m-%d %H:%M" + end + return date(fmt,...) end - return date(fmt,...) end -local osexit=os.exit -local exitcode=nil -function os.setexitcode(code) - exitcode=code -end -function os.exit(c) - if exitcode~=nil then - return osexit(exitcode) +do + local osexit=os.exit + local exitcode=nil + function os.setexitcode(code) + exitcode=code end - if c~=nil then - return osexit(c) + function os.exit(c) + if exitcode~=nil then + return osexit(exitcode) + end + if c~=nil then + return osexit(c) + end + return osexit() end - return osexit() end end -- closure @@ -3872,7 +3872,7 @@ function file.join(one,two,three,...) if not two then return one=="" and one or lpegmatch(reslasher,one) end - if one=="" then + if not one or one=="" then return lpegmatch(stripper,three and concat({ two,three,... },"/") or two) end if lpegmatch(isnetwork,one) then @@ -4031,87 +4031,6 @@ end -- closure do -- begin closure to overcome local limits and interference -if not modules then modules={} end modules ['l-gzip']={ - version=1.001, - author="Hans Hagen, PRAGMA-ADE, Hasselt NL", - copyright="PRAGMA ADE / ConTeXt Development Team", - license="see context related readme files" -} -gzip=gzip or {} -if not zlib then - zlib=xzip -elseif not xzip then - xzip=zlib -end -if zlib then - local suffix=file.suffix - local suffixes=file.suffixes - local find=string.find - local openfile=io.open - local gzipwindow=15+16 - local gziplevel=3 - local identifier="^\x1F\x8B\x08" - local compress=zlib.compress - local decompress=zlib.decompress - function gzip.load(filename) - local f=openfile(filename,"rb") - if not f then - else - local data=f:read("*all") - f:close() - if data and data~="" then - if suffix(filename)=="gz" then - data=decompress(data,gzipwindow) - end - return data - end - end - end - function gzip.save(filename,data,level) - if suffix(filename)~="gz" then - filename=filename..".gz" - end - local f=openfile(filename,"wb") - if f then - data=compress(data or "",level or gziplevel,nil,gzipwindow) - f:write(data) - f:close() - return #data - end - end - function gzip.suffix(filename) - local suffix,extra=suffixes(filename) - local gzipped=extra=="gz" - return suffix,gzipped - end - function gzip.compressed(s) - return s and find(s,identifier) - end - function gzip.compress(s,level) - if s and not find(s,identifier) then - if not level then - level=gziplevel - elseif level<=0 then - return s - elseif level>9 then - level=9 - end - return compress(s,level or gziplevel,nil,gzipwindow) or s - end - end - function gzip.decompress(s) - if s and find(s,identifier) then - return decompress(s,gzipwindow) - else - return s - end - end -end - -end -- closure - -do -- begin closure to overcome local limits and interference - if not modules then modules={} end modules ['l-md5']={ version=1.001, author="Hans Hagen, PRAGMA-ADE, Hasselt NL", @@ -4389,15 +4308,15 @@ local separator,pattern if onwindows then local slash=S("/\\")/"/" pattern={ - [1]=(Cs(P(".")+slash^1)+Cs(R("az","AZ")*P(":")*slash^0)+Cc("./"))*V(2)*V(3), - [2]=Cs(((1-S("*?/\\"))^0*slash)^0), - [3]=Cs(P(1)^0) + (Cs(P(".")+slash^1)+Cs(R("az","AZ")*P(":")*slash^0)+Cc("./"))*V(2)*V(3), + Cs(((1-S("*?/\\"))^0*slash)^0), + Cs(P(1)^0) } else pattern={ - [1]=(C(P(".")+P("/")^1)+Cc("./"))*V(2)*V(3), - [2]=C(((1-S("*?/"))^0*P("/"))^0), - [3]=C(P(1)^0) + (C(P(".")+P("/")^1)+Cc("./"))*V(2)*V(3), + C(((1-S("*?/"))^0*P("/"))^0), + C(P(1)^0) } end local filter=Cs (( |