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 /*----- Main code ---------------------------------------------------------*/
50 /* Table of output formats. */
51 static const struct outform {
53 struct tvec_output *(*makefn)(FILE *fp);
55 { "human", tvec_humanoutput },
56 { "machine", tvec_machineoutput },
57 { "tap", tvec_tapoutput },
61 /* Configuration for ad-hoc testing. */
62 const struct tvec_config tvec_adhocconfig =
63 { 0, 1, 1, sizeof(struct tvec_reg) };
65 /* Common benchmark state. */
66 static struct bench_state bench;
68 /* --- @find_outform@ ---
70 * Arguments: @const char *p@ = output name
72 * Returns: Pointer to output format record.
74 * Use: Looks up an output format by name. Reports a fatal error if
75 * no matching record is found.
78 static const struct outform *find_outform(const char *p)
80 const struct outform *best = 0, *of;
83 for (of = outtab; of->name; of++)
84 if (!STRNCMP(p, ==, of->name, n))
86 else if (!of->name[n])
89 die(2, "ambiguous output format name (`%s' or `%s'?)",
90 best->name, of->name);
93 if (best) return (best);
94 else die(2, "unknown output format `%s'", optarg);
97 /* --- @version@, @usage@, @help@ --- *
99 * Arguments: @FILE *fp@ = stream to write on
103 * Use: Output information about the program.
106 static void version(FILE *fp)
107 { pquis(fp, "$, mLib test-vector framework version " VERSION "\n"); }
109 static void usage(FILE *fp)
112 usage: $ [-B CONFIG] [-f FORMAT] [-o OUTPUT] [-t SECS] [TEST-FILE ...]\n\
116 static void help(FILE *fp)
118 version(fp); fputc('\n', fp);
121 -h, --help show this help text.\n\
122 -v, --version show version number.\n\
123 -u, --usage show usage synopsis.\n\
125 -B, --bench-config=CONFIG set benchmark configuration string.\n\
126 -f, --format=FORMAT produce output in FORMAT.\n\
127 -o, --output=OUTPUT write output to OUTPUT file.\n\
128 -t, --target-time=DURATION run benchmarks for DURATION.\n\
132 /* --- @tvec_parseargs@ --- *
134 * Arguments: @int argc@ = number of command-line arguments
135 * @char *argv[]@ = vector of argument strings
136 * @struct tvec_state *tv_out@ = test vector state to initialize
137 * @int *argpos_out@ = where to leave unread argument index
138 * @const struct tvec_config *cofig@ = test vector configuration
142 * Use: Parse arguments and set up the test vector state @*tv_out@.
143 * If errors occur, print messages to standard error and exit
146 * This function also establishes a common benchmark state.
149 void tvec_parseargs(int argc, char *argv[], struct tvec_state *tv_out,
150 int *argpos_out, const struct tvec_config *config)
153 const struct outform *of = 0;
154 struct tvec_output *o;
155 const char *benchconf = 0;
156 double benchtime = 1.0, scale;
157 struct bench_timer *tm;
158 const char *p; char *q;
164 static const struct option options[] = {
165 { "help", 0, 0, 'h' },
166 { "version", 0, 0, 'v' },
167 { "usage", 0, 0, 'u' },
169 { "bench-config", OPTF_ARGREQ, 0, 'B' },
170 { "format", OPTF_ARGREQ, 0, 'f' },
171 { "output", OPTF_ARGREQ, 0, 'o' },
172 { "target-time", OPTF_ARGREQ, 0, 't' },
178 opt = mdwopt(argc, argv, "hvu" "B:f:o:t:", options, 0, 0, 0);
181 case 'h': help(stdout); exit(0);
182 case 'v': version(stdout); exit(0);
183 case 'u': usage(stdout); exit(0);
185 case 'B': benchconf = optarg; f |= f_bench; break;
186 case 'f': of = find_outform(optarg); break;
188 if (ofp) fclose(ofp);
189 ofp = fopen(optarg, "w");
191 die(2, "failed to open `%s' for writing: %s",
192 optarg, strerror(errno));
195 if (*optarg != '.' && !ISDIGIT(*optarg)) goto bad_time;
196 errno = 0; benchtime = strtod(optarg, &q);
197 if (errno) goto bad_time;
200 do p++; while (ISSPACE(*p));
201 if (!*p) goto bad_time;
203 if (tvec_parsedurunit(&scale, &p)) goto bad_time;
204 if (*p) goto bad_time;
205 benchtime *= scale; f |= f_bench;
208 die(2, "invalid time duration `%s'", optarg);
215 if (f&f_bogus) { usage(stderr); exit(2); }
216 if (!ofp) ofp = stdout;
218 p = getenv("TVEC_FORMAT");
219 if (p) of = find_outform(p);
221 if (of) o = of->makefn(ofp);
222 else o = tvec_dfltout(ofp);
224 tm = bench_createtimer(benchconf);
226 bench_init(&bench, tm); bench.target_s = benchtime;
227 tvec_benchstate = &bench;
228 } else if (f&f_bench) {
229 moan("failed to create benchmark timer");
230 if (!getenv("MLIB_BENCH_DEBUG"))
231 moan("set `MLIB_BENCH_DEBUG=t' in the envionment for more detail");
235 tvec_begin(tv_out, config, o); *argpos_out = optind;
238 /* --- @tvec_readstdin@, @tvec_readfile@, @tvec_readarg@ --- *
240 * Arguments: @struct tvec_state *tv@ = test vector state
241 * @const char *file@ = pathname of file to read
242 * @const char *arg@ = argument to interpret
244 * Returns: Zero on success, @-1@ on error.
246 * Use: Read test vector data from stdin or a named file. The
247 * @tvec_readarg@ function reads from stdin if @arg@ is `%|-|%',
248 * and from the named file otherwise.
251 int tvec_readstdin(struct tvec_state *tv)
252 { return (tvec_read(tv, "<stdin>", stdin)); }
254 int tvec_readfile(struct tvec_state *tv, const char *file)
259 fp = fopen(file, "r");
261 tvec_error(tv, "failed to open `%s' for reading: %s",
262 file, strerror(errno));
265 if (tvec_read(tv, file, fp)) { rc = -1; goto end; }
272 int tvec_readarg(struct tvec_state *tv, const char *arg)
276 if (STRCMP(arg, ==, "-")) rc = tvec_readstdin(tv);
277 else rc = tvec_readfile(tv, arg);
281 /* --- @tvec_readdflt@ --- *
283 * Arguments: @struct tvec_state *tv@ = test vector state
284 * @const char *dflt@ = defsault filename or null
286 * Returns: Zero on success, @-1@ on error.
288 * Use: Reads from the default test vector data. If @file@ is null,
289 * then read from standard input, unless that's a terminal; if
290 * @file@ is not null, then read the named file, looking in the
291 * directory named by the `%|srcdir|%' environment variable if
292 * that's set, or otherwise in the current directory.
295 int tvec_readdflt(struct tvec_state *tv, const char *dflt)
302 p = getenv("srcdir");
303 if (p) { dstr_putf(&d, "%s/%s", p, dflt); dflt = d.buf; }
304 rc = tvec_readfile(tv, dflt);
305 } else if (isatty(0))
306 rc = tvec_error(tv, "use `-' to force reading from interactive stdin");
308 rc = tvec_readstdin(tv);
313 /* --- @tvec_readargs@ --- *
315 * Arguments: @int argc@ = number of command-line arguments
316 * @char *argv[]@ = vector of argument strings
317 * @struct tvec_state *tv@ = test vector state
318 * @int *argpos_inout@ = current argument position (updated)
319 * @const char *dflt@ = default filename or null
321 * Returns: Zero on success, @-1@ on error.
323 * Use: Reads from the sources indicated by the command-line
324 * arguments, in order, interpreting each as for @tvec_readarg@;
325 * if no arguments are given then read from @dflt@ as for
329 int tvec_readargs(int argc, char *argv[], struct tvec_state *tv,
330 int *argpos_inout, const char *dflt)
332 int i = *argpos_inout;
336 rc = tvec_readdflt(tv, dflt);
340 if (tvec_readarg(tv, argv[i++])) rc = -1;
346 /* --- @tvec_main@ --- *
348 * Arguments: @int argc@ = number of command-line arguments
349 * @char *argv[]@ = vector of argument strings
350 * @const struct tvec_config *cofig@ = test vector configuration
351 * @const char *dflt@ = default filename or null
353 * Returns: Exit code.
355 * Use: All-in-one test vector front-end. Parse options from the
356 * command-line as for @tvec_parseargs@, and then process the
357 * remaining positional arguments as for @tvec_readargs@. The
358 * function constructs and disposes of a test vector state.
361 int tvec_main(int argc, char *argv[],
362 const struct tvec_config *config, const char *dflt)
364 struct tvec_state tv;
367 tvec_parseargs(argc, argv, &tv, &argpos, config);
368 tvec_readargs(argc, argv, &tv, &argpos, dflt);
369 return (tvec_end(&tv));
372 /*----- That's all, folks -------------------------------------------------*/