@@@ tvec error return
[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
50static const struct outform {
51 const char *name;
52 struct tvec_output *(*makefn)(FILE *fp);
53} outtab[] = {
54 { "human", tvec_humanoutput },
55 { "tap", tvec_tapoutput },
56 { 0, 0 }
57};
58
59static struct bench_state benchstate;
60
61const struct tvec_info tvec_adhocinfo =
62 { 0, 1, 1, sizeof(struct tvec_reg) };
63
64static const struct outform *find_outform(const char *p)
65{
66 const struct outform *best = 0, *of;
67 size_t n = strlen(p);
68
69 for (of = outtab; of->name; of++)
70 if (!STRNCMP(p, ==, of->name, n))
71 ;
72 else if (!of->name[n])
73 return (of);
74 else if (best)
75 die(2, "ambiguous output format name (`%s' or `%s'?)",
76 best->name, of->name);
77 else
78 best = of;
79 if (best) return (best);
80 else die(2, "unknown output format `%s'", optarg);
81}
82
83static void version(FILE *fp)
84 { pquis(fp, "$, mLib test-vector framework version " VERSION "\n"); }
85
86static void usage(FILE *fp)
87{
88 pquis(fp, "\
89usage: $ [-f FORMAT] [-o OUTPUT] [-t SECS] [TEST-FILE ...]\n\
90");
91}
92
93static void help(FILE *fp)
94{
95 version(fp); fputc('\n', fp);
96 usage(fp); fputs("\
97Options:\n\
98 -h, --help show this help text.\n\
99 -v, --version show version number.\n\
100 -u, --usage show usage synopsis.\n\
101\n\
102 -f, --format=FORMAT produce output in FORMAT.\n\
103 -o, --output=OUTPUT write output to OUTPUT file.\n\
104 -t, --target=SECS aim to run benchmarks for SECS.\n\
105", fp);
106}
107
108void tvec_parseargs(int argc, char *argv[], struct tvec_state *tv_out,
109 int *argpos_out, const struct tvec_info *info)
110{
111 FILE *ofp = 0;
112 const struct outform *of = 0;
113 struct tvec_output *o;
114 struct bench_timer *tm;
115 const char *p; char *q;
116 int opt;
117 double t;
118 unsigned f = 0;
119#define f_bogus 1u
120
121 static const struct option options[] = {
122 { "help", 0, 0, 'h' },
123 { "version", 0, 0, 'v' },
124 { "usage", 0, 0, 'u' },
125
126 { "format", OPTF_ARGREQ, 0, 'f' },
127 { "output", OPTF_ARGREQ, 0, 'o' },
128 { "target", OPTF_ARGREQ, 0, 't' },
129 { 0, 0, 0, 0 }
130 };
131
132 tvec_benchstate = &benchstate;
133 tm = bench_createtimer(); bench_init(&benchstate, tm);
134 ego(argv[0]);
135 for (;;) {
136 opt = mdwopt(argc, argv, "hvu" "f:o:t:", options, 0, 0, 0);
137 if (opt < 0) break;
138 switch (opt) {
139 case 'h': help(stdout); exit(0);
140 case 'v': version(stdout); exit(0);
141 case 'u': usage(stdout); exit(0);
142
143 case 'f': of = find_outform(optarg); break;
144 case 'o':
145 if (ofp) fclose(ofp);
146 ofp = fopen(optarg, "w");
147 if (!ofp)
148 die(2, "failed to open `%s' for writing: %s",
149 optarg, strerror(errno));
150 break;
151 case 't':
152 errno = 0; t = strtod(optarg, &q);
153 while (ISSPACE(*q)) q++;
154 if (errno || *q || t < 0) die(2, "invalid time `%s'", optarg);
155 benchstate.target_s = t;
156 break;
157
158 default:
159 f |= f_bogus;
160 break;
161 }
162 }
163 if (f&f_bogus) { usage(stderr); exit(2); }
164 if (!ofp) ofp = stdout;
165 if (!of) {
166 p = getenv("TVEC_FORMAT");
167 if (p) of = find_outform(p);
168 }
169 if (of) o = of->makefn(ofp);
170 else o = tvec_dfltout(ofp);
171
172 tvec_begin(tv_out, info, o); *argpos_out = optind;
173}
174
882a39c1
MW
175int tvec_readstdin(struct tvec_state *tv)
176 { return (tvec_read(tv, "<stdin>", stdin)); }
b64eb60f 177
882a39c1 178int tvec_readfile(struct tvec_state *tv, const char *file)
b64eb60f 179{
882a39c1
MW
180 FILE *fp = 0;
181 int rc;
b64eb60f
MW
182
183 fp = fopen(file, "r");
882a39c1 184 if (!fp) {
b64eb60f
MW
185 tvec_error(tv, "failed to open `%s' for reading: %s",
186 file, strerror(errno));
882a39c1
MW
187 rc = -1; goto end;
188 }
189 if (tvec_read(tv, file, fp)) { rc = -1; goto end; }
190 rc = 0;
191end:
192 if (fp) fclose(fp);
193 return (rc);
b64eb60f
MW
194}
195
882a39c1 196int tvec_readdflt(struct tvec_state *tv, const char *file)
b64eb60f
MW
197{
198 dstr d = DSTR_INIT;
199 const char *p;
882a39c1 200 int rc;
b64eb60f
MW
201
202 if (file) {
203 p = getenv("srcdir");
882a39c1
MW
204 if (p) { dstr_putf(&d, "%s/%s", p, file); file = d.buf; }
205 rc = tvec_readfile(tv, file);
b64eb60f 206 } else if (isatty(0))
882a39c1 207 rc = tvec_error(tv, "use `-' to force reading from interactive stdin");
b64eb60f 208 else
882a39c1
MW
209 rc = tvec_readstdin(tv);
210 dstr_destroy(&d);
211 return (rc);
b64eb60f
MW
212}
213
882a39c1 214int tvec_readarg(struct tvec_state *tv, const char *arg)
b64eb60f 215{
882a39c1
MW
216 int rc;
217
218 if (STRCMP(arg, ==, "-")) rc = tvec_readstdin(tv);
219 else rc = tvec_readfile(tv, arg);
220 return (rc);
b64eb60f
MW
221}
222
882a39c1 223int tvec_readargs(int argc, char *argv[], struct tvec_state *tv,
b64eb60f
MW
224 int *argpos_inout, const char *dflt)
225{
226 int i = *argpos_inout;
882a39c1 227 int rc;
b64eb60f 228
882a39c1
MW
229 if (i == argc) rc = tvec_readdflt(tv, dflt);
230 else {
231 rc = 0;
232 while (i < argc)
233 if (tvec_readarg(tv, argv[i++])) rc = -1;
234 }
b64eb60f 235 *argpos_inout = i;
882a39c1 236 return (rc);
b64eb60f
MW
237}
238
239int tvec_main(int argc, char *argv[],
240 const struct tvec_info *info, const char *dflt)
241{
242 struct tvec_state tv;
243 int argpos;
244
245 tvec_parseargs(argc, argv, &tv, &argpos, info);
246 tvec_readargs(argc, argv, &tv, &argpos, dflt);
247 return (tvec_end(&tv));
248}
249
250/*----- That's all, folks -------------------------------------------------*/