Module Name:    src
Committed By:   rillig
Date:           Sun Jul 19 09:26:19 UTC 2020

Modified Files:
        src/usr.bin/make: nonints.h str.c var.c

Log Message:
make(1): move SysV string matching to var.c

This kind of string matching is only used in variable modifiers, and only
if this feature is enabled by SYSVVARSUB.


To generate a diff of this commit:
cvs rdiff -u -r1.78 -r1.79 src/usr.bin/make/nonints.h
cvs rdiff -u -r1.51 -r1.52 src/usr.bin/make/str.c
cvs rdiff -u -r1.255 -r1.256 src/usr.bin/make/var.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/nonints.h
diff -u src/usr.bin/make/nonints.h:1.78 src/usr.bin/make/nonints.h:1.79
--- src/usr.bin/make/nonints.h:1.78	Fri Jul  3 07:40:13 2020
+++ src/usr.bin/make/nonints.h	Sun Jul 19 09:26:18 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: nonints.h,v 1.78 2020/07/03 07:40:13 rillig Exp $	*/
+/*	$NetBSD: nonints.h,v 1.79 2020/07/19 09:26:18 rillig Exp $	*/
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -138,8 +138,6 @@ char *str_concat(const char *, const cha
 char **brk_string(const char *, int *, Boolean, char **);
 char *Str_FindSubstring(const char *, const char *);
 Boolean Str_Match(const char *, const char *);
-char *Str_SYSVMatch(const char *, const char *, size_t *, Boolean *);
-void Str_SYSVSubst(Buffer *, char *, char *, size_t, Boolean);
 
 /* suff.c */
 void Suff_ClearSuffixes(void);

Index: src/usr.bin/make/str.c
diff -u src/usr.bin/make/str.c:1.51 src/usr.bin/make/str.c:1.52
--- src/usr.bin/make/str.c:1.51	Fri Jul  3 07:40:13 2020
+++ src/usr.bin/make/str.c	Sun Jul 19 09:26:18 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: str.c,v 1.51 2020/07/03 07:40:13 rillig Exp $	*/
+/*	$NetBSD: str.c,v 1.52 2020/07/19 09:26:18 rillig Exp $	*/
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: str.c,v 1.51 2020/07/03 07:40:13 rillig Exp $";
+static char rcsid[] = "$NetBSD: str.c,v 1.52 2020/07/19 09:26:18 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.51 2020/07/03 07:40:13 rillig Exp $");
+__RCSID("$NetBSD: str.c,v 1.52 2020/07/19 09:26:18 rillig Exp $");
 #endif
 #endif				/* not lint */
 #endif
@@ -423,107 +423,3 @@ Str_Match(const char *str, const char *p
 		str++;
 	}
 }
-
-/*-
- *-----------------------------------------------------------------------
- * Str_SYSVMatch --
- *	Check word against pattern for a match (% is wild),
- *
- * Input:
- *	word		Word to examine
- *	pattern		Pattern to examine against
- *	len		Number of characters to substitute
- *
- * Results:
- *	Returns the beginning position of a match or null. The number
- *	of characters matched is returned in len.
- *
- * Side Effects:
- *	None
- *
- *-----------------------------------------------------------------------
- */
-char *
-Str_SYSVMatch(const char *word, const char *pattern, size_t *len,
-    Boolean *hasPercent)
-{
-    const char *p = pattern;
-    const char *w = word;
-    const char *m;
-
-    *hasPercent = FALSE;
-    if (*p == '\0') {
-	/* Null pattern is the whole string */
-	*len = strlen(w);
-	return UNCONST(w);
-    }
-
-    if ((m = strchr(p, '%')) != NULL) {
-	*hasPercent = TRUE;
-	if (*w == '\0') {
-		/* empty word does not match pattern */
-		return NULL;
-	}
-	/* check that the prefix matches */
-	for (; p != m && *w && *w == *p; w++, p++)
-	     continue;
-
-	if (p != m)
-	    return NULL;	/* No match */
-
-	if (*++p == '\0') {
-	    /* No more pattern, return the rest of the string */
-	    *len = strlen(w);
-	    return UNCONST(w);
-	}
-    }
-
-    m = w;
-
-    /* Find a matching tail */
-    do
-	if (strcmp(p, w) == 0) {
-	    *len = w - m;
-	    return UNCONST(m);
-	}
-    while (*w++ != '\0');
-
-    return NULL;
-}
-
-
-/*-
- *-----------------------------------------------------------------------
- * Str_SYSVSubst --
- *	Substitute '%' on the pattern with len characters from src.
- *	If the pattern does not contain a '%' prepend len characters
- *	from src.
- *
- * Results:
- *	None
- *
- * Side Effects:
- *	Places result on buf
- *
- *-----------------------------------------------------------------------
- */
-void
-Str_SYSVSubst(Buffer *buf, char *pat, char *src, size_t len,
-    Boolean lhsHasPercent)
-{
-    char *m;
-
-    if ((m = strchr(pat, '%')) != NULL && lhsHasPercent) {
-	/* Copy the prefix */
-	Buf_AddBytes(buf, m - pat, pat);
-	/* skip the % */
-	pat = m + 1;
-    }
-    if (m != NULL || !lhsHasPercent) {
-	/* Copy the pattern */
-	Buf_AddBytes(buf, len, src);
-    }
-
-    /* append the rest */
-    Buf_AddBytes(buf, strlen(pat), pat);
-}

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.255 src/usr.bin/make/var.c:1.256
--- src/usr.bin/make/var.c:1.255	Sat Jul  4 17:41:04 2020
+++ src/usr.bin/make/var.c	Sun Jul 19 09:26:18 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.255 2020/07/04 17:41:04 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.256 2020/07/19 09:26:18 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.255 2020/07/04 17:41:04 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.256 2020/07/19 09:26:18 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.255 2020/07/04 17:41:04 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.256 2020/07/19 09:26:18 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1215,6 +1215,108 @@ VarMatch(GNode *ctx MAKE_ATTR_UNUSED, Va
 }
 
 #ifdef SYSVVARSUB
