From 8a1b739ae60cdba41b7d553efd4aef6ca5c01a3e Mon Sep 17 00:00:00 2001 From: yabu76 Date: Sat, 15 Nov 2025 23:26:43 +0900 Subject: [PATCH] Add cleanup read to Lua API tio.expect[s] tio.expect[s]() performs pattern matching for each character received, but there are cases where the character reception speed exceeds the pattern matching speed, resulting in characters being missed. Add non-blocking reads to read all characters that have been received at that time. --- src/script.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/script.c b/src/script.c index e5b2f4f..74419ef 100644 --- a/src/script.c +++ b/src/script.c @@ -56,16 +56,22 @@ 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.expect = function(pattern, timeout)\n" " local str = ''\n" " while true do\n" -" local c = tio.read(1, timeout)\n" -" if c == nil then\n" -" return nil, str\n" +" local astr = tio.read(tio.EXPECT_CLEANUP_READ_SIZE, 0)\n" +" local c = nil\n" +" if astr == nil then\n" +" c = tio.read(1, timeout)\n" +" if c == nil then\n" +" return nil, str\n" +" end\n" " end\n" -" str = str .. c\n" -" if string.match(str, pattern) then\n" -" return string.match(str, pattern), str\n" +" str = table.concat{str, astr or '', c or ''}\n" +" local captured = { string.match(str, pattern) }\n" +" if #captured > 0 then\n" +" return table.unpack(captured), str\n" " end\n" " end\n" "end\n" @@ -75,11 +81,15 @@ static char script_init[] = " patterns = { patterns }\n" " end\n" " while true do\n" -" local c = tio.read(1, timeout)\n" -" if c == nil then\n" -" return nil, nil, str\n" +" local astr = tio.read(tio.EXPECT_CLEANUP_READ_SIZE, 0)\n" +" local c = nil\n" +" if astr == nil then\n" +" c = tio.read(1, timeout)\n" +" if c == nil then\n" +" return nil, nil, str\n" +" end\n" " end\n" -" str = str .. c\n" +" str = table.concat{str, astr or '', c or ''}\n" " for idx, pat in ipairs(patterns) do\n" " local captured = { string.match(str, pat) }\n" " if #captured > 0 then\n"