+/*----- Automake support --------------------------------------------------*/
+
+struct automake_output {
+ struct tvec_output _o;
+ arena *a; /* arena */
+ struct tvec_state *tv; /* test-vector state */
+ struct tvec_output *progress; /* real-time progress output */
+ struct tvec_output *log; /* log file output */
+ FILE *trs; /* test result stream */
+};
+
+/* --- @am_bsession@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_output@
+ * @struct tvec_state *tv@ = the test state producing output
+ *
+ * Returns: ---
+ *
+ * Use: Begin a test session.
+ *
+ * The Automake driver passes the event on to its subordinates.
+ */
+
+static void am_bsession(struct tvec_output *o, struct tvec_state *tv)
+{
+ struct automake_output *am = (struct automake_output *)o;
+
+ am->tv = tv;
+ human_bsession(am->progress, tv);
+ machine_bsession(am->log, tv);
+}
+
+/* --- @am_esession@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_output@
+ *
+ * Returns: Suggested exit code.
+ *
+ * Use: End a test session.
+ *
+ * The Automake driver completes the test-results file and
+ * passes the event on to its subordinates.
+ */
+
+static void am_report_unusual(struct automake_output *am,
+ unsigned xfail, unsigned skip)
+{
+ unsigned f = 0;
+#define f_any 1u
+
+ if (xfail) {
+ fprintf(am->trs, "%s%u expected %s", f&f_any ? ", " : " (",
+ xfail, xfail == 1 ? "failure" : "failures");
+ f |= f_any;
+ }
+ if (skip) {
+ fprintf(am->trs, "%s%u skipped", f&f_any ? ", " : " (", skip);
+ f |= f_any;
+ }
+ if (f&f_any) fputc(')', am->trs);
+
+#undef f_any
+}
+
+static int am_esession(struct tvec_output *o)
+{
+ struct automake_output *am = (struct automake_output *)o;
+ struct tvec_state *tv = am->tv;
+ unsigned
+ all_win = tv->all[TVOUT_WIN], grps_win = tv->grps[TVOUT_WIN],
+ all_xfail = tv->all[TVOUT_XFAIL],
+ all_lose = tv->all[TVOUT_LOSE], grps_lose = tv->grps[TVOUT_LOSE],
+ all_skip = tv->all[TVOUT_SKIP], grps_skip = tv->grps[TVOUT_SKIP],
+ all_pass = all_win + all_xfail, all_run = all_pass + all_lose,
+ grps_run = grps_win + grps_lose;
+
+ human_esession(am->progress);
+ machine_esession(am->log);
+
+ fputs(":test-global-result: ", am->trs);
+ if (tv->f&TVSF_ERROR) fputs("ERRORS; ", am->trs);
+ if (!all_lose) {
+ fprintf(am->trs, "PASSED %s%u %s",
+ !(all_skip || grps_skip) ? "all " : "",
+ all_win, all_win == 1 ? "test" : "tests");
+ am_report_unusual(am, all_xfail, all_skip);
+ fprintf(am->trs, " in %u %s",
+ grps_win, grps_win == 1 ? "group" : "groups");
+ am_report_unusual(am, 0, grps_skip);
+ } else {
+ fprintf(am->trs, "FAILED %u out of %u %s",
+ all_lose, all_run, all_run == 1 ? "test" : "tests");
+ am_report_unusual(am, all_xfail, all_skip);
+ fprintf(am->trs, " in %u out of %u %s",
+ grps_lose, grps_run, grps_run == 1 ? "group" : "groups");
+ am_report_unusual(am, 0, grps_skip);
+ }
+ fputc('\n', am->trs);
+
+ fprintf(am->trs, ":copy-in-global-log: %s\n",
+ !all_lose && !(tv->f&TVSF_ERROR) ? "no" : "yes");
+ fprintf(am->trs, ":recheck: %s\n",
+ !all_lose && !(tv->f&TVSF_ERROR) ? "no" : "yes");
+
+ return (0);
+}
+
+/* --- @am_bgroup@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_output@
+ *
+ * Returns: ---
+ *
+ * Use: Begin a test group.
+ *
+ * The Automake driver passes the event on to its subordinates.
+ */
+
+static void am_bgroup(struct tvec_output *o)
+{
+ struct automake_output *am = (struct automake_output *)o;
+
+ human_bgroup(am->progress);
+ machine_bgroup(am->log);
+}
+
+/* --- @am_skipgroup@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_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 Automake driver makes a note in the test-results file and
+ * passes the event on to its subordinates.
+ */
+
+static void am_skipgroup(struct tvec_output *o,
+ const char *excuse, va_list *ap)
+{
+ struct automake_output *am = (struct automake_output *)o;
+ struct tvec_state *tv = am->tv;
+
+ fprintf(am->trs, ":test-result: SKIP %s\n", tv->test->name);
+ human_skipgroup(am->progress, excuse, ap);
+ machine_skipgroup(am->log, excuse, ap);
+}
+
+/* --- @am_egroup@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_output@
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test group has finished.
+ *
+ * The Automake driver makes a note in the test-results file and
+ * passes the event on to its subordinates.
+ */
+
+static void am_egroup(struct tvec_output *o)
+{
+ struct automake_output *am = (struct automake_output *)o;
+ struct tvec_state *tv = am->tv;
+
+ fprintf(am->trs, ":test-result: %s %s\n",
+ tv->curr[TVOUT_LOSE] ? "FAIL" : "PASS", tv->test->name);
+ human_egroup(am->progress);
+ machine_egroup(am->log);
+}
+
+/* --- @am_btest@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_output@
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test is starting.
+ *
+ * The Automake driver passes the event on to its subordinates.
+ */
+
+static void am_btest(struct tvec_output *o)
+{
+ struct automake_output *am = (struct automake_output *)o;
+
+ human_btest(am->progress);
+ machine_btest(am->log);
+}
+
+/* --- @am_skip@, @am_fail@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_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 Automake driver passes the event on to its subordinates.
+ */
+
+static void am_skip(struct tvec_output *o, const char *excuse, va_list *ap)
+{
+ struct automake_output *am = (struct automake_output *)o;
+
+ human_skip(am->progress, excuse, ap);
+ machine_skip(am->log, excuse, ap);
+}
+
+static void am_fail(struct tvec_output *o, const char *detail, va_list *ap)
+{
+ struct automake_output *am = (struct automake_output *)o;
+
+ human_fail(am->progress, detail, ap);
+ machine_fail(am->log, detail, ap);
+}
+
+/* --- @am_dumpreg@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_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 Automake driver passes the event on to its subordinates.
+ */
+
+static void am_dumpreg(struct tvec_output *o,
+ unsigned disp, const union tvec_regval *rv,
+ const struct tvec_regdef *rd)
+{
+ struct automake_output *am = (struct automake_output *)o;
+
+ human_dumpreg(am->progress, disp, rv, rd);
+ machine_dumpreg(am->log, disp, rv, rd);
+}
+
+/* --- @am_etest@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_output@
+ * @unsigned outcome@ = the test outcome
+ *
+ * Returns: ---
+ *
+ * Use: Report that a test has finished.
+ *
+ * The Automake driver passes the event on to its subordinates.
+ */
+
+static void am_etest(struct tvec_output *o, unsigned outcome)
+{
+ struct automake_output *am = (struct automake_output *)o;
+
+ human_etest(am->progress, outcome);
+ machine_etest(am->log, outcome);
+}
+
+/* --- @am_report@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_output@
+ * @unsigned level@ = message level (@TVLV_...@)
+ * @const char *msg@, @va_list *ap@ = format string and
+ * arguments
+ *
+ * Returns: ---
+ *
+ * Use: Report a message to the user.
+ *
+ * The Automake driver passes the event on to its subordinates.
+ */
+
+static void am_report(struct tvec_output *o, unsigned level,
+ const char *msg, va_list *ap)
+{
+ struct automake_output *am = (struct automake_output *)o;
+
+ human_report(am->progress, level, msg, ap);
+ machine_report(am->log, level, msg, ap);
+}
+
+/* --- @am_bbench@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_output@
+ * @const char *desc@ = adhoc test description
+ * @unsigned unit@ = measurement unit (@BTU_...@)
+ *
+ * Returns: ---
+ *
+ * Use: Report that a benchmark has started.
+ *
+ * The Automake driver passes the event on to its subordinates.
+ */
+
+static void am_bbench(struct tvec_output *o,
+ const char *desc, unsigned unit)
+{
+ struct automake_output *am = (struct automake_output *)o;
+
+ human_bbench(am->progress, desc, unit);
+ machine_bbench(am->progress, desc, unit);
+}
+
+/* --- @am_ebench@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_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 Automake driver passes the event on to its subordinates.
+ */
+
+static void am_ebench(struct tvec_output *o,
+ const char *desc, unsigned unit,
+ const struct bench_timing *t)
+{
+ struct automake_output *am = (struct automake_output *)o;
+
+ human_ebench(am->progress, desc, unit, t);
+ machine_ebench(am->progress, desc, unit, t);
+}
+
+static const struct tvec_benchoutops am_benchops =
+ { am_bbench, am_ebench };
+
+/* --- @am_extend@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_output@
+ * @const char *name@ = extension name
+ *
+ * Returns: A pointer to the extension implementation, or null.
+ */
+
+static const void *am_extend(struct tvec_output *o, const char *name)
+{
+ if (STRCMP(name, ==, TVEC_BENCHOUTEXT)) return (&am_benchops);
+ else return (0);
+}
+
+/* --- @am_destroy@ --- *
+ *
+ * Arguments: @struct tvec_output *o@ = output sink, secretly a
+ * @struct automake_output@
+ *
+ * Returns: ---
+ *
+ * Use: Release the resources held by the output driver.
+ */
+
+static void am_destroy(struct tvec_output *o)
+{
+ struct automake_output *am = (struct automake_output *)o;
+
+ human_destroy(am->progress);
+ machine_destroy(am->log);
+ fclose(am->trs); x_free(am->a, am);
+}
+
+static const struct tvec_outops automake_ops = {
+ am_bsession, am_esession,
+ am_bgroup, am_skipgroup, am_egroup,
+ am_btest, am_skip, am_fail, am_dumpreg, am_etest,
+ am_report, am_extend, am_destroy
+};
+
+/* --- @tvec_amoutput@ --- *
+ *
+ * Arguments: @const struct tvec_amargs *a@ = arguments from Automake
+ * command-line protocol
+ *
+ * Returns: An output formatter.
+ *
+ * Use: Returns an output formatter which writes on standard output
+ * in human format, pretending that the output is to a terminal
+ * (in order to cope with %%\manpage{make}{1}%%'s output-
+ * buffering behaviour, writes to the log file @a->log@ in
+ * machine-readable format, and writes an Automake rST-format
+ * test result file to @a->trs@. The `test name' is currently
+ * ignored, because the framework has its own means of
+ * determining test names.
+ */
+
+struct tvec_output *tvec_amoutput(const struct tvec_amargs *a)
+{
+ struct automake_output *am;
+ unsigned f;
+
+ f = TVHF_TTY;
+ if (a->f&TVAF_COLOUR) f |= TVHF_COLOUR;
+
+ XNEW(am); am->a = arena_global; am->_o.ops = &automake_ops;
+ am->progress = tvec_humanoutput(stdout, f, TVHF_TTY | TVHF_COLOUR);
+ am->log = tvec_machineoutput(a->log); am->trs = a->trs;
+ return (&am->_o);
+}
+