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