Module Name:    src
Committed By:   rillig
Date:           Sun May  3 12:10:28 UTC 2020

Modified Files:
        src/usr.bin/make: str.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/usr.bin/make/str.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/str.c
diff -u src/usr.bin/make/str.c:1.40 src/usr.bin/make/str.c:1.41
--- src/usr.bin/make/str.c:1.40	Sat Apr 25 18:20:57 2020
+++ src/usr.bin/make/str.c	Sun May  3 12:10:28 2020
@@ -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 @@ str_concat(const char *s1, const char *s
  *
  * 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 @@ brk_string(const char *str, int *store_a
 			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 @@ brk_string(const char *str, int *store_a
 		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 @@ brk_string(const char *str, int *store_a
 			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 @@ brk_string(const char *str, int *store_a
 			}
 			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;
 }
 
 /*

Reply via email to