1 /* Builtin function expansion for GNU Make.
2 Copyright (C) 1988,89,91,92,93,94,95,96,97 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. */
32 struct function_table_entry
38 char *(*func_ptr
) PARAMS((char *output
, char **argv
, const char*funcname
));
42 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
43 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
44 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
45 nonzero, substitutions are done only on matches which are complete
46 whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
47 done only at the ends of whitespace-delimited words. */
50 subst_expand (o
, text
, subst
, replace
, slen
, rlen
, by_word
, suffix_only
)
53 char *subst
, *replace
;
54 unsigned int slen
, rlen
;
55 int by_word
, suffix_only
;
57 register char *t
= text
;
60 if (slen
== 0 && !by_word
&& !suffix_only
)
62 /* The first occurrence of "" in any string is its end. */
63 o
= variable_buffer_output (o
, t
, strlen (t
));
65 o
= variable_buffer_output (o
, replace
, rlen
);
71 if ((by_word
| suffix_only
) && slen
== 0)
72 /* When matching by words, the empty string should match
73 the end of each word, rather than the end of the whole text. */
74 p
= end_of_token (next_token (t
));
77 p
= sindex (t
, 0, subst
, slen
);
80 /* No more matches. Output everything left on the end. */
81 o
= variable_buffer_output (o
, t
, strlen (t
));
86 /* Output everything before this occurrence of the string to replace. */
88 o
= variable_buffer_output (o
, t
, p
- t
);
90 /* If we're substituting only by fully matched words,
91 or only at the ends of words, check that this case qualifies. */
93 && ((p
> t
&& !isblank (p
[-1]))
94 || (p
[slen
] != '\0' && !isblank (p
[slen
]))))
96 && (p
[slen
] != '\0' && !isblank (p
[slen
]))))
97 /* Struck out. Output the rest of the string that is
98 no longer to be replaced. */
99 o
= variable_buffer_output (o
, subst
, slen
);
101 /* Output the replacement string. */
102 o
= variable_buffer_output (o
, replace
, rlen
);
104 /* Advance T past the string to be replaced. */
106 } while (*t
!= '\0');
112 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
113 and replacing strings matching PATTERN with REPLACE.
114 If PATTERN_PERCENT is not nil, PATTERN has already been
115 run through find_percent, and PATTERN_PERCENT is the result.
116 If REPLACE_PERCENT is not nil, REPLACE has already been
117 run through find_percent, and REPLACE_PERCENT is the result. */
120 patsubst_expand (o
, text
, pattern
, replace
, pattern_percent
, replace_percent
)
123 register char *pattern
, *replace
;
124 register char *pattern_percent
, *replace_percent
;
126 unsigned int pattern_prepercent_len
, pattern_postpercent_len
;
127 unsigned int replace_prepercent_len
, replace_postpercent_len
= 0;
132 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
133 will be collapsed before we call subst_expand if PATTERN has no %. */
134 if (replace_percent
== 0)
135 replace_percent
= find_percent (replace
);
136 if (replace_percent
!= 0)
138 /* Record the length of REPLACE before and after the % so
139 we don't have to compute these lengths more than once. */
140 replace_prepercent_len
= replace_percent
- replace
;
141 replace_postpercent_len
= strlen (replace_percent
+ 1);
144 /* We store the length of the replacement
145 so we only need to compute it once. */
146 replace_prepercent_len
= strlen (replace
);
148 if (pattern_percent
== 0)
149 pattern_percent
= find_percent (pattern
);
150 if (pattern_percent
== 0)
151 /* With no % in the pattern, this is just a simple substitution. */
152 return subst_expand (o
, text
, pattern
, replace
,
153 strlen (pattern
), strlen (replace
), 1, 0);
155 /* Record the length of PATTERN before and after the %
156 so we don't have to compute it more than once. */
157 pattern_prepercent_len
= pattern_percent
- pattern
;
158 pattern_postpercent_len
= strlen (pattern_percent
+ 1);
160 while ((t
= find_next_token (&text
, &len
)) != 0)
164 /* Is it big enough to match? */
165 if (len
< pattern_prepercent_len
+ pattern_postpercent_len
)
168 /* Does the prefix match? */
169 if (!fail
&& pattern_prepercent_len
> 0
171 || t
[pattern_prepercent_len
- 1] != pattern_percent
[-1]
172 || !strneq (t
+ 1, pattern
+ 1, pattern_prepercent_len
- 1)))
175 /* Does the suffix match? */
176 if (!fail
&& pattern_postpercent_len
> 0
177 && (t
[len
- 1] != pattern_percent
[pattern_postpercent_len
]
178 || t
[len
- pattern_postpercent_len
] != pattern_percent
[1]
179 || !strneq (&t
[len
- pattern_postpercent_len
],
180 &pattern_percent
[1], pattern_postpercent_len
- 1)))
184 /* It didn't match. Output the string. */
185 o
= variable_buffer_output (o
, t
, len
);
188 /* It matched. Output the replacement. */
190 /* Output the part of the replacement before the %. */
191 o
= variable_buffer_output (o
, replace
, replace_prepercent_len
);
193 if (replace_percent
!= 0)
195 /* Output the part of the matched string that
196 matched the % in the pattern. */
197 o
= variable_buffer_output (o
, t
+ pattern_prepercent_len
,
198 len
- (pattern_prepercent_len
199 + pattern_postpercent_len
));
200 /* Output the part of the replacement after the %. */
201 o
= variable_buffer_output (o
, replace_percent
+ 1,
202 replace_postpercent_len
);
206 /* Output a space, but not if the replacement is "". */
207 if (fail
|| replace_prepercent_len
> 0
208 || (replace_percent
!= 0 && len
+ replace_postpercent_len
> 0))
210 o
= variable_buffer_output (o
, " ", 1);
215 /* Kill the last space. */
222 /* Look up a function by name.
223 The table is currently small enough that it's not really worthwhile to use
224 a fancier lookup algorithm. If it gets larger, maybe...
227 static const struct function_table_entry
*
228 lookup_function (table
, s
)
229 const struct function_table_entry
*table
;
234 for (; table
->name
!= NULL
; ++table
)
235 if (table
->len
<= len
236 && (isblank (s
[table
->len
]) || s
[table
->len
] == '\0')
237 && strneq (s
, table
->name
, table
->len
))
244 /* Return 1 if PATTERN matches STR, 0 if not. */
247 pattern_matches (pattern
, percent
, str
)
248 register char *pattern
, *percent
, *str
;
250 unsigned int sfxlen
, strlength
;
254 unsigned int len
= strlen (pattern
) + 1;
255 char *new_chars
= (char *) alloca (len
);
256 bcopy (pattern
, new_chars
, len
);
258 percent
= find_percent (pattern
);
260 return streq (pattern
, str
);
263 sfxlen
= strlen (percent
+ 1);
264 strlength
= strlen (str
);
266 if (strlength
< (percent
- pattern
) + sfxlen
267 || !strneq (pattern
, str
, percent
- pattern
))
270 return !strcmp (percent
+ 1, str
+ (strlength
- sfxlen
));
274 /* Find the next comma or ENDPAREN (counting nested STARTPAREN and
275 ENDPARENtheses), starting at PTR before END. Return a pointer to
278 If no next argument is found, return NULL.
282 find_next_argument (startparen
, endparen
, ptr
, end
)
290 for (; ptr
< end
; ++ptr
)
291 if (*ptr
== startparen
)
294 else if (*ptr
== endparen
)
301 else if (*ptr
== ',' && !count
)
304 /* We didn't find anything. */
309 /* Glob-expand LINE. The returned pointer is
310 only good until the next call to string_glob. */
316 static char *result
= 0;
317 static unsigned int length
;
318 register struct nameseq
*chain
;
319 register unsigned int idx
;
321 chain
= multi_glob (parse_file_seq
322 (&line
, '\0', sizeof (struct nameseq
),
323 /* We do not want parse_file_seq to strip `./'s.
324 That would break examples like:
325 $(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
327 sizeof (struct nameseq
));
332 result
= (char *) xmalloc (100);
338 register char *name
= chain
->name
;
339 unsigned int len
= strlen (name
);
341 struct nameseq
*next
= chain
->next
;
342 free ((char *) chain
);
345 /* multi_glob will pass names without globbing metacharacters
346 through as is, but we want only files that actually exist. */
347 if (file_exists_p (name
))
349 if (idx
+ len
+ 1 > length
)
351 length
+= (len
+ 1) * 2;
352 result
= (char *) xrealloc (result
, length
);
354 bcopy (name
, &result
[idx
], len
);
362 /* Kill the last space and terminate the string. */
366 result
[idx
- 1] = '\0';
376 func_patsubst (o
, argv
, funcname
)
379 const char *funcname
;
381 o
= patsubst_expand (o
, argv
[2], argv
[0], argv
[1], (char *) 0, (char *) 0);
387 func_join(o
, argv
, funcname
)
390 const char *funcname
;
394 /* Write each word of the first argument directly followed
395 by the corresponding word of the second argument.
396 If the two arguments have a different number of words,
397 the excess words are just output separated by blanks. */
400 char *list1_iterator
= argv
[0];
401 char *list2_iterator
= argv
[1];
404 unsigned int len1
, len2
;
406 tp
= find_next_token (&list1_iterator
, &len1
);
408 o
= variable_buffer_output (o
, tp
, len1
);
410 pp
= find_next_token (&list2_iterator
, &len2
);
412 o
= variable_buffer_output (o
, pp
, len2
);
414 if (tp
!= 0 || pp
!= 0)
416 o
= variable_buffer_output (o
, " ", 1);
420 while (tp
!= 0 || pp
!= 0);
422 /* Kill the last blank. */
430 func_origin(o
, argv
, funcname
)
433 const char *funcname
;
435 /* Expand the argument. */
436 register struct variable
*v
= lookup_variable (argv
[0], strlen (argv
[0]));
438 o
= variable_buffer_output (o
, "undefined", 9);
447 o
= variable_buffer_output (o
, "default", 7);
450 o
= variable_buffer_output (o
, "environment", 11);
453 o
= variable_buffer_output (o
, "file", 4);
456 o
= variable_buffer_output (o
, "environment override", 20);
459 o
= variable_buffer_output (o
, "command line", 12);
462 o
= variable_buffer_output (o
, "override", 8);
465 o
= variable_buffer_output (o
, "automatic", 9);
473 #define IS_PATHSEP(c) ((c) == ']')
475 #if defined(__MSDOS__) || defined(WINDOWS32)
476 #define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
478 #define IS_PATHSEP(c) ((c) == '/')
484 func_notdir_suffix(o
, argv
, funcname
)
487 const char *funcname
;
489 /* Expand the argument. */
490 char *list_iterator
= argv
[0];
495 int is_suffix
= streq(funcname
, "suffix");
496 int is_notdir
= !is_suffix
;
497 while ((p2
= find_next_token (&list_iterator
, &len
)) != 0)
502 while (p
>= p2
&& (!is_suffix
|| *p
!= '.'))
515 o
= variable_buffer_output (o
, p
, len
- (p
- p2
));
517 #if defined(WINDOWS32) || defined(__MSDOS__)
518 /* Handle the case of "d:foo/bar". */
519 else if (streq(funcname
, "notdir") && p2
[0] && p2
[1] == ':')
522 o
= variable_buffer_output (o
, p
, len
- (p
- p2
));
526 o
= variable_buffer_output (o
, p2
, len
);
528 if (is_notdir
|| p
>= p2
)
530 o
= variable_buffer_output (o
, " ", 1);
535 /* Kill last space. */
545 func_basename_dir(o
, argv
, funcname
)
548 const char *funcname
;
550 /* Expand the argument. */
556 int is_basename
= streq(funcname
, "basename");
557 int is_dir
= !is_basename
;
559 while ((p2
= find_next_token (&p3
, &len
)) != 0)
562 while (p
>= p2
&& (!is_basename
|| *p
!= '.'))
569 if (p
>= p2
&& (is_dir
))
570 o
= variable_buffer_output (o
, p2
, ++p
- p2
);
571 else if (p
>= p2
&& (*p
== '.'))
572 o
= variable_buffer_output (o
, p2
, p
- p2
);
573 #if defined(WINDOWS32) || defined(__MSDOS__)
574 /* Handle the "d:foobar" case */
575 else if (p2
[0] && p2
[1] == ':' && is_dir
)
576 o
= variable_buffer_output (o
, p2
, 2);
580 o
= variable_buffer_output (o
, "[]", 2);
583 o
= variable_buffer_output (o
, "./", 2);
585 ; /* Just a nop... */
589 /* The entire name is the basename. */
590 o
= variable_buffer_output (o
, p2
, len
);
592 o
= variable_buffer_output (o
, " ", 1);
596 /* Kill last space. */
604 func_addsuffix_addprefix(o
, argv
, funcname
)
607 const char *funcname
;
609 int fixlen
= strlen (argv
[0]);
610 char *list_iterator
= argv
[1];
611 int is_addprefix
= streq (funcname
, "addprefix");
612 int is_addsuffix
= !is_addprefix
;
618 while ((p
= find_next_token (&list_iterator
, &len
)) != 0)
621 o
= variable_buffer_output (o
, argv
[0], fixlen
);
622 o
= variable_buffer_output (o
, p
, len
);
624 o
= variable_buffer_output (o
, argv
[0], fixlen
);
625 o
= variable_buffer_output (o
, " ", 1);
630 /* Kill last space. */
637 func_subst(o
, argv
, funcname
)
640 const char *funcname
;
642 o
= subst_expand (o
, argv
[2], argv
[0], argv
[1], strlen (argv
[0]),
643 strlen (argv
[1]), 0, 0);
650 func_firstword(o
, argv
, funcname
)
653 const char *funcname
;
656 char *words
= argv
[0];
657 char *p
= find_next_token (&words
, &i
);
660 o
= variable_buffer_output (o
, p
, i
);
667 func_words(o
, argv
, funcname
)
670 const char *funcname
;
673 char *word_iterator
= argv
[0];
676 while (find_next_token (&word_iterator
, (unsigned int *) 0) != 0)
679 sprintf (buf
, "%d", i
);
680 o
= variable_buffer_output (o
, buf
, strlen (buf
));
687 strip_whitespace (begpp
, endpp
)
691 while (isspace (**begpp
) && *begpp
<= *endpp
)
693 while (isspace (**endpp
) && *endpp
>= *begpp
)
702 char *end
= p
+ strlen (p
) - 1;
704 strip_whitespace (&p
, &end
);
707 if (!ISDIGIT (*(p
++))) /* ISDIGIT only evals its arg once: see make.h. */
710 return (end
- beg
>= 0);
714 check_numeric (s
, message
)
719 fatal (reading_file
, message
);
725 func_word(o
, argv
, funcname
)
728 const char *funcname
;
734 /* Check the first argument. */
735 check_numeric (argv
[0], _("non-numeric first argument to `word' function"));
739 fatal (reading_file
, _("the `word' function takes a positive index argument"));
743 while ((p
= find_next_token (&end_p
, 0)) != 0)
748 o
= variable_buffer_output (o
, p
, end_p
- p
);
754 func_wordlist (o
, argv
, funcname
)
757 const char *funcname
;
762 /* Check the first argument. */
763 check_numeric (argv
[0],
764 _("non-numeric first argument to `wordlist' function"));
766 check_numeric (argv
[1],
767 _("non-numeric second argument to `wordlist' function"));
774 char *end_p
= argv
[2];
776 int start
= (i
< j
) ? i
: j
;
784 while (((p
= find_next_token (&end_p
, 0)) != 0) && --start
)
788 while (--count
&& (find_next_token (&end_p
, 0) != 0))
790 o
= variable_buffer_output (o
, p
, end_p
- p
);
797 func_findstring(o
, argv
, funcname
)
800 const char *funcname
;
802 /* Find the first occurrence of the first string in the second. */
803 int i
= strlen (argv
[0]);
804 if (sindex (argv
[1], 0, argv
[0], i
) != 0)
805 o
= variable_buffer_output (o
, argv
[0], i
);
811 func_foreach (o
, argv
, funcname
)
814 const char *funcname
;
816 /* expand only the first two. */
817 char *varname
= expand_argument (argv
[0], argv
[1] - 1);
818 char *list
= expand_argument (argv
[1], argv
[2] -1);
819 char *body
= savestring (argv
[2], argv
[3] - argv
[2] - 1);
822 char *list_iterator
= list
;
824 register struct variable
*var
=0;
827 push_new_variable_scope ();
828 var
= define_variable (varname
, strlen (varname
), "", o_automatic
, 0);
830 /* loop through LIST, put the value in VAR and expand BODY */
831 while ((p
= find_next_token (&list_iterator
, &len
)) != 0)
840 var
->value
= (char *) xstrdup ((char*) p
);
844 result
= allocated_variable_expand (body
);
846 o
= variable_buffer_output (o
, result
, strlen (result
));
847 o
= variable_buffer_output (o
, " ", 1);
853 /* Kill the last space. */
856 pop_variable_scope ();
872 func_filter_filterout (o
, argv
, funcname
)
875 const char *funcname
;
877 struct a_word
*wordhead
=0;
878 struct a_word
*wordtail
=0;
880 int is_filter
= streq (funcname
, "filter");
881 char *patterns
= argv
[0];
886 char *word_iterator
= argv
[1];
888 /* Chop ARGV[1] up into words and then run each pattern through. */
889 while ((p
= find_next_token (&word_iterator
, &len
)) != 0)
891 struct a_word
*w
= (struct a_word
*)alloca(sizeof(struct a_word
));
898 if (*word_iterator
!= '\0')
907 struct a_word
*wp
=0;
908 char *pat_iterator
= patterns
;
914 /* Run each pattern through the words, killing words. */
915 while ((p
= find_next_token (&pat_iterator
, &len
)) != 0)
921 percent
= find_percent (p
);
922 for (wp
= wordhead
; wp
!= 0; wp
= wp
->next
)
923 wp
->matched
|= (percent
== 0 ? streq (p
, wp
->str
)
924 : pattern_matches (p
, percent
, wp
->str
));
929 /* Output the words that matched (or didn't, for filter-out). */
930 for (wp
= wordhead
; wp
!= 0; wp
= wp
->next
)
931 if (is_filter
? wp
->matched
: !wp
->matched
)
933 o
= variable_buffer_output (o
, wp
->str
, strlen (wp
->str
));
934 o
= variable_buffer_output (o
, " ", 1);
939 /* Kill the last space. */
948 func_strip(o
, argv
, funcname
)
951 const char *funcname
;
964 for (i
=0; *p
!= '\0' && !isspace(*p
); ++p
, ++i
)
968 o
= variable_buffer_output (o
, word_start
, i
);
969 o
= variable_buffer_output (o
, " ", 1);
974 /* Kill the last space. */
980 Print a warning or fatal message.
983 func_error (o
, argv
, funcname
)
986 const char *funcname
;
992 /* The arguments will be broken on commas. Rather than create yet
993 another special case where function arguments aren't broken up,
994 just create a format string that puts them back together. */
995 for (len
=0, argvp
=argv
; *argvp
!= 0; ++argvp
)
996 len
+= strlen(*argvp
) + 2;
998 p
= msg
= alloca (len
+ 1);
1000 for (argvp
=argv
; argvp
[1] != 0; ++argvp
)
1003 p
+= strlen(*argvp
);
1009 if (*funcname
== 'e')
1010 fatal (reading_file
, "%s", msg
);
1012 /* The warning function expands to the empty string. */
1013 error (reading_file
, "%s", msg
);
1020 chop argv[0] into words, and sort them.
1023 func_sort (o
, argv
, funcname
)
1026 const char *funcname
;
1030 register int wordi
= 0;
1032 /* Chop ARGV[0] into words and put them in WORDS. */
1038 while ((p
= find_next_token (&t
, &len
)) != 0)
1040 if (wordi
>= nwords
- 1)
1042 nwords
= 2* nwords
+ 5;
1043 words
= (char **) xrealloc ((char *) words
,
1044 nwords
* sizeof (char *));
1046 words
[wordi
++] = savestring (p
, len
);
1052 /* Now sort the list of words. */
1053 qsort ((char *) words
, wordi
, sizeof (char *), alpha_compare
);
1055 /* Now write the sorted list. */
1056 for (i
= 0; i
< wordi
; ++i
)
1058 len
= strlen (words
[i
]);
1059 if (i
== wordi
- 1 || strlen (words
[i
+ 1]) != len
1060 || strcmp (words
[i
], words
[i
+ 1]))
1062 o
= variable_buffer_output (o
, words
[i
], len
);
1063 o
= variable_buffer_output (o
, " ", 1);
1067 /* Kill the last space. */
1076 $(if condition,true-part[,false-part])
1078 CONDITION is false iff it evaluates to an empty string. White
1079 space before and after condition are stripped before evaluation.
1081 If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is
1082 evaluated (if it exists). Because only one of the two PARTs is evaluated,
1083 you can use $(if ...) to create side-effects (with $(shell ...), for
1088 func_if (o
, argv
, funcname
)
1091 const char *funcname
;
1093 char *begp
= argv
[0];
1094 char *endp
= argv
[1]-1;
1097 /* Find the result of the condition: if we have a value, and it's not
1098 empty, the condition is true. If we don't have a value, or it's the
1099 empty string, then it's false. */
1101 strip_whitespace (&begp
, &endp
);
1105 char *expansion
= expand_argument (begp
, endp
);
1107 result
= strlen (expansion
);
1111 /* If the result is true (1) we want to eval the first argument, and if
1112 it's false (0) we want to eval the second. If the argument doesn't
1113 exist we do nothing, otherwise expand it and add to the buffer. */
1115 argv
+= 1 + !result
;
1117 if (argv
[0] != NULL
&& argv
[1] != NULL
)
1120 char **endp
= argv
+1;
1122 /* If we're doing the else-clause, make sure we concatenate any
1123 potential extra arguments into the last argument. */
1125 while (*endp
&& **endp
!= '\0')
1128 expansion
= expand_argument (*argv
, *endp
-1);
1130 o
= variable_buffer_output (o
, expansion
, strlen (expansion
));
1138 func_wildcard(o
, argv
, funcname
)
1141 const char *funcname
;
1145 o
= wildcard_expansion (argv
[0], o
);
1147 char *p
= string_glob (argv
[0]);
1148 o
= variable_buffer_output (o
, p
, strlen (p
));
1154 \r is replaced on UNIX as well. Is this desirable?
1157 fold_newlines (buffer
, length
)
1163 char *last_nonnl
= buffer
-1;
1165 for (; *src
!= '\0'; ++src
)
1167 if (src
[0] == '\r' && src
[1] == '\n')
1179 *(++last_nonnl
) = '\0';
1180 *length
= last_nonnl
- buffer
;
1185 int shell_function_pid
= 0, shell_function_completed
;
1191 #include <windows.h>
1193 #include "sub_proc.h"
1197 windows32_openpipe (int *pipedes
, int *pid_p
, char **command_argv
, char **envp
)
1199 SECURITY_ATTRIBUTES saAttr
;
1207 saAttr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1208 saAttr
.bInheritHandle
= TRUE
;
1209 saAttr
.lpSecurityDescriptor
= NULL
;
1211 if (DuplicateHandle(GetCurrentProcess(),
1212 GetStdHandle(STD_INPUT_HANDLE
),
1213 GetCurrentProcess(),
1217 DUPLICATE_SAME_ACCESS
) == FALSE
) {
1218 fatal (NILF
, _("create_child_process: DuplicateHandle(In) failed (e=%d)\n"),
1222 if (DuplicateHandle(GetCurrentProcess(),
1223 GetStdHandle(STD_ERROR_HANDLE
),
1224 GetCurrentProcess(),
1228 DUPLICATE_SAME_ACCESS
) == FALSE
) {
1229 fatal (NILF
, _("create_child_process: DuplicateHandle(Err) failed (e=%d)\n"),
1233 if (!CreatePipe(&hChildOutRd
, &hChildOutWr
, &saAttr
, 0))
1234 fatal (NILF
, _("CreatePipe() failed (e=%d)\n"), GetLastError());
1238 hProcess
= process_init_fd(hIn
, hChildOutWr
, hErr
);
1241 fatal (NILF
, _("windows32_openpipe (): process_init_fd() failed\n"));
1244 process_register(hProcess
);
1246 /* make sure that CreateProcess() has Path it needs */
1247 sync_Path_environment();
1249 if (!process_begin(hProcess
, command_argv
, envp
, command_argv
[0], NULL
))
1250 *pid_p
= (int) hProcess
;
1252 fatal (NILF
, _("windows32_openpipe (): unable to launch process (e=%d)\n"),
1253 process_last_err(hProcess
));
1255 /* set up to read data from child */
1256 pipedes
[0] = _open_osfhandle((long) hChildOutRd
, O_RDONLY
);
1258 /* this will be closed almost right away */
1259 pipedes
[1] = _open_osfhandle((long) hChildOutWr
, O_APPEND
);
1266 msdos_openpipe (int* pipedes
, int *pidp
, char *text
)
1269 /* MSDOS can't fork, but it has `popen'. */
1270 struct variable
*sh
= lookup_variable ("SHELL", 5);
1272 extern int dos_command_running
, dos_status
;
1274 /* Make sure not to bother processing an empty line. */
1275 while (isblank (*text
))
1282 char buf
[PATH_MAX
+ 7];
1283 /* This makes sure $SHELL value is used by $(shell), even
1284 though the target environment is not passed to it. */
1285 sprintf (buf
, "SHELL=%s", sh
->value
);
1291 dos_command_running
= 1;
1293 /* If dos_status becomes non-zero, it means the child process
1294 was interrupted by a signal, like SIGINT or SIGQUIT. See
1295 fatal_error_signal in commands.c. */
1296 fpipe
= popen (text
, "rt");
1297 dos_command_running
= 0;
1298 if (!fpipe
|| dos_status
)
1304 else if (errno
== 0)
1306 shell_function_completed
= -1;
1310 pipedes
[0] = fileno (fpipe
);
1311 *pidp
= 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
1313 shell_function_completed
= 1;
1320 Do shell spawning, with the naughty bits for different OSes.
1325 /* VMS can't do $(shell ...) */
1326 #define func_shell 0
1331 func_shell (o
, argv
, funcname
)
1334 const char *funcname
;
1336 char* batch_filename
= NULL
;
1342 char **command_argv
;
1349 /* Construct the argument list. */
1350 command_argv
= construct_command_argv (argv
[0],
1351 (char **) NULL
, (struct file
*) 0,
1353 if (command_argv
== 0)
1357 /* Using a target environment for `shell' loses in cases like:
1358 export var = $(shell echo foobie)
1359 because target_environment hits a loop trying to expand $(var)
1360 to put it in the environment. This is even more confusing when
1361 var was not explicitly exported, but just appeared in the
1362 calling environment. */
1366 /* For error messages. */
1367 if (reading_file
!= 0)
1369 error_prefix
= (char *) alloca (strlen(reading_file
->filenm
)+11+4);
1370 sprintf (error_prefix
,
1371 "%s:%lu: ", reading_file
->filenm
, reading_file
->lineno
);
1377 windows32_openpipe (pipedes
, &pid
, command_argv
, envp
);
1378 #else /* WINDOWS32 */
1381 fpipe
= msdos_openpipe (pipedes
, &pid
, argv
[0]);
1384 perror_with_name (error_prefix
, "pipe");
1388 if (pipe (pipedes
) < 0)
1390 perror_with_name (error_prefix
, "pipe");
1396 perror_with_name (error_prefix
, "fork");
1398 child_execute_job (0, pipedes
[1], command_argv
, envp
);
1400 # endif /* ! __MSDOS__ */
1402 #endif /* WINDOWS32 */
1404 /* We are the parent. */
1407 unsigned int maxlen
;
1410 /* Record the PID for reap_children. */
1411 shell_function_pid
= pid
;
1413 shell_function_completed
= 0;
1415 /* Free the storage only the child needed. */
1416 free (command_argv
[0]);
1417 free ((char *) command_argv
);
1419 /* Close the write side of the pipe. */
1420 (void) close (pipedes
[1]);
1423 /* Set up and read from the pipe. */
1426 buffer
= (char *) xmalloc (maxlen
+ 1);
1428 /* Read from the pipe until it gets EOF. */
1435 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
1439 cc
= read (pipedes
[0], &buffer
[i
], maxlen
- i
);
1443 while (cc
> 0 || EINTR_SET
);
1445 /* Close the read side of the pipe. */
1448 (void) pclose (fpipe
);
1450 (void) close (pipedes
[0]);
1453 /* Loop until child_handler sets shell_function_completed
1454 to the status of our child shell. */
1455 while (shell_function_completed
== 0)
1456 reap_children (1, 0);
1458 if (batch_filename
) {
1460 printf(_("Cleaning up temporary batch file %s\n"), batch_filename
);
1461 remove(batch_filename
);
1462 free(batch_filename
);
1464 shell_function_pid
= 0;
1466 /* The child_handler function will set shell_function_completed
1467 to 1 when the child dies normally, or to -1 if it
1468 dies with status 127, which is most likely an exec fail. */
1470 if (shell_function_completed
== -1)
1472 /* This most likely means that the execvp failed,
1473 so we should just write out the error message
1474 that came in over the pipe from the child. */
1475 fputs (buffer
, stderr
);
1480 /* The child finished normally. Replace all
1481 newlines in its output with spaces, and put
1482 that in the variable output buffer. */
1483 fold_newlines (buffer
, &i
);
1484 o
= variable_buffer_output (o
, buffer
, i
);
1495 /* Do the Amiga version of func_shell. */
1498 func_shell (char *o
, char **argv
, const char *funcname
)
1500 /* Amiga can't fork nor spawn, but I can start a program with
1501 redirection of my choice. However, this means that we
1502 don't have an opportunity to reopen stdout to trap it. Thus,
1503 we save our own stdout onto a new descriptor and dup a temp
1504 file's descriptor onto our stdout temporarily. After we
1505 spawn the shell program, we dup our own stdout back to the
1506 stdout descriptor. The buffer reading is the same as above,
1507 except that we're now reading from a file. */
1509 #include <dos/dos.h>
1510 #include <proto/dos.h>
1513 char tmp_output
[FILENAME_MAX
];
1514 unsigned int maxlen
= 200;
1516 char * buffer
, * ptr
;
1519 char* batch_filename
= NULL
;
1521 /* Construct the argument list. */
1522 command_argv
= construct_command_argv (argv
[0], (char **) NULL
,
1523 (struct file
*) 0, &batch_filename
);
1524 if (command_argv
== 0)
1528 strcpy (tmp_output
, "t:MakeshXXXXXXXX");
1529 mktemp (tmp_output
);
1530 child_stdout
= Open (tmp_output
, MODE_NEWFILE
);
1532 for (aptr
=command_argv
; *aptr
; aptr
++)
1533 len
+= strlen (*aptr
) + 1;
1535 buffer
= xmalloc (len
+ 1);
1538 for (aptr
=command_argv
; *aptr
; aptr
++)
1540 strcpy (ptr
, *aptr
);
1541 ptr
+= strlen (ptr
) + 1;
1548 Execute (buffer
, NULL
, child_stdout
);
1551 Close (child_stdout
);
1553 child_stdout
= Open (tmp_output
, MODE_OLDFILE
);
1555 buffer
= xmalloc (maxlen
);
1562 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
1565 cc
= Read (child_stdout
, &buffer
[i
], maxlen
- i
);
1570 Close (child_stdout
);
1572 fold_newlines (buffer
, &i
);
1573 o
= variable_buffer_output (o
, buffer
, i
);
1583 equality. Return is string-boolean, ie, the empty string is false.
1586 func_eq (char* o
, char **argv
, char *funcname
)
1588 int result
= ! strcmp (argv
[0], argv
[1]);
1589 o
= variable_buffer_output (o
, result
? "1" : "", result
);
1595 string-boolean not operator.
1598 func_not (char* o
, char **argv
, char *funcname
)
1602 while (isspace (*s
))
1605 o
= variable_buffer_output (o
, result
? "1" : "", result
);
1611 #define STRING_SIZE_TUPLE(_s) (_s), (sizeof(_s)-1)
1613 /* Lookup table for builtin functions.
1615 This doesn't have to be sorted; we use a straight lookup. We might gain
1616 some efficiency by moving most often used functions to the start of the
1619 If REQUIRED_ARGS is positive, the function takes exactly that many
1620 arguments. All subsequent text is included with the last argument. So,
1621 since $(sort a,b,c) takes only one argument, it will be the full string
1622 "a,b,c". If the value is negative, it's the minimum number of arguments.
1623 A function can have more, but if it has less an error is generated.
1625 EXPAND_ARGS means that all arguments should be expanded before invocation.
1626 Functions that do namespace tricks (foreach) don't automatically expand. */
1628 static char *func_call
PARAMS((char *o
, char **argv
, const char *funcname
));
1631 static struct function_table_entry function_table
[] =
1633 /* Name/size */ /* ARG EXP? Function */
1634 { STRING_SIZE_TUPLE("addprefix"), 2, 1, func_addsuffix_addprefix
},
1635 { STRING_SIZE_TUPLE("addsuffix"), 2, 1, func_addsuffix_addprefix
},
1636 { STRING_SIZE_TUPLE("basename"), 1, 1, func_basename_dir
},
1637 { STRING_SIZE_TUPLE("dir"), 1, 1, func_basename_dir
},
1638 { STRING_SIZE_TUPLE("notdir"), 1, 1, func_notdir_suffix
},
1639 { STRING_SIZE_TUPLE("subst"), 3, 1, func_subst
},
1640 { STRING_SIZE_TUPLE("suffix"), 1, 1, func_notdir_suffix
},
1641 { STRING_SIZE_TUPLE("filter"), 2, 1, func_filter_filterout
},
1642 { STRING_SIZE_TUPLE("filter-out"), 2, 1, func_filter_filterout
},
1643 { STRING_SIZE_TUPLE("findstring"), 2, 1, func_findstring
},
1644 { STRING_SIZE_TUPLE("firstword"), 1, 1, func_firstword
},
1645 { STRING_SIZE_TUPLE("join"), 2, 1, func_join
},
1646 { STRING_SIZE_TUPLE("patsubst"), 3, 1, func_patsubst
},
1647 { STRING_SIZE_TUPLE("shell"), 1, 1, func_shell
},
1648 { STRING_SIZE_TUPLE("sort"), 1, 1, func_sort
},
1649 { STRING_SIZE_TUPLE("strip"), 1, 1, func_strip
},
1650 { STRING_SIZE_TUPLE("wildcard"), 1, 1, func_wildcard
},
1651 { STRING_SIZE_TUPLE("word"), 2, 1, func_word
},
1652 { STRING_SIZE_TUPLE("wordlist"), 3, 1, func_wordlist
},
1653 { STRING_SIZE_TUPLE("words"), 1, 1, func_words
},
1654 { STRING_SIZE_TUPLE("origin"), 1, 1, func_origin
},
1655 { STRING_SIZE_TUPLE("foreach"), 3, 0, func_foreach
},
1656 { STRING_SIZE_TUPLE("call"), -1, 1, func_call
},
1657 { STRING_SIZE_TUPLE("error"), 1, 1, func_error
},
1658 { STRING_SIZE_TUPLE("warning"), 1, 1, func_error
},
1659 { STRING_SIZE_TUPLE("if"), -2, 0, func_if
},
1661 { STRING_SIZE_TUPLE("eq"), 2, 1, func_eq
},
1662 { STRING_SIZE_TUPLE("not"), 1, 1, func_not
},
1668 /* These must come after the definition of function_table[]. */
1671 expand_builtin_function (o
, argc
, argv
, entry_p
)
1675 struct function_table_entry
*entry_p
;
1677 int min
= (entry_p
->required_args
> 0
1678 ? entry_p
->required_args
1679 : -entry_p
->required_args
);
1682 fatal (reading_file
,
1683 _("Insufficient number of arguments (%d) to function `%s'"),
1684 argc
, entry_p
->name
);
1686 if (!entry_p
->func_ptr
)
1687 fatal (reading_file
, _("Unimplemented on this platform: function `%s'"),
1690 return entry_p
->func_ptr (o
, argv
, entry_p
->name
);
1693 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1694 opening ( or { and is not null-terminated. If a function invocation
1695 is found, expand it into the buffer at *OP, updating *OP, incrementing
1696 *STRINGP past the reference and returning nonzero. If not, return zero. */
1699 handle_function (op
, stringp
)
1703 const struct function_table_entry
*entry_p
;
1704 char openparen
= (*stringp
)[0];
1705 char closeparen
= openparen
== '(' ? ')' : '}';
1706 char *beg
= *stringp
+ 1;
1711 char **argv
, **argvp
;
1714 entry_p
= lookup_function (function_table
, beg
);
1719 /* We have found a call to a builtin function. Find the end of the
1720 arguments, and do the function. */
1722 endref
= beg
+ entry_p
->len
;
1724 /* Space after function name isn't part of the args. */
1725 p
= next_token (endref
);
1728 /* Find the end of the function invocation, counting nested use of
1729 whichever kind of parens we use. Since we're looking, count commas
1730 to get a rough estimate of how many arguments we might have. The
1731 count might be high, but it'll never be low. */
1733 for (nargs
=1; *p
!= '\0'; ++p
)
1736 else if (*p
== openparen
)
1738 else if (*p
== closeparen
&& --count
< 0)
1742 fatal (reading_file
,
1743 _("unterminated call to function `%s': missing `%c'"),
1744 entry_p
->name
, closeparen
);
1746 /* Get some memory to store the arg pointers. */
1747 argvp
= argv
= (char **) alloca (sizeof(char *) * (nargs
+ 2));
1749 /* Chop the string into arguments, then store the end pointer and a nul.
1750 If REQUIRED_ARGS is positive, as soon as we hit that many assume the
1751 rest of the string is part of the last argument. */
1754 while (entry_p
->required_args
< 0 || nargs
< entry_p
->required_args
)
1756 char *next
= find_next_argument (openparen
, closeparen
, *argvp
, p
);
1761 *(++argvp
) = next
+1;
1768 /* If we should expand, do it. */
1769 if (entry_p
->expand_args
)
1771 for (argvp
=argv
; argvp
[1] != 0; ++argvp
)
1772 *argvp
= expand_argument (*argvp
, argvp
[1]-1);
1774 /* end pointer doesn't make sense for expanded stuff. */
1778 /* Finally! Run the function... */
1779 *op
= expand_builtin_function (*op
, nargs
, argv
, entry_p
);
1781 /* If we allocated memory for the expanded args, free it again. */
1782 if (entry_p
->expand_args
)
1783 for (argvp
=argv
; *argvp
!= 0; ++argvp
)
1792 /* User-defined functions. Expand the first argument as either a builtin
1793 function or a make variable, in the context of the rest of the arguments
1794 assigned to $1, $2, ... $N. $0 is the name of the function. */
1797 func_call (o
, argv
, funcname
)
1800 const char *funcname
;
1806 const struct function_table_entry
*entry_p
;
1808 /* Calling nothing is a no-op. */
1809 if (*argv
[0] == '\0')
1812 /* There is no way to define a variable with a space in the name, so strip
1813 trailing whitespace as a favor to the user. */
1815 flen
= strlen (argv
[0]);
1816 fname
= argv
[0] + flen
- 1;
1817 while (isspace (*fname
))
1821 flen
= fname
- argv
[0] + 1;
1824 /* Are we invoking a builtin function? */
1826 entry_p
= lookup_function (function_table
, fname
);
1830 for (i
=0; argv
[i
+1]; ++i
)
1833 return expand_builtin_function (o
, i
, argv
+ 1, entry_p
);
1836 /* No, so the first argument is the name of a variable to be expanded and
1837 interpreted as a function. Create the variable reference. */
1838 body
= alloca (flen
+ 4);
1841 strcpy (body
+ 2, fname
);
1845 /* Set up arguments $(1) .. $(N). $(0) is the function name. */
1847 push_new_variable_scope ();
1849 for (i
=0; *argv
; ++i
, ++argv
)
1853 sprintf (num
, "%d", i
);
1854 define_variable (num
, strlen (num
), *argv
, o_automatic
, 0);
1857 /* Expand the body in the context of the arguments, adding the result to
1858 the variable buffer. */
1860 o
= variable_expand_string (o
, body
, flen
+3);
1862 pop_variable_scope ();
1864 return o
+ strlen(o
);