Basic compatibility with LuaJIT and Lua < 5.3

This commit is contained in:
Shy 2024-08-28 16:05:11 +02:00
parent e5b2b4e016
commit 525b98cf10
2 changed files with 21 additions and 17 deletions

View file

@ -2,7 +2,7 @@
## Requirements ## Requirements
* lua >= 5.3 * lua (versions < 5.3 will have problems displaying non-ASCII characters)
* curl * curl
* sed * sed
* date * date

36
c4mate
View file

@ -13,6 +13,7 @@ local function utf8_decode(str)
-- Replace JSON Unicode escape sequences with chars. -- Replace JSON Unicode escape sequences with chars.
if str == nil then return "" end if str == nil then return "" end
str = string.gsub(str, "\\\"", "\"") str = string.gsub(str, "\\\"", "\"")
if utf8 == nil then return str end -- Only available in Lua >= 5.3.
return string.gsub(str, "\\u(%x%x%x%x)", return string.gsub(str, "\\u(%x%x%x%x)",
function (s) return utf8.char(tonumber(s, 16)) end) function (s) return utf8.char(tonumber(s, 16)) end)
end end
@ -62,7 +63,7 @@ function MatePad:fetch(api, data)
"curl %s --cookie '%s' --cookie-jar '%s' '%s/%s'", "curl %s --cookie '%s' --cookie-jar '%s' '%s/%s'",
self.CURL_FLAGS, self.COOKIE_JAR, self.COOKIE_JAR, self.HOST, api)) self.CURL_FLAGS, self.COOKIE_JAR, self.COOKIE_JAR, self.HOST, api))
end end
responce = curl:read("a") responce = curl:read("*a")
ok, exit, signal = curl:close() ok, exit, signal = curl:close()
if not ok then if not ok then
@ -143,17 +144,17 @@ sed -n '
while true do while true do
local entry = {} local entry = {}
local oldbalance = tonumber(pipe:read("l")) local oldbalance = tonumber(pipe:read("*l"))
if oldbalance == nil then break end if oldbalance == nil then break end
entry.oldbalance = oldbalance entry.oldbalance = oldbalance
entry.newbalance = tonumber(pipe:read("l")) entry.newbalance = tonumber(pipe:read("*l"))
entry.parameter = tonumber(pipe:read("l")) entry.parameter = tonumber(pipe:read("*l"))
entry.method = pipe:read("l") entry.method = pipe:read("*l")
if entry.method == "buy" or entry.method == "recharge" then if entry.method == "buy" or entry.method == "recharge" then
entry.name = utf8_decode(pipe:read("l")) entry.name = utf8_decode(pipe:read("*l"))
else else
-- set_balance, transferTo, transferFrom -- set_balance, transferTo, transferFrom
entry.reason = utf8_decode(pipe:read("l")) entry.reason = utf8_decode(pipe:read("*l"))
end end
table.insert(log, entry) table.insert(log, entry)
end end
@ -171,7 +172,7 @@ sed -n '
-- Convert timestamps to local time. -- Convert timestamps to local time.
local timestamps = io.popen("date -f "..datefile.." +'%_c'") local timestamps = io.popen("date -f "..datefile.." +'%_c'")
for i = 1, #log do for i = 1, #log do
log[i].time = timestamps:read("l") log[i].time = timestamps:read("*l")
end end
-- Check date exit code. -- Check date exit code.
@ -248,10 +249,10 @@ sed -n --sandbox '
while true do while true do
local item = {} local item = {}
item.id = tonumber(pipe:read("l")) item.id = tonumber(pipe:read("*l"))
if item.id == nil then break end if item.id == nil then break end
item.name = utf8_decode(pipe:read("l")) item.name = utf8_decode(pipe:read("*l"))
item.price = tonumber(pipe:read("l")) item.price = tonumber(pipe:read("*l"))
table.insert(roster, item) table.insert(roster, item)
end end
@ -305,7 +306,7 @@ function MatePad:give(amount, recipient, reason)
}) })
end end
--
-- Parse -u flag (which may be given multiple times). -- Parse -u flag (which may be given multiple times).
while arg[1] == "-u" do while arg[1] == "-u" do
table.remove(arg, 1) table.remove(arg, 1)
@ -355,7 +356,8 @@ if arg[1] == nil then
elseif arg[1] == "-l" or elseif arg[1] == "-l" or
arg[1] == "--log" then arg[1] == "--log" then
-- Print log. -- Print log.
local size = math.max(0, math.tointeger(arg[2]) or 10) local size = tonumber(arg[2]) or 10
size = math.max(0, size)
pad:print_log(pad:parse_log(size)) pad:print_log(pad:parse_log(size))
elseif arg[1] == "-g" or elseif arg[1] == "-g" or
@ -405,7 +407,7 @@ elseif tonumber(arg[1]) ~= nil then
-- Add to (or subtract from) balance. -- Add to (or subtract from) balance.
pad:init() pad:init()
if pad:update_balance() then if pad:update_balance() then
local new = math.tointeger(pad.balance + tonumber(arg[1])*100) local new = math.floor(pad.balance + tonumber(arg[1])*100)
if pad:set_balance(new) == "OK" then if pad:set_balance(new) == "OK" then
pad:update_balance() pad:update_balance()
print(string.format( print(string.format(
@ -437,8 +439,10 @@ else
-- Sort menu by id. -- Sort menu by id.
table.sort(menu, function (e1, e2) return e1.id < e2.id end) table.sort(menu, function (e1, e2) return e1.id < e2.id end)
-- Check if we can invoke stty. -- Check if we can invoke stty. Lua5.1 returns 0 on success, later versions
local is_tty = os.execute("tty -s") == true -- return true.
local is_tty = os.execute("tty -s")
is_tty = is_tty == true or is_tty == 0
for i, item in ipairs(menu) do for i, item in ipairs(menu) do
io.stdout:write(string.format("[%d/%d] Buy %s (#%d) for %.2f €? ", io.stdout:write(string.format("[%d/%d] Buy %s (#%d) for %.2f €? ",