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:
parent
4585e3ac5a
commit
beed8bb5f6
@ -144,10 +144,6 @@ static char varNoError[] = "";
|
|||||||
GNode *VAR_GLOBAL; /* variables from the makefile */
|
GNode *VAR_GLOBAL; /* variables from the makefile */
|
||||||
GNode *VAR_CMD; /* variables defined on the command-line */
|
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 OPEN_PAREN '('
|
||||||
#define CLOSE_PAREN ')'
|
#define CLOSE_PAREN ')'
|
||||||
#define OPEN_BRACE '{'
|
#define OPEN_BRACE '{'
|
||||||
@ -232,71 +228,106 @@ VarPossiblyExpand(const char *name, GNode *ctxt)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-
|
/**
|
||||||
*-----------------------------------------------------------------------
|
* If the variable name begins with a '.', it could very well be
|
||||||
* VarFind --
|
* one of the local ones. We check the name against all the local
|
||||||
* Find the given variable in the given context and any other contexts
|
* variables and substitute the short version in for 'name' if it
|
||||||
* indicated.
|
* matches one of them.
|
||||||
*
|
*/
|
||||||
* Flags:
|
static const char *
|
||||||
* FIND_GLOBAL set means look in the VAR_GLOBAL context too
|
VarLocal(const char name[])
|
||||||
* FIND_CMD set means to look in the VAR_CMD context too
|
{
|
||||||
* FIND_ENV set means to look in the environment
|
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:
|
* Results:
|
||||||
* A pointer to the structure describing the desired variable or
|
* A pointer to the structure describing the desired variable or
|
||||||
* NULL if the variable does not exist.
|
* NULL if the variable does not exist.
|
||||||
*
|
|
||||||
* Side Effects:
|
|
||||||
* None
|
|
||||||
*-----------------------------------------------------------------------
|
|
||||||
*/
|
*/
|
||||||
static Var *
|
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;
|
Boolean localCheckEnvFirst;
|
||||||
LstNode *ln;
|
LstNode *ln;
|
||||||
Var *var;
|
Var *var;
|
||||||
char *env;
|
char *env;
|
||||||
|
|
||||||
/*
|
name = VarLocal(name);
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Note whether this is one of the specific variables we were told
|
* 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,
|
* look for it in VAR_CMD, VAR_GLOBAL and the environment,
|
||||||
* in that order, depending on the FIND_* flags in 'flags'
|
* in that order, depending on the FIND_* flags in 'flags'
|
||||||
*/
|
*/
|
||||||
var = VarLookup(&ctxt->context, name);
|
if ((var = VarLookup(&ctxt->context, name)) != NULL)
|
||||||
if (var != NULL) {
|
|
||||||
/* got it */
|
|
||||||
return (var);
|
return (var);
|
||||||
}
|
|
||||||
|
|
||||||
/* not there - try command line context */
|
/* not there - try command line context */
|
||||||
if ((flags & FIND_CMD) && (ctxt != VAR_CMD)) {
|
if (ctxt != VAR_CMD) {
|
||||||
var = VarLookup(&VAR_CMD->context, name);
|
if ((var = VarLookup(&VAR_CMD->context, name)) != NULL)
|
||||||
if (var != NULL)
|
|
||||||
return (var);
|
return (var);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* not there - try global context, but only if not -e/-E */
|
/* not there - try global context, but only if not -e/-E */
|
||||||
if ((flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL) &&
|
if (ctxt != VAR_GLOBAL && (!checkEnvFirst && !localCheckEnvFirst)) {
|
||||||
!checkEnvFirst && !localCheckEnvFirst) {
|
if ((var = VarLookup(&VAR_GLOBAL->context, name)) != NULL)
|
||||||
var = VarLookup(&VAR_GLOBAL->context, name);
|
|
||||||
if (var != NULL)
|
|
||||||
return (var);
|
return (var);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(flags & FIND_ENV))
|
|
||||||
/* we were not told to look into the environment */
|
|
||||||
return (NULL);
|
|
||||||
|
|
||||||
/* look in the environment */
|
/* look in the environment */
|
||||||
if ((env = getenv(name)) != NULL) {
|
if ((env = getenv(name)) != NULL) {
|
||||||
/* craft this variable from the environment value */
|
/* 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) */
|
/* deferred check for the environment (in case of -e/-E) */
|
||||||
if ((checkEnvFirst || localCheckEnvFirst) &&
|
if ((ctxt != VAR_GLOBAL) && (checkEnvFirst || localCheckEnvFirst)) {
|
||||||
(flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL)) {
|
if ((var = VarLookup(&VAR_GLOBAL->context, name)) != NULL)
|
||||||
var = VarLookup(&VAR_GLOBAL->context, name);
|
|
||||||
if (var != NULL)
|
|
||||||
return (var);
|
return (var);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (NULL);
|
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...
|
* much point in searching them all just to save a bit of memory...
|
||||||
*/
|
*/
|
||||||
n = VarPossiblyExpand(name, ctxt);
|
n = VarPossiblyExpand(name, ctxt);
|
||||||
v = VarFind(n, ctxt, 0);
|
v = VarFindOnly(n, ctxt);
|
||||||
if (v == NULL) {
|
if (v == NULL) {
|
||||||
VarAdd(n, val, ctxt);
|
VarAdd(n, val, ctxt);
|
||||||
} else {
|
} else {
|
||||||
@ -487,7 +507,11 @@ Var_Append(const char *name, const char *val, GNode *ctxt)
|
|||||||
char *n;
|
char *n;
|
||||||
|
|
||||||
n = VarPossiblyExpand(name, ctxt);
|
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) {
|
if (v == NULL) {
|
||||||
VarAdd(n, val, ctxt);
|
VarAdd(n, val, ctxt);
|
||||||
|
|
||||||
@ -533,7 +557,7 @@ Var_Exists(const char *name, GNode *ctxt)
|
|||||||
char *n;
|
char *n;
|
||||||
|
|
||||||
n = VarPossiblyExpand(name, ctxt);
|
n = VarPossiblyExpand(name, ctxt);
|
||||||
v = VarFind(n, ctxt, FIND_CMD | FIND_GLOBAL | FIND_ENV);
|
v = VarFindAny(n, ctxt);
|
||||||
if (v == NULL) {
|
if (v == NULL) {
|
||||||
free(n);
|
free(n);
|
||||||
return (FALSE);
|
return (FALSE);
|
||||||
@ -567,7 +591,7 @@ Var_Value(const char *name, GNode *ctxt, char **frp)
|
|||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
n = VarPossiblyExpand(name, ctxt);
|
n = VarPossiblyExpand(name, ctxt);
|
||||||
v = VarFind(n, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
|
v = VarFindAny(n, ctxt);
|
||||||
if (v == NULL) {
|
if (v == NULL) {
|
||||||
p = NULL;
|
p = NULL;
|
||||||
*frp = NULL;
|
*frp = NULL;
|
||||||
@ -1293,7 +1317,7 @@ ParseRestModifier(VarParser *vp, char startc, Buffer *buf, Boolean *freeResult)
|
|||||||
|
|
||||||
vname = Buf_GetAll(buf, &vlen);
|
vname = Buf_GetAll(buf, &vlen);
|
||||||
|
|
||||||
v = VarFind(vname, vp->ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
|
v = VarFindAny(vname, vp->ctxt);
|
||||||
if (v != NULL) {
|
if (v != NULL) {
|
||||||
value = ParseModifier(vp, startc, v, freeResult);
|
value = ParseModifier(vp, startc, v, freeResult);
|
||||||
|
|
||||||
@ -1369,7 +1393,7 @@ ParseRestModifier(VarParser *vp, char startc, Buffer *buf, Boolean *freeResult)
|
|||||||
name[0] = vname[0];
|
name[0] = vname[0];
|
||||||
name[1] = '\0';
|
name[1] = '\0';
|
||||||
|
|
||||||
v = VarFind(name, vp->ctxt, 0);
|
v = VarFindOnly(name, vp->ctxt);
|
||||||
if (v != NULL) {
|
if (v != NULL) {
|
||||||
value = ParseModifier(vp, startc, v, freeResult);
|
value = ParseModifier(vp, startc, v, freeResult);
|
||||||
return (value);
|
return (value);
|
||||||
@ -1403,7 +1427,7 @@ ParseRestEnd(VarParser *vp, Buffer *buf, Boolean *freeResult)
|
|||||||
|
|
||||||
vname = Buf_GetAll(buf, &vlen);
|
vname = Buf_GetAll(buf, &vlen);
|
||||||
|
|
||||||
v = VarFind(vname, vp->ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
|
v = VarFindAny(vname, vp->ctxt);
|
||||||
if (v != NULL) {
|
if (v != NULL) {
|
||||||
value = VarExpand(v, vp);
|
value = VarExpand(v, vp);
|
||||||
|
|
||||||
@ -1466,7 +1490,7 @@ ParseRestEnd(VarParser *vp, Buffer *buf, Boolean *freeResult)
|
|||||||
name[0] = vname[0];
|
name[0] = vname[0];
|
||||||
name[1] = '\0';
|
name[1] = '\0';
|
||||||
|
|
||||||
v = VarFind(name, vp->ctxt, 0);
|
v = VarFindOnly(name, vp->ctxt);
|
||||||
if (v != NULL) {
|
if (v != NULL) {
|
||||||
char *val;
|
char *val;
|
||||||
/*
|
/*
|
||||||
@ -1573,7 +1597,7 @@ VarParseShort(VarParser *vp, Boolean *freeResult)
|
|||||||
|
|
||||||
vp->ptr++; /* consume single letter */
|
vp->ptr++; /* consume single letter */
|
||||||
|
|
||||||
v = VarFind(vname, vp->ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
|
v = VarFindAny(vname, vp->ctxt);
|
||||||
if (v != NULL) {
|
if (v != NULL) {
|
||||||
value = VarExpand(v, vp);
|
value = VarExpand(v, vp);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user