2 /* $NetBSD: testlang_parse.y,v 1.13 2012/09/19 11:51:56 blymn Exp $ */
5 * Copyright 2009 Brett Lymn <blymn@NetBSD.org>
9 * This code has been donated to The NetBSD Foundation by the Author.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software withough specific prior written permission
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.
42 #include <sys/syslimits.h>
51 extern int cmdpipe[2];
52 extern int slvpipe[2];
54 extern struct pollfd readfd;
55 extern char *check_path;
56 extern char *cur_file; /* from director.c */
62 static int input_delay;
64 /* time delay between inputs chars - default to 0.1ms minimum to prevent
65 * problems with input tests
69 /* time delay after a function call - allows the slave time to
70 * run the function and output data before we do other actions.
73 #define POST_CALL_DELAY 50
75 static struct timespec delay_spec = {0, 1000 * DELAY_MIN};
76 static struct timespec delay_post_call = {0, 1000 * POST_CALL_DELAY};
78 static char *input_str; /* string to feed in as input */
79 static bool no_input; /* don't need more input */
84 const char *returns_enum_names[] = {
85 "unused", "numeric", "string", "byte", "ERR", "OK", "NULL", "not NULL",
86 "variable", "reference", "returns count", "slave error"
96 static const char *args_enum_names[] = {
97 "static", "byte", "var", "NULL"
101 args_state_t arg_type;
109 int nrets; /* number of returns */
110 returns_t *returns; /* array of expected returns */
111 int nargs; /* number of arguments */
112 args_t *args; /* arguments for the call */
115 static cmd_line_t command;
124 static size_t nvars; /* Number of declared variables */
125 static var_t *vars; /* Variables defined during the test. */
127 static int check_function_table(char *, const char *[], int);
128 static int find_var_index(const char *);
129 static void assign_arg(args_state_t, void *);
130 static int assign_var(char *);
131 void init_parse_variables(int);
132 static void validate(int, void *);
133 static void validate_return(const char *, const char *, int);
134 static void validate_variable(int, returns_enum_t, const void *, int, int);
135 static void validate_byte(returns_t *, returns_t *, int);
136 static void write_cmd_pipe(char *);
137 static void write_cmd_pipe_args(args_state_t, void *);
138 static void read_cmd_pipe(returns_t *);
139 static void write_func_and_args(void);
140 static void compare_streams(char *, bool);
141 static void do_function_call(size_t);
142 static void save_slave_output(bool);
143 static void validate_type(returns_enum_t, returns_t *, int);
144 static void set_var(returns_enum_t, char *, void *);
145 static void validate_reference(int, void *);
146 static char *numeric_or(char *, char *);
147 static char *get_numeric_var(const char *);
148 static void perform_delay(struct timespec *);
150 static const char *input_functions[] = {
151 "getch", "getnstr", "getstr", "mvgetnstr", "mvgetstr", "mvgetnstr",
152 "mvgetstr", "mvscanw", "mvwscanw", "scanw", "wgetch", "wgetnstr",
156 static const unsigned ninput_functions =
157 sizeof(input_functions) / sizeof(char *);
159 saved_data_t saved_output;
169 %token <string> STRING
171 %token <string> VARNAME
172 %token <string> FILENAME
173 %token <string> VARIABLE
174 %token <string> REFERENCE
175 %token <string> NULL_RET
176 %token <string> NON_NULL
177 %token <string> ERR_RET
178 %token <string> OK_RET
179 %token <string> numeric
180 %token <string> DELAY
181 %token <string> INPUT
182 %token <string> COMPARE
183 %token <string> COMPAREND
184 %token <string> ASSIGN
185 %token EOL CALL CHECK NOINPUT OR LHB RHB
186 %token CALL2 CALL3 CALL4 DRAIN
192 statement : /* empty */
203 | comparend statement
207 assign : ASSIGN VARNAME numeric {set_var(ret_number, $2, $3);} eol
208 | ASSIGN VARNAME LHB expr RHB {set_var(ret_number, $2, $<string>4);} eol
209 | ASSIGN VARNAME STRING {set_var(ret_string, $2, $3);} eol
210 | ASSIGN VARNAME BYTE {set_var(ret_byte, $2, $3);} eol
213 call : CALL result fn_name args eol {
218 call2 : CALL2 result result fn_name args eol {
223 call3 : CALL3 result result result fn_name args eol {
228 call4 : CALL4 result result result result fn_name args eol {
233 check : CHECK var returns eol {
236 if (command.returns[0].return_index == -1)
237 err(1, "Undefined variable in check statement, line %zu"
238 " of file %s", line, cur_file);
241 fprintf(stderr, "Checking contents of variable %s for %s\n",
242 vars[command.returns[0].return_index].name,
243 returns_enum_names[command.returns[1].return_type]);
246 if (((command.returns[1].return_type == ret_byte) &&
247 (vars[command.returns[0].return_index].type != ret_byte)) ||
248 vars[command.returns[0].return_index].type != ret_string)
249 err(1, "Var type %s (%d) does not match return type %s (%d)",
251 vars[command.returns[0].return_index].type],
252 vars[command.returns[0].return_index].type,
253 returns_enum_names[command.returns[1].return_type],
254 command.returns[1].return_type);
256 switch (command.returns[1].return_type) {
258 validate_variable(0, ret_string, "ERR",
259 command.returns[0].return_index, 0);
263 validate_variable(0, ret_string, "OK",
264 command.returns[0].return_index, 0);
268 validate_variable(0, ret_string, "NULL",
269 command.returns[0].return_index, 0);
273 validate_variable(0, ret_string, "NULL",
274 command.returns[0].return_index, 1);
280 fprintf(stderr, " %s == returned %s\n",
281 (const char *)command.returns[1].return_value,
283 vars[command.returns[0].return_index].value);
285 validate_variable(0, ret_string,
286 command.returns[1].return_value,
287 command.returns[0].return_index, 0);
291 vptr = &vars[command.returns[0].return_index];
292 retvar.return_len = vptr->len;
293 retvar.return_type = vptr->type;
294 retvar.return_value = vptr->value;
295 validate_byte(&retvar, &command.returns[1], 0);
299 err(1, "Malformed check statement at line %zu "
300 "of file %s", line, cur_file);
304 init_parse_variables(0);
308 delay : DELAY numeric eol {
309 /* set the inter-character delay */
310 if (sscanf($2, "%d", &input_delay) == 0)
311 err(1, "delay specification %s could not be converted to "
312 "numeric at line %zu of file %s", $2, line, cur_file);
314 fprintf(stderr, "Set input delay to %d ms\n", input_delay);
317 if (input_delay < DELAY_MIN)
318 input_delay = DELAY_MIN;
320 * Fill in the timespec structure now ready for use later.
321 * The delay is specified in milliseconds so convert to timespec
324 delay_spec.tv_sec = input_delay / 1000;
325 delay_spec.tv_nsec = (input_delay - 1000 * delay_spec.tv_sec) * 1000;
327 fprintf(stderr, "set delay to %jd.%jd\n",
328 (intmax_t)delay_spec.tv_sec,
329 (intmax_t)delay_spec.tv_nsec);
332 init_parse_variables(0);
336 input : INPUT STRING eol {
337 if (input_str != NULL) {
338 warnx("%s, %zu: Discarding unused input string",
343 if ((input_str = malloc(strlen($2) + 1)) == NULL)
344 err(2, "Cannot allocate memory for input string");
346 strlcpy(input_str, $2, strlen($2) + 1);
351 noinput : NOINPUT eol {
352 if (input_str != NULL) {
353 warnx("%s, %zu: Discarding unused input string",
361 compare : COMPARE PATH eol
362 | COMPARE FILENAME eol
364 compare_streams($2, true);
369 comparend : COMPAREND PATH eol
370 | COMPAREND FILENAME eol
372 compare_streams($2, false);
382 returns : numeric { assign_rets(ret_number, $1); }
383 | LHB expr RHB { assign_rets(ret_number, $<string>2); }
384 | STRING { assign_rets(ret_string, $1); }
385 | BYTE { assign_rets(ret_byte, (void *) $1); }
386 | ERR_RET { assign_rets(ret_err, NULL); }
387 | OK_RET { assign_rets(ret_ok, NULL); }
388 | NULL_RET { assign_rets(ret_null, NULL); }
389 | NON_NULL { assign_rets(ret_nonnull, NULL); }
393 assign_rets(ret_var, $1);
397 reference : VARIABLE {
398 assign_rets(ret_ref, $1);
402 if (command.function != NULL)
403 free(command.function);
405 command.function = malloc(strlen($1) + 1);
406 if (command.function == NULL)
407 err(1, "Could not allocate memory for function name");
408 strcpy(command.function, $1);
414 { $<string>$ = get_numeric_var($1); }
416 { $<string>$ = numeric_or($<string>1, $<string>3); }
420 | LHB expr RHB { assign_arg(arg_static, $<string>2); } args
421 | numeric { assign_arg(arg_static, $1); } args
422 | STRING { assign_arg(arg_static, $1); } args
423 | BYTE { assign_arg(arg_byte, $1); } args
424 | PATH { assign_arg(arg_static, $1); } args
425 | FILENAME { assign_arg(arg_static, $1); } args
426 | VARNAME { assign_arg(arg_static, $1); } args
427 | VARIABLE { assign_arg(arg_var, $1); } args
428 | NULL_RET { assign_arg(arg_null, $1); } args
437 excess(const char *fname, size_t lineno, const char *func, const char *comment,
438 const void *data, size_t datalen)
440 size_t dstlen = datalen * 4 + 1;
441 char *dst = malloc(dstlen);
446 if (strnvisx(dst, dstlen, data, datalen, VIS_WHITE | VIS_OCTAL) == -1)
449 warnx("%s, %zu: [%s] Excess %zu bytes%s [%s]",
450 fname, lineno, func, datalen, comment, dst);
455 * Get the value of a variable, error if the variable has not been set or
456 * is not a numeric type.
459 get_numeric_var(const char *var)
463 if ((i = find_var_index(var)) < 0)
464 err(1, "Variable %s is undefined", var);
466 if (vars[i].type != ret_number)
467 err(1, "Variable %s is not a numeric type", var);
469 return vars[i].value;
473 * Perform a bitwise OR on two numbers and return the result.
476 numeric_or(char *n1, char *n2)
478 unsigned long i1, i2, result;
481 i1 = strtoul(n1, NULL, 10);
482 i2 = strtoul(n2, NULL, 10);
485 asprintf(&ret, "%lu", result);
488 fprintf(stderr, "numeric or of 0x%lx (%s) and 0x%lx (%s)"
489 " results in 0x%lx (%s)\n",
490 i1, n1, i2, n2, result, ret);
497 * Sleep for the specified time, handle the sleep getting interrupted
501 perform_delay(struct timespec *ts)
503 struct timespec delay_copy, delay_remainder;
506 while (nanosleep(&delay_copy, &delay_remainder) < 0) {
508 err(2, "nanosleep returned error");
509 delay_copy = delay_remainder;
514 * Assign the value given to the named variable.
517 set_var(returns_enum_t type, char *name, void *value)
523 i = find_var_index(name);
525 i = assign_var(name);
528 if ((type == ret_number) || (type == ret_string)) {
530 vars[i].len = strlen(number) + 1;
531 vars[i].value = malloc(vars[i].len + 1);
532 if (vars[i].value == NULL)
533 err(1, "Could not malloc memory for assign string");
534 strcpy(vars[i].value, number);
536 /* can only be a byte value */
538 vars[i].len = ret->return_len;
539 vars[i].value = malloc(vars[i].len);
540 if (vars[i].value == NULL)
541 err(1, "Could not malloc memory to assign byte string");
542 memcpy(vars[i].value, ret->return_value, vars[i].len);
547 * Add a new variable to the vars array, the value will be assigned later,
548 * when a test function call returns.
551 assign_var(char *varname)
556 if ((name = malloc(strlen(varname) + 1)) == NULL)
557 err(1, "Alloc of varname failed");
559 if ((temp = realloc(vars, sizeof(*temp) * (nvars + 1))) == NULL) {
561 err(1, "Realloc of vars array failed");
564 strcpy(name, varname);
566 vars[nvars].name = name;
568 vars[nvars].value = NULL;
575 * Allocate and assign a new argument of the given type.
578 assign_arg(args_state_t arg_type, void *arg)
585 fprintf(stderr, "function is >%s<, adding arg >%s< type %s\n",
586 command.function, str, args_enum_names[arg_type]);
589 cur.arg_type = arg_type;
592 cur.var_index = find_var_index(arg);
593 if (cur.var_index < 0)
594 err(1, "Invalid variable %s at line %zu of file %s",
595 str, line, cur_file);
596 cur.arg_type = ret_string;
601 cur.arg_len = ret->return_len;
602 cur.arg_string = malloc(cur.arg_len);
603 if (cur.arg_string == NULL)
604 err(1, "Could not malloc memory for arg bytes");
605 memcpy(cur.arg_string, ret->return_value, cur.arg_len);
610 cur.arg_string = NULL;
614 cur.arg_len = strlen(str);
615 cur.arg_string = malloc(cur.arg_len + 1);
616 if (cur.arg_string == NULL)
617 err(1, "Could not malloc memory for arg string");
618 strcpy(cur.arg_string, arg);
621 temp = realloc(command.args, sizeof(*temp) * (command.nargs + 1));
623 err(1, "Failed to reallocate args");
625 memcpy(&command.args[command.nargs], &cur, sizeof(args_t));
630 * Allocate and assign a new return.
633 assign_rets(returns_enum_t ret_type, void *ret)
635 returns_t *temp, cur;
639 cur.return_type = ret_type;
640 if (ret_type != ret_var) {
641 if ((ret_type == ret_number) || (ret_type == ret_string)) {
643 cur.return_len = strlen(ret_str) + 1;
644 cur.return_value = malloc(cur.return_len + 1);
645 if (cur.return_value == NULL)
647 "Could not malloc memory for arg string");
648 strcpy(cur.return_value, ret_str);
649 } else if (ret_type == ret_byte) {
651 cur.return_len = ret_ret->return_len;
652 cur.return_value = malloc(cur.return_len);
653 if (cur.return_value == NULL)
655 "Could not malloc memory for byte string");
656 memcpy(cur.return_value, ret_ret->return_value,
658 } else if (ret_type == ret_ref) {
659 if ((cur.return_index = find_var_index(ret)) < 0)
660 err(1, "Undefined variable reference");
663 cur.return_index = find_var_index(ret);
664 if (cur.return_index < 0)
665 cur.return_index = assign_var(ret);
668 temp = realloc(command.returns, sizeof(*temp) * (command.nrets + 1));
670 err(1, "Failed to reallocate returns");
671 command.returns = temp;
672 memcpy(&command.returns[command.nrets], &cur, sizeof(returns_t));
677 * Find the given variable name in the var array and return the i
678 * return -1 if var is not found.
681 find_var_index(const char *var_name)
688 for (i = 0; i < nvars; i++) {
689 if (strcmp(var_name, vars[i].name) == 0) {
699 * Check the given function name in the given table of names, return 1 if
702 static int check_function_table(char *function, const char *table[],
707 for (i = 0; i < nfunctions; i++) {
708 if ((strlen(function) == strlen(table[i])) &&
709 (strcmp(function, table[i]) == 0))
717 * Compare the output from the slave against the given file and report
721 compare_streams(char *filename, bool discard)
723 char check_file[PATH_MAX], drain[100], ref, data;
724 struct pollfd fds[2];
730 * Don't prepend check path iff check file has an absolute
733 if (filename[0] != '/') {
734 if (strlcpy(check_file, check_path, sizeof(check_file))
735 >= sizeof(check_file))
736 err(2, "CHECK_PATH too long");
738 if (strlcat(check_file, "/", sizeof(check_file))
739 >= sizeof(check_file))
740 err(2, "Could not append / to check file path");
742 check_file[0] = '\0';
745 if (strlcat(check_file, filename, sizeof(check_file))
746 >= sizeof(check_file))
747 err(2, "Path to check file path overflowed");
749 if ((check_fd = open(check_file, O_RDONLY, 0)) < 0)
750 err(2, "failed to open file %s line %zu of file %s",
751 check_file, line, cur_file);
753 fds[0].fd = check_fd;
754 fds[0].events = POLLIN;
756 fds[1].events = POLLIN;
760 * if we have saved output then only check for data in the
761 * reference file since the slave data may already be drained.
763 if (saved_output.count > 0)
767 while (poll(fds, nfd, 500) == nfd) {
768 if (fds[0].revents & POLLIN) {
769 if ((result = read(check_fd, &ref, 1)) < 1) {
772 "Bad read on file %s", check_file);
779 if (saved_output.count > 0) {
780 data = saved_output.data[saved_output.readp];
781 saved_output.count--;
782 saved_output.readp++;
783 /* run out of saved data, switch to file */
784 if (saved_output.count == 0)
787 if (fds[0].revents & POLLIN) {
788 if (read(master, &data, 1) < 1)
789 err(2, "Bad read on slave pty");
795 fprintf(stderr, "Comparing reference byte 0x%x (%c)"
796 " against slave byte 0x%x (%c)\n",
797 ref, (ref >= ' ') ? ref : '-',
798 data, (data >= ' ' )? data : '-');
802 errx(2, "%s, %zu: refresh data from slave does "
803 "not match expected from file %s offs %zu "
804 "[reference 0x%x (%c) != slave 0x%x (%c)]",
805 cur_file, line, check_file, offs,
806 ref, (ref >= ' ') ? ref : '-',
807 data, (data >= ' ') ? data : '-');
814 if (saved_output.count > 0)
815 excess(cur_file, line, __func__, " from slave",
816 &saved_output.data[saved_output.readp], saved_output.count);
818 /* discard any excess saved output if required */
820 saved_output.count = 0;
821 saved_output.readp = 0;
824 if ((result = poll(&fds[0], 2, 0)) != 0) {
826 err(2, "poll of file descriptors failed");
828 if ((fds[1].revents & POLLIN) == POLLIN) {
829 save_slave_output(true);
830 } else if ((fds[0].revents & POLLIN) == POLLIN) {
832 * handle excess in file if it exists. Poll
833 * says there is data until EOF is read.
834 * Check next read is EOF, if it is not then
835 * the file really has more data than the
836 * slave produced so flag this as a warning.
838 result = read(check_fd, drain, sizeof(drain));
840 err(1, "read of data file failed");
843 excess(check_file, 0, __func__, "", drain,
853 * Pass a function call and arguments to the slave and wait for the
854 * results. The variable nresults determines how many returns we expect
855 * back from the slave. These results will be validated against the
856 * expected returns or assigned to variables.
859 do_function_call(size_t nresults)
861 #define MAX_RESULTS 4
865 struct pollfd fds[3];
866 returns_t response[MAX_RESULTS], returns_count;
867 assert(nresults <= MAX_RESULTS);
869 do_input = check_function_table(command.function, input_functions,
872 write_func_and_args();
875 * We should get the number of returns back here, grab it before
876 * doing input otherwise it will confuse the input poll
878 read_cmd_pipe(&returns_count);
879 if (returns_count.return_type != ret_count)
880 err(2, "expected return type of ret_count but received %s",
881 returns_enum_names[returns_count.return_type]);
883 perform_delay(&delay_post_call); /* let slave catch up */
886 fprintf(stderr, "Expect %zu results from slave, slave "
887 "reported %zu\n", nresults, returns_count.return_len);
890 if ((no_input == false) && (do_input == 1)) {
892 fprintf(stderr, "doing input with inputstr >%s<\n",
896 if (input_str == NULL)
897 errx(2, "%s, %zu: Call to input function "
898 "but no input defined", cur_file, line);
900 fds[0].fd = slvpipe[READ_PIPE];
901 fds[0].events = POLLIN;
903 fds[1].events = POLLOUT;
905 save_slave_output(false);
907 perform_delay(&delay_spec);
909 if (poll(fds, 2, 0) < 0)
910 err(2, "poll failed");
911 if (fds[0].revents & POLLIN) {
912 warnx("%s, %zu: Slave function "
913 "returned before end of input string",
917 if ((fds[1].revents & POLLOUT) == 0)
920 fprintf(stderr, "Writing char >%c< to slave\n",
923 if (write(master, p, 1) != 1) {
924 warn("%s, %zu: Slave function write error",
931 save_slave_output(false);
934 fprintf(stderr, "Input done.\n");
937 /* done with the input string, free the resources */
943 fds[0].fd = slvpipe[READ_PIPE];
944 fds[0].events = POLLIN;
946 fds[1].fd = slvpipe[WRITE_PIPE];
947 fds[1].events = POLLOUT;
950 fds[2].events = POLLIN | POLLOUT;
952 i = poll(&fds[0], 3, 1000);
953 fprintf(stderr, "Poll returned %zu\n", i);
954 for (i = 0; i < 3; i++) {
955 fprintf(stderr, "revents for fd[%zu] = 0x%x\n",
960 /* drain any trailing output */
961 save_slave_output(false);
963 for (i = 0; i < returns_count.return_len; i++) {
964 read_cmd_pipe(&response[i]);
968 * Check for a slave error in the first return slot, if the
969 * slave errored then we may not have the number of returns we
970 * expect but in this case we should report the slave error
971 * instead of a return count mismatch.
973 if ((returns_count.return_len > 0) &&
974 (response[0].return_type == ret_slave_error))
975 err(2, "Slave returned error: %s",
976 (const char *)response[0].return_value);
978 if (returns_count.return_len != nresults)
979 err(2, "Incorrect number of returns from slave, expected %zu "
980 "but received %zu", nresults, returns_count.return_len);
983 for (i = 0; i < nresults; i++) {
984 if ((response[i].return_type != ret_byte) &&
985 (response[i].return_type != ret_err) &&
986 (response[i].return_type != ret_ok))
988 "received response >%s< "
990 (const char *)response[i].return_value);
992 fprintf(stderr, "received");
994 fprintf(stderr, " return_type %s\n",
995 returns_enum_names[command.returns[i].return_type]);
999 for (i = 0; i < nresults; i++) {
1000 if (command.returns[i].return_type != ret_var) {
1001 validate(i, &response[i]);
1003 vars[command.returns[i].return_index].len =
1004 response[i].return_len;
1005 vars[command.returns[i].return_index].value =
1006 response[i].return_value;
1007 vars[command.returns[i].return_index].type =
1008 response[i].return_type;
1012 if (verbose && (saved_output.count > 0))
1013 excess(cur_file, line, __func__, " from slave",
1014 &saved_output.data[saved_output.readp], saved_output.count);
1016 init_parse_variables(0);
1020 * Write the function and command arguments to the command pipe.
1023 write_func_and_args(void)
1028 fprintf(stderr, "calling function >%s<\n", command.function);
1031 write_cmd_pipe(command.function);
1032 for (i = 0; i < command.nargs; i++) {
1033 if (command.args[i].arg_type == arg_var)
1034 write_cmd_pipe_args(command.args[i].arg_type,
1035 &vars[command.args[i].var_index]);
1037 write_cmd_pipe_args(command.args[i].arg_type,
1041 write_cmd_pipe(NULL); /* signal end of arguments */
1045 * Initialise the command structure - if initial is non-zero then just set
1046 * everything to sane values otherwise free any memory that was allocated
1047 * when building the structure.
1050 init_parse_variables(int initial)
1053 struct pollfd slave_pty;
1056 free(command.function);
1057 for (i = 0; i < command.nrets; i++) {
1058 if (command.returns[i].return_type == ret_number)
1059 free(command.returns[i].return_value);
1061 free(command.returns);
1063 for (i = 0; i < command.nargs; i++) {
1064 if (command.args[i].arg_type != arg_var)
1065 free(command.args[i].arg_string);
1074 saved_output.allocated = 0;
1075 saved_output.count = 0;
1076 saved_output.readp = 0;
1077 saved_output.data = NULL;
1081 command.function = NULL;
1083 command.args = NULL;
1085 command.returns = NULL;
1088 * Check the slave pty for stray output from the slave, at this
1089 * point we should not see any data as it should have been
1090 * consumed by the test functions. If we see data then we have
1091 * either a bug or are not handling an output generating function
1094 slave_pty.fd = master;
1095 slave_pty.events = POLLIN;
1096 result = poll(&slave_pty, 1, 0);
1099 err(2, "Poll of slave pty failed");
1100 else if (result > 0)
1101 warnx("%s, %zu: Unexpected data from slave", cur_file, line);
1105 * Validate the response against the expected return. The variable
1106 * i is the i into the rets array in command.
1109 validate(int i, void *data)
1112 returns_t *byte_response;
1114 byte_response = data;
1115 if ((command.returns[i].return_type != ret_byte) &&
1116 (command.returns[i].return_type != ret_err) &&
1117 (command.returns[i].return_type != ret_ok)) {
1118 if ((byte_response->return_type == ret_byte) ||
1119 (byte_response->return_type == ret_err) ||
1120 (byte_response->return_type == ret_ok))
1121 err(1, "%s: expecting type %s, received type %s"
1122 " at line %zu of file %s", __func__,
1123 returns_enum_names[command.returns[i].return_type],
1124 returns_enum_names[byte_response->return_type],
1127 response = byte_response->return_value;
1130 switch (command.returns[i].return_type) {
1132 validate_type(ret_err, byte_response, 0);
1136 validate_type(ret_ok, byte_response, 0);
1140 validate_return("NULL", response, 0);
1144 validate_return("NULL", response, 1);
1149 validate_return(command.returns[i].return_value,
1154 validate_reference(i, response);
1158 validate_byte(&command.returns[i], byte_response, 0);
1162 err(1, "Malformed statement at line %zu of file %s",
1169 * Validate the return against the contents of a variable.
1172 validate_reference(int i, void *data)
1175 returns_t *byte_response;
1178 varp = &vars[command.returns[i].return_index];
1180 byte_response = data;
1181 if (command.returns[i].return_type != ret_byte)
1186 "%s: return type of %s, value %s \n", __func__,
1187 returns_enum_names[varp->type],
1188 (const char *)varp->value);
1191 switch (varp->type) {
1194 validate_return(varp->value, response, 0);
1198 validate_byte(varp->value, byte_response, 0);
1203 "Invalid return type for reference at line %zu of file %s",
1210 * Validate the return type against the expected type, throw an error
1211 * if they don't match.
1214 validate_type(returns_enum_t expected, returns_t *value, int check)
1216 if (((check == 0) && (expected != value->return_type)) ||
1217 ((check == 1) && (expected == value->return_type)))
1218 err(1, "Validate expected type %s %s %s line %zu of file %s",
1219 returns_enum_names[expected],
1220 (check == 0)? "matching" : "not matching",
1221 returns_enum_names[value->return_type], line, cur_file);
1224 fprintf(stderr, "Validate expected type %s %s %s line %zu"
1226 returns_enum_names[expected],
1227 (check == 0)? "matching" : "not matching",
1228 returns_enum_names[value->return_type], line, cur_file);
1233 * Validate the return value against the expected value, throw an error
1234 * if they don't match.
1237 validate_return(const char *expected, const char *value, int check)
1239 if (((check == 0) && strcmp(expected, value) != 0) ||
1240 ((check == 1) && strcmp(expected, value) == 0))
1241 errx(1, "Validate expected %s %s %s line %zu of file %s",
1243 (check == 0)? "matching" : "not matching", value,
1246 fprintf(stderr, "Validated expected value %s %s %s "
1247 "at line %zu of file %s\n", expected,
1248 (check == 0)? "matches" : "does not match",
1249 value, line, cur_file);
1254 * Validate the return value against the expected value, throw an error
1255 * if they don't match expectations.
1258 validate_byte(returns_t *expected, returns_t *value, int check)
1264 ch = value->return_value;
1265 fprintf(stderr, "checking returned byte stream: ");
1266 for (i = 0; i < value->return_len; i++)
1267 fprintf(stderr, "%s0x%x", (i != 0)? ", " : "", ch[i]);
1268 fprintf(stderr, "\n");
1270 fprintf(stderr, "%s byte stream: ",
1271 (check == 0)? "matches" : "does not match");
1272 ch = (char *) expected->return_value;
1273 for (i = 0; i < expected->return_len; i++)
1274 fprintf(stderr, "%s0x%x", (i != 0)? ", " : "", ch[i]);
1275 fprintf(stderr, "\n");
1279 * No chance of a match if lengths differ...
1281 if ((check == 0) && (expected->return_len != value->return_len))
1282 errx(1, "Byte validation failed, length mismatch, expected %zu,"
1283 "received %zu", expected->return_len, value->return_len);
1286 * If check is 0 then we want to throw an error IFF the byte streams
1287 * do not match, if check is 1 then throw an error if the byte
1290 if (((check == 0) && memcmp(expected->return_value, value->return_value,
1291 value->return_len) != 0) ||
1292 ((check == 1) && (expected->return_len == value->return_len) &&
1293 memcmp(expected->return_value, value->return_value,
1294 value->return_len) == 0))
1295 errx(1, "Validate expected %s byte stream at line %zu"
1297 (check == 0)? "matching" : "not matching", line, cur_file);
1299 fprintf(stderr, "Validated expected %s byte stream "
1300 "at line %zu of file %s\n",
1301 (check == 0)? "matching" : "not matching",
1307 * Validate the variable at i against the expected value, throw an
1308 * error if they don't match, if check is non-zero then the match is
1312 validate_variable(int ret, returns_enum_t type, const void *value, int i,
1318 retval = &command.returns[ret];
1319 varptr = &vars[command.returns[ret].return_index];
1321 if (varptr->value == NULL)
1322 err(1, "Variable %s has no value assigned to it", varptr->name);
1325 if (varptr->type != type)
1326 err(1, "Variable %s is not the expected type", varptr->name);
1328 if (type != ret_byte) {
1329 if ((((check == 0) && strcmp(value, varptr->value) != 0))
1330 || ((check == 1) && strcmp(value, varptr->value) == 0))
1331 err(1, "Variable %s contains %s instead of %s"
1332 " value %s at line %zu of file %s",
1333 varptr->name, (const char *)varptr->value,
1334 (check == 0)? "expected" : "not matching",
1335 (const char *)value,
1338 fprintf(stderr, "Variable %s contains %s value "
1339 "%s at line %zu of file %s\n",
1341 (check == 0)? "expected" : "not matching",
1342 (const char *)varptr->value, line, cur_file);
1345 if ((check == 0) && (retval->return_len != varptr->len))
1346 err(1, "Byte validation failed, length mismatch");
1349 * If check is 0 then we want to throw an error IFF
1350 * the byte streams do not match, if check is 1 then
1351 * throw an error if the byte streams match.
1353 if (((check == 0) && memcmp(retval->return_value, varptr->value,
1354 varptr->len) != 0) ||
1355 ((check == 1) && (retval->return_len == varptr->len) &&
1356 memcmp(retval->return_value, varptr->value,
1358 err(1, "Validate expected %s byte stream at line %zu"
1360 (check == 0)? "matching" : "not matching",
1363 fprintf(stderr, "Validated expected %s byte stream "
1364 "at line %zu of file %s\n",
1365 (check == 0)? "matching" : "not matching",
1372 * Write a string to the command pipe - we feed the number of bytes coming
1373 * down first to allow storage allocation and then follow up with the data.
1374 * If cmd is NULL then feed a -1 down the pipe to say the end of the args.
1377 write_cmd_pipe(char *cmd)
1387 arg.arg_type = arg_static;
1389 arg.arg_string = cmd;
1390 write_cmd_pipe_args(arg.arg_type, &arg);
1395 write_cmd_pipe_args(args_state_t type, void *data)
1406 len = var_data->len;
1407 cmd = var_data->value;
1408 if (type == arg_byte)
1409 send_type = ret_byte;
1411 send_type = ret_string;
1415 send_type = ret_null;
1420 if ((arg_data->arg_len == 0) && (arg_data->arg_string == NULL))
1423 len = arg_data->arg_len;
1424 cmd = arg_data->arg_string;
1425 if (type == arg_byte)
1426 send_type = ret_byte;
1428 send_type = ret_string;
1432 fprintf(stderr, "Writing type %s to command pipe\n",
1433 returns_enum_names[send_type]);
1436 if (write(cmdpipe[WRITE_PIPE], &send_type, sizeof(int)) < 0)
1437 err(1, "command pipe write for type failed");
1440 fprintf(stderr, "Writing length %d to command pipe\n", len);
1443 if (write(cmdpipe[WRITE_PIPE], &len, sizeof(int)) < 0)
1444 err(1, "command pipe write for length failed");
1448 fprintf(stderr, "Writing data >%s< to command pipe\n",
1451 if (write(cmdpipe[WRITE_PIPE], cmd, len) < 0)
1452 err(1, "command pipe write of data failed");
1457 * Read a response from the command pipe, first we will receive the
1458 * length of the response then the actual data.
1461 read_cmd_pipe(returns_t *response)
1464 struct pollfd rfd[2];
1468 * Check if there is data to read - just in case slave has died, we
1469 * don't want to block on the read and just hang. We also check
1470 * output from the slave because the slave may be blocked waiting
1471 * for a flush on its stdout.
1473 rfd[0].fd = slvpipe[READ_PIPE];
1474 rfd[0].events = POLLIN;
1476 rfd[1].events = POLLIN;
1479 if (poll(rfd, 2, 4000) == 0)
1480 errx(2, "%s, %zu: Command pipe read timeout",
1483 if ((rfd[1].revents & POLLIN) == POLLIN) {
1486 "draining output from slave\n");
1488 save_slave_output(false);
1491 while((rfd[1].revents & POLLIN) == POLLIN);
1493 if (read(slvpipe[READ_PIPE], &type, sizeof(int)) < 0)
1494 err(1, "command pipe read for type failed");
1495 response->return_type = type;
1497 if ((type != ret_ok) && (type != ret_err) && (type != ret_count)) {
1498 if (read(slvpipe[READ_PIPE], &len, sizeof(int)) < 0)
1499 err(1, "command pipe read for length failed");
1500 response->return_len = len;
1504 "Reading %d bytes from command pipe\n", len);
1507 if ((response->return_value = malloc(len + 1)) == NULL)
1508 err(1, "Failed to alloc memory for cmd pipe read");
1510 if (read(slvpipe[READ_PIPE], response->return_value, len) < 0)
1511 err(1, "command pipe read of data failed");
1513 if (response->return_type != ret_byte) {
1514 str = response->return_value;
1518 fprintf(stderr, "Read data >%s< from pipe\n",
1519 (const char *)response->return_value);
1523 response->return_value = NULL;
1524 if (type == ret_count) {
1525 if (read(slvpipe[READ_PIPE], &len, sizeof(int)) < 0)
1526 err(1, "command pipe read for number of "
1528 response->return_len = len;
1532 fprintf(stderr, "Read type %s from pipe\n",
1533 returns_enum_names[type]);
1539 * Check for writes from the slave on the pty, save the output into a
1540 * buffer for later checking if discard is false.
1542 #define MAX_DRAIN 256
1545 save_slave_output(bool discard)
1547 char *new_data, drain[MAX_DRAIN];
1555 err(2, "poll of slave pty failed");
1557 if ((result = read(master, drain, result)) < 0) {
1558 if (errno == EAGAIN)
1561 err(2, "draining slave pty failed");
1567 if ((size_t)result >
1568 (saved_output.allocated - saved_output.count)) {
1569 to_allocate = 1024 * ((result / 1024) + 1);
1571 if ((new_data = realloc(saved_output.data,
1572 saved_output.allocated + to_allocate))
1574 err(2, "Realloc of saved_output failed");
1575 saved_output.data = new_data;
1576 saved_output.allocated += to_allocate;
1580 fprintf(stderr, "count = %zu, "
1581 "allocated = %zu\n", saved_output.count,
1582 saved_output.allocated);
1583 for (i = 0; i < (size_t)result; i++) {
1584 fprintf(stderr, "Saving slave output "
1585 "at %zu: 0x%x (%c)\n",
1586 saved_output.count + i, drain[i],
1587 (drain[i] >= ' ')? drain[i] : '-');
1591 memcpy(&saved_output.data[saved_output.count], drain,
1593 saved_output.count += result;
1596 fprintf(stderr, "count = %zu, "
1597 "allocated = %zu\n", saved_output.count,
1598 saved_output.allocated);
1602 for (i = 0; i < (size_t)result; i++) {
1603 fprintf(stderr, "Discarding slave "
1604 "output 0x%x (%c)\n",
1606 (drain[i] >= ' ')? drain[i] : '-');
1614 yyerror(const char *msg)
1616 warnx("%s in line %zu of file %s", msg, line, cur_file);