*** empty log message ***
[make.git] / function.c
blobc918ec915339470176ab7b6063071338df9e623c
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 /* Construct the environment. */
341 envp = target_environment ((struct file *) 0);
343 /* For error messages. */
344 if (reading_filename != 0)
346 error_prefix = (char *) alloca (strlen (reading_filename) + 100);
347 sprintf (error_prefix,
348 "%s:%u: ", reading_filename, *reading_lineno_ptr);
350 else
351 error_prefix = "";
353 if (pipe (pipedes) < 0)
355 perror_with_name (error_prefix, "pipe");
356 break;
359 pid = vfork ();
360 if (pid < 0)
361 perror_with_name (error_prefix, "fork");
362 else if (pid == 0)
363 child_execute_job (0, pipedes[1], argv, envp);
364 else
366 /* We are the parent. */
368 char *buffer;
369 unsigned int maxlen;
370 int cc;
372 /* Free the storage only the child needed. */
373 free (argv[0]);
374 free ((char *) argv);
375 for (i = 0; envp[i] != 0; ++i)
376 free (envp[i]);
377 free ((char *) envp);
379 /* Record the PID for reap_children. */
380 shell_function_pid = pid;
381 shell_function_completed = 0;
384 /* Set up and read from the pipe. */
386 maxlen = 200;
387 buffer = (char *) xmalloc (maxlen + 1);
389 /* Close the write side of the pipe. */
390 (void) close (pipedes[1]);
392 /* Read from the pipe until it gets EOF. */
393 i = 0;
396 if (i == maxlen)
398 maxlen += 512;
399 buffer = (char *) xrealloc (buffer, maxlen + 1);
402 errno = 0;
403 cc = read (pipedes[0], &buffer[i], maxlen - i);
404 if (cc > 0)
405 i += cc;
407 #ifdef EINTR
408 while (cc > 0 || errno == EINTR);
409 #else
410 while (cc > 0);
411 #endif
413 /* Close the read side of the pipe. */
414 (void) close (pipedes[0]);
416 /* Loop until child_handler sets shell_function_completed
417 to the status of our child shell. */
418 while (shell_function_completed == 0)
419 reap_children (1, 0);
421 shell_function_pid = 0;
423 /* The child_handler function will set shell_function_completed
424 to 1 when the child dies normally, or to -1 if it
425 dies with status 127, which is most likely an exec fail. */
427 if (shell_function_completed == -1)
429 /* This most likely means that the execvp failed,
430 so we should just write out the error message
431 that came in over the pipe from the child. */
432 fputs (buffer, stderr);
433 fflush (stderr);
435 else
437 /* The child finished normally. Replace all
438 newlines in its output with spaces, and put
439 that in the variable output buffer. */
440 if (i > 0)
442 if (buffer[i - 1] == '\n')
443 buffer[--i] = '\0';
444 else
445 buffer[i] = '\0';
446 p = buffer;
447 while ((p = index (p, '\n')) != 0)
448 *p++ = ' ';
449 o = variable_buffer_output (o, buffer, i);
453 free (buffer);
456 free (text);
457 break;
460 case function_origin:
461 /* Expand the argument. */
462 text = expand_argument (text, end);
465 register struct variable *v = lookup_variable (text, strlen (text));
466 if (v == 0)
467 o = variable_buffer_output (o, "undefined", 9);
468 else
469 switch (v->origin)
471 default:
472 case o_invalid:
473 abort ();
474 break;
475 case o_default:
476 o = variable_buffer_output (o, "default", 7);
477 break;
478 case o_env:
479 o = variable_buffer_output (o, "environment", 11);
480 break;
481 case o_file:
482 o = variable_buffer_output (o, "file", 4);
483 break;
484 case o_env_override:
485 o = variable_buffer_output (o, "environment override", 20);
486 break;
487 case o_command:
488 o = variable_buffer_output (o, "command line", 12);
489 break;
490 case o_override:
491 o = variable_buffer_output (o, "override", 8);
492 break;
493 case o_automatic:
494 o = variable_buffer_output (o, "automatic", 9);
495 break;
499 free (text);
500 break;
502 case function_sort:
503 /* Expand the argument. */
504 text = expand_argument (text, end);
507 char **words = (char **) xmalloc (10 * sizeof (char *));
508 unsigned int nwords = 10;
509 register unsigned int wordi = 0;
510 char *t;
512 /* Chop TEXT into words and put them in WORDS. */
513 t = text;
514 while ((p = find_next_token (&t, &len)) != 0)
516 if (wordi >= nwords - 1)
518 nwords += 5;
519 words = (char **) xrealloc ((char *) words,
520 nwords * sizeof (char *));
522 words[wordi++] = savestring (p, len);
525 if (wordi > 0)
527 /* Now sort the list of words. */
528 qsort ((char *) words, wordi, sizeof (char *), alpha_compare);
530 /* Now write the sorted list. */
531 for (i = 0; i < wordi; ++i)
533 len = strlen (words[i]);
534 if (i == wordi - 1 || strlen (words[i + 1]) != len
535 || strcmp (words[i], words[i + 1]))
537 o = variable_buffer_output (o, words[i], len);
538 o = variable_buffer_output (o, " ", 1);
540 free (words[i]);
542 /* Kill the last space. */
543 --o;
546 free ((char *) words);
549 free (text);
550 break;
552 case function_foreach:
554 /* Get three comma-separated arguments but
555 expand only the first two. */
556 char *var, *list;
557 register struct variable *v;
559 count = 0;
560 for (p = text; p < end; ++p)
562 if (*p == startparen)
563 ++count;
564 else if (*p == endparen)
565 --count;
566 else if (*p == ',' && count <= 0)
567 break;
569 if (p == end)
570 BADARGS ("foreach");
571 var = expand_argument (text, p);
573 p2 = p + 1;
574 count = 0;
575 for (p = p2; p < end; ++p)
577 if (*p == startparen)
578 ++count;
579 else if (*p == endparen)
580 --count;
581 else if (*p == ',' && count <= 0)
582 break;
584 if (p == end)
585 BADARGS ("foreach");
586 list = expand_argument (p2, p);
588 ++p;
589 text = savestring (p, end - p);
591 push_new_variable_scope ();
592 v = define_variable (var, strlen (var), "", o_automatic, 0);
593 p3 = list;
594 while ((p = find_next_token (&p3, &len)) != 0)
596 char *result;
597 char save = p[len];
598 p[len] = '\0';
599 v->value = p;
600 result = allocated_variable_expand (text);
601 p[len] = save;
603 o = variable_buffer_output (o, result, strlen (result));
604 o = variable_buffer_output (o, " ", 1);
605 doneany = 1;
606 free (result);
608 if (doneany)
609 /* Kill the last space. */
610 --o;
612 pop_variable_scope ();
614 free (var);
615 free (list);
616 free (text);
618 break;
620 case function_filter:
621 case function_filter_out:
623 struct word
625 struct word *next;
626 char *word;
627 int matched;
628 } *words, *wordtail, *wp;
630 /* Get two comma-separated arguments and expand each one. */
631 count = 0;
632 for (p = text; p < end; ++p)
634 if (*p == startparen)
635 ++count;
636 else if (*p == endparen)
637 --count;
638 else if (*p == ',' && count <= 0)
639 break;
641 if (p == end)
642 BADARGS (function == function_filter ? "filter" : "filter-out");
643 p2 = expand_argument (text, p);
645 text = expand_argument (p + 1, end);
647 /* Chop TEXT up into words and then run each pattern through. */
648 words = wordtail = 0;
649 p3 = text;
650 while ((p = find_next_token (&p3, &len)) != 0)
652 struct word *w = (struct word *) alloca (sizeof (struct word));
653 if (words == 0)
654 words = w;
655 else
656 wordtail->next = w;
657 wordtail = w;
659 if (*p3 != '\0')
660 ++p3;
661 p[len] = '\0';
662 w->word = p;
663 w->matched = 0;
666 if (words != 0)
668 wordtail->next = 0;
670 /* Run each pattern through the words, killing words. */
671 p3 = p2;
672 while ((p = find_next_token (&p3, &len)) != 0)
674 char *percent;
675 char save = p[len];
676 p[len] = '\0';
678 percent = find_percent (p);
679 for (wp = words; wp != 0; wp = wp->next)
680 wp->matched |= (percent == 0 ? streq (p, wp->word)
681 : pattern_matches (p, percent, wp->word));
683 p[len] = save;
686 /* Output the words that matched (or didn't, for filter-out). */
687 for (wp = words; wp != 0; wp = wp->next)
688 if (function == function_filter ? wp->matched : !wp->matched)
690 o = variable_buffer_output (o, wp->word, strlen (wp->word));
691 o = variable_buffer_output (o, " ", 1);
692 doneany = 1;
694 if (doneany)
695 /* Kill the last space. */
696 --o;
699 free (p2);
700 free (text);
702 break;
704 case function_patsubst:
705 /* Get three comma-separated arguments and expand each one. */
706 count = 0;
707 for (p = text; p < end; ++p)
709 if (*p == startparen)
710 ++count;
711 else if (*p == endparen)
712 --count;
713 else if (*p == ',' && count <= 0)
714 break;
716 if (p == end)
717 BADARGS ("patsubst");
719 p2 = p;
720 count = 0;
721 for (++p; p < end; ++p)
723 if (*p == startparen)
724 ++count;
725 else if (*p == endparen)
726 --count;
727 else if (*p == ',' && count <= 0)
728 break;
730 if (p == end)
731 BADARGS ("patsubst");
733 text = expand_argument (text, p2);
734 p3 = expand_argument (p2 + 1, p);
735 p2 = expand_argument (p + 1, end);
737 o = patsubst_expand (o, p2, text, p3, (char *) 0, (char *) 0);
739 free (text);
740 free (p3);
741 free (p2);
742 break;
744 case function_join:
745 /* Get two comma-separated arguments and expand each one. */
746 count = 0;
747 for (p = text; p < end; ++p)
749 if (*p == startparen)
750 ++count;
751 else if (*p == endparen)
752 --count;
753 else if (*p == ',' && count <= 0)
754 break;
756 if (p == end)
757 BADARGS ("join");
758 text = expand_argument (text, p);
760 p = expand_argument (p + 1, end);
763 /* Write each word of the first argument directly followed
764 by the corresponding word of the second argument.
765 If the two arguments have a different number of words,
766 the excess words are just output separated by blanks. */
767 register char *tp, *pp;
768 p2 = text;
769 p3 = p;
772 unsigned int tlen, plen;
774 tp = find_next_token (&p2, &tlen);
775 if (tp != 0)
776 o = variable_buffer_output (o, tp, tlen);
778 pp = find_next_token (&p3, &plen);
779 if (pp != 0)
780 o = variable_buffer_output (o, pp, plen);
782 if (tp != 0 || pp != 0)
784 o = variable_buffer_output (o, " ", 1);
785 doneany = 1;
788 while (tp != 0 || pp != 0);
789 if (doneany)
790 /* Kill the last blank. */
791 --o;
794 free (text);
795 free (p);
796 break;
798 case function_strip:
799 /* Expand the argument. */
800 text = expand_argument (text, end);
802 p2 = text;
803 while ((p = find_next_token (&p2, &i)) != 0)
805 o = variable_buffer_output (o, p, i);
806 o = variable_buffer_output (o, " ", 1);
807 doneany = 1;
809 if (doneany)
810 /* Kill the last space. */
811 --o;
813 free (text);
814 break;
816 case function_wildcard:
817 text = expand_argument (text, end);
819 p = string_glob (text);
820 o = variable_buffer_output (o, p, strlen (p));
822 free (text);
823 break;
825 case function_subst:
826 /* Get three comma-separated arguments and expand each one. */
827 count = 0;
828 for (p = text; p < end; ++p)
830 if (*p == startparen)
831 ++count;
832 else if (*p == endparen)
833 --count;
834 else if (*p == ',' && count <= 0)
835 break;
837 if (p == end)
838 BADARGS ("subst");
840 p2 = p;
841 count = 0;
842 for (++p; p < end; ++p)
844 if (*p == startparen)
845 ++count;
846 else if (*p == endparen)
847 --count;
848 else if (*p == ',' && count <= 0)
849 break;
851 if (p == end)
852 BADARGS ("subst");
854 text = expand_argument (text, p2);
855 p3 = expand_argument (p2 + 1, p);
856 p2 = expand_argument (p + 1, end);
858 o = subst_expand (o, p2, text, p3, strlen (text), strlen (p3), 0, 0);
860 free (text);
861 free (p3);
862 free (p2);
863 break;
865 case function_firstword:
866 /* Expand the argument. */
867 text = expand_argument (text, end);
869 /* Find the first word in TEXT. */
870 p2 = text;
871 p = find_next_token (&p2, &i);
872 if (p != 0)
873 o = variable_buffer_output (o, p, i);
875 free (text);
876 break;
878 case function_word:
879 /* Get two comma-separated arguments and expand each one. */
880 count = 0;
881 for (p = text; p < end; ++p)
883 if (*p == startparen)
884 ++count;
885 else if (*p == endparen)
886 --count;
887 else if (*p == ',' && count <= 0)
888 break;
890 if (p == end)
891 BADARGS ("word");
892 text = expand_argument (text, p);
894 p3 = expand_argument (p + 1, end);
896 /* Check the first argument. */
897 for (p2 = text; *p2 != '\0'; ++p2)
898 if (*p2 < '0' || *p2 > '9')
900 if (reading_filename != 0)
901 makefile_fatal (reading_filename, *reading_lineno_ptr,
902 "non-numeric first argument to `word' function");
903 else
904 fatal ("non-numeric first argument to `word' function");
907 i = (unsigned int) atoi (text);
908 if (i == 0)
910 if (reading_filename != 0)
911 makefile_fatal (reading_filename, *reading_lineno_ptr,
912 "the `word' function takes a one-origin \
913 index argument");
914 else
915 fatal ("the `word' function takes a one-origin index argument");
918 p2 = p3;
919 while ((p = find_next_token (&p2, &len)) != 0)
920 if (--i == 0)
921 break;
922 if (i == 0)
923 o = variable_buffer_output (o, p, len);
925 free (text);
926 free (p3);
927 break;
929 case function_words:
930 /* Expand the argument. */
931 text = expand_argument (text, end);
933 i = 0;
934 p2 = text;
935 while (find_next_token (&p2, (unsigned int *) 0) != 0)
936 ++i;
939 char buf[20];
940 sprintf (buf, "%d", i);
941 o = variable_buffer_output (o, buf, strlen (buf));
944 free (text);
945 break;
947 case function_findstring:
948 /* Get two comma-separated arguments and expand each one. */
949 count = 0;
950 for (p = text; p < end; ++p)
952 if (*p == startparen)
953 ++count;
954 else if (*p == endparen)
955 --count;
956 else if (*p == ',' && count <= 0)
957 break;
959 if (p == end)
960 BADARGS ("findstring");
961 text = expand_argument (text, p);
963 p = expand_argument (p + 1, end);
965 /* Find the first occurrence of the first string in the second. */
966 i = strlen (text);
967 if (sindex (p, 0, text, i) != 0)
968 o = variable_buffer_output (o, text, i);
970 free (p);
971 free (text);
972 break;
974 case function_addsuffix:
975 case function_addprefix:
976 /* Get two comma-separated arguments and expand each one. */
977 count = 0;
978 for (p = text; p < end; ++p)
980 if (*p == startparen)
981 ++count;
982 else if (*p == endparen)
983 --count;
984 else if (*p == ',' && count <= 0)
985 break;
987 if (p == end)
988 BADARGS (function == function_addsuffix ? "addsuffix" : "addprefix");
989 text = expand_argument (text, p);
990 i = strlen (text);
992 p2 = expand_argument (p + 1, end);
994 p3 = p2;
995 while ((p = find_next_token (&p3, &len)) != 0)
997 if (function == function_addprefix)
998 o = variable_buffer_output (o, text, i);
999 o = variable_buffer_output (o, p, len);
1000 if (function == function_addsuffix)
1001 o = variable_buffer_output (o, text, i);
1002 o = variable_buffer_output (o, " ", 1);
1003 doneany = 1;
1005 if (doneany)
1006 /* Kill last space. */
1007 --o;
1009 free (p2);
1010 free (text);
1011 break;
1013 case function_dir:
1014 case function_basename:
1015 /* Expand the argument. */
1016 text = expand_argument (text, end);
1018 p3 = text;
1019 while ((p2 = find_next_token (&p3, &len)) != 0)
1021 p = p2 + len;
1022 while (p >= p2 && *p != (function == function_dir ? '/' : '.'))
1023 --p;
1024 if (p >= p2)
1026 if (function == function_dir)
1027 ++p;
1028 o = variable_buffer_output (o, p2, p - p2);
1030 else if (function == function_dir)
1031 o = variable_buffer_output (o, "./", 2);
1032 else
1033 /* The entire name is the basename. */
1034 o = variable_buffer_output (o, p2, len);
1036 o = variable_buffer_output (o, " ", 1);
1037 doneany = 1;
1039 if (doneany)
1040 /* Kill last space. */
1041 --o;
1043 free (text);
1044 break;
1046 case function_notdir:
1047 case function_suffix:
1048 /* Expand the argument. */
1049 text = expand_argument (text, end);
1051 p3 = text;
1052 while ((p2 = find_next_token (&p3, &len)) != 0)
1054 p = p2 + len;
1055 while (p >= p2 && *p != (function == function_notdir ? '/' : '.'))
1056 --p;
1057 if (p >= p2)
1059 if (function == function_notdir)
1060 ++p;
1061 o = variable_buffer_output (o, p, len - (p - p2));
1063 else if (function == function_notdir)
1064 o = variable_buffer_output (o, p2, len);
1066 if (function == function_notdir || p >= p2)
1068 o = variable_buffer_output (o, " ", 1);
1069 doneany = 1;
1072 if (doneany)
1073 /* Kill last space. */
1074 --o;
1076 free (text);
1077 break;
1080 return o;
1083 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1084 opening ( or { and is not null-terminated. If a function invocation
1085 is found, expand it into the buffer at *OP, updating *OP, incrementing
1086 *STRINGP past the reference and returning nonzero. If not, return zero. */
1089 handle_function (op, stringp)
1090 char **op;
1091 char **stringp;
1094 register unsigned int code;
1095 unsigned int maxlen;
1096 char *beg = *stringp + 1;
1097 char *endref;
1099 endref = lindex (beg, beg + MAXFUNCTIONLEN, '\0');
1100 maxlen = endref != 0 ? endref - beg : MAXFUNCTIONLEN;
1102 for (code = 0; function_table[code].name != 0; ++code)
1104 if (maxlen < function_table[code].len)
1105 continue;
1106 endref = beg + function_table[code].len;
1107 if (isblank (*endref)
1108 && !strncmp (function_table[code].name, beg,
1109 function_table[code].len))
1110 break;
1112 if (function_table[code].name != 0)
1114 /* We have found a call to an expansion-time function.
1115 Find the end of the arguments, and do the function. */
1117 char openparen = beg[-1], closeparen = openparen == '(' ? ')' : '}';
1118 int count = 0;
1119 char *argbeg;
1120 register char *p;
1122 /* Space after function name isn't part of the args. */
1123 p = next_token (endref);
1124 argbeg = p;
1126 /* Count nested use of whichever kind of parens we use,
1127 so that nested calls and variable refs work. */
1129 for (; *p != '\0'; ++p)
1131 if (*p == openparen)
1132 ++count;
1133 else if (*p == closeparen && --count < 0)
1134 break;
1137 if (count >= 0)
1139 static const char errmsg[]
1140 = "unterminated call to function `%s': missing `%c'";
1141 if (reading_filename == 0)
1142 fatal (errmsg, function_table[code].name, closeparen);
1143 else
1144 makefile_fatal (reading_filename, *reading_lineno_ptr, errmsg,
1145 function_table[code].name, closeparen);
1148 /* We found the end; expand the function call. */
1150 *op = expand_function (*op, function_table[code].function, argbeg, p);
1151 *stringp = p;
1152 return 1;
1155 return 0;
1158 /* Glob-expand LINE. The returned pointer is
1159 only good until the next call to string_glob. */
1161 static char *
1162 string_glob (line)
1163 char *line;
1165 static char *result = 0;
1166 static unsigned int length;
1167 register struct nameseq *chain;
1168 register unsigned int idx;
1170 chain = multi_glob (parse_file_seq
1171 (&line, '\0', sizeof (struct nameseq),
1172 /* We do not want parse_file_seq to strip `./'s.
1173 That would break examples like:
1174 $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)). */
1176 sizeof (struct nameseq));
1178 if (result == 0)
1180 length = 100;
1181 result = (char *) xmalloc (100);
1184 idx = 0;
1185 while (chain != 0)
1187 register char *name = chain->name;
1188 unsigned int len = strlen (name);
1190 struct nameseq *next = chain->next;
1191 free ((char *) chain);
1192 chain = next;
1194 /* multi_glob will pass names without globbing metacharacters
1195 through as is, but we want only files that actually exist. */
1196 if (file_exists_p (name))
1198 if (idx + len + 1 > length)
1200 length += (len + 1) * 2;
1201 result = (char *) xrealloc (result, length);
1203 bcopy (name, &result[idx], len);
1204 idx += len;
1205 result[idx++] = ' ';
1208 free (name);
1211 /* Kill the last space and terminate the string. */
1212 if (idx == 0)
1213 result[0] = '\0';
1214 else
1215 result[idx - 1] = '\0';
1217 return result;