Split VarFind() into a series of functions tailored for the different sets

of flags originally passed to VarFind(). This eliminates the code by
removing a bunch of tests.

Patch:		7.173

Submitted by:	Max Okumoto <okumoto@ucsd.edu>
This commit is contained in:
Hartmut Brandt 2005-04-13 11:40:44 +00:00
parent 4585e3ac5a
commit beed8bb5f6

View File

@ -144,10 +144,6 @@ static char varNoError[] = "";
GNode *VAR_GLOBAL; /* variables from the makefile */
GNode *VAR_CMD; /* variables defined on the command-line */
#define FIND_CMD 0x1 /* look in VAR_CMD when searching */
#define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */
#define FIND_ENV 0x4 /* look in the environment also */
#define OPEN_PAREN '('
#define CLOSE_PAREN ')'
#define OPEN_BRACE '{'
@ -232,71 +228,106 @@ VarPossiblyExpand(const char *name, GNode *ctxt)
}
}
/*-
*-----------------------------------------------------------------------
* VarFind --
* Find the given variable in the given context and any other contexts
* indicated.
*
* Flags:
* FIND_GLOBAL set means look in the VAR_GLOBAL context too
* FIND_CMD set means to look in the VAR_CMD context too
* FIND_ENV set means to look in the environment
/**
* If the variable name begins with a '.', it could very well be
* one of the local ones. We check the name against all the local
* variables and substitute the short version in for 'name' if it
* matches one of them.
*/
static const char *
VarLocal(const char name[])
{
if (name[0] == '.') {
switch (name[1]) {
case 'A':
if (!strcmp(name, ".ALLSRC"))
return (ALLSRC);
if (!strcmp(name, ".ARCHIVE"))
return (ARCHIVE);
break;
case 'I':
if (!strcmp(name, ".IMPSRC"))
return (IMPSRC);
break;
case 'M':
if (!strcmp(name, ".MEMBER"))
return (MEMBER);
break;
case 'O':
if (!strcmp(name, ".OODATE"))
return (OODATE);
break;
case 'P':
if (!strcmp(name, ".PREFIX"))
return (PREFIX);
break;
case 'T':
if (!strcmp(name, ".TARGET"))
return (TARGET);
break;
default:
break;
}
}
return (name);
}
/**
* Find the given variable in the given context and the enviornment.
*
* Results:
* A pointer to the structure describing the desired variable or
* NULL if the variable does not exist.
*
* Side Effects:
* None
*-----------------------------------------------------------------------
*/
static Var *
VarFind(const char *name, GNode *ctxt, int flags)
VarFindEnv(const char name[], GNode *ctxt)
{
Var *var;
char *env;
name = VarLocal(name);
/* First look for the variable in the given context. */
if ((var = VarLookup(&ctxt->context, name)) != NULL)
return (var);
/* Look for the variable in the environment. */
if ((env = getenv(name)) != NULL) {
/* craft this variable from the environment value */
return (VarCreate(name, env, VAR_FROM_ENV));
}
return (NULL);
}
/**
* Look for the variable in the given context.
*/
static Var *
VarFindOnly(const char name[], GNode *ctxt)
{
Var *var;
name = VarLocal(name);
if ((var = VarLookup(&ctxt->context, name)) != NULL)
return (var);
return (NULL);
}
/**
* Look for the variable in all contexts.
*/
static Var *
VarFindAny(const char name[], GNode *ctxt)
{
Boolean localCheckEnvFirst;
LstNode *ln;
Var *var;
char *env;
/*
* If the variable name begins with a '.', it could very well be one of
* the local ones. We check the name against all the local variables
* and substitute the short version in for 'name' if it matches one of
* them.
*/
if (name[0] == '.') {
switch (name[1]) {
case 'A':
if (!strcmp(name, ".ALLSRC"))
name = ALLSRC;
if (!strcmp(name, ".ARCHIVE"))
name = ARCHIVE;
break;
case 'I':
if (!strcmp(name, ".IMPSRC"))
name = IMPSRC;
break;
case 'M':
if (!strcmp(name, ".MEMBER"))
name = MEMBER;
break;
case 'O':
if (!strcmp(name, ".OODATE"))
name = OODATE;
break;
case 'P':
if (!strcmp(name, ".PREFIX"))
name = PREFIX;
break;
case 'T':
if (!strcmp(name, ".TARGET"))
name = TARGET;
break;
default:
break;
}
}
name = VarLocal(name);
/*
* Note whether this is one of the specific variables we were told
@ -315,31 +346,21 @@ VarFind(const char *name, GNode *ctxt, int flags)
* look for it in VAR_CMD, VAR_GLOBAL and the environment,
* in that order, depending on the FIND_* flags in 'flags'
*/
var = VarLookup(&ctxt->context, name);
if (var != NULL) {
/* got it */
if ((var = VarLookup(&ctxt->context, name)) != NULL)
return (var);
}
/* not there - try command line context */
if ((flags & FIND_CMD) && (ctxt != VAR_CMD)) {
var = VarLookup(&VAR_CMD->context, name);
if (var != NULL)
if (ctxt != VAR_CMD) {
if ((var = VarLookup(&VAR_CMD->context, name)) != NULL)
return (var);
}
/* not there - try global context, but only if not -e/-E */
if ((flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL) &&
!checkEnvFirst && !localCheckEnvFirst) {
var = VarLookup(&VAR_GLOBAL->context, name);
if (var != NULL)
if (ctxt != VAR_GLOBAL && (!checkEnvFirst && !localCheckEnvFirst)) {
if ((var = VarLookup(&VAR_GLOBAL->context, name)) != NULL)
return (var);
}
if (!(flags & FIND_ENV))
/* we were not told to look into the environment */
return (NULL);
/* look in the environment */
if ((env = getenv(name)) != NULL) {
/* craft this variable from the environment value */
@ -347,12 +368,11 @@ VarFind(const char *name, GNode *ctxt, int flags)
}
/* deferred check for the environment (in case of -e/-E) */
if ((checkEnvFirst || localCheckEnvFirst) &&
(flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL)) {
var = VarLookup(&VAR_GLOBAL->context, name);
if (var != NULL)
if ((ctxt != VAR_GLOBAL) && (checkEnvFirst || localCheckEnvFirst)) {
if ((var = VarLookup(&VAR_GLOBAL->context, name)) != NULL)
return (var);
}
return (NULL);
}
@ -439,7 +459,7 @@ Var_Set(const char *name, const char *val, GNode *ctxt)
* much point in searching them all just to save a bit of memory...
*/
n = VarPossiblyExpand(name, ctxt);
v = VarFind(n, ctxt, 0);
v = VarFindOnly(n, ctxt);
if (v == NULL) {
VarAdd(n, val, ctxt);
} else {
@ -487,7 +507,11 @@ Var_Append(const char *name, const char *val, GNode *ctxt)
char *n;
n = VarPossiblyExpand(name, ctxt);
v = VarFind(n, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0);
if (ctxt == VAR_GLOBAL) {
v = VarFindEnv(n, ctxt);
} else {
v = VarFindOnly(n, ctxt);
}
if (v == NULL) {
VarAdd(n, val, ctxt);
@ -533,7 +557,7 @@ Var_Exists(const char *name, GNode *ctxt)
char *n;
n = VarPossiblyExpand(name, ctxt);
v = VarFind(n, ctxt, FIND_CMD | FIND_GLOBAL | FIND_ENV);
v = VarFindAny(n, ctxt);
if (v == NULL) {
free(n);
return (FALSE);
@ -567,7 +591,7 @@ Var_Value(const char *name, GNode *ctxt, char **frp)
char *p;
n = VarPossiblyExpand(name, ctxt);
v = VarFind(n, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
v = VarFindAny(n, ctxt);
if (v == NULL) {
p = NULL;
*frp = NULL;
@ -1293,7 +1317,7 @@ ParseRestModifier(VarParser *vp, char startc, Buffer *buf, Boolean *freeResult)
vname = Buf_GetAll(buf, &vlen);
v = VarFind(vname, vp->ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
v = VarFindAny(vname, vp->ctxt);
if (v != NULL) {
value = ParseModifier(vp, startc, v, freeResult);
@ -1369,7 +1393,7 @@ ParseRestModifier(VarParser *vp, char startc, Buffer *buf, Boolean *freeResult)
name[0] = vname[0];
name[1] = '\0';
v = VarFind(name, vp->ctxt, 0);
v = VarFindOnly(name, vp->ctxt);
if (v != NULL) {
value = ParseModifier(vp, startc, v, freeResult);
return (value);
@ -1403,7 +1427,7 @@ ParseRestEnd(VarParser *vp, Buffer *buf, Boolean *freeResult)
vname = Buf_GetAll(buf, &vlen);
v = VarFind(vname, vp->ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
v = VarFindAny(vname, vp->ctxt);
if (v != NULL) {
value = VarExpand(v, vp);
@ -1466,7 +1490,7 @@ ParseRestEnd(VarParser *vp, Buffer *buf, Boolean *freeResult)
name[0] = vname[0];
name[1] = '\0';
v = VarFind(name, vp->ctxt, 0);
v = VarFindOnly(name, vp->ctxt);
if (v != NULL) {
char *val;
/*
@ -1573,7 +1597,7 @@ VarParseShort(VarParser *vp, Boolean *freeResult)
vp->ptr++; /* consume single letter */
v = VarFind(vname, vp->ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
v = VarFindAny(vname, vp->ctxt);
if (v != NULL) {
value = VarExpand(v, vp);