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