2005-02-01 10:50:37 +00:00
|
|
|
/*-
|
2005-05-10 11:53:20 +00:00
|
|
|
* Copyright (c) 2002 Juli Mallett.
|
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
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
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-02-01 10:50:37 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-05-10 11:53:20 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <regex.h>
|
2005-02-01 10:50:37 +00:00
|
|
|
|
|
|
|
#include "buf.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "globals.h"
|
|
|
|
#include "GNode.h"
|
2005-05-10 13:18:58 +00:00
|
|
|
#include "job.h"
|
2005-05-12 14:31:42 +00:00
|
|
|
#include "lst.h"
|
2005-02-01 10:50:37 +00:00
|
|
|
#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 */
|
2005-03-21 07:40:33 +00:00
|
|
|
const char *ptr; /* current parser pos in input str */
|
2005-03-18 15:34:07 +00:00
|
|
|
GNode *ctxt;
|
|
|
|
Boolean err;
|
2005-05-10 08:06:13 +00:00
|
|
|
Boolean execute;
|
2005-03-18 15:34:07 +00:00
|
|
|
} VarParser;
|
2005-05-10 11:53:20 +00:00
|
|
|
|
|
|
|
typedef struct Var {
|
|
|
|
char *name; /* the variable's name */
|
|
|
|
struct 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_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_TO_ENV 8 /* Place variable in environment */
|
|
|
|
} Var;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
struct Buffer *lhs; /* String to match */
|
|
|
|
struct Buffer *rhs; /* Replacement string (w/ &'s removed) */
|
|
|
|
|
|
|
|
regex_t re;
|
|
|
|
int nsub;
|
|
|
|
regmatch_t *matches;
|
|
|
|
|
|
|
|
int 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 */
|
|
|
|
} VarPattern;
|
|
|
|
|
|
|
|
typedef Boolean VarModifyProc(const char *, Boolean, struct Buffer *, void *);
|
|
|
|
|
2005-03-21 12:07:14 +00:00
|
|
|
static char *VarParse(VarParser *, Boolean *);
|
2005-03-18 15:34:07 +00:00
|
|
|
|
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-05-12 11:58:39 +00:00
|
|
|
static GNode *VAR_ENV; /* variables from the environment */
|
|
|
|
GNode *VAR_GLOBAL; /* variables from the makefile */
|
|
|
|
GNode *VAR_CMD; /* variables defined on the command-line */
|
|
|
|
|
|
|
|
Boolean oldVars; /* variable substitution style */
|
|
|
|
Boolean checkEnvFirst; /* -e flag */
|
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-05-10 11:55:46 +00:00
|
|
|
/**
|
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);
|
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
2005-02-09 09:53:28 +00:00
|
|
|
* Destroy a Var object.
|
|
|
|
*
|
|
|
|
* Params:
|
2005-05-10 08:14:26 +00:00
|
|
|
* v Object to destroy.
|
|
|
|
* f True if internal buffer in Buffer object is to be removed.
|
2005-02-09 09:53:28 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
VarDestroy(Var *v, Boolean f)
|
|
|
|
{
|
|
|
|
|
|
|
|
Buf_Destroy(v->val, f);
|
|
|
|
free(v->name);
|
|
|
|
free(v);
|
|
|
|
}
|
|
|
|
|
2005-05-10 11:53:20 +00:00
|
|
|
/**
|
2005-05-10 11:55:46 +00:00
|
|
|
* Remove the tail of the given word and place the result in the given
|
|
|
|
* buffer.
|
2005-05-10 11:53:20 +00:00
|
|
|
*
|
|
|
|
* 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(const char *word, Boolean addSpace, Buffer *buf, void *dummy __unused)
|
|
|
|
{
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
slash = strrchr(word, '/');
|
|
|
|
if (slash != NULL) {
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
}
|
|
|
|
Buf_AppendRange(buf, word, slash);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If no directory part, give . (q.v. the POSIX standard)
|
|
|
|
*/
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_Append(buf, " .");
|
|
|
|
} else {
|
|
|
|
Buf_AddByte(buf, (Byte)'.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-05-10 11:55:46 +00:00
|
|
|
* Remove the head of the given word and place the result in the given
|
|
|
|
* buffer.
|
2005-05-10 11:53:20 +00:00
|
|
|
*
|
|
|
|
* 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(const char *word, Boolean addSpace, Buffer *buf, void *dummy __unused)
|
|
|
|
{
|
|
|
|
const char *slash;
|
|
|
|
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_AddByte (buf, (Byte)' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
slash = strrchr(word, '/');
|
|
|
|
if (slash != NULL) {
|
|
|
|
slash++;
|
|
|
|
Buf_Append(buf, slash);
|
|
|
|
} else {
|
|
|
|
Buf_Append(buf, word);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-05-10 11:55:46 +00:00
|
|
|
* Place the suffix of the given word in the given buffer.
|
2005-05-10 11:53:20 +00:00
|
|
|
*
|
|
|
|
* 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(const char *word, Boolean addSpace, Buffer *buf, void *dummy __unused)
|
|
|
|
{
|
|
|
|
const char *dot;
|
|
|
|
|
|
|
|
dot = strrchr(word, '.');
|
|
|
|
if (dot != NULL) {
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
}
|
|
|
|
dot++;
|
|
|
|
Buf_Append(buf, dot);
|
|
|
|
addSpace = TRUE;
|
|
|
|
}
|
|
|
|
return (addSpace);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-05-10 11:55:46 +00:00
|
|
|
* Remove the suffix of the given word and place the result in the
|
|
|
|
* buffer.
|
2005-05-10 11:53:20 +00:00
|
|
|
*
|
|
|
|
* 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(const char *word, Boolean addSpace, Buffer *buf, void *dummy __unused)
|
|
|
|
{
|
|
|
|
char *dot;
|
|
|
|
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
dot = strrchr(word, '.');
|
|
|
|
if (dot != NULL) {
|
|
|
|
Buf_AppendRange(buf, word, dot);
|
|
|
|
} else {
|
|
|
|
Buf_Append(buf, word);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-05-10 11:55:46 +00:00
|
|
|
* Place the word in the buffer if it matches the given pattern.
|
|
|
|
* Callback function for VarModify to implement the :M modifier.
|
|
|
|
* A space will be added if requested. A pattern is supplied
|
|
|
|
* which the word must match.
|
2005-05-10 11:53:20 +00:00
|
|
|
*
|
|
|
|
* 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(const char *word, Boolean addSpace, Buffer *buf, void *pattern)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (Str_Match(word, pattern)) {
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
}
|
|
|
|
addSpace = TRUE;
|
|
|
|
Buf_Append(buf, word);
|
|
|
|
}
|
|
|
|
return (addSpace);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SYSVVARSUB
|
|
|
|
/**
|
2005-05-10 11:55:46 +00:00
|
|
|
* Place the word in the buffer if it matches the given pattern.
|
|
|
|
* Callback function for VarModify to implement the System V %
|
|
|
|
* modifiers. A space is added if requested.
|
2005-05-10 11:53:20 +00:00
|
|
|
*
|
|
|
|
* 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(const char *word, Boolean addSpace, Buffer *buf, void *patp)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
const char *ptr;
|
|
|
|
VarPattern *pat = (VarPattern *)patp;
|
|
|
|
|
|
|
|
if (addSpace)
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
|
|
|
|
addSpace = TRUE;
|
|
|
|
|
|
|
|
if ((ptr = Str_SYSVMatch(word, Buf_Data(pat->lhs), &len)) != NULL)
|
|
|
|
Str_SYSVSubst(buf, Buf_Data(pat->rhs), ptr, len);
|
|
|
|
else
|
|
|
|
Buf_Append(buf, word);
|
|
|
|
|
|
|
|
return (addSpace);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2005-05-10 11:55:46 +00:00
|
|
|
* Place the word in the buffer if it doesn't match the given pattern.
|
|
|
|
* Callback function for VarModify to implement the :N modifier. A
|
|
|
|
* space is added if requested.
|
2005-05-10 11:53:20 +00:00
|
|
|
*
|
|
|
|
* 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(const char *word, Boolean addSpace, Buffer *buf, void *pattern)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!Str_Match(word, pattern)) {
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
}
|
|
|
|
addSpace = TRUE;
|
|
|
|
Buf_Append(buf, word);
|
|
|
|
}
|
|
|
|
return (addSpace);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-05-10 11:55:46 +00:00
|
|
|
* Perform a string-substitution on the given word, placing the
|
|
|
|
* result in the passed buffer. A space is added if requested.
|
2005-05-10 11:53:20 +00:00
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* TRUE if a space is needed before more characters are added.
|
|
|
|
*/
|
|
|
|
static Boolean
|
|
|
|
VarSubstitute(const char *word, Boolean addSpace, Buffer *buf, void *patternp)
|
|
|
|
{
|
|
|
|
size_t wordLen; /* Length of word */
|
|
|
|
const char *cp; /* General pointer */
|
|
|
|
VarPattern *pattern = patternp;
|
|
|
|
|
|
|
|
wordLen = strlen(word);
|
|
|
|
if (1) { /* substitute in each word of the variable */
|
|
|
|
/*
|
|
|
|
* Break substitution down into simple anchored cases
|
|
|
|
* and if none of them fits, perform the general substitution
|
|
|
|
* case.
|
|
|
|
*/
|
|
|
|
if ((pattern->flags & VAR_MATCH_START) &&
|
|
|
|
(strncmp(word, Buf_Data(pattern->lhs),
|
|
|
|
Buf_Size(pattern->lhs)) == 0)) {
|
|
|
|
/*
|
|
|
|
* Anchored at start and beginning of word matches
|
|
|
|
* pattern.
|
|
|
|
*/
|
|
|
|
if ((pattern->flags & VAR_MATCH_END) &&
|
|
|
|
(wordLen == Buf_Size(pattern->lhs))) {
|
|
|
|
/*
|
|
|
|
* 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 (Buf_Size(pattern->rhs) != 0) {
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
}
|
|
|
|
addSpace = TRUE;
|
|
|
|
Buf_AppendBuf(buf, pattern->rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
} 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 ((Buf_Size(pattern->rhs) + wordLen -
|
|
|
|
Buf_Size(pattern->lhs)) != 0) {
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
}
|
|
|
|
addSpace = TRUE;
|
|
|
|
}
|
|
|
|
Buf_AppendBuf(buf, pattern->rhs);
|
|
|
|
Buf_AddBytes(buf, wordLen -
|
|
|
|
Buf_Size(pattern->lhs),
|
|
|
|
(word + Buf_Size(pattern->lhs)));
|
|
|
|
}
|
|
|
|
|
|
|
|
} 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 - Buf_Size(pattern->lhs));
|
|
|
|
if ((cp >= word) && (strncmp(cp, Buf_Data(pattern->lhs),
|
|
|
|
Buf_Size(pattern->lhs)) == 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) + Buf_Size(pattern->rhs) != 0) {
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
}
|
|
|
|
addSpace = TRUE;
|
|
|
|
}
|
|
|
|
Buf_AppendRange(buf, word, cp);
|
|
|
|
Buf_AppendBuf(buf, pattern->rhs);
|
|
|
|
|
|
|
|
} 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 strstr(3), 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;
|
|
|
|
size_t origSize;
|
|
|
|
|
|
|
|
done = FALSE;
|
|
|
|
origSize = Buf_Size(buf);
|
|
|
|
while (!done) {
|
|
|
|
cp = strstr(word, Buf_Data(pattern->lhs));
|
|
|
|
if (cp != NULL) {
|
|
|
|
if (addSpace && (((cp - word) +
|
|
|
|
Buf_Size(pattern->rhs)) != 0)) {
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
addSpace = FALSE;
|
|
|
|
}
|
|
|
|
Buf_AppendRange(buf, word, cp);
|
|
|
|
Buf_AppendBuf(buf, pattern->rhs);
|
|
|
|
wordLen -= (cp - word) +
|
|
|
|
Buf_Size(pattern->lhs);
|
|
|
|
word = cp + Buf_Size(pattern->lhs);
|
|
|
|
if (wordLen == 0 || (pattern->flags &
|
|
|
|
VAR_SUB_GLOBAL) == 0) {
|
|
|
|
done = TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
done = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wordLen != 0) {
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
}
|
|
|
|
Buf_AddBytes(buf, wordLen, (const Byte *)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);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Common code for anchored substitutions:
|
|
|
|
* addSpace was set TRUE if characters were added to the buffer.
|
|
|
|
*/
|
|
|
|
return (addSpace);
|
|
|
|
}
|
|
|
|
nosub:
|
|
|
|
if (addSpace) {
|
|
|
|
Buf_AddByte(buf, (Byte)' ');
|
|
|
|
}
|
|
|
|
Buf_AddBytes(buf, wordLen, (const Byte *)word);
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print the error caused by a regcomp or regexec call.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* An error gets printed.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
VarREError(int err, regex_t *pat, const char *str)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2005-05-10 11:55:46 +00:00
|
|
|
* Perform a regex substitution on the given word, placing the
|
|
|
|
* result in the passed buffer. A space is added if requested.
|
2005-05-10 11:53:20 +00:00
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* TRUE if a space is needed before more characters are added.
|
|
|
|
*/
|
|
|
|
static Boolean
|
|
|
|
VarRESubstitute(const char *word, Boolean addSpace, Buffer *buf, void *patternp)
|
|
|
|
{
|
|
|
|
VarPattern *pat;
|
|
|
|
int xrv;
|
|
|
|
const char *wp;
|
|
|
|
char *rp;
|
|
|
|
int added;
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
#define MAYBE_ADD_SPACE() \
|
|
|
|
if (addSpace && !added) \
|
|
|
|
Buf_AddByte(buf, (Byte)' '); \
|
|
|
|
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,
|
|
|
|
(const Byte *)wp);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (rp = Buf_Data(pat->rhs); *rp; rp++) {
|
|
|
|
if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) {
|
|
|
|
MAYBE_ADD_SPACE();
|
|
|
|
Buf_AddByte(buf, (Byte)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,
|
|
|
|
(const Byte *)subbuf);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MAYBE_ADD_SPACE();
|
|
|
|
Buf_AddByte(buf, (Byte)*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, (Byte)*wp);
|
|
|
|
wp++;
|
|
|
|
}
|
|
|
|
if (*wp)
|
|
|
|
goto tryagain;
|
|
|
|
}
|
|
|
|
if (*wp) {
|
|
|
|
MAYBE_ADD_SPACE();
|
|
|
|
Buf_Append(buf, wp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
VarREError(xrv, &pat->re, "Unexpected regex error");
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case REG_NOMATCH:
|
|
|
|
if (*wp) {
|
|
|
|
MAYBE_ADD_SPACE();
|
|
|
|
Buf_Append(buf, wp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (addSpace || added);
|
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
2005-03-22 12:38:55 +00:00
|
|
|
* Find a variable in a variable list.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2005-03-22 12:38:55 +00:00
|
|
|
static Var *
|
|
|
|
VarLookup(Lst *vlist, const char *name)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-03-22 12:38:55 +00:00
|
|
|
LstNode *ln;
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-03-22 12:38:55 +00:00
|
|
|
LST_FOREACH(ln, vlist)
|
|
|
|
if (strcmp(((const Var *)Lst_Datum(ln))->name, name) == 0)
|
|
|
|
return (Lst_Datum(ln));
|
|
|
|
return (NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* Expand a variable name's embedded variables in the given context.
|
2002-06-19 17:23:08 +00:00
|
|
|
*
|
|
|
|
* 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;
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-02-04 12:10:20 +00:00
|
|
|
if (strchr(name, '$') != NULL) {
|
2005-05-09 14:06:04 +00:00
|
|
|
buf = Var_Subst(name, ctxt, 0);
|
2005-03-22 07:50:40 +00:00
|
|
|
return (Buf_Peel(buf));
|
2005-02-25 13:16:56 +00:00
|
|
|
} else {
|
2005-03-21 12:32:41 +00:00
|
|
|
return estrdup(name);
|
2005-02-25 13:16:56 +00:00
|
|
|
}
|
2002-06-19 17:23:08 +00:00
|
|
|
}
|
|
|
|
|
2005-04-13 11:40:44 +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.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2005-05-10 11:55:46 +00:00
|
|
|
static const char *
|
2005-04-13 11:40:44 +00:00
|
|
|
VarLocal(const char name[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
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"))
|
2005-04-13 11:40:44 +00:00
|
|
|
return (ALLSRC);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!strcmp(name, ".ARCHIVE"))
|
2005-04-13 11:40:44 +00:00
|
|
|
return (ARCHIVE);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
if (!strcmp(name, ".IMPSRC"))
|
2005-04-13 11:40:44 +00:00
|
|
|
return (IMPSRC);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
if (!strcmp(name, ".MEMBER"))
|
2005-04-13 11:40:44 +00:00
|
|
|
return (MEMBER);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
if (!strcmp(name, ".OODATE"))
|
2005-04-13 11:40:44 +00:00
|
|
|
return (OODATE);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
if (!strcmp(name, ".PREFIX"))
|
2005-04-13 11:40:44 +00:00
|
|
|
return (PREFIX);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
if (!strcmp(name, ".TARGET"))
|
2005-04-13 11:40:44 +00:00
|
|
|
return (TARGET);
|
1994-05-27 12:33:43 +00:00
|
|
|
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
|
|
|
}
|
2005-04-13 11:40:44 +00:00
|
|
|
return (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the given variable in the given context and the enviornment.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* A pointer to the structure describing the desired variable or
|
|
|
|
* NULL if the variable does not exist.
|
|
|
|
*/
|
|
|
|
static Var *
|
|
|
|
VarFindEnv(const char name[], GNode *ctxt)
|
|
|
|
{
|
|
|
|
Var *var;
|
|
|
|
|
|
|
|
name = VarLocal(name);
|
|
|
|
|
|
|
|
if ((var = VarLookup(&ctxt->context, name)) != NULL)
|
|
|
|
return (var);
|
|
|
|
|
2005-05-06 18:30:06 +00:00
|
|
|
if ((var = VarLookup(&VAR_ENV->context, name)) != NULL)
|
|
|
|
return (var);
|
2005-04-13 11:40:44 +00:00
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look for the variable in the given context.
|
|
|
|
*/
|
|
|
|
static Var *
|
|
|
|
VarFindOnly(const char name[], GNode *ctxt)
|
|
|
|
{
|
|
|
|
Var *var;
|
|
|
|
|
|
|
|
name = VarLocal(name);
|
|
|
|
|
|
|
|
if ((var = VarLookup(&ctxt->context, name)) != NULL)
|
|
|
|
return (var);
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look for the variable in all contexts.
|
|
|
|
*/
|
|
|
|
static Var *
|
|
|
|
VarFindAny(const char name[], GNode *ctxt)
|
|
|
|
{
|
|
|
|
Boolean localCheckEnvFirst;
|
|
|
|
LstNode *ln;
|
|
|
|
Var *var;
|
|
|
|
|
|
|
|
name = VarLocal(name);
|
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.
|
|
|
|
*/
|
2005-03-22 12:38:55 +00:00
|
|
|
localCheckEnvFirst = FALSE;
|
|
|
|
LST_FOREACH(ln, &envFirstVars) {
|
|
|
|
if (strcmp(Lst_Datum(ln), name) == 0) {
|
|
|
|
localCheckEnvFirst = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2005-02-09 13:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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'
|
|
|
|
*/
|
2005-04-13 11:40:44 +00:00
|
|
|
if ((var = VarLookup(&ctxt->context, name)) != NULL)
|
2005-03-22 12:38:55 +00:00
|
|
|
return (var);
|
1999-07-31 20:53:02 +00:00
|
|
|
|
2005-02-09 13:13:59 +00:00
|
|
|
/* not there - try command line context */
|
2005-04-13 11:40:44 +00:00
|
|
|
if (ctxt != VAR_CMD) {
|
|
|
|
if ((var = VarLookup(&VAR_CMD->context, name)) != NULL)
|
2005-03-22 12:38:55 +00:00
|
|
|
return (var);
|
2005-02-09 13:13:59 +00:00
|
|
|
}
|
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 */
|
2005-04-13 11:40:44 +00:00
|
|
|
if (ctxt != VAR_GLOBAL && (!checkEnvFirst && !localCheckEnvFirst)) {
|
|
|
|
if ((var = VarLookup(&VAR_GLOBAL->context, name)) != NULL)
|
2005-03-22 12:38:55 +00:00
|
|
|
return (var);
|
2005-02-09 13:13:59 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-05-06 18:30:06 +00:00
|
|
|
if ((var = VarLookup(&VAR_ENV->context, name)) != NULL)
|
|
|
|
return (var);
|
2005-02-09 13:13:59 +00:00
|
|
|
|
|
|
|
/* deferred check for the environment (in case of -e/-E) */
|
2005-04-13 11:40:44 +00:00
|
|
|
if ((ctxt != VAR_GLOBAL) && (checkEnvFirst || localCheckEnvFirst)) {
|
|
|
|
if ((var = VarLookup(&VAR_GLOBAL->context, name)) != NULL)
|
2005-03-22 12:38:55 +00:00
|
|
|
return (var);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-04-13 11:40:44 +00:00
|
|
|
|
2004-12-01 10:29:20 +00:00
|
|
|
return (NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2005-05-06 18:30:06 +00:00
|
|
|
/**
|
|
|
|
* Add a new variable of name name and value val to the given context.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-01-03 10:14:01 +00:00
|
|
|
static Var *
|
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
|
|
|
{
|
2009-01-03 10:14:01 +00:00
|
|
|
Var *v;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2009-01-03 10:14:01 +00:00
|
|
|
Lst_AtFront(&ctxt->context, v = VarCreate(name, val, 0));
|
2005-03-18 15:44:23 +00:00
|
|
|
DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, name, val));
|
2009-01-03 10:14:01 +00:00
|
|
|
return (v);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* Remove a variable from a context.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* 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));
|
2005-03-22 12:38:55 +00:00
|
|
|
LST_FOREACH(ln, &ctxt->context) {
|
|
|
|
if (strcmp(((const Var *)Lst_Datum(ln))->name, name) == 0) {
|
|
|
|
VarDestroy(Lst_Datum(ln), TRUE);
|
|
|
|
Lst_Remove(&ctxt->context, ln);
|
|
|
|
break;
|
|
|
|
}
|
2005-03-18 15:44:23 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* Set the variable name to the value val in the given context.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* 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);
|
2005-04-13 11:40:44 +00:00
|
|
|
v = VarFindOnly(n, ctxt);
|
2005-03-18 15:44:23 +00:00
|
|
|
if (v == NULL) {
|
2009-01-03 10:14:01 +00:00
|
|
|
v = VarAdd(n, val, ctxt);
|
2005-03-18 15:44:23 +00:00
|
|
|
} else {
|
|
|
|
Buf_Clear(v->val);
|
|
|
|
Buf_Append(v->val, val);
|
2006-07-17 21:05:27 +00:00
|
|
|
DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, n, val));
|
2005-03-18 15:44:23 +00:00
|
|
|
}
|
2005-05-06 18:30:06 +00:00
|
|
|
|
2009-01-03 10:14:01 +00:00
|
|
|
if (ctxt == VAR_CMD || (v->flags & VAR_TO_ENV)) {
|
|
|
|
/*
|
|
|
|
* Any variables given on the command line
|
|
|
|
* are automatically exported to the
|
|
|
|
* environment (as per POSIX standard)
|
|
|
|
*/
|
|
|
|
setenv(n, val, 1);
|
|
|
|
}
|
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
free(n);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2005-05-12 14:47:22 +00:00
|
|
|
/**
|
|
|
|
* Set the a global name variable to the value.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Var_SetGlobal(const char name[], const char value[])
|
|
|
|
{
|
|
|
|
|
|
|
|
Var_Set(name, value, VAR_GLOBAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* Set the VAR_TO_ENV flag on a variable
|
2005-05-06 18:30:06 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
Var_SetEnv(const char *name, GNode *ctxt)
|
|
|
|
{
|
|
|
|
Var *v;
|
|
|
|
|
|
|
|
v = VarFindOnly(name, VAR_CMD);
|
|
|
|
if (v != NULL) {
|
|
|
|
/*
|
|
|
|
* Do not allow .EXPORT: to be set on variables
|
|
|
|
* from the comand line or MAKEFLAGS.
|
|
|
|
*/
|
|
|
|
Error(
|
|
|
|
"Warning: Did not set .EXPORTVAR: on %s because it "
|
|
|
|
"is from the comand line or MAKEFLAGS", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v = VarFindAny(name, ctxt);
|
|
|
|
if (v == NULL) {
|
|
|
|
Lst_AtFront(&VAR_ENV->context,
|
|
|
|
VarCreate(name, NULL, VAR_TO_ENV));
|
|
|
|
setenv(name, "", 1);
|
|
|
|
Error("Warning: .EXPORTVAR: set on undefined variable %s", name);
|
|
|
|
} else {
|
|
|
|
if ((v->flags & VAR_TO_ENV) == 0) {
|
|
|
|
v->flags |= VAR_TO_ENV;
|
|
|
|
setenv(v->name, Buf_Data(v->val), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* The variable of the given name has the given value appended to it in
|
|
|
|
* the given context.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* 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);
|
2005-04-13 11:40:44 +00:00
|
|
|
if (ctxt == VAR_GLOBAL) {
|
|
|
|
v = VarFindEnv(n, ctxt);
|
|
|
|
} else {
|
|
|
|
v = VarFindOnly(n, ctxt);
|
|
|
|
}
|
2005-03-18 15:44:23 +00:00
|
|
|
if (v == NULL) {
|
|
|
|
VarAdd(n, val, ctxt);
|
2005-03-22 08:16:09 +00:00
|
|
|
} else {
|
|
|
|
Buf_AddByte(v->val, (Byte)' ');
|
|
|
|
Buf_Append(v->val, val);
|
|
|
|
DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, n, Buf_Data(v->val)));
|
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
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* See if the given variable exists.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* TRUE if it does, FALSE if it doesn't
|
|
|
|
*/
|
|
|
|
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);
|
2005-04-13 11:40:44 +00:00
|
|
|
v = VarFindAny(n, ctxt);
|
2005-03-18 15:44:23 +00:00
|
|
|
if (v == NULL) {
|
2005-03-22 08:16:09 +00:00
|
|
|
free(n);
|
2005-03-18 15:44:23 +00:00
|
|
|
return (FALSE);
|
2005-03-22 08:16:09 +00:00
|
|
|
} else {
|
|
|
|
free(n);
|
|
|
|
return (TRUE);
|
2005-03-18 15:44:23 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* Return the value of the named variable in the given context
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* Results:
|
2005-05-24 15:58:35 +00:00
|
|
|
* The value if the variable exists, NULL if it doesn't.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2005-05-24 16:05:51 +00:00
|
|
|
const char *
|
2005-05-24 15:58:35 +00:00
|
|
|
Var_Value(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;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
n = VarPossiblyExpand(name, ctxt);
|
2005-04-13 11:40:44 +00:00
|
|
|
v = VarFindAny(n, ctxt);
|
2005-05-24 15:58:35 +00:00
|
|
|
free(n);
|
2005-03-22 08:16:09 +00:00
|
|
|
if (v == NULL) {
|
2005-05-24 15:58:35 +00:00
|
|
|
return (NULL);
|
2005-03-18 15:44:23 +00:00
|
|
|
} else {
|
2005-05-24 15:58:35 +00:00
|
|
|
return (Buf_Data(v->val));
|
1995-01-23 21:03:17 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* Modify each of the words of the passed string using the given
|
|
|
|
* function. Used to implement all modifiers.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* 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-05-18 14:50:35 +00:00
|
|
|
ArgArray aa;
|
|
|
|
Buffer *buf; /* Buffer for the new string */
|
|
|
|
int i;
|
|
|
|
Boolean addSpace; /*
|
|
|
|
* TRUE if need to add a space to
|
|
|
|
* the buffer before adding the
|
|
|
|
* trimmed word
|
|
|
|
*/
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2005-05-18 14:50:35 +00:00
|
|
|
brk_string(&aa, str, FALSE);
|
2005-03-07 16:23:53 +00:00
|
|
|
|
|
|
|
addSpace = FALSE;
|
2005-05-18 14:50:35 +00:00
|
|
|
buf = Buf_Init(0);
|
|
|
|
for (i = 1; i < aa.argc; i++)
|
|
|
|
addSpace = (*modProc)(aa.argv[i], addSpace, buf, datum);
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2005-05-18 14:50:35 +00:00
|
|
|
ArgArray_Done(&aa);
|
2005-03-22 07:50:40 +00:00
|
|
|
return (Buf_Peel(buf));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* Sort the words in the string.
|
2003-09-18 03:15:57 +00:00
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* str String whose words should be sorted
|
|
|
|
* cmp A comparison function to control the ordering
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* A string containing the words sorted
|
|
|
|
*/
|
|
|
|
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-05-18 14:50:35 +00:00
|
|
|
ArgArray aa;
|
|
|
|
Buffer *buf;
|
|
|
|
int i;
|
2003-09-18 03:15:57 +00:00
|
|
|
|
2005-05-18 14:50:35 +00:00
|
|
|
brk_string(&aa, str, FALSE);
|
|
|
|
qsort(aa.argv + 1, aa.argc - 1, sizeof(char *), cmp);
|
2005-03-07 16:23:53 +00:00
|
|
|
|
|
|
|
buf = Buf_Init(0);
|
2005-05-18 14:50:35 +00:00
|
|
|
for (i = 1; i < aa.argc; i++) {
|
|
|
|
Buf_Append(buf, aa.argv[i]);
|
|
|
|
Buf_AddByte(buf, (Byte)((i < aa.argc - 1) ? ' ' : '\0'));
|
2003-09-18 03:15:57 +00:00
|
|
|
}
|
2005-03-07 16:23:53 +00:00
|
|
|
|
2005-05-18 14:50:35 +00:00
|
|
|
ArgArray_Done(&aa);
|
2005-03-22 07:50:40 +00:00
|
|
|
return (Buf_Peel(buf));
|
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));
|
|
|
|
}
|
|
|
|
|
2006-04-08 06:59:54 +00:00
|
|
|
/**
|
|
|
|
* Remove adjacent duplicate words.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* A string containing the resulting words.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
VarUniq(const char *str)
|
|
|
|
{
|
|
|
|
ArgArray aa;
|
|
|
|
Buffer *buf; /* Buffer for new string */
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
buf = Buf_Init(0);
|
|
|
|
brk_string(&aa, str, FALSE);
|
|
|
|
|
|
|
|
if (aa.argc > 2) {
|
|
|
|
for (j = 1, i = 2; i < aa.argc; i++) {
|
|
|
|
if (strcmp(aa.argv[i], aa.argv[j]) != 0 && (++j != i))
|
|
|
|
aa.argv[j] = aa.argv[i];
|
|
|
|
}
|
|
|
|
aa.argc = j + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < aa.argc; i++) {
|
|
|
|
Buf_AddBytes(buf, strlen(aa.argv[i]), (Byte *)aa.argv[i]);
|
|
|
|
if (i != aa.argc - 1)
|
|
|
|
Buf_AddByte(buf, ' ');
|
|
|
|
}
|
|
|
|
Buf_AddByte(buf, '\0');
|
|
|
|
|
|
|
|
ArgArray_Done(&aa);
|
|
|
|
return (Buf_Peel(buf));
|
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
* 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.
|
2000-10-09 04:31:43 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-03-22 08:09:43 +00:00
|
|
|
static Buffer *
|
|
|
|
VarGetPattern(VarParser *vp, int delim, int *flags, VarPattern *patt)
|
2000-10-09 04:31:43 +00:00
|
|
|
{
|
2005-03-22 07:38:46 +00:00
|
|
|
Buffer *buf;
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-03-22 07:38:46 +00:00
|
|
|
buf = Buf_Init(0);
|
2000-10-09 04:31:43 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2005-03-22 07:38:46 +00:00
|
|
|
while (*vp->ptr != '\0') {
|
|
|
|
if (*vp->ptr == delim) {
|
2005-03-22 08:09:43 +00:00
|
|
|
return (buf);
|
2005-03-22 07:38:46 +00:00
|
|
|
|
|
|
|
} else if ((vp->ptr[0] == '\\') &&
|
|
|
|
((vp->ptr[1] == delim) ||
|
|
|
|
(vp->ptr[1] == '\\') ||
|
|
|
|
(vp->ptr[1] == '$') ||
|
|
|
|
(vp->ptr[1] == '&' && patt != NULL))) {
|
|
|
|
vp->ptr++; /* consume backslash */
|
|
|
|
Buf_AddByte(buf, (Byte)vp->ptr[0]);
|
|
|
|
vp->ptr++;
|
|
|
|
|
|
|
|
} else if (vp->ptr[0] == '$') {
|
|
|
|
if (vp->ptr[1] == delim) {
|
|
|
|
if (flags == NULL) {
|
|
|
|
Buf_AddByte(buf, (Byte)vp->ptr[0]);
|
|
|
|
vp->ptr++;
|
|
|
|
} else {
|
2005-03-18 15:44:23 +00:00
|
|
|
/*
|
2005-03-22 07:38:46 +00:00
|
|
|
* Unescaped $ at end of patt =>
|
|
|
|
* anchor patt at end.
|
2005-03-18 15:44:23 +00:00
|
|
|
*/
|
|
|
|
*flags |= VAR_MATCH_END;
|
2005-03-22 07:38:46 +00:00
|
|
|
vp->ptr++;
|
|
|
|
}
|
2005-03-18 15:44:23 +00:00
|
|
|
} else {
|
2005-03-22 08:20:08 +00:00
|
|
|
VarParser subvp = {
|
|
|
|
vp->ptr,
|
|
|
|
vp->ptr,
|
|
|
|
vp->ctxt,
|
2005-05-10 08:06:13 +00:00
|
|
|
vp->err,
|
|
|
|
vp->execute
|
2005-03-22 08:20:08 +00:00
|
|
|
};
|
2005-03-22 08:18:15 +00:00
|
|
|
char *rval;
|
|
|
|
Boolean rfree;
|
2005-03-18 15:44:23 +00:00
|
|
|
|
2005-03-22 07:38:46 +00:00
|
|
|
/*
|
|
|
|
* If unescaped dollar sign not
|
|
|
|
* before the delimiter, assume it's
|
|
|
|
* a variable substitution and
|
|
|
|
* recurse.
|
|
|
|
*/
|
2005-03-22 08:20:08 +00:00
|
|
|
rval = VarParse(&subvp, &rfree);
|
2005-03-22 08:18:15 +00:00
|
|
|
Buf_Append(buf, rval);
|
|
|
|
if (rfree)
|
|
|
|
free(rval);
|
2005-03-22 08:20:08 +00:00
|
|
|
vp->ptr = subvp.ptr;
|
2000-10-09 04:31:43 +00:00
|
|
|
}
|
2005-03-22 07:38:46 +00:00
|
|
|
} else if (vp->ptr[0] == '&' && patt != NULL) {
|
2005-03-22 08:09:43 +00:00
|
|
|
Buf_AppendBuf(buf, patt->lhs);
|
2005-03-22 07:38:46 +00:00
|
|
|
vp->ptr++;
|
|
|
|
} else {
|
|
|
|
Buf_AddByte(buf, (Byte)vp->ptr[0]);
|
|
|
|
vp->ptr++;
|
|
|
|
}
|
2000-10-09 04:31:43 +00:00
|
|
|
}
|
|
|
|
|
2005-03-22 07:38:46 +00:00
|
|
|
Buf_Destroy(buf, TRUE);
|
|
|
|
return (NULL);
|
2000-10-09 04:31:43 +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-03-22 07:50:40 +00:00
|
|
|
value = Buf_Data(v->val);
|
2005-02-26 08:54:40 +00:00
|
|
|
if (strchr(value, '$') == NULL) {
|
|
|
|
result = strdup(value);
|
|
|
|
} else {
|
|
|
|
Buffer *buf;
|
|
|
|
|
2005-05-09 14:06:04 +00:00
|
|
|
buf = Var_Subst(value, vp->ctxt, vp->err);
|
2005-03-22 07:50:40 +00:00
|
|
|
result = Buf_Peel(buf);
|
2005-02-26 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 *
|
2005-03-21 12:07:14 +00:00
|
|
|
modifier_M(VarParser *vp, const char value[], char endc)
|
2005-03-07 12:43:59 +00:00
|
|
|
{
|
2005-03-21 12:07:14 +00:00
|
|
|
char *patt;
|
|
|
|
char *ptr;
|
|
|
|
char *newValue;
|
|
|
|
char modifier;
|
|
|
|
|
|
|
|
modifier = vp->ptr[0];
|
|
|
|
vp->ptr++; /* consume 'M' or 'N' */
|
2005-03-07 12:43:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-21 12:07:14 +00:00
|
|
|
patt = estrdup(vp->ptr);
|
2005-03-07 12:43:59 +00:00
|
|
|
ptr = patt;
|
2005-03-21 12:07:14 +00:00
|
|
|
while (vp->ptr[0] != '\0') {
|
2005-03-22 08:12:16 +00:00
|
|
|
if (vp->ptr[0] == endc || vp->ptr[0] == ':') {
|
2005-03-15 15:05:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-03-22 08:12:16 +00:00
|
|
|
if (vp->ptr[0] == '\\' &&
|
|
|
|
(vp->ptr[1] == endc || vp->ptr[1] == ':')) {
|
2005-03-21 12:32:41 +00:00
|
|
|
vp->ptr++; /* consume backslash */
|
2005-03-07 12:43:59 +00:00
|
|
|
}
|
2005-03-21 12:07:14 +00:00
|
|
|
*ptr = vp->ptr[0];
|
2005-03-07 12:43:59 +00:00
|
|
|
ptr++;
|
2005-03-21 12:07:14 +00:00
|
|
|
vp->ptr++;
|
2005-03-07 12:43:59 +00:00
|
|
|
}
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2005-03-21 12:07:14 +00:00
|
|
|
if (modifier == 'M') {
|
2005-03-07 12:43:59 +00:00
|
|
|
newValue = VarModify(value, VarMatch, patt);
|
|
|
|
} else {
|
|
|
|
newValue = VarModify(value, VarNoMatch, patt);
|
|
|
|
}
|
|
|
|
free(patt);
|
|
|
|
|
|
|
|
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-21 12:07:14 +00:00
|
|
|
modifier_S(VarParser *vp, const char value[], Var *v)
|
2005-03-07 16:08:39 +00:00
|
|
|
{
|
2005-03-22 07:38:46 +00:00
|
|
|
VarPattern patt;
|
2005-03-07 16:08:39 +00:00
|
|
|
char delim;
|
|
|
|
char *newValue;
|
|
|
|
|
2005-03-22 07:38:46 +00:00
|
|
|
patt.flags = 0;
|
2005-03-07 16:08:39 +00:00
|
|
|
|
2005-03-21 12:40:27 +00:00
|
|
|
vp->ptr++; /* consume 'S' */
|
|
|
|
|
|
|
|
delim = *vp->ptr; /* used to find end of pattern */
|
|
|
|
vp->ptr++; /* consume 1st delim */
|
2005-03-07 16:08:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If pattern begins with '^', it is anchored to the start of the
|
|
|
|
* word -- skip over it and flag pattern.
|
|
|
|
*/
|
2005-03-21 12:40:27 +00:00
|
|
|
if (*vp->ptr == '^') {
|
2005-03-22 07:38:46 +00:00
|
|
|
patt.flags |= VAR_MATCH_START;
|
2005-03-21 12:40:27 +00:00
|
|
|
vp->ptr++;
|
2005-03-07 16:08:39 +00:00
|
|
|
}
|
|
|
|
|
2005-03-22 08:09:43 +00:00
|
|
|
patt.lhs = VarGetPattern(vp, delim, &patt.flags, NULL);
|
2005-03-22 07:38:46 +00:00
|
|
|
if (patt.lhs == NULL) {
|
|
|
|
/*
|
|
|
|
* LHS didn't end with the delim, complain and exit.
|
|
|
|
*/
|
|
|
|
Fatal("Unclosed substitution for %s (%c missing)",
|
|
|
|
v->name, delim);
|
2005-03-07 16:08:39 +00:00
|
|
|
}
|
2005-03-21 12:40:27 +00:00
|
|
|
|
2005-03-22 07:38:46 +00:00
|
|
|
vp->ptr++; /* consume 2nd delim */
|
2005-03-07 16:08:39 +00:00
|
|
|
|
2005-03-22 08:09:43 +00:00
|
|
|
patt.rhs = VarGetPattern(vp, delim, NULL, &patt);
|
2005-03-22 07:38:46 +00:00
|
|
|
if (patt.rhs == NULL) {
|
|
|
|
/*
|
|
|
|
* RHS didn't end with the delim, complain and exit.
|
|
|
|
*/
|
|
|
|
Fatal("Unclosed substitution for %s (%c missing)",
|
|
|
|
v->name, delim);
|
2005-03-07 16:08:39 +00:00
|
|
|
}
|
|
|
|
|
2005-03-22 07:38:46 +00:00
|
|
|
vp->ptr++; /* consume last delim */
|
2005-03-07 16:08:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for global substitution. If 'g' after the final delimiter,
|
|
|
|
* substitution is global and is marked that way.
|
|
|
|
*/
|
2005-03-21 12:40:27 +00:00
|
|
|
if (vp->ptr[0] == 'g') {
|
2005-03-22 07:38:46 +00:00
|
|
|
patt.flags |= VAR_SUB_GLOBAL;
|
2005-03-21 12:40:27 +00:00
|
|
|
vp->ptr++;
|
2005-03-07 16:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-03-22 08:09:43 +00:00
|
|
|
if (Buf_Size(patt.lhs) == 0 && patt.flags == VAR_SUB_GLOBAL)
|
2005-03-07 16:08:39 +00:00
|
|
|
Fatal("Global substitution of the empty string");
|
|
|
|
|
2005-03-22 07:38:46 +00:00
|
|
|
newValue = VarModify(value, VarSubstitute, &patt);
|
2005-03-07 16:08:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the two strings.
|
|
|
|
*/
|
2005-03-22 07:38:46 +00:00
|
|
|
free(patt.lhs);
|
|
|
|
free(patt.rhs);
|
2005-03-07 16:08:39 +00:00
|
|
|
|
|
|
|
return (newValue);
|
|
|
|
}
|
2005-03-07 12:43:59 +00:00
|
|
|
|
2005-03-18 15:37:31 +00:00
|
|
|
static char *
|
2005-03-21 12:07:14 +00:00
|
|
|
modifier_C(VarParser *vp, char value[], Var *v)
|
2005-03-18 15:37:31 +00:00
|
|
|
{
|
2005-03-22 08:03:42 +00:00
|
|
|
VarPattern patt;
|
2005-03-21 11:54:46 +00:00
|
|
|
char delim;
|
2005-03-18 15:37:31 +00:00
|
|
|
int error;
|
|
|
|
char *newValue;
|
|
|
|
|
|
|
|
patt.flags = 0;
|
|
|
|
|
2005-03-21 12:40:27 +00:00
|
|
|
vp->ptr++; /* consume 'C' */
|
|
|
|
|
|
|
|
delim = *vp->ptr; /* delimiter between sections */
|
2005-03-18 15:37:31 +00:00
|
|
|
|
2005-03-21 12:40:27 +00:00
|
|
|
vp->ptr++; /* consume 1st delim */
|
2005-03-18 15:37:31 +00:00
|
|
|
|
2005-03-22 08:09:43 +00:00
|
|
|
patt.lhs = VarGetPattern(vp, delim, NULL, NULL);
|
2005-03-22 08:06:34 +00:00
|
|
|
if (patt.lhs == NULL) {
|
2005-03-18 15:37:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-03-22 07:38:46 +00:00
|
|
|
vp->ptr++; /* consume 2st delim */
|
|
|
|
|
2005-03-22 08:09:43 +00:00
|
|
|
patt.rhs = VarGetPattern(vp, delim, NULL, NULL);
|
2005-03-22 08:03:42 +00:00
|
|
|
if (patt.rhs == NULL) {
|
2005-03-18 15:37:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-03-22 07:38:46 +00:00
|
|
|
vp->ptr++; /* consume last delim */
|
|
|
|
|
2005-03-21 12:40:27 +00:00
|
|
|
switch (*vp->ptr) {
|
|
|
|
case 'g':
|
|
|
|
patt.flags |= VAR_SUB_GLOBAL;
|
|
|
|
vp->ptr++; /* consume 'g' */
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
patt.flags |= VAR_SUB_ONE;
|
|
|
|
vp->ptr++; /* consume '1' */
|
|
|
|
break;
|
|
|
|
default:
|
2005-03-18 15:37:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-03-22 08:09:43 +00:00
|
|
|
error = regcomp(&patt.re, Buf_Data(patt.lhs), REG_EXTENDED);
|
2005-03-18 15:37:31 +00:00
|
|
|
if (error) {
|
|
|
|
VarREError(error, &patt.re, "RE substitution error");
|
2005-03-22 08:03:42 +00:00
|
|
|
free(patt.rhs);
|
2005-03-22 08:06:34 +00:00
|
|
|
free(patt.lhs);
|
2005-03-18 15:37:31 +00:00
|
|
|
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);
|
2005-03-22 08:03:42 +00:00
|
|
|
free(patt.rhs);
|
2005-03-22 08:06:34 +00:00
|
|
|
free(patt.lhs);
|
2005-03-18 15:37:31 +00:00
|
|
|
|
|
|
|
return (newValue);
|
|
|
|
}
|
|
|
|
|
2005-03-21 12:27:53 +00:00
|
|
|
static char *
|
|
|
|
sysVvarsub(VarParser *vp, char startc, Var *v, const char value[])
|
|
|
|
{
|
|
|
|
#ifdef SYSVVARSUB
|
|
|
|
/*
|
|
|
|
* This can either be a bogus modifier or a System-V substitution
|
|
|
|
* command.
|
|
|
|
*/
|
|
|
|
char endc;
|
|
|
|
VarPattern patt;
|
|
|
|
Boolean eqFound;
|
|
|
|
int cnt;
|
|
|
|
char *newStr;
|
|
|
|
const char *cp;
|
|
|
|
|
|
|
|
endc = (startc == OPEN_PAREN) ? CLOSE_PAREN : CLOSE_BRACE;
|
|
|
|
|
|
|
|
patt.flags = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-22 07:38:46 +00:00
|
|
|
eqFound = FALSE;
|
2005-03-21 12:27:53 +00:00
|
|
|
cp = vp->ptr;
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*cp == endc && eqFound) {
|
|
|
|
/*
|
|
|
|
* Now we break this sucker into the lhs and rhs.
|
|
|
|
*/
|
2005-03-22 08:09:43 +00:00
|
|
|
patt.lhs = VarGetPattern(vp, '=', &patt.flags, NULL);
|
2005-03-21 12:27:53 +00:00
|
|
|
if (patt.lhs == NULL) {
|
|
|
|
Fatal("Unclosed substitution for %s (%c missing)",
|
|
|
|
v->name, '=');
|
|
|
|
}
|
2005-03-22 07:38:46 +00:00
|
|
|
vp->ptr++; /* consume '=' */
|
|
|
|
|
2005-03-22 08:09:43 +00:00
|
|
|
patt.rhs = VarGetPattern(vp, endc, NULL, &patt);
|
2005-03-21 12:27:53 +00:00
|
|
|
if (patt.rhs == NULL) {
|
|
|
|
Fatal("Unclosed substitution for %s (%c missing)",
|
|
|
|
v->name, endc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SYSV modifications happen through the whole string. Note
|
|
|
|
* the pattern is anchored at the end.
|
|
|
|
*/
|
|
|
|
newStr = VarModify(value, VarSYSVMatch, &patt);
|
|
|
|
|
|
|
|
free(patt.lhs);
|
|
|
|
free(patt.rhs);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
Error("Unknown modifier '%c'\n", *vp->ptr);
|
|
|
|
vp->ptr++;
|
|
|
|
while (*vp->ptr != '\0') {
|
|
|
|
if (*vp->ptr == endc && *vp->ptr == ':') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
vp->ptr++;
|
|
|
|
}
|
|
|
|
newStr = var_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (newStr);
|
|
|
|
}
|
|
|
|
|
2005-05-10 11:53:20 +00:00
|
|
|
/**
|
|
|
|
* Quote shell meta-characters in the string
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* The quoted string
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
Var_Quote(const char *str)
|
|
|
|
{
|
|
|
|
Buffer *buf;
|
|
|
|
/* This should cover most shells :-( */
|
|
|
|
static char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
|
|
|
|
|
|
|
|
buf = Buf_Init(MAKE_BSIZE);
|
|
|
|
for (; *str; str++) {
|
|
|
|
if (strchr(meta, *str) != NULL)
|
|
|
|
Buf_AddByte(buf, (Byte)'\\');
|
|
|
|
Buf_AddByte(buf, (Byte)*str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Buf_Peel(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
2006-04-08 07:02:39 +00:00
|
|
|
* :N<pattern>
|
|
|
|
* words which do not match the given <pattern>
|
|
|
|
* <pattern> is of the standard file
|
|
|
|
* wildcarding form.
|
2005-03-01 12:26:32 +00:00
|
|
|
* :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.
|
2006-04-08 07:02:39 +00:00
|
|
|
* :O ("Order") Alphabeticaly sort words in variable.
|
2006-04-08 06:59:54 +00:00
|
|
|
* :u ("uniq") Remove adjacent duplicate words.
|
2005-03-01 12:26:32 +00:00
|
|
|
*/
|
|
|
|
static char *
|
2005-03-21 12:19:23 +00:00
|
|
|
ParseModifier(VarParser *vp, char startc, Var *v, Boolean *freeResult)
|
2005-03-01 12:26:32 +00:00
|
|
|
{
|
2005-03-21 12:07:14 +00:00
|
|
|
char *value;
|
|
|
|
char endc;
|
2005-03-01 12:26:32 +00:00
|
|
|
|
2005-03-18 15:34:07 +00:00
|
|
|
value = VarExpand(v, vp);
|
2005-03-21 12:19:23 +00:00
|
|
|
*freeResult = TRUE;
|
2005-03-01 12:26:32 +00:00
|
|
|
|
2005-03-21 12:07:14 +00:00
|
|
|
endc = (startc == OPEN_PAREN) ? CLOSE_PAREN : CLOSE_BRACE;
|
|
|
|
|
2005-03-21 12:27:53 +00:00
|
|
|
vp->ptr++; /* consume first colon */
|
|
|
|
|
|
|
|
while (*vp->ptr != '\0') {
|
2005-03-18 15:44:23 +00:00
|
|
|
char *newStr; /* New value to return */
|
2005-03-01 12:26:32 +00:00
|
|
|
|
2005-03-21 12:27:53 +00:00
|
|
|
if (*vp->ptr == endc) {
|
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
|
2005-03-21 12:07:14 +00:00
|
|
|
DEBUGF(VAR, ("Applying :%c to \"%s\"\n", *vp->ptr, value));
|
|
|
|
switch (*vp->ptr) {
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'N':
|
|
|
|
case 'M':
|
2005-03-21 12:07:14 +00:00
|
|
|
newStr = modifier_M(vp, value, endc);
|
2005-03-03 11:26:28 +00:00
|
|
|
break;
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'S':
|
2005-03-21 12:07:14 +00:00
|
|
|
newStr = modifier_S(vp, value, v);
|
2005-03-07 16:08:39 +00:00
|
|
|
break;
|
2005-03-01 12:26:32 +00:00
|
|
|
case 'C':
|
2005-03-21 12:07:14 +00:00
|
|
|
newStr = modifier_C(vp, value, v);
|
2005-03-01 12:26:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
2005-03-21 12:27:53 +00:00
|
|
|
if (vp->ptr[1] != endc && vp->ptr[1] != ':') {
|
2005-03-18 15:51:04 +00:00
|
|
|
#ifdef SUNSHCMD
|
2005-03-21 12:07:14 +00:00
|
|
|
if ((vp->ptr[0] == 's') &&
|
|
|
|
(vp->ptr[1] == 'h') &&
|
|
|
|
(vp->ptr[2] == endc || vp->ptr[2] == ':')) {
|
2005-03-18 16:52:46 +00:00
|
|
|
const char *error;
|
2005-03-18 15:51:04 +00:00
|
|
|
|
2005-05-10 08:06:13 +00:00
|
|
|
if (vp->execute) {
|
|
|
|
newStr = Buf_Peel(
|
|
|
|
Cmd_Exec(value, &error));
|
|
|
|
} else {
|
|
|
|
newStr = estrdup("");
|
|
|
|
}
|
2005-03-18 15:51:04 +00:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
Error(error, value);
|
2005-03-21 12:27:53 +00:00
|
|
|
vp->ptr += 2;
|
2005-03-18 15:51:04 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2005-03-21 12:27:53 +00:00
|
|
|
newStr = sysVvarsub(vp, startc, v, value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2005-03-18 15:51:04 +00:00
|
|
|
|
2005-03-21 12:27:53 +00:00
|
|
|
switch (vp->ptr[0]) {
|
|
|
|
case 'L':
|
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
Buffer *buf;
|
|
|
|
buf = Buf_Init(MAKE_BSIZE);
|
|
|
|
for (cp = value; *cp; cp++)
|
|
|
|
Buf_AddByte(buf, (Byte)tolower(*cp));
|
2005-03-18 15:51:04 +00:00
|
|
|
|
2005-03-22 07:50:40 +00:00
|
|
|
newStr = Buf_Peel(buf);
|
2005-03-01 12:26:32 +00:00
|
|
|
|
2005-03-21 12:27:53 +00:00
|
|
|
vp->ptr++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'O':
|
|
|
|
newStr = VarSortWords(value, SortIncreasing);
|
|
|
|
vp->ptr++;
|
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
newStr = Var_Quote(value);
|
|
|
|
vp->ptr++;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
newStr = VarModify(value, VarTail, NULL);
|
|
|
|
vp->ptr++;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
Buffer *buf;
|
|
|
|
buf = Buf_Init(MAKE_BSIZE);
|
|
|
|
for (cp = value; *cp; cp++)
|
|
|
|
Buf_AddByte(buf, (Byte)toupper(*cp));
|
2005-03-01 12:26:32 +00:00
|
|
|
|
2005-03-22 07:50:40 +00:00
|
|
|
newStr = Buf_Peel(buf);
|
2005-03-18 16:52:46 +00:00
|
|
|
|
2005-03-21 12:27:53 +00:00
|
|
|
vp->ptr++;
|
|
|
|
break;
|
2005-03-18 15:51:04 +00:00
|
|
|
}
|
2005-03-21 12:27:53 +00:00
|
|
|
case 'H':
|
|
|
|
newStr = VarModify(value, VarHead, NULL);
|
|
|
|
vp->ptr++;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
newStr = VarModify(value, VarSuffix, NULL);
|
|
|
|
vp->ptr++;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
newStr = VarModify(value, VarRoot, NULL);
|
|
|
|
vp->ptr++;
|
|
|
|
break;
|
2006-04-08 06:59:54 +00:00
|
|
|
case 'u':
|
|
|
|
newStr = VarUniq(value);
|
|
|
|
vp->ptr++;
|
|
|
|
break;
|
2005-03-21 12:27:53 +00:00
|
|
|
default:
|
|
|
|
newStr = sysVvarsub(vp, startc, v, value);
|
|
|
|
break;
|
2005-03-18 15:44:23 +00:00
|
|
|
}
|
2005-03-18 15:51:04 +00:00
|
|
|
break;
|
2005-03-01 12:26:32 +00:00
|
|
|
}
|
2005-03-18 15:44:23 +00:00
|
|
|
|
|
|
|
DEBUGF(VAR, ("Result is \"%s\"\n", newStr));
|
2005-03-21 12:19:23 +00:00
|
|
|
if (*freeResult) {
|
2005-03-18 15:44:23 +00:00
|
|
|
free(value);
|
|
|
|
}
|
2005-03-21 12:27:53 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
value = newStr;
|
2005-03-21 12:27:53 +00:00
|
|
|
*freeResult = (value == var_Error) ? FALSE : TRUE;
|
|
|
|
|
|
|
|
if (vp->ptr[0] == ':') {
|
|
|
|
vp->ptr++; /* consume colon */
|
2005-03-18 15:44:23 +00:00
|
|
|
}
|
2005-03-01 12:26:32 +00:00
|
|
|
}
|
|
|
|
|
2005-03-21 12:17:44 +00:00
|
|
|
return (value);
|
2005-03-01 12:26:32 +00:00
|
|
|
}
|
|
|
|
|
2005-02-26 08:54:40 +00:00
|
|
|
static char *
|
2005-03-21 12:19:23 +00:00
|
|
|
ParseRestModifier(VarParser *vp, char startc, Buffer *buf, Boolean *freeResult)
|
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-21 12:15:06 +00:00
|
|
|
char *value;
|
2005-03-03 07:25:04 +00:00
|
|
|
|
2005-03-14 12:03:30 +00:00
|
|
|
vname = Buf_GetAll(buf, &vlen);
|
|
|
|
|
2005-04-13 11:40:44 +00:00
|
|
|
v = VarFindAny(vname, vp->ctxt);
|
2005-03-03 07:25:04 +00:00
|
|
|
if (v != NULL) {
|
2005-03-21 12:19:23 +00:00
|
|
|
value = ParseModifier(vp, startc, v, freeResult);
|
2005-03-21 12:17:44 +00:00
|
|
|
return (value);
|
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-30 15:14:22 +00:00
|
|
|
size_t consumed;
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
value = ParseModifier(vp, startc, v, freeResult);
|
|
|
|
if (*freeResult) {
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
VarDestroy(v, TRUE);
|
2005-03-21 12:17:44 +00:00
|
|
|
|
2005-03-30 15:14:22 +00:00
|
|
|
consumed = vp->ptr - vp->input + 1;
|
2005-03-21 12:15:06 +00:00
|
|
|
/*
|
2005-03-21 12:17:44 +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-21 12:15:06 +00:00
|
|
|
*/
|
2005-03-22 08:12:16 +00:00
|
|
|
if (vlen == 1 ||
|
|
|
|
(vlen == 2 && (vname[1] == 'F' || vname[1] == 'D'))) {
|
2005-03-03 07:25:04 +00:00
|
|
|
if (strchr("!%*@", vname[0]) != NULL) {
|
2005-03-21 12:15:06 +00:00
|
|
|
value = emalloc(consumed + 1);
|
|
|
|
strncpy(value, vp->input, consumed);
|
|
|
|
value[consumed] = '\0';
|
|
|
|
|
2005-03-21 12:19:23 +00:00
|
|
|
*freeResult = TRUE;
|
2005-03-21 12:15:06 +00:00
|
|
|
return (value);
|
2005-03-03 07:25:04 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-03-22 08:12:16 +00:00
|
|
|
if (vlen > 2 &&
|
|
|
|
vname[0] == '.' &&
|
2005-03-03 07:25:04 +00:00
|
|
|
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-21 12:15:06 +00:00
|
|
|
value = emalloc(consumed + 1);
|
|
|
|
strncpy(value, vp->input, consumed);
|
|
|
|
value[consumed] = '\0';
|
|
|
|
|
2005-03-21 12:19:23 +00:00
|
|
|
*freeResult = TRUE;
|
2005-03-21 12:15:06 +00:00
|
|
|
return (value);
|
2005-03-03 07:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-30 15:14:22 +00:00
|
|
|
|
|
|
|
*freeResult = FALSE;
|
|
|
|
return (vp->err ? var_Error : varNoError);
|
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.
|
|
|
|
*/
|
2005-03-22 08:12:16 +00:00
|
|
|
if (vlen == 2 &&
|
2005-03-14 12:03:30 +00:00
|
|
|
(vname[1] == 'F' || vname[1] == 'D') &&
|
|
|
|
(strchr("!%*<>@", vname[0]) != NULL)) {
|
|
|
|
char name[2];
|
|
|
|
|
|
|
|
name[0] = vname[0];
|
|
|
|
name[1] = '\0';
|
|
|
|
|
2005-04-13 11:40:44 +00:00
|
|
|
v = VarFindOnly(name, vp->ctxt);
|
2005-03-14 12:03:30 +00:00
|
|
|
if (v != NULL) {
|
2005-03-21 12:19:23 +00:00
|
|
|
value = ParseModifier(vp, startc, v, freeResult);
|
2005-03-21 12:17:44 +00:00
|
|
|
return (value);
|
2005-03-14 12:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
2005-02-26 08:54:40 +00:00
|
|
|
|
2005-03-30 15:14:22 +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);
|
|
|
|
value = ParseModifier(vp, startc, v, freeResult);
|
|
|
|
if (*freeResult) {
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
VarDestroy(v, TRUE);
|
2005-03-21 12:32:41 +00:00
|
|
|
|
2005-03-30 15:14:22 +00:00
|
|
|
*freeResult = FALSE;
|
|
|
|
return (vp->err ? var_Error : varNoError);
|
|
|
|
}
|
2005-03-14 12:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2005-03-21 12:19:23 +00:00
|
|
|
ParseRestEnd(VarParser *vp, Buffer *buf, Boolean *freeResult)
|
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-04-13 11:40:44 +00:00
|
|
|
v = VarFindAny(vname, vp->ctxt);
|
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-21 12:19:23 +00:00
|
|
|
*freeResult = 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-22 08:12:16 +00:00
|
|
|
size_t consumed = vp->ptr - vp->input + 1;
|
2005-03-21 12:17:44 +00:00
|
|
|
|
2005-03-14 12:03:30 +00:00
|
|
|
/*
|
2005-03-21 12:17:44 +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
|
2005-03-14 12:03:30 +00:00
|
|
|
* dynamic sources are expanded.
|
|
|
|
*/
|
2005-03-22 08:12:16 +00:00
|
|
|
if (vlen == 1 ||
|
|
|
|
(vlen == 2 && (vname[1] == 'F' || vname[1] == 'D'))) {
|
2005-03-14 12:03:30 +00:00
|
|
|
if (strchr("!%*@", vname[0]) != NULL) {
|
2005-03-21 11:43:02 +00:00
|
|
|
value = emalloc(consumed + 1);
|
|
|
|
strncpy(value, vp->input, consumed);
|
|
|
|
value[consumed] = '\0';
|
2005-03-14 12:03:30 +00:00
|
|
|
|
2005-03-21 12:19:23 +00:00
|
|
|
*freeResult = TRUE;
|
2005-03-15 14:52:10 +00:00
|
|
|
return (value);
|
2005-03-14 12:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-22 08:12:16 +00:00
|
|
|
if (vlen > 2 &&
|
|
|
|
vname[0] == '.' &&
|
2005-03-14 12:03:30 +00:00
|
|
|
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-21 11:43:02 +00:00
|
|
|
value = emalloc(consumed + 1);
|
|
|
|
strncpy(value, vp->input, consumed);
|
|
|
|
value[consumed] = '\0';
|
2005-03-14 12:03:30 +00:00
|
|
|
|
2005-03-21 12:19:23 +00:00
|
|
|
*freeResult = TRUE;
|
2005-03-15 14:52:10 +00:00
|
|
|
return (value);
|
2005-03-14 12:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
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-22 08:12:16 +00:00
|
|
|
if (vlen == 2 &&
|
2005-03-14 12:03:30 +00:00
|
|
|
(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-04-13 11:40:44 +00:00
|
|
|
v = VarFindOnly(name, vp->ctxt);
|
2005-03-14 12:03:30 +00:00
|
|
|
if (v != NULL) {
|
|
|
|
char *val;
|
|
|
|
/*
|
2005-03-21 12:17:44 +00:00
|
|
|
* 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...
|
2005-03-14 12:03:30 +00:00
|
|
|
*/
|
2005-03-22 07:50:40 +00:00
|
|
|
val = Buf_Data(v->val);
|
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-21 12:19:23 +00:00
|
|
|
*freeResult = TRUE;
|
2005-03-14 12:03:30 +00:00
|
|
|
return (val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-03-21 12:32:41 +00:00
|
|
|
|
|
|
|
*freeResult = FALSE;
|
|
|
|
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-21 12:19:23 +00:00
|
|
|
VarParseLong(VarParser *vp, Boolean *freeResult)
|
2005-03-14 12:03:30 +00:00
|
|
|
{
|
|
|
|
Buffer *buf;
|
2005-03-15 14:27:29 +00:00
|
|
|
char startc;
|
|
|
|
char endc;
|
2005-03-21 12:19:23 +00:00
|
|
|
char *value;
|
2005-03-14 12:03:30 +00:00
|
|
|
|
|
|
|
buf = Buf_Init(MAKE_BSIZE);
|
|
|
|
|
2005-03-21 11:43:02 +00:00
|
|
|
startc = vp->ptr[0];
|
2005-03-21 12:32:41 +00:00
|
|
|
vp->ptr++; /* consume opening paren or brace */
|
2005-03-14 12:03:30 +00:00
|
|
|
|
2005-03-21 12:07:14 +00:00
|
|
|
endc = (startc == OPEN_PAREN) ? CLOSE_PAREN : CLOSE_BRACE;
|
2005-03-14 17:03:34 +00:00
|
|
|
|
2005-03-21 12:32:41 +00:00
|
|
|
/*
|
|
|
|
* Process characters until we reach an end character or a colon,
|
|
|
|
* replacing embedded variables as we go.
|
|
|
|
*/
|
2005-03-21 11:43:02 +00:00
|
|
|
while (*vp->ptr != '\0') {
|
|
|
|
if (*vp->ptr == endc) {
|
2005-03-21 12:19:23 +00:00
|
|
|
value = ParseRestEnd(vp, buf, freeResult);
|
2005-03-21 12:17:44 +00:00
|
|
|
vp->ptr++; /* consume closing paren or brace */
|
2005-03-03 07:25:04 +00:00
|
|
|
Buf_Destroy(buf, TRUE);
|
2005-03-21 12:19:23 +00:00
|
|
|
return (value);
|
2005-03-14 12:03:30 +00:00
|
|
|
|
2005-03-21 11:43:02 +00:00
|
|
|
} else if (*vp->ptr == ':') {
|
2005-03-21 12:19:23 +00:00
|
|
|
value = ParseRestModifier(vp, startc, buf, freeResult);
|
2005-03-21 12:07:14 +00:00
|
|
|
vp->ptr++; /* consume closing paren or brace */
|
2005-03-15 14:27:29 +00:00
|
|
|
Buf_Destroy(buf, TRUE);
|
2005-03-21 12:19:23 +00:00
|
|
|
return (value);
|
2005-03-15 14:27:29 +00:00
|
|
|
|
2005-03-21 11:43:02 +00:00
|
|
|
} else if (*vp->ptr == '$') {
|
2005-03-22 08:20:08 +00:00
|
|
|
VarParser subvp = {
|
|
|
|
vp->ptr,
|
|
|
|
vp->ptr,
|
|
|
|
vp->ctxt,
|
2005-05-10 08:06:13 +00:00
|
|
|
vp->err,
|
|
|
|
vp->execute
|
2005-03-22 08:20:08 +00:00
|
|
|
};
|
2005-03-14 12:03:30 +00:00
|
|
|
char *rval;
|
2005-03-22 08:20:08 +00:00
|
|
|
Boolean rfree;
|
2005-03-14 12:03:30 +00:00
|
|
|
|
2005-03-22 08:20:08 +00:00
|
|
|
rval = VarParse(&subvp, &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-22 08:20:08 +00:00
|
|
|
vp->ptr = subvp.ptr;
|
2005-03-14 12:03:30 +00:00
|
|
|
} else {
|
2005-03-21 11:43:02 +00:00
|
|
|
Buf_AddByte(buf, (Byte)*vp->ptr);
|
|
|
|
vp->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-21 12:17:44 +00:00
|
|
|
/* If we did not find the end character, return var_Error */
|
2005-03-14 12:03:30 +00:00
|
|
|
Buf_Destroy(buf, TRUE);
|
2005-03-21 12:19:23 +00:00
|
|
|
*freeResult = FALSE;
|
2005-03-21 11:43:02 +00:00
|
|
|
return (var_Error);
|
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-21 11:43:02 +00:00
|
|
|
VarParseShort(VarParser *vp, 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-21 11:43:02 +00:00
|
|
|
vname[0] = vp->ptr[0];
|
2005-03-15 14:52:10 +00:00
|
|
|
vname[1] = '\0';
|
2005-02-26 08:54:40 +00:00
|
|
|
|
2005-03-21 11:43:02 +00:00
|
|
|
vp->ptr++; /* consume single letter */
|
2005-03-02 14:30:36 +00:00
|
|
|
|
2005-04-13 11:40:44 +00:00
|
|
|
v = VarFindAny(vname, vp->ctxt);
|
2005-03-01 07:58:18 +00:00
|
|
|
if (v != NULL) {
|
2005-03-18 15:34:07 +00:00
|
|
|
value = VarExpand(v, vp);
|
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-21 12:27:53 +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 *
|
2005-03-21 12:07:14 +00:00
|
|
|
VarParse(VarParser *vp, Boolean *freeResult)
|
2005-03-18 15:34:07 +00:00
|
|
|
{
|
2005-03-21 07:40:33 +00:00
|
|
|
|
2005-03-21 12:32:41 +00:00
|
|
|
vp->ptr++; /* consume '$' or last letter of conditional */
|
2005-03-18 15:34:07 +00:00
|
|
|
|
2005-03-21 07:40:33 +00:00
|
|
|
if (vp->ptr[0] == '\0') {
|
2005-03-18 15:34:07 +00:00
|
|
|
/* Error, there is only a dollar sign in the input string. */
|
|
|
|
*freeResult = FALSE;
|
2005-03-21 12:07:14 +00:00
|
|
|
return (vp->err ? var_Error : varNoError);
|
2005-03-18 15:34:07 +00:00
|
|
|
|
2005-03-21 07:40:33 +00:00
|
|
|
} else if (vp->ptr[0] == OPEN_PAREN || vp->ptr[0] == OPEN_BRACE) {
|
2005-03-18 15:34:07 +00:00
|
|
|
/* multi letter variable name */
|
2005-03-21 12:07:14 +00:00
|
|
|
return (VarParseLong(vp, freeResult));
|
2005-03-18 15:34:07 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* single letter variable name */
|
2005-03-21 12:07:14 +00:00
|
|
|
return (VarParseShort(vp, freeResult));
|
2005-03-18 15:34:07 +00:00
|
|
|
}
|
2005-02-26 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* Given the start of a variable invocation, extract the variable
|
|
|
|
* name and find its value, then modify it according to the
|
|
|
|
* specification.
|
2005-02-26 08:54:40 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
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 = {
|
2005-03-21 07:40:33 +00:00
|
|
|
input,
|
2005-03-18 15:34:07 +00:00
|
|
|
input,
|
|
|
|
ctxt,
|
2005-05-10 08:06:13 +00:00
|
|
|
err,
|
|
|
|
TRUE
|
2005-03-18 15:34:07 +00:00
|
|
|
};
|
2005-03-21 11:43:02 +00:00
|
|
|
char *value;
|
2005-03-02 14:30:36 +00:00
|
|
|
|
2005-03-21 12:07:14 +00:00
|
|
|
value = VarParse(&vp, freeResult);
|
|
|
|
*consumed += vp.ptr - vp.input;
|
2005-03-21 11:43:02 +00:00
|
|
|
return (value);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 08:06:13 +00:00
|
|
|
/*
|
2005-05-10 11:55:46 +00:00
|
|
|
* Given the start of a variable invocation, determine the length
|
|
|
|
* of the specification.
|
2005-05-10 08:06:13 +00:00
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* The number of characters in the specification. For invalid
|
|
|
|
* specifications, this is just 2 to skip the '$' and the
|
|
|
|
* following letter, or 1 if '$' was the last character in the
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
size_t
|
|
|
|
Var_Match(const char input[], GNode *ctxt)
|
|
|
|
{
|
|
|
|
VarParser vp = {
|
|
|
|
input,
|
|
|
|
input,
|
|
|
|
ctxt,
|
|
|
|
FALSE,
|
|
|
|
FALSE
|
|
|
|
};
|
|
|
|
char *value;
|
|
|
|
Boolean freeResult;
|
|
|
|
|
|
|
|
value = VarParse(&vp, &freeResult);
|
|
|
|
if (freeResult) {
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
return (vp.ptr - vp.input);
|
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
static int
|
|
|
|
match_var(const char str[], const char var[])
|
|
|
|
{
|
|
|
|
const char *start = str;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
str++; /* consume '$' */
|
|
|
|
|
|
|
|
if (str[0] == OPEN_PAREN || str[0] == OPEN_BRACE) {
|
|
|
|
str++; /* consume opening paren or brace */
|
|
|
|
|
|
|
|
while (str[0] != '\0') {
|
|
|
|
if (str[0] == '$') {
|
|
|
|
/*
|
|
|
|
* A variable inside the variable. We cannot
|
|
|
|
* expand the external variable yet.
|
|
|
|
*/
|
|
|
|
return (str - start);
|
|
|
|
} else if (str[0] == ':' ||
|
|
|
|
str[0] == CLOSE_PAREN ||
|
|
|
|
str[0] == CLOSE_BRACE) {
|
|
|
|
len = str - (start + 2);
|
|
|
|
|
2005-11-30 20:38:03 +00:00
|
|
|
if (strncmp(var, start + 2, len) == 0 && var[len] == '\0') {
|
2005-05-10 11:55:46 +00:00
|
|
|
return (0); /* match */
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Not the variable we want to
|
|
|
|
* expand.
|
|
|
|
*/
|
|
|
|
return (str - start);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (str - start);
|
|
|
|
} else {
|
|
|
|
/* Single letter variable name */
|
|
|
|
if (var[1] == '\0' && var[0] == str[0]) {
|
|
|
|
return (0); /* match */
|
|
|
|
} else {
|
|
|
|
str++; /* consume variable name */
|
|
|
|
return (str - start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Substitute for all variables in the given string in the given
|
|
|
|
* context If err is TRUE, Parse_Error will be called when an
|
|
|
|
* undefined variable is encountered.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* 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 *
|
2009-01-06 16:24:22 +00:00
|
|
|
Var_Subst(const char *str, GNode *ctxt, Boolean err)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-05-09 14:06:04 +00:00
|
|
|
Boolean errorReported;
|
2005-03-18 15:44:23 +00:00
|
|
|
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);
|
2005-05-09 14:06:04 +00:00
|
|
|
while (str[0] != '\0') {
|
|
|
|
if ((str[0] == '$') && (str[1] == '$')) {
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2005-05-09 14:06:04 +00:00
|
|
|
* A dollar sign may be escaped 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-05-09 14:06:04 +00:00
|
|
|
str++;
|
2005-03-18 15:44:23 +00:00
|
|
|
Buf_AddByte(buf, (Byte)str[0]);
|
2005-05-09 14:06:04 +00:00
|
|
|
str++;
|
2005-03-18 15:44:23 +00:00
|
|
|
|
|
|
|
} else if (str[0] == '$') {
|
2005-05-09 14:06:04 +00:00
|
|
|
/* Variable invocation. */
|
|
|
|
VarParser subvp = {
|
2005-03-22 08:20:08 +00:00
|
|
|
str,
|
|
|
|
str,
|
|
|
|
ctxt,
|
2005-05-10 08:06:13 +00:00
|
|
|
err,
|
|
|
|
TRUE
|
2005-03-22 08:20:08 +00:00
|
|
|
};
|
|
|
|
char *rval;
|
|
|
|
Boolean rfree;
|
|
|
|
|
|
|
|
rval = VarParse(&subvp, &rfree);
|
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-22 08:18:15 +00:00
|
|
|
if (rval == var_Error || rval == varNoError) {
|
2005-03-18 15:44:23 +00:00
|
|
|
/*
|
|
|
|
* 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) {
|
2005-03-22 08:20:08 +00:00
|
|
|
str = subvp.ptr;
|
2005-03-22 08:18:15 +00:00
|
|
|
} else if (err) {
|
2005-03-18 15:44:23 +00:00
|
|
|
/*
|
|
|
|
* 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,
|
2005-03-22 08:20:08 +00:00
|
|
|
"Undefined variable \"%.*s\"", subvp.ptr - subvp.input, str);
|
2005-03-18 15:44:23 +00:00
|
|
|
}
|
|
|
|
errorReported = TRUE;
|
2005-05-09 14:06:04 +00:00
|
|
|
str = subvp.ptr;
|
2005-03-18 15:44:23 +00:00
|
|
|
} else {
|
2005-05-09 14:06:04 +00:00
|
|
|
Buf_AddByte(buf, (Byte)str[0]);
|
|
|
|
str++;
|
2005-03-18 15:44:23 +00:00
|
|
|
}
|
2005-02-11 10:49:01 +00:00
|
|
|
} else {
|
2005-03-18 15:44:23 +00:00
|
|
|
/*
|
|
|
|
* Copy all the characters from the variable
|
|
|
|
* value straight into the new string.
|
|
|
|
*/
|
2005-03-22 08:18:15 +00:00
|
|
|
Buf_Append(buf, rval);
|
|
|
|
if (rfree) {
|
|
|
|
free(rval);
|
2005-03-18 15:44:23 +00:00
|
|
|
}
|
2005-05-09 14:06:04 +00:00
|
|
|
str = subvp.ptr;
|
2005-02-10 17:00:16 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
} else {
|
2005-05-09 14:06:04 +00:00
|
|
|
Buf_AddByte(buf, (Byte)str[0]);
|
|
|
|
str++;
|
2005-03-18 15:44:23 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* Substitute for all variables except if it is the same as 'var',
|
|
|
|
* in the given string in the given context. If err 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-05-09 14:06:04 +00:00
|
|
|
Buffer *
|
2005-05-10 12:02:15 +00:00
|
|
|
Var_SubstOnly(const char *var, const char *str, Boolean err)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-05-10 12:02:15 +00:00
|
|
|
GNode *ctxt = VAR_GLOBAL;
|
2005-05-09 14:06:04 +00:00
|
|
|
Boolean errorReported;
|
|
|
|
Buffer *buf; /* Buffer for forming things */
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-05-09 14:06:04 +00:00
|
|
|
/*
|
|
|
|
* Set TRUE if an error has already been reported to prevent a
|
|
|
|
* plethora of messages when recursing. XXXHB this comment sounds
|
|
|
|
* wrong.
|
|
|
|
*/
|
|
|
|
errorReported = FALSE;
|
|
|
|
|
|
|
|
buf = Buf_Init(0);
|
|
|
|
while (str[0] != '\0') {
|
|
|
|
if (str[0] == '$') {
|
|
|
|
int skip;
|
|
|
|
|
|
|
|
skip = match_var(str, var);
|
|
|
|
if (skip > 0) {
|
|
|
|
Buf_AddBytes(buf, skip, str);
|
|
|
|
str += skip;
|
|
|
|
} else {
|
|
|
|
/* Variable invocation. */
|
|
|
|
VarParser subvp = {
|
|
|
|
str,
|
|
|
|
str,
|
|
|
|
ctxt,
|
2005-05-10 08:06:13 +00:00
|
|
|
err,
|
|
|
|
TRUE
|
2005-05-09 14:06:04 +00:00
|
|
|
};
|
|
|
|
char *rval;
|
|
|
|
Boolean rfree;
|
|
|
|
|
|
|
|
rval = VarParse(&subvp, &rfree);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When we get down here, rval should either
|
|
|
|
* point to the value of this variable, or be
|
|
|
|
* NULL.
|
|
|
|
*/
|
|
|
|
if (rval == var_Error || rval == 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 = subvp.ptr;
|
|
|
|
} else if (err) {
|
|
|
|
/*
|
|
|
|
* 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\"", subvp.ptr - subvp.input, str);
|
|
|
|
}
|
|
|
|
errorReported = TRUE;
|
|
|
|
str = subvp.ptr;
|
|
|
|
} else {
|
|
|
|
Buf_AddByte(buf, (Byte)str[0]);
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Copy all the characters from the
|
|
|
|
* variable value straight into the
|
|
|
|
* new string.
|
|
|
|
*/
|
|
|
|
Buf_Append(buf, rval);
|
|
|
|
if (rfree) {
|
|
|
|
free(rval);
|
|
|
|
}
|
|
|
|
str = subvp.ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Buf_AddByte(buf, (Byte)str[0]);
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (buf);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* Initialize the module
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-05-06 18:30:06 +00:00
|
|
|
Var_Init(char **env)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-05-06 18:30:06 +00:00
|
|
|
char **ptr;
|
2004-11-30 17:46:29 +00:00
|
|
|
|
2005-03-18 15:44:23 +00:00
|
|
|
VAR_CMD = Targ_NewGN("Command");
|
2005-05-06 18:30:06 +00:00
|
|
|
VAR_ENV = Targ_NewGN("Environment");
|
|
|
|
VAR_GLOBAL = Targ_NewGN("Global");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy user environment variables into ENV context.
|
|
|
|
*/
|
|
|
|
for (ptr = env; *ptr != NULL; ++ptr) {
|
|
|
|
char *tmp = estrdup(*ptr);
|
|
|
|
const char *name = tmp;
|
|
|
|
char *sep = strchr(name, '=');
|
|
|
|
const char *value = sep + 1;
|
|
|
|
|
|
|
|
if (sep != NULL) {
|
|
|
|
*sep = '\0';
|
|
|
|
VarAdd(name, value, VAR_ENV);
|
|
|
|
}
|
|
|
|
free(tmp);
|
|
|
|
}
|
1995-01-23 21:03:17 +00:00
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2005-05-10 11:55:46 +00:00
|
|
|
/**
|
|
|
|
* Print all variables in global and command line contexts.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
void
|
2005-05-10 08:14:26 +00:00
|
|
|
Var_Dump(void)
|
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-05-10 08:14:26 +00:00
|
|
|
printf("#*** Global Variables:\n");
|
|
|
|
LST_FOREACH(ln, &VAR_GLOBAL->context) {
|
|
|
|
v = Lst_Datum(ln);
|
|
|
|
printf("%-16s = %s\n", v->name, Buf_Data(v->val));
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("#*** Command-line Variables:\n");
|
|
|
|
LST_FOREACH(ln, &VAR_CMD->context) {
|
2005-03-10 15:38:01 +00:00
|
|
|
v = Lst_Datum(ln);
|
2005-03-22 07:50:40 +00:00
|
|
|
printf("%-16s = %s\n", v->name, Buf_Data(v->val));
|
2005-03-10 15:38:01 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-05-10 08:14:26 +00:00
|
|
|
|
2005-05-12 14:31:42 +00:00
|
|
|
/**
|
|
|
|
* Print the values of any variables requested by
|
|
|
|
* the user.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Var_Print(Lst *vlist, Boolean expandVars)
|
|
|
|
{
|
|
|
|
LstNode *n;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
LST_FOREACH(n, vlist) {
|
|
|
|
name = Lst_Datum(n);
|
|
|
|
if (expandVars) {
|
2005-05-24 16:05:51 +00:00
|
|
|
char *value;
|
|
|
|
char *v;
|
|
|
|
|
2005-05-12 14:31:42 +00:00
|
|
|
v = emalloc(strlen(name) + 1 + 3);
|
|
|
|
sprintf(v, "${%s}", name);
|
|
|
|
|
2005-05-24 15:58:35 +00:00
|
|
|
value = Buf_Peel(Var_Subst(v, VAR_GLOBAL, FALSE));
|
2005-05-12 14:31:42 +00:00
|
|
|
printf("%s\n", value);
|
|
|
|
|
|
|
|
free(v);
|
|
|
|
free(value);
|
|
|
|
} else {
|
2005-05-24 16:05:51 +00:00
|
|
|
const char *value = Var_Value(name, VAR_GLOBAL);
|
2005-05-12 14:31:42 +00:00
|
|
|
printf("%s\n", value != NULL ? value : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|