Finish constification of Var_Parse() and Var_Subst().

Patch:		0.18, 0.8, 7.110

Submitted by:	Max Okumoto <okumoto@ucsd.edu>
This commit is contained in:
Hartmut Brandt 2005-03-08 07:43:57 +00:00
parent 9e1fb7fbad
commit 34d49b885f
2 changed files with 32 additions and 41 deletions

View File

@ -142,8 +142,6 @@ GNode *VAR_CMD; /* variables defined on the command-line */
#define OPEN_BRACE '{' #define OPEN_BRACE '{'
#define CLOSE_BRACE '}' #define CLOSE_BRACE '}'
static char *VarGetPattern(GNode *, int, char **, int, int *, size_t *,
VarPattern *);
static int VarPrintVar(void *, void *); static int VarPrintVar(void *, void *);
/* /*
@ -693,10 +691,10 @@ SortIncreasing(const void *l, const void *r)
*----------------------------------------------------------------------- *-----------------------------------------------------------------------
*/ */
static char * static char *
VarGetPattern(GNode *ctxt, int err, char **tstr, int delim, int *flags, VarGetPattern(GNode *ctxt, int err, const char **tstr, int delim, int *flags,
size_t *length, VarPattern *pattern) size_t *length, VarPattern *pattern)
{ {
char *cp; const char *cp;
Buffer *buf = Buf_Init(0); Buffer *buf = Buf_Init(0);
size_t junk; size_t junk;
@ -745,7 +743,7 @@ VarGetPattern(GNode *ctxt, int err, char **tstr, int delim, int *flags,
free(cp2); free(cp2);
cp += len - 1; cp += len - 1;
} else { } else {
char *cp2 = &cp[1]; const char *cp2 = &cp[1];
if (*cp2 == OPEN_PAREN || *cp2 == OPEN_BRACE) { if (*cp2 == OPEN_PAREN || *cp2 == OPEN_BRACE) {
/* /*
@ -785,11 +783,12 @@ VarGetPattern(GNode *ctxt, int err, char **tstr, int delim, int *flags,
*length = 0; *length = 0;
return (NULL); return (NULL);
} else { } else {
char *result;
*tstr = ++cp; *tstr = ++cp;
cp = (char *)Buf_GetAll(buf, length); result = (char *)Buf_GetAll(buf, length);
*length -= 1; /* Don't count the NULL */ *length -= 1; /* Don't count the NULL */
Buf_Destroy(buf, FALSE); Buf_Destroy(buf, FALSE);
return (cp); return (result);
} }
} }
@ -1157,12 +1156,12 @@ modifier_S(const char mod[], const char value[], Var *v, GNode *ctxt, Boolean er
* XXXHB update this comment or remove it and point to the man page. * XXXHB update this comment or remove it and point to the man page.
*/ */
static char * static char *
ParseModifier(const char input[], char tstr[], ParseModifier(const char input[], const char tstr[],
char startc, char endc, Boolean dynamic, Var *v, char startc, char endc, Boolean dynamic, Var *v,
GNode *ctxt, Boolean err, size_t *lengthPtr, Boolean *freePtr) GNode *ctxt, Boolean err, size_t *lengthPtr, Boolean *freePtr)
{ {
char *rw_str; char *rw_str;
char *cp; const char *cp;
rw_str = VarExpand(v, ctxt, err); rw_str = VarExpand(v, ctxt, err);
*freePtr = TRUE; *freePtr = TRUE;
@ -1476,10 +1475,8 @@ ParseModifier(const char input[], char tstr[],
Error("Unclosed variable specification for %s", Error("Unclosed variable specification for %s",
v->name); v->name);
} else if (termc == ':') { } else if (termc == ':') {
*cp = termc;
cp++; cp++;
} else { } else {
*cp = termc;
} }
tstr = cp; tstr = cp;
} }
@ -1533,30 +1530,28 @@ ParseModifier(const char input[], char tstr[],
* Check if brackets contain a variable name. * Check if brackets contain a variable name.
*/ */
static char * static char *
VarParseLong(char foo[], GNode *ctxt, Boolean err, size_t *lengthPtr, VarParseLong(const char input[], GNode *ctxt, Boolean err, size_t *lengthPtr,
Boolean *freePtr) Boolean *freePtr)
{ {
const char *input = foo; Buffer *buf;
char *rw_str = foo;
char *ptr;
char endc; /* Ending character when variable in parens char endc; /* Ending character when variable in parens
* or braces */ * or braces */
char startc; /* Starting character when variable in parens char startc; /* Starting character when variable in parens
* or braces */ * or braces */
const char *ptr;
const char *vname; const char *vname;
size_t vlen; /* length of variable name, after embedded size_t vlen; /* length of variable name, after embedded
* variable expansion */ * variable expansion */
/* build up expanded variable name in this buffer */ buf = Buf_Init(MAKE_BSIZE);
Buffer *buf = Buf_Init(MAKE_BSIZE);
/* /*
* Skip to the end character or a colon, whichever comes first, * Skip to the end character or a colon, whichever comes first,
* replacing embedded variables as we go. * replacing embedded variables as we go.
*/ */
startc = input[1]; startc = input[0];
endc = (startc == OPEN_PAREN) ? CLOSE_PAREN : CLOSE_BRACE; endc = (startc == OPEN_PAREN) ? CLOSE_PAREN : CLOSE_BRACE;
ptr = rw_str + 2; ptr = input + 1;
while (*ptr != endc && *ptr != ':') { while (*ptr != endc && *ptr != ':') {
if (*ptr == '\0') { if (*ptr == '\0') {
@ -1567,7 +1562,7 @@ VarParseLong(char foo[], GNode *ctxt, Boolean err, size_t *lengthPtr,
* the string, since that's what make does. * the string, since that's what make does.
*/ */
*freePtr = FALSE; *freePtr = FALSE;
*lengthPtr = ptr - input; *lengthPtr += ptr - input;
Buf_Destroy(buf, TRUE); Buf_Destroy(buf, TRUE);
return (var_Error); return (var_Error);
@ -1591,12 +1586,10 @@ VarParseLong(char foo[], GNode *ctxt, Boolean err, size_t *lengthPtr,
ptr++; ptr++;
} }
vname = Buf_GetAll(buf, (size_t *)NULL); /* REPLACE str */ vname = Buf_GetAll(buf, &vlen);
vlen = strlen(vname);
{ {
char *const tstr = ptr; const char *const tstr = ptr;
size_t consumed = tstr - input + 1; size_t consumed = tstr - (input - 1) + 1;
Var *v; /* Variable in invocation */ Var *v; /* Variable in invocation */
Boolean haveModifier; /* TRUE if have modifiers for the variable */ Boolean haveModifier; /* TRUE if have modifiers for the variable */
@ -1605,6 +1598,7 @@ VarParseLong(char foo[], GNode *ctxt, Boolean err, size_t *lengthPtr,
* is done to support dynamic sources. The * is done to support dynamic sources. The
* result is just the invocation, unaltered */ * result is just the invocation, unaltered */
input--;
haveModifier = (*tstr == ':'); haveModifier = (*tstr == ':');
dynamic = FALSE; dynamic = FALSE;
@ -1825,28 +1819,25 @@ VarParseShort(const char input[], GNode *ctxt, Boolean err,
* specification. * specification.
* *
* Results: * Results:
* The (possibly-modified) value of the variable or var_Error if the * The value of the variable or var_Error if the specification
* specification is invalid. The length of the specification is * is invalid. The length of the specification is placed in
* placed in *lengthPtr (for invalid specifications, this is just * *lengthPtr (for invalid specifications, this is just 2 to
* 2 to skip the '$' and the following letter, or 1 if '$' was the * skip the '$' and the following letter, or 1 if '$' was the
* last character in the string). * last character in the string). A Boolean in *freePtr telling
* A Boolean in *freePtr telling whether the returned string should * whether the returned string should be freed by the caller.
* be freed by the caller.
* *
* Side Effects: * Side Effects:
* None. * None.
* *
* Assumption: * Assumption:
* It is assumed that Var_Parse() is called with str[0] == '$'. * It is assumed that Var_Parse() is called with input[0] == '$'.
* *
*----------------------------------------------------------------------- *-----------------------------------------------------------------------
*/ */
char * char *
Var_Parse(char *foo, GNode *ctxt, Boolean err, Var_Parse(const char input[], GNode *ctxt, Boolean err,
size_t *lengthPtr, Boolean *freePtr) size_t *lengthPtr, Boolean *freePtr)
{ {
const char *input = foo;
/* assert(input[0] == '$'); */ /* assert(input[0] == '$'); */
/* consume '$' */ /* consume '$' */
@ -1860,7 +1851,7 @@ Var_Parse(char *foo, GNode *ctxt, Boolean err,
} else if (input[0] == OPEN_PAREN || input[0] == OPEN_BRACE) { } else if (input[0] == OPEN_PAREN || input[0] == OPEN_BRACE) {
/* multi letter variable name */ /* multi letter variable name */
return (VarParseLong(foo, ctxt, err, lengthPtr, freePtr)); return (VarParseLong(input, ctxt, err, lengthPtr, freePtr));
} else { } else {
/* single letter variable name */ /* single letter variable name */
@ -1883,7 +1874,7 @@ Var_Parse(char *foo, GNode *ctxt, Boolean err,
*----------------------------------------------------------------------- *-----------------------------------------------------------------------
*/ */
Buffer * Buffer *
Var_Subst(const char *var, char *str, GNode *ctxt, Boolean undefErr) Var_Subst(const char *var, const char *str, GNode *ctxt, Boolean undefErr)
{ {
Boolean errorReported; Boolean errorReported;
Buffer *buf; /* Buffer for forming things */ Buffer *buf; /* Buffer for forming things */

View File

@ -117,8 +117,8 @@ void Var_Append(const char *, const char *, struct GNode *);
Boolean Var_Exists(const char *, struct GNode *); Boolean Var_Exists(const char *, struct GNode *);
char *Var_Value(const char *, struct GNode *, char **); char *Var_Value(const char *, struct GNode *, char **);
char *Var_Quote(const char *); char *Var_Quote(const char *);
char *Var_Parse(char *, struct GNode *, Boolean, size_t *, Boolean *); char *Var_Parse(const char *, struct GNode *, Boolean, size_t *, Boolean *);
struct Buffer *Var_Subst(const char *, char *, struct GNode *, Boolean); struct Buffer *Var_Subst(const char *, const char *, struct GNode *, Boolean);
char *Var_GetTail(char *); char *Var_GetTail(char *);
char *Var_GetHead(char *); char *Var_GetHead(char *);
void Var_Init(void); void Var_Init(void);