diff options
author | Marc Espie <espie@cvs.openbsd.org> | 1999-12-09 18:18:25 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 1999-12-09 18:18:25 +0000 |
commit | d9223f1d8df224b1ed3755c095dd99fd859077d2 (patch) | |
tree | 5d804373f1365a45cddaee1468bbaa8c8ae1be74 /usr.bin | |
parent | 046c3d1156d795ac61465e04ca8b44a3c0744870 (diff) |
Introduce `common usage' buf patterns and use them.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/make/buf.h | 12 | ||||
-rw-r--r-- | usr.bin/make/cond.c | 12 | ||||
-rw-r--r-- | usr.bin/make/for.c | 6 | ||||
-rw-r--r-- | usr.bin/make/str.c | 8 | ||||
-rw-r--r-- | usr.bin/make/var.c | 167 |
5 files changed, 91 insertions, 114 deletions
diff --git a/usr.bin/make/buf.h b/usr.bin/make/buf.h index dcb8188720c..643bb4f5cbe 100644 --- a/usr.bin/make/buf.h +++ b/usr.bin/make/buf.h @@ -1,4 +1,4 @@ -/* $OpenBSD: buf.h,v 1.6 1999/12/06 22:24:31 espie Exp $ */ +/* $OpenBSD: buf.h,v 1.7 1999/12/09 18:18:24 espie Exp $ */ /* $NetBSD: buf.h,v 1.7 1996/12/31 17:53:22 christos Exp $ */ /* @@ -67,7 +67,17 @@ typedef struct Buffer { #define BUF_ERROR 256 void Buf_OvAddChar __P((Buffer, char)); + +/* Buf_AddChars -- Add a number of chars to the buffer. */ void Buf_AddChars __P((Buffer, size_t, const char *)); +/* Buf_AddSpace -- Add a space to buffer. */ +#define Buf_AddSpace(b) Buf_AddChar((b), ' ') +/* Buf_AddString -- Add the contents of a NULL terminated string to buffer. */ +#define Buf_AddString(b, s) Buf_AddChars((b), strlen(s), (s)) +/* Buf_AddInterval -- Add characters between pointers s and e to buffer. */ +#define Buf_AddInterval(b, s, e) Buf_AddChars((b), (e) - (s), (s)) + + char *Buf_GetAll __P((Buffer, size_t *)); void Buf_Discard __P((Buffer, size_t)); int Buf_Size __P((Buffer)); diff --git a/usr.bin/make/cond.c b/usr.bin/make/cond.c index 436ab50c8f0..562e6b73d8d 100644 --- a/usr.bin/make/cond.c +++ b/usr.bin/make/cond.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cond.c,v 1.6 1999/12/06 22:28:44 espie Exp $ */ +/* $OpenBSD: cond.c,v 1.7 1999/12/09 18:18:24 espie Exp $ */ /* $NetBSD: cond.c,v 1.7 1996/11/06 17:59:02 christos Exp $ */ /* @@ -43,7 +43,7 @@ #if 0 static char sccsid[] = "@(#)cond.c 8.2 (Berkeley) 1/2/94"; #else -static char rcsid[] = "$OpenBSD: cond.c,v 1.6 1999/12/06 22:28:44 espie Exp $"; +static char rcsid[] = "$OpenBSD: cond.c,v 1.7 1999/12/09 18:18:24 espie Exp $"; #endif #endif /* not lint */ @@ -234,7 +234,7 @@ CondGetArg(linePtr, argPtr, func, parens) cp2 = Var_Parse(cp, VAR_CMD, TRUE, &len, &doFree); - Buf_AddChars(buf, strlen(cp2), cp2); + Buf_AddString(buf, cp2); if (doFree) { free(cp2); } @@ -543,12 +543,10 @@ CondToken(doEval) if (!isspace((unsigned char) *condExpr) && strchr("!=><", *condExpr) == NULL) { Buffer buf; - char *cp; buf = Buf_Init(0); - for (cp = lhs; *cp; cp++) - Buf_AddChar(buf, *cp); + Buf_AddString(buf, lhs); if (doFree) free(lhs); @@ -640,7 +638,7 @@ do_string_compare: cp2 = Var_Parse(cp, VAR_CMD, doEval,&len, &freeIt); if (cp2 != var_Error) { - Buf_AddChars(buf, strlen(cp2), cp2); + Buf_AddString(buf, cp2); if (freeIt) { free(cp2); } diff --git a/usr.bin/make/for.c b/usr.bin/make/for.c index b3aa91525dc..7274e0ed873 100644 --- a/usr.bin/make/for.c +++ b/usr.bin/make/for.c @@ -1,4 +1,4 @@ -/* $OpenBSD: for.c,v 1.7 1999/12/06 22:24:31 espie Exp $ */ +/* $OpenBSD: for.c,v 1.8 1999/12/09 18:18:24 espie Exp $ */ /* $NetBSD: for.c,v 1.4 1996/11/06 17:59:05 christos Exp $ */ /* @@ -65,7 +65,7 @@ #if 0 static char sccsid[] = "@(#)for.c 8.1 (Berkeley) 6/6/93"; #else -static char rcsid[] = "$OpenBSD: for.c,v 1.7 1999/12/06 22:24:31 espie Exp $"; +static char rcsid[] = "$OpenBSD: for.c,v 1.8 1999/12/09 18:18:24 espie Exp $"; #endif #endif /* not lint */ @@ -246,7 +246,7 @@ For_Eval (line) } if (forLevel != 0) { - Buf_AddChars(forBuf, strlen(line), line); + Buf_AddString(forBuf, line); Buf_AddChar(forBuf, '\n'); return 1; } diff --git a/usr.bin/make/str.c b/usr.bin/make/str.c index 0ac7f35dd82..04bded83f89 100644 --- a/usr.bin/make/str.c +++ b/usr.bin/make/str.c @@ -1,4 +1,4 @@ -/* $OpenBSD: str.c,v 1.10 1999/12/06 22:24:32 espie Exp $ */ +/* $OpenBSD: str.c,v 1.11 1999/12/09 18:18:24 espie Exp $ */ /* $NetBSD: str.c,v 1.13 1996/11/06 17:59:23 christos Exp $ */ /*- @@ -43,7 +43,7 @@ #if 0 static char sccsid[] = "@(#)str.c 5.8 (Berkeley) 6/1/90"; #else -static char rcsid[] = "$OpenBSD: str.c,v 1.10 1999/12/06 22:24:32 espie Exp $"; +static char rcsid[] = "$OpenBSD: str.c,v 1.11 1999/12/09 18:18:24 espie Exp $"; #endif #endif /* not lint */ @@ -412,7 +412,7 @@ Str_SYSVSubst(buf, pat, src, len) if ((m = strchr(pat, '%')) != NULL) { /* Copy the prefix */ - Buf_AddChars(buf, m - pat, pat); + Buf_AddInterval(buf, pat, m); /* skip the % */ pat = m + 1; } @@ -421,7 +421,7 @@ Str_SYSVSubst(buf, pat, src, len) Buf_AddChars(buf, len, src); /* append the rest */ - Buf_AddChars(buf, strlen(pat), pat); + Buf_AddString(buf, pat); } char * diff --git a/usr.bin/make/var.c b/usr.bin/make/var.c index 1a809ab7bf4..04c56938c97 100644 --- a/usr.bin/make/var.c +++ b/usr.bin/make/var.c @@ -1,4 +1,4 @@ -/* $OpenBSD: var.c,v 1.18 1999/12/06 22:28:44 espie Exp $ */ +/* $OpenBSD: var.c,v 1.19 1999/12/09 18:18:24 espie Exp $ */ /* $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $ */ /* @@ -70,7 +70,7 @@ #if 0 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; #else -static char rcsid[] = "$OpenBSD: var.c,v 1.18 1999/12/06 22:28:44 espie Exp $"; +static char rcsid[] = "$OpenBSD: var.c,v 1.19 1999/12/09 18:18:24 espie Exp $"; #endif #endif /* not lint */ @@ -485,7 +485,7 @@ Var_Set (name, val, ctxt) (void)VarAdd(name, val, ctxt); } else { Buf_Discard(v->val, Buf_Size(v->val)); - Buf_AddChars(v->val, strlen(val), val); + Buf_AddString(v->val, val); if (DEBUG(VAR)) { printf("%s:%s = %s\n", ctxt->name, name, val); @@ -539,8 +539,8 @@ Var_Append (name, val, ctxt) if (v == (Var *) NIL) { (void)VarAdd(name, val, ctxt); } else { - Buf_AddChar(v->val, ' '); - Buf_AddChars(v->val, strlen(val), val); + Buf_AddSpace(v->val); + Buf_AddString(v->val, val); if (DEBUG(VAR)) { printf("%s:%s = %s\n", ctxt->name, name, @@ -629,13 +629,10 @@ VarUppercase (word, addSpace, buf, dummy) { size_t len = strlen(word); - if (addSpace) { - Buf_AddChar(buf, ' '); - } - - while (len--) { + if (addSpace) + Buf_AddSpace(buf); + while (len--) Buf_AddChar(buf, toupper(*word++)); - } return (TRUE); } @@ -663,13 +660,10 @@ VarLowercase (word, addSpace, buf, dummy) { size_t len = strlen(word); - if (addSpace) { - Buf_AddChar(buf, ' '); - } - - while (len--) { + if (addSpace) + Buf_AddSpace(buf); + while (len--) Buf_AddChar(buf, tolower(*word++)); - } return (TRUE); } @@ -698,24 +692,18 @@ VarHead (word, addSpace, buf, dummy) { register char *slash; - slash = strrchr (word, '/'); - if (slash != (char *)NULL) { - if (addSpace) { - Buf_AddChar(buf, ' '); - } - *slash = '\0'; - Buf_AddChars(buf, strlen(word), word); - *slash = '/'; + slash = strrchr(word, '/'); + if (slash != NULL) { + if (addSpace) + Buf_AddSpace(buf); + Buf_AddInterval(buf, word, slash); return (TRUE); } else { - /* - * If no directory part, give . (q.v. the POSIX standard) - */ - if (addSpace) { - Buf_AddChars(buf, 2, " ."); - } else { + /* If no directory part, give . (q.v. the POSIX standard) */ + if (addSpace) + Buf_AddString(buf, " ."); + else Buf_AddChar(buf, '.'); - } } return(dummy ? TRUE : TRUE); } @@ -745,18 +733,13 @@ VarTail (word, addSpace, buf, dummy) { register char *slash; - if (addSpace) { - Buf_AddChar(buf, ' '); - } - - slash = strrchr (word, '/'); - if (slash != (char *)NULL) { - *slash++ = '\0'; - Buf_AddChars(buf, strlen(slash), slash); - slash[-1] = '/'; - } else { - Buf_AddChars(buf, strlen(word), word); - } + if (addSpace) + Buf_AddSpace(buf); + slash = strrchr(word, '/'); + if (slash != NULL) + Buf_AddString(buf, slash+1); + else + Buf_AddString(buf, word); return (dummy ? TRUE : TRUE); } @@ -782,16 +765,13 @@ VarSuffix (word, addSpace, buf, dummy) Buffer buf; /* Buffer in which to store it */ ClientData dummy; { - register char *dot; + char *dot; - dot = strrchr (word, '.'); - if (dot != (char *)NULL) { - if (addSpace) { - Buf_AddChar(buf, ' '); - } - *dot++ = '\0'; - Buf_AddChars(buf, strlen(dot), dot); - dot[-1] = '.'; + dot = strrchr(word, '.'); + if (dot != NULL) { + if (addSpace) + Buf_AddSpace(buf); + Buf_AddString(buf, dot+1); addSpace = TRUE; } return (dummy ? addSpace : addSpace); @@ -820,20 +800,16 @@ VarRoot (word, addSpace, buf, dummy) Buffer buf; /* Buffer in which to store it */ ClientData dummy; { - register char *dot; + char *dot; - if (addSpace) { - Buf_AddChar(buf, ' '); - } + if (addSpace) + Buf_AddSpace(buf); - dot = strrchr (word, '.'); - if (dot != (char *)NULL) { - *dot = '\0'; - Buf_AddChars(buf, strlen(word), word); - *dot = '.'; - } else { - Buf_AddChars(buf, strlen(word), word); - } + dot = strrchr(word, '.'); + if (dot != NULL) + Buf_AddInterval(buf, word, dot); + else + Buf_AddString(buf, word); return (dummy ? TRUE : TRUE); } @@ -862,11 +838,10 @@ VarMatch (word, addSpace, buf, pattern) ClientData pattern; /* Pattern the word must match */ { if (Str_Match(word, (char *) pattern)) { - if (addSpace) { - Buf_AddChar(buf, ' '); - } + if (addSpace) + Buf_AddSpace(buf); addSpace = TRUE; - Buf_AddChars(buf, strlen(word), word); + Buf_AddString(buf, word); } return(addSpace); } @@ -903,14 +878,14 @@ VarSYSVMatch (word, addSpace, buf, patp) if (*word) { if (addSpace) - Buf_AddChar(buf, ' '); + Buf_AddSpace(buf); addSpace = TRUE; if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL) Str_SYSVSubst(buf, pat->rhs, ptr, len); else - Buf_AddChars(buf, strlen(word), word); + Buf_AddString(buf, word); } return(addSpace); } @@ -942,11 +917,10 @@ VarNoMatch (word, addSpace, buf, pattern) ClientData pattern; /* Pattern the word must match */ { if (!Str_Match(word, (char *) pattern)) { - if (addSpace) { - Buf_AddChar(buf, ' '); - } + if (addSpace) + Buf_AddSpace(buf); addSpace = TRUE; - Buf_AddChars(buf, strlen(word), word); + Buf_AddString(buf, word); } return(addSpace); } @@ -998,9 +972,8 @@ VarSubstitute (word, addSpace, buf, patternp) * if rhs is non-null. */ if (pattern->rightLen != 0) { - if (addSpace) { - Buf_AddChar(buf, ' '); - } + if (addSpace) + Buf_AddSpace(buf); addSpace = TRUE; Buf_AddChars(buf, pattern->rightLen, pattern->rhs); } @@ -1015,9 +988,8 @@ VarSubstitute (word, addSpace, buf, patternp) * Matches at start but need to copy in trailing characters */ if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){ - if (addSpace) { - Buf_AddChar(buf, ' '); - } + if (addSpace) + Buf_AddSpace(buf); addSpace = TRUE; } Buf_AddChars(buf, pattern->rightLen, pattern->rhs); @@ -1047,12 +1019,11 @@ VarSubstitute (word, addSpace, buf, patternp) * by the right-hand-side. */ if (((cp - word) + pattern->rightLen) != 0) { - if (addSpace) { - Buf_AddChar(buf, ' '); - } + if (addSpace) + Buf_AddSpace(buf); addSpace = TRUE; } - Buf_AddChars(buf, cp - word, word); + Buf_AddInterval(buf, word, cp); Buf_AddChars(buf, pattern->rightLen, pattern->rhs); pattern->flags |= VAR_SUB_MATCHED; } else { @@ -1082,10 +1053,10 @@ VarSubstitute (word, addSpace, buf, patternp) cp = strstr(word, pattern->lhs); if (cp != (char *)NULL) { if (addSpace && (((cp - word) + pattern->rightLen) != 0)){ - Buf_AddChar(buf, ' '); + Buf_AddSpace(buf); addSpace = FALSE; } - Buf_AddChars(buf, cp-word, word); + Buf_AddInterval(buf, word, cp); Buf_AddChars(buf, pattern->rightLen, pattern->rhs); wordLen -= (cp - word) + pattern->leftLen; word = cp + pattern->leftLen; @@ -1098,9 +1069,8 @@ VarSubstitute (word, addSpace, buf, patternp) } } if (wordLen != 0) { - if (addSpace) { - Buf_AddChar(buf, ' '); - } + if (addSpace) + Buf_AddSpace(buf); Buf_AddChars(buf, wordLen, word); } /* @@ -1113,9 +1083,8 @@ VarSubstitute (word, addSpace, buf, patternp) return (addSpace); } nosub: - if (addSpace) { - Buf_AddChar(buf, ' '); - } + if (addSpace) + Buf_AddSpace(buf); Buf_AddChars(buf, wordLen, word); return(TRUE); } @@ -1179,7 +1148,7 @@ VarRESubstitute(word, addSpace, buf, patternp) #define MAYBE_ADD_SPACE() \ if (addSpace && !added) \ - Buf_AddChar(buf, ' '); \ + Buf_AddSpace(buf); \ added = 1 added = 0; @@ -1389,7 +1358,7 @@ VarGetPattern(ctxt, err, tstr, delim, flags, length, pattern) * substitution and recurse. */ cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt); - Buf_AddChars(buf, strlen(cp2), cp2); + Buf_AddString(buf, cp2); if (freeIt) free(cp2); cp += len - 1; @@ -2179,7 +2148,7 @@ Var_Subst (var, str, ctxt, undefErr) for (cp = str++; *str != '$' && *str != '\0'; str++) continue; - Buf_AddChars(buf, str - cp, cp); + Buf_AddInterval(buf, cp, str); } else { if (var != NULL) { int expand; @@ -2210,7 +2179,7 @@ Var_Subst (var, str, ctxt, undefErr) * the nested one */ if (*p == '$') { - Buf_AddChars(buf, p - str, str); + Buf_AddInterval(buf, str, p); str = p; continue; } @@ -2223,7 +2192,7 @@ Var_Subst (var, str, ctxt, undefErr) */ for (;*p != '$' && *p != '\0'; p++) continue; - Buf_AddChars(buf, p - str, str); + Buf_AddInterval(buf, str, p); str = p; expand = FALSE; } @@ -2280,7 +2249,7 @@ Var_Subst (var, str, ctxt, undefErr) * Copy all the characters from the variable value straight * into the new string. */ - Buf_AddChars(buf, strlen(val), val); + Buf_AddString(buf, val); if (doFree) { free ((Address)val); } |