source: trunk/src/kmk/function.c@ 2156

Last change on this file since 2156 was 2154, checked in by bird, 17 years ago

kmk: $(translate ) changes and testcase.

  • Property svn:eol-style set to native
File size: 142.2 KB
Line 
1/* Builtin function expansion for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 3 of the License, or (at your option) any later
10version.
11
12GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#include "make.h"
20#include "filedef.h"
21#include "variable.h"
22#include "dep.h"
23#include "job.h"
24#include "commands.h"
25#include "debug.h"
26
27#ifdef _AMIGA
28#include "amiga.h"
29#endif
30
31#ifdef WINDOWS32 /* bird */
32# include "pathstuff.h"
33#endif
34
35#ifdef KMK_HELPERS
36# include "kbuild.h"
37#endif
38#ifdef CONFIG_WITH_PRINTF
39# include "kmkbuiltin.h"
40#endif
41#ifdef CONFIG_WITH_XARGS /* bird */
42# ifdef HAVE_LIMITS_H
43# include <limits.h>
44# endif
45#endif
46#include <assert.h> /* bird */
47
48#if defined (CONFIG_WITH_MATH) || defined (CONFIG_WITH_NANOTS) || defined (CONFIG_WITH_FILE_SIZE) /* bird */
49# include <ctype.h>
50typedef big_int math_int;
51static char *math_int_to_variable_buffer (char *, math_int);
52static math_int math_int_from_string (const char *str);
53#endif
54
55#ifdef CONFIG_WITH_NANOTS /* bird */
56# ifdef WINDOWS32
57# include <Windows.h>
58# endif
59#endif
60
61#ifdef __OS2__
62# define CONFIG_WITH_OS2_LIBPATH 1
63#endif
64#ifdef CONFIG_WITH_OS2_LIBPATH
65# define INCL_BASE
66# define INCL_ERRROS
67# include <os2.h>
68
69# define QHINF_EXEINFO 1 /* NE exeinfo. */
70# define QHINF_READRSRCTBL 2 /* Reads from the resource table. */
71# define QHINF_READFILE 3 /* Reads from the executable file. */
72# define QHINF_LIBPATHLENGTH 4 /* Gets the libpath length. */
73# define QHINF_LIBPATH 5 /* Gets the entire libpath. */
74# define QHINF_FIXENTRY 6 /* NE only */
75# define QHINF_STE 7 /* NE only */
76# define QHINF_MAPSEL 8 /* NE only */
77 extern APIRET APIENTRY DosQueryHeaderInfo(HMODULE hmod, ULONG ulIndex, PVOID pvBuffer, ULONG cbBuffer, ULONG ulSubFunction);
78#endif /* CONFIG_WITH_OS2_LIBPATH */
79
80
81struct function_table_entry
82 {
83 const char *name;
84 unsigned char len;
85 unsigned char minimum_args;
86 unsigned char maximum_args;
87 char expand_args;
88 char *(*func_ptr) (char *output, char **argv, const char *fname);
89 };
90
91static unsigned long
92function_table_entry_hash_1 (const void *keyv)
93{
94 const struct function_table_entry *key = keyv;
95 return_STRING_N_HASH_1 (key->name, key->len);
96}
97
98static unsigned long
99function_table_entry_hash_2 (const void *keyv)
100{
101 const struct function_table_entry *key = keyv;
102 return_STRING_N_HASH_2 (key->name, key->len);
103}
104
105static int
106function_table_entry_hash_cmp (const void *xv, const void *yv)
107{
108 const struct function_table_entry *x = xv;
109 const struct function_table_entry *y = yv;
110 int result = x->len - y->len;
111 if (result)
112 return result;
113 return_STRING_N_COMPARE (x->name, y->name, x->len);
114}
115
116static struct hash_table function_table;
117
118#ifdef CONFIG_WITH_MAKE_STATS
119long make_stats_allocations = 0;
120long make_stats_reallocations = 0;
121unsigned long make_stats_allocated = 0;
122unsigned long make_stats_ht_lookups = 0;
123unsigned long make_stats_ht_collisions = 0;
124#endif
125
126
127
128/* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
129 each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
130 the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
131 nonzero, substitutions are done only on matches which are complete
132 whitespace-delimited words. */
133
134char *
135subst_expand (char *o, const char *text, const char *subst, const char *replace,
136 unsigned int slen, unsigned int rlen, int by_word)
137{
138 const char *t = text;
139 const char *p;
140
141 if (slen == 0 && !by_word)
142 {
143 /* The first occurrence of "" in any string is its end. */
144 o = variable_buffer_output (o, t, strlen (t));
145 if (rlen > 0)
146 o = variable_buffer_output (o, replace, rlen);
147 return o;
148 }
149
150 do
151 {
152 if (by_word && slen == 0)
153 /* When matching by words, the empty string should match
154 the end of each word, rather than the end of the whole text. */
155 p = end_of_token (next_token (t));
156 else
157 {
158 p = strstr (t, subst);
159 if (p == 0)
160 {
161 /* No more matches. Output everything left on the end. */
162 o = variable_buffer_output (o, t, strlen (t));
163 return o;
164 }
165 }
166
167 /* Output everything before this occurrence of the string to replace. */
168 if (p > t)
169 o = variable_buffer_output (o, t, p - t);
170
171 /* If we're substituting only by fully matched words,
172 or only at the ends of words, check that this case qualifies. */
173 if (by_word
174 && ((p > text && !isblank ((unsigned char)p[-1]))
175 || (p[slen] != '\0' && !isblank ((unsigned char)p[slen]))))
176 /* Struck out. Output the rest of the string that is
177 no longer to be replaced. */
178 o = variable_buffer_output (o, subst, slen);
179 else if (rlen > 0)
180 /* Output the replacement string. */
181 o = variable_buffer_output (o, replace, rlen);
182
183 /* Advance T past the string to be replaced. */
184 t = p + slen;
185 } while (*t != '\0');
186
187 return o;
188}
189
190
191
192/* Store into VARIABLE_BUFFER at O the result of scanning TEXT
193 and replacing strings matching PATTERN with REPLACE.
194 If PATTERN_PERCENT is not nil, PATTERN has already been
195 run through find_percent, and PATTERN_PERCENT is the result.
196 If REPLACE_PERCENT is not nil, REPLACE has already been
197 run through find_percent, and REPLACE_PERCENT is the result.
198 Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
199 character _AFTER_ the %, not to the % itself.
200*/
201
202char *
203patsubst_expand_pat (char *o, const char *text,
204 const char *pattern, const char *replace,
205 const char *pattern_percent, const char *replace_percent)
206{
207 unsigned int pattern_prepercent_len, pattern_postpercent_len;
208 unsigned int replace_prepercent_len, replace_postpercent_len;
209 const char *t;
210 unsigned int len;
211 int doneany = 0;
212
213 /* Record the length of REPLACE before and after the % so we don't have to
214 compute these lengths more than once. */
215 if (replace_percent)
216 {
217 replace_prepercent_len = replace_percent - replace - 1;
218 replace_postpercent_len = strlen (replace_percent);
219 }
220 else
221 {
222 replace_prepercent_len = strlen (replace);
223 replace_postpercent_len = 0;
224 }
225
226 if (!pattern_percent)
227 /* With no % in the pattern, this is just a simple substitution. */
228 return subst_expand (o, text, pattern, replace,
229 strlen (pattern), strlen (replace), 1);
230
231 /* Record the length of PATTERN before and after the %
232 so we don't have to compute it more than once. */
233 pattern_prepercent_len = pattern_percent - pattern - 1;
234 pattern_postpercent_len = strlen (pattern_percent);
235
236 while ((t = find_next_token (&text, &len)) != 0)
237 {
238 int fail = 0;
239
240 /* Is it big enough to match? */
241 if (len < pattern_prepercent_len + pattern_postpercent_len)
242 fail = 1;
243
244 /* Does the prefix match? */
245 if (!fail && pattern_prepercent_len > 0
246 && (*t != *pattern
247 || t[pattern_prepercent_len - 1] != pattern_percent[-2]
248 || !strneq (t + 1, pattern + 1, pattern_prepercent_len - 1)))
249 fail = 1;
250
251 /* Does the suffix match? */
252 if (!fail && pattern_postpercent_len > 0
253 && (t[len - 1] != pattern_percent[pattern_postpercent_len - 1]
254 || t[len - pattern_postpercent_len] != *pattern_percent
255 || !strneq (&t[len - pattern_postpercent_len],
256 pattern_percent, pattern_postpercent_len - 1)))
257 fail = 1;
258
259 if (fail)
260 /* It didn't match. Output the string. */
261 o = variable_buffer_output (o, t, len);
262 else
263 {
264 /* It matched. Output the replacement. */
265
266 /* Output the part of the replacement before the %. */
267 o = variable_buffer_output (o, replace, replace_prepercent_len);
268
269 if (replace_percent != 0)
270 {
271 /* Output the part of the matched string that
272 matched the % in the pattern. */
273 o = variable_buffer_output (o, t + pattern_prepercent_len,
274 len - (pattern_prepercent_len
275 + pattern_postpercent_len));
276 /* Output the part of the replacement after the %. */
277 o = variable_buffer_output (o, replace_percent,
278 replace_postpercent_len);
279 }
280 }
281
282 /* Output a space, but not if the replacement is "". */
283 if (fail || replace_prepercent_len > 0
284 || (replace_percent != 0 && len + replace_postpercent_len > 0))
285 {
286 o = variable_buffer_output (o, " ", 1);
287 doneany = 1;
288 }
289 }
290#ifndef CONFIG_WITH_VALUE_LENGTH
291 if (doneany)
292 /* Kill the last space. */
293 --o;
294#else
295 /* Kill the last space and make sure there is a terminator there
296 so that strcache_add_len doesn't have to do a lot of exacty work
297 when expand_deps sends the output its way. */
298 if (doneany)
299 *--o = '\0';
300 else
301 o = variable_buffer_output (o, "\0", 1) - 1;
302#endif
303
304 return o;
305}
306
307/* Store into VARIABLE_BUFFER at O the result of scanning TEXT
308 and replacing strings matching PATTERN with REPLACE.
309 If PATTERN_PERCENT is not nil, PATTERN has already been
310 run through find_percent, and PATTERN_PERCENT is the result.
311 If REPLACE_PERCENT is not nil, REPLACE has already been
312 run through find_percent, and REPLACE_PERCENT is the result.
313 Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
314 character _AFTER_ the %, not to the % itself.
315*/
316
317char *
318patsubst_expand (char *o, const char *text, char *pattern, char *replace)
319{
320 const char *pattern_percent = find_percent (pattern);
321 const char *replace_percent = find_percent (replace);
322
323 /* If there's a percent in the pattern or replacement skip it. */
324 if (replace_percent)
325 ++replace_percent;
326 if (pattern_percent)
327 ++pattern_percent;
328
329 return patsubst_expand_pat (o, text, pattern, replace,
330 pattern_percent, replace_percent);
331}
332
333
334#if defined (CONFIG_WITH_OPTIMIZATION_HACKS) || defined (CONFIG_WITH_VALUE_LENGTH)
335
336/* Char map containing the valid function name characters. */
337char func_char_map[256];
338
339/* Do the hash table lookup. */
340
341MY_INLINE const struct function_table_entry *
342lookup_function_in_hash_tab (const char *s, unsigned char len)
343{
344 struct function_table_entry function_table_entry_key;
345 function_table_entry_key.name = s;
346 function_table_entry_key.len = len;
347
348 return hash_find_item (&function_table, &function_table_entry_key);
349}
350
351/* Look up a function by name. */
352
353MY_INLINE const struct function_table_entry *
354lookup_function (const char *s, unsigned int len)
355{
356 unsigned char ch;
357# if 0 /* insane loop unroll */
358
359 if (len > MAX_FUNCTION_LENGTH)
360 len = MAX_FUNCTION_LENGTH + 1;
361
362# define X(idx) \
363 if (!func_char_map[ch = s[idx]]) \
364 { \
365 if (isblank (ch)) \
366 return lookup_function_in_hash_tab (s, idx); \
367 return 0; \
368 }
369# define Z(idx) \
370 return lookup_function_in_hash_tab (s, idx);
371
372 switch (len)
373 {
374 default:
375 assert (0);
376 case 0: return 0;
377 case 1: return 0;
378 case 2: X(0); X(1); Z(2);
379 case 3: X(0); X(1); X(2); Z(3);
380 case 4: X(0); X(1); X(2); X(3); Z(4);
381 case 5: X(0); X(1); X(2); X(3); X(4); Z(5);
382 case 6: X(0); X(1); X(2); X(3); X(4); X(5); Z(6);
383 case 7: X(0); X(1); X(2); X(3); X(4); X(5); X(6); Z(7);
384 case 8: X(0); X(1); X(2); X(3); X(4); X(5); X(6); X(7); Z(8);
385 case 9: X(0); X(1); X(2); X(3); X(4); X(5); X(6); X(7); X(8); Z(9);
386 case 10: X(0); X(1); X(2); X(3); X(4); X(5); X(6); X(7); X(8); X(9); Z(10);
387 case 11: X(0); X(1); X(2); X(3); X(4); X(5); X(6); X(7); X(8); X(9); X(10); Z(11);
388 case 12: X(0); X(1); X(2); X(3); X(4); X(5); X(6); X(7); X(8); X(9); X(10); X(11); Z(12);
389 case 13: X(0); X(1); X(2); X(3); X(4); X(5); X(6); X(7); X(8); X(9); X(10); X(11); X(12);
390 if ((ch = s[12]) == '\0' || isblank (ch))
391 return lookup_function_in_hash_tab (s, 12);
392 return 0;
393 }
394# undef Z
395# undef X
396
397# else /* normal loop */
398 const char *e = s;
399 if (len > MAX_FUNCTION_LENGTH)
400 len = MAX_FUNCTION_LENGTH;
401 while (func_char_map[ch = *e])
402 {
403 if (!len--)
404 return 0;
405 e++;
406 }
407 if (ch == '\0' || isblank (ch))
408 return lookup_function_in_hash_tab (s, e - s);
409 return 0;
410# endif /* normal loop */
411}
412
413#else /* original code */
414/* Look up a function by name. */
415
416static const struct function_table_entry *
417lookup_function (const char *s)
418{
419 const char *e = s;
420 while (*e && ( (*e >= 'a' && *e <= 'z') || *e == '-'))
421 e++;
422 if (*e == '\0' || isblank ((unsigned char) *e))
423 {
424 struct function_table_entry function_table_entry_key;
425 function_table_entry_key.name = s;
426 function_table_entry_key.len = e - s;
427
428 return hash_find_item (&function_table, &function_table_entry_key);
429 }
430 return 0;
431}
432#endif /* original code */
433
434
435
436/* Return 1 if PATTERN matches STR, 0 if not. */
437
438int
439pattern_matches (const char *pattern, const char *percent, const char *str)
440{
441 unsigned int sfxlen, strlength;
442
443 if (percent == 0)
444 {
445 unsigned int len = strlen (pattern) + 1;
446 char *new_chars = alloca (len);
447 memcpy (new_chars, pattern, len);
448 percent = find_percent (new_chars);
449 if (percent == 0)
450 return streq (new_chars, str);
451 pattern = new_chars;
452 }
453
454 sfxlen = strlen (percent + 1);
455 strlength = strlen (str);
456
457 if (strlength < (percent - pattern) + sfxlen
458 || !strneq (pattern, str, percent - pattern))
459 return 0;
460
461 return !strcmp (percent + 1, str + (strlength - sfxlen));
462}
463
464
465
466/* Find the next comma or ENDPAREN (counting nested STARTPAREN and
467 ENDPARENtheses), starting at PTR before END. Return a pointer to
468 next character.
469
470 If no next argument is found, return NULL.
471*/
472
473static char *
474find_next_argument (char startparen, char endparen,
475 const char *ptr, const char *end)
476{
477 int count = 0;
478
479 for (; ptr < end; ++ptr)
480 if (*ptr == startparen)
481 ++count;
482
483 else if (*ptr == endparen)
484 {
485 --count;
486 if (count < 0)
487 return NULL;
488 }
489
490 else if (*ptr == ',' && !count)
491 return (char *)ptr;
492
493 /* We didn't find anything. */
494 return NULL;
495}
496
497
498
499/* Glob-expand LINE. The returned pointer is
500 only good until the next call to string_glob. */
501
502static char *
503string_glob (char *line)
504{
505 static char *result = 0;
506 static unsigned int length;
507 struct nameseq *chain;
508 unsigned int idx;
509
510#ifndef CONFIG_WITH_ALLOC_CACHES
511 chain = multi_glob (parse_file_seq
512 (&line, '\0', sizeof (struct nameseq),
513 /* We do not want parse_file_seq to strip `./'s.
514 That would break examples like:
515 $(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
516 0),
517 sizeof (struct nameseq));
518#else /* CONFIG_WITH_ALLOC_CACHES */
519 chain = multi_glob (parse_file_seq
520 (&line, '\0', &nameseq_cache,
521 /* We do not want parse_file_seq to strip `./'s.
522 That would break examples like:
523 $(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
524 0),
525 &nameseq_cache);
526#endif /* CONFIG_WITH_ALLOC_CACHES */
527
528 if (result == 0)
529 {
530 length = 100;
531 result = xmalloc (100);
532 }
533
534 idx = 0;
535 while (chain != 0)
536 {
537 const char *name = chain->name;
538 unsigned int len = strlen (name);
539
540 struct nameseq *next = chain->next;
541#ifndef CONFIG_WITH_ALLOC_CACHES
542 free (chain);
543#else
544 alloccache_free (&nameseq_cache, chain);
545#endif
546 chain = next;
547
548 /* multi_glob will pass names without globbing metacharacters
549 through as is, but we want only files that actually exist. */
550 if (file_exists_p (name))
551 {
552 if (idx + len + 1 > length)
553 {
554 length += (len + 1) * 2;
555 result = xrealloc (result, length);
556 }
557 memcpy (&result[idx], name, len);
558 idx += len;
559 result[idx++] = ' ';
560 }
561 }
562
563 /* Kill the last space and terminate the string. */
564 if (idx == 0)
565 result[0] = '\0';
566 else
567 result[idx - 1] = '\0';
568
569 return result;
570}
571
572
573/*
574 Builtin functions
575 */
576
577static char *
578func_patsubst (char *o, char **argv, const char *funcname UNUSED)
579{
580 o = patsubst_expand (o, argv[2], argv[0], argv[1]);
581 return o;
582}
583
584
585static char *
586func_join (char *o, char **argv, const char *funcname UNUSED)
587{
588 int doneany = 0;
589
590 /* Write each word of the first argument directly followed
591 by the corresponding word of the second argument.
592 If the two arguments have a different number of words,
593 the excess words are just output separated by blanks. */
594 const char *tp;
595 const char *pp;
596 const char *list1_iterator = argv[0];
597 const char *list2_iterator = argv[1];
598 do
599 {
600 unsigned int len1, len2;
601
602 tp = find_next_token (&list1_iterator, &len1);
603 if (tp != 0)
604 o = variable_buffer_output (o, tp, len1);
605
606 pp = find_next_token (&list2_iterator, &len2);
607 if (pp != 0)
608 o = variable_buffer_output (o, pp, len2);
609
610 if (tp != 0 || pp != 0)
611 {
612 o = variable_buffer_output (o, " ", 1);
613 doneany = 1;
614 }
615 }
616 while (tp != 0 || pp != 0);
617 if (doneany)
618 /* Kill the last blank. */
619 --o;
620
621 return o;
622}
623
624
625static char *
626func_origin (char *o, char **argv, const char *funcname UNUSED)
627{
628 /* Expand the argument. */
629 struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
630 if (v == 0)
631 o = variable_buffer_output (o, "undefined", 9);
632 else
633 switch (v->origin)
634 {
635 default:
636 case o_invalid:
637 abort ();
638 break;
639 case o_default:
640 o = variable_buffer_output (o, "default", 7);
641 break;
642 case o_env:
643 o = variable_buffer_output (o, "environment", 11);
644 break;
645 case o_file:
646 o = variable_buffer_output (o, "file", 4);
647 break;
648 case o_env_override:
649 o = variable_buffer_output (o, "environment override", 20);
650 break;
651 case o_command:
652 o = variable_buffer_output (o, "command line", 12);
653 break;
654 case o_override:
655 o = variable_buffer_output (o, "override", 8);
656 break;
657 case o_automatic:
658 o = variable_buffer_output (o, "automatic", 9);
659 break;
660#ifdef CONFIG_WITH_LOCAL_VARIABLES
661 case o_local:
662 o = variable_buffer_output (o, "local", 5);
663 break;
664#endif
665 }
666
667 return o;
668}
669
670static char *
671func_flavor (char *o, char **argv, const char *funcname UNUSED)
672{
673 struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
674
675 if (v == 0)
676 o = variable_buffer_output (o, "undefined", 9);
677 else
678 if (v->recursive)
679 o = variable_buffer_output (o, "recursive", 9);
680 else
681 o = variable_buffer_output (o, "simple", 6);
682
683 return o;
684}
685
686#ifdef VMS
687# define IS_PATHSEP(c) ((c) == ']')
688#else
689# ifdef HAVE_DOS_PATHS
690# define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
691# else
692# define IS_PATHSEP(c) ((c) == '/')
693# endif
694#endif
695
696
697static char *
698func_notdir_suffix (char *o, char **argv, const char *funcname)
699{
700 /* Expand the argument. */
701 const char *list_iterator = argv[0];
702 const char *p2;
703 int doneany =0;
704 unsigned int len=0;
705
706 int is_suffix = streq (funcname, "suffix");
707 int is_notdir = !is_suffix;
708 while ((p2 = find_next_token (&list_iterator, &len)) != 0)
709 {
710 const char *p = p2 + len;
711
712
713 while (p >= p2 && (!is_suffix || *p != '.'))
714 {
715 if (IS_PATHSEP (*p))
716 break;
717 --p;
718 }
719
720 if (p >= p2)
721 {
722 if (is_notdir)
723 ++p;
724 else if (*p != '.')
725 continue;
726 o = variable_buffer_output (o, p, len - (p - p2));
727 }
728#ifdef HAVE_DOS_PATHS
729 /* Handle the case of "d:foo/bar". */
730 else if (streq (funcname, "notdir") && p2[0] && p2[1] == ':')
731 {
732 p = p2 + 2;
733 o = variable_buffer_output (o, p, len - (p - p2));
734 }
735#endif
736 else if (is_notdir)
737 o = variable_buffer_output (o, p2, len);
738
739 if (is_notdir || p >= p2)
740 {
741 o = variable_buffer_output (o, " ", 1);
742 doneany = 1;
743 }
744 }
745
746 if (doneany)
747 /* Kill last space. */
748 --o;
749
750 return o;
751}
752
753
754static char *
755func_basename_dir (char *o, char **argv, const char *funcname)
756{
757 /* Expand the argument. */
758 const char *p3 = argv[0];
759 const char *p2;
760 int doneany=0;
761 unsigned int len=0;
762
763 int is_basename= streq (funcname, "basename");
764 int is_dir= !is_basename;
765
766 while ((p2 = find_next_token (&p3, &len)) != 0)
767 {
768 const char *p = p2 + len;
769 while (p >= p2 && (!is_basename || *p != '.'))
770 {
771 if (IS_PATHSEP (*p))
772 break;
773 --p;
774 }
775
776 if (p >= p2 && (is_dir))
777 o = variable_buffer_output (o, p2, ++p - p2);
778 else if (p >= p2 && (*p == '.'))
779 o = variable_buffer_output (o, p2, p - p2);
780#ifdef HAVE_DOS_PATHS
781 /* Handle the "d:foobar" case */
782 else if (p2[0] && p2[1] == ':' && is_dir)
783 o = variable_buffer_output (o, p2, 2);
784#endif
785 else if (is_dir)
786#ifdef VMS
787 o = variable_buffer_output (o, "[]", 2);
788#else
789#ifndef _AMIGA
790 o = variable_buffer_output (o, "./", 2);
791#else
792 ; /* Just a nop... */
793#endif /* AMIGA */
794#endif /* !VMS */
795 else
796 /* The entire name is the basename. */
797 o = variable_buffer_output (o, p2, len);
798
799 o = variable_buffer_output (o, " ", 1);
800 doneany = 1;
801 }
802
803 if (doneany)
804 /* Kill last space. */
805 --o;
806
807 return o;
808}
809
810static char *
811func_addsuffix_addprefix (char *o, char **argv, const char *funcname)
812{
813 int fixlen = strlen (argv[0]);
814 const char *list_iterator = argv[1];
815 int is_addprefix = streq (funcname, "addprefix");
816 int is_addsuffix = !is_addprefix;
817
818 int doneany = 0;
819 const char *p;
820 unsigned int len;
821
822 while ((p = find_next_token (&list_iterator, &len)) != 0)
823 {
824 if (is_addprefix)
825 o = variable_buffer_output (o, argv[0], fixlen);
826 o = variable_buffer_output (o, p, len);
827 if (is_addsuffix)
828 o = variable_buffer_output (o, argv[0], fixlen);
829 o = variable_buffer_output (o, " ", 1);
830 doneany = 1;
831 }
832
833 if (doneany)
834 /* Kill last space. */
835 --o;
836
837 return o;
838}
839
840static char *
841func_subst (char *o, char **argv, const char *funcname UNUSED)
842{
843 o = subst_expand (o, argv[2], argv[0], argv[1], strlen (argv[0]),
844 strlen (argv[1]), 0);
845
846 return o;
847}
848
849
850static char *
851func_firstword (char *o, char **argv, const char *funcname UNUSED)
852{
853 unsigned int i;
854 const char *words = argv[0]; /* Use a temp variable for find_next_token */
855 const char *p = find_next_token (&words, &i);
856
857 if (p != 0)
858 o = variable_buffer_output (o, p, i);
859
860 return o;
861}
862
863static char *
864func_lastword (char *o, char **argv, const char *funcname UNUSED)
865{
866 unsigned int i;
867 const char *words = argv[0]; /* Use a temp variable for find_next_token */
868 const char *p = NULL;
869 const char *t;
870
871 while ((t = find_next_token (&words, &i)))
872 p = t;
873
874 if (p != 0)
875 o = variable_buffer_output (o, p, i);
876
877 return o;
878}
879
880static char *
881func_words (char *o, char **argv, const char *funcname UNUSED)
882{
883 int i = 0;
884 const char *word_iterator = argv[0];
885 char buf[20];
886
887 while (find_next_token (&word_iterator, (unsigned int *) 0) != 0)
888 ++i;
889
890 sprintf (buf, "%d", i);
891 o = variable_buffer_output (o, buf, strlen (buf));
892
893 return o;
894}
895
896/* Set begpp to point to the first non-whitespace character of the string,
897 * and endpp to point to the last non-whitespace character of the string.
898 * If the string is empty or contains nothing but whitespace, endpp will be
899 * begpp-1.
900 */
901char *
902strip_whitespace (const char **begpp, const char **endpp)
903{
904 while (*begpp <= *endpp && isspace ((unsigned char)**begpp))
905 (*begpp) ++;
906 while (*endpp >= *begpp && isspace ((unsigned char)**endpp))
907 (*endpp) --;
908 return (char *)*begpp;
909}
910
911static void
912check_numeric (const char *s, const char *msg)
913{
914 const char *end = s + strlen (s) - 1;
915 const char *beg = s;
916 strip_whitespace (&s, &end);
917
918 for (; s <= end; ++s)
919 if (!ISDIGIT (*s)) /* ISDIGIT only evals its arg once: see make.h. */
920 break;
921
922 if (s <= end || end - beg < 0)
923 fatal (*expanding_var, "%s: '%s'", msg, beg);
924}
925
926
927
928static char *
929func_word (char *o, char **argv, const char *funcname UNUSED)
930{
931 const char *end_p;
932 const char *p;
933 int i;
934
935 /* Check the first argument. */
936 check_numeric (argv[0], _("non-numeric first argument to `word' function"));
937 i = atoi (argv[0]);
938
939 if (i == 0)
940 fatal (*expanding_var,
941 _("first argument to `word' function must be greater than 0"));
942
943 end_p = argv[1];
944 while ((p = find_next_token (&end_p, 0)) != 0)
945 if (--i == 0)
946 break;
947
948 if (i == 0)
949 o = variable_buffer_output (o, p, end_p - p);
950
951 return o;
952}
953
954static char *
955func_wordlist (char *o, char **argv, const char *funcname UNUSED)
956{
957 int start, count;
958
959 /* Check the arguments. */
960 check_numeric (argv[0],
961 _("non-numeric first argument to `wordlist' function"));
962 check_numeric (argv[1],
963 _("non-numeric second argument to `wordlist' function"));
964
965 start = atoi (argv[0]);
966 if (start < 1)
967 fatal (*expanding_var,
968 "invalid first argument to `wordlist' function: `%d'", start);
969
970 count = atoi (argv[1]) - start + 1;
971
972 if (count > 0)
973 {
974 const char *p;
975 const char *end_p = argv[2];
976
977 /* Find the beginning of the "start"th word. */
978 while (((p = find_next_token (&end_p, 0)) != 0) && --start)
979 ;
980
981 if (p)
982 {
983 /* Find the end of the "count"th word from start. */
984 while (--count && (find_next_token (&end_p, 0) != 0))
985 ;
986
987 /* Return the stuff in the middle. */
988 o = variable_buffer_output (o, p, end_p - p);
989 }
990 }
991
992 return o;
993}
994
995static char *
996func_findstring (char *o, char **argv, const char *funcname UNUSED)
997{
998 /* Find the first occurrence of the first string in the second. */
999 if (strstr (argv[1], argv[0]) != 0)
1000 o = variable_buffer_output (o, argv[0], strlen (argv[0]));
1001
1002 return o;
1003}
1004
1005static char *
1006func_foreach (char *o, char **argv, const char *funcname UNUSED)
1007{
1008 /* expand only the first two. */
1009 char *varname = expand_argument (argv[0], NULL);
1010 char *list = expand_argument (argv[1], NULL);
1011 const char *body = argv[2];
1012#ifdef CONFIG_WITH_VALUE_LENGTH
1013 long body_len = strlen (body);
1014#endif
1015
1016 int doneany = 0;
1017 const char *list_iterator = list;
1018 const char *p;
1019 unsigned int len;
1020 struct variable *var;
1021
1022 push_new_variable_scope ();
1023 var = define_variable (varname, strlen (varname), "", o_automatic, 0);
1024
1025 /* loop through LIST, put the value in VAR and expand BODY */
1026 while ((p = find_next_token (&list_iterator, &len)) != 0)
1027 {
1028#ifndef CONFIG_WITH_VALUE_LENGTH
1029 char *result = 0;
1030
1031 free (var->value);
1032 var->value = savestring (p, len);
1033
1034 result = allocated_variable_expand (body);
1035
1036 o = variable_buffer_output (o, result, strlen (result));
1037 o = variable_buffer_output (o, " ", 1);
1038 doneany = 1;
1039 free (result);
1040#else /* CONFIG_WITH_VALUE_LENGTH */
1041 if (len >= var->value_alloc_len)
1042 {
1043# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1044 if (var->rdonly_val)
1045 var->rdonly_val = 0;
1046 else
1047# endif
1048 free (var->value);
1049 var->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (len + 1);
1050 var->value = xmalloc (var->value_alloc_len);
1051 }
1052 memcpy (var->value, p, len);
1053 var->value[len] = '\0';
1054 var->value_length = len;
1055
1056 variable_expand_string_2 (o, body, body_len, &o);
1057 o = variable_buffer_output (o, " ", 1);
1058 doneany = 1;
1059#endif /* CONFIG_WITH_VALUE_LENGTH */
1060 }
1061
1062 if (doneany)
1063 /* Kill the last space. */
1064 --o;
1065
1066 pop_variable_scope ();
1067 free (varname);
1068 free (list);
1069
1070 return o;
1071}
1072
1073struct a_word
1074{
1075 struct a_word *next;
1076 struct a_word *chain;
1077 char *str;
1078 int length;
1079 int matched;
1080};
1081
1082static unsigned long
1083a_word_hash_1 (const void *key)
1084{
1085 return_STRING_HASH_1 (((struct a_word const *) key)->str);
1086}
1087
1088static unsigned long
1089a_word_hash_2 (const void *key)
1090{
1091 return_STRING_HASH_2 (((struct a_word const *) key)->str);
1092}
1093
1094static int
1095a_word_hash_cmp (const void *x, const void *y)
1096{
1097 int result = ((struct a_word const *) x)->length - ((struct a_word const *) y)->length;
1098 if (result)
1099 return result;
1100 return_STRING_COMPARE (((struct a_word const *) x)->str,
1101 ((struct a_word const *) y)->str);
1102}
1103
1104struct a_pattern
1105{
1106 struct a_pattern *next;
1107 char *str;
1108 char *percent;
1109 int length;
1110 int save_c;
1111};
1112
1113static char *
1114func_filter_filterout (char *o, char **argv, const char *funcname)
1115{
1116 struct a_word *wordhead;
1117 struct a_word **wordtail;
1118 struct a_word *wp;
1119 struct a_pattern *pathead;
1120 struct a_pattern **pattail;
1121 struct a_pattern *pp;
1122
1123 struct hash_table a_word_table;
1124 int is_filter = streq (funcname, "filter");
1125 const char *pat_iterator = argv[0];
1126 const char *word_iterator = argv[1];
1127 int literals = 0;
1128 int words = 0;
1129 int hashing = 0;
1130 char *p;
1131 unsigned int len;
1132
1133 /* Chop ARGV[0] up into patterns to match against the words. */
1134
1135 pattail = &pathead;
1136 while ((p = find_next_token (&pat_iterator, &len)) != 0)
1137 {
1138 struct a_pattern *pat = alloca (sizeof (struct a_pattern));
1139
1140 *pattail = pat;
1141 pattail = &pat->next;
1142
1143 if (*pat_iterator != '\0')
1144 ++pat_iterator;
1145
1146 pat->str = p;
1147 pat->length = len;
1148 pat->save_c = p[len];
1149 p[len] = '\0';
1150 pat->percent = find_percent (p);
1151 if (pat->percent == 0)
1152 literals++;
1153 }
1154 *pattail = 0;
1155
1156 /* Chop ARGV[1] up into words to match against the patterns. */
1157
1158 wordtail = &wordhead;
1159 while ((p = find_next_token (&word_iterator, &len)) != 0)
1160 {
1161 struct a_word *word = alloca (sizeof (struct a_word));
1162
1163 *wordtail = word;
1164 wordtail = &word->next;
1165
1166 if (*word_iterator != '\0')
1167 ++word_iterator;
1168
1169 p[len] = '\0';
1170 word->str = p;
1171 word->length = len;
1172 word->matched = 0;
1173 word->chain = 0;
1174 words++;
1175 }
1176 *wordtail = 0;
1177
1178 /* Only use a hash table if arg list lengths justifies the cost. */
1179 hashing = (literals >= 2 && (literals * words) >= 10);
1180 if (hashing)
1181 {
1182 hash_init (&a_word_table, words, a_word_hash_1, a_word_hash_2,
1183 a_word_hash_cmp);
1184 for (wp = wordhead; wp != 0; wp = wp->next)
1185 {
1186 struct a_word *owp = hash_insert (&a_word_table, wp);
1187 if (owp)
1188 wp->chain = owp;
1189 }
1190 }
1191
1192 if (words)
1193 {
1194 int doneany = 0;
1195
1196 /* Run each pattern through the words, killing words. */
1197 for (pp = pathead; pp != 0; pp = pp->next)
1198 {
1199 if (pp->percent)
1200 for (wp = wordhead; wp != 0; wp = wp->next)
1201 wp->matched |= pattern_matches (pp->str, pp->percent, wp->str);
1202 else if (hashing)
1203 {
1204 struct a_word a_word_key;
1205 a_word_key.str = pp->str;
1206 a_word_key.length = pp->length;
1207 wp = hash_find_item (&a_word_table, &a_word_key);
1208 while (wp)
1209 {
1210 wp->matched |= 1;
1211 wp = wp->chain;
1212 }
1213 }
1214 else
1215 for (wp = wordhead; wp != 0; wp = wp->next)
1216 wp->matched |= (wp->length == pp->length
1217 && strneq (pp->str, wp->str, wp->length));
1218 }
1219
1220 /* Output the words that matched (or didn't, for filter-out). */
1221 for (wp = wordhead; wp != 0; wp = wp->next)
1222 if (is_filter ? wp->matched : !wp->matched)
1223 {
1224 o = variable_buffer_output (o, wp->str, strlen (wp->str));
1225 o = variable_buffer_output (o, " ", 1);
1226 doneany = 1;
1227 }
1228
1229 if (doneany)
1230 /* Kill the last space. */
1231 --o;
1232 }
1233
1234 for (pp = pathead; pp != 0; pp = pp->next)
1235 pp->str[pp->length] = pp->save_c;
1236
1237 if (hashing)
1238 hash_free (&a_word_table, 0);
1239
1240 return o;
1241}
1242
1243
1244static char *
1245func_strip (char *o, char **argv, const char *funcname UNUSED)
1246{
1247 const char *p = argv[0];
1248 int doneany = 0;
1249
1250 while (*p != '\0')
1251 {
1252 int i=0;
1253 const char *word_start;
1254
1255 while (isspace ((unsigned char)*p))
1256 ++p;
1257 word_start = p;
1258 for (i=0; *p != '\0' && !isspace ((unsigned char)*p); ++p, ++i)
1259 {}
1260 if (!i)
1261 break;
1262 o = variable_buffer_output (o, word_start, i);
1263 o = variable_buffer_output (o, " ", 1);
1264 doneany = 1;
1265 }
1266
1267 if (doneany)
1268 /* Kill the last space. */
1269 --o;
1270
1271 return o;
1272}
1273
1274/*
1275 Print a warning or fatal message.
1276*/
1277static char *
1278func_error (char *o, char **argv, const char *funcname)
1279{
1280 char **argvp;
1281 char *msg, *p;
1282 int len;
1283
1284 /* The arguments will be broken on commas. Rather than create yet
1285 another special case where function arguments aren't broken up,
1286 just create a format string that puts them back together. */
1287 for (len=0, argvp=argv; *argvp != 0; ++argvp)
1288 len += strlen (*argvp) + 2;
1289
1290 p = msg = alloca (len + 1);
1291
1292 for (argvp=argv; argvp[1] != 0; ++argvp)
1293 {
1294 strcpy (p, *argvp);
1295 p += strlen (*argvp);
1296 *(p++) = ',';
1297 *(p++) = ' ';
1298 }
1299 strcpy (p, *argvp);
1300
1301 switch (*funcname) {
1302 case 'e':
1303 fatal (reading_file, "%s", msg);
1304
1305 case 'w':
1306 error (reading_file, "%s", msg);
1307 break;
1308
1309 case 'i':
1310 printf ("%s\n", msg);
1311 fflush(stdout);
1312 break;
1313
1314 default:
1315 fatal (*expanding_var, "Internal error: func_error: '%s'", funcname);
1316 }
1317
1318 /* The warning function expands to the empty string. */
1319 return o;
1320}
1321
1322
1323/*
1324 chop argv[0] into words, and sort them.
1325 */
1326static char *
1327func_sort (char *o, char **argv, const char *funcname UNUSED)
1328{
1329 const char *t;
1330 char **words;
1331 int wordi;
1332 char *p;
1333 unsigned int len;
1334 int i;
1335
1336 /* Find the maximum number of words we'll have. */
1337 t = argv[0];
1338 wordi = 1;
1339 while (*t != '\0')
1340 {
1341 char c = *(t++);
1342
1343 if (! isspace ((unsigned char)c))
1344 continue;
1345
1346 ++wordi;
1347
1348 while (isspace ((unsigned char)*t))
1349 ++t;
1350 }
1351
1352 words = xmalloc (wordi * sizeof (char *));
1353
1354 /* Now assign pointers to each string in the array. */
1355 t = argv[0];
1356 wordi = 0;
1357 while ((p = find_next_token (&t, &len)) != 0)
1358 {
1359 ++t;
1360 p[len] = '\0';
1361 words[wordi++] = p;
1362 }
1363
1364 if (wordi)
1365 {
1366 /* Now sort the list of words. */
1367 qsort (words, wordi, sizeof (char *), alpha_compare);
1368
1369 /* Now write the sorted list, uniquified. */
1370#ifdef CONFIG_WITH_RSORT
1371 if (strcmp (funcname, "rsort"))
1372 {
1373 /* sort */
1374#endif
1375 for (i = 0; i < wordi; ++i)
1376 {
1377 len = strlen (words[i]);
1378 if (i == wordi - 1 || strlen (words[i + 1]) != len
1379 || strcmp (words[i], words[i + 1]))
1380 {
1381 o = variable_buffer_output (o, words[i], len);
1382 o = variable_buffer_output (o, " ", 1);
1383 }
1384 }
1385#ifdef CONFIG_WITH_RSORT
1386 }
1387 else
1388 {
1389 /* rsort - reverse the result */
1390 i = wordi;
1391 while (i-- > 0)
1392 {
1393 len = strlen (words[i]);
1394 if (i == 0 || strlen (words[i - 1]) != len
1395 || strcmp (words[i], words[i - 1]))
1396 {
1397 o = variable_buffer_output (o, words[i], len);
1398 o = variable_buffer_output (o, " ", 1);
1399 }
1400 }
1401 }
1402#endif
1403
1404 /* Kill the last space. */
1405 --o;
1406 }
1407
1408 free (words);
1409
1410 return o;
1411}
1412
1413/*
1414 $(if condition,true-part[,false-part])
1415
1416 CONDITION is false iff it evaluates to an empty string. White
1417 space before and after condition are stripped before evaluation.
1418
1419 If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is
1420 evaluated (if it exists). Because only one of the two PARTs is evaluated,
1421 you can use $(if ...) to create side-effects (with $(shell ...), for
1422 example).
1423*/
1424
1425static char *
1426func_if (char *o, char **argv, const char *funcname UNUSED)
1427{
1428 const char *begp = argv[0];
1429 const char *endp = begp + strlen (argv[0]) - 1;
1430 int result = 0;
1431
1432 /* Find the result of the condition: if we have a value, and it's not
1433 empty, the condition is true. If we don't have a value, or it's the
1434 empty string, then it's false. */
1435
1436 strip_whitespace (&begp, &endp);
1437
1438 if (begp <= endp)
1439 {
1440 char *expansion = expand_argument (begp, endp+1);
1441
1442 result = strlen (expansion);
1443 free (expansion);
1444 }
1445
1446 /* If the result is true (1) we want to eval the first argument, and if
1447 it's false (0) we want to eval the second. If the argument doesn't
1448 exist we do nothing, otherwise expand it and add to the buffer. */
1449
1450 argv += 1 + !result;
1451
1452 if (*argv)
1453 {
1454 char *expansion = expand_argument (*argv, NULL);
1455
1456 o = variable_buffer_output (o, expansion, strlen (expansion));
1457
1458 free (expansion);
1459 }
1460
1461 return o;
1462}
1463
1464/*
1465 $(or condition1[,condition2[,condition3[...]]])
1466
1467 A CONDITION is false iff it evaluates to an empty string. White
1468 space before and after CONDITION are stripped before evaluation.
1469
1470 CONDITION1 is evaluated. If it's true, then this is the result of
1471 expansion. If it's false, CONDITION2 is evaluated, and so on. If none of
1472 the conditions are true, the expansion is the empty string.
1473
1474 Once a CONDITION is true no further conditions are evaluated
1475 (short-circuiting).
1476*/
1477
1478static char *
1479func_or (char *o, char **argv, const char *funcname UNUSED)
1480{
1481 for ( ; *argv ; ++argv)
1482 {
1483 const char *begp = *argv;
1484 const char *endp = begp + strlen (*argv) - 1;
1485 char *expansion;
1486 int result = 0;
1487
1488 /* Find the result of the condition: if it's false keep going. */
1489
1490 strip_whitespace (&begp, &endp);
1491
1492 if (begp > endp)
1493 continue;
1494
1495 expansion = expand_argument (begp, endp+1);
1496 result = strlen (expansion);
1497
1498 /* If the result is false keep going. */
1499 if (!result)
1500 {
1501 free (expansion);
1502 continue;
1503 }
1504
1505 /* It's true! Keep this result and return. */
1506 o = variable_buffer_output (o, expansion, result);
1507 free (expansion);
1508 break;
1509 }
1510
1511 return o;
1512}
1513
1514/*
1515 $(and condition1[,condition2[,condition3[...]]])
1516
1517 A CONDITION is false iff it evaluates to an empty string. White
1518 space before and after CONDITION are stripped before evaluation.
1519
1520 CONDITION1 is evaluated. If it's false, then this is the result of
1521 expansion. If it's true, CONDITION2 is evaluated, and so on. If all of
1522 the conditions are true, the expansion is the result of the last condition.
1523
1524 Once a CONDITION is false no further conditions are evaluated
1525 (short-circuiting).
1526*/
1527
1528static char *
1529func_and (char *o, char **argv, const char *funcname UNUSED)
1530{
1531 char *expansion;
1532 int result;
1533
1534 while (1)
1535 {
1536 const char *begp = *argv;
1537 const char *endp = begp + strlen (*argv) - 1;
1538
1539 /* An empty condition is always false. */
1540 strip_whitespace (&begp, &endp);
1541 if (begp > endp)
1542 return o;
1543
1544 expansion = expand_argument (begp, endp+1);
1545 result = strlen (expansion);
1546
1547 /* If the result is false, stop here: we're done. */
1548 if (!result)
1549 break;
1550
1551 /* Otherwise the result is true. If this is the last one, keep this
1552 result and quit. Otherwise go on to the next one! */
1553
1554 if (*(++argv))
1555 free (expansion);
1556 else
1557 {
1558 o = variable_buffer_output (o, expansion, result);
1559 break;
1560 }
1561 }
1562
1563 free (expansion);
1564
1565 return o;
1566}
1567
1568static char *
1569func_wildcard (char *o, char **argv, const char *funcname UNUSED)
1570{
1571#ifdef _AMIGA
1572 o = wildcard_expansion (argv[0], o);
1573#else
1574 char *p = string_glob (argv[0]);
1575 o = variable_buffer_output (o, p, strlen (p));
1576#endif
1577 return o;
1578}
1579
1580/*
1581 $(eval <makefile string>)
1582
1583 Always resolves to the empty string.
1584
1585 Treat the arguments as a segment of makefile, and parse them.
1586*/
1587
1588static char *
1589func_eval (char *o, char **argv, const char *funcname UNUSED)
1590{
1591 char *buf;
1592 unsigned int len;
1593
1594 /* Eval the buffer. Pop the current variable buffer setting so that the
1595 eval'd code can use its own without conflicting. */
1596
1597 install_variable_buffer (&buf, &len);
1598
1599#ifndef CONFIG_WITH_VALUE_LENGTH
1600 eval_buffer (argv[0]);
1601#else
1602 eval_buffer (argv[0], strchr (argv[0], '\0'));
1603#endif
1604
1605 restore_variable_buffer (buf, len);
1606
1607 return o;
1608}
1609
1610
1611#ifdef CONFIG_WITH_EVALPLUS
1612/* Same as func_eval except that we push and pop the local variable
1613 context before evaluating the buffer. */
1614static char *
1615func_evalctx (char *o, char **argv, const char *funcname UNUSED)
1616{
1617 char *buf;
1618 unsigned int len;
1619
1620 /* Eval the buffer. Pop the current variable buffer setting so that the
1621 eval'd code can use its own without conflicting. */
1622
1623 install_variable_buffer (&buf, &len);
1624
1625 push_new_variable_scope ();
1626
1627 eval_buffer (argv[0], strchr (argv[0], '\0'));
1628
1629 pop_variable_scope ();
1630
1631 restore_variable_buffer (buf, len);
1632
1633 return o;
1634}
1635
1636/* A mix of func_eval and func_value, saves memory for the expansion.
1637 This implements both evalval and evalvalctx, the latter has its own
1638 variable context just like evalctx. */
1639static char *
1640func_evalval (char *o, char **argv, const char *funcname)
1641{
1642 /* Look up the variable. */
1643 struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
1644 if (v)
1645 {
1646 char *buf;
1647 unsigned int len;
1648 int var_ctx;
1649 size_t off;
1650 const struct floc *reading_file_saved = reading_file;
1651
1652 /* Make a copy of the value to the variable buffer since
1653 eval_buffer will make changes to its input. */
1654
1655 off = o - variable_buffer;
1656 variable_buffer_output (o, v->value, v->value_length + 1);
1657 o = variable_buffer + off;
1658
1659 /* Eval the value. Pop the current variable buffer setting so that the
1660 eval'd code can use its own without conflicting. (really necessary?) */
1661
1662 install_variable_buffer (&buf, &len);
1663 var_ctx = !strcmp (funcname, "evalvalctx");
1664 if (var_ctx)
1665 push_new_variable_scope ();
1666 if (v->fileinfo.filenm)
1667 reading_file = &v->fileinfo;
1668
1669 assert (!o[v->value_length]);
1670 eval_buffer (o, o + v->value_length);
1671
1672 reading_file = reading_file_saved;
1673 if (var_ctx)
1674 pop_variable_scope ();
1675 restore_variable_buffer (buf, len);
1676 }
1677
1678 return o;
1679}
1680
1681/* Optimizes the content of one or more variables to save time in
1682 the eval functions. This function will collapse line continuations
1683 and remove comments. */
1684static char *
1685func_eval_optimize_variable (char *o, char **argv, const char *funcname)
1686{
1687 unsigned int i;
1688
1689 for (i = 0; argv[i]; i++)
1690 {
1691 struct variable *v = lookup_variable (argv[i], strlen (argv[i]));
1692# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1693 if (v && !v->origin != o_automatic && !v->rdonly_val)
1694# else
1695 if (v && !v->origin != o_automatic)
1696# endif
1697 {
1698 char *eos, *src;
1699
1700 eos = collapse_continuations (v->value, v->value_length);
1701 v->value_length = eos - v->value;
1702
1703 /* remove comments */
1704
1705 src = memchr (v->value, '#', v->value_length);
1706 if (src)
1707 {
1708 unsigned char ch = '\0';
1709 char *dst = src;
1710 do
1711 {
1712 /* drop blanks preceeding the comment */
1713 while (dst > v->value)
1714 {
1715 ch = (unsigned char)dst[-1];
1716 if (!isblank (ch))
1717 break;
1718 dst--;
1719 }
1720
1721 /* advance SRC to eol / eos. */
1722 src = memchr (src, '\n', eos - src);
1723 if (!src)
1724 break;
1725
1726 /* drop a preceeding newline if possible (full line comment) */
1727 if (dst > v->value && dst[-1] == '\n')
1728 dst--;
1729
1730 /* copy till next comment or eol. */
1731 while (src < eos)
1732 {
1733 ch = *src++;
1734 if (ch == '#')
1735 break;
1736 *dst++ = ch;
1737 }
1738 }
1739 while (ch == '#' && src < eos);
1740
1741 *dst = '\0';
1742 v->value_length = dst - v->value;
1743 }
1744 }
1745 else if (v)
1746 error (NILF, _("$(%s ): variable `%s' is of the wrong type\n"), funcname, v->name);
1747 }
1748
1749 return o;
1750}
1751
1752#endif /* CONFIG_WITH_EVALPLUS */
1753
1754static char *
1755func_value (char *o, char **argv, const char *funcname UNUSED)
1756{
1757 /* Look up the variable. */
1758 struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
1759
1760 /* Copy its value into the output buffer without expanding it. */
1761 if (v)
1762#ifdef CONFIG_WITH_VALUE_LENGTH
1763 {
1764 assert (v->value_length == strlen (v->value));
1765 o = variable_buffer_output (o, v->value, v->value_length);
1766 }
1767#else
1768 o = variable_buffer_output (o, v->value, strlen(v->value));
1769#endif
1770
1771 return o;
1772}
1773
1774/*
1775 \r is replaced on UNIX as well. Is this desirable?
1776 */
1777static void
1778fold_newlines (char *buffer, unsigned int *length)
1779{
1780 char *dst = buffer;
1781 char *src = buffer;
1782 char *last_nonnl = buffer -1;
1783 src[*length] = 0;
1784 for (; *src != '\0'; ++src)
1785 {
1786 if (src[0] == '\r' && src[1] == '\n')
1787 continue;
1788 if (*src == '\n')
1789 {
1790 *dst++ = ' ';
1791 }
1792 else
1793 {
1794 last_nonnl = dst;
1795 *dst++ = *src;
1796 }
1797 }
1798 *(++last_nonnl) = '\0';
1799 *length = last_nonnl - buffer;
1800}
1801
1802
1803
1804int shell_function_pid = 0, shell_function_completed;
1805
1806
1807#ifdef WINDOWS32
1808/*untested*/
1809
1810#include <windows.h>
1811#include <io.h>
1812#include "sub_proc.h"
1813
1814
1815void
1816windows32_openpipe (int *pipedes, int *pid_p, char **command_argv, char **envp)
1817{
1818 SECURITY_ATTRIBUTES saAttr;
1819 HANDLE hIn;
1820 HANDLE hErr;
1821 HANDLE hChildOutRd;
1822 HANDLE hChildOutWr;
1823 HANDLE hProcess;
1824
1825
1826 saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
1827 saAttr.bInheritHandle = TRUE;
1828 saAttr.lpSecurityDescriptor = NULL;
1829
1830 if (DuplicateHandle (GetCurrentProcess(),
1831 GetStdHandle(STD_INPUT_HANDLE),
1832 GetCurrentProcess(),
1833 &hIn,
1834 0,
1835 TRUE,
1836 DUPLICATE_SAME_ACCESS) == FALSE) {
1837 fatal (NILF, _("create_child_process: DuplicateHandle(In) failed (e=%ld)\n"),
1838 GetLastError());
1839
1840 }
1841 if (DuplicateHandle(GetCurrentProcess(),
1842 GetStdHandle(STD_ERROR_HANDLE),
1843 GetCurrentProcess(),
1844 &hErr,
1845 0,
1846 TRUE,
1847 DUPLICATE_SAME_ACCESS) == FALSE) {
1848 fatal (NILF, _("create_child_process: DuplicateHandle(Err) failed (e=%ld)\n"),
1849 GetLastError());
1850 }
1851
1852 if (!CreatePipe(&hChildOutRd, &hChildOutWr, &saAttr, 0))
1853 fatal (NILF, _("CreatePipe() failed (e=%ld)\n"), GetLastError());
1854
1855 hProcess = process_init_fd(hIn, hChildOutWr, hErr);
1856
1857 if (!hProcess)
1858 fatal (NILF, _("windows32_openpipe (): process_init_fd() failed\n"));
1859
1860 /* make sure that CreateProcess() has Path it needs */
1861 sync_Path_environment();
1862
1863 if (!process_begin(hProcess, command_argv, envp, command_argv[0], NULL)) {
1864 /* register process for wait */
1865 process_register(hProcess);
1866
1867 /* set the pid for returning to caller */
1868 *pid_p = (int) hProcess;
1869
1870 /* set up to read data from child */
1871 pipedes[0] = _open_osfhandle((long) hChildOutRd, O_RDONLY);
1872
1873 /* this will be closed almost right away */
1874 pipedes[1] = _open_osfhandle((long) hChildOutWr, O_APPEND);
1875 } else {
1876 /* reap/cleanup the failed process */
1877 process_cleanup(hProcess);
1878
1879 /* close handles which were duplicated, they weren't used */
1880 CloseHandle(hIn);
1881 CloseHandle(hErr);
1882
1883 /* close pipe handles, they won't be used */
1884 CloseHandle(hChildOutRd);
1885 CloseHandle(hChildOutWr);
1886
1887 /* set status for return */
1888 pipedes[0] = pipedes[1] = -1;
1889 *pid_p = -1;
1890 }
1891}
1892#endif
1893
1894
1895#ifdef __MSDOS__
1896FILE *
1897msdos_openpipe (int* pipedes, int *pidp, char *text)
1898{
1899 FILE *fpipe=0;
1900 /* MSDOS can't fork, but it has `popen'. */
1901 struct variable *sh = lookup_variable ("SHELL", 5);
1902 int e;
1903 extern int dos_command_running, dos_status;
1904
1905 /* Make sure not to bother processing an empty line. */
1906 while (isblank ((unsigned char)*text))
1907 ++text;
1908 if (*text == '\0')
1909 return 0;
1910
1911 if (sh)
1912 {
1913 char buf[PATH_MAX + 7];
1914 /* This makes sure $SHELL value is used by $(shell), even
1915 though the target environment is not passed to it. */
1916 sprintf (buf, "SHELL=%s", sh->value);
1917 putenv (buf);
1918 }
1919
1920 e = errno;
1921 errno = 0;
1922 dos_command_running = 1;
1923 dos_status = 0;
1924 /* If dos_status becomes non-zero, it means the child process
1925 was interrupted by a signal, like SIGINT or SIGQUIT. See
1926 fatal_error_signal in commands.c. */
1927 fpipe = popen (text, "rt");
1928 dos_command_running = 0;
1929 if (!fpipe || dos_status)
1930 {
1931 pipedes[0] = -1;
1932 *pidp = -1;
1933 if (dos_status)
1934 errno = EINTR;
1935 else if (errno == 0)
1936 errno = ENOMEM;
1937 shell_function_completed = -1;
1938 }
1939 else
1940 {
1941 pipedes[0] = fileno (fpipe);
1942 *pidp = 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
1943 errno = e;
1944 shell_function_completed = 1;
1945 }
1946 return fpipe;
1947}
1948#endif
1949
1950/*
1951 Do shell spawning, with the naughty bits for different OSes.
1952 */
1953
1954#ifdef VMS
1955
1956/* VMS can't do $(shell ...) */
1957#define func_shell 0
1958
1959#else
1960#ifndef _AMIGA
1961static char *
1962func_shell (char *o, char **argv, const char *funcname UNUSED)
1963{
1964 char *batch_filename = NULL;
1965
1966#ifdef __MSDOS__
1967 FILE *fpipe;
1968#endif
1969 char **command_argv;
1970 const char *error_prefix;
1971 char **envp;
1972 int pipedes[2];
1973 int pid;
1974
1975#ifndef __MSDOS__
1976 /* Construct the argument list. */
1977 command_argv = construct_command_argv (argv[0], NULL, NULL, 0,
1978 &batch_filename);
1979 if (command_argv == 0)
1980 return o;
1981#endif
1982
1983 /* Using a target environment for `shell' loses in cases like:
1984 export var = $(shell echo foobie)
1985 because target_environment hits a loop trying to expand $(var)
1986 to put it in the environment. This is even more confusing when
1987 var was not explicitly exported, but just appeared in the
1988 calling environment.
1989
1990 See Savannah bug #10593.
1991
1992 envp = target_environment (NILF);
1993 */
1994
1995 envp = environ;
1996
1997 /* For error messages. */
1998 if (reading_file && reading_file->filenm)
1999 {
2000 char *p = alloca (strlen (reading_file->filenm)+11+4);
2001 sprintf (p, "%s:%lu: ", reading_file->filenm, reading_file->lineno);
2002 error_prefix = p;
2003 }
2004 else
2005 error_prefix = "";
2006
2007#if defined(__MSDOS__)
2008 fpipe = msdos_openpipe (pipedes, &pid, argv[0]);
2009 if (pipedes[0] < 0)
2010 {
2011 perror_with_name (error_prefix, "pipe");
2012 return o;
2013 }
2014#elif defined(WINDOWS32)
2015 windows32_openpipe (pipedes, &pid, command_argv, envp);
2016 if (pipedes[0] < 0)
2017 {
2018 /* open of the pipe failed, mark as failed execution */
2019 shell_function_completed = -1;
2020
2021 return o;
2022 }
2023 else
2024#else
2025 if (pipe (pipedes) < 0)
2026 {
2027 perror_with_name (error_prefix, "pipe");
2028 return o;
2029 }
2030
2031# ifdef __EMX__
2032 /* close some handles that are unnecessary for the child process */
2033 CLOSE_ON_EXEC(pipedes[1]);
2034 CLOSE_ON_EXEC(pipedes[0]);
2035 /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
2036 pid = child_execute_job (0, pipedes[1], command_argv, envp);
2037 if (pid < 0)
2038 perror_with_name (error_prefix, "spawn");
2039# else /* ! __EMX__ */
2040 pid = vfork ();
2041 if (pid < 0)
2042 perror_with_name (error_prefix, "fork");
2043 else if (pid == 0)
2044 child_execute_job (0, pipedes[1], command_argv, envp);
2045 else
2046# endif
2047#endif
2048 {
2049 /* We are the parent. */
2050 char *buffer;
2051 unsigned int maxlen, i;
2052 int cc;
2053
2054 /* Record the PID for reap_children. */
2055 shell_function_pid = pid;
2056#ifndef __MSDOS__
2057 shell_function_completed = 0;
2058
2059 /* Free the storage only the child needed. */
2060 free (command_argv[0]);
2061 free (command_argv);
2062
2063 /* Close the write side of the pipe. */
2064# ifdef _MSC_VER /* Avoid annoying msvcrt when debugging. (bird) */
2065 if (pipedes[1] != -1)
2066# endif
2067 close (pipedes[1]);
2068#endif
2069
2070 /* Set up and read from the pipe. */
2071
2072 maxlen = 200;
2073 buffer = xmalloc (maxlen + 1);
2074
2075 /* Read from the pipe until it gets EOF. */
2076 for (i = 0; ; i += cc)
2077 {
2078 if (i == maxlen)
2079 {
2080 maxlen += 512;
2081 buffer = xrealloc (buffer, maxlen + 1);
2082 }
2083
2084 EINTRLOOP (cc, read (pipedes[0], &buffer[i], maxlen - i));
2085 if (cc <= 0)
2086 break;
2087 }
2088 buffer[i] = '\0';
2089
2090 /* Close the read side of the pipe. */
2091#ifdef __MSDOS__
2092 if (fpipe)
2093 (void) pclose (fpipe);
2094#else
2095# ifdef _MSC_VER /* Avoid annoying msvcrt when debugging. (bird) */
2096 if (pipedes[0] != -1)
2097# endif
2098 (void) close (pipedes[0]);
2099#endif
2100
2101 /* Loop until child_handler or reap_children() sets
2102 shell_function_completed to the status of our child shell. */
2103 while (shell_function_completed == 0)
2104 reap_children (1, 0);
2105
2106 if (batch_filename) {
2107 DB (DB_VERBOSE, (_("Cleaning up temporary batch file %s\n"),
2108 batch_filename));
2109 remove (batch_filename);
2110 free (batch_filename);
2111 }
2112 shell_function_pid = 0;
2113
2114 /* The child_handler function will set shell_function_completed
2115 to 1 when the child dies normally, or to -1 if it
2116 dies with status 127, which is most likely an exec fail. */
2117
2118 if (shell_function_completed == -1)
2119 {
2120 /* This likely means that the execvp failed, so we should just
2121 write the error message in the pipe from the child. */
2122 fputs (buffer, stderr);
2123 fflush (stderr);
2124 }
2125 else
2126 {
2127 /* The child finished normally. Replace all newlines in its output
2128 with spaces, and put that in the variable output buffer. */
2129 fold_newlines (buffer, &i);
2130 o = variable_buffer_output (o, buffer, i);
2131 }
2132
2133 free (buffer);
2134 }
2135
2136 return o;
2137}
2138
2139#else /* _AMIGA */
2140
2141/* Do the Amiga version of func_shell. */
2142
2143static char *
2144func_shell (char *o, char **argv, const char *funcname)
2145{
2146 /* Amiga can't fork nor spawn, but I can start a program with
2147 redirection of my choice. However, this means that we
2148 don't have an opportunity to reopen stdout to trap it. Thus,
2149 we save our own stdout onto a new descriptor and dup a temp
2150 file's descriptor onto our stdout temporarily. After we
2151 spawn the shell program, we dup our own stdout back to the
2152 stdout descriptor. The buffer reading is the same as above,
2153 except that we're now reading from a file. */
2154
2155#include <dos/dos.h>
2156#include <proto/dos.h>
2157
2158 BPTR child_stdout;
2159 char tmp_output[FILENAME_MAX];
2160 unsigned int maxlen = 200, i;
2161 int cc;
2162 char * buffer, * ptr;
2163 char ** aptr;
2164 int len = 0;
2165 char* batch_filename = NULL;
2166
2167 /* Construct the argument list. */
2168 command_argv = construct_command_argv (argv[0], NULL, NULL, 0,
2169 &batch_filename);
2170 if (command_argv == 0)
2171 return o;
2172
2173 /* Note the mktemp() is a security hole, but this only runs on Amiga.
2174 Ideally we would use main.c:open_tmpfile(), but this uses a special
2175 Open(), not fopen(), and I'm not familiar enough with the code to mess
2176 with it. */
2177 strcpy (tmp_output, "t:MakeshXXXXXXXX");
2178 mktemp (tmp_output);
2179 child_stdout = Open (tmp_output, MODE_NEWFILE);
2180
2181 for (aptr=command_argv; *aptr; aptr++)
2182 len += strlen (*aptr) + 1;
2183
2184 buffer = xmalloc (len + 1);
2185 ptr = buffer;
2186
2187 for (aptr=command_argv; *aptr; aptr++)
2188 {
2189 strcpy (ptr, *aptr);
2190 ptr += strlen (ptr) + 1;
2191 *ptr ++ = ' ';
2192 *ptr = 0;
2193 }
2194
2195 ptr[-1] = '\n';
2196
2197 Execute (buffer, NULL, child_stdout);
2198 free (buffer);
2199
2200 Close (child_stdout);
2201
2202 child_stdout = Open (tmp_output, MODE_OLDFILE);
2203
2204 buffer = xmalloc (maxlen);
2205 i = 0;
2206 do
2207 {
2208 if (i == maxlen)
2209 {
2210 maxlen += 512;
2211 buffer = xrealloc (buffer, maxlen + 1);
2212 }
2213
2214 cc = Read (child_stdout, &buffer[i], maxlen - i);
2215 if (cc > 0)
2216 i += cc;
2217 } while (cc > 0);
2218
2219 Close (child_stdout);
2220
2221 fold_newlines (buffer, &i);
2222 o = variable_buffer_output (o, buffer, i);
2223 free (buffer);
2224 return o;
2225}
2226#endif /* _AMIGA */
2227#endif /* !VMS */
2228
2229#ifdef EXPERIMENTAL
2230
2231/*
2232 equality. Return is string-boolean, ie, the empty string is false.
2233 */
2234static char *
2235func_eq (char *o, char **argv, const char *funcname UNUSED)
2236{
2237 int result = ! strcmp (argv[0], argv[1]);
2238 o = variable_buffer_output (o, result ? "1" : "", result);
2239 return o;
2240}
2241
2242
2243/*
2244 string-boolean not operator.
2245 */
2246static char *
2247func_not (char *o, char **argv, const char *funcname UNUSED)
2248{
2249 const char *s = argv[0];
2250 int result = 0;
2251 while (isspace ((unsigned char)*s))
2252 s++;
2253 result = ! (*s);
2254 o = variable_buffer_output (o, result ? "1" : "", result);
2255 return o;
2256}
2257#endif
2258
2259
2260#ifdef CONFIG_WITH_STRING_FUNCTIONS
2261/*
2262 $(length string)
2263
2264 XXX: This doesn't take multibyte locales into account.
2265 */
2266static char *
2267func_length (char *o, char **argv, const char *funcname UNUSED)
2268{
2269 size_t len = strlen (argv[0]);
2270 return math_int_to_variable_buffer (o, len);
2271}
2272/*
2273 $(length-var var)
2274
2275 XXX: This doesn't take multibyte locales into account.
2276 */
2277static char *
2278func_length_var (char *o, char **argv, const char *funcname UNUSED)
2279{
2280 struct variable *var = lookup_variable (argv[0], strlen (argv[0]));
2281 return math_int_to_variable_buffer (o, var ? var->value_length : 0);
2282}
2283
2284
2285/* func_insert helper. */
2286static char *
2287helper_insert_pad (char *o, size_t to_add, const char *pad, size_t pad_len)
2288{
2289 while (to_add > 0)
2290 {
2291 size_t size = to_add > pad_len ? pad_len : to_add;
2292 o = variable_buffer_output (o, pad, size);
2293 to_add -= size;
2294 }
2295 return o;
2296}
2297
2298/*
2299 $(insert in, str[, n[, length[, pad]]])
2300
2301 XXX: This doesn't take multibyte locales into account.
2302 */
2303static char *
2304func_insert (char *o, char **argv, const char *funcname UNUSED)
2305{
2306 const char *in = argv[0];
2307 size_t in_len = strlen (in);
2308 const char *str = argv[1];
2309 size_t str_len = strlen (str);
2310 math_int n = 0;
2311 math_int length = str_len;
2312 const char *pad = " ";
2313 size_t pad_len = 16;
2314 size_t i;
2315
2316 if (argv[2] != NULL)
2317 {
2318 n = math_int_from_string (argv[2]);
2319 if (n > 0)
2320 n--; /* one-origin */
2321 else if (n == 0)
2322 n = str_len; /* append */
2323 else
2324 { /* n < 0: from the end */
2325 n = str_len + n;
2326 if (n < 0)
2327 n = 0;
2328 }
2329 if (n > 16*1024*1024) /* 16MB */
2330 fatal (NILF, _("$(insert ): n=%s is out of bounds\n"), argv[2]);
2331
2332 if (argv[3] != NULL)
2333 {
2334 length = math_int_from_string (argv[3]);
2335 if (length < 0 || length > 16*1024*1024 /* 16MB */)
2336 fatal (NILF, _("$(insert ): length=%s is out of bounds\n"), argv[3]);
2337
2338 if (argv[4] != NULL)
2339 {
2340 const char *tmp = argv[4];
2341 for (i = 0; tmp[i] == ' '; i++)
2342 /* nothing */;
2343 if (tmp[i] != '\0')
2344 {
2345 pad = argv[4];
2346 pad_len = strlen (pad);
2347 }
2348 /* else: it was all default spaces. */
2349 }
2350 }
2351 }
2352
2353 /* the head of the original string */
2354 if (n > 0)
2355 {
2356 if (n <= str_len)
2357 o = variable_buffer_output (o, str, n);
2358 else
2359 {
2360 o = variable_buffer_output (o, str, str_len);
2361 o = helper_insert_pad (o, n - str_len, pad, pad_len);
2362 }
2363 }
2364
2365 /* insert the string */
2366 if (length <= in_len)
2367 o = variable_buffer_output (o, in, length);
2368 else
2369 {
2370 o = variable_buffer_output (o, in, in_len);
2371 o = helper_insert_pad (o, length - in_len, pad, pad_len);
2372 }
2373
2374 /* the tail of the original string */
2375 if (n < str_len)
2376 o = variable_buffer_output (o, str + n, str_len - n);
2377
2378 return o;
2379}
2380
2381
2382/*
2383 $(pos needle, haystack[, start])
2384 $(lastpos needle, haystack[, start])
2385
2386 XXX: This doesn't take multibyte locales into account.
2387 */
2388static char *
2389func_pos (char *o, char **argv, const char *funcname UNUSED)
2390{
2391 const char *needle = *argv[0] ? argv[0] : " ";
2392 size_t needle_len = strlen (needle);
2393 const char *haystack = argv[1];
2394 size_t haystack_len = strlen (haystack);
2395 math_int start = 0;
2396 const char *hit;
2397
2398 if (argv[2] != NULL)
2399 {
2400 start = math_int_from_string (argv[2]);
2401 if (start > 0)
2402 start--; /* one-origin */
2403 else if (start < 0)
2404 start = haystack_len + start; /* from the end */
2405 if (start < 0 || start + needle_len > haystack_len)
2406 return math_int_to_variable_buffer (o, 0);
2407 }
2408 else if (funcname[0] == 'l')
2409 start = haystack_len - 1;
2410
2411 /* do the searching */
2412 if (funcname[0] != 'l')
2413 { /* pos */
2414 if (needle_len == 1)
2415 hit = strchr (haystack + start, *needle);
2416 else
2417 hit = strstr (haystack + start, needle);
2418 }
2419 else
2420 { /* last pos */
2421 int ch = *needle;
2422 size_t off = start + 1;
2423
2424 hit = NULL;
2425 while (off-- > 0)
2426 {
2427 if ( haystack[off] == ch
2428 && ( needle_len == 1
2429 || strncmp (&haystack[off], needle, needle_len) == 0))
2430 {
2431 hit = haystack + off;
2432 break;
2433 }
2434 }
2435 }
2436
2437 return math_int_to_variable_buffer (o, hit ? hit - haystack + 1 : 0);
2438}
2439
2440
2441/*
2442 $(substr str, start[, length[, pad]])
2443
2444 XXX: This doesn't take multibyte locales into account.
2445 */
2446static char *
2447func_substr (char *o, char **argv, const char *funcname UNUSED)
2448{
2449 const char *str = argv[0];
2450 size_t str_len = strlen (str);
2451 math_int start = math_int_from_string (argv[1]);
2452 math_int length = 0;
2453 const char *pad = NULL;
2454 size_t pad_len = 0;
2455
2456 if (argv[2] != NULL)
2457 {
2458 if (argv[3] != NULL)
2459 {
2460 pad = argv[3];
2461 for (pad_len = 0; pad[pad_len] == ' '; pad_len++)
2462 /* nothing */;
2463 if (pad[pad_len] != '\0')
2464 pad_len = strlen (pad);
2465 else
2466 {
2467 pad = " ";
2468 pad_len = 16;
2469 }
2470 }
2471 length = math_int_from_string (argv[2]);
2472 if (length < 0 || (pad != NULL && length > 16*1024*1024 /* 16MB */))
2473 fatal (NILF, _("$(substr ): length=%s is out of bounds\n"), argv[3]);
2474 if (length == 0)
2475 return o;
2476 }
2477
2478 /* adjust start and length. */
2479 if (pad == NULL)
2480 {
2481 if (start > 0)
2482 {
2483 start--; /* one-origin */
2484 if (start >= str_len)
2485 return o;
2486 if (length == 0 || start + length > str_len)
2487 length = str_len - start;
2488 }
2489 else
2490 {
2491 start = str_len + start;
2492 if (start <= 0)
2493 {
2494 start += length;
2495 if (start <= 0)
2496 return o;
2497 length = start;
2498 start = 0;
2499 }
2500 else if (length == 0 || start + length > str_len)
2501 length = str_len - start;
2502 }
2503
2504 o = variable_buffer_output (o, str + start, length);
2505 }
2506 else
2507 {
2508 if (start > 0)
2509 {
2510 start--; /* one-origin */
2511 if (start >= str_len)
2512 return length ? helper_insert_pad (o, length, pad, pad_len) : o;
2513 if (length == 0)
2514 length = str_len - start;
2515 }
2516 else
2517 {
2518 start = str_len + start;
2519 if (start <= 0)
2520 {
2521 if (start + length <= 0)
2522 return length ? helper_insert_pad (o, length, pad, pad_len) : o;
2523 o = helper_insert_pad (o, -start, pad, pad_len);
2524 return variable_buffer_output (o, str, length + start);
2525 }
2526 if (length == 0)
2527 length = str_len - start;
2528 }
2529 if (start + length <= str_len)
2530 o = variable_buffer_output (o, str + start, length);
2531 else
2532 {
2533 o = variable_buffer_output (o, str + start, str_len - start);
2534 o = helper_insert_pad (o, start + length - str_len, pad, pad_len);
2535 }
2536 }
2537
2538 return o;
2539}
2540
2541
2542/*
2543 $(translate string, from-set[, to-set[, pad-char]])
2544
2545 XXX: This doesn't take multibyte locales into account.
2546 */
2547static char *
2548func_translate (char *o, char **argv, const char *funcname UNUSED)
2549{
2550 const unsigned char *str = (const unsigned char *)argv[0];
2551 const unsigned char *from_set = (const unsigned char *)argv[1];
2552 const char *to_set = argv[2] != NULL ? argv[2] : "";
2553 char trans_tab[1 << CHAR_BIT];
2554 int i;
2555 char ch;
2556
2557 /* init the array. */
2558 for (i = 0; i < (1 << CHAR_BIT); i++)
2559 trans_tab[i] = i;
2560
2561 while ( (i = *from_set) != '\0'
2562 && (ch = *to_set) != '\0')
2563 {
2564 trans_tab[i] = ch;
2565 from_set++;
2566 to_set++;
2567 }
2568
2569 if (i != '\0')
2570 {
2571 ch = '\0'; /* no padding == remove char */
2572 if (argv[2] != NULL && argv[3] != NULL)
2573 {
2574 ch = argv[3][0];
2575 if (ch && argv[3][1])
2576 fatal (NILF, _("$(translate ): pad=`%s' expected a single char\n"), argv[3]);
2577 if (ch == '\0') /* no char == space */
2578 ch = ' ';
2579 }
2580 while ((i = *from_set++) != '\0')
2581 trans_tab[i] = ch;
2582 }
2583
2584 /* do the translation */
2585 while ((i = *str++) != '\0')
2586 {
2587 ch = trans_tab[i];
2588 if (ch)
2589 o = variable_buffer_output (o, &ch, 1);
2590 }
2591
2592 return o;
2593}
2594#endif /* CONFIG_WITH_STRING_FUNCTIONS */
2595
2596
2597#ifdef CONFIG_WITH_LAZY_DEPS_VARS
2598
2599/* This is also in file.c (bad). */
2600# if VMS
2601# define FILE_LIST_SEPARATOR ','
2602# else
2603# define FILE_LIST_SEPARATOR ' '
2604# endif
2605
2606/* Implements $^ and $+.
2607
2608 The first is somes with with FUNCNAME 'deps', the second as 'deps-all'.
2609
2610 If no second argument is given, or if it's empty, or if it's zero,
2611 all dependencies will be returned. If the second argument is non-zero
2612 the dependency at that position will be returned. If the argument is
2613 negative a fatal error is thrown. */
2614static char *
2615func_deps (char *o, char **argv, const char *funcname)
2616{
2617 unsigned int idx = 0;
2618 struct file *file;
2619
2620 /* Handle the argument if present. */
2621
2622 if (argv[1])
2623 {
2624 char *p = argv[1];
2625 while (isspace ((unsigned int)*p))
2626 p++;
2627 if (*p != '\0')
2628 {
2629 char *n;
2630 long l = strtol (p, &n, 0);
2631 while (isspace ((unsigned int)*n))
2632 n++;
2633 idx = l;
2634 if (*n != '\0' || l < 0 || (long)idx != l)
2635 fatal (NILF, _("%s: invalid index value: `%s'\n"), funcname, p);
2636 }
2637 }
2638
2639 /* Find the file and select the list corresponding to FUNCNAME. */
2640
2641 file = lookup_file (argv[0]);
2642 if (file)
2643 {
2644 struct dep *deps = funcname[4] != '\0' && file->org_deps
2645 ? file->org_deps : file->deps;
2646 struct dep *d;
2647
2648 if ( file->double_colon
2649 && ( file->double_colon != file
2650 || file->last != file))
2651 error (NILF, _("$(%s ) cannot be used on files with multiple double colon rules like `%s'\n"),
2652 funcname, file->name);
2653
2654 if (idx == 0 /* all */)
2655 {
2656 unsigned int total_len = 0;
2657
2658 /* calc the result length. */
2659
2660 for (d = deps; d; d = d->next)
2661 if (!d->ignore_mtime)
2662 {
2663 const char *c = dep_name (d);
2664
2665#ifndef NO_ARCHIVES
2666 if (ar_name (c))
2667 {
2668 c = strchr (c, '(') + 1;
2669 total_len += strlen (c);
2670 }
2671 else
2672#elif defined (CONFIG_WITH_STRCACHE2)
2673 total_len += strcache2_get_len (&file_strcache, c) + 1;
2674#else
2675 total_len += strlen (c) + 1;
2676#endif
2677 }
2678
2679 if (total_len)
2680 {
2681 /* prepare the variable buffer dude wrt to the output size and
2682 pass along the strings. */
2683
2684 o = variable_buffer_output (o + total_len, "", 0) - total_len; /* a hack */
2685
2686 for (d = deps; d; d = d->next)
2687 if (!d->ignore_mtime)
2688 {
2689 unsigned int len;
2690 const char *c = dep_name (d);
2691
2692#ifndef NO_ARCHIVES
2693 if (ar_name (c))
2694 {
2695 c = strchr (c, '(') + 1;
2696 len = strlen (c);
2697 }
2698 else
2699#elif defined (CONFIG_WITH_STRCACHE2)
2700 len = strcache2_get_len (&file_strcache, c) + 1;
2701#else
2702 len = strlen (c) + 1;
2703#endif
2704 o = variable_buffer_output (o, c, len);
2705 o[-1] = FILE_LIST_SEPARATOR;
2706 }
2707
2708 --o; /* nuke the last list separator */
2709 *o = '\0';
2710 }
2711 }
2712 else
2713 {
2714 /* Dependency given by index. */
2715
2716 for (d = deps; d; d = d->next)
2717 if (!d->ignore_mtime)
2718 {
2719 if (--idx == 0) /* 1 based indexing */
2720 {
2721 unsigned int len;
2722 const char *c = dep_name (d);
2723
2724#ifndef NO_ARCHIVES
2725 if (ar_name (c))
2726 {
2727 c = strchr (c, '(') + 1;
2728 len = strlen (c) - 1;
2729 }
2730 else
2731#elif defined (CONFIG_WITH_STRCACHE2)
2732 len = strcache2_get_len (&file_strcache, c);
2733#else
2734 len = strlen (c);
2735#endif
2736 o = variable_buffer_output (o, c, len);
2737 break;
2738 }
2739 }
2740 }
2741 }
2742
2743 return o;
2744}
2745
2746/* Implements $?.
2747
2748 If no second argument is given, or if it's empty, or if it's zero,
2749 all dependencies will be returned. If the second argument is non-zero
2750 the dependency at that position will be returned. If the argument is
2751 negative a fatal error is thrown. */
2752static char *
2753func_deps_newer (char *o, char **argv, const char *funcname)
2754{
2755 unsigned int idx = 0;
2756 struct file *file;
2757
2758 /* Handle the argument if present. */
2759
2760 if (argv[1])
2761 {
2762 char *p = argv[1];
2763 while (isspace ((unsigned int)*p))
2764 p++;
2765 if (*p != '\0')
2766 {
2767 char *n;
2768 long l = strtol (p, &n, 0);
2769 while (isspace ((unsigned int)*n))
2770 n++;
2771 idx = l;
2772 if (*n != '\0' || l < 0 || (long)idx != l)
2773 fatal (NILF, _("%s: invalid index value: `%s'\n"), funcname, p);
2774 }
2775 }
2776
2777 /* Find the file. */
2778
2779 file = lookup_file (argv[0]);
2780 if (file)
2781 {
2782 struct dep *deps = file->deps;
2783 struct dep *d;
2784
2785 if ( file->double_colon
2786 && ( file->double_colon != file
2787 || file->last != file))
2788 error (NILF, _("$(%s ) cannot be used on files with multiple double colon rules like `%s'\n"),
2789 funcname, file->name);
2790
2791 if (idx == 0 /* all */)
2792 {
2793 unsigned int total_len = 0;
2794
2795 /* calc the result length. */
2796
2797 for (d = deps; d; d = d->next)
2798 if (!d->ignore_mtime && d->changed)
2799 {
2800 const char *c = dep_name (d);
2801
2802#ifndef NO_ARCHIVES
2803 if (ar_name (c))
2804 {
2805 c = strchr (c, '(') + 1;
2806 total_len += strlen (c);
2807 }
2808 else
2809#elif defined (CONFIG_WITH_STRCACHE2)
2810 total_len += strcache2_get_len (&file_strcache, c) + 1;
2811#else
2812 total_len += strlen (c) + 1;
2813#endif
2814 }
2815
2816 if (total_len)
2817 {
2818 /* prepare the variable buffer dude wrt to the output size and
2819 pass along the strings. */
2820
2821 o = variable_buffer_output (o + total_len, "", 0) - total_len; /* a hack */
2822
2823 for (d = deps; d; d = d->next)
2824 if (!d->ignore_mtime && d->changed)
2825 {
2826 unsigned int len;
2827 const char *c = dep_name (d);
2828
2829#ifndef NO_ARCHIVES
2830 if (ar_name (c))
2831 {
2832 c = strchr (c, '(') + 1;
2833 len = strlen (c);
2834 }
2835 else
2836#elif defined (CONFIG_WITH_STRCACHE2)
2837 len = strcache2_get_len (&file_strcache, c) + 1;
2838#else
2839 len = strlen (c) + 1;
2840#endif
2841 o = variable_buffer_output (o, c, len);
2842 o[-1] = FILE_LIST_SEPARATOR;
2843 }
2844
2845 --o; /* nuke the last list separator */
2846 *o = '\0';
2847 }
2848 }
2849 else
2850 {
2851 /* Dependency given by index. */
2852
2853 for (d = deps; d; d = d->next)
2854 if (!d->ignore_mtime && d->changed)
2855 {
2856 if (--idx == 0) /* 1 based indexing */
2857 {
2858 unsigned int len;
2859 const char *c = dep_name (d);
2860
2861#ifndef NO_ARCHIVES
2862 if (ar_name (c))
2863 {
2864 c = strchr (c, '(') + 1;
2865 len = strlen (c) - 1;
2866 }
2867 else
2868#elif defined (CONFIG_WITH_STRCACHE2)
2869 len = strcache2_get_len (&file_strcache, c);
2870#else
2871 len = strlen (c);
2872#endif
2873 o = variable_buffer_output (o, c, len);
2874 break;
2875 }
2876 }
2877 }
2878 }
2879
2880 return o;
2881}
2882
2883/* Implements $|, the order only dependency list.
2884
2885 If no second argument is given, or if it's empty, or if it's zero,
2886 all dependencies will be returned. If the second argument is non-zero
2887 the dependency at that position will be returned. If the argument is
2888 negative a fatal error is thrown. */
2889static char *
2890func_deps_order_only (char *o, char **argv, const char *funcname)
2891{
2892 unsigned int idx = 0;
2893 struct file *file;
2894
2895 /* Handle the argument if present. */
2896
2897 if (argv[1])
2898 {
2899 char *p = argv[1];
2900 while (isspace ((unsigned int)*p))
2901 p++;
2902 if (*p != '\0')
2903 {
2904 char *n;
2905 long l = strtol (p, &n, 0);
2906 while (isspace ((unsigned int)*n))
2907 n++;
2908 idx = l;
2909 if (*n != '\0' || l < 0 || (long)idx != l)
2910 fatal (NILF, _("%s: invalid index value: `%s'\n"), funcname, p);
2911 }
2912 }
2913
2914 /* Find the file. */
2915
2916 file = lookup_file (argv[0]);
2917 if (file)
2918 {
2919 struct dep *deps = file->deps;
2920 struct dep *d;
2921
2922 if ( file->double_colon
2923 && ( file->double_colon != file
2924 || file->last != file))
2925 error (NILF, _("$(%s ) cannot be used on files with multiple double colon rules like `%s'\n"),
2926 funcname, file->name);
2927
2928 if (idx == 0 /* all */)
2929 {
2930 unsigned int total_len = 0;
2931
2932 /* calc the result length. */
2933
2934 for (d = deps; d; d = d->next)
2935 if (d->ignore_mtime)
2936 {
2937 const char *c = dep_name (d);
2938
2939#ifndef NO_ARCHIVES
2940 if (ar_name (c))
2941 {
2942 c = strchr (c, '(') + 1;
2943 total_len += strlen (c);
2944 }
2945 else
2946#elif defined (CONFIG_WITH_STRCACHE2)
2947 total_len += strcache2_get_len (&file_strcache, c) + 1;
2948#else
2949 total_len += strlen (c) + 1;
2950#endif
2951 }
2952
2953 if (total_len)
2954 {
2955 /* prepare the variable buffer dude wrt to the output size and
2956 pass along the strings. */
2957
2958 o = variable_buffer_output (o + total_len, "", 0) - total_len; /* a hack */
2959
2960 for (d = deps; d; d = d->next)
2961 if (d->ignore_mtime)
2962 {
2963 unsigned int len;
2964 const char *c = dep_name (d);
2965
2966#ifndef NO_ARCHIVES
2967 if (ar_name (c))
2968 {
2969 c = strchr (c, '(') + 1;
2970 len = strlen (c);
2971 }
2972 else
2973#elif defined (CONFIG_WITH_STRCACHE2)
2974 len = strcache2_get_len (&file_strcache, c) + 1;
2975#else
2976 len = strlen (c) + 1;
2977#endif
2978 o = variable_buffer_output (o, c, len);
2979 o[-1] = FILE_LIST_SEPARATOR;
2980 }
2981
2982 --o; /* nuke the last list separator */
2983 *o = '\0';
2984 }
2985 }
2986 else
2987 {
2988 /* Dependency given by index. */
2989
2990 for (d = deps; d; d = d->next)
2991 if (d->ignore_mtime)
2992 {
2993 if (--idx == 0) /* 1 based indexing */
2994 {
2995 unsigned int len;
2996 const char *c = dep_name (d);
2997
2998#ifndef NO_ARCHIVES
2999 if (ar_name (c))
3000 {
3001 c = strchr (c, '(') + 1;
3002 len = strlen (c) - 1;
3003 }
3004 else
3005#elif defined (CONFIG_WITH_STRCACHE2)
3006 len = strcache2_get_len (&file_strcache, c);
3007#else
3008 len = strlen (c);
3009#endif
3010 o = variable_buffer_output (o, c, len);
3011 break;
3012 }
3013 }
3014 }
3015 }
3016
3017 return o;
3018}
3019#endif /* CONFIG_WITH_LAZY_DEPS_VARS */
3020
3021
3022
3023#ifdef CONFIG_WITH_DEFINED
3024/* Similar to ifdef. */
3025static char *
3026func_defined (char *o, char **argv, const char *funcname UNUSED)
3027{
3028 struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
3029 int result = v != NULL && *v->value != '\0';
3030 o = variable_buffer_output (o, result ? "1" : "", result);
3031 return o;
3032}
3033#endif /* CONFIG_WITH_DEFINED*/
3034
3035
3036
3037/* Return the absolute name of file NAME which does not contain any `.',
3038 `..' components nor any repeated path separators ('/'). */
3039#ifdef KMK
3040char *
3041#else
3042static char *
3043#endif
3044abspath (const char *name, char *apath)
3045{
3046 char *dest;
3047 const char *start, *end, *apath_limit;
3048
3049 if (name[0] == '\0' || apath == NULL)
3050 return NULL;
3051
3052#ifdef WINDOWS32 /* bird */
3053 dest = w32ify((char *)name, 1);
3054 if (!dest)
3055 return NULL;
3056 {
3057 size_t len = strlen(dest);
3058 memcpy(apath, dest, len);
3059 dest = apath + len;
3060 }
3061
3062 (void)end; (void)start; (void)apath_limit;
3063
3064#elif defined __OS2__ /* bird */
3065 if (_abspath(apath, name, GET_PATH_MAX))
3066 return NULL;
3067 dest = strchr(apath, '\0');
3068
3069 (void)end; (void)start; (void)apath_limit; (void)dest;
3070
3071#else /* !WINDOWS32 && !__OS2__ */
3072 apath_limit = apath + GET_PATH_MAX;
3073
3074#ifdef HAVE_DOS_PATHS /* bird added this */
3075 if (isalpha(name[0]) && name[1] == ':')
3076 {
3077 /* drive spec */
3078 apath[0] = toupper(name[0]);
3079 apath[1] = ':';
3080 apath[2] = '/';
3081 name += 2;
3082 }
3083 else
3084#endif /* HAVE_DOS_PATHS */
3085 if (name[0] != '/')
3086 {
3087 /* It is unlikely we would make it until here but just to make sure. */
3088 if (!starting_directory)
3089 return NULL;
3090
3091 strcpy (apath, starting_directory);
3092
3093 dest = strchr (apath, '\0');
3094 }
3095 else
3096 {
3097 apath[0] = '/';
3098 dest = apath + 1;
3099 }
3100
3101 for (start = end = name; *start != '\0'; start = end)
3102 {
3103 unsigned long len;
3104
3105 /* Skip sequence of multiple path-separators. */
3106 while (*start == '/')
3107 ++start;
3108
3109 /* Find end of path component. */
3110 for (end = start; *end != '\0' && *end != '/'; ++end)
3111 ;
3112
3113 len = end - start;
3114
3115 if (len == 0)
3116 break;
3117 else if (len == 1 && start[0] == '.')
3118 /* nothing */;
3119 else if (len == 2 && start[0] == '.' && start[1] == '.')
3120 {
3121 /* Back up to previous component, ignore if at root already. */
3122 if (dest > apath + 1)
3123 while ((--dest)[-1] != '/');
3124 }
3125 else
3126 {
3127 if (dest[-1] != '/')
3128 *dest++ = '/';
3129
3130 if (dest + len >= apath_limit)
3131 return NULL;
3132
3133 dest = memcpy (dest, start, len);
3134 dest += len;
3135 *dest = '\0';
3136 }
3137 }
3138#endif /* !WINDOWS32 && !__OS2__ */
3139
3140 /* Unless it is root strip trailing separator. */
3141#ifdef HAVE_DOS_PATHS /* bird (is this correct? what about UNC?) */
3142 if (dest > apath + 1 + (apath[0] != '/') && dest[-1] == '/')
3143#else
3144 if (dest > apath + 1 && dest[-1] == '/')
3145#endif
3146 --dest;
3147
3148 *dest = '\0';
3149
3150 return apath;
3151}
3152
3153
3154static char *
3155func_realpath (char *o, char **argv, const char *funcname UNUSED)
3156{
3157 /* Expand the argument. */
3158 const char *p = argv[0];
3159 const char *path = 0;
3160 int doneany = 0;
3161 unsigned int len = 0;
3162 PATH_VAR (in);
3163 PATH_VAR (out);
3164
3165 while ((path = find_next_token (&p, &len)) != 0)
3166 {
3167 if (len < GET_PATH_MAX)
3168 {
3169 strncpy (in, path, len);
3170 in[len] = '\0';
3171
3172 if (
3173#ifdef HAVE_REALPATH
3174 realpath (in, out)
3175#else
3176 abspath (in, out)
3177#endif
3178 )
3179 {
3180 o = variable_buffer_output (o, out, strlen (out));
3181 o = variable_buffer_output (o, " ", 1);
3182 doneany = 1;
3183 }
3184 }
3185 }
3186
3187 /* Kill last space. */
3188 if (doneany)
3189 --o;
3190
3191 return o;
3192}
3193
3194static char *
3195func_abspath (char *o, char **argv, const char *funcname UNUSED)
3196{
3197 /* Expand the argument. */
3198 const char *p = argv[0];
3199 const char *path = 0;
3200 int doneany = 0;
3201 unsigned int len = 0;
3202 PATH_VAR (in);
3203 PATH_VAR (out);
3204
3205 while ((path = find_next_token (&p, &len)) != 0)
3206 {
3207 if (len < GET_PATH_MAX)
3208 {
3209 strncpy (in, path, len);
3210 in[len] = '\0';
3211
3212 if (abspath (in, out))
3213 {
3214 o = variable_buffer_output (o, out, strlen (out));
3215 o = variable_buffer_output (o, " ", 1);
3216 doneany = 1;
3217 }
3218 }
3219 }
3220
3221 /* Kill last space. */
3222 if (doneany)
3223 --o;
3224
3225 return o;
3226}
3227
3228#ifdef CONFIG_WITH_ABSPATHEX
3229/* Same as abspath except that the current path may be given as the
3230 2nd argument. */
3231static char *
3232func_abspathex (char *o, char **argv, const char *funcname UNUSED)
3233{
3234 char *cwd = argv[1];
3235
3236 /* cwd needs leading spaces chopped and may be optional,
3237 in which case we're exactly like $(abspath ). */
3238 while (isblank(*cwd))
3239 cwd++;
3240 if (!*cwd)
3241 o = func_abspath (o, argv, funcname);
3242 else
3243 {
3244 /* Expand the argument. */
3245 const char *p = argv[0];
3246 unsigned int cwd_len = ~0U;
3247 char *path = 0;
3248 int doneany = 0;
3249 unsigned int len = 0;
3250 PATH_VAR (in);
3251 PATH_VAR (out);
3252
3253 while ((path = find_next_token (&p, &len)) != 0)
3254 {
3255 if (len < GET_PATH_MAX)
3256 {
3257#ifdef HAVE_DOS_PATHS
3258 if (path[0] != '/' && path[0] != '\\' && (len < 2 || path[1] != ':') && cwd)
3259#else
3260 if (path[0] != '/' && cwd)
3261#endif
3262 {
3263 /* relative path, prefix with cwd. */
3264 if (cwd_len == ~0U)
3265 cwd_len = strlen (cwd);
3266 if (cwd_len + len + 1 >= GET_PATH_MAX)
3267 continue;
3268 memcpy (in, cwd, cwd_len);
3269 in[cwd_len] = '/';
3270 memcpy (in + cwd_len + 1, path, len);
3271 in[cwd_len + len + 1] = '\0';
3272 }
3273 else
3274 {
3275 /* absolute path pass it as-is. */
3276 memcpy (in, path, len);
3277 in[len] = '\0';
3278 }
3279
3280 if (abspath (in, out))
3281 {
3282 o = variable_buffer_output (o, out, strlen (out));
3283 o = variable_buffer_output (o, " ", 1);
3284 doneany = 1;
3285 }
3286 }
3287 }
3288
3289 /* Kill last space. */
3290 if (doneany)
3291 --o;
3292 }
3293
3294 return o;
3295}
3296#endif
3297
3298#ifdef CONFIG_WITH_XARGS
3299/* Create one or more command lines avoiding the max argument
3300 length restriction of the host OS.
3301
3302 The last argument is the list of arguments that the normal
3303 xargs command would be fed from stdin.
3304
3305 The first argument is initial command and it's arguments.
3306
3307 If there are three or more arguments, the 2nd argument is
3308 the command and arguments to be used on subsequent
3309 command lines. Defaults to the initial command.
3310
3311 If there are four or more arguments, the 3rd argument is
3312 the command to be used at the final command line. Defaults
3313 to the sub sequent or initial command .
3314
3315 A future version of this function may define more arguments
3316 and therefor anyone specifying six or more arguments will
3317 cause fatal errors.
3318
3319 Typical usage is:
3320 $(xargs ar cas mylib.a,$(objects))
3321 or
3322 $(xargs ar cas mylib.a,ar as mylib.a,$(objects))
3323
3324 It will then create one or more "ar mylib.a ..." command
3325 lines with proper \n\t separation so it can be used when
3326 writing rules. */
3327static char *
3328func_xargs (char *o, char **argv, const char *funcname UNUSED)
3329{
3330 int argc;
3331 const char *initial_cmd;
3332 size_t initial_cmd_len;
3333 const char *subsequent_cmd;
3334 size_t subsequent_cmd_len;
3335 const char *final_cmd;
3336 size_t final_cmd_len;
3337 const char *args;
3338 size_t max_args;
3339 int i;
3340
3341#ifdef ARG_MAX
3342 /* ARG_MAX is a bit unreliable (environment), so drop 25% of the max. */
3343# define XARGS_MAX (ARG_MAX - (ARG_MAX / 4))
3344#else /* FIXME: update configure with a command line length test. */
3345# define XARGS_MAX 10240
3346#endif
3347
3348 argc = 0;
3349 while (argv[argc])
3350 argc++;
3351 if (argc > 4)
3352 fatal (NILF, _("Too many arguments for $(xargs)!\n"));
3353
3354 /* first: the initial / default command.*/
3355 initial_cmd = argv[0];
3356 while (isspace ((unsigned char)*initial_cmd))
3357 initial_cmd++;
3358 max_args = initial_cmd_len = strlen (initial_cmd);
3359
3360 /* second: the command for the subsequent command lines. defaults to the initial cmd. */
3361 subsequent_cmd = argc > 2 && argv[1][0] != '\0' ? argv[1] : "";
3362 while (isspace ((unsigned char)*subsequent_cmd))
3363 subsequent_cmd++;
3364 if (*subsequent_cmd)
3365 {
3366 subsequent_cmd_len = strlen (subsequent_cmd);
3367 if (subsequent_cmd_len > max_args)
3368 max_args = subsequent_cmd_len;
3369 }
3370 else
3371 {
3372 subsequent_cmd = initial_cmd;
3373 subsequent_cmd_len = initial_cmd_len;
3374 }
3375
3376 /* third: the final command. defaults to the subseq cmd. */
3377 final_cmd = argc > 3 && argv[2][0] != '\0' ? argv[2] : "";
3378 while (isspace ((unsigned char)*final_cmd))
3379 final_cmd++;
3380 if (*final_cmd)
3381 {
3382 final_cmd_len = strlen (final_cmd);
3383 if (final_cmd_len > max_args)
3384 max_args = final_cmd_len;
3385 }
3386 else
3387 {
3388 final_cmd = subsequent_cmd;
3389 final_cmd_len = subsequent_cmd_len;
3390 }
3391
3392 /* last: the arguments to split up into sensible portions. */
3393 args = argv[argc - 1];
3394
3395 /* calc the max argument length. */
3396 if (XARGS_MAX <= max_args + 2)
3397 fatal (NILF, _("$(xargs): the commands are longer than the max exec argument length. (%lu <= %lu)\n"),
3398 (unsigned long)XARGS_MAX, (unsigned long)max_args + 2);
3399 max_args = XARGS_MAX - max_args - 1;
3400
3401 /* generate the commands. */
3402 i = 0;
3403 for (i = 0; ; i++)
3404 {
3405 unsigned int len;
3406 const char *iterator = args;
3407 const char *end = args;
3408 const char *cur;
3409 const char *tmp;
3410
3411 /* scan the arguments till we reach the end or the max length. */
3412 while ((cur = find_next_token(&iterator, &len))
3413 && (size_t)((cur + len) - args) < max_args)
3414 end = cur + len;
3415 if (cur && end == args)
3416 fatal (NILF, _("$(xargs): command + one single arg is too much. giving up.\n"));
3417
3418 /* emit the command. */
3419 if (i == 0)
3420 {
3421 o = variable_buffer_output (o, (char *)initial_cmd, initial_cmd_len);
3422 o = variable_buffer_output (o, " ", 1);
3423 }
3424 else if (cur)
3425 {
3426 o = variable_buffer_output (o, "\n\t", 2);
3427 o = variable_buffer_output (o, (char *)subsequent_cmd, subsequent_cmd_len);
3428 o = variable_buffer_output (o, " ", 1);
3429 }
3430 else
3431 {
3432 o = variable_buffer_output (o, "\n\t", 2);
3433 o = variable_buffer_output (o, (char *)final_cmd, final_cmd_len);
3434 o = variable_buffer_output (o, " ", 1);
3435 }
3436
3437 tmp = end;
3438 while (tmp > args && isspace ((unsigned char)tmp[-1])) /* drop trailing spaces. */
3439 tmp--;
3440 o = variable_buffer_output (o, (char *)args, tmp - args);
3441
3442
3443 /* next */
3444 if (!cur)
3445 break;
3446 args = end;
3447 while (isspace ((unsigned char)*args))
3448 args++;
3449 }
3450
3451 return o;
3452}
3453#endif
3454
3455#ifdef CONFIG_WITH_TOUPPER_TOLOWER
3456static char *
3457func_toupper_tolower (char *o, char **argv, const char *funcname)
3458{
3459 /* Expand the argument. */
3460 const char *p = argv[0];
3461 while (*p)
3462 {
3463 /* convert to temporary buffer */
3464 char tmp[256];
3465 unsigned int i;
3466 if (!strcmp(funcname, "toupper"))
3467 for (i = 0; i < sizeof(tmp) && *p; i++, p++)
3468 tmp[i] = toupper(*p);
3469 else
3470 for (i = 0; i < sizeof(tmp) && *p; i++, p++)
3471 tmp[i] = tolower(*p);
3472 o = variable_buffer_output (o, tmp, i);
3473 }
3474
3475 return o;
3476}
3477#endif /* CONFIG_WITH_TOUPPER_TOLOWER */
3478
3479#if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
3480
3481/* Strip leading spaces and other things off a command. */
3482static const char *
3483comp_cmds_strip_leading (const char *s, const char *e)
3484{
3485 while (s < e)
3486 {
3487 const char ch = *s;
3488 if (!isblank (ch)
3489 && ch != '@'
3490#ifdef CONFIG_WITH_COMMANDS_FUNC
3491 && ch != '%'
3492#endif
3493 && ch != '+'
3494 && ch != '-')
3495 break;
3496 s++;
3497 }
3498 return s;
3499}
3500
3501/* Worker for func_comp_vars() which is called if the comparision failed.
3502 It will do the slow command by command comparision of the commands
3503 when there invoked as comp-cmds. */
3504static char *
3505comp_vars_ne (char *o, const char *s1, const char *e1, const char *s2, const char *e2,
3506 char *ne_retval, const char *funcname)
3507{
3508 /* give up at once if not comp-cmds or comp-cmds-ex. */
3509 if (strcmp (funcname, "comp-cmds") != 0
3510 && strcmp (funcname, "comp-cmds-ex") != 0)
3511 o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
3512 else
3513 {
3514 const char * const s1_start = s1;
3515 int new_cmd = 1;
3516 int diff;
3517 for (;;)
3518 {
3519 /* if it's a new command, strip leading stuff. */
3520 if (new_cmd)
3521 {
3522 s1 = comp_cmds_strip_leading (s1, e1);
3523 s2 = comp_cmds_strip_leading (s2, e2);
3524 new_cmd = 0;
3525 }
3526 if (s1 >= e1 || s2 >= e2)
3527 break;
3528
3529 /*
3530 * Inner compare loop which compares one line.
3531 * FIXME: parse quoting!
3532 */
3533 for (;;)
3534 {
3535 const char ch1 = *s1;
3536 const char ch2 = *s2;
3537 diff = ch1 - ch2;
3538 if (diff)
3539 break;
3540 if (ch1 == '\n')
3541 break;
3542 assert (ch1 != '\r');
3543
3544 /* next */
3545 s1++;
3546 s2++;
3547 if (s1 >= e1 || s2 >= e2)
3548 break;
3549 }
3550
3551 /*
3552 * If we exited because of a difference try to end-of-command
3553 * comparision, e.g. ignore trailing spaces.
3554 */
3555 if (diff)
3556 {
3557 /* strip */
3558 while (s1 < e1 && isblank (*s1))
3559 s1++;
3560 while (s2 < e2 && isblank (*s2))
3561 s2++;
3562 if (s1 >= e1 || s2 >= e2)
3563 break;
3564
3565 /* compare again and check that it's a newline. */
3566 if (*s2 != '\n' || *s1 != '\n')
3567 break;
3568 }
3569 /* Break out if we exited because of EOS. */
3570 else if (s1 >= e1 || s2 >= e2)
3571 break;
3572
3573 /*
3574 * Detect the end of command lines.
3575 */
3576 if (*s1 == '\n')
3577 new_cmd = s1 == s1_start || s1[-1] != '\\';
3578 s1++;
3579 s2++;
3580 }
3581
3582 /*
3583 * Ignore trailing empty lines.
3584 */
3585 if (s1 < e1 || s2 < e2)
3586 {
3587 while (s1 < e1 && (isblank (*s1) || *s1 == '\n'))
3588 if (*s1++ == '\n')
3589 s1 = comp_cmds_strip_leading (s1, e1);
3590 while (s2 < e2 && (isblank (*s2) || *s2 == '\n'))
3591 if (*s2++ == '\n')
3592 s2 = comp_cmds_strip_leading (s2, e2);
3593 }
3594
3595 /* emit the result. */
3596 if (s1 == e1 && s2 == e2)
3597 o = variable_buffer_output (o, "", 1) - 1; /** @todo check why this was necessary back the... */
3598 else
3599 o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
3600 }
3601 return o;
3602}
3603
3604/*
3605 $(comp-vars var1,var2,not-equal-return)
3606 or
3607 $(comp-cmds cmd-var1,cmd-var2,not-equal-return)
3608
3609 Compares the two variables (that's given by name to avoid unnecessary
3610 expanding) and return the string in the third argument if not equal.
3611 If equal, nothing is returned.
3612
3613 comp-vars will to an exact comparision only stripping leading and
3614 trailing spaces.
3615
3616 comp-cmds will compare command by command, ignoring not only leading
3617 and trailing spaces on each line but also leading one leading '@',
3618 '-', '+' and '%'
3619*/
3620static char *
3621func_comp_vars (char *o, char **argv, const char *funcname)
3622{
3623 const char *s1, *e1, *x1, *s2, *e2, *x2;
3624 char *a1 = NULL, *a2 = NULL;
3625 size_t l, l1, l2;
3626 struct variable *var1 = lookup_variable (argv[0], strlen (argv[0]));
3627 struct variable *var2 = lookup_variable (argv[1], strlen (argv[1]));
3628
3629 /* the simple cases */
3630 if (var1 == var2)
3631 return variable_buffer_output (o, "", 0); /* eq */
3632 if (!var1 || !var2)
3633 return variable_buffer_output (o, argv[2], strlen(argv[2]));
3634 if (var1->value == var2->value)
3635 return variable_buffer_output (o, "", 0); /* eq */
3636 if (!var1->recursive && !var2->recursive)
3637 {
3638 if ( var1->value_length == var2->value_length
3639 && !memcmp (var1->value, var2->value, var1->value_length))
3640 return variable_buffer_output (o, "", 0); /* eq */
3641
3642 /* ignore trailing and leading blanks */
3643 s1 = var1->value;
3644 e1 = s1 + var1->value_length;
3645 while (isblank ((unsigned char) *s1))
3646 s1++;
3647 while (e1 > s1 && isblank ((unsigned char) e1[-1]))
3648 e1--;
3649
3650 s2 = var2->value;
3651 e2 = s2 + var2->value_length;
3652 while (isblank ((unsigned char) *s2))
3653 s2++;
3654 while (e2 > s2 && isblank ((unsigned char) e2[-1]))
3655 e2--;
3656
3657 if (e1 - s1 != e2 - s2)
3658 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3659 if (!memcmp (s1, s2, e1 - s1))
3660 return variable_buffer_output (o, "", 0); /* eq */
3661 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3662 }
3663
3664 /* ignore trailing and leading blanks */
3665 s1 = var1->value;
3666 e1 = s1 + var1->value_length;
3667 while (isblank ((unsigned char) *s1))
3668 s1++;
3669 while (e1 > s1 && isblank ((unsigned char) e1[-1]))
3670 e1--;
3671
3672 s2 = var2->value;
3673 e2 = s2 + var2->value_length;
3674 while (isblank((unsigned char)*s2))
3675 s2++;
3676 while (e2 > s2 && isblank ((unsigned char) e2[-1]))
3677 e2--;
3678
3679 /* both empty after stripping? */
3680 if (s1 == e1 && s2 == e2)
3681 return variable_buffer_output (o, "", 0); /* eq */
3682
3683 /* optimist. */
3684 if ( e1 - s1 == e2 - s2
3685 && !memcmp(s1, s2, e1 - s1))
3686 return variable_buffer_output (o, "", 0); /* eq */
3687
3688 /* compare up to the first '$' or the end. */
3689 x1 = var1->recursive ? memchr (s1, '$', e1 - s1) : NULL;
3690 x2 = var2->recursive ? memchr (s2, '$', e2 - s2) : NULL;
3691 if (!x1 && !x2)
3692 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3693
3694 l1 = x1 ? x1 - s1 : e1 - s1;
3695 l2 = x2 ? x2 - s2 : e2 - s2;
3696 l = l1 <= l2 ? l1 : l2;
3697 if (l && memcmp (s1, s2, l))
3698 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3699
3700 /* one or both buffers now require expanding. */
3701 if (!x1)
3702 s1 += l;
3703 else
3704 {
3705 s1 = a1 = allocated_variable_expand ((char *)s1 + l);
3706 if (!l)
3707 while (isblank ((unsigned char) *s1))
3708 s1++;
3709 e1 = strchr (s1, '\0');
3710 while (e1 > s1 && isblank ((unsigned char) e1[-1]))
3711 e1--;
3712 }
3713
3714 if (!x2)
3715 s2 += l;
3716 else
3717 {
3718 s2 = a2 = allocated_variable_expand ((char *)s2 + l);
3719 if (!l)
3720 while (isblank ((unsigned char) *s2))
3721 s2++;
3722 e2 = strchr (s2, '\0');
3723 while (e2 > s2 && isblank ((unsigned char) e2[-1]))
3724 e2--;
3725 }
3726
3727 /* the final compare */
3728 if ( e1 - s1 != e2 - s2
3729 || memcmp (s1, s2, e1 - s1))
3730 o = comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3731 else
3732 o = variable_buffer_output (o, "", 1) - 1; /* eq */ /** @todo check why this was necessary back the... */
3733 if (a1)
3734 free (a1);
3735 if (a2)
3736 free (a2);
3737 return o;
3738}
3739
3740/*
3741 $(comp-cmds-ex cmds1,cmds2,not-equal-return)
3742
3743 Compares the two strings and return the string in the third argument
3744 if not equal. If equal, nothing is returned.
3745
3746 The comparision will be performed command by command, ignoring not
3747 only leading and trailing spaces on each line but also leading one
3748 leading '@', '-', '+' and '%'.
3749*/
3750static char *
3751func_comp_cmds_ex (char *o, char **argv, const char *funcname)
3752{
3753 const char *s1, *e1, *s2, *e2;
3754 size_t l1, l2;
3755
3756 /* the simple cases */
3757 s1 = argv[0];
3758 s2 = argv[1];
3759 if (s1 == s2)
3760 return variable_buffer_output (o, "", 0); /* eq */
3761 l1 = strlen (argv[0]);
3762 l2 = strlen (argv[1]);
3763
3764 if ( l1 == l2
3765 && !memcmp (s1, s2, l1))
3766 return variable_buffer_output (o, "", 0); /* eq */
3767
3768 /* ignore trailing and leading blanks */
3769 e1 = s1 + l1;
3770 s1 = comp_cmds_strip_leading (s1, e1);
3771
3772 e2 = s2 + l2;
3773 s2 = comp_cmds_strip_leading (s2, e2);
3774
3775 if (e1 - s1 != e2 - s2)
3776 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3777 if (!memcmp (s1, s2, e1 - s1))
3778 return variable_buffer_output (o, "", 0); /* eq */
3779 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3780}
3781#endif
3782
3783#ifdef CONFIG_WITH_DATE
3784# if defined (_MSC_VER) /* FIXME: !defined (HAVE_STRPTIME) */
3785char *strptime(const char *s, const char *format, struct tm *tm)
3786{
3787 return (char *)"strptime is not implemented";
3788}
3789# endif
3790/* Check if the string is all blanks or not. */
3791static int
3792all_blanks (const char *s)
3793{
3794 if (!s)
3795 return 1;
3796 while (isspace ((unsigned char)*s))
3797 s++;
3798 return *s == '\0';
3799}
3800
3801/* The first argument is the strftime format string, a iso
3802 timestamp is the default if nothing is given.
3803
3804 The second argument is a time value if given. The format
3805 is either the format from the first argument or given as
3806 an additional third argument. */
3807static char *
3808func_date (char *o, char **argv, const char *funcname)
3809{
3810 char *p;
3811 char *buf;
3812 size_t buf_size;
3813 struct tm t;
3814 const char *format;
3815
3816 /* determin the format - use a single word as the default. */
3817 format = !strcmp (funcname, "date-utc")
3818 ? "%Y-%m-%dT%H:%M:%SZ"
3819 : "%Y-%m-%dT%H:%M:%S";
3820 if (!all_blanks (argv[0]))
3821 format = argv[0];
3822
3823 /* get the time. */
3824 memset (&t, 0, sizeof(t));
3825 if (argv[0] && !all_blanks (argv[1]))
3826 {
3827 const char *input_format = !all_blanks (argv[2]) ? argv[2] : format;
3828 p = strptime (argv[1], input_format, &t);
3829 if (!p || *p != '\0')
3830 {
3831 error (NILF, _("$(%s): strptime(%s,%s,) -> %s\n"), funcname,
3832 argv[1], input_format, p ? p : "<null>");
3833 return variable_buffer_output (o, "", 0);
3834 }
3835 }
3836 else
3837 {
3838 time_t tval;
3839 time (&tval);
3840 if (!strcmp (funcname, "date-utc"))
3841 t = *gmtime (&tval);
3842 else
3843 t = *localtime (&tval);
3844 }
3845
3846 /* format it. note that zero isn't necessarily an error, so we'll
3847 have to keep shut about failures. */
3848 buf_size = 64;
3849 buf = xmalloc (buf_size);
3850 while (strftime (buf, buf_size, format, &t) == 0)
3851 {
3852 if (buf_size >= 4096)
3853 {
3854 *buf = '\0';
3855 break;
3856 }
3857 buf = xrealloc (buf, buf_size <<= 1);
3858 }
3859 o = variable_buffer_output (o, buf, strlen (buf));
3860 free (buf);
3861 return o;
3862}
3863#endif
3864
3865#ifdef CONFIG_WITH_FILE_SIZE
3866/* Prints the size of the specified file. Only one file is
3867 permitted, notthing is stripped. -1 is returned if stat
3868 fails. */
3869static char *
3870func_file_size (char *o, char **argv, const char *funcname UNUSED)
3871{
3872 struct stat st;
3873 if (stat (argv[0], &st))
3874 return variable_buffer_output (o, "-1", 2);
3875 return math_int_to_variable_buffer (o, st.st_size);
3876}
3877#endif
3878
3879#ifdef CONFIG_WITH_WHICH
3880/* Checks if the specified file exists an is executable.
3881 On systems employing executable extensions, the name may
3882 be modified to include the extension. */
3883static int func_which_test_x (char *file)
3884{
3885 struct stat st;
3886# if defined(WINDOWS32) || defined(__OS2__)
3887 char *ext;
3888 char *slash;
3889
3890 /* fix slashes first. */
3891 slash = file;
3892 while ((slash = strchr (slash, '\\')) != NULL)
3893 *slash++ = '/';
3894
3895 /* straight */
3896 if (stat (file, &st) == 0
3897 && S_ISREG (st.st_mode))
3898 return 1;
3899
3900 /* don't try add an extension if there already is one */
3901 ext = strchr (file, '\0');
3902 if (ext - file >= 4
3903 && ( !stricmp (ext - 4, ".exe")
3904 || !stricmp (ext - 4, ".cmd")
3905 || !stricmp (ext - 4, ".bat")
3906 || !stricmp (ext - 4, ".com")))
3907 return 0;
3908
3909 /* try the extensions. */
3910 strcpy (ext, ".exe");
3911 if (stat (file, &st) == 0
3912 && S_ISREG (st.st_mode))
3913 return 1;
3914
3915 strcpy (ext, ".cmd");
3916 if (stat (file, &st) == 0
3917 && S_ISREG (st.st_mode))
3918 return 1;
3919
3920 strcpy (ext, ".bat");
3921 if (stat (file, &st) == 0
3922 && S_ISREG (st.st_mode))
3923 return 1;
3924
3925 strcpy (ext, ".com");
3926 if (stat (file, &st) == 0
3927 && S_ISREG (st.st_mode))
3928 return 1;
3929
3930 return 0;
3931
3932# else
3933
3934 return access (file, X_OK) == 0
3935 && stat (file, &st) == 0
3936 && S_ISREG (st.st_mode);
3937# endif
3938}
3939
3940/* Searches for the specified programs in the PATH and print
3941 their full location if found. Prints nothing if not found. */
3942static char *
3943func_which (char *o, char **argv, const char *funcname UNUSED)
3944{
3945 const char *path;
3946 struct variable *path_var;
3947 unsigned i;
3948 int first = 1;
3949 PATH_VAR (buf);
3950
3951 path_var = lookup_variable ("PATH", 4);
3952 if (path_var)
3953 path = path_var->value;
3954 else
3955 path = ".";
3956
3957 /* iterate input */
3958 for (i = 0; argv[i]; i++)
3959 {
3960 unsigned int len;
3961 const char *iterator = argv[i];
3962 char *cur;
3963
3964 while ((cur = find_next_token (&iterator, &len)))
3965 {
3966 /* if there is a separator, don't walk the path. */
3967 if (memchr (cur, '/', len)
3968#ifdef HAVE_DOS_PATHS
3969 || memchr (cur, '\\', len)
3970 || memchr (cur, ':', len)
3971#endif
3972 )
3973 {
3974 if (len + 1 + 4 < GET_PATH_MAX) /* +4 for .exe */
3975 {
3976 memcpy (buf, cur, len);
3977 buf[len] = '\0';
3978 if (func_which_test_x (buf))
3979 o = variable_buffer_output (o, buf, strlen (buf));
3980 }
3981 }
3982 else
3983 {
3984 const char *comp = path;
3985 for (;;)
3986 {
3987 const char *src = comp;
3988 const char *end = strchr (comp, PATH_SEPARATOR_CHAR);
3989 size_t comp_len = end ? (size_t)(end - comp) : strlen (comp);
3990 if (!comp_len)
3991 {
3992 comp_len = 1;
3993 src = ".";
3994 }
3995 if (len + comp_len + 2 + 4 < GET_PATH_MAX) /* +4 for .exe */
3996 {
3997 memcpy (buf, comp, comp_len);
3998 buf [comp_len] = '/';
3999 memcpy (&buf[comp_len + 1], cur, len);
4000 buf[comp_len + 1 + len] = '\0';
4001
4002 if (func_which_test_x (buf))
4003 {
4004 if (!first)
4005 o = variable_buffer_output (o, " ", 1);
4006 o = variable_buffer_output (o, buf, strlen (buf));
4007 first = 0;
4008 break;
4009 }
4010 }
4011
4012 /* next */
4013 if (!end)
4014 break;
4015 comp = end + 1;
4016 }
4017 }
4018 }
4019 }
4020
4021 return variable_buffer_output (o, "", 0);
4022}
4023#endif /* CONFIG_WITH_WHICH */
4024
4025#ifdef CONFIG_WITH_IF_CONDITIONALS
4026
4027/* Evaluates the expression given in the argument using the
4028 same evaluator as for the new 'if' statements, except now
4029 we don't force the result into a boolean like for 'if' and
4030 '$(if-expr ,,)'. */
4031static char *
4032func_expr (char *o, char **argv, const char *funcname UNUSED)
4033{
4034 o = expr_eval_to_string (o, argv[0]);
4035 return o;
4036}
4037
4038/* Same as '$(if ,,)' except the first argument is evaluated
4039 using the same evaluator as for the new 'if' statements. */
4040static char *
4041func_if_expr (char *o, char **argv, const char *funcname UNUSED)
4042{
4043 int rc;
4044 char *to_expand;
4045
4046 /* Evaluate the condition in argv[0] and expand the 2nd or
4047 3rd argument according to the result. */
4048 rc = expr_eval_if_conditionals (argv[0], NULL);
4049 to_expand = rc == 0 ? argv[1] : argv[2];
4050 if (*to_expand)
4051 {
4052 char *expansion = expand_argument (to_expand, NULL);
4053
4054 o = variable_buffer_output (o, expansion, strlen (expansion));
4055
4056 free (expansion);
4057 }
4058
4059 return o;
4060}
4061
4062#endif /* CONFIG_WITH_IF_CONDITIONALS */
4063
4064#ifdef CONFIG_WITH_SET_CONDITIONALS
4065static char *
4066func_set_intersects (char *o, char **argv, const char *funcname UNUSED)
4067{
4068 const char *s1_cur;
4069 unsigned int s1_len;
4070 const char *s1_iterator = argv[0];
4071
4072 while ((s1_cur = find_next_token (&s1_iterator, &s1_len)) != 0)
4073 {
4074 const char *s2_cur;
4075 unsigned int s2_len;
4076 const char *s2_iterator = argv[1];
4077 while ((s2_cur = find_next_token (&s2_iterator, &s2_len)) != 0)
4078 if (s2_len == s1_len
4079 && strneq (s2_cur, s1_cur, s1_len) )
4080 return variable_buffer_output (o, "1", 1); /* found intersection */
4081 }
4082
4083 return o; /* no intersection */
4084}
4085#endif /* CONFIG_WITH_SET_CONDITIONALS */
4086
4087#ifdef CONFIG_WITH_STACK
4088
4089/* Push an item (string without spaces). */
4090static char *
4091func_stack_push (char *o, char **argv, const char *funcname UNUSED)
4092{
4093 do_variable_definition(NILF, argv[0], argv[1], o_file, f_append, 0 /* !target_var */);
4094 return o;
4095}
4096
4097/* Pops an item off the stack / get the top stack element.
4098 (This is what's tricky to do in pure GNU make syntax.) */
4099static char *
4100func_stack_pop_top (char *o, char **argv, const char *funcname)
4101{
4102 struct variable *stack_var;
4103 const char *stack = argv[0];
4104
4105 stack_var = lookup_variable (stack, strlen (stack) );
4106 if (stack_var)
4107 {
4108 unsigned int len;
4109 const char *iterator = stack_var->value;
4110 char *lastitem = NULL;
4111 char *cur;
4112
4113 while ((cur = find_next_token (&iterator, &len)))
4114 lastitem = cur;
4115
4116 if (lastitem != NULL)
4117 {
4118 if (strcmp (funcname, "stack-popv") != 0)
4119 o = variable_buffer_output (o, lastitem, len);
4120 if (strcmp (funcname, "stack-top") != 0)
4121 {
4122 *lastitem = '\0';
4123 while (lastitem > stack_var->value && isspace (lastitem[-1]))
4124 *--lastitem = '\0';
4125#ifdef CONFIG_WITH_VALUE_LENGTH
4126 stack_var->value_length = lastitem - stack_var->value;
4127#endif
4128 }
4129 }
4130 }
4131 return o;
4132}
4133#endif /* CONFIG_WITH_STACK */
4134
4135#if defined (CONFIG_WITH_MATH) || defined (CONFIG_WITH_NANOTS) || defined (CONFIG_WITH_FILE_SIZE)
4136/* outputs the number (as a string) into the variable buffer. */
4137static char *
4138math_int_to_variable_buffer (char *o, math_int num)
4139{
4140 static const char xdigits[17] = "0123456789abcdef";
4141 int negative;
4142 char strbuf[24]; /* 16 hex + 2 prefix + sign + term => 20
4143 or 20 dec + sign + term => 22 */
4144 char *str = &strbuf[sizeof (strbuf) - 1];
4145
4146 negative = num < 0;
4147 if (negative)
4148 num = -num;
4149
4150 *str = '\0';
4151
4152 do
4153 {
4154#ifdef HEX_MATH_NUMBERS
4155 *--str = xdigits[num & 0xf];
4156 num >>= 4;
4157#else
4158 *--str = xdigits[num % 10];
4159 num /= 10;
4160#endif
4161 }
4162 while (num);
4163
4164#ifdef HEX_MATH_NUMBERS
4165 *--str = 'x';
4166 *--str = '0';
4167#endif
4168
4169 if (negative)
4170 *--str = '-';
4171
4172 return variable_buffer_output (o, str, &strbuf[sizeof (strbuf) - 1] - str);
4173}
4174#endif /* CONFIG_WITH_MATH || CONFIG_WITH_NANOTS */
4175
4176#ifdef CONFIG_WITH_MATH
4177
4178/* Converts a string to an integer, causes an error if the format is invalid. */
4179static math_int
4180math_int_from_string (const char *str)
4181{
4182 const char *start;
4183 unsigned base = 0;
4184 int negative = 0;
4185 math_int num = 0;
4186
4187 /* strip spaces */
4188 while (isspace (*str))
4189 str++;
4190 if (!*str)
4191 {
4192 error (NILF, _("bad number: empty\n"));
4193 return 0;
4194 }
4195 start = str;
4196
4197 /* check for +/- */
4198 while (*str == '+' || *str == '-' || isspace (*str))
4199 if (*str++ == '-')
4200 negative = !negative;
4201
4202 /* check for prefix - we do not accept octal numbers, sorry. */
4203 if (*str == '0' && (str[1] == 'x' || str[1] == 'X'))
4204 {
4205 base = 16;
4206 str += 2;
4207 }
4208 else
4209 {
4210 /* look for a hex digit, if not found treat it as decimal */
4211 const char *p2 = str;
4212 for ( ; *p2; p2++)
4213 if (isxdigit (*p2) && !isdigit (*p2) && isascii (*p2) )
4214 {
4215 base = 16;
4216 break;
4217 }
4218 if (base == 0)
4219 base = 10;
4220 }
4221
4222 /* must have at least one digit! */
4223 if ( !isascii (*str)
4224 || !(base == 16 ? isxdigit (*str) : isdigit (*str)) )
4225 {
4226 error (NILF, _("bad number: '%s'\n"), start);
4227 return 0;
4228 }
4229
4230 /* convert it! */
4231 while (*str && !isspace (*str))
4232 {
4233 int ch = *str++;
4234 if (ch >= '0' && ch <= '9')
4235 ch -= '0';
4236 else if (base == 16 && ch >= 'a' && ch <= 'f')
4237 ch -= 'a' - 10;
4238 else if (base == 16 && ch >= 'A' && ch <= 'F')
4239 ch -= 'A' - 10;
4240 else
4241 {
4242 error (NILF, _("bad number: '%s' (base=%d, pos=%d)\n"), start, base, str - start);
4243 return 0;
4244 }
4245 num *= base;
4246 num += ch;
4247 }
4248
4249 /* check trailing spaces. */
4250 while (isspace (*str))
4251 str++;
4252 if (*str)
4253 {
4254 error (NILF, _("bad number: '%s'\n"), start);
4255 return 0;
4256 }
4257
4258 return negative ? -num : num;
4259}
4260
4261/* Add two or more integer numbers. */
4262static char *
4263func_int_add (char *o, char **argv, const char *funcname UNUSED)
4264{
4265 math_int num;
4266 int i;
4267
4268 num = math_int_from_string (argv[0]);
4269 for (i = 1; argv[i]; i++)
4270 num += math_int_from_string (argv[i]);
4271
4272 return math_int_to_variable_buffer (o, num);
4273}
4274
4275/* Subtract two or more integer numbers. */
4276static char *
4277func_int_sub (char *o, char **argv, const char *funcname UNUSED)
4278{
4279 math_int num;
4280 int i;
4281
4282 num = math_int_from_string (argv[0]);
4283 for (i = 1; argv[i]; i++)
4284 num -= math_int_from_string (argv[i]);
4285
4286 return math_int_to_variable_buffer (o, num);
4287}
4288
4289/* Multiply two or more integer numbers. */
4290static char *
4291func_int_mul (char *o, char **argv, const char *funcname UNUSED)
4292{
4293 math_int num;
4294 int i;
4295
4296 num = math_int_from_string (argv[0]);
4297 for (i = 1; argv[i]; i++)
4298 num *= math_int_from_string (argv[i]);
4299
4300 return math_int_to_variable_buffer (o, num);
4301}
4302
4303/* Divide an integer number by one or more divisors. */
4304static char *
4305func_int_div (char *o, char **argv, const char *funcname UNUSED)
4306{
4307 math_int num;
4308 math_int divisor;
4309 int i;
4310
4311 num = math_int_from_string (argv[0]);
4312 for (i = 1; argv[i]; i++)
4313 {
4314 divisor = math_int_from_string (argv[i]);
4315 if (!divisor)
4316 {
4317 error (NILF, _("divide by zero ('%s')\n"), argv[i]);
4318 return math_int_to_variable_buffer (o, 0);
4319 }
4320 num /= divisor;
4321 }
4322
4323 return math_int_to_variable_buffer (o, num);
4324}
4325
4326
4327/* Divide and return the remainder. */
4328static char *
4329func_int_mod (char *o, char **argv, const char *funcname UNUSED)
4330{
4331 math_int num;
4332 math_int divisor;
4333
4334 num = math_int_from_string (argv[0]);
4335 divisor = math_int_from_string (argv[1]);
4336 if (!divisor)
4337 {
4338 error (NILF, _("divide by zero ('%s')\n"), argv[1]);
4339 return math_int_to_variable_buffer (o, 0);
4340 }
4341 num %= divisor;
4342
4343 return math_int_to_variable_buffer (o, num);
4344}
4345
4346/* 2-complement. */
4347static char *
4348func_int_not (char *o, char **argv, const char *funcname UNUSED)
4349{
4350 math_int num;
4351
4352 num = math_int_from_string (argv[0]);
4353 num = ~num;
4354
4355 return math_int_to_variable_buffer (o, num);
4356}
4357
4358/* Bitwise AND (two or more numbers). */
4359static char *
4360func_int_and (char *o, char **argv, const char *funcname UNUSED)
4361{
4362 math_int num;
4363 int i;
4364
4365 num = math_int_from_string (argv[0]);
4366 for (i = 1; argv[i]; i++)
4367 num &= math_int_from_string (argv[i]);
4368
4369 return math_int_to_variable_buffer (o, num);
4370}
4371
4372/* Bitwise OR (two or more numbers). */
4373static char *
4374func_int_or (char *o, char **argv, const char *funcname UNUSED)
4375{
4376 math_int num;
4377 int i;
4378
4379 num = math_int_from_string (argv[0]);
4380 for (i = 1; argv[i]; i++)
4381 num |= math_int_from_string (argv[i]);
4382
4383 return math_int_to_variable_buffer (o, num);
4384}
4385
4386/* Bitwise XOR (two or more numbers). */
4387static char *
4388func_int_xor (char *o, char **argv, const char *funcname UNUSED)
4389{
4390 math_int num;
4391 int i;
4392
4393 num = math_int_from_string (argv[0]);
4394 for (i = 1; argv[i]; i++)
4395 num ^= math_int_from_string (argv[i]);
4396
4397 return math_int_to_variable_buffer (o, num);
4398}
4399
4400/* Compare two integer numbers. Returns make boolean (true="1"; false=""). */
4401static char *
4402func_int_cmp (char *o, char **argv, const char *funcname)
4403{
4404 math_int num1;
4405 math_int num2;
4406 int rc;
4407
4408 num1 = math_int_from_string (argv[0]);
4409 num2 = math_int_from_string (argv[1]);
4410
4411 funcname += sizeof ("int-") - 1;
4412 if (!strcmp (funcname, "eq"))
4413 rc = num1 == num2;
4414 else if (!strcmp (funcname, "ne"))
4415 rc = num1 != num2;
4416 else if (!strcmp (funcname, "gt"))
4417 rc = num1 > num2;
4418 else if (!strcmp (funcname, "ge"))
4419 rc = num1 >= num2;
4420 else if (!strcmp (funcname, "lt"))
4421 rc = num1 < num2;
4422 else /*if (!strcmp (funcname, "le"))*/
4423 rc = num1 <= num2;
4424
4425 return variable_buffer_output (o, rc ? "1" : "", rc);
4426}
4427
4428#endif /* CONFIG_WITH_MATH */
4429
4430#ifdef CONFIG_WITH_NANOTS
4431/* Returns the current timestamp as nano seconds. The time
4432 source is a high res monotone one if the platform provides
4433 this (and we know about it).
4434
4435 Tip. Use this with int-sub to profile makefile reading
4436 and similar. */
4437static char *
4438func_nanots (char *o, char **argv UNUSED, const char *funcname UNUSED)
4439{
4440 return math_int_to_variable_buffer (o, nano_timestamp ());
4441}
4442#endif
4443
4444#ifdef CONFIG_WITH_OS2_LIBPATH
4445/* Sets or gets the OS/2 libpath variables.
4446
4447 The first argument indicates which variable - BEGINLIBPATH,
4448 ENDLIBPATH, LIBPATHSTRICT or LIBPATH.
4449
4450 The second indicates whether this is a get (not present) or
4451 set (present) operation. When present it is the new value for
4452 the variable. */
4453static char *
4454func_os2_libpath (char *o, char **argv, const char *funcname UNUSED)
4455{
4456 char buf[4096];
4457 ULONG fVar;
4458 APIRET rc;
4459
4460 /* translate variable name (first arg) */
4461 if (!strcmp (argv[0], "BEGINLIBPATH"))
4462 fVar = BEGIN_LIBPATH;
4463 else if (!strcmp (argv[0], "ENDLIBPATH"))
4464 fVar = END_LIBPATH;
4465 else if (!strcmp (argv[0], "LIBPATHSTRICT"))
4466 fVar = LIBPATHSTRICT;
4467 else if (!strcmp (argv[0], "LIBPATH"))
4468 fVar = 0;
4469 else
4470 {
4471 error (NILF, _("$(libpath): unknown variable `%s'"), argv[0]);
4472 return variable_buffer_output (o, "", 0);
4473 }
4474
4475 if (!argv[1])
4476 {
4477 /* get the variable value. */
4478 if (fVar != 0)
4479 {
4480 buf[0] = buf[1] = buf[2] = buf[3] = '\0';
4481 rc = DosQueryExtLIBPATH (buf, fVar);
4482 }
4483 else
4484 rc = DosQueryHeaderInfo (NULLHANDLE, 0, buf, sizeof(buf), QHINF_LIBPATH);
4485 if (rc != NO_ERROR)
4486 {
4487 error (NILF, _("$(libpath): failed to query `%s', rc=%d"), argv[0], rc);
4488 return variable_buffer_output (o, "", 0);
4489 }
4490 o = variable_buffer_output (o, buf, strlen (buf));
4491 }
4492 else
4493 {
4494 /* set the variable value. */
4495 size_t len;
4496 size_t len_max = sizeof (buf) < 2048 ? sizeof (buf) : 2048;
4497 const char *val;
4498 const char *end;
4499
4500 if (fVar == 0)
4501 {
4502 error (NILF, _("$(libpath): LIBPATH is read-only"));
4503 return variable_buffer_output (o, "", 0);
4504 }
4505
4506 /* strip leading and trailing spaces and check for max length. */
4507 val = argv[1];
4508 while (isspace (*val))
4509 val++;
4510 end = strchr (val, '\0');
4511 while (end > val && isspace (end[-1]))
4512 end--;
4513
4514 len = end - val;
4515 if (len >= len_max)
4516 {
4517 error (NILF, _("$(libpath): The new `%s' value is too long (%d bytes, max %d)"),
4518 argv[0], len, len_max);
4519 return variable_buffer_output (o, "", 0);
4520 }
4521
4522 /* make a stripped copy in low memory and try set it. */
4523 memcpy (buf, val, len);
4524 buf[len] = '\0';
4525 rc = DosSetExtLIBPATH (buf, fVar);
4526 if (rc != NO_ERROR)
4527 {
4528 error (NILF, _("$(libpath): failed to set `%s' to `%s', rc=%d"), argv[0], buf, rc);
4529 return variable_buffer_output (o, "", 0);
4530 }
4531
4532 o = variable_buffer_output (o, "", 0);
4533 }
4534 return o;
4535}
4536#endif /* CONFIG_WITH_OS2_LIBPATH */
4537
4538#if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
4539/* Retrieve make statistics. */
4540static char *
4541func_make_stats (char *o, char **argv, const char *funcname UNUSED)
4542{
4543 char buf[512];
4544 int len;
4545
4546 if (!argv[0] || (!argv[0][0] && !argv[1]))
4547 {
4548# ifdef CONFIG_WITH_MAKE_STATS
4549 len = sprintf (buf, "alloc-cur: %5ld/%3ld %3luMB hash: %5lu %2lu%%",
4550 make_stats_allocations,
4551 make_stats_reallocations,
4552 make_stats_allocated / (1024*1024),
4553 make_stats_ht_lookups,
4554 (make_stats_ht_collisions * 100) / make_stats_ht_lookups);
4555 o = variable_buffer_output (o, buf, len);
4556#endif
4557 }
4558 else
4559 {
4560 /* selective */
4561 int i;
4562 for (i = 0; argv[i]; i++)
4563 {
4564 unsigned long val;
4565 if (i != 0)
4566 o = variable_buffer_output (o, " ", 1);
4567 if (0)
4568 continue;
4569# ifdef CONFIG_WITH_MAKE_STATS
4570 else if (!strcmp(argv[i], "allocations"))
4571 val = make_stats_allocations;
4572 else if (!strcmp(argv[i], "reallocations"))
4573 val = make_stats_reallocations;
4574 else if (!strcmp(argv[i], "allocated"))
4575 val = make_stats_allocated;
4576 else if (!strcmp(argv[i], "ht_lookups"))
4577 val = make_stats_ht_lookups;
4578 else if (!strcmp(argv[i], "ht_collisions"))
4579 val = make_stats_ht_collisions;
4580 else if (!strcmp(argv[i], "ht_collisions_pct"))
4581 val = (make_stats_ht_collisions * 100) / make_stats_ht_lookups;
4582#endif
4583 else
4584 {
4585 o = variable_buffer_output (o, argv[i], strlen (argv[i]));
4586 continue;
4587 }
4588
4589 len = sprintf (buf, "%ld", val);
4590 o = variable_buffer_output (o, buf, len);
4591 }
4592 }
4593
4594 return o;
4595}
4596#endif /* CONFIG_WITH_MAKE_STATS */
4597
4598#ifdef CONFIG_WITH_COMMANDS_FUNC
4599/* Gets all the commands for a target, separated by newlines.
4600
4601 This is useful when creating and checking target dependencies since
4602 it reduces the amount of work and the memory consuption. A new prefix
4603 character '%' has been introduced for skipping certain lines, like
4604 for instance the one calling this function and pushing to a dep file.
4605 Blank lines are also skipped.
4606
4607 The commands function takes exactly one argument, which is the name of
4608 the target which commands should be returned.
4609
4610 The commands-sc is identical to commands except that it uses a ';' to
4611 separate the commands.
4612
4613 The commands-usr is similar to commands except that it takes a 2nd
4614 argument that is used to separate the commands. */
4615char *
4616func_commands (char *o, char **argv, const char *funcname)
4617{
4618 struct file *file;
4619 static int recursive = 0;
4620
4621 if (recursive)
4622 {
4623 error (reading_file, _("$(%s ) was invoked recursivly"), funcname);
4624 return variable_buffer_output (o, "recursive", sizeof ("recursive") - 1);
4625 }
4626 if (*argv[0] == '\0')
4627 {
4628 error (reading_file, _("$(%s ) was invoked with an empty target name"), funcname);
4629 return o;
4630 }
4631 recursive = 1;
4632
4633 file = lookup_file (argv[0]);
4634 if (file && file->cmds)
4635 {
4636 unsigned int i;
4637 int cmd_sep_len;
4638 struct commands *cmds = file->cmds;
4639 const char *cmd_sep;
4640
4641 if (!strcmp (funcname, "commands"))
4642 {
4643 cmd_sep = "\n";
4644 cmd_sep_len = 1;
4645 }
4646 else if (!strcmp (funcname, "commands-sc"))
4647 {
4648 cmd_sep = ";";
4649 cmd_sep_len = 1;
4650 }
4651 else /*if (!strcmp (funcname, "commands-usr"))*/
4652 {
4653 cmd_sep = argv[1];
4654 cmd_sep_len = strlen (cmd_sep);
4655 }
4656
4657 initialize_file_variables (file, 1 /* don't search for pattern vars */);
4658 set_file_variables (file, 1 /* early call */);
4659 chop_commands (cmds);
4660
4661 for (i = 0; i < cmds->ncommand_lines; i++)
4662 {
4663 char *p;
4664 char *in, *out, *ref;
4665
4666 /* Skip it if it has a '%' prefix or is blank. */
4667 if (cmds->lines_flags[i] & COMMAND_GETTER_SKIP_IT)
4668 continue;
4669 p = cmds->command_lines[i];
4670 while (isblank ((unsigned char)*p))
4671 p++;
4672 if (*p == '\0')
4673 continue;
4674
4675 /* --- copied from new_job() in job.c --- */
4676
4677 /* Collapse backslash-newline combinations that are inside variable
4678 or function references. These are left alone by the parser so
4679 that they will appear in the echoing of commands (where they look
4680 nice); and collapsed by construct_command_argv when it tokenizes.
4681 But letting them survive inside function invocations loses because
4682 we don't want the functions to see them as part of the text. */
4683
4684 /* IN points to where in the line we are scanning.
4685 OUT points to where in the line we are writing.
4686 When we collapse a backslash-newline combination,
4687 IN gets ahead of OUT. */
4688
4689 in = out = p;
4690 while ((ref = strchr (in, '$')) != 0)
4691 {
4692 ++ref; /* Move past the $. */
4693
4694 if (out != in)
4695 /* Copy the text between the end of the last chunk
4696 we processed (where IN points) and the new chunk
4697 we are about to process (where REF points). */
4698 memmove (out, in, ref - in);
4699
4700 /* Move both pointers past the boring stuff. */
4701 out += ref - in;
4702 in = ref;
4703
4704 if (*ref == '(' || *ref == '{')
4705 {
4706 char openparen = *ref;
4707 char closeparen = openparen == '(' ? ')' : '}';
4708 int count;
4709 char *p2;
4710
4711 *out++ = *in++; /* Copy OPENPAREN. */
4712 /* IN now points past the opening paren or brace.
4713 Count parens or braces until it is matched. */
4714 count = 0;
4715 while (*in != '\0')
4716 {
4717 if (*in == closeparen && --count < 0)
4718 break;
4719 else if (*in == '\\' && in[1] == '\n')
4720 {
4721 /* We have found a backslash-newline inside a
4722 variable or function reference. Eat it and
4723 any following whitespace. */
4724
4725 int quoted = 0;
4726 for (p2 = in - 1; p2 > ref && *p2 == '\\'; --p2)
4727 quoted = !quoted;
4728
4729 if (quoted)
4730 /* There were two or more backslashes, so this is
4731 not really a continuation line. We don't collapse
4732 the quoting backslashes here as is done in
4733 collapse_continuations, because the line will
4734 be collapsed again after expansion. */
4735 *out++ = *in++;
4736 else
4737 {
4738 /* Skip the backslash, newline and
4739 any following whitespace. */
4740 in = next_token (in + 2);
4741
4742 /* Discard any preceding whitespace that has
4743 already been written to the output. */
4744 while (out > ref
4745 && isblank ((unsigned char)out[-1]))
4746 --out;
4747
4748 /* Replace it all with a single space. */
4749 *out++ = ' ';
4750 }
4751 }
4752 else
4753 {
4754 if (*in == openparen)
4755 ++count;
4756
4757 *out++ = *in++;
4758 }
4759 }
4760 }
4761 /* Some of these can be amended ($< perhaps), but we're likely to be called while the
4762 dep expansion happens, so it would have to be on a hackish basis. sad... */
4763 else if (*ref == '<' || *ref == '*' || *ref == '%' || *ref == '^' || *ref == '+')
4764 error (reading_file, _("$(%s ) does not work reliably with $%c in all cases"), funcname, *ref);
4765 }
4766
4767 /* There are no more references in this line to worry about.
4768 Copy the remaining uninteresting text to the output. */
4769 if (out != in)
4770 strcpy (out, in);
4771
4772 /* --- copied from new_job() in job.c --- */
4773
4774 /* Finally, expand the line. */
4775 if (i)
4776 o = variable_buffer_output (o, cmd_sep, cmd_sep_len);
4777 o = variable_expand_for_file_2 (o, cmds->command_lines[i], ~0U, file, NULL);
4778
4779 /* Skip it if it has a '%' prefix or is blank. */
4780 p = o;
4781 while (isblank ((unsigned char)*o)
4782 || *o == '@'
4783 || *o == '-'
4784 || *o == '+')
4785 o++;
4786 if (*o != '\0' && *o != '%')
4787 o = strchr (o, '\0');
4788 else if (i)
4789 o = p - cmd_sep_len;
4790 else
4791 o = p;
4792 } /* for each command line */
4793 }
4794 /* else FIXME: bitch about it? */
4795
4796 recursive = 0;
4797 return o;
4798}
4799#endif /* CONFIG_WITH_COMMANDS_FUNC */
4800
4801#ifdef KMK
4802/* Useful when debugging kmk and/or makefiles. */
4803char *
4804func_breakpoint (char *o, char **argv UNUSED, const char *funcname UNUSED)
4805{
4806#ifdef _MSC_VER
4807 __debugbreak();
4808#elif defined(__i386__) || defined(__x86__) || defined(__X86__) || defined(_M_IX86) || defined(__i386) \
4809 || defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || defined(_M_X64) || defined(__amd64)
4810 __asm__ __volatile__ ("int3\n\t");
4811#else
4812 char *p = (char *)0;
4813 *p = '\0';
4814#endif
4815 return o;
4816}
4817#endif /* KMK */
4818
4819
4820/* Lookup table for builtin functions.
4821
4822 This doesn't have to be sorted; we use a straight lookup. We might gain
4823 some efficiency by moving most often used functions to the start of the
4824 table.
4825
4826 If MAXIMUM_ARGS is 0, that means there is no maximum and all
4827 comma-separated values are treated as arguments.
4828
4829 EXPAND_ARGS means that all arguments should be expanded before invocation.
4830 Functions that do namespace tricks (foreach) don't automatically expand. */
4831
4832static char *func_call (char *o, char **argv, const char *funcname);
4833
4834
4835static struct function_table_entry function_table_init[] =
4836{
4837 /* Name/size */ /* MIN MAX EXP? Function */
4838 { STRING_SIZE_TUPLE("abspath"), 0, 1, 1, func_abspath},
4839 { STRING_SIZE_TUPLE("addprefix"), 2, 2, 1, func_addsuffix_addprefix},
4840 { STRING_SIZE_TUPLE("addsuffix"), 2, 2, 1, func_addsuffix_addprefix},
4841 { STRING_SIZE_TUPLE("basename"), 0, 1, 1, func_basename_dir},
4842 { STRING_SIZE_TUPLE("dir"), 0, 1, 1, func_basename_dir},
4843 { STRING_SIZE_TUPLE("notdir"), 0, 1, 1, func_notdir_suffix},
4844 { STRING_SIZE_TUPLE("subst"), 3, 3, 1, func_subst},
4845 { STRING_SIZE_TUPLE("suffix"), 0, 1, 1, func_notdir_suffix},
4846 { STRING_SIZE_TUPLE("filter"), 2, 2, 1, func_filter_filterout},
4847 { STRING_SIZE_TUPLE("filter-out"), 2, 2, 1, func_filter_filterout},
4848 { STRING_SIZE_TUPLE("findstring"), 2, 2, 1, func_findstring},
4849 { STRING_SIZE_TUPLE("firstword"), 0, 1, 1, func_firstword},
4850 { STRING_SIZE_TUPLE("flavor"), 0, 1, 1, func_flavor},
4851 { STRING_SIZE_TUPLE("join"), 2, 2, 1, func_join},
4852 { STRING_SIZE_TUPLE("lastword"), 0, 1, 1, func_lastword},
4853 { STRING_SIZE_TUPLE("patsubst"), 3, 3, 1, func_patsubst},
4854 { STRING_SIZE_TUPLE("realpath"), 0, 1, 1, func_realpath},
4855#ifdef CONFIG_WITH_RSORT
4856 { STRING_SIZE_TUPLE("rsort"), 0, 1, 1, func_sort},
4857#endif
4858 { STRING_SIZE_TUPLE("shell"), 0, 1, 1, func_shell},
4859 { STRING_SIZE_TUPLE("sort"), 0, 1, 1, func_sort},
4860 { STRING_SIZE_TUPLE("strip"), 0, 1, 1, func_strip},
4861 { STRING_SIZE_TUPLE("wildcard"), 0, 1, 1, func_wildcard},
4862 { STRING_SIZE_TUPLE("word"), 2, 2, 1, func_word},
4863 { STRING_SIZE_TUPLE("wordlist"), 3, 3, 1, func_wordlist},
4864 { STRING_SIZE_TUPLE("words"), 0, 1, 1, func_words},
4865 { STRING_SIZE_TUPLE("origin"), 0, 1, 1, func_origin},
4866 { STRING_SIZE_TUPLE("foreach"), 3, 3, 0, func_foreach},
4867 { STRING_SIZE_TUPLE("call"), 1, 0, 1, func_call},
4868 { STRING_SIZE_TUPLE("info"), 0, 1, 1, func_error},
4869 { STRING_SIZE_TUPLE("error"), 0, 1, 1, func_error},
4870 { STRING_SIZE_TUPLE("warning"), 0, 1, 1, func_error},
4871 { STRING_SIZE_TUPLE("if"), 2, 3, 0, func_if},
4872 { STRING_SIZE_TUPLE("or"), 1, 0, 0, func_or},
4873 { STRING_SIZE_TUPLE("and"), 1, 0, 0, func_and},
4874 { STRING_SIZE_TUPLE("value"), 0, 1, 1, func_value},
4875 { STRING_SIZE_TUPLE("eval"), 0, 1, 1, func_eval},
4876#ifdef CONFIG_WITH_EVALPLUS
4877 { STRING_SIZE_TUPLE("evalctx"), 0, 1, 1, func_evalctx},
4878 { STRING_SIZE_TUPLE("evalval"), 1, 1, 1, func_evalval},
4879 { STRING_SIZE_TUPLE("evalvalctx"), 1, 1, 1, func_evalval},
4880 { STRING_SIZE_TUPLE("evalcall"), 1, 0, 1, func_call},
4881 { STRING_SIZE_TUPLE("evalcall2"), 1, 0, 1, func_call},
4882 { STRING_SIZE_TUPLE("eval-opt-var"), 1, 0, 1, func_eval_optimize_variable},
4883#endif
4884#ifdef EXPERIMENTAL
4885 { STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq},
4886 { STRING_SIZE_TUPLE("not"), 0, 1, 1, func_not},
4887#endif
4888#ifdef CONFIG_WITH_STRING_FUNCTIONS
4889 { STRING_SIZE_TUPLE("length"), 1, 1, 1, func_length},
4890 { STRING_SIZE_TUPLE("length-var"), 1, 1, 1, func_length_var},
4891 { STRING_SIZE_TUPLE("insert"), 2, 5, 1, func_insert},
4892 { STRING_SIZE_TUPLE("pos"), 2, 3, 1, func_pos},
4893 { STRING_SIZE_TUPLE("lastpos"), 2, 3, 1, func_pos},
4894 { STRING_SIZE_TUPLE("substr"), 2, 4, 1, func_substr},
4895 { STRING_SIZE_TUPLE("translate"), 2, 4, 1, func_translate},
4896#endif
4897#ifdef CONFIG_WITH_PRINTF
4898 { STRING_SIZE_TUPLE("printf"), 1, 0, 1, kmk_builtin_func_printf},
4899#endif
4900#ifdef CONFIG_WITH_LAZY_DEPS_VARS
4901 { STRING_SIZE_TUPLE("deps"), 1, 2, 1, func_deps},
4902 { STRING_SIZE_TUPLE("deps-all"), 1, 2, 1, func_deps},
4903 { STRING_SIZE_TUPLE("deps-newer"), 1, 2, 1, func_deps_newer},
4904 { STRING_SIZE_TUPLE("deps-oo"), 1, 2, 1, func_deps_order_only},
4905#endif
4906#ifdef CONFIG_WITH_DEFINED
4907 { STRING_SIZE_TUPLE("defined"), 1, 1, 1, func_defined},
4908#endif
4909#ifdef CONFIG_WITH_TOUPPER_TOLOWER
4910 { STRING_SIZE_TUPLE("toupper"), 0, 1, 1, func_toupper_tolower},
4911 { STRING_SIZE_TUPLE("tolower"), 0, 1, 1, func_toupper_tolower},
4912#endif
4913#ifdef CONFIG_WITH_ABSPATHEX
4914 { STRING_SIZE_TUPLE("abspathex"), 0, 2, 1, func_abspathex},
4915#endif
4916#ifdef CONFIG_WITH_XARGS
4917 { STRING_SIZE_TUPLE("xargs"), 2, 0, 1, func_xargs},
4918#endif
4919#if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
4920 { STRING_SIZE_TUPLE("comp-vars"), 3, 3, 1, func_comp_vars},
4921 { STRING_SIZE_TUPLE("comp-cmds"), 3, 3, 1, func_comp_vars},
4922 { STRING_SIZE_TUPLE("comp-cmds-ex"), 3, 3, 1, func_comp_cmds_ex},
4923#endif
4924#ifdef CONFIG_WITH_DATE
4925 { STRING_SIZE_TUPLE("date"), 0, 1, 1, func_date},
4926 { STRING_SIZE_TUPLE("date-utc"), 0, 3, 1, func_date},
4927#endif
4928#ifdef CONFIG_WITH_FILE_SIZE
4929 { STRING_SIZE_TUPLE("file-size"), 1, 1, 1, func_file_size},
4930#endif
4931#ifdef CONFIG_WITH_WHICH
4932 { STRING_SIZE_TUPLE("which"), 0, 0, 1, func_which},
4933#endif
4934#ifdef CONFIG_WITH_IF_CONDITIONALS
4935 { STRING_SIZE_TUPLE("expr"), 1, 1, 0, func_expr},
4936 { STRING_SIZE_TUPLE("if-expr"), 2, 3, 0, func_if_expr},
4937#endif
4938#ifdef CONFIG_WITH_SET_CONDITIONALS
4939 { STRING_SIZE_TUPLE("intersects"), 2, 2, 1, func_set_intersects},
4940#endif
4941#ifdef CONFIG_WITH_STACK
4942 { STRING_SIZE_TUPLE("stack-push"), 2, 2, 1, func_stack_push},
4943 { STRING_SIZE_TUPLE("stack-pop"), 1, 1, 1, func_stack_pop_top},
4944 { STRING_SIZE_TUPLE("stack-popv"), 1, 1, 1, func_stack_pop_top},
4945 { STRING_SIZE_TUPLE("stack-top"), 1, 1, 1, func_stack_pop_top},
4946#endif
4947#ifdef CONFIG_WITH_MATH
4948 { STRING_SIZE_TUPLE("int-add"), 2, 0, 1, func_int_add},
4949 { STRING_SIZE_TUPLE("int-sub"), 2, 0, 1, func_int_sub},
4950 { STRING_SIZE_TUPLE("int-mul"), 2, 0, 1, func_int_mul},
4951 { STRING_SIZE_TUPLE("int-div"), 2, 0, 1, func_int_div},
4952 { STRING_SIZE_TUPLE("int-mod"), 2, 2, 1, func_int_mod},
4953 { STRING_SIZE_TUPLE("int-not"), 1, 1, 1, func_int_not},
4954 { STRING_SIZE_TUPLE("int-and"), 2, 0, 1, func_int_and},
4955 { STRING_SIZE_TUPLE("int-or"), 2, 0, 1, func_int_or},
4956 { STRING_SIZE_TUPLE("int-xor"), 2, 0, 1, func_int_xor},
4957 { STRING_SIZE_TUPLE("int-eq"), 2, 2, 1, func_int_cmp},
4958 { STRING_SIZE_TUPLE("int-ne"), 2, 2, 1, func_int_cmp},
4959 { STRING_SIZE_TUPLE("int-gt"), 2, 2, 1, func_int_cmp},
4960 { STRING_SIZE_TUPLE("int-ge"), 2, 2, 1, func_int_cmp},
4961 { STRING_SIZE_TUPLE("int-lt"), 2, 2, 1, func_int_cmp},
4962 { STRING_SIZE_TUPLE("int-le"), 2, 2, 1, func_int_cmp},
4963#endif
4964#ifdef CONFIG_WITH_NANOTS
4965 { STRING_SIZE_TUPLE("nanots"), 0, 0, 0, func_nanots},
4966#endif
4967#ifdef CONFIG_WITH_OS2_LIBPATH
4968 { STRING_SIZE_TUPLE("libpath"), 1, 2, 1, func_os2_libpath},
4969#endif
4970#if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
4971 { STRING_SIZE_TUPLE("make-stats"), 0, 0, 0, func_make_stats},
4972#endif
4973#ifdef CONFIG_WITH_COMMANDS_FUNC
4974 { STRING_SIZE_TUPLE("commands"), 1, 1, 1, func_commands},
4975 { STRING_SIZE_TUPLE("commands-sc"), 1, 1, 1, func_commands},
4976 { STRING_SIZE_TUPLE("commands-usr"), 2, 2, 1, func_commands},
4977#endif
4978#ifdef KMK_HELPERS
4979 { STRING_SIZE_TUPLE("kb-src-tool"), 1, 1, 0, func_kbuild_source_tool},
4980 { STRING_SIZE_TUPLE("kb-obj-base"), 1, 1, 0, func_kbuild_object_base},
4981 { STRING_SIZE_TUPLE("kb-obj-suff"), 1, 1, 0, func_kbuild_object_suffix},
4982 { STRING_SIZE_TUPLE("kb-src-prop"), 3, 4, 0, func_kbuild_source_prop},
4983 { STRING_SIZE_TUPLE("kb-src-one"), 0, 1, 0, func_kbuild_source_one},
4984 { STRING_SIZE_TUPLE("kb-exp-tmpl"), 6, 6, 1, func_kbuild_expand_template},
4985#endif
4986#ifdef KMK
4987 { STRING_SIZE_TUPLE("breakpoint"), 0, 0, 0, func_breakpoint},
4988#endif
4989};
4990
4991#define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry))
4992
4993
4994
4995/* These must come after the definition of function_table. */
4996
4997static char *
4998expand_builtin_function (char *o, int argc, char **argv,
4999 const struct function_table_entry *entry_p)
5000{
5001 if (argc < (int)entry_p->minimum_args)
5002 fatal (*expanding_var,
5003 _("insufficient number of arguments (%d) to function `%s'"),
5004 argc, entry_p->name);
5005
5006 /* I suppose technically some function could do something with no
5007 arguments, but so far none do, so just test it for all functions here
5008 rather than in each one. We can change it later if necessary. */
5009
5010 if (!argc)
5011 return o;
5012
5013 if (!entry_p->func_ptr)
5014 fatal (*expanding_var,
5015 _("unimplemented on this platform: function `%s'"), entry_p->name);
5016
5017 return entry_p->func_ptr (o, argv, entry_p->name);
5018}
5019
5020/* Check for a function invocation in *STRINGP. *STRINGP points at the
5021 opening ( or { and is not null-terminated. If a function invocation
5022 is found, expand it into the buffer at *OP, updating *OP, incrementing
5023 *STRINGP past the reference and returning nonzero. If not, return zero. */
5024
5025static int
5026handle_function2 (const struct function_table_entry *entry_p, char **op, const char **stringp) /* bird split it up. */
5027{
5028 char openparen = (*stringp)[0];
5029 char closeparen = openparen == '(' ? ')' : '}';
5030 const char *beg;
5031 const char *end;
5032 int count = 0;
5033 char *abeg = NULL;
5034 char **argv, **argvp;
5035 int nargs;
5036
5037 beg = *stringp + 1;
5038
5039 /* We found a builtin function. Find the beginning of its arguments (skip
5040 whitespace after the name). */
5041
5042 beg = next_token (beg + entry_p->len);
5043
5044 /* Find the end of the function invocation, counting nested use of
5045 whichever kind of parens we use. Since we're looking, count commas
5046 to get a rough estimate of how many arguments we might have. The
5047 count might be high, but it'll never be low. */
5048
5049 for (nargs=1, end=beg; *end != '\0'; ++end)
5050 if (*end == ',')
5051 ++nargs;
5052 else if (*end == openparen)
5053 ++count;
5054 else if (*end == closeparen && --count < 0)
5055 break;
5056
5057 if (count >= 0)
5058 fatal (*expanding_var,
5059 _("unterminated call to function `%s': missing `%c'"),
5060 entry_p->name, closeparen);
5061
5062 *stringp = end;
5063
5064 /* Get some memory to store the arg pointers. */
5065 argvp = argv = alloca (sizeof (char *) * (nargs + 2));
5066
5067 /* Chop the string into arguments, then a nul. As soon as we hit
5068 MAXIMUM_ARGS (if it's >0) assume the rest of the string is part of the
5069 last argument.
5070
5071 If we're expanding, store pointers to the expansion of each one. If
5072 not, make a duplicate of the string and point into that, nul-terminating
5073 each argument. */
5074
5075 if (entry_p->expand_args)
5076 {
5077 const char *p;
5078 for (p=beg, nargs=0; p <= end; ++argvp)
5079 {
5080 const char *next;
5081
5082 ++nargs;
5083
5084 if (nargs == entry_p->maximum_args
5085 || (! (next = find_next_argument (openparen, closeparen, p, end))))
5086 next = end;
5087
5088 *argvp = expand_argument (p, next);
5089 p = next + 1;
5090 }
5091 }
5092 else
5093 {
5094 int len = end - beg;
5095 char *p, *aend;
5096
5097 abeg = xmalloc (len+1);
5098 memcpy (abeg, beg, len);
5099 abeg[len] = '\0';
5100 aend = abeg + len;
5101
5102 for (p=abeg, nargs=0; p <= aend; ++argvp)
5103 {
5104 char *next;
5105
5106 ++nargs;
5107
5108 if (nargs == entry_p->maximum_args
5109 || (! (next = find_next_argument (openparen, closeparen, p, aend))))
5110 next = aend;
5111
5112 *argvp = p;
5113 *next = '\0';
5114 p = next + 1;
5115 }
5116 }
5117 *argvp = NULL;
5118
5119 /* Finally! Run the function... */
5120 *op = expand_builtin_function (*op, nargs, argv, entry_p);
5121
5122 /* Free memory. */
5123 if (entry_p->expand_args)
5124 for (argvp=argv; *argvp != 0; ++argvp)
5125 free (*argvp);
5126 if (abeg)
5127 free (abeg);
5128
5129 return 1;
5130}
5131
5132
5133int /* bird split it up and hacked it. */
5134#ifndef CONFIG_WITH_VALUE_LENGTH
5135handle_function (char **op, const char **stringp)
5136{
5137 const struct function_table_entry *entry_p = lookup_function (*stringp + 1);
5138 if (!entry_p)
5139 return 0;
5140 return handle_function2 (entry_p, op, stringp);
5141}
5142#else /* CONFIG_WITH_VALUE_LENGTH */
5143handle_function (char **op, const char **stringp, const char *nameend, const char *eol UNUSED)
5144{
5145 const char *fname = *stringp + 1;
5146 const struct function_table_entry *entry_p =
5147 lookup_function_in_hash_tab (fname, nameend - fname);
5148 if (!entry_p)
5149 return 0;
5150 return handle_function2 (entry_p, op, stringp);
5151}
5152#endif /* CONFIG_WITH_VALUE_LENGTH */
5153
5154
5155
5156/* User-defined functions. Expand the first argument as either a builtin
5157 function or a make variable, in the context of the rest of the arguments
5158 assigned to $1, $2, ... $N. $0 is the name of the function. */
5159
5160static char *
5161func_call (char *o, char **argv, const char *funcname UNUSED)
5162{
5163 static int max_args = 0;
5164 char *fname;
5165 char *cp;
5166 char *body;
5167 int flen;
5168 int i;
5169 int saved_args;
5170 const struct function_table_entry *entry_p;
5171 struct variable *v;
5172#ifdef CONFIG_WITH_EVALPLUS
5173 char *buf;
5174 unsigned int len;
5175#endif
5176
5177 /* There is no way to define a variable with a space in the name, so strip
5178 leading and trailing whitespace as a favor to the user. */
5179 fname = argv[0];
5180 while (*fname != '\0' && isspace ((unsigned char)*fname))
5181 ++fname;
5182
5183 cp = fname + strlen (fname) - 1;
5184 while (cp > fname && isspace ((unsigned char)*cp))
5185 --cp;
5186 cp[1] = '\0';
5187
5188 /* Calling nothing is a no-op */
5189 if (*fname == '\0')
5190 return o;
5191
5192 /* Are we invoking a builtin function? */
5193
5194#ifndef CONFIG_WITH_VALUE_LENGTH
5195 entry_p = lookup_function (fname);
5196#else
5197 entry_p = lookup_function (fname, cp - fname + 1);
5198#endif
5199 if (entry_p)
5200 {
5201 /* How many arguments do we have? */
5202 for (i=0; argv[i+1]; ++i)
5203 ;
5204 return expand_builtin_function (o, i, argv+1, entry_p);
5205 }
5206
5207 /* Not a builtin, so the first argument is the name of a variable to be
5208 expanded and interpreted as a function. Find it. */
5209 flen = strlen (fname);
5210
5211 v = lookup_variable (fname, flen);
5212
5213 if (v == 0)
5214 warn_undefined (fname, flen);
5215
5216 if (v == 0 || *v->value == '\0')
5217 return o;
5218
5219 body = alloca (flen + 4);
5220 body[0] = '$';
5221 body[1] = '(';
5222 memcpy (body + 2, fname, flen);
5223 body[flen+2] = ')';
5224 body[flen+3] = '\0';
5225
5226 /* Set up arguments $(1) .. $(N). $(0) is the function name. */
5227
5228 push_new_variable_scope ();
5229
5230 for (i=0; *argv; ++i, ++argv)
5231 {
5232 char num[11];
5233
5234 sprintf (num, "%d", i);
5235 define_variable (num, strlen (num), *argv, o_automatic, 0);
5236 }
5237
5238 /* If the number of arguments we have is < max_args, it means we're inside
5239 a recursive invocation of $(call ...). Fill in the remaining arguments
5240 in the new scope with the empty value, to hide them from this
5241 invocation. */
5242
5243 for (; i < max_args; ++i)
5244 {
5245 char num[11];
5246
5247#ifndef CONFIG_WITH_VALUE_LENGTH
5248 sprintf (num, "%d", i);
5249 define_variable (num, strlen (num), "", o_automatic, 0);
5250#else
5251 define_variable (num, sprintf (num, "%d", i), "", o_automatic, 0);
5252#endif
5253 }
5254
5255 saved_args = max_args;
5256 max_args = i;
5257
5258#ifdef CONFIG_WITH_EVALPLUS
5259 if (!strcmp (funcname, "call"))
5260 {
5261#endif
5262 /* Expand the body in the context of the arguments, adding the result to
5263 the variable buffer. */
5264
5265 v->exp_count = EXP_COUNT_MAX;
5266#ifndef CONFIG_WITH_VALUE_LENGTH
5267 o = variable_expand_string (o, body, flen+3);
5268 v->exp_count = 0;
5269
5270 o += strlen (o);
5271#else /* CONFIG_WITH_VALUE_LENGTH */
5272 variable_expand_string_2 (o, body, flen+3, &o);
5273 v->exp_count = 0;
5274#endif /* CONFIG_WITH_VALUE_LENGTH */
5275#ifdef CONFIG_WITH_EVALPLUS
5276 }
5277 else
5278 {
5279 const struct floc *reading_file_saved = reading_file;
5280 char *eos;
5281
5282 if (!strcmp (funcname, "evalcall"))
5283 {
5284 /* Evaluate the variable value without expanding it. We
5285 need a copy since eval_buffer is destructive. */
5286
5287 size_t off = o - variable_buffer;
5288 eos = variable_buffer_output (o, v->value, v->value_length + 1) - 1;
5289 o = variable_buffer + off;
5290 if (v->fileinfo.filenm)
5291 reading_file = &v->fileinfo;
5292 }
5293 else
5294 {
5295 /* Expand the body first and then evaluate the output. */
5296
5297 v->exp_count = EXP_COUNT_MAX;
5298 o = variable_expand_string_2 (o, body, flen+3, &eos);
5299 v->exp_count = 0;
5300 }
5301
5302 install_variable_buffer (&buf, &len);
5303 eval_buffer (o, eos);
5304 restore_variable_buffer (buf, len);
5305 reading_file = reading_file_saved;
5306 }
5307#endif /* CONFIG_WITH_EVALPLUS */
5308
5309 max_args = saved_args;
5310
5311 pop_variable_scope ();
5312
5313 return o;
5314}
5315
5316void
5317hash_init_function_table (void)
5318{
5319 hash_init (&function_table, FUNCTION_TABLE_ENTRIES * 2,
5320 function_table_entry_hash_1, function_table_entry_hash_2,
5321 function_table_entry_hash_cmp);
5322 hash_load (&function_table, function_table_init,
5323 FUNCTION_TABLE_ENTRIES, sizeof (struct function_table_entry));
5324#if defined (CONFIG_WITH_OPTIMIZATION_HACKS) || defined (CONFIG_WITH_VALUE_LENGTH)
5325 {
5326 unsigned int i;
5327 for (i = 0; i < FUNCTION_TABLE_ENTRIES; i++)
5328 {
5329 const char *fn = function_table_init[i].name;
5330 while (*fn)
5331 {
5332 func_char_map[(int)*fn] = 1;
5333 fn++;
5334 }
5335 assert (function_table_init[i].len <= MAX_FUNCTION_LENGTH);
5336 assert (function_table_init[i].len >= MIN_FUNCTION_LENGTH);
5337 }
5338 }
5339#endif
5340}
Note: See TracBrowser for help on using the repository browser.