+/*-
+ *-----------------------------------------------------------------------
+ * Str_SYSVMatch --
+ *	Check word against pattern for a match (% is wild),
+ *
+ * Input:
+ *	word		Word to examine
+ *	pattern		Pattern to examine against
+ *	len		Number of characters to substitute
+ *
+ * Results:
+ *	Returns the beginning position of a match or null. The number
+ *	of characters matched is returned in len.
+ *
+ * Side Effects:
+ *	None
+ *
+ *-----------------------------------------------------------------------
+ */
+static char *
+Str_SYSVMatch(const char *word, const char *pattern, size_t *len,
+    Boolean *hasPercent)
+{
+    const char *p = pattern;
+    const char *w = word;
+    const char *m;
+
+    *hasPercent = FALSE;
+    if (*p == '\0') {
+	/* Null pattern is the whole string */
+	*len = strlen(w);
+	return UNCONST(w);
+    }
+
+    if ((m = strchr(p, '%')) != NULL) {
+	*hasPercent = TRUE;
+	if (*w == '\0') {
+		/* empty word does not match pattern */
+		return NULL;
+	}
+	/* check that the prefix matches */
+	for (; p != m && *w && *w == *p; w++, p++)
+	     continue;
+
+	if (p != m)
+	    return NULL;	/* No match */
+
+	if (*++p == '\0') {
+	    /* No more pattern, return the rest of the string */
+	    *len = strlen(w);
+	    return UNCONST(w);
+	}
+    }
+
+    m = w;
+
+    /* Find a matching tail */
+    do
+	if (strcmp(p, w) == 0) {
+	    *len = w - m;
+	    return UNCONST(m);
+	}
+    while (*w++ != '\0');
+
+    return NULL;
+}
+
+
+/*-
+ *-----------------------------------------------------------------------
+ * Str_SYSVSubst --
+ *	Substitute '%' on the pattern with len characters from src.
+ *	If the pattern does not contain a '%' prepend len characters
+ *	from src.
+ *
+ * Side Effects:
+ *	Places result on buf
+ *
+ *-----------------------------------------------------------------------
+ */
+static void
+Str_SYSVSubst(Buffer *buf, const char *pat, const char *src, size_t len,
+    Boolean lhsHasPercent)
+{
+    const char *m;
+
+    if ((m = strchr(pat, '%')) != NULL && lhsHasPercent) {
+	/* Copy the prefix */
+	Buf_AddBytes(buf, m - pat, pat);
+	/* skip the % */
+	pat = m + 1;
+    }
+    if (m != NULL || !lhsHasPercent) {
+	/* Copy the pattern */
+	Buf_AddBytes(buf, len, src);
+    }
+
+    /* append the rest */
+    Buf_AddBytes(buf, strlen(pat), pat);
+}
+
+
 /* Callback function for VarModify to implement the :%.from=%.to modifier. */
 static Boolean
 VarSYSVMatch(GNode *ctx, Var_Parse_State *vpstate,
@@ -1222,7 +1324,7 @@ VarSYSVMatch(GNode *ctx, Var_Parse_State
 	     void *data)
 {
     size_t len;
-    char *ptr;
+    const char *ptr;
     Boolean hasPercent;
     VarPattern *pat = data;
 

Reply via email to