.
[make.git] / function.c
blobe5b46d75bfbdbbe8d21cd437be0dcdde8cf88054
1 /* Variable function expansion for GNU Make.
2 Copyright (C) 1988, 89, 91, 92, 93, 94, 95 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 #include <fcntl.h>
28 #endif
30 static char *string_glob ();
32 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
33 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
34 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
35 nonzero, substitutions are done only on matches which are complete
36 whitespace-delimited words. If SUFFIX_ONLY is nonzero, substitutions are
37 done only at the ends of whitespace-delimited words. */
39 char *
40 subst_expand (o, text, subst, replace, slen, rlen, by_word, suffix_only)
41 char *o;
42 char *text;
43 char *subst, *replace;
44 unsigned int slen, rlen;
45 int by_word, suffix_only;
47 register char *t = text;
48 register char *p;
50 if (slen == 0 && !by_word && !suffix_only)
52 /* The first occurrence of "" in any string is its end. */
53 o = variable_buffer_output (o, t, strlen (t));
54 if (rlen > 0)
55 o = variable_buffer_output (o, replace, rlen);
56 return o;
61 if ((by_word | suffix_only) && slen == 0)
62 /* When matching by words, the empty string should match
63 the end of each word, rather than the end of the whole text. */
64 p = end_of_token (next_token (t));
65 else
67 p = sindex (t, 0, subst, slen);
68 if (p == 0)
70 /* No more matches. Output everything left on the end. */
71 o = variable_buffer_output (o, t, strlen (t));
72 return o;
76 /* Output everything before this occurrence of the string to replace. */
77 if (p > t)
78 o = variable_buffer_output (o, t, p - t);
80 /* If we're substituting only by fully matched words,
81 or only at the ends of words, check that this case qualifies. */
82 if ((by_word
83 && ((p > t && !isblank (p[-1]))
84 || (p[slen] != '\0' && !isblank (p[slen]))))
85 || (suffix_only
86 && (p[slen] != '\0' && !isblank (p[slen]))))
87 /* Struck out. Output the rest of the string that is
88 no longer to be replaced. */
89 o = variable_buffer_output (o, subst, slen);
90 else if (rlen > 0)
91 /* Output the replacement string. */
92 o = variable_buffer_output (o, replace, rlen);
94 /* Advance T past the string to be replaced. */
95 t = p + slen;
96 } while (*t != '\0');
98 return o;
102 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
103 and replacing strings matching PATTERN with REPLACE.
104 If PATTERN_PERCENT is not nil, PATTERN has already been
105 run through find_percent, and PATTERN_PERCENT is the result.
106 If REPLACE_PERCENT is not nil, REPLACE has already been
107 run through find_percent, and REPLACE_PERCENT is the result. */
109 char *
110 patsubst_expand (o, text, pattern, replace, pattern_percent, replace_percent)
111 char *o;
112 char *text;
113 register char *pattern, *replace;
114 register char *pattern_percent, *replace_percent;
116 register int pattern_prepercent_len, pattern_postpercent_len;
117 register int replace_prepercent_len, replace_postpercent_len;
118 register char *t;
119 unsigned int len;
120 int doneany = 0;
122 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
123 will be collapsed before we call subst_expand if PATTERN has no %. */
124 if (replace_percent == 0)
125 replace_percent = find_percent (replace);
126 if (replace_percent != 0)
128 /* Record the length of REPLACE before and after the % so
129 we don't have to compute these lengths more than once. */
130 replace_prepercent_len = replace_percent - replace;
131 replace_postpercent_len = strlen (replace_percent + 1);
133 else
134 /* We store the length of the replacement
135 so we only need to compute it once. */
136 replace_prepercent_len = strlen (replace);
138 if (pattern_percent == 0)
139 pattern_percent = find_percent (pattern);
140 if (pattern_percent == 0)
141 /* With no % in the pattern, this is just a simple substitution. */
142 return subst_expand (o, text, pattern, replace,
143 strlen (pattern), strlen (replace), 1, 0);
145 /* Record the length of PATTERN before and after the %
146 so we don't have to compute it more than once. */
147 pattern_prepercent_len = pattern_percent - pattern;
148 pattern_postpercent_len = strlen (pattern_percent + 1);
150 while ((t = find_next_token (&text, &len)) != 0)
152 int fail = 0;
154 /* Is it big enough to match? */
155 if (len < pattern_prepercent_len + pattern_postpercent_len)
156 fail = 1;
158 /* Does the prefix match? */
159 if (!fail && pattern_prepercent_len > 0
160 && (*t != *pattern
161 || t[pattern_prepercent_len - 1] != pattern_percent[-1]
162 || strncmp (t + 1, pattern + 1, pattern_prepercent_len - 1)))
163 fail = 1;
165 /* Does the suffix match? */
166 if (!fail && pattern_postpercent_len > 0
167 && (t[len - 1] != pattern_percent[pattern_postpercent_len]
168 || t[len - pattern_postpercent_len] != pattern_percent[1]
169 || strncmp (&t[len - pattern_postpercent_len],
170 &pattern_percent[1], pattern_postpercent_len - 1)))
171 fail = 1;
173 if (fail)
174 /* It didn't match. Output the string. */
175 o = variable_buffer_output (o, t, len);
176 else
178 /* It matched. Output the replacement. */
180 /* Output the part of the replacement before the %. */
181 o = variable_buffer_output (o, replace, replace_prepercent_len);
183 if (replace_percent != 0)
185 /* Output the part of the matched string that
186 matched the % in the pattern. */
187 o = variable_buffer_output (o, t + pattern_prepercent_len,
188 len - (pattern_prepercent_len
189 + pattern_postpercent_len));
190 /* Output the part of the replacement after the %. */
191 o = variable_buffer_output (o, replace_percent + 1,
192 replace_postpercent_len);
196 /* Output a space, but not if the replacement is "". */
197 if (fail || replace_prepercent_len > 0
198 || (replace_percent != 0 && len + replace_postpercent_len > 0))
200 o = variable_buffer_output (o, " ", 1);
201 doneany = 1;
204 if (doneany)
205 /* Kill the last space. */
206 --o;
208 return o;
211 /* Handle variable-expansion-time functions such as $(dir foo/bar) ==> foo/ */
213 /* These enumeration constants distinguish the
214 various expansion-time built-in functions. */
216 enum function
218 function_subst,
219 function_addsuffix,
220 function_addprefix,
221 function_dir,
222 function_notdir,
223 function_suffix,
224 function_basename,
225 function_wildcard,
226 function_firstword,
227 function_word,
228 function_words,
229 function_findstring,
230 function_strip,
231 function_join,
232 function_patsubst,
233 function_filter,
234 function_filter_out,
235 function_foreach,
236 function_sort,
237 function_origin,
238 function_shell,
239 function_invalid
242 /* Greater than the length of any function name. */
243 #define MAXFUNCTIONLEN 11
245 /* The function names and lengths of names, for looking them up. */
247 static struct
249 char *name;
250 unsigned int len;
251 enum function function;
252 } function_table[] =
254 { "subst", 5, function_subst },
255 { "addsuffix", 9, function_addsuffix },
256 { "addprefix", 9, function_addprefix },
257 { "dir", 3, function_dir },
258 { "notdir", 6, function_notdir },
259 { "suffix", 6, function_suffix },
260 { "basename", 8, function_basename },
261 { "wildcard", 8, function_wildcard },
262 { "firstword", 9, function_firstword },
263 { "word", 4, function_word },
264 { "words", 5, function_words },
265 { "findstring", 10, function_findstring },
266 { "strip", 5, function_strip },
267 { "join", 4, function_join },
268 { "patsubst", 8, function_patsubst },
269 { "filter", 6, function_filter },
270 { "filter-out", 10, function_filter_out },
271 { "foreach", 7, function_foreach },
272 { "sort", 4, function_sort },
273 { "origin", 6, function_origin },
274 { "shell", 5, function_shell },
275 { 0, 0, function_invalid }
278 /* Return 1 if PATTERN matches WORD, 0 if not. */
281 pattern_matches (pattern, percent, word)
282 register char *pattern, *percent, *word;
284 unsigned int sfxlen, wordlen;
286 if (percent == 0)
288 unsigned int len = strlen (pattern) + 1;
289 char *new = (char *) alloca (len);
290 bcopy (pattern, new, len);
291 pattern = new;
292 percent = find_percent (pattern);
293 if (percent == 0)
294 return streq (pattern, word);
297 sfxlen = strlen (percent + 1);
298 wordlen = strlen (word);
300 if (wordlen < (percent - pattern) + sfxlen
301 || strncmp (pattern, word, percent - pattern))
302 return 0;
304 return !strcmp (percent + 1, word + (wordlen - sfxlen));
307 int shell_function_pid = 0, shell_function_completed;
309 /* Perform the function specified by FUNCTION on the text at TEXT.
310 END is points to the end of the argument text (exclusive).
311 The output is written into VARIABLE_BUFFER starting at O. */
313 /* Note this absorbs a semicolon and is safe to use in conditionals. */
314 #define BADARGS(func) \
315 if (reading_filename != 0) \
316 makefile_fatal (reading_filename, *reading_lineno_ptr, \
317 "insufficient arguments to function `%s'", \
318 func); \
319 else \
320 fatal ("insufficient arguments to function `%s'", func)
322 static char *
323 expand_function (o, function, text, end)
324 char *o;
325 enum function function;
326 char *text;
327 char *end;
329 char *p, *p2, *p3;
330 unsigned int i, len;
331 int doneany = 0;
332 int count;
333 char endparen = *end, startparen = *end == ')' ? '(' : '{';
335 switch (function)
337 default:
338 abort ();
339 break;
341 case function_shell:
343 char **argv, **envp;
344 char *error_prefix;
345 int pipedes[2];
346 int pid;
348 /* Expand the command line. */
349 text = expand_argument (text, end);
351 /* Construct the argument list. */
352 argv = construct_command_argv (text, (char *) NULL, (struct file *) 0);
353 if (argv == 0)
354 break;
356 /* Using a target environment for `shell' loses in cases like:
357 export var = $(shell echo foobie)
358 because target_environment hits a loop trying to expand $(var)
359 to put it in the environment. This is even more confusing when
360 var was not explicitly exported, but just appeared in the
361 calling environment. */
362 #if 1
363 envp = environ;
364 #else
365 /* Construct the environment. */
366 envp = target_environment ((struct file *) 0);
367 #endif
369 /* For error messages. */
370 if (reading_filename != 0)
372 error_prefix = (char *) alloca (strlen (reading_filename) + 100);
373 sprintf (error_prefix,
374 "%s:%u: ", reading_filename, *reading_lineno_ptr);
376 else
377 error_prefix = "";
379 #ifndef __MSDOS__
380 if (pipe (pipedes) < 0)
382 perror_with_name (error_prefix, "pipe");
383 break;
386 pid = vfork ();
387 if (pid < 0)
388 perror_with_name (error_prefix, "fork");
389 else if (pid == 0)
390 child_execute_job (0, pipedes[1], argv, envp);
391 else
393 /* We are the parent. */
395 char *buffer;
396 unsigned int maxlen;
397 int cc;
399 /* Free the storage only the child needed. */
400 free (argv[0]);
401 free ((char *) argv);
402 #if 0
403 for (i = 0; envp[i] != 0; ++i)
404 free (envp[i]);
405 free ((char *) envp);
406 #endif
408 /* Record the PID for reap_children. */
409 shell_function_pid = pid;
410 shell_function_completed = 0;
413 /* Set up and read from the pipe. */
415 maxlen = 200;
416 buffer = (char *) xmalloc (maxlen + 1);
418 /* Close the write side of the pipe. */
419 (void) close (pipedes[1]);
421 /* Read from the pipe until it gets EOF. */
422 i = 0;
425 if (i == maxlen)
427 maxlen += 512;
428 buffer = (char *) xrealloc (buffer, maxlen + 1);
431 errno = 0;
432 cc = read (pipedes[0], &buffer[i], maxlen - i);
433 if (cc > 0)
434 i += cc;
436 #ifdef EINTR
437 while (cc > 0 || errno == EINTR);
438 #else
439 while (cc > 0);
440 #endif
442 /* Close the read side of the pipe. */
443 (void) close (pipedes[0]);
445 /* Loop until child_handler sets shell_function_completed
446 to the status of our child shell. */
447 while (shell_function_completed == 0)
448 reap_children (1, 0);
450 shell_function_pid = 0;
452 /* The child_handler function will set shell_function_completed
453 to 1 when the child dies normally, or to -1 if it
454 dies with status 127, which is most likely an exec fail. */
456 if (shell_function_completed == -1)
458 /* This most likely means that the execvp failed,
459 so we should just write out the error message
460 that came in over the pipe from the child. */
461 fputs (buffer, stderr);
462 fflush (stderr);
464 else
466 /* The child finished normally. Replace all
467 newlines in its output with spaces, and put
468 that in the variable output buffer. */
469 if (i > 0)
471 if (buffer[i - 1] == '\n')
472 buffer[--i] = '\0';
473 else
474 buffer[i] = '\0';
475 p = buffer;
476 while ((p = index (p, '\n')) != 0)
477 *p++ = ' ';
478 o = variable_buffer_output (o, buffer, i);
482 free (buffer);
484 #else /* MSDOS. */
486 /* MS-DOS can't do fork, but it can do spawn. However, this
487 means that we don't have an opportunity to reopen stdout to
488 trap it. Thus, we save our own stdout onto a new descriptor
489 and dup a temp file's descriptor onto our stdout temporarily.
490 After we spawn the shell program, we dup our own stdout back
491 to the stdout descriptor. The buffer reading is the same as
492 above, except that we're now reading from a file. */
494 int save_stdout;
495 int child_stdout;
496 char tmp_output[FILENAME_MAX];
497 FILE *child_stream;
498 unsigned int maxlen = 200;
499 int cc;
500 char *buffer;
502 strcpy (tmp_output, "shXXXXXX");
503 mktemp (tmp_output);
504 child_stdout = open (tmp_output,
505 O_WRONLY|O_CREAT|O_TRUNC|O_TEXT, 0644);
506 save_stdout = dup (1);
507 dup2 (child_stdout, 1);
508 spawnvp (P_WAIT, argv[0], argv);
509 dup2 (save_stdout, 1);
510 close (child_stdout);
511 close (save_stdout);
513 child_stdout = open (tmp_output, O_RDONLY|O_TEXT, 0644);
515 buffer = xmalloc (maxlen);
516 i = 0;
519 if (i == maxlen)
521 maxlen += 512;
522 buffer = (char *) xrealloc (buffer, maxlen + 1);
525 cc = read (child_stdout, &buffer[i], maxlen - i);
526 if (cc > 0)
527 i += cc;
528 } while (cc > 0);
530 close (child_stdout);
531 unlink (tmp_output);
533 if (i > 0)
535 if (buffer[i - 1] == '\n')
536 buffer[--i] = '\0';
537 else
538 buffer[i] = '\0';
539 p = buffer;
540 while ((p = index (p, '\n')) != 0)
541 *p++ = ' ';
542 o = variable_buffer_output (o, buffer, i);
544 free (buffer);
546 #endif /* Not MSDOS. */
548 free (text);
549 break;
552 case function_origin:
553 /* Expand the argument. */
554 text = expand_argument (text, end);
557 register struct variable *v = lookup_variable (text, strlen (text));
558 if (v == 0)
559 o = variable_buffer_output (o, "undefined", 9);
560 else
561 switch (v->origin)
563 default:
564 case o_invalid:
565 abort ();
566 break;
567 case o_default:
568 o = variable_buffer_output (o, "default", 7);
569 break;
570 case o_env:
571 o = variable_buffer_output (o, "environment", 11);
572 break;
573 case o_file:
574 o = variable_buffer_output (o, "file", 4);
575 break;
576 case o_env_override:
577 o = variable_buffer_output (o, "environment override", 20);
578 break;
579 case o_command:
580 o = variable_buffer_output (o, "command line", 12);
581 break;
582 case o_override:
583 o = variable_buffer_output (o, "override", 8);
584 break;
585 case o_automatic:
586 o = variable_buffer_output (o, "automatic", 9);
587 break;
591 free (text);
592 break;
594 case function_sort:
595 /* Expand the argument. */
596 text = expand_argument (text, end);
599 char **words = (char **) xmalloc (10 * sizeof (char *));
600 unsigned int nwords = 10;
601 register unsigned int wordi = 0;
602 char *t;
604 /* Chop TEXT into words and put them in WORDS. */
605 t = text;
606 while ((p = find_next_token (&t, &len)) != 0)
608 if (wordi >= nwords - 1)
610 nwords *= 2;
611 words = (char **) xrealloc ((char *) words,
612 nwords * sizeof (char *));
614 words[wordi++] = savestring (p, len);
617 if (wordi > 0)
619 /* Now sort the list of words. */
620 qsort ((char *) words, wordi, sizeof (char *), alpha_compare);
622 /* Now write the sorted list. */
623 for (i = 0; i < wordi; ++i)
625 len = strlen (words[i]);
626 if (i == wordi - 1 || strlen (words[i + 1]) != len
627 || strcmp (words[i], words[i + 1]))
629 o = variable_buffer_output (o, words[i], len);
630 o = variable_buffer_output (o, " ", 1);
632 free (words[i]);
634 /* Kill the last space. */
635 --o;
638 free ((char *) words);
641 free (text);
642 break;
644 case function_foreach:
646 /* Get three comma-separated arguments but
647 expand only the first two. */
648 char *var, *list;
649 register struct variable *v;
651 count = 0;
652 for (p = text; p < end; ++p)
654 if (*p == startparen)
655 ++count;
656 else if (*p == endparen)
657 --count;
658 else if (*p == ',' && count <= 0)
659 break;
661 if (p == end)
662 BADARGS ("foreach");
663 var = expand_argument (text, p);
665 p2 = p + 1;
666 count = 0;
667 for (p = p2; p < end; ++p)
669 if (*p == startparen)
670 ++count;
671 else if (*p == endparen)
672 --count;
673 else if (*p == ',' && count <= 0)
674 break;
676 if (p == end)
677 BADARGS ("foreach");
678 list = expand_argument (p2, p);
680 ++p;
681 text = savestring (p, end - p);
683 push_new_variable_scope ();
684 v = define_variable (var, strlen (var), "", o_automatic, 0);
685 p3 = list;
686 while ((p = find_next_token (&p3, &len)) != 0)
688 char *result;
689 char save = p[len];
690 p[len] = '\0';
691 v->value = p;
692 result = allocated_variable_expand (text);
693 p[len] = save;
695 o = variable_buffer_output (o, result, strlen (result));
696 o = variable_buffer_output (o, " ", 1);
697 doneany = 1;
698 free (result);
700 if (doneany)
701 /* Kill the last space. */
702 --o;
704 pop_variable_scope ();
706 free (var);
707 free (list);
708 free (text);
710 break;
712 case function_filter:
713 case function_filter_out:
715 struct word
717 struct word *next;
718 char *word;
719 int matched;
720 } *words, *wordtail, *wp;
722 /* Get two comma-separated arguments and expand each one. */
723 count = 0;
724 for (p = text; p < end; ++p)
726 if (*p == startparen)
727 ++count;
728 else if (*p == endparen)
729 --count;
730 else if (*p == ',' && count <= 0)
731 break;
733 if (p == end)
734 BADARGS (function == function_filter ? "filter" : "filter-out");
735 p2 = expand_argument (text, p);
737 text = expand_argument (p + 1, end);
739 /* Chop TEXT up into words and then run each pattern through. */
740 words = wordtail = 0;
741 p3 = text;
742 while ((p = find_next_token (&p3, &len)) != 0)
744 struct word *w = (struct word *) alloca (sizeof (struct word));
745 if (words == 0)
746 words = w;
747 else
748 wordtail->next = w;
749 wordtail = w;
751 if (*p3 != '\0')
752 ++p3;
753 p[len] = '\0';
754 w->word = p;
755 w->matched = 0;
758 if (words != 0)
760 wordtail->next = 0;
762 /* Run each pattern through the words, killing words. */
763 p3 = p2;
764 while ((p = find_next_token (&p3, &len)) != 0)
766 char *percent;
767 char save = p[len];
768 p[len] = '\0';
770 percent = find_percent (p);
771 for (wp = words; wp != 0; wp = wp->next)
772 wp->matched |= (percent == 0 ? streq (p, wp->word)
773 : pattern_matches (p, percent, wp->word));
775 p[len] = save;
778 /* Output the words that matched (or didn't, for filter-out). */
779 for (wp = words; wp != 0; wp = wp->next)
780 if (function == function_filter ? wp->matched : !wp->matched)
782 o = variable_buffer_output (o, wp->word, strlen (wp->word));
783 o = variable_buffer_output (o, " ", 1);
784 doneany = 1;
786 if (doneany)
787 /* Kill the last space. */
788 --o;
791 free (p2);
792 free (text);
794 break;
796 case function_patsubst:
797 /* Get three comma-separated arguments and expand each one. */
798 count = 0;
799 for (p = text; p < end; ++p)
801 if (*p == startparen)
802 ++count;
803 else if (*p == endparen)
804 --count;
805 else if (*p == ',' && count <= 0)
806 break;
808 if (p == end)
809 BADARGS ("patsubst");
811 p2 = p;
812 count = 0;
813 for (++p; p < end; ++p)
815 if (*p == startparen)
816 ++count;
817 else if (*p == endparen)
818 --count;
819 else if (*p == ',' && count <= 0)
820 break;
822 if (p == end)
823 BADARGS ("patsubst");
825 text = expand_argument (text, p2);
826 p3 = expand_argument (p2 + 1, p);
827 p2 = expand_argument (p + 1, end);
829 o = patsubst_expand (o, p2, text, p3, (char *) 0, (char *) 0);
831 free (text);
832 free (p3);
833 free (p2);
834 break;
836 case function_join:
837 /* Get two comma-separated arguments and expand each one. */
838 count = 0;
839 for (p = text; p < end; ++p)
841 if (*p == startparen)
842 ++count;
843 else if (*p == endparen)
844 --count;
845 else if (*p == ',' && count <= 0)
846 break;
848 if (p == end)
849 BADARGS ("join");
850 text = expand_argument (text, p);
852 p = expand_argument (p + 1, end);
855 /* Write each word of the first argument directly followed
856 by the corresponding word of the second argument.
857 If the two arguments have a different number of words,
858 the excess words are just output separated by blanks. */
859 register char *tp, *pp;
860 p2 = text;
861 p3 = p;
864 unsigned int tlen, plen;
866 tp = find_next_token (&p2, &tlen);
867 if (tp != 0)
868 o = variable_buffer_output (o, tp, tlen);
870 pp = find_next_token (&p3, &plen);
871 if (pp != 0)
872 o = variable_buffer_output (o, pp, plen);
874 if (tp != 0 || pp != 0)
876 o = variable_buffer_output (o, " ", 1);
877 doneany = 1;
880 while (tp != 0 || pp != 0);
881 if (doneany)
882 /* Kill the last blank. */
883 --o;
886 free (text);
887 free (p);
888 break;
890 case function_strip:
891 /* Expand the argument. */
892 text = expand_argument (text, end);
894 p2 = text;
895 while ((p = find_next_token (&p2, &i)) != 0)
897 o = variable_buffer_output (o, p, i);
898 o = variable_buffer_output (o, " ", 1);
899 doneany = 1;
901 if (doneany)
902 /* Kill the last space. */
903 --o;
905 free (text);
906 break;
908 case function_wildcard:
909 text = expand_argument (text, end);
911 p = string_glob (text);
912 o = variable_buffer_output (o, p, strlen (p));
914 free (text);
915 break;
917 case function_subst:
918 /* Get three comma-separated arguments and expand each one. */
919 count = 0;
920 for (p = text; p < end; ++p)
922 if (*p == startparen)
923 ++count;
924 else if (*p == endparen)
925 --count;
926 else if (*p == ',' && count <= 0)
927 break;
929 if (p == end)
930 BADARGS ("subst");
932 p2 = p;
933 count = 0;
934 for (++p; p < end; ++p)
936 if (*p == startparen)
937 ++count;
938 else if (*p == endparen)
939 --count;
940 else if (*p == ',' && count <= 0)
941 break;
943 if (p == end)
944 BADARGS ("subst");
946 text = expand_argument (text, p2);
947 p3 = expand_argument (p2 + 1, p);
948 p2 = expand_argument (p + 1, end);
950 o = subst_expand (o, p2, text, p3, strlen (text), strlen (p3), 0, 0);
952 free (text);
953 free (p3);
954 free (p2);
955 break;
957 case function_firstword:
958 /* Expand the argument. */
959 text = expand_argument (text, end);
961 /* Find the first word in TEXT. */
962 p2 = text;
963 p = find_next_token (&p2, &i);
964 if (p != 0)
965 o = variable_buffer_output (o, p, i);
967 free (text);
968 break;
970 case function_word:
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 ("word");
984 text = expand_argument (text, p);
986 p3 = expand_argument (p + 1, end);
988 /* Check the first argument. */
989 for (p2 = text; *p2 != '\0'; ++p2)
990 if (*p2 < '0' || *p2 > '9')
992 if (reading_filename != 0)
993 makefile_fatal (reading_filename, *reading_lineno_ptr,
994 "non-numeric first argument to `word' function");
995 else
996 fatal ("non-numeric first argument to `word' function");
999 i = (unsigned int) atoi (text);
1000 if (i == 0)
1002 if (reading_filename != 0)
1003 makefile_fatal (reading_filename, *reading_lineno_ptr,
1004 "the `word' function takes a one-origin \
1005 index argument");
1006 else
1007 fatal ("the `word' function takes a one-origin index argument");
1010 p2 = p3;
1011 while ((p = find_next_token (&p2, &len)) != 0)
1012 if (--i == 0)
1013 break;
1014 if (i == 0)
1015 o = variable_buffer_output (o, p, len);
1017 free (text);
1018 free (p3);
1019 break;
1021 case function_words:
1022 /* Expand the argument. */
1023 text = expand_argument (text, end);
1025 i = 0;
1026 p2 = text;
1027 while (find_next_token (&p2, (unsigned int *) 0) != 0)
1028 ++i;
1031 char buf[20];
1032 sprintf (buf, "%d", i);
1033 o = variable_buffer_output (o, buf, strlen (buf));
1036 free (text);
1037 break;
1039 case function_findstring:
1040 /* Get two comma-separated arguments and expand each one. */
1041 count = 0;
1042 for (p = text; p < end; ++p)
1044 if (*p == startparen)
1045 ++count;
1046 else if (*p == endparen)
1047 --count;
1048 else if (*p == ',' && count <= 0)
1049 break;
1051 if (p == end)
1052 BADARGS ("findstring");
1053 text = expand_argument (text, p);
1055 p = expand_argument (p + 1, end);
1057 /* Find the first occurrence of the first string in the second. */
1058 i = strlen (text);
1059 if (sindex (p, 0, text, i) != 0)
1060 o = variable_buffer_output (o, text, i);
1062 free (p);
1063 free (text);
1064 break;
1066 case function_addsuffix:
1067 case function_addprefix:
1068 /* Get two comma-separated arguments and expand each one. */
1069 count = 0;
1070 for (p = text; p < end; ++p)
1072 if (*p == startparen)
1073 ++count;
1074 else if (*p == endparen)
1075 --count;
1076 else if (*p == ',' && count <= 0)
1077 break;
1079 if (p == end)
1080 BADARGS (function == function_addsuffix ? "addsuffix" : "addprefix");
1081 text = expand_argument (text, p);
1082 i = strlen (text);
1084 p2 = expand_argument (p + 1, end);
1086 p3 = p2;
1087 while ((p = find_next_token (&p3, &len)) != 0)
1089 if (function == function_addprefix)
1090 o = variable_buffer_output (o, text, i);
1091 o = variable_buffer_output (o, p, len);
1092 if (function == function_addsuffix)
1093 o = variable_buffer_output (o, text, i);
1094 o = variable_buffer_output (o, " ", 1);
1095 doneany = 1;
1097 if (doneany)
1098 /* Kill last space. */
1099 --o;
1101 free (p2);
1102 free (text);
1103 break;
1105 case function_dir:
1106 case function_basename:
1107 /* Expand the argument. */
1108 text = expand_argument (text, end);
1110 p3 = text;
1111 while ((p2 = find_next_token (&p3, &len)) != 0)
1113 p = p2 + len;
1114 while (p >= p2 && *p != (function == function_dir ? '/' : '.'))
1115 --p;
1116 if (p >= p2)
1118 if (function == function_dir)
1119 ++p;
1120 o = variable_buffer_output (o, p2, p - p2);
1122 else if (function == function_dir)
1123 o = variable_buffer_output (o, "./", 2);
1124 else
1125 /* The entire name is the basename. */
1126 o = variable_buffer_output (o, p2, len);
1128 o = variable_buffer_output (o, " ", 1);
1129 doneany = 1;
1131 if (doneany)
1132 /* Kill last space. */
1133 --o;
1135 free (text);
1136 break;
1138 case function_notdir:
1139 case function_suffix:
1140 /* Expand the argument. */
1141 text = expand_argument (text, end);
1143 p3 = text;
1144 while ((p2 = find_next_token (&p3, &len)) != 0)
1146 p = p2 + len;
1147 while (p >= p2 && *p != (function == function_notdir ? '/' : '.'))
1148 --p;
1149 if (p >= p2)
1151 if (function == function_notdir)
1152 ++p;
1153 o = variable_buffer_output (o, p, len - (p - p2));
1155 else if (function == function_notdir)
1156 o = variable_buffer_output (o, p2, len);
1158 if (function == function_notdir || p >= p2)
1160 o = variable_buffer_output (o, " ", 1);
1161 doneany = 1;
1164 if (doneany)
1165 /* Kill last space. */
1166 --o;
1168 free (text);
1169 break;
1172 return o;
1175 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1176 opening ( or { and is not null-terminated. If a function invocation
1177 is found, expand it into the buffer at *OP, updating *OP, incrementing
1178 *STRINGP past the reference and returning nonzero. If not, return zero. */
1181 handle_function (op, stringp)
1182 char **op;
1183 char **stringp;
1186 register unsigned int code;
1187 unsigned int maxlen;
1188 char *beg = *stringp + 1;
1189 char *endref;
1191 endref = lindex (beg, beg + MAXFUNCTIONLEN, '\0');
1192 maxlen = endref != 0 ? endref - beg : MAXFUNCTIONLEN;
1194 for (code = 0; function_table[code].name != 0; ++code)
1196 if (maxlen < function_table[code].len)
1197 continue;
1198 endref = beg + function_table[code].len;
1199 if (isblank (*endref)
1200 && !strncmp (function_table[code].name, beg,
1201 function_table[code].len))
1202 break;
1204 if (function_table[code].name != 0)
1206 /* We have found a call to an expansion-time function.
1207 Find the end of the arguments, and do the function. */
1209 char openparen = beg[-1], closeparen = openparen == '(' ? ')' : '}';
1210 int count = 0;
1211 char *argbeg;
1212 register char *p;
1214 /* Space after function name isn't part of the args. */
1215 p = next_token (endref);
1216 argbeg = p;
1218 /* Count nested use of whichever kind of parens we use,
1219 so that nested calls and variable refs work. */
1221 for (; *p != '\0'; ++p)
1223 if (*p == openparen)
1224 ++count;
1225 else if (*p == closeparen && --count < 0)
1226 break;
1229 if (count >= 0)
1231 static const char errmsg[]
1232 = "unterminated call to function `%s': missing `%c'";
1233 if (reading_filename == 0)
1234 fatal (errmsg, function_table[code].name, closeparen);
1235 else
1236 makefile_fatal (reading_filename, *reading_lineno_ptr, errmsg,
1237 function_table[code].name, closeparen);
1240 /* We found the end; expand the function call. */
1242 *op = expand_function (*op, function_table[code].function, argbeg, p);
1243 *stringp = p;
1244 return 1;
1247 return 0;
1250 /* Glob-expand LINE. The returned pointer is
1251 only good until the next call to string_glob. */
1253 static char *
1254 string_glob (line)
1255 char *line;
1257 static char *result = 0;
1258 static unsigned int length;
1259 register struct nameseq *chain;
1260 register unsigned int idx;
1262 chain = multi_glob (parse_file_seq
1263 (&line, '\0', sizeof (struct nameseq),
1264 /* We do not want parse_file_seq to strip `./'s.
1265 That would break examples like:
1266 $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)). */
1268 sizeof (struct nameseq));
1270 if (result == 0)
1272 length = 100;
1273 result = (char *) xmalloc (100);
1276 idx = 0;
1277 while (chain != 0)
1279 register char *name = chain->name;
1280 unsigned int len = strlen (name);
1282 struct nameseq *next = chain->next;
1283 free ((char *) chain);
1284 chain = next;
1286 /* multi_glob will pass names without globbing metacharacters
1287 through as is, but we want only files that actually exist. */
1288 if (file_exists_p (name))
1290 if (idx + len + 1 > length)
1292 length += (len + 1) * 2;
1293 result = (char *) xrealloc (result, length);
1295 bcopy (name, &result[idx], len);
1296 idx += len;
1297 result[idx++] = ' ';
1300 free (name);
1303 /* Kill the last space and terminate the string. */
1304 if (idx == 0)
1305 result[0] = '\0';
1306 else
1307 result[idx - 1] = '\0';
1309 return result;