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