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 | #if defined (CONFIG_WITH_MATH) || defined (CONFIG_WITH_NANOTS) || defined (CONFIG_WITH_FILE_SIZE) /* bird */
|
---|
46 | # include <ctype.h>
|
---|
47 | # ifdef _MSC_VER
|
---|
48 | typedef __int64 math_int;
|
---|
49 | # else
|
---|
50 | # include <stdint.h>
|
---|
51 | typedef int64_t math_int;
|
---|
52 | # endif
|
---|
53 | static char *math_int_to_variable_buffer (char *, math_int);
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | #ifdef CONFIG_WITH_NANOTS /* bird */
|
---|
57 | # ifdef WINDOWS32
|
---|
58 | # include <Windows.h>
|
---|
59 | # endif
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #ifdef __OS2__
|
---|
63 | # define CONFIG_WITH_OS2_LIBPATH 1
|
---|
64 | #endif
|
---|
65 | #ifdef CONFIG_WITH_OS2_LIBPATH
|
---|
66 | # define INCL_BASE
|
---|
67 | # define INCL_ERRROS
|
---|
68 | # include <os2.h>
|
---|
69 |
|
---|
70 | # define QHINF_EXEINFO 1 /* NE exeinfo. */
|
---|
71 | # define QHINF_READRSRCTBL 2 /* Reads from the resource table. */
|
---|
72 | # define QHINF_READFILE 3 /* Reads from the executable file. */
|
---|
73 | # define QHINF_LIBPATHLENGTH 4 /* Gets the libpath length. */
|
---|
74 | # define QHINF_LIBPATH 5 /* Gets the entire libpath. */
|
---|
75 | # define QHINF_FIXENTRY 6 /* NE only */
|
---|
76 | # define QHINF_STE 7 /* NE only */
|
---|
77 | # define QHINF_MAPSEL 8 /* NE only */
|
---|
78 | extern APIRET APIENTRY DosQueryHeaderInfo(HMODULE hmod, ULONG ulIndex, PVOID pvBuffer, ULONG cbBuffer, ULONG ulSubFunction);
|
---|
79 | #endif /* CONFIG_WITH_OS2_LIBPATH */
|
---|
80 |
|
---|
81 |
|
---|
82 | struct function_table_entry
|
---|
83 | {
|
---|
84 | const char *name;
|
---|
85 | unsigned char len;
|
---|
86 | unsigned char minimum_args;
|
---|
87 | unsigned char maximum_args;
|
---|
88 | char expand_args;
|
---|
89 | char *(*func_ptr) (char *output, char **argv, const char *fname);
|
---|
90 | };
|
---|
91 |
|
---|
92 | static unsigned long
|
---|
93 | function_table_entry_hash_1 (const void *keyv)
|
---|
94 | {
|
---|
95 | const struct function_table_entry *key = keyv;
|
---|
96 | return_STRING_N_HASH_1 (key->name, key->len);
|
---|
97 | }
|
---|
98 |
|
---|
99 | static unsigned long
|
---|
100 | function_table_entry_hash_2 (const void *keyv)
|
---|
101 | {
|
---|
102 | const struct function_table_entry *key = keyv;
|
---|
103 | return_STRING_N_HASH_2 (key->name, key->len);
|
---|
104 | }
|
---|
105 |
|
---|
106 | static int
|
---|
107 | function_table_entry_hash_cmp (const void *xv, const void *yv)
|
---|
108 | {
|
---|
109 | const struct function_table_entry *x = xv;
|
---|
110 | const struct function_table_entry *y = yv;
|
---|
111 | int result = x->len - y->len;
|
---|
112 | if (result)
|
---|
113 | return result;
|
---|
114 | return_STRING_N_COMPARE (x->name, y->name, x->len);
|
---|
115 | }
|
---|
116 |
|
---|
117 | static struct hash_table function_table;
|
---|
118 |
|
---|
119 | #ifdef CONFIG_WITH_MAKE_STATS
|
---|
120 | unsigned long make_stats_allocations = 0;
|
---|
121 | unsigned long make_stats_allocated = 0;
|
---|
122 | unsigned long make_stats_allocated_sum = 0;
|
---|
123 | unsigned long make_stats_ht_lookups = 0;
|
---|
124 | unsigned long make_stats_ht_collisions = 0;
|
---|
125 | #endif
|
---|
126 | |
---|
127 |
|
---|
128 |
|
---|
129 | /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
|
---|
130 | each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
|
---|
131 | the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
|
---|
132 | nonzero, substitutions are done only on matches which are complete
|
---|
133 | whitespace-delimited words. */
|
---|
134 |
|
---|
135 | char *
|
---|
136 | subst_expand (char *o, const char *text, const char *subst, const char *replace,
|
---|
137 | unsigned int slen, unsigned int rlen, int by_word)
|
---|
138 | {
|
---|
139 | const char *t = text;
|
---|
140 | const char *p;
|
---|
141 |
|
---|
142 | if (slen == 0 && !by_word)
|
---|
143 | {
|
---|
144 | /* The first occurrence of "" in any string is its end. */
|
---|
145 | o = variable_buffer_output (o, t, strlen (t));
|
---|
146 | if (rlen > 0)
|
---|
147 | o = variable_buffer_output (o, replace, rlen);
|
---|
148 | return o;
|
---|
149 | }
|
---|
150 |
|
---|
151 | do
|
---|
152 | {
|
---|
153 | if (by_word && slen == 0)
|
---|
154 | /* When matching by words, the empty string should match
|
---|
155 | the end of each word, rather than the end of the whole text. */
|
---|
156 | p = end_of_token (next_token (t));
|
---|
157 | else
|
---|
158 | {
|
---|
159 | p = strstr (t, subst);
|
---|
160 | if (p == 0)
|
---|
161 | {
|
---|
162 | /* No more matches. Output everything left on the end. */
|
---|
163 | o = variable_buffer_output (o, t, strlen (t));
|
---|
164 | return o;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | /* Output everything before this occurrence of the string to replace. */
|
---|
169 | if (p > t)
|
---|
170 | o = variable_buffer_output (o, t, p - t);
|
---|
171 |
|
---|
172 | /* If we're substituting only by fully matched words,
|
---|
173 | or only at the ends of words, check that this case qualifies. */
|
---|
174 | if (by_word
|
---|
175 | && ((p > text && !isblank ((unsigned char)p[-1]))
|
---|
176 | || (p[slen] != '\0' && !isblank ((unsigned char)p[slen]))))
|
---|
177 | /* Struck out. Output the rest of the string that is
|
---|
178 | no longer to be replaced. */
|
---|
179 | o = variable_buffer_output (o, subst, slen);
|
---|
180 | else if (rlen > 0)
|
---|
181 | /* Output the replacement string. */
|
---|
182 | o = variable_buffer_output (o, replace, rlen);
|
---|
183 |
|
---|
184 | /* Advance T past the string to be replaced. */
|
---|
185 | t = p + slen;
|
---|
186 | } while (*t != '\0');
|
---|
187 |
|
---|
188 | return o;
|
---|
189 | }
|
---|
190 | |
---|
191 |
|
---|
192 |
|
---|
193 | /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
|
---|
194 | and replacing strings matching PATTERN with REPLACE.
|
---|
195 | If PATTERN_PERCENT is not nil, PATTERN has already been
|
---|
196 | run through find_percent, and PATTERN_PERCENT is the result.
|
---|
197 | If REPLACE_PERCENT is not nil, REPLACE has already been
|
---|
198 | run through find_percent, and REPLACE_PERCENT is the result.
|
---|
199 | Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
|
---|
200 | character _AFTER_ the %, not to the % itself.
|
---|
201 | */
|
---|
202 |
|
---|
203 | char *
|
---|
204 | patsubst_expand_pat (char *o, const char *text,
|
---|
205 | const char *pattern, const char *replace,
|
---|
206 | const char *pattern_percent, const char *replace_percent)
|
---|
207 | {
|
---|
208 | unsigned int pattern_prepercent_len, pattern_postpercent_len;
|
---|
209 | unsigned int replace_prepercent_len, replace_postpercent_len;
|
---|
210 | const char *t;
|
---|
211 | unsigned int len;
|
---|
212 | int doneany = 0;
|
---|
213 |
|
---|
214 | /* Record the length of REPLACE before and after the % so we don't have to
|
---|
215 | compute these lengths more than once. */
|
---|
216 | if (replace_percent)
|
---|
217 | {
|
---|
218 | replace_prepercent_len = replace_percent - replace - 1;
|
---|
219 | replace_postpercent_len = strlen (replace_percent);
|
---|
220 | }
|
---|
221 | else
|
---|
222 | {
|
---|
223 | replace_prepercent_len = strlen (replace);
|
---|
224 | replace_postpercent_len = 0;
|
---|
225 | }
|
---|
226 |
|
---|
227 | if (!pattern_percent)
|
---|
228 | /* With no % in the pattern, this is just a simple substitution. */
|
---|
229 | return subst_expand (o, text, pattern, replace,
|
---|
230 | strlen (pattern), strlen (replace), 1);
|
---|
231 |
|
---|
232 | /* Record the length of PATTERN before and after the %
|
---|
233 | so we don't have to compute it more than once. */
|
---|
234 | pattern_prepercent_len = pattern_percent - pattern - 1;
|
---|
235 | pattern_postpercent_len = strlen (pattern_percent);
|
---|
236 |
|
---|
237 | while ((t = find_next_token (&text, &len)) != 0)
|
---|
238 | {
|
---|
239 | int fail = 0;
|
---|
240 |
|
---|
241 | /* Is it big enough to match? */
|
---|
242 | if (len < pattern_prepercent_len + pattern_postpercent_len)
|
---|
243 | fail = 1;
|
---|
244 |
|
---|
245 | /* Does the prefix match? */
|
---|
246 | if (!fail && pattern_prepercent_len > 0
|
---|
247 | && (*t != *pattern
|
---|
248 | || t[pattern_prepercent_len - 1] != pattern_percent[-2]
|
---|
249 | || !strneq (t + 1, pattern + 1, pattern_prepercent_len - 1)))
|
---|
250 | fail = 1;
|
---|
251 |
|
---|
252 | /* Does the suffix match? */
|
---|
253 | if (!fail && pattern_postpercent_len > 0
|
---|
254 | && (t[len - 1] != pattern_percent[pattern_postpercent_len - 1]
|
---|
255 | || t[len - pattern_postpercent_len] != *pattern_percent
|
---|
256 | || !strneq (&t[len - pattern_postpercent_len],
|
---|
257 | pattern_percent, pattern_postpercent_len - 1)))
|
---|
258 | fail = 1;
|
---|
259 |
|
---|
260 | if (fail)
|
---|
261 | /* It didn't match. Output the string. */
|
---|
262 | o = variable_buffer_output (o, t, len);
|
---|
263 | else
|
---|
264 | {
|
---|
265 | /* It matched. Output the replacement. */
|
---|
266 |
|
---|
267 | /* Output the part of the replacement before the %. */
|
---|
268 | o = variable_buffer_output (o, replace, replace_prepercent_len);
|
---|
269 |
|
---|
270 | if (replace_percent != 0)
|
---|
271 | {
|
---|
272 | /* Output the part of the matched string that
|
---|
273 | matched the % in the pattern. */
|
---|
274 | o = variable_buffer_output (o, t + pattern_prepercent_len,
|
---|
275 | len - (pattern_prepercent_len
|
---|
276 | + pattern_postpercent_len));
|
---|
277 | /* Output the part of the replacement after the %. */
|
---|
278 | o = variable_buffer_output (o, replace_percent,
|
---|
279 | replace_postpercent_len);
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | /* Output a space, but not if the replacement is "". */
|
---|
284 | if (fail || replace_prepercent_len > 0
|
---|
285 | || (replace_percent != 0 && len + replace_postpercent_len > 0))
|
---|
286 | {
|
---|
287 | o = variable_buffer_output (o, " ", 1);
|
---|
288 | doneany = 1;
|
---|
289 | }
|
---|
290 | }
|
---|
291 | if (doneany)
|
---|
292 | /* Kill the last space. */
|
---|
293 | --o;
|
---|
294 |
|
---|
295 | return o;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
|
---|
299 | and replacing strings matching PATTERN with REPLACE.
|
---|
300 | If PATTERN_PERCENT is not nil, PATTERN has already been
|
---|
301 | run through find_percent, and PATTERN_PERCENT is the result.
|
---|
302 | If REPLACE_PERCENT is not nil, REPLACE has already been
|
---|
303 | run through find_percent, and REPLACE_PERCENT is the result.
|
---|
304 | Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
|
---|
305 | character _AFTER_ the %, not to the % itself.
|
---|
306 | */
|
---|
307 |
|
---|
308 | char *
|
---|
309 | patsubst_expand (char *o, const char *text, char *pattern, char *replace)
|
---|
310 | {
|
---|
311 | const char *pattern_percent = find_percent (pattern);
|
---|
312 | const char *replace_percent = find_percent (replace);
|
---|
313 |
|
---|
314 | /* If there's a percent in the pattern or replacement skip it. */
|
---|
315 | if (replace_percent)
|
---|
316 | ++replace_percent;
|
---|
317 | if (pattern_percent)
|
---|
318 | ++pattern_percent;
|
---|
319 |
|
---|
320 | return patsubst_expand_pat (o, text, pattern, replace,
|
---|
321 | pattern_percent, replace_percent);
|
---|
322 | }
|
---|
323 | |
---|
324 |
|
---|
325 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
326 | /* The maximum length of a function, once reached there is
|
---|
327 | it can't be function and we can skip the hash lookup drop out. */
|
---|
328 |
|
---|
329 | # ifdef KMK
|
---|
330 | # define MAX_FUNCTION_LENGTH 12
|
---|
331 | # else
|
---|
332 | # define MAX_FUNCTION_LENGTH 10
|
---|
333 | # endif
|
---|
334 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
335 |
|
---|
336 | /* Look up a function by name. */
|
---|
337 |
|
---|
338 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
339 | __inline
|
---|
340 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
341 | static const struct function_table_entry *
|
---|
342 | lookup_function (const char *s)
|
---|
343 | {
|
---|
344 | const char *e = s;
|
---|
345 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
346 | int left = MAX_FUNCTION_LENGTH;
|
---|
347 | int ch;
|
---|
348 | while (((ch = *e) >= 'a' && ch <='z') || ch == '-')
|
---|
349 | {
|
---|
350 | if (!left--)
|
---|
351 | return 0;
|
---|
352 | e++;
|
---|
353 | }
|
---|
354 | #else
|
---|
355 | while (*e && ( (*e >= 'a' && *e <= 'z') || *e == '-'))
|
---|
356 | e++;
|
---|
357 | #endif
|
---|
358 | if (*e == '\0' || isblank ((unsigned char) *e))
|
---|
359 | {
|
---|
360 | struct function_table_entry function_table_entry_key;
|
---|
361 | function_table_entry_key.name = s;
|
---|
362 | function_table_entry_key.len = e - s;
|
---|
363 |
|
---|
364 | return hash_find_item (&function_table, &function_table_entry_key);
|
---|
365 | }
|
---|
366 | return 0;
|
---|
367 | }
|
---|
368 | |
---|
369 |
|
---|
370 |
|
---|
371 | /* Return 1 if PATTERN matches STR, 0 if not. */
|
---|
372 |
|
---|
373 | int
|
---|
374 | pattern_matches (const char *pattern, const char *percent, const char *str)
|
---|
375 | {
|
---|
376 | unsigned int sfxlen, strlength;
|
---|
377 |
|
---|
378 | if (percent == 0)
|
---|
379 | {
|
---|
380 | unsigned int len = strlen (pattern) + 1;
|
---|
381 | char *new_chars = alloca (len);
|
---|
382 | memcpy (new_chars, pattern, len);
|
---|
383 | percent = find_percent (new_chars);
|
---|
384 | if (percent == 0)
|
---|
385 | return streq (new_chars, str);
|
---|
386 | pattern = new_chars;
|
---|
387 | }
|
---|
388 |
|
---|
389 | sfxlen = strlen (percent + 1);
|
---|
390 | strlength = strlen (str);
|
---|
391 |
|
---|
392 | if (strlength < (percent - pattern) + sfxlen
|
---|
393 | || !strneq (pattern, str, percent - pattern))
|
---|
394 | return 0;
|
---|
395 |
|
---|
396 | return !strcmp (percent + 1, str + (strlength - sfxlen));
|
---|
397 | }
|
---|
398 | |
---|
399 |
|
---|
400 |
|
---|
401 | /* Find the next comma or ENDPAREN (counting nested STARTPAREN and
|
---|
402 | ENDPARENtheses), starting at PTR before END. Return a pointer to
|
---|
403 | next character.
|
---|
404 |
|
---|
405 | If no next argument is found, return NULL.
|
---|
406 | */
|
---|
407 |
|
---|
408 | static char *
|
---|
409 | find_next_argument (char startparen, char endparen,
|
---|
410 | const char *ptr, const char *end)
|
---|
411 | {
|
---|
412 | int count = 0;
|
---|
413 |
|
---|
414 | for (; ptr < end; ++ptr)
|
---|
415 | if (*ptr == startparen)
|
---|
416 | ++count;
|
---|
417 |
|
---|
418 | else if (*ptr == endparen)
|
---|
419 | {
|
---|
420 | --count;
|
---|
421 | if (count < 0)
|
---|
422 | return NULL;
|
---|
423 | }
|
---|
424 |
|
---|
425 | else if (*ptr == ',' && !count)
|
---|
426 | return (char *)ptr;
|
---|
427 |
|
---|
428 | /* We didn't find anything. */
|
---|
429 | return NULL;
|
---|
430 | }
|
---|
431 | |
---|
432 |
|
---|
433 |
|
---|
434 | /* Glob-expand LINE. The returned pointer is
|
---|
435 | only good until the next call to string_glob. */
|
---|
436 |
|
---|
437 | static char *
|
---|
438 | string_glob (char *line)
|
---|
439 | {
|
---|
440 | static char *result = 0;
|
---|
441 | static unsigned int length;
|
---|
442 | struct nameseq *chain;
|
---|
443 | unsigned int idx;
|
---|
444 |
|
---|
445 | chain = multi_glob (parse_file_seq
|
---|
446 | (&line, '\0', sizeof (struct nameseq),
|
---|
447 | /* We do not want parse_file_seq to strip `./'s.
|
---|
448 | That would break examples like:
|
---|
449 | $(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
|
---|
450 | 0),
|
---|
451 | sizeof (struct nameseq));
|
---|
452 |
|
---|
453 | if (result == 0)
|
---|
454 | {
|
---|
455 | length = 100;
|
---|
456 | result = xmalloc (100);
|
---|
457 | }
|
---|
458 |
|
---|
459 | idx = 0;
|
---|
460 | while (chain != 0)
|
---|
461 | {
|
---|
462 | const char *name = chain->name;
|
---|
463 | unsigned int len = strlen (name);
|
---|
464 |
|
---|
465 | struct nameseq *next = chain->next;
|
---|
466 | free (chain);
|
---|
467 | chain = next;
|
---|
468 |
|
---|
469 | /* multi_glob will pass names without globbing metacharacters
|
---|
470 | through as is, but we want only files that actually exist. */
|
---|
471 | if (file_exists_p (name))
|
---|
472 | {
|
---|
473 | if (idx + len + 1 > length)
|
---|
474 | {
|
---|
475 | length += (len + 1) * 2;
|
---|
476 | result = xrealloc (result, length);
|
---|
477 | }
|
---|
478 | memcpy (&result[idx], name, len);
|
---|
479 | idx += len;
|
---|
480 | result[idx++] = ' ';
|
---|
481 | }
|
---|
482 | }
|
---|
483 |
|
---|
484 | /* Kill the last space and terminate the string. */
|
---|
485 | if (idx == 0)
|
---|
486 | result[0] = '\0';
|
---|
487 | else
|
---|
488 | result[idx - 1] = '\0';
|
---|
489 |
|
---|
490 | return result;
|
---|
491 | }
|
---|
492 | |
---|
493 |
|
---|
494 | /*
|
---|
495 | Builtin functions
|
---|
496 | */
|
---|
497 |
|
---|
498 | static char *
|
---|
499 | func_patsubst (char *o, char **argv, const char *funcname UNUSED)
|
---|
500 | {
|
---|
501 | o = patsubst_expand (o, argv[2], argv[0], argv[1]);
|
---|
502 | return o;
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 | static char *
|
---|
507 | func_join (char *o, char **argv, const char *funcname UNUSED)
|
---|
508 | {
|
---|
509 | int doneany = 0;
|
---|
510 |
|
---|
511 | /* Write each word of the first argument directly followed
|
---|
512 | by the corresponding word of the second argument.
|
---|
513 | If the two arguments have a different number of words,
|
---|
514 | the excess words are just output separated by blanks. */
|
---|
515 | const char *tp;
|
---|
516 | const char *pp;
|
---|
517 | const char *list1_iterator = argv[0];
|
---|
518 | const char *list2_iterator = argv[1];
|
---|
519 | do
|
---|
520 | {
|
---|
521 | unsigned int len1, len2;
|
---|
522 |
|
---|
523 | tp = find_next_token (&list1_iterator, &len1);
|
---|
524 | if (tp != 0)
|
---|
525 | o = variable_buffer_output (o, tp, len1);
|
---|
526 |
|
---|
527 | pp = find_next_token (&list2_iterator, &len2);
|
---|
528 | if (pp != 0)
|
---|
529 | o = variable_buffer_output (o, pp, len2);
|
---|
530 |
|
---|
531 | if (tp != 0 || pp != 0)
|
---|
532 | {
|
---|
533 | o = variable_buffer_output (o, " ", 1);
|
---|
534 | doneany = 1;
|
---|
535 | }
|
---|
536 | }
|
---|
537 | while (tp != 0 || pp != 0);
|
---|
538 | if (doneany)
|
---|
539 | /* Kill the last blank. */
|
---|
540 | --o;
|
---|
541 |
|
---|
542 | return o;
|
---|
543 | }
|
---|
544 |
|
---|
545 |
|
---|
546 | static char *
|
---|
547 | func_origin (char *o, char **argv, const char *funcname UNUSED)
|
---|
548 | {
|
---|
549 | /* Expand the argument. */
|
---|
550 | struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
---|
551 | if (v == 0)
|
---|
552 | o = variable_buffer_output (o, "undefined", 9);
|
---|
553 | else
|
---|
554 | switch (v->origin)
|
---|
555 | {
|
---|
556 | default:
|
---|
557 | case o_invalid:
|
---|
558 | abort ();
|
---|
559 | break;
|
---|
560 | case o_default:
|
---|
561 | o = variable_buffer_output (o, "default", 7);
|
---|
562 | break;
|
---|
563 | case o_env:
|
---|
564 | o = variable_buffer_output (o, "environment", 11);
|
---|
565 | break;
|
---|
566 | case o_file:
|
---|
567 | o = variable_buffer_output (o, "file", 4);
|
---|
568 | break;
|
---|
569 | case o_env_override:
|
---|
570 | o = variable_buffer_output (o, "environment override", 20);
|
---|
571 | break;
|
---|
572 | case o_command:
|
---|
573 | o = variable_buffer_output (o, "command line", 12);
|
---|
574 | break;
|
---|
575 | case o_override:
|
---|
576 | o = variable_buffer_output (o, "override", 8);
|
---|
577 | break;
|
---|
578 | case o_automatic:
|
---|
579 | o = variable_buffer_output (o, "automatic", 9);
|
---|
580 | break;
|
---|
581 | #ifdef CONFIG_WITH_LOCAL_VARIABLES
|
---|
582 | case o_local:
|
---|
583 | o = variable_buffer_output (o, "local", 5);
|
---|
584 | break;
|
---|
585 | #endif
|
---|
586 | }
|
---|
587 |
|
---|
588 | return o;
|
---|
589 | }
|
---|
590 |
|
---|
591 | static char *
|
---|
592 | func_flavor (char *o, char **argv, const char *funcname UNUSED)
|
---|
593 | {
|
---|
594 | struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
---|
595 |
|
---|
596 | if (v == 0)
|
---|
597 | o = variable_buffer_output (o, "undefined", 9);
|
---|
598 | else
|
---|
599 | if (v->recursive)
|
---|
600 | o = variable_buffer_output (o, "recursive", 9);
|
---|
601 | else
|
---|
602 | o = variable_buffer_output (o, "simple", 6);
|
---|
603 |
|
---|
604 | return o;
|
---|
605 | }
|
---|
606 |
|
---|
607 | #ifdef VMS
|
---|
608 | # define IS_PATHSEP(c) ((c) == ']')
|
---|
609 | #else
|
---|
610 | # ifdef HAVE_DOS_PATHS
|
---|
611 | # define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
|
---|
612 | # else
|
---|
613 | # define IS_PATHSEP(c) ((c) == '/')
|
---|
614 | # endif
|
---|
615 | #endif
|
---|
616 |
|
---|
617 |
|
---|
618 | static char *
|
---|
619 | func_notdir_suffix (char *o, char **argv, const char *funcname)
|
---|
620 | {
|
---|
621 | /* Expand the argument. */
|
---|
622 | const char *list_iterator = argv[0];
|
---|
623 | const char *p2;
|
---|
624 | int doneany =0;
|
---|
625 | unsigned int len=0;
|
---|
626 |
|
---|
627 | int is_suffix = streq (funcname, "suffix");
|
---|
628 | int is_notdir = !is_suffix;
|
---|
629 | while ((p2 = find_next_token (&list_iterator, &len)) != 0)
|
---|
630 | {
|
---|
631 | const char *p = p2 + len;
|
---|
632 |
|
---|
633 |
|
---|
634 | while (p >= p2 && (!is_suffix || *p != '.'))
|
---|
635 | {
|
---|
636 | if (IS_PATHSEP (*p))
|
---|
637 | break;
|
---|
638 | --p;
|
---|
639 | }
|
---|
640 |
|
---|
641 | if (p >= p2)
|
---|
642 | {
|
---|
643 | if (is_notdir)
|
---|
644 | ++p;
|
---|
645 | else if (*p != '.')
|
---|
646 | continue;
|
---|
647 | o = variable_buffer_output (o, p, len - (p - p2));
|
---|
648 | }
|
---|
649 | #ifdef HAVE_DOS_PATHS
|
---|
650 | /* Handle the case of "d:foo/bar". */
|
---|
651 | else if (streq (funcname, "notdir") && p2[0] && p2[1] == ':')
|
---|
652 | {
|
---|
653 | p = p2 + 2;
|
---|
654 | o = variable_buffer_output (o, p, len - (p - p2));
|
---|
655 | }
|
---|
656 | #endif
|
---|
657 | else if (is_notdir)
|
---|
658 | o = variable_buffer_output (o, p2, len);
|
---|
659 |
|
---|
660 | if (is_notdir || p >= p2)
|
---|
661 | {
|
---|
662 | o = variable_buffer_output (o, " ", 1);
|
---|
663 | doneany = 1;
|
---|
664 | }
|
---|
665 | }
|
---|
666 |
|
---|
667 | if (doneany)
|
---|
668 | /* Kill last space. */
|
---|
669 | --o;
|
---|
670 |
|
---|
671 | return o;
|
---|
672 | }
|
---|
673 |
|
---|
674 |
|
---|
675 | static char *
|
---|
676 | func_basename_dir (char *o, char **argv, const char *funcname)
|
---|
677 | {
|
---|
678 | /* Expand the argument. */
|
---|
679 | const char *p3 = argv[0];
|
---|
680 | const char *p2;
|
---|
681 | int doneany=0;
|
---|
682 | unsigned int len=0;
|
---|
683 |
|
---|
684 | int is_basename= streq (funcname, "basename");
|
---|
685 | int is_dir= !is_basename;
|
---|
686 |
|
---|
687 | while ((p2 = find_next_token (&p3, &len)) != 0)
|
---|
688 | {
|
---|
689 | const char *p = p2 + len;
|
---|
690 | while (p >= p2 && (!is_basename || *p != '.'))
|
---|
691 | {
|
---|
692 | if (IS_PATHSEP (*p))
|
---|
693 | break;
|
---|
694 | --p;
|
---|
695 | }
|
---|
696 |
|
---|
697 | if (p >= p2 && (is_dir))
|
---|
698 | o = variable_buffer_output (o, p2, ++p - p2);
|
---|
699 | else if (p >= p2 && (*p == '.'))
|
---|
700 | o = variable_buffer_output (o, p2, p - p2);
|
---|
701 | #ifdef HAVE_DOS_PATHS
|
---|
702 | /* Handle the "d:foobar" case */
|
---|
703 | else if (p2[0] && p2[1] == ':' && is_dir)
|
---|
704 | o = variable_buffer_output (o, p2, 2);
|
---|
705 | #endif
|
---|
706 | else if (is_dir)
|
---|
707 | #ifdef VMS
|
---|
708 | o = variable_buffer_output (o, "[]", 2);
|
---|
709 | #else
|
---|
710 | #ifndef _AMIGA
|
---|
711 | o = variable_buffer_output (o, "./", 2);
|
---|
712 | #else
|
---|
713 | ; /* Just a nop... */
|
---|
714 | #endif /* AMIGA */
|
---|
715 | #endif /* !VMS */
|
---|
716 | else
|
---|
717 | /* The entire name is the basename. */
|
---|
718 | o = variable_buffer_output (o, p2, len);
|
---|
719 |
|
---|
720 | o = variable_buffer_output (o, " ", 1);
|
---|
721 | doneany = 1;
|
---|
722 | }
|
---|
723 |
|
---|
724 | if (doneany)
|
---|
725 | /* Kill last space. */
|
---|
726 | --o;
|
---|
727 |
|
---|
728 | return o;
|
---|
729 | }
|
---|
730 |
|
---|
731 | static char *
|
---|
732 | func_addsuffix_addprefix (char *o, char **argv, const char *funcname)
|
---|
733 | {
|
---|
734 | int fixlen = strlen (argv[0]);
|
---|
735 | const char *list_iterator = argv[1];
|
---|
736 | int is_addprefix = streq (funcname, "addprefix");
|
---|
737 | int is_addsuffix = !is_addprefix;
|
---|
738 |
|
---|
739 | int doneany = 0;
|
---|
740 | const char *p;
|
---|
741 | unsigned int len;
|
---|
742 |
|
---|
743 | while ((p = find_next_token (&list_iterator, &len)) != 0)
|
---|
744 | {
|
---|
745 | if (is_addprefix)
|
---|
746 | o = variable_buffer_output (o, argv[0], fixlen);
|
---|
747 | o = variable_buffer_output (o, p, len);
|
---|
748 | if (is_addsuffix)
|
---|
749 | o = variable_buffer_output (o, argv[0], fixlen);
|
---|
750 | o = variable_buffer_output (o, " ", 1);
|
---|
751 | doneany = 1;
|
---|
752 | }
|
---|
753 |
|
---|
754 | if (doneany)
|
---|
755 | /* Kill last space. */
|
---|
756 | --o;
|
---|
757 |
|
---|
758 | return o;
|
---|
759 | }
|
---|
760 |
|
---|
761 | static char *
|
---|
762 | func_subst (char *o, char **argv, const char *funcname UNUSED)
|
---|
763 | {
|
---|
764 | o = subst_expand (o, argv[2], argv[0], argv[1], strlen (argv[0]),
|
---|
765 | strlen (argv[1]), 0);
|
---|
766 |
|
---|
767 | return o;
|
---|
768 | }
|
---|
769 |
|
---|
770 |
|
---|
771 | static char *
|
---|
772 | func_firstword (char *o, char **argv, const char *funcname UNUSED)
|
---|
773 | {
|
---|
774 | unsigned int i;
|
---|
775 | const char *words = argv[0]; /* Use a temp variable for find_next_token */
|
---|
776 | const char *p = find_next_token (&words, &i);
|
---|
777 |
|
---|
778 | if (p != 0)
|
---|
779 | o = variable_buffer_output (o, p, i);
|
---|
780 |
|
---|
781 | return o;
|
---|
782 | }
|
---|
783 |
|
---|
784 | static char *
|
---|
785 | func_lastword (char *o, char **argv, const char *funcname UNUSED)
|
---|
786 | {
|
---|
787 | unsigned int i;
|
---|
788 | const char *words = argv[0]; /* Use a temp variable for find_next_token */
|
---|
789 | const char *p = NULL;
|
---|
790 | const char *t;
|
---|
791 |
|
---|
792 | while ((t = find_next_token (&words, &i)))
|
---|
793 | p = t;
|
---|
794 |
|
---|
795 | if (p != 0)
|
---|
796 | o = variable_buffer_output (o, p, i);
|
---|
797 |
|
---|
798 | return o;
|
---|
799 | }
|
---|
800 |
|
---|
801 | static char *
|
---|
802 | func_words (char *o, char **argv, const char *funcname UNUSED)
|
---|
803 | {
|
---|
804 | int i = 0;
|
---|
805 | const char *word_iterator = argv[0];
|
---|
806 | char buf[20];
|
---|
807 |
|
---|
808 | while (find_next_token (&word_iterator, (unsigned int *) 0) != 0)
|
---|
809 | ++i;
|
---|
810 |
|
---|
811 | sprintf (buf, "%d", i);
|
---|
812 | o = variable_buffer_output (o, buf, strlen (buf));
|
---|
813 |
|
---|
814 | return o;
|
---|
815 | }
|
---|
816 |
|
---|
817 | /* Set begpp to point to the first non-whitespace character of the string,
|
---|
818 | * and endpp to point to the last non-whitespace character of the string.
|
---|
819 | * If the string is empty or contains nothing but whitespace, endpp will be
|
---|
820 | * begpp-1.
|
---|
821 | */
|
---|
822 | char *
|
---|
823 | strip_whitespace (const char **begpp, const char **endpp)
|
---|
824 | {
|
---|
825 | while (*begpp <= *endpp && isspace ((unsigned char)**begpp))
|
---|
826 | (*begpp) ++;
|
---|
827 | while (*endpp >= *begpp && isspace ((unsigned char)**endpp))
|
---|
828 | (*endpp) --;
|
---|
829 | return (char *)*begpp;
|
---|
830 | }
|
---|
831 |
|
---|
832 | static void
|
---|
833 | check_numeric (const char *s, const char *msg)
|
---|
834 | {
|
---|
835 | const char *end = s + strlen (s) - 1;
|
---|
836 | const char *beg = s;
|
---|
837 | strip_whitespace (&s, &end);
|
---|
838 |
|
---|
839 | for (; s <= end; ++s)
|
---|
840 | if (!ISDIGIT (*s)) /* ISDIGIT only evals its arg once: see make.h. */
|
---|
841 | break;
|
---|
842 |
|
---|
843 | if (s <= end || end - beg < 0)
|
---|
844 | fatal (*expanding_var, "%s: '%s'", msg, beg);
|
---|
845 | }
|
---|
846 |
|
---|
847 |
|
---|
848 |
|
---|
849 | static char *
|
---|
850 | func_word (char *o, char **argv, const char *funcname UNUSED)
|
---|
851 | {
|
---|
852 | const char *end_p;
|
---|
853 | const char *p;
|
---|
854 | int i;
|
---|
855 |
|
---|
856 | /* Check the first argument. */
|
---|
857 | check_numeric (argv[0], _("non-numeric first argument to `word' function"));
|
---|
858 | i = atoi (argv[0]);
|
---|
859 |
|
---|
860 | if (i == 0)
|
---|
861 | fatal (*expanding_var,
|
---|
862 | _("first argument to `word' function must be greater than 0"));
|
---|
863 |
|
---|
864 | end_p = argv[1];
|
---|
865 | while ((p = find_next_token (&end_p, 0)) != 0)
|
---|
866 | if (--i == 0)
|
---|
867 | break;
|
---|
868 |
|
---|
869 | if (i == 0)
|
---|
870 | o = variable_buffer_output (o, p, end_p - p);
|
---|
871 |
|
---|
872 | return o;
|
---|
873 | }
|
---|
874 |
|
---|
875 | static char *
|
---|
876 | func_wordlist (char *o, char **argv, const char *funcname UNUSED)
|
---|
877 | {
|
---|
878 | int start, count;
|
---|
879 |
|
---|
880 | /* Check the arguments. */
|
---|
881 | check_numeric (argv[0],
|
---|
882 | _("non-numeric first argument to `wordlist' function"));
|
---|
883 | check_numeric (argv[1],
|
---|
884 | _("non-numeric second argument to `wordlist' function"));
|
---|
885 |
|
---|
886 | start = atoi (argv[0]);
|
---|
887 | if (start < 1)
|
---|
888 | fatal (*expanding_var,
|
---|
889 | "invalid first argument to `wordlist' function: `%d'", start);
|
---|
890 |
|
---|
891 | count = atoi (argv[1]) - start + 1;
|
---|
892 |
|
---|
893 | if (count > 0)
|
---|
894 | {
|
---|
895 | const char *p;
|
---|
896 | const char *end_p = argv[2];
|
---|
897 |
|
---|
898 | /* Find the beginning of the "start"th word. */
|
---|
899 | while (((p = find_next_token (&end_p, 0)) != 0) && --start)
|
---|
900 | ;
|
---|
901 |
|
---|
902 | if (p)
|
---|
903 | {
|
---|
904 | /* Find the end of the "count"th word from start. */
|
---|
905 | while (--count && (find_next_token (&end_p, 0) != 0))
|
---|
906 | ;
|
---|
907 |
|
---|
908 | /* Return the stuff in the middle. */
|
---|
909 | o = variable_buffer_output (o, p, end_p - p);
|
---|
910 | }
|
---|
911 | }
|
---|
912 |
|
---|
913 | return o;
|
---|
914 | }
|
---|
915 |
|
---|
916 | static char *
|
---|
917 | func_findstring (char *o, char **argv, const char *funcname UNUSED)
|
---|
918 | {
|
---|
919 | /* Find the first occurrence of the first string in the second. */
|
---|
920 | if (strstr (argv[1], argv[0]) != 0)
|
---|
921 | o = variable_buffer_output (o, argv[0], strlen (argv[0]));
|
---|
922 |
|
---|
923 | return o;
|
---|
924 | }
|
---|
925 |
|
---|
926 | static char *
|
---|
927 | func_foreach (char *o, char **argv, const char *funcname UNUSED)
|
---|
928 | {
|
---|
929 | /* expand only the first two. */
|
---|
930 | char *varname = expand_argument (argv[0], NULL);
|
---|
931 | char *list = expand_argument (argv[1], NULL);
|
---|
932 | const char *body = argv[2];
|
---|
933 |
|
---|
934 | int doneany = 0;
|
---|
935 | const char *list_iterator = list;
|
---|
936 | const char *p;
|
---|
937 | unsigned int len;
|
---|
938 | struct variable *var;
|
---|
939 |
|
---|
940 | push_new_variable_scope ();
|
---|
941 | var = define_variable (varname, strlen (varname), "", o_automatic, 0);
|
---|
942 |
|
---|
943 | /* loop through LIST, put the value in VAR and expand BODY */
|
---|
944 | while ((p = find_next_token (&list_iterator, &len)) != 0)
|
---|
945 | {
|
---|
946 | char *result = 0;
|
---|
947 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
948 | if (len >= (unsigned int)var->value_alloc_len)
|
---|
949 | {
|
---|
950 | free (var->value);
|
---|
951 | var->value_alloc_len = (len + 32) & ~31;
|
---|
952 | var->value = xmalloc (var->value_alloc_len);
|
---|
953 | }
|
---|
954 | memcpy (var->value, p, len);
|
---|
955 | var->value[len] = '\0';
|
---|
956 | var->value_length = len;
|
---|
957 | #else
|
---|
958 | free (var->value);
|
---|
959 | var->value = savestring (p, len);
|
---|
960 | #endif
|
---|
961 |
|
---|
962 | result = allocated_variable_expand (body);
|
---|
963 |
|
---|
964 | o = variable_buffer_output (o, result, strlen (result));
|
---|
965 | o = variable_buffer_output (o, " ", 1);
|
---|
966 | doneany = 1;
|
---|
967 | free (result);
|
---|
968 | }
|
---|
969 |
|
---|
970 | if (doneany)
|
---|
971 | /* Kill the last space. */
|
---|
972 | --o;
|
---|
973 |
|
---|
974 | pop_variable_scope ();
|
---|
975 | free (varname);
|
---|
976 | free (list);
|
---|
977 |
|
---|
978 | return o;
|
---|
979 | }
|
---|
980 |
|
---|
981 | struct a_word
|
---|
982 | {
|
---|
983 | struct a_word *next;
|
---|
984 | struct a_word *chain;
|
---|
985 | char *str;
|
---|
986 | int length;
|
---|
987 | int matched;
|
---|
988 | };
|
---|
989 |
|
---|
990 | static unsigned long
|
---|
991 | a_word_hash_1 (const void *key)
|
---|
992 | {
|
---|
993 | return_STRING_HASH_1 (((struct a_word const *) key)->str);
|
---|
994 | }
|
---|
995 |
|
---|
996 | static unsigned long
|
---|
997 | a_word_hash_2 (const void *key)
|
---|
998 | {
|
---|
999 | return_STRING_HASH_2 (((struct a_word const *) key)->str);
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | static int
|
---|
1003 | a_word_hash_cmp (const void *x, const void *y)
|
---|
1004 | {
|
---|
1005 | int result = ((struct a_word const *) x)->length - ((struct a_word const *) y)->length;
|
---|
1006 | if (result)
|
---|
1007 | return result;
|
---|
1008 | return_STRING_COMPARE (((struct a_word const *) x)->str,
|
---|
1009 | ((struct a_word const *) y)->str);
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | struct a_pattern
|
---|
1013 | {
|
---|
1014 | struct a_pattern *next;
|
---|
1015 | char *str;
|
---|
1016 | char *percent;
|
---|
1017 | int length;
|
---|
1018 | int save_c;
|
---|
1019 | };
|
---|
1020 |
|
---|
1021 | static char *
|
---|
1022 | func_filter_filterout (char *o, char **argv, const char *funcname)
|
---|
1023 | {
|
---|
1024 | struct a_word *wordhead;
|
---|
1025 | struct a_word **wordtail;
|
---|
1026 | struct a_word *wp;
|
---|
1027 | struct a_pattern *pathead;
|
---|
1028 | struct a_pattern **pattail;
|
---|
1029 | struct a_pattern *pp;
|
---|
1030 |
|
---|
1031 | struct hash_table a_word_table;
|
---|
1032 | int is_filter = streq (funcname, "filter");
|
---|
1033 | const char *pat_iterator = argv[0];
|
---|
1034 | const char *word_iterator = argv[1];
|
---|
1035 | int literals = 0;
|
---|
1036 | int words = 0;
|
---|
1037 | int hashing = 0;
|
---|
1038 | char *p;
|
---|
1039 | unsigned int len;
|
---|
1040 |
|
---|
1041 | /* Chop ARGV[0] up into patterns to match against the words. */
|
---|
1042 |
|
---|
1043 | pattail = &pathead;
|
---|
1044 | while ((p = find_next_token (&pat_iterator, &len)) != 0)
|
---|
1045 | {
|
---|
1046 | struct a_pattern *pat = alloca (sizeof (struct a_pattern));
|
---|
1047 |
|
---|
1048 | *pattail = pat;
|
---|
1049 | pattail = &pat->next;
|
---|
1050 |
|
---|
1051 | if (*pat_iterator != '\0')
|
---|
1052 | ++pat_iterator;
|
---|
1053 |
|
---|
1054 | pat->str = p;
|
---|
1055 | pat->length = len;
|
---|
1056 | pat->save_c = p[len];
|
---|
1057 | p[len] = '\0';
|
---|
1058 | pat->percent = find_percent (p);
|
---|
1059 | if (pat->percent == 0)
|
---|
1060 | literals++;
|
---|
1061 | }
|
---|
1062 | *pattail = 0;
|
---|
1063 |
|
---|
1064 | /* Chop ARGV[1] up into words to match against the patterns. */
|
---|
1065 |
|
---|
1066 | wordtail = &wordhead;
|
---|
1067 | while ((p = find_next_token (&word_iterator, &len)) != 0)
|
---|
1068 | {
|
---|
1069 | struct a_word *word = alloca (sizeof (struct a_word));
|
---|
1070 |
|
---|
1071 | *wordtail = word;
|
---|
1072 | wordtail = &word->next;
|
---|
1073 |
|
---|
1074 | if (*word_iterator != '\0')
|
---|
1075 | ++word_iterator;
|
---|
1076 |
|
---|
1077 | p[len] = '\0';
|
---|
1078 | word->str = p;
|
---|
1079 | word->length = len;
|
---|
1080 | word->matched = 0;
|
---|
1081 | word->chain = 0;
|
---|
1082 | words++;
|
---|
1083 | }
|
---|
1084 | *wordtail = 0;
|
---|
1085 |
|
---|
1086 | /* Only use a hash table if arg list lengths justifies the cost. */
|
---|
1087 | hashing = (literals >= 2 && (literals * words) >= 10);
|
---|
1088 | if (hashing)
|
---|
1089 | {
|
---|
1090 | hash_init (&a_word_table, words, a_word_hash_1, a_word_hash_2,
|
---|
1091 | a_word_hash_cmp);
|
---|
1092 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
1093 | {
|
---|
1094 | struct a_word *owp = hash_insert (&a_word_table, wp);
|
---|
1095 | if (owp)
|
---|
1096 | wp->chain = owp;
|
---|
1097 | }
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | if (words)
|
---|
1101 | {
|
---|
1102 | int doneany = 0;
|
---|
1103 |
|
---|
1104 | /* Run each pattern through the words, killing words. */
|
---|
1105 | for (pp = pathead; pp != 0; pp = pp->next)
|
---|
1106 | {
|
---|
1107 | if (pp->percent)
|
---|
1108 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
1109 | wp->matched |= pattern_matches (pp->str, pp->percent, wp->str);
|
---|
1110 | else if (hashing)
|
---|
1111 | {
|
---|
1112 | struct a_word a_word_key;
|
---|
1113 | a_word_key.str = pp->str;
|
---|
1114 | a_word_key.length = pp->length;
|
---|
1115 | wp = hash_find_item (&a_word_table, &a_word_key);
|
---|
1116 | while (wp)
|
---|
1117 | {
|
---|
1118 | wp->matched |= 1;
|
---|
1119 | wp = wp->chain;
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 | else
|
---|
1123 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
1124 | wp->matched |= (wp->length == pp->length
|
---|
1125 | && strneq (pp->str, wp->str, wp->length));
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | /* Output the words that matched (or didn't, for filter-out). */
|
---|
1129 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
1130 | if (is_filter ? wp->matched : !wp->matched)
|
---|
1131 | {
|
---|
1132 | o = variable_buffer_output (o, wp->str, strlen (wp->str));
|
---|
1133 | o = variable_buffer_output (o, " ", 1);
|
---|
1134 | doneany = 1;
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | if (doneany)
|
---|
1138 | /* Kill the last space. */
|
---|
1139 | --o;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | for (pp = pathead; pp != 0; pp = pp->next)
|
---|
1143 | pp->str[pp->length] = pp->save_c;
|
---|
1144 |
|
---|
1145 | if (hashing)
|
---|
1146 | hash_free (&a_word_table, 0);
|
---|
1147 |
|
---|
1148 | return o;
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 |
|
---|
1152 | static char *
|
---|
1153 | func_strip (char *o, char **argv, const char *funcname UNUSED)
|
---|
1154 | {
|
---|
1155 | const char *p = argv[0];
|
---|
1156 | int doneany = 0;
|
---|
1157 |
|
---|
1158 | while (*p != '\0')
|
---|
1159 | {
|
---|
1160 | int i=0;
|
---|
1161 | const char *word_start;
|
---|
1162 |
|
---|
1163 | while (isspace ((unsigned char)*p))
|
---|
1164 | ++p;
|
---|
1165 | word_start = p;
|
---|
1166 | for (i=0; *p != '\0' && !isspace ((unsigned char)*p); ++p, ++i)
|
---|
1167 | {}
|
---|
1168 | if (!i)
|
---|
1169 | break;
|
---|
1170 | o = variable_buffer_output (o, word_start, i);
|
---|
1171 | o = variable_buffer_output (o, " ", 1);
|
---|
1172 | doneany = 1;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | if (doneany)
|
---|
1176 | /* Kill the last space. */
|
---|
1177 | --o;
|
---|
1178 |
|
---|
1179 | return o;
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | /*
|
---|
1183 | Print a warning or fatal message.
|
---|
1184 | */
|
---|
1185 | static char *
|
---|
1186 | func_error (char *o, char **argv, const char *funcname)
|
---|
1187 | {
|
---|
1188 | char **argvp;
|
---|
1189 | char *msg, *p;
|
---|
1190 | int len;
|
---|
1191 |
|
---|
1192 | /* The arguments will be broken on commas. Rather than create yet
|
---|
1193 | another special case where function arguments aren't broken up,
|
---|
1194 | just create a format string that puts them back together. */
|
---|
1195 | for (len=0, argvp=argv; *argvp != 0; ++argvp)
|
---|
1196 | len += strlen (*argvp) + 2;
|
---|
1197 |
|
---|
1198 | p = msg = alloca (len + 1);
|
---|
1199 |
|
---|
1200 | for (argvp=argv; argvp[1] != 0; ++argvp)
|
---|
1201 | {
|
---|
1202 | strcpy (p, *argvp);
|
---|
1203 | p += strlen (*argvp);
|
---|
1204 | *(p++) = ',';
|
---|
1205 | *(p++) = ' ';
|
---|
1206 | }
|
---|
1207 | strcpy (p, *argvp);
|
---|
1208 |
|
---|
1209 | switch (*funcname) {
|
---|
1210 | case 'e':
|
---|
1211 | fatal (reading_file, "%s", msg);
|
---|
1212 |
|
---|
1213 | case 'w':
|
---|
1214 | error (reading_file, "%s", msg);
|
---|
1215 | break;
|
---|
1216 |
|
---|
1217 | case 'i':
|
---|
1218 | printf ("%s\n", msg);
|
---|
1219 | fflush(stdout);
|
---|
1220 | break;
|
---|
1221 |
|
---|
1222 | default:
|
---|
1223 | fatal (*expanding_var, "Internal error: func_error: '%s'", funcname);
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | /* The warning function expands to the empty string. */
|
---|
1227 | return o;
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 |
|
---|
1231 | /*
|
---|
1232 | chop argv[0] into words, and sort them.
|
---|
1233 | */
|
---|
1234 | static char *
|
---|
1235 | func_sort (char *o, char **argv, const char *funcname UNUSED)
|
---|
1236 | {
|
---|
1237 | const char *t;
|
---|
1238 | char **words;
|
---|
1239 | int wordi;
|
---|
1240 | char *p;
|
---|
1241 | unsigned int len;
|
---|
1242 | int i;
|
---|
1243 |
|
---|
1244 | /* Find the maximum number of words we'll have. */
|
---|
1245 | t = argv[0];
|
---|
1246 | wordi = 1;
|
---|
1247 | while (*t != '\0')
|
---|
1248 | {
|
---|
1249 | char c = *(t++);
|
---|
1250 |
|
---|
1251 | if (! isspace ((unsigned char)c))
|
---|
1252 | continue;
|
---|
1253 |
|
---|
1254 | ++wordi;
|
---|
1255 |
|
---|
1256 | while (isspace ((unsigned char)*t))
|
---|
1257 | ++t;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | words = xmalloc (wordi * sizeof (char *));
|
---|
1261 |
|
---|
1262 | /* Now assign pointers to each string in the array. */
|
---|
1263 | t = argv[0];
|
---|
1264 | wordi = 0;
|
---|
1265 | while ((p = find_next_token (&t, &len)) != 0)
|
---|
1266 | {
|
---|
1267 | ++t;
|
---|
1268 | p[len] = '\0';
|
---|
1269 | words[wordi++] = p;
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | if (wordi)
|
---|
1273 | {
|
---|
1274 | /* Now sort the list of words. */
|
---|
1275 | qsort (words, wordi, sizeof (char *), alpha_compare);
|
---|
1276 |
|
---|
1277 | /* Now write the sorted list, uniquified. */
|
---|
1278 | #ifdef CONFIG_WITH_RSORT
|
---|
1279 | if (strcmp (funcname, "rsort"))
|
---|
1280 | {
|
---|
1281 | /* sort */
|
---|
1282 | #endif
|
---|
1283 | for (i = 0; i < wordi; ++i)
|
---|
1284 | {
|
---|
1285 | len = strlen (words[i]);
|
---|
1286 | if (i == wordi - 1 || strlen (words[i + 1]) != len
|
---|
1287 | || strcmp (words[i], words[i + 1]))
|
---|
1288 | {
|
---|
1289 | o = variable_buffer_output (o, words[i], len);
|
---|
1290 | o = variable_buffer_output (o, " ", 1);
|
---|
1291 | }
|
---|
1292 | }
|
---|
1293 | #ifdef CONFIG_WITH_RSORT
|
---|
1294 | }
|
---|
1295 | else
|
---|
1296 | {
|
---|
1297 | /* rsort - reverse the result */
|
---|
1298 | i = wordi;
|
---|
1299 | while (i-- > 0)
|
---|
1300 | {
|
---|
1301 | len = strlen (words[i]);
|
---|
1302 | if (i == 0 || strlen (words[i - 1]) != len
|
---|
1303 | || strcmp (words[i], words[i - 1]))
|
---|
1304 | {
|
---|
1305 | o = variable_buffer_output (o, words[i], len);
|
---|
1306 | o = variable_buffer_output (o, " ", 1);
|
---|
1307 | }
|
---|
1308 | }
|
---|
1309 | }
|
---|
1310 | #endif
|
---|
1311 |
|
---|
1312 | /* Kill the last space. */
|
---|
1313 | --o;
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | free (words);
|
---|
1317 |
|
---|
1318 | return o;
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | /*
|
---|
1322 | $(if condition,true-part[,false-part])
|
---|
1323 |
|
---|
1324 | CONDITION is false iff it evaluates to an empty string. White
|
---|
1325 | space before and after condition are stripped before evaluation.
|
---|
1326 |
|
---|
1327 | If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is
|
---|
1328 | evaluated (if it exists). Because only one of the two PARTs is evaluated,
|
---|
1329 | you can use $(if ...) to create side-effects (with $(shell ...), for
|
---|
1330 | example).
|
---|
1331 | */
|
---|
1332 |
|
---|
1333 | static char *
|
---|
1334 | func_if (char *o, char **argv, const char *funcname UNUSED)
|
---|
1335 | {
|
---|
1336 | const char *begp = argv[0];
|
---|
1337 | const char *endp = begp + strlen (argv[0]) - 1;
|
---|
1338 | int result = 0;
|
---|
1339 |
|
---|
1340 | /* Find the result of the condition: if we have a value, and it's not
|
---|
1341 | empty, the condition is true. If we don't have a value, or it's the
|
---|
1342 | empty string, then it's false. */
|
---|
1343 |
|
---|
1344 | strip_whitespace (&begp, &endp);
|
---|
1345 |
|
---|
1346 | if (begp <= endp)
|
---|
1347 | {
|
---|
1348 | char *expansion = expand_argument (begp, endp+1);
|
---|
1349 |
|
---|
1350 | result = strlen (expansion);
|
---|
1351 | free (expansion);
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | /* If the result is true (1) we want to eval the first argument, and if
|
---|
1355 | it's false (0) we want to eval the second. If the argument doesn't
|
---|
1356 | exist we do nothing, otherwise expand it and add to the buffer. */
|
---|
1357 |
|
---|
1358 | argv += 1 + !result;
|
---|
1359 |
|
---|
1360 | if (*argv)
|
---|
1361 | {
|
---|
1362 | char *expansion = expand_argument (*argv, NULL);
|
---|
1363 |
|
---|
1364 | o = variable_buffer_output (o, expansion, strlen (expansion));
|
---|
1365 |
|
---|
1366 | free (expansion);
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | return o;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | /*
|
---|
1373 | $(or condition1[,condition2[,condition3[...]]])
|
---|
1374 |
|
---|
1375 | A CONDITION is false iff it evaluates to an empty string. White
|
---|
1376 | space before and after CONDITION are stripped before evaluation.
|
---|
1377 |
|
---|
1378 | CONDITION1 is evaluated. If it's true, then this is the result of
|
---|
1379 | expansion. If it's false, CONDITION2 is evaluated, and so on. If none of
|
---|
1380 | the conditions are true, the expansion is the empty string.
|
---|
1381 |
|
---|
1382 | Once a CONDITION is true no further conditions are evaluated
|
---|
1383 | (short-circuiting).
|
---|
1384 | */
|
---|
1385 |
|
---|
1386 | static char *
|
---|
1387 | func_or (char *o, char **argv, const char *funcname UNUSED)
|
---|
1388 | {
|
---|
1389 | for ( ; *argv ; ++argv)
|
---|
1390 | {
|
---|
1391 | const char *begp = *argv;
|
---|
1392 | const char *endp = begp + strlen (*argv) - 1;
|
---|
1393 | char *expansion;
|
---|
1394 | int result = 0;
|
---|
1395 |
|
---|
1396 | /* Find the result of the condition: if it's false keep going. */
|
---|
1397 |
|
---|
1398 | strip_whitespace (&begp, &endp);
|
---|
1399 |
|
---|
1400 | if (begp > endp)
|
---|
1401 | continue;
|
---|
1402 |
|
---|
1403 | expansion = expand_argument (begp, endp+1);
|
---|
1404 | result = strlen (expansion);
|
---|
1405 |
|
---|
1406 | /* If the result is false keep going. */
|
---|
1407 | if (!result)
|
---|
1408 | {
|
---|
1409 | free (expansion);
|
---|
1410 | continue;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | /* It's true! Keep this result and return. */
|
---|
1414 | o = variable_buffer_output (o, expansion, result);
|
---|
1415 | free (expansion);
|
---|
1416 | break;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | return o;
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | /*
|
---|
1423 | $(and condition1[,condition2[,condition3[...]]])
|
---|
1424 |
|
---|
1425 | A CONDITION is false iff it evaluates to an empty string. White
|
---|
1426 | space before and after CONDITION are stripped before evaluation.
|
---|
1427 |
|
---|
1428 | CONDITION1 is evaluated. If it's false, then this is the result of
|
---|
1429 | expansion. If it's true, CONDITION2 is evaluated, and so on. If all of
|
---|
1430 | the conditions are true, the expansion is the result of the last condition.
|
---|
1431 |
|
---|
1432 | Once a CONDITION is false no further conditions are evaluated
|
---|
1433 | (short-circuiting).
|
---|
1434 | */
|
---|
1435 |
|
---|
1436 | static char *
|
---|
1437 | func_and (char *o, char **argv, const char *funcname UNUSED)
|
---|
1438 | {
|
---|
1439 | char *expansion;
|
---|
1440 | int result;
|
---|
1441 |
|
---|
1442 | while (1)
|
---|
1443 | {
|
---|
1444 | const char *begp = *argv;
|
---|
1445 | const char *endp = begp + strlen (*argv) - 1;
|
---|
1446 |
|
---|
1447 | /* An empty condition is always false. */
|
---|
1448 | strip_whitespace (&begp, &endp);
|
---|
1449 | if (begp > endp)
|
---|
1450 | return o;
|
---|
1451 |
|
---|
1452 | expansion = expand_argument (begp, endp+1);
|
---|
1453 | result = strlen (expansion);
|
---|
1454 |
|
---|
1455 | /* If the result is false, stop here: we're done. */
|
---|
1456 | if (!result)
|
---|
1457 | break;
|
---|
1458 |
|
---|
1459 | /* Otherwise the result is true. If this is the last one, keep this
|
---|
1460 | result and quit. Otherwise go on to the next one! */
|
---|
1461 |
|
---|
1462 | if (*(++argv))
|
---|
1463 | free (expansion);
|
---|
1464 | else
|
---|
1465 | {
|
---|
1466 | o = variable_buffer_output (o, expansion, result);
|
---|
1467 | break;
|
---|
1468 | }
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | free (expansion);
|
---|
1472 |
|
---|
1473 | return o;
|
---|
1474 | }
|
---|
1475 |
|
---|
1476 | static char *
|
---|
1477 | func_wildcard (char *o, char **argv, const char *funcname UNUSED)
|
---|
1478 | {
|
---|
1479 | #ifdef _AMIGA
|
---|
1480 | o = wildcard_expansion (argv[0], o);
|
---|
1481 | #else
|
---|
1482 | char *p = string_glob (argv[0]);
|
---|
1483 | o = variable_buffer_output (o, p, strlen (p));
|
---|
1484 | #endif
|
---|
1485 | return o;
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | /*
|
---|
1489 | $(eval <makefile string>)
|
---|
1490 |
|
---|
1491 | Always resolves to the empty string.
|
---|
1492 |
|
---|
1493 | Treat the arguments as a segment of makefile, and parse them.
|
---|
1494 | */
|
---|
1495 |
|
---|
1496 | static char *
|
---|
1497 | func_eval (char *o, char **argv, const char *funcname UNUSED)
|
---|
1498 | {
|
---|
1499 | char *buf;
|
---|
1500 | unsigned int len;
|
---|
1501 |
|
---|
1502 | /* Eval the buffer. Pop the current variable buffer setting so that the
|
---|
1503 | eval'd code can use its own without conflicting. */
|
---|
1504 |
|
---|
1505 | install_variable_buffer (&buf, &len);
|
---|
1506 |
|
---|
1507 | eval_buffer (argv[0]);
|
---|
1508 |
|
---|
1509 | restore_variable_buffer (buf, len);
|
---|
1510 |
|
---|
1511 | return o;
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 |
|
---|
1515 | #ifdef CONFIG_WITH_EVALPLUS
|
---|
1516 | /* Same as func_eval except that we push and pop the local variable
|
---|
1517 | context before evaluating the buffer. */
|
---|
1518 | static char *
|
---|
1519 | func_evalctx (char *o, char **argv, const char *funcname UNUSED)
|
---|
1520 | {
|
---|
1521 | char *buf;
|
---|
1522 | unsigned int len;
|
---|
1523 |
|
---|
1524 | /* Eval the buffer. Pop the current variable buffer setting so that the
|
---|
1525 | eval'd code can use its own without conflicting. */
|
---|
1526 |
|
---|
1527 | install_variable_buffer (&buf, &len);
|
---|
1528 |
|
---|
1529 | push_new_variable_scope ();
|
---|
1530 |
|
---|
1531 | eval_buffer (argv[0]);
|
---|
1532 |
|
---|
1533 | pop_variable_scope ();
|
---|
1534 |
|
---|
1535 | restore_variable_buffer (buf, len);
|
---|
1536 |
|
---|
1537 | return o;
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 | /* A mix of func_eval and func_value, saves memory for the expansion.
|
---|
1541 | This implements both evalval and evalvalctx, the latter has its own
|
---|
1542 | variable context just like evalctx. */
|
---|
1543 | static char *
|
---|
1544 | func_evalval (char *o, char **argv, const char *funcname)
|
---|
1545 | {
|
---|
1546 | /* Look up the variable. */
|
---|
1547 | struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
---|
1548 | if (v)
|
---|
1549 | {
|
---|
1550 | char *buf;
|
---|
1551 | unsigned int len;
|
---|
1552 | int var_ctx;
|
---|
1553 | size_t off;
|
---|
1554 | const struct floc *reading_file_saved = reading_file;
|
---|
1555 |
|
---|
1556 | /* Make a copy of the value to the variable buffer since
|
---|
1557 | eval_buffer will make changes to its input. */
|
---|
1558 |
|
---|
1559 | off = o - variable_buffer;
|
---|
1560 | o = variable_buffer_output (o, v->value, v->value_length + 1);
|
---|
1561 | o = variable_buffer + off;
|
---|
1562 | assert (!o[v->value_length]);
|
---|
1563 |
|
---|
1564 | /* Eval the value. Pop the current variable buffer setting so that the
|
---|
1565 | eval'd code can use its own without conflicting. (really necessary?) */
|
---|
1566 |
|
---|
1567 | install_variable_buffer (&buf, &len);
|
---|
1568 | var_ctx = !strcmp (funcname, "evalvalctx");
|
---|
1569 | if (var_ctx)
|
---|
1570 | push_new_variable_scope ();
|
---|
1571 | if (v->fileinfo.filenm)
|
---|
1572 | reading_file = &v->fileinfo;
|
---|
1573 |
|
---|
1574 | eval_buffer (o);
|
---|
1575 |
|
---|
1576 | reading_file = reading_file_saved;
|
---|
1577 | if (var_ctx)
|
---|
1578 | pop_variable_scope ();
|
---|
1579 | restore_variable_buffer (buf, len);
|
---|
1580 | }
|
---|
1581 |
|
---|
1582 | return o;
|
---|
1583 | }
|
---|
1584 | #endif /* CONFIG_WITH_EVALPLUS */
|
---|
1585 |
|
---|
1586 | static char *
|
---|
1587 | func_value (char *o, char **argv, const char *funcname UNUSED)
|
---|
1588 | {
|
---|
1589 | /* Look up the variable. */
|
---|
1590 | struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
---|
1591 |
|
---|
1592 | /* Copy its value into the output buffer without expanding it. */
|
---|
1593 | if (v)
|
---|
1594 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1595 | o = variable_buffer_output (o, v->value,
|
---|
1596 | v->value_length >= 0 ? v->value_length : strlen(v->value));
|
---|
1597 | #else
|
---|
1598 | o = variable_buffer_output (o, v->value, strlen(v->value));
|
---|
1599 | #endif
|
---|
1600 |
|
---|
1601 | return o;
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | /*
|
---|
1605 | \r is replaced on UNIX as well. Is this desirable?
|
---|
1606 | */
|
---|
1607 | static void
|
---|
1608 | fold_newlines (char *buffer, unsigned int *length)
|
---|
1609 | {
|
---|
1610 | char *dst = buffer;
|
---|
1611 | char *src = buffer;
|
---|
1612 | char *last_nonnl = buffer -1;
|
---|
1613 | src[*length] = 0;
|
---|
1614 | for (; *src != '\0'; ++src)
|
---|
1615 | {
|
---|
1616 | if (src[0] == '\r' && src[1] == '\n')
|
---|
1617 | continue;
|
---|
1618 | if (*src == '\n')
|
---|
1619 | {
|
---|
1620 | *dst++ = ' ';
|
---|
1621 | }
|
---|
1622 | else
|
---|
1623 | {
|
---|
1624 | last_nonnl = dst;
|
---|
1625 | *dst++ = *src;
|
---|
1626 | }
|
---|
1627 | }
|
---|
1628 | *(++last_nonnl) = '\0';
|
---|
1629 | *length = last_nonnl - buffer;
|
---|
1630 | }
|
---|
1631 |
|
---|
1632 |
|
---|
1633 |
|
---|
1634 | int shell_function_pid = 0, shell_function_completed;
|
---|
1635 |
|
---|
1636 |
|
---|
1637 | #ifdef WINDOWS32
|
---|
1638 | /*untested*/
|
---|
1639 |
|
---|
1640 | #include <windows.h>
|
---|
1641 | #include <io.h>
|
---|
1642 | #include "sub_proc.h"
|
---|
1643 |
|
---|
1644 |
|
---|
1645 | void
|
---|
1646 | windows32_openpipe (int *pipedes, int *pid_p, char **command_argv, char **envp)
|
---|
1647 | {
|
---|
1648 | SECURITY_ATTRIBUTES saAttr;
|
---|
1649 | HANDLE hIn;
|
---|
1650 | HANDLE hErr;
|
---|
1651 | HANDLE hChildOutRd;
|
---|
1652 | HANDLE hChildOutWr;
|
---|
1653 | HANDLE hProcess;
|
---|
1654 |
|
---|
1655 |
|
---|
1656 | saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
|
---|
1657 | saAttr.bInheritHandle = TRUE;
|
---|
1658 | saAttr.lpSecurityDescriptor = NULL;
|
---|
1659 |
|
---|
1660 | if (DuplicateHandle (GetCurrentProcess(),
|
---|
1661 | GetStdHandle(STD_INPUT_HANDLE),
|
---|
1662 | GetCurrentProcess(),
|
---|
1663 | &hIn,
|
---|
1664 | 0,
|
---|
1665 | TRUE,
|
---|
1666 | DUPLICATE_SAME_ACCESS) == FALSE) {
|
---|
1667 | fatal (NILF, _("create_child_process: DuplicateHandle(In) failed (e=%ld)\n"),
|
---|
1668 | GetLastError());
|
---|
1669 |
|
---|
1670 | }
|
---|
1671 | if (DuplicateHandle(GetCurrentProcess(),
|
---|
1672 | GetStdHandle(STD_ERROR_HANDLE),
|
---|
1673 | GetCurrentProcess(),
|
---|
1674 | &hErr,
|
---|
1675 | 0,
|
---|
1676 | TRUE,
|
---|
1677 | DUPLICATE_SAME_ACCESS) == FALSE) {
|
---|
1678 | fatal (NILF, _("create_child_process: DuplicateHandle(Err) failed (e=%ld)\n"),
|
---|
1679 | GetLastError());
|
---|
1680 | }
|
---|
1681 |
|
---|
1682 | if (!CreatePipe(&hChildOutRd, &hChildOutWr, &saAttr, 0))
|
---|
1683 | fatal (NILF, _("CreatePipe() failed (e=%ld)\n"), GetLastError());
|
---|
1684 |
|
---|
1685 | hProcess = process_init_fd(hIn, hChildOutWr, hErr);
|
---|
1686 |
|
---|
1687 | if (!hProcess)
|
---|
1688 | fatal (NILF, _("windows32_openpipe (): process_init_fd() failed\n"));
|
---|
1689 |
|
---|
1690 | /* make sure that CreateProcess() has Path it needs */
|
---|
1691 | sync_Path_environment();
|
---|
1692 |
|
---|
1693 | if (!process_begin(hProcess, command_argv, envp, command_argv[0], NULL)) {
|
---|
1694 | /* register process for wait */
|
---|
1695 | process_register(hProcess);
|
---|
1696 |
|
---|
1697 | /* set the pid for returning to caller */
|
---|
1698 | *pid_p = (int) hProcess;
|
---|
1699 |
|
---|
1700 | /* set up to read data from child */
|
---|
1701 | pipedes[0] = _open_osfhandle((long) hChildOutRd, O_RDONLY);
|
---|
1702 |
|
---|
1703 | /* this will be closed almost right away */
|
---|
1704 | pipedes[1] = _open_osfhandle((long) hChildOutWr, O_APPEND);
|
---|
1705 | } else {
|
---|
1706 | /* reap/cleanup the failed process */
|
---|
1707 | process_cleanup(hProcess);
|
---|
1708 |
|
---|
1709 | /* close handles which were duplicated, they weren't used */
|
---|
1710 | CloseHandle(hIn);
|
---|
1711 | CloseHandle(hErr);
|
---|
1712 |
|
---|
1713 | /* close pipe handles, they won't be used */
|
---|
1714 | CloseHandle(hChildOutRd);
|
---|
1715 | CloseHandle(hChildOutWr);
|
---|
1716 |
|
---|
1717 | /* set status for return */
|
---|
1718 | pipedes[0] = pipedes[1] = -1;
|
---|
1719 | *pid_p = -1;
|
---|
1720 | }
|
---|
1721 | }
|
---|
1722 | #endif
|
---|
1723 |
|
---|
1724 |
|
---|
1725 | #ifdef __MSDOS__
|
---|
1726 | FILE *
|
---|
1727 | msdos_openpipe (int* pipedes, int *pidp, char *text)
|
---|
1728 | {
|
---|
1729 | FILE *fpipe=0;
|
---|
1730 | /* MSDOS can't fork, but it has `popen'. */
|
---|
1731 | struct variable *sh = lookup_variable ("SHELL", 5);
|
---|
1732 | int e;
|
---|
1733 | extern int dos_command_running, dos_status;
|
---|
1734 |
|
---|
1735 | /* Make sure not to bother processing an empty line. */
|
---|
1736 | while (isblank ((unsigned char)*text))
|
---|
1737 | ++text;
|
---|
1738 | if (*text == '\0')
|
---|
1739 | return 0;
|
---|
1740 |
|
---|
1741 | if (sh)
|
---|
1742 | {
|
---|
1743 | char buf[PATH_MAX + 7];
|
---|
1744 | /* This makes sure $SHELL value is used by $(shell), even
|
---|
1745 | though the target environment is not passed to it. */
|
---|
1746 | sprintf (buf, "SHELL=%s", sh->value);
|
---|
1747 | putenv (buf);
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | e = errno;
|
---|
1751 | errno = 0;
|
---|
1752 | dos_command_running = 1;
|
---|
1753 | dos_status = 0;
|
---|
1754 | /* If dos_status becomes non-zero, it means the child process
|
---|
1755 | was interrupted by a signal, like SIGINT or SIGQUIT. See
|
---|
1756 | fatal_error_signal in commands.c. */
|
---|
1757 | fpipe = popen (text, "rt");
|
---|
1758 | dos_command_running = 0;
|
---|
1759 | if (!fpipe || dos_status)
|
---|
1760 | {
|
---|
1761 | pipedes[0] = -1;
|
---|
1762 | *pidp = -1;
|
---|
1763 | if (dos_status)
|
---|
1764 | errno = EINTR;
|
---|
1765 | else if (errno == 0)
|
---|
1766 | errno = ENOMEM;
|
---|
1767 | shell_function_completed = -1;
|
---|
1768 | }
|
---|
1769 | else
|
---|
1770 | {
|
---|
1771 | pipedes[0] = fileno (fpipe);
|
---|
1772 | *pidp = 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
|
---|
1773 | errno = e;
|
---|
1774 | shell_function_completed = 1;
|
---|
1775 | }
|
---|
1776 | return fpipe;
|
---|
1777 | }
|
---|
1778 | #endif
|
---|
1779 |
|
---|
1780 | /*
|
---|
1781 | Do shell spawning, with the naughty bits for different OSes.
|
---|
1782 | */
|
---|
1783 |
|
---|
1784 | #ifdef VMS
|
---|
1785 |
|
---|
1786 | /* VMS can't do $(shell ...) */
|
---|
1787 | #define func_shell 0
|
---|
1788 |
|
---|
1789 | #else
|
---|
1790 | #ifndef _AMIGA
|
---|
1791 | static char *
|
---|
1792 | func_shell (char *o, char **argv, const char *funcname UNUSED)
|
---|
1793 | {
|
---|
1794 | char *batch_filename = NULL;
|
---|
1795 |
|
---|
1796 | #ifdef __MSDOS__
|
---|
1797 | FILE *fpipe;
|
---|
1798 | #endif
|
---|
1799 | char **command_argv;
|
---|
1800 | const char *error_prefix;
|
---|
1801 | char **envp;
|
---|
1802 | int pipedes[2];
|
---|
1803 | int pid;
|
---|
1804 |
|
---|
1805 | #ifndef __MSDOS__
|
---|
1806 | /* Construct the argument list. */
|
---|
1807 | command_argv = construct_command_argv (argv[0], NULL, NULL, &batch_filename);
|
---|
1808 | if (command_argv == 0)
|
---|
1809 | return o;
|
---|
1810 | #endif
|
---|
1811 |
|
---|
1812 | /* Using a target environment for `shell' loses in cases like:
|
---|
1813 | export var = $(shell echo foobie)
|
---|
1814 | because target_environment hits a loop trying to expand $(var)
|
---|
1815 | to put it in the environment. This is even more confusing when
|
---|
1816 | var was not explicitly exported, but just appeared in the
|
---|
1817 | calling environment.
|
---|
1818 |
|
---|
1819 | See Savannah bug #10593.
|
---|
1820 |
|
---|
1821 | envp = target_environment (NILF);
|
---|
1822 | */
|
---|
1823 |
|
---|
1824 | envp = environ;
|
---|
1825 |
|
---|
1826 | /* For error messages. */
|
---|
1827 | if (reading_file && reading_file->filenm)
|
---|
1828 | {
|
---|
1829 | char *p = alloca (strlen (reading_file->filenm)+11+4);
|
---|
1830 | sprintf (p, "%s:%lu: ", reading_file->filenm, reading_file->lineno);
|
---|
1831 | error_prefix = p;
|
---|
1832 | }
|
---|
1833 | else
|
---|
1834 | error_prefix = "";
|
---|
1835 |
|
---|
1836 | #if defined(__MSDOS__)
|
---|
1837 | fpipe = msdos_openpipe (pipedes, &pid, argv[0]);
|
---|
1838 | if (pipedes[0] < 0)
|
---|
1839 | {
|
---|
1840 | perror_with_name (error_prefix, "pipe");
|
---|
1841 | return o;
|
---|
1842 | }
|
---|
1843 | #elif defined(WINDOWS32)
|
---|
1844 | windows32_openpipe (pipedes, &pid, command_argv, envp);
|
---|
1845 | if (pipedes[0] < 0)
|
---|
1846 | {
|
---|
1847 | /* open of the pipe failed, mark as failed execution */
|
---|
1848 | shell_function_completed = -1;
|
---|
1849 |
|
---|
1850 | return o;
|
---|
1851 | }
|
---|
1852 | else
|
---|
1853 | #else
|
---|
1854 | if (pipe (pipedes) < 0)
|
---|
1855 | {
|
---|
1856 | perror_with_name (error_prefix, "pipe");
|
---|
1857 | return o;
|
---|
1858 | }
|
---|
1859 |
|
---|
1860 | # ifdef __EMX__
|
---|
1861 | /* close some handles that are unnecessary for the child process */
|
---|
1862 | CLOSE_ON_EXEC(pipedes[1]);
|
---|
1863 | CLOSE_ON_EXEC(pipedes[0]);
|
---|
1864 | /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
|
---|
1865 | pid = child_execute_job (0, pipedes[1], command_argv, envp);
|
---|
1866 | if (pid < 0)
|
---|
1867 | perror_with_name (error_prefix, "spawn");
|
---|
1868 | # else /* ! __EMX__ */
|
---|
1869 | pid = vfork ();
|
---|
1870 | if (pid < 0)
|
---|
1871 | perror_with_name (error_prefix, "fork");
|
---|
1872 | else if (pid == 0)
|
---|
1873 | child_execute_job (0, pipedes[1], command_argv, envp);
|
---|
1874 | else
|
---|
1875 | # endif
|
---|
1876 | #endif
|
---|
1877 | {
|
---|
1878 | /* We are the parent. */
|
---|
1879 | char *buffer;
|
---|
1880 | unsigned int maxlen, i;
|
---|
1881 | int cc;
|
---|
1882 |
|
---|
1883 | /* Record the PID for reap_children. */
|
---|
1884 | shell_function_pid = pid;
|
---|
1885 | #ifndef __MSDOS__
|
---|
1886 | shell_function_completed = 0;
|
---|
1887 |
|
---|
1888 | /* Free the storage only the child needed. */
|
---|
1889 | free (command_argv[0]);
|
---|
1890 | free (command_argv);
|
---|
1891 |
|
---|
1892 | /* Close the write side of the pipe. */
|
---|
1893 | # ifdef _MSC_VER /* Avoid annoying msvcrt when debugging. (bird) */
|
---|
1894 | if (pipedes[1] != -1)
|
---|
1895 | # endif
|
---|
1896 | close (pipedes[1]);
|
---|
1897 | #endif
|
---|
1898 |
|
---|
1899 | /* Set up and read from the pipe. */
|
---|
1900 |
|
---|
1901 | maxlen = 200;
|
---|
1902 | buffer = xmalloc (maxlen + 1);
|
---|
1903 |
|
---|
1904 | /* Read from the pipe until it gets EOF. */
|
---|
1905 | for (i = 0; ; i += cc)
|
---|
1906 | {
|
---|
1907 | if (i == maxlen)
|
---|
1908 | {
|
---|
1909 | maxlen += 512;
|
---|
1910 | buffer = xrealloc (buffer, maxlen + 1);
|
---|
1911 | }
|
---|
1912 |
|
---|
1913 | EINTRLOOP (cc, read (pipedes[0], &buffer[i], maxlen - i));
|
---|
1914 | if (cc <= 0)
|
---|
1915 | break;
|
---|
1916 | }
|
---|
1917 | buffer[i] = '\0';
|
---|
1918 |
|
---|
1919 | /* Close the read side of the pipe. */
|
---|
1920 | #ifdef __MSDOS__
|
---|
1921 | if (fpipe)
|
---|
1922 | (void) pclose (fpipe);
|
---|
1923 | #else
|
---|
1924 | # ifdef _MSC_VER /* Avoid annoying msvcrt when debugging. (bird) */
|
---|
1925 | if (pipedes[0] != -1)
|
---|
1926 | # endif
|
---|
1927 | (void) close (pipedes[0]);
|
---|
1928 | #endif
|
---|
1929 |
|
---|
1930 | /* Loop until child_handler or reap_children() sets
|
---|
1931 | shell_function_completed to the status of our child shell. */
|
---|
1932 | while (shell_function_completed == 0)
|
---|
1933 | reap_children (1, 0);
|
---|
1934 |
|
---|
1935 | if (batch_filename) {
|
---|
1936 | DB (DB_VERBOSE, (_("Cleaning up temporary batch file %s\n"),
|
---|
1937 | batch_filename));
|
---|
1938 | remove (batch_filename);
|
---|
1939 | free (batch_filename);
|
---|
1940 | }
|
---|
1941 | shell_function_pid = 0;
|
---|
1942 |
|
---|
1943 | /* The child_handler function will set shell_function_completed
|
---|
1944 | to 1 when the child dies normally, or to -1 if it
|
---|
1945 | dies with status 127, which is most likely an exec fail. */
|
---|
1946 |
|
---|
1947 | if (shell_function_completed == -1)
|
---|
1948 | {
|
---|
1949 | /* This likely means that the execvp failed, so we should just
|
---|
1950 | write the error message in the pipe from the child. */
|
---|
1951 | fputs (buffer, stderr);
|
---|
1952 | fflush (stderr);
|
---|
1953 | }
|
---|
1954 | else
|
---|
1955 | {
|
---|
1956 | /* The child finished normally. Replace all newlines in its output
|
---|
1957 | with spaces, and put that in the variable output buffer. */
|
---|
1958 | fold_newlines (buffer, &i);
|
---|
1959 | o = variable_buffer_output (o, buffer, i);
|
---|
1960 | }
|
---|
1961 |
|
---|
1962 | free (buffer);
|
---|
1963 | }
|
---|
1964 |
|
---|
1965 | return o;
|
---|
1966 | }
|
---|
1967 |
|
---|
1968 | #else /* _AMIGA */
|
---|
1969 |
|
---|
1970 | /* Do the Amiga version of func_shell. */
|
---|
1971 |
|
---|
1972 | static char *
|
---|
1973 | func_shell (char *o, char **argv, const char *funcname)
|
---|
1974 | {
|
---|
1975 | /* Amiga can't fork nor spawn, but I can start a program with
|
---|
1976 | redirection of my choice. However, this means that we
|
---|
1977 | don't have an opportunity to reopen stdout to trap it. Thus,
|
---|
1978 | we save our own stdout onto a new descriptor and dup a temp
|
---|
1979 | file's descriptor onto our stdout temporarily. After we
|
---|
1980 | spawn the shell program, we dup our own stdout back to the
|
---|
1981 | stdout descriptor. The buffer reading is the same as above,
|
---|
1982 | except that we're now reading from a file. */
|
---|
1983 |
|
---|
1984 | #include <dos/dos.h>
|
---|
1985 | #include <proto/dos.h>
|
---|
1986 |
|
---|
1987 | BPTR child_stdout;
|
---|
1988 | char tmp_output[FILENAME_MAX];
|
---|
1989 | unsigned int maxlen = 200, i;
|
---|
1990 | int cc;
|
---|
1991 | char * buffer, * ptr;
|
---|
1992 | char ** aptr;
|
---|
1993 | int len = 0;
|
---|
1994 | char* batch_filename = NULL;
|
---|
1995 |
|
---|
1996 | /* Construct the argument list. */
|
---|
1997 | command_argv = construct_command_argv (argv[0], (char **) NULL,
|
---|
1998 | (struct file *) 0, &batch_filename);
|
---|
1999 | if (command_argv == 0)
|
---|
2000 | return o;
|
---|
2001 |
|
---|
2002 | /* Note the mktemp() is a security hole, but this only runs on Amiga.
|
---|
2003 | Ideally we would use main.c:open_tmpfile(), but this uses a special
|
---|
2004 | Open(), not fopen(), and I'm not familiar enough with the code to mess
|
---|
2005 | with it. */
|
---|
2006 | strcpy (tmp_output, "t:MakeshXXXXXXXX");
|
---|
2007 | mktemp (tmp_output);
|
---|
2008 | child_stdout = Open (tmp_output, MODE_NEWFILE);
|
---|
2009 |
|
---|
2010 | for (aptr=command_argv; *aptr; aptr++)
|
---|
2011 | len += strlen (*aptr) + 1;
|
---|
2012 |
|
---|
2013 | buffer = xmalloc (len + 1);
|
---|
2014 | ptr = buffer;
|
---|
2015 |
|
---|
2016 | for (aptr=command_argv; *aptr; aptr++)
|
---|
2017 | {
|
---|
2018 | strcpy (ptr, *aptr);
|
---|
2019 | ptr += strlen (ptr) + 1;
|
---|
2020 | *ptr ++ = ' ';
|
---|
2021 | *ptr = 0;
|
---|
2022 | }
|
---|
2023 |
|
---|
2024 | ptr[-1] = '\n';
|
---|
2025 |
|
---|
2026 | Execute (buffer, NULL, child_stdout);
|
---|
2027 | free (buffer);
|
---|
2028 |
|
---|
2029 | Close (child_stdout);
|
---|
2030 |
|
---|
2031 | child_stdout = Open (tmp_output, MODE_OLDFILE);
|
---|
2032 |
|
---|
2033 | buffer = xmalloc (maxlen);
|
---|
2034 | i = 0;
|
---|
2035 | do
|
---|
2036 | {
|
---|
2037 | if (i == maxlen)
|
---|
2038 | {
|
---|
2039 | maxlen += 512;
|
---|
2040 | buffer = xrealloc (buffer, maxlen + 1);
|
---|
2041 | }
|
---|
2042 |
|
---|
2043 | cc = Read (child_stdout, &buffer[i], maxlen - i);
|
---|
2044 | if (cc > 0)
|
---|
2045 | i += cc;
|
---|
2046 | } while (cc > 0);
|
---|
2047 |
|
---|
2048 | Close (child_stdout);
|
---|
2049 |
|
---|
2050 | fold_newlines (buffer, &i);
|
---|
2051 | o = variable_buffer_output (o, buffer, i);
|
---|
2052 | free (buffer);
|
---|
2053 | return o;
|
---|
2054 | }
|
---|
2055 | #endif /* _AMIGA */
|
---|
2056 | #endif /* !VMS */
|
---|
2057 |
|
---|
2058 | #ifdef EXPERIMENTAL
|
---|
2059 |
|
---|
2060 | /*
|
---|
2061 | equality. Return is string-boolean, ie, the empty string is false.
|
---|
2062 | */
|
---|
2063 | static char *
|
---|
2064 | func_eq (char *o, char **argv, const char *funcname)
|
---|
2065 | {
|
---|
2066 | int result = ! strcmp (argv[0], argv[1]);
|
---|
2067 | o = variable_buffer_output (o, result ? "1" : "", result);
|
---|
2068 | return o;
|
---|
2069 | }
|
---|
2070 |
|
---|
2071 |
|
---|
2072 | /*
|
---|
2073 | string-boolean not operator.
|
---|
2074 | */
|
---|
2075 | static char *
|
---|
2076 | func_not (char *o, char **argv, const char *funcname)
|
---|
2077 | {
|
---|
2078 | const char *s = argv[0];
|
---|
2079 | int result = 0;
|
---|
2080 | while (isspace ((unsigned char)*s))
|
---|
2081 | s++;
|
---|
2082 | result = ! (*s);
|
---|
2083 | o = variable_buffer_output (o, result ? "1" : "", result);
|
---|
2084 | return o;
|
---|
2085 | }
|
---|
2086 | #endif
|
---|
2087 | |
---|
2088 |
|
---|
2089 |
|
---|
2090 | /* Return the absolute name of file NAME which does not contain any `.',
|
---|
2091 | `..' components nor any repeated path separators ('/'). */
|
---|
2092 | #ifdef KMK
|
---|
2093 | char *
|
---|
2094 | #else
|
---|
2095 | static char *
|
---|
2096 | #endif
|
---|
2097 | abspath (const char *name, char *apath)
|
---|
2098 | {
|
---|
2099 | char *dest;
|
---|
2100 | const char *start, *end, *apath_limit;
|
---|
2101 |
|
---|
2102 | if (name[0] == '\0' || apath == NULL)
|
---|
2103 | return NULL;
|
---|
2104 |
|
---|
2105 | #ifdef WINDOWS32 /* bird */
|
---|
2106 | dest = w32ify((char *)name, 1);
|
---|
2107 | if (!dest)
|
---|
2108 | return NULL;
|
---|
2109 | {
|
---|
2110 | size_t len = strlen(dest);
|
---|
2111 | memcpy(apath, dest, len);
|
---|
2112 | dest = apath + len;
|
---|
2113 | }
|
---|
2114 |
|
---|
2115 | (void)end; (void)start; (void)apath_limit;
|
---|
2116 |
|
---|
2117 | #elif defined __OS2__ /* bird */
|
---|
2118 | if (_abspath(apath, name, GET_PATH_MAX))
|
---|
2119 | return NULL;
|
---|
2120 | dest = strchr(apath, '\0');
|
---|
2121 |
|
---|
2122 | (void)end; (void)start; (void)apath_limit; (void)dest;
|
---|
2123 |
|
---|
2124 | #else /* !WINDOWS32 && !__OS2__ */
|
---|
2125 | apath_limit = apath + GET_PATH_MAX;
|
---|
2126 |
|
---|
2127 | #ifdef HAVE_DOS_PATHS /* bird added this */
|
---|
2128 | if (isalpha(name[0]) && name[1] == ':')
|
---|
2129 | {
|
---|
2130 | /* drive spec */
|
---|
2131 | apath[0] = toupper(name[0]);
|
---|
2132 | apath[1] = ':';
|
---|
2133 | apath[2] = '/';
|
---|
2134 | name += 2;
|
---|
2135 | }
|
---|
2136 | else
|
---|
2137 | #endif /* HAVE_DOS_PATHS */
|
---|
2138 | if (name[0] != '/')
|
---|
2139 | {
|
---|
2140 | /* It is unlikely we would make it until here but just to make sure. */
|
---|
2141 | if (!starting_directory)
|
---|
2142 | return NULL;
|
---|
2143 |
|
---|
2144 | strcpy (apath, starting_directory);
|
---|
2145 |
|
---|
2146 | dest = strchr (apath, '\0');
|
---|
2147 | }
|
---|
2148 | else
|
---|
2149 | {
|
---|
2150 | apath[0] = '/';
|
---|
2151 | dest = apath + 1;
|
---|
2152 | }
|
---|
2153 |
|
---|
2154 | for (start = end = name; *start != '\0'; start = end)
|
---|
2155 | {
|
---|
2156 | unsigned long len;
|
---|
2157 |
|
---|
2158 | /* Skip sequence of multiple path-separators. */
|
---|
2159 | while (*start == '/')
|
---|
2160 | ++start;
|
---|
2161 |
|
---|
2162 | /* Find end of path component. */
|
---|
2163 | for (end = start; *end != '\0' && *end != '/'; ++end)
|
---|
2164 | ;
|
---|
2165 |
|
---|
2166 | len = end - start;
|
---|
2167 |
|
---|
2168 | if (len == 0)
|
---|
2169 | break;
|
---|
2170 | else if (len == 1 && start[0] == '.')
|
---|
2171 | /* nothing */;
|
---|
2172 | else if (len == 2 && start[0] == '.' && start[1] == '.')
|
---|
2173 | {
|
---|
2174 | /* Back up to previous component, ignore if at root already. */
|
---|
2175 | if (dest > apath + 1)
|
---|
2176 | while ((--dest)[-1] != '/');
|
---|
2177 | }
|
---|
2178 | else
|
---|
2179 | {
|
---|
2180 | if (dest[-1] != '/')
|
---|
2181 | *dest++ = '/';
|
---|
2182 |
|
---|
2183 | if (dest + len >= apath_limit)
|
---|
2184 | return NULL;
|
---|
2185 |
|
---|
2186 | dest = memcpy (dest, start, len);
|
---|
2187 | dest += len;
|
---|
2188 | *dest = '\0';
|
---|
2189 | }
|
---|
2190 | }
|
---|
2191 | #endif /* !WINDOWS32 && !__OS2__ */
|
---|
2192 |
|
---|
2193 | /* Unless it is root strip trailing separator. */
|
---|
2194 | #ifdef HAVE_DOS_PATHS /* bird (is this correct? what about UNC?) */
|
---|
2195 | if (dest > apath + 1 + (apath[0] != '/') && dest[-1] == '/')
|
---|
2196 | #else
|
---|
2197 | if (dest > apath + 1 && dest[-1] == '/')
|
---|
2198 | #endif
|
---|
2199 | --dest;
|
---|
2200 |
|
---|
2201 | *dest = '\0';
|
---|
2202 |
|
---|
2203 | return apath;
|
---|
2204 | }
|
---|
2205 |
|
---|
2206 |
|
---|
2207 | static char *
|
---|
2208 | func_realpath (char *o, char **argv, const char *funcname UNUSED)
|
---|
2209 | {
|
---|
2210 | /* Expand the argument. */
|
---|
2211 | const char *p = argv[0];
|
---|
2212 | const char *path = 0;
|
---|
2213 | int doneany = 0;
|
---|
2214 | unsigned int len = 0;
|
---|
2215 | PATH_VAR (in);
|
---|
2216 | PATH_VAR (out);
|
---|
2217 |
|
---|
2218 | while ((path = find_next_token (&p, &len)) != 0)
|
---|
2219 | {
|
---|
2220 | if (len < GET_PATH_MAX)
|
---|
2221 | {
|
---|
2222 | strncpy (in, path, len);
|
---|
2223 | in[len] = '\0';
|
---|
2224 |
|
---|
2225 | if (
|
---|
2226 | #ifdef HAVE_REALPATH
|
---|
2227 | realpath (in, out)
|
---|
2228 | #else
|
---|
2229 | abspath (in, out)
|
---|
2230 | #endif
|
---|
2231 | )
|
---|
2232 | {
|
---|
2233 | o = variable_buffer_output (o, out, strlen (out));
|
---|
2234 | o = variable_buffer_output (o, " ", 1);
|
---|
2235 | doneany = 1;
|
---|
2236 | }
|
---|
2237 | }
|
---|
2238 | }
|
---|
2239 |
|
---|
2240 | /* Kill last space. */
|
---|
2241 | if (doneany)
|
---|
2242 | --o;
|
---|
2243 |
|
---|
2244 | return o;
|
---|
2245 | }
|
---|
2246 |
|
---|
2247 | static char *
|
---|
2248 | func_abspath (char *o, char **argv, const char *funcname UNUSED)
|
---|
2249 | {
|
---|
2250 | /* Expand the argument. */
|
---|
2251 | const char *p = argv[0];
|
---|
2252 | const char *path = 0;
|
---|
2253 | int doneany = 0;
|
---|
2254 | unsigned int len = 0;
|
---|
2255 | PATH_VAR (in);
|
---|
2256 | PATH_VAR (out);
|
---|
2257 |
|
---|
2258 | while ((path = find_next_token (&p, &len)) != 0)
|
---|
2259 | {
|
---|
2260 | if (len < GET_PATH_MAX)
|
---|
2261 | {
|
---|
2262 | strncpy (in, path, len);
|
---|
2263 | in[len] = '\0';
|
---|
2264 |
|
---|
2265 | if (abspath (in, out))
|
---|
2266 | {
|
---|
2267 | o = variable_buffer_output (o, out, strlen (out));
|
---|
2268 | o = variable_buffer_output (o, " ", 1);
|
---|
2269 | doneany = 1;
|
---|
2270 | }
|
---|
2271 | }
|
---|
2272 | }
|
---|
2273 |
|
---|
2274 | /* Kill last space. */
|
---|
2275 | if (doneany)
|
---|
2276 | --o;
|
---|
2277 |
|
---|
2278 | return o;
|
---|
2279 | }
|
---|
2280 |
|
---|
2281 | #ifdef CONFIG_WITH_ABSPATHEX
|
---|
2282 | /* Same as abspath except that the current path may be given as the
|
---|
2283 | 2nd argument. */
|
---|
2284 | static char *
|
---|
2285 | func_abspathex (char *o, char **argv, const char *funcname UNUSED)
|
---|
2286 | {
|
---|
2287 | char *cwd = argv[1];
|
---|
2288 |
|
---|
2289 | /* cwd needs leading spaces chopped and may be optional,
|
---|
2290 | in which case we're exactly like $(abspath ). */
|
---|
2291 | while (isblank(*cwd))
|
---|
2292 | cwd++;
|
---|
2293 | if (!*cwd)
|
---|
2294 | o = func_abspath (o, argv, funcname);
|
---|
2295 | else
|
---|
2296 | {
|
---|
2297 | /* Expand the argument. */
|
---|
2298 | const char *p = argv[0];
|
---|
2299 | unsigned int cwd_len = ~0U;
|
---|
2300 | char *path = 0;
|
---|
2301 | int doneany = 0;
|
---|
2302 | unsigned int len = 0;
|
---|
2303 | PATH_VAR (in);
|
---|
2304 | PATH_VAR (out);
|
---|
2305 |
|
---|
2306 | while ((path = find_next_token (&p, &len)) != 0)
|
---|
2307 | {
|
---|
2308 | if (len < GET_PATH_MAX)
|
---|
2309 | {
|
---|
2310 | #ifdef HAVE_DOS_PATHS
|
---|
2311 | if (path[0] != '/' && path[0] != '\\' && (len < 2 || path[1] != ':') && cwd)
|
---|
2312 | #else
|
---|
2313 | if (path[0] != '/' && cwd)
|
---|
2314 | #endif
|
---|
2315 | {
|
---|
2316 | /* relative path, prefix with cwd. */
|
---|
2317 | if (cwd_len == ~0U)
|
---|
2318 | cwd_len = strlen (cwd);
|
---|
2319 | if (cwd_len + len + 1 >= GET_PATH_MAX)
|
---|
2320 | continue;
|
---|
2321 | memcpy (in, cwd, cwd_len);
|
---|
2322 | in[cwd_len] = '/';
|
---|
2323 | memcpy (in + cwd_len + 1, path, len);
|
---|
2324 | in[cwd_len + len + 1] = '\0';
|
---|
2325 | }
|
---|
2326 | else
|
---|
2327 | {
|
---|
2328 | /* absolute path pass it as-is. */
|
---|
2329 | memcpy (in, path, len);
|
---|
2330 | in[len] = '\0';
|
---|
2331 | }
|
---|
2332 |
|
---|
2333 | if (abspath (in, out))
|
---|
2334 | {
|
---|
2335 | o = variable_buffer_output (o, out, strlen (out));
|
---|
2336 | o = variable_buffer_output (o, " ", 1);
|
---|
2337 | doneany = 1;
|
---|
2338 | }
|
---|
2339 | }
|
---|
2340 | }
|
---|
2341 |
|
---|
2342 | /* Kill last space. */
|
---|
2343 | if (doneany)
|
---|
2344 | --o;
|
---|
2345 | }
|
---|
2346 |
|
---|
2347 | return o;
|
---|
2348 | }
|
---|
2349 | #endif
|
---|
2350 |
|
---|
2351 | #ifdef CONFIG_WITH_XARGS
|
---|
2352 | /* Create one or more command lines avoiding the max argument
|
---|
2353 | lenght restriction of the host OS.
|
---|
2354 |
|
---|
2355 | The last argument is the list of arguments that the normal
|
---|
2356 | xargs command would be fed from stdin.
|
---|
2357 |
|
---|
2358 | The first argument is initial command and it's arguments.
|
---|
2359 |
|
---|
2360 | If there are three or more arguments, the 2nd argument is
|
---|
2361 | the command and arguments to be used on subsequent
|
---|
2362 | command lines. Defaults to the initial command.
|
---|
2363 |
|
---|
2364 | If there are four or more arguments, the 3rd argument is
|
---|
2365 | the command to be used at the final command line. Defaults
|
---|
2366 | to the sub sequent or initial command .
|
---|
2367 |
|
---|
2368 | A future version of this function may define more arguments
|
---|
2369 | and therefor anyone specifying six or more arguments will
|
---|
2370 | cause fatal errors.
|
---|
2371 |
|
---|
2372 | Typical usage is:
|
---|
2373 | $(xargs ar cas mylib.a,$(objects))
|
---|
2374 | or
|
---|
2375 | $(xargs ar cas mylib.a,ar as mylib.a,$(objects))
|
---|
2376 |
|
---|
2377 | It will then create one or more "ar mylib.a ..." command
|
---|
2378 | lines with proper \n\t separation so it can be used when
|
---|
2379 | writing rules. */
|
---|
2380 | static char *
|
---|
2381 | func_xargs (char *o, char **argv, const char *funcname UNUSED)
|
---|
2382 | {
|
---|
2383 | int argc;
|
---|
2384 | const char *initial_cmd;
|
---|
2385 | size_t initial_cmd_len;
|
---|
2386 | const char *subsequent_cmd;
|
---|
2387 | size_t subsequent_cmd_len;
|
---|
2388 | const char *final_cmd;
|
---|
2389 | size_t final_cmd_len;
|
---|
2390 | const char *args;
|
---|
2391 | size_t max_args;
|
---|
2392 | int i;
|
---|
2393 |
|
---|
2394 | #ifdef ARG_MAX
|
---|
2395 | /* ARG_MAX is a bit unreliable (environment), so drop 25% of the max. */
|
---|
2396 | # define XARGS_MAX (ARG_MAX - (ARG_MAX / 4))
|
---|
2397 | #else /* FIXME: update configure with a command line length test. */
|
---|
2398 | # define XARGS_MAX 10240
|
---|
2399 | #endif
|
---|
2400 |
|
---|
2401 | argc = 0;
|
---|
2402 | while (argv[argc])
|
---|
2403 | argc++;
|
---|
2404 | if (argc > 4)
|
---|
2405 | fatal (NILF, _("Too many arguments for $(xargs)!\n"));
|
---|
2406 |
|
---|
2407 | /* first: the initial / default command.*/
|
---|
2408 | initial_cmd = argv[0];
|
---|
2409 | while (isspace ((unsigned char)*initial_cmd))
|
---|
2410 | initial_cmd++;
|
---|
2411 | max_args = initial_cmd_len = strlen (initial_cmd);
|
---|
2412 |
|
---|
2413 | /* second: the command for the subsequent command lines. defaults to the initial cmd. */
|
---|
2414 | subsequent_cmd = argc > 2 && argv[1][0] != '\0' ? argv[1] : "";
|
---|
2415 | while (isspace ((unsigned char)*subsequent_cmd))
|
---|
2416 | subsequent_cmd++;
|
---|
2417 | if (*subsequent_cmd)
|
---|
2418 | {
|
---|
2419 | subsequent_cmd_len = strlen (subsequent_cmd);
|
---|
2420 | if (subsequent_cmd_len > max_args)
|
---|
2421 | max_args = subsequent_cmd_len;
|
---|
2422 | }
|
---|
2423 | else
|
---|
2424 | {
|
---|
2425 | subsequent_cmd = initial_cmd;
|
---|
2426 | subsequent_cmd_len = initial_cmd_len;
|
---|
2427 | }
|
---|
2428 |
|
---|
2429 | /* third: the final command. defaults to the subseq cmd. */
|
---|
2430 | final_cmd = argc > 3 && argv[2][0] != '\0' ? argv[2] : "";
|
---|
2431 | while (isspace ((unsigned char)*final_cmd))
|
---|
2432 | final_cmd++;
|
---|
2433 | if (*final_cmd)
|
---|
2434 | {
|
---|
2435 | final_cmd_len = strlen (final_cmd);
|
---|
2436 | if (final_cmd_len > max_args)
|
---|
2437 | max_args = final_cmd_len;
|
---|
2438 | }
|
---|
2439 | else
|
---|
2440 | {
|
---|
2441 | final_cmd = subsequent_cmd;
|
---|
2442 | final_cmd_len = subsequent_cmd_len;
|
---|
2443 | }
|
---|
2444 |
|
---|
2445 | /* last: the arguments to split up into sensible portions. */
|
---|
2446 | args = argv[argc - 1];
|
---|
2447 |
|
---|
2448 | /* calc the max argument length. */
|
---|
2449 | if (XARGS_MAX <= max_args + 2)
|
---|
2450 | fatal (NILF, _("$(xargs): the commands are longer than the max exec argument length. (%lu <= %lu)\n"),
|
---|
2451 | (unsigned long)XARGS_MAX, (unsigned long)max_args + 2);
|
---|
2452 | max_args = XARGS_MAX - max_args - 1;
|
---|
2453 |
|
---|
2454 | /* generate the commands. */
|
---|
2455 | i = 0;
|
---|
2456 | for (i = 0; ; i++)
|
---|
2457 | {
|
---|
2458 | unsigned int len;
|
---|
2459 | const char *iterator = args;
|
---|
2460 | const char *end = args;
|
---|
2461 | const char *cur;
|
---|
2462 | const char *tmp;
|
---|
2463 |
|
---|
2464 | /* scan the arguments till we reach the end or the max length. */
|
---|
2465 | while ((cur = find_next_token(&iterator, &len))
|
---|
2466 | && (size_t)((cur + len) - args) < max_args)
|
---|
2467 | end = cur + len;
|
---|
2468 | if (cur && end == args)
|
---|
2469 | fatal (NILF, _("$(xargs): command + one single arg is too much. giving up.\n"));
|
---|
2470 |
|
---|
2471 | /* emit the command. */
|
---|
2472 | if (i == 0)
|
---|
2473 | {
|
---|
2474 | o = variable_buffer_output (o, (char *)initial_cmd, initial_cmd_len);
|
---|
2475 | o = variable_buffer_output (o, " ", 1);
|
---|
2476 | }
|
---|
2477 | else if (cur)
|
---|
2478 | {
|
---|
2479 | o = variable_buffer_output (o, "\n\t", 2);
|
---|
2480 | o = variable_buffer_output (o, (char *)subsequent_cmd, subsequent_cmd_len);
|
---|
2481 | o = variable_buffer_output (o, " ", 1);
|
---|
2482 | }
|
---|
2483 | else
|
---|
2484 | {
|
---|
2485 | o = variable_buffer_output (o, "\n\t", 2);
|
---|
2486 | o = variable_buffer_output (o, (char *)final_cmd, final_cmd_len);
|
---|
2487 | o = variable_buffer_output (o, " ", 1);
|
---|
2488 | }
|
---|
2489 |
|
---|
2490 | tmp = end;
|
---|
2491 | while (tmp > args && isspace ((unsigned char)tmp[-1])) /* drop trailing spaces. */
|
---|
2492 | tmp--;
|
---|
2493 | o = variable_buffer_output (o, (char *)args, tmp - args);
|
---|
2494 |
|
---|
2495 |
|
---|
2496 | /* next */
|
---|
2497 | if (!cur)
|
---|
2498 | break;
|
---|
2499 | args = end;
|
---|
2500 | while (isspace ((unsigned char)*args))
|
---|
2501 | args++;
|
---|
2502 | }
|
---|
2503 |
|
---|
2504 | return o;
|
---|
2505 | }
|
---|
2506 | #endif
|
---|
2507 |
|
---|
2508 | #ifdef CONFIG_WITH_TOUPPER_TOLOWER
|
---|
2509 | static char *
|
---|
2510 | func_toupper_tolower (char *o, char **argv, const char *funcname)
|
---|
2511 | {
|
---|
2512 | /* Expand the argument. */
|
---|
2513 | const char *p = argv[0];
|
---|
2514 | while (*p)
|
---|
2515 | {
|
---|
2516 | /* convert to temporary buffer */
|
---|
2517 | char tmp[256];
|
---|
2518 | unsigned int i;
|
---|
2519 | if (!strcmp(funcname, "toupper"))
|
---|
2520 | for (i = 0; i < sizeof(tmp) && *p; i++, p++)
|
---|
2521 | tmp[i] = toupper(*p);
|
---|
2522 | else
|
---|
2523 | for (i = 0; i < sizeof(tmp) && *p; i++, p++)
|
---|
2524 | tmp[i] = tolower(*p);
|
---|
2525 | o = variable_buffer_output (o, tmp, i);
|
---|
2526 | }
|
---|
2527 |
|
---|
2528 | return o;
|
---|
2529 | }
|
---|
2530 | #endif /* CONFIG_WITH_TOUPPER_TOLOWER */
|
---|
2531 |
|
---|
2532 | #if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
|
---|
2533 |
|
---|
2534 | /* Strip leading spaces and other things off a command. */
|
---|
2535 | static const char *
|
---|
2536 | comp_cmds_strip_leading (const char *s, const char *e)
|
---|
2537 | {
|
---|
2538 | while (s < e)
|
---|
2539 | {
|
---|
2540 | const char ch = *s;
|
---|
2541 | if (!isblank (ch)
|
---|
2542 | && ch != '@'
|
---|
2543 | #ifdef CONFIG_WITH_COMMANDS_FUNC
|
---|
2544 | && ch != '%'
|
---|
2545 | #endif
|
---|
2546 | && ch != '+'
|
---|
2547 | && ch != '-')
|
---|
2548 | break;
|
---|
2549 | s++;
|
---|
2550 | }
|
---|
2551 | return s;
|
---|
2552 | }
|
---|
2553 |
|
---|
2554 | /* Worker for func_comp_vars() which is called if the comparision failed.
|
---|
2555 | It will do the slow command by command comparision of the commands
|
---|
2556 | when there invoked as comp-cmds. */
|
---|
2557 | static char *
|
---|
2558 | comp_vars_ne (char *o, const char *s1, const char *e1, const char *s2, const char *e2,
|
---|
2559 | char *ne_retval, const char *funcname)
|
---|
2560 | {
|
---|
2561 | /* give up at once if not comp-cmds or comp-cmds-ex. */
|
---|
2562 | if (strcmp (funcname, "comp-cmds") != 0
|
---|
2563 | && strcmp (funcname, "comp-cmds-ex") != 0)
|
---|
2564 | o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
|
---|
2565 | else
|
---|
2566 | {
|
---|
2567 | const char * const s1_start = s1;
|
---|
2568 | int new_cmd = 1;
|
---|
2569 | int diff;
|
---|
2570 | for (;;)
|
---|
2571 | {
|
---|
2572 | /* if it's a new command, strip leading stuff. */
|
---|
2573 | if (new_cmd)
|
---|
2574 | {
|
---|
2575 | s1 = comp_cmds_strip_leading (s1, e1);
|
---|
2576 | s2 = comp_cmds_strip_leading (s2, e2);
|
---|
2577 | new_cmd = 0;
|
---|
2578 | }
|
---|
2579 | if (s1 >= e1 || s2 >= e2)
|
---|
2580 | break;
|
---|
2581 |
|
---|
2582 | /*
|
---|
2583 | * Inner compare loop which compares one line.
|
---|
2584 | * FIXME: parse quoting!
|
---|
2585 | */
|
---|
2586 | for (;;)
|
---|
2587 | {
|
---|
2588 | const char ch1 = *s1;
|
---|
2589 | const char ch2 = *s2;
|
---|
2590 | diff = ch1 - ch2;
|
---|
2591 | if (diff)
|
---|
2592 | break;
|
---|
2593 | if (ch1 == '\n')
|
---|
2594 | break;
|
---|
2595 | assert (ch1 != '\r');
|
---|
2596 |
|
---|
2597 | /* next */
|
---|
2598 | s1++;
|
---|
2599 | s2++;
|
---|
2600 | if (s1 >= e1 || s2 >= e2)
|
---|
2601 | break;
|
---|
2602 | }
|
---|
2603 |
|
---|
2604 | /*
|
---|
2605 | * If we exited because of a difference try to end-of-command
|
---|
2606 | * comparision, e.g. ignore trailing spaces.
|
---|
2607 | */
|
---|
2608 | if (diff)
|
---|
2609 | {
|
---|
2610 | /* strip */
|
---|
2611 | while (s1 < e1 && isblank (*s1))
|
---|
2612 | s1++;
|
---|
2613 | while (s2 < e2 && isblank (*s2))
|
---|
2614 | s2++;
|
---|
2615 | if (s1 >= e1 || s2 >= e2)
|
---|
2616 | break;
|
---|
2617 |
|
---|
2618 | /* compare again and check that it's a newline. */
|
---|
2619 | if (*s2 != '\n' || *s1 != '\n')
|
---|
2620 | break;
|
---|
2621 | }
|
---|
2622 | /* Break out if we exited because of EOS. */
|
---|
2623 | else if (s1 >= e1 || s2 >= e2)
|
---|
2624 | break;
|
---|
2625 |
|
---|
2626 | /*
|
---|
2627 | * Detect the end of command lines.
|
---|
2628 | */
|
---|
2629 | if (*s1 == '\n')
|
---|
2630 | new_cmd = s1 == s1_start || s1[-1] != '\\';
|
---|
2631 | s1++;
|
---|
2632 | s2++;
|
---|
2633 | }
|
---|
2634 |
|
---|
2635 | /*
|
---|
2636 | * Ignore trailing empty lines.
|
---|
2637 | */
|
---|
2638 | if (s1 < e1 || s2 < e2)
|
---|
2639 | {
|
---|
2640 | while (s1 < e1 && (isblank (*s1) || *s1 == '\n'))
|
---|
2641 | if (*s1++ == '\n')
|
---|
2642 | s1 = comp_cmds_strip_leading (s1, e1);
|
---|
2643 | while (s2 < e2 && (isblank (*s2) || *s2 == '\n'))
|
---|
2644 | if (*s2++ == '\n')
|
---|
2645 | s2 = comp_cmds_strip_leading (s2, e2);
|
---|
2646 | }
|
---|
2647 |
|
---|
2648 | /* emit the result. */
|
---|
2649 | if (s1 == e1 && s2 == e2)
|
---|
2650 | o = variable_buffer_output (o, "", 1);
|
---|
2651 | else
|
---|
2652 | o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
|
---|
2653 | }
|
---|
2654 | return o;
|
---|
2655 | }
|
---|
2656 |
|
---|
2657 | /*
|
---|
2658 | $(comp-vars var1,var2,not-equal-return)
|
---|
2659 | or
|
---|
2660 | $(comp-cmds cmd-var1,cmd-var2,not-equal-return)
|
---|
2661 |
|
---|
2662 | Compares the two variables (that's given by name to avoid unnecessary
|
---|
2663 | expanding) and return the string in the third argument if not equal.
|
---|
2664 | If equal, nothing is returned.
|
---|
2665 |
|
---|
2666 | comp-vars will to an exact comparision only stripping leading and
|
---|
2667 | trailing spaces.
|
---|
2668 |
|
---|
2669 | comp-cmds will compare command by command, ignoring not only leading
|
---|
2670 | and trailing spaces on each line but also leading one leading '@',
|
---|
2671 | '-', '+' and '%'
|
---|
2672 | */
|
---|
2673 | static char *
|
---|
2674 | func_comp_vars (char *o, char **argv, const char *funcname)
|
---|
2675 | {
|
---|
2676 | const char *s1, *e1, *x1, *s2, *e2, *x2;
|
---|
2677 | char *a1 = NULL, *a2 = NULL;
|
---|
2678 | size_t l, l1, l2;
|
---|
2679 | struct variable *var1 = lookup_variable (argv[0], strlen (argv[0]));
|
---|
2680 | struct variable *var2 = lookup_variable (argv[1], strlen (argv[1]));
|
---|
2681 |
|
---|
2682 | /* the simple cases */
|
---|
2683 | if (var1 == var2)
|
---|
2684 | return variable_buffer_output (o, "", 0); /* eq */
|
---|
2685 | if (!var1 || !var2)
|
---|
2686 | return variable_buffer_output (o, argv[2], strlen(argv[2]));
|
---|
2687 | if (var1->value == var2->value)
|
---|
2688 | return variable_buffer_output (o, "", 0); /* eq */
|
---|
2689 | if (!var1->recursive && !var2->recursive)
|
---|
2690 | {
|
---|
2691 | if ( var1->value_length == var2->value_length
|
---|
2692 | && !memcmp (var1->value, var2->value, var1->value_length))
|
---|
2693 | return variable_buffer_output (o, "", 0); /* eq */
|
---|
2694 |
|
---|
2695 | /* ignore trailing and leading blanks */
|
---|
2696 | s1 = var1->value;
|
---|
2697 | e1 = s1 + var1->value_length;
|
---|
2698 | while (isblank ((unsigned char) *s1))
|
---|
2699 | s1++;
|
---|
2700 | while (e1 > s1 && isblank ((unsigned char) e1[-1]))
|
---|
2701 | e1--;
|
---|
2702 |
|
---|
2703 | s2 = var2->value;
|
---|
2704 | e2 = s2 + var2->value_length;
|
---|
2705 | while (isblank ((unsigned char) *s2))
|
---|
2706 | s2++;
|
---|
2707 | while (e2 > s2 && isblank ((unsigned char) e2[-1]))
|
---|
2708 | e2--;
|
---|
2709 |
|
---|
2710 | if (e1 - s1 != e2 - s2)
|
---|
2711 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2712 | l_simple_compare:
|
---|
2713 | if (!memcmp (s1, s2, e1 - s1))
|
---|
2714 | return variable_buffer_output (o, "", 0); /* eq */
|
---|
2715 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2716 | }
|
---|
2717 |
|
---|
2718 | /* ignore trailing and leading blanks */
|
---|
2719 | s1 = var1->value;
|
---|
2720 | e1 = s1 + var1->value_length;
|
---|
2721 | while (isblank ((unsigned char) *s1))
|
---|
2722 | s1++;
|
---|
2723 | while (e1 > s1 && isblank ((unsigned char) e1[-1]))
|
---|
2724 | e1--;
|
---|
2725 |
|
---|
2726 | s2 = var2->value;
|
---|
2727 | e2 = s2 + var2->value_length;
|
---|
2728 | while (isblank((unsigned char)*s2))
|
---|
2729 | s2++;
|
---|
2730 | while (e2 > s2 && isblank ((unsigned char) e2[-1]))
|
---|
2731 | e2--;
|
---|
2732 |
|
---|
2733 | /* both empty after stripping? */
|
---|
2734 | if (s1 == e1 && s2 == e2)
|
---|
2735 | return variable_buffer_output (o, "", 0); /* eq */
|
---|
2736 |
|
---|
2737 | /* optimist. */
|
---|
2738 | if ( e1 - s1 == e2 - s2
|
---|
2739 | && !memcmp(s1, s2, e1 - s1))
|
---|
2740 | return variable_buffer_output (o, "", 0); /* eq */
|
---|
2741 |
|
---|
2742 | /* compare up to the first '$' or the end. */
|
---|
2743 | x1 = var1->recursive ? memchr (s1, '$', e1 - s1) : NULL;
|
---|
2744 | x2 = var2->recursive ? memchr (s2, '$', e2 - s2) : NULL;
|
---|
2745 | if (!x1 && !x2)
|
---|
2746 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2747 |
|
---|
2748 | l1 = x1 ? x1 - s1 : e1 - s1;
|
---|
2749 | l2 = x2 ? x2 - s2 : e2 - s2;
|
---|
2750 | l = l1 <= l2 ? l1 : l2;
|
---|
2751 | if (l && memcmp (s1, s2, l))
|
---|
2752 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2753 |
|
---|
2754 | /* one or both buffers now require expanding. */
|
---|
2755 | if (!x1)
|
---|
2756 | s1 += l;
|
---|
2757 | else
|
---|
2758 | {
|
---|
2759 | s1 = a1 = allocated_variable_expand ((char *)s1 + l);
|
---|
2760 | if (!l)
|
---|
2761 | while (isblank ((unsigned char) *s1))
|
---|
2762 | s1++;
|
---|
2763 | e1 = strchr (s1, '\0');
|
---|
2764 | while (e1 > s1 && isblank ((unsigned char) e1[-1]))
|
---|
2765 | e1--;
|
---|
2766 | }
|
---|
2767 |
|
---|
2768 | if (!x2)
|
---|
2769 | s2 += l;
|
---|
2770 | else
|
---|
2771 | {
|
---|
2772 | s2 = a2 = allocated_variable_expand ((char *)s2 + l);
|
---|
2773 | if (!l)
|
---|
2774 | while (isblank ((unsigned char) *s2))
|
---|
2775 | s2++;
|
---|
2776 | e2 = strchr (s2, '\0');
|
---|
2777 | while (e2 > s2 && isblank ((unsigned char) e2[-1]))
|
---|
2778 | e2--;
|
---|
2779 | }
|
---|
2780 |
|
---|
2781 | /* the final compare */
|
---|
2782 | if ( e1 - s1 != e2 - s2
|
---|
2783 | || memcmp (s1, s2, e1 - s1))
|
---|
2784 | o = comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2785 | else
|
---|
2786 | o = variable_buffer_output (o, "", 1); /* eq */
|
---|
2787 | if (a1)
|
---|
2788 | free (a1);
|
---|
2789 | if (a2)
|
---|
2790 | free (a2);
|
---|
2791 | return o;
|
---|
2792 | }
|
---|
2793 |
|
---|
2794 | /*
|
---|
2795 | $(comp-cmds-ex cmds1,cmds2,not-equal-return)
|
---|
2796 |
|
---|
2797 | Compares the two strings and return the string in the third argument
|
---|
2798 | if not equal. If equal, nothing is returned.
|
---|
2799 |
|
---|
2800 | The comparision will be performed command by command, ignoring not
|
---|
2801 | only leading and trailing spaces on each line but also leading one
|
---|
2802 | leading '@', '-', '+' and '%'.
|
---|
2803 | */
|
---|
2804 | static char *
|
---|
2805 | func_comp_cmds_ex (char *o, char **argv, const char *funcname)
|
---|
2806 | {
|
---|
2807 | const char *s1, *e1, *s2, *e2;
|
---|
2808 | size_t l, l1, l2;
|
---|
2809 |
|
---|
2810 | /* the simple cases */
|
---|
2811 | s1 = argv[0];
|
---|
2812 | s2 = argv[1];
|
---|
2813 | if (s1 == s2)
|
---|
2814 | return variable_buffer_output (o, "", 0); /* eq */
|
---|
2815 | l1 = strlen (argv[0]);
|
---|
2816 | l2 = strlen (argv[1]);
|
---|
2817 |
|
---|
2818 | if ( l1 == l2
|
---|
2819 | && !memcmp (s1, s2, l1))
|
---|
2820 | return variable_buffer_output (o, "", 0); /* eq */
|
---|
2821 |
|
---|
2822 | /* ignore trailing and leading blanks */
|
---|
2823 | e1 = s1 + l1;
|
---|
2824 | s1 = comp_cmds_strip_leading (s1, e1);
|
---|
2825 |
|
---|
2826 | e2 = s2 + l2;
|
---|
2827 | s2 = comp_cmds_strip_leading (s2, e2);
|
---|
2828 |
|
---|
2829 | if (e1 - s1 != e2 - s2)
|
---|
2830 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2831 | if (!memcmp (s1, s2, e1 - s1))
|
---|
2832 | return variable_buffer_output (o, "", 0); /* eq */
|
---|
2833 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
---|
2834 | }
|
---|
2835 | #endif
|
---|
2836 |
|
---|
2837 | #ifdef CONFIG_WITH_DATE
|
---|
2838 | # if defined (_MSC_VER) /* FIXME: !defined (HAVE_STRPTIME) */
|
---|
2839 | char *strptime(const char *s, const char *format, struct tm *tm)
|
---|
2840 | {
|
---|
2841 | return (char *)"strptime is not implemented";
|
---|
2842 | }
|
---|
2843 | # endif
|
---|
2844 | /* Check if the string is all blanks or not. */
|
---|
2845 | static int
|
---|
2846 | all_blanks (const char *s)
|
---|
2847 | {
|
---|
2848 | if (!s)
|
---|
2849 | return 1;
|
---|
2850 | while (isspace ((unsigned char)*s))
|
---|
2851 | s++;
|
---|
2852 | return *s == '\0';
|
---|
2853 | }
|
---|
2854 |
|
---|
2855 | /* The first argument is the strftime format string, a iso
|
---|
2856 | timestamp is the default if nothing is given.
|
---|
2857 |
|
---|
2858 | The second argument is a time value if given. The format
|
---|
2859 | is either the format from the first argument or given as
|
---|
2860 | an additional third argument. */
|
---|
2861 | static char *
|
---|
2862 | func_date (char *o, char **argv, const char *funcname)
|
---|
2863 | {
|
---|
2864 | char *p;
|
---|
2865 | char *buf;
|
---|
2866 | size_t buf_size;
|
---|
2867 | struct tm t;
|
---|
2868 | const char *format;
|
---|
2869 |
|
---|
2870 | /* determin the format - use a single word as the default. */
|
---|
2871 | format = !strcmp (funcname, "date-utc")
|
---|
2872 | ? "%Y-%m-%dT%H:%M:%SZ"
|
---|
2873 | : "%Y-%m-%dT%H:%M:%S";
|
---|
2874 | if (!all_blanks (argv[0]))
|
---|
2875 | format = argv[0];
|
---|
2876 |
|
---|
2877 | /* get the time. */
|
---|
2878 | memset (&t, 0, sizeof(t));
|
---|
2879 | if (argv[0] && !all_blanks (argv[1]))
|
---|
2880 | {
|
---|
2881 | const char *input_format = !all_blanks (argv[2]) ? argv[2] : format;
|
---|
2882 | p = strptime (argv[1], input_format, &t);
|
---|
2883 | if (!p || *p != '\0')
|
---|
2884 | {
|
---|
2885 | error (NILF, _("$(%s): strptime(%s,%s,) -> %s\n"), funcname,
|
---|
2886 | argv[1], input_format, p ? p : "<null>");
|
---|
2887 | return variable_buffer_output (o, "", 0);
|
---|
2888 | }
|
---|
2889 | }
|
---|
2890 | else
|
---|
2891 | {
|
---|
2892 | time_t tval;
|
---|
2893 | time (&tval);
|
---|
2894 | if (!strcmp (funcname, "date-utc"))
|
---|
2895 | t = *gmtime (&tval);
|
---|
2896 | else
|
---|
2897 | t = *localtime (&tval);
|
---|
2898 | }
|
---|
2899 |
|
---|
2900 | /* format it. note that zero isn't necessarily an error, so we'll
|
---|
2901 | have to keep shut about failures. */
|
---|
2902 | buf_size = 64;
|
---|
2903 | buf = xmalloc (buf_size);
|
---|
2904 | while (strftime (buf, buf_size, format, &t) == 0)
|
---|
2905 | {
|
---|
2906 | if (buf_size >= 4096)
|
---|
2907 | {
|
---|
2908 | *buf = '\0';
|
---|
2909 | break;
|
---|
2910 | }
|
---|
2911 | buf = xrealloc (buf, buf_size <<= 1);
|
---|
2912 | }
|
---|
2913 | o = variable_buffer_output (o, buf, strlen (buf));
|
---|
2914 | free (buf);
|
---|
2915 | return o;
|
---|
2916 | }
|
---|
2917 | #endif
|
---|
2918 |
|
---|
2919 | #ifdef CONFIG_WITH_FILE_SIZE
|
---|
2920 | /* Prints the size of the specified file. Only one file is
|
---|
2921 | permitted, notthing is stripped. -1 is returned if stat
|
---|
2922 | fails. */
|
---|
2923 | static char *
|
---|
2924 | func_file_size (char *o, char **argv, const char *funcname UNUSED)
|
---|
2925 | {
|
---|
2926 | struct stat st;
|
---|
2927 | if (stat (argv[0], &st))
|
---|
2928 | return variable_buffer_output (o, "-1", 2);
|
---|
2929 | return math_int_to_variable_buffer (o, st.st_size);
|
---|
2930 | }
|
---|
2931 | #endif
|
---|
2932 |
|
---|
2933 | #ifdef CONFIG_WITH_WHICH
|
---|
2934 | /* Checks if the specified file exists an is executable.
|
---|
2935 | On systems employing executable extensions, the name may
|
---|
2936 | be modified to include the extension. */
|
---|
2937 | static int func_which_test_x (char *file)
|
---|
2938 | {
|
---|
2939 | struct stat st;
|
---|
2940 | # if defined(WINDOWS32) || defined(__OS2__)
|
---|
2941 | char *ext;
|
---|
2942 | char *slash;
|
---|
2943 |
|
---|
2944 | /* fix slashes first. */
|
---|
2945 | slash = file;
|
---|
2946 | while ((slash = strchr (slash, '\\')) != NULL)
|
---|
2947 | *slash++ = '/';
|
---|
2948 |
|
---|
2949 | /* straight */
|
---|
2950 | if (stat (file, &st) == 0
|
---|
2951 | && S_ISREG (st.st_mode))
|
---|
2952 | return 1;
|
---|
2953 |
|
---|
2954 | /* don't try add an extension if there already is one */
|
---|
2955 | ext = strchr (file, '\0');
|
---|
2956 | if (ext - file >= 4
|
---|
2957 | && ( !stricmp (ext - 4, ".exe")
|
---|
2958 | || !stricmp (ext - 4, ".cmd")
|
---|
2959 | || !stricmp (ext - 4, ".bat")
|
---|
2960 | || !stricmp (ext - 4, ".com")))
|
---|
2961 | return 0;
|
---|
2962 |
|
---|
2963 | /* try the extensions. */
|
---|
2964 | strcpy (ext, ".exe");
|
---|
2965 | if (stat (file, &st) == 0
|
---|
2966 | && S_ISREG (st.st_mode))
|
---|
2967 | return 1;
|
---|
2968 |
|
---|
2969 | strcpy (ext, ".cmd");
|
---|
2970 | if (stat (file, &st) == 0
|
---|
2971 | && S_ISREG (st.st_mode))
|
---|
2972 | return 1;
|
---|
2973 |
|
---|
2974 | strcpy (ext, ".bat");
|
---|
2975 | if (stat (file, &st) == 0
|
---|
2976 | && S_ISREG (st.st_mode))
|
---|
2977 | return 1;
|
---|
2978 |
|
---|
2979 | strcpy (ext, ".com");
|
---|
2980 | if (stat (file, &st) == 0
|
---|
2981 | && S_ISREG (st.st_mode))
|
---|
2982 | return 1;
|
---|
2983 |
|
---|
2984 | return 0;
|
---|
2985 |
|
---|
2986 | # else
|
---|
2987 |
|
---|
2988 | return access (file, X_OK) == 0
|
---|
2989 | && stat (file, &st) == 0
|
---|
2990 | && S_ISREG (st.st_mode);
|
---|
2991 | # endif
|
---|
2992 | }
|
---|
2993 |
|
---|
2994 | /* Searches for the specified programs in the PATH and print
|
---|
2995 | their full location if found. Prints nothing if not found. */
|
---|
2996 | static char *
|
---|
2997 | func_which (char *o, char **argv, const char *funcname UNUSED)
|
---|
2998 | {
|
---|
2999 | const char *path;
|
---|
3000 | struct variable *path_var;
|
---|
3001 | unsigned i;
|
---|
3002 | PATH_VAR (buf);
|
---|
3003 |
|
---|
3004 | path_var = lookup_variable ("PATH", 4);
|
---|
3005 | if (path_var)
|
---|
3006 | path = path_var->value;
|
---|
3007 | else
|
---|
3008 | path = ".";
|
---|
3009 |
|
---|
3010 | /* iterate input */
|
---|
3011 | for (i = 0; argv[i]; i++)
|
---|
3012 | {
|
---|
3013 | unsigned int len;
|
---|
3014 | const char *iterator = argv[i];
|
---|
3015 | char *cur;
|
---|
3016 |
|
---|
3017 | while ((cur = find_next_token (&iterator, &len)))
|
---|
3018 | {
|
---|
3019 | /* if there is a separator, don't walk the path. */
|
---|
3020 | if (memchr (cur, '/', len)
|
---|
3021 | #ifdef HAVE_DOS_PATHS
|
---|
3022 | || memchr (cur, '\\', len)
|
---|
3023 | || memchr (cur, ':', len)
|
---|
3024 | #endif
|
---|
3025 | )
|
---|
3026 | {
|
---|
3027 | if (len + 1 + 4 < GET_PATH_MAX) /* +4 for .exe */
|
---|
3028 | {
|
---|
3029 | memcpy (buf, cur, len);
|
---|
3030 | buf[len] = '\0';
|
---|
3031 | if (func_which_test_x (buf))
|
---|
3032 | o = variable_buffer_output (o, buf, strlen (buf));
|
---|
3033 | }
|
---|
3034 | }
|
---|
3035 | else
|
---|
3036 | {
|
---|
3037 | const char *comp = path;
|
---|
3038 | for (;;)
|
---|
3039 | {
|
---|
3040 | const char *src = comp;
|
---|
3041 | const char *end = strchr (comp, PATH_SEPARATOR_CHAR);
|
---|
3042 | size_t comp_len = end ? end - comp : strlen (comp);
|
---|
3043 | if (!comp_len)
|
---|
3044 | {
|
---|
3045 | comp_len = 1;
|
---|
3046 | src = ".";
|
---|
3047 | }
|
---|
3048 | if (len + comp_len + 2 + 4 < GET_PATH_MAX) /* +4 for .exe */
|
---|
3049 | {
|
---|
3050 | memcpy (buf, comp, comp_len);
|
---|
3051 | buf [comp_len] = '/';
|
---|
3052 | memcpy (&buf[comp_len + 1], cur, len);
|
---|
3053 | buf[comp_len + 1 + len] = '\0';
|
---|
3054 |
|
---|
3055 | if (func_which_test_x (buf))
|
---|
3056 | {
|
---|
3057 | o = variable_buffer_output (o, buf, strlen (buf));
|
---|
3058 | break;
|
---|
3059 | }
|
---|
3060 | }
|
---|
3061 |
|
---|
3062 | /* next */
|
---|
3063 | if (!end)
|
---|
3064 | break;
|
---|
3065 | comp = end + 1;
|
---|
3066 | }
|
---|
3067 | }
|
---|
3068 | }
|
---|
3069 | }
|
---|
3070 |
|
---|
3071 | return variable_buffer_output (o, "", 0);
|
---|
3072 | }
|
---|
3073 | #endif
|
---|
3074 |
|
---|
3075 | #ifdef CONFIG_WITH_STACK
|
---|
3076 |
|
---|
3077 | /* Push an item (string without spaces). */
|
---|
3078 | static char *
|
---|
3079 | func_stack_push (char *o, char **argv, const char *funcname UNUSED)
|
---|
3080 | {
|
---|
3081 | do_variable_definition(NILF, argv[0], argv[1], o_file, f_append, 0 /* !target_var */);
|
---|
3082 | return o;
|
---|
3083 | }
|
---|
3084 |
|
---|
3085 | /* Pops an item off the stack / get the top stack element.
|
---|
3086 | (This is what's tricky to do in pure GNU make syntax.) */
|
---|
3087 | static char *
|
---|
3088 | func_stack_pop_top (char *o, char **argv, const char *funcname)
|
---|
3089 | {
|
---|
3090 | struct variable *stack_var;
|
---|
3091 | const char *stack = argv[0];
|
---|
3092 | const int return_item = argv[0][sizeof("stack-pop") - 1] == '\0';
|
---|
3093 |
|
---|
3094 | stack_var = lookup_variable (stack, strlen (stack) );
|
---|
3095 | if (stack_var)
|
---|
3096 | {
|
---|
3097 | unsigned int len;
|
---|
3098 | const char *iterator = stack_var->value;
|
---|
3099 | char *lastitem = NULL;
|
---|
3100 | char *cur;
|
---|
3101 |
|
---|
3102 | while ((cur = find_next_token (&iterator, &len)))
|
---|
3103 | lastitem = cur;
|
---|
3104 |
|
---|
3105 | if (lastitem != NULL)
|
---|
3106 | {
|
---|
3107 | if (strcmp (funcname, "stack-popv") != 0)
|
---|
3108 | o = variable_buffer_output (o, lastitem, len);
|
---|
3109 | if (strcmp (funcname, "stack-top") != 0)
|
---|
3110 | {
|
---|
3111 | *lastitem = '\0';
|
---|
3112 | while (lastitem > stack_var->value && isspace (lastitem[-1]))
|
---|
3113 | *--lastitem = '\0';
|
---|
3114 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
3115 | stack_var->value_length = lastitem - stack_var->value;
|
---|
3116 | #endif
|
---|
3117 | }
|
---|
3118 | }
|
---|
3119 | }
|
---|
3120 | return o;
|
---|
3121 | }
|
---|
3122 | #endif /* CONFIG_WITH_STACK */
|
---|
3123 |
|
---|
3124 | #if defined (CONFIG_WITH_MATH) || defined (CONFIG_WITH_NANOTS) || defined (CONFIG_WITH_FILE_SIZE)
|
---|
3125 | /* outputs the number (as a string) into the variable buffer. */
|
---|
3126 | static char *
|
---|
3127 | math_int_to_variable_buffer (char *o, math_int num)
|
---|
3128 | {
|
---|
3129 | static const char xdigits[17] = "0123456789abcdef";
|
---|
3130 | int negative;
|
---|
3131 | char strbuf[24]; /* 16 hex + 2 prefix + sign + term => 20
|
---|
3132 | or 20 dec + sign + term => 22 */
|
---|
3133 | char *str = &strbuf[sizeof (strbuf) - 1];
|
---|
3134 |
|
---|
3135 | negative = num < 0;
|
---|
3136 | if (negative)
|
---|
3137 | num = -num;
|
---|
3138 |
|
---|
3139 | *str = '\0';
|
---|
3140 |
|
---|
3141 | do
|
---|
3142 | {
|
---|
3143 | #ifdef HEX_MATH_NUMBERS
|
---|
3144 | *--str = xdigits[num & 0xf];
|
---|
3145 | num >>= 4;
|
---|
3146 | #else
|
---|
3147 | *--str = xdigits[num % 10];
|
---|
3148 | num /= 10;
|
---|
3149 | #endif
|
---|
3150 | }
|
---|
3151 | while (num);
|
---|
3152 |
|
---|
3153 | #ifdef HEX_MATH_NUMBERS
|
---|
3154 | *--str = 'x';
|
---|
3155 | *--str = '0';
|
---|
3156 | #endif
|
---|
3157 |
|
---|
3158 | if (negative)
|
---|
3159 | *--str = '-';
|
---|
3160 |
|
---|
3161 | return variable_buffer_output (o, str, &strbuf[sizeof (strbuf) - 1] - str);
|
---|
3162 | }
|
---|
3163 | #endif /* CONFIG_WITH_MATH || CONFIG_WITH_NANOTS */
|
---|
3164 |
|
---|
3165 | #ifdef CONFIG_WITH_MATH
|
---|
3166 |
|
---|
3167 | /* Converts a string to an integer, causes an error if the format is invalid. */
|
---|
3168 | static math_int
|
---|
3169 | math_int_from_string (const char *str)
|
---|
3170 | {
|
---|
3171 | const char *start;
|
---|
3172 | unsigned base = 0;
|
---|
3173 | int negative = 0;
|
---|
3174 | math_int num = 0;
|
---|
3175 |
|
---|
3176 | /* strip spaces */
|
---|
3177 | while (isspace (*str))
|
---|
3178 | str++;
|
---|
3179 | if (!*str)
|
---|
3180 | {
|
---|
3181 | error (NILF, _("bad number: empty\n"));
|
---|
3182 | return 0;
|
---|
3183 | }
|
---|
3184 | start = str;
|
---|
3185 |
|
---|
3186 | /* check for +/- */
|
---|
3187 | while (*str == '+' || *str == '-' || isspace (*str))
|
---|
3188 | if (*str++ == '-')
|
---|
3189 | negative = !negative;
|
---|
3190 |
|
---|
3191 | /* check for prefix - we do not accept octal numbers, sorry. */
|
---|
3192 | if (*str == '0' && (str[1] == 'x' || str[1] == 'X'))
|
---|
3193 | {
|
---|
3194 | base = 16;
|
---|
3195 | str += 2;
|
---|
3196 | }
|
---|
3197 | else
|
---|
3198 | {
|
---|
3199 | /* look for a hex digit, if not found treat it as decimal */
|
---|
3200 | const char *p2 = str;
|
---|
3201 | for ( ; *p2; p2++)
|
---|
3202 | if (isxdigit (*p2) && !isdigit (*p2) && isascii (*p2) )
|
---|
3203 | {
|
---|
3204 | base = 16;
|
---|
3205 | break;
|
---|
3206 | }
|
---|
3207 | if (base == 0)
|
---|
3208 | base = 10;
|
---|
3209 | }
|
---|
3210 |
|
---|
3211 | /* must have at least one digit! */
|
---|
3212 | if ( !isascii (*str)
|
---|
3213 | || !(base == 16 ? isxdigit (*str) : isdigit (*str)) )
|
---|
3214 | {
|
---|
3215 | error (NILF, _("bad number: '%s'\n"), start);
|
---|
3216 | return 0;
|
---|
3217 | }
|
---|
3218 |
|
---|
3219 | /* convert it! */
|
---|
3220 | while (*str && !isspace (*str))
|
---|
3221 | {
|
---|
3222 | int ch = *str++;
|
---|
3223 | if (ch >= '0' && ch <= '9')
|
---|
3224 | ch -= '0';
|
---|
3225 | else if (base == 16 && ch >= 'a' && ch <= 'f')
|
---|
3226 | ch -= 'a' - 10;
|
---|
3227 | else if (base == 16 && ch >= 'A' && ch <= 'F')
|
---|
3228 | ch -= 'A' - 10;
|
---|
3229 | else
|
---|
3230 | {
|
---|
3231 | error (NILF, _("bad number: '%s' (base=%d, pos=%d)\n"), start, base, str - start);
|
---|
3232 | return 0;
|
---|
3233 | }
|
---|
3234 | num *= base;
|
---|
3235 | num += ch;
|
---|
3236 | }
|
---|
3237 |
|
---|
3238 | /* check trailing spaces. */
|
---|
3239 | while (isspace (*str))
|
---|
3240 | str++;
|
---|
3241 | if (*str)
|
---|
3242 | {
|
---|
3243 | error (NILF, _("bad number: '%s'\n"), start);
|
---|
3244 | return 0;
|
---|
3245 | }
|
---|
3246 |
|
---|
3247 | return negative ? -num : num;
|
---|
3248 | }
|
---|
3249 |
|
---|
3250 | /* Add two or more integer numbers. */
|
---|
3251 | static char *
|
---|
3252 | func_int_add (char *o, char **argv, const char *funcname UNUSED)
|
---|
3253 | {
|
---|
3254 | math_int num;
|
---|
3255 | int i;
|
---|
3256 |
|
---|
3257 | num = math_int_from_string (argv[0]);
|
---|
3258 | for (i = 1; argv[i]; i++)
|
---|
3259 | num += math_int_from_string (argv[i]);
|
---|
3260 |
|
---|
3261 | return math_int_to_variable_buffer (o, num);
|
---|
3262 | }
|
---|
3263 |
|
---|
3264 | /* Subtract two or more integer numbers. */
|
---|
3265 | static char *
|
---|
3266 | func_int_sub (char *o, char **argv, const char *funcname UNUSED)
|
---|
3267 | {
|
---|
3268 | math_int num;
|
---|
3269 | int i;
|
---|
3270 |
|
---|
3271 | num = math_int_from_string (argv[0]);
|
---|
3272 | for (i = 1; argv[i]; i++)
|
---|
3273 | num -= math_int_from_string (argv[i]);
|
---|
3274 |
|
---|
3275 | return math_int_to_variable_buffer (o, num);
|
---|
3276 | }
|
---|
3277 |
|
---|
3278 | /* Multiply two or more integer numbers. */
|
---|
3279 | static char *
|
---|
3280 | func_int_mul (char *o, char **argv, const char *funcname UNUSED)
|
---|
3281 | {
|
---|
3282 | math_int num;
|
---|
3283 | int i;
|
---|
3284 |
|
---|
3285 | num = math_int_from_string (argv[0]);
|
---|
3286 | for (i = 1; argv[i]; i++)
|
---|
3287 | num *= math_int_from_string (argv[i]);
|
---|
3288 |
|
---|
3289 | return math_int_to_variable_buffer (o, num);
|
---|
3290 | }
|
---|
3291 |
|
---|
3292 | /* Divide an integer number by one or more divisors. */
|
---|
3293 | static char *
|
---|
3294 | func_int_div (char *o, char **argv, const char *funcname UNUSED)
|
---|
3295 | {
|
---|
3296 | math_int num;
|
---|
3297 | math_int divisor;
|
---|
3298 | int i;
|
---|
3299 |
|
---|
3300 | num = math_int_from_string (argv[0]);
|
---|
3301 | for (i = 1; argv[i]; i++)
|
---|
3302 | {
|
---|
3303 | divisor = math_int_from_string (argv[i]);
|
---|
3304 | if (!divisor)
|
---|
3305 | {
|
---|
3306 | error (NILF, _("divide by zero ('%s')\n"), argv[i]);
|
---|
3307 | return math_int_to_variable_buffer (o, 0);
|
---|
3308 | }
|
---|
3309 | num /= divisor;
|
---|
3310 | }
|
---|
3311 |
|
---|
3312 | return math_int_to_variable_buffer (o, num);
|
---|
3313 | }
|
---|
3314 |
|
---|
3315 |
|
---|
3316 | /* Divide and return the remainder. */
|
---|
3317 | static char *
|
---|
3318 | func_int_mod (char *o, char **argv, const char *funcname UNUSED)
|
---|
3319 | {
|
---|
3320 | math_int num;
|
---|
3321 | math_int divisor;
|
---|
3322 |
|
---|
3323 | num = math_int_from_string (argv[0]);
|
---|
3324 | divisor = math_int_from_string (argv[1]);
|
---|
3325 | if (!divisor)
|
---|
3326 | {
|
---|
3327 | error (NILF, _("divide by zero ('%s')\n"), argv[1]);
|
---|
3328 | return math_int_to_variable_buffer (o, 0);
|
---|
3329 | }
|
---|
3330 | num %= divisor;
|
---|
3331 |
|
---|
3332 | return math_int_to_variable_buffer (o, num);
|
---|
3333 | }
|
---|
3334 |
|
---|
3335 | /* 2-complement. */
|
---|
3336 | static char *
|
---|
3337 | func_int_not (char *o, char **argv, const char *funcname UNUSED)
|
---|
3338 | {
|
---|
3339 | math_int num;
|
---|
3340 |
|
---|
3341 | num = math_int_from_string (argv[0]);
|
---|
3342 | num = ~num;
|
---|
3343 |
|
---|
3344 | return math_int_to_variable_buffer (o, num);
|
---|
3345 | }
|
---|
3346 |
|
---|
3347 | /* Bitwise AND (two or more numbers). */
|
---|
3348 | static char *
|
---|
3349 | func_int_and (char *o, char **argv, const char *funcname UNUSED)
|
---|
3350 | {
|
---|
3351 | math_int num;
|
---|
3352 | int i;
|
---|
3353 |
|
---|
3354 | num = math_int_from_string (argv[0]);
|
---|
3355 | for (i = 1; argv[i]; i++)
|
---|
3356 | num &= math_int_from_string (argv[i]);
|
---|
3357 |
|
---|
3358 | return math_int_to_variable_buffer (o, num);
|
---|
3359 | }
|
---|
3360 |
|
---|
3361 | /* Bitwise OR (two or more numbers). */
|
---|
3362 | static char *
|
---|
3363 | func_int_or (char *o, char **argv, const char *funcname UNUSED)
|
---|
3364 | {
|
---|
3365 | math_int num;
|
---|
3366 | int i;
|
---|
3367 |
|
---|
3368 | num = math_int_from_string (argv[0]);
|
---|
3369 | for (i = 1; argv[i]; i++)
|
---|
3370 | num |= math_int_from_string (argv[i]);
|
---|
3371 |
|
---|
3372 | return math_int_to_variable_buffer (o, num);
|
---|
3373 | }
|
---|
3374 |
|
---|
3375 | /* Bitwise XOR (two or more numbers). */
|
---|
3376 | static char *
|
---|
3377 | func_int_xor (char *o, char **argv, const char *funcname UNUSED)
|
---|
3378 | {
|
---|
3379 | math_int num;
|
---|
3380 | int i;
|
---|
3381 |
|
---|
3382 | num = math_int_from_string (argv[0]);
|
---|
3383 | for (i = 1; argv[i]; i++)
|
---|
3384 | num ^= math_int_from_string (argv[i]);
|
---|
3385 |
|
---|
3386 | return math_int_to_variable_buffer (o, num);
|
---|
3387 | }
|
---|
3388 |
|
---|
3389 | /* Compare two integer numbers. Returns make boolean (true="1"; false=""). */
|
---|
3390 | static char *
|
---|
3391 | func_int_cmp (char *o, char **argv, const char *funcname)
|
---|
3392 | {
|
---|
3393 | math_int num1;
|
---|
3394 | math_int num2;
|
---|
3395 | int rc;
|
---|
3396 |
|
---|
3397 | num1 = math_int_from_string (argv[0]);
|
---|
3398 | num2 = math_int_from_string (argv[1]);
|
---|
3399 |
|
---|
3400 | funcname += sizeof ("int-") - 1;
|
---|
3401 | if (!strcmp (funcname, "eq"))
|
---|
3402 | rc = num1 == num2;
|
---|
3403 | else if (!strcmp (funcname, "ne"))
|
---|
3404 | rc = num1 != num2;
|
---|
3405 | else if (!strcmp (funcname, "gt"))
|
---|
3406 | rc = num1 > num2;
|
---|
3407 | else if (!strcmp (funcname, "ge"))
|
---|
3408 | rc = num1 >= num2;
|
---|
3409 | else if (!strcmp (funcname, "lt"))
|
---|
3410 | rc = num1 < num2;
|
---|
3411 | else /*if (!strcmp (funcname, "le"))*/
|
---|
3412 | rc = num1 <= num2;
|
---|
3413 |
|
---|
3414 | return variable_buffer_output (o, rc ? "1" : "", rc);
|
---|
3415 | }
|
---|
3416 |
|
---|
3417 | #endif /* CONFIG_WITH_MATH */
|
---|
3418 |
|
---|
3419 | #ifdef CONFIG_WITH_NANOTS
|
---|
3420 | /* Returns the current timestamp as nano seconds. The time
|
---|
3421 | source is a high res monotone one if the platform provides
|
---|
3422 | this (and we know about it).
|
---|
3423 |
|
---|
3424 | Tip. Use this with int-sub to profile makefile reading
|
---|
3425 | and similar. */
|
---|
3426 | static char *
|
---|
3427 | func_nanots (char *o, char **argv, const char *funcname)
|
---|
3428 | {
|
---|
3429 | math_int ts;
|
---|
3430 |
|
---|
3431 | #if defined (WINDOWS32)
|
---|
3432 | static int s_state = -1;
|
---|
3433 | static LARGE_INTEGER s_freq;
|
---|
3434 |
|
---|
3435 | if (s_state == -1)
|
---|
3436 | s_state = QueryPerformanceFrequency (&s_freq);
|
---|
3437 | if (s_state)
|
---|
3438 | {
|
---|
3439 | LARGE_INTEGER pc;
|
---|
3440 | if (!QueryPerformanceCounter (&pc))
|
---|
3441 | {
|
---|
3442 | s_state = 0;
|
---|
3443 | return func_nanots (o, argv, funcname);
|
---|
3444 | }
|
---|
3445 | ts = (math_int)((long double)pc.QuadPart / (long double)s_freq.QuadPart * 1000000000);
|
---|
3446 | }
|
---|
3447 | else
|
---|
3448 | {
|
---|
3449 | /* fall back to low resolution system time. */
|
---|
3450 | LARGE_INTEGER bigint;
|
---|
3451 | FILETIME ft = {0,0};
|
---|
3452 | GetSystemTimeAsFileTime (&ft);
|
---|
3453 | bigint.u.LowPart = ft.dwLowDateTime;
|
---|
3454 | bigint.u.HighPart = ft.dwLowDateTime;
|
---|
3455 | ts = bigint.QuadPart * 100;
|
---|
3456 | }
|
---|
3457 |
|
---|
3458 | /* FIXME: Linux and others have the realtime clock_* api, detect and use it. */
|
---|
3459 |
|
---|
3460 | #elif HAVE_GETTIMEOFDAY
|
---|
3461 | struct timeval tv;
|
---|
3462 | if (!gettimeofday (&tv, NULL))
|
---|
3463 | ts = (math_int)tv.tv_sec * 1000000000
|
---|
3464 | + tv.tv_usec * 1000;
|
---|
3465 | else
|
---|
3466 | {
|
---|
3467 | error (NILF, _("$(nanots): gettimeofday failed"));
|
---|
3468 | ts = 0;
|
---|
3469 | }
|
---|
3470 |
|
---|
3471 | #else
|
---|
3472 | # error "PORTME"
|
---|
3473 | #endif
|
---|
3474 |
|
---|
3475 | return math_int_to_variable_buffer (o, ts);
|
---|
3476 | }
|
---|
3477 | #endif
|
---|
3478 |
|
---|
3479 | #ifdef CONFIG_WITH_OS2_LIBPATH
|
---|
3480 | /* Sets or gets the OS/2 libpath variables.
|
---|
3481 |
|
---|
3482 | The first argument indicates which variable - BEGINLIBPATH,
|
---|
3483 | ENDLIBPATH, LIBPATHSTRICT or LIBPATH.
|
---|
3484 |
|
---|
3485 | The second indicates whether this is a get (not present) or
|
---|
3486 | set (present) operation. When present it is the new value for
|
---|
3487 | the variable. */
|
---|
3488 | static char *
|
---|
3489 | func_os2_libpath (char *o, char **argv, const char *funcname UNUSED)
|
---|
3490 | {
|
---|
3491 | char buf[4096];
|
---|
3492 | ULONG fVar;
|
---|
3493 | APIRET rc;
|
---|
3494 |
|
---|
3495 | /* translate variable name (first arg) */
|
---|
3496 | if (!strcmp (argv[0], "BEGINLIBPATH"))
|
---|
3497 | fVar = BEGIN_LIBPATH;
|
---|
3498 | else if (!strcmp (argv[0], "ENDLIBPATH"))
|
---|
3499 | fVar = END_LIBPATH;
|
---|
3500 | else if (!strcmp (argv[0], "LIBPATHSTRICT"))
|
---|
3501 | fVar = LIBPATHSTRICT;
|
---|
3502 | else if (!strcmp (argv[0], "LIBPATH"))
|
---|
3503 | fVar = 0;
|
---|
3504 | else
|
---|
3505 | {
|
---|
3506 | error (NILF, _("$(libpath): unknown variable `%s'"), argv[0]);
|
---|
3507 | return variable_buffer_output (o, "", 0);
|
---|
3508 | }
|
---|
3509 |
|
---|
3510 | if (!argv[1])
|
---|
3511 | {
|
---|
3512 | /* get the variable value. */
|
---|
3513 | if (fVar != 0)
|
---|
3514 | {
|
---|
3515 | buf[0] = buf[1] = buf[2] = buf[3] = '\0';
|
---|
3516 | rc = DosQueryExtLIBPATH (buf, fVar);
|
---|
3517 | }
|
---|
3518 | else
|
---|
3519 | rc = DosQueryHeaderInfo (NULLHANDLE, 0, buf, sizeof(buf), QHINF_LIBPATH);
|
---|
3520 | if (rc != NO_ERROR)
|
---|
3521 | {
|
---|
3522 | error (NILF, _("$(libpath): failed to query `%s', rc=%d"), argv[0], rc);
|
---|
3523 | return variable_buffer_output (o, "", 0);
|
---|
3524 | }
|
---|
3525 | o = variable_buffer_output (o, buf, strlen (buf));
|
---|
3526 | }
|
---|
3527 | else
|
---|
3528 | {
|
---|
3529 | /* set the variable value. */
|
---|
3530 | size_t len;
|
---|
3531 | size_t len_max = sizeof (buf) < 2048 ? sizeof (buf) : 2048;
|
---|
3532 | const char *val;
|
---|
3533 | const char *end;
|
---|
3534 |
|
---|
3535 | if (fVar == 0)
|
---|
3536 | {
|
---|
3537 | error (NILF, _("$(libpath): LIBPATH is read-only"));
|
---|
3538 | return variable_buffer_output (o, "", 0);
|
---|
3539 | }
|
---|
3540 |
|
---|
3541 | /* strip leading and trailing spaces and check for max length. */
|
---|
3542 | val = argv[1];
|
---|
3543 | while (isspace (*val))
|
---|
3544 | val++;
|
---|
3545 | end = strchr (val, '\0');
|
---|
3546 | while (end > val && isspace (end[-1]))
|
---|
3547 | end--;
|
---|
3548 |
|
---|
3549 | len = end - val;
|
---|
3550 | if (len >= len_max)
|
---|
3551 | {
|
---|
3552 | error (NILF, _("$(libpath): The new `%s' value is too long (%d bytes, max %d)"),
|
---|
3553 | argv[0], len, len_max);
|
---|
3554 | return variable_buffer_output (o, "", 0);
|
---|
3555 | }
|
---|
3556 |
|
---|
3557 | /* make a stripped copy in low memory and try set it. */
|
---|
3558 | memcpy (buf, val, len);
|
---|
3559 | buf[len] = '\0';
|
---|
3560 | rc = DosSetExtLIBPATH (buf, fVar);
|
---|
3561 | if (rc != NO_ERROR)
|
---|
3562 | {
|
---|
3563 | error (NILF, _("$(libpath): failed to set `%s' to `%s', rc=%d"), argv[0], buf, rc);
|
---|
3564 | return variable_buffer_output (o, "", 0);
|
---|
3565 | }
|
---|
3566 |
|
---|
3567 | o = variable_buffer_output (o, "", 0);
|
---|
3568 | }
|
---|
3569 | return o;
|
---|
3570 | }
|
---|
3571 | #endif /* CONFIG_WITH_OS2_LIBPATH */
|
---|
3572 |
|
---|
3573 | #ifdef CONFIG_WITH_MAKE_STATS
|
---|
3574 | /* Retrieve make statistics. */
|
---|
3575 | static char *
|
---|
3576 | func_make_stats (char *o, char **argv, const char *funcname UNUSED)
|
---|
3577 | {
|
---|
3578 | char buf[512];
|
---|
3579 | int len;
|
---|
3580 |
|
---|
3581 | if (!argv[0] || (!argv[0][0] && !argv[1]))
|
---|
3582 | {
|
---|
3583 | len = sprintf (buf, "alloc-cur: %5lu %6luKB (/%3luMB) hash: %5lu %2lu%%",
|
---|
3584 | make_stats_allocations,
|
---|
3585 | make_stats_allocated / 1024,
|
---|
3586 | make_stats_allocated_sum / (1024*1024),
|
---|
3587 | make_stats_ht_lookups,
|
---|
3588 | (make_stats_ht_collisions * 100) / make_stats_ht_lookups);
|
---|
3589 | o = variable_buffer_output (o, buf, len);
|
---|
3590 | }
|
---|
3591 | else
|
---|
3592 | {
|
---|
3593 | /* selective */
|
---|
3594 | int i;
|
---|
3595 | for (i = 0; argv[i]; i++)
|
---|
3596 | {
|
---|
3597 | unsigned long val;
|
---|
3598 | if (i != 0)
|
---|
3599 | o = variable_buffer_output (o, " ", 1);
|
---|
3600 | if (!strcmp(argv[i], "allocations"))
|
---|
3601 | val = make_stats_allocations;
|
---|
3602 | else if (!strcmp(argv[i], "allocated"))
|
---|
3603 | val = make_stats_allocated;
|
---|
3604 | else if (!strcmp(argv[i], "allocated_sum"))
|
---|
3605 | val = make_stats_allocated_sum;
|
---|
3606 | else if (!strcmp(argv[i], "ht_lookups"))
|
---|
3607 | val = make_stats_ht_lookups;
|
---|
3608 | else if (!strcmp(argv[i], "ht_collisions"))
|
---|
3609 | val = make_stats_ht_collisions;
|
---|
3610 | else if (!strcmp(argv[i], "ht_collisions_pct"))
|
---|
3611 | val = (make_stats_ht_collisions * 100) / make_stats_ht_lookups;
|
---|
3612 | else
|
---|
3613 | {
|
---|
3614 | o = variable_buffer_output (o, argv[i], strlen (argv[i]));
|
---|
3615 | continue;
|
---|
3616 | }
|
---|
3617 |
|
---|
3618 | len = sprintf (buf, "%ld", val);
|
---|
3619 | o = variable_buffer_output (o, buf, len);
|
---|
3620 | }
|
---|
3621 | }
|
---|
3622 | return o;
|
---|
3623 | }
|
---|
3624 | #endif /* CONFIG_WITH_MAKE_STATS */
|
---|
3625 |
|
---|
3626 | #ifdef CONFIG_WITH_COMMANDS_FUNC
|
---|
3627 | /* Gets all the commands for a target, separated by newlines.
|
---|
3628 |
|
---|
3629 | This is useful when creating and checking target dependencies since
|
---|
3630 | it reduces the amount of work and the memory consuption. A new prefix
|
---|
3631 | character '%' has been introduced for skipping certain lines, like
|
---|
3632 | for instance the one calling this function and pushing to a dep file.
|
---|
3633 | Blank lines are also skipped.
|
---|
3634 |
|
---|
3635 | The commands function takes exactly one argument, which is the name of
|
---|
3636 | the target which commands should be returned.
|
---|
3637 |
|
---|
3638 | The commands-sc is identical to commands except that it uses a ';' to
|
---|
3639 | separate the commands.
|
---|
3640 |
|
---|
3641 | The commands-usr is similar to commands except that it takes a 2nd
|
---|
3642 | argument that is used to separate the commands. */
|
---|
3643 | char *
|
---|
3644 | func_commands (char *o, char **argv, const char *funcname)
|
---|
3645 | {
|
---|
3646 | struct file *file;
|
---|
3647 | static int recursive = 0;
|
---|
3648 |
|
---|
3649 | if (recursive)
|
---|
3650 | return variable_buffer_output (o, "recursive", sizeof ("recursive") - 1);
|
---|
3651 | recursive = 1;
|
---|
3652 |
|
---|
3653 | file = lookup_file (argv[0]);
|
---|
3654 | if (file)
|
---|
3655 | {
|
---|
3656 | int i, cmd_sep_len;
|
---|
3657 | struct commands *cmds = file->cmds;
|
---|
3658 | const char *cmd_sep;
|
---|
3659 |
|
---|
3660 | if (!strcmp (funcname, "commands"))
|
---|
3661 | {
|
---|
3662 | cmd_sep = "\n";
|
---|
3663 | cmd_sep_len = 1;
|
---|
3664 | }
|
---|
3665 | else if (!strcmp (funcname, "commands-sc"))
|
---|
3666 | {
|
---|
3667 | cmd_sep = ";";
|
---|
3668 | cmd_sep_len = 1;
|
---|
3669 | }
|
---|
3670 | else /*if (!strcmp (funcname, "commands-usr"))*/
|
---|
3671 | {
|
---|
3672 | cmd_sep = argv[1];
|
---|
3673 | cmd_sep_len = strlen (cmd_sep);
|
---|
3674 | }
|
---|
3675 |
|
---|
3676 | initialize_file_variables (file, 1 /* reading - FIXME: we don't know? */);
|
---|
3677 | set_file_variables (file);
|
---|
3678 | chop_commands (cmds);
|
---|
3679 |
|
---|
3680 | for (i = 0; i < cmds->ncommand_lines; i++)
|
---|
3681 | {
|
---|
3682 | char *p;
|
---|
3683 | char *in, *out, *ref;
|
---|
3684 |
|
---|
3685 | /* Skip it if it has a '%' prefix or is blank. */
|
---|
3686 | if (cmds->lines_flags[i] & COMMAND_GETTER_SKIP_IT)
|
---|
3687 | continue;
|
---|
3688 | p = cmds->command_lines[i];
|
---|
3689 | while (isblank ((unsigned char)*p))
|
---|
3690 | p++;
|
---|
3691 | if (*p == '\0')
|
---|
3692 | continue;
|
---|
3693 |
|
---|
3694 | /* --- copied from new_job() in job.c --- */
|
---|
3695 |
|
---|
3696 | /* Collapse backslash-newline combinations that are inside variable
|
---|
3697 | or function references. These are left alone by the parser so
|
---|
3698 | that they will appear in the echoing of commands (where they look
|
---|
3699 | nice); and collapsed by construct_command_argv when it tokenizes.
|
---|
3700 | But letting them survive inside function invocations loses because
|
---|
3701 | we don't want the functions to see them as part of the text. */
|
---|
3702 |
|
---|
3703 | /* IN points to where in the line we are scanning.
|
---|
3704 | OUT points to where in the line we are writing.
|
---|
3705 | When we collapse a backslash-newline combination,
|
---|
3706 | IN gets ahead of OUT. */
|
---|
3707 |
|
---|
3708 | in = out = p;
|
---|
3709 | while ((ref = strchr (in, '$')) != 0)
|
---|
3710 | {
|
---|
3711 | ++ref; /* Move past the $. */
|
---|
3712 |
|
---|
3713 | if (out != in)
|
---|
3714 | /* Copy the text between the end of the last chunk
|
---|
3715 | we processed (where IN points) and the new chunk
|
---|
3716 | we are about to process (where REF points). */
|
---|
3717 | memmove (out, in, ref - in);
|
---|
3718 |
|
---|
3719 | /* Move both pointers past the boring stuff. */
|
---|
3720 | out += ref - in;
|
---|
3721 | in = ref;
|
---|
3722 |
|
---|
3723 | if (*ref == '(' || *ref == '{')
|
---|
3724 | {
|
---|
3725 | char openparen = *ref;
|
---|
3726 | char closeparen = openparen == '(' ? ')' : '}';
|
---|
3727 | int count;
|
---|
3728 | char *p;
|
---|
3729 |
|
---|
3730 | *out++ = *in++; /* Copy OPENPAREN. */
|
---|
3731 | /* IN now points past the opening paren or brace.
|
---|
3732 | Count parens or braces until it is matched. */
|
---|
3733 | count = 0;
|
---|
3734 | while (*in != '\0')
|
---|
3735 | {
|
---|
3736 | if (*in == closeparen && --count < 0)
|
---|
3737 | break;
|
---|
3738 | else if (*in == '\\' && in[1] == '\n')
|
---|
3739 | {
|
---|
3740 | /* We have found a backslash-newline inside a
|
---|
3741 | variable or function reference. Eat it and
|
---|
3742 | any following whitespace. */
|
---|
3743 |
|
---|
3744 | int quoted = 0;
|
---|
3745 | for (p = in - 1; p > ref && *p == '\\'; --p)
|
---|
3746 | quoted = !quoted;
|
---|
3747 |
|
---|
3748 | if (quoted)
|
---|
3749 | /* There were two or more backslashes, so this is
|
---|
3750 | not really a continuation line. We don't collapse
|
---|
3751 | the quoting backslashes here as is done in
|
---|
3752 | collapse_continuations, because the line will
|
---|
3753 | be collapsed again after expansion. */
|
---|
3754 | *out++ = *in++;
|
---|
3755 | else
|
---|
3756 | {
|
---|
3757 | /* Skip the backslash, newline and
|
---|
3758 | any following whitespace. */
|
---|
3759 | in = next_token (in + 2);
|
---|
3760 |
|
---|
3761 | /* Discard any preceding whitespace that has
|
---|
3762 | already been written to the output. */
|
---|
3763 | while (out > ref
|
---|
3764 | && isblank ((unsigned char)out[-1]))
|
---|
3765 | --out;
|
---|
3766 |
|
---|
3767 | /* Replace it all with a single space. */
|
---|
3768 | *out++ = ' ';
|
---|
3769 | }
|
---|
3770 | }
|
---|
3771 | else
|
---|
3772 | {
|
---|
3773 | if (*in == openparen)
|
---|
3774 | ++count;
|
---|
3775 |
|
---|
3776 | *out++ = *in++;
|
---|
3777 | }
|
---|
3778 | }
|
---|
3779 | }
|
---|
3780 | }
|
---|
3781 |
|
---|
3782 | /* There are no more references in this line to worry about.
|
---|
3783 | Copy the remaining uninteresting text to the output. */
|
---|
3784 | if (out != in)
|
---|
3785 | strcpy (out, in);
|
---|
3786 |
|
---|
3787 | /* --- copied from new_job() in job.c --- */
|
---|
3788 |
|
---|
3789 | /* Finally, expand the line. */
|
---|
3790 | if (i)
|
---|
3791 | o = variable_buffer_output (o, cmd_sep, cmd_sep_len);
|
---|
3792 | o = variable_expand_for_file_2 (o, cmds->command_lines[i], file);
|
---|
3793 |
|
---|
3794 | /* Skip it if it has a '%' prefix or is blank. */
|
---|
3795 | p = o;
|
---|
3796 | while (isblank ((unsigned char)*o)
|
---|
3797 | || *o == '@'
|
---|
3798 | || *o == '-'
|
---|
3799 | || *o == '+')
|
---|
3800 | o++;
|
---|
3801 | if (*o != '\0' && *o != '%')
|
---|
3802 | o = strchr (o, '\0');
|
---|
3803 | else if (i)
|
---|
3804 | o = p - cmd_sep_len;
|
---|
3805 | else
|
---|
3806 | o = p;
|
---|
3807 | }
|
---|
3808 | }
|
---|
3809 | /* else FIXME: bitch about it? */
|
---|
3810 |
|
---|
3811 | recursive = 0;
|
---|
3812 | return o;
|
---|
3813 | }
|
---|
3814 | #endif /* CONFIG_WITH_COMMANDS_FUNC */
|
---|
3815 |
|
---|
3816 | /* Lookup table for builtin functions.
|
---|
3817 |
|
---|
3818 | This doesn't have to be sorted; we use a straight lookup. We might gain
|
---|
3819 | some efficiency by moving most often used functions to the start of the
|
---|
3820 | table.
|
---|
3821 |
|
---|
3822 | If MAXIMUM_ARGS is 0, that means there is no maximum and all
|
---|
3823 | comma-separated values are treated as arguments.
|
---|
3824 |
|
---|
3825 | EXPAND_ARGS means that all arguments should be expanded before invocation.
|
---|
3826 | Functions that do namespace tricks (foreach) don't automatically expand. */
|
---|
3827 |
|
---|
3828 | static char *func_call (char *o, char **argv, const char *funcname);
|
---|
3829 |
|
---|
3830 |
|
---|
3831 | static struct function_table_entry function_table_init[] =
|
---|
3832 | {
|
---|
3833 | /* Name/size */ /* MIN MAX EXP? Function */
|
---|
3834 | { STRING_SIZE_TUPLE("abspath"), 0, 1, 1, func_abspath},
|
---|
3835 | { STRING_SIZE_TUPLE("addprefix"), 2, 2, 1, func_addsuffix_addprefix},
|
---|
3836 | { STRING_SIZE_TUPLE("addsuffix"), 2, 2, 1, func_addsuffix_addprefix},
|
---|
3837 | { STRING_SIZE_TUPLE("basename"), 0, 1, 1, func_basename_dir},
|
---|
3838 | { STRING_SIZE_TUPLE("dir"), 0, 1, 1, func_basename_dir},
|
---|
3839 | { STRING_SIZE_TUPLE("notdir"), 0, 1, 1, func_notdir_suffix},
|
---|
3840 | { STRING_SIZE_TUPLE("subst"), 3, 3, 1, func_subst},
|
---|
3841 | { STRING_SIZE_TUPLE("suffix"), 0, 1, 1, func_notdir_suffix},
|
---|
3842 | { STRING_SIZE_TUPLE("filter"), 2, 2, 1, func_filter_filterout},
|
---|
3843 | { STRING_SIZE_TUPLE("filter-out"), 2, 2, 1, func_filter_filterout},
|
---|
3844 | { STRING_SIZE_TUPLE("findstring"), 2, 2, 1, func_findstring},
|
---|
3845 | { STRING_SIZE_TUPLE("firstword"), 0, 1, 1, func_firstword},
|
---|
3846 | { STRING_SIZE_TUPLE("flavor"), 0, 1, 1, func_flavor},
|
---|
3847 | { STRING_SIZE_TUPLE("join"), 2, 2, 1, func_join},
|
---|
3848 | { STRING_SIZE_TUPLE("lastword"), 0, 1, 1, func_lastword},
|
---|
3849 | { STRING_SIZE_TUPLE("patsubst"), 3, 3, 1, func_patsubst},
|
---|
3850 | { STRING_SIZE_TUPLE("realpath"), 0, 1, 1, func_realpath},
|
---|
3851 | #ifdef CONFIG_WITH_RSORT
|
---|
3852 | { STRING_SIZE_TUPLE("rsort"), 0, 1, 1, func_sort},
|
---|
3853 | #endif
|
---|
3854 | { STRING_SIZE_TUPLE("shell"), 0, 1, 1, func_shell},
|
---|
3855 | { STRING_SIZE_TUPLE("sort"), 0, 1, 1, func_sort},
|
---|
3856 | { STRING_SIZE_TUPLE("strip"), 0, 1, 1, func_strip},
|
---|
3857 | { STRING_SIZE_TUPLE("wildcard"), 0, 1, 1, func_wildcard},
|
---|
3858 | { STRING_SIZE_TUPLE("word"), 2, 2, 1, func_word},
|
---|
3859 | { STRING_SIZE_TUPLE("wordlist"), 3, 3, 1, func_wordlist},
|
---|
3860 | { STRING_SIZE_TUPLE("words"), 0, 1, 1, func_words},
|
---|
3861 | { STRING_SIZE_TUPLE("origin"), 0, 1, 1, func_origin},
|
---|
3862 | { STRING_SIZE_TUPLE("foreach"), 3, 3, 0, func_foreach},
|
---|
3863 | { STRING_SIZE_TUPLE("call"), 1, 0, 1, func_call},
|
---|
3864 | { STRING_SIZE_TUPLE("info"), 0, 1, 1, func_error},
|
---|
3865 | { STRING_SIZE_TUPLE("error"), 0, 1, 1, func_error},
|
---|
3866 | { STRING_SIZE_TUPLE("warning"), 0, 1, 1, func_error},
|
---|
3867 | { STRING_SIZE_TUPLE("if"), 2, 3, 0, func_if},
|
---|
3868 | { STRING_SIZE_TUPLE("or"), 1, 0, 0, func_or},
|
---|
3869 | { STRING_SIZE_TUPLE("and"), 1, 0, 0, func_and},
|
---|
3870 | { STRING_SIZE_TUPLE("value"), 0, 1, 1, func_value},
|
---|
3871 | { STRING_SIZE_TUPLE("eval"), 0, 1, 1, func_eval},
|
---|
3872 | #ifdef CONFIG_WITH_EVALPLUS
|
---|
3873 | { STRING_SIZE_TUPLE("evalctx"), 0, 1, 1, func_evalctx},
|
---|
3874 | { STRING_SIZE_TUPLE("evalval"), 1, 1, 1, func_evalval},
|
---|
3875 | { STRING_SIZE_TUPLE("evalvalctx"), 1, 1, 1, func_evalval},
|
---|
3876 | { STRING_SIZE_TUPLE("evalcall"), 1, 0, 1, func_call},
|
---|
3877 | { STRING_SIZE_TUPLE("evalcall2"), 1, 0, 1, func_call},
|
---|
3878 | #endif
|
---|
3879 | #ifdef EXPERIMENTAL
|
---|
3880 | { STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq},
|
---|
3881 | { STRING_SIZE_TUPLE("not"), 0, 1, 1, func_not},
|
---|
3882 | #endif
|
---|
3883 | #ifdef CONFIG_WITH_TOUPPER_TOLOWER
|
---|
3884 | { STRING_SIZE_TUPLE("toupper"), 0, 1, 1, func_toupper_tolower},
|
---|
3885 | { STRING_SIZE_TUPLE("tolower"), 0, 1, 1, func_toupper_tolower},
|
---|
3886 | #endif
|
---|
3887 | #ifdef CONFIG_WITH_ABSPATHEX
|
---|
3888 | { STRING_SIZE_TUPLE("abspathex"), 0, 2, 1, func_abspathex},
|
---|
3889 | #endif
|
---|
3890 | #ifdef CONFIG_WITH_XARGS
|
---|
3891 | { STRING_SIZE_TUPLE("xargs"), 2, 0, 1, func_xargs},
|
---|
3892 | #endif
|
---|
3893 | #if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
|
---|
3894 | { STRING_SIZE_TUPLE("comp-vars"), 3, 3, 1, func_comp_vars},
|
---|
3895 | { STRING_SIZE_TUPLE("comp-cmds"), 3, 3, 1, func_comp_vars},
|
---|
3896 | { STRING_SIZE_TUPLE("comp-cmds-ex"), 3, 3, 1, func_comp_cmds_ex},
|
---|
3897 | #endif
|
---|
3898 | #ifdef CONFIG_WITH_DATE
|
---|
3899 | { STRING_SIZE_TUPLE("date"), 0, 1, 1, func_date},
|
---|
3900 | { STRING_SIZE_TUPLE("date-utc"), 0, 3, 1, func_date},
|
---|
3901 | #endif
|
---|
3902 | #ifdef CONFIG_WITH_FILE_SIZE
|
---|
3903 | { STRING_SIZE_TUPLE("file-size"), 1, 1, 1, func_file_size},
|
---|
3904 | #endif
|
---|
3905 | #ifdef CONFIG_WITH_WHICH
|
---|
3906 | { STRING_SIZE_TUPLE("which"), 0, 0, 1, func_which},
|
---|
3907 | #endif
|
---|
3908 | #ifdef CONFIG_WITH_STACK
|
---|
3909 | { STRING_SIZE_TUPLE("stack-push"), 2, 2, 1, func_stack_push},
|
---|
3910 | { STRING_SIZE_TUPLE("stack-pop"), 1, 1, 1, func_stack_pop_top},
|
---|
3911 | { STRING_SIZE_TUPLE("stack-popv"), 1, 1, 1, func_stack_pop_top},
|
---|
3912 | { STRING_SIZE_TUPLE("stack-top"), 1, 1, 1, func_stack_pop_top},
|
---|
3913 | #endif
|
---|
3914 | #ifdef CONFIG_WITH_MATH
|
---|
3915 | { STRING_SIZE_TUPLE("int-add"), 2, 0, 1, func_int_add},
|
---|
3916 | { STRING_SIZE_TUPLE("int-sub"), 2, 0, 1, func_int_sub},
|
---|
3917 | { STRING_SIZE_TUPLE("int-mul"), 2, 0, 1, func_int_mul},
|
---|
3918 | { STRING_SIZE_TUPLE("int-div"), 2, 0, 1, func_int_div},
|
---|
3919 | { STRING_SIZE_TUPLE("int-mod"), 2, 2, 1, func_int_mod},
|
---|
3920 | { STRING_SIZE_TUPLE("int-not"), 1, 1, 1, func_int_not},
|
---|
3921 | { STRING_SIZE_TUPLE("int-and"), 2, 0, 1, func_int_and},
|
---|
3922 | { STRING_SIZE_TUPLE("int-or"), 2, 0, 1, func_int_or},
|
---|
3923 | { STRING_SIZE_TUPLE("int-xor"), 2, 0, 1, func_int_xor},
|
---|
3924 | { STRING_SIZE_TUPLE("int-eq"), 2, 2, 1, func_int_cmp},
|
---|
3925 | { STRING_SIZE_TUPLE("int-ne"), 2, 2, 1, func_int_cmp},
|
---|
3926 | { STRING_SIZE_TUPLE("int-gt"), 2, 2, 1, func_int_cmp},
|
---|
3927 | { STRING_SIZE_TUPLE("int-ge"), 2, 2, 1, func_int_cmp},
|
---|
3928 | { STRING_SIZE_TUPLE("int-lt"), 2, 2, 1, func_int_cmp},
|
---|
3929 | { STRING_SIZE_TUPLE("int-le"), 2, 2, 1, func_int_cmp},
|
---|
3930 | #endif
|
---|
3931 | #ifdef CONFIG_WITH_NANOTS
|
---|
3932 | { STRING_SIZE_TUPLE("nanots"), 0, 0, 0, func_nanots},
|
---|
3933 | #endif
|
---|
3934 | #ifdef CONFIG_WITH_OS2_LIBPATH
|
---|
3935 | { STRING_SIZE_TUPLE("libpath"), 1, 2, 1, func_os2_libpath},
|
---|
3936 | #endif
|
---|
3937 | #ifdef CONFIG_WITH_MAKE_STATS
|
---|
3938 | { STRING_SIZE_TUPLE("make-stats"), 0, 0, 0, func_make_stats},
|
---|
3939 | #endif
|
---|
3940 | #ifdef CONFIG_WITH_COMMANDS_FUNC
|
---|
3941 | { STRING_SIZE_TUPLE("commands"), 1, 1, 1, func_commands},
|
---|
3942 | { STRING_SIZE_TUPLE("commands-sc"), 1, 1, 1, func_commands},
|
---|
3943 | { STRING_SIZE_TUPLE("commands-usr"), 2, 2, 1, func_commands},
|
---|
3944 | #endif
|
---|
3945 | #ifdef KMK_HELPERS
|
---|
3946 | { STRING_SIZE_TUPLE("kb-src-tool"), 1, 1, 0, func_kbuild_source_tool},
|
---|
3947 | { STRING_SIZE_TUPLE("kb-obj-base"), 1, 1, 0, func_kbuild_object_base},
|
---|
3948 | { STRING_SIZE_TUPLE("kb-obj-suff"), 1, 1, 0, func_kbuild_object_suffix},
|
---|
3949 | { STRING_SIZE_TUPLE("kb-src-prop"), 4, 4, 0, func_kbuild_source_prop},
|
---|
3950 | { STRING_SIZE_TUPLE("kb-src-one"), 0, 1, 0, func_kbuild_source_one},
|
---|
3951 | #endif
|
---|
3952 | };
|
---|
3953 |
|
---|
3954 | #define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry))
|
---|
3955 | |
---|
3956 |
|
---|
3957 |
|
---|
3958 | /* These must come after the definition of function_table. */
|
---|
3959 |
|
---|
3960 | static char *
|
---|
3961 | expand_builtin_function (char *o, int argc, char **argv,
|
---|
3962 | const struct function_table_entry *entry_p)
|
---|
3963 | {
|
---|
3964 | if (argc < (int)entry_p->minimum_args)
|
---|
3965 | fatal (*expanding_var,
|
---|
3966 | _("insufficient number of arguments (%d) to function `%s'"),
|
---|
3967 | argc, entry_p->name);
|
---|
3968 |
|
---|
3969 | /* I suppose technically some function could do something with no
|
---|
3970 | arguments, but so far none do, so just test it for all functions here
|
---|
3971 | rather than in each one. We can change it later if necessary. */
|
---|
3972 |
|
---|
3973 | if (!argc)
|
---|
3974 | return o;
|
---|
3975 |
|
---|
3976 | if (!entry_p->func_ptr)
|
---|
3977 | fatal (*expanding_var,
|
---|
3978 | _("unimplemented on this platform: function `%s'"), entry_p->name);
|
---|
3979 |
|
---|
3980 | return entry_p->func_ptr (o, argv, entry_p->name);
|
---|
3981 | }
|
---|
3982 |
|
---|
3983 | /* Check for a function invocation in *STRINGP. *STRINGP points at the
|
---|
3984 | opening ( or { and is not null-terminated. If a function invocation
|
---|
3985 | is found, expand it into the buffer at *OP, updating *OP, incrementing
|
---|
3986 | *STRINGP past the reference and returning nonzero. If not, return zero. */
|
---|
3987 |
|
---|
3988 | static int
|
---|
3989 | handle_function2 (const struct function_table_entry *entry_p, char **op, const char **stringp) /* bird split it up. */
|
---|
3990 | {
|
---|
3991 | char openparen = (*stringp)[0];
|
---|
3992 | char closeparen = openparen == '(' ? ')' : '}';
|
---|
3993 | const char *beg;
|
---|
3994 | const char *end;
|
---|
3995 | int count = 0;
|
---|
3996 | char *abeg = NULL;
|
---|
3997 | char **argv, **argvp;
|
---|
3998 | int nargs;
|
---|
3999 |
|
---|
4000 | beg = *stringp + 1;
|
---|
4001 |
|
---|
4002 | /* We found a builtin function. Find the beginning of its arguments (skip
|
---|
4003 | whitespace after the name). */
|
---|
4004 |
|
---|
4005 | beg = next_token (beg + entry_p->len);
|
---|
4006 |
|
---|
4007 | /* Find the end of the function invocation, counting nested use of
|
---|
4008 | whichever kind of parens we use. Since we're looking, count commas
|
---|
4009 | to get a rough estimate of how many arguments we might have. The
|
---|
4010 | count might be high, but it'll never be low. */
|
---|
4011 |
|
---|
4012 | for (nargs=1, end=beg; *end != '\0'; ++end)
|
---|
4013 | if (*end == ',')
|
---|
4014 | ++nargs;
|
---|
4015 | else if (*end == openparen)
|
---|
4016 | ++count;
|
---|
4017 | else if (*end == closeparen && --count < 0)
|
---|
4018 | break;
|
---|
4019 |
|
---|
4020 | if (count >= 0)
|
---|
4021 | fatal (*expanding_var,
|
---|
4022 | _("unterminated call to function `%s': missing `%c'"),
|
---|
4023 | entry_p->name, closeparen);
|
---|
4024 |
|
---|
4025 | *stringp = end;
|
---|
4026 |
|
---|
4027 | /* Get some memory to store the arg pointers. */
|
---|
4028 | argvp = argv = alloca (sizeof (char *) * (nargs + 2));
|
---|
4029 |
|
---|
4030 | /* Chop the string into arguments, then a nul. As soon as we hit
|
---|
4031 | MAXIMUM_ARGS (if it's >0) assume the rest of the string is part of the
|
---|
4032 | last argument.
|
---|
4033 |
|
---|
4034 | If we're expanding, store pointers to the expansion of each one. If
|
---|
4035 | not, make a duplicate of the string and point into that, nul-terminating
|
---|
4036 | each argument. */
|
---|
4037 |
|
---|
4038 | if (entry_p->expand_args)
|
---|
4039 | {
|
---|
4040 | const char *p;
|
---|
4041 | for (p=beg, nargs=0; p <= end; ++argvp)
|
---|
4042 | {
|
---|
4043 | const char *next;
|
---|
4044 |
|
---|
4045 | ++nargs;
|
---|
4046 |
|
---|
4047 | if (nargs == entry_p->maximum_args
|
---|
4048 | || (! (next = find_next_argument (openparen, closeparen, p, end))))
|
---|
4049 | next = end;
|
---|
4050 |
|
---|
4051 | *argvp = expand_argument (p, next);
|
---|
4052 | p = next + 1;
|
---|
4053 | }
|
---|
4054 | }
|
---|
4055 | else
|
---|
4056 | {
|
---|
4057 | int len = end - beg;
|
---|
4058 | char *p, *aend;
|
---|
4059 |
|
---|
4060 | abeg = xmalloc (len+1);
|
---|
4061 | memcpy (abeg, beg, len);
|
---|
4062 | abeg[len] = '\0';
|
---|
4063 | aend = abeg + len;
|
---|
4064 |
|
---|
4065 | for (p=abeg, nargs=0; p <= aend; ++argvp)
|
---|
4066 | {
|
---|
4067 | char *next;
|
---|
4068 |
|
---|
4069 | ++nargs;
|
---|
4070 |
|
---|
4071 | if (nargs == entry_p->maximum_args
|
---|
4072 | || (! (next = find_next_argument (openparen, closeparen, p, aend))))
|
---|
4073 | next = aend;
|
---|
4074 |
|
---|
4075 | *argvp = p;
|
---|
4076 | *next = '\0';
|
---|
4077 | p = next + 1;
|
---|
4078 | }
|
---|
4079 | }
|
---|
4080 | *argvp = NULL;
|
---|
4081 |
|
---|
4082 | /* Finally! Run the function... */
|
---|
4083 | *op = expand_builtin_function (*op, nargs, argv, entry_p);
|
---|
4084 |
|
---|
4085 | /* Free memory. */
|
---|
4086 | if (entry_p->expand_args)
|
---|
4087 | for (argvp=argv; *argvp != 0; ++argvp)
|
---|
4088 | free (*argvp);
|
---|
4089 | if (abeg)
|
---|
4090 | free (abeg);
|
---|
4091 |
|
---|
4092 | return 1;
|
---|
4093 | }
|
---|
4094 |
|
---|
4095 | int
|
---|
4096 | handle_function (char **op, const char **stringp) /* bird split it up */
|
---|
4097 | {
|
---|
4098 | const struct function_table_entry *entry_p = lookup_function (*stringp + 1);
|
---|
4099 | if (!entry_p)
|
---|
4100 | return 0;
|
---|
4101 | return handle_function2 (entry_p, op, stringp);
|
---|
4102 | }
|
---|
4103 | |
---|
4104 |
|
---|
4105 |
|
---|
4106 | /* User-defined functions. Expand the first argument as either a builtin
|
---|
4107 | function or a make variable, in the context of the rest of the arguments
|
---|
4108 | assigned to $1, $2, ... $N. $0 is the name of the function. */
|
---|
4109 |
|
---|
4110 | static char *
|
---|
4111 | func_call (char *o, char **argv, const char *funcname UNUSED)
|
---|
4112 | {
|
---|
4113 | static int max_args = 0;
|
---|
4114 | char *fname;
|
---|
4115 | char *cp;
|
---|
4116 | char *body;
|
---|
4117 | int flen;
|
---|
4118 | int i;
|
---|
4119 | int saved_args;
|
---|
4120 | const struct function_table_entry *entry_p;
|
---|
4121 | struct variable *v;
|
---|
4122 | #ifdef CONFIG_WITH_EVALPLUS
|
---|
4123 | char *buf;
|
---|
4124 | unsigned int len;
|
---|
4125 | #endif
|
---|
4126 |
|
---|
4127 | /* There is no way to define a variable with a space in the name, so strip
|
---|
4128 | leading and trailing whitespace as a favor to the user. */
|
---|
4129 | fname = argv[0];
|
---|
4130 | while (*fname != '\0' && isspace ((unsigned char)*fname))
|
---|
4131 | ++fname;
|
---|
4132 |
|
---|
4133 | cp = fname + strlen (fname) - 1;
|
---|
4134 | while (cp > fname && isspace ((unsigned char)*cp))
|
---|
4135 | --cp;
|
---|
4136 | cp[1] = '\0';
|
---|
4137 |
|
---|
4138 | /* Calling nothing is a no-op */
|
---|
4139 | if (*fname == '\0')
|
---|
4140 | return o;
|
---|
4141 |
|
---|
4142 | /* Are we invoking a builtin function? */
|
---|
4143 |
|
---|
4144 | entry_p = lookup_function (fname);
|
---|
4145 | if (entry_p)
|
---|
4146 | {
|
---|
4147 | /* How many arguments do we have? */
|
---|
4148 | for (i=0; argv[i+1]; ++i)
|
---|
4149 | ;
|
---|
4150 | return expand_builtin_function (o, i, argv+1, entry_p);
|
---|
4151 | }
|
---|
4152 |
|
---|
4153 | /* Not a builtin, so the first argument is the name of a variable to be
|
---|
4154 | expanded and interpreted as a function. Find it. */
|
---|
4155 | flen = strlen (fname);
|
---|
4156 |
|
---|
4157 | v = lookup_variable (fname, flen);
|
---|
4158 |
|
---|
4159 | if (v == 0)
|
---|
4160 | warn_undefined (fname, flen);
|
---|
4161 |
|
---|
4162 | if (v == 0 || *v->value == '\0')
|
---|
4163 | return o;
|
---|
4164 |
|
---|
4165 | body = alloca (flen + 4);
|
---|
4166 | body[0] = '$';
|
---|
4167 | body[1] = '(';
|
---|
4168 | memcpy (body + 2, fname, flen);
|
---|
4169 | body[flen+2] = ')';
|
---|
4170 | body[flen+3] = '\0';
|
---|
4171 |
|
---|
4172 | /* Set up arguments $(1) .. $(N). $(0) is the function name. */
|
---|
4173 |
|
---|
4174 | push_new_variable_scope ();
|
---|
4175 |
|
---|
4176 | for (i=0; *argv; ++i, ++argv)
|
---|
4177 | {
|
---|
4178 | char num[11];
|
---|
4179 |
|
---|
4180 | sprintf (num, "%d", i);
|
---|
4181 | define_variable (num, strlen (num), *argv, o_automatic, 0);
|
---|
4182 | }
|
---|
4183 |
|
---|
4184 | /* If the number of arguments we have is < max_args, it means we're inside
|
---|
4185 | a recursive invocation of $(call ...). Fill in the remaining arguments
|
---|
4186 | in the new scope with the empty value, to hide them from this
|
---|
4187 | invocation. */
|
---|
4188 |
|
---|
4189 | for (; i < max_args; ++i)
|
---|
4190 | {
|
---|
4191 | char num[11];
|
---|
4192 |
|
---|
4193 | sprintf (num, "%d", i);
|
---|
4194 | define_variable (num, strlen (num), "", o_automatic, 0);
|
---|
4195 | }
|
---|
4196 |
|
---|
4197 | saved_args = max_args;
|
---|
4198 | max_args = i;
|
---|
4199 |
|
---|
4200 | #ifdef CONFIG_WITH_EVALPLUS
|
---|
4201 | if (!strcmp (funcname, "call"))
|
---|
4202 | {
|
---|
4203 | #endif
|
---|
4204 | /* Expand the body in the context of the arguments, adding the result to
|
---|
4205 | the variable buffer. */
|
---|
4206 |
|
---|
4207 | v->exp_count = EXP_COUNT_MAX;
|
---|
4208 | o = variable_expand_string (o, body, flen+3);
|
---|
4209 | v->exp_count = 0;
|
---|
4210 |
|
---|
4211 | o += strlen (o);
|
---|
4212 | #ifdef CONFIG_WITH_EVALPLUS
|
---|
4213 | }
|
---|
4214 | else
|
---|
4215 | {
|
---|
4216 | const struct floc *reading_file_saved = reading_file;
|
---|
4217 |
|
---|
4218 | if (!strcmp (funcname, "evalcall"))
|
---|
4219 | {
|
---|
4220 | /* Evaluate the variable value without expanding it. We
|
---|
4221 | need a copy since eval_buffer is destructive. */
|
---|
4222 |
|
---|
4223 | size_t off = o - variable_buffer;
|
---|
4224 | o = variable_buffer_output (o, v->value, v->value_length + 1);
|
---|
4225 | o = variable_buffer + off;
|
---|
4226 | if (v->fileinfo.filenm)
|
---|
4227 | reading_file = &v->fileinfo;
|
---|
4228 | }
|
---|
4229 | else
|
---|
4230 | {
|
---|
4231 | /* Expand the body first and then evaluate the output. */
|
---|
4232 |
|
---|
4233 | v->exp_count = EXP_COUNT_MAX;
|
---|
4234 | o = variable_expand_string (o, body, flen+3);
|
---|
4235 | v->exp_count = 0;
|
---|
4236 | }
|
---|
4237 |
|
---|
4238 | install_variable_buffer (&buf, &len);
|
---|
4239 | eval_buffer (o);
|
---|
4240 | restore_variable_buffer (buf, len);
|
---|
4241 | reading_file = reading_file_saved;
|
---|
4242 | }
|
---|
4243 | #endif /* CONFIG_WITH_EVALPLUS */
|
---|
4244 |
|
---|
4245 | max_args = saved_args;
|
---|
4246 |
|
---|
4247 | pop_variable_scope ();
|
---|
4248 |
|
---|
4249 | return o;
|
---|
4250 | }
|
---|
4251 |
|
---|
4252 | void
|
---|
4253 | hash_init_function_table (void)
|
---|
4254 | {
|
---|
4255 | hash_init (&function_table, FUNCTION_TABLE_ENTRIES * 2,
|
---|
4256 | function_table_entry_hash_1, function_table_entry_hash_2,
|
---|
4257 | function_table_entry_hash_cmp);
|
---|
4258 | hash_load (&function_table, function_table_init,
|
---|
4259 | FUNCTION_TABLE_ENTRIES, sizeof (struct function_table_entry));
|
---|
4260 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
4261 | {
|
---|
4262 | unsigned i;
|
---|
4263 | for (i = 0; i < FUNCTION_TABLE_ENTRIES; i++)
|
---|
4264 | assert (function_table_init[i].len <= MAX_FUNCTION_LENGTH);
|
---|
4265 | }
|
---|
4266 | #endif
|
---|
4267 | }
|
---|