summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lualibs-dir.lua90
-rw-r--r--lualibs-file.lua131
-rw-r--r--lualibs-io.lua5
-rw-r--r--lualibs-lua.lua11
-rw-r--r--lualibs-os.lua14
-rw-r--r--lualibs-table.lua32
-rw-r--r--lualibs-util-str.lua4
-rw-r--r--lualibs-util-tab.lua8
8 files changed, 162 insertions, 133 deletions
diff --git a/lualibs-dir.lua b/lualibs-dir.lua
index c56af1b..81ac65e 100644
--- a/lualibs-dir.lua
+++ b/lualibs-dir.lua
@@ -6,7 +6,8 @@ if not modules then modules = { } end modules ['l-dir'] = {
license = "see context related readme files"
}
--- dir.expandname will be merged with cleanpath and collapsepath
+-- todo: dir.expandname will be sped up and merged with cleanpath and collapsepath
+-- todo: keep track of currentdir (chdir, pushdir, popdir)
local type, select = type, select
local find, gmatch, match, gsub, sub = string.find, string.gmatch, string.match, string.gsub, string.sub
@@ -490,52 +491,63 @@ end
dir.makedirs = dir.mkdirs
--- we can only define it here as it uses dir.current
-if onwindows then
+do
- function dir.expandname(str) -- will be merged with cleanpath and collapsepath\
- local first, nothing, last = match(str,"^(//)(//*)(.*)$")
- if first then
- first = dir.current() .. "/" -- dir.current sanitizes
- end
- if not first then
- first, last = match(str,"^(//)/*(.*)$")
- end
- if not first then
- first, last = match(str,"^([a-zA-Z]:)(.*)$")
- if first and not find(last,"^/") then
- local d = currentdir()
- if chdir(first) then
- first = dir.current()
+ -- we can only define it here as it uses dir.chdir and we also need to
+ -- make sure we use the non sandboxed variant because otherwise we get
+ -- into a recursive loop due to usage of expandname in the file resolver
+
+ local chdir = sandbox and sandbox.original(chdir) or chdir
+
+ if onwindows then
+
+ local xcurrentdir = dir.current
+
+ function dir.expandname(str) -- will be merged with cleanpath and collapsepath\
+ local first, nothing, last = match(str,"^(//)(//*)(.*)$")
+ if first then
+ first = xcurrentdir() .. "/" -- xcurrentdir sanitizes
+ end
+ if not first then
+ first, last = match(str,"^(//)/*(.*)$")
+ end
+ if not first then
+ first, last = match(str,"^([a-zA-Z]:)(.*)$")
+ if first and not find(last,"^/") then
+ local d = currentdir() -- push / pop
+ if chdir(first) then
+ first = xcurrentdir() -- xcurrentdir sanitizes
+ end
+ chdir(d)
end
- chdir(d)
+ end
+ if not first then
+ first, last = xcurrentdir(), str
+ end
+ last = gsub(last,"//","/")
+ last = gsub(last,"/%./","/")
+ last = gsub(last,"^/*","")
+ first = gsub(first,"/*$","")
+ if last == "" or last == "." then
+ return first
+ else
+ return first .. "/" .. last
end
end
- if not first then
- first, last = dir.current(), str
- end
- last = gsub(last,"//","/")
- last = gsub(last,"/%./","/")
- last = gsub(last,"^/*","")
- first = gsub(first,"/*$","")
- if last == "" or last == "." then
- return first
- else
- return first .. "/" .. last
- end
- end
-else
+ else
- function dir.expandname(str) -- will be merged with cleanpath and collapsepath
- if not find(str,"^/") then
- str = currentdir() .. "/" .. str
+ function dir.expandname(str) -- will be merged with cleanpath and collapsepath
+ if not find(str,"^/") then
+ str = currentdir() .. "/" .. str
+ end
+ str = gsub(str,"//","/")
+ str = gsub(str,"/%./","/")
+ str = gsub(str,"(.)/%.$","%1")
+ return str
end
- str = gsub(str,"//","/")
- str = gsub(str,"/%./","/")
- str = gsub(str,"(.)/%.$","%1")
- return str
+
end
end
diff --git a/lualibs-file.lua b/lualibs-file.lua
index 2742e99..2c471d7 100644
--- a/lualibs-file.lua
+++ b/lualibs-file.lua
@@ -15,51 +15,53 @@ if not lfs then
lfs = optionalrequire("lfs")
end
-if not lfs then
-
- lfs = {
- getcurrentdir = function()
- return "."
- end,
- attributes = function()
- return nil
- end,
- isfile = function(name)
- local f = io.open(name,'rb')
- if f then
- f:close()
- return true
- end
- end,
- isdir = function(name)
- print("you need to load lfs")
- return false
- end
- }
-
-elseif not lfs.isfile then
-
- local attributes = lfs.attributes
-
- function lfs.isdir(name)
- return attributes(name,"mode") == "directory"
- end
-
- function lfs.isfile(name)
- return attributes(name,"mode") == "file"
- end
-
- -- function lfs.isdir(name)
- -- local a = attributes(name)
- -- return a and a.mode == "directory"
- -- end
-
- -- function lfs.isfile(name)
- -- local a = attributes(name)
- -- return a and a.mode == "file"
- -- end
-
-end
+-- -- see later
+--
+-- if not lfs then
+--
+-- lfs = {
+-- getcurrentdir = function()
+-- return "."
+-- end,
+-- attributes = function()
+-- return nil
+-- end,
+-- isfile = function(name)
+-- local f = io.open(name,'rb')
+-- if f then
+-- f:close()
+-- return true
+-- end
+-- end,
+-- isdir = function(name)
+-- print("you need to load lfs")
+-- return false
+-- end
+-- }
+--
+-- elseif not lfs.isfile then
+--
+-- local attributes = lfs.attributes
+--
+-- function lfs.isdir(name)
+-- return attributes(name,"mode") == "directory"
+-- end
+--
+-- function lfs.isfile(name)
+-- return attributes(name,"mode") == "file"
+-- end
+--
+-- -- function lfs.isdir(name)
+-- -- local a = attributes(name)
+-- -- return a and a.mode == "directory"
+-- -- end
+--
+-- -- function lfs.isfile(name)
+-- -- local a = attributes(name)
+-- -- return a and a.mode == "file"
+-- -- end
+--
+-- end
local insert, concat = table.insert, table.concat
local match, find, gmatch = string.match, string.find, string.gmatch
@@ -72,6 +74,28 @@ local checkedsplit = string.checkedsplit
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
+-- better this way:
+
+local tricky = S("/\\") * P(-1)
+local attributes = lfs.attributes
+
+if sandbox then
+ sandbox.redefine(lfs.isfile,"lfs.isfile")
+ sandbox.redefine(lfs.isdir, "lfs.isdir")
+end
+
+function lfs.isdir(name)
+ if lpegmatch(tricky,name) then
+ return attributes(name,"mode") == "directory"
+ else
+ return attributes(name.."/.","mode") == "directory"
+ end
+end
+
+function lfs.isfile(name)
+ return attributes(name,"mode") == "file"
+end
+
local colon = P(":")
local period = P(".")
local periods = P("..")
@@ -554,23 +578,6 @@ function file.collapsepath(str,anchor) -- anchor: false|nil, true, "."
end
end
--- better this way:
-
-local tricky = S("/\\") * P(-1)
-local attributes = lfs.attributes
-
-function lfs.isdir(name)
- if lpegmatch(tricky,name) then
- return attributes(name,"mode") == "directory"
- else
- return attributes(name.."/.","mode") == "directory"
- end
-end
-
-function lfs.isfile(name)
- return attributes(name,"mode") == "file"
-end
-
-- local function test(str,...)
-- print(string.format("%-20s %-15s %-30s %-20s",str,file.collapsepath(str),file.collapsepath(str,true),file.collapsepath(str,".")))
-- end
diff --git a/lualibs-io.lua b/lualibs-io.lua
index 020e811..a91d44d 100644
--- a/lualibs-io.lua
+++ b/lualibs-io.lua
@@ -339,11 +339,6 @@ function io.readstring(f,n,m)
return str
end
---
-
-if not io.i_limiter then function io.i_limiter() end end -- dummy so we can test safely
-if not io.o_limiter then function io.o_limiter() end end -- dummy so we can test safely
-
-- This works quite ok:
--
-- function io.piped(command,writer)
diff --git a/lualibs-lua.lua b/lualibs-lua.lua
index 9565f48..1a2a987 100644
--- a/lualibs-lua.lua
+++ b/lualibs-lua.lua
@@ -165,3 +165,14 @@ end
if lua then
lua.mask = load([[τεχ = 1]]) and "utf" or "ascii"
end
+
+local flush = io.flush
+
+if flush then
+
+ local execute = os.execute if execute then function os.execute(...) flush() return execute(...) end end
+ local exec = os.exec if exec then function os.exec (...) flush() return exec (...) end end
+ local spawn = os.spawn if spawn then function os.spawn (...) flush() return spawn (...) end end
+ local popen = io.popen if popen then function io.popen (...) flush() return popen (...) end end
+
+end
diff --git a/lualibs-os.lua b/lualibs-os.lua
index 1dff79c..f44b316 100644
--- a/lualibs-os.lua
+++ b/lualibs-os.lua
@@ -25,8 +25,6 @@ if not modules then modules = { } end modules ['l-os'] = {
-- os.sleep() => socket.sleep()
-- math.randomseed(tonumber(string.sub(string.reverse(tostring(math.floor(socket.gettime()*10000))),1,6)))
--- maybe build io.flush in os.execute
-
local os = os
local date, time = os.date, os.time
local find, format, gsub, upper, gmatch = string.find, string.format, string.gsub, string.upper, string.gmatch
@@ -118,15 +116,11 @@ end
-- end of environment hack
-local execute, spawn, exec, iopopen, ioflush = os.execute, os.spawn or os.execute, os.exec or os.execute, io.popen, io.flush
-
-function os.execute(...) ioflush() return execute(...) end
-function os.spawn (...) ioflush() return spawn (...) end
-function os.exec (...) ioflush() return exec (...) end
-function io.popen (...) ioflush() return iopopen(...) end
+local execute = os.execute
+local iopopen = io.popen
function os.resultof(command)
- local handle = io.popen(command,"r")
+ local handle = iopopen(command,"r") -- already has flush
if handle then
local result = handle:read("*all") or ""
handle:close()
@@ -160,7 +154,7 @@ local launchers = {
}
function os.launch(str)
- os.execute(format(launchers[os.name] or launchers.unix,str))
+ execute(format(launchers[os.name] or launchers.unix,str))
end
if not os.times then -- ?
diff --git a/lualibs-table.lua b/lualibs-table.lua
index 3eb8b85..97e0441 100644
--- a/lualibs-table.lua
+++ b/lualibs-table.lua
@@ -49,9 +49,19 @@ function table.keys(t)
end
end
+-- local function compare(a,b)
+-- local ta, tb = type(a), type(b) -- needed, else 11 < 2
+-- if ta == tb then
+-- return a < b
+-- else
+-- return tostring(a) < tostring(b) -- not that efficient
+-- end
+-- end
+
local function compare(a,b)
- local ta, tb = type(a), type(b) -- needed, else 11 < 2
- if ta == tb then
+ local ta = type(a) -- needed, else 11 < 2
+ local tb = type(b) -- needed, else 11 < 2
+ if ta == tb and ta == "number" then
return a < b
else
return tostring(a) < tostring(b) -- not that efficient
@@ -469,7 +479,7 @@ local function do_serialize(root,name,depth,level,indexed)
end
end
-- we could check for k (index) being number (cardinal)
- if root and next(root) then
+ if root and next(root) ~= nil then
-- local first, last = nil, 0 -- #root cannot be trusted here (will be ok in 5.2 when ipairs is gone)
-- if compact then
-- -- NOT: for k=1,#root do (we need to quit at nil)
@@ -513,7 +523,7 @@ local function do_serialize(root,name,depth,level,indexed)
handle(format("%s %q,",depth,v))
end
elseif tv == "table" then
- if not next(v) then
+ if next(v) == nil then
handle(format("%s {},",depth))
elseif inline then -- and #t > 0
local st = simple_table(v)
@@ -597,7 +607,7 @@ local function do_serialize(root,name,depth,level,indexed)
end
end
elseif tv == "table" then
- if not next(v) then
+ if next(v) == nil then
if tk == "number" then
if hexify then
handle(format("%s [0x%X]={},",depth,k))
@@ -683,7 +693,7 @@ local function do_serialize(root,name,depth,level,indexed)
--~ end
end
end
- if level > 0 then
+ if level > 0 then
handle(format("%s},",depth))
end
end
@@ -748,7 +758,7 @@ local function serialize(_handle,root,name,specification) -- handle wins
root._w_h_a_t_e_v_e_r_ = nil
end
-- Let's forget about empty tables.
- if next(root) then
+ if next(root) ~= nil then
do_serialize(root,name,"",0)
end
end
@@ -928,7 +938,7 @@ local function sparse(old,nest,keeptables)
if not (v == "" or v == false) then
if nest and type(v) == "table" then
v = sparse(v,nest)
- if keeptables or next(v) then
+ if keeptables or next(v) ~= nil then
new[k] = v
end
else
@@ -1066,11 +1076,11 @@ end
-- slower than #t on indexed tables (#t only returns the size of the numerically indexed slice)
function table.is_empty(t)
- return not t or not next(t)
+ return not t or next(t) == nil
end
function table.has_one_entry(t)
- return t and not next(t,next(t))
+ return t and next(t,next(t)) == nil
end
-- new
@@ -1157,7 +1167,7 @@ function table.filtered(t,pattern,sort,cmp)
else
local n = next(t)
local function iterator()
- while n do
+ while n ~= nil do
local k = n
n = next(t,k)
if find(k,pattern) then
diff --git a/lualibs-util-str.lua b/lualibs-util-str.lua
index a040b01..a677a82 100644
--- a/lualibs-util-str.lua
+++ b/lualibs-util-str.lua
@@ -361,10 +361,10 @@ strings.tracers = tracedchars
function string.tracedchar(b)
-- todo: table
if type(b) == "number" then
- return tracedchars[b] or (utfchar(b) .. " (U+" .. format('%05X',b) .. ")")
+ return tracedchars[b] or (utfchar(b) .. " (U+" .. format("%05X",b) .. ")")
else
local c = utfbyte(b)
- return tracedchars[c] or (b .. " (U+" .. format('%05X',c) .. ")")
+ return tracedchars[c] or (b .. " (U+" .. (c and format("%05X",c) or "?????") .. ")")
end
end
diff --git a/lualibs-util-tab.lua b/lualibs-util-tab.lua
index 077c906..5eae0d5 100644
--- a/lualibs-util-tab.lua
+++ b/lualibs-util-tab.lua
@@ -623,7 +623,7 @@ function table.serialize(root,name,specification)
depth = depth + 1
end
-- we could check for k (index) being number (cardinal)
- if root and next(root) then
+ if root and next(root) ~= nil then
local first = nil
local last = 0
last = #root
@@ -648,7 +648,7 @@ function table.serialize(root,name,specification)
elseif tv == "string" then
n = n + 1 t[n] = f_val_str(depth,v)
elseif tv == "table" then
- if not next(v) then
+ if next(v) == nil then
n = n + 1 t[n] = f_val_not(depth)
else
local st = simple_table(v)
@@ -678,7 +678,7 @@ function table.serialize(root,name,specification)
n = n + 1 t[n] = f_key_boo_value_str(depth,k,v)
end
elseif tv == "table" then
- if not next(v) then
+ if next(v) == nil then
if tk == "number" then
n = n + 1 t[n] = f_key_num_value_not(depth,k,v)
elseif tk == "string" then
@@ -742,7 +742,7 @@ function table.serialize(root,name,specification)
root._w_h_a_t_e_v_e_r_ = nil
end
-- Let's forget about empty tables.
- if next(root) then
+ if next(root) ~= nil then
do_serialize(root,name,1,0)
end
end