1 /* Builtin function expansion for GNU Make.
2 Copyright (C) 1988, 1989, 1991-1997, 1999, 2002 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, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
33 struct function_table_entry
37 unsigned char minimum_args
;
38 unsigned char maximum_args
;
40 char *(*func_ptr
) PARAMS ((char *output
, char **argv
, const char *fname
));
44 function_table_entry_hash_1 (const void *keyv
)
46 struct function_table_entry
const *key
= (struct function_table_entry
const *) keyv
;
47 return_STRING_N_HASH_1 (key
->name
, key
->len
);
51 function_table_entry_hash_2 (const void *keyv
)
53 struct function_table_entry
const *key
= (struct function_table_entry
const *) keyv
;
54 return_STRING_N_HASH_2 (key
->name
, key
->len
);
58 function_table_entry_hash_cmp (const void *xv
, const void *yv
)
60 struct function_table_entry
const *x
= (struct function_table_entry
const *) xv
;
61 struct function_table_entry
const *y
= (struct function_table_entry
const *) yv
;
62 int result
= x
->len
- y
->len
;
65 return_STRING_N_COMPARE (x
->name
, y
->name
, x
->len
);
68 static struct hash_table function_table
;
71 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
72 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
73 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
74 nonzero, substitutions are done only on matches which are complete
75 whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
76 done only at the ends of whitespace-delimited words. */
79 subst_expand (char *o
, char *text
, char *subst
, char *replace
,
80 unsigned int slen
, unsigned int rlen
,
81 int by_word
, int suffix_only
)
84 unsigned int tlen
= strlen (text
);
87 if (slen
== 0 && !by_word
&& !suffix_only
)
89 /* The first occurrence of "" in any string is its end. */
90 o
= variable_buffer_output (o
, t
, tlen
);
92 o
= variable_buffer_output (o
, replace
, rlen
);
98 if ((by_word
| suffix_only
) && slen
== 0)
99 /* When matching by words, the empty string should match
100 the end of each word, rather than the end of the whole text. */
101 p
= end_of_token (next_token (t
));
104 p
= sindex (t
, tlen
, subst
, slen
);
107 /* No more matches. Output everything left on the end. */
108 o
= variable_buffer_output (o
, t
, tlen
);
113 /* Output everything before this occurrence of the string to replace. */
115 o
= variable_buffer_output (o
, t
, p
- t
);
117 /* If we're substituting only by fully matched words,
118 or only at the ends of words, check that this case qualifies. */
120 && ((p
> t
&& !isblank ((unsigned char)p
[-1]))
121 || (p
[slen
] != '\0' && !isblank ((unsigned char)p
[slen
]))))
123 && (p
[slen
] != '\0' && !isblank ((unsigned char)p
[slen
]))))
124 /* Struck out. Output the rest of the string that is
125 no longer to be replaced. */
126 o
= variable_buffer_output (o
, subst
, slen
);
128 /* Output the replacement string. */
129 o
= variable_buffer_output (o
, replace
, rlen
);
131 /* Advance T past the string to be replaced; adjust tlen. */
137 } while (*t
!= '\0');
143 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
144 and replacing strings matching PATTERN with REPLACE.
145 If PATTERN_PERCENT is not nil, PATTERN has already been
146 run through find_percent, and PATTERN_PERCENT is the result.
147 If REPLACE_PERCENT is not nil, REPLACE has already been
148 run through find_percent, and REPLACE_PERCENT is the result. */
151 patsubst_expand (char *o
, char *text
, char *pattern
, char *replace
,
152 char *pattern_percent
, char *replace_percent
)
154 unsigned int pattern_prepercent_len
, pattern_postpercent_len
;
155 unsigned int replace_prepercent_len
, replace_postpercent_len
= 0;
160 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
161 will be collapsed before we call subst_expand if PATTERN has no %. */
162 if (replace_percent
== 0)
163 replace_percent
= find_percent (replace
);
164 if (replace_percent
!= 0)
166 /* Record the length of REPLACE before and after the % so
167 we don't have to compute these lengths more than once. */
168 replace_prepercent_len
= replace_percent
- replace
;
169 replace_postpercent_len
= strlen (replace_percent
+ 1);
172 /* We store the length of the replacement
173 so we only need to compute it once. */
174 replace_prepercent_len
= strlen (replace
);
176 if (pattern_percent
== 0)
177 pattern_percent
= find_percent (pattern
);
178 if (pattern_percent
== 0)
179 /* With no % in the pattern, this is just a simple substitution. */
180 return subst_expand (o
, text
, pattern
, replace
,
181 strlen (pattern
), strlen (replace
), 1, 0);
183 /* Record the length of PATTERN before and after the %
184 so we don't have to compute it more than once. */
185 pattern_prepercent_len
= pattern_percent
- pattern
;
186 pattern_postpercent_len
= strlen (pattern_percent
+ 1);
188 while ((t
= find_next_token (&text
, &len
)) != 0)
192 /* Is it big enough to match? */
193 if (len
< pattern_prepercent_len
+ pattern_postpercent_len
)
196 /* Does the prefix match? */
197 if (!fail
&& pattern_prepercent_len
> 0
199 || t
[pattern_prepercent_len
- 1] != pattern_percent
[-1]
200 || !strneq (t
+ 1, pattern
+ 1, pattern_prepercent_len
- 1)))
203 /* Does the suffix match? */
204 if (!fail
&& pattern_postpercent_len
> 0
205 && (t
[len
- 1] != pattern_percent
[pattern_postpercent_len
]
206 || t
[len
- pattern_postpercent_len
] != pattern_percent
[1]
207 || !strneq (&t
[len
- pattern_postpercent_len
],
208 &pattern_percent
[1], pattern_postpercent_len
- 1)))
212 /* It didn't match. Output the string. */
213 o
= variable_buffer_output (o
, t
, len
);
216 /* It matched. Output the replacement. */
218 /* Output the part of the replacement before the %. */
219 o
= variable_buffer_output (o
, replace
, replace_prepercent_len
);
221 if (replace_percent
!= 0)
223 /* Output the part of the matched string that
224 matched the % in the pattern. */
225 o
= variable_buffer_output (o
, t
+ pattern_prepercent_len
,
226 len
- (pattern_prepercent_len
227 + pattern_postpercent_len
));
228 /* Output the part of the replacement after the %. */
229 o
= variable_buffer_output (o
, replace_percent
+ 1,
230 replace_postpercent_len
);
234 /* Output a space, but not if the replacement is "". */
235 if (fail
|| replace_prepercent_len
> 0
236 || (replace_percent
!= 0 && len
+ replace_postpercent_len
> 0))
238 o
= variable_buffer_output (o
, " ", 1);
243 /* Kill the last space. */
250 /* Look up a function by name. */
252 static const struct function_table_entry
*
253 lookup_function (const char *s
)
257 while (*e
&& ( (*e
>= 'a' && *e
<= 'z') || *e
== '-'))
259 if (*e
== '\0' || isblank ((unsigned char) *e
))
261 struct function_table_entry function_table_entry_key
;
262 function_table_entry_key
.name
= s
;
263 function_table_entry_key
.len
= e
- s
;
265 return hash_find_item (&function_table
, &function_table_entry_key
);
271 /* Return 1 if PATTERN matches STR, 0 if not. */
274 pattern_matches (char *pattern
, char *percent
, char *str
)
276 unsigned int sfxlen
, strlength
;
280 unsigned int len
= strlen (pattern
) + 1;
281 char *new_chars
= (char *) alloca (len
);
282 bcopy (pattern
, new_chars
, len
);
284 percent
= find_percent (pattern
);
286 return streq (pattern
, str
);
289 sfxlen
= strlen (percent
+ 1);
290 strlength
= strlen (str
);
292 if (strlength
< (percent
- pattern
) + sfxlen
293 || !strneq (pattern
, str
, percent
- pattern
))
296 return !strcmp (percent
+ 1, str
+ (strlength
- sfxlen
));
300 /* Find the next comma or ENDPAREN (counting nested STARTPAREN and
301 ENDPARENtheses), starting at PTR before END. Return a pointer to
304 If no next argument is found, return NULL.
308 find_next_argument (char startparen
, char endparen
,
309 const char *ptr
, const char *end
)
313 for (; ptr
< end
; ++ptr
)
314 if (*ptr
== startparen
)
317 else if (*ptr
== endparen
)
324 else if (*ptr
== ',' && !count
)
327 /* We didn't find anything. */
332 /* Glob-expand LINE. The returned pointer is
333 only good until the next call to string_glob. */
336 string_glob (char *line
)
338 static char *result
= 0;
339 static unsigned int length
;
340 register struct nameseq
*chain
;
341 register unsigned int idx
;
343 chain
= multi_glob (parse_file_seq
344 (&line
, '\0', sizeof (struct nameseq
),
345 /* We do not want parse_file_seq to strip `./'s.
346 That would break examples like:
347 $(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
349 sizeof (struct nameseq
));
354 result
= (char *) xmalloc (100);
360 register char *name
= chain
->name
;
361 unsigned int len
= strlen (name
);
363 struct nameseq
*next
= chain
->next
;
364 free ((char *) chain
);
367 /* multi_glob will pass names without globbing metacharacters
368 through as is, but we want only files that actually exist. */
369 if (file_exists_p (name
))
371 if (idx
+ len
+ 1 > length
)
373 length
+= (len
+ 1) * 2;
374 result
= (char *) xrealloc (result
, length
);
376 bcopy (name
, &result
[idx
], len
);
384 /* Kill the last space and terminate the string. */
388 result
[idx
- 1] = '\0';
398 func_patsubst (char *o
, char **argv
, const char *funcname UNUSED
)
400 o
= patsubst_expand (o
, argv
[2], argv
[0], argv
[1], (char *) 0, (char *) 0);
406 func_join (char *o
, char **argv
, const char *funcname UNUSED
)
410 /* Write each word of the first argument directly followed
411 by the corresponding word of the second argument.
412 If the two arguments have a different number of words,
413 the excess words are just output separated by blanks. */
416 char *list1_iterator
= argv
[0];
417 char *list2_iterator
= argv
[1];
420 unsigned int len1
, len2
;
422 tp
= find_next_token (&list1_iterator
, &len1
);
424 o
= variable_buffer_output (o
, tp
, len1
);
426 pp
= find_next_token (&list2_iterator
, &len2
);
428 o
= variable_buffer_output (o
, pp
, len2
);
430 if (tp
!= 0 || pp
!= 0)
432 o
= variable_buffer_output (o
, " ", 1);
436 while (tp
!= 0 || pp
!= 0);
438 /* Kill the last blank. */
446 func_origin (char *o
, char **argv
, const char *funcname UNUSED
)
448 /* Expand the argument. */
449 register struct variable
*v
= lookup_variable (argv
[0], strlen (argv
[0]));
451 o
= variable_buffer_output (o
, "undefined", 9);
460 o
= variable_buffer_output (o
, "default", 7);
463 o
= variable_buffer_output (o
, "environment", 11);
466 o
= variable_buffer_output (o
, "file", 4);
469 o
= variable_buffer_output (o
, "environment override", 20);
472 o
= variable_buffer_output (o
, "command line", 12);
475 o
= variable_buffer_output (o
, "override", 8);
478 o
= variable_buffer_output (o
, "automatic", 9);
486 # define IS_PATHSEP(c) ((c) == ']')
488 # ifdef HAVE_DOS_PATHS
489 # define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
491 # define IS_PATHSEP(c) ((c) == '/')
497 func_notdir_suffix (char *o
, char **argv
, const char *funcname
)
499 /* Expand the argument. */
500 char *list_iterator
= argv
[0];
505 int is_suffix
= streq (funcname
, "suffix");
506 int is_notdir
= !is_suffix
;
507 while ((p2
= find_next_token (&list_iterator
, &len
)) != 0)
512 while (p
>= p2
&& (!is_suffix
|| *p
!= '.'))
525 o
= variable_buffer_output (o
, p
, len
- (p
- p2
));
527 #ifdef HAVE_DOS_PATHS
528 /* Handle the case of "d:foo/bar". */
529 else if (streq (funcname
, "notdir") && p2
[0] && p2
[1] == ':')
532 o
= variable_buffer_output (o
, p
, len
- (p
- p2
));
536 o
= variable_buffer_output (o
, p2
, len
);
538 if (is_notdir
|| p
>= p2
)
540 o
= variable_buffer_output (o
, " ", 1);
545 /* Kill last space. */
555 func_basename_dir (char *o
, char **argv
, const char *funcname
)
557 /* Expand the argument. */
563 int is_basename
= streq (funcname
, "basename");
564 int is_dir
= !is_basename
;
566 while ((p2
= find_next_token (&p3
, &len
)) != 0)
569 while (p
>= p2
&& (!is_basename
|| *p
!= '.'))
576 if (p
>= p2
&& (is_dir
))
577 o
= variable_buffer_output (o
, p2
, ++p
- p2
);
578 else if (p
>= p2
&& (*p
== '.'))
579 o
= variable_buffer_output (o
, p2
, p
- p2
);
580 #ifdef HAVE_DOS_PATHS
581 /* Handle the "d:foobar" case */
582 else if (p2
[0] && p2
[1] == ':' && is_dir
)
583 o
= variable_buffer_output (o
, p2
, 2);
587 o
= variable_buffer_output (o
, "[]", 2);
590 o
= variable_buffer_output (o
, "./", 2);
592 ; /* Just a nop... */
596 /* The entire name is the basename. */
597 o
= variable_buffer_output (o
, p2
, len
);
599 o
= variable_buffer_output (o
, " ", 1);
603 /* Kill last space. */
611 func_addsuffix_addprefix (char *o
, char **argv
, const char *funcname
)
613 int fixlen
= strlen (argv
[0]);
614 char *list_iterator
= argv
[1];
615 int is_addprefix
= streq (funcname
, "addprefix");
616 int is_addsuffix
= !is_addprefix
;
622 while ((p
= find_next_token (&list_iterator
, &len
)) != 0)
625 o
= variable_buffer_output (o
, argv
[0], fixlen
);
626 o
= variable_buffer_output (o
, p
, len
);
628 o
= variable_buffer_output (o
, argv
[0], fixlen
);
629 o
= variable_buffer_output (o
, " ", 1);
634 /* Kill last space. */
641 func_subst (char *o
, char **argv
, const char *funcname UNUSED
)
643 o
= subst_expand (o
, argv
[2], argv
[0], argv
[1], strlen (argv
[0]),
644 strlen (argv
[1]), 0, 0);
651 func_firstword (char *o
, char **argv
, const char *funcname UNUSED
)
654 char *words
= argv
[0]; /* Use a temp variable for find_next_token */
655 char *p
= find_next_token (&words
, &i
);
658 o
= variable_buffer_output (o
, p
, i
);
665 func_words (char *o
, char **argv
, const char *funcname UNUSED
)
668 char *word_iterator
= argv
[0];
671 while (find_next_token (&word_iterator
, (unsigned int *) 0) != 0)
674 sprintf (buf
, "%d", i
);
675 o
= variable_buffer_output (o
, buf
, strlen (buf
));
681 /* Set begpp to point to the first non-whitespace character of the string,
682 * and endpp to point to the last non-whitespace character of the string.
683 * If the string is empty or contains nothing but whitespace, endpp will be
687 strip_whitespace (const char **begpp
, const char **endpp
)
689 while (*begpp
<= *endpp
&& isspace ((unsigned char)**begpp
))
691 while (*endpp
>= *begpp
&& isspace ((unsigned char)**endpp
))
693 return (char *)*begpp
;
697 check_numeric (const char *s
, const char *message
)
699 const char *end
= s
+ strlen (s
) - 1;
701 strip_whitespace (&s
, &end
);
703 for (; s
<= end
; ++s
)
704 if (!ISDIGIT (*s
)) /* ISDIGIT only evals its arg once: see make.h. */
707 if (s
<= end
|| end
- beg
< 0)
708 fatal (reading_file
, "%s: '%s'", message
, beg
);
714 func_word (char *o
, char **argv
, const char *funcname UNUSED
)
720 /* Check the first argument. */
721 check_numeric (argv
[0], _("non-numeric first argument to `word' function"));
725 fatal (reading_file
, _("first argument to `word' function must be greater than 0"));
729 while ((p
= find_next_token (&end_p
, 0)) != 0)
734 o
= variable_buffer_output (o
, p
, end_p
- p
);
740 func_wordlist (char *o
, char **argv
, const char *funcname UNUSED
)
744 /* Check the arguments. */
745 check_numeric (argv
[0],
746 _("non-numeric first argument to `wordlist' function"));
747 check_numeric (argv
[1],
748 _("non-numeric second argument to `wordlist' function"));
750 start
= atoi (argv
[0]);
751 count
= atoi (argv
[1]) - start
+ 1;
756 char *end_p
= argv
[2];
758 /* Find the beginning of the "start"th word. */
759 while (((p
= find_next_token (&end_p
, 0)) != 0) && --start
)
764 /* Find the end of the "count"th word from start. */
765 while (--count
&& (find_next_token (&end_p
, 0) != 0))
768 /* Return the stuff in the middle. */
769 o
= variable_buffer_output (o
, p
, end_p
- p
);
777 func_findstring (char *o
, char **argv
, const char *funcname UNUSED
)
779 /* Find the first occurrence of the first string in the second. */
780 int i
= strlen (argv
[0]);
781 if (sindex (argv
[1], 0, argv
[0], i
) != 0)
782 o
= variable_buffer_output (o
, argv
[0], i
);
788 func_foreach (char *o
, char **argv
, const char *funcname UNUSED
)
790 /* expand only the first two. */
791 char *varname
= expand_argument (argv
[0], NULL
);
792 char *list
= expand_argument (argv
[1], NULL
);
793 char *body
= argv
[2];
796 char *list_iterator
= list
;
799 register struct variable
*var
;
801 push_new_variable_scope ();
802 var
= define_variable (varname
, strlen (varname
), "", o_automatic
, 0);
804 /* loop through LIST, put the value in VAR and expand BODY */
805 while ((p
= find_next_token (&list_iterator
, &len
)) != 0)
814 var
->value
= (char *) xstrdup ((char*) p
);
818 result
= allocated_variable_expand (body
);
820 o
= variable_buffer_output (o
, result
, strlen (result
));
821 o
= variable_buffer_output (o
, " ", 1);
827 /* Kill the last space. */
830 pop_variable_scope ();
840 struct a_word
*chain
;
847 a_word_hash_1 (const void *key
)
849 return_STRING_HASH_1 (((struct a_word
const *) key
)->str
);
853 a_word_hash_2 (const void *key
)
855 return_STRING_HASH_2 (((struct a_word
const *) key
)->str
);
859 a_word_hash_cmp (const void *x
, const void *y
)
861 int result
= ((struct a_word
const *) x
)->length
- ((struct a_word
const *) y
)->length
;
864 return_STRING_COMPARE (((struct a_word
const *) x
)->str
,
865 ((struct a_word
const *) y
)->str
);
870 struct a_pattern
*next
;
878 func_filter_filterout (char *o
, char **argv
, const char *funcname
)
880 struct a_word
*wordhead
;
881 struct a_word
**wordtail
;
883 struct a_pattern
*pathead
;
884 struct a_pattern
**pattail
;
885 struct a_pattern
*pp
;
887 struct hash_table a_word_table
;
888 int is_filter
= streq (funcname
, "filter");
889 char *pat_iterator
= argv
[0];
890 char *word_iterator
= argv
[1];
897 /* Chop ARGV[0] up into patterns to match against the words. */
900 while ((p
= find_next_token (&pat_iterator
, &len
)) != 0)
902 struct a_pattern
*pat
= (struct a_pattern
*) alloca (sizeof (struct a_pattern
));
905 pattail
= &pat
->next
;
907 if (*pat_iterator
!= '\0')
912 pat
->save_c
= p
[len
];
914 pat
->percent
= find_percent (p
);
915 if (pat
->percent
== 0)
920 /* Chop ARGV[1] up into words to match against the patterns. */
922 wordtail
= &wordhead
;
923 while ((p
= find_next_token (&word_iterator
, &len
)) != 0)
925 struct a_word
*word
= (struct a_word
*) alloca (sizeof (struct a_word
));
928 wordtail
= &word
->next
;
930 if (*word_iterator
!= '\0')
942 /* Only use a hash table if arg list lengths justifies the cost. */
943 hashing
= (literals
>= 2 && (literals
* words
) >= 10);
946 hash_init (&a_word_table
, words
, a_word_hash_1
, a_word_hash_2
, a_word_hash_cmp
);
947 for (wp
= wordhead
; wp
!= 0; wp
= wp
->next
)
949 struct a_word
*owp
= hash_insert (&a_word_table
, wp
);
959 /* Run each pattern through the words, killing words. */
960 for (pp
= pathead
; pp
!= 0; pp
= pp
->next
)
963 for (wp
= wordhead
; wp
!= 0; wp
= wp
->next
)
964 wp
->matched
|= pattern_matches (pp
->str
, pp
->percent
, wp
->str
);
967 struct a_word a_word_key
;
968 a_word_key
.str
= pp
->str
;
969 a_word_key
.length
= pp
->length
;
970 wp
= (struct a_word
*) hash_find_item (&a_word_table
, &a_word_key
);
978 for (wp
= wordhead
; wp
!= 0; wp
= wp
->next
)
979 wp
->matched
|= (wp
->length
== pp
->length
980 && strneq (pp
->str
, wp
->str
, wp
->length
));
983 /* Output the words that matched (or didn't, for filter-out). */
984 for (wp
= wordhead
; wp
!= 0; wp
= wp
->next
)
985 if (is_filter
? wp
->matched
: !wp
->matched
)
987 o
= variable_buffer_output (o
, wp
->str
, strlen (wp
->str
));
988 o
= variable_buffer_output (o
, " ", 1);
993 /* Kill the last space. */
997 for (pp
= pathead
; pp
!= 0; pp
= pp
->next
)
998 pp
->str
[pp
->length
] = pp
->save_c
;
1001 hash_free (&a_word_table
, 0);
1008 func_strip (char *o
, char **argv
, const char *funcname UNUSED
)
1018 while (isspace ((unsigned char)*p
))
1021 for (i
=0; *p
!= '\0' && !isspace ((unsigned char)*p
); ++p
, ++i
)
1025 o
= variable_buffer_output (o
, word_start
, i
);
1026 o
= variable_buffer_output (o
, " ", 1);
1031 /* Kill the last space. */
1037 Print a warning or fatal message.
1040 func_error (char *o
, char **argv
, const char *funcname
)
1046 /* The arguments will be broken on commas. Rather than create yet
1047 another special case where function arguments aren't broken up,
1048 just create a format string that puts them back together. */
1049 for (len
=0, argvp
=argv
; *argvp
!= 0; ++argvp
)
1050 len
+= strlen (*argvp
) + 2;
1052 p
= msg
= (char *) alloca (len
+ 1);
1054 for (argvp
=argv
; argvp
[1] != 0; ++argvp
)
1057 p
+= strlen (*argvp
);
1063 if (*funcname
== 'e')
1064 fatal (reading_file
, "%s", msg
);
1066 /* The warning function expands to the empty string. */
1067 error (reading_file
, "%s", msg
);
1074 chop argv[0] into words, and sort them.
1077 func_sort (char *o
, char **argv
, const char *funcname UNUSED
)
1081 register int wordi
= 0;
1083 /* Chop ARGV[0] into words and put them in WORDS. */
1089 while ((p
= find_next_token (&t
, &len
)) != 0)
1091 if (wordi
>= nwords
- 1)
1093 nwords
= (2 * nwords
) + 5;
1094 words
= (char **) xrealloc ((char *) words
,
1095 nwords
* sizeof (char *));
1097 words
[wordi
++] = savestring (p
, len
);
1103 /* Now sort the list of words. */
1104 qsort ((char *) words
, wordi
, sizeof (char *), alpha_compare
);
1106 /* Now write the sorted list. */
1107 for (i
= 0; i
< wordi
; ++i
)
1109 len
= strlen (words
[i
]);
1110 if (i
== wordi
- 1 || strlen (words
[i
+ 1]) != len
1111 || strcmp (words
[i
], words
[i
+ 1]))
1113 o
= variable_buffer_output (o
, words
[i
], len
);
1114 o
= variable_buffer_output (o
, " ", 1);
1118 /* Kill the last space. */
1127 $(if condition,true-part[,false-part])
1129 CONDITION is false iff it evaluates to an empty string. White
1130 space before and after condition are stripped before evaluation.
1132 If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is
1133 evaluated (if it exists). Because only one of the two PARTs is evaluated,
1134 you can use $(if ...) to create side-effects (with $(shell ...), for
1139 func_if (char *o
, char **argv
, const char *funcname UNUSED
)
1141 const char *begp
= argv
[0];
1142 const char *endp
= begp
+ strlen (argv
[0]) - 1;
1145 /* Find the result of the condition: if we have a value, and it's not
1146 empty, the condition is true. If we don't have a value, or it's the
1147 empty string, then it's false. */
1149 strip_whitespace (&begp
, &endp
);
1153 char *expansion
= expand_argument (begp
, endp
+1);
1155 result
= strlen (expansion
);
1159 /* If the result is true (1) we want to eval the first argument, and if
1160 it's false (0) we want to eval the second. If the argument doesn't
1161 exist we do nothing, otherwise expand it and add to the buffer. */
1163 argv
+= 1 + !result
;
1169 expansion
= expand_argument (argv
[0], NULL
);
1171 o
= variable_buffer_output (o
, expansion
, strlen (expansion
));
1180 func_wildcard (char *o
, char **argv
, const char *funcname UNUSED
)
1184 o
= wildcard_expansion (argv
[0], o
);
1186 char *p
= string_glob (argv
[0]);
1187 o
= variable_buffer_output (o
, p
, strlen (p
));
1193 $(eval <makefile string>)
1195 Always resolves to the empty string.
1197 Treat the arguments as a segment of makefile, and parse them.
1201 func_eval (char *o
, char **argv
, const char *funcname UNUSED
)
1206 /* Eval the buffer. Pop the current variable buffer setting so that the
1207 eval'd code can use its own without conflicting. */
1209 install_variable_buffer (&buf
, &len
);
1211 eval_buffer (argv
[0]);
1213 restore_variable_buffer (buf
, len
);
1220 func_value (char *o
, char **argv
, const char *funcname UNUSED
)
1222 /* Look up the variable. */
1223 struct variable
*v
= lookup_variable (argv
[0], strlen (argv
[0]));
1225 /* Copy its value into the output buffer without expanding it. */
1227 o
= variable_buffer_output (o
, v
->value
, strlen(v
->value
));
1233 \r is replaced on UNIX as well. Is this desirable?
1236 fold_newlines (char *buffer
, int *length
)
1240 char *last_nonnl
= buffer
-1;
1242 for (; *src
!= '\0'; ++src
)
1244 if (src
[0] == '\r' && src
[1] == '\n')
1256 *(++last_nonnl
) = '\0';
1257 *length
= last_nonnl
- buffer
;
1262 int shell_function_pid
= 0, shell_function_completed
;
1268 #include <windows.h>
1270 #include "sub_proc.h"
1274 windows32_openpipe (int *pipedes
, int *pid_p
, char **command_argv
, char **envp
)
1276 SECURITY_ATTRIBUTES saAttr
;
1284 saAttr
.nLength
= sizeof (SECURITY_ATTRIBUTES
);
1285 saAttr
.bInheritHandle
= TRUE
;
1286 saAttr
.lpSecurityDescriptor
= NULL
;
1288 if (DuplicateHandle (GetCurrentProcess(),
1289 GetStdHandle(STD_INPUT_HANDLE
),
1290 GetCurrentProcess(),
1294 DUPLICATE_SAME_ACCESS
) == FALSE
) {
1295 fatal (NILF
, _("create_child_process: DuplicateHandle(In) failed (e=%d)\n"),
1299 if (DuplicateHandle(GetCurrentProcess(),
1300 GetStdHandle(STD_ERROR_HANDLE
),
1301 GetCurrentProcess(),
1305 DUPLICATE_SAME_ACCESS
) == FALSE
) {
1306 fatal (NILF
, _("create_child_process: DuplicateHandle(Err) failed (e=%d)\n"),
1310 if (!CreatePipe(&hChildOutRd
, &hChildOutWr
, &saAttr
, 0))
1311 fatal (NILF
, _("CreatePipe() failed (e=%d)\n"), GetLastError());
1313 hProcess
= process_init_fd(hIn
, hChildOutWr
, hErr
);
1316 fatal (NILF
, _("windows32_openpipe (): process_init_fd() failed\n"));
1318 /* make sure that CreateProcess() has Path it needs */
1319 sync_Path_environment();
1321 if (!process_begin(hProcess
, command_argv
, envp
, command_argv
[0], NULL
)) {
1322 /* register process for wait */
1323 process_register(hProcess
);
1325 /* set the pid for returning to caller */
1326 *pid_p
= (int) hProcess
;
1328 /* set up to read data from child */
1329 pipedes
[0] = _open_osfhandle((long) hChildOutRd
, O_RDONLY
);
1331 /* this will be closed almost right away */
1332 pipedes
[1] = _open_osfhandle((long) hChildOutWr
, O_APPEND
);
1334 /* reap/cleanup the failed process */
1335 process_cleanup(hProcess
);
1337 /* close handles which were duplicated, they weren't used */
1341 /* close pipe handles, they won't be used */
1342 CloseHandle(hChildOutRd
);
1343 CloseHandle(hChildOutWr
);
1345 /* set status for return */
1346 pipedes
[0] = pipedes
[1] = -1;
1355 msdos_openpipe (int* pipedes
, int *pidp
, char *text
)
1358 /* MSDOS can't fork, but it has `popen'. */
1359 struct variable
*sh
= lookup_variable ("SHELL", 5);
1361 extern int dos_command_running
, dos_status
;
1363 /* Make sure not to bother processing an empty line. */
1364 while (isblank ((unsigned char)*text
))
1371 char buf
[PATH_MAX
+ 7];
1372 /* This makes sure $SHELL value is used by $(shell), even
1373 though the target environment is not passed to it. */
1374 sprintf (buf
, "SHELL=%s", sh
->value
);
1380 dos_command_running
= 1;
1382 /* If dos_status becomes non-zero, it means the child process
1383 was interrupted by a signal, like SIGINT or SIGQUIT. See
1384 fatal_error_signal in commands.c. */
1385 fpipe
= popen (text
, "rt");
1386 dos_command_running
= 0;
1387 if (!fpipe
|| dos_status
)
1393 else if (errno
== 0)
1395 shell_function_completed
= -1;
1399 pipedes
[0] = fileno (fpipe
);
1400 *pidp
= 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
1402 shell_function_completed
= 1;
1409 Do shell spawning, with the naughty bits for different OSes.
1414 /* VMS can't do $(shell ...) */
1415 #define func_shell 0
1420 func_shell (char *o
, char **argv
, const char *funcname UNUSED
)
1422 char* batch_filename
= NULL
;
1428 char **command_argv
;
1435 /* Construct the argument list. */
1436 command_argv
= construct_command_argv (argv
[0],
1437 (char **) NULL
, (struct file
*) 0,
1439 if (command_argv
== 0)
1443 /* Using a target environment for `shell' loses in cases like:
1444 export var = $(shell echo foobie)
1445 because target_environment hits a loop trying to expand $(var)
1446 to put it in the environment. This is even more confusing when
1447 var was not explicitly exported, but just appeared in the
1448 calling environment. */
1452 /* For error messages. */
1453 if (reading_file
!= 0)
1455 error_prefix
= (char *) alloca (strlen (reading_file
->filenm
)+11+4);
1456 sprintf (error_prefix
,
1457 "%s:%lu: ", reading_file
->filenm
, reading_file
->lineno
);
1464 windows32_openpipe (pipedes
, &pid
, command_argv
, envp
);
1466 if (pipedes
[0] < 0) {
1467 /* open of the pipe failed, mark as failed execution */
1468 shell_function_completed
= -1;
1473 #elif defined(__MSDOS__)
1475 fpipe
= msdos_openpipe (pipedes
, &pid
, argv
[0]);
1478 perror_with_name (error_prefix
, "pipe");
1484 if (pipe (pipedes
) < 0)
1486 perror_with_name (error_prefix
, "pipe");
1492 /* close some handles that are unnecessary for the child process */
1493 CLOSE_ON_EXEC(pipedes
[1]);
1494 CLOSE_ON_EXEC(pipedes
[0]);
1495 /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
1496 pid
= child_execute_job (0, pipedes
[1], command_argv
, envp
);
1498 perror_with_name (error_prefix
, "spawn");
1500 # else /* ! __EMX__ */
1504 perror_with_name (error_prefix
, "fork");
1506 child_execute_job (0, pipedes
[1], command_argv
, envp
);
1513 /* We are the parent. */
1516 unsigned int maxlen
;
1519 /* Record the PID for reap_children. */
1520 shell_function_pid
= pid
;
1522 shell_function_completed
= 0;
1524 /* Free the storage only the child needed. */
1525 free (command_argv
[0]);
1526 free ((char *) command_argv
);
1528 /* Close the write side of the pipe. */
1529 (void) close (pipedes
[1]);
1532 /* Set up and read from the pipe. */
1535 buffer
= (char *) xmalloc (maxlen
+ 1);
1537 /* Read from the pipe until it gets EOF. */
1538 for (i
= 0; ; i
+= cc
)
1543 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
1546 EINTRLOOP (cc
, read (pipedes
[0], &buffer
[i
], maxlen
- i
));
1552 /* Close the read side of the pipe. */
1555 (void) pclose (fpipe
);
1557 (void) close (pipedes
[0]);
1560 /* Loop until child_handler or reap_children() sets
1561 shell_function_completed to the status of our child shell. */
1562 while (shell_function_completed
== 0)
1563 reap_children (1, 0);
1565 if (batch_filename
) {
1566 DB (DB_VERBOSE
, (_("Cleaning up temporary batch file %s\n"),
1568 remove (batch_filename
);
1569 free (batch_filename
);
1571 shell_function_pid
= 0;
1573 /* The child_handler function will set shell_function_completed
1574 to 1 when the child dies normally, or to -1 if it
1575 dies with status 127, which is most likely an exec fail. */
1577 if (shell_function_completed
== -1)
1579 /* This most likely means that the execvp failed,
1580 so we should just write out the error message
1581 that came in over the pipe from the child. */
1582 fputs (buffer
, stderr
);
1587 /* The child finished normally. Replace all
1588 newlines in its output with spaces, and put
1589 that in the variable output buffer. */
1590 fold_newlines (buffer
, &i
);
1591 o
= variable_buffer_output (o
, buffer
, i
);
1602 /* Do the Amiga version of func_shell. */
1605 func_shell (char *o
, char **argv
, const char *funcname
)
1607 /* Amiga can't fork nor spawn, but I can start a program with
1608 redirection of my choice. However, this means that we
1609 don't have an opportunity to reopen stdout to trap it. Thus,
1610 we save our own stdout onto a new descriptor and dup a temp
1611 file's descriptor onto our stdout temporarily. After we
1612 spawn the shell program, we dup our own stdout back to the
1613 stdout descriptor. The buffer reading is the same as above,
1614 except that we're now reading from a file. */
1616 #include <dos/dos.h>
1617 #include <proto/dos.h>
1620 char tmp_output
[FILENAME_MAX
];
1621 unsigned int maxlen
= 200;
1623 char * buffer
, * ptr
;
1626 char* batch_filename
= NULL
;
1628 /* Construct the argument list. */
1629 command_argv
= construct_command_argv (argv
[0], (char **) NULL
,
1630 (struct file
*) 0, &batch_filename
);
1631 if (command_argv
== 0)
1634 /* Note the mktemp() is a security hole, but this only runs on Amiga.
1635 Ideally we would use main.c:open_tmpfile(), but this uses a special
1636 Open(), not fopen(), and I'm not familiar enough with the code to mess
1638 strcpy (tmp_output
, "t:MakeshXXXXXXXX");
1639 mktemp (tmp_output
);
1640 child_stdout
= Open (tmp_output
, MODE_NEWFILE
);
1642 for (aptr
=command_argv
; *aptr
; aptr
++)
1643 len
+= strlen (*aptr
) + 1;
1645 buffer
= xmalloc (len
+ 1);
1648 for (aptr
=command_argv
; *aptr
; aptr
++)
1650 strcpy (ptr
, *aptr
);
1651 ptr
+= strlen (ptr
) + 1;
1658 Execute (buffer
, NULL
, child_stdout
);
1661 Close (child_stdout
);
1663 child_stdout
= Open (tmp_output
, MODE_OLDFILE
);
1665 buffer
= xmalloc (maxlen
);
1672 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
1675 cc
= Read (child_stdout
, &buffer
[i
], maxlen
- i
);
1680 Close (child_stdout
);
1682 fold_newlines (buffer
, &i
);
1683 o
= variable_buffer_output (o
, buffer
, i
);
1693 equality. Return is string-boolean, ie, the empty string is false.
1696 func_eq (char* o
, char **argv
, char *funcname
)
1698 int result
= ! strcmp (argv
[0], argv
[1]);
1699 o
= variable_buffer_output (o
, result
? "1" : "", result
);
1705 string-boolean not operator.
1708 func_not (char* o
, char **argv
, char *funcname
)
1712 while (isspace ((unsigned char)*s
))
1715 o
= variable_buffer_output (o
, result
? "1" : "", result
);
1721 /* Lookup table for builtin functions.
1723 This doesn't have to be sorted; we use a straight lookup. We might gain
1724 some efficiency by moving most often used functions to the start of the
1727 If MAXIMUM_ARGS is 0, that means there is no maximum and all
1728 comma-separated values are treated as arguments.
1730 EXPAND_ARGS means that all arguments should be expanded before invocation.
1731 Functions that do namespace tricks (foreach) don't automatically expand. */
1733 static char *func_call
PARAMS ((char *o
, char **argv
, const char *funcname
));
1736 static struct function_table_entry function_table_init
[] =
1738 /* Name/size */ /* MIN MAX EXP? Function */
1739 { STRING_SIZE_TUPLE("addprefix"), 2, 2, 1, func_addsuffix_addprefix
},
1740 { STRING_SIZE_TUPLE("addsuffix"), 2, 2, 1, func_addsuffix_addprefix
},
1741 { STRING_SIZE_TUPLE("basename"), 0, 1, 1, func_basename_dir
},
1742 { STRING_SIZE_TUPLE("dir"), 0, 1, 1, func_basename_dir
},
1743 { STRING_SIZE_TUPLE("notdir"), 0, 1, 1, func_notdir_suffix
},
1744 { STRING_SIZE_TUPLE("subst"), 3, 3, 1, func_subst
},
1745 { STRING_SIZE_TUPLE("suffix"), 0, 1, 1, func_notdir_suffix
},
1746 { STRING_SIZE_TUPLE("filter"), 2, 2, 1, func_filter_filterout
},
1747 { STRING_SIZE_TUPLE("filter-out"), 2, 2, 1, func_filter_filterout
},
1748 { STRING_SIZE_TUPLE("findstring"), 2, 2, 1, func_findstring
},
1749 { STRING_SIZE_TUPLE("firstword"), 0, 1, 1, func_firstword
},
1750 { STRING_SIZE_TUPLE("join"), 2, 2, 1, func_join
},
1751 { STRING_SIZE_TUPLE("patsubst"), 3, 3, 1, func_patsubst
},
1752 { STRING_SIZE_TUPLE("shell"), 0, 1, 1, func_shell
},
1753 { STRING_SIZE_TUPLE("sort"), 0, 1, 1, func_sort
},
1754 { STRING_SIZE_TUPLE("strip"), 0, 1, 1, func_strip
},
1755 { STRING_SIZE_TUPLE("wildcard"), 0, 1, 1, func_wildcard
},
1756 { STRING_SIZE_TUPLE("word"), 2, 2, 1, func_word
},
1757 { STRING_SIZE_TUPLE("wordlist"), 3, 3, 1, func_wordlist
},
1758 { STRING_SIZE_TUPLE("words"), 0, 1, 1, func_words
},
1759 { STRING_SIZE_TUPLE("origin"), 0, 1, 1, func_origin
},
1760 { STRING_SIZE_TUPLE("foreach"), 3, 3, 0, func_foreach
},
1761 { STRING_SIZE_TUPLE("call"), 1, 0, 1, func_call
},
1762 { STRING_SIZE_TUPLE("error"), 0, 1, 1, func_error
},
1763 { STRING_SIZE_TUPLE("warning"), 0, 1, 1, func_error
},
1764 { STRING_SIZE_TUPLE("if"), 2, 3, 0, func_if
},
1765 { STRING_SIZE_TUPLE("value"), 0, 1, 1, func_value
},
1766 { STRING_SIZE_TUPLE("eval"), 0, 1, 1, func_eval
},
1768 { STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq
},
1769 { STRING_SIZE_TUPLE("not"), 0, 1, 1, func_not
},
1773 #define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry))
1776 /* These must come after the definition of function_table. */
1779 expand_builtin_function (char *o
, int argc
, char **argv
,
1780 const struct function_table_entry
*entry_p
)
1782 if (argc
< (int)entry_p
->minimum_args
)
1783 fatal (reading_file
,
1784 _("Insufficient number of arguments (%d) to function `%s'"),
1785 argc
, entry_p
->name
);
1787 /* I suppose technically some function could do something with no
1788 arguments, but so far none do, so just test it for all functions here
1789 rather than in each one. We can change it later if necessary. */
1794 if (!entry_p
->func_ptr
)
1795 fatal (reading_file
, _("Unimplemented on this platform: function `%s'"),
1798 return entry_p
->func_ptr (o
, argv
, entry_p
->name
);
1801 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1802 opening ( or { and is not null-terminated. If a function invocation
1803 is found, expand it into the buffer at *OP, updating *OP, incrementing
1804 *STRINGP past the reference and returning nonzero. If not, return zero. */
1807 handle_function (char **op
, char **stringp
)
1809 const struct function_table_entry
*entry_p
;
1810 char openparen
= (*stringp
)[0];
1811 char closeparen
= openparen
== '(' ? ')' : '}';
1816 char **argv
, **argvp
;
1821 entry_p
= lookup_function (beg
);
1826 /* We found a builtin function. Find the beginning of its arguments (skip
1827 whitespace after the name). */
1829 beg
= next_token (beg
+ entry_p
->len
);
1831 /* Find the end of the function invocation, counting nested use of
1832 whichever kind of parens we use. Since we're looking, count commas
1833 to get a rough estimate of how many arguments we might have. The
1834 count might be high, but it'll never be low. */
1836 for (nargs
=1, end
=beg
; *end
!= '\0'; ++end
)
1839 else if (*end
== openparen
)
1841 else if (*end
== closeparen
&& --count
< 0)
1845 fatal (reading_file
,
1846 _("unterminated call to function `%s': missing `%c'"),
1847 entry_p
->name
, closeparen
);
1851 /* Get some memory to store the arg pointers. */
1852 argvp
= argv
= (char **) alloca (sizeof (char *) * (nargs
+ 2));
1854 /* Chop the string into arguments, then a nul. As soon as we hit
1855 MAXIMUM_ARGS (if it's >0) assume the rest of the string is part of the
1858 If we're expanding, store pointers to the expansion of each one. If
1859 not, make a duplicate of the string and point into that, nul-terminating
1862 if (!entry_p
->expand_args
)
1864 int len
= end
- beg
;
1866 p
= xmalloc (len
+1);
1867 memcpy (p
, beg
, len
);
1873 for (p
=beg
, nargs
=0; p
<= end
; ++argvp
)
1879 if (nargs
== entry_p
->maximum_args
1880 || (! (next
= find_next_argument (openparen
, closeparen
, p
, end
))))
1883 if (entry_p
->expand_args
)
1884 *argvp
= expand_argument (p
, next
);
1895 /* Finally! Run the function... */
1896 *op
= expand_builtin_function (*op
, nargs
, argv
, entry_p
);
1899 if (entry_p
->expand_args
)
1900 for (argvp
=argv
; *argvp
!= 0; ++argvp
)
1909 /* User-defined functions. Expand the first argument as either a builtin
1910 function or a make variable, in the context of the rest of the arguments
1911 assigned to $1, $2, ... $N. $0 is the name of the function. */
1914 func_call (char *o
, char **argv
, const char *funcname UNUSED
)
1916 static int max_args
= 0;
1923 const struct function_table_entry
*entry_p
;
1926 /* There is no way to define a variable with a space in the name, so strip
1927 leading and trailing whitespace as a favor to the user. */
1929 while (*fname
!= '\0' && isspace ((unsigned char)*fname
))
1932 cp
= fname
+ strlen (fname
) - 1;
1933 while (cp
> fname
&& isspace ((unsigned char)*cp
))
1937 /* Calling nothing is a no-op */
1941 /* Are we invoking a builtin function? */
1943 entry_p
= lookup_function (fname
);
1947 /* How many arguments do we have? */
1948 for (i
=0; argv
[i
+1]; ++i
)
1951 return expand_builtin_function (o
, i
, argv
+1, entry_p
);
1954 /* Not a builtin, so the first argument is the name of a variable to be
1955 expanded and interpreted as a function. Find it. */
1956 flen
= strlen (fname
);
1958 v
= lookup_variable (fname
, flen
);
1961 warn_undefined (fname
, flen
);
1963 if (v
== 0 || *v
->value
== '\0')
1966 body
= (char *) alloca (flen
+ 4);
1969 memcpy (body
+ 2, fname
, flen
);
1971 body
[flen
+3] = '\0';
1973 /* Set up arguments $(1) .. $(N). $(0) is the function name. */
1975 push_new_variable_scope ();
1977 for (i
=0; *argv
; ++i
, ++argv
)
1981 sprintf (num
, "%d", i
);
1982 define_variable (num
, strlen (num
), *argv
, o_automatic
, 0);
1985 /* If the number of arguments we have is < max_args, it means we're inside
1986 a recursive invocation of $(call ...). Fill in the remaining arguments
1987 in the new scope with the empty value, to hide them from this
1990 for (; i
< max_args
; ++i
)
1994 sprintf (num
, "%d", i
);
1995 define_variable (num
, strlen (num
), "", o_automatic
, 0);
1998 /* Expand the body in the context of the arguments, adding the result to
1999 the variable buffer. */
2001 v
->exp_count
= EXP_COUNT_MAX
;
2003 saved_args
= max_args
;
2005 o
= variable_expand_string (o
, body
, flen
+3);
2006 max_args
= saved_args
;
2010 pop_variable_scope ();
2012 return o
+ strlen (o
);
2016 hash_init_function_table (void)
2018 hash_init (&function_table
, FUNCTION_TABLE_ENTRIES
* 2,
2019 function_table_entry_hash_1
, function_table_entry_hash_2
,
2020 function_table_entry_hash_cmp
);
2021 hash_load (&function_table
, function_table_init
,
2022 FUNCTION_TABLE_ENTRIES
, sizeof (struct function_table_entry
));