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