(main): Don't call signame_init #ifdef HAVE_STRSIGNAL.
[make.git] / function.c
blobad8e06bdb79ce0acc5c20ff1653d49b4e646eb57
1 /* Variable function expansion for GNU Make.
2 Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994 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)
8 any later version.
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. */
19 #include "make.h"
20 #include "variable.h"
21 #include "dep.h"
22 #include "commands.h"
23 #include "job.h"
25 #ifdef __MSDOS__
26 #include <process.h>
27 #endif
29 static char *string_glob ();
31 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
32 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
33 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
34 nonzero, substitutions are done only on matches which are complete
35 whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
36 done only at the ends of whitespace-delimited words. */
38 char *
39 subst_expand (o, text, subst, replace, slen, rlen, by_word, suffix_only)
40 char *o;
41 char *text;
42 char *subst, *replace;
43 unsigned int slen, rlen;
44 int by_word, suffix_only;
46 register char *t = text;
47 register char *p;
49 if (slen == 0 && !by_word && !suffix_only)
51 /* The first occurrence of "" in any string is its end. */
52 o = variable_buffer_output (o, t, strlen (t));
53 if (rlen > 0)
54 o = variable_buffer_output (o, replace, rlen);
55 return o;
60 if ((by_word | suffix_only) && slen == 0)
61 /* When matching by words, the empty string should match
62 the end of each word, rather than the end of the whole text. */
63 p = end_of_token (next_token (t));
64 else
66 p = sindex (t, 0, subst, slen);
67 if (p == 0)
69 /* No more matches. Output everything left on the end. */
70 o = variable_buffer_output (o, t, strlen (t));
71 return o;
75 /* Output everything before this occurrence of the string to replace. */
76 if (p > t)
77 o = variable_buffer_output (o, t, p - t);
79 /* If we're substituting only by fully matched words,
80 or only at the ends of words, check that this case qualifies. */
81 if ((by_word
82 && ((p > t && !isblank (p[-1]))
83 || (p[slen] != '\0' && !isblank (p[slen]))))
84 || (suffix_only
85 && (p[slen] != '\0' && !isblank (p[slen]))))
86 /* Struck out. Output the rest of the string that is
87 no longer to be replaced. */
88 o = variable_buffer_output (o, subst, slen);
89 else if (rlen > 0)
90 /* Output the replacement string. */
91 o = variable_buffer_output (o, replace, rlen);
93 /* Advance T past the string to be replaced. */
94 t = p + slen;
95 } while (*t != '\0');
97 return o;
101 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
102 and replacing strings matching PATTERN with REPLACE.
103 If PATTERN_PERCENT is not nil, PATTERN has already been
104 run through find_percent, and PATTERN_PERCENT is the result.
105 If REPLACE_PERCENT is not nil, REPLACE has already been
106 run through find_percent, and REPLACE_PERCENT is the result. */
108 char *
109 patsubst_expand (o, text, pattern, replace, pattern_percent, replace_percent)
110 char *o;
111 char *text;
112 register char *pattern, *replace;
113 register char *pattern_percent, *replace_percent;
115 register int pattern_prepercent_len, pattern_postpercent_len;
116 register int replace_prepercent_len, replace_postpercent_len;
117 register char *t;
118 unsigned int len;
119 int doneany = 0;
121 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
122 will be collapsed before we call subst_expand if PATTERN has no %. */
123 if (replace_percent == 0)
124 replace_percent = find_percent (replace);
125 if (replace_percent != 0)
127 /* Record the length of REPLACE before and after the % so
128 we don't have to compute these lengths more than once. */
129 replace_prepercent_len = replace_percent - replace;
130 replace_postpercent_len = strlen (replace_percent + 1);
132 else
133 /* We store the length of the replacement
134 so we only need to compute it once. */
135 replace_prepercent_len = strlen (replace);
137 if (pattern_percent == 0)
138 pattern_percent = find_percent (pattern);
139 if (pattern_percent == 0)
140 /* With no % in the pattern, this is just a simple substitution. */
141 return subst_expand (o, text, pattern, replace,
142 strlen (pattern), strlen (replace), 1, 0);
144 /* Record the length of PATTERN before and after the %
145 so we don't have to compute it more than once. */
146 pattern_prepercent_len = pattern_percent - pattern;
147 pattern_postpercent_len = strlen (pattern_percent + 1);
149 while ((t = find_next_token (&text, &len)) != 0)
151 int fail = 0;
153 /* Is it big enough to match? */
154 if (len < pattern_prepercent_len + pattern_postpercent_len)
155 fail = 1;
157 /* Does the prefix match? */
158 if (!fail && pattern_prepercent_len > 0
159 && (*t != *pattern
160 || t[pattern_prepercent_len - 1] != pattern_percent[-1]
161 || strncmp (t + 1, pattern + 1, pattern_prepercent_len - 1)))
162 fail = 1;
164 /* Does the suffix match? */
165 if (!fail && pattern_postpercent_len > 0
166 && (t[len - 1] != pattern_percent[pattern_postpercent_len]
167 || t[len - pattern_postpercent_len] != pattern_percent[1]
168 || strncmp (&t[len - pattern_postpercent_len],
169 &pattern_percent[1], pattern_postpercent_len - 1)))
170 fail = 1;
172 if (fail)
173 /* It didn't match. Output the string. */
174 o = variable_buffer_output (o, t, len);
175 else
177 /* It matched. Output the replacement. */
179 /* Output the part of the replacement before the %. */
180 o = variable_buffer_output (o, replace, replace_prepercent_len);
182 if (replace_percent != 0)
184 /* Output the part of the matched string that
185 matched the % in the pattern. */
186 o = variable_buffer_output (o, t + pattern_prepercent_len,
187 len - (pattern_prepercent_len
188 + pattern_postpercent_len));
189 /* Output the part of the replacement after the %. */
190 o = variable_buffer_output (o, replace_percent + 1,
191 replace_postpercent_len);
195 /* Output a space, but not if the replacement is "". */
196 if (fail || replace_prepercent_len > 0
197 || (replace_percent != 0 && len + replace_postpercent_len > 0))
199 o = variable_buffer_output (o, " ", 1);
200 doneany = 1;
203 if (doneany)
204 /* Kill the last space. */
205 --o;
207 return o;
210 /* Handle variable-expansion-time functions such as $(dir foo/bar) ==> foo/ */
212 /* These enumeration constants distinguish the
213 various expansion-time built-in functions. */
215 enum function
217 function_subst,
218 function_addsuffix,
219 function_addprefix,
220 function_dir,
221 function_notdir,
222 function_suffix,
223 function_basename,
224 function_wildcard,
225 function_firstword,
226 function_word,
227 function_words,
228 function_findstring,
229 function_strip,
230 function_join,
231 function_patsubst,
232 function_filter,
233 function_filter_out,
234 function_foreach,
235 function_sort,
236 function_origin,
237 function_shell,
238 function_invalid
241 /* Greater than the length of any function name. */
242 #define MAXFUNCTIONLEN 11
244 /* The function names and lengths of names, for looking them up. */
246 static struct
248 char *name;
249 unsigned int len;
250 enum function function;
251 } function_table[] =
253 { "subst", 5, function_subst },
254 { "addsuffix", 9, function_addsuffix },
255 { "addprefix", 9, function_addprefix },
256 { "dir", 3, function_dir },
257 { "notdir", 6, function_notdir },
258 { "suffix", 6, function_suffix },
259 { "basename", 8, function_basename },
260 { "wildcard", 8, function_wildcard },
261 { "firstword", 9, function_firstword },
262 { "word", 4, function_word },
263 { "words", 5, function_words },
264 { "findstring", 10, function_findstring },
265 { "strip", 5, function_strip },
266 { "join", 4, function_join },
267 { "patsubst", 8, function_patsubst },
268 { "filter", 6, function_filter },
269 { "filter-out", 10, function_filter_out },
270 { "foreach", 7, function_foreach },
271 { "sort", 4, function_sort },
272 { "origin", 6, function_origin },
273 { "shell", 5, function_shell },
274 { 0, 0, function_invalid }
277 /* Return 1 if PATTERN matches WORD, 0 if not. */
280 pattern_matches (pattern, percent, word)
281 register char *pattern, *percent, *word;
283 unsigned int sfxlen, wordlen;
285 if (percent == 0)
287 unsigned int len = strlen (pattern) + 1;
288 char *new = (char *) alloca (len);
289 bcopy (pattern, new, len);
290 pattern = new;
291 percent = find_percent (pattern);
292 if (percent == 0)
293 return streq (pattern, word);
296 sfxlen = strlen (percent + 1);
297 wordlen = strlen (word);
299 if (wordlen < (percent - pattern) + sfxlen
300 || strncmp (pattern, word, percent - pattern))
301 return 0;
303 return !strcmp (percent + 1, word + (wordlen - sfxlen));
306 int shell_function_pid = 0, shell_function_completed;
308 /* Perform the function specified by FUNCTION on the text at TEXT.
309 END is points to the end of the argument text (exclusive).
310 The output is written into VARIABLE_BUFFER starting at O. */
312 /* Note this absorbs a semicolon and is safe to use in conditionals. */
313 #define BADARGS(func) \
314 if (reading_filename != 0) \
315 makefile_fatal (reading_filename, *reading_lineno_ptr, \
316 "insufficient arguments to function `%s'", \
317 func); \
318 else \
319 fatal ("insufficient arguments to function `%s'", func)
321 static char *
322 expand_function (o, function, text, end)
323 char *o;
324 enum function function;
325 char *text;
326 char *end;
328 char *p, *p2, *p3;
329 unsigned int i, len;
330 int doneany = 0;
331 int count;
332 char endparen = *end, startparen = *end == ')' ? '(' : '{';
334 switch (function)
336 default:
337 abort ();
338 break;
340 case function_shell:
342 char **argv, **envp;
343 char *error_prefix;
344 int pipedes[2];
345 int pid;
347 /* Expand the command line. */
348 text = expand_argument (text, end);
350 /* Construct the argument list. */
351 argv = construct_command_argv (text, (char *) NULL, (struct file *) 0);
352 if (argv == 0)
353 break;
355 /* Using a target environment for `shell' loses in cases like:
356 export var = $(shell echo foobie)
357 because target_environment hits a loop trying to expand $(var)
358 to put it in the environment. This is even more confusing when
359 var was not explicitly exported, but just appeared in the
360 calling environment. */
361 #if 1
362 envp = environ;
363 #else
364 /* Construct the environment. */
365 envp = target_environment ((struct file *) 0);
366 #endif
368 /* For error messages. */
369 if (reading_filename != 0)
371 error_prefix = (char *) alloca (strlen (reading_filename) + 100);
372 sprintf (error_prefix,
373 "%s:%u: ", reading_filename, *reading_lineno_ptr);
375 else
376 error_prefix = "";
378 #ifndef __MSDOS__
379 if (pipe (pipedes) < 0)
381 perror_with_name (error_prefix, "pipe");
382 break;
385 pid = vfork ();
386 if (pid < 0)
387 perror_with_name (error_prefix, "fork");
388 else if (pid == 0)
389 child_execute_job (0, pipedes[1], argv, envp);
390 else
392 /* We are the parent. */
394 char *buffer;
395 unsigned int maxlen;
396 int cc;
398 /* Free the storage only the child needed. */
399 free (argv[0]);
400 free ((char *) argv);
401 #if 0
402 for (i = 0; envp[i] != 0; ++i)
403 free (envp[i]);
404 free ((char *) envp);
405 #endif
407 /* Record the PID for reap_children. */
408 shell_function_pid = pid;
409 shell_function_completed = 0;
412 /* Set up and read from the pipe. */
414 maxlen = 200;
415 buffer = (char *) xmalloc (maxlen + 1);
417 /* Close the write side of the pipe. */
418 (void) close (pipedes[1]);
420 /* Read from the pipe until it gets EOF. */
421 i = 0;
424 if (i == maxlen)
426 maxlen += 512;
427 buffer = (char *) xrealloc (buffer, maxlen + 1);
430 errno = 0;
431 cc = read (pipedes[0], &buffer[i], maxlen - i);
432 if (cc > 0)
433 i += cc;
435 #ifdef EINTR
436 while (cc > 0 || errno == EINTR);
437 #else
438 while (cc > 0);
439 #endif
441 /* Close the read side of the pipe. */
442 (void) close (pipedes[0]);
444 /* Loop until child_handler sets shell_function_completed
445 to the status of our child shell. */
446 while (shell_function_completed == 0)
447 reap_children (1, 0);
449 shell_function_pid = 0;
451 /* The child_handler function will set shell_function_completed
452 to 1 when the child dies normally, or to -1 if it
453 dies with status 127, which is most likely an exec fail. */
455 if (shell_function_completed == -1)
457 /* This most likely means that the execvp failed,
458 so we should just write out the error message
459 that came in over the pipe from the child. */
460 fputs (buffer, stderr);
461 fflush (stderr);
463 else
465 /* The child finished normally. Replace all
466 newlines in its output with spaces, and put
467 that in the variable output buffer. */
468 if (i > 0)
470 if (buffer[i - 1] == '\n')
471 buffer[--i] = '\0';
472 else
473 buffer[i] = '\0';
474 p = buffer;
475 while ((p = index (p, '\n')) != 0)
476 *p++ = ' ';
477 o = variable_buffer_output (o, buffer, i);
481 free (buffer);
483 #else /* MSDOS. */
485 /* MS-DOS can't do fork, but it can do spawn. However, this
486 means that we don't have an opportunity to reopen stdout to
487 trap it. Thus, we save our own stdout onto a new descriptor
488 and dup a temp file's descriptor onto our stdout temporarily.
489 After we spawn the shell program, we dup our own stdout back
490 to the stdout descriptor. The buffer reading is the same as
491 above, except that we're now reading from a file. */
493 int save_stdout;
494 int child_stdout;
495 char tmp_output[FILENAME_MAX];
496 FILE *child_stream;
497 unsigned int maxlen = 200;
498 int cc;
499 char *buffer;
501 strcpy (tmp_output, "shXXXXXX");
502 mktemp (tmp_output);
503 child_stdout = open (tmp_output,
504 O_WRONLY|O_CREAT|O_TRUNC|O_TEXT, 0644);
505 save_stdout = dup (1);
506 dup2 (child_stdout, 1);
507 spawnvp (P_WAIT, argv[0], argv);
508 dup2 (save_stdout, 1);
509 close (child_stdout);
510 close (save_stdout);
512 child_stdout = open (tmp_output, O_RDONLY|O_TEXT, 0644);
514 buffer = xmalloc (maxlen);
515 i = 0;
518 if (i == maxlen)
520 maxlen += 512;
521 buffer = (char *) xrealloc (buffer, maxlen + 1);
524 cc = read (child_stdout, &buffer[i], maxlen - i);
525 if (cc > 0)
526 i += cc;
527 } while (cc > 0);
529 close (child_stdout);
530 unlink (tmp_output);
532 if (i > 0)
534 if (buffer[i - 1] == '\n')
535 buffer[--i] = '\0';
536 else
537 buffer[i] = '\0';
538 p = buffer;
539 while ((p = index (p, '\n')) != 0)
540 *p++ = ' ';
541 o = variable_buffer_output (o, buffer, i);
543 free (buffer);
545 #endif /* Not MSDOS. */
547 free (text);
548 break;
551 case function_origin:
552 /* Expand the argument. */
553 text = expand_argument (text, end);
556 register struct variable *v = lookup_variable (text, strlen (text));
557 if (v == 0)
558 o = variable_buffer_output (o, "undefined", 9);
559 else
560 switch (v->origin)
562 default:
563 case o_invalid:
564 abort ();
565 break;
566 case o_default:
567 o = variable_buffer_output (o, "default", 7);
568 break;
569 case o_env:
570 o = variable_buffer_output (o, "environment", 11);
571 break;
572 case o_file:
573 o = variable_buffer_output (o, "file", 4);
574 break;
575 case o_env_override:
576 o = variable_buffer_output (o, "environment override", 20);
577 break;
578 case o_command:
579 o = variable_buffer_output (o, "command line", 12);
580 break;
581 case o_override:
582 o = variable_buffer_output (o, "override", 8);
583 break;
584 case o_automatic:
585 o = variable_buffer_output (o, "automatic", 9);
586 break;
590 free (text);
591 break;
593 case function_sort:
594 /* Expand the argument. */
595 text = expand_argument (text, end);
598 char **words = (char **) xmalloc (10 * sizeof (char *));
599 unsigned int nwords = 10;
600 register unsigned int wordi = 0;
601 char *t;
603 /* Chop TEXT into words and put them in WORDS. */
604 t = text;
605 while ((p = find_next_token (&t, &len)) != 0)
607 if (wordi >= nwords - 1)
609 nwords *= 2;
610 words = (char **) xrealloc ((char *) words,
611 nwords * sizeof (char *));
613 words[wordi++] = savestring (p, len);
616 if (wordi > 0)
618 /* Now sort the list of words. */
619 qsort ((char *) words, wordi, sizeof (char *), alpha_compare);
621 /* Now write the sorted list. */
622 for (i = 0; i < wordi; ++i)
624 len = strlen (words[i]);
625 if (i == wordi - 1 || strlen (words[i + 1]) != len
626 || strcmp (words[i], words[i + 1]))
628 o = variable_buffer_output (o, words[i], len);
629 o = variable_buffer_output (o, " ", 1);
631 free (words[i]);
633 /* Kill the last space. */
634 --o;
637 free ((char *) words);
640 free (text);
641 break;
643 case function_foreach:
645 /* Get three comma-separated arguments but
646 expand only the first two. */
647 char *var, *list;
648 register struct variable *v;
650 count = 0;
651 for (p = text; p < end; ++p)
653 if (*p == startparen)
654 ++count;
655 else if (*p == endparen)
656 --count;
657 else if (*p == ',' && count <= 0)
658 break;
660 if (p == end)
661 BADARGS ("foreach");
662 var = expand_argument (text, p);
664 p2 = p + 1;
665 count = 0;
666 for (p = p2; p < end; ++p)
668 if (*p == startparen)
669 ++count;
670 else if (*p == endparen)
671 --count;
672 else if (*p == ',' && count <= 0)
673 break;
675 if (p == end)
676 BADARGS ("foreach");
677 list = expand_argument (p2, p);
679 ++p;
680 text = savestring (p, end - p);
682 push_new_variable_scope ();
683 v = define_variable (var, strlen (var), "", o_automatic, 0);
684 p3 = list;
685 while ((p = find_next_token (&p3, &len)) != 0)
687 char *result;
688 char save = p[len];
689 p[len] = '\0';
690 v->value = p;
691 result = allocated_variable_expand (text);
692 p[len] = save;
694 o = variable_buffer_output (o, result, strlen (result));
695 o = variable_buffer_output (o, " ", 1);
696 doneany = 1;
697 free (result);
699 if (doneany)
700 /* Kill the last space. */
701 --o;
703 pop_variable_scope ();
705 free (var);
706 free (list);
707 free (text);
709 break;
711 case function_filter:
712 case function_filter_out:
714 struct word
716 struct word *next;
717 char *word;
718 int matched;
719 } *words, *wordtail, *wp;
721 /* Get two comma-separated arguments and expand each one. */
722 count = 0;
723 for (p = text; p < end; ++p)
725 if (*p == startparen)
726 ++count;
727 else if (*p == endparen)
728 --count;
729 else if (*p == ',' && count <= 0)
730 break;
732 if (p == end)
733 BADARGS (function == function_filter ? "filter" : "filter-out");
734 p2 = expand_argument (text, p);
736 text = expand_argument (p + 1, end);
738 /* Chop TEXT up into words and then run each pattern through. */
739 words = wordtail = 0;
740 p3 = text;
741 while ((p = find_next_token (&p3, &len)) != 0)
743 struct word *w = (struct word *) alloca (sizeof (struct word));
744 if (words == 0)
745 words = w;
746 else
747 wordtail->next = w;
748 wordtail = w;
750 if (*p3 != '\0')
751 ++p3;
752 p[len] = '\0';
753 w->word = p;
754 w->matched = 0;
757 if (words != 0)
759 wordtail->next = 0;
761 /* Run each pattern through the words, killing words. */
762 p3 = p2;
763 while ((p = find_next_token (&p3, &len)) != 0)
765 char *percent;
766 char save = p[len];
767 p[len] = '\0';
769 percent = find_percent (p);
770 for (wp = words; wp != 0; wp = wp->next)
771 wp->matched |= (percent == 0 ? streq (p, wp->word)
772 : pattern_matches (p, percent, wp->word));
774 p[len] = save;
777 /* Output the words that matched (or didn't, for filter-out). */
778 for (wp = words; wp != 0; wp = wp->next)
779 if (function == function_filter ? wp->matched : !wp->matched)
781 o = variable_buffer_output (o, wp->word, strlen (wp->word));
782 o = variable_buffer_output (o, " ", 1);
783 doneany = 1;
785 if (doneany)
786 /* Kill the last space. */
787 --o;
790 free (p2);
791 free (text);
793 break;
795 case function_patsubst:
796 /* Get three comma-separated arguments and expand each one. */
797 count = 0;
798 for (p = text; p < end; ++p)
800 if (*p == startparen)
801 ++count;
802 else if (*p == endparen)
803 --count;
804 else if (*p == ',' && count <= 0)
805 break;
807 if (p == end)
808 BADARGS ("patsubst");
810 p2 = p;
811 count = 0;
812 for (++p; p < end; ++p)
814 if (*p == startparen)
815 ++count;
816 else if (*p == endparen)
817 --count;
818 else if (*p == ',' && count <= 0)
819 break;
821 if (p == end)
822 BADARGS ("patsubst");
824 text = expand_argument (text, p2);
825 p3 = expand_argument (p2 + 1, p);
826 p2 = expand_argument (p + 1, end);
828 o = patsubst_expand (o, p2, text, p3, (char *) 0, (char *) 0);
830 free (text);
831 free (p3);
832 free (p2);
833 break;
835 case function_join:
836 /* Get two comma-separated arguments and expand each one. */
837 count = 0;
838 for (p = text; p < end; ++p)
840 if (*p == startparen)
841 ++count;
842 else if (*p == endparen)
843 --count;
844 else if (*p == ',' && count <= 0)
845 break;
847 if (p == end)
848 BADARGS ("join");
849 text = expand_argument (text, p);
851 p = expand_argument (p + 1, end);
854 /* Write each word of the first argument directly followed
855 by the corresponding word of the second argument.
856 If the two arguments have a different number of words,
857 the excess words are just output separated by blanks. */
858 register char *tp, *pp;
859 p2 = text;
860 p3 = p;
863 unsigned int tlen, plen;
865 tp = find_next_token (&p2, &tlen);
866 if (tp != 0)
867 o = variable_buffer_output (o, tp, tlen);
869 pp = find_next_token (&p3, &plen);
870 if (pp != 0)
871 o = variable_buffer_output (o, pp, plen);
873 if (tp != 0 || pp != 0)
875 o = variable_buffer_output (o, " ", 1);
876 doneany = 1;
879 while (tp != 0 || pp != 0);
880 if (doneany)
881 /* Kill the last blank. */
882 --o;
885 free (text);
886 free (p);
887 break;
889 case function_strip:
890 /* Expand the argument. */
891 text = expand_argument (text, end);
893 p2 = text;
894 while ((p = find_next_token (&p2, &i)) != 0)
896 o = variable_buffer_output (o, p, i);
897 o = variable_buffer_output (o, " ", 1);
898 doneany = 1;
900 if (doneany)
901 /* Kill the last space. */
902 --o;
904 free (text);
905 break;
907 case function_wildcard:
908 text = expand_argument (text, end);
910 p = string_glob (text);
911 o = variable_buffer_output (o, p, strlen (p));
913 free (text);
914 break;
916 case function_subst:
917 /* Get three comma-separated arguments and expand each one. */
918 count = 0;
919 for (p = text; p < end; ++p)
921 if (*p == startparen)
922 ++count;
923 else if (*p == endparen)
924 --count;
925 else if (*p == ',' && count <= 0)
926 break;
928 if (p == end)
929 BADARGS ("subst");
931 p2 = p;
932 count = 0;
933 for (++p; p < end; ++p)
935 if (*p == startparen)
936 ++count;
937 else if (*p == endparen)
938 --count;
939 else if (*p == ',' && count <= 0)
940 break;
942 if (p == end)
943 BADARGS ("subst");
945 text = expand_argument (text, p2);
946 p3 = expand_argument (p2 + 1, p);
947 p2 = expand_argument (p + 1, end);
949 o = subst_expand (o, p2, text, p3, strlen (text), strlen (p3), 0, 0);
951 free (text);
952 free (p3);
953 free (p2);
954 break;
956 case function_firstword:
957 /* Expand the argument. */
958 text = expand_argument (text, end);
960 /* Find the first word in TEXT. */
961 p2 = text;
962 p = find_next_token (&p2, &i);
963 if (p != 0)
964 o = variable_buffer_output (o, p, i);
966 free (text);
967 break;
969 case function_word:
970 /* Get two comma-separated arguments and expand each one. */
971 count = 0;
972 for (p = text; p < end; ++p)
974 if (*p == startparen)
975 ++count;
976 else if (*p == endparen)
977 --count;
978 else if (*p == ',' && count <= 0)
979 break;
981 if (p == end)
982 BADARGS ("word");
983 text = expand_argument (text, p);
985 p3 = expand_argument (p + 1, end);
987 /* Check the first argument. */
988 for (p2 = text; *p2 != '\0'; ++p2)
989 if (*p2 < '0' || *p2 > '9')
991 if (reading_filename != 0)
992 makefile_fatal (reading_filename, *reading_lineno_ptr,
993 "non-numeric first argument to `word' function");
994 else
995 fatal ("non-numeric first argument to `word' function");
998 i = (unsigned int) atoi (text);
999 if (i == 0)
1001 if (reading_filename != 0)
1002 makefile_fatal (reading_filename, *reading_lineno_ptr,
1003 "the `word' function takes a one-origin \
1004 index argument");
1005 else
1006 fatal ("the `word' function takes a one-origin index argument");
1009 p2 = p3;
1010 while ((p = find_next_token (&p2, &len)) != 0)
1011 if (--i == 0)
1012 break;
1013 if (i == 0)
1014 o = variable_buffer_output (o, p, len);
1016 free (text);
1017 free (p3);
1018 break;
1020 case function_words:
1021 /* Expand the argument. */
1022 text = expand_argument (text, end);
1024 i = 0;
1025 p2 = text;
1026 while (find_next_token (&p2, (unsigned int *) 0) != 0)
1027 ++i;
1030 char buf[20];
1031 sprintf (buf, "%d", i);
1032 o = variable_buffer_output (o, buf, strlen (buf));
1035 free (text);
1036 break;
1038 case function_findstring:
1039 /* Get two comma-separated arguments and expand each one. */
1040 count = 0;
1041 for (p = text; p < end; ++p)
1043 if (*p == startparen)
1044 ++count;
1045 else if (*p == endparen)
1046 --count;
1047 else if (*p == ',' && count <= 0)
1048 break;
1050 if (p == end)
1051 BADARGS ("findstring");
1052 text = expand_argument (text, p);
1054 p = expand_argument (p + 1, end);
1056 /* Find the first occurrence of the first string in the second. */
1057 i = strlen (text);
1058 if (sindex (p, 0, text, i) != 0)
1059 o = variable_buffer_output (o, text, i);
1061 free (p);
1062 free (text);
1063 break;
1065 case function_addsuffix:
1066 case function_addprefix:
1067 /* Get two comma-separated arguments and expand each one. */
1068 count = 0;
1069 for (p = text; p < end; ++p)
1071 if (*p == startparen)
1072 ++count;
1073 else if (*p == endparen)
1074 --count;
1075 else if (*p == ',' && count <= 0)
1076 break;
1078 if (p == end)
1079 BADARGS (function == function_addsuffix ? "addsuffix" : "addprefix");
1080 text = expand_argument (text, p);
1081 i = strlen (text);
1083 p2 = expand_argument (p + 1, end);
1085 p3 = p2;
1086 while ((p = find_next_token (&p3, &len)) != 0)
1088 if (function == function_addprefix)
1089 o = variable_buffer_output (o, text, i);
1090 o = variable_buffer_output (o, p, len);
1091 if (function == function_addsuffix)
1092 o = variable_buffer_output (o, text, i);
1093 o = variable_buffer_output (o, " ", 1);
1094 doneany = 1;
1096 if (doneany)
1097 /* Kill last space. */
1098 --o;
1100 free (p2);
1101 free (text);
1102 break;
1104 case function_dir:
1105 case function_basename:
1106 /* Expand the argument. */
1107 text = expand_argument (text, end);
1109 p3 = text;
1110 while ((p2 = find_next_token (&p3, &len)) != 0)
1112 p = p2 + len;
1113 while (p >= p2 && *p != (function == function_dir ? '/' : '.'))
1114 --p;
1115 if (p >= p2)
1117 if (function == function_dir)
1118 ++p;
1119 o = variable_buffer_output (o, p2, p - p2);
1121 else if (function == function_dir)
1122 o = variable_buffer_output (o, "./", 2);
1123 else
1124 /* The entire name is the basename. */
1125 o = variable_buffer_output (o, p2, len);
1127 o = variable_buffer_output (o, " ", 1);
1128 doneany = 1;
1130 if (doneany)
1131 /* Kill last space. */
1132 --o;
1134 free (text);
1135 break;
1137 case function_notdir:
1138 case function_suffix:
1139 /* Expand the argument. */
1140 text = expand_argument (text, end);
1142 p3 = text;
1143 while ((p2 = find_next_token (&p3, &len)) != 0)
1145 p = p2 + len;
1146 while (p >= p2 && *p != (function == function_notdir ? '/' : '.'))
1147 --p;
1148 if (p >= p2)
1150 if (function == function_notdir)
1151 ++p;
1152 o = variable_buffer_output (o, p, len - (p - p2));
1154 else if (function == function_notdir)
1155 o = variable_buffer_output (o, p2, len);
1157 if (function == function_notdir || p >= p2)
1159 o = variable_buffer_output (o, " ", 1);
1160 doneany = 1;
1163 if (doneany)
1164 /* Kill last space. */
1165 --o;
1167 free (text);
1168 break;
1171 return o;
1174 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1175 opening ( or { and is not null-terminated. If a function invocation
1176 is found, expand it into the buffer at *OP, updating *OP, incrementing
1177 *STRINGP past the reference and returning nonzero. If not, return zero. */
1180 handle_function (op, stringp)
1181 char **op;
1182 char **stringp;
1185 register unsigned int code;
1186 unsigned int maxlen;
1187 char *beg = *stringp + 1;
1188 char *endref;
1190 endref = lindex (beg, beg + MAXFUNCTIONLEN, '\0');
1191 maxlen = endref != 0 ? endref - beg : MAXFUNCTIONLEN;
1193 for (code = 0; function_table[code].name != 0; ++code)
1195 if (maxlen < function_table[code].len)
1196 continue;
1197 endref = beg + function_table[code].len;
1198 if (isblank (*endref)
1199 && !strncmp (function_table[code].name, beg,
1200 function_table[code].len))
1201 break;
1203 if (function_table[code].name != 0)
1205 /* We have found a call to an expansion-time function.
1206 Find the end of the arguments, and do the function. */
1208 char openparen = beg[-1], closeparen = openparen == '(' ? ')' : '}';
1209 int count = 0;
1210 char *argbeg;
1211 register char *p;
1213 /* Space after function name isn't part of the args. */
1214 p = next_token (endref);
1215 argbeg = p;
1217 /* Count nested use of whichever kind of parens we use,
1218 so that nested calls and variable refs work. */
1220 for (; *p != '\0'; ++p)
1222 if (*p == openparen)
1223 ++count;
1224 else if (*p == closeparen && --count < 0)
1225 break;
1228 if (count >= 0)
1230 static const char errmsg[]
1231 = "unterminated call to function `%s': missing `%c'";
1232 if (reading_filename == 0)
1233 fatal (errmsg, function_table[code].name, closeparen);
1234 else
1235 makefile_fatal (reading_filename, *reading_lineno_ptr, errmsg,
1236 function_table[code].name, closeparen);
1239 /* We found the end; expand the function call. */
1241 *op = expand_function (*op, function_table[code].function, argbeg, p);
1242 *stringp = p;
1243 return 1;
1246 return 0;
1249 /* Glob-expand LINE. The returned pointer is
1250 only good until the next call to string_glob. */
1252 static char *
1253 string_glob (line)
1254 char *line;
1256 static char *result = 0;
1257 static unsigned int length;
1258 register struct nameseq *chain;
1259 register unsigned int idx;
1261 chain = multi_glob (parse_file_seq
1262 (&line, '\0', sizeof (struct nameseq),
1263 /* We do not want parse_file_seq to strip `./'s.
1264 That would break examples like:
1265 $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)). */
1267 sizeof (struct nameseq));
1269 if (result == 0)
1271 length = 100;
1272 result = (char *) xmalloc (100);
1275 idx = 0;
1276 while (chain != 0)
1278 register char *name = chain->name;
1279 unsigned int len = strlen (name);
1281 struct nameseq *next = chain->next;
1282 free ((char *) chain);
1283 chain = next;
1285 /* multi_glob will pass names without globbing metacharacters
1286 through as is, but we want only files that actually exist. */
1287 if (file_exists_p (name))
1289 if (idx + len + 1 > length)
1291 length += (len + 1) * 2;
1292 result = (char *) xrealloc (result, length);
1294 bcopy (name, &result[idx], len);
1295 idx += len;
1296 result[idx++] = ' ';
1299 free (name);
1302 /* Kill the last space and terminate the string. */
1303 if (idx == 0)
1304 result[0] = '\0';
1305 else
1306 result[idx - 1] = '\0';
1308 return result;