1999-03-26 12:04:42 +08:00
|
|
|
|
/* Builtin function expansion for GNU Make.
|
2013-05-17 13:46:11 +08:00
|
|
|
|
Copyright (C) 1988-2013 Free Software Foundation, Inc.
|
1992-01-11 19:37:36 +08:00
|
|
|
|
This file is part of GNU Make.
|
|
|
|
|
|
2006-02-12 03:02:21 +08: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-05 03:35:15 +08:00
|
|
|
|
Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
|
version.
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2006-02-12 03:02:21 +08: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 19:37:36 +08:00
|
|
|
|
|
2006-02-12 03:02:21 +08:00
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
2007-07-05 03:35:15 +08:00
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>. */
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2013-01-21 00:01:01 +08:00
|
|
|
|
#include "makeint.h"
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#include "filedef.h"
|
1992-01-11 19:37:36 +08:00
|
|
|
|
#include "variable.h"
|
|
|
|
|
#include "dep.h"
|
|
|
|
|
#include "job.h"
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#include "commands.h"
|
1999-11-22 14:15:35 +08:00
|
|
|
|
#include "debug.h"
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1996-05-14 02:39:52 +08:00
|
|
|
|
#ifdef _AMIGA
|
|
|
|
|
#include "amiga.h"
|
|
|
|
|
#endif
|
1994-07-26 07:06:00 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
struct function_table_entry
|
|
|
|
|
{
|
2013-02-25 14:38:36 +08:00
|
|
|
|
union {
|
|
|
|
|
char *(*func_ptr) (char *output, char **argv, const char *fname);
|
|
|
|
|
char *(*alloc_func_ptr) (const char *fname, int argc, char **argv);
|
|
|
|
|
} fptr;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
const char *name;
|
2000-01-11 15:31:42 +08:00
|
|
|
|
unsigned char len;
|
|
|
|
|
unsigned char minimum_args;
|
|
|
|
|
unsigned char maximum_args;
|
2013-02-25 14:38:36 +08:00
|
|
|
|
unsigned char expand_args:1;
|
|
|
|
|
unsigned char alloc_fn:1;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
};
|
2002-07-11 14:38:57 +08:00
|
|
|
|
|
|
|
|
|
static unsigned long
|
2002-10-15 05:54:04 +08:00
|
|
|
|
function_table_entry_hash_1 (const void *keyv)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const struct function_table_entry *key = keyv;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
return_STRING_N_HASH_1 (key->name, key->len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unsigned long
|
2002-10-15 05:54:04 +08:00
|
|
|
|
function_table_entry_hash_2 (const void *keyv)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const struct function_table_entry *key = keyv;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
return_STRING_N_HASH_2 (key->name, key->len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2002-10-15 05:54:04 +08:00
|
|
|
|
function_table_entry_hash_cmp (const void *xv, const void *yv)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const struct function_table_entry *x = xv;
|
|
|
|
|
const struct function_table_entry *y = yv;
|
2002-07-11 14:38:57 +08: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 19:37:36 +08:00
|
|
|
|
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
1992-01-11 19:37:36 +08: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 12:00:31 +08:00
|
|
|
|
whitespace-delimited words. */
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1992-01-11 19:37:36 +08:00
|
|
|
|
char *
|
2007-03-20 11:02:26 +08:00
|
|
|
|
subst_expand (char *o, const char *text, const char *subst, const char *replace,
|
2004-09-21 12:00:31 +08:00
|
|
|
|
unsigned int slen, unsigned int rlen, int by_word)
|
1992-01-11 19:37:36 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *t = text;
|
|
|
|
|
const char *p;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2004-09-21 12:00:31 +08:00
|
|
|
|
if (slen == 0 && !by_word)
|
1992-01-11 19:37:36 +08:00
|
|
|
|
{
|
|
|
|
|
/* The first occurrence of "" in any string is its end. */
|
2004-09-21 20:07:12 +08:00
|
|
|
|
o = variable_buffer_output (o, t, strlen (t));
|
1992-01-11 19:37:36 +08:00
|
|
|
|
if (rlen > 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, replace, rlen);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
1994-03-04 13:36:26 +08:00
|
|
|
|
do
|
1992-01-11 19:37:36 +08:00
|
|
|
|
{
|
2004-09-21 12:00:31 +08:00
|
|
|
|
if (by_word && slen == 0)
|
2013-05-17 14:29:46 +08: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));
|
1994-03-04 13:36:26 +08:00
|
|
|
|
else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
p = strstr (t, subst);
|
|
|
|
|
if (p == 0)
|
|
|
|
|
{
|
|
|
|
|
/* No more matches. Output everything left on the end. */
|
|
|
|
|
o = variable_buffer_output (o, t, strlen (t));
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
}
|
1994-03-04 13:36:26 +08:00
|
|
|
|
|
1992-01-11 19:37:36 +08:00
|
|
|
|
/* Output everything before this occurrence of the string to replace. */
|
|
|
|
|
if (p > t)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, t, p - t);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
/* If we're substituting only by fully matched words,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
or only at the ends of words, check that this case qualifies. */
|
2004-09-21 12:00:31 +08:00
|
|
|
|
if (by_word
|
|
|
|
|
&& ((p > text && !isblank ((unsigned char)p[-1]))
|
|
|
|
|
|| (p[slen] != '\0' && !isblank ((unsigned char)p[slen]))))
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* Struck out. Output the rest of the string that is
|
|
|
|
|
no longer to be replaced. */
|
|
|
|
|
o = variable_buffer_output (o, subst, slen);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
else if (rlen > 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* Output the replacement string. */
|
|
|
|
|
o = variable_buffer_output (o, replace, rlen);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2004-09-21 20:07:12 +08:00
|
|
|
|
/* Advance T past the string to be replaced. */
|
2007-03-20 11:02:26 +08:00
|
|
|
|
t = p + slen;
|
1994-03-04 13:36:26 +08:00
|
|
|
|
} while (*t != '\0');
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
2004-09-21 12:00:31 +08:00
|
|
|
|
|
1992-01-11 19:37:36 +08: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 12:00:31 +08: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 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
char *
|
2007-03-20 11:02:26 +08: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 19:37:36 +08:00
|
|
|
|
{
|
1996-05-10 02:28:15 +08:00
|
|
|
|
unsigned int pattern_prepercent_len, pattern_postpercent_len;
|
2004-09-21 12:00:31 +08:00
|
|
|
|
unsigned int replace_prepercent_len, replace_postpercent_len;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *t;
|
2001-05-21 14:16:00 +08:00
|
|
|
|
unsigned int len;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
int doneany = 0;
|
|
|
|
|
|
2004-09-21 12:00:31 +08: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 19:37:36 +08:00
|
|
|
|
{
|
2004-09-21 12:00:31 +08:00
|
|
|
|
replace_prepercent_len = replace_percent - replace - 1;
|
|
|
|
|
replace_postpercent_len = strlen (replace_percent);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
2004-09-21 12:00:31 +08:00
|
|
|
|
{
|
|
|
|
|
replace_prepercent_len = strlen (replace);
|
|
|
|
|
replace_postpercent_len = 0;
|
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2004-09-21 12:00:31 +08:00
|
|
|
|
if (!pattern_percent)
|
1992-01-11 19:37:36 +08:00
|
|
|
|
/* With no % in the pattern, this is just a simple substitution. */
|
|
|
|
|
return subst_expand (o, text, pattern, replace,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
strlen (pattern), strlen (replace), 1);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
/* Record the length of PATTERN before and after the %
|
|
|
|
|
so we don't have to compute it more than once. */
|
2004-09-21 12:00:31 +08:00
|
|
|
|
pattern_prepercent_len = pattern_percent - pattern - 1;
|
|
|
|
|
pattern_postpercent_len = strlen (pattern_percent);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1993-01-07 06:57:35 +08:00
|
|
|
|
while ((t = find_next_token (&text, &len)) != 0)
|
1992-01-11 19:37:36 +08:00
|
|
|
|
{
|
|
|
|
|
int fail = 0;
|
|
|
|
|
|
|
|
|
|
/* Is it big enough to match? */
|
|
|
|
|
if (len < pattern_prepercent_len + pattern_postpercent_len)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
fail = 1;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1997-09-16 22:17:23 +08:00
|
|
|
|
/* Does the prefix match? */
|
1992-01-11 19:37:36 +08:00
|
|
|
|
if (!fail && pattern_prepercent_len > 0
|
2013-05-17 14:29:46 +08:00
|
|
|
|
&& (*t != *pattern
|
|
|
|
|
|| t[pattern_prepercent_len - 1] != pattern_percent[-2]
|
|
|
|
|
|| !strneq (t + 1, pattern + 1, pattern_prepercent_len - 1)))
|
|
|
|
|
fail = 1;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1997-09-16 22:17:23 +08:00
|
|
|
|
/* Does the suffix match? */
|
1992-01-11 19:37:36 +08:00
|
|
|
|
if (!fail && pattern_postpercent_len > 0
|
2013-05-17 14:29:46 +08:00
|
|
|
|
&& (t[len - 1] != pattern_percent[pattern_postpercent_len - 1]
|
|
|
|
|
|| t[len - pattern_postpercent_len] != *pattern_percent
|
|
|
|
|
|| !strneq (&t[len - pattern_postpercent_len],
|
|
|
|
|
pattern_percent, pattern_postpercent_len - 1)))
|
|
|
|
|
fail = 1;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
if (fail)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* It didn't match. Output the string. */
|
|
|
|
|
o = variable_buffer_output (o, t, len);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
/* 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 %. */
|
|
|
|
|
o = variable_buffer_output (o, replace_percent,
|
|
|
|
|
replace_postpercent_len);
|
|
|
|
|
}
|
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
/* Output a space, but not if the replacement is "". */
|
|
|
|
|
if (fail || replace_prepercent_len > 0
|
2013-05-17 14:29:46 +08:00
|
|
|
|
|| (replace_percent != 0 && len + replace_postpercent_len > 0))
|
|
|
|
|
{
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
}
|
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill the last space. */
|
|
|
|
|
--o;
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
2007-03-20 11:02:26 +08: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 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
/* Look up a function by name. */
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
static const struct function_table_entry *
|
2002-10-15 05:54:04 +08:00
|
|
|
|
lookup_function (const char *s)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2002-07-11 14:38:57 +08:00
|
|
|
|
const char *e = s;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08: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 19:37:36 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
return hash_find_item (&function_table, &function_table_entry_key);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
1998-07-31 04:54:47 +08:00
|
|
|
|
/* Return 1 if PATTERN matches STR, 0 if not. */
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
int
|
2006-11-19 04:53:44 +08:00
|
|
|
|
pattern_matches (const char *pattern, const char *percent, const char *str)
|
1992-01-11 19:37:36 +08:00
|
|
|
|
{
|
1998-07-31 04:54:47 +08:00
|
|
|
|
unsigned int sfxlen, strlength;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
if (percent == 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned int len = strlen (pattern) + 1;
|
2006-04-10 06:09:24 +08:00
|
|
|
|
char *new_chars = alloca (len);
|
|
|
|
|
memcpy (new_chars, pattern, len);
|
2006-11-19 04:53:44 +08:00
|
|
|
|
percent = find_percent (new_chars);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
if (percent == 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
return streq (new_chars, str);
|
2006-11-19 04:53:44 +08:00
|
|
|
|
pattern = new_chars;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
1992-08-20 06:16:15 +08:00
|
|
|
|
sfxlen = strlen (percent + 1);
|
1998-07-31 04:54:47 +08:00
|
|
|
|
strlength = strlen (str);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1998-07-31 04:54:47 +08:00
|
|
|
|
if (strlength < (percent - pattern) + sfxlen
|
1999-07-21 13:53:23 +08:00
|
|
|
|
|| !strneq (pattern, str, percent - pattern))
|
1992-01-11 19:37:36 +08:00
|
|
|
|
return 0;
|
|
|
|
|
|
1998-07-31 04:54:47 +08:00
|
|
|
|
return !strcmp (percent + 1, str + (strlength - sfxlen));
|
1992-01-11 19:37:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-03-26 12:04:42 +08: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 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
If no next argument is found, return NULL.
|
|
|
|
|
*/
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
static char *
|
2002-10-15 05:54:04 +08:00
|
|
|
|
find_next_argument (char startparen, char endparen,
|
|
|
|
|
const char *ptr, const char *end)
|
1992-01-11 19:37:36 +08:00
|
|
|
|
{
|
1999-03-26 12:04:42 +08:00
|
|
|
|
int count = 0;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
for (; ptr < end; ++ptr)
|
|
|
|
|
if (*ptr == startparen)
|
|
|
|
|
++count;
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
else if (*ptr == endparen)
|
1992-01-11 19:37:36 +08:00
|
|
|
|
{
|
2013-05-17 14:29:46 +08:00
|
|
|
|
--count;
|
|
|
|
|
if (count < 0)
|
|
|
|
|
return NULL;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
else if (*ptr == ',' && !count)
|
|
|
|
|
return (char *)ptr;
|
1997-09-16 22:17:23 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* We didn't find anything. */
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-16 22:17:23 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* Glob-expand LINE. The returned pointer is
|
|
|
|
|
only good until the next call to string_glob. */
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
static char *
|
2002-10-15 05:54:04 +08:00
|
|
|
|
string_glob (char *line)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
static char *result = 0;
|
|
|
|
|
static unsigned int length;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
struct nameseq *chain;
|
|
|
|
|
unsigned int idx;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2009-09-24 10:41:44 +08:00
|
|
|
|
chain = PARSE_FILE_SEQ (&line, struct nameseq, '\0', NULL,
|
2012-03-04 08:24:20 +08:00
|
|
|
|
/* We do not want parse_file_seq to strip './'s.
|
2009-09-17 01:07:01 +08:00
|
|
|
|
That would break examples like:
|
|
|
|
|
$(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
|
|
|
|
|
PARSEFS_NOSTRIP|PARSEFS_NOCACHE|PARSEFS_EXISTS);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (result == 0)
|
|
|
|
|
{
|
|
|
|
|
length = 100;
|
2006-04-10 06:09:24 +08:00
|
|
|
|
result = xmalloc (100);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
idx = 0;
|
|
|
|
|
while (chain != 0)
|
|
|
|
|
{
|
|
|
|
|
struct nameseq *next = chain->next;
|
2009-06-14 05:21:48 +08: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++] = ' ';
|
|
|
|
|
|
2009-10-04 04:08:20 +08:00
|
|
|
|
/* Because we used PARSEFS_NOCACHE above, we have to free() NAME. */
|
|
|
|
|
free ((char *)chain->name);
|
2006-04-10 06:09:24 +08:00
|
|
|
|
free (chain);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
chain = next;
|
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* Kill the last space and terminate the string. */
|
|
|
|
|
if (idx == 0)
|
|
|
|
|
result[0] = '\0';
|
|
|
|
|
else
|
|
|
|
|
result[idx - 1] = '\0';
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Builtin functions
|
|
|
|
|
*/
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_patsubst (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
o = patsubst_expand (o, argv[2], argv[0], argv[1]);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
return o;
|
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_join (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08: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 11:02:26 +08:00
|
|
|
|
const char *tp;
|
|
|
|
|
const char *pp;
|
|
|
|
|
const char *list1_iterator = argv[0];
|
|
|
|
|
const char *list2_iterator = argv[1];
|
1999-03-26 12:04:42 +08:00
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
unsigned int len1, len2;
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
tp = find_next_token (&list1_iterator, &len1);
|
|
|
|
|
if (tp != 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, tp, len1);
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
pp = find_next_token (&list2_iterator, &len2);
|
|
|
|
|
if (pp != 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, pp, len2);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (tp != 0 || pp != 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
|
|
|
|
while (tp != 0 || pp != 0);
|
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill the last blank. */
|
|
|
|
|
--o;
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_origin (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
/* Expand the argument. */
|
2007-03-20 11:02:26 +08:00
|
|
|
|
struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (v == 0)
|
|
|
|
|
o = variable_buffer_output (o, "undefined", 9);
|
|
|
|
|
else
|
|
|
|
|
switch (v->origin)
|
1992-01-11 19:37:36 +08:00
|
|
|
|
{
|
1999-03-26 12:04:42 +08:00
|
|
|
|
default:
|
|
|
|
|
case o_invalid:
|
2013-05-17 14:29:46 +08:00
|
|
|
|
abort ();
|
|
|
|
|
break;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
case o_default:
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, "default", 7);
|
|
|
|
|
break;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
case o_env:
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, "environment", 11);
|
|
|
|
|
break;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
case o_file:
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, "file", 4);
|
|
|
|
|
break;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
case o_env_override:
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, "environment override", 20);
|
|
|
|
|
break;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
case o_command:
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, "command line", 12);
|
|
|
|
|
break;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
case o_override:
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, "override", 8);
|
|
|
|
|
break;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
case o_automatic:
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, "automatic", 9);
|
|
|
|
|
break;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
return o;
|
|
|
|
|
}
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
2005-11-17 15:27:28 +08:00
|
|
|
|
static char *
|
|
|
|
|
func_flavor (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
2005-11-17 15:27:28 +08: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 12:04:42 +08:00
|
|
|
|
#ifdef VMS
|
2002-08-10 09:27:16 +08:00
|
|
|
|
# define IS_PATHSEP(c) ((c) == ']')
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#else
|
2002-08-10 09:27:16 +08:00
|
|
|
|
# ifdef HAVE_DOS_PATHS
|
|
|
|
|
# define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
|
|
|
|
|
# else
|
|
|
|
|
# define IS_PATHSEP(c) ((c) == '/')
|
|
|
|
|
# endif
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#endif
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
static char *
|
2002-10-15 05:54:04 +08:00
|
|
|
|
func_notdir_suffix (char *o, char **argv, const char *funcname)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
/* Expand the argument. */
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *list_iterator = argv[0];
|
|
|
|
|
const char *p2;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
int doneany =0;
|
2000-02-09 15:02:18 +08:00
|
|
|
|
unsigned int len=0;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2012-03-04 16:58:44 +08:00
|
|
|
|
int is_suffix = funcname[0] == 's';
|
1999-03-26 12:04:42 +08:00
|
|
|
|
int is_notdir = !is_suffix;
|
|
|
|
|
while ((p2 = find_next_token (&list_iterator, &len)) != 0)
|
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *p = p2 + len;
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
while (p >= p2 && (!is_suffix || *p != '.'))
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
if (IS_PATHSEP (*p))
|
|
|
|
|
break;
|
|
|
|
|
--p;
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
if (p >= p2)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
if (is_notdir)
|
|
|
|
|
++p;
|
|
|
|
|
else if (*p != '.')
|
|
|
|
|
continue;
|
|
|
|
|
o = variable_buffer_output (o, p, len - (p - p2));
|
|
|
|
|
}
|
2002-08-10 09:27:16 +08:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* Handle the case of "d:foo/bar". */
|
2012-03-04 16:58:44 +08:00
|
|
|
|
else if (is_notdir && p2[0] && p2[1] == ':')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
p = p2 + 2;
|
|
|
|
|
o = variable_buffer_output (o, p, len - (p - p2));
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#endif
|
|
|
|
|
else if (is_notdir)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, p2, len);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
if (is_notdir || p >= p2)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
2007-03-20 11:02:26 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill last space. */
|
|
|
|
|
--o;
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2002-10-15 05:54:04 +08:00
|
|
|
|
func_basename_dir (char *o, char **argv, const char *funcname)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
/* Expand the argument. */
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *p3 = argv[0];
|
|
|
|
|
const char *p2;
|
2012-03-04 16:58:44 +08:00
|
|
|
|
int doneany = 0;
|
|
|
|
|
unsigned int len = 0;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
|
2012-03-04 16:58:44 +08:00
|
|
|
|
int is_basename = funcname[0] == 'b';
|
|
|
|
|
int is_dir = !is_basename;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
while ((p2 = find_next_token (&p3, &len)) != 0)
|
2007-03-20 11:02:26 +08:00
|
|
|
|
{
|
|
|
|
|
const char *p = p2 + len;
|
|
|
|
|
while (p >= p2 && (!is_basename || *p != '.'))
|
|
|
|
|
{
|
|
|
|
|
if (IS_PATHSEP (*p))
|
|
|
|
|
break;
|
|
|
|
|
--p;
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2007-03-20 11:02:26 +08: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 09:27:16 +08:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
2007-03-20 11:02:26 +08:00
|
|
|
|
/* Handle the "d:foobar" case */
|
|
|
|
|
else if (p2[0] && p2[1] == ':' && is_dir)
|
|
|
|
|
o = variable_buffer_output (o, p2, 2);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#endif
|
2007-03-20 11:02:26 +08:00
|
|
|
|
else if (is_dir)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#ifdef VMS
|
2007-03-20 11:02:26 +08:00
|
|
|
|
o = variable_buffer_output (o, "[]", 2);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#else
|
|
|
|
|
#ifndef _AMIGA
|
2007-03-20 11:02:26 +08:00
|
|
|
|
o = variable_buffer_output (o, "./", 2);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#else
|
2007-03-20 11:02:26 +08:00
|
|
|
|
; /* Just a nop... */
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#endif /* AMIGA */
|
|
|
|
|
#endif /* !VMS */
|
2007-03-20 11:02:26 +08:00
|
|
|
|
else
|
|
|
|
|
/* The entire name is the basename. */
|
|
|
|
|
o = variable_buffer_output (o, p2, len);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill last space. */
|
|
|
|
|
--o;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
return o;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
2002-10-15 05:54:04 +08:00
|
|
|
|
func_addsuffix_addprefix (char *o, char **argv, const char *funcname)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
int fixlen = strlen (argv[0]);
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *list_iterator = argv[1];
|
2012-03-04 16:58:44 +08:00
|
|
|
|
int is_addprefix = funcname[3] == 'p';
|
1999-03-26 12:04:42 +08:00
|
|
|
|
int is_addsuffix = !is_addprefix;
|
|
|
|
|
|
1999-10-15 15:00:58 +08:00
|
|
|
|
int doneany = 0;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *p;
|
1999-10-15 15:00:58 +08:00
|
|
|
|
unsigned int len;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
while ((p = find_next_token (&list_iterator, &len)) != 0)
|
|
|
|
|
{
|
|
|
|
|
if (is_addprefix)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, argv[0], fixlen);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
o = variable_buffer_output (o, p, len);
|
|
|
|
|
if (is_addsuffix)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, argv[0], fixlen);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doneany)
|
|
|
|
|
/* Kill last space. */
|
|
|
|
|
--o;
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_subst (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
o = subst_expand (o, argv[2], argv[0], argv[1], strlen (argv[0]),
|
2013-05-17 14:29:46 +08:00
|
|
|
|
strlen (argv[1]), 0);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_firstword (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
1999-10-15 15:00:58 +08:00
|
|
|
|
unsigned int i;
|
2007-03-20 11:02:26 +08: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 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
if (p != 0)
|
|
|
|
|
o = variable_buffer_output (o, p, i);
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-22 01:42:24 +08:00
|
|
|
|
static char *
|
|
|
|
|
func_lastword (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
|
|
|
|
unsigned int i;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *words = argv[0]; /* Use a temp variable for find_next_token */
|
2007-05-12 04:57:21 +08:00
|
|
|
|
const char *p = NULL;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *t;
|
2004-10-22 01:42:24 +08: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 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_words (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *word_iterator = argv[0];
|
1999-03-26 12:04:42 +08:00
|
|
|
|
char buf[20];
|
|
|
|
|
|
2011-05-02 20:35:01 +08:00
|
|
|
|
while (find_next_token (&word_iterator, NULL) != 0)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
++i;
|
|
|
|
|
|
|
|
|
|
sprintf (buf, "%d", i);
|
|
|
|
|
o = variable_buffer_output (o, buf, strlen (buf));
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-04 15:40:29 +08: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-28 05:40:23 +08:00
|
|
|
|
char *
|
2003-01-30 14:21:36 +08:00
|
|
|
|
strip_whitespace (const char **begpp, const char **endpp)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2003-05-02 09:44:59 +08:00
|
|
|
|
while (*begpp <= *endpp && isspace ((unsigned char)**begpp))
|
1999-03-26 12:04:42 +08:00
|
|
|
|
(*begpp) ++;
|
2003-05-02 09:44:59 +08:00
|
|
|
|
while (*endpp >= *begpp && isspace ((unsigned char)**endpp))
|
1999-03-26 12:04:42 +08:00
|
|
|
|
(*endpp) --;
|
2003-01-30 14:21:36 +08:00
|
|
|
|
return (char *)*begpp;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-30 14:21:36 +08:00
|
|
|
|
static void
|
2006-04-10 06:09:24 +08:00
|
|
|
|
check_numeric (const char *s, const char *msg)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2003-01-30 14:21:36 +08:00
|
|
|
|
const char *end = s + strlen (s) - 1;
|
|
|
|
|
const char *beg = s;
|
|
|
|
|
strip_whitespace (&s, &end);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2003-01-30 14:21:36 +08:00
|
|
|
|
for (; s <= end; ++s)
|
2013-01-21 00:01:01 +08:00
|
|
|
|
if (!ISDIGIT (*s)) /* ISDIGIT only evals its arg once: see makeint.h. */
|
2003-01-30 14:21:36 +08:00
|
|
|
|
break;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2003-01-30 14:21:36 +08:00
|
|
|
|
if (s <= end || end - beg < 0)
|
2006-04-10 06:09:24 +08:00
|
|
|
|
fatal (*expanding_var, "%s: '%s'", msg, beg);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_word (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *end_p;
|
|
|
|
|
const char *p;
|
|
|
|
|
int i;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
/* Check the first argument. */
|
2012-03-04 08:24:20 +08:00
|
|
|
|
check_numeric (argv[0], _("non-numeric first argument to 'word' function"));
|
2007-03-20 11:02:26 +08:00
|
|
|
|
i = atoi (argv[0]);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
if (i == 0)
|
2006-02-16 07:54:42 +08:00
|
|
|
|
fatal (*expanding_var,
|
2012-03-04 08:24:20 +08:00
|
|
|
|
_("first argument to 'word' function must be greater than 0"));
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
end_p = argv[1];
|
|
|
|
|
while ((p = find_next_token (&end_p, 0)) != 0)
|
|
|
|
|
if (--i == 0)
|
1992-01-11 19:37:36 +08:00
|
|
|
|
break;
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (i == 0)
|
|
|
|
|
o = variable_buffer_output (o, p, end_p - p);
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_wordlist (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2000-04-06 00:02:55 +08:00
|
|
|
|
int start, count;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2000-04-06 00:02:55 +08:00
|
|
|
|
/* Check the arguments. */
|
1999-03-26 12:04:42 +08:00
|
|
|
|
check_numeric (argv[0],
|
2013-05-17 14:29:46 +08:00
|
|
|
|
_("non-numeric first argument to 'wordlist' function"));
|
1999-03-26 12:04:42 +08:00
|
|
|
|
check_numeric (argv[1],
|
2013-05-17 14:29:46 +08:00
|
|
|
|
_("non-numeric second argument to 'wordlist' function"));
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2000-04-06 00:02:55 +08:00
|
|
|
|
start = atoi (argv[0]);
|
2005-02-28 17:41:25 +08:00
|
|
|
|
if (start < 1)
|
2006-02-16 07:54:42 +08:00
|
|
|
|
fatal (*expanding_var,
|
2012-03-04 08:24:20 +08:00
|
|
|
|
"invalid first argument to 'wordlist' function: '%d'", start);
|
2005-02-28 17:41:25 +08:00
|
|
|
|
|
2000-04-06 00:02:55 +08:00
|
|
|
|
count = atoi (argv[1]) - start + 1;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2000-04-06 00:02:55 +08:00
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *p;
|
|
|
|
|
const char *end_p = argv[2];
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2000-04-06 00:02:55 +08:00
|
|
|
|
/* Find the beginning of the "start"th word. */
|
|
|
|
|
while (((p = find_next_token (&end_p, 0)) != 0) && --start)
|
|
|
|
|
;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2000-04-06 00:02:55 +08: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 12:04:42 +08:00
|
|
|
|
|
2000-04-06 00:02:55 +08:00
|
|
|
|
/* Return the stuff in the middle. */
|
|
|
|
|
o = variable_buffer_output (o, p, end_p - p);
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_findstring (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
/* Find the first occurrence of the first string in the second. */
|
2004-09-21 20:07:12 +08:00
|
|
|
|
if (strstr (argv[1], argv[0]) != 0)
|
|
|
|
|
o = variable_buffer_output (o, argv[0], strlen (argv[0]));
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_foreach (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
/* expand only the first two. */
|
2000-01-11 15:31:42 +08:00
|
|
|
|
char *varname = expand_argument (argv[0], NULL);
|
|
|
|
|
char *list = expand_argument (argv[1], NULL);
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *body = argv[2];
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
1999-10-15 15:00:58 +08:00
|
|
|
|
int doneany = 0;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *list_iterator = list;
|
|
|
|
|
const char *p;
|
1999-10-15 15:00:58 +08:00
|
|
|
|
unsigned int len;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
struct variable *var;
|
1999-03-26 12:04:42 +08: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 11:02:26 +08:00
|
|
|
|
free (var->value);
|
2009-05-25 02:31:18 +08:00
|
|
|
|
var->value = xstrndup (p, len);
|
1999-03-26 12:04:42 +08: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 14:38:57 +08:00
|
|
|
|
struct a_word *chain;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
char *str;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
int length;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
int matched;
|
|
|
|
|
};
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
static unsigned long
|
2002-10-15 05:54:04 +08:00
|
|
|
|
a_word_hash_1 (const void *key)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
|
|
|
|
return_STRING_HASH_1 (((struct a_word const *) key)->str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unsigned long
|
2002-10-15 05:54:04 +08:00
|
|
|
|
a_word_hash_2 (const void *key)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
|
|
|
|
return_STRING_HASH_2 (((struct a_word const *) key)->str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2002-10-15 05:54:04 +08:00
|
|
|
|
a_word_hash_cmp (const void *x, const void *y)
|
2002-07-11 14:38:57 +08: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,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
((struct a_word const *) y)->str);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct a_pattern
|
|
|
|
|
{
|
|
|
|
|
struct a_pattern *next;
|
|
|
|
|
char *str;
|
|
|
|
|
char *percent;
|
|
|
|
|
int length;
|
|
|
|
|
};
|
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
static char *
|
2002-10-15 05:54:04 +08:00
|
|
|
|
func_filter_filterout (char *o, char **argv, const char *funcname)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2002-07-11 14:38:57 +08: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 09:27:16 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
struct hash_table a_word_table;
|
2012-03-04 16:58:44 +08:00
|
|
|
|
int is_filter = funcname[CSTRLEN ("filter")] == '\0';
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *pat_iterator = argv[0];
|
|
|
|
|
const char *word_iterator = argv[1];
|
2002-07-11 14:38:57 +08:00
|
|
|
|
int literals = 0;
|
|
|
|
|
int words = 0;
|
|
|
|
|
int hashing = 0;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
char *p;
|
1999-10-15 15:00:58 +08:00
|
|
|
|
unsigned int len;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2012-03-04 06:12:46 +08:00
|
|
|
|
/* Chop ARGV[0] up into patterns to match against the words.
|
|
|
|
|
We don't need to preserve it because our caller frees all the
|
|
|
|
|
argument memory anyway. */
|
2002-07-11 14:38:57 +08:00
|
|
|
|
|
|
|
|
|
pattail = &pathead;
|
|
|
|
|
while ((p = find_next_token (&pat_iterator, &len)) != 0)
|
|
|
|
|
{
|
2006-04-10 06:09:24 +08:00
|
|
|
|
struct a_pattern *pat = alloca (sizeof (struct a_pattern));
|
2002-07-11 14:38:57 +08:00
|
|
|
|
|
|
|
|
|
*pattail = pat;
|
|
|
|
|
pattail = &pat->next;
|
|
|
|
|
|
|
|
|
|
if (*pat_iterator != '\0')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
++pat_iterator;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
|
|
|
|
|
pat->str = p;
|
|
|
|
|
p[len] = '\0';
|
|
|
|
|
pat->percent = find_percent (p);
|
|
|
|
|
if (pat->percent == 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
literals++;
|
2012-03-04 06:12:46 +08:00
|
|
|
|
|
|
|
|
|
/* find_percent() might shorten the string so LEN is wrong. */
|
|
|
|
|
pat->length = strlen (pat->str);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
}
|
|
|
|
|
*pattail = 0;
|
|
|
|
|
|
|
|
|
|
/* Chop ARGV[1] up into words to match against the patterns. */
|
|
|
|
|
|
|
|
|
|
wordtail = &wordhead;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
while ((p = find_next_token (&word_iterator, &len)) != 0)
|
|
|
|
|
{
|
2006-04-10 06:09:24 +08:00
|
|
|
|
struct a_word *word = alloca (sizeof (struct a_word));
|
2002-07-11 14:38:57 +08:00
|
|
|
|
|
|
|
|
|
*wordtail = word;
|
|
|
|
|
wordtail = &word->next;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
if (*word_iterator != '\0')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
++word_iterator;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
p[len] = '\0';
|
2002-07-11 14:38:57 +08:00
|
|
|
|
word->str = p;
|
|
|
|
|
word->length = len;
|
|
|
|
|
word->matched = 0;
|
|
|
|
|
word->chain = 0;
|
|
|
|
|
words++;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
2002-07-11 14:38:57 +08:00
|
|
|
|
*wordtail = 0;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
/* Only use a hash table if arg list lengths justifies the cost. */
|
|
|
|
|
hashing = (literals >= 2 && (literals * words) >= 10);
|
|
|
|
|
if (hashing)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
hash_init (&a_word_table, words, a_word_hash_1, a_word_hash_2,
|
|
|
|
|
a_word_hash_cmp);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
for (wp = wordhead; wp != 0; wp = wp->next)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
struct a_word *owp = hash_insert (&a_word_table, wp);
|
|
|
|
|
if (owp)
|
|
|
|
|
wp->chain = owp;
|
|
|
|
|
}
|
2002-07-11 14:38:57 +08:00
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (words)
|
|
|
|
|
{
|
|
|
|
|
int doneany = 0;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
/* Run each pattern through the words, killing words. */
|
2002-07-11 14:38:57 +08:00
|
|
|
|
for (pp = pathead; pp != 0; pp = pp->next)
|
2013-05-17 14:29:46 +08: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;
|
|
|
|
|
wp = hash_find_item (&a_word_table, &a_word_key);
|
|
|
|
|
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 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
/* Output the words that matched (or didn't, for filter-out). */
|
|
|
|
|
for (wp = wordhead; wp != 0; wp = wp->next)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
if (doneany)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* Kill the last space. */
|
|
|
|
|
--o;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (hashing)
|
|
|
|
|
hash_free (&a_word_table, 0);
|
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_strip (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *p = argv[0];
|
|
|
|
|
int doneany = 0;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
while (*p != '\0')
|
|
|
|
|
{
|
|
|
|
|
int i=0;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *word_start;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
1999-10-15 15:00:58 +08:00
|
|
|
|
while (isspace ((unsigned char)*p))
|
2013-05-17 14:29:46 +08:00
|
|
|
|
++p;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
word_start = p;
|
1999-10-15 15:00:58 +08:00
|
|
|
|
for (i=0; *p != '\0' && !isspace ((unsigned char)*p); ++p, ++i)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (!i)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
break;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
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 11:02:26 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Print a warning or fatal message.
|
|
|
|
|
*/
|
|
|
|
|
static char *
|
2002-10-15 05:54:04 +08:00
|
|
|
|
func_error (char *o, char **argv, const char *funcname)
|
1999-03-26 12:04:42 +08: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-06 00:02:55 +08:00
|
|
|
|
len += strlen (*argvp) + 2;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2006-04-10 06:09:24 +08:00
|
|
|
|
p = msg = alloca (len + 1);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
for (argvp=argv; argvp[1] != 0; ++argvp)
|
|
|
|
|
{
|
2000-04-06 00:02:55 +08:00
|
|
|
|
strcpy (p, *argvp);
|
|
|
|
|
p += strlen (*argvp);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
*(p++) = ',';
|
|
|
|
|
*(p++) = ' ';
|
|
|
|
|
}
|
2000-04-06 00:02:55 +08:00
|
|
|
|
strcpy (p, *argvp);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
switch (*funcname)
|
|
|
|
|
{
|
2005-02-28 15:48:22 +08:00
|
|
|
|
case 'e':
|
|
|
|
|
fatal (reading_file, "%s", msg);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2005-02-28 15:48:22 +08:00
|
|
|
|
case 'w':
|
|
|
|
|
error (reading_file, "%s", msg);
|
|
|
|
|
break;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2005-02-28 15:48:22 +08:00
|
|
|
|
case 'i':
|
|
|
|
|
printf ("%s\n", msg);
|
2013-05-17 14:29:46 +08:00
|
|
|
|
fflush (stdout);
|
2005-02-28 15:48:22 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2006-02-16 07:54:42 +08:00
|
|
|
|
fatal (*expanding_var, "Internal error: func_error: '%s'", funcname);
|
2013-05-17 14:29:46 +08:00
|
|
|
|
}
|
2005-02-28 15:48:22 +08:00
|
|
|
|
|
|
|
|
|
/* The warning function expands to the empty string. */
|
1999-03-26 12:04:42 +08:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
chop argv[0] into words, and sort them.
|
|
|
|
|
*/
|
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_sort (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *t;
|
|
|
|
|
char **words;
|
|
|
|
|
int wordi;
|
1999-10-15 15:00:58 +08:00
|
|
|
|
char *p;
|
|
|
|
|
unsigned int len;
|
1999-07-21 13:53:23 +08:00
|
|
|
|
int i;
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
/* Find the maximum number of words we'll have. */
|
|
|
|
|
t = argv[0];
|
2011-05-02 20:35:01 +08:00
|
|
|
|
wordi = 0;
|
|
|
|
|
while ((p = find_next_token (&t, NULL)) != 0)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2011-05-02 20:35:01 +08:00
|
|
|
|
++t;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
++wordi;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-02 20:35:01 +08:00
|
|
|
|
words = xmalloc ((wordi == 0 ? 1 : wordi) * sizeof (char *));
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2007-03-20 11:02:26 +08: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 13:53:23 +08:00
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
if (wordi)
|
1999-07-21 13:53:23 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08: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 13:53:23 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08: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 13:53:23 +08:00
|
|
|
|
}
|
2007-03-20 11:02:26 +08:00
|
|
|
|
|
|
|
|
|
/* Kill the last space. */
|
|
|
|
|
--o;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
1999-07-21 13:53:23 +08:00
|
|
|
|
|
|
|
|
|
free (words);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
1999-08-13 15:36:26 +08: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 21:50:19 +08:00
|
|
|
|
func_if (char *o, char **argv, const char *funcname UNUSED)
|
1999-08-13 15:36:26 +08:00
|
|
|
|
{
|
2003-01-30 14:21:36 +08:00
|
|
|
|
const char *begp = argv[0];
|
2003-11-04 15:40:29 +08:00
|
|
|
|
const char *endp = begp + strlen (argv[0]) - 1;
|
1999-08-13 15:36:26 +08: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 15:40:29 +08:00
|
|
|
|
if (begp <= endp)
|
1999-08-13 15:36:26 +08:00
|
|
|
|
{
|
2003-11-04 15:40:29 +08:00
|
|
|
|
char *expansion = expand_argument (begp, endp+1);
|
1999-08-13 15:36:26 +08: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 11:02:26 +08:00
|
|
|
|
if (*argv)
|
1999-08-13 15:36:26 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
char *expansion = expand_argument (*argv, NULL);
|
1999-08-13 15:36:26 +08:00
|
|
|
|
|
|
|
|
|
o = variable_buffer_output (o, expansion, strlen (expansion));
|
2000-01-11 15:31:42 +08:00
|
|
|
|
|
1999-08-13 15:36:26 +08:00
|
|
|
|
free (expansion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-10 13:29:00 +08: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 12:04:42 +08:00
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_wildcard (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
1996-05-14 02:39:52 +08:00
|
|
|
|
#ifdef _AMIGA
|
1999-03-26 12:04:42 +08:00
|
|
|
|
o = wildcard_expansion (argv[0], o);
|
1996-05-14 02:39:52 +08:00
|
|
|
|
#else
|
1999-03-26 12:04:42 +08:00
|
|
|
|
char *p = string_glob (argv[0]);
|
|
|
|
|
o = variable_buffer_output (o, p, strlen (p));
|
1996-05-14 02:39:52 +08:00
|
|
|
|
#endif
|
1999-03-26 12:04:42 +08:00
|
|
|
|
return o;
|
|
|
|
|
}
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
2002-07-08 10:26:47 +08: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 21:50:19 +08:00
|
|
|
|
func_eval (char *o, char **argv, const char *funcname UNUSED)
|
2002-07-08 10:26:47 +08:00
|
|
|
|
{
|
2002-10-26 06:01:47 +08: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);
|
|
|
|
|
|
2013-02-25 14:38:36 +08:00
|
|
|
|
eval_buffer (argv[0], NULL);
|
2002-07-08 10:26:47 +08:00
|
|
|
|
|
2002-10-26 06:01:47 +08:00
|
|
|
|
restore_variable_buffer (buf, len);
|
|
|
|
|
|
2002-07-08 10:26:47 +08:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_value (char *o, char **argv, const char *funcname UNUSED)
|
2002-07-08 10:26:47 +08: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)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
o = variable_buffer_output (o, v->value, strlen (v->value));
|
2002-07-08 10:26:47 +08:00
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/*
|
2011-04-18 09:25:20 +08:00
|
|
|
|
\r is replaced on UNIX as well. Is this desirable?
|
1999-03-26 12:04:42 +08:00
|
|
|
|
*/
|
2006-04-01 14:36:40 +08:00
|
|
|
|
static void
|
2011-04-18 09:25:20 +08:00
|
|
|
|
fold_newlines (char *buffer, unsigned int *length, int trim_newlines)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
char *dst = buffer;
|
|
|
|
|
char *src = buffer;
|
2011-04-18 09:25:20 +08:00
|
|
|
|
char *last_nonnl = buffer - 1;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
src[*length] = 0;
|
|
|
|
|
for (; *src != '\0'; ++src)
|
|
|
|
|
{
|
|
|
|
|
if (src[0] == '\r' && src[1] == '\n')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
continue;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (*src == '\n')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
*dst++ = ' ';
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
last_nonnl = dst;
|
|
|
|
|
*dst++ = *src;
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
2011-04-18 09:25:20 +08:00
|
|
|
|
|
|
|
|
|
if (!trim_newlines && (last_nonnl < (dst - 2)))
|
|
|
|
|
last_nonnl = dst - 2;
|
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
*(++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"
|
|
|
|
|
|
|
|
|
|
|
Support --output-sync on MS-Windows.
w32/compat/posixfcn.c: New file, with emulations of Posix
functions and Posix functionality for MS-Windows.
w32/subproc/sub_proc.c: Include io.h.
(process_noinherit): New function, forces a file descriptor to not
be inherited by child processes.
(process_easy): Accept two additional arguments, and use them to
set up the standard output and standard error handles of the child
process.
w32/include/sub_proc.h (process_easy): Adjust prototype.
(process_noinherit): Add prototype.
read.c [WINDOWS32]: Include windows.h and sub_proc.h.
makeint.h (LOCALEDIR) [WINDOWS32}: Define to NULL if not
defined. This is needed because the MS-Windows build doesn't have
a canonical place for LOCALEDIR.
(WIN32_LEAN_AND_MEAN) [WINDOWS32]: Define, to avoid getting from
windows.h header too much stuff that could conflict with the code.
main.c <sync_mutex>: New static variable.
<switches>: Add support for "--sync-mutex" switch.
(decode_output_sync_flags): Decode the --sync-mutex= switch.
(prepare_mutex_handle_string) [WINDOWS32]: New function.
(main): Add "output-sync" to .FEATURES.
job.h (CLOSE_ON_EXEC) [WINDOWS32]: Define to call
process_noinherit.
(F_GETFD, F_SETLKW, F_WRLCK, F_UNLCK, struct flock) [WINDOWS32]:
New macros.
(RECORD_SYNC_MUTEX): New macro, a no-op for Posix platforms.
(sync_handle_t): New typedef.
job.c <sync_handle>: Change type to sync_handle_t.
(FD_NOT_EMPTY): Seek to the file's end. Suggested by Frank
Heckenbach <f.heckenbach@fh-soft.de>.
(pump_from_tmp_fd) [WINDOWS32]: Switch to_fd to binary mode for
the duration of this function, and then change back before
returning.
(start_job_command) [WINDOWS32]: Support output_sync mode on
MS-Windows. Use a system-wide mutex instead of locking
stdout/stderr. Call process_easy with two additional arguments:
child->outfd and child->errfd.
(exec_command) [WINDOWS32]: Pass two additional arguments, both
-1, to process_easy, to adjust for the changed function signature.
function.c (windows32_openpipe) [WINDOWS32]: This function now
returns an int, which is -1 if it fails and zero otherwise. It
also calls 'error' instead of 'fatal', to avoid exiting
prematurely.
(func_shell_base) [WINDOWS32]: Call perror_with_name if
windows32_openpipe fails, now that it always returns. This avoids
a compiler warning that error_prefix is not used in the MS-Windows
build.
config.h.W32.template (OUTPUT_SYNC): Define.
build_w32.bat: Add w32/compat/posixfcn.c to compilation and
linking commands.
From Frank Heckenbach <f.heckenbach@fh-soft.de>:
job.c (sync_output): Don't discard the output if
acquire_semaphore fails; instead, dump the output unsynchronized.
2013-04-27 19:20:49 +08:00
|
|
|
|
int
|
2010-07-09 19:10:04 +08:00
|
|
|
|
windows32_openpipe (int *pipedes, pid_t *pid_p, char **command_argv, char **envp)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
SECURITY_ATTRIBUTES saAttr;
|
2012-01-29 00:50:21 +08:00
|
|
|
|
HANDLE hIn = INVALID_HANDLE_VALUE;
|
|
|
|
|
HANDLE hErr = INVALID_HANDLE_VALUE;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
HANDLE hChildOutRd;
|
|
|
|
|
HANDLE hChildOutWr;
|
2012-01-29 00:50:21 +08:00
|
|
|
|
HANDLE hProcess, tmpIn, tmpErr;
|
|
|
|
|
DWORD e;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
Support --output-sync on MS-Windows.
w32/compat/posixfcn.c: New file, with emulations of Posix
functions and Posix functionality for MS-Windows.
w32/subproc/sub_proc.c: Include io.h.
(process_noinherit): New function, forces a file descriptor to not
be inherited by child processes.
(process_easy): Accept two additional arguments, and use them to
set up the standard output and standard error handles of the child
process.
w32/include/sub_proc.h (process_easy): Adjust prototype.
(process_noinherit): Add prototype.
read.c [WINDOWS32]: Include windows.h and sub_proc.h.
makeint.h (LOCALEDIR) [WINDOWS32}: Define to NULL if not
defined. This is needed because the MS-Windows build doesn't have
a canonical place for LOCALEDIR.
(WIN32_LEAN_AND_MEAN) [WINDOWS32]: Define, to avoid getting from
windows.h header too much stuff that could conflict with the code.
main.c <sync_mutex>: New static variable.
<switches>: Add support for "--sync-mutex" switch.
(decode_output_sync_flags): Decode the --sync-mutex= switch.
(prepare_mutex_handle_string) [WINDOWS32]: New function.
(main): Add "output-sync" to .FEATURES.
job.h (CLOSE_ON_EXEC) [WINDOWS32]: Define to call
process_noinherit.
(F_GETFD, F_SETLKW, F_WRLCK, F_UNLCK, struct flock) [WINDOWS32]:
New macros.
(RECORD_SYNC_MUTEX): New macro, a no-op for Posix platforms.
(sync_handle_t): New typedef.
job.c <sync_handle>: Change type to sync_handle_t.
(FD_NOT_EMPTY): Seek to the file's end. Suggested by Frank
Heckenbach <f.heckenbach@fh-soft.de>.
(pump_from_tmp_fd) [WINDOWS32]: Switch to_fd to binary mode for
the duration of this function, and then change back before
returning.
(start_job_command) [WINDOWS32]: Support output_sync mode on
MS-Windows. Use a system-wide mutex instead of locking
stdout/stderr. Call process_easy with two additional arguments:
child->outfd and child->errfd.
(exec_command) [WINDOWS32]: Pass two additional arguments, both
-1, to process_easy, to adjust for the changed function signature.
function.c (windows32_openpipe) [WINDOWS32]: This function now
returns an int, which is -1 if it fails and zero otherwise. It
also calls 'error' instead of 'fatal', to avoid exiting
prematurely.
(func_shell_base) [WINDOWS32]: Call perror_with_name if
windows32_openpipe fails, now that it always returns. This avoids
a compiler warning that error_prefix is not used in the MS-Windows
build.
config.h.W32.template (OUTPUT_SYNC): Define.
build_w32.bat: Add w32/compat/posixfcn.c to compilation and
linking commands.
From Frank Heckenbach <f.heckenbach@fh-soft.de>:
job.c (sync_output): Don't discard the output if
acquire_semaphore fails; instead, dump the output unsynchronized.
2013-04-27 19:20:49 +08:00
|
|
|
|
/* Set status for return. */
|
|
|
|
|
pipedes[0] = pipedes[1] = -1;
|
|
|
|
|
*pid_p = (pid_t)-1;
|
|
|
|
|
|
2000-04-06 00:02:55 +08:00
|
|
|
|
saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
saAttr.bInheritHandle = TRUE;
|
|
|
|
|
saAttr.lpSecurityDescriptor = NULL;
|
|
|
|
|
|
2012-01-29 00:50:21 +08:00
|
|
|
|
/* Standard handles returned by GetStdHandle can be NULL or
|
|
|
|
|
INVALID_HANDLE_VALUE if the parent process closed them. If that
|
|
|
|
|
happens, we open the null device and pass its handle to
|
|
|
|
|
process_begin below as the corresponding handle to inherit. */
|
2013-05-17 14:29:46 +08:00
|
|
|
|
tmpIn = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
|
if (DuplicateHandle (GetCurrentProcess (), tmpIn,
|
|
|
|
|
GetCurrentProcess (), &hIn,
|
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE)
|
|
|
|
|
{
|
|
|
|
|
e = GetLastError ();
|
|
|
|
|
if (e == ERROR_INVALID_HANDLE)
|
|
|
|
|
{
|
|
|
|
|
tmpIn = CreateFile ("NUL", GENERIC_READ,
|
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
|
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
if (tmpIn != INVALID_HANDLE_VALUE
|
|
|
|
|
&& DuplicateHandle (GetCurrentProcess (), tmpIn,
|
|
|
|
|
GetCurrentProcess (), &hIn,
|
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE)
|
|
|
|
|
CloseHandle (tmpIn);
|
|
|
|
|
}
|
|
|
|
|
if (hIn == INVALID_HANDLE_VALUE)
|
|
|
|
|
{
|
|
|
|
|
error (NILF, _("windows32_openpipe: DuplicateHandle(In) failed (e=%ld)\n"), e);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
Support --output-sync on MS-Windows.
w32/compat/posixfcn.c: New file, with emulations of Posix
functions and Posix functionality for MS-Windows.
w32/subproc/sub_proc.c: Include io.h.
(process_noinherit): New function, forces a file descriptor to not
be inherited by child processes.
(process_easy): Accept two additional arguments, and use them to
set up the standard output and standard error handles of the child
process.
w32/include/sub_proc.h (process_easy): Adjust prototype.
(process_noinherit): Add prototype.
read.c [WINDOWS32]: Include windows.h and sub_proc.h.
makeint.h (LOCALEDIR) [WINDOWS32}: Define to NULL if not
defined. This is needed because the MS-Windows build doesn't have
a canonical place for LOCALEDIR.
(WIN32_LEAN_AND_MEAN) [WINDOWS32]: Define, to avoid getting from
windows.h header too much stuff that could conflict with the code.
main.c <sync_mutex>: New static variable.
<switches>: Add support for "--sync-mutex" switch.
(decode_output_sync_flags): Decode the --sync-mutex= switch.
(prepare_mutex_handle_string) [WINDOWS32]: New function.
(main): Add "output-sync" to .FEATURES.
job.h (CLOSE_ON_EXEC) [WINDOWS32]: Define to call
process_noinherit.
(F_GETFD, F_SETLKW, F_WRLCK, F_UNLCK, struct flock) [WINDOWS32]:
New macros.
(RECORD_SYNC_MUTEX): New macro, a no-op for Posix platforms.
(sync_handle_t): New typedef.
job.c <sync_handle>: Change type to sync_handle_t.
(FD_NOT_EMPTY): Seek to the file's end. Suggested by Frank
Heckenbach <f.heckenbach@fh-soft.de>.
(pump_from_tmp_fd) [WINDOWS32]: Switch to_fd to binary mode for
the duration of this function, and then change back before
returning.
(start_job_command) [WINDOWS32]: Support output_sync mode on
MS-Windows. Use a system-wide mutex instead of locking
stdout/stderr. Call process_easy with two additional arguments:
child->outfd and child->errfd.
(exec_command) [WINDOWS32]: Pass two additional arguments, both
-1, to process_easy, to adjust for the changed function signature.
function.c (windows32_openpipe) [WINDOWS32]: This function now
returns an int, which is -1 if it fails and zero otherwise. It
also calls 'error' instead of 'fatal', to avoid exiting
prematurely.
(func_shell_base) [WINDOWS32]: Call perror_with_name if
windows32_openpipe fails, now that it always returns. This avoids
a compiler warning that error_prefix is not used in the MS-Windows
build.
config.h.W32.template (OUTPUT_SYNC): Define.
build_w32.bat: Add w32/compat/posixfcn.c to compilation and
linking commands.
From Frank Heckenbach <f.heckenbach@fh-soft.de>:
job.c (sync_output): Don't discard the output if
acquire_semaphore fails; instead, dump the output unsynchronized.
2013-04-27 19:20:49 +08:00
|
|
|
|
}
|
2013-05-17 14:29:46 +08:00
|
|
|
|
tmpErr = GetStdHandle (STD_ERROR_HANDLE);
|
|
|
|
|
if (DuplicateHandle (GetCurrentProcess (), tmpErr,
|
|
|
|
|
GetCurrentProcess (), &hErr,
|
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE)
|
|
|
|
|
{
|
|
|
|
|
e = GetLastError ();
|
|
|
|
|
if (e == ERROR_INVALID_HANDLE)
|
|
|
|
|
{
|
|
|
|
|
tmpErr = CreateFile ("NUL", GENERIC_WRITE,
|
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
|
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
if (tmpErr != INVALID_HANDLE_VALUE
|
|
|
|
|
&& DuplicateHandle (GetCurrentProcess (), tmpErr,
|
|
|
|
|
GetCurrentProcess (), &hErr,
|
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE)
|
|
|
|
|
CloseHandle (tmpErr);
|
|
|
|
|
}
|
|
|
|
|
if (hErr == INVALID_HANDLE_VALUE)
|
|
|
|
|
{
|
|
|
|
|
error (NILF, _("windows32_openpipe: DuplicateHandle(Err) failed (e=%ld)\n"), e);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2012-01-29 00:50:21 +08:00
|
|
|
|
}
|
2013-05-17 14:29:46 +08:00
|
|
|
|
|
|
|
|
|
if (! CreatePipe (&hChildOutRd, &hChildOutWr, &saAttr, 0))
|
|
|
|
|
{
|
|
|
|
|
error (NILF, _("CreatePipe() failed (e=%ld)\n"), GetLastError());
|
Support --output-sync on MS-Windows.
w32/compat/posixfcn.c: New file, with emulations of Posix
functions and Posix functionality for MS-Windows.
w32/subproc/sub_proc.c: Include io.h.
(process_noinherit): New function, forces a file descriptor to not
be inherited by child processes.
(process_easy): Accept two additional arguments, and use them to
set up the standard output and standard error handles of the child
process.
w32/include/sub_proc.h (process_easy): Adjust prototype.
(process_noinherit): Add prototype.
read.c [WINDOWS32]: Include windows.h and sub_proc.h.
makeint.h (LOCALEDIR) [WINDOWS32}: Define to NULL if not
defined. This is needed because the MS-Windows build doesn't have
a canonical place for LOCALEDIR.
(WIN32_LEAN_AND_MEAN) [WINDOWS32]: Define, to avoid getting from
windows.h header too much stuff that could conflict with the code.
main.c <sync_mutex>: New static variable.
<switches>: Add support for "--sync-mutex" switch.
(decode_output_sync_flags): Decode the --sync-mutex= switch.
(prepare_mutex_handle_string) [WINDOWS32]: New function.
(main): Add "output-sync" to .FEATURES.
job.h (CLOSE_ON_EXEC) [WINDOWS32]: Define to call
process_noinherit.
(F_GETFD, F_SETLKW, F_WRLCK, F_UNLCK, struct flock) [WINDOWS32]:
New macros.
(RECORD_SYNC_MUTEX): New macro, a no-op for Posix platforms.
(sync_handle_t): New typedef.
job.c <sync_handle>: Change type to sync_handle_t.
(FD_NOT_EMPTY): Seek to the file's end. Suggested by Frank
Heckenbach <f.heckenbach@fh-soft.de>.
(pump_from_tmp_fd) [WINDOWS32]: Switch to_fd to binary mode for
the duration of this function, and then change back before
returning.
(start_job_command) [WINDOWS32]: Support output_sync mode on
MS-Windows. Use a system-wide mutex instead of locking
stdout/stderr. Call process_easy with two additional arguments:
child->outfd and child->errfd.
(exec_command) [WINDOWS32]: Pass two additional arguments, both
-1, to process_easy, to adjust for the changed function signature.
function.c (windows32_openpipe) [WINDOWS32]: This function now
returns an int, which is -1 if it fails and zero otherwise. It
also calls 'error' instead of 'fatal', to avoid exiting
prematurely.
(func_shell_base) [WINDOWS32]: Call perror_with_name if
windows32_openpipe fails, now that it always returns. This avoids
a compiler warning that error_prefix is not used in the MS-Windows
build.
config.h.W32.template (OUTPUT_SYNC): Define.
build_w32.bat: Add w32/compat/posixfcn.c to compilation and
linking commands.
From Frank Heckenbach <f.heckenbach@fh-soft.de>:
job.c (sync_output): Don't discard the output if
acquire_semaphore fails; instead, dump the output unsynchronized.
2013-04-27 19:20:49 +08:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
hProcess = process_init_fd (hIn, hChildOutWr, hErr);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
if (!hProcess)
|
|
|
|
|
{
|
|
|
|
|
error (NILF, _("windows32_openpipe(): process_init_fd() failed\n"));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
/* make sure that CreateProcess() has Path it needs */
|
2013-05-17 14:29:46 +08:00
|
|
|
|
sync_Path_environment ();
|
2012-03-04 08:24:20 +08:00
|
|
|
|
/* 'sync_Path_environment' may realloc 'environ', so take note of
|
2009-09-01 01:54:11 +08:00
|
|
|
|
the new value. */
|
|
|
|
|
envp = environ;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
if (! process_begin (hProcess, command_argv, envp, command_argv[0], NULL))
|
|
|
|
|
{
|
|
|
|
|
/* register process for wait */
|
|
|
|
|
process_register (hProcess);
|
1999-12-09 04:13:50 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* set the pid for returning to caller */
|
|
|
|
|
*pid_p = (pid_t) hProcess;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* set up to read data from child */
|
|
|
|
|
pipedes[0] = _open_osfhandle ((intptr_t) hChildOutRd, O_RDONLY);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* this will be closed almost right away */
|
|
|
|
|
pipedes[1] = _open_osfhandle ((intptr_t) hChildOutWr, O_APPEND);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* reap/cleanup the failed process */
|
|
|
|
|
process_cleanup (hProcess);
|
1999-12-09 04:13:50 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* close handles which were duplicated, they weren't used */
|
|
|
|
|
if (hIn != INVALID_HANDLE_VALUE)
|
|
|
|
|
CloseHandle (hIn);
|
|
|
|
|
if (hErr != INVALID_HANDLE_VALUE)
|
|
|
|
|
CloseHandle (hErr);
|
1999-12-09 04:13:50 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* close pipe handles, they won't be used */
|
|
|
|
|
CloseHandle (hChildOutRd);
|
|
|
|
|
CloseHandle (hChildOutWr);
|
1999-12-09 04:13:50 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __MSDOS__
|
1999-08-13 07:16:42 +08:00
|
|
|
|
FILE *
|
1999-03-26 12:04:42 +08:00
|
|
|
|
msdos_openpipe (int* pipedes, int *pidp, char *text)
|
|
|
|
|
{
|
|
|
|
|
FILE *fpipe=0;
|
2012-03-04 08:24:20 +08:00
|
|
|
|
/* MSDOS can't fork, but it has 'popen'. */
|
1999-03-26 12:04:42 +08: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 13:43:37 +08:00
|
|
|
|
while (isblank ((unsigned char)*text))
|
1999-03-26 12:04:42 +08:00
|
|
|
|
++text;
|
|
|
|
|
if (*text == '\0')
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (sh)
|
|
|
|
|
{
|
|
|
|
|
char buf[PATH_MAX + 7];
|
|
|
|
|
/* This makes sure $SHELL value is used by $(shell), even
|
2013-05-17 14:29:46 +08:00
|
|
|
|
though the target environment is not passed to it. */
|
1999-03-26 12:04:42 +08:00
|
|
|
|
sprintf (buf, "SHELL=%s", sh->value);
|
|
|
|
|
putenv (buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
e = errno;
|
|
|
|
|
errno = 0;
|
|
|
|
|
dos_command_running = 1;
|
|
|
|
|
dos_status = 0;
|
1999-08-01 14:05:17 +08: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 12:04:42 +08:00
|
|
|
|
fpipe = popen (text, "rt");
|
|
|
|
|
dos_command_running = 0;
|
|
|
|
|
if (!fpipe || dos_status)
|
|
|
|
|
{
|
|
|
|
|
pipedes[0] = -1;
|
|
|
|
|
*pidp = -1;
|
|
|
|
|
if (dos_status)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
errno = EINTR;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
else if (errno == 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
errno = ENOMEM;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
shell_function_completed = -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pipedes[0] = fileno (fpipe);
|
1999-08-01 14:05:17 +08:00
|
|
|
|
*pidp = 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
|
1999-03-26 12:04:42 +08: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 ...) */
|
2011-04-18 09:25:20 +08:00
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
func_shell_base (char *o, char **argv, int trim_newlines)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "This platform does not support shell\n");
|
|
|
|
|
die (EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#define func_shell 0
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
#ifndef _AMIGA
|
2011-04-18 09:25:20 +08:00
|
|
|
|
char *
|
|
|
|
|
func_shell_base (char *o, char **argv, int trim_newlines)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
char *batch_filename = NULL;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
#ifdef __MSDOS__
|
|
|
|
|
FILE *fpipe;
|
|
|
|
|
#endif
|
|
|
|
|
char **command_argv;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *error_prefix;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
char **envp;
|
|
|
|
|
int pipedes[2];
|
2010-07-09 19:10:04 +08:00
|
|
|
|
pid_t pid;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
#ifndef __MSDOS__
|
2010-08-07 16:46:06 +08:00
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
/* Reset just_print_flag. This is needed on Windows when batch files
|
|
|
|
|
are used to run the commands, because we normally refrain from
|
|
|
|
|
creating batch files under -n. */
|
|
|
|
|
int j_p_f = just_print_flag;
|
|
|
|
|
|
|
|
|
|
just_print_flag = 0;
|
|
|
|
|
#endif
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* Construct the argument list. */
|
2007-10-10 21:22:21 +08:00
|
|
|
|
command_argv = construct_command_argv (argv[0], NULL, NULL, 0,
|
|
|
|
|
&batch_filename);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (command_argv == 0)
|
2010-08-07 16:46:06 +08:00
|
|
|
|
{
|
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
just_print_flag = j_p_f;
|
|
|
|
|
#endif
|
|
|
|
|
return o;
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2012-03-04 08:24:20 +08:00
|
|
|
|
/* Using a target environment for 'shell' loses in cases like:
|
1999-03-26 12:04:42 +08:00
|
|
|
|
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 09:01:07 +08:00
|
|
|
|
calling environment.
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
See Savannah bug #10593.
|
|
|
|
|
|
2005-06-27 09:01:07 +08:00
|
|
|
|
envp = target_environment (NILF);
|
|
|
|
|
*/
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
|
|
|
|
envp = environ;
|
|
|
|
|
|
|
|
|
|
/* For error messages. */
|
2005-02-28 15:48:22 +08:00
|
|
|
|
if (reading_file && reading_file->filenm)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08: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 12:04:42 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
error_prefix = "";
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
#if defined(__MSDOS__)
|
1999-08-01 14:05:17 +08:00
|
|
|
|
fpipe = msdos_openpipe (pipedes, &pid, argv[0]);
|
|
|
|
|
if (pipedes[0] < 0)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
1999-08-01 14:05:17 +08:00
|
|
|
|
perror_with_name (error_prefix, "pipe");
|
|
|
|
|
return o;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
2007-03-20 11:02:26 +08:00
|
|
|
|
#elif defined(WINDOWS32)
|
|
|
|
|
windows32_openpipe (pipedes, &pid, command_argv, envp);
|
2010-08-07 16:46:06 +08:00
|
|
|
|
/* Restore the value of just_print_flag. */
|
|
|
|
|
just_print_flag = j_p_f;
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
if (pipedes[0] < 0)
|
|
|
|
|
{
|
2010-08-07 16:46:06 +08:00
|
|
|
|
/* Open of the pipe failed, mark as failed execution. */
|
2007-03-20 11:02:26 +08:00
|
|
|
|
shell_function_completed = -1;
|
Support --output-sync on MS-Windows.
w32/compat/posixfcn.c: New file, with emulations of Posix
functions and Posix functionality for MS-Windows.
w32/subproc/sub_proc.c: Include io.h.
(process_noinherit): New function, forces a file descriptor to not
be inherited by child processes.
(process_easy): Accept two additional arguments, and use them to
set up the standard output and standard error handles of the child
process.
w32/include/sub_proc.h (process_easy): Adjust prototype.
(process_noinherit): Add prototype.
read.c [WINDOWS32]: Include windows.h and sub_proc.h.
makeint.h (LOCALEDIR) [WINDOWS32}: Define to NULL if not
defined. This is needed because the MS-Windows build doesn't have
a canonical place for LOCALEDIR.
(WIN32_LEAN_AND_MEAN) [WINDOWS32]: Define, to avoid getting from
windows.h header too much stuff that could conflict with the code.
main.c <sync_mutex>: New static variable.
<switches>: Add support for "--sync-mutex" switch.
(decode_output_sync_flags): Decode the --sync-mutex= switch.
(prepare_mutex_handle_string) [WINDOWS32]: New function.
(main): Add "output-sync" to .FEATURES.
job.h (CLOSE_ON_EXEC) [WINDOWS32]: Define to call
process_noinherit.
(F_GETFD, F_SETLKW, F_WRLCK, F_UNLCK, struct flock) [WINDOWS32]:
New macros.
(RECORD_SYNC_MUTEX): New macro, a no-op for Posix platforms.
(sync_handle_t): New typedef.
job.c <sync_handle>: Change type to sync_handle_t.
(FD_NOT_EMPTY): Seek to the file's end. Suggested by Frank
Heckenbach <f.heckenbach@fh-soft.de>.
(pump_from_tmp_fd) [WINDOWS32]: Switch to_fd to binary mode for
the duration of this function, and then change back before
returning.
(start_job_command) [WINDOWS32]: Support output_sync mode on
MS-Windows. Use a system-wide mutex instead of locking
stdout/stderr. Call process_easy with two additional arguments:
child->outfd and child->errfd.
(exec_command) [WINDOWS32]: Pass two additional arguments, both
-1, to process_easy, to adjust for the changed function signature.
function.c (windows32_openpipe) [WINDOWS32]: This function now
returns an int, which is -1 if it fails and zero otherwise. It
also calls 'error' instead of 'fatal', to avoid exiting
prematurely.
(func_shell_base) [WINDOWS32]: Call perror_with_name if
windows32_openpipe fails, now that it always returns. This avoids
a compiler warning that error_prefix is not used in the MS-Windows
build.
config.h.W32.template (OUTPUT_SYNC): Define.
build_w32.bat: Add w32/compat/posixfcn.c to compilation and
linking commands.
From Frank Heckenbach <f.heckenbach@fh-soft.de>:
job.c (sync_output): Don't discard the output if
acquire_semaphore fails; instead, dump the output unsynchronized.
2013-04-27 19:20:49 +08:00
|
|
|
|
perror_with_name (error_prefix, "pipe");
|
2007-03-20 11:02:26 +08:00
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
else
|
2003-03-25 07:14:15 +08:00
|
|
|
|
#else
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (pipe (pipedes) < 0)
|
|
|
|
|
{
|
|
|
|
|
perror_with_name (error_prefix, "pipe");
|
|
|
|
|
return o;
|
|
|
|
|
}
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
2003-03-25 07:14:15 +08: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__ */
|
2011-11-14 10:26:00 +08:00
|
|
|
|
pid = fork ();
|
1999-03-26 12:04:42 +08:00
|
|
|
|
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-25 07:14:15 +08:00
|
|
|
|
# endif
|
|
|
|
|
#endif
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
/* We are the parent. */
|
|
|
|
|
char *buffer;
|
2006-04-01 14:36:40 +08:00
|
|
|
|
unsigned int maxlen, i;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
int cc;
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* Record the PID for reap_children. */
|
|
|
|
|
shell_function_pid = pid;
|
|
|
|
|
#ifndef __MSDOS__
|
|
|
|
|
shell_function_completed = 0;
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* Free the storage only the child needed. */
|
|
|
|
|
free (command_argv[0]);
|
2006-04-10 06:09:24 +08:00
|
|
|
|
free (command_argv);
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
2009-03-08 01:30:30 +08:00
|
|
|
|
/* Close the write side of the pipe. We test for -1, since
|
2013-05-17 14:29:46 +08:00
|
|
|
|
pipedes[1] is -1 on MS-Windows, and some versions of MS
|
|
|
|
|
libraries barf when 'close' is called with -1. */
|
2009-03-08 01:30:30 +08:00
|
|
|
|
if (pipedes[1] >= 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
close (pipedes[1]);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#endif
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* Set up and read from the pipe. */
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
maxlen = 200;
|
2006-04-10 06:09:24 +08:00
|
|
|
|
buffer = xmalloc (maxlen + 1);
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* Read from the pipe until it gets EOF. */
|
2001-06-01 11:56:50 +08:00
|
|
|
|
for (i = 0; ; i += cc)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
if (i == maxlen)
|
|
|
|
|
{
|
|
|
|
|
maxlen += 512;
|
|
|
|
|
buffer = xrealloc (buffer, maxlen + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EINTRLOOP (cc, read (pipedes[0], &buffer[i], maxlen - i));
|
|
|
|
|
if (cc <= 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2000-10-06 00:27:06 +08:00
|
|
|
|
buffer[i] = '\0';
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* Close the read side of the pipe. */
|
|
|
|
|
#ifdef __MSDOS__
|
|
|
|
|
if (fpipe)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
(void) pclose (fpipe);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#else
|
|
|
|
|
(void) close (pipedes[0]);
|
|
|
|
|
#endif
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2003-03-25 07:14:15 +08:00
|
|
|
|
/* Loop until child_handler or reap_children() sets
|
|
|
|
|
shell_function_completed to the status of our child shell. */
|
1999-03-26 12:04:42 +08:00
|
|
|
|
while (shell_function_completed == 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
reap_children (1, 0);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
if (batch_filename)
|
|
|
|
|
{
|
|
|
|
|
DB (DB_VERBOSE, (_("Cleaning up temporary batch file %s\n"),
|
|
|
|
|
batch_filename));
|
|
|
|
|
remove (batch_filename);
|
|
|
|
|
free (batch_filename);
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
shell_function_pid = 0;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* The child_handler function will set shell_function_completed
|
2013-05-17 14:29:46 +08:00
|
|
|
|
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 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (shell_function_completed == -1)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
/* This likely means that the execvp failed, so we should just
|
|
|
|
|
write the error message in the pipe from the child. */
|
|
|
|
|
fputs (buffer, stderr);
|
|
|
|
|
fflush (stderr);
|
|
|
|
|
}
|
1997-04-07 15:21:16 +08:00
|
|
|
|
else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
/* The child finished normally. Replace all newlines in its output
|
|
|
|
|
with spaces, and put that in the variable output buffer. */
|
|
|
|
|
fold_newlines (buffer, &i, trim_newlines);
|
|
|
|
|
o = variable_buffer_output (o, buffer, i);
|
|
|
|
|
}
|
1997-04-07 15:21:16 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
free (buffer);
|
|
|
|
|
}
|
1997-04-07 15:21:16 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
return o;
|
|
|
|
|
}
|
1997-04-07 15:21:16 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
#else /* _AMIGA */
|
1997-04-07 15:21:16 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* Do the Amiga version of func_shell. */
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2011-04-18 09:25:20 +08:00
|
|
|
|
char *
|
|
|
|
|
func_shell_base (char *o, char **argv, int trim_newlines)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
|
|
|
|
/* 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 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#include <dos/dos.h>
|
|
|
|
|
#include <proto/dos.h>
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
BPTR child_stdout;
|
|
|
|
|
char tmp_output[FILENAME_MAX];
|
2006-04-01 14:36:40 +08:00
|
|
|
|
unsigned int maxlen = 200, i;
|
|
|
|
|
int cc;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
char * buffer, * ptr;
|
|
|
|
|
char ** aptr;
|
|
|
|
|
int len = 0;
|
|
|
|
|
char* batch_filename = NULL;
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/* Construct the argument list. */
|
2007-10-10 21:22:21 +08:00
|
|
|
|
command_argv = construct_command_argv (argv[0], NULL, NULL, 0,
|
|
|
|
|
&batch_filename);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (command_argv == 0)
|
|
|
|
|
return o;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2000-03-27 14:54:37 +08: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 12:04:42 +08:00
|
|
|
|
strcpy (tmp_output, "t:MakeshXXXXXXXX");
|
|
|
|
|
mktemp (tmp_output);
|
|
|
|
|
child_stdout = Open (tmp_output, MODE_NEWFILE);
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
for (aptr=command_argv; *aptr; aptr++)
|
|
|
|
|
len += strlen (*aptr) + 1;
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
buffer = xmalloc (len + 1);
|
|
|
|
|
ptr = buffer;
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
for (aptr=command_argv; *aptr; aptr++)
|
|
|
|
|
{
|
|
|
|
|
strcpy (ptr, *aptr);
|
|
|
|
|
ptr += strlen (ptr) + 1;
|
|
|
|
|
*ptr ++ = ' ';
|
|
|
|
|
*ptr = 0;
|
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
ptr[-1] = '\n';
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
Execute (buffer, NULL, child_stdout);
|
|
|
|
|
free (buffer);
|
1996-03-20 22:57:41 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
Close (child_stdout);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
child_stdout = Open (tmp_output, MODE_OLDFILE);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
buffer = xmalloc (maxlen);
|
|
|
|
|
i = 0;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if (i == maxlen)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
maxlen += 512;
|
|
|
|
|
buffer = xrealloc (buffer, maxlen + 1);
|
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
cc = Read (child_stdout, &buffer[i], maxlen - i);
|
|
|
|
|
if (cc > 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
i += cc;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
} while (cc > 0);
|
|
|
|
|
|
|
|
|
|
Close (child_stdout);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2011-04-18 09:25:20 +08:00
|
|
|
|
fold_newlines (buffer, &i, trim_newlines);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
o = variable_buffer_output (o, buffer, i);
|
|
|
|
|
free (buffer);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
return o;
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#endif /* _AMIGA */
|
2011-04-18 09:25:20 +08:00
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
func_shell (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
|
|
|
|
return func_shell_base (o, argv, 1);
|
|
|
|
|
}
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#endif /* !VMS */
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
#ifdef EXPERIMENTAL
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/*
|
2013-04-30 21:37:03 +08:00
|
|
|
|
equality. Return is string-boolean, i.e., the empty string is false.
|
1999-03-26 12:04:42 +08:00
|
|
|
|
*/
|
|
|
|
|
static char *
|
2012-03-04 16:58:44 +08:00
|
|
|
|
func_eq (char *o, char **argv, char *funcname UNUSED)
|
1992-01-11 19:37:36 +08:00
|
|
|
|
{
|
1999-03-26 12:04:42 +08:00
|
|
|
|
int result = ! strcmp (argv[0], argv[1]);
|
|
|
|
|
o = variable_buffer_output (o, result ? "1" : "", result);
|
|
|
|
|
return o;
|
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
/*
|
|
|
|
|
string-boolean not operator.
|
|
|
|
|
*/
|
|
|
|
|
static char *
|
2012-03-04 16:58:44 +08:00
|
|
|
|
func_not (char *o, char **argv, char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *s = argv[0];
|
1999-03-26 12:04:42 +08:00
|
|
|
|
int result = 0;
|
1999-10-15 15:00:58 +08:00
|
|
|
|
while (isspace ((unsigned char)*s))
|
1999-03-26 12:04:42 +08:00
|
|
|
|
s++;
|
|
|
|
|
result = ! (*s);
|
|
|
|
|
o = variable_buffer_output (o, result ? "1" : "", result);
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
|
|
|
|
|
2009-07-04 19:15:14 +08:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
|
|
|
|
#define IS_ABSOLUTE(n) (n[0] && n[1] == ':')
|
|
|
|
|
#define ROOT_LEN 3
|
|
|
|
|
#else
|
|
|
|
|
#define IS_ABSOLUTE(n) (n[0] == '/')
|
|
|
|
|
#define ROOT_LEN 1
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-03-04 08:24:20 +08:00
|
|
|
|
/* Return the absolute name of file NAME which does not contain any '.',
|
|
|
|
|
'..' components nor any repeated path separators ('/'). */
|
2004-12-01 03:51:24 +08:00
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
abspath (const char *name, char *apath)
|
|
|
|
|
{
|
|
|
|
|
char *dest;
|
|
|
|
|
const char *start, *end, *apath_limit;
|
2009-07-04 19:15:14 +08:00
|
|
|
|
unsigned long root_len = ROOT_LEN;
|
2004-12-01 03:51:24 +08:00
|
|
|
|
|
|
|
|
|
if (name[0] == '\0' || apath == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2005-02-28 15:48:22 +08:00
|
|
|
|
apath_limit = apath + GET_PATH_MAX;
|
2004-12-01 03:51:24 +08:00
|
|
|
|
|
2009-07-04 19:15:14 +08:00
|
|
|
|
if (!IS_ABSOLUTE(name))
|
2004-12-01 03:51:24 +08:00
|
|
|
|
{
|
|
|
|
|
/* It is unlikely we would make it until here but just to make sure. */
|
|
|
|
|
if (!starting_directory)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
return NULL;
|
2004-12-01 03:51:24 +08:00
|
|
|
|
|
|
|
|
|
strcpy (apath, starting_directory);
|
|
|
|
|
|
2009-07-04 19:15:14 +08:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
|
|
|
|
if (IS_PATHSEP(name[0]))
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
if (IS_PATHSEP(name[1]))
|
|
|
|
|
{
|
|
|
|
|
/* A UNC. Don't prepend a drive letter. */
|
|
|
|
|
apath[0] = name[0];
|
|
|
|
|
apath[1] = name[1];
|
|
|
|
|
root_len = 2;
|
|
|
|
|
}
|
|
|
|
|
/* We have /foo, an absolute file name except for the drive
|
|
|
|
|
letter. Assume the missing drive letter is the current
|
|
|
|
|
drive, which we can get if we remove from starting_directory
|
|
|
|
|
everything past the root directory. */
|
|
|
|
|
apath[root_len] = '\0';
|
|
|
|
|
}
|
2009-07-04 19:15:14 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2004-12-01 03:51:24 +08:00
|
|
|
|
dest = strchr (apath, '\0');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-07-04 19:15:14 +08:00
|
|
|
|
strncpy (apath, name, root_len);
|
|
|
|
|
apath[root_len] = '\0';
|
|
|
|
|
dest = apath + root_len;
|
|
|
|
|
/* Get past the root, since we already copied it. */
|
|
|
|
|
name += root_len;
|
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
|
|
|
|
if (!IS_PATHSEP(apath[2]))
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
/* Convert d:foo into d:./foo and increase root_len. */
|
|
|
|
|
apath[2] = '.';
|
|
|
|
|
apath[3] = '/';
|
|
|
|
|
dest++;
|
|
|
|
|
root_len++;
|
|
|
|
|
/* strncpy above copied one character too many. */
|
|
|
|
|
name--;
|
|
|
|
|
}
|
2009-07-04 19:15:14 +08:00
|
|
|
|
else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
apath[2] = '/'; /* make sure it's a forward slash */
|
2009-07-04 19:15:14 +08:00
|
|
|
|
#endif
|
2004-12-01 03:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (start = end = name; *start != '\0'; start = end)
|
|
|
|
|
{
|
|
|
|
|
unsigned long len;
|
|
|
|
|
|
|
|
|
|
/* Skip sequence of multiple path-separators. */
|
2009-07-04 19:15:14 +08:00
|
|
|
|
while (IS_PATHSEP(*start))
|
2013-05-17 14:29:46 +08:00
|
|
|
|
++start;
|
2004-12-01 03:51:24 +08:00
|
|
|
|
|
|
|
|
|
/* Find end of path component. */
|
2009-07-04 19:15:14 +08:00
|
|
|
|
for (end = start; *end != '\0' && !IS_PATHSEP(*end); ++end)
|
2004-12-01 03:51:24 +08:00
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
len = end - start;
|
|
|
|
|
|
|
|
|
|
if (len == 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
break;
|
2004-12-01 03:51:24 +08:00
|
|
|
|
else if (len == 1 && start[0] == '.')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* nothing */;
|
2004-12-01 03:51:24 +08:00
|
|
|
|
else if (len == 2 && start[0] == '.' && start[1] == '.')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
/* Back up to previous component, ignore if at root already. */
|
|
|
|
|
if (dest > apath + root_len)
|
|
|
|
|
for (--dest; !IS_PATHSEP(dest[-1]); --dest);
|
|
|
|
|
}
|
2004-12-01 03:51:24 +08:00
|
|
|
|
else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
if (!IS_PATHSEP(dest[-1]))
|
2004-12-01 03:51:24 +08:00
|
|
|
|
*dest++ = '/';
|
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
if (dest + len >= apath_limit)
|
2004-12-01 03:51:24 +08:00
|
|
|
|
return NULL;
|
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dest = memcpy (dest, start, len);
|
2004-12-01 03:51:24 +08:00
|
|
|
|
dest += len;
|
2013-05-17 14:29:46 +08:00
|
|
|
|
*dest = '\0';
|
|
|
|
|
}
|
2004-12-01 03:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Unless it is root strip trailing separator. */
|
2009-07-04 19:15:14 +08:00
|
|
|
|
if (dest > apath + root_len && IS_PATHSEP(dest[-1]))
|
2004-12-01 03:51:24 +08:00
|
|
|
|
--dest;
|
|
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
|
|
|
|
|
|
return apath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
func_realpath (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
|
|
|
|
/* Expand the argument. */
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *p = argv[0];
|
|
|
|
|
const char *path = 0;
|
2004-12-01 03:51:24 +08:00
|
|
|
|
int doneany = 0;
|
|
|
|
|
unsigned int len = 0;
|
|
|
|
|
|
|
|
|
|
while ((path = find_next_token (&p, &len)) != 0)
|
|
|
|
|
{
|
2005-02-28 15:48:22 +08:00
|
|
|
|
if (len < GET_PATH_MAX)
|
2004-12-01 03:51:24 +08:00
|
|
|
|
{
|
2013-02-28 13:55:15 +08:00
|
|
|
|
char *rp;
|
|
|
|
|
struct stat st;
|
|
|
|
|
PATH_VAR (in);
|
|
|
|
|
PATH_VAR (out);
|
|
|
|
|
|
2004-12-01 03:51:24 +08:00
|
|
|
|
strncpy (in, path, len);
|
|
|
|
|
in[len] = '\0';
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_REALPATH
|
2013-02-28 13:55:15 +08:00
|
|
|
|
ENULLLOOP (rp, realpath (in, out));
|
2004-12-01 03:51:24 +08:00
|
|
|
|
#else
|
2013-02-28 13:55:15 +08:00
|
|
|
|
rp = abspath (in, out);
|
2004-12-01 03:51:24 +08:00
|
|
|
|
#endif
|
2013-02-28 13:55:15 +08:00
|
|
|
|
|
|
|
|
|
if (rp)
|
2004-12-01 03:51:24 +08:00
|
|
|
|
{
|
2013-02-28 13:55:15 +08:00
|
|
|
|
int r;
|
|
|
|
|
EINTRLOOP (r, stat (out, &st));
|
|
|
|
|
if (r == 0)
|
|
|
|
|
{
|
|
|
|
|
o = variable_buffer_output (o, out, strlen (out));
|
|
|
|
|
o = variable_buffer_output (o, " ", 1);
|
|
|
|
|
doneany = 1;
|
|
|
|
|
}
|
2004-12-01 03:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Kill last space. */
|
|
|
|
|
if (doneany)
|
|
|
|
|
--o;
|
|
|
|
|
|
2007-03-20 11:02:26 +08:00
|
|
|
|
return o;
|
2004-12-01 03:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-30 02:12:22 +08:00
|
|
|
|
static char *
|
|
|
|
|
func_file (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
|
|
|
|
char *fn = argv[0];
|
|
|
|
|
|
|
|
|
|
if (fn[0] == '>')
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
const char *mode = "w";
|
|
|
|
|
|
|
|
|
|
/* We are writing a file. */
|
|
|
|
|
++fn;
|
|
|
|
|
if (fn[0] == '>')
|
|
|
|
|
{
|
|
|
|
|
mode = "a";
|
|
|
|
|
++fn;
|
|
|
|
|
}
|
|
|
|
|
fn = next_token (fn);
|
|
|
|
|
|
|
|
|
|
fp = fopen (fn, mode);
|
|
|
|
|
if (fp == NULL)
|
|
|
|
|
fatal (reading_file, _("open: %s: %s"), fn, strerror (errno));
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int l = strlen (argv[1]);
|
|
|
|
|
int nl = (l == 0 || argv[1][l-1] != '\n');
|
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
if (fputs (argv[1], fp) == EOF || (nl && fputc ('\n', fp) == EOF))
|
2012-01-30 02:12:22 +08:00
|
|
|
|
fatal (reading_file, _("write: %s: %s"), fn, strerror (errno));
|
|
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
fatal (reading_file, _("Invalid file operation: %s"), fn);
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-01 03:51:24 +08:00
|
|
|
|
static char *
|
|
|
|
|
func_abspath (char *o, char **argv, const char *funcname UNUSED)
|
|
|
|
|
{
|
|
|
|
|
/* Expand the argument. */
|
2007-03-20 11:02:26 +08:00
|
|
|
|
const char *p = argv[0];
|
|
|
|
|
const char *path = 0;
|
2004-12-01 03:51:24 +08:00
|
|
|
|
int doneany = 0;
|
|
|
|
|
unsigned int len = 0;
|
|
|
|
|
|
|
|
|
|
while ((path = find_next_token (&p, &len)) != 0)
|
|
|
|
|
{
|
2005-02-28 15:48:22 +08:00
|
|
|
|
if (len < GET_PATH_MAX)
|
2004-12-01 03:51:24 +08:00
|
|
|
|
{
|
2013-02-28 13:55:15 +08:00
|
|
|
|
PATH_VAR (in);
|
|
|
|
|
PATH_VAR (out);
|
|
|
|
|
|
2004-12-01 03:51:24 +08: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 11:02:26 +08:00
|
|
|
|
return o;
|
2004-12-01 03:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
1999-07-22 14:29:02 +08: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 15:31:42 +08:00
|
|
|
|
If MAXIMUM_ARGS is 0, that means there is no maximum and all
|
|
|
|
|
comma-separated values are treated as arguments.
|
1999-07-22 14:29:02 +08: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 09:43:44 +08:00
|
|
|
|
static char *func_call (char *o, char **argv, const char *funcname);
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
2013-02-25 14:38:36 +08:00
|
|
|
|
#define FT_ENTRY(_name, _min, _max, _exp, _func) \
|
2013-02-28 13:55:15 +08:00
|
|
|
|
{ { (_func) }, STRING_SIZE_TUPLE(_name), (_min), (_max), (_exp), 0 }
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
static struct function_table_entry function_table_init[] =
|
1999-07-22 14:29:02 +08:00
|
|
|
|
{
|
2013-02-25 14:38:36 +08:00
|
|
|
|
/* Name MIN MAX EXP? Function */
|
|
|
|
|
FT_ENTRY ("abspath", 0, 1, 1, func_abspath),
|
|
|
|
|
FT_ENTRY ("addprefix", 2, 2, 1, func_addsuffix_addprefix),
|
|
|
|
|
FT_ENTRY ("addsuffix", 2, 2, 1, func_addsuffix_addprefix),
|
|
|
|
|
FT_ENTRY ("basename", 0, 1, 1, func_basename_dir),
|
|
|
|
|
FT_ENTRY ("dir", 0, 1, 1, func_basename_dir),
|
|
|
|
|
FT_ENTRY ("notdir", 0, 1, 1, func_notdir_suffix),
|
|
|
|
|
FT_ENTRY ("subst", 3, 3, 1, func_subst),
|
|
|
|
|
FT_ENTRY ("suffix", 0, 1, 1, func_notdir_suffix),
|
|
|
|
|
FT_ENTRY ("filter", 2, 2, 1, func_filter_filterout),
|
|
|
|
|
FT_ENTRY ("filter-out", 2, 2, 1, func_filter_filterout),
|
|
|
|
|
FT_ENTRY ("findstring", 2, 2, 1, func_findstring),
|
|
|
|
|
FT_ENTRY ("firstword", 0, 1, 1, func_firstword),
|
|
|
|
|
FT_ENTRY ("flavor", 0, 1, 1, func_flavor),
|
|
|
|
|
FT_ENTRY ("join", 2, 2, 1, func_join),
|
|
|
|
|
FT_ENTRY ("lastword", 0, 1, 1, func_lastword),
|
|
|
|
|
FT_ENTRY ("patsubst", 3, 3, 1, func_patsubst),
|
|
|
|
|
FT_ENTRY ("realpath", 0, 1, 1, func_realpath),
|
|
|
|
|
FT_ENTRY ("shell", 0, 1, 1, func_shell),
|
|
|
|
|
FT_ENTRY ("sort", 0, 1, 1, func_sort),
|
|
|
|
|
FT_ENTRY ("strip", 0, 1, 1, func_strip),
|
|
|
|
|
FT_ENTRY ("wildcard", 0, 1, 1, func_wildcard),
|
|
|
|
|
FT_ENTRY ("word", 2, 2, 1, func_word),
|
|
|
|
|
FT_ENTRY ("wordlist", 3, 3, 1, func_wordlist),
|
|
|
|
|
FT_ENTRY ("words", 0, 1, 1, func_words),
|
|
|
|
|
FT_ENTRY ("origin", 0, 1, 1, func_origin),
|
|
|
|
|
FT_ENTRY ("foreach", 3, 3, 0, func_foreach),
|
|
|
|
|
FT_ENTRY ("call", 1, 0, 1, func_call),
|
|
|
|
|
FT_ENTRY ("info", 0, 1, 1, func_error),
|
|
|
|
|
FT_ENTRY ("error", 0, 1, 1, func_error),
|
|
|
|
|
FT_ENTRY ("warning", 0, 1, 1, func_error),
|
|
|
|
|
FT_ENTRY ("if", 2, 3, 0, func_if),
|
|
|
|
|
FT_ENTRY ("or", 1, 0, 0, func_or),
|
|
|
|
|
FT_ENTRY ("and", 1, 0, 0, func_and),
|
|
|
|
|
FT_ENTRY ("value", 0, 1, 1, func_value),
|
|
|
|
|
FT_ENTRY ("eval", 0, 1, 1, func_eval),
|
|
|
|
|
FT_ENTRY ("file", 1, 2, 1, func_file),
|
1999-07-22 14:29:02 +08:00
|
|
|
|
#ifdef EXPERIMENTAL
|
2013-02-25 14:38:36 +08:00
|
|
|
|
FT_ENTRY ("eq", 2, 2, 1, func_eq),
|
|
|
|
|
FT_ENTRY ("not", 0, 1, 1, func_not),
|
1999-07-22 14:29:02 +08:00
|
|
|
|
#endif
|
|
|
|
|
};
|
2002-07-11 14:38:57 +08:00
|
|
|
|
|
|
|
|
|
#define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry))
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
/* These must come after the definition of function_table. */
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
|
|
|
|
static char *
|
2002-10-15 05:54:04 +08:00
|
|
|
|
expand_builtin_function (char *o, int argc, char **argv,
|
|
|
|
|
const struct function_table_entry *entry_p)
|
1999-07-22 14:29:02 +08:00
|
|
|
|
{
|
2013-02-25 14:38:36 +08:00
|
|
|
|
char *p;
|
|
|
|
|
|
2002-09-18 12:35:52 +08:00
|
|
|
|
if (argc < (int)entry_p->minimum_args)
|
2006-02-16 07:54:42 +08:00
|
|
|
|
fatal (*expanding_var,
|
2012-03-04 08:24:20 +08:00
|
|
|
|
_("insufficient number of arguments (%d) to function '%s'"),
|
1999-07-22 14:29:02 +08:00
|
|
|
|
argc, entry_p->name);
|
|
|
|
|
|
2013-02-25 14:38:36 +08:00
|
|
|
|
/* I suppose technically some function could do something with no arguments,
|
|
|
|
|
but so far no internal ones do, so just test it for all functions here
|
2000-04-22 10:11:17 +08:00
|
|
|
|
rather than in each one. We can change it later if necessary. */
|
|
|
|
|
|
2013-02-25 14:38:36 +08:00
|
|
|
|
if (!argc && !entry_p->alloc_fn)
|
2000-04-22 10:11:17 +08:00
|
|
|
|
return o;
|
|
|
|
|
|
2013-02-25 14:38:36 +08:00
|
|
|
|
if (!entry_p->fptr.func_ptr)
|
2006-02-16 07:54:42 +08:00
|
|
|
|
fatal (*expanding_var,
|
2012-03-04 08:24:20 +08:00
|
|
|
|
_("unimplemented on this platform: function '%s'"), entry_p->name);
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
2013-02-25 14:38:36 +08:00
|
|
|
|
if (!entry_p->alloc_fn)
|
|
|
|
|
return entry_p->fptr.func_ptr (o, argv, entry_p->name);
|
|
|
|
|
|
|
|
|
|
/* This function allocates memory and returns it to us.
|
|
|
|
|
Write it to the variable buffer, then free it. */
|
|
|
|
|
|
|
|
|
|
p = entry_p->fptr.alloc_func_ptr (entry_p->name, argc, argv);
|
|
|
|
|
if (p)
|
|
|
|
|
{
|
|
|
|
|
o = variable_buffer_output (o, p, strlen (p));
|
|
|
|
|
free (p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return o;
|
1999-07-22 14:29:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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-19 04:53:44 +08:00
|
|
|
|
handle_function (char **op, const char **stringp)
|
1999-07-22 14:29:02 +08:00
|
|
|
|
{
|
|
|
|
|
const struct function_table_entry *entry_p;
|
|
|
|
|
char openparen = (*stringp)[0];
|
|
|
|
|
char closeparen = openparen == '(' ? ')' : '}';
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char *beg;
|
|
|
|
|
const char *end;
|
1999-07-22 14:29:02 +08:00
|
|
|
|
int count = 0;
|
2006-11-19 04:53:44 +08:00
|
|
|
|
char *abeg = NULL;
|
1999-07-22 14:29:02 +08:00
|
|
|
|
char **argv, **argvp;
|
|
|
|
|
int nargs;
|
|
|
|
|
|
2000-01-11 15:31:42 +08:00
|
|
|
|
beg = *stringp + 1;
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
entry_p = lookup_function (beg);
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
|
|
|
|
if (!entry_p)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2000-01-11 15:31:42 +08:00
|
|
|
|
/* We found a builtin function. Find the beginning of its arguments (skip
|
|
|
|
|
whitespace after the name). */
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
2000-01-11 15:31:42 +08:00
|
|
|
|
beg = next_token (beg + entry_p->len);
|
1999-07-22 14:29:02 +08: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 15:31:42 +08:00
|
|
|
|
for (nargs=1, end=beg; *end != '\0'; ++end)
|
|
|
|
|
if (*end == ',')
|
1999-07-22 14:29:02 +08:00
|
|
|
|
++nargs;
|
2000-01-11 15:31:42 +08:00
|
|
|
|
else if (*end == openparen)
|
1999-07-22 14:29:02 +08:00
|
|
|
|
++count;
|
2000-01-11 15:31:42 +08:00
|
|
|
|
else if (*end == closeparen && --count < 0)
|
1999-07-22 14:29:02 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (count >= 0)
|
2006-02-16 07:54:42 +08:00
|
|
|
|
fatal (*expanding_var,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
_("unterminated call to function '%s': missing '%c'"),
|
|
|
|
|
entry_p->name, closeparen);
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
2000-01-11 15:31:42 +08:00
|
|
|
|
*stringp = end;
|
|
|
|
|
|
1999-07-22 14:29:02 +08:00
|
|
|
|
/* Get some memory to store the arg pointers. */
|
2006-04-10 06:09:24 +08:00
|
|
|
|
argvp = argv = alloca (sizeof (char *) * (nargs + 2));
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
2000-01-11 15:31:42 +08: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-19 04:53:44 +08:00
|
|
|
|
if (entry_p->expand_args)
|
1999-07-22 14:29:02 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char *p;
|
|
|
|
|
for (p=beg, nargs=0; p <= end; ++argvp)
|
|
|
|
|
{
|
|
|
|
|
const char *next;
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
2006-11-19 04:53:44 +08:00
|
|
|
|
++nargs;
|
2000-01-11 15:31:42 +08:00
|
|
|
|
|
2006-11-19 04:53:44 +08:00
|
|
|
|
if (nargs == entry_p->maximum_args
|
|
|
|
|
|| (! (next = find_next_argument (openparen, closeparen, p, end))))
|
|
|
|
|
next = end;
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
2006-11-19 04:53:44 +08:00
|
|
|
|
*argvp = expand_argument (p, next);
|
|
|
|
|
p = next + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int len = end - beg;
|
|
|
|
|
char *p, *aend;
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
2006-11-19 04:53:44 +08:00
|
|
|
|
abeg = xmalloc (len+1);
|
|
|
|
|
memcpy (abeg, beg, len);
|
|
|
|
|
abeg[len] = '\0';
|
|
|
|
|
aend = abeg + len;
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
2006-11-19 04:53:44 +08:00
|
|
|
|
for (p=abeg, nargs=0; p <= aend; ++argvp)
|
2000-01-11 15:31:42 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
char *next;
|
|
|
|
|
|
|
|
|
|
++nargs;
|
|
|
|
|
|
|
|
|
|
if (nargs == entry_p->maximum_args
|
|
|
|
|
|| (! (next = find_next_argument (openparen, closeparen, p, aend))))
|
|
|
|
|
next = aend;
|
|
|
|
|
|
2000-01-11 15:31:42 +08:00
|
|
|
|
*argvp = p;
|
|
|
|
|
*next = '\0';
|
2006-11-19 04:53:44 +08:00
|
|
|
|
p = next + 1;
|
2000-01-11 15:31:42 +08:00
|
|
|
|
}
|
1999-07-22 14:29:02 +08:00
|
|
|
|
}
|
2000-01-11 15:31:42 +08:00
|
|
|
|
*argvp = NULL;
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
|
|
|
|
/* Finally! Run the function... */
|
|
|
|
|
*op = expand_builtin_function (*op, nargs, argv, entry_p);
|
|
|
|
|
|
2000-01-11 15:31:42 +08:00
|
|
|
|
/* Free memory. */
|
1999-07-22 14:29:02 +08:00
|
|
|
|
if (entry_p->expand_args)
|
|
|
|
|
for (argvp=argv; *argvp != 0; ++argvp)
|
|
|
|
|
free (*argvp);
|
2012-03-04 06:12:46 +08:00
|
|
|
|
else if (abeg)
|
2006-11-19 04:53:44 +08:00
|
|
|
|
free (abeg);
|
1999-07-22 14:29:02 +08:00
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
1999-07-15 15:36:44 +08: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 12:04:42 +08:00
|
|
|
|
|
1999-07-22 14:29:02 +08:00
|
|
|
|
static char *
|
2004-02-24 21:50:19 +08:00
|
|
|
|
func_call (char *o, char **argv, const char *funcname UNUSED)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
2003-01-22 21:45:44 +08:00
|
|
|
|
static int max_args = 0;
|
1999-07-15 15:36:44 +08:00
|
|
|
|
char *fname;
|
2000-01-11 15:31:42 +08:00
|
|
|
|
char *cp;
|
1999-07-15 15:36:44 +08:00
|
|
|
|
char *body;
|
2002-05-10 11:15:07 +08:00
|
|
|
|
int flen;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
int i;
|
2003-01-22 21:45:44 +08:00
|
|
|
|
int saved_args;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
const struct function_table_entry *entry_p;
|
2002-05-10 11:15:07 +08:00
|
|
|
|
struct variable *v;
|
1999-03-26 12:04:42 +08:00
|
|
|
|
|
1999-07-15 15:36:44 +08:00
|
|
|
|
/* There is no way to define a variable with a space in the name, so strip
|
2000-01-11 15:31:42 +08:00
|
|
|
|
leading and trailing whitespace as a favor to the user. */
|
2000-03-27 14:54:37 +08:00
|
|
|
|
fname = argv[0];
|
|
|
|
|
while (*fname != '\0' && isspace ((unsigned char)*fname))
|
|
|
|
|
++fname;
|
2000-01-11 15:31:42 +08:00
|
|
|
|
|
2000-04-06 00:02:55 +08:00
|
|
|
|
cp = fname + strlen (fname) - 1;
|
2000-03-27 14:54:37 +08:00
|
|
|
|
while (cp > fname && isspace ((unsigned char)*cp))
|
2000-01-11 15:31:42 +08:00
|
|
|
|
--cp;
|
|
|
|
|
cp[1] = '\0';
|
|
|
|
|
|
|
|
|
|
/* Calling nothing is a no-op */
|
2000-03-27 14:54:37 +08:00
|
|
|
|
if (*fname == '\0')
|
|
|
|
|
return o;
|
1999-07-15 15:36:44 +08:00
|
|
|
|
|
|
|
|
|
/* Are we invoking a builtin function? */
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
entry_p = lookup_function (fname);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
if (entry_p)
|
1992-01-11 19:37:36 +08:00
|
|
|
|
{
|
2000-01-11 15:31:42 +08:00
|
|
|
|
/* How many arguments do we have? */
|
1999-07-15 15:36:44 +08:00
|
|
|
|
for (i=0; argv[i+1]; ++i)
|
2007-05-09 10:01:53 +08:00
|
|
|
|
;
|
2000-03-27 14:54:37 +08:00
|
|
|
|
return expand_builtin_function (o, i, argv+1, entry_p);
|
1999-03-26 12:04:42 +08:00
|
|
|
|
}
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2000-01-11 15:31:42 +08:00
|
|
|
|
/* Not a builtin, so the first argument is the name of a variable to be
|
2002-05-10 11:15:07 +08:00
|
|
|
|
expanded and interpreted as a function. Find it. */
|
2000-03-27 14:54:37 +08:00
|
|
|
|
flen = strlen (fname);
|
2000-01-11 15:31:42 +08:00
|
|
|
|
|
2002-05-10 11:15:07 +08:00
|
|
|
|
v = lookup_variable (fname, flen);
|
|
|
|
|
|
|
|
|
|
if (v == 0)
|
|
|
|
|
warn_undefined (fname, flen);
|
|
|
|
|
|
|
|
|
|
if (v == 0 || *v->value == '\0')
|
|
|
|
|
return o;
|
|
|
|
|
|
2006-04-10 06:09:24 +08:00
|
|
|
|
body = alloca (flen + 4);
|
2000-01-11 15:31:42 +08:00
|
|
|
|
body[0] = '$';
|
|
|
|
|
body[1] = '(';
|
|
|
|
|
memcpy (body + 2, fname, flen);
|
|
|
|
|
body[flen+2] = ')';
|
|
|
|
|
body[flen+3] = '\0';
|
1999-07-15 15:36:44 +08:00
|
|
|
|
|
|
|
|
|
/* Set up arguments $(1) .. $(N). $(0) is the function name. */
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
push_new_variable_scope ();
|
|
|
|
|
|
1999-07-15 15:36:44 +08:00
|
|
|
|
for (i=0; *argv; ++i, ++argv)
|
1999-03-26 12:04:42 +08:00
|
|
|
|
{
|
1999-07-15 15:36:44 +08:00
|
|
|
|
char num[11];
|
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
sprintf (num, "%d", i);
|
2002-04-21 03:25:54 +08:00
|
|
|
|
define_variable (num, strlen (num), *argv, o_automatic, 0);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-22 21:45:44 +08: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 15:36:44 +08:00
|
|
|
|
/* Expand the body in the context of the arguments, adding the result to
|
|
|
|
|
the variable buffer. */
|
|
|
|
|
|
2002-05-10 11:15:07 +08:00
|
|
|
|
v->exp_count = EXP_COUNT_MAX;
|
|
|
|
|
|
2003-01-22 21:45:44 +08:00
|
|
|
|
saved_args = max_args;
|
|
|
|
|
max_args = i;
|
1999-07-15 15:36:44 +08:00
|
|
|
|
o = variable_expand_string (o, body, flen+3);
|
2003-01-22 21:45:44 +08:00
|
|
|
|
max_args = saved_args;
|
1999-07-15 15:36:44 +08:00
|
|
|
|
|
2002-05-10 11:15:07 +08:00
|
|
|
|
v->exp_count = 0;
|
|
|
|
|
|
1999-03-26 12:04:42 +08:00
|
|
|
|
pop_variable_scope ();
|
1992-01-11 19:37:36 +08:00
|
|
|
|
|
2000-04-06 00:02:55 +08:00
|
|
|
|
return o + strlen (o);
|
1992-01-11 19:37:36 +08:00
|
|
|
|
}
|
2002-07-11 14:38:57 +08:00
|
|
|
|
|
2012-01-16 11:32:49 +08:00
|
|
|
|
void
|
2013-05-17 14:29:46 +08:00
|
|
|
|
define_new_function (const gmk_floc *flocp,
|
|
|
|
|
const char *name, int min, int max, int expand,
|
|
|
|
|
char *(*func)(const char *, int, char **))
|
2012-01-16 11:32:49 +08:00
|
|
|
|
{
|
2013-02-25 14:38:36 +08:00
|
|
|
|
struct function_table_entry *ent;
|
2012-01-16 11:32:49 +08:00
|
|
|
|
size_t len = strlen (name);
|
|
|
|
|
|
|
|
|
|
if (len > 255)
|
|
|
|
|
fatal (flocp, _("Function name too long: %s\n"), name);
|
|
|
|
|
if (min < 0 || min > 255)
|
2012-01-26 02:16:14 +08:00
|
|
|
|
fatal (flocp, _("Invalid minimum argument count (%d) for function %s\n"),
|
2012-01-16 11:32:49 +08:00
|
|
|
|
min, name);
|
2013-02-25 14:38:36 +08:00
|
|
|
|
if (max < 0 || max > 255 || (max && max < min))
|
2012-01-26 02:16:14 +08:00
|
|
|
|
fatal (flocp, _("Invalid maximum argument count (%d) for function %s\n"),
|
2012-01-16 11:32:49 +08:00
|
|
|
|
max, name);
|
|
|
|
|
|
2013-02-25 14:38:36 +08:00
|
|
|
|
ent = xmalloc (sizeof (struct function_table_entry));
|
2012-01-16 11:32:49 +08:00
|
|
|
|
ent->name = name;
|
|
|
|
|
ent->len = len;
|
|
|
|
|
ent->minimum_args = min;
|
|
|
|
|
ent->maximum_args = max;
|
|
|
|
|
ent->expand_args = expand ? 1 : 0;
|
2013-02-25 14:38:36 +08:00
|
|
|
|
ent->alloc_fn = 1;
|
|
|
|
|
ent->fptr.alloc_func_ptr = func;
|
2012-01-16 11:32:49 +08:00
|
|
|
|
|
|
|
|
|
hash_insert (&function_table, ent);
|
|
|
|
|
}
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
void
|
2002-10-15 05:54:04 +08:00
|
|
|
|
hash_init_function_table (void)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
|
|
|
|
hash_init (&function_table, FUNCTION_TABLE_ENTRIES * 2,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
function_table_entry_hash_1, function_table_entry_hash_2,
|
|
|
|
|
function_table_entry_hash_cmp);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
hash_load (&function_table, function_table_init,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
FUNCTION_TABLE_ENTRIES, sizeof (struct function_table_entry));
|
2002-07-11 14:38:57 +08:00
|
|
|
|
}
|