chiark / gitweb /
@@@ tvec doc wip
[mLib] / test / tvec-output.c
1 /* -*-c-*-
2  *
3  * Test vector output management
4  *
5  * (c) 2023 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU Library General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or (at
15  * your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib.  If not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25  * USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <ctype.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include <unistd.h>
39
40 #include "alloc.h"
41 #include "bench.h"
42 #include "dstr.h"
43 #include "macros.h"
44 #include "quis.h"
45 #include "report.h"
46 #include "tvec.h"
47
48 /*----- Common machinery --------------------------------------------------*/
49
50 /* --- @regdisp@ --- *
51  *
52  * Arguments:   @unsigned disp@ = a @TVRD_...@ disposition code
53  *
54  * Returns:     A human-readable adjective describing the register
55  *              disposition.
56  */
57
58 static const char *regdisp(unsigned disp)
59 {
60   switch (disp) {
61     case TVRD_INPUT: return "input";
62     case TVRD_OUTPUT: return "output";
63     case TVRD_MATCH: return "matched";
64     case TVRD_EXPECT: return "expected";
65     case TVRD_FOUND: return "found";
66     default: abort();
67   }
68 }
69
70 /* --- @getenv_boolean@ --- *
71  *
72  * Arguments:   @const char *var@ = environment variable name
73  *              @int dflt@ = default value
74  *
75  * Returns:     @0@ if the variable is set to something falseish, @1@ if it's
76  *              set to something truish, or @dflt@ otherwise.
77  */
78
79 static int getenv_boolean(const char *var, int dflt)
80 {
81   const char *p;
82
83   p = getenv(var);
84   if (!p)
85     return (dflt);
86   else if (STRCMP(p, ==, "y") || STRCMP(p, ==, "yes") ||
87            STRCMP(p, ==, "t") || STRCMP(p, ==, "true") ||
88            STRCMP(p, ==, "on") || STRCMP(p, ==, "force") ||
89            STRCMP(p, ==, "1"))
90     return (1);
91   else if (STRCMP(p, ==, "n") || STRCMP(p, ==, "no") ||
92            STRCMP(p, ==, "f") || STRCMP(p, ==, "false") ||
93            STRCMP(p, ==, "nil") || STRCMP(p, ==, "off") ||
94            STRCMP(p, ==, "0"))
95     return (0);
96   else {
97     moan("ignoring unexpected value `%s' for environment variable `%s'",
98          var, p);
99     return (dflt);
100   }
101 }
102
103 /* --- @register_maxnamelen@ --- *
104  *
105  * Arguments:   @const struct tvec_state *tv@ = test vector state
106  *
107  * Returns:     The maximum length of a register name in the current test.
108  */
109
110 static int register_maxnamelen(const struct tvec_state *tv)
111 {
112   const struct tvec_regdef *rd;
113   int maxlen = 10, n;
114
115   for (rd = tv->test->regs; rd->name; rd++)
116     { n = strlen(rd->name); if (n > maxlen) maxlen = n; }
117   return (maxlen);
118 }
119
120 /*----- Output layout -----------------------------------------------------*/
121
122 /* We have two main jobs in output layout: trimming trailing blanks; and
123  * adding a prefix to each line.
124  *
125  * This is somehow much more complicated than it ought to be.
126  */
127
128 struct layout {
129   FILE *fp;                              /* output file */
130   const char *prefix, *pfxtail, *pfxlim; /* prefix pointers */
131   dstr w;                               /* trailing whitespace */
132   unsigned f;                           /* flags */
133 #define LYTF_NEWL 1u                    /*   start of output line */
134 };
135
136 /* Support macros.  These assume `lyt' is defined as a pointer to the `struct
137  * layout' state.
138  */
139
140 #define SPLIT_RANGE(tail, base, limit) do {                             \
141   /* Set TAIL to point just after the last nonspace character between   \
142    * BASE and LIMIT.  If there are no nonspace characters, then set     \
143    * TAIL to equal BASE.                                                \
144    */                                                                   \
145                                                                         \
146   for (tail = limit; tail > base && ISSPACE(tail[-1]); tail--);         \
147 } while (0)
148
149 #define PUT_RANGE(base, limit) do {                                     \
150   /* Write the range of characters between BASE and LIMIT to the output \
151    * file.  Return immediately on error.                                \
152    */                                                                   \
153                                                                         \
154   size_t _n = limit - base;                                             \
155   if (_n && fwrite(base, 1, _n, lyt->fp) < _n) return (-1);             \
156 } while (0)
157
158 #define PUT_CHAR(ch) do {                                               \
159   /* Write CH to the output. Return immediately on error. */            \
160                                                                         \
161   if (putc(ch, lyt->fp) == EOF) return (-1);                            \
162 } while (0)
163
164 #define PUT_PREFIX do {                                                 \
165   /* Output the prefix, if there is one.  Return immediately on error. */ \
166                                                                         \
167   if (lyt->prefix) PUT_RANGE(lyt->prefix, lyt->pfxlim);                 \
168 } while (0)
169
170 #define PUT_SAVED do {                                                  \
171   /* Output the saved trailing blank material in the buffer. */         \
172                                                                         \
173   size_t _n = lyt->w.len;                                               \
174   if (_n && fwrite(lyt->w.buf, 1, _n, lyt->fp) < _n) return (-1);       \
175 } while (0)
176
177 #define PUT_PFXINB do {                                                 \
178   /* Output the initial nonblank portion of the prefix, if there is     \
179    * one.  Return immediately on error.                                 \
180    */                                                                   \
181                                                                         \
182   if (lyt->prefix) PUT_RANGE(lyt->prefix, lyt->pfxtail);                \
183 } while (0)
184
185 #define SAVE_PFXTAIL do {                                               \
186   /* Save the trailing blank portion of the prefix. */                  \
187                                                                         \
188   if (lyt->prefix)                                                      \
189     DPUTM(&lyt->w, lyt->pfxtail, lyt->pfxlim - lyt->pfxtail);           \
190 } while (0)
191
192 /* --- @set_layout_prefix@ --- *
193  *
194  * Arguments:   @struct layout *lyt@ = layout state
195  *              @const char *prefix@ = new prefix string or null
196  *
197  * Returns:     ---
198  *
199  * Use:         Change the configured prefix string.  The change takes effect
200  *              at the start of the next line (or the current line if it's
201  *              empty or only whitespace so far).
202  */
203
204 static void set_layout_prefix(struct layout *lyt, const char *prefix)
205 {
206   const char *q, *l;
207
208  if (!prefix || !*prefix)
209     lyt->prefix = lyt->pfxtail = lyt->pfxlim = 0;
210   else {
211     lyt->prefix = prefix;
212     l = lyt->pfxlim = prefix + strlen(prefix);
213     SPLIT_RANGE(q, prefix, l); lyt->pfxtail = q;
214   }
215 }
216
217 /* --- @init_layout@ --- *
218  *
219  * Arguments:   @struct layout *lyt@ = layout state to initialize
220  *              @FILE *fp@ = output file
221  *              @const char *prefix@ = prefix string (or null if empty)
222  *
223  * Returns:     ---
224  *
225  * Use:         Initialize a layout state.
226  */
227
228 static void init_layout(struct layout *lyt, FILE *fp, const char *prefix)
229 {
230   lyt->fp = fp;
231   lyt->f = LYTF_NEWL;
232   dstr_create(&lyt->w);
233   set_layout_prefix(lyt, prefix);
234 }
235
236 /* --- @destroy_layout@ --- *
237  *
238  * Arguments:   @struct layout *lyt@ = layout state
239  *              @unsigned f@ = flags (@DLF_...@)
240  *
241  * Returns:     ---
242  *
243  * Use:         Releases a layout state and the resources it holds.
244  *              Close the file if @DLF_CLOSE@ is set in @f@; otherwise leave
245  *              it open (in case it's @stderr@ or something).
246  */
247
248 #define DLF_CLOSE 1u
249 static void destroy_layout(struct layout *lyt, unsigned f)
250 {
251   if (f&DLF_CLOSE) fclose(lyt->fp);
252   dstr_destroy(&lyt->w);
253 }
254
255 /* --- @layout_char@ --- *
256  *
257  * Arguments:   @struct layout *lyt@ = layout state
258  *              @int ch@ = character to write
259  *
260  * Returns:     Zero on success, @-1@ on failure.
261  *
262  * Use:         Write a single character to the output.
263  */
264
265 static int layout_char(struct layout *lyt, int ch)
266 {
267   if (ch == '\n') {
268     if (lyt->f&LYTF_NEWL) PUT_PFXINB;
269     PUT_CHAR('\n'); lyt->f |= LYTF_NEWL; DRESET(&lyt->w);
270   } else if (isspace(ch))
271     DPUTC(&lyt->w, ch);
272   else {
273     if (lyt->f&LYTF_NEWL) { PUT_PFXINB; lyt->f &= ~LYTF_NEWL; }
274     PUT_SAVED; PUT_CHAR(ch); DRESET(&lyt->w);
275   }
276   return (0);
277 }
278
279 /* --- @layout_string@ --- *
280  *
281  * Arguments:   @struct layout *lyt@ = layout state
282  *              @const char *p@ = string to write
283  *              @size_t sz@ = length of string
284  *
285  * Returns:     Zero on success, @-1@ on failure.
286  *
287  * Use:         Write a string to the output.
288  */
289
290 static int layout_string(struct layout *lyt, const char *p, size_t sz)
291 {
292   const char *q, *r, *l = p + sz;
293
294   /* This is rather vexing.  There are a small number of jobs to do, but the
295    * logic for deciding which to do when gets rather hairy if, as I've tried
296    * here, one aims to minimize the number of decisions being checked, so
297    * it's worth canning them into macros.
298    *
299    * Here, a `blank' is a whitespace character other than newline.  The input
300    * buffer consists of one or more `segments', each of which consists of:
301    *
302    *   * an initial portion, which is either empty or ends with a nonblank
303    *     character;
304    *
305    *   * a suffix which consists only of blanks; and
306    *
307    *   * an optional newline.
308    *
309    * All segments except the last end with a newline.
310    */
311
312 #define SPLIT_SEGMENT do {                                              \
313   /* Determine the bounds of the current segment.  If there is a final  \
314    * newline, then q is non-null and points to this newline; otherwise, \
315    * q is null.  The initial portion of the segment lies between p .. r \
316    * and the blank suffix lies between r .. q (or r .. l if q is null). \
317    * This sounds awkward, but the suffix is only relevant if there is   \
318    * no newline.                                                        \
319    */                                                                   \
320                                                                         \
321   q = memchr(p, '\n', l - p); SPLIT_RANGE(r, p, q ? q : l);             \
322 } while (0)
323
324 #define PUT_NONBLANK do {                                               \
325   /* Output the initial portion of the segment. */                      \
326                                                                         \
327   PUT_RANGE(p, r);                                                      \
328 } while (0)
329
330 #define PUT_NEWLINE do {                                                \
331   /* Write a newline, and advance to the next segment. */               \
332                                                                         \
333   PUT_CHAR('\n'); p = q + 1;                                            \
334 } while (0)
335
336 #define SAVE_TAIL do {                                                  \
337   /* Save the trailing blank portion of the segment in the buffer.      \
338    * Assumes that there is no newline, since otherwise the suffix would \
339    * be omitted.                                                        \
340    */                                                                   \
341                                                                         \
342   DPUTM(&lyt->w, r, l - r);                                             \
343 } while (0)
344
345   /* Determine the bounds of the first segment.  Handling this is the most
346    * complicated part of this function.
347    */
348   SPLIT_SEGMENT;
349
350   if (!q) {
351     /* This is the only segment.  We'll handle the whole thing here.
352      *
353      * If there's an initial nonblank portion, then we need to write that
354      * out.  Furthermore, if we're at the start of the line then we'll need
355      * to write the prefix, and if there's saved blank material then we'll
356      * need to write that.  Otherwise, there's only blank stuff, which we
357      * accumulate in the buffer.
358      *
359      * If we're at the start of a line here, then put the prefix followed by
360      * any saved whitespace, and then our initial nonblank portion.  Then
361      * save our new trailing space.
362      */
363
364     if (r > p) {
365       if (lyt->f&LYTF_NEWL) { PUT_PREFIX; lyt->f &= ~LYTF_NEWL; }
366       PUT_SAVED; PUT_NONBLANK; DRESET(&lyt->w);
367     }
368     SAVE_TAIL;
369     return (0);
370   }
371
372   /* There is at least one more segment, so we know that there'll be a line
373    * to output.
374    */
375   if (r > p) {
376     if (lyt->f&LYTF_NEWL) PUT_PREFIX;
377     PUT_SAVED; PUT_NONBLANK;
378   } else if (lyt->f&LYTF_NEWL)
379     PUT_PFXINB;
380   PUT_NEWLINE; DRESET(&lyt->w);
381   SPLIT_SEGMENT;
382
383   /* Main loop over whole segments with trailing newlines.  For each one, we
384    * know that we're starting at the beginning of a line and there's a final
385    * newline, so we write the initial prefix and drop the trailing blanks.
386    */
387   while (q) {
388     if (r > p) { PUT_PREFIX; PUT_NONBLANK; }
389     else PUT_PFXINB;
390     PUT_NEWLINE;
391     SPLIT_SEGMENT;
392   }
393
394   /* At the end, there's no final newline.  If there's nonblank material,
395    * then we can write the prefix and the nonblank stuff.  Otherwise, stash
396    * the blank stuff (including the trailing blanks of the prefix) and leave
397    * the newline flag set.
398    */
399   if (r > p) { PUT_PREFIX; PUT_NONBLANK; lyt->f &= ~LYTF_NEWL; }
400   else { lyt->f |= LYTF_NEWL; SAVE_PFXTAIL; }
401   SAVE_TAIL;
402
403 #undef SPLIT_SEGMENT
404 #undef PUT_NONBLANK
405 #undef PUT_NEWLINE
406 #undef SAVE_TAIL
407
408   return (0);
409 }
410
411 #undef SPLIT_RANGE
412 #undef PUT_RANGE
413 #undef PUT_PREFIX
414 #undef PUT_PFXINB
415 #undef PUT_SAVED
416 #undef PUT_CHAR
417 #undef SAVE_PFXTAIL
418
419 /*----- Human-readable output ---------------------------------------------*/
420
421 /* Attributes for colour output.  This should be done better, but @terminfo@
422  * is a disaster.
423  *
424  * An attribute byte holds a foreground colour in the low nibble, a
425  * background colour in the next nibble, and some flags in the next few
426  * bits.  A colour is expressed in classic 1-bit-per-channel style, with red,
427  * green, and blue in bits 0, 1, and 2, and a `bright' flag in bit 3.
428  */
429 #define HAF_FGMASK 0x0f                 /* foreground colour mask */
430 #define HAF_FGSHIFT 0                   /* foreground colour shift */
431 #define HAF_BGMASK 0xf0                 /* background colour mask */
432 #define HAF_BGSHIFT 4                   /* background colour shift */
433 #define HAF_FG 256u                     /* set foreground? */
434 #define HAF_BG 512u                     /* set background? */
435 #define HAF_BOLD 1024u                  /* set bold? */
436 #define HCOL_BLACK 0u                   /* colour codes... */
437 #define HCOL_RED 1u
438 #define HCOL_GREEN 2u
439 #define HCOL_YELLOW 3u
440 #define HCOL_BLUE 4u
441 #define HCOL_MAGENTA 5u
442 #define HCOL_CYAN 6u
443 #define HCOL_WHITE 7u
444 #define HCF_BRIGHT 8u                   /* bright colour flag */
445 #define HFG(col) (HAF_FG | (HCOL_##col) << HAF_FGSHIFT) /* set foreground */
446 #define HBG(col) (HAF_BG | (HCOL_##col) << HAF_BGSHIFT) /* set background */
447
448 /* Predefined attributes. */
449 #define HA_PLAIN 0                   /* nothing special: terminal defaults */
450 #define HA_LOC (HFG(CYAN))              /* filename or line number */
451 #define HA_LOCSEP (HFG(BLUE))           /* location separator `:' */
452 #define HA_ERR (HFG(MAGENTA) | HAF_BOLD) /* error messages */
453 #define HA_NOTE (HFG(YELLOW))           /* notices */
454 #define HA_UNKLEV (HFG(WHITE) | HBG(RED) | HAF_BOLD) /* unknown level */
455 #define HA_UNSET (HFG(YELLOW))          /* register not set */
456 #define HA_FOUND (HFG(RED))             /* incorrect output value */
457 #define HA_EXPECT (HFG(GREEN))          /* what the value should have been */
458 #define HA_WIN (HFG(GREEN))             /* reporting success */
459 #define HA_LOSE (HFG(RED) | HAF_BOLD)   /* reporting failure */
460 #define HA_XFAIL (HFG(BLUE) | HAF_BOLD) /* reporting expected failure */
461 #define HA_SKIP (HFG(YELLOW))           /* reporting a skipped test/group */
462
463 /* Scoreboard indicators. */
464 #define HSB_WIN '.'                     /* test passed */
465 #define HSB_LOSE 'x'                    /* test failed */
466 #define HSB_XFAIL 'o'                   /* test failed expectedly */
467 #define HSB_SKIP '_'                    /* test wasn't run */
468
469 struct human_output {
470   struct tvec_output _o;                /* output base class */
471   struct tvec_state *tv;                /* stashed testing state */
472   struct layout lyt;                    /* output layout */
473   char *outbuf; size_t outsz;           /* buffer for formatted output */
474   dstr scoreboard;                      /* history of test group results */
475   unsigned attr;                        /* current terminal attributes */
476   int maxlen;                           /* longest register name */
477   unsigned f;                           /* flags */
478 #define HOF_TTY 1u                      /*   writing to terminal */
479 #define HOF_DUPERR 2u                   /*   duplicate errors to stderr */
480 #define HOF_COLOUR 4u                   /*   print in angry fruit salad */
481 #define HOF_PROGRESS 8u                 /*   progress display is active */
482 };
483
484 /* --- @set_colour@ --- *
485  *
486  * Arguments:   @FILE *fp@ = output stream to write on
487  *              @int *sep_inout@ = where to maintain separator
488  *              @const char *norm@ = prefix for normal colour
489  *              @const char *bright@ = prefix for bright colour
490  *              @unsigned colour@ = four bit colour code
491  *
492  * Returns:     ---
493  *
494  * Use:         Write to the output stream @fp@, the current character at
495  *              @*sep_inout@, if that's not zero, followed by either @norm@
496  *              or @bright@, according to whether the @HCF_BRIGHT@ flag is
497  *              set in @colour@, followed by the plain colour code from
498  *              @colour@; finally, update @*sep_inout@ to be a `%|;|%'.
499  *
500  *              This is an internal subroutine for @setattr@ below.
501  */
502
503 static void set_colour(FILE *fp, int *sep_inout,
504                        const char *norm, const char *bright,
505                        unsigned colour)
506 {
507   if (*sep_inout) putc(*sep_inout, fp);
508   fprintf(fp, "%s%d", colour&HCF_BRIGHT ? bright : norm, colour&7);
509   *sep_inout = ';';
510 }
511
512 /* --- @setattr@ --- *
513  *
514  * Arguments:   @struct human_output *h@ = output state
515  *              @unsigned attr@ = attribute code to set
516  *
517  * Returns:     ---
518  *
519  * Use:         Send a control sequence to the output stream so that
520  *              subsequent text is printed with the given attributes.
521  *
522  *              Some effort is taken to avoid unnecessary control sequences.
523  *              In particular, if @attr@ matches the current terminal
524  *              settings already, then nothing is written.
525  */
526
527 static void setattr(struct human_output *h, unsigned attr)
528 {
529   unsigned diff = h->attr ^ attr;
530   int sep = 0;
531
532   /* If there's nothing to do, we might as well stop now. */
533   if (!diff || !(h->f&HOF_COLOUR)) return;
534
535   /* Start on the control command. */
536   fputs("\x1b[", h->lyt.fp);
537
538   /* Change the boldness if necessary. */
539   if (diff&HAF_BOLD) {
540     if (attr&HAF_BOLD) putc('1', h->lyt.fp);
541     else { putc('0', h->lyt.fp); diff = h->attr; }
542     sep = ';';
543   }
544
545   /* Change the foreground colour if necessary. */
546   if (diff&(HAF_FG | HAF_FGMASK)) {
547     if (attr&HAF_FG)
548       set_colour(h->lyt.fp, &sep, "3", "9",
549                  (attr&HAF_FGMASK) >> HAF_FGSHIFT);
550     else {
551       if (sep) putc(sep, h->lyt.fp);
552       fputs("39", h->lyt.fp); sep = ';';
553     }
554   }
555
556   /* Change the background colour if necessary. */
557   if (diff&(HAF_BG | HAF_BGMASK)) {
558     if (attr&HAF_BG)
559       set_colour(h->lyt.fp, &sep, "4", "10",
560                  (attr&HAF_BGMASK) >> HAF_BGSHIFT);
561     else {
562       if (sep) putc(sep, h->lyt.fp);
563       fputs("49", h->lyt.fp); sep = ';';
564     }
565   }
566
567   /* Terminate the control command and save the new attributes. */
568   putc('m', h->lyt.fp); h->attr = attr;
569 }
570
571 /* --- @clear_progress@ --- *
572  *
573  * Arguments:   @struct human_output *h@ = output state
574  *
575  * Returns:     ---
576  *
577  * Use:         Remove the progress display from the terminal.
578  *
579  *              If the progress display isn't active then do nothing.
580  */
581
582 static void clear_progress(struct human_output *h)
583 {
584   size_t i, n;
585
586   if (h->f&HOF_PROGRESS) {
587     n = strlen(h->tv->test->name) + 2 + h->scoreboard.len;
588     for (i = 0; i < n; i++) fputs("\b \b", h->lyt.fp);
589     h->f &= ~HOF_PROGRESS;
590   }
591 }
592
593 /* --- @write_scoreboard_char@ --- *
594  *
595  * Arguments:   @struct human_output *h@ = output state
596  *              @int ch@ = scoreboard character to print
597  *
598  * Returns:     ---
599  *
600  * Use:         Write a scoreboard character, indicating the outcome of a
601  *              test, to the output stream, with appropriate highlighting.
602  */
603
604 static void write_scoreboard_char(struct human_output *h, int ch)
605 {
606   switch (ch) {
607     case HSB_LOSE: setattr(h, HA_LOSE); break;
608     case HSB_SKIP: setattr(h, HA_SKIP); break;
609     case HSB_XFAIL: setattr(h, HA_XFAIL); break;
610     default: setattr(h, HA_PLAIN); break;
611   }
612   putc(ch, h->lyt.fp); setattr(h, HA_PLAIN);
613 }
614
615 /* --- @show_progress@ --- *
616  *
617  * Arguments:   @struct human_output *h@ = output state
618  *
619  * Returns:     ---
620  *
621  * Use:         Show the progress display, with the record of outcomes for
622  *              the current test group.
623  *
624  *              If the progress display is already active, or the output
625  *              stream is not interactive, then nothing happens.
626  */
627
628 static void show_progress(struct human_output *h)
629 {
630   struct tvec_state *tv = h->tv;
631   const char *p, *l;
632
633   if (tv->test && (h->f&HOF_TTY) && !(h->f&HOF_PROGRESS)) {
634     fprintf(h->lyt.fp, "%s: ", tv->test->name);
635     if (!(h->f&HOF_COLOUR))
636       dstr_write(&h->scoreboard, h->lyt.fp);
637     else for (p = h->scoreboard.buf, l = p + h->scoreboard.len; p < l; p++)
638       write_scoreboard_char(h, *p);
639     fflush(h->lyt.fp); h->f |= HOF_PROGRESS;
640   }
641 }
642
643 /* --- @human_writech@, @human_write@, @human_writef@ --- *
644  *
645  * Arguments:   @void *go@ = output sink, secretly a @struct human_output@
646  *              @int ch@ = character to write
647  *              @const char *@p@, @size_t sz@ = string (with explicit length)
648  *                      to write
649  *              @const char *p, ...@ = format control string and arguments to
650  *                      write
651  *
652  * Returns:     ---
653  *
654  * Use:         Write characters, strings, or formatted strings to the
655  *              output, applying appropriate layout.
656  *
657  *              For the human output driver, the layout machinery just strips
658  *              trailing spaces.
659  */
660
661 static int human_writech(void *go, int ch)
662   { struct human_output *h = go; return (layout_char(&h->lyt, ch)); }
663
664 static int human_writem(void *go, const char *p, size_t sz)
665   { struct human_output *h = go; return (layout_string(&h->lyt, p, sz)); }
666
667 static int human_nwritef(void *go, size_t maxsz, const char *p, ...)
668 {
669   struct human_output *h = go;
670   size_t n;
671   va_list ap;
672
673   va_start(ap, p);
674   n = gprintf_memputf(&h->outbuf, &h->outsz, maxsz, p, ap);
675   va_end(ap);
676   return (layout_string(&h->lyt, h->outbuf, n));
677 }
678
679 static const struct gprintf_ops human_printops =
680   { human_writech, human_writem, human_nwritef };
681
682 /* --- @human_bsession@ --- *
683  *
684  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
685  *                      human_output@
686  *              @struct tvec_state *tv@ = the test state producing output
687  *
688  * Returns:     ---
689  *
690  * Use:         Begin a test session.
691  *
692  *              The human driver just records the test state for later
693  *              reference.
694  */
695
696 static void human_bsession(struct tvec_output *o, struct tvec_state *tv)
697   { struct human_output *h = (struct human_output *)o; h->tv = tv; }
698
699 /* --- @report_unusual@ --- *
700  *
701  * Arguments:   @struct human_output *h@ = output sink
702  *              @unsigned nxfail, nskip@ = number of expected failures and
703  *                      skipped tests
704  *
705  * Returns:     ---
706  *
707  * Use:         Write (directly on the output stream) a note about expected
708  *              failures and/or skipped tests, if there were any.
709  */
710
711 static void report_unusual(struct human_output *h,
712                            unsigned nxfail, unsigned nskip)
713 {
714   const char *sep = " (";
715   unsigned f = 0;
716 #define f_any 1u
717
718   if (nxfail) {
719     fprintf(h->lyt.fp, "%s%u ", sep, nxfail);
720     setattr(h, HA_XFAIL);
721     fprintf(h->lyt.fp, "expected %s", nxfail == 1 ? "failure" : "failures");
722     setattr(h, HA_PLAIN);
723     sep = ", "; f |= f_any;
724   }
725
726   if (nskip) {
727     fprintf(h->lyt.fp, "%s%u ", sep, nskip);
728     setattr(h, HA_SKIP); fputs("skipped", h->lyt.fp); setattr(h, HA_PLAIN);
729     sep = ", "; f |= f_any;
730   }
731
732   if (f&f_any) fputc(')', h->lyt.fp);
733
734 #undef f_any
735 }
736
737 /* --- @human_esession@ --- *
738  *
739  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
740  *                      human_output@
741  *
742  * Returns:     Suggested exit code.
743  *
744  * Use:         End a test session.
745  *
746  *              The human driver prints a final summary of the rest results
747  *              and returns a suitable exit code.
748  */
749
750 static int human_esession(struct tvec_output *o)
751 {
752   struct human_output *h = (struct human_output *)o;
753   struct tvec_state *tv = h->tv;
754   unsigned
755     all_win = tv->all[TVOUT_WIN], grps_win = tv->grps[TVOUT_WIN],
756     all_xfail = tv->all[TVOUT_XFAIL],
757     all_lose = tv->all[TVOUT_LOSE], grps_lose = tv->grps[TVOUT_LOSE],
758     all_skip = tv->all[TVOUT_SKIP], grps_skip = tv->grps[TVOUT_SKIP],
759     all_pass = all_win + all_xfail, all_run = all_pass + all_lose,
760     grps_run = grps_win + grps_lose;
761
762   if (!all_lose) {
763     setattr(h, HA_WIN); fputs("PASSED", h->lyt.fp); setattr(h, HA_PLAIN);
764     fprintf(h->lyt.fp, " %s%u %s",
765             !(all_skip || grps_skip) ? "all " : "",
766             all_pass, all_pass == 1 ? "test" : "tests");
767     report_unusual(h, all_xfail, all_skip);
768     fprintf(h->lyt.fp, " in %u %s",
769             grps_win, grps_win == 1 ? "group" : "groups");
770     report_unusual(h, 0, grps_skip);
771   } else {
772     setattr(h, HA_LOSE); fputs("FAILED", h->lyt.fp); setattr(h, HA_PLAIN);
773     fprintf(h->lyt.fp, " %u out of %u %s",
774             all_lose, all_run, all_run == 1 ? "test" : "tests");
775     report_unusual(h, all_xfail, all_skip);
776     fprintf(h->lyt.fp, " in %u out of %u %s",
777             grps_lose, grps_run, grps_run == 1 ? "group" : "groups");
778     report_unusual(h, 0, grps_skip);
779   }
780   fputc('\n', h->lyt.fp);
781
782   if (tv->f&TVSF_ERROR) {
783     setattr(h, HA_ERR); fputs("ERRORS", h->lyt.fp); setattr(h, HA_PLAIN);
784     fputs(" found in input; tests may not have run correctly\n", h->lyt.fp);
785   }
786
787   h->tv = 0; return (tv->f&TVSF_ERROR ? 2 : all_lose ? 1 : 0);
788 }
789
790 /* --- @human_bgroup@ --- *
791  *
792  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
793  *                      human_output@
794  *
795  * Returns:     ---
796  *
797  * Use:         Begin a test group.
798  *
799  *              The human driver determines the length of the longest
800  *              register name, resets the group progress scoreboard, and
801  *              activates the progress display.
802  */
803
804 static void human_bgroup(struct tvec_output *o)
805 {
806   struct human_output *h = (struct human_output *)o;
807
808   h->maxlen = register_maxnamelen(h->tv);
809   dstr_reset(&h->scoreboard); show_progress(h);
810 }
811
812 /* --- @human_skipgroup@ --- *
813  *
814  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
815  *                      human_output@
816  *              @const char *excuse@, @va_list *ap@ = reason for skipping the
817  *                      group, or null
818  *
819  * Returns:     ---
820  *
821  * Use:         Report that a test group is being skipped.
822  *
823  *              The human driver just reports the situation to its output
824  *              stream.
825  */
826
827 static void human_skipgroup(struct tvec_output *o,
828                             const char *excuse, va_list *ap)
829 {
830   struct human_output *h = (struct human_output *)o;
831
832   if (!(h->f&HOF_TTY))
833     fprintf(h->lyt.fp, "%s ", h->tv->test->name);
834   else {
835     show_progress(h); h->f &= ~HOF_PROGRESS;
836     if (h->scoreboard.len) putc(' ', h->lyt.fp);
837   }
838   setattr(h, HA_SKIP); fputs("skipped", h->lyt.fp); setattr(h, HA_PLAIN);
839   if (excuse) { fputs(": ", h->lyt.fp); vfprintf(h->lyt.fp, excuse, *ap); }
840   fputc('\n', h->lyt.fp);
841 }
842
843 /* --- @human_egroup@ --- *
844  *
845  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
846  *                      human_output@
847  *
848  * Returns:     ---
849  *
850  * Use:         Report that a test group has finished.
851  *
852  *              The human driver reports a summary of the group's tests.
853  */
854
855 static void human_egroup(struct tvec_output *o)
856 {
857   struct human_output *h = (struct human_output *)o;
858   struct tvec_state *tv = h->tv;
859   unsigned win = tv->curr[TVOUT_WIN], xfail = tv->curr[TVOUT_XFAIL],
860     lose = tv->curr[TVOUT_LOSE], skip = tv->curr[TVOUT_SKIP],
861     run = win + lose + xfail;
862
863   if (h->f&HOF_TTY) h->f &= ~HOF_PROGRESS;
864   else fprintf(h->lyt.fp, "%s:", h->tv->test->name);
865
866   if (lose) {
867     fprintf(h->lyt.fp, " %u/%u ", lose, run);
868     setattr(h, HA_LOSE); fputs("FAILED", h->lyt.fp); setattr(h, HA_PLAIN);
869     report_unusual(h, xfail, skip);
870   } else {
871     fputc(' ', h->lyt.fp); setattr(h, HA_WIN);
872     fputs("ok", h->lyt.fp); setattr(h, HA_PLAIN);
873     report_unusual(h, xfail, skip);
874   }
875   fputc('\n', h->lyt.fp);
876 }
877
878 /* --- @human_btest@ --- *
879  *
880  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
881  *                      human_output@
882  *
883  * Returns:     ---
884  *
885  * Use:         Report that a test is starting.
886  *
887  *              The human driver makes sure the progress display is active.
888  */
889
890 static void human_btest(struct tvec_output *o)
891   { struct human_output *h = (struct human_output *)o; show_progress(h); }
892
893 /* --- @report_location@ --- *
894  *
895  * Arguments:   @struct human_output *h@ = output state
896  *              @FILE *fp@ = stream to write the location on
897  *              @const char *file@ = filename
898  *              @unsigned lno@ = line number
899  *
900  * Returns:     ---
901  *
902  * Use:         Print the filename and line number to the output stream @fp@.
903  *              Also, if appropriate, print interleaved highlighting control
904  *              codes to our usual output stream.  If @file@ is null then do
905  *              nothing.
906  */
907
908 static void report_location(struct human_output *h, FILE *fp,
909                             const char *file, unsigned lno)
910 {
911   unsigned f = 0;
912 #define f_flush 1u
913
914   /* We emit highlighting if @fp@ is our usual output stream, or the
915    * duplicate-errors flag is clear indicating that (we assume) they're
916    * secretly going to the same place anyway.  If they're different streams,
917    * though, we have to be careful to keep the highlighting and the actual
918    * text synchronized.
919    */
920
921   if (!file)
922     /* nothing to do */;
923   else if (fp != h->lyt.fp && (h->f&HOF_DUPERR))
924     fprintf(fp, "%s:%u: ", file, lno);
925   else {
926     if (fp != h->lyt.fp) f |= f_flush;
927
928 #define FLUSH(fp) do if (f&f_flush) fflush(fp); while (0)
929
930     setattr(h, HA_LOC);         FLUSH(h->lyt.fp);
931     fputs(file, fp);            FLUSH(fp);
932     setattr(h, HA_LOCSEP);      FLUSH(h->lyt.fp);
933     fputc(':', fp);             FLUSH(fp);
934     setattr(h, HA_LOC);         FLUSH(h->lyt.fp);
935     fprintf(fp, "%u", lno);     FLUSH(fp);
936     setattr(h, HA_LOCSEP);      FLUSH(h->lyt.fp);
937     fputc(':', fp);             FLUSH(fp);
938     setattr(h, HA_PLAIN);       FLUSH(h->lyt.fp);
939     fputc(' ', fp);
940
941 #undef FLUSH
942   }
943
944 #undef f_flush
945 }
946
947 /* --- @human_outcome@, @human_skip@, @human_fail@ --- *
948  *
949  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
950  *                      human_output@
951  *              @unsigned attr@ = attribute to apply to the outcome
952  *              @const char *outcome@ = outcome string to report
953  *              @const char *detail@, @va_list *ap@ = a detail message
954  *              @const char *excuse@, @va_list *ap@ = reason for skipping the
955  *                      test
956  *
957  * Returns:     ---
958  *
959  * Use:         Report that a test has been skipped or failed.
960  *
961  *              The human driver reports the situation on its output stream.
962  */
963
964 static void human_outcome(struct tvec_output *o,
965                           unsigned attr, const char *outcome,
966                           const char *detail, va_list *ap)
967 {
968   struct human_output *h = (struct human_output *)o;
969   struct tvec_state *tv = h->tv;
970
971   clear_progress(h);
972   report_location(h, h->lyt.fp, tv->infile, tv->test_lno);
973   fprintf(h->lyt.fp, "`%s' ", tv->test->name);
974   setattr(h, attr); fputs(outcome, h->lyt.fp); setattr(h, HA_PLAIN);
975   if (detail) { fputs(": ", h->lyt.fp); vfprintf(h->lyt.fp, detail, *ap); }
976   fputc('\n', h->lyt.fp);
977 }
978
979 static void human_skip(struct tvec_output *o,
980                        const char *excuse, va_list *ap)
981   { human_outcome(o, HA_SKIP, "skipped", excuse, ap); }
982 static void human_fail(struct tvec_output *o,
983                        const char *detail, va_list *ap)
984   { human_outcome(o, HA_LOSE, "FAILED", detail, ap); }
985
986 /* --- @human_dumpreg@ --- *
987  *
988  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
989  *                      human_output@
990  *              @unsigned disp@ = register disposition
991  *              @const union tvec_regval *rv@ = register value
992  *              @const struct tvec_regdef *rd@ = register definition
993  *
994  * Returns:     ---
995  *
996  * Use:         Dump a register.
997  *
998  *              The human driver applies highlighting to mismatching output
999  *              registers, but otherwise delegates to the register type
1000  *              handler and the layout machinery.
1001  */
1002
1003 static void human_dumpreg(struct tvec_output *o,
1004                           unsigned disp, const union tvec_regval *rv,
1005                           const struct tvec_regdef *rd)
1006 {
1007   struct human_output *h = (struct human_output *)o;
1008   const char *ds = regdisp(disp); int n = strlen(ds) + strlen(rd->name);
1009
1010   clear_progress(h);
1011   gprintf(&human_printops, h, "%*s%s %s = ",
1012           10 + h->maxlen - n, "", ds, rd->name);
1013   if (h->f&HOF_COLOUR) {
1014     if (!rv) setattr(h, HA_UNSET);
1015     else if (disp == TVRD_FOUND) setattr(h, HA_FOUND);
1016     else if (disp == TVRD_EXPECT) setattr(h, HA_EXPECT);
1017   }
1018   if (!rv) gprintf(&human_printops, h, "#unset");
1019   else rd->ty->dump(rv, rd, 0, &human_printops, h);
1020   setattr(h, HA_PLAIN); layout_char(&h->lyt, '\n');
1021 }
1022
1023 /* --- @human_etest@ --- *
1024  *
1025  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1026  *                      human_output@
1027  *              @unsigned outcome@ = the test outcome
1028  *
1029  * Returns:     ---
1030  *
1031  * Use:         Report that a test has finished.
1032  *
1033  *              The human driver reactivates the progress display, if
1034  *              necessary, and adds a new character for the completed test.
1035  */
1036
1037 static void human_etest(struct tvec_output *o, unsigned outcome)
1038 {
1039   struct human_output *h = (struct human_output *)o;
1040   int ch;
1041
1042   if (h->f&HOF_TTY) {
1043     show_progress(h);
1044     switch (outcome) {
1045       case TVOUT_WIN: ch = HSB_WIN; break;
1046       case TVOUT_LOSE: ch = HSB_LOSE; break;
1047       case TVOUT_XFAIL: ch = HSB_XFAIL; break;
1048       case TVOUT_SKIP: ch = HSB_SKIP; break;
1049       default: abort();
1050     }
1051     dstr_putc(&h->scoreboard, ch);
1052     write_scoreboard_char(h, ch); fflush(h->lyt.fp);
1053   }
1054 }
1055
1056 /* --- @human_bbench@ --- *
1057  *
1058  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1059  *                      human_output@
1060  *              @const char *ident@ = identifying register values
1061  *              @unsigned unit@ = measurement unit (@TVBU_...@)
1062  *
1063  * Returns:     ---
1064  *
1065  * Use:         Report that a benchmark has started.
1066  *
1067  *              The human driver just prints the start of the benchmark
1068  *              report.
1069  */
1070
1071 static void human_bbench(struct tvec_output *o,
1072                          const char *ident, unsigned unit)
1073 {
1074   struct human_output *h = (struct human_output *)o;
1075   struct tvec_state *tv = h->tv;
1076
1077   clear_progress(h);
1078   fprintf(h->lyt.fp, "%s: %s: ", tv->test->name, ident); fflush(h->lyt.fp);
1079 }
1080
1081 /* --- @human_ebench@ --- *
1082  *
1083  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1084  *                      human_output@
1085  *              @const char *ident@ = identifying register values
1086  *              @unsigned unit@ = measurement unit (@TVBU_...@)
1087  *              @const struct bench_timing *tm@ = measurement
1088  *
1089  * Returns:     ---
1090  *
1091  * Use:         Report a benchmark's results
1092  *
1093  *              The human driver just delegates to the default benchmark
1094  *              reporting, via the layout machinery.
1095  */
1096
1097 static void human_ebench(struct tvec_output *o,
1098                          const char *ident, unsigned unit,
1099                          const struct bench_timing *tm)
1100 {
1101   struct human_output *h = (struct human_output *)o;
1102
1103   tvec_benchreport(&human_printops, h, unit, tm);
1104   fputc('\n', h->lyt.fp);
1105 }
1106
1107 /* --- @human_report@ --- *
1108  *
1109  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1110  *                      human_output@
1111  *              @unsigned level@ = message level (@TVLEV_...@)
1112  *              @const char *msg@, @va_list *ap@ = format string and
1113  *                      arguments
1114  *
1115  * Returns:     ---
1116  *
1117  * Use:         Report a message to the user.
1118  *
1119  *              The human driver arranges to show the message on @stderr@ as
1120  *              well as the usual output, with a certain amount of
1121  *              intelligence in case they're both actually the same device.
1122  */
1123
1124 static void human_report(struct tvec_output *o, unsigned level,
1125                          const char *msg, va_list *ap)
1126 {
1127   struct human_output *h = (struct human_output *)o;
1128   struct tvec_state *tv = h->tv;
1129   const char *levstr; unsigned levattr;
1130   dstr d = DSTR_INIT;
1131   unsigned f = 0;
1132 #define f_flush 1u
1133 #define f_progress 2u
1134
1135   dstr_vputf(&d, msg, ap); dstr_putc(&d, '\n');
1136
1137   switch (level) {
1138 #define CASE(tag, name, val)                                            \
1139     case TVLEV_##tag: levstr = name; levattr = HA_##tag; break;
1140     TVEC_LEVELS(CASE)
1141     default: levstr = "??"; levattr = HA_UNKLEV; break;
1142   }
1143
1144   if (h->lyt.fp != stderr && !(h->f&HOF_DUPERR)) f |= f_flush;
1145
1146 #define FLUSH do if (f&f_flush) fflush(h->lyt.fp); while (0)
1147
1148   if (h->f^HOF_PROGRESS)
1149     { clear_progress(h); fflush(h->lyt.fp); f |= f_progress; }
1150   fprintf(stderr, "%s: ", QUIS);
1151   report_location(h, stderr, tv->infile, tv->lno);
1152   setattr(h, levattr); FLUSH; fputs(levstr, stderr); setattr(h, 0); FLUSH;
1153   fputs(": ", stderr); fwrite(d.buf, 1, d.len, stderr);
1154
1155 #undef FLUSH
1156
1157   if (h->f&HOF_DUPERR) {
1158     report_location(h, h->lyt.fp, tv->infile, tv->lno);
1159     fprintf(h->lyt.fp, "%s: ", levstr);
1160     fwrite(d.buf, 1, d.len, h->lyt.fp);
1161   }
1162   if (f&f_progress) show_progress(h);
1163
1164 #undef f_flush
1165 #undef f_progress
1166 }
1167
1168 /* --- @human_destroy@ --- *
1169  *
1170  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1171  *                      human_output@
1172  *
1173  * Returns:     ---
1174  *
1175  * Use:         Release the resources held by the output driver.
1176  */
1177
1178 static void human_destroy(struct tvec_output *o)
1179 {
1180   struct human_output *h = (struct human_output *)o;
1181
1182   destroy_layout(&h->lyt,
1183                  h->lyt.fp == stdout || h->lyt.fp == stderr ? 0 : DLF_CLOSE);
1184   dstr_destroy(&h->scoreboard);
1185   xfree(h->outbuf); xfree(h);
1186 }
1187
1188 static const struct tvec_outops human_ops = {
1189   human_bsession, human_esession,
1190   human_bgroup, human_skipgroup, human_egroup,
1191   human_btest, human_skip, human_fail, human_dumpreg, human_etest,
1192   human_bbench, human_ebench,
1193   human_report,
1194   human_destroy
1195 };
1196
1197 /* --- @tvec_humanoutput@ --- *
1198  *
1199  * Arguments:   @FILE *fp@ = output file to write on
1200  *
1201  * Returns:     An output formatter.
1202  *
1203  * Use:         Return an output formatter which writes on @fp@ with the
1204  *              expectation that a human will be watching and interpreting
1205  *              the output.  If @fp@ denotes a terminal, the display shows a
1206  *              `scoreboard' indicating the outcome of each test case
1207  *              attempted, and may in addition use colour and other
1208  *              highlighting.
1209  */
1210
1211 struct tvec_output *tvec_humanoutput(FILE *fp)
1212 {
1213   struct human_output *h;
1214   const char *p;
1215
1216   h = xmalloc(sizeof(*h)); h->_o.ops = &human_ops;
1217   h->f = 0; h->attr = 0;
1218
1219   init_layout(&h->lyt, fp, 0);
1220   h->outbuf = 0; h->outsz = 0;
1221
1222   switch (getenv_boolean("TVEC_TTY", -1)) {
1223     case 1: h->f |= HOF_TTY; break;
1224     case 0: break;
1225     default:
1226       if (isatty(fileno(fp))) h->f |= HOF_TTY;
1227       break;
1228   }
1229   switch (getenv_boolean("TVEC_COLOUR", -1)) {
1230     case 1: h->f |= HOF_COLOUR; break;
1231     case 0: break;
1232     default:
1233       if (h->f&HOF_TTY) {
1234         p = getenv("TERM");
1235         if (p && STRCMP(p, !=, "dumb")) h->f |= HOF_COLOUR;
1236       }
1237       break;
1238   }
1239
1240   if (fp != stderr && (fp != stdout || !(h->f&HOF_TTY))) h->f |= HOF_DUPERR;
1241   dstr_create(&h->scoreboard);
1242   return (&h->_o);
1243 }
1244
1245 /*----- Perl's `Test Anything Protocol' -----------------------------------*/
1246
1247 struct tap_output {
1248   struct tvec_output _o;                /* output base class */
1249   struct tvec_state *tv;                /* stashed testing state */
1250   struct layout lyt;                    /* output layout */
1251   char *outbuf; size_t outsz;           /* buffer for formatted output */
1252   unsigned grpix, testix;               /* group and test indices */
1253   unsigned previx;                      /* previously reported test index */
1254   int maxlen;                           /* longest register name */
1255 };
1256
1257 /* --- @tap_writech@, @tap_write@, @tap_writef@ --- *
1258  *
1259  * Arguments:   @void *go@ = output sink, secretly a @struct tap_output@
1260  *              @int ch@ = character to write
1261  *              @const char *@p@, @size_t sz@ = string (with explicit length)
1262  *                      to write
1263  *              @const char *p, ...@ = format control string and arguments to
1264  *                      write
1265  *
1266  * Returns:     ---
1267  *
1268  * Use:         Write characters, strings, or formatted strings to the
1269  *              output, applying appropriate layout.
1270  *
1271  *              For the TAP output driver, the layout machinery prefixes each
1272  *              line with `    ## ' and strips trailing spaces.
1273  */
1274
1275 static int tap_writech(void *go, int ch)
1276   { struct tap_output *t = go; return (layout_char(&t->lyt, ch)); }
1277
1278 static int tap_writem(void *go, const char *p, size_t sz)
1279   { struct tap_output *t = go; return (layout_string(&t->lyt, p, sz)); }
1280
1281 static int tap_nwritef(void *go, size_t maxsz, const char *p, ...)
1282 {
1283   struct tap_output *t = go;
1284   size_t n;
1285   va_list ap;
1286
1287   va_start(ap, p);
1288   n = gprintf_memputf(&t->outbuf, &t->outsz, maxsz, p, ap);
1289   va_end(ap);
1290   return (layout_string(&t->lyt, t->outbuf, n));
1291 }
1292
1293 static const struct gprintf_ops tap_printops =
1294   { tap_writech, tap_writem, tap_nwritef };
1295
1296 /* --- @tap_bsession@ --- *
1297  *
1298  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1299  *                      tap_output@
1300  *              @struct tvec_state *tv@ = the test state producing output
1301  *
1302  * Returns:     ---
1303  *
1304  * Use:         Begin a test session.
1305  *
1306  *              The TAP driver records the test state for later reference,
1307  *              initializes the group index counter, and prints the version
1308  *              number.
1309  */
1310
1311 static void tap_bsession(struct tvec_output *o, struct tvec_state *tv)
1312 {
1313   struct tap_output *t = (struct tap_output *)o;
1314
1315   t->tv = tv; t->grpix = 0;
1316   fputs("TAP version 13\n", t->lyt.fp); /* but secretly 14 really */
1317 }
1318
1319 /* --- @tap_esession@ --- *
1320  *
1321  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1322  *                      tap_output@
1323  *
1324  * Returns:     Suggested exit code.
1325  *
1326  * Use:         End a test session.
1327  *
1328  *              The TAP driver prints a final summary of the rest results
1329  *              and returns a suitable exit code.  If errors occurred, it
1330  *              instead prints a `Bail out!' line forcing the reader to
1331  *              report a failure.
1332  */
1333
1334 static int tap_esession(struct tvec_output *o)
1335 {
1336   struct tap_output *t = (struct tap_output *)o;
1337   struct tvec_state *tv = t->tv;
1338
1339   if (tv->f&TVSF_ERROR) {
1340     fputs("Bail out!  "
1341           "Errors found in input; tests may not have run correctly\n",
1342           t->lyt.fp);
1343     return (2);
1344   }
1345
1346   fprintf(t->lyt.fp, "1..%u\n", t->grpix);
1347   t->tv = 0; return (tv->all[TVOUT_LOSE] ? 1 : 0);
1348 }
1349
1350 /* --- @tap_bgroup@ --- *
1351  *
1352  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1353  *                      tap_output@
1354  *
1355  * Returns:     ---
1356  *
1357  * Use:         Begin a test group.
1358  *
1359  *              The TAP driver determines the length of the longest
1360  *              register name, resets the group progress scoreboard, and
1361  *              activates the progress display.
1362  */
1363
1364 static void tap_bgroup(struct tvec_output *o)
1365 {
1366   struct tap_output *t = (struct tap_output *)o;
1367   struct tvec_state *tv = t->tv;
1368
1369   t->grpix++; t->testix = t->previx = 0;
1370   t->maxlen = register_maxnamelen(t->tv);
1371   fprintf(t->lyt.fp, "# Subtest: %s\n", tv->test->name);
1372 }
1373
1374 /* --- @tap_skipgroup@ --- *
1375  *
1376  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1377  *                      tap_output@
1378  *              @const char *excuse@, @va_list *ap@ = reason for skipping the
1379  *                      group, or null
1380  *
1381  * Returns:     ---
1382  *
1383  * Use:         Report that a test group is being skipped.
1384  *
1385  *              The TAP driver just reports the situation to its output
1386  *              stream.
1387  */
1388
1389 static void tap_skipgroup(struct tvec_output *o,
1390                           const char *excuse, va_list *ap)
1391 {
1392   struct tap_output *t = (struct tap_output *)o;
1393
1394   fprintf(t->lyt.fp, "    1..%u\n", t->testix);
1395   fprintf(t->lyt.fp, "ok %u %s # SKIP", t->grpix, t->tv->test->name);
1396   if (excuse) { fputc(' ', t->lyt.fp); vfprintf(t->lyt.fp, excuse, *ap); }
1397   fputc('\n', t->lyt.fp);
1398 }
1399
1400 /* --- @tap_egroup@ --- *
1401  *
1402  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1403  *                      tap_output@
1404  *
1405  * Returns:     ---
1406  *
1407  * Use:         Report that a test group has finished.
1408  *
1409  *              The TAP driver reports a summary of the group's tests.
1410  */
1411
1412 static void tap_egroup(struct tvec_output *o)
1413 {
1414   struct tap_output *t = (struct tap_output *)o;
1415   struct tvec_state *tv = t->tv;
1416
1417   fprintf(t->lyt.fp, "    1..%u\n", t->testix);
1418   fprintf(t->lyt.fp, "%s %u - %s\n",
1419           tv->curr[TVOUT_LOSE] ? "not ok" : "ok",
1420           t->grpix, tv->test->name);
1421 }
1422
1423 /* --- @tap_btest@ --- *
1424  *
1425  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1426  *                      tap_output@
1427  *
1428  * Returns:     ---
1429  *
1430  * Use:         Report that a test is starting.
1431  *
1432  *              The TAP driver advances its test counter.  (We could do this
1433  *              by adding up up the counters in @tv->curr@, and add on the
1434  *              current test, but it's easier this way.)
1435  */
1436
1437 static void tap_btest(struct tvec_output *o)
1438   { struct tap_output *t = (struct tap_output *)o; t->testix++; }
1439
1440 /* --- @tap_outcome@, @tap_skip@, @tap_fail@ --- *
1441  *
1442  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1443  *                      tap_output@
1444  *              @unsigned attr@ = attribute to apply to the outcome
1445  *              @const char *outcome@ = outcome string to report
1446  *              @const char *detail@, @va_list *ap@ = a detail message
1447  *              @const char *excuse@, @va_list *ap@ = reason for skipping the
1448  *                      test
1449  *
1450  * Returns:     ---
1451  *
1452  * Use:         Report that a test has been skipped or failed.
1453  *
1454  *              The TAP driver reports the situation on its output stream.
1455  *              TAP only allows us to report a single status for each
1456  *              subtest, so we notice when we've already reported a status
1457  *              for the current test and convert the second report as a
1458  *              comment.  This should only happen in the case of multiple
1459  *              failures.
1460  */
1461
1462 static void tap_outcome(struct tvec_output *o,
1463                         const char *head, const char *tail,
1464                         const char *detail, va_list *ap)
1465 {
1466   struct tap_output *t = (struct tap_output *)o;
1467   struct tvec_state *tv = t->tv;
1468
1469   fprintf(t->lyt.fp, "    %s %u - %s:%u%s",
1470           t->testix == t->previx ? "##" : head,
1471           t->testix, tv->infile, tv->test_lno, tail);
1472   if (detail)
1473     { fputc(' ', t->lyt.fp); vfprintf(t->lyt.fp, detail, *ap); }
1474   fputc('\n', t->lyt.fp);
1475   t->previx = t->testix;
1476 }
1477
1478 static void tap_skip(struct tvec_output *o, const char *excuse, va_list *ap)
1479   { tap_outcome(o, "ok", " # SKIP", excuse, ap); }
1480 static void tap_fail(struct tvec_output *o, const char *detail, va_list *ap)
1481   { tap_outcome(o, "not ok", "", detail, ap); }
1482
1483 /* --- @tap_dumpreg@ --- *
1484  *
1485  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1486  *                      tap_output@
1487  *              @unsigned disp@ = register disposition
1488  *              @const union tvec_regval *rv@ = register value
1489  *              @const struct tvec_regdef *rd@ = register definition
1490  *
1491  * Returns:     ---
1492  *
1493  * Use:         Dump a register.
1494  *
1495  *              The TAP driver applies highlighting to mismatching output
1496  *              registers, but otherwise delegates to the register type
1497  *              handler and the layout machinery.  The result is that the
1498  *              register dump is marked as a comment and indented.
1499  */
1500
1501 static void tap_dumpreg(struct tvec_output *o,
1502                         unsigned disp, const union tvec_regval *rv,
1503                         const struct tvec_regdef *rd)
1504 {
1505   struct tap_output *t = (struct tap_output *)o;
1506   const char *ds = regdisp(disp); int n = strlen(ds) + strlen(rd->name);
1507
1508   set_layout_prefix(&t->lyt, "    ## ");
1509   gprintf(&tap_printops, t, "%*s%s %s = ",
1510           10 + t->maxlen - n, "", ds, rd->name);
1511   if (!rv) gprintf(&tap_printops, t, "#<unset>");
1512   else rd->ty->dump(rv, rd, 0, &tap_printops, t);
1513   layout_char(&t->lyt, '\n');
1514 }
1515
1516 /* --- @tap_etest@ --- *
1517  *
1518  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1519  *                      tap_output@
1520  *              @unsigned outcome@ = the test outcome
1521  *
1522  * Returns:     ---
1523  *
1524  * Use:         Report that a test has finished.
1525  *
1526  *              The TAP driver reports the outcome of the test, if that's not
1527  *              already decided.
1528  */
1529
1530 static void tap_etest(struct tvec_output *o, unsigned outcome)
1531 {
1532   switch (outcome) {
1533     case TVOUT_WIN:
1534       tap_outcome(o, "ok", "", 0, 0);
1535       break;
1536     case TVOUT_XFAIL:
1537       tap_outcome(o, "not ok", " # TODO expected failure", 0, 0);
1538       break;
1539   }
1540 }
1541
1542 /* --- @tap_bbench@ --- *
1543  *
1544  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1545  *                      tap_output@
1546  *              @const char *ident@ = identifying register values
1547  *              @unsigned unit@ = measurement unit (@TVBU_...@)
1548  *
1549  * Returns:     ---
1550  *
1551  * Use:         Report that a benchmark has started.
1552  *
1553  *              The TAP driver does nothing here.  All of the reporting
1554  *              happens in @tap_ebench@.
1555  */
1556
1557 static void tap_bbench(struct tvec_output *o,
1558                        const char *ident, unsigned unit)
1559   { ; }
1560
1561 /* --- @tap_ebench@ --- *
1562  *
1563  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1564  *                      tap_output@
1565  *              @const char *ident@ = identifying register values
1566  *              @unsigned unit@ = measurement unit (@TVBU_...@)
1567  *              @const struct bench_timing *tm@ = measurement
1568  *
1569  * Returns:     ---
1570  *
1571  * Use:         Report a benchmark's results
1572  *
1573  *              The TAP driver just delegates to the default benchmark
1574  *              reporting, via the layout machinery so that the result is
1575  *              printed as a comment.
1576  */
1577
1578 static void tap_ebench(struct tvec_output *o,
1579                        const char *ident, unsigned unit,
1580                        const struct bench_timing *tm)
1581 {
1582   struct tap_output *t = (struct tap_output *)o;
1583   struct tvec_state *tv = t->tv;
1584
1585   set_layout_prefix(&t->lyt, "    ## ");
1586   gprintf(&tap_printops, t, "%s: %s: ", tv->test->name, ident);
1587   tvec_benchreport(&tap_printops, t, unit, tm);
1588   layout_char(&t->lyt, '\n');
1589 }
1590
1591 /* --- @tap_report@ --- *
1592  *
1593  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1594  *                      tap_output@
1595  *              @unsigned level@ = message level (@TVLEV_...@)
1596  *              @const char *msg@, @va_list *ap@ = format string and
1597  *                      arguments
1598  *
1599  * Returns:     ---
1600  *
1601  * Use:         Report a message to the user.
1602  *
1603  *              Messages are reported as comments, so that they can be
1604  *              accumulated by the reader.  An error will cause a later
1605  *              bailout or, if we crash before then, a missing plan line,
1606  *              either of which will cause the reader to report a serious
1607  *              problem.
1608  */
1609
1610 static void tap_report(struct tvec_output *o, unsigned level,
1611                        const char *msg, va_list *ap)
1612 {
1613   struct tap_output *t = (struct tap_output *)o;
1614   struct tvec_state *tv = t->tv;
1615
1616   if (tv->test) set_layout_prefix(&t->lyt, "    ## ");
1617   else set_layout_prefix(&t->lyt, "## ");
1618
1619   if (tv->infile) gprintf(&tap_printops, t, "%s:%u: ", tv->infile, tv->lno);
1620   gprintf(&tap_printops, t, "%s: ", tvec_strlevel(level));
1621   vgprintf(&tap_printops, t, msg, ap);
1622   layout_char(&t->lyt, '\n');
1623 }
1624
1625 /* --- @tap_destroy@ --- *
1626  *
1627  * Arguments:   @struct tvec_output *o@ = output sink, secretly a @struct
1628  *                      tap_output@
1629  *
1630  * Returns:     ---
1631  *
1632  * Use:         Release the resources held by the output driver.
1633  */
1634
1635 static void tap_destroy(struct tvec_output *o)
1636 {
1637   struct tap_output *t = (struct tap_output *)o;
1638
1639   destroy_layout(&t->lyt,
1640                  t->lyt.fp == stdout || t->lyt.fp == stderr ? 0 : DLF_CLOSE);
1641   xfree(t->outbuf); xfree(t);
1642 }
1643
1644 static const struct tvec_outops tap_ops = {
1645   tap_bsession, tap_esession,
1646   tap_bgroup, tap_skipgroup, tap_egroup,
1647   tap_btest, tap_skip, tap_fail, tap_dumpreg, tap_etest,
1648   tap_bbench, tap_ebench,
1649   tap_report,
1650   tap_destroy
1651 };
1652
1653 /* --- @tvec_tapoutput@ --- *
1654  *
1655  * Arguments:   @FILE *fp@ = output file to write on
1656  *
1657  * Returns:     An output formatter.
1658  *
1659  * Use:         Return an output formatter which writes on @fp@ in `TAP'
1660  *              (`Test Anything Protocol') format.
1661  *
1662  *              TAP comes from the Perl community, but has spread rather
1663  *              further.  This driver produces TAP version 14, but pretends
1664  *              to be version 13.  The driver produces a TAP `test point' --
1665  *              i.e., a result reported as `ok' or `not ok' -- for each input
1666  *              test group.  Failure reports and register dumps are produced
1667  *              as diagnostic messages before the final group result.  (TAP
1668  *              permits structuerd YAML data after the test-point result,
1669  *              which could be used to report details, but (a) postponing the
1670  *              details until after the report is inconvenient, and (b) there
1671  *              is no standardization for the YAML anyway, so in practice
1672  *              it's no more useful than the unstructured diagnostics.
1673  */
1674
1675 struct tvec_output *tvec_tapoutput(FILE *fp)
1676 {
1677   struct tap_output *t;
1678
1679   t = xmalloc(sizeof(*t)); t->_o.ops = &tap_ops;
1680   init_layout(&t->lyt, fp, 0);
1681   t->outbuf = 0; t->outsz = 0;
1682   return (&t->_o);
1683 }
1684
1685 /*----- Default output ----------------------------------------------------*/
1686
1687 /* --- @tvec_dfltoutput@ --- *
1688  *
1689  * Arguments:   @FILE *fp@ = output file to write on
1690  *
1691  * Returns:     An output formatter.
1692  *
1693  * Use:         Selects and instantiates an output formatter suitable for
1694  *              writing on @fp@.  The policy is subject to change, but
1695  *              currently the `human' output format is selected if @fp@ is
1696  *              interactive (i.e., if @isatty(fileno(fp))@ is true), and
1697  *              otherwise the `tap' format is used.
1698  */
1699
1700 struct tvec_output *tvec_dfltout(FILE *fp)
1701 {
1702   int ttyp = getenv_boolean("TVEC_TTY", -1);
1703
1704   if (ttyp == -1) ttyp = isatty(fileno(fp));
1705   if (ttyp) return (tvec_humanoutput(fp));
1706   else return (tvec_tapoutput(fp));
1707 }
1708
1709 /*----- That's all, folks -------------------------------------------------*/