1999-03-26 04:04:42 +00:00
|
|
|
|
/* Builtin function expansion 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.
|
1992-01-11 11:37:36 +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.
|
1992-01-11 11:37:36 +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.
|
1992-01-11 11:37:36 +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/>. */
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
|
|
|
|
#include "make.h"
|
1996-03-20 14:57:41 +00:00
|
|
|
|
#include "filedef.h"
|
1992-01-11 11:37:36 +00:00
|
|
|
|
#include "variable.h"
|
|
|
|
|
#include "dep.h"
|
|
|
|
|
#include "job.h"
|
1996-03-20 14:57:41 +00:00
|
|
|
|
#include "commands.h"
|
1999-11-22 06:15:35 +00:00
|
|
|
|
#include "debug.h"
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1996-05-13 18:39:52 +00:00
|
|
|
|
#ifdef _AMIGA
|
|
|
|
|
#include "amiga.h"
|
|
|
|
|
#endif
|
1994-07-25 23:06:00 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
struct function_table_entry
|
|
|
|
|
{
|
|
|
|
|
const char *name;
|
2000-01-11 07:31:42 +00:00
|
|
|
|
unsigned char len;
|
|
|
|
|
unsigned char minimum_args;
|
|
|
|
|
unsigned char maximum_args;
|
|
|
|
|
char expand_args;
|
2006-04-07 01:43:44 +00:00
|
|
|
|
char *(*func_ptr) (char *output, char **argv, const char *fname);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
};
|
2002-07-11 06:38:57 +00:00
|
|
|
|
|
|
|
|
|
static unsigned long
|
2002-10-14 21:54:04 +00:00
|
|
|
|
function_table_entry_hash_1 (const void *keyv)
|
2002-07-11 06:38:57 +00:00
|
|
|
|
{
|
2006-11-18 20:53:44 +00:00
|
|
|
|
const struct function_table_entry *key = keyv;
|
2002-07-11 06:38:57 +00:00
|
|
|
|
return_STRING_N_HASH_1 (key->name, key->len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unsigned long
|
2002-10-14 21:54:04 +00:00
|
|
|
|
function_table_entry_hash_2 (const void *keyv)
|
2002-07-11 06:38:57 +00:00
|
|
|
|
{
|
2006-11-18 20:53:44 +00:00
|
|
|
|
const struct function_table_entry *key = keyv;
|
2002-07-11 06:38:57 +00:00
|
|
|
|
return_STRING_N_HASH_2 (key->name, key->len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2002-10-14 21:54:04 +00:00
|
|
|
|
function_table_entry_hash_cmp (const void *xv, const void *yv)
|
2002-07-11 06:38:57 +00:00
|
|
|
|
{
|
2006-11-18 20:53:44 +00:00
|
|
|
|
const struct function_table_entry *x = xv;
|
|
|
|
|
const struct function_table_entry *y = yv;
|
2002-07-11 06:38:57 +00:00
|
|
|
|
int result = x->len - y->len;
|
|
|
|
|
if (result)
|
|
|
|
|
return result;
|
|
|
|
|
return_STRING_N_COMPARE (x->name, y->name, x->len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct hash_table function_table;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
1992-01-11 11:37:36 +00:00
|
|
|
|
/* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
|
|
|
|
|
each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
|
|
|
|
|
the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
|
|
|
|
|
nonzero, substitutions are done only on matches which are complete
|
2004-09-21 04:00:31 +00:00
|
|
|
|
whitespace-delimited words. */
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1992-01-11 11:37:36 +00:00
|
|
|
|
char *
|
2007-03-20 03:02:26 +00:00
|
|
|
|
subst_expand (char *o, const char *text, const char *subst, const char *replace,
|
2004-09-21 04:00:31 +00:00
|
|
|
|
unsigned int slen, unsigned int rlen, int by_word)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *t = text;
|
|
|
|
|
const char *p;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
2004-09-21 04:00:31 +00:00
|
|
|
|
if (slen == 0 && !by_word)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
|
|
|
|
/* The first occurrence of "" in any string is its end. */
|
2004-09-21 12:07:12 +00:00
|
|
|
|
o = variable_buffer_output (o, t, strlen (t));
|
1992-01-11 11:37:36 +00:00
|
|
|
|
if (rlen > 0)
|
|
|
|
|
o = variable_buffer_output (o, replace, rlen);
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
1994-03-04 05:36:26 +00:00
|
|
|
|
do
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
2004-09-21 04:00:31 +00:00
|
|
|
|
if (by_word && slen == 0)
|
1994-03-04 05:36:26 +00:00
|
|
|
|
/* When matching by words, the empty string should match
|
|
|
|
|
the end of each word, rather than the end of the whole text. */
|
|
|
|
|
p = end_of_token (next_token (t));
|
|
|
|
|
else
|
|
|
|
|
{
|
2004-09-21 12:07:12 +00:00
|
|
|
|
p = strstr (t, subst);
|
1994-03-04 05:36:26 +00:00
|
|
|
|
if (p == 0)
|
|
|
|
|
{
|
|
|
|
|
/* No more matches. Output everything left on the end. */
|
2004-09-21 12:07:12 +00:00
|
|
|
|
o = variable_buffer_output (o, t, strlen (t));
|
1994-03-04 05:36:26 +00:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1992-01-11 11:37:36 +00:00
|
|
|
|
/* Output everything before this occurrence of the string to replace. */
|
|
|
|
|
if (p > t)
|
|
|
|
|
o = variable_buffer_output (o, t, p - t);
|
|
|
|
|
|
|
|
|
|
/* If we're substituting only by fully matched words,
|
|
|
|
|
or only at the ends of words, check that this case qualifies. */
|
2004-09-21 04:00:31 +00:00
|
|
|
|
if (by_word
|
|
|
|
|
&& ((p > text && !isblank ((unsigned char)p[-1]))
|
|
|
|
|
|| (p[slen] != '\0' && !isblank ((unsigned char)p[slen]))))
|
1992-01-11 11:37:36 +00:00
|
|
|
|
/* Struck out. Output the rest of the string that is
|
|
|
|
|
no longer to be replaced. */
|
|
|
|
|
o = variable_buffer_output (o, subst, slen);
|
|
|
|
|
else if (rlen > 0)
|
|
|
|
|
/* Output the replacement string. */
|
|
|
|
|
o = variable_buffer_output (o, replace, rlen);
|
|
|
|
|
|
2004-09-21 12:07:12 +00:00
|
|
|
|
/* Advance T past the string to be replaced. */
|
2007-03-20 03:02:26 +00:00
|
|
|
|
t = p + slen;
|
1994-03-04 05:36:26 +00:00
|
|
|
|
} while (*t != '\0');
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
2004-09-21 04:00:31 +00:00
|
|
|
|
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
|
|
|
|
/* Store into VARIABLE_BUFFER at O the result of scanning TEXT
|
|
|
|
|
and replacing strings matching PATTERN with REPLACE.
|
|
|
|
|
If PATTERN_PERCENT is not nil, PATTERN has already been
|
|
|
|
|
run through find_percent, and PATTERN_PERCENT is the result.
|
|
|
|
|
If REPLACE_PERCENT is not nil, REPLACE has already been
|
2004-09-21 04:00:31 +00:00
|
|
|
|
run through find_percent, and REPLACE_PERCENT is the result.
|
|
|
|
|
Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
|
|
|
|
|
character _AFTER_ the %, not to the % itself.
|
|
|
|
|
*/
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
|
|
|
|
char *
|
2007-03-20 03:02:26 +00:00
|
|
|
|
patsubst_expand_pat (char *o, const char *text,
|
|
|
|
|
const char *pattern, const char *replace,
|
|
|
|
|
const char *pattern_percent, const char *replace_percent)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
1996-05-09 18:28:15 +00:00
|
|
|
|
unsigned int pattern_prepercent_len, pattern_postpercent_len;
|
2004-09-21 04:00:31 +00:00
|
|
|
|
unsigned int replace_prepercent_len, replace_postpercent_len;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *t;
|
2001-05-21 06:16:00 +00:00
|
|
|
|
unsigned int len;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
int doneany = 0;
|
|
|
|
|
|
2004-09-21 04:00:31 +00:00
|
|
|
|
/* Record the length of REPLACE before and after the % so we don't have to
|
|
|
|
|
compute these lengths more than once. */
|
|
|
|
|
if (replace_percent)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
2004-09-21 04:00:31 +00:00
|
|
|
|
replace_prepercent_len = replace_percent - replace - 1;
|
|
|
|
|
replace_postpercent_len = strlen (replace_percent);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2004-09-21 04:00:31 +00:00
|
|
|
|
{
|
|
|
|
|
replace_prepercent_len = strlen (replace);
|
|
|
|
|
replace_postpercent_len = 0;
|
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
2004-09-21 04:00:31 +00:00
|
|
|
|
if (!pattern_percent)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
/* With no % in the pattern, this is just a simple substitution. */
|
|
|
|
|
return subst_expand (o, text, pattern, replace,
|
2004-09-21 04:00:31 +00:00
|
|
|
|
strlen (pattern), strlen (replace), 1);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
|
|
|
|
/* Record the length of PATTERN before and after the %
|
|
|
|
|
so we don't have to compute it more than once. */
|
2004-09-21 04:00:31 +00:00
|
|
|
|
pattern_prepercent_len = pattern_percent - pattern - 1;
|
|
|
|
|
pattern_postpercent_len = strlen (pattern_percent);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1993-01-06 22:57:35 +00:00
|
|
|
|
while ((t = find_next_token (&text, &len)) != 0)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
|
|
|
|
int fail = 0;
|
|
|
|
|
|
|
|
|
|
/* Is it big enough to match? */
|
|
|
|
|
if (len < pattern_prepercent_len + pattern_postpercent_len)
|
|
|
|
|
fail = 1;
|
|
|
|
|
|
1997-09-16 14:17:23 +00:00
|
|
|
|
/* Does the prefix match? */
|
1992-01-11 11:37:36 +00:00
|
|
|
|
if (!fail && pattern_prepercent_len > 0
|
|
|
|
|
&& (*t != *pattern
|
2004-09-21 04:00:31 +00:00
|
|
|
|
|| t[pattern_prepercent_len - 1] != pattern_percent[-2]
|
1999-07-21 05:53:23 +00:00
|
|
|
|
|| !strneq (t + 1, pattern + 1, pattern_prepercent_len - 1)))
|
1992-01-11 11:37:36 +00:00
|
|
|
|
fail = 1;
|
|
|
|
|
|
1997-09-16 14:17:23 +00:00
|
|
|
|
/* Does the suffix match? */
|
1992-01-11 11:37:36 +00:00
|
|
|
|
if (!fail && pattern_postpercent_len > 0
|
2004-09-21 04:00:31 +00:00
|
|
|
|
&& (t[len - 1] != pattern_percent[pattern_postpercent_len - 1]
|
|
|
|
|
|| t[len - pattern_postpercent_len] != *pattern_percent
|
1999-07-21 05:53:23 +00:00
|
|
|
|
|| !strneq (&t[len - pattern_postpercent_len],
|
2004-09-21 04:00:31 +00:00
|
|
|
|
pattern_percent, pattern_postpercent_len - 1)))
|
1992-01-11 11:37:36 +00:00
|
|
|
|
fail = 1;
|
|
|
|
|
|
|
|
|
|
if (fail)
|
|
|
|
|
/* It didn't match. Output the string. */
|
|
|
|
|
o = variable_buffer_output (o, t, len);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* It matched. Output the replacement. */
|
|
|
|
|
|
|
|
|
|
/* Output the part of the replacement before the %. */
|
|
|
|
|
o = variable_buffer_output (o, replace, replace_prepercent_len);
|
|
|
|
|
|
|
|
|
|
if (replace_percent != 0)
|
|
|
|
|
{
|
|
|
|
|
/* Output the part of the matched string that
|
|
|
|
|
matched the % in the pattern. */
|
|
|
|
|
o = variable_buffer_output (o, t + pattern_prepercent_len,
|
|
|
|
|
len - (pattern_prepercent_len
|
|
|
|
|
+ pattern_postpercent_len));
|
|
|
|
|
/* Output the part of the replacement after the %. */
|
2004-09-21 04:00:31 +00:00
|
|
|
|
o = variable_buffer_output (o, replace_percent,
|
1992-01-11 11:37:36 +00:00
|
|
|
|
replace_postpercent_len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Output a space, but not if the replacement is "". */
|
|
|
|
|
if (fail || replace_prepercent_len > 0
|
|
|
|
|
|| (replace_percent != 0 && len + replace_postpercent_len > 0))
|
|
|
|
|
{
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill the last space. */
|
|
|
|
|
--o;
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
2007-03-20 03:02:26 +00:00
|
|
|
|
|
|
|
|
|
/* Store into VARIABLE_BUFFER at O the result of scanning TEXT
|
|
|
|
|
and replacing strings matching PATTERN with REPLACE.
|
|
|
|
|
If PATTERN_PERCENT is not nil, PATTERN has already been
|
|
|
|
|
run through find_percent, and PATTERN_PERCENT is the result.
|
|
|
|
|
If REPLACE_PERCENT is not nil, REPLACE has already been
|
|
|
|
|
run through find_percent, and REPLACE_PERCENT is the result.
|
|
|
|
|
Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
|
|
|
|
|
character _AFTER_ the %, not to the % itself.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
patsubst_expand (char *o, const char *text, char *pattern, char *replace)
|
|
|
|
|
{
|
|
|
|
|
const char *pattern_percent = find_percent (pattern);
|
|
|
|
|
const char *replace_percent = find_percent (replace);
|
|
|
|
|
|
|
|
|
|
/* If there's a percent in the pattern or replacement skip it. */
|
|
|
|
|
if (replace_percent)
|
|
|
|
|
++replace_percent;
|
|
|
|
|
if (pattern_percent)
|
|
|
|
|
++pattern_percent;
|
|
|
|
|
|
|
|
|
|
return patsubst_expand_pat (o, text, pattern, replace,
|
|
|
|
|
pattern_percent, replace_percent);
|
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
/* Look up a function by name. */
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
static const struct function_table_entry *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
lookup_function (const char *s)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2002-07-11 06:38:57 +00:00
|
|
|
|
const char *e = s;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
while (*e && ( (*e >= 'a' && *e <= 'z') || *e == '-'))
|
|
|
|
|
e++;
|
|
|
|
|
if (*e == '\0' || isblank ((unsigned char) *e))
|
|
|
|
|
{
|
|
|
|
|
struct function_table_entry function_table_entry_key;
|
|
|
|
|
function_table_entry_key.name = s;
|
|
|
|
|
function_table_entry_key.len = e - s;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
return hash_find_item (&function_table, &function_table_entry_key);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
1998-07-30 20:54:47 +00:00
|
|
|
|
/* Return 1 if PATTERN matches STR, 0 if not. */
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
|
|
|
|
int
|
2006-11-18 20:53:44 +00:00
|
|
|
|
pattern_matches (const char *pattern, const char *percent, const char *str)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
1998-07-30 20:54:47 +00:00
|
|
|
|
unsigned int sfxlen, strlength;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
|
|
|
|
if (percent == 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned int len = strlen (pattern) + 1;
|
2006-04-09 22:09:24 +00:00
|
|
|
|
char *new_chars = alloca (len);
|
|
|
|
|
memcpy (new_chars, pattern, len);
|
2006-11-18 20:53:44 +00:00
|
|
|
|
percent = find_percent (new_chars);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
if (percent == 0)
|
2006-11-18 20:53:44 +00:00
|
|
|
|
return streq (new_chars, str);
|
|
|
|
|
pattern = new_chars;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
1992-08-19 22:16:15 +00:00
|
|
|
|
sfxlen = strlen (percent + 1);
|
1998-07-30 20:54:47 +00:00
|
|
|
|
strlength = strlen (str);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1998-07-30 20:54:47 +00:00
|
|
|
|
if (strlength < (percent - pattern) + sfxlen
|
1999-07-21 05:53:23 +00:00
|
|
|
|
|| !strneq (pattern, str, percent - pattern))
|
1992-01-11 11:37:36 +00:00
|
|
|
|
return 0;
|
|
|
|
|
|
1998-07-30 20:54:47 +00:00
|
|
|
|
return !strcmp (percent + 1, str + (strlength - sfxlen));
|
1992-01-11 11:37:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* Find the next comma or ENDPAREN (counting nested STARTPAREN and
|
|
|
|
|
ENDPARENtheses), starting at PTR before END. Return a pointer to
|
|
|
|
|
next character.
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
If no next argument is found, return NULL.
|
|
|
|
|
*/
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
|
|
|
|
static char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
find_next_argument (char startparen, char endparen,
|
|
|
|
|
const char *ptr, const char *end)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int count = 0;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
for (; ptr < end; ++ptr)
|
|
|
|
|
if (*ptr == startparen)
|
|
|
|
|
++count;
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
else if (*ptr == endparen)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
1999-03-26 04:04:42 +00:00
|
|
|
|
--count;
|
|
|
|
|
if (count < 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
else if (*ptr == ',' && !count)
|
|
|
|
|
return (char *)ptr;
|
1997-09-16 14:17:23 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* We didn't find anything. */
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-16 14:17:23 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* Glob-expand LINE. The returned pointer is
|
|
|
|
|
only good until the next call to string_glob. */
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
static char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
string_glob (char *line)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
static char *result = 0;
|
|
|
|
|
static unsigned int length;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
struct nameseq *chain;
|
|
|
|
|
unsigned int idx;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
chain = multi_glob (parse_file_seq
|
|
|
|
|
(&line, '\0', sizeof (struct nameseq),
|
|
|
|
|
/* We do not want parse_file_seq to strip `./'s.
|
|
|
|
|
That would break examples like:
|
1999-03-26 07:08:57 +00:00
|
|
|
|
$(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
0),
|
2009-06-13 21:21:48 +00:00
|
|
|
|
sizeof (struct nameseq), 1);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (result == 0)
|
|
|
|
|
{
|
|
|
|
|
length = 100;
|
2006-04-09 22:09:24 +00:00
|
|
|
|
result = xmalloc (100);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
idx = 0;
|
|
|
|
|
while (chain != 0)
|
|
|
|
|
{
|
|
|
|
|
struct nameseq *next = chain->next;
|
2009-06-13 21:21:48 +00:00
|
|
|
|
unsigned int len = strlen (chain->name);
|
|
|
|
|
|
|
|
|
|
if (idx + len + 1 > length)
|
|
|
|
|
{
|
|
|
|
|
length += (len + 1) * 2;
|
|
|
|
|
result = xrealloc (result, length);
|
|
|
|
|
}
|
|
|
|
|
memcpy (&result[idx], chain->name, len);
|
|
|
|
|
idx += len;
|
|
|
|
|
result[idx++] = ' ';
|
|
|
|
|
|
2006-04-09 22:09:24 +00:00
|
|
|
|
free (chain);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
chain = next;
|
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* Kill the last space and terminate the string. */
|
|
|
|
|
if (idx == 0)
|
|
|
|
|
result[0] = '\0';
|
|
|
|
|
else
|
|
|
|
|
result[idx - 1] = '\0';
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Builtin functions
|
|
|
|
|
*/
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_patsubst (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
o = patsubst_expand (o, argv[2], argv[0], argv[1]);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
return o;
|
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_join (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
int doneany = 0;
|
|
|
|
|
|
|
|
|
|
/* Write each word of the first argument directly followed
|
|
|
|
|
by the corresponding word of the second argument.
|
|
|
|
|
If the two arguments have a different number of words,
|
|
|
|
|
the excess words are just output separated by blanks. */
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *tp;
|
|
|
|
|
const char *pp;
|
|
|
|
|
const char *list1_iterator = argv[0];
|
|
|
|
|
const char *list2_iterator = argv[1];
|
1999-03-26 04:04:42 +00:00
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
unsigned int len1, len2;
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
tp = find_next_token (&list1_iterator, &len1);
|
|
|
|
|
if (tp != 0)
|
|
|
|
|
o = variable_buffer_output (o, tp, len1);
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
pp = find_next_token (&list2_iterator, &len2);
|
|
|
|
|
if (pp != 0)
|
|
|
|
|
o = variable_buffer_output (o, pp, len2);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (tp != 0 || pp != 0)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
1999-03-26 04:04:42 +00:00
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
}
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
|
|
|
|
while (tp != 0 || pp != 0);
|
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill the last blank. */
|
|
|
|
|
--o;
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_origin (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
/* Expand the argument. */
|
2007-03-20 03:02:26 +00:00
|
|
|
|
struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (v == 0)
|
|
|
|
|
o = variable_buffer_output (o, "undefined", 9);
|
|
|
|
|
else
|
|
|
|
|
switch (v->origin)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
1999-03-26 04:04:42 +00:00
|
|
|
|
default:
|
|
|
|
|
case o_invalid:
|
|
|
|
|
abort ();
|
|
|
|
|
break;
|
|
|
|
|
case o_default:
|
|
|
|
|
o = variable_buffer_output (o, "default", 7);
|
|
|
|
|
break;
|
|
|
|
|
case o_env:
|
|
|
|
|
o = variable_buffer_output (o, "environment", 11);
|
|
|
|
|
break;
|
|
|
|
|
case o_file:
|
|
|
|
|
o = variable_buffer_output (o, "file", 4);
|
|
|
|
|
break;
|
|
|
|
|
case o_env_override:
|
|
|
|
|
o = variable_buffer_output (o, "environment override", 20);
|
|
|
|
|
break;
|
|
|
|
|
case o_command:
|
|
|
|
|
o = variable_buffer_output (o, "command line", 12);
|
|
|
|
|
break;
|
|
|
|
|
case o_override:
|
|
|
|
|
o = variable_buffer_output (o, "override", 8);
|
|
|
|
|
break;
|
|
|
|
|
case o_automatic:
|
|
|
|
|
o = variable_buffer_output (o, "automatic", 9);
|
|
|
|
|
break;
|
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
return o;
|
|
|
|
|
}
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
2005-11-17 07:27:28 +00:00
|
|
|
|
static char *
|
|
|
|
|
func_flavor (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
2005-11-17 07:27:28 +00:00
|
|
|
|
|
|
|
|
|
if (v == 0)
|
|
|
|
|
o = variable_buffer_output (o, "undefined", 9);
|
|
|
|
|
else
|
|
|
|
|
if (v->recursive)
|
|
|
|
|
o = variable_buffer_output (o, "recursive", 9);
|
|
|
|
|
else
|
|
|
|
|
o = variable_buffer_output (o, "simple", 6);
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#ifdef VMS
|
2002-08-10 01:27:16 +00:00
|
|
|
|
# define IS_PATHSEP(c) ((c) == ']')
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#else
|
2002-08-10 01:27:16 +00:00
|
|
|
|
# ifdef HAVE_DOS_PATHS
|
|
|
|
|
# define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
|
|
|
|
|
# else
|
|
|
|
|
# define IS_PATHSEP(c) ((c) == '/')
|
|
|
|
|
# endif
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#endif
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
static char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
func_notdir_suffix (char *o, char **argv, const char *funcname)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
/* Expand the argument. */
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *list_iterator = argv[0];
|
|
|
|
|
const char *p2;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int doneany =0;
|
2000-02-09 07:02:18 +00:00
|
|
|
|
unsigned int len=0;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2000-02-09 07:02:18 +00:00
|
|
|
|
int is_suffix = streq (funcname, "suffix");
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int is_notdir = !is_suffix;
|
|
|
|
|
while ((p2 = find_next_token (&list_iterator, &len)) != 0)
|
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *p = p2 + len;
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
while (p >= p2 && (!is_suffix || *p != '.'))
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (IS_PATHSEP (*p))
|
1997-09-16 14:17:23 +00:00
|
|
|
|
break;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
--p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (p >= p2)
|
|
|
|
|
{
|
|
|
|
|
if (is_notdir)
|
|
|
|
|
++p;
|
|
|
|
|
else if (*p != '.')
|
|
|
|
|
continue;
|
|
|
|
|
o = variable_buffer_output (o, p, len - (p - p2));
|
|
|
|
|
}
|
2002-08-10 01:27:16 +00:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* Handle the case of "d:foo/bar". */
|
2000-04-05 16:02:55 +00:00
|
|
|
|
else if (streq (funcname, "notdir") && p2[0] && p2[1] == ':')
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
p = p2 + 2;
|
|
|
|
|
o = variable_buffer_output (o, p, len - (p - p2));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
else if (is_notdir)
|
|
|
|
|
o = variable_buffer_output (o, p2, len);
|
|
|
|
|
|
|
|
|
|
if (is_notdir || p >= p2)
|
|
|
|
|
{
|
1992-01-11 11:37:36 +00:00
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
2007-03-20 03:02:26 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill last space. */
|
|
|
|
|
--o;
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
func_basename_dir (char *o, char **argv, const char *funcname)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
/* Expand the argument. */
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *p3 = argv[0];
|
|
|
|
|
const char *p2;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int doneany=0;
|
1999-10-15 07:00:58 +00:00
|
|
|
|
unsigned int len=0;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
|
2000-02-09 07:02:18 +00:00
|
|
|
|
int is_basename= streq (funcname, "basename");
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int is_dir= !is_basename;
|
|
|
|
|
|
|
|
|
|
while ((p2 = find_next_token (&p3, &len)) != 0)
|
2007-03-20 03:02:26 +00:00
|
|
|
|
{
|
|
|
|
|
const char *p = p2 + len;
|
|
|
|
|
while (p >= p2 && (!is_basename || *p != '.'))
|
|
|
|
|
{
|
|
|
|
|
if (IS_PATHSEP (*p))
|
|
|
|
|
break;
|
|
|
|
|
--p;
|
|
|
|
|
}
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
if (p >= p2 && (is_dir))
|
|
|
|
|
o = variable_buffer_output (o, p2, ++p - p2);
|
|
|
|
|
else if (p >= p2 && (*p == '.'))
|
|
|
|
|
o = variable_buffer_output (o, p2, p - p2);
|
2002-08-10 01:27:16 +00:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
2007-03-20 03:02:26 +00:00
|
|
|
|
/* Handle the "d:foobar" case */
|
|
|
|
|
else if (p2[0] && p2[1] == ':' && is_dir)
|
|
|
|
|
o = variable_buffer_output (o, p2, 2);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#endif
|
2007-03-20 03:02:26 +00:00
|
|
|
|
else if (is_dir)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#ifdef VMS
|
2007-03-20 03:02:26 +00:00
|
|
|
|
o = variable_buffer_output (o, "[]", 2);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#else
|
|
|
|
|
#ifndef _AMIGA
|
2007-03-20 03:02:26 +00:00
|
|
|
|
o = variable_buffer_output (o, "./", 2);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#else
|
2007-03-20 03:02:26 +00:00
|
|
|
|
; /* Just a nop... */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#endif /* AMIGA */
|
|
|
|
|
#endif /* !VMS */
|
2007-03-20 03:02:26 +00:00
|
|
|
|
else
|
|
|
|
|
/* The entire name is the basename. */
|
|
|
|
|
o = variable_buffer_output (o, p2, len);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill last space. */
|
|
|
|
|
--o;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
return o;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
func_addsuffix_addprefix (char *o, char **argv, const char *funcname)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
int fixlen = strlen (argv[0]);
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *list_iterator = argv[1];
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int is_addprefix = streq (funcname, "addprefix");
|
|
|
|
|
int is_addsuffix = !is_addprefix;
|
|
|
|
|
|
1999-10-15 07:00:58 +00:00
|
|
|
|
int doneany = 0;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *p;
|
1999-10-15 07:00:58 +00:00
|
|
|
|
unsigned int len;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
while ((p = find_next_token (&list_iterator, &len)) != 0)
|
|
|
|
|
{
|
|
|
|
|
if (is_addprefix)
|
|
|
|
|
o = variable_buffer_output (o, argv[0], fixlen);
|
|
|
|
|
o = variable_buffer_output (o, p, len);
|
|
|
|
|
if (is_addsuffix)
|
|
|
|
|
o = variable_buffer_output (o, argv[0], fixlen);
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill last space. */
|
|
|
|
|
--o;
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_subst (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
o = subst_expand (o, argv[2], argv[0], argv[1], strlen (argv[0]),
|
2004-09-21 04:00:31 +00:00
|
|
|
|
strlen (argv[1]), 0);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_firstword (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
1999-10-15 07:00:58 +00:00
|
|
|
|
unsigned int i;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *words = argv[0]; /* Use a temp variable for find_next_token */
|
|
|
|
|
const char *p = find_next_token (&words, &i);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
if (p != 0)
|
|
|
|
|
o = variable_buffer_output (o, p, i);
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-21 17:42:24 +00:00
|
|
|
|
static char *
|
|
|
|
|
func_lastword (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
|
|
|
|
unsigned int i;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *words = argv[0]; /* Use a temp variable for find_next_token */
|
2007-05-11 20:57:21 +00:00
|
|
|
|
const char *p = NULL;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *t;
|
2004-10-21 17:42:24 +00:00
|
|
|
|
|
|
|
|
|
while ((t = find_next_token (&words, &i)))
|
|
|
|
|
p = t;
|
|
|
|
|
|
|
|
|
|
if (p != 0)
|
|
|
|
|
o = variable_buffer_output (o, p, i);
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_words (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *word_iterator = argv[0];
|
1999-03-26 04:04:42 +00:00
|
|
|
|
char buf[20];
|
|
|
|
|
|
|
|
|
|
while (find_next_token (&word_iterator, (unsigned int *) 0) != 0)
|
|
|
|
|
++i;
|
|
|
|
|
|
|
|
|
|
sprintf (buf, "%d", i);
|
|
|
|
|
o = variable_buffer_output (o, buf, strlen (buf));
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-04 07:40:29 +00:00
|
|
|
|
/* Set begpp to point to the first non-whitespace character of the string,
|
|
|
|
|
* and endpp to point to the last non-whitespace character of the string.
|
|
|
|
|
* If the string is empty or contains nothing but whitespace, endpp will be
|
|
|
|
|
* begpp-1.
|
|
|
|
|
*/
|
2005-02-27 21:40:23 +00:00
|
|
|
|
char *
|
2003-01-30 06:21:36 +00:00
|
|
|
|
strip_whitespace (const char **begpp, const char **endpp)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2003-05-02 01:44:59 +00:00
|
|
|
|
while (*begpp <= *endpp && isspace ((unsigned char)**begpp))
|
1999-03-26 04:04:42 +00:00
|
|
|
|
(*begpp) ++;
|
2003-05-02 01:44:59 +00:00
|
|
|
|
while (*endpp >= *begpp && isspace ((unsigned char)**endpp))
|
1999-03-26 04:04:42 +00:00
|
|
|
|
(*endpp) --;
|
2003-01-30 06:21:36 +00:00
|
|
|
|
return (char *)*begpp;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-30 06:21:36 +00:00
|
|
|
|
static void
|
2006-04-09 22:09:24 +00:00
|
|
|
|
check_numeric (const char *s, const char *msg)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2003-01-30 06:21:36 +00:00
|
|
|
|
const char *end = s + strlen (s) - 1;
|
|
|
|
|
const char *beg = s;
|
|
|
|
|
strip_whitespace (&s, &end);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2003-01-30 06:21:36 +00:00
|
|
|
|
for (; s <= end; ++s)
|
|
|
|
|
if (!ISDIGIT (*s)) /* ISDIGIT only evals its arg once: see make.h. */
|
|
|
|
|
break;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2003-01-30 06:21:36 +00:00
|
|
|
|
if (s <= end || end - beg < 0)
|
2006-04-09 22:09:24 +00:00
|
|
|
|
fatal (*expanding_var, "%s: '%s'", msg, beg);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_word (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *end_p;
|
|
|
|
|
const char *p;
|
|
|
|
|
int i;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
/* Check the first argument. */
|
1999-07-28 06:23:37 +00:00
|
|
|
|
check_numeric (argv[0], _("non-numeric first argument to `word' function"));
|
2007-03-20 03:02:26 +00:00
|
|
|
|
i = atoi (argv[0]);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
if (i == 0)
|
2006-02-15 23:54:42 +00:00
|
|
|
|
fatal (*expanding_var,
|
|
|
|
|
_("first argument to `word' function must be greater than 0"));
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
end_p = argv[1];
|
|
|
|
|
while ((p = find_next_token (&end_p, 0)) != 0)
|
|
|
|
|
if (--i == 0)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
break;
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (i == 0)
|
|
|
|
|
o = variable_buffer_output (o, p, end_p - p);
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_wordlist (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2000-04-05 16:02:55 +00:00
|
|
|
|
int start, count;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2000-04-05 16:02:55 +00:00
|
|
|
|
/* Check the arguments. */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
check_numeric (argv[0],
|
1999-07-28 06:23:37 +00:00
|
|
|
|
_("non-numeric first argument to `wordlist' function"));
|
1999-03-26 04:04:42 +00:00
|
|
|
|
check_numeric (argv[1],
|
1999-07-28 06:23:37 +00:00
|
|
|
|
_("non-numeric second argument to `wordlist' function"));
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2000-04-05 16:02:55 +00:00
|
|
|
|
start = atoi (argv[0]);
|
2005-02-28 09:41:25 +00:00
|
|
|
|
if (start < 1)
|
2006-02-15 23:54:42 +00:00
|
|
|
|
fatal (*expanding_var,
|
2005-03-04 12:52:32 +00:00
|
|
|
|
"invalid first argument to `wordlist' function: `%d'", start);
|
2005-02-28 09:41:25 +00:00
|
|
|
|
|
2000-04-05 16:02:55 +00:00
|
|
|
|
count = atoi (argv[1]) - start + 1;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2000-04-05 16:02:55 +00:00
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *p;
|
|
|
|
|
const char *end_p = argv[2];
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2000-04-05 16:02:55 +00:00
|
|
|
|
/* Find the beginning of the "start"th word. */
|
|
|
|
|
while (((p = find_next_token (&end_p, 0)) != 0) && --start)
|
|
|
|
|
;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2000-04-05 16:02:55 +00:00
|
|
|
|
if (p)
|
|
|
|
|
{
|
|
|
|
|
/* Find the end of the "count"th word from start. */
|
|
|
|
|
while (--count && (find_next_token (&end_p, 0) != 0))
|
|
|
|
|
;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2000-04-05 16:02:55 +00:00
|
|
|
|
/* Return the stuff in the middle. */
|
|
|
|
|
o = variable_buffer_output (o, p, end_p - p);
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_findstring (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
/* Find the first occurrence of the first string in the second. */
|
2004-09-21 12:07:12 +00:00
|
|
|
|
if (strstr (argv[1], argv[0]) != 0)
|
|
|
|
|
o = variable_buffer_output (o, argv[0], strlen (argv[0]));
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_foreach (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
/* expand only the first two. */
|
2000-01-11 07:31:42 +00:00
|
|
|
|
char *varname = expand_argument (argv[0], NULL);
|
|
|
|
|
char *list = expand_argument (argv[1], NULL);
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *body = argv[2];
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
1999-10-15 07:00:58 +00:00
|
|
|
|
int doneany = 0;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *list_iterator = list;
|
|
|
|
|
const char *p;
|
1999-10-15 07:00:58 +00:00
|
|
|
|
unsigned int len;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
struct variable *var;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
push_new_variable_scope ();
|
|
|
|
|
var = define_variable (varname, strlen (varname), "", o_automatic, 0);
|
|
|
|
|
|
|
|
|
|
/* loop through LIST, put the value in VAR and expand BODY */
|
|
|
|
|
while ((p = find_next_token (&list_iterator, &len)) != 0)
|
|
|
|
|
{
|
|
|
|
|
char *result = 0;
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
free (var->value);
|
2009-05-24 18:31:18 +00:00
|
|
|
|
var->value = xstrndup (p, len);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
result = allocated_variable_expand (body);
|
|
|
|
|
|
|
|
|
|
o = variable_buffer_output (o, result, strlen (result));
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
free (result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill the last space. */
|
|
|
|
|
--o;
|
|
|
|
|
|
|
|
|
|
pop_variable_scope ();
|
|
|
|
|
free (varname);
|
|
|
|
|
free (list);
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct a_word
|
|
|
|
|
{
|
|
|
|
|
struct a_word *next;
|
2002-07-11 06:38:57 +00:00
|
|
|
|
struct a_word *chain;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
char *str;
|
2002-07-11 06:38:57 +00:00
|
|
|
|
int length;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int matched;
|
|
|
|
|
};
|
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
static unsigned long
|
2002-10-14 21:54:04 +00:00
|
|
|
|
a_word_hash_1 (const void *key)
|
2002-07-11 06:38:57 +00:00
|
|
|
|
{
|
|
|
|
|
return_STRING_HASH_1 (((struct a_word const *) key)->str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unsigned long
|
2002-10-14 21:54:04 +00:00
|
|
|
|
a_word_hash_2 (const void *key)
|
2002-07-11 06:38:57 +00:00
|
|
|
|
{
|
|
|
|
|
return_STRING_HASH_2 (((struct a_word const *) key)->str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2002-10-14 21:54:04 +00:00
|
|
|
|
a_word_hash_cmp (const void *x, const void *y)
|
2002-07-11 06:38:57 +00:00
|
|
|
|
{
|
|
|
|
|
int result = ((struct a_word const *) x)->length - ((struct a_word const *) y)->length;
|
|
|
|
|
if (result)
|
|
|
|
|
return result;
|
|
|
|
|
return_STRING_COMPARE (((struct a_word const *) x)->str,
|
|
|
|
|
((struct a_word const *) y)->str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct a_pattern
|
|
|
|
|
{
|
|
|
|
|
struct a_pattern *next;
|
|
|
|
|
char *str;
|
|
|
|
|
char *percent;
|
|
|
|
|
int length;
|
|
|
|
|
int save_c;
|
|
|
|
|
};
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
static char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
func_filter_filterout (char *o, char **argv, const char *funcname)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2002-07-11 06:38:57 +00:00
|
|
|
|
struct a_word *wordhead;
|
|
|
|
|
struct a_word **wordtail;
|
|
|
|
|
struct a_word *wp;
|
|
|
|
|
struct a_pattern *pathead;
|
|
|
|
|
struct a_pattern **pattail;
|
|
|
|
|
struct a_pattern *pp;
|
2002-08-10 01:27:16 +00:00
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
struct hash_table a_word_table;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int is_filter = streq (funcname, "filter");
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *pat_iterator = argv[0];
|
|
|
|
|
const char *word_iterator = argv[1];
|
2002-07-11 06:38:57 +00:00
|
|
|
|
int literals = 0;
|
|
|
|
|
int words = 0;
|
|
|
|
|
int hashing = 0;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
char *p;
|
1999-10-15 07:00:58 +00:00
|
|
|
|
unsigned int len;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
/* Chop ARGV[0] up into patterns to match against the words. */
|
|
|
|
|
|
|
|
|
|
pattail = &pathead;
|
|
|
|
|
while ((p = find_next_token (&pat_iterator, &len)) != 0)
|
|
|
|
|
{
|
2006-04-09 22:09:24 +00:00
|
|
|
|
struct a_pattern *pat = alloca (sizeof (struct a_pattern));
|
2002-07-11 06:38:57 +00:00
|
|
|
|
|
|
|
|
|
*pattail = pat;
|
|
|
|
|
pattail = &pat->next;
|
|
|
|
|
|
|
|
|
|
if (*pat_iterator != '\0')
|
|
|
|
|
++pat_iterator;
|
|
|
|
|
|
|
|
|
|
pat->str = p;
|
|
|
|
|
pat->length = len;
|
|
|
|
|
pat->save_c = p[len];
|
|
|
|
|
p[len] = '\0';
|
|
|
|
|
pat->percent = find_percent (p);
|
|
|
|
|
if (pat->percent == 0)
|
|
|
|
|
literals++;
|
|
|
|
|
}
|
|
|
|
|
*pattail = 0;
|
|
|
|
|
|
|
|
|
|
/* Chop ARGV[1] up into words to match against the patterns. */
|
|
|
|
|
|
|
|
|
|
wordtail = &wordhead;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
while ((p = find_next_token (&word_iterator, &len)) != 0)
|
|
|
|
|
{
|
2006-04-09 22:09:24 +00:00
|
|
|
|
struct a_word *word = alloca (sizeof (struct a_word));
|
2002-07-11 06:38:57 +00:00
|
|
|
|
|
|
|
|
|
*wordtail = word;
|
|
|
|
|
wordtail = &word->next;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
if (*word_iterator != '\0')
|
|
|
|
|
++word_iterator;
|
2002-07-11 06:38:57 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
p[len] = '\0';
|
2002-07-11 06:38:57 +00:00
|
|
|
|
word->str = p;
|
|
|
|
|
word->length = len;
|
|
|
|
|
word->matched = 0;
|
|
|
|
|
word->chain = 0;
|
|
|
|
|
words++;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
2002-07-11 06:38:57 +00:00
|
|
|
|
*wordtail = 0;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
/* Only use a hash table if arg list lengths justifies the cost. */
|
|
|
|
|
hashing = (literals >= 2 && (literals * words) >= 10);
|
|
|
|
|
if (hashing)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
hash_init (&a_word_table, words, a_word_hash_1, a_word_hash_2,
|
|
|
|
|
a_word_hash_cmp);
|
2002-07-11 06:38:57 +00:00
|
|
|
|
for (wp = wordhead; wp != 0; wp = wp->next)
|
|
|
|
|
{
|
|
|
|
|
struct a_word *owp = hash_insert (&a_word_table, wp);
|
|
|
|
|
if (owp)
|
|
|
|
|
wp->chain = owp;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
if (words)
|
|
|
|
|
{
|
|
|
|
|
int doneany = 0;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
/* Run each pattern through the words, killing words. */
|
2002-07-11 06:38:57 +00:00
|
|
|
|
for (pp = pathead; pp != 0; pp = pp->next)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2002-07-11 06:38:57 +00:00
|
|
|
|
if (pp->percent)
|
|
|
|
|
for (wp = wordhead; wp != 0; wp = wp->next)
|
|
|
|
|
wp->matched |= pattern_matches (pp->str, pp->percent, wp->str);
|
|
|
|
|
else if (hashing)
|
|
|
|
|
{
|
|
|
|
|
struct a_word a_word_key;
|
|
|
|
|
a_word_key.str = pp->str;
|
|
|
|
|
a_word_key.length = pp->length;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
wp = hash_find_item (&a_word_table, &a_word_key);
|
2002-07-11 06:38:57 +00:00
|
|
|
|
while (wp)
|
|
|
|
|
{
|
|
|
|
|
wp->matched |= 1;
|
|
|
|
|
wp = wp->chain;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
for (wp = wordhead; wp != 0; wp = wp->next)
|
|
|
|
|
wp->matched |= (wp->length == pp->length
|
|
|
|
|
&& strneq (pp->str, wp->str, wp->length));
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Output the words that matched (or didn't, for filter-out). */
|
|
|
|
|
for (wp = wordhead; wp != 0; wp = wp->next)
|
|
|
|
|
if (is_filter ? wp->matched : !wp->matched)
|
|
|
|
|
{
|
|
|
|
|
o = variable_buffer_output (o, wp->str, strlen (wp->str));
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill the last space. */
|
|
|
|
|
--o;
|
|
|
|
|
}
|
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
for (pp = pathead; pp != 0; pp = pp->next)
|
|
|
|
|
pp->str[pp->length] = pp->save_c;
|
|
|
|
|
|
|
|
|
|
if (hashing)
|
|
|
|
|
hash_free (&a_word_table, 0);
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_strip (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *p = argv[0];
|
|
|
|
|
int doneany = 0;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
while (*p != '\0')
|
|
|
|
|
{
|
|
|
|
|
int i=0;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *word_start;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
1999-10-15 07:00:58 +00:00
|
|
|
|
while (isspace ((unsigned char)*p))
|
1999-03-26 04:04:42 +00:00
|
|
|
|
++p;
|
|
|
|
|
word_start = p;
|
1999-10-15 07:00:58 +00:00
|
|
|
|
for (i=0; *p != '\0' && !isspace ((unsigned char)*p); ++p, ++i)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{}
|
|
|
|
|
if (!i)
|
|
|
|
|
break;
|
|
|
|
|
o = variable_buffer_output (o, word_start, i);
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill the last space. */
|
|
|
|
|
--o;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Print a warning or fatal message.
|
|
|
|
|
*/
|
|
|
|
|
static char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
func_error (char *o, char **argv, const char *funcname)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
char **argvp;
|
|
|
|
|
char *msg, *p;
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
/* The arguments will be broken on commas. Rather than create yet
|
|
|
|
|
another special case where function arguments aren't broken up,
|
|
|
|
|
just create a format string that puts them back together. */
|
|
|
|
|
for (len=0, argvp=argv; *argvp != 0; ++argvp)
|
2000-04-05 16:02:55 +00:00
|
|
|
|
len += strlen (*argvp) + 2;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2006-04-09 22:09:24 +00:00
|
|
|
|
p = msg = alloca (len + 1);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
for (argvp=argv; argvp[1] != 0; ++argvp)
|
|
|
|
|
{
|
2000-04-05 16:02:55 +00:00
|
|
|
|
strcpy (p, *argvp);
|
|
|
|
|
p += strlen (*argvp);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
*(p++) = ',';
|
|
|
|
|
*(p++) = ' ';
|
|
|
|
|
}
|
2000-04-05 16:02:55 +00:00
|
|
|
|
strcpy (p, *argvp);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2005-02-28 07:48:22 +00:00
|
|
|
|
switch (*funcname) {
|
|
|
|
|
case 'e':
|
|
|
|
|
fatal (reading_file, "%s", msg);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2005-02-28 07:48:22 +00:00
|
|
|
|
case 'w':
|
|
|
|
|
error (reading_file, "%s", msg);
|
|
|
|
|
break;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2005-02-28 07:48:22 +00:00
|
|
|
|
case 'i':
|
|
|
|
|
printf ("%s\n", msg);
|
2005-08-29 14:11:00 +00:00
|
|
|
|
fflush(stdout);
|
2005-02-28 07:48:22 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2006-02-15 23:54:42 +00:00
|
|
|
|
fatal (*expanding_var, "Internal error: func_error: '%s'", funcname);
|
2005-02-28 07:48:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The warning function expands to the empty string. */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
chop argv[0] into words, and sort them.
|
|
|
|
|
*/
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_sort (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *t;
|
|
|
|
|
char **words;
|
|
|
|
|
int wordi;
|
1999-10-15 07:00:58 +00:00
|
|
|
|
char *p;
|
|
|
|
|
unsigned int len;
|
1999-07-21 05:53:23 +00:00
|
|
|
|
int i;
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
/* Find the maximum number of words we'll have. */
|
|
|
|
|
t = argv[0];
|
|
|
|
|
wordi = 1;
|
|
|
|
|
while (*t != '\0')
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
char c = *(t++);
|
|
|
|
|
|
|
|
|
|
if (! isspace ((unsigned char)c))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
++wordi;
|
|
|
|
|
|
|
|
|
|
while (isspace ((unsigned char)*t))
|
|
|
|
|
++t;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
words = xmalloc (wordi * sizeof (char *));
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
/* Now assign pointers to each string in the array. */
|
|
|
|
|
t = argv[0];
|
|
|
|
|
wordi = 0;
|
|
|
|
|
while ((p = find_next_token (&t, &len)) != 0)
|
|
|
|
|
{
|
|
|
|
|
++t;
|
|
|
|
|
p[len] = '\0';
|
|
|
|
|
words[wordi++] = p;
|
|
|
|
|
}
|
1999-07-21 05:53:23 +00:00
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
if (wordi)
|
1999-07-21 05:53:23 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
/* Now sort the list of words. */
|
|
|
|
|
qsort (words, wordi, sizeof (char *), alpha_compare);
|
|
|
|
|
|
|
|
|
|
/* Now write the sorted list, uniquified. */
|
|
|
|
|
for (i = 0; i < wordi; ++i)
|
1999-07-21 05:53:23 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
len = strlen (words[i]);
|
|
|
|
|
if (i == wordi - 1 || strlen (words[i + 1]) != len
|
|
|
|
|
|| strcmp (words[i], words[i + 1]))
|
|
|
|
|
{
|
|
|
|
|
o = variable_buffer_output (o, words[i], len);
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
}
|
1999-07-21 05:53:23 +00:00
|
|
|
|
}
|
2007-03-20 03:02:26 +00:00
|
|
|
|
|
|
|
|
|
/* Kill the last space. */
|
|
|
|
|
--o;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
1999-07-21 05:53:23 +00:00
|
|
|
|
|
|
|
|
|
free (words);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
1999-08-13 07:36:26 +00:00
|
|
|
|
/*
|
|
|
|
|
$(if condition,true-part[,false-part])
|
|
|
|
|
|
|
|
|
|
CONDITION is false iff it evaluates to an empty string. White
|
|
|
|
|
space before and after condition are stripped before evaluation.
|
|
|
|
|
|
|
|
|
|
If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is
|
|
|
|
|
evaluated (if it exists). Because only one of the two PARTs is evaluated,
|
|
|
|
|
you can use $(if ...) to create side-effects (with $(shell ...), for
|
|
|
|
|
example).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_if (char *o, char **argv, const char *funcname UNUSED)
|
1999-08-13 07:36:26 +00:00
|
|
|
|
{
|
2003-01-30 06:21:36 +00:00
|
|
|
|
const char *begp = argv[0];
|
2003-11-04 07:40:29 +00:00
|
|
|
|
const char *endp = begp + strlen (argv[0]) - 1;
|
1999-08-13 07:36:26 +00:00
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
|
|
/* Find the result of the condition: if we have a value, and it's not
|
|
|
|
|
empty, the condition is true. If we don't have a value, or it's the
|
|
|
|
|
empty string, then it's false. */
|
|
|
|
|
|
|
|
|
|
strip_whitespace (&begp, &endp);
|
|
|
|
|
|
2003-11-04 07:40:29 +00:00
|
|
|
|
if (begp <= endp)
|
1999-08-13 07:36:26 +00:00
|
|
|
|
{
|
2003-11-04 07:40:29 +00:00
|
|
|
|
char *expansion = expand_argument (begp, endp+1);
|
1999-08-13 07:36:26 +00:00
|
|
|
|
|
|
|
|
|
result = strlen (expansion);
|
|
|
|
|
free (expansion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If the result is true (1) we want to eval the first argument, and if
|
|
|
|
|
it's false (0) we want to eval the second. If the argument doesn't
|
|
|
|
|
exist we do nothing, otherwise expand it and add to the buffer. */
|
|
|
|
|
|
|
|
|
|
argv += 1 + !result;
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
if (*argv)
|
1999-08-13 07:36:26 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
char *expansion = expand_argument (*argv, NULL);
|
1999-08-13 07:36:26 +00:00
|
|
|
|
|
|
|
|
|
o = variable_buffer_output (o, expansion, strlen (expansion));
|
2000-01-11 07:31:42 +00:00
|
|
|
|
|
1999-08-13 07:36:26 +00:00
|
|
|
|
free (expansion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-10 05:29:00 +00:00
|
|
|
|
/*
|
|
|
|
|
$(or condition1[,condition2[,condition3[...]]])
|
|
|
|
|
|
|
|
|
|
A CONDITION is false iff it evaluates to an empty string. White
|
|
|
|
|
space before and after CONDITION are stripped before evaluation.
|
|
|
|
|
|
|
|
|
|
CONDITION1 is evaluated. If it's true, then this is the result of
|
|
|
|
|
expansion. If it's false, CONDITION2 is evaluated, and so on. If none of
|
|
|
|
|
the conditions are true, the expansion is the empty string.
|
|
|
|
|
|
|
|
|
|
Once a CONDITION is true no further conditions are evaluated
|
|
|
|
|
(short-circuiting).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
func_or (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
|
|
|
|
for ( ; *argv ; ++argv)
|
|
|
|
|
{
|
|
|
|
|
const char *begp = *argv;
|
|
|
|
|
const char *endp = begp + strlen (*argv) - 1;
|
|
|
|
|
char *expansion;
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
|
|
/* Find the result of the condition: if it's false keep going. */
|
|
|
|
|
|
|
|
|
|
strip_whitespace (&begp, &endp);
|
|
|
|
|
|
|
|
|
|
if (begp > endp)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
expansion = expand_argument (begp, endp+1);
|
|
|
|
|
result = strlen (expansion);
|
|
|
|
|
|
|
|
|
|
/* If the result is false keep going. */
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
free (expansion);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* It's true! Keep this result and return. */
|
|
|
|
|
o = variable_buffer_output (o, expansion, result);
|
|
|
|
|
free (expansion);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
$(and condition1[,condition2[,condition3[...]]])
|
|
|
|
|
|
|
|
|
|
A CONDITION is false iff it evaluates to an empty string. White
|
|
|
|
|
space before and after CONDITION are stripped before evaluation.
|
|
|
|
|
|
|
|
|
|
CONDITION1 is evaluated. If it's false, then this is the result of
|
|
|
|
|
expansion. If it's true, CONDITION2 is evaluated, and so on. If all of
|
|
|
|
|
the conditions are true, the expansion is the result of the last condition.
|
|
|
|
|
|
|
|
|
|
Once a CONDITION is false no further conditions are evaluated
|
|
|
|
|
(short-circuiting).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
func_and (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
|
|
|
|
char *expansion;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
const char *begp = *argv;
|
|
|
|
|
const char *endp = begp + strlen (*argv) - 1;
|
|
|
|
|
|
|
|
|
|
/* An empty condition is always false. */
|
|
|
|
|
strip_whitespace (&begp, &endp);
|
|
|
|
|
if (begp > endp)
|
|
|
|
|
return o;
|
|
|
|
|
|
|
|
|
|
expansion = expand_argument (begp, endp+1);
|
|
|
|
|
result = strlen (expansion);
|
|
|
|
|
|
|
|
|
|
/* If the result is false, stop here: we're done. */
|
|
|
|
|
if (!result)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* Otherwise the result is true. If this is the last one, keep this
|
|
|
|
|
result and quit. Otherwise go on to the next one! */
|
|
|
|
|
|
|
|
|
|
if (*(++argv))
|
|
|
|
|
free (expansion);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
o = variable_buffer_output (o, expansion, result);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free (expansion);
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_wildcard (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
1996-05-13 18:39:52 +00:00
|
|
|
|
#ifdef _AMIGA
|
1999-03-26 04:04:42 +00:00
|
|
|
|
o = wildcard_expansion (argv[0], o);
|
1996-05-13 18:39:52 +00:00
|
|
|
|
#else
|
1999-03-26 04:04:42 +00:00
|
|
|
|
char *p = string_glob (argv[0]);
|
|
|
|
|
o = variable_buffer_output (o, p, strlen (p));
|
1996-05-13 18:39:52 +00:00
|
|
|
|
#endif
|
1999-03-26 04:04:42 +00:00
|
|
|
|
return o;
|
|
|
|
|
}
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
2002-07-08 02:26:47 +00:00
|
|
|
|
/*
|
|
|
|
|
$(eval <makefile string>)
|
|
|
|
|
|
|
|
|
|
Always resolves to the empty string.
|
|
|
|
|
|
|
|
|
|
Treat the arguments as a segment of makefile, and parse them.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_eval (char *o, char **argv, const char *funcname UNUSED)
|
2002-07-08 02:26:47 +00:00
|
|
|
|
{
|
2002-10-25 22:01:47 +00:00
|
|
|
|
char *buf;
|
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
|
|
/* Eval the buffer. Pop the current variable buffer setting so that the
|
|
|
|
|
eval'd code can use its own without conflicting. */
|
|
|
|
|
|
|
|
|
|
install_variable_buffer (&buf, &len);
|
|
|
|
|
|
2002-07-08 02:26:47 +00:00
|
|
|
|
eval_buffer (argv[0]);
|
|
|
|
|
|
2002-10-25 22:01:47 +00:00
|
|
|
|
restore_variable_buffer (buf, len);
|
|
|
|
|
|
2002-07-08 02:26:47 +00:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_value (char *o, char **argv, const char *funcname UNUSED)
|
2002-07-08 02:26:47 +00:00
|
|
|
|
{
|
|
|
|
|
/* Look up the variable. */
|
|
|
|
|
struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
|
|
|
|
|
|
|
|
|
/* Copy its value into the output buffer without expanding it. */
|
|
|
|
|
if (v)
|
|
|
|
|
o = variable_buffer_output (o, v->value, strlen(v->value));
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/*
|
|
|
|
|
\r is replaced on UNIX as well. Is this desirable?
|
|
|
|
|
*/
|
2006-04-01 06:36:40 +00:00
|
|
|
|
static void
|
|
|
|
|
fold_newlines (char *buffer, unsigned int *length)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
char *dst = buffer;
|
|
|
|
|
char *src = buffer;
|
|
|
|
|
char *last_nonnl = buffer -1;
|
|
|
|
|
src[*length] = 0;
|
|
|
|
|
for (; *src != '\0'; ++src)
|
|
|
|
|
{
|
|
|
|
|
if (src[0] == '\r' && src[1] == '\n')
|
|
|
|
|
continue;
|
|
|
|
|
if (*src == '\n')
|
|
|
|
|
{
|
|
|
|
|
*dst++ = ' ';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
last_nonnl = dst;
|
|
|
|
|
*dst++ = *src;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*(++last_nonnl) = '\0';
|
|
|
|
|
*length = last_nonnl - buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int shell_function_pid = 0, shell_function_completed;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
/*untested*/
|
|
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#include <io.h>
|
|
|
|
|
#include "sub_proc.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
windows32_openpipe (int *pipedes, int *pid_p, char **command_argv, char **envp)
|
|
|
|
|
{
|
|
|
|
|
SECURITY_ATTRIBUTES saAttr;
|
|
|
|
|
HANDLE hIn;
|
|
|
|
|
HANDLE hErr;
|
|
|
|
|
HANDLE hChildOutRd;
|
|
|
|
|
HANDLE hChildOutWr;
|
|
|
|
|
HANDLE hProcess;
|
|
|
|
|
|
|
|
|
|
|
2000-04-05 16:02:55 +00:00
|
|
|
|
saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
saAttr.bInheritHandle = TRUE;
|
|
|
|
|
saAttr.lpSecurityDescriptor = NULL;
|
|
|
|
|
|
2000-04-05 16:02:55 +00:00
|
|
|
|
if (DuplicateHandle (GetCurrentProcess(),
|
1999-03-26 04:04:42 +00:00
|
|
|
|
GetStdHandle(STD_INPUT_HANDLE),
|
|
|
|
|
GetCurrentProcess(),
|
|
|
|
|
&hIn,
|
|
|
|
|
0,
|
|
|
|
|
TRUE,
|
|
|
|
|
DUPLICATE_SAME_ACCESS) == FALSE) {
|
2009-06-09 15:35:38 +00:00
|
|
|
|
fatal (NILF, _("windows32_openpipe(): DuplicateHandle(In) failed (e=%ld)\n"),
|
1999-03-26 04:04:42 +00:00
|
|
|
|
GetLastError());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if (DuplicateHandle(GetCurrentProcess(),
|
|
|
|
|
GetStdHandle(STD_ERROR_HANDLE),
|
|
|
|
|
GetCurrentProcess(),
|
|
|
|
|
&hErr,
|
|
|
|
|
0,
|
|
|
|
|
TRUE,
|
|
|
|
|
DUPLICATE_SAME_ACCESS) == FALSE) {
|
2009-06-09 15:35:38 +00:00
|
|
|
|
fatal (NILF, _("windows32_open_pipe(): DuplicateHandle(Err) failed (e=%ld)\n"),
|
1999-03-26 04:04:42 +00:00
|
|
|
|
GetLastError());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!CreatePipe(&hChildOutRd, &hChildOutWr, &saAttr, 0))
|
2005-08-08 05:08:00 +00:00
|
|
|
|
fatal (NILF, _("CreatePipe() failed (e=%ld)\n"), GetLastError());
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
hProcess = process_init_fd(hIn, hChildOutWr, hErr);
|
|
|
|
|
|
|
|
|
|
if (!hProcess)
|
2009-06-09 15:35:38 +00:00
|
|
|
|
fatal (NILF, _("windows32_openpipe(): process_init_fd() failed\n"));
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
/* make sure that CreateProcess() has Path it needs */
|
|
|
|
|
sync_Path_environment();
|
|
|
|
|
|
1999-12-08 20:13:50 +00:00
|
|
|
|
if (!process_begin(hProcess, command_argv, envp, command_argv[0], NULL)) {
|
|
|
|
|
/* register process for wait */
|
|
|
|
|
process_register(hProcess);
|
|
|
|
|
|
|
|
|
|
/* set the pid for returning to caller */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
*pid_p = (int) hProcess;
|
|
|
|
|
|
|
|
|
|
/* set up to read data from child */
|
|
|
|
|
pipedes[0] = _open_osfhandle((long) hChildOutRd, O_RDONLY);
|
|
|
|
|
|
|
|
|
|
/* this will be closed almost right away */
|
|
|
|
|
pipedes[1] = _open_osfhandle((long) hChildOutWr, O_APPEND);
|
1999-12-08 20:13:50 +00:00
|
|
|
|
} else {
|
|
|
|
|
/* reap/cleanup the failed process */
|
|
|
|
|
process_cleanup(hProcess);
|
|
|
|
|
|
|
|
|
|
/* close handles which were duplicated, they weren't used */
|
|
|
|
|
CloseHandle(hIn);
|
|
|
|
|
CloseHandle(hErr);
|
|
|
|
|
|
|
|
|
|
/* close pipe handles, they won't be used */
|
|
|
|
|
CloseHandle(hChildOutRd);
|
|
|
|
|
CloseHandle(hChildOutWr);
|
|
|
|
|
|
|
|
|
|
/* set status for return */
|
|
|
|
|
pipedes[0] = pipedes[1] = -1;
|
|
|
|
|
*pid_p = -1;
|
|
|
|
|
}
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __MSDOS__
|
1999-08-12 23:16:42 +00:00
|
|
|
|
FILE *
|
1999-03-26 04:04:42 +00:00
|
|
|
|
msdos_openpipe (int* pipedes, int *pidp, char *text)
|
|
|
|
|
{
|
|
|
|
|
FILE *fpipe=0;
|
1999-08-01 06:05:17 +00:00
|
|
|
|
/* MSDOS can't fork, but it has `popen'. */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
struct variable *sh = lookup_variable ("SHELL", 5);
|
|
|
|
|
int e;
|
|
|
|
|
extern int dos_command_running, dos_status;
|
|
|
|
|
|
|
|
|
|
/* Make sure not to bother processing an empty line. */
|
2000-06-07 05:43:37 +00:00
|
|
|
|
while (isblank ((unsigned char)*text))
|
1999-03-26 04:04:42 +00:00
|
|
|
|
++text;
|
|
|
|
|
if (*text == '\0')
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (sh)
|
|
|
|
|
{
|
|
|
|
|
char buf[PATH_MAX + 7];
|
|
|
|
|
/* This makes sure $SHELL value is used by $(shell), even
|
|
|
|
|
though the target environment is not passed to it. */
|
|
|
|
|
sprintf (buf, "SHELL=%s", sh->value);
|
|
|
|
|
putenv (buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
e = errno;
|
|
|
|
|
errno = 0;
|
|
|
|
|
dos_command_running = 1;
|
|
|
|
|
dos_status = 0;
|
1999-08-01 06:05:17 +00:00
|
|
|
|
/* If dos_status becomes non-zero, it means the child process
|
|
|
|
|
was interrupted by a signal, like SIGINT or SIGQUIT. See
|
|
|
|
|
fatal_error_signal in commands.c. */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
fpipe = popen (text, "rt");
|
|
|
|
|
dos_command_running = 0;
|
|
|
|
|
if (!fpipe || dos_status)
|
|
|
|
|
{
|
|
|
|
|
pipedes[0] = -1;
|
|
|
|
|
*pidp = -1;
|
|
|
|
|
if (dos_status)
|
|
|
|
|
errno = EINTR;
|
|
|
|
|
else if (errno == 0)
|
|
|
|
|
errno = ENOMEM;
|
|
|
|
|
shell_function_completed = -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pipedes[0] = fileno (fpipe);
|
1999-08-01 06:05:17 +00:00
|
|
|
|
*pidp = 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
errno = e;
|
|
|
|
|
shell_function_completed = 1;
|
|
|
|
|
}
|
|
|
|
|
return fpipe;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Do shell spawning, with the naughty bits for different OSes.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef VMS
|
|
|
|
|
|
|
|
|
|
/* VMS can't do $(shell ...) */
|
|
|
|
|
#define func_shell 0
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
#ifndef _AMIGA
|
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_shell (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
char *batch_filename = NULL;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
#ifdef __MSDOS__
|
|
|
|
|
FILE *fpipe;
|
|
|
|
|
#endif
|
|
|
|
|
char **command_argv;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *error_prefix;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
char **envp;
|
|
|
|
|
int pipedes[2];
|
|
|
|
|
int pid;
|
|
|
|
|
|
|
|
|
|
#ifndef __MSDOS__
|
|
|
|
|
/* Construct the argument list. */
|
2007-10-10 13:22:21 +00:00
|
|
|
|
command_argv = construct_command_argv (argv[0], NULL, NULL, 0,
|
|
|
|
|
&batch_filename);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (command_argv == 0)
|
|
|
|
|
return o;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Using a target environment for `shell' loses in cases like:
|
|
|
|
|
export var = $(shell echo foobie)
|
|
|
|
|
because target_environment hits a loop trying to expand $(var)
|
|
|
|
|
to put it in the environment. This is even more confusing when
|
|
|
|
|
var was not explicitly exported, but just appeared in the
|
2005-06-27 01:01:07 +00:00
|
|
|
|
calling environment.
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
See Savannah bug #10593.
|
|
|
|
|
|
2005-06-27 01:01:07 +00:00
|
|
|
|
envp = target_environment (NILF);
|
|
|
|
|
*/
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
|
|
|
|
envp = environ;
|
|
|
|
|
|
|
|
|
|
/* For error messages. */
|
2005-02-28 07:48:22 +00:00
|
|
|
|
if (reading_file && reading_file->filenm)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
char *p = alloca (strlen (reading_file->filenm)+11+4);
|
|
|
|
|
sprintf (p, "%s:%lu: ", reading_file->filenm, reading_file->lineno);
|
|
|
|
|
error_prefix = p;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
error_prefix = "";
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
#if defined(__MSDOS__)
|
1999-08-01 06:05:17 +00:00
|
|
|
|
fpipe = msdos_openpipe (pipedes, &pid, argv[0]);
|
|
|
|
|
if (pipedes[0] < 0)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
1999-08-01 06:05:17 +00:00
|
|
|
|
perror_with_name (error_prefix, "pipe");
|
|
|
|
|
return o;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
2007-03-20 03:02:26 +00:00
|
|
|
|
#elif defined(WINDOWS32)
|
|
|
|
|
windows32_openpipe (pipedes, &pid, command_argv, envp);
|
|
|
|
|
if (pipedes[0] < 0)
|
|
|
|
|
{
|
|
|
|
|
/* open of the pipe failed, mark as failed execution */
|
|
|
|
|
shell_function_completed = -1;
|
2003-03-24 23:14:15 +00:00
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
else
|
2003-03-24 23:14:15 +00:00
|
|
|
|
#else
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (pipe (pipedes) < 0)
|
|
|
|
|
{
|
|
|
|
|
perror_with_name (error_prefix, "pipe");
|
|
|
|
|
return o;
|
|
|
|
|
}
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
2003-03-24 23:14:15 +00:00
|
|
|
|
# ifdef __EMX__
|
|
|
|
|
/* close some handles that are unnecessary for the child process */
|
|
|
|
|
CLOSE_ON_EXEC(pipedes[1]);
|
|
|
|
|
CLOSE_ON_EXEC(pipedes[0]);
|
|
|
|
|
/* Never use fork()/exec() here! Use spawn() instead in exec_command() */
|
|
|
|
|
pid = child_execute_job (0, pipedes[1], command_argv, envp);
|
|
|
|
|
if (pid < 0)
|
|
|
|
|
perror_with_name (error_prefix, "spawn");
|
|
|
|
|
# else /* ! __EMX__ */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
pid = vfork ();
|
|
|
|
|
if (pid < 0)
|
|
|
|
|
perror_with_name (error_prefix, "fork");
|
|
|
|
|
else if (pid == 0)
|
|
|
|
|
child_execute_job (0, pipedes[1], command_argv, envp);
|
|
|
|
|
else
|
2003-03-24 23:14:15 +00:00
|
|
|
|
# endif
|
|
|
|
|
#endif
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
|
|
|
|
/* We are the parent. */
|
|
|
|
|
char *buffer;
|
2006-04-01 06:36:40 +00:00
|
|
|
|
unsigned int maxlen, i;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int cc;
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* Record the PID for reap_children. */
|
|
|
|
|
shell_function_pid = pid;
|
|
|
|
|
#ifndef __MSDOS__
|
|
|
|
|
shell_function_completed = 0;
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* Free the storage only the child needed. */
|
|
|
|
|
free (command_argv[0]);
|
2006-04-09 22:09:24 +00:00
|
|
|
|
free (command_argv);
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
2009-03-07 17:30:30 +00:00
|
|
|
|
/* Close the write side of the pipe. We test for -1, since
|
|
|
|
|
pipedes[1] is -1 on MS-Windows, and some versions of MS
|
|
|
|
|
libraries barf when `close' is called with -1. */
|
|
|
|
|
if (pipedes[1] >= 0)
|
|
|
|
|
close (pipedes[1]);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#endif
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* Set up and read from the pipe. */
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
maxlen = 200;
|
2006-04-09 22:09:24 +00:00
|
|
|
|
buffer = xmalloc (maxlen + 1);
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* Read from the pipe until it gets EOF. */
|
2001-06-01 03:56:50 +00:00
|
|
|
|
for (i = 0; ; i += cc)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (i == maxlen)
|
|
|
|
|
{
|
|
|
|
|
maxlen += 512;
|
2006-04-09 22:09:24 +00:00
|
|
|
|
buffer = xrealloc (buffer, maxlen + 1);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-03-24 23:14:15 +00:00
|
|
|
|
EINTRLOOP (cc, read (pipedes[0], &buffer[i], maxlen - i));
|
2001-06-01 03:56:50 +00:00
|
|
|
|
if (cc <= 0)
|
|
|
|
|
break;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
}
|
2000-10-05 16:27:06 +00:00
|
|
|
|
buffer[i] = '\0';
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* Close the read side of the pipe. */
|
|
|
|
|
#ifdef __MSDOS__
|
|
|
|
|
if (fpipe)
|
|
|
|
|
(void) pclose (fpipe);
|
|
|
|
|
#else
|
|
|
|
|
(void) close (pipedes[0]);
|
|
|
|
|
#endif
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
2003-03-24 23:14:15 +00:00
|
|
|
|
/* Loop until child_handler or reap_children() sets
|
|
|
|
|
shell_function_completed to the status of our child shell. */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
while (shell_function_completed == 0)
|
|
|
|
|
reap_children (1, 0);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (batch_filename) {
|
1999-11-27 08:09:42 +00:00
|
|
|
|
DB (DB_VERBOSE, (_("Cleaning up temporary batch file %s\n"),
|
1999-11-22 06:15:35 +00:00
|
|
|
|
batch_filename));
|
2000-04-05 16:02:55 +00:00
|
|
|
|
remove (batch_filename);
|
|
|
|
|
free (batch_filename);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
}
|
1999-03-26 04:04:42 +00:00
|
|
|
|
shell_function_pid = 0;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* The child_handler function will set shell_function_completed
|
|
|
|
|
to 1 when the child dies normally, or to -1 if it
|
|
|
|
|
dies with status 127, which is most likely an exec fail. */
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (shell_function_completed == -1)
|
1997-09-16 14:17:23 +00:00
|
|
|
|
{
|
2006-04-01 06:36:40 +00:00
|
|
|
|
/* This likely means that the execvp failed, so we should just
|
|
|
|
|
write the error message in the pipe from the child. */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
fputs (buffer, stderr);
|
|
|
|
|
fflush (stderr);
|
1997-09-16 14:17:23 +00:00
|
|
|
|
}
|
1997-04-07 07:21:16 +00:00
|
|
|
|
else
|
1997-09-16 14:17:23 +00:00
|
|
|
|
{
|
2006-04-01 06:36:40 +00:00
|
|
|
|
/* The child finished normally. Replace all newlines in its output
|
|
|
|
|
with spaces, and put that in the variable output buffer. */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
fold_newlines (buffer, &i);
|
|
|
|
|
o = variable_buffer_output (o, buffer, i);
|
1997-09-16 14:17:23 +00:00
|
|
|
|
}
|
1997-04-07 07:21:16 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
free (buffer);
|
|
|
|
|
}
|
1997-04-07 07:21:16 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
return o;
|
|
|
|
|
}
|
1997-04-07 07:21:16 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#else /* _AMIGA */
|
1997-04-07 07:21:16 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* Do the Amiga version of func_shell. */
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
static char *
|
|
|
|
|
func_shell (char *o, char **argv, const char *funcname)
|
|
|
|
|
{
|
|
|
|
|
/* Amiga can't fork nor spawn, but I can start a program with
|
|
|
|
|
redirection of my choice. However, this means that we
|
|
|
|
|
don't have an opportunity to reopen stdout to trap it. Thus,
|
|
|
|
|
we save our own stdout onto a new descriptor and dup a temp
|
|
|
|
|
file's descriptor onto our stdout temporarily. After we
|
|
|
|
|
spawn the shell program, we dup our own stdout back to the
|
|
|
|
|
stdout descriptor. The buffer reading is the same as above,
|
|
|
|
|
except that we're now reading from a file. */
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#include <dos/dos.h>
|
|
|
|
|
#include <proto/dos.h>
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
BPTR child_stdout;
|
|
|
|
|
char tmp_output[FILENAME_MAX];
|
2006-04-01 06:36:40 +00:00
|
|
|
|
unsigned int maxlen = 200, i;
|
|
|
|
|
int cc;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
char * buffer, * ptr;
|
|
|
|
|
char ** aptr;
|
|
|
|
|
int len = 0;
|
|
|
|
|
char* batch_filename = NULL;
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/* Construct the argument list. */
|
2007-10-10 13:22:21 +00:00
|
|
|
|
command_argv = construct_command_argv (argv[0], NULL, NULL, 0,
|
|
|
|
|
&batch_filename);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (command_argv == 0)
|
|
|
|
|
return o;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
2000-03-27 06:54:37 +00:00
|
|
|
|
/* Note the mktemp() is a security hole, but this only runs on Amiga.
|
|
|
|
|
Ideally we would use main.c:open_tmpfile(), but this uses a special
|
|
|
|
|
Open(), not fopen(), and I'm not familiar enough with the code to mess
|
|
|
|
|
with it. */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
strcpy (tmp_output, "t:MakeshXXXXXXXX");
|
|
|
|
|
mktemp (tmp_output);
|
|
|
|
|
child_stdout = Open (tmp_output, MODE_NEWFILE);
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
for (aptr=command_argv; *aptr; aptr++)
|
|
|
|
|
len += strlen (*aptr) + 1;
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
buffer = xmalloc (len + 1);
|
|
|
|
|
ptr = buffer;
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
for (aptr=command_argv; *aptr; aptr++)
|
|
|
|
|
{
|
|
|
|
|
strcpy (ptr, *aptr);
|
|
|
|
|
ptr += strlen (ptr) + 1;
|
|
|
|
|
*ptr ++ = ' ';
|
|
|
|
|
*ptr = 0;
|
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
ptr[-1] = '\n';
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
Execute (buffer, NULL, child_stdout);
|
|
|
|
|
free (buffer);
|
1996-03-20 14:57:41 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
Close (child_stdout);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
child_stdout = Open (tmp_output, MODE_OLDFILE);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
buffer = xmalloc (maxlen);
|
|
|
|
|
i = 0;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if (i == maxlen)
|
|
|
|
|
{
|
|
|
|
|
maxlen += 512;
|
2006-04-09 22:09:24 +00:00
|
|
|
|
buffer = xrealloc (buffer, maxlen + 1);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
cc = Read (child_stdout, &buffer[i], maxlen - i);
|
|
|
|
|
if (cc > 0)
|
|
|
|
|
i += cc;
|
|
|
|
|
} while (cc > 0);
|
|
|
|
|
|
|
|
|
|
Close (child_stdout);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
fold_newlines (buffer, &i);
|
|
|
|
|
o = variable_buffer_output (o, buffer, i);
|
|
|
|
|
free (buffer);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
return o;
|
|
|
|
|
}
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#endif /* _AMIGA */
|
|
|
|
|
#endif /* !VMS */
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
#ifdef EXPERIMENTAL
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/*
|
|
|
|
|
equality. Return is string-boolean, ie, the empty string is false.
|
|
|
|
|
*/
|
|
|
|
|
static char *
|
2004-11-30 19:51:24 +00:00
|
|
|
|
func_eq (char *o, char **argv, char *funcname)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int result = ! strcmp (argv[0], argv[1]);
|
|
|
|
|
o = variable_buffer_output (o, result ? "1" : "", result);
|
|
|
|
|
return o;
|
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
/*
|
|
|
|
|
string-boolean not operator.
|
|
|
|
|
*/
|
|
|
|
|
static char *
|
2004-11-30 19:51:24 +00:00
|
|
|
|
func_not (char *o, char **argv, char *funcname)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *s = argv[0];
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int result = 0;
|
1999-10-15 07:00:58 +00:00
|
|
|
|
while (isspace ((unsigned char)*s))
|
1999-03-26 04:04:42 +00:00
|
|
|
|
s++;
|
|
|
|
|
result = ! (*s);
|
|
|
|
|
o = variable_buffer_output (o, result ? "1" : "", result);
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
|
|
|
|
|
2004-11-30 19:51:24 +00:00
|
|
|
|
/* Return the absolute name of file NAME which does not contain any `.',
|
|
|
|
|
`..' components nor any repeated path separators ('/'). */
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
abspath (const char *name, char *apath)
|
|
|
|
|
{
|
|
|
|
|
char *dest;
|
|
|
|
|
const char *start, *end, *apath_limit;
|
|
|
|
|
|
|
|
|
|
if (name[0] == '\0' || apath == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2005-02-28 07:48:22 +00:00
|
|
|
|
apath_limit = apath + GET_PATH_MAX;
|
2004-11-30 19:51:24 +00:00
|
|
|
|
|
|
|
|
|
if (name[0] != '/')
|
|
|
|
|
{
|
|
|
|
|
/* It is unlikely we would make it until here but just to make sure. */
|
|
|
|
|
if (!starting_directory)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
strcpy (apath, starting_directory);
|
|
|
|
|
|
|
|
|
|
dest = strchr (apath, '\0');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
apath[0] = '/';
|
|
|
|
|
dest = apath + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (start = end = name; *start != '\0'; start = end)
|
|
|
|
|
{
|
|
|
|
|
unsigned long len;
|
|
|
|
|
|
|
|
|
|
/* Skip sequence of multiple path-separators. */
|
|
|
|
|
while (*start == '/')
|
|
|
|
|
++start;
|
|
|
|
|
|
|
|
|
|
/* Find end of path component. */
|
|
|
|
|
for (end = start; *end != '\0' && *end != '/'; ++end)
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
len = end - start;
|
|
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
|
break;
|
|
|
|
|
else if (len == 1 && start[0] == '.')
|
|
|
|
|
/* nothing */;
|
|
|
|
|
else if (len == 2 && start[0] == '.' && start[1] == '.')
|
|
|
|
|
{
|
|
|
|
|
/* Back up to previous component, ignore if at root already. */
|
|
|
|
|
if (dest > apath + 1)
|
|
|
|
|
while ((--dest)[-1] != '/');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (dest[-1] != '/')
|
|
|
|
|
*dest++ = '/';
|
|
|
|
|
|
|
|
|
|
if (dest + len >= apath_limit)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
dest = memcpy (dest, start, len);
|
|
|
|
|
dest += len;
|
|
|
|
|
*dest = '\0';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Unless it is root strip trailing separator. */
|
|
|
|
|
if (dest > apath + 1 && dest[-1] == '/')
|
|
|
|
|
--dest;
|
|
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
|
|
|
|
|
|
return apath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
func_realpath (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
|
|
|
|
/* Expand the argument. */
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *p = argv[0];
|
|
|
|
|
const char *path = 0;
|
2004-11-30 19:51:24 +00:00
|
|
|
|
int doneany = 0;
|
|
|
|
|
unsigned int len = 0;
|
2005-02-28 07:48:22 +00:00
|
|
|
|
PATH_VAR (in);
|
|
|
|
|
PATH_VAR (out);
|
2004-11-30 19:51:24 +00:00
|
|
|
|
|
|
|
|
|
while ((path = find_next_token (&p, &len)) != 0)
|
|
|
|
|
{
|
2005-02-28 07:48:22 +00:00
|
|
|
|
if (len < GET_PATH_MAX)
|
2004-11-30 19:51:24 +00:00
|
|
|
|
{
|
|
|
|
|
strncpy (in, path, len);
|
|
|
|
|
in[len] = '\0';
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
if (
|
2004-11-30 19:51:24 +00:00
|
|
|
|
#ifdef HAVE_REALPATH
|
2007-03-20 03:02:26 +00:00
|
|
|
|
realpath (in, out)
|
2004-11-30 19:51:24 +00:00
|
|
|
|
#else
|
2007-03-20 03:02:26 +00:00
|
|
|
|
abspath (in, out)
|
2004-11-30 19:51:24 +00:00
|
|
|
|
#endif
|
2007-03-20 03:02:26 +00:00
|
|
|
|
)
|
2004-11-30 19:51:24 +00:00
|
|
|
|
{
|
|
|
|
|
o = variable_buffer_output (o, out, strlen (out));
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Kill last space. */
|
|
|
|
|
if (doneany)
|
|
|
|
|
--o;
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
return o;
|
2004-11-30 19:51:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
func_abspath (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
|
|
|
|
/* Expand the argument. */
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *p = argv[0];
|
|
|
|
|
const char *path = 0;
|
2004-11-30 19:51:24 +00:00
|
|
|
|
int doneany = 0;
|
|
|
|
|
unsigned int len = 0;
|
2005-02-28 07:48:22 +00:00
|
|
|
|
PATH_VAR (in);
|
|
|
|
|
PATH_VAR (out);
|
2004-11-30 19:51:24 +00:00
|
|
|
|
|
|
|
|
|
while ((path = find_next_token (&p, &len)) != 0)
|
|
|
|
|
{
|
2005-02-28 07:48:22 +00:00
|
|
|
|
if (len < GET_PATH_MAX)
|
2004-11-30 19:51:24 +00:00
|
|
|
|
{
|
|
|
|
|
strncpy (in, path, len);
|
|
|
|
|
in[len] = '\0';
|
|
|
|
|
|
|
|
|
|
if (abspath (in, out))
|
|
|
|
|
{
|
|
|
|
|
o = variable_buffer_output (o, out, strlen (out));
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Kill last space. */
|
|
|
|
|
if (doneany)
|
|
|
|
|
--o;
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
return o;
|
2004-11-30 19:51:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-07-22 06:29:02 +00:00
|
|
|
|
/* Lookup table for builtin functions.
|
|
|
|
|
|
|
|
|
|
This doesn't have to be sorted; we use a straight lookup. We might gain
|
|
|
|
|
some efficiency by moving most often used functions to the start of the
|
|
|
|
|
table.
|
|
|
|
|
|
2000-01-11 07:31:42 +00:00
|
|
|
|
If MAXIMUM_ARGS is 0, that means there is no maximum and all
|
|
|
|
|
comma-separated values are treated as arguments.
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
|
|
|
|
EXPAND_ARGS means that all arguments should be expanded before invocation.
|
|
|
|
|
Functions that do namespace tricks (foreach) don't automatically expand. */
|
|
|
|
|
|
2006-04-07 01:43:44 +00:00
|
|
|
|
static char *func_call (char *o, char **argv, const char *funcname);
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
static struct function_table_entry function_table_init[] =
|
1999-07-22 06:29:02 +00:00
|
|
|
|
{
|
2000-01-11 07:31:42 +00:00
|
|
|
|
/* Name/size */ /* MIN MAX EXP? Function */
|
2004-11-30 19:51:24 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("abspath"), 0, 1, 1, func_abspath},
|
2000-01-11 07:31:42 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("addprefix"), 2, 2, 1, func_addsuffix_addprefix},
|
|
|
|
|
{ STRING_SIZE_TUPLE("addsuffix"), 2, 2, 1, func_addsuffix_addprefix},
|
2000-04-22 02:11:17 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("basename"), 0, 1, 1, func_basename_dir},
|
|
|
|
|
{ STRING_SIZE_TUPLE("dir"), 0, 1, 1, func_basename_dir},
|
|
|
|
|
{ STRING_SIZE_TUPLE("notdir"), 0, 1, 1, func_notdir_suffix},
|
2000-01-11 07:31:42 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("subst"), 3, 3, 1, func_subst},
|
2000-04-22 02:11:17 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("suffix"), 0, 1, 1, func_notdir_suffix},
|
2000-01-11 07:31:42 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("filter"), 2, 2, 1, func_filter_filterout},
|
|
|
|
|
{ STRING_SIZE_TUPLE("filter-out"), 2, 2, 1, func_filter_filterout},
|
|
|
|
|
{ STRING_SIZE_TUPLE("findstring"), 2, 2, 1, func_findstring},
|
2000-04-22 02:11:17 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("firstword"), 0, 1, 1, func_firstword},
|
2005-11-17 07:27:28 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("flavor"), 0, 1, 1, func_flavor},
|
2000-01-11 07:31:42 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("join"), 2, 2, 1, func_join},
|
2004-10-21 17:42:24 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("lastword"), 0, 1, 1, func_lastword},
|
2000-01-11 07:31:42 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("patsubst"), 3, 3, 1, func_patsubst},
|
2004-11-30 19:51:24 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("realpath"), 0, 1, 1, func_realpath},
|
2000-04-22 02:11:17 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("shell"), 0, 1, 1, func_shell},
|
|
|
|
|
{ STRING_SIZE_TUPLE("sort"), 0, 1, 1, func_sort},
|
|
|
|
|
{ STRING_SIZE_TUPLE("strip"), 0, 1, 1, func_strip},
|
|
|
|
|
{ STRING_SIZE_TUPLE("wildcard"), 0, 1, 1, func_wildcard},
|
2000-01-11 07:31:42 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("word"), 2, 2, 1, func_word},
|
|
|
|
|
{ STRING_SIZE_TUPLE("wordlist"), 3, 3, 1, func_wordlist},
|
2000-04-22 02:11:17 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("words"), 0, 1, 1, func_words},
|
|
|
|
|
{ STRING_SIZE_TUPLE("origin"), 0, 1, 1, func_origin},
|
2000-01-11 07:31:42 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("foreach"), 3, 3, 0, func_foreach},
|
2000-03-27 06:54:37 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("call"), 1, 0, 1, func_call},
|
2005-02-28 07:48:22 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("info"), 0, 1, 1, func_error},
|
2000-04-22 02:11:17 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("error"), 0, 1, 1, func_error},
|
|
|
|
|
{ STRING_SIZE_TUPLE("warning"), 0, 1, 1, func_error},
|
2000-01-11 07:31:42 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("if"), 2, 3, 0, func_if},
|
2006-02-10 05:29:00 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("or"), 1, 0, 0, func_or},
|
|
|
|
|
{ STRING_SIZE_TUPLE("and"), 1, 0, 0, func_and},
|
2002-07-08 02:26:47 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("value"), 0, 1, 1, func_value},
|
|
|
|
|
{ STRING_SIZE_TUPLE("eval"), 0, 1, 1, func_eval},
|
1999-07-22 06:29:02 +00:00
|
|
|
|
#ifdef EXPERIMENTAL
|
2000-01-11 07:31:42 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq},
|
2000-04-22 02:11:17 +00:00
|
|
|
|
{ STRING_SIZE_TUPLE("not"), 0, 1, 1, func_not},
|
1999-07-22 06:29:02 +00:00
|
|
|
|
#endif
|
|
|
|
|
};
|
2002-07-11 06:38:57 +00:00
|
|
|
|
|
|
|
|
|
#define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry))
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
/* These must come after the definition of function_table. */
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
|
|
|
|
static char *
|
2002-10-14 21:54:04 +00:00
|
|
|
|
expand_builtin_function (char *o, int argc, char **argv,
|
|
|
|
|
const struct function_table_entry *entry_p)
|
1999-07-22 06:29:02 +00:00
|
|
|
|
{
|
2002-09-18 04:35:52 +00:00
|
|
|
|
if (argc < (int)entry_p->minimum_args)
|
2006-02-15 23:54:42 +00:00
|
|
|
|
fatal (*expanding_var,
|
|
|
|
|
_("insufficient number of arguments (%d) to function `%s'"),
|
1999-07-22 06:29:02 +00:00
|
|
|
|
argc, entry_p->name);
|
|
|
|
|
|
2000-04-22 02:11:17 +00:00
|
|
|
|
/* I suppose technically some function could do something with no
|
|
|
|
|
arguments, but so far none do, so just test it for all functions here
|
|
|
|
|
rather than in each one. We can change it later if necessary. */
|
|
|
|
|
|
|
|
|
|
if (!argc)
|
|
|
|
|
return o;
|
|
|
|
|
|
1999-07-22 06:29:02 +00:00
|
|
|
|
if (!entry_p->func_ptr)
|
2006-02-15 23:54:42 +00:00
|
|
|
|
fatal (*expanding_var,
|
|
|
|
|
_("unimplemented on this platform: function `%s'"), entry_p->name);
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
|
|
|
|
return entry_p->func_ptr (o, argv, entry_p->name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check for a function invocation in *STRINGP. *STRINGP points at the
|
|
|
|
|
opening ( or { and is not null-terminated. If a function invocation
|
|
|
|
|
is found, expand it into the buffer at *OP, updating *OP, incrementing
|
|
|
|
|
*STRINGP past the reference and returning nonzero. If not, return zero. */
|
|
|
|
|
|
|
|
|
|
int
|
2006-11-18 20:53:44 +00:00
|
|
|
|
handle_function (char **op, const char **stringp)
|
1999-07-22 06:29:02 +00:00
|
|
|
|
{
|
|
|
|
|
const struct function_table_entry *entry_p;
|
|
|
|
|
char openparen = (*stringp)[0];
|
|
|
|
|
char closeparen = openparen == '(' ? ')' : '}';
|
2006-11-18 20:53:44 +00:00
|
|
|
|
const char *beg;
|
|
|
|
|
const char *end;
|
1999-07-22 06:29:02 +00:00
|
|
|
|
int count = 0;
|
2006-11-18 20:53:44 +00:00
|
|
|
|
char *abeg = NULL;
|
1999-07-22 06:29:02 +00:00
|
|
|
|
char **argv, **argvp;
|
|
|
|
|
int nargs;
|
|
|
|
|
|
2000-01-11 07:31:42 +00:00
|
|
|
|
beg = *stringp + 1;
|
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
entry_p = lookup_function (beg);
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
|
|
|
|
if (!entry_p)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2000-01-11 07:31:42 +00:00
|
|
|
|
/* We found a builtin function. Find the beginning of its arguments (skip
|
|
|
|
|
whitespace after the name). */
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
2000-01-11 07:31:42 +00:00
|
|
|
|
beg = next_token (beg + entry_p->len);
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
|
|
|
|
/* Find the end of the function invocation, counting nested use of
|
|
|
|
|
whichever kind of parens we use. Since we're looking, count commas
|
|
|
|
|
to get a rough estimate of how many arguments we might have. The
|
|
|
|
|
count might be high, but it'll never be low. */
|
|
|
|
|
|
2000-01-11 07:31:42 +00:00
|
|
|
|
for (nargs=1, end=beg; *end != '\0'; ++end)
|
|
|
|
|
if (*end == ',')
|
1999-07-22 06:29:02 +00:00
|
|
|
|
++nargs;
|
2000-01-11 07:31:42 +00:00
|
|
|
|
else if (*end == openparen)
|
1999-07-22 06:29:02 +00:00
|
|
|
|
++count;
|
2000-01-11 07:31:42 +00:00
|
|
|
|
else if (*end == closeparen && --count < 0)
|
1999-07-22 06:29:02 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (count >= 0)
|
2006-02-15 23:54:42 +00:00
|
|
|
|
fatal (*expanding_var,
|
1999-07-28 06:23:37 +00:00
|
|
|
|
_("unterminated call to function `%s': missing `%c'"),
|
1999-07-22 06:29:02 +00:00
|
|
|
|
entry_p->name, closeparen);
|
|
|
|
|
|
2000-01-11 07:31:42 +00:00
|
|
|
|
*stringp = end;
|
|
|
|
|
|
1999-07-22 06:29:02 +00:00
|
|
|
|
/* Get some memory to store the arg pointers. */
|
2006-04-09 22:09:24 +00:00
|
|
|
|
argvp = argv = alloca (sizeof (char *) * (nargs + 2));
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
2000-01-11 07:31:42 +00:00
|
|
|
|
/* Chop the string into arguments, then a nul. As soon as we hit
|
|
|
|
|
MAXIMUM_ARGS (if it's >0) assume the rest of the string is part of the
|
|
|
|
|
last argument.
|
|
|
|
|
|
|
|
|
|
If we're expanding, store pointers to the expansion of each one. If
|
|
|
|
|
not, make a duplicate of the string and point into that, nul-terminating
|
|
|
|
|
each argument. */
|
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
if (entry_p->expand_args)
|
1999-07-22 06:29:02 +00:00
|
|
|
|
{
|
2006-11-18 20:53:44 +00:00
|
|
|
|
const char *p;
|
|
|
|
|
for (p=beg, nargs=0; p <= end; ++argvp)
|
|
|
|
|
{
|
|
|
|
|
const char *next;
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
++nargs;
|
2000-01-11 07:31:42 +00:00
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
if (nargs == entry_p->maximum_args
|
|
|
|
|
|| (! (next = find_next_argument (openparen, closeparen, p, end))))
|
|
|
|
|
next = end;
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
*argvp = expand_argument (p, next);
|
|
|
|
|
p = next + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int len = end - beg;
|
|
|
|
|
char *p, *aend;
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
abeg = xmalloc (len+1);
|
|
|
|
|
memcpy (abeg, beg, len);
|
|
|
|
|
abeg[len] = '\0';
|
|
|
|
|
aend = abeg + len;
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
for (p=abeg, nargs=0; p <= aend; ++argvp)
|
2000-01-11 07:31:42 +00:00
|
|
|
|
{
|
2006-11-18 20:53:44 +00:00
|
|
|
|
char *next;
|
|
|
|
|
|
|
|
|
|
++nargs;
|
|
|
|
|
|
|
|
|
|
if (nargs == entry_p->maximum_args
|
|
|
|
|
|| (! (next = find_next_argument (openparen, closeparen, p, aend))))
|
|
|
|
|
next = aend;
|
|
|
|
|
|
2000-01-11 07:31:42 +00:00
|
|
|
|
*argvp = p;
|
|
|
|
|
*next = '\0';
|
2006-11-18 20:53:44 +00:00
|
|
|
|
p = next + 1;
|
2000-01-11 07:31:42 +00:00
|
|
|
|
}
|
1999-07-22 06:29:02 +00:00
|
|
|
|
}
|
2000-01-11 07:31:42 +00:00
|
|
|
|
*argvp = NULL;
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
|
|
|
|
/* Finally! Run the function... */
|
|
|
|
|
*op = expand_builtin_function (*op, nargs, argv, entry_p);
|
|
|
|
|
|
2000-01-11 07:31:42 +00:00
|
|
|
|
/* Free memory. */
|
1999-07-22 06:29:02 +00:00
|
|
|
|
if (entry_p->expand_args)
|
|
|
|
|
for (argvp=argv; *argvp != 0; ++argvp)
|
|
|
|
|
free (*argvp);
|
2006-11-18 20:53:44 +00:00
|
|
|
|
if (abeg)
|
|
|
|
|
free (abeg);
|
1999-07-22 06:29:02 +00:00
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
1999-07-15 07:36:44 +00:00
|
|
|
|
/* User-defined functions. Expand the first argument as either a builtin
|
|
|
|
|
function or a make variable, in the context of the rest of the arguments
|
|
|
|
|
assigned to $1, $2, ... $N. $0 is the name of the function. */
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
1999-07-22 06:29:02 +00:00
|
|
|
|
static char *
|
2004-02-24 13:50:19 +00:00
|
|
|
|
func_call (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
2003-01-22 13:45:44 +00:00
|
|
|
|
static int max_args = 0;
|
1999-07-15 07:36:44 +00:00
|
|
|
|
char *fname;
|
2000-01-11 07:31:42 +00:00
|
|
|
|
char *cp;
|
1999-07-15 07:36:44 +00:00
|
|
|
|
char *body;
|
2002-05-10 03:15:07 +00:00
|
|
|
|
int flen;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
int i;
|
2003-01-22 13:45:44 +00:00
|
|
|
|
int saved_args;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
const struct function_table_entry *entry_p;
|
2002-05-10 03:15:07 +00:00
|
|
|
|
struct variable *v;
|
1999-03-26 04:04:42 +00:00
|
|
|
|
|
1999-07-15 07:36:44 +00:00
|
|
|
|
/* There is no way to define a variable with a space in the name, so strip
|
2000-01-11 07:31:42 +00:00
|
|
|
|
leading and trailing whitespace as a favor to the user. */
|
2000-03-27 06:54:37 +00:00
|
|
|
|
fname = argv[0];
|
|
|
|
|
while (*fname != '\0' && isspace ((unsigned char)*fname))
|
|
|
|
|
++fname;
|
2000-01-11 07:31:42 +00:00
|
|
|
|
|
2000-04-05 16:02:55 +00:00
|
|
|
|
cp = fname + strlen (fname) - 1;
|
2000-03-27 06:54:37 +00:00
|
|
|
|
while (cp > fname && isspace ((unsigned char)*cp))
|
2000-01-11 07:31:42 +00:00
|
|
|
|
--cp;
|
|
|
|
|
cp[1] = '\0';
|
|
|
|
|
|
|
|
|
|
/* Calling nothing is a no-op */
|
2000-03-27 06:54:37 +00:00
|
|
|
|
if (*fname == '\0')
|
|
|
|
|
return o;
|
1999-07-15 07:36:44 +00:00
|
|
|
|
|
|
|
|
|
/* Are we invoking a builtin function? */
|
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
entry_p = lookup_function (fname);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
if (entry_p)
|
1992-01-11 11:37:36 +00:00
|
|
|
|
{
|
2000-01-11 07:31:42 +00:00
|
|
|
|
/* How many arguments do we have? */
|
1999-07-15 07:36:44 +00:00
|
|
|
|
for (i=0; argv[i+1]; ++i)
|
2007-05-09 02:01:53 +00:00
|
|
|
|
;
|
2000-03-27 06:54:37 +00:00
|
|
|
|
return expand_builtin_function (o, i, argv+1, entry_p);
|
1999-03-26 04:04:42 +00:00
|
|
|
|
}
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
2000-01-11 07:31:42 +00:00
|
|
|
|
/* Not a builtin, so the first argument is the name of a variable to be
|
2002-05-10 03:15:07 +00:00
|
|
|
|
expanded and interpreted as a function. Find it. */
|
2000-03-27 06:54:37 +00:00
|
|
|
|
flen = strlen (fname);
|
2000-01-11 07:31:42 +00:00
|
|
|
|
|
2002-05-10 03:15:07 +00:00
|
|
|
|
v = lookup_variable (fname, flen);
|
|
|
|
|
|
|
|
|
|
if (v == 0)
|
|
|
|
|
warn_undefined (fname, flen);
|
|
|
|
|
|
|
|
|
|
if (v == 0 || *v->value == '\0')
|
|
|
|
|
return o;
|
|
|
|
|
|
2006-04-09 22:09:24 +00:00
|
|
|
|
body = alloca (flen + 4);
|
2000-01-11 07:31:42 +00:00
|
|
|
|
body[0] = '$';
|
|
|
|
|
body[1] = '(';
|
|
|
|
|
memcpy (body + 2, fname, flen);
|
|
|
|
|
body[flen+2] = ')';
|
|
|
|
|
body[flen+3] = '\0';
|
1999-07-15 07:36:44 +00:00
|
|
|
|
|
|
|
|
|
/* Set up arguments $(1) .. $(N). $(0) is the function name. */
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
push_new_variable_scope ();
|
|
|
|
|
|
1999-07-15 07:36:44 +00:00
|
|
|
|
for (i=0; *argv; ++i, ++argv)
|
1999-03-26 04:04:42 +00:00
|
|
|
|
{
|
1999-07-15 07:36:44 +00:00
|
|
|
|
char num[11];
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
sprintf (num, "%d", i);
|
2002-04-20 19:25:54 +00:00
|
|
|
|
define_variable (num, strlen (num), *argv, o_automatic, 0);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-22 13:45:44 +00:00
|
|
|
|
/* If the number of arguments we have is < max_args, it means we're inside
|
|
|
|
|
a recursive invocation of $(call ...). Fill in the remaining arguments
|
|
|
|
|
in the new scope with the empty value, to hide them from this
|
|
|
|
|
invocation. */
|
|
|
|
|
|
|
|
|
|
for (; i < max_args; ++i)
|
|
|
|
|
{
|
|
|
|
|
char num[11];
|
|
|
|
|
|
|
|
|
|
sprintf (num, "%d", i);
|
|
|
|
|
define_variable (num, strlen (num), "", o_automatic, 0);
|
|
|
|
|
}
|
|
|
|
|
|
1999-07-15 07:36:44 +00:00
|
|
|
|
/* Expand the body in the context of the arguments, adding the result to
|
|
|
|
|
the variable buffer. */
|
|
|
|
|
|
2002-05-10 03:15:07 +00:00
|
|
|
|
v->exp_count = EXP_COUNT_MAX;
|
|
|
|
|
|
2003-01-22 13:45:44 +00:00
|
|
|
|
saved_args = max_args;
|
|
|
|
|
max_args = i;
|
1999-07-15 07:36:44 +00:00
|
|
|
|
o = variable_expand_string (o, body, flen+3);
|
2003-01-22 13:45:44 +00:00
|
|
|
|
max_args = saved_args;
|
1999-07-15 07:36:44 +00:00
|
|
|
|
|
2002-05-10 03:15:07 +00:00
|
|
|
|
v->exp_count = 0;
|
|
|
|
|
|
1999-03-26 04:04:42 +00:00
|
|
|
|
pop_variable_scope ();
|
1992-01-11 11:37:36 +00:00
|
|
|
|
|
2000-04-05 16:02:55 +00:00
|
|
|
|
return o + strlen (o);
|
1992-01-11 11:37:36 +00:00
|
|
|
|
}
|
2002-07-11 06:38:57 +00:00
|
|
|
|
|
|
|
|
|
void
|
2002-10-14 21:54:04 +00:00
|
|
|
|
hash_init_function_table (void)
|
2002-07-11 06:38:57 +00:00
|
|
|
|
{
|
|
|
|
|
hash_init (&function_table, FUNCTION_TABLE_ENTRIES * 2,
|
|
|
|
|
function_table_entry_hash_1, function_table_entry_hash_2,
|
|
|
|
|
function_table_entry_hash_cmp);
|
|
|
|
|
hash_load (&function_table, function_table_init,
|
|
|
|
|
FUNCTION_TABLE_ENTRIES, sizeof (struct function_table_entry));
|
|
|
|
|
}
|