aboutsummaryrefslogtreecommitdiff
path: root/tests/expect.tcl
blob: 6cb03cda43c3b20c1cf5b807e40b77ae3292af53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# A simplified version of Tcl expect using a pseudo-tty pair
# This could be turned into a standard module, but for now
# it is just used in the test suite

# Example usage:
#
# set p [expect::spawn {cmd pipeline}]
#
# $p timeout 5
# $p send "a command\r"
# $p expect {
#	ab.*c {
#		script
#	}
#	d[a-z] {
#		script
#	}
#   EOF { ... }
#   TIMEOUT { ... }
# }
#
# [$p before] returns data before the match
# [$p after] returns data that matches the pattern
# [$p buf] returns any data after the match that has been read
# $p close
#
# $p tty ?...?
# $p kill ?SIGNAL?
if {![exists -command namespace]} {
	# Just enough to support [namespace current]
	proc namespace {args} {
		return ""
	}
}

proc expect::spawn {cmd} {
	lassign [socket pty] m s
	# By default, turn off echo so that we can see just the output, not the input
	$m tty echo 0
	$m buffering none
	$m ndelay 1
	try {
		lappend cmd <@$s >@$s &
		set pids [exec {*}$cmd]
		$s close
		# Create a unique global variable for vwait
		set donevar ::[ref "" expect]
		set $donevar 0
		set matchinfo {
			buf {}
		}

		return [namespace current]::[lambda {cmd args} {m pids {timeout 30} donevar matchinfo {debug 0}} {
			#puts "expect::spawn cmd=$cmd, matchinfo=$matchinfo"
			# Find our own name
			set self [lindex [info level 0] 0]

			switch -exact -- $cmd {
				dputs {
					if {$debug} {
						set escapes {13 \\r 10 \\n 9 \\t 92 \\\\}
						lassign $args str
						# convert non-printable chars to printable
						set formatted {}
						binary scan $str cu* chars
						foreach c $chars {
							if {[exists escapes($c)]} {
								append formatted $escapes($c)
							} elseif {$c < 32} {
								append formatted [format \\x%02x $c]
							} elseif {$c > 127} {
								append formatted [format \\u%04x $c]
							} else {
								append formatted [format %c $c]
							}
						}
						puts $formatted
					}
				}
				kill {
					# kill the process with the given signal
					foreach i $pids {
						kill {*}$args $i
					}
				}
				pid {
					# return the process pids
					return $pids
				}
				getfd - tty {
					# pass through to the pty file descriptor
					tailcall $m $cmd {*}$args
				}
				close {
					# close the file descriptor, wait for the child process to complete
					# and return the result
					$m close
					set retopts {}
					foreach p $pids {
						lassign [wait $p] status - rc
						if {$status eq "CHILDSTATUS"} {
							# Don't treat a non-zero return code as fatal here
							if {[llength $retopts] <= 1} {
								set retopts $rc
							}
							continue
						} else {
							set msg "child killed: received signal"
						}
						set retopts [list -code error -errorcode [list $status $p $rc] $msg]
					}
					rename $self ""

					return {*}$retopts
				}
				timeout - debug {
					# set or return the variable
					if {[llength $args]} {
						set $cmd [lindex $args 0]
					} else {
						return [set $cmd]
					}
				}
				send {
					$self dputs ">>> [lindex $args 0]"
					# send to the process
					$m puts -nonewline [lindex $args 0]
					$m flush
				}
				before - after - buf {
					# return the before, after and remaining data
					return $matchinfo($cmd)
				}
				handle {
					# Internal use only
					set args [lassign $args type]
					switch -- $type {
						timeout {
							$self dputs "\[TIMEOUT patterns=$matchinfo(patterns) buf=$matchinfo(buf)\]"
							# a timeout occurred
							set matchinfo(before) $matchinfo(buf)
							set matchinfo(buf) {}
							set matchinfo(matched_pattern) TIMEOUT
							incr $donevar
							return 1
						}
						eof {
							$self dputs "\[EOF\]"
							# EOF was reached
							set matchinfo(before) $matchinfo(buf)
							set matchinfo(buf) {}
							set matchinfo(matched_pattern) EOF
							incr $donevar
							return 1
						}
						data {
							# data was received
							lassign $args data
							$self dputs "<<< $data"
							append matchinfo(buf) $data
							foreach pattern $matchinfo(patterns) {
								set result [regexp -inline -indices $pattern $matchinfo(buf)]
								if {[llength $result]} {
									$self dputs "MATCH=\[$pattern\]"
									lassign [lindex $result 0] start end
									set matchinfo(before) [string range $matchinfo(buf) 0 $start-1]
									set matchinfo(after) [string range $matchinfo(buf) $start $end]
									set matchinfo(buf) [string range $matchinfo(buf) $end+1 end]

									# Got a match, stop
									set matchinfo(matched_pattern) $pattern
									incr $donevar
									return 1
								}
							}
						}
					}
					return 0
				}
				expect {
					# Takes a list of regex-pattern, script, ... where the last script can be missing
					if {[llength $args] % 2 == 1} {
						lappend args {}
					}

					# Stash all the state in the matchinfo dict
					# Keep matchinfo(buf)
					array set matchinfo {
						before {}
						after {}
						patterns {}
						matched_pattern {}
					}

					foreach {pattern script} $args {
						lappend matchinfo(patterns) $pattern
					}

					# Handle the case where there is buffered data
					# that matches the pattern
					if {[$self handle data {}] == 0} {
						$m readable [namespace current]::[lambda {} {m self} {
							try {
								set buf [$m read]
								if {$buf eq ""} {
									$self handle eof "EOF"
								} else {
									$self handle data $buf
								}
							} on error msg {
								$self handle eof $msg
							}
						}]
						set matchinfo(afterid) [after $($timeout * 1e3) [list $self handle timeout]]

						vwait $donevar

						after cancel $matchinfo(afterid)
					}

					# Now invoke the matching script
					if {[dict exists $args $matchinfo(matched_pattern)]} {
						uplevel 1 [dict get $args $matchinfo(matched_pattern)]
					}
					# And return the data that matched the pattern
					# (is $matchinfo(before) more generally useful?)
					return $matchinfo(after)
				}
			}
		}]
	} on error {error opts} {
		catch {$m close}
		catch {$s close}
		return -code error $error
	}
}