Module Name: src Committed By: rillig Date: Sat Feb 13 10:03:49 UTC 2021
Modified Files: src/tests/lib/libcurses/slave: slave.c Log Message: 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. To generate a diff of this commit: cvs rdiff -u -r1.15 -r1.16 src/tests/lib/libcurses/slave/slave.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/lib/libcurses/slave/slave.c diff -u src/tests/lib/libcurses/slave/slave.c:1.15 src/tests/lib/libcurses/slave/slave.c:1.16 --- src/tests/lib/libcurses/slave/slave.c:1.15 Sat Feb 13 09:28:27 2021 +++ src/tests/lib/libcurses/slave/slave.c Sat Feb 13 10:03:49 2021 @@ -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 <bl...@netbsd.org> @@ -76,6 +76,50 @@ read_from_director(void *data, size_t n) 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 @@ read_from_director(void *data, size_t n) 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 @@ process_commands(void) 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);