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) == ']')
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
);
713 return (end
- beg
>= 0);
717 check_numeric (s
, message
)
722 fatal (reading_file
, message
);
728 func_word(o
, argv
, funcname
)
731 const char *funcname
;
737 /* Check the first argument. */
738 check_numeric (argv
[0], _("non-numeric first argument to `word' function"));
742 fatal (reading_file
, _("the `word' function takes a positive index argument"));
746 while ((p
= find_next_token (&end_p
, 0)) != 0)
751 o
= variable_buffer_output (o
, p
, end_p
- p
);
757 func_wordlist (o
, argv
, funcname
)
760 const char *funcname
;
765 /* Check the first argument. */
766 check_numeric (argv
[0],
767 _("non-numeric first argument to `wordlist' function"));
769 check_numeric (argv
[1],
770 _("non-numeric second argument to `wordlist' function"));
777 char *end_p
= argv
[2];
779 int start
= (i
< j
) ? i
: j
;
787 while (((p
= find_next_token (&end_p
, 0)) != 0) && --start
)
791 while (--count
&& (find_next_token (&end_p
, 0) != 0))
793 o
= variable_buffer_output (o
, p
, end_p
- p
);
800 func_findstring(o
, argv
, funcname
)
803 const char *funcname
;
805 /* Find the first occurrence of the first string in the second. */
806 int i
= strlen (argv
[0]);
807 if (sindex (argv
[1], 0, argv
[0], i
) != 0)
808 o
= variable_buffer_output (o
, argv
[0], i
);
814 func_foreach (o
, argv
, funcname
)
817 const char *funcname
;
819 /* expand only the first two. */
820 char *varname
= expand_argument (argv
[0], argv
[1] -1);
821 char *list
= expand_argument (argv
[1], argv
[2] -1);
822 char *body
= savestring (argv
[2], argv
[3] - argv
[2] -1 );
825 char *list_iterator
= list
;
827 register struct variable
*var
=0;
830 push_new_variable_scope ();
831 var
= define_variable (varname
, strlen (varname
), "", o_automatic
, 0);
833 /* loop through LIST, put the value in VAR and expand BODY */
834 while ((p
= find_next_token (&list_iterator
, &len
)) != 0)
843 var
->value
= (char *) xstrdup ((char*) p
);
847 result
= allocated_variable_expand (body
);
849 o
= variable_buffer_output (o
, result
, strlen (result
));
850 o
= variable_buffer_output (o
, " ", 1);
856 /* Kill the last space. */
859 pop_variable_scope ();
875 func_filter_filterout (o
, argv
, funcname
)
878 const char *funcname
;
880 struct a_word
*wordhead
=0;
881 struct a_word
*wordtail
=0;
883 int is_filter
= streq (funcname
, "filter");
884 char *patterns
= argv
[0];
889 char *word_iterator
= argv
[1];
891 /* Chop ARGV[1] up into words and then run each pattern through. */
892 while ((p
= find_next_token (&word_iterator
, &len
)) != 0)
894 struct a_word
*w
= (struct a_word
*)alloca(sizeof(struct a_word
));
901 if (*word_iterator
!= '\0')
910 struct a_word
*wp
=0;
911 char *pat_iterator
= patterns
;
917 /* Run each pattern through the words, killing words. */
918 while ((p
= find_next_token (&pat_iterator
, &len
)) != 0)
924 percent
= find_percent (p
);
925 for (wp
= wordhead
; wp
!= 0; wp
= wp
->next
)
926 wp
->matched
|= (percent
== 0 ? streq (p
, wp
->str
)
927 : pattern_matches (p
, percent
, wp
->str
));
932 /* Output the words that matched (or didn't, for filter-out). */
933 for (wp
= wordhead
; wp
!= 0; wp
= wp
->next
)
934 if (is_filter
? wp
->matched
: !wp
->matched
)
936 o
= variable_buffer_output (o
, wp
->str
, strlen (wp
->str
));
937 o
= variable_buffer_output (o
, " ", 1);
942 /* Kill the last space. */
951 func_strip(o
, argv
, funcname
)
954 const char *funcname
;
967 for (i
=0; *p
!= '\0' && !isspace(*p
); ++p
, ++i
)
971 o
= variable_buffer_output (o
, word_start
, i
);
972 o
= variable_buffer_output (o
, " ", 1);
977 /* Kill the last space. */
983 Print a warning or fatal message.
986 func_error (o
, argv
, funcname
)
989 const char *funcname
;
995 /* The arguments will be broken on commas. Rather than create yet
996 another special case where function arguments aren't broken up,
997 just create a format string that puts them back together. */
998 for (len
=0, argvp
=argv
; *argvp
!= 0; ++argvp
)
999 len
+= strlen(*argvp
) + 2;
1001 p
= msg
= alloca (len
+ 1);
1003 for (argvp
=argv
; argvp
[1] != 0; ++argvp
)
1006 p
+= strlen(*argvp
);
1012 if (*funcname
== 'e')
1013 fatal (reading_file
, "%s", msg
);
1015 /* The warning function expands to the empty string. */
1016 error (reading_file
, "%s", msg
);
1023 chop argv[0] into words, and sort them.
1026 func_sort (o
, argv
, funcname
)
1029 const char *funcname
;
1033 register int wordi
= 0;
1035 /* Chop ARGV[0] into words and put them in WORDS. */
1041 while ((p
= find_next_token (&t
, &len
)) != 0)
1043 if (wordi
>= nwords
- 1)
1045 nwords
= 2* nwords
+ 5;
1046 words
= (char **) xrealloc ((char *) words
,
1047 nwords
* sizeof (char *));
1049 words
[wordi
++] = savestring (p
, len
);
1055 /* Now sort the list of words. */
1056 qsort ((char *) words
, wordi
, sizeof (char *), alpha_compare
);
1058 /* Now write the sorted list. */
1059 for (i
= 0; i
< wordi
; ++i
)
1061 len
= strlen (words
[i
]);
1062 if (i
== wordi
- 1 || strlen (words
[i
+ 1]) != len
1063 || strcmp (words
[i
], words
[i
+ 1]))
1065 o
= variable_buffer_output (o
, words
[i
], len
);
1066 o
= variable_buffer_output (o
, " ", 1);
1070 /* Kill the last space. */
1079 func_wildcard(o
, argv
, funcname
)
1082 const char *funcname
;
1086 o
= wildcard_expansion (argv
[0], o
);
1088 char *p
= string_glob (argv
[0]);
1089 o
= variable_buffer_output (o
, p
, strlen (p
));
1095 \r is replaced on UNIX as well. Is this desirable?
1098 fold_newlines (buffer
, length
)
1104 char *last_nonnl
= buffer
-1;
1106 for (; *src
!= '\0'; ++src
)
1108 if (src
[0] == '\r' && src
[1] == '\n')
1120 *(++last_nonnl
) = '\0';
1121 *length
= last_nonnl
- buffer
;
1126 int shell_function_pid
= 0, shell_function_completed
;
1132 #include <windows.h>
1134 #include "sub_proc.h"
1138 windows32_openpipe (int *pipedes
, int *pid_p
, char **command_argv
, char **envp
)
1140 SECURITY_ATTRIBUTES saAttr
;
1148 saAttr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
1149 saAttr
.bInheritHandle
= TRUE
;
1150 saAttr
.lpSecurityDescriptor
= NULL
;
1152 if (DuplicateHandle(GetCurrentProcess(),
1153 GetStdHandle(STD_INPUT_HANDLE
),
1154 GetCurrentProcess(),
1158 DUPLICATE_SAME_ACCESS
) == FALSE
) {
1159 fatal (NILF
, _("create_child_process: DuplicateHandle(In) failed (e=%d)\n"),
1163 if (DuplicateHandle(GetCurrentProcess(),
1164 GetStdHandle(STD_ERROR_HANDLE
),
1165 GetCurrentProcess(),
1169 DUPLICATE_SAME_ACCESS
) == FALSE
) {
1170 fatal (NILF
, _("create_child_process: DuplicateHandle(Err) failed (e=%d)\n"),
1174 if (!CreatePipe(&hChildOutRd
, &hChildOutWr
, &saAttr
, 0))
1175 fatal (NILF
, _("CreatePipe() failed (e=%d)\n"), GetLastError());
1179 hProcess
= process_init_fd(hIn
, hChildOutWr
, hErr
);
1182 fatal (NILF
, _("windows32_openpipe (): process_init_fd() failed\n"));
1185 process_register(hProcess
);
1187 /* make sure that CreateProcess() has Path it needs */
1188 sync_Path_environment();
1190 if (!process_begin(hProcess
, command_argv
, envp
, command_argv
[0], NULL
))
1191 *pid_p
= (int) hProcess
;
1193 fatal (NILF
, _("windows32_openpipe (): unable to launch process (e=%d)\n"),
1194 process_last_err(hProcess
));
1196 /* set up to read data from child */
1197 pipedes
[0] = _open_osfhandle((long) hChildOutRd
, O_RDONLY
);
1199 /* this will be closed almost right away */
1200 pipedes
[1] = _open_osfhandle((long) hChildOutWr
, O_APPEND
);
1207 msdos_openpipe (int* pipedes
, int *pidp
, char *text
)
1210 /* MSDOS can't fork, but it has `popen'. */
1211 struct variable
*sh
= lookup_variable ("SHELL", 5);
1213 extern int dos_command_running
, dos_status
;
1215 /* Make sure not to bother processing an empty line. */
1216 while (isblank (*text
))
1223 char buf
[PATH_MAX
+ 7];
1224 /* This makes sure $SHELL value is used by $(shell), even
1225 though the target environment is not passed to it. */
1226 sprintf (buf
, "SHELL=%s", sh
->value
);
1232 dos_command_running
= 1;
1234 /* If dos_status becomes non-zero, it means the child process
1235 was interrupted by a signal, like SIGINT or SIGQUIT. See
1236 fatal_error_signal in commands.c. */
1237 fpipe
= popen (text
, "rt");
1238 dos_command_running
= 0;
1239 if (!fpipe
|| dos_status
)
1245 else if (errno
== 0)
1247 shell_function_completed
= -1;
1251 pipedes
[0] = fileno (fpipe
);
1252 *pidp
= 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
1254 shell_function_completed
= 1;
1261 Do shell spawning, with the naughty bits for different OSes.
1266 /* VMS can't do $(shell ...) */
1267 #define func_shell 0
1272 func_shell (o
, argv
, funcname
)
1275 const char *funcname
;
1277 char* batch_filename
= NULL
;
1283 char **command_argv
;
1290 /* Construct the argument list. */
1291 command_argv
= construct_command_argv (argv
[0],
1292 (char **) NULL
, (struct file
*) 0,
1294 if (command_argv
== 0)
1298 /* Using a target environment for `shell' loses in cases like:
1299 export var = $(shell echo foobie)
1300 because target_environment hits a loop trying to expand $(var)
1301 to put it in the environment. This is even more confusing when
1302 var was not explicitly exported, but just appeared in the
1303 calling environment. */
1307 /* For error messages. */
1308 if (reading_file
!= 0)
1310 error_prefix
= (char *) alloca (strlen(reading_file
->filenm
)+100);
1311 sprintf (error_prefix
,
1312 "%s:%lu: ", reading_file
->filenm
, reading_file
->lineno
);
1318 windows32_openpipe (pipedes
, &pid
, command_argv
, envp
);
1319 #else /* WINDOWS32 */
1322 fpipe
= msdos_openpipe (pipedes
, &pid
, argv
[0]);
1325 perror_with_name (error_prefix
, "pipe");
1329 if (pipe (pipedes
) < 0)
1331 perror_with_name (error_prefix
, "pipe");
1337 perror_with_name (error_prefix
, "fork");
1339 child_execute_job (0, pipedes
[1], command_argv
, envp
);
1341 # endif /* ! __MSDOS__ */
1343 #endif /* WINDOWS32 */
1345 /* We are the parent. */
1348 unsigned int maxlen
;
1351 /* Record the PID for reap_children. */
1352 shell_function_pid
= pid
;
1354 shell_function_completed
= 0;
1356 /* Free the storage only the child needed. */
1357 free (command_argv
[0]);
1358 free ((char *) command_argv
);
1360 /* Close the write side of the pipe. */
1361 (void) close (pipedes
[1]);
1364 /* Set up and read from the pipe. */
1367 buffer
= (char *) xmalloc (maxlen
+ 1);
1369 /* Read from the pipe until it gets EOF. */
1376 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
1380 cc
= read (pipedes
[0], &buffer
[i
], maxlen
- i
);
1384 while (cc
> 0 || EINTR_SET
);
1386 /* Close the read side of the pipe. */
1389 (void) pclose (fpipe
);
1391 (void) close (pipedes
[0]);
1394 /* Loop until child_handler sets shell_function_completed
1395 to the status of our child shell. */
1396 while (shell_function_completed
== 0)
1397 reap_children (1, 0);
1399 if (batch_filename
) {
1401 printf(_("Cleaning up temporary batch file %s\n"), batch_filename
);
1402 remove(batch_filename
);
1403 free(batch_filename
);
1405 shell_function_pid
= 0;
1407 /* The child_handler function will set shell_function_completed
1408 to 1 when the child dies normally, or to -1 if it
1409 dies with status 127, which is most likely an exec fail. */
1411 if (shell_function_completed
== -1)
1413 /* This most likely means that the execvp failed,
1414 so we should just write out the error message
1415 that came in over the pipe from the child. */
1416 fputs (buffer
, stderr
);
1421 /* The child finished normally. Replace all
1422 newlines in its output with spaces, and put
1423 that in the variable output buffer. */
1424 fold_newlines (buffer
, &i
);
1425 o
= variable_buffer_output (o
, buffer
, i
);
1436 /* Do the Amiga version of func_shell. */
1439 func_shell (char *o
, char **argv
, const char *funcname
)
1441 /* Amiga can't fork nor spawn, but I can start a program with
1442 redirection of my choice. However, this means that we
1443 don't have an opportunity to reopen stdout to trap it. Thus,
1444 we save our own stdout onto a new descriptor and dup a temp
1445 file's descriptor onto our stdout temporarily. After we
1446 spawn the shell program, we dup our own stdout back to the
1447 stdout descriptor. The buffer reading is the same as above,
1448 except that we're now reading from a file. */
1450 #include <dos/dos.h>
1451 #include <proto/dos.h>
1454 char tmp_output
[FILENAME_MAX
];
1455 unsigned int maxlen
= 200;
1457 char * buffer
, * ptr
;
1460 char* batch_filename
= NULL
;
1462 /* Construct the argument list. */
1463 command_argv
= construct_command_argv (argv
[0], (char **) NULL
,
1464 (struct file
*) 0, &batch_filename
);
1465 if (command_argv
== 0)
1469 strcpy (tmp_output
, "t:MakeshXXXXXXXX");
1470 mktemp (tmp_output
);
1471 child_stdout
= Open (tmp_output
, MODE_NEWFILE
);
1473 for (aptr
=command_argv
; *aptr
; aptr
++)
1474 len
+= strlen (*aptr
) + 1;
1476 buffer
= xmalloc (len
+ 1);
1479 for (aptr
=command_argv
; *aptr
; aptr
++)
1481 strcpy (ptr
, *aptr
);
1482 ptr
+= strlen (ptr
) + 1;
1489 Execute (buffer
, NULL
, child_stdout
);
1492 Close (child_stdout
);
1494 child_stdout
= Open (tmp_output
, MODE_OLDFILE
);
1496 buffer
= xmalloc (maxlen
);
1503 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
1506 cc
= Read (child_stdout
, &buffer
[i
], maxlen
- i
);
1511 Close (child_stdout
);
1513 fold_newlines (buffer
, &i
);
1514 o
= variable_buffer_output (o
, buffer
, i
);
1524 equality. Return is string-boolean, ie, the empty string is false.
1527 func_eq (char* o
, char **argv
, char *funcname
)
1529 int result
= ! strcmp (argv
[0], argv
[1]);
1530 o
= variable_buffer_output (o
, result
? "1" : "", result
);
1536 string-boolean not operator.
1539 func_not (char* o
, char **argv
, char *funcname
)
1543 while (isspace (*s
))
1546 o
= variable_buffer_output (o
, result
? "1" : "", result
);
1553 This is an experimental conditional function.
1557 $(if condition, true-part, false-part)
1559 This is fully not consistent with make's syntax, but more in line
1560 with `normal' programming languages.
1564 - CONDITION is false iff it evaluates to an empty string. White
1565 space before and after condition are stripped before evaluation.
1567 - If CONDITION is true, then TRUE-PART is evaluated, otherwise
1568 FALSE-PART is evaluated. Because only one of the two PARTs is
1569 evaluated, you can use $(if ) to create side-effects with the
1574 func_if (char* o
, char **argv
, char *funcname
)
1576 char *begp
= argv
[0];
1577 char *endp
= argv
[1]-2;
1581 strip_whitespace (&begp
, &endp
);
1583 expansion
= expand_argument (begp
, endp
+ 1);
1586 ? strlen (expansion
)
1592 expansion
= expand_argument (argv
[1 + result
], argv
[2+result
] -1);
1593 o
= variable_buffer_output (o
, expansion
, strlen (expansion
));
1600 #define STRING_SIZE_TUPLE(_s) (_s), (sizeof(_s)-1)
1602 /* Lookup table for builtin functions.
1604 This doesn't have to be sorted; we use a straight lookup. We might gain
1605 some efficiency by moving most often used functions to the start of the
1608 If REQUIRED_ARGS is positive, the function takes exactly that many
1609 arguments. All subsequent text is included with the last argument. So,
1610 since $(sort a,b,c) takes only one argument, it will be the full string
1611 "a,b,c". If the value is negative, it's the minimum number of arguments.
1612 A function can have more, but if it has less an error is generated.
1614 EXPAND_ARGS means that all arguments should be expanded before invocation.
1615 Functions that do namespace tricks (foreach) don't automatically expand. */
1617 static char *func_call
PARAMS((char *o
, char **argv
, const char *funcname
));
1620 static struct function_table_entry function_table
[] =
1622 /* Name/size */ /* ARG EXP? Function */
1623 { STRING_SIZE_TUPLE("addprefix"), 2, 1, func_addsuffix_addprefix
},
1624 { STRING_SIZE_TUPLE("addsuffix"), 2, 1, func_addsuffix_addprefix
},
1625 { STRING_SIZE_TUPLE("basename"), 1, 1, func_basename_dir
},
1626 { STRING_SIZE_TUPLE("dir"), 1, 1, func_basename_dir
},
1627 { STRING_SIZE_TUPLE("notdir"), 1, 1, func_notdir_suffix
},
1628 { STRING_SIZE_TUPLE("subst"), 3, 1, func_subst
},
1629 { STRING_SIZE_TUPLE("suffix"), 1, 1, func_notdir_suffix
},
1630 { STRING_SIZE_TUPLE("filter"), 2, 1, func_filter_filterout
},
1631 { STRING_SIZE_TUPLE("filter-out"), 2, 1, func_filter_filterout
},
1632 { STRING_SIZE_TUPLE("findstring"), 2, 1, func_findstring
},
1633 { STRING_SIZE_TUPLE("firstword"), 1, 1, func_firstword
},
1634 { STRING_SIZE_TUPLE("join"), 2, 1, func_join
},
1635 { STRING_SIZE_TUPLE("patsubst"), 3, 1, func_patsubst
},
1636 { STRING_SIZE_TUPLE("shell"), 1, 1, func_shell
},
1637 { STRING_SIZE_TUPLE("sort"), 1, 1, func_sort
},
1638 { STRING_SIZE_TUPLE("strip"), 1, 1, func_strip
},
1639 { STRING_SIZE_TUPLE("wildcard"), 1, 1, func_wildcard
},
1640 { STRING_SIZE_TUPLE("word"), 2, 1, func_word
},
1641 { STRING_SIZE_TUPLE("wordlist"), 3, 1, func_wordlist
},
1642 { STRING_SIZE_TUPLE("words"), 1, 1, func_words
},
1643 { STRING_SIZE_TUPLE("origin"), 1, 1, func_origin
},
1644 { STRING_SIZE_TUPLE("foreach"), 3, 0, func_foreach
},
1645 { STRING_SIZE_TUPLE("call"), -1, 1, func_call
},
1646 { STRING_SIZE_TUPLE("error"), 1, 1, func_error
},
1647 { STRING_SIZE_TUPLE("warning"), 1, 1, func_error
},
1649 { STRING_SIZE_TUPLE("eq"), 2, 1, func_eq
},
1650 { STRING_SIZE_TUPLE("if"), 3, 0, func_if
},
1651 { STRING_SIZE_TUPLE("not"), 1, 1, func_not
},
1657 /* These must come after the definition of function_table[]. */
1660 expand_builtin_function (o
, argc
, argv
, entry_p
)
1664 struct function_table_entry
*entry_p
;
1666 int min
= (entry_p
->required_args
> 0
1667 ? entry_p
->required_args
1668 : -entry_p
->required_args
);
1671 fatal (reading_file
,
1672 _("Insufficient number of arguments (%d) to function `%s'"),
1673 argc
, entry_p
->name
);
1675 if (!entry_p
->func_ptr
)
1676 fatal (reading_file
, _("Unimplemented on this platform: function `%s'"),
1679 return entry_p
->func_ptr (o
, argv
, entry_p
->name
);
1682 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1683 opening ( or { and is not null-terminated. If a function invocation
1684 is found, expand it into the buffer at *OP, updating *OP, incrementing
1685 *STRINGP past the reference and returning nonzero. If not, return zero. */
1688 handle_function (op
, stringp
)
1692 const struct function_table_entry
*entry_p
;
1693 char openparen
= (*stringp
)[0];
1694 char closeparen
= openparen
== '(' ? ')' : '}';
1695 char *beg
= *stringp
+ 1;
1700 char **argv
, **argvp
;
1703 entry_p
= lookup_function (function_table
, beg
);
1708 /* We have found a call to a builtin function. Find the end of the
1709 arguments, and do the function. */
1711 endref
= beg
+ entry_p
->len
;
1713 /* Space after function name isn't part of the args. */
1714 p
= next_token (endref
);
1717 /* Find the end of the function invocation, counting nested use of
1718 whichever kind of parens we use. Since we're looking, count commas
1719 to get a rough estimate of how many arguments we might have. The
1720 count might be high, but it'll never be low. */
1722 for (nargs
=1; *p
!= '\0'; ++p
)
1725 else if (*p
== openparen
)
1727 else if (*p
== closeparen
&& --count
< 0)
1731 fatal (reading_file
,
1732 _("unterminated call to function `%s': missing `%c'"),
1733 entry_p
->name
, closeparen
);
1735 /* Get some memory to store the arg pointers. */
1736 argvp
= argv
= (char **) alloca (sizeof(char *) * (nargs
+ 2));
1738 /* Chop the string into arguments, then store the end pointer and a nul.
1739 If REQUIRED_ARGS is positive, as soon as we hit that many assume the
1740 rest of the string is part of the last argument. */
1743 while (entry_p
->required_args
< 0 || nargs
< entry_p
->required_args
)
1745 char *next
= find_next_argument (openparen
, closeparen
, *argvp
, p
);
1750 *(++argvp
) = next
+1;
1757 /* If we should expand, do it. */
1758 if (entry_p
->expand_args
)
1760 for (argvp
=argv
; argvp
[1] != 0; ++argvp
)
1761 *argvp
= expand_argument (*argvp
, argvp
[1]-1);
1763 /* end pointer doesn't make sense for expanded stuff. */
1767 /* Finally! Run the function... */
1768 *op
= expand_builtin_function (*op
, nargs
, argv
, entry_p
);
1770 /* If we allocated memory for the expanded args, free it again. */
1771 if (entry_p
->expand_args
)
1772 for (argvp
=argv
; *argvp
!= 0; ++argvp
)
1781 /* User-defined functions. Expand the first argument as either a builtin
1782 function or a make variable, in the context of the rest of the arguments
1783 assigned to $1, $2, ... $N. $0 is the name of the function. */
1786 func_call (o
, argv
, funcname
)
1789 const char *funcname
;
1795 const struct function_table_entry
*entry_p
;
1797 /* Calling nothing is a no-op. */
1798 if (*argv
[0] == '\0')
1801 /* There is no way to define a variable with a space in the name, so strip
1802 trailing whitespace as a favor to the user. */
1804 flen
= strlen (argv
[0]);
1805 fname
= argv
[0] + flen
- 1;
1806 while (isspace (*fname
))
1810 flen
= fname
- argv
[0] + 1;
1813 /* Are we invoking a builtin function? */
1815 entry_p
= lookup_function (function_table
, fname
);
1819 for (i
=0; argv
[i
+1]; ++i
)
1822 return expand_builtin_function (o
, i
, argv
+ 1, entry_p
);
1825 /* No, so the first argument is the name of a variable to be expanded and
1826 interpreted as a function. Create the variable reference. */
1827 body
= alloca (flen
+ 4);
1830 strcpy (body
+ 2, fname
);
1834 /* Set up arguments $(1) .. $(N). $(0) is the function name. */
1836 push_new_variable_scope ();
1838 for (i
=0; *argv
; ++i
, ++argv
)
1842 sprintf (num
, "%d", i
);
1843 define_variable (num
, strlen (num
), *argv
, o_automatic
, 0);
1846 /* Expand the body in the context of the arguments, adding the result to
1847 the variable buffer. */
1849 o
= variable_expand_string (o
, body
, flen
+3);
1851 pop_variable_scope ();
1853 return o
+ strlen(o
);