From d1ff5f71424e1901809c0256b635201e56c0aea8 Mon Sep 17 00:00:00 2001 From: yabu76 Date: Sun, 5 Oct 2025 20:43:06 +0900 Subject: [PATCH] Add multipul patterns function to Lua API tio.expect() Lua regular expressions do not have an OR specification, so the current tio.expect() cannot wait until any of multiple patterns match. This will cause OR waiting behavior when a table of multiple regular expressions is passed as a pattern. If you pass a string as a pattern, it will treats a table has single regular expression. and this will change return values. 1st: table of captures if one of the patterns matched nil if any pattern unmatched and timeout. 2nd: whole input strings 3rd: index of the matched pattern. 0 if unmatched. For example, captures, whole, idx = tio.expect( {"\nOK", "\nERROR"} ) captures, whole, idx = tio.expect( "\nOK" ) --- src/script.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/script.c b/src/script.c index 76b5933..9cb5baa 100644 --- a/src/script.c +++ b/src/script.c @@ -58,15 +58,22 @@ static char script_init[] = "end\n" "tio.expect = function(pattern, timeout)\n" " local str = ''\n" +" if type(pattern) ~= 'table' then\n" +" pattern = { pattern }\n" +" end\n" " while true do\n" +" local idx, pat\n" " local c = tio.read(1, timeout)\n" " if c then\n" " str = str .. c\n" -" if string.match(str, pattern) then\n" -" return string.match(str, pattern)\n" +" for idx, pat in ipairs(pattern) do\n" +" local matched = { string.match(str, pat) }\n" +" if matched[1] then\n" +" return matched, str, idx\n" +" end\n" " end\n" " else\n" -" return nil, str\n" +" return nil, str, 0\n" " end\n" " end\n" "end\n"