79254443

Date: 2024-12-05 11:24:03
Score: 0.5
Natty:
Report link

I wrote the same example script in native tcl expect, and there it behaves differently (and as intended).

The tcl expect example script:

#!/usr/bin/expect -f

exp_internal 1
log_user 0

set pattern {[\r\n]+[^\r\n<]+[#>%] ?$}

# spawn process (here: open serial console via picocom):
spawn picocom --baud 9600 --flow n /dev/ttyS8

# wait for output of spawned process to settle down:
expect -timeout 2 -re {.+} exp_continue

# send 2x newline ("enter"):
send "\n\n"
expect -re $pattern

send_user "Anything left in the buffer?\n"
expect -timeout 2 -re {.+} exp_continue

send "\x01\x11"
expect "Thanks for using picocom"
close

Relevant debug output:

send: sending "\n\n" to { exp4 }
Gate keeper glob pattern for '[\r\n]+[^\r\n<]+[#>%] ?$' is ''. Not usable, disabling the performance booster.

expect: does "" (spawn_id exp4) match regular expression "[\r\n]+[^\r\n<]+[#>%] ?$"? (No Gate, RE only) gate=yes re=no

expect: does "\r\n\r\n{mas" (spawn_id exp4) match regular expression "[\r\n]+[^\r\n<]+[#>%] ?$"? (No Gate, RE only) gate=yes re=no

expect: does "\r\n\r\n{master:0}\r\n" (spawn_id exp4) match regular expression "[\r\n]+[^\r\n<]+[#>%] ?$"? (No Gate, RE only) gate=yes re=no

expect: does "\r\n\r\n{master:0}\r\nroot@rou" (spawn_id exp4) match regular expression "[\r\n]+[^\r\n<]+[#>%] ?$"? (No Gate, RE only) gate=yes re=no

expect: does "\r\n\r\n{master:0}\r\nroot@router> \r\n\r" (spawn_id exp4) match regular expression "[\r\n]+[^\r\n<]+[#>%] ?$"? (No Gate, RE only) gate=yes re=no

expect: does "\r\n\r\n{master:0}\r\nroot@router> \r\n\r\n{master" (spawn_id exp4) match regular expression "[\r\n]+[^\r\n<]+[#>%] ?$"? (No Gate, RE only) gate=yes re=no

expect: does "\r\n\r\n{master:0}\r\nroot@router> \r\n\r\n{master:0}\r\nroo" (spawn_id exp4) match regular expression "[\r\n]+[^\r\n<]+[#>%] ?$"? (No Gate, RE only) gate=yes re=no

expect: does "\r\n\r\n{master:0}\r\nroot@router> \r\n\r\n{master:0}\r\nroot@router" (spawn_id exp4) match regular expression "[\r\n]+[^\r\n<]+[#>%] ?$"? (No Gate, RE only) gate=yes re=no

expect: does "\r\n\r\n{master:0}\r\nroot@router> \r\n\r\n{master:0}\r\nroot@router> " (spawn_id exp4) match regular expression "[\r\n]+[^\r\n<]+[#>%] ?$"? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "\r\nroot@router> "
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "\r\n\r\n{master:0}\r\nroot@router> \r\n\r\n{master:0}\r\nroot@router> "
Anything left in the buffer?
Gate keeper glob pattern for '.+' is ''. Not usable, disabling the performance booster.

expect: does "" (spawn_id exp4) match regular expression ".+"? (No Gate, RE only) gate=yes re=no
expect: timed out

The expect_out(buffer) is the equivalent of the concatinated "Before" and "match" in the perl's version debug output; in contrast to the perl version, the match result contains everything up to the anchor at end-of-string, and not only the match up the first end-of-line; the rest of the buffer is empty, thus the timeout of the following expect .+ call.

I think my initial problem is a bug in the perl expect I currently use.

~$ dpkg -l|egrep expect
ii  expect                        5.45.4-2+b1                    i386         Automates interactive applications
ii  libexpect-perl                1.35-2                         all          Perl Expect interface
ii  tcl-expect:i386               5.45.4-2+b1                    i386         Automates interactive applications (Tcl package)
~$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 12 (bookworm)
Release:        12
Codename:       bookworm
~$
Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Karl Hungus