LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-08-2010, 04:23 AM   #1
chekhov_neo
LQ Newbie
 
Registered: May 2010
Location: Bangalore
Distribution: Ubuntu
Posts: 22

Rep: Reputation: 2
tcl expect - problem with regular expression in interact mode


Hi,

I have something like the following in my expect script:

Code:
interact {
    -nobuffer -re {^s\r} {
        send_user "\nExiting\n"
        exit
}
I have put the "^" anchor to match only those pattern that does not have anything before "s"

e.g.

1.when I type "s" followed by "enter" key it should match.

2.if I type something like "chess" followed by "enter" key it shouldn't match.

Unfortunately, the second case is also being matched by the regular expression I have in my code.

Please help!
 
Old 12-08-2010, 08:47 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
I do not see the problem ...

Hi,

I created these two little scripts to verify your observation. However, it seems to work as expected:
exp.scr
Code:
#!/usr/bin/expect -f

spawn ./sh.scr
expect -exact "init: "
send "s\r"
interact {
	-nobuffer -re {^s\r} {
        send_user "\nExiting\n"
        exit
        }
}

exit
sh.scr
Code:
#!/bin/bash

echo "init: "
read ans
echo "your initial response is: '$initans'"

echo "type somthing: "
read ans
if [[ $ans = "s" ]];then
	echo "you typed '$ans' and this part of the program should NOT execute!"
else
	echo "you typed '$ans' instead of 's' and you should see this message."
fi
I used an initial 'send' to make sure that the problem is not the result of a previously entered 's\r' which still resides in the buffer.
Here is the output I get:
Code:
$ ./exp.scr 
spawn ./sh.scr
init: 
s
your initial response is: ''
type somthing: 
s
Exiting
$
The program exits correctly. And when I enter 'chess':
Code:
$ ./exp.scr 
spawn ./sh.scr
init: 
s
your initial response is: ''
type somthing: 
chess
you typed 'chess' instead of 's' and you should see this message.
$
The program does not exit, which is also the correct behaviour.

I assume that the missing second closing '}' is just a typo in your post. Otherwise, you might want to correct it.
If have misunderstood your requirement then maybe you should post some real input/output of your program and what you actually expected.
 
Old 12-09-2010, 01:27 AM   #3
chekhov_neo
LQ Newbie
 
Registered: May 2010
Location: Bangalore
Distribution: Ubuntu
Posts: 22

Original Poster
Rep: Reputation: 2
code output

Hi crts,

Thank you very much for your time. For me the problem occurred when I entered "chesss" and not "chess". I apologize for my mistake. And the missing "}" is a typo. I executed my code again but now with debugging info.

My code:

Code:
#!/usr/bin/expect -d

spawn bash
expect "#"
interact {
    -nobuffer -re {^s\r} {
        send_user "\nExiting\n"
        exit
        }
}
and I got the following output:

Code:
host1:~# ./test
expect version 5.43.0
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = ./test
set argc 0
set argv0 "./test"
set argv ""
executing commands from command file ./test
spawn bash
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {6472}

expect: does "" (spawn_id exp6) match glob pattern "#"? no
host1:~#
expect: does "host1:~# " (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "host1:~#"
defining key ^s\r, action \n        send_user "\nExiting\n"\n        exit\n
tty_raw_noecho: was raw = 0  echo = 1
spawn id exp0 sent <c>
spawn id exp6 sent <c>
cspawn id exp0 sent <h>
spawn id exp6 sent <h>
hspawn id exp0 sent <e>
spawn id exp6 sent <e>
espawn id exp0 sent <s>
spawn id exp6 sent <s>
sspawn id exp0 sent <s>
spawn id exp6 sent <s>
sspawn id exp0 sent <\r>
spawn id exp6 sent <\r\n>

spawn id exp6 sent <bash: chess: command not found\r\n>
bash: chess: command not found
spawn id exp6 sent <host1:~# >
host1:~# spawn id exp0 sent <c>
spawn id exp6 sent <c>
cspawn id exp0 sent <h>
spawn id exp6 sent <h>
hspawn id exp0 sent <e>
spawn id exp6 sent <e>
espawn id exp0 sent <s>
spawn id exp6 sent <s>
sspawn id exp0 sent <s>
spawn id exp6 sent <s>
sspawn id exp0 sent <s>
spawn id exp6 sent <s>
sspawn id exp0 sent <\r>
interact: set interact_out(0,string) "s\r"

Exiting
tty_set: raw = 5, echo = 0
host1:~#
So when I entered "chesss" the pattern matched and it exited. I'm wondering how did it match.
 
Old 12-09-2010, 02:55 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Firstly, it is important to understand that the characters '^' and '$' anchor the beginning and end of received data. They do NOT anchor the beginning and end of a line.

To understand the observed behavior let's assume there is a "RegExBuffer" for the incoming data. Since you have defined two characters in your RegEx this buffer will hold up to two bytes. This buffer will be emptied as soon as it does not match the RegEx. Let's have a closer look at what happens when you type chess:
Our RegEx is "s\r".
'c' is typed
Code:
RegExBuffer	[c][] --> RegEx is mismatched and the buffer is emptied

-> RegExBuffer	[][]
The above procedure is repeated for the characters 'h' and 'e'.
Now the first 's' is typed
Code:
RegExBuffer	[s][] --> RegEx is starting to match; the buffer is not emptied

-> RegExBuffer	[s][]
Now the second 's' is typed
Code:
RegExBuffer	[s][s] --> RegEx is mismatched and the buffer is emptied

-> RegExBuffer	[][]
If you hit 'return' at this point then the buffer would look like
Code:
RegExBuffer	[\r][] --> RegEx is mismatched and the buffer is emptied

-> RegExBuffer	[][]
If you do NOT hit 'return' after the second 's', but misspell chesss and type in a third 's' then this is how RegExBuffer looks like
Code:
RegExBuffer	[s][] --> RegEx is starting to match again; the buffer is not emptied

-> RegExBuffer	[s][]
If you hit 'return' after the third 's' then
Code:
RegExBuffer	[s][\r] --> This is a perfect match! The 'Exiting' branch is executed!
From this example it is hopefully clear that any odd number of consecutive 's' will match the RegEx, i.e. ches, chesss and also chesssss (that's 5 s) will trigger the 'Exiting' branch.
To further verify the mechanism I described above you can change your RegEx to "ss\r". Now the RegEx buffer will hold up to 3 characters. Hence, the 'Exiting' branch will be executed whenever you enter 2,5,8 ... consecutive 's'.
I am quite confident that there is a workaround for this problem; although not sure how it will look. It would be nice if you could post the solution here once you have it.

Last edited by crts; 12-09-2010 at 02:58 PM.
 
  


Reply

Tags
expect, regular expressions, tcl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird TCL Expect problem jenus Programming 1 12-02-2010 07:28 AM
[SOLVED] problem with regular expression ashok.g Programming 2 12-30-2009 06:05 AM
regular expression problem sancho1980 Programming 10 06-08-2009 07:26 AM
Using regular expression in expect script nik1984 Programming 1 08-28-2008 06:25 AM
having problem in writing regular expression in tcl mohtasham1983 Programming 1 10-29-2006 01:29 PM


All times are GMT -5. The time now is 07:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration