VirtualBox

source: kBuild/trunk/src/kmk/function.c@ 1140

Last change on this file since 1140 was 1124, checked in by bird, 17 years ago

the strptime part too.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette