Improve output.
[pintos-anon] / grading / userprog / run-tests
1 #! /usr/bin/perl
2
3 use warnings;
4 use strict;
5 use POSIX;
6 use Algorithm::Diff;
7 use Getopt::Long;
8
9 our ($VERBOSE) = 0;     # Verbosity of output
10 our (@TESTS);           # Tests to run.
11 my ($clean) = 0;
12 my ($grade) = 0;
13
14 GetOptions ("v|verbose+" => \$VERBOSE,
15             "h|help" => sub { usage (0) },
16             "t|test=s" => \@TESTS,
17             "c|clean" => \$clean,
18             "g|grade" => \$grade)
19     or die "Malformed command line; use --help for help.\n";
20 die "Non-option argument not supported; use --help for help.\n"
21     if @ARGV > 0;
22
23 sub usage {
24     my ($exitcode) = @_;
25     print "run-tests, for grading Pintos multiprogramming projects.\n\n";
26     print "Invoke from a directory containing a student tarball named by\n";
27     print "the submit script, e.g. username.Oct.12.04.20.04.09.tar.gz.\n";
28     print "In normal usage, no options are needed.\n\n";
29     print "Output is produced in tests.out and details.out.\n\n";
30     print "Options:\n";
31     print "  -c, --clean     Remove old output files before starting\n";
32     print "  -t, --test=TEST Execute TEST only (allowed multiple times)\n";
33     print "  -g, --grade     Instead of running tests, compose grade.out\n";
34     print "  -v, --verbose   Print commands before executing them\n";
35     print "  -h, --help      Print this help message\n";
36     exit $exitcode;
37 }
38
39 # Default set of tests.
40 @TESTS = qw (args-argc args-argv0 args-argvn args-single args-multiple
41              args-dbl-space
42              sc-bad-sp sc-bad-arg sc-boundary
43              halt exit
44              create-normal create-empty create-null create-bad-ptr 
45              create-long create-exists create-bound
46              open-normal open-missing open-boundary open-empty open-null
47              open-bad-ptr open-twice
48              close-normal close-twice close-stdin close-stdout close-bad-fd
49              read-normal read-bad-ptr read-boundary read-zero read-stdout
50              read-bad-fd
51              write-normal write-bad-ptr write-boundary write-zero write-stdin
52              write-bad-fd
53              exec-once exec-arg exec-multiple exec-missing exec-bad-ptr
54              join-simple join-twice join-killed join-bad-pid
55              multi-recurse multi-oom multi-child-fd
56              ) unless @TESTS > 0;
57
58 our (%args);
59 for my $key ('args-argc', 'args-argv0', 'args-argvn', 'args-multiple') {
60     $args{$key} = "some arguments for you!";
61 }
62 $args{'args-single'} = "onearg";
63 $args{'args-dbl-space'} = "two  args";
64 $args{'multi-recurse'} = "15";
65 $args{'multi-oom'} = "0";
66
67 # Handle final grade mode.
68 if ($grade) {
69     open (OUT, ">grade.out") or die "grade.out: create: $!\n";
70
71     open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
72     while (<GRADE>) {
73         last if /^\s*$/;
74         print OUT;
75     }
76     close (GRADE);
77     
78     my (@tests) = snarf ("tests.out");
79     my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
80
81     my (@review) = snarf ("review.txt");
82     my ($part_lost) = (0, 0);
83     for (my ($i) = $#review; $i >= 0; $i--) {
84         local ($_) = $review[$i];
85         if (my ($loss) = /^\s*([-+]\d+)/) {
86             $part_lost += $loss;
87         } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
88             my ($got) = $out_of + $part_lost;
89             $got = 0 if $got < 0;
90             $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
91             $part_lost = 0;
92
93             $p_got += $got;
94             $p_pos += $out_of;
95         }
96     }
97     die "Lost points outside a section\n" if $part_lost;
98
99     for (my ($i) = 1; $i <= $#review; $i++) {
100         if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
101             $review[$i] = '-' x (length ($review[$i - 1]));
102         }
103     }
104
105     print OUT "\nOVERALL SCORE\n";
106     print OUT "-------------\n";
107     print OUT "$p_got points out of $p_pos total\n\n";
108
109     print OUT map ("$_\n", @tests), "\n";
110     print OUT map ("$_\n", @review), "\n";
111
112     print OUT "DETAILS\n";
113     print OUT "-------\n\n";
114     print OUT map ("$_\n", snarf ("details.out"));
115
116     exit 0;
117 }
118
119 # Find the directory that contains the grading files.
120 our ($GRADES_DIR);
121 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
122 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
123
124 if ($clean) {
125     # Verify that we're roughly in the correct directory
126     # before we go blasting away files.
127     choose_tarball ();
128
129     xsystem ("rm -rf output pintos", VERBOSE => 1);
130     xsystem ("rm -f details.out tests.out", VERBOSE => 1);
131 }
132
133 # Create output directory, if it doesn't already exist.
134 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
135
136 # Extract submission.
137 extract_tarball () if ! -d "pintos";
138
139 # Compile submission.
140 compile ();
141
142 # Verify that the proper directory was submitted.
143 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
144
145 # Run and grade the tests.
146 our $test;
147 our %result;
148 our %details;
149 our %extra;
150 for $test (@TESTS) {
151     print "$test: ";
152     my ($result) = run_test ($test);
153     if ($result eq 'ok') {
154         $result = grade_test ($test);
155         $result =~ s/\n$//;
156     }
157     print "$result";
158     print " - with warnings" if $result eq 'ok' && defined $details{$test};
159     print "\n";
160     
161     $result{$test} = $result;
162 }
163
164 # Write output.
165 write_grades ();
166 write_details ();
167 \f
168 sub choose_tarball {
169     my (@tarballs)
170         = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
171                 glob ("*.tar.gz"));
172     die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
173
174     # Sort tarballs in reverse order by time.
175     @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
176
177     print "Multiple tarballs: choosing $tarballs[0]\n"
178         if scalar (@tarballs) > 1;
179     return $tarballs[0];
180 }
181
182 sub extract_tarball {
183     my ($tarball) = choose_tarball ();
184
185     mkdir "pintos" or die "pintos: mkdir: $!\n";
186     mkdir "pintos/src" or die "pintos: mkdir: $!\n";
187
188     print "Extracting $tarball...\n";
189     xsystem ("cd pintos/src && tar xzf ../../$tarball",
190              DIE => "extraction failed\n");
191
192     if (-e "fixme.sh") {
193         print "Running fixme.sh...\n";
194         xsystem ("sh -e fixme.sh", DIE => "fix script failed\n");
195     }
196
197     print "Patching...\n";
198     xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
199              LOG => "patch",
200              DIE => "patch failed\n");
201     xsystem ("patch -fs pintos/src/lib/kernel/bitmap.c "
202              . "< $GRADES_DIR/random.diff",
203              LOG => "patch",
204              DIE => "patch failed\n");
205
206     open (CONSTANTS, ">pintos/src/constants.h")
207         or die "constants.h: create: $!\n";
208     print CONSTANTS "#define THREAD_JOIN_IMPLEMENTED 1\n";
209     close CONSTANTS;
210 }
211
212 sub ext_mdyHMS {
213     my ($s) = @_;
214     my ($ms, $d, $y, $H, $M, $S) =
215         $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
216         or die;
217     my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
218         or die;
219     return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
220 }
221 \f
222 sub test_source {
223     my ($test) = @_;
224     my ($src) = "$GRADES_DIR/$test.c";
225     -e $src or die "$src: stat: $!\n";
226     return $src;
227 }
228
229 sub test_constants {
230    my ($defines) = "";
231    return $defines;
232  }
233
234 sub run_test {
235     my ($test) = @_;
236
237     # Reuse older results if any.
238     if (open (DONE, "<output/$test/done")) {
239         my ($status);
240         $status = <DONE>;
241         chomp $status;
242         close (DONE);
243         return $status;
244     }
245
246     # Really run the test.
247     my ($status) = really_run_test ($test);
248
249     # Save the results for later.
250     open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
251     print DONE "$status\n";
252     close (DONE);
253
254     return $status;
255 }
256
257 sub compile {
258     print "Compiling...\n";
259     xsystem ("cd pintos/src/userprog && make", LOG => "make")
260         or return "compile error";
261 }
262
263 sub really_run_test {
264     # Need to run it.
265     # If there's residue from an earlier test, move it to .old.
266     # If there's already a .old, delete it.
267     xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
268     rename "output/$test", "output/$test.old" or die "rename: $!\n"
269         if -d "output/$test";
270
271     # Make output directory.
272     mkdir "output/$test";
273     xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
274              DIE => "cp failed\n");
275
276     # Run.
277     my ($timeout) = 10;
278     $timeout = 600 if $test =~ /^multi-/;
279     my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
280     xsystem ("pintos "
281              . "--os-disk=pintos/src/userprog/build/os.dsk "
282              . "--fs-disk=output/$test/fs.dsk "
283              . "-v run -q -ex \"$test$testargs\"",
284              LOG => "$test/run",
285              TIMEOUT => $timeout)
286         or return "Bochs error";
287     
288     return "ok";
289 }
290
291 sub grade_test {
292     my ($test) = @_;
293
294     my (@output) = snarf ("output/$test/run.out");
295
296     my ($grade_func) = "grade_$test";
297     $grade_func =~ s/-/_/g;
298     if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
299         eval {
300             verify_common (@output);
301             compare_output ("$GRADES_DIR/$test.exp", @output);
302         }
303     } else {
304         eval "$grade_func (\@output)";
305     }
306     if ($@) {
307         die $@ if $@ =~ /at \S+ line \d+$/;
308         return $@;
309     }
310     return "ok";
311 }
312 \f
313 sub grade_write_normal {
314     my (@output) = @_;
315     verify_common (@output);
316     compare_output ("$GRADES_DIR/write-normal.exp", @output);
317     my ($test_txt) = "output/$test/test.txt";
318     get_file ("test.txt", $test_txt) if ! -e $test_txt;
319
320     my (@actual) = snarf ($test_txt);
321     my (@expected) = snarf ("$GRADES_DIR/sample.txt");
322
323     my ($eq);
324     if ($#actual == $#expected) {
325         $eq = 1;
326         for my $i (0...$#actual) {
327             $eq = 0 if $actual[$i] ne $expected[$i];
328         }
329     } else {
330         $eq = 0;
331     }
332     if (!$eq) {
333         my ($details);
334         $details = "Expected file content:\n";
335         $details .= join ('', map ("  $_\n", @expected));
336         $details .= "Actual file content:\n";
337         $details .= join ('', map ("  $_\n", @actual));
338         $extra{$test} = $details;
339
340         die "File written didn't have expected content.\n";
341     }
342 }
343
344 sub grade_multi_oom {
345     my (@output) = @_;
346     verify_common (@output);
347
348     @output = fix_exit_codes (get_core_output (@output));
349     my ($n) = 0;
350     while (my ($m) = $output[0] =~ /^\(multi-oom\) begin (\d+)$/) {
351         die "Child process $m started out of order.\n" if $m != $n;
352         $n = $m + 1;
353         shift @output;
354     }
355     die "Only $n child process(es) started.\n" if $n < 15;
356
357     # There could be a death notice for a process that didn't get
358     # fully loaded, and/or notices from the loader.
359     while (@output > 0
360            && ($output[0] =~ /^multi-oom: exit\(-1\)$/
361                || $output[0] =~ /^load: /)) {
362         shift @output;
363     }
364
365     while (--$n >= 0) {
366         die "Output ended unexpectedly before process $n finished.\n"
367             if @output < 2;
368
369         local ($_);
370         chomp ($_ = shift @output);
371         die "Found '$_' expecting 'end' message.\n" if !/^\(multi-oom\) end/;
372         die "Child process $n ended out of order.\n"
373             if !/^\(multi-oom\) end $n$/;
374
375         chomp ($_ = shift @output);
376         die "Kernel didn't print proper exit message for process $n.\n"
377             if !/^multi-oom: exit\($n\)$/;
378     }
379     die "Spurious output at end: '$output[0]'.\n" if @output;
380 }
381
382 sub get_file {
383     my ($guest_fn, $host_fn) = @_;
384     xsystem ("pintos "
385              . "--os-disk=pintos/src/userprog/build/os.dsk "
386              . "--fs-disk=output/$test/fs.dsk "
387              . "-v get $guest_fn $host_fn",
388              LOG => "$test/get-$guest_fn",
389              TIMEOUT => 10)
390         or die "get $guest_fn failed\n";
391 }
392
393 \f
394 sub verify_common {
395     my (@output) = @_;
396
397     my (@assertion) = grep (/PANIC/, @output);
398     if (@assertion != 0) {
399         my ($details) = "Kernel panic:\n  $assertion[0]\n";
400
401         my (@stack_line) = grep (/Call stack:/, @output);
402         if (@stack_line != 0) {
403             $details .= "  $stack_line[0]\n\n";
404             $details .= "Translation of backtrace:\n";
405             my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
406
407             my ($A2L);
408             if (`uname -m`
409                 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
410                 $A2L = "addr2line";
411             } else {
412                 $A2L = "i386-elf-addr2line";
413             }
414             open (A2L, "$A2L -fe pintos/src/userprog/build/kernel.o @addrs|");
415             for (;;) {
416                 my ($function, $line);
417                 last unless defined ($function = <A2L>);
418                 $line = <A2L>;
419                 chomp $function;
420                 chomp $line;
421                 $details .= "  $function ($line)\n";
422             }
423         }
424         $extra{$test} = $details;
425         die "Kernel panic.  Details at end of file.\n"
426     }
427
428     if (grep (/Pintos booting/, @output) > 1) {
429         my ($details);
430
431         $details = "Pintos spontaneously rebooted during this test.\n";
432         $details .= "This is most often due to unhandled page faults.\n";
433         $details .= "Here's the output from the initial boot through the\n";
434         $details .= "first reboot:\n\n";
435
436         my ($i) = 0;
437         local ($_);
438         for (@output) {
439             $details .= "  $_\n";
440             last if /Pintos booting/ && ++$i > 1;
441         }
442         $details{$test} = $details;
443         die "Triple-fault caused spontaneous reboot(s).  "
444             . "Details at end of file.\n";
445     }
446
447     die "No output at all\n" if @output == 0;
448     die "Didn't start up properly: no \"Pintos booting\" startup message\n"
449         if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
450     die "Didn't start up properly: no \"Boot complete\" startup message\n"
451         if !grep (/Boot complete/, @output);
452     die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
453         if !grep (/Timer: \d+ ticks/, @output);
454     die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
455         if !grep (/Powering off/, @output);
456 }
457
458 # Get @output without header or trailer.
459 sub get_core_output {
460     my (@output) = @_;
461
462     our ($test);
463     my ($first);
464     for ($first = 0; $first <= $#output; $first++) {
465         $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
466     }
467
468     my ($last);
469     for ($last = $#output; $last >= 0; $last--) {
470         $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
471     }
472
473     if ($last < $first) {
474         my ($no_first) = $first > $#output;
475         my ($no_last) = $last < $#output;
476         die "Couldn't locate output.\n";
477     }
478
479     return @output[$first ... $last];
480 }
481
482 sub fix_exit_codes {
483     my (@output) = @_;
484
485     # Fix up lines that look like exit codes.
486     # Exit codes are supposed to be printed in the form "process: exit(code)"
487     # but people get unfortunately creative with it.
488     for my $i (0...$#output) {
489         local ($_) = $output[$i];
490         
491         my ($process, $code);
492         if ((($process, $code) = /^([-a-zA-Z0-9 ]+):.*[ \(](-?\d+)\b\)?$/)
493             || (($process, $code) = /^([-a-zA-Z0-9 ]+) exit\((-?\d+)\)$/)
494             || (($process, $code)
495                 = /^([-a-zA-Z0-9 ]+) \(.*\): exit\((-?\d+)\)$/)
496             || (($process, $code) = /^([-a-zA-Z0-9 ]+):\( (-?\d+) \) $/)
497 ) {
498             $process = substr ($process, 0, 15);
499             $process =~ s/\s.*//;
500             $output[$i] = "$process: exit($code)\n";
501         }
502     }
503
504     return @output;
505 }
506
507 sub compare_output {
508     my ($exp, @actual) = @_;
509     @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
510
511     my ($details) = "";
512     $details .= "$test actual output:\n";
513     $details .= join ('', map ("  $_", @actual));
514
515     my (@exp) = map ("$_\n", snarf ($exp));
516
517     my ($fuzzy_match) = 0;
518     while (@exp != 0) {
519         my (@expected);
520         while (@exp != 0) {
521             my ($s) = shift (@exp);
522             last if $s eq "--OR--\n";
523             push (@expected, $s);
524         }
525
526         $details .= "\n$test acceptable output:\n";
527         $details .= join ('', map ("  $_", @expected));
528
529         # Check whether they're the same.
530         if ($#actual == $#expected) {
531             my ($eq) = 1;
532             for (my ($i) = 0; $i <= $#expected; $i++) {
533                 $eq = 0 if $actual[$i] ne $expected[$i];
534             }
535             return if $eq;
536         }
537
538         # They differ.  Output a diff.
539         my (@diff) = "";
540         my ($d) = Algorithm::Diff->new (\@expected, \@actual);
541         my ($not_fuzzy_match) = 0;
542         while ($d->Next ()) {
543             my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
544             if ($d->Same ()) {
545                 push (@diff, map ("  $_", $d->Items (1)));
546             } else {
547                 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
548                 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
549                 if ($d->Items (1)
550                     || grep (/\($test\)|exit\(-?\d+\)/, $d->Items (2))) {
551                     $not_fuzzy_match = 1;
552                 }
553             }
554         }
555         $fuzzy_match = 1 if !$not_fuzzy_match;
556
557         $details .= "Differences in `diff -u' format:\n";
558         $details .= join ('', @diff);
559         $details .= "(This is considered a `fuzzy match'.)\n"
560             if !$not_fuzzy_match;
561     }
562
563     if ($fuzzy_match) {
564         $details =
565             "This test passed, but with extra, unexpected output.\n"
566             . "Please inspect your code to make sure that it does not\n"
567             . "produce output other than as specified in the project\n"
568             . "description.\n\n"
569             . "$details";
570     } else {
571         $details =
572             "This test failed because its output did not match any\n"
573             . "of the acceptable form(s).\n\n"
574             . "$details";
575     }
576
577     $details{$test} = $details;
578     die "Output differs from expected.  Details at end of file.\n"
579         unless $fuzzy_match;
580 }
581 \f
582 sub write_grades {
583     my (@summary) = snarf ("$GRADES_DIR/tests.txt");
584
585     my ($ploss) = 0;
586     my ($tloss) = 0;
587     my ($total) = 0;
588     my ($warnings) = 0;
589     for (my ($i) = 0; $i <= $#summary; $i++) {
590         local ($_) = $summary[$i];
591         if (my ($loss, $test) = /^  -(\d+) ([-a-zA-Z0-9]+):/) {
592             my ($result) = $result{$test} || "Not tested.";
593
594             if ($result eq 'ok') {
595                 if (!defined $details{$test}) {
596                     # Test successful and no warnings.
597                     splice (@summary, $i, 1);
598                     $i--;
599                 } else {
600                     # Test successful with warnings.
601                     s/-(\d+) //;
602                     $summary[$i] = $_;
603                     splice (@summary, $i + 1, 0,
604                             "    Test passed with warnings.  "
605                             . "Details at end of file.");
606                     $warnings++;
607                 } 
608             } else {
609                 $ploss += $loss;
610                 $tloss += $loss;
611                 splice (@summary, $i + 1, 0,
612                         map ("     $_", split ("\n", $result)));
613             }
614         } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
615             $total += $ptotal;
616             $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
617             splice (@summary, $i, 0, "  All tests passed.")
618                 if $ploss == 0 && !$warnings;
619             $ploss = 0;
620             $warnings = 0;
621             $i++;
622         }
623     }
624     my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
625     $summary[0] =~ s/\[\[total\]\]/$ts/;
626
627     open (SUMMARY, ">tests.out");
628     print SUMMARY map ("$_\n", @summary);
629     close (SUMMARY);
630 }
631
632 sub write_details {
633     open (DETAILS, ">details.out");
634     my ($n) = 0;
635     for my $test (@TESTS) {
636         next if $result{$test} eq 'ok' && !defined $details{$test};
637         
638         my ($details) = $details{$test};
639         next if !defined ($details) && ! -e "output/$test/run.out";
640
641         my ($banner);
642         if ($result{$test} ne 'ok') {
643             $banner = "$test failure details"; 
644         } else {
645             $banner = "$test warnings";
646         }
647
648         print DETAILS "\n" if $n++;
649         print DETAILS "--- $banner ", '-' x (50 - length ($banner));
650         print DETAILS "\n\n";
651
652         if (!defined $details) {
653             $details = "Output:\n\n" . snarf ("output/$test/run.out");
654         }
655         print DETAILS $details;
656
657         print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
658             if defined $extra{$test};
659     }
660     close (DETAILS);
661
662 }
663 \f
664 sub xsystem {
665     my ($command, %options) = @_;
666     print "$command\n" if $VERBOSE || $options{VERBOSE};
667
668     my ($log) = $options{LOG};
669     if (defined ($log)) {
670         $command = "($command) >output/$log.out 2>output/$log.err";
671     }
672
673     my ($pid, $status);
674     eval {
675         local $SIG{ALRM} = sub { die "alarm\n" };
676         alarm $options{TIMEOUT} if defined $options{TIMEOUT};
677         $pid = fork;
678         die "fork: $!\n" if !defined $pid;
679         exec ($command), die "$command: exec: $!\n" if !$pid;
680         waitpid ($pid, 0);
681         $status = $?;
682         alarm 0;
683     };
684     if ($@) {
685         die unless $@ eq "alarm\n";   # propagate unexpected errors
686         print "Timed out.\n";
687         kill SIGTERM, $pid;
688         $status = 0;
689     }
690
691     if (WIFSIGNALED ($status)) {
692         my ($signal) = WTERMSIG ($status);
693         die "Interrupted\n" if $signal == SIGINT;
694         print "Child terminated with signal $signal\n";
695     }
696
697     unlink ("output/$log.err") if defined ($log) && $status == 0;
698
699     die $options{DIE} if $status != 0 && defined $options{DIE};
700
701     return $status == 0;
702 }
703
704 sub snarf {
705     my ($file) = @_;
706     open (OUTPUT, $file) or die "$file: open: $!\n";
707     my (@lines) = <OUTPUT>;
708     chomp (@lines);
709     close (OUTPUT);
710     return wantarray ? @lines : join ('', map ("$_\n", @lines));
711 }
712
713 sub files_equal {
714     my ($a, $b) = @_;
715     my ($equal);
716     open (A, "<$a") or die "$a: open: $!\n";
717     open (B, "<$b") or die "$b: open: $!\n";
718     if (-s A != -s B) {
719         $equal = 0;
720     } else {
721         my ($sa, $sb);
722         for (;;) {
723             sysread (A, $sa, 1024);
724             sysread (B, $sb, 1024);
725             $equal = 0, last if $sa ne $sb;
726             $equal = 1, last if $sa eq '';
727         }
728     }
729     close (A);
730     close (B);
731     return $equal;
732 }
733
734 sub file_contains {
735     my ($file, $expected) = @_;
736     open (FILE, "<$file") or die "$file: open: $!\n";
737     my ($actual);
738     sysread (FILE, $actual, -s FILE);
739     my ($equal) = $actual eq $expected;
740     close (FILE);
741     return $equal;
742 }
743
744 sub number_lines {
745     my ($ln, $lines) = @_;
746     my ($out);
747     for my $line (@$lines) {
748         chomp $line;
749         $out .= sprintf "%4d  %s\n", $ln++, $line;
750     }
751     return $out;
752 }