Formerly read.c.~57~
[make.git] / function.c
blob6c394da8f8ef849ebcefc03e22042a6c679431c8
1 /* Variable function expansion for GNU Make.
2 Copyright (C) 1988, 1989, 1991, 1992, 1993 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;
54 while ((p = sindex (t, 0, subst, slen)) != 0)
56 /* Output everything before this occurrence of the string to replace. */
57 if (p > t)
58 o = variable_buffer_output (o, t, p - t);
60 /* If we're substituting only by fully matched words,
61 or only at the ends of words, check that this case qualifies. */
62 if ((by_word
63 && ((p > t && !isblank (p[-1]))
64 || (p[slen] != '\0' && !isblank (p[slen]))))
65 || (suffix_only
66 && (p[slen] != '\0' && !isblank (p[slen]))))
67 /* Struck out. Output the rest of the string that is
68 no longer to be replaced. */
69 o = variable_buffer_output (o, subst, slen);
70 else if (rlen > 0)
71 /* Output the replacement string. */
72 o = variable_buffer_output (o, replace, rlen);
74 /* Advance T past the string to be replaced. */
75 t = p + slen;
78 /* Output everything left on the end. */
79 if (*t != '\0')
80 o = variable_buffer_output (o, t, strlen (t));
82 return o;
86 /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
87 and replacing strings matching PATTERN with REPLACE.
88 If PATTERN_PERCENT is not nil, PATTERN has already been
89 run through find_percent, and PATTERN_PERCENT is the result.
90 If REPLACE_PERCENT is not nil, REPLACE has already been
91 run through find_percent, and REPLACE_PERCENT is the result. */
93 char *
94 patsubst_expand (o, text, pattern, replace, pattern_percent, replace_percent)
95 char *o;
96 char *text;
97 register char *pattern, *replace;
98 register char *pattern_percent, *replace_percent;
100 register int pattern_prepercent_len, pattern_postpercent_len;
101 register int replace_prepercent_len, replace_postpercent_len;
102 register char *t;
103 unsigned int len;
104 int doneany = 0;
106 /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
107 will be collapsed before we call subst_expand if PATTERN has no %. */
108 if (replace_percent == 0)
109 replace_percent = find_percent (replace);
110 if (replace_percent != 0)
112 /* Record the length of REPLACE before and after the % so
113 we don't have to compute these lengths more than once. */
114 replace_prepercent_len = replace_percent - replace;
115 replace_postpercent_len = strlen (replace_percent + 1);
117 else
118 /* We store the length of the replacement
119 so we only need to compute it once. */
120 replace_prepercent_len = strlen (replace);
122 if (pattern_percent == 0)
123 pattern_percent = find_percent (pattern);
124 if (pattern_percent == 0)
125 /* With no % in the pattern, this is just a simple substitution. */
126 return subst_expand (o, text, pattern, replace,
127 strlen (pattern), strlen (replace), 1, 0);
129 /* Record the length of PATTERN before and after the %
130 so we don't have to compute it more than once. */
131 pattern_prepercent_len = pattern_percent - pattern;
132 pattern_postpercent_len = strlen (pattern_percent + 1);
134 while ((t = find_next_token (&text, &len)) != 0)
136 int fail = 0;
138 /* Is it big enough to match? */
139 if (len < pattern_prepercent_len + pattern_postpercent_len)
140 fail = 1;
142 /* Does the prefix match? */
143 if (!fail && pattern_prepercent_len > 0
144 && (*t != *pattern
145 || t[pattern_prepercent_len - 1] != pattern_percent[-1]
146 || strncmp (t + 1, pattern + 1, pattern_prepercent_len - 1)))
147 fail = 1;
149 /* Does the suffix match? */
150 if (!fail && pattern_postpercent_len > 0
151 && (t[len - 1] != pattern_percent[pattern_postpercent_len]
152 || t[len - pattern_postpercent_len] != pattern_percent[1]
153 || strncmp (&t[len - pattern_postpercent_len],
154 &pattern_percent[1], pattern_postpercent_len - 1)))
155 fail = 1;
157 if (fail)
158 /* It didn't match. Output the string. */
159 o = variable_buffer_output (o, t, len);
160 else
162 /* It matched. Output the replacement. */
164 /* Output the part of the replacement before the %. */
165 o = variable_buffer_output (o, replace, replace_prepercent_len);
167 if (replace_percent != 0)
169 /* Output the part of the matched string that
170 matched the % in the pattern. */
171 o = variable_buffer_output (o, t + pattern_prepercent_len,
172 len - (pattern_prepercent_len
173 + pattern_postpercent_len));
174 /* Output the part of the replacement after the %. */
175 o = variable_buffer_output (o, replace_percent + 1,
176 replace_postpercent_len);
180 /* Output a space, but not if the replacement is "". */
181 if (fail || replace_prepercent_len > 0
182 || (replace_percent != 0 && len + replace_postpercent_len > 0))
184 o = variable_buffer_output (o, " ", 1);
185 doneany = 1;
188 if (doneany)
189 /* Kill the last space. */
190 --o;
192 return o;
195 /* Handle variable-expansion-time functions such as $(dir foo/bar) ==> foo/ */
197 /* These enumeration constants distinguish the
198 various expansion-time built-in functions. */
200 enum function
202 function_subst,
203 function_addsuffix,
204 function_addprefix,
205 function_dir,
206 function_notdir,
207 function_suffix,
208 function_basename,
209 function_wildcard,
210 function_firstword,
211 function_word,
212 function_words,
213 function_findstring,
214 function_strip,
215 function_join,
216 function_patsubst,
217 function_filter,
218 function_filter_out,
219 function_foreach,
220 function_sort,
221 function_origin,
222 function_shell,
223 function_invalid
226 /* Greater than the length of any function name. */
227 #define MAXFUNCTIONLEN 11
229 /* The function names and lengths of names, for looking them up. */
231 static struct
233 char *name;
234 unsigned int len;
235 enum function function;
236 } function_table[] =
238 { "subst", 5, function_subst },
239 { "addsuffix", 9, function_addsuffix },
240 { "addprefix", 9, function_addprefix },
241 { "dir", 3, function_dir },
242 { "notdir", 6, function_notdir },
243 { "suffix", 6, function_suffix },
244 { "basename", 8, function_basename },
245 { "wildcard", 8, function_wildcard },
246 { "firstword", 9, function_firstword },
247 { "word", 4, function_word },
248 { "words", 5, function_words },
249 { "findstring", 10, function_findstring },
250 { "strip", 5, function_strip },
251 { "join", 4, function_join },
252 { "patsubst", 8, function_patsubst },
253 { "filter", 6, function_filter },
254 { "filter-out", 10, function_filter_out },
255 { "foreach", 7, function_foreach },
256 { "sort", 4, function_sort },
257 { "origin", 6, function_origin },
258 { "shell", 5, function_shell },
259 { 0, 0, function_invalid }
262 /* Return 1 if PATTERN matches WORD, 0 if not. */
265 pattern_matches (pattern, percent, word)
266 register char *pattern, *percent, *word;
268 unsigned int sfxlen, wordlen;
270 if (percent == 0)
272 unsigned int len = strlen (pattern) + 1;
273 char *new = (char *) alloca (len);
274 bcopy (pattern, new, len);
275 pattern = new;
276 percent = find_percent (pattern);
277 if (percent == 0)
278 return streq (pattern, word);
281 sfxlen = strlen (percent + 1);
282 wordlen = strlen (word);
284 if (wordlen < (percent - pattern) + sfxlen
285 || strncmp (pattern, word, percent - pattern))
286 return 0;
288 return !strcmp (percent + 1, word + (wordlen - sfxlen));
291 int shell_function_pid = 0, shell_function_completed;
293 /* Perform the function specified by FUNCTION on the text at TEXT.
294 END is points to the end of the argument text (exclusive).
295 The output is written into VARIABLE_BUFFER starting at O. */
297 /* Note this absorbs a semicolon and is safe to use in conditionals. */
298 #define BADARGS(func) \
299 if (reading_filename != 0) \
300 makefile_fatal (reading_filename, *reading_lineno_ptr, \
301 "insufficient arguments to function `%s'", \
302 func); \
303 else \
304 fatal ("insufficient arguments to function `%s'", func)
306 static char *
307 expand_function (o, function, text, end)
308 char *o;
309 enum function function;
310 char *text;
311 char *end;
313 char *p, *p2, *p3;
314 unsigned int i, len;
315 int doneany = 0;
316 int count;
317 char endparen = *end, startparen = *end == ')' ? '(' : '{';
319 switch (function)
321 default:
322 abort ();
323 break;
325 case function_shell:
327 char **argv;
328 char *error_prefix;
329 int pipedes[2];
330 int pid;
332 /* Expand the command line. */
333 text = expand_argument (text, end);
335 /* Construct the argument list. */
336 argv = construct_command_argv (text, (char *) NULL, (struct file *) 0);
337 if (argv == 0)
338 break;
340 /* For error messages. */
341 if (reading_filename != 0)
343 error_prefix = (char *) alloca (strlen (reading_filename) + 100);
344 sprintf (error_prefix,
345 "%s:%u: ", reading_filename, *reading_lineno_ptr);
347 else
348 error_prefix = "";
350 if (pipe (pipedes) < 0)
352 perror_with_name (error_prefix, "pipe");
353 break;
356 pid = vfork ();
357 if (pid < 0)
358 perror_with_name (error_prefix, "fork");
359 else if (pid == 0)
360 child_execute_job (0, pipedes[1], argv, environ);
361 else
363 /* We are the parent. Set up and read from the pipe. */
364 char *buffer = (char *) xmalloc (201);
365 unsigned int maxlen = 200;
366 int cc;
368 /* Record the PID for child_handler. */
369 shell_function_pid = pid;
370 shell_function_completed = 0;
372 /* Close the write side of the pipe. */
373 (void) close (pipedes[1]);
375 /* Read from the pipe until it gets EOF. */
376 i = 0;
379 if (i == maxlen)
381 maxlen += 512;
382 buffer = (char *) xrealloc (buffer, maxlen + 1);
385 errno = 0;
386 cc = read (pipedes[0], &buffer[i], maxlen - i);
387 if (cc > 0)
388 i += cc;
390 #ifdef EINTR
391 while (cc > 0 || errno == EINTR);
392 #else
393 while (cc > 0);
394 #endif
396 /* Close the read side of the pipe. */
397 (void) close (pipedes[0]);
399 /* Loop until child_handler sets shell_function_completed
400 to the status of our child shell. */
401 while (shell_function_completed == 0)
402 reap_children (1, 0);
404 shell_function_pid = 0;
406 /* The child_handler function will set shell_function_completed
407 to 1 when the child dies normally, or to -1 if it
408 dies with status 127, which is most likely an exec fail. */
410 if (shell_function_completed == -1)
412 /* This most likely means that the execvp failed,
413 so we should just write out the error message
414 that came in over the pipe from the child. */
415 fputs (buffer, stderr);
416 fflush (stderr);
418 else
420 /* The child finished normally. Replace all
421 newlines in its output with spaces, and put
422 that in the variable output buffer. */
423 if (i > 0)
425 if (buffer[i - 1] == '\n')
426 buffer[--i] = '\0';
427 else
428 buffer[i] = '\0';
429 p = buffer;
430 while ((p = index (p, '\n')) != 0)
431 *p++ = ' ';
432 o = variable_buffer_output (o, buffer, i);
436 free (argv[0]);
437 free ((char *) argv);
438 free (buffer);
441 free (text);
442 break;
445 case function_origin:
446 /* Expand the argument. */
447 text = expand_argument (text, end);
450 register struct variable *v = lookup_variable (text, strlen (text));
451 if (v == 0)
452 o = variable_buffer_output (o, "undefined", 9);
453 else
454 switch (v->origin)
456 default:
457 case o_invalid:
458 abort ();
459 break;
460 case o_default:
461 o = variable_buffer_output (o, "default", 7);
462 break;
463 case o_env:
464 o = variable_buffer_output (o, "environment", 11);
465 break;
466 case o_file:
467 o = variable_buffer_output (o, "file", 4);
468 break;
469 case o_env_override:
470 o = variable_buffer_output (o, "environment override", 20);
471 break;
472 case o_command:
473 o = variable_buffer_output (o, "command line", 12);
474 break;
475 case o_override:
476 o = variable_buffer_output (o, "override", 8);
477 break;
478 case o_automatic:
479 o = variable_buffer_output (o, "automatic", 9);
480 break;
484 free (text);
485 break;
487 case function_sort:
488 /* Expand the argument. */
489 text = expand_argument (text, end);
492 char **words = (char **) xmalloc (10 * sizeof (char *));
493 unsigned int nwords = 10;
494 register unsigned int wordi = 0;
495 char *t;
497 /* Chop TEXT into words and put them in WORDS. */
498 t = text;
499 while ((p = find_next_token (&t, &len)) != 0)
501 if (wordi >= nwords - 1)
503 nwords += 5;
504 words = (char **) xrealloc ((char *) words,
505 nwords * sizeof (char *));
507 words[wordi++] = savestring (p, len);
510 if (wordi > 0)
512 /* Now sort the list of words. */
513 qsort ((char *) words, wordi, sizeof (char *), alpha_compare);
515 /* Now write the sorted list. */
516 for (i = 0; i < wordi; ++i)
518 len = strlen (words[i]);
519 if (i == wordi - 1 || strlen (words[i + 1]) != len
520 || strcmp (words[i], words[i + 1]))
522 o = variable_buffer_output (o, words[i], len);
523 o = variable_buffer_output (o, " ", 1);
525 free (words[i]);
527 /* Kill the last space. */
528 --o;
531 free ((char *) words);
534 free (text);
535 break;
537 case function_foreach:
539 /* Get three comma-separated arguments but
540 expand only the first two. */
541 char *var, *list;
542 register struct variable *v;
544 count = 0;
545 for (p = text; p < end; ++p)
547 if (*p == startparen)
548 ++count;
549 else if (*p == endparen)
550 --count;
551 else if (*p == ',' && count <= 0)
552 break;
554 if (p == end)
555 BADARGS ("foreach");
556 var = expand_argument (text, p);
558 p2 = p + 1;
559 count = 0;
560 for (p = p2; p < end; ++p)
562 if (*p == startparen)
563 ++count;
564 else if (*p == endparen)
565 --count;
566 else if (*p == ',' && count <= 0)
567 break;
569 if (p == end)
570 BADARGS ("foreach");
571 list = expand_argument (p2, p);
573 ++p;
574 text = savestring (p, end - p);
576 push_new_variable_scope ();
577 v = define_variable (var, strlen (var), "", o_automatic, 0);
578 p3 = list;
579 while ((p = find_next_token (&p3, &len)) != 0)
581 char *result;
582 char save = p[len];
583 p[len] = '\0';
584 v->value = p;
585 result = allocated_variable_expand (text);
586 p[len] = save;
588 o = variable_buffer_output (o, result, strlen (result));
589 o = variable_buffer_output (o, " ", 1);
590 doneany = 1;
591 free (result);
593 if (doneany)
594 /* Kill the last space. */
595 --o;
597 pop_variable_scope ();
599 free (var);
600 free (list);
601 free (text);
603 break;
605 case function_filter:
606 case function_filter_out:
608 struct word
610 struct word *next;
611 char *word;
612 int matched;
613 } *words, *wordtail, *wp;
615 /* Get two comma-separated arguments and expand each one. */
616 count = 0;
617 for (p = text; p < end; ++p)
619 if (*p == startparen)
620 ++count;
621 else if (*p == endparen)
622 --count;
623 else if (*p == ',' && count <= 0)
624 break;
626 if (p == end)
627 BADARGS (function == function_filter ? "filter" : "filter-out");
628 p2 = expand_argument (text, p);
630 text = expand_argument (p + 1, end);
632 /* Chop TEXT up into words and then run each pattern through. */
633 words = wordtail = 0;
634 p3 = text;
635 while ((p = find_next_token (&p3, &len)) != 0)
637 struct word *w = (struct word *) alloca (sizeof (struct word));
638 if (words == 0)
639 words = w;
640 else
641 wordtail->next = w;
642 wordtail = w;
644 if (*p3 != '\0')
645 ++p3;
646 p[len] = '\0';
647 w->word = p;
648 w->matched = 0;
651 if (words != 0)
653 wordtail->next = 0;
655 /* Run each pattern through the words, killing words. */
656 p3 = p2;
657 while ((p = find_next_token (&p3, &len)) != 0)
659 char *percent;
660 char save = p[len];
661 p[len] = '\0';
663 percent = find_percent (p);
664 for (wp = words; wp != 0; wp = wp->next)
665 wp->matched |= (percent == 0 ? streq (p, wp->word)
666 : pattern_matches (p, percent, wp->word));
668 p[len] = save;
671 /* Output the words that matched (or didn't, for filter-out). */
672 for (wp = words; wp != 0; wp = wp->next)
673 if (function == function_filter ? wp->matched : !wp->matched)
675 o = variable_buffer_output (o, wp->word, strlen (wp->word));
676 o = variable_buffer_output (o, " ", 1);
677 doneany = 1;
679 if (doneany)
680 /* Kill the last space. */
681 --o;
684 free (p2);
685 free (text);
687 break;
689 case function_patsubst:
690 /* Get three comma-separated arguments and expand each one. */
691 count = 0;
692 for (p = text; p < end; ++p)
694 if (*p == startparen)
695 ++count;
696 else if (*p == endparen)
697 --count;
698 else if (*p == ',' && count <= 0)
699 break;
701 if (p == end)
702 BADARGS ("patsubst");
704 p2 = p;
705 count = 0;
706 for (++p; p < end; ++p)
708 if (*p == startparen)
709 ++count;
710 else if (*p == endparen)
711 --count;
712 else if (*p == ',' && count <= 0)
713 break;
715 if (p == end)
716 BADARGS ("patsubst");
718 text = expand_argument (text, p2);
719 p3 = expand_argument (p2 + 1, p);
720 p2 = expand_argument (p + 1, end);
722 o = patsubst_expand (o, p2, text, p3, (char *) 0, (char *) 0);
724 free (text);
725 free (p3);
726 free (p2);
727 break;
729 case function_join:
730 /* Get two comma-separated arguments and expand each one. */
731 count = 0;
732 for (p = text; p < end; ++p)
734 if (*p == startparen)
735 ++count;
736 else if (*p == endparen)
737 --count;
738 else if (*p == ',' && count <= 0)
739 break;
741 if (p == end)
742 BADARGS ("join");
743 text = expand_argument (text, p);
745 p = expand_argument (p + 1, end);
748 /* Write each word of the first argument directly followed
749 by the corresponding word of the second argument.
750 If the two arguments have a different number of words,
751 the excess words are just output separated by blanks. */
752 register char *tp, *pp;
753 p2 = text;
754 p3 = p;
757 unsigned int tlen, plen;
759 tp = find_next_token (&p2, &tlen);
760 if (tp != 0)
761 o = variable_buffer_output (o, tp, tlen);
763 pp = find_next_token (&p3, &plen);
764 if (pp != 0)
765 o = variable_buffer_output (o, pp, plen);
767 if (tp != 0 || pp != 0)
769 o = variable_buffer_output (o, " ", 1);
770 doneany = 1;
773 while (tp != 0 || pp != 0);
774 if (doneany)
775 /* Kill the last blank. */
776 --o;
779 free (text);
780 free (p);
781 break;
783 case function_strip:
784 /* Expand the argument. */
785 text = expand_argument (text, end);
787 p2 = text;
788 while ((p = find_next_token (&p2, &i)) != 0)
790 o = variable_buffer_output (o, p, i);
791 o = variable_buffer_output (o, " ", 1);
792 doneany = 1;
794 if (doneany)
795 /* Kill the last space. */
796 --o;
798 free (text);
799 break;
801 case function_wildcard:
802 text = expand_argument (text, end);
804 p = string_glob (text);
805 o = variable_buffer_output (o, p, strlen (p));
807 free (text);
808 break;
810 case function_subst:
811 /* Get three comma-separated arguments and expand each one. */
812 count = 0;
813 for (p = text; 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 ("subst");
825 p2 = p;
826 count = 0;
827 for (++p; p < end; ++p)
829 if (*p == startparen)
830 ++count;
831 else if (*p == endparen)
832 --count;
833 else if (*p == ',' && count <= 0)
834 break;
836 if (p == end)
837 BADARGS ("subst");
839 text = expand_argument (text, p2);
840 p3 = expand_argument (p2 + 1, p);
841 p2 = expand_argument (p + 1, end);
843 o = subst_expand (o, p2, text, p3, strlen (text), strlen (p3), 0, 0);
845 free (text);
846 free (p3);
847 free (p2);
848 break;
850 case function_firstword:
851 /* Expand the argument. */
852 text = expand_argument (text, end);
854 /* Find the first word in TEXT. */
855 p2 = text;
856 p = find_next_token (&p2, &i);
857 if (p != 0)
858 o = variable_buffer_output (o, p, i);
860 free (text);
861 break;
863 case function_word:
864 /* Get two comma-separated arguments and expand each one. */
865 count = 0;
866 for (p = text; p < end; ++p)
868 if (*p == startparen)
869 ++count;
870 else if (*p == endparen)
871 --count;
872 else if (*p == ',' && count <= 0)
873 break;
875 if (p == end)
876 BADARGS ("word");
877 text = expand_argument (text, p);
879 p3 = expand_argument (p + 1, end);
881 /* Check the first argument. */
882 for (p2 = text; *p2 != '\0'; ++p2)
883 if (*p2 < '0' || *p2 > '9')
885 if (reading_filename != 0)
886 makefile_fatal (reading_filename, *reading_lineno_ptr,
887 "non-numeric first argument to `word' function");
888 else
889 fatal ("non-numeric first argument to `word' function");
892 i = (unsigned int) atoi (text);
893 if (i == 0)
895 if (reading_filename != 0)
896 makefile_fatal (reading_filename, *reading_lineno_ptr,
897 "the `word' function takes a one-origin \
898 index argument");
899 else
900 fatal ("the `word' function takes a one-origin index argument");
903 p2 = p3;
904 while ((p = find_next_token (&p2, &len)) != 0)
905 if (--i == 0)
906 break;
907 if (i == 0)
908 o = variable_buffer_output (o, p, len);
910 free (text);
911 free (p3);
912 break;
914 case function_words:
915 /* Expand the argument. */
916 text = expand_argument (text, end);
918 i = 0;
919 p2 = text;
920 while (find_next_token (&p2, (unsigned int *) 0) != 0)
921 ++i;
924 char buf[20];
925 sprintf (buf, "%d", i);
926 o = variable_buffer_output (o, buf, strlen (buf));
929 free (text);
930 break;
932 case function_findstring:
933 /* Get two comma-separated arguments and expand each one. */
934 count = 0;
935 for (p = text; p < end; ++p)
937 if (*p == startparen)
938 ++count;
939 else if (*p == endparen)
940 --count;
941 else if (*p == ',' && count <= 0)
942 break;
944 if (p == end)
945 BADARGS ("findstring");
946 text = expand_argument (text, p);
948 p = expand_argument (p + 1, end);
950 /* Find the first occurrence of the first string in the second. */
951 i = strlen (text);
952 if (sindex (p, 0, text, i) != 0)
953 o = variable_buffer_output (o, text, i);
955 free (p);
956 free (text);
957 break;
959 case function_addsuffix:
960 case function_addprefix:
961 /* Get two comma-separated arguments and expand each one. */
962 count = 0;
963 for (p = text; p < end; ++p)
965 if (*p == startparen)
966 ++count;
967 else if (*p == endparen)
968 --count;
969 else if (*p == ',' && count <= 0)
970 break;
972 if (p == end)
973 BADARGS (function == function_addsuffix ? "addsuffix" : "addprefix");
974 text = expand_argument (text, p);
975 i = strlen (text);
977 p2 = expand_argument (p + 1, end);
979 p3 = p2;
980 while ((p = find_next_token (&p3, &len)) != 0)
982 if (function == function_addprefix)
983 o = variable_buffer_output (o, text, i);
984 o = variable_buffer_output (o, p, len);
985 if (function == function_addsuffix)
986 o = variable_buffer_output (o, text, i);
987 o = variable_buffer_output (o, " ", 1);
988 doneany = 1;
990 if (doneany)
991 /* Kill last space. */
992 --o;
994 free (p2);
995 free (text);
996 break;
998 case function_dir:
999 case function_basename:
1000 /* Expand the argument. */
1001 text = expand_argument (text, end);
1003 p3 = text;
1004 while ((p2 = find_next_token (&p3, &len)) != 0)
1006 p = p2 + len;
1007 while (p >= p2 && *p != (function == function_dir ? '/' : '.'))
1008 --p;
1009 if (p >= p2)
1011 if (function == function_dir)
1012 ++p;
1013 o = variable_buffer_output (o, p2, p - p2);
1015 else if (function == function_dir)
1016 o = variable_buffer_output (o, "./", 2);
1017 else
1018 /* The entire name is the basename. */
1019 o = variable_buffer_output (o, p2, len);
1021 o = variable_buffer_output (o, " ", 1);
1022 doneany = 1;
1024 if (doneany)
1025 /* Kill last space. */
1026 --o;
1028 free (text);
1029 break;
1031 case function_notdir:
1032 case function_suffix:
1033 /* Expand the argument. */
1034 text = expand_argument (text, end);
1036 p3 = text;
1037 while ((p2 = find_next_token (&p3, &len)) != 0)
1039 p = p2 + len;
1040 while (p >= p2 && *p != (function == function_notdir ? '/' : '.'))
1041 --p;
1042 if (p >= p2)
1044 if (function == function_notdir)
1045 ++p;
1046 o = variable_buffer_output (o, p, len - (p - p2));
1048 else if (function == function_notdir)
1049 o = variable_buffer_output (o, p2, len);
1051 if (function == function_notdir || p >= p2)
1053 o = variable_buffer_output (o, " ", 1);
1054 doneany = 1;
1057 if (doneany)
1058 /* Kill last space. */
1059 --o;
1061 free (text);
1062 break;
1065 return o;
1068 /* Check for a function invocation in *STRINGP. *STRINGP points at the
1069 opening ( or { and is not null-terminated. If a function invocation
1070 is found, expand it into the buffer at *OP, updating *OP, incrementing
1071 *STRINGP past the reference and returning nonzero. If not, return zero. */
1074 handle_function (op, stringp)
1075 char **op;
1076 char **stringp;
1079 register unsigned int code;
1080 unsigned int maxlen;
1081 char *beg = *stringp + 1;
1082 char *endref;
1084 endref = lindex (beg, beg + MAXFUNCTIONLEN, '\0');
1085 maxlen = endref != 0 ? endref - beg : MAXFUNCTIONLEN;
1087 for (code = 0; function_table[code].name != 0; ++code)
1089 if (maxlen < function_table[code].len)
1090 continue;
1091 endref = beg + function_table[code].len;
1092 if (isblank (*endref)
1093 && !strncmp (function_table[code].name, beg,
1094 function_table[code].len))
1095 break;
1097 if (function_table[code].name != 0)
1099 /* We have found a call to an expansion-time function.
1100 Find the end of the arguments, and do the function. */
1102 char openparen = beg[-1], closeparen = openparen == '(' ? ')' : '}';
1103 int count = 0;
1104 char *argbeg;
1105 register char *p;
1107 /* Space after function name isn't part of the args. */
1108 p = next_token (endref);
1109 argbeg = p;
1111 /* Count nested use of whichever kind of parens we use,
1112 so that nested calls and variable refs work. */
1114 for (; *p != '\0'; ++p)
1116 if (*p == openparen)
1117 ++count;
1118 else if (*p == closeparen && --count < 0)
1119 break;
1122 /* We found the end; expand the function call. */
1124 *op = expand_function (*op, function_table[code].function, argbeg, p);
1125 *stringp = p;
1126 return 1;
1129 return 0;
1132 /* Glob-expand LINE. The returned pointer is
1133 only good until the next call to string_glob. */
1135 static char *
1136 string_glob (line)
1137 char *line;
1139 static char *result = 0;
1140 static unsigned int length;
1141 register struct nameseq *chain;
1142 register unsigned int idx;
1144 chain = multi_glob (parse_file_seq (&line, '\0', sizeof (struct nameseq), 0),
1145 sizeof (struct nameseq),
1146 /* We do not want parse_file_seq to strip `./'s.
1147 That would break examples like:
1148 $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)) */
1151 if (result == 0)
1153 length = 100;
1154 result = (char *) xmalloc (100);
1157 idx = 0;
1158 while (chain != 0)
1160 register char *name = chain->name;
1161 unsigned int len = strlen (name);
1163 struct nameseq *next = chain->next;
1164 free ((char *) chain);
1165 chain = next;
1167 /* multi_glob will pass names without globbing metacharacters
1168 through as is, but we want only files that actually exist. */
1169 if (file_exists_p (name))
1171 if (idx + len + 1 > length)
1173 length += (len + 1) * 2;
1174 result = (char *) xrealloc (result, length);
1176 bcopy (name, &result[idx], len);
1177 idx += len;
1178 result[idx++] = ' ';
1181 free (name);
1184 /* Kill the last space and terminate the string. */
1185 if (idx == 0)
1186 result[0] = '\0';
1187 else
1188 result[idx - 1] = '\0';
1190 return result;