=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/make/var.c,v retrieving revision 1.34 retrieving revision 1.35 diff -c -r1.34 -r1.35 *** src/usr.bin/make/var.c 2000/06/23 16:18:09 1.34 --- src/usr.bin/make/var.c 2000/06/23 16:20:01 1.35 *************** *** 1,4 **** ! /* $OpenBSD: var.c,v 1.34 2000/06/23 16:18:09 espie Exp $ */ /* $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $ */ /* --- 1,4 ---- ! /* $OpenBSD: var.c,v 1.35 2000/06/23 16:20:01 espie Exp $ */ /* $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $ */ /* *************** *** 70,76 **** #if 0 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; #else ! static char rcsid[] = "$OpenBSD: var.c,v 1.34 2000/06/23 16:18:09 espie Exp $"; #endif #endif /* not lint */ --- 70,76 ---- #if 0 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; #else ! static char rcsid[] = "$OpenBSD: var.c,v 1.35 2000/06/23 16:20:01 espie Exp $"; #endif #endif /* not lint */ *************** *** 122,127 **** --- 122,128 ---- #include #endif #include + #include #include "make.h" #include "buf.h" *************** *** 143,149 **** char *val; GNode *gn; { ! Var_Set(varnames[idx], val, gn); } void --- 144,150 ---- char *val; GNode *gn; { ! Var_Set(varnames[idx], val, &gn->context); } void *************** *** 152,158 **** char *val; GNode *gn; { ! Var_Append(varnames[idx], val, gn); } char * --- 153,159 ---- char *val; GNode *gn; { ! Var_Append(varnames[idx], val, &gn->context); } char * *************** *** 160,166 **** int idx; GNode *gn; { ! return Var_Value(varnames[idx], gn); } Boolean --- 161,167 ---- int idx; GNode *gn; { ! return Var_Value(varnames[idx], &gn->context); } Boolean *************** *** 168,174 **** int idx; GNode *gn; { ! return Var_Exists(varnames[idx], gn); } /* --- 169,175 ---- int idx; GNode *gn; { ! return Var_Exists(varnames[idx], &gn->context); } /* *************** *** 201,209 **** * The four contexts are searched in the reverse order from which they are * listed. */ ! GNode *VAR_GLOBAL; /* variables from the makefile */ ! GNode *VAR_CMD; /* variables defined on the command-line */ ! static GNode *VAR_ENV; /* variables read from env */ static LIST allVars; /* List of all variables */ --- 202,210 ---- * The four contexts are searched in the reverse order from which they are * listed. */ ! SymTable *VAR_GLOBAL; /* variables from the makefile */ ! SymTable *VAR_CMD; /* variables defined on the command-line */ ! static SymTable *VAR_ENV; /* variables read from env */ static LIST allVars; /* List of all variables */ *************** *** 250,257 **** #define VarValue(v) Buf_Retrieve(&((v)->val)) static int VarCmp __P((void *, void *)); ! static Var *VarFind __P((char *, GNode *, int)); ! static Var *VarAdd __P((char *, char *, GNode *)); static void VarDelete __P((void *)); static Boolean VarHead __P((char *, Boolean, Buffer, void *)); static Boolean VarTail __P((char *, Boolean, Buffer, void *)); --- 251,258 ---- #define VarValue(v) Buf_Retrieve(&((v)->val)) static int VarCmp __P((void *, void *)); ! static Var *VarFind __P((char *, SymTable *, int)); ! static Var *VarAdd __P((char *, char *, SymTable *)); static void VarDelete __P((void *)); static Boolean VarHead __P((char *, Boolean, Buffer, void *)); static Boolean VarTail __P((char *, Boolean, Buffer, void *)); *************** *** 267,280 **** static Boolean VarRESubstitute __P((char *, Boolean, Buffer, void *)); #endif static Boolean VarSubstitute __P((char *, Boolean, Buffer, void *)); ! static char *VarGetPattern __P((GNode *, int, char **, int, int *, size_t *, VarPattern *)); static char *VarQuote __P((char *)); static char *VarModify __P((char *, Boolean (*)(char *, Boolean, Buffer, void *), void *)); static void VarPrintVar __P((void *)); static Boolean VarUppercase __P((char *word, Boolean addSpace, Buffer buf, void *dummy)); static Boolean VarLowercase __P((char *word, Boolean addSpace, Buffer buf, void *dummy)); /*- *----------------------------------------------------------------------- * VarCmp -- --- 268,301 ---- static Boolean VarRESubstitute __P((char *, Boolean, Buffer, void *)); #endif static Boolean VarSubstitute __P((char *, Boolean, Buffer, void *)); ! static char *VarGetPattern __P((SymTable *, int, char **, int, int *, size_t *, VarPattern *)); static char *VarQuote __P((char *)); static char *VarModify __P((char *, Boolean (*)(char *, Boolean, Buffer, void *), void *)); static void VarPrintVar __P((void *)); static Boolean VarUppercase __P((char *word, Boolean addSpace, Buffer buf, void *dummy)); static Boolean VarLowercase __P((char *word, Boolean addSpace, Buffer buf, void *dummy)); + static char *context_name __P((SymTable *)); + + static char * + context_name(ctxt) + SymTable *ctxt; + { + GNode *g; + + /* specific handling for GLOBAL, CMD, ENV contexts, which are not + owned by a GNode */ + if (ctxt == VAR_GLOBAL) + return "Global"; + if (ctxt == VAR_CMD) + return "Command"; + if (ctxt == VAR_ENV) + return "Environment"; + g = (GNode *)((char *)ctxt - offsetof(GNode, context)); + return g->name; + } + /*- *----------------------------------------------------------------------- * VarCmp -- *************** *** 311,319 **** *----------------------------------------------------------------------- */ static Var * ! VarFind (name, ctxt, flags) char *name; /* name to find */ ! GNode *ctxt; /* context in which to find it */ int flags; /* FIND_GLOBAL set means to look in the * VAR_GLOBAL context as well. * FIND_CMD set means to look in the VAR_CMD --- 332,340 ---- *----------------------------------------------------------------------- */ static Var * ! VarFind(name, ctxt, flags) char *name; /* name to find */ ! SymTable *ctxt; /* context in which to find it */ int flags; /* FIND_GLOBAL set means to look in the * VAR_GLOBAL context as well. * FIND_CMD set means to look in the VAR_CMD *************** *** 364,379 **** * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order, * depending on the FIND_* flags in 'flags' */ ! var = Lst_Find(&ctxt->context, VarCmp, name); if ((var == NULL) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) ! var = Lst_Find(&VAR_CMD->context, VarCmp, name); if (!checkEnvFirst && (var == NULL) && (flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL)) { ! var = Lst_Find(&VAR_GLOBAL->context, VarCmp, name); } if ((var == NULL) && (flags & FIND_ENV)) { ! var = Lst_Find(&VAR_ENV->context, VarCmp, name); if (var == NULL) { char *env; --- 385,400 ---- * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order, * depending on the FIND_* flags in 'flags' */ ! var = Lst_Find(ctxt, VarCmp, name); if ((var == NULL) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) ! var = Lst_Find(VAR_CMD, VarCmp, name); if (!checkEnvFirst && (var == NULL) && (flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL)) { ! var = Lst_Find(VAR_GLOBAL, VarCmp, name); } if ((var == NULL) && (flags & FIND_ENV)) { ! var = Lst_Find(VAR_ENV, VarCmp, name); if (var == NULL) { char *env; *************** *** 383,389 **** } if (var == NULL && checkEnvFirst && (flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL)) ! var = Lst_Find(&VAR_GLOBAL->context, VarCmp, name); if (var == NULL) return NULL; else --- 404,410 ---- } if (var == NULL && checkEnvFirst && (flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL)) ! var = Lst_Find(VAR_GLOBAL, VarCmp, name); if (var == NULL) return NULL; else *************** *** 408,421 **** VarAdd(name, val, ctxt) char *name; /* name of variable to add */ char *val; /* value to set it to */ ! GNode *ctxt; /* context in which to set it */ { register Var *v; int len; ! v = (Var *) emalloc (sizeof (Var)); ! v->name = estrdup (name); len = val ? strlen(val) : 0; Buf_Init(&(v->val), len+1); --- 429,442 ---- VarAdd(name, val, ctxt) char *name; /* name of variable to add */ char *val; /* value to set it to */ ! SymTable *ctxt; /* context in which to set it */ { register Var *v; int len; ! v = (Var *) emalloc(sizeof(Var)); ! v->name = estrdup(name); len = val ? strlen(val) : 0; Buf_Init(&(v->val), len+1); *************** *** 423,432 **** v->flags = 0; ! Lst_AtFront(&ctxt->context, v); Lst_AtEnd(&allVars, v); if (DEBUG(VAR)) { ! printf("%s:%s = %s\n", ctxt->name, name, val); } return v; } --- 444,453 ---- v->flags = 0; ! Lst_AtFront(ctxt, v); Lst_AtEnd(&allVars, v); if (DEBUG(VAR)) { ! printf("%s:%s = %s\n", context_name(ctxt), name, val); } return v; } *************** *** 472,490 **** void Var_Delete(name, ctxt) char *name; ! GNode *ctxt; { LstNode ln; if (DEBUG(VAR)) { ! printf("%s:delete %s\n", ctxt->name, name); } ! ln = Lst_Find(&ctxt->context, VarCmp, name); if (ln != NULL) { register Var *v; v = (Var *)Lst_Datum(ln); ! Lst_Remove(&ctxt->context, ln); ln = Lst_Member(&allVars, v); Lst_Remove(&allVars, ln); VarDelete(v); --- 493,511 ---- void Var_Delete(name, ctxt) char *name; ! SymTable *ctxt; { LstNode ln; if (DEBUG(VAR)) { ! printf("%s:delete %s\n", context_name(ctxt), name); } ! ln = Lst_Find(ctxt, VarCmp, name); if (ln != NULL) { register Var *v; v = (Var *)Lst_Datum(ln); ! Lst_Remove(ctxt, ln); ln = Lst_Member(&allVars, v); Lst_Remove(&allVars, ln); VarDelete(v); *************** *** 513,522 **** *----------------------------------------------------------------------- */ void ! Var_Set (name, val, ctxt) char *name; /* name of variable to set */ char *val; /* value to give to the variable */ ! GNode *ctxt; /* context in which to set it */ { register Var *v; --- 534,543 ---- *----------------------------------------------------------------------- */ void ! Var_Set(name, val, ctxt) char *name; /* name of variable to set */ char *val; /* value to give to the variable */ ! SymTable *ctxt; /* context in which to set it */ { register Var *v; *************** *** 525,531 **** * here will override anything in a lower context, so there's not much * point in searching them all just to save a bit of memory... */ ! v = VarFind (name, ctxt, 0); if (v == NULL) { (void)VarAdd(name, val, ctxt); } else { --- 546,552 ---- * here will override anything in a lower context, so there's not much * point in searching them all just to save a bit of memory... */ ! v = VarFind(name, ctxt, 0); if (v == NULL) { (void)VarAdd(name, val, ctxt); } else { *************** *** 533,539 **** Buf_AddString(&(v->val), val); if (DEBUG(VAR)) { ! printf("%s:%s = %s\n", ctxt->name, name, val); } } /* --- 554,560 ---- Buf_AddString(&(v->val), val); if (DEBUG(VAR)) { ! printf("%s:%s = %s\n", context_name(ctxt), name, val); } } /* *************** *** 572,585 **** *----------------------------------------------------------------------- */ void ! Var_Append (name, val, ctxt) char *name; /* Name of variable to modify */ char *val; /* String to append to it */ ! GNode *ctxt; /* Context in which this should occur */ { register Var *v; ! v = VarFind (name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0); if (v == NULL) { (void)VarAdd(name, val, ctxt); --- 593,606 ---- *----------------------------------------------------------------------- */ void ! Var_Append(name, val, ctxt) char *name; /* Name of variable to modify */ char *val; /* String to append to it */ ! SymTable *ctxt; /* Context in which this should occur */ { register Var *v; ! v = VarFind(name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0); if (v == NULL) { (void)VarAdd(name, val, ctxt); *************** *** 588,594 **** Buf_AddString(&(v->val), val); if (DEBUG(VAR)) { ! printf("%s:%s = %s\n", ctxt->name, name, VarValue(v)); } } --- 609,615 ---- Buf_AddString(&(v->val), val); if (DEBUG(VAR)) { ! printf("%s:%s = %s\n", context_name(ctxt), name, VarValue(v)); } } *************** *** 610,616 **** Boolean Var_Exists(name, ctxt) char *name; /* Variable to find */ ! GNode *ctxt; /* Context in which to start search */ { Var *v; --- 631,637 ---- Boolean Var_Exists(name, ctxt) char *name; /* Variable to find */ ! SymTable *ctxt; /* Context in which to start search */ { Var *v; *************** *** 637,643 **** char * Var_Value(name, ctxt) char *name; /* name to find */ ! GNode *ctxt; /* context in which to search for it */ { Var *v; --- 658,664 ---- char * Var_Value(name, ctxt) char *name; /* name to find */ ! SymTable *ctxt; /* context in which to search for it */ { Var *v; *************** *** 1348,1354 **** */ static char * VarGetPattern(ctxt, err, tstr, delim, flags, length, pattern) ! GNode *ctxt; int err; char **tstr; int delim; --- 1369,1375 ---- */ static char * VarGetPattern(ctxt, err, tstr, delim, flags, length, pattern) ! SymTable *ctxt; int err; char **tstr; int delim; *************** *** 1476,1484 **** *----------------------------------------------------------------------- */ char * ! Var_Parse (str, ctxt, err, lengthPtr, freePtr) char *str; /* The string to parse */ ! GNode *ctxt; /* The context for the variable */ Boolean err; /* TRUE if undefined variables are an error */ size_t *lengthPtr; /* OUT: The length of the specification */ Boolean *freePtr; /* OUT: TRUE if caller should free result */ --- 1497,1505 ---- *----------------------------------------------------------------------- */ char * ! Var_Parse(str, ctxt, err, lengthPtr, freePtr) char *str; /* The string to parse */ ! SymTable *ctxt; /* The context for the variable */ Boolean err; /* TRUE if undefined variables are an error */ size_t *lengthPtr; /* OUT: The length of the specification */ Boolean *freePtr; /* OUT: TRUE if caller should free result */ *************** *** 2145,2151 **** char * Var_Subst(str, ctxt, undefErr) char *str; /* the string in which to substitute */ ! GNode *ctxt; /* the context wherein to find variables */ Boolean undefErr; /* TRUE if undefineds are an error */ { BUFFER buf; /* Buffer for forming things */ --- 2166,2172 ---- char * Var_Subst(str, ctxt, undefErr) char *str; /* the string in which to substitute */ ! SymTable *ctxt; /* the context wherein to find variables */ Boolean undefErr; /* TRUE if undefineds are an error */ { BUFFER buf; /* Buffer for forming things */ *************** *** 2231,2237 **** Buffer buf; /* Where to store the result */ char *str; /* The string in which to substitute */ const char *var; /* Named variable */ ! GNode *ctxt; /* The context wherein to find variables */ { char *val; /* Value substituted for a variable */ size_t length; /* Length of the variable invocation */ --- 2252,2258 ---- Buffer buf; /* Where to store the result */ char *str; /* The string in which to substitute */ const char *var; /* Named variable */ ! SymTable *ctxt; /* The context wherein to find variables */ { char *val; /* Value substituted for a variable */ size_t length; /* Length of the variable invocation */ *************** *** 2355,2363 **** void Var_Init() { ! VAR_GLOBAL = Targ_NewGN("Global"); ! VAR_CMD = Targ_NewGN("Command"); ! VAR_ENV = Targ_NewGN("Environment"); Lst_Init(&allVars); } --- 2376,2389 ---- void Var_Init() { ! static SymTable global_vars, cmd_vars, env_vars; ! ! VAR_GLOBAL = &global_vars; ! VAR_CMD = &cmd_vars; ! VAR_ENV = &env_vars; ! Lst_Init(VAR_GLOBAL); ! Lst_Init(VAR_CMD); ! Lst_Init(VAR_ENV); Lst_Init(&allVars); } *************** *** 2387,2394 **** *----------------------------------------------------------------------- */ void ! Var_Dump (ctxt) ! GNode *ctxt; { ! Lst_Every(&ctxt->context, VarPrintVar); } --- 2413,2420 ---- *----------------------------------------------------------------------- */ void ! Var_Dump(ctxt) ! SymTable *ctxt; { ! Lst_Every(ctxt, VarPrintVar); }