testlang_parse.y revision 1.16 1 1.1 blymn %{
2 1.16 blymn /* $NetBSD: testlang_parse.y,v 1.16 2019/09/19 11:31:57 blymn Exp $ */
3 1.1 blymn
4 1.1 blymn /*-
5 1.1 blymn * Copyright 2009 Brett Lymn <blymn (at) NetBSD.org>
6 1.1 blymn *
7 1.1 blymn * All rights reserved.
8 1.1 blymn *
9 1.1 blymn * This code has been donated to The NetBSD Foundation by the Author.
10 1.1 blymn *
11 1.1 blymn * Redistribution and use in source and binary forms, with or without
12 1.1 blymn * modification, are permitted provided that the following conditions
13 1.1 blymn * are met:
14 1.1 blymn * 1. Redistributions of source code must retain the above copyright
15 1.1 blymn * notice, this list of conditions and the following disclaimer.
16 1.1 blymn * 2. The name of the author may not be used to endorse or promote products
17 1.1 blymn * derived from this software withough specific prior written permission
18 1.1 blymn *
19 1.1 blymn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 blymn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 blymn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 blymn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 blymn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 blymn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 blymn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 blymn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 blymn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 blymn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 blymn *
30 1.1 blymn *
31 1.1 blymn */
32 1.1 blymn #include <assert.h>
33 1.11 blymn #include <curses.h>
34 1.1 blymn #include <errno.h>
35 1.1 blymn #include <fcntl.h>
36 1.3 christos #include <err.h>
37 1.3 christos #include <unistd.h>
38 1.1 blymn #include <poll.h>
39 1.1 blymn #include <stdbool.h>
40 1.1 blymn #include <stdio.h>
41 1.1 blymn #include <string.h>
42 1.14 christos #include <stdlib.h>
43 1.14 christos #include <limits.h>
44 1.1 blymn #include <time.h>
45 1.6 christos #include <vis.h>
46 1.9 christos #include <stdint.h>
47 1.1 blymn #include "returns.h"
48 1.1 blymn
49 1.1 blymn #define YYDEBUG 1
50 1.1 blymn
51 1.1 blymn extern int verbose;
52 1.1 blymn extern int cmdpipe[2];
53 1.1 blymn extern int slvpipe[2];
54 1.1 blymn extern int master;
55 1.1 blymn extern struct pollfd readfd;
56 1.1 blymn extern char *check_path;
57 1.1 blymn extern char *cur_file; /* from director.c */
58 1.1 blymn
59 1.3 christos int yylex(void);
60 1.3 christos
61 1.1 blymn size_t line;
62 1.1 blymn
63 1.1 blymn static int input_delay;
64 1.1 blymn
65 1.1 blymn /* time delay between inputs chars - default to 0.1ms minimum to prevent
66 1.1 blymn * problems with input tests
67 1.1 blymn */
68 1.1 blymn #define DELAY_MIN 0.1
69 1.10 blymn
70 1.10 blymn /* time delay after a function call - allows the slave time to
71 1.10 blymn * run the function and output data before we do other actions.
72 1.10 blymn * Set this to 50ms.
73 1.10 blymn */
74 1.10 blymn #define POST_CALL_DELAY 50
75 1.10 blymn
76 1.1 blymn static struct timespec delay_spec = {0, 1000 * DELAY_MIN};
77 1.10 blymn static struct timespec delay_post_call = {0, 1000 * POST_CALL_DELAY};
78 1.10 blymn
79 1.1 blymn static char *input_str; /* string to feed in as input */
80 1.1 blymn static bool no_input; /* don't need more input */
81 1.1 blymn
82 1.16 blymn static wchar_t *vals = NULL; /* wchars to attach to a cchar type */
83 1.16 blymn static unsigned nvals; /* number of wchars */
84 1.16 blymn
85 1.1 blymn #define READ_PIPE 0
86 1.1 blymn #define WRITE_PIPE 1
87 1.1 blymn
88 1.16 blymn const char *enum_names[] = {
89 1.16 blymn "unused", "static", "numeric", "string", "byte", "cchar", "ERR", "OK", "NULL",
90 1.16 blymn "not NULL", "variable", "reference", "returns count", "slave error"
91 1.1 blymn };
92 1.1 blymn
93 1.1 blymn typedef struct {
94 1.16 blymn data_enum_t arg_type;
95 1.1 blymn size_t arg_len;
96 1.1 blymn char *arg_string;
97 1.1 blymn int var_index;
98 1.1 blymn } args_t;
99 1.1 blymn
100 1.1 blymn typedef struct {
101 1.1 blymn char *function;
102 1.1 blymn int nrets; /* number of returns */
103 1.16 blymn ct_data_t *returns; /* array of expected returns */
104 1.1 blymn int nargs; /* number of arguments */
105 1.1 blymn args_t *args; /* arguments for the call */
106 1.1 blymn } cmd_line_t;
107 1.1 blymn
108 1.1 blymn static cmd_line_t command;
109 1.1 blymn
110 1.1 blymn typedef struct {
111 1.1 blymn char *name;
112 1.1 blymn size_t len;
113 1.16 blymn data_enum_t type;
114 1.1 blymn void *value;
115 1.16 blymn cchar_t cchar;
116 1.1 blymn } var_t;
117 1.1 blymn
118 1.1 blymn static size_t nvars; /* Number of declared variables */
119 1.1 blymn static var_t *vars; /* Variables defined during the test. */
120 1.1 blymn
121 1.1 blymn static int check_function_table(char *, const char *[], int);
122 1.4 christos static int find_var_index(const char *);
123 1.16 blymn static void assign_arg(data_enum_t, void *);
124 1.1 blymn static int assign_var(char *);
125 1.1 blymn void init_parse_variables(int);
126 1.1 blymn static void validate(int, void *);
127 1.4 christos static void validate_return(const char *, const char *, int);
128 1.16 blymn static void validate_variable(int, data_enum_t, const void *, int, int);
129 1.16 blymn static void validate_byte(ct_data_t *, ct_data_t *, int);
130 1.1 blymn static void write_cmd_pipe(char *);
131 1.16 blymn static void write_cmd_pipe_args(data_enum_t, void *);
132 1.16 blymn static void read_cmd_pipe(ct_data_t *);
133 1.4 christos static void write_func_and_args(void);
134 1.1 blymn static void compare_streams(char *, bool);
135 1.4 christos static void do_function_call(size_t);
136 1.1 blymn static void save_slave_output(bool);
137 1.16 blymn static void validate_type(data_enum_t, ct_data_t *, int);
138 1.16 blymn static void set_var(data_enum_t, char *, void *);
139 1.1 blymn static void validate_reference(int, void *);
140 1.1 blymn static char *numeric_or(char *, char *);
141 1.4 christos static char *get_numeric_var(const char *);
142 1.10 blymn static void perform_delay(struct timespec *);
143 1.16 blymn static void set_cchar(char *, void *);
144 1.16 blymn static wchar_t *add_to_vals(data_enum_t, void *);
145 1.1 blymn
146 1.1 blymn static const char *input_functions[] = {
147 1.11 blymn "getch", "getnstr", "getstr", "mvgetnstr", "mvgetstr", "mvgetnstr",
148 1.13 blymn "mvgetstr", "mvscanw", "mvwscanw", "scanw", "wgetch", "wgetnstr",
149 1.13 blymn "wgetstr"
150 1.1 blymn };
151 1.1 blymn
152 1.1 blymn static const unsigned ninput_functions =
153 1.1 blymn sizeof(input_functions) / sizeof(char *);
154 1.1 blymn
155 1.1 blymn saved_data_t saved_output;
156 1.1 blymn
157 1.1 blymn %}
158 1.1 blymn
159 1.1 blymn %union {
160 1.1 blymn char *string;
161 1.16 blymn ct_data_t *retval;
162 1.16 blymn wchar_t *vals;
163 1.1 blymn }
164 1.1 blymn
165 1.1 blymn %token <string> PATH
166 1.1 blymn %token <string> STRING
167 1.1 blymn %token <retval> BYTE
168 1.1 blymn %token <string> VARNAME
169 1.1 blymn %token <string> FILENAME
170 1.1 blymn %token <string> VARIABLE
171 1.1 blymn %token <string> REFERENCE
172 1.1 blymn %token <string> NULL_RET
173 1.1 blymn %token <string> NON_NULL
174 1.1 blymn %token <string> ERR_RET
175 1.1 blymn %token <string> OK_RET
176 1.1 blymn %token <string> numeric
177 1.1 blymn %token <string> DELAY
178 1.1 blymn %token <string> INPUT
179 1.1 blymn %token <string> COMPARE
180 1.1 blymn %token <string> COMPAREND
181 1.1 blymn %token <string> ASSIGN
182 1.16 blymn %token <string> CCHAR
183 1.16 blymn %token EOL CALL CHECK NOINPUT OR MULTIPLIER LHB RHB LHSB RHSB
184 1.1 blymn %token CALL2 CALL3 CALL4 DRAIN
185 1.1 blymn
186 1.1 blymn %nonassoc OR
187 1.1 blymn
188 1.1 blymn %%
189 1.1 blymn
190 1.1 blymn statement : /* empty */
191 1.1 blymn | assign statement
192 1.1 blymn | call statement
193 1.1 blymn | call2 statement
194 1.1 blymn | call3 statement
195 1.1 blymn | call4 statement
196 1.1 blymn | check statement
197 1.1 blymn | delay statement
198 1.1 blymn | input statement
199 1.1 blymn | noinput statement
200 1.1 blymn | compare statement
201 1.1 blymn | comparend statement
202 1.16 blymn | cchar statement
203 1.1 blymn | eol statement
204 1.1 blymn ;
205 1.1 blymn
206 1.16 blymn assign : ASSIGN VARNAME numeric {set_var(data_number, $2, $3);} eol
207 1.16 blymn | ASSIGN VARNAME LHB expr RHB {set_var(data_number, $2, $<string>4);} eol
208 1.16 blymn | ASSIGN VARNAME STRING {set_var(data_string, $2, $3);} eol
209 1.16 blymn | ASSIGN VARNAME BYTE {set_var(data_byte, $2, $3);} eol
210 1.16 blymn ;
211 1.16 blymn
212 1.16 blymn cchar : CCHAR VARNAME attributes char_vals eol
213 1.16 blymn {
214 1.16 blymn set_cchar($2, $<string>3);
215 1.16 blymn }
216 1.16 blymn ;
217 1.16 blymn
218 1.16 blymn
219 1.16 blymn attributes : numeric
220 1.16 blymn | LHB expr RHB
221 1.16 blymn { $<string>$ = $<string>2; }
222 1.16 blymn | VARIABLE
223 1.16 blymn { $<string>$ = get_numeric_var($1); }
224 1.16 blymn ;
225 1.16 blymn
226 1.16 blymn char_vals : numeric
227 1.16 blymn { add_to_vals(data_number, $1); }
228 1.16 blymn | LHSB array RHSB
229 1.16 blymn | VARIABLE
230 1.16 blymn { add_to_vals(data_var, $1); }
231 1.16 blymn | STRING
232 1.16 blymn { add_to_vals(data_string, $1); }
233 1.16 blymn | BYTE
234 1.16 blymn { add_to_vals(data_byte, $1); }
235 1.1 blymn ;
236 1.1 blymn
237 1.1 blymn call : CALL result fn_name args eol {
238 1.1 blymn do_function_call(1);
239 1.1 blymn }
240 1.1 blymn ;
241 1.1 blymn
242 1.1 blymn call2 : CALL2 result result fn_name args eol {
243 1.1 blymn do_function_call(2);
244 1.1 blymn }
245 1.1 blymn ;
246 1.1 blymn
247 1.1 blymn call3 : CALL3 result result result fn_name args eol {
248 1.1 blymn do_function_call(3);
249 1.1 blymn }
250 1.1 blymn ;
251 1.1 blymn
252 1.1 blymn call4 : CALL4 result result result result fn_name args eol {
253 1.1 blymn do_function_call(4);
254 1.1 blymn }
255 1.1 blymn ;
256 1.1 blymn
257 1.1 blymn check : CHECK var returns eol {
258 1.16 blymn ct_data_t retvar;
259 1.1 blymn var_t *vptr;
260 1.16 blymn if (command.returns[0].data_index == -1)
261 1.4 christos err(1, "Undefined variable in check statement, line %zu"
262 1.1 blymn " of file %s", line, cur_file);
263 1.1 blymn
264 1.9 christos if (verbose) {
265 1.1 blymn fprintf(stderr, "Checking contents of variable %s for %s\n",
266 1.16 blymn vars[command.returns[0].data_index].name,
267 1.16 blymn enum_names[command.returns[1].data_type]);
268 1.9 christos }
269 1.1 blymn
270 1.16 blymn if (((command.returns[1].data_type == data_byte) &&
271 1.16 blymn (vars[command.returns[0].data_index].type != data_byte)) ||
272 1.16 blymn vars[command.returns[0].data_index].type != data_string)
273 1.1 blymn err(1, "Var type %s (%d) does not match return type %s (%d)",
274 1.16 blymn enum_names[
275 1.16 blymn vars[command.returns[0].data_index].type],
276 1.16 blymn vars[command.returns[0].data_index].type,
277 1.16 blymn enum_names[command.returns[1].data_type],
278 1.16 blymn command.returns[1].data_type);
279 1.1 blymn
280 1.16 blymn switch (command.returns[1].data_type) {
281 1.16 blymn case data_err:
282 1.16 blymn validate_variable(0, data_string, "ERR",
283 1.16 blymn command.returns[0].data_index, 0);
284 1.1 blymn break;
285 1.1 blymn
286 1.16 blymn case data_ok:
287 1.16 blymn validate_variable(0, data_string, "OK",
288 1.16 blymn command.returns[0].data_index, 0);
289 1.1 blymn break;
290 1.1 blymn
291 1.16 blymn case data_null:
292 1.16 blymn validate_variable(0, data_string, "NULL",
293 1.16 blymn command.returns[0].data_index, 0);
294 1.1 blymn break;
295 1.1 blymn
296 1.16 blymn case data_nonnull:
297 1.16 blymn validate_variable(0, data_string, "NULL",
298 1.16 blymn command.returns[0].data_index, 1);
299 1.1 blymn break;
300 1.1 blymn
301 1.16 blymn case data_string:
302 1.16 blymn case data_number:
303 1.9 christos if (verbose) {
304 1.1 blymn fprintf(stderr, " %s == returned %s\n",
305 1.16 blymn (const char *)command.returns[1].data_value,
306 1.5 christos (const char *)
307 1.16 blymn vars[command.returns[0].data_index].value);
308 1.9 christos }
309 1.16 blymn validate_variable(0, data_string,
310 1.16 blymn command.returns[1].data_value,
311 1.16 blymn command.returns[0].data_index, 0);
312 1.1 blymn break;
313 1.1 blymn
314 1.16 blymn case data_byte:
315 1.16 blymn vptr = &vars[command.returns[0].data_index];
316 1.16 blymn retvar.data_len = vptr->len;
317 1.16 blymn retvar.data_type = vptr->type;
318 1.16 blymn retvar.data_value = vptr->value;
319 1.4 christos validate_byte(&retvar, &command.returns[1], 0);
320 1.1 blymn break;
321 1.1 blymn
322 1.1 blymn default:
323 1.4 christos err(1, "Malformed check statement at line %zu "
324 1.1 blymn "of file %s", line, cur_file);
325 1.1 blymn break;
326 1.1 blymn }
327 1.1 blymn
328 1.1 blymn init_parse_variables(0);
329 1.1 blymn }
330 1.1 blymn ;
331 1.1 blymn
332 1.1 blymn delay : DELAY numeric eol {
333 1.1 blymn /* set the inter-character delay */
334 1.1 blymn if (sscanf($2, "%d", &input_delay) == 0)
335 1.1 blymn err(1, "delay specification %s could not be converted to "
336 1.4 christos "numeric at line %zu of file %s", $2, line, cur_file);
337 1.9 christos if (verbose) {
338 1.1 blymn fprintf(stderr, "Set input delay to %d ms\n", input_delay);
339 1.9 christos }
340 1.1 blymn
341 1.1 blymn if (input_delay < DELAY_MIN)
342 1.10 blymn input_delay = DELAY_MIN;
343 1.1 blymn /*
344 1.1 blymn * Fill in the timespec structure now ready for use later.
345 1.1 blymn * The delay is specified in milliseconds so convert to timespec
346 1.1 blymn * values
347 1.1 blymn */
348 1.1 blymn delay_spec.tv_sec = input_delay / 1000;
349 1.1 blymn delay_spec.tv_nsec = (input_delay - 1000 * delay_spec.tv_sec) * 1000;
350 1.9 christos if (verbose) {
351 1.9 christos fprintf(stderr, "set delay to %jd.%jd\n",
352 1.10 blymn (intmax_t)delay_spec.tv_sec,
353 1.9 christos (intmax_t)delay_spec.tv_nsec);
354 1.9 christos }
355 1.1 blymn
356 1.1 blymn init_parse_variables(0);
357 1.1 blymn }
358 1.1 blymn ;
359 1.1 blymn
360 1.1 blymn input : INPUT STRING eol {
361 1.1 blymn if (input_str != NULL) {
362 1.5 christos warnx("%s, %zu: Discarding unused input string",
363 1.5 christos cur_file, line);
364 1.1 blymn free(input_str);
365 1.1 blymn }
366 1.1 blymn
367 1.1 blymn if ((input_str = malloc(strlen($2) + 1)) == NULL)
368 1.1 blymn err(2, "Cannot allocate memory for input string");
369 1.1 blymn
370 1.1 blymn strlcpy(input_str, $2, strlen($2) + 1);
371 1.1 blymn }
372 1.1 blymn ;
373 1.1 blymn
374 1.1 blymn
375 1.1 blymn noinput : NOINPUT eol {
376 1.1 blymn if (input_str != NULL) {
377 1.5 christos warnx("%s, %zu: Discarding unused input string",
378 1.5 christos cur_file, line);
379 1.1 blymn free(input_str);
380 1.1 blymn }
381 1.1 blymn
382 1.1 blymn no_input = true;
383 1.1 blymn }
384 1.1 blymn
385 1.1 blymn compare : COMPARE PATH eol
386 1.1 blymn | COMPARE FILENAME eol
387 1.1 blymn {
388 1.1 blymn compare_streams($2, true);
389 1.1 blymn }
390 1.1 blymn ;
391 1.1 blymn
392 1.1 blymn
393 1.1 blymn comparend : COMPAREND PATH eol
394 1.1 blymn | COMPAREND FILENAME eol
395 1.1 blymn {
396 1.1 blymn compare_streams($2, false);
397 1.1 blymn }
398 1.1 blymn ;
399 1.1 blymn
400 1.1 blymn
401 1.1 blymn result : returns
402 1.1 blymn | var
403 1.1 blymn | reference
404 1.1 blymn ;
405 1.1 blymn
406 1.16 blymn returns : numeric { assign_rets(data_number, $1); }
407 1.16 blymn | LHB expr RHB { assign_rets(data_number, $<string>2); }
408 1.16 blymn | STRING { assign_rets(data_string, $1); }
409 1.16 blymn | BYTE { assign_rets(data_byte, (void *) $1); }
410 1.16 blymn | ERR_RET { assign_rets(data_err, NULL); }
411 1.16 blymn | OK_RET { assign_rets(data_ok, NULL); }
412 1.16 blymn | NULL_RET { assign_rets(data_null, NULL); }
413 1.16 blymn | NON_NULL { assign_rets(data_nonnull, NULL); }
414 1.1 blymn ;
415 1.1 blymn
416 1.1 blymn var : VARNAME {
417 1.16 blymn assign_rets(data_var, $1);
418 1.1 blymn }
419 1.1 blymn ;
420 1.1 blymn
421 1.1 blymn reference : VARIABLE {
422 1.16 blymn assign_rets(data_ref, $1);
423 1.1 blymn }
424 1.1 blymn
425 1.1 blymn fn_name : VARNAME {
426 1.1 blymn if (command.function != NULL)
427 1.1 blymn free(command.function);
428 1.1 blymn
429 1.1 blymn command.function = malloc(strlen($1) + 1);
430 1.1 blymn if (command.function == NULL)
431 1.1 blymn err(1, "Could not allocate memory for function name");
432 1.1 blymn strcpy(command.function, $1);
433 1.1 blymn }
434 1.1 blymn ;
435 1.1 blymn
436 1.16 blymn array : numeric { $<vals>$ = add_to_vals(data_number, $1); };
437 1.16 blymn | VARIABLE
438 1.16 blymn { $<vals>$ = add_to_vals(data_number,
439 1.16 blymn get_numeric_var($1)); }
440 1.16 blymn | BYTE
441 1.16 blymn {
442 1.16 blymn $<vals>$ = add_to_vals(data_byte, (void *) $1);
443 1.16 blymn }
444 1.16 blymn | numeric MULTIPLIER numeric
445 1.16 blymn {
446 1.16 blymn unsigned long i;
447 1.16 blymn unsigned long acount;
448 1.16 blymn
449 1.16 blymn acount = strtoul($3, NULL, 10);
450 1.16 blymn for (i = 0; i < acount; i++) {
451 1.16 blymn $<vals>$ = add_to_vals(data_number, $1);
452 1.16 blymn }
453 1.16 blymn }
454 1.16 blymn | VARIABLE MULTIPLIER numeric
455 1.16 blymn {
456 1.16 blymn unsigned long i, acount;
457 1.16 blymn char *val;
458 1.16 blymn
459 1.16 blymn acount = strtoul($3, NULL, 10);
460 1.16 blymn val = get_numeric_var($1);
461 1.16 blymn for (i = 0; i < acount; i++) {
462 1.16 blymn $<vals>$ = add_to_vals(data_number, val);
463 1.16 blymn }
464 1.16 blymn }
465 1.16 blymn | BYTE MULTIPLIER numeric
466 1.16 blymn {
467 1.16 blymn unsigned long i, acount;
468 1.16 blymn
469 1.16 blymn acount = strtoul($3, NULL, 10);
470 1.16 blymn for (i = 0; i < acount; i++) {
471 1.16 blymn $<vals>$ = add_to_vals(data_byte, (void *) $1);
472 1.16 blymn }
473 1.16 blymn }
474 1.16 blymn | STRING MULTIPLIER numeric
475 1.16 blymn {
476 1.16 blymn unsigned long i, acount;
477 1.16 blymn
478 1.16 blymn acount = strtoul($3, NULL, 10);
479 1.16 blymn for (i = 0; i < acount; i++) {
480 1.16 blymn $<vals>$ = add_to_vals(data_string,
481 1.16 blymn (void *) $1);
482 1.16 blymn }
483 1.16 blymn }
484 1.16 blymn | array array
485 1.16 blymn ;
486 1.16 blymn
487 1.1 blymn expr : numeric
488 1.1 blymn | VARIABLE
489 1.1 blymn { $<string>$ = get_numeric_var($1); }
490 1.1 blymn | expr OR expr
491 1.1 blymn { $<string>$ = numeric_or($<string>1, $<string>3); }
492 1.1 blymn ;
493 1.1 blymn
494 1.1 blymn args : /* empty */
495 1.16 blymn | LHB expr RHB { assign_arg(data_static, $<string>2); } args
496 1.16 blymn | numeric { assign_arg(data_static, $1); } args
497 1.16 blymn | STRING { assign_arg(data_static, $1); } args
498 1.16 blymn | BYTE { assign_arg(data_byte, $1); } args
499 1.16 blymn | PATH { assign_arg(data_static, $1); } args
500 1.16 blymn | FILENAME { assign_arg(data_static, $1); } args
501 1.16 blymn | VARNAME { assign_arg(data_static, $1); } args
502 1.16 blymn | VARIABLE { assign_arg(data_var, $1); } args
503 1.16 blymn | NULL_RET { assign_arg(data_null, $1); } args
504 1.1 blymn ;
505 1.1 blymn
506 1.1 blymn eol : EOL
507 1.1 blymn ;
508 1.1 blymn
509 1.1 blymn %%
510 1.1 blymn
511 1.6 christos static void
512 1.6 christos excess(const char *fname, size_t lineno, const char *func, const char *comment,
513 1.6 christos const void *data, size_t datalen)
514 1.6 christos {
515 1.6 christos size_t dstlen = datalen * 4 + 1;
516 1.6 christos char *dst = malloc(dstlen);
517 1.6 christos
518 1.6 christos if (dst == NULL)
519 1.6 christos err(1, "malloc");
520 1.6 christos
521 1.6 christos if (strnvisx(dst, dstlen, data, datalen, VIS_WHITE | VIS_OCTAL) == -1)
522 1.6 christos err(1, "strnvisx");
523 1.6 christos
524 1.6 christos warnx("%s, %zu: [%s] Excess %zu bytes%s [%s]",
525 1.6 christos fname, lineno, func, datalen, comment, dst);
526 1.6 christos free(dst);
527 1.6 christos }
528 1.6 christos
529 1.1 blymn /*
530 1.1 blymn * Get the value of a variable, error if the variable has not been set or
531 1.1 blymn * is not a numeric type.
532 1.1 blymn */
533 1.1 blymn static char *
534 1.4 christos get_numeric_var(const char *var)
535 1.1 blymn {
536 1.4 christos int i;
537 1.1 blymn
538 1.4 christos if ((i = find_var_index(var)) < 0)
539 1.1 blymn err(1, "Variable %s is undefined", var);
540 1.1 blymn
541 1.16 blymn if (vars[i].type != data_number)
542 1.4 christos err(1, "Variable %s is not a numeric type", var);
543 1.1 blymn
544 1.4 christos return vars[i].value;
545 1.1 blymn }
546 1.1 blymn
547 1.1 blymn /*
548 1.1 blymn * Perform a bitwise OR on two numbers and return the result.
549 1.1 blymn */
550 1.1 blymn static char *
551 1.1 blymn numeric_or(char *n1, char *n2)
552 1.1 blymn {
553 1.1 blymn unsigned long i1, i2, result;
554 1.1 blymn char *ret;
555 1.1 blymn
556 1.1 blymn i1 = strtoul(n1, NULL, 10);
557 1.1 blymn i2 = strtoul(n2, NULL, 10);
558 1.1 blymn
559 1.1 blymn result = i1 | i2;
560 1.1 blymn asprintf(&ret, "%lu", result);
561 1.1 blymn
562 1.9 christos if (verbose) {
563 1.5 christos fprintf(stderr, "numeric or of 0x%lx (%s) and 0x%lx (%s)"
564 1.5 christos " results in 0x%lx (%s)\n",
565 1.5 christos i1, n1, i2, n2, result, ret);
566 1.9 christos }
567 1.1 blymn
568 1.1 blymn return ret;
569 1.1 blymn }
570 1.1 blymn
571 1.1 blymn /*
572 1.10 blymn * Sleep for the specified time, handle the sleep getting interrupted
573 1.10 blymn * by a signal.
574 1.10 blymn */
575 1.10 blymn static void
576 1.10 blymn perform_delay(struct timespec *ts)
577 1.10 blymn {
578 1.10 blymn struct timespec delay_copy, delay_remainder;
579 1.10 blymn
580 1.10 blymn delay_copy = *ts;
581 1.10 blymn while (nanosleep(&delay_copy, &delay_remainder) < 0) {
582 1.10 blymn if (errno != EINTR)
583 1.10 blymn err(2, "nanosleep returned error");
584 1.10 blymn delay_copy = delay_remainder;
585 1.10 blymn }
586 1.10 blymn }
587 1.10 blymn
588 1.10 blymn /*
589 1.16 blymn * Add to temporary vals array
590 1.16 blymn */
591 1.16 blymn static wchar_t *add_to_vals(data_enum_t argtype, void *arg)
592 1.16 blymn {
593 1.16 blymn wchar_t *retval = NULL;
594 1.16 blymn int have_malloced;
595 1.16 blymn int i;
596 1.16 blymn ct_data_t *ret;
597 1.16 blymn
598 1.16 blymn have_malloced = 0;
599 1.16 blymn
600 1.16 blymn if (nvals == 0) {
601 1.16 blymn have_malloced = 1;
602 1.16 blymn retval = malloc(sizeof(wchar_t));
603 1.16 blymn } else {
604 1.16 blymn retval = realloc(vals, (nvals + 1) * sizeof(wchar_t));
605 1.16 blymn }
606 1.16 blymn
607 1.16 blymn if (retval == NULL)
608 1.16 blymn return retval;
609 1.16 blymn
610 1.16 blymn vals = retval;
611 1.16 blymn
612 1.16 blymn switch (argtype) {
613 1.16 blymn case data_number:
614 1.16 blymn vals[nvals++] = (wchar_t) strtoul((char *) arg, NULL, 10);
615 1.16 blymn break;
616 1.16 blymn
617 1.16 blymn case data_string:
618 1.16 blymn vals[nvals++] = (wchar_t) ((char *)arg)[0];
619 1.16 blymn break;
620 1.16 blymn
621 1.16 blymn case data_byte:
622 1.16 blymn ret = (ct_data_t *) arg;
623 1.16 blymn vals[nvals++] = *((wchar_t *) ret->data_value);
624 1.16 blymn break;
625 1.16 blymn
626 1.16 blymn case data_var:
627 1.16 blymn if ((i = find_var_index((char *) arg)) < 0)
628 1.16 blymn err(1, "Variable %s is undefined at line %zu "
629 1.16 blymn "of file %s", (char *) arg, line, cur_file);
630 1.16 blymn
631 1.16 blymn switch (vars[i].type) {
632 1.16 blymn
633 1.16 blymn case data_number:
634 1.16 blymn case data_string:
635 1.16 blymn case data_byte:
636 1.16 blymn retval = add_to_vals(vars[i].type, vars[i].value);
637 1.16 blymn break;
638 1.16 blymn
639 1.16 blymn default:
640 1.16 blymn err(1, "Variable %s is not a valid type for cchar"
641 1.16 blymn " at line %zu of file %s", (char *) arg, line,
642 1.16 blymn cur_file);
643 1.16 blymn break;
644 1.16 blymn
645 1.16 blymn }
646 1.16 blymn break;
647 1.16 blymn
648 1.16 blymn default:
649 1.16 blymn err(1, "add_to_vals: Unhandled type for vals array "
650 1.16 blymn "at line %zu of file %s", line, cur_file);
651 1.16 blymn
652 1.16 blymn /* if we get here without a value then tidy up */
653 1.16 blymn if ((nvals == 0) && (have_malloced == 1)) {
654 1.16 blymn free(retval);
655 1.16 blymn retval = vals;
656 1.16 blymn }
657 1.16 blymn break;
658 1.16 blymn
659 1.16 blymn }
660 1.16 blymn
661 1.16 blymn return retval;
662 1.16 blymn }
663 1.16 blymn
664 1.16 blymn /*
665 1.1 blymn * Assign the value given to the named variable.
666 1.1 blymn */
667 1.1 blymn static void
668 1.16 blymn set_var(data_enum_t type, char *name, void *value)
669 1.1 blymn {
670 1.4 christos int i;
671 1.1 blymn char *number;
672 1.16 blymn ct_data_t *ret;
673 1.1 blymn
674 1.4 christos i = find_var_index(name);
675 1.4 christos if (i < 0)
676 1.4 christos i = assign_var(name);
677 1.1 blymn
678 1.4 christos vars[i].type = type;
679 1.16 blymn if ((type == data_number) || (type == data_string)) {
680 1.1 blymn number = value;
681 1.4 christos vars[i].len = strlen(number) + 1;
682 1.4 christos vars[i].value = malloc(vars[i].len + 1);
683 1.4 christos if (vars[i].value == NULL)
684 1.1 blymn err(1, "Could not malloc memory for assign string");
685 1.4 christos strcpy(vars[i].value, number);
686 1.1 blymn } else {
687 1.1 blymn /* can only be a byte value */
688 1.1 blymn ret = value;
689 1.16 blymn vars[i].len = ret->data_len;
690 1.4 christos vars[i].value = malloc(vars[i].len);
691 1.4 christos if (vars[i].value == NULL)
692 1.1 blymn err(1, "Could not malloc memory to assign byte string");
693 1.16 blymn memcpy(vars[i].value, ret->data_value, vars[i].len);
694 1.1 blymn }
695 1.1 blymn }
696 1.1 blymn
697 1.1 blymn /*
698 1.16 blymn * Form up a complex character type from the given components.
699 1.16 blymn */
700 1.16 blymn static void
701 1.16 blymn set_cchar(char *name, void *attributes)
702 1.16 blymn {
703 1.16 blymn int i;
704 1.16 blymn unsigned j;
705 1.16 blymn attr_t attribs;
706 1.16 blymn
707 1.16 blymn if (nvals >= CURSES_CCHAR_MAX)
708 1.16 blymn err(1, "%s: too many characters in complex char type at "
709 1.16 blymn "line %zu of file %s", __func__, line, cur_file);
710 1.16 blymn
711 1.16 blymn i = find_var_index(name);
712 1.16 blymn if (i < 0)
713 1.16 blymn i = assign_var(name);
714 1.16 blymn
715 1.16 blymn if (sscanf((char *) attributes, "%d", &attribs) != 1)
716 1.16 blymn err(1, "%s: conversion of attributes to integer failed at"
717 1.16 blymn "line %zu of file %s", __func__, line, cur_file);
718 1.16 blymn
719 1.16 blymn vars[i].type = data_cchar;
720 1.16 blymn vars[i].cchar.attributes = attribs;
721 1.16 blymn vars[i].cchar.elements = nvals;
722 1.16 blymn for (j = 0; j < nvals; j++)
723 1.16 blymn vars[i].cchar.vals[j] = vals[j];
724 1.16 blymn
725 1.16 blymn nvals = 0;
726 1.16 blymn vals = NULL;
727 1.16 blymn
728 1.16 blymn }
729 1.16 blymn
730 1.16 blymn /*
731 1.1 blymn * Add a new variable to the vars array, the value will be assigned later,
732 1.1 blymn * when a test function call returns.
733 1.1 blymn */
734 1.1 blymn static int
735 1.1 blymn assign_var(char *varname)
736 1.1 blymn {
737 1.1 blymn var_t *temp;
738 1.1 blymn char *name;
739 1.1 blymn
740 1.1 blymn if ((name = malloc(strlen(varname) + 1)) == NULL)
741 1.1 blymn err(1, "Alloc of varname failed");
742 1.1 blymn
743 1.9 christos if ((temp = realloc(vars, sizeof(*temp) * (nvars + 1))) == NULL) {
744 1.1 blymn free(name);
745 1.1 blymn err(1, "Realloc of vars array failed");
746 1.1 blymn }
747 1.1 blymn
748 1.1 blymn strcpy(name, varname);
749 1.1 blymn vars = temp;
750 1.1 blymn vars[nvars].name = name;
751 1.1 blymn vars[nvars].len = 0;
752 1.1 blymn vars[nvars].value = NULL;
753 1.1 blymn nvars++;
754 1.1 blymn
755 1.1 blymn return (nvars - 1);
756 1.1 blymn }
757 1.1 blymn
758 1.1 blymn /*
759 1.1 blymn * Allocate and assign a new argument of the given type.
760 1.1 blymn */
761 1.1 blymn static void
762 1.16 blymn assign_arg(data_enum_t arg_type, void *arg)
763 1.1 blymn {
764 1.1 blymn args_t *temp, cur;
765 1.4 christos char *str = arg;
766 1.16 blymn ct_data_t *ret;
767 1.1 blymn
768 1.9 christos if (verbose) {
769 1.16 blymn fprintf(stderr, "function is >%s<, adding arg >%s< type %s (%d)\n",
770 1.16 blymn command.function, str, enum_names[arg_type], arg_type);
771 1.9 christos }
772 1.1 blymn
773 1.1 blymn cur.arg_type = arg_type;
774 1.16 blymn if (cur.arg_type == data_var) {
775 1.1 blymn cur.var_index = find_var_index(arg);
776 1.1 blymn if (cur.var_index < 0)
777 1.4 christos err(1, "Invalid variable %s at line %zu of file %s",
778 1.4 christos str, line, cur_file);
779 1.16 blymn } else if (cur.arg_type == data_byte) {
780 1.2 blymn ret = arg;
781 1.16 blymn cur.arg_len = ret->data_len;
782 1.2 blymn cur.arg_string = malloc(cur.arg_len);
783 1.2 blymn if (cur.arg_string == NULL)
784 1.2 blymn err(1, "Could not malloc memory for arg bytes");
785 1.16 blymn memcpy(cur.arg_string, ret->data_value, cur.arg_len);
786 1.16 blymn } else if (cur.arg_type == data_null) {
787 1.2 blymn cur.arg_len = 0;
788 1.2 blymn cur.arg_string = NULL;
789 1.16 blymn } else {
790 1.2 blymn cur.arg_len = strlen(str);
791 1.2 blymn cur.arg_string = malloc(cur.arg_len + 1);
792 1.2 blymn if (cur.arg_string == NULL)
793 1.2 blymn err(1, "Could not malloc memory for arg string");
794 1.2 blymn strcpy(cur.arg_string, arg);
795 1.1 blymn }
796 1.1 blymn
797 1.9 christos temp = realloc(command.args, sizeof(*temp) * (command.nargs + 1));
798 1.1 blymn if (temp == NULL)
799 1.1 blymn err(1, "Failed to reallocate args");
800 1.1 blymn command.args = temp;
801 1.1 blymn memcpy(&command.args[command.nargs], &cur, sizeof(args_t));
802 1.1 blymn command.nargs++;
803 1.1 blymn }
804 1.1 blymn
805 1.1 blymn /*
806 1.1 blymn * Allocate and assign a new return.
807 1.1 blymn */
808 1.1 blymn static void
809 1.16 blymn assign_rets(data_enum_t ret_type, void *ret)
810 1.1 blymn {
811 1.16 blymn ct_data_t *temp, cur;
812 1.1 blymn char *ret_str;
813 1.16 blymn ct_data_t *ret_ret;
814 1.1 blymn
815 1.16 blymn cur.data_type = ret_type;
816 1.16 blymn if (ret_type != data_var) {
817 1.16 blymn if ((ret_type == data_number) || (ret_type == data_string)) {
818 1.1 blymn ret_str = ret;
819 1.16 blymn cur.data_len = strlen(ret_str) + 1;
820 1.16 blymn cur.data_value = malloc(cur.data_len + 1);
821 1.16 blymn if (cur.data_value == NULL)
822 1.1 blymn err(1,
823 1.1 blymn "Could not malloc memory for arg string");
824 1.16 blymn strcpy(cur.data_value, ret_str);
825 1.16 blymn } else if (ret_type == data_byte) {
826 1.1 blymn ret_ret = ret;
827 1.16 blymn cur.data_len = ret_ret->data_len;
828 1.16 blymn cur.data_value = malloc(cur.data_len);
829 1.16 blymn if (cur.data_value == NULL)
830 1.1 blymn err(1,
831 1.1 blymn "Could not malloc memory for byte string");
832 1.16 blymn memcpy(cur.data_value, ret_ret->data_value,
833 1.16 blymn cur.data_len);
834 1.16 blymn } else if (ret_type == data_ref) {
835 1.16 blymn if ((cur.data_index = find_var_index(ret)) < 0)
836 1.1 blymn err(1, "Undefined variable reference");
837 1.1 blymn }
838 1.1 blymn } else {
839 1.16 blymn cur.data_index = find_var_index(ret);
840 1.16 blymn if (cur.data_index < 0)
841 1.16 blymn cur.data_index = assign_var(ret);
842 1.1 blymn }
843 1.1 blymn
844 1.9 christos temp = realloc(command.returns, sizeof(*temp) * (command.nrets + 1));
845 1.1 blymn if (temp == NULL)
846 1.1 blymn err(1, "Failed to reallocate returns");
847 1.1 blymn command.returns = temp;
848 1.16 blymn memcpy(&command.returns[command.nrets], &cur, sizeof(ct_data_t));
849 1.1 blymn command.nrets++;
850 1.1 blymn }
851 1.1 blymn
852 1.1 blymn /*
853 1.4 christos * Find the given variable name in the var array and return the i
854 1.1 blymn * return -1 if var is not found.
855 1.1 blymn */
856 1.1 blymn static int
857 1.4 christos find_var_index(const char *var_name)
858 1.1 blymn {
859 1.4 christos int result;
860 1.4 christos size_t i;
861 1.1 blymn
862 1.1 blymn result = -1;
863 1.1 blymn
864 1.1 blymn for (i = 0; i < nvars; i++) {
865 1.1 blymn if (strcmp(var_name, vars[i].name) == 0) {
866 1.1 blymn result = i;
867 1.1 blymn break;
868 1.1 blymn }
869 1.1 blymn }
870 1.1 blymn
871 1.1 blymn return result;
872 1.1 blymn }
873 1.1 blymn
874 1.1 blymn /*
875 1.1 blymn * Check the given function name in the given table of names, return 1 if
876 1.1 blymn * there is a match.
877 1.1 blymn */
878 1.1 blymn static int check_function_table(char *function, const char *table[],
879 1.1 blymn int nfunctions)
880 1.1 blymn {
881 1.1 blymn int i;
882 1.1 blymn
883 1.1 blymn for (i = 0; i < nfunctions; i++) {
884 1.1 blymn if ((strlen(function) == strlen(table[i])) &&
885 1.1 blymn (strcmp(function, table[i]) == 0))
886 1.1 blymn return 1;
887 1.1 blymn }
888 1.1 blymn
889 1.1 blymn return 0;
890 1.1 blymn }
891 1.1 blymn
892 1.1 blymn /*
893 1.1 blymn * Compare the output from the slave against the given file and report
894 1.1 blymn * any differences.
895 1.1 blymn */
896 1.1 blymn static void
897 1.1 blymn compare_streams(char *filename, bool discard)
898 1.1 blymn {
899 1.5 christos char check_file[PATH_MAX], drain[100], ref, data;
900 1.1 blymn struct pollfd fds[2];
901 1.5 christos int nfd, check_fd;
902 1.5 christos ssize_t result;
903 1.9 christos size_t offs;
904 1.1 blymn
905 1.1 blymn /*
906 1.1 blymn * Don't prepend check path iff check file has an absolute
907 1.1 blymn * path.
908 1.1 blymn */
909 1.1 blymn if (filename[0] != '/') {
910 1.1 blymn if (strlcpy(check_file, check_path, sizeof(check_file))
911 1.1 blymn >= sizeof(check_file))
912 1.1 blymn err(2, "CHECK_PATH too long");
913 1.1 blymn
914 1.1 blymn if (strlcat(check_file, "/", sizeof(check_file))
915 1.1 blymn >= sizeof(check_file))
916 1.1 blymn err(2, "Could not append / to check file path");
917 1.1 blymn } else {
918 1.1 blymn check_file[0] = '\0';
919 1.1 blymn }
920 1.1 blymn
921 1.1 blymn if (strlcat(check_file, filename, sizeof(check_file))
922 1.1 blymn >= sizeof(check_file))
923 1.1 blymn err(2, "Path to check file path overflowed");
924 1.1 blymn
925 1.1 blymn if ((check_fd = open(check_file, O_RDONLY, 0)) < 0)
926 1.4 christos err(2, "failed to open file %s line %zu of file %s",
927 1.1 blymn check_file, line, cur_file);
928 1.1 blymn
929 1.1 blymn fds[0].fd = check_fd;
930 1.1 blymn fds[0].events = POLLIN;
931 1.1 blymn fds[1].fd = master;
932 1.1 blymn fds[1].events = POLLIN;
933 1.1 blymn
934 1.1 blymn nfd = 2;
935 1.1 blymn /*
936 1.1 blymn * if we have saved output then only check for data in the
937 1.1 blymn * reference file since the slave data may already be drained.
938 1.1 blymn */
939 1.1 blymn if (saved_output.count > 0)
940 1.1 blymn nfd = 1;
941 1.1 blymn
942 1.9 christos offs = 0;
943 1.9 christos while (poll(fds, nfd, 500) == nfd) {
944 1.9 christos if (fds[0].revents & POLLIN) {
945 1.9 christos if ((result = read(check_fd, &ref, 1)) < 1) {
946 1.9 christos if (result != 0) {
947 1.9 christos err(2,
948 1.9 christos "Bad read on file %s", check_file);
949 1.9 christos } else {
950 1.9 christos break;
951 1.9 christos }
952 1.1 blymn }
953 1.1 blymn }
954 1.1 blymn
955 1.1 blymn if (saved_output.count > 0) {
956 1.1 blymn data = saved_output.data[saved_output.readp];
957 1.1 blymn saved_output.count--;
958 1.1 blymn saved_output.readp++;
959 1.1 blymn /* run out of saved data, switch to file */
960 1.1 blymn if (saved_output.count == 0)
961 1.1 blymn nfd = 2;
962 1.1 blymn } else {
963 1.9 christos if (fds[0].revents & POLLIN) {
964 1.9 christos if (read(master, &data, 1) < 1)
965 1.9 christos err(2, "Bad read on slave pty");
966 1.9 christos } else
967 1.9 christos continue;
968 1.1 blymn }
969 1.1 blymn
970 1.1 blymn if (verbose) {
971 1.1 blymn fprintf(stderr, "Comparing reference byte 0x%x (%c)"
972 1.9 christos " against slave byte 0x%x (%c)\n",
973 1.9 christos ref, (ref >= ' ') ? ref : '-',
974 1.9 christos data, (data >= ' ' )? data : '-');
975 1.1 blymn }
976 1.1 blymn
977 1.1 blymn if (ref != data) {
978 1.9 christos errx(2, "%s, %zu: refresh data from slave does "
979 1.9 christos "not match expected from file %s offs %zu "
980 1.9 christos "[reference 0x%x (%c) != slave 0x%x (%c)]",
981 1.9 christos cur_file, line, check_file, offs,
982 1.9 christos ref, (ref >= ' ') ? ref : '-',
983 1.9 christos data, (data >= ' ') ? data : '-');
984 1.1 blymn }
985 1.1 blymn
986 1.9 christos offs++;
987 1.1 blymn }
988 1.1 blymn
989 1.1 blymn
990 1.1 blymn if (saved_output.count > 0)
991 1.6 christos excess(cur_file, line, __func__, " from slave",
992 1.6 christos &saved_output.data[saved_output.readp], saved_output.count);
993 1.1 blymn
994 1.1 blymn /* discard any excess saved output if required */
995 1.1 blymn if (discard) {
996 1.1 blymn saved_output.count = 0;
997 1.1 blymn saved_output.readp = 0;
998 1.1 blymn }
999 1.1 blymn
1000 1.1 blymn if ((result = poll(&fds[0], 2, 0)) != 0) {
1001 1.1 blymn if (result == -1)
1002 1.1 blymn err(2, "poll of file descriptors failed");
1003 1.1 blymn
1004 1.5 christos if ((fds[1].revents & POLLIN) == POLLIN) {
1005 1.1 blymn save_slave_output(true);
1006 1.9 christos } else if ((fds[0].revents & POLLIN) == POLLIN) {
1007 1.1 blymn /*
1008 1.1 blymn * handle excess in file if it exists. Poll
1009 1.1 blymn * says there is data until EOF is read.
1010 1.1 blymn * Check next read is EOF, if it is not then
1011 1.1 blymn * the file really has more data than the
1012 1.1 blymn * slave produced so flag this as a warning.
1013 1.1 blymn */
1014 1.5 christos result = read(check_fd, drain, sizeof(drain));
1015 1.1 blymn if (result == -1)
1016 1.3 christos err(1, "read of data file failed");
1017 1.1 blymn
1018 1.1 blymn if (result > 0) {
1019 1.6 christos excess(check_file, 0, __func__, "", drain,
1020 1.6 christos result);
1021 1.1 blymn }
1022 1.1 blymn }
1023 1.1 blymn }
1024 1.1 blymn
1025 1.1 blymn close(check_fd);
1026 1.1 blymn }
1027 1.1 blymn
1028 1.1 blymn /*
1029 1.1 blymn * Pass a function call and arguments to the slave and wait for the
1030 1.1 blymn * results. The variable nresults determines how many returns we expect
1031 1.1 blymn * back from the slave. These results will be validated against the
1032 1.1 blymn * expected returns or assigned to variables.
1033 1.1 blymn */
1034 1.1 blymn static void
1035 1.4 christos do_function_call(size_t nresults)
1036 1.1 blymn {
1037 1.1 blymn #define MAX_RESULTS 4
1038 1.1 blymn char *p;
1039 1.4 christos int do_input;
1040 1.4 christos size_t i;
1041 1.1 blymn struct pollfd fds[3];
1042 1.16 blymn ct_data_t response[MAX_RESULTS], returns_count;
1043 1.1 blymn assert(nresults <= MAX_RESULTS);
1044 1.1 blymn
1045 1.1 blymn do_input = check_function_table(command.function, input_functions,
1046 1.5 christos ninput_functions);
1047 1.1 blymn
1048 1.1 blymn write_func_and_args();
1049 1.1 blymn
1050 1.1 blymn /*
1051 1.1 blymn * We should get the number of returns back here, grab it before
1052 1.1 blymn * doing input otherwise it will confuse the input poll
1053 1.1 blymn */
1054 1.1 blymn read_cmd_pipe(&returns_count);
1055 1.16 blymn if (returns_count.data_type != data_count)
1056 1.16 blymn err(2, "expected return type of data_count but received %s",
1057 1.16 blymn enum_names[returns_count.data_type]);
1058 1.1 blymn
1059 1.10 blymn perform_delay(&delay_post_call); /* let slave catch up */
1060 1.10 blymn
1061 1.9 christos if (verbose) {
1062 1.4 christos fprintf(stderr, "Expect %zu results from slave, slave "
1063 1.16 blymn "reported %zu\n", nresults, returns_count.data_len);
1064 1.9 christos }
1065 1.1 blymn
1066 1.1 blymn if ((no_input == false) && (do_input == 1)) {
1067 1.9 christos if (verbose) {
1068 1.1 blymn fprintf(stderr, "doing input with inputstr >%s<\n",
1069 1.9 christos input_str);
1070 1.9 christos }
1071 1.1 blymn
1072 1.5 christos if (input_str == NULL)
1073 1.5 christos errx(2, "%s, %zu: Call to input function "
1074 1.5 christos "but no input defined", cur_file, line);
1075 1.1 blymn
1076 1.1 blymn fds[0].fd = slvpipe[READ_PIPE];
1077 1.1 blymn fds[0].events = POLLIN;
1078 1.9 christos fds[1].fd = master;
1079 1.9 christos fds[1].events = POLLOUT;
1080 1.1 blymn p = input_str;
1081 1.1 blymn save_slave_output(false);
1082 1.1 blymn while(*p != '\0') {
1083 1.10 blymn perform_delay(&delay_spec);
1084 1.10 blymn
1085 1.9 christos if (poll(fds, 2, 0) < 0)
1086 1.9 christos err(2, "poll failed");
1087 1.9 christos if (fds[0].revents & POLLIN) {
1088 1.9 christos warnx("%s, %zu: Slave function "
1089 1.9 christos "returned before end of input string",
1090 1.9 christos cur_file, line);
1091 1.9 christos break;
1092 1.9 christos }
1093 1.9 christos if ((fds[1].revents & POLLOUT) == 0)
1094 1.9 christos continue;
1095 1.1 blymn if (verbose) {
1096 1.1 blymn fprintf(stderr, "Writing char >%c< to slave\n",
1097 1.9 christos *p);
1098 1.1 blymn }
1099 1.9 christos if (write(master, p, 1) != 1) {
1100 1.9 christos warn("%s, %zu: Slave function write error",
1101 1.5 christos cur_file, line);
1102 1.1 blymn break;
1103 1.1 blymn }
1104 1.9 christos p++;
1105 1.9 christos
1106 1.1 blymn }
1107 1.9 christos save_slave_output(false);
1108 1.1 blymn
1109 1.1 blymn if (verbose) {
1110 1.1 blymn fprintf(stderr, "Input done.\n");
1111 1.1 blymn }
1112 1.1 blymn
1113 1.1 blymn /* done with the input string, free the resources */
1114 1.1 blymn free(input_str);
1115 1.1 blymn input_str = NULL;
1116 1.1 blymn }
1117 1.1 blymn
1118 1.11 blymn if (verbose) {
1119 1.1 blymn fds[0].fd = slvpipe[READ_PIPE];
1120 1.1 blymn fds[0].events = POLLIN;
1121 1.1 blymn
1122 1.1 blymn fds[1].fd = slvpipe[WRITE_PIPE];
1123 1.1 blymn fds[1].events = POLLOUT;
1124 1.1 blymn
1125 1.1 blymn fds[2].fd = master;
1126 1.1 blymn fds[2].events = POLLIN | POLLOUT;
1127 1.1 blymn
1128 1.1 blymn i = poll(&fds[0], 3, 1000);
1129 1.4 christos fprintf(stderr, "Poll returned %zu\n", i);
1130 1.1 blymn for (i = 0; i < 3; i++) {
1131 1.4 christos fprintf(stderr, "revents for fd[%zu] = 0x%x\n",
1132 1.1 blymn i, fds[i].revents);
1133 1.1 blymn }
1134 1.1 blymn }
1135 1.1 blymn
1136 1.1 blymn /* drain any trailing output */
1137 1.1 blymn save_slave_output(false);
1138 1.1 blymn
1139 1.16 blymn for (i = 0; i < returns_count.data_len; i++) {
1140 1.1 blymn read_cmd_pipe(&response[i]);
1141 1.1 blymn }
1142 1.1 blymn
1143 1.1 blymn /*
1144 1.1 blymn * Check for a slave error in the first return slot, if the
1145 1.1 blymn * slave errored then we may not have the number of returns we
1146 1.1 blymn * expect but in this case we should report the slave error
1147 1.1 blymn * instead of a return count mismatch.
1148 1.1 blymn */
1149 1.16 blymn if ((returns_count.data_len > 0) &&
1150 1.16 blymn (response[0].data_type == data_slave_error))
1151 1.4 christos err(2, "Slave returned error: %s",
1152 1.16 blymn (const char *)response[0].data_value);
1153 1.1 blymn
1154 1.16 blymn if (returns_count.data_len != nresults)
1155 1.4 christos err(2, "Incorrect number of returns from slave, expected %zu "
1156 1.16 blymn "but received %zu", nresults, returns_count.data_len);
1157 1.1 blymn
1158 1.1 blymn if (verbose) {
1159 1.1 blymn for (i = 0; i < nresults; i++) {
1160 1.16 blymn if ((response[i].data_type != data_byte) &&
1161 1.16 blymn (response[i].data_type != data_err) &&
1162 1.16 blymn (response[i].data_type != data_ok))
1163 1.1 blymn fprintf(stderr,
1164 1.1 blymn "received response >%s< "
1165 1.4 christos "expected",
1166 1.16 blymn (const char *)response[i].data_value);
1167 1.1 blymn else
1168 1.1 blymn fprintf(stderr, "received");
1169 1.1 blymn
1170 1.16 blymn fprintf(stderr, " data_type %s\n",
1171 1.16 blymn enum_names[command.returns[i].data_type]);
1172 1.1 blymn }
1173 1.1 blymn }
1174 1.1 blymn
1175 1.1 blymn for (i = 0; i < nresults; i++) {
1176 1.16 blymn if (command.returns[i].data_type != data_var) {
1177 1.1 blymn validate(i, &response[i]);
1178 1.1 blymn } else {
1179 1.16 blymn vars[command.returns[i].data_index].len =
1180 1.16 blymn response[i].data_len;
1181 1.16 blymn vars[command.returns[i].data_index].value =
1182 1.16 blymn response[i].data_value;
1183 1.16 blymn vars[command.returns[i].data_index].type =
1184 1.16 blymn response[i].data_type;
1185 1.1 blymn }
1186 1.1 blymn }
1187 1.10 blymn
1188 1.10 blymn if (verbose && (saved_output.count > 0))
1189 1.6 christos excess(cur_file, line, __func__, " from slave",
1190 1.6 christos &saved_output.data[saved_output.readp], saved_output.count);
1191 1.1 blymn
1192 1.1 blymn init_parse_variables(0);
1193 1.1 blymn }
1194 1.1 blymn
1195 1.1 blymn /*
1196 1.1 blymn * Write the function and command arguments to the command pipe.
1197 1.1 blymn */
1198 1.1 blymn static void
1199 1.4 christos write_func_and_args(void)
1200 1.1 blymn {
1201 1.1 blymn int i;
1202 1.1 blymn
1203 1.9 christos if (verbose) {
1204 1.1 blymn fprintf(stderr, "calling function >%s<\n", command.function);
1205 1.9 christos }
1206 1.1 blymn
1207 1.1 blymn write_cmd_pipe(command.function);
1208 1.1 blymn for (i = 0; i < command.nargs; i++) {
1209 1.16 blymn if (command.args[i].arg_type == data_var)
1210 1.1 blymn write_cmd_pipe_args(command.args[i].arg_type,
1211 1.1 blymn &vars[command.args[i].var_index]);
1212 1.1 blymn else
1213 1.1 blymn write_cmd_pipe_args(command.args[i].arg_type,
1214 1.1 blymn &command.args[i]);
1215 1.1 blymn }
1216 1.1 blymn
1217 1.1 blymn write_cmd_pipe(NULL); /* signal end of arguments */
1218 1.1 blymn }
1219 1.1 blymn
1220 1.1 blymn /*
1221 1.1 blymn * Initialise the command structure - if initial is non-zero then just set
1222 1.1 blymn * everything to sane values otherwise free any memory that was allocated
1223 1.1 blymn * when building the structure.
1224 1.1 blymn */
1225 1.1 blymn void
1226 1.1 blymn init_parse_variables(int initial)
1227 1.1 blymn {
1228 1.1 blymn int i, result;
1229 1.1 blymn struct pollfd slave_pty;
1230 1.1 blymn
1231 1.1 blymn if (initial == 0) {
1232 1.1 blymn free(command.function);
1233 1.1 blymn for (i = 0; i < command.nrets; i++) {
1234 1.16 blymn if (command.returns[i].data_type == data_number)
1235 1.16 blymn free(command.returns[i].data_value);
1236 1.1 blymn }
1237 1.1 blymn free(command.returns);
1238 1.1 blymn
1239 1.1 blymn for (i = 0; i < command.nargs; i++) {
1240 1.16 blymn if (command.args[i].arg_type != data_var)
1241 1.1 blymn free(command.args[i].arg_string);
1242 1.1 blymn }
1243 1.1 blymn free(command.args);
1244 1.1 blymn } else {
1245 1.1 blymn line = 0;
1246 1.1 blymn input_delay = 0;
1247 1.1 blymn vars = NULL;
1248 1.1 blymn nvars = 0;
1249 1.1 blymn input_str = NULL;
1250 1.1 blymn saved_output.allocated = 0;
1251 1.1 blymn saved_output.count = 0;
1252 1.1 blymn saved_output.readp = 0;
1253 1.1 blymn saved_output.data = NULL;
1254 1.1 blymn }
1255 1.1 blymn
1256 1.1 blymn no_input = false;
1257 1.1 blymn command.function = NULL;
1258 1.1 blymn command.nargs = 0;
1259 1.1 blymn command.args = NULL;
1260 1.1 blymn command.nrets = 0;
1261 1.1 blymn command.returns = NULL;
1262 1.1 blymn
1263 1.1 blymn /*
1264 1.1 blymn * Check the slave pty for stray output from the slave, at this
1265 1.1 blymn * point we should not see any data as it should have been
1266 1.1 blymn * consumed by the test functions. If we see data then we have
1267 1.1 blymn * either a bug or are not handling an output generating function
1268 1.1 blymn * correctly.
1269 1.1 blymn */
1270 1.1 blymn slave_pty.fd = master;
1271 1.1 blymn slave_pty.events = POLLIN;
1272 1.1 blymn result = poll(&slave_pty, 1, 0);
1273 1.1 blymn
1274 1.1 blymn if (result < 0)
1275 1.1 blymn err(2, "Poll of slave pty failed");
1276 1.1 blymn else if (result > 0)
1277 1.5 christos warnx("%s, %zu: Unexpected data from slave", cur_file, line);
1278 1.1 blymn }
1279 1.1 blymn
1280 1.1 blymn /*
1281 1.1 blymn * Validate the response against the expected return. The variable
1282 1.4 christos * i is the i into the rets array in command.
1283 1.1 blymn */
1284 1.1 blymn static void
1285 1.4 christos validate(int i, void *data)
1286 1.1 blymn {
1287 1.1 blymn char *response;
1288 1.16 blymn ct_data_t *byte_response;
1289 1.1 blymn
1290 1.1 blymn byte_response = data;
1291 1.16 blymn if ((command.returns[i].data_type != data_byte) &&
1292 1.16 blymn (command.returns[i].data_type != data_err) &&
1293 1.16 blymn (command.returns[i].data_type != data_ok)) {
1294 1.16 blymn if ((byte_response->data_type == data_byte) ||
1295 1.16 blymn (byte_response->data_type == data_err) ||
1296 1.16 blymn (byte_response->data_type == data_ok))
1297 1.12 christos err(1, "%s: expecting type %s, received type %s"
1298 1.12 christos " at line %zu of file %s", __func__,
1299 1.16 blymn enum_names[command.returns[i].data_type],
1300 1.16 blymn enum_names[byte_response->data_type],
1301 1.11 blymn line, cur_file);
1302 1.11 blymn
1303 1.16 blymn response = byte_response->data_value;
1304 1.11 blymn }
1305 1.1 blymn
1306 1.16 blymn switch (command.returns[i].data_type) {
1307 1.16 blymn case data_err:
1308 1.16 blymn validate_type(data_err, byte_response, 0);
1309 1.1 blymn break;
1310 1.1 blymn
1311 1.16 blymn case data_ok:
1312 1.16 blymn validate_type(data_ok, byte_response, 0);
1313 1.1 blymn break;
1314 1.1 blymn
1315 1.16 blymn case data_null:
1316 1.1 blymn validate_return("NULL", response, 0);
1317 1.1 blymn break;
1318 1.1 blymn
1319 1.16 blymn case data_nonnull:
1320 1.1 blymn validate_return("NULL", response, 1);
1321 1.1 blymn break;
1322 1.1 blymn
1323 1.16 blymn case data_string:
1324 1.16 blymn case data_number:
1325 1.16 blymn validate_return(command.returns[i].data_value,
1326 1.1 blymn response, 0);
1327 1.1 blymn break;
1328 1.1 blymn
1329 1.16 blymn case data_ref:
1330 1.4 christos validate_reference(i, response);
1331 1.1 blymn break;
1332 1.1 blymn
1333 1.16 blymn case data_byte:
1334 1.4 christos validate_byte(&command.returns[i], byte_response, 0);
1335 1.1 blymn break;
1336 1.1 blymn
1337 1.1 blymn default:
1338 1.4 christos err(1, "Malformed statement at line %zu of file %s",
1339 1.1 blymn line, cur_file);
1340 1.1 blymn break;
1341 1.1 blymn }
1342 1.1 blymn }
1343 1.1 blymn
1344 1.1 blymn /*
1345 1.1 blymn * Validate the return against the contents of a variable.
1346 1.1 blymn */
1347 1.1 blymn static void
1348 1.4 christos validate_reference(int i, void *data)
1349 1.1 blymn {
1350 1.1 blymn char *response;
1351 1.16 blymn ct_data_t *byte_response;
1352 1.1 blymn var_t *varp;
1353 1.1 blymn
1354 1.16 blymn varp = &vars[command.returns[i].data_index];
1355 1.1 blymn
1356 1.1 blymn byte_response = data;
1357 1.16 blymn if (command.returns[i].data_type != data_byte)
1358 1.1 blymn response = data;
1359 1.1 blymn
1360 1.10 blymn if (verbose) {
1361 1.1 blymn fprintf(stderr,
1362 1.12 christos "%s: return type of %s, value %s \n", __func__,
1363 1.16 blymn enum_names[varp->type],
1364 1.9 christos (const char *)varp->value);
1365 1.9 christos }
1366 1.1 blymn
1367 1.1 blymn switch (varp->type) {
1368 1.16 blymn case data_string:
1369 1.16 blymn case data_number:
1370 1.1 blymn validate_return(varp->value, response, 0);
1371 1.1 blymn break;
1372 1.1 blymn
1373 1.16 blymn case data_byte:
1374 1.1 blymn validate_byte(varp->value, byte_response, 0);
1375 1.1 blymn break;
1376 1.1 blymn
1377 1.1 blymn default:
1378 1.1 blymn err(1,
1379 1.4 christos "Invalid return type for reference at line %zu of file %s",
1380 1.1 blymn line, cur_file);
1381 1.1 blymn break;
1382 1.1 blymn }
1383 1.1 blymn }
1384 1.1 blymn
1385 1.1 blymn /*
1386 1.1 blymn * Validate the return type against the expected type, throw an error
1387 1.1 blymn * if they don't match.
1388 1.1 blymn */
1389 1.1 blymn static void
1390 1.16 blymn validate_type(data_enum_t expected, ct_data_t *value, int check)
1391 1.1 blymn {
1392 1.16 blymn if (((check == 0) && (expected != value->data_type)) ||
1393 1.16 blymn ((check == 1) && (expected == value->data_type)))
1394 1.4 christos err(1, "Validate expected type %s %s %s line %zu of file %s",
1395 1.16 blymn enum_names[expected],
1396 1.1 blymn (check == 0)? "matching" : "not matching",
1397 1.16 blymn enum_names[value->data_type], line, cur_file);
1398 1.1 blymn
1399 1.9 christos if (verbose) {
1400 1.4 christos fprintf(stderr, "Validate expected type %s %s %s line %zu"
1401 1.9 christos " of file %s\n",
1402 1.16 blymn enum_names[expected],
1403 1.9 christos (check == 0)? "matching" : "not matching",
1404 1.16 blymn enum_names[value->data_type], line, cur_file);
1405 1.9 christos }
1406 1.1 blymn }
1407 1.1 blymn
1408 1.1 blymn /*
1409 1.1 blymn * Validate the return value against the expected value, throw an error
1410 1.1 blymn * if they don't match.
1411 1.1 blymn */
1412 1.1 blymn static void
1413 1.4 christos validate_return(const char *expected, const char *value, int check)
1414 1.1 blymn {
1415 1.1 blymn if (((check == 0) && strcmp(expected, value) != 0) ||
1416 1.1 blymn ((check == 1) && strcmp(expected, value) == 0))
1417 1.15 blymn errx(1, "Validate expected >%s< %s >%s< line %zu of file %s",
1418 1.1 blymn expected,
1419 1.1 blymn (check == 0)? "matching" : "not matching", value,
1420 1.1 blymn line, cur_file);
1421 1.9 christos if (verbose) {
1422 1.15 blymn fprintf(stderr, "Validated expected value >%s< %s >%s< "
1423 1.9 christos "at line %zu of file %s\n", expected,
1424 1.9 christos (check == 0)? "matches" : "does not match",
1425 1.9 christos value, line, cur_file);
1426 1.9 christos }
1427 1.1 blymn }
1428 1.1 blymn
1429 1.1 blymn /*
1430 1.1 blymn * Validate the return value against the expected value, throw an error
1431 1.1 blymn * if they don't match expectations.
1432 1.1 blymn */
1433 1.1 blymn static void
1434 1.16 blymn validate_byte(ct_data_t *expected, ct_data_t *value, int check)
1435 1.1 blymn {
1436 1.11 blymn char *ch;
1437 1.11 blymn size_t i;
1438 1.11 blymn
1439 1.11 blymn if (verbose) {
1440 1.16 blymn ch = value->data_value;
1441 1.11 blymn fprintf(stderr, "checking returned byte stream: ");
1442 1.16 blymn for (i = 0; i < value->data_len; i++)
1443 1.11 blymn fprintf(stderr, "%s0x%x", (i != 0)? ", " : "", ch[i]);
1444 1.11 blymn fprintf(stderr, "\n");
1445 1.11 blymn
1446 1.11 blymn fprintf(stderr, "%s byte stream: ",
1447 1.11 blymn (check == 0)? "matches" : "does not match");
1448 1.16 blymn ch = (char *) expected->data_value;
1449 1.16 blymn for (i = 0; i < expected->data_len; i++)
1450 1.11 blymn fprintf(stderr, "%s0x%x", (i != 0)? ", " : "", ch[i]);
1451 1.11 blymn fprintf(stderr, "\n");
1452 1.11 blymn }
1453 1.11 blymn
1454 1.1 blymn /*
1455 1.1 blymn * No chance of a match if lengths differ...
1456 1.1 blymn */
1457 1.16 blymn if ((check == 0) && (expected->data_len != value->data_len))
1458 1.11 blymn errx(1, "Byte validation failed, length mismatch, expected %zu,"
1459 1.16 blymn "received %zu", expected->data_len, value->data_len);
1460 1.1 blymn
1461 1.1 blymn /*
1462 1.1 blymn * If check is 0 then we want to throw an error IFF the byte streams
1463 1.1 blymn * do not match, if check is 1 then throw an error if the byte
1464 1.1 blymn * streams match.
1465 1.1 blymn */
1466 1.16 blymn if (((check == 0) && memcmp(expected->data_value, value->data_value,
1467 1.16 blymn value->data_len) != 0) ||
1468 1.16 blymn ((check == 1) && (expected->data_len == value->data_len) &&
1469 1.16 blymn memcmp(expected->data_value, value->data_value,
1470 1.16 blymn value->data_len) == 0))
1471 1.9 christos errx(1, "Validate expected %s byte stream at line %zu"
1472 1.1 blymn "of file %s",
1473 1.1 blymn (check == 0)? "matching" : "not matching", line, cur_file);
1474 1.9 christos if (verbose) {
1475 1.1 blymn fprintf(stderr, "Validated expected %s byte stream "
1476 1.9 christos "at line %zu of file %s\n",
1477 1.9 christos (check == 0)? "matching" : "not matching",
1478 1.9 christos line, cur_file);
1479 1.9 christos }
1480 1.1 blymn }
1481 1.1 blymn
1482 1.1 blymn /*
1483 1.4 christos * Validate the variable at i against the expected value, throw an
1484 1.1 blymn * error if they don't match, if check is non-zero then the match is
1485 1.1 blymn * negated.
1486 1.1 blymn */
1487 1.1 blymn static void
1488 1.16 blymn validate_variable(int ret, data_enum_t type, const void *value, int i,
1489 1.4 christos int check)
1490 1.1 blymn {
1491 1.16 blymn ct_data_t *retval;
1492 1.1 blymn var_t *varptr;
1493 1.1 blymn
1494 1.7 joerg retval = &command.returns[ret];
1495 1.16 blymn varptr = &vars[command.returns[ret].data_index];
1496 1.1 blymn
1497 1.1 blymn if (varptr->value == NULL)
1498 1.4 christos err(1, "Variable %s has no value assigned to it", varptr->name);
1499 1.1 blymn
1500 1.1 blymn
1501 1.1 blymn if (varptr->type != type)
1502 1.4 christos err(1, "Variable %s is not the expected type", varptr->name);
1503 1.1 blymn
1504 1.16 blymn if (type != data_byte) {
1505 1.1 blymn if ((((check == 0) && strcmp(value, varptr->value) != 0))
1506 1.1 blymn || ((check == 1) && strcmp(value, varptr->value) == 0))
1507 1.1 blymn err(1, "Variable %s contains %s instead of %s"
1508 1.4 christos " value %s at line %zu of file %s",
1509 1.4 christos varptr->name, (const char *)varptr->value,
1510 1.4 christos (check == 0)? "expected" : "not matching",
1511 1.4 christos (const char *)value,
1512 1.1 blymn line, cur_file);
1513 1.9 christos if (verbose) {
1514 1.1 blymn fprintf(stderr, "Variable %s contains %s value "
1515 1.9 christos "%s at line %zu of file %s\n",
1516 1.9 christos varptr->name,
1517 1.9 christos (check == 0)? "expected" : "not matching",
1518 1.9 christos (const char *)varptr->value, line, cur_file);
1519 1.9 christos }
1520 1.1 blymn } else {
1521 1.16 blymn if ((check == 0) && (retval->data_len != varptr->len))
1522 1.1 blymn err(1, "Byte validation failed, length mismatch");
1523 1.1 blymn
1524 1.1 blymn /*
1525 1.1 blymn * If check is 0 then we want to throw an error IFF
1526 1.1 blymn * the byte streams do not match, if check is 1 then
1527 1.1 blymn * throw an error if the byte streams match.
1528 1.1 blymn */
1529 1.16 blymn if (((check == 0) && memcmp(retval->data_value, varptr->value,
1530 1.1 blymn varptr->len) != 0) ||
1531 1.16 blymn ((check == 1) && (retval->data_len == varptr->len) &&
1532 1.16 blymn memcmp(retval->data_value, varptr->value,
1533 1.1 blymn varptr->len) == 0))
1534 1.4 christos err(1, "Validate expected %s byte stream at line %zu"
1535 1.1 blymn " of file %s",
1536 1.1 blymn (check == 0)? "matching" : "not matching",
1537 1.1 blymn line, cur_file);
1538 1.9 christos if (verbose) {
1539 1.1 blymn fprintf(stderr, "Validated expected %s byte stream "
1540 1.9 christos "at line %zu of file %s\n",
1541 1.9 christos (check == 0)? "matching" : "not matching",
1542 1.9 christos line, cur_file);
1543 1.9 christos }
1544 1.1 blymn }
1545 1.1 blymn }
1546 1.1 blymn
1547 1.1 blymn /*
1548 1.1 blymn * Write a string to the command pipe - we feed the number of bytes coming
1549 1.1 blymn * down first to allow storage allocation and then follow up with the data.
1550 1.1 blymn * If cmd is NULL then feed a -1 down the pipe to say the end of the args.
1551 1.1 blymn */
1552 1.1 blymn static void
1553 1.1 blymn write_cmd_pipe(char *cmd)
1554 1.1 blymn {
1555 1.1 blymn args_t arg;
1556 1.1 blymn size_t len;
1557 1.1 blymn
1558 1.1 blymn if (cmd == NULL)
1559 1.1 blymn len = 0;
1560 1.1 blymn else
1561 1.1 blymn len = strlen(cmd);
1562 1.1 blymn
1563 1.16 blymn arg.arg_type = data_static;
1564 1.1 blymn arg.arg_len = len;
1565 1.1 blymn arg.arg_string = cmd;
1566 1.1 blymn write_cmd_pipe_args(arg.arg_type, &arg);
1567 1.1 blymn
1568 1.1 blymn }
1569 1.1 blymn
1570 1.1 blymn static void
1571 1.16 blymn write_cmd_pipe_args(data_enum_t type, void *data)
1572 1.1 blymn {
1573 1.1 blymn var_t *var_data;
1574 1.1 blymn args_t *arg_data;
1575 1.1 blymn int len, send_type;
1576 1.1 blymn void *cmd;
1577 1.1 blymn
1578 1.1 blymn arg_data = data;
1579 1.2 blymn switch (type) {
1580 1.16 blymn case data_var:
1581 1.2 blymn var_data = data;
1582 1.2 blymn len = var_data->len;
1583 1.2 blymn cmd = var_data->value;
1584 1.16 blymn
1585 1.16 blymn switch (var_data->type) {
1586 1.16 blymn case data_byte:
1587 1.16 blymn send_type = data_byte;
1588 1.16 blymn break;
1589 1.16 blymn
1590 1.16 blymn case data_cchar:
1591 1.16 blymn send_type = data_cchar;
1592 1.16 blymn cmd = (void *) &var_data->cchar;
1593 1.16 blymn len = sizeof(cchar_t);
1594 1.16 blymn break;
1595 1.16 blymn
1596 1.16 blymn default:
1597 1.16 blymn send_type = data_string;
1598 1.16 blymn break;
1599 1.16 blymn }
1600 1.2 blymn break;
1601 1.2 blymn
1602 1.16 blymn case data_null:
1603 1.16 blymn send_type = data_null;
1604 1.2 blymn len = 0;
1605 1.2 blymn break;
1606 1.2 blymn
1607 1.2 blymn default:
1608 1.1 blymn if ((arg_data->arg_len == 0) && (arg_data->arg_string == NULL))
1609 1.1 blymn len = -1;
1610 1.1 blymn else
1611 1.1 blymn len = arg_data->arg_len;
1612 1.1 blymn cmd = arg_data->arg_string;
1613 1.16 blymn if (type == data_byte)
1614 1.16 blymn send_type = data_byte;
1615 1.1 blymn else
1616 1.16 blymn send_type = data_string;
1617 1.1 blymn }
1618 1.1 blymn
1619 1.9 christos if (verbose) {
1620 1.1 blymn fprintf(stderr, "Writing type %s to command pipe\n",
1621 1.16 blymn enum_names[send_type]);
1622 1.9 christos }
1623 1.1 blymn
1624 1.4 christos if (write(cmdpipe[WRITE_PIPE], &send_type, sizeof(int)) < 0)
1625 1.4 christos err(1, "command pipe write for type failed");
1626 1.1 blymn
1627 1.9 christos if (verbose) {
1628 1.16 blymn if (send_type == data_cchar)
1629 1.16 blymn fprintf(stderr,
1630 1.16 blymn "Writing cchar to command pipe\n");
1631 1.16 blymn else
1632 1.16 blymn fprintf(stderr,
1633 1.16 blymn "Writing length %d to command pipe\n", len);
1634 1.9 christos }
1635 1.1 blymn
1636 1.4 christos if (write(cmdpipe[WRITE_PIPE], &len, sizeof(int)) < 0)
1637 1.4 christos err(1, "command pipe write for length failed");
1638 1.1 blymn
1639 1.1 blymn if (len > 0) {
1640 1.9 christos if (verbose) {
1641 1.1 blymn fprintf(stderr, "Writing data >%s< to command pipe\n",
1642 1.9 christos (const char *)cmd);
1643 1.9 christos }
1644 1.4 christos if (write(cmdpipe[WRITE_PIPE], cmd, len) < 0)
1645 1.4 christos err(1, "command pipe write of data failed");
1646 1.1 blymn }
1647 1.1 blymn }
1648 1.1 blymn
1649 1.1 blymn /*
1650 1.1 blymn * Read a response from the command pipe, first we will receive the
1651 1.1 blymn * length of the response then the actual data.
1652 1.1 blymn */
1653 1.1 blymn static void
1654 1.16 blymn read_cmd_pipe(ct_data_t *response)
1655 1.1 blymn {
1656 1.1 blymn int len, type;
1657 1.4 christos struct pollfd rfd[2];
1658 1.1 blymn char *str;
1659 1.1 blymn
1660 1.1 blymn /*
1661 1.1 blymn * Check if there is data to read - just in case slave has died, we
1662 1.1 blymn * don't want to block on the read and just hang. We also check
1663 1.1 blymn * output from the slave because the slave may be blocked waiting
1664 1.1 blymn * for a flush on its stdout.
1665 1.1 blymn */
1666 1.4 christos rfd[0].fd = slvpipe[READ_PIPE];
1667 1.4 christos rfd[0].events = POLLIN;
1668 1.4 christos rfd[1].fd = master;
1669 1.4 christos rfd[1].events = POLLIN;
1670 1.1 blymn
1671 1.1 blymn do {
1672 1.5 christos if (poll(rfd, 2, 4000) == 0)
1673 1.5 christos errx(2, "%s, %zu: Command pipe read timeout",
1674 1.5 christos cur_file, line);
1675 1.1 blymn
1676 1.4 christos if ((rfd[1].revents & POLLIN) == POLLIN) {
1677 1.9 christos if (verbose) {
1678 1.1 blymn fprintf(stderr,
1679 1.9 christos "draining output from slave\n");
1680 1.9 christos }
1681 1.1 blymn save_slave_output(false);
1682 1.1 blymn }
1683 1.1 blymn }
1684 1.4 christos while((rfd[1].revents & POLLIN) == POLLIN);
1685 1.1 blymn
1686 1.4 christos if (read(slvpipe[READ_PIPE], &type, sizeof(int)) < 0)
1687 1.4 christos err(1, "command pipe read for type failed");
1688 1.16 blymn response->data_type = type;
1689 1.1 blymn
1690 1.16 blymn if ((type != data_ok) && (type != data_err) && (type != data_count)) {
1691 1.4 christos if (read(slvpipe[READ_PIPE], &len, sizeof(int)) < 0)
1692 1.4 christos err(1, "command pipe read for length failed");
1693 1.16 blymn response->data_len = len;
1694 1.1 blymn
1695 1.9 christos if (verbose) {
1696 1.1 blymn fprintf(stderr,
1697 1.9 christos "Reading %d bytes from command pipe\n", len);
1698 1.9 christos }
1699 1.1 blymn
1700 1.16 blymn if ((response->data_value = malloc(len + 1)) == NULL)
1701 1.1 blymn err(1, "Failed to alloc memory for cmd pipe read");
1702 1.1 blymn
1703 1.16 blymn if (read(slvpipe[READ_PIPE], response->data_value, len) < 0)
1704 1.4 christos err(1, "command pipe read of data failed");
1705 1.1 blymn
1706 1.16 blymn if (response->data_type != data_byte) {
1707 1.16 blymn str = response->data_value;
1708 1.1 blymn str[len] = '\0';
1709 1.1 blymn
1710 1.9 christos if (verbose) {
1711 1.1 blymn fprintf(stderr, "Read data >%s< from pipe\n",
1712 1.16 blymn (const char *)response->data_value);
1713 1.9 christos }
1714 1.1 blymn }
1715 1.1 blymn } else {
1716 1.16 blymn response->data_value = NULL;
1717 1.16 blymn if (type == data_count) {
1718 1.4 christos if (read(slvpipe[READ_PIPE], &len, sizeof(int)) < 0)
1719 1.4 christos err(1, "command pipe read for number of "
1720 1.1 blymn "returns failed");
1721 1.16 blymn response->data_len = len;
1722 1.1 blymn }
1723 1.1 blymn
1724 1.9 christos if (verbose) {
1725 1.1 blymn fprintf(stderr, "Read type %s from pipe\n",
1726 1.16 blymn enum_names[type]);
1727 1.9 christos }
1728 1.1 blymn }
1729 1.1 blymn }
1730 1.1 blymn
1731 1.1 blymn /*
1732 1.1 blymn * Check for writes from the slave on the pty, save the output into a
1733 1.1 blymn * buffer for later checking if discard is false.
1734 1.1 blymn */
1735 1.1 blymn #define MAX_DRAIN 256
1736 1.1 blymn
1737 1.1 blymn static void
1738 1.1 blymn save_slave_output(bool discard)
1739 1.1 blymn {
1740 1.1 blymn char *new_data, drain[MAX_DRAIN];
1741 1.1 blymn size_t to_allocate;
1742 1.4 christos ssize_t result;
1743 1.4 christos size_t i;
1744 1.1 blymn
1745 1.1 blymn result = 0;
1746 1.9 christos for (;;) {
1747 1.1 blymn if (result == -1)
1748 1.1 blymn err(2, "poll of slave pty failed");
1749 1.1 blymn result = MAX_DRAIN;
1750 1.9 christos if ((result = read(master, drain, result)) < 0) {
1751 1.1 blymn if (errno == EAGAIN)
1752 1.1 blymn break;
1753 1.1 blymn else
1754 1.1 blymn err(2, "draining slave pty failed");
1755 1.1 blymn }
1756 1.9 christos if (result == 0)
1757 1.9 christos abort();
1758 1.1 blymn
1759 1.5 christos if (!discard) {
1760 1.4 christos if ((size_t)result >
1761 1.1 blymn (saved_output.allocated - saved_output.count)) {
1762 1.1 blymn to_allocate = 1024 * ((result / 1024) + 1);
1763 1.1 blymn
1764 1.1 blymn if ((new_data = realloc(saved_output.data,
1765 1.1 blymn saved_output.allocated + to_allocate))
1766 1.1 blymn == NULL)
1767 1.1 blymn err(2, "Realloc of saved_output failed");
1768 1.1 blymn saved_output.data = new_data;
1769 1.1 blymn saved_output.allocated += to_allocate;
1770 1.1 blymn }
1771 1.1 blymn
1772 1.1 blymn if (verbose) {
1773 1.5 christos fprintf(stderr, "count = %zu, "
1774 1.5 christos "allocated = %zu\n", saved_output.count,
1775 1.5 christos saved_output.allocated);
1776 1.4 christos for (i = 0; i < (size_t)result; i++) {
1777 1.1 blymn fprintf(stderr, "Saving slave output "
1778 1.9 christos "at %zu: 0x%x (%c)\n",
1779 1.9 christos saved_output.count + i, drain[i],
1780 1.5 christos (drain[i] >= ' ')? drain[i] : '-');
1781 1.1 blymn }
1782 1.1 blymn }
1783 1.1 blymn
1784 1.1 blymn memcpy(&saved_output.data[saved_output.count], drain,
1785 1.1 blymn result);
1786 1.1 blymn saved_output.count += result;
1787 1.1 blymn
1788 1.1 blymn if (verbose) {
1789 1.5 christos fprintf(stderr, "count = %zu, "
1790 1.5 christos "allocated = %zu\n", saved_output.count,
1791 1.5 christos saved_output.allocated);
1792 1.1 blymn }
1793 1.1 blymn } else {
1794 1.1 blymn if (verbose) {
1795 1.9 christos for (i = 0; i < (size_t)result; i++) {
1796 1.1 blymn fprintf(stderr, "Discarding slave "
1797 1.5 christos "output 0x%x (%c)\n",
1798 1.5 christos drain[i],
1799 1.5 christos (drain[i] >= ' ')? drain[i] : '-');
1800 1.1 blymn }
1801 1.1 blymn }
1802 1.1 blymn }
1803 1.1 blymn }
1804 1.1 blymn }
1805 1.1 blymn
1806 1.1 blymn static void
1807 1.1 blymn yyerror(const char *msg)
1808 1.1 blymn {
1809 1.1 blymn warnx("%s in line %zu of file %s", msg, line, cur_file);
1810 1.1 blymn }
1811