Add multipul patterns function tio.expects() to Lua API

Add tio.expects() which has a table of patterns in the arguments to
support "OR" pattern waiting.

The return values of tio.expects() are:
1st: index of the matched pattern in the table. nil if unmatched.
2nd: a table of captures if one of the patterns are matched.
     nil if any of the patterns are unmatched and timeout.
3rd: all received strings in tio.expects() to treat the strings after
     match.

Add all received string to tio.expect()'s return values too.

For example,
  idx, captures, all_received = tio.expects( {"\nOK", "\nERROR"}, 1000 )
  captures, all_received = tio.expect( "\nPROMPT>", 1000 )
This commit is contained in:
yabu76 2025-11-02 10:15:16 +09:00
parent b5944275d3
commit 56c378f5f9

View file

@ -60,14 +60,32 @@ static char script_init[] =
" local str = ''\n"
" while true do\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"
" end\n"
" else\n"
" if c == nil then\n"
" return nil, str\n"
" end\n"
" str = str .. c\n"
" if string.match(str, pattern) then\n"
" return string.match(str, pattern), str\n"
" end\n"
" end\n"
"end\n"
"tio.expects = function(patterns, timeout)\n"
" local str = ''\n"
" if type(patterns) ~= 'table' then\n"
" 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"
" end\n"
" str = str .. c\n"
" for idx, pat in ipairs(patterns) do\n"
" local captured = { string.match(str, pat) }\n"
" if #captured > 0 then\n"
" return idx, captured, str\n"
" end\n"
" end\n"
" end\n"
"end\n"
"tio.alwaysecho = true\n"