Formerly default.c.~29~
[make.git] / function.c
blob0552cd1e6798f2c72c6a127a25f6256d6a1ca227
1 /* Variable function expansion for GNU Make.
2 Copyright (C) 1988, 1989, 1991, 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "variable.h"
21 #include "dep.h"
22 #include "commands.h"
23 #include "job.h"
25 static char *string_glob ();
27 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
28 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
29 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
30 nonzero, substitutions are done only on matches which are complete
31 whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
32 done only at the ends of whitespace-delimited words. */
34 char *
35 subst_expand (o, text, subst, replace, slen, rlen, by_word, suffix_only)
36 char *o;
37 char *text;
38 char *subst, *replace;
39 unsigned int slen, rlen;
40 int by_word, suffix_only;
42 register char *t = text;
43 register char *p;
45 if (slen == 0 && !by_word && !suffix_only)
47 /* The first occurrence of "" in any string is its end. */
48 o = variable_buffer_output (o, t, strlen (t));
49 if (rlen > 0)
50 o = variable_buffer_output (o, replace, rlen);
51 return o;
54 while ((p = sindex (t, 0, subst, slen)) != 0)
56 /* Output everything before this occurrence of the string to replace. */
57 if (p > t)
58 o = variable_buffer_output (o, t, p - t);
60 /* If we're substituting only by fully matched words,
61 or only at the ends of words, check that this case qualifies. */
62 if ((by_word
63 && ((p > t && !isblank (p[-1]))
64 || (p[slen] != '\0' && !isblank (p[slen]))))
65 || (suffix_only
66 && (p[slen] != '\0' && !isblank (p[slen]))))
67 /* Struck out. Output the rest of the string that is
68 no longer to be replaced. */
69 o = variable_buffer_output (o, subst, slen);
70 else if (rlen > 0)
71 /* Output the replacement string. */
72 o = variable_buffer_output (o, replace, rlen);
74 /* Advance T past the string to be replaced. */
75 t = p + slen;
78 /* Output everything left on the end. */
79 if (*t != '\0')
80 o = variable_buffer_output (o, t, strlen (t));
82 return o;
86 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
87 and replacing strings matching PATTERN with REPLACE.
88 If PATTERN_PERCENT is not nil, PATTERN has already been
89 run through find_percent, and PATTERN_PERCENT is the result.
90 If REPLACE_PERCENT is not nil, REPLACE has already been
91 run through find_percent, and REPLACE_PERCENT is the result. */
93 char *
94 patsubst_expand (o, text, pattern, replace, pattern_percent, replace_percent)
95 char *o;
96 char *text;
97 register char *pattern, *replace;
98 register char *pattern_percent, *replace_percent;
100 register int pattern_prepercent_len, pattern_postpercent_len;
101 register int replace_prepercent_len, replace_postpercent_len;
102 register char *t;
103 unsigned int len;
104 int doneany = 0;
106 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
107 will be collapsed before we call subst_expand if PATTERN has no %. */
108 if (replace_percent == 0)
109 replace_percent = find_percent (replace);
110 if (replace_percent != 0)
112 /* Record the length of REPLACE before and after the % so
113 we don't have to compute these lengths more than once. */
114 replace_prepercent_len = replace_percent - replace;
115 replace_postpercent_len = strlen (replace_percent + 1);
117 else
118 /* We store the length of the replacement
119 so we only need to compute it once. */
120 replace_prepercent_len = strlen (replace);
122 if (pattern_percent == 0)
123 pattern_percent = find_percent (pattern);
124 if (pattern_percent == 0)
125 /* With no % in the pattern, this is just a simple substitution. */
126 return subst_expand (o, text, pattern, replace,
127 strlen (pattern), strlen (replace), 1, 0);
129 /* Record the length of PATTERN before and after the %
130 so we don't have to compute it more than once. */
131 pattern_prepercent_len = pattern_percent - pattern;
132 pattern_postpercent_len = strlen (pattern_percent + 1);
134 while ((t = find_next_token (&text, &len)) != 0)
136 int fail = 0;
138 /* Is it big enough to match? */
139 if (len < pattern_prepercent_len + pattern_postpercent_len)
140 fail = 1;
142 /* Does the prefix match? */
143 if (!fail && pattern_prepercent_len > 0
144 && (*t != *pattern
145 || t[pattern_prepercent_len - 1] != pattern_percent[-1]
146 || strncmp (t + 1, pattern + 1, pattern_prepercent_len - 1)))
147 fail = 1;
149 /* Does the suffix match? */
150 if (!fail && pattern_postpercent_len > 0
151 && (t[len - 1] != pattern_percent[pattern_postpercent_len]
152 || t[len - pattern_postpercent_len] != pattern_percent[1]
153 || strncmp (&t[len - pattern_postpercent_len],
154 &pattern_percent[1], pattern_postpercent_len - 1)))
155 fail = 1;
157 if (fail)
158 /* It didn't match. Output the string. */
159 o = variable_buffer_output (o, t, len);
160 else
162 /* It matched. Output the replacement. */
164 /* Output the part of the replacement before the %. */
165 o = variable_buffer_output (o, replace, replace_prepercent_len);
167 if (replace_percent != 0)
169 /* Output the part of the matched string that
170 matched the % in the pattern. */
171 o = variable_buffer_output (o, t + pattern_prepercent_len,
172 len - (pattern_prepercent_len
173 + pattern_postpercent_len));
174 /* Output the part of the replacement after the %. */
175 o = variable_buffer_output (o, replace_percent + 1,
176 replace_postpercent_len);
180 /* Output a space, but not if the replacement is "". */
181 if (fail || replace_prepercent_len > 0
182 || (replace_percent != 0 && len + replace_postpercent_len > 0))
184 o = variable_buffer_output (o, " ", 1);
185 doneany = 1;
188 if (doneany)
189 /* Kill the last space. */
190 --o;
192 return o;
195 /* Handle variable-expansion-time functions such as $(dir foo/bar) ==> foo/ */
197 /* These enumeration constants distinguish the
198 various expansion-time built-in functions. */
200 enum function
202 function_subst,
203 function_addsuffix,
204 function_addprefix,
205 function_dir,
206 function_notdir,
207 function_suffix,
208 function_basename,
209 function_wildcard,
210 function_firstword,
211 function_word,
212 function_words,
213 function_findstring,
214 function_strip,
215 function_join,
216 function_patsubst,
217 function_filter,
218 function_filter_out,
219 function_foreach,
220 function_sort,
221 function_origin,
222 function_shell,
223 function_invalid
226 /* Greater than the length of any function name. */
227 #define MAXFUNCTIONLEN 11
229 /* The function names and lengths of names, for looking them up. */
231 static struct
233 char *name;
234 unsigned int len;
235 enum function function;
236 } function_table[] =
238 { "subst", 5, function_subst },
239 { "addsuffix", 9, function_addsuffix },
240 { "addprefix", 9, function_addprefix },
241 { "dir", 3, function_dir },
242 { "notdir", 6, function_notdir },
243 { "suffix", 6, function_suffix },
244 { "basename", 8, function_basename },
245 { "wildcard", 8, function_wildcard },
246 { "firstword", 9, function_firstword },
247 { "word", 4, function_word },
248 { "words", 5, function_words },
249 { "findstring", 10, function_findstring },
250 { "strip", 5, function_strip },
251 { "join", 4, function_join },
252 { "patsubst", 8, function_patsubst },
253 { "filter", 6, function_filter },
254 { "filter-out", 10, function_filter_out },
255 { "foreach", 7, function_foreach },
256 { "sort", 4, function_sort },
257 { "origin", 6, function_origin },
258 { "shell", 5, function_shell },
259 { 0, 0, function_invalid }
262 /* Return 1 if PATTERN matches WORD, 0 if not. */
265 pattern_matches (pattern, percent, word)
266 register char *pattern, *percent, *word;
268 unsigned int sfxlen, wordlen;
270 if (percent == 0)
272 unsigned int len = strlen (pattern) + 1;
273 char *new = (char *) alloca (len);
274 bcopy (pattern, new, len);
275 pattern = new;
276 percent = find_percent (pattern);
277 if (percent == 0)
278 return streq (pattern, word);
281 sfxlen = strlen (percent + 1);
282 wordlen = strlen (word);
284 if (wordlen < (percent - pattern) + sfxlen
285 || strncmp (pattern, word, percent - pattern))
286 return 0;
288 return !strcmp (percent + 1, word + (wordlen - sfxlen));
291 int shell_function_pid = 0, shell_function_completed;
293 /* Perform the function specified by FUNCTION on the text at TEXT.
294 END is points to the end of the argument text (exclusive).
295 The output is written into VARIABLE_BUFFER starting at O. */
297 /* Note this absorbs a semicolon and is safe to use in conditionals. */
298 #define BADARGS(func) \
299 if (reading_filename != 0) \
300 makefile_fatal (reading_filename, *reading_lineno_ptr, \
301 "insufficient arguments to function `%s'", \
302 func); \
303 else \
304 fatal ("insufficient arguments to function `%s'", func)
306 static char *
307 expand_function (o, function, text, end)
308 char *o;
309 enum function function;
310 char *text;
311 char *end;
313 char *p, *p2, *p3;
314 unsigned int i, len;
315 int doneany = 0;
316 int count;
317 char endparen = *end, startparen = *end == ')' ? '(' : '{';
319 switch (function)
321 default:
322 abort ();
323 break;
325 case function_shell:
327 char **argv, **envp;
328 char *error_prefix;
329 int pipedes[2];
330 int pid;
332 /* Expand the command line. */
333 text = expand_argument (text, end);
335 /* Construct the argument list. */
336 argv = construct_command_argv (text, (char *) NULL, (struct file *) 0);
337 if (argv == 0)
338 break;
340 /* Using a target environment for `shell' loses in cases like:
341 export var = $(shell echo foobie)
342 because target_environment hits a loop trying to expand $(var)
343 to put it in the environment. This is even more confusing when
344 var was not explicitly exported, but just appeared in the
345 calling environment. */
346 #if 1
347 envp = environ;
348 #else
349 /* Construct the environment. */
350 envp = target_environment ((struct file *) 0);
351 #endif
353 /* For error messages. */
354 if (reading_filename != 0)
356 error_prefix = (char *) alloca (strlen (reading_filename) + 100);
357 sprintf (error_prefix,
358 "%s:%u: ", reading_filename, *reading_lineno_ptr);
360 else
361 error_prefix = "";
363 if (pipe (pipedes) < 0)
365 perror_with_name (error_prefix, "pipe");
366 break;
369 pid = vfork ();
370 if (pid < 0)
371 perror_with_name (error_prefix, "fork");
372 else if (pid == 0)
373 child_execute_job (0, pipedes[1], argv, envp);
374 else
376 /* We are the parent. */
378 char *buffer;
379 unsigned int maxlen;
380 int cc;
382 /* Free the storage only the child needed. */
383 free (argv[0]);
384 free ((char *) argv);
385 #if 0
386 for (i = 0; envp[i] != 0; ++i)
387 free (envp[i]);
388 free ((char *) envp);
389 #endif
391 /* Record the PID for reap_children. */
392 shell_function_pid = pid;
393 shell_function_completed = 0;
396 /* Set up and read from the pipe. */
398 maxlen = 200;
399 buffer = (char *) xmalloc (maxlen + 1);
401 /* Close the write side of the pipe. */
402 (void) close (pipedes[1]);
404 /* Read from the pipe until it gets EOF. */
405 i = 0;
408 if (i == maxlen)
410 maxlen += 512;
411 buffer = (char *) xrealloc (buffer, maxlen + 1);
414 errno = 0;
415 cc = read (pipedes[0], &buffer[i], maxlen - i);
416 if (cc > 0)
417 i += cc;
419 #ifdef EINTR
420 while (cc > 0 || errno == EINTR);
421 #else
422 while (cc > 0);
423 #endif
425 /* Close the read side of the pipe. */
426 (void) close (pipedes[0]);
428 /* Loop until child_handler sets shell_function_completed
429 to the status of our child shell. */
430 while (shell_function_completed == 0)
431 reap_children (1, 0);
433 shell_function_pid = 0;
435 /* The child_handler function will set shell_function_completed
436 to 1 when the child dies normally, or to -1 if it
437 dies with status 127, which is most likely an exec fail. */
439 if (shell_function_completed == -1)
441 /* This most likely means that the execvp failed,
442 so we should just write out the error message
443 that came in over the pipe from the child. */
444 fputs (buffer, stderr);
445 fflush (stderr);
447 else
449 /* The child finished normally. Replace all
450 newlines in its output with spaces, and put
451 that in the variable output buffer. */
452 if (i > 0)
454 if (buffer[i - 1] == '\n')
455 buffer[--i] = '\0';
456 else
457 buffer[i] = '\0';
458 p = buffer;
459 while ((p = index (p, '\n')) != 0)
460 *p++ = ' ';
461 o = variable_buffer_output (o, buffer, i);
465 free (buffer);
468 free (text);
469 break;
472 case function_origin:
473 /* Expand the argument. */
474 text = expand_argument (text, end);
477 register struct variable *v = lookup_variable (text, strlen (text));
478 if (v == 0)
479 o = variable_buffer_output (o, "undefined", 9);
480 else
481 switch (v->origin)
483 default:
484 case o_invalid:
485 abort ();
486 break;
487 case o_default:
488 o = variable_buffer_output (o, "default", 7);
489 break;
490 case o_env:
491 o = variable_buffer_output (o, "environment", 11);
492 break;
493 case o_file:
494 o = variable_buffer_output (o, "file", 4);
495 break;
496 case o_env_override:
497 o = variable_buffer_output (o, "environment override", 20);
498 break;
499 case o_command:
500 o = variable_buffer_output (o, "command line", 12);
501 break;
502 case o_override:
503 o = variable_buffer_output (o, "override", 8);
504 break;
505 case o_automatic:
506 o = variable_buffer_output (o, "automatic", 9);
507 break;
511 free (text);
512 break;
514 case function_sort:
515 /* Expand the argument. */
516 text = expand_argument (text, end);
519 char **words = (char **) xmalloc (10 * sizeof (char *));
520 unsigned int nwords = 10;
521 register unsigned int wordi = 0;
522 char *t;
524 /* Chop TEXT into words and put them in WORDS. */
525 t = text;
526 while ((p = find_next_token (&t, &len)) != 0)
528 if (wordi >= nwords - 1)
530 nwords += 5;
531 words = (char **) xrealloc ((char *) words,
532 nwords * sizeof (char *));
534 words[wordi++] = savestring (p, len);
537 if (wordi > 0)
539 /* Now sort the list of words. */
540 qsort ((char *) words, wordi, sizeof (char *), alpha_compare);
542 /* Now write the sorted list. */
543 for (i = 0; i < wordi; ++i)
545 len = strlen (words[i]);
546 if (i == wordi - 1 || strlen (words[i + 1]) != len
547 || strcmp (words[i], words[i + 1]))
549 o = variable_buffer_output (o, words[i], len);
550 o = variable_buffer_output (o, " ", 1);
552 free (words[i]);
554 /* Kill the last space. */
555 --o;
558 free ((char *) words);
561 free (text);
562 break;
564 case function_foreach:
566 /* Get three comma-separated arguments but
567 expand only the first two. */
568 char *var, *list;
569 register struct variable *v;
571 count = 0;
572 for (p = text; p < end; ++p)
574 if (*p == startparen)
575 ++count;
576 else if (*p == endparen)
577 --count;
578 else if (*p == ',' && count <= 0)
579 break;
581 if (p == end)
582 BADARGS ("foreach");
583 var = expand_argument (text, p);
585 p2 = p + 1;
586 count = 0;
587 for (p = p2; p < end; ++p)
589 if (*p == startparen)
590 ++count;
591 else if (*p == endparen)
592 --count;
593 else if (*p == ',' && count <= 0)
594 break;
596 if (p == end)
597 BADARGS ("foreach");
598 list = expand_argument (p2, p);
600 ++p;
601 text = savestring (p, end - p);
603 push_new_variable_scope ();
604 v = define_variable (var, strlen (var), "", o_automatic, 0);
605 p3 = list;
606 while ((p = find_next_token (&p3, &len)) != 0)
608 char *result;
609 char save = p[len];
610 p[len] = '\0';
611 v->value = p;
612 result = allocated_variable_expand (text);
613 p[len] = save;
615 o = variable_buffer_output (o, result, strlen (result));
616 o = variable_buffer_output (o, " ", 1);
617 doneany = 1;
618 free (result);
620 if (doneany)
621 /* Kill the last space. */
622 --o;
624 pop_variable_scope ();
626 free (var);
627 free (list);
628 free (text);
630 break;
632 case function_filter:
633 case function_filter_out:
635 struct word
637 struct word *next;
638 char *word;
639 int matched;
640 } *words, *wordtail, *wp;
642 /* Get two comma-separated arguments and expand each one. */
643 count = 0;
644 for (p = text; p < end; ++p)
646 if (*p == startparen)
647 ++count;
648 else if (*p == endparen)
649 --count;
650 else if (*p == ',' && count <= 0)
651 break;
653 if (p == end)
654 BADARGS (function == function_filter ? "filter" : "filter-out");
655 p2 = expand_argument (text, p);
657 text = expand_argument (p + 1, end);
659 /* Chop TEXT up into words and then run each pattern through. */
660 words = wordtail = 0;
661 p3 = text;
662 while ((p = find_next_token (&p3, &len)) != 0)
664 struct word *w = (struct word *) alloca (sizeof (struct word));
665 if (words == 0)
666 words = w;
667 else
668 wordtail->next = w;
669 wordtail = w;
671 if (*p3 != '\0')
672 ++p3;
673 p[len] = '\0';
674 w->word = p;
675 w->matched = 0;
678 if (words != 0)
680 wordtail->next = 0;
682 /* Run each pattern through the words, killing words. */
683 p3 = p2;
684 while ((p = find_next_token (&p3, &len)) != 0)
686 char *percent;
687 char save = p[len];
688 p[len] = '\0';
690 percent = find_percent (p);
691 for (wp = words; wp != 0; wp = wp->next)
692 wp->matched |= (percent == 0 ? streq (p, wp->word)
693 : pattern_matches (p, percent, wp->word));
695 p[len] = save;
698 /* Output the words that matched (or didn't, for filter-out). */
699 for (wp = words; wp != 0; wp = wp->next)
700 if (function == function_filter ? wp->matched : !wp->matched)
702 o = variable_buffer_output (o, wp->word, strlen (wp->word));
703 o = variable_buffer_output (o, " ", 1);
704 doneany = 1;
706 if (doneany)
707 /* Kill the last space. */
708 --o;
711 free (p2);
712 free (text);
714 break;
716 case function_patsubst:
717 /* Get three comma-separated arguments and expand each one. */
718 count = 0;
719 for (p = text; p < end; ++p)
721 if (*p == startparen)
722 ++count;
723 else if (*p == endparen)
724 --count;
725 else if (*p == ',' && count <= 0)
726 break;
728 if (p == end)
729 BADARGS ("patsubst");
731 p2 = p;
732 count = 0;
733 for (++p; p < end; ++p)
735 if (*p == startparen)
736 ++count;
737 else if (*p == endparen)
738 --count;
739 else if (*p == ',' && count <= 0)
740 break;
742 if (p == end)
743 BADARGS ("patsubst");
745 text = expand_argument (text, p2);
746 p3 = expand_argument (p2 + 1, p);
747 p2 = expand_argument (p + 1, end);
749 o = patsubst_expand (o, p2, text, p3, (char *) 0, (char *) 0);
751 free (text);
752 free (p3);
753 free (p2);
754 break;
756 case function_join:
757 /* Get two comma-separated arguments and expand each one. */
758 count = 0;
759 for (p = text; p < end; ++p)
761 if (*p == startparen)
762 ++count;
763 else if (*p == endparen)
764 --count;
765 else if (*p == ',' && count <= 0)
766 break;
768 if (p == end)
769 BADARGS ("join");
770 text = expand_argument (text, p);
772 p = expand_argument (p + 1, end);
775 /* Write each word of the first argument directly followed
776 by the corresponding word of the second argument.
777 If the two arguments have a different number of words,
778 the excess words are just output separated by blanks. */
779 register char *tp, *pp;
780 p2 = text;
781 p3 = p;
784 unsigned int tlen, plen;
786 tp = find_next_token (&p2, &tlen);
787 if (tp != 0)
788 o = variable_buffer_output (o, tp, tlen);
790 pp = find_next_token (&p3, &plen);
791 if (pp != 0)
792 o = variable_buffer_output (o, pp, plen);
794 if (tp != 0 || pp != 0)
796 o = variable_buffer_output (o, " ", 1);
797 doneany = 1;
800 while (tp != 0 || pp != 0);
801 if (doneany)
802 /* Kill the last blank. */
803 --o;
806 free (text);
807 free (p);
808 break;
810 case function_strip:
811 /* Expand the argument. */
812 text = expand_argument (text, end);
814 p2 = text;
815 while ((p = find_next_token (&p2, &i)) != 0)
817 o = variable_buffer_output (o, p, i);
818 o = variable_buffer_output (o, " ", 1);
819 doneany = 1;
821 if (doneany)
822 /* Kill the last space. */
823 --o;
825 free (text);
826 break;
828 case function_wildcard:
829 text = expand_argument (text, end);
831 p = string_glob (text);
832 o = variable_buffer_output (o, p, strlen (p));
834 free (text);
835 break;
837 case function_subst:
838 /* Get three comma-separated arguments and expand each one. */
839 count = 0;
840 for (p = text; p < end; ++p)
842 if (*p == startparen)
843 ++count;
844 else if (*p == endparen)
845 --count;
846 else if (*p == ',' && count <= 0)
847 break;
849 if (p == end)
850 BADARGS ("subst");
852 p2 = p;
853 count = 0;
854 for (++p; p < end; ++p)
856 if (*p == startparen)
857 ++count;
858 else if (*p == endparen)
859 --count;
860 else if (*p == ',' && count <= 0)
861 break;
863 if (p == end)
864 BADARGS ("subst");
866 text = expand_argument (text, p2);
867 p3 = expand_argument (p2 + 1, p);
868 p2 = expand_argument (p + 1, end);
870 o = subst_expand (o, p2, text, p3, strlen (text), strlen (p3), 0, 0);
872 free (text);
873 free (p3);
874 free (p2);
875 break;
877 case function_firstword:
878 /* Expand the argument. */
879 text = expand_argument (text, end);
881 /* Find the first word in TEXT. */
882 p2 = text;
883 p = find_next_token (&p2, &i);
884 if (p != 0)
885 o = variable_buffer_output (o, p, i);
887 free (text);
888 break;
890 case function_word:
891 /* Get two comma-separated arguments and expand each one. */
892 count = 0;
893 for (p = text; p < end; ++p)
895 if (*p == startparen)
896 ++count;
897 else if (*p == endparen)
898 --count;
899 else if (*p == ',' && count <= 0)
900 break;
902 if (p == end)
903 BADARGS ("word");
904 text = expand_argument (text, p);
906 p3 = expand_argument (p + 1, end);
908 /* Check the first argument. */
909 for (p2 = text; *p2 != '\0'; ++p2)
910 if (*p2 < '0' || *p2 > '9')
912 if (reading_filename != 0)
913 makefile_fatal (reading_filename, *reading_lineno_ptr,
914 "non-numeric first argument to `word' function");
915 else
916 fatal ("non-numeric first argument to `word' function");
919 i = (unsigned int) atoi (text);
920 if (i == 0)
922 if (reading_filename != 0)
923 makefile_fatal (reading_filename, *reading_lineno_ptr,
924 "the `word' function takes a one-origin \
925 index argument");
926 else
927 fatal ("the `word' function takes a one-origin index argument");
930 p2 = p3;
931 while ((p = find_next_token (&p2, &len)) != 0)
932 if (--i == 0)
933 break;
934 if (i == 0)
935 o = variable_buffer_output (o, p, len);
937 free (text);
938 free (p3);
939 break;
941 case function_words:
942 /* Expand the argument. */
943 text = expand_argument (text, end);
945 i = 0;
946 p2 = text;
947 while (find_next_token (&p2, (unsigned int *) 0) != 0)
948 ++i;
951 char buf[20];
952 sprintf (buf, "%d", i);
953 o = variable_buffer_output (o, buf, strlen (buf));
956 free (text);
957 break;
959 case function_findstring:
960 /* Get two comma-separated arguments and expand each one. */
961 count = 0;
962 for (p = text; p < end; ++p)
964 if (*p == startparen)
965 ++count;
966 else if (*p == endparen)
967 --count;
968 else if (*p == ',' && count <= 0)
969 break;
971 if (p == end)
972 BADARGS ("findstring");
973 text = expand_argument (text, p);
975 p = expand_argument (p + 1, end);
977 /* Find the first occurrence of the first string in the second. */
978 i = strlen (text);
979 if (sindex (p, 0, text, i) != 0)
980 o = variable_buffer_output (o, text, i);
982 free (p);
983 free (text);
984 break;
986 case function_addsuffix:
987 case function_addprefix:
988 /* Get two comma-separated arguments and expand each one. */
989 count = 0;
990 for (p = text; p < end; ++p)
992 if (*p == startparen)
993 ++count;
994 else if (*p == endparen)
995 --count;
996 else if (*p == ',' && count <= 0)
997 break;
999 if (p == end)
1000 BADARGS (function == function_addsuffix ? "addsuffix" : "addprefix");
1001 text = expand_argument (text, p);
1002 i = strlen (text);
1004 p2 = expand_argument (p + 1, end);
1006 p3 = p2;
1007 while ((p = find_next_token (&p3, &len)) != 0)
1009 if (function == function_addprefix)
1010 o = variable_buffer_output (o, text, i);
1011 o = variable_buffer_output (o, p, len);
1012 if (function == function_addsuffix)
1013 o = variable_buffer_output (o, text, i);
1014 o = variable_buffer_output (o, " ", 1);
1015 doneany = 1;
1017 if (doneany)
1018 /* Kill last space. */
1019 --o;
1021 free (p2);
1022 free (text);
1023 break;
1025 case function_dir:
1026 case function_basename:
1027 /* Expand the argument. */
1028 text = expand_argument (text, end);
1030 p3 = text;
1031 while ((p2 = find_next_token (&p3, &len)) != 0)
1033 p = p2 + len;
1034 while (p >= p2 && *p != (function == function_dir ? '/' : '.'))
1035 --p;
1036 if (p >= p2)
1038 if (function == function_dir)
1039 ++p;
1040 o = variable_buffer_output (o, p2, p - p2);
1042 else if (function == function_dir)
1043 o = variable_buffer_output (o, "./", 2);
1044 else
1045 /* The entire name is the basename. */
1046 o = variable_buffer_output (o, p2, len);
1048 o = variable_buffer_output (o, " ", 1);
1049 doneany = 1;
1051 if (doneany)
1052 /* Kill last space. */
1053 --o;
1055 free (text);
1056 break;
1058 case function_notdir:
1059 case function_suffix:
1060 /* Expand the argument. */
1061 text = expand_argument (text, end);
1063 p3 = text;
1064 while ((p2 = find_next_token (&p3, &len)) != 0)
1066 p = p2 + len;
1067 while (p >= p2 && *p != (function == function_notdir ? '/' : '.'))
1068 --p;
1069 if (p >= p2)
1071 if (function == function_notdir)
1072 ++p;
1073 o = variable_buffer_output (o, p, len - (p - p2));
1075 else if (function == function_notdir)
1076 o = variable_buffer_output (o, p2, len);
1078 if (function == function_notdir || p >= p2)
1080 o = variable_buffer_output (o, " ", 1);
1081 doneany = 1;
1084 if (doneany)
1085 /* Kill last space. */
1086 --o;
1088 free (text);
1089 break;
1092 return o;
1095 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1096 opening ( or { and is not null-terminated. If a function invocation
1097 is found, expand it into the buffer at *OP, updating *OP, incrementing
1098 *STRINGP past the reference and returning nonzero. If not, return zero. */
1101 handle_function (op, stringp)
1102 char **op;
1103 char **stringp;
1106 register unsigned int code;
1107 unsigned int maxlen;
1108 char *beg = *stringp + 1;
1109 char *endref;
1111 endref = lindex (beg, beg + MAXFUNCTIONLEN, '\0');
1112 maxlen = endref != 0 ? endref - beg : MAXFUNCTIONLEN;
1114 for (code = 0; function_table[code].name != 0; ++code)
1116 if (maxlen < function_table[code].len)
1117 continue;
1118 endref = beg + function_table[code].len;
1119 if (isblank (*endref)
1120 && !strncmp (function_table[code].name, beg,
1121 function_table[code].len))
1122 break;
1124 if (function_table[code].name != 0)
1126 /* We have found a call to an expansion-time function.
1127 Find the end of the arguments, and do the function. */
1129 char openparen = beg[-1], closeparen = openparen == '(' ? ')' : '}';
1130 int count = 0;
1131 char *argbeg;
1132 register char *p;
1134 /* Space after function name isn't part of the args. */
1135 p = next_token (endref);
1136 argbeg = p;
1138 /* Count nested use of whichever kind of parens we use,
1139 so that nested calls and variable refs work. */
1141 for (; *p != '\0'; ++p)
1143 if (*p == openparen)
1144 ++count;
1145 else if (*p == closeparen && --count < 0)
1146 break;
1149 if (count >= 0)
1151 static const char errmsg[]
1152 = "unterminated call to function `%s': missing `%c'";
1153 if (reading_filename == 0)
1154 fatal (errmsg, function_table[code].name, closeparen);
1155 else
1156 makefile_fatal (reading_filename, *reading_lineno_ptr, errmsg,
1157 function_table[code].name, closeparen);
1160 /* We found the end; expand the function call. */
1162 *op = expand_function (*op, function_table[code].function, argbeg, p);
1163 *stringp = p;
1164 return 1;
1167 return 0;
1170 /* Glob-expand LINE. The returned pointer is
1171 only good until the next call to string_glob. */
1173 static char *
1174 string_glob (line)
1175 char *line;
1177 static char *result = 0;
1178 static unsigned int length;
1179 register struct nameseq *chain;
1180 register unsigned int idx;
1182 chain = multi_glob (parse_file_seq
1183 (&line, '\0', sizeof (struct nameseq),
1184 /* We do not want parse_file_seq to strip `./'s.
1185 That would break examples like:
1186 $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)). */
1188 sizeof (struct nameseq));
1190 if (result == 0)
1192 length = 100;
1193 result = (char *) xmalloc (100);
1196 idx = 0;
1197 while (chain != 0)
1199 register char *name = chain->name;
1200 unsigned int len = strlen (name);
1202 struct nameseq *next = chain->next;
1203 free ((char *) chain);
1204 chain = next;
1206 /* multi_glob will pass names without globbing metacharacters
1207 through as is, but we want only files that actually exist. */
1208 if (file_exists_p (name))
1210 if (idx + len + 1 > length)
1212 length += (len + 1) * 2;
1213 result = (char *) xrealloc (result, length);
1215 bcopy (name, &result[idx], len);
1216 idx += len;
1217 result[idx++] = ' ';
1220 free (name);
1223 /* Kill the last space and terminate the string. */
1224 if (idx == 0)
1225 result[0] = '\0';
1226 else
1227 result[idx - 1] = '\0';
1229 return result;