1 /* Builtin function expansion for GNU Make.
2 Copyright (C) 1988,1989,1991-1997,1999 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*funcname
));
44 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
45 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
46 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
47 nonzero, substitutions are done only on matches which are complete
48 whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
49 done only at the ends of whitespace-delimited words. */
52 subst_expand (o
, text
, subst
, replace
, slen
, rlen
, by_word
, suffix_only
)
55 char *subst
, *replace
;
56 unsigned int slen
, rlen
;
57 int by_word
, suffix_only
;
59 register char *t
= text
;
62 if (slen
== 0 && !by_word
&& !suffix_only
)
64 /* The first occurrence of "" in any string is its end. */
65 o
= variable_buffer_output (o
, t
, strlen (t
));
67 o
= variable_buffer_output (o
, replace
, rlen
);
73 if ((by_word
| suffix_only
) && slen
== 0)
74 /* When matching by words, the empty string should match
75 the end of each word, rather than the end of the whole text. */
76 p
= end_of_token (next_token (t
));
79 p
= sindex (t
, 0, subst
, slen
);
82 /* No more matches. Output everything left on the end. */
83 o
= variable_buffer_output (o
, t
, strlen (t
));
88 /* Output everything before this occurrence of the string to replace. */
90 o
= variable_buffer_output (o
, t
, p
- t
);
92 /* If we're substituting only by fully matched words,
93 or only at the ends of words, check that this case qualifies. */
95 && ((p
> t
&& !isblank (p
[-1]))
96 || (p
[slen
] != '\0' && !isblank (p
[slen
]))))
98 && (p
[slen
] != '\0' && !isblank (p
[slen
]))))
99 /* Struck out. Output the rest of the string that is
100 no longer to be replaced. */
101 o
= variable_buffer_output (o
, subst
, slen
);
103 /* Output the replacement string. */
104 o
= variable_buffer_output (o
, replace
, rlen
);
106 /* Advance T past the string to be replaced. */
108 } while (*t
!= '\0');
114 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
115 and replacing strings matching PATTERN with REPLACE.
116 If PATTERN_PERCENT is not nil, PATTERN has already been
117 run through find_percent, and PATTERN_PERCENT is the result.
118 If REPLACE_PERCENT is not nil, REPLACE has already been
119 run through find_percent, and REPLACE_PERCENT is the result. */
122 patsubst_expand (o
, text
, pattern
, replace
, pattern_percent
, replace_percent
)
125 register char *pattern
, *replace
;
126 register char *pattern_percent
, *replace_percent
;
128 unsigned int pattern_prepercent_len
, pattern_postpercent_len
;
129 unsigned int replace_prepercent_len
, replace_postpercent_len
= 0;
134 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
135 will be collapsed before we call subst_expand if PATTERN has no %. */
136 if (replace_percent
== 0)
137 replace_percent
= find_percent (replace
);
138 if (replace_percent
!= 0)
140 /* Record the length of REPLACE before and after the % so
141 we don't have to compute these lengths more than once. */
142 replace_prepercent_len
= replace_percent
- replace
;
143 replace_postpercent_len
= strlen (replace_percent
+ 1);
146 /* We store the length of the replacement
147 so we only need to compute it once. */
148 replace_prepercent_len
= strlen (replace
);
150 if (pattern_percent
== 0)
151 pattern_percent
= find_percent (pattern
);
152 if (pattern_percent
== 0)
153 /* With no % in the pattern, this is just a simple substitution. */
154 return subst_expand (o
, text
, pattern
, replace
,
155 strlen (pattern
), strlen (replace
), 1, 0);
157 /* Record the length of PATTERN before and after the %
158 so we don't have to compute it more than once. */
159 pattern_prepercent_len
= pattern_percent
- pattern
;
160 pattern_postpercent_len
= strlen (pattern_percent
+ 1);
162 while ((t
= find_next_token (&text
, &len
)) != 0)
166 /* Is it big enough to match? */
167 if (len
< pattern_prepercent_len
+ pattern_postpercent_len
)
170 /* Does the prefix match? */
171 if (!fail
&& pattern_prepercent_len
> 0
173 || t
[pattern_prepercent_len
- 1] != pattern_percent
[-1]
174 || !strneq (t
+ 1, pattern
+ 1, pattern_prepercent_len
- 1)))
177 /* Does the suffix match? */
178 if (!fail
&& pattern_postpercent_len
> 0
179 && (t
[len
- 1] != pattern_percent
[pattern_postpercent_len
]
180 || t
[len
- pattern_postpercent_len
] != pattern_percent
[1]
181 || !strneq (&t
[len
- pattern_postpercent_len
],
182 &pattern_percent
[1], pattern_postpercent_len
- 1)))
186 /* It didn't match. Output the string. */
187 o
= variable_buffer_output (o
, t
, len
);
190 /* It matched. Output the replacement. */
192 /* Output the part of the replacement before the %. */
193 o
= variable_buffer_output (o
, replace
, replace_prepercent_len
);
195 if (replace_percent
!= 0)
197 /* Output the part of the matched string that
198 matched the % in the pattern. */
199 o
= variable_buffer_output (o
, t
+ pattern_prepercent_len
,
200 len
- (pattern_prepercent_len
201 + pattern_postpercent_len
));
202 /* Output the part of the replacement after the %. */
203 o
= variable_buffer_output (o
, replace_percent
+ 1,
204 replace_postpercent_len
);
208 /* Output a space, but not if the replacement is "". */
209 if (fail
|| replace_prepercent_len
> 0
210 || (replace_percent
!= 0 && len
+ replace_postpercent_len
> 0))
212 o
= variable_buffer_output (o
, " ", 1);
217 /* Kill the last space. */
224 /* Look up a function by name.
225 The table is currently small enough that it's not really worthwhile to use
226 a fancier lookup algorithm. If it gets larger, maybe...
229 static const struct function_table_entry
*
230 lookup_function (table
, s
)
231 const struct function_table_entry
*table
;
236 for (; table
->name
!= NULL
; ++table
)
237 if (table
->len
<= len
238 && (isblank (s
[table
->len
]) || s
[table
->len
] == '\0')
239 && strneq (s
, table
->name
, table
->len
))
246 /* Return 1 if PATTERN matches STR, 0 if not. */
249 pattern_matches (pattern
, percent
, str
)
250 register char *pattern
, *percent
, *str
;
252 unsigned int sfxlen
, strlength
;
256 unsigned int len
= strlen (pattern
) + 1;
257 char *new_chars
= (char *) alloca (len
);
258 bcopy (pattern
, new_chars
, len
);
260 percent
= find_percent (pattern
);
262 return streq (pattern
, str
);
265 sfxlen
= strlen (percent
+ 1);
266 strlength
= strlen (str
);
268 if (strlength
< (percent
- pattern
) + sfxlen
269 || !strneq (pattern
, str
, percent
- pattern
))
272 return !strcmp (percent
+ 1, str
+ (strlength
- sfxlen
));
276 /* Find the next comma or ENDPAREN (counting nested STARTPAREN and
277 ENDPARENtheses), starting at PTR before END. Return a pointer to
280 If no next argument is found, return NULL.
284 find_next_argument (startparen
, endparen
, ptr
, end
)
292 for (; ptr
< end
; ++ptr
)
293 if (*ptr
== startparen
)
296 else if (*ptr
== endparen
)
303 else if (*ptr
== ',' && !count
)
306 /* We didn't find anything. */
311 /* Glob-expand LINE. The returned pointer is
312 only good until the next call to string_glob. */
318 static char *result
= 0;
319 static unsigned int length
;
320 register struct nameseq
*chain
;
321 register unsigned int idx
;
323 chain
= multi_glob (parse_file_seq
324 (&line
, '\0', sizeof (struct nameseq
),
325 /* We do not want parse_file_seq to strip `./'s.
326 That would break examples like:
327 $(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
329 sizeof (struct nameseq
));
334 result
= (char *) xmalloc (100);
340 register char *name
= chain
->name
;
341 unsigned int len
= strlen (name
);
343 struct nameseq
*next
= chain
->next
;
344 free ((char *) chain
);
347 /* multi_glob will pass names without globbing metacharacters
348 through as is, but we want only files that actually exist. */
349 if (file_exists_p (name
))
351 if (idx
+ len
+ 1 > length
)
353 length
+= (len
+ 1) * 2;
354 result
= (char *) xrealloc (result
, length
);
356 bcopy (name
, &result
[idx
], len
);
364 /* Kill the last space and terminate the string. */
368 result
[idx
- 1] = '\0';
378 func_patsubst (o
, argv
, funcname
)
381 const char *funcname
;
383 o
= patsubst_expand (o
, argv
[2], argv
[0], argv
[1], (char *) 0, (char *) 0);
389 func_join(o
, argv
, funcname
)
392 const char *funcname
;
396 /* Write each word of the first argument directly followed
397 by the corresponding word of the second argument.
398 If the two arguments have a different number of words,
399 the excess words are just output separated by blanks. */
402 char *list1_iterator
= argv
[0];
403 char *list2_iterator
= argv
[1];
406 unsigned int len1
, len2
;
408 tp
= find_next_token (&list1_iterator
, &len1
);
410 o
= variable_buffer_output (o
, tp
, len1
);
412 pp
= find_next_token (&list2_iterator
, &len2
);
414 o
= variable_buffer_output (o
, pp
, len2
);
416 if (tp
!= 0 || pp
!= 0)
418 o
= variable_buffer_output (o
, " ", 1);
422 while (tp
!= 0 || pp
!= 0);
424 /* Kill the last blank. */
432 func_origin(o
, argv
, funcname
)
435 const char *funcname
;
437 /* Expand the argument. */
438 register struct variable
*v
= lookup_variable (argv
[0], strlen (argv
[0]));
440 o
= variable_buffer_output (o
, "undefined", 9);
449 o
= variable_buffer_output (o
, "default", 7);
452 o
= variable_buffer_output (o
, "environment", 11);
455 o
= variable_buffer_output (o
, "file", 4);
458 o
= variable_buffer_output (o
, "environment override", 20);
461 o
= variable_buffer_output (o
, "command line", 12);
464 o
= variable_buffer_output (o
, "override", 8);
467 o
= variable_buffer_output (o
, "automatic", 9);
475 #define IS_PATHSEP(c) ((c) == ']')
477 #if defined(__MSDOS__) || defined(WINDOWS32)
478 #define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
480 #define IS_PATHSEP(c) ((c) == '/')
486 func_notdir_suffix(o
, argv
, funcname
)
489 const char *funcname
;
491 /* Expand the argument. */
492 char *list_iterator
= argv
[0];
497 int is_suffix
= streq(funcname
, "suffix");
498 int is_notdir
= !is_suffix
;
499 while ((p2
= find_next_token (&list_iterator
, &len
)) != 0)
504 while (p
>= p2
&& (!is_suffix
|| *p
!= '.'))
517 o
= variable_buffer_output (o
, p
, len
- (p
- p2
));
519 #if defined(WINDOWS32) || defined(__MSDOS__)
520 /* Handle the case of "d:foo/bar". */
521 else if (streq(funcname
, "notdir") && p2
[0] && p2
[1] == ':')
524 o
= variable_buffer_output (o
, p
, len
- (p
- p2
));
528 o
= variable_buffer_output (o
, p2
, len
);
530 if (is_notdir
|| p
>= p2
)
532 o
= variable_buffer_output (o
, " ", 1);
537 /* Kill last space. */
547 func_basename_dir(o
, argv
, funcname
)
550 const char *funcname
;
552 /* Expand the argument. */
558 int is_basename
= streq(funcname
, "basename");
559 int is_dir
= !is_basename
;
561 while ((p2
= find_next_token (&p3
, &len
)) != 0)
564 while (p
>= p2
&& (!is_basename
|| *p
!= '.'))
571 if (p
>= p2
&& (is_dir
))
572 o
= variable_buffer_output (o
, p2
, ++p
- p2
);
573 else if (p
>= p2
&& (*p
== '.'))
574 o
= variable_buffer_output (o
, p2
, p
- p2
);
575 #if defined(WINDOWS32) || defined(__MSDOS__)
576 /* Handle the "d:foobar" case */
577 else if (p2
[0] && p2
[1] == ':' && is_dir
)
578 o
= variable_buffer_output (o
, p2
, 2);
582 o
= variable_buffer_output (o
, "[]", 2);
585 o
= variable_buffer_output (o
, "./", 2);
587 ; /* Just a nop... */
591 /* The entire name is the basename. */
592 o
= variable_buffer_output (o
, p2
, len
);
594 o
= variable_buffer_output (o
, " ", 1);
598 /* Kill last space. */
606 func_addsuffix_addprefix(o
, argv
, funcname
)
609 const char *funcname
;
611 int fixlen
= strlen (argv
[0]);
612 char *list_iterator
= argv
[1];
613 int is_addprefix
= streq (funcname
, "addprefix");
614 int is_addsuffix
= !is_addprefix
;
620 while ((p
= find_next_token (&list_iterator
, &len
)) != 0)
623 o
= variable_buffer_output (o
, argv
[0], fixlen
);
624 o
= variable_buffer_output (o
, p
, len
);
626 o
= variable_buffer_output (o
, argv
[0], fixlen
);
627 o
= variable_buffer_output (o
, " ", 1);
632 /* Kill last space. */
639 func_subst(o
, argv
, funcname
)
642 const char *funcname
;
644 o
= subst_expand (o
, argv
[2], argv
[0], argv
[1], strlen (argv
[0]),
645 strlen (argv
[1]), 0, 0);
652 func_firstword(o
, argv
, funcname
)
655 const char *funcname
;
658 char *words
= argv
[0]; /* Use a temp variable for find_next_token */
659 char *p
= find_next_token (&words
, &i
);
662 o
= variable_buffer_output (o
, p
, i
);
669 func_words(o
, argv
, funcname
)
672 const char *funcname
;
675 char *word_iterator
= argv
[0];
678 while (find_next_token (&word_iterator
, (unsigned int *) 0) != 0)
681 sprintf (buf
, "%d", i
);
682 o
= variable_buffer_output (o
, buf
, strlen (buf
));
689 strip_whitespace (begpp
, endpp
)
693 while (isspace ((unsigned char)**begpp
) && *begpp
<= *endpp
)
695 while (isspace ((unsigned char)**endpp
) && *endpp
>= *begpp
)
704 char *end
= p
+ strlen (p
) - 1;
706 strip_whitespace (&p
, &end
);
709 if (!ISDIGIT (*(p
++))) /* ISDIGIT only evals its arg once: see make.h. */
712 return (end
- beg
>= 0);
716 check_numeric (s
, message
)
721 fatal (reading_file
, message
);
727 func_word(o
, argv
, funcname
)
730 const char *funcname
;
736 /* Check the first argument. */
737 check_numeric (argv
[0], _("non-numeric first argument to `word' function"));
741 fatal (reading_file
, _("the `word' function takes a positive index argument"));
745 while ((p
= find_next_token (&end_p
, 0)) != 0)
750 o
= variable_buffer_output (o
, p
, end_p
- p
);
756 func_wordlist (o
, argv
, funcname
)
759 const char *funcname
;
764 /* Check the first argument. */
765 check_numeric (argv
[0],
766 _("non-numeric first argument to `wordlist' function"));
768 check_numeric (argv
[1],
769 _("non-numeric second argument to `wordlist' function"));
776 char *end_p
= argv
[2];
778 int start
= (i
< j
) ? i
: j
;
786 while (((p
= find_next_token (&end_p
, 0)) != 0) && --start
)
790 while (--count
&& (find_next_token (&end_p
, 0) != 0))
792 o
= variable_buffer_output (o
, p
, end_p
- p
);
799 func_findstring(o
, argv
, funcname
)
802 const char *funcname
;
804 /* Find the first occurrence of the first string in the second. */
805 int i
= strlen (argv
[0]);
806 if (sindex (argv
[1], 0, argv
[0], i
) != 0)
807 o
= variable_buffer_output (o
, argv
[0], i
);
813 func_foreach (o
, argv
, funcname
)
816 const char *funcname
;
818 /* expand only the first two. */
819 char *varname
= expand_argument (argv
[0], NULL
);
820 char *list
= expand_argument (argv
[1], NULL
);
821 char *body
= argv
[2];
824 char *list_iterator
= list
;
827 register struct variable
*var
;
829 push_new_variable_scope ();
830 var
= define_variable (varname
, strlen (varname
), "", o_automatic
, 0);
832 /* loop through LIST, put the value in VAR and expand BODY */
833 while ((p
= find_next_token (&list_iterator
, &len
)) != 0)
842 var
->value
= (char *) xstrdup ((char*) p
);
846 result
= allocated_variable_expand (body
);
848 o
= variable_buffer_output (o
, result
, strlen (result
));
849 o
= variable_buffer_output (o
, " ", 1);
855 /* Kill the last space. */
858 pop_variable_scope ();
873 func_filter_filterout (o
, argv
, funcname
)
876 const char *funcname
;
878 struct a_word
*wordhead
= 0;
879 struct a_word
*wordtail
= 0;
881 int is_filter
= streq (funcname
, "filter");
882 char *patterns
= argv
[0];
883 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 char *pat_iterator
= patterns
;
913 /* Run each pattern through the words, killing words. */
914 while ((p
= find_next_token (&pat_iterator
, &len
)) != 0)
920 percent
= find_percent (p
);
921 for (wp
= wordhead
; wp
!= 0; wp
= wp
->next
)
922 wp
->matched
|= (percent
== 0 ? streq (p
, wp
->str
)
923 : pattern_matches (p
, percent
, wp
->str
));
928 /* Output the words that matched (or didn't, for filter-out). */
929 for (wp
= wordhead
; wp
!= 0; wp
= wp
->next
)
930 if (is_filter
? wp
->matched
: !wp
->matched
)
932 o
= variable_buffer_output (o
, wp
->str
, strlen (wp
->str
));
933 o
= variable_buffer_output (o
, " ", 1);
938 /* Kill the last space. */
947 func_strip(o
, argv
, funcname
)
950 const char *funcname
;
960 while (isspace ((unsigned char)*p
))
963 for (i
=0; *p
!= '\0' && !isspace ((unsigned char)*p
); ++p
, ++i
)
967 o
= variable_buffer_output (o
, word_start
, i
);
968 o
= variable_buffer_output (o
, " ", 1);
973 /* Kill the last space. */
979 Print a warning or fatal message.
982 func_error (o
, argv
, funcname
)
985 const char *funcname
;
991 /* The arguments will be broken on commas. Rather than create yet
992 another special case where function arguments aren't broken up,
993 just create a format string that puts them back together. */
994 for (len
=0, argvp
=argv
; *argvp
!= 0; ++argvp
)
995 len
+= strlen(*argvp
) + 2;
997 p
= msg
= alloca (len
+ 1);
999 for (argvp
=argv
; argvp
[1] != 0; ++argvp
)
1002 p
+= strlen(*argvp
);
1008 if (*funcname
== 'e')
1009 fatal (reading_file
, "%s", msg
);
1011 /* The warning function expands to the empty string. */
1012 error (reading_file
, "%s", msg
);
1019 chop argv[0] into words, and sort them.
1022 func_sort (o
, argv
, funcname
)
1025 const char *funcname
;
1029 register int wordi
= 0;
1031 /* Chop ARGV[0] into words and put them in WORDS. */
1037 while ((p
= find_next_token (&t
, &len
)) != 0)
1039 if (wordi
>= nwords
- 1)
1041 nwords
= (2 * nwords
) + 5;
1042 words
= (char **) xrealloc ((char *) words
,
1043 nwords
* sizeof (char *));
1045 words
[wordi
++] = savestring (p
, len
);
1051 /* Now sort the list of words. */
1052 qsort ((char *) words
, wordi
, sizeof (char *), alpha_compare
);
1054 /* Now write the sorted list. */
1055 for (i
= 0; i
< wordi
; ++i
)
1057 len
= strlen (words
[i
]);
1058 if (i
== wordi
- 1 || strlen (words
[i
+ 1]) != len
1059 || strcmp (words
[i
], words
[i
+ 1]))
1061 o
= variable_buffer_output (o
, words
[i
], len
);
1062 o
= variable_buffer_output (o
, " ", 1);
1066 /* Kill the last space. */
1075 $(if condition,true-part[,false-part])
1077 CONDITION is false iff it evaluates to an empty string. White
1078 space before and after condition are stripped before evaluation.
1080 If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is
1081 evaluated (if it exists). Because only one of the two PARTs is evaluated,
1082 you can use $(if ...) to create side-effects (with $(shell ...), for
1087 func_if (o
, argv
, funcname
)
1090 const char *funcname
;
1092 char *begp
= argv
[0];
1093 char *endp
= begp
+ strlen (argv
[0]);
1096 /* Find the result of the condition: if we have a value, and it's not
1097 empty, the condition is true. If we don't have a value, or it's the
1098 empty string, then it's false. */
1100 strip_whitespace (&begp
, &endp
);
1104 char *expansion
= expand_argument (begp
, NULL
);
1106 result
= strlen (expansion
);
1110 /* If the result is true (1) we want to eval the first argument, and if
1111 it's false (0) we want to eval the second. If the argument doesn't
1112 exist we do nothing, otherwise expand it and add to the buffer. */
1114 argv
+= 1 + !result
;
1120 expansion
= expand_argument (argv
[0], NULL
);
1122 o
= variable_buffer_output (o
, expansion
, strlen (expansion
));
1131 func_wildcard(o
, argv
, funcname
)
1134 const char *funcname
;
1138 o
= wildcard_expansion (argv
[0], o
);
1140 char *p
= string_glob (argv
[0]);
1141 o
= variable_buffer_output (o
, p
, strlen (p
));
1147 \r is replaced on UNIX as well. Is this desirable?
1150 fold_newlines (buffer
, length
)
1156 char *last_nonnl
= buffer
-1;
1158 for (; *src
!= '\0'; ++src
)
1160 if (src
[0] == '\r' && src
[1] == '\n')
1172 *(++last_nonnl
) = '\0';
1173 *length
= last_nonnl
- buffer
;
1178 int shell_function_pid
= 0, shell_function_completed
;
1184 #include <windows.h>
1186 #include "sub_proc.h"
1190 windows32_openpipe (int *pipedes
, int *pid_p
, char **command_argv
, char **envp
)
1192 SECURITY_ATTRIBUTES saAttr
;
1200 saAttr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1201 saAttr
.bInheritHandle
= TRUE
;
1202 saAttr
.lpSecurityDescriptor
= NULL
;
1204 if (DuplicateHandle(GetCurrentProcess(),
1205 GetStdHandle(STD_INPUT_HANDLE
),
1206 GetCurrentProcess(),
1210 DUPLICATE_SAME_ACCESS
) == FALSE
) {
1211 fatal (NILF
, _("create_child_process: DuplicateHandle(In) failed (e=%d)\n"),
1215 if (DuplicateHandle(GetCurrentProcess(),
1216 GetStdHandle(STD_ERROR_HANDLE
),
1217 GetCurrentProcess(),
1221 DUPLICATE_SAME_ACCESS
) == FALSE
) {
1222 fatal (NILF
, _("create_child_process: DuplicateHandle(Err) failed (e=%d)\n"),
1226 if (!CreatePipe(&hChildOutRd
, &hChildOutWr
, &saAttr
, 0))
1227 fatal (NILF
, _("CreatePipe() failed (e=%d)\n"), GetLastError());
1229 hProcess
= process_init_fd(hIn
, hChildOutWr
, hErr
);
1232 fatal (NILF
, _("windows32_openpipe (): process_init_fd() failed\n"));
1234 /* make sure that CreateProcess() has Path it needs */
1235 sync_Path_environment();
1237 if (!process_begin(hProcess
, command_argv
, envp
, command_argv
[0], NULL
)) {
1238 /* register process for wait */
1239 process_register(hProcess
);
1241 /* set the pid for returning to caller */
1242 *pid_p
= (int) hProcess
;
1244 /* set up to read data from child */
1245 pipedes
[0] = _open_osfhandle((long) hChildOutRd
, O_RDONLY
);
1247 /* this will be closed almost right away */
1248 pipedes
[1] = _open_osfhandle((long) hChildOutWr
, O_APPEND
);
1250 /* reap/cleanup the failed process */
1251 process_cleanup(hProcess
);
1253 /* close handles which were duplicated, they weren't used */
1257 /* close pipe handles, they won't be used */
1258 CloseHandle(hChildOutRd
);
1259 CloseHandle(hChildOutWr
);
1261 /* set status for return */
1262 pipedes
[0] = pipedes
[1] = -1;
1271 msdos_openpipe (int* pipedes
, int *pidp
, char *text
)
1274 /* MSDOS can't fork, but it has `popen'. */
1275 struct variable
*sh
= lookup_variable ("SHELL", 5);
1277 extern int dos_command_running
, dos_status
;
1279 /* Make sure not to bother processing an empty line. */
1280 while (isblank (*text
))
1287 char buf
[PATH_MAX
+ 7];
1288 /* This makes sure $SHELL value is used by $(shell), even
1289 though the target environment is not passed to it. */
1290 sprintf (buf
, "SHELL=%s", sh
->value
);
1296 dos_command_running
= 1;
1298 /* If dos_status becomes non-zero, it means the child process
1299 was interrupted by a signal, like SIGINT or SIGQUIT. See
1300 fatal_error_signal in commands.c. */
1301 fpipe
= popen (text
, "rt");
1302 dos_command_running
= 0;
1303 if (!fpipe
|| dos_status
)
1309 else if (errno
== 0)
1311 shell_function_completed
= -1;
1315 pipedes
[0] = fileno (fpipe
);
1316 *pidp
= 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
1318 shell_function_completed
= 1;
1325 Do shell spawning, with the naughty bits for different OSes.
1330 /* VMS can't do $(shell ...) */
1331 #define func_shell 0
1336 func_shell (o
, argv
, funcname
)
1339 const char *funcname
;
1341 char* batch_filename
= NULL
;
1347 char **command_argv
;
1354 /* Construct the argument list. */
1355 command_argv
= construct_command_argv (argv
[0],
1356 (char **) NULL
, (struct file
*) 0,
1358 if (command_argv
== 0)
1362 /* Using a target environment for `shell' loses in cases like:
1363 export var = $(shell echo foobie)
1364 because target_environment hits a loop trying to expand $(var)
1365 to put it in the environment. This is even more confusing when
1366 var was not explicitly exported, but just appeared in the
1367 calling environment. */
1371 /* For error messages. */
1372 if (reading_file
!= 0)
1374 error_prefix
= (char *) alloca (strlen(reading_file
->filenm
)+11+4);
1375 sprintf (error_prefix
,
1376 "%s:%lu: ", reading_file
->filenm
, reading_file
->lineno
);
1382 windows32_openpipe (pipedes
, &pid
, command_argv
, envp
);
1384 if (pipedes
[0] < 0) {
1385 /* open of the pipe failed, mark as failed execution */
1386 shell_function_completed
= -1;
1390 #else /* WINDOWS32 */
1393 fpipe
= msdos_openpipe (pipedes
, &pid
, argv
[0]);
1396 perror_with_name (error_prefix
, "pipe");
1400 if (pipe (pipedes
) < 0)
1402 perror_with_name (error_prefix
, "pipe");
1408 perror_with_name (error_prefix
, "fork");
1410 child_execute_job (0, pipedes
[1], command_argv
, envp
);
1412 # endif /* ! __MSDOS__ */
1414 #endif /* WINDOWS32 */
1416 /* We are the parent. */
1419 unsigned int maxlen
;
1422 /* Record the PID for reap_children. */
1423 shell_function_pid
= pid
;
1425 shell_function_completed
= 0;
1427 /* Free the storage only the child needed. */
1428 free (command_argv
[0]);
1429 free ((char *) command_argv
);
1431 /* Close the write side of the pipe. */
1432 (void) close (pipedes
[1]);
1435 /* Set up and read from the pipe. */
1438 buffer
= (char *) xmalloc (maxlen
+ 1);
1440 /* Read from the pipe until it gets EOF. */
1447 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
1451 cc
= read (pipedes
[0], &buffer
[i
], maxlen
- i
);
1455 while (cc
> 0 || EINTR_SET
);
1457 /* Close the read side of the pipe. */
1460 (void) pclose (fpipe
);
1462 (void) close (pipedes
[0]);
1465 /* Loop until child_handler sets shell_function_completed
1466 to the status of our child shell. */
1467 while (shell_function_completed
== 0)
1468 reap_children (1, 0);
1470 if (batch_filename
) {
1471 DB (DB_VERBOSE
, (_("Cleaning up temporary batch file %s\n"),
1473 remove(batch_filename
);
1474 free(batch_filename
);
1476 shell_function_pid
= 0;
1478 /* The child_handler function will set shell_function_completed
1479 to 1 when the child dies normally, or to -1 if it
1480 dies with status 127, which is most likely an exec fail. */
1482 if (shell_function_completed
== -1)
1484 /* This most likely means that the execvp failed,
1485 so we should just write out the error message
1486 that came in over the pipe from the child. */
1487 fputs (buffer
, stderr
);
1492 /* The child finished normally. Replace all
1493 newlines in its output with spaces, and put
1494 that in the variable output buffer. */
1495 fold_newlines (buffer
, &i
);
1496 o
= variable_buffer_output (o
, buffer
, i
);
1507 /* Do the Amiga version of func_shell. */
1510 func_shell (char *o
, char **argv
, const char *funcname
)
1512 /* Amiga can't fork nor spawn, but I can start a program with
1513 redirection of my choice. However, this means that we
1514 don't have an opportunity to reopen stdout to trap it. Thus,
1515 we save our own stdout onto a new descriptor and dup a temp
1516 file's descriptor onto our stdout temporarily. After we
1517 spawn the shell program, we dup our own stdout back to the
1518 stdout descriptor. The buffer reading is the same as above,
1519 except that we're now reading from a file. */
1521 #include <dos/dos.h>
1522 #include <proto/dos.h>
1525 char tmp_output
[FILENAME_MAX
];
1526 unsigned int maxlen
= 200;
1528 char * buffer
, * ptr
;
1531 char* batch_filename
= NULL
;
1533 /* Construct the argument list. */
1534 command_argv
= construct_command_argv (argv
[0], (char **) NULL
,
1535 (struct file
*) 0, &batch_filename
);
1536 if (command_argv
== 0)
1540 strcpy (tmp_output
, "t:MakeshXXXXXXXX");
1541 mktemp (tmp_output
);
1542 child_stdout
= Open (tmp_output
, MODE_NEWFILE
);
1544 for (aptr
=command_argv
; *aptr
; aptr
++)
1545 len
+= strlen (*aptr
) + 1;
1547 buffer
= xmalloc (len
+ 1);
1550 for (aptr
=command_argv
; *aptr
; aptr
++)
1552 strcpy (ptr
, *aptr
);
1553 ptr
+= strlen (ptr
) + 1;
1560 Execute (buffer
, NULL
, child_stdout
);
1563 Close (child_stdout
);
1565 child_stdout
= Open (tmp_output
, MODE_OLDFILE
);
1567 buffer
= xmalloc (maxlen
);
1574 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
1577 cc
= Read (child_stdout
, &buffer
[i
], maxlen
- i
);
1582 Close (child_stdout
);
1584 fold_newlines (buffer
, &i
);
1585 o
= variable_buffer_output (o
, buffer
, i
);
1595 equality. Return is string-boolean, ie, the empty string is false.
1598 func_eq (char* o
, char **argv
, char *funcname
)
1600 int result
= ! strcmp (argv
[0], argv
[1]);
1601 o
= variable_buffer_output (o
, result
? "1" : "", result
);
1607 string-boolean not operator.
1610 func_not (char* o
, char **argv
, char *funcname
)
1614 while (isspace ((unsigned char)*s
))
1617 o
= variable_buffer_output (o
, result
? "1" : "", result
);
1623 #define STRING_SIZE_TUPLE(_s) (_s), (sizeof(_s)-1)
1625 /* Lookup table for builtin functions.
1627 This doesn't have to be sorted; we use a straight lookup. We might gain
1628 some efficiency by moving most often used functions to the start of the
1631 If MAXIMUM_ARGS is 0, that means there is no maximum and all
1632 comma-separated values are treated as arguments.
1634 EXPAND_ARGS means that all arguments should be expanded before invocation.
1635 Functions that do namespace tricks (foreach) don't automatically expand. */
1637 static char *func_call
PARAMS((char *o
, char **argv
, const char *funcname
));
1640 static struct function_table_entry function_table
[] =
1642 /* Name/size */ /* MIN MAX EXP? Function */
1643 { STRING_SIZE_TUPLE("addprefix"), 2, 2, 1, func_addsuffix_addprefix
},
1644 { STRING_SIZE_TUPLE("addsuffix"), 2, 2, 1, func_addsuffix_addprefix
},
1645 { STRING_SIZE_TUPLE("basename"), 1, 1, 1, func_basename_dir
},
1646 { STRING_SIZE_TUPLE("dir"), 1, 1, 1, func_basename_dir
},
1647 { STRING_SIZE_TUPLE("notdir"), 1, 1, 1, func_notdir_suffix
},
1648 { STRING_SIZE_TUPLE("subst"), 3, 3, 1, func_subst
},
1649 { STRING_SIZE_TUPLE("suffix"), 1, 1, 1, func_notdir_suffix
},
1650 { STRING_SIZE_TUPLE("filter"), 2, 2, 1, func_filter_filterout
},
1651 { STRING_SIZE_TUPLE("filter-out"), 2, 2, 1, func_filter_filterout
},
1652 { STRING_SIZE_TUPLE("findstring"), 2, 2, 1, func_findstring
},
1653 { STRING_SIZE_TUPLE("firstword"), 1, 1, 1, func_firstword
},
1654 { STRING_SIZE_TUPLE("join"), 2, 2, 1, func_join
},
1655 { STRING_SIZE_TUPLE("patsubst"), 3, 3, 1, func_patsubst
},
1656 { STRING_SIZE_TUPLE("shell"), 1, 1, 1, func_shell
},
1657 { STRING_SIZE_TUPLE("sort"), 1, 1, 1, func_sort
},
1658 { STRING_SIZE_TUPLE("strip"), 1, 1, 1, func_strip
},
1659 { STRING_SIZE_TUPLE("wildcard"), 1, 1, 1, func_wildcard
},
1660 { STRING_SIZE_TUPLE("word"), 2, 2, 1, func_word
},
1661 { STRING_SIZE_TUPLE("wordlist"), 3, 3, 1, func_wordlist
},
1662 { STRING_SIZE_TUPLE("words"), 1, 1, 1, func_words
},
1663 { STRING_SIZE_TUPLE("origin"), 1, 1, 1, func_origin
},
1664 { STRING_SIZE_TUPLE("foreach"), 3, 3, 0, func_foreach
},
1665 { STRING_SIZE_TUPLE("call"), 1, 0, 0, func_call
},
1666 { STRING_SIZE_TUPLE("error"), 1, 1, 1, func_error
},
1667 { STRING_SIZE_TUPLE("warning"), 1, 1, 1, func_error
},
1668 { STRING_SIZE_TUPLE("if"), 2, 3, 0, func_if
},
1670 { STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq
},
1671 { STRING_SIZE_TUPLE("not"), 1, 1, 1, func_not
},
1677 /* These must come after the definition of function_table[]. */
1680 expand_builtin_function (o
, argc
, argv
, entry_p
)
1684 struct function_table_entry
*entry_p
;
1686 if (argc
< entry_p
->minimum_args
)
1687 fatal (reading_file
,
1688 _("Insufficient number of arguments (%d) to function `%s'"),
1689 argc
, entry_p
->name
);
1691 if (!entry_p
->func_ptr
)
1692 fatal (reading_file
, _("Unimplemented on this platform: function `%s'"),
1695 return entry_p
->func_ptr (o
, argv
, entry_p
->name
);
1698 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1699 opening ( or { and is not null-terminated. If a function invocation
1700 is found, expand it into the buffer at *OP, updating *OP, incrementing
1701 *STRINGP past the reference and returning nonzero. If not, return zero. */
1704 handle_function (op
, stringp
)
1708 const struct function_table_entry
*entry_p
;
1709 char openparen
= (*stringp
)[0];
1710 char closeparen
= openparen
== '(' ? ')' : '}';
1715 char **argv
, **argvp
;
1720 entry_p
= lookup_function (function_table
, beg
);
1725 /* We found a builtin function. Find the beginning of its arguments (skip
1726 whitespace after the name). */
1728 beg
= next_token (beg
+ entry_p
->len
);
1730 /* Find the end of the function invocation, counting nested use of
1731 whichever kind of parens we use. Since we're looking, count commas
1732 to get a rough estimate of how many arguments we might have. The
1733 count might be high, but it'll never be low. */
1735 for (nargs
=1, end
=beg
; *end
!= '\0'; ++end
)
1738 else if (*end
== openparen
)
1740 else if (*end
== closeparen
&& --count
< 0)
1744 fatal (reading_file
,
1745 _("unterminated call to function `%s': missing `%c'"),
1746 entry_p
->name
, closeparen
);
1750 /* Get some memory to store the arg pointers. */
1751 argvp
= argv
= (char **) alloca (sizeof(char *) * (nargs
+ 2));
1753 /* Chop the string into arguments, then a nul. As soon as we hit
1754 MAXIMUM_ARGS (if it's >0) assume the rest of the string is part of the
1757 If we're expanding, store pointers to the expansion of each one. If
1758 not, make a duplicate of the string and point into that, nul-terminating
1761 if (!entry_p
->expand_args
)
1763 int len
= end
- beg
;
1765 p
= xmalloc (len
+1);
1766 memcpy (p
, beg
, len
);
1774 for (p
=beg
, nargs
=0; p
< end
; ++argvp
)
1780 if (nargs
== entry_p
->maximum_args
1781 || (! (next
= find_next_argument (openparen
, closeparen
, p
, end
))))
1784 if (entry_p
->expand_args
)
1785 *argvp
= expand_argument (p
, next
);
1796 /* Finally! Run the function... */
1797 *op
= expand_builtin_function (*op
, nargs
, argv
, entry_p
);
1800 if (entry_p
->expand_args
)
1801 for (argvp
=argv
; *argvp
!= 0; ++argvp
)
1810 /* User-defined functions. Expand the first argument as either a builtin
1811 function or a make variable, in the context of the rest of the arguments
1812 assigned to $1, $2, ... $N. $0 is the name of the function. */
1815 func_call (o
, argv
, funcname
)
1818 const char *funcname
;
1825 const struct function_table_entry
*entry_p
;
1827 fname
= expand_argument (argv
[0], NULL
);
1829 /* There is no way to define a variable with a space in the name, so strip
1830 leading and trailing whitespace as a favor to the user. */
1832 while (*cp
!= '\0' && isspace ((unsigned char)*cp
))
1836 cp
+= strlen(cp
) - 1;
1837 while (cp
> argv
[0] && isspace ((unsigned char)*cp
))
1841 /* Calling nothing is a no-op */
1842 if (*argv
[0] == '\0')
1848 /* Are we invoking a builtin function? */
1850 entry_p
= lookup_function (function_table
, argv
[0]);
1858 /* How many arguments do we have? */
1859 for (i
=0; argv
[i
+1]; ++i
)
1862 /* If we need to expand arguments, do it now. */
1863 if (entry_p
->expand_args
)
1864 for (av
=argv
+1; *av
; ++av
)
1865 *av
= expand_argument (*av
, NULL
);
1867 o
= expand_builtin_function (o
, i
, argv
+1, entry_p
);
1869 /* What we expanded we must free... */
1870 if (entry_p
->expand_args
)
1871 for (av
=argv
+1; *av
; ++av
)
1877 /* Not a builtin, so the first argument is the name of a variable to be
1878 expanded and interpreted as a function. Create the variable
1880 flen
= strlen (argv
[0]);
1882 body
= alloca (flen
+ 4);
1885 memcpy (body
+ 2, fname
, flen
);
1887 body
[flen
+3] = '\0';
1889 /* Set up arguments $(1) .. $(N). $(0) is the function name. */
1891 push_new_variable_scope ();
1893 for (i
=0; *argv
; ++i
, ++argv
)
1897 sprintf (num
, "%d", i
);
1898 define_variable (num
, strlen (num
), *argv
, o_automatic
, 1);
1901 /* Expand the body in the context of the arguments, adding the result to
1902 the variable buffer. */
1904 o
= variable_expand_string (o
, body
, flen
+3);
1906 pop_variable_scope ();
1909 return o
+ strlen(o
);