1 /* Variable function expansion for GNU Make.
2 Copyright (C) 1988, 89, 91, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
39 static char *string_glob
PARAMS ((char *line
));
41 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
42 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
43 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
44 nonzero, substitutions are done only on matches which are complete
45 whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
46 done only at the ends of whitespace-delimited words. */
49 subst_expand (o
, text
, subst
, replace
, slen
, rlen
, by_word
, suffix_only
)
52 char *subst
, *replace
;
53 unsigned int slen
, rlen
;
54 int by_word
, suffix_only
;
56 register char *t
= text
;
59 if (slen
== 0 && !by_word
&& !suffix_only
)
61 /* The first occurrence of "" in any string is its end. */
62 o
= variable_buffer_output (o
, t
, strlen (t
));
64 o
= variable_buffer_output (o
, replace
, rlen
);
70 if ((by_word
| suffix_only
) && slen
== 0)
71 /* When matching by words, the empty string should match
72 the end of each word, rather than the end of the whole text. */
73 p
= end_of_token (next_token (t
));
76 p
= sindex (t
, 0, subst
, slen
);
79 /* No more matches. Output everything left on the end. */
80 o
= variable_buffer_output (o
, t
, strlen (t
));
85 /* Output everything before this occurrence of the string to replace. */
87 o
= variable_buffer_output (o
, t
, p
- t
);
89 /* If we're substituting only by fully matched words,
90 or only at the ends of words, check that this case qualifies. */
92 && ((p
> t
&& !isblank (p
[-1]))
93 || (p
[slen
] != '\0' && !isblank (p
[slen
]))))
95 && (p
[slen
] != '\0' && !isblank (p
[slen
]))))
96 /* Struck out. Output the rest of the string that is
97 no longer to be replaced. */
98 o
= variable_buffer_output (o
, subst
, slen
);
100 /* Output the replacement string. */
101 o
= variable_buffer_output (o
, replace
, rlen
);
103 /* Advance T past the string to be replaced. */
105 } while (*t
!= '\0');
111 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
112 and replacing strings matching PATTERN with REPLACE.
113 If PATTERN_PERCENT is not nil, PATTERN has already been
114 run through find_percent, and PATTERN_PERCENT is the result.
115 If REPLACE_PERCENT is not nil, REPLACE has already been
116 run through find_percent, and REPLACE_PERCENT is the result. */
119 patsubst_expand (o
, text
, pattern
, replace
, pattern_percent
, replace_percent
)
122 register char *pattern
, *replace
;
123 register char *pattern_percent
, *replace_percent
;
125 unsigned int pattern_prepercent_len
, pattern_postpercent_len
;
126 unsigned int replace_prepercent_len
, replace_postpercent_len
;
131 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
132 will be collapsed before we call subst_expand if PATTERN has no %. */
133 if (replace_percent
== 0)
134 replace_percent
= find_percent (replace
);
135 if (replace_percent
!= 0)
137 /* Record the length of REPLACE before and after the % so
138 we don't have to compute these lengths more than once. */
139 replace_prepercent_len
= replace_percent
- replace
;
140 replace_postpercent_len
= strlen (replace_percent
+ 1);
143 /* We store the length of the replacement
144 so we only need to compute it once. */
145 replace_prepercent_len
= strlen (replace
);
147 if (pattern_percent
== 0)
148 pattern_percent
= find_percent (pattern
);
149 if (pattern_percent
== 0)
150 /* With no % in the pattern, this is just a simple substitution. */
151 return subst_expand (o
, text
, pattern
, replace
,
152 strlen (pattern
), strlen (replace
), 1, 0);
154 /* Record the length of PATTERN before and after the %
155 so we don't have to compute it more than once. */
156 pattern_prepercent_len
= pattern_percent
- pattern
;
157 pattern_postpercent_len
= strlen (pattern_percent
+ 1);
159 while ((t
= find_next_token (&text
, &len
)) != 0)
163 /* Is it big enough to match? */
164 if (len
< pattern_prepercent_len
+ pattern_postpercent_len
)
167 /* Does the prefix match? */
168 if (!fail
&& pattern_prepercent_len
> 0
170 || t
[pattern_prepercent_len
- 1] != pattern_percent
[-1]
171 || strncmp (t
+ 1, pattern
+ 1, pattern_prepercent_len
- 1)))
174 /* Does the suffix match? */
175 if (!fail
&& pattern_postpercent_len
> 0
176 && (t
[len
- 1] != pattern_percent
[pattern_postpercent_len
]
177 || t
[len
- pattern_postpercent_len
] != pattern_percent
[1]
178 || strncmp (&t
[len
- pattern_postpercent_len
],
179 &pattern_percent
[1], pattern_postpercent_len
- 1)))
183 /* It didn't match. Output the string. */
184 o
= variable_buffer_output (o
, t
, len
);
187 /* It matched. Output the replacement. */
189 /* Output the part of the replacement before the %. */
190 o
= variable_buffer_output (o
, replace
, replace_prepercent_len
);
192 if (replace_percent
!= 0)
194 /* Output the part of the matched string that
195 matched the % in the pattern. */
196 o
= variable_buffer_output (o
, t
+ pattern_prepercent_len
,
197 len
- (pattern_prepercent_len
198 + pattern_postpercent_len
));
199 /* Output the part of the replacement after the %. */
200 o
= variable_buffer_output (o
, replace_percent
+ 1,
201 replace_postpercent_len
);
205 /* Output a space, but not if the replacement is "". */
206 if (fail
|| replace_prepercent_len
> 0
207 || (replace_percent
!= 0 && len
+ replace_postpercent_len
> 0))
209 o
= variable_buffer_output (o
, " ", 1);
214 /* Kill the last space. */
220 /* Handle variable-expansion-time functions such as $(dir foo/bar) ==> foo/ */
222 /* These enumeration constants distinguish the
223 various expansion-time built-in functions. */
251 /* Greater than the length of any function name. */
252 #define MAXFUNCTIONLEN 11
254 /* The function names and lengths of names, for looking them up. */
260 enum function function
;
263 { "subst", 5, function_subst
},
264 { "addsuffix", 9, function_addsuffix
},
265 { "addprefix", 9, function_addprefix
},
266 { "dir", 3, function_dir
},
267 { "notdir", 6, function_notdir
},
268 { "suffix", 6, function_suffix
},
269 { "basename", 8, function_basename
},
270 { "wildcard", 8, function_wildcard
},
271 { "firstword", 9, function_firstword
},
272 { "word", 4, function_word
},
273 { "words", 5, function_words
},
274 { "findstring", 10, function_findstring
},
275 { "strip", 5, function_strip
},
276 { "join", 4, function_join
},
277 { "patsubst", 8, function_patsubst
},
278 { "filter", 6, function_filter
},
279 { "filter-out", 10, function_filter_out
},
280 { "foreach", 7, function_foreach
},
281 { "sort", 4, function_sort
},
282 { "origin", 6, function_origin
},
283 { "shell", 5, function_shell
},
284 { 0, 0, function_invalid
}
287 /* Return 1 if PATTERN matches WORD, 0 if not. */
290 pattern_matches (pattern
, percent
, word
)
291 register char *pattern
, *percent
, *word
;
293 unsigned int sfxlen
, wordlen
;
297 unsigned int len
= strlen (pattern
) + 1;
298 char *new = (char *) alloca (len
);
299 bcopy (pattern
, new, len
);
301 percent
= find_percent (pattern
);
303 return streq (pattern
, word
);
306 sfxlen
= strlen (percent
+ 1);
307 wordlen
= strlen (word
);
309 if (wordlen
< (percent
- pattern
) + sfxlen
310 || strncmp (pattern
, word
, percent
- pattern
))
313 return !strcmp (percent
+ 1, word
+ (wordlen
- sfxlen
));
316 int shell_function_pid
= 0, shell_function_completed
;
318 /* Perform the function specified by FUNCTION on the text at TEXT.
319 END is points to the end of the argument text (exclusive).
320 The output is written into VARIABLE_BUFFER starting at O. */
322 /* Note this absorbs a semicolon and is safe to use in conditionals. */
323 #define BADARGS(func) \
324 if (reading_filename != 0) \
325 makefile_fatal (reading_filename, *reading_lineno_ptr, \
326 "insufficient arguments to function `%s'", \
329 fatal ("insufficient arguments to function `%s'", func)
332 expand_function (o
, function
, text
, end
)
334 enum function function
;
342 char endparen
= *end
, startparen
= *end
== ')' ? '(' : '{';
350 #ifndef VMS /* not supported for vms yet */
354 SECURITY_ATTRIBUTES saAttr
;
369 /* Expand the command line. */
370 text
= expand_argument (text
, end
);
372 /* Construct the argument list. */
373 argv
= construct_command_argv (text
,
374 (char **) NULL
, (struct file
*) 0);
379 /* Using a target environment for `shell' loses in cases like:
380 export var = $(shell echo foobie)
381 because target_environment hits a loop trying to expand $(var)
382 to put it in the environment. This is even more confusing when
383 var was not explicitly exported, but just appeared in the
384 calling environment. */
388 /* Construct the environment. */
389 envp
= target_environment ((struct file
*) 0);
391 #endif /* Not Amiga. */
393 /* For error messages. */
394 if (reading_filename
!= 0)
396 error_prefix
= (char *) alloca (strlen (reading_filename
) + 100);
397 sprintf (error_prefix
,
398 "%s:%u: ", reading_filename
, *reading_lineno_ptr
);
403 #if !defined(__MSDOS__) && !defined(_AMIGA)
405 saAttr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
406 saAttr
.bInheritHandle
= TRUE
;
407 saAttr
.lpSecurityDescriptor
= NULL
;
409 if (DuplicateHandle(GetCurrentProcess(),
410 GetStdHandle(STD_INPUT_HANDLE
),
415 DUPLICATE_SAME_ACCESS
) == FALSE
) {
416 fatal("create_child_process: DuplicateHandle(In) failed (e=%d)\n",
419 if (DuplicateHandle(GetCurrentProcess(),
420 GetStdHandle(STD_ERROR_HANDLE
),
425 DUPLICATE_SAME_ACCESS
) == FALSE
) {
426 fatal("create_child_process: DuplicateHandle(Err) failed (e=%d)\n",
430 if (!CreatePipe(&hChildOutRd
, &hChildOutWr
, &saAttr
, 0))
431 fatal("CreatePipe() failed (e=%d)\n", GetLastError());
433 hProcess
= process_init_fd(hIn
, hChildOutWr
, hErr
);
436 fatal("expand_function: process_init_fd() failed\n");
438 process_register(hProcess
);
440 /* make sure that CreateProcess() has Path it needs */
441 sync_Path_environment();
443 if (!process_begin(hProcess
, argv
, envp
, argv
[0], NULL
))
444 pid
= (int) hProcess
;
446 fatal("expand_function: unable to launch process (e=%d)\n",
447 process_last_err(hProcess
));
449 /* set up to read data from child */
450 pipedes
[0] = _open_osfhandle((long) hChildOutRd
, O_RDONLY
);
452 /* this will be closed almost right away */
453 pipedes
[1] = _open_osfhandle((long) hChildOutWr
, O_APPEND
);
455 if (pipe (pipedes
) < 0)
457 perror_with_name (error_prefix
, "pipe");
463 perror_with_name (error_prefix
, "fork");
465 child_execute_job (0, pipedes
[1], argv
, envp
);
469 /* We are the parent. */
475 /* Free the storage only the child needed. */
477 free ((char *) argv
);
479 for (i
= 0; envp
[i
] != 0; ++i
)
481 free ((char *) envp
);
484 /* Record the PID for reap_children. */
485 shell_function_pid
= pid
;
486 shell_function_completed
= 0;
489 /* Set up and read from the pipe. */
492 buffer
= (char *) xmalloc (maxlen
+ 1);
494 /* Close the write side of the pipe. */
495 (void) close (pipedes
[1]);
497 /* Read from the pipe until it gets EOF. */
504 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
508 cc
= read (pipedes
[0], &buffer
[i
], maxlen
- i
);
513 while (cc
> 0 || errno
== EINTR
);
518 /* Close the read side of the pipe. */
519 (void) close (pipedes
[0]);
521 /* Loop until child_handler sets shell_function_completed
522 to the status of our child shell. */
523 while (shell_function_completed
== 0)
524 reap_children (1, 0);
526 shell_function_pid
= 0;
528 /* The child_handler function will set shell_function_completed
529 to 1 when the child dies normally, or to -1 if it
530 dies with status 127, which is most likely an exec fail. */
532 if (shell_function_completed
== -1)
534 /* This most likely means that the execvp failed,
535 so we should just write out the error message
536 that came in over the pipe from the child. */
537 fputs (buffer
, stderr
);
542 /* The child finished normally. Replace all
543 newlines in its output with spaces, and put
544 that in the variable output buffer. */
547 if (buffer
[i
- 1] == '\n')
552 while ((p
= index (p
, '\n')) != 0)
554 o
= variable_buffer_output (o
, buffer
, i
);
560 #else /* MSDOS or Amiga */
563 /* MS-DOS can't do fork, but it can do spawn. However, this
564 means that we don't have an opportunity to reopen stdout to
565 trap it. Thus, we save our own stdout onto a new descriptor
566 and dup a temp file's descriptor onto our stdout temporarily.
567 After we spawn the shell program, we dup our own stdout back
568 to the stdout descriptor. The buffer reading is the same as
569 above, except that we're now reading from a file. */
573 char tmp_output
[FILENAME_MAX
];
575 unsigned int maxlen
= 200;
579 strcpy (tmp_output
, "shXXXXXX");
581 child_stdout
= open (tmp_output
,
582 O_WRONLY
|O_CREAT
|O_TRUNC
|O_TEXT
, 0644);
583 save_stdout
= dup (1);
584 dup2 (child_stdout
, 1);
585 spawnvp (P_WAIT
, argv
[0], argv
);
586 dup2 (save_stdout
, 1);
587 close (child_stdout
);
590 child_stdout
= open (tmp_output
, O_RDONLY
|O_TEXT
, 0644);
592 buffer
= xmalloc (maxlen
);
599 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
602 cc
= read (child_stdout
, &buffer
[i
], maxlen
- i
);
607 close (child_stdout
);
612 if (buffer
[i
- 1] == '\n')
617 while ((p
= index (p
, '\n')) != 0)
619 o
= variable_buffer_output (o
, buffer
, i
);
625 /* Amiga can't fork nor spawn, but I can start a program with
626 redirection of my choice. The rest is the same as above. */
628 #include <proto/dos.h>
631 char tmp_output
[FILENAME_MAX
];
632 unsigned int maxlen
= 200;
634 char * buffer
, * ptr
;
638 strcpy (tmp_output
, "t:MakeshXXXXXXXX");
640 child_stdout
= Open (tmp_output
, MODE_NEWFILE
);
642 for (aptr
=argv
; *aptr
; aptr
++)
644 len
+= strlen (*aptr
) + 1;
647 buffer
= xmalloc (len
+ 1);
650 for (aptr
=argv
; *aptr
; aptr
++)
653 ptr
+= strlen (ptr
) + 1;
660 Execute (buffer
, NULL
, child_stdout
);
663 Close (child_stdout
);
665 child_stdout
= Open (tmp_output
, MODE_OLDFILE
);
667 buffer
= xmalloc (maxlen
);
674 buffer
= (char *) xrealloc (buffer
, maxlen
+ 1);
677 cc
= Read (child_stdout
, &buffer
[i
], maxlen
- i
);
682 Close (child_stdout
);
683 DeleteFile (tmp_output
);
687 if (buffer
[i
- 1] == '\n')
692 while ((p
= index (p
, '\n')) != 0)
694 o
= variable_buffer_output (o
, buffer
, i
);
698 #endif /* Not Amiga. */
699 #endif /* MSDOS or Amiga. */
706 case function_origin
:
707 /* Expand the argument. */
708 text
= expand_argument (text
, end
);
711 register struct variable
*v
= lookup_variable (text
, strlen (text
));
713 o
= variable_buffer_output (o
, "undefined", 9);
722 o
= variable_buffer_output (o
, "default", 7);
725 o
= variable_buffer_output (o
, "environment", 11);
728 o
= variable_buffer_output (o
, "file", 4);
731 o
= variable_buffer_output (o
, "environment override", 20);
734 o
= variable_buffer_output (o
, "command line", 12);
737 o
= variable_buffer_output (o
, "override", 8);
740 o
= variable_buffer_output (o
, "automatic", 9);
749 /* Expand the argument. */
750 text
= expand_argument (text
, end
);
753 char **words
= (char **) xmalloc (10 * sizeof (char *));
754 unsigned int nwords
= 10;
755 register unsigned int wordi
= 0;
758 /* Chop TEXT into words and put them in WORDS. */
760 while ((p
= find_next_token (&t
, &len
)) != 0)
762 if (wordi
>= nwords
- 1)
765 words
= (char **) xrealloc ((char *) words
,
766 nwords
* sizeof (char *));
768 words
[wordi
++] = savestring (p
, len
);
773 /* Now sort the list of words. */
774 qsort ((char *) words
, wordi
, sizeof (char *), alpha_compare
);
776 /* Now write the sorted list. */
777 for (i
= 0; i
< wordi
; ++i
)
779 len
= strlen (words
[i
]);
780 if (i
== wordi
- 1 || strlen (words
[i
+ 1]) != len
781 || strcmp (words
[i
], words
[i
+ 1]))
783 o
= variable_buffer_output (o
, words
[i
], len
);
784 o
= variable_buffer_output (o
, " ", 1);
788 /* Kill the last space. */
792 free ((char *) words
);
798 case function_foreach
:
800 /* Get three comma-separated arguments but
801 expand only the first two. */
803 register struct variable
*v
;
806 for (p
= text
; p
< end
; ++p
)
808 if (*p
== startparen
)
810 else if (*p
== endparen
)
812 else if (*p
== ',' && count
<= 0)
817 var
= expand_argument (text
, p
);
821 for (p
= p2
; p
< end
; ++p
)
823 if (*p
== startparen
)
825 else if (*p
== endparen
)
827 else if (*p
== ',' && count
<= 0)
832 list
= expand_argument (p2
, p
);
835 text
= savestring (p
, end
- p
);
837 push_new_variable_scope ();
838 v
= define_variable (var
, strlen (var
), "", o_automatic
, 0);
840 while ((p
= find_next_token (&p3
, &len
)) != 0)
846 result
= allocated_variable_expand (text
);
849 o
= variable_buffer_output (o
, result
, strlen (result
));
850 o
= variable_buffer_output (o
, " ", 1);
855 /* Kill the last space. */
858 pop_variable_scope ();
866 case function_filter
:
867 case function_filter_out
:
874 } *words
, *wordtail
, *wp
;
876 /* Get two comma-separated arguments and expand each one. */
878 for (p
= text
; p
< end
; ++p
)
880 if (*p
== startparen
)
882 else if (*p
== endparen
)
884 else if (*p
== ',' && count
<= 0)
888 BADARGS (function
== function_filter
? "filter" : "filter-out");
889 p2
= expand_argument (text
, p
);
891 text
= expand_argument (p
+ 1, end
);
893 /* Chop TEXT up into words and then run each pattern through. */
894 words
= wordtail
= 0;
896 while ((p
= find_next_token (&p3
, &len
)) != 0)
898 struct word
*w
= (struct word
*) alloca (sizeof (struct word
));
916 /* Run each pattern through the words, killing words. */
918 while ((p
= find_next_token (&p3
, &len
)) != 0)
924 percent
= find_percent (p
);
925 for (wp
= words
; wp
!= 0; wp
= wp
->next
)
926 wp
->matched
|= (percent
== 0 ? streq (p
, wp
->word
)
927 : pattern_matches (p
, percent
, wp
->word
));
932 /* Output the words that matched (or didn't, for filter-out). */
933 for (wp
= words
; wp
!= 0; wp
= wp
->next
)
934 if (function
== function_filter
? wp
->matched
: !wp
->matched
)
936 o
= variable_buffer_output (o
, wp
->word
, strlen (wp
->word
));
937 o
= variable_buffer_output (o
, " ", 1);
941 /* Kill the last space. */
950 case function_patsubst
:
951 /* Get three comma-separated arguments and expand each one. */
953 for (p
= text
; p
< end
; ++p
)
955 if (*p
== startparen
)
957 else if (*p
== endparen
)
959 else if (*p
== ',' && count
<= 0)
963 BADARGS ("patsubst");
967 for (++p
; p
< end
; ++p
)
969 if (*p
== startparen
)
971 else if (*p
== endparen
)
973 else if (*p
== ',' && count
<= 0)
977 BADARGS ("patsubst");
979 text
= expand_argument (text
, p2
);
980 p3
= expand_argument (p2
+ 1, p
);
981 p2
= expand_argument (p
+ 1, end
);
983 o
= patsubst_expand (o
, p2
, text
, p3
, (char *) 0, (char *) 0);
991 /* Get two comma-separated arguments and expand each one. */
993 for (p
= text
; p
< end
; ++p
)
995 if (*p
== startparen
)
997 else if (*p
== endparen
)
999 else if (*p
== ',' && count
<= 0)
1004 text
= expand_argument (text
, p
);
1006 p
= expand_argument (p
+ 1, end
);
1009 /* Write each word of the first argument directly followed
1010 by the corresponding word of the second argument.
1011 If the two arguments have a different number of words,
1012 the excess words are just output separated by blanks. */
1013 register char *tp
, *pp
;
1018 unsigned int tlen
, plen
;
1020 tp
= find_next_token (&p2
, &tlen
);
1022 o
= variable_buffer_output (o
, tp
, tlen
);
1024 pp
= find_next_token (&p3
, &plen
);
1026 o
= variable_buffer_output (o
, pp
, plen
);
1028 if (tp
!= 0 || pp
!= 0)
1030 o
= variable_buffer_output (o
, " ", 1);
1034 while (tp
!= 0 || pp
!= 0);
1036 /* Kill the last blank. */
1044 case function_strip
:
1045 /* Expand the argument. */
1046 text
= expand_argument (text
, end
);
1049 while ((p
= find_next_token (&p2
, &i
)) != 0)
1051 o
= variable_buffer_output (o
, p
, i
);
1052 o
= variable_buffer_output (o
, " ", 1);
1056 /* Kill the last space. */
1062 case function_wildcard
:
1063 text
= expand_argument (text
, end
);
1066 o
= wildcard_expansion (text
, o
);
1068 p
= string_glob (text
);
1069 o
= variable_buffer_output (o
, p
, strlen (p
));
1075 case function_subst
:
1076 /* Get three comma-separated arguments and expand each one. */
1078 for (p
= text
; p
< end
; ++p
)
1080 if (*p
== startparen
)
1082 else if (*p
== endparen
)
1084 else if (*p
== ',' && count
<= 0)
1092 for (++p
; p
< end
; ++p
)
1094 if (*p
== startparen
)
1096 else if (*p
== endparen
)
1098 else if (*p
== ',' && count
<= 0)
1104 text
= expand_argument (text
, p2
);
1105 p3
= expand_argument (p2
+ 1, p
);
1106 p2
= expand_argument (p
+ 1, end
);
1108 o
= subst_expand (o
, p2
, text
, p3
, strlen (text
), strlen (p3
), 0, 0);
1115 case function_firstword
:
1116 /* Expand the argument. */
1117 text
= expand_argument (text
, end
);
1119 /* Find the first word in TEXT. */
1121 p
= find_next_token (&p2
, &i
);
1123 o
= variable_buffer_output (o
, p
, i
);
1129 /* Get two comma-separated arguments and expand each one. */
1131 for (p
= text
; p
< end
; ++p
)
1133 if (*p
== startparen
)
1135 else if (*p
== endparen
)
1137 else if (*p
== ',' && count
<= 0)
1142 text
= expand_argument (text
, p
);
1144 p3
= expand_argument (p
+ 1, end
);
1146 /* Check the first argument. */
1147 for (p2
= text
; *p2
!= '\0'; ++p2
)
1148 if (*p2
< '0' || *p2
> '9')
1150 if (reading_filename
!= 0)
1151 makefile_fatal (reading_filename
, *reading_lineno_ptr
,
1152 "non-numeric first argument to `word' function");
1154 fatal ("non-numeric first argument to `word' function");
1157 i
= (unsigned int) atoi (text
);
1160 if (reading_filename
!= 0)
1161 makefile_fatal (reading_filename
, *reading_lineno_ptr
,
1162 "the `word' function takes a one-origin \
1165 fatal ("the `word' function takes a one-origin index argument");
1169 while ((p
= find_next_token (&p2
, &len
)) != 0)
1173 o
= variable_buffer_output (o
, p
, len
);
1179 case function_words
:
1180 /* Expand the argument. */
1181 text
= expand_argument (text
, end
);
1185 while (find_next_token (&p2
, (unsigned int *) 0) != 0)
1190 sprintf (buf
, "%d", i
);
1191 o
= variable_buffer_output (o
, buf
, strlen (buf
));
1197 case function_findstring
:
1198 /* Get two comma-separated arguments and expand each one. */
1200 for (p
= text
; p
< end
; ++p
)
1202 if (*p
== startparen
)
1204 else if (*p
== endparen
)
1206 else if (*p
== ',' && count
<= 0)
1210 BADARGS ("findstring");
1211 text
= expand_argument (text
, p
);
1213 p
= expand_argument (p
+ 1, end
);
1215 /* Find the first occurrence of the first string in the second. */
1217 if (sindex (p
, 0, text
, i
) != 0)
1218 o
= variable_buffer_output (o
, text
, i
);
1224 case function_addsuffix
:
1225 case function_addprefix
:
1226 /* Get two comma-separated arguments and expand each one. */
1228 for (p
= text
; p
< end
; ++p
)
1230 if (*p
== startparen
)
1232 else if (*p
== endparen
)
1234 else if (*p
== ',' && count
<= 0)
1238 BADARGS (function
== function_addsuffix
? "addsuffix" : "addprefix");
1239 text
= expand_argument (text
, p
);
1242 p2
= expand_argument (p
+ 1, end
);
1245 while ((p
= find_next_token (&p3
, &len
)) != 0)
1247 if (function
== function_addprefix
)
1248 o
= variable_buffer_output (o
, text
, i
);
1249 o
= variable_buffer_output (o
, p
, len
);
1250 if (function
== function_addsuffix
)
1251 o
= variable_buffer_output (o
, text
, i
);
1252 o
= variable_buffer_output (o
, " ", 1);
1256 /* Kill last space. */
1264 case function_basename
:
1265 /* Expand the argument. */
1266 text
= expand_argument (text
, end
);
1269 while ((p2
= find_next_token (&p3
, &len
)) != 0)
1273 while (p
>= p2
&& *p
!= (function
== function_dir
? ']' : '.'))
1275 while (p
>= p2
&& *p
!= (function
== function_dir
? '/' : '.'))
1280 if (function
== function_dir
)
1282 o
= variable_buffer_output (o
, p2
, p
- p2
);
1284 else if (function
== function_dir
)
1286 o
= variable_buffer_output (o
, "[]", 2);
1289 o
= variable_buffer_output (o
, "./", 2);
1291 /* o = o */; /* Just a nop... */
1295 /* The entire name is the basename. */
1296 o
= variable_buffer_output (o
, p2
, len
);
1298 o
= variable_buffer_output (o
, " ", 1);
1302 /* Kill last space. */
1308 case function_notdir
:
1309 case function_suffix
:
1310 /* Expand the argument. */
1311 text
= expand_argument (text
, end
);
1314 while ((p2
= find_next_token (&p3
, &len
)) != 0)
1318 while (p
>= p2
&& *p
!= (function
== function_notdir
? ']' : '.'))
1320 while (p
>= p2
&& *p
!= (function
== function_notdir
? '/' : '.'))
1325 if (function
== function_notdir
)
1327 o
= variable_buffer_output (o
, p
, len
- (p
- p2
));
1329 else if (function
== function_notdir
)
1330 o
= variable_buffer_output (o
, p2
, len
);
1332 if (function
== function_notdir
|| p
>= p2
)
1334 o
= variable_buffer_output (o
, " ", 1);
1339 /* Kill last space. */
1349 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1350 opening ( or { and is not null-terminated. If a function invocation
1351 is found, expand it into the buffer at *OP, updating *OP, incrementing
1352 *STRINGP past the reference and returning nonzero. If not, return zero. */
1355 handle_function (op
, stringp
)
1360 register unsigned int code
;
1361 unsigned int maxlen
;
1362 char *beg
= *stringp
+ 1;
1365 endref
= lindex (beg
, beg
+ MAXFUNCTIONLEN
, '\0');
1366 maxlen
= endref
!= 0 ? endref
- beg
: MAXFUNCTIONLEN
;
1368 for (code
= 0; function_table
[code
].name
!= 0; ++code
)
1370 if (maxlen
< function_table
[code
].len
)
1372 endref
= beg
+ function_table
[code
].len
;
1373 if (isblank (*endref
)
1374 && !strncmp (function_table
[code
].name
, beg
,
1375 function_table
[code
].len
))
1378 if (function_table
[code
].name
!= 0)
1380 /* We have found a call to an expansion-time function.
1381 Find the end of the arguments, and do the function. */
1383 char openparen
= beg
[-1], closeparen
= openparen
== '(' ? ')' : '}';
1388 /* Space after function name isn't part of the args. */
1389 p
= next_token (endref
);
1392 /* Count nested use of whichever kind of parens we use,
1393 so that nested calls and variable refs work. */
1395 for (; *p
!= '\0'; ++p
)
1397 if (*p
== openparen
)
1399 else if (*p
== closeparen
&& --count
< 0)
1405 static const char errmsg
[]
1406 = "unterminated call to function `%s': missing `%c'";
1407 if (reading_filename
== 0)
1408 fatal (errmsg
, function_table
[code
].name
, closeparen
);
1410 makefile_fatal (reading_filename
, *reading_lineno_ptr
, errmsg
,
1411 function_table
[code
].name
, closeparen
);
1414 /* We found the end; expand the function call. */
1416 *op
= expand_function (*op
, function_table
[code
].function
, argbeg
, p
);
1424 /* Glob-expand LINE. The returned pointer is
1425 only good until the next call to string_glob. */
1431 static char *result
= 0;
1432 static unsigned int length
;
1433 register struct nameseq
*chain
;
1434 register unsigned int idx
;
1436 chain
= multi_glob (parse_file_seq
1437 (&line
, '\0', sizeof (struct nameseq
),
1438 /* We do not want parse_file_seq to strip `./'s.
1439 That would break examples like:
1440 $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)). */
1442 sizeof (struct nameseq
));
1447 result
= (char *) xmalloc (100);
1453 register char *name
= chain
->name
;
1454 unsigned int len
= strlen (name
);
1456 struct nameseq
*next
= chain
->next
;
1457 free ((char *) chain
);
1460 /* multi_glob will pass names without globbing metacharacters
1461 through as is, but we want only files that actually exist. */
1462 if (file_exists_p (name
))
1464 if (idx
+ len
+ 1 > length
)
1466 length
+= (len
+ 1) * 2;
1467 result
= (char *) xrealloc (result
, length
);
1469 bcopy (name
, &result
[idx
], len
);
1471 result
[idx
++] = ' ';
1477 /* Kill the last space and terminate the string. */
1481 result
[idx
- 1] = '\0';