mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
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.
This commit is contained in:
parent
8e02cded17
commit
8a1b739ae6
1 changed files with 20 additions and 10 deletions
30
src/script.c
30
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"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue