1 | /* Builtin function expansion for GNU Make.
|
---|
2 | Copyright (C) 1988, 1989, 1991-1997, 1999, 2002 Free Software Foundation, Inc.
|
---|
3 | This file is part of GNU Make.
|
---|
4 |
|
---|
5 | GNU Make is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 2, or (at your option)
|
---|
8 | any later version.
|
---|
9 |
|
---|
10 | GNU Make is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with GNU Make; see the file COPYING. If not, write to
|
---|
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
---|
18 | Boston, MA 02111-1307, USA. */
|
---|
19 |
|
---|
20 | #include "make.h"
|
---|
21 | #include "filedef.h"
|
---|
22 | #include "variable.h"
|
---|
23 | #include "dep.h"
|
---|
24 | #include "job.h"
|
---|
25 | #include "commands.h"
|
---|
26 | #include "debug.h"
|
---|
27 |
|
---|
28 | #ifdef _AMIGA
|
---|
29 | #include "amiga.h"
|
---|
30 | #endif
|
---|
31 |
|
---|
32 |
|
---|
33 | struct function_table_entry
|
---|
34 | {
|
---|
35 | const char *name;
|
---|
36 | unsigned char len;
|
---|
37 | unsigned char minimum_args;
|
---|
38 | unsigned char maximum_args;
|
---|
39 | char expand_args;
|
---|
40 | char *(*func_ptr) PARAMS ((char *output, char **argv, const char *fname));
|
---|
41 | };
|
---|
42 |
|
---|
43 | static unsigned long
|
---|
44 | function_table_entry_hash_1 (const void *keyv)
|
---|
45 | {
|
---|
46 | struct function_table_entry const *key = (struct function_table_entry const *) keyv;
|
---|
47 | return_STRING_N_HASH_1 (key->name, key->len);
|
---|
48 | }
|
---|
49 |
|
---|
50 | static unsigned long
|
---|
51 | function_table_entry_hash_2 (const void *keyv)
|
---|
52 | {
|
---|
53 | struct function_table_entry const *key = (struct function_table_entry const *) keyv;
|
---|
54 | return_STRING_N_HASH_2 (key->name, key->len);
|
---|
55 | }
|
---|
56 |
|
---|
57 | static int
|
---|
58 | function_table_entry_hash_cmp (const void *xv, const void *yv)
|
---|
59 | {
|
---|
60 | struct function_table_entry const *x = (struct function_table_entry const *) xv;
|
---|
61 | struct function_table_entry const *y = (struct function_table_entry const *) yv;
|
---|
62 | int result = x->len - y->len;
|
---|
63 | if (result)
|
---|
64 | return result;
|
---|
65 | return_STRING_N_COMPARE (x->name, y->name, x->len);
|
---|
66 | }
|
---|
67 |
|
---|
68 | static struct hash_table function_table;
|
---|
69 | |
---|
70 |
|
---|
71 |
|
---|
72 | /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
|
---|
73 | each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
|
---|
74 | the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
|
---|
75 | nonzero, substitutions are done only on matches which are complete
|
---|
76 | whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
|
---|
77 | done only at the ends of whitespace-delimited words. */
|
---|
78 |
|
---|
79 | char *
|
---|
80 | subst_expand (char *o, char *text, char *subst, char *replace,
|
---|
81 | unsigned int slen, unsigned int rlen,
|
---|
82 | int by_word, int suffix_only)
|
---|
83 | {
|
---|
84 | char *t = text;
|
---|
85 | unsigned int tlen = strlen (text);
|
---|
86 | char *p;
|
---|
87 |
|
---|
88 | if (slen == 0 && !by_word && !suffix_only)
|
---|
89 | {
|
---|
90 | /* The first occurrence of "" in any string is its end. */
|
---|
91 | o = variable_buffer_output (o, t, tlen);
|
---|
92 | if (rlen > 0)
|
---|
93 | o = variable_buffer_output (o, replace, rlen);
|
---|
94 | return o;
|
---|
95 | }
|
---|
96 |
|
---|
97 | do
|
---|
98 | {
|
---|
99 | if ((by_word | suffix_only) && slen == 0)
|
---|
100 | /* When matching by words, the empty string should match
|
---|
101 | the end of each word, rather than the end of the whole text. */
|
---|
102 | p = end_of_token (next_token (t));
|
---|
103 | else
|
---|
104 | {
|
---|
105 | p = sindex (t, tlen, subst, slen);
|
---|
106 | if (p == 0)
|
---|
107 | {
|
---|
108 | /* No more matches. Output everything left on the end. */
|
---|
109 | o = variable_buffer_output (o, t, tlen);
|
---|
110 | return o;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* Output everything before this occurrence of the string to replace. */
|
---|
115 | if (p > t)
|
---|
116 | o = variable_buffer_output (o, t, p - t);
|
---|
117 |
|
---|
118 | /* If we're substituting only by fully matched words,
|
---|
119 | or only at the ends of words, check that this case qualifies. */
|
---|
120 | if ((by_word
|
---|
121 | && ((p > t && !isblank ((unsigned char)p[-1]))
|
---|
122 | || (p[slen] != '\0' && !isblank ((unsigned char)p[slen]))))
|
---|
123 | || (suffix_only
|
---|
124 | && (p[slen] != '\0' && !isblank ((unsigned char)p[slen]))))
|
---|
125 | /* Struck out. Output the rest of the string that is
|
---|
126 | no longer to be replaced. */
|
---|
127 | o = variable_buffer_output (o, subst, slen);
|
---|
128 | else if (rlen > 0)
|
---|
129 | /* Output the replacement string. */
|
---|
130 | o = variable_buffer_output (o, replace, rlen);
|
---|
131 |
|
---|
132 | /* Advance T past the string to be replaced; adjust tlen. */
|
---|
133 | {
|
---|
134 | char *nt = p + slen;
|
---|
135 | tlen -= nt - t;
|
---|
136 | t = nt;
|
---|
137 | }
|
---|
138 | } while (*t != '\0');
|
---|
139 |
|
---|
140 | return o;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
|
---|
145 | and replacing strings matching PATTERN with REPLACE.
|
---|
146 | If PATTERN_PERCENT is not nil, PATTERN has already been
|
---|
147 | run through find_percent, and PATTERN_PERCENT is the result.
|
---|
148 | If REPLACE_PERCENT is not nil, REPLACE has already been
|
---|
149 | run through find_percent, and REPLACE_PERCENT is the result. */
|
---|
150 |
|
---|
151 | char *
|
---|
152 | patsubst_expand (char *o, char *text, char *pattern, char *replace,
|
---|
153 | char *pattern_percent, char *replace_percent)
|
---|
154 | {
|
---|
155 | unsigned int pattern_prepercent_len, pattern_postpercent_len;
|
---|
156 | unsigned int replace_prepercent_len, replace_postpercent_len = 0;
|
---|
157 | char *t;
|
---|
158 | unsigned int len;
|
---|
159 | int doneany = 0;
|
---|
160 |
|
---|
161 | /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
|
---|
162 | will be collapsed before we call subst_expand if PATTERN has no %. */
|
---|
163 | if (replace_percent == 0)
|
---|
164 | replace_percent = find_percent (replace);
|
---|
165 | if (replace_percent != 0)
|
---|
166 | {
|
---|
167 | /* Record the length of REPLACE before and after the % so
|
---|
168 | we don't have to compute these lengths more than once. */
|
---|
169 | replace_prepercent_len = replace_percent - replace;
|
---|
170 | replace_postpercent_len = strlen (replace_percent + 1);
|
---|
171 | }
|
---|
172 | else
|
---|
173 | /* We store the length of the replacement
|
---|
174 | so we only need to compute it once. */
|
---|
175 | replace_prepercent_len = strlen (replace);
|
---|
176 |
|
---|
177 | if (pattern_percent == 0)
|
---|
178 | pattern_percent = find_percent (pattern);
|
---|
179 | if (pattern_percent == 0)
|
---|
180 | /* With no % in the pattern, this is just a simple substitution. */
|
---|
181 | return subst_expand (o, text, pattern, replace,
|
---|
182 | strlen (pattern), strlen (replace), 1, 0);
|
---|
183 |
|
---|
184 | /* Record the length of PATTERN before and after the %
|
---|
185 | so we don't have to compute it more than once. */
|
---|
186 | pattern_prepercent_len = pattern_percent - pattern;
|
---|
187 | pattern_postpercent_len = strlen (pattern_percent + 1);
|
---|
188 |
|
---|
189 | while ((t = find_next_token (&text, &len)) != 0)
|
---|
190 | {
|
---|
191 | int fail = 0;
|
---|
192 |
|
---|
193 | /* Is it big enough to match? */
|
---|
194 | if (len < pattern_prepercent_len + pattern_postpercent_len)
|
---|
195 | fail = 1;
|
---|
196 |
|
---|
197 | /* Does the prefix match? */
|
---|
198 | if (!fail && pattern_prepercent_len > 0
|
---|
199 | && (*t != *pattern
|
---|
200 | || t[pattern_prepercent_len - 1] != pattern_percent[-1]
|
---|
201 | || !strneq (t + 1, pattern + 1, pattern_prepercent_len - 1)))
|
---|
202 | fail = 1;
|
---|
203 |
|
---|
204 | /* Does the suffix match? */
|
---|
205 | if (!fail && pattern_postpercent_len > 0
|
---|
206 | && (t[len - 1] != pattern_percent[pattern_postpercent_len]
|
---|
207 | || t[len - pattern_postpercent_len] != pattern_percent[1]
|
---|
208 | || !strneq (&t[len - pattern_postpercent_len],
|
---|
209 | &pattern_percent[1], pattern_postpercent_len - 1)))
|
---|
210 | fail = 1;
|
---|
211 |
|
---|
212 | if (fail)
|
---|
213 | /* It didn't match. Output the string. */
|
---|
214 | o = variable_buffer_output (o, t, len);
|
---|
215 | else
|
---|
216 | {
|
---|
217 | /* It matched. Output the replacement. */
|
---|
218 |
|
---|
219 | /* Output the part of the replacement before the %. */
|
---|
220 | o = variable_buffer_output (o, replace, replace_prepercent_len);
|
---|
221 |
|
---|
222 | if (replace_percent != 0)
|
---|
223 | {
|
---|
224 | /* Output the part of the matched string that
|
---|
225 | matched the % in the pattern. */
|
---|
226 | o = variable_buffer_output (o, t + pattern_prepercent_len,
|
---|
227 | len - (pattern_prepercent_len
|
---|
228 | + pattern_postpercent_len));
|
---|
229 | /* Output the part of the replacement after the %. */
|
---|
230 | o = variable_buffer_output (o, replace_percent + 1,
|
---|
231 | replace_postpercent_len);
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | /* Output a space, but not if the replacement is "". */
|
---|
236 | if (fail || replace_prepercent_len > 0
|
---|
237 | || (replace_percent != 0 && len + replace_postpercent_len > 0))
|
---|
238 | {
|
---|
239 | o = variable_buffer_output (o, " ", 1);
|
---|
240 | doneany = 1;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | if (doneany)
|
---|
244 | /* Kill the last space. */
|
---|
245 | --o;
|
---|
246 |
|
---|
247 | return o;
|
---|
248 | }
|
---|
249 | |
---|
250 |
|
---|
251 |
|
---|
252 | /* Look up a function by name. */
|
---|
253 |
|
---|
254 | static const struct function_table_entry *
|
---|
255 | lookup_function (const char *s)
|
---|
256 | {
|
---|
257 | const char *e = s;
|
---|
258 |
|
---|
259 | while (*e && ( (*e >= 'a' && *e <= 'z') || *e == '-'))
|
---|
260 | e++;
|
---|
261 | if (*e == '\0' || isblank ((unsigned char) *e))
|
---|
262 | {
|
---|
263 | struct function_table_entry function_table_entry_key;
|
---|
264 | function_table_entry_key.name = s;
|
---|
265 | function_table_entry_key.len = e - s;
|
---|
266 |
|
---|
267 | return hash_find_item (&function_table, &function_table_entry_key);
|
---|
268 | }
|
---|
269 | return 0;
|
---|
270 | }
|
---|
271 | |
---|
272 |
|
---|
273 |
|
---|
274 | /* Return 1 if PATTERN matches STR, 0 if not. */
|
---|
275 |
|
---|
276 | int
|
---|
277 | pattern_matches (char *pattern, char *percent, char *str)
|
---|
278 | {
|
---|
279 | unsigned int sfxlen, strlength;
|
---|
280 |
|
---|
281 | if (percent == 0)
|
---|
282 | {
|
---|
283 | unsigned int len = strlen (pattern) + 1;
|
---|
284 | char *new_chars = (char *) alloca (len);
|
---|
285 | bcopy (pattern, new_chars, len);
|
---|
286 | pattern = new_chars;
|
---|
287 | percent = find_percent (pattern);
|
---|
288 | if (percent == 0)
|
---|
289 | return streq (pattern, str);
|
---|
290 | }
|
---|
291 |
|
---|
292 | sfxlen = strlen (percent + 1);
|
---|
293 | strlength = strlen (str);
|
---|
294 |
|
---|
295 | if (strlength < (percent - pattern) + sfxlen
|
---|
296 | || !strneq (pattern, str, percent - pattern))
|
---|
297 | return 0;
|
---|
298 |
|
---|
299 | return !strcmp (percent + 1, str + (strlength - sfxlen));
|
---|
300 | }
|
---|
301 | |
---|
302 |
|
---|
303 |
|
---|
304 | /* Find the next comma or ENDPAREN (counting nested STARTPAREN and
|
---|
305 | ENDPARENtheses), starting at PTR before END. Return a pointer to
|
---|
306 | next character.
|
---|
307 |
|
---|
308 | If no next argument is found, return NULL.
|
---|
309 | */
|
---|
310 |
|
---|
311 | static char *
|
---|
312 | find_next_argument (char startparen, char endparen,
|
---|
313 | const char *ptr, const char *end)
|
---|
314 | {
|
---|
315 | int count = 0;
|
---|
316 |
|
---|
317 | for (; ptr < end; ++ptr)
|
---|
318 | if (*ptr == startparen)
|
---|
319 | ++count;
|
---|
320 |
|
---|
321 | else if (*ptr == endparen)
|
---|
322 | {
|
---|
323 | --count;
|
---|
324 | if (count < 0)
|
---|
325 | return NULL;
|
---|
326 | }
|
---|
327 |
|
---|
328 | else if (*ptr == ',' && !count)
|
---|
329 | return (char *)ptr;
|
---|
330 |
|
---|
331 | /* We didn't find anything. */
|
---|
332 | return NULL;
|
---|
333 | }
|
---|
334 | |
---|
335 |
|
---|
336 |
|
---|
337 | /* Glob-expand LINE. The returned pointer is
|
---|
338 | only good until the next call to string_glob. */
|
---|
339 |
|
---|
340 | static char *
|
---|
341 | string_glob (char *line)
|
---|
342 | {
|
---|
343 | static char *result = 0;
|
---|
344 | static unsigned int length;
|
---|
345 | register struct nameseq *chain;
|
---|
346 | register unsigned int idx;
|
---|
347 |
|
---|
348 | chain = multi_glob (parse_file_seq
|
---|
349 | (&line, '\0', sizeof (struct nameseq),
|
---|
350 | /* We do not want parse_file_seq to strip `./'s.
|
---|
351 | That would break examples like:
|
---|
352 | $(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
|
---|
353 | 0),
|
---|
354 | sizeof (struct nameseq));
|
---|
355 |
|
---|
356 | if (result == 0)
|
---|
357 | {
|
---|
358 | length = 100;
|
---|
359 | result = (char *) xmalloc (100);
|
---|
360 | }
|
---|
361 |
|
---|
362 | idx = 0;
|
---|
363 | while (chain != 0)
|
---|
364 | {
|
---|
365 | register char *name = chain->name;
|
---|
366 | unsigned int len = strlen (name);
|
---|
367 |
|
---|
368 | struct nameseq *next = chain->next;
|
---|
369 | free ((char *) chain);
|
---|
370 | chain = next;
|
---|
371 |
|
---|
372 | /* multi_glob will pass names without globbing metacharacters
|
---|
373 | through as is, but we want only files that actually exist. */
|
---|
374 | if (file_exists_p (name))
|
---|
375 | {
|
---|
376 | if (idx + len + 1 > length)
|
---|
377 | {
|
---|
378 | length += (len + 1) * 2;
|
---|
379 | result = (char *) xrealloc (result, length);
|
---|
380 | }
|
---|
381 | bcopy (name, &result[idx], len);
|
---|
382 | idx += len;
|
---|
383 | result[idx++] = ' ';
|
---|
384 | }
|
---|
385 |
|
---|
386 | free (name);
|
---|
387 | }
|
---|
388 |
|
---|
389 | /* Kill the last space and terminate the string. */
|
---|
390 | if (idx == 0)
|
---|
391 | result[0] = '\0';
|
---|
392 | else
|
---|
393 | result[idx - 1] = '\0';
|
---|
394 |
|
---|
395 | return result;
|
---|
396 | }
|
---|
397 | |
---|
398 |
|
---|
399 | /*
|
---|
400 | Builtin functions
|
---|
401 | */
|
---|
402 |
|
---|
403 | static char *
|
---|
404 | func_patsubst (char *o, char **argv, const char *funcname)
|
---|
405 | {
|
---|
406 | o = patsubst_expand (o, argv[2], argv[0], argv[1], (char *) 0, (char *) 0);
|
---|
407 | return o;
|
---|
408 | }
|
---|
409 |
|
---|
410 |
|
---|
411 | static char *
|
---|
412 | func_join (char *o, char **argv, const char *funcname)
|
---|
413 | {
|
---|
414 | int doneany = 0;
|
---|
415 |
|
---|
416 | /* Write each word of the first argument directly followed
|
---|
417 | by the corresponding word of the second argument.
|
---|
418 | If the two arguments have a different number of words,
|
---|
419 | the excess words are just output separated by blanks. */
|
---|
420 | register char *tp;
|
---|
421 | register char *pp;
|
---|
422 | char *list1_iterator = argv[0];
|
---|
423 | char *list2_iterator = argv[1];
|
---|
424 | do
|
---|
425 | {
|
---|
426 | unsigned int len1, len2;
|
---|
427 |
|
---|
428 | tp = find_next_token (&list1_iterator, &len1);
|
---|
429 | if (tp != 0)
|
---|
430 | o = variable_buffer_output (o, tp, len1);
|
---|
431 |
|
---|
432 | pp = find_next_token (&list2_iterator, &len2);
|
---|
433 | if (pp != 0)
|
---|
434 | o = variable_buffer_output (o, pp, len2);
|
---|
435 |
|
---|
436 | if (tp != 0 || pp != 0)
|
---|
437 | {
|
---|
438 | o = variable_buffer_output (o, " ", 1);
|
---|
439 | doneany = 1;
|
---|
440 | }
|
---|
441 | }
|
---|
442 | while (tp != 0 || pp != 0);
|
---|
443 | if (doneany)
|
---|
444 | /* Kill the last blank. */
|
---|
445 | --o;
|
---|
446 |
|
---|
447 | return o;
|
---|
448 | }
|
---|
449 |
|
---|
450 |
|
---|
451 | static char *
|
---|
452 | func_origin (char *o, char **argv, const char *funcname)
|
---|
453 | {
|
---|
454 | /* Expand the argument. */
|
---|
455 | register struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
---|
456 | if (v == 0)
|
---|
457 | o = variable_buffer_output (o, "undefined", 9);
|
---|
458 | else
|
---|
459 | switch (v->origin)
|
---|
460 | {
|
---|
461 | default:
|
---|
462 | case o_invalid:
|
---|
463 | abort ();
|
---|
464 | break;
|
---|
465 | case o_default:
|
---|
466 | o = variable_buffer_output (o, "default", 7);
|
---|
467 | break;
|
---|
468 | case o_env:
|
---|
469 | o = variable_buffer_output (o, "environment", 11);
|
---|
470 | break;
|
---|
471 | case o_file:
|
---|
472 | o = variable_buffer_output (o, "file", 4);
|
---|
473 | break;
|
---|
474 | case o_env_override:
|
---|
475 | o = variable_buffer_output (o, "environment override", 20);
|
---|
476 | break;
|
---|
477 | case o_command:
|
---|
478 | o = variable_buffer_output (o, "command line", 12);
|
---|
479 | break;
|
---|
480 | case o_override:
|
---|
481 | o = variable_buffer_output (o, "override", 8);
|
---|
482 | break;
|
---|
483 | case o_automatic:
|
---|
484 | o = variable_buffer_output (o, "automatic", 9);
|
---|
485 | break;
|
---|
486 | }
|
---|
487 |
|
---|
488 | return o;
|
---|
489 | }
|
---|
490 |
|
---|
491 | #ifdef VMS
|
---|
492 | # define IS_PATHSEP(c) ((c) == ']')
|
---|
493 | #else
|
---|
494 | # ifdef HAVE_DOS_PATHS
|
---|
495 | # define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
|
---|
496 | # else
|
---|
497 | # define IS_PATHSEP(c) ((c) == '/')
|
---|
498 | # endif
|
---|
499 | #endif
|
---|
500 |
|
---|
501 |
|
---|
502 | static char *
|
---|
503 | func_notdir_suffix (char *o, char **argv, const char *funcname)
|
---|
504 | {
|
---|
505 | /* Expand the argument. */
|
---|
506 | char *list_iterator = argv[0];
|
---|
507 | char *p2 =0;
|
---|
508 | int doneany =0;
|
---|
509 | unsigned int len=0;
|
---|
510 |
|
---|
511 | int is_suffix = streq (funcname, "suffix");
|
---|
512 | int is_notdir = !is_suffix;
|
---|
513 | while ((p2 = find_next_token (&list_iterator, &len)) != 0)
|
---|
514 | {
|
---|
515 | char *p = p2 + len;
|
---|
516 |
|
---|
517 |
|
---|
518 | while (p >= p2 && (!is_suffix || *p != '.'))
|
---|
519 | {
|
---|
520 | if (IS_PATHSEP (*p))
|
---|
521 | break;
|
---|
522 | --p;
|
---|
523 | }
|
---|
524 |
|
---|
525 | if (p >= p2)
|
---|
526 | {
|
---|
527 | if (is_notdir)
|
---|
528 | ++p;
|
---|
529 | else if (*p != '.')
|
---|
530 | continue;
|
---|
531 | o = variable_buffer_output (o, p, len - (p - p2));
|
---|
532 | }
|
---|
533 | #ifdef HAVE_DOS_PATHS
|
---|
534 | /* Handle the case of "d:foo/bar". */
|
---|
535 | else if (streq (funcname, "notdir") && p2[0] && p2[1] == ':')
|
---|
536 | {
|
---|
537 | p = p2 + 2;
|
---|
538 | o = variable_buffer_output (o, p, len - (p - p2));
|
---|
539 | }
|
---|
540 | #endif
|
---|
541 | else if (is_notdir)
|
---|
542 | o = variable_buffer_output (o, p2, len);
|
---|
543 |
|
---|
544 | if (is_notdir || p >= p2)
|
---|
545 | {
|
---|
546 | o = variable_buffer_output (o, " ", 1);
|
---|
547 | doneany = 1;
|
---|
548 | }
|
---|
549 | }
|
---|
550 | if (doneany)
|
---|
551 | /* Kill last space. */
|
---|
552 | --o;
|
---|
553 |
|
---|
554 |
|
---|
555 | return o;
|
---|
556 |
|
---|
557 | }
|
---|
558 |
|
---|
559 |
|
---|
560 | static char *
|
---|
561 | func_basename_dir (char *o, char **argv, const char *funcname)
|
---|
562 | {
|
---|
563 | /* Expand the argument. */
|
---|
564 | char *p3 = argv[0];
|
---|
565 | char *p2=0;
|
---|
566 | int doneany=0;
|
---|
567 | unsigned int len=0;
|
---|
568 | char *p=0;
|
---|
569 | int is_basename= streq (funcname, "basename");
|
---|
570 | int is_dir= !is_basename;
|
---|
571 |
|
---|
572 | while ((p2 = find_next_token (&p3, &len)) != 0)
|
---|
573 | {
|
---|
574 | p = p2 + len;
|
---|
575 | while (p >= p2 && (!is_basename || *p != '.'))
|
---|
576 | {
|
---|
577 | if (IS_PATHSEP (*p))
|
---|
578 | break;
|
---|
579 | --p;
|
---|
580 | }
|
---|
581 |
|
---|
582 | if (p >= p2 && (is_dir))
|
---|
583 | o = variable_buffer_output (o, p2, ++p - p2);
|
---|
584 | else if (p >= p2 && (*p == '.'))
|
---|
585 | o = variable_buffer_output (o, p2, p - p2);
|
---|
586 | #ifdef HAVE_DOS_PATHS
|
---|
587 | /* Handle the "d:foobar" case */
|
---|
588 | else if (p2[0] && p2[1] == ':' && is_dir)
|
---|
589 | o = variable_buffer_output (o, p2, 2);
|
---|
590 | #endif
|
---|
591 | else if (is_dir)
|
---|
592 | #ifdef VMS
|
---|
593 | o = variable_buffer_output (o, "[]", 2);
|
---|
594 | #else
|
---|
595 | #ifndef _AMIGA
|
---|
596 | o = variable_buffer_output (o, "./", 2);
|
---|
597 | #else
|
---|
598 | ; /* Just a nop... */
|
---|
599 | #endif /* AMIGA */
|
---|
600 | #endif /* !VMS */
|
---|
601 | else
|
---|
602 | /* The entire name is the basename. */
|
---|
603 | o = variable_buffer_output (o, p2, len);
|
---|
604 |
|
---|
605 | o = variable_buffer_output (o, " ", 1);
|
---|
606 | doneany = 1;
|
---|
607 | }
|
---|
608 | if (doneany)
|
---|
609 | /* Kill last space. */
|
---|
610 | --o;
|
---|
611 |
|
---|
612 |
|
---|
613 | return o;
|
---|
614 | }
|
---|
615 |
|
---|
616 | static char *
|
---|
617 | func_addsuffix_addprefix (char *o, char **argv, const char *funcname)
|
---|
618 | {
|
---|
619 | int fixlen = strlen (argv[0]);
|
---|
620 | char *list_iterator = argv[1];
|
---|
621 | int is_addprefix = streq (funcname, "addprefix");
|
---|
622 | int is_addsuffix = !is_addprefix;
|
---|
623 |
|
---|
624 | int doneany = 0;
|
---|
625 | char *p;
|
---|
626 | unsigned int len;
|
---|
627 |
|
---|
628 | while ((p = find_next_token (&list_iterator, &len)) != 0)
|
---|
629 | {
|
---|
630 | if (is_addprefix)
|
---|
631 | o = variable_buffer_output (o, argv[0], fixlen);
|
---|
632 | o = variable_buffer_output (o, p, len);
|
---|
633 | if (is_addsuffix)
|
---|
634 | o = variable_buffer_output (o, argv[0], fixlen);
|
---|
635 | o = variable_buffer_output (o, " ", 1);
|
---|
636 | doneany = 1;
|
---|
637 | }
|
---|
638 |
|
---|
639 | if (doneany)
|
---|
640 | /* Kill last space. */
|
---|
641 | --o;
|
---|
642 |
|
---|
643 | return o;
|
---|
644 | }
|
---|
645 |
|
---|
646 | static char *
|
---|
647 | func_subst (char *o, char **argv, const char *funcname)
|
---|
648 | {
|
---|
649 | o = subst_expand (o, argv[2], argv[0], argv[1], strlen (argv[0]),
|
---|
650 | strlen (argv[1]), 0, 0);
|
---|
651 |
|
---|
652 | return o;
|
---|
653 | }
|
---|
654 |
|
---|
655 |
|
---|
656 | static char *
|
---|
657 | func_firstword (char *o, char **argv, const char *funcname)
|
---|
658 | {
|
---|
659 | unsigned int i;
|
---|
660 | char *words = argv[0]; /* Use a temp variable for find_next_token */
|
---|
661 | char *p = find_next_token (&words, &i);
|
---|
662 |
|
---|
663 | if (p != 0)
|
---|
664 | o = variable_buffer_output (o, p, i);
|
---|
665 |
|
---|
666 | return o;
|
---|
667 | }
|
---|
668 |
|
---|
669 |
|
---|
670 | static char *
|
---|
671 | func_words (char *o, char **argv, const char *funcname)
|
---|
672 | {
|
---|
673 | int i = 0;
|
---|
674 | char *word_iterator = argv[0];
|
---|
675 | char buf[20];
|
---|
676 |
|
---|
677 | while (find_next_token (&word_iterator, (unsigned int *) 0) != 0)
|
---|
678 | ++i;
|
---|
679 |
|
---|
680 | sprintf (buf, "%d", i);
|
---|
681 | o = variable_buffer_output (o, buf, strlen (buf));
|
---|
682 |
|
---|
683 |
|
---|
684 | return o;
|
---|
685 | }
|
---|
686 |
|
---|
687 | /* Set begpp to point to the first non-whitespace character of the string,
|
---|
688 | * and endpp to point to the last non-whitespace character of the string.
|
---|
689 | * If the string is empty or contains nothing but whitespace, endpp will be
|
---|
690 | * begpp-1.
|
---|
691 | */
|
---|
692 | static char *
|
---|
693 | strip_whitespace (const char **begpp, const char **endpp)
|
---|
694 | {
|
---|
695 | while (*begpp <= *endpp && isspace ((unsigned char)**begpp))
|
---|
696 | (*begpp) ++;
|
---|
697 | while (*endpp >= *begpp && isspace ((unsigned char)**endpp))
|
---|
698 | (*endpp) --;
|
---|
699 | return (char *)*begpp;
|
---|
700 | }
|
---|
701 |
|
---|
702 | static void
|
---|
703 | check_numeric (const char *s, const char *message)
|
---|
704 | {
|
---|
705 | const char *end = s + strlen (s) - 1;
|
---|
706 | const char *beg = s;
|
---|
707 | strip_whitespace (&s, &end);
|
---|
708 |
|
---|
709 | for (; s <= end; ++s)
|
---|
710 | if (!ISDIGIT (*s)) /* ISDIGIT only evals its arg once: see make.h. */
|
---|
711 | break;
|
---|
712 |
|
---|
713 | if (s <= end || end - beg < 0)
|
---|
714 | fatal (reading_file, "%s: '%s'", message, beg);
|
---|
715 | }
|
---|
716 |
|
---|
717 |
|
---|
718 |
|
---|
719 | static char *
|
---|
720 | func_word (char *o, char **argv, const char *funcname)
|
---|
721 | {
|
---|
722 | char *end_p=0;
|
---|
723 | int i=0;
|
---|
724 | char *p=0;
|
---|
725 |
|
---|
726 | /* Check the first argument. */
|
---|
727 | check_numeric (argv[0], _("non-numeric first argument to `word' function"));
|
---|
728 | i = atoi (argv[0]);
|
---|
729 |
|
---|
730 | if (i == 0)
|
---|
731 | fatal (reading_file, _("first argument to `word' function must be greater than 0"));
|
---|
732 |
|
---|
733 |
|
---|
734 | end_p = argv[1];
|
---|
735 | while ((p = find_next_token (&end_p, 0)) != 0)
|
---|
736 | if (--i == 0)
|
---|
737 | break;
|
---|
738 |
|
---|
739 | if (i == 0)
|
---|
740 | o = variable_buffer_output (o, p, end_p - p);
|
---|
741 |
|
---|
742 | return o;
|
---|
743 | }
|
---|
744 |
|
---|
745 | static char *
|
---|
746 | func_wordlist (char *o, char **argv, const char *funcname)
|
---|
747 | {
|
---|
748 | int start, count;
|
---|
749 |
|
---|
750 | /* Check the arguments. */
|
---|
751 | check_numeric (argv[0],
|
---|
752 | _("non-numeric first argument to `wordlist' function"));
|
---|
753 | check_numeric (argv[1],
|
---|
754 | _("non-numeric second argument to `wordlist' function"));
|
---|
755 |
|
---|
756 | start = atoi (argv[0]);
|
---|
757 | count = atoi (argv[1]) - start + 1;
|
---|
758 |
|
---|
759 | if (count > 0)
|
---|
760 | {
|
---|
761 | char *p;
|
---|
762 | char *end_p = argv[2];
|
---|
763 |
|
---|
764 | /* Find the beginning of the "start"th word. */
|
---|
765 | while (((p = find_next_token (&end_p, 0)) != 0) && --start)
|
---|
766 | ;
|
---|
767 |
|
---|
768 | if (p)
|
---|
769 | {
|
---|
770 | /* Find the end of the "count"th word from start. */
|
---|
771 | while (--count && (find_next_token (&end_p, 0) != 0))
|
---|
772 | ;
|
---|
773 |
|
---|
774 | /* Return the stuff in the middle. */
|
---|
775 | o = variable_buffer_output (o, p, end_p - p);
|
---|
776 | }
|
---|
777 | }
|
---|
778 |
|
---|
779 | return o;
|
---|
780 | }
|
---|
781 |
|
---|
782 | static char*
|
---|
783 | func_findstring (char *o, char **argv, const char *funcname)
|
---|
784 | {
|
---|
785 | /* Find the first occurrence of the first string in the second. */
|
---|
786 | int i = strlen (argv[0]);
|
---|
787 | if (sindex (argv[1], 0, argv[0], i) != 0)
|
---|
788 | o = variable_buffer_output (o, argv[0], i);
|
---|
789 |
|
---|
790 | return o;
|
---|
791 | }
|
---|
792 |
|
---|
793 | static char *
|
---|
794 | func_foreach (char *o, char **argv, const char *funcname)
|
---|
795 | {
|
---|
796 | /* expand only the first two. */
|
---|
797 | char *varname = expand_argument (argv[0], NULL);
|
---|
798 | char *list = expand_argument (argv[1], NULL);
|
---|
799 | char *body = argv[2];
|
---|
800 |
|
---|
801 | int doneany = 0;
|
---|
802 | char *list_iterator = list;
|
---|
803 | char *p;
|
---|
804 | unsigned int len;
|
---|
805 | register struct variable *var;
|
---|
806 |
|
---|
807 | push_new_variable_scope ();
|
---|
808 | var = define_variable (varname, strlen (varname), "", o_automatic, 0);
|
---|
809 |
|
---|
810 | /* loop through LIST, put the value in VAR and expand BODY */
|
---|
811 | while ((p = find_next_token (&list_iterator, &len)) != 0)
|
---|
812 | {
|
---|
813 | char *result = 0;
|
---|
814 |
|
---|
815 | {
|
---|
816 | char save = p[len];
|
---|
817 |
|
---|
818 | p[len] = '\0';
|
---|
819 | free (var->value);
|
---|
820 | var->value = (char *) xstrdup ((char*) p);
|
---|
821 | p[len] = save;
|
---|
822 | }
|
---|
823 |
|
---|
824 | result = allocated_variable_expand (body);
|
---|
825 |
|
---|
826 | o = variable_buffer_output (o, result, strlen (result));
|
---|
827 | o = variable_buffer_output (o, " ", 1);
|
---|
828 | doneany = 1;
|
---|
829 | free (result);
|
---|
830 | }
|
---|
831 |
|
---|
832 | if (doneany)
|
---|
833 | /* Kill the last space. */
|
---|
834 | --o;
|
---|
835 |
|
---|
836 | pop_variable_scope ();
|
---|
837 | free (varname);
|
---|
838 | free (list);
|
---|
839 |
|
---|
840 | return o;
|
---|
841 | }
|
---|
842 |
|
---|
843 | struct a_word
|
---|
844 | {
|
---|
845 | struct a_word *next;
|
---|
846 | struct a_word *chain;
|
---|
847 | char *str;
|
---|
848 | int length;
|
---|
849 | int matched;
|
---|
850 | };
|
---|
851 |
|
---|
852 | static unsigned long
|
---|
853 | a_word_hash_1 (const void *key)
|
---|
854 | {
|
---|
855 | return_STRING_HASH_1 (((struct a_word const *) key)->str);
|
---|
856 | }
|
---|
857 |
|
---|
858 | static unsigned long
|
---|
859 | a_word_hash_2 (const void *key)
|
---|
860 | {
|
---|
861 | return_STRING_HASH_2 (((struct a_word const *) key)->str);
|
---|
862 | }
|
---|
863 |
|
---|
864 | static int
|
---|
865 | a_word_hash_cmp (const void *x, const void *y)
|
---|
866 | {
|
---|
867 | int result = ((struct a_word const *) x)->length - ((struct a_word const *) y)->length;
|
---|
868 | if (result)
|
---|
869 | return result;
|
---|
870 | return_STRING_COMPARE (((struct a_word const *) x)->str,
|
---|
871 | ((struct a_word const *) y)->str);
|
---|
872 | }
|
---|
873 |
|
---|
874 | struct a_pattern
|
---|
875 | {
|
---|
876 | struct a_pattern *next;
|
---|
877 | char *str;
|
---|
878 | char *percent;
|
---|
879 | int length;
|
---|
880 | int save_c;
|
---|
881 | };
|
---|
882 |
|
---|
883 | static char *
|
---|
884 | func_filter_filterout (char *o, char **argv, const char *funcname)
|
---|
885 | {
|
---|
886 | struct a_word *wordhead;
|
---|
887 | struct a_word **wordtail;
|
---|
888 | struct a_word *wp;
|
---|
889 | struct a_pattern *pathead;
|
---|
890 | struct a_pattern **pattail;
|
---|
891 | struct a_pattern *pp;
|
---|
892 |
|
---|
893 | struct hash_table a_word_table;
|
---|
894 | int is_filter = streq (funcname, "filter");
|
---|
895 | char *pat_iterator = argv[0];
|
---|
896 | char *word_iterator = argv[1];
|
---|
897 | int literals = 0;
|
---|
898 | int words = 0;
|
---|
899 | int hashing = 0;
|
---|
900 | char *p;
|
---|
901 | unsigned int len;
|
---|
902 |
|
---|
903 | /* Chop ARGV[0] up into patterns to match against the words. */
|
---|
904 |
|
---|
905 | pattail = &pathead;
|
---|
906 | while ((p = find_next_token (&pat_iterator, &len)) != 0)
|
---|
907 | {
|
---|
908 | struct a_pattern *pat = (struct a_pattern *) alloca (sizeof (struct a_pattern));
|
---|
909 |
|
---|
910 | *pattail = pat;
|
---|
911 | pattail = &pat->next;
|
---|
912 |
|
---|
913 | if (*pat_iterator != '\0')
|
---|
914 | ++pat_iterator;
|
---|
915 |
|
---|
916 | pat->str = p;
|
---|
917 | pat->length = len;
|
---|
918 | pat->save_c = p[len];
|
---|
919 | p[len] = '\0';
|
---|
920 | pat->percent = find_percent (p);
|
---|
921 | if (pat->percent == 0)
|
---|
922 | literals++;
|
---|
923 | }
|
---|
924 | *pattail = 0;
|
---|
925 |
|
---|
926 | /* Chop ARGV[1] up into words to match against the patterns. */
|
---|
927 |
|
---|
928 | wordtail = &wordhead;
|
---|
929 | while ((p = find_next_token (&word_iterator, &len)) != 0)
|
---|
930 | {
|
---|
931 | struct a_word *word = (struct a_word *) alloca (sizeof (struct a_word));
|
---|
932 |
|
---|
933 | *wordtail = word;
|
---|
934 | wordtail = &word->next;
|
---|
935 |
|
---|
936 | if (*word_iterator != '\0')
|
---|
937 | ++word_iterator;
|
---|
938 |
|
---|
939 | p[len] = '\0';
|
---|
940 | word->str = p;
|
---|
941 | word->length = len;
|
---|
942 | word->matched = 0;
|
---|
943 | word->chain = 0;
|
---|
944 | words++;
|
---|
945 | }
|
---|
946 | *wordtail = 0;
|
---|
947 |
|
---|
948 | /* Only use a hash table if arg list lengths justifies the cost. */
|
---|
949 | hashing = (literals >= 2 && (literals * words) >= 10);
|
---|
950 | if (hashing)
|
---|
951 | {
|
---|
952 | hash_init (&a_word_table, words, a_word_hash_1, a_word_hash_2, a_word_hash_cmp);
|
---|
953 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
954 | {
|
---|
955 | struct a_word *owp = hash_insert (&a_word_table, wp);
|
---|
956 | if (owp)
|
---|
957 | wp->chain = owp;
|
---|
958 | }
|
---|
959 | }
|
---|
960 |
|
---|
961 | if (words)
|
---|
962 | {
|
---|
963 | int doneany = 0;
|
---|
964 |
|
---|
965 | /* Run each pattern through the words, killing words. */
|
---|
966 | for (pp = pathead; pp != 0; pp = pp->next)
|
---|
967 | {
|
---|
968 | if (pp->percent)
|
---|
969 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
970 | wp->matched |= pattern_matches (pp->str, pp->percent, wp->str);
|
---|
971 | else if (hashing)
|
---|
972 | {
|
---|
973 | struct a_word a_word_key;
|
---|
974 | a_word_key.str = pp->str;
|
---|
975 | a_word_key.length = pp->length;
|
---|
976 | wp = (struct a_word *) hash_find_item (&a_word_table, &a_word_key);
|
---|
977 | while (wp)
|
---|
978 | {
|
---|
979 | wp->matched |= 1;
|
---|
980 | wp = wp->chain;
|
---|
981 | }
|
---|
982 | }
|
---|
983 | else
|
---|
984 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
985 | wp->matched |= (wp->length == pp->length
|
---|
986 | && strneq (pp->str, wp->str, wp->length));
|
---|
987 | }
|
---|
988 |
|
---|
989 | /* Output the words that matched (or didn't, for filter-out). */
|
---|
990 | for (wp = wordhead; wp != 0; wp = wp->next)
|
---|
991 | if (is_filter ? wp->matched : !wp->matched)
|
---|
992 | {
|
---|
993 | o = variable_buffer_output (o, wp->str, strlen (wp->str));
|
---|
994 | o = variable_buffer_output (o, " ", 1);
|
---|
995 | doneany = 1;
|
---|
996 | }
|
---|
997 |
|
---|
998 | if (doneany)
|
---|
999 | /* Kill the last space. */
|
---|
1000 | --o;
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | for (pp = pathead; pp != 0; pp = pp->next)
|
---|
1004 | pp->str[pp->length] = pp->save_c;
|
---|
1005 |
|
---|
1006 | if (hashing)
|
---|
1007 | hash_free (&a_word_table, 0);
|
---|
1008 |
|
---|
1009 | return o;
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 |
|
---|
1013 | static char *
|
---|
1014 | func_strip (char *o, char **argv, const char *funcname)
|
---|
1015 | {
|
---|
1016 | char *p = argv[0];
|
---|
1017 | int doneany =0;
|
---|
1018 |
|
---|
1019 | while (*p != '\0')
|
---|
1020 | {
|
---|
1021 | int i=0;
|
---|
1022 | char *word_start=0;
|
---|
1023 |
|
---|
1024 | while (isspace ((unsigned char)*p))
|
---|
1025 | ++p;
|
---|
1026 | word_start = p;
|
---|
1027 | for (i=0; *p != '\0' && !isspace ((unsigned char)*p); ++p, ++i)
|
---|
1028 | {}
|
---|
1029 | if (!i)
|
---|
1030 | break;
|
---|
1031 | o = variable_buffer_output (o, word_start, i);
|
---|
1032 | o = variable_buffer_output (o, " ", 1);
|
---|
1033 | doneany = 1;
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | if (doneany)
|
---|
1037 | /* Kill the last space. */
|
---|
1038 | --o;
|
---|
1039 | return o;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | /*
|
---|
1043 | Print a warning or fatal message.
|
---|
1044 | */
|
---|
1045 | static char *
|
---|
1046 | func_error (char *o, char **argv, const char *funcname)
|
---|
1047 | {
|
---|
1048 | char **argvp;
|
---|
1049 | char *msg, *p;
|
---|
1050 | int len;
|
---|
1051 |
|
---|
1052 | /* The arguments will be broken on commas. Rather than create yet
|
---|
1053 | another special case where function arguments aren't broken up,
|
---|
1054 | just create a format string that puts them back together. */
|
---|
1055 | for (len=0, argvp=argv; *argvp != 0; ++argvp)
|
---|
1056 | len += strlen (*argvp) + 2;
|
---|
1057 |
|
---|
1058 | p = msg = (char *) alloca (len + 1);
|
---|
1059 |
|
---|
1060 | for (argvp=argv; argvp[1] != 0; ++argvp)
|
---|
1061 | {
|
---|
1062 | strcpy (p, *argvp);
|
---|
1063 | p += strlen (*argvp);
|
---|
1064 | *(p++) = ',';
|
---|
1065 | *(p++) = ' ';
|
---|
1066 | }
|
---|
1067 | strcpy (p, *argvp);
|
---|
1068 |
|
---|
1069 | if (*funcname == 'e')
|
---|
1070 | fatal (reading_file, "%s", msg);
|
---|
1071 |
|
---|
1072 | /* The warning function expands to the empty string. */
|
---|
1073 | error (reading_file, "%s", msg);
|
---|
1074 |
|
---|
1075 | return o;
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 |
|
---|
1079 | /*
|
---|
1080 | chop argv[0] into words, and sort them.
|
---|
1081 | */
|
---|
1082 | static char *
|
---|
1083 | func_sort (char *o, char **argv, const char *funcname)
|
---|
1084 | {
|
---|
1085 | char **words = 0;
|
---|
1086 | int nwords = 0;
|
---|
1087 | register int wordi = 0;
|
---|
1088 |
|
---|
1089 | /* Chop ARGV[0] into words and put them in WORDS. */
|
---|
1090 | char *t = argv[0];
|
---|
1091 | char *p;
|
---|
1092 | unsigned int len;
|
---|
1093 | int i;
|
---|
1094 |
|
---|
1095 | while ((p = find_next_token (&t, &len)) != 0)
|
---|
1096 | {
|
---|
1097 | if (wordi >= nwords - 1)
|
---|
1098 | {
|
---|
1099 | nwords = (2 * nwords) + 5;
|
---|
1100 | words = (char **) xrealloc ((char *) words,
|
---|
1101 | nwords * sizeof (char *));
|
---|
1102 | }
|
---|
1103 | words[wordi++] = savestring (p, len);
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | if (!wordi)
|
---|
1107 | return o;
|
---|
1108 |
|
---|
1109 | /* Now sort the list of words. */
|
---|
1110 | qsort ((char *) words, wordi, sizeof (char *), alpha_compare);
|
---|
1111 |
|
---|
1112 | /* Now write the sorted list. */
|
---|
1113 | for (i = 0; i < wordi; ++i)
|
---|
1114 | {
|
---|
1115 | len = strlen (words[i]);
|
---|
1116 | if (i == wordi - 1 || strlen (words[i + 1]) != len
|
---|
1117 | || strcmp (words[i], words[i + 1]))
|
---|
1118 | {
|
---|
1119 | o = variable_buffer_output (o, words[i], len);
|
---|
1120 | o = variable_buffer_output (o, " ", 1);
|
---|
1121 | }
|
---|
1122 | free (words[i]);
|
---|
1123 | }
|
---|
1124 | /* Kill the last space. */
|
---|
1125 | --o;
|
---|
1126 |
|
---|
1127 | free (words);
|
---|
1128 |
|
---|
1129 | return o;
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | /*
|
---|
1133 | $(if condition,true-part[,false-part])
|
---|
1134 |
|
---|
1135 | CONDITION is false iff it evaluates to an empty string. White
|
---|
1136 | space before and after condition are stripped before evaluation.
|
---|
1137 |
|
---|
1138 | If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is
|
---|
1139 | evaluated (if it exists). Because only one of the two PARTs is evaluated,
|
---|
1140 | you can use $(if ...) to create side-effects (with $(shell ...), for
|
---|
1141 | example).
|
---|
1142 | */
|
---|
1143 |
|
---|
1144 | static char *
|
---|
1145 | func_if (char *o, char **argv, const char *funcname)
|
---|
1146 | {
|
---|
1147 | const char *begp = argv[0];
|
---|
1148 | const char *endp = begp + strlen (argv[0]) - 1;
|
---|
1149 | int result = 0;
|
---|
1150 |
|
---|
1151 | /* Find the result of the condition: if we have a value, and it's not
|
---|
1152 | empty, the condition is true. If we don't have a value, or it's the
|
---|
1153 | empty string, then it's false. */
|
---|
1154 |
|
---|
1155 | strip_whitespace (&begp, &endp);
|
---|
1156 |
|
---|
1157 | if (begp <= endp)
|
---|
1158 | {
|
---|
1159 | char *expansion = expand_argument (begp, endp+1);
|
---|
1160 |
|
---|
1161 | result = strlen (expansion);
|
---|
1162 | free (expansion);
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | /* If the result is true (1) we want to eval the first argument, and if
|
---|
1166 | it's false (0) we want to eval the second. If the argument doesn't
|
---|
1167 | exist we do nothing, otherwise expand it and add to the buffer. */
|
---|
1168 |
|
---|
1169 | argv += 1 + !result;
|
---|
1170 |
|
---|
1171 | if (argv[0])
|
---|
1172 | {
|
---|
1173 | char *expansion;
|
---|
1174 |
|
---|
1175 | expansion = expand_argument (argv[0], NULL);
|
---|
1176 |
|
---|
1177 | o = variable_buffer_output (o, expansion, strlen (expansion));
|
---|
1178 |
|
---|
1179 | free (expansion);
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | return o;
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | static char *
|
---|
1186 | func_wildcard (char *o, char **argv, const char *funcname)
|
---|
1187 | {
|
---|
1188 |
|
---|
1189 | #ifdef _AMIGA
|
---|
1190 | o = wildcard_expansion (argv[0], o);
|
---|
1191 | #else
|
---|
1192 | char *p = string_glob (argv[0]);
|
---|
1193 | o = variable_buffer_output (o, p, strlen (p));
|
---|
1194 | #endif
|
---|
1195 | return o;
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | /*
|
---|
1199 | $(eval <makefile string>)
|
---|
1200 |
|
---|
1201 | Always resolves to the empty string.
|
---|
1202 |
|
---|
1203 | Treat the arguments as a segment of makefile, and parse them.
|
---|
1204 | */
|
---|
1205 |
|
---|
1206 | static char *
|
---|
1207 | func_eval (char *o, char **argv, const char *funcname)
|
---|
1208 | {
|
---|
1209 | char *buf;
|
---|
1210 | unsigned int len;
|
---|
1211 |
|
---|
1212 | /* Eval the buffer. Pop the current variable buffer setting so that the
|
---|
1213 | eval'd code can use its own without conflicting. */
|
---|
1214 |
|
---|
1215 | install_variable_buffer (&buf, &len);
|
---|
1216 |
|
---|
1217 | eval_buffer (argv[0]);
|
---|
1218 |
|
---|
1219 | restore_variable_buffer (buf, len);
|
---|
1220 |
|
---|
1221 | return o;
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 |
|
---|
1225 | static char *
|
---|
1226 | func_value (char *o, char **argv, const char *funcname)
|
---|
1227 | {
|
---|
1228 | /* Look up the variable. */
|
---|
1229 | struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
---|
1230 |
|
---|
1231 | /* Copy its value into the output buffer without expanding it. */
|
---|
1232 | if (v)
|
---|
1233 | o = variable_buffer_output (o, v->value, strlen(v->value));
|
---|
1234 |
|
---|
1235 | return o;
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | /*
|
---|
1239 | \r is replaced on UNIX as well. Is this desirable?
|
---|
1240 | */
|
---|
1241 | void
|
---|
1242 | fold_newlines (char *buffer, int *length)
|
---|
1243 | {
|
---|
1244 | char *dst = buffer;
|
---|
1245 | char *src = buffer;
|
---|
1246 | char *last_nonnl = buffer -1;
|
---|
1247 | src[*length] = 0;
|
---|
1248 | for (; *src != '\0'; ++src)
|
---|
1249 | {
|
---|
1250 | if (src[0] == '\r' && src[1] == '\n')
|
---|
1251 | continue;
|
---|
1252 | if (*src == '\n')
|
---|
1253 | {
|
---|
1254 | *dst++ = ' ';
|
---|
1255 | }
|
---|
1256 | else
|
---|
1257 | {
|
---|
1258 | last_nonnl = dst;
|
---|
1259 | *dst++ = *src;
|
---|
1260 | }
|
---|
1261 | }
|
---|
1262 | *(++last_nonnl) = '\0';
|
---|
1263 | *length = last_nonnl - buffer;
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 |
|
---|
1267 |
|
---|
1268 | int shell_function_pid = 0, shell_function_completed;
|
---|
1269 |
|
---|
1270 |
|
---|
1271 | #ifdef WINDOWS32
|
---|
1272 | /*untested*/
|
---|
1273 |
|
---|
1274 | #include <windows.h>
|
---|
1275 | #include <io.h>
|
---|
1276 | #include "sub_proc.h"
|
---|
1277 |
|
---|
1278 |
|
---|
1279 | void
|
---|
1280 | windows32_openpipe (int *pipedes, int *pid_p, char **command_argv, char **envp)
|
---|
1281 | {
|
---|
1282 | SECURITY_ATTRIBUTES saAttr;
|
---|
1283 | HANDLE hIn;
|
---|
1284 | HANDLE hErr;
|
---|
1285 | HANDLE hChildOutRd;
|
---|
1286 | HANDLE hChildOutWr;
|
---|
1287 | HANDLE hProcess;
|
---|
1288 |
|
---|
1289 |
|
---|
1290 | saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
|
---|
1291 | saAttr.bInheritHandle = TRUE;
|
---|
1292 | saAttr.lpSecurityDescriptor = NULL;
|
---|
1293 |
|
---|
1294 | if (DuplicateHandle (GetCurrentProcess(),
|
---|
1295 | GetStdHandle(STD_INPUT_HANDLE),
|
---|
1296 | GetCurrentProcess(),
|
---|
1297 | &hIn,
|
---|
1298 | 0,
|
---|
1299 | TRUE,
|
---|
1300 | DUPLICATE_SAME_ACCESS) == FALSE) {
|
---|
1301 | fatal (NILF, _("create_child_process: DuplicateHandle(In) failed (e=%d)\n"),
|
---|
1302 | GetLastError());
|
---|
1303 |
|
---|
1304 | }
|
---|
1305 | if (DuplicateHandle(GetCurrentProcess(),
|
---|
1306 | GetStdHandle(STD_ERROR_HANDLE),
|
---|
1307 | GetCurrentProcess(),
|
---|
1308 | &hErr,
|
---|
1309 | 0,
|
---|
1310 | TRUE,
|
---|
1311 | DUPLICATE_SAME_ACCESS) == FALSE) {
|
---|
1312 | fatal (NILF, _("create_child_process: DuplicateHandle(Err) failed (e=%d)\n"),
|
---|
1313 | GetLastError());
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | if (!CreatePipe(&hChildOutRd, &hChildOutWr, &saAttr, 0))
|
---|
1317 | fatal (NILF, _("CreatePipe() failed (e=%d)\n"), GetLastError());
|
---|
1318 |
|
---|
1319 | hProcess = process_init_fd(hIn, hChildOutWr, hErr);
|
---|
1320 |
|
---|
1321 | if (!hProcess)
|
---|
1322 | fatal (NILF, _("windows32_openpipe (): process_init_fd() failed\n"));
|
---|
1323 |
|
---|
1324 | /* make sure that CreateProcess() has Path it needs */
|
---|
1325 | sync_Path_environment();
|
---|
1326 |
|
---|
1327 | if (!process_begin(hProcess, command_argv, envp, command_argv[0], NULL)) {
|
---|
1328 | /* register process for wait */
|
---|
1329 | process_register(hProcess);
|
---|
1330 |
|
---|
1331 | /* set the pid for returning to caller */
|
---|
1332 | *pid_p = (int) hProcess;
|
---|
1333 |
|
---|
1334 | /* set up to read data from child */
|
---|
1335 | pipedes[0] = _open_osfhandle((long) hChildOutRd, O_RDONLY);
|
---|
1336 |
|
---|
1337 | /* this will be closed almost right away */
|
---|
1338 | pipedes[1] = _open_osfhandle((long) hChildOutWr, O_APPEND);
|
---|
1339 | } else {
|
---|
1340 | /* reap/cleanup the failed process */
|
---|
1341 | process_cleanup(hProcess);
|
---|
1342 |
|
---|
1343 | /* close handles which were duplicated, they weren't used */
|
---|
1344 | CloseHandle(hIn);
|
---|
1345 | CloseHandle(hErr);
|
---|
1346 |
|
---|
1347 | /* close pipe handles, they won't be used */
|
---|
1348 | CloseHandle(hChildOutRd);
|
---|
1349 | CloseHandle(hChildOutWr);
|
---|
1350 |
|
---|
1351 | /* set status for return */
|
---|
1352 | pipedes[0] = pipedes[1] = -1;
|
---|
1353 | *pid_p = -1;
|
---|
1354 | }
|
---|
1355 | }
|
---|
1356 | #endif
|
---|
1357 |
|
---|
1358 |
|
---|
1359 | #ifdef __MSDOS__
|
---|
1360 | FILE *
|
---|
1361 | msdos_openpipe (int* pipedes, int *pidp, char *text)
|
---|
1362 | {
|
---|
1363 | FILE *fpipe=0;
|
---|
1364 | /* MSDOS can't fork, but it has `popen'. */
|
---|
1365 | struct variable *sh = lookup_variable ("SHELL", 5);
|
---|
1366 | int e;
|
---|
1367 | extern int dos_command_running, dos_status;
|
---|
1368 |
|
---|
1369 | /* Make sure not to bother processing an empty line. */
|
---|
1370 | while (isblank ((unsigned char)*text))
|
---|
1371 | ++text;
|
---|
1372 | if (*text == '\0')
|
---|
1373 | return 0;
|
---|
1374 |
|
---|
1375 | if (sh)
|
---|
1376 | {
|
---|
1377 | char buf[PATH_MAX + 7];
|
---|
1378 | /* This makes sure $SHELL value is used by $(shell), even
|
---|
1379 | though the target environment is not passed to it. */
|
---|
1380 | sprintf (buf, "SHELL=%s", sh->value);
|
---|
1381 | putenv (buf);
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 | e = errno;
|
---|
1385 | errno = 0;
|
---|
1386 | dos_command_running = 1;
|
---|
1387 | dos_status = 0;
|
---|
1388 | /* If dos_status becomes non-zero, it means the child process
|
---|
1389 | was interrupted by a signal, like SIGINT or SIGQUIT. See
|
---|
1390 | fatal_error_signal in commands.c. */
|
---|
1391 | fpipe = popen (text, "rt");
|
---|
1392 | dos_command_running = 0;
|
---|
1393 | if (!fpipe || dos_status)
|
---|
1394 | {
|
---|
1395 | pipedes[0] = -1;
|
---|
1396 | *pidp = -1;
|
---|
1397 | if (dos_status)
|
---|
1398 | errno = EINTR;
|
---|
1399 | else if (errno == 0)
|
---|
1400 | errno = ENOMEM;
|
---|
1401 | shell_function_completed = -1;
|
---|
1402 | }
|
---|
1403 | else
|
---|
1404 | {
|
---|
1405 | pipedes[0] = fileno (fpipe);
|
---|
1406 | *pidp = 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
|
---|
1407 | errno = e;
|
---|
1408 | shell_function_completed = 1;
|
---|
1409 | }
|
---|
1410 | return fpipe;
|
---|
1411 | }
|
---|
1412 | #endif
|
---|
1413 |
|
---|
1414 | /*
|
---|
1415 | Do shell spawning, with the naughty bits for different OSes.
|
---|
1416 | */
|
---|
1417 |
|
---|
1418 | #ifdef VMS
|
---|
1419 |
|
---|
1420 | /* VMS can't do $(shell ...) */
|
---|
1421 | #define func_shell 0
|
---|
1422 |
|
---|
1423 | #else
|
---|
1424 | #ifndef _AMIGA
|
---|
1425 | static char *
|
---|
1426 | func_shell (char *o, char **argv, const char *funcname)
|
---|
1427 | {
|
---|
1428 | char* batch_filename = NULL;
|
---|
1429 | int i;
|
---|
1430 |
|
---|
1431 | #ifdef __MSDOS__
|
---|
1432 | FILE *fpipe;
|
---|
1433 | #endif
|
---|
1434 | char **command_argv;
|
---|
1435 | char *error_prefix;
|
---|
1436 | char **envp;
|
---|
1437 | int pipedes[2];
|
---|
1438 | int pid;
|
---|
1439 |
|
---|
1440 | #ifndef __MSDOS__
|
---|
1441 | /* Construct the argument list. */
|
---|
1442 | command_argv = construct_command_argv (argv[0],
|
---|
1443 | (char **) NULL, (struct file *) 0,
|
---|
1444 | &batch_filename);
|
---|
1445 | if (command_argv == 0)
|
---|
1446 | return o;
|
---|
1447 | #endif
|
---|
1448 |
|
---|
1449 | /* Using a target environment for `shell' loses in cases like:
|
---|
1450 | export var = $(shell echo foobie)
|
---|
1451 | because target_environment hits a loop trying to expand $(var)
|
---|
1452 | to put it in the environment. This is even more confusing when
|
---|
1453 | var was not explicitly exported, but just appeared in the
|
---|
1454 | calling environment. */
|
---|
1455 |
|
---|
1456 | envp = environ;
|
---|
1457 |
|
---|
1458 | /* For error messages. */
|
---|
1459 | if (reading_file != 0)
|
---|
1460 | {
|
---|
1461 | error_prefix = (char *) alloca (strlen (reading_file->filenm)+11+4);
|
---|
1462 | sprintf (error_prefix,
|
---|
1463 | "%s:%lu: ", reading_file->filenm, reading_file->lineno);
|
---|
1464 | }
|
---|
1465 | else
|
---|
1466 | error_prefix = "";
|
---|
1467 |
|
---|
1468 | #ifdef WINDOWS32
|
---|
1469 |
|
---|
1470 | windows32_openpipe (pipedes, &pid, command_argv, envp);
|
---|
1471 |
|
---|
1472 | if (pipedes[0] < 0) {
|
---|
1473 | /* open of the pipe failed, mark as failed execution */
|
---|
1474 | shell_function_completed = -1;
|
---|
1475 |
|
---|
1476 | return o;
|
---|
1477 | } else
|
---|
1478 |
|
---|
1479 | #elif defined(__MSDOS__)
|
---|
1480 |
|
---|
1481 | fpipe = msdos_openpipe (pipedes, &pid, argv[0]);
|
---|
1482 | if (pipedes[0] < 0)
|
---|
1483 | {
|
---|
1484 | perror_with_name (error_prefix, "pipe");
|
---|
1485 | return o;
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | #else
|
---|
1489 |
|
---|
1490 | if (pipe (pipedes) < 0)
|
---|
1491 | {
|
---|
1492 | perror_with_name (error_prefix, "pipe");
|
---|
1493 | return o;
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | # ifdef __EMX__
|
---|
1497 |
|
---|
1498 | /* close some handles that are unnecessary for the child process */
|
---|
1499 | CLOSE_ON_EXEC(pipedes[1]);
|
---|
1500 | CLOSE_ON_EXEC(pipedes[0]);
|
---|
1501 | /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
|
---|
1502 | pid = child_execute_job (0, pipedes[1], command_argv, envp);
|
---|
1503 | if (pid < 0)
|
---|
1504 | perror_with_name (error_prefix, "spawn");
|
---|
1505 |
|
---|
1506 | # else /* ! __EMX__ */
|
---|
1507 |
|
---|
1508 | pid = vfork ();
|
---|
1509 | if (pid < 0)
|
---|
1510 | perror_with_name (error_prefix, "fork");
|
---|
1511 | else if (pid == 0)
|
---|
1512 | child_execute_job (0, pipedes[1], command_argv, envp);
|
---|
1513 | else
|
---|
1514 |
|
---|
1515 | # endif
|
---|
1516 |
|
---|
1517 | #endif
|
---|
1518 | {
|
---|
1519 | /* We are the parent. */
|
---|
1520 |
|
---|
1521 | char *buffer;
|
---|
1522 | unsigned int maxlen;
|
---|
1523 | int cc;
|
---|
1524 |
|
---|
1525 | /* Record the PID for reap_children. */
|
---|
1526 | shell_function_pid = pid;
|
---|
1527 | #ifndef __MSDOS__
|
---|
1528 | shell_function_completed = 0;
|
---|
1529 |
|
---|
1530 | /* Free the storage only the child needed. */
|
---|
1531 | free (command_argv[0]);
|
---|
1532 | free ((char *) command_argv);
|
---|
1533 |
|
---|
1534 | /* Close the write side of the pipe. */
|
---|
1535 | (void) close (pipedes[1]);
|
---|
1536 | #endif
|
---|
1537 |
|
---|
1538 | /* Set up and read from the pipe. */
|
---|
1539 |
|
---|
1540 | maxlen = 200;
|
---|
1541 | buffer = (char *) xmalloc (maxlen + 1);
|
---|
1542 |
|
---|
1543 | /* Read from the pipe until it gets EOF. */
|
---|
1544 | for (i = 0; ; i += cc)
|
---|
1545 | {
|
---|
1546 | if (i == maxlen)
|
---|
1547 | {
|
---|
1548 | maxlen += 512;
|
---|
1549 | buffer = (char *) xrealloc (buffer, maxlen + 1);
|
---|
1550 | }
|
---|
1551 |
|
---|
1552 | EINTRLOOP (cc, read (pipedes[0], &buffer[i], maxlen - i));
|
---|
1553 | if (cc <= 0)
|
---|
1554 | break;
|
---|
1555 | }
|
---|
1556 | buffer[i] = '\0';
|
---|
1557 |
|
---|
1558 | /* Close the read side of the pipe. */
|
---|
1559 | #ifdef __MSDOS__
|
---|
1560 | if (fpipe)
|
---|
1561 | (void) pclose (fpipe);
|
---|
1562 | #else
|
---|
1563 | (void) close (pipedes[0]);
|
---|
1564 | #endif
|
---|
1565 |
|
---|
1566 | /* Loop until child_handler or reap_children() sets
|
---|
1567 | shell_function_completed to the status of our child shell. */
|
---|
1568 | while (shell_function_completed == 0)
|
---|
1569 | reap_children (1, 0);
|
---|
1570 |
|
---|
1571 | if (batch_filename) {
|
---|
1572 | DB (DB_VERBOSE, (_("Cleaning up temporary batch file %s\n"),
|
---|
1573 | batch_filename));
|
---|
1574 | remove (batch_filename);
|
---|
1575 | free (batch_filename);
|
---|
1576 | }
|
---|
1577 | shell_function_pid = 0;
|
---|
1578 |
|
---|
1579 | /* The child_handler function will set shell_function_completed
|
---|
1580 | to 1 when the child dies normally, or to -1 if it
|
---|
1581 | dies with status 127, which is most likely an exec fail. */
|
---|
1582 |
|
---|
1583 | if (shell_function_completed == -1)
|
---|
1584 | {
|
---|
1585 | /* This most likely means that the execvp failed,
|
---|
1586 | so we should just write out the error message
|
---|
1587 | that came in over the pipe from the child. */
|
---|
1588 | fputs (buffer, stderr);
|
---|
1589 | fflush (stderr);
|
---|
1590 | }
|
---|
1591 | else
|
---|
1592 | {
|
---|
1593 | /* The child finished normally. Replace all
|
---|
1594 | newlines in its output with spaces, and put
|
---|
1595 | that in the variable output buffer. */
|
---|
1596 | fold_newlines (buffer, &i);
|
---|
1597 | o = variable_buffer_output (o, buffer, i);
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | free (buffer);
|
---|
1601 | }
|
---|
1602 |
|
---|
1603 | return o;
|
---|
1604 | }
|
---|
1605 |
|
---|
1606 | #else /* _AMIGA */
|
---|
1607 |
|
---|
1608 | /* Do the Amiga version of func_shell. */
|
---|
1609 |
|
---|
1610 | static char *
|
---|
1611 | func_shell (char *o, char **argv, const char *funcname)
|
---|
1612 | {
|
---|
1613 | /* Amiga can't fork nor spawn, but I can start a program with
|
---|
1614 | redirection of my choice. However, this means that we
|
---|
1615 | don't have an opportunity to reopen stdout to trap it. Thus,
|
---|
1616 | we save our own stdout onto a new descriptor and dup a temp
|
---|
1617 | file's descriptor onto our stdout temporarily. After we
|
---|
1618 | spawn the shell program, we dup our own stdout back to the
|
---|
1619 | stdout descriptor. The buffer reading is the same as above,
|
---|
1620 | except that we're now reading from a file. */
|
---|
1621 |
|
---|
1622 | #include <dos/dos.h>
|
---|
1623 | #include <proto/dos.h>
|
---|
1624 |
|
---|
1625 | BPTR child_stdout;
|
---|
1626 | char tmp_output[FILENAME_MAX];
|
---|
1627 | unsigned int maxlen = 200;
|
---|
1628 | int cc, i;
|
---|
1629 | char * buffer, * ptr;
|
---|
1630 | char ** aptr;
|
---|
1631 | int len = 0;
|
---|
1632 | char* batch_filename = NULL;
|
---|
1633 |
|
---|
1634 | /* Construct the argument list. */
|
---|
1635 | command_argv = construct_command_argv (argv[0], (char **) NULL,
|
---|
1636 | (struct file *) 0, &batch_filename);
|
---|
1637 | if (command_argv == 0)
|
---|
1638 | return o;
|
---|
1639 |
|
---|
1640 | /* Note the mktemp() is a security hole, but this only runs on Amiga.
|
---|
1641 | Ideally we would use main.c:open_tmpfile(), but this uses a special
|
---|
1642 | Open(), not fopen(), and I'm not familiar enough with the code to mess
|
---|
1643 | with it. */
|
---|
1644 | strcpy (tmp_output, "t:MakeshXXXXXXXX");
|
---|
1645 | mktemp (tmp_output);
|
---|
1646 | child_stdout = Open (tmp_output, MODE_NEWFILE);
|
---|
1647 |
|
---|
1648 | for (aptr=command_argv; *aptr; aptr++)
|
---|
1649 | len += strlen (*aptr) + 1;
|
---|
1650 |
|
---|
1651 | buffer = xmalloc (len + 1);
|
---|
1652 | ptr = buffer;
|
---|
1653 |
|
---|
1654 | for (aptr=command_argv; *aptr; aptr++)
|
---|
1655 | {
|
---|
1656 | strcpy (ptr, *aptr);
|
---|
1657 | ptr += strlen (ptr) + 1;
|
---|
1658 | *ptr ++ = ' ';
|
---|
1659 | *ptr = 0;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | ptr[-1] = '\n';
|
---|
1663 |
|
---|
1664 | Execute (buffer, NULL, child_stdout);
|
---|
1665 | free (buffer);
|
---|
1666 |
|
---|
1667 | Close (child_stdout);
|
---|
1668 |
|
---|
1669 | child_stdout = Open (tmp_output, MODE_OLDFILE);
|
---|
1670 |
|
---|
1671 | buffer = xmalloc (maxlen);
|
---|
1672 | i = 0;
|
---|
1673 | do
|
---|
1674 | {
|
---|
1675 | if (i == maxlen)
|
---|
1676 | {
|
---|
1677 | maxlen += 512;
|
---|
1678 | buffer = (char *) xrealloc (buffer, maxlen + 1);
|
---|
1679 | }
|
---|
1680 |
|
---|
1681 | cc = Read (child_stdout, &buffer[i], maxlen - i);
|
---|
1682 | if (cc > 0)
|
---|
1683 | i += cc;
|
---|
1684 | } while (cc > 0);
|
---|
1685 |
|
---|
1686 | Close (child_stdout);
|
---|
1687 |
|
---|
1688 | fold_newlines (buffer, &i);
|
---|
1689 | o = variable_buffer_output (o, buffer, i);
|
---|
1690 | free (buffer);
|
---|
1691 | return o;
|
---|
1692 | }
|
---|
1693 | #endif /* _AMIGA */
|
---|
1694 | #endif /* !VMS */
|
---|
1695 |
|
---|
1696 | #ifdef EXPERIMENTAL
|
---|
1697 |
|
---|
1698 | /*
|
---|
1699 | equality. Return is string-boolean, ie, the empty string is false.
|
---|
1700 | */
|
---|
1701 | static char *
|
---|
1702 | func_eq (char* o, char **argv, char *funcname)
|
---|
1703 | {
|
---|
1704 | int result = ! strcmp (argv[0], argv[1]);
|
---|
1705 | o = variable_buffer_output (o, result ? "1" : "", result);
|
---|
1706 | return o;
|
---|
1707 | }
|
---|
1708 |
|
---|
1709 |
|
---|
1710 | /*
|
---|
1711 | string-boolean not operator.
|
---|
1712 | */
|
---|
1713 | static char *
|
---|
1714 | func_not (char* o, char **argv, char *funcname)
|
---|
1715 | {
|
---|
1716 | char * s = argv[0];
|
---|
1717 | int result = 0;
|
---|
1718 | while (isspace ((unsigned char)*s))
|
---|
1719 | s++;
|
---|
1720 | result = ! (*s);
|
---|
1721 | o = variable_buffer_output (o, result ? "1" : "", result);
|
---|
1722 | return o;
|
---|
1723 | }
|
---|
1724 | #endif
|
---|
1725 | |
---|
1726 |
|
---|
1727 |
|
---|
1728 | /* Lookup table for builtin functions.
|
---|
1729 |
|
---|
1730 | This doesn't have to be sorted; we use a straight lookup. We might gain
|
---|
1731 | some efficiency by moving most often used functions to the start of the
|
---|
1732 | table.
|
---|
1733 |
|
---|
1734 | If MAXIMUM_ARGS is 0, that means there is no maximum and all
|
---|
1735 | comma-separated values are treated as arguments.
|
---|
1736 |
|
---|
1737 | EXPAND_ARGS means that all arguments should be expanded before invocation.
|
---|
1738 | Functions that do namespace tricks (foreach) don't automatically expand. */
|
---|
1739 |
|
---|
1740 | static char *func_call PARAMS ((char *o, char **argv, const char *funcname));
|
---|
1741 |
|
---|
1742 |
|
---|
1743 | static struct function_table_entry function_table_init[] =
|
---|
1744 | {
|
---|
1745 | /* Name/size */ /* MIN MAX EXP? Function */
|
---|
1746 | { STRING_SIZE_TUPLE("addprefix"), 2, 2, 1, func_addsuffix_addprefix},
|
---|
1747 | { STRING_SIZE_TUPLE("addsuffix"), 2, 2, 1, func_addsuffix_addprefix},
|
---|
1748 | { STRING_SIZE_TUPLE("basename"), 0, 1, 1, func_basename_dir},
|
---|
1749 | { STRING_SIZE_TUPLE("dir"), 0, 1, 1, func_basename_dir},
|
---|
1750 | { STRING_SIZE_TUPLE("notdir"), 0, 1, 1, func_notdir_suffix},
|
---|
1751 | { STRING_SIZE_TUPLE("subst"), 3, 3, 1, func_subst},
|
---|
1752 | { STRING_SIZE_TUPLE("suffix"), 0, 1, 1, func_notdir_suffix},
|
---|
1753 | { STRING_SIZE_TUPLE("filter"), 2, 2, 1, func_filter_filterout},
|
---|
1754 | { STRING_SIZE_TUPLE("filter-out"), 2, 2, 1, func_filter_filterout},
|
---|
1755 | { STRING_SIZE_TUPLE("findstring"), 2, 2, 1, func_findstring},
|
---|
1756 | { STRING_SIZE_TUPLE("firstword"), 0, 1, 1, func_firstword},
|
---|
1757 | { STRING_SIZE_TUPLE("join"), 2, 2, 1, func_join},
|
---|
1758 | { STRING_SIZE_TUPLE("patsubst"), 3, 3, 1, func_patsubst},
|
---|
1759 | { STRING_SIZE_TUPLE("shell"), 0, 1, 1, func_shell},
|
---|
1760 | { STRING_SIZE_TUPLE("sort"), 0, 1, 1, func_sort},
|
---|
1761 | { STRING_SIZE_TUPLE("strip"), 0, 1, 1, func_strip},
|
---|
1762 | { STRING_SIZE_TUPLE("wildcard"), 0, 1, 1, func_wildcard},
|
---|
1763 | { STRING_SIZE_TUPLE("word"), 2, 2, 1, func_word},
|
---|
1764 | { STRING_SIZE_TUPLE("wordlist"), 3, 3, 1, func_wordlist},
|
---|
1765 | { STRING_SIZE_TUPLE("words"), 0, 1, 1, func_words},
|
---|
1766 | { STRING_SIZE_TUPLE("origin"), 0, 1, 1, func_origin},
|
---|
1767 | { STRING_SIZE_TUPLE("foreach"), 3, 3, 0, func_foreach},
|
---|
1768 | { STRING_SIZE_TUPLE("call"), 1, 0, 1, func_call},
|
---|
1769 | { STRING_SIZE_TUPLE("error"), 0, 1, 1, func_error},
|
---|
1770 | { STRING_SIZE_TUPLE("warning"), 0, 1, 1, func_error},
|
---|
1771 | { STRING_SIZE_TUPLE("if"), 2, 3, 0, func_if},
|
---|
1772 | { STRING_SIZE_TUPLE("value"), 0, 1, 1, func_value},
|
---|
1773 | { STRING_SIZE_TUPLE("eval"), 0, 1, 1, func_eval},
|
---|
1774 | #ifdef EXPERIMENTAL
|
---|
1775 | { STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq},
|
---|
1776 | { STRING_SIZE_TUPLE("not"), 0, 1, 1, func_not},
|
---|
1777 | #endif
|
---|
1778 | };
|
---|
1779 |
|
---|
1780 | #define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry))
|
---|
1781 | |
---|
1782 |
|
---|
1783 |
|
---|
1784 | /* These must come after the definition of function_table. */
|
---|
1785 |
|
---|
1786 | static char *
|
---|
1787 | expand_builtin_function (char *o, int argc, char **argv,
|
---|
1788 | const struct function_table_entry *entry_p)
|
---|
1789 | {
|
---|
1790 | if (argc < (int)entry_p->minimum_args)
|
---|
1791 | fatal (reading_file,
|
---|
1792 | _("Insufficient number of arguments (%d) to function `%s'"),
|
---|
1793 | argc, entry_p->name);
|
---|
1794 |
|
---|
1795 | /* I suppose technically some function could do something with no
|
---|
1796 | arguments, but so far none do, so just test it for all functions here
|
---|
1797 | rather than in each one. We can change it later if necessary. */
|
---|
1798 |
|
---|
1799 | if (!argc)
|
---|
1800 | return o;
|
---|
1801 |
|
---|
1802 | if (!entry_p->func_ptr)
|
---|
1803 | fatal (reading_file, _("Unimplemented on this platform: function `%s'"),
|
---|
1804 | entry_p->name);
|
---|
1805 |
|
---|
1806 | return entry_p->func_ptr (o, argv, entry_p->name);
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 | /* Check for a function invocation in *STRINGP. *STRINGP points at the
|
---|
1810 | opening ( or { and is not null-terminated. If a function invocation
|
---|
1811 | is found, expand it into the buffer at *OP, updating *OP, incrementing
|
---|
1812 | *STRINGP past the reference and returning nonzero. If not, return zero. */
|
---|
1813 |
|
---|
1814 | int
|
---|
1815 | handle_function (char **op, char **stringp)
|
---|
1816 | {
|
---|
1817 | const struct function_table_entry *entry_p;
|
---|
1818 | char openparen = (*stringp)[0];
|
---|
1819 | char closeparen = openparen == '(' ? ')' : '}';
|
---|
1820 | char *beg;
|
---|
1821 | char *end;
|
---|
1822 | int count = 0;
|
---|
1823 | register char *p;
|
---|
1824 | char **argv, **argvp;
|
---|
1825 | int nargs;
|
---|
1826 |
|
---|
1827 | beg = *stringp + 1;
|
---|
1828 |
|
---|
1829 | entry_p = lookup_function (beg);
|
---|
1830 |
|
---|
1831 | if (!entry_p)
|
---|
1832 | return 0;
|
---|
1833 |
|
---|
1834 | /* We found a builtin function. Find the beginning of its arguments (skip
|
---|
1835 | whitespace after the name). */
|
---|
1836 |
|
---|
1837 | beg = next_token (beg + entry_p->len);
|
---|
1838 |
|
---|
1839 | /* Find the end of the function invocation, counting nested use of
|
---|
1840 | whichever kind of parens we use. Since we're looking, count commas
|
---|
1841 | to get a rough estimate of how many arguments we might have. The
|
---|
1842 | count might be high, but it'll never be low. */
|
---|
1843 |
|
---|
1844 | for (nargs=1, end=beg; *end != '\0'; ++end)
|
---|
1845 | if (*end == ',')
|
---|
1846 | ++nargs;
|
---|
1847 | else if (*end == openparen)
|
---|
1848 | ++count;
|
---|
1849 | else if (*end == closeparen && --count < 0)
|
---|
1850 | break;
|
---|
1851 |
|
---|
1852 | if (count >= 0)
|
---|
1853 | fatal (reading_file,
|
---|
1854 | _("unterminated call to function `%s': missing `%c'"),
|
---|
1855 | entry_p->name, closeparen);
|
---|
1856 |
|
---|
1857 | *stringp = end;
|
---|
1858 |
|
---|
1859 | /* Get some memory to store the arg pointers. */
|
---|
1860 | argvp = argv = (char **) alloca (sizeof (char *) * (nargs + 2));
|
---|
1861 |
|
---|
1862 | /* Chop the string into arguments, then a nul. As soon as we hit
|
---|
1863 | MAXIMUM_ARGS (if it's >0) assume the rest of the string is part of the
|
---|
1864 | last argument.
|
---|
1865 |
|
---|
1866 | If we're expanding, store pointers to the expansion of each one. If
|
---|
1867 | not, make a duplicate of the string and point into that, nul-terminating
|
---|
1868 | each argument. */
|
---|
1869 |
|
---|
1870 | if (!entry_p->expand_args)
|
---|
1871 | {
|
---|
1872 | int len = end - beg;
|
---|
1873 |
|
---|
1874 | p = xmalloc (len+1);
|
---|
1875 | memcpy (p, beg, len);
|
---|
1876 | p[len] = '\0';
|
---|
1877 | beg = p;
|
---|
1878 | end = beg + len;
|
---|
1879 | }
|
---|
1880 |
|
---|
1881 | for (p=beg, nargs=0; p <= end; ++argvp)
|
---|
1882 | {
|
---|
1883 | char *next;
|
---|
1884 |
|
---|
1885 | ++nargs;
|
---|
1886 |
|
---|
1887 | if (nargs == entry_p->maximum_args
|
---|
1888 | || (! (next = find_next_argument (openparen, closeparen, p, end))))
|
---|
1889 | next = end;
|
---|
1890 |
|
---|
1891 | if (entry_p->expand_args)
|
---|
1892 | *argvp = expand_argument (p, next);
|
---|
1893 | else
|
---|
1894 | {
|
---|
1895 | *argvp = p;
|
---|
1896 | *next = '\0';
|
---|
1897 | }
|
---|
1898 |
|
---|
1899 | p = next + 1;
|
---|
1900 | }
|
---|
1901 | *argvp = NULL;
|
---|
1902 |
|
---|
1903 | /* Finally! Run the function... */
|
---|
1904 | *op = expand_builtin_function (*op, nargs, argv, entry_p);
|
---|
1905 |
|
---|
1906 | /* Free memory. */
|
---|
1907 | if (entry_p->expand_args)
|
---|
1908 | for (argvp=argv; *argvp != 0; ++argvp)
|
---|
1909 | free (*argvp);
|
---|
1910 | else
|
---|
1911 | free (beg);
|
---|
1912 |
|
---|
1913 | return 1;
|
---|
1914 | }
|
---|
1915 | |
---|
1916 |
|
---|
1917 |
|
---|
1918 | /* User-defined functions. Expand the first argument as either a builtin
|
---|
1919 | function or a make variable, in the context of the rest of the arguments
|
---|
1920 | assigned to $1, $2, ... $N. $0 is the name of the function. */
|
---|
1921 |
|
---|
1922 | static char *
|
---|
1923 | func_call (char *o, char **argv, const char *funcname)
|
---|
1924 | {
|
---|
1925 | static int max_args = 0;
|
---|
1926 | char *fname;
|
---|
1927 | char *cp;
|
---|
1928 | char *body;
|
---|
1929 | int flen;
|
---|
1930 | int i;
|
---|
1931 | int saved_args;
|
---|
1932 | const struct function_table_entry *entry_p;
|
---|
1933 | struct variable *v;
|
---|
1934 |
|
---|
1935 | /* There is no way to define a variable with a space in the name, so strip
|
---|
1936 | leading and trailing whitespace as a favor to the user. */
|
---|
1937 | fname = argv[0];
|
---|
1938 | while (*fname != '\0' && isspace ((unsigned char)*fname))
|
---|
1939 | ++fname;
|
---|
1940 |
|
---|
1941 | cp = fname + strlen (fname) - 1;
|
---|
1942 | while (cp > fname && isspace ((unsigned char)*cp))
|
---|
1943 | --cp;
|
---|
1944 | cp[1] = '\0';
|
---|
1945 |
|
---|
1946 | /* Calling nothing is a no-op */
|
---|
1947 | if (*fname == '\0')
|
---|
1948 | return o;
|
---|
1949 |
|
---|
1950 | /* Are we invoking a builtin function? */
|
---|
1951 |
|
---|
1952 | entry_p = lookup_function (fname);
|
---|
1953 |
|
---|
1954 | if (entry_p)
|
---|
1955 | {
|
---|
1956 | /* How many arguments do we have? */
|
---|
1957 | for (i=0; argv[i+1]; ++i)
|
---|
1958 | ;
|
---|
1959 |
|
---|
1960 | return expand_builtin_function (o, i, argv+1, entry_p);
|
---|
1961 | }
|
---|
1962 |
|
---|
1963 | /* Not a builtin, so the first argument is the name of a variable to be
|
---|
1964 | expanded and interpreted as a function. Find it. */
|
---|
1965 | flen = strlen (fname);
|
---|
1966 |
|
---|
1967 | v = lookup_variable (fname, flen);
|
---|
1968 |
|
---|
1969 | if (v == 0)
|
---|
1970 | warn_undefined (fname, flen);
|
---|
1971 |
|
---|
1972 | if (v == 0 || *v->value == '\0')
|
---|
1973 | return o;
|
---|
1974 |
|
---|
1975 | body = (char *) alloca (flen + 4);
|
---|
1976 | body[0] = '$';
|
---|
1977 | body[1] = '(';
|
---|
1978 | memcpy (body + 2, fname, flen);
|
---|
1979 | body[flen+2] = ')';
|
---|
1980 | body[flen+3] = '\0';
|
---|
1981 |
|
---|
1982 | /* Set up arguments $(1) .. $(N). $(0) is the function name. */
|
---|
1983 |
|
---|
1984 | push_new_variable_scope ();
|
---|
1985 |
|
---|
1986 | for (i=0; *argv; ++i, ++argv)
|
---|
1987 | {
|
---|
1988 | char num[11];
|
---|
1989 |
|
---|
1990 | sprintf (num, "%d", i);
|
---|
1991 | define_variable (num, strlen (num), *argv, o_automatic, 0);
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | /* If the number of arguments we have is < max_args, it means we're inside
|
---|
1995 | a recursive invocation of $(call ...). Fill in the remaining arguments
|
---|
1996 | in the new scope with the empty value, to hide them from this
|
---|
1997 | invocation. */
|
---|
1998 |
|
---|
1999 | for (; i < max_args; ++i)
|
---|
2000 | {
|
---|
2001 | char num[11];
|
---|
2002 |
|
---|
2003 | sprintf (num, "%d", i);
|
---|
2004 | define_variable (num, strlen (num), "", o_automatic, 0);
|
---|
2005 | }
|
---|
2006 |
|
---|
2007 | /* Expand the body in the context of the arguments, adding the result to
|
---|
2008 | the variable buffer. */
|
---|
2009 |
|
---|
2010 | v->exp_count = EXP_COUNT_MAX;
|
---|
2011 |
|
---|
2012 | saved_args = max_args;
|
---|
2013 | max_args = i;
|
---|
2014 | o = variable_expand_string (o, body, flen+3);
|
---|
2015 | max_args = saved_args;
|
---|
2016 |
|
---|
2017 | v->exp_count = 0;
|
---|
2018 |
|
---|
2019 | pop_variable_scope ();
|
---|
2020 |
|
---|
2021 | return o + strlen (o);
|
---|
2022 | }
|
---|
2023 |
|
---|
2024 | void
|
---|
2025 | hash_init_function_table (void)
|
---|
2026 | {
|
---|
2027 | hash_init (&function_table, FUNCTION_TABLE_ENTRIES * 2,
|
---|
2028 | function_table_entry_hash_1, function_table_entry_hash_2,
|
---|
2029 | function_table_entry_hash_cmp);
|
---|
2030 | hash_load (&function_table, function_table_init,
|
---|
2031 | FUNCTION_TABLE_ENTRIES, sizeof (struct function_table_entry));
|
---|
2032 | }
|
---|