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