1991-09-13 23:39:37 +00:00
|
|
|
|
/* Variable expansion functions for GNU Make.
|
2006-02-11 19:02:21 +00:00
|
|
|
|
Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
2007-07-04 19:35:15 +00:00
|
|
|
|
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
|
2006-02-11 22:16:04 +00:00
|
|
|
|
Foundation, Inc.
|
1991-09-13 23:39:37 +00:00
|
|
|
|
This file is part of GNU Make.
|
|
|
|
|
|
2006-02-11 19:02:21 +00:00
|
|
|
|
GNU Make is free software; you can redistribute it and/or modify it under the
|
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
2007-07-04 19:35:15 +00:00
|
|
|
|
Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
|
version.
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
2006-02-11 19:02:21 +00:00
|
|
|
|
GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
2006-02-11 19:02:21 +00:00
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
2007-07-04 19:35:15 +00:00
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>. */
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
2000-06-19 21:22:44 +00:00
|
|
|
|
#include "make.h"
|
|
|
|
|
|
1999-11-17 07:33:47 +00:00
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
1996-03-20 14:57:41 +00:00
|
|
|
|
#include "filedef.h"
|
|
|
|
|
#include "job.h"
|
1991-09-13 23:39:37 +00:00
|
|
|
|
#include "commands.h"
|
|
|
|
|
#include "variable.h"
|
1998-07-30 20:54:47 +00:00
|
|
|
|
#include "rule.h"
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
2006-02-15 23:54:42 +00:00
|
|
|
|
/* Initially, any errors reported when expanding strings will be reported
|
|
|
|
|
against the file where the error appears. */
|
|
|
|
|
const struct floc **expanding_var = &reading_file;
|
|
|
|
|
|
1992-06-11 02:27:09 +00:00
|
|
|
|
/* The next two describe the variable output buffer.
|
|
|
|
|
This buffer is used to hold the variable-expansion of a line of the
|
|
|
|
|
makefile. It is made bigger with realloc whenever it is too small.
|
|
|
|
|
variable_buffer_length is the size currently allocated.
|
1998-07-30 20:54:47 +00:00
|
|
|
|
variable_buffer is the address of the buffer.
|
|
|
|
|
|
|
|
|
|
For efficiency, it's guaranteed that the buffer will always have
|
|
|
|
|
VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
|
|
|
|
|
extra chars without having to call a function. Note you should never use
|
|
|
|
|
these bytes unless you're _sure_ you have room (you know when the buffer
|
|
|
|
|
length was last checked. */
|
|
|
|
|
|
|
|
|
|
#define VARIABLE_BUFFER_ZONE 5
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
1992-06-11 02:27:09 +00:00
|
|
|
|
static unsigned int variable_buffer_length;
|
1998-07-30 20:54:47 +00:00
|
|
|
|
char *variable_buffer;
|
1992-06-11 02:27:09 +00:00
|
|
|
|
|
|
|
|
|
/* Subroutine of variable_expand and friends:
|
|
|
|
|
The text to add is LENGTH chars starting at STRING to the variable_buffer.
|
|
|
|
|
The text is added to the buffer at PTR, and the updated pointer into
|
|
|
|
|
the buffer is returned as the value. Thus, the value returned by
|
|
|
|
|
each call to variable_buffer_output should be the first argument to
|
|
|
|
|
the following call. */
|
|
|
|
|
|
1992-06-12 00:29:21 +00:00
|
|
|
|
char *
|
2006-11-18 20:53:44 +00:00
|
|
|
|
variable_buffer_output (char *ptr, const char *string, unsigned int length)
|
1992-06-11 02:27:09 +00:00
|
|
|
|
{
|
|
|
|
|
register unsigned int newlen = length + (ptr - variable_buffer);
|
|
|
|
|
|
1998-07-30 20:54:47 +00:00
|
|
|
|
if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
|
1992-06-11 02:27:09 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned int offset = ptr - variable_buffer;
|
1992-06-16 23:43:29 +00:00
|
|
|
|
variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
|
|
|
|
|
? newlen + 100
|
|
|
|
|
: 2 * variable_buffer_length);
|
2006-04-09 22:09:24 +00:00
|
|
|
|
variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
|
1992-06-11 02:27:09 +00:00
|
|
|
|
ptr = variable_buffer + offset;
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-09 22:09:24 +00:00
|
|
|
|
memcpy (ptr, string, length);
|
1992-06-11 02:27:09 +00:00
|
|
|
|
return ptr + length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return a pointer to the beginning of the variable buffer. */
|
|
|
|
|
|
|
|
|
|
static char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
initialize_variable_output (void)
|
1992-06-11 02:27:09 +00:00
|
|
|
|
{
|
|
|
|
|
/* If we don't have a variable output buffer yet, get one. */
|
|
|
|
|
|
|
|
|
|
if (variable_buffer == 0)
|
|
|
|
|
{
|
|
|
|
|
variable_buffer_length = 200;
|
2006-04-09 22:09:24 +00:00
|
|
|
|
variable_buffer = xmalloc (variable_buffer_length);
|
1992-06-11 02:27:09 +00:00
|
|
|
|
variable_buffer[0] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return variable_buffer;
|
|
|
|
|
}
|
|
|
|
|
|
1991-09-13 23:39:37 +00:00
|
|
|
|
/* Recursively expand V. The returned string is malloc'd. */
|
|
|
|
|
|
2006-04-07 01:43:44 +00:00
|
|
|
|
static char *allocated_variable_append (const struct variable *v);
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
1992-12-22 23:44:03 +00:00
|
|
|
|
char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
recursively_expand_for_file (struct variable *v, struct file *file)
|
1991-09-13 23:39:37 +00:00
|
|
|
|
{
|
|
|
|
|
char *value;
|
2006-02-15 23:54:42 +00:00
|
|
|
|
const struct floc *this_var;
|
|
|
|
|
const struct floc **saved_varp;
|
2006-03-10 02:20:45 +00:00
|
|
|
|
struct variable_set_list *save = 0;
|
2003-11-04 07:40:29 +00:00
|
|
|
|
int set_reading = 0;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
2006-02-15 23:54:42 +00:00
|
|
|
|
/* Don't install a new location if this location is empty.
|
|
|
|
|
This can happen for command-line variables, builtin variables, etc. */
|
|
|
|
|
saved_varp = expanding_var;
|
|
|
|
|
if (v->fileinfo.filenm)
|
|
|
|
|
{
|
|
|
|
|
this_var = &v->fileinfo;
|
|
|
|
|
expanding_var = &this_var;
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-20 02:14:00 +00:00
|
|
|
|
/* If we have no other file-reading context, use the variable's context. */
|
|
|
|
|
if (!reading_file)
|
|
|
|
|
{
|
|
|
|
|
set_reading = 1;
|
|
|
|
|
reading_file = &v->fileinfo;
|
|
|
|
|
}
|
|
|
|
|
|
1991-09-13 23:39:37 +00:00
|
|
|
|
if (v->expanding)
|
2002-05-10 03:15:07 +00:00
|
|
|
|
{
|
|
|
|
|
if (!v->exp_count)
|
|
|
|
|
/* Expanding V causes infinite recursion. Lose. */
|
2006-02-20 02:14:00 +00:00
|
|
|
|
fatal (*expanding_var,
|
2002-05-10 03:15:07 +00:00
|
|
|
|
_("Recursive variable `%s' references itself (eventually)"),
|
|
|
|
|
v->name);
|
|
|
|
|
--v->exp_count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
|
{
|
|
|
|
|
save = current_variable_set_list;
|
|
|
|
|
current_variable_set_list = file->variables;
|
|
|
|
|
}
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
|
|
|
|
v->expanding = 1;
|
1999-11-17 07:33:47 +00:00
|
|
|
|
if (v->append)
|
2000-03-27 06:54:37 +00:00
|
|
|
|
value = allocated_variable_append (v);
|
1999-11-17 07:33:47 +00:00
|
|
|
|
else
|
|
|
|
|
value = allocated_variable_expand (v->value);
|
1991-09-13 23:39:37 +00:00
|
|
|
|
v->expanding = 0;
|
|
|
|
|
|
2003-11-04 07:40:29 +00:00
|
|
|
|
if (set_reading)
|
|
|
|
|
reading_file = 0;
|
2006-02-15 23:54:42 +00:00
|
|
|
|
|
2002-05-10 03:15:07 +00:00
|
|
|
|
if (file)
|
|
|
|
|
current_variable_set_list = save;
|
1993-06-25 19:20:36 +00:00
|
|
|
|
|
2006-02-15 23:54:42 +00:00
|
|
|
|
expanding_var = saved_varp;
|
|
|
|
|
|
2002-05-10 03:15:07 +00:00
|
|
|
|
return value;
|
1993-06-25 19:20:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Expand a simple reference to variable NAME, which is LENGTH chars long. */
|
1993-05-06 21:14:33 +00:00
|
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
|
__inline
|
|
|
|
|
#endif
|
|
|
|
|
static char *
|
2006-11-18 20:53:44 +00:00
|
|
|
|
reference_variable (char *o, const char *name, unsigned int length)
|
1993-05-06 21:14:33 +00:00
|
|
|
|
{
|
2006-11-18 20:53:44 +00:00
|
|
|
|
struct variable *v;
|
1999-11-17 07:33:47 +00:00
|
|
|
|
char *value;
|
1993-05-06 21:14:33 +00:00
|
|
|
|
|
2000-03-27 06:54:37 +00:00
|
|
|
|
v = lookup_variable (name, length);
|
2000-02-07 19:54:04 +00:00
|
|
|
|
|
1993-06-25 19:20:36 +00:00
|
|
|
|
if (v == 0)
|
|
|
|
|
warn_undefined (name, length);
|
|
|
|
|
|
2004-03-22 15:11:48 +00:00
|
|
|
|
/* If there's no variable by that name or it has no value, stop now. */
|
|
|
|
|
if (v == 0 || (*v->value == '\0' && !v->append))
|
1999-11-17 07:33:47 +00:00
|
|
|
|
return o;
|
|
|
|
|
|
2000-03-27 06:54:37 +00:00
|
|
|
|
value = (v->recursive ? recursively_expand (v) : v->value);
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
|
|
|
|
o = variable_buffer_output (o, value, strlen (value));
|
|
|
|
|
|
|
|
|
|
if (v->recursive)
|
|
|
|
|
free (value);
|
1993-05-06 21:14:33 +00:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
1998-07-30 20:54:47 +00:00
|
|
|
|
/* Scan STRING for variable references and expansion-function calls. Only
|
|
|
|
|
LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
|
|
|
|
|
a null byte is found.
|
|
|
|
|
|
|
|
|
|
Write the results to LINE, which must point into `variable_buffer'. If
|
|
|
|
|
LINE is NULL, start at the beginning of the buffer.
|
|
|
|
|
Return a pointer to LINE, or to the beginning of the buffer if LINE is
|
2006-11-18 20:53:44 +00:00
|
|
|
|
NULL.
|
|
|
|
|
*/
|
1991-09-13 23:39:37 +00:00
|
|
|
|
char *
|
2006-11-18 20:53:44 +00:00
|
|
|
|
variable_expand_string (char *line, const char *string, long length)
|
1991-09-13 23:39:37 +00:00
|
|
|
|
{
|
2006-04-09 22:09:24 +00:00
|
|
|
|
struct variable *v;
|
2006-11-18 20:53:44 +00:00
|
|
|
|
const char *p, *p1;
|
|
|
|
|
char *abuf = NULL;
|
|
|
|
|
char *o;
|
1998-07-30 20:54:47 +00:00
|
|
|
|
unsigned int line_offset;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
1998-07-30 20:54:47 +00:00
|
|
|
|
if (!line)
|
|
|
|
|
line = initialize_variable_output();
|
|
|
|
|
o = line;
|
|
|
|
|
line_offset = line - variable_buffer;
|
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
if (length == 0)
|
1998-07-30 20:54:47 +00:00
|
|
|
|
{
|
2006-11-18 20:53:44 +00:00
|
|
|
|
variable_buffer_output (o, "", 1);
|
|
|
|
|
return (variable_buffer);
|
1998-07-30 20:54:47 +00:00
|
|
|
|
}
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
/* If we want a subset of the string, allocate a temporary buffer for it.
|
|
|
|
|
Most of the functions we use here don't work with length limits. */
|
|
|
|
|
if (length > 0 && string[length] != '\0')
|
|
|
|
|
{
|
|
|
|
|
abuf = xmalloc(length+1);
|
|
|
|
|
memcpy(abuf, string, length);
|
|
|
|
|
abuf[length] = '\0';
|
|
|
|
|
string = abuf;
|
|
|
|
|
}
|
|
|
|
|
p = string;
|
|
|
|
|
|
1991-09-13 23:39:37 +00:00
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
/* Copy all following uninteresting chars all at once to the
|
|
|
|
|
variable output buffer, and skip them. Uninteresting chars end
|
|
|
|
|
at the next $ or the end of the input. */
|
|
|
|
|
|
1999-10-15 07:00:58 +00:00
|
|
|
|
p1 = strchr (p, '$');
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
2005-07-12 04:35:13 +00:00
|
|
|
|
o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
|
|
|
|
if (p1 == 0)
|
|
|
|
|
break;
|
|
|
|
|
p = p1 + 1;
|
|
|
|
|
|
|
|
|
|
/* Dispatch on the char that follows the $. */
|
|
|
|
|
|
|
|
|
|
switch (*p)
|
|
|
|
|
{
|
|
|
|
|
case '$':
|
|
|
|
|
/* $$ seen means output one $ to the variable output buffer. */
|
|
|
|
|
o = variable_buffer_output (o, p, 1);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
|
case '{':
|
|
|
|
|
/* $(...) or ${...} is the general case of substitution. */
|
|
|
|
|
{
|
|
|
|
|
char openparen = *p;
|
|
|
|
|
char closeparen = (openparen == '(') ? ')' : '}';
|
2006-11-18 20:53:44 +00:00
|
|
|
|
const char *begp;
|
|
|
|
|
const char *beg = p + 1;
|
|
|
|
|
char *op;
|
|
|
|
|
char *abeg = NULL;
|
|
|
|
|
const char *end, *colon;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
|
|
|
|
op = o;
|
|
|
|
|
begp = p;
|
|
|
|
|
if (handle_function (&op, &begp))
|
|
|
|
|
{
|
|
|
|
|
o = op;
|
|
|
|
|
p = begp;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Is there a variable reference inside the parens or braces?
|
|
|
|
|
If so, expand it before expanding the entire reference. */
|
|
|
|
|
|
1999-10-15 07:00:58 +00:00
|
|
|
|
end = strchr (beg, closeparen);
|
1995-01-15 15:32:37 +00:00
|
|
|
|
if (end == 0)
|
1998-10-03 05:39:55 +00:00
|
|
|
|
/* Unterminated variable reference. */
|
2006-02-15 23:54:42 +00:00
|
|
|
|
fatal (*expanding_var, _("unterminated variable reference"));
|
1995-01-15 15:32:37 +00:00
|
|
|
|
p1 = lindex (beg, end, '$');
|
1991-09-13 23:39:37 +00:00
|
|
|
|
if (p1 != 0)
|
|
|
|
|
{
|
|
|
|
|
/* BEG now points past the opening paren or brace.
|
|
|
|
|
Count parens or braces until it is matched. */
|
|
|
|
|
int count = 0;
|
|
|
|
|
for (p = beg; *p != '\0'; ++p)
|
|
|
|
|
{
|
|
|
|
|
if (*p == openparen)
|
|
|
|
|
++count;
|
|
|
|
|
else if (*p == closeparen && --count < 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
1993-02-03 20:59:22 +00:00
|
|
|
|
/* If COUNT is >= 0, there were unmatched opening parens
|
1991-09-13 23:39:37 +00:00
|
|
|
|
or braces, so we go to the simple case of a variable name
|
|
|
|
|
such as `$($(a)'. */
|
|
|
|
|
if (count < 0)
|
|
|
|
|
{
|
2006-11-18 20:53:44 +00:00
|
|
|
|
abeg = expand_argument (beg, p); /* Expand the name. */
|
|
|
|
|
beg = abeg;
|
1999-10-15 07:00:58 +00:00
|
|
|
|
end = strchr (beg, '\0');
|
1991-09-13 23:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1995-01-15 15:32:37 +00:00
|
|
|
|
else
|
|
|
|
|
/* Advance P to the end of this reference. After we are
|
|
|
|
|
finished expanding this one, P will be incremented to
|
|
|
|
|
continue the scan. */
|
|
|
|
|
p = end;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
|
|
|
|
/* This is not a reference to a built-in function and
|
1995-01-15 15:32:37 +00:00
|
|
|
|
any variable references inside are now expanded.
|
|
|
|
|
Is the resultant text a substitution reference? */
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
1995-01-15 16:33:35 +00:00
|
|
|
|
colon = lindex (beg, end, ':');
|
2002-05-10 03:15:07 +00:00
|
|
|
|
if (colon)
|
1991-09-13 23:39:37 +00:00
|
|
|
|
{
|
1995-01-15 15:32:37 +00:00
|
|
|
|
/* This looks like a substitution reference: $(FOO:A=B). */
|
2006-11-18 20:53:44 +00:00
|
|
|
|
const char *subst_beg, *subst_end, *replace_beg, *replace_end;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
1993-05-06 21:14:33 +00:00
|
|
|
|
subst_beg = colon + 1;
|
2002-10-05 13:45:47 +00:00
|
|
|
|
subst_end = lindex (subst_beg, end, '=');
|
1995-01-15 15:32:37 +00:00
|
|
|
|
if (subst_end == 0)
|
|
|
|
|
/* There is no = in sight. Punt on the substitution
|
|
|
|
|
reference and treat this as a variable name containing
|
|
|
|
|
a colon, in the code below. */
|
|
|
|
|
colon = 0;
|
|
|
|
|
else
|
1991-09-13 23:39:37 +00:00
|
|
|
|
{
|
1995-01-15 15:32:37 +00:00
|
|
|
|
replace_beg = subst_end + 1;
|
|
|
|
|
replace_end = end;
|
1993-05-06 21:14:33 +00:00
|
|
|
|
|
1995-01-15 15:32:37 +00:00
|
|
|
|
/* Extract the variable name before the colon
|
|
|
|
|
and look up that variable. */
|
|
|
|
|
v = lookup_variable (beg, colon - beg);
|
|
|
|
|
if (v == 0)
|
|
|
|
|
warn_undefined (beg, colon - beg);
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
2004-09-21 04:00:31 +00:00
|
|
|
|
/* If the variable is not empty, perform the
|
|
|
|
|
substitution. */
|
1995-01-15 15:32:37 +00:00
|
|
|
|
if (v != 0 && *v->value != '\0')
|
|
|
|
|
{
|
2004-09-21 04:00:31 +00:00
|
|
|
|
char *pattern, *replace, *ppercent, *rpercent;
|
|
|
|
|
char *value = (v->recursive
|
|
|
|
|
? recursively_expand (v)
|
1995-01-15 15:32:37 +00:00
|
|
|
|
: v->value);
|
2004-09-21 04:00:31 +00:00
|
|
|
|
|
|
|
|
|
/* Copy the pattern and the replacement. Add in an
|
|
|
|
|
extra % at the beginning to use in case there
|
|
|
|
|
isn't one in the pattern. */
|
2006-04-09 22:09:24 +00:00
|
|
|
|
pattern = alloca (subst_end - subst_beg + 2);
|
2004-09-21 04:00:31 +00:00
|
|
|
|
*(pattern++) = '%';
|
2006-04-09 22:09:24 +00:00
|
|
|
|
memcpy (pattern, subst_beg, subst_end - subst_beg);
|
2004-09-21 04:00:31 +00:00
|
|
|
|
pattern[subst_end - subst_beg] = '\0';
|
|
|
|
|
|
2006-04-09 22:09:24 +00:00
|
|
|
|
replace = alloca (replace_end - replace_beg + 2);
|
2004-09-21 04:00:31 +00:00
|
|
|
|
*(replace++) = '%';
|
2006-04-09 22:09:24 +00:00
|
|
|
|
memcpy (replace, replace_beg,
|
2004-09-21 04:00:31 +00:00
|
|
|
|
replace_end - replace_beg);
|
|
|
|
|
replace[replace_end - replace_beg] = '\0';
|
|
|
|
|
|
|
|
|
|
/* Look for %. Set the percent pointers properly
|
|
|
|
|
based on whether we find one or not. */
|
|
|
|
|
ppercent = find_percent (pattern);
|
|
|
|
|
if (ppercent)
|
|
|
|
|
{
|
|
|
|
|
++ppercent;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
rpercent = find_percent (replace);
|
|
|
|
|
if (rpercent)
|
|
|
|
|
++rpercent;
|
2004-09-21 04:00:31 +00:00
|
|
|
|
}
|
1995-01-15 15:32:37 +00:00
|
|
|
|
else
|
2004-09-21 04:00:31 +00:00
|
|
|
|
{
|
|
|
|
|
ppercent = pattern;
|
|
|
|
|
rpercent = replace;
|
|
|
|
|
--pattern;
|
|
|
|
|
--replace;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
o = patsubst_expand_pat (o, value, pattern, replace,
|
|
|
|
|
ppercent, rpercent);
|
2004-09-21 04:00:31 +00:00
|
|
|
|
|
1995-01-15 15:32:37 +00:00
|
|
|
|
if (v->recursive)
|
|
|
|
|
free (value);
|
|
|
|
|
}
|
1991-09-13 23:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1995-01-15 15:32:37 +00:00
|
|
|
|
if (colon == 0)
|
|
|
|
|
/* This is an ordinary variable reference.
|
|
|
|
|
Look up the value of the variable. */
|
1993-05-06 21:14:33 +00:00
|
|
|
|
o = reference_variable (o, beg, end - beg);
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
if (abeg)
|
|
|
|
|
free (abeg);
|
1991-09-13 23:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2000-06-07 05:43:37 +00:00
|
|
|
|
if (isblank ((unsigned char)p[-1]))
|
1991-09-13 23:39:37 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* A $ followed by a random char is a variable reference:
|
|
|
|
|
$a is equivalent to $(a). */
|
2006-02-15 23:54:42 +00:00
|
|
|
|
o = reference_variable (o, p, 1);
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
|
|
|
|
break;
|
1998-07-30 20:54:47 +00:00
|
|
|
|
}
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
|
|
|
|
if (*p == '\0')
|
|
|
|
|
break;
|
|
|
|
|
else
|
|
|
|
|
++p;
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
if (abuf)
|
|
|
|
|
free (abuf);
|
1998-07-30 20:54:47 +00:00
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
variable_buffer_output (o, "", 1);
|
1998-07-30 20:54:47 +00:00
|
|
|
|
return (variable_buffer + line_offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Scan LINE for variable references and expansion-function calls.
|
|
|
|
|
Build in `variable_buffer' the result of expanding the references and calls.
|
|
|
|
|
Return the address of the resulting string, which is null-terminated
|
|
|
|
|
and is valid only until the next time this function is called. */
|
|
|
|
|
|
|
|
|
|
char *
|
2006-11-18 20:53:44 +00:00
|
|
|
|
variable_expand (const char *line)
|
1998-07-30 20:54:47 +00:00
|
|
|
|
{
|
1999-06-14 05:26:28 +00:00
|
|
|
|
return variable_expand_string(NULL, line, (long)-1);
|
1991-09-13 23:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Expand an argument for an expansion function.
|
|
|
|
|
The text starting at STR and ending at END is variable-expanded
|
|
|
|
|
into a null-terminated string that is returned as the value.
|
|
|
|
|
This is done without clobbering `variable_buffer' or the current
|
|
|
|
|
variable-expansion that is in progress. */
|
|
|
|
|
|
|
|
|
|
char *
|
2003-01-30 06:21:36 +00:00
|
|
|
|
expand_argument (const char *str, const char *end)
|
1991-09-13 23:39:37 +00:00
|
|
|
|
{
|
2009-08-02 16:05:42 +00:00
|
|
|
|
char *tmp, *alloc = NULL;
|
|
|
|
|
char *r;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
2000-06-19 21:22:44 +00:00
|
|
|
|
if (str == end)
|
|
|
|
|
return xstrdup("");
|
|
|
|
|
|
2000-01-11 07:31:42 +00:00
|
|
|
|
if (!end || *end == '\0')
|
2006-04-09 22:09:24 +00:00
|
|
|
|
return allocated_variable_expand (str);
|
2003-01-30 06:21:36 +00:00
|
|
|
|
|
2009-08-02 16:05:42 +00:00
|
|
|
|
if (end - str + 1 > 1000)
|
|
|
|
|
tmp = alloc = xmalloc (end - str + 1);
|
|
|
|
|
else
|
|
|
|
|
tmp = alloca (end - str + 1);
|
|
|
|
|
|
2006-04-09 22:09:24 +00:00
|
|
|
|
memcpy (tmp, str, end - str);
|
2003-01-30 06:21:36 +00:00
|
|
|
|
tmp[end - str] = '\0';
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
2009-08-02 16:05:42 +00:00
|
|
|
|
r = allocated_variable_expand (tmp);
|
|
|
|
|
|
|
|
|
|
if (alloc)
|
|
|
|
|
free (alloc);
|
|
|
|
|
|
|
|
|
|
return r;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Expand LINE for FILE. Error messages refer to the file and line where
|
|
|
|
|
FILE's commands were found. Expansion uses FILE's variable set list. */
|
|
|
|
|
|
2005-02-27 21:40:23 +00:00
|
|
|
|
char *
|
2006-11-18 20:53:44 +00:00
|
|
|
|
variable_expand_for_file (const char *line, struct file *file)
|
1991-09-13 23:39:37 +00:00
|
|
|
|
{
|
|
|
|
|
char *result;
|
2007-06-19 04:47:58 +00:00
|
|
|
|
struct variable_set_list *savev;
|
|
|
|
|
const struct floc *savef;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
|
|
|
|
if (file == 0)
|
|
|
|
|
return variable_expand (line);
|
|
|
|
|
|
2007-06-19 04:47:58 +00:00
|
|
|
|
savev = current_variable_set_list;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
current_variable_set_list = file->variables;
|
2007-06-19 04:47:58 +00:00
|
|
|
|
|
|
|
|
|
savef = reading_file;
|
1999-08-13 07:36:26 +00:00
|
|
|
|
if (file->cmds && file->cmds->fileinfo.filenm)
|
|
|
|
|
reading_file = &file->cmds->fileinfo;
|
|
|
|
|
else
|
|
|
|
|
reading_file = 0;
|
2007-06-19 04:47:58 +00:00
|
|
|
|
|
1991-09-13 23:39:37 +00:00
|
|
|
|
result = variable_expand (line);
|
2007-06-19 04:47:58 +00:00
|
|
|
|
|
|
|
|
|
current_variable_set_list = savev;
|
|
|
|
|
reading_file = savef;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-21 06:49:11 +00:00
|
|
|
|
/* Like allocated_variable_expand, but for += target-specific variables.
|
|
|
|
|
First recursively construct the variable value from its appended parts in
|
|
|
|
|
any upper variable sets. Then expand the resulting value. */
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
|
|
|
|
static char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
variable_append (const char *name, unsigned int length,
|
|
|
|
|
const struct variable_set_list *set)
|
1999-11-17 07:33:47 +00:00
|
|
|
|
{
|
2001-01-21 06:49:11 +00:00
|
|
|
|
const struct variable *v;
|
|
|
|
|
char *buf = 0;
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
2001-01-21 06:49:11 +00:00
|
|
|
|
/* If there's nothing left to check, return the empty buffer. */
|
|
|
|
|
if (!set)
|
|
|
|
|
return initialize_variable_output ();
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
2001-01-21 06:49:11 +00:00
|
|
|
|
/* Try to find the variable in this variable set. */
|
|
|
|
|
v = lookup_variable_in_set (name, length, set->set);
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
2001-01-21 06:49:11 +00:00
|
|
|
|
/* If there isn't one, look to see if there's one in a set above us. */
|
|
|
|
|
if (!v)
|
|
|
|
|
return variable_append (name, length, set->next);
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
2001-01-21 06:49:11 +00:00
|
|
|
|
/* If this variable type is append, first get any upper values.
|
|
|
|
|
If not, initialize the buffer. */
|
|
|
|
|
if (v->append)
|
|
|
|
|
buf = variable_append (name, length, set->next);
|
|
|
|
|
else
|
|
|
|
|
buf = initialize_variable_output ();
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
2001-01-21 06:49:11 +00:00
|
|
|
|
/* Append this value to the buffer, and return it.
|
|
|
|
|
If we already have a value, first add a space. */
|
|
|
|
|
if (buf > variable_buffer)
|
|
|
|
|
buf = variable_buffer_output (buf, " ", 1);
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
2006-03-15 03:31:30 +00:00
|
|
|
|
/* Either expand it or copy it, depending. */
|
|
|
|
|
if (! v->recursive)
|
|
|
|
|
return variable_buffer_output (buf, v->value, strlen (v->value));
|
|
|
|
|
|
|
|
|
|
buf = variable_expand_string (buf, v->value, strlen (v->value));
|
|
|
|
|
return (buf + strlen (buf));
|
2001-01-21 06:49:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
allocated_variable_append (const struct variable *v)
|
2001-01-21 06:49:11 +00:00
|
|
|
|
{
|
2006-03-15 03:31:30 +00:00
|
|
|
|
char *val;
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
2001-01-21 06:49:11 +00:00
|
|
|
|
/* Construct the appended variable value. */
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
2001-01-21 06:49:11 +00:00
|
|
|
|
char *obuf = variable_buffer;
|
|
|
|
|
unsigned int olen = variable_buffer_length;
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
2001-01-21 06:49:11 +00:00
|
|
|
|
variable_buffer = 0;
|
|
|
|
|
|
|
|
|
|
val = variable_append (v->name, strlen (v->name), current_variable_set_list);
|
|
|
|
|
variable_buffer_output (val, "", 1);
|
|
|
|
|
val = variable_buffer;
|
1999-11-17 07:33:47 +00:00
|
|
|
|
|
|
|
|
|
variable_buffer = obuf;
|
|
|
|
|
variable_buffer_length = olen;
|
|
|
|
|
|
2006-03-15 03:31:30 +00:00
|
|
|
|
return val;
|
1999-11-17 07:33:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
1992-06-11 02:27:09 +00:00
|
|
|
|
/* Like variable_expand_for_file, but the returned string is malloc'd.
|
|
|
|
|
This function is called a lot. It wants to be efficient. */
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
|
|
|
|
char *
|
2006-11-18 20:53:44 +00:00
|
|
|
|
allocated_variable_expand_for_file (const char *line, struct file *file)
|
1991-09-13 23:39:37 +00:00
|
|
|
|
{
|
|
|
|
|
char *value;
|
|
|
|
|
|
1992-06-11 02:27:09 +00:00
|
|
|
|
char *obuf = variable_buffer;
|
|
|
|
|
unsigned int olen = variable_buffer_length;
|
|
|
|
|
|
|
|
|
|
variable_buffer = 0;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
|
|
|
|
value = variable_expand_for_file (line, file);
|
|
|
|
|
|
1992-06-11 02:27:09 +00:00
|
|
|
|
variable_buffer = obuf;
|
|
|
|
|
variable_buffer_length = olen;
|
1991-09-13 23:39:37 +00:00
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2002-10-25 22:01:47 +00:00
|
|
|
|
|
|
|
|
|
/* Install a new variable_buffer context, returning the current one for
|
|
|
|
|
safe-keeping. */
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
install_variable_buffer (char **bufp, unsigned int *lenp)
|
|
|
|
|
{
|
|
|
|
|
*bufp = variable_buffer;
|
|
|
|
|
*lenp = variable_buffer_length;
|
|
|
|
|
|
|
|
|
|
variable_buffer = 0;
|
|
|
|
|
initialize_variable_output ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Restore a previously-saved variable_buffer setting (free the current one).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
restore_variable_buffer (char *buf, unsigned int len)
|
|
|
|
|
{
|
|
|
|
|
free (variable_buffer);
|
|
|
|
|
|
|
|
|
|
variable_buffer = buf;
|
|
|
|
|
variable_buffer_length = len;
|
|
|
|
|
}
|