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 /* --- @find_outform@ ---
66 * Arguments: @const char *p@ = output name
68 * Returns: Pointer to output format record.
70 * Use: Looks up an output format by name. Reports a fatal error if
71 * no matching record is found.
74 static const struct outform
*find_outform(const char *p
)
76 const struct outform
*best
= 0, *of
;
79 for (of
= outtab
; of
->name
; of
++)
80 if (!STRNCMP(p
, ==, of
->name
, n
))
82 else if (!of
->name
[n
])
85 die(2, "ambiguous output format name (`%s' or `%s'?)",
86 best
->name
, of
->name
);
89 if (best
) return (best
);
90 else die(2, "unknown output format `%s'", optarg
);
93 /* --- @version@, @usage@, @help@ --- *
95 * Arguments: @FILE *fp@ = stream to write on
99 * Use: Output information about the program.
102 static void version(FILE *fp
)
103 { pquis(fp
, "$, mLib test-vector framework version " VERSION
"\n"); }
105 static void usage(FILE *fp
)
108 usage: $ [-f FORMAT] [-o OUTPUT] [-t SECS] [TEST-FILE ...]\n\
112 static void help(FILE *fp
)
114 version(fp
); fputc('\n', fp
);
117 -h, --help show this help text.\n\
118 -v, --version show version number.\n\
119 -u, --usage show usage synopsis.\n\
121 -f, --format=FORMAT produce output in FORMAT.\n\
122 -o, --output=OUTPUT write output to OUTPUT file.\n\
126 /* --- @tvec_parseargs@ --- *
128 * Arguments: @int argc@ = number of command-line arguments
129 * @char *argv[]@ = vector of argument strings
130 * @struct tvec_state *tv_out@ = test vector state to initialize
131 * @int *argpos_out@ = where to leave unread argument index
132 * @const struct tvec_config *cofig@ = test vector configuration
136 * Use: Parse arguments and set up the test vector state @*tv_out@.
137 * If errors occur, print messages to standard error and exit
141 void tvec_parseargs(int argc
, char *argv
[], struct tvec_state
*tv_out
,
142 int *argpos_out
, const struct tvec_config
*config
)
145 const struct outform
*of
= 0;
146 struct tvec_output
*o
;
152 static const struct option options
[] = {
153 { "help", 0, 0, 'h' },
154 { "version", 0, 0, 'v' },
155 { "usage", 0, 0, 'u' },
157 { "format", OPTF_ARGREQ
, 0, 'f' },
158 { "output", OPTF_ARGREQ
, 0, 'o' },
164 opt
= mdwopt(argc
, argv
, "hvu" "f:o:", options
, 0, 0, 0);
167 case 'h': help(stdout
); exit(0);
168 case 'v': version(stdout
); exit(0);
169 case 'u': usage(stdout
); exit(0);
171 case 'f': of
= find_outform(optarg
); break;
173 if (ofp
) fclose(ofp
);
174 ofp
= fopen(optarg
, "w");
176 die(2, "failed to open `%s' for writing: %s",
177 optarg
, strerror(errno
));
185 if (f
&f_bogus
) { usage(stderr
); exit(2); }
186 if (!ofp
) ofp
= stdout
;
188 p
= getenv("TVEC_FORMAT");
189 if (p
) of
= find_outform(p
);
191 if (of
) o
= of
->makefn(ofp
);
192 else o
= tvec_dfltout(ofp
);
194 tvec_begin(tv_out
, config
, o
); *argpos_out
= optind
;
197 /* --- @tvec_readstdin@, @tvec_readfile@, @tvec_readarg@ --- *
199 * Arguments: @struct tvec_state *tv@ = test vector state
200 * @const char *file@ = pathname of file to read
201 * @const char *arg@ = argument to interpret
203 * Returns: Zero on success, @-1@ on error.
205 * Use: Read test vector data from stdin or a named file. The
206 * @tvec_readarg@ function reads from stdin if @arg@ is `%|-|%',
207 * and from the named file otherwise.
210 int tvec_readstdin(struct tvec_state
*tv
)
211 { return (tvec_read(tv
, "<stdin>", stdin
)); }
213 int tvec_readfile(struct tvec_state
*tv
, const char *file
)
218 fp
= fopen(file
, "r");
220 tvec_error(tv
, "failed to open `%s' for reading: %s",
221 file
, strerror(errno
));
224 if (tvec_read(tv
, file
, fp
)) { rc
= -1; goto end
; }
231 int tvec_readarg(struct tvec_state
*tv
, const char *arg
)
235 if (STRCMP(arg
, ==, "-")) rc
= tvec_readstdin(tv
);
236 else rc
= tvec_readfile(tv
, arg
);
240 /* --- @tvec_readdflt@ --- *
242 * Arguments: @struct tvec_state *tv@ = test vector state
243 * @const char *dflt@ = defsault filename or null
245 * Returns: Zero on success, @-1@ on error.
247 * Use: Reads from the default test vector data. If @file@ is null,
248 * then read from standard input, unless that's a terminal; if
249 * @file@ is not null, then read the named file, looking in the
250 * directory named by the `%|srcdir|%' environment variable if
251 * that's set, or otherwise in the current directory.
254 int tvec_readdflt(struct tvec_state
*tv
, const char *dflt
)
261 p
= getenv("srcdir");
262 if (p
) { dstr_putf(&d
, "%s/%s", p
, dflt
); dflt
= d
.buf
; }
263 rc
= tvec_readfile(tv
, dflt
);
264 } else if (isatty(0))
265 rc
= tvec_error(tv
, "use `-' to force reading from interactive stdin");
267 rc
= tvec_readstdin(tv
);
272 /* --- @tvec_readargs@ --- *
274 * Arguments: @int argc@ = number of command-line arguments
275 * @char *argv[]@ = vector of argument strings
276 * @struct tvec_state *tv@ = test vector state
277 * @int *argpos_inout@ = current argument position (updated)
278 * @const char *dflt@ = default filename or null
280 * Returns: Zero on success, @-1@ on error.
282 * Use: Reads from the sources indicated by the command-line
283 * arguments, in order, interpreting each as for @tvec_readarg@;
284 * if no arguments are given then read from @dflt@ as for
288 int tvec_readargs(int argc
, char *argv
[], struct tvec_state
*tv
,
289 int *argpos_inout
, const char *dflt
)
291 int i
= *argpos_inout
;
295 rc
= tvec_readdflt(tv
, dflt
);
299 if (tvec_readarg(tv
, argv
[i
++])) rc
= -1;
305 /* --- @tvec_main@ --- *
307 * Arguments: @int argc@ = number of command-line arguments
308 * @char *argv[]@ = vector of argument strings
309 * @const struct tvec_config *cofig@ = test vector configuration
310 * @const char *dflt@ = default filename or null
312 * Returns: Exit code.
314 * Use: All-in-one test vector front-end. Parse options from the
315 * command-line as for @tvec_parseargs@, and then process the
316 * remaining positional arguments as for @tvec_readargs@. The
317 * function constructs and disposes of a test vector state.
320 int tvec_main(int argc
, char *argv
[],
321 const struct tvec_config
*config
, const char *dflt
)
323 struct tvec_state tv
;
326 tvec_parseargs(argc
, argv
, &tv
, &argpos
, config
);
327 tvec_readargs(argc
, argv
, &tv
, &argpos
, dflt
);
328 return (tvec_end(&tv
));
331 /*----- That's all, folks -------------------------------------------------*/