diff --git a/src/misc.h b/src/misc.h index c064bde..6edbb9c 100644 --- a/src/misc.h +++ b/src/misc.h @@ -24,7 +24,8 @@ #include #include -#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); diff --git a/src/script.c b/src/script.c index 45e9fea..2bf4a66 100644 --- a/src/script.c +++ b/src/script.c @@ -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)); diff --git a/src/tty.c b/src/tty.c index 088a876..4c3d40c 100644 --- a/src/tty.c +++ b/src/tty.c @@ -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