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