+/* --- @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);
+ break;
+ case TVOUT_XFAIL:
+ tap_outcome(o, "not ok", " # TODO expected failure", 0, 0);
+ break;
+ }
+}
+
+/* --- @tap_report@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ * @unsigned level@ = message level (@TVLV_...@)
+ * @const char *msg@, @va_list *ap@ = format string and
+ * arguments
+ *
+ * Returns: ---
+ *
+ * Use: Report a message to the user.
+ *
+ * Messages are reported as comments, so that they can be
+ * accumulated by the reader. An error will cause a later
+ * bailout or, if we crash before then, a missing plan line,
+ * either of which will cause the reader to report a serious
+ * problem.
+ */
+
+static void tap_report(struct tvec_output *o, unsigned level,
+ const char *msg, va_list *ap)
+{
+ struct tap_output *t = (struct tap_output *)o;
+ struct tvec_state *tv = t->tv;
+
+ if (tv->test) set_layout_prefix(&t->lyt, " ## ");
+ else set_layout_prefix(&t->lyt, "## ");
+
+ if (tv->infile) gprintf(&tap_printops, t, "%s:%u: ", tv->infile, tv->lno);
+ gprintf(&tap_printops, t, "%s: ", tvec_strlevel(level));
+ vgprintf(&tap_printops, t, msg, ap);
+ layout_char(&t->lyt, '\n');
+}
+
+/* --- @tap_extend@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ * @const char *name@ = extension name
+ *
+ * Returns: A pointer to the extension implementation, or null.
+ */
+
+static const void *tap_extend(struct tvec_output *o, const char *name)
+ { return (0); }
+
+/* --- @tap_destroy@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct tap_output@
+ *
+ * Returns: ---
+ *
+ * Use: Release the resources held by the output driver.
+ */
+
+static void tap_destroy(struct tvec_output *o)
+{
+ struct tap_output *t = (struct tap_output *)o;
+
+ destroy_layout(&t->lyt,
+ t->lyt.fp == stdout || t->lyt.fp == stderr ? 0 : DLF_CLOSE);
+ x_free(t->a, t->outbuf); x_free(t->a, t);
+}
+
+static const struct tvec_outops tap_ops = {