1# Copyright (C) 1992 - 2002, 2003 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17# Please email any bugs, comments, and/or additions to this file to:
18# bug-dejagnu@gnu.org
19
20#
21# Setup an environment so we can execute library procs without DejaGnu
22#
23
24#
25# Create a default environment and start expect.
26#
27proc make_defaults_file { defs } {
28    global srcdir
29    global objdir
30    global subdir
31    global build_triplet
32    global host_triplet
33    global target_triplet
34    global target_os
35    global target_cpu
36
37    # We need to setup default values and a few default procs so we
38    # can execute library code without DejaGnu
39    set fd [open $defs w]
40    puts ${fd} "set tool foobar"
41    puts ${fd} "set srcdir ${srcdir}"
42    puts ${fd} "set objdir ${objdir}"
43    puts ${fd} "set subdir ${subdir}"
44    puts ${fd} "set build_triplet ${build_triplet}"
45    puts ${fd} "set host_triplet ${host_triplet}"
46    puts ${fd} "set target_triplet ${target_triplet}"
47    puts ${fd} "set target_os ${target_os}"
48    puts ${fd} "set target_cpu ${target_cpu}"
49    puts ${fd} "set tool foobar"
50    puts ${fd} "set testcnt 0"
51    puts ${fd} "set warncnt 0"
52    puts ${fd} "set errcnt 0"
53    puts ${fd} "set passcnt 0"
54    puts ${fd} "set xpasscnt 0"
55    puts ${fd} "set kpasscnt 0"
56    puts ${fd} "set failcnt 0"
57    puts ${fd} "set xfailcnt 0"
58    puts ${fd} "set kfailcnt 0"
59    puts ${fd} "set prms_id 0"
60    puts ${fd} "set bug_id 0"
61    puts ${fd} "set exit_status 0"
62    puts ${fd} "set untestedcnt 0"
63    puts ${fd} "set unresolvedcnt 0"
64    puts ${fd} "set unsupportedcnt 0"
65    puts ${fd} "set xfail_flag 0"
66    puts ${fd} "set xfail_prms 0"
67    puts ${fd} "set kfail_flag 0"
68    puts ${fd} "set kfail_prms 0"
69    puts ${fd} "set mail_logs 0"
70    puts ${fd} "set multipass_name 0"
71    catch "close $fd"
72}
73
74proc start_expect { } {
75    global spawn_id
76    global base_dir
77
78    # We need to setup default values and a few default procs so we
79    # can execute library code without DejaGnu
80    set defaults_file setval.tmp
81    make_defaults_file $defaults_file
82    set fd [open ${defaults_file} w]
83
84    # look for expect
85    if ![info exists EXPECT] {
86	set EXPECT [findfile $base_dir/../../expect/expect "$base_dir/../../expect/expect" expect]
87	verbose "EXPECT defaulting to $EXPECT" 2
88    }
89
90    #    catch close
91    #    catch wait
92
93    # Start expect runing
94    spawn "$EXPECT"
95    expect {
96	-re "expect.*> " {
97	    verbose "Started the child expect shell" 2
98	}
99	timeout {
100	    perror "Timed out starting the child expect shell."
101	    return -1
102	}
103    }
104
105    # Load the defaults file
106    exp_send "source ${defaults_file}\n"
107    expect {
108	"expect*> " {
109	    verbose "Loaded testing defaults file." 2
110	    return 1
111	}
112	timeout {
113	    perror "Couldn't load the testing defaults file."
114	    return -1
115	}
116    }
117}
118
119#
120# Stop the runing expect process
121#
122proc stop_expect { }  {
123    global spawn_id
124
125    # make expect exit
126    exp_send "exit\n"
127    catch "close -i $spawn_id"
128    catch "wait -i $spawn_id"
129}
130
131#
132# Load the library to test
133#
134proc load_test_lib { lib } {
135    global spawn_id
136    exp_send "source ${lib}\n"
137    expect {
138	"expect*> " {
139	    verbose "Testing ${lib}" 2
140	}
141	timeout {
142	    perror "Couldn't load the libraries to test"
143	    return -1
144	}
145    }
146}
147
148#
149# test a library proc that emits patterns
150#
151proc exp_test { cmd pattern msg } {
152    global spawn_id
153
154    exp_send "puts ACK ; $cmd ; puts NAK\r\n"
155    expect {
156	"puts ACK*puts NAK" {
157	    verbose "Got command echo" 3
158	}
159	timeout {
160	    warning "Never got command echo"
161	}
162    }
163
164    expect {
165	"ACK" {
166	    exp_continue
167	}
168	-re "\r\n1\r\n" {
169	    warning "$msg, 1 was returned"
170	    exp_continue
171	}
172	-re "\r\n0\r\n" {
173	    warning "$msg, 0 was returned"
174	    exp_continue
175	}
176	"$pattern" {
177	    pass "$msg"
178	}
179	timeout {
180	    fail "$msg"
181	}
182    }
183}
184
185# test a config proc that only returns a code
186# ex... config_test "isbuild $build_triplet" "pass" "fail" "isbuild, native"
187# args are:  command, true condition, false condition, message to print
188proc config_test { cmd true false msg } {
189    global spawn_id
190
191    set timeout 20
192    exp_send "puts ACK ; puts \[$cmd\] ; puts NAK\r\n"
193    expect {
194	"puts ACK*$cmd*puts NAK" {
195	    verbose "Got command echo" 3
196	}
197	timeout {
198	    warning "Never got command echo"
199	}
200    }
201
202    expect {
203	-re "Checking pattern*with*\[\r\n\]" {
204	    exp_continue
205	}
206	-re "\r\n1\r\n" {
207	    $true "$msg"
208	}
209	-re "\r\n0\r\n" {
210	    $false "$msg"
211	}
212	timeout {
213	    perror "$msg (timed out)"
214	}
215    }
216}
217