Formerly function.c.~29~
[make.git] / function.c
blobc2436bda81b78470b54c8f09cb2769b3dd4ba4a5
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 for (i = 0; envp[i] != 0; ++i)
386 free (envp[i]);
387 free ((char *) envp);
389 /* Record the PID for reap_children. */
390 shell_function_pid = pid;
391 shell_function_completed = 0;
394 /* Set up and read from the pipe. */
396 maxlen = 200;
397 buffer = (char *) xmalloc (maxlen + 1);
399 /* Close the write side of the pipe. */
400 (void) close (pipedes[1]);
402 /* Read from the pipe until it gets EOF. */
403 i = 0;
406 if (i == maxlen)
408 maxlen += 512;
409 buffer = (char *) xrealloc (buffer, maxlen + 1);
412 errno = 0;
413 cc = read (pipedes[0], &buffer[i], maxlen - i);
414 if (cc > 0)
415 i += cc;
417 #ifdef EINTR
418 while (cc > 0 || errno == EINTR);
419 #else
420 while (cc > 0);
421 #endif
423 /* Close the read side of the pipe. */
424 (void) close (pipedes[0]);
426 /* Loop until child_handler sets shell_function_completed
427 to the status of our child shell. */
428 while (shell_function_completed == 0)
429 reap_children (1, 0);
431 shell_function_pid = 0;
433 /* The child_handler function will set shell_function_completed
434 to 1 when the child dies normally, or to -1 if it
435 dies with status 127, which is most likely an exec fail. */
437 if (shell_function_completed == -1)
439 /* This most likely means that the execvp failed,
440 so we should just write out the error message
441 that came in over the pipe from the child. */
442 fputs (buffer, stderr);
443 fflush (stderr);
445 else
447 /* The child finished normally. Replace all
448 newlines in its output with spaces, and put
449 that in the variable output buffer. */
450 if (i > 0)
452 if (buffer[i - 1] == '\n')
453 buffer[--i] = '\0';
454 else
455 buffer[i] = '\0';
456 p = buffer;
457 while ((p = index (p, '\n')) != 0)
458 *p++ = ' ';
459 o = variable_buffer_output (o, buffer, i);
463 free (buffer);
466 free (text);
467 break;
470 case function_origin:
471 /* Expand the argument. */
472 text = expand_argument (text, end);
475 register struct variable *v = lookup_variable (text, strlen (text));
476 if (v == 0)
477 o = variable_buffer_output (o, "undefined", 9);
478 else
479 switch (v->origin)
481 default:
482 case o_invalid:
483 abort ();
484 break;
485 case o_default:
486 o = variable_buffer_output (o, "default", 7);
487 break;
488 case o_env:
489 o = variable_buffer_output (o, "environment", 11);
490 break;
491 case o_file:
492 o = variable_buffer_output (o, "file", 4);
493 break;
494 case o_env_override:
495 o = variable_buffer_output (o, "environment override", 20);
496 break;
497 case o_command:
498 o = variable_buffer_output (o, "command line", 12);
499 break;
500 case o_override:
501 o = variable_buffer_output (o, "override", 8);
502 break;
503 case o_automatic:
504 o = variable_buffer_output (o, "automatic", 9);
505 break;
509 free (text);
510 break;
512 case function_sort:
513 /* Expand the argument. */
514 text = expand_argument (text, end);
517 char **words = (char **) xmalloc (10 * sizeof (char *));
518 unsigned int nwords = 10;
519 register unsigned int wordi = 0;
520 char *t;
522 /* Chop TEXT into words and put them in WORDS. */
523 t = text;
524 while ((p = find_next_token (&t, &len)) != 0)
526 if (wordi >= nwords - 1)
528 nwords += 5;
529 words = (char **) xrealloc ((char *) words,
530 nwords * sizeof (char *));
532 words[wordi++] = savestring (p, len);
535 if (wordi > 0)
537 /* Now sort the list of words. */
538 qsort ((char *) words, wordi, sizeof (char *), alpha_compare);
540 /* Now write the sorted list. */
541 for (i = 0; i < wordi; ++i)
543 len = strlen (words[i]);
544 if (i == wordi - 1 || strlen (words[i + 1]) != len
545 || strcmp (words[i], words[i + 1]))
547 o = variable_buffer_output (o, words[i], len);
548 o = variable_buffer_output (o, " ", 1);
550 free (words[i]);
552 /* Kill the last space. */
553 --o;
556 free ((char *) words);
559 free (text);
560 break;
562 case function_foreach:
564 /* Get three comma-separated arguments but
565 expand only the first two. */
566 char *var, *list;
567 register struct variable *v;
569 count = 0;
570 for (p = text; p < end; ++p)
572 if (*p == startparen)
573 ++count;
574 else if (*p == endparen)
575 --count;
576 else if (*p == ',' && count <= 0)
577 break;
579 if (p == end)
580 BADARGS ("foreach");
581 var = expand_argument (text, p);
583 p2 = p + 1;
584 count = 0;
585 for (p = p2; p < end; ++p)
587 if (*p == startparen)
588 ++count;
589 else if (*p == endparen)
590 --count;
591 else if (*p == ',' && count <= 0)
592 break;
594 if (p == end)
595 BADARGS ("foreach");
596 list = expand_argument (p2, p);
598 ++p;
599 text = savestring (p, end - p);
601 push_new_variable_scope ();
602 v = define_variable (var, strlen (var), "", o_automatic, 0);
603 p3 = list;
604 while ((p = find_next_token (&p3, &len)) != 0)
606 char *result;
607 char save = p[len];
608 p[len] = '\0';
609 v->value = p;
610 result = allocated_variable_expand (text);
611 p[len] = save;
613 o = variable_buffer_output (o, result, strlen (result));
614 o = variable_buffer_output (o, " ", 1);
615 doneany = 1;
616 free (result);
618 if (doneany)
619 /* Kill the last space. */
620 --o;
622 pop_variable_scope ();
624 free (var);
625 free (list);
626 free (text);
628 break;
630 case function_filter:
631 case function_filter_out:
633 struct word
635 struct word *next;
636 char *word;
637 int matched;
638 } *words, *wordtail, *wp;
640 /* Get two comma-separated arguments and expand each one. */
641 count = 0;
642 for (p = text; p < end; ++p)
644 if (*p == startparen)
645 ++count;
646 else if (*p == endparen)
647 --count;
648 else if (*p == ',' && count <= 0)
649 break;
651 if (p == end)
652 BADARGS (function == function_filter ? "filter" : "filter-out");
653 p2 = expand_argument (text, p);
655 text = expand_argument (p + 1, end);
657 /* Chop TEXT up into words and then run each pattern through. */
658 words = wordtail = 0;
659 p3 = text;
660 while ((p = find_next_token (&p3, &len)) != 0)
662 struct word *w = (struct word *) alloca (sizeof (struct word));
663 if (words == 0)
664 words = w;
665 else
666 wordtail->next = w;
667 wordtail = w;
669 if (*p3 != '\0')
670 ++p3;
671 p[len] = '\0';
672 w->word = p;
673 w->matched = 0;
676 if (words != 0)
678 wordtail->next = 0;
680 /* Run each pattern through the words, killing words. */
681 p3 = p2;
682 while ((p = find_next_token (&p3, &len)) != 0)
684 char *percent;
685 char save = p[len];
686 p[len] = '\0';
688 percent = find_percent (p);
689 for (wp = words; wp != 0; wp = wp->next)
690 wp->matched |= (percent == 0 ? streq (p, wp->word)
691 : pattern_matches (p, percent, wp->word));
693 p[len] = save;
696 /* Output the words that matched (or didn't, for filter-out). */
697 for (wp = words; wp != 0; wp = wp->next)
698 if (function == function_filter ? wp->matched : !wp->matched)
700 o = variable_buffer_output (o, wp->word, strlen (wp->word));
701 o = variable_buffer_output (o, " ", 1);
702 doneany = 1;
704 if (doneany)
705 /* Kill the last space. */
706 --o;
709 free (p2);
710 free (text);
712 break;
714 case function_patsubst:
715 /* Get three comma-separated arguments and expand each one. */
716 count = 0;
717 for (p = text; p < end; ++p)
719 if (*p == startparen)
720 ++count;
721 else if (*p == endparen)
722 --count;
723 else if (*p == ',' && count <= 0)
724 break;
726 if (p == end)
727 BADARGS ("patsubst");
729 p2 = p;
730 count = 0;
731 for (++p; p < end; ++p)
733 if (*p == startparen)
734 ++count;
735 else if (*p == endparen)
736 --count;
737 else if (*p == ',' && count <= 0)
738 break;
740 if (p == end)
741 BADARGS ("patsubst");
743 text = expand_argument (text, p2);
744 p3 = expand_argument (p2 + 1, p);
745 p2 = expand_argument (p + 1, end);
747 o = patsubst_expand (o, p2, text, p3, (char *) 0, (char *) 0);
749 free (text);
750 free (p3);
751 free (p2);
752 break;
754 case function_join:
755 /* Get two comma-separated arguments and expand each one. */
756 count = 0;
757 for (p = text; p < end; ++p)
759 if (*p == startparen)
760 ++count;
761 else if (*p == endparen)
762 --count;
763 else if (*p == ',' && count <= 0)
764 break;
766 if (p == end)
767 BADARGS ("join");
768 text = expand_argument (text, p);
770 p = expand_argument (p + 1, end);
773 /* Write each word of the first argument directly followed
774 by the corresponding word of the second argument.
775 If the two arguments have a different number of words,
776 the excess words are just output separated by blanks. */
777 register char *tp, *pp;
778 p2 = text;
779 p3 = p;
782 unsigned int tlen, plen;
784 tp = find_next_token (&p2, &tlen);
785 if (tp != 0)
786 o = variable_buffer_output (o, tp, tlen);
788 pp = find_next_token (&p3, &plen);
789 if (pp != 0)
790 o = variable_buffer_output (o, pp, plen);
792 if (tp != 0 || pp != 0)
794 o = variable_buffer_output (o, " ", 1);
795 doneany = 1;
798 while (tp != 0 || pp != 0);
799 if (doneany)
800 /* Kill the last blank. */
801 --o;
804 free (text);
805 free (p);
806 break;
808 case function_strip:
809 /* Expand the argument. */
810 text = expand_argument (text, end);
812 p2 = text;
813 while ((p = find_next_token (&p2, &i)) != 0)
815 o = variable_buffer_output (o, p, i);
816 o = variable_buffer_output (o, " ", 1);
817 doneany = 1;
819 if (doneany)
820 /* Kill the last space. */
821 --o;
823 free (text);
824 break;
826 case function_wildcard:
827 text = expand_argument (text, end);
829 p = string_glob (text);
830 o = variable_buffer_output (o, p, strlen (p));
832 free (text);
833 break;
835 case function_subst:
836 /* Get three comma-separated arguments and expand each one. */
837 count = 0;
838 for (p = text; p < end; ++p)
840 if (*p == startparen)
841 ++count;
842 else if (*p == endparen)
843 --count;
844 else if (*p == ',' && count <= 0)
845 break;
847 if (p == end)
848 BADARGS ("subst");
850 p2 = p;
851 count = 0;
852 for (++p; p < end; ++p)
854 if (*p == startparen)
855 ++count;
856 else if (*p == endparen)
857 --count;
858 else if (*p == ',' && count <= 0)
859 break;
861 if (p == end)
862 BADARGS ("subst");
864 text = expand_argument (text, p2);
865 p3 = expand_argument (p2 + 1, p);
866 p2 = expand_argument (p + 1, end);
868 o = subst_expand (o, p2, text, p3, strlen (text), strlen (p3), 0, 0);
870 free (text);
871 free (p3);
872 free (p2);
873 break;
875 case function_firstword:
876 /* Expand the argument. */
877 text = expand_argument (text, end);
879 /* Find the first word in TEXT. */
880 p2 = text;
881 p = find_next_token (&p2, &i);
882 if (p != 0)
883 o = variable_buffer_output (o, p, i);
885 free (text);
886 break;
888 case function_word:
889 /* Get two comma-separated arguments and expand each one. */
890 count = 0;
891 for (p = text; p < end; ++p)
893 if (*p == startparen)
894 ++count;
895 else if (*p == endparen)
896 --count;
897 else if (*p == ',' && count <= 0)
898 break;
900 if (p == end)
901 BADARGS ("word");
902 text = expand_argument (text, p);
904 p3 = expand_argument (p + 1, end);
906 /* Check the first argument. */
907 for (p2 = text; *p2 != '\0'; ++p2)
908 if (*p2 < '0' || *p2 > '9')
910 if (reading_filename != 0)
911 makefile_fatal (reading_filename, *reading_lineno_ptr,
912 "non-numeric first argument to `word' function");
913 else
914 fatal ("non-numeric first argument to `word' function");
917 i = (unsigned int) atoi (text);
918 if (i == 0)
920 if (reading_filename != 0)
921 makefile_fatal (reading_filename, *reading_lineno_ptr,
922 "the `word' function takes a one-origin \
923 index argument");
924 else
925 fatal ("the `word' function takes a one-origin index argument");
928 p2 = p3;
929 while ((p = find_next_token (&p2, &len)) != 0)
930 if (--i == 0)
931 break;
932 if (i == 0)
933 o = variable_buffer_output (o, p, len);
935 free (text);
936 free (p3);
937 break;
939 case function_words:
940 /* Expand the argument. */
941 text = expand_argument (text, end);
943 i = 0;
944 p2 = text;
945 while (find_next_token (&p2, (unsigned int *) 0) != 0)
946 ++i;
949 char buf[20];
950 sprintf (buf, "%d", i);
951 o = variable_buffer_output (o, buf, strlen (buf));
954 free (text);
955 break;
957 case function_findstring:
958 /* Get two comma-separated arguments and expand each one. */
959 count = 0;
960 for (p = text; p < end; ++p)
962 if (*p == startparen)
963 ++count;
964 else if (*p == endparen)
965 --count;
966 else if (*p == ',' && count <= 0)
967 break;
969 if (p == end)
970 BADARGS ("findstring");
971 text = expand_argument (text, p);
973 p = expand_argument (p + 1, end);
975 /* Find the first occurrence of the first string in the second. */
976 i = strlen (text);
977 if (sindex (p, 0, text, i) != 0)
978 o = variable_buffer_output (o, text, i);
980 free (p);
981 free (text);
982 break;
984 case function_addsuffix:
985 case function_addprefix:
986 /* Get two comma-separated arguments and expand each one. */
987 count = 0;
988 for (p = text; p < end; ++p)
990 if (*p == startparen)
991 ++count;
992 else if (*p == endparen)
993 --count;
994 else if (*p == ',' && count <= 0)
995 break;
997 if (p == end)
998 BADARGS (function == function_addsuffix ? "addsuffix" : "addprefix");
999 text = expand_argument (text, p);
1000 i = strlen (text);
1002 p2 = expand_argument (p + 1, end);
1004 p3 = p2;
1005 while ((p = find_next_token (&p3, &len)) != 0)
1007 if (function == function_addprefix)
1008 o = variable_buffer_output (o, text, i);
1009 o = variable_buffer_output (o, p, len);
1010 if (function == function_addsuffix)
1011 o = variable_buffer_output (o, text, i);
1012 o = variable_buffer_output (o, " ", 1);
1013 doneany = 1;
1015 if (doneany)
1016 /* Kill last space. */
1017 --o;
1019 free (p2);
1020 free (text);
1021 break;
1023 case function_dir:
1024 case function_basename:
1025 /* Expand the argument. */
1026 text = expand_argument (text, end);
1028 p3 = text;
1029 while ((p2 = find_next_token (&p3, &len)) != 0)
1031 p = p2 + len;
1032 while (p >= p2 && *p != (function == function_dir ? '/' : '.'))
1033 --p;
1034 if (p >= p2)
1036 if (function == function_dir)
1037 ++p;
1038 o = variable_buffer_output (o, p2, p - p2);
1040 else if (function == function_dir)
1041 o = variable_buffer_output (o, "./", 2);
1042 else
1043 /* The entire name is the basename. */
1044 o = variable_buffer_output (o, p2, len);
1046 o = variable_buffer_output (o, " ", 1);
1047 doneany = 1;
1049 if (doneany)
1050 /* Kill last space. */
1051 --o;
1053 free (text);
1054 break;
1056 case function_notdir:
1057 case function_suffix:
1058 /* Expand the argument. */
1059 text = expand_argument (text, end);
1061 p3 = text;
1062 while ((p2 = find_next_token (&p3, &len)) != 0)
1064 p = p2 + len;
1065 while (p >= p2 && *p != (function == function_notdir ? '/' : '.'))
1066 --p;
1067 if (p >= p2)
1069 if (function == function_notdir)
1070 ++p;
1071 o = variable_buffer_output (o, p, len - (p - p2));
1073 else if (function == function_notdir)
1074 o = variable_buffer_output (o, p2, len);
1076 if (function == function_notdir || p >= p2)
1078 o = variable_buffer_output (o, " ", 1);
1079 doneany = 1;
1082 if (doneany)
1083 /* Kill last space. */
1084 --o;
1086 free (text);
1087 break;
1090 return o;
1093 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1094 opening ( or { and is not null-terminated. If a function invocation
1095 is found, expand it into the buffer at *OP, updating *OP, incrementing
1096 *STRINGP past the reference and returning nonzero. If not, return zero. */
1099 handle_function (op, stringp)
1100 char **op;
1101 char **stringp;
1104 register unsigned int code;
1105 unsigned int maxlen;
1106 char *beg = *stringp + 1;
1107 char *endref;
1109 endref = lindex (beg, beg + MAXFUNCTIONLEN, '\0');
1110 maxlen = endref != 0 ? endref - beg : MAXFUNCTIONLEN;
1112 for (code = 0; function_table[code].name != 0; ++code)
1114 if (maxlen < function_table[code].len)
1115 continue;
1116 endref = beg + function_table[code].len;
1117 if (isblank (*endref)
1118 && !strncmp (function_table[code].name, beg,
1119 function_table[code].len))
1120 break;
1122 if (function_table[code].name != 0)
1124 /* We have found a call to an expansion-time function.
1125 Find the end of the arguments, and do the function. */
1127 char openparen = beg[-1], closeparen = openparen == '(' ? ')' : '}';
1128 int count = 0;
1129 char *argbeg;
1130 register char *p;
1132 /* Space after function name isn't part of the args. */
1133 p = next_token (endref);
1134 argbeg = p;
1136 /* Count nested use of whichever kind of parens we use,
1137 so that nested calls and variable refs work. */
1139 for (; *p != '\0'; ++p)
1141 if (*p == openparen)
1142 ++count;
1143 else if (*p == closeparen && --count < 0)
1144 break;
1147 if (count >= 0)
1149 static const char errmsg[]
1150 = "unterminated call to function `%s': missing `%c'";
1151 if (reading_filename == 0)
1152 fatal (errmsg, function_table[code].name, closeparen);
1153 else
1154 makefile_fatal (reading_filename, *reading_lineno_ptr, errmsg,
1155 function_table[code].name, closeparen);
1158 /* We found the end; expand the function call. */
1160 *op = expand_function (*op, function_table[code].function, argbeg, p);
1161 *stringp = p;
1162 return 1;
1165 return 0;
1168 /* Glob-expand LINE. The returned pointer is
1169 only good until the next call to string_glob. */
1171 static char *
1172 string_glob (line)
1173 char *line;
1175 static char *result = 0;
1176 static unsigned int length;
1177 register struct nameseq *chain;
1178 register unsigned int idx;
1180 chain = multi_glob (parse_file_seq
1181 (&line, '\0', sizeof (struct nameseq),
1182 /* We do not want parse_file_seq to strip `./'s.
1183 That would break examples like:
1184 $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)). */
1186 sizeof (struct nameseq));
1188 if (result == 0)
1190 length = 100;
1191 result = (char *) xmalloc (100);
1194 idx = 0;
1195 while (chain != 0)
1197 register char *name = chain->name;
1198 unsigned int len = strlen (name);
1200 struct nameseq *next = chain->next;
1201 free ((char *) chain);
1202 chain = next;
1204 /* multi_glob will pass names without globbing metacharacters
1205 through as is, but we want only files that actually exist. */
1206 if (file_exists_p (name))
1208 if (idx + len + 1 > length)
1210 length += (len + 1) * 2;
1211 result = (char *) xrealloc (result, length);
1213 bcopy (name, &result[idx], len);
1214 idx += len;
1215 result[idx++] = ' ';
1218 free (name);
1221 /* Kill the last space and terminate the string. */
1222 if (idx == 0)
1223 result[0] = '\0';
1224 else
1225 result[idx - 1] = '\0';
1227 return result;