@@@ misc wip
[mLib] / test / tvec-main.c
CommitLineData
b64eb60f
MW
1/* -*-c-*-
2 *
3 * Main entry point for test-vector processing
4 *
5 * (c) 2023 Straylight/Edgeware
6 */
7
8/*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
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.
16 *
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.
21 *
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,
25 * USA.
26 */
27
28/*----- Header files ------------------------------------------------------*/
29
30#include "config.h"
31
32#include <ctype.h>
33#include <errno.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37
38#include <unistd.h>
39
40#include "bench.h"
41#include "dstr.h"
42#include "macros.h"
43#include "mdwopt.h"
44#include "quis.h"
45#include "report.h"
46#include "tvec.h"
47
48/*----- Main code ---------------------------------------------------------*/
49
67b5031e 50/* Table of output formats. */
b64eb60f
MW
51static const struct outform {
52 const char *name;
53 struct tvec_output *(*makefn)(FILE *fp);
54} outtab[] = {
55 { "human", tvec_humanoutput },
5c0f2e08 56 { "machine", tvec_machineoutput },
b64eb60f
MW
57 { "tap", tvec_tapoutput },
58 { 0, 0 }
59};
60
67b5031e 61/* Configuration for ad-hoc testing. */
c5e0e403 62const struct tvec_config tvec_adhocconfig =
b64eb60f
MW
63 { 0, 1, 1, sizeof(struct tvec_reg) };
64
13ee7406
MW
65/* Common benchmark state. */
66static struct bench_state bench;
67
67b5031e
MW
68/* --- @find_outform@ ---
69 *
70 * Arguments: @const char *p@ = output name
71 *
72 * Returns: Pointer to output format record.
73 *
74 * Use: Looks up an output format by name. Reports a fatal error if
75 * no matching record is found.
76 */
77
b64eb60f
MW
78static const struct outform *find_outform(const char *p)
79{
80 const struct outform *best = 0, *of;
81 size_t n = strlen(p);
82
83 for (of = outtab; of->name; of++)
84 if (!STRNCMP(p, ==, of->name, n))
85 ;
86 else if (!of->name[n])
87 return (of);
88 else if (best)
89 die(2, "ambiguous output format name (`%s' or `%s'?)",
90 best->name, of->name);
91 else
92 best = of;
93 if (best) return (best);
94 else die(2, "unknown output format `%s'", optarg);
95}
96
67b5031e
MW
97/* --- @version@, @usage@, @help@ --- *
98 *
99 * Arguments: @FILE *fp@ = stream to write on
100 *
101 * Returns: ---
102 *
103 * Use: Output information about the program.
104 */
105
b64eb60f
MW
106static void version(FILE *fp)
107 { pquis(fp, "$, mLib test-vector framework version " VERSION "\n"); }
108
109static void usage(FILE *fp)
110{
111 pquis(fp, "\
13ee7406 112usage: $ [-B CONFIG] [-f FORMAT] [-o OUTPUT] [-t SECS] [TEST-FILE ...]\n\
b64eb60f
MW
113");
114}
115
116static void help(FILE *fp)
117{
118 version(fp); fputc('\n', fp);
119 usage(fp); fputs("\
120Options:\n\
13ee7406
MW
121 -h, --help show this help text.\n\
122 -v, --version show version number.\n\
123 -u, --usage show usage synopsis.\n\
b64eb60f 124\n\
13ee7406
MW
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\
b64eb60f
MW
129", fp);
130}
131
67b5031e
MW
132/* --- @tvec_parseargs@ --- *
133 *
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
139 *
140 * Returns: ---
141 *
142 * Use: Parse arguments and set up the test vector state @*tv_out@.
143 * If errors occur, print messages to standard error and exit
144 * with status 2.
13ee7406
MW
145 *
146 * This function also establishes a common benchmark state.
67b5031e
MW
147 */
148
b64eb60f 149void tvec_parseargs(int argc, char *argv[], struct tvec_state *tv_out,
c5e0e403 150 int *argpos_out, const struct tvec_config *config)
b64eb60f
MW
151{
152 FILE *ofp = 0;
153 const struct outform *of = 0;
154 struct tvec_output *o;
13ee7406
MW
155 const char *benchconf = 0;
156 double benchtime = 1.0, scale;
157 struct bench_timer *tm;
158 const char *p; char *q;
b64eb60f 159 int opt;
b64eb60f
MW
160 unsigned f = 0;
161#define f_bogus 1u
13ee7406 162#define f_bench 2u
b64eb60f
MW
163
164 static const struct option options[] = {
165 { "help", 0, 0, 'h' },
166 { "version", 0, 0, 'v' },
167 { "usage", 0, 0, 'u' },
168
13ee7406 169 { "bench-config", OPTF_ARGREQ, 0, 'B' },
b64eb60f
MW
170 { "format", OPTF_ARGREQ, 0, 'f' },
171 { "output", OPTF_ARGREQ, 0, 'o' },
13ee7406 172 { "target-time", OPTF_ARGREQ, 0, 't' },
b64eb60f
MW
173 { 0, 0, 0, 0 }
174 };
175
b64eb60f
MW
176 ego(argv[0]);
177 for (;;) {
13ee7406 178 opt = mdwopt(argc, argv, "hvu" "B:f:o:t:", options, 0, 0, 0);
b64eb60f
MW
179 if (opt < 0) break;
180 switch (opt) {
181 case 'h': help(stdout); exit(0);
182 case 'v': version(stdout); exit(0);
183 case 'u': usage(stdout); exit(0);
184
13ee7406 185 case 'B': benchconf = optarg; f |= f_bench; break;
b64eb60f
MW
186 case 'f': of = find_outform(optarg); break;
187 case 'o':
188 if (ofp) fclose(ofp);
189 ofp = fopen(optarg, "w");
190 if (!ofp)
191 die(2, "failed to open `%s' for writing: %s",
192 optarg, strerror(errno));
193 break;
13ee7406
MW
194 case 't':
195 if (*optarg != '.' && !ISDIGIT(*optarg)) goto bad_time;
196 errno = 0; benchtime = strtod(optarg, &q);
197 if (errno) goto bad_time;
198 p = q;
199 if (ISSPACE(*p)) {
200 do p++; while (ISSPACE(*p));
201 if (!*p) goto bad_time;
202 }
203 if (tvec_parsedurunit(&scale, &p)) goto bad_time;
204 if (*p) goto bad_time;
205 benchtime *= scale; f |= f_bench;
206 break;
207 bad_time:
208 die(2, "invalid time duration `%s'", optarg);
b64eb60f
MW
209
210 default:
211 f |= f_bogus;
212 break;
213 }
214 }
215 if (f&f_bogus) { usage(stderr); exit(2); }
216 if (!ofp) ofp = stdout;
217 if (!of) {
218 p = getenv("TVEC_FORMAT");
219 if (p) of = find_outform(p);
220 }
221 if (of) o = of->makefn(ofp);
222 else o = tvec_dfltout(ofp);
223
13ee7406
MW
224 tm = bench_createtimer(benchconf);
225 if (tm) {
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");
232 exit(2);
233 }
234
c5e0e403 235 tvec_begin(tv_out, config, o); *argpos_out = optind;
b64eb60f
MW
236}
237
67b5031e
MW
238/* --- @tvec_readstdin@, @tvec_readfile@, @tvec_readarg@ --- *
239 *
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
243 *
244 * Returns: Zero on success, @-1@ on error.
245 *
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.
249 */
250
882a39c1
MW
251int tvec_readstdin(struct tvec_state *tv)
252 { return (tvec_read(tv, "<stdin>", stdin)); }
b64eb60f 253
882a39c1 254int tvec_readfile(struct tvec_state *tv, const char *file)
b64eb60f 255{
882a39c1
MW
256 FILE *fp = 0;
257 int rc;
b64eb60f
MW
258
259 fp = fopen(file, "r");
882a39c1 260 if (!fp) {
b64eb60f
MW
261 tvec_error(tv, "failed to open `%s' for reading: %s",
262 file, strerror(errno));
882a39c1
MW
263 rc = -1; goto end;
264 }
265 if (tvec_read(tv, file, fp)) { rc = -1; goto end; }
266 rc = 0;
267end:
268 if (fp) fclose(fp);
269 return (rc);
b64eb60f
MW
270}
271
67b5031e
MW
272int tvec_readarg(struct tvec_state *tv, const char *arg)
273{
274 int rc;
275
276 if (STRCMP(arg, ==, "-")) rc = tvec_readstdin(tv);
277 else rc = tvec_readfile(tv, arg);
278 return (rc);
279}
280
281/* --- @tvec_readdflt@ --- *
282 *
283 * Arguments: @struct tvec_state *tv@ = test vector state
284 * @const char *dflt@ = defsault filename or null
285 *
286 * Returns: Zero on success, @-1@ on error.
287 *
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.
293 */
294
295int tvec_readdflt(struct tvec_state *tv, const char *dflt)
b64eb60f
MW
296{
297 dstr d = DSTR_INIT;
298 const char *p;
882a39c1 299 int rc;
b64eb60f 300
67b5031e 301 if (dflt) {
b64eb60f 302 p = getenv("srcdir");
67b5031e
MW
303 if (p) { dstr_putf(&d, "%s/%s", p, dflt); dflt = d.buf; }
304 rc = tvec_readfile(tv, dflt);
b64eb60f 305 } else if (isatty(0))
882a39c1 306 rc = tvec_error(tv, "use `-' to force reading from interactive stdin");
b64eb60f 307 else
882a39c1
MW
308 rc = tvec_readstdin(tv);
309 dstr_destroy(&d);
310 return (rc);
b64eb60f
MW
311}
312
67b5031e
MW
313/* --- @tvec_readargs@ --- *
314 *
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
320 *
321 * Returns: Zero on success, @-1@ on error.
322 *
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
326 * @tvec_readdflt@.
327 */
b64eb60f 328
882a39c1 329int tvec_readargs(int argc, char *argv[], struct tvec_state *tv,
b64eb60f
MW
330 int *argpos_inout, const char *dflt)
331{
332 int i = *argpos_inout;
882a39c1 333 int rc;
b64eb60f 334
67b5031e
MW
335 if (i == argc)
336 rc = tvec_readdflt(tv, dflt);
882a39c1
MW
337 else {
338 rc = 0;
339 while (i < argc)
340 if (tvec_readarg(tv, argv[i++])) rc = -1;
341 }
b64eb60f 342 *argpos_inout = i;
882a39c1 343 return (rc);
b64eb60f
MW
344}
345
67b5031e
MW
346/* --- @tvec_main@ --- *
347 *
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
352 *
353 * Returns: Exit code.
354 *
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.
359 */
360
b64eb60f 361int tvec_main(int argc, char *argv[],
c5e0e403 362 const struct tvec_config *config, const char *dflt)
b64eb60f
MW
363{
364 struct tvec_state tv;
365 int argpos;
366
c5e0e403 367 tvec_parseargs(argc, argv, &tv, &argpos, config);
b64eb60f
MW
368 tvec_readargs(argc, argv, &tv, &argpos, dflt);
369 return (tvec_end(&tv));
370}
371
372/*----- That's all, folks -------------------------------------------------*/