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. */
29 static char *string_glob ();
31 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
32 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
33 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
34 nonzero, substitutions are done only on matches which are complete
35 whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
36 done only at the ends of whitespace-delimited words. */
39 subst_expand (o
, text
, subst
, replace
, slen
, rlen
, by_word
, suffix_only
)
42 char *subst
, *replace
;
43 unsigned int slen
, rlen
;
44 int by_word
, suffix_only
;
46 register char *t
= text
;
49 if (slen
== 0 && !by_word
&& !suffix_only
)
51 /* The first occurrence of "" in any string is its end. */
52 o
= variable_buffer_output (o
, t
, strlen (t
));
54 o
= variable_buffer_output (o
, replace
, rlen
);
60 if ((by_word
| suffix_only
) && slen
== 0)
61 /* When matching by words, the empty string should match
62 the end of each word, rather than the end of the whole text. */
63 p
= end_of_token (next_token (t
));
66 p
= sindex (t
, 0, subst
, slen
);
69 /* No more matches. Output everything left on the end. */
70 o
= variable_buffer_output (o
, t
, strlen (t
));
75 /* Output everything before this occurrence of the string to replace. */
77 o
= variable_buffer_output (o
, t
, p
- t
);
79 /* If we're substituting only by fully matched words,
80 or only at the ends of words, check that this case qualifies. */
82 && ((p
> t
&& !isblank (p
[-1]))
83 || (p
[slen
] != '\0' && !isblank (p
[slen
]))))
85 && (p
[slen
] != '\0' && !isblank (p
[slen
]))))
86 /* Struck out. Output the rest of the string that is
87 no longer to be replaced. */
88 o
= variable_buffer_output (o
, subst
, slen
);
90 /* Output the replacement string. */
91 o
= variable_buffer_output (o
, replace
, rlen
);
93 /* Advance T past the string to be replaced. */
101 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
102 and replacing strings matching PATTERN with REPLACE.
103 If PATTERN_PERCENT is not nil, PATTERN has already been
104 run through find_percent, and PATTERN_PERCENT is the result.
105 If REPLACE_PERCENT is not nil, REPLACE has already been
106 run through find_percent, and REPLACE_PERCENT is the result. */
109 patsubst_expand (o
, text
, pattern
, replace
, pattern_percent
, replace_percent
)
112 register char *pattern
, *replace
;
113 register char *pattern_percent
, *replace_percent
;
115 register int pattern_prepercent_len
, pattern_postpercent_len
;
116 register int replace_prepercent_len
, replace_postpercent_len
;
121 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
122 will be collapsed before we call subst_expand if PATTERN has no %. */
123 if (replace_percent
== 0)
124 replace_percent
= find_percent (replace
);
125 if (replace_percent
!= 0)
127 /* Record the length of REPLACE before and after the % so
128 we don't have to compute these lengths more than once. */
129 replace_prepercent_len
= replace_percent
- replace
;
130 replace_postpercent_len
= strlen (replace_percent
+ 1);
133 /* We store the length of the replacement
134 so we only need to compute it once. */
135 replace_prepercent_len
= strlen (replace
);
137 if (pattern_percent
== 0)
138 pattern_percent
= find_percent (pattern
);
139 if (pattern_percent
== 0)
140 /* With no % in the pattern, this is just a simple substitution. */
141 return subst_expand (o
, text
, pattern
, replace
,
142 strlen (pattern
), strlen (replace
), 1, 0);
144 /* Record the length of PATTERN before and after the %
145 so we don't have to compute it more than once. */
146 pattern_prepercent_len
= pattern_percent
- pattern
;
147 pattern_postpercent_len
= strlen (pattern_percent
+ 1);
149 while ((t
= find_next_token (&text
, &len
)) != 0)
153 /* Is it big enough to match? */
154 if (len
< pattern_prepercent_len
+ pattern_postpercent_len
)
157 /* Does the prefix match? */
158 if (!fail
&& pattern_prepercent_len
> 0
160 || t
[pattern_prepercent_len
- 1] != pattern_percent
[-1]
161 || strncmp (t
+ 1, pattern
+ 1, pattern_prepercent_len
- 1)))
164 /* Does the suffix match? */
165 if (!fail
&& pattern_postpercent_len
> 0
166 && (t
[len
- 1] != pattern_percent
[pattern_postpercent_len
]
167 || t
[len
- pattern_postpercent_len
] != pattern_percent
[1]
168 || strncmp (&t
[len
- pattern_postpercent_len
],
169 &pattern_percent
[1], pattern_postpercent_len
- 1)))
173 /* It didn't match. Output the string. */
174 o
= variable_buffer_output (o
, t
, len
);
177 /* It matched. Output the replacement. */
179 /* Output the part of the replacement before the %. */
180 o
= variable_buffer_output (o
, replace
, replace_prepercent_len
);
182 if (replace_percent
!= 0)
184 /* Output the part of the matched string that
185 matched the % in the pattern. */
186 o
= variable_buffer_output (o
, t
+ pattern_prepercent_len
,
187 len
- (pattern_prepercent_len
188 + pattern_postpercent_len
));
189 /* Output the part of the replacement after the %. */
190 o
= variable_buffer_output (o
, replace_percent
+ 1,
191 replace_postpercent_len
);
195 /* Output a space, but not if the replacement is "". */
196 if (fail
|| replace_prepercent_len
> 0
197 || (replace_percent
!= 0 && len
+ replace_postpercent_len
> 0))
199 o
= variable_buffer_output (o
, " ", 1);
204 /* Kill the last space. */
210 /* Handle variable-expansion-time functions such as $(dir foo/bar) ==> foo/ */
212 /* These enumeration constants distinguish the
213 various expansion-time built-in functions. */
241 /* Greater than the length of any function name. */
242 #define MAXFUNCTIONLEN 11
244 /* The function names and lengths of names, for looking them up. */
250 enum function function
;
253 { "subst", 5, function_subst
},
254 { "addsuffix", 9, function_addsuffix
},
255 { "addprefix", 9, function_addprefix
},
256 { "dir", 3, function_dir
},
257 { "notdir", 6, function_notdir
},
258 { "suffix", 6, function_suffix
},
259 { "basename", 8, function_basename
},
260 { "wildcard", 8, function_wildcard
},
261 { "firstword", 9, function_firstword
},
262 { "word", 4, function_word
},
263 { "words", 5, function_words
},
264 { "findstring", 10, function_findstring
},
265 { "strip", 5, function_strip
},
266 { "join", 4, function_join
},
267 { "patsubst", 8, function_patsubst
},
268 { "filter", 6, function_filter
},
269 { "filter-out", 10, function_filter_out
},
270 { "foreach", 7, function_foreach
},
271 { "sort", 4, function_sort
},
272 { "origin", 6, function_origin
},
273 { "shell", 5, function_shell
},
274 { 0, 0, function_invalid
}
277 /* Return 1 if PATTERN matches WORD, 0 if not. */
280 pattern_matches (pattern
, percent
, word
)
281 register char *pattern
, *percent
, *word
;
283 unsigned int sfxlen
, wordlen
;
287 unsigned int len
= strlen (pattern
) + 1;
288 char *new = (char *) alloca (len
);
289 bcopy (pattern
, new, len
);
291 percent
= find_percent (pattern
);
293 return streq (pattern
, word
);
296 sfxlen
= strlen (percent
+ 1);
297 wordlen
= strlen (word
);
299 if (wordlen
< (percent
- pattern
) + sfxlen
300 || strncmp (pattern
, word
, percent
- pattern
))
303 return !strcmp (percent
+ 1, word
+ (wordlen
- sfxlen
));
306 int shell_function_pid
= 0, shell_function_completed
;
308 /* Perform the function specified by FUNCTION on the text at TEXT.
309 END is points to the end of the argument text (exclusive).
310 The output is written into VARIABLE_BUFFER starting at O. */
312 /* Note this absorbs a semicolon and is safe to use in conditionals. */
313 #define BADARGS(func) \
314 if (reading_filename != 0) \
315 makefile_fatal (reading_filename, *reading_lineno_ptr, \
316 "insufficient arguments to function `%s'", \
319 fatal ("insufficient arguments to function `%s'", func)
322 expand_function (o
, function
, text
, end
)
324 enum function function
;
332 char endparen
= *end
, startparen
= *end
== ')' ? '(' : '{';
347 /* Expand the command line. */
348 text
= expand_argument (text
, end
);
350 /* Construct the argument list. */
351 argv
= construct_command_argv (text
, (char *) NULL
, (struct file
*) 0);
355 /* Using a target environment for `shell' loses in cases like:
356 export var = $(shell echo foobie)
357 because target_environment hits a loop trying to expand $(var)
358 to put it in the environment. This is even more confusing when
359 var was not explicitly exported, but just appeared in the
360 calling environment. */
364 /* Construct the environment. */
365 envp
= target_environment ((struct file
*) 0);
368 /* For error messages. */
369 if (reading_filename
!= 0)
371 error_prefix
= (char *) alloca (strlen (reading_filename
) + 100);
372 sprintf (error_prefix
,
373 "%s:%u: ", reading_filename
, *reading_lineno_ptr
);
379 if (pipe (pipedes
) < 0)
381 perror_with_name (error_prefix
, "pipe");
387 perror_with_name (error_prefix
, "fork");
389 child_execute_job (0, pipedes
[1], argv
, envp
);
392 /* We are the parent. */
398 /* Free the storage only the child needed. */
400 free ((char *) argv
);
402 for (i
= 0; envp
[i
] != 0; ++i
)
404 free ((char *) envp
);
407 /* Record the PID for reap_children. */
408 shell_function_pid
= pid
;
409 shell_function_completed
= 0;
412 /* Set up and read from the pipe. */
415 buffer
= (char *) xmalloc (maxlen
+ 1);
417 /* Close the write side of the pipe. */
418 (void) close (pipedes
[1]);
420 /* Read from the pipe until it gets EOF. */
427 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
431 cc
= read (pipedes
[0], &buffer
[i
], maxlen
- i
);
436 while (cc
> 0 || errno
== EINTR
);
441 /* Close the read side of the pipe. */
442 (void) close (pipedes
[0]);
444 /* Loop until child_handler sets shell_function_completed
445 to the status of our child shell. */
446 while (shell_function_completed
== 0)
447 reap_children (1, 0);
449 shell_function_pid
= 0;
451 /* The child_handler function will set shell_function_completed
452 to 1 when the child dies normally, or to -1 if it
453 dies with status 127, which is most likely an exec fail. */
455 if (shell_function_completed
== -1)
457 /* This most likely means that the execvp failed,
458 so we should just write out the error message
459 that came in over the pipe from the child. */
460 fputs (buffer
, stderr
);
465 /* The child finished normally. Replace all
466 newlines in its output with spaces, and put
467 that in the variable output buffer. */
470 if (buffer
[i
- 1] == '\n')
475 while ((p
= index (p
, '\n')) != 0)
477 o
= variable_buffer_output (o
, buffer
, i
);
485 /* MS-DOS can't do fork, but it can do spawn. However, this
486 means that we don't have an opportunity to reopen stdout to
487 trap it. Thus, we save our own stdout onto a new descriptor
488 and dup a temp file's descriptor onto our stdout temporarily.
489 After we spawn the shell program, we dup our own stdout back
490 to the stdout descriptor. The buffer reading is the same as
491 above, except that we're now reading from a file. */
495 char tmp_output
[FILENAME_MAX
];
497 unsigned int maxlen
= 200;
501 strcpy (tmp_output
, "shXXXXXX");
503 child_stdout
= open (tmp_output
,
504 O_WRONLY
|O_CREAT
|O_TRUNC
|O_TEXT
, 0644);
505 save_stdout
= dup (1);
506 dup2 (child_stdout
, 1);
507 spawnvp (P_WAIT
, argv
[0], argv
);
508 dup2 (save_stdout
, 1);
509 close (child_stdout
);
512 child_stdout
= open (tmp_output
, O_RDONLY
|O_TEXT
, 0644);
514 buffer
= xmalloc (maxlen
);
521 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
524 cc
= read (child_stdout
, &buffer
[i
], maxlen
- i
);
529 close (child_stdout
);
534 if (buffer
[i
- 1] == '\n')
539 while ((p
= index (p
, '\n')) != 0)
541 o
= variable_buffer_output (o
, buffer
, i
);
545 #endif /* Not MSDOS. */
551 case function_origin
:
552 /* Expand the argument. */
553 text
= expand_argument (text
, end
);
556 register struct variable
*v
= lookup_variable (text
, strlen (text
));
558 o
= variable_buffer_output (o
, "undefined", 9);
567 o
= variable_buffer_output (o
, "default", 7);
570 o
= variable_buffer_output (o
, "environment", 11);
573 o
= variable_buffer_output (o
, "file", 4);
576 o
= variable_buffer_output (o
, "environment override", 20);
579 o
= variable_buffer_output (o
, "command line", 12);
582 o
= variable_buffer_output (o
, "override", 8);
585 o
= variable_buffer_output (o
, "automatic", 9);
594 /* Expand the argument. */
595 text
= expand_argument (text
, end
);
598 char **words
= (char **) xmalloc (10 * sizeof (char *));
599 unsigned int nwords
= 10;
600 register unsigned int wordi
= 0;
603 /* Chop TEXT into words and put them in WORDS. */
605 while ((p
= find_next_token (&t
, &len
)) != 0)
607 if (wordi
>= nwords
- 1)
610 words
= (char **) xrealloc ((char *) words
,
611 nwords
* sizeof (char *));
613 words
[wordi
++] = savestring (p
, len
);
618 /* Now sort the list of words. */
619 qsort ((char *) words
, wordi
, sizeof (char *), alpha_compare
);
621 /* Now write the sorted list. */
622 for (i
= 0; i
< wordi
; ++i
)
624 len
= strlen (words
[i
]);
625 if (i
== wordi
- 1 || strlen (words
[i
+ 1]) != len
626 || strcmp (words
[i
], words
[i
+ 1]))
628 o
= variable_buffer_output (o
, words
[i
], len
);
629 o
= variable_buffer_output (o
, " ", 1);
633 /* Kill the last space. */
637 free ((char *) words
);
643 case function_foreach
:
645 /* Get three comma-separated arguments but
646 expand only the first two. */
648 register struct variable
*v
;
651 for (p
= text
; p
< end
; ++p
)
653 if (*p
== startparen
)
655 else if (*p
== endparen
)
657 else if (*p
== ',' && count
<= 0)
662 var
= expand_argument (text
, p
);
666 for (p
= p2
; p
< end
; ++p
)
668 if (*p
== startparen
)
670 else if (*p
== endparen
)
672 else if (*p
== ',' && count
<= 0)
677 list
= expand_argument (p2
, p
);
680 text
= savestring (p
, end
- p
);
682 push_new_variable_scope ();
683 v
= define_variable (var
, strlen (var
), "", o_automatic
, 0);
685 while ((p
= find_next_token (&p3
, &len
)) != 0)
691 result
= allocated_variable_expand (text
);
694 o
= variable_buffer_output (o
, result
, strlen (result
));
695 o
= variable_buffer_output (o
, " ", 1);
700 /* Kill the last space. */
703 pop_variable_scope ();
711 case function_filter
:
712 case function_filter_out
:
719 } *words
, *wordtail
, *wp
;
721 /* Get two comma-separated arguments and expand each one. */
723 for (p
= text
; p
< end
; ++p
)
725 if (*p
== startparen
)
727 else if (*p
== endparen
)
729 else if (*p
== ',' && count
<= 0)
733 BADARGS (function
== function_filter
? "filter" : "filter-out");
734 p2
= expand_argument (text
, p
);
736 text
= expand_argument (p
+ 1, end
);
738 /* Chop TEXT up into words and then run each pattern through. */
739 words
= wordtail
= 0;
741 while ((p
= find_next_token (&p3
, &len
)) != 0)
743 struct word
*w
= (struct word
*) alloca (sizeof (struct word
));
761 /* Run each pattern through the words, killing words. */
763 while ((p
= find_next_token (&p3
, &len
)) != 0)
769 percent
= find_percent (p
);
770 for (wp
= words
; wp
!= 0; wp
= wp
->next
)
771 wp
->matched
|= (percent
== 0 ? streq (p
, wp
->word
)
772 : pattern_matches (p
, percent
, wp
->word
));
777 /* Output the words that matched (or didn't, for filter-out). */
778 for (wp
= words
; wp
!= 0; wp
= wp
->next
)
779 if (function
== function_filter
? wp
->matched
: !wp
->matched
)
781 o
= variable_buffer_output (o
, wp
->word
, strlen (wp
->word
));
782 o
= variable_buffer_output (o
, " ", 1);
786 /* Kill the last space. */
795 case function_patsubst
:
796 /* Get three comma-separated arguments and expand each one. */
798 for (p
= text
; p
< end
; ++p
)
800 if (*p
== startparen
)
802 else if (*p
== endparen
)
804 else if (*p
== ',' && count
<= 0)
808 BADARGS ("patsubst");
812 for (++p
; p
< end
; ++p
)
814 if (*p
== startparen
)
816 else if (*p
== endparen
)
818 else if (*p
== ',' && count
<= 0)
822 BADARGS ("patsubst");
824 text
= expand_argument (text
, p2
);
825 p3
= expand_argument (p2
+ 1, p
);
826 p2
= expand_argument (p
+ 1, end
);
828 o
= patsubst_expand (o
, p2
, text
, p3
, (char *) 0, (char *) 0);
836 /* Get two comma-separated arguments and expand each one. */
838 for (p
= text
; p
< end
; ++p
)
840 if (*p
== startparen
)
842 else if (*p
== endparen
)
844 else if (*p
== ',' && count
<= 0)
849 text
= expand_argument (text
, p
);
851 p
= expand_argument (p
+ 1, end
);
854 /* Write each word of the first argument directly followed
855 by the corresponding word of the second argument.
856 If the two arguments have a different number of words,
857 the excess words are just output separated by blanks. */
858 register char *tp
, *pp
;
863 unsigned int tlen
, plen
;
865 tp
= find_next_token (&p2
, &tlen
);
867 o
= variable_buffer_output (o
, tp
, tlen
);
869 pp
= find_next_token (&p3
, &plen
);
871 o
= variable_buffer_output (o
, pp
, plen
);
873 if (tp
!= 0 || pp
!= 0)
875 o
= variable_buffer_output (o
, " ", 1);
879 while (tp
!= 0 || pp
!= 0);
881 /* Kill the last blank. */
890 /* Expand the argument. */
891 text
= expand_argument (text
, end
);
894 while ((p
= find_next_token (&p2
, &i
)) != 0)
896 o
= variable_buffer_output (o
, p
, i
);
897 o
= variable_buffer_output (o
, " ", 1);
901 /* Kill the last space. */
907 case function_wildcard
:
908 text
= expand_argument (text
, end
);
910 p
= string_glob (text
);
911 o
= variable_buffer_output (o
, p
, strlen (p
));
917 /* Get three comma-separated arguments and expand each one. */
919 for (p
= text
; p
< end
; ++p
)
921 if (*p
== startparen
)
923 else if (*p
== endparen
)
925 else if (*p
== ',' && count
<= 0)
933 for (++p
; p
< end
; ++p
)
935 if (*p
== startparen
)
937 else if (*p
== endparen
)
939 else if (*p
== ',' && count
<= 0)
945 text
= expand_argument (text
, p2
);
946 p3
= expand_argument (p2
+ 1, p
);
947 p2
= expand_argument (p
+ 1, end
);
949 o
= subst_expand (o
, p2
, text
, p3
, strlen (text
), strlen (p3
), 0, 0);
956 case function_firstword
:
957 /* Expand the argument. */
958 text
= expand_argument (text
, end
);
960 /* Find the first word in TEXT. */
962 p
= find_next_token (&p2
, &i
);
964 o
= variable_buffer_output (o
, p
, i
);
970 /* Get two comma-separated arguments and expand each one. */
972 for (p
= text
; p
< end
; ++p
)
974 if (*p
== startparen
)
976 else if (*p
== endparen
)
978 else if (*p
== ',' && count
<= 0)
983 text
= expand_argument (text
, p
);
985 p3
= expand_argument (p
+ 1, end
);
987 /* Check the first argument. */
988 for (p2
= text
; *p2
!= '\0'; ++p2
)
989 if (*p2
< '0' || *p2
> '9')
991 if (reading_filename
!= 0)
992 makefile_fatal (reading_filename
, *reading_lineno_ptr
,
993 "non-numeric first argument to `word' function");
995 fatal ("non-numeric first argument to `word' function");
998 i
= (unsigned int) atoi (text
);
1001 if (reading_filename
!= 0)
1002 makefile_fatal (reading_filename
, *reading_lineno_ptr
,
1003 "the `word' function takes a one-origin \
1006 fatal ("the `word' function takes a one-origin index argument");
1010 while ((p
= find_next_token (&p2
, &len
)) != 0)
1014 o
= variable_buffer_output (o
, p
, len
);
1020 case function_words
:
1021 /* Expand the argument. */
1022 text
= expand_argument (text
, end
);
1026 while (find_next_token (&p2
, (unsigned int *) 0) != 0)
1031 sprintf (buf
, "%d", i
);
1032 o
= variable_buffer_output (o
, buf
, strlen (buf
));
1038 case function_findstring
:
1039 /* Get two comma-separated arguments and expand each one. */
1041 for (p
= text
; p
< end
; ++p
)
1043 if (*p
== startparen
)
1045 else if (*p
== endparen
)
1047 else if (*p
== ',' && count
<= 0)
1051 BADARGS ("findstring");
1052 text
= expand_argument (text
, p
);
1054 p
= expand_argument (p
+ 1, end
);
1056 /* Find the first occurrence of the first string in the second. */
1058 if (sindex (p
, 0, text
, i
) != 0)
1059 o
= variable_buffer_output (o
, text
, i
);
1065 case function_addsuffix
:
1066 case function_addprefix
:
1067 /* Get two comma-separated arguments and expand each one. */
1069 for (p
= text
; p
< end
; ++p
)
1071 if (*p
== startparen
)
1073 else if (*p
== endparen
)
1075 else if (*p
== ',' && count
<= 0)
1079 BADARGS (function
== function_addsuffix
? "addsuffix" : "addprefix");
1080 text
= expand_argument (text
, p
);
1083 p2
= expand_argument (p
+ 1, end
);
1086 while ((p
= find_next_token (&p3
, &len
)) != 0)
1088 if (function
== function_addprefix
)
1089 o
= variable_buffer_output (o
, text
, i
);
1090 o
= variable_buffer_output (o
, p
, len
);
1091 if (function
== function_addsuffix
)
1092 o
= variable_buffer_output (o
, text
, i
);
1093 o
= variable_buffer_output (o
, " ", 1);
1097 /* Kill last space. */
1105 case function_basename
:
1106 /* Expand the argument. */
1107 text
= expand_argument (text
, end
);
1110 while ((p2
= find_next_token (&p3
, &len
)) != 0)
1113 while (p
>= p2
&& *p
!= (function
== function_dir
? '/' : '.'))
1117 if (function
== function_dir
)
1119 o
= variable_buffer_output (o
, p2
, p
- p2
);
1121 else if (function
== function_dir
)
1122 o
= variable_buffer_output (o
, "./", 2);
1124 /* The entire name is the basename. */
1125 o
= variable_buffer_output (o
, p2
, len
);
1127 o
= variable_buffer_output (o
, " ", 1);
1131 /* Kill last space. */
1137 case function_notdir
:
1138 case function_suffix
:
1139 /* Expand the argument. */
1140 text
= expand_argument (text
, end
);
1143 while ((p2
= find_next_token (&p3
, &len
)) != 0)
1146 while (p
>= p2
&& *p
!= (function
== function_notdir
? '/' : '.'))
1150 if (function
== function_notdir
)
1152 o
= variable_buffer_output (o
, p
, len
- (p
- p2
));
1154 else if (function
== function_notdir
)
1155 o
= variable_buffer_output (o
, p2
, len
);
1157 if (function
== function_notdir
|| p
>= p2
)
1159 o
= variable_buffer_output (o
, " ", 1);
1164 /* Kill last space. */
1174 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1175 opening ( or { and is not null-terminated. If a function invocation
1176 is found, expand it into the buffer at *OP, updating *OP, incrementing
1177 *STRINGP past the reference and returning nonzero. If not, return zero. */
1180 handle_function (op
, stringp
)
1185 register unsigned int code
;
1186 unsigned int maxlen
;
1187 char *beg
= *stringp
+ 1;
1190 endref
= lindex (beg
, beg
+ MAXFUNCTIONLEN
, '\0');
1191 maxlen
= endref
!= 0 ? endref
- beg
: MAXFUNCTIONLEN
;
1193 for (code
= 0; function_table
[code
].name
!= 0; ++code
)
1195 if (maxlen
< function_table
[code
].len
)
1197 endref
= beg
+ function_table
[code
].len
;
1198 if (isblank (*endref
)
1199 && !strncmp (function_table
[code
].name
, beg
,
1200 function_table
[code
].len
))
1203 if (function_table
[code
].name
!= 0)
1205 /* We have found a call to an expansion-time function.
1206 Find the end of the arguments, and do the function. */
1208 char openparen
= beg
[-1], closeparen
= openparen
== '(' ? ')' : '}';
1213 /* Space after function name isn't part of the args. */
1214 p
= next_token (endref
);
1217 /* Count nested use of whichever kind of parens we use,
1218 so that nested calls and variable refs work. */
1220 for (; *p
!= '\0'; ++p
)
1222 if (*p
== openparen
)
1224 else if (*p
== closeparen
&& --count
< 0)
1230 static const char errmsg
[]
1231 = "unterminated call to function `%s': missing `%c'";
1232 if (reading_filename
== 0)
1233 fatal (errmsg
, function_table
[code
].name
, closeparen
);
1235 makefile_fatal (reading_filename
, *reading_lineno_ptr
, errmsg
,
1236 function_table
[code
].name
, closeparen
);
1239 /* We found the end; expand the function call. */
1241 *op
= expand_function (*op
, function_table
[code
].function
, argbeg
, p
);
1249 /* Glob-expand LINE. The returned pointer is
1250 only good until the next call to string_glob. */
1256 static char *result
= 0;
1257 static unsigned int length
;
1258 register struct nameseq
*chain
;
1259 register unsigned int idx
;
1261 chain
= multi_glob (parse_file_seq
1262 (&line
, '\0', sizeof (struct nameseq
),
1263 /* We do not want parse_file_seq to strip `./'s.
1264 That would break examples like:
1265 $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)). */
1267 sizeof (struct nameseq
));
1272 result
= (char *) xmalloc (100);
1278 register char *name
= chain
->name
;
1279 unsigned int len
= strlen (name
);
1281 struct nameseq
*next
= chain
->next
;
1282 free ((char *) chain
);
1285 /* multi_glob will pass names without globbing metacharacters
1286 through as is, but we want only files that actually exist. */
1287 if (file_exists_p (name
))
1289 if (idx
+ len
+ 1 > length
)
1291 length
+= (len
+ 1) * 2;
1292 result
= (char *) xrealloc (result
, length
);
1294 bcopy (name
, &result
[idx
], len
);
1296 result
[idx
++] = ' ';
1302 /* Kill the last space and terminate the string. */
1306 result
[idx
- 1] = '\0';