* Ensure -Iglob comes before any user-specified CPPFLAGS.
[make.git] / function.c
blobf6f482a42dcfab1384b1985090608d9d95274e04
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 #if defined(__MSDOS__) || defined(WINDOWS32)
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);
706 while (p <= end)
707 if (!ISDIGIT (*(p++))) /* ISDIGIT only evals its arg once: see make.h. */
708 return 0;
710 return (end - beg >= 0);
713 void
714 check_numeric (s, message)
715 char *s;
716 char *message;
718 if (!is_numeric (s))
719 fatal (reading_file, message);
724 static char *
725 func_word(o, argv, funcname)
726 char *o;
727 char **argv;
728 const char *funcname;
730 char *end_p=0;
731 int i=0;
732 char *p=0;
734 /* Check the first argument. */
735 check_numeric (argv[0], _("non-numeric first argument to `word' function"));
736 i = atoi (argv[0]);
738 if (i == 0)
739 fatal (reading_file, _("the `word' function takes a positive index argument"));
742 end_p = argv[1];
743 while ((p = find_next_token (&end_p, 0)) != 0)
744 if (--i == 0)
745 break;
747 if (i == 0)
748 o = variable_buffer_output (o, p, end_p - p);
750 return o;
753 static char *
754 func_wordlist (o, argv, funcname)
755 char *o;
756 char **argv;
757 const char *funcname;
759 int i=0;
760 int j=0;
762 /* Check the first argument. */
763 check_numeric (argv[0],
764 _("non-numeric first argument to `wordlist' function"));
765 i =atoi(argv[0]);
766 check_numeric (argv[1],
767 _("non-numeric second argument to `wordlist' function"));
769 j = atoi(argv[1]);
773 char *p;
774 char *end_p = argv[2];
776 int start = (i < j) ? i : j;
777 int count = j -i ;
778 if (count < 0)
779 count = - count;
780 count ++;
784 while (((p = find_next_token (&end_p, 0)) != 0) && --start)
786 if (p)
788 while (--count && (find_next_token (&end_p, 0) != 0))
790 o = variable_buffer_output (o, p, end_p - p);
793 return o;
796 static char*
797 func_findstring(o, argv, funcname)
798 char *o;
799 char **argv;
800 const char *funcname;
802 /* Find the first occurrence of the first string in the second. */
803 int i = strlen (argv[0]);
804 if (sindex (argv[1], 0, argv[0], i) != 0)
805 o = variable_buffer_output (o, argv[0], i);
807 return o;
810 static char *
811 func_foreach (o, argv, funcname)
812 char *o;
813 char **argv;
814 const char *funcname;
816 /* expand only the first two. */
817 char *varname = expand_argument (argv[0], argv[1] - 1);
818 char *list = expand_argument (argv[1], argv[2] -1);
819 char *body = savestring (argv[2], argv[3] - argv[2] - 1);
821 int len =0;
822 char *list_iterator = list;
823 char *p;
824 register struct variable *var=0;
825 int doneany =0;
827 push_new_variable_scope ();
828 var = define_variable (varname, strlen (varname), "", o_automatic, 0);
830 /* loop through LIST, put the value in VAR and expand BODY */
831 while ((p = find_next_token (&list_iterator, &len)) != 0)
833 char *result = 0;
836 char save = p[len];
838 p[len] = '\0';
839 free (var->value);
840 var->value = (char *) xstrdup ((char*) p);
841 p[len] = save;
844 result = allocated_variable_expand (body);
846 o = variable_buffer_output (o, result, strlen (result));
847 o = variable_buffer_output (o, " ", 1);
848 doneany = 1;
849 free (result);
852 if (doneany)
853 /* Kill the last space. */
854 --o;
856 pop_variable_scope ();
857 free (varname);
858 free (list);
859 free (body);
861 return o;
864 struct a_word
866 struct a_word *next;
867 char *str;
868 int matched;
871 static char *
872 func_filter_filterout (o, argv, funcname)
873 char *o;
874 char **argv;
875 const char *funcname;
877 struct a_word *wordhead =0;
878 struct a_word *wordtail =0;
880 int is_filter = streq (funcname, "filter");
881 char *patterns = argv[0];
884 char *p;
885 int len;
886 char *word_iterator = argv[1];
888 /* Chop ARGV[1] up into words and then run each pattern through. */
889 while ((p = find_next_token (&word_iterator, &len)) != 0)
891 struct a_word *w = (struct a_word *)alloca(sizeof(struct a_word));
892 if (wordhead == 0)
893 wordhead = w;
894 else
895 wordtail->next = w;
896 wordtail = w;
898 if (*word_iterator != '\0')
899 ++word_iterator;
900 p[len] = '\0';
901 w->str = p;
902 w->matched = 0;
905 if (wordhead != 0)
907 struct a_word *wp =0;
908 char *pat_iterator = patterns;
909 int doneany = 0;
911 wordtail->next = 0;
914 /* Run each pattern through the words, killing words. */
915 while ((p = find_next_token (&pat_iterator, &len)) != 0)
917 char *percent;
918 char save = p[len];
919 p[len] = '\0';
921 percent = find_percent (p);
922 for (wp = wordhead; wp != 0; wp = wp->next)
923 wp->matched |= (percent == 0 ? streq (p, wp->str)
924 : pattern_matches (p, percent, wp->str));
926 p[len] = save;
929 /* Output the words that matched (or didn't, for filter-out). */
930 for (wp = wordhead; wp != 0; wp = wp->next)
931 if (is_filter ? wp->matched : !wp->matched)
933 o = variable_buffer_output (o, wp->str, strlen (wp->str));
934 o = variable_buffer_output (o, " ", 1);
935 doneany = 1;
938 if (doneany)
939 /* Kill the last space. */
940 --o;
943 return o;
947 static char *
948 func_strip(o, argv, funcname)
949 char *o;
950 char **argv;
951 const char *funcname;
953 char *p = argv[0];
954 int doneany =0;
956 while (*p != '\0')
958 int i=0;
959 char *word_start=0;
961 while (isspace(*p))
962 ++p;
963 word_start = p;
964 for (i=0; *p != '\0' && !isspace(*p); ++p, ++i)
966 if (!i)
967 break;
968 o = variable_buffer_output (o, word_start, i);
969 o = variable_buffer_output (o, " ", 1);
970 doneany = 1;
973 if (doneany)
974 /* Kill the last space. */
975 --o;
976 return o;
980 Print a warning or fatal message.
982 static char *
983 func_error (o, argv, funcname)
984 char *o;
985 char **argv;
986 const char *funcname;
988 char **argvp;
989 char *msg, *p;
990 int len;
992 /* The arguments will be broken on commas. Rather than create yet
993 another special case where function arguments aren't broken up,
994 just create a format string that puts them back together. */
995 for (len=0, argvp=argv; *argvp != 0; ++argvp)
996 len += strlen(*argvp) + 2;
998 p = msg = alloca (len + 1);
1000 for (argvp=argv; argvp[1] != 0; ++argvp)
1002 strcpy(p, *argvp);
1003 p += strlen(*argvp);
1004 *(p++) = ',';
1005 *(p++) = ' ';
1007 strcpy(p, *argvp);
1009 if (*funcname == 'e')
1010 fatal (reading_file, "%s", msg);
1012 /* The warning function expands to the empty string. */
1013 error (reading_file, "%s", msg);
1015 return o;
1020 chop argv[0] into words, and sort them.
1022 static char *
1023 func_sort (o, argv, funcname)
1024 char *o;
1025 char **argv;
1026 const char *funcname;
1028 char **words = 0;
1029 int nwords = 0;
1030 register int wordi = 0;
1032 /* Chop ARGV[0] into words and put them in WORDS. */
1033 char *t = argv[0];
1034 char *p=0;
1035 int len;
1036 int i;
1038 while ((p = find_next_token (&t, &len)) != 0)
1040 if (wordi >= nwords - 1)
1042 nwords = 2* nwords + 5;
1043 words = (char **) xrealloc ((char *) words,
1044 nwords * sizeof (char *));
1046 words[wordi++] = savestring (p, len);
1049 if (!wordi)
1050 return o;
1052 /* Now sort the list of words. */
1053 qsort ((char *) words, wordi, sizeof (char *), alpha_compare);
1055 /* Now write the sorted list. */
1056 for (i = 0; i < wordi; ++i)
1058 len = strlen (words[i]);
1059 if (i == wordi - 1 || strlen (words[i + 1]) != len
1060 || strcmp (words[i], words[i + 1]))
1062 o = variable_buffer_output (o, words[i], len);
1063 o = variable_buffer_output (o, " ", 1);
1065 free (words[i]);
1067 /* Kill the last space. */
1068 --o;
1070 free (words);
1072 return o;
1076 $(if condition,true-part[,false-part])
1078 CONDITION is false iff it evaluates to an empty string. White
1079 space before and after condition are stripped before evaluation.
1081 If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is
1082 evaluated (if it exists). Because only one of the two PARTs is evaluated,
1083 you can use $(if ...) to create side-effects (with $(shell ...), for
1084 example).
1087 static char *
1088 func_if (o, argv, funcname)
1089 char *o;
1090 char **argv;
1091 const char *funcname;
1093 char *begp = argv[0];
1094 char *endp = argv[1]-1;
1095 int result = 0;
1097 /* Find the result of the condition: if we have a value, and it's not
1098 empty, the condition is true. If we don't have a value, or it's the
1099 empty string, then it's false. */
1101 strip_whitespace (&begp, &endp);
1103 if (begp < endp)
1105 char *expansion = expand_argument (begp, endp);
1107 result = strlen (expansion);
1108 free (expansion);
1111 /* If the result is true (1) we want to eval the first argument, and if
1112 it's false (0) we want to eval the second. If the argument doesn't
1113 exist we do nothing, otherwise expand it and add to the buffer. */
1115 argv += 1 + !result;
1117 if (argv[0] != NULL && argv[1] != NULL)
1119 char *expansion;
1120 char **endp = argv+1;
1122 /* If we're doing the else-clause, make sure we concatenate any
1123 potential extra arguments into the last argument. */
1124 if (!result)
1125 while (*endp && **endp != '\0')
1126 ++endp;
1128 expansion = expand_argument (*argv, *endp-1);
1130 o = variable_buffer_output (o, expansion, strlen (expansion));
1131 free (expansion);
1134 return o;
1137 static char *
1138 func_wildcard(o, argv, funcname)
1139 char *o;
1140 char **argv;
1141 const char *funcname;
1144 #ifdef _AMIGA
1145 o = wildcard_expansion (argv[0], o);
1146 #else
1147 char *p = string_glob (argv[0]);
1148 o = variable_buffer_output (o, p, strlen (p));
1149 #endif
1150 return o;
1154 \r is replaced on UNIX as well. Is this desirable?
1156 void
1157 fold_newlines (buffer, length)
1158 char *buffer;
1159 int *length;
1161 char *dst = buffer;
1162 char *src = buffer;
1163 char *last_nonnl = buffer -1;
1164 src[*length] = 0;
1165 for (; *src != '\0'; ++src)
1167 if (src[0] == '\r' && src[1] == '\n')
1168 continue;
1169 if (*src == '\n')
1171 *dst++ = ' ';
1173 else
1175 last_nonnl = dst;
1176 *dst++ = *src;
1179 *(++last_nonnl) = '\0';
1180 *length = last_nonnl - buffer;
1185 int shell_function_pid = 0, shell_function_completed;
1188 #ifdef WINDOWS32
1189 /*untested*/
1191 #include <windows.h>
1192 #include <io.h>
1193 #include "sub_proc.h"
1196 void
1197 windows32_openpipe (int *pipedes, int *pid_p, char **command_argv, char **envp)
1199 SECURITY_ATTRIBUTES saAttr;
1200 HANDLE hIn;
1201 HANDLE hErr;
1202 HANDLE hChildOutRd;
1203 HANDLE hChildOutWr;
1204 HANDLE hProcess;
1207 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1208 saAttr.bInheritHandle = TRUE;
1209 saAttr.lpSecurityDescriptor = NULL;
1211 if (DuplicateHandle(GetCurrentProcess(),
1212 GetStdHandle(STD_INPUT_HANDLE),
1213 GetCurrentProcess(),
1214 &hIn,
1216 TRUE,
1217 DUPLICATE_SAME_ACCESS) == FALSE) {
1218 fatal (NILF, _("create_child_process: DuplicateHandle(In) failed (e=%d)\n"),
1219 GetLastError());
1222 if (DuplicateHandle(GetCurrentProcess(),
1223 GetStdHandle(STD_ERROR_HANDLE),
1224 GetCurrentProcess(),
1225 &hErr,
1227 TRUE,
1228 DUPLICATE_SAME_ACCESS) == FALSE) {
1229 fatal (NILF, _("create_child_process: DuplicateHandle(Err) failed (e=%d)\n"),
1230 GetLastError());
1233 if (!CreatePipe(&hChildOutRd, &hChildOutWr, &saAttr, 0))
1234 fatal (NILF, _("CreatePipe() failed (e=%d)\n"), GetLastError());
1238 hProcess = process_init_fd(hIn, hChildOutWr, hErr);
1240 if (!hProcess)
1241 fatal (NILF, _("windows32_openpipe (): process_init_fd() failed\n"));
1243 else
1244 process_register(hProcess);
1246 /* make sure that CreateProcess() has Path it needs */
1247 sync_Path_environment();
1249 if (!process_begin(hProcess, command_argv, envp, command_argv[0], NULL))
1250 *pid_p = (int) hProcess;
1251 else
1252 fatal (NILF, _("windows32_openpipe (): unable to launch process (e=%d)\n"),
1253 process_last_err(hProcess));
1255 /* set up to read data from child */
1256 pipedes[0] = _open_osfhandle((long) hChildOutRd, O_RDONLY);
1258 /* this will be closed almost right away */
1259 pipedes[1] = _open_osfhandle((long) hChildOutWr, O_APPEND);
1261 #endif
1264 #ifdef __MSDOS__
1265 FILE *
1266 msdos_openpipe (int* pipedes, int *pidp, char *text)
1268 FILE *fpipe=0;
1269 /* MSDOS can't fork, but it has `popen'. */
1270 struct variable *sh = lookup_variable ("SHELL", 5);
1271 int e;
1272 extern int dos_command_running, dos_status;
1274 /* Make sure not to bother processing an empty line. */
1275 while (isblank (*text))
1276 ++text;
1277 if (*text == '\0')
1278 return 0;
1280 if (sh)
1282 char buf[PATH_MAX + 7];
1283 /* This makes sure $SHELL value is used by $(shell), even
1284 though the target environment is not passed to it. */
1285 sprintf (buf, "SHELL=%s", sh->value);
1286 putenv (buf);
1289 e = errno;
1290 errno = 0;
1291 dos_command_running = 1;
1292 dos_status = 0;
1293 /* If dos_status becomes non-zero, it means the child process
1294 was interrupted by a signal, like SIGINT or SIGQUIT. See
1295 fatal_error_signal in commands.c. */
1296 fpipe = popen (text, "rt");
1297 dos_command_running = 0;
1298 if (!fpipe || dos_status)
1300 pipedes[0] = -1;
1301 *pidp = -1;
1302 if (dos_status)
1303 errno = EINTR;
1304 else if (errno == 0)
1305 errno = ENOMEM;
1306 shell_function_completed = -1;
1308 else
1310 pipedes[0] = fileno (fpipe);
1311 *pidp = 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
1312 errno = e;
1313 shell_function_completed = 1;
1315 return fpipe;
1317 #endif
1320 Do shell spawning, with the naughty bits for different OSes.
1323 #ifdef VMS
1325 /* VMS can't do $(shell ...) */
1326 #define func_shell 0
1328 #else
1329 #ifndef _AMIGA
1330 static char *
1331 func_shell (o, argv, funcname)
1332 char *o;
1333 char **argv;
1334 const char *funcname;
1336 char* batch_filename = NULL;
1337 int i;
1339 #ifdef __MSDOS__
1340 FILE *fpipe;
1341 #endif
1342 char **command_argv;
1343 char *error_prefix;
1344 char **envp;
1345 int pipedes[2];
1346 int pid;
1348 #ifndef __MSDOS__
1349 /* Construct the argument list. */
1350 command_argv = construct_command_argv (argv[0],
1351 (char **) NULL, (struct file *) 0,
1352 &batch_filename);
1353 if (command_argv == 0)
1354 return o;
1355 #endif
1357 /* Using a target environment for `shell' loses in cases like:
1358 export var = $(shell echo foobie)
1359 because target_environment hits a loop trying to expand $(var)
1360 to put it in the environment. This is even more confusing when
1361 var was not explicitly exported, but just appeared in the
1362 calling environment. */
1364 envp = environ;
1366 /* For error messages. */
1367 if (reading_file != 0)
1369 error_prefix = (char *) alloca (strlen(reading_file->filenm)+11+4);
1370 sprintf (error_prefix,
1371 "%s:%lu: ", reading_file->filenm, reading_file->lineno);
1373 else
1374 error_prefix = "";
1376 #ifdef WINDOWS32
1377 windows32_openpipe (pipedes, &pid, command_argv, envp);
1378 #else /* WINDOWS32 */
1380 # ifdef __MSDOS__
1381 fpipe = msdos_openpipe (pipedes, &pid, argv[0]);
1382 if (pipedes[0] < 0)
1384 perror_with_name (error_prefix, "pipe");
1385 return o;
1387 # else
1388 if (pipe (pipedes) < 0)
1390 perror_with_name (error_prefix, "pipe");
1391 return o;
1394 pid = vfork ();
1395 if (pid < 0)
1396 perror_with_name (error_prefix, "fork");
1397 else if (pid == 0)
1398 child_execute_job (0, pipedes[1], command_argv, envp);
1399 else
1400 # endif /* ! __MSDOS__ */
1402 #endif /* WINDOWS32 */
1404 /* We are the parent. */
1406 char *buffer;
1407 unsigned int maxlen;
1408 int cc;
1410 /* Record the PID for reap_children. */
1411 shell_function_pid = pid;
1412 #ifndef __MSDOS__
1413 shell_function_completed = 0;
1415 /* Free the storage only the child needed. */
1416 free (command_argv[0]);
1417 free ((char *) command_argv);
1419 /* Close the write side of the pipe. */
1420 (void) close (pipedes[1]);
1421 #endif
1423 /* Set up and read from the pipe. */
1425 maxlen = 200;
1426 buffer = (char *) xmalloc (maxlen + 1);
1428 /* Read from the pipe until it gets EOF. */
1429 i = 0;
1432 if (i == maxlen)
1434 maxlen += 512;
1435 buffer = (char *) xrealloc (buffer, maxlen + 1);
1438 errno = 0;
1439 cc = read (pipedes[0], &buffer[i], maxlen - i);
1440 if (cc > 0)
1441 i += cc;
1443 while (cc > 0 || EINTR_SET);
1445 /* Close the read side of the pipe. */
1446 #ifdef __MSDOS__
1447 if (fpipe)
1448 (void) pclose (fpipe);
1449 #else
1450 (void) close (pipedes[0]);
1451 #endif
1453 /* Loop until child_handler sets shell_function_completed
1454 to the status of our child shell. */
1455 while (shell_function_completed == 0)
1456 reap_children (1, 0);
1458 if (batch_filename) {
1459 if (debug_flag)
1460 printf(_("Cleaning up temporary batch file %s\n"), batch_filename);
1461 remove(batch_filename);
1462 free(batch_filename);
1464 shell_function_pid = 0;
1466 /* The child_handler function will set shell_function_completed
1467 to 1 when the child dies normally, or to -1 if it
1468 dies with status 127, which is most likely an exec fail. */
1470 if (shell_function_completed == -1)
1472 /* This most likely means that the execvp failed,
1473 so we should just write out the error message
1474 that came in over the pipe from the child. */
1475 fputs (buffer, stderr);
1476 fflush (stderr);
1478 else
1480 /* The child finished normally. Replace all
1481 newlines in its output with spaces, and put
1482 that in the variable output buffer. */
1483 fold_newlines (buffer, &i);
1484 o = variable_buffer_output (o, buffer, i);
1487 free (buffer);
1490 return o;
1493 #else /* _AMIGA */
1495 /* Do the Amiga version of func_shell. */
1497 static char *
1498 func_shell (char *o, char **argv, const char *funcname)
1500 /* Amiga can't fork nor spawn, but I can start a program with
1501 redirection of my choice. However, this means that we
1502 don't have an opportunity to reopen stdout to trap it. Thus,
1503 we save our own stdout onto a new descriptor and dup a temp
1504 file's descriptor onto our stdout temporarily. After we
1505 spawn the shell program, we dup our own stdout back to the
1506 stdout descriptor. The buffer reading is the same as above,
1507 except that we're now reading from a file. */
1509 #include <dos/dos.h>
1510 #include <proto/dos.h>
1512 BPTR child_stdout;
1513 char tmp_output[FILENAME_MAX];
1514 unsigned int maxlen = 200;
1515 int cc, i;
1516 char * buffer, * ptr;
1517 char ** aptr;
1518 int len = 0;
1519 char* batch_filename = NULL;
1521 /* Construct the argument list. */
1522 command_argv = construct_command_argv (argv[0], (char **) NULL,
1523 (struct file *) 0, &batch_filename);
1524 if (command_argv == 0)
1525 return o;
1528 strcpy (tmp_output, "t:MakeshXXXXXXXX");
1529 mktemp (tmp_output);
1530 child_stdout = Open (tmp_output, MODE_NEWFILE);
1532 for (aptr=command_argv; *aptr; aptr++)
1533 len += strlen (*aptr) + 1;
1535 buffer = xmalloc (len + 1);
1536 ptr = buffer;
1538 for (aptr=command_argv; *aptr; aptr++)
1540 strcpy (ptr, *aptr);
1541 ptr += strlen (ptr) + 1;
1542 *ptr ++ = ' ';
1543 *ptr = 0;
1546 ptr[-1] = '\n';
1548 Execute (buffer, NULL, child_stdout);
1549 free (buffer);
1551 Close (child_stdout);
1553 child_stdout = Open (tmp_output, MODE_OLDFILE);
1555 buffer = xmalloc (maxlen);
1556 i = 0;
1559 if (i == maxlen)
1561 maxlen += 512;
1562 buffer = (char *) xrealloc (buffer, maxlen + 1);
1565 cc = Read (child_stdout, &buffer[i], maxlen - i);
1566 if (cc > 0)
1567 i += cc;
1568 } while (cc > 0);
1570 Close (child_stdout);
1572 fold_newlines (buffer, &i);
1573 o = variable_buffer_output (o, buffer, i);
1574 free (buffer);
1575 return o;
1577 #endif /* _AMIGA */
1578 #endif /* !VMS */
1580 #ifdef EXPERIMENTAL
1583 equality. Return is string-boolean, ie, the empty string is false.
1585 static char *
1586 func_eq (char* o, char **argv, char *funcname)
1588 int result = ! strcmp (argv[0], argv[1]);
1589 o = variable_buffer_output (o, result ? "1" : "", result);
1590 return o;
1595 string-boolean not operator.
1597 static char *
1598 func_not (char* o, char **argv, char *funcname)
1600 char * s = argv[0];
1601 int result = 0;
1602 while (isspace (*s))
1603 s++;
1604 result = ! (*s);
1605 o = variable_buffer_output (o, result ? "1" : "", result);
1606 return o;
1608 #endif
1611 #define STRING_SIZE_TUPLE(_s) (_s), (sizeof(_s)-1)
1613 /* Lookup table for builtin functions.
1615 This doesn't have to be sorted; we use a straight lookup. We might gain
1616 some efficiency by moving most often used functions to the start of the
1617 table.
1619 If REQUIRED_ARGS is positive, the function takes exactly that many
1620 arguments. All subsequent text is included with the last argument. So,
1621 since $(sort a,b,c) takes only one argument, it will be the full string
1622 "a,b,c". If the value is negative, it's the minimum number of arguments.
1623 A function can have more, but if it has less an error is generated.
1625 EXPAND_ARGS means that all arguments should be expanded before invocation.
1626 Functions that do namespace tricks (foreach) don't automatically expand. */
1628 static char *func_call PARAMS((char *o, char **argv, const char *funcname));
1631 static struct function_table_entry function_table[] =
1633 /* Name/size */ /* ARG EXP? Function */
1634 { STRING_SIZE_TUPLE("addprefix"), 2, 1, func_addsuffix_addprefix},
1635 { STRING_SIZE_TUPLE("addsuffix"), 2, 1, func_addsuffix_addprefix},
1636 { STRING_SIZE_TUPLE("basename"), 1, 1, func_basename_dir},
1637 { STRING_SIZE_TUPLE("dir"), 1, 1, func_basename_dir},
1638 { STRING_SIZE_TUPLE("notdir"), 1, 1, func_notdir_suffix},
1639 { STRING_SIZE_TUPLE("subst"), 3, 1, func_subst},
1640 { STRING_SIZE_TUPLE("suffix"), 1, 1, func_notdir_suffix},
1641 { STRING_SIZE_TUPLE("filter"), 2, 1, func_filter_filterout},
1642 { STRING_SIZE_TUPLE("filter-out"), 2, 1, func_filter_filterout},
1643 { STRING_SIZE_TUPLE("findstring"), 2, 1, func_findstring},
1644 { STRING_SIZE_TUPLE("firstword"), 1, 1, func_firstword},
1645 { STRING_SIZE_TUPLE("join"), 2, 1, func_join},
1646 { STRING_SIZE_TUPLE("patsubst"), 3, 1, func_patsubst},
1647 { STRING_SIZE_TUPLE("shell"), 1, 1, func_shell},
1648 { STRING_SIZE_TUPLE("sort"), 1, 1, func_sort},
1649 { STRING_SIZE_TUPLE("strip"), 1, 1, func_strip},
1650 { STRING_SIZE_TUPLE("wildcard"), 1, 1, func_wildcard},
1651 { STRING_SIZE_TUPLE("word"), 2, 1, func_word},
1652 { STRING_SIZE_TUPLE("wordlist"), 3, 1, func_wordlist},
1653 { STRING_SIZE_TUPLE("words"), 1, 1, func_words},
1654 { STRING_SIZE_TUPLE("origin"), 1, 1, func_origin},
1655 { STRING_SIZE_TUPLE("foreach"), 3, 0, func_foreach},
1656 { STRING_SIZE_TUPLE("call"), -1, 1, func_call},
1657 { STRING_SIZE_TUPLE("error"), 1, 1, func_error},
1658 { STRING_SIZE_TUPLE("warning"), 1, 1, func_error},
1659 { STRING_SIZE_TUPLE("if"), -2, 0, func_if},
1660 #ifdef EXPERIMENTAL
1661 { STRING_SIZE_TUPLE("eq"), 2, 1, func_eq},
1662 { STRING_SIZE_TUPLE("not"), 1, 1, func_not},
1663 #endif
1664 { 0 }
1668 /* These must come after the definition of function_table[]. */
1670 static char *
1671 expand_builtin_function (o, argc, argv, entry_p)
1672 char *o;
1673 int argc;
1674 char **argv;
1675 struct function_table_entry *entry_p;
1677 int min = (entry_p->required_args > 0
1678 ? entry_p->required_args
1679 : -entry_p->required_args);
1681 if (argc < min)
1682 fatal (reading_file,
1683 _("Insufficient number of arguments (%d) to function `%s'"),
1684 argc, entry_p->name);
1686 if (!entry_p->func_ptr)
1687 fatal (reading_file, _("Unimplemented on this platform: function `%s'"),
1688 entry_p->name);
1690 return entry_p->func_ptr (o, argv, entry_p->name);
1693 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1694 opening ( or { and is not null-terminated. If a function invocation
1695 is found, expand it into the buffer at *OP, updating *OP, incrementing
1696 *STRINGP past the reference and returning nonzero. If not, return zero. */
1699 handle_function (op, stringp)
1700 char **op;
1701 char **stringp;
1703 const struct function_table_entry *entry_p;
1704 char openparen = (*stringp)[0];
1705 char closeparen = openparen == '(' ? ')' : '}';
1706 char *beg = *stringp + 1;
1707 char *endref;
1708 int count = 0;
1709 char *argbeg;
1710 register char *p;
1711 char **argv, **argvp;
1712 int nargs;
1714 entry_p = lookup_function (function_table, beg);
1716 if (!entry_p)
1717 return 0;
1719 /* We have found a call to a builtin function. Find the end of the
1720 arguments, and do the function. */
1722 endref = beg + entry_p->len;
1724 /* Space after function name isn't part of the args. */
1725 p = next_token (endref);
1726 argbeg = p;
1728 /* Find the end of the function invocation, counting nested use of
1729 whichever kind of parens we use. Since we're looking, count commas
1730 to get a rough estimate of how many arguments we might have. The
1731 count might be high, but it'll never be low. */
1733 for (nargs=1; *p != '\0'; ++p)
1734 if (*p == ',')
1735 ++nargs;
1736 else if (*p == openparen)
1737 ++count;
1738 else if (*p == closeparen && --count < 0)
1739 break;
1741 if (count >= 0)
1742 fatal (reading_file,
1743 _("unterminated call to function `%s': missing `%c'"),
1744 entry_p->name, closeparen);
1746 /* Get some memory to store the arg pointers. */
1747 argvp = argv = (char **) alloca (sizeof(char *) * (nargs + 2));
1749 /* Chop the string into arguments, then store the end pointer and a nul.
1750 If REQUIRED_ARGS is positive, as soon as we hit that many assume the
1751 rest of the string is part of the last argument. */
1752 *argvp = argbeg;
1753 nargs = 1;
1754 while (entry_p->required_args < 0 || nargs < entry_p->required_args)
1756 char *next = find_next_argument (openparen, closeparen, *argvp, p);
1758 if (!next)
1759 break;
1761 *(++argvp) = next+1;
1762 ++nargs;
1765 *(++argvp) = p+1;
1766 *(++argvp) = 0;
1768 /* If we should expand, do it. */
1769 if (entry_p->expand_args)
1771 for (argvp=argv; argvp[1] != 0; ++argvp)
1772 *argvp = expand_argument (*argvp, argvp[1]-1);
1774 /* end pointer doesn't make sense for expanded stuff. */
1775 *argvp = 0;
1778 /* Finally! Run the function... */
1779 *op = expand_builtin_function (*op, nargs, argv, entry_p);
1781 /* If we allocated memory for the expanded args, free it again. */
1782 if (entry_p->expand_args)
1783 for (argvp=argv; *argvp != 0; ++argvp)
1784 free (*argvp);
1786 *stringp = p;
1788 return 1;
1792 /* User-defined functions. Expand the first argument as either a builtin
1793 function or a make variable, in the context of the rest of the arguments
1794 assigned to $1, $2, ... $N. $0 is the name of the function. */
1796 static char *
1797 func_call (o, argv, funcname)
1798 char *o;
1799 char **argv;
1800 const char *funcname;
1802 char *fname;
1803 int flen;
1804 char *body;
1805 int i;
1806 const struct function_table_entry *entry_p;
1808 /* Calling nothing is a no-op. */
1809 if (*argv[0] == '\0')
1810 return o;
1812 /* There is no way to define a variable with a space in the name, so strip
1813 trailing whitespace as a favor to the user. */
1815 flen = strlen (argv[0]);
1816 fname = argv[0] + flen - 1;
1817 while (isspace (*fname))
1818 --fname;
1819 fname[1] = '\0';
1821 flen = fname - argv[0] + 1;
1822 fname = argv[0];
1824 /* Are we invoking a builtin function? */
1826 entry_p = lookup_function (function_table, fname);
1828 if (entry_p)
1830 for (i=0; argv[i+1]; ++i)
1833 return expand_builtin_function (o, i, argv + 1, entry_p);
1836 /* No, so the first argument is the name of a variable to be expanded and
1837 interpreted as a function. Create the variable reference. */
1838 body = alloca (flen + 4);
1839 body[0]='$';
1840 body[1]='(';
1841 strcpy (body + 2, fname);
1842 body[flen+2]=')';
1843 body[flen+3]= '\0';
1845 /* Set up arguments $(1) .. $(N). $(0) is the function name. */
1847 push_new_variable_scope ();
1849 for (i=0; *argv; ++i, ++argv)
1851 char num[11];
1853 sprintf (num, "%d", i);
1854 define_variable (num, strlen (num), *argv, o_automatic, 0);
1857 /* Expand the body in the context of the arguments, adding the result to
1858 the variable buffer. */
1860 o = variable_expand_string (o, body, flen+3);
1862 pop_variable_scope ();
1864 return o + strlen(o);