Import of code from c-tap-harness
[openafs.git] / src / external / c-tap-harness / tests / runtests.c
1 /*
2  * Run a set of tests, reporting results.
3  *
4  * Test suite driver that runs a set of tests implementing a subset of the
5  * Test Anything Protocol (TAP) and reports the results.
6  *
7  * Any bug reports, bug fixes, and improvements are very much welcome and
8  * should be sent to the e-mail address below.  This program is part of C TAP
9  * Harness <https://www.eyrie.org/~eagle/software/c-tap-harness/>.
10  *
11  * Copyright 2000-2001, 2004, 2006-2019 Russ Allbery <eagle@eyrie.org>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  *
31  * SPDX-License-Identifier: MIT
32  */
33
34 /*
35  * Usage:
36  *
37  *      runtests [-hv] [-b <build-dir>] [-s <source-dir>] -l <test-list>
38  *      runtests [-hv] [-b <build-dir>] [-s <source-dir>] <test> [<test> ...]
39  *      runtests -o [-h] [-b <build-dir>] [-s <source-dir>] <test>
40  *
41  * In the first case, expects a list of executables located in the given file,
42  * one line per executable, possibly followed by a space-separated list of
43  * options.  For each one, runs it as part of a test suite, reporting results.
44  * In the second case, use the same infrastructure, but run only the tests
45  * listed on the command line.
46  *
47  * Test output should start with a line containing the number of tests
48  * (numbered from 1 to this number), optionally preceded by "1..", although
49  * that line may be given anywhere in the output.  Each additional line should
50  * be in the following format:
51  *
52  *      ok <number>
53  *      not ok <number>
54  *      ok <number> # skip
55  *      not ok <number> # todo
56  *
57  * where <number> is the number of the test.  An optional comment is permitted
58  * after the number if preceded by whitespace.  ok indicates success, not ok
59  * indicates failure.  "# skip" and "# todo" are a special cases of a comment,
60  * and must start with exactly that formatting.  They indicate the test was
61  * skipped for some reason (maybe because it doesn't apply to this platform)
62  * or is testing something known to currently fail.  The text following either
63  * "# skip" or "# todo" and whitespace is the reason.
64  *
65  * As a special case, the first line of the output may be in the form:
66  *
67  *      1..0 # skip some reason
68  *
69  * which indicates that this entire test case should be skipped and gives a
70  * reason.
71  *
72  * Any other lines are ignored, although for compliance with the TAP protocol
73  * all lines other than the ones in the above format should be sent to
74  * standard error rather than standard output and start with #.
75  *
76  * This is a subset of TAP as documented in Test::Harness::TAP or
77  * TAP::Parser::Grammar, which comes with Perl.
78  *
79  * If the -o option is given, instead run a single test and display all of its
80  * output.  This is intended for use with failing tests so that the person
81  * running the test suite can get more details about what failed.
82  *
83  * If built with the C preprocessor symbols C_TAP_SOURCE and C_TAP_BUILD
84  * defined, C TAP Harness will export those values in the environment so that
85  * tests can find the source and build directory and will look for tests under
86  * both directories.  These paths can also be set with the -b and -s
87  * command-line options, which will override anything set at build time.
88  *
89  * If the -v option is given, or the C_TAP_VERBOSE environment variable is set,
90  * display the full output of each test as it runs rather than showing a
91  * summary of the results of each test.
92  */
93
94 /* Required for fdopen(), getopt(), and putenv(). */
95 #if defined(__STRICT_ANSI__) || defined(PEDANTIC)
96 #    ifndef _XOPEN_SOURCE
97 #        define _XOPEN_SOURCE 500
98 #    endif
99 #endif
100
101 #include <ctype.h>
102 #include <errno.h>
103 #include <fcntl.h>
104 #include <limits.h>
105 #include <stdarg.h>
106 #include <stddef.h>
107 #include <stdio.h>
108 #include <stdlib.h>
109 #include <string.h>
110 #include <strings.h>
111 #include <sys/stat.h>
112 #include <sys/time.h>
113 #include <sys/types.h>
114 #include <sys/wait.h>
115 #include <time.h>
116 #include <unistd.h>
117
118 /* sys/time.h must be included before sys/resource.h on some platforms. */
119 #include <sys/resource.h>
120
121 /* AIX 6.1 (and possibly later) doesn't have WCOREDUMP. */
122 #ifndef WCOREDUMP
123 #    define WCOREDUMP(status) ((unsigned) (status) &0x80)
124 #endif
125
126 /*
127  * POSIX requires that these be defined in <unistd.h>, but they're not always
128  * available.  If one of them has been defined, all the rest almost certainly
129  * have.
130  */
131 #ifndef STDIN_FILENO
132 #    define STDIN_FILENO  0
133 #    define STDOUT_FILENO 1
134 #    define STDERR_FILENO 2
135 #endif
136
137 /*
138  * Used for iterating through arrays.  Returns the number of elements in the
139  * array (useful for a < upper bound in a for loop).
140  */
141 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
142
143 /*
144  * The source and build versions of the tests directory.  This is used to set
145  * the C_TAP_SOURCE and C_TAP_BUILD environment variables (and the SOURCE and
146  * BUILD environment variables set for backward compatibility) and find test
147  * programs, if set.  Normally, this should be set as part of the build
148  * process to the test subdirectories of $(abs_top_srcdir) and
149  * $(abs_top_builddir) respectively.
150  */
151 #ifndef C_TAP_SOURCE
152 #    define C_TAP_SOURCE NULL
153 #endif
154 #ifndef C_TAP_BUILD
155 #    define C_TAP_BUILD NULL
156 #endif
157
158 /* Test status codes. */
159 enum test_status { TEST_FAIL, TEST_PASS, TEST_SKIP, TEST_INVALID };
160
161 /* Really, just a boolean, but this is more self-documenting. */
162 enum test_verbose { CONCISE = 0, VERBOSE = 1 };
163
164 /* Indicates the state of our plan. */
165 enum plan_status {
166     PLAN_INIT,    /* Nothing seen yet. */
167     PLAN_FIRST,   /* Plan seen before any tests. */
168     PLAN_PENDING, /* Test seen and no plan yet. */
169     PLAN_FINAL    /* Plan seen after some tests. */
170 };
171
172 /* Error exit statuses for test processes. */
173 #define CHILDERR_DUP    100 /* Couldn't redirect stderr or stdout. */
174 #define CHILDERR_EXEC   101 /* Couldn't exec child process. */
175 #define CHILDERR_STDIN  102 /* Couldn't open stdin file. */
176 #define CHILDERR_STDERR 103 /* Couldn't open stderr file. */
177
178 /* Structure to hold data for a set of tests. */
179 struct testset {
180     char *file;                /* The file name of the test. */
181     char **command;            /* The argv vector to run the command. */
182     enum plan_status plan;     /* The status of our plan. */
183     unsigned long count;       /* Expected count of tests. */
184     unsigned long current;     /* The last seen test number. */
185     unsigned int length;       /* The length of the last status message. */
186     unsigned long passed;      /* Count of passing tests. */
187     unsigned long failed;      /* Count of failing lists. */
188     unsigned long skipped;     /* Count of skipped tests (passed). */
189     unsigned long allocated;   /* The size of the results table. */
190     enum test_status *results; /* Table of results by test number. */
191     unsigned int aborted;      /* Whether the set was aborted. */
192     unsigned int reported;     /* Whether the results were reported. */
193     int status;                /* The exit status of the test. */
194     unsigned int all_skipped;  /* Whether all tests were skipped. */
195     char *reason;              /* Why all tests were skipped. */
196 };
197
198 /* Structure to hold a linked list of test sets. */
199 struct testlist {
200     struct testset *ts;
201     struct testlist *next;
202 };
203
204 /*
205  * Usage message.  Should be used as a printf format with four arguments: the
206  * path to runtests, given three times, and the usage_description.  This is
207  * split into variables to satisfy the pedantic ISO C90 limit on strings.
208  */
209 static const char usage_message[] = "\
210 Usage: %s [-hv] [-b <build-dir>] [-s <source-dir>] <test> ...\n\
211        %s [-hv] [-b <build-dir>] [-s <source-dir>] -l <test-list>\n\
212        %s -o [-h] [-b <build-dir>] [-s <source-dir>] <test>\n\
213 \n\
214 Options:\n\
215     -b <build-dir>      Set the build directory to <build-dir>\n\
216 %s";
217 static const char usage_extra[] = "\
218     -l <list>           Take the list of tests to run from <test-list>\n\
219     -o                  Run a single test rather than a list of tests\n\
220     -s <source-dir>     Set the source directory to <source-dir>\n\
221     -v                  Show the full output of each test\n\
222 \n\
223 runtests normally runs each test listed on the command line.  With the -l\n\
224 option, it instead runs every test listed in a file.  With the -o option,\n\
225 it instead runs a single test and shows its complete output.\n";
226
227 /*
228  * Header used for test output.  %s is replaced by the file name of the list
229  * of tests.
230  */
231 static const char banner[] = "\n\
232 Running all tests listed in %s.  If any tests fail, run the failing\n\
233 test program with runtests -o to see more details.\n\n";
234
235 /* Header for reports of failed tests. */
236 static const char header[] = "\n\
237 Failed Set                 Fail/Total (%) Skip Stat  Failing Tests\n\
238 -------------------------- -------------- ---- ----  ------------------------";
239
240 /* Include the file name and line number in malloc failures. */
241 #define xcalloc(n, type) \
242     ((type *) x_calloc((n), sizeof(type), __FILE__, __LINE__))
243 #define xmalloc(size)     ((char *) x_malloc((size), __FILE__, __LINE__))
244 #define xstrdup(p)        x_strdup((p), __FILE__, __LINE__)
245 #define xstrndup(p, size) x_strndup((p), (size), __FILE__, __LINE__)
246 #define xreallocarray(p, n, type) \
247     ((type *) x_reallocarray((p), (n), sizeof(type), __FILE__, __LINE__))
248
249 /*
250  * __attribute__ is available in gcc 2.5 and later, but only with gcc 2.7
251  * could you use the __format__ form of the attributes, which is what we use
252  * (to avoid confusion with other macros).
253  */
254 #ifndef __attribute__
255 #    if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
256 #        define __attribute__(spec) /* empty */
257 #    endif
258 #endif
259
260 /*
261  * We use __alloc_size__, but it was only available in fairly recent versions
262  * of GCC.  Suppress warnings about the unknown attribute if GCC is too old.
263  * We know that we're GCC at this point, so we can use the GCC variadic macro
264  * extension, which will still work with versions of GCC too old to have C99
265  * variadic macro support.
266  */
267 #if !defined(__attribute__) && !defined(__alloc_size__)
268 #    if defined(__GNUC__) && !defined(__clang__)
269 #        if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
270 #            define __alloc_size__(spec, args...) /* empty */
271 #        endif
272 #    endif
273 #endif
274
275 /*
276  * LLVM and Clang pretend to be GCC but don't support all of the __attribute__
277  * settings that GCC does.  For them, suppress warnings about unknown
278  * attributes on declarations.  This unfortunately will affect the entire
279  * compilation context, but there's no push and pop available.
280  */
281 #if !defined(__attribute__) && (defined(__llvm__) || defined(__clang__))
282 #    pragma GCC diagnostic ignored "-Wattributes"
283 #endif
284
285 /* Declare internal functions that benefit from compiler attributes. */
286 static void die(const char *, ...)
287     __attribute__((__nonnull__, __noreturn__, __format__(printf, 1, 2)));
288 static void sysdie(const char *, ...)
289     __attribute__((__nonnull__, __noreturn__, __format__(printf, 1, 2)));
290 static void *x_calloc(size_t, size_t, const char *, int)
291     __attribute__((__alloc_size__(1, 2), __malloc__, __nonnull__));
292 static void *x_malloc(size_t, const char *, int)
293     __attribute__((__alloc_size__(1), __malloc__, __nonnull__));
294 static void *x_reallocarray(void *, size_t, size_t, const char *, int)
295     __attribute__((__alloc_size__(2, 3), __malloc__, __nonnull__(4)));
296 static char *x_strdup(const char *, const char *, int)
297     __attribute__((__malloc__, __nonnull__));
298 static char *x_strndup(const char *, size_t, const char *, int)
299     __attribute__((__malloc__, __nonnull__));
300
301
302 /*
303  * Report a fatal error and exit.
304  */
305 static void
306 die(const char *format, ...)
307 {
308     va_list args;
309
310     fflush(stdout);
311     fprintf(stderr, "runtests: ");
312     va_start(args, format);
313     vfprintf(stderr, format, args);
314     va_end(args);
315     fprintf(stderr, "\n");
316     exit(1);
317 }
318
319
320 /*
321  * Report a fatal error, including the results of strerror, and exit.
322  */
323 static void
324 sysdie(const char *format, ...)
325 {
326     int oerrno;
327     va_list args;
328
329     oerrno = errno;
330     fflush(stdout);
331     fprintf(stderr, "runtests: ");
332     va_start(args, format);
333     vfprintf(stderr, format, args);
334     va_end(args);
335     fprintf(stderr, ": %s\n", strerror(oerrno));
336     exit(1);
337 }
338
339
340 /*
341  * Allocate zeroed memory, reporting a fatal error and exiting on failure.
342  */
343 static void *
344 x_calloc(size_t n, size_t size, const char *file, int line)
345 {
346     void *p;
347
348     n = (n > 0) ? n : 1;
349     size = (size > 0) ? size : 1;
350     p = calloc(n, size);
351     if (p == NULL)
352         sysdie("failed to calloc %lu bytes at %s line %d",
353                (unsigned long) size, file, line);
354     return p;
355 }
356
357
358 /*
359  * Allocate memory, reporting a fatal error and exiting on failure.
360  */
361 static void *
362 x_malloc(size_t size, const char *file, int line)
363 {
364     void *p;
365
366     p = malloc(size);
367     if (p == NULL)
368         sysdie("failed to malloc %lu bytes at %s line %d",
369                (unsigned long) size, file, line);
370     return p;
371 }
372
373
374 /*
375  * Reallocate memory, reporting a fatal error and exiting on failure.
376  *
377  * We should technically use SIZE_MAX here for the overflow check, but
378  * SIZE_MAX is C99 and we're only assuming C89 + SUSv3, which does not
379  * guarantee that it exists.  They do guarantee that UINT_MAX exists, and we
380  * can assume that UINT_MAX <= SIZE_MAX.  And we should not be allocating
381  * anything anywhere near that large.
382  *
383  * (In theory, C89 and C99 permit size_t to be smaller than unsigned int, but
384  * I disbelieve in the existence of such systems and they will have to cope
385  * without overflow checks.)
386  */
387 static void *
388 x_reallocarray(void *p, size_t n, size_t size, const char *file, int line)
389 {
390     n = (n > 0) ? n : 1;
391     size = (size > 0) ? size : 1;
392
393     if (n > 0 && UINT_MAX / n <= size)
394         sysdie("realloc too large at %s line %d", file, line);
395     p = realloc(p, n * size);
396     if (p == NULL)
397         sysdie("failed to realloc %lu bytes at %s line %d",
398                (unsigned long) (n * size), file, line);
399     return p;
400 }
401
402
403 /*
404  * Copy a string, reporting a fatal error and exiting on failure.
405  */
406 static char *
407 x_strdup(const char *s, const char *file, int line)
408 {
409     char *p;
410     size_t len;
411
412     len = strlen(s) + 1;
413     p = (char *) malloc(len);
414     if (p == NULL)
415         sysdie("failed to strdup %lu bytes at %s line %d", (unsigned long) len,
416                file, line);
417     memcpy(p, s, len);
418     return p;
419 }
420
421
422 /*
423  * Copy the first n characters of a string, reporting a fatal error and
424  * existing on failure.
425  *
426  * Avoid using the system strndup function since it may not exist (on Mac OS
427  * X, for example), and there's no need to introduce another portability
428  * requirement.
429  */
430 char *
431 x_strndup(const char *s, size_t size, const char *file, int line)
432 {
433     const char *p;
434     size_t len;
435     char *copy;
436
437     /* Don't assume that the source string is nul-terminated. */
438     for (p = s; (size_t)(p - s) < size && *p != '\0'; p++)
439         ;
440     len = (size_t)(p - s);
441     copy = (char *) malloc(len + 1);
442     if (copy == NULL)
443         sysdie("failed to strndup %lu bytes at %s line %d",
444                (unsigned long) len, file, line);
445     memcpy(copy, s, len);
446     copy[len] = '\0';
447     return copy;
448 }
449
450
451 /*
452  * Form a new string by concatenating multiple strings.  The arguments must be
453  * terminated by (const char *) 0.
454  *
455  * This function only exists because we can't assume asprintf.  We can't
456  * simulate asprintf with snprintf because we're only assuming SUSv3, which
457  * does not require that snprintf with a NULL buffer return the required
458  * length.  When those constraints are relaxed, this should be ripped out and
459  * replaced with asprintf or a more trivial replacement with snprintf.
460  */
461 static char *
462 concat(const char *first, ...)
463 {
464     va_list args;
465     char *result;
466     const char *string;
467     size_t offset;
468     size_t length = 0;
469
470     /*
471      * Find the total memory required.  Ensure we don't overflow length.  We
472      * aren't guaranteed to have SIZE_MAX, so use UINT_MAX as an acceptable
473      * substitute (see the x_nrealloc comments).
474      */
475     va_start(args, first);
476     for (string = first; string != NULL; string = va_arg(args, const char *)) {
477         if (length >= UINT_MAX - strlen(string)) {
478             errno = EINVAL;
479             sysdie("strings too long in concat");
480         }
481         length += strlen(string);
482     }
483     va_end(args);
484     length++;
485
486     /* Create the string. */
487     result = xmalloc(length);
488     va_start(args, first);
489     offset = 0;
490     for (string = first; string != NULL; string = va_arg(args, const char *)) {
491         memcpy(result + offset, string, strlen(string));
492         offset += strlen(string);
493     }
494     va_end(args);
495     result[offset] = '\0';
496     return result;
497 }
498
499
500 /*
501  * Given a struct timeval, return the number of seconds it represents as a
502  * double.  Use difftime() to convert a time_t to a double.
503  */
504 static double
505 tv_seconds(const struct timeval *tv)
506 {
507     return difftime(tv->tv_sec, 0) + (double) tv->tv_usec * 1e-6;
508 }
509
510
511 /*
512  * Given two struct timevals, return the difference in seconds.
513  */
514 static double
515 tv_diff(const struct timeval *tv1, const struct timeval *tv0)
516 {
517     return tv_seconds(tv1) - tv_seconds(tv0);
518 }
519
520
521 /*
522  * Given two struct timevals, return the sum in seconds as a double.
523  */
524 static double
525 tv_sum(const struct timeval *tv1, const struct timeval *tv2)
526 {
527     return tv_seconds(tv1) + tv_seconds(tv2);
528 }
529
530
531 /*
532  * Given a pointer to a string, skip any leading whitespace and return a
533  * pointer to the first non-whitespace character.
534  */
535 static const char *
536 skip_whitespace(const char *p)
537 {
538     while (isspace((unsigned char) (*p)))
539         p++;
540     return p;
541 }
542
543
544 /*
545  * Given a pointer to a string, skip any non-whitespace characters and return
546  * a pointer to the first whitespace character, or to the end of the string.
547  */
548 static const char *
549 skip_non_whitespace(const char *p)
550 {
551     while (*p != '\0' && !isspace((unsigned char) (*p)))
552         p++;
553     return p;
554 }
555
556
557 /*
558  * Start a program, connecting its stdout to a pipe on our end and its stderr
559  * to /dev/null, and storing the file descriptor to read from in the two
560  * argument.  Returns the PID of the new process.  Errors are fatal.
561  */
562 static pid_t
563 test_start(char *const *command, int *fd)
564 {
565     int fds[2], infd, errfd;
566     pid_t child;
567
568     /* Create a pipe used to capture the output from the test program. */
569     if (pipe(fds) == -1) {
570         puts("ABORTED");
571         fflush(stdout);
572         sysdie("can't create pipe");
573     }
574
575     /* Fork a child process, massage the file descriptors, and exec. */
576     child = fork();
577     switch (child) {
578     case -1:
579         puts("ABORTED");
580         fflush(stdout);
581         sysdie("can't fork");
582
583     /* In the child.  Set up our standard output. */
584     case 0:
585         close(fds[0]);
586         close(STDOUT_FILENO);
587         if (dup2(fds[1], STDOUT_FILENO) < 0)
588             _exit(CHILDERR_DUP);
589         close(fds[1]);
590
591         /* Point standard input at /dev/null. */
592         close(STDIN_FILENO);
593         infd = open("/dev/null", O_RDONLY);
594         if (infd < 0)
595             _exit(CHILDERR_STDIN);
596         if (infd != STDIN_FILENO) {
597             if (dup2(infd, STDIN_FILENO) < 0)
598                 _exit(CHILDERR_DUP);
599             close(infd);
600         }
601
602         /* Point standard error at /dev/null. */
603         close(STDERR_FILENO);
604         errfd = open("/dev/null", O_WRONLY);
605         if (errfd < 0)
606             _exit(CHILDERR_STDERR);
607         if (errfd != STDERR_FILENO) {
608             if (dup2(errfd, STDERR_FILENO) < 0)
609                 _exit(CHILDERR_DUP);
610             close(errfd);
611         }
612
613         /* Now, exec our process. */
614         if (execv(command[0], command) == -1)
615             _exit(CHILDERR_EXEC);
616         break;
617
618     /* In parent.  Close the extra file descriptor. */
619     default:
620         close(fds[1]);
621         break;
622     }
623     *fd = fds[0];
624     return child;
625 }
626
627
628 /*
629  * Back up over the output saying what test we were executing.
630  */
631 static void
632 test_backspace(struct testset *ts)
633 {
634     unsigned int i;
635
636     if (!isatty(STDOUT_FILENO))
637         return;
638     for (i = 0; i < ts->length; i++)
639         putchar('\b');
640     for (i = 0; i < ts->length; i++)
641         putchar(' ');
642     for (i = 0; i < ts->length; i++)
643         putchar('\b');
644     ts->length = 0;
645 }
646
647
648 /*
649  * Allocate or resize the array of test results to be large enough to contain
650  * the test number in.
651  */
652 static void
653 resize_results(struct testset *ts, unsigned long n)
654 {
655     unsigned long i;
656     size_t s;
657
658     /* If there's already enough space, return quickly. */
659     if (n <= ts->allocated)
660         return;
661
662     /*
663      * If no space has been allocated, do the initial allocation.  Otherwise,
664      * resize.  Start with 32 test cases and then add 1024 with each resize to
665      * try to reduce the number of reallocations.
666      */
667     if (ts->allocated == 0) {
668         s = (n > 32) ? n : 32;
669         ts->results = xcalloc(s, enum test_status);
670     } else {
671         s = (n > ts->allocated + 1024) ? n : ts->allocated + 1024;
672         ts->results = xreallocarray(ts->results, s, enum test_status);
673     }
674
675     /* Set the results for the newly-allocated test array. */
676     for (i = ts->allocated; i < s; i++)
677         ts->results[i] = TEST_INVALID;
678     ts->allocated = s;
679 }
680
681
682 /*
683  * Report an invalid test number and set the appropriate flags.  Pulled into a
684  * separate function since we do this in several places.
685  */
686 static void
687 invalid_test_number(struct testset *ts, long n, enum test_verbose verbose)
688 {
689     if (!verbose)
690         test_backspace(ts);
691     printf("ABORTED (invalid test number %ld)\n", n);
692     ts->aborted = 1;
693     ts->reported = 1;
694 }
695
696
697 /*
698  * Read the plan line of test output, which should contain the range of test
699  * numbers.  We may initialize the testset structure here if we haven't yet
700  * seen a test.  Return true if initialization succeeded and the test should
701  * continue, false otherwise.
702  */
703 static int
704 test_plan(const char *line, struct testset *ts, enum test_verbose verbose)
705 {
706     long n;
707
708     /*
709      * Accept a plan without the leading 1.. for compatibility with older
710      * versions of runtests.  This will only be allowed if we've not yet seen
711      * a test result.
712      */
713     line = skip_whitespace(line);
714     if (strncmp(line, "1..", 3) == 0)
715         line += 3;
716
717     /*
718      * Get the count and check it for validity.
719      *
720      * If we have something of the form "1..0 # skip foo", the whole file was
721      * skipped; record that.  If we do skip the whole file, zero out all of
722      * our statistics, since they're no longer relevant.
723      *
724      * strtol is called with a second argument to advance the line pointer
725      * past the count to make it simpler to detect the # skip case.
726      */
727     n = strtol(line, (char **) &line, 10);
728     if (n == 0) {
729         line = skip_whitespace(line);
730         if (*line == '#') {
731             line = skip_whitespace(line + 1);
732             if (strncasecmp(line, "skip", 4) == 0) {
733                 line = skip_whitespace(line + 4);
734                 if (*line != '\0') {
735                     ts->reason = xstrdup(line);
736                     ts->reason[strlen(ts->reason) - 1] = '\0';
737                 }
738                 ts->all_skipped = 1;
739                 ts->aborted = 1;
740                 ts->count = 0;
741                 ts->passed = 0;
742                 ts->skipped = 0;
743                 ts->failed = 0;
744                 return 0;
745             }
746         }
747     }
748     if (n <= 0) {
749         puts("ABORTED (invalid test count)");
750         ts->aborted = 1;
751         ts->reported = 1;
752         return 0;
753     }
754
755     /*
756      * If we are doing lazy planning, check the plan against the largest test
757      * number that we saw and fail now if we saw a check outside the plan
758      * range.
759      */
760     if (ts->plan == PLAN_PENDING && (unsigned long) n < ts->count) {
761         invalid_test_number(ts, (long) ts->count, verbose);
762         return 0;
763     }
764
765     /*
766      * Otherwise, allocated or resize the results if needed and update count,
767      * and then record that we've seen a plan.
768      */
769     resize_results(ts, (unsigned long) n);
770     ts->count = (unsigned long) n;
771     if (ts->plan == PLAN_INIT)
772         ts->plan = PLAN_FIRST;
773     else if (ts->plan == PLAN_PENDING)
774         ts->plan = PLAN_FINAL;
775     return 1;
776 }
777
778
779 /*
780  * Given a single line of output from a test, parse it and return the success
781  * status of that test.  Anything printed to stdout not matching the form
782  * /^(not )?ok \d+/ is ignored.  Sets ts->current to the test number that just
783  * reported status.
784  */
785 static void
786 test_checkline(const char *line, struct testset *ts, enum test_verbose verbose)
787 {
788     enum test_status status = TEST_PASS;
789     const char *bail;
790     char *end;
791     long number;
792     unsigned long current;
793     int outlen;
794
795     /* Before anything, check for a test abort. */
796     bail = strstr(line, "Bail out!");
797     if (bail != NULL) {
798         bail = skip_whitespace(bail + strlen("Bail out!"));
799         if (*bail != '\0') {
800             size_t length;
801
802             length = strlen(bail);
803             if (bail[length - 1] == '\n')
804                 length--;
805             if (!verbose)
806                 test_backspace(ts);
807             printf("ABORTED (%.*s)\n", (int) length, bail);
808             ts->reported = 1;
809         }
810         ts->aborted = 1;
811         return;
812     }
813
814     /*
815      * If the given line isn't newline-terminated, it was too big for an
816      * fgets(), which means ignore it.
817      */
818     if (line[strlen(line) - 1] != '\n')
819         return;
820
821     /* If the line begins with a hash mark, ignore it. */
822     if (line[0] == '#')
823         return;
824
825     /* If we haven't yet seen a plan, look for one. */
826     if (ts->plan == PLAN_INIT && isdigit((unsigned char) (*line))) {
827         if (!test_plan(line, ts, verbose))
828             return;
829     } else if (strncmp(line, "1..", 3) == 0) {
830         if (ts->plan == PLAN_PENDING) {
831             if (!test_plan(line, ts, verbose))
832                 return;
833         } else {
834             if (!verbose)
835                 test_backspace(ts);
836             puts("ABORTED (multiple plans)");
837             ts->aborted = 1;
838             ts->reported = 1;
839             return;
840         }
841     }
842
843     /* Parse the line, ignoring something we can't parse. */
844     if (strncmp(line, "not ", 4) == 0) {
845         status = TEST_FAIL;
846         line += 4;
847     }
848     if (strncmp(line, "ok", 2) != 0)
849         return;
850     line = skip_whitespace(line + 2);
851     errno = 0;
852     number = strtol(line, &end, 10);
853     if (errno != 0 || end == line)
854         current = ts->current + 1;
855     else if (number <= 0) {
856         invalid_test_number(ts, number, verbose);
857         return;
858     } else
859         current = (unsigned long) number;
860     if (current > ts->count && ts->plan == PLAN_FIRST) {
861         invalid_test_number(ts, (long) current, verbose);
862         return;
863     }
864
865     /* We have a valid test result.  Tweak the results array if needed. */
866     if (ts->plan == PLAN_INIT || ts->plan == PLAN_PENDING) {
867         ts->plan = PLAN_PENDING;
868         resize_results(ts, current);
869         if (current > ts->count)
870             ts->count = current;
871     }
872
873     /*
874      * Handle directives.  We should probably do something more interesting
875      * with unexpected passes of todo tests.
876      */
877     while (isdigit((unsigned char) (*line)))
878         line++;
879     line = skip_whitespace(line);
880     if (*line == '#') {
881         line = skip_whitespace(line + 1);
882         if (strncasecmp(line, "skip", 4) == 0)
883             status = TEST_SKIP;
884         if (strncasecmp(line, "todo", 4) == 0)
885             status = (status == TEST_FAIL) ? TEST_SKIP : TEST_FAIL;
886     }
887
888     /* Make sure that the test number is in range and not a duplicate. */
889     if (ts->results[current - 1] != TEST_INVALID) {
890         if (!verbose)
891             test_backspace(ts);
892         printf("ABORTED (duplicate test number %lu)\n", current);
893         ts->aborted = 1;
894         ts->reported = 1;
895         return;
896     }
897
898     /* Good results.  Increment our various counters. */
899     switch (status) {
900     case TEST_PASS:
901         ts->passed++;
902         break;
903     case TEST_FAIL:
904         ts->failed++;
905         break;
906     case TEST_SKIP:
907         ts->skipped++;
908         break;
909     case TEST_INVALID:
910         break;
911     }
912     ts->current = current;
913     ts->results[current - 1] = status;
914     if (!verbose && isatty(STDOUT_FILENO)) {
915         test_backspace(ts);
916         if (ts->plan == PLAN_PENDING)
917             outlen = printf("%lu/?", current);
918         else
919             outlen = printf("%lu/%lu", current, ts->count);
920         ts->length = (outlen >= 0) ? (unsigned int) outlen : 0;
921         fflush(stdout);
922     }
923 }
924
925
926 /*
927  * Print out a range of test numbers, returning the number of characters it
928  * took up.  Takes the first number, the last number, the number of characters
929  * already printed on the line, and the limit of number of characters the line
930  * can hold.  Add a comma and a space before the range if chars indicates that
931  * something has already been printed on the line, and print ... instead if
932  * chars plus the space needed would go over the limit (use a limit of 0 to
933  * disable this).
934  */
935 static unsigned int
936 test_print_range(unsigned long first, unsigned long last, unsigned long chars,
937                  unsigned int limit)
938 {
939     unsigned int needed = 0;
940     unsigned long n;
941
942     for (n = first; n > 0; n /= 10)
943         needed++;
944     if (last > first) {
945         for (n = last; n > 0; n /= 10)
946             needed++;
947         needed++;
948     }
949     if (chars > 0)
950         needed += 2;
951     if (limit > 0 && chars + needed > limit) {
952         needed = 0;
953         if (chars <= limit) {
954             if (chars > 0) {
955                 printf(", ");
956                 needed += 2;
957             }
958             printf("...");
959             needed += 3;
960         }
961     } else {
962         if (chars > 0)
963             printf(", ");
964         if (last > first)
965             printf("%lu-", first);
966         printf("%lu", last);
967     }
968     return needed;
969 }
970
971
972 /*
973  * Summarize a single test set.  The second argument is 0 if the set exited
974  * cleanly, a positive integer representing the exit status if it exited
975  * with a non-zero status, and a negative integer representing the signal
976  * that terminated it if it was killed by a signal.
977  */
978 static void
979 test_summarize(struct testset *ts, int status)
980 {
981     unsigned long i;
982     unsigned long missing = 0;
983     unsigned long failed = 0;
984     unsigned long first = 0;
985     unsigned long last = 0;
986
987     if (ts->aborted) {
988         fputs("ABORTED", stdout);
989         if (ts->count > 0)
990             printf(" (passed %lu/%lu)", ts->passed, ts->count - ts->skipped);
991     } else {
992         for (i = 0; i < ts->count; i++) {
993             if (ts->results[i] == TEST_INVALID) {
994                 if (missing == 0)
995                     fputs("MISSED ", stdout);
996                 if (first && i == last)
997                     last = i + 1;
998                 else {
999                     if (first)
1000                         test_print_range(first, last, missing - 1, 0);
1001                     missing++;
1002                     first = i + 1;
1003                     last = i + 1;
1004                 }
1005             }
1006         }
1007         if (first)
1008             test_print_range(first, last, missing - 1, 0);
1009         first = 0;
1010         last = 0;
1011         for (i = 0; i < ts->count; i++) {
1012             if (ts->results[i] == TEST_FAIL) {
1013                 if (missing && !failed)
1014                     fputs("; ", stdout);
1015                 if (failed == 0)
1016                     fputs("FAILED ", stdout);
1017                 if (first && i == last)
1018                     last = i + 1;
1019                 else {
1020                     if (first)
1021                         test_print_range(first, last, failed - 1, 0);
1022                     failed++;
1023                     first = i + 1;
1024                     last = i + 1;
1025                 }
1026             }
1027         }
1028         if (first)
1029             test_print_range(first, last, failed - 1, 0);
1030         if (!missing && !failed) {
1031             fputs(!status ? "ok" : "dubious", stdout);
1032             if (ts->skipped > 0) {
1033                 if (ts->skipped == 1)
1034                     printf(" (skipped %lu test)", ts->skipped);
1035                 else
1036                     printf(" (skipped %lu tests)", ts->skipped);
1037             }
1038         }
1039     }
1040     if (status > 0)
1041         printf(" (exit status %d)", status);
1042     else if (status < 0)
1043         printf(" (killed by signal %d%s)", -status,
1044                WCOREDUMP(ts->status) ? ", core dumped" : "");
1045     putchar('\n');
1046 }
1047
1048
1049 /*
1050  * Given a test set, analyze the results, classify the exit status, handle a
1051  * few special error messages, and then pass it along to test_summarize() for
1052  * the regular output.  Returns true if the test set ran successfully and all
1053  * tests passed or were skipped, false otherwise.
1054  */
1055 static int
1056 test_analyze(struct testset *ts)
1057 {
1058     if (ts->reported)
1059         return 0;
1060     if (ts->all_skipped) {
1061         if (ts->reason == NULL)
1062             puts("skipped");
1063         else
1064             printf("skipped (%s)\n", ts->reason);
1065         return 1;
1066     } else if (WIFEXITED(ts->status) && WEXITSTATUS(ts->status) != 0) {
1067         switch (WEXITSTATUS(ts->status)) {
1068         case CHILDERR_DUP:
1069             if (!ts->reported)
1070                 puts("ABORTED (can't dup file descriptors)");
1071             break;
1072         case CHILDERR_EXEC:
1073             if (!ts->reported)
1074                 puts("ABORTED (execution failed -- not found?)");
1075             break;
1076         case CHILDERR_STDIN:
1077         case CHILDERR_STDERR:
1078             if (!ts->reported)
1079                 puts("ABORTED (can't open /dev/null)");
1080             break;
1081         default:
1082             test_summarize(ts, WEXITSTATUS(ts->status));
1083             break;
1084         }
1085         return 0;
1086     } else if (WIFSIGNALED(ts->status)) {
1087         test_summarize(ts, -WTERMSIG(ts->status));
1088         return 0;
1089     } else if (ts->plan != PLAN_FIRST && ts->plan != PLAN_FINAL) {
1090         puts("ABORTED (no valid test plan)");
1091         ts->aborted = 1;
1092         return 0;
1093     } else {
1094         test_summarize(ts, 0);
1095         return (ts->failed == 0);
1096     }
1097 }
1098
1099
1100 /*
1101  * Runs a single test set, accumulating and then reporting the results.
1102  * Returns true if the test set was successfully run and all tests passed,
1103  * false otherwise.
1104  */
1105 static int
1106 test_run(struct testset *ts, enum test_verbose verbose)
1107 {
1108     pid_t testpid, child;
1109     int outfd, status;
1110     unsigned long i;
1111     FILE *output;
1112     char buffer[BUFSIZ];
1113
1114     /* Run the test program. */
1115     testpid = test_start(ts->command, &outfd);
1116     output = fdopen(outfd, "r");
1117     if (!output) {
1118         puts("ABORTED");
1119         fflush(stdout);
1120         sysdie("fdopen failed");
1121     }
1122
1123     /*
1124      * Pass each line of output to test_checkline(), and print the line if
1125      * verbosity is requested.
1126      */
1127     while (!ts->aborted && fgets(buffer, sizeof(buffer), output)) {
1128         if (verbose)
1129             printf("%s", buffer);
1130         test_checkline(buffer, ts, verbose);
1131     }
1132     if (ferror(output) || ts->plan == PLAN_INIT)
1133         ts->aborted = 1;
1134     if (!verbose)
1135         test_backspace(ts);
1136
1137     /*
1138      * Consume the rest of the test output, close the output descriptor,
1139      * retrieve the exit status, and pass that information to test_analyze()
1140      * for eventual output.
1141      */
1142     while (fgets(buffer, sizeof(buffer), output))
1143         if (verbose)
1144             printf("%s", buffer);
1145     fclose(output);
1146     child = waitpid(testpid, &ts->status, 0);
1147     if (child == (pid_t) -1) {
1148         if (!ts->reported) {
1149             puts("ABORTED");
1150             fflush(stdout);
1151         }
1152         sysdie("waitpid for %u failed", (unsigned int) testpid);
1153     }
1154     if (ts->all_skipped)
1155         ts->aborted = 0;
1156     status = test_analyze(ts);
1157
1158     /* Convert missing tests to failed tests. */
1159     for (i = 0; i < ts->count; i++) {
1160         if (ts->results[i] == TEST_INVALID) {
1161             ts->failed++;
1162             ts->results[i] = TEST_FAIL;
1163             status = 0;
1164         }
1165     }
1166     return status;
1167 }
1168
1169
1170 /* Summarize a list of test failures. */
1171 static void
1172 test_fail_summary(const struct testlist *fails)
1173 {
1174     struct testset *ts;
1175     unsigned int chars;
1176     unsigned long i, first, last, total;
1177     double failed;
1178
1179     puts(header);
1180
1181     /* Failed Set                 Fail/Total (%) Skip Stat  Failing (25)
1182        -------------------------- -------------- ---- ----  -------------- */
1183     for (; fails; fails = fails->next) {
1184         ts = fails->ts;
1185         total = ts->count - ts->skipped;
1186         failed = (double) ts->failed;
1187         printf("%-26.26s %4lu/%-4lu %3.0f%% %4lu ", ts->file, ts->failed,
1188                total, total ? (failed * 100.0) / (double) total : 0,
1189                ts->skipped);
1190         if (WIFEXITED(ts->status))
1191             printf("%4d  ", WEXITSTATUS(ts->status));
1192         else
1193             printf("  --  ");
1194         if (ts->aborted) {
1195             puts("aborted");
1196             continue;
1197         }
1198         chars = 0;
1199         first = 0;
1200         last = 0;
1201         for (i = 0; i < ts->count; i++) {
1202             if (ts->results[i] == TEST_FAIL) {
1203                 if (first != 0 && i == last)
1204                     last = i + 1;
1205                 else {
1206                     if (first != 0)
1207                         chars += test_print_range(first, last, chars, 19);
1208                     first = i + 1;
1209                     last = i + 1;
1210                 }
1211             }
1212         }
1213         if (first != 0)
1214             test_print_range(first, last, chars, 19);
1215         putchar('\n');
1216     }
1217 }
1218
1219
1220 /*
1221  * Check whether a given file path is a valid test.  Currently, this checks
1222  * whether it is executable and is a regular file.  Returns true or false.
1223  */
1224 static int
1225 is_valid_test(const char *path)
1226 {
1227     struct stat st;
1228
1229     if (access(path, X_OK) < 0)
1230         return 0;
1231     if (stat(path, &st) < 0)
1232         return 0;
1233     if (!S_ISREG(st.st_mode))
1234         return 0;
1235     return 1;
1236 }
1237
1238
1239 /*
1240  * Given the name of a test, a pointer to the testset struct, and the source
1241  * and build directories, find the test.  We try first relative to the current
1242  * directory, then in the build directory (if not NULL), then in the source
1243  * directory.  In each of those directories, we first try a "-t" extension and
1244  * then a ".t" extension.  When we find an executable program, we return the
1245  * path to that program.  If none of those paths are executable, just fill in
1246  * the name of the test as is.
1247  *
1248  * The caller is responsible for freeing the path member of the testset
1249  * struct.
1250  */
1251 static char *
1252 find_test(const char *name, const char *source, const char *build)
1253 {
1254     char *path = NULL;
1255     const char *bases[3], *suffix, *base;
1256     unsigned int i, j;
1257     const char *suffixes[3] = {"-t", ".t", ""};
1258
1259     /* Possible base directories. */
1260     bases[0] = ".";
1261     bases[1] = build;
1262     bases[2] = source;
1263
1264     /* Try each suffix with each base. */
1265     for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1266         suffix = suffixes[i];
1267         for (j = 0; j < ARRAY_SIZE(bases); j++) {
1268             base = bases[j];
1269             if (base == NULL)
1270                 continue;
1271             path = concat(base, "/", name, suffix, (const char *) 0);
1272             if (is_valid_test(path))
1273                 return path;
1274             free(path);
1275             path = NULL;
1276         }
1277     }
1278     if (path == NULL)
1279         path = xstrdup(name);
1280     return path;
1281 }
1282
1283
1284 /*
1285  * Parse a single line of a test list and store the test name and command to
1286  * execute it in the given testset struct.
1287  *
1288  * Normally, each line is just the name of the test, which is located in the
1289  * test directory and turned into a command to run.  However, each line may
1290  * have whitespace-separated options, which change the command that's run.
1291  * Current supported options are:
1292  *
1293  * valgrind
1294  *     Run the test under valgrind if C_TAP_VALGRIND is set.  The contents
1295  *     of that environment variable are taken as the valgrind command (with
1296  *     options) to run.  The command is parsed with a simple split on
1297  *     whitespace and no quoting is supported.
1298  *
1299  * libtool
1300  *     If running under valgrind, use libtool to invoke valgrind.  This avoids
1301  *     running valgrind on the wrapper shell script generated by libtool.  If
1302  *     set, C_TAP_LIBTOOL must be set to the full path to the libtool program
1303  *     to use to run valgrind and thus the test.  Ignored if the test isn't
1304  *     being run under valgrind.
1305  */
1306 static void
1307 parse_test_list_line(const char *line, struct testset *ts, const char *source,
1308                      const char *build)
1309 {
1310     const char *p, *end, *option, *libtool;
1311     const char *valgrind = NULL;
1312     unsigned int use_libtool = 0;
1313     unsigned int use_valgrind = 0;
1314     size_t len, i;
1315
1316     /* Determine the name of the test. */
1317     p = skip_non_whitespace(line);
1318     ts->file = xstrndup(line, p - line);
1319
1320     /* Check if any test options are set. */
1321     p = skip_whitespace(p);
1322     while (*p != '\0') {
1323         end = skip_non_whitespace(p);
1324         if (strncmp(p, "libtool", end - p) == 0) {
1325             use_libtool = 1;
1326         } else if (strncmp(p, "valgrind", end - p) == 0) {
1327             valgrind = getenv("C_TAP_VALGRIND");
1328             use_valgrind = (valgrind != NULL);
1329         } else {
1330             option = xstrndup(p, end - p);
1331             die("unknown test list option %s", option);
1332         }
1333         p = skip_whitespace(end);
1334     }
1335
1336     /* Construct the argv to run the test.  First, find the length. */
1337     len = 1;
1338     if (use_valgrind && valgrind != NULL) {
1339         p = skip_whitespace(valgrind);
1340         while (*p != '\0') {
1341             len++;
1342             p = skip_whitespace(skip_non_whitespace(p));
1343         }
1344         if (use_libtool)
1345             len += 2;
1346     }
1347
1348     /* Now, build the command. */
1349     ts->command = xcalloc(len + 1, char *);
1350     i = 0;
1351     if (use_valgrind && valgrind != NULL) {
1352         if (use_libtool) {
1353             libtool = getenv("C_TAP_LIBTOOL");
1354             if (libtool == NULL)
1355                 die("valgrind with libtool requested, but C_TAP_LIBTOOL is not"
1356                     " set");
1357             ts->command[i++] = xstrdup(libtool);
1358             ts->command[i++] = xstrdup("--mode=execute");
1359         }
1360         p = skip_whitespace(valgrind);
1361         while (*p != '\0') {
1362             end = skip_non_whitespace(p);
1363             ts->command[i++] = xstrndup(p, end - p);
1364             p = skip_whitespace(end);
1365         }
1366     }
1367     if (i != len - 1)
1368         die("internal error while constructing command line");
1369     ts->command[i++] = find_test(ts->file, source, build);
1370     ts->command[i] = NULL;
1371 }
1372
1373
1374 /*
1375  * Read a list of tests from a file, returning the list of tests as a struct
1376  * testlist, or NULL if there were no tests (such as a file containing only
1377  * comments).  Reports an error to standard error and exits if the list of
1378  * tests cannot be read.
1379  */
1380 static struct testlist *
1381 read_test_list(const char *filename, const char *source, const char *build)
1382 {
1383     FILE *file;
1384     unsigned int line;
1385     size_t length;
1386     char buffer[BUFSIZ];
1387     const char *start;
1388     struct testlist *listhead, *current;
1389
1390     /* Create the initial container list that will hold our results. */
1391     listhead = xcalloc(1, struct testlist);
1392     current = NULL;
1393
1394     /*
1395      * Open our file of tests to run and read it line by line, creating a new
1396      * struct testlist and struct testset for each line.
1397      */
1398     file = fopen(filename, "r");
1399     if (file == NULL)
1400         sysdie("can't open %s", filename);
1401     line = 0;
1402     while (fgets(buffer, sizeof(buffer), file)) {
1403         line++;
1404         length = strlen(buffer) - 1;
1405         if (buffer[length] != '\n') {
1406             fprintf(stderr, "%s:%u: line too long\n", filename, line);
1407             exit(1);
1408         }
1409         buffer[length] = '\0';
1410
1411         /* Skip comments, leading spaces, and blank lines. */
1412         start = skip_whitespace(buffer);
1413         if (strlen(start) == 0)
1414             continue;
1415         if (start[0] == '#')
1416             continue;
1417
1418         /* Allocate the new testset structure. */
1419         if (current == NULL)
1420             current = listhead;
1421         else {
1422             current->next = xcalloc(1, struct testlist);
1423             current = current->next;
1424         }
1425         current->ts = xcalloc(1, struct testset);
1426         current->ts->plan = PLAN_INIT;
1427
1428         /* Parse the line and store the results in the testset struct. */
1429         parse_test_list_line(start, current->ts, source, build);
1430     }
1431     fclose(file);
1432
1433     /* If there were no tests, current is still NULL. */
1434     if (current == NULL) {
1435         free(listhead);
1436         return NULL;
1437     }
1438
1439     /* Return the results. */
1440     return listhead;
1441 }
1442
1443
1444 /*
1445  * Build a list of tests from command line arguments.  Takes the argv and argc
1446  * representing the command line arguments and returns a newly allocated test
1447  * list, or NULL if there were no tests.  The caller is responsible for
1448  * freeing.
1449  */
1450 static struct testlist *
1451 build_test_list(char *argv[], int argc, const char *source, const char *build)
1452 {
1453     int i;
1454     struct testlist *listhead, *current;
1455
1456     /* Create the initial container list that will hold our results. */
1457     listhead = xcalloc(1, struct testlist);
1458     current = NULL;
1459
1460     /* Walk the list of arguments and create test sets for them. */
1461     for (i = 0; i < argc; i++) {
1462         if (current == NULL)
1463             current = listhead;
1464         else {
1465             current->next = xcalloc(1, struct testlist);
1466             current = current->next;
1467         }
1468         current->ts = xcalloc(1, struct testset);
1469         current->ts->plan = PLAN_INIT;
1470         current->ts->file = xstrdup(argv[i]);
1471         current->ts->command = xcalloc(2, char *);
1472         current->ts->command[0] = find_test(current->ts->file, source, build);
1473         current->ts->command[1] = NULL;
1474     }
1475
1476     /* If there were no tests, current is still NULL. */
1477     if (current == NULL) {
1478         free(listhead);
1479         return NULL;
1480     }
1481
1482     /* Return the results. */
1483     return listhead;
1484 }
1485
1486
1487 /* Free a struct testset. */
1488 static void
1489 free_testset(struct testset *ts)
1490 {
1491     size_t i;
1492
1493     free(ts->file);
1494     for (i = 0; ts->command[i] != NULL; i++)
1495         free(ts->command[i]);
1496     free(ts->command);
1497     free(ts->results);
1498     free(ts->reason);
1499     free(ts);
1500 }
1501
1502
1503 /*
1504  * Run a batch of tests.  Takes two additional parameters: the root of the
1505  * source directory and the root of the build directory.  Test programs will
1506  * be first searched for in the current directory, then the build directory,
1507  * then the source directory.  Returns true iff all tests passed, and always
1508  * frees the test list that's passed in.
1509  */
1510 static int
1511 test_batch(struct testlist *tests, enum test_verbose verbose)
1512 {
1513     size_t length, i;
1514     size_t longest = 0;
1515     unsigned int count = 0;
1516     struct testset *ts;
1517     struct timeval start, end;
1518     struct rusage stats;
1519     struct testlist *failhead = NULL;
1520     struct testlist *failtail = NULL;
1521     struct testlist *current, *next;
1522     int succeeded;
1523     unsigned long total = 0;
1524     unsigned long passed = 0;
1525     unsigned long skipped = 0;
1526     unsigned long failed = 0;
1527     unsigned long aborted = 0;
1528
1529     /* Walk the list of tests to find the longest name. */
1530     for (current = tests; current != NULL; current = current->next) {
1531         length = strlen(current->ts->file);
1532         if (length > longest)
1533             longest = length;
1534     }
1535
1536     /*
1537      * Add two to longest and round up to the nearest tab stop.  This is how
1538      * wide the column for printing the current test name will be.
1539      */
1540     longest += 2;
1541     if (longest % 8)
1542         longest += 8 - (longest % 8);
1543
1544     /* Start the wall clock timer. */
1545     gettimeofday(&start, NULL);
1546
1547     /* Now, plow through our tests again, running each one. */
1548     for (current = tests; current != NULL; current = current->next) {
1549         ts = current->ts;
1550
1551         /* Print out the name of the test file. */
1552         fputs(ts->file, stdout);
1553         if (verbose)
1554             fputs("\n\n", stdout);
1555         else
1556             for (i = strlen(ts->file); i < longest; i++)
1557                 putchar('.');
1558         if (isatty(STDOUT_FILENO))
1559             fflush(stdout);
1560
1561         /* Run the test. */
1562         succeeded = test_run(ts, verbose);
1563         fflush(stdout);
1564         if (verbose)
1565             putchar('\n');
1566
1567         /* Record cumulative statistics. */
1568         aborted += ts->aborted;
1569         total += ts->count + ts->all_skipped;
1570         passed += ts->passed;
1571         skipped += ts->skipped + ts->all_skipped;
1572         failed += ts->failed;
1573         count++;
1574
1575         /* If the test fails, we shuffle it over to the fail list. */
1576         if (!succeeded) {
1577             if (failhead == NULL) {
1578                 failhead = xcalloc(1, struct testlist);
1579                 failtail = failhead;
1580             } else {
1581                 failtail->next = xcalloc(1, struct testlist);
1582                 failtail = failtail->next;
1583             }
1584             failtail->ts = ts;
1585             failtail->next = NULL;
1586         }
1587     }
1588     total -= skipped;
1589
1590     /* Stop the timer and get our child resource statistics. */
1591     gettimeofday(&end, NULL);
1592     getrusage(RUSAGE_CHILDREN, &stats);
1593
1594     /* Summarize the failures and free the failure list. */
1595     if (failhead != NULL) {
1596         test_fail_summary(failhead);
1597         while (failhead != NULL) {
1598             next = failhead->next;
1599             free(failhead);
1600             failhead = next;
1601         }
1602     }
1603
1604     /* Free the memory used by the test lists. */
1605     while (tests != NULL) {
1606         next = tests->next;
1607         free_testset(tests->ts);
1608         free(tests);
1609         tests = next;
1610     }
1611
1612     /* Print out the final test summary. */
1613     putchar('\n');
1614     if (aborted != 0) {
1615         if (aborted == 1)
1616             printf("Aborted %lu test set", aborted);
1617         else
1618             printf("Aborted %lu test sets", aborted);
1619         printf(", passed %lu/%lu tests", passed, total);
1620     } else if (failed == 0)
1621         fputs("All tests successful", stdout);
1622     else
1623         printf("Failed %lu/%lu tests, %.2f%% okay", failed, total,
1624                (double) (total - failed) * 100.0 / (double) total);
1625     if (skipped != 0) {
1626         if (skipped == 1)
1627             printf(", %lu test skipped", skipped);
1628         else
1629             printf(", %lu tests skipped", skipped);
1630     }
1631     puts(".");
1632     printf("Files=%u,  Tests=%lu", count, total);
1633     printf(",  %.2f seconds", tv_diff(&end, &start));
1634     printf(" (%.2f usr + %.2f sys = %.2f CPU)\n", tv_seconds(&stats.ru_utime),
1635            tv_seconds(&stats.ru_stime),
1636            tv_sum(&stats.ru_utime, &stats.ru_stime));
1637     return (failed == 0 && aborted == 0);
1638 }
1639
1640
1641 /*
1642  * Run a single test case.  This involves just running the test program after
1643  * having done the environment setup and finding the test program.
1644  */
1645 static void
1646 test_single(const char *program, const char *source, const char *build)
1647 {
1648     char *path;
1649
1650     path = find_test(program, source, build);
1651     if (execl(path, path, (char *) 0) == -1)
1652         sysdie("cannot exec %s", path);
1653 }
1654
1655
1656 /*
1657  * Main routine.  Set the C_TAP_SOURCE, C_TAP_BUILD, SOURCE, and BUILD
1658  * environment variables and then, given a file listing tests, run each test
1659  * listed.
1660  */
1661 int
1662 main(int argc, char *argv[])
1663 {
1664     int option;
1665     int status = 0;
1666     int single = 0;
1667     enum test_verbose verbose = CONCISE;
1668     char *c_tap_source_env = NULL;
1669     char *c_tap_build_env = NULL;
1670     char *source_env = NULL;
1671     char *build_env = NULL;
1672     const char *program;
1673     const char *shortlist;
1674     const char *list = NULL;
1675     const char *source = C_TAP_SOURCE;
1676     const char *build = C_TAP_BUILD;
1677     struct testlist *tests;
1678
1679     program = argv[0];
1680     while ((option = getopt(argc, argv, "b:hl:os:v")) != EOF) {
1681         switch (option) {
1682         case 'b':
1683             build = optarg;
1684             break;
1685         case 'h':
1686             printf(usage_message, program, program, program, usage_extra);
1687             exit(0);
1688         case 'l':
1689             list = optarg;
1690             break;
1691         case 'o':
1692             single = 1;
1693             break;
1694         case 's':
1695             source = optarg;
1696             break;
1697         case 'v':
1698             verbose = VERBOSE;
1699             break;
1700         default:
1701             exit(1);
1702         }
1703     }
1704     argv += optind;
1705     argc -= optind;
1706     if ((list == NULL && argc < 1) || (list != NULL && argc > 0)) {
1707         fprintf(stderr, usage_message, program, program, program, usage_extra);
1708         exit(1);
1709     }
1710
1711     /*
1712      * If C_TAP_VERBOSE is set in the environment, that also turns on verbose
1713      * mode.
1714      */
1715     if (getenv("C_TAP_VERBOSE") != NULL)
1716         verbose = VERBOSE;
1717
1718     /*
1719      * Set C_TAP_SOURCE and C_TAP_BUILD environment variables.  Also set
1720      * SOURCE and BUILD for backward compatibility, although we're trying to
1721      * migrate to the ones with a C_TAP_* prefix.
1722      */
1723     if (source != NULL) {
1724         c_tap_source_env = concat("C_TAP_SOURCE=", source, (const char *) 0);
1725         if (putenv(c_tap_source_env) != 0)
1726             sysdie("cannot set C_TAP_SOURCE in the environment");
1727         source_env = concat("SOURCE=", source, (const char *) 0);
1728         if (putenv(source_env) != 0)
1729             sysdie("cannot set SOURCE in the environment");
1730     }
1731     if (build != NULL) {
1732         c_tap_build_env = concat("C_TAP_BUILD=", build, (const char *) 0);
1733         if (putenv(c_tap_build_env) != 0)
1734             sysdie("cannot set C_TAP_BUILD in the environment");
1735         build_env = concat("BUILD=", build, (const char *) 0);
1736         if (putenv(build_env) != 0)
1737             sysdie("cannot set BUILD in the environment");
1738     }
1739
1740     /* Run the tests as instructed. */
1741     if (single)
1742         test_single(argv[0], source, build);
1743     else if (list != NULL) {
1744         shortlist = strrchr(list, '/');
1745         if (shortlist == NULL)
1746             shortlist = list;
1747         else
1748             shortlist++;
1749         printf(banner, shortlist);
1750         tests = read_test_list(list, source, build);
1751         status = test_batch(tests, verbose) ? 0 : 1;
1752     } else {
1753         tests = build_test_list(argv, argc, source, build);
1754         status = test_batch(tests, verbose) ? 0 : 1;
1755     }
1756
1757     /* For valgrind cleanliness, free all our memory. */
1758     if (source_env != NULL) {
1759         putenv((char *) "C_TAP_SOURCE=");
1760         putenv((char *) "SOURCE=");
1761         free(c_tap_source_env);
1762         free(source_env);
1763     }
1764     if (build_env != NULL) {
1765         putenv((char *) "C_TAP_BUILD=");
1766         putenv((char *) "BUILD=");
1767         free(c_tap_build_env);
1768         free(build_env);
1769     }
1770     exit(status);
1771 }