Blog: 2025-02-15: Difference between revisions

From razwiki
Jump to navigation Jump to search
(Created page with "I figure before I get to writing fennel and stuff I should understand the host platform: namely lua! I have lua 5.4 installed First thing: I wanted to install the lua socket library to make a lua irc server. However the default install tried to write to /usr/ which failed with a permission error. Better to write to ~/.local <pre> $ cat ~/.config/lua/luarocks-5.4.lua rocks_trees = { home .. "/.local/lib/lua"; } lua_interpreter = "lua5.4" </pre> Also need to set it...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 52: Line 52:
linenoise 0.9-1 is now installed in /Users/razzi/.local/lib/lua (license: MIT/X11)
linenoise 0.9-1 is now installed in /Users/razzi/.local/lib/lua (license: MIT/X11)
</pre>
</pre>

Troublesomely lua calls like server:accept are blocking and are not interrupted by control-c so the server doesn't exit?? but c-\ does work as a force quit .. huh

https://stackoverflow.com/questions/50506099/why-is-lua-not-respecting-my-sigint-signal

<pre>
socket = require('socket')
signal = require('posix.signal')

signal.signal(signal.SIGINT, function()
os.exit()
end)

server = socket.bind('*', 0)

local ip, port = server:getsockname()

print('Got a socket on ' .. ip .. ':' .. port)

while 1 do
local client = server:accept()
local ip, port = client:getpeername()
print('Client socket: ' .. ip .. ', port: ' .. port)

client:settimeout(10)

local line, err = client:receive()

if not err then
client:send('You said: ' .. line .. '\n')
end

client:close()
end
</pre>

Another iteration, with "better" control-c handling (times out after 5 seconds)

<pre>
socket = require('socket')
signal = require('posix.signal')

signal.signal(signal.SIGINT, function()
os.exit()
end)

server = socket.bind('*', 0)

local ip, port = server:getsockname()

print('Got a socket on ' .. ip .. ':' .. port)

server:settimeout(5)

function server_loop()
local client, err = server:accept()

if err then
print('Accept err: ' .. err)
return
end

local ip, port = client:getpeername()
print('Client socket: ' .. ip .. ', port: ' .. port)

client:settimeout(5)

local line, err = client:receive()

if err then
print('Receive err: ' .. err)
return
end

print('Client said: ' .. line)
client:send('You said: ' .. line .. '\n')

client:close()
end

while 1 do
server_loop()
end

server.close()
</pre>

Also lua has no continue ?? haha so I had to wrap my loop body in a function. Not a bad refactor, could add as an answer to https://stackoverflow.com/questions/3524970/why-does-lua-have-no-continue-statement

Latest revision as of 01:28, 15 February 2025

I figure before I get to writing fennel and stuff I should understand the host platform: namely lua!

I have lua 5.4 installed

First thing: I wanted to install the lua socket library to make a lua irc server. However the default install tried to write to /usr/ which failed with a permission error. Better to write to ~/.local

$ cat ~/.config/lua/luarocks-5.4.lua
rocks_trees = {
  home .. "/.local/lib/lua";
}

lua_interpreter = "lua5.4"

Also need to set it up in ~/.profile:

export LUAROCKS_CONFIG="$HOME/.config/lua/luarocks-5.4.lua"
export LUA_PATH="$LUA_PATH;$HOME/.local/lib/lua/share/lua/5.4/?.lua"
export LUA_CPATH="$HOME/.local/lib/lua/lib/lua/5.4/?.so"

Ok then I can get the library:

$ luarocks install luasocket

and require it:

$ lua
Lua 5.4.7  Copyright (C) 1994-2024 Lua.org, PUC-Rio
> require('socket')
table: 0x7fb742c07480	/Users/razzi/.local/lib/lua/share/lua/5.4/socket.lua

And this works, however I don't have history in the lua repl !!

Luckily https://github.com/hoelzro/lua-repl provides history !

Just needed to also install linenoise as well for it to have command line editing:

$ luarocks install linenoise
Installing https://luarocks.org/linenoise-0.9-1.rockspec

linenoise 0.9-1 depends on lua >= 5.1 (5.4-1 provided by VM: success)
env MACOSX_DEPLOYMENT_TARGET=11.0 gcc -O2 -fPIC -I/usr/local/opt/lua/include/lua5.4 -c linenoise.c -o linenoise.o
env MACOSX_DEPLOYMENT_TARGET=11.0 gcc -O2 -fPIC -I/usr/local/opt/lua/include/lua5.4 -c linenoiselib.c -o linenoiselib.o
env MACOSX_DEPLOYMENT_TARGET=11.0 gcc -O2 -fPIC -I/usr/local/opt/lua/include/lua5.4 -c encodings/utf8.c -o encodings/utf8.o
env MACOSX_DEPLOYMENT_TARGET=11.0 gcc  -bundle -undefined dynamic_lookup -all_load -o /var/folders/f5/gj618n116zd11_yml7p22dww0000gn/T/luarocks_build-linenoise-0.9-1-4513498/linenoise.so linenoise.o linenoiselib.o encodings/utf8.o
linenoise 0.9-1 is now installed in /Users/razzi/.local/lib/lua (license: MIT/X11)

Troublesomely lua calls like server:accept are blocking and are not interrupted by control-c so the server doesn't exit?? but c-\ does work as a force quit .. huh

https://stackoverflow.com/questions/50506099/why-is-lua-not-respecting-my-sigint-signal

socket = require('socket')
signal = require('posix.signal')

signal.signal(signal.SIGINT, function()
  os.exit()
end)

server = socket.bind('*', 0)

local ip, port = server:getsockname()

print('Got a socket on ' .. ip .. ':' .. port)

while 1 do
  local client = server:accept()
  local ip, port = client:getpeername()
  print('Client socket: ' .. ip .. ', port: ' .. port)

  client:settimeout(10)

  local line, err = client:receive()

  if not err then
    client:send('You said: ' .. line .. '\n')
  end

  client:close()
end

Another iteration, with "better" control-c handling (times out after 5 seconds)

socket = require('socket')
signal = require('posix.signal')

signal.signal(signal.SIGINT, function()
  os.exit()
end)

server = socket.bind('*', 0)

local ip, port = server:getsockname()

print('Got a socket on ' .. ip .. ':' .. port)

server:settimeout(5)

function server_loop()
  local client, err = server:accept()

  if err then
    print('Accept err: ' .. err)
    return
  end

  local ip, port = client:getpeername()
  print('Client socket: ' .. ip .. ', port: ' .. port)

  client:settimeout(5)

  local line, err = client:receive()

  if err then
    print('Receive err: ' .. err)
    return
  end

  print('Client said: ' .. line)
  client:send('You said: ' .. line .. '\n')

  client:close()
end

while 1 do
  server_loop()
end

server.close()

Also lua has no continue ?? haha so I had to wrap my loop body in a function. Not a bad refactor, could add as an answer to https://stackoverflow.com/questions/3524970/why-does-lua-have-no-continue-statement