Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libcurses/director - err -> errx mistakes



details:   https://anonhg.NetBSD.org/src/rev/7f5be4238450
branches:  trunk
changeset: 768944:7f5be4238450
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Aug 29 12:49:37 2011 +0000

description:
- err -> errx mistakes
- make sure that -v does not change the program behavior
- don't set revents in poll
- add more debugging
- only call read when we have POLLIN
- don't mix I/O from master while processing input

diffstat:

 tests/lib/libcurses/director/testlang_parse.y |  259 ++++++++++++++-----------
 1 files changed, 140 insertions(+), 119 deletions(-)

diffs (truncated from 623 to 300 lines):

diff -r 053a2fab2152 -r 7f5be4238450 tests/lib/libcurses/director/testlang_parse.y
--- a/tests/lib/libcurses/director/testlang_parse.y     Mon Aug 29 12:46:58 2011 +0000
+++ b/tests/lib/libcurses/director/testlang_parse.y     Mon Aug 29 12:49:37 2011 +0000
@@ -1,5 +1,5 @@
 %{
-/*     $NetBSD: testlang_parse.y,v 1.8 2011/08/18 02:44:45 christos Exp $      */
+/*     $NetBSD: testlang_parse.y,v 1.9 2011/08/29 12:49:37 christos Exp $      */
 
 /*-
  * Copyright 2009 Brett Lymn <blymn%NetBSD.org@localhost>
@@ -41,6 +41,7 @@
 #include <sys/syslimits.h>
 #include <time.h>
 #include <vis.h>
+#include <stdint.h>
 #include "returns.h"
 
 #define YYDEBUG 1
@@ -226,16 +227,18 @@
                err(1, "Undefined variable in check statement, line %zu"
                    " of file %s", line, cur_file);
 
-       if (verbose)
+       if (verbose) {
                fprintf(stderr, "Checking contents of variable %s for %s\n",
                    vars[command.returns[0].return_index].name,
                    returns_enum_names[command.returns[1].return_type]);
+       }
 
        if (((command.returns[1].return_type == ret_byte) &&
             (vars[command.returns[0].return_index].type != ret_byte)) ||
            vars[command.returns[0].return_index].type != ret_string)
                err(1, "Var type %s (%d) does not match return type %s (%d)",
-                   returns_enum_names[vars[command.returns[0].return_index].type],
+                   returns_enum_names[
+                   vars[command.returns[0].return_index].type],
                    vars[command.returns[0].return_index].type,
                    returns_enum_names[command.returns[1].return_type],
                    command.returns[1].return_type);
@@ -263,11 +266,12 @@
 
        case ret_string:
        case ret_number:
-               if (verbose)
+               if (verbose) {
                        fprintf(stderr, " %s == returned %s\n",
                            (const char *)command.returns[1].return_value,
                            (const char *)
                            vars[command.returns[0].return_index].value);
+               }
                validate_variable(0, ret_string,
                    command.returns[1].return_value,
                    command.returns[0].return_index, 0);
@@ -296,8 +300,9 @@
        if (sscanf($2, "%d", &input_delay) == 0)
                err(1, "delay specification %s could not be converted to "
                    "numeric at line %zu of file %s", $2, line, cur_file);
-       if (verbose)
+       if (verbose) {
                fprintf(stderr, "Set input delay to %d ms\n", input_delay);
+       }
 
        if (input_delay < DELAY_MIN)
                input_delay = 1000 * DELAY_MIN; /* ms to ns */
@@ -308,6 +313,11 @@
         */
        delay_spec.tv_sec = input_delay / 1000;
        delay_spec.tv_nsec = (input_delay - 1000 * delay_spec.tv_sec) * 1000;
+       if (verbose) {
+               fprintf(stderr, "set delay to %jd.%jd\n",
+                   (intmax_t)delay_spec.tv_sec, 
+                   (intmax_t)delay_spec.tv_nsec);
+       }
 
        init_parse_variables(0);
  }
@@ -464,10 +474,11 @@
        result = i1 | i2;
        asprintf(&ret, "%lu", result);
 
-       if (verbose)
+       if (verbose) {
                fprintf(stderr, "numeric or of 0x%lx (%s) and 0x%lx (%s)"
                    " results in 0x%lx (%s)\n",
                    i1, n1, i2, n2, result, ret);
+       }
 
        return ret;
 }
@@ -518,8 +529,7 @@
        if ((name = malloc(strlen(varname) + 1)) == NULL)
                err(1, "Alloc of varname failed");
 
