/commands/make/var.c
C | 4032 lines | 2402 code | 275 blank | 1355 comment | 914 complexity | 5f97aa9cba085dd1cec3ef59b48a2dc7 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- /* $NetBSD: var.c,v 1.159 2010/06/06 01:13:12 sjg Exp $ */
- /*
- * Copyright (c) 1988, 1989, 1990, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Adam de Boor.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- /*
- * Copyright (c) 1989 by Berkeley Softworks
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Adam de Boor.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- #ifndef MAKE_NATIVE
- static char rcsid[] = "$NetBSD: var.c,v 1.159 2010/06/06 01:13:12 sjg Exp $";
- #else
- #include <sys/cdefs.h>
- #ifndef lint
- #if 0
- static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94";
- #else
- __RCSID("$NetBSD: var.c,v 1.159 2010/06/06 01:13:12 sjg Exp $");
- #endif
- #endif /* not lint */
- #endif
- /*-
- * var.c --
- * Variable-handling functions
- *
- * Interface:
- * Var_Set Set the value of a variable in the given
- * context. The variable is created if it doesn't
- * yet exist. The value and variable name need not
- * be preserved.
- *
- * Var_Append Append more characters to an existing variable
- * in the given context. The variable needn't
- * exist already -- it will be created if it doesn't.
- * A space is placed between the old value and the
- * new one.
- *
- * Var_Exists See if a variable exists.
- *
- * Var_Value Return the value of a variable in a context or
- * NULL if the variable is undefined.
- *
- * Var_Subst Substitute named variable, or all variables if
- * NULL in a string using
- * the given context as the top-most one. If the
- * third argument is non-zero, Parse_Error is
- * called if any variables are undefined.
- *
- * Var_Parse Parse a variable expansion from a string and
- * return the result and the number of characters
- * consumed.
- *
- * Var_Delete Delete a variable in a context.
- *
- * Var_Init Initialize this module.
- *
- * Debugging:
- * Var_Dump Print out all variables defined in the given
- * context.
- *
- * XXX: There's a lot of duplication in these functions.
- */
- #include <sys/stat.h>
- #ifndef NO_REGEX
- #include <sys/types.h>
- #include <regex.h>
- #endif
- #include <ctype.h>
- #include <stdlib.h>
- #include <limits.h>
- #include "make.h"
- #include "buf.h"
- #include "dir.h"
- #include "job.h"
- /*
- * This is a harmless return value for Var_Parse that can be used by Var_Subst
- * to determine if there was an error in parsing -- easier than returning
- * a flag, as things outside this module don't give a hoot.
- */
- char var_Error[] = "";
- /*
- * Similar to var_Error, but returned when the 'errnum' flag for Var_Parse is
- * set false. Why not just use a constant? Well, gcc likes to condense
- * identical string instances...
- */
- static char varNoError[] = "";
- /*
- * Internally, variables are contained in four different contexts.
- * 1) the environment. They may not be changed. If an environment
- * variable is appended-to, the result is placed in the global
- * context.
- * 2) the global context. Variables set in the Makefile are located in
- * the global context. It is the penultimate context searched when
- * substituting.
- * 3) the command-line context. All variables set on the command line
- * are placed in this context. They are UNALTERABLE once placed here.
- * 4) the local context. Each target has associated with it a context
- * list. On this list are located the structures describing such
- * local variables as $(@) and $(*)
- * 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 */
- #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 */
- typedef struct Var {
- char *name; /* the variable's name */
- Buffer val; /* its value */
- int flags; /* miscellaneous status flags */
- #define VAR_IN_USE 1 /* Variable's value currently being used.
- * Used to avoid recursion */
- #define VAR_FROM_ENV 2 /* Variable comes from the environment */
- #define VAR_JUNK 4 /* Variable is a junk variable that
- * should be destroyed when done with
- * it. Used by Var_Parse for undefined,
- * modified variables */
- #define VAR_KEEP 8 /* Variable is VAR_JUNK, but we found
- * a use for it in some modifier and
- * the value is therefore valid */
- #define VAR_EXPORTED 16 /* Variable is exported */
- #define VAR_REEXPORT 32 /* Indicate if var needs re-export.
- * This would be true if it contains $'s
- */
- #define VAR_FROM_CMD 64 /* Variable came from command line */
- } Var;
- /*
- * Exporting vars is expensive so skip it if we can
- */
- #define VAR_EXPORTED_NONE 0
- #define VAR_EXPORTED_YES 1
- #define VAR_EXPORTED_ALL 2
- static int var_exportedVars = VAR_EXPORTED_NONE;
- /*
- * We pass this to Var_Export when doing the initial export
- * or after updating an exported var.
- */
- #define VAR_EXPORT_PARENT 1
- /* Var*Pattern flags */
- #define VAR_SUB_GLOBAL 0x01 /* Apply substitution globally */
- #define VAR_SUB_ONE 0x02 /* Apply substitution to one word */
- #define VAR_SUB_MATCHED 0x04 /* There was a match */
- #define VAR_MATCH_START 0x08 /* Match at start of word */
- #define VAR_MATCH_END 0x10 /* Match at end of word */
- #define VAR_NOSUBST 0x20 /* don't expand vars in VarGetPattern */
- /* Var_Set flags */
- #define VAR_NO_EXPORT 0x01 /* do not export */
- typedef struct {
- /*
- * The following fields are set by Var_Parse() when it
- * encounters modifiers that need to keep state for use by
- * subsequent modifiers within the same variable expansion.
- */
- Byte varSpace; /* Word separator in expansions */
- Boolean oneBigWord; /* TRUE if we will treat the variable as a
- * single big word, even if it contains
- * embedded spaces (as opposed to the
- * usual behaviour of treating it as
- * several space-separated words). */
- } Var_Parse_State;
- /* struct passed as 'void *' to VarSubstitute() for ":S/lhs/rhs/",
- * to VarSYSVMatch() for ":lhs=rhs". */
- typedef struct {
- const char *lhs; /* String to match */
- int leftLen; /* Length of string */
- const char *rhs; /* Replacement string (w/ &'s removed) */
- int rightLen; /* Length of replacement */
- int flags;
- } VarPattern;
- /* struct passed as 'void *' to VarLoopExpand() for ":@tvar@str@" */
- typedef struct {
- GNode *ctxt; /* variable context */
- char *tvar; /* name of temp var */
- int tvarLen;
- char *str; /* string to expand */
- int strLen;
- int errnum; /* errnum for not defined */
- } VarLoop_t;
- #ifndef NO_REGEX
- /* struct passed as 'void *' to VarRESubstitute() for ":C///" */
- typedef struct {
- regex_t re;
- int nsub;
- regmatch_t *matches;
- char *replace;
- int flags;
- } VarREPattern;
- #endif
- /* struct passed to VarSelectWords() for ":[start..end]" */
- typedef struct {
- int start; /* first word to select */
- int end; /* last word to select */
- } VarSelectWords_t;
- static Var *VarFind(const char *, GNode *, int);
- static void VarAdd(const char *, const char *, GNode *);
- static Boolean VarHead(GNode *, Var_Parse_State *,
- char *, Boolean, Buffer *, void *);
- static Boolean VarTail(GNode *, Var_Parse_State *,
- char *, Boolean, Buffer *, void *);
- static Boolean VarSuffix(GNode *, Var_Parse_State *,
- char *, Boolean, Buffer *, void *);
- static Boolean VarRoot(GNode *, Var_Parse_State *,
- char *, Boolean, Buffer *, void *);
- static Boolean VarMatch(GNode *, Var_Parse_State *,
- char *, Boolean, Buffer *, void *);
- #ifdef SYSVVARSUB
- static Boolean VarSYSVMatch(GNode *, Var_Parse_State *,
- char *, Boolean, Buffer *, void *);
- #endif
- static Boolean VarNoMatch(GNode *, Var_Parse_State *,
- char *, Boolean, Buffer *, void *);
- #ifndef NO_REGEX
- static void VarREError(int, regex_t *, const char *);
- static Boolean VarRESubstitute(GNode *, Var_Parse_State *,
- char *, Boolean, Buffer *, void *);
- #endif
- static Boolean VarSubstitute(GNode *, Var_Parse_State *,
- char *, Boolean, Buffer *, void *);
- static Boolean VarLoopExpand(GNode *, Var_Parse_State *,
- char *, Boolean, Buffer *, void *);
- static char *VarGetPattern(GNode *, Var_Parse_State *,
- int, const char **, int, int *, int *,
- VarPattern *);
- static char *VarQuote(char *);
- static char *VarChangeCase(char *, int);
- static char *VarModify(GNode *, Var_Parse_State *,
- const char *,
- Boolean (*)(GNode *, Var_Parse_State *, char *, Boolean, Buffer *, void *),
- void *);
- static char *VarOrder(const char *, const char);
- static char *VarUniq(const char *);
- static int VarWordCompare(const void *, const void *);
- static void VarPrintVar(void *);
- #define BROPEN '{'
- #define BRCLOSE '}'
- #define PROPEN '('
- #define PRCLOSE ')'
- /*-
- *-----------------------------------------------------------------------
- * VarFind --
- * Find the given variable in the given context and any other contexts
- * indicated.
- *
- * Input:
- * name name to find
- * ctxt context in which to find it
- * flags FIND_GLOBAL set means to look in the
- * VAR_GLOBAL context as well. FIND_CMD set means
- * to look in the VAR_CMD context also. FIND_ENV
- * set means to look in the environment
- *
- * 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)
- {
- Hash_Entry *var;
- Var *v;
- /*
- * 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 == '.' && isupper((unsigned char) name[1]))
- 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;
- }
- /*
- * First look for the variable in the given context. If it's not there,
- * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order,
- * depending on the FIND_* flags in 'flags'
- */
- var = Hash_FindEntry(&ctxt->context, name);
- if ((var == NULL) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) {
- var = Hash_FindEntry(&VAR_CMD->context, name);
- }
- if (!checkEnvFirst && (var == NULL) && (flags & FIND_GLOBAL) &&
- (ctxt != VAR_GLOBAL))
- {
- var = Hash_FindEntry(&VAR_GLOBAL->context, name);
- }
- if ((var == NULL) && (flags & FIND_ENV)) {
- char *env;
- if ((env = getenv(name)) != NULL) {
- int len;
- v = bmake_malloc(sizeof(Var));
- v->name = bmake_strdup(name);
- len = strlen(env);
- Buf_Init(&v->val, len + 1);
- Buf_AddBytes(&v->val, len, env);
- v->flags = VAR_FROM_ENV;
- return (v);
- } else if (checkEnvFirst && (flags & FIND_GLOBAL) &&
- (ctxt != VAR_GLOBAL))
- {
- var = Hash_FindEntry(&VAR_GLOBAL->context, name);
- if (var == NULL) {
- return NULL;
- } else {
- return ((Var *)Hash_GetValue(var));
- }
- } else {
- return NULL;
- }
- } else if (var == NULL) {
- return NULL;
- } else {
- return ((Var *)Hash_GetValue(var));
- }
- }
- /*-
- *-----------------------------------------------------------------------
- * VarFreeEnv --
- * If the variable is an environment variable, free it
- *
- * Input:
- * v the variable
- * destroy true if the value buffer should be destroyed.
- *
- * Results:
- * 1 if it is an environment variable 0 ow.
- *
- * Side Effects:
- * The variable is free'ed if it is an environent variable.
- *-----------------------------------------------------------------------
- */
- static Boolean
- VarFreeEnv(Var *v, Boolean destroy)
- {
- if ((v->flags & VAR_FROM_ENV) == 0)
- return FALSE;
- free(v->name);
- Buf_Destroy(&v->val, destroy);
- free(v);
- return TRUE;
- }
- /*-
- *-----------------------------------------------------------------------
- * VarAdd --
- * Add a new variable of name name and value val to the given context
- *
- * Input:
- * name name of variable to add
- * val value to set it to
- * ctxt context in which to set it
- *
- * Results:
- * None
- *
- * Side Effects:
- * The new variable is placed at the front of 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)
- {
- Var *v;
- int len;
- Hash_Entry *h;
- v = bmake_malloc(sizeof(Var));
- len = val ? strlen(val) : 0;
- Buf_Init(&v->val, len+1);
- Buf_AddBytes(&v->val, len, val);
- v->flags = 0;
- h = Hash_CreateEntry(&ctxt->context, name, NULL);
- Hash_SetValue(h, v);
- v->name = h->name;
- if (DEBUG(VAR)) {
- fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
- }
- }
- /*-
- *-----------------------------------------------------------------------
- * Var_Delete --
- * Remove a variable from a context.
- *
- * Results:
- * None.
- *
- * Side Effects:
- * The Var structure is removed and freed.
- *
- *-----------------------------------------------------------------------
- */
- void
- Var_Delete(const char *name, GNode *ctxt)
- {
- Hash_Entry *ln;
- ln = Hash_FindEntry(&ctxt->context, name);
- if (DEBUG(VAR)) {
- fprintf(debug_file, "%s:delete %s%s\n",
- ctxt->name, name, ln ? "" : " (not found)");
- }
- if (ln != NULL) {
- Var *v;
- v = (Var *)Hash_GetValue(ln);
- if ((v->flags & VAR_EXPORTED)) {
- unsetenv(v->name);
- }
- if (strcmp(MAKE_EXPORTED, v->name) == 0) {
- var_exportedVars = VAR_EXPORTED_NONE;
- }
- if (v->name != ln->name)
- free(v->name);
- Hash_DeleteEntry(&ctxt->context, ln);
- Buf_Destroy(&v->val, TRUE);
- free(v);
- }
- }
- /*
- * Export a var.
- * We ignore make internal variables (those which start with '.')
- * Also we jump through some hoops to avoid calling setenv
- * more than necessary since it can leak.
- * We only manipulate flags of vars if 'parent' is set.
- */
- static int
- Var_Export1(const char *name, int parent)
- {
- char tmp[BUFSIZ];
- Var *v;
- char *val = NULL;
- int n;
- if (*name == '.')
- return 0; /* skip internals */
- if (!name[1]) {
- /*
- * A single char.
- * If it is one of the vars that should only appear in
- * local context, skip it, else we can get Var_Subst
- * into a loop.
- */
- switch (name[0]) {
- case '@':
- case '%':
- case '*':
- case '!':
- return 0;
- }
- }
- v = VarFind(name, VAR_GLOBAL, 0);
- if (v == NULL) {
- return 0;
- }
- if (!parent &&
- (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
- return 0; /* nothing to do */
- }
- val = Buf_GetAll(&v->val, NULL);
- if (strchr(val, '$')) {
- if (parent) {
- /*
- * Flag this as something we need to re-export.
- * No point actually exporting it now though,
- * the child can do it at the last minute.
- */
- v->flags |= (VAR_EXPORTED|VAR_REEXPORT);
- return 1;
- }
- if (v->flags & VAR_IN_USE) {
- /*
- * We recursed while exporting in a child.
- * This isn't going to end well, just skip it.
- */
- return 0;
- }
- n = snprintf(tmp, sizeof(tmp), "${%s}", name);
- if (n < (int)sizeof(tmp)) {
- val = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
- setenv(name, val, 1);
- free(val);
- }
- } else {
- if (parent) {
- v->flags &= ~VAR_REEXPORT; /* once will do */
- }
- if (parent || !(v->flags & VAR_EXPORTED)) {
- setenv(name, val, 1);
- }
- }
- /*
- * This is so Var_Set knows to call Var_Export again...
- */
- if (parent) {
- v->flags |= VAR_EXPORTED;
- }
- return 1;
- }
- /*
- * This gets called from our children.
- */
- void
- Var_ExportVars(void)
- {
- char tmp[BUFSIZ];
- Hash_Entry *var;
- Hash_Search state;
- Var *v;
- char *val;
- int n;
- if (VAR_EXPORTED_NONE == var_exportedVars)
- return;
- if (VAR_EXPORTED_ALL == var_exportedVars) {
- /*
- * Ouch! This is crazy...
- */
- for (var = Hash_EnumFirst(&VAR_GLOBAL->context, &state);
- var != NULL;
- var = Hash_EnumNext(&state)) {
- v = (Var *)Hash_GetValue(var);
- Var_Export1(v->name, 0);
- }
- return;
- }
- /*
- * We have a number of exported vars,
- */
- n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
- if (n < (int)sizeof(tmp)) {
- char **av;
- char *as;
- int ac;
- int i;
- val = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
- av = brk_string(val, &ac, FALSE, &as);
- for (i = 0; i < ac; i++) {
- Var_Export1(av[i], 0);
- }
- free(val);
- free(as);
- free(av);
- }
- }
- /*
- * This is called when .export is seen or
- * .MAKE.EXPORTED is modified.
- * It is also called when any exported var is modified.
- */
- void
- Var_Export(char *str, int isExport)
- {
- char *name;
- char *val;
- char **av;
- char *as;
- int track;
- int ac;
- int i;
- if (isExport && (!str || !str[0])) {
- var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
- return;
- }
- if (strncmp(str, "-env", 4) == 0) {
- track = 0;
- str += 4;
- } else {
- track = VAR_EXPORT_PARENT;
- }
- val = Var_Subst(NULL, str, VAR_GLOBAL, 0);
- av = brk_string(val, &ac, FALSE, &as);
- for (i = 0; i < ac; i++) {
- name = av[i];
- if (!name[1]) {
- /*
- * A single char.
- * If it is one of the vars that should only appear in
- * local context, skip it, else we can get Var_Subst
- * into a loop.
- */
- switch (name[0]) {
- case '@':
- case '%':
- case '*':
- case '!':
- continue;
- }
- }
- if (Var_Export1(name, track)) {
- if (VAR_EXPORTED_ALL != var_exportedVars)
- var_exportedVars = VAR_EXPORTED_YES;
- if (isExport && track) {
- Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL);
- }
- }
- }
- free(val);
- free(as);
- free(av);
- }
- /*
- * This is called when .unexport[-env] is seen.
- */
- void
- Var_UnExport(char *str)
- {
- char tmp[BUFSIZ];
- char *vlist;
- char *cp;
- Boolean unexport_env;
- int n;
- if (!str || !str[0]) {
- return; /* assert? */
- }
- vlist = NULL;
- str += 8;
- unexport_env = (strncmp(str, "-env", 4) == 0);
- if (unexport_env) {
- extern char **environ;
- static char **savenv;
- char **newenv;
- cp = getenv(MAKE_LEVEL); /* we should preserve this */
- if (environ == savenv) {
- /* we have been here before! */
- newenv = bmake_realloc(environ, 2 * sizeof(char *));
- } else {
- if (savenv) {
- free(savenv);
- savenv = NULL;
- }
- newenv = bmake_malloc(2 * sizeof(char *));
- }
- if (!newenv)
- return;
- /* Note: we cannot safely free() the original environ. */
- environ = savenv = newenv;
- newenv[0] = NULL;
- newenv[1] = NULL;
- setenv(MAKE_LEVEL, cp, 1);
- } else {
- for (; *str != '\n' && isspace((unsigned char) *str); str++)
- continue;
- if (str[0] && str[0] != '\n') {
- vlist = str;
- }
- }
- if (!vlist) {
- /* Using .MAKE.EXPORTED */
- n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
- if (n < (int)sizeof(tmp)) {
- vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
- }
- }
- if (vlist) {
- Var *v;
- char **av;
- char *as;
- int ac;
- int i;
- av = brk_string(vlist, &ac, FALSE, &as);
- for (i = 0; i < ac; i++) {
- v = VarFind(av[i], VAR_GLOBAL, 0);
- if (!v)
- continue;
- if (!unexport_env &&
- (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
- unsetenv(v->name);
- }
- v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT);
- /*
- * If we are unexporting a list,
- * remove each one from .MAKE.EXPORTED.
- * If we are removing them all,
- * just delete .MAKE.EXPORTED below.
- */
- if (vlist == str) {
- n = snprintf(tmp, sizeof(tmp),
- "${" MAKE_EXPORTED ":N%s}", v->name);
- if (n < (int)sizeof(tmp)) {
- cp = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
- Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL, 0);
- free(cp);
- }
- }
- }
- free(as);
- free(av);
- if (vlist != str) {
- Var_Delete(MAKE_EXPORTED, VAR_GLOBAL);
- free(vlist);
- }
- }
- }
- /*-
- *-----------------------------------------------------------------------
- * Var_Set --
- * Set the variable name to the value val in the given context.
- *
- * Input:
- * name name of variable to set
- * val value to give to the variable
- * ctxt context in which to set it
- *
- * Results:
- * None.
- *
- * Side Effects:
- * If the variable doesn't yet exist, a new record is created for it.
- * Else the old value is freed and the new one stuck in its place
- *
- * Notes:
- * The variable is searched for only in its context before being
- * created in that context. I.e. if the context is VAR_GLOBAL,
- * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
- * VAR_CMD->context is searched. This is done to avoid the literally
- * thousands of unnecessary strcmp's that used to be done to
- * set, say, $(@) or $(<).
- * If the context is VAR_GLOBAL though, we check if the variable
- * was set in VAR_CMD from the command line and skip it if so.
- *-----------------------------------------------------------------------
- */
- void
- Var_Set(const char *name, const char *val, GNode *ctxt, int flags)
- {
- Var *v;
- char *expanded_name = NULL;
- /*
- * We only look for a variable in the given context since anything set
- * 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...
- */
- if (strchr(name, '$') != NULL) {
- expanded_name = Var_Subst(NULL, name, ctxt, 0);
- if (expanded_name[0] == 0) {
- if (DEBUG(VAR)) {
- fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) "
- "name expands to empty string - ignored\n",
- name, val);
- }
- free(expanded_name);
- return;
- }
- name = expanded_name;
- }
- if (ctxt == VAR_GLOBAL) {
- v = VarFind(name, VAR_CMD, 0);
- if (v != NULL) {
- if ((v->flags & VAR_FROM_CMD)) {
- if (DEBUG(VAR)) {
- fprintf(debug_file, "%s:%s = %s ignored!\n", ctxt->name, name, val);
- }
- goto out;
- }
- VarFreeEnv(v, TRUE);
- }
- }
- v = VarFind(name, ctxt, 0);
- if (v == NULL) {
- VarAdd(name, val, ctxt);
- } else {
- Buf_Empty(&v->val);
- Buf_AddBytes(&v->val, strlen(val), val);
- if (DEBUG(VAR)) {
- fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
- }
- if ((v->flags & VAR_EXPORTED)) {
- Var_Export1(name, VAR_EXPORT_PARENT);
- }
- }
- /*
- * Any variables given on the command line are automatically exported
- * to the environment (as per POSIX standard)
- */
- if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) {
- if (v == NULL) {
- /* we just added it */
- v = VarFind(name, ctxt, 0);
- }
- if (v != NULL)
- v->flags |= VAR_FROM_CMD;
- /*
- * If requested, don't export these in the environment
- * individually. We still put them in MAKEOVERRIDES so
- * that the command-line settings continue to override
- * Makefile settings.
- */
- if (varNoExportEnv != TRUE)
- setenv(name, val, 1);
- Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL);
- }
- /*
- * Another special case.
- * Several make's support this sort of mechanism for tracking
- * recursion - but each uses a different name.
- * We allow the makefiles to update .MAKE.LEVEL and ensure
- * children see a correctly incremented value.
- */
- if (ctxt == VAR_GLOBAL && strcmp(MAKE_LEVEL, name) == 0) {
- char tmp[64];
- int level;
-
- level = atoi(val);
- snprintf(tmp, sizeof(tmp), "%u", level + 1);
- setenv(MAKE_LEVEL, tmp, 1);
- }
-
-
- out:
- if (expanded_name != NULL)
- free(expanded_name);
- if (v != NULL)
- VarFreeEnv(v, TRUE);
- }
- /*-
- *-----------------------------------------------------------------------
- * Var_Append --
- * The variable of the given name has the given value appended to it in
- * the given context.
- *
- * Input:
- * name name of variable to modify
- * val String to append to it
- * ctxt Context in which this should occur
- *
- * Results:
- * None
- *
- * Side Effects:
- * If the variable doesn't exist, it is created. Else the strings
- * are concatenated (with a space in between).
- *
- * Notes:
- * Only if the variable is being sought in the global context is the
- * environment searched.
- * XXX: Knows its calling circumstances in that if called with ctxt
- * an actual target, it will only search that context since only
- * a local variable could be being appended to. This is actually
- * a big win and must be tolerated.
- *-----------------------------------------------------------------------
- */
- void
- Var_Append(const char *name, const char *val, GNode *ctxt)
- {
- Var *v;
- Hash_Entry *h;
- char *expanded_name = NULL;
- if (strchr(name, '$') != NULL) {
- expanded_name = Var_Subst(NULL, name, ctxt, 0);
- if (expanded_name[0] == 0) {
- if (DEBUG(VAR)) {
- fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) "
- "name expands to empty string - ignored\n",
- name, val);
- }
- free(expanded_name);
- return;
- }
- name = expanded_name;
- }
- v = VarFind(name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0);
- if (v == NULL) {
- VarAdd(name, val, ctxt);
- } else {
- Buf_AddByte(&v->val, ' ');
- Buf_AddBytes(&v->val, strlen(val), val);
- if (DEBUG(VAR)) {
- fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name,
- Buf_GetAll(&v->val, NULL));
- }
- if (v->flags & VAR_FROM_ENV) {
- /*
- * If the original variable came from the environment, we
- * have to install it in the global context (we could place
- * it in the environment, but then we should provide a way to
- * export other variables...)
- */
- v->flags &= ~VAR_FROM_ENV;
- h = Hash_CreateEntry(&ctxt->context, name, NULL);
- Hash_SetValue(h, v);
- }
- }
- if (expanded_name != NULL)
- free(expanded_name);
- }
- /*-
- *-----------------------------------------------------------------------
- * Var_Exists --
- * See if the given variable exists.
- *
- * Input:
- * name Variable to find
- * ctxt Context in which to start search
- *
- * Results:
- * TRUE if it does, FALSE if it doesn't
- *
- * Side Effects:
- * None.
- *
- *-----------------------------------------------------------------------
- */
- Boolean
- Var_Exists(const char *name, GNode *ctxt)
- {
- Var *v;
- char *cp;
- if ((cp = strchr(name, '$')) != NULL) {
- cp = Var_Subst(NULL, name, ctxt, FALSE);
- }
- v = VarFind(cp ? cp : name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
- if (cp != NULL) {
- free(cp);
- }
- if (v == NULL) {
- return(FALSE);
- } else {
- (void)VarFreeEnv(v, TRUE);
- }
- return(TRUE);
- }
- /*-
- *-----------------------------------------------------------------------
- * Var_Value --
- * Return the value of the named variable in the given context
- *
- * Input:
- * name name to find
- * ctxt context in which to search for it
- *
- * Results:
- * The value if the variable exists, NULL if it doesn't
- *
- * Side Effects:
- * None
- *-----------------------------------------------------------------------
- */
- char *
- Var_Value(const char *name, GNode *ctxt, char **frp)
- {
- Var *v;
- v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
- *frp = NULL;
- if (v != NULL) {
- char *p = (Buf_GetAll(&v->val, NULL));
- if (VarFreeEnv(v, FALSE))
- *frp = p;
- return p;
- } else {
- return NULL;
- }
- }
- /*-
- *-----------------------------------------------------------------------
- * VarHead --
- * Remove the tail of the given word and place the result in the given
- * buffer.
- *
- * Input:
- * word Word to trim
- * addSpace True if need to add a space to the buffer
- * before sticking in the head
- * buf Buffer in which to store it
- *
- * Results:
- * TRUE if characters were added to the buffer (a space needs to be
- * added to the buffer before the next word).
- *
- * Side Effects:
- * The trimmed word is added to the buffer.
- *
- *-----------------------------------------------------------------------
- */
- static Boolean
- VarHead(GNode *ctx __unused, Var_Parse_State *vpstate,
- char *word, Boolean addSpace, Buffer *buf,
- void *dummy)
- {
- char *slash;
- slash = strrchr(word, '/');
- if (slash != NULL) {
- if (addSpace && vpstate->varSpace) {
- Buf_AddByte(buf, vpstate->varSpace);
- }
- *slash = '\0';
- Buf_AddBytes(buf, strlen(word), word);
- *slash = '/';
- return (TRUE);
- } else {
- /*
- * If no directory part, give . (q.v. the POSIX standard)
- */
- if (addSpace && vpstate->varSpace)
- Buf_AddByte(buf, vpstate->varSpace);
- Buf_AddByte(buf, '.');
- }
- return(dummy ? TRUE : TRUE);
- }
- /*-
- *-----------------------------------------------------------------------
- * VarTail --
- * Remove the head of the given word and place the result in the given
- * buffer.
- *
- * Input:
- * word Word to trim
- * addSpace True if need to add a space to the buffer
- * before adding the tail
- * buf Buffer in which to store it
- *
- * Results:
- * TRUE if characters were added to the buffer (a space needs to be
- * added to the buffer before the next word).
- *
- * Side Effects:
- * The trimmed word is added to the buffer.
- *
- *-----------------------------------------------------------------------
- */
- static Boolean
- VarTail(GNode *ctx __unused, Var_Parse_State *vpstate,
- char *word, Boolean addSpace, Buffer *buf,
- void *dummy)
- {
- char *slash;
- if (addSpace && vpstate->varSpace) {
- Buf_AddByte(buf, vpstate->varSpace);
- }
- slash = strrchr(word, '/');
- if (slash != NULL) {
- *slash++ = '\0';
- Buf_AddBytes(buf, strlen(slash), slash);
- slash[-1] = '/';
- } else {
- Buf_AddBytes(buf, strlen(word), word);
- }
- return (dummy ? TRUE : TRUE);
- }
- /*-
- *-----------------------------------------------------------------------
- * VarSuffix --
- * Place the suffix of the given word in the given buffer.
- *
- * Input:
- * word Word to trim
- * addSpace TRUE if need to add a space before placing the
- * suffix in the buffer
- * buf Buffer in which to store it
- *
- * Results:
- * TRUE if characters were added to the buffer (a space needs to be
- * added to the buffer before the next word).
- *
- * Side Effects:
- * The suffix from the word is placed in the buffer.
- *
- *-----------------------------------------------------------------------
- */
- static Boolean
- VarSuffix(GNode *ctx __unused, Var_Parse_State *vpstate,
- char *word, Boolean addSpace, Buffer *buf,
- void *dummy)
- {
- char *dot;
- dot = strrchr(word, '.');
- if (dot != NULL) {
- if (addSpace && vpstate->varSpace) {
- Buf_AddByte(buf, vpstate->varSpace);
- }
- *dot++ = '\0';
- Buf_AddBytes(buf, strlen(dot), dot);
- dot[-1] = '.';
- addSpace = TRUE;
- }
- return (dummy ? addSpace : addSpace);
- }
- /*-
- *-----------------------------------------------------------------------
- * VarRoot --
- * Remove the suffix of the given word and place the result in the
- * buffer.
- *
- * Input:
- * word Word to trim
- * addSpace TRUE if need to add a space to the buffer
- * before placing the root in it
- * buf Buffer in which to store it
- *
- * Results:
- * TRUE if characters were added to the buffer (a space needs to be
- * added to the buffer before the next word).
- *
- * Side Effects:
- * The trimmed word is added to the buffer.
- *
- *-----------------------------------------------------------------------
- */
- static Boolean
- VarRoot(GNode *ctx __unused, Var_Parse_State *vpstate,
- char *word, Boolean addSpace, Buffer *buf,
- void *dummy)
- {
- char *dot;
- if (addSpace && vpstate->varSpace) {
- Buf_AddByte(buf, vpstate->varSpace);
- }
- dot = strrchr(word, '.');
- if (dot != NULL) {
- *dot = '\0';
- Buf_AddBytes(buf, strlen(word), word);
- *dot = '.';
- } else {
- Buf_AddBytes(buf, strlen(word), word);
- }
- return (dummy ? TRUE : TRUE);
- }
- /*-
- *-----------------------------------------------------------------------
- * VarMatch --
- * Place the word in the buffer if it matches the given pattern.
- * Callback function for VarModify to implement the :M modifier.
- *
- * Input:
- * word Word to examine
- * addSpace TRUE if need to add a space to the buffer
- * before adding the word, if it matches
- * buf Buffer in which to store it
- * pattern Pattern the word must match
- *
- * Results:
- * TRUE if a space should be placed in the buffer before the next
- * word.
- *
- * Side Effects:
- * The word may be copied to the buffer.
- *
- *-----------------------------------------------------------------------
- */
- static Boolean
- VarMatch(GNode *ctx __unused, Var_Parse_State *vpstate,
- char *word, Boolean addSpace, Buffer *buf,
- void *pattern)
- {
- if (DEBUG(VAR))
- fprintf(debug_file, "VarMatch [%s] [%s]\n", word, (char *)pattern);
- if (Str_Match(word, (char *)pattern)) {
- if (addSpace && vpstate->varSpace) {
- Buf_AddByte(buf, vpstate->varSpace);
- }
- addSpace = TRUE;
- Buf_AddBytes(buf, strlen(word), word);
- }
- return(addSpace);
- }
- #ifdef SYSVVARSUB
- /*-
- *-----------------------------------------------------------------------
- * VarSYSVMatch --
- * Place the word in the buffer if it matches the given pattern.
- * Callback function for VarModify to implement the System V %
- * modifiers.
- *
- * Input:
- * word Word to examine
- * addSpace TRUE if need to add a space to the buffer
- * before adding the word, if it matches
- * buf Buffer in which to store it
- * patp Pattern the word must match
- *
- * Results:
- * TRUE if a space should be placed in the buffer before the next
- * word.
- *
- * Side Effects:
- * The word may be copied to the buffer.
- *
- *-----------------------------------------------------------------------
- */
- static Boolean
- VarSYSVMatch(GNode *ctx, Var_Parse_State *vpstate,
- char *word, Boolean addSpace, Buffer *buf,
- void *patp)
- {
- int len;
- char *ptr;
- VarPattern *pat = (VarPattern *)patp;
- char *varexp;
- if (addSpace && vpstate->varSpace)
- Buf_AddByte(buf, vpstate->varSpace);
- addSpace = TRUE;
- if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL) {
- varexp = Var_Subst(NULL, pat->rhs, ctx, 0);
- Str_SYSVSubst(buf, varexp, ptr, len);
- free(varexp);
- } else {
- Buf_AddBytes(buf, strlen(word), word);
- }
- return(addSpace);
- }
- #endif
- /*-
- *-----------------------------------------------------------------------
- * VarNoMatch --
- * Place the word in the buffer if it doesn't match the given pattern.
- * Callback function for VarModify to implement the :N modifier.
- *
- * Input:
- * word Word to examine
- * addSpace TRUE if need to add a space to the buffer
- * before adding the word, if it matches
- * buf Buffer in which to store it
- * pattern Pattern the word must match
- *
- * Results:
- * TRUE if a space should be placed in the buffer before the next
- * word.
- *
- * Side Effects:
- * The word may be copied to the buffer.
- *
- *-----------------------------------------------------------------------
- */
- static Boolean
- VarNoMatch(GNode *ctx __unused, Var_Parse_State *vpstate,
- char *word, Boolean addSpace, Buffer *buf,
- void *pattern)
- {
- if (!Str_Match(word, (char *)pattern)) {
- if (addSpace && vpstate->varSpace) {
- Buf_AddByte(buf, vpstate->varSpace);
- }
- addSpace = TRUE;
- Buf_AddBytes(buf, strlen(word), word);
- }
- return(addSpace);
- }
- /*-
- *-----------------------------------------------------------------------
- * VarSubstitute --
- * Perform a string-substitution on the given word, placing the
- * result in the passed buffer.
- *
- * Input:
- * word Word to modify
- * addSpace True if space should be added before
- * other characters
- * buf Buffer for result
- * patternp Pattern for substitution
- *
- * Results:
- * TRUE if a space is needed before more characters are added.
- *
- * Side Effects:
- * None.
- *
- *-----------------------------------------------------------------------
- */
- static Boolean
- VarSubstitute(GNode *ctx __unused, Var_Parse_State *vpstate,
- char *word, Boolean addSpace, Buffer *buf,
- void *patternp)
- {
- int wordLen; /* Length of word */
- char *cp; /* General pointer */
- VarPattern *pattern = (VarPattern *)patternp;
- wordLen = strlen(word);
- if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) !=
- (VAR_SUB_ONE|VAR_SUB_MATCHED)) {
- /*
- * Still substituting -- break it down into simple anchored cases
- * and if none of them fits, perform the general substitution case.
- */
- if ((pattern->flags & VAR_MATCH_START) &&
- (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) {
- /*
- * Anchored at start and beginning of word matches pattern
- */
- if ((pattern->flags & VAR_MATCH_END) &&
- (wordLen == pattern->leftLen)) {
- /*
- * Also anchored at end and matches to the end (word
- * is same length as pattern) add space and rhs only
- * if rhs is non-null.
- */
- if (pattern->rightLen != 0) {
- if (addSpace && vpstate->varSpace) {
- Buf_AddByte(buf, vpstate->varSpace);
- }
- addSpace = TRUE;
- Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
- }
- pattern->flags |= VAR_SUB_MATCHED;
- } else if (pattern->flags & VAR_MATCH_END) {
- /*
- * Doesn't match to end -- copy word wholesale
- */
- goto nosub;
- } else {
- /*
- * Matches at start but need to copy in trailing characters
- */
- if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){
- if (addSpace && vpstate->varSpace) {
- Buf_AddByte(buf, vpstate->varSpace);
- }
- addSpace = TRUE;
- }
- Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
- Buf_AddBytes(buf, wordLen - pattern->leftLen,
- (word + pattern->leftLen));
- pattern->flags |= VAR_SUB_MATCHED;
- }
- } else if (pattern->flags & VAR_MATCH_START) {
- /*
- * Had to match at start of word and didn't -- copy whole word.
- */
- goto nosub;
- } else if (pattern->flags & VAR_MATCH_END) {
- /*
- * Anchored at end, Find only place match could occur (leftLen
- * characters from the end of the word) and see if it does. Note
- * that because the $ will be left at the end of the lhs, we have
- * to use strncmp.
- */
- cp = word + (wordLen - pattern->leftLen);
- if ((cp >= word) &&
- (strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) {
- /*
- * Match found. If we will place characters in the buffer,
- * add a space before hand as indicated by addSpace, then
- * stuff in the initial, unmatched part of the word followed
- * by the right-hand-side.
- */
- if (((cp - word) + pattern->rightLen) != 0) {
- if (addSpace && vpstate->varSpace) {
- Buf_AddByte(buf, vpstate->varSpace);
- }
- addSpace = TRUE;
- }
- Buf_AddBytes(buf, cp - word, word);
- Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
- pattern->flags |= VAR_SUB_MATCHED;
- } else {
- /*
- * Had to match at end and didn't. Copy entire word.
- */
- goto nosub;
- }
- } else {
- /*
- * Pattern is unanchored: search for the pattern in the word using
- * String_FindSubstring, copying unmatched portions and the
- * right-hand-side for each match found, handling non-global
- * substitutions correctly, etc. When the loop is done, any
- * remaining part of the word (word and wordLen are adjusted
- * accordingly through the loop) is copied straight into the
- * buffer.
- * addSpace is set FALSE as soon as a space is added to the
- * buffer.
- */
- Boolean done;
- int origSize;
- done = FALSE;
- origSize = Buf_Size(buf);
- while (!done) {
- cp = Str_FindSubstring(word, pattern->lhs);
- if (cp != NULL) {
- if (addSpace && (((cp - word) + pattern->rightLen) != 0)){
- Buf_AddByte(buf, vpstate->varSpace);
- addSpace = FALSE;
- }
- Buf_AddBytes(buf, cp-word, word);
- Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
- wordLen -= (cp - word) + pattern->leftLen;
- word = cp + pattern->leftLen;
- if (wordLen == 0) {
- done = TRUE;
- }
- if ((pattern->flags & VAR_SUB_GLOBAL) == 0) {
- done = TRUE;
- }
- pattern->flags |= VAR_SUB_MATCHED;
- } else {
- done = TRUE;
- }
- }
- if (wordLen != 0) {
- if (addSpace && vpstate->varSpace) {
- Buf_AddByte(buf, vpstate->varSpace);
- }
- Buf_AddBytes(buf, wordLen, word);
- }
- /*
- * If added characters to the buffer, need to add a space
- * before we add any more. If we didn't add any, just return
- * the previous value of addSpace.
- */
- return ((Buf_Size(buf) != origSize) || addSpace);
- }
- return (addSpace);
- }
- nosub:
- if (addSpace && vpstate->varSpace) {
- Buf_AddByte(buf, vpstate->varSpace);
- }
- Buf_AddBytes(buf, wordLen, word);
- return(TRUE);
- }
- #ifndef NO_REGEX
- /*-
- *-----------------------------------------------------------------------
- * VarREError --
- * Print the error caused by a regcomp or regexec call.
- *
- * Results:
- * None.
- *
- * Side Effects:
- * An error gets printed.
- *
- *-----------------------------------------------------------------------
- */
- static void
- VarREError(int errnum, regex_t *pat, const char *str)
- {
- char *errbuf;
- int errlen;
- errlen = regerror(errnum, pat, 0, 0);
- errbuf = bmake_malloc(errlen);
- regerror(errnum, pat, errbuf, errlen);
- Error("%s: %s", str, errbuf);
- free(errbuf);
- }
- /*-
- *-----------------------------------------------------------------------
- * VarRESubstitute --
- * Perform a regex substitution on the given word, placing the
- * result in the passed buffer.
- *
- * Results:
- * TRUE if a space is needed before more characters are added.
- *
- * Side Effects:
- * None.
- *
- *-----------------------------------------------------------------------
- */
- static Boolean
- VarRESubstitute(GNode *ctx __unused, Var_Parse_State *vpstate __unused,
- char *word, Boolean addSpace, Buffer *buf,
- void *patternp)
- {
- VarREPattern *pat;
- int xrv;
- char *wp;
- char *rp;
- int added;
- int flags = 0;
- #define MAYBE_ADD_SPACE() \
- if (addSpace && !added) \
- Buf_AddByte(buf, ' '); \
- added = 1
- added = 0;
- wp = word;
- pat = patternp;
- if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) ==
- (VAR_SUB_ONE|VAR_SUB_MATCHED))
- xrv = REG_NOMATCH;
- else {
- tryagain:
- xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, flags);
- }
- switch (xrv) {
- case 0:
- pat->flags |= VAR_SUB_MATCHED;
- if (pat->matches[0].rm_so > 0) {
- MAYBE_ADD_SPACE();
- Buf_AddBytes(buf, pat->matches[0].rm_so, wp);
- }
- for (rp = pat->replace; *rp; rp++) {
- if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) {
- MAYBE_ADD_SPACE();
- Buf_AddByte(buf,rp[1]);
- rp++;
- }
- else if ((*rp == '&') ||
- ((*rp == '\\') && isdigit((unsigned char)rp[1]))) {
- int n;
- const char *subbuf;
- int sublen;
- char errstr[3];
- if (*rp == '&') {
- n = 0;
- errstr[0] = '&';
- errstr[1] = '\0';
- } else {
- n = rp[1] - '0';
- errstr[0] = '\\';
- errstr[1] = rp[1];
- errstr[2] = '\0';
- rp++;
- }
- if (n > pat->nsub) {
- Error("No subexpression %s", &errstr[0]);
- subbuf = "";
- sublen = 0;
- } else if ((pat->matches[n].rm_so == -1) &&
- (pat->matches[n].rm_eo == -1)) {
- Error("No match for subexpression %s", &errstr[0]);
- subbuf = "";
- sublen = 0;
- } else {
- subbuf = wp + pat->matches[n].rm_so;
- sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so;
- }
- if (sublen > 0) {
- MAYBE_ADD_SPACE();
- Buf_AddBytes(buf, sublen, subbuf);
- }
- } else {
- MAYBE_ADD_SPACE();
- Buf_AddByte(buf, *rp);
- }
- }
- wp += pat->matches[0].rm_eo;
- if (pat->flags & VAR_SUB_GLOBAL) {
- flags |= REG_NOTBOL;
- if (pat->matches[0].rm_so == 0 && pat->matches[0].rm_eo == 0) {
- MAYBE_ADD_SPACE();
- Buf_AddByte(buf, *wp);
- wp++;
- }
- if (*wp)
- goto tryagain;
- }
- if (*wp) {
- MAYBE_ADD_SPACE();
- Buf_AddBytes(buf, strlen(wp), wp);
- }
- break;
- default:
- VarREError(xrv, &pat->re, "Unexpected regex error");
- /* fall through */
- case REG_NOMATCH:
- if (*wp) {
- MAYBE_ADD_SPACE();
- Buf_AddBytes(buf,strlen(wp),wp);
- }
- break;
- }
- return(addSpace||added);
- }
- #endif
- /*-
- *-----------------------------------------------------------------------
- * VarLoopExpand --
- * Implements the :@<temp>@<string>@ modifier of ODE make.
- * We set the temp variable named in pattern.lhs to word and expand
- * pattern.rhs storing the result in the passed buffer.
- *
- * Input:
- * word Word to modify
- * addSpace True if space should be added before
- * other characters
- * buf Buffer for result
- * pattern Datafor substitution
- *
- * Results:
- * TRUE if a space is needed before more characters are added.
- *
- * Side Effects:
- * None.
- *
- *-----------------------------------------------------------------------
- */
- static Boolean
- VarLoopExpand(GNode *ctx __unused, Var_Parse_State *vpstate __unused,
- char *word, Boolean addSpace, Buffer *buf,
- void *loopp)
- {
- VarLoop_t *loop = (VarLoop_t *)loopp;
- char *s;
- int slen;
- if (word && *word) {
- Var_Set(loop->tvar, word, loop->ctxt, VAR_NO_EXPORT);
- s = Var_Subst(NULL, loop->str, loop->ctxt, loop->errnum);
- if (s != NULL && *s != '\0') {
- if (addSpace && *s != '\n')
- Buf_AddByte(buf, ' ');
- Buf_AddBytes(buf, (slen = strlen(s)), s);
- addSpace = (slen > 0 && s[slen - 1] != '\n');
- free(s);
- }
- }
- return addSpace;
- }
- /*-
- *-----------------------------------------------------------------------
- * VarSelectWords --
- * Implements the :[start..end] modifier.
- * This is a special case of VarModify since we want to be able
- * to scan the list backwards if start > end.
- *
- * Input:
- * str String whose words should be trimmed
- * seldata words to select
- *
- * Results:
- * A string of all the words selected.
- *
- * Side Effects:
- * None.
- *
- *-----------------------------------------------------------------------
- */
- static char *
- VarSelectWords(GNode *ctx __unused, Var_Parse_State *vpstate,
- const char *str, VarSelectWords_t *seldata)
- {
- Buffer buf; /* Buffer for the new string */
- Boolean addSpace; /* TRUE if need to add a space to the
- * buffer before adding the trimmed
- * word */
- char **av; /* word list */
- char *as; /* word list memory */
- int ac, i;
- int start, end, step;
- Buf_Init(&buf, 0);
- addSpace = FALSE;
- if (vpstate->oneBigWord) {
- /* fake what brk_string() would do if there were only one word */
- ac = 1;
- av = bmake_malloc((ac + 1) * sizeof(char *));
- as = bmake_strdup(str);
- av[0] = as;
- av[1] = NULL;
- } else {
- av = brk_string(str, &ac, FALSE, &as);
- }
- /*
- * Now sanitize sel…
Large files files are truncated, but you can click here to view the full file