1 /* Variable function expansion for GNU Make.
2 Copyright (C) 1988, 89, 91, 92, 93, 94, 95 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. */
30 static char *string_glob ();
32 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
33 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
34 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
35 nonzero, substitutions are done only on matches which are complete
36 whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
37 done only at the ends of whitespace-delimited words. */
40 subst_expand (o
, text
, subst
, replace
, slen
, rlen
, by_word
, suffix_only
)
43 char *subst
, *replace
;
44 unsigned int slen
, rlen
;
45 int by_word
, suffix_only
;
47 register char *t
= text
;
50 if (slen
== 0 && !by_word
&& !suffix_only
)
52 /* The first occurrence of "" in any string is its end. */
53 o
= variable_buffer_output (o
, t
, strlen (t
));
55 o
= variable_buffer_output (o
, replace
, rlen
);
61 if ((by_word
| suffix_only
) && slen
== 0)
62 /* When matching by words, the empty string should match
63 the end of each word, rather than the end of the whole text. */
64 p
= end_of_token (next_token (t
));
67 p
= sindex (t
, 0, subst
, slen
);
70 /* No more matches. Output everything left on the end. */
71 o
= variable_buffer_output (o
, t
, strlen (t
));
76 /* Output everything before this occurrence of the string to replace. */
78 o
= variable_buffer_output (o
, t
, p
- t
);
80 /* If we're substituting only by fully matched words,
81 or only at the ends of words, check that this case qualifies. */
83 && ((p
> t
&& !isblank (p
[-1]))
84 || (p
[slen
] != '\0' && !isblank (p
[slen
]))))
86 && (p
[slen
] != '\0' && !isblank (p
[slen
]))))
87 /* Struck out. Output the rest of the string that is
88 no longer to be replaced. */
89 o
= variable_buffer_output (o
, subst
, slen
);
91 /* Output the replacement string. */
92 o
= variable_buffer_output (o
, replace
, rlen
);
94 /* Advance T past the string to be replaced. */
102 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
103 and replacing strings matching PATTERN with REPLACE.
104 If PATTERN_PERCENT is not nil, PATTERN has already been
105 run through find_percent, and PATTERN_PERCENT is the result.
106 If REPLACE_PERCENT is not nil, REPLACE has already been
107 run through find_percent, and REPLACE_PERCENT is the result. */
110 patsubst_expand (o
, text
, pattern
, replace
, pattern_percent
, replace_percent
)
113 register char *pattern
, *replace
;
114 register char *pattern_percent
, *replace_percent
;
116 register int pattern_prepercent_len
, pattern_postpercent_len
;
117 register int replace_prepercent_len
, replace_postpercent_len
;
122 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
123 will be collapsed before we call subst_expand if PATTERN has no %. */
124 if (replace_percent
== 0)
125 replace_percent
= find_percent (replace
);
126 if (replace_percent
!= 0)
128 /* Record the length of REPLACE before and after the % so
129 we don't have to compute these lengths more than once. */
130 replace_prepercent_len
= replace_percent
- replace
;
131 replace_postpercent_len
= strlen (replace_percent
+ 1);
134 /* We store the length of the replacement
135 so we only need to compute it once. */
136 replace_prepercent_len
= strlen (replace
);
138 if (pattern_percent
== 0)
139 pattern_percent
= find_percent (pattern
);
140 if (pattern_percent
== 0)
141 /* With no % in the pattern, this is just a simple substitution. */
142 return subst_expand (o
, text
, pattern
, replace
,
143 strlen (pattern
), strlen (replace
), 1, 0);
145 /* Record the length of PATTERN before and after the %
146 so we don't have to compute it more than once. */
147 pattern_prepercent_len
= pattern_percent
- pattern
;
148 pattern_postpercent_len
= strlen (pattern_percent
+ 1);
150 while ((t
= find_next_token (&text
, &len
)) != 0)
154 /* Is it big enough to match? */
155 if (len
< pattern_prepercent_len
+ pattern_postpercent_len
)
158 /* Does the prefix match? */
159 if (!fail
&& pattern_prepercent_len
> 0
161 || t
[pattern_prepercent_len
- 1] != pattern_percent
[-1]
162 || strncmp (t
+ 1, pattern
+ 1, pattern_prepercent_len
- 1)))
165 /* Does the suffix match? */
166 if (!fail
&& pattern_postpercent_len
> 0
167 && (t
[len
- 1] != pattern_percent
[pattern_postpercent_len
]
168 || t
[len
- pattern_postpercent_len
] != pattern_percent
[1]
169 || strncmp (&t
[len
- pattern_postpercent_len
],
170 &pattern_percent
[1], pattern_postpercent_len
- 1)))
174 /* It didn't match. Output the string. */
175 o
= variable_buffer_output (o
, t
, len
);
178 /* It matched. Output the replacement. */
180 /* Output the part of the replacement before the %. */
181 o
= variable_buffer_output (o
, replace
, replace_prepercent_len
);
183 if (replace_percent
!= 0)
185 /* Output the part of the matched string that
186 matched the % in the pattern. */
187 o
= variable_buffer_output (o
, t
+ pattern_prepercent_len
,
188 len
- (pattern_prepercent_len
189 + pattern_postpercent_len
));
190 /* Output the part of the replacement after the %. */
191 o
= variable_buffer_output (o
, replace_percent
+ 1,
192 replace_postpercent_len
);
196 /* Output a space, but not if the replacement is "". */
197 if (fail
|| replace_prepercent_len
> 0
198 || (replace_percent
!= 0 && len
+ replace_postpercent_len
> 0))
200 o
= variable_buffer_output (o
, " ", 1);
205 /* Kill the last space. */
211 /* Handle variable-expansion-time functions such as $(dir foo/bar) ==> foo/ */
213 /* These enumeration constants distinguish the
214 various expansion-time built-in functions. */
242 /* Greater than the length of any function name. */
243 #define MAXFUNCTIONLEN 11
245 /* The function names and lengths of names, for looking them up. */
251 enum function function
;
254 { "subst", 5, function_subst
},
255 { "addsuffix", 9, function_addsuffix
},
256 { "addprefix", 9, function_addprefix
},
257 { "dir", 3, function_dir
},
258 { "notdir", 6, function_notdir
},
259 { "suffix", 6, function_suffix
},
260 { "basename", 8, function_basename
},
261 { "wildcard", 8, function_wildcard
},
262 { "firstword", 9, function_firstword
},
263 { "word", 4, function_word
},
264 { "words", 5, function_words
},
265 { "findstring", 10, function_findstring
},
266 { "strip", 5, function_strip
},
267 { "join", 4, function_join
},
268 { "patsubst", 8, function_patsubst
},
269 { "filter", 6, function_filter
},
270 { "filter-out", 10, function_filter_out
},
271 { "foreach", 7, function_foreach
},
272 { "sort", 4, function_sort
},
273 { "origin", 6, function_origin
},
274 { "shell", 5, function_shell
},
275 { 0, 0, function_invalid
}
278 /* Return 1 if PATTERN matches WORD, 0 if not. */
281 pattern_matches (pattern
, percent
, word
)
282 register char *pattern
, *percent
, *word
;
284 unsigned int sfxlen
, wordlen
;
288 unsigned int len
= strlen (pattern
) + 1;
289 char *new = (char *) alloca (len
);
290 bcopy (pattern
, new, len
);
292 percent
= find_percent (pattern
);
294 return streq (pattern
, word
);
297 sfxlen
= strlen (percent
+ 1);
298 wordlen
= strlen (word
);
300 if (wordlen
< (percent
- pattern
) + sfxlen
301 || strncmp (pattern
, word
, percent
- pattern
))
304 return !strcmp (percent
+ 1, word
+ (wordlen
- sfxlen
));
307 int shell_function_pid
= 0, shell_function_completed
;
309 /* Perform the function specified by FUNCTION on the text at TEXT.
310 END is points to the end of the argument text (exclusive).
311 The output is written into VARIABLE_BUFFER starting at O. */
313 /* Note this absorbs a semicolon and is safe to use in conditionals. */
314 #define BADARGS(func) \
315 if (reading_filename != 0) \
316 makefile_fatal (reading_filename, *reading_lineno_ptr, \
317 "insufficient arguments to function `%s'", \
320 fatal ("insufficient arguments to function `%s'", func)
323 expand_function (o
, function
, text
, end
)
325 enum function function
;
333 char endparen
= *end
, startparen
= *end
== ')' ? '(' : '{';
348 /* Expand the command line. */
349 text
= expand_argument (text
, end
);
351 /* Construct the argument list. */
352 argv
= construct_command_argv (text
, (char *) NULL
, (struct file
*) 0);
356 /* Using a target environment for `shell' loses in cases like:
357 export var = $(shell echo foobie)
358 because target_environment hits a loop trying to expand $(var)
359 to put it in the environment. This is even more confusing when
360 var was not explicitly exported, but just appeared in the
361 calling environment. */
365 /* Construct the environment. */
366 envp
= target_environment ((struct file
*) 0);
369 /* For error messages. */
370 if (reading_filename
!= 0)
372 error_prefix
= (char *) alloca (strlen (reading_filename
) + 100);
373 sprintf (error_prefix
,
374 "%s:%u: ", reading_filename
, *reading_lineno_ptr
);
380 if (pipe (pipedes
) < 0)
382 perror_with_name (error_prefix
, "pipe");
388 perror_with_name (error_prefix
, "fork");
390 child_execute_job (0, pipedes
[1], argv
, envp
);
393 /* We are the parent. */
399 /* Free the storage only the child needed. */
401 free ((char *) argv
);
403 for (i
= 0; envp
[i
] != 0; ++i
)
405 free ((char *) envp
);
408 /* Record the PID for reap_children. */
409 shell_function_pid
= pid
;
410 shell_function_completed
= 0;
413 /* Set up and read from the pipe. */
416 buffer
= (char *) xmalloc (maxlen
+ 1);
418 /* Close the write side of the pipe. */
419 (void) close (pipedes
[1]);
421 /* Read from the pipe until it gets EOF. */
428 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
432 cc
= read (pipedes
[0], &buffer
[i
], maxlen
- i
);
437 while (cc
> 0 || errno
== EINTR
);
442 /* Close the read side of the pipe. */
443 (void) close (pipedes
[0]);
445 /* Loop until child_handler sets shell_function_completed
446 to the status of our child shell. */
447 while (shell_function_completed
== 0)
448 reap_children (1, 0);
450 shell_function_pid
= 0;
452 /* The child_handler function will set shell_function_completed
453 to 1 when the child dies normally, or to -1 if it
454 dies with status 127, which is most likely an exec fail. */
456 if (shell_function_completed
== -1)
458 /* This most likely means that the execvp failed,
459 so we should just write out the error message
460 that came in over the pipe from the child. */
461 fputs (buffer
, stderr
);
466 /* The child finished normally. Replace all
467 newlines in its output with spaces, and put
468 that in the variable output buffer. */
471 if (buffer
[i
- 1] == '\n')
476 while ((p
= index (p
, '\n')) != 0)
478 o
= variable_buffer_output (o
, buffer
, i
);
486 /* MS-DOS can't do fork, but it can do spawn. However, this
487 means that we don't have an opportunity to reopen stdout to
488 trap it. Thus, we save our own stdout onto a new descriptor
489 and dup a temp file's descriptor onto our stdout temporarily.
490 After we spawn the shell program, we dup our own stdout back
491 to the stdout descriptor. The buffer reading is the same as
492 above, except that we're now reading from a file. */
496 char tmp_output
[FILENAME_MAX
];
498 unsigned int maxlen
= 200;
502 strcpy (tmp_output
, "shXXXXXX");
504 child_stdout
= open (tmp_output
,
505 O_WRONLY
|O_CREAT
|O_TRUNC
|O_TEXT
, 0644);
506 save_stdout
= dup (1);
507 dup2 (child_stdout
, 1);
508 spawnvp (P_WAIT
, argv
[0], argv
);
509 dup2 (save_stdout
, 1);
510 close (child_stdout
);
513 child_stdout
= open (tmp_output
, O_RDONLY
|O_TEXT
, 0644);
515 buffer
= xmalloc (maxlen
);
522 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
525 cc
= read (child_stdout
, &buffer
[i
], maxlen
- i
);
530 close (child_stdout
);
535 if (buffer
[i
- 1] == '\n')
540 while ((p
= index (p
, '\n')) != 0)
542 o
= variable_buffer_output (o
, buffer
, i
);
546 #endif /* Not MSDOS. */
552 case function_origin
:
553 /* Expand the argument. */
554 text
= expand_argument (text
, end
);
557 register struct variable
*v
= lookup_variable (text
, strlen (text
));
559 o
= variable_buffer_output (o
, "undefined", 9);
568 o
= variable_buffer_output (o
, "default", 7);
571 o
= variable_buffer_output (o
, "environment", 11);
574 o
= variable_buffer_output (o
, "file", 4);
577 o
= variable_buffer_output (o
, "environment override", 20);
580 o
= variable_buffer_output (o
, "command line", 12);
583 o
= variable_buffer_output (o
, "override", 8);
586 o
= variable_buffer_output (o
, "automatic", 9);
595 /* Expand the argument. */
596 text
= expand_argument (text
, end
);
599 char **words
= (char **) xmalloc (10 * sizeof (char *));
600 unsigned int nwords
= 10;
601 register unsigned int wordi
= 0;
604 /* Chop TEXT into words and put them in WORDS. */
606 while ((p
= find_next_token (&t
, &len
)) != 0)
608 if (wordi
>= nwords
- 1)
611 words
= (char **) xrealloc ((char *) words
,
612 nwords
* sizeof (char *));
614 words
[wordi
++] = savestring (p
, len
);
619 /* Now sort the list of words. */
620 qsort ((char *) words
, wordi
, sizeof (char *), alpha_compare
);
622 /* Now write the sorted list. */
623 for (i
= 0; i
< wordi
; ++i
)
625 len
= strlen (words
[i
]);
626 if (i
== wordi
- 1 || strlen (words
[i
+ 1]) != len
627 || strcmp (words
[i
], words
[i
+ 1]))
629 o
= variable_buffer_output (o
, words
[i
], len
);
630 o
= variable_buffer_output (o
, " ", 1);
634 /* Kill the last space. */
638 free ((char *) words
);
644 case function_foreach
:
646 /* Get three comma-separated arguments but
647 expand only the first two. */
649 register struct variable
*v
;
652 for (p
= text
; p
< end
; ++p
)
654 if (*p
== startparen
)
656 else if (*p
== endparen
)
658 else if (*p
== ',' && count
<= 0)
663 var
= expand_argument (text
, p
);
667 for (p
= p2
; p
< end
; ++p
)
669 if (*p
== startparen
)
671 else if (*p
== endparen
)
673 else if (*p
== ',' && count
<= 0)
678 list
= expand_argument (p2
, p
);
681 text
= savestring (p
, end
- p
);
683 push_new_variable_scope ();
684 v
= define_variable (var
, strlen (var
), "", o_automatic
, 0);
686 while ((p
= find_next_token (&p3
, &len
)) != 0)
692 result
= allocated_variable_expand (text
);
695 o
= variable_buffer_output (o
, result
, strlen (result
));
696 o
= variable_buffer_output (o
, " ", 1);
701 /* Kill the last space. */
704 pop_variable_scope ();
712 case function_filter
:
713 case function_filter_out
:
720 } *words
, *wordtail
, *wp
;
722 /* Get two comma-separated arguments and expand each one. */
724 for (p
= text
; p
< end
; ++p
)
726 if (*p
== startparen
)
728 else if (*p
== endparen
)
730 else if (*p
== ',' && count
<= 0)
734 BADARGS (function
== function_filter
? "filter" : "filter-out");
735 p2
= expand_argument (text
, p
);
737 text
= expand_argument (p
+ 1, end
);
739 /* Chop TEXT up into words and then run each pattern through. */
740 words
= wordtail
= 0;
742 while ((p
= find_next_token (&p3
, &len
)) != 0)
744 struct word
*w
= (struct word
*) alloca (sizeof (struct word
));
762 /* Run each pattern through the words, killing words. */
764 while ((p
= find_next_token (&p3
, &len
)) != 0)
770 percent
= find_percent (p
);
771 for (wp
= words
; wp
!= 0; wp
= wp
->next
)
772 wp
->matched
|= (percent
== 0 ? streq (p
, wp
->word
)
773 : pattern_matches (p
, percent
, wp
->word
));
778 /* Output the words that matched (or didn't, for filter-out). */
779 for (wp
= words
; wp
!= 0; wp
= wp
->next
)
780 if (function
== function_filter
? wp
->matched
: !wp
->matched
)
782 o
= variable_buffer_output (o
, wp
->word
, strlen (wp
->word
));
783 o
= variable_buffer_output (o
, " ", 1);
787 /* Kill the last space. */
796 case function_patsubst
:
797 /* Get three comma-separated arguments and expand each one. */
799 for (p
= text
; p
< end
; ++p
)
801 if (*p
== startparen
)
803 else if (*p
== endparen
)
805 else if (*p
== ',' && count
<= 0)
809 BADARGS ("patsubst");
813 for (++p
; p
< end
; ++p
)
815 if (*p
== startparen
)
817 else if (*p
== endparen
)
819 else if (*p
== ',' && count
<= 0)
823 BADARGS ("patsubst");
825 text
= expand_argument (text
, p2
);
826 p3
= expand_argument (p2
+ 1, p
);
827 p2
= expand_argument (p
+ 1, end
);
829 o
= patsubst_expand (o
, p2
, text
, p3
, (char *) 0, (char *) 0);
837 /* Get two comma-separated arguments and expand each one. */
839 for (p
= text
; p
< end
; ++p
)
841 if (*p
== startparen
)
843 else if (*p
== endparen
)
845 else if (*p
== ',' && count
<= 0)
850 text
= expand_argument (text
, p
);
852 p
= expand_argument (p
+ 1, end
);
855 /* Write each word of the first argument directly followed
856 by the corresponding word of the second argument.
857 If the two arguments have a different number of words,
858 the excess words are just output separated by blanks. */
859 register char *tp
, *pp
;
864 unsigned int tlen
, plen
;
866 tp
= find_next_token (&p2
, &tlen
);
868 o
= variable_buffer_output (o
, tp
, tlen
);
870 pp
= find_next_token (&p3
, &plen
);
872 o
= variable_buffer_output (o
, pp
, plen
);
874 if (tp
!= 0 || pp
!= 0)
876 o
= variable_buffer_output (o
, " ", 1);
880 while (tp
!= 0 || pp
!= 0);
882 /* Kill the last blank. */
891 /* Expand the argument. */
892 text
= expand_argument (text
, end
);
895 while ((p
= find_next_token (&p2
, &i
)) != 0)
897 o
= variable_buffer_output (o
, p
, i
);
898 o
= variable_buffer_output (o
, " ", 1);
902 /* Kill the last space. */
908 case function_wildcard
:
909 text
= expand_argument (text
, end
);
911 p
= string_glob (text
);
912 o
= variable_buffer_output (o
, p
, strlen (p
));
918 /* Get three comma-separated arguments and expand each one. */
920 for (p
= text
; p
< end
; ++p
)
922 if (*p
== startparen
)
924 else if (*p
== endparen
)
926 else if (*p
== ',' && count
<= 0)
934 for (++p
; p
< end
; ++p
)
936 if (*p
== startparen
)
938 else if (*p
== endparen
)
940 else if (*p
== ',' && count
<= 0)
946 text
= expand_argument (text
, p2
);
947 p3
= expand_argument (p2
+ 1, p
);
948 p2
= expand_argument (p
+ 1, end
);
950 o
= subst_expand (o
, p2
, text
, p3
, strlen (text
), strlen (p3
), 0, 0);
957 case function_firstword
:
958 /* Expand the argument. */
959 text
= expand_argument (text
, end
);
961 /* Find the first word in TEXT. */
963 p
= find_next_token (&p2
, &i
);
965 o
= variable_buffer_output (o
, p
, i
);
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)
984 text
= expand_argument (text
, p
);
986 p3
= expand_argument (p
+ 1, end
);
988 /* Check the first argument. */
989 for (p2
= text
; *p2
!= '\0'; ++p2
)
990 if (*p2
< '0' || *p2
> '9')
992 if (reading_filename
!= 0)
993 makefile_fatal (reading_filename
, *reading_lineno_ptr
,
994 "non-numeric first argument to `word' function");
996 fatal ("non-numeric first argument to `word' function");
999 i
= (unsigned int) atoi (text
);
1002 if (reading_filename
!= 0)
1003 makefile_fatal (reading_filename
, *reading_lineno_ptr
,
1004 "the `word' function takes a one-origin \
1007 fatal ("the `word' function takes a one-origin index argument");
1011 while ((p
= find_next_token (&p2
, &len
)) != 0)
1015 o
= variable_buffer_output (o
, p
, len
);
1021 case function_words
:
1022 /* Expand the argument. */
1023 text
= expand_argument (text
, end
);
1027 while (find_next_token (&p2
, (unsigned int *) 0) != 0)
1032 sprintf (buf
, "%d", i
);
1033 o
= variable_buffer_output (o
, buf
, strlen (buf
));
1039 case function_findstring
:
1040 /* Get two comma-separated arguments and expand each one. */
1042 for (p
= text
; p
< end
; ++p
)
1044 if (*p
== startparen
)
1046 else if (*p
== endparen
)
1048 else if (*p
== ',' && count
<= 0)
1052 BADARGS ("findstring");
1053 text
= expand_argument (text
, p
);
1055 p
= expand_argument (p
+ 1, end
);
1057 /* Find the first occurrence of the first string in the second. */
1059 if (sindex (p
, 0, text
, i
) != 0)
1060 o
= variable_buffer_output (o
, text
, i
);
1066 case function_addsuffix
:
1067 case function_addprefix
:
1068 /* Get two comma-separated arguments and expand each one. */
1070 for (p
= text
; p
< end
; ++p
)
1072 if (*p
== startparen
)
1074 else if (*p
== endparen
)
1076 else if (*p
== ',' && count
<= 0)
1080 BADARGS (function
== function_addsuffix
? "addsuffix" : "addprefix");
1081 text
= expand_argument (text
, p
);
1084 p2
= expand_argument (p
+ 1, end
);
1087 while ((p
= find_next_token (&p3
, &len
)) != 0)
1089 if (function
== function_addprefix
)
1090 o
= variable_buffer_output (o
, text
, i
);
1091 o
= variable_buffer_output (o
, p
, len
);
1092 if (function
== function_addsuffix
)
1093 o
= variable_buffer_output (o
, text
, i
);
1094 o
= variable_buffer_output (o
, " ", 1);
1098 /* Kill last space. */
1106 case function_basename
:
1107 /* Expand the argument. */
1108 text
= expand_argument (text
, end
);
1111 while ((p2
= find_next_token (&p3
, &len
)) != 0)
1114 while (p
>= p2
&& *p
!= (function
== function_dir
? '/' : '.'))
1118 if (function
== function_dir
)
1120 o
= variable_buffer_output (o
, p2
, p
- p2
);
1122 else if (function
== function_dir
)
1123 o
= variable_buffer_output (o
, "./", 2);
1125 /* The entire name is the basename. */
1126 o
= variable_buffer_output (o
, p2
, len
);
1128 o
= variable_buffer_output (o
, " ", 1);
1132 /* Kill last space. */
1138 case function_notdir
:
1139 case function_suffix
:
1140 /* Expand the argument. */
1141 text
= expand_argument (text
, end
);
1144 while ((p2
= find_next_token (&p3
, &len
)) != 0)
1147 while (p
>= p2
&& *p
!= (function
== function_notdir
? '/' : '.'))
1151 if (function
== function_notdir
)
1153 o
= variable_buffer_output (o
, p
, len
- (p
- p2
));
1155 else if (function
== function_notdir
)
1156 o
= variable_buffer_output (o
, p2
, len
);
1158 if (function
== function_notdir
|| p
>= p2
)
1160 o
= variable_buffer_output (o
, " ", 1);
1165 /* Kill last space. */
1175 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1176 opening ( or { and is not null-terminated. If a function invocation
1177 is found, expand it into the buffer at *OP, updating *OP, incrementing
1178 *STRINGP past the reference and returning nonzero. If not, return zero. */
1181 handle_function (op
, stringp
)
1186 register unsigned int code
;
1187 unsigned int maxlen
;
1188 char *beg
= *stringp
+ 1;
1191 endref
= lindex (beg
, beg
+ MAXFUNCTIONLEN
, '\0');
1192 maxlen
= endref
!= 0 ? endref
- beg
: MAXFUNCTIONLEN
;
1194 for (code
= 0; function_table
[code
].name
!= 0; ++code
)
1196 if (maxlen
< function_table
[code
].len
)
1198 endref
= beg
+ function_table
[code
].len
;
1199 if (isblank (*endref
)
1200 && !strncmp (function_table
[code
].name
, beg
,
1201 function_table
[code
].len
))
1204 if (function_table
[code
].name
!= 0)
1206 /* We have found a call to an expansion-time function.
1207 Find the end of the arguments, and do the function. */
1209 char openparen
= beg
[-1], closeparen
= openparen
== '(' ? ')' : '}';
1214 /* Space after function name isn't part of the args. */
1215 p
= next_token (endref
);
1218 /* Count nested use of whichever kind of parens we use,
1219 so that nested calls and variable refs work. */
1221 for (; *p
!= '\0'; ++p
)
1223 if (*p
== openparen
)
1225 else if (*p
== closeparen
&& --count
< 0)
1231 static const char errmsg
[]
1232 = "unterminated call to function `%s': missing `%c'";
1233 if (reading_filename
== 0)
1234 fatal (errmsg
, function_table
[code
].name
, closeparen
);
1236 makefile_fatal (reading_filename
, *reading_lineno_ptr
, errmsg
,
1237 function_table
[code
].name
, closeparen
);
1240 /* We found the end; expand the function call. */
1242 *op
= expand_function (*op
, function_table
[code
].function
, argbeg
, p
);
1250 /* Glob-expand LINE. The returned pointer is
1251 only good until the next call to string_glob. */
1257 static char *result
= 0;
1258 static unsigned int length
;
1259 register struct nameseq
*chain
;
1260 register unsigned int idx
;
1262 chain
= multi_glob (parse_file_seq
1263 (&line
, '\0', sizeof (struct nameseq
),
1264 /* We do not want parse_file_seq to strip `./'s.
1265 That would break examples like:
1266 $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)). */
1268 sizeof (struct nameseq
));
1273 result
= (char *) xmalloc (100);
1279 register char *name
= chain
->name
;
1280 unsigned int len
= strlen (name
);
1282 struct nameseq
*next
= chain
->next
;
1283 free ((char *) chain
);
1286 /* multi_glob will pass names without globbing metacharacters
1287 through as is, but we want only files that actually exist. */
1288 if (file_exists_p (name
))
1290 if (idx
+ len
+ 1 > length
)
1292 length
+= (len
+ 1) * 2;
1293 result
= (char *) xrealloc (result
, length
);
1295 bcopy (name
, &result
[idx
], len
);
1297 result
[idx
++] = ' ';
1303 /* Kill the last space and terminate the string. */
1307 result
[idx
- 1] = '\0';