Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/make usr.bin/make: refactor brk_string



details:   https://anonhg.NetBSD.org/src/rev/7237b9d680cd
branches:  trunk
changeset: 971755:7237b9d680cd
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun May 03 12:10:28 2020 +0000

description:
usr.bin/make: refactor brk_string

The variables are renamed to reflect to which memory region each pointer
belongs.

The variable "curlen" was always zero.

The type of "ch" has changed to char, and its scope is now limited to
its actual use.

Comparisons of pointers are now consistently written as p != NULL
instead of !p, like character comparisons are written as ch != '\0'.

The "store_words_buf" is updated when the function returns, not before.

diffstat:

 usr.bin/make/str.c |  119 +++++++++++++++++++++++++++-------------------------
 1 files changed, 61 insertions(+), 58 deletions(-)

diffs (216 lines):

diff -r 63925b0c9af8 -r 7237b9d680cd usr.bin/make/str.c
--- a/usr.bin/make/str.c        Sun May 03 10:06:29 2020 +0000
+++ b/usr.bin/make/str.c        Sun May 03 12:10:28 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: str.c,v 1.40 2020/04/25 18:20:57 christos Exp $        */
+/*     $NetBSD: str.c,v 1.41 2020/05/03 12:10:28 rillig Exp $  */
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: str.c,v 1.40 2020/04/25 18:20:57 christos Exp $";
+static char rcsid[] = "$NetBSD: str.c,v 1.41 2020/05/03 12:10:28 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char     sccsid[] = "@(#)str.c  5.8 (Berkeley) 6/1/90";
 #else
-__RCSID("$NetBSD: str.c,v 1.40 2020/04/25 18:20:57 christos Exp $");
+__RCSID("$NetBSD: str.c,v 1.41 2020/05/03 12:10:28 rillig Exp $");
 #endif
 #endif                         /* not lint */
 #endif
@@ -133,42 +133,43 @@
  *
  * returns --
  *     Pointer to the array of pointers to the words.
- *      Memory containing the actual words in *buffer.
+ *      Memory containing the actual words in *store_words_buf.
  *             Both of these must be free'd by the caller.
- *      Number of words in *store_argc.
+ *      Number of words in *store_words_len.
  */
 char **
-brk_string(const char *str, int *store_argc, Boolean expand, char **buffer)
+brk_string(const char *str, int *store_words_len, Boolean expand,
+       char **store_words_buf)
 {
-       int argc, ch;
-       char inquote, *start, *t;
-       const char *p;
-       int len;
-       int argmax = 50, curlen = 0;
-       char **argv;
+       char inquote;
+       const char *str_p;
+       size_t str_len;
+       char **words;
+       int words_len;
+       int words_cap = 50;
+       char *words_buf, *word_start, *word_end;
 
        /* skip leading space chars. */
        for (; *str == ' ' || *str == '\t'; ++str)
                continue;
 
-       /* allocate room for a copy of the string */
-       if ((len = strlen(str) + 1) > curlen)
-               *buffer = bmake_malloc(curlen = len);
+       /* words_buf holds the words, separated by '\0'. */
+       str_len = strlen(str);
+       words_buf = bmake_malloc(strlen(str) + 1);
 
-       /*
-        * initial argmax based on len
-        */
-       argmax = MAX((len / 5), 50);
-       argv = bmake_malloc((argmax + 1) * sizeof(char *));
+       words_cap = MAX((str_len / 5), 50);
+       words = bmake_malloc((words_cap + 1) * sizeof(char *));
 
        /*
         * copy the string; at the same time, parse backslashes,
-        * quotes and build the argument list.
+        * quotes and build the word list.
         */
-       argc = 0;
+       words_len = 0;
        inquote = '\0';
-       for (p = str, start = t = *buffer;; ++p) {
-               switch(ch = *p) {
+       word_start = word_end = words_buf;
+       for (str_p = str;; ++str_p) {
+               char ch = *str_p;
+               switch(ch) {
                case '"':
                case '\'':
                        if (inquote) {
@@ -180,21 +181,21 @@
                        else {
                                inquote = (char) ch;
                                /* Don't miss "" or '' */
-                               if (start == NULL && p[1] == inquote) {
+                               if (word_start == NULL && str_p[1] == inquote) {
                                        if (!expand) {
-                                               start = t;
-                                               *t++ = ch;
+                                               word_start = word_end;
+                                               *word_end++ = ch;
                                        } else
-                                               start = t + 1;
-                                       p++;
+                                               word_start = word_end + 1;
+                                       str_p++;
                                        inquote = '\0';
                                        break;
                                }
                        }
                        if (!expand) {
-                               if (!start)
-                                       start = t;
-                               *t++ = ch;
+                               if (word_start == NULL)
+                                       word_start = word_end;
+                               *word_end++ = ch;
                        }
                        continue;
                case ' ':
@@ -202,30 +203,30 @@
                case '\n':
                        if (inquote)
                                break;
-                       if (!start)
+                       if (word_start == NULL)
                                continue;
                        /* FALLTHROUGH */
                case '\0':
                        /*
-                        * end of a token -- make sure there's enough argv
+                        * end of a token -- make sure there's enough words
                         * space and save off a pointer.
                         */
-                       if (!start)
+                       if (word_start == NULL)
                            goto done;
 
-                       *t++ = '\0';
-                       if (argc == argmax) {
-                               argmax *= 2;            /* ramp up fast */
-                               argv = (char **)bmake_realloc(argv,
-                                   (argmax + 1) * sizeof(char *));
+                       *word_end++ = '\0';
+                       if (words_len == words_cap) {
+                               words_cap *= 2;         /* ramp up fast */
+                               words = (char **)bmake_realloc(words,
+                                   (words_cap + 1) * sizeof(char *));
                        }
-                       argv[argc++] = start;
-                       start = NULL;
+                       words[words_len++] = word_start;
+                       word_start = NULL;
                        if (ch == '\n' || ch == '\0') {
                                if (expand && inquote) {
-                                       free(argv);
-                                       free(*buffer);
-                                       *buffer = NULL;
+                                       free(words);
+                                       free(words_buf);
+                                       *store_words_buf = NULL;
                                        return NULL;
                                }
                                goto done;
@@ -233,21 +234,22 @@
                        continue;
                case '\\':
                        if (!expand) {
-                               if (!start)
-                                       start = t;
-                               *t++ = '\\';
-                               if (*(p+1) == '\0') /* catch '\' at end of line */
+                               if (word_start == NULL)
+                                       word_start = word_end;
+                               *word_end++ = '\\';
+                               /* catch '\' at end of line */
+                               if (str_p[1] == '\0')
                                        continue;
-                               ch = *++p;
+                               ch = *++str_p;
                                break;
                        }
 
-                       switch (ch = *++p) {
+                       switch (ch = *++str_p) {
                        case '\0':
                        case '\n':
                                /* hmmm; fix it up as best we can */
                                ch = '\\';
-                               --p;
+                               --str_p;
                                break;
                        case 'b':
                                ch = '\b';
@@ -267,13 +269,14 @@
                        }
                        break;
                }
-               if (!start)
-                       start = t;
-               *t++ = (char) ch;
+               if (word_start == NULL)
+                       word_start = word_end;
+               *word_end++ = ch;
        }
-done:  argv[argc] = NULL;
-       *store_argc = argc;
-       return(argv);
+done:  words[words_len] = NULL;
+       *store_words_len = words_len;
+       *store_words_buf = words_buf;
+       return words;
 }
 
 /*



Home | Main Index | Thread Index | Old Index