chiark / gitweb /
@@@ misc wip
[mLib] / test / tvec-main.c
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
50 /* Table of output formats. */
51 static const struct outform {
52   const char *name;
53   struct tvec_output *(*makefn)(FILE *fp);
54 } outtab[] = {
55   { "human",            tvec_humanoutput },
56   { "machine",          tvec_machineoutput },
57   { "tap",              tvec_tapoutput },
58   { 0,                  0 }
59 };
60
61 /* Configuration for ad-hoc testing. */
62 const struct tvec_config tvec_adhocconfig =
63   { 0, 1, 1, sizeof(struct tvec_reg) };
64
65 /* Common benchmark state. */
66 static struct bench_state bench;
67
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
78 static 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
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
106 static void version(FILE *fp)
107   { pquis(fp, "$, mLib test-vector framework version " VERSION "\n"); }
108
109 static void usage(FILE *fp)
110 {
111   pquis(fp, "\
112 usage: $ [-B CONFIG] [-f FORMAT] [-o OUTPUT] [-t SECS] [TEST-FILE ...]\n\
113 ");
114 }
115
116 static void help(FILE *fp)
117 {
118   version(fp); fputc('\n', fp);
119   usage(fp); fputs("\
120 Options:\n\
121   -h, --help                    show this help text.\n\
122   -v, --version                 show version number.\n\
123   -u, --usage                   show usage synopsis.\n\
124 \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\
129 ", fp);
130 }
131
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.
145  *
146  *              This function also establishes a common benchmark state.
147  */
148
149 void tvec_parseargs(int argc, char *argv[], struct tvec_state *tv_out,
150                     int *argpos_out, const struct tvec_config *config)
151 {
152   FILE *ofp = 0;
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;
159   int opt;
160   unsigned f = 0;
161 #define f_bogus 1u
162 #define f_bench 2u
163
164   static const struct option options[] = {
165     { "help",           0,              0,      'h' },
166     { "version",        0,              0,      'v' },
167     { "usage",          0,              0,      'u' },
168
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' },
173     { 0,                0,              0,      0 }
174   };
175
176   ego(argv[0]);
177   for (;;) {
178     opt = mdwopt(argc, argv, "hvu" "B:f:o:t:", options, 0, 0, 0);
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
185       case 'B': benchconf = optarg; f |= f_bench; break;
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;
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);
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
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
235   tvec_begin(tv_out, config, o); *argpos_out = optind;
236 }
237
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
251 int tvec_readstdin(struct tvec_state *tv)
252   { return (tvec_read(tv, "<stdin>", stdin)); }
253
254 int tvec_readfile(struct tvec_state *tv, const char *file)
255 {
256   FILE *fp = 0;
257   int rc;
258
259   fp = fopen(file, "r");
260   if (!fp) {
261     tvec_error(tv, "failed to open `%s' for reading: %s",
262                file, strerror(errno));
263     rc = -1; goto end;
264   }
265   if (tvec_read(tv, file, fp)) { rc = -1; goto end; }
266   rc = 0;
267 end:
268   if (fp) fclose(fp);
269   return (rc);
270 }
271
272 int 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
295 int tvec_readdflt(struct tvec_state *tv, const char *dflt)
296 {
297   dstr d = DSTR_INIT;
298   const char *p;
299   int rc;
300
301   if (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");
307   else
308     rc = tvec_readstdin(tv);
309   dstr_destroy(&d);
310   return (rc);
311 }
312
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  */
328
329 int tvec_readargs(int argc, char *argv[], struct tvec_state *tv,
330                    int *argpos_inout, const char *dflt)
331 {
332   int i = *argpos_inout;
333   int rc;
334
335   if (i == argc)
336     rc = tvec_readdflt(tv, dflt);
337   else {
338     rc = 0;
339     while (i < argc)
340       if (tvec_readarg(tv, argv[i++])) rc = -1;
341   }
342   *argpos_inout = i;
343   return (rc);
344 }
345
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
361 int tvec_main(int argc, char *argv[],
362               const struct tvec_config *config, const char *dflt)
363 {
364   struct tvec_state tv;
365   int argpos;
366
367   tvec_parseargs(argc, argv, &tv, &argpos, config);
368   tvec_readargs(argc, argv, &tv, &argpos, dflt);
369   return (tvec_end(&tv));
370 }
371
372 /*----- That's all, folks -------------------------------------------------*/