5 # * a program to run (1st arg)
6 # * some "command-line opts" ( -O<opt1> -O<opt2> ... )
7 # [default: anything on the cmd line this script doesn't recognise ]
8 # the first opt not starting w/ "-" is taken to be an input
9 # file and (if it exists) is grepped for "what's going on here"
11 # * a file to feed to stdin ( -i<file> ) [default: $dev_null ]
12 # * a "time" command to use (-t <cmd>).
13 # * a "start" line (-s <line>) - all preceeding lines of output
14 # * are ignored (from stdout).
15 # * a "start" pattern (-f <regexp>) - all preceeding lines of output
16 # * are deleted (from stdout).
17 # * an "end" pattern (-l <regexp>) - all later lines of output
18 # * are deleted (from stdout).
20 # * alternatively, a "-script <script>" argument says: run the
21 # named Bourne-shell script to do the test. It's passed the
22 # pgm-to-run as the one-and-only arg.
24 # Run the program with those options and that input, and check:
27 # * an expected exit status ( -x <val> ) [ default 0 ]
28 # * expected output on stdout ( -o1 <file> ) [ default $dev_null ]
29 # ( we'll accept one of several...)
30 # * expected output on stderr ( -o2 <file> ) [ default $dev_null ]
31 # ( we'll accept one of several...)
33 # (if the expected-output files' names end in .Z, then
34 # they are uncompressed before doing the comparison)
36 # (This is supposed to be a "prettier" replacement for runstdtest.)
39 die "$0 requires perl 5.0 or higher" unless $] >= 5.0;
41 ($Pgm = $0) =~ s|.*/||;
43 $tmpdir = &fromEnv('TMPDIR',"/tmp");
46 $diff = &fromEnv('CONTEXT_DIFF',"diff -c1");
47 $dev_null = &fromEnv('DEV_NULL',"/dev/null");
53 $PgmStdinFile = $dev_null;
54 $DefaultStdoutFile = "${tmpdir}/no_stdout$$"; # can't use $dev_null (e.g. Alphas)
55 $DefaultStderrFile = "${tmpdir}/no_stderr$$";
60 $PgmEndPat = 'WILLNAEMATCH'; # hack!
64 die "$Pgm: program to run not given as first argument\n" if $#ARGV < 0;
65 $ToRun = $ARGV[0]; shift(@ARGV);
66 # avoid picking up same-named thing from somewhere else on $PATH...
67 $ToRun = "./$ToRun" if $ToRun !~ /^\//;
69 arg: while ($_ = $ARGV[0]) {
72 /^-v$/ && do { $Verbose = 1; next arg; };
73 /^-O(.*)/ && do { push(@PgmArgs, &grab_arg_arg('-O',$1)); next arg; };
74 /^-i(.*)/ && do { $PgmStdinFile = &grab_arg_arg('-i',$1);
76 print STDERR "$Pgm: bogus -i input file: $PgmStdinFile\n"
77 if ! -f $PgmStdinFile;
79 /^-x(.*)/ && do { $PgmExitStatus = &grab_arg_arg('-x',$1);
81 print STDERR "$Pgm: bogus -x expected exit status: $PgmExitStatus\n"
82 if $PgmExitStatus !~ /^\d+$/;
84 /^-s(.*)/ && do { $PgmStartLine = &grab_arg_arg('-x',$1);
86 print STDERR "$Pgm: bogus -s start line: $PgmStartLine\n"
87 if $PgmStartLine !~ /^\d+$/;
89 /^-f(.*)/ && do { $PgmStartPat = &grab_arg_arg('-f',$1);
91 /^-l(.*)/ && do { $PgmEndPat = &grab_arg_arg('-l',$1);
93 /^-o1(.*)/ && do { $out_file = &grab_arg_arg('-o1',$1);
95 print STDERR "$Pgm: bogus -o1 expected-output file: $out_file\n"
97 push(@PgmStdoutFile, $out_file);
99 /^-o2(.*)/ && do { $out_file = &grab_arg_arg('-o2',$1);
101 print STDERR "$Pgm: bogus -o2 expected-stderr file: $out_file\n"
103 push(@PgmStderrFile, $out_file);
105 /^-script(.*)/ && do { $AltScript = &grab_arg_arg('-script',$1);
107 /^-t(.*)/ && do { $TimeCmd = &grab_arg_arg('-t', $1); next arg; };
109 # anything else is taken to be a pgm arg
114 # add on defaults if none specified
115 @PgmStdoutFile = ( $DefaultStdoutFile ) if $#PgmStdoutFile < 0;
116 @PgmStderrFile = ( $DefaultStderrFile ) if $#PgmStderrFile < 0;
118 # tidy up the pgm args:
119 # (1) look for the "first input file"
120 # and grep it for "interesting" comments (--!!! )
121 # (2) quote any args w/ whitespace in them.
123 foreach $a ( @PgmArgs ) {
124 if (! $grep_done && $a !~ /^-/ && -f $a) {
125 unless (open(ARG, $a)) {
126 print STDERR "Can't open $a: $!\n";
135 if ($a =~ /\s/ || $a =~ /'/) {
136 $a =~ s/'/\\'/g; # backslash the quotes;
137 $a =~ s/"/\\"/g; # backslash the quotes;
138 $a = "\"$a\""; # quote the arg
142 if ($AltScript ne '') {
144 $to_do = `cat $AltScript`;
145 # glue in pgm to run...
147 $to_do =~ s/^\$1 /$ToRun /;
148 &run_something($to_do);
150 # exec "$AltScript $ToRun";
151 # print STDERR "Failed to exec!!! $AltScript $ToRun\n";
155 # OK, so we're gonna do the normal thing...
157 $Script = <<EOSCRIPT;
158 CONTEXT_DIFF='/usr/bin/diff -C 1'
164 /bin/rm -f $DefaultStdoutFile $DefaultStderrFile
165 cat $dev_null > $DefaultStdoutFile
166 cat $dev_null > $DefaultStderrFile
167 $TimeCmd ${shell} -c \'$ToRun @PgmArgs < $PgmStdinFile 1> ${tmpdir}/runtest$$.1 2> ${tmpdir}/runtest$$.2\'
169 if [ \$progexit -ne $PgmExitStatus ]; then
170 echo $ToRun @PgmArgs \\< $PgmStdinFile
171 echo expected exit status $PgmExitStatus not seen \\; got \$progexit
174 # Pipe that filters out stuff we don't want to check
175 tail +$PgmStartLine ${tmpdir}/runtest$$.1 | test/after "$PgmStartPat" | test/before "$PgmEndPat" >${tmpdir}/runtest$$.3
177 for out_file in @PgmStdoutFile ; do
178 $diff \$out_file ${tmpdir}/runtest$$.3 > ${tmpdir}/diffs$$
179 if [ \$? -ne 0 ]; then
180 echo $ToRun @PgmArgs \\< $PgmStdinFile
181 echo expected stdout not matched by reality
182 cat ${tmpdir}/diffs$$
185 /bin/rm -f ${tmpdir}/diffs$$
188 for out_file in @PgmStderrFile ; do
189 $diff \$out_file ${tmpdir}/runtest$$.2 > ${tmpdir}/diffs$$
190 if [ \$? -ne 0 ]; then
191 echo $ToRun @PgmArgs \\< $PgmStdinFile
192 echo expected stderr not matched by reality
193 cat ${tmpdir}/diffs$$
196 /bin/rm -f ${tmpdir}/diffs$$
198 /bin/rm -f core $DefaultStdoutFile $DefaultStderrFile ${tmpdir}/runtest$$.1 ${tmpdir}/runtest$$.3 ${tmpdir}/runtest$$.2
202 &run_something($Script);
203 # print $Script if $Verbose;
204 # open(SH, "| ${shell}") || die "Can't open shell pipe\n";
211 local($varname,$default) = @_;
212 local($val) = $ENV{$varname};
213 $val = $default if $val eq "";
218 local($option, $rest_of_arg) = @_;
221 return($rest_of_arg);
222 } elsif ($#ARGV >= 0) {
223 local($temp) = $ARGV[0]; shift(@ARGV);
226 print STDERR "$Pgm: no argument following $option option\n";
232 local($str_to_do) = @_;
234 print STDERR "$str_to_do\n" if $Verbose;
236 local($return_val) = 0;
238 # On Windows NT, we have to build a file before we can interpret it.
239 local($scriptfile) = "./script$$";
240 open(FOO,">$scriptfile") || die "Can't create script $scriptfile";
241 print FOO $str_to_do;
244 system("sh $scriptfile");
246 system("rm $scriptfile");
248 if ($return_val != 0) {
249 #ToDo: this return-value mangling is wrong
250 # local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
251 # $die_msg .= " (program not found)" if $return_val == 255;
252 # $die_msg .= " ($!)" if $Verbose && $! != 0;
255 exit (($return_val == 0) ? 0 : 1);