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" )
This commit is contained in:
yabu76 2025-10-05 20:43:06 +09:00
parent 58aae5511f
commit d1ff5f7142

View file

@ -58,15 +58,22 @@ static char script_init[] =
"end\n" "end\n"
"tio.expect = function(pattern, timeout)\n" "tio.expect = function(pattern, timeout)\n"
" local str = ''\n" " local str = ''\n"
" if type(pattern) ~= 'table' then\n"
" pattern = { pattern }\n"
" end\n"
" while true do\n" " while true do\n"
" local idx, pat\n"
" local c = tio.read(1, timeout)\n" " local c = tio.read(1, timeout)\n"
" if c then\n" " if c then\n"
" str = str .. c\n" " str = str .. c\n"
" if string.match(str, pattern) then\n" " for idx, pat in ipairs(pattern) do\n"
" return string.match(str, pattern)\n" " local matched = { string.match(str, pat) }\n"
" if matched[1] then\n"
" return matched, str, idx\n"
" end\n"
" end\n" " end\n"
" else\n" " else\n"
" return nil, str\n" " return nil, str, 0\n"
" end\n" " end\n"
" end\n" " end\n"
"end\n" "end\n"