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
* lua >= 5.3
* lua (versions < 5.3 will have problems displaying non-ASCII characters)
* curl
* sed
* date

36
c4mate
View file

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