Modified the Lua API timeout argument specification to be an extension of the v3.9 specification

Fix the v3.9-incompatible changes in 8e02cde ("Lua API Timeout
Specification Changes to Enable Non-Blocking Reads", November 15, 2025)
to make them compatible.

The timeout arguments for tio.expect/s(), tio.read(), and tio.readline()
have been changed as follows:
- The timeout is in milliseconds, and the default is 0, which means to
wait forever (as in v3.9).
- A negative value means to nowait.

A constant table (tio.C) has also been added,
defining tio.C.FOREVER to 0 and tio.C.NOWAIT to -1.
This commit is contained in:
yabu76 2026-02-01 11:56:29 +09:00
parent 0da8731c0b
commit c009ed755c
3 changed files with 33 additions and 11 deletions

View file

@ -24,7 +24,8 @@
#include <stdbool.h>
#include <stdio.h>
#define WRITE_POLL_FOREVER (-1)
#define POLL_NOWAIT (0)
#define POLL_FOREVER (-1)
#define UNUSED(expr) do { (void)(expr); } while (0)
void delay(long ms);

View file

@ -58,11 +58,14 @@ static char script_init[] =
" local ri = arg.RI or -1\n"
" tio.line_set(dtr, rts, cts, dsr, cd, ri)\n"
"end\n"
"tio.EXPECT_CLEANUP_READ_SIZE = 4096\n"
"tio.C = {}\n"
"tio.C.EXPECT_CLEANUP_READ_SIZE = 4096\n"
"tio.C.WAIT_FOREVER = 0\n"
"tio.C.NOWAIT = -1\n"
"tio.expect = function(pattern, timeout)\n"
" local str = ''\n"
" while true do\n"
" local astr = tio.read(tio.EXPECT_CLEANUP_READ_SIZE, 0)\n"
" local astr = tio.read(tio.C.EXPECT_CLEANUP_READ_SIZE, tio.C.NOWAIT)\n"
" local c = nil\n"
" if astr == nil then\n"
" c = tio.read(1, timeout)\n"
@ -83,7 +86,7 @@ static char script_init[] =
" patterns = { patterns }\n"
" end\n"
" while true do\n"
" local astr = tio.read(tio.EXPECT_CLEANUP_READ_SIZE, 0)\n"
" local astr = tio.read(tio.C.EXPECT_CLEANUP_READ_SIZE, tio.C.NOWAIT)\n"
" local c = nil\n"
" if astr == nil then\n"
" c = tio.read(1, timeout)\n"
@ -319,7 +322,7 @@ static int api_write(lua_State *L)
do
{
ret = write_poll(device_fd, string, len, WRITE_POLL_FOREVER);
ret = write_poll(device_fd, string, len, POLL_FOREVER);
if (ret < 0)
return luaL_error(L, "%s", strerror(errno));
@ -364,13 +367,22 @@ static int api_twrite(lua_State *L)
static int api_read(lua_State *L)
{
int size = luaL_checkinteger(L, 1);
int timeout = luaL_optinteger(L, 2, -1); // ms, negative value means forever.
int timeout = luaL_optinteger(L, 2, 0); // ms, zero value means forever, negative value means nowait.
if (device_fd == 0)
{
return luaL_error(L, "tty device not ready");
}
// For C API, the values for forever and nowait are swapped.
int timeout_c;
if (timeout > 0)
timeout_c = timeout;
else if (timeout == 0)
timeout_c = POLL_FOREVER;
else if (timeout < 0)
timeout_c = POLL_NOWAIT;
luaL_Buffer buffer;
luaL_buffinit(L, &buffer);
@ -382,7 +394,7 @@ static int api_read(lua_State *L)
char *p = luaL_prepbuffer(&buffer);
#endif
ssize_t ret = read_poll(device_fd, p, size, timeout);
ssize_t ret = read_poll(device_fd, p, size, timeout_c);
if (ret < 0)
return luaL_error(L, "%s", strerror(errno));
@ -406,7 +418,7 @@ static int api_read(lua_State *L)
// lua: string = tio.readline(timeout)
static int api_readline(lua_State *L)
{
int timeout = luaL_optinteger(L, 1, -1); // ms, negative value means forever.
int timeout = luaL_optinteger(L, 1, 0); // ms, zero value means forever, negative value means nowait.
luaL_Buffer b;
char ch;
@ -415,11 +427,20 @@ static int api_readline(lua_State *L)
return luaL_error(L, "tty device not ready");
}
// For C API, the values for forever and nowait are swapped.
int timeout_c;
if (timeout > 0)
timeout_c = timeout;
else if (timeout == 0)
timeout_c = POLL_FOREVER;
else if (timeout < 0)
timeout_c = POLL_NOWAIT;
luaL_buffinit(L, &b);
luaL_prepbuffer(&b);
while (true)
{
int ret = read_poll(device_fd, &ch, 1, timeout);
int ret = read_poll(device_fd, &ch, 1, timeout_c);
if (ret < 0)
return luaL_error(L, "%s", strerror(errno));

View file

@ -302,7 +302,7 @@ void tty_sync(int fd)
while (remain > 0)
{
count = write_poll(fd, cp, remain, WRITE_POLL_FOREVER);
count = write_poll(fd, cp, remain, POLL_FOREVER);
if (count < 0)
{
// Error
@ -360,7 +360,7 @@ static ssize_t tty_raw_write(int fd)
continue;
}
retval = write_poll(fd, &tty_buffer[i], 1, WRITE_POLL_FOREVER);
retval = write_poll(fd, &tty_buffer[i], 1, POLL_FOREVER);
if (retval < 0)
{
// Error