| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
if not modules then modules = { } end modules ['mtxrun'] = { |
| 4 |
version = 1.001, |
| 5 |
comment = "runner, lua replacement for texmfstart.rb", |
| 6 |
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 7 |
copyright = "PRAGMA ADE / ConTeXt Development Team", |
| 8 |
license = "see context related readme files" |
| 9 |
} |
| 10 |
|
| 11 |
-- one can make a stub: |
| 12 |
|
| 13 |
-- mtxrun : |
| 14 |
-- |
| 15 |
-- #!/bin/sh |
| 16 |
-- env LUATEXDIR=/....../texmf/scripts/context/lua luatex --luaonly mtxrun.lua "$@" |
| 17 |
|
| 18 |
-- mtxrun.cmd : |
| 19 |
-- |
| 20 |
-- @luatex --luaonly %~d0%~p0mtxrun.lua %* |
| 21 |
|
| 22 |
-- filename : mtxrun.lua |
| 23 |
-- comment : companion to context.tex |
| 24 |
-- author : Hans Hagen, PRAGMA-ADE, Hasselt NL |
| 25 |
-- copyright: PRAGMA ADE / ConTeXt Development Team |
| 26 |
-- license : see context related readme files |
| 27 |
|
| 28 |
-- This script is based on texmfstart.rb but does not use kpsewhich to locate files. |
| 29 |
-- Although kpse is a library it never came to opening up its interface to other |
| 30 |
-- programs (esp scripting languages) and so we do it ourselves. The lua variant |
| 31 |
-- evolved out of an experimental ruby one. Interesting is that using a scripting |
| 32 |
-- language instead of c does not have a speed penalty. Actually the lua variant is |
| 33 |
-- more efficient, especially when multiple calls to kpsewhich are involved. The lua |
| 34 |
-- library also gives way more control. |
| 35 |
|
| 36 |
-- When libraries used here are updates you can run |
| 37 |
-- |
| 38 |
-- mtxrun --selfmerge |
| 39 |
-- |
| 40 |
-- to update the embedded code. After that you might need to run |
| 41 |
-- |
| 42 |
-- mtxrun --selfupdate |
| 43 |
-- |
| 44 |
-- to copy the new script (from scripts/context/lua) to location where |
| 45 |
-- binaries are expected. If you want to remove the embedded code you can run |
| 46 |
-- |
| 47 |
-- mtxxun --selfclean |
| 48 |
|
| 49 |
-- to be done / considered |
| 50 |
-- |
| 51 |
-- support for --exec or make it default |
| 52 |
-- support for jar files (or maybe not, never used, too messy) |
| 53 |
-- support for $RUBYINPUTS cum suis (if still needed) |
| 54 |
-- remember for subruns: _CTX_K_V_#{original}_ |
| 55 |
-- remember for subruns: _CTX_K_S_#{original}_ |
| 56 |
-- remember for subruns: TEXMFSTART.#{original} [tex.rb texmfstart.rb] |
| 57 |
|
| 58 |
-- begin library merge |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
do -- create closure to overcome 200 locals limit |
| 63 |
|
| 64 |
package.loaded["l-bit32"] = package.loaded["l-bit32"] or true |
| 65 |
|
| 66 |
-- original size: 3607, stripped down to: 3009 |
| 67 |
|
| 68 |
if not modules then modules={} end modules ['l-bit32']={ |
| 69 |
version=1.001, |
| 70 |
license="the same as regular Lua", |
| 71 |
source="bitwise.lua, v 1.24 2014/12/26 17:20:53 roberto", |
| 72 |
comment="drop-in for bit32, adapted a bit by Hans Hagen", |
| 73 |
} |
| 74 |
if bit32 then |
| 75 |
elseif utf8 then |
| 76 |
load ([[ |
| 77 |
local select = select -- instead of: arg = { ... } |
| 78 |
bit32 = { |
| 79 |
bnot = function (a) |
| 80 |
return ~a & 0xFFFFFFFF |
| 81 |
end, |
| 82 |
band = function (x, y, z, ...) |
| 83 |
if not z then |
| 84 |
return ((x or -1) & (y or -1)) & 0xFFFFFFFF |
| 85 |
else |
| 86 |
local res = x & y & z |
| 87 |
for i=1,select("#",...) do |
| 88 |
res = res & select(i,...) |
| 89 |
end |
| 90 |
return res & 0xFFFFFFFF |
| 91 |
end |
| 92 |
end, |
| 93 |
bor = function (x, y, z, ...) |
| 94 |
if not z then |
| 95 |
return ((x or 0) | (y or 0)) & 0xFFFFFFFF |
| 96 |
else |
| 97 |
local res = x | y | z |
| 98 |
for i=1,select("#",...) do |
| 99 |
res = res | select(i,...) |
| 100 |
end |
| 101 |
return res & 0xFFFFFFFF |
| 102 |
end |
| 103 |
end, |
| 104 |
bxor = function (x, y, z, ...) |
| 105 |
if not z then |
| 106 |
return ((x or 0) ~ (y or 0)) & 0xFFFFFFFF |
| 107 |
else |
| 108 |
local res = x ~ y ~ z |
| 109 |
for i=1,select("#",...) do |
| 110 |
res = res ~ select(i,...) |
| 111 |
end |
| 112 |
return res & 0xFFFFFFFF |
| 113 |
end |
| 114 |
end, |
| 115 |
btest = function (x, y, z, ...) |
| 116 |
if not z then |
| 117 |
return (((x or -1) & (y or -1)) & 0xFFFFFFFF) ~= 0 |
| 118 |
else |
| 119 |
local res = x & y & z |
| 120 |
for i=1,select("#",...) do |
| 121 |
res = res & select(i,...) |
| 122 |
end |
| 123 |
return (res & 0xFFFFFFFF) ~= 0 |
| 124 |
end |
| 125 |
end, |
| 126 |
lshift = function (a, b) |
| 127 |
return ((a & 0xFFFFFFFF) << b) & 0xFFFFFFFF |
| 128 |
end, |
| 129 |
rshift = function (a, b) |
| 130 |
return ((a & 0xFFFFFFFF) >> b) & 0xFFFFFFFF |
| 131 |
end, |
| 132 |
arshift = function (a, b) |
| 133 |
a = a & 0xFFFFFFFF |
| 134 |
if b <= 0 or (a & 0x80000000) == 0 then |
| 135 |
return (a >> b) & 0xFFFFFFFF |
| 136 |
else |
| 137 |
return ((a >> b) | ~(0xFFFFFFFF >> b)) & 0xFFFFFFFF |
| 138 |
end |
| 139 |
end, |
| 140 |
lrotate = function (a ,b) |
| 141 |
b = b & 31 |
| 142 |
a = a & 0xFFFFFFFF |
| 143 |
a = (a << b) | (a >> (32 - b)) |
| 144 |
return a & 0xFFFFFFFF |
| 145 |
end, |
| 146 |
rrotate = function (a, b) |
| 147 |
b = -b & 31 |
| 148 |
a = a & 0xFFFFFFFF |
| 149 |
a = (a << b) | (a >> (32 - b)) |
| 150 |
return a & 0xFFFFFFFF |
| 151 |
end, |
| 152 |
extract = function (a, f, w) |
| 153 |
return (a >> f) & ~(-1 << (w or 1)) |
| 154 |
end, |
| 155 |
replace = function (a, v, f, w) |
| 156 |
local mask = ~(-1 << (w or 1)) |
| 157 |
return ((a & ~(mask << f)) | ((v & mask) << f)) & 0xFFFFFFFF |
| 158 |
end, |
| 159 |
} |
| 160 |
]] ) () |
| 161 |
elseif bit then |
| 162 |
load ([[ |
| 163 |
local band, bnot, rshift, lshift = bit.band, bit.bnot, bit.rshift, bit.lshift |
| 164 |
bit32 = { |
| 165 |
arshift = bit.arshift, |
| 166 |
band = band, |
| 167 |
bnot = bnot, |
| 168 |
bor = bit.bor, |
| 169 |
bxor = bit.bxor, |
| 170 |
btest = function(...) |
| 171 |
return band(...) ~= 0 |
| 172 |
end, |
| 173 |
extract = function(a,f,w) |
| 174 |
return band(rshift(a,f),2^(w or 1)-1) |
| 175 |
end, |
| 176 |
lrotate = bit.rol, |
| 177 |
lshift = lshift, |
| 178 |
replace = function(a,v,f,w) |
| 179 |
local mask = 2^(w or 1)-1 |
| 180 |
return band(a,bnot(lshift(mask,f)))+lshift(band(v,mask),f) |
| 181 |
end, |
| 182 |
rrotate = bit.ror, |
| 183 |
rshift = rshift, |
| 184 |
} |
| 185 |
]] ) () |
| 186 |
else |
| 187 |
xpcall(function() local _,t=require("bit32") if t then bit32=t end return end,function() end) |
| 188 |
end |
| 189 |
|
| 190 |
|
| 191 |
end -- of closure |
| 192 |
|
| 193 |
do -- create closure to overcome 200 locals limit |
| 194 |
|
| 195 |
package.loaded["l-lua"] = package.loaded["l-lua"] or true |
| 196 |
|
| 197 |
-- original size: 6546, stripped down to: 2909 |
| 198 |
|
| 199 |
if not modules then modules={} end modules ['l-lua']={ |
| 200 |
version=1.001, |
| 201 |
comment="companion to luat-lib.mkiv", |
| 202 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 203 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 204 |
license="see context related readme files" |
| 205 |
} |
| 206 |
local next,type,tonumber=next,type,tonumber |
| 207 |
LUAMAJORVERSION,LUAMINORVERSION=string.match(_VERSION,"^[^%d]+(%d+)%.(%d+).*$") |
| 208 |
LUAMAJORVERSION=tonumber(LUAMAJORVERSION) or 5 |
| 209 |
LUAMINORVERSION=tonumber(LUAMINORVERSION) or 1 |
| 210 |
LUAVERSION=LUAMAJORVERSION+LUAMINORVERSION/10 |
| 211 |
LUAFORMAT=status and status.lua_format or 0 |
| 212 |
if LUAVERSION<5.2 and jit then |
| 213 |
MINORVERSION=2 |
| 214 |
LUAVERSION=5.2 |
| 215 |
end |
| 216 |
if not lpeg then |
| 217 |
lpeg=require("lpeg") |
| 218 |
end |
| 219 |
if loadstring then |
| 220 |
local loadnormal=load |
| 221 |
function load(first,...) |
| 222 |
if type(first)=="string" then |
| 223 |
return loadstring(first,...) |
| 224 |
else |
| 225 |
return loadnormal(first,...) |
| 226 |
end |
| 227 |
end |
| 228 |
else |
| 229 |
loadstring=load |
| 230 |
end |
| 231 |
if not ipairs then |
| 232 |
local function iterate(a,i) |
| 233 |
i=i+1 |
| 234 |
local v=a[i] |
| 235 |
if v~=nil then |
| 236 |
return i,v |
| 237 |
end |
| 238 |
end |
| 239 |
function ipairs(a) |
| 240 |
return iterate,a,0 |
| 241 |
end |
| 242 |
end |
| 243 |
if not pairs then |
| 244 |
function pairs(t) |
| 245 |
return next,t |
| 246 |
end |
| 247 |
end |
| 248 |
if not table.unpack then |
| 249 |
table.unpack=_G.unpack |
| 250 |
elseif not unpack then |
| 251 |
_G.unpack=table.unpack |
| 252 |
end |
| 253 |
if not package.loaders then |
| 254 |
package.loaders=package.searchers |
| 255 |
end |
| 256 |
local print,select,tostring=print,select,tostring |
| 257 |
local inspectors={} |
| 258 |
function setinspector(kind,inspector) |
| 259 |
inspectors[kind]=inspector |
| 260 |
end |
| 261 |
function inspect(...) |
| 262 |
for s=1,select("#",...) do |
| 263 |
local value=select(s,...) |
| 264 |
if value==nil then |
| 265 |
print("nil") |
| 266 |
else |
| 267 |
local done=false |
| 268 |
local kind=type(value) |
| 269 |
local inspector=inspectors[kind] |
| 270 |
if inspector then |
| 271 |
done=inspector(value) |
| 272 |
if done then |
| 273 |
break |
| 274 |
end |
| 275 |
end |
| 276 |
for kind,inspector in next,inspectors do |
| 277 |
done=inspector(value) |
| 278 |
if done then |
| 279 |
break |
| 280 |
end |
| 281 |
end |
| 282 |
if not done then |
| 283 |
print(tostring(value)) |
| 284 |
end |
| 285 |
end |
| 286 |
end |
| 287 |
end |
| 288 |
local dummy=function() end |
| 289 |
function optionalrequire(...) |
| 290 |
local ok,result=xpcall(require,dummy,...) |
| 291 |
if ok then |
| 292 |
return result |
| 293 |
end |
| 294 |
end |
| 295 |
local flush=io.flush |
| 296 |
if flush then |
| 297 |
local execute=os.execute if execute then function os.execute(...) flush() return execute(...) end end |
| 298 |
local exec=os.exec if exec then function os.exec (...) flush() return exec (...) end end |
| 299 |
local spawn=os.spawn if spawn then function os.spawn (...) flush() return spawn (...) end end |
| 300 |
local popen=io.popen if popen then function io.popen (...) flush() return popen (...) end end |
| 301 |
end |
| 302 |
FFISUPPORTED=type(ffi)=="table" and ffi.os~="" and ffi.arch~="" and ffi.load |
| 303 |
if not FFISUPPORTED then |
| 304 |
local okay;okay,ffi=pcall(require,"ffi") |
| 305 |
FFISUPPORTED=type(ffi)=="table" and ffi.os~="" and ffi.arch~="" and ffi.load |
| 306 |
end |
| 307 |
if not FFISUPPORTED then |
| 308 |
ffi=nil |
| 309 |
elseif not ffi.number then |
| 310 |
ffi.number=tonumber |
| 311 |
end |
| 312 |
if LUAVERSION>5.3 then |
| 313 |
end |
| 314 |
if status and os.setenv then |
| 315 |
os.setenv("engine",string.lower(status.luatex_engine or "unknown")) |
| 316 |
end |
| 317 |
|
| 318 |
|
| 319 |
end -- of closure |
| 320 |
|
| 321 |
do -- create closure to overcome 200 locals limit |
| 322 |
|
| 323 |
package.loaded["l-macro"] = package.loaded["l-macro"] or true |
| 324 |
|
| 325 |
-- original size: 10130, stripped down to: 5990 |
| 326 |
|
| 327 |
if not modules then modules={} end modules ['l-macros']={ |
| 328 |
version=1.001, |
| 329 |
comment="companion to luat-lib.mkiv", |
| 330 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 331 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 332 |
license="see context related readme files" |
| 333 |
} |
| 334 |
local S,P,R,V,C,Cs,Cc,Ct,Carg=lpeg.S,lpeg.P,lpeg.R,lpeg.V,lpeg.C,lpeg.Cs,lpeg.Cc,lpeg.Ct,lpeg.Carg |
| 335 |
local lpegmatch=lpeg.match |
| 336 |
local concat=table.concat |
| 337 |
local format,sub,match=string.format,string.sub,string.match |
| 338 |
local next,load,type=next,load,type |
| 339 |
local newline=S("\n\r")^1 |
| 340 |
local continue=P("\\")*newline |
| 341 |
local whitespace=S(" \t\n\r") |
| 342 |
local spaces=S(" \t")+continue |
| 343 |
local nametoken=R("az","AZ","__","09") |
| 344 |
local name=nametoken^1 |
| 345 |
local body=((continue/""+1)-newline)^1 |
| 346 |
local lparent=P("(") |
| 347 |
local rparent=P(")") |
| 348 |
local noparent=1-(lparent+rparent) |
| 349 |
local nested=P { lparent*(noparent+V(1))^0*rparent } |
| 350 |
local escaped=P("\\")*P(1) |
| 351 |
local squote=P("'") |
| 352 |
local dquote=P('"') |
| 353 |
local quoted=dquote*(escaped+(1-dquote))^0*dquote+squote*(escaped+(1-squote))^0*squote |
| 354 |
local arguments=lparent*Ct((Cs((nested+(quoted+1-S("),")))^1)+S(", "))^0)*rparent |
| 355 |
local macros=lua.macros or {} |
| 356 |
lua.macros=macros |
| 357 |
local patterns={} |
| 358 |
local definitions={} |
| 359 |
local resolve |
| 360 |
local subparser |
| 361 |
local report_lua=function(...) |
| 362 |
if logs and logs.reporter then |
| 363 |
report_lua=logs.reporter("system","lua") |
| 364 |
report_lua(...) |
| 365 |
else |
| 366 |
print(format(...)) |
| 367 |
end |
| 368 |
end |
| 369 |
local safeguard=P("local")*whitespace^1*name*(whitespace+P("=")) |
| 370 |
resolve=safeguard+C(C(name)*(arguments^-1))/function(raw,s,a) |
| 371 |
local d=definitions[s] |
| 372 |
if d then |
| 373 |
if a then |
| 374 |
local n=#a |
| 375 |
local p=patterns[s][n] |
| 376 |
if p then |
| 377 |
local d=d[n] |
| 378 |
for i=1,n do |
| 379 |
a[i]=lpegmatch(subparser,a[i]) or a[i] |
| 380 |
end |
| 381 |
return lpegmatch(p,d,1,a) or d |
| 382 |
else |
| 383 |
return raw |
| 384 |
end |
| 385 |
else |
| 386 |
return d[0] or raw |
| 387 |
end |
| 388 |
elseif a then |
| 389 |
for i=1,#a do |
| 390 |
a[i]=lpegmatch(subparser,a[i]) or a[i] |
| 391 |
end |
| 392 |
return s.."("..concat(a,",")..")" |
| 393 |
else |
| 394 |
return raw |
| 395 |
end |
| 396 |
end |
| 397 |
subparser=Cs((resolve+P(1))^1) |
| 398 |
local enddefine=P("#enddefine")/"" |
| 399 |
local beginregister=(C(name)*(arguments+Cc(false))*C((1-enddefine)^1)*enddefine)/function(k,a,v) |
| 400 |
local n=0 |
| 401 |
if a then |
| 402 |
n=#a |
| 403 |
local pattern=P(false) |
| 404 |
for i=1,n do |
| 405 |
pattern=pattern+(P(a[i])*Carg(1))/function(t) return t[i] end |
| 406 |
end |
| 407 |
pattern=Cs((pattern+P(1))^1) |
| 408 |
local p=patterns[k] |
| 409 |
if not p then |
| 410 |
p={ [0]=false,false,false,false,false,false,false,false,false } |
| 411 |
patterns[k]=p |
| 412 |
end |
| 413 |
p[n]=pattern |
| 414 |
end |
| 415 |
local d=definitions[k] |
| 416 |
if not d then |
| 417 |
d={ a=a,[0]=false,false,false,false,false,false,false,false,false } |
| 418 |
definitions[k]=d |
| 419 |
end |
| 420 |
d[n]=lpegmatch(subparser,v) or v |
| 421 |
return "" |
| 422 |
end |
| 423 |
local register=(Cs(name)*(arguments+Cc(false))*spaces^0*Cs(body))/function(k,a,v) |
| 424 |
local n=0 |
| 425 |
if a then |
| 426 |
n=#a |
| 427 |
local pattern=P(false) |
| 428 |
for i=1,n do |
| 429 |
pattern=pattern+(P(a[i])*Carg(1))/function(t) return t[i] end |
| 430 |
end |
| 431 |
pattern=Cs((pattern+P(1))^1) |
| 432 |
local p=patterns[k] |
| 433 |
if not p then |
| 434 |
p={ [0]=false,false,false,false,false,false,false,false,false } |
| 435 |
patterns[k]=p |
| 436 |
end |
| 437 |
p[n]=pattern |
| 438 |
end |
| 439 |
local d=definitions[k] |
| 440 |
if not d then |
| 441 |
d={ a=a,[0]=false,false,false,false,false,false,false,false,false } |
| 442 |
definitions[k]=d |
| 443 |
end |
| 444 |
d[n]=lpegmatch(subparser,v) or v |
| 445 |
return "" |
| 446 |
end |
| 447 |
local unregister=(C(name)*spaces^0*(arguments+Cc(false)))/function(k,a) |
| 448 |
local n=0 |
| 449 |
if a then |
| 450 |
n=#a |
| 451 |
local p=patterns[k] |
| 452 |
if p then |
| 453 |
p[n]=false |
| 454 |
end |
| 455 |
end |
| 456 |
local d=definitions[k] |
| 457 |
if d then |
| 458 |
d[n]=false |
| 459 |
end |
| 460 |
return "" |
| 461 |
end |
| 462 |
local begindefine=(P("begindefine")*spaces^0/"")*beginregister |
| 463 |
local define=(P("define" )*spaces^0/"")*register |
| 464 |
local undefine=(P("undefine" )*spaces^0/"")*unregister |
| 465 |
local parser=Cs((((P("#")/"")*(define+begindefine+undefine)*(newline^0/"") )+resolve+P(1) )^0 ) |
| 466 |
function macros.reset() |
| 467 |
definitions={} |
| 468 |
patterns={} |
| 469 |
end |
| 470 |
function macros.showdefinitions() |
| 471 |
for name,list in table.sortedhash(definitions) do |
| 472 |
local arguments=list.a |
| 473 |
if arguments then |
| 474 |
arguments="("..concat(arguments,",")..")" |
| 475 |
else |
| 476 |
arguments="" |
| 477 |
end |
| 478 |
print("macro: "..name..arguments) |
| 479 |
for i=0,#list do |
| 480 |
local l=list[i] |
| 481 |
if l then |
| 482 |
print(" "..l) |
| 483 |
end |
| 484 |
end |
| 485 |
end |
| 486 |
end |
| 487 |
function macros.resolvestring(str) |
| 488 |
return lpegmatch(parser,str) or str |
| 489 |
end |
| 490 |
function macros.resolving() |
| 491 |
return next(patterns) |
| 492 |
end |
| 493 |
local function reload(path,name,data) |
| 494 |
local only=match(name,".-([^/]+)%.lua") |
| 495 |
if only and only~="" then |
| 496 |
local name=path.."/"..only |
| 497 |
local f=io.open(name,"wb") |
| 498 |
f:write(data) |
| 499 |
f:close() |
| 500 |
local f=loadfile(name) |
| 501 |
os.remove(name) |
| 502 |
return f |
| 503 |
end |
| 504 |
end |
| 505 |
local function reload(path,name,data) |
| 506 |
if path and path~="" then |
| 507 |
local only=string.match(name,".-([^/]+)%.lua") |
| 508 |
if only and only~="" then |
| 509 |
local name=path.."/"..only.."-macro.lua" |
| 510 |
local f=io.open(name,"wb") |
| 511 |
if f then |
| 512 |
f:write(data) |
| 513 |
f:close() |
| 514 |
local l=loadfile(name) |
| 515 |
os.remove(name) |
| 516 |
return l |
| 517 |
end |
| 518 |
end |
| 519 |
end |
| 520 |
return load(data,name) |
| 521 |
end |
| 522 |
local function loaded(name,trace,detail) |
| 523 |
local f=io.open(name,"rb") |
| 524 |
if not f then |
| 525 |
return false,format("file '%s' not found",name) |
| 526 |
end |
| 527 |
local c=f:read("*a") |
| 528 |
if not c then |
| 529 |
return false,format("file '%s' is invalid",name) |
| 530 |
end |
| 531 |
f:close() |
| 532 |
local n=lpegmatch(parser,c) |
| 533 |
if trace then |
| 534 |
if #n~=#c then |
| 535 |
report_lua("macros expanded in '%s' (%i => %i bytes)",name,#c,#n) |
| 536 |
if detail then |
| 537 |
report_lua() |
| 538 |
report_lua(n) |
| 539 |
report_lua() |
| 540 |
end |
| 541 |
elseif detail then |
| 542 |
report_lua("no macros expanded in '%s'",name) |
| 543 |
end |
| 544 |
end |
| 545 |
return reload(lfs and lfs.currentdir(),name,n) |
| 546 |
end |
| 547 |
macros.loaded=loaded |
| 548 |
function required(name,trace) |
| 549 |
local filename=file.addsuffix(name,"lua") |
| 550 |
local fullname=resolvers and resolvers.findfile(filename) or filename |
| 551 |
if not fullname or fullname=="" then |
| 552 |
return false |
| 553 |
end |
| 554 |
local codeblob=package.loaded[fullname] |
| 555 |
if codeblob then |
| 556 |
return codeblob |
| 557 |
end |
| 558 |
local code,message=loaded(fullname,macros,trace,trace) |
| 559 |
if type(code)=="function" then |
| 560 |
code=code() |
| 561 |
else |
| 562 |
report_lua("error when loading '%s'",fullname) |
| 563 |
return false,message |
| 564 |
end |
| 565 |
if code==nil then |
| 566 |
code=false |
| 567 |
end |
| 568 |
package.loaded[fullname]=code |
| 569 |
return code |
| 570 |
end |
| 571 |
macros.required=required |
| 572 |
|
| 573 |
|
| 574 |
end -- of closure |
| 575 |
|
| 576 |
do -- create closure to overcome 200 locals limit |
| 577 |
|
| 578 |
package.loaded["l-sandbox"] = package.loaded["l-sandbox"] or true |
| 579 |
|
| 580 |
-- original size: 9700, stripped down to: 6452 |
| 581 |
|
| 582 |
if not modules then modules={} end modules ['l-sandbox']={ |
| 583 |
version=1.001, |
| 584 |
comment="companion to luat-lib.mkiv", |
| 585 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 586 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 587 |
license="see context related readme files" |
| 588 |
} |
| 589 |
local global=_G |
| 590 |
local next=next |
| 591 |
local unpack=unpack or table.unpack |
| 592 |
local type=type |
| 593 |
local tprint=texio and texio.write_nl or print |
| 594 |
local tostring=tostring |
| 595 |
local format=string.format |
| 596 |
local concat=table.concat |
| 597 |
local sort=table.sort |
| 598 |
local gmatch=string.gmatch |
| 599 |
local gsub=string.gsub |
| 600 |
local requiem=require |
| 601 |
sandbox={} |
| 602 |
local sandboxed=false |
| 603 |
local overloads={} |
| 604 |
local skiploads={} |
| 605 |
local initializers={} |
| 606 |
local finalizers={} |
| 607 |
local originals={} |
| 608 |
local comments={} |
| 609 |
local trace=false |
| 610 |
local logger=false |
| 611 |
local blocked={} |
| 612 |
local function report(...) |
| 613 |
tprint("sandbox ! "..format(...)) |
| 614 |
end |
| 615 |
sandbox.report=report |
| 616 |
function sandbox.setreporter(r) |
| 617 |
report=r |
| 618 |
sandbox.report=r |
| 619 |
end |
| 620 |
function sandbox.settrace(v) |
| 621 |
trace=v |
| 622 |
end |
| 623 |
function sandbox.setlogger(l) |
| 624 |
logger=type(l)=="function" and l or false |
| 625 |
end |
| 626 |
local function register(func,overload,comment) |
| 627 |
if type(func)=="function" then |
| 628 |
if type(overload)=="string" then |
| 629 |
comment=overload |
| 630 |
overload=nil |
| 631 |
end |
| 632 |
local function f(...) |
| 633 |
if sandboxed then |
| 634 |
local overload=overloads[f] |
| 635 |
if overload then |
| 636 |
if logger then |
| 637 |
local result={ overload(func,...) } |
| 638 |
logger { |
| 639 |
comment=comments[f] or tostring(f), |
| 640 |
arguments={... }, |
| 641 |
result=result[1] and true or false, |
| 642 |
} |
| 643 |
return unpack(result) |
| 644 |
else |
| 645 |
return overload(func,...) |
| 646 |
end |
| 647 |
else |
| 648 |
end |
| 649 |
else |
| 650 |
return func(...) |
| 651 |
end |
| 652 |
end |
| 653 |
if comment then |
| 654 |
comments[f]=comment |
| 655 |
if trace then |
| 656 |
report("registering function: %s",comment) |
| 657 |
end |
| 658 |
end |
| 659 |
overloads[f]=overload or false |
| 660 |
originals[f]=func |
| 661 |
return f |
| 662 |
end |
| 663 |
end |
| 664 |
local function redefine(func,comment) |
| 665 |
if type(func)=="function" then |
| 666 |
skiploads[func]=comment or comments[func] or "unknown" |
| 667 |
if overloads[func]==false then |
| 668 |
overloads[func]=nil |
| 669 |
end |
| 670 |
end |
| 671 |
end |
| 672 |
sandbox.register=register |
| 673 |
sandbox.redefine=redefine |
| 674 |
function sandbox.original(func) |
| 675 |
return originals and originals[func] or func |
| 676 |
end |
| 677 |
function sandbox.overload(func,overload,comment) |
| 678 |
comment=comment or comments[func] or "?" |
| 679 |
if type(func)~="function" then |
| 680 |
if trace then |
| 681 |
report("overloading unknown function: %s",comment) |
| 682 |
end |
| 683 |
elseif type(overload)~="function" then |
| 684 |
if trace then |
| 685 |
report("overloading function with bad overload: %s",comment) |
| 686 |
end |
| 687 |
elseif overloads[func]==nil then |
| 688 |
if trace then |
| 689 |
report("function is not registered: %s",comment) |
| 690 |
end |
| 691 |
elseif skiploads[func] then |
| 692 |
if trace then |
| 693 |
report("function is not skipped: %s",comment) |
| 694 |
end |
| 695 |
else |
| 696 |
if trace then |
| 697 |
report("overloading function: %s",comment) |
| 698 |
end |
| 699 |
overloads[func]=overload |
| 700 |
end |
| 701 |
return func |
| 702 |
end |
| 703 |
local function whatever(specification,what,target) |
| 704 |
if type(specification)~="table" then |
| 705 |
report("%s needs a specification",what) |
| 706 |
elseif type(specification.category)~="string" or type(specification.action)~="function" then |
| 707 |
report("%s needs a category and action",what) |
| 708 |
elseif not sandboxed then |
| 709 |
target[#target+1]=specification |
| 710 |
elseif trace then |
| 711 |
report("already enabled, discarding %s",what) |
| 712 |
end |
| 713 |
end |
| 714 |
function sandbox.initializer(specification) |
| 715 |
whatever(specification,"initializer",initializers) |
| 716 |
end |
| 717 |
function sandbox.finalizer(specification) |
| 718 |
whatever(specification,"finalizer",finalizers) |
| 719 |
end |
| 720 |
function require(name) |
| 721 |
local n=gsub(name,"^.*[\\/]","") |
| 722 |
local n=gsub(n,"[%.].*$","") |
| 723 |
local b=blocked[n] |
| 724 |
if b==false then |
| 725 |
return nil |
| 726 |
elseif b then |
| 727 |
if trace then |
| 728 |
report("using blocked: %s",n) |
| 729 |
end |
| 730 |
return b |
| 731 |
else |
| 732 |
if trace then |
| 733 |
report("requiring: %s",name) |
| 734 |
end |
| 735 |
return requiem(name) |
| 736 |
end |
| 737 |
end |
| 738 |
function sandbox.enable() |
| 739 |
if not sandboxed then |
| 740 |
debug={ |
| 741 |
traceback=debug.traceback, |
| 742 |
} |
| 743 |
for i=1,#initializers do |
| 744 |
initializers[i].action() |
| 745 |
end |
| 746 |
for i=1,#finalizers do |
| 747 |
finalizers[i].action() |
| 748 |
end |
| 749 |
local nnot=0 |
| 750 |
local nyes=0 |
| 751 |
local cnot={} |
| 752 |
local cyes={} |
| 753 |
local skip={} |
| 754 |
for k,v in next,overloads do |
| 755 |
local c=comments[k] |
| 756 |
if v then |
| 757 |
if c then |
| 758 |
cyes[#cyes+1]=c |
| 759 |
else |
| 760 |
nyes=nyes+1 |
| 761 |
end |
| 762 |
else |
| 763 |
if c then |
| 764 |
cnot[#cnot+1]=c |
| 765 |
else |
| 766 |
nnot=nnot+1 |
| 767 |
end |
| 768 |
end |
| 769 |
end |
| 770 |
for k,v in next,skiploads do |
| 771 |
skip[#skip+1]=v |
| 772 |
end |
| 773 |
if #cyes>0 then |
| 774 |
sort(cyes) |
| 775 |
report("overloaded known: %s",concat(cyes," | ")) |
| 776 |
end |
| 777 |
if nyes>0 then |
| 778 |
report("overloaded unknown: %s",nyes) |
| 779 |
end |
| 780 |
if #cnot>0 then |
| 781 |
sort(cnot) |
| 782 |
report("not overloaded known: %s",concat(cnot," | ")) |
| 783 |
end |
| 784 |
if nnot>0 then |
| 785 |
report("not overloaded unknown: %s",nnot) |
| 786 |
end |
| 787 |
if #skip>0 then |
| 788 |
sort(skip) |
| 789 |
report("not overloaded redefined: %s",concat(skip," | ")) |
| 790 |
end |
| 791 |
initializers=nil |
| 792 |
finalizers=nil |
| 793 |
originals=nil |
| 794 |
sandboxed=true |
| 795 |
end |
| 796 |
end |
| 797 |
do |
| 798 |
local function blockrequire(name,lib) |
| 799 |
if trace then |
| 800 |
report("preventing reload of: %s",name) |
| 801 |
end |
| 802 |
blocked[name]=lib or _G[name] or false |
| 803 |
end |
| 804 |
blockrequire("lfs",lfs) |
| 805 |
blockrequire("io",io) |
| 806 |
blockrequire("os",os) |
| 807 |
blockrequire("ffi",ffi) |
| 808 |
sandbox.blockrequire=blockrequire |
| 809 |
end |
| 810 |
local function supported(library) |
| 811 |
local l=_G[library] |
| 812 |
return l |
| 813 |
end |
| 814 |
loadfile=register(loadfile,"loadfile") |
| 815 |
if supported("lua") then |
| 816 |
lua.openfile=register(lua.openfile,"lua.openfile") |
| 817 |
end |
| 818 |
if supported("io") then |
| 819 |
io.open=register(io.open,"io.open") |
| 820 |
io.popen=register(io.popen,"io.popen") |
| 821 |
io.lines=register(io.lines,"io.lines") |
| 822 |
io.output=register(io.output,"io.output") |
| 823 |
io.input=register(io.input,"io.input") |
| 824 |
end |
| 825 |
if supported("os") then |
| 826 |
os.execute=register(os.execute,"os.execute") |
| 827 |
os.spawn=register(os.spawn,"os.spawn") |
| 828 |
os.exec=register(os.exec,"os.exec") |
| 829 |
os.rename=register(os.rename,"os.rename") |
| 830 |
os.remove=register(os.remove,"os.remove") |
| 831 |
end |
| 832 |
if supported("lfs") then |
| 833 |
lfs.chdir=register(lfs.chdir,"lfs.chdir") |
| 834 |
lfs.mkdir=register(lfs.mkdir,"lfs.mkdir") |
| 835 |
lfs.rmdir=register(lfs.rmdir,"lfs.rmdir") |
| 836 |
lfs.isfile=register(lfs.isfile,"lfs.isfile") |
| 837 |
lfs.isdir=register(lfs.isdir,"lfs.isdir") |
| 838 |
lfs.attributes=register(lfs.attributes,"lfs.attributes") |
| 839 |
lfs.dir=register(lfs.dir,"lfs.dir") |
| 840 |
lfs.lock_dir=register(lfs.lock_dir,"lfs.lock_dir") |
| 841 |
lfs.touch=register(lfs.touch,"lfs.touch") |
| 842 |
lfs.link=register(lfs.link,"lfs.link") |
| 843 |
lfs.setmode=register(lfs.setmode,"lfs.setmode") |
| 844 |
lfs.readlink=register(lfs.readlink,"lfs.readlink") |
| 845 |
lfs.shortname=register(lfs.shortname,"lfs.shortname") |
| 846 |
lfs.symlinkattributes=register(lfs.symlinkattributes,"lfs.symlinkattributes") |
| 847 |
end |
| 848 |
|
| 849 |
|
| 850 |
end -- of closure |
| 851 |
|
| 852 |
do -- create closure to overcome 200 locals limit |
| 853 |
|
| 854 |
package.loaded["l-package"] = package.loaded["l-package"] or true |
| 855 |
|
| 856 |
-- original size: 12566, stripped down to: 8937 |
| 857 |
|
| 858 |
if not modules then modules={} end modules ['l-package']={ |
| 859 |
version=1.001, |
| 860 |
comment="companion to luat-lib.mkiv", |
| 861 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 862 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 863 |
license="see context related readme files" |
| 864 |
} |
| 865 |
local type,unpack=type,unpack |
| 866 |
local gsub,format,find=string.gsub,string.format,string.find |
| 867 |
local insert,remove=table.insert,table.remove |
| 868 |
local P,S,Cs,lpegmatch=lpeg.P,lpeg.S,lpeg.Cs,lpeg.match |
| 869 |
local package=package |
| 870 |
local searchers=package.searchers or package.loaders |
| 871 |
local filejoin=file and file.join or function(path,name) return path.."/"..name end |
| 872 |
local isreadable=file and file.is_readable or function(name) local f=io.open(name) if f then f:close() return true end end |
| 873 |
local addsuffix=file and file.addsuffix or function(name,suffix) return name.."."..suffix end |
| 874 |
local function cleanpath(path) |
| 875 |
return path |
| 876 |
end |
| 877 |
local pattern=Cs((((1-S("\\/"))^0*(S("\\/")^1/"/"))^0*(P(".")^1/"/"+P(1))^1)*-1) |
| 878 |
local function lualibfile(name) |
| 879 |
return lpegmatch(pattern,name) or name |
| 880 |
end |
| 881 |
local offset=luarocks and 1 or 0 |
| 882 |
local helpers=package.helpers or { |
| 883 |
cleanpath=cleanpath, |
| 884 |
lualibfile=lualibfile, |
| 885 |
trace=false, |
| 886 |
report=function(...) print(format(...)) end, |
| 887 |
builtin={ |
| 888 |
["preload table"]=searchers[1+offset], |
| 889 |
["path specification"]=searchers[2+offset], |
| 890 |
["cpath specification"]=searchers[3+offset], |
| 891 |
["all in one fallback"]=searchers[4+offset], |
| 892 |
}, |
| 893 |
methods={}, |
| 894 |
sequence={ |
| 895 |
"reset loaded", |
| 896 |
"already loaded", |
| 897 |
"preload table", |
| 898 |
"qualified path", |
| 899 |
"lua extra list", |
| 900 |
"lib extra list", |
| 901 |
"path specification", |
| 902 |
"cpath specification", |
| 903 |
"all in one fallback", |
| 904 |
"not loaded", |
| 905 |
} |
| 906 |
} |
| 907 |
package.helpers=helpers |
| 908 |
local methods=helpers.methods |
| 909 |
local builtin=helpers.builtin |
| 910 |
local extraluapaths={} |
| 911 |
local extralibpaths={} |
| 912 |
local checkedfiles={} |
| 913 |
local luapaths=nil |
| 914 |
local libpaths=nil |
| 915 |
local oldluapath=nil |
| 916 |
local oldlibpath=nil |
| 917 |
local nofextralua=-1 |
| 918 |
local nofextralib=-1 |
| 919 |
local nofpathlua=-1 |
| 920 |
local nofpathlib=-1 |
| 921 |
local function listpaths(what,paths) |
| 922 |
local nofpaths=#paths |
| 923 |
if nofpaths>0 then |
| 924 |
for i=1,nofpaths do |
| 925 |
helpers.report("using %s path %i: %s",what,i,paths[i]) |
| 926 |
end |
| 927 |
else |
| 928 |
helpers.report("no %s paths defined",what) |
| 929 |
end |
| 930 |
return nofpaths |
| 931 |
end |
| 932 |
local function getextraluapaths() |
| 933 |
if helpers.trace and #extraluapaths~=nofextralua then |
| 934 |
nofextralua=listpaths("extra lua",extraluapaths) |
| 935 |
end |
| 936 |
return extraluapaths |
| 937 |
end |
| 938 |
local function getextralibpaths() |
| 939 |
if helpers.trace and #extralibpaths~=nofextralib then |
| 940 |
nofextralib=listpaths("extra lib",extralibpaths) |
| 941 |
end |
| 942 |
return extralibpaths |
| 943 |
end |
| 944 |
local function getluapaths() |
| 945 |
local luapath=package.path or "" |
| 946 |
if oldluapath~=luapath then |
| 947 |
luapaths=file.splitpath(luapath,";") |
| 948 |
oldluapath=luapath |
| 949 |
nofpathlua=-1 |
| 950 |
end |
| 951 |
if helpers.trace and #luapaths~=nofpathlua then |
| 952 |
nofpathlua=listpaths("builtin lua",luapaths) |
| 953 |
end |
| 954 |
return luapaths |
| 955 |
end |
| 956 |
local function getlibpaths() |
| 957 |
local libpath=package.cpath or "" |
| 958 |
if oldlibpath~=libpath then |
| 959 |
libpaths=file.splitpath(libpath,";") |
| 960 |
oldlibpath=libpath |
| 961 |
nofpathlib=-1 |
| 962 |
end |
| 963 |
if helpers.trace and #libpaths~=nofpathlib then |
| 964 |
nofpathlib=listpaths("builtin lib",libpaths) |
| 965 |
end |
| 966 |
return libpaths |
| 967 |
end |
| 968 |
package.luapaths=getluapaths |
| 969 |
package.libpaths=getlibpaths |
| 970 |
package.extraluapaths=getextraluapaths |
| 971 |
package.extralibpaths=getextralibpaths |
| 972 |
local hashes={ |
| 973 |
lua={}, |
| 974 |
lib={}, |
| 975 |
} |
| 976 |
local function registerpath(tag,what,target,...) |
| 977 |
local pathlist={... } |
| 978 |
local cleanpath=helpers.cleanpath |
| 979 |
local trace=helpers.trace |
| 980 |
local report=helpers.report |
| 981 |
local hash=hashes[what] |
| 982 |
local function add(path) |
| 983 |
local path=cleanpath(path) |
| 984 |
if not hash[path] then |
| 985 |
target[#target+1]=path |
| 986 |
hash[path]=true |
| 987 |
if trace then |
| 988 |
report("registered %s path %s: %s",tag,#target,path) |
| 989 |
end |
| 990 |
else |
| 991 |
if trace then |
| 992 |
report("duplicate %s path: %s",tag,path) |
| 993 |
end |
| 994 |
end |
| 995 |
end |
| 996 |
for p=1,#pathlist do |
| 997 |
local path=pathlist[p] |
| 998 |
if type(path)=="table" then |
| 999 |
for i=1,#path do |
| 1000 |
add(path[i]) |
| 1001 |
end |
| 1002 |
else |
| 1003 |
add(path) |
| 1004 |
end |
| 1005 |
end |
| 1006 |
end |
| 1007 |
local function pushpath(tag,what,target,path) |
| 1008 |
local path=helpers.cleanpath(path) |
| 1009 |
insert(target,1,path) |
| 1010 |
if helpers.trace then |
| 1011 |
helpers.report("pushing %s path in front: %s",tag,path) |
| 1012 |
end |
| 1013 |
end |
| 1014 |
local function poppath(tag,what,target) |
| 1015 |
local path=remove(target,1) |
| 1016 |
if helpers.trace then |
| 1017 |
if path then |
| 1018 |
helpers.report("popping %s path from front: %s",tag,path) |
| 1019 |
else |
| 1020 |
helpers.report("no %s path to pop",tag) |
| 1021 |
end |
| 1022 |
end |
| 1023 |
end |
| 1024 |
helpers.registerpath=registerpath |
| 1025 |
function package.extraluapath(...) |
| 1026 |
registerpath("extra lua","lua",extraluapaths,...) |
| 1027 |
end |
| 1028 |
function package.pushluapath(path) |
| 1029 |
pushpath("extra lua","lua",extraluapaths,path) |
| 1030 |
end |
| 1031 |
function package.popluapath() |
| 1032 |
poppath("extra lua","lua",extraluapaths) |
| 1033 |
end |
| 1034 |
function package.extralibpath(...) |
| 1035 |
registerpath("extra lib","lib",extralibpaths,...) |
| 1036 |
end |
| 1037 |
function package.pushlibpath(path) |
| 1038 |
pushpath("extra lib","lib",extralibpaths,path) |
| 1039 |
end |
| 1040 |
function package.poplibpath() |
| 1041 |
poppath("extra lib","lua",extralibpaths) |
| 1042 |
end |
| 1043 |
local function loadedaslib(resolved,rawname) |
| 1044 |
local base=gsub(rawname,"%.","_") |
| 1045 |
local init="luaopen_"..gsub(base,"%.","_") |
| 1046 |
local data={ resolved,init,"" } |
| 1047 |
checkedfiles[#checkedfiles+1]=data |
| 1048 |
if helpers.trace then |
| 1049 |
helpers.report("calling loadlib with '%s' with init '%s'",resolved,init) |
| 1050 |
end |
| 1051 |
local a,b,c=package.loadlib(resolved,init) |
| 1052 |
if not a and type(b)=="string" then |
| 1053 |
data[3]=string.fullstrip(b or "unknown error") |
| 1054 |
end |
| 1055 |
return a,b,c |
| 1056 |
end |
| 1057 |
helpers.loadedaslib=loadedaslib |
| 1058 |
local function loadedbypath(name,rawname,paths,islib,what) |
| 1059 |
local trace=helpers.trace |
| 1060 |
for p=1,#paths do |
| 1061 |
local path=paths[p] |
| 1062 |
local resolved=filejoin(path,name) |
| 1063 |
if trace then |
| 1064 |
helpers.report("%s path, identifying '%s' on '%s'",what,name,path) |
| 1065 |
end |
| 1066 |
if isreadable(resolved) then |
| 1067 |
if trace then |
| 1068 |
helpers.report("%s path, '%s' found on '%s'",what,name,resolved) |
| 1069 |
end |
| 1070 |
if islib then |
| 1071 |
return loadedaslib(resolved,rawname) |
| 1072 |
else |
| 1073 |
return loadfile(resolved) |
| 1074 |
end |
| 1075 |
end |
| 1076 |
end |
| 1077 |
end |
| 1078 |
helpers.loadedbypath=loadedbypath |
| 1079 |
local function loadedbyname(name,rawname) |
| 1080 |
if find(name,"^/") or find(name,"^[a-zA-Z]:/") then |
| 1081 |
local trace=helpers.trace |
| 1082 |
if trace then |
| 1083 |
helpers.report("qualified name, identifying '%s'",what,name) |
| 1084 |
end |
| 1085 |
if isreadable(name) then |
| 1086 |
if trace then |
| 1087 |
helpers.report("qualified name, '%s' found",what,name) |
| 1088 |
end |
| 1089 |
return loadfile(name) |
| 1090 |
end |
| 1091 |
end |
| 1092 |
end |
| 1093 |
helpers.loadedbyname=loadedbyname |
| 1094 |
methods["reset loaded"]=function(name) |
| 1095 |
checkedfiles={} |
| 1096 |
return false |
| 1097 |
end |
| 1098 |
methods["already loaded"]=function(name) |
| 1099 |
return package.loaded[name] |
| 1100 |
end |
| 1101 |
methods["preload table"]=function(name) |
| 1102 |
local f=builtin["preload table"] |
| 1103 |
if f then |
| 1104 |
return f(name) |
| 1105 |
end |
| 1106 |
end |
| 1107 |
methods["qualified path"]=function(name) |
| 1108 |
return loadedbyname(addsuffix(lualibfile(name),"lua"),name) |
| 1109 |
end |
| 1110 |
methods["lua extra list"]=function(name) |
| 1111 |
return loadedbypath(addsuffix(lualibfile(name),"lua"),name,getextraluapaths(),false,"lua") |
| 1112 |
end |
| 1113 |
methods["lib extra list"]=function(name) |
| 1114 |
return loadedbypath(addsuffix(lualibfile(name),os.libsuffix),name,getextralibpaths(),true,"lib") |
| 1115 |
end |
| 1116 |
methods["path specification"]=function(name) |
| 1117 |
local f=builtin["path specification"] |
| 1118 |
if f then |
| 1119 |
getluapaths() |
| 1120 |
return f(name) |
| 1121 |
end |
| 1122 |
end |
| 1123 |
methods["cpath specification"]=function(name) |
| 1124 |
local f=builtin["cpath specification"] |
| 1125 |
if f then |
| 1126 |
getlibpaths() |
| 1127 |
return f(name) |
| 1128 |
end |
| 1129 |
end |
| 1130 |
methods["all in one fallback"]=function(name) |
| 1131 |
local f=builtin["all in one fallback"] |
| 1132 |
if f then |
| 1133 |
return f(name) |
| 1134 |
end |
| 1135 |
end |
| 1136 |
methods["not loaded"]=function(name) |
| 1137 |
if helpers.trace then |
| 1138 |
helpers.report("unable to locate '%s'",name or "?") |
| 1139 |
for i=1,#checkedfiles do |
| 1140 |
helpers.report("checked file '%s', initializer '%s', message '%s'",unpack(checkedfiles[i])) |
| 1141 |
end |
| 1142 |
end |
| 1143 |
return nil |
| 1144 |
end |
| 1145 |
local level=0 |
| 1146 |
local used={} |
| 1147 |
helpers.traceused=false |
| 1148 |
function helpers.loaded(name) |
| 1149 |
local sequence=helpers.sequence |
| 1150 |
level=level+1 |
| 1151 |
for i=1,#sequence do |
| 1152 |
local method=sequence[i] |
| 1153 |
local lookup=method and methods[method] |
| 1154 |
if type(lookup)=="function" then |
| 1155 |
if helpers.trace then |
| 1156 |
helpers.report("%s, level '%s', method '%s', name '%s'","locating",level,method,name) |
| 1157 |
end |
| 1158 |
local result,rest=lookup(name) |
| 1159 |
if type(result)=="function" then |
| 1160 |
if helpers.trace then |
| 1161 |
helpers.report("%s, level '%s', method '%s', name '%s'","found",level,method,name) |
| 1162 |
end |
| 1163 |
if helpers.traceused then |
| 1164 |
used[#used+1]={ level=level,name=name } |
| 1165 |
end |
| 1166 |
level=level-1 |
| 1167 |
return result,rest |
| 1168 |
end |
| 1169 |
end |
| 1170 |
end |
| 1171 |
level=level-1 |
| 1172 |
return nil |
| 1173 |
end |
| 1174 |
function helpers.showused() |
| 1175 |
local n=#used |
| 1176 |
if n>0 then |
| 1177 |
helpers.report("%s libraries loaded:",n) |
| 1178 |
helpers.report() |
| 1179 |
for i=1,n do |
| 1180 |
local u=used[i] |
| 1181 |
helpers.report("%i %a",u.level,u.name) |
| 1182 |
end |
| 1183 |
helpers.report() |
| 1184 |
end |
| 1185 |
end |
| 1186 |
function helpers.unload(name) |
| 1187 |
if helpers.trace then |
| 1188 |
if package.loaded[name] then |
| 1189 |
helpers.report("unloading, name '%s', %s",name,"done") |
| 1190 |
else |
| 1191 |
helpers.report("unloading, name '%s', %s",name,"not loaded") |
| 1192 |
end |
| 1193 |
end |
| 1194 |
package.loaded[name]=nil |
| 1195 |
end |
| 1196 |
table.insert(searchers,1,helpers.loaded) |
| 1197 |
if context then |
| 1198 |
package.path="" |
| 1199 |
end |
| 1200 |
|
| 1201 |
|
| 1202 |
end -- of closure |
| 1203 |
|
| 1204 |
do -- create closure to overcome 200 locals limit |
| 1205 |
|
| 1206 |
package.loaded["l-lpeg"] = package.loaded["l-lpeg"] or true |
| 1207 |
|
| 1208 |
-- original size: 38786, stripped down to: 19523 |
| 1209 |
|
| 1210 |
if not modules then modules={} end modules ['l-lpeg']={ |
| 1211 |
version=1.001, |
| 1212 |
comment="companion to luat-lib.mkiv", |
| 1213 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 1214 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 1215 |
license="see context related readme files" |
| 1216 |
} |
| 1217 |
lpeg=require("lpeg") |
| 1218 |
local lpeg=lpeg |
| 1219 |
if not lpeg.print then function lpeg.print(...) print(lpeg.pcode(...)) end end |
| 1220 |
local type,next,tostring=type,next,tostring |
| 1221 |
local byte,char,gmatch,format=string.byte,string.char,string.gmatch,string.format |
| 1222 |
local floor=math.floor |
| 1223 |
local P,R,S,V,Ct,C,Cs,Cc,Cp,Cmt=lpeg.P,lpeg.R,lpeg.S,lpeg.V,lpeg.Ct,lpeg.C,lpeg.Cs,lpeg.Cc,lpeg.Cp,lpeg.Cmt |
| 1224 |
local lpegtype,lpegmatch,lpegprint=lpeg.type,lpeg.match,lpeg.print |
| 1225 |
if setinspector then |
| 1226 |
setinspector("lpeg",function(v) if lpegtype(v) then lpegprint(v) return true end end) |
| 1227 |
end |
| 1228 |
lpeg.patterns=lpeg.patterns or {} |
| 1229 |
local patterns=lpeg.patterns |
| 1230 |
local anything=P(1) |
| 1231 |
local endofstring=P(-1) |
| 1232 |
local alwaysmatched=P(true) |
| 1233 |
patterns.anything=anything |
| 1234 |
patterns.endofstring=endofstring |
| 1235 |
patterns.beginofstring=alwaysmatched |
| 1236 |
patterns.alwaysmatched=alwaysmatched |
| 1237 |
local sign=S('+-') |
| 1238 |
local zero=P('0') |
| 1239 |
local digit=R('09') |
| 1240 |
local digits=digit^1 |
| 1241 |
local octdigit=R("07") |
| 1242 |
local octdigits=octdigit^1 |
| 1243 |
local lowercase=R("az") |
| 1244 |
local uppercase=R("AZ") |
| 1245 |
local underscore=P("_") |
| 1246 |
local hexdigit=digit+lowercase+uppercase |
| 1247 |
local hexdigits=hexdigit^1 |
| 1248 |
local cr,lf,crlf=P("\r"),P("\n"),P("\r\n") |
| 1249 |
local newline=P("\r")*(P("\n")+P(true))+P("\n") |
| 1250 |
local escaped=P("\\")*anything |
| 1251 |
local squote=P("'") |
| 1252 |
local dquote=P('"') |
| 1253 |
local space=P(" ") |
| 1254 |
local period=P(".") |
| 1255 |
local comma=P(",") |
| 1256 |
local utfbom_32_be=P('\000\000\254\255') |
| 1257 |
local utfbom_32_le=P('\255\254\000\000') |
| 1258 |
local utfbom_16_be=P('\254\255') |
| 1259 |
local utfbom_16_le=P('\255\254') |
| 1260 |
local utfbom_8=P('\239\187\191') |
| 1261 |
local utfbom=utfbom_32_be+utfbom_32_le+utfbom_16_be+utfbom_16_le+utfbom_8 |
| 1262 |
local utftype=utfbom_32_be*Cc("utf-32-be")+utfbom_32_le*Cc("utf-32-le")+utfbom_16_be*Cc("utf-16-be")+utfbom_16_le*Cc("utf-16-le")+utfbom_8*Cc("utf-8")+alwaysmatched*Cc("utf-8") |
| 1263 |
local utfstricttype=utfbom_32_be*Cc("utf-32-be")+utfbom_32_le*Cc("utf-32-le")+utfbom_16_be*Cc("utf-16-be")+utfbom_16_le*Cc("utf-16-le")+utfbom_8*Cc("utf-8") |
| 1264 |
local utfoffset=utfbom_32_be*Cc(4)+utfbom_32_le*Cc(4)+utfbom_16_be*Cc(2)+utfbom_16_le*Cc(2)+utfbom_8*Cc(3)+Cc(0) |
| 1265 |
local utf8next=R("\128\191") |
| 1266 |
patterns.utfbom_32_be=utfbom_32_be |
| 1267 |
patterns.utfbom_32_le=utfbom_32_le |
| 1268 |
patterns.utfbom_16_be=utfbom_16_be |
| 1269 |
patterns.utfbom_16_le=utfbom_16_le |
| 1270 |
patterns.utfbom_8=utfbom_8 |
| 1271 |
patterns.utf_16_be_nl=P("\000\r\000\n")+P("\000\r")+P("\000\n") |
| 1272 |
patterns.utf_16_le_nl=P("\r\000\n\000")+P("\r\000")+P("\n\000") |
| 1273 |
patterns.utf_32_be_nl=P("\000\000\000\r\000\000\000\n")+P("\000\000\000\r")+P("\000\000\000\n") |
| 1274 |
patterns.utf_32_le_nl=P("\r\000\000\000\n\000\000\000")+P("\r\000\000\000")+P("\n\000\000\000") |
| 1275 |
patterns.utf8one=R("\000\127") |
| 1276 |
patterns.utf8two=R("\194\223")*utf8next |
| 1277 |
patterns.utf8three=R("\224\239")*utf8next*utf8next |
| 1278 |
patterns.utf8four=R("\240\244")*utf8next*utf8next*utf8next |
| 1279 |
patterns.utfbom=utfbom |
| 1280 |
patterns.utftype=utftype |
| 1281 |
patterns.utfstricttype=utfstricttype |
| 1282 |
patterns.utfoffset=utfoffset |
| 1283 |
local utf8char=patterns.utf8one+patterns.utf8two+patterns.utf8three+patterns.utf8four |
| 1284 |
local validutf8char=utf8char^0*endofstring*Cc(true)+Cc(false) |
| 1285 |
local utf8character=P(1)*R("\128\191")^0 |
| 1286 |
patterns.utf8=utf8char |
| 1287 |
patterns.utf8char=utf8char |
| 1288 |
patterns.utf8character=utf8character |
| 1289 |
patterns.validutf8=validutf8char |
| 1290 |
patterns.validutf8char=validutf8char |
| 1291 |
local eol=S("\n\r") |
| 1292 |
local spacer=S(" \t\f\v") |
| 1293 |
local whitespace=eol+spacer |
| 1294 |
local nonspacer=1-spacer |
| 1295 |
local nonwhitespace=1-whitespace |
| 1296 |
patterns.eol=eol |
| 1297 |
patterns.spacer=spacer |
| 1298 |
patterns.whitespace=whitespace |
| 1299 |
patterns.nonspacer=nonspacer |
| 1300 |
patterns.nonwhitespace=nonwhitespace |
| 1301 |
local stripper=spacer^0*C((spacer^0*nonspacer^1)^0) |
| 1302 |
local fullstripper=whitespace^0*C((whitespace^0*nonwhitespace^1)^0) |
| 1303 |
local collapser=Cs(spacer^0/""*nonspacer^0*((spacer^0/" "*nonspacer^1)^0)) |
| 1304 |
local nospacer=Cs((whitespace^1/""+nonwhitespace^1)^0) |
| 1305 |
local b_collapser=Cs(whitespace^0/""*(nonwhitespace^1+whitespace^1/" ")^0) |
| 1306 |
local m_collapser=Cs((nonwhitespace^1+whitespace^1/" ")^0) |
| 1307 |
local e_collapser=Cs((whitespace^1*endofstring/""+nonwhitespace^1+whitespace^1/" ")^0) |
| 1308 |
local x_collapser=Cs((nonwhitespace^1+whitespace^1/"" )^0) |
| 1309 |
local b_stripper=Cs(spacer^0/""*(nonspacer^1+spacer^1/" ")^0) |
| 1310 |
local m_stripper=Cs((nonspacer^1+spacer^1/" ")^0) |
| 1311 |
local e_stripper=Cs((spacer^1*endofstring/""+nonspacer^1+spacer^1/" ")^0) |
| 1312 |
local x_stripper=Cs((nonspacer^1+spacer^1/"" )^0) |
| 1313 |
patterns.stripper=stripper |
| 1314 |
patterns.fullstripper=fullstripper |
| 1315 |
patterns.collapser=collapser |
| 1316 |
patterns.nospacer=nospacer |
| 1317 |
patterns.b_collapser=b_collapser |
| 1318 |
patterns.m_collapser=m_collapser |
| 1319 |
patterns.e_collapser=e_collapser |
| 1320 |
patterns.x_collapser=x_collapser |
| 1321 |
patterns.b_stripper=b_stripper |
| 1322 |
patterns.m_stripper=m_stripper |
| 1323 |
patterns.e_stripper=e_stripper |
| 1324 |
patterns.x_stripper=x_stripper |
| 1325 |
patterns.lowercase=lowercase |
| 1326 |
patterns.uppercase=uppercase |
| 1327 |
patterns.letter=patterns.lowercase+patterns.uppercase |
| 1328 |
patterns.space=space |
| 1329 |
patterns.tab=P("\t") |
| 1330 |
patterns.spaceortab=patterns.space+patterns.tab |
| 1331 |
patterns.newline=newline |
| 1332 |
patterns.emptyline=newline^1 |
| 1333 |
patterns.equal=P("=") |
| 1334 |
patterns.comma=comma |
| 1335 |
patterns.commaspacer=comma*spacer^0 |
| 1336 |
patterns.period=period |
| 1337 |
patterns.colon=P(":") |
| 1338 |
patterns.semicolon=P(";") |
| 1339 |
patterns.underscore=underscore |
| 1340 |
patterns.escaped=escaped |
| 1341 |
patterns.squote=squote |
| 1342 |
patterns.dquote=dquote |
| 1343 |
patterns.nosquote=(escaped+(1-squote))^0 |
| 1344 |
patterns.nodquote=(escaped+(1-dquote))^0 |
| 1345 |
patterns.unsingle=(squote/"")*patterns.nosquote*(squote/"") |
| 1346 |
patterns.undouble=(dquote/"")*patterns.nodquote*(dquote/"") |
| 1347 |
patterns.unquoted=patterns.undouble+patterns.unsingle |
| 1348 |
patterns.unspacer=((patterns.spacer^1)/"")^0 |
| 1349 |
patterns.singlequoted=squote*patterns.nosquote*squote |
| 1350 |
patterns.doublequoted=dquote*patterns.nodquote*dquote |
| 1351 |
patterns.quoted=patterns.doublequoted+patterns.singlequoted |
| 1352 |
patterns.digit=digit |
| 1353 |
patterns.digits=digits |
| 1354 |
patterns.octdigit=octdigit |
| 1355 |
patterns.octdigits=octdigits |
| 1356 |
patterns.hexdigit=hexdigit |
| 1357 |
patterns.hexdigits=hexdigits |
| 1358 |
patterns.sign=sign |
| 1359 |
patterns.cardinal=digits |
| 1360 |
patterns.integer=sign^-1*digits |
| 1361 |
patterns.unsigned=digit^0*period*digits |
| 1362 |
patterns.float=sign^-1*patterns.unsigned |
| 1363 |
patterns.cunsigned=digit^0*comma*digits |
| 1364 |
patterns.cpunsigned=digit^0*(period+comma)*digits |
| 1365 |
patterns.cfloat=sign^-1*patterns.cunsigned |
| 1366 |
patterns.cpfloat=sign^-1*patterns.cpunsigned |
| 1367 |
patterns.number=patterns.float+patterns.integer |
| 1368 |
patterns.cnumber=patterns.cfloat+patterns.integer |
| 1369 |
patterns.cpnumber=patterns.cpfloat+patterns.integer |
| 1370 |
patterns.oct=zero*octdigits |
| 1371 |
patterns.octal=patterns.oct |
| 1372 |
patterns.HEX=zero*P("X")*(digit+uppercase)^1 |
| 1373 |
patterns.hex=zero*P("x")*(digit+lowercase)^1 |
| 1374 |
patterns.hexadecimal=zero*S("xX")*hexdigits |
| 1375 |
patterns.hexafloat=sign^-1*zero*S("xX")*(hexdigit^0*period*hexdigits+hexdigits*period*hexdigit^0+hexdigits)*(S("pP")*sign^-1*hexdigits)^-1 |
| 1376 |
patterns.decafloat=sign^-1*(digit^0*period*digits+digits*period*digit^0+digits)*S("eE")*sign^-1*digits |
| 1377 |
patterns.propername=(uppercase+lowercase+underscore)*(uppercase+lowercase+underscore+digit)^0*endofstring |
| 1378 |
patterns.somecontent=(anything-newline-space)^1 |
| 1379 |
patterns.beginline=#(1-newline) |
| 1380 |
patterns.longtostring=Cs(whitespace^0/""*((patterns.quoted+nonwhitespace^1+whitespace^1/""*(endofstring+Cc(" ")))^0)) |
| 1381 |
local function anywhere(pattern) |
| 1382 |
return (1-P(pattern))^0*P(pattern) |
| 1383 |
end |
| 1384 |
lpeg.anywhere=anywhere |
| 1385 |
function lpeg.instringchecker(p) |
| 1386 |
p=anywhere(p) |
| 1387 |
return function(str) |
| 1388 |
return lpegmatch(p,str) and true or false |
| 1389 |
end |
| 1390 |
end |
| 1391 |
function lpeg.splitter(pattern,action) |
| 1392 |
if action then |
| 1393 |
return (((1-P(pattern))^1)/action+1)^0 |
| 1394 |
else |
| 1395 |
return (Cs((1-P(pattern))^1)+1)^0 |
| 1396 |
end |
| 1397 |
end |
| 1398 |
function lpeg.tsplitter(pattern,action) |
| 1399 |
if action then |
| 1400 |
return Ct((((1-P(pattern))^1)/action+1)^0) |
| 1401 |
else |
| 1402 |
return Ct((Cs((1-P(pattern))^1)+1)^0) |
| 1403 |
end |
| 1404 |
end |
| 1405 |
local splitters_s,splitters_m,splitters_t={},{},{} |
| 1406 |
local function splitat(separator,single) |
| 1407 |
local splitter=(single and splitters_s[separator]) or splitters_m[separator] |
| 1408 |
if not splitter then |
| 1409 |
separator=P(separator) |
| 1410 |
local other=C((1-separator)^0) |
| 1411 |
if single then |
| 1412 |
local any=anything |
| 1413 |
splitter=other*(separator*C(any^0)+"") |
| 1414 |
splitters_s[separator]=splitter |
| 1415 |
else |
| 1416 |
splitter=other*(separator*other)^0 |
| 1417 |
splitters_m[separator]=splitter |
| 1418 |
end |
| 1419 |
end |
| 1420 |
return splitter |
| 1421 |
end |
| 1422 |
local function tsplitat(separator) |
| 1423 |
local splitter=splitters_t[separator] |
| 1424 |
if not splitter then |
| 1425 |
splitter=Ct(splitat(separator)) |
| 1426 |
splitters_t[separator]=splitter |
| 1427 |
end |
| 1428 |
return splitter |
| 1429 |
end |
| 1430 |
lpeg.splitat=splitat |
| 1431 |
lpeg.tsplitat=tsplitat |
| 1432 |
function string.splitup(str,separator) |
| 1433 |
if not separator then |
| 1434 |
separator="," |
| 1435 |
end |
| 1436 |
return lpegmatch(splitters_m[separator] or splitat(separator),str) |
| 1437 |
end |
| 1438 |
local cache={} |
| 1439 |
function lpeg.split(separator,str) |
| 1440 |
local c=cache[separator] |
| 1441 |
if not c then |
| 1442 |
c=tsplitat(separator) |
| 1443 |
cache[separator]=c |
| 1444 |
end |
| 1445 |
return lpegmatch(c,str) |
| 1446 |
end |
| 1447 |
function string.split(str,separator) |
| 1448 |
if separator then |
| 1449 |
local c=cache[separator] |
| 1450 |
if not c then |
| 1451 |
c=tsplitat(separator) |
| 1452 |
cache[separator]=c |
| 1453 |
end |
| 1454 |
return lpegmatch(c,str) |
| 1455 |
else |
| 1456 |
return { str } |
| 1457 |
end |
| 1458 |
end |
| 1459 |
local spacing=patterns.spacer^0*newline |
| 1460 |
local empty=spacing*Cc("") |
| 1461 |
local nonempty=Cs((1-spacing)^1)*spacing^-1 |
| 1462 |
local content=(empty+nonempty)^1 |
| 1463 |
patterns.textline=content |
| 1464 |
local linesplitter=tsplitat(newline) |
| 1465 |
patterns.linesplitter=linesplitter |
| 1466 |
function string.splitlines(str) |
| 1467 |
return lpegmatch(linesplitter,str) |
| 1468 |
end |
| 1469 |
local cache={} |
| 1470 |
function lpeg.checkedsplit(separator,str) |
| 1471 |
local c=cache[separator] |
| 1472 |
if not c then |
| 1473 |
separator=P(separator) |
| 1474 |
local other=C((1-separator)^1) |
| 1475 |
c=Ct(separator^0*other*(separator^1*other)^0) |
| 1476 |
cache[separator]=c |
| 1477 |
end |
| 1478 |
return lpegmatch(c,str) |
| 1479 |
end |
| 1480 |
function string.checkedsplit(str,separator) |
| 1481 |
local c=cache[separator] |
| 1482 |
if not c then |
| 1483 |
separator=P(separator) |
| 1484 |
local other=C((1-separator)^1) |
| 1485 |
c=Ct(separator^0*other*(separator^1*other)^0) |
| 1486 |
cache[separator]=c |
| 1487 |
end |
| 1488 |
return lpegmatch(c,str) |
| 1489 |
end |
| 1490 |
local function f2(s) local c1,c2=byte(s,1,2) return c1*64+c2-12416 end |
| 1491 |
local function f3(s) local c1,c2,c3=byte(s,1,3) return (c1*64+c2)*64+c3-925824 end |
| 1492 |
local function f4(s) local c1,c2,c3,c4=byte(s,1,4) return ((c1*64+c2)*64+c3)*64+c4-63447168 end |
| 1493 |
local utf8byte=patterns.utf8one/byte+patterns.utf8two/f2+patterns.utf8three/f3+patterns.utf8four/f4 |
| 1494 |
patterns.utf8byte=utf8byte |
| 1495 |
local cache={} |
| 1496 |
function lpeg.stripper(str) |
| 1497 |
if type(str)=="string" then |
| 1498 |
local s=cache[str] |
| 1499 |
if not s then |
| 1500 |
s=Cs(((S(str)^1)/""+1)^0) |
| 1501 |
cache[str]=s |
| 1502 |
end |
| 1503 |
return s |
| 1504 |
else |
| 1505 |
return Cs(((str^1)/""+1)^0) |
| 1506 |
end |
| 1507 |
end |
| 1508 |
local cache={} |
| 1509 |
function lpeg.keeper(str) |
| 1510 |
if type(str)=="string" then |
| 1511 |
local s=cache[str] |
| 1512 |
if not s then |
| 1513 |
s=Cs((((1-S(str))^1)/""+1)^0) |
| 1514 |
cache[str]=s |
| 1515 |
end |
| 1516 |
return s |
| 1517 |
else |
| 1518 |
return Cs((((1-str)^1)/""+1)^0) |
| 1519 |
end |
| 1520 |
end |
| 1521 |
function lpeg.frontstripper(str) |
| 1522 |
return (P(str)+P(true))*Cs(anything^0) |
| 1523 |
end |
| 1524 |
function lpeg.endstripper(str) |
| 1525 |
return Cs((1-P(str)*endofstring)^0) |
| 1526 |
end |
| 1527 |
function lpeg.replacer(one,two,makefunction,isutf) |
| 1528 |
local pattern |
| 1529 |
local u=isutf and utf8char or 1 |
| 1530 |
if type(one)=="table" then |
| 1531 |
local no=#one |
| 1532 |
local p=P(false) |
| 1533 |
if no==0 then |
| 1534 |
for k,v in next,one do |
| 1535 |
p=p+P(k)/v |
| 1536 |
end |
| 1537 |
pattern=Cs((p+u)^0) |
| 1538 |
elseif no==1 then |
| 1539 |
local o=one[1] |
| 1540 |
one,two=P(o[1]),o[2] |
| 1541 |
pattern=Cs((one/two+u)^0) |
| 1542 |
else |
| 1543 |
for i=1,no do |
| 1544 |
local o=one[i] |
| 1545 |
p=p+P(o[1])/o[2] |
| 1546 |
end |
| 1547 |
pattern=Cs((p+u)^0) |
| 1548 |
end |
| 1549 |
else |
| 1550 |
pattern=Cs((P(one)/(two or "")+u)^0) |
| 1551 |
end |
| 1552 |
if makefunction then |
| 1553 |
return function(str) |
| 1554 |
return lpegmatch(pattern,str) |
| 1555 |
end |
| 1556 |
else |
| 1557 |
return pattern |
| 1558 |
end |
| 1559 |
end |
| 1560 |
function lpeg.finder(lst,makefunction,isutf) |
| 1561 |
local pattern |
| 1562 |
if type(lst)=="table" then |
| 1563 |
pattern=P(false) |
| 1564 |
if #lst==0 then |
| 1565 |
for k,v in next,lst do |
| 1566 |
pattern=pattern+P(k) |
| 1567 |
end |
| 1568 |
else |
| 1569 |
for i=1,#lst do |
| 1570 |
pattern=pattern+P(lst[i]) |
| 1571 |
end |
| 1572 |
end |
| 1573 |
else |
| 1574 |
pattern=P(lst) |
| 1575 |
end |
| 1576 |
if isutf then |
| 1577 |
pattern=((utf8char or 1)-pattern)^0*pattern |
| 1578 |
else |
| 1579 |
pattern=(1-pattern)^0*pattern |
| 1580 |
end |
| 1581 |
if makefunction then |
| 1582 |
return function(str) |
| 1583 |
return lpegmatch(pattern,str) |
| 1584 |
end |
| 1585 |
else |
| 1586 |
return pattern |
| 1587 |
end |
| 1588 |
end |
| 1589 |
local splitters_f,splitters_s={},{} |
| 1590 |
function lpeg.firstofsplit(separator) |
| 1591 |
local splitter=splitters_f[separator] |
| 1592 |
if not splitter then |
| 1593 |
local pattern=P(separator) |
| 1594 |
splitter=C((1-pattern)^0) |
| 1595 |
splitters_f[separator]=splitter |
| 1596 |
end |
| 1597 |
return splitter |
| 1598 |
end |
| 1599 |
function lpeg.secondofsplit(separator) |
| 1600 |
local splitter=splitters_s[separator] |
| 1601 |
if not splitter then |
| 1602 |
local pattern=P(separator) |
| 1603 |
splitter=(1-pattern)^0*pattern*C(anything^0) |
| 1604 |
splitters_s[separator]=splitter |
| 1605 |
end |
| 1606 |
return splitter |
| 1607 |
end |
| 1608 |
local splitters_s,splitters_p={},{} |
| 1609 |
function lpeg.beforesuffix(separator) |
| 1610 |
local splitter=splitters_s[separator] |
| 1611 |
if not splitter then |
| 1612 |
local pattern=P(separator) |
| 1613 |
splitter=C((1-pattern)^0)*pattern*endofstring |
| 1614 |
splitters_s[separator]=splitter |
| 1615 |
end |
| 1616 |
return splitter |
| 1617 |
end |
| 1618 |
function lpeg.afterprefix(separator) |
| 1619 |
local splitter=splitters_p[separator] |
| 1620 |
if not splitter then |
| 1621 |
local pattern=P(separator) |
| 1622 |
splitter=pattern*C(anything^0) |
| 1623 |
splitters_p[separator]=splitter |
| 1624 |
end |
| 1625 |
return splitter |
| 1626 |
end |
| 1627 |
function lpeg.balancer(left,right) |
| 1628 |
left,right=P(left),P(right) |
| 1629 |
return P { left*((1-left-right)+V(1))^0*right } |
| 1630 |
end |
| 1631 |
function lpeg.counter(pattern,action) |
| 1632 |
local n=0 |
| 1633 |
local pattern=(P(pattern)/function() n=n+1 end+anything)^0 |
| 1634 |
if action then |
| 1635 |
return function(str) n=0;lpegmatch(pattern,str);action(n) end |
| 1636 |
else |
| 1637 |
return function(str) n=0;lpegmatch(pattern,str);return n end |
| 1638 |
end |
| 1639 |
end |
| 1640 |
function lpeg.is_lpeg(p) |
| 1641 |
return p and lpegtype(p)=="pattern" |
| 1642 |
end |
| 1643 |
function lpeg.oneof(list,...) |
| 1644 |
if type(list)~="table" then |
| 1645 |
list={ list,... } |
| 1646 |
end |
| 1647 |
local p=P(list[1]) |
| 1648 |
for l=2,#list do |
| 1649 |
p=p+P(list[l]) |
| 1650 |
end |
| 1651 |
return p |
| 1652 |
end |
| 1653 |
local sort=table.sort |
| 1654 |
local function copyindexed(old) |
| 1655 |
local new={} |
| 1656 |
for i=1,#old do |
| 1657 |
new[i]=old |
| 1658 |
end |
| 1659 |
return new |
| 1660 |
end |
| 1661 |
local function sortedkeys(tab) |
| 1662 |
local keys,s={},0 |
| 1663 |
for key,_ in next,tab do |
| 1664 |
s=s+1 |
| 1665 |
keys[s]=key |
| 1666 |
end |
| 1667 |
sort(keys) |
| 1668 |
return keys |
| 1669 |
end |
| 1670 |
function lpeg.append(list,pp,delayed,checked) |
| 1671 |
local p=pp |
| 1672 |
if #list>0 then |
| 1673 |
local keys=copyindexed(list) |
| 1674 |
sort(keys) |
| 1675 |
for i=#keys,1,-1 do |
| 1676 |
local k=keys[i] |
| 1677 |
if p then |
| 1678 |
p=P(k)+p |
| 1679 |
else |
| 1680 |
p=P(k) |
| 1681 |
end |
| 1682 |
end |
| 1683 |
elseif delayed then |
| 1684 |
local keys=sortedkeys(list) |
| 1685 |
if p then |
| 1686 |
for i=1,#keys,1 do |
| 1687 |
local k=keys[i] |
| 1688 |
local v=list[k] |
| 1689 |
p=P(k)/list+p |
| 1690 |
end |
| 1691 |
else |
| 1692 |
for i=1,#keys do |
| 1693 |
local k=keys[i] |
| 1694 |
local v=list[k] |
| 1695 |
if p then |
| 1696 |
p=P(k)+p |
| 1697 |
else |
| 1698 |
p=P(k) |
| 1699 |
end |
| 1700 |
end |
| 1701 |
if p then |
| 1702 |
p=p/list |
| 1703 |
end |
| 1704 |
end |
| 1705 |
elseif checked then |
| 1706 |
local keys=sortedkeys(list) |
| 1707 |
for i=1,#keys do |
| 1708 |
local k=keys[i] |
| 1709 |
local v=list[k] |
| 1710 |
if p then |
| 1711 |
if k==v then |
| 1712 |
p=P(k)+p |
| 1713 |
else |
| 1714 |
p=P(k)/v+p |
| 1715 |
end |
| 1716 |
else |
| 1717 |
if k==v then |
| 1718 |
p=P(k) |
| 1719 |
else |
| 1720 |
p=P(k)/v |
| 1721 |
end |
| 1722 |
end |
| 1723 |
end |
| 1724 |
else |
| 1725 |
local keys=sortedkeys(list) |
| 1726 |
for i=1,#keys do |
| 1727 |
local k=keys[i] |
| 1728 |
local v=list[k] |
| 1729 |
if p then |
| 1730 |
p=P(k)/v+p |
| 1731 |
else |
| 1732 |
p=P(k)/v |
| 1733 |
end |
| 1734 |
end |
| 1735 |
end |
| 1736 |
return p |
| 1737 |
end |
| 1738 |
local p_false=P(false) |
| 1739 |
local p_true=P(true) |
| 1740 |
local lower=utf and utf.lower or string.lower |
| 1741 |
local upper=utf and utf.upper or string.upper |
| 1742 |
function lpeg.setutfcasers(l,u) |
| 1743 |
lower=l or lower |
| 1744 |
upper=u or upper |
| 1745 |
utf.upper=upper |
| 1746 |
utf.lower=lower |
| 1747 |
end |
| 1748 |
local function make1(t,rest) |
| 1749 |
local p=p_false |
| 1750 |
local keys=sortedkeys(t) |
| 1751 |
for i=1,#keys do |
| 1752 |
local k=keys[i] |
| 1753 |
if k~="" then |
| 1754 |
local v=t[k] |
| 1755 |
if v==true then |
| 1756 |
p=p+P(k)*p_true |
| 1757 |
elseif v==false then |
| 1758 |
else |
| 1759 |
p=p+P(k)*make1(v,v[""]) |
| 1760 |
end |
| 1761 |
end |
| 1762 |
end |
| 1763 |
if rest then |
| 1764 |
p=p+p_true |
| 1765 |
end |
| 1766 |
return p |
| 1767 |
end |
| 1768 |
local function make2(t,rest) |
| 1769 |
local p=p_false |
| 1770 |
local keys=sortedkeys(t) |
| 1771 |
for i=1,#keys do |
| 1772 |
local k=keys[i] |
| 1773 |
if k~="" then |
| 1774 |
local v=t[k] |
| 1775 |
if v==true then |
| 1776 |
p=p+(P(lower(k))+P(upper(k)))*p_true |
| 1777 |
elseif v==false then |
| 1778 |
else |
| 1779 |
p=p+(P(lower(k))+P(upper(k)))*make2(v,v[""]) |
| 1780 |
end |
| 1781 |
end |
| 1782 |
end |
| 1783 |
if rest then |
| 1784 |
p=p+p_true |
| 1785 |
end |
| 1786 |
return p |
| 1787 |
end |
| 1788 |
local function utfchartabletopattern(list,insensitive) |
| 1789 |
local tree={} |
| 1790 |
local n=#list |
| 1791 |
if n==0 then |
| 1792 |
for s in next,list do |
| 1793 |
local t=tree |
| 1794 |
local p,pk |
| 1795 |
for c in gmatch(s,".") do |
| 1796 |
if t==true then |
| 1797 |
t={ [c]=true,[""]=true } |
| 1798 |
p[pk]=t |
| 1799 |
p=t |
| 1800 |
t=false |
| 1801 |
elseif t==false then |
| 1802 |
t={ [c]=false } |
| 1803 |
p[pk]=t |
| 1804 |
p=t |
| 1805 |
t=false |
| 1806 |
else |
| 1807 |
local tc=t[c] |
| 1808 |
if not tc then |
| 1809 |
tc=false |
| 1810 |
t[c]=false |
| 1811 |
end |
| 1812 |
p=t |
| 1813 |
t=tc |
| 1814 |
end |
| 1815 |
pk=c |
| 1816 |
end |
| 1817 |
if t==false then |
| 1818 |
p[pk]=true |
| 1819 |
elseif t==true then |
| 1820 |
else |
| 1821 |
t[""]=true |
| 1822 |
end |
| 1823 |
end |
| 1824 |
else |
| 1825 |
for i=1,n do |
| 1826 |
local s=list[i] |
| 1827 |
local t=tree |
| 1828 |
local p,pk |
| 1829 |
for c in gmatch(s,".") do |
| 1830 |
if t==true then |
| 1831 |
t={ [c]=true,[""]=true } |
| 1832 |
p[pk]=t |
| 1833 |
p=t |
| 1834 |
t=false |
| 1835 |
elseif t==false then |
| 1836 |
t={ [c]=false } |
| 1837 |
p[pk]=t |
| 1838 |
p=t |
| 1839 |
t=false |
| 1840 |
else |
| 1841 |
local tc=t[c] |
| 1842 |
if not tc then |
| 1843 |
tc=false |
| 1844 |
t[c]=false |
| 1845 |
end |
| 1846 |
p=t |
| 1847 |
t=tc |
| 1848 |
end |
| 1849 |
pk=c |
| 1850 |
end |
| 1851 |
if t==false then |
| 1852 |
p[pk]=true |
| 1853 |
elseif t==true then |
| 1854 |
else |
| 1855 |
t[""]=true |
| 1856 |
end |
| 1857 |
end |
| 1858 |
end |
| 1859 |
return (insensitive and make2 or make1)(tree) |
| 1860 |
end |
| 1861 |
lpeg.utfchartabletopattern=utfchartabletopattern |
| 1862 |
function lpeg.utfreplacer(list,insensitive) |
| 1863 |
local pattern=Cs((utfchartabletopattern(list,insensitive)/list+utf8character)^0) |
| 1864 |
return function(str) |
| 1865 |
return lpegmatch(pattern,str) or str |
| 1866 |
end |
| 1867 |
end |
| 1868 |
patterns.containseol=lpeg.finder(eol) |
| 1869 |
local function nextstep(n,step,result) |
| 1870 |
local m=n%step |
| 1871 |
local d=floor(n/step) |
| 1872 |
if d>0 then |
| 1873 |
local v=V(tostring(step)) |
| 1874 |
local s=result.start |
| 1875 |
for i=1,d do |
| 1876 |
if s then |
| 1877 |
s=v*s |
| 1878 |
else |
| 1879 |
s=v |
| 1880 |
end |
| 1881 |
end |
| 1882 |
result.start=s |
| 1883 |
end |
| 1884 |
if step>1 and result.start then |
| 1885 |
local v=V(tostring(step/2)) |
| 1886 |
result[tostring(step)]=v*v |
| 1887 |
end |
| 1888 |
if step>0 then |
| 1889 |
return nextstep(m,step/2,result) |
| 1890 |
else |
| 1891 |
return result |
| 1892 |
end |
| 1893 |
end |
| 1894 |
function lpeg.times(pattern,n) |
| 1895 |
return P(nextstep(n,2^16,{ "start",["1"]=pattern })) |
| 1896 |
end |
| 1897 |
do |
| 1898 |
local trailingzeros=zero^0*-digit |
| 1899 |
local stripper=Cs(( |
| 1900 |
digits*( |
| 1901 |
period*trailingzeros/""+period*(digit-trailingzeros)^1*(trailingzeros/"") |
| 1902 |
)+1 |
| 1903 |
)^0) |
| 1904 |
lpeg.patterns.stripzeros=stripper |
| 1905 |
local nonzero=digit-zero |
| 1906 |
local trailingzeros=zero^1*endofstring |
| 1907 |
local stripper=Cs((1-period)^0*( |
| 1908 |
period*trailingzeros/""+period*(nonzero^1+(trailingzeros/"")+zero^1)^0+endofstring |
| 1909 |
)) |
| 1910 |
lpeg.patterns.stripzero=stripper |
| 1911 |
end |
| 1912 |
local byte_to_HEX={} |
| 1913 |
local byte_to_hex={} |
| 1914 |
local byte_to_dec={} |
| 1915 |
local hex_to_byte={} |
| 1916 |
for i=0,255 do |
| 1917 |
local H=format("%02X",i) |
| 1918 |
local h=format("%02x",i) |
| 1919 |
local d=format("%03i",i) |
| 1920 |
local c=char(i) |
| 1921 |
byte_to_HEX[c]=H |
| 1922 |
byte_to_hex[c]=h |
| 1923 |
byte_to_dec[c]=d |
| 1924 |
hex_to_byte[h]=c |
| 1925 |
hex_to_byte[H]=c |
| 1926 |
end |
| 1927 |
local hextobyte=P(2)/hex_to_byte |
| 1928 |
local bytetoHEX=P(1)/byte_to_HEX |
| 1929 |
local bytetohex=P(1)/byte_to_hex |
| 1930 |
local bytetodec=P(1)/byte_to_dec |
| 1931 |
local hextobytes=Cs(hextobyte^0) |
| 1932 |
local bytestoHEX=Cs(bytetoHEX^0) |
| 1933 |
local bytestohex=Cs(bytetohex^0) |
| 1934 |
local bytestodec=Cs(bytetodec^0) |
| 1935 |
patterns.hextobyte=hextobyte |
| 1936 |
patterns.bytetoHEX=bytetoHEX |
| 1937 |
patterns.bytetohex=bytetohex |
| 1938 |
patterns.bytetodec=bytetodec |
| 1939 |
patterns.hextobytes=hextobytes |
| 1940 |
patterns.bytestoHEX=bytestoHEX |
| 1941 |
patterns.bytestohex=bytestohex |
| 1942 |
patterns.bytestodec=bytestodec |
| 1943 |
function string.toHEX(s) |
| 1944 |
if not s or s=="" then |
| 1945 |
return s |
| 1946 |
else |
| 1947 |
return lpegmatch(bytestoHEX,s) |
| 1948 |
end |
| 1949 |
end |
| 1950 |
function string.tohex(s) |
| 1951 |
if not s or s=="" then |
| 1952 |
return s |
| 1953 |
else |
| 1954 |
return lpegmatch(bytestohex,s) |
| 1955 |
end |
| 1956 |
end |
| 1957 |
function string.todec(s) |
| 1958 |
if not s or s=="" then |
| 1959 |
return s |
| 1960 |
else |
| 1961 |
return lpegmatch(bytestodec,s) |
| 1962 |
end |
| 1963 |
end |
| 1964 |
function string.tobytes(s) |
| 1965 |
if not s or s=="" then |
| 1966 |
return s |
| 1967 |
else |
| 1968 |
return lpegmatch(hextobytes,s) |
| 1969 |
end |
| 1970 |
end |
| 1971 |
local patterns={} |
| 1972 |
local function containsws(what) |
| 1973 |
local p=patterns[what] |
| 1974 |
if not p then |
| 1975 |
local p1=P(what)*(whitespace+endofstring)*Cc(true) |
| 1976 |
local p2=whitespace*P(p1) |
| 1977 |
p=P(p1)+P(1-p2)^0*p2+Cc(false) |
| 1978 |
patterns[what]=p |
| 1979 |
end |
| 1980 |
return p |
| 1981 |
end |
| 1982 |
lpeg.containsws=containsws |
| 1983 |
function string.containsws(str,what) |
| 1984 |
return lpegmatch(patterns[what] or containsws(what),str) |
| 1985 |
end |
| 1986 |
|
| 1987 |
|
| 1988 |
end -- of closure |
| 1989 |
|
| 1990 |
do -- create closure to overcome 200 locals limit |
| 1991 |
|
| 1992 |
package.loaded["l-function"] = package.loaded["l-function"] or true |
| 1993 |
|
| 1994 |
-- original size: 361, stripped down to: 317 |
| 1995 |
|
| 1996 |
if not modules then modules={} end modules ['l-functions']={ |
| 1997 |
version=1.001, |
| 1998 |
comment="companion to luat-lib.mkiv", |
| 1999 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 2000 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 2001 |
license="see context related readme files" |
| 2002 |
} |
| 2003 |
functions=functions or {} |
| 2004 |
function functions.dummy() end |
| 2005 |
|
| 2006 |
|
| 2007 |
end -- of closure |
| 2008 |
|
| 2009 |
do -- create closure to overcome 200 locals limit |
| 2010 |
|
| 2011 |
package.loaded["l-string"] = package.loaded["l-string"] or true |
| 2012 |
|
| 2013 |
-- original size: 6955, stripped down to: 3504 |
| 2014 |
|
| 2015 |
if not modules then modules={} end modules ['l-string']={ |
| 2016 |
version=1.001, |
| 2017 |
comment="companion to luat-lib.mkiv", |
| 2018 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 2019 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 2020 |
license="see context related readme files" |
| 2021 |
} |
| 2022 |
local string=string |
| 2023 |
local sub,gmatch,format,char,byte,rep,lower,find=string.sub,string.gmatch,string.format,string.char,string.byte,string.rep,string.lower,string.find |
| 2024 |
local lpegmatch,patterns=lpeg.match,lpeg.patterns |
| 2025 |
local P,S,C,Ct,Cc,Cs=lpeg.P,lpeg.S,lpeg.C,lpeg.Ct,lpeg.Cc,lpeg.Cs |
| 2026 |
local unquoted=patterns.squote*C(patterns.nosquote)*patterns.squote+patterns.dquote*C(patterns.nodquote)*patterns.dquote |
| 2027 |
function string.unquoted(str) |
| 2028 |
return lpegmatch(unquoted,str) or str |
| 2029 |
end |
| 2030 |
function string.quoted(str) |
| 2031 |
return format("%q",str) |
| 2032 |
end |
| 2033 |
function string.count(str,pattern) |
| 2034 |
local n=0 |
| 2035 |
local i=1 |
| 2036 |
local l=#pattern |
| 2037 |
while true do |
| 2038 |
i=find(str,pattern,i) |
| 2039 |
if i then |
| 2040 |
n=n+1 |
| 2041 |
i=i+l |
| 2042 |
else |
| 2043 |
break |
| 2044 |
end |
| 2045 |
end |
| 2046 |
return n |
| 2047 |
end |
| 2048 |
function string.limit(str,n,sentinel) |
| 2049 |
if #str>n then |
| 2050 |
sentinel=sentinel or "..." |
| 2051 |
return sub(str,1,(n-#sentinel))..sentinel |
| 2052 |
else |
| 2053 |
return str |
| 2054 |
end |
| 2055 |
end |
| 2056 |
local stripper=patterns.stripper |
| 2057 |
local fullstripper=patterns.fullstripper |
| 2058 |
local collapser=patterns.collapser |
| 2059 |
local nospacer=patterns.nospacer |
| 2060 |
local longtostring=patterns.longtostring |
| 2061 |
function string.strip(str) |
| 2062 |
return str and lpegmatch(stripper,str) or "" |
| 2063 |
end |
| 2064 |
function string.fullstrip(str) |
| 2065 |
return str and lpegmatch(fullstripper,str) or "" |
| 2066 |
end |
| 2067 |
function string.collapsespaces(str) |
| 2068 |
return str and lpegmatch(collapser,str) or "" |
| 2069 |
end |
| 2070 |
function string.nospaces(str) |
| 2071 |
return str and lpegmatch(nospacer,str) or "" |
| 2072 |
end |
| 2073 |
function string.longtostring(str) |
| 2074 |
return str and lpegmatch(longtostring,str) or "" |
| 2075 |
end |
| 2076 |
local pattern=P(" ")^0*P(-1) |
| 2077 |
function string.is_empty(str) |
| 2078 |
if not str or str=="" then |
| 2079 |
return true |
| 2080 |
else |
| 2081 |
return lpegmatch(pattern,str) and true or false |
| 2082 |
end |
| 2083 |
end |
| 2084 |
local anything=patterns.anything |
| 2085 |
local moreescapes=Cc("%")*S(".-+%?()[]*$^{}") |
| 2086 |
local allescapes=Cc("%")*S(".-+%?()[]*") |
| 2087 |
local someescapes=Cc("%")*S(".-+%()[]") |
| 2088 |
local matchescapes=Cc(".")*S("*?") |
| 2089 |
local pattern_m=Cs ((moreescapes+anything )^0 ) |
| 2090 |
local pattern_a=Cs ((allescapes+anything )^0 ) |
| 2091 |
local pattern_b=Cs ((someescapes+matchescapes+anything )^0 ) |
| 2092 |
local pattern_c=Cs (Cc("^")*(someescapes+matchescapes+anything )^0*Cc("$") ) |
| 2093 |
function string.escapedpattern(str,simple) |
| 2094 |
return lpegmatch(simple and pattern_b or pattern_a,str) |
| 2095 |
end |
| 2096 |
function string.topattern(str,lowercase,strict) |
| 2097 |
if str=="" or type(str)~="string" then |
| 2098 |
return ".*" |
| 2099 |
elseif strict=="all" then |
| 2100 |
str=lpegmatch(pattern_m,str) |
| 2101 |
elseif strict then |
| 2102 |
str=lpegmatch(pattern_c,str) |
| 2103 |
else |
| 2104 |
str=lpegmatch(pattern_b,str) |
| 2105 |
end |
| 2106 |
if lowercase then |
| 2107 |
return lower(str) |
| 2108 |
else |
| 2109 |
return str |
| 2110 |
end |
| 2111 |
end |
| 2112 |
function string.valid(str,default) |
| 2113 |
return (type(str)=="string" and str~="" and str) or default or nil |
| 2114 |
end |
| 2115 |
string.itself=function(s) return s end |
| 2116 |
local pattern_c=Ct(C(1)^0) |
| 2117 |
local pattern_b=Ct((C(1)/byte)^0) |
| 2118 |
function string.totable(str,bytes) |
| 2119 |
return lpegmatch(bytes and pattern_b or pattern_c,str) |
| 2120 |
end |
| 2121 |
local replacer=lpeg.replacer("@","%%") |
| 2122 |
function string.tformat(fmt,...) |
| 2123 |
return format(lpegmatch(replacer,fmt),...) |
| 2124 |
end |
| 2125 |
string.quote=string.quoted |
| 2126 |
string.unquote=string.unquoted |
| 2127 |
if not string.bytetable then |
| 2128 |
local limit=5000 |
| 2129 |
function string.bytetable(str) |
| 2130 |
local n=#str |
| 2131 |
if n>limit then |
| 2132 |
local t={ byte(str,1,limit) } |
| 2133 |
for i=limit+1,n do |
| 2134 |
t[i]=byte(str,i) |
| 2135 |
end |
| 2136 |
return t |
| 2137 |
else |
| 2138 |
return { byte(str,1,n) } |
| 2139 |
end |
| 2140 |
end |
| 2141 |
end |
| 2142 |
|
| 2143 |
|
| 2144 |
end -- of closure |
| 2145 |
|
| 2146 |
do -- create closure to overcome 200 locals limit |
| 2147 |
|
| 2148 |
package.loaded["l-table"] = package.loaded["l-table"] or true |
| 2149 |
|
| 2150 |
-- original size: 44262, stripped down to: 24008 |
| 2151 |
|
| 2152 |
if not modules then modules={} end modules ['l-table']={ |
| 2153 |
version=1.001, |
| 2154 |
comment="companion to luat-lib.mkiv", |
| 2155 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 2156 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 2157 |
license="see context related readme files" |
| 2158 |
} |
| 2159 |
local type,next,tostring,tonumber,select,rawget=type,next,tostring,tonumber,select,rawget |
| 2160 |
local table,string=table,string |
| 2161 |
local concat,sort=table.concat,table.sort |
| 2162 |
local format,lower,dump=string.format,string.lower,string.dump |
| 2163 |
local getmetatable,setmetatable=getmetatable,setmetatable |
| 2164 |
local lpegmatch,patterns=lpeg.match,lpeg.patterns |
| 2165 |
local floor=math.floor |
| 2166 |
local stripper=patterns.stripper |
| 2167 |
function table.getn(t) |
| 2168 |
return t and #t |
| 2169 |
end |
| 2170 |
function table.strip(tab) |
| 2171 |
local lst={} |
| 2172 |
local l=0 |
| 2173 |
for i=1,#tab do |
| 2174 |
local s=lpegmatch(stripper,tab[i]) or "" |
| 2175 |
if s=="" then |
| 2176 |
else |
| 2177 |
l=l+1 |
| 2178 |
lst[l]=s |
| 2179 |
end |
| 2180 |
end |
| 2181 |
return lst |
| 2182 |
end |
| 2183 |
function table.keys(t) |
| 2184 |
if t then |
| 2185 |
local keys={} |
| 2186 |
local k=0 |
| 2187 |
for key in next,t do |
| 2188 |
k=k+1 |
| 2189 |
keys[k]=key |
| 2190 |
end |
| 2191 |
return keys |
| 2192 |
else |
| 2193 |
return {} |
| 2194 |
end |
| 2195 |
end |
| 2196 |
local function compare(a,b) |
| 2197 |
local ta=type(a) |
| 2198 |
if ta=="number" then |
| 2199 |
local tb=type(b) |
| 2200 |
if ta==tb then |
| 2201 |
return a<b |
| 2202 |
elseif tb=="string" then |
| 2203 |
return tostring(a)<b |
| 2204 |
end |
| 2205 |
elseif ta=="string" then |
| 2206 |
local tb=type(b) |
| 2207 |
if ta==tb then |
| 2208 |
return a<b |
| 2209 |
else |
| 2210 |
return a<tostring(b) |
| 2211 |
end |
| 2212 |
end |
| 2213 |
return tostring(a)<tostring(b) |
| 2214 |
end |
| 2215 |
local function sortedkeys(tab) |
| 2216 |
if tab then |
| 2217 |
local srt={} |
| 2218 |
local category=0 |
| 2219 |
local s=0 |
| 2220 |
for key in next,tab do |
| 2221 |
s=s+1 |
| 2222 |
srt[s]=key |
| 2223 |
if category~=3 then |
| 2224 |
local tkey=type(key) |
| 2225 |
if category==1 then |
| 2226 |
if tkey~="string" then |
| 2227 |
category=3 |
| 2228 |
end |
| 2229 |
elseif category==2 then |
| 2230 |
if tkey~="number" then |
| 2231 |
category=3 |
| 2232 |
end |
| 2233 |
else |
| 2234 |
if tkey=="string" then |
| 2235 |
category=1 |
| 2236 |
elseif tkey=="number" then |
| 2237 |
category=2 |
| 2238 |
else |
| 2239 |
category=3 |
| 2240 |
end |
| 2241 |
end |
| 2242 |
end |
| 2243 |
end |
| 2244 |
if s<2 then |
| 2245 |
elseif category==3 then |
| 2246 |
sort(srt,compare) |
| 2247 |
else |
| 2248 |
sort(srt) |
| 2249 |
end |
| 2250 |
return srt |
| 2251 |
else |
| 2252 |
return {} |
| 2253 |
end |
| 2254 |
end |
| 2255 |
local function sortedhashonly(tab) |
| 2256 |
if tab then |
| 2257 |
local srt={} |
| 2258 |
local s=0 |
| 2259 |
for key in next,tab do |
| 2260 |
if type(key)=="string" then |
| 2261 |
s=s+1 |
| 2262 |
srt[s]=key |
| 2263 |
end |
| 2264 |
end |
| 2265 |
if s>1 then |
| 2266 |
sort(srt) |
| 2267 |
end |
| 2268 |
return srt |
| 2269 |
else |
| 2270 |
return {} |
| 2271 |
end |
| 2272 |
end |
| 2273 |
local function sortedindexonly(tab) |
| 2274 |
if tab then |
| 2275 |
local srt={} |
| 2276 |
local s=0 |
| 2277 |
for key in next,tab do |
| 2278 |
if type(key)=="number" then |
| 2279 |
s=s+1 |
| 2280 |
srt[s]=key |
| 2281 |
end |
| 2282 |
end |
| 2283 |
if s>1 then |
| 2284 |
sort(srt) |
| 2285 |
end |
| 2286 |
return srt |
| 2287 |
else |
| 2288 |
return {} |
| 2289 |
end |
| 2290 |
end |
| 2291 |
local function sortedhashkeys(tab,cmp) |
| 2292 |
if tab then |
| 2293 |
local srt={} |
| 2294 |
local s=0 |
| 2295 |
for key in next,tab do |
| 2296 |
if key then |
| 2297 |
s=s+1 |
| 2298 |
srt[s]=key |
| 2299 |
end |
| 2300 |
end |
| 2301 |
if s>1 then |
| 2302 |
sort(srt,cmp) |
| 2303 |
end |
| 2304 |
return srt |
| 2305 |
else |
| 2306 |
return {} |
| 2307 |
end |
| 2308 |
end |
| 2309 |
function table.allkeys(t) |
| 2310 |
local keys={} |
| 2311 |
for k,v in next,t do |
| 2312 |
for k in next,v do |
| 2313 |
keys[k]=true |
| 2314 |
end |
| 2315 |
end |
| 2316 |
return sortedkeys(keys) |
| 2317 |
end |
| 2318 |
if lua.getcheckedkeys then |
| 2319 |
local getcheckedkeys=lua.getcheckedkeys |
| 2320 |
local getstringkeys=lua.getstringkeys |
| 2321 |
local getnumerickeys=lua.getnumerickeys |
| 2322 |
local getkeys=lua.getallkeys |
| 2323 |
local comparekeys=lua.comparekeys |
| 2324 |
local sort=table.sort |
| 2325 |
sortedkeys=function(tab) |
| 2326 |
if tab then |
| 2327 |
local srt,category=getcheckedkeys(tab) |
| 2328 |
if category==0 then |
| 2329 |
elseif category==3 then |
| 2330 |
sort(srt,comparekeys) |
| 2331 |
else |
| 2332 |
sort(srt) |
| 2333 |
end |
| 2334 |
return srt |
| 2335 |
else |
| 2336 |
return {} |
| 2337 |
end |
| 2338 |
end |
| 2339 |
sortedhashonly=function(tab) |
| 2340 |
if tab then |
| 2341 |
local srt,n=getstringkeys(tab) |
| 2342 |
if n>1 then |
| 2343 |
sort(srt) |
| 2344 |
end |
| 2345 |
return srt |
| 2346 |
else |
| 2347 |
return {} |
| 2348 |
end |
| 2349 |
end |
| 2350 |
sortedindexonly=function(tab) |
| 2351 |
if tab then |
| 2352 |
local srt,n=getnumerickeys(tab) |
| 2353 |
if n>1 then |
| 2354 |
sort(srt) |
| 2355 |
end |
| 2356 |
return srt |
| 2357 |
else |
| 2358 |
return {} |
| 2359 |
end |
| 2360 |
end |
| 2361 |
sortedhashkeys=function(tab,cmp) |
| 2362 |
if tab then |
| 2363 |
local srt,n=getkeys(tab) |
| 2364 |
if n>1 then |
| 2365 |
sort(srt,cmp) |
| 2366 |
end |
| 2367 |
return srt |
| 2368 |
else |
| 2369 |
return {} |
| 2370 |
end |
| 2371 |
end |
| 2372 |
end |
| 2373 |
table.sortedkeys=sortedkeys |
| 2374 |
table.sortedhashonly=sortedhashonly |
| 2375 |
table.sortedindexonly=sortedindexonly |
| 2376 |
table.sortedhashkeys=sortedhashkeys |
| 2377 |
local function nothing() end |
| 2378 |
local function sortedhash(t,cmp) |
| 2379 |
if t then |
| 2380 |
local s |
| 2381 |
if cmp then |
| 2382 |
s=sortedhashkeys(t,function(a,b) return cmp(t,a,b) end) |
| 2383 |
else |
| 2384 |
s=sortedkeys(t) |
| 2385 |
end |
| 2386 |
local m=#s |
| 2387 |
if m==1 then |
| 2388 |
return next,t |
| 2389 |
elseif m>0 then |
| 2390 |
local n=0 |
| 2391 |
return function() |
| 2392 |
if n<m then |
| 2393 |
n=n+1 |
| 2394 |
local k=s[n] |
| 2395 |
return k,t[k] |
| 2396 |
end |
| 2397 |
end |
| 2398 |
end |
| 2399 |
end |
| 2400 |
return nothing |
| 2401 |
end |
| 2402 |
table.sortedhash=sortedhash |
| 2403 |
table.sortedpairs=sortedhash |
| 2404 |
function table.append(t,list) |
| 2405 |
local n=#t |
| 2406 |
for i=1,#list do |
| 2407 |
n=n+1 |
| 2408 |
t[n]=list[i] |
| 2409 |
end |
| 2410 |
return t |
| 2411 |
end |
| 2412 |
function table.prepend(t,list) |
| 2413 |
local nl=#list |
| 2414 |
local nt=nl+#t |
| 2415 |
for i=#t,1,-1 do |
| 2416 |
t[nt]=t[i] |
| 2417 |
nt=nt-1 |
| 2418 |
end |
| 2419 |
for i=1,#list do |
| 2420 |
t[i]=list[i] |
| 2421 |
end |
| 2422 |
return t |
| 2423 |
end |
| 2424 |
function table.merge(t,...) |
| 2425 |
if not t then |
| 2426 |
t={} |
| 2427 |
end |
| 2428 |
for i=1,select("#",...) do |
| 2429 |
for k,v in next,(select(i,...)) do |
| 2430 |
t[k]=v |
| 2431 |
end |
| 2432 |
end |
| 2433 |
return t |
| 2434 |
end |
| 2435 |
function table.merged(...) |
| 2436 |
local t={} |
| 2437 |
for i=1,select("#",...) do |
| 2438 |
for k,v in next,(select(i,...)) do |
| 2439 |
t[k]=v |
| 2440 |
end |
| 2441 |
end |
| 2442 |
return t |
| 2443 |
end |
| 2444 |
function table.imerge(t,...) |
| 2445 |
local nt=#t |
| 2446 |
for i=1,select("#",...) do |
| 2447 |
local nst=select(i,...) |
| 2448 |
for j=1,#nst do |
| 2449 |
nt=nt+1 |
| 2450 |
t[nt]=nst[j] |
| 2451 |
end |
| 2452 |
end |
| 2453 |
return t |
| 2454 |
end |
| 2455 |
function table.imerged(...) |
| 2456 |
local tmp={} |
| 2457 |
local ntmp=0 |
| 2458 |
for i=1,select("#",...) do |
| 2459 |
local nst=select(i,...) |
| 2460 |
for j=1,#nst do |
| 2461 |
ntmp=ntmp+1 |
| 2462 |
tmp[ntmp]=nst[j] |
| 2463 |
end |
| 2464 |
end |
| 2465 |
return tmp |
| 2466 |
end |
| 2467 |
local function fastcopy(old,metatabletoo) |
| 2468 |
if old then |
| 2469 |
local new={} |
| 2470 |
for k,v in next,old do |
| 2471 |
if type(v)=="table" then |
| 2472 |
new[k]=fastcopy(v,metatabletoo) |
| 2473 |
else |
| 2474 |
new[k]=v |
| 2475 |
end |
| 2476 |
end |
| 2477 |
if metatabletoo then |
| 2478 |
local mt=getmetatable(old) |
| 2479 |
if mt then |
| 2480 |
setmetatable(new,mt) |
| 2481 |
end |
| 2482 |
end |
| 2483 |
return new |
| 2484 |
else |
| 2485 |
return {} |
| 2486 |
end |
| 2487 |
end |
| 2488 |
local function copy(t,tables) |
| 2489 |
if not tables then |
| 2490 |
tables={} |
| 2491 |
end |
| 2492 |
local tcopy={} |
| 2493 |
if not tables[t] then |
| 2494 |
tables[t]=tcopy |
| 2495 |
end |
| 2496 |
for i,v in next,t do |
| 2497 |
local k |
| 2498 |
if type(i)=="table" then |
| 2499 |
if tables[i] then |
| 2500 |
k=tables[i] |
| 2501 |
else |
| 2502 |
k=copy(i,tables) |
| 2503 |
end |
| 2504 |
else |
| 2505 |
k=i |
| 2506 |
end |
| 2507 |
if type(v)~="table" then |
| 2508 |
tcopy[k]=v |
| 2509 |
elseif tables[v] then |
| 2510 |
tcopy[k]=tables[v] |
| 2511 |
else |
| 2512 |
tcopy[k]=copy(v,tables) |
| 2513 |
end |
| 2514 |
end |
| 2515 |
local mt=getmetatable(t) |
| 2516 |
if mt then |
| 2517 |
setmetatable(tcopy,mt) |
| 2518 |
end |
| 2519 |
return tcopy |
| 2520 |
end |
| 2521 |
table.fastcopy=fastcopy |
| 2522 |
table.copy=copy |
| 2523 |
function table.derive(parent) |
| 2524 |
local child={} |
| 2525 |
if parent then |
| 2526 |
setmetatable(child,{ __index=parent }) |
| 2527 |
end |
| 2528 |
return child |
| 2529 |
end |
| 2530 |
function table.tohash(t,value) |
| 2531 |
local h={} |
| 2532 |
if t then |
| 2533 |
if value==nil then value=true end |
| 2534 |
for _,v in next,t do |
| 2535 |
h[v]=value |
| 2536 |
end |
| 2537 |
end |
| 2538 |
return h |
| 2539 |
end |
| 2540 |
function table.fromhash(t) |
| 2541 |
local hsh={} |
| 2542 |
local h=0 |
| 2543 |
for k,v in next,t do |
| 2544 |
if v then |
| 2545 |
h=h+1 |
| 2546 |
hsh[h]=k |
| 2547 |
end |
| 2548 |
end |
| 2549 |
return hsh |
| 2550 |
end |
| 2551 |
local noquotes,hexify,handle,compact,inline,functions,metacheck,accurate |
| 2552 |
local reserved=table.tohash { |
| 2553 |
'and','break','do','else','elseif','end','false','for','function','if', |
| 2554 |
'in','local','nil','not','or','repeat','return','then','true','until','while', |
| 2555 |
'NaN','goto','const', |
| 2556 |
} |
| 2557 |
local function is_simple_table(t,hexify,accurate) |
| 2558 |
local nt=#t |
| 2559 |
if nt>0 then |
| 2560 |
local n=0 |
| 2561 |
for _,v in next,t do |
| 2562 |
n=n+1 |
| 2563 |
if type(v)=="table" then |
| 2564 |
return nil |
| 2565 |
end |
| 2566 |
end |
| 2567 |
local haszero=rawget(t,0) |
| 2568 |
if n==nt then |
| 2569 |
local tt={} |
| 2570 |
for i=1,nt do |
| 2571 |
local v=t[i] |
| 2572 |
local tv=type(v) |
| 2573 |
if tv=="number" then |
| 2574 |
if hexify then |
| 2575 |
tt[i]=format("0x%X",v) |
| 2576 |
elseif accurate then |
| 2577 |
tt[i]=format("%q",v) |
| 2578 |
else |
| 2579 |
tt[i]=v |
| 2580 |
end |
| 2581 |
elseif tv=="string" then |
| 2582 |
tt[i]=format("%q",v) |
| 2583 |
elseif tv=="boolean" then |
| 2584 |
tt[i]=v and "true" or "false" |
| 2585 |
else |
| 2586 |
return nil |
| 2587 |
end |
| 2588 |
end |
| 2589 |
return tt |
| 2590 |
elseif haszero and (n==nt+1) then |
| 2591 |
local tt={} |
| 2592 |
for i=0,nt do |
| 2593 |
local v=t[i] |
| 2594 |
local tv=type(v) |
| 2595 |
if tv=="number" then |
| 2596 |
if hexify then |
| 2597 |
tt[i+1]=format("0x%X",v) |
| 2598 |
elseif accurate then |
| 2599 |
tt[i+1]=format("%q",v) |
| 2600 |
else |
| 2601 |
tt[i+1]=v |
| 2602 |
end |
| 2603 |
elseif tv=="string" then |
| 2604 |
tt[i+1]=format("%q",v) |
| 2605 |
elseif tv=="boolean" then |
| 2606 |
tt[i+1]=v and "true" or "false" |
| 2607 |
else |
| 2608 |
return nil |
| 2609 |
end |
| 2610 |
end |
| 2611 |
tt[1]="[0] = "..tt[1] |
| 2612 |
return tt |
| 2613 |
end |
| 2614 |
end |
| 2615 |
return nil |
| 2616 |
end |
| 2617 |
table.is_simple_table=is_simple_table |
| 2618 |
local propername=patterns.propername |
| 2619 |
local function dummy() end |
| 2620 |
local function do_serialize(root,name,depth,level,indexed) |
| 2621 |
if level>0 then |
| 2622 |
depth=depth.." " |
| 2623 |
if indexed then |
| 2624 |
handle(format("%s{",depth)) |
| 2625 |
else |
| 2626 |
local tn=type(name) |
| 2627 |
if tn=="number" then |
| 2628 |
if hexify then |
| 2629 |
handle(format("%s[0x%X]={",depth,name)) |
| 2630 |
else |
| 2631 |
handle(format("%s[%s]={",depth,name)) |
| 2632 |
end |
| 2633 |
elseif tn=="string" then |
| 2634 |
if noquotes and not reserved[name] and lpegmatch(propername,name) then |
| 2635 |
handle(format("%s%s={",depth,name)) |
| 2636 |
else |
| 2637 |
handle(format("%s[%q]={",depth,name)) |
| 2638 |
end |
| 2639 |
elseif tn=="boolean" then |
| 2640 |
handle(format("%s[%s]={",depth,name and "true" or "false")) |
| 2641 |
else |
| 2642 |
handle(format("%s{",depth)) |
| 2643 |
end |
| 2644 |
end |
| 2645 |
end |
| 2646 |
if root and next(root)~=nil then |
| 2647 |
local first=nil |
| 2648 |
local last=0 |
| 2649 |
if compact then |
| 2650 |
last=#root |
| 2651 |
for k=1,last do |
| 2652 |
if rawget(root,k)==nil then |
| 2653 |
last=k-1 |
| 2654 |
break |
| 2655 |
end |
| 2656 |
end |
| 2657 |
if last>0 then |
| 2658 |
first=1 |
| 2659 |
end |
| 2660 |
end |
| 2661 |
local sk=sortedkeys(root) |
| 2662 |
for i=1,#sk do |
| 2663 |
local k=sk[i] |
| 2664 |
local v=root[k] |
| 2665 |
local tv=type(v) |
| 2666 |
local tk=type(k) |
| 2667 |
if compact and first and tk=="number" and k>=first and k<=last then |
| 2668 |
if tv=="number" then |
| 2669 |
if hexify then |
| 2670 |
handle(format("%s 0x%X,",depth,v)) |
| 2671 |
elseif accurate then |
| 2672 |
handle(format("%s %q,",depth,v)) |
| 2673 |
else |
| 2674 |
handle(format("%s %s,",depth,v)) |
| 2675 |
end |
| 2676 |
elseif tv=="string" then |
| 2677 |
handle(format("%s %q,",depth,v)) |
| 2678 |
elseif tv=="table" then |
| 2679 |
if next(v)==nil then |
| 2680 |
handle(format("%s {},",depth)) |
| 2681 |
elseif inline then |
| 2682 |
local st=is_simple_table(v,hexify,accurate) |
| 2683 |
if st then |
| 2684 |
handle(format("%s { %s },",depth,concat(st,", "))) |
| 2685 |
else |
| 2686 |
do_serialize(v,k,depth,level+1,true) |
| 2687 |
end |
| 2688 |
else |
| 2689 |
do_serialize(v,k,depth,level+1,true) |
| 2690 |
end |
| 2691 |
elseif tv=="boolean" then |
| 2692 |
handle(format("%s %s,",depth,v and "true" or "false")) |
| 2693 |
elseif tv=="function" then |
| 2694 |
if functions then |
| 2695 |
handle(format('%s load(%q),',depth,dump(v))) |
| 2696 |
else |
| 2697 |
handle(format('%s "function",',depth)) |
| 2698 |
end |
| 2699 |
else |
| 2700 |
handle(format("%s %q,",depth,tostring(v))) |
| 2701 |
end |
| 2702 |
elseif k=="__p__" then |
| 2703 |
if false then |
| 2704 |
handle(format("%s __p__=nil,",depth)) |
| 2705 |
end |
| 2706 |
elseif tv=="number" then |
| 2707 |
if tk=="number" then |
| 2708 |
if hexify then |
| 2709 |
if accurate then |
| 2710 |
handle(format("%s [0x%X]=%q,",depth,k,v)) |
| 2711 |
else |
| 2712 |
handle(format("%s [0x%X]=%s,",depth,k,v)) |
| 2713 |
end |
| 2714 |
elseif accurate then |
| 2715 |
handle(format("%s [%s]=%q,",depth,k,v)) |
| 2716 |
else |
| 2717 |
handle(format("%s [%s]=%s,",depth,k,v)) |
| 2718 |
end |
| 2719 |
elseif tk=="boolean" then |
| 2720 |
if hexify then |
| 2721 |
if accurate then |
| 2722 |
handle(format("%s [%s]=%q,",depth,k and "true" or "false",v)) |
| 2723 |
else |
| 2724 |
handle(format("%s [%s]=%s,",depth,k and "true" or "false",v)) |
| 2725 |
end |
| 2726 |
elseif accurate then |
| 2727 |
handle(format("%s [%s]=%q,",depth,k and "true" or "false",v)) |
| 2728 |
else |
| 2729 |
handle(format("%s [%s]=%s,",depth,k and "true" or "false",v)) |
| 2730 |
end |
| 2731 |
elseif tk~="string" then |
| 2732 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 2733 |
if hexify then |
| 2734 |
if accurate then |
| 2735 |
handle(format("%s %s=%q,",depth,k,v)) |
| 2736 |
else |
| 2737 |
handle(format("%s %s=0x%X,",depth,k,v)) |
| 2738 |
end |
| 2739 |
elseif accurate then |
| 2740 |
handle(format("%s %s=%q,",depth,k,v)) |
| 2741 |
else |
| 2742 |
handle(format("%s %s=%s,",depth,k,v)) |
| 2743 |
end |
| 2744 |
else |
| 2745 |
if hexify then |
| 2746 |
if accurate then |
| 2747 |
handle(format("%s [%q]=%q,",depth,k,v)) |
| 2748 |
else |
| 2749 |
handle(format("%s [%q]=0x%X,",depth,k,v)) |
| 2750 |
end |
| 2751 |
elseif accurate then |
| 2752 |
handle(format("%s [%q]=%q,",depth,k,v)) |
| 2753 |
else |
| 2754 |
handle(format("%s [%q]=%s,",depth,k,v)) |
| 2755 |
end |
| 2756 |
end |
| 2757 |
elseif tv=="string" then |
| 2758 |
if tk=="number" then |
| 2759 |
if hexify then |
| 2760 |
handle(format("%s [0x%X]=%q,",depth,k,v)) |
| 2761 |
elseif accurate then |
| 2762 |
handle(format("%s [%q]=%q,",depth,k,v)) |
| 2763 |
else |
| 2764 |
handle(format("%s [%s]=%q,",depth,k,v)) |
| 2765 |
end |
| 2766 |
elseif tk=="boolean" then |
| 2767 |
handle(format("%s [%s]=%q,",depth,k and "true" or "false",v)) |
| 2768 |
elseif tk~="string" then |
| 2769 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 2770 |
handle(format("%s %s=%q,",depth,k,v)) |
| 2771 |
else |
| 2772 |
handle(format("%s [%q]=%q,",depth,k,v)) |
| 2773 |
end |
| 2774 |
elseif tv=="table" then |
| 2775 |
if next(v)==nil then |
| 2776 |
if tk=="number" then |
| 2777 |
if hexify then |
| 2778 |
handle(format("%s [0x%X]={},",depth,k)) |
| 2779 |
elseif accurate then |
| 2780 |
handle(format("%s [%q]={},",depth,k)) |
| 2781 |
else |
| 2782 |
handle(format("%s [%s]={},",depth,k)) |
| 2783 |
end |
| 2784 |
elseif tk=="boolean" then |
| 2785 |
handle(format("%s [%s]={},",depth,k and "true" or "false")) |
| 2786 |
elseif tk~="string" then |
| 2787 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 2788 |
handle(format("%s %s={},",depth,k)) |
| 2789 |
else |
| 2790 |
handle(format("%s [%q]={},",depth,k)) |
| 2791 |
end |
| 2792 |
elseif inline then |
| 2793 |
local st=is_simple_table(v,hexify,accurate) |
| 2794 |
if st then |
| 2795 |
if tk=="number" then |
| 2796 |
if hexify then |
| 2797 |
handle(format("%s [0x%X]={ %s },",depth,k,concat(st,", "))) |
| 2798 |
elseif accurate then |
| 2799 |
handle(format("%s [%q]={ %s },",depth,k,concat(st,", "))) |
| 2800 |
else |
| 2801 |
handle(format("%s [%s]={ %s },",depth,k,concat(st,", "))) |
| 2802 |
end |
| 2803 |
elseif tk=="boolean" then |
| 2804 |
handle(format("%s [%s]={ %s },",depth,k and "true" or "false",concat(st,", "))) |
| 2805 |
elseif tk~="string" then |
| 2806 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 2807 |
handle(format("%s %s={ %s },",depth,k,concat(st,", "))) |
| 2808 |
else |
| 2809 |
handle(format("%s [%q]={ %s },",depth,k,concat(st,", "))) |
| 2810 |
end |
| 2811 |
else |
| 2812 |
do_serialize(v,k,depth,level+1) |
| 2813 |
end |
| 2814 |
else |
| 2815 |
do_serialize(v,k,depth,level+1) |
| 2816 |
end |
| 2817 |
elseif tv=="boolean" then |
| 2818 |
if tk=="number" then |
| 2819 |
if hexify then |
| 2820 |
handle(format("%s [0x%X]=%s,",depth,k,v and "true" or "false")) |
| 2821 |
elseif accurate then |
| 2822 |
handle(format("%s [%q]=%s,",depth,k,v and "true" or "false")) |
| 2823 |
else |
| 2824 |
handle(format("%s [%s]=%s,",depth,k,v and "true" or "false")) |
| 2825 |
end |
| 2826 |
elseif tk=="boolean" then |
| 2827 |
handle(format("%s [%s]=%s,",depth,tostring(k),v and "true" or "false")) |
| 2828 |
elseif tk~="string" then |
| 2829 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 2830 |
handle(format("%s %s=%s,",depth,k,v and "true" or "false")) |
| 2831 |
else |
| 2832 |
handle(format("%s [%q]=%s,",depth,k,v and "true" or "false")) |
| 2833 |
end |
| 2834 |
elseif tv=="function" then |
| 2835 |
if functions then |
| 2836 |
local getinfo=debug and debug.getinfo |
| 2837 |
if getinfo then |
| 2838 |
local f=getinfo(v).what=="C" and dump(dummy) or dump(v) |
| 2839 |
if tk=="number" then |
| 2840 |
if hexify then |
| 2841 |
handle(format("%s [0x%X]=load(%q),",depth,k,f)) |
| 2842 |
elseif accurate then |
| 2843 |
handle(format("%s [%q]=load(%q),",depth,k,f)) |
| 2844 |
else |
| 2845 |
handle(format("%s [%s]=load(%q),",depth,k,f)) |
| 2846 |
end |
| 2847 |
elseif tk=="boolean" then |
| 2848 |
handle(format("%s [%s]=load(%q),",depth,k and "true" or "false",f)) |
| 2849 |
elseif tk~="string" then |
| 2850 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 2851 |
handle(format("%s %s=load(%q),",depth,k,f)) |
| 2852 |
else |
| 2853 |
handle(format("%s [%q]=load(%q),",depth,k,f)) |
| 2854 |
end |
| 2855 |
end |
| 2856 |
end |
| 2857 |
else |
| 2858 |
if tk=="number" then |
| 2859 |
if hexify then |
| 2860 |
handle(format("%s [0x%X]=%q,",depth,k,tostring(v))) |
| 2861 |
elseif accurate then |
| 2862 |
handle(format("%s [%q]=%q,",depth,k,tostring(v))) |
| 2863 |
else |
| 2864 |
handle(format("%s [%s]=%q,",depth,k,tostring(v))) |
| 2865 |
end |
| 2866 |
elseif tk=="boolean" then |
| 2867 |
handle(format("%s [%s]=%q,",depth,k and "true" or "false",tostring(v))) |
| 2868 |
elseif tk~="string" then |
| 2869 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 2870 |
handle(format("%s %s=%q,",depth,k,tostring(v))) |
| 2871 |
else |
| 2872 |
handle(format("%s [%q]=%q,",depth,k,tostring(v))) |
| 2873 |
end |
| 2874 |
end |
| 2875 |
end |
| 2876 |
end |
| 2877 |
if level>0 then |
| 2878 |
handle(format("%s},",depth)) |
| 2879 |
end |
| 2880 |
end |
| 2881 |
local function serialize(_handle,root,name,specification) |
| 2882 |
local tname=type(name) |
| 2883 |
if type(specification)=="table" then |
| 2884 |
noquotes=specification.noquotes |
| 2885 |
hexify=specification.hexify |
| 2886 |
accurate=specification.accurate |
| 2887 |
handle=_handle or specification.handle or print |
| 2888 |
functions=specification.functions |
| 2889 |
compact=specification.compact |
| 2890 |
inline=specification.inline and compact |
| 2891 |
metacheck=specification.metacheck |
| 2892 |
if functions==nil then |
| 2893 |
functions=true |
| 2894 |
end |
| 2895 |
if compact==nil then |
| 2896 |
compact=true |
| 2897 |
end |
| 2898 |
if inline==nil then |
| 2899 |
inline=compact |
| 2900 |
end |
| 2901 |
if metacheck==nil then |
| 2902 |
metacheck=true |
| 2903 |
end |
| 2904 |
else |
| 2905 |
noquotes=false |
| 2906 |
hexify=false |
| 2907 |
handle=_handle or print |
| 2908 |
compact=true |
| 2909 |
inline=true |
| 2910 |
functions=true |
| 2911 |
metacheck=true |
| 2912 |
end |
| 2913 |
if tname=="string" then |
| 2914 |
if name=="return" then |
| 2915 |
handle("return {") |
| 2916 |
else |
| 2917 |
handle(name.."={") |
| 2918 |
end |
| 2919 |
elseif tname=="number" then |
| 2920 |
if hexify then |
| 2921 |
handle(format("[0x%X]={",name)) |
| 2922 |
else |
| 2923 |
handle("["..name.."]={") |
| 2924 |
end |
| 2925 |
elseif tname=="boolean" then |
| 2926 |
if name then |
| 2927 |
handle("return {") |
| 2928 |
else |
| 2929 |
handle("{") |
| 2930 |
end |
| 2931 |
else |
| 2932 |
handle("t={") |
| 2933 |
end |
| 2934 |
if root then |
| 2935 |
if metacheck and getmetatable(root) then |
| 2936 |
local dummy=root._w_h_a_t_e_v_e_r_ |
| 2937 |
root._w_h_a_t_e_v_e_r_=nil |
| 2938 |
end |
| 2939 |
if next(root)~=nil then |
| 2940 |
do_serialize(root,name,"",0) |
| 2941 |
end |
| 2942 |
end |
| 2943 |
handle("}") |
| 2944 |
end |
| 2945 |
function table.serialize(root,name,specification) |
| 2946 |
local t={} |
| 2947 |
local n=0 |
| 2948 |
local function flush(s) |
| 2949 |
n=n+1 |
| 2950 |
t[n]=s |
| 2951 |
end |
| 2952 |
serialize(flush,root,name,specification) |
| 2953 |
return concat(t,"\n") |
| 2954 |
end |
| 2955 |
table.tohandle=serialize |
| 2956 |
local maxtab=2*1024 |
| 2957 |
function table.tofile(filename,root,name,specification) |
| 2958 |
local f=io.open(filename,'w') |
| 2959 |
if f then |
| 2960 |
if maxtab>1 then |
| 2961 |
local t={} |
| 2962 |
local n=0 |
| 2963 |
local function flush(s) |
| 2964 |
n=n+1 |
| 2965 |
t[n]=s |
| 2966 |
if n>maxtab then |
| 2967 |
f:write(concat(t,"\n"),"\n") |
| 2968 |
t={} |
| 2969 |
n=0 |
| 2970 |
end |
| 2971 |
end |
| 2972 |
serialize(flush,root,name,specification) |
| 2973 |
f:write(concat(t,"\n"),"\n") |
| 2974 |
else |
| 2975 |
local function flush(s) |
| 2976 |
f:write(s,"\n") |
| 2977 |
end |
| 2978 |
serialize(flush,root,name,specification) |
| 2979 |
end |
| 2980 |
f:close() |
| 2981 |
io.flush() |
| 2982 |
end |
| 2983 |
end |
| 2984 |
local function flattened(t,f,depth) |
| 2985 |
if f==nil then |
| 2986 |
f={} |
| 2987 |
depth=0xFFFF |
| 2988 |
elseif tonumber(f) then |
| 2989 |
depth=f |
| 2990 |
f={} |
| 2991 |
elseif not depth then |
| 2992 |
depth=0xFFFF |
| 2993 |
end |
| 2994 |
for k,v in next,t do |
| 2995 |
if type(k)~="number" then |
| 2996 |
if depth>0 and type(v)=="table" then |
| 2997 |
flattened(v,f,depth-1) |
| 2998 |
else |
| 2999 |
f[#f+1]=v |
| 3000 |
end |
| 3001 |
end |
| 3002 |
end |
| 3003 |
for k=1,#t do |
| 3004 |
local v=t[k] |
| 3005 |
if depth>0 and type(v)=="table" then |
| 3006 |
flattened(v,f,depth-1) |
| 3007 |
else |
| 3008 |
f[#f+1]=v |
| 3009 |
end |
| 3010 |
end |
| 3011 |
return f |
| 3012 |
end |
| 3013 |
table.flattened=flattened |
| 3014 |
local function collapsed(t,f,h) |
| 3015 |
if f==nil then |
| 3016 |
f={} |
| 3017 |
h={} |
| 3018 |
end |
| 3019 |
for k=1,#t do |
| 3020 |
local v=t[k] |
| 3021 |
if type(v)=="table" then |
| 3022 |
collapsed(v,f,h) |
| 3023 |
elseif not h[v] then |
| 3024 |
f[#f+1]=v |
| 3025 |
h[v]=true |
| 3026 |
end |
| 3027 |
end |
| 3028 |
return f |
| 3029 |
end |
| 3030 |
local function collapsedhash(t,h) |
| 3031 |
if h==nil then |
| 3032 |
h={} |
| 3033 |
end |
| 3034 |
for k=1,#t do |
| 3035 |
local v=t[k] |
| 3036 |
if type(v)=="table" then |
| 3037 |
collapsedhash(v,h) |
| 3038 |
else |
| 3039 |
h[v]=true |
| 3040 |
end |
| 3041 |
end |
| 3042 |
return h |
| 3043 |
end |
| 3044 |
table.collapsed=collapsed |
| 3045 |
table.collapsedhash=collapsedhash |
| 3046 |
local function unnest(t,f) |
| 3047 |
if not f then |
| 3048 |
f={} |
| 3049 |
end |
| 3050 |
for i=1,#t do |
| 3051 |
local v=t[i] |
| 3052 |
if type(v)=="table" then |
| 3053 |
if type(v[1])=="table" then |
| 3054 |
unnest(v,f) |
| 3055 |
else |
| 3056 |
f[#f+1]=v |
| 3057 |
end |
| 3058 |
else |
| 3059 |
f[#f+1]=v |
| 3060 |
end |
| 3061 |
end |
| 3062 |
return f |
| 3063 |
end |
| 3064 |
function table.unnest(t) |
| 3065 |
return unnest(t) |
| 3066 |
end |
| 3067 |
local function are_equal(a,b,n,m) |
| 3068 |
if a==b then |
| 3069 |
return true |
| 3070 |
elseif a and b and #a==#b then |
| 3071 |
if not n then |
| 3072 |
n=1 |
| 3073 |
end |
| 3074 |
if not m then |
| 3075 |
m=#a |
| 3076 |
end |
| 3077 |
for i=n,m do |
| 3078 |
local ai,bi=a[i],b[i] |
| 3079 |
if ai==bi then |
| 3080 |
elseif type(ai)=="table" and type(bi)=="table" then |
| 3081 |
if not are_equal(ai,bi) then |
| 3082 |
return false |
| 3083 |
end |
| 3084 |
else |
| 3085 |
return false |
| 3086 |
end |
| 3087 |
end |
| 3088 |
return true |
| 3089 |
else |
| 3090 |
return false |
| 3091 |
end |
| 3092 |
end |
| 3093 |
local function identical(a,b) |
| 3094 |
if a~=b then |
| 3095 |
for ka,va in next,a do |
| 3096 |
local vb=b[ka] |
| 3097 |
if va==vb then |
| 3098 |
elseif type(va)=="table" and type(vb)=="table" then |
| 3099 |
if not identical(va,vb) then |
| 3100 |
return false |
| 3101 |
end |
| 3102 |
else |
| 3103 |
return false |
| 3104 |
end |
| 3105 |
end |
| 3106 |
end |
| 3107 |
return true |
| 3108 |
end |
| 3109 |
table.identical=identical |
| 3110 |
table.are_equal=are_equal |
| 3111 |
local function sparse(old,nest,keeptables) |
| 3112 |
local new={} |
| 3113 |
for k,v in next,old do |
| 3114 |
if not (v=="" or v==false) then |
| 3115 |
if nest and type(v)=="table" then |
| 3116 |
v=sparse(v,nest) |
| 3117 |
if keeptables or next(v)~=nil then |
| 3118 |
new[k]=v |
| 3119 |
end |
| 3120 |
else |
| 3121 |
new[k]=v |
| 3122 |
end |
| 3123 |
end |
| 3124 |
end |
| 3125 |
return new |
| 3126 |
end |
| 3127 |
table.sparse=sparse |
| 3128 |
function table.compact(t) |
| 3129 |
return sparse(t,true,true) |
| 3130 |
end |
| 3131 |
function table.contains(t,v) |
| 3132 |
if t then |
| 3133 |
for i=1,#t do |
| 3134 |
if t[i]==v then |
| 3135 |
return i |
| 3136 |
end |
| 3137 |
end |
| 3138 |
end |
| 3139 |
return false |
| 3140 |
end |
| 3141 |
function table.count(t) |
| 3142 |
local n=0 |
| 3143 |
for k,v in next,t do |
| 3144 |
n=n+1 |
| 3145 |
end |
| 3146 |
return n |
| 3147 |
end |
| 3148 |
function table.swapped(t,s) |
| 3149 |
local n={} |
| 3150 |
if s then |
| 3151 |
for k,v in next,s do |
| 3152 |
n[k]=v |
| 3153 |
end |
| 3154 |
end |
| 3155 |
for k,v in next,t do |
| 3156 |
n[v]=k |
| 3157 |
end |
| 3158 |
return n |
| 3159 |
end |
| 3160 |
function table.hashed(t) |
| 3161 |
for i=1,#t do |
| 3162 |
t[t[i]]=i |
| 3163 |
end |
| 3164 |
return t |
| 3165 |
end |
| 3166 |
function table.mirrored(t) |
| 3167 |
local n={} |
| 3168 |
for k,v in next,t do |
| 3169 |
n[v]=k |
| 3170 |
n[k]=v |
| 3171 |
end |
| 3172 |
return n |
| 3173 |
end |
| 3174 |
function table.reversed(t) |
| 3175 |
if t then |
| 3176 |
local tt={} |
| 3177 |
local tn=#t |
| 3178 |
if tn>0 then |
| 3179 |
local ttn=0 |
| 3180 |
for i=tn,1,-1 do |
| 3181 |
ttn=ttn+1 |
| 3182 |
tt[ttn]=t[i] |
| 3183 |
end |
| 3184 |
end |
| 3185 |
return tt |
| 3186 |
end |
| 3187 |
end |
| 3188 |
function table.reverse(t) |
| 3189 |
if t then |
| 3190 |
local n=#t |
| 3191 |
local m=n+1 |
| 3192 |
for i=1,floor(n/2) do |
| 3193 |
local j=m-i |
| 3194 |
t[i],t[j]=t[j],t[i] |
| 3195 |
end |
| 3196 |
return t |
| 3197 |
end |
| 3198 |
end |
| 3199 |
local function sequenced(t,sep,simple) |
| 3200 |
if not t then |
| 3201 |
return "" |
| 3202 |
elseif type(t)~="table" then |
| 3203 |
return t |
| 3204 |
end |
| 3205 |
local n=#t |
| 3206 |
local s={} |
| 3207 |
if n>0 then |
| 3208 |
for i=1,n do |
| 3209 |
local v=t[i] |
| 3210 |
if type(v)=="table" then |
| 3211 |
s[i]="{"..sequenced(v,sep,simple).."}" |
| 3212 |
else |
| 3213 |
s[i]=tostring(t[i]) |
| 3214 |
end |
| 3215 |
end |
| 3216 |
else |
| 3217 |
n=0 |
| 3218 |
for k,v in sortedhash(t) do |
| 3219 |
if simple then |
| 3220 |
if v==true then |
| 3221 |
n=n+1 |
| 3222 |
s[n]=k |
| 3223 |
elseif v and v~="" then |
| 3224 |
n=n+1 |
| 3225 |
if type(v)=="table" then |
| 3226 |
s[n]=k.."={"..sequenced(v,sep,simple).."}" |
| 3227 |
else |
| 3228 |
s[n]=k.."="..tostring(v) |
| 3229 |
end |
| 3230 |
end |
| 3231 |
else |
| 3232 |
n=n+1 |
| 3233 |
if type(v)=="table" then |
| 3234 |
s[n]=k.."={"..sequenced(v,sep,simple).."}" |
| 3235 |
else |
| 3236 |
s[n]=k.."="..tostring(v) |
| 3237 |
end |
| 3238 |
end |
| 3239 |
end |
| 3240 |
end |
| 3241 |
if sep==true then |
| 3242 |
return "{ "..concat(s,", ").." }" |
| 3243 |
else |
| 3244 |
return concat(s,sep or " | ") |
| 3245 |
end |
| 3246 |
end |
| 3247 |
table.sequenced=sequenced |
| 3248 |
function table.print(t,...) |
| 3249 |
if type(t)~="table" then |
| 3250 |
print(tostring(t)) |
| 3251 |
else |
| 3252 |
serialize(print,t,...) |
| 3253 |
end |
| 3254 |
end |
| 3255 |
if setinspector then |
| 3256 |
setinspector("table",function(v) if type(v)=="table" then serialize(print,v,"table") return true end end) |
| 3257 |
end |
| 3258 |
function table.sub(t,i,j) |
| 3259 |
return { unpack(t,i,j) } |
| 3260 |
end |
| 3261 |
function table.is_empty(t) |
| 3262 |
return not t or next(t)==nil |
| 3263 |
end |
| 3264 |
function table.has_one_entry(t) |
| 3265 |
return t and next(t,next(t))==nil |
| 3266 |
end |
| 3267 |
function table.loweredkeys(t) |
| 3268 |
local l={} |
| 3269 |
for k,v in next,t do |
| 3270 |
l[lower(k)]=v |
| 3271 |
end |
| 3272 |
return l |
| 3273 |
end |
| 3274 |
function table.unique(old) |
| 3275 |
local hash={} |
| 3276 |
local new={} |
| 3277 |
local n=0 |
| 3278 |
for i=1,#old do |
| 3279 |
local oi=old[i] |
| 3280 |
if not hash[oi] then |
| 3281 |
n=n+1 |
| 3282 |
new[n]=oi |
| 3283 |
hash[oi]=true |
| 3284 |
end |
| 3285 |
end |
| 3286 |
return new |
| 3287 |
end |
| 3288 |
function table.sorted(t,...) |
| 3289 |
sort(t,...) |
| 3290 |
return t |
| 3291 |
end |
| 3292 |
function table.values(t,s) |
| 3293 |
if t then |
| 3294 |
local values={} |
| 3295 |
local keys={} |
| 3296 |
local v=0 |
| 3297 |
for key,value in next,t do |
| 3298 |
if not keys[value] then |
| 3299 |
v=v+1 |
| 3300 |
values[v]=value |
| 3301 |
keys[k]=key |
| 3302 |
end |
| 3303 |
end |
| 3304 |
if s then |
| 3305 |
sort(values) |
| 3306 |
end |
| 3307 |
return values |
| 3308 |
else |
| 3309 |
return {} |
| 3310 |
end |
| 3311 |
end |
| 3312 |
function table.filtered(t,pattern,sort,cmp) |
| 3313 |
if t and type(pattern)=="string" then |
| 3314 |
if sort then |
| 3315 |
local s |
| 3316 |
if cmp then |
| 3317 |
s=sortedhashkeys(t,function(a,b) return cmp(t,a,b) end) |
| 3318 |
else |
| 3319 |
s=sortedkeys(t) |
| 3320 |
end |
| 3321 |
local n=0 |
| 3322 |
local m=#s |
| 3323 |
local function kv(s) |
| 3324 |
while n<m do |
| 3325 |
n=n+1 |
| 3326 |
local k=s[n] |
| 3327 |
if find(k,pattern) then |
| 3328 |
return k,t[k] |
| 3329 |
end |
| 3330 |
end |
| 3331 |
end |
| 3332 |
return kv,s |
| 3333 |
else |
| 3334 |
local n=next(t) |
| 3335 |
local function iterator() |
| 3336 |
while n~=nil do |
| 3337 |
local k=n |
| 3338 |
n=next(t,k) |
| 3339 |
if find(k,pattern) then |
| 3340 |
return k,t[k] |
| 3341 |
end |
| 3342 |
end |
| 3343 |
end |
| 3344 |
return iterator,t |
| 3345 |
end |
| 3346 |
else |
| 3347 |
return nothing |
| 3348 |
end |
| 3349 |
end |
| 3350 |
if not table.move then |
| 3351 |
function table.move(a1,f,e,t,a2) |
| 3352 |
if a2 and a1~=a2 then |
| 3353 |
for i=f,e do |
| 3354 |
a2[t]=a1[i] |
| 3355 |
t=t+1 |
| 3356 |
end |
| 3357 |
return a2 |
| 3358 |
else |
| 3359 |
t=t+e-f |
| 3360 |
for i=e,f,-1 do |
| 3361 |
a1[t]=a1[i] |
| 3362 |
t=t-1 |
| 3363 |
end |
| 3364 |
return a1 |
| 3365 |
end |
| 3366 |
end |
| 3367 |
end |
| 3368 |
|
| 3369 |
|
| 3370 |
end -- of closure |
| 3371 |
|
| 3372 |
do -- create closure to overcome 200 locals limit |
| 3373 |
|
| 3374 |
package.loaded["l-io"] = package.loaded["l-io"] or true |
| 3375 |
|
| 3376 |
-- original size: 11988, stripped down to: 6430 |
| 3377 |
|
| 3378 |
if not modules then modules={} end modules ['l-io']={ |
| 3379 |
version=1.001, |
| 3380 |
comment="companion to luat-lib.mkiv", |
| 3381 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 3382 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 3383 |
license="see context related readme files" |
| 3384 |
} |
| 3385 |
local io=io |
| 3386 |
local open,flush,write,read=io.open,io.flush,io.write,io.read |
| 3387 |
local byte,find,gsub,format=string.byte,string.find,string.gsub,string.format |
| 3388 |
local concat=table.concat |
| 3389 |
local type=type |
| 3390 |
if string.find(os.getenv("PATH") or "",";",1,true) then |
| 3391 |
io.fileseparator,io.pathseparator="\\",";" |
| 3392 |
else |
| 3393 |
io.fileseparator,io.pathseparator="/",":" |
| 3394 |
end |
| 3395 |
local large=0x01000000 |
| 3396 |
local medium=0x00100000 |
| 3397 |
local small=0x00020000 |
| 3398 |
local function readall(f) |
| 3399 |
local size=f:seek("end") |
| 3400 |
if size>0 then |
| 3401 |
f:seek("set",0) |
| 3402 |
return f:read(size) |
| 3403 |
else |
| 3404 |
return "" |
| 3405 |
end |
| 3406 |
end |
| 3407 |
io.readall=readall |
| 3408 |
function io.loaddata(filename,textmode) |
| 3409 |
local f=open(filename,(textmode and 'r') or 'rb') |
| 3410 |
if f then |
| 3411 |
local size=f:seek("end") |
| 3412 |
local data=nil |
| 3413 |
if size>0 then |
| 3414 |
f:seek("set",0) |
| 3415 |
data=f:read(size) |
| 3416 |
end |
| 3417 |
f:close() |
| 3418 |
return data |
| 3419 |
end |
| 3420 |
end |
| 3421 |
function io.copydata(source,target,action) |
| 3422 |
local f=open(source,"rb") |
| 3423 |
if f then |
| 3424 |
local g=open(target,"wb") |
| 3425 |
if g then |
| 3426 |
local size=f:seek("end") |
| 3427 |
if size>0 then |
| 3428 |
f:seek("set",0) |
| 3429 |
local data=f:read(size) |
| 3430 |
if action then |
| 3431 |
data=action(data) |
| 3432 |
end |
| 3433 |
if data then |
| 3434 |
g:write(data) |
| 3435 |
end |
| 3436 |
end |
| 3437 |
g:close() |
| 3438 |
end |
| 3439 |
f:close() |
| 3440 |
flush() |
| 3441 |
end |
| 3442 |
end |
| 3443 |
function io.savedata(filename,data,joiner,append) |
| 3444 |
local f=open(filename,append and "ab" or "wb") |
| 3445 |
if f then |
| 3446 |
if append and joiner and f:seek("end")>0 then |
| 3447 |
f:write(joiner) |
| 3448 |
end |
| 3449 |
if type(data)=="table" then |
| 3450 |
f:write(concat(data,joiner or "")) |
| 3451 |
elseif type(data)=="function" then |
| 3452 |
data(f) |
| 3453 |
else |
| 3454 |
f:write(data or "") |
| 3455 |
end |
| 3456 |
f:close() |
| 3457 |
flush() |
| 3458 |
return true |
| 3459 |
else |
| 3460 |
return false |
| 3461 |
end |
| 3462 |
end |
| 3463 |
if fio and fio.readline then |
| 3464 |
local readline=fio.readline |
| 3465 |
function io.loadlines(filename,n) |
| 3466 |
local f=open(filename,'r') |
| 3467 |
if not f then |
| 3468 |
elseif n then |
| 3469 |
local lines={} |
| 3470 |
for i=1,n do |
| 3471 |
local line=readline(f) |
| 3472 |
if line then |
| 3473 |
lines[i]=line |
| 3474 |
else |
| 3475 |
break |
| 3476 |
end |
| 3477 |
end |
| 3478 |
f:close() |
| 3479 |
lines=concat(lines,"\n") |
| 3480 |
if #lines>0 then |
| 3481 |
return lines |
| 3482 |
end |
| 3483 |
else |
| 3484 |
local line=readline(f) |
| 3485 |
f:close() |
| 3486 |
if line and #line>0 then |
| 3487 |
return line |
| 3488 |
end |
| 3489 |
end |
| 3490 |
end |
| 3491 |
else |
| 3492 |
function io.loadlines(filename,n) |
| 3493 |
local f=open(filename,'r') |
| 3494 |
if not f then |
| 3495 |
elseif n then |
| 3496 |
local lines={} |
| 3497 |
for i=1,n do |
| 3498 |
local line=f:read("*lines") |
| 3499 |
if line then |
| 3500 |
lines[i]=line |
| 3501 |
else |
| 3502 |
break |
| 3503 |
end |
| 3504 |
end |
| 3505 |
f:close() |
| 3506 |
lines=concat(lines,"\n") |
| 3507 |
if #lines>0 then |
| 3508 |
return lines |
| 3509 |
end |
| 3510 |
else |
| 3511 |
local line=f:read("*line") or "" |
| 3512 |
f:close() |
| 3513 |
if #line>0 then |
| 3514 |
return line |
| 3515 |
end |
| 3516 |
end |
| 3517 |
end |
| 3518 |
end |
| 3519 |
function io.loadchunk(filename,n) |
| 3520 |
local f=open(filename,'rb') |
| 3521 |
if f then |
| 3522 |
local data=f:read(n or 1024) |
| 3523 |
f:close() |
| 3524 |
if #data>0 then |
| 3525 |
return data |
| 3526 |
end |
| 3527 |
end |
| 3528 |
end |
| 3529 |
function io.exists(filename) |
| 3530 |
local f=open(filename) |
| 3531 |
if f==nil then |
| 3532 |
return false |
| 3533 |
else |
| 3534 |
f:close() |
| 3535 |
return true |
| 3536 |
end |
| 3537 |
end |
| 3538 |
function io.size(filename) |
| 3539 |
local f=open(filename) |
| 3540 |
if f==nil then |
| 3541 |
return 0 |
| 3542 |
else |
| 3543 |
local s=f:seek("end") |
| 3544 |
f:close() |
| 3545 |
return s |
| 3546 |
end |
| 3547 |
end |
| 3548 |
local function noflines(f) |
| 3549 |
if type(f)=="string" then |
| 3550 |
local f=open(filename) |
| 3551 |
if f then |
| 3552 |
local n=f and noflines(f) or 0 |
| 3553 |
f:close() |
| 3554 |
return n |
| 3555 |
else |
| 3556 |
return 0 |
| 3557 |
end |
| 3558 |
else |
| 3559 |
local n=0 |
| 3560 |
for _ in f:lines() do |
| 3561 |
n=n+1 |
| 3562 |
end |
| 3563 |
f:seek('set',0) |
| 3564 |
return n |
| 3565 |
end |
| 3566 |
end |
| 3567 |
io.noflines=noflines |
| 3568 |
local nextchar={ |
| 3569 |
[ 4]=function(f) |
| 3570 |
return f:read(1,1,1,1) |
| 3571 |
end, |
| 3572 |
[ 2]=function(f) |
| 3573 |
return f:read(1,1) |
| 3574 |
end, |
| 3575 |
[ 1]=function(f) |
| 3576 |
return f:read(1) |
| 3577 |
end, |
| 3578 |
[-2]=function(f) |
| 3579 |
local a,b=f:read(1,1) |
| 3580 |
return b,a |
| 3581 |
end, |
| 3582 |
[-4]=function(f) |
| 3583 |
local a,b,c,d=f:read(1,1,1,1) |
| 3584 |
return d,c,b,a |
| 3585 |
end |
| 3586 |
} |
| 3587 |
function io.characters(f,n) |
| 3588 |
if f then |
| 3589 |
return nextchar[n or 1],f |
| 3590 |
end |
| 3591 |
end |
| 3592 |
local nextbyte={ |
| 3593 |
[4]=function(f) |
| 3594 |
local a,b,c,d=f:read(1,1,1,1) |
| 3595 |
if d then |
| 3596 |
return byte(a),byte(b),byte(c),byte(d) |
| 3597 |
end |
| 3598 |
end, |
| 3599 |
[3]=function(f) |
| 3600 |
local a,b,c=f:read(1,1,1) |
| 3601 |
if b then |
| 3602 |
return byte(a),byte(b),byte(c) |
| 3603 |
end |
| 3604 |
end, |
| 3605 |
[2]=function(f) |
| 3606 |
local a,b=f:read(1,1) |
| 3607 |
if b then |
| 3608 |
return byte(a),byte(b) |
| 3609 |
end |
| 3610 |
end, |
| 3611 |
[1]=function (f) |
| 3612 |
local a=f:read(1) |
| 3613 |
if a then |
| 3614 |
return byte(a) |
| 3615 |
end |
| 3616 |
end, |
| 3617 |
[-2]=function (f) |
| 3618 |
local a,b=f:read(1,1) |
| 3619 |
if b then |
| 3620 |
return byte(b),byte(a) |
| 3621 |
end |
| 3622 |
end, |
| 3623 |
[-3]=function(f) |
| 3624 |
local a,b,c=f:read(1,1,1) |
| 3625 |
if b then |
| 3626 |
return byte(c),byte(b),byte(a) |
| 3627 |
end |
| 3628 |
end, |
| 3629 |
[-4]=function(f) |
| 3630 |
local a,b,c,d=f:read(1,1,1,1) |
| 3631 |
if d then |
| 3632 |
return byte(d),byte(c),byte(b),byte(a) |
| 3633 |
end |
| 3634 |
end |
| 3635 |
} |
| 3636 |
function io.bytes(f,n) |
| 3637 |
if f then |
| 3638 |
return nextbyte[n or 1],f |
| 3639 |
else |
| 3640 |
return nil,nil |
| 3641 |
end |
| 3642 |
end |
| 3643 |
function io.ask(question,default,options) |
| 3644 |
while true do |
| 3645 |
write(question) |
| 3646 |
if options then |
| 3647 |
write(format(" [%s]",concat(options,"|"))) |
| 3648 |
end |
| 3649 |
if default then |
| 3650 |
write(format(" [%s]",default)) |
| 3651 |
end |
| 3652 |
write(format(" ")) |
| 3653 |
flush() |
| 3654 |
local answer=read() |
| 3655 |
answer=gsub(answer,"^%s*(.*)%s*$","%1") |
| 3656 |
if answer=="" and default then |
| 3657 |
return default |
| 3658 |
elseif not options then |
| 3659 |
return answer |
| 3660 |
else |
| 3661 |
for k=1,#options do |
| 3662 |
if options[k]==answer then |
| 3663 |
return answer |
| 3664 |
end |
| 3665 |
end |
| 3666 |
local pattern="^"..answer |
| 3667 |
for k=1,#options do |
| 3668 |
local v=options[k] |
| 3669 |
if find(v,pattern) then |
| 3670 |
return v |
| 3671 |
end |
| 3672 |
end |
| 3673 |
end |
| 3674 |
end |
| 3675 |
end |
| 3676 |
local function readnumber(f,n,m) |
| 3677 |
if m then |
| 3678 |
f:seek("set",n) |
| 3679 |
n=m |
| 3680 |
end |
| 3681 |
if n==1 then |
| 3682 |
return byte(f:read(1)) |
| 3683 |
elseif n==2 then |
| 3684 |
local a,b=byte(f:read(2),1,2) |
| 3685 |
return 0x100*a+b |
| 3686 |
elseif n==3 then |
| 3687 |
local a,b,c=byte(f:read(3),1,3) |
| 3688 |
return 0x10000*a+0x100*b+c |
| 3689 |
elseif n==4 then |
| 3690 |
local a,b,c,d=byte(f:read(4),1,4) |
| 3691 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 3692 |
elseif n==8 then |
| 3693 |
local a,b=readnumber(f,4),readnumber(f,4) |
| 3694 |
return 0x100*a+b |
| 3695 |
elseif n==12 then |
| 3696 |
local a,b,c=readnumber(f,4),readnumber(f,4),readnumber(f,4) |
| 3697 |
return 0x10000*a+0x100*b+c |
| 3698 |
elseif n==-2 then |
| 3699 |
local b,a=byte(f:read(2),1,2) |
| 3700 |
return 0x100*a+b |
| 3701 |
elseif n==-3 then |
| 3702 |
local c,b,a=byte(f:read(3),1,3) |
| 3703 |
return 0x10000*a+0x100*b+c |
| 3704 |
elseif n==-4 then |
| 3705 |
local d,c,b,a=byte(f:read(4),1,4) |
| 3706 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 3707 |
elseif n==-8 then |
| 3708 |
local h,g,f,e,d,c,b,a=byte(f:read(8),1,8) |
| 3709 |
return 0x100000000000000*a+0x1000000000000*b+0x10000000000*c+0x100000000*d+0x1000000*e+0x10000*f+0x100*g+h |
| 3710 |
else |
| 3711 |
return 0 |
| 3712 |
end |
| 3713 |
end |
| 3714 |
io.readnumber=readnumber |
| 3715 |
function io.readstring(f,n,m) |
| 3716 |
if m then |
| 3717 |
f:seek("set",n) |
| 3718 |
n=m |
| 3719 |
end |
| 3720 |
local str=gsub(f:read(n),"\000","") |
| 3721 |
return str |
| 3722 |
end |
| 3723 |
|
| 3724 |
|
| 3725 |
end -- of closure |
| 3726 |
|
| 3727 |
do -- create closure to overcome 200 locals limit |
| 3728 |
|
| 3729 |
package.loaded["l-number"] = package.loaded["l-number"] or true |
| 3730 |
|
| 3731 |
-- original size: 4588, stripped down to: 2159 |
| 3732 |
|
| 3733 |
if not modules then modules={} end modules ['l-number']={ |
| 3734 |
version=1.001, |
| 3735 |
comment="companion to luat-lib.mkxl", |
| 3736 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 3737 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 3738 |
license="see context related readme files" |
| 3739 |
} |
| 3740 |
local tostring,tonumber=tostring,tonumber |
| 3741 |
local format,match,rep=string.format,string.match,string.rep |
| 3742 |
local concat,insert=table.concat,table.insert |
| 3743 |
local lpegmatch=lpeg.match |
| 3744 |
local floor=math.floor |
| 3745 |
number=number or {} |
| 3746 |
local number=number |
| 3747 |
if bit32 then |
| 3748 |
local bextract=bit32.extract |
| 3749 |
local t={ |
| 3750 |
"0","0","0","0","0","0","0","0", |
| 3751 |
"0","0","0","0","0","0","0","0", |
| 3752 |
"0","0","0","0","0","0","0","0", |
| 3753 |
"0","0","0","0","0","0","0","0", |
| 3754 |
} |
| 3755 |
function number.tobitstring(b,m,w) |
| 3756 |
if not w then |
| 3757 |
w=32 |
| 3758 |
end |
| 3759 |
local n=w |
| 3760 |
for i=0,w-1 do |
| 3761 |
local v=bextract(b,i) |
| 3762 |
local k=w-i |
| 3763 |
if v==1 then |
| 3764 |
n=k |
| 3765 |
t[k]="1" |
| 3766 |
else |
| 3767 |
t[k]="0" |
| 3768 |
end |
| 3769 |
end |
| 3770 |
if w then |
| 3771 |
return concat(t,"",1,w) |
| 3772 |
elseif m then |
| 3773 |
m=33-m*8 |
| 3774 |
if m<1 then |
| 3775 |
m=1 |
| 3776 |
end |
| 3777 |
return concat(t,"",1,m) |
| 3778 |
elseif n<8 then |
| 3779 |
return concat(t) |
| 3780 |
elseif n<16 then |
| 3781 |
return concat(t,"",9) |
| 3782 |
elseif n<24 then |
| 3783 |
return concat(t,"",17) |
| 3784 |
else |
| 3785 |
return concat(t,"",25) |
| 3786 |
end |
| 3787 |
end |
| 3788 |
else |
| 3789 |
function number.tobitstring(n,m) |
| 3790 |
if n>0 then |
| 3791 |
local t={} |
| 3792 |
while n>0 do |
| 3793 |
insert(t,1,n%2>0 and 1 or 0) |
| 3794 |
n=floor(n/2) |
| 3795 |
end |
| 3796 |
local nn=8-#t%8 |
| 3797 |
if nn>0 and nn<8 then |
| 3798 |
for i=1,nn do |
| 3799 |
insert(t,1,0) |
| 3800 |
end |
| 3801 |
end |
| 3802 |
if m then |
| 3803 |
m=m*8-#t |
| 3804 |
if m>0 then |
| 3805 |
insert(t,1,rep("0",m)) |
| 3806 |
end |
| 3807 |
end |
| 3808 |
return concat(t) |
| 3809 |
elseif m then |
| 3810 |
rep("00000000",m) |
| 3811 |
else |
| 3812 |
return "00000000" |
| 3813 |
end |
| 3814 |
end |
| 3815 |
end |
| 3816 |
function number.valid(str,default) |
| 3817 |
return tonumber(str) or default or nil |
| 3818 |
end |
| 3819 |
function number.toevenhex(n) |
| 3820 |
local s=format("%X",n) |
| 3821 |
if #s%2==0 then |
| 3822 |
return s |
| 3823 |
else |
| 3824 |
return "0"..s |
| 3825 |
end |
| 3826 |
end |
| 3827 |
function number.bytetodecimal(b) |
| 3828 |
local d=floor(b*100/255+0.5) |
| 3829 |
if d>100 then |
| 3830 |
return 100 |
| 3831 |
elseif d<-100 then |
| 3832 |
return -100 |
| 3833 |
else |
| 3834 |
return d |
| 3835 |
end |
| 3836 |
end |
| 3837 |
function number.decimaltobyte(d) |
| 3838 |
local b=floor(d*255/100+0.5) |
| 3839 |
if b>255 then |
| 3840 |
return 255 |
| 3841 |
elseif b<-255 then |
| 3842 |
return -255 |
| 3843 |
else |
| 3844 |
return b |
| 3845 |
end |
| 3846 |
end |
| 3847 |
function number.idiv(i,d) |
| 3848 |
return floor(i/d) |
| 3849 |
end |
| 3850 |
|
| 3851 |
|
| 3852 |
end -- of closure |
| 3853 |
|
| 3854 |
do -- create closure to overcome 200 locals limit |
| 3855 |
|
| 3856 |
package.loaded["l-set"] = package.loaded["l-set"] or true |
| 3857 |
|
| 3858 |
-- original size: 1923, stripped down to: 1044 |
| 3859 |
|
| 3860 |
if not modules then modules={} end modules ['l-set']={ |
| 3861 |
version=1.001, |
| 3862 |
comment="companion to luat-lib.mkiv", |
| 3863 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 3864 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 3865 |
license="see context related readme files" |
| 3866 |
} |
| 3867 |
set=set or {} |
| 3868 |
local nums={} |
| 3869 |
local tabs={} |
| 3870 |
local concat=table.concat |
| 3871 |
local next,type=next,type |
| 3872 |
set.create=table.tohash |
| 3873 |
function set.tonumber(t) |
| 3874 |
if next(t) then |
| 3875 |
local s="" |
| 3876 |
for k,v in next,t do |
| 3877 |
if v then |
| 3878 |
s=s.." "..k |
| 3879 |
end |
| 3880 |
end |
| 3881 |
local n=nums[s] |
| 3882 |
if not n then |
| 3883 |
n=#tabs+1 |
| 3884 |
tabs[n]=t |
| 3885 |
nums[s]=n |
| 3886 |
end |
| 3887 |
return n |
| 3888 |
else |
| 3889 |
return 0 |
| 3890 |
end |
| 3891 |
end |
| 3892 |
function set.totable(n) |
| 3893 |
if n==0 then |
| 3894 |
return {} |
| 3895 |
else |
| 3896 |
return tabs[n] or {} |
| 3897 |
end |
| 3898 |
end |
| 3899 |
function set.tolist(n) |
| 3900 |
if n==0 or not tabs[n] then |
| 3901 |
return "" |
| 3902 |
else |
| 3903 |
local t,n={},0 |
| 3904 |
for k,v in next,tabs[n] do |
| 3905 |
if v then |
| 3906 |
n=n+1 |
| 3907 |
t[n]=k |
| 3908 |
end |
| 3909 |
end |
| 3910 |
return concat(t," ") |
| 3911 |
end |
| 3912 |
end |
| 3913 |
function set.contains(n,s) |
| 3914 |
if type(n)=="table" then |
| 3915 |
return n[s] |
| 3916 |
elseif n==0 then |
| 3917 |
return false |
| 3918 |
else |
| 3919 |
local t=tabs[n] |
| 3920 |
return t and t[s] |
| 3921 |
end |
| 3922 |
end |
| 3923 |
|
| 3924 |
|
| 3925 |
end -- of closure |
| 3926 |
|
| 3927 |
do -- create closure to overcome 200 locals limit |
| 3928 |
|
| 3929 |
package.loaded["l-os"] = package.loaded["l-os"] or true |
| 3930 |
|
| 3931 |
-- original size: 20908, stripped down to: 10884 |
| 3932 |
|
| 3933 |
if not modules then modules={} end modules ['l-os']={ |
| 3934 |
version=1.001, |
| 3935 |
comment="companion to luat-lib.mkiv", |
| 3936 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 3937 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 3938 |
license="see context related readme files" |
| 3939 |
} |
| 3940 |
local os=os |
| 3941 |
local date,time,difftime=os.date,os.time,os.difftime |
| 3942 |
local find,format,gsub,upper,gmatch=string.find,string.format,string.gsub,string.upper,string.gmatch |
| 3943 |
local concat=table.concat |
| 3944 |
local random,ceil,randomseed,modf=math.random,math.ceil,math.randomseed,math.modf |
| 3945 |
local type,setmetatable,tonumber,tostring=type,setmetatable,tonumber,tostring |
| 3946 |
do |
| 3947 |
local selfdir=os.selfdir |
| 3948 |
if selfdir=="" then |
| 3949 |
selfdir=nil |
| 3950 |
end |
| 3951 |
if not selfdir then |
| 3952 |
if arg then |
| 3953 |
for i=1,#arg do |
| 3954 |
local a=arg[i] |
| 3955 |
if find(a,"^%-%-[c:]*texmfbinpath=") then |
| 3956 |
selfdir=gsub(a,"^.-=","") |
| 3957 |
break |
| 3958 |
end |
| 3959 |
end |
| 3960 |
end |
| 3961 |
if not selfdir then |
| 3962 |
selfdir=os.selfbin or "luatex" |
| 3963 |
if find(selfdir,"[/\\]") then |
| 3964 |
selfdir=gsub(selfdir,"[/\\][^/\\]*$","") |
| 3965 |
elseif os.getenv then |
| 3966 |
local path=os.getenv("PATH") |
| 3967 |
local name=gsub(selfdir,"^.*[/\\][^/\\]","") |
| 3968 |
local patt="[^:]+" |
| 3969 |
if os.type=="windows" then |
| 3970 |
patt="[^;]+" |
| 3971 |
name=name..".exe" |
| 3972 |
end |
| 3973 |
local isfile |
| 3974 |
if lfs then |
| 3975 |
local attributes=lfs.attributes |
| 3976 |
isfile=function(name) |
| 3977 |
local a=attributes(name,"mode") |
| 3978 |
return a=="file" or a=="link" or nil |
| 3979 |
end |
| 3980 |
else |
| 3981 |
local open=io.open |
| 3982 |
isfile=function(name) |
| 3983 |
local f=open(name) |
| 3984 |
if f then |
| 3985 |
f:close() |
| 3986 |
return true |
| 3987 |
end |
| 3988 |
end |
| 3989 |
end |
| 3990 |
for p in gmatch(path,patt) do |
| 3991 |
if isfile(p.."/"..name) then |
| 3992 |
selfdir=p |
| 3993 |
break |
| 3994 |
end |
| 3995 |
end |
| 3996 |
end |
| 3997 |
end |
| 3998 |
os.selfdir=selfdir or "." |
| 3999 |
end |
| 4000 |
end |
| 4001 |
math.initialseed=tonumber(string.sub(string.reverse(tostring(ceil(socket and socket.gettime()*10000 or time()))),1,6)) |
| 4002 |
randomseed(math.initialseed) |
| 4003 |
if not os.__getenv__ then |
| 4004 |
os.__getenv__=os.getenv |
| 4005 |
os.__setenv__=os.setenv |
| 4006 |
if os.env then |
| 4007 |
local osgetenv=os.getenv |
| 4008 |
local ossetenv=os.setenv |
| 4009 |
local osenv=os.env local _=osenv.PATH |
| 4010 |
function os.setenv(k,v) |
| 4011 |
if v==nil then |
| 4012 |
v="" |
| 4013 |
end |
| 4014 |
local K=upper(k) |
| 4015 |
osenv[K]=v |
| 4016 |
if type(v)=="table" then |
| 4017 |
v=concat(v,";") |
| 4018 |
end |
| 4019 |
ossetenv(K,v) |
| 4020 |
end |
| 4021 |
function os.getenv(k) |
| 4022 |
local K=upper(k) |
| 4023 |
local v=osenv[K] or osenv[k] or osgetenv(K) or osgetenv(k) |
| 4024 |
if v=="" then |
| 4025 |
return nil |
| 4026 |
else |
| 4027 |
return v |
| 4028 |
end |
| 4029 |
end |
| 4030 |
else |
| 4031 |
local ossetenv=os.setenv |
| 4032 |
local osgetenv=os.getenv |
| 4033 |
local osenv={} |
| 4034 |
function os.setenv(k,v) |
| 4035 |
if v==nil then |
| 4036 |
v="" |
| 4037 |
end |
| 4038 |
local K=upper(k) |
| 4039 |
osenv[K]=v |
| 4040 |
end |
| 4041 |
function os.getenv(k) |
| 4042 |
local K=upper(k) |
| 4043 |
local v=osenv[K] or osgetenv(K) or osgetenv(k) |
| 4044 |
if v=="" then |
| 4045 |
return nil |
| 4046 |
else |
| 4047 |
return v |
| 4048 |
end |
| 4049 |
end |
| 4050 |
local function __index(t,k) |
| 4051 |
return os.getenv(k) |
| 4052 |
end |
| 4053 |
local function __newindex(t,k,v) |
| 4054 |
os.setenv(k,v) |
| 4055 |
end |
| 4056 |
os.env={} |
| 4057 |
setmetatable(os.env,{ __index=__index,__newindex=__newindex } ) |
| 4058 |
end |
| 4059 |
end |
| 4060 |
if not io.fileseparator then |
| 4061 |
if find(os.getenv("PATH"),";",1,true) then |
| 4062 |
io.fileseparator,io.pathseparator,os.type="\\",";",os.type or "windows" |
| 4063 |
else |
| 4064 |
io.fileseparator,io.pathseparator,os.type="/",":",os.type or "unix" |
| 4065 |
end |
| 4066 |
end |
| 4067 |
os.type=os.type or (io.pathseparator==";" and "windows") or "unix" |
| 4068 |
os.name=os.name or (os.type=="windows" and "mswin" ) or "linux" |
| 4069 |
if os.type=="windows" then |
| 4070 |
os.libsuffix,os.binsuffix,os.binsuffixes='dll','exe',{ 'exe','cmd','bat' } |
| 4071 |
elseif os.name=="macosx" then |
| 4072 |
os.libsuffix,os.binsuffix,os.binsuffixes='dylib','',{ '' } |
| 4073 |
else |
| 4074 |
os.libsuffix,os.binsuffix,os.binsuffixes='so','',{ '' } |
| 4075 |
end |
| 4076 |
do |
| 4077 |
local execute=os.execute |
| 4078 |
local iopopen=io.popen |
| 4079 |
local ostype=os.type |
| 4080 |
local function resultof(command) |
| 4081 |
local handle=iopopen(command,"r") |
| 4082 |
if handle then |
| 4083 |
local result=handle:read("*all") or "" |
| 4084 |
handle:close() |
| 4085 |
return result |
| 4086 |
else |
| 4087 |
return "" |
| 4088 |
end |
| 4089 |
end |
| 4090 |
os.resultof=resultof |
| 4091 |
function os.pipeto(command) |
| 4092 |
return iopopen(command,"w") |
| 4093 |
end |
| 4094 |
local launchers={ |
| 4095 |
windows="start %s", |
| 4096 |
macosx="open %s", |
| 4097 |
unix="xdg-open %s &> /dev/null &", |
| 4098 |
} |
| 4099 |
function os.launch(str) |
| 4100 |
local command=format(launchers[os.name] or launchers.unix,str) |
| 4101 |
execute(command) |
| 4102 |
end |
| 4103 |
end |
| 4104 |
do |
| 4105 |
local gettimeofday=os.gettimeofday or os.clock |
| 4106 |
os.gettimeofday=gettimeofday |
| 4107 |
local startuptime=gettimeofday() |
| 4108 |
function os.runtime() |
| 4109 |
return gettimeofday()-startuptime |
| 4110 |
end |
| 4111 |
function os.startuptime() |
| 4112 |
return startuptime |
| 4113 |
end |
| 4114 |
end |
| 4115 |
do |
| 4116 |
local name=os.name or "linux" |
| 4117 |
local platform=os.getenv("MTX_PLATFORM") or "" |
| 4118 |
local architecture=os.uname and os.uname().machine |
| 4119 |
local bits=os.getenv("MTX_BITS") or find(platform,"64") and 64 or 32 |
| 4120 |
if platform~="" then |
| 4121 |
elseif os.type=="windows" then |
| 4122 |
architecture=string.lower(architecture or os.getenv("PROCESSOR_ARCHITECTURE") or "") |
| 4123 |
if architecture=="x86_64" then |
| 4124 |
bits,platform=64,"win64" |
| 4125 |
elseif find(architecture,"amd64") then |
| 4126 |
bits,platform=64,"win64" |
| 4127 |
elseif find(architecture,"arm64") then |
| 4128 |
bits,platform=64,"windows-arm64" |
| 4129 |
elseif find(architecture,"arm32") then |
| 4130 |
bits,platform=32,"windows-arm32" |
| 4131 |
else |
| 4132 |
bits,platform=32,"mswin" |
| 4133 |
end |
| 4134 |
elseif name=="linux" then |
| 4135 |
architecture=architecture or os.getenv("HOSTTYPE") or resultof("uname -m") or "" |
| 4136 |
local musl=find(os.selfdir or "","linuxmusl") |
| 4137 |
if find(architecture,"x86_64") then |
| 4138 |
bits,platform=64,musl and "linuxmusl-64" or "linux-64" |
| 4139 |
elseif find(architecture,"ppc") then |
| 4140 |
bits,platform=32,"linux-ppc" |
| 4141 |
else |
| 4142 |
bits,platform=32,musl and "linuxmusl" or "linux" |
| 4143 |
end |
| 4144 |
elseif name=="macosx" then |
| 4145 |
architecture=architecture or resultof("echo $HOSTTYPE") or "" |
| 4146 |
if architecture=="" then |
| 4147 |
bits,platform=64,"osx-intel" |
| 4148 |
elseif find(architecture,"i386") then |
| 4149 |
bits,platform=64,"osx-intel" |
| 4150 |
elseif find(architecture,"x86_64") then |
| 4151 |
bits,platform=64,"osx-64" |
| 4152 |
elseif find(architecture,"arm64") then |
| 4153 |
bits,platform=64,"osx-arm" |
| 4154 |
else |
| 4155 |
bits,platform=32,"osx-ppc" |
| 4156 |
end |
| 4157 |
elseif name=="sunos" then |
| 4158 |
architecture=architecture or resultof("uname -m") or "" |
| 4159 |
if find(architecture,"sparc") then |
| 4160 |
bits,platform=32,"solaris-sparc" |
| 4161 |
else |
| 4162 |
bits,platform=32,"solaris-intel" |
| 4163 |
end |
| 4164 |
elseif name=="freebsd" then |
| 4165 |
architecture=architecture or os.getenv("MACHTYPE") or resultof("uname -m") or "" |
| 4166 |
if find(architecture,"amd64") or find(architecture,"AMD64") then |
| 4167 |
bits,platform=64,"freebsd-amd64" |
| 4168 |
else |
| 4169 |
bits,platform=32,"freebsd" |
| 4170 |
end |
| 4171 |
elseif name=="kfreebsd" then |
| 4172 |
architecture=architecture or os.getenv("HOSTTYPE") or resultof("uname -m") or "" |
| 4173 |
if architecture=="x86_64" then |
| 4174 |
bits,platform=64,"kfreebsd-amd64" |
| 4175 |
else |
| 4176 |
bits,platform=32,"kfreebsd-i386" |
| 4177 |
end |
| 4178 |
else |
| 4179 |
architecture=architecture or resultof("uname -m") or "" |
| 4180 |
if find(architecture,"aarch64") then |
| 4181 |
bits,platform="linux-aarch64" |
| 4182 |
elseif find(architecture,"armv7l") then |
| 4183 |
bits,platform=32,"linux-armhf" |
| 4184 |
elseif find(architecture,"mips64") or find(architecture,"mips64el") then |
| 4185 |
bits,platform=64,"linux-mipsel" |
| 4186 |
elseif find(architecture,"mipsel") or find(architecture,"mips") then |
| 4187 |
bits,platform=32,"linux-mipsel" |
| 4188 |
else |
| 4189 |
bits,platform=64,"linux-64" |
| 4190 |
end |
| 4191 |
end |
| 4192 |
os.setenv("MTX_PLATFORM",platform) |
| 4193 |
os.setenv("MTX_BITS",bits) |
| 4194 |
os.platform=platform |
| 4195 |
os.bits=bits |
| 4196 |
os.newline=name=="windows" and "\013\010" or "\010" |
| 4197 |
end |
| 4198 |
do |
| 4199 |
local t={ 8,9,"a","b" } |
| 4200 |
function os.uuid() |
| 4201 |
return format("%04x%04x-4%03x-%s%03x-%04x-%04x%04x%04x", |
| 4202 |
random(0xFFFF),random(0xFFFF), |
| 4203 |
random(0x0FFF), |
| 4204 |
t[ceil(random(4))] or 8,random(0x0FFF), |
| 4205 |
random(0xFFFF), |
| 4206 |
random(0xFFFF),random(0xFFFF),random(0xFFFF) |
| 4207 |
) |
| 4208 |
end |
| 4209 |
end |
| 4210 |
do |
| 4211 |
local hour,min |
| 4212 |
function os.timezone(difference) |
| 4213 |
if not hour then |
| 4214 |
local current=time() |
| 4215 |
local utcdate=date("!*t",current) |
| 4216 |
local localdate=date("*t",current) |
| 4217 |
localdate.isdst=false |
| 4218 |
local timediff=difftime(time(localdate),time(utcdate)) |
| 4219 |
hour,min=modf(timediff/3600) |
| 4220 |
min=min*60 |
| 4221 |
end |
| 4222 |
if difference then |
| 4223 |
return hour,min |
| 4224 |
else |
| 4225 |
return format("%+03d:%02d",hour,min) |
| 4226 |
end |
| 4227 |
end |
| 4228 |
local timeformat=format("%%s%s",os.timezone()) |
| 4229 |
local dateformat="%Y-%m-%d %H:%M:%S" |
| 4230 |
local lasttime=nil |
| 4231 |
local lastdate=nil |
| 4232 |
function os.fulltime(t,default) |
| 4233 |
t=t and tonumber(t) or 0 |
| 4234 |
if t>0 then |
| 4235 |
elseif default then |
| 4236 |
return default |
| 4237 |
else |
| 4238 |
t=time() |
| 4239 |
end |
| 4240 |
if t~=lasttime then |
| 4241 |
lasttime=t |
| 4242 |
lastdate=format(timeformat,date(dateformat)) |
| 4243 |
end |
| 4244 |
return lastdate |
| 4245 |
end |
| 4246 |
local dateformat="%Y-%m-%d %H:%M:%S" |
| 4247 |
local lasttime=nil |
| 4248 |
local lastdate=nil |
| 4249 |
function os.localtime(t,default) |
| 4250 |
t=t and tonumber(t) or 0 |
| 4251 |
if t>0 then |
| 4252 |
elseif default then |
| 4253 |
return default |
| 4254 |
else |
| 4255 |
t=time() |
| 4256 |
end |
| 4257 |
if t~=lasttime then |
| 4258 |
lasttime=t |
| 4259 |
lastdate=date(dateformat,t) |
| 4260 |
end |
| 4261 |
return lastdate |
| 4262 |
end |
| 4263 |
function os.converttime(t,default) |
| 4264 |
local t=tonumber(t) |
| 4265 |
if t and t>0 then |
| 4266 |
return date(dateformat,t) |
| 4267 |
else |
| 4268 |
return default or "-" |
| 4269 |
end |
| 4270 |
end |
| 4271 |
function os.today() |
| 4272 |
return date("!*t") |
| 4273 |
end |
| 4274 |
function os.now() |
| 4275 |
return date("!%Y-%m-%d %H:%M:%S") |
| 4276 |
end |
| 4277 |
end |
| 4278 |
do |
| 4279 |
local cache={} |
| 4280 |
local function which(filename) |
| 4281 |
local fullname=cache[filename] |
| 4282 |
if fullname==nil then |
| 4283 |
local suffix=file.suffix(filename) |
| 4284 |
local suffixes=suffix=="" and os.binsuffixes or { suffix } |
| 4285 |
for directory in gmatch(os.getenv("PATH"),"[^"..io.pathseparator.."]+") do |
| 4286 |
local df=file.join(directory,filename) |
| 4287 |
for i=1,#suffixes do |
| 4288 |
local dfs=file.addsuffix(df,suffixes[i]) |
| 4289 |
if io.exists(dfs) then |
| 4290 |
fullname=dfs |
| 4291 |
break |
| 4292 |
end |
| 4293 |
end |
| 4294 |
end |
| 4295 |
if not fullname then |
| 4296 |
fullname=false |
| 4297 |
end |
| 4298 |
cache[filename]=fullname |
| 4299 |
end |
| 4300 |
return fullname |
| 4301 |
end |
| 4302 |
os.which=which |
| 4303 |
os.where=which |
| 4304 |
end |
| 4305 |
if not os.sleep then |
| 4306 |
local socket=socket |
| 4307 |
function os.sleep(n) |
| 4308 |
if not socket then |
| 4309 |
socket=require("socket") |
| 4310 |
end |
| 4311 |
socket.sleep(n) |
| 4312 |
end |
| 4313 |
end |
| 4314 |
do |
| 4315 |
local function isleapyear(year) |
| 4316 |
return (year%4==0) and (year%100~=0 or year%400==0) |
| 4317 |
end |
| 4318 |
os.isleapyear=isleapyear |
| 4319 |
local days={ 31,28,31,30,31,30,31,31,30,31,30,31 } |
| 4320 |
local function nofdays(year,month,day) |
| 4321 |
if not month then |
| 4322 |
return isleapyear(year) and 365 or 364 |
| 4323 |
elseif not day then |
| 4324 |
return month==2 and isleapyear(year) and 29 or days[month] |
| 4325 |
else |
| 4326 |
for i=1,month-1 do |
| 4327 |
day=day+days[i] |
| 4328 |
end |
| 4329 |
if month>2 and isleapyear(year) then |
| 4330 |
day=day+1 |
| 4331 |
end |
| 4332 |
return day |
| 4333 |
end |
| 4334 |
end |
| 4335 |
os.nofdays=nofdays |
| 4336 |
function os.weekday(day,month,year) |
| 4337 |
return date("%w",time { year=year,month=month,day=day })+1 |
| 4338 |
end |
| 4339 |
function os.validdate(year,month,day) |
| 4340 |
if month<1 then |
| 4341 |
month=1 |
| 4342 |
elseif month>12 then |
| 4343 |
month=12 |
| 4344 |
end |
| 4345 |
if day<1 then |
| 4346 |
day=1 |
| 4347 |
else |
| 4348 |
local max=nofdays(year,month) |
| 4349 |
if day>max then |
| 4350 |
day=max |
| 4351 |
end |
| 4352 |
end |
| 4353 |
return year,month,day |
| 4354 |
end |
| 4355 |
function os.date(fmt,...) |
| 4356 |
if not fmt then |
| 4357 |
fmt="%Y-%m-%d %H:%M" |
| 4358 |
end |
| 4359 |
return date(fmt,...) |
| 4360 |
end |
| 4361 |
end |
| 4362 |
do |
| 4363 |
local osexit=os.exit |
| 4364 |
local exitcode=nil |
| 4365 |
function os.setexitcode(code) |
| 4366 |
exitcode=code |
| 4367 |
end |
| 4368 |
function os.exit(c) |
| 4369 |
if exitcode~=nil then |
| 4370 |
return osexit(exitcode) |
| 4371 |
end |
| 4372 |
if c~=nil then |
| 4373 |
return osexit(c) |
| 4374 |
end |
| 4375 |
return osexit() |
| 4376 |
end |
| 4377 |
end |
| 4378 |
if serial and serial.write then |
| 4379 |
os.serialwrite=serial.write |
| 4380 |
end |
| 4381 |
|
| 4382 |
|
| 4383 |
end -- of closure |
| 4384 |
|
| 4385 |
do -- create closure to overcome 200 locals limit |
| 4386 |
|
| 4387 |
package.loaded["l-file"] = package.loaded["l-file"] or true |
| 4388 |
|
| 4389 |
-- original size: 22637, stripped down to: 10595 |
| 4390 |
|
| 4391 |
if not modules then modules={} end modules ['l-file']={ |
| 4392 |
version=1.001, |
| 4393 |
comment="companion to luat-lib.mkiv", |
| 4394 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4395 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4396 |
license="see context related readme files" |
| 4397 |
} |
| 4398 |
file=file or {} |
| 4399 |
local file=file |
| 4400 |
if not lfs then |
| 4401 |
lfs=optionalrequire("lfs") |
| 4402 |
end |
| 4403 |
local insert,concat=table.insert,table.concat |
| 4404 |
local match,find,gmatch=string.match,string.find,string.gmatch |
| 4405 |
local lpegmatch=lpeg.match |
| 4406 |
local getcurrentdir,attributes=lfs.currentdir,lfs.attributes |
| 4407 |
local checkedsplit=string.checkedsplit |
| 4408 |
local P,R,S,C,Cs,Cp,Cc,Ct=lpeg.P,lpeg.R,lpeg.S,lpeg.C,lpeg.Cs,lpeg.Cp,lpeg.Cc,lpeg.Ct |
| 4409 |
local attributes=lfs.attributes |
| 4410 |
function lfs.isdir(name) |
| 4411 |
if name then |
| 4412 |
return attributes(name,"mode")=="directory" |
| 4413 |
end |
| 4414 |
end |
| 4415 |
function lfs.isfile(name) |
| 4416 |
if name then |
| 4417 |
local a=attributes(name,"mode") |
| 4418 |
return a=="file" or a=="link" or nil |
| 4419 |
end |
| 4420 |
end |
| 4421 |
function lfs.isfound(name) |
| 4422 |
if name then |
| 4423 |
local a=attributes(name,"mode") |
| 4424 |
return (a=="file" or a=="link") and name or nil |
| 4425 |
end |
| 4426 |
end |
| 4427 |
function lfs.modification(name) |
| 4428 |
return name and attributes(name,"modification") or nil |
| 4429 |
end |
| 4430 |
if sandbox then |
| 4431 |
sandbox.redefine(lfs.isfile,"lfs.isfile") |
| 4432 |
sandbox.redefine(lfs.isdir,"lfs.isdir") |
| 4433 |
sandbox.redefine(lfs.isfound,"lfs.isfound") |
| 4434 |
end |
| 4435 |
local colon=P(":") |
| 4436 |
local period=P(".") |
| 4437 |
local periods=P("..") |
| 4438 |
local fwslash=P("/") |
| 4439 |
local bwslash=P("\\") |
| 4440 |
local slashes=S("\\/") |
| 4441 |
local noperiod=1-period |
| 4442 |
local noslashes=1-slashes |
| 4443 |
local name=noperiod^1 |
| 4444 |
local suffix=period/""*(1-period-slashes)^1*-1 |
| 4445 |
local pattern=C((1-(slashes^1*noslashes^1*-1))^1)*P(1) |
| 4446 |
local function pathpart(name,default) |
| 4447 |
return name and lpegmatch(pattern,name) or default or "" |
| 4448 |
end |
| 4449 |
local pattern=(noslashes^0*slashes)^1*C(noslashes^1)*-1 |
| 4450 |
local function basename(name) |
| 4451 |
return name and lpegmatch(pattern,name) or name |
| 4452 |
end |
| 4453 |
local pattern=(noslashes^0*slashes^1)^0*Cs((1-suffix)^1)*suffix^0 |
| 4454 |
local function nameonly(name) |
| 4455 |
return name and lpegmatch(pattern,name) or name |
| 4456 |
end |
| 4457 |
local pattern=(noslashes^0*slashes)^0*(noperiod^1*period)^1*C(noperiod^1)*-1 |
| 4458 |
local function suffixonly(name) |
| 4459 |
return name and lpegmatch(pattern,name) or "" |
| 4460 |
end |
| 4461 |
local pattern=(noslashes^0*slashes)^0*noperiod^1*((period*C(noperiod^1))^1)*-1+Cc("") |
| 4462 |
local function suffixesonly(name) |
| 4463 |
if name then |
| 4464 |
return lpegmatch(pattern,name) |
| 4465 |
else |
| 4466 |
return "" |
| 4467 |
end |
| 4468 |
end |
| 4469 |
file.pathpart=pathpart |
| 4470 |
file.basename=basename |
| 4471 |
file.nameonly=nameonly |
| 4472 |
file.suffixonly=suffixonly |
| 4473 |
file.suffix=suffixonly |
| 4474 |
file.suffixesonly=suffixesonly |
| 4475 |
file.suffixes=suffixesonly |
| 4476 |
file.dirname=pathpart |
| 4477 |
file.extname=suffixonly |
| 4478 |
local drive=C(R("az","AZ"))*colon |
| 4479 |
local path=C((noslashes^0*slashes)^0) |
| 4480 |
local suffix=period*C(P(1-period)^0*P(-1)) |
| 4481 |
local base=C((1-suffix)^0) |
| 4482 |
local rest=C(P(1)^0) |
| 4483 |
drive=drive+Cc("") |
| 4484 |
path=path+Cc("") |
| 4485 |
base=base+Cc("") |
| 4486 |
suffix=suffix+Cc("") |
| 4487 |
local pattern_a=drive*path*base*suffix |
| 4488 |
local pattern_b=path*base*suffix |
| 4489 |
local pattern_c=C(drive*path)*C(base*suffix) |
| 4490 |
local pattern_d=path*rest |
| 4491 |
function file.splitname(str,splitdrive) |
| 4492 |
if not str then |
| 4493 |
elseif splitdrive then |
| 4494 |
return lpegmatch(pattern_a,str) |
| 4495 |
else |
| 4496 |
return lpegmatch(pattern_b,str) |
| 4497 |
end |
| 4498 |
end |
| 4499 |
function file.splitbase(str) |
| 4500 |
if str then |
| 4501 |
return lpegmatch(pattern_d,str) |
| 4502 |
else |
| 4503 |
return "",str |
| 4504 |
end |
| 4505 |
end |
| 4506 |
function file.nametotable(str,splitdrive) |
| 4507 |
if str then |
| 4508 |
local path,drive,subpath,name,base,suffix=lpegmatch(pattern_c,str) |
| 4509 |
if splitdrive then |
| 4510 |
return { |
| 4511 |
path=path, |
| 4512 |
drive=drive, |
| 4513 |
subpath=subpath, |
| 4514 |
name=name, |
| 4515 |
base=base, |
| 4516 |
suffix=suffix, |
| 4517 |
} |
| 4518 |
else |
| 4519 |
return { |
| 4520 |
path=path, |
| 4521 |
name=name, |
| 4522 |
base=base, |
| 4523 |
suffix=suffix, |
| 4524 |
} |
| 4525 |
end |
| 4526 |
end |
| 4527 |
end |
| 4528 |
local pattern=Cs(((period*(1-period-slashes)^1*-1)/""+1)^1) |
| 4529 |
function file.removesuffix(name) |
| 4530 |
return name and lpegmatch(pattern,name) |
| 4531 |
end |
| 4532 |
local suffix=period/""*(1-period-slashes)^1*-1 |
| 4533 |
local pattern=Cs((noslashes^0*slashes^1)^0*((1-suffix)^1))*Cs(suffix) |
| 4534 |
function file.addsuffix(filename,suffix,criterium) |
| 4535 |
if not filename or not suffix or suffix=="" then |
| 4536 |
return filename |
| 4537 |
elseif criterium==true then |
| 4538 |
return filename.."."..suffix |
| 4539 |
elseif not criterium then |
| 4540 |
local n,s=lpegmatch(pattern,filename) |
| 4541 |
if not s or s=="" then |
| 4542 |
return filename.."."..suffix |
| 4543 |
else |
| 4544 |
return filename |
| 4545 |
end |
| 4546 |
else |
| 4547 |
local n,s=lpegmatch(pattern,filename) |
| 4548 |
if s and s~="" then |
| 4549 |
local t=type(criterium) |
| 4550 |
if t=="table" then |
| 4551 |
for i=1,#criterium do |
| 4552 |
if s==criterium[i] then |
| 4553 |
return filename |
| 4554 |
end |
| 4555 |
end |
| 4556 |
elseif t=="string" then |
| 4557 |
if s==criterium then |
| 4558 |
return filename |
| 4559 |
end |
| 4560 |
end |
| 4561 |
end |
| 4562 |
return (n or filename).."."..suffix |
| 4563 |
end |
| 4564 |
end |
| 4565 |
local suffix=period*(1-period-slashes)^1*-1 |
| 4566 |
local pattern=Cs((1-suffix)^0) |
| 4567 |
function file.replacesuffix(name,suffix) |
| 4568 |
if name and suffix and suffix~="" then |
| 4569 |
return lpegmatch(pattern,name).."."..suffix |
| 4570 |
else |
| 4571 |
return name |
| 4572 |
end |
| 4573 |
end |
| 4574 |
local reslasher=lpeg.replacer(P("\\"),"/") |
| 4575 |
function file.reslash(str) |
| 4576 |
return str and lpegmatch(reslasher,str) |
| 4577 |
end |
| 4578 |
if lfs.isreadablefile and lfs.iswritablefile then |
| 4579 |
file.is_readable=lfs.isreadablefile |
| 4580 |
file.is_writable=lfs.iswritablefile |
| 4581 |
else |
| 4582 |
function file.is_writable(name) |
| 4583 |
if not name then |
| 4584 |
elseif lfs.isdir(name) then |
| 4585 |
name=name.."/m_t_x_t_e_s_t.tmp" |
| 4586 |
local f=io.open(name,"wb") |
| 4587 |
if f then |
| 4588 |
f:close() |
| 4589 |
os.remove(name) |
| 4590 |
return true |
| 4591 |
end |
| 4592 |
elseif lfs.isfile(name) then |
| 4593 |
local f=io.open(name,"ab") |
| 4594 |
if f then |
| 4595 |
f:close() |
| 4596 |
return true |
| 4597 |
end |
| 4598 |
else |
| 4599 |
local f=io.open(name,"ab") |
| 4600 |
if f then |
| 4601 |
f:close() |
| 4602 |
os.remove(name) |
| 4603 |
return true |
| 4604 |
end |
| 4605 |
end |
| 4606 |
return false |
| 4607 |
end |
| 4608 |
local readable=P("r")*Cc(true) |
| 4609 |
function file.is_readable(name) |
| 4610 |
if name then |
| 4611 |
local a=attributes(name) |
| 4612 |
return a and lpegmatch(readable,a.permissions) or false |
| 4613 |
else |
| 4614 |
return false |
| 4615 |
end |
| 4616 |
end |
| 4617 |
end |
| 4618 |
file.isreadable=file.is_readable |
| 4619 |
file.iswritable=file.is_writable |
| 4620 |
function file.size(name) |
| 4621 |
if name then |
| 4622 |
local a=attributes(name) |
| 4623 |
return a and a.size or 0 |
| 4624 |
else |
| 4625 |
return 0 |
| 4626 |
end |
| 4627 |
end |
| 4628 |
function file.splitpath(str,separator) |
| 4629 |
return str and checkedsplit(lpegmatch(reslasher,str),separator or io.pathseparator) |
| 4630 |
end |
| 4631 |
function file.joinpath(tab,separator) |
| 4632 |
return tab and concat(tab,separator or io.pathseparator) |
| 4633 |
end |
| 4634 |
local someslash=S("\\/") |
| 4635 |
local stripper=Cs(P(fwslash)^0/""*reslasher) |
| 4636 |
local isnetwork=someslash*someslash*(1-someslash)+(1-fwslash-colon)^1*colon |
| 4637 |
local isroot=fwslash^1*-1 |
| 4638 |
local hasroot=fwslash^1 |
| 4639 |
local reslasher=lpeg.replacer(S("\\/"),"/") |
| 4640 |
local deslasher=lpeg.replacer(S("\\/")^1,"/") |
| 4641 |
function file.join(one,two,three,...) |
| 4642 |
if not two then |
| 4643 |
return one=="" and one or lpegmatch(reslasher,one) |
| 4644 |
end |
| 4645 |
if not one or one=="" then |
| 4646 |
return lpegmatch(stripper,three and concat({ two,three,... },"/") or two) |
| 4647 |
end |
| 4648 |
if lpegmatch(isnetwork,one) then |
| 4649 |
local one=lpegmatch(reslasher,one) |
| 4650 |
local two=lpegmatch(deslasher,three and concat({ two,three,... },"/") or two) |
| 4651 |
if lpegmatch(hasroot,two) then |
| 4652 |
return one..two |
| 4653 |
else |
| 4654 |
return one.."/"..two |
| 4655 |
end |
| 4656 |
elseif lpegmatch(isroot,one) then |
| 4657 |
local two=lpegmatch(deslasher,three and concat({ two,three,... },"/") or two) |
| 4658 |
if lpegmatch(hasroot,two) then |
| 4659 |
return two |
| 4660 |
else |
| 4661 |
return "/"..two |
| 4662 |
end |
| 4663 |
else |
| 4664 |
return lpegmatch(deslasher,concat({ one,two,three,... },"/")) |
| 4665 |
end |
| 4666 |
end |
| 4667 |
local drivespec=R("az","AZ")^1*colon |
| 4668 |
local anchors=fwslash+drivespec |
| 4669 |
local untouched=periods+(1-period)^1*P(-1) |
| 4670 |
local mswindrive=Cs(drivespec*(bwslash/"/"+fwslash)^0) |
| 4671 |
local mswinuncpath=(bwslash+fwslash)*(bwslash+fwslash)*Cc("//") |
| 4672 |
local splitstarter=(mswindrive+mswinuncpath+Cc(false))*Ct(lpeg.splitat(S("/\\")^1)) |
| 4673 |
local absolute=fwslash |
| 4674 |
function file.collapsepath(str,anchor) |
| 4675 |
if not str then |
| 4676 |
return |
| 4677 |
end |
| 4678 |
if anchor==true and not lpegmatch(anchors,str) then |
| 4679 |
str=getcurrentdir().."/"..str |
| 4680 |
end |
| 4681 |
if str=="" or str=="." then |
| 4682 |
return "." |
| 4683 |
elseif lpegmatch(untouched,str) then |
| 4684 |
return lpegmatch(reslasher,str) |
| 4685 |
end |
| 4686 |
local starter,oldelements=lpegmatch(splitstarter,str) |
| 4687 |
local newelements={} |
| 4688 |
local i=#oldelements |
| 4689 |
while i>0 do |
| 4690 |
local element=oldelements[i] |
| 4691 |
if element=='.' then |
| 4692 |
elseif element=='..' then |
| 4693 |
local n=i-1 |
| 4694 |
while n>0 do |
| 4695 |
local element=oldelements[n] |
| 4696 |
if element~='..' and element~='.' then |
| 4697 |
oldelements[n]='.' |
| 4698 |
break |
| 4699 |
else |
| 4700 |
n=n-1 |
| 4701 |
end |
| 4702 |
end |
| 4703 |
if n<1 then |
| 4704 |
insert(newelements,1,'..') |
| 4705 |
end |
| 4706 |
elseif element~="" then |
| 4707 |
insert(newelements,1,element) |
| 4708 |
end |
| 4709 |
i=i-1 |
| 4710 |
end |
| 4711 |
if #newelements==0 then |
| 4712 |
return starter or "." |
| 4713 |
elseif starter then |
| 4714 |
return starter..concat(newelements,'/') |
| 4715 |
elseif lpegmatch(absolute,str) then |
| 4716 |
return "/"..concat(newelements,'/') |
| 4717 |
else |
| 4718 |
newelements=concat(newelements,'/') |
| 4719 |
if anchor=="." and find(str,"^%./") then |
| 4720 |
return "./"..newelements |
| 4721 |
else |
| 4722 |
return newelements |
| 4723 |
end |
| 4724 |
end |
| 4725 |
end |
| 4726 |
local validchars=R("az","09","AZ","--","..") |
| 4727 |
local pattern_a=lpeg.replacer(1-validchars) |
| 4728 |
local pattern_a=Cs((validchars+P(1)/"-")^1) |
| 4729 |
local whatever=P("-")^0/"" |
| 4730 |
local pattern_b=Cs(whatever*(1-whatever*-1)^1) |
| 4731 |
function file.robustname(str,strict) |
| 4732 |
if str then |
| 4733 |
str=lpegmatch(pattern_a,str) or str |
| 4734 |
if strict then |
| 4735 |
return lpegmatch(pattern_b,str) or str |
| 4736 |
else |
| 4737 |
return str |
| 4738 |
end |
| 4739 |
end |
| 4740 |
end |
| 4741 |
local loaddata=io.loaddata |
| 4742 |
local savedata=io.savedata |
| 4743 |
file.readdata=loaddata |
| 4744 |
file.savedata=savedata |
| 4745 |
function file.copy(oldname,newname) |
| 4746 |
if oldname and newname then |
| 4747 |
local data=loaddata(oldname) |
| 4748 |
if data and data~="" then |
| 4749 |
savedata(newname,data) |
| 4750 |
end |
| 4751 |
end |
| 4752 |
end |
| 4753 |
local letter=R("az","AZ")+S("_-+") |
| 4754 |
local separator=P("://") |
| 4755 |
local qualified=period^0*fwslash+letter*colon+letter^1*separator+letter^1*fwslash |
| 4756 |
local rootbased=fwslash+letter*colon |
| 4757 |
lpeg.patterns.qualified=qualified |
| 4758 |
lpeg.patterns.rootbased=rootbased |
| 4759 |
function file.is_qualified_path(filename) |
| 4760 |
return filename and lpegmatch(qualified,filename)~=nil |
| 4761 |
end |
| 4762 |
function file.is_rootbased_path(filename) |
| 4763 |
return filename and lpegmatch(rootbased,filename)~=nil |
| 4764 |
end |
| 4765 |
function file.strip(name,dir) |
| 4766 |
if name then |
| 4767 |
local b,a=match(name,"^(.-)"..dir.."(.*)$") |
| 4768 |
return a~="" and a or name |
| 4769 |
end |
| 4770 |
end |
| 4771 |
function lfs.mkdirs(path) |
| 4772 |
local full="" |
| 4773 |
for sub in gmatch(path,"(/*[^\\/]+)") do |
| 4774 |
full=full..sub |
| 4775 |
lfs.mkdir(full) |
| 4776 |
end |
| 4777 |
end |
| 4778 |
function file.withinbase(path) |
| 4779 |
local l=0 |
| 4780 |
if not find(path,"^/") then |
| 4781 |
path="/"..path |
| 4782 |
end |
| 4783 |
for dir in gmatch(path,"/([^/]+)") do |
| 4784 |
if dir==".." then |
| 4785 |
l=l-1 |
| 4786 |
elseif dir~="." then |
| 4787 |
l=l+1 |
| 4788 |
end |
| 4789 |
if l<0 then |
| 4790 |
return false |
| 4791 |
end |
| 4792 |
end |
| 4793 |
return true |
| 4794 |
end |
| 4795 |
do |
| 4796 |
local symlinktarget=lfs.symlinktarget |
| 4797 |
local symlinkattributes=lfs.symlinkattributes |
| 4798 |
if symlinktarget then |
| 4799 |
function lfs.readlink(name) |
| 4800 |
local target=symlinktarget(name) |
| 4801 |
return name~=target and name or nil |
| 4802 |
end |
| 4803 |
elseif symlinkattributes then |
| 4804 |
function lfs.readlink(name) |
| 4805 |
return symlinkattributes(name,"target") or nil |
| 4806 |
end |
| 4807 |
else |
| 4808 |
function lfs.readlink(name) |
| 4809 |
return nil |
| 4810 |
end |
| 4811 |
end |
| 4812 |
end |
| 4813 |
|
| 4814 |
|
| 4815 |
end -- of closure |
| 4816 |
|
| 4817 |
do -- create closure to overcome 200 locals limit |
| 4818 |
|
| 4819 |
package.loaded["l-gzip"] = package.loaded["l-gzip"] or true |
| 4820 |
|
| 4821 |
-- original size: 268, stripped down to: 216 |
| 4822 |
|
| 4823 |
if not modules then modules={} end modules ['l-gzip']={ |
| 4824 |
version=1.001, |
| 4825 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4826 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4827 |
license="see context related readme files" |
| 4828 |
} |
| 4829 |
|
| 4830 |
|
| 4831 |
end -- of closure |
| 4832 |
|
| 4833 |
do -- create closure to overcome 200 locals limit |
| 4834 |
|
| 4835 |
package.loaded["l-md5"] = package.loaded["l-md5"] or true |
| 4836 |
|
| 4837 |
-- original size: 3414, stripped down to: 2307 |
| 4838 |
|
| 4839 |
if not modules then modules={} end modules ['l-md5']={ |
| 4840 |
version=1.001, |
| 4841 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4842 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4843 |
license="see context related readme files" |
| 4844 |
} |
| 4845 |
if not md5 then |
| 4846 |
md5=optionalrequire("md5") |
| 4847 |
end |
| 4848 |
if not md5 then |
| 4849 |
md5={ |
| 4850 |
sum=function(str) print("error: md5 is not loaded (sum ignored)") return str end, |
| 4851 |
sumhexa=function(str) print("error: md5 is not loaded (sumhexa ignored)") return str end, |
| 4852 |
} |
| 4853 |
end |
| 4854 |
local md5,file=md5,file |
| 4855 |
local gsub=string.gsub |
| 4856 |
local modification,isfile,touch=lfs.modification,lfs.isfile,lfs.touch |
| 4857 |
local loaddata,savedata=io.loaddata,io.savedata |
| 4858 |
do |
| 4859 |
local patterns=lpeg and lpeg.patterns |
| 4860 |
if patterns then |
| 4861 |
local bytestoHEX=patterns.bytestoHEX |
| 4862 |
local bytestohex=patterns.bytestohex |
| 4863 |
local bytestodec=patterns.bytestodec |
| 4864 |
local lpegmatch=lpeg.match |
| 4865 |
local md5sum=md5.sum |
| 4866 |
if not md5.HEX then function md5.HEX(str) if str then return lpegmatch(bytestoHEX,md5sum(str)) end end end |
| 4867 |
if not md5.hex then function md5.hex(str) if str then return lpegmatch(bytestohex,md5sum(str)) end end end |
| 4868 |
if not md5.dec then function md5.dec(str) if str then return lpegmatch(bytestodec,md5sum(str)) end end end |
| 4869 |
md5.sumhexa=md5.hex |
| 4870 |
md5.sumHEXA=md5.HEX |
| 4871 |
end |
| 4872 |
end |
| 4873 |
local md5HEX=md5.HEX |
| 4874 |
function file.needsupdating(oldname,newname,threshold) |
| 4875 |
local oldtime=modification(oldname) |
| 4876 |
if oldtime then |
| 4877 |
local newtime=modification(newname) |
| 4878 |
if not newtime then |
| 4879 |
return true |
| 4880 |
elseif newtime>=oldtime then |
| 4881 |
return false |
| 4882 |
elseif oldtime-newtime<(threshold or 1) then |
| 4883 |
return false |
| 4884 |
else |
| 4885 |
return true |
| 4886 |
end |
| 4887 |
else |
| 4888 |
return false |
| 4889 |
end |
| 4890 |
end |
| 4891 |
file.needs_updating=file.needsupdating |
| 4892 |
function file.syncmtimes(oldname,newname) |
| 4893 |
local oldtime=modification(oldname) |
| 4894 |
if oldtime and isfile(newname) then |
| 4895 |
touch(newname,oldtime,oldtime) |
| 4896 |
end |
| 4897 |
end |
| 4898 |
local function checksum(name) |
| 4899 |
if md5 then |
| 4900 |
local data=loaddata(name) |
| 4901 |
if data then |
| 4902 |
return md5HEX(data) |
| 4903 |
end |
| 4904 |
end |
| 4905 |
return nil |
| 4906 |
end |
| 4907 |
file.checksum=checksum |
| 4908 |
function file.loadchecksum(name) |
| 4909 |
if md5 then |
| 4910 |
local data=loaddata(name..".md5") |
| 4911 |
return data and (gsub(data,"%s","")) |
| 4912 |
end |
| 4913 |
return nil |
| 4914 |
end |
| 4915 |
function file.savechecksum(name,checksum) |
| 4916 |
if not checksum then checksum=checksum(name) end |
| 4917 |
if checksum then |
| 4918 |
savedata(name..".md5",checksum) |
| 4919 |
return checksum |
| 4920 |
end |
| 4921 |
return nil |
| 4922 |
end |
| 4923 |
|
| 4924 |
|
| 4925 |
end -- of closure |
| 4926 |
|
| 4927 |
do -- create closure to overcome 200 locals limit |
| 4928 |
|
| 4929 |
package.loaded["l-sha"] = package.loaded["l-sha"] or true |
| 4930 |
|
| 4931 |
-- original size: 1085, stripped down to: 969 |
| 4932 |
|
| 4933 |
if not modules then modules={} end modules ['l-sha']={ |
| 4934 |
version=1.001, |
| 4935 |
comment="companion to luat-lib.mkiv", |
| 4936 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4937 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4938 |
license="see context related readme files" |
| 4939 |
} |
| 4940 |
if sha2 then |
| 4941 |
local lpegmatch=lpeg.match |
| 4942 |
local lpegpatterns=lpeg.patterns |
| 4943 |
local bytestohex=lpegpatterns.bytestohex |
| 4944 |
local bytestoHEX=lpegpatterns.bytestoHEX |
| 4945 |
local digest256=sha2.digest256 |
| 4946 |
local digest384=sha2.digest384 |
| 4947 |
local digest512=sha2.digest512 |
| 4948 |
sha2.hash256=function(str) return lpegmatch(bytestohex,digest256(str)) end |
| 4949 |
sha2.hash384=function(str) return lpegmatch(bytestohex,digest384(str)) end |
| 4950 |
sha2.hash512=function(str) return lpegmatch(bytestohex,digest512(str)) end |
| 4951 |
sha2.HASH256=function(str) return lpegmatch(bytestoHEX,digest256(str)) end |
| 4952 |
sha2.HASH384=function(str) return lpegmatch(bytestoHEX,digest384(str)) end |
| 4953 |
sha2.HASH512=function(str) return lpegmatch(bytestoHEX,digest512(str)) end |
| 4954 |
end |
| 4955 |
|
| 4956 |
|
| 4957 |
end -- of closure |
| 4958 |
|
| 4959 |
do -- create closure to overcome 200 locals limit |
| 4960 |
|
| 4961 |
package.loaded["l-url"] = package.loaded["l-url"] or true |
| 4962 |
|
| 4963 |
-- original size: 14727, stripped down to: 6995 |
| 4964 |
|
| 4965 |
if not modules then modules={} end modules ['l-url']={ |
| 4966 |
version=1.001, |
| 4967 |
comment="companion to luat-lib.mkiv", |
| 4968 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4969 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4970 |
license="see context related readme files" |
| 4971 |
} |
| 4972 |
local char,format,byte=string.char,string.format,string.byte |
| 4973 |
local concat=table.concat |
| 4974 |
local tonumber,type,next=tonumber,type,next |
| 4975 |
local P,C,R,S,Cs,Cc,Ct,Cf,Cg,V=lpeg.P,lpeg.C,lpeg.R,lpeg.S,lpeg.Cs,lpeg.Cc,lpeg.Ct,lpeg.Cf,lpeg.Cg,lpeg.V |
| 4976 |
local lpegmatch,lpegpatterns,replacer=lpeg.match,lpeg.patterns,lpeg.replacer |
| 4977 |
local sortedhash=table.sortedhash |
| 4978 |
url=url or {} |
| 4979 |
local url=url |
| 4980 |
local unescapes={} |
| 4981 |
local escapes={} |
| 4982 |
setmetatable(unescapes,{ __index=function(t,k) |
| 4983 |
local v=char(tonumber(k,16)) |
| 4984 |
t[k]=v |
| 4985 |
return v |
| 4986 |
end }) |
| 4987 |
setmetatable(escapes,{ __index=function(t,k) |
| 4988 |
local v=format("%%%02X",byte(k)) |
| 4989 |
t[k]=v |
| 4990 |
return v |
| 4991 |
end }) |
| 4992 |
local colon=P(":") |
| 4993 |
local qmark=P("?") |
| 4994 |
local hash=P("#") |
| 4995 |
local slash=P("/") |
| 4996 |
local atsign=P("@") |
| 4997 |
local percent=P("%") |
| 4998 |
local endofstring=P(-1) |
| 4999 |
local hexdigit=R("09","AF","af") |
| 5000 |
local plus=P("+") |
| 5001 |
local nothing=Cc("") |
| 5002 |
local okay=R("09","AZ","az")+S("-_.,:=+*~!'()@&$") |
| 5003 |
local escapedchar=(percent*C(hexdigit*hexdigit))/unescapes |
| 5004 |
local unescapedchar=P(1)/escapes |
| 5005 |
local escaped=(plus/" ")+escapedchar |
| 5006 |
local noslash=P("/")/"" |
| 5007 |
local plustospace=P("+")/" " |
| 5008 |
local decoder=Cs(( |
| 5009 |
plustospace+escapedchar+P("\r\n")/"\n"+P(1) |
| 5010 |
)^0 ) |
| 5011 |
local encoder=Cs(( |
| 5012 |
R("09","AZ","az")^1+S("-./_")^1+P(" ")/"+"+P("\n")/"\r\n"+unescapedchar |
| 5013 |
)^0 ) |
| 5014 |
lpegpatterns.urldecoder=decoder |
| 5015 |
lpegpatterns.urlencoder=encoder |
| 5016 |
function url.decode (str) return str and lpegmatch(decoder,str) or str end |
| 5017 |
function url.encode (str) return str and lpegmatch(encoder,str) or str end |
| 5018 |
function url.unescape(str) return str and lpegmatch(unescaper,str) or str end |
| 5019 |
local schemestr=Cs((escaped+(1-colon-slash-qmark-hash))^2) |
| 5020 |
local authoritystr=Cs((escaped+(1- slash-qmark-hash))^0) |
| 5021 |
local pathstr=Cs((escaped+(1- qmark-hash))^0) |
| 5022 |
local querystr=Cs(((1- hash))^0) |
| 5023 |
local fragmentstr=Cs((escaped+(1- endofstring))^0) |
| 5024 |
local scheme=schemestr*colon+nothing |
| 5025 |
local authority=slash*slash*authoritystr+nothing |
| 5026 |
local path=slash*pathstr+nothing |
| 5027 |
local query=qmark*querystr+nothing |
| 5028 |
local fragment=hash*fragmentstr+nothing |
| 5029 |
local validurl=scheme*authority*path*query*fragment |
| 5030 |
local parser=Ct(validurl) |
| 5031 |
lpegpatterns.url=validurl |
| 5032 |
lpegpatterns.urlsplitter=parser |
| 5033 |
local escaper=Cs((R("09","AZ","az")^1+P(" ")/"%%20"+S("-./_:")^1+P(1)/escapes)^0) |
| 5034 |
local unescaper=Cs((escapedchar+1)^0) |
| 5035 |
local getcleaner=Cs((P("+++")/"%%2B"+P("+")/"%%20"+P(1))^1) |
| 5036 |
lpegpatterns.urlunescaped=escapedchar |
| 5037 |
lpegpatterns.urlescaper=escaper |
| 5038 |
lpegpatterns.urlunescaper=unescaper |
| 5039 |
lpegpatterns.urlgetcleaner=getcleaner |
| 5040 |
function url.unescapeget(str) |
| 5041 |
return str and lpegmatch(getcleaner,str) or "" |
| 5042 |
end |
| 5043 |
local function split(str) |
| 5044 |
return (type(str)=="string" and lpegmatch(parser,str)) or str |
| 5045 |
end |
| 5046 |
local isscheme=schemestr*colon*slash*slash |
| 5047 |
local function hasscheme(str) |
| 5048 |
if str then |
| 5049 |
local scheme=lpegmatch(isscheme,str) |
| 5050 |
return scheme~="" and scheme or false |
| 5051 |
else |
| 5052 |
return false |
| 5053 |
end |
| 5054 |
end |
| 5055 |
local rootletter=R("az","AZ")+S("_-+") |
| 5056 |
local separator=P("://") |
| 5057 |
local qualified=P(".")^0*P("/")+rootletter*P(":")+rootletter^1*separator+rootletter^1*P("/") |
| 5058 |
local rootbased=P("/")+rootletter*P(":") |
| 5059 |
local barswapper=replacer("|",":") |
| 5060 |
local backslashswapper=replacer("\\","/") |
| 5061 |
local equal=P("=") |
| 5062 |
local amp=P("&") |
| 5063 |
local key=Cs(((plustospace+escapedchar+1)-equal )^0) |
| 5064 |
local value=Cs(((plustospace+escapedchar+1)-amp-endofstring)^0) |
| 5065 |
local splitquery=Cf (Ct("")*P { "sequence", |
| 5066 |
sequence=V("pair")*(amp*V("pair"))^0, |
| 5067 |
pair=Cg(key*equal*value), |
| 5068 |
},rawset) |
| 5069 |
local userpart=(1-atsign-colon)^1 |
| 5070 |
local serverpart=(1-colon)^1 |
| 5071 |
local splitauthority=((Cs(userpart)*colon*Cs(userpart)+Cs(userpart)*Cc(nil))*atsign+Cc(nil)*Cc(nil))*Cs(serverpart)*(colon*(serverpart/tonumber)+Cc(nil)) |
| 5072 |
local function hashed(str) |
| 5073 |
if not str or str=="" then |
| 5074 |
return { |
| 5075 |
scheme="invalid", |
| 5076 |
original=str, |
| 5077 |
} |
| 5078 |
end |
| 5079 |
local detailed=split(str) |
| 5080 |
local rawscheme="" |
| 5081 |
local rawquery="" |
| 5082 |
local somescheme=false |
| 5083 |
local somequery=false |
| 5084 |
if detailed then |
| 5085 |
rawscheme=detailed[1] |
| 5086 |
rawquery=detailed[4] |
| 5087 |
somescheme=rawscheme~="" |
| 5088 |
somequery=rawquery~="" |
| 5089 |
end |
| 5090 |
if not somescheme and not somequery then |
| 5091 |
return { |
| 5092 |
scheme="file", |
| 5093 |
authority="", |
| 5094 |
path=str, |
| 5095 |
query="", |
| 5096 |
fragment="", |
| 5097 |
original=str, |
| 5098 |
noscheme=true, |
| 5099 |
filename=str, |
| 5100 |
} |
| 5101 |
end |
| 5102 |
local authority=detailed[2] |
| 5103 |
local path=detailed[3] |
| 5104 |
local filename |
| 5105 |
local username |
| 5106 |
local password |
| 5107 |
local host |
| 5108 |
local port |
| 5109 |
if authority~="" then |
| 5110 |
username,password,host,port=lpegmatch(splitauthority,authority) |
| 5111 |
end |
| 5112 |
if authority=="" then |
| 5113 |
filename=path |
| 5114 |
elseif path=="" then |
| 5115 |
filename="" |
| 5116 |
else |
| 5117 |
filename=authority.."/"..path |
| 5118 |
end |
| 5119 |
return { |
| 5120 |
scheme=rawscheme, |
| 5121 |
authority=authority, |
| 5122 |
path=path, |
| 5123 |
query=lpegmatch(unescaper,rawquery), |
| 5124 |
queries=lpegmatch(splitquery,rawquery), |
| 5125 |
fragment=detailed[5], |
| 5126 |
original=str, |
| 5127 |
noscheme=false, |
| 5128 |
filename=filename, |
| 5129 |
host=host, |
| 5130 |
port=port, |
| 5131 |
} |
| 5132 |
end |
| 5133 |
url.split=split |
| 5134 |
url.hasscheme=hasscheme |
| 5135 |
url.hashed=hashed |
| 5136 |
function url.addscheme(str,scheme) |
| 5137 |
if hasscheme(str) then |
| 5138 |
return str |
| 5139 |
elseif not scheme then |
| 5140 |
return "file:///"..str |
| 5141 |
else |
| 5142 |
return scheme..":///"..str |
| 5143 |
end |
| 5144 |
end |
| 5145 |
function url.construct(hash) |
| 5146 |
local result,r={},0 |
| 5147 |
local scheme=hash.scheme |
| 5148 |
local authority=hash.authority |
| 5149 |
local path=hash.path |
| 5150 |
local queries=hash.queries |
| 5151 |
local fragment=hash.fragment |
| 5152 |
if scheme and scheme~="" then |
| 5153 |
r=r+1;result[r]=lpegmatch(escaper,scheme) |
| 5154 |
r=r+1;result[r]="://" |
| 5155 |
end |
| 5156 |
if authority and authority~="" then |
| 5157 |
r=r+1;result[r]=lpegmatch(escaper,authority) |
| 5158 |
end |
| 5159 |
if path and path~="" then |
| 5160 |
r=r+1;result[r]="/" |
| 5161 |
r=r+1;result[r]=lpegmatch(escaper,path) |
| 5162 |
end |
| 5163 |
if queries then |
| 5164 |
local done=false |
| 5165 |
for k,v in sortedhash(queries) do |
| 5166 |
r=r+1;result[r]=done and "&" or "?" |
| 5167 |
r=r+1;result[r]=lpegmatch(escaper,k) |
| 5168 |
r=r+1;result[r]="=" |
| 5169 |
r=r+1;result[r]=lpegmatch(escaper,v) |
| 5170 |
done=true |
| 5171 |
end |
| 5172 |
end |
| 5173 |
if fragment and fragment~="" then |
| 5174 |
r=r+1;result[r]="#" |
| 5175 |
r=r+1;result[r]=lpegmatch(escaper,fragment) |
| 5176 |
end |
| 5177 |
return concat(result) |
| 5178 |
end |
| 5179 |
local pattern=Cs(slash^-1/""*R("az","AZ")*((S(":|")/":")+P(":"))*slash*P(1)^0) |
| 5180 |
function url.filename(filename) |
| 5181 |
local spec=hashed(filename) |
| 5182 |
local path=spec.path |
| 5183 |
return (spec.scheme=="file" and path and lpegmatch(pattern,path)) or filename |
| 5184 |
end |
| 5185 |
local function escapestring(str) |
| 5186 |
return lpegmatch(escaper,str) |
| 5187 |
end |
| 5188 |
url.escape=escapestring |
| 5189 |
function url.query(str) |
| 5190 |
if type(str)=="string" then |
| 5191 |
return lpegmatch(splitquery,str) or "" |
| 5192 |
else |
| 5193 |
return str |
| 5194 |
end |
| 5195 |
end |
| 5196 |
function url.toquery(data) |
| 5197 |
local td=type(data) |
| 5198 |
if td=="string" then |
| 5199 |
return #str and escape(data) or nil |
| 5200 |
elseif td=="table" then |
| 5201 |
if next(data) then |
| 5202 |
local t={} |
| 5203 |
for k,v in next,data do |
| 5204 |
t[#t+1]=format("%s=%s",k,escapestring(v)) |
| 5205 |
end |
| 5206 |
return concat(t,"&") |
| 5207 |
end |
| 5208 |
else |
| 5209 |
end |
| 5210 |
end |
| 5211 |
local pattern=Cs(noslash^0*(1-noslash*P(-1))^0) |
| 5212 |
function url.barepath(path) |
| 5213 |
if not path or path=="" then |
| 5214 |
return "" |
| 5215 |
else |
| 5216 |
return lpegmatch(pattern,path) |
| 5217 |
end |
| 5218 |
end |
| 5219 |
|
| 5220 |
|
| 5221 |
end -- of closure |
| 5222 |
|
| 5223 |
do -- create closure to overcome 200 locals limit |
| 5224 |
|
| 5225 |
package.loaded["l-dir"] = package.loaded["l-dir"] or true |
| 5226 |
|
| 5227 |
-- original size: 19379, stripped down to: 11553 |
| 5228 |
|
| 5229 |
if not modules then modules={} end modules ['l-dir']={ |
| 5230 |
version=1.001, |
| 5231 |
comment="companion to luat-lib.mkiv", |
| 5232 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 5233 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 5234 |
license="see context related readme files" |
| 5235 |
} |
| 5236 |
local type,select=type,select |
| 5237 |
local find,gmatch,match,gsub,sub=string.find,string.gmatch,string.match,string.gsub,string.sub |
| 5238 |
local concat,insert,remove,unpack=table.concat,table.insert,table.remove,table.unpack |
| 5239 |
local lpegmatch=lpeg.match |
| 5240 |
local P,S,R,C,Cc,Cs,Ct,Cv,V=lpeg.P,lpeg.S,lpeg.R,lpeg.C,lpeg.Cc,lpeg.Cs,lpeg.Ct,lpeg.Cv,lpeg.V |
| 5241 |
dir=dir or {} |
| 5242 |
local dir=dir |
| 5243 |
local lfs=lfs |
| 5244 |
local attributes=lfs.attributes |
| 5245 |
local scandir=lfs.dir |
| 5246 |
local isdir=lfs.isdir |
| 5247 |
local isfile=lfs.isfile |
| 5248 |
local currentdir=lfs.currentdir |
| 5249 |
local chdir=lfs.chdir |
| 5250 |
local mkdir=lfs.mkdir |
| 5251 |
local onwindows=os.type=="windows" or find(os.getenv("PATH"),";",1,true) |
| 5252 |
if onwindows then |
| 5253 |
local tricky=S("/\\")*P(-1) |
| 5254 |
isdir=function(name) |
| 5255 |
if lpegmatch(tricky,name) then |
| 5256 |
return attributes(name,"mode")=="directory" |
| 5257 |
else |
| 5258 |
return attributes(name.."/.","mode")=="directory" |
| 5259 |
end |
| 5260 |
end |
| 5261 |
isfile=function(name) |
| 5262 |
return attributes(name,"mode")=="file" |
| 5263 |
end |
| 5264 |
lfs.isdir=isdir |
| 5265 |
lfs.isfile=isfile |
| 5266 |
else |
| 5267 |
isdir=function(name) |
| 5268 |
return attributes(name,"mode")=="directory" |
| 5269 |
end |
| 5270 |
isfile=function(name) |
| 5271 |
return attributes(name,"mode")=="file" |
| 5272 |
end |
| 5273 |
lfs.isdir=isdir |
| 5274 |
lfs.isfile=isfile |
| 5275 |
end |
| 5276 |
local isreadable=file.isreadable |
| 5277 |
local walkdir=function(p,...) |
| 5278 |
if isreadable(p.."/.") then |
| 5279 |
return scandir(p,...) |
| 5280 |
else |
| 5281 |
return function() end |
| 5282 |
end |
| 5283 |
end |
| 5284 |
lfs.walkdir=walkdir |
| 5285 |
function dir.current() |
| 5286 |
return (gsub(currentdir(),"\\","/")) |
| 5287 |
end |
| 5288 |
local function glob_pattern_function(path,patt,recurse,action) |
| 5289 |
if isdir(path) then |
| 5290 |
local usedpath |
| 5291 |
if path=="/" then |
| 5292 |
usedpath="/." |
| 5293 |
elseif not find(path,"/$") then |
| 5294 |
usedpath=path.."/." |
| 5295 |
path=path.."/" |
| 5296 |
else |
| 5297 |
usedpath=path |
| 5298 |
end |
| 5299 |
local dirs |
| 5300 |
local nofdirs=0 |
| 5301 |
for name,mode,size,time in walkdir(usedpath) do |
| 5302 |
if name~="." and name~=".." then |
| 5303 |
local full=path..name |
| 5304 |
if mode==nil then |
| 5305 |
mode=attributes(full,'mode') |
| 5306 |
end |
| 5307 |
if mode=='file' then |
| 5308 |
if not patt or find(full,patt) then |
| 5309 |
action(full,size,time) |
| 5310 |
end |
| 5311 |
elseif recurse and mode=="directory" then |
| 5312 |
if dirs then |
| 5313 |
nofdirs=nofdirs+1 |
| 5314 |
dirs[nofdirs]=full |
| 5315 |
else |
| 5316 |
nofdirs=1 |
| 5317 |
dirs={ full } |
| 5318 |
end |
| 5319 |
end |
| 5320 |
end |
| 5321 |
end |
| 5322 |
if dirs then |
| 5323 |
for i=1,nofdirs do |
| 5324 |
glob_pattern_function(dirs[i],patt,recurse,action) |
| 5325 |
end |
| 5326 |
end |
| 5327 |
end |
| 5328 |
end |
| 5329 |
local function glob_pattern_table(path,patt,recurse,result,dirresult) |
| 5330 |
if not result then |
| 5331 |
result={} |
| 5332 |
end |
| 5333 |
local usedpath |
| 5334 |
if path=="/" then |
| 5335 |
usedpath="/." |
| 5336 |
elseif not find(path,"/$") then |
| 5337 |
usedpath=path.."/." |
| 5338 |
path=path.."/" |
| 5339 |
else |
| 5340 |
usedpath=path |
| 5341 |
end |
| 5342 |
local dirs |
| 5343 |
local nofdirs=0 |
| 5344 |
local noffiles=#result |
| 5345 |
for name,mode in walkdir(usedpath) do |
| 5346 |
if name~="." and name~=".." then |
| 5347 |
local full=path..name |
| 5348 |
if mode==nil then |
| 5349 |
mode=attributes(full,'mode') |
| 5350 |
end |
| 5351 |
if mode=='file' then |
| 5352 |
if not patt or find(full,patt) then |
| 5353 |
noffiles=noffiles+1 |
| 5354 |
result[noffiles]=full |
| 5355 |
end |
| 5356 |
elseif recurse and mode=="directory" then |
| 5357 |
if dirs then |
| 5358 |
nofdirs=nofdirs+1 |
| 5359 |
dirs[nofdirs]=full |
| 5360 |
else |
| 5361 |
nofdirs=1 |
| 5362 |
dirs={ full } |
| 5363 |
end |
| 5364 |
end |
| 5365 |
end |
| 5366 |
end |
| 5367 |
if dirs then |
| 5368 |
for i=1,nofdirs do |
| 5369 |
local dir=dirs[i] |
| 5370 |
glob_pattern_table(dir,patt,recurse,result,dirresult) |
| 5371 |
if dirresult then |
| 5372 |
dirresult[#dirresult+1]=dir |
| 5373 |
end |
| 5374 |
end |
| 5375 |
end |
| 5376 |
return result,dirresult |
| 5377 |
end |
| 5378 |
local function globpattern(path,patt,recurse,method,dirmethod) |
| 5379 |
local kind=type(method) |
| 5380 |
if patt and sub(patt,1,-3)==path then |
| 5381 |
patt=false |
| 5382 |
end |
| 5383 |
local okay=isdir(path) |
| 5384 |
if kind=="function" then |
| 5385 |
return okay and glob_pattern_function(path,patt,recurse,method) or {} |
| 5386 |
elseif kind=="table" then |
| 5387 |
return okay and glob_pattern_table(path,patt,recurse,method) or method |
| 5388 |
elseif okay then |
| 5389 |
local files,dirs=glob_pattern_table(path,patt,recurse,{},{}) |
| 5390 |
return files or {},dirs or {} |
| 5391 |
else |
| 5392 |
return {},dirmethod and {} or nil |
| 5393 |
end |
| 5394 |
end |
| 5395 |
dir.globpattern=globpattern |
| 5396 |
local function collectpattern(path,patt,recurse,result) |
| 5397 |
local ok,scanner |
| 5398 |
result=result or {} |
| 5399 |
if path=="/" then |
| 5400 |
ok,scanner,first=xpcall(function() return walkdir(path..".") end,function() end) |
| 5401 |
else |
| 5402 |
ok,scanner,first=xpcall(function() return walkdir(path) end,function() end) |
| 5403 |
end |
| 5404 |
if ok and type(scanner)=="function" then |
| 5405 |
if not find(path,"/$") then |
| 5406 |
path=path..'/' |
| 5407 |
end |
| 5408 |
for name in scanner,first do |
| 5409 |
if name=="." then |
| 5410 |
elseif name==".." then |
| 5411 |
else |
| 5412 |
local full=path..name |
| 5413 |
local attr=attributes(full) |
| 5414 |
local mode=attr.mode |
| 5415 |
if mode=='file' then |
| 5416 |
if find(full,patt) then |
| 5417 |
result[name]=attr |
| 5418 |
end |
| 5419 |
elseif recurse and mode=="directory" then |
| 5420 |
attr.list=collectpattern(full,patt,recurse) |
| 5421 |
result[name]=attr |
| 5422 |
end |
| 5423 |
end |
| 5424 |
end |
| 5425 |
end |
| 5426 |
return result |
| 5427 |
end |
| 5428 |
dir.collectpattern=collectpattern |
| 5429 |
local separator,pattern |
| 5430 |
if onwindows then |
| 5431 |
local slash=S("/\\")/"/" |
| 5432 |
pattern={ |
| 5433 |
(Cs(P(".")+slash^1)+Cs(R("az","AZ")*P(":")*slash^0)+Cc("./"))*V(2)*V(3), |
| 5434 |
Cs(((1-S("*?/\\"))^0*slash)^0), |
| 5435 |
Cs(P(1)^0) |
| 5436 |
} |
| 5437 |
else |
| 5438 |
pattern={ |
| 5439 |
(C(P(".")+P("/")^1)+Cc("./"))*V(2)*V(3), |
| 5440 |
C(((1-S("*?/"))^0*P("/"))^0), |
| 5441 |
C(P(1)^0) |
| 5442 |
} |
| 5443 |
end |
| 5444 |
local filter=Cs (( |
| 5445 |
P("**")/".*"+P("*")/"[^/]*"+P("?")/"[^/]"+P(".")/"%%."+P("+")/"%%+"+P("-")/"%%-"+P(1) |
| 5446 |
)^0 ) |
| 5447 |
local function glob(str,t,dirstoo) |
| 5448 |
if type(t)=="function" then |
| 5449 |
if type(str)=="table" then |
| 5450 |
for s=1,#str do |
| 5451 |
glob(str[s],t) |
| 5452 |
end |
| 5453 |
elseif isfile(str) then |
| 5454 |
t(str) |
| 5455 |
else |
| 5456 |
local root,path,base=lpegmatch(pattern,str) |
| 5457 |
if root and path and base then |
| 5458 |
local recurse=find(base,"**",1,true) |
| 5459 |
local start=root..path |
| 5460 |
local result=lpegmatch(filter,start..base) |
| 5461 |
globpattern(start,result,recurse,t) |
| 5462 |
end |
| 5463 |
end |
| 5464 |
elseif type(str)=="table" then |
| 5465 |
local t=t or {} |
| 5466 |
for s=1,#str do |
| 5467 |
glob(str[s],t) |
| 5468 |
end |
| 5469 |
return t |
| 5470 |
elseif isfile(str) then |
| 5471 |
if t then |
| 5472 |
t[#t+1]=str |
| 5473 |
return t |
| 5474 |
else |
| 5475 |
return { str } |
| 5476 |
end |
| 5477 |
else |
| 5478 |
local root,path,base=lpegmatch(pattern,str) |
| 5479 |
if root and path and base then |
| 5480 |
local recurse=find(base,"**",1,true) |
| 5481 |
local start=root..path |
| 5482 |
local result=lpegmatch(filter,start..base) |
| 5483 |
return globpattern(start,result,recurse,t,dirstoo) |
| 5484 |
else |
| 5485 |
return {},dirstoo and {} or nil |
| 5486 |
end |
| 5487 |
end |
| 5488 |
end |
| 5489 |
dir.glob=glob |
| 5490 |
local function globfiles(path,recurse,func,files) |
| 5491 |
if type(func)=="string" then |
| 5492 |
local s=func |
| 5493 |
func=function(name) return find(name,s) end |
| 5494 |
end |
| 5495 |
files=files or {} |
| 5496 |
local noffiles=#files |
| 5497 |
for name,mode in walkdir(path) do |
| 5498 |
if find(name,"^%.") then |
| 5499 |
else |
| 5500 |
if mode==nil then |
| 5501 |
mode=attributes(name,'mode') |
| 5502 |
end |
| 5503 |
if mode=="directory" then |
| 5504 |
if recurse then |
| 5505 |
globfiles(path.."/"..name,recurse,func,files) |
| 5506 |
end |
| 5507 |
elseif mode=="file" then |
| 5508 |
if not func or func(name) then |
| 5509 |
noffiles=noffiles+1 |
| 5510 |
files[noffiles]=path.."/"..name |
| 5511 |
end |
| 5512 |
end |
| 5513 |
end |
| 5514 |
end |
| 5515 |
return files |
| 5516 |
end |
| 5517 |
dir.globfiles=globfiles |
| 5518 |
local function globdirs(path,recurse,func,files) |
| 5519 |
if type(func)=="string" then |
| 5520 |
local s=func |
| 5521 |
func=function(name) return find(name,s) end |
| 5522 |
end |
| 5523 |
files=files or {} |
| 5524 |
local noffiles=#files |
| 5525 |
for name,mode in walkdir(path) do |
| 5526 |
if find(name,"^%.") then |
| 5527 |
else |
| 5528 |
if mode==nil then |
| 5529 |
mode=attributes(name,'mode') |
| 5530 |
end |
| 5531 |
if mode=="directory" then |
| 5532 |
if not func or func(name) then |
| 5533 |
noffiles=noffiles+1 |
| 5534 |
files[noffiles]=path.."/"..name |
| 5535 |
if recurse then |
| 5536 |
globdirs(path.."/"..name,recurse,func,files) |
| 5537 |
end |
| 5538 |
end |
| 5539 |
end |
| 5540 |
end |
| 5541 |
end |
| 5542 |
return files |
| 5543 |
end |
| 5544 |
dir.globdirs=globdirs |
| 5545 |
function dir.ls(pattern) |
| 5546 |
return concat(glob(pattern),"\n") |
| 5547 |
end |
| 5548 |
local make_indeed=true |
| 5549 |
if onwindows then |
| 5550 |
function dir.mkdirs(...) |
| 5551 |
local n=select("#",...) |
| 5552 |
local str |
| 5553 |
if n==1 then |
| 5554 |
str=select(1,...) |
| 5555 |
if isdir(str) then |
| 5556 |
return str,true |
| 5557 |
end |
| 5558 |
else |
| 5559 |
str="" |
| 5560 |
for i=1,n do |
| 5561 |
local s=select(i,...) |
| 5562 |
if s=="" then |
| 5563 |
elseif str=="" then |
| 5564 |
str=s |
| 5565 |
else |
| 5566 |
str=str.."/"..s |
| 5567 |
end |
| 5568 |
end |
| 5569 |
end |
| 5570 |
local pth="" |
| 5571 |
local drive=false |
| 5572 |
local first,middle,last=match(str,"^(//)(//*)(.*)$") |
| 5573 |
if first then |
| 5574 |
else |
| 5575 |
first,last=match(str,"^(//)/*(.-)$") |
| 5576 |
if first then |
| 5577 |
middle,last=match(str,"([^/]+)/+(.-)$") |
| 5578 |
if middle then |
| 5579 |
pth="//"..middle |
| 5580 |
else |
| 5581 |
pth="//"..last |
| 5582 |
last="" |
| 5583 |
end |
| 5584 |
else |
| 5585 |
first,middle,last=match(str,"^([a-zA-Z]:)(/*)(.-)$") |
| 5586 |
if first then |
| 5587 |
pth,drive=first..middle,true |
| 5588 |
else |
| 5589 |
middle,last=match(str,"^(/*)(.-)$") |
| 5590 |
if not middle then |
| 5591 |
last=str |
| 5592 |
end |
| 5593 |
end |
| 5594 |
end |
| 5595 |
end |
| 5596 |
for s in gmatch(last,"[^/]+") do |
| 5597 |
if pth=="" then |
| 5598 |
pth=s |
| 5599 |
elseif drive then |
| 5600 |
pth,drive=pth..s,false |
| 5601 |
else |
| 5602 |
pth=pth.."/"..s |
| 5603 |
end |
| 5604 |
if make_indeed and not isdir(pth) then |
| 5605 |
mkdir(pth) |
| 5606 |
end |
| 5607 |
end |
| 5608 |
return pth,(isdir(pth)==true) |
| 5609 |
end |
| 5610 |
else |
| 5611 |
function dir.mkdirs(...) |
| 5612 |
local n=select("#",...) |
| 5613 |
local str,pth |
| 5614 |
if n==1 then |
| 5615 |
str=select(1,...) |
| 5616 |
if isdir(str) then |
| 5617 |
return str,true |
| 5618 |
end |
| 5619 |
else |
| 5620 |
str="" |
| 5621 |
for i=1,n do |
| 5622 |
local s=select(i,...) |
| 5623 |
if s and s~="" then |
| 5624 |
if str~="" then |
| 5625 |
str=str.."/"..s |
| 5626 |
else |
| 5627 |
str=s |
| 5628 |
end |
| 5629 |
end |
| 5630 |
end |
| 5631 |
end |
| 5632 |
str=gsub(str,"/+","/") |
| 5633 |
if find(str,"^/") then |
| 5634 |
pth="/" |
| 5635 |
for s in gmatch(str,"[^/]+") do |
| 5636 |
local first=(pth=="/") |
| 5637 |
if first then |
| 5638 |
pth=pth..s |
| 5639 |
else |
| 5640 |
pth=pth.."/"..s |
| 5641 |
end |
| 5642 |
if make_indeed and not first and not isdir(pth) then |
| 5643 |
mkdir(pth) |
| 5644 |
end |
| 5645 |
end |
| 5646 |
else |
| 5647 |
pth="." |
| 5648 |
for s in gmatch(str,"[^/]+") do |
| 5649 |
pth=pth.."/"..s |
| 5650 |
if make_indeed and not isdir(pth) then |
| 5651 |
mkdir(pth) |
| 5652 |
end |
| 5653 |
end |
| 5654 |
end |
| 5655 |
return pth,(isdir(pth)==true) |
| 5656 |
end |
| 5657 |
end |
| 5658 |
dir.makedirs=dir.mkdirs |
| 5659 |
do |
| 5660 |
local chdir=sandbox and sandbox.original(chdir) or chdir |
| 5661 |
if onwindows then |
| 5662 |
local xcurrentdir=dir.current |
| 5663 |
function dir.expandname(str) |
| 5664 |
local first,nothing,last=match(str,"^(//)(//*)(.*)$") |
| 5665 |
if first then |
| 5666 |
first=xcurrentdir().."/" |
| 5667 |
end |
| 5668 |
if not first then |
| 5669 |
first,last=match(str,"^(//)/*(.*)$") |
| 5670 |
end |
| 5671 |
if not first then |
| 5672 |
first,last=match(str,"^([a-zA-Z]:)(.*)$") |
| 5673 |
if first and not find(last,"^/") then |
| 5674 |
local d=currentdir() |
| 5675 |
if chdir(first) then |
| 5676 |
first=xcurrentdir() |
| 5677 |
end |
| 5678 |
chdir(d) |
| 5679 |
end |
| 5680 |
end |
| 5681 |
if not first then |
| 5682 |
first,last=xcurrentdir(),str |
| 5683 |
end |
| 5684 |
last=gsub(last,"//","/") |
| 5685 |
last=gsub(last,"/%./","/") |
| 5686 |
last=gsub(last,"^/*","") |
| 5687 |
first=gsub(first,"/*$","") |
| 5688 |
if last=="" or last=="." then |
| 5689 |
return first |
| 5690 |
else |
| 5691 |
return first.."/"..last |
| 5692 |
end |
| 5693 |
end |
| 5694 |
else |
| 5695 |
function dir.expandname(str) |
| 5696 |
if not find(str,"^/") then |
| 5697 |
str=currentdir().."/"..str |
| 5698 |
end |
| 5699 |
str=gsub(str,"//","/") |
| 5700 |
str=gsub(str,"/%./","/") |
| 5701 |
str=gsub(str,"(.)/%.$","%1") |
| 5702 |
return str |
| 5703 |
end |
| 5704 |
end |
| 5705 |
function dir.expandlink(dir,report) |
| 5706 |
local curdir=currentdir() |
| 5707 |
local trace=type(report)=="function" |
| 5708 |
if chdir(dir) then |
| 5709 |
local newdir=currentdir() |
| 5710 |
if newdir~=dir and trace then |
| 5711 |
report("following symlink %a to %a",dir,newdir) |
| 5712 |
end |
| 5713 |
chdir(curdir) |
| 5714 |
return newdir |
| 5715 |
else |
| 5716 |
if trace then |
| 5717 |
report("unable to check path %a",dir) |
| 5718 |
end |
| 5719 |
return dir |
| 5720 |
end |
| 5721 |
end |
| 5722 |
end |
| 5723 |
file.expandname=dir.expandname |
| 5724 |
local stack={} |
| 5725 |
function dir.push(newdir) |
| 5726 |
local curdir=currentdir() |
| 5727 |
insert(stack,curdir) |
| 5728 |
if newdir and newdir~="" and chdir(newdir) then |
| 5729 |
return newdir |
| 5730 |
else |
| 5731 |
return curdir |
| 5732 |
end |
| 5733 |
end |
| 5734 |
function dir.pop() |
| 5735 |
local d=remove(stack) |
| 5736 |
if d then |
| 5737 |
chdir(d) |
| 5738 |
end |
| 5739 |
return d |
| 5740 |
end |
| 5741 |
local function found(...) |
| 5742 |
for i=1,select("#",...) do |
| 5743 |
local path=select(i,...) |
| 5744 |
local kind=type(path) |
| 5745 |
if kind=="string" then |
| 5746 |
if isdir(path) then |
| 5747 |
return path |
| 5748 |
end |
| 5749 |
elseif kind=="table" then |
| 5750 |
local path=found(unpack(path)) |
| 5751 |
if path then |
| 5752 |
return path |
| 5753 |
end |
| 5754 |
end |
| 5755 |
end |
| 5756 |
end |
| 5757 |
dir.found=found |
| 5758 |
|
| 5759 |
|
| 5760 |
end -- of closure |
| 5761 |
|
| 5762 |
do -- create closure to overcome 200 locals limit |
| 5763 |
|
| 5764 |
package.loaded["l-boolean"] = package.loaded["l-boolean"] or true |
| 5765 |
|
| 5766 |
-- original size: 1850, stripped down to: 1498 |
| 5767 |
|
| 5768 |
if not modules then modules={} end modules ['l-boolean']={ |
| 5769 |
version=1.001, |
| 5770 |
comment="companion to luat-lib.mkiv", |
| 5771 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 5772 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 5773 |
license="see context related readme files" |
| 5774 |
} |
| 5775 |
local type,tonumber=type,tonumber |
| 5776 |
boolean=boolean or {} |
| 5777 |
local boolean=boolean |
| 5778 |
function boolean.tonumber(b) |
| 5779 |
if b then return 1 else return 0 end |
| 5780 |
end |
| 5781 |
function toboolean(str,tolerant) |
| 5782 |
if str==nil then |
| 5783 |
return false |
| 5784 |
elseif str==false then |
| 5785 |
return false |
| 5786 |
elseif str==true then |
| 5787 |
return true |
| 5788 |
elseif str=="true" then |
| 5789 |
return true |
| 5790 |
elseif str=="false" then |
| 5791 |
return false |
| 5792 |
elseif not tolerant then |
| 5793 |
return false |
| 5794 |
elseif str==0 then |
| 5795 |
return false |
| 5796 |
elseif (tonumber(str) or 0)>0 then |
| 5797 |
return true |
| 5798 |
else |
| 5799 |
return str=="yes" or str=="on" or str=="t" |
| 5800 |
end |
| 5801 |
end |
| 5802 |
string.toboolean=toboolean |
| 5803 |
function string.booleanstring(str) |
| 5804 |
if str=="0" then |
| 5805 |
return false |
| 5806 |
elseif str=="1" then |
| 5807 |
return true |
| 5808 |
elseif str=="" then |
| 5809 |
return false |
| 5810 |
elseif str=="false" then |
| 5811 |
return false |
| 5812 |
elseif str=="true" then |
| 5813 |
return true |
| 5814 |
elseif (tonumber(str) or 0)>0 then |
| 5815 |
return true |
| 5816 |
else |
| 5817 |
return str=="yes" or str=="on" or str=="t" |
| 5818 |
end |
| 5819 |
end |
| 5820 |
function string.is_boolean(str,default,strict) |
| 5821 |
if type(str)=="string" then |
| 5822 |
if str=="true" or str=="yes" or str=="on" or str=="t" or (not strict and str=="1") then |
| 5823 |
return true |
| 5824 |
elseif str=="false" or str=="no" or str=="off" or str=="f" or (not strict and str=="0") then |
| 5825 |
return false |
| 5826 |
end |
| 5827 |
end |
| 5828 |
return default |
| 5829 |
end |
| 5830 |
|
| 5831 |
|
| 5832 |
end -- of closure |
| 5833 |
|
| 5834 |
do -- create closure to overcome 200 locals limit |
| 5835 |
|
| 5836 |
package.loaded["l-unicode"] = package.loaded["l-unicode"] or true |
| 5837 |
|
| 5838 |
-- original size: 41303, stripped down to: 17277 |
| 5839 |
|
| 5840 |
if not modules then modules={} end modules ['l-unicode']={ |
| 5841 |
version=1.001, |
| 5842 |
optimize=true, |
| 5843 |
comment="companion to luat-lib.mkiv", |
| 5844 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 5845 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 5846 |
license="see context related readme files" |
| 5847 |
} |
| 5848 |
utf=utf or {} |
| 5849 |
unicode=nil |
| 5850 |
if not string.utfcharacters then |
| 5851 |
local gmatch=string.gmatch |
| 5852 |
function string.characters(str) |
| 5853 |
return gmatch(str,".[\128-\191]*") |
| 5854 |
end |
| 5855 |
end |
| 5856 |
utf.characters=string.utfcharacters |
| 5857 |
local type=type |
| 5858 |
local char,byte,format,sub,gmatch=string.char,string.byte,string.format,string.sub,string.gmatch |
| 5859 |
local concat=table.concat |
| 5860 |
local P,C,R,Cs,Ct,Cmt,Cc,Carg,Cp=lpeg.P,lpeg.C,lpeg.R,lpeg.Cs,lpeg.Ct,lpeg.Cmt,lpeg.Cc,lpeg.Carg,lpeg.Cp |
| 5861 |
local lpegmatch=lpeg.match |
| 5862 |
local patterns=lpeg.patterns |
| 5863 |
local tabletopattern=lpeg.utfchartabletopattern |
| 5864 |
local bytepairs=string.bytepairs |
| 5865 |
local finder=lpeg.finder |
| 5866 |
local replacer=lpeg.replacer |
| 5867 |
local p_utftype=patterns.utftype |
| 5868 |
local p_utfstricttype=patterns.utfstricttype |
| 5869 |
local p_utfoffset=patterns.utfoffset |
| 5870 |
local p_utf8character=patterns.utf8character |
| 5871 |
local p_utf8char=patterns.utf8char |
| 5872 |
local p_utf8byte=patterns.utf8byte |
| 5873 |
local p_utfbom=patterns.utfbom |
| 5874 |
local p_newline=patterns.newline |
| 5875 |
local p_whitespace=patterns.whitespace |
| 5876 |
if not utf.char then |
| 5877 |
utf.char=string.utfcharacter or (utf8 and utf8.char) |
| 5878 |
if not utf.char then |
| 5879 |
local char=string.char |
| 5880 |
if bit32 then |
| 5881 |
local rshift=bit32.rshift |
| 5882 |
function utf.char(n) |
| 5883 |
if n<0x80 then |
| 5884 |
return char(n) |
| 5885 |
elseif n<0x800 then |
| 5886 |
return char( |
| 5887 |
0xC0+rshift(n,6), |
| 5888 |
0x80+(n%0x40) |
| 5889 |
) |
| 5890 |
elseif n<0x10000 then |
| 5891 |
return char( |
| 5892 |
0xE0+rshift(n,12), |
| 5893 |
0x80+(rshift(n,6)%0x40), |
| 5894 |
0x80+(n%0x40) |
| 5895 |
) |
| 5896 |
elseif n<0x200000 then |
| 5897 |
return char( |
| 5898 |
0xF0+rshift(n,18), |
| 5899 |
0x80+(rshift(n,12)%0x40), |
| 5900 |
0x80+(rshift(n,6)%0x40), |
| 5901 |
0x80+(n%0x40) |
| 5902 |
) |
| 5903 |
else |
| 5904 |
return "" |
| 5905 |
end |
| 5906 |
end |
| 5907 |
else |
| 5908 |
local floor=math.floor |
| 5909 |
function utf.char(n) |
| 5910 |
if n<0x80 then |
| 5911 |
return char(n) |
| 5912 |
elseif n<0x800 then |
| 5913 |
return char( |
| 5914 |
0xC0+floor(n/0x40), |
| 5915 |
0x80+(n%0x40) |
| 5916 |
) |
| 5917 |
elseif n<0x10000 then |
| 5918 |
return char( |
| 5919 |
0xE0+floor(n/0x1000), |
| 5920 |
0x80+(floor(n/0x40)%0x40), |
| 5921 |
0x80+(n%0x40) |
| 5922 |
) |
| 5923 |
elseif n<0x200000 then |
| 5924 |
return char( |
| 5925 |
0xF0+floor(n/0x40000), |
| 5926 |
0x80+(floor(n/0x1000)%0x40), |
| 5927 |
0x80+(floor(n/0x40)%0x40), |
| 5928 |
0x80+(n%0x40) |
| 5929 |
) |
| 5930 |
else |
| 5931 |
return "" |
| 5932 |
end |
| 5933 |
end |
| 5934 |
end |
| 5935 |
end |
| 5936 |
end |
| 5937 |
if not utf.byte then |
| 5938 |
utf.byte=string.utfvalue or (utf8 and utf8.codepoint) |
| 5939 |
if not utf.byte then |
| 5940 |
function utf.byte(c) |
| 5941 |
return lpegmatch(p_utf8byte,c) |
| 5942 |
end |
| 5943 |
end |
| 5944 |
end |
| 5945 |
local utfchar,utfbyte=utf.char,utf.byte |
| 5946 |
function utf.filetype(data) |
| 5947 |
return data and lpegmatch(p_utftype,data) or "unknown" |
| 5948 |
end |
| 5949 |
local toentities=Cs ( |
| 5950 |
( |
| 5951 |
patterns.utf8one+( |
| 5952 |
patterns.utf8two+patterns.utf8three+patterns.utf8four |
| 5953 |
)/function(s) local b=utfbyte(s) if b<127 then return s else return format("&#%X;",b) end end |
| 5954 |
)^0 |
| 5955 |
) |
| 5956 |
patterns.toentities=toentities |
| 5957 |
function utf.toentities(str) |
| 5958 |
return lpegmatch(toentities,str) |
| 5959 |
end |
| 5960 |
local one=P(1) |
| 5961 |
local two=C(1)*C(1) |
| 5962 |
local four=C(R(utfchar(0xD8),utfchar(0xFF)))*C(1)*C(1)*C(1) |
| 5963 |
local pattern=P("\254\255")*Cs(( |
| 5964 |
four/function(a,b,c,d) |
| 5965 |
local ab=0xFF*byte(a)+byte(b) |
| 5966 |
local cd=0xFF*byte(c)+byte(d) |
| 5967 |
return utfchar((ab-0xD800)*0x400+(cd-0xDC00)+0x10000) |
| 5968 |
end+two/function(a,b) |
| 5969 |
return utfchar(byte(a)*256+byte(b)) |
| 5970 |
end+one |
| 5971 |
)^1 )+P("\255\254")*Cs(( |
| 5972 |
four/function(b,a,d,c) |
| 5973 |
local ab=0xFF*byte(a)+byte(b) |
| 5974 |
local cd=0xFF*byte(c)+byte(d) |
| 5975 |
return utfchar((ab-0xD800)*0x400+(cd-0xDC00)+0x10000) |
| 5976 |
end+two/function(b,a) |
| 5977 |
return utfchar(byte(a)*256+byte(b)) |
| 5978 |
end+one |
| 5979 |
)^1 ) |
| 5980 |
function string.toutf(s) |
| 5981 |
return lpegmatch(pattern,s) or s |
| 5982 |
end |
| 5983 |
local validatedutf=Cs ( |
| 5984 |
( |
| 5985 |
patterns.utf8one+patterns.utf8two+patterns.utf8three+patterns.utf8four+P(1)/"�" |
| 5986 |
)^0 |
| 5987 |
) |
| 5988 |
patterns.validatedutf=validatedutf |
| 5989 |
function utf.is_valid(str) |
| 5990 |
return type(str)=="string" and lpegmatch(validatedutf,str) or false |
| 5991 |
end |
| 5992 |
if not utf.len then |
| 5993 |
utf.len=string.utflength or (utf8 and utf8.len) |
| 5994 |
if not utf.len then |
| 5995 |
local n,f=0,1 |
| 5996 |
local utfcharcounter=patterns.utfbom^-1*Cmt ( |
| 5997 |
Cc(1)*patterns.utf8one^1+Cc(2)*patterns.utf8two^1+Cc(3)*patterns.utf8three^1+Cc(4)*patterns.utf8four^1, |
| 5998 |
function(_,t,d) |
| 5999 |
n=n+(t-f)/d |
| 6000 |
f=t |
| 6001 |
return true |
| 6002 |
end |
| 6003 |
)^0 |
| 6004 |
function utf.len(str) |
| 6005 |
n,f=0,1 |
| 6006 |
lpegmatch(utfcharcounter,str or "") |
| 6007 |
return n |
| 6008 |
end |
| 6009 |
end |
| 6010 |
end |
| 6011 |
utf.length=utf.len |
| 6012 |
if not utf.sub then |
| 6013 |
local utflength=utf.length |
| 6014 |
local b,e,n,first,last=0,0,0,0,0 |
| 6015 |
local function slide_zero(s,p) |
| 6016 |
n=n+1 |
| 6017 |
if n>=last then |
| 6018 |
e=p-1 |
| 6019 |
else |
| 6020 |
return p |
| 6021 |
end |
| 6022 |
end |
| 6023 |
local function slide_one(s,p) |
| 6024 |
n=n+1 |
| 6025 |
if n==first then |
| 6026 |
b=p |
| 6027 |
end |
| 6028 |
if n>=last then |
| 6029 |
e=p-1 |
| 6030 |
else |
| 6031 |
return p |
| 6032 |
end |
| 6033 |
end |
| 6034 |
local function slide_two(s,p) |
| 6035 |
n=n+1 |
| 6036 |
if n==first then |
| 6037 |
b=p |
| 6038 |
else |
| 6039 |
return true |
| 6040 |
end |
| 6041 |
end |
| 6042 |
local pattern_zero=Cmt(p_utf8character,slide_zero)^0 |
| 6043 |
local pattern_one=Cmt(p_utf8character,slide_one )^0 |
| 6044 |
local pattern_two=Cmt(p_utf8character,slide_two )^0 |
| 6045 |
local pattern_first=C(p_utf8character) |
| 6046 |
function utf.sub(str,start,stop) |
| 6047 |
if not start then |
| 6048 |
return str |
| 6049 |
end |
| 6050 |
if start==0 then |
| 6051 |
start=1 |
| 6052 |
end |
| 6053 |
if not stop then |
| 6054 |
if start<0 then |
| 6055 |
local l=utflength(str) |
| 6056 |
start=l+start |
| 6057 |
else |
| 6058 |
start=start-1 |
| 6059 |
end |
| 6060 |
b,n,first=0,0,start |
| 6061 |
lpegmatch(pattern_two,str) |
| 6062 |
if n>=first then |
| 6063 |
return sub(str,b) |
| 6064 |
else |
| 6065 |
return "" |
| 6066 |
end |
| 6067 |
end |
| 6068 |
if start<0 or stop<0 then |
| 6069 |
local l=utf.length(str) |
| 6070 |
if start<0 then |
| 6071 |
start=l+start |
| 6072 |
if start<=0 then |
| 6073 |
start=1 |
| 6074 |
else |
| 6075 |
start=start+1 |
| 6076 |
end |
| 6077 |
end |
| 6078 |
if stop<0 then |
| 6079 |
stop=l+stop |
| 6080 |
if stop==0 then |
| 6081 |
stop=1 |
| 6082 |
else |
| 6083 |
stop=stop+1 |
| 6084 |
end |
| 6085 |
end |
| 6086 |
end |
| 6087 |
if start==1 and stop==1 then |
| 6088 |
return lpegmatch(pattern_first,str) or "" |
| 6089 |
elseif start>stop then |
| 6090 |
return "" |
| 6091 |
elseif start>1 then |
| 6092 |
b,e,n,first,last=0,0,0,start-1,stop |
| 6093 |
lpegmatch(pattern_one,str) |
| 6094 |
if n>=first and e==0 then |
| 6095 |
e=#str |
| 6096 |
end |
| 6097 |
return sub(str,b,e) |
| 6098 |
else |
| 6099 |
b,e,n,last=1,0,0,stop |
| 6100 |
lpegmatch(pattern_zero,str) |
| 6101 |
if e==0 then |
| 6102 |
e=#str |
| 6103 |
end |
| 6104 |
return sub(str,b,e) |
| 6105 |
end |
| 6106 |
end |
| 6107 |
end |
| 6108 |
function utf.remapper(mapping,option,action) |
| 6109 |
local variant=type(mapping) |
| 6110 |
if variant=="table" then |
| 6111 |
action=action or mapping |
| 6112 |
if option=="dynamic" then |
| 6113 |
local pattern=false |
| 6114 |
table.setmetatablenewindex(mapping,function(t,k,v) rawset(t,k,v) pattern=false end) |
| 6115 |
return function(str) |
| 6116 |
if not str or str=="" then |
| 6117 |
return "" |
| 6118 |
else |
| 6119 |
if not pattern then |
| 6120 |
pattern=Cs((tabletopattern(mapping)/action+p_utf8character)^0) |
| 6121 |
end |
| 6122 |
return lpegmatch(pattern,str) |
| 6123 |
end |
| 6124 |
end |
| 6125 |
elseif option=="pattern" then |
| 6126 |
return Cs((tabletopattern(mapping)/action+p_utf8character)^0) |
| 6127 |
else |
| 6128 |
local pattern=Cs((tabletopattern(mapping)/action+p_utf8character)^0) |
| 6129 |
return function(str) |
| 6130 |
if not str or str=="" then |
| 6131 |
return "" |
| 6132 |
else |
| 6133 |
return lpegmatch(pattern,str) |
| 6134 |
end |
| 6135 |
end,pattern |
| 6136 |
end |
| 6137 |
elseif variant=="function" then |
| 6138 |
if option=="pattern" then |
| 6139 |
return Cs((p_utf8character/mapping+p_utf8character)^0) |
| 6140 |
else |
| 6141 |
local pattern=Cs((p_utf8character/mapping+p_utf8character)^0) |
| 6142 |
return function(str) |
| 6143 |
if not str or str=="" then |
| 6144 |
return "" |
| 6145 |
else |
| 6146 |
return lpegmatch(pattern,str) |
| 6147 |
end |
| 6148 |
end,pattern |
| 6149 |
end |
| 6150 |
else |
| 6151 |
return function(str) |
| 6152 |
return str or "" |
| 6153 |
end |
| 6154 |
end |
| 6155 |
end |
| 6156 |
function utf.replacer(t) |
| 6157 |
local r=replacer(t,false,false,true) |
| 6158 |
return function(str) |
| 6159 |
return lpegmatch(r,str) |
| 6160 |
end |
| 6161 |
end |
| 6162 |
function utf.subtituter(t) |
| 6163 |
local f=finder (t) |
| 6164 |
local r=replacer(t,false,false,true) |
| 6165 |
return function(str) |
| 6166 |
local i=lpegmatch(f,str) |
| 6167 |
if not i then |
| 6168 |
return str |
| 6169 |
elseif i>#str then |
| 6170 |
return str |
| 6171 |
else |
| 6172 |
return lpegmatch(r,str) |
| 6173 |
end |
| 6174 |
end |
| 6175 |
end |
| 6176 |
local utflinesplitter=p_utfbom^-1*lpeg.tsplitat(p_newline) |
| 6177 |
local utfcharsplitter_ows=p_utfbom^-1*Ct(C(p_utf8character)^0) |
| 6178 |
local utfcharsplitter_iws=p_utfbom^-1*Ct((p_whitespace^1+C(p_utf8character))^0) |
| 6179 |
local utfcharsplitter_raw=Ct(C(p_utf8character)^0) |
| 6180 |
patterns.utflinesplitter=utflinesplitter |
| 6181 |
function utf.splitlines(str) |
| 6182 |
return lpegmatch(utflinesplitter,str or "") |
| 6183 |
end |
| 6184 |
function utf.split(str,ignorewhitespace) |
| 6185 |
if ignorewhitespace then |
| 6186 |
return lpegmatch(utfcharsplitter_iws,str or "") |
| 6187 |
else |
| 6188 |
return lpegmatch(utfcharsplitter_ows,str or "") |
| 6189 |
end |
| 6190 |
end |
| 6191 |
function utf.totable(str) |
| 6192 |
return lpegmatch(utfcharsplitter_raw,str) |
| 6193 |
end |
| 6194 |
function utf.magic(f) |
| 6195 |
local str=f:read(4) or "" |
| 6196 |
local off=lpegmatch(p_utfoffset,str) |
| 6197 |
if off<4 then |
| 6198 |
f:seek('set',off) |
| 6199 |
end |
| 6200 |
return lpegmatch(p_utftype,str) |
| 6201 |
end |
| 6202 |
local utf16_to_utf8_be,utf16_to_utf8_le |
| 6203 |
local utf32_to_utf8_be,utf32_to_utf8_le |
| 6204 |
local utf_16_be_getbom=patterns.utfbom_16_be^-1 |
| 6205 |
local utf_16_le_getbom=patterns.utfbom_16_le^-1 |
| 6206 |
local utf_32_be_getbom=patterns.utfbom_32_be^-1 |
| 6207 |
local utf_32_le_getbom=patterns.utfbom_32_le^-1 |
| 6208 |
local utf_16_be_linesplitter=utf_16_be_getbom*lpeg.tsplitat(patterns.utf_16_be_nl) |
| 6209 |
local utf_16_le_linesplitter=utf_16_le_getbom*lpeg.tsplitat(patterns.utf_16_le_nl) |
| 6210 |
local utf_32_be_linesplitter=utf_32_be_getbom*lpeg.tsplitat(patterns.utf_32_be_nl) |
| 6211 |
local utf_32_le_linesplitter=utf_32_le_getbom*lpeg.tsplitat(patterns.utf_32_le_nl) |
| 6212 |
local more=0 |
| 6213 |
local p_utf16_to_utf8_be=C(1)*C(1)/function(left,right) |
| 6214 |
local now=256*byte(left)+byte(right) |
| 6215 |
if more>0 then |
| 6216 |
now=(more-0xD800)*0x400+(now-0xDC00)+0x10000 |
| 6217 |
more=0 |
| 6218 |
return utfchar(now) |
| 6219 |
elseif now>=0xD800 and now<=0xDBFF then |
| 6220 |
more=now |
| 6221 |
return "" |
| 6222 |
else |
| 6223 |
return utfchar(now) |
| 6224 |
end |
| 6225 |
end |
| 6226 |
local p_utf16_to_utf8_le=C(1)*C(1)/function(right,left) |
| 6227 |
local now=256*byte(left)+byte(right) |
| 6228 |
if more>0 then |
| 6229 |
now=(more-0xD800)*0x400+(now-0xDC00)+0x10000 |
| 6230 |
more=0 |
| 6231 |
return utfchar(now) |
| 6232 |
elseif now>=0xD800 and now<=0xDBFF then |
| 6233 |
more=now |
| 6234 |
return "" |
| 6235 |
else |
| 6236 |
return utfchar(now) |
| 6237 |
end |
| 6238 |
end |
| 6239 |
local p_utf32_to_utf8_be=C(1)*C(1)*C(1)*C(1)/function(a,b,c,d) |
| 6240 |
return utfchar(256*256*256*byte(a)+256*256*byte(b)+256*byte(c)+byte(d)) |
| 6241 |
end |
| 6242 |
local p_utf32_to_utf8_le=C(1)*C(1)*C(1)*C(1)/function(a,b,c,d) |
| 6243 |
return utfchar(256*256*256*byte(d)+256*256*byte(c)+256*byte(b)+byte(a)) |
| 6244 |
end |
| 6245 |
p_utf16_to_utf8_be=P(true)/function() more=0 end*utf_16_be_getbom*Cs(p_utf16_to_utf8_be^0) |
| 6246 |
p_utf16_to_utf8_le=P(true)/function() more=0 end*utf_16_le_getbom*Cs(p_utf16_to_utf8_le^0) |
| 6247 |
p_utf32_to_utf8_be=P(true)/function() more=0 end*utf_32_be_getbom*Cs(p_utf32_to_utf8_be^0) |
| 6248 |
p_utf32_to_utf8_le=P(true)/function() more=0 end*utf_32_le_getbom*Cs(p_utf32_to_utf8_le^0) |
| 6249 |
patterns.utf16_to_utf8_be=p_utf16_to_utf8_be |
| 6250 |
patterns.utf16_to_utf8_le=p_utf16_to_utf8_le |
| 6251 |
patterns.utf32_to_utf8_be=p_utf32_to_utf8_be |
| 6252 |
patterns.utf32_to_utf8_le=p_utf32_to_utf8_le |
| 6253 |
utf16_to_utf8_be=function(s) |
| 6254 |
if s and s~="" then |
| 6255 |
return lpegmatch(p_utf16_to_utf8_be,s) |
| 6256 |
else |
| 6257 |
return s |
| 6258 |
end |
| 6259 |
end |
| 6260 |
local utf16_to_utf8_be_t=function(t) |
| 6261 |
if not t then |
| 6262 |
return nil |
| 6263 |
elseif type(t)=="string" then |
| 6264 |
t=lpegmatch(utf_16_be_linesplitter,t) |
| 6265 |
end |
| 6266 |
for i=1,#t do |
| 6267 |
local s=t[i] |
| 6268 |
if s~="" then |
| 6269 |
t[i]=lpegmatch(p_utf16_to_utf8_be,s) |
| 6270 |
end |
| 6271 |
end |
| 6272 |
return t |
| 6273 |
end |
| 6274 |
utf16_to_utf8_le=function(s) |
| 6275 |
if s and s~="" then |
| 6276 |
return lpegmatch(p_utf16_to_utf8_le,s) |
| 6277 |
else |
| 6278 |
return s |
| 6279 |
end |
| 6280 |
end |
| 6281 |
local utf16_to_utf8_le_t=function(t) |
| 6282 |
if not t then |
| 6283 |
return nil |
| 6284 |
elseif type(t)=="string" then |
| 6285 |
t=lpegmatch(utf_16_le_linesplitter,t) |
| 6286 |
end |
| 6287 |
for i=1,#t do |
| 6288 |
local s=t[i] |
| 6289 |
if s~="" then |
| 6290 |
t[i]=lpegmatch(p_utf16_to_utf8_le,s) |
| 6291 |
end |
| 6292 |
end |
| 6293 |
return t |
| 6294 |
end |
| 6295 |
utf32_to_utf8_be=function(s) |
| 6296 |
if s and s~="" then |
| 6297 |
return lpegmatch(p_utf32_to_utf8_be,s) |
| 6298 |
else |
| 6299 |
return s |
| 6300 |
end |
| 6301 |
end |
| 6302 |
local utf32_to_utf8_be_t=function(t) |
| 6303 |
if not t then |
| 6304 |
return nil |
| 6305 |
elseif type(t)=="string" then |
| 6306 |
t=lpegmatch(utf_32_be_linesplitter,t) |
| 6307 |
end |
| 6308 |
for i=1,#t do |
| 6309 |
local s=t[i] |
| 6310 |
if s~="" then |
| 6311 |
t[i]=lpegmatch(p_utf32_to_utf8_be,s) |
| 6312 |
end |
| 6313 |
end |
| 6314 |
return t |
| 6315 |
end |
| 6316 |
utf32_to_utf8_le=function(s) |
| 6317 |
if s and s~="" then |
| 6318 |
return lpegmatch(p_utf32_to_utf8_le,s) |
| 6319 |
else |
| 6320 |
return s |
| 6321 |
end |
| 6322 |
end |
| 6323 |
local utf32_to_utf8_le_t=function(t) |
| 6324 |
if not t then |
| 6325 |
return nil |
| 6326 |
elseif type(t)=="string" then |
| 6327 |
t=lpegmatch(utf_32_le_linesplitter,t) |
| 6328 |
end |
| 6329 |
for i=1,#t do |
| 6330 |
local s=t[i] |
| 6331 |
if s~="" then |
| 6332 |
t[i]=lpegmatch(p_utf32_to_utf8_le,s) |
| 6333 |
end |
| 6334 |
end |
| 6335 |
return t |
| 6336 |
end |
| 6337 |
utf.utf16_to_utf8_le_t=utf16_to_utf8_le_t |
| 6338 |
utf.utf16_to_utf8_be_t=utf16_to_utf8_be_t |
| 6339 |
utf.utf32_to_utf8_le_t=utf32_to_utf8_le_t |
| 6340 |
utf.utf32_to_utf8_be_t=utf32_to_utf8_be_t |
| 6341 |
utf.utf16_to_utf8_le=utf16_to_utf8_le |
| 6342 |
utf.utf16_to_utf8_be=utf16_to_utf8_be |
| 6343 |
utf.utf32_to_utf8_le=utf32_to_utf8_le |
| 6344 |
utf.utf32_to_utf8_be=utf32_to_utf8_be |
| 6345 |
function utf.utf8_to_utf8_t(t) |
| 6346 |
return type(t)=="string" and lpegmatch(utflinesplitter,t) or t |
| 6347 |
end |
| 6348 |
function utf.utf16_to_utf8_t(t,endian) |
| 6349 |
return endian and utf16_to_utf8_be_t(t) or utf16_to_utf8_le_t(t) or t |
| 6350 |
end |
| 6351 |
function utf.utf32_to_utf8_t(t,endian) |
| 6352 |
return endian and utf32_to_utf8_be_t(t) or utf32_to_utf8_le_t(t) or t |
| 6353 |
end |
| 6354 |
if bit32 then |
| 6355 |
local rshift=bit32.rshift |
| 6356 |
local function little(b) |
| 6357 |
if b<0x10000 then |
| 6358 |
return char(b%256,rshift(b,8)) |
| 6359 |
else |
| 6360 |
b=b-0x10000 |
| 6361 |
local b1=rshift(b,10)+0xD800 |
| 6362 |
local b2=b%1024+0xDC00 |
| 6363 |
return char(b1%256,rshift(b1,8),b2%256,rshift(b2,8)) |
| 6364 |
end |
| 6365 |
end |
| 6366 |
local function big(b) |
| 6367 |
if b<0x10000 then |
| 6368 |
return char(rshift(b,8),b%256) |
| 6369 |
else |
| 6370 |
b=b-0x10000 |
| 6371 |
local b1=rshift(b,10)+0xD800 |
| 6372 |
local b2=b%1024+0xDC00 |
| 6373 |
return char(rshift(b1,8),b1%256,rshift(b2,8),b2%256) |
| 6374 |
end |
| 6375 |
end |
| 6376 |
local l_remap=Cs((p_utf8byte/little+P(1)/"")^0) |
| 6377 |
local b_remap=Cs((p_utf8byte/big+P(1)/"")^0) |
| 6378 |
local function utf8_to_utf16_be(str,nobom) |
| 6379 |
if nobom then |
| 6380 |
return lpegmatch(b_remap,str) |
| 6381 |
else |
| 6382 |
return char(254,255)..lpegmatch(b_remap,str) |
| 6383 |
end |
| 6384 |
end |
| 6385 |
local function utf8_to_utf16_le(str,nobom) |
| 6386 |
if nobom then |
| 6387 |
return lpegmatch(l_remap,str) |
| 6388 |
else |
| 6389 |
return char(255,254)..lpegmatch(l_remap,str) |
| 6390 |
end |
| 6391 |
end |
| 6392 |
utf.utf8_to_utf16_be=utf8_to_utf16_be |
| 6393 |
utf.utf8_to_utf16_le=utf8_to_utf16_le |
| 6394 |
function utf.utf8_to_utf16(str,littleendian,nobom) |
| 6395 |
if littleendian then |
| 6396 |
return utf8_to_utf16_le(str,nobom) |
| 6397 |
else |
| 6398 |
return utf8_to_utf16_be(str,nobom) |
| 6399 |
end |
| 6400 |
end |
| 6401 |
end |
| 6402 |
local pattern=Cs ( |
| 6403 |
(p_utf8byte/function(unicode ) return format("0x%04X",unicode) end)*(p_utf8byte*Carg(1)/function(unicode,separator) return format("%s0x%04X",separator,unicode) end)^0 |
| 6404 |
) |
| 6405 |
function utf.tocodes(str,separator) |
| 6406 |
return lpegmatch(pattern,str,1,separator or " ") |
| 6407 |
end |
| 6408 |
function utf.ustring(s) |
| 6409 |
return format("U+%05X",type(s)=="number" and s or utfbyte(s)) |
| 6410 |
end |
| 6411 |
function utf.xstring(s) |
| 6412 |
return format("0x%05X",type(s)=="number" and s or utfbyte(s)) |
| 6413 |
end |
| 6414 |
function utf.toeight(str) |
| 6415 |
if not str or str=="" then |
| 6416 |
return nil |
| 6417 |
end |
| 6418 |
local utftype=lpegmatch(p_utfstricttype,str) |
| 6419 |
if utftype=="utf-8" then |
| 6420 |
return sub(str,4) |
| 6421 |
elseif utftype=="utf-16-be" then |
| 6422 |
return utf16_to_utf8_be(str) |
| 6423 |
elseif utftype=="utf-16-le" then |
| 6424 |
return utf16_to_utf8_le(str) |
| 6425 |
else |
| 6426 |
return str |
| 6427 |
end |
| 6428 |
end |
| 6429 |
do |
| 6430 |
local p_nany=p_utf8character/"" |
| 6431 |
local cache={} |
| 6432 |
function utf.count(str,what) |
| 6433 |
if type(what)=="string" then |
| 6434 |
local p=cache[what] |
| 6435 |
if not p then |
| 6436 |
p=Cs((P(what)/" "+p_nany)^0) |
| 6437 |
cache[p]=p |
| 6438 |
end |
| 6439 |
return #lpegmatch(p,str) |
| 6440 |
else |
| 6441 |
return #lpegmatch(Cs((P(what)/" "+p_nany)^0),str) |
| 6442 |
end |
| 6443 |
end |
| 6444 |
end |
| 6445 |
if not string.utfvalues then |
| 6446 |
local find=string.find |
| 6447 |
local dummy=function() |
| 6448 |
end |
| 6449 |
function string.utfvalues(str) |
| 6450 |
local n=#str |
| 6451 |
if n==0 then |
| 6452 |
return dummy |
| 6453 |
elseif n==1 then |
| 6454 |
return function() return utfbyte(str) end |
| 6455 |
else |
| 6456 |
local p=1 |
| 6457 |
return function() |
| 6458 |
local b,e=find(str,".[\128-\191]*",p) |
| 6459 |
if b then |
| 6460 |
p=e+1 |
| 6461 |
return utfbyte(sub(str,b,e)) |
| 6462 |
end |
| 6463 |
end |
| 6464 |
end |
| 6465 |
end |
| 6466 |
end |
| 6467 |
utf.values=string.utfvalues |
| 6468 |
function utf.chrlen(u) |
| 6469 |
return |
| 6470 |
(u<0x80 and 1) or |
| 6471 |
(u<0xE0 and 2) or |
| 6472 |
(u<0xF0 and 3) or |
| 6473 |
(u<0xF8 and 4) or |
| 6474 |
(u<0xFC and 5) or |
| 6475 |
(u<0xFE and 6) or 0 |
| 6476 |
end |
| 6477 |
if bit32 then |
| 6478 |
local extract=bit32.extract |
| 6479 |
local char=string.char |
| 6480 |
function utf.toutf32string(n) |
| 6481 |
if n<=0xFF then |
| 6482 |
return |
| 6483 |
char(n).."\000\000\000" |
| 6484 |
elseif n<=0xFFFF then |
| 6485 |
return |
| 6486 |
char(extract(n,0,8))..char(extract(n,8,8)).."\000\000" |
| 6487 |
elseif n<=0xFFFFFF then |
| 6488 |
return |
| 6489 |
char(extract(n,0,8))..char(extract(n,8,8))..char(extract(n,16,8)).."\000" |
| 6490 |
else |
| 6491 |
return |
| 6492 |
char(extract(n,0,8))..char(extract(n,8,8))..char(extract(n,16,8))..char(extract(n,24,8)) |
| 6493 |
end |
| 6494 |
end |
| 6495 |
end |
| 6496 |
local len=utf.len |
| 6497 |
local rep=rep |
| 6498 |
function string.utfpadd(s,n) |
| 6499 |
if n and n~=0 then |
| 6500 |
local l=len(s) |
| 6501 |
if n>0 then |
| 6502 |
local d=n-l |
| 6503 |
if d>0 then |
| 6504 |
return rep(c or " ",d)..s |
| 6505 |
end |
| 6506 |
else |
| 6507 |
local d=- n-l |
| 6508 |
if d>0 then |
| 6509 |
return s..rep(c or " ",d) |
| 6510 |
end |
| 6511 |
end |
| 6512 |
end |
| 6513 |
return s |
| 6514 |
end |
| 6515 |
do |
| 6516 |
local utfcharacters=utf.characters or string.utfcharacters |
| 6517 |
local utfchar=utf.char or string.utfcharacter |
| 6518 |
lpeg.UP=P |
| 6519 |
if utfcharacters then |
| 6520 |
function lpeg.US(str) |
| 6521 |
local p=P(false) |
| 6522 |
for uc in utfcharacters(str) do |
| 6523 |
p=p+P(uc) |
| 6524 |
end |
| 6525 |
return p |
| 6526 |
end |
| 6527 |
else |
| 6528 |
function lpeg.US(str) |
| 6529 |
local p=P(false) |
| 6530 |
local f=function(uc) |
| 6531 |
p=p+P(uc) |
| 6532 |
end |
| 6533 |
lpegmatch((p_utf8char/f)^0,str) |
| 6534 |
return p |
| 6535 |
end |
| 6536 |
end |
| 6537 |
local range=p_utf8byte*p_utf8byte+Cc(false) |
| 6538 |
function lpeg.UR(str,more) |
| 6539 |
local first,last |
| 6540 |
if type(str)=="number" then |
| 6541 |
first=str |
| 6542 |
last=more or first |
| 6543 |
else |
| 6544 |
first,last=lpegmatch(range,str) |
| 6545 |
if not last then |
| 6546 |
return P(str) |
| 6547 |
end |
| 6548 |
end |
| 6549 |
if first==last then |
| 6550 |
return P(str) |
| 6551 |
end |
| 6552 |
if not utfchar then |
| 6553 |
utfchar=utf.char |
| 6554 |
end |
| 6555 |
if utfchar and (last-first<8) then |
| 6556 |
local p=P(false) |
| 6557 |
for i=first,last do |
| 6558 |
p=p+P(utfchar(i)) |
| 6559 |
end |
| 6560 |
return p |
| 6561 |
else |
| 6562 |
local f=function(b) |
| 6563 |
return b>=first and b<=last |
| 6564 |
end |
| 6565 |
return p_utf8byte/f |
| 6566 |
end |
| 6567 |
end |
| 6568 |
end |
| 6569 |
|
| 6570 |
|
| 6571 |
end -- of closure |
| 6572 |
|
| 6573 |
do -- create closure to overcome 200 locals limit |
| 6574 |
|
| 6575 |
package.loaded["l-math"] = package.loaded["l-math"] or true |
| 6576 |
|
| 6577 |
-- original size: 3260, stripped down to: 2345 |
| 6578 |
|
| 6579 |
if not modules then modules={} end modules ['l-math']={ |
| 6580 |
version=1.001, |
| 6581 |
comment="companion to luat-lib.mkiv", |
| 6582 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 6583 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 6584 |
license="see context related readme files" |
| 6585 |
} |
| 6586 |
if not math.ceiling then |
| 6587 |
math.ceiling=math.ceil |
| 6588 |
end |
| 6589 |
if not math.round then |
| 6590 |
if xmath then |
| 6591 |
math.round=xmath.round |
| 6592 |
else |
| 6593 |
local floor=math.floor |
| 6594 |
function math.round(x) |
| 6595 |
return x<0 and -floor(-x+0.5) or floor(x+0.5) |
| 6596 |
end |
| 6597 |
end |
| 6598 |
end |
| 6599 |
if not math.div then |
| 6600 |
local floor=math.floor |
| 6601 |
function math.div(n,m) return floor(n/m) end |
| 6602 |
end |
| 6603 |
if not math.mod then |
| 6604 |
function math.mod(n,m) return n%m end |
| 6605 |
end |
| 6606 |
if not math.sind then |
| 6607 |
local sin,cos,tan=math.sin,math.cos,math.tan |
| 6608 |
local pipi=2*math.pi/360 |
| 6609 |
function math.sind(d) return sin(d*pipi) end |
| 6610 |
function math.cosd(d) return cos(d*pipi) end |
| 6611 |
function math.tand(d) return tan(d*pipi) end |
| 6612 |
end |
| 6613 |
if not math.odd then |
| 6614 |
function math.odd (n) return n%2~=0 end |
| 6615 |
function math.even(n) return n%2==0 end |
| 6616 |
end |
| 6617 |
if not math.cosh then |
| 6618 |
local exp=math.exp |
| 6619 |
function math.cosh(x) |
| 6620 |
local xx=exp(x) |
| 6621 |
return (xx+1/xx)/2 |
| 6622 |
end |
| 6623 |
function math.sinh(x) |
| 6624 |
local xx=exp(x) |
| 6625 |
return (xx-1/xx)/2 |
| 6626 |
end |
| 6627 |
function math.tanh(x) |
| 6628 |
local xx=exp(x) |
| 6629 |
return (xx-1/xx)/(xx+1/xx) |
| 6630 |
end |
| 6631 |
end |
| 6632 |
if not math.pow then |
| 6633 |
function math.pow(x,y) |
| 6634 |
return x^y |
| 6635 |
end |
| 6636 |
end |
| 6637 |
if not math.atan2 then |
| 6638 |
math.atan2=math.atan |
| 6639 |
end |
| 6640 |
if not math.ldexp then |
| 6641 |
function math.ldexp(x,e) |
| 6642 |
return x*2.0^e |
| 6643 |
end |
| 6644 |
end |
| 6645 |
if not math.log10 then |
| 6646 |
local log=math.log |
| 6647 |
function math.log10(x) |
| 6648 |
return log(x,10) |
| 6649 |
end |
| 6650 |
end |
| 6651 |
if not math.type then |
| 6652 |
function math.type() |
| 6653 |
return "float" |
| 6654 |
end |
| 6655 |
end |
| 6656 |
if not math.tointeger then |
| 6657 |
math.mininteger=-0x4FFFFFFFFFFF |
| 6658 |
math.maxinteger=0x4FFFFFFFFFFF |
| 6659 |
local floor=math.floor |
| 6660 |
function math.tointeger(n) |
| 6661 |
local f=floor(n) |
| 6662 |
return f==n and f or nil |
| 6663 |
end |
| 6664 |
end |
| 6665 |
if not math.ult then |
| 6666 |
local floor=math.floor |
| 6667 |
function math.ult(m,n) |
| 6668 |
return floor(m)<floor(n) |
| 6669 |
end |
| 6670 |
end |
| 6671 |
if setinspector and vector then |
| 6672 |
local inspect=inspect |
| 6673 |
local isvector=vector.isvector |
| 6674 |
local totable=vector.totable |
| 6675 |
setinspector("vector",function(v) |
| 6676 |
if isvector(v) then |
| 6677 |
inspect(totable(v)) |
| 6678 |
return true |
| 6679 |
end |
| 6680 |
end) |
| 6681 |
local mesh=vector.mesh |
| 6682 |
if mesh then |
| 6683 |
local ismesh=mesh.ismesh |
| 6684 |
local totable=mesh.totable |
| 6685 |
setinspector("mesh",function(v) |
| 6686 |
if ismesh(v) then |
| 6687 |
inspect(totable(v)) |
| 6688 |
return true |
| 6689 |
end |
| 6690 |
end) |
| 6691 |
end |
| 6692 |
end |
| 6693 |
|
| 6694 |
|
| 6695 |
end -- of closure |
| 6696 |
|
| 6697 |
do -- create closure to overcome 200 locals limit |
| 6698 |
|
| 6699 |
package.loaded["util-str"] = package.loaded["util-str"] or true |
| 6700 |
|
| 6701 |
-- original size: 46220, stripped down to: 24637 |
| 6702 |
|
| 6703 |
if not modules then modules={} end modules ['util-str']={ |
| 6704 |
version=1.001, |
| 6705 |
comment="companion to luat-lib.mkiv", |
| 6706 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 6707 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 6708 |
license="see context related readme files" |
| 6709 |
} |
| 6710 |
utilities=utilities or {} |
| 6711 |
utilities.strings=utilities.strings or {} |
| 6712 |
local strings=utilities.strings |
| 6713 |
local format,gsub,rep,sub,find,char=string.format,string.gsub,string.rep,string.sub,string.find,string.char |
| 6714 |
local load,dump=load,string.dump |
| 6715 |
local tonumber,type,tostring,next,setmetatable=tonumber,type,tostring,next,setmetatable |
| 6716 |
local unpack,concat=table.unpack,table.concat |
| 6717 |
local P,V,C,S,R,Ct,Cs,Cp,Carg,Cc=lpeg.P,lpeg.V,lpeg.C,lpeg.S,lpeg.R,lpeg.Ct,lpeg.Cs,lpeg.Cp,lpeg.Carg,lpeg.Cc |
| 6718 |
local patterns,lpegmatch=lpeg.patterns,lpeg.match |
| 6719 |
local tsplitat=lpeg.tsplitat |
| 6720 |
local utfchar,utfbyte,utflen=utf.char,utf.byte,utf.len |
| 6721 |
local loadstripped=function(str,shortcuts) |
| 6722 |
if shortcuts then |
| 6723 |
return load(dump(load(str),true),nil,nil,shortcuts) |
| 6724 |
else |
| 6725 |
return load(dump(load(str),true)) |
| 6726 |
end |
| 6727 |
end |
| 6728 |
if not number then number={} end |
| 6729 |
local stripzero=patterns.stripzero |
| 6730 |
local stripzeros=patterns.stripzeros |
| 6731 |
local newline=patterns.newline |
| 6732 |
local endofstring=patterns.endofstring |
| 6733 |
local anything=patterns.anything |
| 6734 |
local whitespace=patterns.whitespace |
| 6735 |
local space=patterns.space |
| 6736 |
local spacer=patterns.spacer |
| 6737 |
local spaceortab=patterns.spaceortab |
| 6738 |
local digit=patterns.digit |
| 6739 |
local sign=patterns.sign |
| 6740 |
local period=patterns.period |
| 6741 |
local ptf=1/65536 |
| 6742 |
local bpf=(7200/7227)/65536 |
| 6743 |
local function points(n) |
| 6744 |
if n==0 then |
| 6745 |
return "0pt" |
| 6746 |
end |
| 6747 |
n=tonumber(n) |
| 6748 |
if not n or n==0 then |
| 6749 |
return "0pt" |
| 6750 |
end |
| 6751 |
n=n*ptf |
| 6752 |
if n%1==0 then |
| 6753 |
return format("%ipt",n) |
| 6754 |
else |
| 6755 |
return lpegmatch(stripzeros,format("%.5fpt",n)) |
| 6756 |
end |
| 6757 |
end |
| 6758 |
local function nupoints(n) |
| 6759 |
if n==0 then |
| 6760 |
return "0" |
| 6761 |
end |
| 6762 |
n=tonumber(n) |
| 6763 |
if not n or n==0 then |
| 6764 |
return "0" |
| 6765 |
end |
| 6766 |
n=n*ptf |
| 6767 |
if n%1==0 then |
| 6768 |
return format("%i",n) |
| 6769 |
else |
| 6770 |
return format("%.5f",n) |
| 6771 |
end |
| 6772 |
end |
| 6773 |
local function basepoints(n) |
| 6774 |
if n==0 then |
| 6775 |
return "0bp" |
| 6776 |
end |
| 6777 |
n=tonumber(n) |
| 6778 |
if not n or n==0 then |
| 6779 |
return "0bp" |
| 6780 |
end |
| 6781 |
n=n*bpf |
| 6782 |
if n%1==0 then |
| 6783 |
return format("%ibp",n) |
| 6784 |
else |
| 6785 |
return lpegmatch(stripzeros,format("%.5fbp",n)) |
| 6786 |
end |
| 6787 |
end |
| 6788 |
local function nubasepoints(n) |
| 6789 |
if n==0 then |
| 6790 |
return "0" |
| 6791 |
end |
| 6792 |
n=tonumber(n) |
| 6793 |
if not n or n==0 then |
| 6794 |
return "0" |
| 6795 |
end |
| 6796 |
n=n*bpf |
| 6797 |
if n%1==0 then |
| 6798 |
return format("%i",n) |
| 6799 |
else |
| 6800 |
return format("%.5f",n) |
| 6801 |
end |
| 6802 |
end |
| 6803 |
number.points=points |
| 6804 |
number.nupoints=nupoints |
| 6805 |
number.basepoints=basepoints |
| 6806 |
number.nubasepoints=nubasepoints |
| 6807 |
local rubish=spaceortab^0*newline |
| 6808 |
local anyrubish=spaceortab+newline |
| 6809 |
local stripped=(spaceortab^1/"")*newline |
| 6810 |
local leading=rubish^0/"" |
| 6811 |
local trailing=(anyrubish^1*endofstring)/"" |
| 6812 |
local redundant=rubish^3/"\n" |
| 6813 |
local pattern=Cs(leading*(trailing+redundant+stripped+anything)^0) |
| 6814 |
function strings.collapsecrlf(str) |
| 6815 |
return lpegmatch(pattern,str) |
| 6816 |
end |
| 6817 |
local repeaters={} |
| 6818 |
function strings.newrepeater(str,offset) |
| 6819 |
offset=offset or 0 |
| 6820 |
local s=repeaters[str] |
| 6821 |
if not s then |
| 6822 |
s={} |
| 6823 |
repeaters[str]=s |
| 6824 |
end |
| 6825 |
local t=s[offset] |
| 6826 |
if t then |
| 6827 |
return t |
| 6828 |
end |
| 6829 |
t={} |
| 6830 |
setmetatable(t,{ __index=function(t,k) |
| 6831 |
if not k then |
| 6832 |
return "" |
| 6833 |
end |
| 6834 |
local n=k+offset |
| 6835 |
local s=n>0 and rep(str,n) or "" |
| 6836 |
t[k]=s |
| 6837 |
return s |
| 6838 |
end }) |
| 6839 |
s[offset]=t |
| 6840 |
return t |
| 6841 |
end |
| 6842 |
local extra,tab,start=0,0,4,0 |
| 6843 |
local nspaces=strings.newrepeater(" ") |
| 6844 |
string.nspaces=nspaces |
| 6845 |
local pattern=Carg(1)/function(t) |
| 6846 |
extra,tab,start=0,t or 7,1 |
| 6847 |
end*Cs(( |
| 6848 |
Cp()*patterns.tab/function(position) |
| 6849 |
local current=(position-start+1)+extra |
| 6850 |
local spaces=tab-(current-1)%tab |
| 6851 |
if spaces>0 then |
| 6852 |
extra=extra+spaces-1 |
| 6853 |
return nspaces[spaces] |
| 6854 |
else |
| 6855 |
return "" |
| 6856 |
end |
| 6857 |
end+newline*Cp()/function(position) |
| 6858 |
extra,start=0,position |
| 6859 |
end+anything |
| 6860 |
)^1) |
| 6861 |
function strings.tabtospace(str,tab) |
| 6862 |
return lpegmatch(pattern,str,1,tab or 7) |
| 6863 |
end |
| 6864 |
function string.utfpadding(s,n) |
| 6865 |
if not n or n==0 then |
| 6866 |
return "" |
| 6867 |
end |
| 6868 |
local l=utflen(s) |
| 6869 |
if n>0 then |
| 6870 |
return nspaces[n-l] |
| 6871 |
else |
| 6872 |
return nspaces[-n-l] |
| 6873 |
end |
| 6874 |
end |
| 6875 |
local optionalspace=spacer^0 |
| 6876 |
local nospace=optionalspace/"" |
| 6877 |
local endofline=nospace*newline |
| 6878 |
local stripend=(whitespace^1*endofstring)/"" |
| 6879 |
local normalline=(nospace*((1-optionalspace*(newline+endofstring))^1)*nospace) |
| 6880 |
local stripempty=endofline^1/"" |
| 6881 |
local normalempty=endofline^1 |
| 6882 |
local singleempty=endofline*(endofline^0/"") |
| 6883 |
local doubleempty=endofline*endofline^-1*(endofline^0/"") |
| 6884 |
local stripstart=stripempty^0 |
| 6885 |
local intospace=whitespace^1/" " |
| 6886 |
local noleading=whitespace^1/"" |
| 6887 |
local notrailing=noleading*endofstring |
| 6888 |
local p_prune_normal=Cs (stripstart*(stripend+normalline+normalempty )^0 ) |
| 6889 |
local p_prune_collapse=Cs (stripstart*(stripend+normalline+doubleempty )^0 ) |
| 6890 |
local p_prune_noempty=Cs (stripstart*(stripend+normalline+singleempty )^0 ) |
| 6891 |
local p_prune_intospace=Cs (noleading*(notrailing+intospace+1 )^0 ) |
| 6892 |
local p_retain_normal=Cs ((normalline+normalempty )^0 ) |
| 6893 |
local p_retain_collapse=Cs ((normalline+doubleempty )^0 ) |
| 6894 |
local p_retain_noempty=Cs ((normalline+singleempty )^0 ) |
| 6895 |
local p_collapse_all=Cs (stripstart*(stripend+((whitespace+newline)^1/" ")+1)^0 ) |
| 6896 |
local striplinepatterns={ |
| 6897 |
["prune"]=p_prune_normal, |
| 6898 |
["prune and collapse"]=p_prune_collapse, |
| 6899 |
["prune and no empty"]=p_prune_noempty, |
| 6900 |
["prune and to space"]=p_prune_intospace, |
| 6901 |
["retain"]=p_retain_normal, |
| 6902 |
["retain and collapse"]=p_retain_collapse, |
| 6903 |
["retain and no empty"]=p_retain_noempty, |
| 6904 |
["collapse all"]=p_collapse_all, |
| 6905 |
["collapse"]=patterns.collapser, |
| 6906 |
} |
| 6907 |
setmetatable(striplinepatterns,{ __index=function(t,k) return p_prune_collapse end }) |
| 6908 |
strings.striplinepatterns=striplinepatterns |
| 6909 |
function strings.striplines(str,how) |
| 6910 |
return str and lpegmatch(striplinepatterns[how],str) or str |
| 6911 |
end |
| 6912 |
function strings.collapse(str) |
| 6913 |
return str and lpegmatch(p_prune_intospace,str) or str |
| 6914 |
end |
| 6915 |
strings.striplong=strings.striplines |
| 6916 |
function strings.nice(str) |
| 6917 |
str=gsub(str,"[:%-+_]+"," ") |
| 6918 |
return str |
| 6919 |
end |
| 6920 |
local n=0 |
| 6921 |
local sequenced=table.sequenced |
| 6922 |
function string.autodouble(s,sep) |
| 6923 |
if s==nil then |
| 6924 |
return '""' |
| 6925 |
end |
| 6926 |
local t=type(s) |
| 6927 |
if t=="number" then |
| 6928 |
return tostring(s) |
| 6929 |
end |
| 6930 |
if t=="table" then |
| 6931 |
return ('"'..sequenced(s,sep or ",")..'"') |
| 6932 |
end |
| 6933 |
return ('"'..tostring(s)..'"') |
| 6934 |
end |
| 6935 |
function string.autosingle(s,sep) |
| 6936 |
if s==nil then |
| 6937 |
return "''" |
| 6938 |
end |
| 6939 |
local t=type(s) |
| 6940 |
if t=="number" then |
| 6941 |
return tostring(s) |
| 6942 |
end |
| 6943 |
if t=="table" then |
| 6944 |
return ("'"..sequenced(s,sep or ",").."'") |
| 6945 |
end |
| 6946 |
return ("'"..tostring(s).."'") |
| 6947 |
end |
| 6948 |
local tracedchars={ [0]= |
| 6949 |
"[null]","[soh]","[stx]","[etx]","[eot]","[enq]","[ack]","[bel]", |
| 6950 |
"[bs]","[ht]","[lf]","[vt]","[ff]","[cr]","[so]","[si]", |
| 6951 |
"[dle]","[dc1]","[dc2]","[dc3]","[dc4]","[nak]","[syn]","[etb]", |
| 6952 |
"[can]","[em]","[sub]","[esc]","[fs]","[gs]","[rs]","[us]", |
| 6953 |
"[space]", |
| 6954 |
} |
| 6955 |
string.tracedchars=tracedchars |
| 6956 |
strings.tracers=tracedchars |
| 6957 |
function string.tracedchar(b) |
| 6958 |
if type(b)=="number" then |
| 6959 |
return tracedchars[b] or (utfchar(b).." (U+"..format("%05X",b)..")") |
| 6960 |
else |
| 6961 |
local c=utfbyte(b) |
| 6962 |
return tracedchars[c] or (b.." (U+"..(c and format("%05X",c) or "?????")..")") |
| 6963 |
end |
| 6964 |
end |
| 6965 |
function number.signed(i) |
| 6966 |
if i>0 then |
| 6967 |
return "+",i |
| 6968 |
else |
| 6969 |
return "-",-i |
| 6970 |
end |
| 6971 |
end |
| 6972 |
local two=digit*digit |
| 6973 |
local three=two*digit |
| 6974 |
local prefix=(Carg(1)*three)^1 |
| 6975 |
local splitter=Cs ( |
| 6976 |
(((1-(three^1*period))^1+C(three))*prefix+C((1-period)^1))*(anything/""*Carg(2))*C(2) |
| 6977 |
) |
| 6978 |
local splitter3=Cs ( |
| 6979 |
three*prefix*endofstring+two*prefix*endofstring+digit*prefix*endofstring+three+two+digit |
| 6980 |
) |
| 6981 |
patterns.formattednumber=splitter |
| 6982 |
function number.formatted(n,sep1,sep2) |
| 6983 |
if sep1==false then |
| 6984 |
if type(n)=="number" then |
| 6985 |
n=tostring(n) |
| 6986 |
end |
| 6987 |
return lpegmatch(splitter3,n,1,sep2 or ".") |
| 6988 |
else |
| 6989 |
if type(n)=="number" then |
| 6990 |
n=format("%0.2f",n) |
| 6991 |
end |
| 6992 |
if sep1==true then |
| 6993 |
return lpegmatch(splitter,n,1,".",",") |
| 6994 |
elseif sep1=="." then |
| 6995 |
return lpegmatch(splitter,n,1,sep1,sep2 or ",") |
| 6996 |
elseif sep1=="," then |
| 6997 |
return lpegmatch(splitter,n,1,sep1,sep2 or ".") |
| 6998 |
else |
| 6999 |
return lpegmatch(splitter,n,1,sep1 or ",",sep2 or ".") |
| 7000 |
end |
| 7001 |
end |
| 7002 |
end |
| 7003 |
local p=Cs( |
| 7004 |
P("-")^0*(P("0")^1/"")^0*(1-period)^0*(period*P("0")^1*endofstring/""+period^0)*P(1-P("0")^1*endofstring)^0 |
| 7005 |
) |
| 7006 |
function number.compactfloat(n,fmt) |
| 7007 |
if n==0 then |
| 7008 |
return "0" |
| 7009 |
elseif n==1 then |
| 7010 |
return "1" |
| 7011 |
end |
| 7012 |
n=lpegmatch(p,format(fmt or "%0.3f",n)) |
| 7013 |
if n=="." or n=="" or n=="-" then |
| 7014 |
return "0" |
| 7015 |
end |
| 7016 |
return n |
| 7017 |
end |
| 7018 |
local zero=P("0")^1/"" |
| 7019 |
local plus=P("+")/"" |
| 7020 |
local minus=P("-") |
| 7021 |
local separator=period |
| 7022 |
local trailing=zero^1*#S("eE") |
| 7023 |
local exponent=(S("eE")*(plus+Cs((minus*zero^0*endofstring)/"")+minus)*zero^0*(endofstring*Cc("0")+anything^1)) |
| 7024 |
local pattern_a=Cs(minus^0*digit^1*(separator/""*trailing+separator*(trailing+digit)^0)*exponent) |
| 7025 |
local pattern_b=Cs((exponent+anything)^0) |
| 7026 |
function number.sparseexponent(f,n) |
| 7027 |
if not n then |
| 7028 |
n=f |
| 7029 |
f="%e" |
| 7030 |
end |
| 7031 |
local tn=type(n) |
| 7032 |
if tn=="string" then |
| 7033 |
local m=tonumber(n) |
| 7034 |
if m then |
| 7035 |
return lpegmatch((f=="%e" or f=="%E") and pattern_a or pattern_b,format(f,m)) |
| 7036 |
end |
| 7037 |
elseif tn=="number" then |
| 7038 |
return lpegmatch((f=="%e" or f=="%E") and pattern_a or pattern_b,format(f,n)) |
| 7039 |
end |
| 7040 |
return tostring(n) |
| 7041 |
end |
| 7042 |
local hf={} |
| 7043 |
local hs={} |
| 7044 |
setmetatable(hf,{ __index=function(t,k) |
| 7045 |
local v="%."..k.."f" |
| 7046 |
t[k]=v |
| 7047 |
return v |
| 7048 |
end } ) |
| 7049 |
setmetatable(hs,{ __index=function(t,k) |
| 7050 |
local v="%"..k.."s" |
| 7051 |
t[k]=v |
| 7052 |
return v |
| 7053 |
end } ) |
| 7054 |
function number.formattedfloat(n,b,a) |
| 7055 |
local s=format(hf[a],n) |
| 7056 |
local l=(b or 0)+(a or 0)+1 |
| 7057 |
if #s<l then |
| 7058 |
return format(hs[l],s) |
| 7059 |
else |
| 7060 |
return s |
| 7061 |
end |
| 7062 |
end |
| 7063 |
local template=[[ |
| 7064 |
%s |
| 7065 |
%s |
| 7066 |
return function(%s) return %s end |
| 7067 |
]] |
| 7068 |
local pattern=Cs(Cc('"')*( |
| 7069 |
(1-S('"\\\n\r'))^1+P('"')/'\\"'+P('\\')/'\\\\'+P('\n')/'\\n'+P('\r')/'\\r' |
| 7070 |
)^0*Cc('"')) |
| 7071 |
patterns.escapedquotes=pattern |
| 7072 |
function string.escapedquotes(s) |
| 7073 |
return lpegmatch(pattern,s) |
| 7074 |
end |
| 7075 |
local pattern=(1-P("\\"))^1;pattern=Cs ( |
| 7076 |
pattern*((P("\\")/""*(digit^-3/function(s) return char(tonumber(s)) end))+pattern )^1 |
| 7077 |
) |
| 7078 |
patterns.unescapedquotes=pattern |
| 7079 |
function string.unescapedquotes(s) |
| 7080 |
return lpegmatch(pattern,s) or s |
| 7081 |
end |
| 7082 |
string.texnewlines=lpeg.replacer(patterns.newline,"\r",true) |
| 7083 |
local preamble="" |
| 7084 |
local environment={ |
| 7085 |
["global"]=_G, |
| 7086 |
lpeg=lpeg, |
| 7087 |
type=type, |
| 7088 |
tostring=tostring, |
| 7089 |
tonumber=tonumber, |
| 7090 |
format=string.format, |
| 7091 |
concat=table.concat, |
| 7092 |
signed=number.signed, |
| 7093 |
points=number.points, |
| 7094 |
nupoints=number.nupoints, |
| 7095 |
basepoints=number.basepoints, |
| 7096 |
nubasepoints=number.nubasepoints, |
| 7097 |
utfchar=utf.char, |
| 7098 |
utfbyte=utf.byte, |
| 7099 |
lpegmatch=lpeg.match, |
| 7100 |
nspaces=string.nspaces, |
| 7101 |
utfpadding=string.utfpadding, |
| 7102 |
tracedchar=string.tracedchar, |
| 7103 |
autosingle=string.autosingle, |
| 7104 |
autodouble=string.autodouble, |
| 7105 |
sequenced=table.sequenced, |
| 7106 |
formattednumber=number.formatted, |
| 7107 |
sparseexponent=number.sparseexponent, |
| 7108 |
formattedfloat=number.formattedfloat, |
| 7109 |
stripzero=patterns.stripzero, |
| 7110 |
stripzeros=patterns.stripzeros, |
| 7111 |
escapedquotes=string.escapedquotes, |
| 7112 |
FORMAT=string.f6, |
| 7113 |
} |
| 7114 |
local arguments={ "a1" } |
| 7115 |
setmetatable(arguments,{ __index=function(t,k) |
| 7116 |
local v=t[k-1]..",a"..k |
| 7117 |
t[k]=v |
| 7118 |
return v |
| 7119 |
end |
| 7120 |
}) |
| 7121 |
local prefix_any=C((sign+space+period+digit)^0) |
| 7122 |
local prefix_sub=(C((sign+digit)^0)+Cc(0))*period*(C((sign+digit)^0)+Cc(0)) |
| 7123 |
local prefix_tab=P("{")*C((1-P("}"))^0)*P("}")+C((1-R("az","AZ","09","%%"))^0) |
| 7124 |
local format_s=function(f) |
| 7125 |
n=n+1 |
| 7126 |
if f and f~="" then |
| 7127 |
return format("format('%%%ss',a%s)",f,n) |
| 7128 |
else |
| 7129 |
return format("(a%s or '')",n) |
| 7130 |
end |
| 7131 |
end |
| 7132 |
local format_S=function(f) |
| 7133 |
n=n+1 |
| 7134 |
if f and f~="" then |
| 7135 |
return format("format('%%%ss',tostring(a%s))",f,n) |
| 7136 |
else |
| 7137 |
return format("tostring(a%s)",n) |
| 7138 |
end |
| 7139 |
end |
| 7140 |
local format_right=function(f) |
| 7141 |
n=n+1 |
| 7142 |
f=tonumber(f) |
| 7143 |
if not f or f==0 then |
| 7144 |
return format("(a%s or '')",n) |
| 7145 |
elseif f>0 then |
| 7146 |
return format("utfpadding(a%s,%i)..a%s",n,f,n) |
| 7147 |
else |
| 7148 |
return format("a%s..utfpadding(a%s,%i)",n,n,f) |
| 7149 |
end |
| 7150 |
end |
| 7151 |
local format_left=function(f) |
| 7152 |
n=n+1 |
| 7153 |
f=tonumber(f) |
| 7154 |
if not f or f==0 then |
| 7155 |
return format("(a%s or '')",n) |
| 7156 |
end |
| 7157 |
if f<0 then |
| 7158 |
return format("utfpadding(a%s,%i)..a%s",n,-f,n) |
| 7159 |
else |
| 7160 |
return format("a%s..utfpadding(a%s,%i)",n,n,-f) |
| 7161 |
end |
| 7162 |
end |
| 7163 |
local format_q=JITSUPPORTED and function() |
| 7164 |
n=n+1 |
| 7165 |
return format("(a%s ~= nil and format('%%q',tostring(a%s)) or '')",n,n) |
| 7166 |
end or function() |
| 7167 |
n=n+1 |
| 7168 |
return format("(a%s ~= nil and format('%%q',a%s) or '')",n,n) |
| 7169 |
end |
| 7170 |
local format_Q=function() |
| 7171 |
n=n+1 |
| 7172 |
return format("escapedquotes(tostring(a%s))",n) |
| 7173 |
end |
| 7174 |
local format_i=function(f) |
| 7175 |
n=n+1 |
| 7176 |
if f and f~="" then |
| 7177 |
return format("format('%%%si',a%s)",f,n) |
| 7178 |
else |
| 7179 |
return format("format('%%i',a%s)",n) |
| 7180 |
end |
| 7181 |
end |
| 7182 |
local format_d=format_i |
| 7183 |
local format_I=function(f) |
| 7184 |
n=n+1 |
| 7185 |
return format("format('%%s%%%si',signed(a%s))",f,n) |
| 7186 |
end |
| 7187 |
local format_f=function(f) |
| 7188 |
n=n+1 |
| 7189 |
return format("format('%%%sf',a%s)",f,n) |
| 7190 |
end |
| 7191 |
local format_F=function(f) |
| 7192 |
n=n+1 |
| 7193 |
if not f or f=="" then |
| 7194 |
return format("(((a%s > -0.0000000005 and a%s < 0.0000000005) and '0') or format((a%s %% 1 == 0) and '%%i' or '%%.9f',a%s))",n,n,n,n) |
| 7195 |
else |
| 7196 |
return format("format((a%s %% 1 == 0) and '%%i' or '%%%sf',a%s)",n,f,n) |
| 7197 |
end |
| 7198 |
end |
| 7199 |
local format_k=function(b,a) |
| 7200 |
n=n+1 |
| 7201 |
return format("formattedfloat(a%s,%s,%s)",n,b or 0,a or 0) |
| 7202 |
end |
| 7203 |
local format_g=function(f) |
| 7204 |
n=n+1 |
| 7205 |
return format("format('%%%sg',a%s)",f,n) |
| 7206 |
end |
| 7207 |
local format_G=function(f) |
| 7208 |
n=n+1 |
| 7209 |
return format("format('%%%sG',a%s)",f,n) |
| 7210 |
end |
| 7211 |
local format_e=function(f) |
| 7212 |
n=n+1 |
| 7213 |
return format("format('%%%se',a%s)",f,n) |
| 7214 |
end |
| 7215 |
local format_E=function(f) |
| 7216 |
n=n+1 |
| 7217 |
return format("format('%%%sE',a%s)",f,n) |
| 7218 |
end |
| 7219 |
local format_j=function(f) |
| 7220 |
n=n+1 |
| 7221 |
return format("sparseexponent('%%%se',a%s)",f,n) |
| 7222 |
end |
| 7223 |
local format_J=function(f) |
| 7224 |
n=n+1 |
| 7225 |
return format("sparseexponent('%%%sE',a%s)",f,n) |
| 7226 |
end |
| 7227 |
local format_x=function(f) |
| 7228 |
n=n+1 |
| 7229 |
return format("format('%%%sx',a%s)",f,n) |
| 7230 |
end |
| 7231 |
local format_X=function(f) |
| 7232 |
n=n+1 |
| 7233 |
return format("format('%%%sX',a%s)",f,n) |
| 7234 |
end |
| 7235 |
local format_o=function(f) |
| 7236 |
n=n+1 |
| 7237 |
return format("format('%%%so',a%s)",f,n) |
| 7238 |
end |
| 7239 |
local format_c=function() |
| 7240 |
n=n+1 |
| 7241 |
return format("utfchar(a%s)",n) |
| 7242 |
end |
| 7243 |
local format_C=function() |
| 7244 |
n=n+1 |
| 7245 |
return format("tracedchar(a%s)",n) |
| 7246 |
end |
| 7247 |
local format_r=function(f) |
| 7248 |
n=n+1 |
| 7249 |
return format("format('%%%s.0f',a%s)",f,n) |
| 7250 |
end |
| 7251 |
local format_h=function(f) |
| 7252 |
n=n+1 |
| 7253 |
if f=="-" then |
| 7254 |
f=sub(f,2) |
| 7255 |
return format("format('%%%sx',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 7256 |
else |
| 7257 |
return format("format('0x%%%sx',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 7258 |
end |
| 7259 |
end |
| 7260 |
local format_H=function(f) |
| 7261 |
n=n+1 |
| 7262 |
if f=="-" then |
| 7263 |
f=sub(f,2) |
| 7264 |
return format("format('%%%sX',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 7265 |
else |
| 7266 |
return format("format('0x%%%sX',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 7267 |
end |
| 7268 |
end |
| 7269 |
local format_u=function(f) |
| 7270 |
n=n+1 |
| 7271 |
if f=="-" then |
| 7272 |
f=sub(f,2) |
| 7273 |
return format("format('%%%sx',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 7274 |
else |
| 7275 |
return format("format('u+%%%sx',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 7276 |
end |
| 7277 |
end |
| 7278 |
local format_U=function(f) |
| 7279 |
n=n+1 |
| 7280 |
if f=="-" then |
| 7281 |
f=sub(f,2) |
| 7282 |
return format("format('%%%sX',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 7283 |
else |
| 7284 |
return format("format('U+%%%sX',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 7285 |
end |
| 7286 |
end |
| 7287 |
local format_p=function() |
| 7288 |
n=n+1 |
| 7289 |
return format("points(a%s)",n) |
| 7290 |
end |
| 7291 |
local format_P=function() |
| 7292 |
n=n+1 |
| 7293 |
return format("nupoints(a%s)",n) |
| 7294 |
end |
| 7295 |
local format_b=function() |
| 7296 |
n=n+1 |
| 7297 |
return format("basepoints(a%s)",n) |
| 7298 |
end |
| 7299 |
local format_B=function() |
| 7300 |
n=n+1 |
| 7301 |
return format("nubasepoints(a%s)",n) |
| 7302 |
end |
| 7303 |
local format_t=function(f) |
| 7304 |
n=n+1 |
| 7305 |
if f and f~="" then |
| 7306 |
return format("concat(a%s,%q)",n,f) |
| 7307 |
else |
| 7308 |
return format("concat(a%s)",n) |
| 7309 |
end |
| 7310 |
end |
| 7311 |
local format_T=function(f) |
| 7312 |
n=n+1 |
| 7313 |
if f and f~="" then |
| 7314 |
return format("sequenced(a%s,%q)",n,f) |
| 7315 |
else |
| 7316 |
return format("sequenced(a%s)",n) |
| 7317 |
end |
| 7318 |
end |
| 7319 |
local format_l=function() |
| 7320 |
n=n+1 |
| 7321 |
return format("(a%s and 'true' or 'false')",n) |
| 7322 |
end |
| 7323 |
local format_L=function() |
| 7324 |
n=n+1 |
| 7325 |
return format("(a%s and 'TRUE' or 'FALSE')",n) |
| 7326 |
end |
| 7327 |
local format_n=function() |
| 7328 |
n=n+1 |
| 7329 |
return format("((a%s %% 1 == 0) and format('%%i',a%s) or tostring(a%s))",n,n,n) |
| 7330 |
end |
| 7331 |
local format_N if environment.FORMAT then |
| 7332 |
format_N=function(f) |
| 7333 |
n=n+1 |
| 7334 |
if not f or f=="" then |
| 7335 |
return format("FORMAT(a%s,'%%.9f')",n) |
| 7336 |
elseif f==".6" or f=="0.6" then |
| 7337 |
return format("FORMAT(a%s)",n) |
| 7338 |
else |
| 7339 |
return format("FORMAT(a%s,'%%%sf')",n,f) |
| 7340 |
end |
| 7341 |
end |
| 7342 |
else |
| 7343 |
format_N=function(f) |
| 7344 |
n=n+1 |
| 7345 |
if not f or f=="" then |
| 7346 |
f=".9" |
| 7347 |
end |
| 7348 |
return format("(((a%s %% 1 == 0) and format('%%i',a%s)) or lpegmatch(stripzero,format('%%%sf',a%s)))",n,n,f,n) |
| 7349 |
end |
| 7350 |
end |
| 7351 |
local format_a=function(f) |
| 7352 |
n=n+1 |
| 7353 |
if f and f~="" then |
| 7354 |
return format("autosingle(a%s,%q)",n,f) |
| 7355 |
else |
| 7356 |
return format("autosingle(a%s)",n) |
| 7357 |
end |
| 7358 |
end |
| 7359 |
local format_A=function(f) |
| 7360 |
n=n+1 |
| 7361 |
if f and f~="" then |
| 7362 |
return format("autodouble(a%s,%q)",n,f) |
| 7363 |
else |
| 7364 |
return format("autodouble(a%s)",n) |
| 7365 |
end |
| 7366 |
end |
| 7367 |
local format_w=function(f) |
| 7368 |
n=n+1 |
| 7369 |
f=tonumber(f) |
| 7370 |
if f then |
| 7371 |
return format("nspaces[%s+a%s]",f,n) |
| 7372 |
else |
| 7373 |
return format("nspaces[a%s]",n) |
| 7374 |
end |
| 7375 |
end |
| 7376 |
local format_W=function(f) |
| 7377 |
return format("nspaces[%s]",tonumber(f) or 0) |
| 7378 |
end |
| 7379 |
local format_m=function(f) |
| 7380 |
n=n+1 |
| 7381 |
if not f or f=="" then |
| 7382 |
f="," |
| 7383 |
end |
| 7384 |
if f=="0" then |
| 7385 |
return format([[formattednumber(a%s,false)]],n) |
| 7386 |
else |
| 7387 |
return format([[formattednumber(a%s,%q,".")]],n,f) |
| 7388 |
end |
| 7389 |
end |
| 7390 |
local format_M=function(f) |
| 7391 |
n=n+1 |
| 7392 |
if not f or f=="" then |
| 7393 |
f="." |
| 7394 |
end |
| 7395 |
if f=="0" then |
| 7396 |
return format([[formattednumber(a%s,false)]],n) |
| 7397 |
else |
| 7398 |
return format([[formattednumber(a%s,%q,",")]],n,f) |
| 7399 |
end |
| 7400 |
end |
| 7401 |
local format_z=function(f) |
| 7402 |
n=n+(tonumber(f) or 1) |
| 7403 |
return "''" |
| 7404 |
end |
| 7405 |
local format_Z=function(f) |
| 7406 |
return format("a%s",tonumber(f) or 1) |
| 7407 |
end |
| 7408 |
local format_rest=function(s) |
| 7409 |
return format("%q",s) |
| 7410 |
end |
| 7411 |
local format_extension=function(extensions,f,name) |
| 7412 |
local extension=extensions[name] or "tostring(%s)" |
| 7413 |
local f=tonumber(f) or 1 |
| 7414 |
local w=find(extension,"%.%.%.") |
| 7415 |
if f==0 then |
| 7416 |
if w then |
| 7417 |
extension=gsub(extension,"%.%.%.","") |
| 7418 |
end |
| 7419 |
return extension |
| 7420 |
elseif f==1 then |
| 7421 |
if w then |
| 7422 |
extension=gsub(extension,"%.%.%.","%%s") |
| 7423 |
end |
| 7424 |
n=n+1 |
| 7425 |
local a="a"..n |
| 7426 |
return format(extension,a,a) |
| 7427 |
elseif f<0 then |
| 7428 |
if w then |
| 7429 |
extension=gsub(extension,"%.%.%.","") |
| 7430 |
return extension |
| 7431 |
else |
| 7432 |
local a="a"..(n+f+1) |
| 7433 |
return format(extension,a,a) |
| 7434 |
end |
| 7435 |
else |
| 7436 |
if w then |
| 7437 |
extension=gsub(extension,"%.%.%.",rep("%%s,",f-1).."%%s") |
| 7438 |
end |
| 7439 |
local t={} |
| 7440 |
for i=1,f do |
| 7441 |
n=n+1 |
| 7442 |
t[i]="a"..n |
| 7443 |
end |
| 7444 |
return format(extension,unpack(t)) |
| 7445 |
end |
| 7446 |
end |
| 7447 |
local builder=Cs { "start", |
| 7448 |
start=( |
| 7449 |
( |
| 7450 |
P("%")/""*( |
| 7451 |
V("!") |
| 7452 |
+V("s")+V("q")+V("i")+V("d")+V("f")+V("F")+V("g")+V("G")+V("e")+V("E")+V("x")+V("X")+V("o") |
| 7453 |
+V("c")+V("C")+V("S") |
| 7454 |
+V("Q") |
| 7455 |
+V("n") |
| 7456 |
+V("N") |
| 7457 |
+V("k") |
| 7458 |
+V("r")+V("h")+V("H")+V("u")+V("U")+V("p")+V("P")+V("b")+V("B")+V("t")+V("T")+V("l")+V("L")+V("I")+V("w") |
| 7459 |
+V("W") |
| 7460 |
+V("a") |
| 7461 |
+V("A") |
| 7462 |
+V("j")+V("J") |
| 7463 |
+V("m")+V("M") |
| 7464 |
+V("z")+V("Z") |
| 7465 |
+V(">") |
| 7466 |
+V("<") |
| 7467 |
)+V("*") |
| 7468 |
)*(endofstring+Carg(1)) |
| 7469 |
)^0, |
| 7470 |
["s"]=(prefix_any*P("s"))/format_s, |
| 7471 |
["q"]=(prefix_any*P("q"))/format_q, |
| 7472 |
["i"]=(prefix_any*P("i"))/format_i, |
| 7473 |
["d"]=(prefix_any*P("d"))/format_d, |
| 7474 |
["f"]=(prefix_any*P("f"))/format_f, |
| 7475 |
["F"]=(prefix_any*P("F"))/format_F, |
| 7476 |
["g"]=(prefix_any*P("g"))/format_g, |
| 7477 |
["G"]=(prefix_any*P("G"))/format_G, |
| 7478 |
["e"]=(prefix_any*P("e"))/format_e, |
| 7479 |
["E"]=(prefix_any*P("E"))/format_E, |
| 7480 |
["x"]=(prefix_any*P("x"))/format_x, |
| 7481 |
["X"]=(prefix_any*P("X"))/format_X, |
| 7482 |
["o"]=(prefix_any*P("o"))/format_o, |
| 7483 |
["S"]=(prefix_any*P("S"))/format_S, |
| 7484 |
["Q"]=(prefix_any*P("Q"))/format_Q, |
| 7485 |
["n"]=(prefix_any*P("n"))/format_n, |
| 7486 |
["N"]=(prefix_any*P("N"))/format_N, |
| 7487 |
["k"]=(prefix_sub*P("k"))/format_k, |
| 7488 |
["c"]=(prefix_any*P("c"))/format_c, |
| 7489 |
["C"]=(prefix_any*P("C"))/format_C, |
| 7490 |
["r"]=(prefix_any*P("r"))/format_r, |
| 7491 |
["h"]=(prefix_any*P("h"))/format_h, |
| 7492 |
["H"]=(prefix_any*P("H"))/format_H, |
| 7493 |
["u"]=(prefix_any*P("u"))/format_u, |
| 7494 |
["U"]=(prefix_any*P("U"))/format_U, |
| 7495 |
["p"]=(prefix_any*P("p"))/format_p, |
| 7496 |
["P"]=(prefix_any*P("P"))/format_P, |
| 7497 |
["b"]=(prefix_any*P("b"))/format_b, |
| 7498 |
["B"]=(prefix_any*P("B"))/format_B, |
| 7499 |
["t"]=(prefix_tab*P("t"))/format_t, |
| 7500 |
["T"]=(prefix_tab*P("T"))/format_T, |
| 7501 |
["l"]=(prefix_any*P("l"))/format_l, |
| 7502 |
["L"]=(prefix_any*P("L"))/format_L, |
| 7503 |
["I"]=(prefix_any*P("I"))/format_I, |
| 7504 |
["w"]=(prefix_any*P("w"))/format_w, |
| 7505 |
["W"]=(prefix_any*P("W"))/format_W, |
| 7506 |
["j"]=(prefix_any*P("j"))/format_j, |
| 7507 |
["J"]=(prefix_any*P("J"))/format_J, |
| 7508 |
["m"]=(prefix_any*P("m"))/format_m, |
| 7509 |
["M"]=(prefix_any*P("M"))/format_M, |
| 7510 |
["z"]=(prefix_any*P("z"))/format_z, |
| 7511 |
["Z"]=(prefix_any*P("Z"))/format_Z, |
| 7512 |
["a"]=(prefix_any*P("a"))/format_a, |
| 7513 |
["A"]=(prefix_any*P("A"))/format_A, |
| 7514 |
["<"]=(prefix_any*P("<"))/format_left, |
| 7515 |
[">"]=(prefix_any*P(">"))/format_right, |
| 7516 |
["*"]=Cs(((1-P("%"))^1+P("%%")/"%%")^1)/format_rest, |
| 7517 |
["?"]=Cs(((1-P("%"))^1 )^1)/format_rest, |
| 7518 |
["!"]=Carg(2)*prefix_any*P("!")*C((1-P("!"))^1)*P("!")/format_extension, |
| 7519 |
} |
| 7520 |
local xx=setmetatable({},{ __index=function(t,k) local v=format("%02x",k) t[k]=v return v end }) |
| 7521 |
local XX=setmetatable({},{ __index=function(t,k) local v=format("%02X",k) t[k]=v return v end }) |
| 7522 |
local preset={ |
| 7523 |
["%02x"]=function(n) return xx[n] end, |
| 7524 |
["%02X"]=function(n) return XX[n] end, |
| 7525 |
} |
| 7526 |
local direct=P("%")*(sign+space+period+digit)^0*S("sqidfgGeExXo")*endofstring/[[local format = string.format return function(str) return format("%0",str) end]] |
| 7527 |
local function make(t,str) |
| 7528 |
local f=preset[str] |
| 7529 |
if f then |
| 7530 |
return f |
| 7531 |
end |
| 7532 |
local p=lpegmatch(direct,str) |
| 7533 |
if p then |
| 7534 |
f=loadstripped(p)() |
| 7535 |
else |
| 7536 |
n=0 |
| 7537 |
p=lpegmatch(builder,str,1,t._connector_,t._extensions_) |
| 7538 |
if n>0 then |
| 7539 |
p=format(template,preamble,t._preamble_,arguments[n],p) |
| 7540 |
f=loadstripped(p,t._environment_)() |
| 7541 |
else |
| 7542 |
f=function() return str end |
| 7543 |
end |
| 7544 |
end |
| 7545 |
t[str]=f |
| 7546 |
return f |
| 7547 |
end |
| 7548 |
local function use(t,fmt,...) |
| 7549 |
return t[fmt](...) |
| 7550 |
end |
| 7551 |
strings.formatters={} |
| 7552 |
function strings.formatters.new(noconcat) |
| 7553 |
local e={} |
| 7554 |
for k,v in next,environment do |
| 7555 |
e[k]=v |
| 7556 |
end |
| 7557 |
local t={ |
| 7558 |
_type_="formatter", |
| 7559 |
_connector_=noconcat and "," or "..", |
| 7560 |
_extensions_={}, |
| 7561 |
_preamble_="", |
| 7562 |
_environment_=e, |
| 7563 |
} |
| 7564 |
setmetatable(t,{ __index=make,__call=use }) |
| 7565 |
return t |
| 7566 |
end |
| 7567 |
local formatters=strings.formatters.new() |
| 7568 |
string.formatters=formatters |
| 7569 |
string.formatter=function(str,...) return formatters[str](...) end |
| 7570 |
local function add(t,name,template,preamble) |
| 7571 |
if type(t)=="table" and t._type_=="formatter" then |
| 7572 |
t._extensions_[name]=template or "%s" |
| 7573 |
if type(preamble)=="string" then |
| 7574 |
t._preamble_=preamble.."\n"..t._preamble_ |
| 7575 |
elseif type(preamble)=="table" then |
| 7576 |
for k,v in next,preamble do |
| 7577 |
t._environment_[k]=v |
| 7578 |
end |
| 7579 |
end |
| 7580 |
end |
| 7581 |
end |
| 7582 |
strings.formatters.add=add |
| 7583 |
patterns.xmlescape=Cs((P("<")/"<"+P(">")/">"+P("&")/"&"+P('"')/"""+anything)^0) |
| 7584 |
patterns.texescape=Cs((C(S("#$%\\{}"))/"\\%1"+anything)^0) |
| 7585 |
patterns.ctxescape=Cs((C(S("#$%\\{}|"))/"\\%1"+anything)^0) |
| 7586 |
patterns.luaescape=Cs(((1-S('"\n'))^1+P('"')/'\\"'+P('\n')/'\\n"')^0) |
| 7587 |
patterns.luaquoted=Cs(Cc('"')*((1-S('"\n'))^1+P('"')/'\\"'+P('\n')/'\\n"')^0*Cc('"')) |
| 7588 |
add(formatters,"xml",[[lpegmatch(xmlescape,%s)]],{ xmlescape=patterns.xmlescape }) |
| 7589 |
add(formatters,"tex",[[lpegmatch(texescape,%s)]],{ texescape=patterns.texescape }) |
| 7590 |
add(formatters,"lua",[[lpegmatch(luaescape,%s)]],{ luaescape=patterns.luaescape }) |
| 7591 |
local dquote=patterns.dquote |
| 7592 |
local equote=patterns.escaped+dquote/'\\"'+1 |
| 7593 |
local cquote=Cc('"') |
| 7594 |
local pattern=Cs(dquote*(equote-P(-2))^0*dquote) |
| 7595 |
+Cs(cquote*(equote-space)^0*space*equote^0*cquote) |
| 7596 |
function string.optionalquoted(str) |
| 7597 |
return lpegmatch(pattern,str) or str |
| 7598 |
end |
| 7599 |
local pattern=Cs((newline/(os.newline or "\r")+1)^0) |
| 7600 |
function string.replacenewlines(str) |
| 7601 |
return lpegmatch(pattern,str) |
| 7602 |
end |
| 7603 |
function strings.newcollector() |
| 7604 |
local result,r={},0 |
| 7605 |
return |
| 7606 |
function(fmt,str,...) |
| 7607 |
r=r+1 |
| 7608 |
result[r]=str==nil and fmt or formatters[fmt](str,...) |
| 7609 |
end, |
| 7610 |
function(connector) |
| 7611 |
if result then |
| 7612 |
local str=concat(result,connector) |
| 7613 |
result,r={},0 |
| 7614 |
return str |
| 7615 |
end |
| 7616 |
end |
| 7617 |
end |
| 7618 |
local f_16_16=formatters["%0.5N"] |
| 7619 |
function number.to16dot16(n) |
| 7620 |
return f_16_16(n/65536.0) |
| 7621 |
end |
| 7622 |
if not string.explode then |
| 7623 |
local p_utf=patterns.utf8character |
| 7624 |
local p_check=C(p_utf)*(P("+")*Cc(true))^0 |
| 7625 |
local p_split=Ct(C(p_utf)^0) |
| 7626 |
local p_space=Ct((C(1-P(" ")^1)+P(" ")^1)^0) |
| 7627 |
function string.explode(str,symbol) |
| 7628 |
if symbol=="" then |
| 7629 |
return lpegmatch(p_split,str) |
| 7630 |
elseif symbol then |
| 7631 |
local a,b=lpegmatch(p_check,symbol) |
| 7632 |
if b then |
| 7633 |
return lpegmatch(tsplitat(P(a)^1),str) |
| 7634 |
else |
| 7635 |
return lpegmatch(tsplitat(a),str) |
| 7636 |
end |
| 7637 |
else |
| 7638 |
return lpegmatch(p_space,str) |
| 7639 |
end |
| 7640 |
end |
| 7641 |
end |
| 7642 |
do |
| 7643 |
local p_whitespace=patterns.whitespace^1 |
| 7644 |
local cache=setmetatable({},{ __index=function(t,k) |
| 7645 |
local p=tsplitat(p_whitespace*P(k)*p_whitespace) |
| 7646 |
local v=function(s) |
| 7647 |
return lpegmatch(p,s) |
| 7648 |
end |
| 7649 |
t[k]=v |
| 7650 |
return v |
| 7651 |
end }) |
| 7652 |
function string.wordsplitter(s) |
| 7653 |
return cache[s] |
| 7654 |
end |
| 7655 |
end |
| 7656 |
if CONTEXTLMTXMODE and CONTEXTLMTXMODE>0 then |
| 7657 |
local t={ |
| 7658 |
["#"]="#H", |
| 7659 |
["\n"]="#L", |
| 7660 |
['"']="#Q", |
| 7661 |
["\r"]="#R", |
| 7662 |
[" "]="#S", |
| 7663 |
["\t"]="#T", |
| 7664 |
["\\"]="#X", |
| 7665 |
} |
| 7666 |
function string.texhashed(s) |
| 7667 |
return (gsub(s,".",t)) |
| 7668 |
end |
| 7669 |
end |
| 7670 |
|
| 7671 |
|
| 7672 |
end -- of closure |
| 7673 |
|
| 7674 |
do -- create closure to overcome 200 locals limit |
| 7675 |
|
| 7676 |
package.loaded["util-tab"] = package.loaded["util-tab"] or true |
| 7677 |
|
| 7678 |
-- original size: 35024, stripped down to: 18453 |
| 7679 |
|
| 7680 |
if not modules then modules={} end modules ['util-tab']={ |
| 7681 |
version=1.001, |
| 7682 |
comment="companion to luat-lib.mkiv", |
| 7683 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 7684 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 7685 |
license="see context related readme files" |
| 7686 |
} |
| 7687 |
utilities=utilities or {} |
| 7688 |
utilities.tables=utilities.tables or {} |
| 7689 |
local tables=utilities.tables |
| 7690 |
local format,gmatch,gsub,sub=string.format,string.gmatch,string.gsub,string.sub |
| 7691 |
local concat,insert,remove,sort=table.concat,table.insert,table.remove,table.sort |
| 7692 |
local setmetatable,getmetatable,tonumber,tostring,rawget=setmetatable,getmetatable,tonumber,tostring,rawget |
| 7693 |
local type,next,rawset,rawget,tonumber,tostring,load,select=type,next,rawset,rawget,tonumber,tostring,load,select |
| 7694 |
local lpegmatch,P,Cs,Cc=lpeg.match,lpeg.P,lpeg.Cs,lpeg.Cc |
| 7695 |
local sortedkeys,sortedpairs=table.sortedkeys,table.sortedpairs |
| 7696 |
local formatters=string.formatters |
| 7697 |
local utftoeight=utf.toeight |
| 7698 |
local splitter=lpeg.tsplitat(".") |
| 7699 |
function utilities.tables.definetable(target,nofirst,nolast) |
| 7700 |
local composed=nil |
| 7701 |
local t={} |
| 7702 |
local snippets=lpegmatch(splitter,target) |
| 7703 |
for i=1,#snippets-(nolast and 1 or 0) do |
| 7704 |
local name=snippets[i] |
| 7705 |
if composed then |
| 7706 |
composed=composed.."."..name |
| 7707 |
t[#t+1]=formatters["if not %s then %s = { } end"](composed,composed) |
| 7708 |
else |
| 7709 |
composed=name |
| 7710 |
if not nofirst then |
| 7711 |
t[#t+1]=formatters["%s = %s or { }"](composed,composed) |
| 7712 |
end |
| 7713 |
end |
| 7714 |
end |
| 7715 |
if composed then |
| 7716 |
if nolast then |
| 7717 |
composed=composed.."."..snippets[#snippets] |
| 7718 |
end |
| 7719 |
return concat(t,"\n"),composed |
| 7720 |
else |
| 7721 |
return "",target |
| 7722 |
end |
| 7723 |
end |
| 7724 |
function tables.definedtable(...) |
| 7725 |
local t=_G |
| 7726 |
for i=1,select("#",...) do |
| 7727 |
local li=select(i,...) |
| 7728 |
local tl=t[li] |
| 7729 |
if not tl then |
| 7730 |
tl={} |
| 7731 |
t[li]=tl |
| 7732 |
end |
| 7733 |
t=tl |
| 7734 |
end |
| 7735 |
return t |
| 7736 |
end |
| 7737 |
function tables.accesstable(target,root) |
| 7738 |
local t=root or _G |
| 7739 |
for name in gmatch(target,"([^%.]+)") do |
| 7740 |
t=t[name] |
| 7741 |
if not t then |
| 7742 |
return |
| 7743 |
end |
| 7744 |
end |
| 7745 |
return t |
| 7746 |
end |
| 7747 |
function tables.migratetable(target,v,root) |
| 7748 |
local t=root or _G |
| 7749 |
local names=lpegmatch(splitter,target) |
| 7750 |
for i=1,#names-1 do |
| 7751 |
local name=names[i] |
| 7752 |
t[name]=t[name] or {} |
| 7753 |
t=t[name] |
| 7754 |
if not t then |
| 7755 |
return |
| 7756 |
end |
| 7757 |
end |
| 7758 |
t[names[#names]]=v |
| 7759 |
end |
| 7760 |
function tables.removevalue(t,value) |
| 7761 |
if value then |
| 7762 |
for i=1,#t do |
| 7763 |
if t[i]==value then |
| 7764 |
remove(t,i) |
| 7765 |
end |
| 7766 |
end |
| 7767 |
end |
| 7768 |
end |
| 7769 |
function tables.replacevalue(t,oldvalue,newvalue) |
| 7770 |
if oldvalue and newvalue then |
| 7771 |
for i=1,#t do |
| 7772 |
if t[i]==oldvalue then |
| 7773 |
t[i]=newvalue |
| 7774 |
end |
| 7775 |
end |
| 7776 |
end |
| 7777 |
end |
| 7778 |
function tables.insertbeforevalue(t,value,extra) |
| 7779 |
for i=1,#t do |
| 7780 |
if t[i]==extra then |
| 7781 |
remove(t,i) |
| 7782 |
end |
| 7783 |
end |
| 7784 |
for i=1,#t do |
| 7785 |
if t[i]==value then |
| 7786 |
insert(t,i,extra) |
| 7787 |
return |
| 7788 |
end |
| 7789 |
end |
| 7790 |
insert(t,1,extra) |
| 7791 |
end |
| 7792 |
function tables.insertaftervalue(t,value,extra) |
| 7793 |
for i=1,#t do |
| 7794 |
if t[i]==extra then |
| 7795 |
remove(t,i) |
| 7796 |
end |
| 7797 |
end |
| 7798 |
for i=1,#t do |
| 7799 |
if t[i]==value then |
| 7800 |
insert(t,i+1,extra) |
| 7801 |
return |
| 7802 |
end |
| 7803 |
end |
| 7804 |
insert(t,#t+1,extra) |
| 7805 |
end |
| 7806 |
local escape=Cs(Cc('"')*((P('"')/'""'+P(1))^0)*Cc('"')) |
| 7807 |
function table.tocsv(t,specification) |
| 7808 |
if t and #t>0 then |
| 7809 |
local result={} |
| 7810 |
local r={} |
| 7811 |
specification=specification or {} |
| 7812 |
local fields=specification.fields |
| 7813 |
if type(fields)~="string" then |
| 7814 |
fields=sortedkeys(t[1]) |
| 7815 |
end |
| 7816 |
local separator=specification.separator or "," |
| 7817 |
local noffields=#fields |
| 7818 |
if specification.preamble==true then |
| 7819 |
for f=1,noffields do |
| 7820 |
r[f]=lpegmatch(escape,tostring(fields[f])) |
| 7821 |
end |
| 7822 |
result[1]=concat(r,separator) |
| 7823 |
end |
| 7824 |
for i=1,#t do |
| 7825 |
local ti=t[i] |
| 7826 |
for f=1,noffields do |
| 7827 |
local field=ti[fields[f]] |
| 7828 |
if type(field)=="string" then |
| 7829 |
r[f]=lpegmatch(escape,field) |
| 7830 |
else |
| 7831 |
r[f]=tostring(field) |
| 7832 |
end |
| 7833 |
end |
| 7834 |
result[i+1]=concat(r,separator) |
| 7835 |
end |
| 7836 |
return concat(result,"\n") |
| 7837 |
else |
| 7838 |
return "" |
| 7839 |
end |
| 7840 |
end |
| 7841 |
local nspaces=utilities.strings.newrepeater(" ") |
| 7842 |
local function toxml(t,d,result,step) |
| 7843 |
local r=#result |
| 7844 |
for k,v in sortedpairs(t) do |
| 7845 |
local s=nspaces[d] |
| 7846 |
local tk=type(k) |
| 7847 |
local tv=type(v) |
| 7848 |
if tv=="table" then |
| 7849 |
if tk=="number" then |
| 7850 |
r=r+1 result[r]=formatters["%s<entry n='%s'>"](s,k) |
| 7851 |
r=toxml(v,d+step,result,step) |
| 7852 |
r=r+1 result[r]=formatters["%s</entry>"](s,k) |
| 7853 |
else |
| 7854 |
r=r+1 result[r]=formatters["%s<%s>"](s,k) |
| 7855 |
r=toxml(v,d+step,result,step) |
| 7856 |
r=r+1 result[r]=formatters["%s</%s>"](s,k) |
| 7857 |
end |
| 7858 |
elseif tv=="string" then |
| 7859 |
if tk=="number" then |
| 7860 |
r=r+1 result[r]=formatters["%s<entry n='%s'>%!xml!</entry>"](s,k,v,k) |
| 7861 |
else |
| 7862 |
r=r+1 result[r]=formatters["%s<%s>%!xml!</%s>"](s,k,v,k) |
| 7863 |
end |
| 7864 |
elseif tk=="number" then |
| 7865 |
r=r+1 result[r]=formatters["%s<entry n='%s'>%S</entry>"](s,k,v,k) |
| 7866 |
else |
| 7867 |
r=r+1 result[r]=formatters["%s<%s>%S</%s>"](s,k,v,k) |
| 7868 |
end |
| 7869 |
end |
| 7870 |
return r |
| 7871 |
end |
| 7872 |
function table.toxml(t,specification) |
| 7873 |
specification=specification or {} |
| 7874 |
local name=specification.name |
| 7875 |
local noroot=name==false |
| 7876 |
local result=(specification.nobanner or noroot) and {} or { "<?xml version='1.0' standalone='yes' ?>" } |
| 7877 |
local indent=specification.indent or 0 |
| 7878 |
local spaces=specification.spaces or 1 |
| 7879 |
if noroot then |
| 7880 |
toxml(t,indent,result,spaces) |
| 7881 |
else |
| 7882 |
toxml({ [name or "data"]=t },indent,result,spaces) |
| 7883 |
end |
| 7884 |
return concat(result,"\n") |
| 7885 |
end |
| 7886 |
function tables.encapsulate(core,capsule,protect) |
| 7887 |
if type(capsule)~="table" then |
| 7888 |
protect=true |
| 7889 |
capsule={} |
| 7890 |
end |
| 7891 |
for key,value in next,core do |
| 7892 |
if capsule[key] then |
| 7893 |
print(formatters["\ninvalid %s %a in %a"]("inheritance",key,core)) |
| 7894 |
os.exit() |
| 7895 |
else |
| 7896 |
capsule[key]=value |
| 7897 |
end |
| 7898 |
end |
| 7899 |
if protect then |
| 7900 |
for key,value in next,core do |
| 7901 |
core[key]=nil |
| 7902 |
end |
| 7903 |
setmetatable(core,{ |
| 7904 |
__index=capsule, |
| 7905 |
__newindex=function(t,key,value) |
| 7906 |
if capsule[key] then |
| 7907 |
print(formatters["\ninvalid %s %a' in %a"]("overload",key,core)) |
| 7908 |
os.exit() |
| 7909 |
else |
| 7910 |
rawset(t,key,value) |
| 7911 |
end |
| 7912 |
end |
| 7913 |
} ) |
| 7914 |
end |
| 7915 |
end |
| 7916 |
if JITSUPPORTED then |
| 7917 |
local f_hashed_string=formatters["[%Q]=%Q,"] |
| 7918 |
local f_hashed_number=formatters["[%Q]=%s,"] |
| 7919 |
local f_hashed_boolean=formatters["[%Q]=%l,"] |
| 7920 |
local f_hashed_table=formatters["[%Q]="] |
| 7921 |
local f_indexed_string=formatters["[%s]=%Q,"] |
| 7922 |
local f_indexed_number=formatters["[%s]=%s,"] |
| 7923 |
local f_indexed_boolean=formatters["[%s]=%l,"] |
| 7924 |
local f_indexed_table=formatters["[%s]="] |
| 7925 |
local f_ordered_string=formatters["%Q,"] |
| 7926 |
local f_ordered_number=formatters["%s,"] |
| 7927 |
local f_ordered_boolean=formatters["%l,"] |
| 7928 |
function table.fastserialize(t,prefix) |
| 7929 |
local r={ type(prefix)=="string" and prefix or "return" } |
| 7930 |
local m=1 |
| 7931 |
local function fastserialize(t,outer) |
| 7932 |
local n=#t |
| 7933 |
m=m+1 |
| 7934 |
r[m]="{" |
| 7935 |
if n>0 then |
| 7936 |
local v=t[0] |
| 7937 |
if v then |
| 7938 |
local tv=type(v) |
| 7939 |
if tv=="string" then |
| 7940 |
m=m+1 r[m]=f_indexed_string(0,v) |
| 7941 |
elseif tv=="number" then |
| 7942 |
m=m+1 r[m]=f_indexed_number(0,v) |
| 7943 |
elseif tv=="table" then |
| 7944 |
m=m+1 r[m]=f_indexed_table(0) |
| 7945 |
fastserialize(v) |
| 7946 |
m=m+1 r[m]=f_indexed_table(0) |
| 7947 |
elseif tv=="boolean" then |
| 7948 |
m=m+1 r[m]=f_indexed_boolean(0,v) |
| 7949 |
end |
| 7950 |
end |
| 7951 |
for i=1,n do |
| 7952 |
local v=t[i] |
| 7953 |
local tv=type(v) |
| 7954 |
if tv=="string" then |
| 7955 |
m=m+1 r[m]=f_ordered_string(v) |
| 7956 |
elseif tv=="number" then |
| 7957 |
m=m+1 r[m]=f_ordered_number(v) |
| 7958 |
elseif tv=="table" then |
| 7959 |
fastserialize(v) |
| 7960 |
elseif tv=="boolean" then |
| 7961 |
m=m+1 r[m]=f_ordered_boolean(v) |
| 7962 |
end |
| 7963 |
end |
| 7964 |
end |
| 7965 |
for k,v in next,t do |
| 7966 |
local tk=type(k) |
| 7967 |
if tk=="number" then |
| 7968 |
if k>n or k<0 then |
| 7969 |
local tv=type(v) |
| 7970 |
if tv=="string" then |
| 7971 |
m=m+1 r[m]=f_indexed_string(k,v) |
| 7972 |
elseif tv=="number" then |
| 7973 |
m=m+1 r[m]=f_indexed_number(k,v) |
| 7974 |
elseif tv=="table" then |
| 7975 |
m=m+1 r[m]=f_indexed_table(k) |
| 7976 |
fastserialize(v) |
| 7977 |
elseif tv=="boolean" then |
| 7978 |
m=m+1 r[m]=f_indexed_boolean(k,v) |
| 7979 |
end |
| 7980 |
end |
| 7981 |
else |
| 7982 |
local tv=type(v) |
| 7983 |
if tv=="string" then |
| 7984 |
m=m+1 r[m]=f_hashed_string(k,v) |
| 7985 |
elseif tv=="number" then |
| 7986 |
m=m+1 r[m]=f_hashed_number(k,v) |
| 7987 |
elseif tv=="table" then |
| 7988 |
m=m+1 r[m]=f_hashed_table(k) |
| 7989 |
fastserialize(v) |
| 7990 |
elseif tv=="boolean" then |
| 7991 |
m=m+1 r[m]=f_hashed_boolean(k,v) |
| 7992 |
end |
| 7993 |
end |
| 7994 |
end |
| 7995 |
m=m+1 |
| 7996 |
if outer then |
| 7997 |
r[m]="}" |
| 7998 |
else |
| 7999 |
r[m]="}," |
| 8000 |
end |
| 8001 |
return r |
| 8002 |
end |
| 8003 |
return concat(fastserialize(t,true)) |
| 8004 |
end |
| 8005 |
else |
| 8006 |
function table.fastserialize(t,prefix) |
| 8007 |
local r={ type(prefix)=="string" and prefix or "return" } |
| 8008 |
local m=1 |
| 8009 |
local function fastserialize(t,outer) |
| 8010 |
local n=#t |
| 8011 |
m=m+1 |
| 8012 |
r[m]="{" |
| 8013 |
if n>0 then |
| 8014 |
local v=t[0] |
| 8015 |
if v then |
| 8016 |
m=m+1 |
| 8017 |
r[m]="[0]=" |
| 8018 |
if type(v)=="table" then |
| 8019 |
fastserialize(v) |
| 8020 |
else |
| 8021 |
r[m]=format("%q,",v) |
| 8022 |
end |
| 8023 |
end |
| 8024 |
for i=1,n do |
| 8025 |
local v=t[i] |
| 8026 |
m=m+1 |
| 8027 |
if type(v)=="table" then |
| 8028 |
r[m]=format("[%i]=",i) |
| 8029 |
fastserialize(v) |
| 8030 |
else |
| 8031 |
r[m]=format("[%i]=%q,",i,v) |
| 8032 |
end |
| 8033 |
end |
| 8034 |
end |
| 8035 |
for k,v in next,t do |
| 8036 |
local tk=type(k) |
| 8037 |
if tk=="number" then |
| 8038 |
if k>n or k<0 then |
| 8039 |
m=m+1 |
| 8040 |
if type(v)=="table" then |
| 8041 |
r[m]=format("[%i]=",k) |
| 8042 |
fastserialize(v) |
| 8043 |
else |
| 8044 |
r[m]=format("[%i]=%q,",k,v) |
| 8045 |
end |
| 8046 |
end |
| 8047 |
else |
| 8048 |
m=m+1 |
| 8049 |
if type(v)=="table" then |
| 8050 |
r[m]=format("[%q]=",k) |
| 8051 |
fastserialize(v) |
| 8052 |
else |
| 8053 |
r[m]=format("[%q]=%q,",k,v) |
| 8054 |
end |
| 8055 |
end |
| 8056 |
end |
| 8057 |
m=m+1 |
| 8058 |
if outer then |
| 8059 |
r[m]="}" |
| 8060 |
else |
| 8061 |
r[m]="}," |
| 8062 |
end |
| 8063 |
return r |
| 8064 |
end |
| 8065 |
return concat(fastserialize(t,true)) |
| 8066 |
end |
| 8067 |
end |
| 8068 |
function table.deserialize(str) |
| 8069 |
if not str or str=="" then |
| 8070 |
return |
| 8071 |
end |
| 8072 |
local code=load(str) |
| 8073 |
if not code then |
| 8074 |
return |
| 8075 |
end |
| 8076 |
code=code() |
| 8077 |
if not code then |
| 8078 |
return |
| 8079 |
end |
| 8080 |
return code |
| 8081 |
end |
| 8082 |
function table.load(filename,loader) |
| 8083 |
if filename then |
| 8084 |
local t=(loader or io.loaddata)(filename) |
| 8085 |
if t and t~="" then |
| 8086 |
local t=utftoeight(t) |
| 8087 |
t=load(t) |
| 8088 |
if type(t)=="function" then |
| 8089 |
t=t() |
| 8090 |
if type(t)=="table" then |
| 8091 |
return t |
| 8092 |
end |
| 8093 |
end |
| 8094 |
end |
| 8095 |
end |
| 8096 |
end |
| 8097 |
function table.save(filename,t,n,...) |
| 8098 |
io.savedata(filename,table.serialize(t,n==nil and true or n,...)) |
| 8099 |
end |
| 8100 |
local f_key_value=formatters["%s=%q"] |
| 8101 |
local f_add_table=formatters[" {%t},\n"] |
| 8102 |
local f_return_table=formatters["return {\n%t}"] |
| 8103 |
local function slowdrop(t) |
| 8104 |
local r={} |
| 8105 |
local l={} |
| 8106 |
for i=1,#t do |
| 8107 |
local ti=t[i] |
| 8108 |
local j=0 |
| 8109 |
for k,v in next,ti do |
| 8110 |
j=j+1 |
| 8111 |
l[j]=f_key_value(k,v) |
| 8112 |
end |
| 8113 |
r[i]=f_add_table(l) |
| 8114 |
end |
| 8115 |
return f_return_table(r) |
| 8116 |
end |
| 8117 |
local function fastdrop(t) |
| 8118 |
local r={ "return {\n" } |
| 8119 |
local m=1 |
| 8120 |
for i=1,#t do |
| 8121 |
local ti=t[i] |
| 8122 |
m=m+1 r[m]=" {" |
| 8123 |
for k,v in next,ti do |
| 8124 |
m=m+1 r[m]=f_key_value(k,v) |
| 8125 |
end |
| 8126 |
m=m+1 r[m]="},\n" |
| 8127 |
end |
| 8128 |
m=m+1 |
| 8129 |
r[m]="}" |
| 8130 |
return concat(r) |
| 8131 |
end |
| 8132 |
function table.drop(t,slow) |
| 8133 |
if #t==0 then |
| 8134 |
return "return { }" |
| 8135 |
elseif slow==true then |
| 8136 |
return slowdrop(t) |
| 8137 |
else |
| 8138 |
return fastdrop(t) |
| 8139 |
end |
| 8140 |
end |
| 8141 |
local selfmapper={ __index=function(t,k) t[k]=k return k end } |
| 8142 |
function table.twowaymapper(t) |
| 8143 |
if not t then |
| 8144 |
t={} |
| 8145 |
else |
| 8146 |
local zero=rawget(t,0) |
| 8147 |
for i=zero and 0 or 1,#t do |
| 8148 |
local ti=t[i] |
| 8149 |
if ti then |
| 8150 |
local i=tostring(i) |
| 8151 |
t[i]=ti |
| 8152 |
t[ti]=i |
| 8153 |
end |
| 8154 |
end |
| 8155 |
end |
| 8156 |
setmetatable(t,selfmapper) |
| 8157 |
return t |
| 8158 |
end |
| 8159 |
local f_start_key_idx=formatters["%w{"] |
| 8160 |
local f_start_key_num=JITSUPPORTED and formatters["%w[%s]={"] or formatters["%w[%q]={"] |
| 8161 |
local f_start_key_str=formatters["%w[%q]={"] |
| 8162 |
local f_start_key_boo=formatters["%w[%l]={"] |
| 8163 |
local f_start_key_nop=formatters["%w{"] |
| 8164 |
local f_stop=formatters["%w},"] |
| 8165 |
local f_key_num_value_num=JITSUPPORTED and formatters["%w[%s]=%s,"] or formatters["%w[%s]=%q,"] |
| 8166 |
local f_key_str_value_num=JITSUPPORTED and formatters["%w[%Q]=%s,"] or formatters["%w[%Q]=%q,"] |
| 8167 |
local f_key_boo_value_num=JITSUPPORTED and formatters["%w[%l]=%s,"] or formatters["%w[%l]=%q,"] |
| 8168 |
local f_key_num_value_str=JITSUPPORTED and formatters["%w[%s]=%Q,"] or formatters["%w[%q]=%Q,"] |
| 8169 |
local f_key_str_value_str=formatters["%w[%Q]=%Q,"] |
| 8170 |
local f_key_boo_value_str=formatters["%w[%l]=%Q,"] |
| 8171 |
local f_key_num_value_boo=JITSUPPORTED and formatters["%w[%s]=%l,"] or formatters["%w[%q]=%l,"] |
| 8172 |
local f_key_str_value_boo=formatters["%w[%Q]=%l,"] |
| 8173 |
local f_key_boo_value_boo=formatters["%w[%l]=%l,"] |
| 8174 |
local f_key_num_value_not=JITSUPPORTED and formatters["%w[%s]={},"] or formatters["%w[%q]={},"] |
| 8175 |
local f_key_str_value_not=formatters["%w[%Q]={},"] |
| 8176 |
local f_key_boo_value_not=formatters["%w[%l]={},"] |
| 8177 |
local f_key_num_value_seq=JITSUPPORTED and formatters["%w[%s]={ %, t },"] or formatters["%w[%q]={ %, t },"] |
| 8178 |
local f_key_str_value_seq=formatters["%w[%Q]={ %, t },"] |
| 8179 |
local f_key_boo_value_seq=formatters["%w[%l]={ %, t },"] |
| 8180 |
local f_val_num=JITSUPPORTED and formatters["%w%s,"] or formatters["%w%q,"] |
| 8181 |
local f_val_str=formatters["%w%Q,"] |
| 8182 |
local f_val_boo=formatters["%w%l,"] |
| 8183 |
local f_val_not=formatters["%w{},"] |
| 8184 |
local f_val_seq=formatters["%w{ %, t },"] |
| 8185 |
local f_fin_seq=formatters[" %, t }"] |
| 8186 |
local f_table_return=formatters["return {"] |
| 8187 |
local f_table_name=formatters["%s={"] |
| 8188 |
local f_table_direct=formatters["{"] |
| 8189 |
local f_table_entry=formatters["[%Q]={"] |
| 8190 |
local f_table_finish=formatters["}"] |
| 8191 |
local spaces=utilities.strings.newrepeater(" ") |
| 8192 |
local original_serialize=table.serialize |
| 8193 |
local is_simple_table=table.is_simple_table |
| 8194 |
local function serialize(root,name,specification) |
| 8195 |
if type(specification)=="table" then |
| 8196 |
return original_serialize(root,name,specification) |
| 8197 |
end |
| 8198 |
local t |
| 8199 |
local n=1 |
| 8200 |
local unknown=false |
| 8201 |
local function do_serialize(root,name,depth,level,indexed) |
| 8202 |
if level>0 then |
| 8203 |
n=n+1 |
| 8204 |
if indexed then |
| 8205 |
t[n]=f_start_key_idx(depth) |
| 8206 |
else |
| 8207 |
local tn=type(name) |
| 8208 |
if tn=="number" then |
| 8209 |
t[n]=f_start_key_num(depth,name) |
| 8210 |
elseif tn=="string" then |
| 8211 |
t[n]=f_start_key_str(depth,name) |
| 8212 |
elseif tn=="boolean" then |
| 8213 |
t[n]=f_start_key_boo(depth,name) |
| 8214 |
else |
| 8215 |
t[n]=f_start_key_nop(depth) |
| 8216 |
end |
| 8217 |
end |
| 8218 |
depth=depth+1 |
| 8219 |
end |
| 8220 |
if root and next(root)~=nil then |
| 8221 |
local first=nil |
| 8222 |
local last=#root |
| 8223 |
if last>0 then |
| 8224 |
for k=1,last do |
| 8225 |
if rawget(root,k)==nil then |
| 8226 |
last=k-1 |
| 8227 |
break |
| 8228 |
end |
| 8229 |
end |
| 8230 |
if last>0 then |
| 8231 |
first=1 |
| 8232 |
end |
| 8233 |
end |
| 8234 |
local sk=sortedkeys(root) |
| 8235 |
for i=1,#sk do |
| 8236 |
local k=sk[i] |
| 8237 |
local v=root[k] |
| 8238 |
local tv=type(v) |
| 8239 |
local tk=type(k) |
| 8240 |
if first and tk=="number" and k<=last and k>=first then |
| 8241 |
if tv=="number" then |
| 8242 |
n=n+1 t[n]=f_val_num(depth,v) |
| 8243 |
elseif tv=="string" then |
| 8244 |
n=n+1 t[n]=f_val_str(depth,v) |
| 8245 |
elseif tv=="table" then |
| 8246 |
if next(v)==nil then |
| 8247 |
n=n+1 t[n]=f_val_not(depth) |
| 8248 |
else |
| 8249 |
local st=is_simple_table(v) |
| 8250 |
if st then |
| 8251 |
n=n+1 t[n]=f_val_seq(depth,st) |
| 8252 |
else |
| 8253 |
do_serialize(v,k,depth,level+1,true) |
| 8254 |
end |
| 8255 |
end |
| 8256 |
elseif tv=="boolean" then |
| 8257 |
n=n+1 t[n]=f_val_boo(depth,v) |
| 8258 |
elseif unknown then |
| 8259 |
n=n+1 t[n]=f_val_str(depth,tostring(v)) |
| 8260 |
end |
| 8261 |
elseif tv=="number" then |
| 8262 |
if tk=="number" then |
| 8263 |
n=n+1 t[n]=f_key_num_value_num(depth,k,v) |
| 8264 |
elseif tk=="string" then |
| 8265 |
n=n+1 t[n]=f_key_str_value_num(depth,k,v) |
| 8266 |
elseif tk=="boolean" then |
| 8267 |
n=n+1 t[n]=f_key_boo_value_num(depth,k,v) |
| 8268 |
elseif unknown then |
| 8269 |
n=n+1 t[n]=f_key_str_value_num(depth,tostring(k),v) |
| 8270 |
end |
| 8271 |
elseif tv=="string" then |
| 8272 |
if tk=="number" then |
| 8273 |
n=n+1 t[n]=f_key_num_value_str(depth,k,v) |
| 8274 |
elseif tk=="string" then |
| 8275 |
n=n+1 t[n]=f_key_str_value_str(depth,k,v) |
| 8276 |
elseif tk=="boolean" then |
| 8277 |
n=n+1 t[n]=f_key_boo_value_str(depth,k,v) |
| 8278 |
elseif unknown then |
| 8279 |
n=n+1 t[n]=f_key_str_value_str(depth,tostring(k),v) |
| 8280 |
end |
| 8281 |
elseif tv=="table" then |
| 8282 |
if next(v)==nil then |
| 8283 |
if tk=="number" then |
| 8284 |
n=n+1 t[n]=f_key_num_value_not(depth,k) |
| 8285 |
elseif tk=="string" then |
| 8286 |
n=n+1 t[n]=f_key_str_value_not(depth,k) |
| 8287 |
elseif tk=="boolean" then |
| 8288 |
n=n+1 t[n]=f_key_boo_value_not(depth,k) |
| 8289 |
elseif unknown then |
| 8290 |
n=n+1 t[n]=f_key_str_value_not(depth,tostring(k)) |
| 8291 |
end |
| 8292 |
else |
| 8293 |
local st=is_simple_table(v) |
| 8294 |
if not st then |
| 8295 |
do_serialize(v,k,depth,level+1) |
| 8296 |
elseif tk=="number" then |
| 8297 |
n=n+1 t[n]=f_key_num_value_seq(depth,k,st) |
| 8298 |
elseif tk=="string" then |
| 8299 |
n=n+1 t[n]=f_key_str_value_seq(depth,k,st) |
| 8300 |
elseif tk=="boolean" then |
| 8301 |
n=n+1 t[n]=f_key_boo_value_seq(depth,k,st) |
| 8302 |
elseif unknown then |
| 8303 |
n=n+1 t[n]=f_key_str_value_seq(depth,tostring(k),st) |
| 8304 |
end |
| 8305 |
end |
| 8306 |
elseif tv=="boolean" then |
| 8307 |
if tk=="number" then |
| 8308 |
n=n+1 t[n]=f_key_num_value_boo(depth,k,v) |
| 8309 |
elseif tk=="string" then |
| 8310 |
n=n+1 t[n]=f_key_str_value_boo(depth,k,v) |
| 8311 |
elseif tk=="boolean" then |
| 8312 |
n=n+1 t[n]=f_key_boo_value_boo(depth,k,v) |
| 8313 |
elseif unknown then |
| 8314 |
n=n+1 t[n]=f_key_str_value_boo(depth,tostring(k),v) |
| 8315 |
end |
| 8316 |
else |
| 8317 |
if tk=="number" then |
| 8318 |
n=n+1 t[n]=f_key_num_value_str(depth,k,tostring(v)) |
| 8319 |
elseif tk=="string" then |
| 8320 |
n=n+1 t[n]=f_key_str_value_str(depth,k,tostring(v)) |
| 8321 |
elseif tk=="boolean" then |
| 8322 |
n=n+1 t[n]=f_key_boo_value_str(depth,k,tostring(v)) |
| 8323 |
elseif unknown then |
| 8324 |
n=n+1 t[n]=f_key_str_value_str(depth,tostring(k),tostring(v)) |
| 8325 |
end |
| 8326 |
end |
| 8327 |
end |
| 8328 |
end |
| 8329 |
if level>0 then |
| 8330 |
n=n+1 t[n]=f_stop(depth-1) |
| 8331 |
end |
| 8332 |
end |
| 8333 |
local tname=type(name) |
| 8334 |
if tname=="string" then |
| 8335 |
if name=="return" then |
| 8336 |
t={ f_table_return() } |
| 8337 |
else |
| 8338 |
t={ f_table_name(name) } |
| 8339 |
end |
| 8340 |
elseif tname=="number" then |
| 8341 |
t={ f_table_entry(name) } |
| 8342 |
elseif tname=="boolean" then |
| 8343 |
if name then |
| 8344 |
t={ f_table_return() } |
| 8345 |
else |
| 8346 |
t={ f_table_direct() } |
| 8347 |
end |
| 8348 |
else |
| 8349 |
t={ f_table_name("t") } |
| 8350 |
end |
| 8351 |
if root then |
| 8352 |
if getmetatable(root) then |
| 8353 |
local dummy=root._w_h_a_t_e_v_e_r_ |
| 8354 |
root._w_h_a_t_e_v_e_r_=nil |
| 8355 |
end |
| 8356 |
if next(root)~=nil then |
| 8357 |
local st=is_simple_table(root) |
| 8358 |
if st then |
| 8359 |
return t[1]..f_fin_seq(st) |
| 8360 |
else |
| 8361 |
do_serialize(root,name,1,0) |
| 8362 |
end |
| 8363 |
end |
| 8364 |
end |
| 8365 |
n=n+1 |
| 8366 |
t[n]=f_table_finish() |
| 8367 |
return concat(t,"\n") |
| 8368 |
end |
| 8369 |
table.serialize=serialize |
| 8370 |
if setinspector then |
| 8371 |
setinspector("table",function(v) |
| 8372 |
if type(v)=="table" then |
| 8373 |
print(serialize(v,"table",{ metacheck=false })) |
| 8374 |
return true |
| 8375 |
end |
| 8376 |
end) |
| 8377 |
end |
| 8378 |
local mt={ |
| 8379 |
__newindex=function(t,k,v) |
| 8380 |
local n=t.last+1 |
| 8381 |
t.last=n |
| 8382 |
t.list[n]=k |
| 8383 |
t.hash[k]=v |
| 8384 |
end, |
| 8385 |
__index=function(t,k) |
| 8386 |
return t.hash[k] |
| 8387 |
end, |
| 8388 |
__len=function(t) |
| 8389 |
return t.last |
| 8390 |
end, |
| 8391 |
} |
| 8392 |
function table.orderedhash() |
| 8393 |
return setmetatable({ list={},hash={},last=0 },mt) |
| 8394 |
end |
| 8395 |
function table.ordered(t) |
| 8396 |
local n=t.last |
| 8397 |
if n>0 then |
| 8398 |
local l=t.list |
| 8399 |
local i=1 |
| 8400 |
local h=t.hash |
| 8401 |
local f=function() |
| 8402 |
if i<=n then |
| 8403 |
local k=i |
| 8404 |
local v=h[l[k]] |
| 8405 |
i=i+1 |
| 8406 |
return k,v |
| 8407 |
end |
| 8408 |
end |
| 8409 |
return f,1,h[l[1]] |
| 8410 |
else |
| 8411 |
return function() end |
| 8412 |
end |
| 8413 |
end |
| 8414 |
local function combine(target,source) |
| 8415 |
if target then |
| 8416 |
for k,v in next,source do |
| 8417 |
if type(v)=="table" then |
| 8418 |
target[k]=combine(target[k],source[k]) |
| 8419 |
else |
| 8420 |
target[k]=v |
| 8421 |
end |
| 8422 |
end |
| 8423 |
return target |
| 8424 |
else |
| 8425 |
return source |
| 8426 |
end |
| 8427 |
end |
| 8428 |
table.combine=combine |
| 8429 |
|
| 8430 |
|
| 8431 |
end -- of closure |
| 8432 |
|
| 8433 |
do -- create closure to overcome 200 locals limit |
| 8434 |
|
| 8435 |
package.loaded["util-fil"] = package.loaded["util-fil"] or true |
| 8436 |
|
| 8437 |
-- original size: 11474, stripped down to: 8973 |
| 8438 |
|
| 8439 |
if not modules then modules={} end modules ['util-fil']={ |
| 8440 |
version=1.001, |
| 8441 |
optimize=true, |
| 8442 |
comment="companion to luat-lib.mkiv", |
| 8443 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 8444 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 8445 |
license="see context related readme files" |
| 8446 |
} |
| 8447 |
local tonumber=tonumber |
| 8448 |
local byte=string.byte |
| 8449 |
local char=string.char |
| 8450 |
utilities=utilities or {} |
| 8451 |
local files={} |
| 8452 |
utilities.files=files |
| 8453 |
local zerobased={} |
| 8454 |
function files.open(filename,zb) |
| 8455 |
local f=io.open(filename,"rb") |
| 8456 |
if f then |
| 8457 |
zerobased[f]=zb or false |
| 8458 |
end |
| 8459 |
return f |
| 8460 |
end |
| 8461 |
function files.close(f) |
| 8462 |
zerobased[f]=nil |
| 8463 |
f:close() |
| 8464 |
end |
| 8465 |
function files.size(f) |
| 8466 |
local current=f:seek() |
| 8467 |
local size=f:seek("end") |
| 8468 |
f:seek("set",current) |
| 8469 |
return size |
| 8470 |
end |
| 8471 |
files.getsize=files.size |
| 8472 |
function files.setposition(f,n) |
| 8473 |
if zerobased[f] then |
| 8474 |
f:seek("set",n) |
| 8475 |
else |
| 8476 |
f:seek("set",n-1) |
| 8477 |
end |
| 8478 |
end |
| 8479 |
function files.getposition(f) |
| 8480 |
if zerobased[f] then |
| 8481 |
return f:seek() |
| 8482 |
else |
| 8483 |
return f:seek()+1 |
| 8484 |
end |
| 8485 |
end |
| 8486 |
function files.look(f,n,chars) |
| 8487 |
local p=f:seek() |
| 8488 |
local s=f:read(n) |
| 8489 |
f:seek("set",p) |
| 8490 |
if chars then |
| 8491 |
return s |
| 8492 |
else |
| 8493 |
return byte(s,1,#s) |
| 8494 |
end |
| 8495 |
end |
| 8496 |
function files.skip(f,n) |
| 8497 |
if n==1 then |
| 8498 |
f:read(n) |
| 8499 |
else |
| 8500 |
f:seek("set",f:seek()+n) |
| 8501 |
end |
| 8502 |
end |
| 8503 |
function files.readbyte(f) |
| 8504 |
return byte(f:read(1)) |
| 8505 |
end |
| 8506 |
function files.readbytes(f,n) |
| 8507 |
return byte(f:read(n),1,n) |
| 8508 |
end |
| 8509 |
function files.readbytetable(f,n) |
| 8510 |
local s=f:read(n or 1) |
| 8511 |
return { byte(s,1,#s) } |
| 8512 |
end |
| 8513 |
function files.readchar(f) |
| 8514 |
return f:read(1) |
| 8515 |
end |
| 8516 |
function files.readstring(f,n) |
| 8517 |
return f:read(n or 1) |
| 8518 |
end |
| 8519 |
function files.readinteger1(f) |
| 8520 |
local n=byte(f:read(1)) |
| 8521 |
if n>=0x80 then |
| 8522 |
return n-0x100 |
| 8523 |
else |
| 8524 |
return n |
| 8525 |
end |
| 8526 |
end |
| 8527 |
files.readcardinal1=files.readbyte |
| 8528 |
files.readcardinal=files.readcardinal1 |
| 8529 |
files.readinteger=files.readinteger1 |
| 8530 |
files.readsignedbyte=files.readinteger1 |
| 8531 |
function files.readcardinal2(f) |
| 8532 |
local a,b=byte(f:read(2),1,2) |
| 8533 |
return 0x100*a+b |
| 8534 |
end |
| 8535 |
function files.readcardinal2le(f) |
| 8536 |
local b,a=byte(f:read(2),1,2) |
| 8537 |
return 0x100*a+b |
| 8538 |
end |
| 8539 |
function files.readinteger2(f) |
| 8540 |
local a,b=byte(f:read(2),1,2) |
| 8541 |
if a>=0x80 then |
| 8542 |
return 0x100*a+b-0x10000 |
| 8543 |
else |
| 8544 |
return 0x100*a+b |
| 8545 |
end |
| 8546 |
end |
| 8547 |
function files.readinteger2le(f) |
| 8548 |
local b,a=byte(f:read(2),1,2) |
| 8549 |
if a>=0x80 then |
| 8550 |
return 0x100*a+b-0x10000 |
| 8551 |
else |
| 8552 |
return 0x100*a+b |
| 8553 |
end |
| 8554 |
end |
| 8555 |
function files.readcardinal3(f) |
| 8556 |
local a,b,c=byte(f:read(3),1,3) |
| 8557 |
return 0x10000*a+0x100*b+c |
| 8558 |
end |
| 8559 |
function files.readcardinal3le(f) |
| 8560 |
local c,b,a=byte(f:read(3),1,3) |
| 8561 |
return 0x10000*a+0x100*b+c |
| 8562 |
end |
| 8563 |
function files.readinteger3(f) |
| 8564 |
local a,b,c=byte(f:read(3),1,3) |
| 8565 |
if a>=0x80 then |
| 8566 |
return 0x10000*a+0x100*b+c-0x1000000 |
| 8567 |
else |
| 8568 |
return 0x10000*a+0x100*b+c |
| 8569 |
end |
| 8570 |
end |
| 8571 |
function files.readinteger3le(f) |
| 8572 |
local c,b,a=byte(f:read(3),1,3) |
| 8573 |
if a>=0x80 then |
| 8574 |
return 0x10000*a+0x100*b+c-0x1000000 |
| 8575 |
else |
| 8576 |
return 0x10000*a+0x100*b+c |
| 8577 |
end |
| 8578 |
end |
| 8579 |
function files.readcardinal4(f) |
| 8580 |
local a,b,c,d=byte(f:read(4),1,4) |
| 8581 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 8582 |
end |
| 8583 |
function files.readcardinal4le(f) |
| 8584 |
local d,c,b,a=byte(f:read(4),1,4) |
| 8585 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 8586 |
end |
| 8587 |
function files.readinteger4(f) |
| 8588 |
local a,b,c,d=byte(f:read(4),1,4) |
| 8589 |
if a>=0x80 then |
| 8590 |
return 0x1000000*a+0x10000*b+0x100*c+d-0x100000000 |
| 8591 |
else |
| 8592 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 8593 |
end |
| 8594 |
end |
| 8595 |
function files.readinteger4le(f) |
| 8596 |
local d,c,b,a=byte(f:read(4),1,4) |
| 8597 |
if a>=0x80 then |
| 8598 |
return 0x1000000*a+0x10000*b+0x100*c+d-0x100000000 |
| 8599 |
else |
| 8600 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 8601 |
end |
| 8602 |
end |
| 8603 |
function files.readfixed2(f) |
| 8604 |
local n1,n2=byte(f:read(2),1,2) |
| 8605 |
if n1>=0x80 then |
| 8606 |
n1=n1-0x100 |
| 8607 |
end |
| 8608 |
return n1+n2/0xFF |
| 8609 |
end |
| 8610 |
function files.readfixed4(f) |
| 8611 |
local a,b,c,d=byte(f:read(4),1,4) |
| 8612 |
local n1=0x100*a+b |
| 8613 |
local n2=0x100*c+d |
| 8614 |
if n1>=0x8000 then |
| 8615 |
n1=n1-0x10000 |
| 8616 |
end |
| 8617 |
return n1+n2/0xFFFF |
| 8618 |
end |
| 8619 |
if bit32 then |
| 8620 |
local extract=bit32.extract |
| 8621 |
local band=bit32.band |
| 8622 |
function files.read2dot14(f) |
| 8623 |
local a,b=byte(f:read(2),1,2) |
| 8624 |
if a>=0x80 then |
| 8625 |
local n=-(0x100*a+b) |
| 8626 |
return-(extract(n,14,2)+(band(n,0x3FFF)/16384.0)) |
| 8627 |
else |
| 8628 |
local n=0x100*a+b |
| 8629 |
return (extract(n,14,2)+(band(n,0x3FFF)/16384.0)) |
| 8630 |
end |
| 8631 |
end |
| 8632 |
end |
| 8633 |
function files.skipshort(f,n) |
| 8634 |
f:read(2*(n or 1)) |
| 8635 |
end |
| 8636 |
function files.skiplong(f,n) |
| 8637 |
f:read(4*(n or 1)) |
| 8638 |
end |
| 8639 |
if bit32 then |
| 8640 |
local rshift=bit32.rshift |
| 8641 |
function files.writecardinal2(f,n) |
| 8642 |
local a=char(n%256) |
| 8643 |
n=rshift(n,8) |
| 8644 |
local b=char(n%256) |
| 8645 |
f:write(b,a) |
| 8646 |
end |
| 8647 |
function files.writecardinal4(f,n) |
| 8648 |
local a=char(n%256) |
| 8649 |
n=rshift(n,8) |
| 8650 |
local b=char(n%256) |
| 8651 |
n=rshift(n,8) |
| 8652 |
local c=char(n%256) |
| 8653 |
n=rshift(n,8) |
| 8654 |
local d=char(n%256) |
| 8655 |
f:write(d,c,b,a) |
| 8656 |
end |
| 8657 |
function files.writecardinal2le(f,n) |
| 8658 |
local a=char(n%256) |
| 8659 |
n=rshift(n,8) |
| 8660 |
local b=char(n%256) |
| 8661 |
f:write(a,b) |
| 8662 |
end |
| 8663 |
function files.writecardinal4le(f,n) |
| 8664 |
local a=char(n%256) |
| 8665 |
n=rshift(n,8) |
| 8666 |
local b=char(n%256) |
| 8667 |
n=rshift(n,8) |
| 8668 |
local c=char(n%256) |
| 8669 |
n=rshift(n,8) |
| 8670 |
local d=char(n%256) |
| 8671 |
f:write(a,b,c,d) |
| 8672 |
end |
| 8673 |
else |
| 8674 |
local floor=math.floor |
| 8675 |
function files.writecardinal2(f,n) |
| 8676 |
local a=char(n%256) |
| 8677 |
n=floor(n/256) |
| 8678 |
local b=char(n%256) |
| 8679 |
f:write(b,a) |
| 8680 |
end |
| 8681 |
function files.writecardinal4(f,n) |
| 8682 |
local a=char(n%256) |
| 8683 |
n=floor(n/256) |
| 8684 |
local b=char(n%256) |
| 8685 |
n=floor(n/256) |
| 8686 |
local c=char(n%256) |
| 8687 |
n=floor(n/256) |
| 8688 |
local d=char(n%256) |
| 8689 |
f:write(d,c,b,a) |
| 8690 |
end |
| 8691 |
function files.writecardinal2le(f,n) |
| 8692 |
local a=char(n%256) |
| 8693 |
n=floor(n/256) |
| 8694 |
local b=char(n%256) |
| 8695 |
f:write(a,b) |
| 8696 |
end |
| 8697 |
function files.writecardinal4le(f,n) |
| 8698 |
local a=char(n%256) |
| 8699 |
n=floor(n/256) |
| 8700 |
local b=char(n%256) |
| 8701 |
n=floor(n/256) |
| 8702 |
local c=char(n%256) |
| 8703 |
n=floor(n/256) |
| 8704 |
local d=char(n%256) |
| 8705 |
f:write(a,b,c,d) |
| 8706 |
end |
| 8707 |
end |
| 8708 |
function files.writestring(f,s) |
| 8709 |
f:write(char(byte(s,1,#s))) |
| 8710 |
end |
| 8711 |
function files.writebyte(f,b) |
| 8712 |
f:write(char(b)) |
| 8713 |
end |
| 8714 |
if fio and fio.readcardinal1 then |
| 8715 |
files.readcardinal1=fio.readcardinal1 |
| 8716 |
files.readcardinal2=fio.readcardinal2 |
| 8717 |
files.readcardinal3=fio.readcardinal3 |
| 8718 |
files.readcardinal4=fio.readcardinal4 |
| 8719 |
files.readcardinal1le=fio.readcardinal1le or files.readcardinal1le |
| 8720 |
files.readcardinal2le=fio.readcardinal2le or files.readcardinal2le |
| 8721 |
files.readcardinal3le=fio.readcardinal3le or files.readcardinal3le |
| 8722 |
files.readcardinal4le=fio.readcardinal4le or files.readcardinal4le |
| 8723 |
files.readinteger1=fio.readinteger1 |
| 8724 |
files.readinteger2=fio.readinteger2 |
| 8725 |
files.readinteger3=fio.readinteger3 |
| 8726 |
files.readinteger4=fio.readinteger4 |
| 8727 |
files.readinteger1le=fio.readinteger1le or files.readinteger1le |
| 8728 |
files.readinteger2le=fio.readinteger2le or files.readinteger2le |
| 8729 |
files.readinteger3le=fio.readinteger3le or files.readinteger3le |
| 8730 |
files.readinteger4le=fio.readinteger4le or files.readinteger4le |
| 8731 |
files.readfixed2=fio.readfixed2 |
| 8732 |
files.readfixed4=fio.readfixed4 |
| 8733 |
files.read2dot14=fio.read2dot14 |
| 8734 |
files.setposition=fio.setposition |
| 8735 |
files.getposition=fio.getposition |
| 8736 |
files.readbyte=files.readcardinal1 |
| 8737 |
files.readsignedbyte=files.readinteger1 |
| 8738 |
files.readcardinal=files.readcardinal1 |
| 8739 |
files.readinteger=files.readinteger1 |
| 8740 |
local skipposition=fio.skipposition |
| 8741 |
files.skipposition=skipposition |
| 8742 |
files.readbytes=fio.readbytes |
| 8743 |
files.readbytetable=fio.readbytetable |
| 8744 |
function files.skipshort(f,n) |
| 8745 |
skipposition(f,2*(n or 1)) |
| 8746 |
end |
| 8747 |
function files.skiplong(f,n) |
| 8748 |
skipposition(f,4*(n or 1)) |
| 8749 |
end |
| 8750 |
end |
| 8751 |
if fio and fio.writecardinal1 then |
| 8752 |
files.writecardinal1=fio.writecardinal1 |
| 8753 |
files.writecardinal2=fio.writecardinal2 |
| 8754 |
files.writecardinal3=fio.writecardinal3 |
| 8755 |
files.writecardinal4=fio.writecardinal4 |
| 8756 |
files.writecardinal1le=fio.writecardinal1le |
| 8757 |
files.writecardinal2le=fio.writecardinal2le |
| 8758 |
files.writecardinal3le=fio.writecardinal3le |
| 8759 |
files.writecardinal4le=fio.writecardinal4le |
| 8760 |
files.writeinteger1=fio.writeinteger1 or fio.writecardinal1 |
| 8761 |
files.writeinteger2=fio.writeinteger2 or fio.writecardinal2 |
| 8762 |
files.writeinteger3=fio.writeinteger3 or fio.writecardinal3 |
| 8763 |
files.writeinteger4=fio.writeinteger4 or fio.writecardinal4 |
| 8764 |
files.writeinteger1le=files.writeinteger1le or fio.writecardinal1le |
| 8765 |
files.writeinteger2le=files.writeinteger2le or fio.writecardinal2le |
| 8766 |
files.writeinteger3le=files.writeinteger3le or fio.writecardinal3le |
| 8767 |
files.writeinteger4le=files.writeinteger4le or fio.writecardinal4le |
| 8768 |
end |
| 8769 |
if fio and fio.readcardinaltable then |
| 8770 |
files.readcardinaltable=fio.readcardinaltable |
| 8771 |
files.readintegertable=fio.readintegertable |
| 8772 |
else |
| 8773 |
local readcardinal1=files.readcardinal1 |
| 8774 |
local readcardinal2=files.readcardinal2 |
| 8775 |
local readcardinal3=files.readcardinal3 |
| 8776 |
local readcardinal4=files.readcardinal4 |
| 8777 |
function files.readcardinaltable(f,n,b) |
| 8778 |
local t={} |
| 8779 |
if b==1 then for i=1,n do t[i]=readcardinal1(f) end |
| 8780 |
elseif b==2 then for i=1,n do t[i]=readcardinal2(f) end |
| 8781 |
elseif b==3 then for i=1,n do t[i]=readcardinal3(f) end |
| 8782 |
elseif b==4 then for i=1,n do t[i]=readcardinal4(f) end end |
| 8783 |
return t |
| 8784 |
end |
| 8785 |
local readinteger1=files.readinteger1 |
| 8786 |
local readinteger2=files.readinteger2 |
| 8787 |
local readinteger3=files.readinteger3 |
| 8788 |
local readinteger4=files.readinteger4 |
| 8789 |
function files.readintegertable(f,n,b) |
| 8790 |
local t={} |
| 8791 |
if b==1 then for i=1,n do t[i]=readinteger1(f) end |
| 8792 |
elseif b==2 then for i=1,n do t[i]=readinteger2(f) end |
| 8793 |
elseif b==3 then for i=1,n do t[i]=readinteger3(f) end |
| 8794 |
elseif b==4 then for i=1,n do t[i]=readinteger4(f) end end |
| 8795 |
return t |
| 8796 |
end |
| 8797 |
end |
| 8798 |
|
| 8799 |
|
| 8800 |
end -- of closure |
| 8801 |
|
| 8802 |
do -- create closure to overcome 200 locals limit |
| 8803 |
|
| 8804 |
package.loaded["util-sac"] = package.loaded["util-sac"] or true |
| 8805 |
|
| 8806 |
-- original size: 14107, stripped down to: 10453 |
| 8807 |
|
| 8808 |
if not modules then modules={} end modules ['util-sac']={ |
| 8809 |
version=1.001, |
| 8810 |
optimize=true, |
| 8811 |
comment="companion to luat-lib.mkiv", |
| 8812 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 8813 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 8814 |
license="see context related readme files" |
| 8815 |
} |
| 8816 |
local byte,sub=string.byte,string.sub |
| 8817 |
local tonumber=tonumber |
| 8818 |
utilities=utilities or {} |
| 8819 |
local streams={} |
| 8820 |
utilities.streams=streams |
| 8821 |
function streams.open(filename,zerobased) |
| 8822 |
local f=filename and io.loaddata(filename) |
| 8823 |
if f then |
| 8824 |
return { f,1,#f,zerobased or false } |
| 8825 |
end |
| 8826 |
end |
| 8827 |
function streams.openstring(f,zerobased) |
| 8828 |
if f then |
| 8829 |
return { f,1,#f,zerobased or false } |
| 8830 |
end |
| 8831 |
end |
| 8832 |
function streams.getstring(f) |
| 8833 |
if f then |
| 8834 |
return f[1] |
| 8835 |
end |
| 8836 |
end |
| 8837 |
function streams.close() |
| 8838 |
end |
| 8839 |
function streams.size(f) |
| 8840 |
return f and f[3] or 0 |
| 8841 |
end |
| 8842 |
streams.getsize=streams.size |
| 8843 |
function streams.setposition(f,i) |
| 8844 |
if f[4] then |
| 8845 |
if i<=0 then |
| 8846 |
f[2]=1 |
| 8847 |
else |
| 8848 |
f[2]=i+1 |
| 8849 |
end |
| 8850 |
else |
| 8851 |
if i<=1 then |
| 8852 |
f[2]=1 |
| 8853 |
else |
| 8854 |
f[2]=i |
| 8855 |
end |
| 8856 |
end |
| 8857 |
end |
| 8858 |
function streams.getposition(f) |
| 8859 |
if f[4] then |
| 8860 |
return f[2]-1 |
| 8861 |
else |
| 8862 |
return f[2] |
| 8863 |
end |
| 8864 |
end |
| 8865 |
function streams.look(f,n,chars) |
| 8866 |
local b=f[2] |
| 8867 |
local e=b+n-1 |
| 8868 |
if chars then |
| 8869 |
return sub(f[1],b,e) |
| 8870 |
else |
| 8871 |
return byte(f[1],b,e) |
| 8872 |
end |
| 8873 |
end |
| 8874 |
function streams.skip(f,n) |
| 8875 |
f[2]=f[2]+n |
| 8876 |
end |
| 8877 |
function streams.readbyte(f) |
| 8878 |
local i=f[2] |
| 8879 |
f[2]=i+1 |
| 8880 |
return byte(f[1],i) |
| 8881 |
end |
| 8882 |
function streams.readbytes(f,n) |
| 8883 |
local i=f[2] |
| 8884 |
local j=i+n |
| 8885 |
f[2]=j |
| 8886 |
return byte(f[1],i,j-1) |
| 8887 |
end |
| 8888 |
function streams.readbytetable(f,n) |
| 8889 |
local i=f[2] |
| 8890 |
local j=i+n |
| 8891 |
f[2]=j |
| 8892 |
return { byte(f[1],i,j-1) } |
| 8893 |
end |
| 8894 |
function streams.skipbytes(f,n) |
| 8895 |
f[2]=f[2]+n |
| 8896 |
end |
| 8897 |
function streams.readchar(f) |
| 8898 |
local i=f[2] |
| 8899 |
f[2]=i+1 |
| 8900 |
return sub(f[1],i,i) |
| 8901 |
end |
| 8902 |
function streams.readstring(f,n) |
| 8903 |
local i=f[2] |
| 8904 |
local j=i+n |
| 8905 |
f[2]=j |
| 8906 |
return sub(f[1],i,j-1) |
| 8907 |
end |
| 8908 |
function streams.readinteger1(f) |
| 8909 |
local i=f[2] |
| 8910 |
f[2]=i+1 |
| 8911 |
local n=byte(f[1],i) |
| 8912 |
if n>=0x80 then |
| 8913 |
return n-0x100 |
| 8914 |
else |
| 8915 |
return n |
| 8916 |
end |
| 8917 |
end |
| 8918 |
streams.readcardinal1=streams.readbyte |
| 8919 |
streams.readcardinal=streams.readcardinal1 |
| 8920 |
streams.readinteger=streams.readinteger1 |
| 8921 |
function streams.readcardinal2(f) |
| 8922 |
local i=f[2] |
| 8923 |
local j=i+1 |
| 8924 |
f[2]=j+1 |
| 8925 |
local a,b=byte(f[1],i,j) |
| 8926 |
return 0x100*a+b |
| 8927 |
end |
| 8928 |
function streams.readcardinal2le(f) |
| 8929 |
local i=f[2] |
| 8930 |
local j=i+1 |
| 8931 |
f[2]=j+1 |
| 8932 |
local b,a=byte(f[1],i,j) |
| 8933 |
return 0x100*a+b |
| 8934 |
end |
| 8935 |
function streams.readinteger2(f) |
| 8936 |
local i=f[2] |
| 8937 |
local j=i+1 |
| 8938 |
f[2]=j+1 |
| 8939 |
local a,b=byte(f[1],i,j) |
| 8940 |
if a>=0x80 then |
| 8941 |
return 0x100*a+b-0x10000 |
| 8942 |
else |
| 8943 |
return 0x100*a+b |
| 8944 |
end |
| 8945 |
end |
| 8946 |
function streams.readinteger2le(f) |
| 8947 |
local i=f[2] |
| 8948 |
local j=i+1 |
| 8949 |
f[2]=j+1 |
| 8950 |
local b,a=byte(f[1],i,j) |
| 8951 |
if a>=0x80 then |
| 8952 |
return 0x100*a+b-0x10000 |
| 8953 |
else |
| 8954 |
return 0x100*a+b |
| 8955 |
end |
| 8956 |
end |
| 8957 |
function streams.readcardinal3(f) |
| 8958 |
local i=f[2] |
| 8959 |
local j=i+2 |
| 8960 |
f[2]=j+1 |
| 8961 |
local a,b,c=byte(f[1],i,j) |
| 8962 |
return 0x10000*a+0x100*b+c |
| 8963 |
end |
| 8964 |
function streams.readcardinal3le(f) |
| 8965 |
local i=f[2] |
| 8966 |
local j=i+2 |
| 8967 |
f[2]=j+1 |
| 8968 |
local c,b,a=byte(f[1],i,j) |
| 8969 |
return 0x10000*a+0x100*b+c |
| 8970 |
end |
| 8971 |
function streams.readinteger3(f) |
| 8972 |
local i=f[2] |
| 8973 |
local j=i+3 |
| 8974 |
f[2]=j+1 |
| 8975 |
local a,b,c=byte(f[1],i,j) |
| 8976 |
if a>=0x80 then |
| 8977 |
return 0x10000*a+0x100*b+c-0x1000000 |
| 8978 |
else |
| 8979 |
return 0x10000*a+0x100*b+c |
| 8980 |
end |
| 8981 |
end |
| 8982 |
function streams.readinteger3le(f) |
| 8983 |
local i=f[2] |
| 8984 |
local j=i+3 |
| 8985 |
f[2]=j+1 |
| 8986 |
local c,b,a=byte(f[1],i,j) |
| 8987 |
if a>=0x80 then |
| 8988 |
return 0x10000*a+0x100*b+c-0x1000000 |
| 8989 |
else |
| 8990 |
return 0x10000*a+0x100*b+c |
| 8991 |
end |
| 8992 |
end |
| 8993 |
function streams.readcardinal4(f) |
| 8994 |
local i=f[2] |
| 8995 |
local j=i+3 |
| 8996 |
f[2]=j+1 |
| 8997 |
local a,b,c,d=byte(f[1],i,j) |
| 8998 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 8999 |
end |
| 9000 |
function streams.readcardinal4le(f) |
| 9001 |
local i=f[2] |
| 9002 |
local j=i+3 |
| 9003 |
f[2]=j+1 |
| 9004 |
local d,c,b,a=byte(f[1],i,j) |
| 9005 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 9006 |
end |
| 9007 |
function streams.readinteger4(f) |
| 9008 |
local i=f[2] |
| 9009 |
local j=i+3 |
| 9010 |
f[2]=j+1 |
| 9011 |
local a,b,c,d=byte(f[1],i,j) |
| 9012 |
if a>=0x80 then |
| 9013 |
return 0x1000000*a+0x10000*b+0x100*c+d-0x100000000 |
| 9014 |
else |
| 9015 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 9016 |
end |
| 9017 |
end |
| 9018 |
function streams.readinteger4le(f) |
| 9019 |
local i=f[2] |
| 9020 |
local j=i+3 |
| 9021 |
f[2]=j+1 |
| 9022 |
local d,c,b,a=byte(f[1],i,j) |
| 9023 |
if a>=0x80 then |
| 9024 |
return 0x1000000*a+0x10000*b+0x100*c+d-0x100000000 |
| 9025 |
else |
| 9026 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 9027 |
end |
| 9028 |
end |
| 9029 |
function streams.readfixed2(f) |
| 9030 |
local i=f[2] |
| 9031 |
local j=i+1 |
| 9032 |
f[2]=j+1 |
| 9033 |
local n1,n2=byte(f[1],i,j) |
| 9034 |
if n1>=0x80 then |
| 9035 |
n1=n1-0x100 |
| 9036 |
end |
| 9037 |
return n1+n2/0xFF |
| 9038 |
end |
| 9039 |
function streams.readfixed4(f) |
| 9040 |
local i=f[2] |
| 9041 |
local j=i+3 |
| 9042 |
f[2]=j+1 |
| 9043 |
local a,b,c,d=byte(f[1],i,j) |
| 9044 |
local n1=0x100*a+b |
| 9045 |
local n2=0x100*c+d |
| 9046 |
if n1>=0x8000 then |
| 9047 |
n1=n1-0x10000 |
| 9048 |
end |
| 9049 |
return n1+n2/0xFFFF |
| 9050 |
end |
| 9051 |
if bit32 then |
| 9052 |
local extract=bit32.extract |
| 9053 |
local band=bit32.band |
| 9054 |
function streams.read2dot14(f) |
| 9055 |
local i=f[2] |
| 9056 |
local j=i+1 |
| 9057 |
f[2]=j+1 |
| 9058 |
local a,b=byte(f[1],i,j) |
| 9059 |
if a>=0x80 then |
| 9060 |
local n=-(0x100*a+b) |
| 9061 |
return-(extract(n,14,2)+(band(n,0x3FFF)/16384.0)) |
| 9062 |
else |
| 9063 |
local n=0x100*a+b |
| 9064 |
return (extract(n,14,2)+(band(n,0x3FFF)/16384.0)) |
| 9065 |
end |
| 9066 |
end |
| 9067 |
end |
| 9068 |
function streams.skipshort(f,n) |
| 9069 |
f[2]=f[2]+2*(n or 1) |
| 9070 |
end |
| 9071 |
function streams.skiplong(f,n) |
| 9072 |
f[2]=f[2]+4*(n or 1) |
| 9073 |
end |
| 9074 |
if sio and sio.readcardinal2 then |
| 9075 |
local readcardinal1=sio.readcardinal1 |
| 9076 |
local readcardinal2=sio.readcardinal2 |
| 9077 |
local readcardinal3=sio.readcardinal3 |
| 9078 |
local readcardinal4=sio.readcardinal4 |
| 9079 |
local readinteger1=sio.readinteger1 |
| 9080 |
local readinteger2=sio.readinteger2 |
| 9081 |
local readinteger3=sio.readinteger3 |
| 9082 |
local readinteger4=sio.readinteger4 |
| 9083 |
local readfixed2=sio.readfixed2 |
| 9084 |
local readfixed4=sio.readfixed4 |
| 9085 |
local read2dot14=sio.read2dot14 |
| 9086 |
local readbytes=sio.readbytes |
| 9087 |
local readbytetable=sio.readbytetable |
| 9088 |
function streams.readcardinal1(f) |
| 9089 |
local i=f[2] |
| 9090 |
f[2]=i+1 |
| 9091 |
return readcardinal1(f[1],i) |
| 9092 |
end |
| 9093 |
function streams.readcardinal2(f) |
| 9094 |
local i=f[2] |
| 9095 |
f[2]=i+2 |
| 9096 |
return readcardinal2(f[1],i) |
| 9097 |
end |
| 9098 |
function streams.readcardinal3(f) |
| 9099 |
local i=f[2] |
| 9100 |
f[2]=i+3 |
| 9101 |
return readcardinal3(f[1],i) |
| 9102 |
end |
| 9103 |
function streams.readcardinal4(f) |
| 9104 |
local i=f[2] |
| 9105 |
f[2]=i+4 |
| 9106 |
return readcardinal4(f[1],i) |
| 9107 |
end |
| 9108 |
function streams.readinteger1(f) |
| 9109 |
local i=f[2] |
| 9110 |
f[2]=i+1 |
| 9111 |
return readinteger1(f[1],i) |
| 9112 |
end |
| 9113 |
function streams.readinteger2(f) |
| 9114 |
local i=f[2] |
| 9115 |
f[2]=i+2 |
| 9116 |
return readinteger2(f[1],i) |
| 9117 |
end |
| 9118 |
function streams.readinteger3(f) |
| 9119 |
local i=f[2] |
| 9120 |
f[2]=i+3 |
| 9121 |
return readinteger3(f[1],i) |
| 9122 |
end |
| 9123 |
function streams.readinteger4(f) |
| 9124 |
local i=f[2] |
| 9125 |
f[2]=i+4 |
| 9126 |
return readinteger4(f[1],i) |
| 9127 |
end |
| 9128 |
function streams.readfixed2(f) |
| 9129 |
local i=f[2] |
| 9130 |
f[2]=i+2 |
| 9131 |
return readfixed2(f[1],i) |
| 9132 |
end |
| 9133 |
function streams.readfixed4(f) |
| 9134 |
local i=f[2] |
| 9135 |
f[2]=i+4 |
| 9136 |
return readfixed4(f[1],i) |
| 9137 |
end |
| 9138 |
function streams.read2dot14(f) |
| 9139 |
local i=f[2] |
| 9140 |
f[2]=i+2 |
| 9141 |
return read2dot14(f[1],i) |
| 9142 |
end |
| 9143 |
function streams.readbytes(f,n) |
| 9144 |
local i=f[2] |
| 9145 |
local s=f[3] |
| 9146 |
local p=i+n |
| 9147 |
if p>s then |
| 9148 |
f[2]=s+1 |
| 9149 |
else |
| 9150 |
f[2]=p |
| 9151 |
end |
| 9152 |
return readbytes(f[1],i,n) |
| 9153 |
end |
| 9154 |
function streams.readbytetable(f,n) |
| 9155 |
local i=f[2] |
| 9156 |
local s=f[3] |
| 9157 |
local p=i+n |
| 9158 |
if p>s then |
| 9159 |
f[2]=s+1 |
| 9160 |
else |
| 9161 |
f[2]=p |
| 9162 |
end |
| 9163 |
return readbytetable(f[1],i,n) |
| 9164 |
end |
| 9165 |
streams.readbyte=streams.readcardinal1 |
| 9166 |
streams.readsignedbyte=streams.readinteger1 |
| 9167 |
streams.readcardinal=streams.readcardinal1 |
| 9168 |
streams.readinteger=streams.readinteger1 |
| 9169 |
end |
| 9170 |
if sio and sio.readcardinaltable then |
| 9171 |
local readcardinaltable=sio.readcardinaltable |
| 9172 |
local readintegertable=sio.readintegertable |
| 9173 |
function utilities.streams.readcardinaltable(f,n,b) |
| 9174 |
local i=f[2] |
| 9175 |
local s=f[3] |
| 9176 |
local p=i+n*b |
| 9177 |
if p>s then |
| 9178 |
f[2]=s+1 |
| 9179 |
else |
| 9180 |
f[2]=p |
| 9181 |
end |
| 9182 |
return readcardinaltable(f[1],i,n,b) |
| 9183 |
end |
| 9184 |
function utilities.streams.readintegertable(f,n,b) |
| 9185 |
local i=f[2] |
| 9186 |
local s=f[3] |
| 9187 |
local p=i+n*b |
| 9188 |
if p>s then |
| 9189 |
f[2]=s+1 |
| 9190 |
else |
| 9191 |
f[2]=p |
| 9192 |
end |
| 9193 |
return readintegertable(f[1],i,n,b) |
| 9194 |
end |
| 9195 |
else |
| 9196 |
local readcardinal1=streams.readcardinal1 |
| 9197 |
local readcardinal2=streams.readcardinal2 |
| 9198 |
local readcardinal3=streams.readcardinal3 |
| 9199 |
local readcardinal4=streams.readcardinal4 |
| 9200 |
function streams.readcardinaltable(f,n,b) |
| 9201 |
local i=f[2] |
| 9202 |
local s=f[3] |
| 9203 |
local p=i+n*b |
| 9204 |
if p>s then |
| 9205 |
f[2]=s+1 |
| 9206 |
else |
| 9207 |
f[2]=p |
| 9208 |
end |
| 9209 |
local t={} |
| 9210 |
if b==1 then for i=1,n do t[i]=readcardinal1(f[1],i) end |
| 9211 |
elseif b==2 then for i=1,n do t[i]=readcardinal2(f[1],i) end |
| 9212 |
elseif b==3 then for i=1,n do t[i]=readcardinal3(f[1],i) end |
| 9213 |
elseif b==4 then for i=1,n do t[i]=readcardinal4(f[1],i) end end |
| 9214 |
return t |
| 9215 |
end |
| 9216 |
local readinteger1=streams.readinteger1 |
| 9217 |
local readinteger2=streams.readinteger2 |
| 9218 |
local readinteger3=streams.readinteger3 |
| 9219 |
local readinteger4=streams.readinteger4 |
| 9220 |
function streams.readintegertable(f,n,b) |
| 9221 |
local i=f[2] |
| 9222 |
local s=f[3] |
| 9223 |
local p=i+n*b |
| 9224 |
if p>s then |
| 9225 |
f[2]=s+1 |
| 9226 |
else |
| 9227 |
f[2]=p |
| 9228 |
end |
| 9229 |
local t={} |
| 9230 |
if b==1 then for i=1,n do t[i]=readinteger1(f[1],i) end |
| 9231 |
elseif b==2 then for i=1,n do t[i]=readinteger2(f[1],i) end |
| 9232 |
elseif b==3 then for i=1,n do t[i]=readinteger3(f[1],i) end |
| 9233 |
elseif b==4 then for i=1,n do t[i]=readinteger4(f[1],i) end end |
| 9234 |
return t |
| 9235 |
end |
| 9236 |
end |
| 9237 |
do |
| 9238 |
local files=utilities.files |
| 9239 |
if files then |
| 9240 |
local openfile=files.open |
| 9241 |
local openstream=streams.open |
| 9242 |
local openstring=streams.openstring |
| 9243 |
local setmetatable=setmetatable |
| 9244 |
function io.newreader(str,method) |
| 9245 |
local f,m |
| 9246 |
if method=="string" then |
| 9247 |
f=openstring(str,true) |
| 9248 |
m=streams |
| 9249 |
elseif method=="stream" then |
| 9250 |
f=openstream(str,true) |
| 9251 |
m=streams |
| 9252 |
else |
| 9253 |
f=openfile(str,"rb") |
| 9254 |
m=files |
| 9255 |
end |
| 9256 |
if f then |
| 9257 |
local t={} |
| 9258 |
setmetatable(t,{ |
| 9259 |
__index=function(t,k) |
| 9260 |
local r=m[k] |
| 9261 |
if k=="close" then |
| 9262 |
if f then |
| 9263 |
m.close(f) |
| 9264 |
f=nil |
| 9265 |
end |
| 9266 |
return function() end |
| 9267 |
elseif r then |
| 9268 |
local v=function(_,a,b) return r(f,a,b) end |
| 9269 |
t[k]=v |
| 9270 |
return v |
| 9271 |
else |
| 9272 |
print("unknown key",k) |
| 9273 |
end |
| 9274 |
end |
| 9275 |
} ) |
| 9276 |
return t |
| 9277 |
end |
| 9278 |
end |
| 9279 |
end |
| 9280 |
end |
| 9281 |
if bit32 and not streams.tocardinal1 then |
| 9282 |
local extract=bit32.extract |
| 9283 |
local char=string.char |
| 9284 |
streams.tocardinal1=char |
| 9285 |
function streams.tocardinal2(n) return char(extract(n,8,8),extract(n,0,8)) end |
| 9286 |
function streams.tocardinal3(n) return char(extract(n,16,8),extract(n,8,8),extract(n,0,8)) end |
| 9287 |
function streams.tocardinal4(n) return char(extract(n,24,8),extract(n,16,8),extract(n,8,8),extract(n,0,8)) end |
| 9288 |
streams.tocardinal1le=char |
| 9289 |
function streams.tocardinal2le(n) return char(extract(n,0,8),extract(n,8,8)) end |
| 9290 |
function streams.tocardinal3le(n) return char(extract(n,0,8),extract(n,8,8),extract(n,16,8)) end |
| 9291 |
function streams.tocardinal4le(n) return char(extract(n,0,8),extract(n,8,8),extract(n,16,8),extract(n,24,8)) end |
| 9292 |
end |
| 9293 |
if not streams.readcstring then |
| 9294 |
local readchar=streams.readchar |
| 9295 |
local concat=table.concat |
| 9296 |
function streams.readcstring(f) |
| 9297 |
local t={} |
| 9298 |
while true do |
| 9299 |
local c=readchar(f) |
| 9300 |
if c and c~="\0" then |
| 9301 |
t[#t+1]=c |
| 9302 |
else |
| 9303 |
return concat(t) |
| 9304 |
end |
| 9305 |
end |
| 9306 |
end |
| 9307 |
end |
| 9308 |
|
| 9309 |
|
| 9310 |
end -- of closure |
| 9311 |
|
| 9312 |
do -- create closure to overcome 200 locals limit |
| 9313 |
|
| 9314 |
package.loaded["util-sto"] = package.loaded["util-sto"] or true |
| 9315 |
|
| 9316 |
-- original size: 6661, stripped down to: 3074 |
| 9317 |
|
| 9318 |
if not modules then modules={} end modules ['util-sto']={ |
| 9319 |
version=1.001, |
| 9320 |
comment="companion to luat-lib.mkiv", |
| 9321 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 9322 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 9323 |
license="see context related readme files" |
| 9324 |
} |
| 9325 |
local setmetatable,getmetatable,rawset,type=setmetatable,getmetatable,rawset,type |
| 9326 |
utilities=utilities or {} |
| 9327 |
utilities.storage=utilities.storage or {} |
| 9328 |
local storage=utilities.storage |
| 9329 |
function storage.mark(t) |
| 9330 |
if not t then |
| 9331 |
print("\nfatal error: storage cannot be marked\n") |
| 9332 |
os.exit() |
| 9333 |
return |
| 9334 |
end |
| 9335 |
local m=getmetatable(t) |
| 9336 |
if not m then |
| 9337 |
m={} |
| 9338 |
setmetatable(t,m) |
| 9339 |
end |
| 9340 |
m.__storage__=true |
| 9341 |
return t |
| 9342 |
end |
| 9343 |
function storage.allocate(t) |
| 9344 |
t=t or {} |
| 9345 |
local m=getmetatable(t) |
| 9346 |
if not m then |
| 9347 |
m={} |
| 9348 |
setmetatable(t,m) |
| 9349 |
end |
| 9350 |
m.__storage__=true |
| 9351 |
return t |
| 9352 |
end |
| 9353 |
function storage.marked(t) |
| 9354 |
local m=getmetatable(t) |
| 9355 |
return m and m.__storage__ |
| 9356 |
end |
| 9357 |
function storage.checked(t) |
| 9358 |
if not t then |
| 9359 |
report("\nfatal error: storage has not been allocated\n") |
| 9360 |
os.exit() |
| 9361 |
return |
| 9362 |
end |
| 9363 |
return t |
| 9364 |
end |
| 9365 |
function storage.setinitializer(data,initialize) |
| 9366 |
local m=getmetatable(data) or {} |
| 9367 |
m.__index=function(data,k) |
| 9368 |
m.__index=nil |
| 9369 |
initialize() |
| 9370 |
return data[k] |
| 9371 |
end |
| 9372 |
setmetatable(data,m) |
| 9373 |
end |
| 9374 |
local keyisvalue={ __index=function(t,k) |
| 9375 |
t[k]=k |
| 9376 |
return k |
| 9377 |
end } |
| 9378 |
function storage.sparse(t) |
| 9379 |
t=t or {} |
| 9380 |
setmetatable(t,keyisvalue) |
| 9381 |
return t |
| 9382 |
end |
| 9383 |
local function f_empty () return "" end |
| 9384 |
local function f_self (t,k) t[k]=k return k end |
| 9385 |
local function f_table (t,k) local v={} t[k]=v return v end |
| 9386 |
local function f_number(t,k) t[k]=0 return 0 end |
| 9387 |
local function f_ignore() end |
| 9388 |
local f_index={ |
| 9389 |
["empty"]=f_empty, |
| 9390 |
["self"]=f_self, |
| 9391 |
["table"]=f_table, |
| 9392 |
["number"]=f_number, |
| 9393 |
} |
| 9394 |
function table.setmetatableindex(t,f) |
| 9395 |
if type(t)~="table" then |
| 9396 |
f,t=t,{} |
| 9397 |
end |
| 9398 |
local m=getmetatable(t) |
| 9399 |
local i=f_index[f] or f |
| 9400 |
if m then |
| 9401 |
m.__index=i |
| 9402 |
else |
| 9403 |
setmetatable(t,{ __index=i }) |
| 9404 |
end |
| 9405 |
return t |
| 9406 |
end |
| 9407 |
local f_index={ |
| 9408 |
["ignore"]=f_ignore, |
| 9409 |
} |
| 9410 |
function table.setmetatablenewindex(t,f) |
| 9411 |
if type(t)~="table" then |
| 9412 |
f,t=t,{} |
| 9413 |
end |
| 9414 |
local m=getmetatable(t) |
| 9415 |
local i=f_index[f] or f |
| 9416 |
if m then |
| 9417 |
m.__newindex=i |
| 9418 |
else |
| 9419 |
setmetatable(t,{ __newindex=i }) |
| 9420 |
end |
| 9421 |
return t |
| 9422 |
end |
| 9423 |
function table.setmetatablecall(t,f) |
| 9424 |
if type(t)~="table" then |
| 9425 |
f,t=t,{} |
| 9426 |
end |
| 9427 |
local m=getmetatable(t) |
| 9428 |
if m then |
| 9429 |
m.__call=f |
| 9430 |
else |
| 9431 |
setmetatable(t,{ __call=f }) |
| 9432 |
end |
| 9433 |
return t |
| 9434 |
end |
| 9435 |
function table.setmetatableindices(t,f,n,c) |
| 9436 |
if type(t)~="table" then |
| 9437 |
f,t=t,{} |
| 9438 |
end |
| 9439 |
local m=getmetatable(t) |
| 9440 |
local i=f_index[f] or f |
| 9441 |
if m then |
| 9442 |
m.__index=i |
| 9443 |
m.__newindex=n |
| 9444 |
m.__call=c |
| 9445 |
else |
| 9446 |
setmetatable(t,{ |
| 9447 |
__index=i, |
| 9448 |
__newindex=n, |
| 9449 |
__call=c, |
| 9450 |
}) |
| 9451 |
end |
| 9452 |
return t |
| 9453 |
end |
| 9454 |
function table.setmetatablekey(t,key,value) |
| 9455 |
local m=getmetatable(t) |
| 9456 |
if not m then |
| 9457 |
m={} |
| 9458 |
setmetatable(t,m) |
| 9459 |
end |
| 9460 |
m[key]=value |
| 9461 |
return t |
| 9462 |
end |
| 9463 |
function table.getmetatablekey(t,key,value) |
| 9464 |
local m=getmetatable(t) |
| 9465 |
return m and m[key] |
| 9466 |
end |
| 9467 |
function table.makeweak(t) |
| 9468 |
if not t then |
| 9469 |
t={} |
| 9470 |
end |
| 9471 |
local m=getmetatable(t) |
| 9472 |
if m then |
| 9473 |
m.__mode="v" |
| 9474 |
else |
| 9475 |
setmetatable(t,{ __mode="v" }) |
| 9476 |
end |
| 9477 |
return t |
| 9478 |
end |
| 9479 |
|
| 9480 |
|
| 9481 |
end -- of closure |
| 9482 |
|
| 9483 |
do -- create closure to overcome 200 locals limit |
| 9484 |
|
| 9485 |
package.loaded["util-prs"] = package.loaded["util-prs"] or true |
| 9486 |
|
| 9487 |
-- original size: 26298, stripped down to: 17137 |
| 9488 |
|
| 9489 |
if not modules then modules={} end modules ['util-prs']={ |
| 9490 |
version=1.001, |
| 9491 |
comment="companion to luat-lib.mkiv", |
| 9492 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 9493 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 9494 |
license="see context related readme files" |
| 9495 |
} |
| 9496 |
local lpeg,table,string=lpeg,table,string |
| 9497 |
local P,R,V,S,C,Ct,Cs,Carg,Cc,Cg,Cf,Cp=lpeg.P,lpeg.R,lpeg.V,lpeg.S,lpeg.C,lpeg.Ct,lpeg.Cs,lpeg.Carg,lpeg.Cc,lpeg.Cg,lpeg.Cf,lpeg.Cp |
| 9498 |
local lpegmatch,lpegpatterns=lpeg.match,lpeg.patterns |
| 9499 |
local concat,gmatch,find=table.concat,string.gmatch,string.find |
| 9500 |
local tonumber,tostring,type,next,rawset=tonumber,tostring,type,next,rawset |
| 9501 |
local mod,div=math.mod,math.div |
| 9502 |
utilities=utilities or {} |
| 9503 |
local parsers=utilities.parsers or {} |
| 9504 |
utilities.parsers=parsers |
| 9505 |
local patterns=parsers.patterns or {} |
| 9506 |
parsers.patterns=patterns |
| 9507 |
local setmetatableindex=table.setmetatableindex |
| 9508 |
local sortedhash=table.sortedhash |
| 9509 |
local sortedkeys=table.sortedkeys |
| 9510 |
local tohash=table.tohash |
| 9511 |
local hashes={} |
| 9512 |
parsers.hashes=hashes |
| 9513 |
local digit=R("09") |
| 9514 |
local space=P(' ') |
| 9515 |
local equal=P("=") |
| 9516 |
local colon=P(":") |
| 9517 |
local comma=P(",") |
| 9518 |
local lbrace=P("{") |
| 9519 |
local rbrace=P("}") |
| 9520 |
local lparent=P("(") |
| 9521 |
local rparent=P(")") |
| 9522 |
local lbracket=P("[") |
| 9523 |
local rbracket=P("]") |
| 9524 |
local period=S(".") |
| 9525 |
local punctuation=S(".,:;") |
| 9526 |
local spacer=lpegpatterns.spacer |
| 9527 |
local whitespace=lpegpatterns.whitespace |
| 9528 |
local newline=lpegpatterns.newline |
| 9529 |
local anything=lpegpatterns.anything |
| 9530 |
local endofstring=lpegpatterns.endofstring |
| 9531 |
local nobrace=1-(lbrace+rbrace ) |
| 9532 |
local noparent=1-(lparent+rparent) |
| 9533 |
local nobracket=1-(lbracket+rbracket) |
| 9534 |
local escape,left,right=P("\\"),P('{'),P('}') |
| 9535 |
lpegpatterns.balanced=P { |
| 9536 |
((escape*(left+right))+(1-(left+right))+V(2))^0, |
| 9537 |
left*V(1)*right |
| 9538 |
} |
| 9539 |
local nestedbraces=P { lbrace*(nobrace+V(1))^0*rbrace } |
| 9540 |
local nestedparents=P { lparent*(noparent+V(1))^0*rparent } |
| 9541 |
local nestedbrackets=P { lbracket*(nobracket+V(1))^0*rbracket } |
| 9542 |
local spaces=space^0 |
| 9543 |
local argument=Cs((lbrace/"")*((nobrace+nestedbraces)^0)*(rbrace/"")) |
| 9544 |
local content=(1-endofstring)^0 |
| 9545 |
lpegpatterns.nestedbraces=nestedbraces |
| 9546 |
lpegpatterns.nestedparents=nestedparents |
| 9547 |
lpegpatterns.nestedbrackets=nestedbrackets |
| 9548 |
lpegpatterns.nested=nestedbraces |
| 9549 |
lpegpatterns.argument=argument |
| 9550 |
lpegpatterns.content=content |
| 9551 |
local value=lbrace*C((nobrace+nestedbraces)^0)*rbrace+C((nestedbraces+(1-comma))^0) |
| 9552 |
local key=C((1-equal-comma)^1) |
| 9553 |
local pattern_a=(space+comma)^0*(key*equal*value+key*C("")) |
| 9554 |
local pattern_c=(space+comma)^0*(key*equal*value) |
| 9555 |
local pattern_d=(space+comma)^0*(key*(equal+colon)*value+key*C("")) |
| 9556 |
local key=C((1-space-equal-comma)^1) |
| 9557 |
local pattern_b=spaces*comma^0*spaces*(key*((spaces*equal*spaces*value)+C(""))) |
| 9558 |
local hash={} |
| 9559 |
local function set(key,value) |
| 9560 |
hash[key]=value |
| 9561 |
end |
| 9562 |
local pattern_a_s=(pattern_a/set)^1 |
| 9563 |
local pattern_b_s=(pattern_b/set)^1 |
| 9564 |
local pattern_c_s=(pattern_c/set)^1 |
| 9565 |
local pattern_d_s=(pattern_d/set)^1 |
| 9566 |
patterns.settings_to_hash_a=pattern_a_s |
| 9567 |
patterns.settings_to_hash_b=pattern_b_s |
| 9568 |
patterns.settings_to_hash_c=pattern_c_s |
| 9569 |
patterns.settings_to_hash_d=pattern_d_s |
| 9570 |
function parsers.make_settings_to_hash_pattern(set,how) |
| 9571 |
if how=="strict" then |
| 9572 |
return (pattern_c/set)^1 |
| 9573 |
elseif how=="tolerant" then |
| 9574 |
return (pattern_b/set)^1 |
| 9575 |
else |
| 9576 |
return (pattern_a/set)^1 |
| 9577 |
end |
| 9578 |
end |
| 9579 |
function parsers.settings_to_hash(str,existing) |
| 9580 |
if not str or str=="" then |
| 9581 |
return {} |
| 9582 |
elseif type(str)=="table" then |
| 9583 |
if existing then |
| 9584 |
for k,v in next,str do |
| 9585 |
existing[k]=v |
| 9586 |
end |
| 9587 |
return exiting |
| 9588 |
else |
| 9589 |
return str |
| 9590 |
end |
| 9591 |
else |
| 9592 |
hash=existing or {} |
| 9593 |
lpegmatch(pattern_a_s,str) |
| 9594 |
return hash |
| 9595 |
end |
| 9596 |
end |
| 9597 |
function parsers.settings_to_hash_colon_too(str) |
| 9598 |
if not str or str=="" then |
| 9599 |
return {} |
| 9600 |
elseif type(str)=="table" then |
| 9601 |
return str |
| 9602 |
else |
| 9603 |
hash={} |
| 9604 |
lpegmatch(pattern_d_s,str) |
| 9605 |
return hash |
| 9606 |
end |
| 9607 |
end |
| 9608 |
function parsers.settings_to_hash_tolerant(str,existing) |
| 9609 |
if not str or str=="" then |
| 9610 |
return {} |
| 9611 |
elseif type(str)=="table" then |
| 9612 |
if existing then |
| 9613 |
for k,v in next,str do |
| 9614 |
existing[k]=v |
| 9615 |
end |
| 9616 |
return exiting |
| 9617 |
else |
| 9618 |
return str |
| 9619 |
end |
| 9620 |
else |
| 9621 |
hash=existing or {} |
| 9622 |
lpegmatch(pattern_b_s,str) |
| 9623 |
return hash |
| 9624 |
end |
| 9625 |
end |
| 9626 |
function parsers.settings_to_hash_strict(str,existing) |
| 9627 |
if not str or str=="" then |
| 9628 |
return nil |
| 9629 |
elseif type(str)=="table" then |
| 9630 |
if existing then |
| 9631 |
for k,v in next,str do |
| 9632 |
existing[k]=v |
| 9633 |
end |
| 9634 |
return exiting |
| 9635 |
else |
| 9636 |
return str |
| 9637 |
end |
| 9638 |
elseif str and str~="" then |
| 9639 |
hash=existing or {} |
| 9640 |
lpegmatch(pattern_c_s,str) |
| 9641 |
return next(hash) and hash |
| 9642 |
end |
| 9643 |
end |
| 9644 |
local separator=comma*space^0 |
| 9645 |
local value=lbrace*C((nobrace+nestedbraces)^0)*rbrace+C((nestedbraces+(1-comma))^0) |
| 9646 |
local pattern=spaces*Ct(value*(separator*value)^0) |
| 9647 |
patterns.settings_to_array=pattern |
| 9648 |
function parsers.settings_to_array(str,strict) |
| 9649 |
if not str or str=="" then |
| 9650 |
return {} |
| 9651 |
elseif type(str)=="table" then |
| 9652 |
return str |
| 9653 |
elseif strict then |
| 9654 |
if find(str,"{",1,true) then |
| 9655 |
return lpegmatch(pattern,str) |
| 9656 |
else |
| 9657 |
return { str } |
| 9658 |
end |
| 9659 |
elseif find(str,",",1,true) then |
| 9660 |
return lpegmatch(pattern,str) |
| 9661 |
else |
| 9662 |
return { str } |
| 9663 |
end |
| 9664 |
end |
| 9665 |
function parsers.settings_to_numbers(str) |
| 9666 |
if not str or str=="" then |
| 9667 |
return {} |
| 9668 |
end |
| 9669 |
if type(str)=="table" then |
| 9670 |
elseif find(str,",",1,true) then |
| 9671 |
str=lpegmatch(pattern,str) |
| 9672 |
else |
| 9673 |
return { tonumber(str) } |
| 9674 |
end |
| 9675 |
for i=1,#str do |
| 9676 |
str[i]=tonumber(str[i]) |
| 9677 |
end |
| 9678 |
return str |
| 9679 |
end |
| 9680 |
local value=lbrace*C((nobrace+nestedbraces)^0)*rbrace+C((nestedbraces+nestedbrackets+nestedparents+(1-comma))^0) |
| 9681 |
local pattern=spaces*Ct(value*(separator*value)^0) |
| 9682 |
function parsers.settings_to_array_obey_fences(str) |
| 9683 |
return lpegmatch(pattern,str) |
| 9684 |
end |
| 9685 |
local cache_a={} |
| 9686 |
local cache_b={} |
| 9687 |
function parsers.groupedsplitat(symbol,withaction) |
| 9688 |
if not symbol then |
| 9689 |
symbol="," |
| 9690 |
end |
| 9691 |
local pattern=(withaction and cache_b or cache_a)[symbol] |
| 9692 |
if not pattern then |
| 9693 |
local symbols=S(symbol) |
| 9694 |
local separator=space^0*symbols*space^0 |
| 9695 |
local value=lbrace*C((nobrace+nestedbraces)^0) |
| 9696 |
*(rbrace*(#symbols+P(-1))) |
| 9697 |
+C((nestedbraces+(1-(space^0*(symbols+P(-1)))))^0) |
| 9698 |
if withaction then |
| 9699 |
local withvalue=Carg(1)*value/function(f,s) return f(s) end |
| 9700 |
pattern=spaces*withvalue*(separator*withvalue)^0 |
| 9701 |
cache_b[symbol]=pattern |
| 9702 |
else |
| 9703 |
pattern=spaces*Ct(value*(separator*value)^0) |
| 9704 |
cache_a[symbol]=pattern |
| 9705 |
end |
| 9706 |
end |
| 9707 |
return pattern |
| 9708 |
end |
| 9709 |
local pattern_a=parsers.groupedsplitat(",",false) |
| 9710 |
local pattern_b=parsers.groupedsplitat(",",true) |
| 9711 |
function parsers.stripped_settings_to_array(str) |
| 9712 |
if not str or str=="" then |
| 9713 |
return {} |
| 9714 |
else |
| 9715 |
return lpegmatch(pattern_a,str) |
| 9716 |
end |
| 9717 |
end |
| 9718 |
function parsers.process_stripped_settings(str,action) |
| 9719 |
if not str or str=="" then |
| 9720 |
return {} |
| 9721 |
else |
| 9722 |
return lpegmatch(pattern_b,str,1,action) |
| 9723 |
end |
| 9724 |
end |
| 9725 |
local function set(t,v) |
| 9726 |
t[#t+1]=v |
| 9727 |
end |
| 9728 |
local value=P(Carg(1)*value)/set |
| 9729 |
local pattern=value*(separator*value)^0*Carg(1) |
| 9730 |
function parsers.add_settings_to_array(t,str) |
| 9731 |
return lpegmatch(pattern,str,nil,t) |
| 9732 |
end |
| 9733 |
function parsers.hash_to_string(h,separator,yes,no,strict,omit) |
| 9734 |
if h then |
| 9735 |
local t={} |
| 9736 |
local tn=0 |
| 9737 |
local s=sortedkeys(h) |
| 9738 |
omit=omit and tohash(omit) |
| 9739 |
for i=1,#s do |
| 9740 |
local key=s[i] |
| 9741 |
if not omit or not omit[key] then |
| 9742 |
local value=h[key] |
| 9743 |
if type(value)=="boolean" then |
| 9744 |
if yes and no then |
| 9745 |
if value then |
| 9746 |
tn=tn+1 |
| 9747 |
t[tn]=key..'='..yes |
| 9748 |
elseif not strict then |
| 9749 |
tn=tn+1 |
| 9750 |
t[tn]=key..'='..no |
| 9751 |
end |
| 9752 |
elseif value or not strict then |
| 9753 |
tn=tn+1 |
| 9754 |
t[tn]=key..'='..tostring(value) |
| 9755 |
end |
| 9756 |
else |
| 9757 |
tn=tn+1 |
| 9758 |
t[tn]=key..'='..value |
| 9759 |
end |
| 9760 |
end |
| 9761 |
end |
| 9762 |
return concat(t,separator or ",") |
| 9763 |
else |
| 9764 |
return "" |
| 9765 |
end |
| 9766 |
end |
| 9767 |
function parsers.array_to_string(a,separator) |
| 9768 |
if a then |
| 9769 |
return concat(a,separator or ",") |
| 9770 |
else |
| 9771 |
return "" |
| 9772 |
end |
| 9773 |
end |
| 9774 |
local pattern=Cf(Ct("")*Cg(C((1-S(", "))^1)*S(", ")^0*Cc(true))^1,rawset) |
| 9775 |
function parsers.settings_to_set(str) |
| 9776 |
return str and lpegmatch(pattern,str) or {} |
| 9777 |
end |
| 9778 |
hashes.settings_to_set=table.setmetatableindex(function(t,k) |
| 9779 |
local v=k and lpegmatch(pattern,k) or {} |
| 9780 |
t[k]=v |
| 9781 |
return v |
| 9782 |
end) |
| 9783 |
function parsers.settings_to_set(str) |
| 9784 |
return str and lpegmatch(pattern,str) or {} |
| 9785 |
end |
| 9786 |
local pattern=Ct((C((1-S(", "))^1)*S(", ")^0)^1) |
| 9787 |
hashes.settings_to_list=table.setmetatableindex(function(t,k) |
| 9788 |
local v=k and lpegmatch(pattern,k) or {} |
| 9789 |
t[k]=v |
| 9790 |
return v |
| 9791 |
end) |
| 9792 |
getmetatable(hashes.settings_to_set ).__mode="kv" |
| 9793 |
getmetatable(hashes.settings_to_list).__mode="kv" |
| 9794 |
function parsers.simple_hash_to_string(h,separator) |
| 9795 |
local t={} |
| 9796 |
local tn=0 |
| 9797 |
for k,v in sortedhash(h) do |
| 9798 |
if v then |
| 9799 |
tn=tn+1 |
| 9800 |
t[tn]=k |
| 9801 |
end |
| 9802 |
end |
| 9803 |
return concat(t,separator or ",") |
| 9804 |
end |
| 9805 |
local str=Cs(lpegpatterns.unquoted)+C((1-whitespace-equal)^1) |
| 9806 |
local setting=Cf(Carg(1)*(whitespace^0*Cg(str*whitespace^0*(equal*whitespace^0*str+Cc(""))))^1,rawset) |
| 9807 |
local splitter=setting^1 |
| 9808 |
function parsers.options_to_hash(str,target) |
| 9809 |
return str and lpegmatch(splitter,str,1,target or {}) or {} |
| 9810 |
end |
| 9811 |
local splitter=lpeg.tsplitat(" ") |
| 9812 |
function parsers.options_to_array(str) |
| 9813 |
return str and lpegmatch(splitter,str) or {} |
| 9814 |
end |
| 9815 |
local value=P(lbrace*C((nobrace+nestedbraces)^0)*rbrace)+C(digit^1*lparent*(noparent+nestedparents)^1*rparent)+C((nestedbraces+(1-comma))^1)+Cc("") |
| 9816 |
local pattern_a=spaces*Ct(value*(separator*value)^0) |
| 9817 |
local function repeater(n,str) |
| 9818 |
if not n then |
| 9819 |
return str |
| 9820 |
else |
| 9821 |
local s=lpegmatch(pattern_a,str) |
| 9822 |
if n==1 then |
| 9823 |
return unpack(s) |
| 9824 |
else |
| 9825 |
local t={} |
| 9826 |
local tn=0 |
| 9827 |
for i=1,n do |
| 9828 |
for j=1,#s do |
| 9829 |
tn=tn+1 |
| 9830 |
t[tn]=s[j] |
| 9831 |
end |
| 9832 |
end |
| 9833 |
return unpack(t) |
| 9834 |
end |
| 9835 |
end |
| 9836 |
end |
| 9837 |
local value=P(lbrace*C((nobrace+nestedbraces)^0)*rbrace)+(C(digit^1)/tonumber*lparent*Cs((noparent+nestedparents)^1)*rparent)/repeater+C((nestedbraces+(1-comma))^1)+Cc("") |
| 9838 |
local pattern_b=spaces*Ct(value*(separator*value)^0) |
| 9839 |
function parsers.settings_to_array_with_repeat(str,expand) |
| 9840 |
if expand then |
| 9841 |
return lpegmatch(pattern_b,str) or {} |
| 9842 |
else |
| 9843 |
return lpegmatch(pattern_a,str) or {} |
| 9844 |
end |
| 9845 |
end |
| 9846 |
local value=lbrace*C((nobrace+nestedbraces)^0)*rbrace |
| 9847 |
local pattern=Ct((space+value)^0) |
| 9848 |
function parsers.arguments_to_table(str) |
| 9849 |
return lpegmatch(pattern,str) |
| 9850 |
end |
| 9851 |
function parsers.getparameters(self,class,parentclass,settings) |
| 9852 |
local sc=self[class] |
| 9853 |
if not sc then |
| 9854 |
sc={} |
| 9855 |
self[class]=sc |
| 9856 |
if parentclass then |
| 9857 |
local sp=self[parentclass] |
| 9858 |
if not sp then |
| 9859 |
sp={} |
| 9860 |
self[parentclass]=sp |
| 9861 |
end |
| 9862 |
setmetatableindex(sc,sp) |
| 9863 |
end |
| 9864 |
end |
| 9865 |
parsers.settings_to_hash(settings,sc) |
| 9866 |
end |
| 9867 |
function parsers.listitem(str) |
| 9868 |
return gmatch(str,"[^, ]+") |
| 9869 |
end |
| 9870 |
local pattern=Cs { "start", |
| 9871 |
start=V("one")+V("two")+V("three"), |
| 9872 |
rest=(Cc(",")*V("thousand"))^0*(P(".")+endofstring)*anything^0, |
| 9873 |
thousand=digit*digit*digit, |
| 9874 |
one=digit*V("rest"), |
| 9875 |
two=digit*digit*V("rest"), |
| 9876 |
three=V("thousand")*V("rest"), |
| 9877 |
} |
| 9878 |
lpegpatterns.splitthousands=pattern |
| 9879 |
function parsers.splitthousands(str) |
| 9880 |
return lpegmatch(pattern,str) or str |
| 9881 |
end |
| 9882 |
local optionalwhitespace=whitespace^0 |
| 9883 |
lpegpatterns.words=Ct((Cs((1-punctuation-whitespace)^1)+anything)^1) |
| 9884 |
lpegpatterns.sentences=Ct((optionalwhitespace*Cs((1-period)^0*period))^1) |
| 9885 |
lpegpatterns.paragraphs=Ct((optionalwhitespace*Cs((whitespace^1*endofstring/""+1-(spacer^0*newline*newline))^1))^1) |
| 9886 |
local dquote=P('"') |
| 9887 |
local equal=P('=') |
| 9888 |
local escape=P('\\') |
| 9889 |
local separator=S(' ,') |
| 9890 |
local key=C((1-equal)^1) |
| 9891 |
local value=dquote*C((1-dquote-escape*dquote)^0)*dquote |
| 9892 |
local pattern=Cf(Ct("")*(Cg(key*equal*value)*separator^0)^1,rawset)^0*P(-1) |
| 9893 |
function parsers.keq_to_hash(str) |
| 9894 |
if str and str~="" then |
| 9895 |
return lpegmatch(pattern,str) |
| 9896 |
else |
| 9897 |
return {} |
| 9898 |
end |
| 9899 |
end |
| 9900 |
local defaultspecification={ separator=",",quote='"' } |
| 9901 |
function parsers.csvsplitter(specification) |
| 9902 |
specification=specification and setmetatableindex(specification,defaultspecification) or defaultspecification |
| 9903 |
local separator=specification.separator |
| 9904 |
local quotechar=specification.quote |
| 9905 |
local numbers=specification.numbers |
| 9906 |
local separator=S(separator~="" and separator or ",") |
| 9907 |
local whatever=C((1-separator-newline)^0) |
| 9908 |
if quotechar and quotechar~="" then |
| 9909 |
local quotedata=nil |
| 9910 |
for chr in gmatch(quotechar,".") do |
| 9911 |
local quotechar=P(chr) |
| 9912 |
local quoteitem=(1-quotechar)^0 |
| 9913 |
local quoteword=quotechar*(numbers and (quoteitem/tonumber) or C(quoteitem))*quotechar |
| 9914 |
if quotedata then |
| 9915 |
quotedata=quotedata+quoteword |
| 9916 |
else |
| 9917 |
quotedata=quoteword |
| 9918 |
end |
| 9919 |
end |
| 9920 |
whatever=quotedata+whatever |
| 9921 |
end |
| 9922 |
local parser=Ct((Ct(whatever*(separator*whatever)^0)*S("\n\r")^1)^0 ) |
| 9923 |
return function(data) |
| 9924 |
return lpegmatch(parser,data) |
| 9925 |
end |
| 9926 |
end |
| 9927 |
function parsers.rfc4180splitter(specification) |
| 9928 |
specification=specification and setmetatableindex(specification,defaultspecification) or defaultspecification |
| 9929 |
local numbers=specification.numbers |
| 9930 |
local separator=specification.separator |
| 9931 |
local quotechar=P(specification.quote) |
| 9932 |
local dquotechar=quotechar*quotechar |
| 9933 |
/specification.quote |
| 9934 |
local separator=S(separator~="" and separator or ",") |
| 9935 |
local whatever=(dquotechar+(1-quotechar))^0 |
| 9936 |
local escaped=quotechar*(numbers and (whatever/tonumber) or Cs(whatever))*quotechar |
| 9937 |
local non_escaped=C((1-quotechar-newline-separator)^1) |
| 9938 |
local field=escaped+non_escaped+Cc("") |
| 9939 |
local record=Ct(field*(separator*field)^1) |
| 9940 |
local headerline=record*Cp() |
| 9941 |
local morerecords=(newline^(specification.strict and -1 or 1)*record)^0 |
| 9942 |
local headeryes=Ct(morerecords) |
| 9943 |
local headernop=Ct(record*morerecords) |
| 9944 |
return function(data,getheader) |
| 9945 |
if getheader then |
| 9946 |
local header,position=lpegmatch(headerline,data) |
| 9947 |
local data=lpegmatch(headeryes,data,position) |
| 9948 |
return data,header |
| 9949 |
else |
| 9950 |
return lpegmatch(headernop,data) |
| 9951 |
end |
| 9952 |
end |
| 9953 |
end |
| 9954 |
local function ranger(first,last,n,action) |
| 9955 |
if not first then |
| 9956 |
elseif last==true then |
| 9957 |
for i=first,n or first do |
| 9958 |
action(i) |
| 9959 |
end |
| 9960 |
elseif last then |
| 9961 |
for i=first,last do |
| 9962 |
action(i) |
| 9963 |
end |
| 9964 |
else |
| 9965 |
action(first) |
| 9966 |
end |
| 9967 |
end |
| 9968 |
local cardinal=(lpegpatterns.hexadecimal+lpegpatterns.cardinal)/tonumber |
| 9969 |
local spacers=lpegpatterns.spacer^0 |
| 9970 |
local endofstring=lpegpatterns.endofstring |
| 9971 |
local stepper=spacers*(cardinal*(spacers*S(":-")*spacers*(cardinal+Cc(true) )+Cc(false) )*Carg(1)*Carg(2)/ranger*S(", ")^0 )^1 |
| 9972 |
local stepper=spacers*(cardinal*(spacers*S(":-")*spacers*(cardinal+(P("*")+endofstring)*Cc(true) )+Cc(false) )*Carg(1)*Carg(2)/ranger*S(", ")^0 )^1*endofstring |
| 9973 |
function parsers.stepper(str,n,action) |
| 9974 |
local ts=type(str) |
| 9975 |
if type(n)=="function" then |
| 9976 |
if ts=="number" then |
| 9977 |
n(str) |
| 9978 |
elseif ts=="table" then |
| 9979 |
for i=1,#str do |
| 9980 |
n(str[i]) |
| 9981 |
end |
| 9982 |
else |
| 9983 |
lpegmatch(stepper,str,1,false,n or print) |
| 9984 |
end |
| 9985 |
elseif ts=="string" then |
| 9986 |
lpegmatch(stepper,str,1,n,action or print) |
| 9987 |
end |
| 9988 |
end |
| 9989 |
local pattern_math=Cs((P("%")/"\\percent "+P("^")*Cc("{")*lpegpatterns.integer*Cc("}")+anything)^0) |
| 9990 |
local pattern_text=Cs((P("%")/"\\percent "+(P("^")/"\\high")*Cc("{")*lpegpatterns.integer*Cc("}")+anything)^0) |
| 9991 |
patterns.unittotex=pattern |
| 9992 |
function parsers.unittotex(str,textmode) |
| 9993 |
return lpegmatch(textmode and pattern_text or pattern_math,str) |
| 9994 |
end |
| 9995 |
local pattern=Cs((P("^")/"<sup>"*lpegpatterns.integer*Cc("</sup>")+anything)^0) |
| 9996 |
function parsers.unittoxml(str) |
| 9997 |
return lpegmatch(pattern,str) |
| 9998 |
end |
| 9999 |
local cache={} |
| 10000 |
local spaces=lpegpatterns.space^0 |
| 10001 |
local dummy=function() end |
| 10002 |
setmetatableindex(cache,function(t,k) |
| 10003 |
local separator=S(k) |
| 10004 |
local value=(1-separator)^0 |
| 10005 |
local pattern=spaces*C(value)*separator^0*Cp() |
| 10006 |
t[k]=pattern |
| 10007 |
return pattern |
| 10008 |
end) |
| 10009 |
local commalistiterator=cache[","] |
| 10010 |
function parsers.iterator(str,separator) |
| 10011 |
local n=#str |
| 10012 |
if n==0 then |
| 10013 |
return dummy |
| 10014 |
else |
| 10015 |
local pattern=separator and cache[separator] or commalistiterator |
| 10016 |
local p=1 |
| 10017 |
return function() |
| 10018 |
if p<=n then |
| 10019 |
local s,e=lpegmatch(pattern,str,p) |
| 10020 |
if e then |
| 10021 |
p=e |
| 10022 |
return s |
| 10023 |
end |
| 10024 |
end |
| 10025 |
end |
| 10026 |
end |
| 10027 |
end |
| 10028 |
local function initialize(t,name) |
| 10029 |
local source=t[name] |
| 10030 |
if source then |
| 10031 |
local result={} |
| 10032 |
for k,v in next,t[name] do |
| 10033 |
result[k]=v |
| 10034 |
end |
| 10035 |
return result |
| 10036 |
else |
| 10037 |
return {} |
| 10038 |
end |
| 10039 |
end |
| 10040 |
local function fetch(t,name) |
| 10041 |
return t[name] or {} |
| 10042 |
end |
| 10043 |
local function process(result,more) |
| 10044 |
for k,v in next,more do |
| 10045 |
result[k]=v |
| 10046 |
end |
| 10047 |
return result |
| 10048 |
end |
| 10049 |
local name=C((1-S(", "))^1) |
| 10050 |
local parser=(Carg(1)*name/initialize)*(S(", ")^1*(Carg(1)*name/fetch))^0 |
| 10051 |
local merge=Cf(parser,process) |
| 10052 |
function parsers.mergehashes(hash,list) |
| 10053 |
return lpegmatch(merge,list,1,hash) |
| 10054 |
end |
| 10055 |
function parsers.runtime(time) |
| 10056 |
if not time then |
| 10057 |
time=os.runtime() |
| 10058 |
end |
| 10059 |
local days=div(time,24*60*60) |
| 10060 |
time=mod(time,24*60*60) |
| 10061 |
local hours=div(time,60*60) |
| 10062 |
time=mod(time,60*60) |
| 10063 |
local minutes=div(time,60) |
| 10064 |
local seconds=mod(time,60) |
| 10065 |
return days,hours,minutes,seconds |
| 10066 |
end |
| 10067 |
local spacing=whitespace^0 |
| 10068 |
local apply=P("->") |
| 10069 |
local method=C((1-apply)^1) |
| 10070 |
local token=lbrace*C((1-rbrace)^1)*rbrace+C(anything^1) |
| 10071 |
local pattern=spacing*(method*spacing*apply+Carg(1))*spacing*token |
| 10072 |
function parsers.splitmethod(str,default) |
| 10073 |
if str then |
| 10074 |
return lpegmatch(pattern,str,1,default or false) |
| 10075 |
else |
| 10076 |
return default or false,"" |
| 10077 |
end |
| 10078 |
end |
| 10079 |
local p_year=lpegpatterns.digit^4/tonumber |
| 10080 |
local pattern=Cf(Ct("")*( |
| 10081 |
(Cg(Cc("year")*p_year)*S("-/")*Cg(Cc("month")*cardinal)*S("-/")*Cg(Cc("day")*cardinal) |
| 10082 |
)+(Cg(Cc("day")*cardinal)*S("-/")*Cg(Cc("month")*cardinal)*S("-/")*Cg(Cc("year")*p_year) |
| 10083 |
)+(Cg(Cc("year")*p_year)*S("-/")*Cg(Cc("month")*cardinal) |
| 10084 |
)+(Cg(Cc("month")*cardinal)*S("-/")*Cg(Cc("year")*p_year) |
| 10085 |
) |
| 10086 |
)*( |
| 10087 |
P(" ")*Cg(Cc("hour")*cardinal)*P(":")*Cg(Cc("min")*cardinal)*(P(":")*Cg(Cc("sec")*cardinal))^-1+P(-1) ) |
| 10088 |
,rawset) |
| 10089 |
lpegpatterns.splittime=pattern |
| 10090 |
function parsers.totime(str) |
| 10091 |
return lpegmatch(pattern,str) |
| 10092 |
end |
| 10093 |
|
| 10094 |
|
| 10095 |
end -- of closure |
| 10096 |
|
| 10097 |
do -- create closure to overcome 200 locals limit |
| 10098 |
|
| 10099 |
package.loaded["util-fmt"] = package.loaded["util-fmt"] or true |
| 10100 |
|
| 10101 |
-- original size: 3379, stripped down to: 2273 |
| 10102 |
|
| 10103 |
if not modules then modules={} end modules ['util-fmt']={ |
| 10104 |
version=1.001, |
| 10105 |
comment="companion to luat-lib.mkiv", |
| 10106 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 10107 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 10108 |
license="see context related readme files" |
| 10109 |
} |
| 10110 |
utilities=utilities or {} |
| 10111 |
utilities.formatters=utilities.formatters or {} |
| 10112 |
local formatters=utilities.formatters |
| 10113 |
local concat,format=table.concat,string.format |
| 10114 |
local tostring,type,unpack=tostring,type,unpack |
| 10115 |
local strip=string.strip |
| 10116 |
local lpegmatch=lpeg.match |
| 10117 |
local stripper=lpeg.patterns.stripzeros |
| 10118 |
function formatters.stripzeros(str) |
| 10119 |
return lpegmatch(stripper,str) |
| 10120 |
end |
| 10121 |
function formatters.formatcolumns(result,between,header) |
| 10122 |
if result and #result>0 then |
| 10123 |
local widths={} |
| 10124 |
local numbers={} |
| 10125 |
local templates={} |
| 10126 |
local first=result[1] |
| 10127 |
local n=#first |
| 10128 |
between=between or " " |
| 10129 |
for i=1,n do |
| 10130 |
widths[i]=0 |
| 10131 |
end |
| 10132 |
for i=1,#result do |
| 10133 |
local r=result[i] |
| 10134 |
for j=1,n do |
| 10135 |
local rj=r[j] |
| 10136 |
local tj=type(rj) |
| 10137 |
if tj=="number" then |
| 10138 |
numbers[j]=true |
| 10139 |
rj=tostring(rj) |
| 10140 |
elseif tj~="string" then |
| 10141 |
rj=tostring(rj) |
| 10142 |
r[j]=rj |
| 10143 |
end |
| 10144 |
local w=#rj |
| 10145 |
if w>widths[j] then |
| 10146 |
widths[j]=w |
| 10147 |
end |
| 10148 |
end |
| 10149 |
end |
| 10150 |
if header then |
| 10151 |
for i=1,#header do |
| 10152 |
local h=header[i] |
| 10153 |
for j=1,n do |
| 10154 |
local hj=tostring(h[j]) |
| 10155 |
h[j]=hj |
| 10156 |
local w=#hj |
| 10157 |
if w>widths[j] then |
| 10158 |
widths[j]=w |
| 10159 |
end |
| 10160 |
end |
| 10161 |
end |
| 10162 |
end |
| 10163 |
for i=1,n do |
| 10164 |
local w=widths[i] |
| 10165 |
if numbers[i] then |
| 10166 |
if w>80 then |
| 10167 |
templates[i]="%s"..between |
| 10168 |
else |
| 10169 |
templates[i]="% "..w.."i"..between |
| 10170 |
end |
| 10171 |
else |
| 10172 |
if w>80 then |
| 10173 |
templates[i]="%s"..between |
| 10174 |
elseif w>0 then |
| 10175 |
templates[i]="%-"..w.."s"..between |
| 10176 |
else |
| 10177 |
templates[i]="%s" |
| 10178 |
end |
| 10179 |
end |
| 10180 |
end |
| 10181 |
local template=strip(concat(templates)) |
| 10182 |
for i=1,#result do |
| 10183 |
local str=format(template,unpack(result[i])) |
| 10184 |
result[i]=strip(str) |
| 10185 |
end |
| 10186 |
if header then |
| 10187 |
for i=1,n do |
| 10188 |
local w=widths[i] |
| 10189 |
if w>80 then |
| 10190 |
templates[i]="%s"..between |
| 10191 |
elseif w>0 then |
| 10192 |
templates[i]="%-"..w.."s"..between |
| 10193 |
else |
| 10194 |
templates[i]="%s" |
| 10195 |
end |
| 10196 |
end |
| 10197 |
local template=strip(concat(templates)) |
| 10198 |
for i=1,#header do |
| 10199 |
local str=format(template,unpack(header[i])) |
| 10200 |
header[i]=strip(str) |
| 10201 |
end |
| 10202 |
end |
| 10203 |
end |
| 10204 |
return result,header |
| 10205 |
end |
| 10206 |
|
| 10207 |
|
| 10208 |
end -- of closure |
| 10209 |
|
| 10210 |
do -- create closure to overcome 200 locals limit |
| 10211 |
|
| 10212 |
package.loaded["util-soc-imp-reset"] = package.loaded["util-soc-imp-reset"] or true |
| 10213 |
|
| 10214 |
-- original size: 374, stripped down to: 282 |
| 10215 |
|
| 10216 |
local loaded=package.loaded |
| 10217 |
loaded["socket"]=nil |
| 10218 |
loaded["copas"]=nil |
| 10219 |
loaded["ltn12"]=nil |
| 10220 |
loaded["mbox"]=nil |
| 10221 |
loaded["mime"]=nil |
| 10222 |
loaded["socket.url"]=nil |
| 10223 |
loaded["socket.headers"]=nil |
| 10224 |
loaded["socket.tp"]=nil |
| 10225 |
loaded["socket.http"]=nil |
| 10226 |
loaded["socket.ftp"]=nil |
| 10227 |
loaded["socket.smtp"]=nil |
| 10228 |
|
| 10229 |
|
| 10230 |
end -- of closure |
| 10231 |
|
| 10232 |
do -- create closure to overcome 200 locals limit |
| 10233 |
|
| 10234 |
package.loaded["util-soc-imp-socket"] = package.loaded["util-soc-imp-socket"] or true |
| 10235 |
|
| 10236 |
-- original size: 4905, stripped down to: 3562 |
| 10237 |
|
| 10238 |
|
| 10239 |
local type,tostring,setmetatable=type,tostring,setmetatable |
| 10240 |
local min=math.min |
| 10241 |
local format=string.format |
| 10242 |
local socket=socket or package.loaded.socket or require("socket.core") |
| 10243 |
local connect=socket.connect |
| 10244 |
local tcp4=socket.tcp4 |
| 10245 |
local tcp6=socket.tcp6 |
| 10246 |
local getaddrinfo=socket.dns.getaddrinfo |
| 10247 |
local defaulthost="0.0.0.0" |
| 10248 |
local function report(fmt,first,...) |
| 10249 |
if logs then |
| 10250 |
report=logs and logs.reporter("socket") |
| 10251 |
report(fmt,first,...) |
| 10252 |
elseif fmt then |
| 10253 |
fmt="socket: "..fmt |
| 10254 |
if first then |
| 10255 |
print(format(fmt,first,...)) |
| 10256 |
else |
| 10257 |
print(fmt) |
| 10258 |
end |
| 10259 |
end |
| 10260 |
end |
| 10261 |
socket.report=report |
| 10262 |
function socket.connect4(address,port,laddress,lport) |
| 10263 |
return connect(address,port,laddress,lport,"inet") |
| 10264 |
end |
| 10265 |
function socket.connect6(address,port,laddress,lport) |
| 10266 |
return connect(address,port,laddress,lport,"inet6") |
| 10267 |
end |
| 10268 |
function socket.bind(host,port,backlog) |
| 10269 |
if host=="*" or host=="" then |
| 10270 |
host=defaulthost |
| 10271 |
end |
| 10272 |
local addrinfo,err=getaddrinfo(host) |
| 10273 |
if not addrinfo then |
| 10274 |
return nil,err |
| 10275 |
end |
| 10276 |
for i=1,#addrinfo do |
| 10277 |
local alt=addrinfo[i] |
| 10278 |
local sock,err=(alt.family=="inet" and tcp4 or tcp6)() |
| 10279 |
if not sock then |
| 10280 |
return nil,err or "unknown error" |
| 10281 |
end |
| 10282 |
sock:setoption("reuseaddr",true) |
| 10283 |
local res,err=sock:bind(alt.addr,port) |
| 10284 |
if res then |
| 10285 |
res,err=sock:listen(backlog) |
| 10286 |
if res then |
| 10287 |
return sock |
| 10288 |
else |
| 10289 |
sock:close() |
| 10290 |
end |
| 10291 |
else |
| 10292 |
sock:close() |
| 10293 |
end |
| 10294 |
end |
| 10295 |
return nil,"invalid address" |
| 10296 |
end |
| 10297 |
socket.try=socket.newtry() |
| 10298 |
function socket.choose(list) |
| 10299 |
return function(name,opt1,opt2) |
| 10300 |
if type(name)~="string" then |
| 10301 |
name,opt1,opt2="default",name,opt1 |
| 10302 |
end |
| 10303 |
local f=list[name or "nil"] |
| 10304 |
if f then |
| 10305 |
return f(opt1,opt2) |
| 10306 |
else |
| 10307 |
report("error: unknown key '%s'",tostring(name)) |
| 10308 |
end |
| 10309 |
end |
| 10310 |
end |
| 10311 |
local sourcet={} |
| 10312 |
local sinkt={} |
| 10313 |
socket.sourcet=sourcet |
| 10314 |
socket.sinkt=sinkt |
| 10315 |
socket.BLOCKSIZE=2048 |
| 10316 |
sinkt["close-when-done"]=function(sock) |
| 10317 |
return setmetatable ( |
| 10318 |
{ |
| 10319 |
getfd=function() return sock:getfd() end, |
| 10320 |
dirty=function() return sock:dirty() end, |
| 10321 |
}, |
| 10322 |
{ |
| 10323 |
__call=function(self,chunk,err) |
| 10324 |
if chunk then |
| 10325 |
return sock:send(chunk) |
| 10326 |
else |
| 10327 |
sock:close() |
| 10328 |
return 1 |
| 10329 |
end |
| 10330 |
end |
| 10331 |
} |
| 10332 |
) |
| 10333 |
end |
| 10334 |
sinkt["keep-open"]=function(sock) |
| 10335 |
return setmetatable ( |
| 10336 |
{ |
| 10337 |
getfd=function() return sock:getfd() end, |
| 10338 |
dirty=function() return sock:dirty() end, |
| 10339 |
},{ |
| 10340 |
__call=function(self,chunk,err) |
| 10341 |
if chunk then |
| 10342 |
return sock:send(chunk) |
| 10343 |
else |
| 10344 |
return 1 |
| 10345 |
end |
| 10346 |
end |
| 10347 |
} |
| 10348 |
) |
| 10349 |
end |
| 10350 |
sinkt["default"]=sinkt["keep-open"] |
| 10351 |
socket.sink=socket.choose(sinkt) |
| 10352 |
sourcet["by-length"]=function(sock,length) |
| 10353 |
local blocksize=socket.BLOCKSIZE |
| 10354 |
return setmetatable ( |
| 10355 |
{ |
| 10356 |
getfd=function() return sock:getfd() end, |
| 10357 |
dirty=function() return sock:dirty() end, |
| 10358 |
}, |
| 10359 |
{ |
| 10360 |
__call=function() |
| 10361 |
if length<=0 then |
| 10362 |
return nil |
| 10363 |
end |
| 10364 |
local chunk,err=sock:receive(min(blocksize,length)) |
| 10365 |
if err then |
| 10366 |
return nil,err |
| 10367 |
end |
| 10368 |
length=length-#chunk |
| 10369 |
return chunk |
| 10370 |
end |
| 10371 |
} |
| 10372 |
) |
| 10373 |
end |
| 10374 |
sourcet["until-closed"]=function(sock) |
| 10375 |
local blocksize=socket.BLOCKSIZE |
| 10376 |
local done=false |
| 10377 |
return setmetatable ( |
| 10378 |
{ |
| 10379 |
getfd=function() return sock:getfd() end, |
| 10380 |
dirty=function() return sock:dirty() end, |
| 10381 |
},{ |
| 10382 |
__call=function() |
| 10383 |
if done then |
| 10384 |
return nil |
| 10385 |
end |
| 10386 |
local chunk,status,partial=sock:receive(blocksize) |
| 10387 |
if not status then |
| 10388 |
return chunk |
| 10389 |
elseif status=="closed" then |
| 10390 |
sock:close() |
| 10391 |
done=true |
| 10392 |
return partial |
| 10393 |
else |
| 10394 |
return nil,status |
| 10395 |
end |
| 10396 |
end |
| 10397 |
} |
| 10398 |
) |
| 10399 |
end |
| 10400 |
sourcet["default"]=sourcet["until-closed"] |
| 10401 |
socket.source=socket.choose(sourcet) |
| 10402 |
_G.socket=socket |
| 10403 |
package.loaded["socket"]=socket |
| 10404 |
|
| 10405 |
|
| 10406 |
end -- of closure |
| 10407 |
|
| 10408 |
do -- create closure to overcome 200 locals limit |
| 10409 |
|
| 10410 |
package.loaded["util-soc-imp-copas"] = package.loaded["util-soc-imp-copas"] or true |
| 10411 |
|
| 10412 |
-- original size: 26186, stripped down to: 14893 |
| 10413 |
|
| 10414 |
|
| 10415 |
local socket=socket or require("socket") |
| 10416 |
local ssl=ssl or nil |
| 10417 |
local WATCH_DOG_TIMEOUT=120 |
| 10418 |
local UDP_DATAGRAM_MAX=8192 |
| 10419 |
local type,next,pcall,getmetatable,tostring=type,next,pcall,getmetatable,tostring |
| 10420 |
local min,max,random=math.min,math.max,math.random |
| 10421 |
local find=string.find |
| 10422 |
local insert,remove=table.insert,table.remove |
| 10423 |
local gettime=socket.gettime |
| 10424 |
local selectsocket=socket.select |
| 10425 |
local createcoroutine=coroutine.create |
| 10426 |
local resumecoroutine=coroutine.resume |
| 10427 |
local yieldcoroutine=coroutine.yield |
| 10428 |
local runningcoroutine=coroutine.running |
| 10429 |
local function report(fmt,first,...) |
| 10430 |
if logs then |
| 10431 |
report=logs and logs.reporter("copas") |
| 10432 |
report(fmt,first,...) |
| 10433 |
elseif fmt then |
| 10434 |
fmt="copas: "..fmt |
| 10435 |
if first then |
| 10436 |
print(format(fmt,first,...)) |
| 10437 |
else |
| 10438 |
print(fmt) |
| 10439 |
end |
| 10440 |
end |
| 10441 |
end |
| 10442 |
local copas={ |
| 10443 |
_COPYRIGHT="Copyright (C) 2005-2016 Kepler Project", |
| 10444 |
_DESCRIPTION="Coroutine Oriented Portable Asynchronous Services", |
| 10445 |
_VERSION="Copas 2.0.1", |
| 10446 |
autoclose=true, |
| 10447 |
running=false, |
| 10448 |
report=report, |
| 10449 |
trace=false, |
| 10450 |
} |
| 10451 |
local function statushandler(status,...) |
| 10452 |
if status then |
| 10453 |
return... |
| 10454 |
end |
| 10455 |
local err=(...) |
| 10456 |
if type(err)=="table" then |
| 10457 |
err=err[1] |
| 10458 |
end |
| 10459 |
if copas.trace then |
| 10460 |
report("error: %s",tostring(err)) |
| 10461 |
end |
| 10462 |
return nil,err |
| 10463 |
end |
| 10464 |
function socket.protect(func) |
| 10465 |
return function(...) |
| 10466 |
return statushandler(pcall(func,...)) |
| 10467 |
end |
| 10468 |
end |
| 10469 |
function socket.newtry(finalizer) |
| 10470 |
return function (...) |
| 10471 |
local status=(...) |
| 10472 |
if not status then |
| 10473 |
local detail=select(2,...) |
| 10474 |
pcall(finalizer,detail) |
| 10475 |
if copas.trace then |
| 10476 |
report("error: %s",tostring(detail)) |
| 10477 |
end |
| 10478 |
return |
| 10479 |
end |
| 10480 |
return... |
| 10481 |
end |
| 10482 |
end |
| 10483 |
local function newset() |
| 10484 |
local reverse={} |
| 10485 |
local set={} |
| 10486 |
local queue={} |
| 10487 |
setmetatable(set,{ |
| 10488 |
__index={ |
| 10489 |
insert=function(set,value) |
| 10490 |
if not reverse[value] then |
| 10491 |
local n=#set+1 |
| 10492 |
set[n]=value |
| 10493 |
reverse[value]=n |
| 10494 |
end |
| 10495 |
end, |
| 10496 |
remove=function(set,value) |
| 10497 |
local index=reverse[value] |
| 10498 |
if index then |
| 10499 |
reverse[value]=nil |
| 10500 |
local n=#set |
| 10501 |
local top=set[n] |
| 10502 |
set[n]=nil |
| 10503 |
if top~=value then |
| 10504 |
reverse[top]=index |
| 10505 |
set[index]=top |
| 10506 |
end |
| 10507 |
end |
| 10508 |
end, |
| 10509 |
push=function (set,key,itm) |
| 10510 |
local entry=queue[key] |
| 10511 |
if entry==nil then |
| 10512 |
queue[key]={ itm } |
| 10513 |
else |
| 10514 |
entry[#entry+1]=itm |
| 10515 |
end |
| 10516 |
end, |
| 10517 |
pop=function (set,key) |
| 10518 |
local top=queue[key] |
| 10519 |
if top~=nil then |
| 10520 |
local ret=remove(top,1) |
| 10521 |
if top[1]==nil then |
| 10522 |
queue[key]=nil |
| 10523 |
end |
| 10524 |
return ret |
| 10525 |
end |
| 10526 |
end |
| 10527 |
} |
| 10528 |
} ) |
| 10529 |
return set |
| 10530 |
end |
| 10531 |
local _sleeping={ |
| 10532 |
times={}, |
| 10533 |
cos={}, |
| 10534 |
lethargy={}, |
| 10535 |
insert=function() |
| 10536 |
end, |
| 10537 |
remove=function() |
| 10538 |
end, |
| 10539 |
push=function(self,sleeptime,co) |
| 10540 |
if not co then |
| 10541 |
return |
| 10542 |
end |
| 10543 |
if sleeptime<0 then |
| 10544 |
self.lethargy[co]=true |
| 10545 |
return |
| 10546 |
else |
| 10547 |
sleeptime=gettime()+sleeptime |
| 10548 |
end |
| 10549 |
local t=self.times |
| 10550 |
local c=self.cos |
| 10551 |
local i=1 |
| 10552 |
local n=#t |
| 10553 |
while i<=n and t[i]<=sleeptime do |
| 10554 |
i=i+1 |
| 10555 |
end |
| 10556 |
insert(t,i,sleeptime) |
| 10557 |
insert(c,i,co) |
| 10558 |
end, |
| 10559 |
getnext= |
| 10560 |
function(self) |
| 10561 |
local t=self.times |
| 10562 |
local delay=t[1] and t[1]-gettime() or nil |
| 10563 |
return delay and max(delay,0) or nil |
| 10564 |
end, |
| 10565 |
pop= |
| 10566 |
function(self,time) |
| 10567 |
local t=self.times |
| 10568 |
local c=self.cos |
| 10569 |
if #t==0 or time<t[1] then |
| 10570 |
return |
| 10571 |
end |
| 10572 |
local co=c[1] |
| 10573 |
remove(t,1) |
| 10574 |
remove(c,1) |
| 10575 |
return co |
| 10576 |
end, |
| 10577 |
wakeup=function(self,co) |
| 10578 |
local let=self.lethargy |
| 10579 |
if let[co] then |
| 10580 |
self:push(0,co) |
| 10581 |
let[co]=nil |
| 10582 |
else |
| 10583 |
local c=self.cos |
| 10584 |
local t=self.times |
| 10585 |
for i=1,#c do |
| 10586 |
if c[i]==co then |
| 10587 |
remove(c,i) |
| 10588 |
remove(t,i) |
| 10589 |
self:push(0,co) |
| 10590 |
return |
| 10591 |
end |
| 10592 |
end |
| 10593 |
end |
| 10594 |
end |
| 10595 |
} |
| 10596 |
local _servers=newset() |
| 10597 |
local _reading=newset() |
| 10598 |
local _writing=newset() |
| 10599 |
local _reading_log={} |
| 10600 |
local _writing_log={} |
| 10601 |
local _is_timeout={ |
| 10602 |
timeout=true, |
| 10603 |
wantread=true, |
| 10604 |
wantwrite=true, |
| 10605 |
} |
| 10606 |
local function isTCP(socket) |
| 10607 |
return not find(tostring(socket),"^udp") |
| 10608 |
end |
| 10609 |
local function copasreceive(client,pattern,part) |
| 10610 |
if not pattern or pattern=="" then |
| 10611 |
pattern="*l" |
| 10612 |
end |
| 10613 |
local current_log=_reading_log |
| 10614 |
local s,err |
| 10615 |
repeat |
| 10616 |
s,err,part=client:receive(pattern,part) |
| 10617 |
if s or (not _is_timeout[err]) then |
| 10618 |
current_log[client]=nil |
| 10619 |
return s,err,part |
| 10620 |
end |
| 10621 |
if err=="wantwrite" then |
| 10622 |
current_log=_writing_log |
| 10623 |
current_log[client]=gettime() |
| 10624 |
yieldcoroutine(client,_writing) |
| 10625 |
else |
| 10626 |
current_log=_reading_log |
| 10627 |
current_log[client]=gettime() |
| 10628 |
yieldcoroutine(client,_reading) |
| 10629 |
end |
| 10630 |
until false |
| 10631 |
end |
| 10632 |
local function copasreceivefrom(client,size) |
| 10633 |
local s,err,port |
| 10634 |
if not size or size==0 then |
| 10635 |
size=UDP_DATAGRAM_MAX |
| 10636 |
end |
| 10637 |
repeat |
| 10638 |
s,err,port=client:receivefrom(size) |
| 10639 |
if s or err~="timeout" then |
| 10640 |
_reading_log[client]=nil |
| 10641 |
return s,err,port |
| 10642 |
end |
| 10643 |
_reading_log[client]=gettime() |
| 10644 |
yieldcoroutine(client,_reading) |
| 10645 |
until false |
| 10646 |
end |
| 10647 |
local function copasreceivepartial(client,pattern,part) |
| 10648 |
if not pattern or pattern=="" then |
| 10649 |
pattern="*l" |
| 10650 |
end |
| 10651 |
local logger=_reading_log |
| 10652 |
local queue=_reading |
| 10653 |
local s,err |
| 10654 |
repeat |
| 10655 |
s,err,part=client:receive(pattern,part) |
| 10656 |
if s or (type(pattern)=="number" and part~="" and part) or not _is_timeout[err] then |
| 10657 |
logger[client]=nil |
| 10658 |
return s,err,part |
| 10659 |
end |
| 10660 |
if err=="wantwrite" then |
| 10661 |
logger=_writing_log |
| 10662 |
queue=_writing |
| 10663 |
else |
| 10664 |
logger=_reading_log |
| 10665 |
queue=_reading |
| 10666 |
end |
| 10667 |
logger[client]=gettime() |
| 10668 |
yieldcoroutine(client,queue) |
| 10669 |
until false |
| 10670 |
end |
| 10671 |
local function copassend(client,data,from,to) |
| 10672 |
if not from then |
| 10673 |
from=1 |
| 10674 |
end |
| 10675 |
local lastIndex=from-1 |
| 10676 |
local logger=_writing_log |
| 10677 |
local queue=_writing |
| 10678 |
local s,err |
| 10679 |
repeat |
| 10680 |
s,err,lastIndex=client:send(data,lastIndex+1,to) |
| 10681 |
if random(100)>90 then |
| 10682 |
logger[client]=gettime() |
| 10683 |
yieldcoroutine(client,queue) |
| 10684 |
end |
| 10685 |
if s or not _is_timeout[err] then |
| 10686 |
logger[client]=nil |
| 10687 |
return s,err,lastIndex |
| 10688 |
end |
| 10689 |
if err=="wantread" then |
| 10690 |
logger=_reading_log |
| 10691 |
queue=_reading |
| 10692 |
else |
| 10693 |
logger=_writing_log |
| 10694 |
queue=_writing |
| 10695 |
end |
| 10696 |
logger[client]=gettime() |
| 10697 |
yieldcoroutine(client,queue) |
| 10698 |
until false |
| 10699 |
end |
| 10700 |
local function copassendto(client,data,ip,port) |
| 10701 |
repeat |
| 10702 |
local s,err=client:sendto(data,ip,port) |
| 10703 |
if random(100)>90 then |
| 10704 |
_writing_log[client]=gettime() |
| 10705 |
yieldcoroutine(client,_writing) |
| 10706 |
end |
| 10707 |
if s or err~="timeout" then |
| 10708 |
_writing_log[client]=nil |
| 10709 |
return s,err |
| 10710 |
end |
| 10711 |
_writing_log[client]=gettime() |
| 10712 |
yieldcoroutine(client,_writing) |
| 10713 |
until false |
| 10714 |
end |
| 10715 |
local function copasconnect(skt,host,port) |
| 10716 |
skt:settimeout(0) |
| 10717 |
local ret,err,tried_more_than_once |
| 10718 |
repeat |
| 10719 |
ret,err=skt:connect (host,port) |
| 10720 |
if ret or (err~="timeout" and err~="Operation already in progress") then |
| 10721 |
if not ret and err=="already connected" and tried_more_than_once then |
| 10722 |
ret=1 |
| 10723 |
err=nil |
| 10724 |
end |
| 10725 |
_writing_log[skt]=nil |
| 10726 |
return ret,err |
| 10727 |
end |
| 10728 |
tried_more_than_once=tried_more_than_once or true |
| 10729 |
_writing_log[skt]=gettime() |
| 10730 |
yieldcoroutine(skt,_writing) |
| 10731 |
until false |
| 10732 |
end |
| 10733 |
local function copasdohandshake(skt,sslt) |
| 10734 |
if not ssl then |
| 10735 |
ssl=require("ssl") |
| 10736 |
end |
| 10737 |
if not ssl then |
| 10738 |
report("error: no ssl library") |
| 10739 |
return |
| 10740 |
end |
| 10741 |
local nskt,err=ssl.wrap(skt,sslt) |
| 10742 |
if not nskt then |
| 10743 |
report("error: %s",tostring(err)) |
| 10744 |
return |
| 10745 |
end |
| 10746 |
nskt:settimeout(0) |
| 10747 |
local queue |
| 10748 |
repeat |
| 10749 |
local success,err=nskt:dohandshake() |
| 10750 |
if success then |
| 10751 |
return nskt |
| 10752 |
elseif err=="wantwrite" then |
| 10753 |
queue=_writing |
| 10754 |
elseif err=="wantread" then |
| 10755 |
queue=_reading |
| 10756 |
else |
| 10757 |
report("error: %s",tostring(err)) |
| 10758 |
return |
| 10759 |
end |
| 10760 |
yieldcoroutine(nskt,queue) |
| 10761 |
until false |
| 10762 |
end |
| 10763 |
local function copasflush(client) |
| 10764 |
end |
| 10765 |
copas.connect=copassconnect |
| 10766 |
copas.send=copassend |
| 10767 |
copas.sendto=copassendto |
| 10768 |
copas.receive=copasreceive |
| 10769 |
copas.receivefrom=copasreceivefrom |
| 10770 |
copas.copasreceivepartial=copasreceivepartial |
| 10771 |
copas.copasreceivePartial=copasreceivepartial |
| 10772 |
copas.dohandshake=copasdohandshake |
| 10773 |
copas.flush=copasflush |
| 10774 |
local function _skt_mt_tostring(self) |
| 10775 |
return tostring(self.socket).." (copas wrapped)" |
| 10776 |
end |
| 10777 |
local _skt_mt_tcp_index={ |
| 10778 |
send=function(self,data,from,to) |
| 10779 |
return copassend (self.socket,data,from,to) |
| 10780 |
end, |
| 10781 |
receive=function (self,pattern,prefix) |
| 10782 |
if self.timeout==0 then |
| 10783 |
return copasreceivePartial(self.socket,pattern,prefix) |
| 10784 |
else |
| 10785 |
return copasreceive(self.socket,pattern,prefix) |
| 10786 |
end |
| 10787 |
end, |
| 10788 |
flush=function (self) |
| 10789 |
return copasflush(self.socket) |
| 10790 |
end, |
| 10791 |
settimeout=function (self,time) |
| 10792 |
self.timeout=time |
| 10793 |
return true |
| 10794 |
end, |
| 10795 |
connect=function(self,...) |
| 10796 |
local res,err=copasconnect(self.socket,...) |
| 10797 |
if res and self.ssl_params then |
| 10798 |
res,err=self:dohandshake() |
| 10799 |
end |
| 10800 |
return res,err |
| 10801 |
end, |
| 10802 |
close=function(self,...) |
| 10803 |
return self.socket:close(...) |
| 10804 |
end, |
| 10805 |
bind=function(self,...) |
| 10806 |
return self.socket:bind(...) |
| 10807 |
end, |
| 10808 |
getsockname=function(self,...) |
| 10809 |
return self.socket:getsockname(...) |
| 10810 |
end, |
| 10811 |
getstats=function(self,...) |
| 10812 |
return self.socket:getstats(...) |
| 10813 |
end, |
| 10814 |
setstats=function(self,...) |
| 10815 |
return self.socket:setstats(...) |
| 10816 |
end, |
| 10817 |
listen=function(self,...) |
| 10818 |
return self.socket:listen(...) |
| 10819 |
end, |
| 10820 |
accept=function(self,...) |
| 10821 |
return self.socket:accept(...) |
| 10822 |
end, |
| 10823 |
setoption=function(self,...) |
| 10824 |
return self.socket:setoption(...) |
| 10825 |
end, |
| 10826 |
getpeername=function(self,...) |
| 10827 |
return self.socket:getpeername(...) |
| 10828 |
end, |
| 10829 |
shutdown=function(self,...) |
| 10830 |
return self.socket:shutdown(...) |
| 10831 |
end, |
| 10832 |
dohandshake=function(self,sslt) |
| 10833 |
self.ssl_params=sslt or self.ssl_params |
| 10834 |
local nskt,err=copasdohandshake(self.socket,self.ssl_params) |
| 10835 |
if not nskt then |
| 10836 |
return nskt,err |
| 10837 |
end |
| 10838 |
self.socket=nskt |
| 10839 |
return self |
| 10840 |
end, |
| 10841 |
} |
| 10842 |
local _skt_mt_tcp={ |
| 10843 |
__tostring=_skt_mt_tostring, |
| 10844 |
__index=_skt_mt_tcp_index, |
| 10845 |
} |
| 10846 |
local _skt_mt_udp_index={ |
| 10847 |
sendto=function (self,...) |
| 10848 |
return copassendto(self.socket,...) |
| 10849 |
end, |
| 10850 |
receive=function (self,size) |
| 10851 |
return copasreceive(self.socket,size or UDP_DATAGRAM_MAX) |
| 10852 |
end, |
| 10853 |
receivefrom=function (self,size) |
| 10854 |
return copasreceivefrom(self.socket,size or UDP_DATAGRAM_MAX) |
| 10855 |
end, |
| 10856 |
setpeername=function(self,...) |
| 10857 |
return self.socket:getpeername(...) |
| 10858 |
end, |
| 10859 |
setsockname=function(self,...) |
| 10860 |
return self.socket:setsockname(...) |
| 10861 |
end, |
| 10862 |
close=function(self,...) |
| 10863 |
return true |
| 10864 |
end |
| 10865 |
} |
| 10866 |
local _skt_mt_udp={ |
| 10867 |
__tostring=_skt_mt_tostring, |
| 10868 |
__index=_skt_mt_udp_index, |
| 10869 |
} |
| 10870 |
for k,v in next,_skt_mt_tcp_index do |
| 10871 |
if not _skt_mt_udp_index[k] then |
| 10872 |
_skt_mt_udp_index[k]=v |
| 10873 |
end |
| 10874 |
end |
| 10875 |
local function wrap(skt,sslt) |
| 10876 |
if getmetatable(skt)==_skt_mt_tcp or getmetatable(skt)==_skt_mt_udp then |
| 10877 |
return skt |
| 10878 |
end |
| 10879 |
skt:settimeout(0) |
| 10880 |
if isTCP(skt) then |
| 10881 |
return setmetatable ({ socket=skt,ssl_params=sslt },_skt_mt_tcp) |
| 10882 |
else |
| 10883 |
return setmetatable ({ socket=skt },_skt_mt_udp) |
| 10884 |
end |
| 10885 |
end |
| 10886 |
copas.wrap=wrap |
| 10887 |
function copas.handler(handler,sslparams) |
| 10888 |
return function (skt,...) |
| 10889 |
skt=wrap(skt) |
| 10890 |
if sslparams then |
| 10891 |
skt:dohandshake(sslparams) |
| 10892 |
end |
| 10893 |
return handler(skt,...) |
| 10894 |
end |
| 10895 |
end |
| 10896 |
local _errhandlers={} |
| 10897 |
function copas.setErrorHandler(err) |
| 10898 |
local co=runningcoroutine() |
| 10899 |
if co then |
| 10900 |
_errhandlers[co]=err |
| 10901 |
end |
| 10902 |
end |
| 10903 |
local function _deferror (msg,co,skt) |
| 10904 |
report("%s (%s) (%s)",msg,tostring(co),tostring(skt)) |
| 10905 |
end |
| 10906 |
local function _doTick (co,skt,...) |
| 10907 |
if not co then |
| 10908 |
return |
| 10909 |
end |
| 10910 |
local ok,res,new_q=resumecoroutine(co,skt,...) |
| 10911 |
if ok and res and new_q then |
| 10912 |
new_q:insert(res) |
| 10913 |
new_q:push(res,co) |
| 10914 |
else |
| 10915 |
if not ok then |
| 10916 |
pcall(_errhandlers[co] or _deferror,res,co,skt) |
| 10917 |
end |
| 10918 |
if skt and copas.autoclose and isTCP(skt) then |
| 10919 |
skt:close() |
| 10920 |
end |
| 10921 |
_errhandlers[co]=nil |
| 10922 |
end |
| 10923 |
end |
| 10924 |
local function _accept(input,handler) |
| 10925 |
local client=input:accept() |
| 10926 |
if client then |
| 10927 |
client:settimeout(0) |
| 10928 |
local co=createcoroutine(handler) |
| 10929 |
_doTick (co,client) |
| 10930 |
end |
| 10931 |
return client |
| 10932 |
end |
| 10933 |
local function _tickRead(skt) |
| 10934 |
_doTick(_reading:pop(skt),skt) |
| 10935 |
end |
| 10936 |
local function _tickWrite(skt) |
| 10937 |
_doTick(_writing:pop(skt),skt) |
| 10938 |
end |
| 10939 |
local function addTCPserver(server,handler,timeout) |
| 10940 |
server:settimeout(timeout or 0) |
| 10941 |
_servers[server]=handler |
| 10942 |
_reading:insert(server) |
| 10943 |
end |
| 10944 |
local function addUDPserver(server,handler,timeout) |
| 10945 |
server:settimeout(timeout or 0) |
| 10946 |
local co=createcoroutine(handler) |
| 10947 |
_reading:insert(server) |
| 10948 |
_doTick(co,server) |
| 10949 |
end |
| 10950 |
function copas.addserver(server,handler,timeout) |
| 10951 |
if isTCP(server) then |
| 10952 |
addTCPserver(server,handler,timeout) |
| 10953 |
else |
| 10954 |
addUDPserver(server,handler,timeout) |
| 10955 |
end |
| 10956 |
end |
| 10957 |
function copas.removeserver(server,keep_open) |
| 10958 |
local s=server |
| 10959 |
local mt=getmetatable(server) |
| 10960 |
if mt==_skt_mt_tcp or mt==_skt_mt_udp then |
| 10961 |
s=server.socket |
| 10962 |
end |
| 10963 |
_servers[s]=nil |
| 10964 |
_reading:remove(s) |
| 10965 |
if keep_open then |
| 10966 |
return true |
| 10967 |
end |
| 10968 |
return server:close() |
| 10969 |
end |
| 10970 |
function copas.addthread(handler,...) |
| 10971 |
local thread=createcoroutine(function(_,...) return handler(...) end) |
| 10972 |
_doTick(thread,nil,...) |
| 10973 |
return thread |
| 10974 |
end |
| 10975 |
local _tasks={} |
| 10976 |
local function addtaskRead(task) |
| 10977 |
task.def_tick=_tickRead |
| 10978 |
_tasks[task]=true |
| 10979 |
end |
| 10980 |
local function addtaskWrite(task) |
| 10981 |
task.def_tick=_tickWrite |
| 10982 |
_tasks[task]=true |
| 10983 |
end |
| 10984 |
local function tasks() |
| 10985 |
return next,_tasks |
| 10986 |
end |
| 10987 |
local _readable_t={ |
| 10988 |
events=function(self) |
| 10989 |
local i=0 |
| 10990 |
return function () |
| 10991 |
i=i+1 |
| 10992 |
return self._evs[i] |
| 10993 |
end |
| 10994 |
end, |
| 10995 |
tick=function(self,input) |
| 10996 |
local handler=_servers[input] |
| 10997 |
if handler then |
| 10998 |
input=_accept(input,handler) |
| 10999 |
else |
| 11000 |
_reading:remove(input) |
| 11001 |
self.def_tick(input) |
| 11002 |
end |
| 11003 |
end |
| 11004 |
} |
| 11005 |
addtaskRead(_readable_t) |
| 11006 |
local _writable_t={ |
| 11007 |
events=function(self) |
| 11008 |
local i=0 |
| 11009 |
return function() |
| 11010 |
i=i+1 |
| 11011 |
return self._evs[i] |
| 11012 |
end |
| 11013 |
end, |
| 11014 |
tick=function(self,output) |
| 11015 |
_writing:remove(output) |
| 11016 |
self.def_tick(output) |
| 11017 |
end |
| 11018 |
} |
| 11019 |
addtaskWrite(_writable_t) |
| 11020 |
local _sleeping_t={ |
| 11021 |
tick=function(self,time,...) |
| 11022 |
_doTick(_sleeping:pop(time),...) |
| 11023 |
end |
| 11024 |
} |
| 11025 |
function copas.sleep(sleeptime) |
| 11026 |
yieldcoroutine((sleeptime or 0),_sleeping) |
| 11027 |
end |
| 11028 |
function copas.wakeup(co) |
| 11029 |
_sleeping:wakeup(co) |
| 11030 |
end |
| 11031 |
local last_cleansing=0 |
| 11032 |
local function _select(timeout) |
| 11033 |
local now=gettime() |
| 11034 |
local r_evs,w_evs,err=selectsocket(_reading,_writing,timeout) |
| 11035 |
_readable_t._evs=r_evs |
| 11036 |
_writable_t._evs=w_evs |
| 11037 |
if (last_cleansing-now)>WATCH_DOG_TIMEOUT then |
| 11038 |
last_cleansing=now |
| 11039 |
for skt,time in next,_reading_log do |
| 11040 |
if not r_evs[skt] and (time-now)>WATCH_DOG_TIMEOUT then |
| 11041 |
local n=#r_evs+1 |
| 11042 |
_reading_log[skt]=nil |
| 11043 |
r_evs[n]=skt |
| 11044 |
r_evs[skt]=n |
| 11045 |
end |
| 11046 |
end |
| 11047 |
for skt,time in next,_writing_log do |
| 11048 |
if not w_evs[skt] and (time-now)>WATCH_DOG_TIMEOUT then |
| 11049 |
local n=#w_evs+1 |
| 11050 |
_writing_log[skt]=nil |
| 11051 |
w_evs[n]=skt |
| 11052 |
w_evs[skt]=n |
| 11053 |
end |
| 11054 |
end |
| 11055 |
end |
| 11056 |
if err=="timeout" and #r_evs+#w_evs>0 then |
| 11057 |
return nil |
| 11058 |
else |
| 11059 |
return err |
| 11060 |
end |
| 11061 |
end |
| 11062 |
local function copasfinished() |
| 11063 |
return not (next(_reading) or next(_writing) or _sleeping:getnext()) |
| 11064 |
end |
| 11065 |
local function copasstep(timeout) |
| 11066 |
_sleeping_t:tick(gettime()) |
| 11067 |
local nextwait=_sleeping:getnext() |
| 11068 |
if nextwait then |
| 11069 |
timeout=timeout and min(nextwait,timeout) or nextwait |
| 11070 |
elseif copasfinished() then |
| 11071 |
return false |
| 11072 |
end |
| 11073 |
local err=_select(timeout) |
| 11074 |
if err then |
| 11075 |
if err=="timeout" then |
| 11076 |
return false |
| 11077 |
end |
| 11078 |
return nil,err |
| 11079 |
end |
| 11080 |
for task in tasks() do |
| 11081 |
for event in task:events() do |
| 11082 |
task:tick(event) |
| 11083 |
end |
| 11084 |
end |
| 11085 |
return true |
| 11086 |
end |
| 11087 |
copas.finished=copasfinished |
| 11088 |
copas.step=copasstep |
| 11089 |
function copas.loop(timeout) |
| 11090 |
copas.running=true |
| 11091 |
while not copasfinished() do |
| 11092 |
copasstep(timeout) |
| 11093 |
end |
| 11094 |
copas.running=false |
| 11095 |
end |
| 11096 |
package.loaded["copas"]=copas |
| 11097 |
|
| 11098 |
|
| 11099 |
end -- of closure |
| 11100 |
|
| 11101 |
do -- create closure to overcome 200 locals limit |
| 11102 |
|
| 11103 |
package.loaded["util-soc-imp-ltn12"] = package.loaded["util-soc-imp-ltn12"] or true |
| 11104 |
|
| 11105 |
-- original size: 8709, stripped down to: 5411 |
| 11106 |
|
| 11107 |
|
| 11108 |
local select,unpack=select,unpack |
| 11109 |
local insert,remove=table.insert,table.remove |
| 11110 |
local sub=string.sub |
| 11111 |
local function report(fmt,first,...) |
| 11112 |
if logs then |
| 11113 |
report=logs and logs.reporter("ltn12") |
| 11114 |
report(fmt,first,...) |
| 11115 |
elseif fmt then |
| 11116 |
fmt="ltn12: "..fmt |
| 11117 |
if first then |
| 11118 |
print(format(fmt,first,...)) |
| 11119 |
else |
| 11120 |
print(fmt) |
| 11121 |
end |
| 11122 |
end |
| 11123 |
end |
| 11124 |
local filter={} |
| 11125 |
local source={} |
| 11126 |
local sink={} |
| 11127 |
local pump={} |
| 11128 |
local ltn12={ |
| 11129 |
_VERSION="LTN12 1.0.3", |
| 11130 |
BLOCKSIZE=2048, |
| 11131 |
filter=filter, |
| 11132 |
source=source, |
| 11133 |
sink=sink, |
| 11134 |
pump=pump, |
| 11135 |
report=report, |
| 11136 |
} |
| 11137 |
function filter.cycle(low,ctx,extra) |
| 11138 |
if low then |
| 11139 |
return function(chunk) |
| 11140 |
return (low(ctx,chunk,extra)) |
| 11141 |
end |
| 11142 |
end |
| 11143 |
end |
| 11144 |
function filter.chain(...) |
| 11145 |
local arg={... } |
| 11146 |
local n=select('#',...) |
| 11147 |
local top=1 |
| 11148 |
local index=1 |
| 11149 |
local retry="" |
| 11150 |
return function(chunk) |
| 11151 |
retry=chunk and retry |
| 11152 |
while true do |
| 11153 |
local action=arg[index] |
| 11154 |
if index==top then |
| 11155 |
chunk=action(chunk) |
| 11156 |
if chunk=="" or top==n then |
| 11157 |
return chunk |
| 11158 |
elseif chunk then |
| 11159 |
index=index+1 |
| 11160 |
else |
| 11161 |
top=top+1 |
| 11162 |
index=top |
| 11163 |
end |
| 11164 |
else |
| 11165 |
chunk=action(chunk or "") |
| 11166 |
if chunk=="" then |
| 11167 |
index=index-1 |
| 11168 |
chunk=retry |
| 11169 |
elseif chunk then |
| 11170 |
if index==n then |
| 11171 |
return chunk |
| 11172 |
else |
| 11173 |
index=index+1 |
| 11174 |
end |
| 11175 |
else |
| 11176 |
report("error: filter returned inappropriate 'nil'") |
| 11177 |
return |
| 11178 |
end |
| 11179 |
end |
| 11180 |
end |
| 11181 |
end |
| 11182 |
end |
| 11183 |
local function empty() |
| 11184 |
return nil |
| 11185 |
end |
| 11186 |
function source.empty() |
| 11187 |
return empty |
| 11188 |
end |
| 11189 |
local function sourceerror(err) |
| 11190 |
return function() |
| 11191 |
return nil,err |
| 11192 |
end |
| 11193 |
end |
| 11194 |
source.error=sourceerror |
| 11195 |
function source.file(handle,io_err) |
| 11196 |
if handle then |
| 11197 |
local blocksize=ltn12.BLOCKSIZE |
| 11198 |
return function() |
| 11199 |
local chunk=handle:read(blocksize) |
| 11200 |
if not chunk then |
| 11201 |
handle:close() |
| 11202 |
end |
| 11203 |
return chunk |
| 11204 |
end |
| 11205 |
else |
| 11206 |
return sourceerror(io_err or "unable to open file") |
| 11207 |
end |
| 11208 |
end |
| 11209 |
function source.simplify(src) |
| 11210 |
return function() |
| 11211 |
local chunk,err_or_new=src() |
| 11212 |
if err_or_new then |
| 11213 |
src=err_or_new |
| 11214 |
end |
| 11215 |
if chunk then |
| 11216 |
return chunk |
| 11217 |
else |
| 11218 |
return nil,err_or_new |
| 11219 |
end |
| 11220 |
end |
| 11221 |
end |
| 11222 |
function source.string(s) |
| 11223 |
if s then |
| 11224 |
local blocksize=ltn12.BLOCKSIZE |
| 11225 |
local i=1 |
| 11226 |
return function() |
| 11227 |
local nexti=i+blocksize |
| 11228 |
local chunk=sub(s,i,nexti-1) |
| 11229 |
i=nexti |
| 11230 |
if chunk~="" then |
| 11231 |
return chunk |
| 11232 |
else |
| 11233 |
return nil |
| 11234 |
end |
| 11235 |
end |
| 11236 |
else return source.empty() end |
| 11237 |
end |
| 11238 |
function source.rewind(src) |
| 11239 |
local t={} |
| 11240 |
return function(chunk) |
| 11241 |
if chunk then |
| 11242 |
insert(t,chunk) |
| 11243 |
else |
| 11244 |
chunk=remove(t) |
| 11245 |
if chunk then |
| 11246 |
return chunk |
| 11247 |
else |
| 11248 |
return src() |
| 11249 |
end |
| 11250 |
end |
| 11251 |
end |
| 11252 |
end |
| 11253 |
function source.chain(src,f,...) |
| 11254 |
if... then |
| 11255 |
f=filter.chain(f,...) |
| 11256 |
end |
| 11257 |
local last_in="" |
| 11258 |
local last_out="" |
| 11259 |
local state="feeding" |
| 11260 |
local err |
| 11261 |
return function() |
| 11262 |
if not last_out then |
| 11263 |
report("error: source is empty") |
| 11264 |
return |
| 11265 |
end |
| 11266 |
while true do |
| 11267 |
if state=="feeding" then |
| 11268 |
last_in,err=src() |
| 11269 |
if err then |
| 11270 |
return nil,err |
| 11271 |
end |
| 11272 |
last_out=f(last_in) |
| 11273 |
if not last_out then |
| 11274 |
if last_in then |
| 11275 |
report("error: filter returned inappropriate 'nil'") |
| 11276 |
end |
| 11277 |
return nil |
| 11278 |
elseif last_out~="" then |
| 11279 |
state="eating" |
| 11280 |
if last_in then |
| 11281 |
last_in="" |
| 11282 |
end |
| 11283 |
return last_out |
| 11284 |
end |
| 11285 |
else |
| 11286 |
last_out=f(last_in) |
| 11287 |
if last_out=="" then |
| 11288 |
if last_in=="" then |
| 11289 |
state="feeding" |
| 11290 |
else |
| 11291 |
report("error: filter returned nothing") |
| 11292 |
return |
| 11293 |
end |
| 11294 |
elseif not last_out then |
| 11295 |
if last_in then |
| 11296 |
report("filter returned inappropriate 'nil'") |
| 11297 |
end |
| 11298 |
return nil |
| 11299 |
else |
| 11300 |
return last_out |
| 11301 |
end |
| 11302 |
end |
| 11303 |
end |
| 11304 |
end |
| 11305 |
end |
| 11306 |
function source.cat(...) |
| 11307 |
local arg={... } |
| 11308 |
local src=remove(arg,1) |
| 11309 |
return function() |
| 11310 |
while src do |
| 11311 |
local chunk,err=src() |
| 11312 |
if chunk then |
| 11313 |
return chunk |
| 11314 |
end |
| 11315 |
if err then |
| 11316 |
return nil,err |
| 11317 |
end |
| 11318 |
src=remove(arg,1) |
| 11319 |
end |
| 11320 |
end |
| 11321 |
end |
| 11322 |
function sink.table(t) |
| 11323 |
if not t then |
| 11324 |
t={} |
| 11325 |
end |
| 11326 |
local f=function(chunk,err) |
| 11327 |
if chunk then |
| 11328 |
insert(t,chunk) |
| 11329 |
end |
| 11330 |
return 1 |
| 11331 |
end |
| 11332 |
return f,t |
| 11333 |
end |
| 11334 |
function sink.simplify(snk) |
| 11335 |
return function(chunk,err) |
| 11336 |
local ret,err_or_new=snk(chunk,err) |
| 11337 |
if not ret then |
| 11338 |
return nil,err_or_new |
| 11339 |
end |
| 11340 |
if err_or_new then |
| 11341 |
snk=err_or_new |
| 11342 |
end |
| 11343 |
return 1 |
| 11344 |
end |
| 11345 |
end |
| 11346 |
local function null() |
| 11347 |
return 1 |
| 11348 |
end |
| 11349 |
function sink.null() |
| 11350 |
return null |
| 11351 |
end |
| 11352 |
local function sinkerror(err) |
| 11353 |
return function() |
| 11354 |
return nil,err |
| 11355 |
end |
| 11356 |
end |
| 11357 |
sink.error=sinkerror |
| 11358 |
function sink.file(handle,io_err) |
| 11359 |
if handle then |
| 11360 |
return function(chunk,err) |
| 11361 |
if not chunk then |
| 11362 |
handle:close() |
| 11363 |
return 1 |
| 11364 |
else |
| 11365 |
return handle:write(chunk) |
| 11366 |
end |
| 11367 |
end |
| 11368 |
else |
| 11369 |
return sinkerror(io_err or "unable to open file") |
| 11370 |
end |
| 11371 |
end |
| 11372 |
function sink.chain(f,snk,...) |
| 11373 |
if... then |
| 11374 |
local args={ f,snk,... } |
| 11375 |
snk=remove(args,#args) |
| 11376 |
f=filter.chain(unpack(args)) |
| 11377 |
end |
| 11378 |
return function(chunk,err) |
| 11379 |
if chunk~="" then |
| 11380 |
local filtered=f(chunk) |
| 11381 |
local done=chunk and "" |
| 11382 |
while true do |
| 11383 |
local ret,snkerr=snk(filtered,err) |
| 11384 |
if not ret then |
| 11385 |
return nil,snkerr |
| 11386 |
end |
| 11387 |
if filtered==done then |
| 11388 |
return 1 |
| 11389 |
end |
| 11390 |
filtered=f(done) |
| 11391 |
end |
| 11392 |
else |
| 11393 |
return 1 |
| 11394 |
end |
| 11395 |
end |
| 11396 |
end |
| 11397 |
function pump.step(src,snk) |
| 11398 |
local chunk,src_err=src() |
| 11399 |
local ret,snk_err=snk(chunk,src_err) |
| 11400 |
if chunk and ret then |
| 11401 |
return 1 |
| 11402 |
else |
| 11403 |
return nil,src_err or snk_err |
| 11404 |
end |
| 11405 |
end |
| 11406 |
function pump.all(src,snk,step) |
| 11407 |
if not step then |
| 11408 |
step=pump.step |
| 11409 |
end |
| 11410 |
while true do |
| 11411 |
local ret,err=step(src,snk) |
| 11412 |
if not ret then |
| 11413 |
if err then |
| 11414 |
return nil,err |
| 11415 |
else |
| 11416 |
return 1 |
| 11417 |
end |
| 11418 |
end |
| 11419 |
end |
| 11420 |
end |
| 11421 |
package.loaded["ltn12"]=ltn12 |
| 11422 |
|
| 11423 |
|
| 11424 |
end -- of closure |
| 11425 |
|
| 11426 |
do -- create closure to overcome 200 locals limit |
| 11427 |
|
| 11428 |
package.loaded["util-soc-imp-mime"] = package.loaded["util-soc-imp-mime"] or true |
| 11429 |
|
| 11430 |
-- original size: 2373, stripped down to: 1931 |
| 11431 |
|
| 11432 |
|
| 11433 |
local type,tostring=type,tostring |
| 11434 |
local mime=mime or package.loaded.mime or require("mime.core") |
| 11435 |
local ltn12=ltn12 or package.loaded.ltn12 or require("ltn12") |
| 11436 |
local filtercycle=ltn12.filter.cycle |
| 11437 |
local function report(fmt,first,...) |
| 11438 |
if logs then |
| 11439 |
report=logs and logs.reporter("mime") |
| 11440 |
report(fmt,first,...) |
| 11441 |
elseif fmt then |
| 11442 |
fmt="mime: "..fmt |
| 11443 |
if first then |
| 11444 |
print(format(fmt,first,...)) |
| 11445 |
else |
| 11446 |
print(fmt) |
| 11447 |
end |
| 11448 |
end |
| 11449 |
end |
| 11450 |
mime.report=report |
| 11451 |
local encodet={} |
| 11452 |
local decodet={} |
| 11453 |
local wrapt={} |
| 11454 |
mime.encodet=encodet |
| 11455 |
mime.decodet=decodet |
| 11456 |
mime.wrapt=wrapt |
| 11457 |
local mime_b64=mime.b64 |
| 11458 |
local mime_qp=mime.qp |
| 11459 |
local mime_unb64=mime.unb64 |
| 11460 |
local mime_unqp=mime.unqp |
| 11461 |
local mime_wrp=mime.wrp |
| 11462 |
local mime_qpwrp=mime.qpwrp |
| 11463 |
local mime_eol=mime_eol |
| 11464 |
local mime_dot=mime_dot |
| 11465 |
encodet['base64']=function() |
| 11466 |
return filtercycle(mime_b64,"") |
| 11467 |
end |
| 11468 |
encodet['quoted-printable']=function(mode) |
| 11469 |
return filtercycle(mime_qp,"",mode=="binary" and "=0D=0A" or "\r\n") |
| 11470 |
end |
| 11471 |
decodet['base64']=function() |
| 11472 |
return filtercycle(mime_unb64,"") |
| 11473 |
end |
| 11474 |
decodet['quoted-printable']=function() |
| 11475 |
return filtercycle(mime_unqp,"") |
| 11476 |
end |
| 11477 |
local wraptext=function(length) |
| 11478 |
if not length then |
| 11479 |
length=76 |
| 11480 |
end |
| 11481 |
return filtercycle(mime_wrp,length,length) |
| 11482 |
end |
| 11483 |
local wrapquoted=function() |
| 11484 |
return filtercycle(mime_qpwrp,76,76) |
| 11485 |
end |
| 11486 |
wrapt['text']=wraptext |
| 11487 |
wrapt['base64']=wraptext |
| 11488 |
wrapt['default']=wraptext |
| 11489 |
wrapt['quoted-printable']=wrapquoted |
| 11490 |
function mime.normalize(marker) |
| 11491 |
return filtercycle(mime_eol,0,marker) |
| 11492 |
end |
| 11493 |
function mime.stuff() |
| 11494 |
return filtercycle(mime_dot,2) |
| 11495 |
end |
| 11496 |
local function choose(list) |
| 11497 |
return function(name,opt1,opt2) |
| 11498 |
if type(name)~="string" then |
| 11499 |
name,opt1,opt2="default",name,opt1 |
| 11500 |
end |
| 11501 |
local filter=list[name or "nil"] |
| 11502 |
if filter then |
| 11503 |
return filter(opt1,opt2) |
| 11504 |
else |
| 11505 |
report("error: unknown key '%s'",tostring(name)) |
| 11506 |
end |
| 11507 |
end |
| 11508 |
end |
| 11509 |
mime.encode=choose(encodet) |
| 11510 |
mime.decode=choose(decodet) |
| 11511 |
mime.wrap=choose(wrapt) |
| 11512 |
package.loaded["mime"]=mime |
| 11513 |
|
| 11514 |
|
| 11515 |
end -- of closure |
| 11516 |
|
| 11517 |
do -- create closure to overcome 200 locals limit |
| 11518 |
|
| 11519 |
package.loaded["util-soc-imp-url"] = package.loaded["util-soc-imp-url"] or true |
| 11520 |
|
| 11521 |
-- original size: 6863, stripped down to: 5269 |
| 11522 |
|
| 11523 |
|
| 11524 |
local tonumber,tostring,type=tonumber,tostring,type |
| 11525 |
local gsub,sub,match,find,format,byte,char=string.gsub,string.sub,string.match,string.find,string.format,string.byte,string.char |
| 11526 |
local insert=table.insert |
| 11527 |
local socket=socket or require("socket") |
| 11528 |
local url={ |
| 11529 |
_VERSION="URL 1.0.3", |
| 11530 |
} |
| 11531 |
socket.url=url |
| 11532 |
function url.escape(s) |
| 11533 |
return (gsub(s,"([^A-Za-z0-9_])",function(c) |
| 11534 |
return format("%%%02x",byte(c)) |
| 11535 |
end)) |
| 11536 |
end |
| 11537 |
local function make_set(t) |
| 11538 |
local s={} |
| 11539 |
for i=1,#t do |
| 11540 |
s[t[i]]=true |
| 11541 |
end |
| 11542 |
return s |
| 11543 |
end |
| 11544 |
local segment_set=make_set { |
| 11545 |
"-","_",".","!","~","*","'","(", |
| 11546 |
")",":","@","&","=","+","$",",", |
| 11547 |
} |
| 11548 |
local function protect_segment(s) |
| 11549 |
return gsub(s,"([^A-Za-z0-9_])",function(c) |
| 11550 |
if segment_set[c] then |
| 11551 |
return c |
| 11552 |
else |
| 11553 |
return format("%%%02X",byte(c)) |
| 11554 |
end |
| 11555 |
end) |
| 11556 |
end |
| 11557 |
function url.unescape(s) |
| 11558 |
return (gsub(s,"%%(%x%x)",function(hex) |
| 11559 |
return char(tonumber(hex,16)) |
| 11560 |
end)) |
| 11561 |
end |
| 11562 |
local function absolute_path(base_path,relative_path) |
| 11563 |
if find(relative_path,"^/") then |
| 11564 |
return relative_path |
| 11565 |
end |
| 11566 |
local path=gsub(base_path,"[^/]*$","") |
| 11567 |
path=path..relative_path |
| 11568 |
path=gsub(path,"([^/]*%./)",function (s) |
| 11569 |
if s~="./" then |
| 11570 |
return s |
| 11571 |
else |
| 11572 |
return "" |
| 11573 |
end |
| 11574 |
end) |
| 11575 |
path=gsub(path,"/%.$","/") |
| 11576 |
local reduced |
| 11577 |
while reduced~=path do |
| 11578 |
reduced=path |
| 11579 |
path=gsub(reduced,"([^/]*/%.%./)",function (s) |
| 11580 |
if s~="../../" then |
| 11581 |
return "" |
| 11582 |
else |
| 11583 |
return s |
| 11584 |
end |
| 11585 |
end) |
| 11586 |
end |
| 11587 |
path=gsub(reduced,"([^/]*/%.%.)$",function (s) |
| 11588 |
if s~="../.." then |
| 11589 |
return "" |
| 11590 |
else |
| 11591 |
return s |
| 11592 |
end |
| 11593 |
end) |
| 11594 |
return path |
| 11595 |
end |
| 11596 |
function url.parse(url,default) |
| 11597 |
local parsed={} |
| 11598 |
for k,v in next,default or parsed do |
| 11599 |
parsed[k]=v |
| 11600 |
end |
| 11601 |
if not url or url=="" then |
| 11602 |
return nil,"invalid url" |
| 11603 |
end |
| 11604 |
url=gsub(url,"#(.*)$",function(f) |
| 11605 |
parsed.fragment=f |
| 11606 |
return "" |
| 11607 |
end) |
| 11608 |
url=gsub(url,"^([%w][%w%+%-%.]*)%:",function(s) |
| 11609 |
parsed.scheme=s |
| 11610 |
return "" |
| 11611 |
end) |
| 11612 |
url=gsub(url,"^//([^/]*)",function(n) |
| 11613 |
parsed.authority=n |
| 11614 |
return "" |
| 11615 |
end) |
| 11616 |
url=gsub(url,"%?(.*)",function(q) |
| 11617 |
parsed.query=q |
| 11618 |
return "" |
| 11619 |
end) |
| 11620 |
url=gsub(url,"%;(.*)",function(p) |
| 11621 |
parsed.params=p |
| 11622 |
return "" |
| 11623 |
end) |
| 11624 |
if url~="" then |
| 11625 |
parsed.path=url |
| 11626 |
end |
| 11627 |
local authority=parsed.authority |
| 11628 |
if not authority then |
| 11629 |
return parsed |
| 11630 |
end |
| 11631 |
authority=gsub(authority,"^([^@]*)@",function(u) |
| 11632 |
parsed.userinfo=u |
| 11633 |
return "" |
| 11634 |
end) |
| 11635 |
authority=gsub(authority,":([^:%]]*)$",function(p) |
| 11636 |
parsed.port=p |
| 11637 |
return "" |
| 11638 |
end) |
| 11639 |
if authority~="" then |
| 11640 |
parsed.host=match(authority,"^%[(.+)%]$") or authority |
| 11641 |
end |
| 11642 |
local userinfo=parsed.userinfo |
| 11643 |
if not userinfo then |
| 11644 |
return parsed |
| 11645 |
end |
| 11646 |
userinfo=gsub(userinfo,":([^:]*)$",function(p) |
| 11647 |
parsed.password=p |
| 11648 |
return "" |
| 11649 |
end) |
| 11650 |
parsed.user=userinfo |
| 11651 |
return parsed |
| 11652 |
end |
| 11653 |
function url.build(parsed) |
| 11654 |
local url=parsed.path or "" |
| 11655 |
if parsed.params then |
| 11656 |
url=url..";"..parsed.params |
| 11657 |
end |
| 11658 |
if parsed.query then |
| 11659 |
url=url.."?"..parsed.query |
| 11660 |
end |
| 11661 |
local authority=parsed.authority |
| 11662 |
if parsed.host then |
| 11663 |
authority=parsed.host |
| 11664 |
if find(authority,":") then |
| 11665 |
authority="["..authority.."]" |
| 11666 |
end |
| 11667 |
if parsed.port then |
| 11668 |
authority=authority..":"..tostring(parsed.port) |
| 11669 |
end |
| 11670 |
local userinfo=parsed.userinfo |
| 11671 |
if parsed.user then |
| 11672 |
userinfo=parsed.user |
| 11673 |
if parsed.password then |
| 11674 |
userinfo=userinfo..":"..parsed.password |
| 11675 |
end |
| 11676 |
end |
| 11677 |
if userinfo then authority=userinfo.."@"..authority end |
| 11678 |
end |
| 11679 |
if authority then |
| 11680 |
url="//"..authority..url |
| 11681 |
end |
| 11682 |
if parsed.scheme then |
| 11683 |
url=parsed.scheme..":"..url |
| 11684 |
end |
| 11685 |
if parsed.fragment then |
| 11686 |
url=url.."#"..parsed.fragment |
| 11687 |
end |
| 11688 |
return url |
| 11689 |
end |
| 11690 |
function url.absolute(base_url,relative_url) |
| 11691 |
local base_parsed |
| 11692 |
if type(base_url)=="table" then |
| 11693 |
base_parsed=base_url |
| 11694 |
base_url=url.build(base_parsed) |
| 11695 |
else |
| 11696 |
base_parsed=url.parse(base_url) |
| 11697 |
end |
| 11698 |
local relative_parsed=url.parse(relative_url) |
| 11699 |
if not base_parsed then |
| 11700 |
return relative_url |
| 11701 |
elseif not relative_parsed then |
| 11702 |
return base_url |
| 11703 |
elseif relative_parsed.scheme then |
| 11704 |
return relative_url |
| 11705 |
else |
| 11706 |
relative_parsed.scheme=base_parsed.scheme |
| 11707 |
if not relative_parsed.authority then |
| 11708 |
relative_parsed.authority=base_parsed.authority |
| 11709 |
if not relative_parsed.path then |
| 11710 |
relative_parsed.path=base_parsed.path |
| 11711 |
if not relative_parsed.params then |
| 11712 |
relative_parsed.params=base_parsed.params |
| 11713 |
if not relative_parsed.query then |
| 11714 |
relative_parsed.query=base_parsed.query |
| 11715 |
end |
| 11716 |
end |
| 11717 |
else |
| 11718 |
relative_parsed.path=absolute_path(base_parsed.path or "",relative_parsed.path) |
| 11719 |
end |
| 11720 |
end |
| 11721 |
return url.build(relative_parsed) |
| 11722 |
end |
| 11723 |
end |
| 11724 |
function url.parse_path(path) |
| 11725 |
local parsed={} |
| 11726 |
path=path or "" |
| 11727 |
gsub(path,"([^/]+)",function (s) |
| 11728 |
insert(parsed,s) |
| 11729 |
end) |
| 11730 |
for i=1,#parsed do |
| 11731 |
parsed[i]=url.unescape(parsed[i]) |
| 11732 |
end |
| 11733 |
if sub(path,1,1)=="/" then |
| 11734 |
parsed.is_absolute=1 |
| 11735 |
end |
| 11736 |
if sub(path,-1,-1)=="/" then |
| 11737 |
parsed.is_directory=1 |
| 11738 |
end |
| 11739 |
return parsed |
| 11740 |
end |
| 11741 |
function url.build_path(parsed,unsafe) |
| 11742 |
local path="" |
| 11743 |
local n=#parsed |
| 11744 |
if unsafe then |
| 11745 |
for i=1,n-1 do |
| 11746 |
path=path..parsed[i].."/" |
| 11747 |
end |
| 11748 |
if n>0 then |
| 11749 |
path=path..parsed[n] |
| 11750 |
if parsed.is_directory then |
| 11751 |
path=path.."/" |
| 11752 |
end |
| 11753 |
end |
| 11754 |
else |
| 11755 |
for i=1,n-1 do |
| 11756 |
path=path..protect_segment(parsed[i]).."/" |
| 11757 |
end |
| 11758 |
if n>0 then |
| 11759 |
path=path..protect_segment(parsed[n]) |
| 11760 |
if parsed.is_directory then |
| 11761 |
path=path.."/" |
| 11762 |
end |
| 11763 |
end |
| 11764 |
end |
| 11765 |
if parsed.is_absolute then |
| 11766 |
path="/"..path |
| 11767 |
end |
| 11768 |
return path |
| 11769 |
end |
| 11770 |
package.loaded["socket.url"]=url |
| 11771 |
|
| 11772 |
|
| 11773 |
end -- of closure |
| 11774 |
|
| 11775 |
do -- create closure to overcome 200 locals limit |
| 11776 |
|
| 11777 |
package.loaded["util-soc-imp-headers"] = package.loaded["util-soc-imp-headers"] or true |
| 11778 |
|
| 11779 |
-- original size: 5721, stripped down to: 3754 |
| 11780 |
|
| 11781 |
|
| 11782 |
local next=next |
| 11783 |
local lower=string.lower |
| 11784 |
local concat=table.concat |
| 11785 |
local socket=socket or require("socket") |
| 11786 |
local headers={} |
| 11787 |
socket.headers=headers |
| 11788 |
local canonic={ |
| 11789 |
["accept"]="Accept", |
| 11790 |
["accept-charset"]="Accept-Charset", |
| 11791 |
["accept-encoding"]="Accept-Encoding", |
| 11792 |
["accept-language"]="Accept-Language", |
| 11793 |
["accept-ranges"]="Accept-Ranges", |
| 11794 |
["action"]="Action", |
| 11795 |
["alternate-recipient"]="Alternate-Recipient", |
| 11796 |
["age"]="Age", |
| 11797 |
["allow"]="Allow", |
| 11798 |
["arrival-date"]="Arrival-Date", |
| 11799 |
["authorization"]="Authorization", |
| 11800 |
["bcc"]="Bcc", |
| 11801 |
["cache-control"]="Cache-Control", |
| 11802 |
["cc"]="Cc", |
| 11803 |
["comments"]="Comments", |
| 11804 |
["connection"]="Connection", |
| 11805 |
["content-description"]="Content-Description", |
| 11806 |
["content-disposition"]="Content-Disposition", |
| 11807 |
["content-encoding"]="Content-Encoding", |
| 11808 |
["content-id"]="Content-ID", |
| 11809 |
["content-language"]="Content-Language", |
| 11810 |
["content-length"]="Content-Length", |
| 11811 |
["content-location"]="Content-Location", |
| 11812 |
["content-md5"]="Content-MD5", |
| 11813 |
["content-range"]="Content-Range", |
| 11814 |
["content-transfer-encoding"]="Content-Transfer-Encoding", |
| 11815 |
["content-type"]="Content-Type", |
| 11816 |
["cookie"]="Cookie", |
| 11817 |
["date"]="Date", |
| 11818 |
["diagnostic-code"]="Diagnostic-Code", |
| 11819 |
["dsn-gateway"]="DSN-Gateway", |
| 11820 |
["etag"]="ETag", |
| 11821 |
["expect"]="Expect", |
| 11822 |
["expires"]="Expires", |
| 11823 |
["final-log-id"]="Final-Log-ID", |
| 11824 |
["final-recipient"]="Final-Recipient", |
| 11825 |
["from"]="From", |
| 11826 |
["host"]="Host", |
| 11827 |
["if-match"]="If-Match", |
| 11828 |
["if-modified-since"]="If-Modified-Since", |
| 11829 |
["if-none-match"]="If-None-Match", |
| 11830 |
["if-range"]="If-Range", |
| 11831 |
["if-unmodified-since"]="If-Unmodified-Since", |
| 11832 |
["in-reply-to"]="In-Reply-To", |
| 11833 |
["keywords"]="Keywords", |
| 11834 |
["last-attempt-date"]="Last-Attempt-Date", |
| 11835 |
["last-modified"]="Last-Modified", |
| 11836 |
["location"]="Location", |
| 11837 |
["max-forwards"]="Max-Forwards", |
| 11838 |
["message-id"]="Message-ID", |
| 11839 |
["mime-version"]="MIME-Version", |
| 11840 |
["original-envelope-id"]="Original-Envelope-ID", |
| 11841 |
["original-recipient"]="Original-Recipient", |
| 11842 |
["pragma"]="Pragma", |
| 11843 |
["proxy-authenticate"]="Proxy-Authenticate", |
| 11844 |
["proxy-authorization"]="Proxy-Authorization", |
| 11845 |
["range"]="Range", |
| 11846 |
["received"]="Received", |
| 11847 |
["received-from-mta"]="Received-From-MTA", |
| 11848 |
["references"]="References", |
| 11849 |
["referer"]="Referer", |
| 11850 |
["remote-mta"]="Remote-MTA", |
| 11851 |
["reply-to"]="Reply-To", |
| 11852 |
["reporting-mta"]="Reporting-MTA", |
| 11853 |
["resent-bcc"]="Resent-Bcc", |
| 11854 |
["resent-cc"]="Resent-Cc", |
| 11855 |
["resent-date"]="Resent-Date", |
| 11856 |
["resent-from"]="Resent-From", |
| 11857 |
["resent-message-id"]="Resent-Message-ID", |
| 11858 |
["resent-reply-to"]="Resent-Reply-To", |
| 11859 |
["resent-sender"]="Resent-Sender", |
| 11860 |
["resent-to"]="Resent-To", |
| 11861 |
["retry-after"]="Retry-After", |
| 11862 |
["return-path"]="Return-Path", |
| 11863 |
["sender"]="Sender", |
| 11864 |
["server"]="Server", |
| 11865 |
["smtp-remote-recipient"]="SMTP-Remote-Recipient", |
| 11866 |
["status"]="Status", |
| 11867 |
["subject"]="Subject", |
| 11868 |
["te"]="TE", |
| 11869 |
["to"]="To", |
| 11870 |
["trailer"]="Trailer", |
| 11871 |
["transfer-encoding"]="Transfer-Encoding", |
| 11872 |
["upgrade"]="Upgrade", |
| 11873 |
["user-agent"]="User-Agent", |
| 11874 |
["vary"]="Vary", |
| 11875 |
["via"]="Via", |
| 11876 |
["warning"]="Warning", |
| 11877 |
["will-retry-until"]="Will-Retry-Until", |
| 11878 |
["www-authenticate"]="WWW-Authenticate", |
| 11879 |
["x-mailer"]="X-Mailer", |
| 11880 |
} |
| 11881 |
headers.canonic=setmetatable(canonic,{ |
| 11882 |
__index=function(t,k) |
| 11883 |
socket.report("invalid header: %s",k) |
| 11884 |
t[k]=k |
| 11885 |
return k |
| 11886 |
end |
| 11887 |
}) |
| 11888 |
function headers.normalize(headers) |
| 11889 |
if not headers then |
| 11890 |
return {} |
| 11891 |
end |
| 11892 |
local normalized={} |
| 11893 |
for k,v in next,headers do |
| 11894 |
normalized[#normalized+1]=canonic[k]..": "..v |
| 11895 |
end |
| 11896 |
normalized[#normalized+1]="" |
| 11897 |
normalized[#normalized+1]="" |
| 11898 |
return concat(normalized,"\r\n") |
| 11899 |
end |
| 11900 |
function headers.lower(lowered,headers) |
| 11901 |
if not lowered then |
| 11902 |
return {} |
| 11903 |
end |
| 11904 |
if not headers then |
| 11905 |
lowered,headers={},lowered |
| 11906 |
end |
| 11907 |
for k,v in next,headers do |
| 11908 |
lowered[lower(k)]=v |
| 11909 |
end |
| 11910 |
return lowered |
| 11911 |
end |
| 11912 |
socket.headers=headers |
| 11913 |
package.loaded["socket.headers"]=headers |
| 11914 |
|
| 11915 |
|
| 11916 |
end -- of closure |
| 11917 |
|
| 11918 |
do -- create closure to overcome 200 locals limit |
| 11919 |
|
| 11920 |
package.loaded["util-soc-imp-tp"] = package.loaded["util-soc-imp-tp"] or true |
| 11921 |
|
| 11922 |
-- original size: 3116, stripped down to: 2533 |
| 11923 |
|
| 11924 |
|
| 11925 |
local setmetatable,next,type,tonumber=setmetatable,next,type,tonumber |
| 11926 |
local find,upper=string.find,string.upper |
| 11927 |
local socket=socket or require("socket") |
| 11928 |
local ltn12=ltn12 or require("ltn12") |
| 11929 |
local skipsocket=socket.skip |
| 11930 |
local sinksocket=socket.sink |
| 11931 |
local tcpsocket=socket.tcp |
| 11932 |
local ltn12pump=ltn12.pump |
| 11933 |
local pumpall=ltn12pump.all |
| 11934 |
local pumpstep=ltn12pump.step |
| 11935 |
local tp={ |
| 11936 |
TIMEOUT=60, |
| 11937 |
} |
| 11938 |
socket.tp=tp |
| 11939 |
local function get_reply(c) |
| 11940 |
local line,err=c:receive() |
| 11941 |
local reply=line |
| 11942 |
if err then return |
| 11943 |
nil,err |
| 11944 |
end |
| 11945 |
local code,sep=skipsocket(2,find(line,"^(%d%d%d)(.?)")) |
| 11946 |
if not code then |
| 11947 |
return nil,"invalid server reply" |
| 11948 |
end |
| 11949 |
if sep=="-" then |
| 11950 |
local current |
| 11951 |
repeat |
| 11952 |
line,err=c:receive() |
| 11953 |
if err then |
| 11954 |
return nil,err |
| 11955 |
end |
| 11956 |
current,sep=skipsocket(2,find(line,"^(%d%d%d)(.?)")) |
| 11957 |
reply=reply.."\n"..line |
| 11958 |
until code==current and sep==" " |
| 11959 |
end |
| 11960 |
return code,reply |
| 11961 |
end |
| 11962 |
local methods={} |
| 11963 |
local mt={ __index=methods } |
| 11964 |
function methods.getpeername(self) |
| 11965 |
return self.c:getpeername() |
| 11966 |
end |
| 11967 |
function methods.getsockname(self) |
| 11968 |
return self.c:getpeername() |
| 11969 |
end |
| 11970 |
function methods.check(self,ok) |
| 11971 |
local code,reply=get_reply(self.c) |
| 11972 |
if not code then |
| 11973 |
return nil,reply |
| 11974 |
end |
| 11975 |
local c=tonumber(code) |
| 11976 |
local t=type(ok) |
| 11977 |
if t=="function" then |
| 11978 |
return ok(c,reply) |
| 11979 |
elseif t=="table" then |
| 11980 |
for i=1,#ok do |
| 11981 |
if find(code,ok[i]) then |
| 11982 |
return c,reply |
| 11983 |
end |
| 11984 |
end |
| 11985 |
return nil,reply |
| 11986 |
elseif find(code,ok) then |
| 11987 |
return c,reply |
| 11988 |
else |
| 11989 |
return nil,reply |
| 11990 |
end |
| 11991 |
end |
| 11992 |
function methods.command(self,cmd,arg) |
| 11993 |
cmd=upper(cmd) |
| 11994 |
if arg then |
| 11995 |
cmd=cmd.." "..arg.."\r\n" |
| 11996 |
else |
| 11997 |
cmd=cmd.."\r\n" |
| 11998 |
end |
| 11999 |
return self.c:send(cmd) |
| 12000 |
end |
| 12001 |
function methods.sink(self,snk,pat) |
| 12002 |
local chunk,err=self.c:receive(pat) |
| 12003 |
return snk(chunk,err) |
| 12004 |
end |
| 12005 |
function methods.send(self,data) |
| 12006 |
return self.c:send(data) |
| 12007 |
end |
| 12008 |
function methods.receive(self,pat) |
| 12009 |
return self.c:receive(pat) |
| 12010 |
end |
| 12011 |
function methods.getfd(self) |
| 12012 |
return self.c:getfd() |
| 12013 |
end |
| 12014 |
function methods.dirty(self) |
| 12015 |
return self.c:dirty() |
| 12016 |
end |
| 12017 |
function methods.getcontrol(self) |
| 12018 |
return self.c |
| 12019 |
end |
| 12020 |
function methods.source(self,source,step) |
| 12021 |
local sink=sinksocket("keep-open",self.c) |
| 12022 |
local ret,err=pumpall(source,sink,step or pumpstep) |
| 12023 |
return ret,err |
| 12024 |
end |
| 12025 |
function methods.close(self) |
| 12026 |
self.c:close() |
| 12027 |
return 1 |
| 12028 |
end |
| 12029 |
function tp.connect(host,port,timeout,create) |
| 12030 |
local c,e=(create or tcpsocket)() |
| 12031 |
if not c then |
| 12032 |
return nil,e |
| 12033 |
end |
| 12034 |
c:settimeout(timeout or tp.TIMEOUT) |
| 12035 |
local r,e=c:connect(host,port) |
| 12036 |
if not r then |
| 12037 |
c:close() |
| 12038 |
return nil,e |
| 12039 |
end |
| 12040 |
return setmetatable({ c=c },mt) |
| 12041 |
end |
| 12042 |
package.loaded["socket.tp"]=tp |
| 12043 |
|
| 12044 |
|
| 12045 |
end -- of closure |
| 12046 |
|
| 12047 |
do -- create closure to overcome 200 locals limit |
| 12048 |
|
| 12049 |
package.loaded["util-soc-imp-http"] = package.loaded["util-soc-imp-http"] or true |
| 12050 |
|
| 12051 |
-- original size: 13055, stripped down to: 9818 |
| 12052 |
|
| 12053 |
|
| 12054 |
local tostring,tonumber,setmetatable,next,type=tostring,tonumber,setmetatable,next,type |
| 12055 |
local find,lower,format,gsub,match=string.find,string.lower,string.format,string.gsub,string.match |
| 12056 |
local concat=table.concat |
| 12057 |
local socket=socket or require("socket") |
| 12058 |
local url=socket.url or require("socket.url") |
| 12059 |
local ltn12=ltn12 or require("ltn12") |
| 12060 |
local mime=mime or require("mime") |
| 12061 |
local headers=socket.headers or require("socket.headers") |
| 12062 |
local normalizeheaders=headers.normalize |
| 12063 |
local parseurl=url.parse |
| 12064 |
local buildurl=url.build |
| 12065 |
local absoluteurl=url.absolute |
| 12066 |
local unescapeurl=url.unescape |
| 12067 |
local skipsocket=socket.skip |
| 12068 |
local sinksocket=socket.sink |
| 12069 |
local sourcesocket=socket.source |
| 12070 |
local trysocket=socket.try |
| 12071 |
local tcpsocket=socket.tcp |
| 12072 |
local newtrysocket=socket.newtry |
| 12073 |
local protectsocket=socket.protect |
| 12074 |
local emptysource=ltn12.source.empty |
| 12075 |
local stringsource=ltn12.source.string |
| 12076 |
local rewindsource=ltn12.source.rewind |
| 12077 |
local pumpstep=ltn12.pump.step |
| 12078 |
local pumpall=ltn12.pump.all |
| 12079 |
local sinknull=ltn12.sink.null |
| 12080 |
local sinktable=ltn12.sink.table |
| 12081 |
local lowerheaders=headers.lower |
| 12082 |
local mimeb64=mime.b64 |
| 12083 |
local http={ |
| 12084 |
TIMEOUT=60, |
| 12085 |
USERAGENT=socket._VERSION, |
| 12086 |
} |
| 12087 |
socket.http=http |
| 12088 |
local PORT=80 |
| 12089 |
local SCHEMES={ |
| 12090 |
http=true, |
| 12091 |
} |
| 12092 |
local function receiveheaders(sock,headers) |
| 12093 |
if not headers then |
| 12094 |
headers={} |
| 12095 |
end |
| 12096 |
local line,err=sock:receive("*l") |
| 12097 |
if err then |
| 12098 |
return nil,err |
| 12099 |
end |
| 12100 |
while line~="" do |
| 12101 |
local name,value=skipsocket(2,find(line,"^(.-):%s*(.*)")) |
| 12102 |
if not (name and value) then |
| 12103 |
return nil,"malformed response headers" |
| 12104 |
end |
| 12105 |
name=lower(name) |
| 12106 |
line,err=sock:receive("*l") |
| 12107 |
if err then |
| 12108 |
return nil,err |
| 12109 |
end |
| 12110 |
while find(line,"^%s") do |
| 12111 |
value=value..line |
| 12112 |
line=sock:receive("*l") |
| 12113 |
if err then |
| 12114 |
return nil,err |
| 12115 |
end |
| 12116 |
end |
| 12117 |
local found=headers[name] |
| 12118 |
if found then |
| 12119 |
value=found..", "..value |
| 12120 |
end |
| 12121 |
headers[name]=value |
| 12122 |
end |
| 12123 |
return headers |
| 12124 |
end |
| 12125 |
socket.sourcet["http-chunked"]=function(sock,headers) |
| 12126 |
return setmetatable ( |
| 12127 |
{ |
| 12128 |
getfd=function() return sock:getfd() end, |
| 12129 |
dirty=function() return sock:dirty() end, |
| 12130 |
},{ |
| 12131 |
__call=function() |
| 12132 |
local line,err=sock:receive("*l") |
| 12133 |
if err then |
| 12134 |
return nil,err |
| 12135 |
end |
| 12136 |
local size=tonumber(gsub(line,";.*",""),16) |
| 12137 |
if not size then |
| 12138 |
return nil,"invalid chunk size" |
| 12139 |
end |
| 12140 |
if size>0 then |
| 12141 |
local chunk,err,part=sock:receive(size) |
| 12142 |
if chunk then |
| 12143 |
sock:receive("*a") |
| 12144 |
end |
| 12145 |
return chunk,err |
| 12146 |
else |
| 12147 |
headers,err=receiveheaders(sock,headers) |
| 12148 |
if not headers then |
| 12149 |
return nil,err |
| 12150 |
end |
| 12151 |
end |
| 12152 |
end |
| 12153 |
} |
| 12154 |
) |
| 12155 |
end |
| 12156 |
socket.sinkt["http-chunked"]=function(sock) |
| 12157 |
return setmetatable( |
| 12158 |
{ |
| 12159 |
getfd=function() return sock:getfd() end, |
| 12160 |
dirty=function() return sock:dirty() end, |
| 12161 |
}, |
| 12162 |
{ |
| 12163 |
__call=function(self,chunk,err) |
| 12164 |
if not chunk then |
| 12165 |
chunk="" |
| 12166 |
end |
| 12167 |
return sock:send(format("%X\r\n%s\r\n",#chunk,chunk)) |
| 12168 |
end |
| 12169 |
}) |
| 12170 |
end |
| 12171 |
local methods={} |
| 12172 |
local mt={ __index=methods } |
| 12173 |
local function openhttp(host,port,create) |
| 12174 |
local c=trysocket((create or tcpsocket)()) |
| 12175 |
local h=setmetatable({ c=c },mt) |
| 12176 |
local try=newtrysocket(function() h:close() end) |
| 12177 |
h.try=try |
| 12178 |
try(c:settimeout(http.TIMEOUT)) |
| 12179 |
try(c:connect(host,port or PORT)) |
| 12180 |
return h |
| 12181 |
end |
| 12182 |
http.open=openhttp |
| 12183 |
function methods.sendrequestline(self,method,uri) |
| 12184 |
local requestline=format("%s %s HTTP/1.1\r\n",method or "GET",uri) |
| 12185 |
return self.try(self.c:send(requestline)) |
| 12186 |
end |
| 12187 |
function methods.sendheaders(self,headers) |
| 12188 |
self.try(self.c:send(normalizeheaders(headers))) |
| 12189 |
return 1 |
| 12190 |
end |
| 12191 |
function methods.sendbody(self,headers,source,step) |
| 12192 |
if not source then |
| 12193 |
source=emptysource() |
| 12194 |
end |
| 12195 |
if not step then |
| 12196 |
step=pumpstep |
| 12197 |
end |
| 12198 |
local mode="http-chunked" |
| 12199 |
if headers["content-length"] then |
| 12200 |
mode="keep-open" |
| 12201 |
end |
| 12202 |
return self.try(pumpall(source,sinksocket(mode,self.c),step)) |
| 12203 |
end |
| 12204 |
function methods.receivestatusline(self) |
| 12205 |
local try=self.try |
| 12206 |
local status,err=try(self.c:receive(5)) |
| 12207 |
if status~="HTTP/" then |
| 12208 |
if err=="timeout" then |
| 12209 |
return 408 |
| 12210 |
else |
| 12211 |
return nil,status |
| 12212 |
end |
| 12213 |
end |
| 12214 |
status=try(self.c:receive("*l",status)) |
| 12215 |
local code=skipsocket(2,find(status,"HTTP/%d*%.%d* (%d%d%d)")) |
| 12216 |
return try(tonumber(code),status) |
| 12217 |
end |
| 12218 |
function methods.receiveheaders(self) |
| 12219 |
return self.try(receiveheaders(self.c)) |
| 12220 |
end |
| 12221 |
function methods.receivebody(self,headers,sink,step) |
| 12222 |
if not sink then |
| 12223 |
sink=sinknull() |
| 12224 |
end |
| 12225 |
if not step then |
| 12226 |
step=pumpstep |
| 12227 |
end |
| 12228 |
local length=tonumber(headers["content-length"]) |
| 12229 |
local encoding=headers["transfer-encoding"] |
| 12230 |
local mode="default" |
| 12231 |
if encoding and encoding~="identity" then |
| 12232 |
mode="http-chunked" |
| 12233 |
elseif length then |
| 12234 |
mode="by-length" |
| 12235 |
end |
| 12236 |
return self.try(pumpall(sourcesocket(mode,self.c,length),sink,step)) |
| 12237 |
end |
| 12238 |
function methods.receive09body(self,status,sink,step) |
| 12239 |
local source=rewindsource(sourcesocket("until-closed",self.c)) |
| 12240 |
source(status) |
| 12241 |
return self.try(pumpall(source,sink,step)) |
| 12242 |
end |
| 12243 |
function methods.close(self) |
| 12244 |
return self.c:close() |
| 12245 |
end |
| 12246 |
local function adjusturi(request) |
| 12247 |
if not request.proxy and not http.PROXY then |
| 12248 |
request={ |
| 12249 |
path=trysocket(request.path,"invalid path 'nil'"), |
| 12250 |
params=request.params, |
| 12251 |
query=request.query, |
| 12252 |
fragment=request.fragment, |
| 12253 |
} |
| 12254 |
end |
| 12255 |
return buildurl(request) |
| 12256 |
end |
| 12257 |
local function adjustheaders(request) |
| 12258 |
local headers={ |
| 12259 |
["user-agent"]=http.USERAGENT, |
| 12260 |
["host"]=gsub(request.authority,"^.-@",""), |
| 12261 |
["connection"]="close, TE", |
| 12262 |
["te"]="trailers" |
| 12263 |
} |
| 12264 |
local username=request.user |
| 12265 |
local password=request.password |
| 12266 |
if username and password then |
| 12267 |
headers["authorization"]="Basic "..(mimeb64(username..":"..unescapeurl(password))) |
| 12268 |
end |
| 12269 |
local proxy=request.proxy or http.PROXY |
| 12270 |
if proxy then |
| 12271 |
proxy=parseurl(proxy) |
| 12272 |
local username=proxy.user |
| 12273 |
local password=proxy.password |
| 12274 |
if username and password then |
| 12275 |
headers["proxy-authorization"]="Basic "..(mimeb64(username..":"..password)) |
| 12276 |
end |
| 12277 |
end |
| 12278 |
local requestheaders=request.headers |
| 12279 |
if requestheaders then |
| 12280 |
headers=lowerheaders(headers,requestheaders) |
| 12281 |
end |
| 12282 |
return headers |
| 12283 |
end |
| 12284 |
local default={ |
| 12285 |
host="", |
| 12286 |
port=PORT, |
| 12287 |
path="/", |
| 12288 |
scheme="http" |
| 12289 |
} |
| 12290 |
local function adjustrequest(originalrequest) |
| 12291 |
local url=originalrequest.url |
| 12292 |
local request=url and parseurl(url,default) or {} |
| 12293 |
for k,v in next,originalrequest do |
| 12294 |
request[k]=v |
| 12295 |
end |
| 12296 |
local host=request.host |
| 12297 |
local port=request.port |
| 12298 |
local uri=request.uri |
| 12299 |
if not host or host=="" then |
| 12300 |
trysocket(nil,"invalid host '"..tostring(host).."'") |
| 12301 |
end |
| 12302 |
if port=="" then |
| 12303 |
request.port=PORT |
| 12304 |
end |
| 12305 |
if not uri or uri=="" then |
| 12306 |
request.uri=adjusturi(request) |
| 12307 |
end |
| 12308 |
request.headers=adjustheaders(request) |
| 12309 |
local proxy=request.proxy or http.PROXY |
| 12310 |
if proxy then |
| 12311 |
proxy=parseurl(proxy) |
| 12312 |
request.host=proxy.host |
| 12313 |
request.port=proxy.port or 3128 |
| 12314 |
end |
| 12315 |
return request |
| 12316 |
end |
| 12317 |
local maxredericts=5 |
| 12318 |
local validredirects={ [301]=true,[302]=true,[303]=true,[307]=true } |
| 12319 |
local validmethods={ [false]=true,GET=true,HEAD=true } |
| 12320 |
local function shouldredirect(request,code,headers) |
| 12321 |
local location=headers.location |
| 12322 |
if not location then |
| 12323 |
return false |
| 12324 |
end |
| 12325 |
location=gsub(location,"%s","") |
| 12326 |
if location=="" then |
| 12327 |
return false |
| 12328 |
end |
| 12329 |
local scheme=match(location,"^([%w][%w%+%-%.]*)%:") |
| 12330 |
if scheme and not SCHEMES[scheme] then |
| 12331 |
return false |
| 12332 |
end |
| 12333 |
local method=request.method |
| 12334 |
local redirect=request.redirect |
| 12335 |
local redirects=request.nredirects or 0 |
| 12336 |
local maxredirects=request.maxredirects or maxredirects |
| 12337 |
return redirect and validredirects[code] and validmethods[method] and redirects<=maxredirects |
| 12338 |
end |
| 12339 |
local function shouldreceivebody(request,code) |
| 12340 |
if request.method=="HEAD" then |
| 12341 |
return nil |
| 12342 |
end |
| 12343 |
if code==204 or code==304 then |
| 12344 |
return nil |
| 12345 |
end |
| 12346 |
if code>=100 and code<200 then |
| 12347 |
return nil |
| 12348 |
end |
| 12349 |
return 1 |
| 12350 |
end |
| 12351 |
local tredirect,trequest,srequest |
| 12352 |
tredirect=function(request,location) |
| 12353 |
local result,code,headers,status=trequest { |
| 12354 |
url=absoluteurl(request.url,location), |
| 12355 |
source=request.source, |
| 12356 |
sink=request.sink, |
| 12357 |
headers=request.headers, |
| 12358 |
proxy=request.proxy, |
| 12359 |
nredirects=(request.nredirects or 0)+1, |
| 12360 |
maxredirects=request.maxredirects or maxredirects, |
| 12361 |
create=request.create, |
| 12362 |
} |
| 12363 |
if not headers then |
| 12364 |
headers={} |
| 12365 |
end |
| 12366 |
if not headers.location then |
| 12367 |
headers.location=location |
| 12368 |
end |
| 12369 |
return result,code,headers,status |
| 12370 |
end |
| 12371 |
trequest=function(originalrequest) |
| 12372 |
local request=adjustrequest(originalrequest) |
| 12373 |
local connection=openhttp(request.host,request.port,request.create) |
| 12374 |
local headers=request.headers |
| 12375 |
connection:sendrequestline(request.method,request.uri) |
| 12376 |
connection:sendheaders(headers) |
| 12377 |
if request.source then |
| 12378 |
connection:sendbody(headers,request.source,request.step) |
| 12379 |
end |
| 12380 |
local code,status=connection:receivestatusline() |
| 12381 |
if not code then |
| 12382 |
connection:receive09body(status,request.sink,request.step) |
| 12383 |
connection:close() |
| 12384 |
return 1,200 |
| 12385 |
elseif code==408 then |
| 12386 |
return 1,code |
| 12387 |
end |
| 12388 |
while code==100 do |
| 12389 |
connection:receiveheaders() |
| 12390 |
code,status=connection:receivestatusline() |
| 12391 |
end |
| 12392 |
headers=connection:receiveheaders() |
| 12393 |
if shouldredirect(request,code,headers) and not request.source then |
| 12394 |
connection:close() |
| 12395 |
return tredirect(originalrequest,headers.location) |
| 12396 |
end |
| 12397 |
if shouldreceivebody(request,code) then |
| 12398 |
connection:receivebody(headers,request.sink,request.step) |
| 12399 |
end |
| 12400 |
connection:close() |
| 12401 |
return 1,code,headers,status |
| 12402 |
end |
| 12403 |
local function genericform(url,body) |
| 12404 |
local buffer={} |
| 12405 |
local request={ |
| 12406 |
url=url, |
| 12407 |
sink=sinktable(buffer), |
| 12408 |
target=buffer, |
| 12409 |
} |
| 12410 |
if body then |
| 12411 |
request.source=stringsource(body) |
| 12412 |
request.method="POST" |
| 12413 |
request.headers={ |
| 12414 |
["content-length"]=#body, |
| 12415 |
["content-type"]="application/x-www-form-urlencoded" |
| 12416 |
} |
| 12417 |
end |
| 12418 |
return request |
| 12419 |
end |
| 12420 |
http.genericform=genericform |
| 12421 |
srequest=function(url,body) |
| 12422 |
local request=genericform(url,body) |
| 12423 |
local _,code,headers,status=trequest(request) |
| 12424 |
return concat(request.target),code,headers,status |
| 12425 |
end |
| 12426 |
http.request=protectsocket(function(request,body) |
| 12427 |
if type(request)=="string" then |
| 12428 |
return srequest(request,body) |
| 12429 |
else |
| 12430 |
return trequest(request) |
| 12431 |
end |
| 12432 |
end) |
| 12433 |
package.loaded["socket.http"]=http |
| 12434 |
|
| 12435 |
|
| 12436 |
end -- of closure |
| 12437 |
|
| 12438 |
do -- create closure to overcome 200 locals limit |
| 12439 |
|
| 12440 |
package.loaded["util-soc-imp-ftp"] = package.loaded["util-soc-imp-ftp"] or true |
| 12441 |
|
| 12442 |
-- original size: 10345, stripped down to: 8538 |
| 12443 |
|
| 12444 |
|
| 12445 |
local setmetatable,type,next=setmetatable,type,next |
| 12446 |
local find,format,gsub,match=string.find,string.format,string.gsub,string.match |
| 12447 |
local concat=table.concat |
| 12448 |
local mod=math.mod |
| 12449 |
local socket=socket or require("socket") |
| 12450 |
local url=socket.url or require("socket.url") |
| 12451 |
local tp=socket.tp or require("socket.tp") |
| 12452 |
local ltn12=ltn12 or require("ltn12") |
| 12453 |
local tcpsocket=socket.tcp |
| 12454 |
local trysocket=socket.try |
| 12455 |
local skipsocket=socket.skip |
| 12456 |
local sinksocket=socket.sink |
| 12457 |
local selectsocket=socket.select |
| 12458 |
local bindsocket=socket.bind |
| 12459 |
local newtrysocket=socket.newtry |
| 12460 |
local sourcesocket=socket.source |
| 12461 |
local protectsocket=socket.protect |
| 12462 |
local parseurl=url.parse |
| 12463 |
local unescapeurl=url.unescape |
| 12464 |
local pumpall=ltn12.pump.all |
| 12465 |
local pumpstep=ltn12.pump.step |
| 12466 |
local sourcestring=ltn12.source.string |
| 12467 |
local sinktable=ltn12.sink.table |
| 12468 |
local ftp={ |
| 12469 |
TIMEOUT=60, |
| 12470 |
USER="ftp", |
| 12471 |
PASSWORD="[email protected]", |
| 12472 |
} |
| 12473 |
socket.ftp=ftp |
| 12474 |
local PORT=21 |
| 12475 |
local methods={} |
| 12476 |
local mt={ __index=methods } |
| 12477 |
function ftp.open(server,port,create) |
| 12478 |
local tp=trysocket(tp.connect(server,port or PORT,ftp.TIMEOUT,create)) |
| 12479 |
local f=setmetatable({ tp=tp },metat) |
| 12480 |
f.try=newtrysocket(function() f:close() end) |
| 12481 |
return f |
| 12482 |
end |
| 12483 |
function methods.portconnect(self) |
| 12484 |
local try=self.try |
| 12485 |
local server=self.server |
| 12486 |
try(server:settimeout(ftp.TIMEOUT)) |
| 12487 |
self.data=try(server:accept()) |
| 12488 |
try(self.data:settimeout(ftp.TIMEOUT)) |
| 12489 |
end |
| 12490 |
function methods.pasvconnect(self) |
| 12491 |
local try=self.try |
| 12492 |
self.data=try(tcpsocket()) |
| 12493 |
self(self.data:settimeout(ftp.TIMEOUT)) |
| 12494 |
self(self.data:connect(self.pasvt.address,self.pasvt.port)) |
| 12495 |
end |
| 12496 |
function methods.login(self,user,password) |
| 12497 |
local try=self.try |
| 12498 |
local tp=self.tp |
| 12499 |
try(tp:command("user",user or ftp.USER)) |
| 12500 |
local code,reply=try(tp:check{"2..",331}) |
| 12501 |
if code==331 then |
| 12502 |
try(tp:command("pass",password or ftp.PASSWORD)) |
| 12503 |
try(tp:check("2..")) |
| 12504 |
end |
| 12505 |
return 1 |
| 12506 |
end |
| 12507 |
function methods.pasv(self) |
| 12508 |
local try=self.try |
| 12509 |
local tp=self.tp |
| 12510 |
try(tp:command("pasv")) |
| 12511 |
local code,reply=try(self.tp:check("2..")) |
| 12512 |
local pattern="(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)" |
| 12513 |
local a,b,c,d,p1,p2=skipsocket(2,find(reply,pattern)) |
| 12514 |
try(a and b and c and d and p1 and p2,reply) |
| 12515 |
local address=format("%d.%d.%d.%d",a,b,c,d) |
| 12516 |
local port=p1*256+p2 |
| 12517 |
local server=self.server |
| 12518 |
self.pasvt={ |
| 12519 |
address=address, |
| 12520 |
port=port, |
| 12521 |
} |
| 12522 |
if server then |
| 12523 |
server:close() |
| 12524 |
self.server=nil |
| 12525 |
end |
| 12526 |
return address,port |
| 12527 |
end |
| 12528 |
function methods.epsv(self) |
| 12529 |
local try=self.try |
| 12530 |
local tp=self.tp |
| 12531 |
try(tp:command("epsv")) |
| 12532 |
local code,reply=try(tp:check("229")) |
| 12533 |
local pattern="%((.)(.-)%1(.-)%1(.-)%1%)" |
| 12534 |
local d,prt,address,port=match(reply,pattern) |
| 12535 |
try(port,"invalid epsv response") |
| 12536 |
local address=tp:getpeername() |
| 12537 |
local server=self.server |
| 12538 |
self.pasvt={ |
| 12539 |
address=address, |
| 12540 |
port=port, |
| 12541 |
} |
| 12542 |
if self.server then |
| 12543 |
server:close() |
| 12544 |
self.server=nil |
| 12545 |
end |
| 12546 |
return address,port |
| 12547 |
end |
| 12548 |
function methods.port(self,address,port) |
| 12549 |
local try=self.try |
| 12550 |
local tp=self.tp |
| 12551 |
self.pasvt=nil |
| 12552 |
if not address then |
| 12553 |
address=try(tp:getsockname()) |
| 12554 |
self.server=try(bindsocket(address,0)) |
| 12555 |
address,port=try(self.server:getsockname()) |
| 12556 |
try(self.server:settimeout(ftp.TIMEOUT)) |
| 12557 |
end |
| 12558 |
local pl=mod(port,256) |
| 12559 |
local ph=(port-pl)/256 |
| 12560 |
local arg=gsub(format("%s,%d,%d",address,ph,pl),"%.",",") |
| 12561 |
try(tp:command("port",arg)) |
| 12562 |
try(tp:check("2..")) |
| 12563 |
return 1 |
| 12564 |
end |
| 12565 |
function methods.eprt(self,family,address,port) |
| 12566 |
local try=self.try |
| 12567 |
local tp=self.tp |
| 12568 |
self.pasvt=nil |
| 12569 |
if not address then |
| 12570 |
address=try(tp:getsockname()) |
| 12571 |
self.server=try(bindsocket(address,0)) |
| 12572 |
address,port=try(self.server:getsockname()) |
| 12573 |
try(self.server:settimeout(ftp.TIMEOUT)) |
| 12574 |
end |
| 12575 |
local arg=format("|%s|%s|%d|",family,address,port) |
| 12576 |
try(tp:command("eprt",arg)) |
| 12577 |
try(tp:check("2..")) |
| 12578 |
return 1 |
| 12579 |
end |
| 12580 |
function methods.send(self,sendt) |
| 12581 |
local try=self.try |
| 12582 |
local tp=self.tp |
| 12583 |
try(self.pasvt or self.server,"need port or pasv first") |
| 12584 |
if self.pasvt then |
| 12585 |
self:pasvconnect() |
| 12586 |
end |
| 12587 |
local argument=sendt.argument or unescapeurl(gsub(sendt.path or "","^[/\\]","")) |
| 12588 |
if argument=="" then |
| 12589 |
argument=nil |
| 12590 |
end |
| 12591 |
local command=sendt.command or "stor" |
| 12592 |
try(tp:command(command,argument)) |
| 12593 |
local code,reply=try(tp:check{"2..","1.."}) |
| 12594 |
if not self.pasvt then |
| 12595 |
self:portconnect() |
| 12596 |
end |
| 12597 |
local step=sendt.step or pumpstep |
| 12598 |
local readt={ tp } |
| 12599 |
local checkstep=function(src,snk) |
| 12600 |
local readyt=selectsocket(readt,nil,0) |
| 12601 |
if readyt[tp] then |
| 12602 |
code=try(tp:check("2..")) |
| 12603 |
end |
| 12604 |
return step(src,snk) |
| 12605 |
end |
| 12606 |
local sink=sinksocket("close-when-done",self.data) |
| 12607 |
try(pumpall(sendt.source,sink,checkstep)) |
| 12608 |
if find(code,"1..") then |
| 12609 |
try(tp:check("2..")) |
| 12610 |
end |
| 12611 |
self.data:close() |
| 12612 |
local sent=skipsocket(1,self.data:getstats()) |
| 12613 |
self.data=nil |
| 12614 |
return sent |
| 12615 |
end |
| 12616 |
function methods.receive(self,recvt) |
| 12617 |
local try=self.try |
| 12618 |
local tp=self.tp |
| 12619 |
try(self.pasvt or self.server,"need port or pasv first") |
| 12620 |
if self.pasvt then self:pasvconnect() end |
| 12621 |
local argument=recvt.argument or unescapeurl(gsub(recvt.path or "","^[/\\]","")) |
| 12622 |
if argument=="" then |
| 12623 |
argument=nil |
| 12624 |
end |
| 12625 |
local command=recvt.command or "retr" |
| 12626 |
try(tp:command(command,argument)) |
| 12627 |
local code,reply=try(tp:check{"1..","2.."}) |
| 12628 |
if code>=200 and code<=299 then |
| 12629 |
recvt.sink(reply) |
| 12630 |
return 1 |
| 12631 |
end |
| 12632 |
if not self.pasvt then |
| 12633 |
self:portconnect() |
| 12634 |
end |
| 12635 |
local source=sourcesocket("until-closed",self.data) |
| 12636 |
local step=recvt.step or pumpstep |
| 12637 |
try(pumpall(source,recvt.sink,step)) |
| 12638 |
if find(code,"1..") then |
| 12639 |
try(tp:check("2..")) |
| 12640 |
end |
| 12641 |
self.data:close() |
| 12642 |
self.data=nil |
| 12643 |
return 1 |
| 12644 |
end |
| 12645 |
function methods.cwd(self,dir) |
| 12646 |
local try=self.try |
| 12647 |
local tp=self.tp |
| 12648 |
try(tp:command("cwd",dir)) |
| 12649 |
try(tp:check(250)) |
| 12650 |
return 1 |
| 12651 |
end |
| 12652 |
function methods.type(self,typ) |
| 12653 |
local try=self.try |
| 12654 |
local tp=self.tp |
| 12655 |
try(tp:command("type",typ)) |
| 12656 |
try(tp:check(200)) |
| 12657 |
return 1 |
| 12658 |
end |
| 12659 |
function methods.greet(self) |
| 12660 |
local try=self.try |
| 12661 |
local tp=self.tp |
| 12662 |
local code=try(tp:check{"1..","2.."}) |
| 12663 |
if find(code,"1..") then |
| 12664 |
try(tp:check("2..")) |
| 12665 |
end |
| 12666 |
return 1 |
| 12667 |
end |
| 12668 |
function methods.quit(self) |
| 12669 |
local try=self.try |
| 12670 |
try(self.tp:command("quit")) |
| 12671 |
try(self.tp:check("2..")) |
| 12672 |
return 1 |
| 12673 |
end |
| 12674 |
function methods.close(self) |
| 12675 |
local data=self.data |
| 12676 |
if data then |
| 12677 |
data:close() |
| 12678 |
end |
| 12679 |
local server=self.server |
| 12680 |
if server then |
| 12681 |
server:close() |
| 12682 |
end |
| 12683 |
local tp=self.tp |
| 12684 |
if tp then |
| 12685 |
tp:close() |
| 12686 |
end |
| 12687 |
end |
| 12688 |
local function override(t) |
| 12689 |
if t.url then |
| 12690 |
local u=parseurl(t.url) |
| 12691 |
for k,v in next,t do |
| 12692 |
u[k]=v |
| 12693 |
end |
| 12694 |
return u |
| 12695 |
else |
| 12696 |
return t |
| 12697 |
end |
| 12698 |
end |
| 12699 |
local function tput(putt) |
| 12700 |
putt=override(putt) |
| 12701 |
local host=putt.host |
| 12702 |
trysocket(host,"missing hostname") |
| 12703 |
local f=ftp.open(host,putt.port,putt.create) |
| 12704 |
f:greet() |
| 12705 |
f:login(putt.user,putt.password) |
| 12706 |
local typ=putt.type |
| 12707 |
if typ then |
| 12708 |
f:type(typ) |
| 12709 |
end |
| 12710 |
f:epsv() |
| 12711 |
local sent=f:send(putt) |
| 12712 |
f:quit() |
| 12713 |
f:close() |
| 12714 |
return sent |
| 12715 |
end |
| 12716 |
local default={ |
| 12717 |
path="/", |
| 12718 |
scheme="ftp", |
| 12719 |
} |
| 12720 |
local function genericform(u) |
| 12721 |
local t=trysocket(parseurl(u,default)) |
| 12722 |
trysocket(t.scheme=="ftp","wrong scheme '"..t.scheme.."'") |
| 12723 |
trysocket(t.host,"missing hostname") |
| 12724 |
local pat="^type=(.)$" |
| 12725 |
if t.params then |
| 12726 |
local typ=skipsocket(2,find(t.params,pat)) |
| 12727 |
t.type=typ |
| 12728 |
trysocket(typ=="a" or typ=="i","invalid type '"..typ.."'") |
| 12729 |
end |
| 12730 |
return t |
| 12731 |
end |
| 12732 |
ftp.genericform=genericform |
| 12733 |
local function sput(u,body) |
| 12734 |
local putt=genericform(u) |
| 12735 |
putt.source=sourcestring(body) |
| 12736 |
return tput(putt) |
| 12737 |
end |
| 12738 |
ftp.put=protectsocket(function(putt,body) |
| 12739 |
if type(putt)=="string" then |
| 12740 |
return sput(putt,body) |
| 12741 |
else |
| 12742 |
return tput(putt) |
| 12743 |
end |
| 12744 |
end) |
| 12745 |
local function tget(gett) |
| 12746 |
gett=override(gett) |
| 12747 |
local host=gett.host |
| 12748 |
trysocket(host,"missing hostname") |
| 12749 |
local f=ftp.open(host,gett.port,gett.create) |
| 12750 |
f:greet() |
| 12751 |
f:login(gett.user,gett.password) |
| 12752 |
if gett.type then |
| 12753 |
f:type(gett.type) |
| 12754 |
end |
| 12755 |
f:epsv() |
| 12756 |
f:receive(gett) |
| 12757 |
f:quit() |
| 12758 |
return f:close() |
| 12759 |
end |
| 12760 |
local function sget(u) |
| 12761 |
local gett=genericform(u) |
| 12762 |
local t={} |
| 12763 |
gett.sink=sinktable(t) |
| 12764 |
tget(gett) |
| 12765 |
return concat(t) |
| 12766 |
end |
| 12767 |
ftp.command=protectsocket(function(cmdt) |
| 12768 |
cmdt=override(cmdt) |
| 12769 |
local command=cmdt.command |
| 12770 |
local argument=cmdt.argument |
| 12771 |
local check=cmdt.check |
| 12772 |
local host=cmdt.host |
| 12773 |
trysocket(host,"missing hostname") |
| 12774 |
trysocket(command,"missing command") |
| 12775 |
local f=ftp.open(host,cmdt.port,cmdt.create) |
| 12776 |
local try=f.try |
| 12777 |
local tp=f.tp |
| 12778 |
f:greet() |
| 12779 |
f:login(cmdt.user,cmdt.password) |
| 12780 |
if type(command)=="table" then |
| 12781 |
local argument=argument or {} |
| 12782 |
for i=1,#command do |
| 12783 |
local cmd=command[i] |
| 12784 |
try(tp:command(cmd,argument[i])) |
| 12785 |
if check and check[i] then |
| 12786 |
try(tp:check(check[i])) |
| 12787 |
end |
| 12788 |
end |
| 12789 |
else |
| 12790 |
try(tp:command(command,argument)) |
| 12791 |
if check then |
| 12792 |
try(tp:check(check)) |
| 12793 |
end |
| 12794 |
end |
| 12795 |
f:quit() |
| 12796 |
return f:close() |
| 12797 |
end) |
| 12798 |
ftp.get=protectsocket(function(gett) |
| 12799 |
if type(gett)=="string" then |
| 12800 |
return sget(gett) |
| 12801 |
else |
| 12802 |
return tget(gett) |
| 12803 |
end |
| 12804 |
end) |
| 12805 |
package.loaded["socket.ftp"]=ftp |
| 12806 |
|
| 12807 |
|
| 12808 |
end -- of closure |
| 12809 |
|
| 12810 |
do -- create closure to overcome 200 locals limit |
| 12811 |
|
| 12812 |
package.loaded["util-soc-imp-smtp"] = package.loaded["util-soc-imp-smtp"] or true |
| 12813 |
|
| 12814 |
-- original size: 7018, stripped down to: 5883 |
| 12815 |
|
| 12816 |
|
| 12817 |
local type,setmetatable,next=type,setmetatable,next |
| 12818 |
local find,lower,format=string.find,string.lower,string.format |
| 12819 |
local osdate,osgetenv=os.date,os.getenv |
| 12820 |
local random=math.random |
| 12821 |
local socket=socket or require("socket") |
| 12822 |
local headers=socket.headers or require("socket.headers") |
| 12823 |
local ltn12=ltn12 or require("ltn12") |
| 12824 |
local tp=socket.tp or require("socket.tp") |
| 12825 |
local mime=mime or require("mime") |
| 12826 |
local mimeb64=mime.b64 |
| 12827 |
local mimestuff=mime.stuff |
| 12828 |
local skipsocket=socket.skip |
| 12829 |
local trysocket=socket.try |
| 12830 |
local newtrysocket=socket.newtry |
| 12831 |
local protectsocket=socket.protect |
| 12832 |
local normalizeheaders=headers.normalize |
| 12833 |
local lowerheaders=headers.lower |
| 12834 |
local createcoroutine=coroutine.create |
| 12835 |
local resumecoroutine=coroutine.resume |
| 12836 |
local yieldcoroutine=coroutine.resume |
| 12837 |
local smtp={ |
| 12838 |
TIMEOUT=60, |
| 12839 |
SERVER="localhost", |
| 12840 |
PORT=25, |
| 12841 |
DOMAIN=osgetenv("SERVER_NAME") or "localhost", |
| 12842 |
ZONE="-0000", |
| 12843 |
} |
| 12844 |
socket.smtp=smtp |
| 12845 |
local methods={} |
| 12846 |
local mt={ __index=methods } |
| 12847 |
function methods.greet(self,domain) |
| 12848 |
local try=self.try |
| 12849 |
local tp=self.tp |
| 12850 |
try(tp:check("2..")) |
| 12851 |
try(tp:command("EHLO",domain or _M.DOMAIN)) |
| 12852 |
return skipsocket(1,try(tp:check("2.."))) |
| 12853 |
end |
| 12854 |
function methods.mail(self,from) |
| 12855 |
local try=self.try |
| 12856 |
local tp=self.tp |
| 12857 |
try(tp:command("MAIL","FROM:"..from)) |
| 12858 |
return try(tp:check("2..")) |
| 12859 |
end |
| 12860 |
function methods.rcpt(self,to) |
| 12861 |
local try=self.try |
| 12862 |
local tp=self.tp |
| 12863 |
try(tp:command("RCPT","TO:"..to)) |
| 12864 |
return try(tp:check("2..")) |
| 12865 |
end |
| 12866 |
function methods.data(self,src,step) |
| 12867 |
local try=self.try |
| 12868 |
local tp=self.tp |
| 12869 |
try(tp:command("DATA")) |
| 12870 |
try(tp:check("3..")) |
| 12871 |
try(tp:source(src,step)) |
| 12872 |
try(tp:send("\r\n.\r\n")) |
| 12873 |
return try(tp:check("2..")) |
| 12874 |
end |
| 12875 |
function methods.quit(self) |
| 12876 |
local try=self.try |
| 12877 |
local tp=self.tp |
| 12878 |
try(tp:command("QUIT")) |
| 12879 |
return try(tp:check("2..")) |
| 12880 |
end |
| 12881 |
function methods.close(self) |
| 12882 |
return self.tp:close() |
| 12883 |
end |
| 12884 |
function methods.login(self,user,password) |
| 12885 |
local try=self.try |
| 12886 |
local tp=self.tp |
| 12887 |
try(tp:command("AUTH","LOGIN")) |
| 12888 |
try(tp:check("3..")) |
| 12889 |
try(tp:send(mimeb64(user).."\r\n")) |
| 12890 |
try(tp:check("3..")) |
| 12891 |
try(tp:send(mimeb64(password).."\r\n")) |
| 12892 |
return try(tp:check("2..")) |
| 12893 |
end |
| 12894 |
function methods.plain(self,user,password) |
| 12895 |
local try=self.try |
| 12896 |
local tp=self.tp |
| 12897 |
local auth="PLAIN "..mimeb64("\0"..user.."\0"..password) |
| 12898 |
try(tp:command("AUTH",auth)) |
| 12899 |
return try(tp:check("2..")) |
| 12900 |
end |
| 12901 |
function methods.auth(self,user,password,ext) |
| 12902 |
if not user or not password then |
| 12903 |
return 1 |
| 12904 |
end |
| 12905 |
local try=self.try |
| 12906 |
if find(ext,"AUTH[^\n]+LOGIN") then |
| 12907 |
return self:login(user,password) |
| 12908 |
elseif find(ext,"AUTH[^\n]+PLAIN") then |
| 12909 |
return self:plain(user,password) |
| 12910 |
else |
| 12911 |
try(nil,"authentication not supported") |
| 12912 |
end |
| 12913 |
end |
| 12914 |
function methods.send(self,mail) |
| 12915 |
self:mail(mail.from) |
| 12916 |
local receipt=mail.rcpt |
| 12917 |
if type(receipt)=="table" then |
| 12918 |
for i=1,#receipt do |
| 12919 |
self:rcpt(receipt[i]) |
| 12920 |
end |
| 12921 |
elseif receipt then |
| 12922 |
self:rcpt(receipt) |
| 12923 |
end |
| 12924 |
self:data(ltn12.source.chain(mail.source,mimestuff()),mail.step) |
| 12925 |
end |
| 12926 |
local function opensmtp(self,server,port,create) |
| 12927 |
if not server or server=="" then |
| 12928 |
server=smtp.SERVER |
| 12929 |
end |
| 12930 |
if not port or port=="" then |
| 12931 |
port=smtp.PORT |
| 12932 |
end |
| 12933 |
local s={ |
| 12934 |
tp=trysocket(tp.connect(server,port,smtp.TIMEOUT,create)), |
| 12935 |
try=newtrysocket(function() |
| 12936 |
s:close() |
| 12937 |
end), |
| 12938 |
} |
| 12939 |
setmetatable(s,mt) |
| 12940 |
return s |
| 12941 |
end |
| 12942 |
smtp.open=opensmtp |
| 12943 |
local nofboundaries=0 |
| 12944 |
local function newboundary() |
| 12945 |
nofboundaries=nofboundaries+1 |
| 12946 |
return format('%s%05d==%05u',osdate('%d%m%Y%H%M%S'),random(0,99999),nofboundaries) |
| 12947 |
end |
| 12948 |
local send_message |
| 12949 |
local function send_headers(headers) |
| 12950 |
yieldcoroutine(normalizeheaders(headers)) |
| 12951 |
end |
| 12952 |
local function send_multipart(message) |
| 12953 |
local boundary=newboundary() |
| 12954 |
local headers=lowerheaders(message.headers) |
| 12955 |
local body=message.body |
| 12956 |
local preamble=body.preamble |
| 12957 |
local epilogue=body.epilogue |
| 12958 |
local content=headers['content-type'] or 'multipart/mixed' |
| 12959 |
headers['content-type']=content..'; boundary="'..boundary..'"' |
| 12960 |
send_headers(headers) |
| 12961 |
if preamble then |
| 12962 |
yieldcoroutine(preamble) |
| 12963 |
yieldcoroutine("\r\n") |
| 12964 |
end |
| 12965 |
for i=1,#body do |
| 12966 |
yieldcoroutine("\r\n--"..boundary.."\r\n") |
| 12967 |
send_message(body[i]) |
| 12968 |
end |
| 12969 |
yieldcoroutine("\r\n--"..boundary.."--\r\n\r\n") |
| 12970 |
if epilogue then |
| 12971 |
yieldcoroutine(epilogue) |
| 12972 |
yieldcoroutine("\r\n") |
| 12973 |
end |
| 12974 |
end |
| 12975 |
local default_content_type='text/plain; charset="UTF-8"' |
| 12976 |
local function send_source(message) |
| 12977 |
local headers=lowerheaders(message.headers) |
| 12978 |
if not headers['content-type'] then |
| 12979 |
headers['content-type']=default_content_type |
| 12980 |
end |
| 12981 |
send_headers(headers) |
| 12982 |
local getchunk=message.body |
| 12983 |
while true do |
| 12984 |
local chunk,err=getchunk() |
| 12985 |
if err then |
| 12986 |
yieldcoroutine(nil,err) |
| 12987 |
elseif chunk then |
| 12988 |
yieldcoroutine(chunk) |
| 12989 |
else |
| 12990 |
break |
| 12991 |
end |
| 12992 |
end |
| 12993 |
end |
| 12994 |
local function send_string(message) |
| 12995 |
local headers=lowerheaders(message.headers) |
| 12996 |
if not headers['content-type'] then |
| 12997 |
headers['content-type']=default_content_type |
| 12998 |
end |
| 12999 |
send_headers(headers) |
| 13000 |
yieldcoroutine(message.body) |
| 13001 |
end |
| 13002 |
function send_message(message) |
| 13003 |
local body=message.body |
| 13004 |
if type(body)=="table" then |
| 13005 |
send_multipart(message) |
| 13006 |
elseif type(body)=="function" then |
| 13007 |
send_source(message) |
| 13008 |
else |
| 13009 |
send_string(message) |
| 13010 |
end |
| 13011 |
end |
| 13012 |
local function adjust_headers(message) |
| 13013 |
local headers=lowerheaders(message.headers) |
| 13014 |
if not headers["date"] then |
| 13015 |
headers["date"]=osdate("!%a, %d %b %Y %H:%M:%S ")..(message.zone or smtp.ZONE) |
| 13016 |
end |
| 13017 |
if not headers["x-mailer"] then |
| 13018 |
headers["x-mailer"]=socket._VERSION |
| 13019 |
end |
| 13020 |
headers["mime-version"]="1.0" |
| 13021 |
return headers |
| 13022 |
end |
| 13023 |
function smtp.message(message) |
| 13024 |
message.headers=adjust_headers(message) |
| 13025 |
local action=createcoroutine(function() |
| 13026 |
send_message(message) |
| 13027 |
end) |
| 13028 |
return function() |
| 13029 |
local ret,a,b=resumecoroutine(action) |
| 13030 |
if ret then |
| 13031 |
return a,b |
| 13032 |
else |
| 13033 |
return nil,a |
| 13034 |
end |
| 13035 |
end |
| 13036 |
end |
| 13037 |
smtp.send=protectsocket(function(mail) |
| 13038 |
local snd=opensmtp(smtp,mail.server,mail.port,mail.create) |
| 13039 |
local ext=snd:greet(mail.domain) |
| 13040 |
snd:auth(mail.user,mail.password,ext) |
| 13041 |
snd:send(mail) |
| 13042 |
snd:quit() |
| 13043 |
return snd:close() |
| 13044 |
end) |
| 13045 |
package.loaded["socket.smtp"]=smtp |
| 13046 |
|
| 13047 |
|
| 13048 |
end -- of closure |
| 13049 |
|
| 13050 |
do -- create closure to overcome 200 locals limit |
| 13051 |
|
| 13052 |
package.loaded["trac-set"] = package.loaded["trac-set"] or true |
| 13053 |
|
| 13054 |
-- original size: 14574, stripped down to: 9650 |
| 13055 |
|
| 13056 |
if not modules then modules={} end modules ['trac-set']={ |
| 13057 |
version=1.001, |
| 13058 |
comment="companion to luat-lib.mkiv", |
| 13059 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 13060 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 13061 |
license="see context related readme files" |
| 13062 |
} |
| 13063 |
local type,next,tostring,tonumber=type,next,tostring,tonumber |
| 13064 |
local print=print |
| 13065 |
local concat,sortedhash=table.concat,table.sortedhash |
| 13066 |
local formatters,find,lower,gsub,topattern=string.formatters,string.find,string.lower,string.gsub,string.topattern |
| 13067 |
local is_boolean=string.is_boolean |
| 13068 |
local settings_to_hash=utilities.parsers.settings_to_hash |
| 13069 |
local allocate=utilities.storage.allocate |
| 13070 |
utilities=utilities or {} |
| 13071 |
local utilities=utilities |
| 13072 |
local setters=utilities.setters or {} |
| 13073 |
utilities.setters=setters |
| 13074 |
local data={} |
| 13075 |
local trace_initialize=false |
| 13076 |
local frozen=true |
| 13077 |
local function initialize_setter(filename,name,values) |
| 13078 |
local setter=data[name] |
| 13079 |
if setter then |
| 13080 |
local data=setter.data |
| 13081 |
if data then |
| 13082 |
for key,newvalue in sortedhash(values) do |
| 13083 |
local newvalue=is_boolean(newvalue,newvalue,true) |
| 13084 |
local functions=data[key] |
| 13085 |
if functions then |
| 13086 |
local oldvalue=functions.value |
| 13087 |
if functions.frozen then |
| 13088 |
if trace_initialize then |
| 13089 |
setter.report("%s: %a is %s to %a",filename,key,"frozen",oldvalue) |
| 13090 |
end |
| 13091 |
elseif #functions>0 and not oldvalue then |
| 13092 |
if trace_initialize then |
| 13093 |
setter.report("%s: %a is %s to %a",filename,key,"set",newvalue) |
| 13094 |
end |
| 13095 |
for i=1,#functions do |
| 13096 |
functions[i](newvalue) |
| 13097 |
end |
| 13098 |
functions.value=newvalue |
| 13099 |
functions.frozen=functions.frozen or frozen |
| 13100 |
else |
| 13101 |
if trace_initialize then |
| 13102 |
setter.report("%s: %a is %s as %a",filename,key,"kept",oldvalue) |
| 13103 |
end |
| 13104 |
end |
| 13105 |
else |
| 13106 |
functions={ default=newvalue,frozen=frozen } |
| 13107 |
data[key]=functions |
| 13108 |
if trace_initialize then |
| 13109 |
setter.report("%s: %a is %s to %a",filename,key,"defaulted",newvalue) |
| 13110 |
end |
| 13111 |
end |
| 13112 |
end |
| 13113 |
return true |
| 13114 |
end |
| 13115 |
end |
| 13116 |
end |
| 13117 |
local function set(t,what,newvalue) |
| 13118 |
local data=t.data |
| 13119 |
if data and not data.frozen then |
| 13120 |
local done=t.done |
| 13121 |
if type(what)=="string" then |
| 13122 |
what=settings_to_hash(what) |
| 13123 |
end |
| 13124 |
if type(what)~="table" then |
| 13125 |
return |
| 13126 |
end |
| 13127 |
if not done then |
| 13128 |
done={} |
| 13129 |
t.done=done |
| 13130 |
end |
| 13131 |
for w,value in sortedhash(what) do |
| 13132 |
if value=="" then |
| 13133 |
value=newvalue |
| 13134 |
elseif not value then |
| 13135 |
value=false |
| 13136 |
else |
| 13137 |
value=is_boolean(value,value,true) |
| 13138 |
end |
| 13139 |
local p=topattern(w,true,true) |
| 13140 |
for name,functions in sortedhash(data) do |
| 13141 |
if done[name] then |
| 13142 |
elseif find(name,p) then |
| 13143 |
done[name]=true |
| 13144 |
for i=1,#functions do |
| 13145 |
functions[i](value) |
| 13146 |
end |
| 13147 |
functions.value=value |
| 13148 |
end |
| 13149 |
end |
| 13150 |
end |
| 13151 |
end |
| 13152 |
end |
| 13153 |
local function reset(t) |
| 13154 |
local data=t.data |
| 13155 |
if data and not data.frozen then |
| 13156 |
for name,functions in sortedthash(data) do |
| 13157 |
for i=1,#functions do |
| 13158 |
functions[i](false) |
| 13159 |
end |
| 13160 |
functions.value=false |
| 13161 |
end |
| 13162 |
end |
| 13163 |
end |
| 13164 |
local function enable(t,what) |
| 13165 |
set(t,what,true) |
| 13166 |
end |
| 13167 |
local function disable(t,what) |
| 13168 |
local data=t.data |
| 13169 |
if not what or what=="" then |
| 13170 |
t.done={} |
| 13171 |
reset(t) |
| 13172 |
else |
| 13173 |
set(t,what,false) |
| 13174 |
end |
| 13175 |
end |
| 13176 |
local function register_setter(t,what,...) |
| 13177 |
local data=t.data |
| 13178 |
what=lower(what) |
| 13179 |
local functions=data[what] |
| 13180 |
if not functions then |
| 13181 |
functions={} |
| 13182 |
data[what]=functions |
| 13183 |
if trace_initialize then |
| 13184 |
t.report("defining %a",what) |
| 13185 |
end |
| 13186 |
end |
| 13187 |
local default=functions.default |
| 13188 |
for i=1,select("#",...) do |
| 13189 |
local fnc=select(i,...) |
| 13190 |
local typ=type(fnc) |
| 13191 |
if typ=="string" then |
| 13192 |
if trace_initialize then |
| 13193 |
t.report("coupling %a to %a",what,fnc) |
| 13194 |
end |
| 13195 |
local s=fnc |
| 13196 |
fnc=function(value) set(t,s,value) end |
| 13197 |
elseif typ=="table" then |
| 13198 |
functions.values=fnc |
| 13199 |
fnc=nil |
| 13200 |
elseif typ~="function" then |
| 13201 |
fnc=nil |
| 13202 |
end |
| 13203 |
if fnc then |
| 13204 |
functions[#functions+1]=fnc |
| 13205 |
local value=functions.value or default |
| 13206 |
if value~=nil then |
| 13207 |
fnc(value) |
| 13208 |
functions.value=value |
| 13209 |
end |
| 13210 |
end |
| 13211 |
end |
| 13212 |
return false |
| 13213 |
end |
| 13214 |
local function enable_setter(t,what) |
| 13215 |
local e=t.enable |
| 13216 |
t.enable,t.done=enable,{} |
| 13217 |
set(t,what,true) |
| 13218 |
enable(t,what) |
| 13219 |
t.enable,t.done=e,{} |
| 13220 |
end |
| 13221 |
local function disable_setter(t,what) |
| 13222 |
local e=t.disable |
| 13223 |
t.disable,t.done=disable,{} |
| 13224 |
disable(t,what) |
| 13225 |
t.disable,t.done=e,{} |
| 13226 |
end |
| 13227 |
local function reset_setter(t) |
| 13228 |
t.done={} |
| 13229 |
reset(t) |
| 13230 |
end |
| 13231 |
local function list_setter(t) |
| 13232 |
local list=table.sortedkeys(t.data) |
| 13233 |
local user,system={},{} |
| 13234 |
for l=1,#list do |
| 13235 |
local what=list[l] |
| 13236 |
if find(what,"^%*") then |
| 13237 |
system[#system+1]=what |
| 13238 |
else |
| 13239 |
user[#user+1]=what |
| 13240 |
end |
| 13241 |
end |
| 13242 |
return user,system |
| 13243 |
end |
| 13244 |
local function show_setter(t,pattern) |
| 13245 |
local list=list_setter(t) |
| 13246 |
t.report() |
| 13247 |
for k=1,#list do |
| 13248 |
local name=list[k] |
| 13249 |
if not pattern or find(name,pattern) then |
| 13250 |
local functions=t.data[name] |
| 13251 |
if functions then |
| 13252 |
local value=functions.value |
| 13253 |
local default=functions.default |
| 13254 |
local values=functions.values |
| 13255 |
local modules=#functions |
| 13256 |
if default==nil then |
| 13257 |
default="unset" |
| 13258 |
elseif type(default)=="table" then |
| 13259 |
default=concat(default,"|") |
| 13260 |
else |
| 13261 |
default=tostring(default) |
| 13262 |
end |
| 13263 |
if value==nil then |
| 13264 |
value="unset" |
| 13265 |
elseif type(value)=="table" then |
| 13266 |
value=concat(value,"|") |
| 13267 |
else |
| 13268 |
value=tostring(value) |
| 13269 |
end |
| 13270 |
t.report(name) |
| 13271 |
t.report(" modules : %i",modules) |
| 13272 |
t.report(" default : %s",default) |
| 13273 |
t.report(" value : %s",value) |
| 13274 |
if values then |
| 13275 |
local v={} for i=1,#values do v[i]=tostring(values[i]) end |
| 13276 |
t.report(" values : % t",v) |
| 13277 |
end |
| 13278 |
t.report() |
| 13279 |
end |
| 13280 |
end |
| 13281 |
end |
| 13282 |
end |
| 13283 |
function setters.report(setter,fmt,...) |
| 13284 |
if fmt then |
| 13285 |
print(formatters["%-15s : %s"](setter.name,formatters[fmt](...))) |
| 13286 |
else |
| 13287 |
print("") |
| 13288 |
end |
| 13289 |
end |
| 13290 |
local function setter_default(setter,name) |
| 13291 |
local d=setter.data[name] |
| 13292 |
return d and d.default |
| 13293 |
end |
| 13294 |
local function setter_value(setter,name) |
| 13295 |
local d=setter.data[name] |
| 13296 |
return d and (d.value or d.default) |
| 13297 |
end |
| 13298 |
local function setter_values(setter,name) |
| 13299 |
local d=setter.data[name] |
| 13300 |
return d and d.values |
| 13301 |
end |
| 13302 |
local function new_setter(name) |
| 13303 |
local setter |
| 13304 |
setter={ |
| 13305 |
data=allocate(), |
| 13306 |
name=name, |
| 13307 |
report=function(...) setters.report (setter,...) end, |
| 13308 |
enable=function(...) enable_setter (setter,...) end, |
| 13309 |
disable=function(...) disable_setter (setter,...) end, |
| 13310 |
reset=function(...) reset_setter (setter,...) end, |
| 13311 |
register=function(...) register_setter(setter,...) end, |
| 13312 |
list=function(...) return list_setter (setter,...) end, |
| 13313 |
show=function(...) show_setter (setter,...) end, |
| 13314 |
default=function(...) return setter_default (setter,...) end, |
| 13315 |
value=function(...) return setter_value (setter,...) end, |
| 13316 |
values=function(...) return setter_values (setter,...) end, |
| 13317 |
} |
| 13318 |
data[name]=setter |
| 13319 |
return setter |
| 13320 |
end |
| 13321 |
setters.enable=enable_setter |
| 13322 |
setters.disable=disable_setter |
| 13323 |
setters.register=register_setter |
| 13324 |
setters.list=list_setter |
| 13325 |
setters.show=show_setter |
| 13326 |
setters.reset=reset_setter |
| 13327 |
setters.new=new_setter |
| 13328 |
setters.initialize=initialize_setter |
| 13329 |
trackers=new_setter("trackers") |
| 13330 |
directives=new_setter("directives") |
| 13331 |
experiments=new_setter("experiments") |
| 13332 |
local t_enable,t_disable=trackers .enable,trackers .disable |
| 13333 |
local d_enable,d_disable=directives .enable,directives .disable |
| 13334 |
local e_enable,e_disable=experiments.enable,experiments.disable |
| 13335 |
local trace_directives=false local trace_directives=false trackers.register("system.directives",function(v) trace_directives=v end) |
| 13336 |
local trace_experiments=false local trace_experiments=false trackers.register("system.experiments",function(v) trace_experiments=v end) |
| 13337 |
function directives.enable(...) |
| 13338 |
if trace_directives then |
| 13339 |
directives.report("enabling: % t",{...}) |
| 13340 |
end |
| 13341 |
d_enable(...) |
| 13342 |
end |
| 13343 |
function directives.disable(...) |
| 13344 |
if trace_directives then |
| 13345 |
directives.report("disabling: % t",{...}) |
| 13346 |
end |
| 13347 |
d_disable(...) |
| 13348 |
end |
| 13349 |
function experiments.enable(...) |
| 13350 |
if trace_experiments then |
| 13351 |
experiments.report("enabling: % t",{...}) |
| 13352 |
end |
| 13353 |
e_enable(...) |
| 13354 |
end |
| 13355 |
function experiments.disable(...) |
| 13356 |
if trace_experiments then |
| 13357 |
experiments.report("disabling: % t",{...}) |
| 13358 |
end |
| 13359 |
e_disable(...) |
| 13360 |
end |
| 13361 |
directives.register("system.nostatistics",function(v) |
| 13362 |
if statistics then |
| 13363 |
statistics.enable=not v |
| 13364 |
else |
| 13365 |
end |
| 13366 |
end) |
| 13367 |
directives.register("system.nolibraries",function(v) |
| 13368 |
if libraries then |
| 13369 |
libraries=nil |
| 13370 |
else |
| 13371 |
end |
| 13372 |
end) |
| 13373 |
if environment then |
| 13374 |
local engineflags=environment.engineflags |
| 13375 |
if engineflags then |
| 13376 |
local list=engineflags["c:trackers"] or engineflags["trackers"] |
| 13377 |
if type(list)=="string" then |
| 13378 |
initialize_setter("commandline flags","trackers",settings_to_hash(list)) |
| 13379 |
end |
| 13380 |
local list=engineflags["c:directives"] or engineflags["directives"] |
| 13381 |
if type(list)=="string" then |
| 13382 |
initialize_setter("commandline flags","directives",settings_to_hash(list)) |
| 13383 |
end |
| 13384 |
end |
| 13385 |
end |
| 13386 |
if texconfig then |
| 13387 |
local function set(k,v) |
| 13388 |
local v=tonumber(v) |
| 13389 |
if v then |
| 13390 |
texconfig[k]=v |
| 13391 |
end |
| 13392 |
end |
| 13393 |
directives.register("luatex.expanddepth",function(v) set("expand_depth",v) end) |
| 13394 |
directives.register("luatex.hashextra",function(v) set("hash_extra",v) end) |
| 13395 |
directives.register("luatex.nestsize",function(v) set("nest_size",v) end) |
| 13396 |
directives.register("luatex.maxinopen",function(v) set("max_in_open",v) end) |
| 13397 |
directives.register("luatex.maxprintline",function(v) set("max_print_line",v) end) |
| 13398 |
directives.register("luatex.maxstrings",function(v) set("max_strings",v) end) |
| 13399 |
directives.register("luatex.paramsize",function(v) set("param_size",v) end) |
| 13400 |
directives.register("luatex.savesize",function(v) set("save_size",v) end) |
| 13401 |
directives.register("luatex.stacksize",function(v) set("stack_size",v) end) |
| 13402 |
end |
| 13403 |
local data=table.setmetatableindex("table") |
| 13404 |
updaters={ |
| 13405 |
register=function(what,f) |
| 13406 |
local d=data[what] |
| 13407 |
d[#d+1]=f |
| 13408 |
end, |
| 13409 |
apply=function(what,...) |
| 13410 |
local d=data[what] |
| 13411 |
for i=1,#d do |
| 13412 |
d[i](...) |
| 13413 |
end |
| 13414 |
end, |
| 13415 |
} |
| 13416 |
|
| 13417 |
|
| 13418 |
end -- of closure |
| 13419 |
|
| 13420 |
do -- create closure to overcome 200 locals limit |
| 13421 |
|
| 13422 |
package.loaded["trac-log"] = package.loaded["trac-log"] or true |
| 13423 |
|
| 13424 |
-- original size: 16046, stripped down to: 11072 |
| 13425 |
|
| 13426 |
if not modules then modules={} end modules ['trac-log']={ |
| 13427 |
version=1.001, |
| 13428 |
comment="companion to trac-log.mkiv", |
| 13429 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 13430 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 13431 |
license="see context related readme files" |
| 13432 |
} |
| 13433 |
local next,type,select,print=next,type,select,print |
| 13434 |
local format,gmatch,find=string.format,string.gmatch,string.find |
| 13435 |
local concat,insert,remove=table.concat,table.insert,table.remove |
| 13436 |
local topattern=string.topattern |
| 13437 |
local utfchar=utf.char |
| 13438 |
local datetime=os.date |
| 13439 |
local sleep=os.sleep |
| 13440 |
local openfile=io.open |
| 13441 |
local write_nl=print |
| 13442 |
local write=io.write |
| 13443 |
local setmetatableindex=table.setmetatableindex |
| 13444 |
local formatters=string.formatters |
| 13445 |
local settings_to_hash=utilities.parsers.settings_to_hash |
| 13446 |
local sortedkeys=table.sortedkeys |
| 13447 |
local variant="default" |
| 13448 |
logs=logs or {} |
| 13449 |
local logs=logs |
| 13450 |
local moreinfo=[[ |
| 13451 |
More information about ConTeXt and the tools that come with it can be found at: |
| 13452 |
]].."\n"..[[ |
| 13453 |
maillist : [email protected] / http://www.ntg.nl/mailman/listinfo/ntg-context |
| 13454 |
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net |
| 13455 |
wiki : http://contextgarden.net |
| 13456 |
]] |
| 13457 |
formatters.add ( |
| 13458 |
formatters,"unichr", |
| 13459 |
[["U+" .. format("%%05X",%s) .. " (" .. utfchar(%s) .. ")"]] |
| 13460 |
) |
| 13461 |
formatters.add ( |
| 13462 |
formatters,"chruni", |
| 13463 |
[[utfchar(%s) .. " (U+" .. format("%%05X",%s) .. ")"]] |
| 13464 |
) |
| 13465 |
local function ignore() end |
| 13466 |
setmetatableindex(logs,function(t,k) t[k]=ignore;return ignore end) |
| 13467 |
local report,subreport,status,settarget,setformats,settranslations |
| 13468 |
local direct,subdirect,writer,pushtarget,poptarget,setlogfile,settimedlog,setprocessor,setformatters,newline |
| 13469 |
local function ansisupported(specification) |
| 13470 |
if specification~="ansi" and specification~="ansilog" then |
| 13471 |
return false |
| 13472 |
elseif os and os.enableansi then |
| 13473 |
return os.enableansi() |
| 13474 |
else |
| 13475 |
return false |
| 13476 |
end |
| 13477 |
end |
| 13478 |
do |
| 13479 |
local report_yes,subreport_yes,status_yes |
| 13480 |
local report_nop,subreport_nop,status_nop |
| 13481 |
local variants={ |
| 13482 |
default={ |
| 13483 |
formats={ |
| 13484 |
report_yes=formatters["%-15s | %s"], |
| 13485 |
report_nop=formatters["%-15s |"], |
| 13486 |
subreport_yes=formatters["%-15s | %s | %s"], |
| 13487 |
subreport_nop=formatters["%-15s | %s |"], |
| 13488 |
status_yes=formatters["%-15s : %s\n"], |
| 13489 |
status_nop=formatters["%-15s :\n"], |
| 13490 |
}, |
| 13491 |
}, |
| 13492 |
ansi={ |
| 13493 |
formats={ |
| 13494 |
report_yes=formatters["[0;32m%-15s [0;1m|[0m %s"], |
| 13495 |
report_nop=formatters["[0;32m%-15s [0;1m|[0m"], |
| 13496 |
subreport_yes=formatters["[0;32m%-15s [0;1m|[0;31m %s [0;1m|[0m %s"], |
| 13497 |
subreport_nop=formatters["[0;32m%-15s [0;1m|[0;31m %s [0;1m|[0m"], |
| 13498 |
status_yes=formatters["[0;32m%-15s [0;1m:[0m %s\n"], |
| 13499 |
status_nop=formatters["[0;32m%-15s [0;1m:[0m\n"], |
| 13500 |
}, |
| 13501 |
}, |
| 13502 |
} |
| 13503 |
logs.flush=ignore |
| 13504 |
writer=function(s) |
| 13505 |
write_nl(s) |
| 13506 |
end |
| 13507 |
newline=function() |
| 13508 |
write_nl("\n") |
| 13509 |
end |
| 13510 |
report=function(a,b,c,...) |
| 13511 |
if c then |
| 13512 |
write_nl(report_yes(a,formatters[b](c,...))) |
| 13513 |
elseif b then |
| 13514 |
write_nl(report_yes(a,b)) |
| 13515 |
elseif a then |
| 13516 |
write_nl(report_nop(a)) |
| 13517 |
else |
| 13518 |
write_nl("") |
| 13519 |
end |
| 13520 |
end |
| 13521 |
subreport=function(a,sub,b,c,...) |
| 13522 |
if c then |
| 13523 |
write_nl(subreport_yes(a,sub,formatters[b](c,...))) |
| 13524 |
elseif b then |
| 13525 |
write_nl(subreport_yes(a,sub,b)) |
| 13526 |
elseif a then |
| 13527 |
write_nl(subreport_nop(a,sub)) |
| 13528 |
else |
| 13529 |
write_nl("") |
| 13530 |
end |
| 13531 |
end |
| 13532 |
status=function(a,b,c,...) |
| 13533 |
if c then |
| 13534 |
write_nl(status_yes(a,formatters[b](c,...))) |
| 13535 |
elseif b then |
| 13536 |
write_nl(status_yes(a,b)) |
| 13537 |
elseif a then |
| 13538 |
write_nl(status_nop(a)) |
| 13539 |
else |
| 13540 |
write_nl("\n") |
| 13541 |
end |
| 13542 |
end |
| 13543 |
direct=ignore |
| 13544 |
subdirect=ignore |
| 13545 |
settarget=ignore |
| 13546 |
pushtarget=ignore |
| 13547 |
poptarget=ignore |
| 13548 |
setformats=ignore |
| 13549 |
settranslations=ignore |
| 13550 |
setprocessor=function(f) |
| 13551 |
local writeline=write_nl |
| 13552 |
write_nl=function(s) |
| 13553 |
writeline(f(s)) |
| 13554 |
end |
| 13555 |
end |
| 13556 |
setformatters=function(specification) |
| 13557 |
local f=nil |
| 13558 |
local d=variants.default |
| 13559 |
if specification then |
| 13560 |
if type(specification)=="table" then |
| 13561 |
f=specification.formats or specification |
| 13562 |
else |
| 13563 |
if not ansisupported(specification) then |
| 13564 |
specification="default" |
| 13565 |
end |
| 13566 |
local v=variants[specification] |
| 13567 |
if v then |
| 13568 |
f=v.formats |
| 13569 |
end |
| 13570 |
end |
| 13571 |
end |
| 13572 |
if f then |
| 13573 |
d=d.formats |
| 13574 |
else |
| 13575 |
f=d.formats |
| 13576 |
d=f |
| 13577 |
end |
| 13578 |
setmetatableindex(f,d) |
| 13579 |
report_yes=f.report_yes |
| 13580 |
report_nop=f.report_nop |
| 13581 |
subreport_yes=f.subreport_yes |
| 13582 |
subreport_nop=f.subreport_nop |
| 13583 |
status_yes=f.status_yes |
| 13584 |
status_nop=f.status_nop |
| 13585 |
end |
| 13586 |
setformatters(variant) |
| 13587 |
setlogfile=function(name,keepopen) |
| 13588 |
if name and name~="" then |
| 13589 |
local localtime=os.localtime |
| 13590 |
local writeline=write_nl |
| 13591 |
if keepopen then |
| 13592 |
local f=io.open(name,"ab") |
| 13593 |
write_nl=function(s) |
| 13594 |
writeline(s) |
| 13595 |
f:write(localtime()," | ",s,"\n") |
| 13596 |
end |
| 13597 |
else |
| 13598 |
write_nl=function(s) |
| 13599 |
writeline(s) |
| 13600 |
local f=io.open(name,"ab") |
| 13601 |
f:write(localtime()," | ",s,"\n") |
| 13602 |
f:close() |
| 13603 |
end |
| 13604 |
end |
| 13605 |
end |
| 13606 |
setlogfile=ignore |
| 13607 |
end |
| 13608 |
settimedlog=function() |
| 13609 |
local localtime=os.localtime |
| 13610 |
local writeline=write_nl |
| 13611 |
write_nl=function(s) |
| 13612 |
writeline(localtime().." | "..s) |
| 13613 |
end |
| 13614 |
settimedlog=ignore |
| 13615 |
end |
| 13616 |
end |
| 13617 |
logs.report=report |
| 13618 |
logs.subreport=subreport |
| 13619 |
logs.status=status |
| 13620 |
logs.settarget=settarget |
| 13621 |
logs.pushtarget=pushtarget |
| 13622 |
logs.poptarget=poptarget |
| 13623 |
logs.setformats=setformats |
| 13624 |
logs.settranslations=settranslations |
| 13625 |
logs.setlogfile=setlogfile |
| 13626 |
logs.settimedlog=settimedlog |
| 13627 |
logs.setprocessor=setprocessor |
| 13628 |
logs.setformatters=setformatters |
| 13629 |
logs.direct=direct |
| 13630 |
logs.subdirect=subdirect |
| 13631 |
logs.writer=writer |
| 13632 |
logs.newline=newline |
| 13633 |
local data={} |
| 13634 |
local states=nil |
| 13635 |
local force=false |
| 13636 |
function logs.reporter(category,subcategory) |
| 13637 |
local logger=data[category] |
| 13638 |
if not logger then |
| 13639 |
local state=states==true |
| 13640 |
if not state and type(states)=="table" then |
| 13641 |
for c,_ in next,states do |
| 13642 |
if find(category,c) then |
| 13643 |
state=true |
| 13644 |
break |
| 13645 |
end |
| 13646 |
end |
| 13647 |
end |
| 13648 |
logger={ |
| 13649 |
reporters={}, |
| 13650 |
state=state, |
| 13651 |
} |
| 13652 |
data[category]=logger |
| 13653 |
end |
| 13654 |
local reporter=logger.reporters[subcategory or "default"] |
| 13655 |
if not reporter then |
| 13656 |
if subcategory then |
| 13657 |
reporter=function(...) |
| 13658 |
if force or not logger.state then |
| 13659 |
subreport(category,subcategory,...) |
| 13660 |
end |
| 13661 |
end |
| 13662 |
logger.reporters[subcategory]=reporter |
| 13663 |
else |
| 13664 |
local tag=category |
| 13665 |
reporter=function(...) |
| 13666 |
if force or not logger.state then |
| 13667 |
report(category,...) |
| 13668 |
end |
| 13669 |
end |
| 13670 |
logger.reporters.default=reporter |
| 13671 |
end |
| 13672 |
end |
| 13673 |
return reporter |
| 13674 |
end |
| 13675 |
logs.new=logs.reporter |
| 13676 |
local ctxreport=logs.writer |
| 13677 |
function logs.setmessenger(m) |
| 13678 |
ctxreport=m |
| 13679 |
end |
| 13680 |
function logs.messenger(category,subcategory) |
| 13681 |
if subcategory then |
| 13682 |
return function(...) |
| 13683 |
ctxreport(subdirect(category,subcategory,...)) |
| 13684 |
end |
| 13685 |
else |
| 13686 |
return function(...) |
| 13687 |
ctxreport(direct(category,...)) |
| 13688 |
end |
| 13689 |
end |
| 13690 |
end |
| 13691 |
local function setblocked(category,value) |
| 13692 |
if category==true or category=="all" then |
| 13693 |
category,value="*",true |
| 13694 |
elseif category==false then |
| 13695 |
category,value="*",false |
| 13696 |
elseif value==nil then |
| 13697 |
value=true |
| 13698 |
end |
| 13699 |
if category=="*" then |
| 13700 |
states=value |
| 13701 |
for k,v in next,data do |
| 13702 |
v.state=value |
| 13703 |
end |
| 13704 |
else |
| 13705 |
alllocked=false |
| 13706 |
states=settings_to_hash(category,type(states)=="table" and states or nil) |
| 13707 |
for c in next,states do |
| 13708 |
local v=data[c] |
| 13709 |
if v then |
| 13710 |
v.state=value |
| 13711 |
else |
| 13712 |
local p=topattern(c,true,true) |
| 13713 |
for k,v in next,data do |
| 13714 |
if find(k,p) then |
| 13715 |
v.state=value |
| 13716 |
end |
| 13717 |
end |
| 13718 |
end |
| 13719 |
end |
| 13720 |
end |
| 13721 |
end |
| 13722 |
function logs.disable(category,value) |
| 13723 |
setblocked(category,value==nil and true or value) |
| 13724 |
end |
| 13725 |
function logs.enable(category) |
| 13726 |
setblocked(category,false) |
| 13727 |
end |
| 13728 |
function logs.categories() |
| 13729 |
return sortedkeys(data) |
| 13730 |
end |
| 13731 |
function logs.show() |
| 13732 |
local n,c,s,max=0,0,0,0 |
| 13733 |
for category,v in table.sortedpairs(data) do |
| 13734 |
n=n+1 |
| 13735 |
local state=v.state |
| 13736 |
local reporters=v.reporters |
| 13737 |
local nc=#category |
| 13738 |
if nc>c then |
| 13739 |
c=nc |
| 13740 |
end |
| 13741 |
for subcategory,_ in next,reporters do |
| 13742 |
local ns=#subcategory |
| 13743 |
if ns>c then |
| 13744 |
s=ns |
| 13745 |
end |
| 13746 |
local m=nc+ns |
| 13747 |
if m>max then |
| 13748 |
max=m |
| 13749 |
end |
| 13750 |
end |
| 13751 |
local subcategories=concat(sortedkeys(reporters),", ") |
| 13752 |
if state==true then |
| 13753 |
state="disabled" |
| 13754 |
elseif state==false then |
| 13755 |
state="enabled" |
| 13756 |
else |
| 13757 |
state="unknown" |
| 13758 |
end |
| 13759 |
report("logging","category %a, subcategories %a, state %a",category,subcategories,state) |
| 13760 |
end |
| 13761 |
report("logging","categories: %s, max category: %s, max subcategory: %s, max combined: %s",n,c,s,max) |
| 13762 |
end |
| 13763 |
local delayed_reporters={} |
| 13764 |
setmetatableindex(delayed_reporters,function(t,k) |
| 13765 |
local v=logs.reporter(k.name) |
| 13766 |
t[k]=v |
| 13767 |
return v |
| 13768 |
end) |
| 13769 |
function utilities.setters.report(setter,...) |
| 13770 |
delayed_reporters[setter](...) |
| 13771 |
end |
| 13772 |
directives.register("logs.blocked",function(v) |
| 13773 |
setblocked(v,true) |
| 13774 |
end) |
| 13775 |
directives.register("logs.target",function(v) |
| 13776 |
settarget(v) |
| 13777 |
end) |
| 13778 |
local nesting=0 |
| 13779 |
local verbose=false |
| 13780 |
local hasscheme=url.hasscheme |
| 13781 |
local simple=logs.reporter("comment") |
| 13782 |
logs.simple=simple |
| 13783 |
logs.simpleline=simple |
| 13784 |
logs.setprogram=ignore |
| 13785 |
logs.extendbanner=ignore |
| 13786 |
logs.reportlines=ignore |
| 13787 |
logs.reportbanner=ignore |
| 13788 |
logs.reportline=ignore |
| 13789 |
logs.simplelines=ignore |
| 13790 |
logs.help=ignore |
| 13791 |
local Carg,C,lpegmatch=lpeg.Carg,lpeg.C,lpeg.match |
| 13792 |
local p_newline=lpeg.patterns.newline |
| 13793 |
local linewise=( |
| 13794 |
Carg(1)*C((1-p_newline)^1)/function(t,s) t.report(s) end+Carg(1)*p_newline^2/function(t) t.report() end+p_newline |
| 13795 |
)^1 |
| 13796 |
local function reportlines(t,str) |
| 13797 |
if str then |
| 13798 |
lpegmatch(linewise,str,1,t) |
| 13799 |
end |
| 13800 |
end |
| 13801 |
local function reportbanner(t) |
| 13802 |
local banner=t.banner |
| 13803 |
if banner then |
| 13804 |
t.report(banner) |
| 13805 |
t.report() |
| 13806 |
end |
| 13807 |
end |
| 13808 |
local function reportversion(t) |
| 13809 |
local banner=t.banner |
| 13810 |
if banner then |
| 13811 |
t.report(banner) |
| 13812 |
end |
| 13813 |
end |
| 13814 |
local function reporthelp(t,...) |
| 13815 |
local helpinfo=t.helpinfo |
| 13816 |
if type(helpinfo)=="string" then |
| 13817 |
reportlines(t,helpinfo) |
| 13818 |
elseif type(helpinfo)=="table" then |
| 13819 |
for i=1,select("#",...) do |
| 13820 |
reportlines(t,t.helpinfo[select(i,...)]) |
| 13821 |
if i<n then |
| 13822 |
t.report() |
| 13823 |
end |
| 13824 |
end |
| 13825 |
end |
| 13826 |
end |
| 13827 |
local function reportinfo(t) |
| 13828 |
t.report() |
| 13829 |
reportlines(t,t.moreinfo) |
| 13830 |
end |
| 13831 |
local function reportexport(t,method) |
| 13832 |
report(t.helpinfo) |
| 13833 |
end |
| 13834 |
local reporters={ |
| 13835 |
lines=reportlines, |
| 13836 |
banner=reportbanner, |
| 13837 |
version=reportversion, |
| 13838 |
help=reporthelp, |
| 13839 |
info=reportinfo, |
| 13840 |
export=reportexport, |
| 13841 |
} |
| 13842 |
local exporters={ |
| 13843 |
} |
| 13844 |
logs.reporters=reporters |
| 13845 |
logs.exporters=exporters |
| 13846 |
function logs.application(t) |
| 13847 |
local arguments=environment and environment.arguments |
| 13848 |
if arguments then |
| 13849 |
local ansi=arguments.ansi or arguments.ansilog |
| 13850 |
if ansi then |
| 13851 |
logs.setformatters(arguments.ansi and "ansi" or "ansilog") |
| 13852 |
end |
| 13853 |
end |
| 13854 |
t.name=t.name or "unknown" |
| 13855 |
t.banner=t.banner |
| 13856 |
t.moreinfo=moreinfo |
| 13857 |
t.report=logs.reporter(t.name) |
| 13858 |
t.help=function(...) |
| 13859 |
reporters.banner(t) |
| 13860 |
reporters.help(t,...) |
| 13861 |
reporters.info(t) |
| 13862 |
end |
| 13863 |
t.export=function(...) |
| 13864 |
reporters.export(t,...) |
| 13865 |
end |
| 13866 |
t.identify=function() |
| 13867 |
reporters.banner(t) |
| 13868 |
end |
| 13869 |
t.version=function() |
| 13870 |
reporters.version(t) |
| 13871 |
end |
| 13872 |
return t |
| 13873 |
end |
| 13874 |
local f_syslog=formatters["%s %s => %s => %s => %s\r"] |
| 13875 |
function logs.system(whereto,process,jobname,category,fmt,arg,...) |
| 13876 |
local message=f_syslog(datetime("%d/%m/%y %H:%m:%S"),process,jobname,category,arg==nil and fmt or format(fmt,arg,...)) |
| 13877 |
for i=1,10 do |
| 13878 |
local f=openfile(whereto,"a") |
| 13879 |
if f then |
| 13880 |
f:write(message) |
| 13881 |
f:close() |
| 13882 |
break |
| 13883 |
else |
| 13884 |
sleep(0.1) |
| 13885 |
end |
| 13886 |
end |
| 13887 |
end |
| 13888 |
local report_system=logs.reporter("system","logs") |
| 13889 |
if utilities then |
| 13890 |
utilities.report=report_system |
| 13891 |
end |
| 13892 |
if package.helpers.report then |
| 13893 |
package.helpers.report=logs.reporter("package loader") |
| 13894 |
end |
| 13895 |
|
| 13896 |
|
| 13897 |
end -- of closure |
| 13898 |
|
| 13899 |
do -- create closure to overcome 200 locals limit |
| 13900 |
|
| 13901 |
package.loaded["trac-inf"] = package.loaded["trac-inf"] or true |
| 13902 |
|
| 13903 |
-- original size: 9740, stripped down to: 7296 |
| 13904 |
|
| 13905 |
if not modules then modules={} end modules ['trac-inf']={ |
| 13906 |
version=1.001, |
| 13907 |
comment="companion to trac-inf.mkiv", |
| 13908 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 13909 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 13910 |
license="see context related readme files" |
| 13911 |
} |
| 13912 |
local type,tonumber,select=type,tonumber,select |
| 13913 |
local format,lower,find=string.format,string.lower,string.find |
| 13914 |
local concat=table.concat |
| 13915 |
local clock=os.gettimeofday or os.clock |
| 13916 |
local setmetatableindex=table.setmetatableindex |
| 13917 |
local serialize=table.serialize |
| 13918 |
local formatters=string.formatters |
| 13919 |
statistics=statistics or {} |
| 13920 |
local statistics=statistics |
| 13921 |
statistics.enable=true |
| 13922 |
statistics.threshold=0.01 |
| 13923 |
local statusinfo,n,registered,timers={},0,{},{} |
| 13924 |
setmetatableindex(timers,function(t,k) |
| 13925 |
local v={ timing=0,loadtime=0,offset=0 } |
| 13926 |
t[k]=v |
| 13927 |
return v |
| 13928 |
end) |
| 13929 |
local function hastiming(instance) |
| 13930 |
return instance and timers[instance] |
| 13931 |
end |
| 13932 |
local function resettiming(instance) |
| 13933 |
timers[instance or "notimer"]={ timing=0,loadtime=0,offset=0 } |
| 13934 |
end |
| 13935 |
local ticks=clock |
| 13936 |
local seconds=function(n) return n or 0 end |
| 13937 |
if os.type~="windows" then |
| 13938 |
elseif lua.getpreciseticks then |
| 13939 |
ticks=lua.getpreciseticks |
| 13940 |
seconds=lua.getpreciseseconds |
| 13941 |
elseif FFISUPPORTED then |
| 13942 |
local okay,kernel=pcall(ffi.load,"kernel32") |
| 13943 |
if kernel then |
| 13944 |
local tonumber=ffi.number or tonumber |
| 13945 |
ffi.cdef[[ |
| 13946 |
int QueryPerformanceFrequency(int64_t *lpFrequency); |
| 13947 |
int QueryPerformanceCounter(int64_t *lpPerformanceCount); |
| 13948 |
]] |
| 13949 |
local target=ffi.new("__int64[1]") |
| 13950 |
ticks=function() |
| 13951 |
if kernel.QueryPerformanceCounter(target)==1 then |
| 13952 |
return tonumber(target[0]) |
| 13953 |
else |
| 13954 |
return 0 |
| 13955 |
end |
| 13956 |
end |
| 13957 |
local target=ffi.new("__int64[1]") |
| 13958 |
seconds=function(ticks) |
| 13959 |
if kernel.QueryPerformanceFrequency(target)==1 then |
| 13960 |
return ticks/tonumber(target[0]) |
| 13961 |
else |
| 13962 |
return 0 |
| 13963 |
end |
| 13964 |
end |
| 13965 |
end |
| 13966 |
else |
| 13967 |
end |
| 13968 |
local function starttiming(instance,reset) |
| 13969 |
local timer=timers[instance or "notimer"] |
| 13970 |
local it=timer.timing |
| 13971 |
if reset then |
| 13972 |
it=0 |
| 13973 |
timer.loadtime=0 |
| 13974 |
end |
| 13975 |
if it==0 then |
| 13976 |
timer.starttime=ticks() |
| 13977 |
if not timer.loadtime then |
| 13978 |
timer.loadtime=0 |
| 13979 |
end |
| 13980 |
end |
| 13981 |
timer.timing=it+1 |
| 13982 |
end |
| 13983 |
local function stoptiming(instance) |
| 13984 |
local timer=timers[instance or "notimer"] |
| 13985 |
local it=timer.timing |
| 13986 |
if it>1 then |
| 13987 |
timer.timing=it-1 |
| 13988 |
else |
| 13989 |
local starttime=timer.starttime |
| 13990 |
if starttime and starttime>0 then |
| 13991 |
local stoptime=ticks() |
| 13992 |
local loadtime=stoptime-starttime |
| 13993 |
timer.stoptime=stoptime |
| 13994 |
timer.loadtime=timer.loadtime+loadtime |
| 13995 |
timer.timing=0 |
| 13996 |
timer.starttime=0 |
| 13997 |
end |
| 13998 |
end |
| 13999 |
end |
| 14000 |
local function benchmarktimer(instance) |
| 14001 |
local timer=timers[instance or "notimer"] |
| 14002 |
local it=timer.timing |
| 14003 |
if it>1 then |
| 14004 |
timer.timing=it-1 |
| 14005 |
else |
| 14006 |
local starttime=timer.starttime |
| 14007 |
if starttime and starttime>0 then |
| 14008 |
timer.offset=ticks()-starttime |
| 14009 |
else |
| 14010 |
timer.offset=0 |
| 14011 |
end |
| 14012 |
end |
| 14013 |
end |
| 14014 |
local function elapsed(instance) |
| 14015 |
if type(instance)=="number" then |
| 14016 |
return instance |
| 14017 |
else |
| 14018 |
local timer=timers[instance or "notimer"] |
| 14019 |
return timer and seconds(timer.loadtime-2*(timer.offset or 0)) or 0 |
| 14020 |
end |
| 14021 |
end |
| 14022 |
local function currenttime(instance) |
| 14023 |
if type(instance)=="number" then |
| 14024 |
return instance |
| 14025 |
else |
| 14026 |
local timer=timers[instance or "notimer"] |
| 14027 |
local it=timer.timing |
| 14028 |
if it>1 then |
| 14029 |
else |
| 14030 |
local starttime=timer.starttime |
| 14031 |
if starttime and starttime>0 then |
| 14032 |
return seconds(timer.loadtime+ticks()-starttime-2*(timer.offset or 0)) |
| 14033 |
end |
| 14034 |
end |
| 14035 |
return 0 |
| 14036 |
end |
| 14037 |
end |
| 14038 |
local function elapsedtime(instance) |
| 14039 |
return format("%0.3f",elapsed(instance)) |
| 14040 |
end |
| 14041 |
local function elapsedindeed(instance) |
| 14042 |
return elapsed(instance)>statistics.threshold |
| 14043 |
end |
| 14044 |
local function elapsedseconds(instance,rest) |
| 14045 |
if elapsedindeed(instance) then |
| 14046 |
return format("%0.3f seconds %s",elapsed(instance),rest or "") |
| 14047 |
end |
| 14048 |
end |
| 14049 |
statistics.hastiming=hastiming |
| 14050 |
statistics.resettiming=resettiming |
| 14051 |
statistics.starttiming=starttiming |
| 14052 |
statistics.stoptiming=stoptiming |
| 14053 |
statistics.currenttime=currenttime |
| 14054 |
statistics.elapsed=elapsed |
| 14055 |
statistics.elapsedtime=elapsedtime |
| 14056 |
statistics.elapsedindeed=elapsedindeed |
| 14057 |
statistics.elapsedseconds=elapsedseconds |
| 14058 |
statistics.benchmarktimer=benchmarktimer |
| 14059 |
function statistics.register(tag,fnc) |
| 14060 |
if statistics.enable and type(fnc)=="function" then |
| 14061 |
local rt=registered[tag] or (#statusinfo+1) |
| 14062 |
statusinfo[rt]={ tag,fnc } |
| 14063 |
registered[tag]=rt |
| 14064 |
if #tag>n then n=#tag end |
| 14065 |
end |
| 14066 |
end |
| 14067 |
local report=logs.reporter("mkiv lua stats") |
| 14068 |
function statistics.show() |
| 14069 |
if statistics.enable then |
| 14070 |
local register=statistics.register |
| 14071 |
register("used platform",function() |
| 14072 |
return format("%s, type: %s, binary subtree: %s", |
| 14073 |
os.platform or "unknown",os.type or "unknown",environment.texos or "unknown") |
| 14074 |
end) |
| 14075 |
register("used engine",function() |
| 14076 |
return format("%s version: %s, functionality level: %s, banner: %s", |
| 14077 |
LUATEXENGINE,LUATEXVERSION,LUATEXFUNCTIONALITY,lower(status.banner)) |
| 14078 |
end) |
| 14079 |
register("used hash slots",function() |
| 14080 |
return format("%s of %s + %s",status.cs_count,status.hash_size,status.hash_extra) |
| 14081 |
end) |
| 14082 |
register("callbacks",statistics.callbacks) |
| 14083 |
if JITSUPPORTED then |
| 14084 |
local jitstatus=jit.status |
| 14085 |
if jitstatus then |
| 14086 |
local jitstatus={ jitstatus() } |
| 14087 |
if jitstatus[1] then |
| 14088 |
register("luajit options",concat(jitstatus," ",2)) |
| 14089 |
end |
| 14090 |
end |
| 14091 |
end |
| 14092 |
register("lua properties",function() |
| 14093 |
local hash=2^status.luatex_hashchars |
| 14094 |
local mask=load([[τεχ = 1]]) and "utf" or "ascii" |
| 14095 |
return format("engine: %s %s, used memory: %s, hash chars: min(%i,40), symbol mask: %s (%s)", |
| 14096 |
jit and "luajit" or "lua",LUAVERSION,statistics.memused(),hash,mask,mask=="utf" and "τεχ" or "tex") |
| 14097 |
end) |
| 14098 |
register("runtime",statistics.runtime) |
| 14099 |
logs.newline() |
| 14100 |
for i=1,#statusinfo do |
| 14101 |
local s=statusinfo[i] |
| 14102 |
local r=s[2]() |
| 14103 |
if r then |
| 14104 |
report("%s: %s",s[1],r) |
| 14105 |
end |
| 14106 |
end |
| 14107 |
statistics.enable=false |
| 14108 |
end |
| 14109 |
end |
| 14110 |
function statistics.memused() |
| 14111 |
local round=math.round or math.floor |
| 14112 |
return format("%s MB, ctx: %s MB, max: %s MB", |
| 14113 |
round(collectgarbage("count")/1000), |
| 14114 |
round(status.luastate_bytes/1000000), |
| 14115 |
status.luastate_bytes_max and round(status.luastate_bytes_max/1000000) or "unknown" |
| 14116 |
) |
| 14117 |
end |
| 14118 |
starttiming(statistics) |
| 14119 |
function statistics.formatruntime(runtime) |
| 14120 |
return format("%s seconds",runtime) |
| 14121 |
end |
| 14122 |
function statistics.runtime() |
| 14123 |
stoptiming(statistics) |
| 14124 |
local runtime=lua.getruntime and lua.getruntime() or elapsedtime(statistics) |
| 14125 |
return statistics.formatruntime(runtime) |
| 14126 |
end |
| 14127 |
local report=logs.reporter("system") |
| 14128 |
function statistics.timed(action,all) |
| 14129 |
starttiming("run") |
| 14130 |
action() |
| 14131 |
stoptiming("run") |
| 14132 |
local runtime=tonumber(elapsedtime("run")) |
| 14133 |
if all then |
| 14134 |
local alltime=tonumber(lua.getruntime and lua.getruntime() or elapsedtime(statistics)) |
| 14135 |
if alltime and alltime>0 then |
| 14136 |
report("total runtime: %0.3f seconds of %0.3f seconds",runtime,alltime) |
| 14137 |
return |
| 14138 |
end |
| 14139 |
end |
| 14140 |
report("total runtime: %0.3f seconds",runtime) |
| 14141 |
end |
| 14142 |
function statistics.tracefunction(base,tag,...) |
| 14143 |
for i=1,select("#",...) do |
| 14144 |
local name=select(i,...) |
| 14145 |
local stat={} |
| 14146 |
local func=base[name] |
| 14147 |
setmetatableindex(stat,function(t,k) t[k]=0 return 0 end) |
| 14148 |
base[name]=function(n,k,v) stat[k]=stat[k]+1 return func(n,k,v) end |
| 14149 |
statistics.register(formatters["%s.%s"](tag,name),function() return serialize(stat,"calls") end) |
| 14150 |
end |
| 14151 |
end |
| 14152 |
function status.getreadstate() |
| 14153 |
return { |
| 14154 |
filename=status.filename or "?", |
| 14155 |
linenumber=status.linenumber or 0, |
| 14156 |
iocode=status.inputid or 0, |
| 14157 |
} |
| 14158 |
end |
| 14159 |
|
| 14160 |
|
| 14161 |
end -- of closure |
| 14162 |
|
| 14163 |
do -- create closure to overcome 200 locals limit |
| 14164 |
|
| 14165 |
package.loaded["trac-pro"] = package.loaded["trac-pro"] or true |
| 14166 |
|
| 14167 |
-- original size: 5841, stripped down to: 3352 |
| 14168 |
|
| 14169 |
if not modules then modules={} end modules ['trac-pro']={ |
| 14170 |
version=1.001, |
| 14171 |
comment="companion to luat-lib.mkiv", |
| 14172 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 14173 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 14174 |
license="see context related readme files" |
| 14175 |
} |
| 14176 |
local getmetatable,setmetatable,rawset,type,next=getmetatable,setmetatable,rawset,type,next |
| 14177 |
local trace_namespaces=false trackers.register("system.namespaces",function(v) trace_namespaces=v end) |
| 14178 |
local report_system=logs.reporter("system","protection") |
| 14179 |
namespaces=namespaces or {} |
| 14180 |
local namespaces=namespaces |
| 14181 |
local registered={} |
| 14182 |
local function report_index(k,name) |
| 14183 |
if trace_namespaces then |
| 14184 |
report_system("reference to %a in protected namespace %a: %s",k,name) |
| 14185 |
debugger.showtraceback(report_system) |
| 14186 |
else |
| 14187 |
report_system("reference to %a in protected namespace %a",k,name) |
| 14188 |
end |
| 14189 |
end |
| 14190 |
local function report_newindex(k,name) |
| 14191 |
if trace_namespaces then |
| 14192 |
report_system("assignment to %a in protected namespace %a: %s",k,name) |
| 14193 |
debugger.showtraceback(report_system) |
| 14194 |
else |
| 14195 |
report_system("assignment to %a in protected namespace %a",k,name) |
| 14196 |
end |
| 14197 |
end |
| 14198 |
local function register(name) |
| 14199 |
local data=name=="global" and _G or _G[name] |
| 14200 |
if not data then |
| 14201 |
return |
| 14202 |
end |
| 14203 |
registered[name]=data |
| 14204 |
local m=getmetatable(data) |
| 14205 |
if not m then |
| 14206 |
m={} |
| 14207 |
setmetatable(data,m) |
| 14208 |
end |
| 14209 |
local index,newindex={},{} |
| 14210 |
m.__saved__index=m.__index |
| 14211 |
m.__no__index=function(t,k) |
| 14212 |
if not index[k] then |
| 14213 |
index[k]=true |
| 14214 |
report_index(k,name) |
| 14215 |
end |
| 14216 |
return nil |
| 14217 |
end |
| 14218 |
m.__saved__newindex=m.__newindex |
| 14219 |
m.__no__newindex=function(t,k,v) |
| 14220 |
if not newindex[k] then |
| 14221 |
newindex[k]=true |
| 14222 |
report_newindex(k,name) |
| 14223 |
end |
| 14224 |
rawset(t,k,v) |
| 14225 |
end |
| 14226 |
m.__protection__depth=0 |
| 14227 |
end |
| 14228 |
local function private(name) |
| 14229 |
local data=registered[name] |
| 14230 |
if not data then |
| 14231 |
data=_G[name] |
| 14232 |
if not data then |
| 14233 |
data={} |
| 14234 |
_G[name]=data |
| 14235 |
end |
| 14236 |
register(name) |
| 14237 |
end |
| 14238 |
return data |
| 14239 |
end |
| 14240 |
local function protect(name) |
| 14241 |
local data=registered[name] |
| 14242 |
if not data then |
| 14243 |
return |
| 14244 |
end |
| 14245 |
local m=getmetatable(data) |
| 14246 |
local pd=m.__protection__depth |
| 14247 |
if pd>0 then |
| 14248 |
m.__protection__depth=pd+1 |
| 14249 |
else |
| 14250 |
m.__save_d_index,m.__saved__newindex=m.__index,m.__newindex |
| 14251 |
m.__index,m.__newindex=m.__no__index,m.__no__newindex |
| 14252 |
m.__protection__depth=1 |
| 14253 |
end |
| 14254 |
end |
| 14255 |
local function unprotect(name) |
| 14256 |
local data=registered[name] |
| 14257 |
if not data then |
| 14258 |
return |
| 14259 |
end |
| 14260 |
local m=getmetatable(data) |
| 14261 |
local pd=m.__protection__depth |
| 14262 |
if pd>1 then |
| 14263 |
m.__protection__depth=pd-1 |
| 14264 |
else |
| 14265 |
m.__index,m.__newindex=m.__saved__index,m.__saved__newindex |
| 14266 |
m.__protection__depth=0 |
| 14267 |
end |
| 14268 |
end |
| 14269 |
local function protectall() |
| 14270 |
for name,_ in next,registered do |
| 14271 |
if name~="global" then |
| 14272 |
protect(name) |
| 14273 |
end |
| 14274 |
end |
| 14275 |
end |
| 14276 |
local function unprotectall() |
| 14277 |
for name,_ in next,registered do |
| 14278 |
if name~="global" then |
| 14279 |
unprotect(name) |
| 14280 |
end |
| 14281 |
end |
| 14282 |
end |
| 14283 |
namespaces.register=register |
| 14284 |
namespaces.private=private |
| 14285 |
namespaces.protect=protect |
| 14286 |
namespaces.unprotect=unprotect |
| 14287 |
namespaces.protectall=protectall |
| 14288 |
namespaces.unprotectall=unprotectall |
| 14289 |
namespaces.private("namespaces") registered={} register("global") |
| 14290 |
directives.register("system.protect",function(v) |
| 14291 |
if v then |
| 14292 |
protectall() |
| 14293 |
else |
| 14294 |
unprotectall() |
| 14295 |
end |
| 14296 |
end) |
| 14297 |
directives.register("system.checkglobals",function(v) |
| 14298 |
if v then |
| 14299 |
report_system("enabling global namespace guard") |
| 14300 |
protect("global") |
| 14301 |
else |
| 14302 |
report_system("disabling global namespace guard") |
| 14303 |
unprotect("global") |
| 14304 |
end |
| 14305 |
end) |
| 14306 |
|
| 14307 |
|
| 14308 |
end -- of closure |
| 14309 |
|
| 14310 |
do -- create closure to overcome 200 locals limit |
| 14311 |
|
| 14312 |
package.loaded["util-lua"] = package.loaded["util-lua"] or true |
| 14313 |
|
| 14314 |
-- original size: 7166, stripped down to: 5009 |
| 14315 |
|
| 14316 |
if not modules then modules={} end modules ['util-lua']={ |
| 14317 |
version=1.001, |
| 14318 |
comment="companion to luat-lib.mkiv", |
| 14319 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 14320 |
comment="the strip code is written by Peter Cawley", |
| 14321 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 14322 |
license="see context related readme files" |
| 14323 |
} |
| 14324 |
local rep,sub,byte,dump,format=string.rep,string.sub,string.byte,string.dump,string.format |
| 14325 |
local load,loadfile,type,collectgarbage=load,loadfile,type,collectgarbage |
| 14326 |
utilities=utilities or {} |
| 14327 |
utilities.lua=utilities.lua or {} |
| 14328 |
local luautilities=utilities.lua |
| 14329 |
local report_lua=logs.reporter("system","lua") |
| 14330 |
local report_mem=logs.reporter("system","lua memory") |
| 14331 |
local tracestripping=false |
| 14332 |
local tracememory=false |
| 14333 |
luautilities.stripcode=true |
| 14334 |
luautilities.alwaysstripcode=false |
| 14335 |
luautilities.nofstrippedchunks=0 |
| 14336 |
luautilities.nofstrippedbytes=0 |
| 14337 |
local strippedchunks={} |
| 14338 |
luautilities.strippedchunks=strippedchunks |
| 14339 |
if not LUATEXENGINE then |
| 14340 |
LUATEXENGINE=status.luatex_engine and string.lower(status.luatex_engine) |
| 14341 |
JITSUPPORTED=LUATEXENGINE=="luajittex" or jit |
| 14342 |
CONTEXTLMTXMODE=CONTEXTLMTXMODE or (LUATEXENGINE=="luametatex" and 1) or 0 |
| 14343 |
end |
| 14344 |
luautilities.suffixes={ |
| 14345 |
tma="tma", |
| 14346 |
tmc=(CONTEXTLMTXMODE and CONTEXTLMTXMODE>0 and "tmd") or (jit and "tmb") or "tmc", |
| 14347 |
lua="lua", |
| 14348 |
lmt="lmt", |
| 14349 |
luc=(CONTEXTLMTXMODE and CONTEXTLMTXMODE>0 and "lud") or (jit and "lub") or "luc", |
| 14350 |
lui="lui", |
| 14351 |
luv="luv", |
| 14352 |
luj="luj", |
| 14353 |
tua="tua", |
| 14354 |
tuc=(CONTEXTLMTXMODE and CONTEXTLMTXMODE>0 and "tud") or (jit and "tub") or "tuc", |
| 14355 |
} |
| 14356 |
local function register(name) |
| 14357 |
if tracestripping then |
| 14358 |
report_lua("stripped bytecode from %a",name or "unknown") |
| 14359 |
end |
| 14360 |
strippedchunks[#strippedchunks+1]=name |
| 14361 |
luautilities.nofstrippedchunks=luautilities.nofstrippedchunks+1 |
| 14362 |
end |
| 14363 |
local function stupidcompile(luafile,lucfile,strip) |
| 14364 |
local code=io.loaddata(luafile) |
| 14365 |
if code and code~="" then |
| 14366 |
code=load(code) |
| 14367 |
if code then |
| 14368 |
code=dump(code,strip and luautilities.stripcode or luautilities.alwaysstripcode) |
| 14369 |
if code and code~="" then |
| 14370 |
register(name) |
| 14371 |
io.savedata(lucfile,code) |
| 14372 |
return true,0 |
| 14373 |
end |
| 14374 |
else |
| 14375 |
report_lua("fatal error %a in file %a",1,luafile) |
| 14376 |
end |
| 14377 |
else |
| 14378 |
report_lua("fatal error %a in file %a",2,luafile) |
| 14379 |
end |
| 14380 |
return false,0 |
| 14381 |
end |
| 14382 |
function luautilities.loadedluacode(fullname,forcestrip,name,macros) |
| 14383 |
name=name or fullname |
| 14384 |
if macros then |
| 14385 |
macros=lua.macros |
| 14386 |
end |
| 14387 |
local code,message |
| 14388 |
if macros then |
| 14389 |
code,message=macros.loaded(fullname,true,false) |
| 14390 |
else |
| 14391 |
code,message=loadfile(fullname) |
| 14392 |
end |
| 14393 |
if code then |
| 14394 |
code() |
| 14395 |
else |
| 14396 |
report_lua("loading of file %a failed:\n\t%s",fullname,message or "no message") |
| 14397 |
code,message=loadfile(fullname) |
| 14398 |
end |
| 14399 |
if forcestrip and luautilities.stripcode then |
| 14400 |
if type(forcestrip)=="function" then |
| 14401 |
forcestrip=forcestrip(fullname) |
| 14402 |
end |
| 14403 |
if forcestrip or luautilities.alwaysstripcode then |
| 14404 |
register(name) |
| 14405 |
return load(dump(code,true)),0 |
| 14406 |
else |
| 14407 |
return code,0 |
| 14408 |
end |
| 14409 |
elseif luautilities.alwaysstripcode then |
| 14410 |
register(name) |
| 14411 |
return load(dump(code,true)),0 |
| 14412 |
else |
| 14413 |
return code,0 |
| 14414 |
end |
| 14415 |
end |
| 14416 |
function luautilities.strippedloadstring(code,name,forcestrip) |
| 14417 |
local code,message=load(code) |
| 14418 |
if not code then |
| 14419 |
report_lua("loading of file %a failed:\n\t%s",name,message or "no message") |
| 14420 |
end |
| 14421 |
if forcestrip and luautilities.stripcode or luautilities.alwaysstripcode then |
| 14422 |
register(name) |
| 14423 |
return load(dump(code,true)),0 |
| 14424 |
else |
| 14425 |
return code,0 |
| 14426 |
end |
| 14427 |
end |
| 14428 |
function luautilities.loadstring(code,name) |
| 14429 |
local code,message=load(code) |
| 14430 |
if not code then |
| 14431 |
report_lua("loading of file %a failed:\n\t%s",name,message or "no message") |
| 14432 |
end |
| 14433 |
return code,0 |
| 14434 |
end |
| 14435 |
function luautilities.compile(luafile,lucfile,cleanup,strip,fallback) |
| 14436 |
report_lua("compiling %a into %a",luafile,lucfile) |
| 14437 |
os.remove(lucfile) |
| 14438 |
local done=stupidcompile(luafile,lucfile,strip~=false) |
| 14439 |
if done then |
| 14440 |
report_lua("dumping %a into %a stripped",luafile,lucfile) |
| 14441 |
if cleanup==true and lfs.isfile(lucfile) and lfs.isfile(luafile) then |
| 14442 |
report_lua("removing %a",luafile) |
| 14443 |
os.remove(luafile) |
| 14444 |
end |
| 14445 |
end |
| 14446 |
return done |
| 14447 |
end |
| 14448 |
function luautilities.loadstripped(...) |
| 14449 |
local l=load(...) |
| 14450 |
if l then |
| 14451 |
return load(dump(l,true)) |
| 14452 |
end |
| 14453 |
end |
| 14454 |
local finalizers={} |
| 14455 |
setmetatable(finalizers,{ |
| 14456 |
__gc=function(t) |
| 14457 |
for i=1,#t do |
| 14458 |
pcall(t[i]) |
| 14459 |
end |
| 14460 |
end |
| 14461 |
} ) |
| 14462 |
function luautilities.registerfinalizer(f) |
| 14463 |
finalizers[#finalizers+1]=f |
| 14464 |
end |
| 14465 |
function luautilities.checkmemory(previous,threshold,trace) |
| 14466 |
local current=collectgarbage("count") |
| 14467 |
if previous then |
| 14468 |
local checked=(threshold or 64)*1024 |
| 14469 |
local delta=current-previous |
| 14470 |
if current-previous>checked then |
| 14471 |
collectgarbage("collect") |
| 14472 |
local afterwards=collectgarbage("count") |
| 14473 |
if trace or tracememory then |
| 14474 |
report_mem("previous %r MB, current %r MB, delta %r MB, threshold %r MB, afterwards %r MB", |
| 14475 |
previous/1024,current/1024,delta/1024,threshold,afterwards) |
| 14476 |
end |
| 14477 |
return afterwards |
| 14478 |
elseif trace or tracememory then |
| 14479 |
report_mem("previous %r MB, current %r MB, delta %r MB, threshold %r MB", |
| 14480 |
previous/1024,current/1024,delta/1024,threshold) |
| 14481 |
end |
| 14482 |
end |
| 14483 |
return current |
| 14484 |
end |
| 14485 |
|
| 14486 |
|
| 14487 |
end -- of closure |
| 14488 |
|
| 14489 |
do -- create closure to overcome 200 locals limit |
| 14490 |
|
| 14491 |
package.loaded["util-deb"] = package.loaded["util-deb"] or true |
| 14492 |
|
| 14493 |
-- original size: 10416, stripped down to: 7076 |
| 14494 |
|
| 14495 |
if not modules then modules={} end modules ['util-deb']={ |
| 14496 |
version=1.001, |
| 14497 |
comment="companion to luat-lib.mkiv", |
| 14498 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 14499 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 14500 |
license="see context related readme files" |
| 14501 |
} |
| 14502 |
local type,next,tostring,tonumber=type,next,tostring,tonumber |
| 14503 |
local format,find,sub,gsub=string.format,string.find,string.sub,string.gsub |
| 14504 |
local insert,remove,sort=table.insert,table.remove,table.sort |
| 14505 |
local setmetatableindex=table.setmetatableindex |
| 14506 |
utilities=utilities or {} |
| 14507 |
local debugger=utilities.debugger or {} |
| 14508 |
utilities.debugger=debugger |
| 14509 |
local report=logs.reporter("debugger") |
| 14510 |
local ticks=os.gettimeofday or os.clock |
| 14511 |
local seconds=function(n) return n or 0 end |
| 14512 |
local overhead=0 |
| 14513 |
local dummycalls=10*1000 |
| 14514 |
local nesting=0 |
| 14515 |
local names={} |
| 14516 |
local initialize=false |
| 14517 |
if lua.getpreciseticks then |
| 14518 |
initialize=function() |
| 14519 |
ticks=lua.getpreciseticks |
| 14520 |
seconds=lua.getpreciseseconds |
| 14521 |
initialize=false |
| 14522 |
end |
| 14523 |
elseif not (FFISUPPORTED and ffi) then |
| 14524 |
elseif os.type=="windows" then |
| 14525 |
initialize=function() |
| 14526 |
local kernel=ffilib("kernel32","system") |
| 14527 |
if kernel then |
| 14528 |
local tonumber=ffi.number or tonumber |
| 14529 |
ffi.cdef[[ |
| 14530 |
int QueryPerformanceFrequency(int64_t *lpFrequency); |
| 14531 |
int QueryPerformanceCounter(int64_t *lpPerformanceCount); |
| 14532 |
]] |
| 14533 |
local target=ffi.new("__int64[1]") |
| 14534 |
ticks=function() |
| 14535 |
if kernel.QueryPerformanceCounter(target)==1 then |
| 14536 |
return tonumber(target[0]) |
| 14537 |
else |
| 14538 |
return 0 |
| 14539 |
end |
| 14540 |
end |
| 14541 |
local target=ffi.new("__int64[1]") |
| 14542 |
seconds=function(ticks) |
| 14543 |
if kernel.QueryPerformanceFrequency(target)==1 then |
| 14544 |
return ticks/tonumber(target[0]) |
| 14545 |
else |
| 14546 |
return 0 |
| 14547 |
end |
| 14548 |
end |
| 14549 |
end |
| 14550 |
initialize=false |
| 14551 |
end |
| 14552 |
elseif os.type=="unix" then |
| 14553 |
initialize=function() |
| 14554 |
local C=ffi.C |
| 14555 |
local tonumber=ffi.number or tonumber |
| 14556 |
ffi.cdef [[ |
| 14557 |
/* what a mess */ |
| 14558 |
typedef int clk_id_t; |
| 14559 |
typedef enum { CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID } clk_id; |
| 14560 |
typedef struct timespec { long sec; long nsec; } ctx_timespec; |
| 14561 |
int clock_gettime(clk_id_t timerid, struct timespec *t); |
| 14562 |
]] |
| 14563 |
local target=ffi.new("ctx_timespec[?]",1) |
| 14564 |
local clock=C.CLOCK_PROCESS_CPUTIME_ID |
| 14565 |
ticks=function () |
| 14566 |
C.clock_gettime(clock,target) |
| 14567 |
return tonumber(target[0].sec*1000000000+target[0].nsec) |
| 14568 |
end |
| 14569 |
seconds=function(ticks) |
| 14570 |
return ticks/1000000000 |
| 14571 |
end |
| 14572 |
initialize=false |
| 14573 |
end |
| 14574 |
end |
| 14575 |
setmetatableindex(names,function(t,name) |
| 14576 |
local v=setmetatableindex(function(t,source) |
| 14577 |
local v=setmetatableindex(function(t,line) |
| 14578 |
local v={ total=0,count=0,nesting=0 } |
| 14579 |
t[line]=v |
| 14580 |
return v |
| 14581 |
end) |
| 14582 |
t[source]=v |
| 14583 |
return v |
| 14584 |
end) |
| 14585 |
t[name]=v |
| 14586 |
return v |
| 14587 |
end) |
| 14588 |
local getinfo=nil |
| 14589 |
local sethook=nil |
| 14590 |
local function hook(where) |
| 14591 |
local f=getinfo(2,"nSl") |
| 14592 |
if f then |
| 14593 |
local source=f.short_src |
| 14594 |
if not source then |
| 14595 |
return |
| 14596 |
end |
| 14597 |
local line=f.linedefined or 0 |
| 14598 |
local name=f.name |
| 14599 |
if not name then |
| 14600 |
local what=f.what |
| 14601 |
if what=="C" then |
| 14602 |
name="<anonymous>" |
| 14603 |
else |
| 14604 |
name=f.namewhat or what or "<unknown>" |
| 14605 |
end |
| 14606 |
end |
| 14607 |
local data=names[name][source][line] |
| 14608 |
if where=="call" then |
| 14609 |
local nesting=data.nesting |
| 14610 |
if nesting==0 then |
| 14611 |
data.count=data.count+1 |
| 14612 |
insert(data,ticks()) |
| 14613 |
data.nesting=1 |
| 14614 |
else |
| 14615 |
data.nesting=nesting+1 |
| 14616 |
end |
| 14617 |
elseif where=="return" then |
| 14618 |
local nesting=data.nesting |
| 14619 |
if nesting==1 then |
| 14620 |
local t=remove(data) |
| 14621 |
if t then |
| 14622 |
data.total=data.total+ticks()-t |
| 14623 |
end |
| 14624 |
data.nesting=0 |
| 14625 |
else |
| 14626 |
data.nesting=nesting-1 |
| 14627 |
end |
| 14628 |
end |
| 14629 |
end |
| 14630 |
end |
| 14631 |
function debugger.showstats(printer,threshold) |
| 14632 |
local printer=printer or report |
| 14633 |
local calls=0 |
| 14634 |
local functions=0 |
| 14635 |
local dataset={} |
| 14636 |
local length=0 |
| 14637 |
local realtime=0 |
| 14638 |
local totaltime=0 |
| 14639 |
local threshold=threshold or 0 |
| 14640 |
for name,sources in next,names do |
| 14641 |
for source,lines in next,sources do |
| 14642 |
for line,data in next,lines do |
| 14643 |
local count=data.count |
| 14644 |
if count>threshold then |
| 14645 |
if #name>length then |
| 14646 |
length=#name |
| 14647 |
end |
| 14648 |
local total=data.total |
| 14649 |
local real=total |
| 14650 |
if real>0 then |
| 14651 |
real=total-(count*overhead/dummycalls) |
| 14652 |
if real<0 then |
| 14653 |
real=0 |
| 14654 |
end |
| 14655 |
realtime=realtime+real |
| 14656 |
end |
| 14657 |
totaltime=totaltime+total |
| 14658 |
dataset[#dataset+1]={ real,total,count,name,source,line<0 and 0 or line } |
| 14659 |
end |
| 14660 |
end |
| 14661 |
end |
| 14662 |
end |
| 14663 |
sort(dataset,function(a,b) |
| 14664 |
if a[1]==b[1] then |
| 14665 |
if a[2]==b[2] then |
| 14666 |
if a[3]==b[3] then |
| 14667 |
if a[4]==b[4] then |
| 14668 |
if a[5]==b[5] then |
| 14669 |
return a[6]<b[6] |
| 14670 |
else |
| 14671 |
return a[5]<b[5] |
| 14672 |
end |
| 14673 |
else |
| 14674 |
return a[4]<b[4] |
| 14675 |
end |
| 14676 |
else |
| 14677 |
return b[3]<a[3] |
| 14678 |
end |
| 14679 |
else |
| 14680 |
return b[2]<a[2] |
| 14681 |
end |
| 14682 |
else |
| 14683 |
return b[1]<a[1] |
| 14684 |
end |
| 14685 |
end) |
| 14686 |
if length>50 then |
| 14687 |
length=50 |
| 14688 |
end |
| 14689 |
local fmt=string.formatters["%4.9k s %3.3k %% %4.9k s %3.3k %% %8i # %-"..length.."s %4i %s"] |
| 14690 |
for i=1,#dataset do |
| 14691 |
local data=dataset[i] |
| 14692 |
local real=data[1] |
| 14693 |
local total=data[2] |
| 14694 |
local count=data[3] |
| 14695 |
local name=data[4] |
| 14696 |
local source=data[5] |
| 14697 |
local line=data[6] |
| 14698 |
calls=calls+count |
| 14699 |
functions=functions+1 |
| 14700 |
name=gsub(name,"%s+"," ") |
| 14701 |
if #name>length then |
| 14702 |
name=sub(name,1,length) |
| 14703 |
end |
| 14704 |
printer(fmt(seconds(total),100*total/totaltime,seconds(real),100*real/realtime,count,name,line,source)) |
| 14705 |
end |
| 14706 |
printer("") |
| 14707 |
printer(format("functions : %i",functions)) |
| 14708 |
printer(format("calls : %i",calls)) |
| 14709 |
printer(format("overhead : %f",seconds(overhead/1000))) |
| 14710 |
end |
| 14711 |
local function getdebug() |
| 14712 |
if sethook and getinfo then |
| 14713 |
return |
| 14714 |
end |
| 14715 |
if not debug then |
| 14716 |
local okay |
| 14717 |
okay,debug=pcall(require,"debug") |
| 14718 |
end |
| 14719 |
if type(debug)~="table" then |
| 14720 |
return |
| 14721 |
end |
| 14722 |
getinfo=debug.getinfo |
| 14723 |
sethook=debug.sethook |
| 14724 |
if type(getinfo)~="function" then |
| 14725 |
getinfo=nil |
| 14726 |
end |
| 14727 |
if type(sethook)~="function" then |
| 14728 |
sethook=nil |
| 14729 |
end |
| 14730 |
end |
| 14731 |
function debugger.savestats(filename,threshold) |
| 14732 |
local f=io.open(filename,'w') |
| 14733 |
if f then |
| 14734 |
debugger.showstats(function(str) f:write(str,"\n") end,threshold) |
| 14735 |
f:close() |
| 14736 |
end |
| 14737 |
end |
| 14738 |
function debugger.enable() |
| 14739 |
getdebug() |
| 14740 |
if sethook and getinfo and nesting==0 then |
| 14741 |
running=true |
| 14742 |
if initialize then |
| 14743 |
initialize() |
| 14744 |
end |
| 14745 |
sethook(hook,"cr") |
| 14746 |
local function dummy() end |
| 14747 |
local t=ticks() |
| 14748 |
for i=1,dummycalls do |
| 14749 |
dummy() |
| 14750 |
end |
| 14751 |
overhead=ticks()-t |
| 14752 |
end |
| 14753 |
if nesting>0 then |
| 14754 |
nesting=nesting+1 |
| 14755 |
end |
| 14756 |
end |
| 14757 |
function debugger.disable() |
| 14758 |
if nesting>0 then |
| 14759 |
nesting=nesting-1 |
| 14760 |
end |
| 14761 |
if sethook and getinfo and nesting==0 then |
| 14762 |
sethook() |
| 14763 |
end |
| 14764 |
end |
| 14765 |
local function showtraceback(rep) |
| 14766 |
getdebug() |
| 14767 |
if getinfo then |
| 14768 |
local level=2 |
| 14769 |
local reporter=rep or report |
| 14770 |
while true do |
| 14771 |
local info=getinfo(level,"Sl") |
| 14772 |
if not info then |
| 14773 |
break |
| 14774 |
elseif info.what=="C" then |
| 14775 |
reporter("%2i : %s",level-1,"C function") |
| 14776 |
else |
| 14777 |
reporter("%2i : %s : %s",level-1,info.short_src,info.currentline) |
| 14778 |
end |
| 14779 |
level=level+1 |
| 14780 |
end |
| 14781 |
end |
| 14782 |
end |
| 14783 |
debugger.showtraceback=showtraceback |
| 14784 |
if luac then |
| 14785 |
local show,dump=luac.print,string.dump |
| 14786 |
function luac.inspect(v) |
| 14787 |
if type(v)=="function" then |
| 14788 |
local ok,str=xpcall(dump,function() end,v) |
| 14789 |
if ok then |
| 14790 |
v=str |
| 14791 |
end |
| 14792 |
end |
| 14793 |
if type(v)=="string" then |
| 14794 |
show(v,true) |
| 14795 |
else |
| 14796 |
print(v) |
| 14797 |
end |
| 14798 |
end |
| 14799 |
end |
| 14800 |
|
| 14801 |
|
| 14802 |
end -- of closure |
| 14803 |
|
| 14804 |
do -- create closure to overcome 200 locals limit |
| 14805 |
|
| 14806 |
package.loaded["util-tpl"] = package.loaded["util-tpl"] or true |
| 14807 |
|
| 14808 |
-- original size: 7708, stripped down to: 4212 |
| 14809 |
|
| 14810 |
if not modules then modules={} end modules ['util-tpl']={ |
| 14811 |
version=1.001, |
| 14812 |
comment="companion to luat-lib.mkiv", |
| 14813 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 14814 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 14815 |
license="see context related readme files" |
| 14816 |
} |
| 14817 |
utilities.templates=utilities.templates or {} |
| 14818 |
local templates=utilities.templates |
| 14819 |
local trace_template=false trackers.register("templates.trace",function(v) trace_template=v end) |
| 14820 |
local report_template=logs.reporter("template") |
| 14821 |
local tostring,next=tostring,next |
| 14822 |
local format,sub,byte=string.format,string.sub,string.byte |
| 14823 |
local P,C,R,Cs,Cc,Carg,lpegmatch,lpegpatterns=lpeg.P,lpeg.C,lpeg.R,lpeg.Cs,lpeg.Cc,lpeg.Carg,lpeg.match,lpeg.patterns |
| 14824 |
local formatters=string.formatters |
| 14825 |
local replacer |
| 14826 |
local function replacekey(k,t,how,recursive) |
| 14827 |
local v=t[k] |
| 14828 |
if not v then |
| 14829 |
if trace_template then |
| 14830 |
report_template("unknown key %a",k) |
| 14831 |
end |
| 14832 |
return "" |
| 14833 |
else |
| 14834 |
v=tostring(v) |
| 14835 |
if trace_template then |
| 14836 |
report_template("setting key %a to value %a",k,v) |
| 14837 |
end |
| 14838 |
if recursive then |
| 14839 |
return lpegmatch(replacer,v,1,t,how,recursive) |
| 14840 |
else |
| 14841 |
return v |
| 14842 |
end |
| 14843 |
end |
| 14844 |
end |
| 14845 |
local sqlescape=lpeg.replacer { |
| 14846 |
{ "'","''" }, |
| 14847 |
{ "\\","\\\\" }, |
| 14848 |
{ "\r\n","\\n" }, |
| 14849 |
{ "\r","\\n" }, |
| 14850 |
} |
| 14851 |
local sqlquoted=Cs(Cc("'")*sqlescape*Cc("'")) |
| 14852 |
lpegpatterns.sqlescape=sqlescape |
| 14853 |
lpegpatterns.sqlquoted=sqlquoted |
| 14854 |
local luaescape=lpegpatterns.luaescape |
| 14855 |
local escapers={ |
| 14856 |
lua=function(s) |
| 14857 |
return lpegmatch(luaescape,s) |
| 14858 |
end, |
| 14859 |
sql=function(s) |
| 14860 |
return lpegmatch(sqlescape,s) |
| 14861 |
end, |
| 14862 |
} |
| 14863 |
local quotedescapers={ |
| 14864 |
lua=function(s) |
| 14865 |
return format("%q",s) |
| 14866 |
end, |
| 14867 |
sql=function(s) |
| 14868 |
return lpegmatch(sqlquoted,s) |
| 14869 |
end, |
| 14870 |
} |
| 14871 |
local luaescaper=escapers.lua |
| 14872 |
local quotedluaescaper=quotedescapers.lua |
| 14873 |
local function replacekeyunquoted(s,t,how,recurse) |
| 14874 |
if how==false then |
| 14875 |
return replacekey(s,t,how,recurse) |
| 14876 |
else |
| 14877 |
local escaper=how and escapers[how] or luaescaper |
| 14878 |
return escaper(replacekey(s,t,how,recurse)) |
| 14879 |
end |
| 14880 |
end |
| 14881 |
local function replacekeyquoted(s,t,how,recurse) |
| 14882 |
if how==false then |
| 14883 |
return replacekey(s,t,how,recurse) |
| 14884 |
else |
| 14885 |
local escaper=how and quotedescapers[how] or quotedluaescaper |
| 14886 |
return escaper(replacekey(s,t,how,recurse)) |
| 14887 |
end |
| 14888 |
end |
| 14889 |
local function replaceoptional(l,m,r,t,how,recurse) |
| 14890 |
local v=t[l] |
| 14891 |
return v and v~="" and lpegmatch(replacer,r,1,t,how or "lua",recurse or false) or "" |
| 14892 |
end |
| 14893 |
local function replaceformatted(l,m,r,t,how,recurse) |
| 14894 |
local v=t[r] |
| 14895 |
return v and formatters[l](v) |
| 14896 |
end |
| 14897 |
local single=P("%") |
| 14898 |
local double=P("%%") |
| 14899 |
local lquoted=P("%[") |
| 14900 |
local rquoted=P("]%") |
| 14901 |
local lquotedq=P("%(") |
| 14902 |
local rquotedq=P(")%") |
| 14903 |
local escape=double/'%%' |
| 14904 |
local nosingle=single/'' |
| 14905 |
local nodouble=double/'' |
| 14906 |
local nolquoted=lquoted/'' |
| 14907 |
local norquoted=rquoted/'' |
| 14908 |
local nolquotedq=lquotedq/'' |
| 14909 |
local norquotedq=rquotedq/'' |
| 14910 |
local nolformatted=P(":")/"%%" |
| 14911 |
local norformatted=P(":")/"" |
| 14912 |
local noloptional=P("%?")/'' |
| 14913 |
local noroptional=P("?%")/'' |
| 14914 |
local nomoptional=P(":")/'' |
| 14915 |
local args=Carg(1)*Carg(2)*Carg(3) |
| 14916 |
local key=nosingle*((C((1-nosingle)^1)*args)/replacekey)*nosingle |
| 14917 |
local quoted=nolquotedq*((C((1-norquotedq)^1)*args)/replacekeyquoted)*norquotedq |
| 14918 |
local unquoted=nolquoted*((C((1-norquoted)^1)*args)/replacekeyunquoted)*norquoted |
| 14919 |
local optional=noloptional*((C((1-nomoptional)^1)*nomoptional*C((1-noroptional)^1)*args)/replaceoptional)*noroptional |
| 14920 |
local formatted=nosingle*((Cs(nolformatted*(1-norformatted )^1)*norformatted*C((1-nosingle)^1)*args)/replaceformatted)*nosingle |
| 14921 |
local any=P(1) |
| 14922 |
replacer=Cs((unquoted+quoted+formatted+escape+optional+key+any)^0) |
| 14923 |
local function replace(str,mapping,how,recurse) |
| 14924 |
if mapping and str then |
| 14925 |
return lpegmatch(replacer,str,1,mapping,how or "lua",recurse or false) or str |
| 14926 |
else |
| 14927 |
return str |
| 14928 |
end |
| 14929 |
end |
| 14930 |
templates.replace=replace |
| 14931 |
function templates.replacer(str,how,recurse) |
| 14932 |
return function(mapping) |
| 14933 |
return lpegmatch(replacer,str,1,mapping,how or "lua",recurse or false) or str |
| 14934 |
end |
| 14935 |
end |
| 14936 |
function templates.load(filename,mapping,how,recurse) |
| 14937 |
local data=io.loaddata(filename) or "" |
| 14938 |
if mapping and next(mapping) then |
| 14939 |
return replace(data,mapping,how,recurse) |
| 14940 |
else |
| 14941 |
return data |
| 14942 |
end |
| 14943 |
end |
| 14944 |
function templates.resolve(t,mapping,how,recurse) |
| 14945 |
if not mapping then |
| 14946 |
mapping=t |
| 14947 |
end |
| 14948 |
for k,v in next,t do |
| 14949 |
t[k]=replace(v,mapping,how,recurse) |
| 14950 |
end |
| 14951 |
return t |
| 14952 |
end |
| 14953 |
|
| 14954 |
|
| 14955 |
end -- of closure |
| 14956 |
|
| 14957 |
do -- create closure to overcome 200 locals limit |
| 14958 |
|
| 14959 |
package.loaded["util-sbx"] = package.loaded["util-sbx"] or true |
| 14960 |
|
| 14961 |
-- original size: 21448, stripped down to: 13434 |
| 14962 |
|
| 14963 |
if not modules then modules={} end modules ['util-sbx']={ |
| 14964 |
version=1.001, |
| 14965 |
comment="companion to luat-lib.mkiv", |
| 14966 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 14967 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 14968 |
license="see context related readme files" |
| 14969 |
} |
| 14970 |
if not sandbox then require("l-sandbox") end |
| 14971 |
local next,type=next,type |
| 14972 |
local replace=utilities.templates.replace |
| 14973 |
local collapsepath=file.collapsepath |
| 14974 |
local expandname=dir.expandname |
| 14975 |
local sortedhash=table.sortedhash |
| 14976 |
local lpegmatch=lpeg.match |
| 14977 |
local platform=os.type |
| 14978 |
local P,S,C=lpeg.P,lpeg.S,lpeg.C |
| 14979 |
local gsub=string.gsub |
| 14980 |
local lower=string.lower |
| 14981 |
local find=string.find |
| 14982 |
local concat=string.concat |
| 14983 |
local unquoted=string.unquoted |
| 14984 |
local optionalquoted=string.optionalquoted |
| 14985 |
local basename=file.basename |
| 14986 |
local nameonly=file.nameonly |
| 14987 |
local sandbox=sandbox |
| 14988 |
local validroots={} |
| 14989 |
local validrunners={} |
| 14990 |
local validbinaries=true |
| 14991 |
local validlibraries=true |
| 14992 |
local validators={} |
| 14993 |
local finalized=nil |
| 14994 |
local trace=false |
| 14995 |
local p_validroot=nil |
| 14996 |
local p_split=lpeg.firstofsplit(" ") |
| 14997 |
local report=logs.reporter("sandbox") |
| 14998 |
trackers.register("sandbox",function(v) trace=v end) |
| 14999 |
sandbox.setreporter(report) |
| 15000 |
sandbox.finalizer { |
| 15001 |
category="files", |
| 15002 |
action=function() |
| 15003 |
finalized=true |
| 15004 |
end |
| 15005 |
} |
| 15006 |
local function registerroot(root,what) |
| 15007 |
if finalized then |
| 15008 |
report("roots are already finalized") |
| 15009 |
else |
| 15010 |
if type(root)=="table" then |
| 15011 |
root,what=root[1],root[2] |
| 15012 |
end |
| 15013 |
if type(root)=="string" and root~="" then |
| 15014 |
root=collapsepath(expandname(root)) |
| 15015 |
if what=="r" or what=="ro" or what=="readable" then |
| 15016 |
what="read" |
| 15017 |
elseif what=="w" or what=="wo" or what=="writable" then |
| 15018 |
what="write" |
| 15019 |
end |
| 15020 |
validroots[root]=what=="write" or false |
| 15021 |
end |
| 15022 |
end |
| 15023 |
end |
| 15024 |
sandbox.finalizer { |
| 15025 |
category="files", |
| 15026 |
action=function() |
| 15027 |
if p_validroot then |
| 15028 |
report("roots are already initialized") |
| 15029 |
else |
| 15030 |
sandbox.registerroot(".","write") |
| 15031 |
for name in sortedhash(validroots) do |
| 15032 |
if p_validroot then |
| 15033 |
p_validroot=P(name)+p_validroot |
| 15034 |
else |
| 15035 |
p_validroot=P(name) |
| 15036 |
end |
| 15037 |
end |
| 15038 |
p_validroot=p_validroot/validroots |
| 15039 |
end |
| 15040 |
end |
| 15041 |
} |
| 15042 |
local function registerbinary(name) |
| 15043 |
if finalized then |
| 15044 |
report("binaries are already finalized") |
| 15045 |
elseif type(name)=="string" and name~="" then |
| 15046 |
if not validbinaries then |
| 15047 |
return |
| 15048 |
end |
| 15049 |
if validbinaries==true then |
| 15050 |
validbinaries={ [name]=true } |
| 15051 |
else |
| 15052 |
validbinaries[name]=true |
| 15053 |
end |
| 15054 |
elseif name==true then |
| 15055 |
validbinaries={} |
| 15056 |
end |
| 15057 |
end |
| 15058 |
local function registerlibrary(name) |
| 15059 |
if finalized then |
| 15060 |
report("libraries are already finalized") |
| 15061 |
elseif type(name)=="string" and name~="" then |
| 15062 |
if not validlibraries then |
| 15063 |
return |
| 15064 |
end |
| 15065 |
if validlibraries==true then |
| 15066 |
validlibraries={ [nameonly(name)]=true } |
| 15067 |
else |
| 15068 |
validlibraries[nameonly(name)]=true |
| 15069 |
end |
| 15070 |
elseif name==true then |
| 15071 |
validlibraries={} |
| 15072 |
end |
| 15073 |
end |
| 15074 |
local p_write=S("wa") p_write=(1-p_write)^0*p_write |
| 15075 |
local p_path=S("\\/~$%:") p_path=(1-p_path )^0*p_path |
| 15076 |
local function normalized(name) |
| 15077 |
if platform=="windows" then |
| 15078 |
name=gsub(name,"/","\\") |
| 15079 |
end |
| 15080 |
return name |
| 15081 |
end |
| 15082 |
function sandbox.possiblepath(name) |
| 15083 |
return lpegmatch(p_path,name) and true or false |
| 15084 |
end |
| 15085 |
local filenamelogger=false |
| 15086 |
function sandbox.setfilenamelogger(l) |
| 15087 |
filenamelogger=type(l)=="function" and l or false |
| 15088 |
end |
| 15089 |
local function validfilename(name,what) |
| 15090 |
if p_validroot and type(name)=="string" and lpegmatch(p_path,name) then |
| 15091 |
local asked=collapsepath(expandname(name)) |
| 15092 |
local okay=lpegmatch(p_validroot,asked) |
| 15093 |
if okay==true then |
| 15094 |
if filenamelogger then |
| 15095 |
filenamelogger(name,"w",asked,true) |
| 15096 |
end |
| 15097 |
return name |
| 15098 |
elseif okay==false then |
| 15099 |
if not what then |
| 15100 |
if filenamelogger then |
| 15101 |
filenamelogger(name,"r",asked,true) |
| 15102 |
end |
| 15103 |
return name |
| 15104 |
elseif lpegmatch(p_write,what) then |
| 15105 |
if filenamelogger then |
| 15106 |
filenamelogger(name,"w",asked,false) |
| 15107 |
end |
| 15108 |
return |
| 15109 |
else |
| 15110 |
if filenamelogger then |
| 15111 |
filenamelogger(name,"r",asked,true) |
| 15112 |
end |
| 15113 |
return name |
| 15114 |
end |
| 15115 |
elseif filenamelogger then |
| 15116 |
filenamelogger(name,"*",name,false) |
| 15117 |
end |
| 15118 |
else |
| 15119 |
return name |
| 15120 |
end |
| 15121 |
end |
| 15122 |
local function readable(name,finalized) |
| 15123 |
return validfilename(name,"r") |
| 15124 |
end |
| 15125 |
local function normalizedreadable(name,finalized) |
| 15126 |
local valid=validfilename(name,"r") |
| 15127 |
if valid then |
| 15128 |
return normalized(valid) |
| 15129 |
end |
| 15130 |
end |
| 15131 |
local function writeable(name,finalized) |
| 15132 |
return validfilename(name,"w") |
| 15133 |
end |
| 15134 |
local function normalizedwriteable(name,finalized) |
| 15135 |
local valid=validfilename(name,"w") |
| 15136 |
if valid then |
| 15137 |
return normalized(valid) |
| 15138 |
end |
| 15139 |
end |
| 15140 |
validators.readable=readable |
| 15141 |
validators.writeable=normalizedwriteable |
| 15142 |
validators.normalizedreadable=normalizedreadable |
| 15143 |
validators.normalizedwriteable=writeable |
| 15144 |
validators.filename=readable |
| 15145 |
table.setmetatableindex(validators,function(t,k) |
| 15146 |
if k then |
| 15147 |
t[k]=readable |
| 15148 |
end |
| 15149 |
return readable |
| 15150 |
end) |
| 15151 |
function validators.string(s,finalized) |
| 15152 |
if finalized and suspicious(s) then |
| 15153 |
return "" |
| 15154 |
else |
| 15155 |
return s |
| 15156 |
end |
| 15157 |
end |
| 15158 |
function validators.cache(s) |
| 15159 |
if finalized then |
| 15160 |
return basename(s) |
| 15161 |
else |
| 15162 |
return s |
| 15163 |
end |
| 15164 |
end |
| 15165 |
function validators.url(s) |
| 15166 |
if finalized and find("^file:") then |
| 15167 |
return "" |
| 15168 |
else |
| 15169 |
return s |
| 15170 |
end |
| 15171 |
end |
| 15172 |
local function filehandlerone(action,one,...) |
| 15173 |
local checkedone=validfilename(one) |
| 15174 |
if checkedone then |
| 15175 |
return action(one,...) |
| 15176 |
else |
| 15177 |
end |
| 15178 |
end |
| 15179 |
local function filehandlertwo(action,one,two,...) |
| 15180 |
local checkedone=validfilename(one) |
| 15181 |
if checkedone then |
| 15182 |
local checkedtwo=validfilename(two) |
| 15183 |
if checkedtwo then |
| 15184 |
return action(one,two,...) |
| 15185 |
else |
| 15186 |
end |
| 15187 |
else |
| 15188 |
end |
| 15189 |
end |
| 15190 |
local function iohandler(action,one,...) |
| 15191 |
if type(one)=="string" then |
| 15192 |
local checkedone=validfilename(one) |
| 15193 |
if checkedone then |
| 15194 |
return action(one,...) |
| 15195 |
end |
| 15196 |
elseif one then |
| 15197 |
return action(one,...) |
| 15198 |
else |
| 15199 |
return action() |
| 15200 |
end |
| 15201 |
end |
| 15202 |
local osexecute=sandbox.original(os.execute) |
| 15203 |
local iopopen=sandbox.original(io.popen) |
| 15204 |
local reported={} |
| 15205 |
local function validcommand(name,program,template,checkers,defaults,variables,reporter,strict) |
| 15206 |
if validbinaries~=false and (validbinaries==true or validbinaries[program]) then |
| 15207 |
local binpath=nil |
| 15208 |
if variables then |
| 15209 |
for variable,value in next,variables do |
| 15210 |
local chktype=checkers[variable] |
| 15211 |
if chktype=="verbose" then |
| 15212 |
else |
| 15213 |
local checker=validators[chktype] |
| 15214 |
if checker and type(value)=="string" then |
| 15215 |
value=checker(unquoted(value),strict) |
| 15216 |
if value then |
| 15217 |
variables[variable]=optionalquoted(value) |
| 15218 |
else |
| 15219 |
report("variable %a with value %a fails the check",variable,value) |
| 15220 |
return |
| 15221 |
end |
| 15222 |
else |
| 15223 |
report("variable %a has no checker",variable) |
| 15224 |
return |
| 15225 |
end |
| 15226 |
end |
| 15227 |
end |
| 15228 |
for variable,default in next,defaults do |
| 15229 |
local value=variables[variable] |
| 15230 |
if not value or value=="" then |
| 15231 |
local chktype=checkers[variable] |
| 15232 |
if chktype=="verbose" then |
| 15233 |
elseif type(default)=="string" then |
| 15234 |
local checker=validators[chktype] |
| 15235 |
if checker then |
| 15236 |
default=checker(unquoted(default),strict) |
| 15237 |
if default then |
| 15238 |
variables[variable]=optionalquoted(default) |
| 15239 |
else |
| 15240 |
report("variable %a with default %a fails the check",variable,default) |
| 15241 |
return |
| 15242 |
end |
| 15243 |
end |
| 15244 |
end |
| 15245 |
end |
| 15246 |
end |
| 15247 |
binpath=variables.binarypath |
| 15248 |
end |
| 15249 |
if type(binpath)=="string" and binpath~="" then |
| 15250 |
program=binpath.."/"..program |
| 15251 |
end |
| 15252 |
local command=program.." "..replace(template,variables) |
| 15253 |
if reporter then |
| 15254 |
reporter("executing runner %a: %s",name,command) |
| 15255 |
elseif trace then |
| 15256 |
report("executing runner %a: %s",name,command) |
| 15257 |
end |
| 15258 |
return command |
| 15259 |
elseif not reported[name] then |
| 15260 |
report("executing program %a of runner %a is not permitted",program,name) |
| 15261 |
reported[name]=true |
| 15262 |
end |
| 15263 |
end |
| 15264 |
local runners={ |
| 15265 |
resultof=function(...) |
| 15266 |
local command=validcommand(...) |
| 15267 |
if command then |
| 15268 |
if trace then |
| 15269 |
report("resultof: %s",command) |
| 15270 |
end |
| 15271 |
local handle=iopopen(command,"r") |
| 15272 |
if handle then |
| 15273 |
local result=handle:read("*all") or "" |
| 15274 |
handle:close() |
| 15275 |
return result |
| 15276 |
end |
| 15277 |
end |
| 15278 |
end, |
| 15279 |
execute=function(...) |
| 15280 |
local command=validcommand(...) |
| 15281 |
if command then |
| 15282 |
if trace then |
| 15283 |
report("execute: %s",command) |
| 15284 |
end |
| 15285 |
local okay=osexecute(command) |
| 15286 |
return okay |
| 15287 |
end |
| 15288 |
end, |
| 15289 |
pipeto=function(...) |
| 15290 |
local command=validcommand(...) |
| 15291 |
if command then |
| 15292 |
if trace then |
| 15293 |
report("pipeto: %s",command) |
| 15294 |
end |
| 15295 |
return iopopen(command,"w") |
| 15296 |
end |
| 15297 |
end, |
| 15298 |
command=function(...) |
| 15299 |
local command=validcommand(...) |
| 15300 |
if command then |
| 15301 |
if trace then |
| 15302 |
report("command: %s",command) |
| 15303 |
end |
| 15304 |
return command |
| 15305 |
end |
| 15306 |
end, |
| 15307 |
} |
| 15308 |
function sandbox.registerrunner(specification) |
| 15309 |
if type(specification)=="string" then |
| 15310 |
local wrapped=validrunners[specification] |
| 15311 |
inspect(table.sortedkeys(validrunners)) |
| 15312 |
if wrapped then |
| 15313 |
return wrapped |
| 15314 |
else |
| 15315 |
report("unknown predefined runner %a",specification) |
| 15316 |
return |
| 15317 |
end |
| 15318 |
end |
| 15319 |
if type(specification)~="table" then |
| 15320 |
report("specification should be a table (or string)") |
| 15321 |
return |
| 15322 |
end |
| 15323 |
local name=specification.name |
| 15324 |
if type(name)~="string" then |
| 15325 |
report("invalid name, string expected",name) |
| 15326 |
return |
| 15327 |
end |
| 15328 |
if validrunners[name] then |
| 15329 |
report("invalid name, runner %a already defined",name) |
| 15330 |
return |
| 15331 |
end |
| 15332 |
local program=specification.program |
| 15333 |
if type(program)=="string" then |
| 15334 |
elseif type(program)=="table" then |
| 15335 |
program=program[platform] or program.default or program.unix |
| 15336 |
end |
| 15337 |
if type(program)~="string" or program=="" then |
| 15338 |
report("invalid runner %a specified for platform %a",name,platform) |
| 15339 |
return |
| 15340 |
end |
| 15341 |
local template=specification.template |
| 15342 |
if not template then |
| 15343 |
report("missing template for runner %a",name) |
| 15344 |
return |
| 15345 |
end |
| 15346 |
local method=specification.method or "execute" |
| 15347 |
local checkers=specification.checkers or {} |
| 15348 |
local defaults=specification.defaults or {} |
| 15349 |
local runner=runners[method] |
| 15350 |
if runner then |
| 15351 |
local finalized=finalized |
| 15352 |
local wrapped=function(variables) |
| 15353 |
return runner(name,program,template,checkers,defaults,variables,specification.reporter,finalized) |
| 15354 |
end |
| 15355 |
validrunners[name]=wrapped |
| 15356 |
return wrapped |
| 15357 |
else |
| 15358 |
validrunners[name]=nil |
| 15359 |
report("invalid method for runner %a",name) |
| 15360 |
end |
| 15361 |
end |
| 15362 |
function sandbox.getrunner(name) |
| 15363 |
return name and validrunners[name] |
| 15364 |
end |
| 15365 |
local function suspicious(str) |
| 15366 |
return (find(str,"[/\\]") or find(command,"..",1,true)) and true or false |
| 15367 |
end |
| 15368 |
local function binaryrunner(action,command,...) |
| 15369 |
if validbinaries==false then |
| 15370 |
report("no binaries permitted, ignoring command: %s",command) |
| 15371 |
return |
| 15372 |
end |
| 15373 |
if type(command)~="string" then |
| 15374 |
report("command should be a string") |
| 15375 |
return |
| 15376 |
end |
| 15377 |
local program=lpegmatch(p_split,command) |
| 15378 |
if not program or program=="" then |
| 15379 |
report("unable to filter binary from command: %s",command) |
| 15380 |
return |
| 15381 |
end |
| 15382 |
if validbinaries==true then |
| 15383 |
elseif not validbinaries[program] then |
| 15384 |
report("binary not permitted, ignoring command: %s",command) |
| 15385 |
return |
| 15386 |
elseif suspicious(command) then |
| 15387 |
report("/ \\ or .. found, ignoring command (use sandbox.registerrunner): %s",command) |
| 15388 |
return |
| 15389 |
end |
| 15390 |
return action(command,...) |
| 15391 |
end |
| 15392 |
local function dummyrunner(action,command,...) |
| 15393 |
if type(command)=="table" then |
| 15394 |
command=concat(command," ",command[0] and 0 or 1) |
| 15395 |
end |
| 15396 |
report("ignoring command: %s",command) |
| 15397 |
end |
| 15398 |
sandbox.filehandlerone=filehandlerone |
| 15399 |
sandbox.filehandlertwo=filehandlertwo |
| 15400 |
sandbox.iohandler=iohandler |
| 15401 |
function sandbox.disablerunners() |
| 15402 |
validbinaries=false |
| 15403 |
end |
| 15404 |
function sandbox.disablelibraries() |
| 15405 |
validlibraries=false |
| 15406 |
end |
| 15407 |
if FFISUPPORTED and ffi then |
| 15408 |
function sandbox.disablelibraries() |
| 15409 |
validlibraries=false |
| 15410 |
for k,v in next,ffi do |
| 15411 |
if k~="gc" then |
| 15412 |
ffi[k]=nil |
| 15413 |
end |
| 15414 |
end |
| 15415 |
end |
| 15416 |
local fiiload=ffi.load |
| 15417 |
if fiiload then |
| 15418 |
local reported={} |
| 15419 |
function ffi.load(name,...) |
| 15420 |
if validlibraries==false then |
| 15421 |
elseif validlibraries==true then |
| 15422 |
return fiiload(name,...) |
| 15423 |
elseif validlibraries[nameonly(name)] then |
| 15424 |
return fiiload(name,...) |
| 15425 |
else |
| 15426 |
end |
| 15427 |
if not reported[name] then |
| 15428 |
report("using library %a is not permitted",name) |
| 15429 |
reported[name]=true |
| 15430 |
end |
| 15431 |
return nil |
| 15432 |
end |
| 15433 |
end |
| 15434 |
end |
| 15435 |
local overload=sandbox.overload |
| 15436 |
local register=sandbox.register |
| 15437 |
overload(loadfile,filehandlerone,"loadfile") |
| 15438 |
if io then |
| 15439 |
overload(io.open,filehandlerone,"io.open") |
| 15440 |
overload(io.popen,binaryrunner,"io.popen") |
| 15441 |
overload(io.input,iohandler,"io.input") |
| 15442 |
overload(io.output,iohandler,"io.output") |
| 15443 |
overload(io.lines,filehandlerone,"io.lines") |
| 15444 |
end |
| 15445 |
if os then |
| 15446 |
overload(os.execute,binaryrunner,"os.execute") |
| 15447 |
overload(os.spawn,dummyrunner,"os.spawn") |
| 15448 |
overload(os.exec,dummyrunner,"os.exec") |
| 15449 |
overload(os.resultof,binaryrunner,"os.resultof") |
| 15450 |
overload(os.pipeto,binaryrunner,"os.pipeto") |
| 15451 |
overload(os.rename,filehandlertwo,"os.rename") |
| 15452 |
overload(os.remove,filehandlerone,"os.remove") |
| 15453 |
end |
| 15454 |
if lfs then |
| 15455 |
overload(lfs.chdir,filehandlerone,"lfs.chdir") |
| 15456 |
overload(lfs.mkdir,filehandlerone,"lfs.mkdir") |
| 15457 |
overload(lfs.rmdir,filehandlerone,"lfs.rmdir") |
| 15458 |
overload(lfs.isfile,filehandlerone,"lfs.isfile") |
| 15459 |
overload(lfs.isdir,filehandlerone,"lfs.isdir") |
| 15460 |
overload(lfs.attributes,filehandlerone,"lfs.attributes") |
| 15461 |
overload(lfs.dir,filehandlerone,"lfs.dir") |
| 15462 |
overload(lfs.lock_dir,filehandlerone,"lfs.lock_dir") |
| 15463 |
overload(lfs.touch,filehandlerone,"lfs.touch") |
| 15464 |
overload(lfs.link,filehandlertwo,"lfs.link") |
| 15465 |
overload(lfs.setmode,filehandlerone,"lfs.setmode") |
| 15466 |
overload(lfs.readlink,filehandlerone,"lfs.readlink") |
| 15467 |
overload(lfs.shortname,filehandlerone,"lfs.shortname") |
| 15468 |
overload(lfs.symlinkattributes,filehandlerone,"lfs.symlinkattributes") |
| 15469 |
end |
| 15470 |
if zip then |
| 15471 |
zip.open=register(zip.open,filehandlerone,"zip.open") |
| 15472 |
end |
| 15473 |
sandbox.registerroot=registerroot |
| 15474 |
sandbox.registerbinary=registerbinary |
| 15475 |
sandbox.registerlibrary=registerlibrary |
| 15476 |
sandbox.validfilename=validfilename |
| 15477 |
|
| 15478 |
|
| 15479 |
end -- of closure |
| 15480 |
|
| 15481 |
do -- create closure to overcome 200 locals limit |
| 15482 |
|
| 15483 |
package.loaded["util-mrg"] = package.loaded["util-mrg"] or true |
| 15484 |
|
| 15485 |
-- original size: 7819, stripped down to: 5881 |
| 15486 |
|
| 15487 |
if not modules then modules={} end modules ['util-mrg']={ |
| 15488 |
version=1.001, |
| 15489 |
comment="companion to luat-lib.mkiv", |
| 15490 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 15491 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 15492 |
license="see context related readme files" |
| 15493 |
} |
| 15494 |
local gsub,format=string.gsub,string.format |
| 15495 |
local concat=table.concat |
| 15496 |
local type,next=type,next |
| 15497 |
local P,R,S,V,Ct,C,Cs,Cc,Cp,Cmt,Cb,Cg=lpeg.P,lpeg.R,lpeg.S,lpeg.V,lpeg.Ct,lpeg.C,lpeg.Cs,lpeg.Cc,lpeg.Cp,lpeg.Cmt,lpeg.Cb,lpeg.Cg |
| 15498 |
local lpegmatch,patterns=lpeg.match,lpeg.patterns |
| 15499 |
utilities=utilities or {} |
| 15500 |
local merger=utilities.merger or {} |
| 15501 |
utilities.merger=merger |
| 15502 |
merger.strip_comment=true |
| 15503 |
local report=logs.reporter("system","merge") |
| 15504 |
utilities.report=report |
| 15505 |
local m_begin_merge="begin library merge" |
| 15506 |
local m_end_merge="end library merge" |
| 15507 |
local m_begin_closure="do -- create closure to overcome 200 locals limit" |
| 15508 |
local m_end_closure="end -- of closure" |
| 15509 |
local m_pattern="%c+".."%-%-%s+"..m_begin_merge.."%c+(.-)%c+".."%-%-%s+"..m_end_merge.."%c+" |
| 15510 |
local m_format="\n\n-- "..m_begin_merge.."\n%s\n".."-- "..m_end_merge.."\n\n" |
| 15511 |
local m_faked="-- ".."created merged file".."\n\n".."-- "..m_begin_merge.."\n\n".."-- "..m_end_merge.."\n\n" |
| 15512 |
local m_report=[[ |
| 15513 |
-- used libraries : %s |
| 15514 |
-- skipped libraries : %s |
| 15515 |
-- original bytes : %s |
| 15516 |
-- stripped bytes : %s |
| 15517 |
]] |
| 15518 |
local m_preloaded=[[package.loaded[%q] = package.loaded[%q] or true]] |
| 15519 |
local function self_fake() |
| 15520 |
return m_faked |
| 15521 |
end |
| 15522 |
local function self_nothing() |
| 15523 |
return "" |
| 15524 |
end |
| 15525 |
local function self_load(name) |
| 15526 |
local data=io.loaddata(name) or "" |
| 15527 |
if data=="" then |
| 15528 |
report("unknown file %a",name) |
| 15529 |
else |
| 15530 |
report("inserting file %a",name) |
| 15531 |
end |
| 15532 |
return data or "" |
| 15533 |
end |
| 15534 |
local space=patterns.space |
| 15535 |
local eol=patterns.newline |
| 15536 |
local equals=P("=")^0 |
| 15537 |
local open=P("[")*Cg(equals,"init")*P("[")*P("\n")^-1 |
| 15538 |
local close=P("]")*C(equals)*P("]") |
| 15539 |
local closeeq=Cmt(close*Cb("init"),function(s,i,a,b) return a==b end) |
| 15540 |
local longstring=open*(1-closeeq)^0*close |
| 15541 |
local quoted=patterns.quoted |
| 15542 |
local digit=patterns.digit |
| 15543 |
local emptyline=space^0*eol |
| 15544 |
local operator1=P("<=")+P(">=")+P("~=")+P("..")+S("/^<>=*+%%") |
| 15545 |
local operator2=S("*+/") |
| 15546 |
local operator3=S("-") |
| 15547 |
local operator4=P("..") |
| 15548 |
local separator=S(",;") |
| 15549 |
local ignore=(P("]")*space^1*P("=")*space^1*P("]"))/"]=["+(P("=")*space^1*P("{"))/"={"+(P("(")*space^1)/"("+(P("{")*(space+eol)^1*P("}"))/"{}" |
| 15550 |
local strings=quoted |
| 15551 |
local longcmt=(emptyline^0*P("--")*longstring*emptyline^0)/"" |
| 15552 |
local longstr=longstring |
| 15553 |
local comment=emptyline^0*P("--")*P("-")^0*(1-eol)^0*emptyline^1/"\n" |
| 15554 |
local optionalspaces=space^0/"" |
| 15555 |
local mandatespaces=space^1/"" |
| 15556 |
local optionalspacing=(eol+space)^0/"" |
| 15557 |
local mandatespacing=(eol+space)^1/"" |
| 15558 |
local pack=digit*space^1*operator4*optionalspacing+optionalspacing*operator1*optionalspacing+optionalspacing*operator2*optionalspaces+mandatespacing*operator3*mandatespaces+optionalspaces*separator*optionalspaces |
| 15559 |
local lines=emptyline^2/"\n" |
| 15560 |
local spaces=(space*space)/" " |
| 15561 |
local spaces=(space*space*space*space)/" " |
| 15562 |
local compact=Cs (( |
| 15563 |
ignore+strings+longcmt+longstr+comment+pack+lines+spaces+1 |
| 15564 |
)^1 ) |
| 15565 |
local strip=Cs((emptyline^2/"\n"+1)^0) |
| 15566 |
local stripreturn=Cs((1-P("return")*space^1*P(1-space-eol)^1*(space+eol)^0*P(-1))^1) |
| 15567 |
function merger.compact(data) |
| 15568 |
return lpegmatch(strip,lpegmatch(compact,data)) |
| 15569 |
end |
| 15570 |
local function self_compact(data) |
| 15571 |
local delta=0 |
| 15572 |
if merger.strip_comment then |
| 15573 |
local before=#data |
| 15574 |
data=lpegmatch(compact,data) |
| 15575 |
data=lpegmatch(strip,data) |
| 15576 |
local after=#data |
| 15577 |
delta=before-after |
| 15578 |
report("original size %s, compacted to %s, stripped %s",before,after,delta) |
| 15579 |
data=format("-- original size: %s, stripped down to: %s\n\n%s",before,after,data) |
| 15580 |
end |
| 15581 |
return lpegmatch(stripreturn,data) or data,delta |
| 15582 |
end |
| 15583 |
local function self_save(name,data) |
| 15584 |
if data~="" then |
| 15585 |
io.savedata(name,data) |
| 15586 |
report("saving %s with size %s",name,#data) |
| 15587 |
end |
| 15588 |
end |
| 15589 |
local function self_swap(data,code) |
| 15590 |
return data~="" and (gsub(data,m_pattern,function() return format(m_format,code) end,1)) or "" |
| 15591 |
end |
| 15592 |
local function self_libs(libs,list) |
| 15593 |
local result,f,frozen,foundpath={},nil,false,nil |
| 15594 |
result[#result+1]="\n" |
| 15595 |
if type(libs)=='string' then libs={ libs } end |
| 15596 |
if type(list)=='string' then list={ list } end |
| 15597 |
for i=1,#libs do |
| 15598 |
local lib=libs[i] |
| 15599 |
for j=1,#list do |
| 15600 |
local pth=gsub(list[j],"\\","/") |
| 15601 |
report("checking library path %a",pth) |
| 15602 |
local name=pth.."/"..lib |
| 15603 |
if lfs.isfile(name) then |
| 15604 |
foundpath=pth |
| 15605 |
end |
| 15606 |
end |
| 15607 |
if foundpath then break end |
| 15608 |
end |
| 15609 |
if foundpath then |
| 15610 |
report("using library path %a",foundpath) |
| 15611 |
local right,wrong,original,stripped={},{},0,0 |
| 15612 |
for i=1,#libs do |
| 15613 |
local lib=libs[i] |
| 15614 |
local fullname=foundpath.."/"..lib |
| 15615 |
if lfs.isfile(fullname) then |
| 15616 |
report("using library %a",fullname) |
| 15617 |
local preloaded=file.nameonly(lib) |
| 15618 |
local data=io.loaddata(fullname,true) |
| 15619 |
original=original+#data |
| 15620 |
local data,delta=self_compact(data) |
| 15621 |
right[#right+1]=lib |
| 15622 |
result[#result+1]=m_begin_closure |
| 15623 |
result[#result+1]=format(m_preloaded,preloaded,preloaded) |
| 15624 |
result[#result+1]=data |
| 15625 |
result[#result+1]=m_end_closure |
| 15626 |
stripped=stripped+delta |
| 15627 |
else |
| 15628 |
report("skipping library %a",fullname) |
| 15629 |
wrong[#wrong+1]=lib |
| 15630 |
end |
| 15631 |
end |
| 15632 |
right=#right>0 and concat(right," ") or "-" |
| 15633 |
wrong=#wrong>0 and concat(wrong," ") or "-" |
| 15634 |
report("used libraries: %a",right) |
| 15635 |
report("skipped libraries: %a",wrong) |
| 15636 |
report("original bytes: %a",original) |
| 15637 |
report("stripped bytes: %a",stripped) |
| 15638 |
result[#result+1]=format(m_report,right,wrong,original,stripped) |
| 15639 |
else |
| 15640 |
report("no valid library path found") |
| 15641 |
end |
| 15642 |
return concat(result,"\n\n") |
| 15643 |
end |
| 15644 |
function merger.selfcreate(libs,list,target) |
| 15645 |
if target then |
| 15646 |
self_save(target,self_swap(self_fake(),self_libs(libs,list))) |
| 15647 |
end |
| 15648 |
end |
| 15649 |
function merger.selfmerge(name,libs,list,target) |
| 15650 |
self_save(target or name,self_swap(self_load(name),self_libs(libs,list))) |
| 15651 |
end |
| 15652 |
function merger.selfclean(name) |
| 15653 |
self_save(name,self_swap(self_load(name),self_nothing())) |
| 15654 |
end |
| 15655 |
|
| 15656 |
|
| 15657 |
end -- of closure |
| 15658 |
|
| 15659 |
do -- create closure to overcome 200 locals limit |
| 15660 |
|
| 15661 |
package.loaded["util-env"] = package.loaded["util-env"] or true |
| 15662 |
|
| 15663 |
-- original size: 10594, stripped down to: 5492 |
| 15664 |
|
| 15665 |
if not modules then modules={} end modules ['util-env']={ |
| 15666 |
version=1.001, |
| 15667 |
comment="companion to luat-lib.mkiv", |
| 15668 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 15669 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 15670 |
license="see context related readme files" |
| 15671 |
} |
| 15672 |
local allocate,mark=utilities.storage.allocate,utilities.storage.mark |
| 15673 |
local format,sub,match,gsub,find=string.format,string.sub,string.match,string.gsub,string.find |
| 15674 |
local unquoted,quoted,optionalquoted=string.unquoted,string.quoted,string.optionalquoted |
| 15675 |
local concat,insert,remove=table.concat,table.insert,table.remove |
| 15676 |
local globfiles=dir.glob |
| 15677 |
environment=environment or {} |
| 15678 |
local environment=environment |
| 15679 |
os.setlocale(nil,nil) function os.setlocale() end |
| 15680 |
local validengines=allocate { |
| 15681 |
["luatex"]=true, |
| 15682 |
["luajittex"]=true, |
| 15683 |
} |
| 15684 |
local basicengines=allocate { |
| 15685 |
["luatex"]="luatex", |
| 15686 |
["texlua"]="luatex", |
| 15687 |
["texluac"]="luatex", |
| 15688 |
["luajittex"]="luajittex", |
| 15689 |
["texluajit"]="luajittex", |
| 15690 |
} |
| 15691 |
local luaengines=allocate { |
| 15692 |
["lua"]=true, |
| 15693 |
["luajit"]=true, |
| 15694 |
} |
| 15695 |
environment.validengines=validengines |
| 15696 |
environment.basicengines=basicengines |
| 15697 |
if not arg then |
| 15698 |
environment.used_as_library=true |
| 15699 |
elseif luaengines[file.removesuffix(arg[-1])] then |
| 15700 |
elseif validengines[file.removesuffix(arg[0])] then |
| 15701 |
if arg[1]=="--luaonly" then |
| 15702 |
arg[-1]=arg[0] |
| 15703 |
arg[ 0]=arg[2] |
| 15704 |
for k=3,#arg do |
| 15705 |
arg[k-2]=arg[k] |
| 15706 |
end |
| 15707 |
remove(arg) |
| 15708 |
remove(arg) |
| 15709 |
else |
| 15710 |
end |
| 15711 |
local originalzero=file.basename(arg[0]) |
| 15712 |
local specialmapping={ luatools=="base" } |
| 15713 |
if originalzero~="mtxrun" and originalzero~="mtxrun.lua" then |
| 15714 |
arg[0]=specialmapping[originalzero] or originalzero |
| 15715 |
insert(arg,0,"--script") |
| 15716 |
insert(arg,0,"mtxrun") |
| 15717 |
end |
| 15718 |
end |
| 15719 |
environment.arguments=allocate() |
| 15720 |
environment.files=allocate() |
| 15721 |
environment.sortedflags=nil |
| 15722 |
function environment.initializearguments(arg) |
| 15723 |
local arguments={} |
| 15724 |
local files={} |
| 15725 |
environment.arguments=arguments |
| 15726 |
environment.files=files |
| 15727 |
environment.sortedflags=nil |
| 15728 |
for index=1,#arg do |
| 15729 |
local argument=arg[index] |
| 15730 |
if index>0 then |
| 15731 |
local flag,value=match(argument,"^%-+(.-)=(.-)$") |
| 15732 |
if flag then |
| 15733 |
flag=gsub(flag,"^c:","") |
| 15734 |
arguments[flag]=unquoted(value or "") |
| 15735 |
else |
| 15736 |
flag=match(argument,"^%-+(.+)") |
| 15737 |
if flag then |
| 15738 |
flag=gsub(flag,"^c:","") |
| 15739 |
arguments[flag]=true |
| 15740 |
else |
| 15741 |
files[#files+1]=argument |
| 15742 |
end |
| 15743 |
end |
| 15744 |
end |
| 15745 |
end |
| 15746 |
if not environment.ownname then |
| 15747 |
if os.selfpath and os.selfname then |
| 15748 |
environment.ownname=file.addsuffix(file.join(os.selfpath,os.selfname),"lua") |
| 15749 |
end |
| 15750 |
end |
| 15751 |
environment.ownname=file.reslash(environment.ownname or arg[0] or 'unknown.lua') |
| 15752 |
end |
| 15753 |
function environment.setargument(name,value) |
| 15754 |
environment.arguments[name]=value |
| 15755 |
end |
| 15756 |
function environment.getargument(name,partial) |
| 15757 |
local arguments=environment.arguments |
| 15758 |
local sortedflags=environment.sortedflags |
| 15759 |
if arguments[name] then |
| 15760 |
return arguments[name] |
| 15761 |
elseif partial then |
| 15762 |
if not sortedflags then |
| 15763 |
sortedflags=allocate(table.sortedkeys(arguments)) |
| 15764 |
for k=1,#sortedflags do |
| 15765 |
sortedflags[k]="^"..sortedflags[k] |
| 15766 |
end |
| 15767 |
environment.sortedflags=sortedflags |
| 15768 |
end |
| 15769 |
for k=1,#sortedflags do |
| 15770 |
local v=sortedflags[k] |
| 15771 |
if find(name,v) then |
| 15772 |
return arguments[sub(v,2,#v)] |
| 15773 |
end |
| 15774 |
end |
| 15775 |
end |
| 15776 |
return nil |
| 15777 |
end |
| 15778 |
environment.argument=environment.getargument |
| 15779 |
function environment.splitarguments(separator) |
| 15780 |
local done,before,after=false,{},{} |
| 15781 |
local originalarguments=environment.originalarguments |
| 15782 |
for k=1,#originalarguments do |
| 15783 |
local v=originalarguments[k] |
| 15784 |
if not done and v==separator then |
| 15785 |
done=true |
| 15786 |
elseif done then |
| 15787 |
after[#after+1]=v |
| 15788 |
else |
| 15789 |
before[#before+1]=v |
| 15790 |
end |
| 15791 |
end |
| 15792 |
return before,after |
| 15793 |
end |
| 15794 |
function environment.reconstructcommandline(arg,noquote) |
| 15795 |
local resolveprefix=resolvers.resolve |
| 15796 |
arg=arg or environment.originalarguments |
| 15797 |
if noquote and #arg==1 then |
| 15798 |
return unquoted(resolveprefix and resolveprefix(arg[1]) or arg[1]) |
| 15799 |
elseif #arg>0 then |
| 15800 |
local result={} |
| 15801 |
for i=1,#arg do |
| 15802 |
result[i]=optionalquoted(resolveprefix and resolveprefix(arg[i]) or resolveprefix) |
| 15803 |
end |
| 15804 |
return concat(result," ") |
| 15805 |
else |
| 15806 |
return "" |
| 15807 |
end |
| 15808 |
end |
| 15809 |
function environment.relativepath(path,root) |
| 15810 |
if not path then |
| 15811 |
path="" |
| 15812 |
end |
| 15813 |
if not file.is_rootbased_path(path) then |
| 15814 |
if not root then |
| 15815 |
root=file.pathpart(environment.ownscript or environment.ownname or ".") |
| 15816 |
end |
| 15817 |
if root=="" then |
| 15818 |
root="." |
| 15819 |
end |
| 15820 |
path=root.."/"..path |
| 15821 |
end |
| 15822 |
return file.collapsepath(path,true) |
| 15823 |
end |
| 15824 |
if arg then |
| 15825 |
local newarg,instring={},false |
| 15826 |
for index=1,#arg do |
| 15827 |
local argument=arg[index] |
| 15828 |
if find(argument,"^\"") then |
| 15829 |
if find(argument,"\"$") then |
| 15830 |
newarg[#newarg+1]=gsub(argument,"^\"(.-)\"$","%1") |
| 15831 |
instring=false |
| 15832 |
else |
| 15833 |
newarg[#newarg+1]=gsub(argument,"^\"","") |
| 15834 |
instring=true |
| 15835 |
end |
| 15836 |
elseif find(argument,"\"$") then |
| 15837 |
if instring then |
| 15838 |
newarg[#newarg]=newarg[#newarg].." "..gsub(argument,"\"$","") |
| 15839 |
instring=false |
| 15840 |
else |
| 15841 |
newarg[#newarg+1]=argument |
| 15842 |
end |
| 15843 |
elseif instring then |
| 15844 |
newarg[#newarg]=newarg[#newarg].." "..argument |
| 15845 |
else |
| 15846 |
newarg[#newarg+1]=argument |
| 15847 |
end |
| 15848 |
end |
| 15849 |
for i=1,-5,-1 do |
| 15850 |
newarg[i]=arg[i] |
| 15851 |
end |
| 15852 |
environment.initializearguments(newarg) |
| 15853 |
environment.originalarguments=mark(newarg) |
| 15854 |
environment.rawarguments=mark(arg) |
| 15855 |
arg={} |
| 15856 |
end |
| 15857 |
function environment.globfiles(files) |
| 15858 |
if not files then |
| 15859 |
files=environment.files |
| 15860 |
end |
| 15861 |
if files then |
| 15862 |
local globbed={} |
| 15863 |
for i=1,#files do |
| 15864 |
local f=files[i] |
| 15865 |
if find(f,"%*") then |
| 15866 |
local g=globfiles(f) |
| 15867 |
if g then |
| 15868 |
for i=1,#g do |
| 15869 |
globbed[#globbed+1]=g[i] |
| 15870 |
end |
| 15871 |
end |
| 15872 |
else |
| 15873 |
globbed[#globbed+1]=f |
| 15874 |
end |
| 15875 |
end |
| 15876 |
return globbed |
| 15877 |
end |
| 15878 |
end |
| 15879 |
|
| 15880 |
|
| 15881 |
end -- of closure |
| 15882 |
|
| 15883 |
do -- create closure to overcome 200 locals limit |
| 15884 |
|
| 15885 |
package.loaded["luat-env"] = package.loaded["luat-env"] or true |
| 15886 |
|
| 15887 |
-- original size: 6293, stripped down to: 4141 |
| 15888 |
|
| 15889 |
if not modules then modules={} end modules ['luat-env']={ |
| 15890 |
version=1.001, |
| 15891 |
comment="companion to luat-lib.mkiv", |
| 15892 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 15893 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 15894 |
license="see context related readme files" |
| 15895 |
} |
| 15896 |
local rawset,loadfile=rawset,loadfile |
| 15897 |
local gsub=string.gsub |
| 15898 |
local trace_locating=false trackers.register("resolvers.locating",function(v) trace_locating=v end) |
| 15899 |
local report_lua=logs.reporter("resolvers","lua") |
| 15900 |
local luautilities=utilities.lua |
| 15901 |
local luasuffixes=luautilities.suffixes |
| 15902 |
local texgettoks=tex and tex.gettoks |
| 15903 |
environment=environment or {} |
| 15904 |
local environment=environment |
| 15905 |
local mt={ |
| 15906 |
__index=function(_,k) |
| 15907 |
if k=="version" then |
| 15908 |
local version=texgettoks and texgettoks("contextversiontoks") |
| 15909 |
if version and version~="" then |
| 15910 |
rawset(environment,"version",version) |
| 15911 |
return version |
| 15912 |
else |
| 15913 |
return "unknown" |
| 15914 |
end |
| 15915 |
elseif k=="jobname" or k=="formatname" then |
| 15916 |
local name=tex and tex[k] |
| 15917 |
if name or name=="" then |
| 15918 |
rawset(environment,k,name) |
| 15919 |
return name |
| 15920 |
else |
| 15921 |
return "unknown" |
| 15922 |
end |
| 15923 |
elseif k=="outputfilename" then |
| 15924 |
local name=environment.jobname |
| 15925 |
rawset(environment,k,name) |
| 15926 |
return name |
| 15927 |
end |
| 15928 |
end |
| 15929 |
} |
| 15930 |
setmetatable(environment,mt) |
| 15931 |
function environment.texfile(filename) |
| 15932 |
return resolvers.findfile(filename,'tex') |
| 15933 |
end |
| 15934 |
function environment.luafile(filename) |
| 15935 |
if CONTEXTLMTXMODE and CONTEXTLMTXMODE>0 and file.suffix(filename)=="lua" then |
| 15936 |
local resolved=resolvers.findfile(file.replacesuffix(filename,"lmt")) or "" |
| 15937 |
if resolved~="" then |
| 15938 |
return resolved |
| 15939 |
end |
| 15940 |
end |
| 15941 |
local resolved=resolvers.findfile(filename,'tex') or "" |
| 15942 |
if resolved~="" then |
| 15943 |
return resolved |
| 15944 |
end |
| 15945 |
resolved=resolvers.findfile(filename,'texmfscripts') or "" |
| 15946 |
if resolved~="" then |
| 15947 |
return resolved |
| 15948 |
end |
| 15949 |
return resolvers.findfile(filename,'luatexlibs') or "" |
| 15950 |
end |
| 15951 |
local stripindeed=false directives.register("system.compile.strip",function(v) stripindeed=v end) |
| 15952 |
local function strippable(filename) |
| 15953 |
if stripindeed then |
| 15954 |
local modu=modules[file.nameonly(filename)] |
| 15955 |
return modu and modu.dataonly |
| 15956 |
else |
| 15957 |
return false |
| 15958 |
end |
| 15959 |
end |
| 15960 |
function environment.luafilechunk(filename,silent,macros,optional) |
| 15961 |
filename=file.replacesuffix(filename,"lua") |
| 15962 |
local fullname=environment.luafile(filename) |
| 15963 |
if fullname and fullname~="" then |
| 15964 |
local data=luautilities.loadedluacode(fullname,strippable,filename,macros) |
| 15965 |
if not silent then |
| 15966 |
report_lua("loading file %a %s",fullname,not data and "failed" or "succeeded") |
| 15967 |
end |
| 15968 |
return data |
| 15969 |
else |
| 15970 |
if not optional and not silent then |
| 15971 |
report_lua("unknown file %a",filename) |
| 15972 |
end |
| 15973 |
return nil |
| 15974 |
end |
| 15975 |
end |
| 15976 |
function environment.loadluafile(filename,version) |
| 15977 |
local lucname,luaname,chunk |
| 15978 |
local basename=file.removesuffix(filename) |
| 15979 |
if basename==filename then |
| 15980 |
luaname=file.addsuffix(basename,luasuffixes.lua) |
| 15981 |
lucname=file.addsuffix(basename,luasuffixes.luc) |
| 15982 |
else |
| 15983 |
luaname=filename |
| 15984 |
lucname=nil |
| 15985 |
end |
| 15986 |
local fullname=(lucname and environment.luafile(lucname)) or "" |
| 15987 |
if fullname~="" then |
| 15988 |
if trace_locating then |
| 15989 |
report_lua("loading %a",fullname) |
| 15990 |
end |
| 15991 |
chunk=loadfile(fullname) |
| 15992 |
end |
| 15993 |
if chunk then |
| 15994 |
chunk() |
| 15995 |
if version then |
| 15996 |
local v=version |
| 15997 |
if modules and modules[filename] then |
| 15998 |
v=modules[filename].version |
| 15999 |
elseif versions and versions[filename] then |
| 16000 |
v=versions[filename] |
| 16001 |
end |
| 16002 |
if v==version then |
| 16003 |
return true |
| 16004 |
else |
| 16005 |
if trace_locating then |
| 16006 |
report_lua("version mismatch for %a, lua version %a, luc version %a",filename,v,version) |
| 16007 |
end |
| 16008 |
environment.loadluafile(filename) |
| 16009 |
end |
| 16010 |
else |
| 16011 |
return true |
| 16012 |
end |
| 16013 |
end |
| 16014 |
fullname=(luaname and environment.luafile(luaname)) or "" |
| 16015 |
if fullname~="" then |
| 16016 |
if trace_locating then |
| 16017 |
report_lua("loading %a",fullname) |
| 16018 |
end |
| 16019 |
chunk=loadfile(fullname) |
| 16020 |
if not chunk then |
| 16021 |
if trace_locating then |
| 16022 |
report_lua("unknown file %a",filename) |
| 16023 |
end |
| 16024 |
else |
| 16025 |
chunk() |
| 16026 |
return true |
| 16027 |
end |
| 16028 |
end |
| 16029 |
return false |
| 16030 |
end |
| 16031 |
environment.filenames=setmetatable({},{ |
| 16032 |
__index=function(t,k) |
| 16033 |
local v=environment.files[k] |
| 16034 |
if v then |
| 16035 |
return (gsub(v,"%.+$","")) |
| 16036 |
end |
| 16037 |
end, |
| 16038 |
__newindex=function(t,k) |
| 16039 |
end, |
| 16040 |
__len=function(t) |
| 16041 |
return #environment.files |
| 16042 |
end, |
| 16043 |
} ) |
| 16044 |
|
| 16045 |
|
| 16046 |
end -- of closure |
| 16047 |
|
| 16048 |
do -- create closure to overcome 200 locals limit |
| 16049 |
|
| 16050 |
package.loaded["util-zip"] = package.loaded["util-zip"] or true |
| 16051 |
|
| 16052 |
-- original size: 33051, stripped down to: 16324 |
| 16053 |
|
| 16054 |
if not modules then modules={} end modules ['util-zip']={ |
| 16055 |
version=1.001, |
| 16056 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 16057 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 16058 |
license="see context related readme files" |
| 16059 |
} |
| 16060 |
local type,tostring,tonumber=type,tostring,tonumber |
| 16061 |
local sort,concat=table.sort,table.concat |
| 16062 |
local find,format,sub,gsub=string.find,string.format,string.sub,string.gsub |
| 16063 |
local osdate,ostime,osclock=os.date,os.time,os.clock |
| 16064 |
local ioopen=io.open |
| 16065 |
local loaddata,savedata=io.loaddata,io.savedata |
| 16066 |
local filejoin,isdir,dirname,mkdirs=file.join,lfs.isdir,file.dirname,dir.mkdirs |
| 16067 |
local suffix,suffixes=file.suffix,file.suffixes |
| 16068 |
local openfile=io.open |
| 16069 |
gzip=gzip or {} |
| 16070 |
if not zlib then |
| 16071 |
zlib=xzip |
| 16072 |
elseif not xzip then |
| 16073 |
xzip=zlib |
| 16074 |
end |
| 16075 |
local files=utilities.files |
| 16076 |
local openfile=files.open |
| 16077 |
local closefile=files.close |
| 16078 |
local getsize=files.size |
| 16079 |
local readstring=files.readstring |
| 16080 |
local readcardinal2=files.readcardinal2le |
| 16081 |
local readcardinal4=files.readcardinal4le |
| 16082 |
local setposition=files.setposition |
| 16083 |
local getposition=files.getposition |
| 16084 |
local skipbytes=files.skip |
| 16085 |
local band=bit32.band |
| 16086 |
local rshift=bit32.rshift |
| 16087 |
local lshift=bit32.lshift |
| 16088 |
local zlibdecompress=zlib.decompress |
| 16089 |
local zlibdecompresssize=zlib.decompresssize |
| 16090 |
local zlibchecksum=zlib.crc32 |
| 16091 |
if not CONTEXTLMTXMODE or CONTEXTLMTXMODE==0 then |
| 16092 |
local cs=zlibchecksum |
| 16093 |
zlibchecksum=function(str,n) return cs(n or 0,str) end |
| 16094 |
end |
| 16095 |
local decompress=function(source) return zlibdecompress (source,-15) end |
| 16096 |
local decompresssize=function(source,targetsize) return zlibdecompresssize(source,targetsize,-15) end |
| 16097 |
local calculatecrc=function(buffer,initial) return zlibchecksum (initial or 0,buffer) end |
| 16098 |
local zipfiles={} |
| 16099 |
utilities.zipfiles=zipfiles |
| 16100 |
local openzipfile,closezipfile,unzipfile,foundzipfile,getziphash,getziplist do |
| 16101 |
function openzipfile(name) |
| 16102 |
return { |
| 16103 |
name=name, |
| 16104 |
handle=openfile(name,0), |
| 16105 |
} |
| 16106 |
end |
| 16107 |
local function update(handle,data) |
| 16108 |
position=data.offset |
| 16109 |
setposition(handle,position) |
| 16110 |
local signature=readstring(handle,4) |
| 16111 |
if signature=="PK\3\4" then |
| 16112 |
local version=readcardinal2(handle) |
| 16113 |
local flag=readcardinal2(handle) |
| 16114 |
local method=readcardinal2(handle) |
| 16115 |
skipbytes(handle,4) |
| 16116 |
local crc32=readcardinal4(handle) |
| 16117 |
local compressed=readcardinal4(handle) |
| 16118 |
local uncompressed=readcardinal4(handle) |
| 16119 |
local namelength=readcardinal2(handle) |
| 16120 |
local extralength=readcardinal2(handle) |
| 16121 |
local filename=readstring(handle,namelength) |
| 16122 |
local descriptor=band(flag,8)~=0 |
| 16123 |
local encrypted=band(flag,1)~=0 |
| 16124 |
local acceptable=method==0 or method==8 |
| 16125 |
local skipped=0 |
| 16126 |
local size=0 |
| 16127 |
if encrypted then |
| 16128 |
size=readcardinal2(handle) |
| 16129 |
skipbytes(handle,size) |
| 16130 |
skipped=skipped+size+2 |
| 16131 |
skipbytes(8) |
| 16132 |
skipped=skipped+8 |
| 16133 |
size=readcardinal2(handle) |
| 16134 |
skipbytes(handle,size) |
| 16135 |
skipped=skipped+size+2 |
| 16136 |
size=readcardinal4(handle) |
| 16137 |
skipbytes(handle,size) |
| 16138 |
skipped=skipped+size+4 |
| 16139 |
size=readcardinal2(handle) |
| 16140 |
skipbytes(handle,size) |
| 16141 |
skipped=skipped+size+2 |
| 16142 |
end |
| 16143 |
if acceptable then |
| 16144 |
if filename~=data.filename then |
| 16145 |
else |
| 16146 |
position=position+30+namelength+extralength+skipped |
| 16147 |
data.position=position |
| 16148 |
return position |
| 16149 |
end |
| 16150 |
else |
| 16151 |
end |
| 16152 |
end |
| 16153 |
data.position=false |
| 16154 |
return false |
| 16155 |
end |
| 16156 |
local function collect(z) |
| 16157 |
if not z.list then |
| 16158 |
local list={} |
| 16159 |
local hash={} |
| 16160 |
local position=0 |
| 16161 |
local index=0 |
| 16162 |
local handle=z.handle |
| 16163 |
local size=getsize(handle) |
| 16164 |
for i=size-4,size-64*1024,-1 do |
| 16165 |
setposition(handle,i) |
| 16166 |
local enddirsignature=readcardinal4(handle) |
| 16167 |
if enddirsignature==0x06054B50 then |
| 16168 |
local thisdisknumber=readcardinal2(handle) |
| 16169 |
local centraldisknumber=readcardinal2(handle) |
| 16170 |
local thisnofentries=readcardinal2(handle) |
| 16171 |
local totalnofentries=readcardinal2(handle) |
| 16172 |
local centralsize=readcardinal4(handle) |
| 16173 |
local centraloffset=readcardinal4(handle) |
| 16174 |
local commentlength=readcardinal2(handle) |
| 16175 |
local comment=readstring(handle,length) |
| 16176 |
if size-i>=22 then |
| 16177 |
if thisdisknumber==centraldisknumber then |
| 16178 |
setposition(handle,centraloffset) |
| 16179 |
while true do |
| 16180 |
if readcardinal4(handle)==0x02014B50 then |
| 16181 |
skipbytes(handle,4) |
| 16182 |
local flag=readcardinal2(handle) |
| 16183 |
local method=readcardinal2(handle) |
| 16184 |
skipbytes(handle,4) |
| 16185 |
local crc32=readcardinal4(handle) |
| 16186 |
local compressed=readcardinal4(handle) |
| 16187 |
local uncompressed=readcardinal4(handle) |
| 16188 |
local namelength=readcardinal2(handle) |
| 16189 |
local extralength=readcardinal2(handle) |
| 16190 |
local commentlength=readcardinal2(handle) |
| 16191 |
skipbytes(handle,8) |
| 16192 |
local headeroffset=readcardinal4(handle) |
| 16193 |
local filename=readstring(handle,namelength) |
| 16194 |
skipbytes(handle,extralength+commentlength) |
| 16195 |
local descriptor=band(flag,8)~=0 |
| 16196 |
local encrypted=band(flag,1)~=0 |
| 16197 |
local acceptable=method==0 or method==8 |
| 16198 |
if acceptable then |
| 16199 |
index=index+1 |
| 16200 |
local data={ |
| 16201 |
filename=filename, |
| 16202 |
index=index, |
| 16203 |
position=nil, |
| 16204 |
method=method, |
| 16205 |
compressed=compressed, |
| 16206 |
uncompressed=uncompressed, |
| 16207 |
crc32=crc32, |
| 16208 |
encrypted=encrypted, |
| 16209 |
offset=headeroffset, |
| 16210 |
} |
| 16211 |
hash[filename]=data |
| 16212 |
list[index]=data |
| 16213 |
end |
| 16214 |
else |
| 16215 |
break |
| 16216 |
end |
| 16217 |
end |
| 16218 |
end |
| 16219 |
break |
| 16220 |
end |
| 16221 |
end |
| 16222 |
end |
| 16223 |
z.list=list |
| 16224 |
z.hash=hash |
| 16225 |
end |
| 16226 |
end |
| 16227 |
function getziplist(z) |
| 16228 |
local list=z.list |
| 16229 |
if not list then |
| 16230 |
collect(z) |
| 16231 |
end |
| 16232 |
return z.list |
| 16233 |
end |
| 16234 |
function getziphash(z) |
| 16235 |
local hash=z.hash |
| 16236 |
if not hash then |
| 16237 |
collect(z) |
| 16238 |
end |
| 16239 |
return z.hash |
| 16240 |
end |
| 16241 |
function foundzipfile(z,name) |
| 16242 |
return getziphash(z)[name] |
| 16243 |
end |
| 16244 |
function closezipfile(z) |
| 16245 |
local f=z.handle |
| 16246 |
if f then |
| 16247 |
closefile(f) |
| 16248 |
z.handle=nil |
| 16249 |
end |
| 16250 |
end |
| 16251 |
function unzipfile(z,filename,check) |
| 16252 |
local hash=z.hash |
| 16253 |
if not hash then |
| 16254 |
hash=zipfiles.hash(z) |
| 16255 |
end |
| 16256 |
local data=hash[filename] |
| 16257 |
if not data then |
| 16258 |
end |
| 16259 |
if data then |
| 16260 |
local handle=z.handle |
| 16261 |
local position=data.position |
| 16262 |
local compressed=data.compressed |
| 16263 |
if position==nil then |
| 16264 |
position=update(handle,data) |
| 16265 |
end |
| 16266 |
if position and compressed>0 then |
| 16267 |
setposition(handle,position) |
| 16268 |
local result=readstring(handle,compressed) |
| 16269 |
if data.method==8 then |
| 16270 |
if decompresssize then |
| 16271 |
result=decompresssize(result,data.uncompressed) |
| 16272 |
else |
| 16273 |
result=decompress(result) |
| 16274 |
end |
| 16275 |
end |
| 16276 |
if check and data.crc32~=calculatecrc(result) then |
| 16277 |
print("checksum mismatch") |
| 16278 |
return "" |
| 16279 |
end |
| 16280 |
return result |
| 16281 |
else |
| 16282 |
return "" |
| 16283 |
end |
| 16284 |
end |
| 16285 |
end |
| 16286 |
zipfiles.open=openzipfile |
| 16287 |
zipfiles.close=closezipfile |
| 16288 |
zipfiles.unzip=unzipfile |
| 16289 |
zipfiles.hash=getziphash |
| 16290 |
zipfiles.list=getziplist |
| 16291 |
zipfiles.found=foundzipfile |
| 16292 |
end |
| 16293 |
if xzip then |
| 16294 |
local writecardinal1=files.writebyte |
| 16295 |
local writecardinal2=files.writecardinal2le |
| 16296 |
local writecardinal4=files.writecardinal4le |
| 16297 |
local logwriter=logs.writer |
| 16298 |
local globpattern=dir.globpattern |
| 16299 |
local compress=xzip.compress |
| 16300 |
local checksum=xzip.crc32 |
| 16301 |
local function fromdostime(dostime,dosdate) |
| 16302 |
return ostime { |
| 16303 |
year=rshift(dosdate,9)+1980, |
| 16304 |
month=band(rshift(dosdate,5),0x0F), |
| 16305 |
day=band((dosdate ),0x1F), |
| 16306 |
hour=band(rshift(dostime,11) ), |
| 16307 |
min=band(rshift(dostime,5),0x3F), |
| 16308 |
sec=band((dostime ),0x1F), |
| 16309 |
} |
| 16310 |
end |
| 16311 |
local function todostime(time) |
| 16312 |
local t=osdate("*t",time) |
| 16313 |
return |
| 16314 |
lshift(t.year-1980,9)+lshift(t.month,5)+t.day, |
| 16315 |
lshift(t.hour,11)+lshift(t.min,5)+rshift(t.sec,1) |
| 16316 |
end |
| 16317 |
local function openzip(filename,level,comment,verbose) |
| 16318 |
local f=ioopen(filename,"wb") |
| 16319 |
if f then |
| 16320 |
return { |
| 16321 |
filename=filename, |
| 16322 |
handle=f, |
| 16323 |
list={}, |
| 16324 |
level=tonumber(level) or 3, |
| 16325 |
comment=tostring(comment), |
| 16326 |
verbose=verbose, |
| 16327 |
uncompressed=0, |
| 16328 |
compressed=0, |
| 16329 |
} |
| 16330 |
end |
| 16331 |
end |
| 16332 |
local function writezip(z,name,data,level,time) |
| 16333 |
local f=z.handle |
| 16334 |
local list=z.list |
| 16335 |
local level=tonumber(level) or z.level or 3 |
| 16336 |
local method=8 |
| 16337 |
local zipped=compress(data,level) |
| 16338 |
local checksum=checksum(data) |
| 16339 |
local verbose=z.verbose |
| 16340 |
if not zipped then |
| 16341 |
method=0 |
| 16342 |
zipped=data |
| 16343 |
end |
| 16344 |
local start=f:seek() |
| 16345 |
local compressed=#zipped |
| 16346 |
local uncompressed=#data |
| 16347 |
z.compressed=z.compressed+compressed |
| 16348 |
z.uncompressed=z.uncompressed+uncompressed |
| 16349 |
if verbose then |
| 16350 |
local pct=100*compressed/uncompressed |
| 16351 |
if pct>=100 then |
| 16352 |
logwriter(format("%10i %s",uncompressed,name)) |
| 16353 |
else |
| 16354 |
logwriter(format("%10i %02.1f %s",uncompressed,pct,name)) |
| 16355 |
end |
| 16356 |
end |
| 16357 |
f:write("\x50\x4b\x03\x04") |
| 16358 |
writecardinal2(f,0) |
| 16359 |
writecardinal2(f,0) |
| 16360 |
writecardinal2(f,method) |
| 16361 |
writecardinal2(f,0) |
| 16362 |
writecardinal2(f,0) |
| 16363 |
writecardinal4(f,checksum) |
| 16364 |
writecardinal4(f,compressed) |
| 16365 |
writecardinal4(f,uncompressed) |
| 16366 |
writecardinal2(f,#name) |
| 16367 |
writecardinal2(f,0) |
| 16368 |
f:write(name) |
| 16369 |
f:write(zipped) |
| 16370 |
list[#list+1]={ #zipped,#data,name,checksum,start,time or 0 } |
| 16371 |
end |
| 16372 |
local function closezip(z) |
| 16373 |
local f=z.handle |
| 16374 |
local list=z.list |
| 16375 |
local comment=z.comment |
| 16376 |
local verbose=z.verbose |
| 16377 |
local count=#list |
| 16378 |
local start=f:seek() |
| 16379 |
for i=1,count do |
| 16380 |
local l=list[i] |
| 16381 |
local compressed=l[1] |
| 16382 |
local uncompressed=l[2] |
| 16383 |
local name=l[3] |
| 16384 |
local checksum=l[4] |
| 16385 |
local start=l[5] |
| 16386 |
local time=l[6] |
| 16387 |
local date,time=todostime(time) |
| 16388 |
f:write('\x50\x4b\x01\x02') |
| 16389 |
writecardinal2(f,0) |
| 16390 |
writecardinal2(f,0) |
| 16391 |
writecardinal2(f,0) |
| 16392 |
writecardinal2(f,8) |
| 16393 |
writecardinal2(f,time) |
| 16394 |
writecardinal2(f,date) |
| 16395 |
writecardinal4(f,checksum) |
| 16396 |
writecardinal4(f,compressed) |
| 16397 |
writecardinal4(f,uncompressed) |
| 16398 |
writecardinal2(f,#name) |
| 16399 |
writecardinal2(f,0) |
| 16400 |
writecardinal2(f,0) |
| 16401 |
writecardinal2(f,0) |
| 16402 |
writecardinal2(f,0) |
| 16403 |
writecardinal4(f,0) |
| 16404 |
writecardinal4(f,start) |
| 16405 |
f:write(name) |
| 16406 |
end |
| 16407 |
local stop=f:seek() |
| 16408 |
local size=stop-start |
| 16409 |
f:write('\x50\x4b\x05\x06') |
| 16410 |
writecardinal2(f,0) |
| 16411 |
writecardinal2(f,0) |
| 16412 |
writecardinal2(f,count) |
| 16413 |
writecardinal2(f,count) |
| 16414 |
writecardinal4(f,size) |
| 16415 |
writecardinal4(f,start) |
| 16416 |
if type(comment)=="string" and comment~="" then |
| 16417 |
writecardinal2(f,#comment) |
| 16418 |
f:write(comment) |
| 16419 |
else |
| 16420 |
writecardinal2(f,0) |
| 16421 |
end |
| 16422 |
if verbose then |
| 16423 |
local compressed=z.compressed |
| 16424 |
local uncompressed=z.uncompressed |
| 16425 |
local filename=z.filename |
| 16426 |
local pct=100*compressed/uncompressed |
| 16427 |
logwriter("") |
| 16428 |
if pct>=100 then |
| 16429 |
logwriter(format("%10i %s",uncompressed,filename)) |
| 16430 |
else |
| 16431 |
logwriter(format("%10i %02.1f %s",uncompressed,pct,filename)) |
| 16432 |
end |
| 16433 |
end |
| 16434 |
f:close() |
| 16435 |
end |
| 16436 |
local function zipdir(zipname,path,level,verbose) |
| 16437 |
if type(zipname)=="table" then |
| 16438 |
verbose=zipname.verbose |
| 16439 |
level=zipname.level |
| 16440 |
path=zipname.path |
| 16441 |
zipname=zipname.zipname |
| 16442 |
end |
| 16443 |
if not zipname or zipname=="" then |
| 16444 |
return |
| 16445 |
end |
| 16446 |
if not path or path=="" then |
| 16447 |
path="." |
| 16448 |
end |
| 16449 |
if not isdir(path) then |
| 16450 |
return |
| 16451 |
end |
| 16452 |
path=gsub(path,"\\+","/") |
| 16453 |
path=gsub(path,"/+","/") |
| 16454 |
local list={} |
| 16455 |
local count=0 |
| 16456 |
globpattern(path,"",true,function(name,size,time) |
| 16457 |
count=count+1 |
| 16458 |
list[count]={ name,time } |
| 16459 |
end) |
| 16460 |
sort(list,function(a,b) |
| 16461 |
return a[1]<b[1] |
| 16462 |
end) |
| 16463 |
local zipf=openzip(zipname,level,comment,verbose) |
| 16464 |
if zipf then |
| 16465 |
local p=#path+2 |
| 16466 |
for i=1,count do |
| 16467 |
local li=list[i] |
| 16468 |
local name=li[1] |
| 16469 |
local time=li[2] |
| 16470 |
local data=loaddata(name) |
| 16471 |
local name=sub(name,p,#name) |
| 16472 |
writezip(zipf,name,data,level,time,verbose) |
| 16473 |
end |
| 16474 |
closezip(zipf) |
| 16475 |
end |
| 16476 |
end |
| 16477 |
local function unzipdir(zipname,path,verbose,collect,validate) |
| 16478 |
if type(zipname)=="table" then |
| 16479 |
validate=zipname.validate |
| 16480 |
collect=zipname.collect |
| 16481 |
verbose=zipname.verbose |
| 16482 |
path=zipname.path |
| 16483 |
zipname=zipname.zipname |
| 16484 |
end |
| 16485 |
if not zipname or zipname=="" then |
| 16486 |
return |
| 16487 |
end |
| 16488 |
if not path or path=="" then |
| 16489 |
path="." |
| 16490 |
end |
| 16491 |
local z=openzipfile(zipname) |
| 16492 |
if z then |
| 16493 |
local list=getziplist(z) |
| 16494 |
if list then |
| 16495 |
local total=0 |
| 16496 |
local count=#list |
| 16497 |
local step=number.idiv(count,10) |
| 16498 |
local done=0 |
| 16499 |
local steps=verbose=="steps" |
| 16500 |
local time=steps and osclock() |
| 16501 |
if collect then |
| 16502 |
collect={} |
| 16503 |
else |
| 16504 |
collect=false |
| 16505 |
end |
| 16506 |
for i=1,count do |
| 16507 |
local l=list[i] |
| 16508 |
local n=l.filename |
| 16509 |
if not validate or validate(n) then |
| 16510 |
local d=unzipfile(z,n) |
| 16511 |
if d then |
| 16512 |
local p=filejoin(path,n) |
| 16513 |
if mkdirs(dirname(p)) then |
| 16514 |
if steps then |
| 16515 |
total=total+#d |
| 16516 |
done=done+1 |
| 16517 |
if done>=step then |
| 16518 |
done=0 |
| 16519 |
logwriter(format("%4i files of %4i done, %10i bytes, %0.3f seconds",i,count,total,osclock()-time)) |
| 16520 |
end |
| 16521 |
elseif verbose then |
| 16522 |
logwriter(n) |
| 16523 |
end |
| 16524 |
savedata(p,d) |
| 16525 |
if collect then |
| 16526 |
collect[#collect+1]=p |
| 16527 |
end |
| 16528 |
end |
| 16529 |
else |
| 16530 |
logwriter(format("problem with file %s",n)) |
| 16531 |
end |
| 16532 |
else |
| 16533 |
end |
| 16534 |
end |
| 16535 |
if steps then |
| 16536 |
logwriter(format("%4i files of %4i done, %10i bytes, %0.3f seconds",count,count,total,osclock()-time)) |
| 16537 |
end |
| 16538 |
closezipfile(z) |
| 16539 |
if collect then |
| 16540 |
return collect |
| 16541 |
end |
| 16542 |
else |
| 16543 |
closezipfile(z) |
| 16544 |
end |
| 16545 |
end |
| 16546 |
end |
| 16547 |
zipfiles.zipdir=zipdir |
| 16548 |
zipfiles.unzipdir=unzipdir |
| 16549 |
end |
| 16550 |
local pattern="^\x1F\x8B\x08" |
| 16551 |
local gziplevel=3 |
| 16552 |
function gzip.suffix(filename) |
| 16553 |
local suffix,extra=suffixes(filename) |
| 16554 |
local gzipped=extra=="gz" |
| 16555 |
return suffix,gzipped |
| 16556 |
end |
| 16557 |
function gzip.compressed(s) |
| 16558 |
return s and find(s,pattern) |
| 16559 |
end |
| 16560 |
local getdecompressed |
| 16561 |
local putcompressed |
| 16562 |
if gzip.compress then |
| 16563 |
local gzipwindow=15+16 |
| 16564 |
local compress=zlib.compress |
| 16565 |
local decompress=zlib.decompress |
| 16566 |
getdecompressed=function(str) |
| 16567 |
return decompress(str,gzipwindow) |
| 16568 |
end |
| 16569 |
putcompressed=function(str,level) |
| 16570 |
return compress(str,level or gziplevel,nil,gzipwindow) |
| 16571 |
end |
| 16572 |
else |
| 16573 |
local gzipwindow=-15 |
| 16574 |
local identifier="\x1F\x8B" |
| 16575 |
local compress=zlib.compress |
| 16576 |
local decompress=zlib.decompress |
| 16577 |
local zlibchecksum=zlib.crc32 |
| 16578 |
if not CONTEXTLMTXMODE or CONTEXTLMTXMODE==0 then |
| 16579 |
local cs=zlibchecksum |
| 16580 |
zlibchecksum=function(str,n) return cs(n or 0,str) end |
| 16581 |
end |
| 16582 |
local streams=utilities.streams |
| 16583 |
local openstream=streams.openstring |
| 16584 |
local closestream=streams.close |
| 16585 |
local getposition=streams.getposition |
| 16586 |
local readbyte=streams.readbyte |
| 16587 |
local readcardinal4=streams.readcardinal4le |
| 16588 |
local readcardinal2=streams.readcardinal2le |
| 16589 |
local readstring=streams.readstring |
| 16590 |
local readcstring=streams.readcstring |
| 16591 |
local skipbytes=streams.skip |
| 16592 |
local tocardinal1=streams.tocardinal1 |
| 16593 |
local tocardinal4=streams.tocardinal4le |
| 16594 |
getdecompressed=function(str) |
| 16595 |
local s=openstream(str) |
| 16596 |
local identifier=readstring(s,2) |
| 16597 |
local method=readbyte(s,1) |
| 16598 |
local flags=readbyte(s,1) |
| 16599 |
local timestamp=readcardinal4(s) |
| 16600 |
local compression=readbyte(s,1) |
| 16601 |
local operating=readbyte(s,1) |
| 16602 |
local isjusttext=band(flags,0x01)~=0 and true or false |
| 16603 |
local extrasize=band(flags,0x04)~=0 and readcardinal2(s) or 0 |
| 16604 |
local filename=band(flags,0x08)~=0 and readcstring(s) or "" |
| 16605 |
local comment=band(flags,0x10)~=0 and readcstring(s) or "" |
| 16606 |
local checksum=band(flags,0x02)~=0 and readcardinal2(s) or 0 |
| 16607 |
local compressed=readstring(s,#str) |
| 16608 |
local data=decompress(compressed,gzipwindow) |
| 16609 |
return data |
| 16610 |
end |
| 16611 |
putcompressed=function(str,level,originalname) |
| 16612 |
return concat { |
| 16613 |
identifier, |
| 16614 |
tocardinal1(0x08), |
| 16615 |
tocardinal1(0x08), |
| 16616 |
tocardinal4(os.time()), |
| 16617 |
tocardinal1(0x02), |
| 16618 |
tocardinal1(0xFF), |
| 16619 |
(originalname or "unknownname").."\0", |
| 16620 |
compress(str,level,nil,gzipwindow), |
| 16621 |
tocardinal4(zlibchecksum(str)), |
| 16622 |
tocardinal4(#str), |
| 16623 |
} |
| 16624 |
end |
| 16625 |
end |
| 16626 |
function gzip.load(filename) |
| 16627 |
local f=openfile(filename,"rb") |
| 16628 |
if not f then |
| 16629 |
else |
| 16630 |
local data=f:read("*all") |
| 16631 |
f:close() |
| 16632 |
if data and data~="" then |
| 16633 |
if suffix(filename)=="gz" then |
| 16634 |
data=getdecompressed(data) |
| 16635 |
end |
| 16636 |
return data |
| 16637 |
end |
| 16638 |
end |
| 16639 |
end |
| 16640 |
function gzip.save(filename,data,level,originalname) |
| 16641 |
if suffix(filename)~="gz" then |
| 16642 |
filename=filename..".gz" |
| 16643 |
end |
| 16644 |
local f=openfile(filename,"wb") |
| 16645 |
if f then |
| 16646 |
data=putcompressed(data or "",level or gziplevel,originalname) |
| 16647 |
f:write(data) |
| 16648 |
f:close() |
| 16649 |
return #data |
| 16650 |
end |
| 16651 |
end |
| 16652 |
function gzip.compress(s,level) |
| 16653 |
if s and not find(s,pattern) then |
| 16654 |
if not level then |
| 16655 |
level=gziplevel |
| 16656 |
elseif level<=0 then |
| 16657 |
return s |
| 16658 |
elseif level>9 then |
| 16659 |
level=9 |
| 16660 |
end |
| 16661 |
return putcompressed(s,level or gziplevel) or s |
| 16662 |
end |
| 16663 |
end |
| 16664 |
function gzip.decompress(s) |
| 16665 |
if s and find(s,pattern) then |
| 16666 |
return getdecompressed(s) |
| 16667 |
else |
| 16668 |
return s |
| 16669 |
end |
| 16670 |
end |
| 16671 |
|
| 16672 |
|
| 16673 |
end -- of closure |
| 16674 |
|
| 16675 |
do -- create closure to overcome 200 locals limit |
| 16676 |
|
| 16677 |
package.loaded["util-sig"] = package.loaded["util-sig"] or true |
| 16678 |
|
| 16679 |
-- original size: 5963, stripped down to: 3240 |
| 16680 |
|
| 16681 |
if not modules then modules={} end modules ['util-sig']={ |
| 16682 |
version=1.001, |
| 16683 |
comment="companion to luat-lib.mkiv", |
| 16684 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 16685 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 16686 |
license="see context related readme files" |
| 16687 |
} |
| 16688 |
local type,dofile=type,dofile |
| 16689 |
local format=string.format |
| 16690 |
local resultof=os.resultof |
| 16691 |
utilities=utilities or {} |
| 16692 |
utilities.signals=utilities.signals or {} |
| 16693 |
local signals=utilities.signals |
| 16694 |
local serialwrite=serial and serial.write |
| 16695 |
if serialwrite then |
| 16696 |
signals.serialwrite=serialwrite |
| 16697 |
signals.serialprefix=":lmtx:1:" |
| 16698 |
local ports={} |
| 16699 |
local function clean(port) |
| 16700 |
return "serial_port_"..string.gsub(port,"[^a-zA-Z0-9]","").."" |
| 16701 |
end |
| 16702 |
function signals.serialopen(port,baud) |
| 16703 |
local p=ports[port] |
| 16704 |
if p then |
| 16705 |
p.level=p.level+1 |
| 16706 |
elseif os.getenv(clean(port))=="busy" then |
| 16707 |
p=false |
| 16708 |
else |
| 16709 |
os.setenv(clean(port),"busy") |
| 16710 |
p={ |
| 16711 |
handle=serial.open(port,baud), |
| 16712 |
level=1, |
| 16713 |
port=port, |
| 16714 |
} |
| 16715 |
ports[port]=p |
| 16716 |
end |
| 16717 |
return p |
| 16718 |
end |
| 16719 |
function signals.serialclose(p) |
| 16720 |
if type(p)=="string" then |
| 16721 |
p=ports[p] |
| 16722 |
end |
| 16723 |
if type(p)=="table" then |
| 16724 |
local l=p.level |
| 16725 |
local h=p.handle |
| 16726 |
if l and h and l>0 then |
| 16727 |
l=l-1 |
| 16728 |
if l==0 then |
| 16729 |
serial.close(h) |
| 16730 |
ports[p.port]=nil |
| 16731 |
end |
| 16732 |
p.level=l |
| 16733 |
end |
| 16734 |
end |
| 16735 |
end |
| 16736 |
function signals.serialsend(p,str) |
| 16737 |
if type(p)=="string" then |
| 16738 |
p=ports[p] |
| 16739 |
end |
| 16740 |
if p then |
| 16741 |
local l=p.level |
| 16742 |
local h=p.handle |
| 16743 |
if l and h and l>0 then |
| 16744 |
return serial.send(h,str) |
| 16745 |
end |
| 16746 |
end |
| 16747 |
end |
| 16748 |
function signals.serialwrite(port,baud,str) |
| 16749 |
return serialwrite(post,baud,str) |
| 16750 |
end |
| 16751 |
function signals.serialfast(port,baud,str) |
| 16752 |
local p=port[port] |
| 16753 |
if not p then |
| 16754 |
p=signals.serialopen(port,baud) |
| 16755 |
end |
| 16756 |
if p then |
| 16757 |
local h=p.handle |
| 16758 |
if h then |
| 16759 |
return serial.send(h,str) |
| 16760 |
end |
| 16761 |
end |
| 16762 |
end |
| 16763 |
else |
| 16764 |
function signals.serialwrite() |
| 16765 |
return false |
| 16766 |
end |
| 16767 |
end |
| 16768 |
local loaded=table.setmetatableindex(function(t,signal) |
| 16769 |
local signalled=false |
| 16770 |
if type(signal)=="string" then |
| 16771 |
local found=resolvers.findfile("util-sig-imp-"..signal..".lua") or "" |
| 16772 |
if found~="" then |
| 16773 |
local triggers=dofile(found) |
| 16774 |
if triggers then |
| 16775 |
signalled=triggers.trigger |
| 16776 |
end |
| 16777 |
end |
| 16778 |
if type(signalled)~="function" then |
| 16779 |
signalled=function(state,run) |
| 16780 |
local c=format("mtxrun --script %s --state=%s --run=%i",signal,state,run) |
| 16781 |
resultof(c) |
| 16782 |
end |
| 16783 |
end |
| 16784 |
end |
| 16785 |
t[signal]=signalled |
| 16786 |
return signalled |
| 16787 |
end) |
| 16788 |
utilities.signals.version=1.001 |
| 16789 |
local configuration="ctxsignals.lua" |
| 16790 |
signals.configuration=configuration |
| 16791 |
function signals.loadstate() |
| 16792 |
local name=resolvers.findfile(configuration) |
| 16793 |
if name then |
| 16794 |
local data=table.load(name) |
| 16795 |
if data then |
| 16796 |
return data,name |
| 16797 |
end |
| 16798 |
end |
| 16799 |
end |
| 16800 |
function signals.savestate(data) |
| 16801 |
if data then |
| 16802 |
local name=resolvers.findfile(configuration) or "" |
| 16803 |
if name=="" then |
| 16804 |
name=configuration |
| 16805 |
end |
| 16806 |
table.save(name,data) |
| 16807 |
return name |
| 16808 |
end |
| 16809 |
end |
| 16810 |
function signals.initialize(signal) |
| 16811 |
if signal then |
| 16812 |
local data=signals.loadstate() |
| 16813 |
if not data then |
| 16814 |
elseif not data.signals or not data.signals[signal] then |
| 16815 |
elseif data.usage.enabled==false then |
| 16816 |
return false |
| 16817 |
elseif data.signals[signal].enabled==false then |
| 16818 |
return false |
| 16819 |
else |
| 16820 |
return loaded[signal] or false |
| 16821 |
end |
| 16822 |
else |
| 16823 |
return false |
| 16824 |
end |
| 16825 |
end |
| 16826 |
signals.report=logs.reporter("signal") |
| 16827 |
|
| 16828 |
|
| 16829 |
end -- of closure |
| 16830 |
|
| 16831 |
do -- create closure to overcome 200 locals limit |
| 16832 |
|
| 16833 |
package.loaded["lxml-tab"] = package.loaded["lxml-tab"] or true |
| 16834 |
|
| 16835 |
-- original size: 62856, stripped down to: 36566 |
| 16836 |
|
| 16837 |
if not modules then modules={} end modules ['lxml-tab']={ |
| 16838 |
version=1.001, |
| 16839 |
comment="this module is the basis for the lxml-* ones", |
| 16840 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 16841 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 16842 |
license="see context related readme files" |
| 16843 |
} |
| 16844 |
local trace_entities=false trackers.register("xml.entities",function(v) trace_entities=v end) |
| 16845 |
local report_xml=logs and logs.reporter("xml","core") or function(...) print(string.format(...)) end |
| 16846 |
if lpeg.setmaxstack then lpeg.setmaxstack(1000) end |
| 16847 |
xml=xml or {} |
| 16848 |
local xml=xml |
| 16849 |
local concat,remove,insert=table.concat,table.remove,table.insert |
| 16850 |
local type,next,setmetatable,getmetatable,tonumber,rawset,select=type,next,setmetatable,getmetatable,tonumber,rawset,select |
| 16851 |
local lower,find,match,gsub=string.lower,string.find,string.match,string.gsub |
| 16852 |
local sort=table.sort |
| 16853 |
local utfchar=utf.char |
| 16854 |
local lpegmatch,lpegpatterns=lpeg.match,lpeg.patterns |
| 16855 |
local P,S,R,C,V,C,Cs=lpeg.P,lpeg.S,lpeg.R,lpeg.C,lpeg.V,lpeg.C,lpeg.Cs |
| 16856 |
local formatters=string.formatters |
| 16857 |
do |
| 16858 |
xml.xmlns=xml.xmlns or {} |
| 16859 |
local check=P(false) |
| 16860 |
local parse=check |
| 16861 |
function xml.registerns(namespace,pattern) |
| 16862 |
check=check+C(P(lower(pattern)))/namespace |
| 16863 |
parse=P { P(check)+1*V(1) } |
| 16864 |
end |
| 16865 |
function xml.checkns(namespace,url) |
| 16866 |
local ns=lpegmatch(parse,lower(url)) |
| 16867 |
if ns and namespace~=ns then |
| 16868 |
xml.xmlns[namespace]=ns |
| 16869 |
end |
| 16870 |
end |
| 16871 |
function xml.resolvens(url) |
| 16872 |
return lpegmatch(parse,lower(url)) or "" |
| 16873 |
end |
| 16874 |
end |
| 16875 |
local nsremap,resolvens=xml.xmlns,xml.resolvens |
| 16876 |
local stack,level,top,at,xmlns,errorstr |
| 16877 |
local entities,parameters |
| 16878 |
local strip,utfize,resolve,cleanup,resolve_predefined,unify_predefined |
| 16879 |
local dcache,hcache,acache |
| 16880 |
local mt,dt,nt |
| 16881 |
local currentfilename,currentline,linenumbers |
| 16882 |
local reported_at_errors |
| 16883 |
local grammar_parsed_text_one |
| 16884 |
local grammar_parsed_text_two |
| 16885 |
local grammar_unparsed_text |
| 16886 |
local handle_hex_entity |
| 16887 |
local handle_dec_entity |
| 16888 |
local handle_any_entity_dtd |
| 16889 |
local handle_any_entity_text |
| 16890 |
local function preparexmlstate(settings) |
| 16891 |
if settings then |
| 16892 |
linenumbers=settings.linenumbers |
| 16893 |
stack={} |
| 16894 |
level=0 |
| 16895 |
top={} |
| 16896 |
at={} |
| 16897 |
mt={} |
| 16898 |
dt={} |
| 16899 |
nt=0 |
| 16900 |
xmlns={} |
| 16901 |
errorstr=nil |
| 16902 |
strip=settings.strip_cm_and_dt |
| 16903 |
utfize=settings.utfize_entities |
| 16904 |
resolve=settings.resolve_entities |
| 16905 |
resolve_predefined=settings.resolve_predefined_entities |
| 16906 |
unify_predefined=settings.unify_predefined_entities |
| 16907 |
cleanup=settings.text_cleanup |
| 16908 |
entities=settings.entities or {} |
| 16909 |
currentfilename=settings.currentresource |
| 16910 |
currentline=1 |
| 16911 |
parameters={} |
| 16912 |
reported_at_errors={} |
| 16913 |
dcache={} |
| 16914 |
hcache={} |
| 16915 |
acache={} |
| 16916 |
if utfize==nil then |
| 16917 |
settings.utfize_entities=true |
| 16918 |
utfize=true |
| 16919 |
end |
| 16920 |
if resolve_predefined==nil then |
| 16921 |
settings.resolve_predefined_entities=true |
| 16922 |
resolve_predefined=true |
| 16923 |
end |
| 16924 |
else |
| 16925 |
linenumbers=false |
| 16926 |
stack=nil |
| 16927 |
level=nil |
| 16928 |
top=nil |
| 16929 |
at=nil |
| 16930 |
mt=nil |
| 16931 |
dt=nil |
| 16932 |
nt=nil |
| 16933 |
xmlns=nil |
| 16934 |
errorstr=nil |
| 16935 |
strip=nil |
| 16936 |
utfize=nil |
| 16937 |
resolve=nil |
| 16938 |
resolve_predefined=nil |
| 16939 |
unify_predefined=nil |
| 16940 |
cleanup=nil |
| 16941 |
entities=nil |
| 16942 |
parameters=nil |
| 16943 |
reported_at_errors=nil |
| 16944 |
dcache=nil |
| 16945 |
hcache=nil |
| 16946 |
acache=nil |
| 16947 |
currentfilename=nil |
| 16948 |
currentline=1 |
| 16949 |
end |
| 16950 |
end |
| 16951 |
local function initialize_mt(root) |
| 16952 |
mt={ __index=root } |
| 16953 |
end |
| 16954 |
function xml.setproperty(root,k,v) |
| 16955 |
getmetatable(root).__index[k]=v |
| 16956 |
end |
| 16957 |
function xml.checkerror(top,toclose) |
| 16958 |
return "" |
| 16959 |
end |
| 16960 |
local checkns=xml.checkns |
| 16961 |
local function add_attribute(namespace,tag,value) |
| 16962 |
if cleanup and value~="" then |
| 16963 |
value=cleanup(value) |
| 16964 |
end |
| 16965 |
if tag=="xmlns" then |
| 16966 |
xmlns[#xmlns+1]=resolvens(value) |
| 16967 |
at[tag]=value |
| 16968 |
elseif namespace=="" then |
| 16969 |
at[tag]=value |
| 16970 |
elseif namespace=="xmlns" then |
| 16971 |
checkns(tag,value) |
| 16972 |
at["xmlns:"..tag]=value |
| 16973 |
else |
| 16974 |
at[namespace..":"..tag]=value |
| 16975 |
end |
| 16976 |
end |
| 16977 |
local function add_empty(spacing,namespace,tag) |
| 16978 |
if spacing~="" then |
| 16979 |
nt=nt+1 |
| 16980 |
dt[nt]=spacing |
| 16981 |
end |
| 16982 |
local resolved=namespace=="" and xmlns[#xmlns] or nsremap[namespace] or namespace |
| 16983 |
top=stack[level] |
| 16984 |
dt=top.dt |
| 16985 |
nt=#dt+1 |
| 16986 |
local t=linenumbers and { |
| 16987 |
ns=namespace or "", |
| 16988 |
rn=resolved, |
| 16989 |
tg=tag, |
| 16990 |
at=at, |
| 16991 |
dt={}, |
| 16992 |
ni=nt, |
| 16993 |
cf=currentfilename, |
| 16994 |
cl=currentline, |
| 16995 |
__p__=top, |
| 16996 |
} or { |
| 16997 |
ns=namespace or "", |
| 16998 |
rn=resolved, |
| 16999 |
tg=tag, |
| 17000 |
at=at, |
| 17001 |
dt={}, |
| 17002 |
ni=nt, |
| 17003 |
__p__=top, |
| 17004 |
} |
| 17005 |
dt[nt]=t |
| 17006 |
setmetatable(t,mt) |
| 17007 |
if at.xmlns then |
| 17008 |
remove(xmlns) |
| 17009 |
end |
| 17010 |
at={} |
| 17011 |
end |
| 17012 |
local function add_begin(spacing,namespace,tag) |
| 17013 |
if spacing~="" then |
| 17014 |
nt=nt+1 |
| 17015 |
dt[nt]=spacing |
| 17016 |
end |
| 17017 |
local resolved=namespace=="" and xmlns[#xmlns] or nsremap[namespace] or namespace |
| 17018 |
dt={} |
| 17019 |
top=linenumbers and { |
| 17020 |
ns=namespace or "", |
| 17021 |
rn=resolved, |
| 17022 |
tg=tag, |
| 17023 |
at=at, |
| 17024 |
dt=dt, |
| 17025 |
ni=nil, |
| 17026 |
cf=currentfilename, |
| 17027 |
cl=currentline, |
| 17028 |
__p__=stack[level], |
| 17029 |
} or { |
| 17030 |
ns=namespace or "", |
| 17031 |
rn=resolved, |
| 17032 |
tg=tag, |
| 17033 |
at=at, |
| 17034 |
dt=dt, |
| 17035 |
ni=nil, |
| 17036 |
__p__=stack[level], |
| 17037 |
} |
| 17038 |
setmetatable(top,mt) |
| 17039 |
nt=0 |
| 17040 |
level=level+1 |
| 17041 |
stack[level]=top |
| 17042 |
at={} |
| 17043 |
end |
| 17044 |
local function add_end(spacing,namespace,tag) |
| 17045 |
if spacing~="" then |
| 17046 |
nt=nt+1 |
| 17047 |
dt[nt]=spacing |
| 17048 |
end |
| 17049 |
local toclose=stack[level] |
| 17050 |
level=level-1 |
| 17051 |
top=stack[level] |
| 17052 |
if level<1 then |
| 17053 |
errorstr=formatters["unable to close %s %s"](tag,xml.checkerror(top,toclose) or "") |
| 17054 |
report_xml(errorstr) |
| 17055 |
elseif toclose.tg~=tag then |
| 17056 |
errorstr=formatters["unable to close %s with %s %s"](toclose.tg,tag,xml.checkerror(top,toclose) or "") |
| 17057 |
report_xml(errorstr) |
| 17058 |
end |
| 17059 |
dt=top.dt |
| 17060 |
nt=#dt+1 |
| 17061 |
dt[nt]=toclose |
| 17062 |
toclose.ni=nt |
| 17063 |
if toclose.at.xmlns then |
| 17064 |
remove(xmlns) |
| 17065 |
end |
| 17066 |
end |
| 17067 |
local function add_text(text) |
| 17068 |
if text=="" then |
| 17069 |
return |
| 17070 |
elseif cleanup then |
| 17071 |
if nt>0 then |
| 17072 |
local s=dt[nt] |
| 17073 |
if type(s)=="string" then |
| 17074 |
dt[nt]=s..cleanup(text) |
| 17075 |
else |
| 17076 |
nt=nt+1 |
| 17077 |
dt[nt]=cleanup(text) |
| 17078 |
end |
| 17079 |
else |
| 17080 |
nt=1 |
| 17081 |
dt[1]=cleanup(text) |
| 17082 |
end |
| 17083 |
else |
| 17084 |
if nt>0 then |
| 17085 |
local s=dt[nt] |
| 17086 |
if type(s)=="string" then |
| 17087 |
dt[nt]=s..text |
| 17088 |
else |
| 17089 |
nt=nt+1 |
| 17090 |
dt[nt]=text |
| 17091 |
end |
| 17092 |
else |
| 17093 |
nt=1 |
| 17094 |
dt[1]=text |
| 17095 |
end |
| 17096 |
end |
| 17097 |
end |
| 17098 |
local function add_special(what,spacing,text) |
| 17099 |
if spacing~="" then |
| 17100 |
nt=nt+1 |
| 17101 |
dt[nt]=spacing |
| 17102 |
end |
| 17103 |
if strip and (what=="@cm@" or what=="@dt@") then |
| 17104 |
else |
| 17105 |
nt=nt+1 |
| 17106 |
dt[nt]=linenumbers and { |
| 17107 |
special=true, |
| 17108 |
ns="", |
| 17109 |
tg=what, |
| 17110 |
ni=nil, |
| 17111 |
dt={ text }, |
| 17112 |
cf=currentfilename, |
| 17113 |
cl=currentline, |
| 17114 |
} or { |
| 17115 |
special=true, |
| 17116 |
ns="", |
| 17117 |
tg=what, |
| 17118 |
ni=nil, |
| 17119 |
dt={ text }, |
| 17120 |
} |
| 17121 |
end |
| 17122 |
end |
| 17123 |
local function set_message(txt) |
| 17124 |
errorstr="garbage at the end of the file: "..gsub(txt,"([ \n\r\t]*)","") |
| 17125 |
end |
| 17126 |
local function attribute_value_error(str) |
| 17127 |
if not reported_at_errors[str] then |
| 17128 |
report_xml("invalid attribute value %a",str) |
| 17129 |
reported_at_errors[str]=true |
| 17130 |
at._error_=str |
| 17131 |
end |
| 17132 |
return str |
| 17133 |
end |
| 17134 |
local function attribute_specification_error(str) |
| 17135 |
if not reported_at_errors[str] then |
| 17136 |
report_xml("invalid attribute specification %a",str) |
| 17137 |
reported_at_errors[str]=true |
| 17138 |
at._error_=str |
| 17139 |
end |
| 17140 |
return str |
| 17141 |
end |
| 17142 |
do |
| 17143 |
local badentity="&" |
| 17144 |
xml.placeholders={ |
| 17145 |
unknown_dec_entity=function(str) return str=="" and badentity or formatters["&%s;"](str) end, |
| 17146 |
unknown_hex_entity=function(str) return formatters["&#x%s;"](str) end, |
| 17147 |
unknown_any_entity=function(str) return formatters["&#x%s;"](str) end, |
| 17148 |
} |
| 17149 |
local function fromhex(s) |
| 17150 |
local n=tonumber(s,16) |
| 17151 |
if n then |
| 17152 |
return utfchar(n) |
| 17153 |
else |
| 17154 |
return formatters["h:%s"](s),true |
| 17155 |
end |
| 17156 |
end |
| 17157 |
local function fromdec(s) |
| 17158 |
local n=tonumber(s) |
| 17159 |
if n then |
| 17160 |
return utfchar(n) |
| 17161 |
else |
| 17162 |
return formatters["d:%s"](s),true |
| 17163 |
end |
| 17164 |
end |
| 17165 |
local p_rest=(1-P(";"))^0 |
| 17166 |
local p_many=P(1)^0 |
| 17167 |
local parsedentity=P("&#")*(P("x")*(p_rest/fromhex)+(p_rest/fromdec))*P(";")*P(-1)+P ("#")*(P("x")*(p_many/fromhex)+(p_many/fromdec)) |
| 17168 |
xml.parsedentitylpeg=parsedentity |
| 17169 |
local predefined_unified={ |
| 17170 |
[38]="&", |
| 17171 |
[42]=""", |
| 17172 |
[47]="'", |
| 17173 |
[74]="<", |
| 17174 |
[76]=">", |
| 17175 |
} |
| 17176 |
local predefined_simplified={ |
| 17177 |
[38]="&",amp="&", |
| 17178 |
[42]='"',quot='"', |
| 17179 |
[47]="'",apos="'", |
| 17180 |
[74]="<",lt="<", |
| 17181 |
[76]=">",gt=">", |
| 17182 |
} |
| 17183 |
local nofprivates=0xF0000 |
| 17184 |
local privates_u={ |
| 17185 |
[ [[&]] ]="&", |
| 17186 |
[ [["]] ]=""", |
| 17187 |
[ [[']] ]="'", |
| 17188 |
[ [[<]] ]="<", |
| 17189 |
[ [[>]] ]=">", |
| 17190 |
} |
| 17191 |
local privates_p={ |
| 17192 |
} |
| 17193 |
local privates_s={ |
| 17194 |
[ [["]] ]="&U+22;", |
| 17195 |
[ [[#]] ]="&U+23;", |
| 17196 |
[ [[$]] ]="&U+24;", |
| 17197 |
[ [[%]] ]="&U+25;", |
| 17198 |
[ [[&]] ]="&U+26;", |
| 17199 |
[ [[']] ]="&U+27;", |
| 17200 |
[ [[<]] ]="&U+3C;", |
| 17201 |
[ [[>]] ]="&U+3E;", |
| 17202 |
[ [[\]] ]="&U+5C;", |
| 17203 |
[ [[{]] ]="&U+7B;", |
| 17204 |
[ [[|]] ]="&U+7C;", |
| 17205 |
[ [[}]] ]="&U+7D;", |
| 17206 |
[ [[~]] ]="&U+7E;", |
| 17207 |
} |
| 17208 |
local privates_x={ |
| 17209 |
[ [["]] ]="&U+22;", |
| 17210 |
[ [[#]] ]="&U+23;", |
| 17211 |
[ [[$]] ]="&U+24;", |
| 17212 |
[ [[%]] ]="&U+25;", |
| 17213 |
[ [[']] ]="&U+27;", |
| 17214 |
[ [[\]] ]="&U+5C;", |
| 17215 |
[ [[{]] ]="&U+7B;", |
| 17216 |
[ [[|]] ]="&U+7C;", |
| 17217 |
[ [[}]] ]="&U+7D;", |
| 17218 |
[ [[~]] ]="&U+7E;", |
| 17219 |
} |
| 17220 |
local privates_n={ |
| 17221 |
} |
| 17222 |
utilities.storage.mark(privates_u) |
| 17223 |
utilities.storage.mark(privates_p) |
| 17224 |
utilities.storage.mark(privates_s) |
| 17225 |
utilities.storage.mark(privates_x) |
| 17226 |
utilities.storage.mark(privates_n) |
| 17227 |
local escaped=utf.remapper(privates_u,"dynamic") |
| 17228 |
local unprivatized=utf.remapper(privates_p,"dynamic") |
| 17229 |
local unspecialized=utf.remapper(privates_s,"dynamic") |
| 17230 |
local despecialized=utf.remapper(privates_x,"dynamic") |
| 17231 |
xml.unprivatized=unprivatized |
| 17232 |
xml.unspecialized=unspecialized |
| 17233 |
xml.despecialized=despecialized |
| 17234 |
xml.escaped=escaped |
| 17235 |
local function unescaped(s) |
| 17236 |
local p=privates_n[s] |
| 17237 |
if not p then |
| 17238 |
nofprivates=nofprivates+1 |
| 17239 |
p=utfchar(nofprivates) |
| 17240 |
privates_n[s]=p |
| 17241 |
s="&"..s..";" |
| 17242 |
privates_u[p]=s |
| 17243 |
privates_p[p]=s |
| 17244 |
privates_s[p]=s |
| 17245 |
end |
| 17246 |
return p |
| 17247 |
end |
| 17248 |
xml.privatetoken=unescaped |
| 17249 |
xml.privatecodes=privates_n |
| 17250 |
xml.specialcodes=privates_s |
| 17251 |
function xml.addspecialcode(key,value) |
| 17252 |
privates_s[key]=value or "&"..s..";" |
| 17253 |
end |
| 17254 |
handle_hex_entity=function(str) |
| 17255 |
local h=hcache[str] |
| 17256 |
if not h then |
| 17257 |
local n=tonumber(str,16) |
| 17258 |
h=unify_predefined and predefined_unified[n] |
| 17259 |
if h then |
| 17260 |
if trace_entities then |
| 17261 |
report_xml("utfize, converting hex entity &#x%s; into %a",str,h) |
| 17262 |
end |
| 17263 |
elseif utfize then |
| 17264 |
h=(n and utfchar(n)) or xml.unknown_hex_entity(str) or "" |
| 17265 |
if not n then |
| 17266 |
report_xml("utfize, ignoring hex entity &#x%s;",str) |
| 17267 |
elseif trace_entities then |
| 17268 |
report_xml("utfize, converting hex entity &#x%s; into %a",str,h) |
| 17269 |
end |
| 17270 |
else |
| 17271 |
if trace_entities then |
| 17272 |
report_xml("found entity &#x%s;",str) |
| 17273 |
end |
| 17274 |
h="&#x"..str..";" |
| 17275 |
end |
| 17276 |
hcache[str]=h |
| 17277 |
end |
| 17278 |
return h |
| 17279 |
end |
| 17280 |
handle_dec_entity=function(str) |
| 17281 |
local d=dcache[str] |
| 17282 |
if not d then |
| 17283 |
local n=tonumber(str) |
| 17284 |
d=unify_predefined and predefined_unified[n] |
| 17285 |
if d then |
| 17286 |
if trace_entities then |
| 17287 |
report_xml("utfize, converting dec entity &#%s; into %a",str,d) |
| 17288 |
end |
| 17289 |
elseif utfize then |
| 17290 |
d=(n and utfchar(n)) or placeholders.unknown_dec_entity(str) or "" |
| 17291 |
if not n then |
| 17292 |
report_xml("utfize, ignoring dec entity &#%s;",str) |
| 17293 |
elseif trace_entities then |
| 17294 |
report_xml("utfize, converting dec entity &#%s; into %a",str,d) |
| 17295 |
end |
| 17296 |
else |
| 17297 |
if trace_entities then |
| 17298 |
report_xml("found entity &#%s;",str) |
| 17299 |
end |
| 17300 |
d="&#"..str..";" |
| 17301 |
end |
| 17302 |
dcache[str]=d |
| 17303 |
end |
| 17304 |
return d |
| 17305 |
end |
| 17306 |
handle_any_entity_dtd=function(str) |
| 17307 |
if resolve then |
| 17308 |
local a=resolve_predefined and predefined_simplified[str] |
| 17309 |
if a then |
| 17310 |
if trace_entities then |
| 17311 |
report_xml("resolving entity &%s; to predefined %a",str,a) |
| 17312 |
end |
| 17313 |
else |
| 17314 |
if type(resolve)=="function" then |
| 17315 |
a=resolve(str,entities) or entities[str] |
| 17316 |
else |
| 17317 |
a=entities[str] |
| 17318 |
end |
| 17319 |
if a then |
| 17320 |
if type(a)=="function" then |
| 17321 |
if trace_entities then |
| 17322 |
report_xml("expanding entity &%s; to function call",str) |
| 17323 |
end |
| 17324 |
a=a(str) or "" |
| 17325 |
end |
| 17326 |
a=lpegmatch(parsedentity,a) or a |
| 17327 |
if trace_entities then |
| 17328 |
report_xml("resolving entity &%s; to internal %a",str,a) |
| 17329 |
end |
| 17330 |
else |
| 17331 |
local unknown_any_entity=placeholders.unknown_any_entity |
| 17332 |
if unknown_any_entity then |
| 17333 |
a=unknown_any_entity(str) or "" |
| 17334 |
end |
| 17335 |
if a then |
| 17336 |
if trace_entities then |
| 17337 |
report_xml("resolving entity &%s; to external %s",str,a) |
| 17338 |
end |
| 17339 |
else |
| 17340 |
if trace_entities then |
| 17341 |
report_xml("keeping entity &%s;",str) |
| 17342 |
end |
| 17343 |
if str=="" then |
| 17344 |
a=badentity |
| 17345 |
else |
| 17346 |
a="&"..str..";" |
| 17347 |
end |
| 17348 |
end |
| 17349 |
end |
| 17350 |
end |
| 17351 |
return a |
| 17352 |
else |
| 17353 |
local a=acache[str] |
| 17354 |
if not a then |
| 17355 |
a=resolve_predefined and predefined_simplified[str] |
| 17356 |
if a then |
| 17357 |
acache[str]=a |
| 17358 |
if trace_entities then |
| 17359 |
report_xml("entity &%s; becomes %a",str,a) |
| 17360 |
end |
| 17361 |
elseif str=="" then |
| 17362 |
if trace_entities then |
| 17363 |
report_xml("invalid entity &%s;",str) |
| 17364 |
end |
| 17365 |
a=badentity |
| 17366 |
acache[str]=a |
| 17367 |
else |
| 17368 |
if trace_entities then |
| 17369 |
report_xml("entity &%s; is made private",str) |
| 17370 |
end |
| 17371 |
a=unescaped(str) |
| 17372 |
acache[str]=a |
| 17373 |
end |
| 17374 |
end |
| 17375 |
return a |
| 17376 |
end |
| 17377 |
end |
| 17378 |
handle_any_entity_text=function(str) |
| 17379 |
if resolve then |
| 17380 |
local a=resolve_predefined and predefined_simplified[str] |
| 17381 |
if a then |
| 17382 |
if trace_entities then |
| 17383 |
report_xml("resolving entity &%s; to predefined %a",str,a) |
| 17384 |
end |
| 17385 |
else |
| 17386 |
if type(resolve)=="function" then |
| 17387 |
a=resolve(str,entities) or entities[str] |
| 17388 |
else |
| 17389 |
a=entities[str] |
| 17390 |
end |
| 17391 |
if a then |
| 17392 |
if type(a)=="function" then |
| 17393 |
if trace_entities then |
| 17394 |
report_xml("expanding entity &%s; to function call",str) |
| 17395 |
end |
| 17396 |
a=a(str) or "" |
| 17397 |
end |
| 17398 |
a=lpegmatch(grammar_parsed_text_two,a) or a |
| 17399 |
if type(a)=="number" then |
| 17400 |
return "" |
| 17401 |
else |
| 17402 |
a=lpegmatch(parsedentity,a) or a |
| 17403 |
if trace_entities then |
| 17404 |
report_xml("resolving entity &%s; to internal %a",str,a) |
| 17405 |
end |
| 17406 |
end |
| 17407 |
if trace_entities then |
| 17408 |
report_xml("resolving entity &%s; to internal %a",str,a) |
| 17409 |
end |
| 17410 |
else |
| 17411 |
local unknown_any_entity=placeholders.unknown_any_entity |
| 17412 |
if unknown_any_entity then |
| 17413 |
a=unknown_any_entity(str) or "" |
| 17414 |
end |
| 17415 |
if a then |
| 17416 |
if trace_entities then |
| 17417 |
report_xml("resolving entity &%s; to external %s",str,a) |
| 17418 |
end |
| 17419 |
else |
| 17420 |
if trace_entities then |
| 17421 |
report_xml("keeping entity &%s;",str) |
| 17422 |
end |
| 17423 |
if str=="" then |
| 17424 |
a=badentity |
| 17425 |
else |
| 17426 |
a="&"..str..";" |
| 17427 |
end |
| 17428 |
end |
| 17429 |
end |
| 17430 |
end |
| 17431 |
return a |
| 17432 |
else |
| 17433 |
local a=acache[str] |
| 17434 |
if not a then |
| 17435 |
a=resolve_predefined and predefined_simplified[str] |
| 17436 |
if a then |
| 17437 |
acache[str]=a |
| 17438 |
if trace_entities then |
| 17439 |
report_xml("entity &%s; becomes %a",str,a) |
| 17440 |
end |
| 17441 |
elseif str=="" then |
| 17442 |
if trace_entities then |
| 17443 |
report_xml("invalid entity &%s;",str) |
| 17444 |
end |
| 17445 |
a=badentity |
| 17446 |
acache[str]=a |
| 17447 |
else |
| 17448 |
if trace_entities then |
| 17449 |
report_xml("entity &%s; is made private",str) |
| 17450 |
end |
| 17451 |
a=unescaped(str) |
| 17452 |
acache[str]=a |
| 17453 |
end |
| 17454 |
end |
| 17455 |
return a |
| 17456 |
end |
| 17457 |
end |
| 17458 |
local p_rest=(1-P(";"))^1 |
| 17459 |
local spec={ |
| 17460 |
[0x23]="\\Ux{23}", |
| 17461 |
[0x24]="\\Ux{24}", |
| 17462 |
[0x25]="\\Ux{25}", |
| 17463 |
[0x5C]="\\Ux{5C}", |
| 17464 |
[0x7B]="\\Ux{7B}", |
| 17465 |
[0x7C]="\\Ux{7C}", |
| 17466 |
[0x7D]="\\Ux{7D}", |
| 17467 |
[0x7E]="\\Ux{7E}", |
| 17468 |
} |
| 17469 |
local hash=table.setmetatableindex(spec,function(t,k) |
| 17470 |
local v=utfchar(k) |
| 17471 |
t[k]=v |
| 17472 |
return v |
| 17473 |
end) |
| 17474 |
local function fromuni(s) |
| 17475 |
local n=tonumber(s,16) |
| 17476 |
if n then |
| 17477 |
return hash[n] |
| 17478 |
else |
| 17479 |
return formatters["u:%s"](s),true |
| 17480 |
end |
| 17481 |
end |
| 17482 |
local function fromhex(s) |
| 17483 |
local n=tonumber(s,16) |
| 17484 |
if n then |
| 17485 |
return hash[n] |
| 17486 |
else |
| 17487 |
return formatters["h:%s"](s),true |
| 17488 |
end |
| 17489 |
end |
| 17490 |
local function fromdec(s) |
| 17491 |
local n=tonumber(s) |
| 17492 |
if n then |
| 17493 |
return hash[n] |
| 17494 |
else |
| 17495 |
return formatters["d:%s"](s),true |
| 17496 |
end |
| 17497 |
end |
| 17498 |
local reparsedentity=P("U+")*(p_rest/fromuni)+P("#")*( |
| 17499 |
P("x")*(p_rest/fromhex)+p_rest/fromdec |
| 17500 |
) |
| 17501 |
local hash=table.setmetatableindex(function(t,k) |
| 17502 |
local v=utfchar(k) |
| 17503 |
t[k]=v |
| 17504 |
return v |
| 17505 |
end) |
| 17506 |
local function fromuni(s) |
| 17507 |
local n=tonumber(s,16) |
| 17508 |
if n then |
| 17509 |
return hash[n] |
| 17510 |
else |
| 17511 |
return formatters["u:%s"](s),true |
| 17512 |
end |
| 17513 |
end |
| 17514 |
local function fromhex(s) |
| 17515 |
local n=tonumber(s,16) |
| 17516 |
if n then |
| 17517 |
return hash[n] |
| 17518 |
else |
| 17519 |
return formatters["h:%s"](s),true |
| 17520 |
end |
| 17521 |
end |
| 17522 |
local function fromdec(s) |
| 17523 |
local n=tonumber(s) |
| 17524 |
if n then |
| 17525 |
return hash[n] |
| 17526 |
else |
| 17527 |
return formatters["d:%s"](s),true |
| 17528 |
end |
| 17529 |
end |
| 17530 |
local unescapedentity=P("U+")*(p_rest/fromuni)+P("#")*( |
| 17531 |
P("x")*(p_rest/fromhex)+p_rest/fromdec |
| 17532 |
) |
| 17533 |
xml.reparsedentitylpeg=reparsedentity |
| 17534 |
xml.unescapedentitylpeg=unescapedentity |
| 17535 |
end |
| 17536 |
local escaped=xml.escaped |
| 17537 |
local unescaped=xml.unescaped |
| 17538 |
local placeholders=xml.placeholders |
| 17539 |
local function handle_end_entity(str) |
| 17540 |
report_xml("error in entity, %a found without ending %a",str,";") |
| 17541 |
return str |
| 17542 |
end |
| 17543 |
local function handle_crap_error(chr) |
| 17544 |
report_xml("error in parsing, unexpected %a found ",chr) |
| 17545 |
add_text(chr) |
| 17546 |
return chr |
| 17547 |
end |
| 17548 |
local function handlenewline() |
| 17549 |
currentline=currentline+1 |
| 17550 |
end |
| 17551 |
local spacetab=S(' \t') |
| 17552 |
local space=S(' \r\n\t') |
| 17553 |
local newline=lpegpatterns.newline/handlenewline |
| 17554 |
local anything=P(1) |
| 17555 |
local open=P('<') |
| 17556 |
local close=P('>') |
| 17557 |
local squote=S("'") |
| 17558 |
local dquote=S('"') |
| 17559 |
local equal=P('=') |
| 17560 |
local slash=P('/') |
| 17561 |
local colon=P(':') |
| 17562 |
local semicolon=P(';') |
| 17563 |
local ampersand=P('&') |
| 17564 |
local valid_0=R("\128\255") |
| 17565 |
local valid_1=R('az','AZ')+S('_')+valid_0 |
| 17566 |
local valid_2=valid_1+R('09')+S('-.') |
| 17567 |
local valid=valid_1*valid_2^0 |
| 17568 |
local name_yes=C(valid^1)*colon*C(valid^1) |
| 17569 |
local name_nop=C(P(true))*C(valid^1) |
| 17570 |
local name=name_yes+name_nop |
| 17571 |
local utfbom=lpegpatterns.utfbom |
| 17572 |
local spacing=C(space^0) |
| 17573 |
local space_nl=spacetab+newline |
| 17574 |
local spacing_nl=Cs((space_nl)^0) |
| 17575 |
local anything_nl=newline+P(1) |
| 17576 |
local function weirdentity(k,v) |
| 17577 |
if trace_entities then |
| 17578 |
report_xml("registering %s entity %a as %a","weird",k,v) |
| 17579 |
end |
| 17580 |
parameters[k]=v |
| 17581 |
end |
| 17582 |
local function normalentity(k,v) |
| 17583 |
if trace_entities then |
| 17584 |
report_xml("registering %s entity %a as %a","normal",k,v) |
| 17585 |
end |
| 17586 |
entities[k]=v |
| 17587 |
end |
| 17588 |
local function systementity(k,v,n) |
| 17589 |
if trace_entities then |
| 17590 |
report_xml("registering %s entity %a as %a","system",k,v) |
| 17591 |
end |
| 17592 |
entities[k]=v |
| 17593 |
end |
| 17594 |
local function publicentity(k,v,n) |
| 17595 |
if trace_entities then |
| 17596 |
report_xml("registering %s entity %a as %a","public",k,v) |
| 17597 |
end |
| 17598 |
entities[k]=v |
| 17599 |
end |
| 17600 |
local function entityfile(pattern,k,v,n) |
| 17601 |
if n then |
| 17602 |
local okay,data |
| 17603 |
local loadbinfile=resolvers and resolvers.loadbinfile |
| 17604 |
if loadbinfile then |
| 17605 |
okay,data=loadbinfile(n) |
| 17606 |
else |
| 17607 |
data=io.loaddata(n) |
| 17608 |
okay=data and data~="" |
| 17609 |
end |
| 17610 |
if okay then |
| 17611 |
if trace_entities then |
| 17612 |
report_xml("loading public entities %a as %a from %a",k,v,n) |
| 17613 |
end |
| 17614 |
lpegmatch(pattern,data) |
| 17615 |
return |
| 17616 |
end |
| 17617 |
end |
| 17618 |
report_xml("ignoring public entities %a as %a from %a",k,v,n) |
| 17619 |
end |
| 17620 |
local function install(spacenewline,spacing,anything) |
| 17621 |
local anyentitycontent=(1-open-semicolon-space-close-ampersand)^0 |
| 17622 |
local hexentitycontent=R("AF","af","09")^1 |
| 17623 |
local decentitycontent=R("09")^1 |
| 17624 |
local parsedentity=P("#")/""*( |
| 17625 |
P("x")/""*(hexentitycontent/handle_hex_entity)+(decentitycontent/handle_dec_entity) |
| 17626 |
)+(anyentitycontent/handle_any_entity_dtd) |
| 17627 |
local parsedentity_text=P("#")/""*( |
| 17628 |
P("x")/""*(hexentitycontent/handle_hex_entity)+(decentitycontent/handle_dec_entity) |
| 17629 |
)+(anyentitycontent/handle_any_entity_text) |
| 17630 |
local entity=(ampersand/"")*parsedentity*(semicolon/"")+ampersand*(anyentitycontent/handle_end_entity) |
| 17631 |
local entity_text=(ampersand/"")*parsedentity_text*(semicolon/"")+ampersand*(anyentitycontent/handle_end_entity) |
| 17632 |
local text_unparsed=Cs((anything-open)^1) |
| 17633 |
local text_parsed=(Cs((anything-open-ampersand)^1)/add_text+Cs(entity_text)/add_text)^1 |
| 17634 |
local somespace=(spacenewline)^1 |
| 17635 |
local optionalspace=(spacenewline)^0 |
| 17636 |
local value=(squote*Cs((entity+(anything-squote))^0)*squote)+(dquote*Cs((entity+(anything-dquote))^0)*dquote) |
| 17637 |
local endofattributes=slash*close+close |
| 17638 |
local whatever=space*name*optionalspace*equal |
| 17639 |
local wrongvalue=Cs(P(entity+(1-space-endofattributes))^1)/attribute_value_error |
| 17640 |
local attributevalue=value+wrongvalue |
| 17641 |
local attribute=(somespace*name*optionalspace*equal*optionalspace*attributevalue)/add_attribute |
| 17642 |
local attributes=(attribute+somespace^-1*(((anything-endofattributes)^1)/attribute_specification_error))^0 |
| 17643 |
local parsedtext=text_parsed |
| 17644 |
local unparsedtext=text_unparsed/add_text |
| 17645 |
local balanced=P { "["*((anything-S"[]")+V(1))^0*"]" } |
| 17646 |
local emptyelement=(spacing*open*name*attributes*optionalspace*slash*close)/add_empty |
| 17647 |
local beginelement=(spacing*open*name*attributes*optionalspace*close)/add_begin |
| 17648 |
local endelement=(spacing*open*slash*name*optionalspace*close)/add_end |
| 17649 |
local begincomment=open*P("!--") |
| 17650 |
local endcomment=P("--")*close |
| 17651 |
local begininstruction=open*P("?") |
| 17652 |
local endinstruction=P("?")*close |
| 17653 |
local begincdata=open*P("![CDATA[") |
| 17654 |
local endcdata=P("]]")*close |
| 17655 |
local someinstruction=C((anything-endinstruction)^0) |
| 17656 |
local somecomment=C((anything-endcomment )^0) |
| 17657 |
local somecdata=C((anything-endcdata )^0) |
| 17658 |
local begindoctype=open*P("!DOCTYPE") |
| 17659 |
local enddoctype=close |
| 17660 |
local beginset=P("[") |
| 17661 |
local endset=P("]") |
| 17662 |
local wrdtypename=C((anything-somespace-P(";"))^1) |
| 17663 |
local doctypename=C((anything-somespace-close)^0) |
| 17664 |
local elementdoctype=optionalspace*P("<!ELEMENT")*(anything-close)^0*close |
| 17665 |
local basiccomment=begincomment*((anything-endcomment)^0)*endcomment |
| 17666 |
local weirdentitytype=P("%")*(somespace*doctypename*somespace*value)/weirdentity |
| 17667 |
local normalentitytype=(doctypename*somespace*value)/normalentity |
| 17668 |
local publicentitytype=(doctypename*somespace*P("PUBLIC")*somespace*value)/publicentity |
| 17669 |
local systementitytype=(doctypename*somespace*P("SYSTEM")*somespace*value*somespace*P("NDATA")*somespace*doctypename)/systementity |
| 17670 |
local entitydoctype=optionalspace*P("<!ENTITY")*somespace*(systementitytype+publicentitytype+normalentitytype+weirdentitytype)*optionalspace*close |
| 17671 |
local publicentityfile=(doctypename*somespace*P("PUBLIC")*somespace*value*(somespace*value)^0)/function(...) |
| 17672 |
entityfile(entitydoctype,...) |
| 17673 |
end |
| 17674 |
local function weirdresolve(s) |
| 17675 |
lpegmatch(entitydoctype,parameters[s]) |
| 17676 |
end |
| 17677 |
local function normalresolve(s) |
| 17678 |
lpegmatch(entitydoctype,entities[s]) |
| 17679 |
end |
| 17680 |
local entityresolve=P("%")*(wrdtypename/weirdresolve )*P(";")+P("&")*(wrdtypename/normalresolve)*P(";") |
| 17681 |
entitydoctype=entitydoctype+entityresolve |
| 17682 |
local attlistdoctype=optionalspace*P("<!ATTLIST")*somespace*(1-close)^0 |
| 17683 |
*optionalspace*close |
| 17684 |
local doctypeset=beginset*optionalspace*P(elementdoctype+entitydoctype+entityresolve+attlistdoctype+basiccomment+space)^0*optionalspace*endset |
| 17685 |
local definitiondoctype=doctypename*somespace*doctypeset |
| 17686 |
local publicdoctype=doctypename*somespace*P("PUBLIC")*somespace*value*somespace*value*somespace*doctypeset |
| 17687 |
local systemdoctype=doctypename*somespace*P("SYSTEM")*somespace*value*somespace*doctypeset |
| 17688 |
local simpledoctype=(anything-close)^1 |
| 17689 |
local somedoctype=C((somespace*(publicentityfile+publicdoctype+systemdoctype+definitiondoctype+simpledoctype)*optionalspace)^0) |
| 17690 |
local instruction=(spacing*begininstruction*someinstruction*endinstruction)/function(...) add_special("@pi@",...) end |
| 17691 |
local comment=(spacing*begincomment*somecomment*endcomment )/function(...) add_special("@cm@",...) end |
| 17692 |
local cdata=(spacing*begincdata*somecdata*endcdata )/function(...) add_special("@cd@",...) end |
| 17693 |
local doctype=(spacing*begindoctype*somedoctype*enddoctype )/function(...) add_special("@dt@",...) end |
| 17694 |
local crap_parsed=anything-beginelement-endelement-emptyelement-begininstruction-begincomment-begincdata-ampersand |
| 17695 |
local crap_unparsed=anything-beginelement-endelement-emptyelement-begininstruction-begincomment-begincdata |
| 17696 |
local parsedcrap=Cs((crap_parsed^1+entity_text)^1)/handle_crap_error |
| 17697 |
local parsedcrap=Cs((crap_parsed^1+entity_text)^1)/handle_crap_error |
| 17698 |
local unparsedcrap=Cs((crap_unparsed )^1)/handle_crap_error |
| 17699 |
local trailer=space^0*(text_unparsed/set_message)^0 |
| 17700 |
local grammar_parsed_text_one=P { "preamble", |
| 17701 |
preamble=utfbom^0*instruction^0*(doctype+comment+instruction)^0, |
| 17702 |
} |
| 17703 |
local grammar_parsed_text_two=P { "followup", |
| 17704 |
followup=V("parent")*trailer, |
| 17705 |
parent=beginelement*V("children")^0*endelement, |
| 17706 |
children=parsedtext+V("parent")+emptyelement+comment+cdata+instruction+parsedcrap, |
| 17707 |
} |
| 17708 |
local grammar_unparsed_text=P { "preamble", |
| 17709 |
preamble=utfbom^0*instruction^0*(doctype+comment+instruction)^0*V("parent")*trailer, |
| 17710 |
parent=beginelement*V("children")^0*endelement, |
| 17711 |
children=unparsedtext+V("parent")+emptyelement+comment+cdata+instruction+unparsedcrap, |
| 17712 |
} |
| 17713 |
return grammar_parsed_text_one,grammar_parsed_text_two,grammar_unparsed_text |
| 17714 |
end |
| 17715 |
local |
| 17716 |
grammar_parsed_text_one_nop, |
| 17717 |
grammar_parsed_text_two_nop, |
| 17718 |
grammar_unparsed_text_nop=install(space,spacing,anything) |
| 17719 |
local |
| 17720 |
grammar_parsed_text_one_yes, |
| 17721 |
grammar_parsed_text_two_yes, |
| 17722 |
grammar_unparsed_text_yes=install(space_nl,spacing_nl,anything_nl) |
| 17723 |
local function _xmlconvert_(data,settings,detail) |
| 17724 |
settings=settings or {} |
| 17725 |
preparexmlstate(settings) |
| 17726 |
if settings.linenumbers then |
| 17727 |
grammar_parsed_text_one=grammar_parsed_text_one_yes |
| 17728 |
grammar_parsed_text_two=grammar_parsed_text_two_yes |
| 17729 |
grammar_unparsed_text=grammar_unparsed_text_yes |
| 17730 |
else |
| 17731 |
grammar_parsed_text_one=grammar_parsed_text_one_nop |
| 17732 |
grammar_parsed_text_two=grammar_parsed_text_two_nop |
| 17733 |
grammar_unparsed_text=grammar_unparsed_text_nop |
| 17734 |
end |
| 17735 |
local preprocessor=settings.preprocessor |
| 17736 |
if data and data~="" and type(preprocessor)=="function" then |
| 17737 |
data=preprocessor(data,settings) or data |
| 17738 |
end |
| 17739 |
if settings.parent_root then |
| 17740 |
mt=getmetatable(settings.parent_root) |
| 17741 |
else |
| 17742 |
initialize_mt(top) |
| 17743 |
end |
| 17744 |
level=level+1 |
| 17745 |
stack[level]=top |
| 17746 |
top.dt={} |
| 17747 |
dt=top.dt |
| 17748 |
nt=0 |
| 17749 |
if not data or data=="" then |
| 17750 |
errorstr="empty xml file" |
| 17751 |
elseif data==true then |
| 17752 |
errorstr=detail or "problematic xml file" |
| 17753 |
elseif utfize or resolve then |
| 17754 |
local m=lpegmatch(grammar_parsed_text_one,data) |
| 17755 |
if m then |
| 17756 |
m=lpegmatch(grammar_parsed_text_two,data,m) |
| 17757 |
end |
| 17758 |
if m then |
| 17759 |
else |
| 17760 |
errorstr="invalid xml file - parsed text" |
| 17761 |
end |
| 17762 |
elseif type(data)=="string" then |
| 17763 |
if lpegmatch(grammar_unparsed_text,data) then |
| 17764 |
errorstr="" |
| 17765 |
else |
| 17766 |
errorstr="invalid xml file - unparsed text" |
| 17767 |
end |
| 17768 |
else |
| 17769 |
errorstr="invalid xml file - no text at all" |
| 17770 |
end |
| 17771 |
local result |
| 17772 |
if errorstr and errorstr~="" then |
| 17773 |
result={ dt={ { ns="",tg="error",dt={ errorstr },at={},er=true } } } |
| 17774 |
setmetatable(result,mt) |
| 17775 |
setmetatable(result.dt[1],mt) |
| 17776 |
setmetatable(stack,mt) |
| 17777 |
local errorhandler=settings.error_handler |
| 17778 |
if errorhandler==false then |
| 17779 |
else |
| 17780 |
errorhandler=errorhandler or xml.errorhandler |
| 17781 |
if errorhandler then |
| 17782 |
local currentresource=settings.currentresource |
| 17783 |
if currentresource and currentresource~="" then |
| 17784 |
xml.errorhandler(formatters["load error in [%s]: %s"](currentresource,errorstr),currentresource) |
| 17785 |
else |
| 17786 |
xml.errorhandler(formatters["load error: %s"](errorstr)) |
| 17787 |
end |
| 17788 |
end |
| 17789 |
end |
| 17790 |
else |
| 17791 |
result=stack[1] |
| 17792 |
end |
| 17793 |
if not settings.no_root then |
| 17794 |
result={ special=true,ns="",tg='@rt@',dt=result.dt,at={},entities=entities,settings=settings } |
| 17795 |
setmetatable(result,mt) |
| 17796 |
local rdt=result.dt |
| 17797 |
for k=1,#rdt do |
| 17798 |
local v=rdt[k] |
| 17799 |
if type(v)=="table" and not v.special then |
| 17800 |
result.ri=k |
| 17801 |
v.__p__=result |
| 17802 |
break |
| 17803 |
end |
| 17804 |
end |
| 17805 |
end |
| 17806 |
if errorstr and errorstr~="" then |
| 17807 |
result.error=true |
| 17808 |
else |
| 17809 |
errorstr=nil |
| 17810 |
end |
| 17811 |
result.statistics={ |
| 17812 |
errormessage=errorstr, |
| 17813 |
entities={ |
| 17814 |
decimals=dcache, |
| 17815 |
hexadecimals=hcache, |
| 17816 |
names=acache, |
| 17817 |
intermediates=parameters, |
| 17818 |
} |
| 17819 |
} |
| 17820 |
preparexmlstate() |
| 17821 |
return result |
| 17822 |
end |
| 17823 |
local function xmlconvert(data,settings) |
| 17824 |
local ok,result=pcall(function() return _xmlconvert_(data,settings) end) |
| 17825 |
if ok then |
| 17826 |
return result |
| 17827 |
elseif type(result)=="string" then |
| 17828 |
return _xmlconvert_(true,settings,result) |
| 17829 |
else |
| 17830 |
return _xmlconvert_(true,settings) |
| 17831 |
end |
| 17832 |
end |
| 17833 |
xml.convert=xmlconvert |
| 17834 |
function xml.inheritedconvert(data,xmldata,cleanup) |
| 17835 |
local settings=xmldata.settings |
| 17836 |
if settings then |
| 17837 |
settings.parent_root=xmldata |
| 17838 |
end |
| 17839 |
local xc=xmlconvert(data,settings) |
| 17840 |
if cleanup then |
| 17841 |
local x=xc.dt |
| 17842 |
if x then |
| 17843 |
x=x[1] |
| 17844 |
if x and x.tg=="@pi@" then |
| 17845 |
local dt=x.dt |
| 17846 |
local pi=dt and dt[1] |
| 17847 |
if type(pi)=="string" and find(pi,"^xml") then |
| 17848 |
remove(dt,1) |
| 17849 |
end |
| 17850 |
end |
| 17851 |
end |
| 17852 |
end |
| 17853 |
return xc |
| 17854 |
end |
| 17855 |
function xml.is_valid(root) |
| 17856 |
return root and root.dt and root.dt[1] and type(root.dt[1])=="table" and not root.dt[1].er |
| 17857 |
end |
| 17858 |
function xml.package(tag,attributes,data) |
| 17859 |
local ns,tg=match(tag,"^(.-):?([^:]+)$") |
| 17860 |
local t={ ns=ns,tg=tg,dt=data or "",at=attributes or {} } |
| 17861 |
setmetatable(t,mt) |
| 17862 |
return t |
| 17863 |
end |
| 17864 |
function xml.is_valid(root) |
| 17865 |
return root and not root.error |
| 17866 |
end |
| 17867 |
xml.errorhandler=report_xml |
| 17868 |
function xml.load(filename,settings) |
| 17869 |
local data="" |
| 17870 |
if type(filename)=="string" then |
| 17871 |
local f=io.open(filename,'r') |
| 17872 |
if f then |
| 17873 |
data=f:read("*all") |
| 17874 |
f:close() |
| 17875 |
end |
| 17876 |
elseif filename then |
| 17877 |
data=filename:read("*all") |
| 17878 |
end |
| 17879 |
if settings then |
| 17880 |
settings.currentresource=filename |
| 17881 |
local result=xmlconvert(data,settings) |
| 17882 |
settings.currentresource=nil |
| 17883 |
return result |
| 17884 |
else |
| 17885 |
return xmlconvert(data,{ currentresource=filename }) |
| 17886 |
end |
| 17887 |
end |
| 17888 |
local no_root={ no_root=true } |
| 17889 |
function xml.toxml(data) |
| 17890 |
if type(data)=="string" then |
| 17891 |
local root={ xmlconvert(data,no_root) } |
| 17892 |
return (#root>1 and root) or root[1] |
| 17893 |
else |
| 17894 |
return data |
| 17895 |
end |
| 17896 |
end |
| 17897 |
local function copy(old,p) |
| 17898 |
if old then |
| 17899 |
local new={} |
| 17900 |
for k,v in next,old do |
| 17901 |
local t=type(v)=="table" |
| 17902 |
if k=="at" then |
| 17903 |
local t={} |
| 17904 |
for k,v in next,v do |
| 17905 |
t[k]=v |
| 17906 |
end |
| 17907 |
new[k]=t |
| 17908 |
elseif k=="dt" then |
| 17909 |
v.__p__=nil |
| 17910 |
local t={} |
| 17911 |
for i=1,#v do |
| 17912 |
local vi=v[i] |
| 17913 |
if type(vi)=="table" then |
| 17914 |
t[i]=copy(vi,new) |
| 17915 |
else |
| 17916 |
t[i]=vi |
| 17917 |
end |
| 17918 |
end |
| 17919 |
new[k]=t |
| 17920 |
t.__p__=p |
| 17921 |
else |
| 17922 |
new[k]=v |
| 17923 |
end |
| 17924 |
end |
| 17925 |
local mt=getmetatable(old) |
| 17926 |
if mt then |
| 17927 |
setmetatable(new,mt) |
| 17928 |
end |
| 17929 |
return new |
| 17930 |
else |
| 17931 |
return {} |
| 17932 |
end |
| 17933 |
end |
| 17934 |
xml.copy=copy |
| 17935 |
function xml.checkbom(root) |
| 17936 |
if root.ri then |
| 17937 |
local dt=root.dt |
| 17938 |
for k=1,#dt do |
| 17939 |
local v=dt[k] |
| 17940 |
if type(v)=="table" and v.special and v.tg=="@pi@" and find(v.dt[1],"xml.*version=") then |
| 17941 |
return |
| 17942 |
end |
| 17943 |
end |
| 17944 |
insert(dt,1,{ special=true,ns="",tg="@pi@",dt={ "xml version='1.0' standalone='yes'" } } ) |
| 17945 |
insert(dt,2,"\n" ) |
| 17946 |
end |
| 17947 |
end |
| 17948 |
local f_attribute=formatters['%s=%q'] |
| 17949 |
local function verbose_element(e,handlers,escape) |
| 17950 |
local handle=handlers.handle |
| 17951 |
local serialize=handlers.serialize |
| 17952 |
local ens=e.ns |
| 17953 |
local etg=e.tg |
| 17954 |
local eat=e.at |
| 17955 |
local edt=e.dt |
| 17956 |
local ern=e.rn |
| 17957 |
local ats=eat and next(eat) and {} |
| 17958 |
if ats then |
| 17959 |
local n=0 |
| 17960 |
for k in next,eat do |
| 17961 |
n=n+1 |
| 17962 |
ats[n]=k |
| 17963 |
end |
| 17964 |
if n==1 then |
| 17965 |
local k=ats[1] |
| 17966 |
ats=f_attribute(k,escaped(eat[k])) |
| 17967 |
else |
| 17968 |
sort(ats) |
| 17969 |
for i=1,n do |
| 17970 |
local k=ats[i] |
| 17971 |
ats[i]=f_attribute(k,escaped(eat[k])) |
| 17972 |
end |
| 17973 |
ats=concat(ats," ") |
| 17974 |
end |
| 17975 |
end |
| 17976 |
if ern and trace_entities and ern~=ens then |
| 17977 |
ens=ern |
| 17978 |
end |
| 17979 |
local n=edt and #edt |
| 17980 |
if ens~="" then |
| 17981 |
if n and n>0 then |
| 17982 |
if ats then |
| 17983 |
handle("<",ens,":",etg," ",ats,">") |
| 17984 |
else |
| 17985 |
handle("<",ens,":",etg,">") |
| 17986 |
end |
| 17987 |
for i=1,n do |
| 17988 |
local e=edt[i] |
| 17989 |
if type(e)=="string" then |
| 17990 |
handle(escaped(e)) |
| 17991 |
else |
| 17992 |
serialize(e,handlers) |
| 17993 |
end |
| 17994 |
end |
| 17995 |
handle("</",ens,":",etg,">") |
| 17996 |
else |
| 17997 |
if ats then |
| 17998 |
handle("<",ens,":",etg," ",ats,"/>") |
| 17999 |
else |
| 18000 |
handle("<",ens,":",etg,"/>") |
| 18001 |
end |
| 18002 |
end |
| 18003 |
else |
| 18004 |
if n and n>0 then |
| 18005 |
if ats then |
| 18006 |
handle("<",etg," ",ats,">") |
| 18007 |
else |
| 18008 |
handle("<",etg,">") |
| 18009 |
end |
| 18010 |
for i=1,n do |
| 18011 |
local e=edt[i] |
| 18012 |
if type(e)=="string" then |
| 18013 |
handle(escaped(e)) |
| 18014 |
else |
| 18015 |
serialize(e,handlers) |
| 18016 |
end |
| 18017 |
end |
| 18018 |
handle("</",etg,">") |
| 18019 |
else |
| 18020 |
if ats then |
| 18021 |
handle("<",etg," ",ats,"/>") |
| 18022 |
else |
| 18023 |
handle("<",etg,"/>") |
| 18024 |
end |
| 18025 |
end |
| 18026 |
end |
| 18027 |
end |
| 18028 |
local function verbose_pi(e,handlers) |
| 18029 |
handlers.handle("<?",e.dt[1],"?>") |
| 18030 |
end |
| 18031 |
local function verbose_comment(e,handlers) |
| 18032 |
handlers.handle("<!--",e.dt[1],"-->") |
| 18033 |
end |
| 18034 |
local function verbose_cdata(e,handlers) |
| 18035 |
handlers.handle("<![CDATA[",e.dt[1],"]]>") |
| 18036 |
end |
| 18037 |
local function verbose_doctype(e,handlers) |
| 18038 |
handlers.handle("<!DOCTYPE",e.dt[1],">") |
| 18039 |
end |
| 18040 |
local function verbose_root(e,handlers) |
| 18041 |
handlers.serialize(e.dt,handlers) |
| 18042 |
end |
| 18043 |
local function verbose_text(e,handlers) |
| 18044 |
handlers.handle(escaped(e)) |
| 18045 |
end |
| 18046 |
local function verbose_document(e,handlers) |
| 18047 |
local serialize=handlers.serialize |
| 18048 |
local functions=handlers.functions |
| 18049 |
for i=1,#e do |
| 18050 |
local ei=e[i] |
| 18051 |
if type(ei)=="string" then |
| 18052 |
functions["@tx@"](ei,handlers) |
| 18053 |
else |
| 18054 |
serialize(ei,handlers) |
| 18055 |
end |
| 18056 |
end |
| 18057 |
end |
| 18058 |
local function serialize(e,handlers,...) |
| 18059 |
if e then |
| 18060 |
local initialize=handlers.initialize |
| 18061 |
local finalize=handlers.finalize |
| 18062 |
local functions=handlers.functions |
| 18063 |
if initialize then |
| 18064 |
local state=initialize(...) |
| 18065 |
if not state==true then |
| 18066 |
return state |
| 18067 |
end |
| 18068 |
end |
| 18069 |
local etg=e.tg |
| 18070 |
if etg then |
| 18071 |
(functions[etg] or functions["@el@"])(e,handlers) |
| 18072 |
else |
| 18073 |
functions["@dc@"](e,handlers) |
| 18074 |
end |
| 18075 |
if finalize then |
| 18076 |
return finalize() |
| 18077 |
end |
| 18078 |
end |
| 18079 |
end |
| 18080 |
local function xserialize(e,handlers) |
| 18081 |
if e then |
| 18082 |
local functions=handlers.functions |
| 18083 |
local etg=e.tg |
| 18084 |
if etg then |
| 18085 |
(functions[etg] or functions["@el@"])(e,handlers) |
| 18086 |
else |
| 18087 |
functions["@dc@"](e,handlers) |
| 18088 |
end |
| 18089 |
end |
| 18090 |
end |
| 18091 |
local handlers={} |
| 18092 |
local function newhandlers(settings) |
| 18093 |
local t=table.copy(handlers[settings and settings.parent or "verbose"] or {}) |
| 18094 |
if settings then |
| 18095 |
for k,v in next,settings do |
| 18096 |
if type(v)=="table" then |
| 18097 |
local tk=t[k] if not tk then tk={} t[k]=tk end |
| 18098 |
for kk,vv in next,v do |
| 18099 |
tk[kk]=vv |
| 18100 |
end |
| 18101 |
else |
| 18102 |
t[k]=v |
| 18103 |
end |
| 18104 |
end |
| 18105 |
if settings.name then |
| 18106 |
handlers[settings.name]=t |
| 18107 |
end |
| 18108 |
end |
| 18109 |
utilities.storage.mark(t) |
| 18110 |
return t |
| 18111 |
end |
| 18112 |
local nofunction=function() end |
| 18113 |
function xml.sethandlersfunction(handler,name,fnc) |
| 18114 |
handler.functions[name]=fnc or nofunction |
| 18115 |
end |
| 18116 |
function xml.gethandlersfunction(handler,name) |
| 18117 |
return handler.functions[name] |
| 18118 |
end |
| 18119 |
function xml.gethandlers(name) |
| 18120 |
return handlers[name] |
| 18121 |
end |
| 18122 |
newhandlers { |
| 18123 |
name="verbose", |
| 18124 |
initialize=false, |
| 18125 |
finalize=false, |
| 18126 |
serialize=xserialize, |
| 18127 |
handle=print, |
| 18128 |
functions={ |
| 18129 |
["@dc@"]=verbose_document, |
| 18130 |
["@dt@"]=verbose_doctype, |
| 18131 |
["@rt@"]=verbose_root, |
| 18132 |
["@el@"]=verbose_element, |
| 18133 |
["@pi@"]=verbose_pi, |
| 18134 |
["@cm@"]=verbose_comment, |
| 18135 |
["@cd@"]=verbose_cdata, |
| 18136 |
["@tx@"]=verbose_text, |
| 18137 |
} |
| 18138 |
} |
| 18139 |
local result |
| 18140 |
local xmlfilehandler=newhandlers { |
| 18141 |
name="file", |
| 18142 |
initialize=function(name) |
| 18143 |
result=io.open(name,"wb") |
| 18144 |
return result |
| 18145 |
end, |
| 18146 |
finalize=function() |
| 18147 |
result:close() |
| 18148 |
return true |
| 18149 |
end, |
| 18150 |
handle=function(...) |
| 18151 |
result:write(...) |
| 18152 |
end, |
| 18153 |
} |
| 18154 |
function xml.save(root,name) |
| 18155 |
serialize(root,xmlfilehandler,name) |
| 18156 |
end |
| 18157 |
local result,r,threshold={},0,512 |
| 18158 |
local xmlstringhandler=newhandlers { |
| 18159 |
name="string", |
| 18160 |
initialize=function() |
| 18161 |
r=0 |
| 18162 |
return result |
| 18163 |
end, |
| 18164 |
finalize=function() |
| 18165 |
local done=concat(result,"",1,r) |
| 18166 |
r=0 |
| 18167 |
if r>threshold then |
| 18168 |
result={} |
| 18169 |
end |
| 18170 |
return done |
| 18171 |
end, |
| 18172 |
handle=function(...) |
| 18173 |
for i=1,select("#",...) do |
| 18174 |
r=r+1 |
| 18175 |
result[r]=select(i,...) |
| 18176 |
end |
| 18177 |
end, |
| 18178 |
} |
| 18179 |
local function xmltostring(root) |
| 18180 |
if not root then |
| 18181 |
return "" |
| 18182 |
elseif type(root)=="string" then |
| 18183 |
return root |
| 18184 |
else |
| 18185 |
return serialize(root,xmlstringhandler) or "" |
| 18186 |
end |
| 18187 |
end |
| 18188 |
local function __tostring(root) |
| 18189 |
return (root and xmltostring(root)) or "" |
| 18190 |
end |
| 18191 |
initialize_mt=function(root) |
| 18192 |
mt={ __tostring=__tostring,__index=root } |
| 18193 |
end |
| 18194 |
xml.defaulthandlers=handlers |
| 18195 |
xml.newhandlers=newhandlers |
| 18196 |
xml.serialize=serialize |
| 18197 |
xml.tostring=xmltostring |
| 18198 |
local function xmlstring(e,handle) |
| 18199 |
if not handle or (e.special and e.tg~="@rt@") then |
| 18200 |
elseif e.tg then |
| 18201 |
local edt=e.dt |
| 18202 |
if edt then |
| 18203 |
for i=1,#edt do |
| 18204 |
xmlstring(edt[i],handle) |
| 18205 |
end |
| 18206 |
end |
| 18207 |
else |
| 18208 |
handle(e) |
| 18209 |
end |
| 18210 |
end |
| 18211 |
xml.string=xmlstring |
| 18212 |
function xml.settings(e) |
| 18213 |
while e do |
| 18214 |
local s=e.settings |
| 18215 |
if s then |
| 18216 |
return s |
| 18217 |
else |
| 18218 |
e=e.__p__ |
| 18219 |
end |
| 18220 |
end |
| 18221 |
return nil |
| 18222 |
end |
| 18223 |
function xml.root(e) |
| 18224 |
local r=e |
| 18225 |
while e do |
| 18226 |
e=e.__p__ |
| 18227 |
if e then |
| 18228 |
r=e |
| 18229 |
end |
| 18230 |
end |
| 18231 |
return r |
| 18232 |
end |
| 18233 |
function xml.parent(root) |
| 18234 |
return root.__p__ |
| 18235 |
end |
| 18236 |
function xml.body(root) |
| 18237 |
return root.ri and root.dt[root.ri] or root |
| 18238 |
end |
| 18239 |
function xml.name(root) |
| 18240 |
if not root then |
| 18241 |
return "" |
| 18242 |
end |
| 18243 |
local ns=root.ns |
| 18244 |
local tg=root.tg |
| 18245 |
if ns=="" then |
| 18246 |
return tg |
| 18247 |
else |
| 18248 |
return ns..":"..tg |
| 18249 |
end |
| 18250 |
end |
| 18251 |
function xml.erase(dt,k) |
| 18252 |
if dt then |
| 18253 |
if k then |
| 18254 |
dt[k]="" |
| 18255 |
else for k=1,#dt do |
| 18256 |
dt[1]={ "" } |
| 18257 |
end end |
| 18258 |
end |
| 18259 |
end |
| 18260 |
function xml.assign(dt,k,root) |
| 18261 |
if dt and k then |
| 18262 |
dt[k]=type(root)=="table" and xml.body(root) or root |
| 18263 |
return dt[k] |
| 18264 |
else |
| 18265 |
return xml.body(root) |
| 18266 |
end |
| 18267 |
end |
| 18268 |
function xml.tocdata(e,wrapper) |
| 18269 |
local whatever=type(e)=="table" and xmltostring(e.dt) or e or "" |
| 18270 |
if wrapper then |
| 18271 |
whatever=formatters["<%s>%s</%s>"](wrapper,whatever,wrapper) |
| 18272 |
end |
| 18273 |
local t={ special=true,ns="",tg="@cd@",at={},rn="",dt={ whatever },__p__=e } |
| 18274 |
setmetatable(t,getmetatable(e)) |
| 18275 |
e.dt={ t } |
| 18276 |
end |
| 18277 |
function xml.makestandalone(root) |
| 18278 |
if root.ri then |
| 18279 |
local dt=root.dt |
| 18280 |
for k=1,#dt do |
| 18281 |
local v=dt[k] |
| 18282 |
if type(v)=="table" and v.special and v.tg=="@pi@" then |
| 18283 |
local txt=v.dt[1] |
| 18284 |
if find(txt,"xml.*version=") then |
| 18285 |
v.dt[1]=txt.." standalone='yes'" |
| 18286 |
break |
| 18287 |
end |
| 18288 |
end |
| 18289 |
end |
| 18290 |
end |
| 18291 |
return root |
| 18292 |
end |
| 18293 |
function xml.kind(e) |
| 18294 |
local dt=e and e.dt |
| 18295 |
if dt then |
| 18296 |
local n=#dt |
| 18297 |
if n==1 then |
| 18298 |
local d=dt[1] |
| 18299 |
if d.special then |
| 18300 |
local tg=d.tg |
| 18301 |
if tg=="@cd@" then |
| 18302 |
return "cdata" |
| 18303 |
elseif tg=="@cm@" then |
| 18304 |
return "comment" |
| 18305 |
elseif tg=="@pi@" then |
| 18306 |
return "instruction" |
| 18307 |
elseif tg=="@dt@" then |
| 18308 |
return "declaration" |
| 18309 |
end |
| 18310 |
elseif type(d)=="string" then |
| 18311 |
return "text" |
| 18312 |
end |
| 18313 |
return "element" |
| 18314 |
elseif n>0 then |
| 18315 |
return "mixed" |
| 18316 |
end |
| 18317 |
end |
| 18318 |
return "empty" |
| 18319 |
end |
| 18320 |
|
| 18321 |
|
| 18322 |
end -- of closure |
| 18323 |
|
| 18324 |
do -- create closure to overcome 200 locals limit |
| 18325 |
|
| 18326 |
package.loaded["lxml-lpt"] = package.loaded["lxml-lpt"] or true |
| 18327 |
|
| 18328 |
-- original size: 54738, stripped down to: 31395 |
| 18329 |
|
| 18330 |
if not modules then modules={} end modules ['lxml-lpt']={ |
| 18331 |
version=1.001, |
| 18332 |
comment="this module is the basis for the lxml-* ones", |
| 18333 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 18334 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 18335 |
license="see context related readme files" |
| 18336 |
} |
| 18337 |
local concat,remove,insert=table.concat,table.remove,table.insert |
| 18338 |
local type,next,tonumber,tostring,setmetatable,load,select=type,next,tonumber,tostring,setmetatable,load,select |
| 18339 |
local format,upper,lower,gmatch,gsub,find,rep=string.format,string.upper,string.lower,string.gmatch,string.gsub,string.find,string.rep |
| 18340 |
local lpegmatch,lpegpatterns=lpeg.match,lpeg.patterns |
| 18341 |
local setmetatableindex=table.setmetatableindex |
| 18342 |
local formatters=string.formatters |
| 18343 |
local trace_lpath=false |
| 18344 |
local trace_lparse=false |
| 18345 |
local trace_lprofile=false |
| 18346 |
local report_lpath=logs.reporter("xml","lpath") |
| 18347 |
if trackers then |
| 18348 |
trackers.register("xml.path",function(v) |
| 18349 |
trace_lpath=v |
| 18350 |
end) |
| 18351 |
trackers.register("xml.parse",function(v) |
| 18352 |
trace_lparse=v |
| 18353 |
end) |
| 18354 |
trackers.register("xml.profile",function(v) |
| 18355 |
trace_lpath=v |
| 18356 |
trace_lparse=v |
| 18357 |
trace_lprofile=v |
| 18358 |
end) |
| 18359 |
end |
| 18360 |
local xml=xml |
| 18361 |
local lpathcalls=0 function xml.lpathcalls () return lpathcalls end |
| 18362 |
local lpathcached=0 function xml.lpathcached() return lpathcached end |
| 18363 |
xml.functions=xml.functions or {} |
| 18364 |
local functions=xml.functions |
| 18365 |
xml.expressions=xml.expressions or {} |
| 18366 |
local expressions=xml.expressions |
| 18367 |
xml.finalizers=xml.finalizers or {} |
| 18368 |
local finalizers=xml.finalizers |
| 18369 |
xml.specialhandler=xml.specialhandler or {} |
| 18370 |
local specialhandler=xml.specialhandler |
| 18371 |
lpegpatterns.xml=lpegpatterns.xml or {} |
| 18372 |
local xmlpatterns=lpegpatterns.xml |
| 18373 |
finalizers.xml=finalizers.xml or {} |
| 18374 |
finalizers.tex=finalizers.tex or {} |
| 18375 |
local function fallback (t,name) |
| 18376 |
local fn=finalizers[name] |
| 18377 |
if fn then |
| 18378 |
t[name]=fn |
| 18379 |
else |
| 18380 |
report_lpath("unknown sub finalizer %a",name) |
| 18381 |
fn=function() end |
| 18382 |
end |
| 18383 |
return fn |
| 18384 |
end |
| 18385 |
setmetatableindex(finalizers.xml,fallback) |
| 18386 |
setmetatableindex(finalizers.tex,fallback) |
| 18387 |
xml.defaultprotocol="xml" |
| 18388 |
local apply_axis={} |
| 18389 |
apply_axis['root']=function(list) |
| 18390 |
local collected={} |
| 18391 |
for l=1,#list do |
| 18392 |
local ll=list[l] |
| 18393 |
local rt=ll |
| 18394 |
while ll do |
| 18395 |
ll=ll.__p__ |
| 18396 |
if ll then |
| 18397 |
rt=ll |
| 18398 |
end |
| 18399 |
end |
| 18400 |
collected[l]=rt |
| 18401 |
end |
| 18402 |
return collected |
| 18403 |
end |
| 18404 |
apply_axis['self']=function(list) |
| 18405 |
return list |
| 18406 |
end |
| 18407 |
apply_axis['child']=function(list) |
| 18408 |
local collected={} |
| 18409 |
local c=0 |
| 18410 |
for l=1,#list do |
| 18411 |
local ll=list[l] |
| 18412 |
local dt=ll.dt |
| 18413 |
if dt then |
| 18414 |
local n=#dt |
| 18415 |
if n==0 then |
| 18416 |
ll.en=0 |
| 18417 |
elseif n==1 then |
| 18418 |
local dk=dt[1] |
| 18419 |
if dk.tg then |
| 18420 |
c=c+1 |
| 18421 |
collected[c]=dk |
| 18422 |
dk.ni=1 |
| 18423 |
dk.ei=1 |
| 18424 |
ll.en=1 |
| 18425 |
end |
| 18426 |
else |
| 18427 |
local en=0 |
| 18428 |
for k=1,#dt do |
| 18429 |
local dk=dt[k] |
| 18430 |
if dk.tg then |
| 18431 |
c=c+1 |
| 18432 |
en=en+1 |
| 18433 |
collected[c]=dk |
| 18434 |
dk.ni=k |
| 18435 |
dk.ei=en |
| 18436 |
end |
| 18437 |
end |
| 18438 |
ll.en=en |
| 18439 |
end |
| 18440 |
end |
| 18441 |
end |
| 18442 |
return collected |
| 18443 |
end |
| 18444 |
local function collect(list,collected,c) |
| 18445 |
local dt=list.dt |
| 18446 |
if dt then |
| 18447 |
local n=#dt |
| 18448 |
if n==0 then |
| 18449 |
list.en=0 |
| 18450 |
elseif n==1 then |
| 18451 |
local dk=dt[1] |
| 18452 |
if dk.tg then |
| 18453 |
c=c+1 |
| 18454 |
collected[c]=dk |
| 18455 |
dk.ni=1 |
| 18456 |
dk.ei=1 |
| 18457 |
c=collect(dk,collected,c) |
| 18458 |
list.en=1 |
| 18459 |
else |
| 18460 |
list.en=0 |
| 18461 |
end |
| 18462 |
else |
| 18463 |
local en=0 |
| 18464 |
for k=1,n do |
| 18465 |
local dk=dt[k] |
| 18466 |
if dk.tg then |
| 18467 |
c=c+1 |
| 18468 |
en=en+1 |
| 18469 |
collected[c]=dk |
| 18470 |
dk.ni=k |
| 18471 |
dk.ei=en |
| 18472 |
c=collect(dk,collected,c) |
| 18473 |
end |
| 18474 |
end |
| 18475 |
list.en=en |
| 18476 |
end |
| 18477 |
end |
| 18478 |
return c |
| 18479 |
end |
| 18480 |
apply_axis['descendant']=function(list) |
| 18481 |
local collected={} |
| 18482 |
local c=0 |
| 18483 |
for l=1,#list do |
| 18484 |
c=collect(list[l],collected,c) |
| 18485 |
end |
| 18486 |
return collected |
| 18487 |
end |
| 18488 |
local function collect(list,collected,c) |
| 18489 |
local dt=list.dt |
| 18490 |
if dt then |
| 18491 |
local n=#dt |
| 18492 |
if n==0 then |
| 18493 |
list.en=0 |
| 18494 |
elseif n==1 then |
| 18495 |
local dk=dt[1] |
| 18496 |
if dk.tg then |
| 18497 |
c=c+1 |
| 18498 |
collected[c]=dk |
| 18499 |
dk.ni=1 |
| 18500 |
dk.ei=1 |
| 18501 |
c=collect(dk,collected,c) |
| 18502 |
list.en=1 |
| 18503 |
end |
| 18504 |
else |
| 18505 |
local en=0 |
| 18506 |
for k=1,#dt do |
| 18507 |
local dk=dt[k] |
| 18508 |
if dk.tg then |
| 18509 |
c=c+1 |
| 18510 |
en=en+1 |
| 18511 |
collected[c]=dk |
| 18512 |
dk.ni=k |
| 18513 |
dk.ei=en |
| 18514 |
c=collect(dk,collected,c) |
| 18515 |
end |
| 18516 |
end |
| 18517 |
list.en=en |
| 18518 |
end |
| 18519 |
end |
| 18520 |
return c |
| 18521 |
end |
| 18522 |
apply_axis['descendant-or-self']=function(list) |
| 18523 |
local collected={} |
| 18524 |
local c=0 |
| 18525 |
for l=1,#list do |
| 18526 |
local ll=list[l] |
| 18527 |
if ll.special~=true then |
| 18528 |
c=c+1 |
| 18529 |
collected[c]=ll |
| 18530 |
end |
| 18531 |
c=collect(ll,collected,c) |
| 18532 |
end |
| 18533 |
return collected |
| 18534 |
end |
| 18535 |
apply_axis['ancestor']=function(list) |
| 18536 |
local collected={} |
| 18537 |
local c=0 |
| 18538 |
for l=1,#list do |
| 18539 |
local ll=list[l] |
| 18540 |
while ll do |
| 18541 |
ll=ll.__p__ |
| 18542 |
if ll then |
| 18543 |
c=c+1 |
| 18544 |
collected[c]=ll |
| 18545 |
end |
| 18546 |
end |
| 18547 |
end |
| 18548 |
return collected |
| 18549 |
end |
| 18550 |
apply_axis['ancestor-or-self']=function(list) |
| 18551 |
local collected={} |
| 18552 |
local c=0 |
| 18553 |
for l=1,#list do |
| 18554 |
local ll=list[l] |
| 18555 |
c=c+1 |
| 18556 |
collected[c]=ll |
| 18557 |
while ll do |
| 18558 |
ll=ll.__p__ |
| 18559 |
if ll then |
| 18560 |
c=c+1 |
| 18561 |
collected[c]=ll |
| 18562 |
end |
| 18563 |
end |
| 18564 |
end |
| 18565 |
return collected |
| 18566 |
end |
| 18567 |
apply_axis['parent']=function(list) |
| 18568 |
local collected={} |
| 18569 |
local c=0 |
| 18570 |
for l=1,#list do |
| 18571 |
local pl=list[l].__p__ |
| 18572 |
if pl then |
| 18573 |
c=c+1 |
| 18574 |
collected[c]=pl |
| 18575 |
end |
| 18576 |
end |
| 18577 |
return collected |
| 18578 |
end |
| 18579 |
apply_axis['attribute']=function(list) |
| 18580 |
return {} |
| 18581 |
end |
| 18582 |
apply_axis['namespace']=function(list) |
| 18583 |
return {} |
| 18584 |
end |
| 18585 |
apply_axis['following']=function(list) |
| 18586 |
return {} |
| 18587 |
end |
| 18588 |
apply_axis['preceding']=function(list) |
| 18589 |
return {} |
| 18590 |
end |
| 18591 |
apply_axis['following-sibling']=function(list) |
| 18592 |
local collected={} |
| 18593 |
local c=0 |
| 18594 |
for l=1,#list do |
| 18595 |
local ll=list[l] |
| 18596 |
local p=ll.__p__ |
| 18597 |
local d=p.dt |
| 18598 |
for i=ll.ni+1,#d do |
| 18599 |
local di=d[i] |
| 18600 |
if type(di)=="table" then |
| 18601 |
c=c+1 |
| 18602 |
collected[c]=di |
| 18603 |
end |
| 18604 |
end |
| 18605 |
end |
| 18606 |
return collected |
| 18607 |
end |
| 18608 |
apply_axis['preceding-sibling']=function(list) |
| 18609 |
local collected={} |
| 18610 |
local c=0 |
| 18611 |
for l=1,#list do |
| 18612 |
local ll=list[l] |
| 18613 |
local p=ll.__p__ |
| 18614 |
local d=p.dt |
| 18615 |
for i=1,ll.ni-1 do |
| 18616 |
local di=d[i] |
| 18617 |
if type(di)=="table" then |
| 18618 |
c=c+1 |
| 18619 |
collected[c]=di |
| 18620 |
end |
| 18621 |
end |
| 18622 |
end |
| 18623 |
return collected |
| 18624 |
end |
| 18625 |
apply_axis['reverse-sibling']=function(list) |
| 18626 |
local collected={} |
| 18627 |
local c=0 |
| 18628 |
for l=1,#list do |
| 18629 |
local ll=list[l] |
| 18630 |
local p=ll.__p__ |
| 18631 |
local d=p.dt |
| 18632 |
for i=ll.ni-1,1,-1 do |
| 18633 |
local di=d[i] |
| 18634 |
if type(di)=="table" then |
| 18635 |
c=c+1 |
| 18636 |
collected[c]=di |
| 18637 |
end |
| 18638 |
end |
| 18639 |
end |
| 18640 |
return collected |
| 18641 |
end |
| 18642 |
apply_axis['auto-descendant-or-self']=apply_axis['descendant-or-self'] |
| 18643 |
apply_axis['auto-descendant']=apply_axis['descendant'] |
| 18644 |
apply_axis['auto-child']=apply_axis['child'] |
| 18645 |
apply_axis['auto-self']=apply_axis['self'] |
| 18646 |
apply_axis['initial-child']=apply_axis['child'] |
| 18647 |
local function apply_nodes(list,directive,nodes) |
| 18648 |
local maxn=#nodes |
| 18649 |
if maxn==3 then |
| 18650 |
local nns=nodes[2] |
| 18651 |
local ntg=nodes[3] |
| 18652 |
if not nns and not ntg then |
| 18653 |
if directive then |
| 18654 |
return list |
| 18655 |
else |
| 18656 |
return {} |
| 18657 |
end |
| 18658 |
else |
| 18659 |
local collected={} |
| 18660 |
local c=0 |
| 18661 |
local m=0 |
| 18662 |
local p=nil |
| 18663 |
if not nns then |
| 18664 |
for l=1,#list do |
| 18665 |
local ll=list[l] |
| 18666 |
local ltg=ll.tg |
| 18667 |
if ltg then |
| 18668 |
if directive then |
| 18669 |
if ntg==ltg then |
| 18670 |
local llp=ll.__p__;if llp~=p then p=llp;m=1 else m=m+1 end |
| 18671 |
c=c+1 |
| 18672 |
collected[c]=ll |
| 18673 |
ll.mi=m |
| 18674 |
end |
| 18675 |
elseif ntg~=ltg then |
| 18676 |
local llp=ll.__p__;if llp~=p then p=llp;m=1 else m=m+1 end |
| 18677 |
c=c+1 |
| 18678 |
collected[c]=ll |
| 18679 |
ll.mi=m |
| 18680 |
end |
| 18681 |
end |
| 18682 |
end |
| 18683 |
elseif not ntg then |
| 18684 |
for l=1,#list do |
| 18685 |
local ll=list[l] |
| 18686 |
local lns=ll.rn or ll.ns |
| 18687 |
if lns then |
| 18688 |
if directive then |
| 18689 |
if lns==nns then |
| 18690 |
local llp=ll.__p__;if llp~=p then p=llp;m=1 else m=m+1 end |
| 18691 |
c=c+1 |
| 18692 |
collected[c]=ll |
| 18693 |
ll.mi=m |
| 18694 |
end |
| 18695 |
elseif lns~=nns then |
| 18696 |
local llp=ll.__p__;if llp~=p then p=llp;m=1 else m=m+1 end |
| 18697 |
c=c+1 |
| 18698 |
collected[c]=ll |
| 18699 |
ll.mi=m |
| 18700 |
end |
| 18701 |
end |
| 18702 |
end |
| 18703 |
else |
| 18704 |
for l=1,#list do |
| 18705 |
local ll=list[l] |
| 18706 |
local ltg=ll.tg |
| 18707 |
if ltg then |
| 18708 |
local lns=ll.rn or ll.ns |
| 18709 |
local ok=ltg==ntg and lns==nns |
| 18710 |
if directive then |
| 18711 |
if ok then |
| 18712 |
local llp=ll.__p__;if llp~=p then p=llp;m=1 else m=m+1 end |
| 18713 |
c=c+1 |
| 18714 |
collected[c]=ll |
| 18715 |
ll.mi=m |
| 18716 |
end |
| 18717 |
elseif not ok then |
| 18718 |
local llp=ll.__p__;if llp~=p then p=llp;m=1 else m=m+1 end |
| 18719 |
c=c+1 |
| 18720 |
collected[c]=ll |
| 18721 |
ll.mi=m |
| 18722 |
end |
| 18723 |
end |
| 18724 |
end |
| 18725 |
end |
| 18726 |
return collected |
| 18727 |
end |
| 18728 |
else |
| 18729 |
local collected={} |
| 18730 |
local c=0 |
| 18731 |
local m=0 |
| 18732 |
local p=nil |
| 18733 |
for l=1,#list do |
| 18734 |
local ll=list[l] |
| 18735 |
local ltg=ll.tg |
| 18736 |
if ltg then |
| 18737 |
local lns=ll.rn or ll.ns |
| 18738 |
local ok=false |
| 18739 |
for n=1,maxn,3 do |
| 18740 |
local nns=nodes[n+1] |
| 18741 |
local ntg=nodes[n+2] |
| 18742 |
ok=(not ntg or ltg==ntg) and (not nns or lns==nns) |
| 18743 |
if ok then |
| 18744 |
break |
| 18745 |
end |
| 18746 |
end |
| 18747 |
if directive then |
| 18748 |
if ok then |
| 18749 |
local llp=ll.__p__;if llp~=p then p=llp;m=1 else m=m+1 end |
| 18750 |
c=c+1 |
| 18751 |
collected[c]=ll |
| 18752 |
ll.mi=m |
| 18753 |
end |
| 18754 |
elseif not ok then |
| 18755 |
local llp=ll.__p__;if llp~=p then p=llp;m=1 else m=m+1 end |
| 18756 |
c=c+1 |
| 18757 |
collected[c]=ll |
| 18758 |
ll.mi=m |
| 18759 |
end |
| 18760 |
end |
| 18761 |
end |
| 18762 |
return collected |
| 18763 |
end |
| 18764 |
end |
| 18765 |
local quit_expression=false |
| 18766 |
local function apply_expression(list,expression,order) |
| 18767 |
local collected={} |
| 18768 |
local c=0 |
| 18769 |
quit_expression=false |
| 18770 |
for l=1,#list do |
| 18771 |
local ll=list[l] |
| 18772 |
if expression(list,ll,l,order) then |
| 18773 |
c=c+1 |
| 18774 |
collected[c]=ll |
| 18775 |
end |
| 18776 |
if quit_expression then |
| 18777 |
break |
| 18778 |
end |
| 18779 |
end |
| 18780 |
return collected |
| 18781 |
end |
| 18782 |
local function apply_selector(list,specification) |
| 18783 |
if xml.applyselector then |
| 18784 |
apply_selector=xml.applyselector |
| 18785 |
return apply_selector(list,specification) |
| 18786 |
else |
| 18787 |
return list |
| 18788 |
end |
| 18789 |
end |
| 18790 |
local P,V,C,Cs,Cc,Ct,R,S,Cg,Cb=lpeg.P,lpeg.V,lpeg.C,lpeg.Cs,lpeg.Cc,lpeg.Ct,lpeg.R,lpeg.S,lpeg.Cg,lpeg.Cb |
| 18791 |
local spaces=S(" \n\r\t\f")^0 |
| 18792 |
local lp_space=S(" \n\r\t\f") |
| 18793 |
local lp_any=P(1) |
| 18794 |
local lp_noequal=P("!=")/"~="+P("<=")+P(">=")+P("==") |
| 18795 |
local lp_doequal=P("=")/"==" |
| 18796 |
local lp_or=P("|")/" or " |
| 18797 |
local lp_and=P("&")/" and " |
| 18798 |
local builtin={ |
| 18799 |
text="(ll.dt[1] or '')", |
| 18800 |
content="ll.dt", |
| 18801 |
name="((ll.ns~='' and ll.ns..':'..ll.tg) or ll.tg)", |
| 18802 |
tag="ll.tg", |
| 18803 |
position="l", |
| 18804 |
firstindex="1", |
| 18805 |
firstelement="1", |
| 18806 |
first="1", |
| 18807 |
lastindex="(#ll.__p__.dt or 1)", |
| 18808 |
lastelement="(ll.__p__.en or 1)", |
| 18809 |
last="#list", |
| 18810 |
list="list", |
| 18811 |
self="ll", |
| 18812 |
rootposition="order", |
| 18813 |
order="order", |
| 18814 |
element="(ll.ei or 1)", |
| 18815 |
index="(ll.ni or 1)", |
| 18816 |
match="(ll.mi or 1)", |
| 18817 |
namespace="ll.ns", |
| 18818 |
ns="ll.ns", |
| 18819 |
} |
| 18820 |
local lp_builtin=lpeg.utfchartabletopattern(builtin)/builtin*((spaces*P("(")*spaces*P(")"))/"") |
| 18821 |
local lp_attribute=(P("@")+P("attribute::"))/""*Cc("(ll.at and ll.at['")*((R("az","AZ")+S("-_:"))^1)*Cc("'])") |
| 18822 |
local lp_fastpos_p=P("+")^0*R("09")^1*P(-1)/"l==%0" |
| 18823 |
local lp_fastpos_n=P("-")*R("09")^1*P(-1)/"(%0<0 and (#list+%0+1==l))" |
| 18824 |
local lp_fastpos=lp_fastpos_n+lp_fastpos_p |
| 18825 |
local lp_reserved=C("and")+C("or")+C("not")+C("div")+C("mod")+C("true")+C("false") |
| 18826 |
local lp_lua_function=Cs((R("az","AZ","__")^1*(P(".")*R("az","AZ","__")^1)^1)*("("))/"%0" |
| 18827 |
local lp_function=C(R("az","AZ","__")^1)*P("(")/function(t) |
| 18828 |
if expressions[t] then |
| 18829 |
return "expr."..t.."(" |
| 18830 |
else |
| 18831 |
return "expr.error(" |
| 18832 |
end |
| 18833 |
end |
| 18834 |
local lparent=P("(") |
| 18835 |
local rparent=P(")") |
| 18836 |
local noparent=1-(lparent+rparent) |
| 18837 |
local nested=P{lparent*(noparent+V(1))^0*rparent} |
| 18838 |
local value=P(lparent*C((noparent+nested)^0)*rparent) |
| 18839 |
local lp_child=Cc("expr.child(ll,'")*R("az","AZ")*R("az","AZ","--","__")^0*Cc("')") |
| 18840 |
local lp_number=S("+-")*R("09")^1 |
| 18841 |
local lp_string=Cc("'")*R("az","AZ","--","__")^1*Cc("'") |
| 18842 |
local lp_content=(P("'")*(1-P("'"))^0*P("'")+P('"')*(1-P('"'))^0*P('"')) |
| 18843 |
local cleaner |
| 18844 |
local lp_special=(C(P("name")+P("text")+P("tag")+P("count")+P("child")))*value/function(t,s) |
| 18845 |
if expressions[t] then |
| 18846 |
s=s and s~="" and lpegmatch(cleaner,s) |
| 18847 |
if s and s~="" then |
| 18848 |
return "expr."..t.."(ll,"..s..")" |
| 18849 |
else |
| 18850 |
return "expr."..t.."(ll)" |
| 18851 |
end |
| 18852 |
else |
| 18853 |
return "expr.error("..t..")" |
| 18854 |
end |
| 18855 |
end |
| 18856 |
local content=lp_builtin+lp_attribute+lp_special+lp_noequal+lp_doequal+lp_or+lp_and+lp_reserved+lp_lua_function+lp_function+lp_content+ |
| 18857 |
lp_child+lp_any |
| 18858 |
local converter=Cs ( |
| 18859 |
lp_fastpos+(P { lparent*(V(1))^0*rparent+content } )^0 |
| 18860 |
) |
| 18861 |
cleaner=Cs (( |
| 18862 |
lp_reserved+lp_number+lp_string+1 )^1 ) |
| 18863 |
local template_e=[[ |
| 18864 |
local expr = xml.expressions |
| 18865 |
return function(list,ll,l,order) |
| 18866 |
return %s |
| 18867 |
end |
| 18868 |
]] |
| 18869 |
local template_f_y=[[ |
| 18870 |
local finalizer = xml.finalizers['%s']['%s'] |
| 18871 |
return function(collection) |
| 18872 |
return finalizer(collection,%s) |
| 18873 |
end |
| 18874 |
]] |
| 18875 |
local template_f_n=[[ |
| 18876 |
return xml.finalizers['%s']['%s'] |
| 18877 |
]] |
| 18878 |
local register_last_match={ kind="axis",axis="last-match" } |
| 18879 |
local register_self={ kind="axis",axis="self" } |
| 18880 |
local register_parent={ kind="axis",axis="parent" } |
| 18881 |
local register_descendant={ kind="axis",axis="descendant" } |
| 18882 |
local register_child={ kind="axis",axis="child" } |
| 18883 |
local register_descendant_or_self={ kind="axis",axis="descendant-or-self" } |
| 18884 |
local register_root={ kind="axis",axis="root" } |
| 18885 |
local register_ancestor={ kind="axis",axis="ancestor" } |
| 18886 |
local register_ancestor_or_self={ kind="axis",axis="ancestor-or-self" } |
| 18887 |
local register_attribute={ kind="axis",axis="attribute" } |
| 18888 |
local register_namespace={ kind="axis",axis="namespace" } |
| 18889 |
local register_following={ kind="axis",axis="following" } |
| 18890 |
local register_following_sibling={ kind="axis",axis="following-sibling" } |
| 18891 |
local register_preceding={ kind="axis",axis="preceding" } |
| 18892 |
local register_preceding_sibling={ kind="axis",axis="preceding-sibling" } |
| 18893 |
local register_reverse_sibling={ kind="axis",axis="reverse-sibling" } |
| 18894 |
local register_auto_descendant_or_self={ kind="axis",axis="auto-descendant-or-self" } |
| 18895 |
local register_auto_descendant={ kind="axis",axis="auto-descendant" } |
| 18896 |
local register_auto_self={ kind="axis",axis="auto-self" } |
| 18897 |
local register_auto_child={ kind="axis",axis="auto-child" } |
| 18898 |
local register_initial_child={ kind="axis",axis="initial-child" } |
| 18899 |
local register_all_nodes={ kind="nodes",nodetest=true,nodes={ true,false,false } } |
| 18900 |
local skip={} |
| 18901 |
local function errorrunner_e(str,cnv) |
| 18902 |
if not skip[str] then |
| 18903 |
report_lpath("error in expression: %s => %s",str,cnv) |
| 18904 |
skip[str]=cnv or str |
| 18905 |
end |
| 18906 |
return false |
| 18907 |
end |
| 18908 |
local function errorrunner_f(str,arg) |
| 18909 |
report_lpath("error in finalizer: %s(%s)",str,arg or "") |
| 18910 |
return false |
| 18911 |
end |
| 18912 |
local function register_nodes(nodetest,nodes) |
| 18913 |
return { kind="nodes",nodetest=nodetest,nodes=nodes } |
| 18914 |
end |
| 18915 |
local function register_selector(specification) |
| 18916 |
return { kind="selector",specification=specification } |
| 18917 |
end |
| 18918 |
local function register_expression(expression) |
| 18919 |
local converted=lpegmatch(converter,expression) |
| 18920 |
local wrapped=format(template_e,converted) |
| 18921 |
local runner=load(wrapped) |
| 18922 |
runner=(runner and runner()) or function() errorrunner_e(expression,converted) end |
| 18923 |
return { kind="expression",expression=expression,converted=converted,evaluator=runner } |
| 18924 |
end |
| 18925 |
local function register_finalizer(protocol,name,arguments) |
| 18926 |
local runner |
| 18927 |
if arguments and arguments~="" then |
| 18928 |
runner=load(format(template_f_y,protocol or xml.defaultprotocol,name,arguments)) |
| 18929 |
else |
| 18930 |
runner=load(format(template_f_n,protocol or xml.defaultprotocol,name)) |
| 18931 |
end |
| 18932 |
runner=(runner and runner()) or function() errorrunner_f(name,arguments) end |
| 18933 |
return { kind="finalizer",name=name,arguments=arguments,finalizer=runner } |
| 18934 |
end |
| 18935 |
local expression=P { "ex", |
| 18936 |
ex="["*C((V("sq")+V("dq")+(1-S("[]"))+V("ex"))^0)*"]", |
| 18937 |
sq="'"*(1-S("'"))^0*"'", |
| 18938 |
dq='"'*(1-S('"'))^0*'"', |
| 18939 |
} |
| 18940 |
local arguments=P { "ar", |
| 18941 |
ar="("*Cs((V("sq")+V("dq")+V("nq")+P(1-P(")")))^0)*")", |
| 18942 |
nq=((1-S("),'\""))^1)/function(s) return format("%q",s) end, |
| 18943 |
sq=P("'")*(1-P("'"))^0*P("'"), |
| 18944 |
dq=P('"')*(1-P('"'))^0*P('"'), |
| 18945 |
} |
| 18946 |
local function register_error(str) |
| 18947 |
return { kind="error",error=format("unparsed: %s",str) } |
| 18948 |
end |
| 18949 |
local special_1=P("*")*Cc(register_auto_descendant)*Cc(register_all_nodes) |
| 18950 |
local special_2=P("/")*Cc(register_auto_self) |
| 18951 |
local special_3=P("")*Cc(register_auto_self) |
| 18952 |
local no_nextcolon=P(-1)+#(1-P(":")) |
| 18953 |
local no_nextlparent=P(-1)+#(1-P("(")) |
| 18954 |
local pathparser=Ct { "patterns", |
| 18955 |
patterns=spaces*V("protocol")*spaces*( |
| 18956 |
(V("special")*spaces*P(-1) )+(V("initial")*spaces*V("step")*spaces*(P("/")*spaces*V("step")*spaces)^0 ) |
| 18957 |
), |
| 18958 |
protocol=Cg(V("letters"),"protocol")*P("://")+Cg(Cc(nil),"protocol"), |
| 18959 |
step=((V("shortcuts")+V("selector")+P("/")+V("axis"))*spaces*V("nodes")^0+V("error"))*spaces*V("expressions")^0*spaces*V("finalizer")^0, |
| 18960 |
axis=V("last_match")+V("descendant")+V("child")+V("parent")+V("self")+V("root")+V("ancestor")+V("descendant_or_self")+V("following_sibling")+V("following")+V("reverse_sibling")+V("preceding_sibling")+V("preceding")+V("ancestor_or_self")+#(1-P(-1))*Cc(register_auto_child), |
| 18961 |
special=special_1+special_2+special_3, |
| 18962 |
initial=(P("/")*spaces*Cc(register_initial_child))^-1, |
| 18963 |
error=(P(1)^1)/register_error, |
| 18964 |
shortcuts_a=V("s_descendant_or_self")+V("s_descendant")+V("s_child")+V("s_parent")+V("s_self")+V("s_root")+V("s_ancestor")+V("s_lastmatch"), |
| 18965 |
shortcuts=V("shortcuts_a")*(spaces*"/"*spaces*V("shortcuts_a"))^0, |
| 18966 |
s_descendant_or_self=(P("***/")+P("/"))*Cc(register_descendant_or_self), |
| 18967 |
s_descendant=P("**")*Cc(register_descendant), |
| 18968 |
s_child=P("*")*no_nextcolon*Cc(register_child), |
| 18969 |
s_parent=P("..")*Cc(register_parent), |
| 18970 |
s_self=P("." )*Cc(register_self), |
| 18971 |
s_root=P("^^")*Cc(register_root), |
| 18972 |
s_ancestor=P("^")*Cc(register_ancestor), |
| 18973 |
s_lastmatch=P("=")*Cc(register_last_match), |
| 18974 |
descendant=P("descendant::")*Cc(register_descendant), |
| 18975 |
child=P("child::")*Cc(register_child), |
| 18976 |
parent=P("parent::")*Cc(register_parent), |
| 18977 |
self=P("self::")*Cc(register_self), |
| 18978 |
root=P('root::')*Cc(register_root), |
| 18979 |
ancestor=P('ancestor::')*Cc(register_ancestor), |
| 18980 |
descendant_or_self=P('descendant-or-self::')*Cc(register_descendant_or_self), |
| 18981 |
ancestor_or_self=P('ancestor-or-self::')*Cc(register_ancestor_or_self), |
| 18982 |
following=P('following::')*Cc(register_following), |
| 18983 |
following_sibling=P('following-sibling::')*Cc(register_following_sibling), |
| 18984 |
preceding=P('preceding::')*Cc(register_preceding), |
| 18985 |
preceding_sibling=P('preceding-sibling::')*Cc(register_preceding_sibling), |
| 18986 |
reverse_sibling=P('reverse-sibling::')*Cc(register_reverse_sibling), |
| 18987 |
last_match=P('last-match::')*Cc(register_last_match), |
| 18988 |
selector=P("{")*C((1-P("}"))^1)*P("}")/register_selector, |
| 18989 |
nodes=(V("nodefunction")*spaces*P("(")*V("nodeset")*P(")")+V("nodetest")*V("nodeset"))/register_nodes, |
| 18990 |
expressions=expression/register_expression, |
| 18991 |
letters=R("az")^1, |
| 18992 |
name=(1-S("/[]()|:*!"))^1, |
| 18993 |
negate=P("!")*Cc(false), |
| 18994 |
nodefunction=V("negate")+P("not")*Cc(false)+Cc(true), |
| 18995 |
nodetest=V("negate")+Cc(true), |
| 18996 |
nodename=(V("negate")+Cc(true))*spaces*((V("wildnodename")*P(":")*V("wildnodename"))+(Cc(false)*V("wildnodename"))), |
| 18997 |
wildnodename=(C(V("name"))+P("*")*Cc(false))*no_nextlparent, |
| 18998 |
nodeset=spaces*Ct(V("nodename")*(spaces*P("|")*spaces*V("nodename"))^0)*spaces, |
| 18999 |
finalizer=(Cb("protocol")*P("/")^-1*C(V("name"))*arguments*P(-1))/register_finalizer, |
| 19000 |
} |
| 19001 |
xmlpatterns.pathparser=pathparser |
| 19002 |
local cache={} |
| 19003 |
local function nodesettostring(set,nodetest) |
| 19004 |
local t={} |
| 19005 |
for i=1,#set,3 do |
| 19006 |
local directive,ns,tg=set[i],set[i+1],set[i+2] |
| 19007 |
if not ns or ns=="" then ns="*" end |
| 19008 |
if not tg or tg=="" then tg="*" end |
| 19009 |
tg=(tg=="@rt@" and "[root]") or format("%s:%s",ns,tg) |
| 19010 |
t[#t+1]=(directive and tg) or format("not(%s)",tg) |
| 19011 |
end |
| 19012 |
if nodetest==false then |
| 19013 |
return format("not(%s)",concat(t,"|")) |
| 19014 |
else |
| 19015 |
return concat(t,"|") |
| 19016 |
end |
| 19017 |
end |
| 19018 |
local function tagstostring(list) |
| 19019 |
if #list==0 then |
| 19020 |
return "no elements" |
| 19021 |
else |
| 19022 |
local t={} |
| 19023 |
for i=1,#list do |
| 19024 |
local li=list[i] |
| 19025 |
local ns=li.ns |
| 19026 |
local tg=li.tg |
| 19027 |
if not ns or ns=="" then ns="*" end |
| 19028 |
if not tg or tg=="" then tg="*" end |
| 19029 |
t[i]=(tg=="@rt@" and "[root]") or format("%s:%s",ns,tg) |
| 19030 |
end |
| 19031 |
return concat(t," ") |
| 19032 |
end |
| 19033 |
end |
| 19034 |
xml.nodesettostring=nodesettostring |
| 19035 |
local lpath |
| 19036 |
local function lshow(parsed) |
| 19037 |
if type(parsed)=="string" then |
| 19038 |
parsed=lpath(parsed) |
| 19039 |
end |
| 19040 |
report_lpath("%s://%s => %s",parsed.protocol or xml.defaultprotocol,parsed.pattern, |
| 19041 |
table.serialize(parsed,false)) |
| 19042 |
end |
| 19043 |
xml.lshow=lshow |
| 19044 |
local function add_comment(p,str) |
| 19045 |
local pc=p.comment |
| 19046 |
if not pc then |
| 19047 |
p.comment={ str } |
| 19048 |
else |
| 19049 |
pc[#pc+1]=str |
| 19050 |
end |
| 19051 |
end |
| 19052 |
lpath=function (pattern) |
| 19053 |
lpathcalls=lpathcalls+1 |
| 19054 |
if type(pattern)=="table" then |
| 19055 |
return pattern |
| 19056 |
else |
| 19057 |
local parsed=cache[pattern] |
| 19058 |
if parsed then |
| 19059 |
lpathcached=lpathcached+1 |
| 19060 |
else |
| 19061 |
parsed=lpegmatch(pathparser,pattern) |
| 19062 |
if parsed then |
| 19063 |
parsed.pattern=pattern |
| 19064 |
local np=#parsed |
| 19065 |
if np==0 then |
| 19066 |
parsed={ pattern=pattern,register_self,state="parsing error" } |
| 19067 |
report_lpath("parsing error in pattern: %s",pattern) |
| 19068 |
lshow(parsed) |
| 19069 |
else |
| 19070 |
local pi=parsed[1] |
| 19071 |
if pi.axis=="auto-child" then |
| 19072 |
if false then |
| 19073 |
add_comment(parsed,"auto-child replaced by auto-descendant-or-self") |
| 19074 |
parsed[1]=register_auto_descendant_or_self |
| 19075 |
else |
| 19076 |
add_comment(parsed,"auto-child replaced by auto-descendant") |
| 19077 |
parsed[1]=register_auto_descendant |
| 19078 |
end |
| 19079 |
elseif pi.axis=="initial-child" and np>1 and parsed[2].axis then |
| 19080 |
add_comment(parsed,"initial-child removed") |
| 19081 |
remove(parsed,1) |
| 19082 |
end |
| 19083 |
local np=#parsed |
| 19084 |
if np>1 then |
| 19085 |
local pnp=parsed[np] |
| 19086 |
if pnp.kind=="nodes" and pnp.nodetest==true then |
| 19087 |
local nodes=pnp.nodes |
| 19088 |
if nodes[1]==true and nodes[2]==false and nodes[3]==false then |
| 19089 |
add_comment(parsed,"redundant final wildcard filter removed") |
| 19090 |
remove(parsed,np) |
| 19091 |
end |
| 19092 |
end |
| 19093 |
end |
| 19094 |
end |
| 19095 |
else |
| 19096 |
parsed={ pattern=pattern } |
| 19097 |
end |
| 19098 |
cache[pattern]=parsed |
| 19099 |
if trace_lparse and not trace_lprofile then |
| 19100 |
lshow(parsed) |
| 19101 |
end |
| 19102 |
end |
| 19103 |
return parsed |
| 19104 |
end |
| 19105 |
end |
| 19106 |
xml.lpath=lpath |
| 19107 |
do |
| 19108 |
local profiled={} |
| 19109 |
xml.profiled=profiled |
| 19110 |
local lastmatch=nil |
| 19111 |
local keepmatch=nil |
| 19112 |
if directives then |
| 19113 |
directives.register("xml.path.keeplastmatch",function(v) |
| 19114 |
keepmatch=v |
| 19115 |
lastmatch=nil |
| 19116 |
end) |
| 19117 |
end |
| 19118 |
apply_axis["last-match"]=function() |
| 19119 |
return lastmatch or {} |
| 19120 |
end |
| 19121 |
local function profiled_apply(list,parsed,nofparsed,order) |
| 19122 |
local p=profiled[parsed.pattern] |
| 19123 |
if p then |
| 19124 |
p.tested=p.tested+1 |
| 19125 |
else |
| 19126 |
p={ tested=1,matched=0,finalized=0 } |
| 19127 |
profiled[parsed.pattern]=p |
| 19128 |
end |
| 19129 |
local collected=list |
| 19130 |
for i=1,nofparsed do |
| 19131 |
local pi=parsed[i] |
| 19132 |
local kind=pi.kind |
| 19133 |
if kind=="axis" then |
| 19134 |
collected=apply_axis[pi.axis](collected) |
| 19135 |
elseif kind=="nodes" then |
| 19136 |
collected=apply_nodes(collected,pi.nodetest,pi.nodes) |
| 19137 |
elseif kind=="expression" then |
| 19138 |
collected=apply_expression(collected,pi.evaluator,order) |
| 19139 |
elseif kind=="selector" then |
| 19140 |
collected=apply_selector(collected,pi.specification) |
| 19141 |
elseif kind=="finalizer" then |
| 19142 |
collected=pi.finalizer(collected) |
| 19143 |
p.matched=p.matched+1 |
| 19144 |
p.finalized=p.finalized+1 |
| 19145 |
return collected |
| 19146 |
end |
| 19147 |
if not collected or #collected==0 then |
| 19148 |
local pn=i<nofparsed and parsed[nofparsed] |
| 19149 |
if pn and pn.kind=="finalizer" then |
| 19150 |
collected=pn.finalizer(collected) |
| 19151 |
p.finalized=p.finalized+1 |
| 19152 |
return collected |
| 19153 |
end |
| 19154 |
return nil |
| 19155 |
end |
| 19156 |
end |
| 19157 |
if collected then |
| 19158 |
p.matched=p.matched+1 |
| 19159 |
end |
| 19160 |
return collected |
| 19161 |
end |
| 19162 |
local function traced_apply(list,parsed,nofparsed,order) |
| 19163 |
if trace_lparse then |
| 19164 |
lshow(parsed) |
| 19165 |
end |
| 19166 |
report_lpath("collecting: %s",parsed.pattern) |
| 19167 |
report_lpath("root tags : %s",tagstostring(list)) |
| 19168 |
report_lpath("order : %s",order or "unset") |
| 19169 |
local collected=list |
| 19170 |
for i=1,nofparsed do |
| 19171 |
local pi=parsed[i] |
| 19172 |
local kind=pi.kind |
| 19173 |
if kind=="axis" then |
| 19174 |
collected=apply_axis[pi.axis](collected) |
| 19175 |
report_lpath("% 10i : ax : %s",(collected and #collected) or 0,pi.axis) |
| 19176 |
elseif kind=="nodes" then |
| 19177 |
collected=apply_nodes(collected,pi.nodetest,pi.nodes) |
| 19178 |
report_lpath("% 10i : ns : %s",(collected and #collected) or 0,nodesettostring(pi.nodes,pi.nodetest)) |
| 19179 |
elseif kind=="expression" then |
| 19180 |
collected=apply_expression(collected,pi.evaluator,order) |
| 19181 |
report_lpath("% 10i : ex : %s -> %s",(collected and #collected) or 0,pi.expression,pi.converted) |
| 19182 |
elseif kind=="selector" then |
| 19183 |
collected=apply_selector(collected,pi.specification) |
| 19184 |
report_lpath("% 10i : se : %s ",(collected and #collected) or 0,pi.specification) |
| 19185 |
elseif kind=="finalizer" then |
| 19186 |
collected=pi.finalizer(collected) |
| 19187 |
report_lpath("% 10i : fi : %s : %s(%s)",(type(collected)=="table" and #collected) or 0,parsed.protocol or xml.defaultprotocol,pi.name,pi.arguments or "") |
| 19188 |
return collected |
| 19189 |
end |
| 19190 |
if not collected or #collected==0 then |
| 19191 |
local pn=i<nofparsed and parsed[nofparsed] |
| 19192 |
if pn and pn.kind=="finalizer" then |
| 19193 |
collected=pn.finalizer(collected) |
| 19194 |
report_lpath("% 10i : fi : %s : %s(%s)",(type(collected)=="table" and #collected) or 0,parsed.protocol or xml.defaultprotocol,pn.name,pn.arguments or "") |
| 19195 |
return collected |
| 19196 |
end |
| 19197 |
return nil |
| 19198 |
end |
| 19199 |
end |
| 19200 |
return collected |
| 19201 |
end |
| 19202 |
local function normal_apply(list,parsed,nofparsed,order) |
| 19203 |
local collected=list |
| 19204 |
for i=1,nofparsed do |
| 19205 |
local pi=parsed[i] |
| 19206 |
local kind=pi.kind |
| 19207 |
if kind=="axis" then |
| 19208 |
local axis=pi.axis |
| 19209 |
if axis~="self" then |
| 19210 |
collected=apply_axis[axis](collected) |
| 19211 |
end |
| 19212 |
elseif kind=="nodes" then |
| 19213 |
collected=apply_nodes(collected,pi.nodetest,pi.nodes) |
| 19214 |
elseif kind=="expression" then |
| 19215 |
collected=apply_expression(collected,pi.evaluator,order) |
| 19216 |
elseif kind=="selector" then |
| 19217 |
collected=apply_selector(collected,pi.specification) |
| 19218 |
elseif kind=="finalizer" then |
| 19219 |
return pi.finalizer(collected) |
| 19220 |
end |
| 19221 |
if not collected or #collected==0 then |
| 19222 |
local pf=i<nofparsed and parsed[nofparsed].finalizer |
| 19223 |
if pf then |
| 19224 |
return pf(collected) |
| 19225 |
end |
| 19226 |
return nil |
| 19227 |
end |
| 19228 |
end |
| 19229 |
return collected |
| 19230 |
end |
| 19231 |
local apply=normal_apply |
| 19232 |
if trackers then |
| 19233 |
trackers.register("xml.path,xml.parse,xml.profile",function() |
| 19234 |
if trace_lprofile then |
| 19235 |
apply=profiled_apply |
| 19236 |
elseif trace_lpath then |
| 19237 |
apply=traced_apply |
| 19238 |
else |
| 19239 |
apply=normal_apply |
| 19240 |
end |
| 19241 |
end) |
| 19242 |
end |
| 19243 |
function xml.applylpath(list,pattern) |
| 19244 |
if not list then |
| 19245 |
lastmatch=nil |
| 19246 |
return |
| 19247 |
end |
| 19248 |
local parsed=cache[pattern] |
| 19249 |
if parsed then |
| 19250 |
lpathcalls=lpathcalls+1 |
| 19251 |
lpathcached=lpathcached+1 |
| 19252 |
elseif type(pattern)=="table" then |
| 19253 |
lpathcalls=lpathcalls+1 |
| 19254 |
parsed=pattern |
| 19255 |
else |
| 19256 |
parsed=lpath(pattern) or pattern |
| 19257 |
end |
| 19258 |
if not parsed then |
| 19259 |
lastmatch=nil |
| 19260 |
return |
| 19261 |
end |
| 19262 |
local nofparsed=#parsed |
| 19263 |
if nofparsed==0 then |
| 19264 |
lastmatch=nil |
| 19265 |
return |
| 19266 |
end |
| 19267 |
local collected=apply({ list },parsed,nofparsed,list.mi) |
| 19268 |
lastmatch=keepmatch and collected or nil |
| 19269 |
return collected |
| 19270 |
end |
| 19271 |
function xml.lastmatch() |
| 19272 |
return lastmatch |
| 19273 |
end |
| 19274 |
local stack={} |
| 19275 |
function xml.pushmatch() |
| 19276 |
insert(stack,lastmatch) |
| 19277 |
end |
| 19278 |
function xml.popmatch() |
| 19279 |
lastmatch=remove(stack) |
| 19280 |
end |
| 19281 |
end |
| 19282 |
local applylpath=xml.applylpath |
| 19283 |
function xml.filter(root,pattern) |
| 19284 |
return applylpath(root,pattern) |
| 19285 |
end |
| 19286 |
expressions.child=function(e,pattern) |
| 19287 |
return applylpath(e,pattern) |
| 19288 |
end |
| 19289 |
expressions.count=function(e,pattern) |
| 19290 |
local collected=applylpath(e,pattern) |
| 19291 |
return pattern and (collected and #collected) or 0 |
| 19292 |
end |
| 19293 |
expressions.attribute=function(e,name,value) |
| 19294 |
if type(e)=="table" and name then |
| 19295 |
local a=e.at |
| 19296 |
if a then |
| 19297 |
local v=a[name] |
| 19298 |
if value then |
| 19299 |
return v==value |
| 19300 |
else |
| 19301 |
return v |
| 19302 |
end |
| 19303 |
end |
| 19304 |
end |
| 19305 |
return nil |
| 19306 |
end |
| 19307 |
expressions.oneof=function(s,...) |
| 19308 |
for i=1,select("#",...) do |
| 19309 |
if s==select(i,...) then |
| 19310 |
return true |
| 19311 |
end |
| 19312 |
end |
| 19313 |
return false |
| 19314 |
end |
| 19315 |
expressions.error=function(str) |
| 19316 |
xml.errorhandler(format("unknown function in lpath expression: %s",tostring(str or "?"))) |
| 19317 |
return false |
| 19318 |
end |
| 19319 |
expressions.undefined=function(s) |
| 19320 |
return s==nil |
| 19321 |
end |
| 19322 |
expressions.quit=function(s) |
| 19323 |
if s or s==nil then |
| 19324 |
quit_expression=true |
| 19325 |
end |
| 19326 |
return true |
| 19327 |
end |
| 19328 |
expressions.print=function(...) |
| 19329 |
print(...) |
| 19330 |
return true |
| 19331 |
end |
| 19332 |
expressions.find=function(str,...) |
| 19333 |
return str and find(str,...) |
| 19334 |
end |
| 19335 |
expressions.upper=function(str) return str and upper(str) or "" end |
| 19336 |
expressions.lower=function(str) return str and lower(str) or "" end |
| 19337 |
expressions.number=tonumber |
| 19338 |
expressions.boolean=toboolean |
| 19339 |
function expressions.contains(str,pattern) |
| 19340 |
local t=type(str) |
| 19341 |
if t=="string" then |
| 19342 |
if find(str,pattern) then |
| 19343 |
return true |
| 19344 |
end |
| 19345 |
elseif t=="table" then |
| 19346 |
for i=1,#str do |
| 19347 |
local d=str[i] |
| 19348 |
if type(d)=="string" and find(d,pattern) then |
| 19349 |
return true |
| 19350 |
end |
| 19351 |
end |
| 19352 |
end |
| 19353 |
return false |
| 19354 |
end |
| 19355 |
function expressions.idstring(str) |
| 19356 |
return type(str)=="string" and gsub(str,"^#","") or "" |
| 19357 |
end |
| 19358 |
local function traverse(root,pattern,handle) |
| 19359 |
local collected=applylpath(root,pattern) |
| 19360 |
if collected then |
| 19361 |
for c=1,#collected do |
| 19362 |
local e=collected[c] |
| 19363 |
local r=e.__p__ |
| 19364 |
handle(r,r.dt,e.ni) |
| 19365 |
end |
| 19366 |
end |
| 19367 |
end |
| 19368 |
local function selection(root,pattern,handle) |
| 19369 |
local collected=applylpath(root,pattern) |
| 19370 |
if collected then |
| 19371 |
if handle then |
| 19372 |
for c=1,#collected do |
| 19373 |
handle(collected[c]) |
| 19374 |
end |
| 19375 |
else |
| 19376 |
return collected |
| 19377 |
end |
| 19378 |
end |
| 19379 |
end |
| 19380 |
xml.traverse=traverse |
| 19381 |
xml.selection=selection |
| 19382 |
local function dofunction(collected,fnc,...) |
| 19383 |
if collected then |
| 19384 |
local f=functions[fnc] |
| 19385 |
if f then |
| 19386 |
for c=1,#collected do |
| 19387 |
f(collected[c],...) |
| 19388 |
end |
| 19389 |
else |
| 19390 |
report_lpath("unknown function %a",fnc) |
| 19391 |
end |
| 19392 |
end |
| 19393 |
end |
| 19394 |
finalizers.xml["function"]=dofunction |
| 19395 |
finalizers.tex["function"]=dofunction |
| 19396 |
expressions.text=function(e,n) |
| 19397 |
local rdt=e.__p__.dt |
| 19398 |
return rdt and rdt[n] or "" |
| 19399 |
end |
| 19400 |
expressions.name=function(e,n) |
| 19401 |
local found=false |
| 19402 |
n=tonumber(n) or 0 |
| 19403 |
if n==0 then |
| 19404 |
found=type(e)=="table" and e |
| 19405 |
elseif n<0 then |
| 19406 |
local d=e.__p__.dt |
| 19407 |
local k=e.ni |
| 19408 |
for i=k-1,1,-1 do |
| 19409 |
local di=d[i] |
| 19410 |
if type(di)=="table" then |
| 19411 |
if n==-1 then |
| 19412 |
found=di |
| 19413 |
break |
| 19414 |
else |
| 19415 |
n=n+1 |
| 19416 |
end |
| 19417 |
end |
| 19418 |
end |
| 19419 |
else |
| 19420 |
local d=e.__p__.dt |
| 19421 |
local k=e.ni |
| 19422 |
for i=k+1,#d,1 do |
| 19423 |
local di=d[i] |
| 19424 |
if type(di)=="table" then |
| 19425 |
if n==1 then |
| 19426 |
found=di |
| 19427 |
break |
| 19428 |
else |
| 19429 |
n=n-1 |
| 19430 |
end |
| 19431 |
end |
| 19432 |
end |
| 19433 |
end |
| 19434 |
if found then |
| 19435 |
local ns=found.rn or found.ns or "" |
| 19436 |
local tg=found.tg |
| 19437 |
if ns~="" then |
| 19438 |
return ns..":"..tg |
| 19439 |
else |
| 19440 |
return tg |
| 19441 |
end |
| 19442 |
else |
| 19443 |
return "" |
| 19444 |
end |
| 19445 |
end |
| 19446 |
expressions.tag=function(e,n) |
| 19447 |
if not e then |
| 19448 |
return "" |
| 19449 |
else |
| 19450 |
local found=false |
| 19451 |
n=tonumber(n) or 0 |
| 19452 |
if n==0 then |
| 19453 |
found=(type(e)=="table") and e |
| 19454 |
elseif n<0 then |
| 19455 |
local d=e.__p__.dt |
| 19456 |
local k=e.ni |
| 19457 |
for i=k-1,1,-1 do |
| 19458 |
local di=d[i] |
| 19459 |
if type(di)=="table" then |
| 19460 |
if n==-1 then |
| 19461 |
found=di |
| 19462 |
break |
| 19463 |
else |
| 19464 |
n=n+1 |
| 19465 |
end |
| 19466 |
end |
| 19467 |
end |
| 19468 |
else |
| 19469 |
local d=e.__p__.dt |
| 19470 |
local k=e.ni |
| 19471 |
for i=k+1,#d,1 do |
| 19472 |
local di=d[i] |
| 19473 |
if type(di)=="table" then |
| 19474 |
if n==1 then |
| 19475 |
found=di |
| 19476 |
break |
| 19477 |
else |
| 19478 |
n=n-1 |
| 19479 |
end |
| 19480 |
end |
| 19481 |
end |
| 19482 |
end |
| 19483 |
return (found and found.tg) or "" |
| 19484 |
end |
| 19485 |
end |
| 19486 |
local dummy=function() end |
| 19487 |
function xml.elements(root,pattern,reverse) |
| 19488 |
local collected=applylpath(root,pattern) |
| 19489 |
if not collected then |
| 19490 |
return dummy |
| 19491 |
end |
| 19492 |
local n=#collected |
| 19493 |
if n==0 then |
| 19494 |
return dummy |
| 19495 |
end |
| 19496 |
if reverse then |
| 19497 |
local c=n+1 |
| 19498 |
return function() |
| 19499 |
if c>1 then |
| 19500 |
c=c-1 |
| 19501 |
local e=collected[c] |
| 19502 |
local r=e.__p__ |
| 19503 |
return r,r.dt,e.ni |
| 19504 |
end |
| 19505 |
end |
| 19506 |
else |
| 19507 |
local c=0 |
| 19508 |
return function() |
| 19509 |
if c<n then |
| 19510 |
c=c+1 |
| 19511 |
local e=collected[c] |
| 19512 |
local r=e.__p__ |
| 19513 |
return r,r.dt,e.ni |
| 19514 |
end |
| 19515 |
end |
| 19516 |
end |
| 19517 |
end |
| 19518 |
function xml.collected(root,pattern,reverse) |
| 19519 |
local collected=applylpath(root,pattern) |
| 19520 |
if not collected then |
| 19521 |
return dummy |
| 19522 |
end |
| 19523 |
local n=#collected |
| 19524 |
if n==0 then |
| 19525 |
return dummy |
| 19526 |
end |
| 19527 |
if reverse then |
| 19528 |
local c=n+1 |
| 19529 |
return function() |
| 19530 |
if c>1 then |
| 19531 |
c=c-1 |
| 19532 |
return collected[c] |
| 19533 |
end |
| 19534 |
end |
| 19535 |
else |
| 19536 |
local c=0 |
| 19537 |
return function() |
| 19538 |
if c<n then |
| 19539 |
c=c+1 |
| 19540 |
return collected[c] |
| 19541 |
end |
| 19542 |
end |
| 19543 |
end |
| 19544 |
end |
| 19545 |
function xml.inspect(collection,pattern) |
| 19546 |
pattern=pattern or "." |
| 19547 |
for e in xml.collected(collection,pattern or ".") do |
| 19548 |
report_lpath("pattern: %s\n\n%s\n",pattern,xml.tostring(e)) |
| 19549 |
end |
| 19550 |
end |
| 19551 |
local function split(e) |
| 19552 |
local dt=e.dt |
| 19553 |
if dt then |
| 19554 |
for i=1,#dt do |
| 19555 |
local dti=dt[i] |
| 19556 |
if type(dti)=="string" then |
| 19557 |
dti=gsub(dti,"^[\n\r]*(.-)[\n\r]*","%1") |
| 19558 |
dti=gsub(dti,"[\n\r]+","\n\n") |
| 19559 |
dt[i]=dti |
| 19560 |
else |
| 19561 |
split(dti) |
| 19562 |
end |
| 19563 |
end |
| 19564 |
end |
| 19565 |
return e |
| 19566 |
end |
| 19567 |
function xml.finalizers.paragraphs(c) |
| 19568 |
for i=1,#c do |
| 19569 |
split(c[i]) |
| 19570 |
end |
| 19571 |
return c |
| 19572 |
end |
| 19573 |
|
| 19574 |
|
| 19575 |
end -- of closure |
| 19576 |
|
| 19577 |
do -- create closure to overcome 200 locals limit |
| 19578 |
|
| 19579 |
package.loaded["lxml-mis"] = package.loaded["lxml-mis"] or true |
| 19580 |
|
| 19581 |
-- original size: 3542, stripped down to: 1808 |
| 19582 |
|
| 19583 |
if not modules then modules={} end modules ['lxml-mis']={ |
| 19584 |
version=1.001, |
| 19585 |
comment="this module is the basis for the lxml-* ones", |
| 19586 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 19587 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 19588 |
license="see context related readme files" |
| 19589 |
} |
| 19590 |
local xml,lpeg,string=xml,lpeg,string |
| 19591 |
local type=type |
| 19592 |
local concat=table.concat |
| 19593 |
local format,gsub,match=string.format,string.gsub,string.match |
| 19594 |
local lpegmatch,lpegpatterns=lpeg.match,lpeg.patterns |
| 19595 |
local P,S,R,C,V,Cc,Cs=lpeg.P,lpeg.S,lpeg.R,lpeg.C,lpeg.V,lpeg.Cc,lpeg.Cs |
| 19596 |
lpegpatterns.xml=lpegpatterns.xml or {} |
| 19597 |
local xmlpatterns=lpegpatterns.xml |
| 19598 |
local function xmlgsub(t,old,new) |
| 19599 |
local dt=t.dt |
| 19600 |
if dt then |
| 19601 |
for k=1,#dt do |
| 19602 |
local v=dt[k] |
| 19603 |
if type(v)=="string" then |
| 19604 |
dt[k]=gsub(v,old,new) |
| 19605 |
else |
| 19606 |
xmlgsub(v,old,new) |
| 19607 |
end |
| 19608 |
end |
| 19609 |
end |
| 19610 |
end |
| 19611 |
function xml.stripleadingspaces(dk,d,k) |
| 19612 |
if d and k then |
| 19613 |
local dkm=d[k-1] |
| 19614 |
if dkm and type(dkm)=="string" then |
| 19615 |
local s=match(dkm,"\n(%s+)") |
| 19616 |
xmlgsub(dk,"\n"..rep(" ",#s),"\n") |
| 19617 |
end |
| 19618 |
end |
| 19619 |
end |
| 19620 |
local normal=(1-S("<&>"))^0 |
| 19621 |
local special=P("<")/"<"+P(">")/">"+P("&")/"&" |
| 19622 |
local escaped=Cs(normal*(special*normal)^0) |
| 19623 |
local normal=(1-S"&")^0 |
| 19624 |
local special=P("<")/"<"+P(">")/">"+P("&")/"&" |
| 19625 |
local unescaped=Cs(normal*(special*normal)^0) |
| 19626 |
local cleansed=Cs(((P("<")*(1-P(">"))^0*P(">"))/""+1)^0) |
| 19627 |
xmlpatterns.escaped=escaped |
| 19628 |
xmlpatterns.unescaped=unescaped |
| 19629 |
xmlpatterns.cleansed=cleansed |
| 19630 |
function xml.escaped (str) return lpegmatch(escaped,str) end |
| 19631 |
function xml.unescaped(str) return lpegmatch(unescaped,str) end |
| 19632 |
function xml.cleansed (str) return lpegmatch(cleansed,str) end |
| 19633 |
function xml.fillin(root,pattern,str,check) |
| 19634 |
local e=xml.first(root,pattern) |
| 19635 |
if e then |
| 19636 |
local n=#e.dt |
| 19637 |
if not check or n==0 or (n==1 and e.dt[1]=="") then |
| 19638 |
e.dt={ str } |
| 19639 |
end |
| 19640 |
end |
| 19641 |
end |
| 19642 |
|
| 19643 |
|
| 19644 |
end -- of closure |
| 19645 |
|
| 19646 |
do -- create closure to overcome 200 locals limit |
| 19647 |
|
| 19648 |
package.loaded["lxml-aux"] = package.loaded["lxml-aux"] or true |
| 19649 |
|
| 19650 |
-- original size: 34522, stripped down to: 21511 |
| 19651 |
|
| 19652 |
if not modules then modules={} end modules ['lxml-aux']={ |
| 19653 |
version=1.001, |
| 19654 |
comment="this module is the basis for the lxml-* ones", |
| 19655 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 19656 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 19657 |
license="see context related readme files" |
| 19658 |
} |
| 19659 |
local trace_manipulations=false trackers.register("lxml.manipulations",function(v) trace_manipulations=v end) |
| 19660 |
local trace_inclusions=false trackers.register("lxml.inclusions",function(v) trace_inclusions=v end) |
| 19661 |
local report_xml=logs.reporter("xml") |
| 19662 |
local xml=xml |
| 19663 |
local xmlcopy,xmlname=xml.copy,xml.name |
| 19664 |
local xmlinheritedconvert=xml.inheritedconvert |
| 19665 |
local xmlapplylpath=xml.applylpath |
| 19666 |
local type,next,setmetatable,getmetatable=type,next,setmetatable,getmetatable |
| 19667 |
local insert,remove,fastcopy,concat=table.insert,table.remove,table.fastcopy,table.concat |
| 19668 |
local gmatch,gsub,format,find,strip,match=string.gmatch,string.gsub,string.format,string.find,string.strip,string.match |
| 19669 |
local utfbyte=utf.byte |
| 19670 |
local lpegmatch,lpegpatterns=lpeg.match,lpeg.patterns |
| 19671 |
local striplinepatterns=utilities.strings.striplinepatterns |
| 19672 |
local function report(what,pattern,c,e) |
| 19673 |
report_xml("%s element %a, root %a, position %a, index %a, pattern %a",what,xmlname(e),xmlname(e.__p__),c,e.ni,pattern) |
| 19674 |
end |
| 19675 |
local function withelements(e,handle,depth) |
| 19676 |
if e and handle then |
| 19677 |
local edt=e.dt |
| 19678 |
if edt then |
| 19679 |
depth=depth or 0 |
| 19680 |
for i=1,#edt do |
| 19681 |
local e=edt[i] |
| 19682 |
if type(e)=="table" then |
| 19683 |
handle(e,depth) |
| 19684 |
withelements(e,handle,depth+1) |
| 19685 |
end |
| 19686 |
end |
| 19687 |
end |
| 19688 |
end |
| 19689 |
end |
| 19690 |
xml.withelements=withelements |
| 19691 |
function xml.withelement(e,n,handle) |
| 19692 |
if e and n~=0 and handle then |
| 19693 |
local edt=e.dt |
| 19694 |
if edt then |
| 19695 |
if n>0 then |
| 19696 |
for i=1,#edt do |
| 19697 |
local ei=edt[i] |
| 19698 |
if type(ei)=="table" then |
| 19699 |
if n==1 then |
| 19700 |
handle(ei) |
| 19701 |
return |
| 19702 |
else |
| 19703 |
n=n-1 |
| 19704 |
end |
| 19705 |
end |
| 19706 |
end |
| 19707 |
elseif n<0 then |
| 19708 |
for i=#edt,1,-1 do |
| 19709 |
local ei=edt[i] |
| 19710 |
if type(ei)=="table" then |
| 19711 |
if n==-1 then |
| 19712 |
handle(ei) |
| 19713 |
return |
| 19714 |
else |
| 19715 |
n=n+1 |
| 19716 |
end |
| 19717 |
end |
| 19718 |
end |
| 19719 |
end |
| 19720 |
end |
| 19721 |
end |
| 19722 |
end |
| 19723 |
function xml.each(root,pattern,handle,reverse) |
| 19724 |
local collected=xmlapplylpath(root,pattern) |
| 19725 |
if collected then |
| 19726 |
if handle then |
| 19727 |
if reverse then |
| 19728 |
for c=#collected,1,-1 do |
| 19729 |
handle(collected[c]) |
| 19730 |
end |
| 19731 |
else |
| 19732 |
for c=1,#collected do |
| 19733 |
handle(collected[c]) |
| 19734 |
end |
| 19735 |
end |
| 19736 |
end |
| 19737 |
return collected |
| 19738 |
end |
| 19739 |
end |
| 19740 |
function xml.processattributes(root,pattern,handle) |
| 19741 |
local collected=xmlapplylpath(root,pattern) |
| 19742 |
if collected and handle then |
| 19743 |
for c=1,#collected do |
| 19744 |
handle(collected[c].at) |
| 19745 |
end |
| 19746 |
end |
| 19747 |
return collected |
| 19748 |
end |
| 19749 |
function xml.collect(root,pattern) |
| 19750 |
return xmlapplylpath(root,pattern) |
| 19751 |
end |
| 19752 |
function xml.collecttexts(root,pattern,flatten) |
| 19753 |
local collected=xmlapplylpath(root,pattern) |
| 19754 |
if collected and flatten then |
| 19755 |
local xmltostring=xml.tostring |
| 19756 |
for c=1,#collected do |
| 19757 |
collected[c]=xmltostring(collected[c].dt) |
| 19758 |
end |
| 19759 |
end |
| 19760 |
return collected or {} |
| 19761 |
end |
| 19762 |
function xml.collect_tags(root,pattern,nonamespace) |
| 19763 |
local collected=xmlapplylpath(root,pattern) |
| 19764 |
if collected then |
| 19765 |
local t={} |
| 19766 |
local n=0 |
| 19767 |
for c=1,#collected do |
| 19768 |
local e=collected[c] |
| 19769 |
local ns=e.ns |
| 19770 |
local tg=e.tg |
| 19771 |
n=n+1 |
| 19772 |
if nonamespace then |
| 19773 |
t[n]=tg |
| 19774 |
elseif ns=="" then |
| 19775 |
t[n]=tg |
| 19776 |
else |
| 19777 |
t[n]=ns..":"..tg |
| 19778 |
end |
| 19779 |
end |
| 19780 |
return t |
| 19781 |
end |
| 19782 |
end |
| 19783 |
local no_root={ no_root=true } |
| 19784 |
local function redo_ni(d) |
| 19785 |
for k=1,#d do |
| 19786 |
local dk=d[k] |
| 19787 |
if type(dk)=="table" then |
| 19788 |
dk.ni=k |
| 19789 |
end |
| 19790 |
end |
| 19791 |
end |
| 19792 |
xml.reindex=redo_ni |
| 19793 |
local function xmltoelement(whatever,root) |
| 19794 |
if not whatever then |
| 19795 |
return nil |
| 19796 |
end |
| 19797 |
local element |
| 19798 |
if type(whatever)=="string" then |
| 19799 |
element=xmlinheritedconvert(whatever,root,true) |
| 19800 |
else |
| 19801 |
element=whatever |
| 19802 |
end |
| 19803 |
if element.error then |
| 19804 |
return whatever |
| 19805 |
end |
| 19806 |
if element then |
| 19807 |
end |
| 19808 |
return element |
| 19809 |
end |
| 19810 |
xml.toelement=xmltoelement |
| 19811 |
local function copiedelement(element,newparent) |
| 19812 |
if type(element)~="string" then |
| 19813 |
element=xmlcopy(element).dt |
| 19814 |
if newparent and type(element)=="table" then |
| 19815 |
for i=1,#element do |
| 19816 |
local e=element[i] |
| 19817 |
if type(e)=="table" then |
| 19818 |
e.__p__=newparent |
| 19819 |
end |
| 19820 |
end |
| 19821 |
end |
| 19822 |
end |
| 19823 |
return element |
| 19824 |
end |
| 19825 |
function xml.delete(root,pattern) |
| 19826 |
if not pattern or pattern=="" then |
| 19827 |
local p=root.__p__ |
| 19828 |
if p then |
| 19829 |
if trace_manipulations then |
| 19830 |
report('deleting',"--",c,root) |
| 19831 |
end |
| 19832 |
local d=p.dt |
| 19833 |
remove(d,root.ni) |
| 19834 |
redo_ni(d) |
| 19835 |
end |
| 19836 |
else |
| 19837 |
local collected=xmlapplylpath(root,pattern) |
| 19838 |
if collected then |
| 19839 |
for c=1,#collected do |
| 19840 |
local e=collected[c] |
| 19841 |
local p=e.__p__ |
| 19842 |
if p then |
| 19843 |
if trace_manipulations then |
| 19844 |
report('deleting',pattern,c,e) |
| 19845 |
end |
| 19846 |
local d=p.dt |
| 19847 |
local ni=e.ni |
| 19848 |
if ni<=#d then |
| 19849 |
if false then |
| 19850 |
p.dt[ni]="" |
| 19851 |
else |
| 19852 |
remove(d,ni) |
| 19853 |
redo_ni(d) |
| 19854 |
end |
| 19855 |
else |
| 19856 |
end |
| 19857 |
end |
| 19858 |
end |
| 19859 |
end |
| 19860 |
end |
| 19861 |
end |
| 19862 |
function xml.wipe(root,pattern) |
| 19863 |
local collected=xmlapplylpath(root,pattern) |
| 19864 |
if collected then |
| 19865 |
for c=1,#collected do |
| 19866 |
local e=collected[c] |
| 19867 |
local p=e.__p__ |
| 19868 |
if p then |
| 19869 |
local d=p.dt |
| 19870 |
local ni=e.ni |
| 19871 |
if ni<=#d then |
| 19872 |
local dt=e.dt |
| 19873 |
if #dt==1 then |
| 19874 |
local d1=dt[1] |
| 19875 |
if type(d1)=="string" and match(d1,"^%s*$") then |
| 19876 |
if trace_manipulations then |
| 19877 |
report('wiping',pattern,c,e) |
| 19878 |
end |
| 19879 |
remove(d,ni) |
| 19880 |
redo_ni(d) |
| 19881 |
end |
| 19882 |
end |
| 19883 |
end |
| 19884 |
end |
| 19885 |
end |
| 19886 |
end |
| 19887 |
end |
| 19888 |
function xml.replace(root,pattern,whatever) |
| 19889 |
local element=root and xmltoelement(whatever,root) |
| 19890 |
local collected=element and xmlapplylpath(root,pattern) |
| 19891 |
if collected then |
| 19892 |
for c=1,#collected do |
| 19893 |
local e=collected[c] |
| 19894 |
local p=e.__p__ |
| 19895 |
if p then |
| 19896 |
if trace_manipulations then |
| 19897 |
report('replacing',pattern,c,e) |
| 19898 |
end |
| 19899 |
local d=p.dt |
| 19900 |
local n=e.ni |
| 19901 |
local t=copiedelement(element,p) |
| 19902 |
if type(t)=="table" then |
| 19903 |
d[n]=t[1] |
| 19904 |
for i=2,#t do |
| 19905 |
n=n+1 |
| 19906 |
insert(d,n,t[i]) |
| 19907 |
end |
| 19908 |
else |
| 19909 |
d[n]=t |
| 19910 |
end |
| 19911 |
redo_ni(d) |
| 19912 |
end |
| 19913 |
end |
| 19914 |
end |
| 19915 |
end |
| 19916 |
function xml.expand(root,pattern,whatever) |
| 19917 |
local collected=root and xmlapplylpath(root,pattern) |
| 19918 |
if collected then |
| 19919 |
for c=1,#collected do |
| 19920 |
local e=collected[c] |
| 19921 |
local p=e.__p__ |
| 19922 |
if p then |
| 19923 |
if trace_manipulations then |
| 19924 |
report('expanding',pattern,c,e) |
| 19925 |
end |
| 19926 |
local d=p.dt |
| 19927 |
local n=e.ni |
| 19928 |
local t=whatever(e,p) |
| 19929 |
if t then |
| 19930 |
if type(t)=="table" then |
| 19931 |
t=xmlcopy(t) |
| 19932 |
d[n]=t[1] |
| 19933 |
for i=2,#t do |
| 19934 |
n=n+1 |
| 19935 |
insert(d,n,t[i]) |
| 19936 |
end |
| 19937 |
else |
| 19938 |
d[n]=t |
| 19939 |
end |
| 19940 |
redo_ni(d) |
| 19941 |
end |
| 19942 |
end |
| 19943 |
end |
| 19944 |
end |
| 19945 |
end |
| 19946 |
local function wrap(e,wrapper) |
| 19947 |
local t={ |
| 19948 |
rn=e.rn, |
| 19949 |
tg=e.tg, |
| 19950 |
ns=e.ns, |
| 19951 |
at=e.at, |
| 19952 |
dt=e.dt, |
| 19953 |
__p__=e, |
| 19954 |
} |
| 19955 |
setmetatable(t,getmetatable(e)) |
| 19956 |
e.rn=wrapper.rn or e.rn or "" |
| 19957 |
e.tg=wrapper.tg or e.tg or "" |
| 19958 |
e.ns=wrapper.ns or e.ns or "" |
| 19959 |
e.at=fastcopy(wrapper.at) |
| 19960 |
e.dt={ t } |
| 19961 |
end |
| 19962 |
function xml.wrap(root,pattern,whatever) |
| 19963 |
if whatever then |
| 19964 |
local wrapper=xmltoelement(whatever,root) |
| 19965 |
local collected=xmlapplylpath(root,pattern) |
| 19966 |
if collected then |
| 19967 |
for c=1,#collected do |
| 19968 |
local e=collected[c] |
| 19969 |
if trace_manipulations then |
| 19970 |
report('wrapping',pattern,c,e) |
| 19971 |
end |
| 19972 |
wrap(e,wrapper) |
| 19973 |
end |
| 19974 |
end |
| 19975 |
else |
| 19976 |
wrap(root,xmltoelement(pattern)) |
| 19977 |
end |
| 19978 |
end |
| 19979 |
local function inject_element(root,pattern,whatever,prepend) |
| 19980 |
local element=root and xmltoelement(whatever,root) |
| 19981 |
local collected=element and xmlapplylpath(root,pattern) |
| 19982 |
local function inject_e(e) |
| 19983 |
local r=e.__p__ |
| 19984 |
local d=r.dt |
| 19985 |
local k=e.ni |
| 19986 |
local rri=r.ri |
| 19987 |
local edt=(rri and d[rri].dt) or (d and d[k] and d[k].dt) |
| 19988 |
if edt then |
| 19989 |
local be,af |
| 19990 |
local cp=copiedelement(element,e) |
| 19991 |
if prepend then |
| 19992 |
be,af=cp,edt |
| 19993 |
else |
| 19994 |
be,af=edt,cp |
| 19995 |
end |
| 19996 |
local bn=#be |
| 19997 |
for i=1,#af do |
| 19998 |
bn=bn+1 |
| 19999 |
be[bn]=af[i] |
| 20000 |
end |
| 20001 |
if rri then |
| 20002 |
r.dt[rri].dt=be |
| 20003 |
else |
| 20004 |
d[k].dt=be |
| 20005 |
end |
| 20006 |
redo_ni(d) |
| 20007 |
end |
| 20008 |
end |
| 20009 |
if not collected then |
| 20010 |
elseif collected.tg then |
| 20011 |
inject_e(collected) |
| 20012 |
else |
| 20013 |
for c=1,#collected do |
| 20014 |
inject_e(collected[c]) |
| 20015 |
end |
| 20016 |
end |
| 20017 |
end |
| 20018 |
local function insert_element(root,pattern,whatever,before) |
| 20019 |
local element=root and xmltoelement(whatever,root) |
| 20020 |
local collected=element and xmlapplylpath(root,pattern) |
| 20021 |
local function insert_e(e) |
| 20022 |
local r=e.__p__ |
| 20023 |
local d=r.dt |
| 20024 |
local k=e.ni |
| 20025 |
if not before then |
| 20026 |
k=k+1 |
| 20027 |
end |
| 20028 |
insert(d,k,copiedelement(element,r)) |
| 20029 |
redo_ni(d) |
| 20030 |
end |
| 20031 |
if not collected then |
| 20032 |
elseif collected.tg then |
| 20033 |
insert_e(collected) |
| 20034 |
else |
| 20035 |
for c=1,#collected do |
| 20036 |
insert_e(collected[c]) |
| 20037 |
end |
| 20038 |
end |
| 20039 |
end |
| 20040 |
xml.insert_element=insert_element |
| 20041 |
xml.insertafter=insert_element |
| 20042 |
xml.insertbefore=function(r,p,e) insert_element(r,p,e,true) end |
| 20043 |
xml.injectafter=inject_element |
| 20044 |
xml.injectbefore=function(r,p,e) inject_element(r,p,e,true) end |
| 20045 |
local function include(xmldata,pattern,attribute,recursive,loaddata,level) |
| 20046 |
pattern=pattern or 'include' |
| 20047 |
loaddata=loaddata or io.loaddata |
| 20048 |
local collected=xmlapplylpath(xmldata,pattern) |
| 20049 |
if collected then |
| 20050 |
if not level then |
| 20051 |
level=1 |
| 20052 |
end |
| 20053 |
for c=1,#collected do |
| 20054 |
local ek=collected[c] |
| 20055 |
local name=nil |
| 20056 |
local ekdt=ek.dt |
| 20057 |
if ekdt then |
| 20058 |
local ekat=ek.at |
| 20059 |
local ekrt=ek.__p__ |
| 20060 |
if ekrt then |
| 20061 |
local epdt=ekrt.dt |
| 20062 |
if not attribute or attribute=="" then |
| 20063 |
name=(type(ekdt)=="table" and ekdt[1]) or ekdt |
| 20064 |
end |
| 20065 |
if not name then |
| 20066 |
for a in gmatch(attribute or "href","([^|]+)") do |
| 20067 |
name=ekat[a] |
| 20068 |
if name then |
| 20069 |
break |
| 20070 |
end |
| 20071 |
end |
| 20072 |
end |
| 20073 |
local data=nil |
| 20074 |
if name and name~="" then |
| 20075 |
local d,n=loaddata(name) |
| 20076 |
data=d or "" |
| 20077 |
name=n or name |
| 20078 |
if trace_inclusions then |
| 20079 |
report_xml("including %s bytes from %a at level %s by pattern %a and attribute %a (%srecursing)",#data,name,level,pattern,attribute or "",recursive and "" or "not ") |
| 20080 |
end |
| 20081 |
end |
| 20082 |
if not data or data=="" then |
| 20083 |
epdt[ek.ni]="" |
| 20084 |
elseif ekat["parse"]=="text" then |
| 20085 |
epdt[ek.ni]=xml.escaped(data) |
| 20086 |
else |
| 20087 |
local settings=xmldata.settings |
| 20088 |
local savedresource=settings.currentresource |
| 20089 |
settings.currentresource=name |
| 20090 |
local xi=xmlinheritedconvert(data,xmldata,true) |
| 20091 |
if not xi then |
| 20092 |
epdt[ek.ni]="" |
| 20093 |
else |
| 20094 |
if recursive then |
| 20095 |
include(xi,pattern,attribute,recursive,loaddata,level+1) |
| 20096 |
end |
| 20097 |
local child=xml.body(xi) |
| 20098 |
child.__p__=ekrt |
| 20099 |
child.__f__=name |
| 20100 |
child.cf=name |
| 20101 |
epdt[ek.ni]=child |
| 20102 |
local settings=xmldata.settings |
| 20103 |
local inclusions=settings and settings.inclusions |
| 20104 |
if inclusions then |
| 20105 |
inclusions[#inclusions+1]=name |
| 20106 |
elseif settings then |
| 20107 |
settings.inclusions={ name } |
| 20108 |
else |
| 20109 |
settings={ inclusions={ name } } |
| 20110 |
xmldata.settings=settings |
| 20111 |
end |
| 20112 |
if child.er then |
| 20113 |
local badinclusions=settings.badinclusions |
| 20114 |
if badinclusions then |
| 20115 |
badinclusions[#badinclusions+1]=name |
| 20116 |
else |
| 20117 |
settings.badinclusions={ name } |
| 20118 |
end |
| 20119 |
end |
| 20120 |
end |
| 20121 |
settings.currentresource=savedresource |
| 20122 |
end |
| 20123 |
end |
| 20124 |
end |
| 20125 |
end |
| 20126 |
end |
| 20127 |
end |
| 20128 |
xml.include=include |
| 20129 |
function xml.inclusion(e,default) |
| 20130 |
while e do |
| 20131 |
local f=e.__f__ |
| 20132 |
if f then |
| 20133 |
return f |
| 20134 |
else |
| 20135 |
e=e.__p__ |
| 20136 |
end |
| 20137 |
end |
| 20138 |
return default |
| 20139 |
end |
| 20140 |
local function getinclusions(key,e,sorted) |
| 20141 |
while e do |
| 20142 |
local settings=e.settings |
| 20143 |
if settings then |
| 20144 |
local inclusions=settings[key] |
| 20145 |
if inclusions then |
| 20146 |
inclusions=table.unique(inclusions) |
| 20147 |
if sorted then |
| 20148 |
table.sort(inclusions) |
| 20149 |
end |
| 20150 |
return inclusions |
| 20151 |
else |
| 20152 |
e=e.__p__ |
| 20153 |
end |
| 20154 |
else |
| 20155 |
e=e.__p__ |
| 20156 |
end |
| 20157 |
end |
| 20158 |
end |
| 20159 |
function xml.inclusions(e,sorted) |
| 20160 |
return getinclusions("inclusions",e,sorted) |
| 20161 |
end |
| 20162 |
function xml.badinclusions(e,sorted) |
| 20163 |
return getinclusions("badinclusions",e,sorted) |
| 20164 |
end |
| 20165 |
local b_collapser=lpegpatterns.b_collapser |
| 20166 |
local m_collapser=lpegpatterns.m_collapser |
| 20167 |
local e_collapser=lpegpatterns.e_collapser |
| 20168 |
local x_collapser=lpegpatterns.x_collapser |
| 20169 |
local b_stripper=lpegpatterns.b_stripper |
| 20170 |
local m_stripper=lpegpatterns.m_stripper |
| 20171 |
local e_stripper=lpegpatterns.e_stripper |
| 20172 |
local x_stripper=lpegpatterns.x_stripper |
| 20173 |
local function stripelement(e,nolines,anywhere,everything) |
| 20174 |
local edt=e.dt |
| 20175 |
if edt then |
| 20176 |
local n=#edt |
| 20177 |
if n==0 then |
| 20178 |
return e |
| 20179 |
elseif everything then |
| 20180 |
local t={} |
| 20181 |
local m=0 |
| 20182 |
for i=1,n do |
| 20183 |
local str=edt[i] |
| 20184 |
if type(str)~="string" then |
| 20185 |
m=m+1 |
| 20186 |
t[m]=str |
| 20187 |
elseif str~="" then |
| 20188 |
str=lpegmatch(x_collapser,str) |
| 20189 |
if str~="" then |
| 20190 |
m=m+1 |
| 20191 |
t[m]=str |
| 20192 |
end |
| 20193 |
end |
| 20194 |
end |
| 20195 |
e.dt=t |
| 20196 |
elseif anywhere then |
| 20197 |
local t={} |
| 20198 |
local m=0 |
| 20199 |
for i=1,n do |
| 20200 |
local str=edt[i] |
| 20201 |
if type(str)~="string" then |
| 20202 |
m=m+1 |
| 20203 |
t[m]=str |
| 20204 |
elseif str~="" then |
| 20205 |
if nolines then |
| 20206 |
str=lpegmatch((i==1 and b_collapser) or (i==m and e_collapser) or m_collapser,str) |
| 20207 |
else |
| 20208 |
str=lpegmatch((i==1 and b_stripper) or (i==m and e_stripper) or m_stripper,str) |
| 20209 |
end |
| 20210 |
if str~="" then |
| 20211 |
m=m+1 |
| 20212 |
t[m]=str |
| 20213 |
end |
| 20214 |
end |
| 20215 |
end |
| 20216 |
e.dt=t |
| 20217 |
else |
| 20218 |
local str=edt[1] |
| 20219 |
if type(str)=="string" then |
| 20220 |
if str~="" then |
| 20221 |
str=lpegmatch(nolines and b_collapser or b_stripper,str) |
| 20222 |
end |
| 20223 |
if str=="" then |
| 20224 |
remove(edt,1) |
| 20225 |
n=n-1 |
| 20226 |
else |
| 20227 |
edt[1]=str |
| 20228 |
end |
| 20229 |
end |
| 20230 |
if n>0 then |
| 20231 |
str=edt[n] |
| 20232 |
if type(str)=="string" then |
| 20233 |
if str=="" then |
| 20234 |
remove(edt) |
| 20235 |
else |
| 20236 |
str=lpegmatch(nolines and e_collapser or e_stripper,str) |
| 20237 |
if str=="" then |
| 20238 |
remove(edt) |
| 20239 |
else |
| 20240 |
edt[n]=str |
| 20241 |
end |
| 20242 |
end |
| 20243 |
end |
| 20244 |
end |
| 20245 |
end |
| 20246 |
end |
| 20247 |
return e |
| 20248 |
end |
| 20249 |
xml.stripelement=stripelement |
| 20250 |
function xml.strip(root,pattern,nolines,anywhere,everything) |
| 20251 |
local collected=xmlapplylpath(root,pattern) |
| 20252 |
if collected then |
| 20253 |
for i=1,#collected do |
| 20254 |
stripelement(collected[i],nolines,anywhere,everything) |
| 20255 |
end |
| 20256 |
end |
| 20257 |
end |
| 20258 |
local function compactelement(e) |
| 20259 |
local edt=e.dt |
| 20260 |
if edt then |
| 20261 |
for e=1,#edt do |
| 20262 |
local str=edt[e] |
| 20263 |
if type(str)=="string" and not find(str,"%S") then |
| 20264 |
edt[e]="" |
| 20265 |
end |
| 20266 |
end |
| 20267 |
end |
| 20268 |
return e |
| 20269 |
end |
| 20270 |
xml.compactelement=compactelement |
| 20271 |
local function renamespace(root,oldspace,newspace) |
| 20272 |
local ndt=#root.dt |
| 20273 |
for i=1,ndt or 0 do |
| 20274 |
local e=root[i] |
| 20275 |
if type(e)=="table" then |
| 20276 |
if e.ns==oldspace then |
| 20277 |
e.ns=newspace |
| 20278 |
if e.rn then |
| 20279 |
e.rn=newspace |
| 20280 |
end |
| 20281 |
end |
| 20282 |
local edt=e.dt |
| 20283 |
if edt then |
| 20284 |
renamespace(edt,oldspace,newspace) |
| 20285 |
end |
| 20286 |
end |
| 20287 |
end |
| 20288 |
end |
| 20289 |
xml.renamespace=renamespace |
| 20290 |
function xml.remaptag(root,pattern,newtg) |
| 20291 |
local collected=xmlapplylpath(root,pattern) |
| 20292 |
if collected then |
| 20293 |
for c=1,#collected do |
| 20294 |
collected[c].tg=newtg |
| 20295 |
end |
| 20296 |
end |
| 20297 |
end |
| 20298 |
function xml.remapnamespace(root,pattern,newns) |
| 20299 |
local collected=xmlapplylpath(root,pattern) |
| 20300 |
if collected then |
| 20301 |
for c=1,#collected do |
| 20302 |
collected[c].ns=newns |
| 20303 |
end |
| 20304 |
end |
| 20305 |
end |
| 20306 |
function xml.checknamespace(root,pattern,newns) |
| 20307 |
local collected=xmlapplylpath(root,pattern) |
| 20308 |
if collected then |
| 20309 |
for c=1,#collected do |
| 20310 |
local e=collected[c] |
| 20311 |
if (not e.rn or e.rn=="") and e.ns=="" then |
| 20312 |
e.rn=newns |
| 20313 |
end |
| 20314 |
end |
| 20315 |
end |
| 20316 |
end |
| 20317 |
function xml.remapname(root,pattern,newtg,newns,newrn) |
| 20318 |
local collected=xmlapplylpath(root,pattern) |
| 20319 |
if collected then |
| 20320 |
for c=1,#collected do |
| 20321 |
local e=collected[c] |
| 20322 |
e.tg,e.ns,e.rn=newtg,newns,newrn |
| 20323 |
end |
| 20324 |
end |
| 20325 |
end |
| 20326 |
function xml.cdatatotext(e) |
| 20327 |
local dt=e.dt |
| 20328 |
if #dt==1 then |
| 20329 |
local first=dt[1] |
| 20330 |
if first.tg=="@cd@" then |
| 20331 |
e.dt=first.dt |
| 20332 |
end |
| 20333 |
else |
| 20334 |
end |
| 20335 |
end |
| 20336 |
function xml.texttocdata(e) |
| 20337 |
local dt=e.dt |
| 20338 |
local s=xml.tostring(dt) |
| 20339 |
e.tg="@cd@" |
| 20340 |
e.special=true |
| 20341 |
e.ns="" |
| 20342 |
e.rn="" |
| 20343 |
e.dt={ s } |
| 20344 |
e.at=nil |
| 20345 |
end |
| 20346 |
function xml.elementtocdata(e) |
| 20347 |
local dt=e.dt |
| 20348 |
local s=xml.tostring(e) |
| 20349 |
e.tg="@cd@" |
| 20350 |
e.special=true |
| 20351 |
e.ns="" |
| 20352 |
e.rn="" |
| 20353 |
e.dt={ s } |
| 20354 |
e.at=nil |
| 20355 |
end |
| 20356 |
xml.builtinentities=table.tohash { "amp","quot","apos","lt","gt" } |
| 20357 |
local entities=characters and characters.entities or nil |
| 20358 |
local builtinentities=xml.builtinentities |
| 20359 |
function xml.addentitiesdoctype(root,option) |
| 20360 |
if not entities then |
| 20361 |
require("char-ent") |
| 20362 |
entities=characters.entities |
| 20363 |
end |
| 20364 |
if entities and root and root.tg=="@rt@" and root.statistics then |
| 20365 |
local list={} |
| 20366 |
local hexify=option=="hexadecimal" |
| 20367 |
for k,v in table.sortedhash(root.statistics.entities.names) do |
| 20368 |
if not builtinentities[k] then |
| 20369 |
local e=entities[k] |
| 20370 |
if not e then |
| 20371 |
e=format("[%s]",k) |
| 20372 |
elseif hexify then |
| 20373 |
e=format("&#%05X;",utfbyte(k)) |
| 20374 |
end |
| 20375 |
list[#list+1]=format(" <!ENTITY %s %q >",k,e) |
| 20376 |
end |
| 20377 |
end |
| 20378 |
local dt=root.dt |
| 20379 |
local n=dt[1].tg=="@pi@" and 2 or 1 |
| 20380 |
if #list>0 then |
| 20381 |
insert(dt,n,{ "\n" }) |
| 20382 |
insert(dt,n,{ |
| 20383 |
tg="@dt@", |
| 20384 |
dt={ format("Something [\n%s\n] ",concat(list)) }, |
| 20385 |
ns="", |
| 20386 |
special=true, |
| 20387 |
}) |
| 20388 |
insert(dt,n,{ "\n\n" }) |
| 20389 |
else |
| 20390 |
end |
| 20391 |
end |
| 20392 |
end |
| 20393 |
xml.all=xml.each |
| 20394 |
xml.insert=xml.insertafter |
| 20395 |
xml.inject=xml.injectafter |
| 20396 |
xml.after=xml.insertafter |
| 20397 |
xml.before=xml.insertbefore |
| 20398 |
xml.process=xml.each |
| 20399 |
xml.obsolete=xml.obsolete or {} |
| 20400 |
local obsolete=xml.obsolete |
| 20401 |
xml.strip_whitespace=xml.strip obsolete.strip_whitespace=xml.strip |
| 20402 |
xml.collect_elements=xml.collect obsolete.collect_elements=xml.collect |
| 20403 |
xml.delete_element=xml.delete obsolete.delete_element=xml.delete |
| 20404 |
xml.replace_element=xml.replace obsolete.replace_element=xml.replace |
| 20405 |
xml.each_element=xml.each obsolete.each_element=xml.each |
| 20406 |
xml.process_elements=xml.process obsolete.process_elements=xml.process |
| 20407 |
xml.insert_element_after=xml.insertafter obsolete.insert_element_after=xml.insertafter |
| 20408 |
xml.insert_element_before=xml.insertbefore obsolete.insert_element_before=xml.insertbefore |
| 20409 |
xml.inject_element_after=xml.injectafter obsolete.inject_element_after=xml.injectafter |
| 20410 |
xml.inject_element_before=xml.injectbefore obsolete.inject_element_before=xml.injectbefore |
| 20411 |
xml.process_attributes=xml.processattributes obsolete.process_attributes=xml.processattributes |
| 20412 |
xml.collect_texts=xml.collecttexts obsolete.collect_texts=xml.collecttexts |
| 20413 |
xml.inject_element=xml.inject obsolete.inject_element=xml.inject |
| 20414 |
xml.remap_tag=xml.remaptag obsolete.remap_tag=xml.remaptag |
| 20415 |
xml.remap_name=xml.remapname obsolete.remap_name=xml.remapname |
| 20416 |
xml.remap_namespace=xml.remapnamespace obsolete.remap_namespace=xml.remapnamespace |
| 20417 |
function xml.cdata(e) |
| 20418 |
if e then |
| 20419 |
local dt=e.dt |
| 20420 |
if dt and #dt==1 then |
| 20421 |
local first=dt[1] |
| 20422 |
return first.tg=="@cd@" and first.dt[1] or "" |
| 20423 |
end |
| 20424 |
end |
| 20425 |
return "" |
| 20426 |
end |
| 20427 |
function xml.finalizers.xml.cdata(collected) |
| 20428 |
if collected then |
| 20429 |
local e=collected[1] |
| 20430 |
if e then |
| 20431 |
local dt=e.dt |
| 20432 |
if dt and #dt==1 then |
| 20433 |
local first=dt[1] |
| 20434 |
return first.tg=="@cd@" and first.dt[1] or "" |
| 20435 |
end |
| 20436 |
end |
| 20437 |
end |
| 20438 |
return "" |
| 20439 |
end |
| 20440 |
function xml.insertcomment(e,str,n) |
| 20441 |
insert(e.dt,n or 1,{ |
| 20442 |
tg="@cm@", |
| 20443 |
ns="", |
| 20444 |
special=true, |
| 20445 |
at={}, |
| 20446 |
dt={ str }, |
| 20447 |
}) |
| 20448 |
end |
| 20449 |
function xml.insertcdata(e,str,n) |
| 20450 |
insert(e.dt,n or 1,{ |
| 20451 |
tg="@cd@", |
| 20452 |
ns="", |
| 20453 |
special=true, |
| 20454 |
at={}, |
| 20455 |
dt={ str }, |
| 20456 |
}) |
| 20457 |
end |
| 20458 |
function xml.setcomment(e,str,n) |
| 20459 |
e.dt={ { |
| 20460 |
tg="@cm@", |
| 20461 |
ns="", |
| 20462 |
special=true, |
| 20463 |
at={}, |
| 20464 |
dt={ str }, |
| 20465 |
} } |
| 20466 |
end |
| 20467 |
function xml.setcdata(e,str) |
| 20468 |
e.dt={ { |
| 20469 |
tg="@cd@", |
| 20470 |
ns="", |
| 20471 |
special=true, |
| 20472 |
at={}, |
| 20473 |
dt={ str }, |
| 20474 |
} } |
| 20475 |
end |
| 20476 |
function xml.separate(x,pattern) |
| 20477 |
local collected=xmlapplylpath(x,pattern) |
| 20478 |
if collected then |
| 20479 |
for c=1,#collected do |
| 20480 |
local e=collected[c] |
| 20481 |
local d=e.dt |
| 20482 |
if d==x then |
| 20483 |
report_xml("warning: xml.separate changes root") |
| 20484 |
x=d |
| 20485 |
end |
| 20486 |
local t={ "\n" } |
| 20487 |
local n=1 |
| 20488 |
local i=1 |
| 20489 |
local nd=#d |
| 20490 |
while i<=nd do |
| 20491 |
while i<=nd do |
| 20492 |
local di=d[i] |
| 20493 |
if type(di)=="string" then |
| 20494 |
if di=="\n" or find(di,"^%s+$") then |
| 20495 |
i=i+1 |
| 20496 |
else |
| 20497 |
d[i]=strip(di) |
| 20498 |
break |
| 20499 |
end |
| 20500 |
else |
| 20501 |
break |
| 20502 |
end |
| 20503 |
end |
| 20504 |
if i>nd then |
| 20505 |
break |
| 20506 |
end |
| 20507 |
t[n+1]="\n" |
| 20508 |
t[n+2]=d[i] |
| 20509 |
t[n+3]="\n" |
| 20510 |
n=n+3 |
| 20511 |
i=i+1 |
| 20512 |
end |
| 20513 |
t[n+1]="\n" |
| 20514 |
setmetatable(t,getmetatable(d)) |
| 20515 |
e.dt=t |
| 20516 |
end |
| 20517 |
end |
| 20518 |
return x |
| 20519 |
end |
| 20520 |
local helpers=xml.helpers or {} |
| 20521 |
xml.helpers=helpers |
| 20522 |
local function normal(e,action) |
| 20523 |
local edt=e.dt |
| 20524 |
if edt then |
| 20525 |
for i=1,#edt do |
| 20526 |
local str=edt[i] |
| 20527 |
if type(str)=="string" and str~="" then |
| 20528 |
edt[i]=action(str) |
| 20529 |
end |
| 20530 |
end |
| 20531 |
end |
| 20532 |
end |
| 20533 |
local function recurse(e,action) |
| 20534 |
local edt=e.dt |
| 20535 |
if edt then |
| 20536 |
for i=1,#edt do |
| 20537 |
local str=edt[i] |
| 20538 |
if type(str)~="string" then |
| 20539 |
recurse(str,action) |
| 20540 |
elseif str~="" then |
| 20541 |
edt[i]=action(str) |
| 20542 |
end |
| 20543 |
end |
| 20544 |
end |
| 20545 |
end |
| 20546 |
function helpers.recursetext(collected,action,recursive) |
| 20547 |
if recursive then |
| 20548 |
for i=1,#collected do |
| 20549 |
recurse(collected[i],action) |
| 20550 |
end |
| 20551 |
else |
| 20552 |
for i=1,#collected do |
| 20553 |
normal(collected[i],action) |
| 20554 |
end |
| 20555 |
end |
| 20556 |
end |
| 20557 |
local specials={ |
| 20558 |
["@rt@"]="root", |
| 20559 |
["@pi@"]="instruction", |
| 20560 |
["@cm@"]="comment", |
| 20561 |
["@dt@"]="declaration", |
| 20562 |
["@cd@"]="cdata", |
| 20563 |
} |
| 20564 |
local function convert(x,strip,flat) |
| 20565 |
local ns=x.ns |
| 20566 |
local tg=x.tg |
| 20567 |
local at=x.at |
| 20568 |
local dt=x.dt |
| 20569 |
local node=flat and { |
| 20570 |
[0]=(not x.special and (ns~="" and ns..":"..tg or tg)) or nil, |
| 20571 |
} or { |
| 20572 |
_namespace=ns~="" and ns or nil, |
| 20573 |
_tag=not x.special and tg or nil, |
| 20574 |
_type=specials[tg] or "_element", |
| 20575 |
} |
| 20576 |
if at then |
| 20577 |
for k,v in next,at do |
| 20578 |
node[k]=v |
| 20579 |
end |
| 20580 |
end |
| 20581 |
local n=0 |
| 20582 |
for i=1,#dt do |
| 20583 |
local di=dt[i] |
| 20584 |
if type(di)=="table" then |
| 20585 |
if flat and di.special then |
| 20586 |
else |
| 20587 |
di=convert(di,strip,flat) |
| 20588 |
if di then |
| 20589 |
n=n+1 |
| 20590 |
node[n]=di |
| 20591 |
end |
| 20592 |
end |
| 20593 |
elseif strip then |
| 20594 |
di=lpegmatch(strip,di) |
| 20595 |
if di~="" then |
| 20596 |
n=n+1 |
| 20597 |
node[n]=di |
| 20598 |
end |
| 20599 |
else |
| 20600 |
n=n+1 |
| 20601 |
node[n]=di |
| 20602 |
end |
| 20603 |
end |
| 20604 |
if next(node) then |
| 20605 |
return node |
| 20606 |
end |
| 20607 |
end |
| 20608 |
function xml.totable(x,strip,flat) |
| 20609 |
if type(x)=="table" then |
| 20610 |
if strip then |
| 20611 |
strip=striplinepatterns[strip] |
| 20612 |
end |
| 20613 |
return convert(x,strip,flat) |
| 20614 |
end |
| 20615 |
end |
| 20616 |
function xml.rename(e,namespace,name,attributes) |
| 20617 |
if type(e)~="table" or not e.tg then |
| 20618 |
return |
| 20619 |
end |
| 20620 |
if type(name)=="table" then |
| 20621 |
attributes=name |
| 20622 |
name=namespace |
| 20623 |
namespace="" |
| 20624 |
elseif type(name)~="string" then |
| 20625 |
attributes={} |
| 20626 |
name=namespace |
| 20627 |
namespace="" |
| 20628 |
end |
| 20629 |
if type(attributes)~="table" then |
| 20630 |
attributes={} |
| 20631 |
end |
| 20632 |
e.ns=namespace |
| 20633 |
e.rn=namespace |
| 20634 |
e.tg=name |
| 20635 |
e.at=attributes |
| 20636 |
end |
| 20637 |
|
| 20638 |
|
| 20639 |
end -- of closure |
| 20640 |
|
| 20641 |
do -- create closure to overcome 200 locals limit |
| 20642 |
|
| 20643 |
package.loaded["lxml-xml"] = package.loaded["lxml-xml"] or true |
| 20644 |
|
| 20645 |
-- original size: 11118, stripped down to: 7721 |
| 20646 |
|
| 20647 |
if not modules then modules={} end modules ['lxml-xml']={ |
| 20648 |
version=1.001, |
| 20649 |
comment="this module is the basis for the lxml-* ones", |
| 20650 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 20651 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 20652 |
license="see context related readme files" |
| 20653 |
} |
| 20654 |
local tonumber,next=tonumber,next |
| 20655 |
local concat=table.concat |
| 20656 |
local find,lower,upper=string.find,string.lower,string.upper |
| 20657 |
local xml=xml |
| 20658 |
local finalizers=xml.finalizers.xml |
| 20659 |
local xmlfilter=xml.filter |
| 20660 |
local xmltostring=xml.tostring |
| 20661 |
local xmlserialize=xml.serialize |
| 20662 |
local xmlcollected=xml.collected |
| 20663 |
local xmlnewhandlers=xml.newhandlers |
| 20664 |
local reparsedentity=xml.reparsedentitylpeg |
| 20665 |
local unescapedentity=xml.unescapedentitylpeg |
| 20666 |
local parsedentity=reparsedentity |
| 20667 |
local function first(collected) |
| 20668 |
return collected and collected[1] |
| 20669 |
end |
| 20670 |
local function last(collected) |
| 20671 |
return collected and collected[#collected] |
| 20672 |
end |
| 20673 |
local function all(collected) |
| 20674 |
return collected |
| 20675 |
end |
| 20676 |
local reverse=table.reversed |
| 20677 |
local function attribute(collected,name) |
| 20678 |
if collected and #collected>0 then |
| 20679 |
local at=collected[1].at |
| 20680 |
return at and at[name] |
| 20681 |
end |
| 20682 |
end |
| 20683 |
local function att(id,name) |
| 20684 |
local at=id.at |
| 20685 |
return at and at[name] |
| 20686 |
end |
| 20687 |
local function count(collected) |
| 20688 |
return collected and #collected or 0 |
| 20689 |
end |
| 20690 |
local function position(collected,n) |
| 20691 |
if not collected then |
| 20692 |
return 0 |
| 20693 |
end |
| 20694 |
local nc=#collected |
| 20695 |
if nc==0 then |
| 20696 |
return 0 |
| 20697 |
end |
| 20698 |
n=tonumber(n) or 0 |
| 20699 |
if n<0 then |
| 20700 |
return collected[nc+n+1] |
| 20701 |
elseif n>0 then |
| 20702 |
return collected[n] |
| 20703 |
else |
| 20704 |
return collected[1].mi or 0 |
| 20705 |
end |
| 20706 |
end |
| 20707 |
local function match(collected) |
| 20708 |
return collected and #collected>0 and collected[1].mi or 0 |
| 20709 |
end |
| 20710 |
local function index(collected) |
| 20711 |
return collected and #collected>0 and collected[1].ni or 0 |
| 20712 |
end |
| 20713 |
local function attributes(collected,arguments) |
| 20714 |
if collected and #collected>0 then |
| 20715 |
local at=collected[1].at |
| 20716 |
if arguments then |
| 20717 |
return at[arguments] |
| 20718 |
elseif next(at) then |
| 20719 |
return at |
| 20720 |
end |
| 20721 |
end |
| 20722 |
end |
| 20723 |
local function chainattribute(collected,arguments) |
| 20724 |
if collected and #collected>0 then |
| 20725 |
local e=collected[1] |
| 20726 |
while e do |
| 20727 |
local at=e.at |
| 20728 |
if at then |
| 20729 |
local a=at[arguments] |
| 20730 |
if a then |
| 20731 |
return a |
| 20732 |
end |
| 20733 |
else |
| 20734 |
break |
| 20735 |
end |
| 20736 |
e=e.__p__ |
| 20737 |
end |
| 20738 |
end |
| 20739 |
return "" |
| 20740 |
end |
| 20741 |
local function raw(collected) |
| 20742 |
if collected and #collected>0 then |
| 20743 |
local e=collected[1] or collected |
| 20744 |
return e and xmltostring(e) or "" |
| 20745 |
else |
| 20746 |
return "" |
| 20747 |
end |
| 20748 |
end |
| 20749 |
local result=false |
| 20750 |
local xmltexthandler=xmlnewhandlers { |
| 20751 |
name="string", |
| 20752 |
initialize=function() |
| 20753 |
result={} |
| 20754 |
return result |
| 20755 |
end, |
| 20756 |
finalize=function() |
| 20757 |
return concat(result) |
| 20758 |
end, |
| 20759 |
handle=function(...) |
| 20760 |
result[#result+1]=concat {... } |
| 20761 |
end, |
| 20762 |
escape=false, |
| 20763 |
} |
| 20764 |
local function xmltotext(root) |
| 20765 |
local dt=root.dt |
| 20766 |
if not dt then |
| 20767 |
return "" |
| 20768 |
end |
| 20769 |
local nt=#dt |
| 20770 |
if nt==0 then |
| 20771 |
return "" |
| 20772 |
elseif nt==1 and type(dt[1])=="string" then |
| 20773 |
return dt[1] |
| 20774 |
else |
| 20775 |
return xmlserialize(root,xmltexthandler) or "" |
| 20776 |
end |
| 20777 |
end |
| 20778 |
function xml.serializetotext(root) |
| 20779 |
return root and xmlserialize(root,xmltexthandler) or "" |
| 20780 |
end |
| 20781 |
local function text(collected) |
| 20782 |
if collected then |
| 20783 |
local e=collected[1] or collected |
| 20784 |
return e and xmltotext(e) or "" |
| 20785 |
else |
| 20786 |
return "" |
| 20787 |
end |
| 20788 |
end |
| 20789 |
local function texts(collected) |
| 20790 |
if not collected then |
| 20791 |
return {} |
| 20792 |
end |
| 20793 |
local nc=#collected |
| 20794 |
if nc==0 then |
| 20795 |
return {} |
| 20796 |
end |
| 20797 |
local t,n={},0 |
| 20798 |
for c=1,nc do |
| 20799 |
local e=collected[c] |
| 20800 |
if e and e.dt then |
| 20801 |
n=n+1 |
| 20802 |
t[n]=e.dt |
| 20803 |
end |
| 20804 |
end |
| 20805 |
return t |
| 20806 |
end |
| 20807 |
local function tag(collected,n) |
| 20808 |
if not collected then |
| 20809 |
return |
| 20810 |
end |
| 20811 |
local nc=#collected |
| 20812 |
if nc==0 then |
| 20813 |
return |
| 20814 |
end |
| 20815 |
local c |
| 20816 |
if n==0 or not n then |
| 20817 |
c=collected[1] |
| 20818 |
elseif n>1 then |
| 20819 |
c=collected[n] |
| 20820 |
else |
| 20821 |
c=collected[nc-n+1] |
| 20822 |
end |
| 20823 |
return c and c.tg |
| 20824 |
end |
| 20825 |
local function name(collected,n) |
| 20826 |
if not collected then |
| 20827 |
return |
| 20828 |
end |
| 20829 |
local nc=#collected |
| 20830 |
if nc==0 then |
| 20831 |
return |
| 20832 |
end |
| 20833 |
local c |
| 20834 |
if n==0 or not n then |
| 20835 |
c=collected[1] |
| 20836 |
elseif n>1 then |
| 20837 |
c=collected[n] |
| 20838 |
else |
| 20839 |
c=collected[nc-n+1] |
| 20840 |
end |
| 20841 |
if not c then |
| 20842 |
elseif c.ns=="" then |
| 20843 |
return c.tg |
| 20844 |
else |
| 20845 |
return c.ns..":"..c.tg |
| 20846 |
end |
| 20847 |
end |
| 20848 |
local function tags(collected,nonamespace) |
| 20849 |
if not collected then |
| 20850 |
return |
| 20851 |
end |
| 20852 |
local nc=#collected |
| 20853 |
if nc==0 then |
| 20854 |
return |
| 20855 |
end |
| 20856 |
local t,n={},0 |
| 20857 |
for c=1,nc do |
| 20858 |
local e=collected[c] |
| 20859 |
local ns,tg=e.ns,e.tg |
| 20860 |
n=n+1 |
| 20861 |
if nonamespace or ns=="" then |
| 20862 |
t[n]=tg |
| 20863 |
else |
| 20864 |
t[n]=ns..":"..tg |
| 20865 |
end |
| 20866 |
end |
| 20867 |
return t |
| 20868 |
end |
| 20869 |
local function empty(collected,spacesonly) |
| 20870 |
if not collected then |
| 20871 |
return true |
| 20872 |
end |
| 20873 |
local nc=#collected |
| 20874 |
if nc==0 then |
| 20875 |
return true |
| 20876 |
end |
| 20877 |
for c=1,nc do |
| 20878 |
local e=collected[c] |
| 20879 |
if e then |
| 20880 |
local edt=e.dt |
| 20881 |
if edt then |
| 20882 |
local n=#edt |
| 20883 |
if n==1 then |
| 20884 |
local edk=edt[1] |
| 20885 |
local typ=type(edk) |
| 20886 |
if typ=="table" then |
| 20887 |
return false |
| 20888 |
elseif edk~="" then |
| 20889 |
return false |
| 20890 |
elseif spacesonly and not find(edk,"%S") then |
| 20891 |
return false |
| 20892 |
end |
| 20893 |
elseif n>1 then |
| 20894 |
return false |
| 20895 |
end |
| 20896 |
end |
| 20897 |
end |
| 20898 |
end |
| 20899 |
return true |
| 20900 |
end |
| 20901 |
finalizers.first=first |
| 20902 |
finalizers.last=last |
| 20903 |
finalizers.all=all |
| 20904 |
finalizers.reverse=reverse |
| 20905 |
finalizers.elements=all |
| 20906 |
finalizers.default=all |
| 20907 |
finalizers.attribute=attribute |
| 20908 |
finalizers.att=att |
| 20909 |
finalizers.count=count |
| 20910 |
finalizers.position=position |
| 20911 |
finalizers.match=match |
| 20912 |
finalizers.index=index |
| 20913 |
finalizers.attributes=attributes |
| 20914 |
finalizers.chainattribute=chainattribute |
| 20915 |
finalizers.text=text |
| 20916 |
finalizers.texts=texts |
| 20917 |
finalizers.tag=tag |
| 20918 |
finalizers.name=name |
| 20919 |
finalizers.tags=tags |
| 20920 |
finalizers.empty=empty |
| 20921 |
function xml.first(id,pattern) |
| 20922 |
return first(xmlfilter(id,pattern)) |
| 20923 |
end |
| 20924 |
function xml.last(id,pattern) |
| 20925 |
return last(xmlfilter(id,pattern)) |
| 20926 |
end |
| 20927 |
function xml.count(id,pattern) |
| 20928 |
return count(xmlfilter(id,pattern)) |
| 20929 |
end |
| 20930 |
function xml.attribute(id,pattern,a,default) |
| 20931 |
return attribute(xmlfilter(id,pattern),a,default) |
| 20932 |
end |
| 20933 |
function xml.raw(id,pattern) |
| 20934 |
if pattern then |
| 20935 |
return raw(xmlfilter(id,pattern)) |
| 20936 |
else |
| 20937 |
return raw(id) |
| 20938 |
end |
| 20939 |
end |
| 20940 |
function xml.text(id,pattern) |
| 20941 |
if pattern then |
| 20942 |
local collected=xmlfilter(id,pattern) |
| 20943 |
return collected and #collected>0 and xmltotext(collected[1]) or "" |
| 20944 |
elseif id then |
| 20945 |
return xmltotext(id) or "" |
| 20946 |
else |
| 20947 |
return "" |
| 20948 |
end |
| 20949 |
end |
| 20950 |
function xml.pure(id,pattern) |
| 20951 |
if pattern then |
| 20952 |
local collected=xmlfilter(id,pattern) |
| 20953 |
if collected and #collected>0 then |
| 20954 |
parsedentity=unescapedentity |
| 20955 |
local s=collected and #collected>0 and xmltotext(collected[1]) or "" |
| 20956 |
parsedentity=reparsedentity |
| 20957 |
return s |
| 20958 |
else |
| 20959 |
return "" |
| 20960 |
end |
| 20961 |
else |
| 20962 |
parsedentity=unescapedentity |
| 20963 |
local s=xmltotext(id) or "" |
| 20964 |
parsedentity=reparsedentity |
| 20965 |
return s |
| 20966 |
end |
| 20967 |
end |
| 20968 |
xml.content=text |
| 20969 |
function xml.position(id,pattern,n) |
| 20970 |
return position(xmlfilter(id,pattern),n) |
| 20971 |
end |
| 20972 |
function xml.match(id,pattern) |
| 20973 |
return match(xmlfilter(id,pattern)) |
| 20974 |
end |
| 20975 |
function xml.empty(id,pattern,spacesonly) |
| 20976 |
return empty(xmlfilter(id,pattern),spacesonly) |
| 20977 |
end |
| 20978 |
xml.all=xml.filter |
| 20979 |
xml.index=xml.position |
| 20980 |
xml.found=xml.filter |
| 20981 |
local function totable(x) |
| 20982 |
local t={} |
| 20983 |
for e in xmlcollected(x[1] or x,"/*") do |
| 20984 |
t[e.tg]=xmltostring(e.dt) or "" |
| 20985 |
end |
| 20986 |
return next(t) and t or nil |
| 20987 |
end |
| 20988 |
xml.table=totable |
| 20989 |
finalizers.table=totable |
| 20990 |
local function textonly(e,t) |
| 20991 |
if e then |
| 20992 |
local edt=e.dt |
| 20993 |
if edt then |
| 20994 |
for i=1,#edt do |
| 20995 |
local e=edt[i] |
| 20996 |
if type(e)=="table" then |
| 20997 |
textonly(e,t) |
| 20998 |
else |
| 20999 |
t[#t+1]=e |
| 21000 |
end |
| 21001 |
end |
| 21002 |
end |
| 21003 |
end |
| 21004 |
return t |
| 21005 |
end |
| 21006 |
function xml.textonly(e) |
| 21007 |
return concat(textonly(e,{})) |
| 21008 |
end |
| 21009 |
function finalizers.lowerall(collected) |
| 21010 |
for c=1,#collected do |
| 21011 |
local e=collected[c] |
| 21012 |
if not e.special then |
| 21013 |
e.tg=lower(e.tg) |
| 21014 |
local eat=e.at |
| 21015 |
if eat then |
| 21016 |
local t={} |
| 21017 |
for k,v in next,eat do |
| 21018 |
t[lower(k)]=v |
| 21019 |
end |
| 21020 |
e.at=t |
| 21021 |
end |
| 21022 |
end |
| 21023 |
end |
| 21024 |
end |
| 21025 |
function finalizers.upperall(collected) |
| 21026 |
for c=1,#collected do |
| 21027 |
local e=collected[c] |
| 21028 |
if not e.special then |
| 21029 |
e.tg=upper(e.tg) |
| 21030 |
local eat=e.at |
| 21031 |
if eat then |
| 21032 |
local t={} |
| 21033 |
for k,v in next,eat do |
| 21034 |
t[upper(k)]=v |
| 21035 |
end |
| 21036 |
e.at=t |
| 21037 |
end |
| 21038 |
end |
| 21039 |
end |
| 21040 |
end |
| 21041 |
|
| 21042 |
|
| 21043 |
end -- of closure |
| 21044 |
|
| 21045 |
do -- create closure to overcome 200 locals limit |
| 21046 |
|
| 21047 |
package.loaded["trac-xml"] = package.loaded["trac-xml"] or true |
| 21048 |
|
| 21049 |
-- original size: 6407, stripped down to: 4640 |
| 21050 |
|
| 21051 |
if not modules then modules={} end modules ['trac-xml']={ |
| 21052 |
version=1.001, |
| 21053 |
comment="companion to trac-log.mkiv", |
| 21054 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 21055 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 21056 |
license="see context related readme files" |
| 21057 |
} |
| 21058 |
local formatters=string.formatters |
| 21059 |
local reporters=logs.reporters |
| 21060 |
local xmlserialize=xml.serialize |
| 21061 |
local xmlcollected=xml.collected |
| 21062 |
local xmltext=xml.text |
| 21063 |
local xmlfirst=xml.first |
| 21064 |
local function showhelp(specification,...) |
| 21065 |
local root=xml.convert(specification.helpinfo or "") |
| 21066 |
if not root then |
| 21067 |
return |
| 21068 |
end |
| 21069 |
local xs=xml.gethandlers("string") |
| 21070 |
xml.sethandlersfunction(xs,"short",function(e,handler) xmlserialize(e.dt,handler) end) |
| 21071 |
xml.sethandlersfunction(xs,"ref",function(e,handler) handler.handle("--"..e.at.name) end) |
| 21072 |
local wantedcategories=select("#",...)==0 and true or table.tohash {... } |
| 21073 |
local nofcategories=xml.count(root,"/application/flags/category") |
| 21074 |
local report=specification.report |
| 21075 |
for category in xmlcollected(root,"/application/flags/category") do |
| 21076 |
local categoryname=category.at.name or "" |
| 21077 |
if wantedcategories==true or wantedcategories[categoryname] then |
| 21078 |
if nofcategories>1 then |
| 21079 |
report("%s options:",categoryname) |
| 21080 |
report() |
| 21081 |
end |
| 21082 |
for subcategory in xmlcollected(category,"/subcategory") do |
| 21083 |
for flag in xmlcollected(subcategory,"/flag") do |
| 21084 |
local name=flag.at.name |
| 21085 |
local value=flag.at.value |
| 21086 |
local short=xmltext(xmlfirst(flag,"/short")) |
| 21087 |
if value then |
| 21088 |
report("--%-20s %s",formatters["%s=%s"](name,value),short) |
| 21089 |
else |
| 21090 |
report("--%-20s %s",name,short) |
| 21091 |
end |
| 21092 |
end |
| 21093 |
report() |
| 21094 |
end |
| 21095 |
end |
| 21096 |
end |
| 21097 |
for category in xmlcollected(root,"/application/examples/category") do |
| 21098 |
local title=xmltext(xmlfirst(category,"/title")) |
| 21099 |
if title and title~="" then |
| 21100 |
report() |
| 21101 |
report(title) |
| 21102 |
report() |
| 21103 |
end |
| 21104 |
for subcategory in xmlcollected(category,"/subcategory") do |
| 21105 |
for example in xmlcollected(subcategory,"/example") do |
| 21106 |
local command=xmltext(xmlfirst(example,"/command")) |
| 21107 |
local comment=xmltext(xmlfirst(example,"/comment")) |
| 21108 |
report(command) |
| 21109 |
end |
| 21110 |
report() |
| 21111 |
end |
| 21112 |
end |
| 21113 |
for comment in xmlcollected(root,"/application/comments/comment") do |
| 21114 |
local comment=xmltext(comment) |
| 21115 |
report() |
| 21116 |
report(comment) |
| 21117 |
report() |
| 21118 |
end |
| 21119 |
end |
| 21120 |
local reporthelp=reporters.help |
| 21121 |
local exporthelp=reporters.export |
| 21122 |
local function xmlfound(t) |
| 21123 |
local helpinfo=t.helpinfo |
| 21124 |
if type(helpinfo)=="table" then |
| 21125 |
return false |
| 21126 |
end |
| 21127 |
if type(helpinfo)~="string" then |
| 21128 |
helpinfo="Warning: no helpinfo found." |
| 21129 |
t.helpinfo=helpinfo |
| 21130 |
return false |
| 21131 |
end |
| 21132 |
if string.find(helpinfo,".xml$") then |
| 21133 |
local ownscript=environment.ownscript |
| 21134 |
local helpdata=false |
| 21135 |
if ownscript then |
| 21136 |
local helpfile=file.join(file.pathpart(ownscript),helpinfo) |
| 21137 |
helpdata=io.loaddata(helpfile) |
| 21138 |
if helpdata=="" then |
| 21139 |
helpdata=false |
| 21140 |
end |
| 21141 |
end |
| 21142 |
if not helpdata then |
| 21143 |
local helpfile=resolvers.findfile(helpinfo,"tex") |
| 21144 |
helpdata=helpfile and io.loaddata(helpfile) |
| 21145 |
end |
| 21146 |
if helpdata and helpdata~="" then |
| 21147 |
helpinfo=helpdata |
| 21148 |
else |
| 21149 |
helpinfo=formatters["Warning: help file %a is not found."](helpinfo) |
| 21150 |
end |
| 21151 |
end |
| 21152 |
t.helpinfo=helpinfo |
| 21153 |
return string.find(t.helpinfo,"^<%?xml") and true or false |
| 21154 |
end |
| 21155 |
function reporters.help(t,...) |
| 21156 |
if xmlfound(t) then |
| 21157 |
showhelp(t,...) |
| 21158 |
else |
| 21159 |
reporthelp(t,...) |
| 21160 |
end |
| 21161 |
end |
| 21162 |
function reporters.export(t,methods,filename) |
| 21163 |
if not xmlfound(t) then |
| 21164 |
return exporthelp(t) |
| 21165 |
end |
| 21166 |
if not methods or methods=="" then |
| 21167 |
methods=environment.arguments["exporthelp"] |
| 21168 |
end |
| 21169 |
if not filename or filename=="" then |
| 21170 |
filename=environment.files[1] |
| 21171 |
end |
| 21172 |
dofile(resolvers.findfile("trac-exp.lua","tex")) |
| 21173 |
local exporters=logs.exporters |
| 21174 |
if not exporters or not methods then |
| 21175 |
return exporthelp(t) |
| 21176 |
end |
| 21177 |
if methods=="all" then |
| 21178 |
methods=table.keys(exporters) |
| 21179 |
elseif type(methods)=="string" then |
| 21180 |
methods=utilities.parsers.settings_to_array(methods) |
| 21181 |
else |
| 21182 |
return exporthelp(t) |
| 21183 |
end |
| 21184 |
if type(filename)~="string" or filename=="" then |
| 21185 |
filename=false |
| 21186 |
elseif file.pathpart(filename)=="" then |
| 21187 |
t.report("export file %a will not be saved on the current path (safeguard)",filename) |
| 21188 |
return |
| 21189 |
end |
| 21190 |
for i=1,#methods do |
| 21191 |
local method=methods[i] |
| 21192 |
local exporter=exporters[method] |
| 21193 |
if exporter then |
| 21194 |
local result=exporter(t,method) |
| 21195 |
if result and result~="" then |
| 21196 |
if filename then |
| 21197 |
local fullname=file.replacesuffix(filename,method) |
| 21198 |
t.report("saving export in %a",fullname) |
| 21199 |
dir.mkdirs(file.pathpart(fullname)) |
| 21200 |
io.savedata(fullname,result) |
| 21201 |
else |
| 21202 |
reporters.lines(t,result) |
| 21203 |
end |
| 21204 |
else |
| 21205 |
t.report("no output from exporter %a",method) |
| 21206 |
end |
| 21207 |
else |
| 21208 |
t.report("unknown exporter %a",method) |
| 21209 |
end |
| 21210 |
end |
| 21211 |
end |
| 21212 |
|
| 21213 |
|
| 21214 |
end -- of closure |
| 21215 |
|
| 21216 |
do -- create closure to overcome 200 locals limit |
| 21217 |
|
| 21218 |
package.loaded["data-ini"] = package.loaded["data-ini"] or true |
| 21219 |
|
| 21220 |
-- original size: 10636, stripped down to: 7049 |
| 21221 |
|
| 21222 |
if not modules then modules={} end modules ['data-ini']={ |
| 21223 |
version=1.001, |
| 21224 |
comment="companion to luat-lib.mkiv", |
| 21225 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 21226 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 21227 |
license="see context related readme files", |
| 21228 |
} |
| 21229 |
local next,type,getmetatable,rawset=next,type,getmetatable,rawset |
| 21230 |
local gsub,find,gmatch,char=string.gsub,string.find,string.gmatch,string.char |
| 21231 |
local filedirname,filebasename,filejoin,replacesuffix=file.dirname,file.basename,file.join,file.replacesuffix |
| 21232 |
local ostype,osname,osuname,ossetenv,osgetenv=os.type,os.name,os.uname,os.setenv,os.getenv |
| 21233 |
local sortedpairs=table.sortedpairs |
| 21234 |
local isfile,currentdir=lfs.isfile,lfs.currentdir |
| 21235 |
local expandlink=dir.expandlink |
| 21236 |
local P,S,R,C,Cs,Cc,lpegmatch=lpeg.P,lpeg.S,lpeg.R,lpeg.C,lpeg.Cs,lpeg.Cc,lpeg.match |
| 21237 |
local trace_locating=false trackers.register("resolvers.locating",function(v) trace_locating=v end) |
| 21238 |
local trace_expansions=false trackers.register("resolvers.expansions",function(v) trace_expansions=v end) |
| 21239 |
local report_initialization=logs.reporter("resolvers","initialization") |
| 21240 |
resolvers=resolvers or {} |
| 21241 |
local resolvers=resolvers |
| 21242 |
texconfig.kpse_init=false |
| 21243 |
texconfig.shell_escape='t' |
| 21244 |
if not (environment and environment.default_texmfcnf) and kpse and kpse.default_texmfcnf then |
| 21245 |
local default_texmfcnf=kpse.default_texmfcnf() |
| 21246 |
default_texmfcnf=gsub(default_texmfcnf,"$SELFAUTOLOC","selfautoloc:") |
| 21247 |
default_texmfcnf=gsub(default_texmfcnf,"$SELFAUTODIR","selfautodir:") |
| 21248 |
default_texmfcnf=gsub(default_texmfcnf,"$SELFAUTOPARENT","selfautoparent:") |
| 21249 |
default_texmfcnf=gsub(default_texmfcnf,"$HOME","home:") |
| 21250 |
environment.default_texmfcnf=default_texmfcnf |
| 21251 |
end |
| 21252 |
kpse={ original=kpse } |
| 21253 |
setmetatable(kpse,{ |
| 21254 |
__index=function(kp,name) |
| 21255 |
report_initialization("fatal error: kpse library is accessed (key: %s)",name) |
| 21256 |
os.exit() |
| 21257 |
end |
| 21258 |
} ) |
| 21259 |
do |
| 21260 |
local osfontdir=osgetenv("OSFONTDIR") |
| 21261 |
if osfontdir and osfontdir~="" then |
| 21262 |
elseif osname=="windows" then |
| 21263 |
ossetenv("OSFONTDIR","c:/windows/fonts//") |
| 21264 |
elseif osname=="macosx" then |
| 21265 |
ossetenv("OSFONTDIR","$HOME/Library/Fonts//;/Library/Fonts//;/System/Library/Fonts//") |
| 21266 |
end |
| 21267 |
end |
| 21268 |
if not environment.homedir then |
| 21269 |
local oldhome=osgetenv('HOME') |
| 21270 |
local homedir=osgetenv(ostype=="windows" and 'USERPROFILE' or 'HOME') or '' |
| 21271 |
if not homedir or homedir=="" then |
| 21272 |
homedir=char(127) |
| 21273 |
end |
| 21274 |
homedir=file.collapsepath(homedir) |
| 21275 |
ossetenv("HOME",homedir) |
| 21276 |
ossetenv("USERPROFILE",homedir) |
| 21277 |
environment.oldhome=oldhome |
| 21278 |
environment.homedir=homedir |
| 21279 |
end |
| 21280 |
do |
| 21281 |
local args=environment.originalarguments or arg |
| 21282 |
if not environment.ownmain then |
| 21283 |
environment.ownmain=status and string.match(string.lower(status.banner),"this is ([%a]+)") or "luatex" |
| 21284 |
end |
| 21285 |
local ownbin=environment.ownbin or os.selfbin or args[-2] or arg[-2] or args[-1] or arg[-1] or arg[0] or "luametatex" |
| 21286 |
local ownpath=environment.ownpath or os.selfdir |
| 21287 |
ownbin=file.collapsepath(ownbin) |
| 21288 |
ownpath=file.collapsepath(ownpath) |
| 21289 |
ownpath=expandlink(ownpath,trace_locating and report_initialization) |
| 21290 |
if not ownpath or ownpath=="" or ownpath=="unset" then |
| 21291 |
ownpath=args[-1] or arg[-1] |
| 21292 |
ownpath=ownpath and filedirname(gsub(ownpath,"\\","/")) |
| 21293 |
if not ownpath or ownpath=="" then |
| 21294 |
ownpath=args[-0] or arg[-0] |
| 21295 |
ownpath=ownpath and filedirname(gsub(ownpath,"\\","/")) |
| 21296 |
end |
| 21297 |
local binary=ownbin |
| 21298 |
if not ownpath or ownpath=="" then |
| 21299 |
ownpath=ownpath and filedirname(binary) |
| 21300 |
end |
| 21301 |
if not ownpath or ownpath=="" then |
| 21302 |
if os.binsuffix~="" then |
| 21303 |
binary=replacesuffix(binary,os.binsuffix) |
| 21304 |
end |
| 21305 |
local path=osgetenv("PATH") |
| 21306 |
if path then |
| 21307 |
for p in gmatch(path,"[^"..io.pathseparator.."]+") do |
| 21308 |
local b=filejoin(p,binary) |
| 21309 |
if isfile(b) then |
| 21310 |
ownpath=expandlink(p,trace_locating and report_initialization) |
| 21311 |
break |
| 21312 |
end |
| 21313 |
end |
| 21314 |
end |
| 21315 |
end |
| 21316 |
if not ownpath or ownpath=="" then |
| 21317 |
ownpath="." |
| 21318 |
report_initialization("forcing fallback to ownpath %a",ownpath) |
| 21319 |
elseif trace_locating then |
| 21320 |
report_initialization("using ownpath %a",ownpath) |
| 21321 |
end |
| 21322 |
end |
| 21323 |
environment.ownbin=ownbin |
| 21324 |
environment.ownpath=ownpath |
| 21325 |
end |
| 21326 |
resolvers.ownpath=environment.ownpath |
| 21327 |
function resolvers.getownpath() |
| 21328 |
return environment.ownpath |
| 21329 |
end |
| 21330 |
do |
| 21331 |
local ownpath=environment.ownpath or dir.current() |
| 21332 |
if ownpath then |
| 21333 |
ossetenv('SELFAUTOLOC',file.collapsepath(ownpath)) |
| 21334 |
ossetenv('SELFAUTODIR',file.collapsepath(ownpath.."/..")) |
| 21335 |
ossetenv('SELFAUTOPARENT',file.collapsepath(ownpath.."/../..")) |
| 21336 |
else |
| 21337 |
report_initialization("error: unable to locate ownpath") |
| 21338 |
os.exit() |
| 21339 |
end |
| 21340 |
end |
| 21341 |
local texos=environment.texos or osgetenv("TEXOS") |
| 21342 |
local texmfos=environment.texmfos or osgetenv('SELFAUTODIR') |
| 21343 |
if not texos or texos=="" then |
| 21344 |
texos=file.basename(texmfos) |
| 21345 |
end |
| 21346 |
ossetenv('TEXMFOS',texmfos) |
| 21347 |
ossetenv('TEXOS',texos) |
| 21348 |
ossetenv('SELFAUTOSYSTEM',os.platform) |
| 21349 |
environment.texos=texos |
| 21350 |
environment.texmfos=texmfos |
| 21351 |
local texroot=environment.texroot or osgetenv("TEXROOT") |
| 21352 |
if not texroot or texroot=="" then |
| 21353 |
texroot=osgetenv('SELFAUTOPARENT') |
| 21354 |
ossetenv('TEXROOT',texroot) |
| 21355 |
end |
| 21356 |
environment.texroot=file.collapsepath(texroot) |
| 21357 |
local prefixes=utilities.storage.allocate() |
| 21358 |
resolvers.prefixes=prefixes |
| 21359 |
local resolved={} |
| 21360 |
local abstract={} |
| 21361 |
local dynamic={} |
| 21362 |
function resolvers.resetresolve(str) |
| 21363 |
resolved,abstract={},{} |
| 21364 |
end |
| 21365 |
function resolvers.allprefixes(separator) |
| 21366 |
local all=table.sortedkeys(prefixes) |
| 21367 |
if separator then |
| 21368 |
for i=1,#all do |
| 21369 |
all[i]=all[i]..":" |
| 21370 |
end |
| 21371 |
end |
| 21372 |
return all |
| 21373 |
end |
| 21374 |
local function _resolve_(method,target) |
| 21375 |
local action=prefixes[method] |
| 21376 |
if action then |
| 21377 |
return action(target) |
| 21378 |
else |
| 21379 |
return method..":"..target |
| 21380 |
end |
| 21381 |
end |
| 21382 |
function resolvers.unresolve(str) |
| 21383 |
return abstract[str] or str |
| 21384 |
end |
| 21385 |
function resolvers.setdynamic(str) |
| 21386 |
dynamic[str]=true |
| 21387 |
end |
| 21388 |
local pattern=Cs((C(R("az")^2)*P(":")*C((1-S(" \"\';,"))^1)/_resolve_+P(1))^0) |
| 21389 |
local prefix=C(R("az")^2)*P(":") |
| 21390 |
local target=C((1-S(" \"\';,"))^1) |
| 21391 |
local notarget=(#S(";,")+P(-1))*Cc("") |
| 21392 |
local p_resolve=Cs(((prefix*(target+notarget))/_resolve_+P(1))^0) |
| 21393 |
local p_simple=prefix*P(-1) |
| 21394 |
local function resolve(str) |
| 21395 |
if type(str)=="table" then |
| 21396 |
local res={} |
| 21397 |
for i=1,#str do |
| 21398 |
res[i]=resolve(str[i]) |
| 21399 |
end |
| 21400 |
return res |
| 21401 |
end |
| 21402 |
local res=resolved[str] |
| 21403 |
if res then |
| 21404 |
return res |
| 21405 |
end |
| 21406 |
local simple=lpegmatch(p_simple,str) |
| 21407 |
local action=prefixes[simple] |
| 21408 |
if action then |
| 21409 |
local res=action(res) |
| 21410 |
if not dynamic[simple] then |
| 21411 |
resolved[simple]=res |
| 21412 |
abstract[res]=simple |
| 21413 |
end |
| 21414 |
return res |
| 21415 |
end |
| 21416 |
res=lpegmatch(p_resolve,str) |
| 21417 |
resolved[str]=res |
| 21418 |
abstract[res]=str |
| 21419 |
return res |
| 21420 |
end |
| 21421 |
resolvers.resolve=resolve |
| 21422 |
if type(osuname)=="function" then |
| 21423 |
for k,v in next,osuname() do |
| 21424 |
if not prefixes[k] then |
| 21425 |
prefixes[k]=function() return v end |
| 21426 |
end |
| 21427 |
end |
| 21428 |
end |
| 21429 |
if ostype=="unix" then |
| 21430 |
local pattern |
| 21431 |
local function makepattern(t,k,v) |
| 21432 |
if t then |
| 21433 |
rawset(t,k,v) |
| 21434 |
end |
| 21435 |
local colon=P(":") |
| 21436 |
for k,v in sortedpairs(prefixes) do |
| 21437 |
if p then |
| 21438 |
p=P(k)+p |
| 21439 |
else |
| 21440 |
p=P(k) |
| 21441 |
end |
| 21442 |
end |
| 21443 |
pattern=Cs((p*colon+colon/";"+P(1))^0) |
| 21444 |
end |
| 21445 |
makepattern() |
| 21446 |
table.setmetatablenewindex(prefixes,makepattern) |
| 21447 |
function resolvers.repath(str) |
| 21448 |
return lpegmatch(pattern,str) |
| 21449 |
end |
| 21450 |
else |
| 21451 |
function resolvers.repath(str) |
| 21452 |
return str |
| 21453 |
end |
| 21454 |
end |
| 21455 |
|
| 21456 |
|
| 21457 |
end -- of closure |
| 21458 |
|
| 21459 |
do -- create closure to overcome 200 locals limit |
| 21460 |
|
| 21461 |
package.loaded["data-exp"] = package.loaded["data-exp"] or true |
| 21462 |
|
| 21463 |
-- original size: 18571, stripped down to: 10558 |
| 21464 |
|
| 21465 |
if not modules then modules={} end modules ['data-exp']={ |
| 21466 |
version=1.001, |
| 21467 |
comment="companion to luat-lib.mkiv", |
| 21468 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 21469 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 21470 |
license="see context related readme files", |
| 21471 |
} |
| 21472 |
local format,find,gmatch,lower,char,sub=string.format,string.find,string.gmatch,string.lower,string.char,string.sub |
| 21473 |
local concat,sort=table.concat,table.sort |
| 21474 |
local sortedkeys=table.sortedkeys |
| 21475 |
local lpegmatch,lpegpatterns=lpeg.match,lpeg.patterns |
| 21476 |
local Ct,Cs,Cc,Carg,P,C,S=lpeg.Ct,lpeg.Cs,lpeg.Cc,lpeg.Carg,lpeg.P,lpeg.C,lpeg.S |
| 21477 |
local type,next=type,next |
| 21478 |
local isdir=lfs.isdir |
| 21479 |
local collapsepath,joinpath,basename=file.collapsepath,file.join,file.basename |
| 21480 |
local trace_locating=false trackers.register("resolvers.locating",function(v) trace_locating=v end) |
| 21481 |
local trace_expansions=false trackers.register("resolvers.expansions",function(v) trace_expansions=v end) |
| 21482 |
local trace_globbing=true trackers.register("resolvers.globbing",function(v) trace_globbing=v end) |
| 21483 |
local report_expansions=logs.reporter("resolvers","expansions") |
| 21484 |
local report_globbing=logs.reporter("resolvers","globbing") |
| 21485 |
local resolvers=resolvers |
| 21486 |
local resolveprefix=resolvers.resolve |
| 21487 |
local function f_both(a,b) |
| 21488 |
local t,n={},0 |
| 21489 |
for sb in gmatch(b,"[^,]+") do |
| 21490 |
for sa in gmatch(a,"[^,]+") do |
| 21491 |
n=n+1;t[n]=sa..sb |
| 21492 |
end |
| 21493 |
end |
| 21494 |
return concat(t,",") |
| 21495 |
end |
| 21496 |
local comma=P(",") |
| 21497 |
local nocomma=(1-comma)^1 |
| 21498 |
local docomma=comma^1/"," |
| 21499 |
local before=Cs((nocomma*Carg(1)+docomma)^0) |
| 21500 |
local after=Cs((Carg(1)*nocomma+docomma)^0) |
| 21501 |
local both=Cs(((C(nocomma)*Carg(1))/function(a,b) return lpegmatch(before,b,1,a) end+docomma)^0) |
| 21502 |
local function f_first (a,b) return lpegmatch(after,b,1,a) end |
| 21503 |
local function f_second(a,b) return lpegmatch(before,a,1,b) end |
| 21504 |
local function f_both (a,b) return lpegmatch(both,b,1,a) end |
| 21505 |
local left=P("{") |
| 21506 |
local right=P("}") |
| 21507 |
local var=P((1-S("{}" ))^0) |
| 21508 |
local set=P((1-S("{},"))^0) |
| 21509 |
local other=P(1) |
| 21510 |
local l_first=Cs((Cc("{")*(C(set)*left*C(var)*right/f_first)*Cc("}")+other )^0 ) |
| 21511 |
local l_second=Cs((Cc("{")*(left*C(var)*right*C(set)/f_second)*Cc("}")+other )^0 ) |
| 21512 |
local l_both=Cs((Cc("{")*(left*C(var)*right*left*C(var)*right/f_both)*Cc("}")+other )^0 ) |
| 21513 |
local l_rest=Cs((left*var*(left/"")*var*(right/"")*var*right+other )^0 ) |
| 21514 |
local stripper_1=lpeg.stripper ("{}@") |
| 21515 |
local replacer_1=lpeg.replacer { { ",}",",@}" },{ "{,","{@," },} |
| 21516 |
local function splitpathexpr(str,newlist,validate) |
| 21517 |
if trace_expansions then |
| 21518 |
report_expansions("expanding variable %a",str) |
| 21519 |
end |
| 21520 |
local t,ok,done=newlist or {},false,false |
| 21521 |
local n=#t |
| 21522 |
str=lpegmatch(replacer_1,str) |
| 21523 |
repeat |
| 21524 |
local old=str |
| 21525 |
repeat |
| 21526 |
local old=str |
| 21527 |
str=lpegmatch(l_first,str) |
| 21528 |
until old==str |
| 21529 |
repeat |
| 21530 |
local old=str |
| 21531 |
str=lpegmatch(l_second,str) |
| 21532 |
until old==str |
| 21533 |
repeat |
| 21534 |
local old=str |
| 21535 |
str=lpegmatch(l_both,str) |
| 21536 |
until old==str |
| 21537 |
repeat |
| 21538 |
local old=str |
| 21539 |
str=lpegmatch(l_rest,str) |
| 21540 |
until old==str |
| 21541 |
until old==str |
| 21542 |
str=lpegmatch(stripper_1,str) |
| 21543 |
if validate then |
| 21544 |
for s in gmatch(str,"[^,]+") do |
| 21545 |
local v=validate(s) |
| 21546 |
if v then |
| 21547 |
n=n+1 |
| 21548 |
t[n]=v |
| 21549 |
end |
| 21550 |
end |
| 21551 |
else |
| 21552 |
for s in gmatch(str,"[^,]+") do |
| 21553 |
n=n+1 |
| 21554 |
t[n]=s |
| 21555 |
end |
| 21556 |
end |
| 21557 |
if trace_expansions then |
| 21558 |
for k=1,#t do |
| 21559 |
report_expansions("% 4i: %s",k,t[k]) |
| 21560 |
end |
| 21561 |
end |
| 21562 |
return t |
| 21563 |
end |
| 21564 |
local function validate(s) |
| 21565 |
s=collapsepath(s) |
| 21566 |
return s~="" and not find(s,"^!*unset/*$") and s |
| 21567 |
end |
| 21568 |
resolvers.validatedpath=validate |
| 21569 |
function resolvers.expandedpathfromlist(pathlist) |
| 21570 |
local newlist={} |
| 21571 |
for k=1,#pathlist do |
| 21572 |
splitpathexpr(pathlist[k],newlist,validate) |
| 21573 |
end |
| 21574 |
return newlist |
| 21575 |
end |
| 21576 |
local usedhomedir=nil |
| 21577 |
local donegation=(P("!")/"" )^0 |
| 21578 |
local doslashes=(P("\\")/"/"+1)^0 |
| 21579 |
local function expandedhome() |
| 21580 |
if not usedhomedir then |
| 21581 |
usedhomedir=lpegmatch(Cs(donegation*doslashes),environment.homedir or "") |
| 21582 |
if usedhomedir=="~" or usedhomedir=="" or not isdir(usedhomedir) then |
| 21583 |
if trace_expansions then |
| 21584 |
report_expansions("no home dir set, ignoring dependent path using current path") |
| 21585 |
end |
| 21586 |
usedhomedir="." |
| 21587 |
end |
| 21588 |
end |
| 21589 |
return usedhomedir |
| 21590 |
end |
| 21591 |
local dohome=((P("~")+P("$HOME")+P("%HOME%"))/expandedhome)^0 |
| 21592 |
local cleanup=Cs(donegation*dohome*doslashes) |
| 21593 |
resolvers.cleanpath=function(str) |
| 21594 |
return str and lpegmatch(cleanup,str) or "" |
| 21595 |
end |
| 21596 |
local expandhome=P("~")/"$HOME" |
| 21597 |
local dodouble=P('"')/""*(expandhome+(1-P('"')))^0*P('"')/"" |
| 21598 |
local dosingle=P("'")/""*(expandhome+(1-P("'")))^0*P("'")/"" |
| 21599 |
local dostring=(expandhome+1 )^0 |
| 21600 |
local stripper=Cs( |
| 21601 |
lpegpatterns.unspacer*(dosingle+dodouble+dostring)*lpegpatterns.unspacer |
| 21602 |
) |
| 21603 |
function resolvers.checkedvariable(str) |
| 21604 |
return type(str)=="string" and lpegmatch(stripper,str) or str |
| 21605 |
end |
| 21606 |
local cache={} |
| 21607 |
local splitter=lpeg.tsplitat(";") |
| 21608 |
local backslashswapper=lpeg.replacer("\\","/") |
| 21609 |
local function splitconfigurationpath(str) |
| 21610 |
if str then |
| 21611 |
local found=cache[str] |
| 21612 |
if not found then |
| 21613 |
if str=="" then |
| 21614 |
found={} |
| 21615 |
else |
| 21616 |
local split=lpegmatch(splitter,lpegmatch(backslashswapper,str)) |
| 21617 |
found={} |
| 21618 |
local noffound=0 |
| 21619 |
for i=1,#split do |
| 21620 |
local s=split[i] |
| 21621 |
if not find(s,"^{*unset}*") then |
| 21622 |
noffound=noffound+1 |
| 21623 |
found[noffound]=s |
| 21624 |
end |
| 21625 |
end |
| 21626 |
if trace_expansions then |
| 21627 |
report_expansions("splitting path specification %a",str) |
| 21628 |
for k=1,noffound do |
| 21629 |
report_expansions("% 4i: %s",k,found[k]) |
| 21630 |
end |
| 21631 |
end |
| 21632 |
cache[str]=found |
| 21633 |
end |
| 21634 |
end |
| 21635 |
return found |
| 21636 |
end |
| 21637 |
end |
| 21638 |
resolvers.splitconfigurationpath=splitconfigurationpath |
| 21639 |
function resolvers.splitpath(str) |
| 21640 |
if type(str)=='table' then |
| 21641 |
return str |
| 21642 |
else |
| 21643 |
return splitconfigurationpath(str) |
| 21644 |
end |
| 21645 |
end |
| 21646 |
function resolvers.joinpath(str) |
| 21647 |
if type(str)=='table' then |
| 21648 |
return joinpath(str) |
| 21649 |
else |
| 21650 |
return str |
| 21651 |
end |
| 21652 |
end |
| 21653 |
local attributes,directory=lfs.attributes,lfs.dir |
| 21654 |
local weird=P(".")^1+lpeg.anywhere(S("~`!#$%^&*()={}[]:;\"\'||<>,?\n\r\t")) |
| 21655 |
local lessweird=P(".")^1+lpeg.anywhere(S("~`#$%^&*:;\"\'||<>,?\n\r\t")) |
| 21656 |
local timer={} |
| 21657 |
local scanned={} |
| 21658 |
local nofscans=0 |
| 21659 |
local scancache={} |
| 21660 |
local fullcache={} |
| 21661 |
local nofsharedscans=0 |
| 21662 |
local addcasecraptoo=true |
| 21663 |
local function scan(files,remap,spec,path,n,m,r,onlyone,tolerant,reported) |
| 21664 |
local full=path=="" and spec or (spec..path..'/') |
| 21665 |
local dirlist={} |
| 21666 |
local nofdirs=0 |
| 21667 |
local pattern=tolerant and lessweird or weird |
| 21668 |
local filelist={} |
| 21669 |
local noffiles=0 |
| 21670 |
for name,mode in directory(full) do |
| 21671 |
if not lpegmatch(pattern,name) then |
| 21672 |
if not mode then |
| 21673 |
mode=attributes(full..name,"mode") |
| 21674 |
end |
| 21675 |
if mode=="file" then |
| 21676 |
n=n+1 |
| 21677 |
noffiles=noffiles+1 |
| 21678 |
filelist[noffiles]=name |
| 21679 |
elseif mode=="directory" then |
| 21680 |
m=m+1 |
| 21681 |
nofdirs=nofdirs+1 |
| 21682 |
if path~="" then |
| 21683 |
dirlist[nofdirs]=path.."/"..name |
| 21684 |
else |
| 21685 |
dirlist[nofdirs]=name |
| 21686 |
end |
| 21687 |
end |
| 21688 |
end |
| 21689 |
end |
| 21690 |
if noffiles>0 then |
| 21691 |
sort(filelist) |
| 21692 |
for i=1,noffiles do |
| 21693 |
local name=filelist[i] |
| 21694 |
local lower=lower(name) |
| 21695 |
local paths=files[lower] |
| 21696 |
if paths then |
| 21697 |
if onlyone then |
| 21698 |
else |
| 21699 |
if name~=lower then |
| 21700 |
local rl=remap[lower] |
| 21701 |
if not rl then |
| 21702 |
remap[lower]=name |
| 21703 |
r=r+1 |
| 21704 |
elseif trace_globbing and rl~=name and not reported[name] then |
| 21705 |
report_globbing("confusing filename, name: %a, lower: %a, already: %a",name,lower,rl) |
| 21706 |
reported[name]=true |
| 21707 |
end |
| 21708 |
if addcasecraptoo then |
| 21709 |
local paths=files[name] |
| 21710 |
if not paths then |
| 21711 |
files[name]=path |
| 21712 |
elseif type(paths)=="string" then |
| 21713 |
files[name]={ paths,path } |
| 21714 |
else |
| 21715 |
paths[#paths+1]=path |
| 21716 |
end |
| 21717 |
end |
| 21718 |
end |
| 21719 |
if type(paths)=="string" then |
| 21720 |
files[lower]={ paths,path } |
| 21721 |
else |
| 21722 |
paths[#paths+1]=path |
| 21723 |
end |
| 21724 |
end |
| 21725 |
else |
| 21726 |
files[lower]=path |
| 21727 |
if name~=lower then |
| 21728 |
local rl=remap[lower] |
| 21729 |
if not rl then |
| 21730 |
remap[lower]=name |
| 21731 |
r=r+1 |
| 21732 |
elseif trace_globbing and rl~=name and not reported[name] then |
| 21733 |
report_globbing("confusing filename, name: %a, lower: %a, already: %a",name,lower,rl) |
| 21734 |
reported[name]=true |
| 21735 |
end |
| 21736 |
end |
| 21737 |
end |
| 21738 |
end |
| 21739 |
end |
| 21740 |
if nofdirs>0 then |
| 21741 |
sort(dirlist) |
| 21742 |
for i=1,nofdirs do |
| 21743 |
files,remap,n,m,r=scan(files,remap,spec,dirlist[i],n,m,r,onlyonce,tolerant,reported) |
| 21744 |
end |
| 21745 |
end |
| 21746 |
scancache[sub(full,1,-2)]=files |
| 21747 |
return files,remap,n,m,r |
| 21748 |
end |
| 21749 |
local function scanfiles(path,branch,usecache,onlyonce,tolerant) |
| 21750 |
local realpath=resolveprefix(path) |
| 21751 |
if usecache then |
| 21752 |
local content=fullcache[realpath] |
| 21753 |
if content then |
| 21754 |
if trace_locating then |
| 21755 |
report_expansions("using cached scan of path %a, branch %a",path,branch or path) |
| 21756 |
end |
| 21757 |
nofsharedscans=nofsharedscans+1 |
| 21758 |
return content |
| 21759 |
end |
| 21760 |
end |
| 21761 |
statistics.starttiming(timer) |
| 21762 |
if trace_locating then |
| 21763 |
report_expansions("scanning path %a, branch %a",path,branch or path) |
| 21764 |
end |
| 21765 |
local content |
| 21766 |
if isdir(realpath) then |
| 21767 |
local files,remap,n,m,r=scan({},{},realpath..'/',"",0,0,0,onlyonce,tolerant,{}) |
| 21768 |
content={ |
| 21769 |
metadata={ |
| 21770 |
path=path, |
| 21771 |
files=n, |
| 21772 |
directories=m, |
| 21773 |
remappings=r, |
| 21774 |
}, |
| 21775 |
files=files, |
| 21776 |
remap=remap, |
| 21777 |
} |
| 21778 |
if trace_locating then |
| 21779 |
report_expansions("%s files found on %s directories with %s uppercase remappings",n,m,r) |
| 21780 |
end |
| 21781 |
else |
| 21782 |
content={ |
| 21783 |
metadata={ |
| 21784 |
path=path, |
| 21785 |
files=0, |
| 21786 |
directories=0, |
| 21787 |
remappings=0, |
| 21788 |
}, |
| 21789 |
files={}, |
| 21790 |
remap={}, |
| 21791 |
} |
| 21792 |
if trace_locating then |
| 21793 |
report_expansions("invalid path %a",realpath) |
| 21794 |
end |
| 21795 |
end |
| 21796 |
if usecache then |
| 21797 |
scanned[#scanned+1]=realpath |
| 21798 |
fullcache[realpath]=content |
| 21799 |
end |
| 21800 |
nofscans=nofscans+1 |
| 21801 |
statistics.stoptiming(timer) |
| 21802 |
return content |
| 21803 |
end |
| 21804 |
resolvers.scanfiles=scanfiles |
| 21805 |
function resolvers.simplescanfiles(path,branch,usecache) |
| 21806 |
return scanfiles(path,branch,usecache,true,true) |
| 21807 |
end |
| 21808 |
function resolvers.scandata() |
| 21809 |
table.sort(scanned) |
| 21810 |
return { |
| 21811 |
n=nofscans, |
| 21812 |
shared=nofsharedscans, |
| 21813 |
time=statistics.elapsedtime(timer), |
| 21814 |
paths=scanned, |
| 21815 |
} |
| 21816 |
end |
| 21817 |
function resolvers.get_from_content(content,path,name) |
| 21818 |
if not content then |
| 21819 |
return |
| 21820 |
end |
| 21821 |
local files=content.files |
| 21822 |
if not files then |
| 21823 |
return |
| 21824 |
end |
| 21825 |
local remap=content.remap |
| 21826 |
if not remap then |
| 21827 |
return |
| 21828 |
end |
| 21829 |
if name then |
| 21830 |
local used=lower(name) |
| 21831 |
return path,remap[used] or used |
| 21832 |
else |
| 21833 |
local name=path |
| 21834 |
local used=lower(name) |
| 21835 |
local path=files[used] |
| 21836 |
if path then |
| 21837 |
return path,remap[used] or used |
| 21838 |
end |
| 21839 |
end |
| 21840 |
end |
| 21841 |
local nothing=function() end |
| 21842 |
function resolvers.filtered_from_content(content,pattern) |
| 21843 |
if content and type(pattern)=="string" then |
| 21844 |
local pattern=lower(pattern) |
| 21845 |
local files=content.files |
| 21846 |
local remap=content.remap |
| 21847 |
if files and remap then |
| 21848 |
local f=sortedkeys(files) |
| 21849 |
local n=#f |
| 21850 |
local i=0 |
| 21851 |
local function iterator() |
| 21852 |
while i<n do |
| 21853 |
i=i+1 |
| 21854 |
local k=f[i] |
| 21855 |
if find(k,pattern) then |
| 21856 |
return files[k],remap and remap[k] or k |
| 21857 |
end |
| 21858 |
end |
| 21859 |
end |
| 21860 |
return iterator |
| 21861 |
end |
| 21862 |
end |
| 21863 |
return nothing |
| 21864 |
end |
| 21865 |
|
| 21866 |
|
| 21867 |
end -- of closure |
| 21868 |
|
| 21869 |
do -- create closure to overcome 200 locals limit |
| 21870 |
|
| 21871 |
package.loaded["data-env"] = package.loaded["data-env"] or true |
| 21872 |
|
| 21873 |
-- original size: 9758, stripped down to: 6523 |
| 21874 |
|
| 21875 |
if not modules then modules={} end modules ['data-env']={ |
| 21876 |
version=1.001, |
| 21877 |
comment="companion to luat-lib.mkiv", |
| 21878 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 21879 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 21880 |
license="see context related readme files", |
| 21881 |
} |
| 21882 |
local lower,gsub=string.lower,string.gsub |
| 21883 |
local next,rawget=next,rawget |
| 21884 |
local resolvers=resolvers |
| 21885 |
local allocate=utilities.storage.allocate |
| 21886 |
local setmetatableindex=table.setmetatableindex |
| 21887 |
local sortedhash=table.sortedhash |
| 21888 |
local suffixonly=file.suffixonly |
| 21889 |
local formats=allocate() |
| 21890 |
local suffixes=allocate() |
| 21891 |
local dangerous=allocate() |
| 21892 |
local suffixmap=allocate() |
| 21893 |
local usertypes=allocate() |
| 21894 |
resolvers.formats=formats |
| 21895 |
resolvers.suffixes=suffixes |
| 21896 |
resolvers.dangerous=dangerous |
| 21897 |
resolvers.suffixmap=suffixmap |
| 21898 |
resolvers.usertypes=usertypes |
| 21899 |
local luasuffixes=utilities.lua.suffixes |
| 21900 |
local relations=allocate { |
| 21901 |
core={ |
| 21902 |
ofm={ |
| 21903 |
names={ "ofm","omega font metric","omega font metrics" }, |
| 21904 |
variable='OFMFONTS', |
| 21905 |
suffixes={ 'ofm','tfm' }, |
| 21906 |
}, |
| 21907 |
ovf={ |
| 21908 |
names={ "ovf","omega virtual font","omega virtual fonts" }, |
| 21909 |
variable='OVFFONTS', |
| 21910 |
suffixes={ 'ovf','vf' }, |
| 21911 |
}, |
| 21912 |
tfm={ |
| 21913 |
names={ "tfm","tex font metric","tex font metrics" }, |
| 21914 |
variable='TFMFONTS', |
| 21915 |
suffixes={ 'tfm' }, |
| 21916 |
}, |
| 21917 |
vf={ |
| 21918 |
names={ "vf","virtual font","virtual fonts" }, |
| 21919 |
variable='VFFONTS', |
| 21920 |
suffixes={ 'vf' }, |
| 21921 |
}, |
| 21922 |
otf={ |
| 21923 |
names={ "otf","opentype","opentype font","opentype fonts"}, |
| 21924 |
variable='OPENTYPEFONTS', |
| 21925 |
suffixes={ 'otf' }, |
| 21926 |
}, |
| 21927 |
ttf={ |
| 21928 |
names={ "ttf","truetype","truetype font","truetype fonts","truetype collection","truetype collections","truetype dictionary","truetype dictionaries" }, |
| 21929 |
variable='TTFONTS', |
| 21930 |
suffixes={ 'ttf','ttc','dfont' }, |
| 21931 |
}, |
| 21932 |
afm={ |
| 21933 |
names={ "afm","adobe font metric","adobe font metrics" }, |
| 21934 |
variable="AFMFONTS", |
| 21935 |
suffixes={ "afm" }, |
| 21936 |
}, |
| 21937 |
pfb={ |
| 21938 |
names={ "pfb","type1","type 1","type1 font","type 1 font","type1 fonts","type 1 fonts" }, |
| 21939 |
variable='T1FONTS', |
| 21940 |
suffixes={ 'pfb','pfa' }, |
| 21941 |
}, |
| 21942 |
fea={ |
| 21943 |
names={ "fea","font feature","font features","font feature file","font feature files" }, |
| 21944 |
variable='FONTFEATURES', |
| 21945 |
suffixes={ 'fea' }, |
| 21946 |
}, |
| 21947 |
cid={ |
| 21948 |
names={ "cid","cid map","cid maps","cid file","cid files" }, |
| 21949 |
variable='FONTCIDMAPS', |
| 21950 |
suffixes={ 'cid','cidmap' }, |
| 21951 |
}, |
| 21952 |
fmt={ |
| 21953 |
names={ "fmt","format","tex format" }, |
| 21954 |
variable='TEXFORMATS', |
| 21955 |
suffixes={ 'fmt' }, |
| 21956 |
}, |
| 21957 |
mem={ |
| 21958 |
names={ 'mem',"metapost format" }, |
| 21959 |
variable='MPMEMS', |
| 21960 |
suffixes={ 'mem' }, |
| 21961 |
}, |
| 21962 |
mp={ |
| 21963 |
names={ "mp" }, |
| 21964 |
variable='MPINPUTS', |
| 21965 |
suffixes=CONTEXTLMTXMODE>0 |
| 21966 |
and { 'mpxl','mpvi','mpiv','mpii','mp' } |
| 21967 |
or { 'mpvi','mpiv','mpii','mp' }, |
| 21968 |
usertype=true, |
| 21969 |
}, |
| 21970 |
tex={ |
| 21971 |
names={ "tex" }, |
| 21972 |
variable='TEXINPUTS', |
| 21973 |
suffixes={ "tex","mkiv","mkvi","mkxl","mklx","mkii","cld","lfg","xml" }, |
| 21974 |
usertype=true, |
| 21975 |
}, |
| 21976 |
icc={ |
| 21977 |
names={ "icc","icc profile","icc profiles" }, |
| 21978 |
variable='ICCPROFILES', |
| 21979 |
suffixes={ 'icc' }, |
| 21980 |
}, |
| 21981 |
texmfscripts={ |
| 21982 |
names={ "texmfscript","texmfscripts","script","scripts" }, |
| 21983 |
variable='TEXMFSCRIPTS', |
| 21984 |
suffixes={ 'lua','rb','pl','py' }, |
| 21985 |
}, |
| 21986 |
lua={ |
| 21987 |
names={ "lua" }, |
| 21988 |
variable='LUAINPUTS', |
| 21989 |
suffixes={ luasuffixes.lmt,luasuffixes.lua,luasuffixes.luc,luasuffixes.tma,luasuffixes.tmc }, |
| 21990 |
usertype=true, |
| 21991 |
}, |
| 21992 |
lib={ |
| 21993 |
names={ "lib" }, |
| 21994 |
variable='CLUAINPUTS', |
| 21995 |
suffixes=os.libsuffix and { os.libsuffix } or { 'dll','so' }, |
| 21996 |
}, |
| 21997 |
bib={ |
| 21998 |
names={ 'bib' }, |
| 21999 |
variable='BIBINPUTS', |
| 22000 |
suffixes={ 'bib' }, |
| 22001 |
usertype=true, |
| 22002 |
}, |
| 22003 |
bst={ |
| 22004 |
names={ 'bst' }, |
| 22005 |
variable='BSTINPUTS', |
| 22006 |
suffixes={ 'bst' }, |
| 22007 |
usertype=true, |
| 22008 |
}, |
| 22009 |
fontconfig={ |
| 22010 |
names={ 'fontconfig','fontconfig file','fontconfig files' }, |
| 22011 |
variable='FONTCONFIG_PATH', |
| 22012 |
}, |
| 22013 |
pk={ |
| 22014 |
names={ "pk" }, |
| 22015 |
variable='PKFONTS', |
| 22016 |
suffixes={ 'pk' }, |
| 22017 |
}, |
| 22018 |
}, |
| 22019 |
obsolete={ |
| 22020 |
enc={ |
| 22021 |
names={ "enc","enc files","enc file","encoding files","encoding file" }, |
| 22022 |
variable='ENCFONTS', |
| 22023 |
suffixes={ 'enc' }, |
| 22024 |
}, |
| 22025 |
map={ |
| 22026 |
names={ "map","map files","map file" }, |
| 22027 |
variable='TEXFONTMAPS', |
| 22028 |
suffixes={ 'map' }, |
| 22029 |
}, |
| 22030 |
lig={ |
| 22031 |
names={ "lig files","lig file","ligature file","ligature files" }, |
| 22032 |
variable='LIGFONTS', |
| 22033 |
suffixes={ 'lig' }, |
| 22034 |
}, |
| 22035 |
opl={ |
| 22036 |
names={ "opl" }, |
| 22037 |
variable='OPLFONTS', |
| 22038 |
suffixes={ 'opl' }, |
| 22039 |
}, |
| 22040 |
ovp={ |
| 22041 |
names={ "ovp" }, |
| 22042 |
variable='OVPFONTS', |
| 22043 |
suffixes={ 'ovp' }, |
| 22044 |
}, |
| 22045 |
}, |
| 22046 |
kpse={ |
| 22047 |
base={ |
| 22048 |
names={ 'base',"metafont format" }, |
| 22049 |
variable='MFBASES', |
| 22050 |
suffixes={ 'base','bas' }, |
| 22051 |
}, |
| 22052 |
cmap={ |
| 22053 |
names={ 'cmap','cmap files','cmap file' }, |
| 22054 |
variable='CMAPFONTS', |
| 22055 |
suffixes={ 'cmap' }, |
| 22056 |
}, |
| 22057 |
cnf={ |
| 22058 |
names={ 'cnf' }, |
| 22059 |
suffixes={ 'cnf' }, |
| 22060 |
}, |
| 22061 |
web={ |
| 22062 |
names={ 'web' }, |
| 22063 |
suffixes={ 'web','ch' } |
| 22064 |
}, |
| 22065 |
cweb={ |
| 22066 |
names={ 'cweb' }, |
| 22067 |
suffixes={ 'w','web','ch' }, |
| 22068 |
}, |
| 22069 |
gf={ |
| 22070 |
names={ 'gf' }, |
| 22071 |
suffixes={ '<resolution>gf' }, |
| 22072 |
}, |
| 22073 |
mf={ |
| 22074 |
names={ 'mf' }, |
| 22075 |
variable='MFINPUTS', |
| 22076 |
suffixes={ 'mf' }, |
| 22077 |
}, |
| 22078 |
mft={ |
| 22079 |
names={ 'mft' }, |
| 22080 |
suffixes={ 'mft' }, |
| 22081 |
}, |
| 22082 |
pk={ |
| 22083 |
names={ 'pk' }, |
| 22084 |
suffixes={ '<resolution>pk' }, |
| 22085 |
}, |
| 22086 |
}, |
| 22087 |
} |
| 22088 |
resolvers.relations=relations |
| 22089 |
function resolvers.updaterelations() |
| 22090 |
for category,categories in sortedhash(relations) do |
| 22091 |
for name,relation in sortedhash(categories) do |
| 22092 |
local rn=relation.names |
| 22093 |
local rv=relation.variable |
| 22094 |
if rn and rv then |
| 22095 |
local rs=relation.suffixes |
| 22096 |
local ru=relation.usertype |
| 22097 |
for i=1,#rn do |
| 22098 |
local rni=lower(gsub(rn[i]," ","")) |
| 22099 |
formats[rni]=rv |
| 22100 |
if rs then |
| 22101 |
suffixes[rni]=rs |
| 22102 |
for i=1,#rs do |
| 22103 |
local rsi=rs[i] |
| 22104 |
if not suffixmap[rsi] then |
| 22105 |
suffixmap[rsi]=rni |
| 22106 |
end |
| 22107 |
end |
| 22108 |
end |
| 22109 |
end |
| 22110 |
if ru then |
| 22111 |
usertypes[name]=true |
| 22112 |
end |
| 22113 |
end |
| 22114 |
end |
| 22115 |
end |
| 22116 |
end |
| 22117 |
resolvers.updaterelations() |
| 22118 |
local function simplified(t,k) |
| 22119 |
return k and rawget(t,lower(gsub(k," ",""))) or nil |
| 22120 |
end |
| 22121 |
setmetatableindex(formats,simplified) |
| 22122 |
setmetatableindex(suffixes,simplified) |
| 22123 |
setmetatableindex(suffixmap,simplified) |
| 22124 |
function resolvers.suffixofformat(str) |
| 22125 |
local s=suffixes[str] |
| 22126 |
return s and s[1] or "" |
| 22127 |
end |
| 22128 |
function resolvers.suffixofformat(str) |
| 22129 |
return suffixes[str] or {} |
| 22130 |
end |
| 22131 |
for name,format in next,formats do |
| 22132 |
dangerous[name]=true |
| 22133 |
end |
| 22134 |
dangerous.tex=nil |
| 22135 |
function resolvers.formatofvariable(str) |
| 22136 |
return formats[str] or '' |
| 22137 |
end |
| 22138 |
function resolvers.formatofsuffix(str) |
| 22139 |
return suffixmap[suffixonly(str)] or 'tex' |
| 22140 |
end |
| 22141 |
function resolvers.variableofformat(str) |
| 22142 |
return formats[str] or '' |
| 22143 |
end |
| 22144 |
function resolvers.variableofformatorsuffix(str) |
| 22145 |
local v=formats[str] |
| 22146 |
if v then |
| 22147 |
return v |
| 22148 |
end |
| 22149 |
v=suffixmap[suffixonly(str)] |
| 22150 |
if v then |
| 22151 |
return formats[v] |
| 22152 |
end |
| 22153 |
return '' |
| 22154 |
end |
| 22155 |
|
| 22156 |
|
| 22157 |
end -- of closure |
| 22158 |
|
| 22159 |
do -- create closure to overcome 200 locals limit |
| 22160 |
|
| 22161 |
package.loaded["data-tmp"] = package.loaded["data-tmp"] or true |
| 22162 |
|
| 22163 |
-- original size: 16995, stripped down to: 12083 |
| 22164 |
|
| 22165 |
if not modules then modules={} end modules ['data-tmp']={ |
| 22166 |
version=1.100, |
| 22167 |
comment="companion to luat-lib.mkiv", |
| 22168 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 22169 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 22170 |
license="see context related readme files" |
| 22171 |
} |
| 22172 |
local next,type=next,type |
| 22173 |
local pcall,loadfile,collectgarbage=pcall,loadfile,collectgarbage |
| 22174 |
local format,lower,gsub=string.format,string.lower,string.gsub |
| 22175 |
local concat,serialize,fastserialize,serializetofile=table.concat,table.serialize,table.fastserialize,table.tofile |
| 22176 |
local mkdirs,expanddirname,isdir,isfile=dir.mkdirs,dir.expandname,lfs.isdir,lfs.isfile |
| 22177 |
local is_writable,is_readable=file.is_writable,file.is_readable |
| 22178 |
local collapsepath,joinfile,addsuffix,dirname=file.collapsepath,file.join,file.addsuffix,file.dirname |
| 22179 |
local savedata=file.savedata |
| 22180 |
local formatters=string.formatters |
| 22181 |
local osexit,osdate,osuuid=os.exit,os.date,os.uuid |
| 22182 |
local removefile=os.remove |
| 22183 |
local md5hex=md5.hex |
| 22184 |
local trace_locating=false trackers.register("resolvers.locating",function(v) trace_locating=v end) |
| 22185 |
local trace_cache=false trackers.register("resolvers.cache",function(v) trace_cache=v end) |
| 22186 |
local report_caches=logs.reporter("resolvers","caches") |
| 22187 |
local report_resolvers=logs.reporter("resolvers","caching") |
| 22188 |
local resolvers=resolvers |
| 22189 |
local cleanpath=resolvers.cleanpath |
| 22190 |
local resolvepath=resolvers.resolve |
| 22191 |
local luautilities=utilities.lua |
| 22192 |
do |
| 22193 |
local directive_cleanup=false directives.register("system.compile.cleanup",function(v) directive_cleanup=v end) |
| 22194 |
local directive_strip=false directives.register("system.compile.strip",function(v) directive_strip=v end) |
| 22195 |
local compilelua=luautilities.compile |
| 22196 |
function luautilities.compile(luafile,lucfile,cleanup,strip) |
| 22197 |
if cleanup==nil then cleanup=directive_cleanup end |
| 22198 |
if strip==nil then strip=directive_strip end |
| 22199 |
return compilelua(luafile,lucfile,cleanup,strip) |
| 22200 |
end |
| 22201 |
end |
| 22202 |
caches=caches or {} |
| 22203 |
local caches=caches |
| 22204 |
local writable=nil |
| 22205 |
local readables={} |
| 22206 |
local usedreadables={} |
| 22207 |
local compilelua=luautilities.compile |
| 22208 |
local luasuffixes=luautilities.suffixes |
| 22209 |
caches.base=caches.base or (LUATEXENGINE and LUATEXENGINE.."-cache") or "luatex-cache" |
| 22210 |
caches.more=caches.more or "context" |
| 22211 |
caches.defaults={ "TMPDIR","TEMPDIR","TMP","TEMP","HOME","HOMEPATH" } |
| 22212 |
local direct_cache=false |
| 22213 |
local fast_cache=false |
| 22214 |
local cache_tree=false |
| 22215 |
directives.register("system.caches.direct",function(v) direct_cache=true end) |
| 22216 |
directives.register("system.caches.fast",function(v) fast_cache=true end) |
| 22217 |
local function configfiles() |
| 22218 |
return concat(resolvers.configurationfiles(),";") |
| 22219 |
end |
| 22220 |
local function hashed(tree) |
| 22221 |
tree=gsub(tree,"[\\/]+$","") |
| 22222 |
tree=lower(tree) |
| 22223 |
local hash=md5hex(tree) |
| 22224 |
if trace_cache or trace_locating then |
| 22225 |
report_caches("hashing tree %a, hash %a",tree,hash) |
| 22226 |
end |
| 22227 |
return hash |
| 22228 |
end |
| 22229 |
local function treehash() |
| 22230 |
local tree=configfiles() |
| 22231 |
if not tree or tree=="" then |
| 22232 |
return false |
| 22233 |
else |
| 22234 |
return hashed(tree) |
| 22235 |
end |
| 22236 |
end |
| 22237 |
caches.hashed=hashed |
| 22238 |
caches.treehash=treehash |
| 22239 |
caches.configfiles=configfiles |
| 22240 |
local function identify() |
| 22241 |
local texmfcaches=resolvers.cleanpathlist("TEXMFCACHE") |
| 22242 |
if texmfcaches then |
| 22243 |
for k=1,#texmfcaches do |
| 22244 |
local cachepath=texmfcaches[k] |
| 22245 |
if cachepath~="" then |
| 22246 |
cachepath=resolvepath(cachepath) |
| 22247 |
cachepath=cleanpath(cachepath) |
| 22248 |
cachepath=collapsepath(cachepath) |
| 22249 |
local valid=isdir(cachepath) |
| 22250 |
if valid then |
| 22251 |
if is_readable(cachepath) then |
| 22252 |
readables[#readables+1]=cachepath |
| 22253 |
if not writable and is_writable(cachepath) then |
| 22254 |
writable=cachepath |
| 22255 |
end |
| 22256 |
end |
| 22257 |
elseif not writable then |
| 22258 |
local cacheparent=dirname(cachepath) |
| 22259 |
if is_writable(cacheparent) then |
| 22260 |
mkdirs(cachepath) |
| 22261 |
if isdir(cachepath) and is_writable(cachepath) then |
| 22262 |
report_caches("path %a created",cachepath) |
| 22263 |
writable=cachepath |
| 22264 |
readables[#readables+1]=cachepath |
| 22265 |
end |
| 22266 |
end |
| 22267 |
end |
| 22268 |
end |
| 22269 |
end |
| 22270 |
end |
| 22271 |
local texmfcaches=caches.defaults |
| 22272 |
if texmfcaches then |
| 22273 |
for k=1,#texmfcaches do |
| 22274 |
local cachepath=texmfcaches[k] |
| 22275 |
cachepath=resolvers.expansion(cachepath) |
| 22276 |
if cachepath~="" then |
| 22277 |
cachepath=resolvepath(cachepath) |
| 22278 |
cachepath=cleanpath(cachepath) |
| 22279 |
local valid=isdir(cachepath) |
| 22280 |
if valid and is_readable(cachepath) then |
| 22281 |
if not writable and is_writable(cachepath) then |
| 22282 |
readables[#readables+1]=cachepath |
| 22283 |
writable=cachepath |
| 22284 |
break |
| 22285 |
end |
| 22286 |
end |
| 22287 |
end |
| 22288 |
end |
| 22289 |
end |
| 22290 |
if not writable then |
| 22291 |
report_caches("fatal error: there is no valid writable cache path defined") |
| 22292 |
osexit() |
| 22293 |
elseif #readables==0 then |
| 22294 |
report_caches("fatal error: there is no valid readable cache path defined") |
| 22295 |
osexit() |
| 22296 |
end |
| 22297 |
writable=expanddirname(cleanpath(writable)) |
| 22298 |
local base=caches.base |
| 22299 |
local more=caches.more |
| 22300 |
local tree=cache_tree or treehash() |
| 22301 |
if tree then |
| 22302 |
cache_tree=tree |
| 22303 |
writable=mkdirs(writable,base,more,tree) |
| 22304 |
for i=1,#readables do |
| 22305 |
readables[i]=joinfile(readables[i],base,more,tree) |
| 22306 |
end |
| 22307 |
else |
| 22308 |
writable=mkdirs(writable,base,more) |
| 22309 |
for i=1,#readables do |
| 22310 |
readables[i]=joinfile(readables[i],base,more) |
| 22311 |
end |
| 22312 |
end |
| 22313 |
if trace_cache then |
| 22314 |
for i=1,#readables do |
| 22315 |
report_caches("using readable path %a (order %s)",readables[i],i) |
| 22316 |
end |
| 22317 |
report_caches("using writable path %a",writable) |
| 22318 |
end |
| 22319 |
identify=function() |
| 22320 |
return writable,readables |
| 22321 |
end |
| 22322 |
return writable,readables |
| 22323 |
end |
| 22324 |
function caches.usedpaths(separator) |
| 22325 |
local writable,readables=identify() |
| 22326 |
if #readables>1 then |
| 22327 |
local result={} |
| 22328 |
local done={} |
| 22329 |
for i=1,#readables do |
| 22330 |
local readable=readables[i] |
| 22331 |
if readable==writable then |
| 22332 |
done[readable]=true |
| 22333 |
result[#result+1]=formatters["readable+writable: %a"](readable) |
| 22334 |
elseif usedreadables[i] then |
| 22335 |
done[readable]=true |
| 22336 |
result[#result+1]=formatters["readable: %a"](readable) |
| 22337 |
end |
| 22338 |
end |
| 22339 |
if not done[writable] then |
| 22340 |
result[#result+1]=formatters["writable: %a"](writable) |
| 22341 |
end |
| 22342 |
return concat(result,separator or " | ") |
| 22343 |
else |
| 22344 |
return writable or "?" |
| 22345 |
end |
| 22346 |
end |
| 22347 |
local r_cache={} |
| 22348 |
local w_cache={} |
| 22349 |
local function getreadablepaths(...) |
| 22350 |
local tags={... } |
| 22351 |
local hash=concat(tags,"/") |
| 22352 |
local done=r_cache[hash] |
| 22353 |
if not done then |
| 22354 |
local writable,readables=identify() |
| 22355 |
if #tags>0 then |
| 22356 |
done={} |
| 22357 |
for i=1,#readables do |
| 22358 |
done[i]=joinfile(readables[i],...) |
| 22359 |
end |
| 22360 |
else |
| 22361 |
done=readables |
| 22362 |
end |
| 22363 |
r_cache[hash]=done |
| 22364 |
end |
| 22365 |
return done |
| 22366 |
end |
| 22367 |
local function getwritablepath(...) |
| 22368 |
local tags={... } |
| 22369 |
local hash=concat(tags,"/") |
| 22370 |
local done=w_cache[hash] |
| 22371 |
if not done then |
| 22372 |
local writable,readables=identify() |
| 22373 |
if #tags>0 then |
| 22374 |
done=mkdirs(writable,...) |
| 22375 |
else |
| 22376 |
done=writable |
| 22377 |
end |
| 22378 |
w_cache[hash]=done |
| 22379 |
end |
| 22380 |
return done |
| 22381 |
end |
| 22382 |
local function setfirstwritablefile(filename,...) |
| 22383 |
local wr=getwritablepath(...) |
| 22384 |
local fullname=joinfile(wr,filename) |
| 22385 |
return fullname,wr |
| 22386 |
end |
| 22387 |
local function setluanames(path,name) |
| 22388 |
return |
| 22389 |
format("%s/%s.%s",path,name,luasuffixes.tma), |
| 22390 |
format("%s/%s.%s",path,name,luasuffixes.tmc) |
| 22391 |
end |
| 22392 |
local function getfirstreadablefile(filename,...) |
| 22393 |
local fullname,path=setfirstwritablefile(filename,...) |
| 22394 |
if is_readable(fullname) then |
| 22395 |
return fullname,path |
| 22396 |
end |
| 22397 |
local rd=getreadablepaths(...) |
| 22398 |
for i=1,#rd do |
| 22399 |
local path=rd[i] |
| 22400 |
local fullname=joinfile(path,filename) |
| 22401 |
if is_readable(fullname) then |
| 22402 |
usedreadables[i]=true |
| 22403 |
return fullname,path |
| 22404 |
end |
| 22405 |
end |
| 22406 |
return fullname,path |
| 22407 |
end |
| 22408 |
caches.getreadablepaths=getreadablepaths |
| 22409 |
caches.getwritablepath=getwritablepath |
| 22410 |
caches.setfirstwritablefile=setfirstwritablefile |
| 22411 |
caches.getfirstreadablefile=getfirstreadablefile |
| 22412 |
caches.setluanames=setluanames |
| 22413 |
local checkmemory=utilities and utilities.lua and utilities.lua.checkmemory |
| 22414 |
local threshold=100 |
| 22415 |
function caches.loaddata(readables,name,writable) |
| 22416 |
local used=checkmemory and checkmemory() |
| 22417 |
if type(readables)=="string" then |
| 22418 |
readables={ readables } |
| 22419 |
end |
| 22420 |
for i=1,#readables do |
| 22421 |
local path=readables[i] |
| 22422 |
local loader=false |
| 22423 |
local state=false |
| 22424 |
local tmaname,tmcname=setluanames(path,name) |
| 22425 |
if isfile(tmcname) then |
| 22426 |
state,loader=pcall(loadfile,tmcname) |
| 22427 |
end |
| 22428 |
if not loader and isfile(tmaname) then |
| 22429 |
local tmacrap,tmcname=setluanames(writable,name) |
| 22430 |
if isfile(tmcname) then |
| 22431 |
state,loader=pcall(loadfile,tmcname) |
| 22432 |
end |
| 22433 |
compilelua(tmaname,tmcname) |
| 22434 |
if isfile(tmcname) then |
| 22435 |
state,loader=pcall(loadfile,tmcname) |
| 22436 |
end |
| 22437 |
if not loader then |
| 22438 |
state,loader=pcall(loadfile,tmaname) |
| 22439 |
end |
| 22440 |
end |
| 22441 |
if loader then |
| 22442 |
loader=loader() |
| 22443 |
if checkmemory then |
| 22444 |
checkmemory(used,threshold) |
| 22445 |
else |
| 22446 |
collectgarbage("step") |
| 22447 |
end |
| 22448 |
return loader |
| 22449 |
end |
| 22450 |
end |
| 22451 |
return false |
| 22452 |
end |
| 22453 |
function caches.is_writable(filepath,filename) |
| 22454 |
local tmaname,tmcname=setluanames(filepath,filename) |
| 22455 |
return is_writable(tmaname) |
| 22456 |
end |
| 22457 |
local saveoptions={ compact=true,accurate=not JITSUPPORTED } |
| 22458 |
function caches.savedata(filepath,filename,data,fast) |
| 22459 |
local tmaname,tmcname=setluanames(filepath,filename) |
| 22460 |
data.cache_uuid=osuuid() |
| 22461 |
if fast or fast_cache then |
| 22462 |
savedata(tmaname,fastserialize(data,true)) |
| 22463 |
elseif direct_cache then |
| 22464 |
savedata(tmaname,serialize(data,true,saveoptions)) |
| 22465 |
else |
| 22466 |
serializetofile(tmaname,data,true,saveoptions) |
| 22467 |
end |
| 22468 |
compilelua(tmaname,tmcname) |
| 22469 |
end |
| 22470 |
local content_state={} |
| 22471 |
function caches.contentstate() |
| 22472 |
return content_state or {} |
| 22473 |
end |
| 22474 |
function caches.loadcontent(cachename,dataname,filename) |
| 22475 |
if not filename then |
| 22476 |
local name=hashed(cachename) |
| 22477 |
local full,path=getfirstreadablefile(addsuffix(name,luasuffixes.lua),"trees") |
| 22478 |
filename=joinfile(path,name) |
| 22479 |
end |
| 22480 |
local state,blob=pcall(loadfile,addsuffix(filename,luasuffixes.luc)) |
| 22481 |
if trace_cache and blob then |
| 22482 |
report_caches("getting %s lua content from path %a","regular",filename) |
| 22483 |
end |
| 22484 |
if not blob then |
| 22485 |
state,blob=pcall(loadfile,addsuffix(filename,luasuffixes.lua)) |
| 22486 |
if trace_cache and blob then |
| 22487 |
report_caches("getting %s lua content from path %a","bytecode",filename) |
| 22488 |
end |
| 22489 |
end |
| 22490 |
if blob then |
| 22491 |
local data=blob() |
| 22492 |
if data and data.content then |
| 22493 |
if data.type==dataname then |
| 22494 |
if data.version==resolvers.cacheversion then |
| 22495 |
local uuid=data.uuid |
| 22496 |
content_state[#content_state+1]=uuid |
| 22497 |
if trace_cache then |
| 22498 |
report_caches("registering content uuid %a for %a",uuid,filename) |
| 22499 |
end |
| 22500 |
if trace_locating then |
| 22501 |
report_resolvers("loading %a for %a from %a",dataname,cachename,filename) |
| 22502 |
end |
| 22503 |
return data.content |
| 22504 |
else |
| 22505 |
report_resolvers("skipping %a for %a from %a (version mismatch)",dataname,cachename,filename) |
| 22506 |
end |
| 22507 |
else |
| 22508 |
report_resolvers("skipping %a for %a from %a (datatype mismatch)",dataname,cachename,filename) |
| 22509 |
end |
| 22510 |
elseif trace_locating then |
| 22511 |
report_resolvers("skipping %a for %a from %a (no content)",dataname,cachename,filename) |
| 22512 |
end |
| 22513 |
elseif trace_locating then |
| 22514 |
report_resolvers("skipping %a for %a from %a (invalid file)",dataname,cachename,filename) |
| 22515 |
end |
| 22516 |
end |
| 22517 |
function caches.collapsecontent(content) |
| 22518 |
for k,v in next,content do |
| 22519 |
if type(v)=="table" and #v==1 then |
| 22520 |
content[k]=v[1] |
| 22521 |
end |
| 22522 |
end |
| 22523 |
end |
| 22524 |
function caches.savecontent(cachename,dataname,content,filename) |
| 22525 |
if not filename then |
| 22526 |
local name=hashed(cachename) |
| 22527 |
local full,path=setfirstwritablefile(addsuffix(name,luasuffixes.lua),"trees") |
| 22528 |
filename=joinfile(path,name) |
| 22529 |
end |
| 22530 |
local luaname=addsuffix(filename,luasuffixes.lua) |
| 22531 |
local lucname=addsuffix(filename,luasuffixes.luc) |
| 22532 |
if trace_locating then |
| 22533 |
report_resolvers("preparing %a for %a",dataname,cachename) |
| 22534 |
end |
| 22535 |
local uuid=osuuid() |
| 22536 |
local data={ |
| 22537 |
type=dataname, |
| 22538 |
root=cachename, |
| 22539 |
version=resolvers.cacheversion, |
| 22540 |
date=osdate("%Y-%m-%d"), |
| 22541 |
time=osdate("%H:%M:%S"), |
| 22542 |
content=content, |
| 22543 |
uuid=uuid, |
| 22544 |
} |
| 22545 |
local ok=savedata(luaname,serialize(data,true)) |
| 22546 |
if trace_cache then |
| 22547 |
report_caches("saving %a with uuid %a",luaname,uuid) |
| 22548 |
end |
| 22549 |
if ok then |
| 22550 |
if trace_locating then |
| 22551 |
report_resolvers("category %a, cachename %a saved in %a",dataname,cachename,luaname) |
| 22552 |
end |
| 22553 |
if compilelua(luaname,lucname) then |
| 22554 |
if trace_locating then |
| 22555 |
report_resolvers("%a compiled to %a",dataname,lucname) |
| 22556 |
end |
| 22557 |
return true |
| 22558 |
else |
| 22559 |
if trace_locating then |
| 22560 |
report_resolvers("compiling failed for %a, deleting file %a",dataname,lucname) |
| 22561 |
end |
| 22562 |
removefile(lucname) |
| 22563 |
end |
| 22564 |
elseif trace_locating then |
| 22565 |
report_resolvers("unable to save %a in %a (access error)",dataname,luaname) |
| 22566 |
end |
| 22567 |
end |
| 22568 |
|
| 22569 |
|
| 22570 |
end -- of closure |
| 22571 |
|
| 22572 |
do -- create closure to overcome 200 locals limit |
| 22573 |
|
| 22574 |
package.loaded["data-met"] = package.loaded["data-met"] or true |
| 22575 |
|
| 22576 |
-- original size: 5518, stripped down to: 3854 |
| 22577 |
|
| 22578 |
if not modules then modules={} end modules ['data-met']={ |
| 22579 |
version=1.100, |
| 22580 |
comment="companion to luat-lib.mkiv", |
| 22581 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 22582 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 22583 |
license="see context related readme files" |
| 22584 |
} |
| 22585 |
local type=type |
| 22586 |
local find=string.find |
| 22587 |
local addurlscheme,urlhashed=url.addscheme,url.hashed |
| 22588 |
local collapsepath,joinfile=file.collapsepath,file.join |
| 22589 |
local report_methods=logs.reporter("resolvers","methods") |
| 22590 |
local trace_locating=false |
| 22591 |
local trace_methods=false |
| 22592 |
trackers.register("resolvers.locating",function(v) trace_methods=v end) |
| 22593 |
trackers.register("resolvers.methods",function(v) trace_methods=v end) |
| 22594 |
local allocate=utilities.storage.allocate |
| 22595 |
local resolvers=resolvers |
| 22596 |
local registered={} |
| 22597 |
local function splitmethod(filename) |
| 22598 |
if not filename then |
| 22599 |
return { |
| 22600 |
scheme="unknown", |
| 22601 |
original=filename, |
| 22602 |
} |
| 22603 |
end |
| 22604 |
if type(filename)=="table" then |
| 22605 |
return filename |
| 22606 |
end |
| 22607 |
filename=collapsepath(filename,".") |
| 22608 |
if not find(filename,"://",1,true) then |
| 22609 |
return { |
| 22610 |
scheme="file", |
| 22611 |
path=filename, |
| 22612 |
original=filename, |
| 22613 |
filename=filename, |
| 22614 |
} |
| 22615 |
end |
| 22616 |
local specification=urlhashed(filename) |
| 22617 |
if not specification.scheme or specification.scheme=="" then |
| 22618 |
return { |
| 22619 |
scheme="file", |
| 22620 |
path=filename, |
| 22621 |
original=filename, |
| 22622 |
filename=filename, |
| 22623 |
} |
| 22624 |
else |
| 22625 |
return specification |
| 22626 |
end |
| 22627 |
end |
| 22628 |
resolvers.splitmethod=splitmethod |
| 22629 |
local function methodhandler(what,first,...) |
| 22630 |
local method=registered[what] |
| 22631 |
if method then |
| 22632 |
local how=method.how |
| 22633 |
local namespace=method.namespace |
| 22634 |
if how=="uri" or how=="url" then |
| 22635 |
local specification=splitmethod(first) |
| 22636 |
local scheme=specification.scheme |
| 22637 |
local resolver=namespace and namespace[scheme] |
| 22638 |
if resolver then |
| 22639 |
if trace_methods then |
| 22640 |
report_methods("resolving, method %a, how %a, handler %a, argument %a",what,how,scheme,first) |
| 22641 |
end |
| 22642 |
return resolver(specification,...) |
| 22643 |
else |
| 22644 |
resolver=namespace.default or namespace.file |
| 22645 |
if resolver then |
| 22646 |
if trace_methods then |
| 22647 |
report_methods("resolving, method %a, how %a, handler %a, argument %a",what,how,"default",first) |
| 22648 |
end |
| 22649 |
return resolver(specification,...) |
| 22650 |
elseif trace_methods then |
| 22651 |
report_methods("resolving, method %a, how %a, handler %a, argument %a",what,how,"unset") |
| 22652 |
end |
| 22653 |
end |
| 22654 |
elseif how=="tag" then |
| 22655 |
local resolver=namespace and namespace[first] |
| 22656 |
if resolver then |
| 22657 |
if trace_methods then |
| 22658 |
report_methods("resolving, method %a, how %a, tag %a",what,how,first) |
| 22659 |
end |
| 22660 |
return resolver(...) |
| 22661 |
else |
| 22662 |
resolver=namespace.default or namespace.file |
| 22663 |
if resolver then |
| 22664 |
if trace_methods then |
| 22665 |
report_methods("resolving, method %a, how %a, tag %a",what,how,"default") |
| 22666 |
end |
| 22667 |
return resolver(...) |
| 22668 |
elseif trace_methods then |
| 22669 |
report_methods("resolving, method %a, how %a, tag %a",what,how,"unset") |
| 22670 |
end |
| 22671 |
end |
| 22672 |
end |
| 22673 |
else |
| 22674 |
report_methods("resolving, invalid method %a") |
| 22675 |
end |
| 22676 |
end |
| 22677 |
resolvers.methodhandler=methodhandler |
| 22678 |
function resolvers.registermethod(name,namespace,how) |
| 22679 |
registered[name]={ |
| 22680 |
how=how or "tag", |
| 22681 |
namespace=namespace |
| 22682 |
} |
| 22683 |
namespace["byscheme"]=function(scheme,filename,...) |
| 22684 |
if scheme=="file" then |
| 22685 |
return methodhandler(name,filename,...) |
| 22686 |
else |
| 22687 |
return methodhandler(name,addurlscheme(filename,scheme),...) |
| 22688 |
end |
| 22689 |
end |
| 22690 |
end |
| 22691 |
local concatinators=allocate { notfound=joinfile } |
| 22692 |
local locators=allocate { notfound=function() end } |
| 22693 |
local hashers=allocate { notfound=function() end } |
| 22694 |
local generators=allocate { notfound=function() end } |
| 22695 |
resolvers.concatinators=concatinators |
| 22696 |
resolvers.locators=locators |
| 22697 |
resolvers.hashers=hashers |
| 22698 |
resolvers.generators=generators |
| 22699 |
local registermethod=resolvers.registermethod |
| 22700 |
registermethod("concatinators",concatinators,"tag") |
| 22701 |
registermethod("locators",locators,"uri") |
| 22702 |
registermethod("hashers",hashers,"uri") |
| 22703 |
registermethod("generators",generators,"uri") |
| 22704 |
|
| 22705 |
|
| 22706 |
end -- of closure |
| 22707 |
|
| 22708 |
do -- create closure to overcome 200 locals limit |
| 22709 |
|
| 22710 |
package.loaded["data-res"] = package.loaded["data-res"] or true |
| 22711 |
|
| 22712 |
-- original size: 70699, stripped down to: 45019 |
| 22713 |
|
| 22714 |
if not modules then modules={} end modules ['data-res']={ |
| 22715 |
version=1.001, |
| 22716 |
comment="companion to luat-lib.mkiv", |
| 22717 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 22718 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 22719 |
license="see context related readme files", |
| 22720 |
} |
| 22721 |
local gsub,find,lower,upper,match,gmatch=string.gsub,string.find,string.lower,string.upper,string.match,string.gmatch |
| 22722 |
local concat,insert,remove=table.concat,table.insert,table.remove |
| 22723 |
local next,type,rawget,loadfile=next,type,rawget,loadfile |
| 22724 |
local mergedtable=table.merged |
| 22725 |
local os=os |
| 22726 |
local P,S,R,C,Cc,Cs,Ct,Carg=lpeg.P,lpeg.S,lpeg.R,lpeg.C,lpeg.Cc,lpeg.Cs,lpeg.Ct,lpeg.Carg |
| 22727 |
local lpegmatch,lpegpatterns=lpeg.match,lpeg.patterns |
| 22728 |
local formatters=string.formatters |
| 22729 |
local filedirname=file.dirname |
| 22730 |
local filebasename=file.basename |
| 22731 |
local suffixonly=file.suffixonly |
| 22732 |
local addsuffix=file.addsuffix |
| 22733 |
local removesuffix=file.removesuffix |
| 22734 |
local filejoin=file.join |
| 22735 |
local collapsepath=file.collapsepath |
| 22736 |
local joinpath=file.joinpath |
| 22737 |
local is_qualified_path=file.is_qualified_path |
| 22738 |
local allocate=utilities.storage.allocate |
| 22739 |
local settings_to_array=utilities.parsers.settings_to_array |
| 22740 |
local urlhasscheme=url.hasscheme |
| 22741 |
local getcurrentdir=lfs.currentdir |
| 22742 |
local isfile=lfs.isfile |
| 22743 |
local isdir=lfs.isdir |
| 22744 |
local setmetatableindex=table.setmetatableindex |
| 22745 |
local luasuffixes=utilities.lua.suffixes |
| 22746 |
local trace_locating=false trackers .register("resolvers.locating",function(v) trace_locating=v end) |
| 22747 |
local trace_details=false trackers .register("resolvers.details",function(v) trace_details=v end) |
| 22748 |
local trace_expansions=false trackers .register("resolvers.expansions",function(v) trace_expansions=v end) |
| 22749 |
local trace_paths=false trackers .register("resolvers.paths",function(v) trace_paths=v end) |
| 22750 |
local resolve_otherwise=true directives.register("resolvers.otherwise",function(v) resolve_otherwise=v end) |
| 22751 |
local report_resolving=logs.reporter("resolvers","resolving") |
| 22752 |
local resolvers=resolvers |
| 22753 |
local expandedpathfromlist=resolvers.expandedpathfromlist |
| 22754 |
local checkedvariable=resolvers.checkedvariable |
| 22755 |
local splitconfigurationpath=resolvers.splitconfigurationpath |
| 22756 |
local methodhandler=resolvers.methodhandler |
| 22757 |
local filtered=resolvers.filtered_from_content |
| 22758 |
local lookup=resolvers.get_from_content |
| 22759 |
local cleanpath=resolvers.cleanpath |
| 22760 |
local resolveprefix=resolvers.resolve |
| 22761 |
local initializesetter=utilities.setters.initialize |
| 22762 |
local ostype,osname,osenv,ossetenv,osgetenv=os.type,os.name,os.env,os.setenv,os.getenv |
| 22763 |
resolvers.cacheversion="1.100" |
| 22764 |
resolvers.configbanner="" |
| 22765 |
resolvers.homedir=environment.homedir |
| 22766 |
resolvers.luacnfname="texmfcnf.lua" |
| 22767 |
resolvers.luacnffallback="contextcnf.lua" |
| 22768 |
resolvers.luacnfstate="unknown" |
| 22769 |
local criticalvars={ |
| 22770 |
"SELFAUTOLOC", |
| 22771 |
"SELFAUTODIR", |
| 22772 |
"SELFAUTOPARENT", |
| 22773 |
"TEXMFCNF", |
| 22774 |
"TEXMF", |
| 22775 |
"TEXOS", |
| 22776 |
} |
| 22777 |
do |
| 22778 |
local texroot=environment.texroot |
| 22779 |
resolvers.luacnfspec={ |
| 22780 |
"home:texmf/web2c", |
| 22781 |
"selfautoparent:/texmf-local/web2c", |
| 22782 |
"selfautoparent:/texmf-context/web2c", |
| 22783 |
"selfautoparent:/texmf/web2c", |
| 22784 |
} |
| 22785 |
if environment.default_texmfcnf then |
| 22786 |
resolvers.luacnfspec={ |
| 22787 |
"home:texmf/web2c", |
| 22788 |
environment.default_texmfcnf, |
| 22789 |
} |
| 22790 |
elseif texroot and isdir(texroot.."/texmf-context") then |
| 22791 |
elseif texroot and isdir(texroot.."/texmf-dist") then |
| 22792 |
resolvers.luacnfspec={ |
| 22793 |
"home:texmf/web2c", |
| 22794 |
"selfautoparent:/texmf-local/web2c", |
| 22795 |
"selfautoparent:", |
| 22796 |
"selfautoparent:/texmf-dist/web2c", |
| 22797 |
"selfautoparent:/texmf/web2c", |
| 22798 |
} |
| 22799 |
elseif ostype~="windows" and isdir("/etc/texmf/web2c") then |
| 22800 |
resolvers.luacnfspec={ |
| 22801 |
"home:texmf/web2c", |
| 22802 |
"/etc/texmf/web2c", |
| 22803 |
"selfautodir:/share/texmf/web2c", |
| 22804 |
} |
| 22805 |
else |
| 22806 |
end |
| 22807 |
resolvers.luacnfspec=concat(resolvers.luacnfspec,";") |
| 22808 |
end |
| 22809 |
local unset_variable="unset" |
| 22810 |
local formats=resolvers.formats |
| 22811 |
local suffixes=resolvers.suffixes |
| 22812 |
local usertypes=resolvers.usertypes |
| 22813 |
local dangerous=resolvers.dangerous |
| 22814 |
local suffixmap=resolvers.suffixmap |
| 22815 |
resolvers.defaultsuffixes={ "tex" } |
| 22816 |
local instance=nil |
| 22817 |
local variable |
| 22818 |
local expansion |
| 22819 |
local setenv |
| 22820 |
local getenv |
| 22821 |
local formatofsuffix=resolvers.formatofsuffix |
| 22822 |
local splitpath=resolvers.splitpath |
| 22823 |
local splitmethod=resolvers.splitmethod |
| 22824 |
setenv=function(key,value,raw) |
| 22825 |
if instance then |
| 22826 |
instance.environment[key]=value |
| 22827 |
ossetenv(key,raw and value or resolveprefix(value)) |
| 22828 |
end |
| 22829 |
end |
| 22830 |
getenv=function(key) |
| 22831 |
local value=rawget(instance.environment,key) |
| 22832 |
if value and value~="" then |
| 22833 |
return value |
| 22834 |
else |
| 22835 |
local e=osgetenv(key) |
| 22836 |
return e~=nil and e~="" and checkedvariable(e) or "" |
| 22837 |
end |
| 22838 |
end |
| 22839 |
resolvers.getenv=getenv |
| 22840 |
resolvers.setenv=setenv |
| 22841 |
local dollarstripper=lpeg.stripper("$") |
| 22842 |
local inhibitstripper=P("!")^0*Cs(P(1)^0) |
| 22843 |
local expandedvariable,resolvedvariable do |
| 22844 |
local function resolveinstancevariable(k) |
| 22845 |
return instance.expansions[k] |
| 22846 |
end |
| 22847 |
local p_variable=P("$")/"" |
| 22848 |
local p_key=C(R("az","AZ","09","__","--")^1) |
| 22849 |
local p_whatever=P(";")*((1-S("!{}/\\"))^1*P(";")/"")+P(";")*(P(";")/"")+P(1) |
| 22850 |
local variableexpander=Cs((p_variable*(p_key/resolveinstancevariable)+p_whatever)^1 ) |
| 22851 |
local p_cleaner=P("\\")/"/"+P(";")*S("!{}/\\")^0*P(";")^1/";" |
| 22852 |
local variablecleaner=Cs((p_cleaner+P(1))^0) |
| 22853 |
local p_variable=R("az","AZ","09","__","--")^1/resolveinstancevariable |
| 22854 |
local p_variable=(P("$")/"")*(p_variable+(P("{")/"")*p_variable*(P("}")/"")) |
| 22855 |
local variableresolver=Cs((p_variable+P(1))^0) |
| 22856 |
expandedvariable=function(var) |
| 22857 |
return lpegmatch(variableexpander,var) or var |
| 22858 |
end |
| 22859 |
function resolvers.reset() |
| 22860 |
if trace_locating then |
| 22861 |
report_resolving("creating instance") |
| 22862 |
end |
| 22863 |
local environment={} |
| 22864 |
local variables={} |
| 22865 |
local expansions={} |
| 22866 |
local order={} |
| 22867 |
instance={ |
| 22868 |
environment=environment, |
| 22869 |
variables=variables, |
| 22870 |
expansions=expansions, |
| 22871 |
order=order, |
| 22872 |
files={}, |
| 22873 |
setups={}, |
| 22874 |
found={}, |
| 22875 |
foundintrees={}, |
| 22876 |
hashes={}, |
| 22877 |
hashed={}, |
| 22878 |
pathlists=false, |
| 22879 |
specification={}, |
| 22880 |
lists={}, |
| 22881 |
data={}, |
| 22882 |
fakepaths={}, |
| 22883 |
remember=true, |
| 22884 |
diskcache=true, |
| 22885 |
renewcache=false, |
| 22886 |
renewtree=false, |
| 22887 |
loaderror=false, |
| 22888 |
savelists=true, |
| 22889 |
pattern=nil, |
| 22890 |
force_suffixes=true, |
| 22891 |
pathstack={}, |
| 22892 |
} |
| 22893 |
setmetatableindex(variables,function(t,k) |
| 22894 |
local v |
| 22895 |
for i=1,#order do |
| 22896 |
v=order[i][k] |
| 22897 |
if v~=nil then |
| 22898 |
t[k]=v |
| 22899 |
return v |
| 22900 |
end |
| 22901 |
end |
| 22902 |
if v==nil then |
| 22903 |
v="" |
| 22904 |
end |
| 22905 |
t[k]=v |
| 22906 |
return v |
| 22907 |
end) |
| 22908 |
local repath=resolvers.repath |
| 22909 |
setmetatableindex(environment,function(t,k) |
| 22910 |
local v=osgetenv(k) |
| 22911 |
if v==nil then |
| 22912 |
v=variables[k] |
| 22913 |
end |
| 22914 |
if v~=nil then |
| 22915 |
v=checkedvariable(v) or "" |
| 22916 |
end |
| 22917 |
v=repath(v) |
| 22918 |
t[k]=v |
| 22919 |
return v |
| 22920 |
end) |
| 22921 |
setmetatableindex(expansions,function(t,k) |
| 22922 |
local v=environment[k] |
| 22923 |
if type(v)=="string" then |
| 22924 |
v=lpegmatch(variableresolver,v) |
| 22925 |
v=lpegmatch(variablecleaner,v) |
| 22926 |
end |
| 22927 |
t[k]=v |
| 22928 |
return v |
| 22929 |
end) |
| 22930 |
end |
| 22931 |
end |
| 22932 |
function resolvers.initialized() |
| 22933 |
return instance~=nil |
| 22934 |
end |
| 22935 |
local function reset_hashes() |
| 22936 |
instance.lists={} |
| 22937 |
instance.pathlists=false |
| 22938 |
instance.found={} |
| 22939 |
end |
| 22940 |
local function reset_caches() |
| 22941 |
instance.lists={} |
| 22942 |
instance.pathlists=false |
| 22943 |
end |
| 22944 |
local makepathexpression do |
| 22945 |
local slash=P("/") |
| 22946 |
local pathexpressionpattern=Cs ( |
| 22947 |
Cc("^")*( |
| 22948 |
Cc("%")*S(".-")+slash^2*P(-1)/"/.*" |
| 22949 |
+slash^2/"/"+(1-slash)*P(-1)*Cc("/")+P(1) |
| 22950 |
)^1*Cc("$") |
| 22951 |
) |
| 22952 |
local cache={} |
| 22953 |
makepathexpression=function(str) |
| 22954 |
if str=="." then |
| 22955 |
return "^%./$" |
| 22956 |
else |
| 22957 |
local c=cache[str] |
| 22958 |
if not c then |
| 22959 |
c=lpegmatch(pathexpressionpattern,str) |
| 22960 |
cache[str]=c |
| 22961 |
end |
| 22962 |
return c |
| 22963 |
end |
| 22964 |
end |
| 22965 |
end |
| 22966 |
local function reportcriticalvariables(cnfspec) |
| 22967 |
if trace_locating then |
| 22968 |
for i=1,#criticalvars do |
| 22969 |
local k=criticalvars[i] |
| 22970 |
local v=getenv(k) or "unknown" |
| 22971 |
report_resolving("variable %a set to %a",k,v) |
| 22972 |
end |
| 22973 |
report_resolving() |
| 22974 |
if cnfspec then |
| 22975 |
report_resolving("using configuration specification %a",type(cnfspec)=="table" and concat(cnfspec,",") or cnfspec) |
| 22976 |
end |
| 22977 |
report_resolving() |
| 22978 |
end |
| 22979 |
reportcriticalvariables=function() end |
| 22980 |
end |
| 22981 |
local function identify_configuration_files() |
| 22982 |
local specification=instance.specification |
| 22983 |
if #specification==0 then |
| 22984 |
local cnfspec=getenv("TEXMFCNF") |
| 22985 |
if cnfspec=="" then |
| 22986 |
cnfspec=resolvers.luacnfspec |
| 22987 |
resolvers.luacnfstate="default" |
| 22988 |
else |
| 22989 |
resolvers.luacnfstate="environment" |
| 22990 |
end |
| 22991 |
reportcriticalvariables(cnfspec) |
| 22992 |
local cnfpaths=expandedpathfromlist(splitpath(cnfspec)) |
| 22993 |
local function locatecnf(luacnfname,kind) |
| 22994 |
for i=1,#cnfpaths do |
| 22995 |
local filepath=cnfpaths[i] |
| 22996 |
local filename=collapsepath(filejoin(filepath,luacnfname)) |
| 22997 |
local realname=resolveprefix(filename) |
| 22998 |
if trace_locating then |
| 22999 |
local fullpath=gsub(resolveprefix(collapsepath(filepath)),"//","/") |
| 23000 |
local weirdpath=find(fullpath,"/texmf.+/texmf") or not find(fullpath,"/web2c",1,true) |
| 23001 |
report_resolving("looking for %s %a on %s path %a from specification %a", |
| 23002 |
kind,luacnfname,weirdpath and "weird" or "given",fullpath,filepath) |
| 23003 |
end |
| 23004 |
if isfile(realname) then |
| 23005 |
specification[#specification+1]=filename |
| 23006 |
if trace_locating then |
| 23007 |
report_resolving("found %s configuration file %a",kind,realname) |
| 23008 |
end |
| 23009 |
end |
| 23010 |
end |
| 23011 |
end |
| 23012 |
locatecnf(resolvers.luacnfname,"regular") |
| 23013 |
if #specification==0 then |
| 23014 |
locatecnf(resolvers.luacnffallback,"fallback") |
| 23015 |
end |
| 23016 |
if trace_locating then |
| 23017 |
report_resolving() |
| 23018 |
end |
| 23019 |
elseif trace_locating then |
| 23020 |
report_resolving("configuration files already identified") |
| 23021 |
end |
| 23022 |
end |
| 23023 |
local function load_configuration_files() |
| 23024 |
local specification=instance.specification |
| 23025 |
local setups=instance.setups |
| 23026 |
local order=instance.order |
| 23027 |
if #specification>0 then |
| 23028 |
local luacnfname=resolvers.luacnfname |
| 23029 |
for i=1,#specification do |
| 23030 |
local filename=specification[i] |
| 23031 |
local pathname=filedirname(filename) |
| 23032 |
local filename=filejoin(pathname,luacnfname) |
| 23033 |
local realname=resolveprefix(filename) |
| 23034 |
local blob=loadfile(realname) |
| 23035 |
if blob then |
| 23036 |
local data=blob() |
| 23037 |
local parent=data and data.parent |
| 23038 |
if parent then |
| 23039 |
local filename=filejoin(pathname,parent) |
| 23040 |
local realname=resolveprefix(filename) |
| 23041 |
local blob=loadfile(realname) |
| 23042 |
if blob then |
| 23043 |
local parentdata=blob() |
| 23044 |
if parentdata then |
| 23045 |
report_resolving("loading configuration file %a",filename) |
| 23046 |
data=mergedtable(parentdata,data) |
| 23047 |
end |
| 23048 |
end |
| 23049 |
end |
| 23050 |
data=data and data.content |
| 23051 |
if data then |
| 23052 |
if trace_locating then |
| 23053 |
report_resolving("loading configuration file %a",filename) |
| 23054 |
report_resolving() |
| 23055 |
end |
| 23056 |
local variables=data.variables or {} |
| 23057 |
local warning=false |
| 23058 |
for k,v in next,data do |
| 23059 |
local variant=type(v) |
| 23060 |
if variant=="table" then |
| 23061 |
initializesetter(filename,k,v) |
| 23062 |
elseif variables[k]==nil then |
| 23063 |
if trace_locating and not warning then |
| 23064 |
report_resolving("variables like %a in configuration file %a should move to the 'variables' subtable", |
| 23065 |
k,resolveprefix(filename)) |
| 23066 |
warning=true |
| 23067 |
end |
| 23068 |
variables[k]=v |
| 23069 |
end |
| 23070 |
end |
| 23071 |
setups[pathname]=variables |
| 23072 |
if resolvers.luacnfstate=="default" then |
| 23073 |
local cnfspec=variables["TEXMFCNF"] |
| 23074 |
if cnfspec then |
| 23075 |
if trace_locating then |
| 23076 |
report_resolving("reloading configuration due to TEXMF redefinition") |
| 23077 |
end |
| 23078 |
setenv("TEXMFCNF",cnfspec) |
| 23079 |
instance.specification={} |
| 23080 |
identify_configuration_files() |
| 23081 |
load_configuration_files() |
| 23082 |
resolvers.luacnfstate="configuration" |
| 23083 |
break |
| 23084 |
end |
| 23085 |
end |
| 23086 |
else |
| 23087 |
if trace_locating then |
| 23088 |
report_resolving("skipping configuration file %a (no content)",filename) |
| 23089 |
end |
| 23090 |
setups[pathname]={} |
| 23091 |
instance.loaderror=true |
| 23092 |
end |
| 23093 |
elseif trace_locating then |
| 23094 |
report_resolving("skipping configuration file %a (no valid format)",filename) |
| 23095 |
end |
| 23096 |
order[#order+1]=setups[pathname] |
| 23097 |
if instance.loaderror then |
| 23098 |
break |
| 23099 |
end |
| 23100 |
end |
| 23101 |
elseif trace_locating then |
| 23102 |
report_resolving("warning: no lua configuration files found") |
| 23103 |
end |
| 23104 |
end |
| 23105 |
local expandedpathlist |
| 23106 |
local unexpandedpathlist |
| 23107 |
function resolvers.configurationfiles() |
| 23108 |
return instance.specification or {} |
| 23109 |
end |
| 23110 |
local function load_file_databases() |
| 23111 |
instance.loaderror=false |
| 23112 |
instance.files={} |
| 23113 |
if not instance.renewcache then |
| 23114 |
local hashes=instance.hashes |
| 23115 |
for k=1,#hashes do |
| 23116 |
local hash=hashes[k] |
| 23117 |
resolvers.hashers.byscheme(hash.type,hash.name) |
| 23118 |
if instance.loaderror then break end |
| 23119 |
end |
| 23120 |
end |
| 23121 |
end |
| 23122 |
local function locate_file_databases() |
| 23123 |
local texmfpaths=expandedpathlist("TEXMF") |
| 23124 |
if #texmfpaths>0 then |
| 23125 |
for i=1,#texmfpaths do |
| 23126 |
local path=collapsepath(texmfpaths[i]) |
| 23127 |
path=gsub(path,"/+$","") |
| 23128 |
local stripped=lpegmatch(inhibitstripper,path) |
| 23129 |
if stripped~="" then |
| 23130 |
local runtime=stripped==path |
| 23131 |
path=cleanpath(path) |
| 23132 |
local spec=splitmethod(stripped) |
| 23133 |
if runtime and (spec.noscheme or spec.scheme=="file") then |
| 23134 |
stripped="tree:///"..stripped |
| 23135 |
elseif spec.scheme=="cache" or spec.scheme=="file" then |
| 23136 |
stripped=spec.path |
| 23137 |
end |
| 23138 |
if trace_locating then |
| 23139 |
if runtime then |
| 23140 |
report_resolving("locating list of %a (runtime) (%s)",path,stripped) |
| 23141 |
else |
| 23142 |
report_resolving("locating list of %a (cached)",path) |
| 23143 |
end |
| 23144 |
end |
| 23145 |
methodhandler('locators',stripped) |
| 23146 |
end |
| 23147 |
end |
| 23148 |
if trace_locating then |
| 23149 |
report_resolving() |
| 23150 |
end |
| 23151 |
elseif trace_locating then |
| 23152 |
report_resolving("no texmf paths are defined (using TEXMF)") |
| 23153 |
end |
| 23154 |
end |
| 23155 |
local function generate_file_databases() |
| 23156 |
local hashes=instance.hashes |
| 23157 |
for k=1,#hashes do |
| 23158 |
local hash=hashes[k] |
| 23159 |
methodhandler('generators',hash.name) |
| 23160 |
end |
| 23161 |
if trace_locating then |
| 23162 |
report_resolving() |
| 23163 |
end |
| 23164 |
end |
| 23165 |
local function save_file_databases() |
| 23166 |
local hashes=instance.hashes |
| 23167 |
local files=instance.files |
| 23168 |
for i=1,#hashes do |
| 23169 |
local hash=hashes[i] |
| 23170 |
local cachename=hash.name |
| 23171 |
if hash.cache then |
| 23172 |
local content=files[cachename] |
| 23173 |
caches.collapsecontent(content) |
| 23174 |
if trace_locating then |
| 23175 |
report_resolving("saving tree %a",cachename) |
| 23176 |
end |
| 23177 |
caches.savecontent(cachename,"files",content) |
| 23178 |
elseif trace_locating then |
| 23179 |
report_resolving("not saving runtime tree %a",cachename) |
| 23180 |
end |
| 23181 |
end |
| 23182 |
end |
| 23183 |
function resolvers.renew(hashname) |
| 23184 |
local files=instance.files |
| 23185 |
if hashname and hashname~="" then |
| 23186 |
local expanded=expansion(hashname) or "" |
| 23187 |
if expanded~="" then |
| 23188 |
if trace_locating then |
| 23189 |
report_resolving("identifying tree %a from %a",expanded,hashname) |
| 23190 |
end |
| 23191 |
hashname=expanded |
| 23192 |
else |
| 23193 |
if trace_locating then |
| 23194 |
report_resolving("identifying tree %a",hashname) |
| 23195 |
end |
| 23196 |
end |
| 23197 |
local realpath=resolveprefix(hashname) |
| 23198 |
if isdir(realpath) then |
| 23199 |
if trace_locating then |
| 23200 |
report_resolving("using path %a",realpath) |
| 23201 |
end |
| 23202 |
methodhandler('generators',hashname) |
| 23203 |
local content=files[hashname] |
| 23204 |
caches.collapsecontent(content) |
| 23205 |
if trace_locating then |
| 23206 |
report_resolving("saving tree %a",hashname) |
| 23207 |
end |
| 23208 |
caches.savecontent(hashname,"files",content) |
| 23209 |
else |
| 23210 |
report_resolving("invalid path %a",realpath) |
| 23211 |
end |
| 23212 |
end |
| 23213 |
end |
| 23214 |
local function load_databases() |
| 23215 |
locate_file_databases() |
| 23216 |
if instance.diskcache and not instance.renewcache then |
| 23217 |
load_file_databases() |
| 23218 |
if instance.loaderror then |
| 23219 |
generate_file_databases() |
| 23220 |
save_file_databases() |
| 23221 |
end |
| 23222 |
else |
| 23223 |
generate_file_databases() |
| 23224 |
if instance.renewcache then |
| 23225 |
save_file_databases() |
| 23226 |
end |
| 23227 |
end |
| 23228 |
end |
| 23229 |
function resolvers.appendhash(type,name,cache) |
| 23230 |
local hashed=instance.hashed |
| 23231 |
local hashes=instance.hashes |
| 23232 |
if hashed[name] then |
| 23233 |
else |
| 23234 |
if trace_locating then |
| 23235 |
report_resolving("hash %a appended",name) |
| 23236 |
end |
| 23237 |
insert(hashes,{ type=type,name=name,cache=cache } ) |
| 23238 |
hashed[name]=cache |
| 23239 |
end |
| 23240 |
end |
| 23241 |
function resolvers.prependhash(type,name,cache) |
| 23242 |
local hashed=instance.hashed |
| 23243 |
local hashes=instance.hashes |
| 23244 |
if hashed[name] then |
| 23245 |
else |
| 23246 |
if trace_locating then |
| 23247 |
report_resolving("hash %a prepended",name) |
| 23248 |
end |
| 23249 |
insert(hashes,1,{ type=type,name=name,cache=cache } ) |
| 23250 |
hashed[name]=cache |
| 23251 |
end |
| 23252 |
end |
| 23253 |
function resolvers.extendtexmfvariable(specification) |
| 23254 |
local environment=instance.environment |
| 23255 |
local variables=instance.variables |
| 23256 |
local texmftrees=splitpath(getenv("TEXMF")) |
| 23257 |
insert(texmftrees,1,specification) |
| 23258 |
texmftrees=concat(texmftrees,",") |
| 23259 |
if environment["TEXMF"] then |
| 23260 |
environment["TEXMF"]=texmftrees |
| 23261 |
elseif variables["TEXMF"] then |
| 23262 |
variables["TEXMF"]=texmftrees |
| 23263 |
else |
| 23264 |
end |
| 23265 |
reset_hashes() |
| 23266 |
end |
| 23267 |
function resolvers.splitexpansions() |
| 23268 |
local expansions=instance.expansions |
| 23269 |
for k,v in next,expansions do |
| 23270 |
local t,tn,h,p={},0,{},splitconfigurationpath(v) |
| 23271 |
for kk=1,#p do |
| 23272 |
local vv=p[kk] |
| 23273 |
if vv~="" and not h[vv] then |
| 23274 |
tn=tn+1 |
| 23275 |
t[tn]=vv |
| 23276 |
h[vv]=true |
| 23277 |
end |
| 23278 |
end |
| 23279 |
if tn>1 then |
| 23280 |
expansions[k]=t |
| 23281 |
else |
| 23282 |
expansions[k]=t[1] |
| 23283 |
end |
| 23284 |
end |
| 23285 |
end |
| 23286 |
function resolvers.datastate() |
| 23287 |
return caches.contentstate() |
| 23288 |
end |
| 23289 |
variable=function(name) |
| 23290 |
local variables=instance.variables |
| 23291 |
local name=name and lpegmatch(dollarstripper,name) |
| 23292 |
local result=name and variables[name] |
| 23293 |
return result~=nil and result or "" |
| 23294 |
end |
| 23295 |
expansion=function(name) |
| 23296 |
local expansions=instance.expansions |
| 23297 |
local name=name and lpegmatch(dollarstripper,name) |
| 23298 |
local result=name and expansions[name] |
| 23299 |
return result~=nil and result or "" |
| 23300 |
end |
| 23301 |
resolvers.variable=variable |
| 23302 |
resolvers.expansion=expansion |
| 23303 |
unexpandedpathlist=function(str) |
| 23304 |
local pth=variable(str) |
| 23305 |
local lst=splitpath(pth) |
| 23306 |
return expandedpathfromlist(lst) |
| 23307 |
end |
| 23308 |
function resolvers.unexpandedpath(str) |
| 23309 |
return joinpath(unexpandedpathlist(str)) |
| 23310 |
end |
| 23311 |
function resolvers.pushpath(name) |
| 23312 |
local pathstack=instance.pathstack |
| 23313 |
local lastpath=pathstack[#pathstack] |
| 23314 |
local pluspath=filedirname(name) |
| 23315 |
if lastpath then |
| 23316 |
lastpath=collapsepath(filejoin(lastpath,pluspath)) |
| 23317 |
else |
| 23318 |
lastpath=collapsepath(pluspath) |
| 23319 |
end |
| 23320 |
insert(pathstack,lastpath) |
| 23321 |
if trace_paths then |
| 23322 |
report_resolving("pushing path %a",lastpath) |
| 23323 |
end |
| 23324 |
end |
| 23325 |
function resolvers.poppath() |
| 23326 |
local pathstack=instance.pathstack |
| 23327 |
if trace_paths and #pathstack>0 then |
| 23328 |
report_resolving("popping path %a",pathstack[#pathstack]) |
| 23329 |
end |
| 23330 |
remove(pathstack) |
| 23331 |
end |
| 23332 |
function resolvers.stackpath() |
| 23333 |
local pathstack=instance.pathstack |
| 23334 |
local currentpath=pathstack[#pathstack] |
| 23335 |
return currentpath~="" and currentpath or nil |
| 23336 |
end |
| 23337 |
local done={} |
| 23338 |
function resolvers.resetextrapaths() |
| 23339 |
local extra_paths=instance.extra_paths |
| 23340 |
if not extra_paths then |
| 23341 |
done={} |
| 23342 |
instance.extra_paths={} |
| 23343 |
elseif #ep>0 then |
| 23344 |
done={} |
| 23345 |
reset_caches() |
| 23346 |
end |
| 23347 |
end |
| 23348 |
function resolvers.getextrapaths() |
| 23349 |
return instance.extra_paths or {} |
| 23350 |
end |
| 23351 |
function resolvers.registerextrapath(paths,subpaths) |
| 23352 |
if not subpaths or subpaths=="" then |
| 23353 |
if not paths or path=="" then |
| 23354 |
return |
| 23355 |
elseif done[paths] then |
| 23356 |
return |
| 23357 |
end |
| 23358 |
end |
| 23359 |
local paths=settings_to_array(paths) |
| 23360 |
local subpaths=settings_to_array(subpaths) |
| 23361 |
local extra_paths=instance.extra_paths or {} |
| 23362 |
local oldn=#extra_paths |
| 23363 |
local newn=oldn |
| 23364 |
local nofpaths=#paths |
| 23365 |
local nofsubpaths=#subpaths |
| 23366 |
if nofpaths>0 then |
| 23367 |
if nofsubpaths>0 then |
| 23368 |
for i=1,nofpaths do |
| 23369 |
local p=paths[i] |
| 23370 |
for j=1,nofsubpaths do |
| 23371 |
local s=subpaths[j] |
| 23372 |
local ps=p.."/"..s |
| 23373 |
if not done[ps] then |
| 23374 |
newn=newn+1 |
| 23375 |
extra_paths[newn]=cleanpath(ps) |
| 23376 |
done[ps]=true |
| 23377 |
end |
| 23378 |
end |
| 23379 |
end |
| 23380 |
else |
| 23381 |
for i=1,nofpaths do |
| 23382 |
local p=paths[i] |
| 23383 |
if not done[p] then |
| 23384 |
newn=newn+1 |
| 23385 |
extra_paths[newn]=cleanpath(p) |
| 23386 |
done[p]=true |
| 23387 |
end |
| 23388 |
end |
| 23389 |
end |
| 23390 |
elseif nofsubpaths>0 then |
| 23391 |
for i=1,oldn do |
| 23392 |
for j=1,nofsubpaths do |
| 23393 |
local s=subpaths[j] |
| 23394 |
local ps=extra_paths[i].."/"..s |
| 23395 |
if not done[ps] then |
| 23396 |
newn=newn+1 |
| 23397 |
extra_paths[newn]=cleanpath(ps) |
| 23398 |
done[ps]=true |
| 23399 |
end |
| 23400 |
end |
| 23401 |
end |
| 23402 |
end |
| 23403 |
if newn>0 then |
| 23404 |
instance.extra_paths=extra_paths |
| 23405 |
end |
| 23406 |
if newn~=oldn then |
| 23407 |
reset_caches() |
| 23408 |
end |
| 23409 |
end |
| 23410 |
function resolvers.pushextrapath(path) |
| 23411 |
local paths=settings_to_array(path) |
| 23412 |
local extra_stack=instance.extra_stack |
| 23413 |
if extra_stack then |
| 23414 |
insert(extra_stack,1,paths) |
| 23415 |
else |
| 23416 |
instance.extra_stack={ paths } |
| 23417 |
end |
| 23418 |
reset_caches() |
| 23419 |
end |
| 23420 |
function resolvers.popextrapath() |
| 23421 |
local extra_stack=instance.extra_stack |
| 23422 |
if extra_stack then |
| 23423 |
reset_caches() |
| 23424 |
return remove(extra_stack,1) |
| 23425 |
end |
| 23426 |
end |
| 23427 |
local function made_list(instance,list,extra_too) |
| 23428 |
local done={} |
| 23429 |
local new={} |
| 23430 |
local newn=0 |
| 23431 |
local function add(p) |
| 23432 |
for k=1,#p do |
| 23433 |
local v=p[k] |
| 23434 |
if not done[v] then |
| 23435 |
done[v]=true |
| 23436 |
newn=newn+1 |
| 23437 |
new[newn]=v |
| 23438 |
end |
| 23439 |
end |
| 23440 |
end |
| 23441 |
for k=1,#list do |
| 23442 |
local v=list[k] |
| 23443 |
if done[v] then |
| 23444 |
elseif find(v,"^[%.%/]$") then |
| 23445 |
done[v]=true |
| 23446 |
newn=newn+1 |
| 23447 |
new[newn]=v |
| 23448 |
else |
| 23449 |
break |
| 23450 |
end |
| 23451 |
end |
| 23452 |
if extra_too then |
| 23453 |
local extra_stack=instance.extra_stack |
| 23454 |
local extra_paths=instance.extra_paths |
| 23455 |
if extra_stack and #extra_stack>0 then |
| 23456 |
for k=1,#extra_stack do |
| 23457 |
add(extra_stack[k]) |
| 23458 |
end |
| 23459 |
end |
| 23460 |
if extra_paths and #extra_paths>0 then |
| 23461 |
add(extra_paths) |
| 23462 |
end |
| 23463 |
end |
| 23464 |
add(list) |
| 23465 |
return new |
| 23466 |
end |
| 23467 |
expandedpathlist=function(str,extra_too) |
| 23468 |
if not str then |
| 23469 |
return {} |
| 23470 |
elseif instance.savelists then |
| 23471 |
str=lpegmatch(dollarstripper,str) |
| 23472 |
local lists=instance.lists |
| 23473 |
local lst=lists[str] |
| 23474 |
if not lst then |
| 23475 |
local l=made_list(instance,splitpath(expansion(str)),extra_too) |
| 23476 |
lst=expandedpathfromlist(l) |
| 23477 |
lists[str]=lst |
| 23478 |
end |
| 23479 |
return lst |
| 23480 |
else |
| 23481 |
local lst=splitpath(expansion(str)) |
| 23482 |
return made_list(instance,expandedpathfromlist(lst),extra_too) |
| 23483 |
end |
| 23484 |
end |
| 23485 |
resolvers.expandedpathlist=expandedpathlist |
| 23486 |
resolvers.unexpandedpathlist=unexpandedpathlist |
| 23487 |
function resolvers.cleanpathlist(str) |
| 23488 |
local t=expandedpathlist(str) |
| 23489 |
if t then |
| 23490 |
for i=1,#t do |
| 23491 |
t[i]=collapsepath(cleanpath(t[i])) |
| 23492 |
end |
| 23493 |
end |
| 23494 |
return t |
| 23495 |
end |
| 23496 |
function resolvers.expandpath(str) |
| 23497 |
return joinpath(expandedpathlist(str)) |
| 23498 |
end |
| 23499 |
local function expandedpathlistfromvariable(str) |
| 23500 |
str=lpegmatch(dollarstripper,str) |
| 23501 |
local tmp=resolvers.variableofformatorsuffix(str) |
| 23502 |
return expandedpathlist(tmp~="" and tmp or str) |
| 23503 |
end |
| 23504 |
function resolvers.expandpathfromvariable(str) |
| 23505 |
return joinpath(expandedpathlistfromvariable(str)) |
| 23506 |
end |
| 23507 |
resolvers.expandedpathlistfromvariable=expandedpathlistfromvariable |
| 23508 |
function resolvers.cleanedpathlist(v) |
| 23509 |
local t=expandedpathlist(v) |
| 23510 |
for i=1,#t do |
| 23511 |
t[i]=resolveprefix(cleanpath(t[i])) |
| 23512 |
end |
| 23513 |
return t |
| 23514 |
end |
| 23515 |
function resolvers.expandbraces(str) |
| 23516 |
local pth=expandedpathfromlist(splitpath(str)) |
| 23517 |
return joinpath(pth) |
| 23518 |
end |
| 23519 |
function resolvers.registerfilehash(name,content,someerror) |
| 23520 |
local files=instance.files |
| 23521 |
if content then |
| 23522 |
files[name]=content |
| 23523 |
else |
| 23524 |
files[name]={} |
| 23525 |
if somerror==true then |
| 23526 |
instance.loaderror=someerror |
| 23527 |
end |
| 23528 |
end |
| 23529 |
end |
| 23530 |
function resolvers.getfilehashes() |
| 23531 |
return instance and instance.files or {} |
| 23532 |
end |
| 23533 |
function resolvers.gethashes() |
| 23534 |
return instance and instance.hashes or {} |
| 23535 |
end |
| 23536 |
function resolvers.renewcache() |
| 23537 |
if instance then |
| 23538 |
instance.renewcache=true |
| 23539 |
end |
| 23540 |
end |
| 23541 |
local function isreadable(name) |
| 23542 |
local readable=isfile(name) |
| 23543 |
if trace_details then |
| 23544 |
if readable then |
| 23545 |
report_resolving("file %a is readable",name) |
| 23546 |
else |
| 23547 |
report_resolving("file %a is not readable",name) |
| 23548 |
end |
| 23549 |
end |
| 23550 |
return readable |
| 23551 |
end |
| 23552 |
local function collect_files(names) |
| 23553 |
local filelist={} |
| 23554 |
local noffiles=0 |
| 23555 |
local function check(hash,root,pathname,path,basename,name) |
| 23556 |
if not pathname or find(path,pathname) then |
| 23557 |
local variant=hash.type |
| 23558 |
local search=filejoin(root,path,name) |
| 23559 |
local result=methodhandler('concatinators',variant,root,path,name) |
| 23560 |
if trace_details then |
| 23561 |
report_resolving("match: variant %a, search %a, result %a",variant,search,result) |
| 23562 |
end |
| 23563 |
noffiles=noffiles+1 |
| 23564 |
filelist[noffiles]={ variant,search,result } |
| 23565 |
end |
| 23566 |
end |
| 23567 |
for k=1,#names do |
| 23568 |
local filename=names[k] |
| 23569 |
if trace_details then |
| 23570 |
report_resolving("checking name %a",filename) |
| 23571 |
end |
| 23572 |
local basename=filebasename(filename) |
| 23573 |
local pathname=filedirname(filename) |
| 23574 |
if pathname=="" or find(pathname,"^%.") then |
| 23575 |
pathname=false |
| 23576 |
else |
| 23577 |
pathname=gsub(pathname,"%*",".*") |
| 23578 |
pathname="/"..pathname.."$" |
| 23579 |
end |
| 23580 |
local hashes=instance.hashes |
| 23581 |
local files=instance.files |
| 23582 |
for h=1,#hashes do |
| 23583 |
local hash=hashes[h] |
| 23584 |
local hashname=hash.name |
| 23585 |
local content=hashname and files[hashname] |
| 23586 |
if content then |
| 23587 |
if trace_details then |
| 23588 |
report_resolving("deep checking %a, base %a, pattern %a",hashname,basename,pathname) |
| 23589 |
end |
| 23590 |
local path,name=lookup(content,basename) |
| 23591 |
if path then |
| 23592 |
local metadata=content.metadata |
| 23593 |
local realroot=metadata and metadata.path or hashname |
| 23594 |
if type(path)=="string" then |
| 23595 |
check(hash,realroot,pathname,path,basename,name) |
| 23596 |
else |
| 23597 |
for i=1,#path do |
| 23598 |
check(hash,realroot,pathname,path[i],basename,name) |
| 23599 |
end |
| 23600 |
end |
| 23601 |
end |
| 23602 |
elseif trace_locating then |
| 23603 |
report_resolving("no match in %a (%s)",hashname,basename) |
| 23604 |
end |
| 23605 |
end |
| 23606 |
end |
| 23607 |
return noffiles>0 and filelist or nil |
| 23608 |
end |
| 23609 |
local fit={} |
| 23610 |
function resolvers.registerintrees(filename,format,filetype,usedmethod,foundname) |
| 23611 |
local foundintrees=instance.foundintrees |
| 23612 |
if usedmethod=="direct" and filename==foundname and fit[foundname] then |
| 23613 |
else |
| 23614 |
local collapsed=collapsepath(foundname,true) |
| 23615 |
local t={ |
| 23616 |
filename=filename, |
| 23617 |
format=format~="" and format or nil, |
| 23618 |
filetype=filetype~="" and filetype or nil, |
| 23619 |
usedmethod=usedmethod, |
| 23620 |
foundname=foundname, |
| 23621 |
fullname=collapsed, |
| 23622 |
} |
| 23623 |
fit[foundname]=t |
| 23624 |
foundintrees[#foundintrees+1]=t |
| 23625 |
end |
| 23626 |
end |
| 23627 |
function resolvers.foundintrees() |
| 23628 |
return instance.foundintrees or {} |
| 23629 |
end |
| 23630 |
function resolvers.foundintree(fullname) |
| 23631 |
local f=fit[fullname] |
| 23632 |
return f and f.usedmethod=="database" |
| 23633 |
end |
| 23634 |
local function can_be_dir(name) |
| 23635 |
local fakepaths=instance.fakepaths |
| 23636 |
if not fakepaths[name] then |
| 23637 |
if isdir(name) then |
| 23638 |
fakepaths[name]=1 |
| 23639 |
else |
| 23640 |
fakepaths[name]=2 |
| 23641 |
end |
| 23642 |
end |
| 23643 |
return fakepaths[name]==1 |
| 23644 |
end |
| 23645 |
local preparetreepattern=Cs((P(".")/"%%."+P("-")/"%%-"+P(1))^0*Cc("$")) |
| 23646 |
local collect_instance_files |
| 23647 |
local function find_analyze(filename,askedformat,allresults) |
| 23648 |
local filetype="" |
| 23649 |
local filesuffix=suffixonly(filename) |
| 23650 |
local wantedfiles={} |
| 23651 |
wantedfiles[#wantedfiles+1]=filename |
| 23652 |
if askedformat=="" then |
| 23653 |
if filesuffix=="" or not suffixmap[filesuffix] then |
| 23654 |
local defaultsuffixes=resolvers.defaultsuffixes |
| 23655 |
for i=1,#defaultsuffixes do |
| 23656 |
local forcedname=filename.."."..defaultsuffixes[i] |
| 23657 |
wantedfiles[#wantedfiles+1]=forcedname |
| 23658 |
filetype=formatofsuffix(forcedname) |
| 23659 |
if trace_locating then |
| 23660 |
report_resolving("forcing filetype %a",filetype) |
| 23661 |
end |
| 23662 |
end |
| 23663 |
else |
| 23664 |
filetype=formatofsuffix(filename) |
| 23665 |
if trace_locating then |
| 23666 |
report_resolving("using suffix based filetype %a",filetype) |
| 23667 |
end |
| 23668 |
end |
| 23669 |
else |
| 23670 |
if filesuffix=="" or not suffixmap[filesuffix] then |
| 23671 |
local format_suffixes=suffixes[askedformat] |
| 23672 |
if format_suffixes then |
| 23673 |
for i=1,#format_suffixes do |
| 23674 |
wantedfiles[#wantedfiles+1]=filename.."."..format_suffixes[i] |
| 23675 |
end |
| 23676 |
end |
| 23677 |
end |
| 23678 |
filetype=askedformat |
| 23679 |
if trace_locating then |
| 23680 |
report_resolving("using given filetype %a",filetype) |
| 23681 |
end |
| 23682 |
end |
| 23683 |
return filetype,wantedfiles |
| 23684 |
end |
| 23685 |
local function find_direct(filename,allresults) |
| 23686 |
if not dangerous[askedformat] and isreadable(filename) then |
| 23687 |
if trace_details then |
| 23688 |
report_resolving("file %a found directly",filename) |
| 23689 |
end |
| 23690 |
return "direct",{ filename } |
| 23691 |
end |
| 23692 |
end |
| 23693 |
local function find_wildcard(filename,allresults) |
| 23694 |
if find(filename,'*',1,true) then |
| 23695 |
if trace_locating then |
| 23696 |
report_resolving("checking wildcard %a",filename) |
| 23697 |
end |
| 23698 |
local result=resolvers.findwildcardfiles(filename) |
| 23699 |
if result then |
| 23700 |
return "wildcard",result |
| 23701 |
end |
| 23702 |
end |
| 23703 |
end |
| 23704 |
local function find_qualified(filename,allresults,askedformat,alsostripped) |
| 23705 |
if not is_qualified_path(filename) then |
| 23706 |
return |
| 23707 |
end |
| 23708 |
if trace_locating then |
| 23709 |
report_resolving("checking qualified name %a",filename) |
| 23710 |
end |
| 23711 |
if isreadable(filename) then |
| 23712 |
if trace_details then |
| 23713 |
report_resolving("qualified file %a found",filename) |
| 23714 |
end |
| 23715 |
return "qualified",{ filename } |
| 23716 |
end |
| 23717 |
if trace_details then |
| 23718 |
report_resolving("locating qualified file %a",filename) |
| 23719 |
end |
| 23720 |
local forcedname,suffix="",suffixonly(filename) |
| 23721 |
if suffix=="" then |
| 23722 |
local format_suffixes=askedformat=="" and resolvers.defaultsuffixes or suffixes[askedformat] |
| 23723 |
if format_suffixes then |
| 23724 |
for i=1,#format_suffixes do |
| 23725 |
local suffix=format_suffixes[i] |
| 23726 |
forcedname=filename.."."..suffix |
| 23727 |
if isreadable(forcedname) then |
| 23728 |
if trace_locating then |
| 23729 |
report_resolving("no suffix, forcing format filetype %a",suffix) |
| 23730 |
end |
| 23731 |
return "qualified",{ forcedname } |
| 23732 |
end |
| 23733 |
end |
| 23734 |
end |
| 23735 |
end |
| 23736 |
if alsostripped and suffix and suffix~="" then |
| 23737 |
local basename=filebasename(filename) |
| 23738 |
local pattern=lpegmatch(preparetreepattern,filename) |
| 23739 |
local savedformat=askedformat |
| 23740 |
local format=savedformat or "" |
| 23741 |
if format=="" then |
| 23742 |
askedformat=formatofsuffix(suffix) |
| 23743 |
end |
| 23744 |
if not format then |
| 23745 |
askedformat="othertextfiles" |
| 23746 |
end |
| 23747 |
if basename~=filename then |
| 23748 |
local resolved=collect_instance_files(basename,askedformat,allresults) |
| 23749 |
if #resolved==0 then |
| 23750 |
local lowered=lower(basename) |
| 23751 |
if filename~=lowered then |
| 23752 |
resolved=collect_instance_files(lowered,askedformat,allresults) |
| 23753 |
end |
| 23754 |
end |
| 23755 |
resolvers.format=savedformat |
| 23756 |
if #resolved>0 then |
| 23757 |
local result={} |
| 23758 |
for r=1,#resolved do |
| 23759 |
local rr=resolved[r] |
| 23760 |
if find(rr,pattern) then |
| 23761 |
result[#result+1]=rr |
| 23762 |
end |
| 23763 |
end |
| 23764 |
if #result>0 then |
| 23765 |
return "qualified",result |
| 23766 |
end |
| 23767 |
end |
| 23768 |
end |
| 23769 |
end |
| 23770 |
end |
| 23771 |
local function check_subpath(fname) |
| 23772 |
if isreadable(fname) then |
| 23773 |
if trace_details then |
| 23774 |
report_resolving("found %a by deep scanning",fname) |
| 23775 |
end |
| 23776 |
return fname |
| 23777 |
end |
| 23778 |
end |
| 23779 |
local function makepathlist(list,filetype) |
| 23780 |
local typespec=resolvers.variableofformat(filetype) |
| 23781 |
local pathlist=expandedpathlist(typespec,filetype and usertypes[filetype]) |
| 23782 |
local entry={} |
| 23783 |
if pathlist and #pathlist>0 then |
| 23784 |
for k=1,#pathlist do |
| 23785 |
local path=pathlist[k] |
| 23786 |
local prescanned=find(path,'^!!') |
| 23787 |
local resursive=find(path,'//$') |
| 23788 |
local pathname=lpegmatch(inhibitstripper,path) |
| 23789 |
local expression=makepathexpression(pathname) |
| 23790 |
local barename=gsub(pathname,"/+$","") |
| 23791 |
barename=resolveprefix(barename) |
| 23792 |
local scheme=urlhasscheme(barename) |
| 23793 |
local schemename=gsub(barename,"%.%*$",'') |
| 23794 |
entry[k]={ |
| 23795 |
path=path, |
| 23796 |
pathname=pathname, |
| 23797 |
prescanned=prescanned, |
| 23798 |
recursive=recursive, |
| 23799 |
expression=expression, |
| 23800 |
barename=barename, |
| 23801 |
scheme=scheme, |
| 23802 |
schemename=schemename, |
| 23803 |
} |
| 23804 |
end |
| 23805 |
entry.typespec=typespec |
| 23806 |
list[filetype]=entry |
| 23807 |
else |
| 23808 |
list[filetype]=false |
| 23809 |
end |
| 23810 |
return entry |
| 23811 |
end |
| 23812 |
local function find_intree(filename,filetype,wantedfiles,allresults) |
| 23813 |
local pathlists=instance.pathlists |
| 23814 |
if not pathlists then |
| 23815 |
pathlists=setmetatableindex({},makepathlist) |
| 23816 |
instance.pathlists=pathlists |
| 23817 |
end |
| 23818 |
local pathlist=pathlists[filetype] |
| 23819 |
if pathlist then |
| 23820 |
local method="intree" |
| 23821 |
local filelist=collect_files(wantedfiles) |
| 23822 |
local dirlist={} |
| 23823 |
local result={} |
| 23824 |
if filelist then |
| 23825 |
for i=1,#filelist do |
| 23826 |
dirlist[i]=filedirname(filelist[i][3]).."/" |
| 23827 |
end |
| 23828 |
end |
| 23829 |
if trace_details then |
| 23830 |
report_resolving("checking filename %a in tree",filename) |
| 23831 |
end |
| 23832 |
for k=1,#pathlist do |
| 23833 |
local entry=pathlist[k] |
| 23834 |
local path=entry.path |
| 23835 |
local pathname=entry.pathname |
| 23836 |
local done=false |
| 23837 |
if filelist then |
| 23838 |
local expression=entry.expression |
| 23839 |
if trace_details then |
| 23840 |
report_resolving("using pattern %a for path %a",expression,pathname) |
| 23841 |
end |
| 23842 |
for k=1,#filelist do |
| 23843 |
local fl=filelist[k] |
| 23844 |
local f=fl[2] |
| 23845 |
local d=dirlist[k] |
| 23846 |
if find(d,expression) or find(resolveprefix(d),expression) then |
| 23847 |
result[#result+1]=resolveprefix(fl[3]) |
| 23848 |
done=true |
| 23849 |
if allresults then |
| 23850 |
if trace_details then |
| 23851 |
report_resolving("match to %a in hash for file %a and path %a, continue scanning",expression,f,d) |
| 23852 |
end |
| 23853 |
else |
| 23854 |
if trace_details then |
| 23855 |
report_resolving("match to %a in hash for file %a and path %a, quit scanning",expression,f,d) |
| 23856 |
end |
| 23857 |
break |
| 23858 |
end |
| 23859 |
elseif trace_details then |
| 23860 |
report_resolving("no match to %a in hash for file %a and path %a",expression,f,d) |
| 23861 |
end |
| 23862 |
end |
| 23863 |
end |
| 23864 |
if done then |
| 23865 |
method="database" |
| 23866 |
else |
| 23867 |
method="filesystem" |
| 23868 |
local scheme=entry.scheme |
| 23869 |
if not scheme or scheme=="file" then |
| 23870 |
local pname=entry.schemename |
| 23871 |
if not find(pname,"*",1,true) then |
| 23872 |
if can_be_dir(pname) then |
| 23873 |
if not done and not entry.prescanned then |
| 23874 |
if trace_details then |
| 23875 |
report_resolving("quick root scan for %a",pname) |
| 23876 |
end |
| 23877 |
for k=1,#wantedfiles do |
| 23878 |
local w=wantedfiles[k] |
| 23879 |
local fname=check_subpath(filejoin(pname,w)) |
| 23880 |
if fname then |
| 23881 |
result[#result+1]=fname |
| 23882 |
done=true |
| 23883 |
if not allresults then |
| 23884 |
break |
| 23885 |
end |
| 23886 |
end |
| 23887 |
end |
| 23888 |
if not done and entry.recursive then |
| 23889 |
if trace_details then |
| 23890 |
report_resolving("scanning filesystem for %a",pname) |
| 23891 |
end |
| 23892 |
local files=resolvers.simplescanfiles(pname,false,true) |
| 23893 |
for k=1,#wantedfiles do |
| 23894 |
local w=wantedfiles[k] |
| 23895 |
local subpath=files[w] |
| 23896 |
if not subpath or subpath=="" then |
| 23897 |
elseif type(subpath)=="string" then |
| 23898 |
local fname=check_subpath(filejoin(pname,subpath,w)) |
| 23899 |
if fname then |
| 23900 |
result[#result+1]=fname |
| 23901 |
done=true |
| 23902 |
if not allresults then |
| 23903 |
break |
| 23904 |
end |
| 23905 |
end |
| 23906 |
else |
| 23907 |
for i=1,#subpath do |
| 23908 |
local sp=subpath[i] |
| 23909 |
if sp=="" then |
| 23910 |
else |
| 23911 |
local fname=check_subpath(filejoin(pname,sp,w)) |
| 23912 |
if fname then |
| 23913 |
result[#result+1]=fname |
| 23914 |
done=true |
| 23915 |
if not allresults then |
| 23916 |
break |
| 23917 |
end |
| 23918 |
end |
| 23919 |
end |
| 23920 |
end |
| 23921 |
if done and not allresults then |
| 23922 |
break |
| 23923 |
end |
| 23924 |
end |
| 23925 |
end |
| 23926 |
end |
| 23927 |
end |
| 23928 |
end |
| 23929 |
else |
| 23930 |
end |
| 23931 |
else |
| 23932 |
for k=1,#wantedfiles do |
| 23933 |
local pname=entry.barename |
| 23934 |
local fname=methodhandler('finders',pname.."/"..wantedfiles[k]) |
| 23935 |
if fname then |
| 23936 |
result[#result+1]=fname |
| 23937 |
done=true |
| 23938 |
if not allresults then |
| 23939 |
break |
| 23940 |
end |
| 23941 |
end |
| 23942 |
end |
| 23943 |
end |
| 23944 |
end |
| 23945 |
if done and not allresults then |
| 23946 |
break |
| 23947 |
end |
| 23948 |
end |
| 23949 |
if #result>0 then |
| 23950 |
return method,result |
| 23951 |
end |
| 23952 |
end |
| 23953 |
end |
| 23954 |
local function find_onpath(filename,filetype,wantedfiles,allresults) |
| 23955 |
if trace_details then |
| 23956 |
report_resolving("checking filename %a, filetype %a, wanted files %a",filename,filetype,concat(wantedfiles," | ")) |
| 23957 |
end |
| 23958 |
local result={} |
| 23959 |
for k=1,#wantedfiles do |
| 23960 |
local fname=wantedfiles[k] |
| 23961 |
if fname and isreadable(fname) then |
| 23962 |
filename=fname |
| 23963 |
result[#result+1]=filejoin('.',fname) |
| 23964 |
if not allresults then |
| 23965 |
break |
| 23966 |
end |
| 23967 |
end |
| 23968 |
end |
| 23969 |
if #result>0 then |
| 23970 |
return "onpath",result |
| 23971 |
end |
| 23972 |
end |
| 23973 |
local function find_otherwise(filename,filetype,wantedfiles,allresults) |
| 23974 |
local filelist=collect_files(wantedfiles) |
| 23975 |
local fl=filelist and filelist[1] |
| 23976 |
if fl then |
| 23977 |
return "otherwise",{ resolveprefix(fl[3]) } |
| 23978 |
end |
| 23979 |
end |
| 23980 |
collect_instance_files=function(filename,askedformat,allresults) |
| 23981 |
if not filename or filename=="" then |
| 23982 |
return {} |
| 23983 |
end |
| 23984 |
askedformat=askedformat or "" |
| 23985 |
filename=collapsepath(filename,".") |
| 23986 |
filename=gsub(filename,"^%./",getcurrentdir().."/") |
| 23987 |
if allresults then |
| 23988 |
local filetype,wantedfiles=find_analyze(filename,askedformat) |
| 23989 |
local results={ |
| 23990 |
{ find_direct (filename,true) }, |
| 23991 |
{ find_wildcard (filename,true) }, |
| 23992 |
{ find_qualified(filename,true,askedformat) }, |
| 23993 |
{ find_intree (filename,filetype,wantedfiles,true) }, |
| 23994 |
{ find_onpath (filename,filetype,wantedfiles,true) }, |
| 23995 |
{ find_otherwise(filename,filetype,wantedfiles,true) }, |
| 23996 |
} |
| 23997 |
local result={} |
| 23998 |
local status={} |
| 23999 |
local done={} |
| 24000 |
for k=1,#results do |
| 24001 |
local r=results[k] |
| 24002 |
local method,list=r[1],r[2] |
| 24003 |
if method and list then |
| 24004 |
for i=1,#list do |
| 24005 |
local c=collapsepath(list[i]) |
| 24006 |
if not done[c] then |
| 24007 |
result[#result+1]=c |
| 24008 |
done[c]=true |
| 24009 |
end |
| 24010 |
status[#status+1]=formatters["%-10s: %s"](method,c) |
| 24011 |
end |
| 24012 |
end |
| 24013 |
end |
| 24014 |
if trace_details then |
| 24015 |
report_resolving("lookup status: %s",table.serialize(status,filename)) |
| 24016 |
end |
| 24017 |
return result,status |
| 24018 |
else |
| 24019 |
local method,result,stamp,filetype,wantedfiles |
| 24020 |
if instance.remember then |
| 24021 |
if askedformat=="" then |
| 24022 |
stamp=formatters["%s::%s"](suffixonly(filename),filename) |
| 24023 |
else |
| 24024 |
stamp=formatters["%s::%s"](askedformat,filename) |
| 24025 |
end |
| 24026 |
result=stamp and instance.found[stamp] |
| 24027 |
if result then |
| 24028 |
if trace_locating then |
| 24029 |
report_resolving("remembered file %a",filename) |
| 24030 |
end |
| 24031 |
return result |
| 24032 |
end |
| 24033 |
end |
| 24034 |
method,result=find_direct(filename) |
| 24035 |
if not result then |
| 24036 |
method,result=find_wildcard(filename) |
| 24037 |
if not result then |
| 24038 |
method,result=find_qualified(filename,false,askedformat) |
| 24039 |
if not result then |
| 24040 |
filetype,wantedfiles=find_analyze(filename,askedformat) |
| 24041 |
method,result=find_intree(filename,filetype,wantedfiles) |
| 24042 |
if not result then |
| 24043 |
method,result=find_onpath(filename,filetype,wantedfiles) |
| 24044 |
if resolve_otherwise and not result then |
| 24045 |
method,result=find_otherwise(filename,filetype,wantedfiles) |
| 24046 |
end |
| 24047 |
end |
| 24048 |
end |
| 24049 |
end |
| 24050 |
end |
| 24051 |
if result and #result>0 then |
| 24052 |
local foundname=collapsepath(result[1]) |
| 24053 |
resolvers.registerintrees(filename,askedformat,filetype,method,foundname) |
| 24054 |
result={ foundname } |
| 24055 |
else |
| 24056 |
result={} |
| 24057 |
end |
| 24058 |
if stamp then |
| 24059 |
if trace_locating then |
| 24060 |
report_resolving("remembering file %a using hash %a",filename,stamp) |
| 24061 |
end |
| 24062 |
instance.found[stamp]=result |
| 24063 |
end |
| 24064 |
return result |
| 24065 |
end |
| 24066 |
end |
| 24067 |
local function findfiles(filename,filetype,allresults) |
| 24068 |
if not filename or filename=="" then |
| 24069 |
return {} |
| 24070 |
end |
| 24071 |
if allresults==nil then |
| 24072 |
allresults=true |
| 24073 |
end |
| 24074 |
local result,status=collect_instance_files(filename,filetype or "",allresults) |
| 24075 |
if not result or #result==0 then |
| 24076 |
local lowered=lower(filename) |
| 24077 |
if filename~=lowered then |
| 24078 |
result,status=collect_instance_files(lowered,filetype or "",allresults) |
| 24079 |
end |
| 24080 |
end |
| 24081 |
return result or {},status |
| 24082 |
end |
| 24083 |
local function findfile(filename,filetype) |
| 24084 |
if not filename or filename=="" then |
| 24085 |
return "" |
| 24086 |
else |
| 24087 |
return findfiles(filename,filetype,false)[1] or "" |
| 24088 |
end |
| 24089 |
end |
| 24090 |
resolvers.findfiles=findfiles |
| 24091 |
resolvers.findfile=findfile |
| 24092 |
resolvers.find_file=findfile |
| 24093 |
resolvers.find_files=findfiles |
| 24094 |
function resolvers.findpath(filename,filetype) |
| 24095 |
return filedirname(findfiles(filename,filetype,false)[1] or "") |
| 24096 |
end |
| 24097 |
local function findgivenfiles(filename,allresults) |
| 24098 |
local hashes=instance.hashes |
| 24099 |
local files=instance.files |
| 24100 |
local base=filebasename(filename) |
| 24101 |
local result={} |
| 24102 |
local function okay(hash,path,name) |
| 24103 |
local found=methodhandler('concatinators',hash.type,hash.name,path,name) |
| 24104 |
if found and found~="" then |
| 24105 |
result[#result+1]=resolveprefix(found) |
| 24106 |
return not allresults |
| 24107 |
end |
| 24108 |
end |
| 24109 |
for k=1,#hashes do |
| 24110 |
local hash=hashes[k] |
| 24111 |
local content=files[hash.name] |
| 24112 |
if content then |
| 24113 |
local path,name=lookup(content,base) |
| 24114 |
if not path then |
| 24115 |
elseif type(path)=="string" then |
| 24116 |
if okay(hash,path,name) then |
| 24117 |
return result |
| 24118 |
end |
| 24119 |
else |
| 24120 |
for i=1,#path do |
| 24121 |
if okay(hash,path[i],name) then |
| 24122 |
return result |
| 24123 |
end |
| 24124 |
end |
| 24125 |
end |
| 24126 |
end |
| 24127 |
end |
| 24128 |
return result |
| 24129 |
end |
| 24130 |
function resolvers.findgivenfiles(filename) |
| 24131 |
return findgivenfiles(filename,true) |
| 24132 |
end |
| 24133 |
function resolvers.findgivenfile(filename) |
| 24134 |
return findgivenfiles(filename,false)[1] or "" |
| 24135 |
end |
| 24136 |
local makewildcard=Cs( |
| 24137 |
(P("^")^0*P("/")*P(-1)+P(-1))/".*"+(P("^")^0*P("/")/"")^0*(P("*")/".*"+P("-")/"%%-"+P(".")/"%%."+P("?")/"."+P("\\")/"/"+P(1))^0 |
| 24138 |
) |
| 24139 |
function resolvers.wildcardpattern(pattern) |
| 24140 |
return lpegmatch(makewildcard,pattern) or pattern |
| 24141 |
end |
| 24142 |
local function findwildcardfiles(filename,allresults,result) |
| 24143 |
local files=instance.files |
| 24144 |
local hashes=instance.hashes |
| 24145 |
local result=result or {} |
| 24146 |
local base=filebasename(filename) |
| 24147 |
local dirn=filedirname(filename) |
| 24148 |
local path=lower(lpegmatch(makewildcard,dirn) or dirn) |
| 24149 |
local name=lower(lpegmatch(makewildcard,base) or base) |
| 24150 |
if find(name,"*",1,true) then |
| 24151 |
local function okay(found,path,base,hashname,hashtype) |
| 24152 |
if find(found,path) then |
| 24153 |
local full=methodhandler('concatinators',hashtype,hashname,found,base) |
| 24154 |
if full and full~="" then |
| 24155 |
result[#result+1]=resolveprefix(full) |
| 24156 |
return not allresults |
| 24157 |
end |
| 24158 |
end |
| 24159 |
end |
| 24160 |
for k=1,#hashes do |
| 24161 |
local hash=hashes[k] |
| 24162 |
local hashname=hash.name |
| 24163 |
local hashtype=hash.type |
| 24164 |
if hashname and hashtype then |
| 24165 |
for found,base in filtered(files[hashname],name) do |
| 24166 |
if type(found)=='string' then |
| 24167 |
if okay(found,path,base,hashname,hashtype) then |
| 24168 |
break |
| 24169 |
end |
| 24170 |
else |
| 24171 |
for i=1,#found do |
| 24172 |
if okay(found[i],path,base,hashname,hashtype) then |
| 24173 |
break |
| 24174 |
end |
| 24175 |
end |
| 24176 |
end |
| 24177 |
end |
| 24178 |
end |
| 24179 |
end |
| 24180 |
else |
| 24181 |
local function okayokay(found,path,base,hashname,hashtype) |
| 24182 |
if find(found,path) then |
| 24183 |
local full=methodhandler('concatinators',hashtype,hashname,found,base) |
| 24184 |
if full and full~="" then |
| 24185 |
result[#result+1]=resolveprefix(full) |
| 24186 |
return not allresults |
| 24187 |
end |
| 24188 |
end |
| 24189 |
end |
| 24190 |
for k=1,#hashes do |
| 24191 |
local hash=hashes[k] |
| 24192 |
local hashname=hash.name |
| 24193 |
local hashtype=hash.type |
| 24194 |
if hashname and hashtype then |
| 24195 |
local found,base=lookup(content,base) |
| 24196 |
if not found then |
| 24197 |
elseif type(found)=='string' then |
| 24198 |
if okay(found,path,base,hashname,hashtype) then |
| 24199 |
break |
| 24200 |
end |
| 24201 |
else |
| 24202 |
for i=1,#found do |
| 24203 |
if okay(found[i],path,base,hashname,hashtype) then |
| 24204 |
break |
| 24205 |
end |
| 24206 |
end |
| 24207 |
end |
| 24208 |
end |
| 24209 |
end |
| 24210 |
end |
| 24211 |
return result |
| 24212 |
end |
| 24213 |
function resolvers.findwildcardfiles(filename,result) |
| 24214 |
return findwildcardfiles(filename,true,result) |
| 24215 |
end |
| 24216 |
function resolvers.findwildcardfile(filename) |
| 24217 |
return findwildcardfiles(filename,false)[1] or "" |
| 24218 |
end |
| 24219 |
do |
| 24220 |
local starttiming=statistics.starttiming |
| 24221 |
local stoptiming=statistics.stoptiming |
| 24222 |
local elapsedtime=statistics.elapsedtime |
| 24223 |
function resolvers.starttiming() |
| 24224 |
starttiming(instance) |
| 24225 |
end |
| 24226 |
function resolvers.stoptiming() |
| 24227 |
stoptiming(instance) |
| 24228 |
end |
| 24229 |
function resolvers.loadtime() |
| 24230 |
return elapsedtime(instance) |
| 24231 |
end |
| 24232 |
end |
| 24233 |
function resolvers.automount() |
| 24234 |
end |
| 24235 |
function resolvers.load(option) |
| 24236 |
resolvers.starttiming() |
| 24237 |
identify_configuration_files() |
| 24238 |
load_configuration_files() |
| 24239 |
if option~="nofiles" then |
| 24240 |
load_databases() |
| 24241 |
resolvers.automount() |
| 24242 |
end |
| 24243 |
resolvers.stoptiming() |
| 24244 |
local files=instance.files |
| 24245 |
return files and next(files) and true |
| 24246 |
end |
| 24247 |
local function report(str) |
| 24248 |
if trace_locating then |
| 24249 |
report_resolving(str) |
| 24250 |
else |
| 24251 |
print(str) |
| 24252 |
end |
| 24253 |
end |
| 24254 |
function resolvers.dowithfilesandreport(command,files,...) |
| 24255 |
if files and #files>0 then |
| 24256 |
if trace_locating then |
| 24257 |
report('') |
| 24258 |
end |
| 24259 |
if type(files)=="string" then |
| 24260 |
files={ files } |
| 24261 |
end |
| 24262 |
for f=1,#files do |
| 24263 |
local file=files[f] |
| 24264 |
local result=command(file,...) |
| 24265 |
if type(result)=='string' then |
| 24266 |
report(result) |
| 24267 |
else |
| 24268 |
for i=1,#result do |
| 24269 |
report(result[i]) |
| 24270 |
end |
| 24271 |
end |
| 24272 |
end |
| 24273 |
end |
| 24274 |
end |
| 24275 |
function resolvers.showpath(str) |
| 24276 |
return joinpath(expandedpathlist(resolvers.formatofvariable(str))) |
| 24277 |
end |
| 24278 |
function resolvers.registerfile(files,name,path) |
| 24279 |
if files[name] then |
| 24280 |
if type(files[name])=='string' then |
| 24281 |
files[name]={ files[name],path } |
| 24282 |
else |
| 24283 |
files[name]=path |
| 24284 |
end |
| 24285 |
else |
| 24286 |
files[name]=path |
| 24287 |
end |
| 24288 |
end |
| 24289 |
function resolvers.dowithpath(name,func) |
| 24290 |
local pathlist=expandedpathlist(name) |
| 24291 |
for i=1,#pathlist do |
| 24292 |
func("^"..cleanpath(pathlist[i])) |
| 24293 |
end |
| 24294 |
end |
| 24295 |
function resolvers.dowithvariable(name,func) |
| 24296 |
func(expandedvariable(name)) |
| 24297 |
end |
| 24298 |
function resolvers.locateformat(name) |
| 24299 |
local engine=environment.ownmain or "luatex" |
| 24300 |
local barename=removesuffix(file.basename(name)) |
| 24301 |
local fullname=addsuffix(barename,"fmt") |
| 24302 |
local fmtname=caches.getfirstreadablefile(fullname,"formats",engine) or "" |
| 24303 |
if fmtname=="" then |
| 24304 |
fmtname=findfile(fullname) |
| 24305 |
fmtname=cleanpath(fmtname) |
| 24306 |
end |
| 24307 |
if fmtname~="" then |
| 24308 |
local barename=removesuffix(fmtname) |
| 24309 |
local luaname=addsuffix(barename,luasuffixes.lua) |
| 24310 |
local lucname=addsuffix(barename,luasuffixes.luc) |
| 24311 |
local luiname=addsuffix(barename,luasuffixes.lui) |
| 24312 |
if isfile(luiname) then |
| 24313 |
return fmtname,luiname |
| 24314 |
elseif isfile(lucname) then |
| 24315 |
return fmtname,lucname |
| 24316 |
elseif isfile(luaname) then |
| 24317 |
return fmtname,luaname |
| 24318 |
end |
| 24319 |
end |
| 24320 |
return nil,nil |
| 24321 |
end |
| 24322 |
function resolvers.booleanvariable(str,default) |
| 24323 |
local b=expansion(str) |
| 24324 |
if b=="" then |
| 24325 |
return default |
| 24326 |
else |
| 24327 |
b=toboolean(b) |
| 24328 |
return (b==nil and default) or b |
| 24329 |
end |
| 24330 |
end |
| 24331 |
function resolvers.dowithfilesintree(pattern,handle,before,after) |
| 24332 |
local hashes=instance.hashes |
| 24333 |
local files=instance.files |
| 24334 |
for i=1,#hashes do |
| 24335 |
local hash=hashes[i] |
| 24336 |
local blobtype=hash.type |
| 24337 |
local blobpath=hash.name |
| 24338 |
if blobtype and blobpath then |
| 24339 |
local total=0 |
| 24340 |
local checked=0 |
| 24341 |
local done=0 |
| 24342 |
if before then |
| 24343 |
before(blobtype,blobpath,pattern) |
| 24344 |
end |
| 24345 |
for path,name in filtered(files[blobpath],pattern) do |
| 24346 |
if type(path)=="string" then |
| 24347 |
checked=checked+1 |
| 24348 |
if handle(blobtype,blobpath,path,name) then |
| 24349 |
done=done+1 |
| 24350 |
end |
| 24351 |
else |
| 24352 |
checked=checked+#path |
| 24353 |
for i=1,#path do |
| 24354 |
if handle(blobtype,blobpath,path[i],name) then |
| 24355 |
done=done+1 |
| 24356 |
end |
| 24357 |
end |
| 24358 |
end |
| 24359 |
end |
| 24360 |
if after then |
| 24361 |
after(blobtype,blobpath,pattern,checked,done) |
| 24362 |
end |
| 24363 |
end |
| 24364 |
end |
| 24365 |
end |
| 24366 |
function resolvers.knownvariables(pattern) |
| 24367 |
if instance then |
| 24368 |
local environment=instance.environment |
| 24369 |
local variables=instance.variables |
| 24370 |
local expansions=instance.expansions |
| 24371 |
local order=instance.order |
| 24372 |
local pattern=upper(pattern or "") |
| 24373 |
local result={} |
| 24374 |
for i=1,#order do |
| 24375 |
for key in next,order[i] do |
| 24376 |
if result[key]==nil and key~="" and (pattern=="" or find(upper(key),pattern)) then |
| 24377 |
result[key]={ |
| 24378 |
environment=rawget(environment,key), |
| 24379 |
variable=key, |
| 24380 |
expansion=expansions[key], |
| 24381 |
resolved=resolveprefix(expansions[key]), |
| 24382 |
} |
| 24383 |
end |
| 24384 |
end |
| 24385 |
end |
| 24386 |
return result |
| 24387 |
else |
| 24388 |
return {} |
| 24389 |
end |
| 24390 |
end |
| 24391 |
|
| 24392 |
|
| 24393 |
end -- of closure |
| 24394 |
|
| 24395 |
do -- create closure to overcome 200 locals limit |
| 24396 |
|
| 24397 |
package.loaded["data-pre"] = package.loaded["data-pre"] or true |
| 24398 |
|
| 24399 |
-- original size: 5872, stripped down to: 3691 |
| 24400 |
|
| 24401 |
if not modules then modules={} end modules ['data-pre']={ |
| 24402 |
version=1.001, |
| 24403 |
comment="companion to luat-lib.mkiv", |
| 24404 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 24405 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 24406 |
license="see context related readme files" |
| 24407 |
} |
| 24408 |
local ipairs=ipairs |
| 24409 |
local insert,remove=table.insert,table.remove |
| 24410 |
local resolvers=resolvers |
| 24411 |
local prefixes=resolvers.prefixes |
| 24412 |
local cleanpath=resolvers.cleanpath |
| 24413 |
local findgivenfile=resolvers.findgivenfile |
| 24414 |
local expansion=resolvers.expansion |
| 24415 |
local getenv=resolvers.getenv |
| 24416 |
local basename=file.basename |
| 24417 |
local dirname=file.dirname |
| 24418 |
local joinpath=file.join |
| 24419 |
local isfile=lfs.isfile |
| 24420 |
local isdir=lfs.isdir |
| 24421 |
prefixes.environment=function(str) |
| 24422 |
return cleanpath(expansion(str)) |
| 24423 |
end |
| 24424 |
local function relative(str,n) |
| 24425 |
if not isfile(str) then |
| 24426 |
local pstr="./"..str |
| 24427 |
if isfile(pstr) then |
| 24428 |
str=pstr |
| 24429 |
else |
| 24430 |
local p="../" |
| 24431 |
for i=1,n or 2 do |
| 24432 |
local pstr=p..str |
| 24433 |
if isfile(pstr) then |
| 24434 |
str=pstr |
| 24435 |
break |
| 24436 |
else |
| 24437 |
p=p.."../" |
| 24438 |
end |
| 24439 |
end |
| 24440 |
end |
| 24441 |
end |
| 24442 |
return cleanpath(str) |
| 24443 |
end |
| 24444 |
local function locate(str) |
| 24445 |
local fullname=findgivenfile(str) or "" |
| 24446 |
return cleanpath(fullname~="" and fullname or str) |
| 24447 |
end |
| 24448 |
prefixes.relative=relative |
| 24449 |
prefixes.locate=locate |
| 24450 |
prefixes.auto=function(str) |
| 24451 |
local fullname=relative(str) |
| 24452 |
if not isfile(fullname) then |
| 24453 |
fullname=locate(str) |
| 24454 |
end |
| 24455 |
return fullname |
| 24456 |
end |
| 24457 |
prefixes.filename=function(str) |
| 24458 |
local fullname=findgivenfile(str) or "" |
| 24459 |
return cleanpath(basename((fullname~="" and fullname) or str)) |
| 24460 |
end |
| 24461 |
prefixes.pathname=function(str) |
| 24462 |
local fullname=findgivenfile(str) or "" |
| 24463 |
return cleanpath(dirname((fullname~="" and fullname) or str)) |
| 24464 |
end |
| 24465 |
prefixes.selfautoloc=function(str) |
| 24466 |
local pth=getenv('SELFAUTOLOC') |
| 24467 |
return cleanpath(str and joinpath(pth,str) or pth) |
| 24468 |
end |
| 24469 |
prefixes.selfautoparent=function(str) |
| 24470 |
local pth=getenv('SELFAUTOPARENT') |
| 24471 |
return cleanpath(str and joinpath(pth,str) or pth) |
| 24472 |
end |
| 24473 |
prefixes.selfautodir=function(str) |
| 24474 |
local pth=getenv('SELFAUTODIR') |
| 24475 |
return cleanpath(str and joinpath(pth,str) or pth) |
| 24476 |
end |
| 24477 |
prefixes.home=function(str) |
| 24478 |
local pth=getenv('HOME') |
| 24479 |
return cleanpath(str and joinpath(pth,str) or pth) |
| 24480 |
end |
| 24481 |
do |
| 24482 |
local tmppth |
| 24483 |
prefixes.temp=function(str) |
| 24484 |
if not tmppth then |
| 24485 |
for _,s in ipairs { "TMP","TEMP","TMPDIR","TEMPDIR" } do |
| 24486 |
tmppth=getenv(s) |
| 24487 |
if tmppth~="" and isdir(tmppth) then |
| 24488 |
break |
| 24489 |
end |
| 24490 |
end |
| 24491 |
if not tmppth or tmppth=="" then |
| 24492 |
tmppth="." |
| 24493 |
end |
| 24494 |
end |
| 24495 |
return cleanpath(str and joinpath(tmppth,str) or tmppth) |
| 24496 |
end |
| 24497 |
prefixes.texruns=function(str) |
| 24498 |
local pth=getenv('TEXRUNS') |
| 24499 |
if pth=="" then |
| 24500 |
pth=tmppth |
| 24501 |
end |
| 24502 |
return cleanpath(str and joinpath(pth,str) or pth) |
| 24503 |
end |
| 24504 |
end |
| 24505 |
prefixes.env=prefixes.environment |
| 24506 |
prefixes.rel=prefixes.relative |
| 24507 |
prefixes.loc=prefixes.locate |
| 24508 |
prefixes.kpse=prefixes.locate |
| 24509 |
prefixes.full=prefixes.locate |
| 24510 |
prefixes.file=prefixes.filename |
| 24511 |
prefixes.path=prefixes.pathname |
| 24512 |
local inputstack={} |
| 24513 |
local stackpath=resolvers.stackpath |
| 24514 |
local function toppath() |
| 24515 |
if not inputstack then |
| 24516 |
return "." |
| 24517 |
end |
| 24518 |
local pathname=dirname(inputstack[#inputstack] or "") |
| 24519 |
if pathname=="" then |
| 24520 |
return "." |
| 24521 |
else |
| 24522 |
return pathname |
| 24523 |
end |
| 24524 |
end |
| 24525 |
local function jobpath() |
| 24526 |
local path=stackpath() |
| 24527 |
if not path or path=="" then |
| 24528 |
return "." |
| 24529 |
else |
| 24530 |
return path |
| 24531 |
end |
| 24532 |
end |
| 24533 |
local function pushinputname(name) |
| 24534 |
insert(inputstack,name) |
| 24535 |
end |
| 24536 |
local function popinputname(name) |
| 24537 |
return remove(inputstack) |
| 24538 |
end |
| 24539 |
resolvers.toppath=toppath |
| 24540 |
resolvers.jobpath=jobpath |
| 24541 |
resolvers.pushinputname=pushinputname |
| 24542 |
resolvers.popinputname=popinputname |
| 24543 |
prefixes.toppath=function(str) return cleanpath(joinpath(toppath(),str)) end |
| 24544 |
prefixes.jobpath=function(str) return cleanpath(joinpath(jobpath(),str)) end |
| 24545 |
resolvers.setdynamic("toppath") |
| 24546 |
resolvers.setdynamic("jobpath") |
| 24547 |
|
| 24548 |
|
| 24549 |
end -- of closure |
| 24550 |
|
| 24551 |
do -- create closure to overcome 200 locals limit |
| 24552 |
|
| 24553 |
package.loaded["data-inp"] = package.loaded["data-inp"] or true |
| 24554 |
|
| 24555 |
-- original size: 1050, stripped down to: 946 |
| 24556 |
|
| 24557 |
if not modules then modules={} end modules ['data-inp']={ |
| 24558 |
version=1.001, |
| 24559 |
comment="companion to luat-lib.mkiv", |
| 24560 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 24561 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 24562 |
license="see context related readme files" |
| 24563 |
} |
| 24564 |
local allocate=utilities.storage.allocate |
| 24565 |
local resolvers=resolvers |
| 24566 |
local methodhandler=resolvers.methodhandler |
| 24567 |
local registermethod=resolvers.registermethod |
| 24568 |
local finders=allocate { helpers={},notfound=function() end } |
| 24569 |
local openers=allocate { helpers={},notfound=function() end } |
| 24570 |
local loaders=allocate { helpers={},notfound=function() return false,nil,0 end } |
| 24571 |
local tracers=allocate { helpers={},notfound=function() end } |
| 24572 |
registermethod("finders",finders,"uri") |
| 24573 |
registermethod("openers",openers,"uri") |
| 24574 |
registermethod("loaders",loaders,"uri") |
| 24575 |
registermethod("tracers",tracers,"uri") |
| 24576 |
resolvers.finders=finders |
| 24577 |
resolvers.openers=openers |
| 24578 |
resolvers.loaders=loaders |
| 24579 |
resolvers.tracers=tracers |
| 24580 |
|
| 24581 |
|
| 24582 |
end -- of closure |
| 24583 |
|
| 24584 |
do -- create closure to overcome 200 locals limit |
| 24585 |
|
| 24586 |
package.loaded["data-out"] = package.loaded["data-out"] or true |
| 24587 |
|
| 24588 |
-- original size: 682, stripped down to: 579 |
| 24589 |
|
| 24590 |
if not modules then modules={} end modules ['data-out']={ |
| 24591 |
version=1.001, |
| 24592 |
comment="companion to luat-lib.mkiv", |
| 24593 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 24594 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 24595 |
license="see context related readme files" |
| 24596 |
} |
| 24597 |
local allocate=utilities.storage.allocate |
| 24598 |
local resolvers=resolvers |
| 24599 |
local registermethod=resolvers.registermethod |
| 24600 |
local savers=allocate { helpers={} } |
| 24601 |
resolvers.savers=savers |
| 24602 |
local cleaners=allocate { helpers={} } |
| 24603 |
resolvers.cleaners=cleaners |
| 24604 |
registermethod("savers",savers,"uri") |
| 24605 |
registermethod("cleaners",cleaners,"uri") |
| 24606 |
|
| 24607 |
|
| 24608 |
end -- of closure |
| 24609 |
|
| 24610 |
do -- create closure to overcome 200 locals limit |
| 24611 |
|
| 24612 |
package.loaded["data-fil"] = package.loaded["data-fil"] or true |
| 24613 |
|
| 24614 |
-- original size: 4365, stripped down to: 3452 |
| 24615 |
|
| 24616 |
if not modules then modules={} end modules ['data-fil']={ |
| 24617 |
version=1.001, |
| 24618 |
comment="companion to luat-lib.mkiv", |
| 24619 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 24620 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 24621 |
license="see context related readme files" |
| 24622 |
} |
| 24623 |
local ioopen=io.open |
| 24624 |
local isdir=lfs.isdir |
| 24625 |
local trace_locating=false trackers.register("resolvers.locating",function(v) trace_locating=v end) |
| 24626 |
local report_files=logs.reporter("resolvers","files") |
| 24627 |
local resolvers=resolvers |
| 24628 |
local resolveprefix=resolvers.resolve |
| 24629 |
local findfile=resolvers.findfile |
| 24630 |
local scanfiles=resolvers.scanfiles |
| 24631 |
local registerfilehash=resolvers.registerfilehash |
| 24632 |
local appendhash=resolvers.appendhash |
| 24633 |
local loadcachecontent=caches.loadcontent |
| 24634 |
function resolvers.locators.file(specification) |
| 24635 |
local filename=specification.filename |
| 24636 |
local realname=resolveprefix(filename) |
| 24637 |
if realname and realname~='' and isdir(realname) then |
| 24638 |
if trace_locating then |
| 24639 |
report_files("file locator %a found as %a",filename,realname) |
| 24640 |
end |
| 24641 |
appendhash('file',filename,true) |
| 24642 |
elseif trace_locating then |
| 24643 |
report_files("file locator %a not found",filename) |
| 24644 |
end |
| 24645 |
end |
| 24646 |
function resolvers.hashers.file(specification) |
| 24647 |
local pathname=specification.filename |
| 24648 |
local content=loadcachecontent(pathname,'files') |
| 24649 |
registerfilehash(pathname,content,content==nil) |
| 24650 |
end |
| 24651 |
function resolvers.generators.file(specification) |
| 24652 |
local pathname=specification.filename |
| 24653 |
local content=scanfiles(pathname,false,true) |
| 24654 |
registerfilehash(pathname,content,true) |
| 24655 |
end |
| 24656 |
resolvers.concatinators.file=file.join |
| 24657 |
local finders=resolvers.finders |
| 24658 |
local notfound=finders.notfound |
| 24659 |
function finders.file(specification,filetype) |
| 24660 |
local filename=specification.filename |
| 24661 |
local foundname=findfile(filename,filetype) |
| 24662 |
if foundname and foundname~="" then |
| 24663 |
if trace_locating then |
| 24664 |
report_files("file finder: %a found",filename) |
| 24665 |
end |
| 24666 |
return foundname |
| 24667 |
else |
| 24668 |
if trace_locating then |
| 24669 |
report_files("file finder: %a not found",filename) |
| 24670 |
end |
| 24671 |
return notfound() |
| 24672 |
end |
| 24673 |
end |
| 24674 |
local openers=resolvers.openers |
| 24675 |
local notfound=openers.notfound |
| 24676 |
local overloaded=false |
| 24677 |
local function textopener(tag,filename,f) |
| 24678 |
return { |
| 24679 |
reader=function() return f:read () end, |
| 24680 |
close=function() return f:close() end, |
| 24681 |
} |
| 24682 |
end |
| 24683 |
function openers.helpers.textopener(...) |
| 24684 |
return textopener(...) |
| 24685 |
end |
| 24686 |
function openers.helpers.settextopener(opener) |
| 24687 |
if overloaded then |
| 24688 |
report_files("file opener: %s overloaded","already") |
| 24689 |
else |
| 24690 |
if trace_locating then |
| 24691 |
report_files("file opener: %s overloaded","once") |
| 24692 |
end |
| 24693 |
overloaded=true |
| 24694 |
textopener=opener |
| 24695 |
end |
| 24696 |
end |
| 24697 |
function openers.file(specification,filetype) |
| 24698 |
local filename=specification.filename |
| 24699 |
if filename and filename~="" then |
| 24700 |
local f=ioopen(filename,"r") |
| 24701 |
if f then |
| 24702 |
if trace_locating then |
| 24703 |
report_files("file opener: %a opened",filename) |
| 24704 |
end |
| 24705 |
return textopener("file",filename,f) |
| 24706 |
end |
| 24707 |
end |
| 24708 |
if trace_locating then |
| 24709 |
report_files("file opener: %a not found",filename) |
| 24710 |
end |
| 24711 |
return notfound() |
| 24712 |
end |
| 24713 |
local loaders=resolvers.loaders |
| 24714 |
local notfound=loaders.notfound |
| 24715 |
function loaders.file(specification,filetype) |
| 24716 |
local filename=specification.filename |
| 24717 |
if filename and filename~="" then |
| 24718 |
local f=ioopen(filename,"rb") |
| 24719 |
if f then |
| 24720 |
if trace_locating then |
| 24721 |
report_files("file loader: %a loaded",filename) |
| 24722 |
end |
| 24723 |
local s=f:read("*a") |
| 24724 |
f:close() |
| 24725 |
if s then |
| 24726 |
return true,s,#s |
| 24727 |
end |
| 24728 |
end |
| 24729 |
end |
| 24730 |
if trace_locating then |
| 24731 |
report_files("file loader: %a not found",filename) |
| 24732 |
end |
| 24733 |
return notfound() |
| 24734 |
end |
| 24735 |
|
| 24736 |
|
| 24737 |
end -- of closure |
| 24738 |
|
| 24739 |
do -- create closure to overcome 200 locals limit |
| 24740 |
|
| 24741 |
package.loaded["data-con"] = package.loaded["data-con"] or true |
| 24742 |
|
| 24743 |
-- original size: 5733, stripped down to: 3900 |
| 24744 |
|
| 24745 |
if not modules then modules={} end modules ['data-con']={ |
| 24746 |
version=1.100, |
| 24747 |
comment="companion to luat-lib.mkiv", |
| 24748 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 24749 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 24750 |
license="see context related readme files" |
| 24751 |
} |
| 24752 |
local setmetatable=setmetatable |
| 24753 |
local format,lower,gsub=string.format,string.lower,string.gsub |
| 24754 |
local trace_cache=false trackers.register("resolvers.cache",function(v) trace_cache=v end) |
| 24755 |
local trace_containers=false trackers.register("resolvers.containers",function(v) trace_containers=v end) |
| 24756 |
local trace_storage=false trackers.register("resolvers.storage",function(v) trace_storage=v end) |
| 24757 |
containers=containers or {} |
| 24758 |
local containers=containers |
| 24759 |
containers.usecache=true |
| 24760 |
local getwritablepath=caches.getwritablepath |
| 24761 |
local getreadablepaths=caches.getreadablepaths |
| 24762 |
local cacheiswritable=caches.is_writable |
| 24763 |
local loaddatafromcache=caches.loaddata |
| 24764 |
local savedataincache=caches.savedata |
| 24765 |
local report_containers=logs.reporter("resolvers","containers") |
| 24766 |
local allocated={} |
| 24767 |
local cache_format=1.001 |
| 24768 |
local mt={ |
| 24769 |
__index=function(t,k) |
| 24770 |
if k=="writable" then |
| 24771 |
local writable=getwritablepath(t.category,t.subcategory) or { "." } |
| 24772 |
t.writable=writable |
| 24773 |
return writable |
| 24774 |
elseif k=="readables" then |
| 24775 |
local readables=getreadablepaths(t.category,t.subcategory) or { "." } |
| 24776 |
t.readables=readables |
| 24777 |
return readables |
| 24778 |
end |
| 24779 |
end, |
| 24780 |
__storage__=true |
| 24781 |
} |
| 24782 |
function containers.define(category,subcategory,version,enabled,reload) |
| 24783 |
if category and subcategory then |
| 24784 |
local c=allocated[category] |
| 24785 |
if not c then |
| 24786 |
c={} |
| 24787 |
allocated[category]=c |
| 24788 |
end |
| 24789 |
local s=c[subcategory] |
| 24790 |
if not s then |
| 24791 |
s={ |
| 24792 |
category=category, |
| 24793 |
subcategory=subcategory, |
| 24794 |
storage={}, |
| 24795 |
enabled=enabled, |
| 24796 |
reload=reload, |
| 24797 |
version=version or math.pi, |
| 24798 |
trace=false, |
| 24799 |
} |
| 24800 |
setmetatable(s,mt) |
| 24801 |
c[subcategory]=s |
| 24802 |
end |
| 24803 |
return s |
| 24804 |
end |
| 24805 |
end |
| 24806 |
function containers.is_usable(container,name) |
| 24807 |
return container.enabled and caches and cacheiswritable(container.writable,name) |
| 24808 |
end |
| 24809 |
function containers.is_valid(container,name) |
| 24810 |
if name and name~="" then |
| 24811 |
local storage=container.storage[name] |
| 24812 |
return storage |
| 24813 |
and storage.cache_format==cache_format |
| 24814 |
and storage.cache_version==container.version |
| 24815 |
else |
| 24816 |
return false |
| 24817 |
end |
| 24818 |
end |
| 24819 |
function containers.read(container,name) |
| 24820 |
local storage=container.storage |
| 24821 |
local reload=container.reload |
| 24822 |
local stored=not reload and storage[name] |
| 24823 |
if not stored and container.enabled and caches and containers.usecache then |
| 24824 |
stored=loaddatafromcache(container.readables,name,container.writable) |
| 24825 |
if stored and stored.cache_format==cache_format and stored.cache_version==container.version then |
| 24826 |
if trace_cache or trace_containers then |
| 24827 |
report_containers("action %a, category %a, name %a","load",container.subcategory,name) |
| 24828 |
end |
| 24829 |
else |
| 24830 |
stored=nil |
| 24831 |
end |
| 24832 |
storage[name]=stored |
| 24833 |
elseif stored then |
| 24834 |
if trace_cache or trace_containers then |
| 24835 |
report_containers("action %a, category %a, name %a","reuse",container.subcategory,name) |
| 24836 |
end |
| 24837 |
end |
| 24838 |
return stored |
| 24839 |
end |
| 24840 |
function containers.write(container,name,data,fast) |
| 24841 |
if data then |
| 24842 |
data.cache_format=cache_format |
| 24843 |
data.cache_version=container.version |
| 24844 |
if container.enabled and caches then |
| 24845 |
local unique=data.unique |
| 24846 |
local shared=data.shared |
| 24847 |
data.unique=nil |
| 24848 |
data.shared=nil |
| 24849 |
savedataincache(container.writable,name,data,fast) |
| 24850 |
if trace_cache or trace_containers then |
| 24851 |
report_containers("action %a, category %a, name %a","save",container.subcategory,name) |
| 24852 |
end |
| 24853 |
data.unique=unique |
| 24854 |
data.shared=shared |
| 24855 |
end |
| 24856 |
if trace_cache or trace_containers then |
| 24857 |
report_containers("action %a, category %a, name %a","store",container.subcategory,name) |
| 24858 |
end |
| 24859 |
container.storage[name]=data |
| 24860 |
end |
| 24861 |
return data |
| 24862 |
end |
| 24863 |
function containers.content(container,name) |
| 24864 |
return container.storage[name] |
| 24865 |
end |
| 24866 |
function containers.cleanname(name) |
| 24867 |
return (gsub(lower(name),"[^%w\128-\255]+","-")) |
| 24868 |
end |
| 24869 |
|
| 24870 |
|
| 24871 |
end -- of closure |
| 24872 |
|
| 24873 |
do -- create closure to overcome 200 locals limit |
| 24874 |
|
| 24875 |
package.loaded["data-use"] = package.loaded["data-use"] or true |
| 24876 |
|
| 24877 |
-- original size: 6168, stripped down to: 3201 |
| 24878 |
|
| 24879 |
if not modules then modules={} end modules ['data-use']={ |
| 24880 |
version=1.001, |
| 24881 |
comment="companion to luat-lib.mkiv", |
| 24882 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 24883 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 24884 |
license="see context related readme files" |
| 24885 |
} |
| 24886 |
local format=string.format |
| 24887 |
local trace_locating=false trackers.register("resolvers.locating",function(v) trace_locating=v end) |
| 24888 |
local report_mounts=logs.reporter("resolvers","mounts") |
| 24889 |
local resolvers=resolvers |
| 24890 |
local findfile=resolvers.findfile |
| 24891 |
statistics.register("used config file",function() return caches.configfiles() end) |
| 24892 |
statistics.register("used cache path",function() return caches.usedpaths() end) |
| 24893 |
function statistics.savefmtstatus(texname,formatbanner,sourcefile,banner) |
| 24894 |
local enginebanner=status.banner |
| 24895 |
if formatbanner and enginebanner and sourcefile then |
| 24896 |
local luvname=file.replacesuffix(texname,"luv") |
| 24897 |
local luvdata={ |
| 24898 |
enginebanner=enginebanner, |
| 24899 |
formatbanner=formatbanner, |
| 24900 |
sourcehash=md5.hex(io.loaddata(findfile(sourcefile)) or "unknown"), |
| 24901 |
sourcefile=sourcefile, |
| 24902 |
luaversion=LUAVERSION, |
| 24903 |
luaformat=LUAFORMAT or 0, |
| 24904 |
formatid=LUATEXFORMATID, |
| 24905 |
functionality=LUATEXFUNCTIONALITY, |
| 24906 |
} |
| 24907 |
io.savedata(luvname,table.serialize(luvdata,true)) |
| 24908 |
lua.registerinitexfinalizer(function() |
| 24909 |
if jit then |
| 24910 |
logs.report("format banner","%s lua: %s jit",banner,LUAVERSION) |
| 24911 |
else |
| 24912 |
logs.report("format banner","%s lua: %s, format: %s",banner,LUAVERSION,LUAFORMAT) |
| 24913 |
end |
| 24914 |
logs.newline() |
| 24915 |
end,"show banner") |
| 24916 |
end |
| 24917 |
end |
| 24918 |
function statistics.checkfmtstatus(texname) |
| 24919 |
local enginebanner=status.banner |
| 24920 |
if enginebanner and texname then |
| 24921 |
local luvname=file.replacesuffix(texname,"luv") |
| 24922 |
if lfs.isfile(luvname) then |
| 24923 |
local luv=dofile(luvname) |
| 24924 |
if luv and luv.sourcefile then |
| 24925 |
local sourcehash=md5.hex(io.loaddata(findfile(luv.sourcefile)) or "unknown") |
| 24926 |
local luvbanner=luv.enginebanner or "?" |
| 24927 |
if luvbanner~=enginebanner then |
| 24928 |
return format("engine mismatch (luv: %s <> bin: %s)",luvbanner,enginebanner) |
| 24929 |
end |
| 24930 |
local luvhash=luv.sourcehash or "?" |
| 24931 |
if luvhash~=sourcehash then |
| 24932 |
return format("source mismatch (luv: %s <> bin: %s)",luvhash,sourcehash) |
| 24933 |
end |
| 24934 |
local luvluaversion=luv.luaversion or 0 |
| 24935 |
local engluaversion=LUAVERSION or 0 |
| 24936 |
if luvluaversion~=engluaversion then |
| 24937 |
return format("lua mismatch (luv: %s <> bin: %s)",luvluaversion,engluaversion) |
| 24938 |
end |
| 24939 |
local luvluaformat=luv.luaformat or 0 |
| 24940 |
local engluaformat=LUAFORMAT or 0 |
| 24941 |
if luvluaformat~=engluaformat then |
| 24942 |
return format("lua bytecode format mismatch (luv: %s <> bin: %s)",luvluaformat,engluaformat) |
| 24943 |
end |
| 24944 |
local luvfunctionality=luv.functionality or 0 |
| 24945 |
local engfunctionality=status.development_id or 0 |
| 24946 |
if luvfunctionality~=engfunctionality then |
| 24947 |
return format("functionality mismatch (luv: %s <> bin: %s)",luvfunctionality,engfunctionality) |
| 24948 |
end |
| 24949 |
local luvformatid=luv.formatid or 0 |
| 24950 |
local engformatid=status.format_id or 0 |
| 24951 |
if luvformatid~=engformatid then |
| 24952 |
return format("formatid mismatch (luv: %s <> bin: %s)",luvformatid,engformatid) |
| 24953 |
end |
| 24954 |
else |
| 24955 |
return "invalid status file" |
| 24956 |
end |
| 24957 |
else |
| 24958 |
return "missing status file" |
| 24959 |
end |
| 24960 |
end |
| 24961 |
return true |
| 24962 |
end |
| 24963 |
|
| 24964 |
|
| 24965 |
end -- of closure |
| 24966 |
|
| 24967 |
do -- create closure to overcome 200 locals limit |
| 24968 |
|
| 24969 |
package.loaded["data-zip"] = package.loaded["data-zip"] or true |
| 24970 |
|
| 24971 |
-- original size: 10789, stripped down to: 7951 |
| 24972 |
|
| 24973 |
if not modules then modules={} end modules ['data-zip']={ |
| 24974 |
version=1.001, |
| 24975 |
comment="companion to luat-lib.mkiv", |
| 24976 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 24977 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 24978 |
license="see context related readme files" |
| 24979 |
} |
| 24980 |
local format,find,match=string.format,string.find,string.match |
| 24981 |
local trace_locating=false trackers.register("resolvers.locating",function(v) trace_locating=v end) |
| 24982 |
local report_zip=logs.reporter("resolvers","zip") |
| 24983 |
local resolvers=resolvers |
| 24984 |
local findfile=resolvers.findfile |
| 24985 |
local registerfile=resolvers.registerfile |
| 24986 |
local splitmethod=resolvers.splitmethod |
| 24987 |
local prependhash=resolvers.prependhash |
| 24988 |
local starttiming=resolvers.starttiming |
| 24989 |
local extendtexmf=resolvers.extendtexmfvariable |
| 24990 |
local stoptiming=resolvers.stoptiming |
| 24991 |
local urlquery=url.query |
| 24992 |
zip=zip or {} |
| 24993 |
local zip=zip |
| 24994 |
local archives=zip.archives or {} |
| 24995 |
zip.archives=archives |
| 24996 |
local registeredfiles=zip.registeredfiles or {} |
| 24997 |
zip.registeredfiles=registeredfiles |
| 24998 |
local zipfiles=utilities.zipfiles |
| 24999 |
local openzip,closezip,validfile,wholefile,filehandle,traversezip |
| 25000 |
if zipfiles then |
| 25001 |
local ipairs=ipairs |
| 25002 |
openzip=zipfiles.open |
| 25003 |
closezip=zipfiles.close |
| 25004 |
validfile=zipfiles.found |
| 25005 |
wholefile=zipfiles.unzip |
| 25006 |
local listzip=zipfiles.list |
| 25007 |
traversezip=function(zfile) |
| 25008 |
return ipairs(listzip(zfile)) |
| 25009 |
end |
| 25010 |
local streams=utilities.streams |
| 25011 |
local openstream=streams.open |
| 25012 |
local readstring=streams.readstring |
| 25013 |
local streamsize=streams.size |
| 25014 |
local metatable={ |
| 25015 |
close=streams.close, |
| 25016 |
read=function(stream,n) |
| 25017 |
readstring(stream,n=="*a" and streamsize(stream) or n) |
| 25018 |
end |
| 25019 |
} |
| 25020 |
filehandle=function(zfile,queryname) |
| 25021 |
local data=wholefile(zfile,queryname) |
| 25022 |
if data then |
| 25023 |
local stream=openstream(data) |
| 25024 |
if stream then |
| 25025 |
return setmetatableindex(stream,metatable) |
| 25026 |
end |
| 25027 |
end |
| 25028 |
end |
| 25029 |
else |
| 25030 |
openzip=zip.open |
| 25031 |
closezip=zip.close |
| 25032 |
validfile=function(zfile,queryname) |
| 25033 |
local dfile=zfile:open(queryname) |
| 25034 |
if dfile then |
| 25035 |
dfile:close() |
| 25036 |
return true |
| 25037 |
end |
| 25038 |
return false |
| 25039 |
end |
| 25040 |
traversezip=function(zfile) |
| 25041 |
return z:files() |
| 25042 |
end |
| 25043 |
wholefile=function(zfile,queryname) |
| 25044 |
local dfile=zfile:open(queryname) |
| 25045 |
if dfile then |
| 25046 |
local s=dfile:read("*all") |
| 25047 |
dfile:close() |
| 25048 |
return s |
| 25049 |
end |
| 25050 |
end |
| 25051 |
filehandle=function(zfile,queryname) |
| 25052 |
local dfile=zfile:open(queryname) |
| 25053 |
if dfile then |
| 25054 |
return dfile |
| 25055 |
end |
| 25056 |
end |
| 25057 |
end |
| 25058 |
local function validzip(str) |
| 25059 |
if not find(str,"^zip://") then |
| 25060 |
return "zip:///"..str |
| 25061 |
else |
| 25062 |
return str |
| 25063 |
end |
| 25064 |
end |
| 25065 |
local function openarchive(name) |
| 25066 |
if not name or name=="" then |
| 25067 |
return nil |
| 25068 |
else |
| 25069 |
local arch=archives[name] |
| 25070 |
if not arch then |
| 25071 |
local full=findfile(name) or "" |
| 25072 |
arch=full~="" and openzip(full) or false |
| 25073 |
archives[name]=arch |
| 25074 |
end |
| 25075 |
return arch |
| 25076 |
end |
| 25077 |
end |
| 25078 |
local function closearchive(name) |
| 25079 |
if not name or (name=="" and archives[name]) then |
| 25080 |
closezip(archives[name]) |
| 25081 |
archives[name]=nil |
| 25082 |
end |
| 25083 |
end |
| 25084 |
zip.openarchive=openarchive |
| 25085 |
zip.closearchive=closearchive |
| 25086 |
function resolvers.locators.zip(specification) |
| 25087 |
local archive=specification.filename |
| 25088 |
local zipfile=archive and archive~="" and openarchive(archive) |
| 25089 |
if trace_locating then |
| 25090 |
if zipfile then |
| 25091 |
report_zip("locator: archive %a found",archive) |
| 25092 |
else |
| 25093 |
report_zip("locator: archive %a not found",archive) |
| 25094 |
end |
| 25095 |
end |
| 25096 |
end |
| 25097 |
function resolvers.concatinators.zip(zipfile,path,name) |
| 25098 |
if not path or path=="" then |
| 25099 |
return format('%s?name=%s',zipfile,name) |
| 25100 |
else |
| 25101 |
return format('%s?name=%s/%s',zipfile,path,name) |
| 25102 |
end |
| 25103 |
end |
| 25104 |
local finders=resolvers.finders |
| 25105 |
local notfound=finders.notfound |
| 25106 |
function finders.zip(specification) |
| 25107 |
local original=specification.original |
| 25108 |
local archive=specification.filename |
| 25109 |
if archive then |
| 25110 |
local query=urlquery(specification.query) |
| 25111 |
local queryname=query.name |
| 25112 |
if queryname then |
| 25113 |
local zfile=openarchive(archive) |
| 25114 |
if zfile then |
| 25115 |
if trace_locating then |
| 25116 |
report_zip("finder: archive %a found",archive) |
| 25117 |
end |
| 25118 |
if validfile(zfile,queryname) then |
| 25119 |
if trace_locating then |
| 25120 |
report_zip("finder: file %a found",queryname) |
| 25121 |
end |
| 25122 |
return specification.original |
| 25123 |
elseif trace_locating then |
| 25124 |
report_zip("finder: file %a not found",queryname) |
| 25125 |
end |
| 25126 |
elseif trace_locating then |
| 25127 |
report_zip("finder: unknown archive %a",archive) |
| 25128 |
end |
| 25129 |
end |
| 25130 |
end |
| 25131 |
if trace_locating then |
| 25132 |
report_zip("finder: %a not found",original) |
| 25133 |
end |
| 25134 |
return notfound() |
| 25135 |
end |
| 25136 |
local openers=resolvers.openers |
| 25137 |
local notfound=openers.notfound |
| 25138 |
local textopener=openers.helpers.textopener |
| 25139 |
function openers.zip(specification) |
| 25140 |
local original=specification.original |
| 25141 |
local archive=specification.filename |
| 25142 |
if archive then |
| 25143 |
local query=urlquery(specification.query) |
| 25144 |
local queryname=query.name |
| 25145 |
if queryname then |
| 25146 |
local zfile=openarchive(archive) |
| 25147 |
if zfile then |
| 25148 |
if trace_locating then |
| 25149 |
report_zip("opener; archive %a opened",archive) |
| 25150 |
end |
| 25151 |
local handle=filehandle(zfile,queryname) |
| 25152 |
if handle then |
| 25153 |
if trace_locating then |
| 25154 |
report_zip("opener: file %a found",queryname) |
| 25155 |
end |
| 25156 |
return textopener('zip',original,handle) |
| 25157 |
elseif trace_locating then |
| 25158 |
report_zip("opener: file %a not found",queryname) |
| 25159 |
end |
| 25160 |
elseif trace_locating then |
| 25161 |
report_zip("opener: unknown archive %a",archive) |
| 25162 |
end |
| 25163 |
end |
| 25164 |
end |
| 25165 |
if trace_locating then |
| 25166 |
report_zip("opener: %a not found",original) |
| 25167 |
end |
| 25168 |
return notfound() |
| 25169 |
end |
| 25170 |
local loaders=resolvers.loaders |
| 25171 |
local notfound=loaders.notfound |
| 25172 |
function loaders.zip(specification) |
| 25173 |
local original=specification.original |
| 25174 |
local archive=specification.filename |
| 25175 |
if archive then |
| 25176 |
local query=urlquery(specification.query) |
| 25177 |
local queryname=query.name |
| 25178 |
if queryname then |
| 25179 |
local zfile=openarchive(archive) |
| 25180 |
if zfile then |
| 25181 |
if trace_locating then |
| 25182 |
report_zip("loader: archive %a opened",archive) |
| 25183 |
end |
| 25184 |
local data=wholefile(zfile,queryname) |
| 25185 |
if data then |
| 25186 |
if trace_locating then |
| 25187 |
report_zip("loader; file %a loaded",original) |
| 25188 |
end |
| 25189 |
return true,data,#data |
| 25190 |
elseif trace_locating then |
| 25191 |
report_zip("loader: file %a not found",queryname) |
| 25192 |
end |
| 25193 |
elseif trace_locating then |
| 25194 |
report_zip("loader; unknown archive %a",archive) |
| 25195 |
end |
| 25196 |
end |
| 25197 |
end |
| 25198 |
if trace_locating then |
| 25199 |
report_zip("loader: %a not found",original) |
| 25200 |
end |
| 25201 |
return notfound() |
| 25202 |
end |
| 25203 |
local function registerzipfile(z,tree) |
| 25204 |
local names={} |
| 25205 |
local files={} |
| 25206 |
local remap={} |
| 25207 |
local n=0 |
| 25208 |
local filter=tree=="" and "^(.+)/(.-)$" or format("^%s/(.+)/(.-)$",tree) |
| 25209 |
if trace_locating then |
| 25210 |
report_zip("registering: using filter %a",filter) |
| 25211 |
end |
| 25212 |
starttiming() |
| 25213 |
for i in traversezip(z) do |
| 25214 |
local filename=i.filename |
| 25215 |
local path,name=match(filename,filter) |
| 25216 |
if not path then |
| 25217 |
n=n+1 |
| 25218 |
registerfile(names,filename,"") |
| 25219 |
local usedname=lower(filename) |
| 25220 |
files[usedname]="" |
| 25221 |
if usedname~=filename then |
| 25222 |
remap[usedname]=filename |
| 25223 |
end |
| 25224 |
elseif name and name~="" then |
| 25225 |
n=n+1 |
| 25226 |
register(names,name,path) |
| 25227 |
local usedname=lower(name) |
| 25228 |
files[usedname]=path |
| 25229 |
if usedname~=name then |
| 25230 |
remap[usedname]=name |
| 25231 |
end |
| 25232 |
else |
| 25233 |
end |
| 25234 |
end |
| 25235 |
stoptiming() |
| 25236 |
report_zip("registering: %s files registered",n) |
| 25237 |
return { |
| 25238 |
files=files, |
| 25239 |
remap=remap, |
| 25240 |
} |
| 25241 |
end |
| 25242 |
local function usezipfile(archive) |
| 25243 |
local specification=splitmethod(archive) |
| 25244 |
local archive=specification.filename |
| 25245 |
if archive and not registeredfiles[archive] then |
| 25246 |
local z=openarchive(archive) |
| 25247 |
if z then |
| 25248 |
local tree=urlquery(specification.query).tree or "" |
| 25249 |
if trace_locating then |
| 25250 |
report_zip("registering: archive %a",archive) |
| 25251 |
end |
| 25252 |
prependhash('zip',archive) |
| 25253 |
extendtexmf(archive) |
| 25254 |
registeredfiles[archive]=z |
| 25255 |
registerfilehash(archive,registerzipfile(z,tree)) |
| 25256 |
elseif trace_locating then |
| 25257 |
report_zip("registering: unknown archive %a",archive) |
| 25258 |
end |
| 25259 |
elseif trace_locating then |
| 25260 |
report_zip("registering: archive %a not found",archive) |
| 25261 |
end |
| 25262 |
end |
| 25263 |
resolvers.usezipfile=usezipfile |
| 25264 |
resolvers.registerzipfile=registerzipfile |
| 25265 |
function resolvers.hashers.zip(specification) |
| 25266 |
local archive=specification.filename |
| 25267 |
if trace_locating then |
| 25268 |
report_zip("loading file %a",archive) |
| 25269 |
end |
| 25270 |
usezipfile(specification.original) |
| 25271 |
end |
| 25272 |
|
| 25273 |
|
| 25274 |
end -- of closure |
| 25275 |
|
| 25276 |
do -- create closure to overcome 200 locals limit |
| 25277 |
|
| 25278 |
package.loaded["data-tre"] = package.loaded["data-tre"] or true |
| 25279 |
|
| 25280 |
-- original size: 10802, stripped down to: 6619 |
| 25281 |
|
| 25282 |
if not modules then modules={} end modules ['data-tre']={ |
| 25283 |
version=1.001, |
| 25284 |
comment="companion to luat-lib.mkiv", |
| 25285 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 25286 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 25287 |
license="see context related readme files" |
| 25288 |
} |
| 25289 |
local type=type |
| 25290 |
local find,gsub,lower=string.find,string.gsub,string.lower |
| 25291 |
local basename,dirname,joinname=file.basename,file.dirname,file.join |
| 25292 |
local globdir,isdir,isfile=dir.glob,lfs.isdir,lfs.isfile |
| 25293 |
local P,lpegmatch=lpeg.P,lpeg.match |
| 25294 |
local trace_locating=false trackers.register("resolvers.locating",function(v) trace_locating=v end) |
| 25295 |
local report_trees=logs.reporter("resolvers","trees") |
| 25296 |
local resolvers=resolvers |
| 25297 |
local finders=resolvers.finders |
| 25298 |
local openers=resolvers.openers |
| 25299 |
local loaders=resolvers.loaders |
| 25300 |
local locators=resolvers.locators |
| 25301 |
local hashers=resolvers.hashers |
| 25302 |
local generators=resolvers.generators |
| 25303 |
do |
| 25304 |
local collectors={} |
| 25305 |
local found={} |
| 25306 |
local notfound=finders.notfound |
| 25307 |
function finders.tree(specification) |
| 25308 |
local spec=specification.filename |
| 25309 |
local okay=found[spec] |
| 25310 |
if okay==nil then |
| 25311 |
if spec~="" then |
| 25312 |
local path=dirname(spec) |
| 25313 |
local name=basename(spec) |
| 25314 |
if path=="" then |
| 25315 |
path="." |
| 25316 |
end |
| 25317 |
local names=collectors[path] |
| 25318 |
if not names then |
| 25319 |
local pattern=find(path,"/%*+$") and path or (path.."/*") |
| 25320 |
names=globdir(pattern) |
| 25321 |
collectors[path]=names |
| 25322 |
end |
| 25323 |
local pattern="/"..gsub(name,"([%.%-%+])","%%%1").."$" |
| 25324 |
for i=1,#names do |
| 25325 |
local fullname=names[i] |
| 25326 |
if find(fullname,pattern) then |
| 25327 |
found[spec]=fullname |
| 25328 |
return fullname |
| 25329 |
end |
| 25330 |
end |
| 25331 |
local pattern=lower(pattern) |
| 25332 |
for i=1,#names do |
| 25333 |
local fullname=lower(names[i]) |
| 25334 |
if find(fullname,pattern) then |
| 25335 |
if isfile(fullname) then |
| 25336 |
found[spec]=fullname |
| 25337 |
return fullname |
| 25338 |
else |
| 25339 |
break |
| 25340 |
end |
| 25341 |
end |
| 25342 |
end |
| 25343 |
end |
| 25344 |
okay=notfound() |
| 25345 |
found[spec]=okay |
| 25346 |
end |
| 25347 |
return okay |
| 25348 |
end |
| 25349 |
end |
| 25350 |
do |
| 25351 |
local resolveprefix=resolvers.resolve |
| 25352 |
local appendhash=resolvers.appendhash |
| 25353 |
local function dolocate(specification) |
| 25354 |
local name=specification.filename |
| 25355 |
local realname=resolveprefix(name) |
| 25356 |
if realname and realname~='' and isdir(realname) then |
| 25357 |
if trace_locating then |
| 25358 |
report_trees("locator %a found",realname) |
| 25359 |
end |
| 25360 |
appendhash('tree',name,false) |
| 25361 |
elseif trace_locating then |
| 25362 |
report_trees("locator %a not found",name) |
| 25363 |
end |
| 25364 |
end |
| 25365 |
locators.tree=dolocate |
| 25366 |
locators.dirlist=dolocate |
| 25367 |
locators.dirfile=dolocate |
| 25368 |
end |
| 25369 |
do |
| 25370 |
local filegenerator=generators.file |
| 25371 |
generators.dirlist=filegenerator |
| 25372 |
generators.dirfile=filegenerator |
| 25373 |
end |
| 25374 |
do |
| 25375 |
local filegenerator=generators.file |
| 25376 |
local methodhandler=resolvers.methodhandler |
| 25377 |
local function dohash(specification) |
| 25378 |
local name=specification.filename |
| 25379 |
if trace_locating then |
| 25380 |
report_trees("analyzing %a",name) |
| 25381 |
end |
| 25382 |
methodhandler("hashers",name) |
| 25383 |
filegenerator(specification) |
| 25384 |
end |
| 25385 |
hashers.tree=dohash |
| 25386 |
hashers.dirlist=dohash |
| 25387 |
hashers.dirfile=dohash |
| 25388 |
end |
| 25389 |
local resolve do |
| 25390 |
local collectors={} |
| 25391 |
local splitter=lpeg.splitat("/**/") |
| 25392 |
local stripper=lpeg.replacer { [P("/")*P("*")^1*P(-1)]="" } |
| 25393 |
local loadcontent=caches.loadcontent |
| 25394 |
local savecontent=caches.savecontent |
| 25395 |
local notfound=finders.notfound |
| 25396 |
local scanfiles=resolvers.scanfiles |
| 25397 |
local lookup=resolvers.get_from_content |
| 25398 |
table.setmetatableindex(collectors,function(t,k) |
| 25399 |
local rootname=lpegmatch(stripper,k) |
| 25400 |
local dataname=joinname(rootname,"dirlist") |
| 25401 |
local content=loadcontent(dataname,"files",dataname) |
| 25402 |
if not content then |
| 25403 |
content=scanfiles(rootname,nil,nil,false,true) |
| 25404 |
savecontent(dataname,"files",content,dataname) |
| 25405 |
end |
| 25406 |
t[k]=content |
| 25407 |
return content |
| 25408 |
end) |
| 25409 |
local function checked(root,p,n) |
| 25410 |
if p then |
| 25411 |
if type(p)=="table" then |
| 25412 |
for i=1,#p do |
| 25413 |
local fullname=joinname(root,p[i],n) |
| 25414 |
if isfile(fullname) then |
| 25415 |
return fullname |
| 25416 |
end |
| 25417 |
end |
| 25418 |
else |
| 25419 |
local fullname=joinname(root,p,n) |
| 25420 |
if isfile(fullname) then |
| 25421 |
return fullname |
| 25422 |
end |
| 25423 |
end |
| 25424 |
end |
| 25425 |
return notfound() |
| 25426 |
end |
| 25427 |
resolve=function(specification) |
| 25428 |
local filename=specification.filename |
| 25429 |
if filename~="" then |
| 25430 |
local root,rest=lpegmatch(splitter,filename) |
| 25431 |
if root and rest then |
| 25432 |
local path,name=dirname(rest),basename(rest) |
| 25433 |
if name~=rest then |
| 25434 |
local content=collectors[root] |
| 25435 |
local p,n=lookup(content,name) |
| 25436 |
if not p then |
| 25437 |
return notfound() |
| 25438 |
end |
| 25439 |
local pattern=".*/"..path.."$" |
| 25440 |
local istable=type(p)=="table" |
| 25441 |
if istable then |
| 25442 |
for i=1,#p do |
| 25443 |
local pi=p[i] |
| 25444 |
if pi==path or find(pi,pattern) then |
| 25445 |
local fullname=joinname(root,pi,n) |
| 25446 |
if isfile(fullname) then |
| 25447 |
return fullname |
| 25448 |
end |
| 25449 |
end |
| 25450 |
end |
| 25451 |
elseif p==path or find(p,pattern) then |
| 25452 |
local fullname=joinname(root,p,n) |
| 25453 |
if isfile(fullname) then |
| 25454 |
return fullname |
| 25455 |
end |
| 25456 |
end |
| 25457 |
local queries=specification.queries |
| 25458 |
if queries and queries.option=="fileonly" then |
| 25459 |
return checked(root,p,n) |
| 25460 |
else |
| 25461 |
return notfound() |
| 25462 |
end |
| 25463 |
end |
| 25464 |
end |
| 25465 |
local path=dirname(filename) |
| 25466 |
local name=basename(filename) |
| 25467 |
local root=lpegmatch(stripper,path) |
| 25468 |
local content=collectors[path] |
| 25469 |
local p,n=lookup(content,name) |
| 25470 |
if p then |
| 25471 |
return checked(root,p,n) |
| 25472 |
end |
| 25473 |
end |
| 25474 |
return notfound() |
| 25475 |
end |
| 25476 |
finders.dirlist=resolve |
| 25477 |
function finders.dirfile(specification) |
| 25478 |
local queries=specification.queries |
| 25479 |
if queries then |
| 25480 |
queries.option="fileonly" |
| 25481 |
else |
| 25482 |
specification.queries={ option="fileonly" } |
| 25483 |
end |
| 25484 |
return resolve(specification) |
| 25485 |
end |
| 25486 |
end |
| 25487 |
do |
| 25488 |
local fileopener=openers.file |
| 25489 |
local fileloader=loaders.file |
| 25490 |
openers.dirlist=fileopener |
| 25491 |
loaders.dirlist=fileloader |
| 25492 |
openers.dirfile=fileopener |
| 25493 |
loaders.dirfile=fileloader |
| 25494 |
end |
| 25495 |
do |
| 25496 |
local hashfile="dirhash.lua" |
| 25497 |
local kind="HASH256" |
| 25498 |
local version=1.0 |
| 25499 |
local loadtable=table.load |
| 25500 |
local savetable=table.save |
| 25501 |
local loaddata=io.loaddata |
| 25502 |
function resolvers.dirstatus(patterns) |
| 25503 |
local t=type(patterns) |
| 25504 |
if t=="string" then |
| 25505 |
patterns={ patterns } |
| 25506 |
elseif t~="table" then |
| 25507 |
return false |
| 25508 |
end |
| 25509 |
local status=loadtable(hashfile) |
| 25510 |
if not status or status.version~=version or status.kind~=kind then |
| 25511 |
status={ |
| 25512 |
version=1.0, |
| 25513 |
kind=kind, |
| 25514 |
hashes={}, |
| 25515 |
} |
| 25516 |
end |
| 25517 |
local hashes=status.hashes |
| 25518 |
local changed={} |
| 25519 |
local action=sha2[kind] |
| 25520 |
local update={} |
| 25521 |
for i=1,#patterns do |
| 25522 |
local pattern=patterns[i] |
| 25523 |
local files=globdir(pattern) |
| 25524 |
for i=1,#files do |
| 25525 |
local name=files[i] |
| 25526 |
local hash=action(loaddata(name)) |
| 25527 |
if hashes[name]~=hash then |
| 25528 |
changed[#changed+1]=name |
| 25529 |
end |
| 25530 |
update[name]=hash |
| 25531 |
end |
| 25532 |
end |
| 25533 |
status.hashes=update |
| 25534 |
savetable(hashfile,status) |
| 25535 |
return #changed>0 and changed or false |
| 25536 |
end |
| 25537 |
end |
| 25538 |
|
| 25539 |
|
| 25540 |
end -- of closure |
| 25541 |
|
| 25542 |
do -- create closure to overcome 200 locals limit |
| 25543 |
|
| 25544 |
package.loaded["data-sch"] = package.loaded["data-sch"] or true |
| 25545 |
|
| 25546 |
-- original size: 6945, stripped down to: 5408 |
| 25547 |
|
| 25548 |
if not modules then modules={} end modules ['data-sch']={ |
| 25549 |
version=1.001, |
| 25550 |
comment="companion to luat-lib.mkiv", |
| 25551 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 25552 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 25553 |
license="see context related readme files" |
| 25554 |
} |
| 25555 |
local load,tonumber=load,tonumber |
| 25556 |
local gsub,format=string.gsub,string.format |
| 25557 |
local sortedhash,concat=table.sortedhash,table.concat |
| 25558 |
local finders,openers,loaders=resolvers.finders,resolvers.openers,resolvers.loaders |
| 25559 |
local addsuffix,suffix,splitbase=file.addsuffix,file.suffix,file.splitbase |
| 25560 |
local md5hex=md5.hex |
| 25561 |
local trace_schemes=false trackers.register("resolvers.schemes",function(v) trace_schemes=v end) |
| 25562 |
local report_schemes=logs.reporter("resolvers","schemes") |
| 25563 |
local http=require("socket.http") |
| 25564 |
local ltn12=require("ltn12") |
| 25565 |
if mbox then mbox=nil end |
| 25566 |
local resolvers=resolvers |
| 25567 |
local schemes=resolvers.schemes or {} |
| 25568 |
resolvers.schemes=schemes |
| 25569 |
local cleaners={} |
| 25570 |
schemes.cleaners=cleaners |
| 25571 |
local threshold=24*60*60 |
| 25572 |
directives.register("schemes.threshold",function(v) threshold=tonumber(v) or threshold end) |
| 25573 |
function cleaners.none(specification) |
| 25574 |
return specification.original |
| 25575 |
end |
| 25576 |
function cleaners.strip(specification) |
| 25577 |
local path,name=splitbase(specification.original) |
| 25578 |
if path=="" then |
| 25579 |
return (gsub(name,"[^%a%d%.]+","-")) |
| 25580 |
else |
| 25581 |
return (gsub((gsub(path,"%.","-").."-"..name),"[^%a%d%.]+","-")) |
| 25582 |
end |
| 25583 |
end |
| 25584 |
function cleaners.md5(specification) |
| 25585 |
return addsuffix(md5hex(specification.original),suffix(specification.path)) |
| 25586 |
end |
| 25587 |
local cleaner=cleaners.strip |
| 25588 |
directives.register("schemes.cleanmethod",function(v) cleaner=cleaners[v] or cleaners.strip end) |
| 25589 |
function resolvers.schemes.cleanname(specification) |
| 25590 |
local hash=cleaner(specification) |
| 25591 |
if trace_schemes then |
| 25592 |
report_schemes("hashing %a to %a",specification.original,hash) |
| 25593 |
end |
| 25594 |
return hash |
| 25595 |
end |
| 25596 |
local cached={} |
| 25597 |
local loaded={} |
| 25598 |
local reused={} |
| 25599 |
local thresholds={} |
| 25600 |
local handlers={} |
| 25601 |
local runner=sandbox.registerrunner { |
| 25602 |
name="curl resolver", |
| 25603 |
method="execute", |
| 25604 |
program="curl", |
| 25605 |
template='--silent --insecure --create-dirs --output "%cachename%" "%original%"', |
| 25606 |
checkers={ |
| 25607 |
cachename="cache", |
| 25608 |
original="url", |
| 25609 |
} |
| 25610 |
} |
| 25611 |
local function fetch(specification) |
| 25612 |
local original=specification.original |
| 25613 |
local scheme=specification.scheme |
| 25614 |
local cleanname=schemes.cleanname(specification) |
| 25615 |
local cachename=caches.setfirstwritablefile(cleanname,"schemes") |
| 25616 |
if not cached[original] then |
| 25617 |
statistics.starttiming(schemes) |
| 25618 |
if not io.exists(cachename) or (os.difftime(os.time(),lfs.attributes(cachename).modification)>(thresholds[protocol] or threshold)) then |
| 25619 |
cached[original]=cachename |
| 25620 |
local handler=handlers[scheme] |
| 25621 |
if handler then |
| 25622 |
if trace_schemes then |
| 25623 |
report_schemes("fetching %a, protocol %a, method %a",original,scheme,"built-in") |
| 25624 |
end |
| 25625 |
logs.flush() |
| 25626 |
handler(specification,cachename) |
| 25627 |
else |
| 25628 |
if trace_schemes then |
| 25629 |
report_schemes("fetching %a, protocol %a, method %a",original,scheme,"curl") |
| 25630 |
end |
| 25631 |
logs.flush() |
| 25632 |
runner { |
| 25633 |
original=original, |
| 25634 |
cachename=cachename, |
| 25635 |
} |
| 25636 |
end |
| 25637 |
end |
| 25638 |
if io.exists(cachename) then |
| 25639 |
cached[original]=cachename |
| 25640 |
if trace_schemes then |
| 25641 |
report_schemes("using cached %a, protocol %a, cachename %a",original,scheme,cachename) |
| 25642 |
end |
| 25643 |
else |
| 25644 |
cached[original]="" |
| 25645 |
if trace_schemes then |
| 25646 |
report_schemes("using missing %a, protocol %a",original,scheme) |
| 25647 |
end |
| 25648 |
end |
| 25649 |
loaded[scheme]=loaded[scheme]+1 |
| 25650 |
statistics.stoptiming(schemes) |
| 25651 |
else |
| 25652 |
if trace_schemes then |
| 25653 |
report_schemes("reusing %a, protocol %a",original,scheme) |
| 25654 |
end |
| 25655 |
reused[scheme]=reused[scheme]+1 |
| 25656 |
end |
| 25657 |
return cached[original] |
| 25658 |
end |
| 25659 |
local function finder(specification,filetype) |
| 25660 |
return resolvers.methodhandler("finders",fetch(specification),filetype) |
| 25661 |
end |
| 25662 |
local opener=openers.file |
| 25663 |
local loader=loaders.file |
| 25664 |
local function install(scheme,handler,newthreshold) |
| 25665 |
handlers [scheme]=handler |
| 25666 |
loaded [scheme]=0 |
| 25667 |
reused [scheme]=0 |
| 25668 |
finders [scheme]=finder |
| 25669 |
openers [scheme]=opener |
| 25670 |
loaders [scheme]=loader |
| 25671 |
thresholds[scheme]=newthreshold or threshold |
| 25672 |
end |
| 25673 |
schemes.install=install |
| 25674 |
local function http_handler(specification,cachename) |
| 25675 |
local tempname=cachename..".tmp" |
| 25676 |
local handle=io.open(tempname,"wb") |
| 25677 |
local status,message=http.request { |
| 25678 |
url=specification.original, |
| 25679 |
sink=ltn12.sink.file(handle) |
| 25680 |
} |
| 25681 |
if not status then |
| 25682 |
os.remove(tempname) |
| 25683 |
else |
| 25684 |
os.remove(cachename) |
| 25685 |
os.rename(tempname,cachename) |
| 25686 |
end |
| 25687 |
return cachename |
| 25688 |
end |
| 25689 |
install('http',http_handler) |
| 25690 |
install('https') |
| 25691 |
install('ftp') |
| 25692 |
statistics.register("scheme handling time",function() |
| 25693 |
local l,r,nl,nr={},{},0,0 |
| 25694 |
for k,v in sortedhash(loaded) do |
| 25695 |
if v>0 then |
| 25696 |
nl=nl+1 |
| 25697 |
l[nl]=k..":"..v |
| 25698 |
end |
| 25699 |
end |
| 25700 |
for k,v in sortedhash(reused) do |
| 25701 |
if v>0 then |
| 25702 |
nr=nr+1 |
| 25703 |
r[nr]=k..":"..v |
| 25704 |
end |
| 25705 |
end |
| 25706 |
local n=nl+nr |
| 25707 |
if n>0 then |
| 25708 |
if nl==0 then l={ "none" } end |
| 25709 |
if nr==0 then r={ "none" } end |
| 25710 |
return format("%s seconds, %s processed, threshold %s seconds, loaded: %s, reused: %s", |
| 25711 |
statistics.elapsedtime(schemes),n,threshold,concat(l," "),concat(l," ")) |
| 25712 |
else |
| 25713 |
return nil |
| 25714 |
end |
| 25715 |
end) |
| 25716 |
local httprequest=http.request |
| 25717 |
local toquery=url.toquery |
| 25718 |
local function fetchstring(url,data) |
| 25719 |
local q=data and toquery(data) |
| 25720 |
if q then |
| 25721 |
url=url.."?"..q |
| 25722 |
end |
| 25723 |
local reply=httprequest(url) |
| 25724 |
return reply |
| 25725 |
end |
| 25726 |
schemes.fetchstring=fetchstring |
| 25727 |
function schemes.fetchtable(url,data) |
| 25728 |
local reply=fetchstring(url,data) |
| 25729 |
if reply then |
| 25730 |
local s=load("return "..reply) |
| 25731 |
if s then |
| 25732 |
return s() |
| 25733 |
end |
| 25734 |
end |
| 25735 |
end |
| 25736 |
|
| 25737 |
|
| 25738 |
end -- of closure |
| 25739 |
|
| 25740 |
do -- create closure to overcome 200 locals limit |
| 25741 |
|
| 25742 |
package.loaded["data-lua"] = package.loaded["data-lua"] or true |
| 25743 |
|
| 25744 |
-- original size: 4227, stripped down to: 3049 |
| 25745 |
|
| 25746 |
if not modules then modules={} end modules ['data-lua']={ |
| 25747 |
version=1.001, |
| 25748 |
comment="companion to luat-lib.mkiv", |
| 25749 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 25750 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 25751 |
license="see context related readme files" |
| 25752 |
} |
| 25753 |
local package,lpeg=package,lpeg |
| 25754 |
local loadfile=loadfile |
| 25755 |
local addsuffix=file.addsuffix |
| 25756 |
local P,S,Cs,lpegmatch=lpeg.P,lpeg.S,lpeg.Cs,lpeg.match |
| 25757 |
local luasuffixes={ 'tex','lua' } |
| 25758 |
local libsuffixes={ 'lib' } |
| 25759 |
local luaformats={ 'TEXINPUTS','LUAINPUTS' } |
| 25760 |
local libformats={ 'CLUAINPUTS' } |
| 25761 |
local helpers=package.helpers or {} |
| 25762 |
local methods=helpers.methods or {} |
| 25763 |
local resolvers=resolvers |
| 25764 |
local resolveprefix=resolvers.resolve |
| 25765 |
local expandedpaths=resolvers.expandedpathlistfromvariable |
| 25766 |
local findfile=resolvers.findfile |
| 25767 |
helpers.report=logs.reporter("resolvers","libraries") |
| 25768 |
trackers.register("resolvers.libraries",function(v) helpers.trace=v end) |
| 25769 |
trackers.register("resolvers.locating",function(v) helpers.trace=v end) |
| 25770 |
helpers.sequence={ |
| 25771 |
"already loaded", |
| 25772 |
"preload table", |
| 25773 |
"lua variable format", |
| 25774 |
"lib variable format", |
| 25775 |
"lua extra list", |
| 25776 |
"lib extra list", |
| 25777 |
"path specification", |
| 25778 |
"cpath specification", |
| 25779 |
"all in one fallback", |
| 25780 |
"not loaded", |
| 25781 |
} |
| 25782 |
local pattern=Cs(P("!")^0/""*(P("/")*P(-1)/"/"+P("/")^1/"/"+1)^0) |
| 25783 |
function helpers.cleanpath(path) |
| 25784 |
return resolveprefix(lpegmatch(pattern,path)) |
| 25785 |
end |
| 25786 |
local loadedaslib=helpers.loadedaslib |
| 25787 |
local registerpath=helpers.registerpath |
| 25788 |
local lualibfile=helpers.lualibfile |
| 25789 |
local luaformatpaths |
| 25790 |
local libformatpaths |
| 25791 |
local function getluaformatpaths() |
| 25792 |
if not luaformatpaths then |
| 25793 |
luaformatpaths={} |
| 25794 |
for i=1,#luaformats do |
| 25795 |
registerpath("lua format","lua",luaformatpaths,expandedpaths(luaformats[i])) |
| 25796 |
end |
| 25797 |
end |
| 25798 |
return luaformatpaths |
| 25799 |
end |
| 25800 |
local function getlibformatpaths() |
| 25801 |
if not libformatpaths then |
| 25802 |
libformatpaths={} |
| 25803 |
for i=1,#libformats do |
| 25804 |
registerpath("lib format","lib",libformatpaths,expandedpaths(libformats[i])) |
| 25805 |
end |
| 25806 |
end |
| 25807 |
return libformatpaths |
| 25808 |
end |
| 25809 |
local function loadedbyformat(name,rawname,suffixes,islib,what) |
| 25810 |
local trace=helpers.trace |
| 25811 |
local report=helpers.report |
| 25812 |
for i=1,#suffixes do |
| 25813 |
local format=suffixes[i] |
| 25814 |
local resolved=findfile(name,format) or "" |
| 25815 |
if trace then |
| 25816 |
report("%s format, identifying %a using format %a",what,name,format) |
| 25817 |
end |
| 25818 |
if resolved~="" then |
| 25819 |
if trace then |
| 25820 |
report("%s format, %a found on %a",what,name,resolved) |
| 25821 |
end |
| 25822 |
if islib then |
| 25823 |
return loadedaslib(resolved,rawname) |
| 25824 |
else |
| 25825 |
return loadfile(resolved) |
| 25826 |
end |
| 25827 |
end |
| 25828 |
end |
| 25829 |
end |
| 25830 |
helpers.loadedbyformat=loadedbyformat |
| 25831 |
methods["lua variable format"]=function(name) |
| 25832 |
if helpers.trace then |
| 25833 |
helpers.report("%s format, checking %s paths","lua",#getluaformatpaths()) |
| 25834 |
end |
| 25835 |
return loadedbyformat(addsuffix(lualibfile(name),"lua"),name,luasuffixes,false,"lua") |
| 25836 |
end |
| 25837 |
methods["lib variable format"]=function(name) |
| 25838 |
if helpers.trace then |
| 25839 |
helpers.report("%s format, checking %s paths","lib",#getlibformatpaths()) |
| 25840 |
end |
| 25841 |
return loadedbyformat(addsuffix(lualibfile(name),os.libsuffix),name,libsuffixes,true,"lib") |
| 25842 |
end |
| 25843 |
resolvers.loadlualib=require |
| 25844 |
|
| 25845 |
|
| 25846 |
end -- of closure |
| 25847 |
|
| 25848 |
do -- create closure to overcome 200 locals limit |
| 25849 |
|
| 25850 |
package.loaded["data-aux"] = package.loaded["data-aux"] or true |
| 25851 |
|
| 25852 |
-- original size: 2610, stripped down to: 2019 |
| 25853 |
|
| 25854 |
if not modules then modules={} end modules ['data-aux']={ |
| 25855 |
version=1.001, |
| 25856 |
comment="companion to luat-lib.mkiv", |
| 25857 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 25858 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 25859 |
license="see context related readme files" |
| 25860 |
} |
| 25861 |
local find=string.find |
| 25862 |
local type,next=type,next |
| 25863 |
local addsuffix,removesuffix=file.addsuffix,file.removesuffix |
| 25864 |
local loaddata,savedata=io.loaddata,io.savedata |
| 25865 |
local trace_locating=false trackers.register("resolvers.locating",function(v) trace_locating=v end) |
| 25866 |
local resolvers=resolvers |
| 25867 |
local cleanpath=resolvers.cleanpath |
| 25868 |
local findfiles=resolvers.findfiles |
| 25869 |
local report_scripts=logs.reporter("resolvers","scripts") |
| 25870 |
function resolvers.updatescript(oldname,newname) |
| 25871 |
local scriptpath="context/lua" |
| 25872 |
local oldscript=cleanpath(oldname) |
| 25873 |
local newname=addsuffix(newname,"lua") |
| 25874 |
local newscripts=findfiles(newname) or {} |
| 25875 |
if trace_locating then |
| 25876 |
report_scripts("to be replaced old script %a",oldscript) |
| 25877 |
end |
| 25878 |
if #newscripts==0 then |
| 25879 |
if trace_locating then |
| 25880 |
report_scripts("unable to locate new script") |
| 25881 |
end |
| 25882 |
else |
| 25883 |
for i=1,#newscripts do |
| 25884 |
local newscript=cleanpath(newscripts[i]) |
| 25885 |
if trace_locating then |
| 25886 |
report_scripts("checking new script %a",newscript) |
| 25887 |
end |
| 25888 |
if oldscript==newscript then |
| 25889 |
if trace_locating then |
| 25890 |
report_scripts("old and new script are the same") |
| 25891 |
end |
| 25892 |
elseif not find(newscript,scriptpath,1,true) then |
| 25893 |
if trace_locating then |
| 25894 |
report_scripts("new script should come from %a",scriptpath) |
| 25895 |
end |
| 25896 |
elseif not (find(oldscript,removesuffix(newname).."$") or find(oldscript,newname.."$")) then |
| 25897 |
if trace_locating then |
| 25898 |
report_scripts("invalid new script name") |
| 25899 |
end |
| 25900 |
else |
| 25901 |
local newdata=loaddata(newscript) |
| 25902 |
if newdata then |
| 25903 |
if trace_locating then |
| 25904 |
report_scripts("old script content replaced by new content: %s",oldscript) |
| 25905 |
end |
| 25906 |
savedata(oldscript,newdata) |
| 25907 |
break |
| 25908 |
elseif trace_locating then |
| 25909 |
report_scripts("unable to load new script") |
| 25910 |
end |
| 25911 |
end |
| 25912 |
end |
| 25913 |
end |
| 25914 |
end |
| 25915 |
|
| 25916 |
|
| 25917 |
end -- of closure |
| 25918 |
|
| 25919 |
do -- create closure to overcome 200 locals limit |
| 25920 |
|
| 25921 |
package.loaded["data-tmf"] = package.loaded["data-tmf"] or true |
| 25922 |
|
| 25923 |
-- original size: 2601, stripped down to: 1549 |
| 25924 |
|
| 25925 |
if not modules then modules={} end modules ['data-tmf']={ |
| 25926 |
version=1.001, |
| 25927 |
comment="companion to luat-lib.mkiv", |
| 25928 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 25929 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 25930 |
license="see context related readme files" |
| 25931 |
} |
| 25932 |
local resolvers=resolvers |
| 25933 |
local report_tds=logs.reporter("resolvers","tds") |
| 25934 |
function resolvers.load_tree(tree,resolve) |
| 25935 |
if type(tree)=="string" and tree~="" then |
| 25936 |
local getenv,setenv=resolvers.getenv,resolvers.setenv |
| 25937 |
local texos="texmf-"..os.platform |
| 25938 |
local oldroot=environment.texroot |
| 25939 |
local newroot=file.collapsepath(tree) |
| 25940 |
local newtree=file.join(newroot,texos) |
| 25941 |
local newpath=file.join(newtree,"bin") |
| 25942 |
if not lfs.isdir(newtree) then |
| 25943 |
report_tds("no %a under tree %a",texos,tree) |
| 25944 |
os.exit() |
| 25945 |
end |
| 25946 |
if not lfs.isdir(newpath) then |
| 25947 |
report_tds("no '%s/bin' under tree %a",texos,tree) |
| 25948 |
os.exit() |
| 25949 |
end |
| 25950 |
local texmfos=newtree |
| 25951 |
environment.texroot=newroot |
| 25952 |
environment.texos=texos |
| 25953 |
environment.texmfos=texmfos |
| 25954 |
if resolve then |
| 25955 |
resolvers.luacnfspec=resolvers.resolve(resolvers.luacnfspec) |
| 25956 |
end |
| 25957 |
setenv('SELFAUTOPARENT',newroot) |
| 25958 |
setenv('SELFAUTODIR',newtree) |
| 25959 |
setenv('SELFAUTOLOC',newpath) |
| 25960 |
setenv('TEXROOT',newroot) |
| 25961 |
setenv('TEXOS',texos) |
| 25962 |
setenv('TEXMFOS',texmfos) |
| 25963 |
setenv('TEXMFCNF',resolvers.luacnfspec,true) |
| 25964 |
setenv('PATH',newpath..io.pathseparator..getenv('PATH')) |
| 25965 |
report_tds("changing from root %a to %a",oldroot,newroot) |
| 25966 |
report_tds("prepending %a to PATH",newpath) |
| 25967 |
report_tds("setting TEXMFCNF to %a",resolvers.luacnfspec) |
| 25968 |
report_tds() |
| 25969 |
end |
| 25970 |
end |
| 25971 |
|
| 25972 |
|
| 25973 |
end -- of closure |
| 25974 |
|
| 25975 |
do -- create closure to overcome 200 locals limit |
| 25976 |
|
| 25977 |
package.loaded["data-lst"] = package.loaded["data-lst"] or true |
| 25978 |
|
| 25979 |
-- original size: 2038, stripped down to: 1696 |
| 25980 |
|
| 25981 |
if not modules then modules={} end modules ['data-lst']={ |
| 25982 |
version=1.001, |
| 25983 |
comment="companion to luat-lib.mkiv", |
| 25984 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 25985 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 25986 |
license="see context related readme files" |
| 25987 |
} |
| 25988 |
local type=type |
| 25989 |
local sortedhash=table.sortedhash |
| 25990 |
local isdir=lfs.isdir |
| 25991 |
local resolvers=resolvers |
| 25992 |
local listers=resolvers.listers or {} |
| 25993 |
resolvers.listers=listers |
| 25994 |
local resolveprefix=resolvers.resolve |
| 25995 |
local configurationfiles=resolvers.configurationfiles |
| 25996 |
local expandedpathfromlist=resolvers.expandedpathfromlist |
| 25997 |
local splitpath=resolvers.splitpath |
| 25998 |
local knownvariables=resolvers.knownvariables |
| 25999 |
local report_lists=logs.reporter("resolvers","lists") |
| 26000 |
local report_resolved=logs.reporter("system","resolved") |
| 26001 |
local function tabstr(str) |
| 26002 |
if not str then |
| 26003 |
return "unset" |
| 26004 |
elseif type(str)=='table' then |
| 26005 |
return concat(str," | ") |
| 26006 |
else |
| 26007 |
return str |
| 26008 |
end |
| 26009 |
end |
| 26010 |
function listers.variables(pattern) |
| 26011 |
local result=resolvers.knownvariables(pattern) |
| 26012 |
for key,value in sortedhash(result) do |
| 26013 |
report_lists(key) |
| 26014 |
report_lists(" env: %s",tabstr(value.environment)) |
| 26015 |
report_lists(" var: %s",tabstr(value.variable)) |
| 26016 |
report_lists(" exp: %s",tabstr(value.expansion)) |
| 26017 |
report_lists(" res: %s",tabstr(value.resolved)) |
| 26018 |
end |
| 26019 |
end |
| 26020 |
function listers.configurations() |
| 26021 |
local configurations=configurationfiles() |
| 26022 |
for i=1,#configurations do |
| 26023 |
report_resolved("file : %s",resolveprefix(configurations[i])) |
| 26024 |
end |
| 26025 |
report_resolved("") |
| 26026 |
local list=expandedpathfromlist(splitpath(resolvers.luacnfspec)) |
| 26027 |
for i=1,#list do |
| 26028 |
local li=resolveprefix(list[i]) |
| 26029 |
if isdir(li) then |
| 26030 |
report_resolved("path - %s",li) |
| 26031 |
else |
| 26032 |
report_resolved("path + %s",li) |
| 26033 |
end |
| 26034 |
end |
| 26035 |
end |
| 26036 |
|
| 26037 |
|
| 26038 |
end -- of closure |
| 26039 |
|
| 26040 |
do -- create closure to overcome 200 locals limit |
| 26041 |
|
| 26042 |
package.loaded["libs-ini"] = package.loaded["libs-ini"] or true |
| 26043 |
|
| 26044 |
-- original size: 6524, stripped down to: 4064 |
| 26045 |
|
| 26046 |
if not modules then modules={} end modules ['libs-ini']={ |
| 26047 |
version=1.001, |
| 26048 |
comment="companion to luat-lib.mkiv", |
| 26049 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 26050 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 26051 |
license="see context related readme files" |
| 26052 |
} |
| 26053 |
local type,unpack=type,unpack |
| 26054 |
local find=string.find |
| 26055 |
local nameonly=file.nameonly |
| 26056 |
local joinfile=file.join |
| 26057 |
local addsuffix=file.addsuffix |
| 26058 |
local qualifiedpath=file.is_qualified_path |
| 26059 |
local isfile=lfs.isfile |
| 26060 |
local findfile=resolvers.findfile |
| 26061 |
local expandpaths=resolvers.expandedpathlistfromvariable |
| 26062 |
local report=logs.reporter("resolvers","libraries") |
| 26063 |
local trace=false |
| 26064 |
local silent=false |
| 26065 |
trackers.register("resolvers.lib",function(v) trace=v end) |
| 26066 |
trackers.register("resolvers.lib.silent",function(v) silent=v end) |
| 26067 |
local function findlib(required) |
| 26068 |
local suffix=os.libsuffix or "so" |
| 26069 |
if not qualifiedpath(required) then |
| 26070 |
local list=directives.value("system.librarynames" ) |
| 26071 |
local only=nameonly(required) |
| 26072 |
if type(list)=="table" then |
| 26073 |
list=list[only] |
| 26074 |
if type(list)~="table" then |
| 26075 |
list={ only } |
| 26076 |
end |
| 26077 |
else |
| 26078 |
list={ only } |
| 26079 |
end |
| 26080 |
if trace then |
| 26081 |
report("using lookup list for library %a: % | t",only,list) |
| 26082 |
end |
| 26083 |
for i=1,#list do |
| 26084 |
local name=list[i] |
| 26085 |
local found=findfile(name,"lib") |
| 26086 |
if not found or found=="" then |
| 26087 |
found=findfile(addsuffix(name,suffix),"lib") |
| 26088 |
end |
| 26089 |
if found and found~="" then |
| 26090 |
if trace then |
| 26091 |
report("library %a resolved via %a path to %a",name,"tds lib",found) |
| 26092 |
end |
| 26093 |
return found |
| 26094 |
end |
| 26095 |
end |
| 26096 |
if expandpaths then |
| 26097 |
local list=expandpaths("PATH") |
| 26098 |
local base=addsuffix(only,suffix) |
| 26099 |
for i=1,#list do |
| 26100 |
local full=joinfile(list[i],base) |
| 26101 |
local found=isfile(full) and full |
| 26102 |
if found and found~="" then |
| 26103 |
if trace then |
| 26104 |
report("library %a resolved via %a path to %a",full,"system",found) |
| 26105 |
end |
| 26106 |
return found |
| 26107 |
end |
| 26108 |
end |
| 26109 |
end |
| 26110 |
elseif isfile(addsuffix(required,suffix)) then |
| 26111 |
if trace then |
| 26112 |
report("library with qualified name %a %sfound",required,"") |
| 26113 |
end |
| 26114 |
return required |
| 26115 |
else |
| 26116 |
if trace then |
| 26117 |
report("library with qualified name %a %sfound",required,"not ") |
| 26118 |
end |
| 26119 |
end |
| 26120 |
return false |
| 26121 |
end |
| 26122 |
local foundlibraries=table.setmetatableindex(function(t,k) |
| 26123 |
local v=findlib(k) |
| 26124 |
t[k]=v |
| 26125 |
return v |
| 26126 |
end) |
| 26127 |
function resolvers.findlib(required) |
| 26128 |
return foundlibraries[required] |
| 26129 |
end |
| 26130 |
local libraries={} |
| 26131 |
resolvers.libraries=libraries |
| 26132 |
local report=logs.reporter("optional") |
| 26133 |
if optional then optional.loaded={} end |
| 26134 |
function libraries.validoptional(name) |
| 26135 |
local thelib=optional and optional[name] |
| 26136 |
if not thelib then |
| 26137 |
elseif thelib.initialize then |
| 26138 |
return thelib |
| 26139 |
else |
| 26140 |
report("invalid optional library %a",libname) |
| 26141 |
end |
| 26142 |
end |
| 26143 |
function libraries.optionalloaded(name,libnames) |
| 26144 |
local thelib=optional and optional[name] |
| 26145 |
if not thelib then |
| 26146 |
report("no optional %a library found",name) |
| 26147 |
else |
| 26148 |
local thelib_initialize=thelib.initialize |
| 26149 |
if not thelib_initialize then |
| 26150 |
report("invalid optional library %a",name) |
| 26151 |
else |
| 26152 |
if type(libnames)=="string" then |
| 26153 |
libnames={ libnames } |
| 26154 |
end |
| 26155 |
if type(libnames)=="table" then |
| 26156 |
for i=1,#libnames do |
| 26157 |
local libname=libnames[i] |
| 26158 |
local filename=foundlibraries[libname] |
| 26159 |
if filename and filename~="" then |
| 26160 |
libnames[i]=filename |
| 26161 |
else |
| 26162 |
report("unable to locate library %a",libname) |
| 26163 |
return |
| 26164 |
end |
| 26165 |
end |
| 26166 |
local initialized=thelib_initialize(unpack(libnames)) |
| 26167 |
if not initialized then |
| 26168 |
report("unable to initialize library '% + t'",libnames) |
| 26169 |
elseif not silent then |
| 26170 |
report("using library '% + t'",libnames) |
| 26171 |
end |
| 26172 |
return initialized |
| 26173 |
end |
| 26174 |
end |
| 26175 |
end |
| 26176 |
end |
| 26177 |
if FFISUPPORTED and ffi and ffi.load then |
| 26178 |
local ffiload=ffi.load |
| 26179 |
function ffi.load(name) |
| 26180 |
local full=name and foundlibraries[name] |
| 26181 |
if full then |
| 26182 |
return ffiload(full) |
| 26183 |
else |
| 26184 |
return ffiload(name) |
| 26185 |
end |
| 26186 |
end |
| 26187 |
end |
| 26188 |
local dofile=dofile |
| 26189 |
local savedrequire=require |
| 26190 |
function require(name,version) |
| 26191 |
if find(name,"%.lua$") or find(name,"%.lmt$") then |
| 26192 |
local m=dofile(findfile(name)) |
| 26193 |
if m then |
| 26194 |
package.loaded[name]=m |
| 26195 |
return m |
| 26196 |
end |
| 26197 |
else |
| 26198 |
return savedrequire(name) |
| 26199 |
end |
| 26200 |
end |
| 26201 |
|
| 26202 |
|
| 26203 |
end -- of closure |
| 26204 |
|
| 26205 |
do -- create closure to overcome 200 locals limit |
| 26206 |
|
| 26207 |
package.loaded["luat-sta"] = package.loaded["luat-sta"] or true |
| 26208 |
|
| 26209 |
-- original size: 5608, stripped down to: 2321 |
| 26210 |
|
| 26211 |
if not modules then modules={} end modules ['luat-sta']={ |
| 26212 |
version=1.001, |
| 26213 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 26214 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 26215 |
license="see context related readme files" |
| 26216 |
} |
| 26217 |
local gmatch,match=string.gmatch,string.match |
| 26218 |
local type=type |
| 26219 |
states=states or {} |
| 26220 |
local states=states |
| 26221 |
states.data=states.data or {} |
| 26222 |
local data=states.data |
| 26223 |
states.hash=states.hash or {} |
| 26224 |
local hash=states.hash |
| 26225 |
states.tag=states.tag or "" |
| 26226 |
states.filename=states.filename or "" |
| 26227 |
function states.save(filename,tag) |
| 26228 |
tag=tag or states.tag |
| 26229 |
filename=file.addsuffix(filename or states.filename,'lus') |
| 26230 |
io.savedata(filename, |
| 26231 |
"-- generator : luat-sta.lua\n".."-- state tag : "..tag.."\n\n"..table.serialize(data[tag or states.tag] or {},true) |
| 26232 |
) |
| 26233 |
end |
| 26234 |
function states.load(filename,tag) |
| 26235 |
states.filename=filename |
| 26236 |
states.tag=tag or "whatever" |
| 26237 |
states.filename=file.addsuffix(states.filename,'lus') |
| 26238 |
data[states.tag],hash[states.tag]=(io.exists(filename) and dofile(filename)) or {},{} |
| 26239 |
end |
| 26240 |
local function set_by_tag(tag,key,value,default,persistent) |
| 26241 |
local d,h=data[tag],hash[tag] |
| 26242 |
if d then |
| 26243 |
if type(d)=="table" then |
| 26244 |
local dkey,hkey=key,key |
| 26245 |
local pre,post=match(key,"(.+)%.([^%.]+)$") |
| 26246 |
if pre and post then |
| 26247 |
for k in gmatch(pre,"[^%.]+") do |
| 26248 |
local dk=d[k] |
| 26249 |
if not dk then |
| 26250 |
dk={} |
| 26251 |
d[k]=dk |
| 26252 |
elseif type(dk)=="string" then |
| 26253 |
break |
| 26254 |
end |
| 26255 |
d=dk |
| 26256 |
end |
| 26257 |
dkey,hkey=post,key |
| 26258 |
end |
| 26259 |
if value==nil then |
| 26260 |
value=default |
| 26261 |
elseif value==false then |
| 26262 |
elseif persistent then |
| 26263 |
value=value or d[dkey] or default |
| 26264 |
else |
| 26265 |
value=value or default |
| 26266 |
end |
| 26267 |
d[dkey],h[hkey]=value,value |
| 26268 |
elseif type(d)=="string" then |
| 26269 |
data[tag],hash[tag]=value,value |
| 26270 |
end |
| 26271 |
end |
| 26272 |
end |
| 26273 |
local function get_by_tag(tag,key,default) |
| 26274 |
local h=hash[tag] |
| 26275 |
if h and h[key] then |
| 26276 |
return h[key] |
| 26277 |
else |
| 26278 |
local d=data[tag] |
| 26279 |
if d then |
| 26280 |
for k in gmatch(key,"[^%.]+") do |
| 26281 |
local dk=d[k] |
| 26282 |
if dk~=nil then |
| 26283 |
d=dk |
| 26284 |
else |
| 26285 |
return default |
| 26286 |
end |
| 26287 |
end |
| 26288 |
if d==false then |
| 26289 |
return false |
| 26290 |
else |
| 26291 |
return d or default |
| 26292 |
end |
| 26293 |
end |
| 26294 |
end |
| 26295 |
end |
| 26296 |
states.set_by_tag=set_by_tag |
| 26297 |
states.get_by_tag=get_by_tag |
| 26298 |
function states.set(key,value,default,persistent) |
| 26299 |
set_by_tag(states.tag,key,value,default,persistent) |
| 26300 |
end |
| 26301 |
function states.get(key,default) |
| 26302 |
return get_by_tag(states.tag,key,default) |
| 26303 |
end |
| 26304 |
|
| 26305 |
|
| 26306 |
end -- of closure |
| 26307 |
|
| 26308 |
do -- create closure to overcome 200 locals limit |
| 26309 |
|
| 26310 |
package.loaded["luat-fmt"] = package.loaded["luat-fmt"] or true |
| 26311 |
|
| 26312 |
-- original size: 12056, stripped down to: 8398 |
| 26313 |
|
| 26314 |
if not modules then modules={} end modules ['luat-fmt']={ |
| 26315 |
version=1.001, |
| 26316 |
comment="companion to mtxrun", |
| 26317 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 26318 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 26319 |
license="see context related readme files" |
| 26320 |
} |
| 26321 |
local format=string.format |
| 26322 |
local concat=table.concat |
| 26323 |
local quoted=string.quoted |
| 26324 |
local luasuffixes=utilities.lua.suffixes |
| 26325 |
local report_format=logs.reporter("resolvers","formats") |
| 26326 |
local function primaryflags(arguments) |
| 26327 |
local flags={} |
| 26328 |
if arguments.silent then |
| 26329 |
flags[#flags+1]="--interaction=batchmode" |
| 26330 |
end |
| 26331 |
return concat(flags," ") |
| 26332 |
end |
| 26333 |
local function secondaryflags(arguments) |
| 26334 |
local trackers=arguments.trackers |
| 26335 |
local directives=arguments.directives |
| 26336 |
local flags={} |
| 26337 |
if trackers and trackers~="" then |
| 26338 |
flags[#flags+1]="--c:trackers="..quoted(trackers) |
| 26339 |
end |
| 26340 |
if directives and directives~="" then |
| 26341 |
flags[#flags+1]="--c:directives="..quoted(directives) |
| 26342 |
end |
| 26343 |
if arguments.silent then |
| 26344 |
flags[#flags+1]="--c:silent" |
| 26345 |
end |
| 26346 |
if arguments.errors then |
| 26347 |
flags[#flags+1]="--c:errors" |
| 26348 |
end |
| 26349 |
if arguments.ansi then |
| 26350 |
flags[#flags+1]="--c:ansi" |
| 26351 |
end |
| 26352 |
if arguments.ansilog then |
| 26353 |
flags[#flags+1]="--c:ansilog" |
| 26354 |
end |
| 26355 |
if arguments.strip then |
| 26356 |
flags[#flags+1]="--c:strip" |
| 26357 |
end |
| 26358 |
if arguments.lmtx then |
| 26359 |
flags[#flags+1]="--c:lmtx" |
| 26360 |
end |
| 26361 |
return concat(flags," ") |
| 26362 |
end |
| 26363 |
local template=[[--ini %primaryflags% --socket --shell-escape --lua=%luafile% %texfile% %secondaryflags% %redirect%]] |
| 26364 |
local checkers={ |
| 26365 |
primaryflags="verbose", |
| 26366 |
secondaryflags="verbose", |
| 26367 |
luafile="readable", |
| 26368 |
texfile="readable", |
| 26369 |
redirect="string", |
| 26370 |
binarypath="string", |
| 26371 |
} |
| 26372 |
local runners={ |
| 26373 |
luametatex=sandbox.registerrunner { |
| 26374 |
name="make luametatex format", |
| 26375 |
program="luametatex", |
| 26376 |
template=template, |
| 26377 |
checkers=checkers, |
| 26378 |
reporter=report_format, |
| 26379 |
}, |
| 26380 |
luatex=sandbox.registerrunner { |
| 26381 |
name="make luatex format", |
| 26382 |
program="luatex", |
| 26383 |
template=template, |
| 26384 |
checkers=checkers, |
| 26385 |
reporter=report_format, |
| 26386 |
}, |
| 26387 |
luajittex=sandbox.registerrunner { |
| 26388 |
name="make luajittex format", |
| 26389 |
program="luajittex", |
| 26390 |
template=template, |
| 26391 |
checkers=checkers, |
| 26392 |
reporter=report_format, |
| 26393 |
}, |
| 26394 |
} |
| 26395 |
local stubfiles={ |
| 26396 |
luametatex="luat-cod.lmt", |
| 26397 |
luatex="luat-cod.lua", |
| 26398 |
luajittex="luat-cod.lua", |
| 26399 |
} |
| 26400 |
local suffixes={ |
| 26401 |
luametatex="mkxl", |
| 26402 |
luatex="mkiv", |
| 26403 |
luajittex="mkiv", |
| 26404 |
} |
| 26405 |
local function validbinarypath() |
| 26406 |
if not environment.arguments.nobinarypath then |
| 26407 |
local path=environment.ownpath or file.dirname(environment.ownname) |
| 26408 |
if path and path~="" then |
| 26409 |
path=dir.expandname(path) |
| 26410 |
if path~="" and lfs.isdir(path) then |
| 26411 |
return path |
| 26412 |
end |
| 26413 |
end |
| 26414 |
end |
| 26415 |
end |
| 26416 |
local function fatalerror(startupdir,...) |
| 26417 |
report_format(...) |
| 26418 |
lfs.chdir(startupdir) |
| 26419 |
end |
| 26420 |
function environment.make_format(formatname) |
| 26421 |
local arguments=environment.arguments |
| 26422 |
local engine=environment.ownmain or "luatex" |
| 26423 |
local silent=arguments.silent |
| 26424 |
local errors=arguments.errors |
| 26425 |
local runner=runners[engine] |
| 26426 |
local startupdir=dir.current() |
| 26427 |
if not runner then |
| 26428 |
return fatalerror(startupdir,"the format %a cannot be generated, no runner available for engine %a",name,engine) |
| 26429 |
end |
| 26430 |
local luasourcename=stubfiles[engine] |
| 26431 |
if not luasourcename then |
| 26432 |
return fatalerror(startupdir,"no lua stub file specified for %a",engine) |
| 26433 |
end |
| 26434 |
local texsourcename=file.addsuffix(formatname,suffixes[engine]) |
| 26435 |
local fulltexsourcename=resolvers.findfile(texsourcename,"tex") or "" |
| 26436 |
if fulltexsourcename=="" then |
| 26437 |
return fatalerror(startupdir,"no tex source file with name %a (mkiv or tex)",formatname) |
| 26438 |
end |
| 26439 |
local fulltexsourcename=dir.expandname(fulltexsourcename) |
| 26440 |
local texsourcepath=file.dirname(fulltexsourcename) |
| 26441 |
if lfs.isfile(fulltexsourcename) then |
| 26442 |
report_format("using tex source file %a",fulltexsourcename) |
| 26443 |
else |
| 26444 |
return fatalerror(startupdir,"no accessible tex source file with name %a",fulltexsourcename) |
| 26445 |
end |
| 26446 |
local fullluasourcename=dir.expandname(file.join(texsourcepath,luasourcename) or "") |
| 26447 |
if lfs.isfile(fullluasourcename) then |
| 26448 |
report_format("using lua stub file %a",fullluasourcename) |
| 26449 |
else |
| 26450 |
return fatalerror(startupdir,"no accessible lua stub file with name %a",fulltexsourcename) |
| 26451 |
end |
| 26452 |
local validformatpath=caches.getwritablepath("formats",engine) or "" |
| 26453 |
if validformatpath=="" then |
| 26454 |
return fatalerror(startupdir,"invalid format path, insufficient write access") |
| 26455 |
end |
| 26456 |
local binarypath=validbinarypath() |
| 26457 |
report_format("changing to format path %a",validformatpath) |
| 26458 |
if not lfs.chdir(validformatpath) then |
| 26459 |
return fatalerror(startupdir,"unable to change to format path %a",validformatpath) |
| 26460 |
end |
| 26461 |
local primaryflags=primaryflags(arguments) |
| 26462 |
local secondaryflags=secondaryflags(arguments) |
| 26463 |
local specification={ |
| 26464 |
binarypath=binarypath, |
| 26465 |
primaryflags=primaryflags, |
| 26466 |
secondaryflags=secondaryflags, |
| 26467 |
luafile=quoted(fullluasourcename), |
| 26468 |
texfile=quoted(fulltexsourcename), |
| 26469 |
} |
| 26470 |
if silent then |
| 26471 |
specification.redirect="> temp.log" |
| 26472 |
end |
| 26473 |
statistics.starttiming("format") |
| 26474 |
local result=runner(specification) |
| 26475 |
statistics.stoptiming("format") |
| 26476 |
if silent then |
| 26477 |
os.remove("temp.log") |
| 26478 |
end |
| 26479 |
report_format() |
| 26480 |
if binarypath and binarypath~="" then |
| 26481 |
report_format("binary path : %s",binarypath or "?") |
| 26482 |
end |
| 26483 |
report_format("format path : %s",validformatpath) |
| 26484 |
report_format("luatex engine : %s",engine) |
| 26485 |
report_format("lua startup file : %s",fullluasourcename) |
| 26486 |
if primaryflags~="" then |
| 26487 |
report_format("primary flags : %s",primaryflags) |
| 26488 |
end |
| 26489 |
if secondaryflags~="" then |
| 26490 |
report_format("secondary flags : %s",secondaryflags) |
| 26491 |
end |
| 26492 |
report_format("context file : %s",fulltexsourcename) |
| 26493 |
report_format("run time : %.3f seconds",statistics.elapsed("format")) |
| 26494 |
report_format("return value : %s",result==0 and "okay" or "error") |
| 26495 |
report_format() |
| 26496 |
lfs.chdir(startupdir) |
| 26497 |
end |
| 26498 |
local template=[[%primaryflags% --socket --shell-escape --fmt=%fmtfile% --lua=%luafile% %texfile% %secondaryflags%]] |
| 26499 |
local checkers={ |
| 26500 |
primaryflags="verbose", |
| 26501 |
secondaryflags="verbose", |
| 26502 |
fmtfile="readable", |
| 26503 |
luafile="readable", |
| 26504 |
texfile="readable", |
| 26505 |
} |
| 26506 |
local runners={ |
| 26507 |
luatex=sandbox.registerrunner { |
| 26508 |
name="run luatex format", |
| 26509 |
program="luatex", |
| 26510 |
template=template, |
| 26511 |
checkers=checkers, |
| 26512 |
reporter=report_format, |
| 26513 |
}, |
| 26514 |
luametatex=sandbox.registerrunner { |
| 26515 |
name="run luametatex format", |
| 26516 |
program="luametatex", |
| 26517 |
template=template, |
| 26518 |
checkers=checkers, |
| 26519 |
reporter=report_format, |
| 26520 |
}, |
| 26521 |
luajittex=sandbox.registerrunner { |
| 26522 |
name="run luajittex format", |
| 26523 |
program="luajittex", |
| 26524 |
template=template, |
| 26525 |
checkers=checkers, |
| 26526 |
reporter=report_format, |
| 26527 |
}, |
| 26528 |
} |
| 26529 |
function environment.run_format(formatname,scriptname,filename,primaryflags,secondaryflags,verbose) |
| 26530 |
local engine=environment.ownmain or "luatex" |
| 26531 |
if not formatname or formatname=="" then |
| 26532 |
report_format("missing format name") |
| 26533 |
return |
| 26534 |
end |
| 26535 |
if not scriptname or scriptname=="" then |
| 26536 |
report_format("missing script name") |
| 26537 |
return |
| 26538 |
end |
| 26539 |
if not lfs.isfile(formatname) or not lfs.isfile(scriptname) then |
| 26540 |
formatname,scriptname=resolvers.locateformat(formatname) |
| 26541 |
end |
| 26542 |
if not formatname or formatname=="" then |
| 26543 |
report_format("invalid format name") |
| 26544 |
return |
| 26545 |
end |
| 26546 |
if not scriptname or scriptname=="" then |
| 26547 |
report_format("invalid script name") |
| 26548 |
return |
| 26549 |
end |
| 26550 |
local runner=runners[engine] |
| 26551 |
if not runner then |
| 26552 |
report_format("format %a cannot be run, no runner available for engine %a",file.nameonly(name),engine) |
| 26553 |
return |
| 26554 |
end |
| 26555 |
if not filename then |
| 26556 |
filename "" |
| 26557 |
end |
| 26558 |
local binarypath=validbinarypath() |
| 26559 |
local specification={ |
| 26560 |
binarypath=binarypath, |
| 26561 |
primaryflags=primaryflags or "", |
| 26562 |
secondaryflags=secondaryflags or "", |
| 26563 |
fmtfile=quoted(formatname), |
| 26564 |
luafile=quoted(scriptname), |
| 26565 |
texfile=filename~="" and quoted(filename) or "", |
| 26566 |
} |
| 26567 |
statistics.starttiming("make format") |
| 26568 |
local result=runner(specification) |
| 26569 |
statistics.stoptiming("make format") |
| 26570 |
if verbose then |
| 26571 |
report_format() |
| 26572 |
if binarypath and binarypath~="" then |
| 26573 |
report_format("binary path : %s",binarypath) |
| 26574 |
end |
| 26575 |
report_format("luatex engine : %s",engine) |
| 26576 |
report_format("lua startup file : %s",scriptname) |
| 26577 |
report_format("tex format file : %s",formatname) |
| 26578 |
if filename~="" then |
| 26579 |
report_format("tex input file : %s",filename) |
| 26580 |
end |
| 26581 |
if primaryflags~="" then |
| 26582 |
report_format("primary flags : %s",primaryflags) |
| 26583 |
end |
| 26584 |
if secondaryflags~="" then |
| 26585 |
report_format("secondary flags : %s",secondaryflags) |
| 26586 |
end |
| 26587 |
report_format("run time : %0.3f seconds",statistics.elapsed("make format")) |
| 26588 |
report_format("return value : %s",result==0 and "okay" or "error") |
| 26589 |
report_format() |
| 26590 |
end |
| 26591 |
return result |
| 26592 |
end |
| 26593 |
|
| 26594 |
|
| 26595 |
end -- of closure |
| 26596 |
|
| 26597 |
do -- create closure to overcome 200 locals limit |
| 26598 |
|
| 26599 |
package.loaded["util-jsn"] = package.loaded["util-jsn"] or true |
| 26600 |
|
| 26601 |
-- original size: 16718, stripped down to: 8910 |
| 26602 |
|
| 26603 |
if not modules then modules={} end modules ['util-jsn']={ |
| 26604 |
version=1.001, |
| 26605 |
comment="companion to m-json.mkiv", |
| 26606 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 26607 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 26608 |
license="see context related readme files" |
| 26609 |
} |
| 26610 |
if utilities and utilities.json then |
| 26611 |
return json |
| 26612 |
end |
| 26613 |
local P,V,R,S,C,Cc,Cs,Ct,Cf,Cg=lpeg.P,lpeg.V,lpeg.R,lpeg.S,lpeg.C,lpeg.Cc,lpeg.Cs,lpeg.Ct,lpeg.Cf,lpeg.Cg |
| 26614 |
local lpegmatch=lpeg.match |
| 26615 |
local format,gsub=string.format,string.gsub |
| 26616 |
local formatters=string.formatters |
| 26617 |
local utfchar=utf.char |
| 26618 |
local concat,sortedkeys=table.concat,table.sortedkeys |
| 26619 |
local tonumber,tostring,rawset,type,next=tonumber,tostring,rawset,type,next |
| 26620 |
local json=utilities.json or {} |
| 26621 |
utilities.json=json |
| 26622 |
do |
| 26623 |
local lbrace=P("{") |
| 26624 |
local rbrace=P("}") |
| 26625 |
local lparent=P("[") |
| 26626 |
local rparent=P("]") |
| 26627 |
local comma=P(",") |
| 26628 |
local colon=P(":") |
| 26629 |
local dquote=P('"') |
| 26630 |
local whitespace=lpeg.patterns.whitespace |
| 26631 |
local optionalws=whitespace^0 |
| 26632 |
local escapes={ |
| 26633 |
["b"]="\010", |
| 26634 |
["f"]="\014", |
| 26635 |
["n"]="\n", |
| 26636 |
["r"]="\r", |
| 26637 |
["t"]="\t", |
| 26638 |
} |
| 26639 |
local escape_un=P("\\u")/""*(C(R("09","AF","af")^-4)/function(s) |
| 26640 |
return utfchar(tonumber(s,16)) |
| 26641 |
end) |
| 26642 |
local escape_bs=P([[\]])/""*(P(1)/escapes) |
| 26643 |
local jstring=dquote*Cs((escape_un+escape_bs+(1-dquote))^0)*dquote |
| 26644 |
local jtrue=P("true")*Cc(true) |
| 26645 |
local jfalse=P("false")*Cc(false) |
| 26646 |
local jnull=P("null")*Cc(nil) |
| 26647 |
local jnumber=(1-whitespace-rparent-rbrace-comma)^1/tonumber |
| 26648 |
local key=jstring |
| 26649 |
local jsonconverter={ "value", |
| 26650 |
hash=lbrace*Cf(Ct("")*(V("pair")*(comma*V("pair"))^0+optionalws),rawset)*rbrace, |
| 26651 |
pair=Cg(optionalws*key*optionalws*colon*V("value")), |
| 26652 |
array=Ct(lparent*(V("value")*(comma*V("value"))^0+optionalws)*rparent), |
| 26653 |
value=optionalws*(jstring+V("hash")+V("array")+jtrue+jfalse+jnull+jnumber)*optionalws, |
| 26654 |
} |
| 26655 |
function json.tolua(str) |
| 26656 |
return lpegmatch(jsonconverter,str) |
| 26657 |
end |
| 26658 |
function json.load(filename) |
| 26659 |
local data=io.loaddata(filename) |
| 26660 |
if data then |
| 26661 |
return lpegmatch(jsonconverter,data) |
| 26662 |
end |
| 26663 |
end |
| 26664 |
end |
| 26665 |
do |
| 26666 |
local escaper |
| 26667 |
local f_start_hash=formatters[ '%w{' ] |
| 26668 |
local f_start_array=formatters[ '%w[' ] |
| 26669 |
local f_start_hash_new=formatters[ "\n"..'%w{' ] |
| 26670 |
local f_start_array_new=formatters[ "\n"..'%w[' ] |
| 26671 |
local f_start_hash_key=formatters[ "\n"..'%w"%s" : {' ] |
| 26672 |
local f_start_array_key=formatters[ "\n"..'%w"%s" : [' ] |
| 26673 |
local f_stop_hash=formatters[ "\n"..'%w}' ] |
| 26674 |
local f_stop_array=formatters[ "\n"..'%w]' ] |
| 26675 |
local f_key_val_seq=formatters[ "\n"..'%w"%s" : %s' ] |
| 26676 |
local f_key_val_str=formatters[ "\n"..'%w"%s" : "%s"' ] |
| 26677 |
local f_key_val_num=f_key_val_seq |
| 26678 |
local f_key_val_yes=formatters[ "\n"..'%w"%s" : true' ] |
| 26679 |
local f_key_val_nop=formatters[ "\n"..'%w"%s" : false' ] |
| 26680 |
local f_key_val_null=formatters[ "\n"..'%w"%s" : null' ] |
| 26681 |
local f_val_num=formatters[ "\n"..'%w%s' ] |
| 26682 |
local f_val_str=formatters[ "\n"..'%w"%s"' ] |
| 26683 |
local f_val_yes=formatters[ "\n"..'%wtrue' ] |
| 26684 |
local f_val_nop=formatters[ "\n"..'%wfalse' ] |
| 26685 |
local f_val_null=formatters[ "\n"..'%wnull' ] |
| 26686 |
local f_val_empty=formatters[ "\n"..'%w{ }' ] |
| 26687 |
local f_val_seq=f_val_num |
| 26688 |
local t={} |
| 26689 |
local n=0 |
| 26690 |
local function is_simple_table(tt) |
| 26691 |
local l=#tt |
| 26692 |
if l>0 then |
| 26693 |
for i=1,l do |
| 26694 |
if type(tt[i])=="table" then |
| 26695 |
return false |
| 26696 |
end |
| 26697 |
end |
| 26698 |
local nn=n |
| 26699 |
n=n+1 t[n]="[ " |
| 26700 |
for i=1,l do |
| 26701 |
if i>1 then |
| 26702 |
n=n+1 t[n]=", " |
| 26703 |
end |
| 26704 |
local v=tt[i] |
| 26705 |
local tv=type(v) |
| 26706 |
if tv=="number" then |
| 26707 |
n=n+1 t[n]=v |
| 26708 |
elseif tv=="string" then |
| 26709 |
n=n+1 t[n]='"' |
| 26710 |
n=n+1 t[n]=lpegmatch(escaper,v) or v |
| 26711 |
n=n+1 t[n]='"' |
| 26712 |
elseif tv=="boolean" then |
| 26713 |
n=n+1 t[n]=v and "true" or "false" |
| 26714 |
elseif v then |
| 26715 |
n=n+1 t[n]=tostring(v) |
| 26716 |
else |
| 26717 |
n=n+1 t[n]="null" |
| 26718 |
end |
| 26719 |
end |
| 26720 |
n=n+1 t[n]=" ]" |
| 26721 |
local s=concat(t,"",nn+1,n) |
| 26722 |
n=nn |
| 26723 |
return s |
| 26724 |
end |
| 26725 |
return false |
| 26726 |
end |
| 26727 |
local function tojsonpp(root,name,depth,level,size) |
| 26728 |
if root then |
| 26729 |
local indexed=size>0 |
| 26730 |
n=n+1 |
| 26731 |
if level==0 then |
| 26732 |
if indexed then |
| 26733 |
t[n]=f_start_array(depth) |
| 26734 |
else |
| 26735 |
t[n]=f_start_hash(depth) |
| 26736 |
end |
| 26737 |
elseif name then |
| 26738 |
if tn=="string" then |
| 26739 |
name=lpegmatch(escaper,name) or name |
| 26740 |
elseif tn~="number" then |
| 26741 |
name=tostring(name) |
| 26742 |
end |
| 26743 |
if indexed then |
| 26744 |
t[n]=f_start_array_key(depth,name) |
| 26745 |
else |
| 26746 |
t[n]=f_start_hash_key(depth,name) |
| 26747 |
end |
| 26748 |
else |
| 26749 |
if indexed then |
| 26750 |
t[n]=f_start_array_new(depth) |
| 26751 |
else |
| 26752 |
t[n]=f_start_hash_new(depth) |
| 26753 |
end |
| 26754 |
end |
| 26755 |
depth=depth+1 |
| 26756 |
if indexed then |
| 26757 |
for i=1,size do |
| 26758 |
if i>1 then |
| 26759 |
n=n+1 t[n]="," |
| 26760 |
end |
| 26761 |
local v=root[i] |
| 26762 |
local tv=type(v) |
| 26763 |
if tv=="number" then |
| 26764 |
n=n+1 t[n]=f_val_num(depth,v) |
| 26765 |
elseif tv=="string" then |
| 26766 |
v=lpegmatch(escaper,v) or v |
| 26767 |
n=n+1 t[n]=f_val_str(depth,v) |
| 26768 |
elseif tv=="table" then |
| 26769 |
if next(v) then |
| 26770 |
local st=is_simple_table(v) |
| 26771 |
if st then |
| 26772 |
n=n+1 t[n]=f_val_seq(depth,st) |
| 26773 |
else |
| 26774 |
tojsonpp(v,nil,depth,level+1,#v) |
| 26775 |
end |
| 26776 |
else |
| 26777 |
n=n+1 |
| 26778 |
t[n]=f_val_empty(depth) |
| 26779 |
end |
| 26780 |
elseif tv=="boolean" then |
| 26781 |
n=n+1 |
| 26782 |
if v then |
| 26783 |
t[n]=f_val_yes(depth,v) |
| 26784 |
else |
| 26785 |
t[n]=f_val_nop(depth,v) |
| 26786 |
end |
| 26787 |
else |
| 26788 |
n=n+1 |
| 26789 |
t[n]=f_val_null(depth) |
| 26790 |
end |
| 26791 |
end |
| 26792 |
elseif next(root) then |
| 26793 |
local sk=sortedkeys(root) |
| 26794 |
for i=1,#sk do |
| 26795 |
if i>1 then |
| 26796 |
n=n+1 t[n]="," |
| 26797 |
end |
| 26798 |
local k=sk[i] |
| 26799 |
local v=root[k] |
| 26800 |
local tv=type(v) |
| 26801 |
local tk=type(k) |
| 26802 |
if tv=="number" then |
| 26803 |
if tk=="number" then |
| 26804 |
n=n+1 t[n]=f_key_val_num(depth,k,v) |
| 26805 |
elseif tk=="string" then |
| 26806 |
k=lpegmatch(escaper,k) or k |
| 26807 |
n=n+1 t[n]=f_key_val_num(depth,k,v) |
| 26808 |
end |
| 26809 |
elseif tv=="string" then |
| 26810 |
if tk=="number" then |
| 26811 |
v=lpegmatch(escaper,v) or v |
| 26812 |
n=n+1 t[n]=f_key_val_str(depth,k,v) |
| 26813 |
elseif tk=="string" then |
| 26814 |
k=lpegmatch(escaper,k) or k |
| 26815 |
v=lpegmatch(escaper,v) or v |
| 26816 |
n=n+1 t[n]=f_key_val_str(depth,k,v) |
| 26817 |
elseif i>1 then |
| 26818 |
n=n-1 |
| 26819 |
end |
| 26820 |
elseif tv=="table" then |
| 26821 |
local l=#v |
| 26822 |
if l>0 then |
| 26823 |
local st=is_simple_table(v) |
| 26824 |
if not st then |
| 26825 |
tojsonpp(v,k,depth,level+1,l) |
| 26826 |
elseif tk=="number" then |
| 26827 |
n=n+1 t[n]=f_key_val_seq(depth,k,st) |
| 26828 |
elseif tk=="string" then |
| 26829 |
k=lpegmatch(escaper,k) or k |
| 26830 |
n=n+1 t[n]=f_key_val_seq(depth,k,st) |
| 26831 |
end |
| 26832 |
elseif next(v) then |
| 26833 |
tojsonpp(v,k,depth,level+1,0) |
| 26834 |
elseif i>1 then |
| 26835 |
n=n-1 |
| 26836 |
end |
| 26837 |
elseif tv=="boolean" then |
| 26838 |
if tk=="number" then |
| 26839 |
n=n+1 |
| 26840 |
if v then |
| 26841 |
t[n]=f_key_val_yes(depth,k) |
| 26842 |
else |
| 26843 |
t[n]=f_key_val_nop(depth,k) |
| 26844 |
end |
| 26845 |
elseif tk=="string" then |
| 26846 |
k=lpegmatch(escaper,k) or k |
| 26847 |
n=n+1 |
| 26848 |
if v then |
| 26849 |
t[n]=f_key_val_yes(depth,k) |
| 26850 |
else |
| 26851 |
t[n]=f_key_val_nop(depth,k) |
| 26852 |
end |
| 26853 |
elseif i>1 then |
| 26854 |
n=n-1 |
| 26855 |
end |
| 26856 |
else |
| 26857 |
if tk=="number" then |
| 26858 |
n=n+1 |
| 26859 |
t[n]=f_key_val_null(depth,k) |
| 26860 |
elseif tk=="string" then |
| 26861 |
k=lpegmatch(escaper,k) or k |
| 26862 |
n=n+1 |
| 26863 |
t[n]=f_key_val_null(depth,k) |
| 26864 |
elseif i>1 then |
| 26865 |
n=n-1 |
| 26866 |
end |
| 26867 |
end |
| 26868 |
end |
| 26869 |
end |
| 26870 |
n=n+1 |
| 26871 |
if indexed then |
| 26872 |
t[n]=f_stop_array(depth-1) |
| 26873 |
else |
| 26874 |
t[n]=f_stop_hash(depth-1) |
| 26875 |
end |
| 26876 |
end |
| 26877 |
end |
| 26878 |
local function tojson(value,n) |
| 26879 |
local kind=type(value) |
| 26880 |
if kind=="table" then |
| 26881 |
local done=false |
| 26882 |
local size=#value |
| 26883 |
if size==0 then |
| 26884 |
for k,v in next,value do |
| 26885 |
if done then |
| 26886 |
n=n+1;t[n]=',"' |
| 26887 |
else |
| 26888 |
n=n+1;t[n]='{"' |
| 26889 |
done=true |
| 26890 |
end |
| 26891 |
n=n+1;t[n]=lpegmatch(escaper,k) or k |
| 26892 |
n=n+1;t[n]='":' |
| 26893 |
t,n=tojson(v,n) |
| 26894 |
end |
| 26895 |
if done then |
| 26896 |
n=n+1;t[n]="}" |
| 26897 |
else |
| 26898 |
n=n+1;t[n]="{}" |
| 26899 |
end |
| 26900 |
elseif size==1 then |
| 26901 |
n=n+1;t[n]="[" |
| 26902 |
t,n=tojson(value[1],n) |
| 26903 |
n=n+1;t[n]="]" |
| 26904 |
else |
| 26905 |
for i=1,size do |
| 26906 |
if done then |
| 26907 |
n=n+1;t[n]="," |
| 26908 |
else |
| 26909 |
n=n+1;t[n]="[" |
| 26910 |
done=true |
| 26911 |
end |
| 26912 |
t,n=tojson(value[i],n) |
| 26913 |
end |
| 26914 |
n=n+1;t[n]="]" |
| 26915 |
end |
| 26916 |
elseif kind=="string" then |
| 26917 |
n=n+1;t[n]='"' |
| 26918 |
n=n+1;t[n]=lpegmatch(escaper,value) or value |
| 26919 |
n=n+1;t[n]='"' |
| 26920 |
elseif kind=="number" then |
| 26921 |
n=n+1;t[n]=value |
| 26922 |
elseif kind=="boolean" then |
| 26923 |
n=n+1;t[n]=tostring(value) |
| 26924 |
else |
| 26925 |
n=n+1;t[n]="null" |
| 26926 |
end |
| 26927 |
return t,n |
| 26928 |
end |
| 26929 |
local function jsontostring(value,pretty) |
| 26930 |
local kind=type(value) |
| 26931 |
if kind=="table" then |
| 26932 |
if not escaper then |
| 26933 |
local escapes={ |
| 26934 |
["\\"]="\\u005C", |
| 26935 |
["\""]="\\u0022", |
| 26936 |
} |
| 26937 |
for i=0,0x1F do |
| 26938 |
escapes[utfchar(i)]=format("\\u%04X",i) |
| 26939 |
end |
| 26940 |
escaper=Cs(( |
| 26941 |
(R('\0\x20')+S('\"\\'))/escapes+P(1) |
| 26942 |
)^1 ) |
| 26943 |
end |
| 26944 |
t={} |
| 26945 |
n=0 |
| 26946 |
if pretty then |
| 26947 |
tojsonpp(value,name,0,0,#value) |
| 26948 |
value=concat(t,"",1,n) |
| 26949 |
else |
| 26950 |
t,n=tojson(value,0) |
| 26951 |
value=concat(t,"",1,n) |
| 26952 |
end |
| 26953 |
t=nil |
| 26954 |
n=0 |
| 26955 |
return value |
| 26956 |
elseif kind=="string" or kind=="number" then |
| 26957 |
return lpegmatch(escaper,value) or value |
| 26958 |
else |
| 26959 |
return tostring(value) |
| 26960 |
end |
| 26961 |
end |
| 26962 |
json.tostring=jsontostring |
| 26963 |
function json.tojson(value) |
| 26964 |
return jsontostring(value,true) |
| 26965 |
end |
| 26966 |
end |
| 26967 |
|
| 26968 |
|
| 26969 |
end -- of closure |
| 26970 |
|
| 26971 |
-- used libraries : l-bit32.lua l-lua.lua l-macro.lua l-sandbox.lua l-package.lua l-lpeg.lua l-function.lua l-string.lua l-table.lua l-io.lua l-number.lua l-set.lua l-os.lua l-file.lua l-gzip.lua l-md5.lua l-sha.lua l-url.lua l-dir.lua l-boolean.lua l-unicode.lua l-math.lua util-str.lua util-tab.lua util-fil.lua util-sac.lua util-sto.lua util-prs.lua util-fmt.lua util-soc-imp-reset.lua util-soc-imp-socket.lua util-soc-imp-copas.lua util-soc-imp-ltn12.lua util-soc-imp-mime.lua util-soc-imp-url.lua util-soc-imp-headers.lua util-soc-imp-tp.lua util-soc-imp-http.lua util-soc-imp-ftp.lua util-soc-imp-smtp.lua trac-set.lua trac-log.lua trac-inf.lua trac-pro.lua util-lua.lua util-deb.lua util-tpl.lua util-sbx.lua util-mrg.lua util-env.lua luat-env.lua util-zip.lua util-sig.lua lxml-tab.lua lxml-lpt.lua lxml-mis.lua lxml-aux.lua lxml-xml.lua trac-xml.lua data-ini.lua data-exp.lua data-env.lua data-tmp.lua data-met.lua data-res.lua data-pre.lua data-inp.lua data-out.lua data-fil.lua data-con.lua data-use.lua data-zip.lua data-tre.lua data-sch.lua data-lua.lua data-aux.lua data-tmf.lua data-lst.lua libs-ini.lua luat-sta.lua luat-fmt.lua util-jsn.lua |
| 26972 |
-- skipped libraries : - |
| 26973 |
-- original bytes : 1078878 |
| 26974 |
-- stripped bytes : 430424 |
| 26975 |
|
| 26976 |
-- end library merge |
| 26977 |
|
| 26978 |
-- We need this hack till luatex is fixed. |
| 26979 |
-- |
| 26980 |
-- for k,v in pairs(arg) do print(k,v) end |
| 26981 |
|
| 26982 |
if arg and (arg[0] == 'luatex' or arg[0] == 'luatex.exe') and arg[1] == "--luaonly" then |
| 26983 |
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 |
| 26984 |
end |
| 26985 |
|
| 26986 |
-- End of hack. |
| 26987 |
|
| 26988 |
local format, gsub, gmatch, match, find = string.format, string.gsub, string.gmatch, string.match, string.find |
| 26989 |
local concat = table.concat |
| 26990 |
|
| 26991 |
local ownname = environment and environment.ownname or arg[0] or 'mtxrun.lua' |
| 26992 |
local ownpath = gsub(match(ownname,"^(.+)[\\/].-$") or ".","\\","/") |
| 26993 |
local owntree = environment and environment.ownpath or ownpath |
| 26994 |
|
| 26995 |
local ownlibs = { -- order can be made better |
| 26996 |
|
| 26997 |
'l-bit32.lua', |
| 26998 |
'l-lua.lua', |
| 26999 |
'l-macro.lua', |
| 27000 |
'l-sandbox.lua', |
| 27001 |
'l-package.lua', |
| 27002 |
'l-lpeg.lua', |
| 27003 |
'l-function.lua', |
| 27004 |
'l-string.lua', |
| 27005 |
'l-table.lua', |
| 27006 |
'l-io.lua', |
| 27007 |
'l-number.lua', |
| 27008 |
'l-set.lua', |
| 27009 |
'l-os.lua', |
| 27010 |
'l-file.lua', |
| 27011 |
'l-gzip.lua', |
| 27012 |
'l-md5.lua', |
| 27013 |
'l-sha.lua', |
| 27014 |
'l-url.lua', |
| 27015 |
'l-dir.lua', |
| 27016 |
'l-boolean.lua', |
| 27017 |
'l-unicode.lua', |
| 27018 |
'l-math.lua', |
| 27019 |
|
| 27020 |
'util-str.lua', -- code might move to l-string |
| 27021 |
'util-tab.lua', |
| 27022 |
'util-fil.lua', |
| 27023 |
'util-sac.lua', |
| 27024 |
'util-sto.lua', |
| 27025 |
'util-prs.lua', |
| 27026 |
'util-fmt.lua', |
| 27027 |
|
| 27028 |
'util-soc-imp-reset.lua', |
| 27029 |
'util-soc-imp-socket.lua', |
| 27030 |
'util-soc-imp-copas.lua', |
| 27031 |
'util-soc-imp-ltn12.lua', |
| 27032 |
-- 'util-soc-imp-mbox.lua', |
| 27033 |
'util-soc-imp-mime.lua', |
| 27034 |
'util-soc-imp-url.lua', |
| 27035 |
'util-soc-imp-headers.lua', |
| 27036 |
'util-soc-imp-tp.lua', |
| 27037 |
'util-soc-imp-http.lua', |
| 27038 |
'util-soc-imp-ftp.lua', |
| 27039 |
'util-soc-imp-smtp.lua', |
| 27040 |
|
| 27041 |
'trac-set.lua', |
| 27042 |
'trac-log.lua', |
| 27043 |
'trac-inf.lua', -- was before trac-set |
| 27044 |
'trac-pro.lua', -- not really needed |
| 27045 |
'util-lua.lua', -- indeed here? |
| 27046 |
'util-deb.lua', |
| 27047 |
|
| 27048 |
'util-tpl.lua', |
| 27049 |
'util-sbx.lua', |
| 27050 |
'util-mrg.lua', |
| 27051 |
|
| 27052 |
'util-env.lua', |
| 27053 |
'luat-env.lua', -- can come before inf (as in mkiv) |
| 27054 |
|
| 27055 |
'util-zip.lua', |
| 27056 |
|
| 27057 |
'util-sig.lua', |
| 27058 |
|
| 27059 |
'lxml-tab.lua', |
| 27060 |
'lxml-lpt.lua', |
| 27061 |
-- 'lxml-ent.lua', |
| 27062 |
'lxml-mis.lua', |
| 27063 |
'lxml-aux.lua', |
| 27064 |
'lxml-xml.lua', |
| 27065 |
|
| 27066 |
'trac-xml.lua', |
| 27067 |
|
| 27068 |
'data-ini.lua', |
| 27069 |
'data-exp.lua', |
| 27070 |
'data-env.lua', |
| 27071 |
'data-tmp.lua', |
| 27072 |
'data-met.lua', |
| 27073 |
'data-res.lua', |
| 27074 |
'data-pre.lua', |
| 27075 |
'data-inp.lua', |
| 27076 |
'data-out.lua', |
| 27077 |
'data-fil.lua', |
| 27078 |
'data-con.lua', |
| 27079 |
'data-use.lua', |
| 27080 |
-- 'data-tex.lua', |
| 27081 |
-- 'data-bin.lua', |
| 27082 |
'data-zip.lua', |
| 27083 |
'data-tre.lua', |
| 27084 |
'data-sch.lua', |
| 27085 |
'data-lua.lua', |
| 27086 |
'data-aux.lua', -- updater |
| 27087 |
'data-tmf.lua', |
| 27088 |
'data-lst.lua', |
| 27089 |
|
| 27090 |
'libs-ini.lua', |
| 27091 |
|
| 27092 |
'luat-sta.lua', |
| 27093 |
'luat-fmt.lua', |
| 27094 |
|
| 27095 |
'util-jsn.lua', |
| 27096 |
} |
| 27097 |
|
| 27098 |
-- c:/data/develop/tex-context/tex/texmf-win64/bin/../../texmf-context/tex/context/base/mkiv/data-tmf.lua |
| 27099 |
-- c:/data/develop/context/sources/data-tmf.lua |
| 27100 |
|
| 27101 |
local ownlist = { |
| 27102 |
-- '.', |
| 27103 |
-- ownpath , |
| 27104 |
owntree .. "/../../../../context/sources", -- HH's development path |
| 27105 |
-- |
| 27106 |
owntree .. "/../../texmf-local/tex/context/base/mkiv", |
| 27107 |
owntree .. "/../../texmf-context/tex/context/base/mkiv", |
| 27108 |
owntree .. "/../../texmf/tex/context/base/mkiv", |
| 27109 |
owntree .. "/../../../texmf-local/tex/context/base/mkiv", |
| 27110 |
owntree .. "/../../../texmf-context/tex/context/base/mkiv", |
| 27111 |
owntree .. "/../../../texmf/tex/context/base/mkiv", |
| 27112 |
-- |
| 27113 |
owntree .. "/../../texmf-local/tex/context/base", |
| 27114 |
owntree .. "/../../texmf-context/tex/context/base", |
| 27115 |
owntree .. "/../../texmf/tex/context/base", |
| 27116 |
owntree .. "/../../../texmf-local/tex/context/base", |
| 27117 |
owntree .. "/../../../texmf-context/tex/context/base", |
| 27118 |
owntree .. "/../../../texmf/tex/context/base", |
| 27119 |
} |
| 27120 |
|
| 27121 |
if ownpath == "." then table.remove(ownlist,1) end |
| 27122 |
|
| 27123 |
own = { |
| 27124 |
name = ownname, |
| 27125 |
path = ownpath, |
| 27126 |
tree = owntree, |
| 27127 |
list = ownlist, |
| 27128 |
libs = ownlibs, |
| 27129 |
} |
| 27130 |
|
| 27131 |
local function locate_libs() |
| 27132 |
for l=1,#ownlibs do |
| 27133 |
local lib = ownlibs[l] |
| 27134 |
for p =1,#ownlist do |
| 27135 |
local pth = ownlist[p] |
| 27136 |
local filename = pth .. "/" .. lib |
| 27137 |
local found = lfs.isfile(filename) |
| 27138 |
if found then |
| 27139 |
package.path = package.path .. ";" .. pth .. "/?.lua" -- in case l-* does a require |
| 27140 |
return pth |
| 27141 |
end |
| 27142 |
end |
| 27143 |
end |
| 27144 |
end |
| 27145 |
|
| 27146 |
local function load_libs() |
| 27147 |
local found = locate_libs() |
| 27148 |
if found then |
| 27149 |
for l=1,#ownlibs do |
| 27150 |
local filename = found .. "/" .. ownlibs[l] |
| 27151 |
local codeblob = loadfile(filename) |
| 27152 |
if codeblob then |
| 27153 |
codeblob() |
| 27154 |
end |
| 27155 |
end |
| 27156 |
else |
| 27157 |
resolvers = nil |
| 27158 |
end |
| 27159 |
end |
| 27160 |
|
| 27161 |
if not resolvers then |
| 27162 |
load_libs() |
| 27163 |
end |
| 27164 |
|
| 27165 |
if not resolvers then |
| 27166 |
print("") |
| 27167 |
print("Mtxrun is unable to start up due to lack of libraries. You may") |
| 27168 |
print("try to run 'lua mtxrun.lua --selfmerge' in the path where this") |
| 27169 |
print("script is located (normally under ..../scripts/context/lua) which") |
| 27170 |
print("will make this script library independent.") |
| 27171 |
os.exit() |
| 27172 |
end |
| 27173 |
|
| 27174 |
-- verbosity |
| 27175 |
|
| 27176 |
----- e_verbose = environment.arguments["verbose"] |
| 27177 |
|
| 27178 |
local e_verbose = false |
| 27179 |
|
| 27180 |
-- some common flags (also passed through environment) |
| 27181 |
|
| 27182 |
local e_silent = environment.argument("silent") |
| 27183 |
local e_errors = environment.argument("errors") |
| 27184 |
local e_noconsole = environment.argument("noconsole") |
| 27185 |
|
| 27186 |
local e_trackers = environment.argument("trackers") |
| 27187 |
local e_directives = environment.argument("directives") |
| 27188 |
local e_experiments = environment.argument("experiments") |
| 27189 |
|
| 27190 |
local t = { } |
| 27191 |
|
| 27192 |
if type(e_directives) == "string" then |
| 27193 |
t[#t+1] = e_directives |
| 27194 |
end |
| 27195 |
|
| 27196 |
if type(e_silent) == "string" then |
| 27197 |
t[#t+1] = format("logs.blocked={%s}",e_silent) |
| 27198 |
elseif e_silent == true then |
| 27199 |
t[#t+1] = "logs.blocked" |
| 27200 |
end |
| 27201 |
|
| 27202 |
if type(e_errors) == "string" then |
| 27203 |
t[#t+1] = format("logs.errors={%s}",e_errors) |
| 27204 |
elseif e_errors == true then |
| 27205 |
t[#t+1] = "logs.errors" |
| 27206 |
end |
| 27207 |
|
| 27208 |
if e_noconsole then |
| 27209 |
t[#t+1] = format("logs.target=file") |
| 27210 |
end |
| 27211 |
|
| 27212 |
if #t > 0 then |
| 27213 |
e_directives = concat(t,",") |
| 27214 |
end |
| 27215 |
|
| 27216 |
if e_trackers then trackers .enable(e_trackers) end |
| 27217 |
if e_directives then directives .enable(e_directives) end |
| 27218 |
if e_experiments then experiments.enable(e_experiments) end |
| 27219 |
|
| 27220 |
if not environment.trackers then environment.trackers = e_trackers end |
| 27221 |
if not environment.directives then environment.directives = e_directives end |
| 27222 |
if not environment.experiments then environment.experiments = e_experiments end |
| 27223 |
|
| 27224 |
-- I need to check all the options. |
| 27225 |
|
| 27226 |
resolvers.reset() |
| 27227 |
|
| 27228 |
local helpinfo = [[ |
| 27229 |
<?xml version="1.0" ?> |
| 27230 |
<application> |
| 27231 |
<metadata> |
| 27232 |
<entry name="name">mtxrun</entry> |
| 27233 |
<entry name="detail">ConTeXt TDS Runner Tool</entry> |
| 27234 |
<entry name="version">1.33</entry> |
| 27235 |
</metadata> |
| 27236 |
<flags> |
| 27237 |
<category name="basic"> |
| 27238 |
<subcategory> |
| 27239 |
<flag name="script"><short>run an mtx script (lua prefered method) (<ref name="noquotes"/>), no script gives list</short></flag> |
| 27240 |
<flag name="evaluate"><short>run code passed on the commandline (between quotes) (=loop) (exit|quit aborts)</short></flag> |
| 27241 |
<flag name="execute"><short>run a script or program (texmfstart method) (<ref name="noquotes"/>)</short></flag> |
| 27242 |
<flag name="direct"><short>run a program </short></flag> |
| 27243 |
<flag name="resolve"><short>resolve prefixed arguments</short></flag> |
| 27244 |
<flag name="ctxlua"><short>run internally (using preloaded libs)</short></flag> |
| 27245 |
<flag name="internal"><short>run script using built in libraries (same as <ref name="ctxlua"/>)</short></flag> |
| 27246 |
<flag name="locate"><short>locate given filename in database (default) or system (<ref name="first"/> <ref name="all"/> <ref name="detail"/>)</short></flag> |
| 27247 |
</subcategory> |
| 27248 |
<subcategory> |
| 27249 |
<flag name="tree" value="pathtotree"><short>use given texmf tree (default file: setuptex.tmf)</short></flag> |
| 27250 |
<flag name="path" value="runpath"><short>go to given path before execution</short></flag> |
| 27251 |
<flag name="ifchanged" value="filename"><short>only execute when given file has changed (md checksum)</short></flag> |
| 27252 |
<flag name="iftouched" value="old,new"><short>only execute when given file has changed (time stamp)</short></flag> |
| 27253 |
</subcategory> |
| 27254 |
<subcategory> |
| 27255 |
<flag name="makestubs"><short>create stubs for (context related) scripts</short></flag> |
| 27256 |
<flag name="removestubs"><short>remove stubs (context related) scripts</short></flag> |
| 27257 |
<flag name="stubpath" value="binpath"><short>paths where stubs wil be written</short></flag> |
| 27258 |
<flag name="windows"><short>create windows (mswin) stubs</short></flag> |
| 27259 |
<flag name="unix"><short>create unix (linux) stubs</short></flag> |
| 27260 |
<flag name="addbinarypath"><short>prepend the (found) binarypath to runners</short></flag> |
| 27261 |
</subcategory> |
| 27262 |
<subcategory> |
| 27263 |
<flag name="verbose"><short>give a bit more info</short></flag> |
| 27264 |
<flag name="trackers" value="list"><short>enable given trackers</short></flag> |
| 27265 |
<flag name="progname" value="str"><short>format or backend</short></flag> |
| 27266 |
<flag name="platform" value="str"><short>show platform as seen by script</short></flag> |
| 27267 |
<flag name="systeminfo" value="str"><short>show current operating system, processor, etc</short></flag> |
| 27268 |
</subcategory> |
| 27269 |
<subcategory> |
| 27270 |
<flag name="edit"><short>launch editor with found file</short></flag> |
| 27271 |
<flag name="launch"><short>launch files like manuals, assumes os support (<ref name="all"/>,<ref name="list"/>)</short></flag> |
| 27272 |
<flag name="associate"><short>launch files like manuals, assumes os support via suffix association</short></flag> |
| 27273 |
</subcategory> |
| 27274 |
<subcategory> |
| 27275 |
<flag name="timedrun"><short>run a script and time its run</short></flag> |
| 27276 |
<flag name="autogenerate"><short>regenerate databases if needed (handy when used to run context in an editor)</short></flag> |
| 27277 |
</subcategory> |
| 27278 |
<subcategory> |
| 27279 |
<flag name="usekpse"><short>use kpse as fallback (when no mkiv and cache installed, often slower)</short></flag> |
| 27280 |
<flag name="forcekpse"><short>force using kpse (handy when no mkiv and cache installed but less functionality)</short></flag> |
| 27281 |
</subcategory> |
| 27282 |
<subcategory> |
| 27283 |
<flag name="prefixes"><short>show supported prefixes</short></flag> |
| 27284 |
</subcategory> |
| 27285 |
<subcategory> |
| 27286 |
<flag name="generate"><short>generate file database</short></flag> |
| 27287 |
</subcategory> |
| 27288 |
<subcategory> |
| 27289 |
<flag name="variables"><short>show configuration variables</short></flag> |
| 27290 |
<flag name="configurations"><short>show configuration order</short></flag> |
| 27291 |
</subcategory> |
| 27292 |
<subcategory> |
| 27293 |
<flag name="directives"><short>show (known) directives</short></flag> |
| 27294 |
<flag name="trackers"><short>show (known) trackers</short></flag> |
| 27295 |
<flag name="experiments"><short>show (known) experiments</short></flag> |
| 27296 |
</subcategory> |
| 27297 |
<subcategory> |
| 27298 |
<flag name="expand-braces"><short>expand complex variable</short></flag> |
| 27299 |
<flag name="resolve-path"><short>expand variable (completely resolve paths)</short></flag> |
| 27300 |
<flag name="expand-path"><short>expand variable (resolve paths)</short></flag> |
| 27301 |
<flag name="expand-var"><short>expand variable (resolve references)</short></flag> |
| 27302 |
<flag name="show-path"><short>show path expansion of ...</short></flag> |
| 27303 |
<flag name="var-value"><short>report value of variable</short></flag> |
| 27304 |
<flag name="find-file"><short>report file location</short></flag> |
| 27305 |
<flag name="find-path"><short>report path of file</short></flag> |
| 27306 |
<flag name="format-path"><short>report path where format files end up</short></flag> |
| 27307 |
</subcategory> |
| 27308 |
<subcategory> |
| 27309 |
<flag name="pattern" value="string"><short>filter variables</short></flag> |
| 27310 |
<flag name="all" value="string"><short>report all (when applicable)</short></flag> |
| 27311 |
<flag name="format" value="string"><short>filter by file format (when applicable)</short></flag> |
| 27312 |
</subcategory> |
| 27313 |
<subcategory> |
| 27314 |
<flag name="selfclean" value="string"><short>remove libraries (maintainance)</short></flag> |
| 27315 |
<flag name="selfmerge" value="string"><short>embed libraries (maintainance)</short></flag> |
| 27316 |
<flag name="selfupdate" value="string"><short>move to bin path (maintainance)</short></flag> |
| 27317 |
<subcategory> |
| 27318 |
</subcategory> |
| 27319 |
<flag name="help" value="string"><short>show help info</short></flag> |
| 27320 |
<flag name="help" value="string"><short>show version info</short></flag> |
| 27321 |
<flag name="gethelp" value="string"><short>launch the given help info (<ref name="url"/>)</short></flag> |
| 27322 |
</subcategory> |
| 27323 |
</category> |
| 27324 |
</flags> |
| 27325 |
</application> |
| 27326 |
]] |
| 27327 |
|
| 27328 |
local application = logs.application { |
| 27329 |
name = "mtxrun", |
| 27330 |
banner = "ConTeXt TDS Runner Tool 1.33", |
| 27331 |
helpinfo = helpinfo, |
| 27332 |
} |
| 27333 |
|
| 27334 |
local report = application.report |
| 27335 |
|
| 27336 |
messages = messages or { } -- for the moment |
| 27337 |
|
| 27338 |
runners = runners or { } -- global (might become local) |
| 27339 |
|
| 27340 |
runners.applications = { |
| 27341 |
["lua"] = "luatex --luaonly --socket", |
| 27342 |
["luc"] = "luatex --luaonly --socket", |
| 27343 |
["pl"] = "perl", |
| 27344 |
["py"] = "python", |
| 27345 |
["rb"] = "ruby", |
| 27346 |
} |
| 27347 |
|
| 27348 |
runners.suffixes = { |
| 27349 |
'rb', 'lua', 'py', 'pl' |
| 27350 |
} |
| 27351 |
|
| 27352 |
runners.registered = { |
| 27353 |
texexec = { 'texexec.rb', false }, -- context mkii runner (only tool not to be luafied) |
| 27354 |
texutil = { 'texutil.rb', true }, -- old perl based index sorter for mkii (old versions need it) |
| 27355 |
texfont = { 'texfont.pl', true }, -- perl script that makes mkii font metric files |
| 27356 |
texfind = { 'texfind.pl', false }, -- perltk based tex searching tool, mostly used at pragma |
| 27357 |
texshow = { 'texshow.pl', false }, -- perltk based context help system, will be luafied |
| 27358 |
-- texwork = { 'texwork.pl', false }, -- perltk based editing environment, only used at pragma |
| 27359 |
makempy = { 'makempy.pl', true }, |
| 27360 |
mptopdf = { 'mptopdf.pl', true }, |
| 27361 |
pstopdf = { 'pstopdf.rb', true }, -- converts ps (and some more) images, does some cleaning (replaced) |
| 27362 |
-- examplex = { 'examplex.rb', false }, |
| 27363 |
concheck = { 'concheck.rb', false }, |
| 27364 |
runtools = { 'runtools.rb', true }, |
| 27365 |
textools = { 'textools.rb', true }, |
| 27366 |
tmftools = { 'tmftools.rb', true }, |
| 27367 |
ctxtools = { 'ctxtools.rb', true }, |
| 27368 |
rlxtools = { 'rlxtools.rb', true }, |
| 27369 |
pdftools = { 'pdftools.rb', true }, |
| 27370 |
mpstools = { 'mpstools.rb', true }, |
| 27371 |
-- exatools = { 'exatools.rb', true }, |
| 27372 |
xmltools = { 'xmltools.rb', true }, |
| 27373 |
-- luatools = { 'luatools.lua', true }, |
| 27374 |
mtxtools = { 'mtxtools.rb', true }, |
| 27375 |
pdftrimwhite = { 'pdftrimwhite.pl', false }, |
| 27376 |
} |
| 27377 |
|
| 27378 |
runners.launchers = { |
| 27379 |
windows = { }, |
| 27380 |
unix = { }, |
| 27381 |
} |
| 27382 |
|
| 27383 |
-- like runners.libpath("framework"): looks on script's subpath |
| 27384 |
|
| 27385 |
function runners.libpath(...) |
| 27386 |
package.prepend_libpath(file.dirname(environment.ownscript),...) |
| 27387 |
package.prepend_libpath(file.dirname(environment.ownname) ,...) |
| 27388 |
end |
| 27389 |
|
| 27390 |
function runners.prepare() |
| 27391 |
local checkname = environment.argument("ifchanged") |
| 27392 |
if type(checkname) == "string" and checkname ~= "" then |
| 27393 |
local oldchecksum = file.loadchecksum(checkname) |
| 27394 |
local newchecksum = file.checksum(checkname) |
| 27395 |
if oldchecksum == newchecksum then |
| 27396 |
if e_verbose then |
| 27397 |
report("file '%s' is unchanged",checkname) |
| 27398 |
end |
| 27399 |
return "skip" |
| 27400 |
elseif e_verbose then |
| 27401 |
report("file '%s' is changed, processing started",checkname) |
| 27402 |
end |
| 27403 |
file.savechecksum(checkname) |
| 27404 |
end |
| 27405 |
local touchname = environment.argument("iftouched") |
| 27406 |
if type(touchname) == "string" and touchname ~= "" then |
| 27407 |
local oldname, newname = string.splitup(touchname, ",") |
| 27408 |
if oldname and newname and oldname ~= "" and newname ~= "" then |
| 27409 |
if not file.needs_updating(oldname,newname) then |
| 27410 |
if e_verbose then |
| 27411 |
report("file '%s' and '%s' have same age",oldname,newname) |
| 27412 |
end |
| 27413 |
return "skip" |
| 27414 |
elseif e_verbose then |
| 27415 |
report("file '%s' is older than '%s'",oldname,newname) |
| 27416 |
end |
| 27417 |
end |
| 27418 |
end |
| 27419 |
local runpath = environment.argument("path") |
| 27420 |
if type(runpath) == "string" and not lfs.chdir(runpath) then |
| 27421 |
report("unable to change to path '%s'",runpath) |
| 27422 |
return "error" |
| 27423 |
end |
| 27424 |
runners.prepare = function() end |
| 27425 |
return "run" |
| 27426 |
end |
| 27427 |
|
| 27428 |
function runners.execute_script(fullname,internal,nosplit) |
| 27429 |
local noquote = environment.argument("noquotes") |
| 27430 |
if fullname and fullname ~= "" then |
| 27431 |
local state = runners.prepare() |
| 27432 |
if state == 'error' then |
| 27433 |
return false |
| 27434 |
elseif state == 'skip' then |
| 27435 |
return true |
| 27436 |
elseif state == "run" then |
| 27437 |
local path, name, suffix = file.splitname(fullname) |
| 27438 |
local result = "" |
| 27439 |
if path ~= "" then |
| 27440 |
result = fullname |
| 27441 |
elseif name then |
| 27442 |
name = gsub(name,"^int[%a]*:",function() |
| 27443 |
internal = true |
| 27444 |
return "" |
| 27445 |
end ) |
| 27446 |
name = gsub(name,"^script:","") |
| 27447 |
if suffix == "" and runners.registered[name] and runners.registered[name][1] then |
| 27448 |
name = runners.registered[name][1] |
| 27449 |
suffix = file.suffix(name) |
| 27450 |
end |
| 27451 |
if suffix == "" then |
| 27452 |
-- loop over known suffixes |
| 27453 |
for _,s in pairs(runners.suffixes) do |
| 27454 |
result = resolvers.findfile(name .. "." .. s, 'texmfscripts') |
| 27455 |
if result ~= "" then |
| 27456 |
break |
| 27457 |
end |
| 27458 |
end |
| 27459 |
elseif runners.applications[suffix] then |
| 27460 |
result = resolvers.findfile(name, 'texmfscripts') |
| 27461 |
else |
| 27462 |
-- maybe look on path |
| 27463 |
result = resolvers.findfile(name, 'other text files') |
| 27464 |
end |
| 27465 |
end |
| 27466 |
if result and result ~= "" then |
| 27467 |
if not no_split then |
| 27468 |
local before, after = environment.splitarguments(fullname) -- already done |
| 27469 |
environment.arguments_before, environment.arguments_after = before, after |
| 27470 |
end |
| 27471 |
if internal then |
| 27472 |
arg = { } for _,v in pairs(environment.arguments_after) do arg[#arg+1] = v end |
| 27473 |
environment.ownscript = result |
| 27474 |
dofile(result) |
| 27475 |
else |
| 27476 |
local texmfcnf = resolvers.getenv("TEXMFCNF") |
| 27477 |
if not texmfcnf or texmfcnf == "" then |
| 27478 |
texmfcnf = resolvers.expandedpathfromlist(resolvers.splitpath(resolvers.resolve(resolvers.luacnfspec))) |
| 27479 |
resolvers.setenv("TEXMFCNF",table.concat(texmfcnf,";")) -- for running texexec etc (after tl change to texmf-dist) |
| 27480 |
end |
| 27481 |
local binary = runners.applications[file.suffix(result)] |
| 27482 |
result = string.quoted(string.unquoted(result)) |
| 27483 |
-- if string.match(result,' ') and not string.match(result,"^\".*\"$") then |
| 27484 |
-- result = '"' .. result .. '"' |
| 27485 |
-- end |
| 27486 |
if binary and binary ~= "" then |
| 27487 |
result = binary .. " " .. result |
| 27488 |
end |
| 27489 |
local command = result .. " " .. environment.reconstructcommandline(environment.arguments_after,noquote) |
| 27490 |
if e_verbose then |
| 27491 |
report() |
| 27492 |
report("executing: %s",command) |
| 27493 |
report() |
| 27494 |
report() |
| 27495 |
io.flush() |
| 27496 |
end |
| 27497 |
local code = os.execute(command) |
| 27498 |
if code == 0 then |
| 27499 |
return true |
| 27500 |
else |
| 27501 |
if binary then |
| 27502 |
binary = file.addsuffix(binary,os.binsuffix) |
| 27503 |
for p in gmatch(os.getenv("PATH"),"[^"..io.pathseparator.."]+") do |
| 27504 |
if lfs.isfile(file.join(p,binary)) then |
| 27505 |
return false |
| 27506 |
end |
| 27507 |
end |
| 27508 |
report() |
| 27509 |
report("This script needs '%s' which seems not to be installed.",binary) |
| 27510 |
report() |
| 27511 |
end |
| 27512 |
return false |
| 27513 |
end |
| 27514 |
end |
| 27515 |
end |
| 27516 |
end |
| 27517 |
end |
| 27518 |
return false |
| 27519 |
end |
| 27520 |
|
| 27521 |
function runners.execute_program(fullname) |
| 27522 |
local noquote = environment.argument("noquotes") |
| 27523 |
if fullname and fullname ~= "" then |
| 27524 |
local state = runners.prepare() |
| 27525 |
if state == 'error' then |
| 27526 |
return false |
| 27527 |
elseif state == 'skip' then |
| 27528 |
return true |
| 27529 |
elseif state == "run" then |
| 27530 |
local before, after = environment.splitarguments(fullname) |
| 27531 |
for k=1,#after do after[k] = resolvers.resolve(after[k]) end |
| 27532 |
environment.initializearguments(after) |
| 27533 |
fullname = gsub(fullname,"^bin:","") |
| 27534 |
local command = fullname .. " " .. (environment.reconstructcommandline(after or "",noquote) or "") |
| 27535 |
report() |
| 27536 |
report("executing: %s",command) |
| 27537 |
report() |
| 27538 |
report() |
| 27539 |
io.flush() |
| 27540 |
local code = os.execute(command) |
| 27541 |
return code == 0 |
| 27542 |
end |
| 27543 |
end |
| 27544 |
return false |
| 27545 |
end |
| 27546 |
|
| 27547 |
-- the --usekpse flag will fallback (not default) on kpse (hm, we can better update mtx-stubs) |
| 27548 |
|
| 27549 |
local windows_stub = '@echo off\013\010setlocal\013\010set ownpath=%%~dp0%%\013\010texlua "%%ownpath%%mtxrun.lua" --usekpse --execute %s %%*\013\010endlocal\013\010' |
| 27550 |
local unix_stub = '#!/bin/sh\010mtxrun --usekpse --execute %s \"$@\"\010' |
| 27551 |
|
| 27552 |
function runners.handle_stubs(create) |
| 27553 |
local stubpath = environment.argument('stubpath') or '.' -- 'auto' no longer subpathssupported |
| 27554 |
local windows = environment.argument('windows') or environment.argument('mswin') or false |
| 27555 |
local unix = environment.argument('unix') or environment.argument('linux') or false |
| 27556 |
if not windows and not unix then |
| 27557 |
if os.platform == "unix" then |
| 27558 |
unix = true |
| 27559 |
else |
| 27560 |
windows = true |
| 27561 |
end |
| 27562 |
end |
| 27563 |
for _,v in pairs(runners.registered) do |
| 27564 |
local name, doit = v[1], v[2] |
| 27565 |
if doit then |
| 27566 |
local base = gsub(file.basename(name), "%.(.-)$", "") |
| 27567 |
if create then |
| 27568 |
if windows then |
| 27569 |
io.savedata(file.join(stubpath,base..".bat"),format(windows_stub,name)) |
| 27570 |
report("windows stub for '%s' created",base) |
| 27571 |
end |
| 27572 |
if unix then |
| 27573 |
io.savedata(file.join(stubpath,base),format(unix_stub,name)) |
| 27574 |
report("unix stub for '%s' created",base) |
| 27575 |
end |
| 27576 |
else |
| 27577 |
if windows and (os.remove(file.join(stubpath,base..'.bat')) or os.remove(file.join(stubpath,base..'.cmd'))) then |
| 27578 |
report("windows stub for '%s' removed", base) |
| 27579 |
end |
| 27580 |
if unix and (os.remove(file.join(stubpath,base)) or os.remove(file.join(stubpath,base..'.sh'))) then |
| 27581 |
report("unix stub for '%s' removed",base) |
| 27582 |
end |
| 27583 |
end |
| 27584 |
end |
| 27585 |
end |
| 27586 |
end |
| 27587 |
|
| 27588 |
function runners.resolve_string(filename) |
| 27589 |
if filename and filename ~= "" then |
| 27590 |
runners.report_location(resolvers.resolve(filename)) |
| 27591 |
end |
| 27592 |
end |
| 27593 |
|
| 27594 |
-- differs from texmfstart where locate appends .com .exe .bat ... todo |
| 27595 |
|
| 27596 |
function runners.locate_file(filename) -- was given file but only searches in tree |
| 27597 |
if filename and filename ~= "" then |
| 27598 |
if environment.argument("first") then |
| 27599 |
runners.report_location(resolvers.findfile(filename)) |
| 27600 |
-- resolvers.dowithfilesandreport(resolvers.findfile,filename) |
| 27601 |
elseif environment.argument("all") then |
| 27602 |
local result, status = resolvers.findfiles(filename) |
| 27603 |
if status and environment.argument("detail") then |
| 27604 |
runners.report_location(status) |
| 27605 |
else |
| 27606 |
runners.report_location(result) |
| 27607 |
end |
| 27608 |
else |
| 27609 |
runners.report_location(resolvers.findgivenfile(filename)) |
| 27610 |
-- resolvers.dowithfilesandreport(resolvers.findgivenfile,filename) |
| 27611 |
end |
| 27612 |
end |
| 27613 |
end |
| 27614 |
|
| 27615 |
function runners.locate_platform() |
| 27616 |
runners.report_location(os.platform) |
| 27617 |
end |
| 27618 |
|
| 27619 |
function runners.report_location(result) |
| 27620 |
if type(result) == "table" then |
| 27621 |
for i=1,#result do |
| 27622 |
if i > 1 then |
| 27623 |
io.write("\n") |
| 27624 |
end |
| 27625 |
io.write(result[i]) |
| 27626 |
end |
| 27627 |
else |
| 27628 |
io.write(result) |
| 27629 |
end |
| 27630 |
end |
| 27631 |
|
| 27632 |
function runners.edit_script(filename) -- we assume that gvim is present on most systems (todo: also in cnf file) |
| 27633 |
local editor = os.getenv("MTXRUN_EDITOR") or os.getenv("TEXMFSTART_EDITOR") or os.getenv("EDITOR") or 'gvim' |
| 27634 |
local rest = resolvers.resolve(filename) |
| 27635 |
if rest ~= "" then |
| 27636 |
local command = editor .. " " .. rest |
| 27637 |
if e_verbose then |
| 27638 |
report() |
| 27639 |
report("starting editor: %s",command) |
| 27640 |
report() |
| 27641 |
report() |
| 27642 |
end |
| 27643 |
os.launch(command) |
| 27644 |
end |
| 27645 |
end |
| 27646 |
|
| 27647 |
function runners.save_script_session(filename, list) |
| 27648 |
if type(list) == "table" then |
| 27649 |
local t = { } |
| 27650 |
for i=1,#list do |
| 27651 |
local key = list[i] |
| 27652 |
t[key] = environment.arguments[key] |
| 27653 |
end |
| 27654 |
io.savedata(filename,table.serialize(t,true)) |
| 27655 |
end |
| 27656 |
end |
| 27657 |
|
| 27658 |
function runners.load_script_session(filename) |
| 27659 |
if lfs.isfile(filename) then |
| 27660 |
local t = io.loaddata(filename) |
| 27661 |
if t then |
| 27662 |
t = loadstring(t) |
| 27663 |
if t then t = t() end |
| 27664 |
for key, value in pairs(t) do |
| 27665 |
environment.arguments[key] = value |
| 27666 |
end |
| 27667 |
end |
| 27668 |
end |
| 27669 |
end |
| 27670 |
|
| 27671 |
function resolvers.launch(str) |
| 27672 |
-- maybe we also need to test on mtxrun.launcher.suffix environment |
| 27673 |
-- variable or on windows consult the assoc and ftype vars and such |
| 27674 |
local launchers = runners.launchers[os.platform] if launchers then |
| 27675 |
local suffix = file.suffix(str) if suffix then |
| 27676 |
local runner = launchers[suffix] if runner then |
| 27677 |
str = runner .. " " .. str |
| 27678 |
end |
| 27679 |
end |
| 27680 |
end |
| 27681 |
os.launch(str) |
| 27682 |
end |
| 27683 |
|
| 27684 |
function runners.launch_file(filename) |
| 27685 |
local allresults = environment.arguments["all"] |
| 27686 |
local pattern = environment.arguments["pattern"] |
| 27687 |
local listonly = environment.arguments["list"] |
| 27688 |
if not pattern or pattern == "" then |
| 27689 |
pattern = filename |
| 27690 |
end |
| 27691 |
if not pattern or pattern == "" then |
| 27692 |
report("provide name or --pattern=") |
| 27693 |
else |
| 27694 |
local t = resolvers.findfiles(pattern,nil,allresults) |
| 27695 |
if not t or #t == 0 then |
| 27696 |
t = resolvers.findfiles("*/" .. pattern,nil,allresults) |
| 27697 |
end |
| 27698 |
if not t or #t == 0 then |
| 27699 |
t = resolvers.findfiles("*/" .. pattern .. "*",nil,allresults) |
| 27700 |
end |
| 27701 |
if t and #t > 0 then |
| 27702 |
for i=1,#t do |
| 27703 |
local name = t[i] |
| 27704 |
if listonly then |
| 27705 |
report("% 3i: %-30s %s",i,file.basename(name),file.dirname(name)) |
| 27706 |
else |
| 27707 |
report("launching: %s",name) |
| 27708 |
resolvers.launch(name) |
| 27709 |
if not allresults then |
| 27710 |
break |
| 27711 |
end |
| 27712 |
end |
| 27713 |
end |
| 27714 |
if listonly then |
| 27715 |
io.write("\n") |
| 27716 |
io.write("\n[select number]\n\n>> ") |
| 27717 |
local answer = tonumber(io.read()) |
| 27718 |
if answer then |
| 27719 |
io.write("\n") |
| 27720 |
local name = t[answer] |
| 27721 |
if name then |
| 27722 |
report("launching: %s",name) |
| 27723 |
resolvers.launch(name) |
| 27724 |
else |
| 27725 |
report("invalid number") |
| 27726 |
end |
| 27727 |
end |
| 27728 |
end |
| 27729 |
else |
| 27730 |
report("no match for %s", pattern) |
| 27731 |
end |
| 27732 |
end |
| 27733 |
end |
| 27734 |
|
| 27735 |
local mtxprefixes = { |
| 27736 |
{ "^mtx%-", "mtx-" }, |
| 27737 |
{ "^mtx%-t%-", "mtx-t-" }, |
| 27738 |
} |
| 27739 |
|
| 27740 |
function runners.find_mtx_script(filename) |
| 27741 |
local function found(name) |
| 27742 |
local path = file.dirname(name) |
| 27743 |
if path and path ~= "" then |
| 27744 |
return false |
| 27745 |
else |
| 27746 |
local fullname = own and own.path and file.join(own.path,name) |
| 27747 |
return io.exists(fullname) and fullname |
| 27748 |
end |
| 27749 |
end |
| 27750 |
filename = file.addsuffix(filename,"lua") |
| 27751 |
local basename = file.removesuffix(file.basename(filename)) |
| 27752 |
local suffix = file.suffix(filename) |
| 27753 |
-- qualified path, raw name |
| 27754 |
local fullname = file.is_qualified_path(filename) and io.exists(filename) and filename |
| 27755 |
if fullname and fullname ~= "" then |
| 27756 |
return fullname |
| 27757 |
end |
| 27758 |
-- current path, raw name |
| 27759 |
fullname = "./" .. filename |
| 27760 |
fullname = io.exists(fullname) and fullname |
| 27761 |
if fullname and fullname ~= "" then |
| 27762 |
return fullname |
| 27763 |
end |
| 27764 |
-- mtx- prefix checking |
| 27765 |
for i=1,#mtxprefixes do |
| 27766 |
local mtxprefix = mtxprefixes[i] |
| 27767 |
mtxprefix = find(filename,mtxprefix[1]) and "" or mtxprefix[2] |
| 27768 |
-- context namespace, mtx-<filename> |
| 27769 |
fullname = mtxprefix .. filename |
| 27770 |
fullname = found(fullname) or resolvers.findfile(fullname) |
| 27771 |
if fullname and fullname ~= "" then |
| 27772 |
return fullname |
| 27773 |
end |
| 27774 |
-- context namespace, mtx-<filename>s |
| 27775 |
fullname = mtxprefix .. basename .. "s" .. "." .. suffix |
| 27776 |
fullname = found(fullname) or resolvers.findfile(fullname) |
| 27777 |
if fullname and fullname ~= "" then |
| 27778 |
return fullname |
| 27779 |
end |
| 27780 |
-- context namespace, mtx-<filename minus trailing s> |
| 27781 |
fullname = mtxprefix .. gsub(basename,"s$","") .. "." .. suffix |
| 27782 |
fullname = found(fullname) or resolvers.findfile(fullname) |
| 27783 |
if fullname and fullname ~= "" then |
| 27784 |
return fullname |
| 27785 |
end |
| 27786 |
end |
| 27787 |
-- context namespace, just <filename> |
| 27788 |
fullname = resolvers.findfile(filename) |
| 27789 |
return fullname |
| 27790 |
end |
| 27791 |
|
| 27792 |
function runners.register_arguments(...) |
| 27793 |
local arguments = environment.arguments_after |
| 27794 |
local passedon = { ... } |
| 27795 |
for i=#passedon,1,-1 do |
| 27796 |
local pi = passedon[i] |
| 27797 |
if pi then |
| 27798 |
table.insert(arguments,1,pi) |
| 27799 |
end |
| 27800 |
end |
| 27801 |
end |
| 27802 |
|
| 27803 |
function runners.execute_ctx_script(filename,...) |
| 27804 |
runners.register_arguments(...) |
| 27805 |
local arguments = environment.arguments_after |
| 27806 |
local fullname = runners.find_mtx_script(filename) or "" |
| 27807 |
if file.suffix(fullname) == "cld" then |
| 27808 |
-- handy in editors where we force --autopdf |
| 27809 |
report("running cld script: %s",filename) |
| 27810 |
table.insert(arguments,1,fullname) |
| 27811 |
table.insert(arguments,"--autopdf") |
| 27812 |
fullname = runners.find_mtx_script("context") or "" |
| 27813 |
end |
| 27814 |
-- retry after generate but only if --autogenerate |
| 27815 |
if fullname == "" and environment.argument("autogenerate") then -- might become the default |
| 27816 |
resolvers.renewcache() |
| 27817 |
trackers.enable("resolvers.locating") |
| 27818 |
resolvers.load() |
| 27819 |
-- |
| 27820 |
fullname = runners.find_mtx_script(filename) or "" |
| 27821 |
end |
| 27822 |
-- that should do it |
| 27823 |
if fullname ~= "" then |
| 27824 |
local state = runners.prepare() |
| 27825 |
if state == 'error' then |
| 27826 |
return false |
| 27827 |
elseif state == 'skip' then |
| 27828 |
return true |
| 27829 |
elseif state == "run" then |
| 27830 |
-- load and save ... kind of undocumented |
| 27831 |
arg = { } for _,v in pairs(arguments) do arg[#arg+1] = resolvers.resolve(v) end |
| 27832 |
environment.initializearguments(arg) |
| 27833 |
local loadname = environment.arguments['load'] |
| 27834 |
if loadname then |
| 27835 |
if type(loadname) ~= "string" then loadname = file.basename(fullname) end |
| 27836 |
loadname = file.replacesuffix(loadname,"cfg") |
| 27837 |
runners.load_script_session(loadname) |
| 27838 |
end |
| 27839 |
filename = environment.files[1] |
| 27840 |
if e_verbose then |
| 27841 |
report("using script: %s (if --path is used don't run on path where mtxrun lives)\n",fullname) |
| 27842 |
end |
| 27843 |
environment.ownscript = fullname |
| 27844 |
dofile(fullname) |
| 27845 |
local savename = environment.arguments['save'] |
| 27846 |
if savename then |
| 27847 |
if type(savename) ~= "string" then savename = file.basename(fullname) end |
| 27848 |
savename = file.replacesuffix(savename,"cfg") |
| 27849 |
runners.save_script_session(savename,save_list) |
| 27850 |
end |
| 27851 |
return true |
| 27852 |
end |
| 27853 |
else |
| 27854 |
if filename == "" or filename == "help" then |
| 27855 |
local context = resolvers.findfile("mtx-context.lua") |
| 27856 |
trackers.enable("resolvers.locating") |
| 27857 |
if context ~= "" then |
| 27858 |
local result = dir.glob((gsub(context,"mtx%-context","mtx-*"))) -- () needed |
| 27859 |
local valid = { } |
| 27860 |
table.sort(result) |
| 27861 |
for i=1,#result do |
| 27862 |
local scriptname = result[i] |
| 27863 |
local scriptbase = match(scriptname,".*mtx%-([^%-]-)%.lua") |
| 27864 |
if scriptbase then |
| 27865 |
local data = io.loaddata(scriptname) |
| 27866 |
local application = match(data,"local application.-=.-(%{.-%})") |
| 27867 |
if application then |
| 27868 |
application = loadstring("return " .. application) |
| 27869 |
if application then |
| 27870 |
application = application() |
| 27871 |
local banner = application.banner |
| 27872 |
if banner then |
| 27873 |
local description, version = match(banner,"^(.-) ([%d.]+)$") |
| 27874 |
if description then |
| 27875 |
valid[#valid+1] = { scriptbase, version, description } |
| 27876 |
else |
| 27877 |
valid[#valid+1] = { scriptbase, "", banner } |
| 27878 |
end |
| 27879 |
end |
| 27880 |
end |
| 27881 |
end |
| 27882 |
end |
| 27883 |
end |
| 27884 |
if #valid > 0 then |
| 27885 |
application.identify() |
| 27886 |
report("no script name given, known scripts:") |
| 27887 |
report() |
| 27888 |
for k=1,#valid do |
| 27889 |
local v = valid[k] |
| 27890 |
report("%-12s %4s %s",v[1],v[2],v[3]) |
| 27891 |
end |
| 27892 |
end |
| 27893 |
else |
| 27894 |
report("no script name given") |
| 27895 |
end |
| 27896 |
else |
| 27897 |
filename = file.addsuffix(filename,"lua") |
| 27898 |
if file.is_qualified_path(filename) then |
| 27899 |
report("unknown script '%s'",filename) |
| 27900 |
else |
| 27901 |
report("unknown script '%s' or 'mtx-%s'",filename,filename) |
| 27902 |
end |
| 27903 |
end |
| 27904 |
return false |
| 27905 |
end |
| 27906 |
end |
| 27907 |
|
| 27908 |
function runners.prefixes() |
| 27909 |
application.identify() |
| 27910 |
report() |
| 27911 |
report(concat(resolvers.allprefixes(true)," ")) |
| 27912 |
end |
| 27913 |
|
| 27914 |
function runners.timedrun(filename) -- just for me |
| 27915 |
if filename and filename ~= "" then |
| 27916 |
runners.timed(function() os.execute(filename) end) |
| 27917 |
end |
| 27918 |
end |
| 27919 |
|
| 27920 |
function runners.timed(action) |
| 27921 |
statistics.timed(action,true) |
| 27922 |
end |
| 27923 |
|
| 27924 |
function runners.associate(filename) |
| 27925 |
os.launch(filename) |
| 27926 |
end |
| 27927 |
|
| 27928 |
function runners.evaluate(code,filename) -- for Luigi |
| 27929 |
local environment = table.setmetatableindex(_G) |
| 27930 |
if code == "loop" then |
| 27931 |
while true do |
| 27932 |
io.write("lua > ") |
| 27933 |
local code = io.read() |
| 27934 |
if code == "quit" or code == "exit" then |
| 27935 |
break |
| 27936 |
elseif code ~= "" then |
| 27937 |
local temp = string.match(code,"^= (.*)$") |
| 27938 |
if temp then |
| 27939 |
code = "inspect("..temp..")" |
| 27940 |
end |
| 27941 |
local compiled, message = load(code,"console","t",environment) |
| 27942 |
if type(compiled) ~= "function" then |
| 27943 |
compiled = load("inspect("..code..")","console","t",environment) |
| 27944 |
end |
| 27945 |
if type(compiled) ~= "function" then |
| 27946 |
io.write("! " .. (message or code).."\n") |
| 27947 |
else |
| 27948 |
io.write(compiled()) |
| 27949 |
end |
| 27950 |
end |
| 27951 |
end |
| 27952 |
else |
| 27953 |
if type(code) ~= "string" or code == "" then |
| 27954 |
code = filename |
| 27955 |
end |
| 27956 |
if code ~= "" then |
| 27957 |
local compiled, message = load(code,"console","t",environment) |
| 27958 |
if type(compiled) ~= "function" then |
| 27959 |
compiled = load("inspect("..code..")","console","t",environment) |
| 27960 |
end |
| 27961 |
if type(compiled) ~= "function" then |
| 27962 |
io.write("invalid lua code: " .. (message or code)) |
| 27963 |
return |
| 27964 |
end |
| 27965 |
io.write(compiled()) |
| 27966 |
end |
| 27967 |
end |
| 27968 |
end |
| 27969 |
|
| 27970 |
function runners.gethelp(filename) |
| 27971 |
local url = environment.argument("url") |
| 27972 |
if url and url ~= "" then |
| 27973 |
local command = string.gsub(environment.argument("command") or "unknown","^%s*\\*(.-)%s*$","%1") |
| 27974 |
url = utilities.templates.replace(url,{ command = command }) |
| 27975 |
os.launch(url) |
| 27976 |
else |
| 27977 |
report("no --url given") |
| 27978 |
end |
| 27979 |
end |
| 27980 |
|
| 27981 |
function runners.systeminfo() |
| 27982 |
report("architecture : %s",os.platform or "<unset>") |
| 27983 |
report("operating system : %s",os.name or "<unset>") |
| 27984 |
report("file architecture : %s",os.type or "<unset>") |
| 27985 |
report("binary path : %s",os.selfdir or "<unset>") |
| 27986 |
report("binary suffix : %s",os.binsuffix or "<unset>") |
| 27987 |
report("library suffix : %s",os.libsuffix or "<unset>") |
| 27988 |
end |
| 27989 |
|
| 27990 |
-- this is a bit dirty ... first we store the first filename and next we |
| 27991 |
-- split the arguments so that we only see the ones meant for this script |
| 27992 |
-- ... later we will use the second half |
| 27993 |
|
| 27994 |
local filename = environment.files[1] or "" |
| 27995 |
local ok = true |
| 27996 |
|
| 27997 |
local before, after = environment.splitarguments(filename) |
| 27998 |
environment.arguments_before, environment.arguments_after = before, after |
| 27999 |
environment.initializearguments(before) |
| 28000 |
|
| 28001 |
e_verbose = environment.arguments["verbose"] -- delayed till here (we need the ones before script) |
| 28002 |
|
| 28003 |
if e_verbose then |
| 28004 |
trackers.enable("resolvers.locating") |
| 28005 |
end |
| 28006 |
|
| 28007 |
-- maybe the unset has to go to this level |
| 28008 |
|
| 28009 |
local is_mkii_stub = runners.registered[file.removesuffix(file.basename(filename))] |
| 28010 |
|
| 28011 |
local e_argument = environment.argument |
| 28012 |
|
| 28013 |
if e_argument("timedlog") then |
| 28014 |
logs.settimedlog() |
| 28015 |
end |
| 28016 |
|
| 28017 |
if e_argument("usekpse") or e_argument("forcekpse") or is_mkii_stub then |
| 28018 |
|
| 28019 |
resolvers.load_tree(e_argument('tree'),true) -- force resolve of TEXMFCNF |
| 28020 |
|
| 28021 |
os.setenv("engine","") |
| 28022 |
os.setenv("progname","") |
| 28023 |
|
| 28024 |
local remapper = { |
| 28025 |
otf = "opentype fonts", |
| 28026 |
ttf = "truetype fonts", |
| 28027 |
ttc = "truetype fonts", |
| 28028 |
pfb = "type1 fonts", |
| 28029 |
other = "other text files", |
| 28030 |
} |
| 28031 |
|
| 28032 |
local progname = e_argument("progname") or 'context' |
| 28033 |
|
| 28034 |
local function kpse_initialized() |
| 28035 |
texconfig.kpse_init = true |
| 28036 |
local t = os.clock() |
| 28037 |
local k = kpse.original.new("luatex",progname) |
| 28038 |
local dummy = k:find_file("mtxrun.lua") -- so that we're initialized |
| 28039 |
report("kpse fallback with progname '%s' initialized in %s seconds",progname,os.clock()-t) |
| 28040 |
kpse_initialized = function() return k end |
| 28041 |
return k |
| 28042 |
end |
| 28043 |
|
| 28044 |
local findfile = resolvers.findfile |
| 28045 |
local showpath = resolvers.showpath |
| 28046 |
|
| 28047 |
if e_argument("forcekpse") then |
| 28048 |
|
| 28049 |
function resolvers.findfile(name,kind) |
| 28050 |
return (kpse_initialized():find_file(resolvers.cleanpath(name),(kind ~= "" and (remapper[kind] or kind)) or "tex") or "") or "" |
| 28051 |
end |
| 28052 |
function resolvers.showpath(name) |
| 28053 |
return (kpse_initialized():show_path(name)) or "" |
| 28054 |
end |
| 28055 |
|
| 28056 |
elseif e_argument("usekpse") or is_mkii_stub then |
| 28057 |
|
| 28058 |
resolvers.load() |
| 28059 |
|
| 28060 |
function resolvers.findfile(name,kind) |
| 28061 |
local found = findfile(name,kind) or "" |
| 28062 |
if found ~= "" then |
| 28063 |
return found |
| 28064 |
else |
| 28065 |
return (kpse_initialized():find_file(resolvers.cleanpath(name),(kind ~= "" and (remapper[kind] or kind)) or "tex") or "") or "" |
| 28066 |
end |
| 28067 |
end |
| 28068 |
function resolvers.showpath(name) |
| 28069 |
local found = showpath(name) or "" |
| 28070 |
if found ~= "" then |
| 28071 |
return found |
| 28072 |
else |
| 28073 |
return (kpse_initialized():show_path(name)) or "" |
| 28074 |
end |
| 28075 |
end |
| 28076 |
|
| 28077 |
end |
| 28078 |
|
| 28079 |
function runners.loadbase() |
| 28080 |
end |
| 28081 |
|
| 28082 |
else |
| 28083 |
|
| 28084 |
function runners.loadbase(...) |
| 28085 |
if not resolvers.load(...) then |
| 28086 |
report("forcing cache reload") |
| 28087 |
resolvers.renewcache() |
| 28088 |
trackers.enable("resolvers.locating") |
| 28089 |
if not resolvers.load(...) then |
| 28090 |
report("the resolver databases are not present or outdated") |
| 28091 |
end |
| 28092 |
end |
| 28093 |
end |
| 28094 |
|
| 28095 |
resolvers.load_tree(e_argument('tree'),e_argument("resolve")) |
| 28096 |
|
| 28097 |
end |
| 28098 |
|
| 28099 |
-- -- Gone, as it's useless and also because we don't enable locales in luametatex |
| 28100 |
-- -- at all. Too much hassle. |
| 28101 |
-- |
| 28102 |
-- do |
| 28103 |
-- |
| 28104 |
-- local a_locale = e_argument("locale") |
| 28105 |
-- |
| 28106 |
-- if a_locale then |
| 28107 |
-- |
| 28108 |
-- -- I really hate this crap but am too tired of discussing it over and over |
| 28109 |
-- -- again so for the sake of usage outside context we will provide ways to |
| 28110 |
-- -- use locales in an otherwise supposed to be locale agnostic system. And |
| 28111 |
-- -- forget about support in case of interferences. |
| 28112 |
-- |
| 28113 |
-- report() |
| 28114 |
-- report(what == "force" and "forcing locale:" or "original locale:") |
| 28115 |
-- report() |
| 28116 |
-- report(" collate : %s",status.lc_collate or "<unset>") |
| 28117 |
-- report(" ctype : %s",status.lc_ctype or "<unset>") |
| 28118 |
-- report(" monetary : %s",status.lc_monetary or "<unset>") |
| 28119 |
-- report(" numeric : %s",status.lc_numeric or "<unset>") |
| 28120 |
-- report(" time : %s",status.lc_time or "<unset>") |
| 28121 |
-- report() |
| 28122 |
-- |
| 28123 |
-- end |
| 28124 |
-- |
| 28125 |
-- if a_locale == "force" then |
| 28126 |
-- os.setlocale(status.lc_collate ,"collate") |
| 28127 |
-- os.setlocale(status.lc_ctype ,"ctype") |
| 28128 |
-- os.setlocale(status.lc_monetary,"monetary") |
| 28129 |
-- os.setlocale(status.lc_numeric ,"numeric") |
| 28130 |
-- os.setlocale(status.lc_time ,"time") |
| 28131 |
-- else |
| 28132 |
-- function os.setlocale() |
| 28133 |
-- end |
| 28134 |
-- end |
| 28135 |
-- |
| 28136 |
-- end |
| 28137 |
|
| 28138 |
if e_argument("script") or e_argument("scripts") then |
| 28139 |
|
| 28140 |
-- run a script by loading it (using libs), pass args |
| 28141 |
|
| 28142 |
if e_argument("nofiledatabase") then |
| 28143 |
-- handy for mtx-update |
| 28144 |
else |
| 28145 |
runners.loadbase() |
| 28146 |
end |
| 28147 |
if is_mkii_stub then |
| 28148 |
ok = runners.execute_script(filename,false,true) |
| 28149 |
else |
| 28150 |
ok = runners.execute_ctx_script(filename) |
| 28151 |
end |
| 28152 |
|
| 28153 |
elseif e_argument("evaluate") then |
| 28154 |
|
| 28155 |
runners.evaluate(e_argument("evaluate"),filename) |
| 28156 |
|
| 28157 |
elseif e_argument("selfmerge") then |
| 28158 |
|
| 28159 |
-- embed used libraries |
| 28160 |
|
| 28161 |
runners.loadbase() |
| 28162 |
local found = locate_libs() |
| 28163 |
|
| 28164 |
if found then |
| 28165 |
local mtxrun = resolvers.findfile("mtxrun.lua") -- includes local name |
| 28166 |
if lfs.isfile(mtxrun) then |
| 28167 |
utilities.merger.selfmerge(mtxrun,own.libs,{ found }) |
| 28168 |
application.report("runner updated on resolved path: %s",mtxrun) |
| 28169 |
else |
| 28170 |
utilities.merger.selfmerge(own.name,own.libs,{ found }) |
| 28171 |
application.report("runner updated on relative path: %s",own.name) |
| 28172 |
end |
| 28173 |
end |
| 28174 |
|
| 28175 |
elseif e_argument("selfclean") then |
| 28176 |
|
| 28177 |
-- remove embedded libraries |
| 28178 |
|
| 28179 |
runners.loadbase() |
| 28180 |
|
| 28181 |
local mtxrun = resolvers.findfile("mtxrun.lua") -- includes local name |
| 28182 |
if lfs.isfile(mtxrun) then |
| 28183 |
utilities.merger.selfclean(mtxrun) |
| 28184 |
application.report("runner cleaned on resolved path: %s",mtxrun) |
| 28185 |
else |
| 28186 |
utilities.merger.selfclean(own.name) |
| 28187 |
application.report("runner cleaned on relative path: %s",own.name) |
| 28188 |
end |
| 28189 |
|
| 28190 |
elseif e_argument("selfupdate") then |
| 28191 |
|
| 28192 |
runners.loadbase() |
| 28193 |
trackers.enable("resolvers.locating") |
| 28194 |
resolvers.updatescript(own.name,"mtxrun") |
| 28195 |
|
| 28196 |
elseif e_argument("ctxlua") or e_argument("internal") then |
| 28197 |
|
| 28198 |
-- run a script by loading it (using libs) |
| 28199 |
|
| 28200 |
runners.loadbase() |
| 28201 |
ok = runners.execute_script(filename,true) |
| 28202 |
|
| 28203 |
elseif e_argument("execute") then |
| 28204 |
|
| 28205 |
-- execute script |
| 28206 |
|
| 28207 |
runners.loadbase() |
| 28208 |
ok = runners.execute_script(filename) |
| 28209 |
|
| 28210 |
elseif e_argument("direct") then |
| 28211 |
|
| 28212 |
-- equals bin: |
| 28213 |
|
| 28214 |
runners.loadbase() |
| 28215 |
ok = runners.execute_program(filename) |
| 28216 |
|
| 28217 |
elseif e_argument("edit") then |
| 28218 |
|
| 28219 |
-- edit file |
| 28220 |
|
| 28221 |
runners.loadbase() |
| 28222 |
runners.edit_script(filename) |
| 28223 |
|
| 28224 |
elseif e_argument("launch") then |
| 28225 |
|
| 28226 |
runners.loadbase() |
| 28227 |
runners.launch_file(filename) |
| 28228 |
|
| 28229 |
elseif e_argument("associate") then |
| 28230 |
|
| 28231 |
runners.associate(filename) |
| 28232 |
|
| 28233 |
elseif e_argument("gethelp") then |
| 28234 |
|
| 28235 |
runners.gethelp() |
| 28236 |
|
| 28237 |
elseif e_argument("makestubs") then |
| 28238 |
|
| 28239 |
-- make stubs (depricated) |
| 28240 |
|
| 28241 |
runners.handle_stubs(true) |
| 28242 |
|
| 28243 |
elseif e_argument("removestubs") then |
| 28244 |
|
| 28245 |
-- remove stub (depricated) |
| 28246 |
|
| 28247 |
runners.loadbase() |
| 28248 |
runners.handle_stubs(false) |
| 28249 |
|
| 28250 |
elseif e_argument("resolve") then |
| 28251 |
|
| 28252 |
-- resolve string |
| 28253 |
|
| 28254 |
runners.loadbase() |
| 28255 |
runners.resolve_string(filename) |
| 28256 |
|
| 28257 |
elseif e_argument("locate") then |
| 28258 |
|
| 28259 |
-- locate file (only database) |
| 28260 |
|
| 28261 |
runners.loadbase() |
| 28262 |
runners.locate_file(filename) |
| 28263 |
|
| 28264 |
elseif e_argument("platform") or e_argument("show-platform") then |
| 28265 |
|
| 28266 |
-- locate platform |
| 28267 |
|
| 28268 |
runners.loadbase() |
| 28269 |
runners.locate_platform() |
| 28270 |
|
| 28271 |
elseif e_argument("prefixes") then |
| 28272 |
|
| 28273 |
runners.loadbase() |
| 28274 |
runners.prefixes() |
| 28275 |
|
| 28276 |
elseif e_argument("timedrun") then |
| 28277 |
|
| 28278 |
-- locate platform |
| 28279 |
|
| 28280 |
runners.loadbase() |
| 28281 |
runners.timedrun(filename) |
| 28282 |
|
| 28283 |
elseif e_argument("variables") or e_argument("show-variables") or e_argument("expansions") or e_argument("show-expansions") then |
| 28284 |
|
| 28285 |
resolvers.load("nofiles") |
| 28286 |
resolvers.listers.variables(e_argument("pattern")) |
| 28287 |
|
| 28288 |
elseif e_argument("configurations") or e_argument("show-configurations") then |
| 28289 |
|
| 28290 |
resolvers.load("nofiles") |
| 28291 |
resolvers.listers.configurations() |
| 28292 |
|
| 28293 |
elseif e_argument("find-file") then |
| 28294 |
|
| 28295 |
resolvers.load() |
| 28296 |
local e_all = e_argument("all") |
| 28297 |
local e_pattern = e_argument("pattern") |
| 28298 |
local e_format = e_argument("format") |
| 28299 |
local finder = e_all and resolvers.findfiles or resolvers.findfile |
| 28300 |
if not e_pattern then |
| 28301 |
runners.register_arguments(filename) |
| 28302 |
environment.initializearguments(environment.arguments_after) |
| 28303 |
resolvers.dowithfilesandreport(finder,environment.files,e_format) |
| 28304 |
elseif type(e_pattern) == "string" then |
| 28305 |
resolvers.dowithfilesandreport(finder,{ e_pattern },e_format) |
| 28306 |
end |
| 28307 |
|
| 28308 |
elseif e_argument("find-path") then |
| 28309 |
|
| 28310 |
resolvers.load() |
| 28311 |
local path = resolvers.findpath(filename) |
| 28312 |
if e_verbose then |
| 28313 |
report(path) |
| 28314 |
else |
| 28315 |
print(path) |
| 28316 |
end |
| 28317 |
|
| 28318 |
elseif e_argument("expand-braces") then |
| 28319 |
|
| 28320 |
resolvers.load("nofiles") |
| 28321 |
runners.register_arguments(filename) |
| 28322 |
environment.initializearguments(environment.arguments_after) |
| 28323 |
resolvers.dowithfilesandreport(resolvers.expandbraces, environment.files) |
| 28324 |
|
| 28325 |
elseif e_argument("expand-path") then |
| 28326 |
|
| 28327 |
resolvers.load("nofiles") |
| 28328 |
runners.register_arguments(filename) |
| 28329 |
environment.initializearguments(environment.arguments_after) |
| 28330 |
resolvers.dowithfilesandreport(resolvers.expandpath, environment.files) |
| 28331 |
|
| 28332 |
elseif e_argument("resolve-path") then |
| 28333 |
|
| 28334 |
resolvers.load("nofiles") |
| 28335 |
runners.register_arguments(filename) |
| 28336 |
environment.initializearguments(environment.arguments_after) |
| 28337 |
resolvers.dowithfilesandreport(resolvers.cleanedpathlist, environment.files) |
| 28338 |
|
| 28339 |
elseif e_argument("expand-var") or e_argument("expand-variable") then |
| 28340 |
|
| 28341 |
resolvers.load("nofiles") |
| 28342 |
runners.register_arguments(filename) |
| 28343 |
environment.initializearguments(environment.arguments_after) |
| 28344 |
resolvers.dowithfilesandreport(resolvers.expansion, environment.files) |
| 28345 |
|
| 28346 |
elseif e_argument("show-path") or e_argument("path-value") then |
| 28347 |
|
| 28348 |
resolvers.load("nofiles") |
| 28349 |
runners.register_arguments(filename) |
| 28350 |
environment.initializearguments(environment.arguments_after) |
| 28351 |
resolvers.dowithfilesandreport(resolvers.showpath, environment.files) |
| 28352 |
|
| 28353 |
elseif e_argument("var-value") or e_argument("show-value") then |
| 28354 |
|
| 28355 |
resolvers.load("nofiles") |
| 28356 |
runners.register_arguments(filename) |
| 28357 |
environment.initializearguments(environment.arguments_after) |
| 28358 |
resolvers.dowithfilesandreport(resolvers.variable,environment.files) |
| 28359 |
|
| 28360 |
elseif e_argument("format-path") then |
| 28361 |
|
| 28362 |
-- resolvers.load() |
| 28363 |
resolvers.load("nofiles") |
| 28364 |
report(caches.getwritablepath("format")) |
| 28365 |
|
| 28366 |
elseif e_argument("generate") then |
| 28367 |
|
| 28368 |
-- luatools |
| 28369 |
|
| 28370 |
if filename and filename ~= "" then |
| 28371 |
resolvers.load("nofiles") |
| 28372 |
trackers.enable("resolvers.locating") |
| 28373 |
resolvers.renew(filename) |
| 28374 |
else |
| 28375 |
resolvers.renewcache() |
| 28376 |
trackers.enable("resolvers.locating") |
| 28377 |
resolvers.load() |
| 28378 |
end |
| 28379 |
|
| 28380 |
e_verbose = true |
| 28381 |
|
| 28382 |
-- The original idea was to have a common system for installing formats for |
| 28383 |
-- luatex but I don't see this happen in the current tex ecosystem. Every format |
| 28384 |
-- goes its own (incompatible) way. |
| 28385 |
-- |
| 28386 |
-- elseif e_argument("make") or e_argument("ini") or e_argument("compile") then |
| 28387 |
-- |
| 28388 |
-- resolvers.load() |
| 28389 |
-- trackers.enable("resolvers.locating") |
| 28390 |
-- environment.make_format(filename) |
| 28391 |
|
| 28392 |
elseif e_argument("version") then |
| 28393 |
|
| 28394 |
application.version() |
| 28395 |
|
| 28396 |
application.report("source path",environment.ownbin) |
| 28397 |
|
| 28398 |
elseif e_argument("directives") then |
| 28399 |
|
| 28400 |
directives.show() |
| 28401 |
|
| 28402 |
elseif e_argument("trackers") then |
| 28403 |
|
| 28404 |
trackers.show() |
| 28405 |
|
| 28406 |
elseif e_argument("experiments") then |
| 28407 |
|
| 28408 |
experiments.show() |
| 28409 |
|
| 28410 |
elseif e_argument("exporthelp") then |
| 28411 |
|
| 28412 |
runners.loadbase() |
| 28413 |
application.export(e_argument("exporthelp"),filename) |
| 28414 |
|
| 28415 |
elseif e_argument("systeminfo") then |
| 28416 |
|
| 28417 |
runners.systeminfo() |
| 28418 |
|
| 28419 |
elseif e_argument("help") or filename=='help' or filename == "" then |
| 28420 |
|
| 28421 |
application.help() |
| 28422 |
|
| 28423 |
elseif find(filename,"^bin:") then |
| 28424 |
|
| 28425 |
runners.loadbase() |
| 28426 |
ok = runners.execute_program(filename) |
| 28427 |
|
| 28428 |
elseif is_mkii_stub then |
| 28429 |
|
| 28430 |
-- execute mkii script |
| 28431 |
|
| 28432 |
runners.loadbase() |
| 28433 |
ok = runners.execute_script(filename,false,true) |
| 28434 |
|
| 28435 |
elseif false then |
| 28436 |
|
| 28437 |
runners.loadbase() |
| 28438 |
ok = runners.execute_ctx_script(filename) |
| 28439 |
if not ok then |
| 28440 |
ok = runners.execute_script(filename) |
| 28441 |
end |
| 28442 |
|
| 28443 |
elseif environment.files[1] == 'texmfcnf.lua' then |
| 28444 |
|
| 28445 |
resolvers.load("nofiles") |
| 28446 |
resolvers.listers.configurations() |
| 28447 |
|
| 28448 |
else |
| 28449 |
|
| 28450 |
resolvers.load() |
| 28451 |
local e_pattern = e_argument("pattern") |
| 28452 |
if not e_pattern then |
| 28453 |
runners.register_arguments(filename) |
| 28454 |
environment.initializearguments(environment.arguments_after) |
| 28455 |
resolvers.dowithfilesandreport(resolvers.findfile,environment.files) |
| 28456 |
elseif type(e_pattern) == "string" then |
| 28457 |
resolvers.dowithfilesandreport(resolvers.findfile,{ e_pattern }) |
| 28458 |
end |
| 28459 |
|
| 28460 |
end |
| 28461 |
|
| 28462 |
if e_verbose then |
| 28463 |
report() |
| 28464 |
report("elapsed lua time: %0.3f seconds",os.runtime()) |
| 28465 |
end |
| 28466 |
|
| 28467 |
if os.type ~= "windows" then |
| 28468 |
texio.write("\n") -- is this still valid? |
| 28469 |
end |
| 28470 |
|
| 28471 |
if ok == false then ok = 1 elseif ok == true or ok == nil then ok = 0 end |
| 28472 |
|
| 28473 |
if lua and lua.getexitcode then |
| 28474 |
ok = lua.getexitcode() |
| 28475 |
end |
| 28476 |
|
| 28477 |
-- os.exit(ok,true) -- true forces a cleanup in 5.2+ |
| 28478 |
|
| 28479 |
os.exit(ok) -- true forces a cleanup in 5.2+ but reports a wrong number then |