3 * Main entry point for test-vector processing
5 * (c) 2023 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
48 #include "tvec-adhoc.h"
49 #include "tvec-bench.h"
50 #include "tvec-types.h"
52 /*----- Main code ---------------------------------------------------------*/
54 /* Table of output formats. */
55 static struct tvec_output *default_human_output(FILE *fp)
56 { return (tvec_humanoutput(fp, 0, 0)); }
58 static const struct outform {
60 struct tvec_output *(*makefn)(FILE *fp);
62 { "human", default_human_output },
63 { "machine", tvec_machineoutput },
64 { "tap", tvec_tapoutput },
68 /* Common benchmark state. */
69 static struct bench_state bench;
71 /* --- @find_outform@ ---
73 * Arguments: @const char *p@ = output name
75 * Returns: Pointer to output format record.
77 * Use: Looks up an output format by name. Reports a fatal error if
78 * no matching record is found.
81 static const struct outform *find_outform(const char *p)
83 const struct outform *best = 0, *of;
86 for (of = outtab; of->name; of++)
87 if (!STRNCMP(p, ==, of->name, n))
89 else if (!of->name[n])
92 die(2, "ambiguous output format name (`%s' or `%s'?)",
93 best->name, of->name);
96 if (best) return (best);
97 else die(2, "unknown output format `%s'", optarg);
100 /* --- @version@, @usage@, @help@ --- *
102 * Arguments: @FILE *fp@ = stream to write on
106 * Use: Output information about the program.
109 static void version(FILE *fp)
110 { pquis(fp, "$, mLib test-vector framework version " VERSION "\n"); }
112 static void usage(FILE *fp)
115 usage: $ [-B CONFIG] [-f FORMAT] [-o OUTPUT] [-t SECS] [TEST-FILE ...]\n\
119 static void help(FILE *fp)
121 version(fp); fputc('\n', fp);
124 -h, --help show this help text.\n\
125 -v, --version show version number.\n\
126 -u, --usage show usage synopsis.\n\
129 -B, --bench-config=CONFIG set benchmark configuration string.\n\
130 -f, --format=FORMAT produce output in FORMAT.\n\
131 -o, --output=OUTPUT write output to OUTPUT file.\n\
132 -t, --target-time=DURATION run benchmarks for DURATION.\n\
134 Automake driver options:\n\
135 --test-name NAME set the test name (ignored).\n\
136 --log-file PATH write log to PATH.\n\
137 --trs-file PATH write rest results to PATH.\n\
138 --color-tests yes|no produce coloured output.\n\
139 --expect-failure yes|no must be `no'.\n\
140 --enable-hard-errors yes|no (ignored).\n\
144 struct bench_resource {
146 struct bench_state **bst;
149 static void release_bench(pool_resource *r)
151 struct bench_resource *br = (struct bench_resource *)r;
153 if (*br->bst) { bench_destroy(*br->bst); *br->bst = 0; }
156 /* --- @tvec_parseargs@ --- *
158 * Arguments: @int argc@ = number of command-line arguments
159 * @char *argv[]@ = vector of argument strings
160 * @struct tvec_state *tv_out@ = test vector state to initialize
161 * @int *argpos_out@ = where to leave unread argument index
162 * @const struct tvec_config *config@ = test vector
167 * Use: Parse arguments and set up the test vector state @*tv_out@.
168 * If errors occur, print messages to standard error and exit
171 * This function also establishes a common benchmark state.
174 void tvec_parseargs(int argc, char *argv[], struct tvec_state *tv_out,
175 int *argpos_out, const struct tvec_config *config)
178 const struct outform *of = 0;
179 struct tvec_output *o;
180 const char *benchconf = 0;
181 const char *logfile = 0, *trsfile = 0;
182 struct tvec_amargs am;
183 double benchtime = 1.0, scale;
184 struct bench_timer *tm;
185 struct bench_resource *br;
186 const char *p; char *q;
191 #define f_automake 4u
194 OPT_TESTNAME = 0x100,
202 static const struct option options[] = {
203 { "help", 0, 0, 'h' },
204 { "version", 0, 0, 'v' },
205 { "usage", 0, 0, 'u' },
207 { "bench-config", OPTF_ARGREQ, 0, 'B' },
208 { "format", OPTF_ARGREQ, 0, 'f' },
209 { "output", OPTF_ARGREQ, 0, 'o' },
210 { "target-time", OPTF_ARGREQ, 0, 't' },
212 { "test-name", OPTF_ARGREQ, 0, OPT_TESTNAME },
213 { "log-file", OPTF_ARGREQ, 0, OPT_LOGFILE },
214 { "trs-file", OPTF_ARGREQ, 0, OPT_TRSFILE },
215 { "color-tests", OPTF_ARGREQ, 0, OPT_COLOUR },
216 { "expect-failure", OPTF_ARGREQ, 0, OPT_XFAIL },
217 { "enable-hard-errors", OPTF_ARGREQ, 0, OPT_HARDERR },
222 am.f = 0; am.name = 0;
224 opt = mdwopt(argc, argv, "hvu" "B:f:o:t:", options, 0, 0, 0);
227 case 'h': help(stdout); exit(0);
228 case 'v': version(stdout); exit(0);
229 case 'u': usage(stdout); exit(0);
231 case 'B': benchconf = optarg; f |= f_bench; break;
232 case 'f': of = find_outform(optarg); break;
234 if (ofp) fclose(ofp);
235 ofp = fopen(optarg, "w");
237 die(2, "failed to open `%s' for writing: %s",
238 optarg, strerror(errno));
241 if (*optarg != '.' && !ISDIGIT(*optarg)) goto bad_time;
242 errno = 0; benchtime = strtod(optarg, &q);
243 if (errno) goto bad_time;
246 do p++; while (ISSPACE(*p));
247 if (!*p) goto bad_time;
249 if (tvec_parsedurunit(&scale, &p)) goto bad_time;
250 if (*p) goto bad_time;
251 benchtime *= scale; f |= f_bench;
254 die(2, "invalid time duration `%s'", optarg);
256 case OPT_TESTNAME: f |= f_automake; am.name = optarg; break;
257 case OPT_LOGFILE: f |= f_automake; logfile = optarg; break;
258 case OPT_TRSFILE: f |= f_automake; trsfile = optarg; break;
261 if (STRCMP(optarg, ==, "yes")) am.f |= TVAF_COLOUR;
262 else if (STRCMP(optarg, ==, "no")) am.f &= ~TVAF_COLOUR;
263 else die(2, "invalid `--color-tests' value `%s'", optarg);
267 if (STRCMP(optarg, ==, "yes"))
268 die(2, "expected failure not supported");
269 else if (STRCMP(optarg, !=, "no"))
270 die(2, "invalid `--expect-failure' value `%s'", optarg);
274 if (STRCMP(optarg, !=, "yes") && STRCMP(optarg, !=, "no"))
275 die(2, "invalid `--enable-hard-errors' value `%s'", optarg);
283 if (f&f_bogus) { usage(stderr); exit(2); }
287 die(2, "can't set output file or driver in Automake driver");
288 if (!am.name || !logfile || !trsfile)
289 die(2, "must set all of `--test-name', `--log-file', and `--trs-file' "
290 "for Automake driver");
291 am.log = fopen(logfile, "w");
293 die(2, "failed to open log file `%s': %s",
294 logfile, strerror(errno));
295 am.trs = fopen(trsfile, "w");
297 die(2, "failed to open test-results file `%s': %s",
298 trsfile, strerror(errno));
299 o = tvec_amoutput(&am);
301 if (!ofp) ofp = stdout;
303 p = getenv("TVEC_FORMAT");
304 if (p) of = find_outform(p);
306 if (of) o = of->makefn(ofp);
307 else o = tvec_dfltoutput(ofp);
310 tm = bench_createtimer(benchconf);
312 bench_init(&bench, tm); bench.target_s = benchtime;
313 tvec_benchstate = &bench;
314 } else if (f&f_bench) {
315 moan("failed to create benchmark timer");
316 if (!getenv("MLIB_BENCH_DEBUG"))
317 moan("set `MLIB_BENCH_DEBUG=t' in the envionment for more detail");
321 tvec_begin(tv_out, config, o); *argpos_out = optind;
322 POOL_NEW(br, tv_out->p_session);
323 br->bst = &tvec_benchstate;
324 POOL_ADD(tv_out->p_session, &br->r, release_bench);
331 /* --- @tvec_readstdin@, @tvec_readfile@, @tvec_readarg@ --- *
333 * Arguments: @struct tvec_state *tv@ = test vector state
334 * @const char *file@ = pathname of file to read
335 * @const char *arg@ = argument to interpret
337 * Returns: Zero on success, @-1@ on error.
339 * Use: Read test vector data from stdin or a named file. The
340 * @tvec_readarg@ function reads from stdin if @arg@ is `%|-|%',
341 * and from the named file otherwise.
344 int tvec_readstdin(struct tvec_state *tv)
345 { return (tvec_read(tv, "<stdin>", stdin)); }
347 int tvec_readfile(struct tvec_state *tv, const char *file)
352 fp = fopen(file, "r");
354 tvec_error(tv, "failed to open `%s' for reading: %s",
355 file, strerror(errno));
358 if (tvec_read(tv, file, fp)) { rc = -1; goto end; }
365 int tvec_readarg(struct tvec_state *tv, const char *arg)
369 if (STRCMP(arg, ==, "-")) rc = tvec_readstdin(tv);
370 else rc = tvec_readfile(tv, arg);
374 /* --- @tvec_readdflt@ --- *
376 * Arguments: @struct tvec_state *tv@ = test vector state
377 * @const char *dflt@ = defsault filename or null
379 * Returns: Zero on success, @-1@ on error.
381 * Use: Reads from the default test vector data. If @file@ is null,
382 * then read from standard input, unless that's a terminal; if
383 * @file@ is not null, then read the named file, looking in the
384 * directory named by the `%|srcdir|%' environment variable if
385 * that's set, or otherwise in the current directory.
388 int tvec_readdflt(struct tvec_state *tv, const char *dflt)
395 p = getenv("srcdir");
396 if (p) { dstr_putf(&d, "%s/%s", p, dflt); dflt = d.buf; }
397 rc = tvec_readfile(tv, dflt);
398 } else if (isatty(0))
399 rc = tvec_error(tv, "use `-' to force reading from interactive stdin");
401 rc = tvec_readstdin(tv);
406 /* --- @tvec_readargs@ --- *
408 * Arguments: @int argc@ = number of command-line arguments
409 * @char *argv[]@ = vector of argument strings
410 * @struct tvec_state *tv@ = test vector state
411 * @int *argpos_inout@ = current argument position (updated)
412 * @const char *dflt@ = default filename or null
414 * Returns: Zero on success, @-1@ on error.
416 * Use: Reads from the sources indicated by the command-line
417 * arguments, in order, interpreting each as for @tvec_readarg@;
418 * if no arguments are given then read from @dflt@ as for
422 int tvec_readargs(int argc, char *argv[], struct tvec_state *tv,
423 int *argpos_inout, const char *dflt)
425 int i = *argpos_inout;
429 rc = tvec_readdflt(tv, dflt);
433 if (tvec_readarg(tv, argv[i++])) rc = -1;
439 /* --- @tvec_main@ --- *
441 * Arguments: @int argc@ = number of command-line arguments
442 * @char *argv[]@ = vector of argument strings
443 * @const struct tvec_config *config@ = test vector
445 * @const char *dflt@ = default filename or null
447 * Returns: Exit code.
449 * Use: All-in-one test vector front-end. Parse options from the
450 * command-line as for @tvec_parseargs@, and then process the
451 * remaining positional arguments as for @tvec_readargs@. The
452 * function constructs and disposes of a test vector state.
455 int tvec_main(int argc, char *argv[],
456 const struct tvec_config *config, const char *dflt)
458 struct tvec_state tv;
461 tvec_parseargs(argc, argv, &tv, &argpos, config);
462 tvec_readargs(argc, argv, &tv, &argpos, dflt);
463 return (tvec_end(&tv));
466 /*----- That's all, folks -------------------------------------------------*/