-       if ((temp = (var_t *) realloc(vars, sizeof(var_t) * (nvars + 1)))
-           == NULL) {
+       if ((temp = realloc(vars, sizeof(*temp) * (nvars + 1))) == NULL) {
                free(name);
                err(1, "Realloc of vars array failed");
        }
@@ -544,9 +554,10 @@
        char *str = arg;
        returns_t *ret;
 
-       if (verbose)
-               printf("function is >%s<, adding arg >%s< type %s\n",
+       if (verbose) {
+               fprintf(stderr, "function is >%s<, adding arg >%s< type %s\n",
                       command.function, str, args_enum_names[arg_type]);
+       }
 
        cur.arg_type = arg_type;
        switch (arg_type) {
@@ -580,7 +591,7 @@
                strcpy(cur.arg_string, arg);
        }
 
-       temp = realloc(command.args, sizeof(args_t) * (command.nargs + 1));
+       temp = realloc(command.args, sizeof(*temp) * (command.nargs + 1));
        if (temp == NULL)
                err(1, "Failed to reallocate args");
        command.args = temp;
@@ -627,8 +638,7 @@
                        cur.return_index = assign_var(ret);
        }
 
-       temp = realloc(command.returns,
-                      sizeof(returns_t) * (command.nrets + 1));
+       temp = realloc(command.returns, sizeof(*temp) * (command.nrets + 1));
        if (temp == NULL)
                err(1, "Failed to reallocate returns");
        command.returns = temp;
@@ -687,6 +697,7 @@
        struct pollfd fds[2];
        int nfd, check_fd;
        ssize_t result;
+       size_t offs;
 
        /*
         * Don't prepend check path iff check file has an absolute
@@ -714,10 +725,8 @@
 
        fds[0].fd = check_fd;
        fds[0].events = POLLIN;
-       fds[0].revents = 0;
        fds[1].fd = master;
        fds[1].events = POLLIN;
-       fds[1].revents = 0;
 
        nfd = 2;
        /*
@@ -727,12 +736,16 @@
        if (saved_output.count > 0)
                nfd = 1;
 
-       while (poll(&fds[0], nfd, 500) == nfd) {
-               if ((result = read(check_fd, &ref, 1)) < 1) {
-                       if (result != 0) {
-                               err(2, "Bad read on file %s", check_file);
-                       } else {
-                               break;
+       offs = 0;
+       while (poll(fds, nfd, 500) == nfd) {
+               if (fds[0].revents & POLLIN) {
+                       if ((result = read(check_fd, &ref, 1)) < 1) {
+                               if (result != 0) {
+                                       err(2,
+                                           "Bad read on file %s", check_file);
+                               } else {
+                                       break;
+                               }
                        }
                }
 
@@ -744,28 +757,30 @@
                        if (saved_output.count == 0)
                                nfd = 2;
                } else {
-                       if (read(master, &data, 1) < 1)
-                               err(2, "Bad read on slave pty");
+                       if (fds[0].revents & POLLIN) {
+                               if (read(master, &data, 1) < 1)
+                                       err(2, "Bad read on slave pty");
+                       } else
+                               continue;
                }
 
                if (verbose) {
                        fprintf(stderr, "Comparing reference byte 0x%x (%c)"
-                               " against slave byte 0x%x (%c)\n", ref,
-                               (ref >= ' ')? ref : '-',
-                               data, (data >= ' ')? data : '-');
+                               " against slave byte 0x%x (%c)\n",
+                               ref, (ref >= ' ') ? ref : '-',
+                               data, (data >= ' ' )? data : '-');
                }
 
                if (ref != data) {
-                       warnx("%s, %zu: refresh data from slave does "
-                           "not match expected from file %s",
-                           cur_file, line, check_file);
-
-                       if (!verbose)
-                               exit(2);
+                       errx(2, "%s, %zu: refresh data from slave does "
+                           "not match expected from file %s offs %zu "
+                           "[reference 0x%x (%c) != slave 0x%x (%c)]",
+                           cur_file, line, check_file, offs,
+                           ref, (ref >= ' ') ? ref : '-',
+                           data, (data >= ' ') ? data : '-');
                }
 
-               fds[0].revents = 0;
-               fds[1].revents = 0;
+               offs++;
        }
 
 
@@ -779,15 +794,13 @@
                saved_output.readp = 0;
        }
 
-       fds[0].revents = 0;
-       fds[1].revents = 0;
        if ((result = poll(&fds[0], 2, 0)) != 0) {
                if (result == -1)
                        err(2, "poll of file descriptors failed");
 
                if ((fds[1].revents & POLLIN) == POLLIN) {
                        save_slave_output(true);
-               } else {
+               } else if ((fds[0].revents & POLLIN) == POLLIN) {
                        /*
                         * handle excess in file if it exists.  Poll
                         * says there is data until EOF is read.
@@ -802,8 +815,6 @@
                        if (result > 0) {
                                excess(check_file, 0, __func__, "", drain,
                                    result);
-                               if (!verbose)
-                                       exit(2);
                        }
                }
        }
@@ -843,14 +854,16 @@
                err(2, "expected return type of ret_count but received %s",
                    returns_enum_names[returns_count.return_type]);
 
-       if (verbose)
+       if (verbose) {
                fprintf(stderr, "Expect %zu results from slave, slave "
-                       "reported %zu\n", nresults, returns_count.return_len);
+                   "reported %zu\n", nresults, returns_count.return_len);
+       }
 
        if ((no_input == false) && (do_input == 1)) {
-               if (verbose)
+               if (verbose) {
                        fprintf(stderr, "doing input with inputstr >%s<\n",
-                               input_str);
+                           input_str);
+               }
 
                if (input_str == NULL)
                        errx(2, "%s, %zu: Call to input function "
@@ -858,31 +871,35 @@
 
                fds[0].fd = slvpipe[READ_PIPE];
                fds[0].events = POLLIN;
+               fds[1].fd = master;
+               fds[1].events = POLLOUT;
                p = input_str;
                save_slave_output(false);
                while(*p != '\0') {
-                       if (verbose) {
-                               fprintf(stderr, "Writing char >%c< to slave\n",
-                                       *p);
-                               fflush(stderr);
-                       }
-
                        nanosleep(&delay_spec, NULL);
-
-                       write(master, p, 1);
-                       save_slave_output(false);
-
-                       p++;
-                       fds[0].revents = 0;
-
-                       /* check for slave function returning unexpectedly */
-                       if ((poll(&fds[0], 1, 10) > 0) && (*p != '\0')) {
+                       if (poll(fds, 2, 0) < 0)
+                               err(2, "poll failed");
+                       if (fds[0].revents & POLLIN) {
                                warnx("%s, %zu: Slave function "
                                    "returned before end of input string",
                                    cur_file, line);
                                break;
                        }
+                       if ((fds[1].revents & POLLOUT) == 0)
+                               continue;
+                       if (verbose) {



Home | Main Index | Thread Index | Old Index