1 | /* Builtin function expansion for GNU Make.
|
---|
2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
---|
3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
|
---|
4 | Foundation, Inc.
|
---|
5 | This file is part of GNU Make.
|
---|
6 |
|
---|
7 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
8 | terms of the GNU General Public License as published by the Free Software
|
---|
9 | Foundation; either version 2, or (at your option) any later version.
|
---|
10 |
|
---|
11 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
13 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License along with
|
---|
16 | GNU Make; see the file COPYING. If not, write to the Free Software
|
---|
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
|
---|
18 |
|
---|
19 | #include "make.h"
|
---|
20 | #include "filedef.h"
|
---|
21 | #include "variable.h"
|
---|
22 | #include "dep.h"
|
---|
23 | #include "job.h"
|
---|
24 | #include "commands.h"
|
---|
25 | #include "debug.h"
|
---|
26 |
|
---|
27 | #ifdef _AMIGA
|
---|
28 | #include "amiga.h"
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #ifdef WINDOWS32 /* bird */
|
---|
32 | # include "pathstuff.h"
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #ifdef KMK_HELPERS
|
---|
36 | # include "kbuild.h"
|
---|
37 | #endif
|
---|
38 | #ifdef CONFIG_WITH_XARGS /* bird */
|
---|
39 | # ifdef HAVE_LIMITS_H
|
---|
40 | # include <limits.h>
|
---|
41 | # endif
|
---|
42 | #endif
|
---|
43 | #include <assert.h> /* bird */
|
---|
44 |
|
---|
45 |
|
---|
46 | struct function_table_entry
|
---|
47 | {
|
---|
48 | const char *name;
|
---|
49 | unsigned char len;
|
---|
50 | unsigned char minimum_args;
|
---|
51 | unsigned char maximum_args;
|
---|
52 | char expand_args;
|
---|
53 | char *(*func_ptr) (char *output, char **argv, const char *fname);
|
---|
54 | };
|
---|
55 |
|
---|
56 | static unsigned long
|
---|
57 | function_table_entry_hash_1 (const void *keyv)
|
---|
58 | {
|
---|
59 | const struct function_table_entry *key = keyv;
|
---|
60 | return_STRING_N_HASH_1 (key->name, key->len);
|
---|
61 | }
|
---|
62 |
|
---|
63 | static unsigned long
|
---|
64 | function_table_entry_hash_2 (const void *keyv)
|
---|
65 | {
|
---|
66 | const struct function_table_entry *key = keyv;
|
---|
67 | return_STRING_N_HASH_2 (key->name, key->len);
|
---|
68 | }
|
---|
69 |
|
---|
70 | static int
|
---|
71 | function_table_entry_hash_cmp (const void *xv, const void *yv)
|
---|
72 | {
|
---|
73 | const struct function_table_entry *x = xv;
|
---|
74 | const struct function_table_entry *y = yv;
|
---|
75 | int result = x->len - y->len;
|
---|
76 | if (result)
|
---|
77 | return result;
|
---|
78 | return_STRING_N_COMPARE (x->name, y->name, x->len);
|
---|
79 | }
|
---|
80 |
|
---|
81 | static struct hash_table function_table;
|
---|
82 | |
---|
83 |
|
---|
84 |
|
---|
85 | /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
|
---|
86 | each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
|
---|
87 | the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
|
---|
88 | nonzero, substitutions are done only on matches which are complete
|
---|
89 | whitespace-delimited words. */
|
---|
90 |
|
---|
91 | char *
|
---|
92 | subst_expand (char *o, const char *text, const char *subst, const char *replace,
|
---|
93 | unsigned int slen, unsigned int rlen, int by_word)
|
---|
94 | {
|
---|
95 | const char *t = text;
|
---|
96 | const char *p;
|
---|
97 |
|
---|
98 | if (slen == 0 && !by_word)
|
---|
99 | {
|
---|
100 | /* The first occurrence of "" in any string is its end. */
|
---|
101 | o = variable_buffer_output (o, t, strlen (t));
|
---|
102 | if (rlen > 0)
|
---|
103 | o = variable_buffer_output (o, replace, rlen);
|
---|
104 | return o;
|
---|
105 | }
|
---|
106 |
|
---|
107 | do
|
---|
108 | {
|
---|
109 | if (by_word && slen == 0)
|
---|
110 | /* When matching by words, the empty string should match
|
---|
111 | the end of each word, rather than the end of the whole text. */
|
---|
112 | p = end_of_token (next_token (t));
|
---|
113 | else
|
---|
114 | {
|
---|
115 | p = strstr (t, subst);
|
---|
116 | if (p == 0)
|
---|
117 | {
|
---|
118 | /* No more matches. Output everything left on the end. */
|
---|
119 | o = variable_buffer_output (o, t, strlen (t));
|
---|
120 | return o;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* Output everything before this occurrence of the string to replace. */
|
---|
125 | if (p > t)
|
---|
126 | o = variable_buffer_output (o, t, p - t);
|
---|
127 |
|
---|
128 | /* If we're substituting only by fully matched words,
|
---|
129 | or only at the ends of words, check that this case qualifies. */
|
---|
130 | if (by_word
|
---|
131 | && ((p > text && !isblank ((unsigned char)p[-1]))
|
---|
132 | || (p[slen] != '\0' && !isblank ((unsigned char)p[slen]))))
|
---|
133 | /* Struck out. Output the rest of the string that is
|
---|
134 | no longer to be replaced. */
|
---|
135 | o = variable_buffer_output (o, subst, slen);
|
---|
136 | else if (rlen > 0)
|
---|
137 | /* Output the replacement string. */
|
---|
138 | o = variable_buffer_output (o, replace, rlen);
|
---|
139 |
|
---|
140 | /* Advance T past the string to be replaced. */
|
---|
141 | t = p + slen;
|
---|
142 | } while (*t != '\0');
|
---|
143 |
|
---|
144 | return o;
|
---|
145 | }
|
---|
146 | |
---|
147 |
|
---|
148 |
|
---|
149 | /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
|
---|
150 | and replacing strings matching PATTERN with REPLACE.
|
---|
151 | If PATTERN_PERCENT is not nil, PATTERN has already been
|
---|
152 | run through find_percent, and PATTERN_PERCENT is the result.
|
---|
153 | If REPLACE_PERCENT is not nil, REPLACE has already been
|
---|
154 | run through find_percent, and REPLACE_PERCENT is the result.
|
---|
155 | Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
|
---|
156 | character _AFTER_ the %, not to the % itself.
|
---|
157 | */
|
---|
158 |
|
---|
159 | char *
|
---|
160 | patsubst_expand_pat (char *o, const char *text,
|
---|
161 | const char *pattern, const char *replace,
|
---|
162 | const char *pattern_percent, const char *replace_percent)
|
---|
163 | {
|
---|
164 | unsigned int pattern_prepercent_len, pattern_postpercent_len;
|
---|
165 | unsigned int replace_prepercent_len, replace_postpercent_len;
|
---|
166 | const char *t;
|
---|
167 | unsigned int len;
|
---|
168 | int doneany = 0;
|
---|
169 |
|
---|
170 | /* Record the length of REPLACE before and after the % so we don't have to
|
---|
171 | compute these lengths more than once. */
|
---|
172 | if (replace_percent)
|
---|
173 | {
|
---|
174 | replace_prepercent_len = replace_percent - replace - 1;
|
---|
175 | replace_postpercent_len = strlen (replace_percent);
|
---|
176 | }
|
---|
177 | else
|
---|
178 | {
|
---|
179 | replace_prepercent_len = strlen (replace);
|
---|
180 | replace_postpercent_len = 0;
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (!pattern_percent)
|
---|
184 | /* With no % in the pattern, this is just a simple substitution. */
|
---|
185 | return subst_expand (o, text, pattern, replace,
|
---|
186 | strlen (pattern), strlen (replace), 1);
|
---|
187 |
|
---|
188 | /* Record the length of PATTERN before and after the %
|
---|
189 | so we don't have to compute it more than once. */
|
---|
190 | pattern_prepercent_len = pattern_percent - pattern - 1;
|
---|
191 | pattern_postpercent_len = strlen (pattern_percent);
|
---|
192 |
|
---|
193 | while ((t = find_next_token (&text, &len)) != 0)
|
---|
194 | {
|
---|
195 | int fail = 0;
|
---|
196 |
|
---|
197 | /* Is it big enough to match? */
|
---|
198 | if (len < pattern_prepercent_len + pattern_postpercent_len)
|
---|
199 | fail = 1;
|
---|
200 |
|
---|
201 | /* Does the prefix match? */
|
---|
202 | if (!fail && pattern_prepercent_len > 0
|
---|
203 | && (*t != *pattern
|
---|
204 | || t[pattern_prepercent_len - 1] != pattern_percent[-2]
|
---|
205 | || !strneq (t + 1, pattern + 1, pattern_prepercent_len - 1)))
|
---|
206 | fail = 1;
|
---|
207 |
|
---|
208 | /* Does the suffix match? */
|
---|
209 | if (!fail && pattern_postpercent_len > 0
|
---|
210 | && (t[len - 1] != pattern_percent[pattern_postpercent_len - 1]
|
---|
211 | || t[len - pattern_postpercent_len] != *pattern_percent
|
---|
212 | || !strneq (&t[len - pattern_postpercent_len],
|
---|
213 | pattern_percent, pattern_postpercent_len - 1)))
|
---|
214 | fail = 1;
|
---|
215 |
|
---|
216 | if (fail)
|
---|
217 | /* It didn't match. Output the string. */
|
---|
218 | o = variable_buffer_output (o, t, len);
|
---|
219 | else
|
---|
220 | {
|
---|
221 | /* It matched. Output the replacement. */
|
---|
222 |
|
---|
223 | /* Output the part of the replacement before the %. */
|
---|
224 | o = variable_buffer_output (o, replace, replace_prepercent_len);
|
---|
225 |
|
---|
226 | if (replace_percent != 0)
|
---|
227 | {
|
---|
228 | /* Output the part of the matched string that
|
---|
229 | matched the % in the pattern. */
|
---|
230 | o = variable_buffer_output (o, t + pattern_prepercent_len,
|
---|
231 | len - (pattern_prepercent_len
|
---|
232 | + pattern_postpercent_len));
|
---|
233 | /* Output the part of the replacement after the %. */
|
---|
234 | o = variable_buffer_output (o, replace_percent,
|
---|
235 | replace_postpercent_len);
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | /* Output a space, but not if the replacement is "". */
|
---|
240 | if (fail || replace_prepercent_len > 0
|
---|
241 | || (replace_percent != 0 && len + replace_postpercent_len > 0))
|
---|
242 | {
|
---|
243 | o = variable_buffer_output (o, " ", 1);
|
---|
244 | doneany = 1;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | if (doneany)
|
---|
248 | /* Kill the last space. */
|
---|
249 | --o;
|
---|
250 |
|
---|
251 | return o;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
|
---|
255 | and replacing strings matching PATTERN with REPLACE.
|
---|
256 | If PATTERN_PERCENT is not nil, PATTERN has already been
|
---|
257 | run through find_percent, and PATTERN_PERCENT is the result.
|
---|
258 | If REPLACE_PERCENT is not nil, REPLACE has already been
|
---|
259 | run through find_percent, and REPLACE_PERCENT is the result.
|
---|
260 | Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
|
---|
261 | character _AFTER_ the %, not to the % itself.
|
---|
262 | */
|
---|
263 |
|
---|
264 | char *
|
---|
265 | patsubst_expand (char *o, const char *text, char *pattern, char *replace)
|
---|
266 | {
|
---|
267 | const char *pattern_percent = find_percent (pattern);
|
---|
268 | const char *replace_percent = find_percent (replace);
|
---|
269 |
|
---|
270 | /* If there's a percent in the pattern or replacement skip it. */
|
---|
271 | if (replace_percent)
|
---|
272 | ++replace_percent;
|
---|
273 | if (pattern_percent)
|
---|
274 | ++pattern_percent;
|
---|
275 |
|
---|
276 | return patsubst_expand_pat (o, text, pattern, replace,
|
---|
277 | pattern_percent, replace_percent);
|
---|
278 | }
|
---|
279 | |
---|
280 |
|
---|
281 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
282 | /* The maximum length of a function, once reached there is
|
---|
283 | it can't be function and we can skip the hash lookup drop out. */
|
---|
284 |
|
---|
285 | # ifdef KMK
|
---|
286 | # define MAX_FUNCTION_LENGTH 12
|
---|
287 | # else
|
---|
288 | # define MAX_FUNCTION_LENGTH 10
|
---|
289 | # endif
|
---|
290 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
291 |
|
---|
292 | /* Look up a function by name. */
|
---|
293 |
|
---|
294 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
295 | __inline
|
---|
296 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
297 | static const struct function_table_entry *
|
---|
298 | lookup_function (const char *s)
|
---|
299 | {
|
---|
300 | const char *e = s;
|
---|
301 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
302 | int left = MAX_FUNCTION_LENGTH;
|
---|
303 | int ch;
|
---|
304 | while (((ch = *e) >= 'a' && ch <='z') || ch == '-')
|
---|
305 | {
|
---|
306 | if (!left--)
|
---|
307 | return 0;
|
---|
308 | e++;
|
---|
309 | }
|
---|
310 | #else
|
---|
311 | while (*e && ( (*e >= 'a' && *e <= 'z') || *e == '-'))
|
---|
312 | e++;
|
---|
313 | #endif
|
---|
314 | if (*e == '\0' || isblank ((unsigned char) *e))
|
---|
315 | {
|
---|
316 | struct function_table_entry function_table_entry_key;
|
---|
317 | function_table_entry_key.name = s;
|
---|
318 | function_table_entry_key.len = e - s;
|
---|
319 |
|
---|
320 | return hash_find_item (&function_table, &function_table_entry_key);
|
---|
321 | }
|
---|
322 | return 0;
|
---|
323 | }
|
---|
324 | |
---|
325 |
|
---|
326 |
|
---|
327 | /* Return 1 if PATTERN matches STR, 0 if not. */
|
---|
328 |
|
---|
329 | int
|
---|
330 | pattern_matches (const char *pattern, const char *percent, const char *str)
|
---|
331 | {
|
---|
332 | unsigned int sfxlen, strlength;
|
---|
333 |
|
---|
334 | if (percent == 0)
|
---|
335 | {
|
---|
336 | unsigned int len = strlen (pattern) + 1;
|
---|
337 | char *new_chars = alloca (len);
|
---|
338 | memcpy (new_chars, pattern, len);
|
---|
339 | percent = find_percent (new_chars);
|
---|
340 | if (percent == 0)
|
---|
341 | return streq (new_chars, str);
|
---|
342 | pattern = new_chars;
|
---|
343 | }
|
---|
344 |
|
---|
345 | sfxlen = strlen (percent + 1);
|
---|
346 | strlength = strlen (str);
|
---|
347 |
|
---|
348 | if (strlength < (percent - pattern) + sfxlen
|
---|
349 | || !strneq (pattern, str, percent - pattern))
|
---|
350 | return 0;
|
---|
351 |
|
---|
352 | return !strcmp (percent + 1, str + (strlength - sfxlen));
|
---|
353 | }
|
---|
354 | |
---|
355 |
|
---|
356 |
|
---|
357 | /* Find the next comma or ENDPAREN (counting nested STARTPAREN and
|
---|
358 | ENDPARENtheses), starting at PTR before END. Return a pointer to
|
---|
359 | next character.
|
---|
360 |
|
---|
361 | If no next argument is found, return NULL.
|
---|
362 | */
|
---|
363 |
|
---|
364 | static char *
|
---|
365 | find_next_argument (char startparen, char endparen,
|
---|
366 | const char *ptr, const char *end)
|
---|
367 | {
|
---|
368 | int count = 0;
|
---|
369 |
|
---|
370 | for (; ptr < end; ++ptr)
|
---|
371 | if (*ptr == startparen)
|
---|
372 | ++count;
|
---|
373 |
|
---|
374 | else if (*ptr == endparen)
|
---|
375 | {
|
---|
376 | --count;
|
---|
377 | if (count < 0)
|
---|
378 | return NULL;
|
---|
379 | }
|
---|
380 |
|
---|
381 | else if (*ptr == ',' && !count)
|
---|
382 | return (char *)ptr;
|
---|
383 |
|
---|
384 | /* We didn't find anything. */
|
---|
385 | return NULL;
|
---|
386 | }
|
---|
387 | |
---|
388 |
|
---|
389 |
|
---|
390 | /* Glob-expand LINE. The returned pointer is
|
---|
391 | only good until the next call to string_glob. */
|
---|
392 |
|
---|
393 | static char *
|
---|
394 | string_glob (char *line)
|
---|
395 | {
|
---|
396 | static char *result = 0;
|
---|
397 | static unsigned int length;
|
---|
398 | struct nameseq *chain;
|
---|
399 | unsigned int idx;
|
---|
400 |
|
---|
401 | chain = multi_glob (parse_file_seq
|
---|
402 | (&line, '\0', sizeof (struct nameseq),
|
---|
403 | /* We do not want parse_file_seq to strip `./'s.
|
---|
404 | That would break examples like:
|
---|
405 | $(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
|
---|
406 | 0),
|
---|
407 | sizeof (struct nameseq));
|
---|
408 |
|
---|
409 | if (result == 0)
|
---|
410 | {
|
---|
411 | length = 100;
|
---|
412 | result = xmalloc (100);
|
---|
413 | }
|
---|
414 |
|
---|
415 | idx = 0;
|
---|
416 | while (chain != 0)
|
---|
417 | {
|
---|
418 | const char *name = chain->name;
|
---|
419 | unsigned int len = strlen (name);
|
---|
420 |
|
---|
421 | struct nameseq *next = chain->next;
|
---|
422 | free (chain);
|
---|
423 | chain = next;
|
---|
424 |
|
---|
425 | /* multi_glob will pass names without globbing metacharacters
|
---|
426 | through as is, but we want only files that actually exist. */
|
---|
427 | if (file_exists_p (name))
|
---|
428 | {
|
---|
429 | if (idx + len + 1 > length)
|
---|
430 | {
|
---|
431 | length += (len + 1) * 2;
|
---|
432 | result = xrealloc (result, length);
|
---|
433 | }
|
---|
434 | memcpy (&result[idx], name, len);
|
---|
435 | idx += len;
|
---|
436 | result[idx++] = ' ';
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | /* Kill the last space and terminate the string. */
|
---|
441 | if (idx == 0)
|
---|
442 | result[0] = '\0';
|
---|
443 | else
|
---|
444 | result[idx - 1] = '\0';
|
---|
445 |
|
---|
446 | return result;
|
---|
447 | }
|
---|
448 | |
---|
449 |
|
---|
450 | /*
|
---|
451 | Builtin functions
|
---|
452 | */
|
---|
453 |
|
---|
454 | static char *
|
---|
455 | func_patsubst (char *o, char **argv, const char *funcname UNUSED)
|
---|
456 | {
|
---|
457 | o = patsubst_expand (o, argv[2], argv[0], argv[1]);
|
---|
458 | return o;
|
---|
459 | }
|
---|
460 |
|
---|
461 |
|
---|
462 | static char *
|
---|
463 | func_join (char *o, char **argv, const char *funcname UNUSED)
|
---|
464 | {
|
---|
465 | int doneany = 0;
|
---|
466 |
|
---|
467 | /* Write each word of the first argument directly followed
|
---|
468 | by the corresponding word of the second argument.
|
---|
469 | If the two arguments have a different number of words,
|
---|
470 | the excess words are just output separated by blanks. */
|
---|
471 | const char *tp;
|
---|
472 | const char *pp;
|
---|
473 | const char *list1_iterator = argv[0];
|
---|
474 | const char *list2_iterator = argv[1];
|
---|
475 | do
|
---|
476 | {
|
---|
477 | unsigned int len1, len2;
|
---|
478 |
|
---|
479 | tp = find_next_token (&list1_iterator, &len1);
|
---|
480 | if (tp != 0)
|
---|
481 | o = variable_buffer_output (o, tp, len1);
|
---|
482 |
|
---|
483 | pp = find_next_token (&list2_iterator, &len2);
|
---|
484 | if (pp != 0)
|
---|
485 | o = variable_buffer_output (o, pp, len2);
|
---|
486 |
|
---|
487 | if (tp != 0 || pp != 0)
|
---|
488 | {
|
---|
489 | o = variable_buffer_output (o, " ", 1);
|
---|
490 | doneany = 1;
|
---|
491 | }
|
---|
492 | }
|
---|
493 | while (tp != 0 || pp != 0);
|
---|
494 | if (doneany)
|
---|
495 | /* Kill the last blank. */
|
---|
496 | --o;
|
---|
497 |
|
---|
498 | return o;
|
---|
499 | }
|
---|
500 |
|
---|
501 |
|
---|
502 | static char *
|
---|
503 | func_origin (char *o, char **argv, const char *funcname UNUSED)
|
---|
504 | {
|
---|
505 | /* Expand the argument. */
|
---|
506 | struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
---|
507 | if (v == 0)
|
---|
508 | o = variable_buffer_output (o, "undefined", 9);
|
---|
509 | else
|
---|
510 | switch (v->origin)
|
---|
511 | {
|
---|
512 | default:
|
---|
513 | case o_invalid:
|
---|
514 | abort ();
|
---|
515 | break;
|
---|
516 | case o_default:
|
---|
517 | o = variable_buffer_output (o, "default", 7);
|
---|
518 | break;
|
---|
519 | case o_env:
|
---|
520 | o = variable_buffer_output (o, "environment", 11);
|
---|
521 | break;
|
---|
522 | case o_file:
|
---|
523 | o = variable_buffer_output (o, "file", 4);
|
---|
524 | break;
|
---|
525 | case o_env_override:
|
---|
526 | o = variable_buffer_output (o, "environment override", 20);
|
---|
527 | break;
|
---|
528 | case o_command:
|
---|
529 | o = variable_buffer_output (o, "command line", 12);
|
---|
530 | break;
|
---|
531 | case o_override:
|
---|
532 | o = variable_buffer_output (o, "override", 8);
|
---|
533 | break;
|
---|
534 | case o_automatic:
|
---|
535 | o = variable_buffer_output (o, "automatic", 9);
|
---|
536 | break;
|
---|
537 | }
|
---|
538 |
|
---|
539 | return o;
|
---|
540 | }
|
---|
541 |
|
---|
542 | static char *
|
---|
543 | func_flavor (char *o, char **argv, const char *funcname UNUSED)
|
---|
544 | {
|
---|
545 | struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
---|
546 |
|
---|
547 | if (v == 0)
|
---|
548 | o = variable_buffer_output (o, "undefined", 9);
|
---|
549 | else
|
---|
550 | if (v->recursive)
|
---|
551 | o = variable_buffer_output (o, "recursive", 9);
|
---|
552 | else
|
---|
553 | o = variable_buffer_output (o, "simple", 6);
|
---|
554 |
|
---|
555 | return o;
|
---|
556 | }
|
---|
557 |
|
---|
558 | #ifdef VMS
|
---|
559 | # define IS_PATHSEP(c) ((c) == ']')
|
---|
560 | #else
|
---|
561 | # ifdef HAVE_DOS_PATHS
|
---|
562 | # define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
|
---|
563 | # else
|
---|
564 | # define IS_PATHSEP(c) ((c) == '/')
|
---|
565 | # endif
|
---|
566 | #endif
|
---|
567 |
|
---|
568 |
|
---|
569 | static char *
|
---|
570 | func_notdir_suffix (char *o, char **argv, const char *funcname)
|
---|
571 | {
|
---|
572 | /* Expand the argument. */
|
---|
573 | const char *list_iterator = argv[0];
|
---|
574 | const char *p2;
|
---|
575 | int doneany =0;
|
---|
576 | unsigned int len=0;
|
---|
577 |
|
---|
578 | int is_suffix = streq (funcname, "suffix");
|
---|
579 | int is_notdir = !is_suffix;
|
---|
580 | while ((p2 = find_next_token (&list_iterator, &len)) != 0)
|
---|
581 | {
|
---|
582 | const char *p = p2 + len;
|
---|
583 |
|
---|
584 |
|
---|
585 | while (p >= p2 && (!is_suffix || *p != '.'))
|
---|
586 | {
|
---|
587 | if (IS_PATHSEP (*p))
|
---|
588 | break;
|
---|
589 | --p;
|
---|
590 | }
|
---|
591 |
|
---|
592 | if (p >= p2)
|
---|
593 | {
|
---|
594 | if (is_notdir)
|
---|
595 | ++p;
|
---|
596 | else if (*p != '.')
|
---|
597 | continue;
|
---|
598 | o = variable_buffer_output (o, p, len - (p - p2));
|
---|
599 | }
|
---|
600 | #ifdef HAVE_DOS_PATHS
|
---|
601 | /* Handle the case of "d:foo/bar". */
|
---|
602 | else if (streq (funcname, "notdir") && p2[0] && p2[1] == ':')
|
---|
603 | {
|
---|
604 | p = p2 + 2;
|
---|
605 | o = variable_buffer_output (o, p, len - (p - p2));
|
---|
606 | }
|
---|
607 | #endif
|
---|
608 | else if (is_notdir)
|
---|
609 | o = variable_buffer_output (o, p2, len);
|
---|
610 |
|
---|
611 | if (is_notdir || p >= p2)
|
---|
612 | {
|
---|
613 | o = variable_buffer_output (o, " ", 1);
|
---|
614 | doneany = 1;
|
---|
615 | }
|
---|
616 | }
|
---|
617 |
|
---|
618 | if (doneany)
|
---|
619 | /* Kill last space. */
|
---|
620 | --o;
|
---|
621 |
|
---|
622 | return o;
|
---|
623 | }
|
---|
624 |
|
---|
625 |
|
---|
626 | static char *
|
---|
627 | func_basename_dir (char *o, char **argv, const char *funcname)
|
---|
628 | {
|
---|
629 | /* Expand the argument. */
|
---|
630 | const char *p3 = argv[0];
|
---|
631 | const char *p2;
|
---|
632 | int doneany=0;
|
---|
633 | unsigned int len=0;
|
---|
634 |
|
---|
635 | int is_basename= streq (funcname, "basename");
|
---|
636 | int is_dir= !is_basename;
|
---|
637 |
|
---|
638 | while ((p2 = find_next_token (&p3, &len)) != 0)
|
---|
639 | {
|
---|
640 | const char *p = p2 + len;
|
---|
641 | while (p >= p2 && (!is_basename || *p != '.'))
|
---|
642 | {
|
---|
643 | if (IS_PATHSEP (*p))
|
---|
644 | break;
|
---|
645 | --p;
|
---|
646 | }
|
---|
647 |
|
---|
648 | if (p >= p2 && (is_dir))
|
---|
649 | o = variable_buffer_output (o, p2, ++p - p2);
|
---|
650 | else if (p >= p2 && (*p == '.'))
|
---|
651 | o = variable_buffer_output (o, p2, p - p2);
|
---|
652 | #ifdef HAVE_DOS_PATHS
|
---|
653 | /* Handle the "d:foobar" case */
|
---|
654 | else if (p2[0] && p2[1] == ':' && is_dir)
|
---|
655 | o = variable_buffer_output (o, p2, 2);
|
---|
656 | #endif
|
---|
657 | else if (is_dir)
|
---|
658 | #ifdef VMS
|
---|
659 | o = variable_buffer_output (o, "[]", 2);
|
---|
660 | #else
|
---|
661 | #ifndef _AMIGA
|
---|
662 | o = variable_buffer_output (o, "./", 2);
|
---|
663 | #else
|
---|
664 | ; /* Just a nop... */
|
---|
665 | #endif /* AMIGA */
|
---|
666 | #endif /* !VMS */
|
---|
667 | else
|
---|
668 | /* The entire name is the basename. */
|
---|
669 | o = variable_buffer_output (o, p2, len);
|
---|
670 |
|
---|
671 | o = variable_buffer_output (o, " ", 1);
|
---|
672 | doneany = 1;
|
---|
673 | }
|
---|
674 |
|
---|
675 | if (doneany)
|
---|
676 | /* Kill last space. */
|
---|
677 | --o;
|
---|
678 |
|
---|
679 | return o;
|
---|
680 | }
|
---|
681 |
|
---|
682 | static char *
|
---|
683 | func_addsuffix_addprefix (char *o, char **argv, const char *funcname)
|
---|
684 | {
|
---|
685 | int fixlen = strlen (argv[0]);
|
---|
686 | const char *list_iterator = argv[1];
|
---|
687 | int is_addprefix = streq (funcname, "addprefix");
|
---|
688 | int is_addsuffix = !is_addprefix;
|
---|
689 |
|
---|
690 | int doneany = 0;
|
---|
691 | const char *p;
|
---|
692 | unsigned int len;
|
---|
693 |
|
---|
694 | while ((p = find_next_token (&list_iterator, &len)) != 0)
|
---|
695 | {
|
---|
696 | if (is_addprefix)
|
---|
697 | o = variable_buffer_output (o, argv[0], fixlen);
|
---|
698 | o = variable_buffer_output (o, p, len);
|
---|
699 | if (is_addsuffix)
|
---|
700 | o = variable_buffer_output (o, argv[0], fixlen);
|
---|
701 | o = variable_buffer_output (o, " ", 1);
|
---|
702 | doneany = 1;
|
---|
703 | }
|
---|
704 |
|
---|
705 | if (doneany)
|
---|
706 | /* Kill last space. */
|
---|
707 | --o;
|
---|
708 |
|
---|
709 | return o;
|
---|
710 | }
|
---|
711 |
|
---|
712 | static char *
|
---|
713 | func_subst (char *o, char **argv, const char *funcname UNUSED)
|
---|
714 | {
|
---|
715 | o = subst_expand (o, argv[2], argv[0], argv[1], strlen (argv[0]),
|
---|
716 | strlen (argv[1]), 0);
|
---|
717 |
|
---|
718 | return o;
|
---|
719 | }
|
---|
720 |
|
---|
721 |
|
---|
722 | static char *
|
---|
723 | func_firstword (char *o, char **argv, const char *funcname UNUSED)
|
---|
724 | {
|
---|
725 | unsigned int i;
|
---|
726 | const char *words = argv[0]; /* Use a temp variable for find_next_token */
|
---|
727 | const char *p = find_next_token (&words, &i);
|
---|
728 |
|
---|
729 | if (p != 0)
|
---|
730 | o = variable_buffer_output (o, p, i);
|
---|
731 |
|
---|
732 | return o;
|
---|
733 | }
|
---|
734 |
|
---|
735 | static char *
|
---|
736 | func_lastword (char *o, char **argv, const char *funcname UNUSED)
|
---|
737 | {
|
---|
738 | unsigned int i;
|
---|
739 | const char *words = argv[0]; /* Use a temp variable for find_next_token */
|
---|
740 | const char *p = NULL;
|
---|
741 | const char *t;
|
---|
742 |
|
---|
743 | while ((t = find_next_token (&words, &i)))
|
---|
744 | p = t;
|
---|
745 |
|
---|
746 | if (p != 0)
|
---|
747 | o = variable_buffer_output (o, p, i);
|
---|
748 |
|
---|
749 | return o;
|
---|
750 | }
|
---|
751 |
|
---|
752 | static char *
|
---|
753 | func_words (char *o, char **argv, const char *funcname UNUSED)
|
---|
754 | {
|
---|
755 | int i = 0;
|
---|
756 | const char *word_iterator = argv[0];
|
---|
757 | char buf[20];
|
---|
758 |
|
---|
759 | while (find_next_token (&word_iterator, (unsigned int *) 0) != 0)
|
---|
760 | ++i;
|
---|
761 |
|
---|
762 | sprintf (buf, "%d", i);
|
---|
763 | o = variable_buffer_output (o, buf, strlen (buf));
|
---|
764 |
|
---|
765 | return o;
|
---|
766 | }
|
---|
767 |
|
---|
768 | /* Set begpp to point to the first non-whitespace character of the string,
|
---|
769 | * and endpp to point to the last non-whitespace character of the string.
|
---|
770 | * If the string is empty or contains nothing but whitespace, endpp will be
|
---|
771 | * begpp-1.
|
---|
772 | */
|
---|
773 | char *
|
---|
774 | strip_whitespace (const char **begpp, const char **endpp)
|
---|
775 | {
|
---|
776 | while (*begpp <= *endpp && isspace ((unsigned char)**begpp))
|
---|
777 | (*begpp) ++;
|
---|
778 | while (*endpp >= *begpp && isspace ((unsigned char)**endpp))
|
---|
779 | (*endpp) --;
|
---|
780 | return (char *)*begpp;
|
---|
781 | }
|
---|
782 |
|
---|
783 | static void
|
---|
784 | check_numeric (const char *s, const char *msg)
|
---|
785 | {
|
---|
786 | const char *end = s + strlen (s) - 1;
|
---|
787 | const char *beg = s;
|
---|
788 | strip_whitespace (&s, &end);
|
---|
789 |
|
---|
790 | for (; s <= end; ++s)
|
---|
791 | if (!ISDIGIT (*s)) /* ISDIGIT only evals its arg once: see make.h. */
|
---|
792 | break;
|
---|
793 |
|
---|
794 | if (s <= end || end - beg < 0)
|
---|
795 | fatal (*expanding_var, "%s: '%s'", msg, beg);
|
---|
796 | }
|
---|
797 |
|
---|
798 |
|
---|
799 |
|
---|
800 | static char *
|
---|
801 | func_word (char *o, char **argv, const char *funcname UNUSED)
|
---|
802 | {
|
---|
803 | const char *end_p;
|
---|
804 | const char *p;
|
---|
805 | int i;
|
---|
806 |
|
---|
807 | /* Check the first argument. */
|
---|
808 | check_numeric (argv[0], _("non-numeric first argument to `word' function"));
|
---|
809 | i = atoi (argv[0]);
|
---|
810 |
|
---|
811 | if (i == 0)
|
---|
812 | fatal (*expanding_var,
|
---|
813 | _("first argument to `word' function must be greater than 0"));
|
---|
814 |
|
---|
815 | end_p = argv[1];
|
---|
816 | while ((p = find_next_token (&end_p, 0)) != 0)
|
---|
817 | if (--i == 0)
|
---|
818 | break;
|
---|
819 |
|
---|
820 | if (i == 0)
|
---|
821 | o = variable_buffer_output (o, p, end_p - p);
|
---|
822 |
|
---|
823 | return o;
|
---|
824 | }
|
---|
825 |
|
---|
826 | static char *
|
---|
827 | func_wordlist (char *o, char **argv, const char *funcname UNUSED)
|
---|
828 | {
|
---|
829 | int start, count;
|
---|
830 |
|
---|
831 | /* Check the arguments. */
|
---|
832 | check_numeric (argv[0],
|
---|
833 | _("non-numeric first argument to `wordlist' function"));
|
---|
834 | check_numeric (argv[1],
|
---|
835 | _("non-numeric second argument to `wordlist' function"));
|
---|
836 |
|
---|
837 | start = atoi (argv[0]);
|
---|
838 | if (start < 1)
|
---|
839 | fatal (*expanding_var,
|
---|
840 | "invalid first argument to `wordlist' function: `%d'", start);
|
---|
841 |
|
---|
842 | count = atoi (argv[1]) - start + 1;
|
---|
843 |
|
---|
844 | if (count > 0)
|
---|
845 | {
|
---|
846 | const char *p;
|
---|
847 | const char *end_p = argv[2];
|
---|
848 |
|
---|
849 | /* Find the beginning of the "start"th word. */
|
---|
850 | while (((p = find_next_token (&end_p, 0)) != 0) && --start)
|
---|
851 | ;
|
---|
852 |
|
---|
853 | if (p)
|
---|
854 | {
|
---|
855 | /* Find the end of the "count"th word from start. */
|
---|
856 | while (--count && (find_next_token (&end_p, 0) != 0))
|
---|
857 | ;
|
---|
858 |
|
---|
859 | /* Return the stuff in the middle. */
|
---|
860 | o = variable_buffer_output (o, p, end_p - p);
|
---|
861 | }
|
---|
862 | }
|
---|
863 |
|
---|
864 | return o;
|
---|
865 | }
|
---|
866 |
|
---|
867 | static char *
|
---|
868 | func_findstring (char *o, char **argv, const char *funcname UNUSED)
|
---|
869 | {
|
---|
870 | /* Find the first occurrence of the first string in the second. */
|
---|
871 | if (strstr (argv[1], argv[0]) != 0)
|
---|
872 | o = variable_buffer_output (o, argv[0], strlen (argv[0]));
|
---|
873 |
|
---|
874 | return o;
|
---|
875 | }
|
---|
876 |
|
---|
877 | static char *
|
---|
878 | func_foreach (char *o, char **argv, const char *funcname UNUSED)
|
---|
879 | {
|
---|
880 | /* expand only the first two. */
|
---|
881 | char *varname = expand_argument (argv[0], NULL);
|
---|
882 | char *list = expand_argument (argv[1], NULL);
|
---|
883 | const char *body = argv[2];
|
---|
884 |
|
---|
885 | int doneany = 0;
|
---|
886 | const char *list_iterator = list;
|
---|
887 | const char *p;
|
---|
888 | unsigned int len;
|
---|
889 | struct variable *var;
|
---|
890 |
|
---|
891 | push_new_variable_scope ();
|
---|
892 | var = define_variable (varname, strlen (varname), "", o_automatic, 0);
|
---|
893 |
|
---|
894 | /* loop through LIST, put the value in VAR and expand BODY */
|
---|
895 | while ((p = find_next_token (&list_iterator, &len)) != 0)
|
---|
896 | {
|
---|
897 | char *result = 0;
|
---|
898 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
899 | if (len >= (unsigned int)var->value_alloc_len)
|
---|
900 | {
|
---|
901 | free (var->value);
|
---|
902 | var->value_alloc_len = (len + 32) & ~31;
|
---|
903 | var->value = xmalloc (var->value_alloc_len);
|
---|
904 | }
|
---|
905 | memcpy (var->value, p, len);
|
---|
906 | var->value[len] = '\0';
|
---|
907 | var->value_length = len;
|
---|
908 | #else
|
---|
909 | free (var->value);
|
---|
910 | var->value = savestring (p, len);
|
---|
911 | #endif
|
---|
912 |
|
---|
913 | result = allocated_variable_expand (body);
|
---|
914 |
|
---|
915 | o = variable_buffer_output (o, result, strlen (result));
|
---|
916 | o = variable_buffer_output (o, " ", 1);
|
---|
917 | doneany = 1;
|
---|
918 | free (result);
|
---|
919 | }
|
---|
920 |
|
---|
921 | if (doneany)
|
---|
922 | /* Kill the last space. */
|
---|
923 | --o;
|
---|
924 |
|
---|
925 | pop_variable_scope ();
|
---|
926 | free (varname);
|
---|
927 | free (list);
|
---|
928 |
|
---|
929 | return o;
|
---|
930 | }
|
---|
931 |
|
---|
932 | struct a_word
|
---|
933 | {
|
---|
934 | struct a_word *next;
|
---|
935 | struct a_word *chain;
|
---|
936 | char *str;
|
---|
937 | int length;
|
---|
938 | int matched;
|
---|
939 | };
|
---|
940 |
|
---|
941 | static unsigned long
|
---|
942 | a_word_hash_1 (const void *key)
|
---|
943 | {
|
---|
944 | return_STRING_HASH_1 (((struct a_word const *) key)->str);
|
---|
945 | }
|
---|
946 |
|
---|
947 | static unsigned long
|
---|
948 | a_word_hash_2 (const void *key)
|
---|
949 | {
|
---|
950 | return_STRING_HASH_2 (((struct a_word const *) key)->str);
|
---|
951 | }
|
---|
952 |
|
---|
953 | static int
|
---|
954 | a_word_hash_cmp (const void *x, const void *y)
|
---|
955 | {
|
---|
956 | int result = ((struct a_word const *) x)->length - ((struct a_word const *) y)->length;
|
---|
957 | if (result)
|
---|
958 | return result;
|
---|
959 | return_STRING_COMPARE (((struct a_word const *) x)->str,
|
---|
960 | ((struct a_word const *) y)->str);
|
---|
961 | }
|
---|
962 |
|
---|
963 | struct a_pattern
|
---|
964 | {
|
---|
965 | struct a_pattern *next;
|
---|
966 | char *str;
|
---|
967 | char *percent;
|
---|
968 | int length;
|
---|
969 | int save_c;
|
---|
970 | };
|
---|
971 |
|
---|
972 | static char *
|
---|
973 | func_filter_filterout (char *o, char **argv, const char *funcname)
|
---|
974 | {
|
---|
975 | struct a_word *wordhead;
|
---|
976 | struct a_word **wordtail;
|
---|
977 | struct a_word *wp;
|
---|
978 | struct a_pattern *pathead;
|
---|
979 | struct a_pattern **pattail;
|
---|
980 | struct a_pattern *pp;
|
---|
981 |
|
---|
982 | struct hash_table a_word_table;
|
---|
983 | int is_filter = streq (funcname, "filter");
|
---|
984 | const char *pat_iterator = argv[0];
|
---|
985 | const char *word_iterator = argv[1];
|
---|
986 | int literals = 0;
|
---|
987 | int words = 0;
|
---|
988 | int hashing = 0;
|
---|
989 | char *p;
|
---|
990 | unsigned int len;
|
---|
991 |
|
---|
992 | /* Chop ARGV[0] up into patterns to match against the words. */
|
---|
993 |
|
---|
994 | pattail = &pathead;
|
---|
995 | while ((p = find_next_token (&pat_iterator, &len)) != 0)
|
---|
996 | {
|
---|
997 | struct a_pattern *pat = alloca (sizeof (struct a_pattern));
|
---|
998 |
|
---|
999 | *pattail = pat;
|
---|
1000 | pattail = &pat->next;
|
---|
1001 |
|
---|
1002 | if (*pat_iterator != '\0')
|
---|
1003 | ++pat_iterator;
|
---|
1004 |
|
---|
1005 | pat->str = p;
|
---|
1006 | pat->length = len;
|
---|
1007 | pat->save_c = p[len];
|
---|
1008 | p[len] = '\0';
|
---|
1009 | pat->percent = find_percent (p);
|
---|
1010 | if (pat->percent == 0)
|
---|
1011 | literals++;
|
---|
1012 | }
|
---|
1013 | *pattail = 0;
|
---|
1014 |
|
---|
1015 | /* Chop ARGV[1] up into words to match against the patterns. */
|
---|
1016 |
|
---|
1017 | wordtail = &wordhead;
|
---|
1018 | while ((p = find_next_token (&word_iterator, &len)) != 0)
|
---|
1019 | {
|
---|
1020 | struct a_word *word = alloca (sizeof (struct a_word));
|
---|
1021 |
|
---|
1022 | *wordtail = word;
|
---|
1023 | wordtail = &word->next;
|
---|
1024 |
|
---|
1025 | if (*word_iterator != '\0')
|
---|
1026 | ++word_iterator;
|
---|
1027 |
|
---|
1028 | p[len] = '\0';
|
---|
1029 | word->str = p;
|
---|
1030 | word->length = len;
|
---|
1031 | word->matched = 0;
|
---|
1032 | word->chain = 0;
|
---|
1033 | words++;
|
---|
1034 | }
|
---|
1035 | *wordtail = 0;
|
---|
1036 |
|
---|
1037 | /* Only use a hash table if arg list lengths justifies the cost. */
|
---|
1038 | hashing = (literals >= 2 && (literals * words) >= 10);
|
---|
1039 | if (hashing)
|
---|
1040 | {
|
---|
1041 | hash_init (&a_word_table, words, a_word_hash_1, a_word_hash_2,
|
---|
1042 | a_word_hash_cmp);
|
---|
1043 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
1044 | {
|
---|
1045 | struct a_word *owp = hash_insert (&a_word_table, wp);
|
---|
1046 | if (owp)
|
---|
1047 | wp->chain = owp;
|
---|
1048 | }
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | if (words)
|
---|
1052 | {
|
---|
1053 | int doneany = 0;
|
---|
1054 |
|
---|
1055 | /* Run each pattern through the words, killing words. */
|
---|
1056 | for (pp = pathead; pp != 0; pp = pp->next)
|
---|
1057 | {
|
---|
1058 | if (pp->percent)
|
---|
1059 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
1060 | wp->matched |= pattern_matches (pp->str, pp->percent, wp->str);
|
---|
1061 | else if (hashing)
|
---|
1062 | {
|
---|
1063 | struct a_word a_word_key;
|
---|
1064 | a_word_key.str = pp->str;
|
---|
1065 | a_word_key.length = pp->length;
|
---|
1066 | wp = hash_find_item (&a_word_table, &a_word_key);
|
---|
1067 | while (wp)
|
---|
1068 | {
|
---|
1069 | wp->matched |= 1;
|
---|
1070 | wp = wp->chain;
|
---|
1071 | }
|
---|
1072 | }
|
---|
1073 | else
|
---|
1074 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
1075 | wp->matched |= (wp->length == pp->length
|
---|
1076 | && strneq (pp->str, wp->str, wp->length));
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | /* Output the words that matched (or didn't, for filter-out). */
|
---|
1080 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
1081 | if (is_filter ? wp->matched : !wp->matched)
|
---|
1082 | {
|
---|
1083 | o = variable_buffer_output (o, wp->str, strlen (wp->str));
|
---|
1084 | o = variable_buffer_output (o, " ", 1);
|
---|
1085 | doneany = 1;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | if (doneany)
|
---|
1089 | /* Kill the last space. */
|
---|
1090 | --o;
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | for (pp = pathead; pp != 0; pp = pp->next)
|
---|
1094 | pp->str[pp->length] = pp->save_c;
|
---|
1095 |
|
---|
1096 | if (hashing)
|
---|
1097 | hash_free (&a_word_table, 0);
|
---|
1098 |
|
---|
1099 | return o;
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 |
|
---|
1103 | static char *
|
---|
1104 | func_strip (char *o, char **argv, const char *funcname UNUSED)
|
---|
1105 | {
|
---|
1106 | const char *p = argv[0];
|
---|
1107 | int doneany = 0;
|
---|
1108 |
|
---|
1109 | while (*p != '\0')
|
---|
1110 | {
|
---|
1111 | int i=0;
|
---|
1112 | const char *word_start;
|
---|
1113 |
|
---|
1114 | while (isspace ((unsigned char)*p))
|
---|
1115 | ++p;
|
---|
1116 | word_start = p;
|
---|
1117 | for (i=0; *p != '\0' && !isspace ((unsigned char)*p); ++p, ++i)
|
---|
1118 | {}
|
---|
1119 | if (!i)
|
---|
1120 | break;
|
---|
1121 | o = variable_buffer_output (o, word_start, i);
|
---|
1122 | o = variable_buffer_output (o, " ", 1);
|
---|
1123 | doneany = 1;
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | if (doneany)
|
---|
1127 | /* Kill the last space. */
|
---|
1128 | --o;
|
---|
1129 |
|
---|
1130 | return o;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | /*
|
---|
1134 | Print a warning or fatal message.
|
---|
1135 | */
|
---|
1136 | static char *
|
---|
1137 | func_error (char *o, char **argv, const char *funcname)
|
---|
1138 | {
|
---|
1139 | char **argvp;
|
---|
1140 | char *msg, *p;
|
---|
1141 | int len;
|
---|
1142 |
|
---|
1143 | /* The arguments will be broken on commas. Rather than create yet
|
---|
1144 | another special case where function arguments aren't broken up,
|
---|
1145 | just create a format string that puts them back together. */
|
---|
1146 | for (len=0, argvp=argv; *argvp != 0; ++argvp)
|
---|
1147 | len += strlen (*argvp) + 2;
|
---|
1148 |
|
---|
1149 | p = msg = alloca (len + 1);
|
---|
1150 |
|
---|
1151 | for (argvp=argv; argvp[1] != 0; ++argvp)
|
---|
1152 | {
|
---|
1153 | strcpy (p, *argvp);
|
---|
1154 | p += strlen (*argvp);
|
---|
1155 | *(p++) = ',';
|
---|
1156 | *(p++) = ' ';
|
---|
1157 | }
|
---|
1158 | strcpy (p, *argvp);
|
---|
1159 |
|
---|
1160 | switch (*funcname) {
|
---|
1161 | case 'e':
|
---|
1162 | fatal (reading_file, "%s", msg);
|
---|
1163 |
|
---|
1164 | case 'w':
|
---|
1165 | error (reading_file, "%s", msg);
|
---|
1166 | break;
|
---|
1167 |
|
---|
1168 | case 'i':
|
---|
1169 | printf ("%s\n", msg);
|
---|
1170 | fflush(stdout);
|
---|
1171 | break;
|
---|
1172 |
|
---|
1173 | default:
|
---|
1174 | fatal (*expanding_var, "Internal error: func_error: '%s'", funcname);
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | /* The warning function expands to the empty string. */
|
---|
1178 | return o;
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 |
|
---|
1182 | /*
|
---|
1183 | chop argv[0] into words, and sort them.
|
---|
1184 | */
|
---|
1185 | static char *
|
---|
1186 | func_sort (char *o, char **argv, const char *funcname UNUSED)
|
---|
1187 | {
|
---|
1188 | const char *t;
|
---|
1189 | char **words;
|
---|
1190 | int wordi;
|
---|
1191 | char *p;
|
---|
1192 | unsigned int len;
|
---|
1193 | int i;
|
---|
1194 |
|
---|
1195 | /* Find the maximum number of words we'll have. */
|
---|
1196 | t = argv[0];
|
---|
1197 | wordi = 1;
|
---|
1198 | while (*t != '\0')
|
---|
1199 | {
|
---|
1200 | char c = *(t++);
|
---|
1201 |
|
---|
1202 | if (! isspace ((unsigned char)c))
|
---|
1203 | continue;
|
---|
1204 |
|
---|
1205 | ++wordi;
|
---|
1206 |
|
---|
1207 | while (isspace ((unsigned char)*t))
|
---|
1208 | ++t;
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | words = xmalloc (wordi * sizeof (char *));
|
---|
1212 |
|
---|
1213 | /* Now assign pointers to each string in the array. */
|
---|
1214 | t = argv[0];
|
---|
1215 | wordi = 0;
|
---|
1216 | while ((p = find_next_token (&t, &len)) != 0)
|
---|
1217 | {
|
---|
1218 | ++t;
|
---|
1219 | p[len] = '\0';
|
---|
1220 | words[wordi++] = p;
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | if (wordi)
|
---|
1224 | {
|
---|
1225 | /* Now sort the list of words. */
|
---|
1226 | qsort (words, wordi, sizeof (char *), alpha_compare);
|
---|
1227 |
|
---|
1228 | /* Now write the sorted list, uniquified. */
|
---|
1229 | #ifdef CONFIG_WITH_RSORT
|
---|
1230 | if (strcmp (funcname, "rsort"))
|
---|
1231 | {
|
---|
1232 | /* sort */
|
---|
1233 | #endif
|
---|
1234 | for (i = 0; i < wordi; ++i)
|
---|
1235 | {
|
---|
1236 | len = strlen (words[i]);
|
---|
1237 | if (i == wordi - 1 || strlen (words[i + 1]) != len
|
---|
1238 | || strcmp (words[i], words[i + 1]))
|
---|
1239 | {
|
---|
1240 | o = variable_buffer_output (o, words[i], len);
|
---|
1241 | o = variable_buffer_output (o, " ", 1);
|
---|
1242 | }
|
---|
1243 | }
|
---|
1244 | #ifdef CONFIG_WITH_RSORT
|
---|
1245 | }
|
---|
1246 | else
|
---|
1247 | {
|
---|
1248 | /* rsort - reverse the result */
|
---|
1249 | i = wordi;
|
---|
1250 | while (i-- > 0)
|
---|
1251 | {
|
---|
1252 | len = strlen (words[i]);
|
---|
1253 | if (i == 0 || strlen (words[i - 1]) != len
|
---|
1254 | || strcmp (words[i], words[i - 1]))
|
---|
1255 | {
|
---|
1256 | o = variable_buffer_output (o, words[i], len);
|
---|
1257 | o = variable_buffer_output (o, " ", 1);
|
---|
1258 | }
|
---|
1259 | }
|
---|
1260 | }
|
---|
1261 | #endif
|
---|
1262 |
|
---|
1263 | /* Kill the last space. */
|
---|
1264 | --o;
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | free (words);
|
---|
1268 |
|
---|
1269 | return o;
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | /*
|
---|
1273 | $(if condition,true-part[,false-part])
|
---|
1274 |
|
---|
1275 | CONDITION is false iff it evaluates to an empty string. White
|
---|
1276 | space before and after condition are stripped before evaluation.
|
---|
1277 |
|
---|
1278 | If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is
|
---|
1279 | evaluated (if it exists). Because only one of the two PARTs is evaluated,
|
---|
1280 | you can use $(if ...) to create side-effects (with $(shell ...), for
|
---|
1281 | example).
|
---|
1282 | */
|
---|
1283 |
|
---|
1284 | static char *
|
---|
1285 | func_if (char *o, char **argv, const char *funcname UNUSED)
|
---|
1286 | {
|
---|
1287 | const char *begp = argv[0];
|
---|
1288 | const char *endp = begp + strlen (argv[0]) - 1;
|
---|
1289 | int result = 0;
|
---|
1290 |
|
---|
1291 | /* Find the result of the condition: if we have a value, and it's not
|
---|
1292 | empty, the condition is true. If we don't have a value, or it's the
|
---|
1293 | empty string, then it's false. */
|
---|
1294 |
|
---|
1295 | strip_whitespace (&begp, &endp);
|
---|
1296 |
|
---|
1297 | if (begp <= endp)
|
---|
1298 | {
|
---|
1299 | char *expansion = expand_argument (begp, endp+1);
|
---|
1300 |
|
---|
1301 | result = strlen (expansion);
|
---|
1302 | free (expansion);
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | /* If the result is true (1) we want to eval the first argument, and if
|
---|
1306 | it's false (0) we want to eval the second. If the argument doesn't
|
---|
1307 | exist we do nothing, otherwise expand it and add to the buffer. */
|
---|
1308 |
|
---|
1309 | argv += 1 + !result;
|
---|
1310 |
|
---|
1311 | if (*argv)
|
---|
1312 | {
|
---|
1313 | char *expansion = expand_argument (*argv, NULL);
|
---|
1314 |
|
---|
1315 | o = variable_buffer_output (o, expansion, strlen (expansion));
|
---|
1316 |
|
---|
1317 | free (expansion);
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | return o;
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | /*
|
---|
1324 | $(or condition1[,condition2[,condition3[...]]])
|
---|
1325 |
|
---|
1326 | A CONDITION is false iff it evaluates to an empty string. White
|
---|
1327 | space before and after CONDITION are stripped before evaluation.
|
---|
1328 |
|
---|
1329 | CONDITION1 is evaluated. If it's true, then this is the result of
|
---|
1330 | expansion. If it's false, CONDITION2 is evaluated, and so on. If none of
|
---|
1331 | the conditions are true, the expansion is the empty string.
|
---|
1332 |
|
---|
1333 | Once a CONDITION is true no further conditions are evaluated
|
---|
1334 | (short-circuiting).
|
---|
1335 | */
|
---|
1336 |
|
---|
1337 | static char *
|
---|
1338 | func_or (char *o, char **argv, const char *funcname UNUSED)
|
---|
1339 | {
|
---|
1340 | for ( ; *argv ; ++argv)
|
---|
1341 | {
|
---|
1342 | const char *begp = *argv;
|
---|
1343 | const char *endp = begp + strlen (*argv) - 1;
|
---|
1344 | char *expansion;
|
---|
1345 | int result = 0;
|
---|
1346 |
|
---|
1347 | /* Find the result of the condition: if it's false keep going. */
|
---|
1348 |
|
---|
1349 | strip_whitespace (&begp, &endp);
|
---|
1350 |
|
---|
1351 | if (begp > endp)
|
---|
1352 | continue;
|
---|
1353 |
|
---|
1354 | expansion = expand_argument (begp, endp+1);
|
---|
1355 | result = strlen (expansion);
|
---|
1356 |
|
---|
1357 | /* If the result is false keep going. */
|
---|
1358 | if (!result)
|
---|
1359 | {
|
---|
1360 | free (expansion);
|
---|
1361 | continue;
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | /* It's true! Keep this result and return. */
|
---|
1365 | o = variable_buffer_output (o, expansion, result);
|
---|
1366 | free (expansion);
|
---|
1367 | break;
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | return o;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | /*
|
---|
1374 | $(and condition1[,condition2[,condition3[...]]])
|
---|
1375 |
|
---|
1376 | A CONDITION is false iff it evaluates to an empty string. White
|
---|
1377 | space before and after CONDITION are stripped before evaluation.
|
---|
1378 |
|
---|
1379 | CONDITION1 is evaluated. If it's false, then this is the result of
|
---|
1380 | expansion. If it's true, CONDITION2 is evaluated, and so on. If all of
|
---|
1381 | the conditions are true, the expansion is the result of the last condition.
|
---|
1382 |
|
---|
1383 | Once a CONDITION is false no further conditions are evaluated
|
---|
1384 | (short-circuiting).
|
---|
1385 | */
|
---|
1386 |
|
---|
1387 | static char *
|
---|
1388 | func_and (char *o, char **argv, const char *funcname UNUSED)
|
---|
1389 | {
|
---|
1390 | char *expansion;
|
---|
1391 | int result;
|
---|
1392 |
|
---|
1393 | while (1)
|
---|
1394 | {
|
---|
1395 | const char *begp = *argv;
|
---|
1396 | const char *endp = begp + strlen (*argv) - 1;
|
---|
1397 |
|
---|
1398 | /* An empty condition is always false. */
|
---|
1399 | strip_whitespace (&begp, &endp);
|
---|
1400 | if (begp > endp)
|
---|
1401 | return o;
|
---|
1402 |
|
---|
1403 | expansion = expand_argument (begp, endp+1);
|
---|
1404 | result = strlen (expansion);
|
---|
1405 |
|
---|
1406 | /* If the result is false, stop here: we're done. */
|
---|
1407 | if (!result)
|
---|
1408 | break;
|
---|
1409 |
|
---|
1410 | /* Otherwise the result is true. If this is the last one, keep this
|
---|
1411 | result and quit. Otherwise go on to the next one! */
|
---|
1412 |
|
---|
1413 | if (*(++argv))
|
---|
1414 | free (expansion);
|
---|
1415 | else
|
---|
1416 | {
|
---|
1417 | o = variable_buffer_output (o, expansion, result);
|
---|
1418 | break;
|
---|
1419 | }
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | free (expansion);
|
---|
1423 |
|
---|
1424 | return o;
|
---|
1425 | }
|
---|
1426 |
|
---|
1427 | static char *
|
---|
1428 | func_wildcard (char *o, char **argv, const char *funcname UNUSED)
|
---|
1429 | {
|
---|
1430 | #ifdef _AMIGA
|
---|
1431 | o = wildcard_expansion (argv[0], o);
|
---|
1432 | #else
|
---|
1433 | char *p = string_glob (argv[0]);
|
---|
1434 | o = variable_buffer_output (o, p, strlen (p));
|
---|
1435 | #endif
|
---|
1436 | return o;
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 | /*
|
---|
1440 | $(eval <makefile string>)
|
---|
1441 |
|
---|
1442 | Always resolves to the empty string.
|
---|
1443 |
|
---|
1444 | Treat the arguments as a segment of makefile, and parse them.
|
---|
1445 | */
|
---|
1446 |
|
---|
1447 | static char *
|
---|
1448 | func_eval (char *o, char **argv, const char *funcname UNUSED)
|
---|
1449 | {
|
---|
1450 | char *buf;
|
---|
1451 | unsigned int len;
|
---|
1452 |
|
---|
1453 | /* Eval the buffer. Pop the current variable buffer setting so that the
|
---|
1454 | eval'd code can use its own without conflicting. */
|
---|
1455 |
|
---|
1456 | install_variable_buffer (&buf, &len);
|
---|
1457 |
|
---|
1458 | eval_buffer (argv[0]);
|
---|
1459 |
|
---|
1460 | restore_variable_buffer (buf, len);
|
---|
1461 |
|
---|
1462 | return o;
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 |
|
---|
1466 | static char *
|
---|
1467 | func_value (char *o, char **argv, const char *funcname UNUSED)
|
---|
1468 | {
|
---|
1469 | /* Look up the variable. */
|
---|
1470 | struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
---|
1471 |
|
---|
1472 | /* Copy its value into the output buffer without expanding it. */
|
---|
1473 | if (v)
|
---|
1474 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1475 | o = variable_buffer_output (o, v->value,
|
---|
1476 | v->value_length >= 0 ? v->value_length : strlen(v->value));
|
---|
1477 | #else
|
---|
1478 | o = variable_buffer_output (o, v->value, strlen(v->value));
|
---|
1479 | #endif
|
---|
1480 |
|
---|
1481 | return o;
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 | /*
|
---|
1485 | \r is replaced on UNIX as well. Is this desirable?
|
---|
1486 | */
|
---|
1487 | static void
|
---|
1488 | fold_newlines (char *buffer, unsigned int *length)
|
---|
1489 | {
|
---|
1490 | char *dst = buffer;
|
---|
1491 | char *src = buffer;
|
---|
1492 | char *last_nonnl = buffer -1;
|
---|
1493 | src[*length] = 0;
|
---|
1494 | for (; *src != '\0'; ++src)
|
---|
1495 | {
|
---|
1496 | if (src[0] == '\r' && src[1] == '\n')
|
---|
1497 | continue;
|
---|
1498 | if (*src == '\n')
|
---|
1499 | {
|
---|
1500 | *dst++ = ' ';
|
---|
1501 | }
|
---|
1502 | else
|
---|
1503 | {
|
---|
1504 | last_nonnl = dst;
|
---|
1505 | *dst++ = *src;
|
---|
1506 | }
|
---|
1507 | }
|
---|
1508 | *(++last_nonnl) = '\0';
|
---|
1509 | *length = last_nonnl - buffer;
|
---|
1510 | }
|
---|
1511 |
|
---|
1512 |
|
---|
1513 |
|
---|
1514 | int shell_function_pid = 0, shell_function_completed;
|
---|
1515 |
|
---|
1516 |
|
---|
1517 | #ifdef WINDOWS32
|
---|
1518 | /*untested*/
|
---|
1519 |
|
---|
1520 | #include <windows.h>
|
---|
1521 | #include <io.h>
|
---|
1522 | #include "sub_proc.h"
|
---|
1523 |
|
---|
1524 |
|
---|
1525 | void
|
---|
1526 | windows32_openpipe (int *pipedes, int *pid_p, char **command_argv, char **envp)
|
---|
1527 | {
|
---|
1528 | SECURITY_ATTRIBUTES saAttr;
|
---|
1529 | HANDLE hIn;
|
---|
1530 | HANDLE hErr;
|
---|
1531 | HANDLE hChildOutRd;
|
---|
1532 | HANDLE hChildOutWr;
|
---|
1533 | HANDLE hProcess;
|
---|
1534 |
|
---|
1535 |
|
---|
1536 | saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
|
---|
1537 | saAttr.bInheritHandle = TRUE;
|
---|
1538 | saAttr.lpSecurityDescriptor = NULL;
|
---|
1539 |
|
---|
1540 | if (DuplicateHandle (GetCurrentProcess(),
|
---|
1541 | GetStdHandle(STD_INPUT_HANDLE),
|
---|
1542 | GetCurrentProcess(),
|
---|
1543 | &hIn,
|
---|
1544 | 0,
|
---|
1545 | TRUE,
|
---|
1546 | DUPLICATE_SAME_ACCESS) == FALSE) {
|
---|
1547 | fatal (NILF, _("create_child_process: DuplicateHandle(In) failed (e=%ld)\n"),
|
---|
1548 | GetLastError());
|
---|
1549 |
|
---|
1550 | }
|
---|
1551 | if (DuplicateHandle(GetCurrentProcess(),
|
---|
1552 | GetStdHandle(STD_ERROR_HANDLE),
|
---|
1553 | GetCurrentProcess(),
|
---|
1554 | &hErr,
|
---|
1555 | 0,
|
---|
1556 | TRUE,
|
---|
1557 | DUPLICATE_SAME_ACCESS) == FALSE) {
|
---|
1558 | fatal (NILF, _("create_child_process: DuplicateHandle(Err) failed (e=%ld)\n"),
|
---|
1559 | GetLastError());
|
---|
1560 | }
|
---|
1561 |
|
---|
1562 | if (!CreatePipe(&hChildOutRd, &hChildOutWr, &saAttr, 0))
|
---|
1563 | fatal (NILF, _("CreatePipe() failed (e=%ld)\n"), GetLastError());
|
---|
1564 |
|
---|
1565 | hProcess = process_init_fd(hIn, hChildOutWr, hErr);
|
---|
1566 |
|
---|
1567 | if (!hProcess)
|
---|
1568 | fatal (NILF, _("windows32_openpipe (): process_init_fd() failed\n"));
|
---|
1569 |
|
---|
1570 | /* make sure that CreateProcess() has Path it needs */
|
---|
1571 | sync_Path_environment();
|
---|
1572 |
|
---|
1573 | if (!process_begin(hProcess, command_argv, envp, command_argv[0], NULL)) {
|
---|
1574 | /* register process for wait */
|
---|
1575 | process_register(hProcess);
|
---|
1576 |
|
---|
1577 | /* set the pid for returning to caller */
|
---|
1578 | *pid_p = (int) hProcess;
|
---|
1579 |
|
---|
1580 | /* set up to read data from child */
|
---|
1581 | pipedes[0] = _open_osfhandle((long) hChildOutRd, O_RDONLY);
|
---|
1582 |
|
---|
1583 | /* this will be closed almost right away */
|
---|
1584 | pipedes[1] = _open_osfhandle((long) hChildOutWr, O_APPEND);
|
---|
1585 | } else {
|
---|
1586 | /* reap/cleanup the failed process */
|
---|
1587 | process_cleanup(hProcess);
|
---|
1588 |
|
---|
1589 | /* close handles which were duplicated, they weren't used */
|
---|
1590 | CloseHandle(hIn);
|
---|
1591 | CloseHandle(hErr);
|
---|
1592 |
|
---|
1593 | /* close pipe handles, they won't be used */
|
---|
1594 | CloseHandle(hChildOutRd);
|
---|
1595 | CloseHandle(hChildOutWr);
|
---|
1596 |
|
---|
1597 | /* set status for return */
|
---|
1598 | pipedes[0] = pipedes[1] = -1;
|
---|
1599 | *pid_p = -1;
|
---|
1600 | }
|
---|
1601 | }
|
---|
1602 | #endif
|
---|
1603 |
|
---|
1604 |
|
---|
1605 | #ifdef __MSDOS__
|
---|
1606 | FILE *
|
---|
1607 | msdos_openpipe (int* pipedes, int *pidp, char *text)
|
---|
1608 | {
|
---|
1609 | FILE *fpipe=0;
|
---|
1610 | /* MSDOS can't fork, but it has `popen'. */
|
---|
1611 | struct variable *sh = lookup_variable ("SHELL", 5);
|
---|
1612 | int e;
|
---|
1613 | extern int dos_command_running, dos_status;
|
---|
1614 |
|
---|
1615 | /* Make sure not to bother processing an empty line. */
|
---|
1616 | while (isblank ((unsigned char)*text))
|
---|
1617 | ++text;
|
---|
1618 | if (*text == '\0')
|
---|
1619 | return 0;
|
---|
1620 |
|
---|
1621 | if (sh)
|
---|
1622 | {
|
---|
1623 | char buf[PATH_MAX + 7];
|
---|
1624 | /* This makes sure $SHELL value is used by $(shell), even
|
---|
1625 | though the target environment is not passed to it. */
|
---|
1626 | sprintf (buf, "SHELL=%s", sh->value);
|
---|
1627 | putenv (buf);
|
---|
1628 | }
|
---|
1629 |
|
---|
1630 | e = errno;
|
---|
1631 | errno = 0;
|
---|
1632 | dos_command_running = 1;
|
---|
1633 | dos_status = 0;
|
---|
1634 | /* If dos_status becomes non-zero, it means the child process
|
---|
1635 | was interrupted by a signal, like SIGINT or SIGQUIT. See
|
---|
1636 | fatal_error_signal in commands.c. */
|
---|
1637 | fpipe = popen (text, "rt");
|
---|
1638 | dos_command_running = 0;
|
---|
1639 | if (!fpipe || dos_status)
|
---|
1640 | {
|
---|
1641 | pipedes[0] = -1;
|
---|
1642 | *pidp = -1;
|
---|
1643 | if (dos_status)
|
---|
1644 | errno = EINTR;
|
---|
1645 | else if (errno == 0)
|
---|
1646 | errno = ENOMEM;
|
---|
1647 | shell_function_completed = -1;
|
---|
1648 | }
|
---|
1649 | else
|
---|
1650 | {
|
---|
1651 | pipedes[0] = fileno (fpipe);
|
---|
1652 | *pidp = 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
|
---|
1653 | errno = e;
|
---|
1654 | shell_function_completed = 1;
|
---|
1655 | }
|
---|
1656 | return fpipe;
|
---|
1657 | }
|
---|
1658 | #endif
|
---|
1659 |
|
---|
1660 | /*
|
---|
1661 | Do shell spawning, with the naughty bits for different OSes.
|
---|
1662 | */
|
---|
1663 |
|
---|
1664 | #ifdef VMS
|
---|
1665 |
|
---|
1666 | /* VMS can't do $(shell ...) */
|
---|
1667 | #define func_shell 0
|
---|
1668 |
|
---|
1669 | #else
|
---|
1670 | #ifndef _AMIGA
|
---|
1671 | static char *
|
---|
1672 | func_shell (char *o, char **argv, const char *funcname UNUSED)
|
---|
1673 | {
|
---|
1674 | char *batch_filename = NULL;
|
---|
1675 |
|
---|
1676 | #ifdef __MSDOS__
|
---|
1677 | FILE *fpipe;
|
---|
1678 | #endif
|
---|
1679 | char **command_argv;
|
---|
1680 | const char *error_prefix;
|
---|
1681 | char **envp;
|
---|
1682 | int pipedes[2];
|
---|
1683 | int pid;
|
---|
1684 |
|
---|
1685 | #ifndef __MSDOS__
|
---|
1686 | /* Construct the argument list. */
|
---|
1687 | command_argv = construct_command_argv (argv[0], NULL, NULL, &batch_filename);
|
---|
1688 | if (command_argv == 0)
|
---|
1689 | return o;
|
---|
1690 | #endif
|
---|
1691 |
|
---|
1692 | /* Using a target environment for `shell' loses in cases like:
|
---|
1693 | export var = $(shell echo foobie)
|
---|
1694 | because target_environment hits a loop trying to expand $(var)
|
---|
1695 | to put it in the environment. This is even more confusing when
|
---|
1696 | var was not explicitly exported, but just appeared in the
|
---|
1697 | calling environment.
|
---|
1698 |
|
---|
1699 | See Savannah bug #10593.
|
---|
1700 |
|
---|
1701 | envp = target_environment (NILF);
|
---|
1702 | */
|
---|
1703 |
|
---|
1704 | envp = environ;
|
---|
1705 |
|
---|
1706 | /* For error messages. */
|
---|
1707 | if (reading_file && reading_file->filenm)
|
---|
1708 | {
|
---|
1709 | char *p = alloca (strlen (reading_file->filenm)+11+4);
|
---|
1710 | sprintf (p, "%s:%lu: ", reading_file->filenm, reading_file->lineno);
|
---|
1711 | error_prefix = p;
|
---|
1712 | }
|
---|
1713 | else
|
---|
1714 | error_prefix = "";
|
---|
1715 |
|
---|
1716 | #if defined(__MSDOS__)
|
---|
1717 | fpipe = msdos_openpipe (pipedes, &pid, argv[0]);
|
---|
1718 | if (pipedes[0] < 0)
|
---|
1719 | {
|
---|
1720 | perror_with_name (error_prefix, "pipe");
|
---|
1721 | return o;
|
---|
1722 | }
|
---|
1723 | #elif defined(WINDOWS32)
|
---|
1724 | windows32_openpipe (pipedes, &pid, command_argv, envp);
|
---|
1725 | if (pipedes[0] < 0)
|
---|
1726 | {
|
---|
1727 | /* open of the pipe failed, mark as failed execution */
|
---|
1728 | shell_function_completed = -1;
|
---|
1729 |
|
---|
1730 | return o;
|
---|
1731 | }
|
---|
1732 | else
|
---|
1733 | #else
|
---|
1734 | if (pipe (pipedes) < 0)
|
---|
1735 | {
|
---|
1736 | perror_with_name (error_prefix, "pipe");
|
---|
1737 | return o;
|
---|
1738 | }
|
---|
1739 |
|
---|
1740 | # ifdef __EMX__
|
---|
1741 | /* close some handles that are unnecessary for the child process */
|
---|
1742 | CLOSE_ON_EXEC(pipedes[1]);
|
---|
1743 | CLOSE_ON_EXEC(pipedes[0]);
|
---|
1744 | /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
|
---|
1745 | pid = child_execute_job (0, pipedes[1], command_argv, envp);
|
---|
1746 | if (pid < 0)
|
---|
1747 | perror_with_name (error_prefix, "spawn");
|
---|
1748 | # else /* ! __EMX__ */
|
---|
1749 | pid = vfork ();
|
---|
1750 | if (pid < 0)
|
---|
1751 | perror_with_name (error_prefix, "fork");
|
---|
1752 | else if (pid == 0)
|
---|
1753 | child_execute_job (0, pipedes[1], command_argv, envp);
|
---|
1754 | else
|
---|
1755 | # endif
|
---|
1756 | #endif
|
---|
1757 | {
|
---|
1758 | /* We are the parent. */
|
---|
1759 | char *buffer;
|
---|
1760 | unsigned int maxlen, i;
|
---|
1761 | int cc;
|
---|
1762 |
|
---|
1763 | /* Record the PID for reap_children. */
|
---|
1764 | shell_function_pid = pid;
|
---|
1765 | #ifndef __MSDOS__
|
---|
1766 | shell_function_completed = 0;
|
---|
1767 |
|
---|
1768 | /* Free the storage only the child needed. */
|
---|
1769 | free (command_argv[0]);
|
---|
1770 | free (command_argv);
|
---|
1771 |
|
---|
1772 | /* Close the write side of the pipe. */
|
---|
1773 | # ifdef _MSC_VER /* Avoid annoying msvcrt when debugging. (bird) */
|
---|
1774 | if (pipedes[1] != -1)
|
---|
1775 | # endif
|
---|
1776 | close (pipedes[1]);
|
---|
1777 | #endif
|
---|
1778 |
|
---|
1779 | /* Set up and read from the pipe. */
|
---|
1780 |
|
---|
1781 | maxlen = 200;
|
---|
1782 | buffer = xmalloc (maxlen + 1);
|
---|
1783 |
|
---|
1784 | /* Read from the pipe until it gets EOF. */
|
---|
1785 | for (i = 0; ; i += cc)
|
---|
1786 | {
|
---|
1787 | if (i == maxlen)
|
---|
1788 | {
|
---|
1789 | maxlen += 512;
|
---|
1790 | buffer = xrealloc (buffer, maxlen + 1);
|
---|
1791 | }
|
---|
1792 |
|
---|
1793 | EINTRLOOP (cc, read (pipedes[0], &buffer[i], maxlen - i));
|
---|
1794 | if (cc <= 0)
|
---|
1795 | break;
|
---|
1796 | }
|
---|
1797 | buffer[i] = '\0';
|
---|
1798 |
|
---|
1799 | /* Close the read side of the pipe. */
|
---|
1800 | #ifdef __MSDOS__
|
---|
1801 | if (fpipe)
|
---|
1802 | (void) pclose (fpipe);
|
---|
1803 | #else
|
---|
1804 | # ifdef _MSC_VER /* Avoid annoying msvcrt when debugging. (bird) */
|
---|
1805 | if (pipedes[0] != -1)
|
---|
1806 | # endif
|
---|
1807 | (void) close (pipedes[0]);
|
---|
1808 | #endif
|
---|
1809 |
|
---|
1810 | /* Loop until child_handler or reap_children() sets
|
---|
1811 | shell_function_completed to the status of our child shell. */
|
---|
1812 | while (shell_function_completed == 0)
|
---|
1813 | reap_children (1, 0);
|
---|
1814 |
|
---|
1815 | if (batch_filename) {
|
---|
1816 | DB (DB_VERBOSE, (_("Cleaning up temporary batch file %s\n"),
|
---|
1817 | batch_filename));
|
---|
1818 | remove (batch_filename);
|
---|
1819 | free (batch_filename);
|
---|
1820 | }
|
---|
1821 | shell_function_pid = 0;
|
---|
1822 |
|
---|
1823 | /* The child_handler function will set shell_function_completed
|
---|
1824 | to 1 when the child dies normally, or to -1 if it
|
---|
1825 | dies with status 127, which is most likely an exec fail. */
|
---|
1826 |
|
---|
1827 | if (shell_function_completed == -1)
|
---|
1828 | {
|
---|
1829 | /* This likely means that the execvp failed, so we should just
|
---|
1830 | write the error message in the pipe from the child. */
|
---|
1831 | fputs (buffer, stderr);
|
---|
1832 | fflush (stderr);
|
---|
1833 | }
|
---|
1834 | else
|
---|
1835 | {
|
---|
1836 | /* The child finished normally. Replace all newlines in its output
|
---|
1837 | with spaces, and put that in the variable output buffer. */
|
---|
1838 | fold_newlines (buffer, &i);
|
---|
1839 | o = variable_buffer_output (o, buffer, i);
|
---|
1840 | }
|
---|
1841 |
|
---|
1842 | free (buffer);
|
---|
1843 | }
|
---|
1844 |
|
---|
1845 | return o;
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | #else /* _AMIGA */
|
---|
1849 |
|
---|
1850 | /* Do the Amiga version of func_shell. */
|
---|
1851 |
|
---|
1852 | static char *
|
---|
1853 | func_shell (char *o, char **argv, const char *funcname)
|
---|
1854 | {
|
---|
1855 | /* Amiga can't fork nor spawn, but I can start a program with
|
---|
1856 | redirection of my choice. However, this means that we
|
---|
1857 | don't have an opportunity to reopen stdout to trap it. Thus,
|
---|
1858 | we save our own stdout onto a new descriptor and dup a temp
|
---|
1859 | file's descriptor onto our stdout temporarily. After we
|
---|
1860 | spawn the shell program, we dup our own stdout back to the
|
---|
1861 | stdout descriptor. The buffer reading is the same as above,
|
---|
1862 | except that we're now reading from a file. */
|
---|
1863 |
|
---|
1864 | #include <dos/dos.h>
|
---|
1865 | #include <proto/dos.h>
|
---|
1866 |
|
---|
1867 | BPTR child_stdout;
|
---|
1868 | char tmp_output[FILENAME_MAX];
|
---|
1869 | unsigned int maxlen = 200, i;
|
---|
1870 | int cc;
|
---|
1871 | char * buffer, * ptr;
|
---|
1872 | char ** aptr;
|
---|
1873 | int len = 0;
|
---|
1874 | char* batch_filename = NULL;
|
---|
1875 |
|
---|
1876 | /* Construct the argument list. */
|
---|
1877 | command_argv = construct_command_argv (argv[0], (char **) NULL,
|
---|
1878 | (struct file *) 0, &batch_filename);
|
---|
1879 | if (command_argv == 0)
|
---|
1880 | return o;
|
---|
1881 |
|
---|
1882 | /* Note the mktemp() is a security hole, but this only runs on Amiga.
|
---|
1883 | Ideally we would use main.c:open_tmpfile(), but this uses a special
|
---|
1884 | Open(), not fopen(), and I'm not familiar enough with the code to mess
|
---|
1885 | with it. */
|
---|
1886 | strcpy (tmp_output, "t:MakeshXXXXXXXX");
|
---|
1887 | mktemp (tmp_output);
|
---|
1888 | child_stdout = Open (tmp_output, MODE_NEWFILE);
|
---|
1889 |
|
---|
1890 | for (aptr=command_argv; *aptr; aptr++)
|
---|
1891 | len += strlen (*aptr) + 1;
|
---|
1892 |
|
---|
1893 | buffer = xmalloc (len + 1);
|
---|
1894 | ptr = buffer;
|
---|
1895 |
|
---|
1896 | for (aptr=command_argv; *aptr; aptr++)
|
---|
1897 | {
|
---|
1898 | strcpy (ptr, *aptr);
|
---|
1899 | ptr += strlen (ptr) + 1;
|
---|
1900 | *ptr ++ = ' ';
|
---|
1901 | *ptr = 0;
|
---|
1902 | }
|
---|
1903 |
|
---|
1904 | ptr[-1] = '\n';
|
---|
1905 |
|
---|
1906 | Execute (buffer, NULL, child_stdout);
|
---|
1907 | free (buffer);
|
---|
1908 |
|
---|
1909 | Close (child_stdout);
|
---|
1910 |
|
---|
1911 | child_stdout = Open (tmp_output, MODE_OLDFILE);
|
---|
1912 |
|
---|
1913 | buffer = xmalloc (maxlen);
|
---|
1914 | i = 0;
|
---|
1915 | do
|
---|
1916 | {
|
---|
1917 | if (i == maxlen)
|
---|
1918 | {
|
---|
1919 | maxlen += 512;
|
---|
1920 | buffer = xrealloc (buffer, maxlen + 1);
|
---|
1921 | }
|
---|
1922 |
|
---|
1923 | cc = Read (child_stdout, &buffer[i], maxlen - i);
|
---|
1924 | if (cc > 0)
|
---|
1925 | i += cc;
|
---|
1926 | } while (cc > 0);
|
---|
1927 |
|
---|
1928 | Close (child_stdout);
|
---|
1929 |
|
---|
1930 | fold_newlines (buffer, &i);
|
---|
1931 | o = variable_buffer_output (o, buffer, i);
|
---|
1932 | free (buffer);
|
---|
1933 | return o;
|
---|
1934 | }
|
---|
1935 | #endif /* _AMIGA */
|
---|
1936 | #endif /* !VMS */
|
---|
1937 |
|
---|
1938 | #ifdef EXPERIMENTAL
|
---|
1939 |
|
---|
1940 | /*
|
---|
1941 | equality. Return is string-boolean, ie, the empty string is false.
|
---|
1942 | */
|
---|
1943 | static char *
|
---|
1944 | func_eq (char *o, char **argv, const char *funcname)
|
---|
1945 | {
|
---|
1946 | int result = ! strcmp (argv[0], argv[1]);
|
---|
1947 | o = variable_buffer_output (o, result ? "1" : "", result);
|
---|
1948 | return o;
|
---|
1949 | }
|
---|
1950 |
|
---|
1951 |
|
---|
1952 | /*
|
---|
1953 | string-boolean not operator.
|
---|
1954 | */
|
---|
1955 | static char *
|
---|
1956 | func_not (char *o, char **argv, const char *funcname)
|
---|
1957 | {
|
---|
1958 | const char *s = argv[0];
|
---|
1959 | int result = 0;
|
---|
1960 | while (isspace ((unsigned char)*s))
|
---|
1961 | s++;
|
---|
1962 | result = ! (*s);
|
---|
1963 | o = variable_buffer_output (o, result ? "1" : "", result);
|
---|
1964 | return o;
|
---|
1965 | }
|
---|
1966 | #endif
|
---|
1967 | |
---|
1968 |
|
---|
1969 |
|
---|
1970 | /* Return the absolute name of file NAME which does not contain any `.',
|
---|
1971 | `..' components nor any repeated path separators ('/'). */
|
---|
1972 | #ifdef KMK
|
---|
1973 | char *
|
---|
1974 | #else
|
---|
1975 | static char *
|
---|
1976 | #endif
|
---|
1977 | abspath (const char *name, char *apath)
|
---|
1978 | {
|
---|
1979 | char *dest;
|
---|
1980 | const char *start, *end, *apath_limit;
|
---|
1981 |
|
---|
1982 | if (name[0] == '\0' || apath == NULL)
|
---|
1983 | return NULL;
|
---|
1984 |
|
---|
1985 | #ifdef WINDOWS32 /* bird */
|
---|
1986 | dest = w32ify((char *)name, 1);
|
---|
1987 | if (!dest)
|
---|
1988 | return NULL;
|
---|
1989 | {
|
---|
1990 | size_t len = strlen(dest);
|
---|
1991 | memcpy(apath, dest, len);
|
---|
1992 | dest = apath + len;
|
---|
1993 | }
|
---|
1994 |
|
---|
1995 | (void)end; (void)start; (void)apath_limit;
|
---|
1996 |
|
---|
1997 | #elif defined __OS2__ /* bird */
|
---|
1998 | if (_abspath(apath, name, GET_PATH_MAX))
|
---|
1999 | return NULL;
|
---|
2000 | dest = strchr(apath, '\0');
|
---|
2001 |
|
---|
2002 | (void)end; (void)start; (void)apath_limit; (void)dest;
|
---|
2003 |
|
---|
2004 | #else /* !WINDOWS32 && !__OS2__ */
|
---|
2005 | apath_limit = apath + GET_PATH_MAX;
|
---|
2006 |
|
---|
2007 | #ifdef HAVE_DOS_PATHS /* bird added this */
|
---|
2008 | if (isalpha(name[0]) && name[1] == ':')
|
---|
2009 | {
|
---|
2010 | /* drive spec */
|
---|
2011 | apath[0] = toupper(name[0]);
|
---|
2012 | apath[1] = ':';
|
---|
2013 | apath[2] = '/';
|
---|
2014 | name += 2;
|
---|
2015 | }
|
---|
2016 | else
|
---|
2017 | #endif /* HAVE_DOS_PATHS */
|
---|
2018 | if (name[0] != '/')
|
---|
2019 | {
|
---|
2020 | /* It is unlikely we would make it until here but just to make sure. */
|
---|
2021 | if (!starting_directory)
|
---|
2022 | return NULL;
|
---|
2023 |
|
---|
2024 | strcpy (apath, starting_directory);
|
---|
2025 |
|
---|
2026 | dest = strchr (apath, '\0');
|
---|
2027 | }
|
---|
2028 | else
|
---|
2029 | {
|
---|
2030 | apath[0] = '/';
|
---|
2031 | dest = apath + 1;
|
---|
2032 | }
|
---|
2033 |
|
---|
2034 | for (start = end = name; *start != '\0'; start = end)
|
---|
2035 | {
|
---|
2036 | unsigned long len;
|
---|
2037 |
|
---|
2038 | /* Skip sequence of multiple path-separators. */
|
---|
2039 | while (*start == '/')
|
---|
2040 | ++start;
|
---|
2041 |
|
---|
2042 | /* Find end of path component. */
|
---|
2043 | for (end = start; *end != '\0' && *end != '/'; ++end)
|
---|
2044 | ;
|
---|
2045 |
|
---|
2046 | len = end - start;
|
---|
2047 |
|
---|
2048 | if (len == 0)
|
---|
2049 | break;
|
---|
2050 | else if (len == 1 && start[0] == '.')
|
---|
2051 | /* nothing */;
|
---|
2052 | else if (len == 2 && start[0] == '.' && start[1] == '.')
|
---|
2053 | {
|
---|
2054 | /* Back up to previous component, ignore if at root already. */
|
---|
2055 | if (dest > apath + 1)
|
---|
2056 | while ((--dest)[-1] != '/');
|
---|
2057 | }
|
---|
2058 | else
|
---|
2059 | {
|
---|
2060 | if (dest[-1] != '/')
|
---|
2061 | *dest++ = '/';
|
---|
2062 |
|
---|
2063 | if (dest + len >= apath_limit)
|
---|
2064 | return NULL;
|
---|
2065 |
|
---|
2066 | dest = memcpy (dest, start, len);
|
---|
2067 | dest += len;
|
---|
2068 | *dest = '\0';
|
---|
2069 | }
|
---|
2070 | }
|
---|
2071 | #endif /* !WINDOWS32 && !__OS2__ */
|
---|
2072 |
|
---|
2073 | /* Unless it is root strip trailing separator. */
|
---|
2074 | #ifdef HAVE_DOS_PATHS /* bird (is this correct? what about UNC?) */
|
---|
2075 | if (dest > apath + 1 + (apath[0] != '/') && dest[-1] == '/')
|
---|
2076 | #else
|
---|
2077 | if (dest > apath + 1 && dest[-1] == '/')
|
---|
2078 | #endif
|
---|
2079 | --dest;
|
---|
2080 |
|
---|
2081 | *dest = '\0';
|
---|
2082 |
|
---|
2083 | return apath;
|
---|
2084 | }
|
---|
2085 |
|
---|
2086 |
|
---|
2087 | static char *
|
---|
2088 | func_realpath (char *o, char **argv, const char *funcname UNUSED)
|
---|
2089 | {
|
---|
2090 | /* Expand the argument. */
|
---|
2091 | const char *p = argv[0];
|
---|
2092 | const char *path = 0;
|
---|
2093 | int doneany = 0;
|
---|
2094 | unsigned int len = 0;
|
---|
2095 | PATH_VAR (in);
|
---|
2096 | PATH_VAR (out);
|
---|
2097 |
|
---|
2098 | while ((path = find_next_token (&p, &len)) != 0)
|
---|
2099 | {
|
---|
2100 | if (len < GET_PATH_MAX)
|
---|
2101 | {
|
---|
2102 | strncpy (in, path, len);
|
---|
2103 | in[len] = '\0';
|
---|
2104 |
|
---|
2105 | if (
|
---|
2106 | #ifdef HAVE_REALPATH
|
---|
2107 | realpath (in, out)
|
---|
2108 | #else
|
---|
2109 | abspath (in, out)
|
---|
2110 | #endif
|
---|
2111 | )
|
---|
2112 | {
|
---|
2113 | o = variable_buffer_output (o, out, strlen (out));
|
---|
2114 | o = variable_buffer_output (o, " ", 1);
|
---|
2115 | doneany = 1;
|
---|
2116 | }
|
---|
2117 | }
|
---|
2118 | }
|
---|
2119 |
|
---|
2120 | /* Kill last space. */
|
---|
2121 | if (doneany)
|
---|
2122 | --o;
|
---|
2123 |
|
---|
2124 | return o;
|
---|
2125 | }
|
---|
2126 |
|
---|
2127 | static char *
|
---|
2128 | func_abspath (char *o, char **argv, const char *funcname UNUSED)
|
---|
2129 | {
|
---|
2130 | /* Expand the argument. */
|
---|
2131 | const char *p = argv[0];
|
---|
2132 | const char *path = 0;
|
---|
2133 | int doneany = 0;
|
---|
2134 | unsigned int len = 0;
|
---|
2135 | PATH_VAR (in);
|
---|
2136 | PATH_VAR (out);
|
---|
2137 |
|
---|
2138 | while ((path = find_next_token (&p, &len)) != 0)
|
---|
2139 | {
|
---|
2140 | if (len < GET_PATH_MAX)
|
---|
2141 | {
|
---|
2142 | strncpy (in, path, len);
|
---|
2143 | in[len] = '\0';
|
---|
2144 |
|
---|
2145 | if (abspath (in, out))
|
---|
2146 | {
|
---|
2147 | o = variable_buffer_output (o, out, strlen (out));
|
---|
2148 | o = variable_buffer_output (o, " ", 1);
|
---|
2149 | doneany = 1;
|
---|
2150 | }
|
---|
2151 | }
|
---|
2152 | }
|
---|
2153 |
|
---|
2154 | /* Kill last space. */
|
---|
2155 | if (doneany)
|
---|
2156 | --o;
|
---|
2157 |
|
---|
2158 | return o;
|
---|
2159 | }
|
---|
2160 |
|
---|
2161 | #ifdef CONFIG_WITH_ABSPATHEX
|
---|
2162 | /* same as abspath except that the current path is given as the 2nd argument. */
|
---|
2163 | static char *
|
---|
2164 | func_abspathex (char *o, char **argv, const char *funcname UNUSED)
|
---|
2165 | {
|
---|
2166 | /* Expand the argument. */
|
---|
2167 | char *p = argv[0];
|
---|
2168 | char *cwd = argv[1];
|
---|
2169 | unsigned int cwd_len = ~0U;
|
---|
2170 | char *path = 0;
|
---|
2171 | int doneany = 0;
|
---|
2172 | unsigned int len = 0;
|
---|
2173 | PATH_VAR (in);
|
---|
2174 | PATH_VAR (out);
|
---|
2175 |
|
---|
2176 | while ((path = find_next_token (&p, &len)) != 0)
|
---|
2177 | {
|
---|
2178 | if (len < GET_PATH_MAX)
|
---|
2179 | {
|
---|
2180 | #ifdef HAVE_DOS_PATHS
|
---|
2181 | if (path[0] != '/' && path[0] != '\\' && (len < 2 || path[1] != ':') && cwd)
|
---|
2182 | #else
|
---|
2183 | if (path[0] != '/' && cwd)
|
---|
2184 | #endif
|
---|
2185 | {
|
---|
2186 | /* relative path, prefix with cwd. */
|
---|
2187 | if (cwd_len == ~0U)
|
---|
2188 | cwd_len = strlen (cwd);
|
---|
2189 | if (cwd_len + len + 1 >= GET_PATH_MAX)
|
---|
2190 | continue;
|
---|
2191 | memcpy (in, cwd, cwd_len);
|
---|
2192 | in[cwd_len] = '/';
|
---|
2193 | memcpy (in + cwd_len + 1, path, len);
|
---|
2194 | in[cwd_len + len + 1] = '\0';
|
---|
2195 | }
|
---|
2196 | else
|
---|
2197 | {
|
---|
2198 | /* absolute path pass it as-is. */
|
---|
2199 | memcpy (in, path, len);
|
---|
2200 | in[len] = '\0';
|
---|
2201 | }
|
---|
2202 |
|
---|
2203 | if (abspath (in, out))
|
---|
2204 | {
|
---|
2205 | o = variable_buffer_output (o, out, strlen (out));
|
---|
2206 | o = variable_buffer_output (o, " ", 1);
|
---|
2207 | doneany = 1;
|
---|
2208 | }
|
---|
2209 | }
|
---|
2210 | }
|
---|
2211 |
|
---|
2212 | /* Kill last space. */
|
---|
2213 | if (doneany)
|
---|
2214 | --o;
|
---|
2215 |
|
---|
2216 | return o;
|
---|
2217 | }
|
---|
2218 | #endif
|
---|
2219 |
|
---|
2220 | #ifdef CONFIG_WITH_XARGS
|
---|
2221 | /* Create one or more command lines avoiding the max argument
|
---|
2222 | lenght restriction of the host OS.
|
---|
2223 |
|
---|
2224 | The last argument is the list of arguments that the normal
|
---|
2225 | xargs command would be fed from stdin.
|
---|
2226 |
|
---|
2227 | The first argument is initial command and it's arguments.
|
---|
2228 |
|
---|
2229 | If there are three or more arguments, the 2nd argument is
|
---|
2230 | the command and arguments to be used on subsequent
|
---|
2231 | command lines. Defaults to the initial command.
|
---|
2232 |
|
---|
2233 | If there are four or more arguments, the 3rd argument is
|
---|
2234 | the command to be used at the final command line. Defaults
|
---|
2235 | to the sub sequent or initial command .
|
---|
2236 |
|
---|
2237 | A future version of this function may define more arguments
|
---|
2238 | and therefor anyone specifying six or more arguments will
|
---|
2239 | cause fatal errors.
|
---|
2240 |
|
---|
2241 | Typical usage is:
|
---|
2242 | $(xargs ar cas mylib.a,$(objects))
|
---|
2243 | or
|
---|
2244 | $(xargs ar cas mylib.a,ar as mylib.a,$(objects))
|
---|
2245 |
|
---|
2246 | It will then create one or more "ar mylib.a ..." command
|
---|
2247 | lines with proper \n\t separation so it can be used when
|
---|
2248 | writing rules. */
|
---|
2249 | static char *
|
---|
2250 | func_xargs (char *o, char **argv, const char *funcname)
|
---|
2251 | {
|
---|
2252 | int argc;
|
---|
2253 | const char *initial_cmd;
|
---|
2254 | size_t initial_cmd_len;
|
---|
2255 | const char *subsequent_cmd;
|
---|
2256 | size_t subsequent_cmd_len;
|
---|
2257 | const char *final_cmd;
|
---|
2258 | size_t final_cmd_len;
|
---|
2259 | const char *args;
|
---|
2260 | size_t max_args;
|
---|
2261 | int i;
|
---|
2262 |
|
---|
2263 | #ifdef ARG_MAX
|
---|
2264 | /* ARG_MAX is a bit unreliable (environment), so drop 25% of the max. */
|
---|
2265 | # define XARGS_MAX (ARG_MAX - (ARG_MAX / 4))
|
---|
2266 | #else /* FIXME: update configure with a command line length test. */
|
---|
2267 | # define XARGS_MAX 10240
|
---|
2268 | #endif
|
---|
2269 |
|
---|
2270 | argc = 0;
|
---|
2271 | while (argv[argc])
|
---|
2272 | argc++;
|
---|
2273 | if (argc > 4)
|
---|
2274 | fatal (NILF, _("Too many arguments for $(xargs)!\n"));
|
---|
2275 |
|
---|
2276 | /* first: the initial / default command.*/
|
---|
2277 | initial_cmd = argv[0];
|
---|
2278 | while (isspace ((unsigned char)*initial_cmd))
|
---|
2279 | initial_cmd++;
|
---|
2280 | max_args = initial_cmd_len = strlen (initial_cmd);
|
---|
2281 |
|
---|
2282 | /* second: the command for the subsequent command lines. defaults to the initial cmd. */
|
---|
2283 | subsequent_cmd = argc > 2 && argv[1][0] != '\0' ? argv[1] : "";
|
---|
2284 | while (isspace ((unsigned char)*subsequent_cmd))
|
---|
2285 | subsequent_cmd++;
|
---|
2286 | if (*subsequent_cmd)
|
---|
2287 | {
|
---|
2288 | subsequent_cmd_len = strlen (subsequent_cmd);
|
---|
2289 | if (subsequent_cmd_len > max_args)
|
---|
2290 | max_args = subsequent_cmd_len;
|
---|
2291 | }
|
---|
2292 | else
|
---|
2293 | {
|
---|
2294 | subsequent_cmd = initial_cmd;
|
---|
2295 | subsequent_cmd_len = initial_cmd_len;
|
---|
2296 | }
|
---|
2297 |
|
---|
2298 | /* third: the final command. defaults to the subseq cmd. */
|
---|
2299 | final_cmd = argc > 3 && argv[2][0] != '\0' ? argv[2] : "";
|
---|
2300 | while (isspace ((unsigned char)*final_cmd))
|
---|
2301 | final_cmd++;
|
---|
2302 | if (*final_cmd)
|
---|
2303 | {
|
---|
2304 | final_cmd_len = strlen (final_cmd);
|
---|
2305 | if (final_cmd_len > max_args)
|
---|
2306 | max_args = final_cmd_len;
|
---|
2307 | }
|
---|
2308 | else
|
---|
2309 | {
|
---|
2310 | final_cmd = subsequent_cmd;
|
---|
2311 | final_cmd_len = subsequent_cmd_len;
|
---|
2312 | }
|
---|
2313 |
|
---|
2314 | /* last: the arguments to split up into sensible portions. */
|
---|
2315 | args = argv[argc - 1];
|
---|
2316 |
|
---|
2317 | /* calc the max argument length. */
|
---|
2318 | if (XARGS_MAX <= max_args + 2)
|
---|
2319 | fatal (NILF, _("$(xargs): the commands are longer than the max exec argument length. (%lu <= %lu)\n"),
|
---|
2320 | (unsigned long)XARGS_MAX, (unsigned long)max_args + 2);
|
---|
2321 | max_args = XARGS_MAX - max_args - 1;
|
---|
2322 |
|
---|
2323 | /* generate the commands. */
|
---|
2324 | i = 0;
|
---|
2325 | for (i = 0; ; i++)
|
---|
2326 | {
|
---|
2327 | unsigned int len;
|
---|
2328 | char *iterator = (char *)args;
|
---|
2329 | const char *end = args;
|
---|
2330 | const char *cur;
|
---|
2331 | const char *tmp;
|
---|
2332 |
|
---|
2333 | /* scan the arguments till we reach the end or the max length. */
|
---|
2334 | while ((cur = find_next_token(&iterator, &len))
|
---|
2335 | && (size_t)((cur + len) - args) < max_args)
|
---|
2336 | end = cur + len;
|
---|
2337 | if (cur && end == args)
|
---|
2338 | fatal (NILF, _("$(xargs): command + one single arg is too much. giving up.\n"));
|
---|
2339 |
|
---|
2340 | /* emit the command. */
|
---|
2341 | if (i == 0)
|
---|
2342 | {
|
---|
2343 | o = variable_buffer_output (o, (char *)initial_cmd, initial_cmd_len);
|
---|
2344 | o = variable_buffer_output (o, " ", 1);
|
---|
2345 | }
|
---|
2346 | else if (cur)
|
---|
2347 | {
|
---|
2348 | o = variable_buffer_output (o, "\n\t", 2);
|
---|
2349 | o = variable_buffer_output (o, (char *)subsequent_cmd, subsequent_cmd_len);
|
---|
2350 | o = variable_buffer_output (o, " ", 1);
|
---|
2351 | }
|
---|
2352 | else
|
---|
2353 | {
|
---|
2354 | o = variable_buffer_output (o, "\n\t", 2);
|
---|
2355 | o = variable_buffer_output (o, (char *)final_cmd, final_cmd_len);
|
---|
2356 | o = variable_buffer_output (o, " ", 1);
|
---|
2357 | }
|
---|
2358 |
|
---|
2359 | tmp = end;
|
---|
2360 | while (tmp > args && isspace ((unsigned char)tmp[-1])) /* drop trailing spaces. */
|
---|
2361 | tmp--;
|
---|
2362 | o = variable_buffer_output (o, (char *)args, tmp - args);
|
---|
2363 |
|
---|
2364 |
|
---|
2365 | /* next */
|
---|
2366 | if (!cur)
|
---|
2367 | break;
|
---|
2368 | args = end;
|
---|
2369 | while (isspace ((unsigned char)*args))
|
---|
2370 | args++;
|
---|
2371 | }
|
---|
2372 |
|
---|
2373 | return o;
|
---|
2374 | }
|
---|
2375 | #endif
|
---|
2376 |
|
---|
2377 | #ifdef CONFIG_WITH_TOUPPER_TOLOWER
|
---|
2378 | static char *
|
---|
2379 | func_toupper_tolower (char *o, char **argv, const char *funcname)
|
---|
2380 | {
|
---|
2381 | /* Expand the argument. */
|
---|
2382 | const char *p = argv[0];
|
---|
2383 | while (*p)
|
---|
2384 | {
|
---|
2385 | /* convert to temporary buffer */
|
---|
2386 | char tmp[256];
|
---|
2387 | unsigned int i;
|
---|
2388 | if (!strcmp(funcname, "toupper"))
|
---|
2389 | for (i = 0; i < sizeof(tmp) && *p; i++, p++)
|
---|
2390 | tmp[i] = toupper(*p);
|
---|
2391 | else
|
---|
2392 | for (i = 0; i < sizeof(tmp) && *p; i++, p++)
|
---|
2393 | tmp[i] = tolower(*p);
|
---|
2394 | o = variable_buffer_output (o, tmp, i);
|
---|
2395 | }
|
---|
2396 |
|
---|
2397 | return o;
|
---|
2398 | }
|
---|
2399 | #endif /* CONFIG_WITH_TOUPPER_TOLOWER */
|
---|
2400 |
|
---|
2401 | #if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
|
---|
2402 |
|
---|
2403 | /* Strip leading spaces and other things off a command. */
|
---|
2404 | static const char *
|
---|
2405 | comp_cmds_strip_leading (const char *s, const char *e)
|
---|
2406 | {
|
---|
2407 | while (s < e)
|
---|
2408 | {
|
---|
2409 | const char ch = *s;
|
---|
2410 | if (!isblank (ch)
|
---|
2411 | && ch != '@'
|
---|
2412 | && ch != '+'
|
---|
2413 | && ch != '-')
|
---|
2414 | break;
|
---|
2415 | s++;
|
---|
2416 | }
|
---|
2417 | return s;
|
---|
2418 | }
|
---|
2419 |
|
---|
2420 | /* Worker for func_comp_vars() which is called if the comparision failed.
|
---|
2421 | It will do the slow command by command comparision of the commands
|
---|
2422 | when there invoked as comp-cmds. */
|
---|
2423 | static char *
|
---|
2424 | comp_vars_ne (char *o, const char *s1, const char *e1, const char *s2, const char *e2,
|
---|
2425 | char *ne_retval, const char *funcname)
|
---|
2426 | {
|
---|
2427 | /* give up at once if not comp-cmds. */
|
---|
2428 | if (strcmp (funcname, "comp-cmds") != 0)
|
---|
2429 | o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
|
---|
2430 | else
|
---|
2431 | {
|
---|
2432 | const char * const s1_start = s1;
|
---|
2433 | int new_cmd = 1;
|
---|
2434 | int diff;
|
---|
2435 | for (;;)
|
---|
2436 | {
|
---|
2437 | /* if it's a new command, strip leading stuff. */
|
---|
2438 | if (new_cmd)
|
---|
2439 | {
|
---|
2440 | s1 = comp_cmds_strip_leading (s1, e1);
|
---|
2441 | s2 = comp_cmds_strip_leading (s2, e2);
|
---|
2442 | new_cmd = 0;
|
---|
2443 | }
|
---|
2444 | if (s1 >= e1 || s2 >= e2)
|
---|
2445 | break;
|
---|
2446 |
|
---|
2447 | /*
|
---|
2448 | * Inner compare loop which compares one line.
|
---|
2449 | * FIXME: parse quoting!
|
---|
2450 | */
|
---|
2451 | for (;;)
|
---|
2452 | {
|
---|
2453 | const char ch1 = *s1;
|
---|
2454 | const char ch2 = *s2;
|
---|
2455 | diff = ch1 - ch2;
|
---|
2456 | if (diff)
|
---|
2457 | break;
|
---|
2458 | if (ch1 == '\n')
|
---|
2459 | break;
|
---|
2460 | assert (ch1 != '\r');
|
---|
2461 |
|
---|
2462 | /* next */
|
---|
2463 | s1++;
|
---|
2464 | s2++;
|
---|
2465 | if (s1 >= e1 || s2 >= e2)
|
---|
2466 | break;
|
---|
2467 | }
|
---|
2468 |
|
---|
2469 | /*
|
---|
2470 | * If we exited because of a difference try to end-of-command
|
---|
2471 | * comparision, e.g. ignore trailing spaces.
|
---|
2472 | */
|
---|
2473 | if (diff)
|
---|
2474 | {
|
---|
2475 | /* strip */
|
---|
2476 | while (s1 < e1 && isblank (*s1))
|
---|
2477 | s1++;
|
---|
2478 | while (s2 < e2 && isblank (*s2))
|
---|
2479 | s2++;
|
---|
2480 | if (s1 >= e1 || s2 >= e2)
|
---|
2481 | break;
|
---|
2482 |
|
---|
2483 | /* compare again and check that it's a newline. */
|
---|
2484 | if (*s2 != '\n' || *s1 != '\n')
|
---|
2485 | break;
|
---|
2486 | }
|
---|
2487 | /* Break out if we exited because of EOS. */
|
---|
2488 | else if (s1 >= e1 || s2 >= e2)
|
---|
2489 | break;
|
---|
2490 |
|
---|
2491 | /*
|
---|
2492 | * Detect the end of command lines.
|
---|
2493 | */
|
---|
2494 | if (*s1 == '\n')
|
---|
2495 | new_cmd = s1 == s1_start || s1[-1] != '\\';
|
---|
2496 | s1++;
|
---|
2497 | s2++;
|
---|
2498 | }
|
---|
2499 |
|
---|
2500 | /*
|
---|
2501 | * Ignore trailing empty lines.
|
---|
2502 | */
|
---|
2503 | if (s1 < e1 || s2 < e2)
|
---|
2504 | {
|
---|
2505 | while (s1 < e1 && (isblank (*s1) || *s1 == '\n'))
|
---|
2506 | if (*s1++ == '\n')
|
---|
2507 | s1 = comp_cmds_strip_leading (s1, e1);
|
---|
2508 | while (s2 < e2 && (isblank (*s2) || *s2 == '\n'))
|
---|
2509 | if (*s2++ == '\n')
|
---|
2510 | s2 = comp_cmds_strip_leading (s2, e2);
|
---|
2511 | }
|
---|
2512 |
|
---|
2513 | /* emit the result. */
|
---|
2514 | if (s1 == e1 && s2 == e2)
|
---|
2515 | o = variable_buffer_output (o, "", 1);
|
---|
2516 | else
|
---|
2517 | o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
|
---|
2518 | }
|
---|
2519 | return o;
|
---|
2520 | }
|
---|
2521 |
|
---|
2522 | /*
|
---|
2523 | $(comp-vars var1,var2,not-equal-return)
|
---|
2524 | or
|
---|
2525 | $(comp-cmds cmd-var1,cmd-var2,not-equal-return)
|
---|
2526 |
|
---|
2527 | Compares the two variables (that's given by name to avoid unnecessary
|
---|
2528 | expanding) and return the string in the third argument if not equal.
|
---|
2529 | If equal, nothing is returned.
|
---|
2530 |
|
---|
2531 | comp-vars will to an exact comparision only stripping leading and
|
---|
2532 | trailing spaces.
|
---|
2533 |
|
---|
2534 | comp-cmds will compare command by command, ignoring not only leading
|
---|
2535 | and trailing spaces on each line but also leading one leading '@' and '-'.
|
---|
2536 | */
|
---|
2537 | static char *
|
---|
2538 | func_comp_vars (char *o, char **argv, const char *funcname)
|
---|
2539 | {
|
---|
2540 | const char *s1, *e1, *x1, *s2, *e2, *x2;
|
---|
2541 | char *a1 = NULL, *a2 = NULL;
|
---|
2542 | size_t l, l1, l2;
|
---|
2543 | struct variable *var1 = lookup_variable (argv[0], strlen (argv[0]));
|
---|
2544 | struct variable *var2 = lookup_variable (argv[1], strlen (argv[1]));
|
---|
2545 |
|
---|
2546 | /* the simple cases */
|
---|
2547 | if (var1 == var2)
|
---|
2548 | return variable_buffer_output (o, "", 1); /* eq */
|
---|
2549 | if (!var1 || !var2)
|
---|
2550 | return variable_buffer_output (o, argv[2], strlen(argv[2]));
|
---|
2551 | if (var1->value == var2->value)
|
---|
2552 | return variable_buffer_output (o, "", 1); /* eq */
|
---|
2553 | if (!var1->recursive && !var2->recursive)
|
---|
2554 | {
|
---|
2555 | if ( var1->value_length == var2->value_length
|
---|
2556 | && !memcmp (var1->value, var2->value, var1->value_length))
|
---|
2557 | return variable_buffer_output (o, "", 1); /* eq */
|
---|
2558 |
|
---|
2559 | /* ignore trailing and leading blanks */
|
---|
2560 | s1 = var1->value;
|
---|
2561 | while (isblank ((unsigned char) *s1))
|
---|
2562 | s1++;
|
---|
2563 | e1 = s1 + var1->value_length;
|
---|
2564 | while (e1 > s1 && isblank ((unsigned char) e1[-1]))
|
---|
2565 | e1--;
|
---|
2566 |
|
---|
2567 | s2 = var2->value;
|
---|
2568 | while (isblank ((unsigned char) *s2))
|
---|
2569 | s2++;
|
---|
2570 | e2 = s2 + var2->value_length;
|
---|
2571 | while (e2 > s2 && isblank ((unsigned char) e2[-1]))
|
---|
2572 | e2--;
|
---|
2573 |
|
---|
2574 | if (e1 - s1 != e2 - s2)
|
---|
2575 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2576 | l_simple_compare:
|
---|
2577 | if (!memcmp (s1, s2, e1 - s1))
|
---|
2578 | return variable_buffer_output (o, "", 1); /* eq */
|
---|
2579 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2580 | }
|
---|
2581 |
|
---|
2582 | /* ignore trailing and leading blanks */
|
---|
2583 | s1 = var1->value;
|
---|
2584 | e1 = s1 + var1->value_length;
|
---|
2585 | while (isblank ((unsigned char) *s1))
|
---|
2586 | s1++;
|
---|
2587 | while (e1 > s1 && isblank ((unsigned char) e1[-1]))
|
---|
2588 | e1--;
|
---|
2589 |
|
---|
2590 | s2 = var2->value;
|
---|
2591 | e2 = s2 + var2->value_length;
|
---|
2592 | while (isblank((unsigned char)*s2))
|
---|
2593 | s2++;
|
---|
2594 | while (e2 > s2 && isblank ((unsigned char) e2[-1]))
|
---|
2595 | e2--;
|
---|
2596 |
|
---|
2597 | /* both empty after stripping? */
|
---|
2598 | if (s1 == e1 && s2 == e2)
|
---|
2599 | return variable_buffer_output (o, "", 1); /* eq */
|
---|
2600 |
|
---|
2601 | /* optimist. */
|
---|
2602 | if ( e1 - s1 == e2 - s2
|
---|
2603 | && !memcmp(s1, s2, e1 - s1))
|
---|
2604 | return variable_buffer_output (o, "", 1); /* eq */
|
---|
2605 |
|
---|
2606 | /* compare up to the first '$' or the end. */
|
---|
2607 | x1 = var1->recursive ? memchr (s1, '$', e1 - s1) : NULL;
|
---|
2608 | x2 = var2->recursive ? memchr (s2, '$', e2 - s2) : NULL;
|
---|
2609 | if (!x1 && !x2)
|
---|
2610 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2611 |
|
---|
2612 | l1 = x1 ? x1 - s1 : e1 - s1;
|
---|
2613 | l2 = x2 ? x2 - s2 : e2 - s2;
|
---|
2614 | l = l1 <= l2 ? l1 : l2;
|
---|
2615 | if (l && memcmp (s1, s2, l))
|
---|
2616 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2617 |
|
---|
2618 | /* one or both buffers now require expanding. */
|
---|
2619 | if (!x1)
|
---|
2620 | s1 += l;
|
---|
2621 | else
|
---|
2622 | {
|
---|
2623 | s1 = a1 = allocated_variable_expand ((char *)s1 + l);
|
---|
2624 | if (!l)
|
---|
2625 | while (isblank ((unsigned char) *s1))
|
---|
2626 | s1++;
|
---|
2627 | e1 = strchr (s1, '\0');
|
---|
2628 | while (e1 > s1 && isblank ((unsigned char) e1[-1]))
|
---|
2629 | e1--;
|
---|
2630 | }
|
---|
2631 |
|
---|
2632 | if (!x2)
|
---|
2633 | s2 += l;
|
---|
2634 | else
|
---|
2635 | {
|
---|
2636 | s2 = a2 = allocated_variable_expand ((char *)s2 + l);
|
---|
2637 | if (!l)
|
---|
2638 | while (isblank ((unsigned char) *s2))
|
---|
2639 | s2++;
|
---|
2640 | e2 = strchr (s2, '\0');
|
---|
2641 | while (e2 > s2 && isblank ((unsigned char) e2[-1]))
|
---|
2642 | e2--;
|
---|
2643 | }
|
---|
2644 |
|
---|
2645 | /* the final compare */
|
---|
2646 | if ( e1 - s1 != e2 - s2
|
---|
2647 | || memcmp (s1, s2, e1 - s1))
|
---|
2648 | o = comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2649 | else
|
---|
2650 | o = variable_buffer_output (o, "", 1); /* eq */
|
---|
2651 | if (a1)
|
---|
2652 | free (a1);
|
---|
2653 | if (a2)
|
---|
2654 | free (a2);
|
---|
2655 | return o;
|
---|
2656 | }
|
---|
2657 | #endif
|
---|
2658 |
|
---|
2659 |
|
---|
2660 | #ifdef CONFIG_WITH_STACK
|
---|
2661 |
|
---|
2662 | /* Push an item (string without spaces). */
|
---|
2663 | static char *
|
---|
2664 | func_stack_push (char *o, char **argv, const char *funcname)
|
---|
2665 | {
|
---|
2666 | do_variable_definition(NILF, argv[0], argv[1], o_file, f_append, 0 /* !target_var */);
|
---|
2667 | return o;
|
---|
2668 | }
|
---|
2669 |
|
---|
2670 | /* Pops an item off the stack / get the top stack element.
|
---|
2671 | (This is what's tricky to do in pure GNU make syntax.) */
|
---|
2672 | static char *
|
---|
2673 | func_stack_pop_top (char *o, char **argv, const char *funcname)
|
---|
2674 | {
|
---|
2675 | struct variable *stack_var;
|
---|
2676 | const char *stack = argv[0];
|
---|
2677 | const int return_item = argv[0][sizeof("stack-pop") - 1] == '\0';
|
---|
2678 |
|
---|
2679 | stack_var = lookup_variable (stack, strlen (stack) );
|
---|
2680 | if (stack_var)
|
---|
2681 | {
|
---|
2682 | unsigned int len;
|
---|
2683 | char *iterator = stack_var->value;
|
---|
2684 | char *lastitem = NULL;
|
---|
2685 | char *cur;
|
---|
2686 |
|
---|
2687 | while ((cur = find_next_token (&iterator, &len)))
|
---|
2688 | lastitem = cur;
|
---|
2689 |
|
---|
2690 | if (lastitem != NULL)
|
---|
2691 | {
|
---|
2692 | if (strcmp (funcname, "stack-popv") != 0)
|
---|
2693 | o = variable_buffer_output (o, lastitem, len);
|
---|
2694 | if (strcmp (funcname, "stack-top") != 0)
|
---|
2695 | {
|
---|
2696 | *lastitem = '\0';
|
---|
2697 | while (lastitem > stack_var->value && isspace (lastitem[-1]))
|
---|
2698 | *--lastitem = '\0';
|
---|
2699 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
2700 | stack_var->value_length = lastitem - stack_var->value;
|
---|
2701 | #endif
|
---|
2702 | }
|
---|
2703 | }
|
---|
2704 | }
|
---|
2705 | return o;
|
---|
2706 | }
|
---|
2707 | #endif /* CONFIG_WITH_STACK */
|
---|
2708 |
|
---|
2709 | #ifdef CONFIG_WITH_MATH
|
---|
2710 |
|
---|
2711 | #include <ctype.h>
|
---|
2712 | #ifdef _MSC_VER
|
---|
2713 | typedef __int64 math_int;
|
---|
2714 | #else
|
---|
2715 | # include <stdint.h>
|
---|
2716 | typedef int64_t math_int;
|
---|
2717 | #endif
|
---|
2718 |
|
---|
2719 | /* Converts a string to an integer, causes an error if the format is invalid. */
|
---|
2720 | static math_int
|
---|
2721 | math_int_from_string (const char *str)
|
---|
2722 | {
|
---|
2723 | const char *start;
|
---|
2724 | unsigned base = 0;
|
---|
2725 | int negative = 0;
|
---|
2726 | math_int num = 0;
|
---|
2727 |
|
---|
2728 | /* strip spaces */
|
---|
2729 | while (isspace (*str))
|
---|
2730 | str++;
|
---|
2731 | if (!*str)
|
---|
2732 | {
|
---|
2733 | error (NILF, _("bad number: empty\n"));
|
---|
2734 | return 0;
|
---|
2735 | }
|
---|
2736 | start = str;
|
---|
2737 |
|
---|
2738 | /* check for +/- */
|
---|
2739 | while (*str == '+' || *str == '-' || isspace (*str))
|
---|
2740 | if (*str++ == '-')
|
---|
2741 | negative = !negative;
|
---|
2742 |
|
---|
2743 | /* check for prefix - we do not accept octal numbers, sorry. */
|
---|
2744 | if (*str == '0' && (str[1] == 'x' || str[1] == 'X'))
|
---|
2745 | {
|
---|
2746 | base = 16;
|
---|
2747 | str += 2;
|
---|
2748 | }
|
---|
2749 | else
|
---|
2750 | {
|
---|
2751 | /* look for a hex digit, if not found treat it as decimal */
|
---|
2752 | const char *p2 = str;
|
---|
2753 | for ( ; *p2; p2++)
|
---|
2754 | if (isxdigit (*p2) && !isdigit (*p2) && isascii (*p2) )
|
---|
2755 | {
|
---|
2756 | base = 16;
|
---|
2757 | break;
|
---|
2758 | }
|
---|
2759 | if (base == 0)
|
---|
2760 | base = 10;
|
---|
2761 | }
|
---|
2762 |
|
---|
2763 | /* must have at least one digit! */
|
---|
2764 | if ( !isascii (*str)
|
---|
2765 | || !(base == 16 ? isxdigit (*str) : isdigit (*str)) )
|
---|
2766 | {
|
---|
2767 | error (NILF, _("bad number: '%s'\n"), start);
|
---|
2768 | return 0;
|
---|
2769 | }
|
---|
2770 |
|
---|
2771 | /* convert it! */
|
---|
2772 | while (*str && !isspace (*str))
|
---|
2773 | {
|
---|
2774 | int ch = *str++;
|
---|
2775 | if (ch >= '0' && ch <= '9')
|
---|
2776 | ch -= '0';
|
---|
2777 | else if (base == 16 && ch >= 'a' && ch <= 'f')
|
---|
2778 | ch -= 'a' - 10;
|
---|
2779 | else if (base == 16 && ch >= 'A' && ch <= 'F')
|
---|
2780 | ch -= 'A' - 10;
|
---|
2781 | else
|
---|
2782 | {
|
---|
2783 | error (NILF, _("bad number: '%s' (base=%d, pos=%d)\n"), start, base, str - start);
|
---|
2784 | return 0;
|
---|
2785 | }
|
---|
2786 | num *= base;
|
---|
2787 | num += ch;
|
---|
2788 | }
|
---|
2789 |
|
---|
2790 | /* check trailing spaces. */
|
---|
2791 | while (isspace (*str))
|
---|
2792 | str++;
|
---|
2793 | if (*str)
|
---|
2794 | {
|
---|
2795 | error (NILF, _("bad number: '%s'\n"), start);
|
---|
2796 | return 0;
|
---|
2797 | }
|
---|
2798 |
|
---|
2799 | return negative ? -num : num;
|
---|
2800 | }
|
---|
2801 |
|
---|
2802 | /* outputs the number (as a string) into the variable buffer. */
|
---|
2803 | static char *
|
---|
2804 | math_int_to_variable_buffer (char *o, math_int num)
|
---|
2805 | {
|
---|
2806 | static const char xdigits[17] = "0123456789abcdef";
|
---|
2807 | int negative;
|
---|
2808 | char strbuf[24]; /* 16 hex + 2 prefix + sign + term => 20 */
|
---|
2809 | char *str = &strbuf[sizeof (strbuf) - 1];
|
---|
2810 |
|
---|
2811 | negative = num < 0;
|
---|
2812 | if (negative)
|
---|
2813 | num = -num;
|
---|
2814 |
|
---|
2815 | *str-- = '\0';
|
---|
2816 |
|
---|
2817 | do
|
---|
2818 | {
|
---|
2819 | *str-- = xdigits[num & 0xf];
|
---|
2820 | num >>= 4;
|
---|
2821 | }
|
---|
2822 | while (num);
|
---|
2823 |
|
---|
2824 | *str-- = 'x';
|
---|
2825 | *str = '0';
|
---|
2826 |
|
---|
2827 | if (negative)
|
---|
2828 | *--str = '-';
|
---|
2829 |
|
---|
2830 | return variable_buffer_output (o, str, &strbuf[sizeof (strbuf) - 1] - str);
|
---|
2831 | }
|
---|
2832 |
|
---|
2833 | /* Add two or more integer numbers. */
|
---|
2834 | static char *
|
---|
2835 | func_int_add (char *o, char **argv, const char *funcname)
|
---|
2836 | {
|
---|
2837 | math_int num;
|
---|
2838 | int i;
|
---|
2839 |
|
---|
2840 | num = math_int_from_string (argv[0]);
|
---|
2841 | for (i = 1; argv[i]; i++)
|
---|
2842 | num += math_int_from_string (argv[i]);
|
---|
2843 |
|
---|
2844 | return math_int_to_variable_buffer (o, num);
|
---|
2845 | }
|
---|
2846 |
|
---|
2847 | /* Subtract two or more integer numbers. */
|
---|
2848 | static char *
|
---|
2849 | func_int_sub (char *o, char **argv, const char *funcname)
|
---|
2850 | {
|
---|
2851 | math_int num;
|
---|
2852 | int i;
|
---|
2853 |
|
---|
2854 | num = math_int_from_string (argv[0]);
|
---|
2855 | for (i = 1; argv[i]; i++)
|
---|
2856 | num -= math_int_from_string (argv[i]);
|
---|
2857 |
|
---|
2858 | return math_int_to_variable_buffer (o, num);
|
---|
2859 | }
|
---|
2860 |
|
---|
2861 | /* Multiply two or more integer numbers. */
|
---|
2862 | static char *
|
---|
2863 | func_int_mul (char *o, char **argv, const char *funcname)
|
---|
2864 | {
|
---|
2865 | math_int num;
|
---|
2866 | int i;
|
---|
2867 |
|
---|
2868 | num = math_int_from_string (argv[0]);
|
---|
2869 | for (i = 1; argv[i]; i++)
|
---|
2870 | num *= math_int_from_string (argv[i]);
|
---|
2871 |
|
---|
2872 | return math_int_to_variable_buffer (o, num);
|
---|
2873 | }
|
---|
2874 |
|
---|
2875 | /* Divide an integer number by one or more divisors. */
|
---|
2876 | static char *
|
---|
2877 | func_int_div (char *o, char **argv, const char *funcname)
|
---|
2878 | {
|
---|
2879 | math_int num;
|
---|
2880 | math_int divisor;
|
---|
2881 | int i;
|
---|
2882 |
|
---|
2883 | num = math_int_from_string (argv[0]);
|
---|
2884 | for (i = 1; argv[i]; i++)
|
---|
2885 | {
|
---|
2886 | divisor = math_int_from_string (argv[i]);
|
---|
2887 | if (!divisor)
|
---|
2888 | {
|
---|
2889 | error (NILF, _("divide by zero ('%s')\n"), argv[i]);
|
---|
2890 | return math_int_to_variable_buffer (o, 0);
|
---|
2891 | }
|
---|
2892 | num /= divisor;
|
---|
2893 | }
|
---|
2894 |
|
---|
2895 | return math_int_to_variable_buffer (o, num);
|
---|
2896 | }
|
---|
2897 |
|
---|
2898 |
|
---|
2899 | /* Divide and return the remainder. */
|
---|
2900 | static char *
|
---|
2901 | func_int_mod (char *o, char **argv, const char *funcname)
|
---|
2902 | {
|
---|
2903 | math_int num;
|
---|
2904 | math_int divisor;
|
---|
2905 |
|
---|
2906 | num = math_int_from_string (argv[0]);
|
---|
2907 | divisor = math_int_from_string (argv[1]);
|
---|
2908 | if (!divisor)
|
---|
2909 | {
|
---|
2910 | error (NILF, _("divide by zero ('%s')\n"), argv[1]);
|
---|
2911 | return math_int_to_variable_buffer (o, 0);
|
---|
2912 | }
|
---|
2913 | num %= divisor;
|
---|
2914 |
|
---|
2915 | return math_int_to_variable_buffer (o, num);
|
---|
2916 | }
|
---|
2917 |
|
---|
2918 | /* 2-complement. */
|
---|
2919 | static char *
|
---|
2920 | func_int_not (char *o, char **argv, const char *funcname)
|
---|
2921 | {
|
---|
2922 | math_int num;
|
---|
2923 |
|
---|
2924 | num = math_int_from_string (argv[0]);
|
---|
2925 | num = ~num;
|
---|
2926 |
|
---|
2927 | return math_int_to_variable_buffer (o, num);
|
---|
2928 | }
|
---|
2929 |
|
---|
2930 | /* Bitwise AND (two or more numbers). */
|
---|
2931 | static char *
|
---|
2932 | func_int_and (char *o, char **argv, const char *funcname)
|
---|
2933 | {
|
---|
2934 | math_int num;
|
---|
2935 | int i;
|
---|
2936 |
|
---|
2937 | num = math_int_from_string (argv[0]);
|
---|
2938 | for (i = 1; argv[i]; i++)
|
---|
2939 | num &= math_int_from_string (argv[i]);
|
---|
2940 |
|
---|
2941 | return math_int_to_variable_buffer (o, num);
|
---|
2942 | }
|
---|
2943 |
|
---|
2944 | /* Bitwise OR (two or more numbers). */
|
---|
2945 | static char *
|
---|
2946 | func_int_or (char *o, char **argv, const char *funcname)
|
---|
2947 | {
|
---|
2948 | math_int num;
|
---|
2949 | int i;
|
---|
2950 |
|
---|
2951 | num = math_int_from_string (argv[0]);
|
---|
2952 | for (i = 1; argv[i]; i++)
|
---|
2953 | num |= math_int_from_string (argv[i]);
|
---|
2954 |
|
---|
2955 | return math_int_to_variable_buffer (o, num);
|
---|
2956 | }
|
---|
2957 |
|
---|
2958 | /* Bitwise XOR (two or more numbers). */
|
---|
2959 | static char *
|
---|
2960 | func_int_xor (char *o, char **argv, const char *funcname)
|
---|
2961 | {
|
---|
2962 | math_int num;
|
---|
2963 | int i;
|
---|
2964 |
|
---|
2965 | num = math_int_from_string (argv[0]);
|
---|
2966 | for (i = 1; argv[i]; i++)
|
---|
2967 | num ^= math_int_from_string (argv[i]);
|
---|
2968 |
|
---|
2969 | return math_int_to_variable_buffer (o, num);
|
---|
2970 | }
|
---|
2971 |
|
---|
2972 | /* Compare two integer numbers. Returns make boolean (true="1"; false=""). */
|
---|
2973 | static char *
|
---|
2974 | func_int_cmp (char *o, char **argv, const char *funcname)
|
---|
2975 | {
|
---|
2976 | math_int num1;
|
---|
2977 | math_int num2;
|
---|
2978 | int rc;
|
---|
2979 |
|
---|
2980 | num1 = math_int_from_string (argv[0]);
|
---|
2981 | num2 = math_int_from_string (argv[1]);
|
---|
2982 |
|
---|
2983 | funcname += sizeof ("int-") - 1;
|
---|
2984 | if (!strcmp (funcname, "eq"))
|
---|
2985 | rc = num1 == num2;
|
---|
2986 | else if (!strcmp (funcname, "ne"))
|
---|
2987 | rc = num1 != num2;
|
---|
2988 | else if (!strcmp (funcname, "gt"))
|
---|
2989 | rc = num1 > num2;
|
---|
2990 | else if (!strcmp (funcname, "ge"))
|
---|
2991 | rc = num1 >= num2;
|
---|
2992 | else if (!strcmp (funcname, "lt"))
|
---|
2993 | rc = num1 < num2;
|
---|
2994 | else /*if (!strcmp (funcname, "le"))*/
|
---|
2995 | rc = num1 <= num2;
|
---|
2996 |
|
---|
2997 | return variable_buffer_output (o, rc ? "1" : "", rc);
|
---|
2998 | }
|
---|
2999 |
|
---|
3000 | #endif /* CONFIG_WITH_MATH */
|
---|
3001 |
|
---|
3002 |
|
---|
3003 | /* Lookup table for builtin functions.
|
---|
3004 |
|
---|
3005 | This doesn't have to be sorted; we use a straight lookup. We might gain
|
---|
3006 | some efficiency by moving most often used functions to the start of the
|
---|
3007 | table.
|
---|
3008 |
|
---|
3009 | If MAXIMUM_ARGS is 0, that means there is no maximum and all
|
---|
3010 | comma-separated values are treated as arguments.
|
---|
3011 |
|
---|
3012 | EXPAND_ARGS means that all arguments should be expanded before invocation.
|
---|
3013 | Functions that do namespace tricks (foreach) don't automatically expand. */
|
---|
3014 |
|
---|
3015 | static char *func_call (char *o, char **argv, const char *funcname);
|
---|
3016 |
|
---|
3017 |
|
---|
3018 | static struct function_table_entry function_table_init[] =
|
---|
3019 | {
|
---|
3020 | /* Name/size */ /* MIN MAX EXP? Function */
|
---|
3021 | { STRING_SIZE_TUPLE("abspath"), 0, 1, 1, func_abspath},
|
---|
3022 | { STRING_SIZE_TUPLE("addprefix"), 2, 2, 1, func_addsuffix_addprefix},
|
---|
3023 | { STRING_SIZE_TUPLE("addsuffix"), 2, 2, 1, func_addsuffix_addprefix},
|
---|
3024 | { STRING_SIZE_TUPLE("basename"), 0, 1, 1, func_basename_dir},
|
---|
3025 | { STRING_SIZE_TUPLE("dir"), 0, 1, 1, func_basename_dir},
|
---|
3026 | { STRING_SIZE_TUPLE("notdir"), 0, 1, 1, func_notdir_suffix},
|
---|
3027 | { STRING_SIZE_TUPLE("subst"), 3, 3, 1, func_subst},
|
---|
3028 | { STRING_SIZE_TUPLE("suffix"), 0, 1, 1, func_notdir_suffix},
|
---|
3029 | { STRING_SIZE_TUPLE("filter"), 2, 2, 1, func_filter_filterout},
|
---|
3030 | { STRING_SIZE_TUPLE("filter-out"), 2, 2, 1, func_filter_filterout},
|
---|
3031 | { STRING_SIZE_TUPLE("findstring"), 2, 2, 1, func_findstring},
|
---|
3032 | { STRING_SIZE_TUPLE("firstword"), 0, 1, 1, func_firstword},
|
---|
3033 | { STRING_SIZE_TUPLE("flavor"), 0, 1, 1, func_flavor},
|
---|
3034 | { STRING_SIZE_TUPLE("join"), 2, 2, 1, func_join},
|
---|
3035 | { STRING_SIZE_TUPLE("lastword"), 0, 1, 1, func_lastword},
|
---|
3036 | { STRING_SIZE_TUPLE("patsubst"), 3, 3, 1, func_patsubst},
|
---|
3037 | { STRING_SIZE_TUPLE("realpath"), 0, 1, 1, func_realpath},
|
---|
3038 | #ifdef CONFIG_WITH_RSORT
|
---|
3039 | { STRING_SIZE_TUPLE("rsort"), 0, 1, 1, func_sort},
|
---|
3040 | #endif
|
---|
3041 | { STRING_SIZE_TUPLE("shell"), 0, 1, 1, func_shell},
|
---|
3042 | { STRING_SIZE_TUPLE("sort"), 0, 1, 1, func_sort},
|
---|
3043 | { STRING_SIZE_TUPLE("strip"), 0, 1, 1, func_strip},
|
---|
3044 | { STRING_SIZE_TUPLE("wildcard"), 0, 1, 1, func_wildcard},
|
---|
3045 | { STRING_SIZE_TUPLE("word"), 2, 2, 1, func_word},
|
---|
3046 | { STRING_SIZE_TUPLE("wordlist"), 3, 3, 1, func_wordlist},
|
---|
3047 | { STRING_SIZE_TUPLE("words"), 0, 1, 1, func_words},
|
---|
3048 | { STRING_SIZE_TUPLE("origin"), 0, 1, 1, func_origin},
|
---|
3049 | { STRING_SIZE_TUPLE("foreach"), 3, 3, 0, func_foreach},
|
---|
3050 | { STRING_SIZE_TUPLE("call"), 1, 0, 1, func_call},
|
---|
3051 | { STRING_SIZE_TUPLE("info"), 0, 1, 1, func_error},
|
---|
3052 | { STRING_SIZE_TUPLE("error"), 0, 1, 1, func_error},
|
---|
3053 | { STRING_SIZE_TUPLE("warning"), 0, 1, 1, func_error},
|
---|
3054 | { STRING_SIZE_TUPLE("if"), 2, 3, 0, func_if},
|
---|
3055 | { STRING_SIZE_TUPLE("or"), 1, 0, 0, func_or},
|
---|
3056 | { STRING_SIZE_TUPLE("and"), 1, 0, 0, func_and},
|
---|
3057 | { STRING_SIZE_TUPLE("value"), 0, 1, 1, func_value},
|
---|
3058 | { STRING_SIZE_TUPLE("eval"), 0, 1, 1, func_eval},
|
---|
3059 | #ifdef EXPERIMENTAL
|
---|
3060 | { STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq},
|
---|
3061 | { STRING_SIZE_TUPLE("not"), 0, 1, 1, func_not},
|
---|
3062 | #endif
|
---|
3063 | #ifdef CONFIG_WITH_TOUPPER_TOLOWER
|
---|
3064 | { STRING_SIZE_TUPLE("toupper"), 0, 1, 1, func_toupper_tolower},
|
---|
3065 | { STRING_SIZE_TUPLE("tolower"), 0, 1, 1, func_toupper_tolower},
|
---|
3066 | #endif
|
---|
3067 | #ifdef CONFIG_WITH_ABSPATHEX
|
---|
3068 | { STRING_SIZE_TUPLE("abspathex"), 0, 2, 1, func_abspathex},
|
---|
3069 | #endif
|
---|
3070 | #ifdef CONFIG_WITH_XARGS
|
---|
3071 | { STRING_SIZE_TUPLE("xargs"), 2, 0, 1, func_xargs},
|
---|
3072 | #endif
|
---|
3073 | #if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
|
---|
3074 | { STRING_SIZE_TUPLE("comp-vars"), 3, 3, 1, func_comp_vars},
|
---|
3075 | { STRING_SIZE_TUPLE("comp-cmds"), 3, 3, 1, func_comp_vars},
|
---|
3076 | #endif
|
---|
3077 | #ifdef CONFIG_WITH_STACK
|
---|
3078 | { STRING_SIZE_TUPLE("stack-push"), 2, 2, 1, func_stack_push},
|
---|
3079 | { STRING_SIZE_TUPLE("stack-pop"), 1, 1, 1, func_stack_pop_top},
|
---|
3080 | { STRING_SIZE_TUPLE("stack-popv"), 1, 1, 1, func_stack_pop_top},
|
---|
3081 | { STRING_SIZE_TUPLE("stack-top"), 1, 1, 1, func_stack_pop_top},
|
---|
3082 | #endif
|
---|
3083 | #ifdef CONFIG_WITH_MATH
|
---|
3084 | { STRING_SIZE_TUPLE("int-add"), 2, 0, 1, func_int_add},
|
---|
3085 | { STRING_SIZE_TUPLE("int-sub"), 2, 0, 1, func_int_sub},
|
---|
3086 | { STRING_SIZE_TUPLE("int-mul"), 2, 0, 1, func_int_mul},
|
---|
3087 | { STRING_SIZE_TUPLE("int-div"), 2, 0, 1, func_int_div},
|
---|
3088 | { STRING_SIZE_TUPLE("int-mod"), 2, 2, 1, func_int_mod},
|
---|
3089 | { STRING_SIZE_TUPLE("int-not"), 1, 1, 1, func_int_not},
|
---|
3090 | { STRING_SIZE_TUPLE("int-and"), 2, 0, 1, func_int_and},
|
---|
3091 | { STRING_SIZE_TUPLE("int-or"), 2, 0, 1, func_int_or},
|
---|
3092 | { STRING_SIZE_TUPLE("int-xor"), 2, 0, 1, func_int_xor},
|
---|
3093 | { STRING_SIZE_TUPLE("int-eq"), 2, 2, 1, func_int_cmp},
|
---|
3094 | { STRING_SIZE_TUPLE("int-ne"), 2, 2, 1, func_int_cmp},
|
---|
3095 | { STRING_SIZE_TUPLE("int-gt"), 2, 2, 1, func_int_cmp},
|
---|
3096 | { STRING_SIZE_TUPLE("int-ge"), 2, 2, 1, func_int_cmp},
|
---|
3097 | { STRING_SIZE_TUPLE("int-lt"), 2, 2, 1, func_int_cmp},
|
---|
3098 | { STRING_SIZE_TUPLE("int-le"), 2, 2, 1, func_int_cmp},
|
---|
3099 | #endif
|
---|
3100 | #ifdef KMK_HELPERS
|
---|
3101 | { STRING_SIZE_TUPLE("kb-src-tool"), 1, 1, 0, func_kbuild_source_tool},
|
---|
3102 | { STRING_SIZE_TUPLE("kb-obj-base"), 1, 1, 0, func_kbuild_object_base},
|
---|
3103 | { STRING_SIZE_TUPLE("kb-obj-suff"), 1, 1, 0, func_kbuild_object_suffix},
|
---|
3104 | { STRING_SIZE_TUPLE("kb-src-prop"), 4, 4, 0, func_kbuild_source_prop},
|
---|
3105 | { STRING_SIZE_TUPLE("kb-src-one"), 0, 1, 0, func_kbuild_source_one},
|
---|
3106 | #endif
|
---|
3107 | };
|
---|
3108 |
|
---|
3109 | #define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry))
|
---|
3110 | |
---|
3111 |
|
---|
3112 |
|
---|
3113 | /* These must come after the definition of function_table. */
|
---|
3114 |
|
---|
3115 | static char *
|
---|
3116 | expand_builtin_function (char *o, int argc, char **argv,
|
---|
3117 | const struct function_table_entry *entry_p)
|
---|
3118 | {
|
---|
3119 | if (argc < (int)entry_p->minimum_args)
|
---|
3120 | fatal (*expanding_var,
|
---|
3121 | _("insufficient number of arguments (%d) to function `%s'"),
|
---|
3122 | argc, entry_p->name);
|
---|
3123 |
|
---|
3124 | /* I suppose technically some function could do something with no
|
---|
3125 | arguments, but so far none do, so just test it for all functions here
|
---|
3126 | rather than in each one. We can change it later if necessary. */
|
---|
3127 |
|
---|
3128 | if (!argc)
|
---|
3129 | return o;
|
---|
3130 |
|
---|
3131 | if (!entry_p->func_ptr)
|
---|
3132 | fatal (*expanding_var,
|
---|
3133 | _("unimplemented on this platform: function `%s'"), entry_p->name);
|
---|
3134 |
|
---|
3135 | return entry_p->func_ptr (o, argv, entry_p->name);
|
---|
3136 | }
|
---|
3137 |
|
---|
3138 | /* Check for a function invocation in *STRINGP. *STRINGP points at the
|
---|
3139 | opening ( or { and is not null-terminated. If a function invocation
|
---|
3140 | is found, expand it into the buffer at *OP, updating *OP, incrementing
|
---|
3141 | *STRINGP past the reference and returning nonzero. If not, return zero. */
|
---|
3142 |
|
---|
3143 | static int
|
---|
3144 | handle_function2 (const struct function_table_entry *entry_p, char **op, const char **stringp) /* bird split it up. */
|
---|
3145 | {
|
---|
3146 | char openparen = (*stringp)[0];
|
---|
3147 | char closeparen = openparen == '(' ? ')' : '}';
|
---|
3148 | const char *beg;
|
---|
3149 | const char *end;
|
---|
3150 | int count = 0;
|
---|
3151 | char *abeg = NULL;
|
---|
3152 | char **argv, **argvp;
|
---|
3153 | int nargs;
|
---|
3154 |
|
---|
3155 | beg = *stringp + 1;
|
---|
3156 |
|
---|
3157 | /* We found a builtin function. Find the beginning of its arguments (skip
|
---|
3158 | whitespace after the name). */
|
---|
3159 |
|
---|
3160 | beg = next_token (beg + entry_p->len);
|
---|
3161 |
|
---|
3162 | /* Find the end of the function invocation, counting nested use of
|
---|
3163 | whichever kind of parens we use. Since we're looking, count commas
|
---|
3164 | to get a rough estimate of how many arguments we might have. The
|
---|
3165 | count might be high, but it'll never be low. */
|
---|
3166 |
|
---|
3167 | for (nargs=1, end=beg; *end != '\0'; ++end)
|
---|
3168 | if (*end == ',')
|
---|
3169 | ++nargs;
|
---|
3170 | else if (*end == openparen)
|
---|
3171 | ++count;
|
---|
3172 | else if (*end == closeparen && --count < 0)
|
---|
3173 | break;
|
---|
3174 |
|
---|
3175 | if (count >= 0)
|
---|
3176 | fatal (*expanding_var,
|
---|
3177 | _("unterminated call to function `%s': missing `%c'"),
|
---|
3178 | entry_p->name, closeparen);
|
---|
3179 |
|
---|
3180 | *stringp = end;
|
---|
3181 |
|
---|
3182 | /* Get some memory to store the arg pointers. */
|
---|
3183 | argvp = argv = alloca (sizeof (char *) * (nargs + 2));
|
---|
3184 |
|
---|
3185 | /* Chop the string into arguments, then a nul. As soon as we hit
|
---|
3186 | MAXIMUM_ARGS (if it's >0) assume the rest of the string is part of the
|
---|
3187 | last argument.
|
---|
3188 |
|
---|
3189 | If we're expanding, store pointers to the expansion of each one. If
|
---|
3190 | not, make a duplicate of the string and point into that, nul-terminating
|
---|
3191 | each argument. */
|
---|
3192 |
|
---|
3193 | if (entry_p->expand_args)
|
---|
3194 | {
|
---|
3195 | const char *p;
|
---|
3196 | for (p=beg, nargs=0; p <= end; ++argvp)
|
---|
3197 | {
|
---|
3198 | const char *next;
|
---|
3199 |
|
---|
3200 | ++nargs;
|
---|
3201 |
|
---|
3202 | if (nargs == entry_p->maximum_args
|
---|
3203 | || (! (next = find_next_argument (openparen, closeparen, p, end))))
|
---|
3204 | next = end;
|
---|
3205 |
|
---|
3206 | *argvp = expand_argument (p, next);
|
---|
3207 | p = next + 1;
|
---|
3208 | }
|
---|
3209 | }
|
---|
3210 | else
|
---|
3211 | {
|
---|
3212 | int len = end - beg;
|
---|
3213 | char *p, *aend;
|
---|
3214 |
|
---|
3215 | abeg = xmalloc (len+1);
|
---|
3216 | memcpy (abeg, beg, len);
|
---|
3217 | abeg[len] = '\0';
|
---|
3218 | aend = abeg + len;
|
---|
3219 |
|
---|
3220 | for (p=abeg, nargs=0; p <= aend; ++argvp)
|
---|
3221 | {
|
---|
3222 | char *next;
|
---|
3223 |
|
---|
3224 | ++nargs;
|
---|
3225 |
|
---|
3226 | if (nargs == entry_p->maximum_args
|
---|
3227 | || (! (next = find_next_argument (openparen, closeparen, p, aend))))
|
---|
3228 | next = aend;
|
---|
3229 |
|
---|
3230 | *argvp = p;
|
---|
3231 | *next = '\0';
|
---|
3232 | p = next + 1;
|
---|
3233 | }
|
---|
3234 | }
|
---|
3235 | *argvp = NULL;
|
---|
3236 |
|
---|
3237 | /* Finally! Run the function... */
|
---|
3238 | *op = expand_builtin_function (*op, nargs, argv, entry_p);
|
---|
3239 |
|
---|
3240 | /* Free memory. */
|
---|
3241 | if (entry_p->expand_args)
|
---|
3242 | for (argvp=argv; *argvp != 0; ++argvp)
|
---|
3243 | free (*argvp);
|
---|
3244 | if (abeg)
|
---|
3245 | free (abeg);
|
---|
3246 |
|
---|
3247 | return 1;
|
---|
3248 | }
|
---|
3249 |
|
---|
3250 | int
|
---|
3251 | handle_function (char **op, const char **stringp) /* bird split it up */
|
---|
3252 | {
|
---|
3253 | const struct function_table_entry *entry_p = lookup_function (*stringp + 1);
|
---|
3254 | if (!entry_p)
|
---|
3255 | return 0;
|
---|
3256 | return handle_function2 (entry_p, op, stringp);
|
---|
3257 | }
|
---|
3258 | |
---|
3259 |
|
---|
3260 |
|
---|
3261 | /* User-defined functions. Expand the first argument as either a builtin
|
---|
3262 | function or a make variable, in the context of the rest of the arguments
|
---|
3263 | assigned to $1, $2, ... $N. $0 is the name of the function. */
|
---|
3264 |
|
---|
3265 | static char *
|
---|
3266 | func_call (char *o, char **argv, const char *funcname UNUSED)
|
---|
3267 | {
|
---|
3268 | static int max_args = 0;
|
---|
3269 | char *fname;
|
---|
3270 | char *cp;
|
---|
3271 | char *body;
|
---|
3272 | int flen;
|
---|
3273 | int i;
|
---|
3274 | int saved_args;
|
---|
3275 | const struct function_table_entry *entry_p;
|
---|
3276 | struct variable *v;
|
---|
3277 |
|
---|
3278 | /* There is no way to define a variable with a space in the name, so strip
|
---|
3279 | leading and trailing whitespace as a favor to the user. */
|
---|
3280 | fname = argv[0];
|
---|
3281 | while (*fname != '\0' && isspace ((unsigned char)*fname))
|
---|
3282 | ++fname;
|
---|
3283 |
|
---|
3284 | cp = fname + strlen (fname) - 1;
|
---|
3285 | while (cp > fname && isspace ((unsigned char)*cp))
|
---|
3286 | --cp;
|
---|
3287 | cp[1] = '\0';
|
---|
3288 |
|
---|
3289 | /* Calling nothing is a no-op */
|
---|
3290 | if (*fname == '\0')
|
---|
3291 | return o;
|
---|
3292 |
|
---|
3293 | /* Are we invoking a builtin function? */
|
---|
3294 |
|
---|
3295 | entry_p = lookup_function (fname);
|
---|
3296 | if (entry_p)
|
---|
3297 | {
|
---|
3298 | /* How many arguments do we have? */
|
---|
3299 | for (i=0; argv[i+1]; ++i)
|
---|
3300 | ;
|
---|
3301 | return expand_builtin_function (o, i, argv+1, entry_p);
|
---|
3302 | }
|
---|
3303 |
|
---|
3304 | /* Not a builtin, so the first argument is the name of a variable to be
|
---|
3305 | expanded and interpreted as a function. Find it. */
|
---|
3306 | flen = strlen (fname);
|
---|
3307 |
|
---|
3308 | v = lookup_variable (fname, flen);
|
---|
3309 |
|
---|
3310 | if (v == 0)
|
---|
3311 | warn_undefined (fname, flen);
|
---|
3312 |
|
---|
3313 | if (v == 0 || *v->value == '\0')
|
---|
3314 | return o;
|
---|
3315 |
|
---|
3316 | body = alloca (flen + 4);
|
---|
3317 | body[0] = '$';
|
---|
3318 | body[1] = '(';
|
---|
3319 | memcpy (body + 2, fname, flen);
|
---|
3320 | body[flen+2] = ')';
|
---|
3321 | body[flen+3] = '\0';
|
---|
3322 |
|
---|
3323 | /* Set up arguments $(1) .. $(N). $(0) is the function name. */
|
---|
3324 |
|
---|
3325 | push_new_variable_scope ();
|
---|
3326 |
|
---|
3327 | for (i=0; *argv; ++i, ++argv)
|
---|
3328 | {
|
---|
3329 | char num[11];
|
---|
3330 |
|
---|
3331 | sprintf (num, "%d", i);
|
---|
3332 | define_variable (num, strlen (num), *argv, o_automatic, 0);
|
---|
3333 | }
|
---|
3334 |
|
---|
3335 | /* If the number of arguments we have is < max_args, it means we're inside
|
---|
3336 | a recursive invocation of $(call ...). Fill in the remaining arguments
|
---|
3337 | in the new scope with the empty value, to hide them from this
|
---|
3338 | invocation. */
|
---|
3339 |
|
---|
3340 | for (; i < max_args; ++i)
|
---|
3341 | {
|
---|
3342 | char num[11];
|
---|
3343 |
|
---|
3344 | sprintf (num, "%d", i);
|
---|
3345 | define_variable (num, strlen (num), "", o_automatic, 0);
|
---|
3346 | }
|
---|
3347 |
|
---|
3348 | /* Expand the body in the context of the arguments, adding the result to
|
---|
3349 | the variable buffer. */
|
---|
3350 |
|
---|
3351 | v->exp_count = EXP_COUNT_MAX;
|
---|
3352 |
|
---|
3353 | saved_args = max_args;
|
---|
3354 | max_args = i;
|
---|
3355 | o = variable_expand_string (o, body, flen+3);
|
---|
3356 | max_args = saved_args;
|
---|
3357 |
|
---|
3358 | v->exp_count = 0;
|
---|
3359 |
|
---|
3360 | pop_variable_scope ();
|
---|
3361 |
|
---|
3362 | return o + strlen (o);
|
---|
3363 | }
|
---|
3364 |
|
---|
3365 | void
|
---|
3366 | hash_init_function_table (void)
|
---|
3367 | {
|
---|
3368 | hash_init (&function_table, FUNCTION_TABLE_ENTRIES * 2,
|
---|
3369 | function_table_entry_hash_1, function_table_entry_hash_2,
|
---|
3370 | function_table_entry_hash_cmp);
|
---|
3371 | hash_load (&function_table, function_table_init,
|
---|
3372 | FUNCTION_TABLE_ENTRIES, sizeof (struct function_table_entry));
|
---|
3373 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
3374 | {
|
---|
3375 | unsigned i;
|
---|
3376 | for (i = 0; i < FUNCTION_TABLE_ENTRIES; i++)
|
---|
3377 | assert (function_table_init[i].len <= MAX_FUNCTION_LENGTH);
|
---|
3378 | }
|
---|
3379 | #endif
|
---|
3380 | }
|
---|