diff options
Diffstat (limited to 'contrib/bmake/var.c')
-rw-r--r-- | contrib/bmake/var.c | 740 |
1 files changed, 386 insertions, 354 deletions
diff --git a/contrib/bmake/var.c b/contrib/bmake/var.c index 3f10b44a8ccc..80b8d95d82ef 100644 --- a/contrib/bmake/var.c +++ b/contrib/bmake/var.c @@ -1,4 +1,4 @@ -/* $NetBSD: var.c,v 1.641 2020/11/01 23:17:40 rillig Exp $ */ +/* $NetBSD: var.c,v 1.689 2020/11/17 20:11:02 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990, 1993 @@ -138,7 +138,7 @@ #include "metachar.h" /* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ -MAKE_RCSID("$NetBSD: var.c,v 1.641 2020/11/01 23:17:40 rillig Exp $"); +MAKE_RCSID("$NetBSD: var.c,v 1.689 2020/11/17 20:11:02 rillig Exp $"); #define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1) #define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2) @@ -146,7 +146,7 @@ MAKE_RCSID("$NetBSD: var.c,v 1.641 2020/11/01 23:17:40 rillig Exp $"); #define VAR_DEBUG4(fmt, arg1, arg2, arg3, arg4) DEBUG4(VAR, fmt, arg1, arg2, arg3, arg4) ENUM_FLAGS_RTTI_3(VarEvalFlags, - VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN); + VARE_UNDEFERR, VARE_WANTRES, VARE_KEEP_DOLLAR); /* * This lets us tell if we have replaced the original environ @@ -165,11 +165,6 @@ char var_Error[] = ""; * be deferred until it is defined in an actual target. */ static char varUndefined[] = ""; -/* Special return value for Var_Parse, just to avoid allocating empty strings. - * In contrast to var_Error and varUndefined, this is not an error marker but - * just an ordinary successful return value. */ -static char emptyString[] = ""; - /* * Traditionally this make consumed $$ during := like any other expansion. * Other make's do not, and this make follows straight since 2016-01-09. @@ -274,6 +269,7 @@ typedef enum VarExportedMode { static VarExportedMode var_exportedVars = VAR_EXPORTED_NONE; typedef enum VarExportFlags { + VAR_EXPORT_NORMAL = 0, /* * We pass this to Var_Export when doing the initial export * or after updating an exported var. @@ -300,7 +296,7 @@ VarNew(const char *name, void *name_freeIt, const char *value, VarFlags flags) Var *var = bmake_malloc(sizeof *var); var->name = name; var->name_freeIt = name_freeIt; - Buf_Init(&var->val, value_len + 1); + Buf_InitSize(&var->val, value_len + 1); Buf_AddBytes(&var->val, value, value_len); var->flags = flags; return var; @@ -447,7 +443,7 @@ VarFreeEnv(Var *v, Boolean freeValue) /* Add a new variable of the given name and value to the given context. * The name and val arguments are duplicated so they may safely be freed. */ static void -VarAdd(const char *name, const char *val, GNode *ctxt, VarSet_Flags flags) +VarAdd(const char *name, const char *val, GNode *ctxt, VarSetFlags flags) { HashEntry *he = HashTable_CreateEntry(&ctxt->context, name, NULL); Var *v = VarNew(he->key /* aliased */, NULL, val, @@ -531,7 +527,7 @@ Var_Export1(const char *name, VarExportFlags flags) if (!MayExport(name)) return FALSE; - v = VarFind(name, VAR_GLOBAL, 0); + v = VarFind(name, VAR_GLOBAL, FALSE); if (v == NULL) return FALSE; @@ -597,7 +593,7 @@ Var_ExportVars(void) * children see a correctly incremented value. */ char tmp[BUFSIZ]; - snprintf(tmp, sizeof(tmp), "%d", makelevel + 1); + snprintf(tmp, sizeof tmp, "%d", makelevel + 1); setenv(MAKE_LEVEL_ENV, tmp, 1); if (var_exportedVars == VAR_EXPORTED_NONE) @@ -610,7 +606,7 @@ Var_ExportVars(void) HashIter_Init(&hi, &VAR_GLOBAL->context); while (HashIter_Next(&hi) != NULL) { Var *var = hi.entry->value; - Var_Export1(var->name, 0); + Var_Export1(var->name, VAR_EXPORT_NORMAL); } return; } @@ -622,7 +618,7 @@ Var_ExportVars(void) size_t i; for (i = 0; i < words.len; i++) - Var_Export1(words.words[i], 0); + Var_Export1(words.words[i], VAR_EXPORT_NORMAL); Words_Free(words); } free(val); @@ -741,7 +737,7 @@ Var_UnExport(const char *str) Words words = Str_Words(varnames, FALSE); for (i = 0; i < words.len; i++) { const char *varname = words.words[i]; - v = VarFind(varname, VAR_GLOBAL, 0); + v = VarFind(varname, VAR_GLOBAL, FALSE); if (v == NULL) { VAR_DEBUG1("Not unexporting \"%s\" (not found)\n", varname); continue; @@ -780,8 +776,8 @@ Var_UnExport(const char *str) /* See Var_Set for documentation. */ void -Var_Set_with_flags(const char *name, const char *val, GNode *ctxt, - VarSet_Flags flags) +Var_SetWithFlags(const char *name, const char *val, GNode *ctxt, + VarSetFlags flags) { const char *unexpanded_name = name; char *name_freeIt = NULL; @@ -804,7 +800,7 @@ Var_Set_with_flags(const char *name, const char *val, GNode *ctxt, } if (ctxt == VAR_GLOBAL) { - v = VarFind(name, VAR_CMDLINE, 0); + v = VarFind(name, VAR_CMDLINE, FALSE); if (v != NULL) { if (v->flags & VAR_FROM_CMD) { VAR_DEBUG3("%s:%s = %s ignored!\n", ctxt->name, name, val); @@ -819,9 +815,9 @@ Var_Set_with_flags(const char *name, const char *val, GNode *ctxt, * 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); + v = VarFind(name, ctxt, FALSE); if (v == NULL) { - if (ctxt == VAR_CMDLINE && !(flags & VAR_NO_EXPORT)) { + if (ctxt == VAR_CMDLINE && !(flags & VAR_SET_NO_EXPORT)) { /* * This var would normally prevent the same name being added * to VAR_GLOBAL, so delete it from there if needed. @@ -850,9 +846,9 @@ Var_Set_with_flags(const char *name, const char *val, GNode *ctxt, * to the environment (as per POSIX standard) * Other than internals. */ - if (ctxt == VAR_CMDLINE && !(flags & VAR_NO_EXPORT) && name[0] != '.') { + if (ctxt == VAR_CMDLINE && !(flags & VAR_SET_NO_EXPORT) && name[0] != '.') { if (v == NULL) - v = VarFind(name, ctxt, 0); /* we just added it */ + v = VarFind(name, ctxt, FALSE); /* we just added it */ v->flags |= VAR_FROM_CMD; /* @@ -867,7 +863,7 @@ Var_Set_with_flags(const char *name, const char *val, GNode *ctxt, Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL); } if (name[0] == '.' && strcmp(name, MAKE_SAVE_DOLLARS) == 0) - save_dollars = s2Boolean(val, save_dollars); + save_dollars = ParseBoolean(val, save_dollars); out: free(name_freeIt); @@ -902,7 +898,7 @@ out: void Var_Set(const char *name, const char *val, GNode *ctxt) { - Var_Set_with_flags(name, val, ctxt, 0); + Var_SetWithFlags(name, val, ctxt, VAR_SET_NONE); } /*- @@ -965,8 +961,6 @@ Var_Append(const char *name, const char *val, GNode *ctxt) ctxt->name, name, Buf_GetAll(&v->val, NULL)); if (v->flags & VAR_FROM_ENV) { - HashEntry *h; - /* * If the original variable came from the environment, we * have to install it in the global context (we could place @@ -974,8 +968,9 @@ Var_Append(const char *name, const char *val, GNode *ctxt) * export other variables...) */ v->flags &= ~(unsigned)VAR_FROM_ENV; - h = HashTable_CreateEntry(&ctxt->context, name, NULL); - HashEntry_Set(h, v); + /* This is the only place where a variable is created whose + * v->name is not the same as ctxt->context->key. */ + HashTable_Set(&ctxt->context, name, v); } } free(name_freeIt); @@ -1061,7 +1056,7 @@ typedef struct SepBuf { static void SepBuf_Init(SepBuf *buf, char sep) { - Buf_Init(&buf->buf, 32 /* bytes */); + Buf_InitSize(&buf->buf, 32); buf->needSep = FALSE; buf->sep = sep; } @@ -1354,9 +1349,9 @@ nosub: #ifndef NO_REGEX /* Print the error caused by a regcomp or regexec call. */ static void -VarREError(int reerr, regex_t *pat, const char *str) +VarREError(int reerr, const regex_t *pat, const char *str) { - size_t errlen = regerror(reerr, pat, 0, 0); + size_t errlen = regerror(reerr, pat, NULL, 0); char *errbuf = bmake_malloc(errlen); regerror(reerr, pat, errbuf, errlen); Error("%s: %s", str, errbuf); @@ -1470,7 +1465,7 @@ ModifyWord_Loop(const char *word, SepBuf *buf, void *data) return; args = data; - Var_Set_with_flags(args->tvar, word, args->ctx, VAR_NO_EXPORT); + Var_SetWithFlags(args->tvar, word, args->ctx, VAR_SET_NO_EXPORT); (void)Var_Subst(args->str, args->ctx, args->eflags, &s); /* TODO: handle errors */ @@ -1501,7 +1496,7 @@ VarSelectWords(char sep, Boolean oneBigWord, const char *str, int first, if (oneBigWord) { /* fake what Str_Words() would do if there were only one word */ words.len = 1; - words.words = bmake_malloc((words.len + 1) * sizeof(char *)); + words.words = bmake_malloc((words.len + 1) * sizeof(words.words[0])); words.freeIt = bmake_strdup(str); words.words[0] = words.freeIt; words.words[1] = NULL; @@ -1609,7 +1604,7 @@ Words_JoinFree(Words words) Buffer buf; size_t i; - Buf_Init(&buf, 0); + Buf_Init(&buf); for (i = 0; i < words.len; i++) { if (i != 0) @@ -1646,7 +1641,7 @@ static char * VarQuote(const char *str, Boolean quoteDollar) { Buffer buf; - Buf_Init(&buf, 0); + Buf_Init(&buf); for (; *str != '\0'; str++) { if (*str == '\n') { @@ -1733,19 +1728,24 @@ VarStrftime(const char *fmt, Boolean zulu, time_t tim) { char buf[BUFSIZ]; - if (!tim) + if (tim == 0) time(&tim); - if (!*fmt) + if (*fmt == '\0') fmt = "%c"; - strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&tim) : localtime(&tim)); + strftime(buf, sizeof buf, fmt, zulu ? gmtime(&tim) : localtime(&tim)); - buf[sizeof(buf) - 1] = '\0'; + buf[sizeof buf - 1] = '\0'; return bmake_strdup(buf); } -/* The ApplyModifier functions all work in the same way. They get the - * current parsing position (pp) and parse the modifier from there. The - * modifier typically lasts until the next ':', or a closing '}' or ')' +/* + * The ApplyModifier functions take an expression that is being evaluated. + * Their task is to apply a single modifier to the expression. + * To do this, they parse the modifier and its parameters from pp and apply + * the parsed modifier to the current value of the expression, generating a + * new value from it. + * + * The modifier typically lasts until the next ':', or a closing '}' or ')' * (taken from st->endc), or the end of the string (parse error). * * The high-level behavior of these functions is: @@ -1758,7 +1758,11 @@ VarStrftime(const char *fmt, Boolean zulu, time_t tim) * * If parsing succeeds, the parsing position *pp is updated to point to the * first character following the modifier, which typically is either ':' or - * st->endc. + * st->endc. The modifier doesn't have to check for this delimiter character, + * this is done by ApplyModifiers. + * + * XXX: As of 2020-11-15, some modifiers such as :S, :C, :P, :L do not + * need to be followed by a ':' or endc; this was an unintended mistake. * * If parsing fails because of a missing delimiter (as in the :S, :C or :@ * modifiers), return AMR_CLEANUP. @@ -1767,8 +1771,8 @@ VarStrftime(const char *fmt, Boolean zulu, time_t tim) * try the SysV modifier ${VAR:from=to} as fallback. This should only be * done as long as there have been no side effects from evaluating nested * variables, to avoid evaluating them more than once. In this case, the - * parsing position must not be updated. (XXX: Why not? The original parsing - * position is well-known in ApplyModifiers.) + * parsing position may or may not be updated. (XXX: Why not? The original + * parsing position is well-known in ApplyModifiers.) * * If parsing fails and the SysV modifier ${VAR:from=to} should not be used * as a fallback, either issue an error message using Error or Parse_Error @@ -1786,7 +1790,7 @@ VarStrftime(const char *fmt, Boolean zulu, time_t tim) * during parsing though. * * Evaluating the modifier usually takes the current value of the variable - * expression from st->val, or the variable name from st->v->name and stores + * expression from st->val, or the variable name from st->var->name and stores * the result in st->newVal. * * If evaluating fails (as of 2020-08-23), an error message is printed using @@ -1819,7 +1823,7 @@ ENUM_FLAGS_RTTI_2(VarExprFlags, typedef struct ApplyModifiersState { const char startc; /* '\0' or '{' or '(' */ const char endc; /* '\0' or '}' or ')' */ - Var * const v; + Var * const var; GNode * const ctxt; const VarEvalFlags eflags; @@ -1896,7 +1900,7 @@ ParseModifierPart( Buffer buf; const char *p; - Buf_Init(&buf, 0); + Buf_Init(&buf); /* * Skim through until the matching delimiter is found; pick up variable @@ -1934,7 +1938,7 @@ ParseModifierPart( const char *nested_p = p; const char *nested_val; void *nested_val_freeIt; - VarEvalFlags nested_eflags = eflags & ~(unsigned)VARE_ASSIGN; + VarEvalFlags nested_eflags = eflags & ~(unsigned)VARE_KEEP_DOLLAR; (void)Var_Parse(&nested_p, st->ctxt, nested_eflags, &nested_val, &nested_val_freeIt); @@ -1982,7 +1986,8 @@ ParseModifierPart( if (*p != delim) { *pp = p; - Error("Unfinished modifier for %s ('%c' missing)", st->v->name, delim); + Error("Unfinished modifier for %s ('%c' missing)", + st->var->name, delim); *out_part = NULL; return VPR_PARSE_MSG; } @@ -1997,7 +2002,7 @@ ParseModifierPart( } /* Test whether mod starts with modname, followed by a delimiter. */ -static Boolean +MAKE_INLINE Boolean ModMatch(const char *mod, const char *modname, char endc) { size_t n = strlen(modname); @@ -2006,7 +2011,7 @@ ModMatch(const char *mod, const char *modname, char endc) } /* Test whether mod starts with modname, followed by a delimiter or '='. */ -static inline Boolean +MAKE_INLINE Boolean ModMatchEq(const char *mod, const char *modname, char endc) { size_t n = strlen(modname); @@ -2080,35 +2085,35 @@ ApplyModifier_Loop(const char **pp, ApplyModifiersState *st) { struct ModifyWord_LoopArgs args; char prev_sep; - VarEvalFlags eflags = st->eflags & ~(unsigned)VARE_WANTRES; VarParseResult res; args.ctx = st->ctxt; (*pp)++; /* Skip the first '@' */ - res = ParseModifierPart(pp, '@', eflags, st, + res = ParseModifierPart(pp, '@', VARE_NONE, st, &args.tvar, NULL, NULL, NULL); if (res != VPR_OK) return AMR_CLEANUP; - if (DEBUG(LINT) && strchr(args.tvar, '$') != NULL) { + if (opts.lint && strchr(args.tvar, '$') != NULL) { Parse_Error(PARSE_FATAL, "In the :@ modifier of \"%s\", the variable name \"%s\" " "must not contain a dollar.", - st->v->name, args.tvar); + st->var->name, args.tvar); return AMR_CLEANUP; } - res = ParseModifierPart(pp, '@', eflags, st, + res = ParseModifierPart(pp, '@', VARE_NONE, st, &args.str, NULL, NULL, NULL); if (res != VPR_OK) return AMR_CLEANUP; - args.eflags = st->eflags & (VARE_UNDEFERR | VARE_WANTRES); + args.eflags = st->eflags & ~(unsigned)VARE_KEEP_DOLLAR; prev_sep = st->sep; st->sep = ' '; /* XXX: should be st->sep for consistency */ st->newVal = ModifyWords(st->val, ModifyWord_Loop, &args, st->oneBigWord, st->sep); st->sep = prev_sep; + /* XXX: Consider restoring the previous variable instead of deleting. */ Var_Delete(args.tvar, st->ctxt); free(args.tvar); free(args.str); @@ -2122,16 +2127,19 @@ ApplyModifier_Defined(const char **pp, ApplyModifiersState *st) Buffer buf; const char *p; - VarEvalFlags eflags = st->eflags & ~(unsigned)VARE_WANTRES; - if (st->eflags & VARE_WANTRES) { + VarEvalFlags eflags = VARE_NONE; + if (st->eflags & VARE_WANTRES) if ((**pp == 'D') == !(st->exprFlags & VEF_UNDEF)) - eflags |= VARE_WANTRES; - } + eflags = st->eflags; - Buf_Init(&buf, 0); + Buf_Init(&buf); p = *pp + 1; while (*p != st->endc && *p != ':' && *p != '\0') { + /* XXX: This code is similar to the one in Var_Parse. + * See if the code can be merged. + * See also ApplyModifier_Match. */ + /* Escaped delimiter or other special character */ if (*p == '\\') { char c = p[1]; @@ -2177,7 +2185,7 @@ static ApplyModifierResult ApplyModifier_Literal(const char **pp, ApplyModifiersState *st) { ApplyModifiersState_Define(st); - st->newVal = bmake_strdup(st->v->name); + st->newVal = bmake_strdup(st->var->name); (*pp)++; return AMR_OK; } @@ -2272,17 +2280,17 @@ ApplyModifier_Path(const char **pp, ApplyModifiersState *st) ApplyModifiersState_Define(st); - gn = Targ_FindNode(st->v->name); + gn = Targ_FindNode(st->var->name); if (gn == NULL || gn->type & OP_NOPATH) { path = NULL; } else if (gn->path != NULL) { path = bmake_strdup(gn->path); } else { SearchPath *searchPath = Suff_FindPath(gn); - path = Dir_FindFile(st->v->name, searchPath); + path = Dir_FindFile(st->var->name, searchPath); } if (path == NULL) - path = bmake_strdup(st->v->name); + path = bmake_strdup(st->var->name); st->newVal = path; (*pp)++; @@ -2307,11 +2315,10 @@ ApplyModifier_ShellCommand(const char **pp, ApplyModifiersState *st) if (st->eflags & VARE_WANTRES) st->newVal = Cmd_Exec(cmd, &errfmt); else - st->newVal = emptyString; - free(cmd); - + st->newVal = bmake_strdup(""); if (errfmt != NULL) - Error(errfmt, st->val); /* XXX: why still return AMR_OK? */ + Error(errfmt, cmd); /* XXX: why still return AMR_OK? */ + free(cmd); ApplyModifiersState_Define(st); return AMR_OK; @@ -2348,7 +2355,7 @@ ApplyModifier_Range(const char **pp, ApplyModifiersState *st) Words_Free(words); } - Buf_Init(&buf, 0); + Buf_Init(&buf); for (i = 0; i < n; i++) { if (i != 0) @@ -2374,8 +2381,11 @@ ApplyModifier_Match(const char **pp, ApplyModifiersState *st) /* * In the loop below, ignore ':' unless we are at (or back to) the * original brace level. - * XXX This will likely not work right if $() and ${} are intermixed. + * XXX: This will likely not work right if $() and ${} are intermixed. */ + /* XXX: This code is similar to the one in Var_Parse. + * See if the code can be merged. + * See also ApplyModifier_Defined. */ int nest = 0; const char *p; for (p = mod + 1; *p != '\0' && !(*p == ':' && nest == 0); p++) { @@ -2428,7 +2438,8 @@ ApplyModifier_Match(const char **pp, ApplyModifiersState *st) free(old_pattern); } - VAR_DEBUG3("Pattern[%s] for [%s] is [%s]\n", st->v->name, st->val, pattern); + VAR_DEBUG3("Pattern[%s] for [%s] is [%s]\n", + st->var->name, st->val, pattern); callback = mod[0] == 'M' ? ModifyWord_Match : ModifyWord_NoMatch; st->newVal = ModifyWords(st->val, callback, pattern, @@ -2754,7 +2765,7 @@ ApplyModifier_Words(const char **pp, ApplyModifiersState *st) size_t ac = words.len; Words_Free(words); - Buf_Init(&buf, 4); /* 3 digits + '\0' is usually enough */ + Buf_InitSize(&buf, 4); /* 3 digits + '\0' is usually enough */ Buf_AddInt(&buf, (int)ac); st->newVal = Buf_Destroy(&buf, FALSE); } @@ -2842,7 +2853,7 @@ ApplyModifier_Order(const char **pp, ApplyModifiersState *st) if (mod[1] == st->endc || mod[1] == ':') { /* :O sorts ascending */ - qsort(words.words, words.len, sizeof(char *), str_cmp_asc); + qsort(words.words, words.len, sizeof words.words[0], str_cmp_asc); } else if ((mod[1] == 'r' || mod[1] == 'x') && (mod[2] == st->endc || mod[2] == ':')) { @@ -2850,7 +2861,7 @@ ApplyModifier_Order(const char **pp, ApplyModifiersState *st) if (mod[1] == 'r') { /* :Or sorts descending */ - qsort(words.words, words.len, sizeof(char *), str_cmp_desc); + qsort(words.words, words.len, sizeof words.words[0], str_cmp_desc); } else { /* :Ox shuffles @@ -2885,16 +2896,16 @@ ApplyModifier_IfElse(const char **pp, ApplyModifiersState *st) VarParseResult res; Boolean value = FALSE; - VarEvalFlags then_eflags = st->eflags & ~(unsigned)VARE_WANTRES; - VarEvalFlags else_eflags = st->eflags & ~(unsigned)VARE_WANTRES; + VarEvalFlags then_eflags = VARE_NONE; + VarEvalFlags else_eflags = VARE_NONE; int cond_rc = COND_PARSE; /* anything other than COND_INVALID */ if (st->eflags & VARE_WANTRES) { - cond_rc = Cond_EvalCondition(st->v->name, &value); + cond_rc = Cond_EvalCondition(st->var->name, &value); if (cond_rc != COND_INVALID && value) - then_eflags |= VARE_WANTRES; + then_eflags = st->eflags; if (cond_rc != COND_INVALID && !value) - else_eflags |= VARE_WANTRES; + else_eflags = st->eflags; } (*pp)++; /* skip past the '?' */ @@ -2911,7 +2922,7 @@ ApplyModifier_IfElse(const char **pp, ApplyModifiersState *st) (*pp)--; if (cond_rc == COND_INVALID) { Error("Bad conditional expression `%s' in %s?%s:%s", - st->v->name, st->v->name, then_expr, else_expr); + st->var->name, st->var->name, then_expr, else_expr); return AMR_CLEANUP; } @@ -2965,14 +2976,14 @@ ApplyModifier_Assign(const char **pp, ApplyModifiersState *st) return AMR_UNKNOWN; /* "::<unrecognised>" */ ok: - if (st->v->name[0] == '\0') { + if (st->var->name[0] == '\0') { *pp = mod + 1; return AMR_BAD; } v_ctxt = st->ctxt; /* context where v belongs */ if (!(st->exprFlags & VEF_UNDEF) && st->ctxt != VAR_GLOBAL) { - Var *gv = VarFind(st->v->name, st->ctxt, 0); + Var *gv = VarFind(st->var->name, st->ctxt, FALSE); if (gv == NULL) v_ctxt = VAR_GLOBAL; else @@ -3000,7 +3011,7 @@ ok: if (st->eflags & VARE_WANTRES) { switch (op[0]) { case '+': - Var_Append(st->v->name, val, v_ctxt); + Var_Append(st->var->name, val, v_ctxt); break; case '!': { const char *errfmt; @@ -3008,7 +3019,7 @@ ok: if (errfmt) Error(errfmt, val); else - Var_Set(st->v->name, cmd_output, v_ctxt); + Var_Set(st->var->name, cmd_output, v_ctxt); free(cmd_output); break; } @@ -3017,12 +3028,12 @@ ok: break; /* FALLTHROUGH */ default: - Var_Set(st->v->name, val, v_ctxt); + Var_Set(st->var->name, val, v_ctxt); break; } } free(val); - st->newVal = emptyString; + st->newVal = bmake_strdup(""); return AMR_OK; } @@ -3146,7 +3157,7 @@ ApplyModifier_SunShell(const char **pp, ApplyModifiersState *st) if (errfmt) Error(errfmt, st->val); } else - st->newVal = emptyString; + st->newVal = bmake_strdup(""); *pp = p + 2; return AMR_OK; } else @@ -3166,11 +3177,11 @@ LogBeforeApply(const ApplyModifiersState *st, const char *mod, const char endc) /* At this point, only the first character of the modifier can * be used since the end of the modifier is not yet known. */ debug_printf("Applying ${%s:%c%s} to \"%s\" (%s, %s, %s)\n", - st->v->name, mod[0], is_single_char ? "" : "...", st->val, + st->var->name, mod[0], is_single_char ? "" : "...", st->val, Enum_FlagsToString(eflags_str, sizeof eflags_str, st->eflags, VarEvalFlags_ToStringSpecs), Enum_FlagsToString(vflags_str, sizeof vflags_str, - st->v->flags, VarFlags_ToStringSpecs), + st->var->flags, VarFlags_ToStringSpecs), Enum_FlagsToString(exprflags_str, sizeof exprflags_str, st->exprFlags, VarExprFlags_ToStringSpecs)); @@ -3186,11 +3197,11 @@ LogAfterApply(ApplyModifiersState *st, const char *p, const char *mod) const char *newVal = st->newVal == var_Error ? "error" : st->newVal; debug_printf("Result of ${%s:%.*s} is %s%s%s (%s, %s, %s)\n", - st->v->name, (int)(p - mod), mod, quot, newVal, quot, + st->var->name, (int)(p - mod), mod, quot, newVal, quot, Enum_FlagsToString(eflags_str, sizeof eflags_str, st->eflags, VarEvalFlags_ToStringSpecs), Enum_FlagsToString(vflags_str, sizeof vflags_str, - st->v->flags, VarFlags_ToStringSpecs), + st->var->flags, VarFlags_ToStringSpecs), Enum_FlagsToString(exprflags_str, sizeof exprflags_str, st->exprFlags, VarExprFlags_ToStringSpecs)); @@ -3272,20 +3283,18 @@ typedef enum ApplyModifiersIndirectResult { } ApplyModifiersIndirectResult; /* While expanding a variable expression, expand and apply indirect - * modifiers. */ + * modifiers such as in ${VAR:${M_indirect}}. */ static ApplyModifiersIndirectResult ApplyModifiersIndirect( ApplyModifiersState *const st, - const char **inout_p, - void **const out_freeIt + const char **const inout_p, + void **const inout_freeIt ) { const char *p = *inout_p; - const char *nested_p = p; - void *freeIt; - const char *rval; - char c; + const char *mods; + void *mods_freeIt; - (void)Var_Parse(&nested_p, st->ctxt, st->eflags, &rval, &freeIt); + (void)Var_Parse(&p, st->ctxt, st->eflags, &mods, &mods_freeIt); /* TODO: handle errors */ /* @@ -3293,44 +3302,41 @@ ApplyModifiersIndirect( * interested. This means the expression ${VAR:${M_1}${M_2}} * is not accepted, but ${VAR:${M_1}:${M_2}} is. */ - if (rval[0] != '\0' && - (c = *nested_p) != '\0' && c != ':' && c != st->endc) { - if (DEBUG(LINT)) + if (mods[0] != '\0' && *p != '\0' && *p != ':' && *p != st->endc) { + if (opts.lint) Parse_Error(PARSE_FATAL, "Missing delimiter ':' after indirect modifier \"%.*s\"", - (int)(nested_p - p), p); + (int)(p - *inout_p), *inout_p); - free(freeIt); + free(mods_freeIt); /* XXX: apply_mods doesn't sound like "not interested". */ - /* XXX: Why is the indirect modifier parsed again by + /* XXX: Why is the indirect modifier parsed once more by * apply_mods? If any, p should be advanced to nested_p. */ return AMIR_APPLY_MODS; } VAR_DEBUG3("Indirect modifier \"%s\" from \"%.*s\"\n", - rval, (int)(size_t)(nested_p - p), p); - - p = nested_p; - - if (rval[0] != '\0') { - const char *rval_pp = rval; - st->val = ApplyModifiers(&rval_pp, st->val, '\0', '\0', st->v, - &st->exprFlags, st->ctxt, st->eflags, out_freeIt); - if (st->val == var_Error - || (st->val == varUndefined && !(st->eflags & VARE_UNDEFERR)) - || *rval_pp != '\0') { - free(freeIt); + mods, (int)(p - *inout_p), *inout_p); + + if (mods[0] != '\0') { + const char *rval_pp = mods; + st->val = ApplyModifiers(&rval_pp, st->val, '\0', '\0', st->var, + &st->exprFlags, st->ctxt, st->eflags, + inout_freeIt); + if (st->val == var_Error || st->val == varUndefined || + *rval_pp != '\0') { + free(mods_freeIt); *inout_p = p; return AMIR_OUT; /* error already reported */ } } - free(freeIt); + free(mods_freeIt); if (*p == ':') p++; else if (*p == '\0' && st->endc != '\0') { Error("Unclosed variable specification after complex " - "modifier (expecting '%c') for %s", st->endc, st->v->name); + "modifier (expecting '%c') for %s", st->endc, st->var->name); *inout_p = p; return AMIR_OUT; } @@ -3342,15 +3348,15 @@ ApplyModifiersIndirect( /* Apply any modifiers (such as :Mpattern or :@var@loop@ or :Q or ::=value). */ static char * ApplyModifiers( - const char **pp, /* the parsing position, updated upon return */ + const char **const pp, /* the parsing position, updated upon return */ char *const val, /* the current value of the expression */ char const startc, /* '(' or '{', or '\0' for indirect modifiers */ char const endc, /* ')' or '}', or '\0' for indirect modifiers */ - Var * const v, - VarExprFlags *exprFlags, - GNode * const ctxt, /* for looking up and modifying variables */ + Var *const v, + VarExprFlags *const exprFlags, + GNode *const ctxt, /* for looking up and modifying variables */ VarEvalFlags const eflags, - void ** const out_freeIt /* free this after using the return value */ + void **const inout_freeIt /* free this after using the return value */ ) { ApplyModifiersState st = { startc, endc, v, ctxt, eflags, @@ -3369,11 +3375,18 @@ ApplyModifiers( assert(val != NULL); p = *pp; + + if (*p == '\0' && endc != '\0') { + Error("Unclosed variable expression (expecting '%c') for \"%s\"", + st.endc, st.var->name); + goto cleanup; + } + while (*p != '\0' && *p != endc) { if (*p == '$') { ApplyModifiersIndirectResult amir; - amir = ApplyModifiersIndirect(&st, &p, out_freeIt); + amir = ApplyModifiersIndirect(&st, &p, inout_freeIt); if (amir == AMIR_CONTINUE) continue; if (amir == AMIR_OUT) @@ -3396,6 +3409,9 @@ ApplyModifiers( if (res == AMR_UNKNOWN) { Error("Unknown modifier '%c'", *mod); + /* Guess the end of the current modifier. + * XXX: Skipping the rest of the modifier hides errors and leads + * to wrong results. Parsing should rather stop here. */ for (p++; *p != ':' && *p != st.endc && *p != '\0'; p++) continue; st.newVal = var_Error; @@ -3409,26 +3425,25 @@ ApplyModifiers( LogAfterApply(&st, p, mod); if (st.newVal != st.val) { - if (*out_freeIt) { + if (*inout_freeIt != NULL) { free(st.val); - *out_freeIt = NULL; + *inout_freeIt = NULL; } st.val = st.newVal; - if (st.val != var_Error && st.val != varUndefined && - st.val != emptyString) { - *out_freeIt = st.val; - } + if (st.val != var_Error && st.val != varUndefined) + *inout_freeIt = st.val; } if (*p == '\0' && st.endc != '\0') { Error("Unclosed variable specification (expecting '%c') " "for \"%s\" (value \"%s\") modifier %c", - st.endc, st.v->name, st.val, *mod); + st.endc, st.var->name, st.val, *mod); } else if (*p == ':') { p++; - } else if (DEBUG(LINT) && *p != '\0' && *p != endc) { + } else if (opts.lint && *p != '\0' && *p != endc) { Parse_Error(PARSE_FATAL, "Missing delimiter ':' after modifier \"%.*s\"", (int)(p - mod), mod); + /* TODO: propagate parse error to the enclosing expression */ } } out: @@ -3438,13 +3453,14 @@ out: return st.val; bad_modifier: + /* XXX: The modifier end is only guessed. */ Error("Bad modifier `:%.*s' for %s", - (int)strcspn(mod, ":)}"), mod, st.v->name); + (int)strcspn(mod, ":)}"), mod, st.var->name); cleanup: *pp = p; - free(*out_freeIt); - *out_freeIt = NULL; + free(*inout_freeIt); + *inout_freeIt = NULL; *exprFlags = st.exprFlags; return var_Error; } @@ -3513,7 +3529,7 @@ ParseVarname(const char **pp, char startc, char endc, const char *p = *pp; int depth = 1; - Buf_Init(&buf, 0); + Buf_Init(&buf); while (*p != '\0') { /* Track depth so we can spot parse errors. */ @@ -3528,12 +3544,12 @@ ParseVarname(const char **pp, char startc, char endc, /* A variable inside a variable, expand. */ if (*p == '$') { - void *freeIt; - const char *rval; - (void)Var_Parse(&p, ctxt, eflags, &rval, &freeIt); + const char *nested_val; + void *nested_val_freeIt; + (void)Var_Parse(&p, ctxt, eflags, &nested_val, &nested_val_freeIt); /* TODO: handle errors */ - Buf_AddStr(&buf, rval); - free(freeIt); + Buf_AddStr(&buf, nested_val); + free(nested_val_freeIt); } else { Buf_AddByte(&buf, *p); p++; @@ -3544,7 +3560,7 @@ ParseVarname(const char **pp, char startc, char endc, return Buf_Destroy(&buf, FALSE); } -static Boolean +static VarParseResult ValidShortVarname(char varname, const char *start) { switch (varname) { @@ -3555,11 +3571,11 @@ ValidShortVarname(char varname, const char *start) case '$': break; /* and continue below */ default: - return TRUE; + return VPR_OK; } - if (!DEBUG(LINT)) - return FALSE; + if (!opts.lint) + return VPR_PARSE_SILENT; if (varname == '$') Parse_Error(PARSE_FATAL, @@ -3570,23 +3586,20 @@ ValidShortVarname(char varname, const char *start) Parse_Error(PARSE_FATAL, "Invalid variable name '%c', at \"%s\"", varname, start); - return FALSE; + return VPR_PARSE_MSG; } /* Parse a single-character variable name such as $V or $@. * Return whether to continue parsing. */ static Boolean -ParseVarnameShort( - char startc, - const char **pp, - GNode *ctxt, - VarEvalFlags eflags, - VarParseResult *out_FALSE_res, - const char **out_FALSE_val, - Var **out_TRUE_var -) { +ParseVarnameShort(char startc, const char **pp, GNode *ctxt, + VarEvalFlags eflags, + VarParseResult *out_FALSE_res, const char **out_FALSE_val, + Var **out_TRUE_var) +{ char name[2]; Var *v; + VarParseResult vpr; /* * If it's not bounded by braces of some sort, life is much simpler. @@ -3594,10 +3607,11 @@ ParseVarnameShort( * value if it exists. */ - if (!ValidShortVarname(startc, *pp)) { + vpr = ValidShortVarname(startc, *pp); + if (vpr != VPR_OK) { (*pp)++; *out_FALSE_val = var_Error; - *out_FALSE_res = VPR_PARSE_MSG; + *out_FALSE_res = vpr; return FALSE; } @@ -3608,7 +3622,7 @@ ParseVarnameShort( *pp += 2; *out_FALSE_val = UndefinedShortVarValue(startc, ctxt, eflags); - if (DEBUG(LINT) && *out_FALSE_val == var_Error) { + if (opts.lint && *out_FALSE_val == var_Error) { Parse_Error(PARSE_FATAL, "Variable \"%s\" is undefined", name); *out_FALSE_res = VPR_UNDEF_MSG; return FALSE; @@ -3621,20 +3635,83 @@ ParseVarnameShort( return TRUE; } +/* Find variables like @F or <D. */ +static Var * +FindLocalLegacyVar(const char *varname, size_t namelen, GNode *ctxt, + const char **out_extraModifiers) +{ + /* Only resolve these variables if ctxt is a "real" target. */ + if (ctxt == VAR_CMDLINE || ctxt == VAR_GLOBAL) + return NULL; + + if (namelen != 2) + return NULL; + if (varname[1] != 'F' && varname[1] != 'D') + return NULL; + if (strchr("@%?*!<>", varname[0]) == NULL) + return NULL; + + { + char name[] = { varname[0], '\0' }; + Var *v = VarFind(name, ctxt, FALSE); + + if (v != NULL) { + if (varname[1] == 'D') { + *out_extraModifiers = "H:"; + } else { /* F */ + *out_extraModifiers = "T:"; + } + } + return v; + } +} + +static VarParseResult +EvalUndefined(Boolean dynamic, const char *start, const char *p, char *varname, + VarEvalFlags eflags, + void **out_freeIt, const char **out_val) +{ + if (dynamic) { + char *pstr = bmake_strsedup(start, p); + free(varname); + *out_freeIt = pstr; + *out_val = pstr; + return VPR_OK; + } + + if ((eflags & VARE_UNDEFERR) && opts.lint) { + Parse_Error(PARSE_FATAL, "Variable \"%s\" is undefined", varname); + free(varname); + *out_val = var_Error; + return VPR_UNDEF_MSG; + } + + if (eflags & VARE_UNDEFERR) { + free(varname); + *out_val = var_Error; + return VPR_UNDEF_SILENT; + } + + free(varname); + *out_val = varUndefined; + return VPR_OK; +} + /* Parse a long variable name enclosed in braces or parentheses such as $(VAR) * or ${VAR}, up to the closing brace or parenthesis, or in the case of * ${VAR:Modifiers}, up to the ':' that starts the modifiers. * Return whether to continue parsing. */ static Boolean ParseVarnameLong( - const char **pp, + const char *p, char startc, GNode *ctxt, VarEvalFlags eflags, + const char **out_FALSE_pp, VarParseResult *out_FALSE_res, const char **out_FALSE_val, - void **out_FALSE_freePtr, + void **out_FALSE_freeIt, char *out_TRUE_endc, const char **out_TRUE_p, @@ -3650,10 +3727,10 @@ ParseVarnameLong( Boolean haveModifier; Boolean dynamic = FALSE; - const char *const start = *pp; + const char *const start = p; char endc = startc == '(' ? ')' : '}'; - const char *p = start + 2; + p += 2; /* skip "${" or "$(" or "y(" */ varname = ParseVarname(&p, startc, endc, ctxt, eflags, &namelen); if (*p == ':') { @@ -3662,8 +3739,8 @@ ParseVarnameLong( haveModifier = FALSE; } else { Parse_Error(PARSE_FATAL, "Unclosed variable \"%s\"", varname); - *pp = p; free(varname); + *out_FALSE_pp = p; *out_FALSE_val = var_Error; *out_FALSE_res = VPR_PARSE_MSG; return FALSE; @@ -3674,28 +3751,8 @@ ParseVarnameLong( /* At this point, p points just after the variable name, * either at ':' or at endc. */ - /* - * Check also for bogus D and F forms of local variables since we're - * in a local context and the name is the right length. - */ - if (v == NULL && ctxt != VAR_CMDLINE && ctxt != VAR_GLOBAL && - namelen == 2 && (varname[1] == 'F' || varname[1] == 'D') && - strchr("@%?*!<>", varname[0]) != NULL) - { - /* - * Well, it's local -- go look for it. - */ - char name[] = { varname[0], '\0' }; - v = VarFind(name, ctxt, 0); - - if (v != NULL) { - if (varname[1] == 'D') { - *out_TRUE_extraModifiers = "H:"; - } else { /* F */ - *out_TRUE_extraModifiers = "T:"; - } - } - } + if (v == NULL) + v = FindLocalLegacyVar(varname, namelen, ctxt, out_TRUE_extraModifiers); if (v == NULL) { /* Defer expansion of dynamic variables if they appear in non-local @@ -3705,37 +3762,9 @@ ParseVarnameLong( if (!haveModifier) { p++; /* skip endc */ - *pp = p; - if (dynamic) { - char *pstr = bmake_strsedup(start, p); - free(varname); - *out_FALSE_res = VPR_OK; - *out_FALSE_freePtr = pstr; - *out_FALSE_val = pstr; - return FALSE; - } - - if ((eflags & VARE_UNDEFERR) && (eflags & VARE_WANTRES) && - DEBUG(LINT)) - { - Parse_Error(PARSE_FATAL, "Variable \"%s\" is undefined", - varname); - free(varname); - *out_FALSE_res = VPR_UNDEF_MSG; - *out_FALSE_val = var_Error; - return FALSE; - } - - if (eflags & VARE_UNDEFERR) { - free(varname); - *out_FALSE_res = VPR_UNDEF_SILENT; - *out_FALSE_val = var_Error; - return FALSE; - } - - free(varname); - *out_FALSE_res = VPR_OK; - *out_FALSE_val = varUndefined; + *out_FALSE_pp = p; + *out_FALSE_res = EvalUndefined(dynamic, start, p, varname, eflags, + out_FALSE_freeIt, out_FALSE_val); return FALSE; } @@ -3763,56 +3792,51 @@ ParseVarnameLong( return TRUE; } -/*- - *----------------------------------------------------------------------- - * Var_Parse -- - * Given the start of a variable expression (such as $v, $(VAR), - * ${VAR:Mpattern}), extract the variable name, possibly some - * modifiers and find its value by applying the modifiers to the - * original value. - * - * When parsing a condition in ParseEmptyArg, pp may also point to - * the "y" of "empty(VARNAME:Modifiers)", which is syntactically - * identical. +/* + * Given the start of a variable expression (such as $v, $(VAR), + * ${VAR:Mpattern}), extract the variable name and value, and the modifiers, + * if any. While doing that, apply the modifiers to the value of the + * expression, forming its final value. A few of the modifiers such as :!cmd! + * or ::= have side effects. * * Input: - * str The string to parse - * ctxt The context for the variable - * flags Select the exact details of parsing - * out_val_freeIt Must be freed by the caller after using out_val - * - * Results: - * Returns the value of the variable expression, never NULL. - * Returns var_Error if there was a parse error and VARE_UNDEFERR was - * set. - * Returns varUndefined if there was an undefined variable and - * VARE_UNDEFERR was not set. - * - * Parsing should continue at *pp. - * TODO: Document the value of *pp on parse errors. It might be advanced - * by 0, or +1, or the index of the parse error, or the guessed end of the - * variable expression. - * - * If var_Error is returned, a diagnostic may or may not have been - * printed. XXX: This is inconsistent. - * - * If varUndefined is returned, a diagnostic may or may not have been - * printed. XXX: This is inconsistent. - * - * After using the returned value, *out_val_freeIt must be freed, - * preferably using bmake_free since it is NULL in most cases. + * *pp The string to parse. + * When parsing a condition in ParseEmptyArg, it may also + * point to the "y" of "empty(VARNAME:Modifiers)", which + * is syntactically the same. + * ctxt The context for finding variables + * eflags Control the exact details of parsing * - * Side Effects: - * Any effects from the modifiers, such as :!cmd! or ::=value. - *----------------------------------------------------------------------- + * Output: + * *pp The position where to continue parsing. + * TODO: After a parse error, the value of *pp is + * unspecified. It may not have been updated at all, + * point to some random character in the string, to the + * location of the parse error, or at the end of the + * string. + * *out_val The value of the variable expression, never NULL. + * *out_val var_Error if there was a parse error. + * *out_val var_Error if the base variable of the expression was + * undefined, eflags contains VARE_UNDEFERR, and none of + * the modifiers turned the undefined expression into a + * defined expression. + * XXX: It is not guaranteed that an error message has + * been printed. + * *out_val varUndefined if the base variable of the expression + * was undefined, eflags did not contain VARE_UNDEFERR, + * and none of the modifiers turned the undefined + * expression into a defined expression. + * XXX: It is not guaranteed that an error message has + * been printed. + * *out_val_freeIt Must be freed by the caller after using *out_val. */ /* coverity[+alloc : arg-*4] */ VarParseResult Var_Parse(const char **pp, GNode *ctxt, VarEvalFlags eflags, const char **out_val, void **out_val_freeIt) { - const char *const start = *pp; - const char *p; + const char *p = *pp; + const char *const start = p; Boolean haveModifier; /* TRUE if have modifiers for the variable */ char startc; /* Starting character if variable in parens * or braces */ @@ -3824,7 +3848,7 @@ Var_Parse(const char **pp, GNode *ctxt, VarEvalFlags eflags, * result is just the expression, unaltered */ const char *extramodifiers; Var *v; - char *nstr; + char *value; char eflags_str[VarEvalFlags_ToStringSize]; VarExprFlags exprFlags = 0; @@ -3840,17 +3864,17 @@ Var_Parse(const char **pp, GNode *ctxt, VarEvalFlags eflags, * initialized. */ endc = '\0'; - startc = start[1]; + startc = p[1]; if (startc != '(' && startc != '{') { VarParseResult res; if (!ParseVarnameShort(startc, pp, ctxt, eflags, &res, out_val, &v)) return res; haveModifier = FALSE; - p = start + 1; + p++; } else { VarParseResult res; - if (!ParseVarnameLong(pp, startc, ctxt, eflags, - &res, out_val, out_val_freeIt, + if (!ParseVarnameLong(p, startc, ctxt, eflags, + pp, &res, out_val, out_val_freeIt, &endc, &p, &v, &haveModifier, &extramodifiers, &dynamic, &exprFlags)) return res; @@ -3859,25 +3883,26 @@ Var_Parse(const char **pp, GNode *ctxt, VarEvalFlags eflags, if (v->flags & VAR_IN_USE) Fatal("Variable %s is recursive.", v->name); - /* - * Before doing any modification, we have to make sure the value - * has been fully expanded. If it looks like recursion might be - * necessary (there's a dollar sign somewhere in the variable's value) - * we just call Var_Subst to do any other substitutions that are - * necessary. Note that the value returned by Var_Subst will have - * been dynamically-allocated, so it will need freeing when we - * return. - */ - nstr = Buf_GetAll(&v->val, NULL); - if (strchr(nstr, '$') != NULL && (eflags & VARE_WANTRES)) { + /* XXX: This assignment creates an alias to the current value of the + * variable. This means that as long as the value of the expression stays + * the same, the value of the variable must not change. + * Using the '::=' modifier, it could be possible to do exactly this. + * At the bottom of this function, the resulting value is compared to the + * then-current value of the variable. This might also invoke undefined + * behavior. */ + value = Buf_GetAll(&v->val, NULL); + + /* Before applying any modifiers, expand any nested expressions from the + * variable value. */ + if (strchr(value, '$') != NULL && (eflags & VARE_WANTRES)) { VarEvalFlags nested_eflags = eflags; - if (DEBUG(LINT)) + if (opts.lint) nested_eflags &= ~(unsigned)VARE_UNDEFERR; v->flags |= VAR_IN_USE; - (void)Var_Subst(nstr, ctxt, nested_eflags, &nstr); + (void)Var_Subst(value, ctxt, nested_eflags, &value); v->flags &= ~(unsigned)VAR_IN_USE; /* TODO: handle errors */ - *out_val_freeIt = nstr; + *out_val_freeIt = value; } if (haveModifier || extramodifiers != NULL) { @@ -3886,16 +3911,16 @@ Var_Parse(const char **pp, GNode *ctxt, VarEvalFlags eflags, extraFree = NULL; if (extramodifiers != NULL) { const char *em = extramodifiers; - nstr = ApplyModifiers(&em, nstr, '(', ')', - v, &exprFlags, ctxt, eflags, &extraFree); + value = ApplyModifiers(&em, value, '\0', '\0', + v, &exprFlags, ctxt, eflags, &extraFree); } if (haveModifier) { /* Skip initial colon. */ p++; - nstr = ApplyModifiers(&p, nstr, startc, endc, - v, &exprFlags, ctxt, eflags, out_val_freeIt); + value = ApplyModifiers(&p, value, startc, endc, + v, &exprFlags, ctxt, eflags, out_val_freeIt); free(extraFree); } else { *out_val_freeIt = extraFree; @@ -3910,52 +3935,96 @@ Var_Parse(const char **pp, GNode *ctxt, VarEvalFlags eflags, if (v->flags & VAR_FROM_ENV) { /* Free the environment variable now since we own it, * but don't free the variable value if it will be returned. */ - Boolean keepValue = nstr == Buf_GetAll(&v->val, NULL); + Boolean keepValue = value == Buf_GetAll(&v->val, NULL); if (keepValue) - *out_val_freeIt = nstr; + *out_val_freeIt = value; (void)VarFreeEnv(v, !keepValue); } else if (exprFlags & VEF_UNDEF) { if (!(exprFlags & VEF_DEF)) { + /* TODO: Use a local variable instead of out_val_freeIt. + * Variables named out_* must only be written to. */ if (*out_val_freeIt != NULL) { free(*out_val_freeIt); *out_val_freeIt = NULL; } if (dynamic) { - nstr = bmake_strsedup(start, p); - *out_val_freeIt = nstr; + value = bmake_strsedup(start, p); + *out_val_freeIt = value; } else { /* The expression is still undefined, therefore discard the * actual value and return an error marker instead. */ - nstr = (eflags & VARE_UNDEFERR) ? var_Error : varUndefined; + value = eflags & VARE_UNDEFERR ? var_Error : varUndefined; } } - if (nstr != Buf_GetAll(&v->val, NULL)) + if (value != Buf_GetAll(&v->val, NULL)) Buf_Destroy(&v->val, TRUE); free(v->name_freeIt); free(v); } - *out_val = nstr; + *out_val = value; return VPR_UNKNOWN; } -/* Substitute for all variables in the given string in the given context. - * - * If eflags & VARE_UNDEFERR, Parse_Error will be called when an undefined - * variable is encountered. - * - * If eflags & VARE_WANTRES, any effects from the modifiers, such as ::=, - * :sh or !cmd! take place. +static void +VarSubstNested(const char **const pp, Buffer *const buf, GNode *const ctxt, + VarEvalFlags const eflags, Boolean *inout_errorReported) +{ + const char *p = *pp; + const char *nested_p = p; + const char *val; + void *val_freeIt; + + (void)Var_Parse(&nested_p, ctxt, eflags, &val, &val_freeIt); + /* TODO: handle errors */ + + if (val == var_Error || val == varUndefined) { + if (!preserveUndefined) { + p = nested_p; + } else if ((eflags & VARE_UNDEFERR) || val == var_Error) { + /* XXX: This condition is wrong. If val == var_Error, + * this doesn't necessarily mean there was an undefined + * variable. It could equally well be a parse error; see + * unit-tests/varmod-order.exp. */ + + /* + * If variable is undefined, complain and skip the + * variable. The complaint will stop us from doing anything + * when the file is parsed. + */ + if (!*inout_errorReported) { + Parse_Error(PARSE_FATAL, "Undefined variable \"%.*s\"", + (int)(size_t)(nested_p - p), p); + } + p = nested_p; + *inout_errorReported = TRUE; + } else { + /* Copy the initial '$' of the undefined expression, + * thereby deferring expansion of the expression, but + * expand nested expressions if already possible. + * See unit-tests/varparse-undef-partial.mk. */ + Buf_AddByte(buf, *p); + p++; + } + } else { + p = nested_p; + Buf_AddStr(buf, val); + } + + free(val_freeIt); + + *pp = p; +} + +/* Expand all variable expressions like $V, ${VAR}, $(VAR:Modifiers) in the + * given string. * * Input: - * str the string which to substitute - * ctxt the context wherein to find variables - * eflags VARE_UNDEFERR if undefineds are an error - * VARE_WANTRES if we actually want the result - * VARE_ASSIGN if we are in a := assignment - * - * Results: - * The resulting string. + * str The string in which the variable expressions are + * expanded. + * ctxt The context in which to start searching for + * variables. The other contexts are searched as well. + * eflags Special effects during expansion. */ VarParseResult Var_Subst(const char *str, GNode *ctxt, VarEvalFlags eflags, char **out_res) @@ -3965,19 +4034,24 @@ Var_Subst(const char *str, GNode *ctxt, VarEvalFlags eflags, char **out_res) /* Set true if an error has already been reported, * to prevent a plethora of messages when recursing */ + /* XXX: Why is the 'static' necessary here? */ static Boolean errorReported; - Buf_Init(&buf, 0); + Buf_Init(&buf); errorReported = FALSE; while (*p != '\0') { if (p[0] == '$' && p[1] == '$') { /* A dollar sign may be escaped with another dollar sign. */ - if (save_dollars && (eflags & VARE_ASSIGN)) + if (save_dollars && (eflags & VARE_KEEP_DOLLAR)) Buf_AddByte(&buf, '$'); Buf_AddByte(&buf, '$'); p += 2; - } else if (*p != '$') { + + } else if (p[0] == '$') { + VarSubstNested(&p, &buf, ctxt, eflags, &errorReported); + + } else { /* * Skip as many characters as possible -- either to the end of * the string or to the next dollar sign (variable expression). @@ -3987,48 +4061,6 @@ Var_Subst(const char *str, GNode *ctxt, VarEvalFlags eflags, char **out_res) for (p++; *p != '$' && *p != '\0'; p++) continue; Buf_AddBytesBetween(&buf, plainStart, p); - } else { - const char *nested_p = p; - void *freeIt; - const char *val; - (void)Var_Parse(&nested_p, ctxt, eflags, &val, &freeIt); - /* TODO: handle errors */ - - if (val == var_Error || val == varUndefined) { - /* - * If performing old-time variable substitution, skip over - * the variable and continue with the substitution. Otherwise, - * store the dollar sign and advance str so we continue with - * the string... - */ - if (oldVars) { - p = nested_p; - } else if ((eflags & VARE_UNDEFERR) || val == var_Error) { - /* - * If variable is undefined, complain and skip the - * variable. The complaint will stop us from doing anything - * when the file is parsed. - */ - if (!errorReported) { - Parse_Error(PARSE_FATAL, "Undefined variable \"%.*s\"", - (int)(size_t)(nested_p - p), p); - } - p = nested_p; - errorReported = TRUE; - } else { - /* Copy the initial '$' of the undefined expression, - * thereby deferring expansion of the expression, but - * expand nested expressions if already possible. - * See unit-tests/varparse-undef-partial.mk. */ - Buf_AddByte(&buf, *p); - p++; - } - } else { - p = nested_p; - Buf_AddStr(&buf, val); - } - free(freeIt); - freeIt = NULL; } } @@ -4040,9 +4072,9 @@ Var_Subst(const char *str, GNode *ctxt, VarEvalFlags eflags, char **out_res) void Var_Init(void) { - VAR_INTERNAL = Targ_NewGN("Internal"); - VAR_GLOBAL = Targ_NewGN("Global"); - VAR_CMDLINE = Targ_NewGN("Command"); + VAR_INTERNAL = GNode_New("Internal"); + VAR_GLOBAL = GNode_New("Global"); + VAR_CMDLINE = GNode_New("Command"); } /* Clean up the variables module. */ |