|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- -*- coding: raw-text-unix -*-
-- This is the file `edrxlib.lua' of dednat4.
-- This file is in the public domain.
-- Author: Eduardo Ochs
-- Version: 2005sep07
-- Latest: <http://angg.twu.net/dednat4/edrxlib.lua>.
-- This should be a copy of <http://angg.twu.net/LUA/lua50init.lua>,
-- with minor changes in the headers.
-- (find-tkdiff "~/blogme/edrxlib.lua" "~/LUA/lua50init.lua")
-- (find-fline "~/LUA/lua50init.lua")
-- «.escripts» (to "escripts")
-- «.build-lua» (to "build-lua")
-- «.compat» (to "compat")
-- «.otherfunctions» (to "otherfunctions")
-- «.P» (to "P")
-- «.PP» (to "PP")
-- «.tests» (to "tests")
-- «.envsubst» (to "envsubst")
-- «.readfile» (to "readfile")
-- «.mytostring» (to "mytostring")
-- «.split» (to "split")
-- «.ee_expand» (to "ee_expand")
-- «.load_rex» (to "load_rex")
-- «.load_posix» (to "load_posix")
-- «.load_PP» (to "load_PP")
-- «.loadswigso» (to "loadswigso")
-- «.getoutput» (to "getoutput")
-- «escripts» (to ".escripts")
-- «compat» (to ".compat")
-- (find-lua50file "src/lib/lstrlib.c" "{\"find\", str_find},")
-- (find-lua50file "test/compat.lua" "strfind = str.find")
-- (find-lua50file "src/lua/")
-- (find-fline "/usr/share/lua50/compat.lua")
write = io.write
format = string.format
gsub = string.gsub
strfind = string.find
strlen = string.len
strsub = string.sub
concat = table.concat
foreachi = table.foreachi
getn = table.getn
tinsert = table.insert
tremove = table.remove
-- «otherfunctions» (to ".otherfunctions")
printf = function (...) write(format(unpack(arg))) end
pack = function (...) return arg end
-- 0-based string functions
-- (find-sh "lua -e \"print(substr0('abcdef', 2, 3)) --> cde\"")
substr0 = function (str, start0, len)
return string.sub(str, start0 + 1, len and start0 + len)
end
-- «P» (to ".P")
P = function (...)
for i,v in ipairs(arg) do
if type(v)=="number" then printf(" %d", v)
elseif type(v)=="string" then printf(" %q", v)
else printf(" <%s>", type(v))
end
end
print()
end
-- «PP» (to ".PP")
PP = function (...)
for i,v in ipairs(arg) do printf(" %s", mytostring(v)) end
print()
return unpack(arg)
end
-- «tests» (to ".tests")
--[[
#*
lua50e 'P(1, 3)'
lua50e 'P(string.find("0123456789", "3(45)(67)", 4))'
lua50e 'PP(string.find("0123456789", "3(45)(67)", 4))'
lua50e 'P(string.find("0123456789", "3(45)(67)", 5))'
#*
]]
-- «envsubst» (to ".envsubst")
-- (find-es "lua5" "envsubst")
setenv_ = {}
setenv = function (varname, value) setenv_[varname] = value end
getenv = function (varname) return setenv_[varname] or os.getenv(varname) end
envsubst = function (str)
return string.gsub(str, "%$([%a_][%w_]*)", function (e)
return getenv(e) or ""
end)
end
-- «readfile» (to ".readfile")
-- (find-es "lua5" "readfile")
readfile = function (fname)
local f = assert(io.open(fname, "r"))
local bigstr = f:read("*a")
f:close()
return bigstr
end
writefile = function (fname, bigstr)
local f = assert(io.open(fname, "w+"))
f:write(bigstr)
f:close()
end
-- «mytostring» (to ".mytostring")
-- (find-es "lua5" "mytostring")
mysort = function (origtable)
local tmptable = {}
for key,val in pairs(origtable) do
table.insert(tmptable, {key=key, val=val})
end
local comp = function (item1, item2)
local key1, key2 = item1.key, item2.key
local type1, type2 = type(key1), type(key2)
if type1==type2 then
if type1=="number" then return key1 < key2 end
if type1=="string" then return key1 < key2 end
return tostring(key1) < tostring(key2)
else
return type1<type2
end
end
table.sort(tmptable, comp)
return tmptable
end
mytostring = function (v)
local t = type(v)
if t=="number" then return tostring(v) end
if t=="string" then return format("%q", v) end
if t=="table" then
local tmptable = mysort(v)
local bigstring = "{"
local sep = ""
for i = 1, table.getn(tmptable) do
local entry = tmptable[i]
local keystr, valstr = mytostring(entry.key), mytostring(entry.val)
bigstring = bigstring..sep..keystr.."="..valstr
sep = ", "
end
return bigstring .. "}"
end
return "<"..tostring(v)..">"
end
-- «split» (to ".split")
-- (find-es "lua5" "split")
split = function (str, pat)
local arr = {}
string.gsub(str, pat or "([^%s]+)", function (word)
table.insert(arr, word)
end)
return arr
end
-- «ee_expand» (to ".ee_expand")
-- (find-eevfile "eev-dev.el" "defun ee-expand")
ee_expand = function (path)
path = string.gsub(path, "^~$", "$HOME/", 1)
path = string.gsub(path, "^~/", "$HOME/", 1)
path = string.gsub(path, "^%$(%w+)", os.getenv, 1)
return path
end
min = function (a, b)
if a < b then return a else return b end
end
max = function (a, b)
if a < b then return b else return a end
end
-- «load_rex» (to ".load_rex")
-- (find-es "lua5" "rexlib")
-- Usage: if not rex then load_rex() end
load_rex = function ()
assert(loadlib(getenv("HOME").."/.lua50/lrexlib.so", "luaopen_rex"))()
setmetatable(rex,
{__call = function (self, p, cf, lo) return self.newPOSIX(p, cf, lo) end})
function rex.find(s, p, st) return rex(p):match(s, st) end
function rex.gsub(s, p, f, n) return rex(p):gmatch(s, f, n) end
end
-- «load_posix» (to ".load_posix")
-- (find-es "lua5" "load_posix")
-- (find-es "lua5" "posix-install")
load_posix = function ()
assert(loadlib(getenv("HOME").."/.lua50/lposix.so", "luaopen_posix"))()
end
-- «load_PP» (to ".load_PP")
-- (find-angg ".lua50/PP.c")
-- Load PP.so, that defines a C function called PP for inspecting the stack
load_PP = function ()
assert(loadlib(getenv("HOME").."/.lua50/PP.so", "PP_init"))()
end
-- «loadswigso» (to ".loadswigso")
-- (find-es "swig" "myswiglua")
-- Example: loadswigso("C", "./myparser.so", "parser", "countwords")
loadswigso = function (modulename, fname_so, ...)
assert(loadlib(fname_so, modulename.."_Init"))()
local module = _G[modulename]
for i=1,getn(arg) do
_G[arg[i]] = module[arg[i]] -- export to the table of globals
end
end
-- «getoutput» (to ".getoutput")
-- (find-es "lua5" "getoutput")
getoutput = function (command)
local pipe = assert(io.popen(command))
local output = pipe:read("*a")
pipe:close()
return output
end