Source-Changes-HG archive

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

[src/trunk]: src/tests/lib/libcurses/slave tests/libcurses: extract read_comm...



details:   https://anonhg.NetBSD.org/src/rev/0a3764279ee4
branches:  trunk
changeset: 980643:0a3764279ee4
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Feb 13 10:03:49 2021 +0000

description:
tests/libcurses: extract read_command_argument from process_commands

Having code indented so far to the right that each word gets its own
line is ridiculous.  Fix that.

While here, remove the cargo-cult realloc pattern, which is not needed
if the process exits immediately on error.

While here, reduce the indentation of the code by returning early.

No functional change.

diffstat:

 tests/lib/libcurses/slave/slave.c |  100 ++++++++++++++++++-------------------
 1 files changed, 49 insertions(+), 51 deletions(-)

diffs (128 lines):

diff -r c76816bf4ca8 -r 0a3764279ee4 tests/lib/libcurses/slave/slave.c
--- a/tests/lib/libcurses/slave/slave.c Sat Feb 13 09:28:27 2021 +0000
+++ b/tests/lib/libcurses/slave/slave.c Sat Feb 13 10:03:49 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: slave.c,v 1.15 2021/02/13 09:28:27 rillig Exp $        */
+/*     $NetBSD: slave.c,v 1.16 2021/02/13 10:03:49 rillig Exp $        */
 
 /*-
  * Copyright 2009 Brett Lymn <blymn%NetBSD.org@localhost>
@@ -76,6 +76,50 @@
                    n, (size_t)nread);
 }
 
+static bool
+read_command_argument(char ***pargs, int argslen)
+{
+       int type, len;
+       char **args = *pargs;
+
+       read_from_director(&type, sizeof type);
+       read_from_director(&len, sizeof len);
+       if (len < 0)
+               return false;
+
+       args = realloc(args, (argslen + 1) * sizeof args[0]);
+       if (args == NULL)
+               err(1, "slave realloc of args array failed");
+       *pargs = args;
+
+       if (type != data_null) {
+               args[argslen] = malloc(len + 1);
+
+               if (args[argslen] == NULL)
+                       err(1, "slave alloc of %d bytes for args failed", len);
+       }
+
+       if (len == 0) {
+               if (type == data_null)
+                       args[argslen] = NULL;
+               else
+                       args[argslen][0] = '\0';
+       } else {
+               read_from_director(args[argslen], len);
+               if (type != data_byte)
+                       args[argslen][len] = '\0';
+
+               if (len == 6 && strcmp(args[argslen], "STDSCR") == 0) {
+                       char *stdscr_buf;
+                       if (asprintf(&stdscr_buf, "%p", stdscr) < 0)
+                               err(2, "asprintf of stdscr failed");
+                       free(args[argslen]);
+                       args[argslen] = stdscr_buf;
+               }
+       }
+       return true;
+}
+
 /*
  * Read the command pipe for the function to execute, gather the args
  * and then process the command.
@@ -83,8 +127,8 @@
 static void
 process_commands(void)
 {
-       int len, maxlen, argslen, i, ret, type;
-       char *cmdbuf, *tmpbuf, **args, **tmpargs;
+       int len, maxlen, argslen, i, type;
+       char *cmdbuf, *tmpbuf, **args;
 
        len = maxlen = 30;
        if ((cmdbuf = malloc(maxlen)) == NULL)
@@ -109,54 +153,8 @@
                argslen = 0;
                args = NULL;
 
-               do {
-                       read_from_director(&type, sizeof type);
-                       read_from_director(&len, sizeof len);
-
-                       if (len >= 0) {
-                               tmpargs = realloc(args,
-                                   (argslen + 1) * sizeof(char *));
-                               if (tmpargs == NULL)
-                                       err(1, "slave realloc of args array "
-                                           "failed");
-
-                               args = tmpargs;
-                               if (type != data_null) {
-                                       args[argslen] = malloc(len + 1);
-
-                                       if (args[argslen] == NULL)
-                                               err(1, "slave alloc of %d bytes"
-                                                   " for args failed", len);
-                               }
-
-                               if (len == 0) {
-                                       if (type == data_null)
-                                               args[argslen] = NULL;
-                                       else
-                                               args[argslen][0] = '\0';
-                               } else {
-                                       read_from_director(args[argslen], len);
-                                       if (type != data_byte)
-                                               args[argslen][len] = '\0';
-
-                                       if (len == 6) {
-                                               if (strcmp(args[argslen],
-                                                          "STDSCR") == 0) {
-                                                       ret = asprintf(&tmpbuf,
-                                                                "%p",
-                                                                stdscr);
-                                                       if (ret < 0)
-                                                               err(2,
-                                                                   "asprintf of stdscr failed");
-                                                       free(args[argslen]);
-                                                       args[argslen] = tmpbuf;
-                                               }
-                                       }
-                               }
-
-                               argslen++;
-                       }
-               } while (len >= 0);
+               while (read_command_argument(&args, argslen))
+                       argslen++;
 
                command_execute(cmdbuf, argslen, args);
 



Home | Main Index | Thread Index | Old Index