(Goals): Say that only first target in first rule is default goal.
[make.git] / function.c
blob9353a91650c220e6cebff30b43d23701d6f51ace
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 static char *string_glob ();
27 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
28 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
29 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
30 nonzero, substitutions are done only on matches which are complete
31 whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
32 done only at the ends of whitespace-delimited words. */
34 char *
35 subst_expand (o, text, subst, replace, slen, rlen, by_word, suffix_only)
36 char *o;
37 char *text;
38 char *subst, *replace;
39 unsigned int slen, rlen;
40 int by_word, suffix_only;
42 register char *t = text;
43 register char *p;
45 if (slen == 0 && !by_word && !suffix_only)
47 /* The first occurrence of "" in any string is its end. */
48 o = variable_buffer_output (o, t, strlen (t));
49 if (rlen > 0)
50 o = variable_buffer_output (o, replace, rlen);
51 return o;
56 if ((by_word | suffix_only) && slen == 0)
57 /* When matching by words, the empty string should match
58 the end of each word, rather than the end of the whole text. */
59 p = end_of_token (next_token (t));
60 else
62 p = sindex (t, 0, subst, slen);
63 if (p == 0)
65 /* No more matches. Output everything left on the end. */
66 o = variable_buffer_output (o, t, strlen (t));
67 return o;
71 /* Output everything before this occurrence of the string to replace. */
72 if (p > t)
73 o = variable_buffer_output (o, t, p - t);
75 /* If we're substituting only by fully matched words,
76 or only at the ends of words, check that this case qualifies. */
77 if ((by_word
78 && ((p > t && !isblank (p[-1]))
79 || (p[slen] != '\0' && !isblank (p[slen]))))
80 || (suffix_only
81 && (p[slen] != '\0' && !isblank (p[slen]))))
82 /* Struck out. Output the rest of the string that is
83 no longer to be replaced. */
84 o = variable_buffer_output (o, subst, slen);
85 else if (rlen > 0)
86 /* Output the replacement string. */
87 o = variable_buffer_output (o, replace, rlen);
89 /* Advance T past the string to be replaced. */
90 t = p + slen;
91 } while (*t != '\0');
93 return o;
97 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
98 and replacing strings matching PATTERN with REPLACE.
99 If PATTERN_PERCENT is not nil, PATTERN has already been
100 run through find_percent, and PATTERN_PERCENT is the result.
101 If REPLACE_PERCENT is not nil, REPLACE has already been
102 run through find_percent, and REPLACE_PERCENT is the result. */
104 char *
105 patsubst_expand (o, text, pattern, replace, pattern_percent, replace_percent)
106 char *o;
107 char *text;
108 register char *pattern, *replace;
109 register char *pattern_percent, *replace_percent;
111 register int pattern_prepercent_len, pattern_postpercent_len;
112 register int replace_prepercent_len, replace_postpercent_len;
113 register char *t;
114 unsigned int len;
115 int doneany = 0;
117 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
118 will be collapsed before we call subst_expand if PATTERN has no %. */
119 if (replace_percent == 0)
120 replace_percent = find_percent (replace);
121 if (replace_percent != 0)
123 /* Record the length of REPLACE before and after the % so
124 we don't have to compute these lengths more than once. */
125 replace_prepercent_len = replace_percent - replace;
126 replace_postpercent_len = strlen (replace_percent + 1);
128 else
129 /* We store the length of the replacement
130 so we only need to compute it once. */
131 replace_prepercent_len = strlen (replace);
133 if (pattern_percent == 0)
134 pattern_percent = find_percent (pattern);
135 if (pattern_percent == 0)
136 /* With no % in the pattern, this is just a simple substitution. */
137 return subst_expand (o, text, pattern, replace,
138 strlen (pattern), strlen (replace), 1, 0);
140 /* Record the length of PATTERN before and after the %
141 so we don't have to compute it more than once. */
142 pattern_prepercent_len = pattern_percent - pattern;
143 pattern_postpercent_len = strlen (pattern_percent + 1);
145 while ((t = find_next_token (&text, &len)) != 0)
147 int fail = 0;
149 /* Is it big enough to match? */
150 if (len < pattern_prepercent_len + pattern_postpercent_len)
151 fail = 1;
153 /* Does the prefix match? */
154 if (!fail && pattern_prepercent_len > 0
155 && (*t != *pattern
156 || t[pattern_prepercent_len - 1] != pattern_percent[-1]
157 || strncmp (t + 1, pattern + 1, pattern_prepercent_len - 1)))
158 fail = 1;
160 /* Does the suffix match? */
161 if (!fail && pattern_postpercent_len > 0
162 && (t[len - 1] != pattern_percent[pattern_postpercent_len]
163 || t[len - pattern_postpercent_len] != pattern_percent[1]
164 || strncmp (&t[len - pattern_postpercent_len],
165 &pattern_percent[1], pattern_postpercent_len - 1)))
166 fail = 1;
168 if (fail)
169 /* It didn't match. Output the string. */
170 o = variable_buffer_output (o, t, len);
171 else
173 /* It matched. Output the replacement. */
175 /* Output the part of the replacement before the %. */
176 o = variable_buffer_output (o, replace, replace_prepercent_len);
178 if (replace_percent != 0)
180 /* Output the part of the matched string that
181 matched the % in the pattern. */
182 o = variable_buffer_output (o, t + pattern_prepercent_len,
183 len - (pattern_prepercent_len
184 + pattern_postpercent_len));
185 /* Output the part of the replacement after the %. */
186 o = variable_buffer_output (o, replace_percent + 1,
187 replace_postpercent_len);
191 /* Output a space, but not if the replacement is "". */
192 if (fail || replace_prepercent_len > 0
193 || (replace_percent != 0 && len + replace_postpercent_len > 0))
195 o = variable_buffer_output (o, " ", 1);
196 doneany = 1;
199 if (doneany)
200 /* Kill the last space. */
201 --o;
203 return o;
206 /* Handle variable-expansion-time functions such as $(dir foo/bar) ==> foo/ */
208 /* These enumeration constants distinguish the
209 various expansion-time built-in functions. */
211 enum function
213 function_subst,
214 function_addsuffix,
215 function_addprefix,
216 function_dir,
217 function_notdir,
218 function_suffix,
219 function_basename,
220 function_wildcard,
221 function_firstword,
222 function_word,
223 function_words,
224 function_findstring,
225 function_strip,
226 function_join,
227 function_patsubst,
228 function_filter,
229 function_filter_out,
230 function_foreach,
231 function_sort,
232 function_origin,
233 function_shell,
234 function_invalid
237 /* Greater than the length of any function name. */
238 #define MAXFUNCTIONLEN 11
240 /* The function names and lengths of names, for looking them up. */
242 static struct
244 char *name;
245 unsigned int len;
246 enum function function;
247 } function_table[] =
249 { "subst", 5, function_subst },
250 { "addsuffix", 9, function_addsuffix },
251 { "addprefix", 9, function_addprefix },
252 { "dir", 3, function_dir },
253 { "notdir", 6, function_notdir },
254 { "suffix", 6, function_suffix },
255 { "basename", 8, function_basename },
256 { "wildcard", 8, function_wildcard },
257 { "firstword", 9, function_firstword },
258 { "word", 4, function_word },
259 { "words", 5, function_words },
260 { "findstring", 10, function_findstring },
261 { "strip", 5, function_strip },
262 { "join", 4, function_join },
263 { "patsubst", 8, function_patsubst },
264 { "filter", 6, function_filter },
265 { "filter-out", 10, function_filter_out },
266 { "foreach", 7, function_foreach },
267 { "sort", 4, function_sort },
268 { "origin", 6, function_origin },
269 { "shell", 5, function_shell },
270 { 0, 0, function_invalid }
273 /* Return 1 if PATTERN matches WORD, 0 if not. */
276 pattern_matches (pattern, percent, word)
277 register char *pattern, *percent, *word;
279 unsigned int sfxlen, wordlen;
281 if (percent == 0)
283 unsigned int len = strlen (pattern) + 1;
284 char *new = (char *) alloca (len);
285 bcopy (pattern, new, len);
286 pattern = new;
287 percent = find_percent (pattern);
288 if (percent == 0)
289 return streq (pattern, word);
292 sfxlen = strlen (percent + 1);
293 wordlen = strlen (word);
295 if (wordlen < (percent - pattern) + sfxlen
296 || strncmp (pattern, word, percent - pattern))
297 return 0;
299 return !strcmp (percent + 1, word + (wordlen - sfxlen));
302 int shell_function_pid = 0, shell_function_completed;
304 /* Perform the function specified by FUNCTION on the text at TEXT.
305 END is points to the end of the argument text (exclusive).
306 The output is written into VARIABLE_BUFFER starting at O. */
308 /* Note this absorbs a semicolon and is safe to use in conditionals. */
309 #define BADARGS(func) \
310 if (reading_filename != 0) \
311 makefile_fatal (reading_filename, *reading_lineno_ptr, \
312 "insufficient arguments to function `%s'", \
313 func); \
314 else \
315 fatal ("insufficient arguments to function `%s'", func)
317 static char *
318 expand_function (o, function, text, end)
319 char *o;
320 enum function function;
321 char *text;
322 char *end;
324 char *p, *p2, *p3;
325 unsigned int i, len;
326 int doneany = 0;
327 int count;
328 char endparen = *end, startparen = *end == ')' ? '(' : '{';
330 switch (function)
332 default:
333 abort ();
334 break;
336 case function_shell:
338 char **argv, **envp;
339 char *error_prefix;
340 int pipedes[2];
341 int pid;
343 /* Expand the command line. */
344 text = expand_argument (text, end);
346 /* Construct the argument list. */
347 argv = construct_command_argv (text, (char *) NULL, (struct file *) 0);
348 if (argv == 0)
349 break;
351 /* Using a target environment for `shell' loses in cases like:
352 export var = $(shell echo foobie)
353 because target_environment hits a loop trying to expand $(var)
354 to put it in the environment. This is even more confusing when
355 var was not explicitly exported, but just appeared in the
356 calling environment. */
357 #if 1
358 envp = environ;
359 #else
360 /* Construct the environment. */
361 envp = target_environment ((struct file *) 0);
362 #endif
364 /* For error messages. */
365 if (reading_filename != 0)
367 error_prefix = (char *) alloca (strlen (reading_filename) + 100);
368 sprintf (error_prefix,
369 "%s:%u: ", reading_filename, *reading_lineno_ptr);
371 else
372 error_prefix = "";
374 if (pipe (pipedes) < 0)
376 perror_with_name (error_prefix, "pipe");
377 break;
380 pid = vfork ();
381 if (pid < 0)
382 perror_with_name (error_prefix, "fork");
383 else if (pid == 0)
384 child_execute_job (0, pipedes[1], argv, envp);
385 else
387 /* We are the parent. */
389 char *buffer;
390 unsigned int maxlen;
391 int cc;
393 /* Free the storage only the child needed. */
394 free (argv[0]);
395 free ((char *) argv);
396 #if 0
397 for (i = 0; envp[i] != 0; ++i)
398 free (envp[i]);
399 free ((char *) envp);
400 #endif
402 /* Record the PID for reap_children. */
403 shell_function_pid = pid;
404 shell_function_completed = 0;
407 /* Set up and read from the pipe. */
409 maxlen = 200;
410 buffer = (char *) xmalloc (maxlen + 1);
412 /* Close the write side of the pipe. */
413 (void) close (pipedes[1]);
415 /* Read from the pipe until it gets EOF. */
416 i = 0;
419 if (i == maxlen)
421 maxlen += 512;
422 buffer = (char *) xrealloc (buffer, maxlen + 1);
425 errno = 0;
426 cc = read (pipedes[0], &buffer[i], maxlen - i);
427 if (cc > 0)
428 i += cc;
430 #ifdef EINTR
431 while (cc > 0 || errno == EINTR);
432 #else
433 while (cc > 0);
434 #endif
436 /* Close the read side of the pipe. */
437 (void) close (pipedes[0]);
439 /* Loop until child_handler sets shell_function_completed
440 to the status of our child shell. */
441 while (shell_function_completed == 0)
442 reap_children (1, 0);
444 shell_function_pid = 0;
446 /* The child_handler function will set shell_function_completed
447 to 1 when the child dies normally, or to -1 if it
448 dies with status 127, which is most likely an exec fail. */
450 if (shell_function_completed == -1)
452 /* This most likely means that the execvp failed,
453 so we should just write out the error message
454 that came in over the pipe from the child. */
455 fputs (buffer, stderr);
456 fflush (stderr);
458 else
460 /* The child finished normally. Replace all
461 newlines in its output with spaces, and put
462 that in the variable output buffer. */
463 if (i > 0)
465 if (buffer[i - 1] == '\n')
466 buffer[--i] = '\0';
467 else
468 buffer[i] = '\0';
469 p = buffer;
470 while ((p = index (p, '\n')) != 0)
471 *p++ = ' ';
472 o = variable_buffer_output (o, buffer, i);
476 free (buffer);
479 free (text);
480 break;
483 case function_origin:
484 /* Expand the argument. */
485 text = expand_argument (text, end);
488 register struct variable *v = lookup_variable (text, strlen (text));
489 if (v == 0)
490 o = variable_buffer_output (o, "undefined", 9);
491 else
492 switch (v->origin)
494 default:
495 case o_invalid:
496 abort ();
497 break;
498 case o_default:
499 o = variable_buffer_output (o, "default", 7);
500 break;
501 case o_env:
502 o = variable_buffer_output (o, "environment", 11);
503 break;
504 case o_file:
505 o = variable_buffer_output (o, "file", 4);
506 break;
507 case o_env_override:
508 o = variable_buffer_output (o, "environment override", 20);
509 break;
510 case o_command:
511 o = variable_buffer_output (o, "command line", 12);
512 break;
513 case o_override:
514 o = variable_buffer_output (o, "override", 8);
515 break;
516 case o_automatic:
517 o = variable_buffer_output (o, "automatic", 9);
518 break;
522 free (text);
523 break;
525 case function_sort:
526 /* Expand the argument. */
527 text = expand_argument (text, end);
530 char **words = (char **) xmalloc (10 * sizeof (char *));
531 unsigned int nwords = 10;
532 register unsigned int wordi = 0;
533 char *t;
535 /* Chop TEXT into words and put them in WORDS. */
536 t = text;
537 while ((p = find_next_token (&t, &len)) != 0)
539 if (wordi >= nwords - 1)
541 nwords *= 2;
542 words = (char **) xrealloc ((char *) words,
543 nwords * sizeof (char *));
545 words[wordi++] = savestring (p, len);
548 if (wordi > 0)
550 /* Now sort the list of words. */
551 qsort ((char *) words, wordi, sizeof (char *), alpha_compare);
553 /* Now write the sorted list. */
554 for (i = 0; i < wordi; ++i)
556 len = strlen (words[i]);
557 if (i == wordi - 1 || strlen (words[i + 1]) != len
558 || strcmp (words[i], words[i + 1]))
560 o = variable_buffer_output (o, words[i], len);
561 o = variable_buffer_output (o, " ", 1);
563 free (words[i]);
565 /* Kill the last space. */
566 --o;
569 free ((char *) words);
572 free (text);
573 break;
575 case function_foreach:
577 /* Get three comma-separated arguments but
578 expand only the first two. */
579 char *var, *list;
580 register struct variable *v;
582 count = 0;
583 for (p = text; p < end; ++p)
585 if (*p == startparen)
586 ++count;
587 else if (*p == endparen)
588 --count;
589 else if (*p == ',' && count <= 0)
590 break;
592 if (p == end)
593 BADARGS ("foreach");
594 var = expand_argument (text, p);
596 p2 = p + 1;
597 count = 0;
598 for (p = p2; p < end; ++p)
600 if (*p == startparen)
601 ++count;
602 else if (*p == endparen)
603 --count;
604 else if (*p == ',' && count <= 0)
605 break;
607 if (p == end)
608 BADARGS ("foreach");
609 list = expand_argument (p2, p);
611 ++p;
612 text = savestring (p, end - p);
614 push_new_variable_scope ();
615 v = define_variable (var, strlen (var), "", o_automatic, 0);
616 p3 = list;
617 while ((p = find_next_token (&p3, &len)) != 0)
619 char *result;
620 char save = p[len];
621 p[len] = '\0';
622 v->value = p;
623 result = allocated_variable_expand (text);
624 p[len] = save;
626 o = variable_buffer_output (o, result, strlen (result));
627 o = variable_buffer_output (o, " ", 1);
628 doneany = 1;
629 free (result);
631 if (doneany)
632 /* Kill the last space. */
633 --o;
635 pop_variable_scope ();
637 free (var);
638 free (list);
639 free (text);
641 break;
643 case function_filter:
644 case function_filter_out:
646 struct word
648 struct word *next;
649 char *word;
650 int matched;
651 } *words, *wordtail, *wp;
653 /* Get two comma-separated arguments and expand each one. */
654 count = 0;
655 for (p = text; p < end; ++p)
657 if (*p == startparen)
658 ++count;
659 else if (*p == endparen)
660 --count;
661 else if (*p == ',' && count <= 0)
662 break;
664 if (p == end)
665 BADARGS (function == function_filter ? "filter" : "filter-out");
666 p2 = expand_argument (text, p);
668 text = expand_argument (p + 1, end);
670 /* Chop TEXT up into words and then run each pattern through. */
671 words = wordtail = 0;
672 p3 = text;
673 while ((p = find_next_token (&p3, &len)) != 0)
675 struct word *w = (struct word *) alloca (sizeof (struct word));
676 if (words == 0)
677 words = w;
678 else
679 wordtail->next = w;
680 wordtail = w;
682 if (*p3 != '\0')
683 ++p3;
684 p[len] = '\0';
685 w->word = p;
686 w->matched = 0;
689 if (words != 0)
691 wordtail->next = 0;
693 /* Run each pattern through the words, killing words. */
694 p3 = p2;
695 while ((p = find_next_token (&p3, &len)) != 0)
697 char *percent;
698 char save = p[len];
699 p[len] = '\0';
701 percent = find_percent (p);
702 for (wp = words; wp != 0; wp = wp->next)
703 wp->matched |= (percent == 0 ? streq (p, wp->word)
704 : pattern_matches (p, percent, wp->word));
706 p[len] = save;
709 /* Output the words that matched (or didn't, for filter-out). */
710 for (wp = words; wp != 0; wp = wp->next)
711 if (function == function_filter ? wp->matched : !wp->matched)
713 o = variable_buffer_output (o, wp->word, strlen (wp->word));
714 o = variable_buffer_output (o, " ", 1);
715 doneany = 1;
717 if (doneany)
718 /* Kill the last space. */
719 --o;
722 free (p2);
723 free (text);
725 break;
727 case function_patsubst:
728 /* Get three comma-separated arguments and expand each one. */
729 count = 0;
730 for (p = text; p < end; ++p)
732 if (*p == startparen)
733 ++count;
734 else if (*p == endparen)
735 --count;
736 else if (*p == ',' && count <= 0)
737 break;
739 if (p == end)
740 BADARGS ("patsubst");
742 p2 = p;
743 count = 0;
744 for (++p; p < end; ++p)
746 if (*p == startparen)
747 ++count;
748 else if (*p == endparen)
749 --count;
750 else if (*p == ',' && count <= 0)
751 break;
753 if (p == end)
754 BADARGS ("patsubst");
756 text = expand_argument (text, p2);
757 p3 = expand_argument (p2 + 1, p);
758 p2 = expand_argument (p + 1, end);
760 o = patsubst_expand (o, p2, text, p3, (char *) 0, (char *) 0);
762 free (text);
763 free (p3);
764 free (p2);
765 break;
767 case function_join:
768 /* Get two comma-separated arguments and expand each one. */
769 count = 0;
770 for (p = text; p < end; ++p)
772 if (*p == startparen)
773 ++count;
774 else if (*p == endparen)
775 --count;
776 else if (*p == ',' && count <= 0)
777 break;
779 if (p == end)
780 BADARGS ("join");
781 text = expand_argument (text, p);
783 p = expand_argument (p + 1, end);
786 /* Write each word of the first argument directly followed
787 by the corresponding word of the second argument.
788 If the two arguments have a different number of words,
789 the excess words are just output separated by blanks. */
790 register char *tp, *pp;
791 p2 = text;
792 p3 = p;
795 unsigned int tlen, plen;
797 tp = find_next_token (&p2, &tlen);
798 if (tp != 0)
799 o = variable_buffer_output (o, tp, tlen);
801 pp = find_next_token (&p3, &plen);
802 if (pp != 0)
803 o = variable_buffer_output (o, pp, plen);
805 if (tp != 0 || pp != 0)
807 o = variable_buffer_output (o, " ", 1);
808 doneany = 1;
811 while (tp != 0 || pp != 0);
812 if (doneany)
813 /* Kill the last blank. */
814 --o;
817 free (text);
818 free (p);
819 break;
821 case function_strip:
822 /* Expand the argument. */
823 text = expand_argument (text, end);
825 p2 = text;
826 while ((p = find_next_token (&p2, &i)) != 0)
828 o = variable_buffer_output (o, p, i);
829 o = variable_buffer_output (o, " ", 1);
830 doneany = 1;
832 if (doneany)
833 /* Kill the last space. */
834 --o;
836 free (text);
837 break;
839 case function_wildcard:
840 text = expand_argument (text, end);
842 p = string_glob (text);
843 o = variable_buffer_output (o, p, strlen (p));
845 free (text);
846 break;
848 case function_subst:
849 /* Get three comma-separated arguments and expand each one. */
850 count = 0;
851 for (p = text; p < end; ++p)
853 if (*p == startparen)
854 ++count;
855 else if (*p == endparen)
856 --count;
857 else if (*p == ',' && count <= 0)
858 break;
860 if (p == end)
861 BADARGS ("subst");
863 p2 = p;
864 count = 0;
865 for (++p; p < end; ++p)
867 if (*p == startparen)
868 ++count;
869 else if (*p == endparen)
870 --count;
871 else if (*p == ',' && count <= 0)
872 break;
874 if (p == end)
875 BADARGS ("subst");
877 text = expand_argument (text, p2);
878 p3 = expand_argument (p2 + 1, p);
879 p2 = expand_argument (p + 1, end);
881 o = subst_expand (o, p2, text, p3, strlen (text), strlen (p3), 0, 0);
883 free (text);
884 free (p3);
885 free (p2);
886 break;
888 case function_firstword:
889 /* Expand the argument. */
890 text = expand_argument (text, end);
892 /* Find the first word in TEXT. */
893 p2 = text;
894 p = find_next_token (&p2, &i);
895 if (p != 0)
896 o = variable_buffer_output (o, p, i);
898 free (text);
899 break;
901 case function_word:
902 /* Get two comma-separated arguments and expand each one. */
903 count = 0;
904 for (p = text; p < end; ++p)
906 if (*p == startparen)
907 ++count;
908 else if (*p == endparen)
909 --count;
910 else if (*p == ',' && count <= 0)
911 break;
913 if (p == end)
914 BADARGS ("word");
915 text = expand_argument (text, p);
917 p3 = expand_argument (p + 1, end);
919 /* Check the first argument. */
920 for (p2 = text; *p2 != '\0'; ++p2)
921 if (*p2 < '0' || *p2 > '9')
923 if (reading_filename != 0)
924 makefile_fatal (reading_filename, *reading_lineno_ptr,
925 "non-numeric first argument to `word' function");
926 else
927 fatal ("non-numeric first argument to `word' function");
930 i = (unsigned int) atoi (text);
931 if (i == 0)
933 if (reading_filename != 0)
934 makefile_fatal (reading_filename, *reading_lineno_ptr,
935 "the `word' function takes a one-origin \
936 index argument");
937 else
938 fatal ("the `word' function takes a one-origin index argument");
941 p2 = p3;
942 while ((p = find_next_token (&p2, &len)) != 0)
943 if (--i == 0)
944 break;
945 if (i == 0)
946 o = variable_buffer_output (o, p, len);
948 free (text);
949 free (p3);
950 break;
952 case function_words:
953 /* Expand the argument. */
954 text = expand_argument (text, end);
956 i = 0;
957 p2 = text;
958 while (find_next_token (&p2, (unsigned int *) 0) != 0)
959 ++i;
962 char buf[20];
963 sprintf (buf, "%d", i);
964 o = variable_buffer_output (o, buf, strlen (buf));
967 free (text);
968 break;
970 case function_findstring:
971 /* Get two comma-separated arguments and expand each one. */
972 count = 0;
973 for (p = text; p < end; ++p)
975 if (*p == startparen)
976 ++count;
977 else if (*p == endparen)
978 --count;
979 else if (*p == ',' && count <= 0)
980 break;
982 if (p == end)
983 BADARGS ("findstring");
984 text = expand_argument (text, p);
986 p = expand_argument (p + 1, end);
988 /* Find the first occurrence of the first string in the second. */
989 i = strlen (text);
990 if (sindex (p, 0, text, i) != 0)
991 o = variable_buffer_output (o, text, i);
993 free (p);
994 free (text);
995 break;
997 case function_addsuffix:
998 case function_addprefix:
999 /* Get two comma-separated arguments and expand each one. */
1000 count = 0;
1001 for (p = text; p < end; ++p)
1003 if (*p == startparen)
1004 ++count;
1005 else if (*p == endparen)
1006 --count;
1007 else if (*p == ',' && count <= 0)
1008 break;
1010 if (p == end)
1011 BADARGS (function == function_addsuffix ? "addsuffix" : "addprefix");
1012 text = expand_argument (text, p);
1013 i = strlen (text);
1015 p2 = expand_argument (p + 1, end);
1017 p3 = p2;
1018 while ((p = find_next_token (&p3, &len)) != 0)
1020 if (function == function_addprefix)
1021 o = variable_buffer_output (o, text, i);
1022 o = variable_buffer_output (o, p, len);
1023 if (function == function_addsuffix)
1024 o = variable_buffer_output (o, text, i);
1025 o = variable_buffer_output (o, " ", 1);
1026 doneany = 1;
1028 if (doneany)
1029 /* Kill last space. */
1030 --o;
1032 free (p2);
1033 free (text);
1034 break;
1036 case function_dir:
1037 case function_basename:
1038 /* Expand the argument. */
1039 text = expand_argument (text, end);
1041 p3 = text;
1042 while ((p2 = find_next_token (&p3, &len)) != 0)
1044 p = p2 + len;
1045 while (p >= p2 && *p != (function == function_dir ? '/' : '.'))
1046 --p;
1047 if (p >= p2)
1049 if (function == function_dir)
1050 ++p;
1051 o = variable_buffer_output (o, p2, p - p2);
1053 else if (function == function_dir)
1054 o = variable_buffer_output (o, "./", 2);
1055 else
1056 /* The entire name is the basename. */
1057 o = variable_buffer_output (o, p2, len);
1059 o = variable_buffer_output (o, " ", 1);
1060 doneany = 1;
1062 if (doneany)
1063 /* Kill last space. */
1064 --o;
1066 free (text);
1067 break;
1069 case function_notdir:
1070 case function_suffix:
1071 /* Expand the argument. */
1072 text = expand_argument (text, end);
1074 p3 = text;
1075 while ((p2 = find_next_token (&p3, &len)) != 0)
1077 p = p2 + len;
1078 while (p >= p2 && *p != (function == function_notdir ? '/' : '.'))
1079 --p;
1080 if (p >= p2)
1082 if (function == function_notdir)
1083 ++p;
1084 o = variable_buffer_output (o, p, len - (p - p2));
1086 else if (function == function_notdir)
1087 o = variable_buffer_output (o, p2, len);
1089 if (function == function_notdir || p >= p2)
1091 o = variable_buffer_output (o, " ", 1);
1092 doneany = 1;
1095 if (doneany)
1096 /* Kill last space. */
1097 --o;
1099 free (text);
1100 break;
1103 return o;
1106 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1107 opening ( or { and is not null-terminated. If a function invocation
1108 is found, expand it into the buffer at *OP, updating *OP, incrementing
1109 *STRINGP past the reference and returning nonzero. If not, return zero. */
1112 handle_function (op, stringp)
1113 char **op;
1114 char **stringp;
1117 register unsigned int code;
1118 unsigned int maxlen;
1119 char *beg = *stringp + 1;
1120 char *endref;
1122 endref = lindex (beg, beg + MAXFUNCTIONLEN, '\0');
1123 maxlen = endref != 0 ? endref - beg : MAXFUNCTIONLEN;
1125 for (code = 0; function_table[code].name != 0; ++code)
1127 if (maxlen < function_table[code].len)
1128 continue;
1129 endref = beg + function_table[code].len;
1130 if (isblank (*endref)
1131 && !strncmp (function_table[code].name, beg,
1132 function_table[code].len))
1133 break;
1135 if (function_table[code].name != 0)
1137 /* We have found a call to an expansion-time function.
1138 Find the end of the arguments, and do the function. */
1140 char openparen = beg[-1], closeparen = openparen == '(' ? ')' : '}';
1141 int count = 0;
1142 char *argbeg;
1143 register char *p;
1145 /* Space after function name isn't part of the args. */
1146 p = next_token (endref);
1147 argbeg = p;
1149 /* Count nested use of whichever kind of parens we use,
1150 so that nested calls and variable refs work. */
1152 for (; *p != '\0'; ++p)
1154 if (*p == openparen)
1155 ++count;
1156 else if (*p == closeparen && --count < 0)
1157 break;
1160 if (count >= 0)
1162 static const char errmsg[]
1163 = "unterminated call to function `%s': missing `%c'";
1164 if (reading_filename == 0)
1165 fatal (errmsg, function_table[code].name, closeparen);
1166 else
1167 makefile_fatal (reading_filename, *reading_lineno_ptr, errmsg,
1168 function_table[code].name, closeparen);
1171 /* We found the end; expand the function call. */
1173 *op = expand_function (*op, function_table[code].function, argbeg, p);
1174 *stringp = p;
1175 return 1;
1178 return 0;
1181 /* Glob-expand LINE. The returned pointer is
1182 only good until the next call to string_glob. */
1184 static char *
1185 string_glob (line)
1186 char *line;
1188 static char *result = 0;
1189 static unsigned int length;
1190 register struct nameseq *chain;
1191 register unsigned int idx;
1193 chain = multi_glob (parse_file_seq
1194 (&line, '\0', sizeof (struct nameseq),
1195 /* We do not want parse_file_seq to strip `./'s.
1196 That would break examples like:
1197 $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)). */
1199 sizeof (struct nameseq));
1201 if (result == 0)
1203 length = 100;
1204 result = (char *) xmalloc (100);
1207 idx = 0;
1208 while (chain != 0)
1210 register char *name = chain->name;
1211 unsigned int len = strlen (name);
1213 struct nameseq *next = chain->next;
1214 free ((char *) chain);
1215 chain = next;
1217 /* multi_glob will pass names without globbing metacharacters
1218 through as is, but we want only files that actually exist. */
1219 if (file_exists_p (name))
1221 if (idx + len + 1 > length)
1223 length += (len + 1) * 2;
1224 result = (char *) xrealloc (result, length);
1226 bcopy (name, &result[idx], len);
1227 idx += len;
1228 result[idx++] = ' ';
1231 free (name);
1234 /* Kill the last space and terminate the string. */
1235 if (idx == 0)
1236 result[0] = '\0';
1237 else
1238 result[idx - 1] = '\0';
1240 return result;