parsav  common.lua at [b9cf14c14b]

File common.lua artifact c72e0a0971 part of check-in b9cf14c14b


local map = function(lst,fn)
	local new = {} for k,v in pairs(lst) do
		local nv, nk = fn(v,k)
		new[nk or k] = nv
	end
	return new
end

local chomp = function(str)
	while string.sub(str,#str) == '\n' do
		str = string.sub(str,1,#str - 1)
	end
	return str
end

local exec = function(args, val)
	local cmd = table.concat(map(args, function(a)
		return string.format('%q', a)
	end), ' ')
	if val then return os.execute(cmd) else
		local fd = io.popen(cmd,'r')
		local t = fd:read('*a')
		return chomp(t), fd:close()
	end
end

local function dump(v,pfx,cyc)
	pfx = pfx or ''
	cyc = cyc or {}
	local np = pfx .. '  '

	if type(v) == 'table' then
		if cyc[v] then return '<...>' else cyc[v] = true end
	end

	if type(v) == 'string' then
		return string.format('%q', v)
	elseif type(v) == 'table' then
		local str = ''
		for k,v in pairs(v) do
			local tkey, tval = dump(k,np,cyc), dump(v,np,cyc)
			str = str .. string.format('%s[%s] = %s\n', np, tkey,tval)
		end
		return '{\n' .. str .. pfx .. '}\n'
	else
		return string.format('%s', v)
	end
end
local ping = function(path)
	local f = io.open(path)
	if f then f:close() return true end
	return false
end
local tobool = function(s)
	if s == true then return true
	elseif s == false then return false end
	s = ({
		yes = true, ['true'] = true,   on = true,   ['1'] = true;
		no = false, ['false'] = false, off = false, ['0'] = false;
	})[string.lower(s)]
	if not s then return false end
	return true
end

if ping '/dev/urandom' then
	local r = io.open('/dev/urandom')
	local ent = r:read(8) r:close()
	local seed = 1 for i = 1, 8 do
		seed = seed * string.byte(string.sub(ent,i,i))
	end
	math.randomseed(seed)
else math.randomseed(os.time()) end

return {
	exec = exec;
	dump = dump;
	ping = ping;
	chomp = chomp;
	map = map;
	tobool = tobool;
	rndstr = function(len)
		local s = ''
		for i=1,len do
			local ofs = math.random(0,10 + 26)
			if ofs >= 10 then ofs = 0x60 + (ofs - 10)
						 else ofs = 0x30 + ofs end
			s = s .. string.char(ofs) 
		end
		return s
	end;
	append = function(a,b)
		for _, v in pairs(b) do a[#a+1] = v end
	end;
	has = function(haystack,needle,eq)
		eq = eq or function(a,b) return a == b end
		for k,v in pairs(haystack) do
			if eq(needle,v) then return k end
		end
	end;
	keys = function(ary)
		local kt = {}
		for k,v in pairs(ary) do kt[#kt+1] = k end
		return kt
	end;
	ingest = function(f)
		local h = io.open(f, 'r')
		if h == nil then return nil end
		local txt = h:read('*a') h:close()
		return chomp(txt)
	end;
	parseargs = function(a)
		local raw = false
		local opts, args = {}, {}
		for i,v in ipairs(a) do
			if v == '--' then
				raw = true 
			else
				if (not raw) and string.sub(v,1,2) == '--' then -- longopt
					opts[#opts+1] = string.sub(v,3)
				elseif (not raw) and string.sub(v,1,1) == '-' then -- shortopts
					for c in string.gmatch(string.sub(v,2), '.') do
						opts[#opts+1] = c
					end
				else args[#args+1]=v end
			end
		end
		return opts, args
	end;
}