mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
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:
parent
0da8731c0b
commit
c009ed755c
3 changed files with 33 additions and 11 deletions
|
|
@ -24,7 +24,8 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.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)
|
#define UNUSED(expr) do { (void)(expr); } while (0)
|
||||||
|
|
||||||
void delay(long ms);
|
void delay(long ms);
|
||||||
|
|
|
||||||
37
src/script.c
37
src/script.c
|
|
@ -58,11 +58,14 @@ static char script_init[] =
|
||||||
" local ri = arg.RI or -1\n"
|
" local ri = arg.RI or -1\n"
|
||||||
" tio.line_set(dtr, rts, cts, dsr, cd, ri)\n"
|
" tio.line_set(dtr, rts, cts, dsr, cd, ri)\n"
|
||||||
"end\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"
|
"tio.expect = function(pattern, timeout)\n"
|
||||||
" local str = ''\n"
|
" local str = ''\n"
|
||||||
" while true do\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"
|
" local c = nil\n"
|
||||||
" if astr == nil then\n"
|
" if astr == nil then\n"
|
||||||
" c = tio.read(1, timeout)\n"
|
" c = tio.read(1, timeout)\n"
|
||||||
|
|
@ -83,7 +86,7 @@ static char script_init[] =
|
||||||
" patterns = { patterns }\n"
|
" patterns = { patterns }\n"
|
||||||
" end\n"
|
" end\n"
|
||||||
" while true do\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"
|
" local c = nil\n"
|
||||||
" if astr == nil then\n"
|
" if astr == nil then\n"
|
||||||
" c = tio.read(1, timeout)\n"
|
" c = tio.read(1, timeout)\n"
|
||||||
|
|
@ -319,7 +322,7 @@ static int api_write(lua_State *L)
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
ret = write_poll(device_fd, string, len, WRITE_POLL_FOREVER);
|
ret = write_poll(device_fd, string, len, POLL_FOREVER);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return luaL_error(L, "%s", strerror(errno));
|
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)
|
static int api_read(lua_State *L)
|
||||||
{
|
{
|
||||||
int size = luaL_checkinteger(L, 1);
|
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)
|
if (device_fd == 0)
|
||||||
{
|
{
|
||||||
return luaL_error(L, "tty device not ready");
|
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_Buffer buffer;
|
||||||
luaL_buffinit(L, &buffer);
|
luaL_buffinit(L, &buffer);
|
||||||
|
|
||||||
|
|
@ -382,7 +394,7 @@ static int api_read(lua_State *L)
|
||||||
char *p = luaL_prepbuffer(&buffer);
|
char *p = luaL_prepbuffer(&buffer);
|
||||||
#endif
|
#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)
|
if (ret < 0)
|
||||||
return luaL_error(L, "%s", strerror(errno));
|
return luaL_error(L, "%s", strerror(errno));
|
||||||
|
|
||||||
|
|
@ -406,7 +418,7 @@ static int api_read(lua_State *L)
|
||||||
// lua: string = tio.readline(timeout)
|
// lua: string = tio.readline(timeout)
|
||||||
static int api_readline(lua_State *L)
|
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;
|
luaL_Buffer b;
|
||||||
char ch;
|
char ch;
|
||||||
|
|
||||||
|
|
@ -415,11 +427,20 @@ static int api_readline(lua_State *L)
|
||||||
return luaL_error(L, "tty device not ready");
|
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_buffinit(L, &b);
|
||||||
luaL_prepbuffer(&b);
|
luaL_prepbuffer(&b);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
int ret = read_poll(device_fd, &ch, 1, timeout);
|
int ret = read_poll(device_fd, &ch, 1, timeout_c);
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return luaL_error(L, "%s", strerror(errno));
|
return luaL_error(L, "%s", strerror(errno));
|
||||||
|
|
|
||||||
|
|
@ -302,7 +302,7 @@ void tty_sync(int fd)
|
||||||
|
|
||||||
while (remain > 0)
|
while (remain > 0)
|
||||||
{
|
{
|
||||||
count = write_poll(fd, cp, remain, WRITE_POLL_FOREVER);
|
count = write_poll(fd, cp, remain, POLL_FOREVER);
|
||||||
if (count < 0)
|
if (count < 0)
|
||||||
{
|
{
|
||||||
// Error
|
// Error
|
||||||
|
|
@ -360,7 +360,7 @@ static ssize_t tty_raw_write(int fd)
|
||||||
continue;
|
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)
|
if (retval < 0)
|
||||||
{
|
{
|
||||||
// Error
|
// Error
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue