2005-02-01 10:50:37 +00:00
|
|
|
/*-
|
1994-05-27 12:33:43 +00:00
|
|
|
* Copyright (c) 1988, 1989, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* 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.
|
2000-07-09 02:54:54 +00:00
|
|
|
*
|
|
|
|
* @(#)var.c 8.3 (Berkeley) 3/19/94
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
|
2000-07-09 02:54:54 +00:00
|
|
|
#include <sys/cdefs.h>
|
2002-04-13 10:17:18 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* var.c --
|
|
|
|
* Variable-handling functions
|
|
|
|
*
|
|
|
|
* Interface:
|
2005-03-18 15:44:23 +00:00
|
|
|
* 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.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
2005-03-18 15:44:23 +00:00
|
|
|
* 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.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
2005-03-18 15:44:23 +00:00
|
|
|
* Var_Exists See if a variable exists.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
2005-03-18 15:44:23 +00:00
|
|
|
* Var_Value Return the value of a variable in a context or
|
|
|
|
* NULL if the variable is undefined.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
2005-03-18 15:44:23 +00:00
|
|
|
* 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.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
2005-03-18 15:44:23 +00:00
|
|
|
* Var_Parse Parse a variable expansion from a string and
|
|
|
|
* return the result and the number of characters
|
|
|
|
* consumed.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
2005-03-18 15:44:23 +00:00
|
|
|
* Var_Delete Delete a variable in a context.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
2005-03-18 15:44:23 +00:00
|
|
|
* Var_Init Initialize this module.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* Debugging:
|
2005-03-18 15:44:23 +00:00
|
|
|
* Var_Dump Print out all variables defined in the given
|
|
|
|
* context.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* XXX: There's a lot of duplication in these functions.
|
|
|
|
*/
|
|
|
|
|
2005-03-02 14:30:36 +00:00
|
|
|
#include <assert.h>
|
2005-02-01 10:50:37 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "buf.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "globals.h"
|
|
|
|
#include "GNode.h"
|
|
|
|
#include "make.h"
|
|
|
|
#include "nonints.h"
|
|
|
|
#include "parse.h"
|
|
|
|
#include "str.h"
|
|
|
|
#include "targ.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "var.h"
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
typedef struct VarParser {
|
|
|
|
const char *const input; /* pointer to input string */
|
|
|
|
GNode *ctxt;
|
|
|
|
Boolean err;
|
|
|
|
} VarParser;
|
|
|
|
static char *VarParse(const char [], VarParser *, size_t *, Boolean *);
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-03-18 15:44:23 +00:00
|
|
|
char var_Error[] = "";
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Similar to var_Error, but returned when the 'err' 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
|
2005-03-18 15:44:23 +00:00
|
|
|
* variable is appended-to, the result is placed in the global
|
|
|
|
* context.
|
1994-05-27 12:33:43 +00:00
|
|
|
* 2) the global context. Variables set in the Makefile are located in
|
2005-03-18 15:44:23 +00:00
|
|
|
* the global context. It is the penultimate context searched when
|
|
|
|
* substituting.
|
1994-05-27 12:33:43 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2005-03-18 15:44:23 +00:00
|
|
|
GNode *VAR_GLOBAL; /* variables from the makefile */
|
|
|
|
GNode *VAR_CMD; /* variables defined on the command-line */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-09-17 21:29:06 +00:00
|
|
|
#define FIND_CMD 0x1 /* look in VAR_CMD when searching */
|
|
|
|
#define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */
|
2005-03-18 15:44:23 +00:00
|
|
|
#define FIND_ENV 0x4 /* look in the environment also */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-02-09 18:44:09 +00:00
|
|
|
#define OPEN_PAREN '('
|
|
|
|
#define CLOSE_PAREN ')'
|
|
|
|
#define OPEN_BRACE '{'
|
|
|
|
#define CLOSE_BRACE '}'
|
|
|
|
|
2005-02-09 09:53:28 +00:00
|
|
|
/*
|
|
|
|
* Create a Var object.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* name Name of variable (copied).
|
|
|
|
* value Value of variable (copied) or NULL.
|
|
|
|
* flags Flags set on variable.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* New variable.
|
|
|
|
*/
|
|
|
|
static Var *
|
|
|
|
VarCreate(const char name[], const char value[], int flags)
|
|
|
|
{
|
|
|
|
Var *v;
|
|
|
|
|
|
|
|
v = emalloc(sizeof(Var));
|
|
|
|
v->name = estrdup(name);
|
|
|
|
v->val = Buf_Init(0);
|
|
|
|
v->flags = flags;
|
|
|
|
|
|
|
|
if (value != NULL) {
|
|
|
|
Buf_Append(v->val, value);
|
|
|
|
}
|
|
|
|
return (v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a Var object.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* v Object to destroy.
|
|
|
|
* f True if internal buffer in Buffer object is to be removed.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
VarDestroy(Var *v, Boolean f)
|
|
|
|
{
|
|
|
|
|
|
|
|
Buf_Destroy(v->val, f);
|
|
|
|
free(v->name);
|
|
|
|
free(v);
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* VarCmp --
|
|
|
|
* See if the given variable matches the named one. Called from
|
|
|
|
* Lst_Find when searching for a variable of a given name.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* 0 if they match. non-zero otherwise.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* none
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static int
|
2004-12-08 12:59:27 +00:00
|
|
|
VarCmp(const void *v, const void *name)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
return (strcmp(name, ((const Var *)v)->name));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-06-19 17:23:08 +00:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* VarPossiblyExpand --
|
|
|
|
* Expand a variable name's embedded variables in the given context.
|
|
|
|
*
|
|
|
|
* Results:
|
2002-06-19 17:39:36 +00:00
|
|
|
* The contents of name, possibly expanded.
|
2002-06-19 17:23:08 +00:00
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
2005-02-04 12:10:20 +00:00
|
|
|
static char *
|
|
|
|
VarPossiblyExpand(const char *name, GNode *ctxt)
|
2002-06-19 17:23:08 +00:00
|
|
|
{
|
2005-02-25 13:16:56 +00:00
|
|
|
Buffer *buf;
|
|
|
|
char *str;
|
|
|
|
char *tmp;
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-02-04 12:10:20 +00:00
|
|
|
/*
|
|
|
|
* XXX make a temporary copy of the name because Var_Subst insists
|
|
|
|
* on writing into the string.
|
|
|
|
*/
|
|
|
|
tmp = estrdup(name);
|
|
|
|
if (strchr(name, '$') != NULL) {
|
2005-02-25 13:16:56 +00:00
|
|
|
buf = Var_Subst(NULL, tmp, ctxt, 0);
|
|
|
|
str = Buf_GetAll(buf, NULL);
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
|
2005-02-04 12:10:20 +00:00
|
|
|
free(tmp);
|
2005-02-25 13:16:56 +00:00
|
|
|
return (str);
|
|
|
|
} else {
|
2005-02-04 12:10:20 +00:00
|
|
|
return (tmp);
|
2005-02-25 13:16:56 +00:00
|
|
|
}
|
2002-06-19 17:23:08 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* VarFind --
|
|
|
|
* Find the given variable in the given context and any other contexts
|
|
|
|
* indicated.
|
|
|
|
*
|
2002-10-09 03:42:10 +00:00
|
|
|
* Flags:
|
|
|
|
* FIND_GLOBAL set means look in the VAR_GLOBAL context too
|
|
|
|
* FIND_CMD set means to look in the VAR_CMD context too
|
|
|
|
* FIND_ENV set means to look in the environment
|
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
* Results:
|
|
|
|
* A pointer to the structure describing the desired variable or
|
2000-12-02 18:58:01 +00:00
|
|
|
* NULL if the variable does not exist.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static Var *
|
2005-02-04 07:51:00 +00:00
|
|
|
VarFind(const char *name, GNode *ctxt, int flags)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-02-09 13:13:59 +00:00
|
|
|
Boolean localCheckEnvFirst;
|
|
|
|
LstNode *var;
|
|
|
|
char *env;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-03-15 14:52:10 +00:00
|
|
|
if (name[0] == '.') {
|
1994-05-27 12:33:43 +00:00
|
|
|
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;
|
2005-02-07 16:33:57 +00:00
|
|
|
default:
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-02-07 16:33:57 +00:00
|
|
|
}
|
1999-07-31 20:53:02 +00:00
|
|
|
|
2005-02-09 13:13:59 +00:00
|
|
|
/*
|
|
|
|
* Note whether this is one of the specific variables we were told
|
|
|
|
* through the -E flag to use environment-variable-override for.
|
|
|
|
*/
|
|
|
|
if (Lst_Find(&envFirstVars, name, (CompareProc *)strcmp) != NULL) {
|
|
|
|
localCheckEnvFirst = TRUE;
|
|
|
|
} else {
|
|
|
|
localCheckEnvFirst = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 = Lst_Find(&ctxt->context, name, VarCmp);
|
|
|
|
if (var != NULL) {
|
|
|
|
/* got it */
|
|
|
|
return (Lst_Datum(var));
|
|
|
|
}
|
1999-07-31 20:53:02 +00:00
|
|
|
|
2005-02-09 13:13:59 +00:00
|
|
|
/* not there - try command line context */
|
|
|
|
if ((flags & FIND_CMD) && (ctxt != VAR_CMD)) {
|
|
|
|
var = Lst_Find(&VAR_CMD->context, name, VarCmp);
|
|
|
|
if (var != NULL)
|
|
|
|
return (Lst_Datum(var));
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-02-09 13:13:59 +00:00
|
|
|
/* not there - try global context, but only if not -e/-E */
|
|
|
|
if ((flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL) &&
|
|
|
|
!checkEnvFirst && !localCheckEnvFirst) {
|
|
|
|
var = Lst_Find(&VAR_GLOBAL->context, name, VarCmp);
|
|
|
|
if (var != NULL)
|
|
|
|
return (Lst_Datum(var));
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-02-09 13:13:59 +00:00
|
|
|
if (!(flags & FIND_ENV))
|
|
|
|
/* we were not told to look into the environment */
|
2004-12-01 10:29:20 +00:00
|
|
|
return (NULL);
|
2005-02-09 13:13:59 +00:00
|
|
|
|
|
|
|
/* look in the environment */
|
|
|
|
if ((env = getenv(name)) != NULL) {
|
|
|
|
/* craft this variable from the environment value */
|
|
|
|
return (VarCreate(name, env, VAR_FROM_ENV));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* deferred check for the environment (in case of -e/-E) */
|
|
|
|
if ((checkEnvFirst || localCheckEnvFirst) &&
|
|
|
|
(flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL)) {
|
|
|
|
var = Lst_Find(&VAR_GLOBAL->context, name, VarCmp);
|
|
|
|
if (var != NULL)
|
|
|
|
return (Lst_Datum(var));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2004-12-01 10:29:20 +00:00
|
|
|
return (NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* VarAdd --
|
2002-10-09 03:42:10 +00:00
|
|
|
* Add a new variable of name name and value val to the given context.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-02-04 07:51:00 +00:00
|
|
|
VarAdd(const char *name, const char *val, GNode *ctxt)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
Lst_AtFront(&ctxt->context, VarCreate(name, val, 0));
|
|
|
|
DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, name, val));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Var_Delete --
|
|
|
|
* Remove a variable from a context.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* The Var structure is removed and freed.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2005-02-04 12:10:20 +00:00
|
|
|
Var_Delete(const char *name, GNode *ctxt)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-03-18 15:44:23 +00:00
|
|
|
LstNode *ln;
|
|
|
|
|
|
|
|
DEBUGF(VAR, ("%s:delete %s\n", ctxt->name, name));
|
|
|
|
ln = Lst_Find(&ctxt->context, name, VarCmp);
|
|
|
|
if (ln != NULL) {
|
|
|
|
VarDestroy(Lst_Datum(ln), TRUE);
|
|
|
|
Lst_Remove(&ctxt->context, ln);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Var_Set --
|
|
|
|
* Set the variable name to the value val in the given context.
|
|
|
|
*
|
|
|
|
* 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 $(<).
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2005-02-04 12:10:20 +00:00
|
|
|
Var_Set(const char *name, const char *val, GNode *ctxt)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-03-18 15:44:23 +00:00
|
|
|
Var *v;
|
|
|
|
char *n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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...
|
|
|
|
*/
|
|
|
|
n = VarPossiblyExpand(name, ctxt);
|
|
|
|
v = VarFind(n, ctxt, 0);
|
|
|
|
if (v == NULL) {
|
|
|
|
VarAdd(n, val, ctxt);
|
|
|
|
} else {
|
|
|
|
Buf_Clear(v->val);
|
|
|
|
Buf_Append(v->val, val);
|
|
|
|
|
|
|
|
DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, n, val));
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Any variables given on the command line are automatically exported
|
|
|
|
* to the environment (as per POSIX standard)
|
|
|
|
*/
|
|
|
|
if (ctxt == VAR_CMD) {
|
|
|
|
setenv(n, val, 1);
|
|
|
|
}
|
|
|
|
free(n);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Var_Append --
|
|
|
|
* The variable of the given name has the given value appended to it in
|
|
|
|
* the given context.
|
|
|
|
*
|
|
|
|
* 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
|
2005-02-04 12:10:20 +00:00
|
|
|
Var_Append(const char *name, const char *val, GNode *ctxt)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-03-18 15:44:23 +00:00
|
|
|
Var *v;
|
|
|
|
char *n;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
n = VarPossiblyExpand(name, ctxt);
|
|
|
|
v = VarFind(n, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
if (v == NULL) {
|
|
|
|
VarAdd(n, val, ctxt);
|
|
|
|
} else {
|
|
|
|
Buf_AddByte(v->val, (Byte)' ');
|
|
|
|
Buf_Append(v->val, val);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, n,
|
|
|
|
(char *)Buf_GetAll(v->val, (size_t *)NULL)));
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
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;
|
|
|
|
Lst_AtFront(&ctxt->context, v);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-03-18 15:44:23 +00:00
|
|
|
free(n);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Var_Exists --
|
|
|
|
* See if the given variable exists.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* TRUE if it does, FALSE if it doesn't
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Boolean
|
2005-02-04 12:10:20 +00:00
|
|
|
Var_Exists(const char *name, GNode *ctxt)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-03-18 15:44:23 +00:00
|
|
|
Var *v;
|
|
|
|
char *n;
|
|
|
|
|
|
|
|
n = VarPossiblyExpand(name, ctxt);
|
|
|
|
v = VarFind(n, ctxt, FIND_CMD | FIND_GLOBAL | FIND_ENV);
|
|
|
|
free(n);
|
|
|
|
|
|
|
|
if (v == NULL) {
|
|
|
|
return (FALSE);
|
|
|
|
} else if (v->flags & VAR_FROM_ENV) {
|
|
|
|
VarDestroy(v, TRUE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Var_Value --
|
|
|
|
* Return the value of the named variable in the given context
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* The value if the variable exists, NULL if it doesn't
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
char *
|
2005-02-04 12:10:20 +00:00
|
|
|
Var_Value(const char *name, GNode *ctxt, char **frp)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-03-18 15:44:23 +00:00
|
|
|
Var *v;
|
|
|
|
char *n;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
n = VarPossiblyExpand(name, ctxt);
|
|
|
|
v = VarFind(n, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
|
|
|
|
free(n);
|
|
|
|
*frp = NULL;
|
|
|
|
if (v != NULL) {
|
|
|
|
char *p = (char *)Buf_GetAll(v->val, (size_t *)NULL);
|
2004-12-01 10:29:20 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
if (v->flags & VAR_FROM_ENV) {
|
|
|
|
VarDestroy(v, FALSE);
|
|
|
|
*frp = p;
|
|
|
|
}
|
|
|
|
return (p);
|
|
|
|
} else {
|
|
|
|
return (NULL);
|
1995-01-23 21:03:17 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* VarModify --
|
|
|
|
* Modify each of the words of the passed string using the given
|
|
|
|
* function. Used to implement all modifiers.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* A string of all the words modified appropriately.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
2005-03-07 16:23:53 +00:00
|
|
|
* Uses brk_string() so it invalidates any previous call to
|
|
|
|
* brk_string().
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static char *
|
2005-03-07 16:23:53 +00:00
|
|
|
VarModify(const char *str, VarModifyProc *modProc, void *datum)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-03-07 16:23:53 +00:00
|
|
|
char **av; /* word list [first word does not count] */
|
|
|
|
int ac;
|
2005-03-07 16:14:50 +00:00
|
|
|
Buffer *buf; /* Buffer for the new string */
|
|
|
|
Boolean addSpace; /* TRUE if need to add a space to the buffer
|
|
|
|
* before adding the trimmed word */
|
|
|
|
int i;
|
2005-03-07 16:23:53 +00:00
|
|
|
char *result;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-03-07 16:14:50 +00:00
|
|
|
av = brk_string(str, &ac, FALSE);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-03-07 16:23:53 +00:00
|
|
|
buf = Buf_Init(0);
|
|
|
|
|
|
|
|
addSpace = FALSE;
|
2005-03-07 16:14:50 +00:00
|
|
|
for (i = 1; i < ac; i++)
|
|
|
|
addSpace = (*modProc)(av[i], addSpace, buf, datum);
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2005-03-07 16:23:53 +00:00
|
|
|
result = (char *)Buf_GetAll(buf, (size_t *)NULL);
|
2005-03-07 16:14:50 +00:00
|
|
|
Buf_Destroy(buf, FALSE);
|
2005-03-07 16:23:53 +00:00
|
|
|
return (result);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2003-09-18 03:15:57 +00:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* VarSortWords --
|
|
|
|
* Sort the words in the string.
|
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* str String whose words should be sorted
|
|
|
|
* cmp A comparison function to control the ordering
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* A string containing the words sorted
|
|
|
|
*
|
|
|
|
* Side Effects:
|
2005-03-18 15:44:23 +00:00
|
|
|
* Uses brk_string() so it invalidates any previous call to
|
2005-03-07 16:23:53 +00:00
|
|
|
* brk_string().
|
2003-09-18 03:15:57 +00:00
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static char *
|
2005-03-07 16:23:53 +00:00
|
|
|
VarSortWords(const char *str, int (*cmp)(const void *, const void *))
|
2003-09-18 03:15:57 +00:00
|
|
|
{
|
2005-03-07 16:23:53 +00:00
|
|
|
char **av;
|
|
|
|
int ac;
|
|
|
|
Buffer *buf;
|
|
|
|
int i;
|
|
|
|
char *result;
|
2003-09-18 03:15:57 +00:00
|
|
|
|
|
|
|
av = brk_string(str, &ac, FALSE);
|
2004-12-01 10:29:20 +00:00
|
|
|
qsort(av + 1, ac - 1, sizeof(char *), cmp);
|
2005-03-07 16:23:53 +00:00
|
|
|
|
|
|
|
buf = Buf_Init(0);
|
2003-09-18 03:15:57 +00:00
|
|
|
for (i = 1; i < ac; i++) {
|
2005-02-07 07:49:16 +00:00
|
|
|
Buf_Append(buf, av[i]);
|
2003-09-18 03:15:57 +00:00
|
|
|
Buf_AddByte(buf, (Byte)((i < ac - 1) ? ' ' : '\0'));
|
|
|
|
}
|
2005-03-07 16:23:53 +00:00
|
|
|
|
|
|
|
result = (char *)Buf_GetAll(buf, (size_t *)NULL);
|
2003-09-18 03:15:57 +00:00
|
|
|
Buf_Destroy(buf, FALSE);
|
2005-03-07 16:23:53 +00:00
|
|
|
return (result);
|
2003-09-18 03:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
SortIncreasing(const void *l, const void *r)
|
|
|
|
{
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2003-09-18 03:15:57 +00:00
|
|
|
return (strcmp(*(const char* const*)l, *(const char* const*)r));
|
|
|
|
}
|
|
|
|
|
2000-10-09 04:31:43 +00:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* VarGetPattern --
|
|
|
|
* Pass through the tstr looking for 1) escaped delimiters,
|
|
|
|
* '$'s and backslashes (place the escaped character in
|
|
|
|
* uninterpreted) and 2) unescaped $'s that aren't before
|
|
|
|
* the delimiter (expand the variable substitution unless flags
|
|
|
|
* has VAR_NOSUBST set).
|
|
|
|
* Return the expanded string or NULL if the delimiter was missing
|
|
|
|
* If pattern is specified, handle escaped ampersands, and replace
|
|
|
|
* unescaped ampersands with the lhs of the pattern.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* A string of all the words modified appropriately.
|
|
|
|
* If length is specified, return the string length of the buffer
|
|
|
|
* If flags is specified and the last character of the pattern is a
|
|
|
|
* $ set the VAR_MATCH_END bit of flags.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None.
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static char *
|
2005-03-18 15:34:07 +00:00
|
|
|
VarGetPattern(VarParser *vp, const char **tstr, int delim, int *flags,
|
2004-12-03 12:55:57 +00:00
|
|
|
size_t *length, VarPattern *pattern)
|
2000-10-09 04:31:43 +00:00
|
|
|
{
|
2005-03-18 15:44:23 +00:00
|
|
|
const char *cp;
|
|
|
|
Buffer *buf = Buf_Init(0);
|
|
|
|
size_t junk;
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
if (length == NULL)
|
|
|
|
length = &junk;
|
2000-10-09 04:31:43 +00:00
|
|
|
|
2002-09-17 21:29:06 +00:00
|
|
|
#define IS_A_MATCH(cp, delim) \
|
2000-10-09 04:31:43 +00:00
|
|
|
((cp[0] == '\\') && ((cp[1] == delim) || \
|
|
|
|
(cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&'))))
|
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
/*
|
|
|
|
* Skim through until the matching delimiter is found; pick up
|
|
|
|
* variable substitutions on the way. Also allow backslashes to quote
|
|
|
|
* the delimiter, $, and \, but don't touch other backslashes.
|
|
|
|
*/
|
|
|
|
for (cp = *tstr; *cp && (*cp != delim); cp++) {
|
|
|
|
if (IS_A_MATCH(cp, delim)) {
|
|
|
|
Buf_AddByte(buf, (Byte)cp[1]);
|
|
|
|
cp++;
|
|
|
|
} else if (*cp == '$') {
|
|
|
|
if (cp[1] == delim) {
|
|
|
|
if (flags == NULL)
|
|
|
|
Buf_AddByte(buf, (Byte)*cp);
|
|
|
|
else
|
|
|
|
/*
|
|
|
|
* Unescaped $ at end of pattern =>
|
|
|
|
* anchor pattern at end.
|
|
|
|
*/
|
|
|
|
*flags |= VAR_MATCH_END;
|
|
|
|
} else {
|
|
|
|
if (flags == NULL ||
|
|
|
|
(*flags & VAR_NOSUBST) == 0) {
|
|
|
|
char *cp2;
|
|
|
|
size_t len;
|
|
|
|
Boolean freeIt;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If unescaped dollar sign not
|
|
|
|
* before the delimiter, assume it's
|
|
|
|
* a variable substitution and
|
|
|
|
* recurse.
|
|
|
|
*/
|
|
|
|
len = 0;
|
|
|
|
cp2 = Var_Parse(cp, vp->ctxt, vp->err, &len, &freeIt);
|
|
|
|
Buf_Append(buf, cp2);
|
|
|
|
if (freeIt)
|
|
|
|
free(cp2);
|
|
|
|
cp += len - 1;
|
|
|
|
} else {
|
|
|
|
const char *cp2 = &cp[1];
|
|
|
|
|
|
|
|
if (*cp2 == OPEN_PAREN ||
|
|
|
|
*cp2 == OPEN_BRACE) {
|
|
|
|
/*
|
|
|
|
* Find the end of this
|
|
|
|
* variable reference and
|
|
|
|
* suck it in without further
|
|
|
|
* ado. It will be
|
|
|
|
* interperated later.
|
|
|
|
*/
|
|
|
|
int have = *cp2;
|
|
|
|
int want = (*cp2 == OPEN_PAREN) ? CLOSE_PAREN : CLOSE_BRACE;
|
|
|
|
int depth = 1;
|
|
|
|
|
|
|
|
for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) {
|
|
|
|
if (cp2[-1] != '\\') {
|
|
|
|
if (*cp2 == have)
|
|
|
|
++depth;
|
|
|
|
if (*cp2 == want)
|
|
|
|
--depth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Buf_AppendRange(buf, cp, cp2);
|
|
|
|
cp = --cp2;
|
|
|
|
} else
|
|
|
|
Buf_AddByte(buf, (Byte)*cp);
|
|
|
|
}
|
2000-10-09 04:31:43 +00:00
|
|
|
}
|
2005-03-18 15:44:23 +00:00
|
|
|
} else if (pattern && *cp == '&')
|
|
|
|
Buf_AddBytes(buf, pattern->leftLen, (Byte *) pattern->lhs);
|
|
|
|
else
|
2004-11-30 17:46:29 +00:00
|
|
|
Buf_AddByte(buf, (Byte)*cp);
|
2000-10-09 04:31:43 +00:00
|
|
|
}
|
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
Buf_AddByte(buf, (Byte)'\0');
|
2000-10-09 04:31:43 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
if (*cp != delim) {
|
|
|
|
*tstr = cp;
|
|
|
|
*length = 0;
|
|
|
|
return (NULL);
|
|
|
|
} else {
|
|
|
|
char *result;
|
|
|
|
*tstr = ++cp;
|
|
|
|
result = (char *)Buf_GetAll(buf, length);
|
|
|
|
*length -= 1; /* Don't count the NULL */
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
return (result);
|
|
|
|
}
|
2000-10-09 04:31:43 +00:00
|
|
|
}
|
|
|
|
|
1999-04-19 07:30:04 +00:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
2004-08-12 11:49:55 +00:00
|
|
|
* Var_Quote --
|
1999-04-19 07:30:04 +00:00
|
|
|
* Quote shell meta-characters in the string
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* The quoted string
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
2004-08-12 11:49:55 +00:00
|
|
|
char *
|
|
|
|
Var_Quote(const char *str)
|
1999-04-19 07:30:04 +00:00
|
|
|
{
|
2005-03-18 15:44:23 +00:00
|
|
|
Buffer *buf;
|
|
|
|
/* This should cover most shells :-( */
|
|
|
|
static char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
buf = Buf_Init(MAKE_BSIZE);
|
|
|
|
for (; *str; str++) {
|
|
|
|
if (strchr(meta, *str) != NULL)
|
|
|
|
Buf_AddByte(buf, (Byte)'\\');
|
|
|
|
Buf_AddByte(buf, (Byte)*str);
|
|
|
|
}
|
|
|
|
Buf_AddByte(buf, (Byte)'\0');
|
|
|
|
ret = Buf_GetAll(buf, NULL);
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
return (ret);
|
2002-10-28 23:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* VarREError --
|
|
|
|
* Print the error caused by a regcomp or regexec call.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* An error gets printed.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
VarREError(int err, regex_t *pat, const char *str)
|
|
|
|
{
|
2005-03-18 15:44:23 +00:00
|
|
|
char *errbuf;
|
|
|
|
int errlen;
|
|
|
|
|
|
|
|
errlen = regerror(err, pat, 0, 0);
|
|
|
|
errbuf = emalloc(errlen);
|
|
|
|
regerror(err, pat, errbuf, errlen);
|
|
|
|
Error("%s: %s", str, errbuf);
|
|
|
|
free(errbuf);
|
1999-04-19 07:30:04 +00:00
|
|
|
}
|
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
/**
|
2005-02-26 08:54:40 +00:00
|
|
|
* Make sure this variable is fully expanded.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2005-02-26 08:54:40 +00:00
|
|
|
static char *
|
2005-03-18 15:34:07 +00:00
|
|
|
VarExpand(Var *v, VarParser *vp)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-02-26 08:54:40 +00:00
|
|
|
char *value;
|
|
|
|
char *result;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2005-02-26 08:54:40 +00:00
|
|
|
if (v->flags & VAR_IN_USE) {
|
|
|
|
Fatal("Variable %s is recursive.", v->name);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
v->flags |= VAR_IN_USE;
|
2005-02-17 12:31:53 +00:00
|
|
|
|
|
|
|
/*
|
2005-02-26 08:54:40 +00:00
|
|
|
* 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.
|
2005-02-17 12:31:53 +00:00
|
|
|
*/
|
2005-02-26 08:54:40 +00:00
|
|
|
value = (char *)Buf_GetAll(v->val, (size_t *)NULL);
|
|
|
|
if (strchr(value, '$') == NULL) {
|
|
|
|
result = strdup(value);
|
|
|
|
} else {
|
|
|
|
Buffer *buf;
|
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
buf = Var_Subst(NULL, value, vp->ctxt, vp->err);
|
2005-02-26 08:54:40 +00:00
|
|
|
result = Buf_GetAll(buf, NULL);
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
v->flags &= ~VAR_IN_USE;
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2005-03-07 12:43:59 +00:00
|
|
|
/**
|
2005-03-18 15:37:31 +00:00
|
|
|
* Select only those words in value that match the modifier.
|
2005-03-07 12:43:59 +00:00
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
modifier_M(const char mod[], const char value[], char endc, size_t *consumed)
|
|
|
|
{
|
|
|
|
const char *cur;
|
|
|
|
char *patt;
|
|
|
|
char *ptr;
|
|
|
|
char *newValue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compress the \:'s out of the pattern, so allocate enough
|
|
|
|
* room to hold the uncompressed pattern and compress the
|
2005-03-15 15:05:14 +00:00
|
|
|
* pattern into that space.
|
2005-03-07 12:43:59 +00:00
|
|
|
*/
|
2005-03-15 15:05:14 +00:00
|
|
|
patt = estrdup(mod);
|
2005-03-07 12:43:59 +00:00
|
|
|
ptr = patt;
|
2005-03-15 15:05:14 +00:00
|
|
|
for (cur = mod + 1; cur != '\0'; cur++) {
|
|
|
|
if ((cur[0] == endc) || (cur[0] == ':')) {
|
|
|
|
break;
|
|
|
|
}
|
2005-03-07 12:43:59 +00:00
|
|
|
if ((cur[0] == '\\') &&
|
2005-03-15 15:05:14 +00:00
|
|
|
((cur[1] == endc) || (cur[1] == ':'))) {
|
|
|
|
cur++; /* skip over backslash */
|
2005-03-07 12:43:59 +00:00
|
|
|
}
|
|
|
|
*ptr = *cur;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
if (*mod == 'M' || *mod == 'm') {
|
|
|
|
newValue = VarModify(value, VarMatch, patt);
|
|
|
|
} else {
|
|
|
|
newValue = VarModify(value, VarNoMatch, patt);
|
|
|
|
}
|
|
|
|
free(patt);
|
|
|
|
|
2005-03-15 15:05:14 +00:00
|
|
|
*consumed += (cur - mod);
|
2005-03-07 12:43:59 +00:00
|
|
|
|
2005-03-15 15:05:14 +00:00
|
|
|
if (*cur == ':') {
|
2005-03-07 12:43:59 +00:00
|
|
|
*consumed += 1; /* include colon as part of modifier */
|
|
|
|
}
|
|
|
|
|
|
|
|
return (newValue);
|
|
|
|
}
|
|
|
|
|
2005-03-18 15:37:31 +00:00
|
|
|
/**
|
|
|
|
* Substitute the replacement string for the pattern. The substitution
|
|
|
|
* is applied to each word in value.
|
|
|
|
*/
|
2005-03-07 16:08:39 +00:00
|
|
|
static char *
|
2005-03-18 15:34:07 +00:00
|
|
|
modifier_S(const char mod[], const char value[], Var *v, VarParser *vp, size_t *consumed)
|
2005-03-07 16:08:39 +00:00
|
|
|
{
|
|
|
|
VarPattern pattern;
|
|
|
|
Buffer *buf; /* Buffer for patterns */
|
|
|
|
char delim;
|
|
|
|
const char *cur;
|
|
|
|
char *newValue;
|
|
|
|
|
|
|
|
pattern.flags = 0;
|
|
|
|
buf = Buf_Init(0);
|
|
|
|
|
|
|
|
delim = mod[1]; /* used to find end of pattern */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If pattern begins with '^', it is anchored to the start of the
|
|
|
|
* word -- skip over it and flag pattern.
|
|
|
|
*/
|
|
|
|
if (mod[2] == '^') {
|
|
|
|
pattern.flags |= VAR_MATCH_START;
|
|
|
|
cur = mod + 3;
|
|
|
|
} else {
|
|
|
|
cur = mod + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pass through the lhs looking for 1) escaped delimiters, '$'s and
|
|
|
|
* backslashes (place the escaped character in uninterpreted) and 2)
|
|
|
|
* unescaped $'s that aren't before the delimiter (expand the
|
|
|
|
* variable substitution). The result is left in the Buffer buf.
|
|
|
|
*/
|
|
|
|
while (cur[0] != delim) {
|
|
|
|
if (cur[0] == '\0') {
|
|
|
|
/*
|
|
|
|
* LHS didn't end with the delim, complain and exit.
|
|
|
|
*/
|
|
|
|
Fatal("Unclosed substitution for %s (%c missing)",
|
2005-03-18 15:44:23 +00:00
|
|
|
v->name, delim);
|
2005-03-07 16:08:39 +00:00
|
|
|
|
|
|
|
} else if ((cur[0] == '\\') &&
|
|
|
|
((cur[1] == delim) ||
|
|
|
|
(cur[1] == '$') ||
|
|
|
|
(cur[1] == '\\'))) {
|
|
|
|
cur++; /* skip backslash */
|
2005-03-18 15:44:23 +00:00
|
|
|
Buf_AddByte(buf, (Byte)cur[0]);
|
2005-03-07 16:08:39 +00:00
|
|
|
cur++;
|
|
|
|
|
|
|
|
} else if (cur[0] == '$') {
|
|
|
|
if (cur[1] == delim) {
|
|
|
|
/*
|
|
|
|
* Unescaped $ at end of pattern => anchor
|
|
|
|
* pattern at end.
|
|
|
|
*/
|
|
|
|
pattern.flags |= VAR_MATCH_END;
|
|
|
|
cur++;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If unescaped dollar sign not before the
|
|
|
|
* delimiter, assume it's a variable
|
|
|
|
* substitution and recurse.
|
|
|
|
*/
|
|
|
|
char *cp2;
|
|
|
|
size_t len;
|
|
|
|
Boolean freeIt;
|
|
|
|
|
|
|
|
len = 0;
|
2005-03-18 15:34:07 +00:00
|
|
|
cp2 = Var_Parse(cur, vp->ctxt, vp->err, &len, &freeIt);
|
2005-03-07 16:08:39 +00:00
|
|
|
cur += len;
|
|
|
|
Buf_Append(buf, cp2);
|
|
|
|
if (freeIt) {
|
|
|
|
free(cp2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Buf_AddByte(buf, (Byte)cur[0]);
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cur++; /* skip over delim */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch pattern and destroy buffer, but preserve the data in it,
|
|
|
|
* since that's our lhs.
|
|
|
|
*/
|
|
|
|
pattern.lhs = (char *)Buf_GetAll(buf, &pattern.leftLen);
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now comes the replacement string. Three things need to be done
|
|
|
|
* here: 1) need to compress escaped delimiters and ampersands and 2)
|
|
|
|
* need to replace unescaped ampersands with the l.h.s. (since this
|
|
|
|
* isn't regexp, we can do it right here) and 3) expand any variable
|
|
|
|
* substitutions.
|
|
|
|
*/
|
|
|
|
buf = Buf_Init(0);
|
|
|
|
|
|
|
|
while (cur[0] != delim) {
|
|
|
|
if (cur[0] == '\0') {
|
|
|
|
/*
|
|
|
|
* Didn't end with delim character, complain
|
|
|
|
*/
|
|
|
|
Fatal("Unclosed substitution for %s (%c missing)",
|
2005-03-18 15:44:23 +00:00
|
|
|
v->name, delim);
|
2005-03-07 16:08:39 +00:00
|
|
|
|
|
|
|
} else if ((cur[0] == '\\') &&
|
|
|
|
((cur[1] == delim) ||
|
|
|
|
(cur[1] == '&') ||
|
|
|
|
(cur[1] == '\\') ||
|
|
|
|
(cur[1] == '$'))) {
|
|
|
|
cur++; /* skip backslash */
|
2005-03-18 15:44:23 +00:00
|
|
|
Buf_AddByte(buf, (Byte)cur[0]);
|
2005-03-07 16:08:39 +00:00
|
|
|
cur++;
|
|
|
|
|
|
|
|
} else if (cur[0] == '$') {
|
|
|
|
if (cur[1] == delim) {
|
2005-03-18 15:44:23 +00:00
|
|
|
Buf_AddByte(buf, (Byte)cur[0]);
|
2005-03-07 16:08:39 +00:00
|
|
|
cur++;
|
|
|
|
} else {
|
|
|
|
char *cp2;
|
|
|
|
size_t len;
|
|
|
|
Boolean freeIt;
|
|
|
|
|
|
|
|
len = 0;
|
2005-03-18 15:34:07 +00:00
|
|
|
cp2 = Var_Parse(cur, vp->ctxt, vp->err, &len, &freeIt);
|
2005-03-07 16:08:39 +00:00
|
|
|
cur += len;
|
|
|
|
Buf_Append(buf, cp2);
|
|
|
|
if (freeIt) {
|
|
|
|
free(cp2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (cur[0] == '&') {
|
|
|
|
Buf_AddBytes(buf, pattern.leftLen, (Byte *)pattern.lhs);
|
|
|
|
cur++;
|
|
|
|
} else {
|
2005-03-18 15:44:23 +00:00
|
|
|
Buf_AddByte(buf, (Byte)cur[0]);
|
2005-03-07 16:08:39 +00:00
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cur++; /* skip over delim */
|
|
|
|
|
|
|
|
pattern.rhs = (char *)Buf_GetAll(buf, &pattern.rightLen);
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for global substitution. If 'g' after the final delimiter,
|
|
|
|
* substitution is global and is marked that way.
|
|
|
|
*/
|
|
|
|
if (cur[0] == 'g') {
|
|
|
|
pattern.flags |= VAR_SUB_GLOBAL;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Global substitution of the empty string causes an infinite number
|
|
|
|
* of matches, unless anchored by '^' (start of string) or '$' (end
|
|
|
|
* of string). Catch the infinite substitution here. Note that flags
|
|
|
|
* can only contain the 3 bits we're interested in so we don't have
|
|
|
|
* to mask unrelated bits. We can test for equality.
|
|
|
|
*/
|
|
|
|
if (!pattern.leftLen && pattern.flags == VAR_SUB_GLOBAL)
|
|
|
|
Fatal("Global substitution of the empty string");
|
|
|
|
|
|
|
|
newValue = VarModify(value, VarSubstitute, &pattern);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the two strings.
|
|
|
|
*/
|
|
|
|
free(pattern.lhs);
|
|
|
|
free(pattern.rhs);
|
|
|
|
|
|
|
|
*consumed += (cur - mod);
|
|
|
|
|
|
|
|
if (cur[0] == ':') {
|
|
|
|
*consumed += 1; /* include colin as part of modifier */
|
|
|
|
}
|
|
|
|
|
|
|
|
return (newValue);
|
|
|
|
}
|
2005-03-07 12:43:59 +00:00
|
|
|
|
2005-03-18 15:37:31 +00:00
|
|
|
static char *
|
|
|
|
modifier_C(const char mod[], char value[], Var *v, VarParser *vp, size_t *consumed)
|
|
|
|
{
|
|
|
|
VarREPattern patt;
|
|
|
|
int delim;
|
|
|
|
char *re;
|
|
|
|
const char *cur;
|
|
|
|
int error;
|
|
|
|
char *newValue;
|
|
|
|
|
|
|
|
patt.flags = 0;
|
|
|
|
|
|
|
|
delim = mod[1]; /* delimiter between sections */
|
|
|
|
|
|
|
|
cur = mod + 2;
|
|
|
|
|
|
|
|
re = VarGetPattern(vp, &cur, delim, NULL, NULL, NULL);
|
|
|
|
if (re == NULL) {
|
|
|
|
Fatal("Unclosed substitution for %s (%c missing)",
|
2005-03-18 15:44:23 +00:00
|
|
|
v->name, delim);
|
2005-03-18 15:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
patt.replace = VarGetPattern(vp, &cur, delim, NULL, NULL, NULL);
|
|
|
|
if (patt.replace == NULL) {
|
|
|
|
Fatal("Unclosed substitution for %s (%c missing)",
|
2005-03-18 15:44:23 +00:00
|
|
|
v->name, delim);
|
2005-03-18 15:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;; cur++) {
|
|
|
|
switch (*cur) {
|
|
|
|
case 'g':
|
|
|
|
patt.flags |= VAR_SUB_GLOBAL;
|
|
|
|
continue;
|
|
|
|
case '1':
|
|
|
|
patt.flags |= VAR_SUB_ONE;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = regcomp(&patt.re, re, REG_EXTENDED);
|
|
|
|
if (error) {
|
|
|
|
*consumed = cur - mod;
|
|
|
|
VarREError(error, &patt.re, "RE substitution error");
|
|
|
|
free(patt.replace);
|
|
|
|
free(re);
|
|
|
|
return (var_Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
patt.nsub = patt.re.re_nsub + 1;
|
|
|
|
if (patt.nsub < 1)
|
|
|
|
patt.nsub = 1;
|
|
|
|
if (patt.nsub > 10)
|
|
|
|
patt.nsub = 10;
|
|
|
|
patt.matches = emalloc(patt.nsub * sizeof(regmatch_t));
|
|
|
|
|
|
|
|
newValue = VarModify(value, VarRESubstitute, &patt);
|
|
|
|
|
|
|
|
regfree(&patt.re);
|
|
|
|
free(patt.matches);
|
|
|
|
free(patt.replace);
|
|
|
|
free(re);
|
|
|
|
|
|
|
|
*consumed += (cur - mod);
|
|
|
|
|
|
|
|
if (cur[0] == ':') {
|
|
|
|
*consumed += 1; /* include colin as part of modifier */
|
|
|
|
}
|
|
|
|
return (newValue);
|
|
|
|
}
|
|
|
|
|
2005-03-01 12:26:32 +00:00
|
|
|
/*
|
|
|
|
* Now we need to apply any modifiers the user wants applied.
|
|
|
|
* These are:
|
|
|
|
* :M<pattern>
|
|
|
|
* words which match the given <pattern>.
|
|
|
|
* <pattern> is of the standard file
|
|
|
|
* wildcarding form.
|
|
|
|
* :S<d><pat1><d><pat2><d>[g]
|
|
|
|
* Substitute <pat2> for <pat1> in the value
|
|
|
|
* :C<d><pat1><d><pat2><d>[g]
|
|
|
|
* Substitute <pat2> for regex <pat1> in the value
|
|
|
|
* :H Substitute the head of each word
|
|
|
|
* :T Substitute the tail of each word
|
|
|
|
* :E Substitute the extension (minus '.') of
|
|
|
|
* each word
|
|
|
|
* :R Substitute the root of each word
|
|
|
|
* (pathname minus the suffix).
|
|
|
|
* :lhs=rhs
|
|
|
|
* Like :S, but the rhs goes to the end of
|
|
|
|
* the invocation.
|
|
|
|
* :U Converts variable to upper-case.
|
|
|
|
* :L Converts variable to lower-case.
|
|
|
|
*
|
|
|
|
* XXXHB update this comment or remove it and point to the man page.
|
|
|
|
*/
|
|
|
|
static char *
|
2005-03-08 07:43:57 +00:00
|
|
|
ParseModifier(const char input[], const char tstr[],
|
2005-03-01 12:26:32 +00:00
|
|
|
char startc, char endc, Boolean dynamic, Var *v,
|
2005-03-18 15:34:07 +00:00
|
|
|
VarParser *vp, size_t *lengthPtr, Boolean *freePtr)
|
2005-03-01 12:26:32 +00:00
|
|
|
{
|
2005-03-14 17:03:34 +00:00
|
|
|
char *value;
|
2005-03-08 07:43:57 +00:00
|
|
|
const char *cp;
|
2005-03-14 17:03:34 +00:00
|
|
|
size_t used;
|
2005-03-01 12:26:32 +00:00
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
value = VarExpand(v, vp);
|
2005-03-01 12:26:32 +00:00
|
|
|
*freePtr = TRUE;
|
|
|
|
|
|
|
|
tstr++;
|
|
|
|
while (*tstr != endc) {
|
2005-03-18 15:44:23 +00:00
|
|
|
char *newStr; /* New value to return */
|
|
|
|
char termc; /* Character which terminated scan */
|
|
|
|
Boolean readonly = FALSE;
|
|
|
|
size_t consumed = 0;
|
2005-03-01 12:26:32 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
DEBUGF(VAR, ("Applying :%c to \"%s\"\n", *tstr, value));
|
|
|
|
switch (*tstr) {
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'N':
|
|
|
|
case 'M':
|
2005-03-18 15:37:31 +00:00
|
|
|
readonly = TRUE;
|
2005-03-14 17:03:34 +00:00
|
|
|
newStr = modifier_M(tstr, value, endc, &consumed);
|
2005-03-07 12:43:59 +00:00
|
|
|
tstr += consumed;
|
2005-03-03 11:26:28 +00:00
|
|
|
break;
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'S':
|
2005-03-18 15:37:31 +00:00
|
|
|
readonly = TRUE;
|
2005-03-18 15:34:07 +00:00
|
|
|
newStr = modifier_S(tstr, value, v, vp, &consumed);
|
2005-03-07 16:08:39 +00:00
|
|
|
tstr += consumed;
|
|
|
|
break;
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'C':
|
2005-03-18 15:37:31 +00:00
|
|
|
readonly = TRUE;
|
|
|
|
newStr = modifier_C(tstr, value, v, vp, &consumed);
|
|
|
|
tstr += consumed;
|
2005-03-01 12:26:32 +00:00
|
|
|
break;
|
|
|
|
case 'L':
|
2005-03-18 15:44:23 +00:00
|
|
|
if (tstr[1] == endc || tstr[1] == ':') {
|
|
|
|
Buffer *buf;
|
|
|
|
buf = Buf_Init(MAKE_BSIZE);
|
|
|
|
for (cp = value; *cp; cp++)
|
|
|
|
Buf_AddByte(buf, (Byte)tolower(*cp));
|
|
|
|
|
|
|
|
Buf_AddByte(buf, (Byte)'\0');
|
|
|
|
newStr = (char *)Buf_GetAll(buf, (size_t *)NULL);
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
|
|
|
|
cp = tstr + 1;
|
|
|
|
termc = *cp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'O':
|
2005-03-18 15:44:23 +00:00
|
|
|
if (tstr[1] == endc || tstr[1] == ':') {
|
|
|
|
newStr = VarSortWords(value, SortIncreasing);
|
|
|
|
cp = tstr + 1;
|
|
|
|
termc = *cp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'Q':
|
2005-03-18 15:44:23 +00:00
|
|
|
if (tstr[1] == endc || tstr[1] == ':') {
|
|
|
|
newStr = Var_Quote(value);
|
|
|
|
cp = tstr + 1;
|
|
|
|
termc = *cp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHRU */
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'T':
|
2005-03-18 15:44:23 +00:00
|
|
|
if (tstr[1] == endc || tstr[1] == ':') {
|
|
|
|
newStr = VarModify(value, VarTail, (void *)NULL);
|
|
|
|
cp = tstr + 1;
|
|
|
|
termc = *cp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHRU */
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'U':
|
2005-03-18 15:44:23 +00:00
|
|
|
if (tstr[1] == endc || tstr[1] == ':') {
|
|
|
|
Buffer *buf;
|
|
|
|
buf = Buf_Init(MAKE_BSIZE);
|
|
|
|
for (cp = value; *cp; cp++)
|
|
|
|
Buf_AddByte(buf, (Byte)toupper(*cp));
|
|
|
|
|
|
|
|
Buf_AddByte(buf, (Byte)'\0');
|
|
|
|
newStr = (char *)Buf_GetAll(buf, (size_t *)NULL);
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
|
|
|
|
cp = tstr + 1;
|
|
|
|
termc = *cp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'H':
|
2005-03-18 15:44:23 +00:00
|
|
|
if (tstr[1] == endc || tstr[1] == ':') {
|
|
|
|
newStr = VarModify(value, VarHead, (void *)NULL);
|
|
|
|
cp = tstr + 1;
|
|
|
|
termc = *cp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHRU */
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'E':
|
2005-03-18 15:44:23 +00:00
|
|
|
if (tstr[1] == endc || tstr[1] == ':') {
|
|
|
|
newStr = VarModify(value, VarSuffix, (void *)NULL);
|
|
|
|
cp = tstr + 1;
|
|
|
|
termc = *cp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHRU */
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'R':
|
2005-03-18 15:44:23 +00:00
|
|
|
if (tstr[1] == endc || tstr[1] == ':') {
|
|
|
|
newStr = VarModify(value, VarRoot, (void *)NULL);
|
|
|
|
cp = tstr + 1;
|
|
|
|
termc = *cp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHRU */
|
2005-03-01 12:26:32 +00:00
|
|
|
#ifdef SUNSHCMD
|
|
|
|
case 's':
|
2005-03-18 15:44:23 +00:00
|
|
|
if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) {
|
|
|
|
const char *error;
|
|
|
|
Buffer *buf;
|
|
|
|
|
|
|
|
buf = Cmd_Exec(value, &error);
|
|
|
|
newStr = Buf_GetAll(buf, NULL);
|
|
|
|
Buf_Destroy(buf, FALSE);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
Error(error, value);
|
|
|
|
cp = tstr + 2;
|
|
|
|
termc = *cp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHRU */
|
2005-03-01 12:26:32 +00:00
|
|
|
#endif
|
|
|
|
default:
|
2005-03-18 15:44:23 +00:00
|
|
|
{
|
2005-03-01 12:26:32 +00:00
|
|
|
#ifdef SYSVVARSUB
|
2005-03-18 15:44:23 +00:00
|
|
|
/*
|
|
|
|
* This can either be a bogus modifier or a
|
|
|
|
* System-V substitution command.
|
|
|
|
*/
|
|
|
|
VarPattern pattern;
|
|
|
|
Boolean eqFound;
|
|
|
|
int cnt;
|
2005-03-01 12:26:32 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
pattern.flags = 0;
|
|
|
|
eqFound = FALSE;
|
2005-03-01 12:26:32 +00:00
|
|
|
/*
|
2005-03-18 15:44:23 +00:00
|
|
|
* First we make a pass through the string
|
|
|
|
* trying to verify it is a SYSV-make-style
|
|
|
|
* translation: it must be:
|
|
|
|
* <string1>=<string2>)
|
2005-03-01 12:26:32 +00:00
|
|
|
*/
|
|
|
|
cp = tstr;
|
2005-03-18 15:44:23 +00:00
|
|
|
cnt = 1;
|
|
|
|
while (*cp != '\0' && cnt) {
|
|
|
|
if (*cp == '=') {
|
|
|
|
eqFound = TRUE;
|
|
|
|
/* continue looking for endc */
|
|
|
|
} else if (*cp == endc)
|
|
|
|
cnt--;
|
|
|
|
else if (*cp == startc)
|
|
|
|
cnt++;
|
|
|
|
if (cnt)
|
|
|
|
cp++;
|
2005-03-01 12:26:32 +00:00
|
|
|
}
|
2005-03-18 15:44:23 +00:00
|
|
|
if (*cp == endc && eqFound) {
|
|
|
|
int delim;
|
2005-03-01 12:26:32 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
/*
|
|
|
|
* Now we break this sucker into the
|
|
|
|
* lhs and rhs. We must null
|
|
|
|
* terminate them of course.
|
|
|
|
*/
|
|
|
|
cp = tstr;
|
|
|
|
|
|
|
|
delim = '=';
|
|
|
|
if ((pattern.lhs = VarGetPattern(vp,
|
|
|
|
&cp, delim, &pattern.flags, &pattern.leftLen,
|
|
|
|
NULL)) == NULL) {
|
|
|
|
*lengthPtr = cp - input + 1;
|
|
|
|
if (*freePtr)
|
|
|
|
free(value);
|
|
|
|
if (delim != '\0')
|
|
|
|
Fatal("Unclosed substitution for %s (%c missing)",
|
|
|
|
v->name, delim);
|
|
|
|
return (var_Error);
|
|
|
|
}
|
|
|
|
delim = endc;
|
|
|
|
|
|
|
|
pattern.rhs = VarGetPattern(vp, &cp,
|
|
|
|
delim, NULL, &pattern.rightLen, &pattern);
|
|
|
|
if (pattern.rhs == NULL) {
|
|
|
|
*lengthPtr = cp - input + 1;
|
|
|
|
if (*freePtr)
|
|
|
|
free(value);
|
|
|
|
if (delim != '\0')
|
|
|
|
Fatal("Unclosed substitution for %s (%c missing)",
|
|
|
|
v->name, delim);
|
|
|
|
return (var_Error);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* SYSV modifications happen through
|
|
|
|
* the whole string. Note the pattern
|
|
|
|
* is anchored at the end.
|
|
|
|
*/
|
|
|
|
termc = *--cp;
|
|
|
|
delim = '\0';
|
|
|
|
newStr = VarModify(value, VarSYSVMatch, &pattern);
|
2005-03-01 12:26:32 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
free(pattern.lhs);
|
|
|
|
free(pattern.rhs);
|
2005-03-01 12:26:32 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
termc = endc;
|
|
|
|
} else
|
2005-03-01 12:26:32 +00:00
|
|
|
#endif
|
2005-03-18 15:44:23 +00:00
|
|
|
{
|
|
|
|
Error("Unknown modifier '%c'\n", *tstr);
|
|
|
|
for (cp = tstr + 1;
|
|
|
|
*cp != ':' && *cp != endc && *cp != '\0';
|
|
|
|
cp++)
|
|
|
|
continue;
|
|
|
|
termc = *cp;
|
|
|
|
newStr = var_Error;
|
|
|
|
}
|
2005-03-01 12:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-18 15:44:23 +00:00
|
|
|
|
|
|
|
DEBUGF(VAR, ("Result is \"%s\"\n", newStr));
|
|
|
|
if (*freePtr) {
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
value = newStr;
|
|
|
|
if (value != var_Error) {
|
|
|
|
*freePtr = TRUE;
|
|
|
|
} else {
|
|
|
|
*freePtr = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (readonly == FALSE) {
|
|
|
|
if (termc == '\0') {
|
|
|
|
Error("Unclosed variable specification for %s",
|
|
|
|
v->name);
|
|
|
|
} else if (termc == ':') {
|
|
|
|
cp++;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
tstr = cp;
|
|
|
|
}
|
2005-03-01 12:26:32 +00:00
|
|
|
}
|
|
|
|
|
2005-03-14 17:03:34 +00:00
|
|
|
used = tstr - input + 1;
|
|
|
|
*lengthPtr = used;
|
|
|
|
|
2005-03-01 12:26:32 +00:00
|
|
|
if (v->flags & VAR_FROM_ENV) {
|
2005-03-14 17:03:34 +00:00
|
|
|
if (value == (char *)Buf_GetAll(v->val, (size_t *)NULL)) {
|
|
|
|
VarDestroy(v, FALSE);
|
|
|
|
*freePtr = TRUE;
|
|
|
|
return (value);
|
|
|
|
} else {
|
|
|
|
VarDestroy(v, TRUE);
|
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
} else if (v->flags & VAR_JUNK) {
|
2005-03-01 12:26:32 +00:00
|
|
|
/*
|
2005-03-18 15:44:23 +00:00
|
|
|
* Perform any free'ing needed and set *freePtr to FALSE so
|
|
|
|
* the caller doesn't try to free a static pointer.
|
2005-03-01 12:26:32 +00:00
|
|
|
*/
|
2005-03-14 17:03:34 +00:00
|
|
|
if (*freePtr) {
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
if (dynamic) {
|
2005-03-18 15:44:23 +00:00
|
|
|
char *result;
|
2005-03-14 17:03:34 +00:00
|
|
|
|
|
|
|
VarDestroy(v, TRUE);
|
|
|
|
result = emalloc(used + 1);
|
|
|
|
strncpy(result, input, used);
|
|
|
|
result[used] = '\0';
|
|
|
|
|
|
|
|
*freePtr = TRUE;
|
|
|
|
return (result);
|
|
|
|
} else {
|
|
|
|
VarDestroy(v, TRUE);
|
|
|
|
|
|
|
|
*freePtr = FALSE;
|
2005-03-18 15:34:07 +00:00
|
|
|
return (vp->err ? var_Error : varNoError);
|
2005-03-14 17:03:34 +00:00
|
|
|
}
|
2005-03-01 12:26:32 +00:00
|
|
|
} else {
|
2005-03-14 17:03:34 +00:00
|
|
|
return (value);
|
2005-03-01 12:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-26 08:54:40 +00:00
|
|
|
static char *
|
2005-03-18 15:34:07 +00:00
|
|
|
ParseRestModifier(const char input[], const char ptr[], char startc, char endc, Buffer *buf, VarParser *vp, size_t *lengthPtr, Boolean *freePtr)
|
2005-02-26 08:54:40 +00:00
|
|
|
{
|
2005-03-03 07:25:04 +00:00
|
|
|
const char *vname;
|
2005-03-14 12:03:30 +00:00
|
|
|
size_t vlen;
|
|
|
|
Var *v;
|
2005-03-03 07:25:04 +00:00
|
|
|
Boolean dynamic; /* TRUE if the variable is local and we're
|
|
|
|
* expanding it in a non-local context. This
|
|
|
|
* is done to support dynamic sources. The
|
|
|
|
* result is just the invocation, unaltered */
|
|
|
|
|
2005-03-14 12:03:30 +00:00
|
|
|
vname = Buf_GetAll(buf, &vlen);
|
|
|
|
|
2005-03-03 07:25:04 +00:00
|
|
|
dynamic = FALSE;
|
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
v = VarFind(vname, vp->ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
|
2005-03-03 07:25:04 +00:00
|
|
|
if (v != NULL) {
|
2005-03-14 12:03:30 +00:00
|
|
|
return (ParseModifier(input, ptr,
|
|
|
|
startc, endc, dynamic, v,
|
2005-03-18 15:34:07 +00:00
|
|
|
vp, lengthPtr, freePtr));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
if ((vp->ctxt == VAR_CMD) || (vp->ctxt == VAR_GLOBAL)) {
|
2005-03-15 14:52:10 +00:00
|
|
|
if ((vlen == 1) ||
|
2005-03-03 07:25:04 +00:00
|
|
|
((vlen == 2) && (vname[1] == 'F' || vname[1] == 'D'))) {
|
|
|
|
/*
|
|
|
|
* If substituting a local variable in a non-local
|
|
|
|
* context, assume it's for dynamic source stuff. We
|
|
|
|
* have to handle this specially and return the
|
|
|
|
* longhand for the variable with the dollar sign
|
|
|
|
* escaped so it makes it back to the caller. Only
|
|
|
|
* four of the local variables are treated specially
|
|
|
|
* as they are the only four that will be set when
|
|
|
|
* dynamic sources are expanded.
|
|
|
|
*/
|
|
|
|
if (strchr("!%*@", vname[0]) != NULL) {
|
|
|
|
dynamic = TRUE;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-03-03 07:25:04 +00:00
|
|
|
if ((vlen > 2) &&
|
|
|
|
(vname[0] == '.') &&
|
|
|
|
isupper((unsigned char)vname[1])) {
|
|
|
|
if ((strncmp(vname, ".TARGET", vlen - 1) == 0) ||
|
|
|
|
(strncmp(vname, ".ARCHIVE", vlen - 1) == 0) ||
|
|
|
|
(strncmp(vname, ".PREFIX", vlen - 1) == 0) ||
|
|
|
|
(strncmp(vname, ".MEMBER", vlen - 1) == 0)) {
|
|
|
|
dynamic = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2005-02-11 10:49:01 +00:00
|
|
|
/*
|
2005-03-03 07:25:04 +00:00
|
|
|
* Still need to get to the end of the variable
|
|
|
|
* specification, so kludge up a Var structure for
|
|
|
|
* the modifications
|
2005-02-11 10:49:01 +00:00
|
|
|
*/
|
2005-02-26 08:54:40 +00:00
|
|
|
v = VarCreate(vname, NULL, VAR_JUNK);
|
2005-03-14 12:03:30 +00:00
|
|
|
return (ParseModifier(input, ptr,
|
2005-03-18 15:44:23 +00:00
|
|
|
startc, endc, dynamic, v,
|
|
|
|
vp, lengthPtr, freePtr));
|
2005-03-14 12:03:30 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Check for D and F forms of local variables since we're in
|
|
|
|
* a local context and the name is the right length.
|
|
|
|
*/
|
|
|
|
if ((vlen == 2) &&
|
|
|
|
(vname[1] == 'F' || vname[1] == 'D') &&
|
|
|
|
(strchr("!%*<>@", vname[0]) != NULL)) {
|
|
|
|
char name[2];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Well, it's local -- go look for it.
|
|
|
|
*/
|
|
|
|
name[0] = vname[0];
|
|
|
|
name[1] = '\0';
|
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
v = VarFind(name, vp->ctxt, 0);
|
2005-03-14 12:03:30 +00:00
|
|
|
if (v != NULL) {
|
|
|
|
return (ParseModifier(input, ptr,
|
|
|
|
startc, endc, dynamic, v,
|
2005-03-18 15:34:07 +00:00
|
|
|
vp, lengthPtr, freePtr));
|
2005-03-14 12:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
2005-02-26 08:54:40 +00:00
|
|
|
|
2005-03-14 12:03:30 +00:00
|
|
|
/*
|
|
|
|
* Still need to get to the end of the variable
|
|
|
|
* specification, so kludge up a Var structure for
|
|
|
|
* the modifications
|
|
|
|
*/
|
|
|
|
v = VarCreate(vname, NULL, VAR_JUNK);
|
|
|
|
return (ParseModifier(input, ptr,
|
2005-03-18 15:44:23 +00:00
|
|
|
startc, endc, dynamic, v,
|
|
|
|
vp, lengthPtr, freePtr));
|
2005-03-14 12:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2005-03-15 14:27:29 +00:00
|
|
|
ParseRestEnd(const char input[], Buffer *buf,
|
2005-03-18 15:34:07 +00:00
|
|
|
VarParser *vp, size_t *consumed, Boolean *freePtr)
|
2005-03-14 12:03:30 +00:00
|
|
|
{
|
|
|
|
const char *vname;
|
|
|
|
size_t vlen;
|
|
|
|
Var *v;
|
2005-03-15 14:52:10 +00:00
|
|
|
char *value;
|
2005-03-14 12:03:30 +00:00
|
|
|
|
|
|
|
vname = Buf_GetAll(buf, &vlen);
|
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
v = VarFind(vname, vp->ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
|
2005-03-14 12:03:30 +00:00
|
|
|
if (v != NULL) {
|
2005-03-18 15:34:07 +00:00
|
|
|
value = VarExpand(v, vp);
|
2005-03-14 12:03:30 +00:00
|
|
|
|
|
|
|
if (v->flags & VAR_FROM_ENV) {
|
|
|
|
VarDestroy(v, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
*freePtr = TRUE;
|
2005-03-15 14:52:10 +00:00
|
|
|
return (value);
|
2005-03-14 12:03:30 +00:00
|
|
|
}
|
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
if ((vp->ctxt == VAR_CMD) || (vp->ctxt == VAR_GLOBAL)) {
|
2005-03-14 12:03:30 +00:00
|
|
|
/*
|
|
|
|
* If substituting a local variable in a non-local
|
|
|
|
* context, assume it's for dynamic source stuff. We
|
|
|
|
* have to handle this specially and return the
|
|
|
|
* longhand for the variable with the dollar sign
|
|
|
|
* escaped so it makes it back to the caller. Only
|
|
|
|
* four of the local variables are treated specially
|
|
|
|
* as they are the only four that will be set when
|
|
|
|
* dynamic sources are expanded.
|
|
|
|
*/
|
|
|
|
if (((vlen == 1)) ||
|
|
|
|
((vlen == 2) && (vname[1] == 'F' || vname[1] == 'D'))) {
|
|
|
|
if (strchr("!%*@", vname[0]) != NULL) {
|
2005-03-15 14:52:10 +00:00
|
|
|
value = emalloc(*consumed + 1);
|
|
|
|
strncpy(value, input, *consumed);
|
|
|
|
value[*consumed] = '\0';
|
2005-03-14 12:03:30 +00:00
|
|
|
|
|
|
|
*freePtr = TRUE;
|
2005-03-15 14:52:10 +00:00
|
|
|
return (value);
|
2005-03-14 12:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((vlen > 2) &&
|
|
|
|
(vname[0] == '.') &&
|
|
|
|
isupper((unsigned char)vname[1])) {
|
|
|
|
if ((strncmp(vname, ".TARGET", vlen - 1) == 0) ||
|
|
|
|
(strncmp(vname, ".ARCHIVE", vlen - 1) == 0) ||
|
|
|
|
(strncmp(vname, ".PREFIX", vlen - 1) == 0) ||
|
|
|
|
(strncmp(vname, ".MEMBER", vlen - 1) == 0)) {
|
2005-03-15 14:52:10 +00:00
|
|
|
value = emalloc(*consumed + 1);
|
|
|
|
strncpy(value, input, *consumed);
|
|
|
|
value[*consumed] = '\0';
|
2005-03-14 12:03:30 +00:00
|
|
|
|
|
|
|
*freePtr = TRUE;
|
2005-03-15 14:52:10 +00:00
|
|
|
return (value);
|
2005-03-14 12:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*freePtr = FALSE;
|
2005-03-18 15:34:07 +00:00
|
|
|
return (vp->err ? var_Error : varNoError);
|
2005-03-03 07:25:04 +00:00
|
|
|
} else {
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2005-03-14 12:03:30 +00:00
|
|
|
* Check for D and F forms of local variables since we're in
|
|
|
|
* a local context and the name is the right length.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2005-03-14 12:03:30 +00:00
|
|
|
if ((vlen == 2) &&
|
|
|
|
(vname[1] == 'F' || vname[1] == 'D') &&
|
|
|
|
(strchr("!%*<>@", vname[0]) != NULL)) {
|
|
|
|
char name[2];
|
|
|
|
|
|
|
|
name[0] = vname[0];
|
|
|
|
name[1] = '\0';
|
2005-02-24 16:05:45 +00:00
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
v = VarFind(name, vp->ctxt, 0);
|
2005-03-14 12:03:30 +00:00
|
|
|
if (v != NULL) {
|
|
|
|
char *val;
|
|
|
|
/*
|
|
|
|
* No need for nested expansion or
|
|
|
|
* anything, as we're the only one
|
|
|
|
* who sets these things and we sure
|
|
|
|
* don't put nested invocations in
|
|
|
|
* them...
|
|
|
|
*/
|
|
|
|
val = (char *)Buf_GetAll(v->val, NULL);
|
2005-02-24 16:05:45 +00:00
|
|
|
|
2005-03-14 12:03:30 +00:00
|
|
|
if (vname[1] == 'D') {
|
|
|
|
val = VarModify(val, VarHead, NULL);
|
|
|
|
} else {
|
|
|
|
val = VarModify(val, VarTail, NULL);
|
|
|
|
}
|
2005-02-24 16:05:45 +00:00
|
|
|
|
2005-03-14 12:03:30 +00:00
|
|
|
*freePtr = TRUE;
|
|
|
|
return (val);
|
|
|
|
}
|
|
|
|
}
|
2005-03-01 12:22:07 +00:00
|
|
|
|
2005-03-14 12:03:30 +00:00
|
|
|
*freePtr = FALSE;
|
2005-03-18 15:34:07 +00:00
|
|
|
return (vp->err ? var_Error : varNoError);
|
2005-03-14 12:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a multi letter variable name, and return it's value.
|
|
|
|
*/
|
|
|
|
static char *
|
2005-03-18 15:34:07 +00:00
|
|
|
VarParseLong(const char input[], VarParser *vp,
|
2005-03-14 12:03:30 +00:00
|
|
|
size_t *consumed, Boolean *freePtr)
|
|
|
|
{
|
|
|
|
Buffer *buf;
|
2005-03-15 14:27:29 +00:00
|
|
|
char startc;
|
|
|
|
char endc;
|
2005-03-14 12:03:30 +00:00
|
|
|
const char *ptr;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
buf = Buf_Init(MAKE_BSIZE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process characters until we reach an end character or a
|
|
|
|
* colon, replacing embedded variables as we go.
|
|
|
|
*/
|
|
|
|
startc = input[0];
|
|
|
|
endc = (startc == OPEN_PAREN) ? CLOSE_PAREN : CLOSE_BRACE;
|
|
|
|
|
2005-03-15 14:27:29 +00:00
|
|
|
*consumed += 1; /* consume opening paren or brace */
|
2005-03-14 17:03:34 +00:00
|
|
|
input++;
|
|
|
|
|
|
|
|
ptr = input;
|
2005-03-15 14:27:29 +00:00
|
|
|
while (*ptr != endc) {
|
2005-03-14 12:03:30 +00:00
|
|
|
if (*ptr == '\0') {
|
|
|
|
/*
|
|
|
|
* If we did not find the end character,
|
|
|
|
* return var_Error right now, setting the
|
|
|
|
* length to be the distance to the end of
|
|
|
|
* the string, since that's what make does.
|
|
|
|
*/
|
2005-03-03 07:25:04 +00:00
|
|
|
Buf_Destroy(buf, TRUE);
|
2005-03-14 17:03:34 +00:00
|
|
|
*freePtr = FALSE;
|
2005-03-14 12:03:30 +00:00
|
|
|
return (var_Error);
|
|
|
|
|
2005-03-15 14:27:29 +00:00
|
|
|
} else if (*ptr == ':') {
|
|
|
|
result = ParseRestModifier(input - 2, ptr,
|
2005-03-15 14:52:10 +00:00
|
|
|
startc, endc, buf,
|
2005-03-18 15:34:07 +00:00
|
|
|
vp, consumed, freePtr);
|
2005-03-15 14:27:29 +00:00
|
|
|
Buf_Destroy(buf, TRUE);
|
|
|
|
return (result);
|
|
|
|
|
2005-03-14 12:03:30 +00:00
|
|
|
} else if (*ptr == '$') {
|
|
|
|
size_t rlen;
|
|
|
|
Boolean rfree;
|
|
|
|
char *rval;
|
|
|
|
|
|
|
|
rlen = 0;
|
2005-03-18 15:34:07 +00:00
|
|
|
rval = Var_Parse(ptr, vp->ctxt, vp->err, &rlen, &rfree);
|
2005-03-14 12:03:30 +00:00
|
|
|
if (rval == var_Error) {
|
|
|
|
Fatal("Error expanding embedded variable.");
|
|
|
|
}
|
|
|
|
Buf_Append(buf, rval);
|
|
|
|
if (rfree)
|
|
|
|
free(rval);
|
2005-03-14 17:03:34 +00:00
|
|
|
*consumed += rlen;
|
|
|
|
ptr += rlen;
|
2005-03-15 14:27:29 +00:00
|
|
|
|
2005-03-14 12:03:30 +00:00
|
|
|
} else {
|
|
|
|
Buf_AddByte(buf, (Byte)*ptr);
|
2005-03-14 17:03:34 +00:00
|
|
|
*consumed += 1;
|
|
|
|
ptr++;
|
2005-03-01 12:22:07 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-03-14 12:03:30 +00:00
|
|
|
|
2005-03-15 14:27:29 +00:00
|
|
|
*consumed += 1; /* consume closing paren or brace */
|
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
result = ParseRestEnd(input - 2, buf, vp, consumed, freePtr);
|
2005-03-14 12:03:30 +00:00
|
|
|
|
|
|
|
Buf_Destroy(buf, TRUE);
|
|
|
|
return (result);
|
2005-02-26 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
2005-03-01 07:58:18 +00:00
|
|
|
/**
|
2005-03-14 12:03:30 +00:00
|
|
|
* Parse a single letter variable name, and return it's value.
|
2005-03-01 07:58:18 +00:00
|
|
|
*/
|
2005-02-26 08:54:40 +00:00
|
|
|
static char *
|
2005-03-18 15:34:07 +00:00
|
|
|
VarParseShort(const char input[], VarParser *vp,
|
2005-03-15 14:52:10 +00:00
|
|
|
size_t *consumed, Boolean *freeResult)
|
2005-02-26 08:54:40 +00:00
|
|
|
{
|
2005-03-15 14:52:10 +00:00
|
|
|
char vname[2];
|
2005-03-01 07:58:18 +00:00
|
|
|
Var *v;
|
2005-03-15 14:52:10 +00:00
|
|
|
char *value;
|
2005-02-26 08:54:40 +00:00
|
|
|
|
2005-03-15 14:52:10 +00:00
|
|
|
vname[0] = input[0];
|
|
|
|
vname[1] = '\0';
|
2005-02-26 08:54:40 +00:00
|
|
|
|
2005-03-15 14:52:10 +00:00
|
|
|
*consumed += 1; /* consume single letter */
|
2005-03-02 14:30:36 +00:00
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
v = VarFind(vname, vp->ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
|
2005-03-01 07:58:18 +00:00
|
|
|
if (v != NULL) {
|
2005-03-18 15:34:07 +00:00
|
|
|
value = VarExpand(v, vp);
|
2005-02-26 08:54:40 +00:00
|
|
|
|
|
|
|
if (v->flags & VAR_FROM_ENV) {
|
|
|
|
VarDestroy(v, TRUE);
|
|
|
|
}
|
|
|
|
|
2005-03-15 14:52:10 +00:00
|
|
|
*freeResult = TRUE;
|
|
|
|
return (value);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-03-01 07:58:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If substituting a local variable in a non-local context, assume
|
|
|
|
* it's for dynamic source stuff. We have to handle this specially
|
|
|
|
* and return the longhand for the variable with the dollar sign
|
|
|
|
* escaped so it makes it back to the caller. Only four of the local
|
|
|
|
* variables are treated specially as they are the only four that
|
|
|
|
* will be set when dynamic sources are expanded.
|
|
|
|
*/
|
2005-03-18 15:34:07 +00:00
|
|
|
if ((vp->ctxt == VAR_CMD) || (vp->ctxt == VAR_GLOBAL)) {
|
2005-03-01 07:58:18 +00:00
|
|
|
|
|
|
|
/* XXX: It looks like $% and $! are reversed here */
|
2005-03-15 14:52:10 +00:00
|
|
|
switch (vname[0]) {
|
2005-03-01 07:58:18 +00:00
|
|
|
case '@':
|
2005-03-15 14:52:10 +00:00
|
|
|
*freeResult = TRUE;
|
2005-03-02 14:30:36 +00:00
|
|
|
return (estrdup("$(.TARGET)"));
|
2005-03-01 07:58:18 +00:00
|
|
|
case '%':
|
2005-03-15 14:52:10 +00:00
|
|
|
*freeResult = TRUE;
|
2005-03-02 14:30:36 +00:00
|
|
|
return (estrdup("$(.ARCHIVE)"));
|
2005-03-01 07:58:18 +00:00
|
|
|
case '*':
|
2005-03-15 14:52:10 +00:00
|
|
|
*freeResult = TRUE;
|
2005-03-02 14:30:36 +00:00
|
|
|
return (estrdup("$(.PREFIX)"));
|
2005-03-01 07:58:18 +00:00
|
|
|
case '!':
|
2005-03-15 14:52:10 +00:00
|
|
|
*freeResult = TRUE;
|
2005-03-02 14:30:36 +00:00
|
|
|
return (estrdup("$(.MEMBER)"));
|
2005-03-01 07:58:18 +00:00
|
|
|
default:
|
2005-03-15 14:52:10 +00:00
|
|
|
*freeResult = FALSE;
|
2005-03-18 15:34:07 +00:00
|
|
|
return (vp->err ? var_Error : varNoError);
|
2005-03-01 07:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-14 12:03:30 +00:00
|
|
|
/*
|
|
|
|
* Variable name was not found.
|
|
|
|
*/
|
2005-03-15 14:52:10 +00:00
|
|
|
*freeResult = FALSE;
|
2005-03-18 15:34:07 +00:00
|
|
|
return (vp->err ? var_Error : varNoError);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
VarParse(const char input[], VarParser *vp, size_t *consumed, Boolean *freeResult)
|
|
|
|
{
|
|
|
|
/* assert(input[0] == '$'); */
|
|
|
|
|
|
|
|
*consumed += 1; /* consume '$' */
|
|
|
|
input += 1;
|
|
|
|
|
|
|
|
if (input[0] == '\0') {
|
|
|
|
/* Error, there is only a dollar sign in the input string. */
|
|
|
|
*freeResult = FALSE;
|
|
|
|
return (vp->err ? var_Error : varNoError);
|
|
|
|
|
|
|
|
} else if (input[0] == OPEN_PAREN || input[0] == OPEN_BRACE) {
|
|
|
|
/* multi letter variable name */
|
|
|
|
return (VarParseLong(input, vp, consumed, freeResult));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* single letter variable name */
|
|
|
|
return (VarParseShort(input, vp, consumed, freeResult));
|
|
|
|
}
|
2005-02-26 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Var_Parse --
|
|
|
|
* Given the start of a variable invocation, extract the variable
|
|
|
|
* name and find its value, then modify it according to the
|
|
|
|
* specification.
|
|
|
|
*
|
|
|
|
* Results:
|
2005-03-08 07:43:57 +00:00
|
|
|
* The value of the variable or var_Error if the specification
|
2005-03-14 12:03:30 +00:00
|
|
|
* is invalid. The number of characters in the specification
|
|
|
|
* is placed in the variable pointed to by consumed. (for
|
|
|
|
* invalid specifications, this is just 2 to skip the '$' and
|
|
|
|
* the following letter, or 1 if '$' was the last character
|
2005-03-15 14:52:10 +00:00
|
|
|
* in the string). A Boolean in *freeResult telling whether the
|
2005-03-14 12:03:30 +00:00
|
|
|
* returned string should be freed by the caller.
|
2005-02-26 08:54:40 +00:00
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Assumption:
|
2005-03-08 07:43:57 +00:00
|
|
|
* It is assumed that Var_Parse() is called with input[0] == '$'.
|
2005-02-26 08:54:40 +00:00
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
char *
|
2005-03-08 07:43:57 +00:00
|
|
|
Var_Parse(const char input[], GNode *ctxt, Boolean err,
|
2005-03-15 14:52:10 +00:00
|
|
|
size_t *consumed, Boolean *freeResult)
|
2005-02-26 08:54:40 +00:00
|
|
|
{
|
2005-03-18 15:34:07 +00:00
|
|
|
VarParser vp = {
|
|
|
|
input,
|
|
|
|
ctxt,
|
|
|
|
err
|
|
|
|
};
|
2005-03-02 14:30:36 +00:00
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
return VarParse(input, &vp, consumed, freeResult);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Var_Subst --
|
|
|
|
* Substitute for all variables in the given string in the given context
|
|
|
|
* If undefErr is TRUE, Parse_Error will be called when an undefined
|
|
|
|
* variable is encountered.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* The resulting string.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None. The old string must be freed by the caller
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
2005-02-25 13:16:56 +00:00
|
|
|
Buffer *
|
2005-03-08 07:43:57 +00:00
|
|
|
Var_Subst(const char *var, const char *str, GNode *ctxt, Boolean undefErr)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-03-18 15:44:23 +00:00
|
|
|
Boolean errorReported;
|
|
|
|
Buffer *buf; /* Buffer for forming things */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set TRUE if an error has already been reported to prevent a
|
|
|
|
* plethora of messages when recursing. XXXHB this comment sounds
|
|
|
|
* wrong.
|
|
|
|
*/
|
|
|
|
errorReported = FALSE;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
buf = Buf_Init(0);
|
|
|
|
while (*str) {
|
|
|
|
if (var == NULL && (str[0] == '$') && (str[1] == '$')) {
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2005-03-18 15:44:23 +00:00
|
|
|
* A dollar sign may be escaped either with another
|
|
|
|
* dollar sign. In such a case, we skip over the
|
|
|
|
* escape character and store the dollar sign into
|
|
|
|
* the buffer directly.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2005-03-18 15:44:23 +00:00
|
|
|
Buf_AddByte(buf, (Byte)str[0]);
|
|
|
|
str += 2;
|
|
|
|
|
|
|
|
} else if (str[0] == '$') {
|
|
|
|
char *val; /* Value to substitute for a variable */
|
|
|
|
size_t length; /* Length of the variable invocation */
|
|
|
|
Boolean doFree; /* Set true if val should be freed */
|
2005-02-10 14:39:05 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2005-03-18 15:44:23 +00:00
|
|
|
* Variable invocation.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2005-03-18 15:44:23 +00:00
|
|
|
if (var != NULL) {
|
|
|
|
int expand;
|
|
|
|
for (;;) {
|
|
|
|
if (str[1] == OPEN_PAREN || str[1] == OPEN_BRACE) {
|
|
|
|
size_t ln;
|
|
|
|
const char *p = str + 2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan up to the end of the
|
|
|
|
* variable name.
|
|
|
|
*/
|
|
|
|
while (*p != '\0' &&
|
|
|
|
*p != ':' &&
|
|
|
|
*p != CLOSE_PAREN &&
|
|
|
|
*p != CLOSE_BRACE &&
|
|
|
|
*p != '$') {
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A variable inside the
|
|
|
|
* variable. We cannot expand
|
|
|
|
* the external variable yet,
|
|
|
|
* so we try again with the
|
|
|
|
* nested one
|
|
|
|
*/
|
|
|
|
if (*p == '$') {
|
|
|
|
Buf_AppendRange(buf, str, p);
|
|
|
|
str = p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ln = p - (str + 2);
|
|
|
|
if (var[ln] == '\0' && strncmp(var, str + 2, ln) == 0) {
|
|
|
|
expand = TRUE;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Not the variable
|
|
|
|
* we want to expand,
|
|
|
|
* scan until the
|
|
|
|
* next variable
|
|
|
|
*/
|
|
|
|
while (*p != '$' && *p != '\0')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
Buf_AppendRange(buf, str, p);
|
|
|
|
str = p;
|
|
|
|
expand = FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Single letter variable
|
|
|
|
* name
|
|
|
|
*/
|
|
|
|
if (var[1] == '\0' && var[0] == str[1]) {
|
|
|
|
expand = TRUE;
|
|
|
|
} else {
|
|
|
|
Buf_AddBytes(buf, 2, (const Byte *) str);
|
|
|
|
str += 2;
|
|
|
|
expand = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!expand)
|
|
|
|
continue;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-03-18 15:44:23 +00:00
|
|
|
length = 0;
|
|
|
|
val = Var_Parse(str, ctxt, undefErr, &length, &doFree);
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2005-02-11 10:49:01 +00:00
|
|
|
/*
|
2005-03-18 15:44:23 +00:00
|
|
|
* When we come down here, val should either point to
|
|
|
|
* the value of this variable, suitably modified, or
|
|
|
|
* be NULL. Length should be the total length of the
|
|
|
|
* potential variable invocation (from $ to end
|
|
|
|
* character...)
|
2005-02-11 10:49:01 +00:00
|
|
|
*/
|
2005-03-18 15:44:23 +00:00
|
|
|
if (val == var_Error || val == varNoError) {
|
|
|
|
/*
|
|
|
|
* 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) {
|
|
|
|
str += length;
|
|
|
|
} else if (undefErr) {
|
|
|
|
/*
|
|
|
|
* 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\"", length, str);
|
|
|
|
}
|
|
|
|
str += length;
|
|
|
|
errorReported = TRUE;
|
|
|
|
} else {
|
|
|
|
Buf_AddByte(buf, (Byte)*str);
|
|
|
|
str += 1;
|
|
|
|
}
|
2005-02-11 10:49:01 +00:00
|
|
|
} else {
|
2005-03-18 15:44:23 +00:00
|
|
|
/*
|
|
|
|
* We've now got a variable structure to
|
|
|
|
* store in. But first, advance the string
|
|
|
|
* pointer.
|
|
|
|
*/
|
|
|
|
str += length;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy all the characters from the variable
|
|
|
|
* value straight into the new string.
|
|
|
|
*/
|
|
|
|
Buf_Append(buf, val);
|
|
|
|
if (doFree) {
|
|
|
|
free(val);
|
|
|
|
}
|
2005-02-10 17:00:16 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
} else {
|
2005-03-18 15:44:23 +00:00
|
|
|
/*
|
|
|
|
* Skip as many characters as possible -- either to
|
|
|
|
* the end of the string or to the next dollar sign
|
|
|
|
* (variable invocation).
|
|
|
|
*/
|
|
|
|
const char *cp = str;
|
2005-02-11 10:49:01 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
do {
|
|
|
|
str++;
|
|
|
|
} while (str[0] != '$' && str[0] != '\0');
|
2005-02-25 13:16:56 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
Buf_AppendRange(buf, cp, str);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
return (buf);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Var_GetTail --
|
|
|
|
* Return the tail from each of a list of words. Used to set the
|
|
|
|
* System V local variables.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* The resulting string.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
char *
|
2002-10-09 03:42:10 +00:00
|
|
|
Var_GetTail(char *file)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
return (VarModify(file, VarTail, (void *)NULL));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Var_GetHead --
|
|
|
|
* Find the leading components of a (list of) filename(s).
|
|
|
|
* XXX: VarHead does not replace foo by ., as (sun) System V make
|
|
|
|
* does.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* The leading components.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
char *
|
2002-10-09 03:42:10 +00:00
|
|
|
Var_GetHead(char *file)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
return (VarModify(file, VarHead, (void *)NULL));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Var_Init --
|
|
|
|
* Initialize the module
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Side Effects:
|
1995-05-30 06:41:30 +00:00
|
|
|
* The VAR_CMD and VAR_GLOBAL contexts are created
|
1994-05-27 12:33:43 +00:00
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2004-11-30 17:46:29 +00:00
|
|
|
Var_Init(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
VAR_GLOBAL = Targ_NewGN("Global");
|
|
|
|
VAR_CMD = Targ_NewGN("Command");
|
1995-01-23 21:03:17 +00:00
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Var_Dump --
|
|
|
|
* print all variables in a context
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2005-03-10 15:38:01 +00:00
|
|
|
Var_Dump(const GNode *ctxt)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-03-10 15:38:01 +00:00
|
|
|
const LstNode *ln;
|
|
|
|
const Var *v;
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-03-10 15:38:01 +00:00
|
|
|
LST_FOREACH(ln, &ctxt->context) {
|
|
|
|
v = Lst_Datum(ln);
|
|
|
|
printf("%-16s = %s\n", v->name,
|
|
|
|
(const char *)Buf_GetAll(v->val, NULL));
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|