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 const struct outform {
57 struct tvec_output *(*makefn)(FILE *fp);
59 { "human", tvec_humanoutput },
60 { "machine", tvec_machineoutput },
61 { "tap", tvec_tapoutput },
65 /* Configuration for ad-hoc testing. */
66 const struct tvec_config tvec_adhocconfig =
67 { 0, 1, 1, sizeof(struct tvec_reg) };
69 /* Common benchmark state. */
70 static struct bench_state bench;
72 /* --- @find_outform@ ---
74 * Arguments: @const char *p@ = output name
76 * Returns: Pointer to output format record.
78 * Use: Looks up an output format by name. Reports a fatal error if
79 * no matching record is found.
82 static const struct outform *find_outform(const char *p)
84 const struct outform *best = 0, *of;
87 for (of = outtab; of->name; of++)
88 if (!STRNCMP(p, ==, of->name, n))
90 else if (!of->name[n])
93 die(2, "ambiguous output format name (`%s' or `%s'?)",
94 best->name, of->name);
97 if (best) return (best);
98 else die(2, "unknown output format `%s'", optarg);
101 /* --- @version@, @usage@, @help@ --- *
103 * Arguments: @FILE *fp@ = stream to write on
107 * Use: Output information about the program.
110 static void version(FILE *fp)
111 { pquis(fp, "$, mLib test-vector framework version " VERSION "\n"); }
113 static void usage(FILE *fp)
116 usage: $ [-B CONFIG] [-f FORMAT] [-o OUTPUT] [-t SECS] [TEST-FILE ...]\n\
120 static void help(FILE *fp)
122 version(fp); fputc('\n', fp);
125 -h, --help show this help text.\n\
126 -v, --version show version number.\n\
127 -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\
136 /* --- @tvec_parseargs@ --- *
138 * Arguments: @int argc@ = number of command-line arguments
139 * @char *argv[]@ = vector of argument strings
140 * @struct tvec_state *tv_out@ = test vector state to initialize
141 * @int *argpos_out@ = where to leave unread argument index
142 * @const struct tvec_config *cofig@ = test vector configuration
146 * Use: Parse arguments and set up the test vector state @*tv_out@.
147 * If errors occur, print messages to standard error and exit
150 * This function also establishes a common benchmark state.
153 void tvec_parseargs(int argc, char *argv[], struct tvec_state *tv_out,
154 int *argpos_out, const struct tvec_config *config)
157 const struct outform *of = 0;
158 struct tvec_output *o;
159 const char *benchconf = 0;
160 double benchtime = 1.0, scale;
161 struct bench_timer *tm;
162 const char *p; char *q;
168 static const struct option options[] = {
169 { "help", 0, 0, 'h' },
170 { "version", 0, 0, 'v' },
171 { "usage", 0, 0, 'u' },
173 { "bench-config", OPTF_ARGREQ, 0, 'B' },
174 { "format", OPTF_ARGREQ, 0, 'f' },
175 { "output", OPTF_ARGREQ, 0, 'o' },
176 { "target-time", OPTF_ARGREQ, 0, 't' },
182 opt = mdwopt(argc, argv, "hvu" "B:f:o:t:", options, 0, 0, 0);
185 case 'h': help(stdout); exit(0);
186 case 'v': version(stdout); exit(0);
187 case 'u': usage(stdout); exit(0);
189 case 'B': benchconf = optarg; f |= f_bench; break;
190 case 'f': of = find_outform(optarg); break;
192 if (ofp) fclose(ofp);
193 ofp = fopen(optarg, "w");
195 die(2, "failed to open `%s' for writing: %s",
196 optarg, strerror(errno));
199 if (*optarg != '.' && !ISDIGIT(*optarg)) goto bad_time;
200 errno = 0; benchtime = strtod(optarg, &q);
201 if (errno) goto bad_time;
204 do p++; while (ISSPACE(*p));
205 if (!*p) goto bad_time;
207 if (tvec_parsedurunit(&scale, &p)) goto bad_time;
208 if (*p) goto bad_time;
209 benchtime *= scale; f |= f_bench;
212 die(2, "invalid time duration `%s'", optarg);
219 if (f&f_bogus) { usage(stderr); exit(2); }
220 if (!ofp) ofp = stdout;
222 p = getenv("TVEC_FORMAT");
223 if (p) of = find_outform(p);
225 if (of) o = of->makefn(ofp);
226 else o = tvec_dfltout(ofp);
228 tm = bench_createtimer(benchconf);
230 bench_init(&bench, tm); bench.target_s = benchtime;
231 tvec_benchstate = &bench;
232 } else if (f&f_bench) {
233 moan("failed to create benchmark timer");
234 if (!getenv("MLIB_BENCH_DEBUG"))
235 moan("set `MLIB_BENCH_DEBUG=t' in the envionment for more detail");
239 tvec_begin(tv_out, config, o); *argpos_out = optind;
242 /* --- @tvec_readstdin@, @tvec_readfile@, @tvec_readarg@ --- *
244 * Arguments: @struct tvec_state *tv@ = test vector state
245 * @const char *file@ = pathname of file to read
246 * @const char *arg@ = argument to interpret
248 * Returns: Zero on success, @-1@ on error.
250 * Use: Read test vector data from stdin or a named file. The
251 * @tvec_readarg@ function reads from stdin if @arg@ is `%|-|%',
252 * and from the named file otherwise.
255 int tvec_readstdin(struct tvec_state *tv)
256 { return (tvec_read(tv, "<stdin>", stdin)); }
258 int tvec_readfile(struct tvec_state *tv, const char *file)
263 fp = fopen(file, "r");
265 tvec_error(tv, "failed to open `%s' for reading: %s",
266 file, strerror(errno));
269 if (tvec_read(tv, file, fp)) { rc = -1; goto end; }
276 int tvec_readarg(struct tvec_state *tv, const char *arg)
280 if (STRCMP(arg, ==, "-")) rc = tvec_readstdin(tv);
281 else rc = tvec_readfile(tv, arg);
285 /* --- @tvec_readdflt@ --- *
287 * Arguments: @struct tvec_state *tv@ = test vector state
288 * @const char *dflt@ = defsault filename or null
290 * Returns: Zero on success, @-1@ on error.
292 * Use: Reads from the default test vector data. If @file@ is null,
293 * then read from standard input, unless that's a terminal; if
294 * @file@ is not null, then read the named file, looking in the
295 * directory named by the `%|srcdir|%' environment variable if
296 * that's set, or otherwise in the current directory.
299 int tvec_readdflt(struct tvec_state *tv, const char *dflt)
306 p = getenv("srcdir");
307 if (p) { dstr_putf(&d, "%s/%s", p, dflt); dflt = d.buf; }
308 rc = tvec_readfile(tv, dflt);
309 } else if (isatty(0))
310 rc = tvec_error(tv, "use `-' to force reading from interactive stdin");
312 rc = tvec_readstdin(tv);
317 /* --- @tvec_readargs@ --- *
319 * Arguments: @int argc@ = number of command-line arguments
320 * @char *argv[]@ = vector of argument strings
321 * @struct tvec_state *tv@ = test vector state
322 * @int *argpos_inout@ = current argument position (updated)
323 * @const char *dflt@ = default filename or null
325 * Returns: Zero on success, @-1@ on error.
327 * Use: Reads from the sources indicated by the command-line
328 * arguments, in order, interpreting each as for @tvec_readarg@;
329 * if no arguments are given then read from @dflt@ as for
333 int tvec_readargs(int argc, char *argv[], struct tvec_state *tv,
334 int *argpos_inout, const char *dflt)
336 int i = *argpos_inout;
340 rc = tvec_readdflt(tv, dflt);
344 if (tvec_readarg(tv, argv[i++])) rc = -1;
350 /* --- @tvec_main@ --- *
352 * Arguments: @int argc@ = number of command-line arguments
353 * @char *argv[]@ = vector of argument strings
354 * @const struct tvec_config *cofig@ = test vector configuration
355 * @const char *dflt@ = default filename or null
357 * Returns: Exit code.
359 * Use: All-in-one test vector front-end. Parse options from the
360 * command-line as for @tvec_parseargs@, and then process the
361 * remaining positional arguments as for @tvec_readargs@. The
362 * function constructs and disposes of a test vector state.
365 int tvec_main(int argc, char *argv[],
366 const struct tvec_config *config, const char *dflt)
368 struct tvec_state tv;
371 tvec_parseargs(argc, argv, &tv, &argpos, config);
372 tvec_readargs(argc, argv, &tv, &argpos, dflt);
373 return (tvec_end(&tv));
376 /*----- That's all, folks -------------------------------------------------*/