+ dstr_create(&h->scoreboard);
+ return (&h->_o);
+}
+
+/*----- Machine-readable output -------------------------------------------*/
+
+struct machine_output {
+ struct tvec_output _o; /* output base class */
+ struct tvec_state *tv; /* stashed testing state */
+ arena *a; /* arena for memory allocation */
+ FILE *fp; /* output stream */
+ char *outbuf; size_t outsz; /* buffer for formatted output */
+ unsigned grpix, testix; /* group and test indices */
+ unsigned f; /* flags */
+#define MF_BENCH 1u /* current test is a benchmark */
+};
+
+/* --- @machine_writech@, @machine_write@, @machine_writef@ --- *
+ *
+ * Arguments: @void *go@ = output sink, secretly a @struct machine_output@
+ * @int ch@ = character to write
+ * @const char *@p@, @size_t sz@ = string (with explicit length)
+ * to write
+ * @const char *p, ...@ = format control string and arguments to
+ * write
+ *
+ * Returns: ---
+ *
+ * Use: Write characters, strings, or formatted strings to the
+ * output, applying appropriate quoting.
+ */
+
+static void machine_escape(struct machine_output *m, int ch)
+{
+ switch (ch) {
+ case 0: fputs("\\0", m->fp); break;
+ case '\a': fputs("\\a", m->fp); break;
+ case '\b': fputs("\\b", m->fp); break;
+ case '\x1b': fputs("\\e", m->fp); break;
+ case '\f': fputs("\\f", m->fp); break;
+ case '\n': fputs("\\n", m->fp); break;
+ case '\r': fputs("\\r", m->fp); break;
+ case '\t': fputs("\\t", m->fp); break;
+ case '\v': fputs("\\v", m->fp); break;
+ case '"': fputs("\\\"", m->fp); break;
+ case '\\': fputs("\\\\", m->fp); break;
+ default: fprintf(m->fp, "\\x{%02x}", ch);
+ }
+}
+
+static int machine_writech(void *go, int ch)
+{
+ struct machine_output *m = go;
+
+ if (ISPRINT(ch)) putc(ch, m->fp);
+ else machine_escape(m, ch);
+ return (1);
+}
+
+static int machine_writem(void *go, const char *p, size_t sz)
+{
+ struct machine_output *m = go;
+ const char *q, *l = p + sz;
+
+ for (;;) {
+ q = p;
+ for (;;) {
+ if (q >= l) goto final;
+ if (!ISPRINT(*q) || *q == '\\' || *q == '"') break;
+ q++;
+ }
+ if (p < q) fwrite(p, 1, q - p, m->fp);
+ p = q; machine_escape(m, (unsigned char)*p++);
+ }
+final:
+ if (p < l) fwrite(p, 1, l - p, m->fp);
+ return (sz);
+}
+
+static int machine_nwritef(void *go, size_t maxsz, const char *p, ...)
+{
+ struct machine_output *m = go;
+ int n;
+ va_list ap;
+
+ va_start(ap, p);
+ n = gprintf_memputf(m->a, &m->outbuf, &m->outsz, maxsz, p, ap);
+ va_end(ap);
+ return (machine_writem(m, m->outbuf, n));
+}
+
+static const struct gprintf_ops machine_printops =
+ { machine_writech, machine_writem, machine_nwritef };
+
+/* --- @machine_maybe_quote@ --- *
+ *
+ * Arguments: @struct machine_output *m@ = output sink
+ * @const char *p@ = pointer to string
+ *
+ * Returns: ---
+ *
+ * Use: Print the string @p@, quoting it if necessary.
+ */
+
+static void machine_maybe_quote(struct machine_output *m, const char *p)
+{
+ const char *q;
+
+ for (q = p; *q; q++)
+ if (!ISPRINT(*q) || ISSPACE(*q)) goto quote;
+ fputs(p, m->fp); return;
+quote:
+ putc('"', m->fp); machine_writem(m, p, strlen(p)); putc('"', m->fp);
+}
+
+/* --- @machine_bsession@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ * @struct tvec_state *tv@ = the test state producing output
+ *
+ * Returns: ---
+ *
+ * Use: Begin a test session.
+ *
+ * The TAP driver records the test state for later reference,
+ * initializes the group index counter, and prints the version
+ * number.
+ */
+
+static void machine_bsession(struct tvec_output *o, struct tvec_state *tv)
+{
+ struct machine_output *m = (struct machine_output *)o;
+
+ m->tv = tv; m->grpix = 0;
+}
+
+/* --- @machine_show_stats@ --- *
+ *
+ * Arguments: @struct machine_output *m@ = output sink
+ * @unsigned *out[TVOUT_LIMIT]@ = outcome counter table
+ *
+ * Returns: ---
+ *
+ * Use: Print a machine readable outcome statistics table
+ */
+
+static void machine_show_stats(struct machine_output *m,
+ unsigned out[TVOUT_LIMIT])
+{
+ static const char *outtab[] = { "lose", "skip", "xfail", "win" };
+ unsigned i;
+
+ for (i = 0; i < TVOUT_LIMIT; i++) {
+ if (i) putc(' ', m->fp);
+ fprintf(m->fp, "%s=%u", outtab[i], out[i]);
+ }
+}
+
+/* --- @machine_esession@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ *
+ * Returns: Suggested exit code.
+ *
+ * Use: End a test session.
+ *
+ * The TAP driver prints a final summary of the rest results
+ * and returns a suitable exit code. If errors occurred, it
+ * instead prints a `Bail out!' line forcing the reader to
+ * report a failure.
+ */
+
+static int machine_esession(struct tvec_output *o)
+{
+ struct machine_output *m = (struct machine_output *)o;
+ struct tvec_state *tv = m->tv;
+
+ fputs("END groups: ", m->fp);
+ machine_show_stats(m, tv->grps);
+ fputs("; tests: ", m->fp);
+ machine_show_stats(m, tv->all);
+ putc('\n', m->fp);
+ m->tv = 0; return (tv->f&TVSF_ERROR ? 2 : tv->all[TVOUT_LOSE] ? 1 : 0);
+}
+
+/* --- @machine_bgroup@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ *
+ * Returns: ---
+ *
+ * Use: Begin a test group.
+ *
+ * The TAP driver determines the length of the longest
+ * register name, resets the group progress scoreboard, and
+ * activates the progress display.
+ */
+
+static void machine_bgroup(struct tvec_output *o)
+{
+ struct machine_output *m = (struct machine_output *)o;
+ struct tvec_state *tv = m->tv;
+
+ fputs("BGROUP ", m->fp);
+ machine_maybe_quote(m, tv->test->name);
+ putc('\n', m->fp);
+ m->grpix++; m->testix = 0;
+}
+
+/* --- @machine_skipgroup@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ * @const char *excuse@, @va_list *ap@ = reason for skipping the
+ * group, or null
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test group is being skipped.
+ *
+ * The TAP driver just reports the situation to its output
+ * stream.
+ */
+
+static void machine_skipgroup(struct tvec_output *o,
+ const char *excuse, va_list *ap)
+{
+ struct machine_output *m = (struct machine_output *)o;
+ struct tvec_state *tv = m->tv;
+
+ fputs("SKIPGRP ", m->fp);
+ machine_maybe_quote(m, tv->test->name);
+ if (excuse) {
+ fputs(" \"", m->fp);
+ vgprintf(&machine_printops, m, excuse, ap);
+ putc('\"', m->fp);
+ }
+ putc('\n', m->fp);
+}
+
+/* --- @machine_egroup@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test group has finished.
+ *
+ * The TAP driver reports a summary of the group's tests.
+ */
+
+static void machine_egroup(struct tvec_output *o)
+{
+ struct machine_output *m = (struct machine_output *)o;
+ struct tvec_state *tv = m->tv;
+
+ if (!(tv->f&TVSF_SKIP)) {
+ fputs("EGROUP ", m->fp); machine_maybe_quote(m, tv->test->name);
+ putc(' ', m->fp); machine_show_stats(m, tv->curr);
+ putc('\n', m->fp);
+ }
+}
+
+/* --- @machine_btest@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test is starting.
+ *
+ * The TAP driver advances its test counter. (We could do this
+ * by adding up up the counters in @tv->curr@, and add on the
+ * current test, but it's easier this way.)
+ */
+
+static void machine_btest(struct tvec_output *o) { ; }
+
+/* --- @machine_report_location@ --- *
+ *
+ * Arguments: @struct human_output *h@ = output state
+ * @const char *file@ = filename
+ * @unsigned lno@ = line number
+ *
+ * Returns: ---
+ *
+ * Use: Print the filename and line number to the output stream.
+ */
+
+static void machine_report_location(struct machine_output *m,
+ const char *file, unsigned lno)
+{
+ if (file) {
+ putc(' ', m->fp);
+ machine_maybe_quote(m, file);
+ fprintf(m->fp, ":%u", lno);
+ }
+}
+
+/* --- @machine_outcome@, @machine_skip@, @machine_fail@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ * @const char *outcome@ = outcome string to report
+ * @const char *detail@, @va_list *ap@ = a detail message
+ * @const char *excuse@, @va_list *ap@ = reason for skipping the
+ * test
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test has been skipped or failed.
+ */
+
+static void machine_outcome(struct tvec_output *o, const char *outcome,
+ const char *detail, va_list *ap)
+{
+ struct machine_output *m = (struct machine_output *)o;
+ struct tvec_state *tv = m->tv;
+
+ fprintf(m->fp, "%s %u", outcome, m->testix);
+ machine_report_location(m, tv->infile, tv->test_lno);
+ if (detail)
+ { putc(' ', m->fp); vgprintf(&machine_printops, m, detail, ap); }
+ putc('\n', m->fp);
+}
+
+static void machine_skip(struct tvec_output *o,
+ const char *excuse, va_list *ap)
+ { machine_outcome(o, "SKIP", excuse, ap); }
+static void machine_fail(struct tvec_output *o,
+ const char *detail, va_list *ap)
+ { machine_outcome(o, "FAIL", detail, ap); }
+
+/* --- @machine_dumpreg@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ * @unsigned disp@ = register disposition
+ * @const union tvec_regval *rv@ = register value
+ * @const struct tvec_regdef *rd@ = register definition
+ *
+ * Returns: ---
+ *
+ * Use: Dump a register.
+ *
+ * The machine driver applies highlighting to mismatching output
+ * registers, but otherwise delegates to the register type
+ * handler.
+ */
+
+static void machine_dumpreg(struct tvec_output *o,
+ unsigned disp, const union tvec_regval *rv,
+ const struct tvec_regdef *rd)
+{
+ struct machine_output *m = (struct machine_output *)o;
+ unsigned f = 0;
+#define f_reg 1u
+#define f_nl 2u
+
+ switch (disp) {
+ case TVRD_INPUT: fputs("\tINPUT ", m->fp); f |= f_reg | f_nl; break;
+ case TVRD_OUTPUT: fputs("\tOUTPUT ", m->fp); f |= f_reg | f_nl; break;
+ case TVRD_MATCH: fputs("\tMATCH ", m->fp); f |= f_reg | f_nl; break;
+ case TVRD_FOUND: fputs("\tMISMATCH ", m->fp); f |= f_reg; break;
+ case TVRD_EXPECT: fputs(" /= ", m->fp); f |= f_nl; break;
+ default: abort();
+ }
+
+ if (f&f_reg) fprintf(m->fp, "%s = ", rd->name);
+ if (!rv) fputs("#unset", m->fp);
+ else rd->ty->dump(rv, rd, TVSF_RAW, &file_printops, m->fp);
+ if (f&f_nl) putc('\n', m->fp);
+
+#undef f_reg
+#undef f_newline
+}
+
+/* --- @machine_etest@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ * @unsigned outcome@ = the test outcome
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test has finished.
+ *
+ * The machine driver reports the outcome of the test, if that's
+ * not already decided.
+ */
+
+static void machine_etest(struct tvec_output *o, unsigned outcome)
+{
+ struct machine_output *m = (struct machine_output *)o;
+
+ if (!(m->f&MF_BENCH)) switch (outcome) {
+ case TVOUT_WIN: machine_outcome(o, "WIN", 0, 0); break;
+ case TVOUT_XFAIL: machine_outcome(o, "XFAIL", 0, 0); break;
+ }
+ m->testix++; m->f &= ~MF_BENCH;
+}
+
+/* --- @machine_report@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ * @unsigned level@ = message level (@TVLV_...@)
+ * @const char *msg@, @va_list *ap@ = format string and
+ * arguments
+ *
+ * Returns: ---
+ *
+ * Use: Report a message to the user.
+ *
+ * Each report level has its own output tag
+ */
+
+static void machine_report(struct tvec_output *o, unsigned level,
+ const char *msg, va_list *ap)
+{
+ struct machine_output *m = (struct machine_output *)o;
+ struct tvec_state *tv = m->tv;
+ const char *p;
+
+ for (p = tvec_strlevel(level); *p; p++) putc(TOUPPER(*p), m->fp);
+ machine_report_location(m, tv->infile, tv->lno);
+ fputs(" \"", m->fp); vgprintf(&machine_printops, m, msg, ap);
+ putc('"', m->fp);
+ putc('\n', m->fp);
+}
+
+/* --- @machine_bbench@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ * @const char *desc@ = adhoc test description
+ * @unsigned unit@ = measurement unit (@BTU_...@)
+ *
+ * Returns: ---
+ *
+ * Use: Report that a benchmark has started.
+ *
+ * The machine driver does nothing here. All of the reporting
+ * happens in @machine_ebench@.
+ */
+
+static void machine_bbench(struct tvec_output *o,
+ const char *desc, unsigned unit)
+ { ; }
+
+/* --- @machine_ebench@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ * @const char *desc@ = adhoc test description
+ * @unsigned unit@ = measurement unit (@BTU_...@)
+ * @const struct bench_timing *t@ = measurement
+ *
+ * Returns: ---
+ *
+ * Use: Report a benchmark's results.
+ *
+ * The machine driver prints the result as a `BENCH' output
+ * line, in raw format.
+ */
+
+static void machine_ebench(struct tvec_output *o,
+ const char *desc, unsigned unit,
+ const struct bench_timing *t)
+{
+ struct machine_output *m = (struct machine_output *)o;
+ struct tvec_state *tv = m->tv;
+
+ fprintf(m->fp, "BENCH %u", m->testix);
+ machine_report_location(m, tv->infile, tv->test_lno);
+ putc(' ', m->fp);
+ if (desc) machine_maybe_quote(m, desc);
+ else print_ident(tv, TVSF_RAW, &file_printops, m->fp);
+ fputs(": ", m->fp);
+ tvec_benchreport(&file_printops, m->fp, unit, TVSF_RAW, t);
+ putc('\n', m->fp); m->f |= MF_BENCH;
+}
+
+static const struct tvec_benchoutops machine_benchops =
+ { machine_bbench, machine_ebench };
+
+/* --- @machine_extend@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ * @const char *name@ = extension name
+ *
+ * Returns: A pointer to the extension implementation, or null.
+ */
+
+static const void *machine_extend(struct tvec_output *o, const char *name)
+{
+ if (STRCMP(name, ==, TVEC_BENCHOUTEXT)) return (&machine_benchops);
+ else return (0);
+}
+
+/* --- @machine_destroy@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct machine_output@
+ *
+ * Returns: ---
+ *
+ * Use: Release the resources held by the output driver.
+ */
+
+static void machine_destroy(struct tvec_output *o)
+{
+ struct machine_output *m = (struct machine_output *)o;
+
+ if (m->fp != stdout && m->fp != stderr) fclose(m->fp);
+ x_free(m->a, m->outbuf); x_free(m->a, m);
+}
+
+static const struct tvec_outops machine_ops = {
+ machine_bsession, machine_esession,
+ machine_bgroup, machine_skipgroup, machine_egroup,
+ machine_btest, machine_skip, machine_fail, machine_dumpreg, machine_etest,
+ machine_report, machine_extend, machine_destroy
+};
+
+/* --- @tvec_machineoutput@ --- *
+ *
+ * Arguments: @FILE *fp@ = output file to write on
+ *
+ * Returns: An output formatter.
+ *
+ * Use: Return an output formatter which writes on @fp@ in a
+ * moderately simple machine-readable format.
+ */
+
+struct tvec_output *tvec_machineoutput(FILE *fp)
+{
+ struct machine_output *m;
+
+ XNEW(m); m->a = arena_global; m->_o.ops = &machine_ops;
+ m->f = 0; m->fp = fp; m->outbuf = 0; m->outsz = 0; m->testix = 0;
+ return (&m->_o);
+}
+
+/*----- Perl's `Test Anything Protocol' -----------------------------------*/
+
+struct tap_output {
+ struct tvec_output _o; /* output base class */
+ struct tvec_state *tv; /* stashed testing state */
+ arena *a; /* arena for memory allocation */
+ struct layout lyt; /* output layout */
+ char *outbuf; size_t outsz; /* buffer for formatted output */
+ unsigned grpix, testix; /* group and test indices */
+ unsigned previx; /* previously reported test index */
+ int maxlen; /* longest register name */
+};
+
+/* --- @tap_writech@, @tap_write@, @tap_writef@ --- *
+ *
+ * Arguments: @void *go@ = output sink, secretly a @struct tap_output@
+ * @int ch@ = character to write
+ * @const char *@p@, @size_t sz@ = string (with explicit length)
+ * to write
+ * @const char *p, ...@ = format control string and arguments to
+ * write
+ *
+ * Returns: ---
+ *
+ * Use: Write characters, strings, or formatted strings to the
+ * output, applying appropriate layout.
+ *
+ * For the TAP output driver, the layout machinery prefixes each
+ * line with ` ## ' and strips trailing spaces.
+ */
+
+static int tap_writech(void *go, int ch)
+{
+ struct tap_output *t = go;
+
+ if (layout_char(&t->lyt, ch)) return (-1);
+ else return (1);
+}
+
+static int tap_writem(void *go, const char *p, size_t sz)
+{
+ struct tap_output *t = go;
+
+ if (layout_string(&t->lyt, p, sz)) return (-1);
+ else return (sz);
+}
+
+static int tap_nwritef(void *go, size_t maxsz, const char *p, ...)
+{
+ struct tap_output *t = go;
+ size_t n;
+ va_list ap;
+
+ va_start(ap, p);
+ n = gprintf_memputf(t->a, &t->outbuf, &t->outsz, maxsz, p, ap);
+ va_end(ap);
+ if (layout_string(&t->lyt, t->outbuf, n)) return (-1);
+ return (n);
+}
+
+static const struct gprintf_ops tap_printops =
+ { tap_writech, tap_writem, tap_nwritef };
+
+/* --- @tap_bsession@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ * @struct tvec_state *tv@ = the test state producing output
+ *
+ * Returns: ---
+ *
+ * Use: Begin a test session.
+ *
+ * The TAP driver records the test state for later reference,
+ * initializes the group index counter, and prints the version
+ * number.
+ */
+
+static void tap_bsession(struct tvec_output *o, struct tvec_state *tv)
+{
+ struct tap_output *t = (struct tap_output *)o;
+
+ t->tv = tv; t->grpix = 0;
+ fputs("TAP version 13\n", t->lyt.fp); /* but secretly 14 really */
+}
+
+/* --- @tap_esession@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ *
+ * Returns: Suggested exit code.
+ *
+ * Use: End a test session.
+ *
+ * The TAP driver prints a final summary of the rest results
+ * and returns a suitable exit code. If errors occurred, it
+ * instead prints a `Bail out!' line forcing the reader to
+ * report a failure.
+ */
+
+static int tap_esession(struct tvec_output *o)
+{
+ struct tap_output *t = (struct tap_output *)o;
+ struct tvec_state *tv = t->tv;
+
+ if (tv->f&TVSF_ERROR) {
+ fputs("Bail out! "
+ "Errors found in input; tests may not have run correctly\n",
+ t->lyt.fp);
+ return (2);
+ }
+
+ fprintf(t->lyt.fp, "1..%u\n", t->grpix);
+ t->tv = 0; return (tv->all[TVOUT_LOSE] ? 1 : 0);
+}
+
+/* --- @tap_bgroup@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ *
+ * Returns: ---
+ *
+ * Use: Begin a test group.
+ *
+ * The TAP driver determines the length of the longest
+ * register name, resets the group progress scoreboard, and
+ * activates the progress display.
+ */
+
+static void tap_bgroup(struct tvec_output *o)
+{
+ struct tap_output *t = (struct tap_output *)o;
+ struct tvec_state *tv = t->tv;
+
+ t->grpix++; t->testix = t->previx = 0;
+ t->maxlen = register_maxnamelen(t->tv);
+ fprintf(t->lyt.fp, "# Subtest: %s\n", tv->test->name);
+}
+
+/* --- @tap_skipgroup@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ * @const char *excuse@, @va_list *ap@ = reason for skipping the
+ * group, or null
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test group is being skipped.
+ *
+ * The TAP driver just reports the situation to its output
+ * stream.
+ */
+
+static void tap_skipgroup(struct tvec_output *o,
+ const char *excuse, va_list *ap)
+{
+ struct tap_output *t = (struct tap_output *)o;
+
+ fprintf(t->lyt.fp, " 1..%u\n", t->testix);
+ fprintf(t->lyt.fp, "ok %u %s # SKIP", t->grpix, t->tv->test->name);
+ if (excuse) { fputc(' ', t->lyt.fp); vfprintf(t->lyt.fp, excuse, *ap); }
+ fputc('\n', t->lyt.fp);
+}
+
+/* --- @tap_egroup@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test group has finished.
+ *
+ * The TAP driver reports a summary of the group's tests.
+ */
+
+static void tap_egroup(struct tvec_output *o)
+{
+ struct tap_output *t = (struct tap_output *)o;
+ struct tvec_state *tv = t->tv;
+
+ fprintf(t->lyt.fp, " 1..%u\n", t->testix);
+ fprintf(t->lyt.fp, "%s %u - %s\n",
+ tv->curr[TVOUT_LOSE] ? "not ok" : "ok",
+ t->grpix, tv->test->name);
+}
+
+/* --- @tap_btest@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test is starting.
+ *
+ * The TAP driver advances its test counter. (We could do this
+ * by adding up up the counters in @tv->curr@, and add on the
+ * current test, but it's easier this way.)
+ */
+
+static void tap_btest(struct tvec_output *o)
+ { struct tap_output *t = (struct tap_output *)o; t->testix++; }
+
+/* --- @tap_outcome@, @tap_skip@, @tap_fail@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ * @const char *head, *tail@ = outcome strings to report
+ * @const char *detail@, @va_list *ap@ = a detail message
+ * @const char *excuse@, @va_list *ap@ = reason for skipping the
+ * test
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test has been skipped or failed.
+ *
+ * The TAP driver reports the situation on its output stream.
+ * TAP only allows us to report a single status for each
+ * subtest, so we notice when we've already reported a status
+ * for the current test and convert the second report as a
+ * comment. This should only happen in the case of multiple
+ * failures.
+ */
+
+static void tap_outcome(struct tvec_output *o,
+ const char *head, const char *tail,
+ const char *detail, va_list *ap)
+{
+ struct tap_output *t = (struct tap_output *)o;
+ struct tvec_state *tv = t->tv;
+
+ fprintf(t->lyt.fp, " %s %u - %s:%u%s",
+ t->testix == t->previx ? "##" : head,
+ t->testix, tv->infile, tv->test_lno, tail);
+ if (detail)
+ { fputc(' ', t->lyt.fp); vfprintf(t->lyt.fp, detail, *ap); }
+ fputc('\n', t->lyt.fp);
+ t->previx = t->testix;
+}
+
+static void tap_skip(struct tvec_output *o, const char *excuse, va_list *ap)
+ { tap_outcome(o, "ok", " # SKIP", excuse, ap); }
+static void tap_fail(struct tvec_output *o, const char *detail, va_list *ap)
+ { tap_outcome(o, "not ok", "", detail, ap); }
+
+/* --- @tap_dumpreg@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ * @unsigned disp@ = register disposition
+ * @const union tvec_regval *rv@ = register value
+ * @const struct tvec_regdef *rd@ = register definition
+ *
+ * Returns: ---
+ *
+ * Use: Dump a register.
+ *
+ * The TAP driver applies highlighting to mismatching output
+ * registers, but otherwise delegates to the register type
+ * handler and the layout machinery. The result is that the
+ * register dump is marked as a comment and indented.
+ */
+
+static void tap_dumpreg(struct tvec_output *o,
+ unsigned disp, const union tvec_regval *rv,
+ const struct tvec_regdef *rd)
+{
+ struct tap_output *t = (struct tap_output *)o;
+ const char *ds = regdisp(disp); int n = strlen(ds) + strlen(rd->name);
+
+ set_layout_prefix(&t->lyt, " ## ");
+ gprintf(&tap_printops, t, "%*s%s %s = ",
+ 10 + t->maxlen - n, "", ds, rd->name);
+ if (!rv) gprintf(&tap_printops, t, "#<unset>");
+ else rd->ty->dump(rv, rd, 0, &tap_printops, t);
+ layout_char(&t->lyt, '\n');
+}
+
+/* --- @tap_etest@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ * @unsigned outcome@ = the test outcome
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test has finished.
+ *
+ * The TAP driver reports the outcome of the test, if that's not
+ * already decided.
+ */
+
+static void tap_etest(struct tvec_output *o, unsigned outcome)
+{
+ switch (outcome) {
+ case TVOUT_WIN:
+ tap_outcome(o, "ok", "", 0, 0);