1 /* Variable function expansion for GNU Make.
2 Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994 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)
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. */
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. */
35 subst_expand (o
, text
, subst
, replace
, slen
, rlen
, by_word
, suffix_only
)
38 char *subst
, *replace
;
39 unsigned int slen
, rlen
;
40 int by_word
, suffix_only
;
42 register char *t
= text
;
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
));
50 o
= variable_buffer_output (o
, replace
, rlen
);
56 if ((by_word
| suffix_only
) && slen
== 0)
57 /* When matching by words, the empty string should match
58 the end of each word, rather than the end of the whole text. */
59 p
= end_of_token (next_token (t
));
62 p
= sindex (t
, 0, subst
, slen
);
65 /* No more matches. Output everything left on the end. */
66 o
= variable_buffer_output (o
, t
, strlen (t
));
71 /* Output everything before this occurrence of the string to replace. */
73 o
= variable_buffer_output (o
, t
, p
- t
);
75 /* If we're substituting only by fully matched words,
76 or only at the ends of words, check that this case qualifies. */
78 && ((p
> t
&& !isblank (p
[-1]))
79 || (p
[slen
] != '\0' && !isblank (p
[slen
]))))
81 && (p
[slen
] != '\0' && !isblank (p
[slen
]))))
82 /* Struck out. Output the rest of the string that is
83 no longer to be replaced. */
84 o
= variable_buffer_output (o
, subst
, slen
);
86 /* Output the replacement string. */
87 o
= variable_buffer_output (o
, replace
, rlen
);
89 /* Advance T past the string to be replaced. */
97 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
98 and replacing strings matching PATTERN with REPLACE.
99 If PATTERN_PERCENT is not nil, PATTERN has already been
100 run through find_percent, and PATTERN_PERCENT is the result.
101 If REPLACE_PERCENT is not nil, REPLACE has already been
102 run through find_percent, and REPLACE_PERCENT is the result. */
105 patsubst_expand (o
, text
, pattern
, replace
, pattern_percent
, replace_percent
)
108 register char *pattern
, *replace
;
109 register char *pattern_percent
, *replace_percent
;
111 register int pattern_prepercent_len
, pattern_postpercent_len
;
112 register int replace_prepercent_len
, replace_postpercent_len
;
117 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
118 will be collapsed before we call subst_expand if PATTERN has no %. */
119 if (replace_percent
== 0)
120 replace_percent
= find_percent (replace
);
121 if (replace_percent
!= 0)
123 /* Record the length of REPLACE before and after the % so
124 we don't have to compute these lengths more than once. */
125 replace_prepercent_len
= replace_percent
- replace
;
126 replace_postpercent_len
= strlen (replace_percent
+ 1);
129 /* We store the length of the replacement
130 so we only need to compute it once. */
131 replace_prepercent_len
= strlen (replace
);
133 if (pattern_percent
== 0)
134 pattern_percent
= find_percent (pattern
);
135 if (pattern_percent
== 0)
136 /* With no % in the pattern, this is just a simple substitution. */
137 return subst_expand (o
, text
, pattern
, replace
,
138 strlen (pattern
), strlen (replace
), 1, 0);
140 /* Record the length of PATTERN before and after the %
141 so we don't have to compute it more than once. */
142 pattern_prepercent_len
= pattern_percent
- pattern
;
143 pattern_postpercent_len
= strlen (pattern_percent
+ 1);
145 while ((t
= find_next_token (&text
, &len
)) != 0)
149 /* Is it big enough to match? */
150 if (len
< pattern_prepercent_len
+ pattern_postpercent_len
)
153 /* Does the prefix match? */
154 if (!fail
&& pattern_prepercent_len
> 0
156 || t
[pattern_prepercent_len
- 1] != pattern_percent
[-1]
157 || strncmp (t
+ 1, pattern
+ 1, pattern_prepercent_len
- 1)))
160 /* Does the suffix match? */
161 if (!fail
&& pattern_postpercent_len
> 0
162 && (t
[len
- 1] != pattern_percent
[pattern_postpercent_len
]
163 || t
[len
- pattern_postpercent_len
] != pattern_percent
[1]
164 || strncmp (&t
[len
- pattern_postpercent_len
],
165 &pattern_percent
[1], pattern_postpercent_len
- 1)))
169 /* It didn't match. Output the string. */
170 o
= variable_buffer_output (o
, t
, len
);
173 /* It matched. Output the replacement. */
175 /* Output the part of the replacement before the %. */
176 o
= variable_buffer_output (o
, replace
, replace_prepercent_len
);
178 if (replace_percent
!= 0)
180 /* Output the part of the matched string that
181 matched the % in the pattern. */
182 o
= variable_buffer_output (o
, t
+ pattern_prepercent_len
,
183 len
- (pattern_prepercent_len
184 + pattern_postpercent_len
));
185 /* Output the part of the replacement after the %. */
186 o
= variable_buffer_output (o
, replace_percent
+ 1,
187 replace_postpercent_len
);
191 /* Output a space, but not if the replacement is "". */
192 if (fail
|| replace_prepercent_len
> 0
193 || (replace_percent
!= 0 && len
+ replace_postpercent_len
> 0))
195 o
= variable_buffer_output (o
, " ", 1);
200 /* Kill the last space. */
206 /* Handle variable-expansion-time functions such as $(dir foo/bar) ==> foo/ */
208 /* These enumeration constants distinguish the
209 various expansion-time built-in functions. */
237 /* Greater than the length of any function name. */
238 #define MAXFUNCTIONLEN 11
240 /* The function names and lengths of names, for looking them up. */
246 enum function function
;
249 { "subst", 5, function_subst
},
250 { "addsuffix", 9, function_addsuffix
},
251 { "addprefix", 9, function_addprefix
},
252 { "dir", 3, function_dir
},
253 { "notdir", 6, function_notdir
},
254 { "suffix", 6, function_suffix
},
255 { "basename", 8, function_basename
},
256 { "wildcard", 8, function_wildcard
},
257 { "firstword", 9, function_firstword
},
258 { "word", 4, function_word
},
259 { "words", 5, function_words
},
260 { "findstring", 10, function_findstring
},
261 { "strip", 5, function_strip
},
262 { "join", 4, function_join
},
263 { "patsubst", 8, function_patsubst
},
264 { "filter", 6, function_filter
},
265 { "filter-out", 10, function_filter_out
},
266 { "foreach", 7, function_foreach
},
267 { "sort", 4, function_sort
},
268 { "origin", 6, function_origin
},
269 { "shell", 5, function_shell
},
270 { 0, 0, function_invalid
}
273 /* Return 1 if PATTERN matches WORD, 0 if not. */
276 pattern_matches (pattern
, percent
, word
)
277 register char *pattern
, *percent
, *word
;
279 unsigned int sfxlen
, wordlen
;
283 unsigned int len
= strlen (pattern
) + 1;
284 char *new = (char *) alloca (len
);
285 bcopy (pattern
, new, len
);
287 percent
= find_percent (pattern
);
289 return streq (pattern
, word
);
292 sfxlen
= strlen (percent
+ 1);
293 wordlen
= strlen (word
);
295 if (wordlen
< (percent
- pattern
) + sfxlen
296 || strncmp (pattern
, word
, percent
- pattern
))
299 return !strcmp (percent
+ 1, word
+ (wordlen
- sfxlen
));
302 int shell_function_pid
= 0, shell_function_completed
;
304 /* Perform the function specified by FUNCTION on the text at TEXT.
305 END is points to the end of the argument text (exclusive).
306 The output is written into VARIABLE_BUFFER starting at O. */
308 /* Note this absorbs a semicolon and is safe to use in conditionals. */
309 #define BADARGS(func) \
310 if (reading_filename != 0) \
311 makefile_fatal (reading_filename, *reading_lineno_ptr, \
312 "insufficient arguments to function `%s'", \
315 fatal ("insufficient arguments to function `%s'", func)
318 expand_function (o
, function
, text
, end
)
320 enum function function
;
328 char endparen
= *end
, startparen
= *end
== ')' ? '(' : '{';
343 /* Expand the command line. */
344 text
= expand_argument (text
, end
);
346 /* Construct the argument list. */
347 argv
= construct_command_argv (text
, (char *) NULL
, (struct file
*) 0);
351 /* Using a target environment for `shell' loses in cases like:
352 export var = $(shell echo foobie)
353 because target_environment hits a loop trying to expand $(var)
354 to put it in the environment. This is even more confusing when
355 var was not explicitly exported, but just appeared in the
356 calling environment. */
360 /* Construct the environment. */
361 envp
= target_environment ((struct file
*) 0);
364 /* For error messages. */
365 if (reading_filename
!= 0)
367 error_prefix
= (char *) alloca (strlen (reading_filename
) + 100);
368 sprintf (error_prefix
,
369 "%s:%u: ", reading_filename
, *reading_lineno_ptr
);
374 if (pipe (pipedes
) < 0)
376 perror_with_name (error_prefix
, "pipe");
382 perror_with_name (error_prefix
, "fork");
384 child_execute_job (0, pipedes
[1], argv
, envp
);
387 /* We are the parent. */
393 /* Free the storage only the child needed. */
395 free ((char *) argv
);
397 for (i
= 0; envp
[i
] != 0; ++i
)
399 free ((char *) envp
);
402 /* Record the PID for reap_children. */
403 shell_function_pid
= pid
;
404 shell_function_completed
= 0;
407 /* Set up and read from the pipe. */
410 buffer
= (char *) xmalloc (maxlen
+ 1);
412 /* Close the write side of the pipe. */
413 (void) close (pipedes
[1]);
415 /* Read from the pipe until it gets EOF. */
422 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
426 cc
= read (pipedes
[0], &buffer
[i
], maxlen
- i
);
431 while (cc
> 0 || errno
== EINTR
);
436 /* Close the read side of the pipe. */
437 (void) close (pipedes
[0]);
439 /* Loop until child_handler sets shell_function_completed
440 to the status of our child shell. */
441 while (shell_function_completed
== 0)
442 reap_children (1, 0);
444 shell_function_pid
= 0;
446 /* The child_handler function will set shell_function_completed
447 to 1 when the child dies normally, or to -1 if it
448 dies with status 127, which is most likely an exec fail. */
450 if (shell_function_completed
== -1)
452 /* This most likely means that the execvp failed,
453 so we should just write out the error message
454 that came in over the pipe from the child. */
455 fputs (buffer
, stderr
);
460 /* The child finished normally. Replace all
461 newlines in its output with spaces, and put
462 that in the variable output buffer. */
465 if (buffer
[i
- 1] == '\n')
470 while ((p
= index (p
, '\n')) != 0)
472 o
= variable_buffer_output (o
, buffer
, i
);
483 case function_origin
:
484 /* Expand the argument. */
485 text
= expand_argument (text
, end
);
488 register struct variable
*v
= lookup_variable (text
, strlen (text
));
490 o
= variable_buffer_output (o
, "undefined", 9);
499 o
= variable_buffer_output (o
, "default", 7);
502 o
= variable_buffer_output (o
, "environment", 11);
505 o
= variable_buffer_output (o
, "file", 4);
508 o
= variable_buffer_output (o
, "environment override", 20);
511 o
= variable_buffer_output (o
, "command line", 12);
514 o
= variable_buffer_output (o
, "override", 8);
517 o
= variable_buffer_output (o
, "automatic", 9);
526 /* Expand the argument. */
527 text
= expand_argument (text
, end
);
530 char **words
= (char **) xmalloc (10 * sizeof (char *));
531 unsigned int nwords
= 10;
532 register unsigned int wordi
= 0;
535 /* Chop TEXT into words and put them in WORDS. */
537 while ((p
= find_next_token (&t
, &len
)) != 0)
539 if (wordi
>= nwords
- 1)
542 words
= (char **) xrealloc ((char *) words
,
543 nwords
* sizeof (char *));
545 words
[wordi
++] = savestring (p
, len
);
550 /* Now sort the list of words. */
551 qsort ((char *) words
, wordi
, sizeof (char *), alpha_compare
);
553 /* Now write the sorted list. */
554 for (i
= 0; i
< wordi
; ++i
)
556 len
= strlen (words
[i
]);
557 if (i
== wordi
- 1 || strlen (words
[i
+ 1]) != len
558 || strcmp (words
[i
], words
[i
+ 1]))
560 o
= variable_buffer_output (o
, words
[i
], len
);
561 o
= variable_buffer_output (o
, " ", 1);
565 /* Kill the last space. */
569 free ((char *) words
);
575 case function_foreach
:
577 /* Get three comma-separated arguments but
578 expand only the first two. */
580 register struct variable
*v
;
583 for (p
= text
; p
< end
; ++p
)
585 if (*p
== startparen
)
587 else if (*p
== endparen
)
589 else if (*p
== ',' && count
<= 0)
594 var
= expand_argument (text
, p
);
598 for (p
= p2
; p
< end
; ++p
)
600 if (*p
== startparen
)
602 else if (*p
== endparen
)
604 else if (*p
== ',' && count
<= 0)
609 list
= expand_argument (p2
, p
);
612 text
= savestring (p
, end
- p
);
614 push_new_variable_scope ();
615 v
= define_variable (var
, strlen (var
), "", o_automatic
, 0);
617 while ((p
= find_next_token (&p3
, &len
)) != 0)
623 result
= allocated_variable_expand (text
);
626 o
= variable_buffer_output (o
, result
, strlen (result
));
627 o
= variable_buffer_output (o
, " ", 1);
632 /* Kill the last space. */
635 pop_variable_scope ();
643 case function_filter
:
644 case function_filter_out
:
651 } *words
, *wordtail
, *wp
;
653 /* Get two comma-separated arguments and expand each one. */
655 for (p
= text
; p
< end
; ++p
)
657 if (*p
== startparen
)
659 else if (*p
== endparen
)
661 else if (*p
== ',' && count
<= 0)
665 BADARGS (function
== function_filter
? "filter" : "filter-out");
666 p2
= expand_argument (text
, p
);
668 text
= expand_argument (p
+ 1, end
);
670 /* Chop TEXT up into words and then run each pattern through. */
671 words
= wordtail
= 0;
673 while ((p
= find_next_token (&p3
, &len
)) != 0)
675 struct word
*w
= (struct word
*) alloca (sizeof (struct word
));
693 /* Run each pattern through the words, killing words. */
695 while ((p
= find_next_token (&p3
, &len
)) != 0)
701 percent
= find_percent (p
);
702 for (wp
= words
; wp
!= 0; wp
= wp
->next
)
703 wp
->matched
|= (percent
== 0 ? streq (p
, wp
->word
)
704 : pattern_matches (p
, percent
, wp
->word
));
709 /* Output the words that matched (or didn't, for filter-out). */
710 for (wp
= words
; wp
!= 0; wp
= wp
->next
)
711 if (function
== function_filter
? wp
->matched
: !wp
->matched
)
713 o
= variable_buffer_output (o
, wp
->word
, strlen (wp
->word
));
714 o
= variable_buffer_output (o
, " ", 1);
718 /* Kill the last space. */
727 case function_patsubst
:
728 /* Get three comma-separated arguments and expand each one. */
730 for (p
= text
; p
< end
; ++p
)
732 if (*p
== startparen
)
734 else if (*p
== endparen
)
736 else if (*p
== ',' && count
<= 0)
740 BADARGS ("patsubst");
744 for (++p
; p
< end
; ++p
)
746 if (*p
== startparen
)
748 else if (*p
== endparen
)
750 else if (*p
== ',' && count
<= 0)
754 BADARGS ("patsubst");
756 text
= expand_argument (text
, p2
);
757 p3
= expand_argument (p2
+ 1, p
);
758 p2
= expand_argument (p
+ 1, end
);
760 o
= patsubst_expand (o
, p2
, text
, p3
, (char *) 0, (char *) 0);
768 /* Get two comma-separated arguments and expand each one. */
770 for (p
= text
; p
< end
; ++p
)
772 if (*p
== startparen
)
774 else if (*p
== endparen
)
776 else if (*p
== ',' && count
<= 0)
781 text
= expand_argument (text
, p
);
783 p
= expand_argument (p
+ 1, end
);
786 /* Write each word of the first argument directly followed
787 by the corresponding word of the second argument.
788 If the two arguments have a different number of words,
789 the excess words are just output separated by blanks. */
790 register char *tp
, *pp
;
795 unsigned int tlen
, plen
;
797 tp
= find_next_token (&p2
, &tlen
);
799 o
= variable_buffer_output (o
, tp
, tlen
);
801 pp
= find_next_token (&p3
, &plen
);
803 o
= variable_buffer_output (o
, pp
, plen
);
805 if (tp
!= 0 || pp
!= 0)
807 o
= variable_buffer_output (o
, " ", 1);
811 while (tp
!= 0 || pp
!= 0);
813 /* Kill the last blank. */
822 /* Expand the argument. */
823 text
= expand_argument (text
, end
);
826 while ((p
= find_next_token (&p2
, &i
)) != 0)
828 o
= variable_buffer_output (o
, p
, i
);
829 o
= variable_buffer_output (o
, " ", 1);
833 /* Kill the last space. */
839 case function_wildcard
:
840 text
= expand_argument (text
, end
);
842 p
= string_glob (text
);
843 o
= variable_buffer_output (o
, p
, strlen (p
));
849 /* Get three comma-separated arguments and expand each one. */
851 for (p
= text
; p
< end
; ++p
)
853 if (*p
== startparen
)
855 else if (*p
== endparen
)
857 else if (*p
== ',' && count
<= 0)
865 for (++p
; p
< end
; ++p
)
867 if (*p
== startparen
)
869 else if (*p
== endparen
)
871 else if (*p
== ',' && count
<= 0)
877 text
= expand_argument (text
, p2
);
878 p3
= expand_argument (p2
+ 1, p
);
879 p2
= expand_argument (p
+ 1, end
);
881 o
= subst_expand (o
, p2
, text
, p3
, strlen (text
), strlen (p3
), 0, 0);
888 case function_firstword
:
889 /* Expand the argument. */
890 text
= expand_argument (text
, end
);
892 /* Find the first word in TEXT. */
894 p
= find_next_token (&p2
, &i
);
896 o
= variable_buffer_output (o
, p
, i
);
902 /* Get two comma-separated arguments and expand each one. */
904 for (p
= text
; p
< end
; ++p
)
906 if (*p
== startparen
)
908 else if (*p
== endparen
)
910 else if (*p
== ',' && count
<= 0)
915 text
= expand_argument (text
, p
);
917 p3
= expand_argument (p
+ 1, end
);
919 /* Check the first argument. */
920 for (p2
= text
; *p2
!= '\0'; ++p2
)
921 if (*p2
< '0' || *p2
> '9')
923 if (reading_filename
!= 0)
924 makefile_fatal (reading_filename
, *reading_lineno_ptr
,
925 "non-numeric first argument to `word' function");
927 fatal ("non-numeric first argument to `word' function");
930 i
= (unsigned int) atoi (text
);
933 if (reading_filename
!= 0)
934 makefile_fatal (reading_filename
, *reading_lineno_ptr
,
935 "the `word' function takes a one-origin \
938 fatal ("the `word' function takes a one-origin index argument");
942 while ((p
= find_next_token (&p2
, &len
)) != 0)
946 o
= variable_buffer_output (o
, p
, len
);
953 /* Expand the argument. */
954 text
= expand_argument (text
, end
);
958 while (find_next_token (&p2
, (unsigned int *) 0) != 0)
963 sprintf (buf
, "%d", i
);
964 o
= variable_buffer_output (o
, buf
, strlen (buf
));
970 case function_findstring
:
971 /* Get two comma-separated arguments and expand each one. */
973 for (p
= text
; p
< end
; ++p
)
975 if (*p
== startparen
)
977 else if (*p
== endparen
)
979 else if (*p
== ',' && count
<= 0)
983 BADARGS ("findstring");
984 text
= expand_argument (text
, p
);
986 p
= expand_argument (p
+ 1, end
);
988 /* Find the first occurrence of the first string in the second. */
990 if (sindex (p
, 0, text
, i
) != 0)
991 o
= variable_buffer_output (o
, text
, i
);
997 case function_addsuffix
:
998 case function_addprefix
:
999 /* Get two comma-separated arguments and expand each one. */
1001 for (p
= text
; p
< end
; ++p
)
1003 if (*p
== startparen
)
1005 else if (*p
== endparen
)
1007 else if (*p
== ',' && count
<= 0)
1011 BADARGS (function
== function_addsuffix
? "addsuffix" : "addprefix");
1012 text
= expand_argument (text
, p
);
1015 p2
= expand_argument (p
+ 1, end
);
1018 while ((p
= find_next_token (&p3
, &len
)) != 0)
1020 if (function
== function_addprefix
)
1021 o
= variable_buffer_output (o
, text
, i
);
1022 o
= variable_buffer_output (o
, p
, len
);
1023 if (function
== function_addsuffix
)
1024 o
= variable_buffer_output (o
, text
, i
);
1025 o
= variable_buffer_output (o
, " ", 1);
1029 /* Kill last space. */
1037 case function_basename
:
1038 /* Expand the argument. */
1039 text
= expand_argument (text
, end
);
1042 while ((p2
= find_next_token (&p3
, &len
)) != 0)
1045 while (p
>= p2
&& *p
!= (function
== function_dir
? '/' : '.'))
1049 if (function
== function_dir
)
1051 o
= variable_buffer_output (o
, p2
, p
- p2
);
1053 else if (function
== function_dir
)
1054 o
= variable_buffer_output (o
, "./", 2);
1056 /* The entire name is the basename. */
1057 o
= variable_buffer_output (o
, p2
, len
);
1059 o
= variable_buffer_output (o
, " ", 1);
1063 /* Kill last space. */
1069 case function_notdir
:
1070 case function_suffix
:
1071 /* Expand the argument. */
1072 text
= expand_argument (text
, end
);
1075 while ((p2
= find_next_token (&p3
, &len
)) != 0)
1078 while (p
>= p2
&& *p
!= (function
== function_notdir
? '/' : '.'))
1082 if (function
== function_notdir
)
1084 o
= variable_buffer_output (o
, p
, len
- (p
- p2
));
1086 else if (function
== function_notdir
)
1087 o
= variable_buffer_output (o
, p2
, len
);
1089 if (function
== function_notdir
|| p
>= p2
)
1091 o
= variable_buffer_output (o
, " ", 1);
1096 /* Kill last space. */
1106 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1107 opening ( or { and is not null-terminated. If a function invocation
1108 is found, expand it into the buffer at *OP, updating *OP, incrementing
1109 *STRINGP past the reference and returning nonzero. If not, return zero. */
1112 handle_function (op
, stringp
)
1117 register unsigned int code
;
1118 unsigned int maxlen
;
1119 char *beg
= *stringp
+ 1;
1122 endref
= lindex (beg
, beg
+ MAXFUNCTIONLEN
, '\0');
1123 maxlen
= endref
!= 0 ? endref
- beg
: MAXFUNCTIONLEN
;
1125 for (code
= 0; function_table
[code
].name
!= 0; ++code
)
1127 if (maxlen
< function_table
[code
].len
)
1129 endref
= beg
+ function_table
[code
].len
;
1130 if (isblank (*endref
)
1131 && !strncmp (function_table
[code
].name
, beg
,
1132 function_table
[code
].len
))
1135 if (function_table
[code
].name
!= 0)
1137 /* We have found a call to an expansion-time function.
1138 Find the end of the arguments, and do the function. */
1140 char openparen
= beg
[-1], closeparen
= openparen
== '(' ? ')' : '}';
1145 /* Space after function name isn't part of the args. */
1146 p
= next_token (endref
);
1149 /* Count nested use of whichever kind of parens we use,
1150 so that nested calls and variable refs work. */
1152 for (; *p
!= '\0'; ++p
)
1154 if (*p
== openparen
)
1156 else if (*p
== closeparen
&& --count
< 0)
1162 static const char errmsg
[]
1163 = "unterminated call to function `%s': missing `%c'";
1164 if (reading_filename
== 0)
1165 fatal (errmsg
, function_table
[code
].name
, closeparen
);
1167 makefile_fatal (reading_filename
, *reading_lineno_ptr
, errmsg
,
1168 function_table
[code
].name
, closeparen
);
1171 /* We found the end; expand the function call. */
1173 *op
= expand_function (*op
, function_table
[code
].function
, argbeg
, p
);
1181 /* Glob-expand LINE. The returned pointer is
1182 only good until the next call to string_glob. */
1188 static char *result
= 0;
1189 static unsigned int length
;
1190 register struct nameseq
*chain
;
1191 register unsigned int idx
;
1193 chain
= multi_glob (parse_file_seq
1194 (&line
, '\0', sizeof (struct nameseq
),
1195 /* We do not want parse_file_seq to strip `./'s.
1196 That would break examples like:
1197 $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)). */
1199 sizeof (struct nameseq
));
1204 result
= (char *) xmalloc (100);
1210 register char *name
= chain
->name
;
1211 unsigned int len
= strlen (name
);
1213 struct nameseq
*next
= chain
->next
;
1214 free ((char *) chain
);
1217 /* multi_glob will pass names without globbing metacharacters
1218 through as is, but we want only files that actually exist. */
1219 if (file_exists_p (name
))
1221 if (idx
+ len
+ 1 > length
)
1223 length
+= (len
+ 1) * 2;
1224 result
= (char *) xrealloc (result
, length
);
1226 bcopy (name
, &result
[idx
], len
);
1228 result
[idx
++] = ' ';
1234 /* Kill the last space and terminate the string. */
1238 result
[idx
- 1] = '\0';