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 { "tap", tvec_tapoutput },
60 /* Configuration for ad-hoc testing. */
61 const struct tvec_config tvec_adhocconfig =
62 { 0, 1, 1, sizeof(struct tvec_reg) };
64 /* Common benchmark state. */
65 static struct bench_state bench;
67 /* --- @find_outform@ ---
69 * Arguments: @const char *p@ = output name
71 * Returns: Pointer to output format record.
73 * Use: Looks up an output format by name. Reports a fatal error if
74 * no matching record is found.
77 static const struct outform *find_outform(const char *p)
79 const struct outform *best = 0, *of;
82 for (of = outtab; of->name; of++)
83 if (!STRNCMP(p, ==, of->name, n))
85 else if (!of->name[n])
88 die(2, "ambiguous output format name (`%s' or `%s'?)",
89 best->name, of->name);
92 if (best) return (best);
93 else die(2, "unknown output format `%s'", optarg);
96 /* --- @version@, @usage@, @help@ --- *
98 * Arguments: @FILE *fp@ = stream to write on
102 * Use: Output information about the program.
105 static void version(FILE *fp)
106 { pquis(fp, "$, mLib test-vector framework version " VERSION "\n"); }
108 static void usage(FILE *fp)
111 usage: $ [-B CONFIG] [-f FORMAT] [-o OUTPUT] [-t SECS] [TEST-FILE ...]\n\
115 static void help(FILE *fp)
117 version(fp); fputc('\n', fp);
120 -h, --help show this help text.\n\
121 -v, --version show version number.\n\
122 -u, --usage show usage synopsis.\n\
124 -B, --bench-config=CONFIG set benchmark configuration string.\n\
125 -f, --format=FORMAT produce output in FORMAT.\n\
126 -o, --output=OUTPUT write output to OUTPUT file.\n\
127 -t, --target-time=DURATION run benchmarks for DURATION.\n\
131 /* --- @tvec_parseargs@ --- *
133 * Arguments: @int argc@ = number of command-line arguments
134 * @char *argv[]@ = vector of argument strings
135 * @struct tvec_state *tv_out@ = test vector state to initialize
136 * @int *argpos_out@ = where to leave unread argument index
137 * @const struct tvec_config *cofig@ = test vector configuration
141 * Use: Parse arguments and set up the test vector state @*tv_out@.
142 * If errors occur, print messages to standard error and exit
145 * This function also establishes a common benchmark state.
148 void tvec_parseargs(int argc, char *argv[], struct tvec_state *tv_out,
149 int *argpos_out, const struct tvec_config *config)
152 const struct outform *of = 0;
153 struct tvec_output *o;
154 const char *benchconf = 0;
155 double benchtime = 1.0, scale;
156 struct bench_timer *tm;
157 const char *p; char *q;
163 static const struct option options[] = {
164 { "help", 0, 0, 'h' },
165 { "version", 0, 0, 'v' },
166 { "usage", 0, 0, 'u' },
168 { "bench-config", OPTF_ARGREQ, 0, 'B' },
169 { "format", OPTF_ARGREQ, 0, 'f' },
170 { "output", OPTF_ARGREQ, 0, 'o' },
171 { "target-time", OPTF_ARGREQ, 0, 't' },
177 opt = mdwopt(argc, argv, "hvu" "B:f:o:t:", options, 0, 0, 0);
180 case 'h': help(stdout); exit(0);
181 case 'v': version(stdout); exit(0);
182 case 'u': usage(stdout); exit(0);
184 case 'B': benchconf = optarg; f |= f_bench; break;
185 case 'f': of = find_outform(optarg); break;
187 if (ofp) fclose(ofp);
188 ofp = fopen(optarg, "w");
190 die(2, "failed to open `%s' for writing: %s",
191 optarg, strerror(errno));
194 if (*optarg != '.' && !ISDIGIT(*optarg)) goto bad_time;
195 errno = 0; benchtime = strtod(optarg, &q);
196 if (errno) goto bad_time;
199 do p++; while (ISSPACE(*p));
200 if (!*p) goto bad_time;
202 if (tvec_parsedurunit(&scale, &p)) goto bad_time;
203 if (*p) goto bad_time;
204 benchtime *= scale; f |= f_bench;
207 die(2, "invalid time duration `%s'", optarg);
214 if (f&f_bogus) { usage(stderr); exit(2); }
215 if (!ofp) ofp = stdout;
217 p = getenv("TVEC_FORMAT");
218 if (p) of = find_outform(p);
220 if (of) o = of->makefn(ofp);
221 else o = tvec_dfltout(ofp);
223 tm = bench_createtimer(benchconf);
225 bench_init(&bench, tm); bench.target_s = benchtime;
226 tvec_benchstate = &bench;
227 } else if (f&f_bench) {
228 moan("failed to create benchmark timer");
229 if (!getenv("MLIB_BENCH_DEBUG"))
230 moan("set `MLIB_BENCH_DEBUG=t' in the envionment for more detail");
234 tvec_begin(tv_out, config, o); *argpos_out = optind;
237 /* --- @tvec_readstdin@, @tvec_readfile@, @tvec_readarg@ --- *
239 * Arguments: @struct tvec_state *tv@ = test vector state
240 * @const char *file@ = pathname of file to read
241 * @const char *arg@ = argument to interpret
243 * Returns: Zero on success, @-1@ on error.
245 * Use: Read test vector data from stdin or a named file. The
246 * @tvec_readarg@ function reads from stdin if @arg@ is `%|-|%',
247 * and from the named file otherwise.
250 int tvec_readstdin(struct tvec_state *tv)
251 { return (tvec_read(tv, "<stdin>", stdin)); }
253 int tvec_readfile(struct tvec_state *tv, const char *file)
258 fp = fopen(file, "r");
260 tvec_error(tv, "failed to open `%s' for reading: %s",
261 file, strerror(errno));
264 if (tvec_read(tv, file, fp)) { rc = -1; goto end; }
271 int tvec_readarg(struct tvec_state *tv, const char *arg)
275 if (STRCMP(arg, ==, "-")) rc = tvec_readstdin(tv);
276 else rc = tvec_readfile(tv, arg);
280 /* --- @tvec_readdflt@ --- *
282 * Arguments: @struct tvec_state *tv@ = test vector state
283 * @const char *dflt@ = defsault filename or null
285 * Returns: Zero on success, @-1@ on error.
287 * Use: Reads from the default test vector data. If @file@ is null,
288 * then read from standard input, unless that's a terminal; if
289 * @file@ is not null, then read the named file, looking in the
290 * directory named by the `%|srcdir|%' environment variable if
291 * that's set, or otherwise in the current directory.
294 int tvec_readdflt(struct tvec_state *tv, const char *dflt)
301 p = getenv("srcdir");
302 if (p) { dstr_putf(&d, "%s/%s", p, dflt); dflt = d.buf; }
303 rc = tvec_readfile(tv, dflt);
304 } else if (isatty(0))
305 rc = tvec_error(tv, "use `-' to force reading from interactive stdin");
307 rc = tvec_readstdin(tv);
312 /* --- @tvec_readargs@ --- *
314 * Arguments: @int argc@ = number of command-line arguments
315 * @char *argv[]@ = vector of argument strings
316 * @struct tvec_state *tv@ = test vector state
317 * @int *argpos_inout@ = current argument position (updated)
318 * @const char *dflt@ = default filename or null
320 * Returns: Zero on success, @-1@ on error.
322 * Use: Reads from the sources indicated by the command-line
323 * arguments, in order, interpreting each as for @tvec_readarg@;
324 * if no arguments are given then read from @dflt@ as for
328 int tvec_readargs(int argc, char *argv[], struct tvec_state *tv,
329 int *argpos_inout, const char *dflt)
331 int i = *argpos_inout;
335 rc = tvec_readdflt(tv, dflt);
339 if (tvec_readarg(tv, argv[i++])) rc = -1;
345 /* --- @tvec_main@ --- *
347 * Arguments: @int argc@ = number of command-line arguments
348 * @char *argv[]@ = vector of argument strings
349 * @const struct tvec_config *cofig@ = test vector configuration
350 * @const char *dflt@ = default filename or null
352 * Returns: Exit code.
354 * Use: All-in-one test vector front-end. Parse options from the
355 * command-line as for @tvec_parseargs@, and then process the
356 * remaining positional arguments as for @tvec_readargs@. The
357 * function constructs and disposes of a test vector state.
360 int tvec_main(int argc, char *argv[],
361 const struct tvec_config *config, const char *dflt)
363 struct tvec_state tv;
366 tvec_parseargs(argc, argv, &tv, &argpos, config);
367 tvec_readargs(argc, argv, &tv, &argpos, dflt);
368 return (tvec_end(&tv));
371 /*----- That's all, folks -------------------------------------------------*/