* Release 3.77.92.
[make.git] / function.c
blob4047339f13477cae01e0a6a4e18659fbf7b01563
1 /* Builtin function expansion for GNU Make.
2 Copyright (C) 1988,89,91,92,93,94,95,96,97 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include "make.h"
21 #include "filedef.h"
22 #include "variable.h"
23 #include "dep.h"
24 #include "job.h"
25 #include "commands.h"
27 #ifdef _AMIGA
28 #include "amiga.h"
29 #endif
32 struct function_table_entry
34 const char *name;
35 int len;
36 int required_args;
37 int expand_args;
38 char *(*func_ptr) PARAMS((char *output, char **argv, const char*funcname));
42 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
43 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
44 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
45 nonzero, substitutions are done only on matches which are complete
46 whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
47 done only at the ends of whitespace-delimited words. */
49 char *
50 subst_expand (o, text, subst, replace, slen, rlen, by_word, suffix_only)
51 char *o;
52 char *text;
53 char *subst, *replace;
54 unsigned int slen, rlen;
55 int by_word, suffix_only;
57 register char *t = text;
58 register char *p;
60 if (slen == 0 && !by_word && !suffix_only)
62 /* The first occurrence of "" in any string is its end. */
63 o = variable_buffer_output (o, t, strlen (t));
64 if (rlen > 0)
65 o = variable_buffer_output (o, replace, rlen);
66 return o;
71 if ((by_word | suffix_only) && slen == 0)
72 /* When matching by words, the empty string should match
73 the end of each word, rather than the end of the whole text. */
74 p = end_of_token (next_token (t));
75 else
77 p = sindex (t, 0, subst, slen);
78 if (p == 0)
80 /* No more matches. Output everything left on the end. */
81 o = variable_buffer_output (o, t, strlen (t));
82 return o;
86 /* Output everything before this occurrence of the string to replace. */
87 if (p > t)
88 o = variable_buffer_output (o, t, p - t);
90 /* If we're substituting only by fully matched words,
91 or only at the ends of words, check that this case qualifies. */
92 if ((by_word
93 && ((p > t && !isblank (p[-1]))
94 || (p[slen] != '\0' && !isblank (p[slen]))))
95 || (suffix_only
96 && (p[slen] != '\0' && !isblank (p[slen]))))
97 /* Struck out. Output the rest of the string that is
98 no longer to be replaced. */
99 o = variable_buffer_output (o, subst, slen);
100 else if (rlen > 0)
101 /* Output the replacement string. */
102 o = variable_buffer_output (o, replace, rlen);
104 /* Advance T past the string to be replaced. */
105 t = p + slen;
106 } while (*t != '\0');
108 return o;
112 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
113 and replacing strings matching PATTERN with REPLACE.
114 If PATTERN_PERCENT is not nil, PATTERN has already been
115 run through find_percent, and PATTERN_PERCENT is the result.
116 If REPLACE_PERCENT is not nil, REPLACE has already been
117 run through find_percent, and REPLACE_PERCENT is the result. */
119 char *
120 patsubst_expand (o, text, pattern, replace, pattern_percent, replace_percent)
121 char *o;
122 char *text;
123 register char *pattern, *replace;
124 register char *pattern_percent, *replace_percent;
126 unsigned int pattern_prepercent_len, pattern_postpercent_len;
127 unsigned int replace_prepercent_len, replace_postpercent_len = 0;
128 char *t;
129 unsigned int len;
130 int doneany = 0;
132 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
133 will be collapsed before we call subst_expand if PATTERN has no %. */
134 if (replace_percent == 0)
135 replace_percent = find_percent (replace);
136 if (replace_percent != 0)
138 /* Record the length of REPLACE before and after the % so
139 we don't have to compute these lengths more than once. */
140 replace_prepercent_len = replace_percent - replace;
141 replace_postpercent_len = strlen (replace_percent + 1);
143 else
144 /* We store the length of the replacement
145 so we only need to compute it once. */
146 replace_prepercent_len = strlen (replace);
148 if (pattern_percent == 0)
149 pattern_percent = find_percent (pattern);
150 if (pattern_percent == 0)
151 /* With no % in the pattern, this is just a simple substitution. */
152 return subst_expand (o, text, pattern, replace,
153 strlen (pattern), strlen (replace), 1, 0);
155 /* Record the length of PATTERN before and after the %
156 so we don't have to compute it more than once. */
157 pattern_prepercent_len = pattern_percent - pattern;
158 pattern_postpercent_len = strlen (pattern_percent + 1);
160 while ((t = find_next_token (&text, &len)) != 0)
162 int fail = 0;
164 /* Is it big enough to match? */
165 if (len < pattern_prepercent_len + pattern_postpercent_len)
166 fail = 1;
168 /* Does the prefix match? */
169 if (!fail && pattern_prepercent_len > 0
170 && (*t != *pattern
171 || t[pattern_prepercent_len - 1] != pattern_percent[-1]
172 || !strneq (t + 1, pattern + 1, pattern_prepercent_len - 1)))
173 fail = 1;
175 /* Does the suffix match? */
176 if (!fail && pattern_postpercent_len > 0
177 && (t[len - 1] != pattern_percent[pattern_postpercent_len]
178 || t[len - pattern_postpercent_len] != pattern_percent[1]
179 || !strneq (&t[len - pattern_postpercent_len],
180 &pattern_percent[1], pattern_postpercent_len - 1)))
181 fail = 1;
183 if (fail)
184 /* It didn't match. Output the string. */
185 o = variable_buffer_output (o, t, len);
186 else
188 /* It matched. Output the replacement. */
190 /* Output the part of the replacement before the %. */
191 o = variable_buffer_output (o, replace, replace_prepercent_len);
193 if (replace_percent != 0)
195 /* Output the part of the matched string that
196 matched the % in the pattern. */
197 o = variable_buffer_output (o, t + pattern_prepercent_len,
198 len - (pattern_prepercent_len
199 + pattern_postpercent_len));
200 /* Output the part of the replacement after the %. */
201 o = variable_buffer_output (o, replace_percent + 1,
202 replace_postpercent_len);
206 /* Output a space, but not if the replacement is "". */
207 if (fail || replace_prepercent_len > 0
208 || (replace_percent != 0 && len + replace_postpercent_len > 0))
210 o = variable_buffer_output (o, " ", 1);
211 doneany = 1;
214 if (doneany)
215 /* Kill the last space. */
216 --o;
218 return o;
222 /* Look up a function by name.
223 The table is currently small enough that it's not really worthwhile to use
224 a fancier lookup algorithm. If it gets larger, maybe...
227 static const struct function_table_entry *
228 lookup_function (table, s)
229 const struct function_table_entry *table;
230 const char *s;
232 int len = strlen(s);
234 for (; table->name != NULL; ++table)
235 if (table->len <= len
236 && (isblank (s[table->len]) || s[table->len] == '\0')
237 && strneq (s, table->name, table->len))
238 return table;
240 return NULL;
244 /* Return 1 if PATTERN matches STR, 0 if not. */
247 pattern_matches (pattern, percent, str)
248 register char *pattern, *percent, *str;
250 unsigned int sfxlen, strlength;
252 if (percent == 0)
254 unsigned int len = strlen (pattern) + 1;
255 char *new_chars = (char *) alloca (len);
256 bcopy (pattern, new_chars, len);
257 pattern = new_chars;
258 percent = find_percent (pattern);
259 if (percent == 0)
260 return streq (pattern, str);
263 sfxlen = strlen (percent + 1);
264 strlength = strlen (str);
266 if (strlength < (percent - pattern) + sfxlen
267 || !strneq (pattern, str, percent - pattern))
268 return 0;
270 return !strcmp (percent + 1, str + (strlength - sfxlen));
274 /* Find the next comma or ENDPAREN (counting nested STARTPAREN and
275 ENDPARENtheses), starting at PTR before END. Return a pointer to
276 next character.
278 If no next argument is found, return NULL.
281 static char *
282 find_next_argument (startparen, endparen, ptr, end)
283 char startparen;
284 char endparen;
285 const char *ptr;
286 const char *end;
288 int count = 0;
290 for (; ptr < end; ++ptr)
291 if (*ptr == startparen)
292 ++count;
294 else if (*ptr == endparen)
296 --count;
297 if (count < 0)
298 return NULL;
301 else if (*ptr == ',' && !count)
302 return (char *)ptr;
304 /* We didn't find anything. */
305 return NULL;
309 /* Glob-expand LINE. The returned pointer is
310 only good until the next call to string_glob. */
312 static char *
313 string_glob (line)
314 char *line;
316 static char *result = 0;
317 static unsigned int length;
318 register struct nameseq *chain;
319 register unsigned int idx;
321 chain = multi_glob (parse_file_seq
322 (&line, '\0', sizeof (struct nameseq),
323 /* We do not want parse_file_seq to strip `./'s.
324 That would break examples like:
325 $(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
327 sizeof (struct nameseq));
329 if (result == 0)
331 length = 100;
332 result = (char *) xmalloc (100);
335 idx = 0;
336 while (chain != 0)
338 register char *name = chain->name;
339 unsigned int len = strlen (name);
341 struct nameseq *next = chain->next;
342 free ((char *) chain);
343 chain = next;
345 /* multi_glob will pass names without globbing metacharacters
346 through as is, but we want only files that actually exist. */
347 if (file_exists_p (name))
349 if (idx + len + 1 > length)
351 length += (len + 1) * 2;
352 result = (char *) xrealloc (result, length);
354 bcopy (name, &result[idx], len);
355 idx += len;
356 result[idx++] = ' ';
359 free (name);
362 /* Kill the last space and terminate the string. */
363 if (idx == 0)
364 result[0] = '\0';
365 else
366 result[idx - 1] = '\0';
368 return result;
372 Builtin functions
375 static char *
376 func_patsubst (o, argv, funcname)
377 char *o;
378 char **argv;
379 const char *funcname;
381 o = patsubst_expand (o, argv[2], argv[0], argv[1], (char *) 0, (char *) 0);
382 return o;
386 static char *
387 func_join(o, argv, funcname)
388 char *o;
389 char **argv;
390 const char *funcname;
392 int doneany = 0;
394 /* Write each word of the first argument directly followed
395 by the corresponding word of the second argument.
396 If the two arguments have a different number of words,
397 the excess words are just output separated by blanks. */
398 register char *tp;
399 register char *pp;
400 char *list1_iterator = argv[0];
401 char *list2_iterator = argv[1];
404 unsigned int len1, len2;
406 tp = find_next_token (&list1_iterator, &len1);
407 if (tp != 0)
408 o = variable_buffer_output (o, tp, len1);
410 pp = find_next_token (&list2_iterator, &len2);
411 if (pp != 0)
412 o = variable_buffer_output (o, pp, len2);
414 if (tp != 0 || pp != 0)
416 o = variable_buffer_output (o, " ", 1);
417 doneany = 1;
420 while (tp != 0 || pp != 0);
421 if (doneany)
422 /* Kill the last blank. */
423 --o;
425 return o;
429 static char *
430 func_origin(o, argv, funcname)
431 char *o;
432 char **argv;
433 const char *funcname;
435 /* Expand the argument. */
436 register struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
437 if (v == 0)
438 o = variable_buffer_output (o, "undefined", 9);
439 else
440 switch (v->origin)
442 default:
443 case o_invalid:
444 abort ();
445 break;
446 case o_default:
447 o = variable_buffer_output (o, "default", 7);
448 break;
449 case o_env:
450 o = variable_buffer_output (o, "environment", 11);
451 break;
452 case o_file:
453 o = variable_buffer_output (o, "file", 4);
454 break;
455 case o_env_override:
456 o = variable_buffer_output (o, "environment override", 20);
457 break;
458 case o_command:
459 o = variable_buffer_output (o, "command line", 12);
460 break;
461 case o_override:
462 o = variable_buffer_output (o, "override", 8);
463 break;
464 case o_automatic:
465 o = variable_buffer_output (o, "automatic", 9);
466 break;
469 return o;
472 #ifdef VMS
473 #define IS_PATHSEP(c) ((c) == ']')
474 #else
475 #ifdef __MSDOS__
476 #define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
477 #else
478 #define IS_PATHSEP(c) ((c) == '/')
479 #endif
480 #endif
483 static char *
484 func_notdir_suffix(o, argv, funcname)
485 char *o;
486 char **argv;
487 const char *funcname;
489 /* Expand the argument. */
490 char *list_iterator = argv[0];
491 char *p2 =0;
492 int doneany =0;
493 int len=0;
495 int is_suffix = streq(funcname, "suffix");
496 int is_notdir = !is_suffix;
497 while ((p2 = find_next_token (&list_iterator, &len)) != 0)
499 char *p = p2 + len;
502 while (p >= p2 && (!is_suffix || *p != '.'))
504 if (IS_PATHSEP (*p))
505 break;
506 --p;
509 if (p >= p2)
511 if (is_notdir)
512 ++p;
513 else if (*p != '.')
514 continue;
515 o = variable_buffer_output (o, p, len - (p - p2));
517 #if defined(WINDOWS32) || defined(__MSDOS__)
518 /* Handle the case of "d:foo/bar". */
519 else if (streq(funcname, "notdir") && p2[0] && p2[1] == ':')
521 p = p2 + 2;
522 o = variable_buffer_output (o, p, len - (p - p2));
524 #endif
525 else if (is_notdir)
526 o = variable_buffer_output (o, p2, len);
528 if (is_notdir || p >= p2)
530 o = variable_buffer_output (o, " ", 1);
531 doneany = 1;
534 if (doneany)
535 /* Kill last space. */
536 --o;
539 return o;
544 static char *
545 func_basename_dir(o, argv, funcname)
546 char *o;
547 char **argv;
548 const char *funcname;
550 /* Expand the argument. */
551 char *p3 = argv[0];
552 char *p2=0;
553 int doneany=0;
554 int len=0;
555 char *p=0;
556 int is_basename= streq(funcname, "basename");
557 int is_dir= !is_basename;
559 while ((p2 = find_next_token (&p3, &len)) != 0)
561 p = p2 + len;
562 while (p >= p2 && (!is_basename || *p != '.'))
564 if (IS_PATHSEP(*p))
565 break;
566 --p;
569 if (p >= p2 && (is_dir))
570 o = variable_buffer_output (o, p2, ++p - p2);
571 else if (p >= p2 && (*p == '.'))
572 o = variable_buffer_output (o, p2, p - p2);
573 #if defined(WINDOWS32) || defined(__MSDOS__)
574 /* Handle the "d:foobar" case */
575 else if (p2[0] && p2[1] == ':' && is_dir)
576 o = variable_buffer_output (o, p2, 2);
577 #endif
578 else if (is_dir)
579 #ifdef VMS
580 o = variable_buffer_output (o, "[]", 2);
581 #else
582 #ifndef _AMIGA
583 o = variable_buffer_output (o, "./", 2);
584 #else
585 ; /* Just a nop... */
586 #endif /* AMIGA */
587 #endif /* !VMS */
588 else
589 /* The entire name is the basename. */
590 o = variable_buffer_output (o, p2, len);
592 o = variable_buffer_output (o, " ", 1);
593 doneany = 1;
595 if (doneany)
596 /* Kill last space. */
597 --o;
600 return o;
603 static char *
604 func_addsuffix_addprefix(o, argv, funcname)
605 char *o;
606 char **argv;
607 const char *funcname;
609 int fixlen = strlen (argv[0]);
610 char *list_iterator = argv[1];
611 int is_addprefix = streq (funcname, "addprefix");
612 int is_addsuffix = !is_addprefix;
614 int doneany =0;
615 char *p=0;
616 int len =0;
618 while ((p = find_next_token (&list_iterator, &len)) != 0)
620 if (is_addprefix)
621 o = variable_buffer_output (o, argv[0], fixlen);
622 o = variable_buffer_output (o, p, len);
623 if (is_addsuffix)
624 o = variable_buffer_output (o, argv[0], fixlen);
625 o = variable_buffer_output (o, " ", 1);
626 doneany = 1;
629 if (doneany)
630 /* Kill last space. */
631 --o;
633 return o;
636 static char *
637 func_subst(o, argv, funcname)
638 char *o;
639 char **argv;
640 const char *funcname;
642 o = subst_expand (o, argv[2], argv[0], argv[1], strlen (argv[0]),
643 strlen (argv[1]), 0, 0);
645 return o;
649 static char *
650 func_firstword(o, argv, funcname)
651 char *o;
652 char **argv;
653 const char *funcname;
655 int i=0;
656 char *words = argv[0];
657 char *p = find_next_token (&words, &i);
659 if (p != 0)
660 o = variable_buffer_output (o, p, i);
662 return o;
666 static char *
667 func_words(o, argv, funcname)
668 char *o;
669 char **argv;
670 const char *funcname;
672 int i = 0;
673 char *word_iterator = argv[0];
674 char buf[20];
676 while (find_next_token (&word_iterator, (unsigned int *) 0) != 0)
677 ++i;
679 sprintf (buf, "%d", i);
680 o = variable_buffer_output (o, buf, strlen (buf));
683 return o;
686 char *
687 strip_whitespace (begpp, endpp)
688 char **begpp;
689 char **endpp;
691 while (isspace (**begpp) && *begpp <= *endpp)
692 (*begpp) ++;
693 while (isspace (**endpp) && *endpp >= *begpp)
694 (*endpp) --;
695 return *begpp;
699 is_numeric (p)
700 char *p;
702 char *end = p + strlen (p) -1;
703 char *beg = p;
704 strip_whitespace (&p, &end);
705 while (p <= end)
707 if (!isdigit (*p))
708 return 0;
710 p++;
713 return (end - beg >= 0);
716 void
717 check_numeric (s, message)
718 char *s;
719 char *message;
721 if (!is_numeric (s))
722 fatal (reading_file, message);
727 static char *
728 func_word(o, argv, funcname)
729 char *o;
730 char **argv;
731 const char *funcname;
733 char *end_p=0;
734 int i=0;
735 char *p=0;
737 /* Check the first argument. */
738 check_numeric (argv[0], _("non-numeric first argument to `word' function"));
739 i = atoi (argv[0]);
741 if (i == 0)
742 fatal (reading_file, _("the `word' function takes a positive index argument"));
745 end_p = argv[1];
746 while ((p = find_next_token (&end_p, 0)) != 0)
747 if (--i == 0)
748 break;
750 if (i == 0)
751 o = variable_buffer_output (o, p, end_p - p);
753 return o;
756 static char *
757 func_wordlist (o, argv, funcname)
758 char *o;
759 char **argv;
760 const char *funcname;
762 int i=0;
763 int j=0;
765 /* Check the first argument. */
766 check_numeric (argv[0],
767 _("non-numeric first argument to `wordlist' function"));
768 i =atoi(argv[0]);
769 check_numeric (argv[1],
770 _("non-numeric second argument to `wordlist' function"));
772 j = atoi(argv[1]);
776 char *p;
777 char *end_p = argv[2];
779 int start = (i < j) ? i : j;
780 int count = j -i ;
781 if (count < 0)
782 count = - count;
783 count ++;
787 while (((p = find_next_token (&end_p, 0)) != 0) && --start)
789 if (p)
791 while (--count && (find_next_token (&end_p, 0) != 0))
793 o = variable_buffer_output (o, p, end_p - p);
796 return o;
799 static char*
800 func_findstring(o, argv, funcname)
801 char *o;
802 char **argv;
803 const char *funcname;
805 /* Find the first occurrence of the first string in the second. */
806 int i = strlen (argv[0]);
807 if (sindex (argv[1], 0, argv[0], i) != 0)
808 o = variable_buffer_output (o, argv[0], i);
810 return o;
813 static char *
814 func_foreach (o, argv, funcname)
815 char *o;
816 char **argv;
817 const char *funcname;
819 /* expand only the first two. */
820 char *varname = expand_argument (argv[0], argv[1] -1);
821 char *list = expand_argument (argv[1], argv[2] -1);
822 char *body = savestring (argv[2], argv[3] - argv[2] -1 );
824 int len =0;
825 char *list_iterator = list;
826 char *p;
827 register struct variable *var=0;
828 int doneany =0;
830 push_new_variable_scope ();
831 var = define_variable (varname, strlen (varname), "", o_automatic, 0);
833 /* loop through LIST, put the value in VAR and expand BODY */
834 while ((p = find_next_token (&list_iterator, &len)) != 0)
836 char *result = 0;
839 char save = p[len];
841 p[len] = '\0';
842 free (var->value);
843 var->value = (char *) xstrdup ((char*) p);
844 p[len] = save;
847 result = allocated_variable_expand (body);
849 o = variable_buffer_output (o, result, strlen (result));
850 o = variable_buffer_output (o, " ", 1);
851 doneany = 1;
852 free (result);
855 if (doneany)
856 /* Kill the last space. */
857 --o;
859 pop_variable_scope ();
860 free (varname);
861 free (list);
862 free (body);
864 return o;
867 struct a_word
869 struct a_word *next;
870 char *str;
871 int matched;
874 static char *
875 func_filter_filterout (o, argv, funcname)
876 char *o;
877 char **argv;
878 const char *funcname;
880 struct a_word *wordhead =0;
881 struct a_word *wordtail =0;
883 int is_filter = streq (funcname, "filter");
884 char *patterns = argv[0];
887 char *p;
888 int len;
889 char *word_iterator = argv[1];
891 /* Chop ARGV[1] up into words and then run each pattern through. */
892 while ((p = find_next_token (&word_iterator, &len)) != 0)
894 struct a_word *w = (struct a_word *)alloca(sizeof(struct a_word));
895 if (wordhead == 0)
896 wordhead = w;
897 else
898 wordtail->next = w;
899 wordtail = w;
901 if (*word_iterator != '\0')
902 ++word_iterator;
903 p[len] = '\0';
904 w->str = p;
905 w->matched = 0;
908 if (wordhead != 0)
910 struct a_word *wp =0;
911 char *pat_iterator = patterns;
912 int doneany = 0;
914 wordtail->next = 0;
917 /* Run each pattern through the words, killing words. */
918 while ((p = find_next_token (&pat_iterator, &len)) != 0)
920 char *percent;
921 char save = p[len];
922 p[len] = '\0';
924 percent = find_percent (p);
925 for (wp = wordhead; wp != 0; wp = wp->next)
926 wp->matched |= (percent == 0 ? streq (p, wp->str)
927 : pattern_matches (p, percent, wp->str));
929 p[len] = save;
932 /* Output the words that matched (or didn't, for filter-out). */
933 for (wp = wordhead; wp != 0; wp = wp->next)
934 if (is_filter ? wp->matched : !wp->matched)
936 o = variable_buffer_output (o, wp->str, strlen (wp->str));
937 o = variable_buffer_output (o, " ", 1);
938 doneany = 1;
941 if (doneany)
942 /* Kill the last space. */
943 --o;
946 return o;
950 static char *
951 func_strip(o, argv, funcname)
952 char *o;
953 char **argv;
954 const char *funcname;
956 char *p = argv[0];
957 int doneany =0;
959 while (*p != '\0')
961 int i=0;
962 char *word_start=0;
964 while (isspace(*p))
965 ++p;
966 word_start = p;
967 for (i=0; *p != '\0' && !isspace(*p); ++p, ++i)
969 if (!i)
970 break;
971 o = variable_buffer_output (o, word_start, i);
972 o = variable_buffer_output (o, " ", 1);
973 doneany = 1;
976 if (doneany)
977 /* Kill the last space. */
978 --o;
979 return o;
983 Print a warning or fatal message.
985 static char *
986 func_error (o, argv, funcname)
987 char *o;
988 char **argv;
989 const char *funcname;
991 char **argvp;
992 char *msg, *p;
993 int len;
995 /* The arguments will be broken on commas. Rather than create yet
996 another special case where function arguments aren't broken up,
997 just create a format string that puts them back together. */
998 for (len=0, argvp=argv; *argvp != 0; ++argvp)
999 len += strlen(*argvp) + 2;
1001 p = msg = alloca (len + 1);
1003 for (argvp=argv; argvp[1] != 0; ++argvp)
1005 strcpy(p, *argvp);
1006 p += strlen(*argvp);
1007 *(p++) = ',';
1008 *(p++) = ' ';
1010 strcpy(p, *argvp);
1012 if (*funcname == 'e')
1013 fatal (reading_file, "%s", msg);
1015 /* The warning function expands to the empty string. */
1016 error (reading_file, "%s", msg);
1018 return o;
1023 chop argv[0] into words, and sort them.
1025 static char *
1026 func_sort (o, argv, funcname)
1027 char *o;
1028 char **argv;
1029 const char *funcname;
1031 char **words = 0;
1032 int nwords = 0;
1033 register int wordi = 0;
1035 /* Chop ARGV[0] into words and put them in WORDS. */
1036 char *t = argv[0];
1037 char *p=0;
1038 int len;
1039 int i;
1041 while ((p = find_next_token (&t, &len)) != 0)
1043 if (wordi >= nwords - 1)
1045 nwords = 2* nwords + 5;
1046 words = (char **) xrealloc ((char *) words,
1047 nwords * sizeof (char *));
1049 words[wordi++] = savestring (p, len);
1052 if (!wordi)
1053 return o;
1055 /* Now sort the list of words. */
1056 qsort ((char *) words, wordi, sizeof (char *), alpha_compare);
1058 /* Now write the sorted list. */
1059 for (i = 0; i < wordi; ++i)
1061 len = strlen (words[i]);
1062 if (i == wordi - 1 || strlen (words[i + 1]) != len
1063 || strcmp (words[i], words[i + 1]))
1065 o = variable_buffer_output (o, words[i], len);
1066 o = variable_buffer_output (o, " ", 1);
1068 free (words[i]);
1070 /* Kill the last space. */
1071 --o;
1073 free (words);
1075 return o;
1078 static char *
1079 func_wildcard(o, argv, funcname)
1080 char *o;
1081 char **argv;
1082 const char *funcname;
1085 #ifdef _AMIGA
1086 o = wildcard_expansion (argv[0], o);
1087 #else
1088 char *p = string_glob (argv[0]);
1089 o = variable_buffer_output (o, p, strlen (p));
1090 #endif
1091 return o;
1095 \r is replaced on UNIX as well. Is this desirable?
1097 void
1098 fold_newlines (buffer, length)
1099 char *buffer;
1100 int *length;
1102 char *dst = buffer;
1103 char *src = buffer;
1104 char *last_nonnl = buffer -1;
1105 src[*length] = 0;
1106 for (; *src != '\0'; ++src)
1108 if (src[0] == '\r' && src[1] == '\n')
1109 continue;
1110 if (*src == '\n')
1112 *dst++ = ' ';
1114 else
1116 last_nonnl = dst;
1117 *dst++ = *src;
1120 *(++last_nonnl) = '\0';
1121 *length = last_nonnl - buffer;
1126 int shell_function_pid = 0, shell_function_completed;
1129 #ifdef WINDOWS32
1130 /*untested*/
1132 #include <windows.h>
1133 #include <io.h>
1134 #include "sub_proc.h"
1137 void
1138 windows32_openpipe (int *pipedes, int *pid_p, char **command_argv, char **envp)
1140 SECURITY_ATTRIBUTES saAttr;
1141 HANDLE hIn;
1142 HANDLE hErr;
1143 HANDLE hChildOutRd;
1144 HANDLE hChildOutWr;
1145 HANDLE hProcess;
1148 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1149 saAttr.bInheritHandle = TRUE;
1150 saAttr.lpSecurityDescriptor = NULL;
1152 if (DuplicateHandle(GetCurrentProcess(),
1153 GetStdHandle(STD_INPUT_HANDLE),
1154 GetCurrentProcess(),
1155 &hIn,
1157 TRUE,
1158 DUPLICATE_SAME_ACCESS) == FALSE) {
1159 fatal (NILF, _("create_child_process: DuplicateHandle(In) failed (e=%d)\n"),
1160 GetLastError());
1163 if (DuplicateHandle(GetCurrentProcess(),
1164 GetStdHandle(STD_ERROR_HANDLE),
1165 GetCurrentProcess(),
1166 &hErr,
1168 TRUE,
1169 DUPLICATE_SAME_ACCESS) == FALSE) {
1170 fatal (NILF, _("create_child_process: DuplicateHandle(Err) failed (e=%d)\n"),
1171 GetLastError());
1174 if (!CreatePipe(&hChildOutRd, &hChildOutWr, &saAttr, 0))
1175 fatal (NILF, _("CreatePipe() failed (e=%d)\n"), GetLastError());
1179 hProcess = process_init_fd(hIn, hChildOutWr, hErr);
1181 if (!hProcess)
1182 fatal (NILF, _("windows32_openpipe (): process_init_fd() failed\n"));
1184 else
1185 process_register(hProcess);
1187 /* make sure that CreateProcess() has Path it needs */
1188 sync_Path_environment();
1190 if (!process_begin(hProcess, command_argv, envp, command_argv[0], NULL))
1191 *pid_p = (int) hProcess;
1192 else
1193 fatal (NILF, _("windows32_openpipe (): unable to launch process (e=%d)\n"),
1194 process_last_err(hProcess));
1196 /* set up to read data from child */
1197 pipedes[0] = _open_osfhandle((long) hChildOutRd, O_RDONLY);
1199 /* this will be closed almost right away */
1200 pipedes[1] = _open_osfhandle((long) hChildOutWr, O_APPEND);
1202 #endif
1205 #ifdef __MSDOS__
1207 msdos_openpipe (int* pipedes, int *pidp, char *text)
1209 FILE *fpipe=0;
1210 /* MSDOS can't fork, but it has `popen'. */
1211 struct variable *sh = lookup_variable ("SHELL", 5);
1212 int e;
1213 extern int dos_command_running, dos_status;
1215 /* Make sure not to bother processing an empty line. */
1216 while (isblank (*text))
1217 ++text;
1218 if (*text == '\0')
1219 return 0;
1221 if (sh)
1223 char buf[PATH_MAX + 7];
1224 /* This makes sure $SHELL value is used by $(shell), even
1225 though the target environment is not passed to it. */
1226 sprintf (buf, "SHELL=%s", sh->value);
1227 putenv (buf);
1230 e = errno;
1231 errno = 0;
1232 dos_command_running = 1;
1233 dos_status = 0;
1234 /* If dos_status becomes non-zero, it means the child process
1235 was interrupted by a signal, like SIGINT or SIGQUIT. See
1236 fatal_error_signal in commands.c. */
1237 fpipe = popen (text, "rt");
1238 dos_command_running = 0;
1239 if (!fpipe || dos_status)
1241 pipedes[0] = -1;
1242 *pidp = -1;
1243 if (dos_status)
1244 errno = EINTR;
1245 else if (errno == 0)
1246 errno = ENOMEM;
1247 shell_function_completed = -1;
1249 else
1251 pipedes[0] = fileno (fpipe);
1252 *pidp = 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
1253 errno = e;
1254 shell_function_completed = 1;
1256 return fpipe;
1258 #endif
1261 Do shell spawning, with the naughty bits for different OSes.
1264 #ifdef VMS
1266 /* VMS can't do $(shell ...) */
1267 #define func_shell 0
1269 #else
1270 #ifndef _AMIGA
1271 static char *
1272 func_shell (o, argv, funcname)
1273 char *o;
1274 char **argv;
1275 const char *funcname;
1277 char* batch_filename = NULL;
1278 int i;
1280 #ifdef __MSDOS__
1281 FILE *fpipe;
1282 #endif
1283 char **command_argv;
1284 char *error_prefix;
1285 char **envp;
1286 int pipedes[2];
1287 int pid;
1289 #ifndef __MSDOS__
1290 /* Construct the argument list. */
1291 command_argv = construct_command_argv (argv[0],
1292 (char **) NULL, (struct file *) 0,
1293 &batch_filename);
1294 if (command_argv == 0)
1295 return o;
1296 #endif
1298 /* Using a target environment for `shell' loses in cases like:
1299 export var = $(shell echo foobie)
1300 because target_environment hits a loop trying to expand $(var)
1301 to put it in the environment. This is even more confusing when
1302 var was not explicitly exported, but just appeared in the
1303 calling environment. */
1305 envp = environ;
1307 /* For error messages. */
1308 if (reading_file != 0)
1310 error_prefix = (char *) alloca (strlen(reading_file->filenm)+100);
1311 sprintf (error_prefix,
1312 "%s:%lu: ", reading_file->filenm, reading_file->lineno);
1314 else
1315 error_prefix = "";
1317 #ifdef WINDOWS32
1318 windows32_openpipe (pipedes, &pid, command_argv, envp);
1319 #else /* WINDOWS32 */
1321 # ifdef __MSDOS__
1322 fpipe = msdos_openpipe (pipedes, &pid, argv[0]);
1323 if (pipedes[0] < 0)
1325 perror_with_name (error_prefix, "pipe");
1326 return o;
1328 # else
1329 if (pipe (pipedes) < 0)
1331 perror_with_name (error_prefix, "pipe");
1332 return o;
1335 pid = vfork ();
1336 if (pid < 0)
1337 perror_with_name (error_prefix, "fork");
1338 else if (pid == 0)
1339 child_execute_job (0, pipedes[1], command_argv, envp);
1340 else
1341 # endif /* ! __MSDOS__ */
1343 #endif /* WINDOWS32 */
1345 /* We are the parent. */
1347 char *buffer;
1348 unsigned int maxlen;
1349 int cc;
1351 /* Record the PID for reap_children. */
1352 shell_function_pid = pid;
1353 #ifndef __MSDOS__
1354 shell_function_completed = 0;
1356 /* Free the storage only the child needed. */
1357 free (command_argv[0]);
1358 free ((char *) command_argv);
1360 /* Close the write side of the pipe. */
1361 (void) close (pipedes[1]);
1362 #endif
1364 /* Set up and read from the pipe. */
1366 maxlen = 200;
1367 buffer = (char *) xmalloc (maxlen + 1);
1369 /* Read from the pipe until it gets EOF. */
1370 i = 0;
1373 if (i == maxlen)
1375 maxlen += 512;
1376 buffer = (char *) xrealloc (buffer, maxlen + 1);
1379 errno = 0;
1380 cc = read (pipedes[0], &buffer[i], maxlen - i);
1381 if (cc > 0)
1382 i += cc;
1384 while (cc > 0 || EINTR_SET);
1386 /* Close the read side of the pipe. */
1387 #ifdef __MSDOS__
1388 if (fpipe)
1389 (void) pclose (fpipe);
1390 #else
1391 (void) close (pipedes[0]);
1392 #endif
1394 /* Loop until child_handler sets shell_function_completed
1395 to the status of our child shell. */
1396 while (shell_function_completed == 0)
1397 reap_children (1, 0);
1399 if (batch_filename) {
1400 if (debug_flag)
1401 printf(_("Cleaning up temporary batch file %s\n"), batch_filename);
1402 remove(batch_filename);
1403 free(batch_filename);
1405 shell_function_pid = 0;
1407 /* The child_handler function will set shell_function_completed
1408 to 1 when the child dies normally, or to -1 if it
1409 dies with status 127, which is most likely an exec fail. */
1411 if (shell_function_completed == -1)
1413 /* This most likely means that the execvp failed,
1414 so we should just write out the error message
1415 that came in over the pipe from the child. */
1416 fputs (buffer, stderr);
1417 fflush (stderr);
1419 else
1421 /* The child finished normally. Replace all
1422 newlines in its output with spaces, and put
1423 that in the variable output buffer. */
1424 fold_newlines (buffer, &i);
1425 o = variable_buffer_output (o, buffer, i);
1428 free (buffer);
1431 return o;
1434 #else /* _AMIGA */
1436 /* Do the Amiga version of func_shell. */
1438 static char *
1439 func_shell (char *o, char **argv, const char *funcname)
1441 /* Amiga can't fork nor spawn, but I can start a program with
1442 redirection of my choice. However, this means that we
1443 don't have an opportunity to reopen stdout to trap it. Thus,
1444 we save our own stdout onto a new descriptor and dup a temp
1445 file's descriptor onto our stdout temporarily. After we
1446 spawn the shell program, we dup our own stdout back to the
1447 stdout descriptor. The buffer reading is the same as above,
1448 except that we're now reading from a file. */
1450 #include <dos/dos.h>
1451 #include <proto/dos.h>
1453 BPTR child_stdout;
1454 char tmp_output[FILENAME_MAX];
1455 unsigned int maxlen = 200;
1456 int cc, i;
1457 char * buffer, * ptr;
1458 char ** aptr;
1459 int len = 0;
1460 char* batch_filename = NULL;
1462 /* Construct the argument list. */
1463 command_argv = construct_command_argv (argv[0], (char **) NULL,
1464 (struct file *) 0, &batch_filename);
1465 if (command_argv == 0)
1466 return o;
1469 strcpy (tmp_output, "t:MakeshXXXXXXXX");
1470 mktemp (tmp_output);
1471 child_stdout = Open (tmp_output, MODE_NEWFILE);
1473 for (aptr=command_argv; *aptr; aptr++)
1474 len += strlen (*aptr) + 1;
1476 buffer = xmalloc (len + 1);
1477 ptr = buffer;
1479 for (aptr=command_argv; *aptr; aptr++)
1481 strcpy (ptr, *aptr);
1482 ptr += strlen (ptr) + 1;
1483 *ptr ++ = ' ';
1484 *ptr = 0;
1487 ptr[-1] = '\n';
1489 Execute (buffer, NULL, child_stdout);
1490 free (buffer);
1492 Close (child_stdout);
1494 child_stdout = Open (tmp_output, MODE_OLDFILE);
1496 buffer = xmalloc (maxlen);
1497 i = 0;
1500 if (i == maxlen)
1502 maxlen += 512;
1503 buffer = (char *) xrealloc (buffer, maxlen + 1);
1506 cc = Read (child_stdout, &buffer[i], maxlen - i);
1507 if (cc > 0)
1508 i += cc;
1509 } while (cc > 0);
1511 Close (child_stdout);
1513 fold_newlines (buffer, &i);
1514 o = variable_buffer_output (o, buffer, i);
1515 free (buffer);
1516 return o;
1518 #endif /* _AMIGA */
1519 #endif /* !VMS */
1521 #ifdef EXPERIMENTAL
1524 equality. Return is string-boolean, ie, the empty string is false.
1526 static char *
1527 func_eq (char* o, char **argv, char *funcname)
1529 int result = ! strcmp (argv[0], argv[1]);
1530 o = variable_buffer_output (o, result ? "1" : "", result);
1531 return o;
1536 string-boolean not operator.
1538 static char *
1539 func_not (char* o, char **argv, char *funcname)
1541 char * s = argv[0];
1542 int result = 0;
1543 while (isspace (*s))
1544 s++;
1545 result = ! (*s);
1546 o = variable_buffer_output (o, result ? "1" : "", result);
1547 return o;
1553 This is an experimental conditional function.
1555 Syntax:
1557 $(if condition, true-part, false-part)
1559 This is fully not consistent with make's syntax, but more in line
1560 with `normal' programming languages.
1562 Semantics:
1564 - CONDITION is false iff it evaluates to an empty string. White
1565 space before and after condition are stripped before evaluation.
1567 - If CONDITION is true, then TRUE-PART is evaluated, otherwise
1568 FALSE-PART is evaluated. Because only one of the two PARTs is
1569 evaluated, you can use $(if ) to create side-effects with the
1570 $(shell ) function
1573 static char *
1574 func_if (char* o, char **argv, char *funcname)
1576 char *begp = argv[0];
1577 char *endp = argv[1]-2;
1578 char *expansion =0;
1579 int result = 0;
1581 strip_whitespace (&begp, &endp);
1582 if(begp <= endp)
1583 expansion = expand_argument (begp, endp + 1);
1585 result = expansion
1586 ? strlen (expansion)
1587 : 0;
1589 result = !result;
1590 free (expansion);
1592 expansion = expand_argument (argv[1 + result], argv[2+result] -1);
1593 o = variable_buffer_output (o, expansion, strlen (expansion));
1595 return o;
1597 #endif
1600 #define STRING_SIZE_TUPLE(_s) (_s), (sizeof(_s)-1)
1602 /* Lookup table for builtin functions.
1604 This doesn't have to be sorted; we use a straight lookup. We might gain
1605 some efficiency by moving most often used functions to the start of the
1606 table.
1608 If REQUIRED_ARGS is positive, the function takes exactly that many
1609 arguments. All subsequent text is included with the last argument. So,
1610 since $(sort a,b,c) takes only one argument, it will be the full string
1611 "a,b,c". If the value is negative, it's the minimum number of arguments.
1612 A function can have more, but if it has less an error is generated.
1614 EXPAND_ARGS means that all arguments should be expanded before invocation.
1615 Functions that do namespace tricks (foreach) don't automatically expand. */
1617 static char *func_call PARAMS((char *o, char **argv, const char *funcname));
1620 static struct function_table_entry function_table[] =
1622 /* Name/size */ /* ARG EXP? Function */
1623 { STRING_SIZE_TUPLE("addprefix"), 2, 1, func_addsuffix_addprefix},
1624 { STRING_SIZE_TUPLE("addsuffix"), 2, 1, func_addsuffix_addprefix},
1625 { STRING_SIZE_TUPLE("basename"), 1, 1, func_basename_dir},
1626 { STRING_SIZE_TUPLE("dir"), 1, 1, func_basename_dir},
1627 { STRING_SIZE_TUPLE("notdir"), 1, 1, func_notdir_suffix},
1628 { STRING_SIZE_TUPLE("subst"), 3, 1, func_subst},
1629 { STRING_SIZE_TUPLE("suffix"), 1, 1, func_notdir_suffix},
1630 { STRING_SIZE_TUPLE("filter"), 2, 1, func_filter_filterout},
1631 { STRING_SIZE_TUPLE("filter-out"), 2, 1, func_filter_filterout},
1632 { STRING_SIZE_TUPLE("findstring"), 2, 1, func_findstring},
1633 { STRING_SIZE_TUPLE("firstword"), 1, 1, func_firstword},
1634 { STRING_SIZE_TUPLE("join"), 2, 1, func_join},
1635 { STRING_SIZE_TUPLE("patsubst"), 3, 1, func_patsubst},
1636 { STRING_SIZE_TUPLE("shell"), 1, 1, func_shell},
1637 { STRING_SIZE_TUPLE("sort"), 1, 1, func_sort},
1638 { STRING_SIZE_TUPLE("strip"), 1, 1, func_strip},
1639 { STRING_SIZE_TUPLE("wildcard"), 1, 1, func_wildcard},
1640 { STRING_SIZE_TUPLE("word"), 2, 1, func_word},
1641 { STRING_SIZE_TUPLE("wordlist"), 3, 1, func_wordlist},
1642 { STRING_SIZE_TUPLE("words"), 1, 1, func_words},
1643 { STRING_SIZE_TUPLE("origin"), 1, 1, func_origin},
1644 { STRING_SIZE_TUPLE("foreach"), 3, 0, func_foreach},
1645 { STRING_SIZE_TUPLE("call"), -1, 1, func_call},
1646 { STRING_SIZE_TUPLE("error"), 1, 1, func_error},
1647 { STRING_SIZE_TUPLE("warning"), 1, 1, func_error},
1648 #ifdef EXPERIMENTAL
1649 { STRING_SIZE_TUPLE("eq"), 2, 1, func_eq},
1650 { STRING_SIZE_TUPLE("if"), 3, 0, func_if},
1651 { STRING_SIZE_TUPLE("not"), 1, 1, func_not},
1652 #endif
1653 { 0 }
1657 /* These must come after the definition of function_table[]. */
1659 static char *
1660 expand_builtin_function (o, argc, argv, entry_p)
1661 char *o;
1662 int argc;
1663 char **argv;
1664 struct function_table_entry *entry_p;
1666 int min = (entry_p->required_args > 0
1667 ? entry_p->required_args
1668 : -entry_p->required_args);
1670 if (argc < min)
1671 fatal (reading_file,
1672 _("Insufficient number of arguments (%d) to function `%s'"),
1673 argc, entry_p->name);
1675 if (!entry_p->func_ptr)
1676 fatal (reading_file, _("Unimplemented on this platform: function `%s'"),
1677 entry_p->name);
1679 return entry_p->func_ptr (o, argv, entry_p->name);
1682 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1683 opening ( or { and is not null-terminated. If a function invocation
1684 is found, expand it into the buffer at *OP, updating *OP, incrementing
1685 *STRINGP past the reference and returning nonzero. If not, return zero. */
1688 handle_function (op, stringp)
1689 char **op;
1690 char **stringp;
1692 const struct function_table_entry *entry_p;
1693 char openparen = (*stringp)[0];
1694 char closeparen = openparen == '(' ? ')' : '}';
1695 char *beg = *stringp + 1;
1696 char *endref;
1697 int count = 0;
1698 char *argbeg;
1699 register char *p;
1700 char **argv, **argvp;
1701 int nargs;
1703 entry_p = lookup_function (function_table, beg);
1705 if (!entry_p)
1706 return 0;
1708 /* We have found a call to a builtin function. Find the end of the
1709 arguments, and do the function. */
1711 endref = beg + entry_p->len;
1713 /* Space after function name isn't part of the args. */
1714 p = next_token (endref);
1715 argbeg = p;
1717 /* Find the end of the function invocation, counting nested use of
1718 whichever kind of parens we use. Since we're looking, count commas
1719 to get a rough estimate of how many arguments we might have. The
1720 count might be high, but it'll never be low. */
1722 for (nargs=1; *p != '\0'; ++p)
1723 if (*p == ',')
1724 ++nargs;
1725 else if (*p == openparen)
1726 ++count;
1727 else if (*p == closeparen && --count < 0)
1728 break;
1730 if (count >= 0)
1731 fatal (reading_file,
1732 _("unterminated call to function `%s': missing `%c'"),
1733 entry_p->name, closeparen);
1735 /* Get some memory to store the arg pointers. */
1736 argvp = argv = (char **) alloca (sizeof(char *) * (nargs + 2));
1738 /* Chop the string into arguments, then store the end pointer and a nul.
1739 If REQUIRED_ARGS is positive, as soon as we hit that many assume the
1740 rest of the string is part of the last argument. */
1741 *argvp = argbeg;
1742 nargs = 1;
1743 while (entry_p->required_args < 0 || nargs < entry_p->required_args)
1745 char *next = find_next_argument (openparen, closeparen, *argvp, p);
1747 if (!next)
1748 break;
1750 *(++argvp) = next+1;
1751 ++nargs;
1754 *(++argvp) = p+1;
1755 *(++argvp) = 0;
1757 /* If we should expand, do it. */
1758 if (entry_p->expand_args)
1760 for (argvp=argv; argvp[1] != 0; ++argvp)
1761 *argvp = expand_argument (*argvp, argvp[1]-1);
1763 /* end pointer doesn't make sense for expanded stuff. */
1764 *argvp = 0;
1767 /* Finally! Run the function... */
1768 *op = expand_builtin_function (*op, nargs, argv, entry_p);
1770 /* If we allocated memory for the expanded args, free it again. */
1771 if (entry_p->expand_args)
1772 for (argvp=argv; *argvp != 0; ++argvp)
1773 free (*argvp);
1775 *stringp = p;
1777 return 1;
1781 /* User-defined functions. Expand the first argument as either a builtin
1782 function or a make variable, in the context of the rest of the arguments
1783 assigned to $1, $2, ... $N. $0 is the name of the function. */
1785 static char *
1786 func_call (o, argv, funcname)
1787 char *o;
1788 char **argv;
1789 const char *funcname;
1791 char *fname;
1792 int flen;
1793 char *body;
1794 int i;
1795 const struct function_table_entry *entry_p;
1797 /* Calling nothing is a no-op. */
1798 if (*argv[0] == '\0')
1799 return o;
1801 /* There is no way to define a variable with a space in the name, so strip
1802 trailing whitespace as a favor to the user. */
1804 flen = strlen (argv[0]);
1805 fname = argv[0] + flen - 1;
1806 while (isspace (*fname))
1807 --fname;
1808 fname[1] = '\0';
1810 flen = fname - argv[0] + 1;
1811 fname = argv[0];
1813 /* Are we invoking a builtin function? */
1815 entry_p = lookup_function (function_table, fname);
1817 if (entry_p)
1819 for (i=0; argv[i+1]; ++i)
1822 return expand_builtin_function (o, i, argv + 1, entry_p);
1825 /* No, so the first argument is the name of a variable to be expanded and
1826 interpreted as a function. Create the variable reference. */
1827 body = alloca (flen + 4);
1828 body[0]='$';
1829 body[1]='(';
1830 strcpy (body + 2, fname);
1831 body[flen+2]=')';
1832 body[flen+3]= '\0';
1834 /* Set up arguments $(1) .. $(N). $(0) is the function name. */
1836 push_new_variable_scope ();
1838 for (i=0; *argv; ++i, ++argv)
1840 char num[11];
1842 sprintf (num, "%d", i);
1843 define_variable (num, strlen (num), *argv, o_automatic, 0);
1846 /* Expand the body in the context of the arguments, adding the result to
1847 the variable buffer. */
1849 o = variable_expand_string (o, body, flen+3);
1851 pop_variable_scope ();
1853 return o + strlen(o);