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

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

kmk: Added $(length ), $(length-var ), $(insert ), $(pos ), $(lastpos ), $(substr ) and $(translate ). (all untested)

  • Property svn:eol-style set to native
File size: 142.0 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
2409 /* do the searching */
2410 if (funcname[0] != 'l')
2411 { /* pos */
2412 if (needle_len == 1)
2413 hit = strchr (haystack + start, *needle);
2414 else
2415 hit = strstr (haystack + start, needle);
2416 }
2417 else
2418 { /* last pos */
2419 int ch = *needle;
2420 size_t off = start + 1;
2421
2422 hit = NULL;
2423 while (off-- > 0)
2424 {
2425 if ( haystack[off] == ch
2426 && ( needle_len == 1
2427 || strncmp (&haystack[off], needle, needle_len) == 0))
2428 {
2429 hit = haystack + off;
2430 break;
2431 }
2432 }
2433 }
2434
2435 return math_int_to_variable_buffer (o, hit ? hit - haystack + 1 : 0);
2436}
2437
2438
2439/*
2440 $(substr str, start[, length[, pad]])
2441
2442 XXX: This doesn't take multibyte locales into account.
2443 */
2444static char *
2445func_substr (char *o, char **argv, const char *funcname UNUSED)
2446{
2447 const char *str = argv[0];
2448 size_t str_len = strlen (str);
2449 math_int start = math_int_from_string (argv[1]);
2450 math_int length = 0;
2451 const char *pad = NULL;
2452 size_t pad_len = 0;
2453
2454 if (argv[2] != NULL)
2455 {
2456 if (argv[3] != NULL)
2457 {
2458 pad = argv[3];
2459 for (pad_len = 0; pad[pad_len] == ' '; pad_len++)
2460 /* nothing */;
2461 if (pad[pad_len] != '\0')
2462 pad_len = strlen (pad);
2463 else
2464 {
2465 pad = " ";
2466 pad_len = 16;
2467 }
2468 }
2469 length = math_int_from_string (argv[1]);
2470 if (length < 0 || (pad != NULL && length > 16*1024*1024 /* 16MB */))
2471 fatal (NILF, _("$(substr ): length=%s is out of bounds\n"), argv[3]);
2472 if (length == 0)
2473 return o;
2474 }
2475
2476 /* adjust start and length. */
2477 if (pad == NULL)
2478 {
2479 if (start > str_len)
2480 {
2481 start--; /* one-origin */
2482 if (start >= str_len)
2483 return o;
2484 if (length == 0 || start + length > str_len)
2485 length = str_len - start;
2486 }
2487 else
2488 {
2489 start = str_len + start;
2490 if (start <= 0)
2491 {
2492 start += length;
2493 if (start <= 0)
2494 return o;
2495 length = start;
2496 start = 0;
2497 }
2498 else if (length == 0)
2499 length = str_len - start;
2500 }
2501
2502 o = variable_buffer_output (o, str + start, length);
2503 }
2504 else
2505 {
2506 if (start > str_len)
2507 {
2508 start--; /* one-origin */
2509 if (start >= str_len)
2510 return length ? helper_insert_pad (o, length, pad, pad_len) : o;
2511 if (length == 0)
2512 length = str_len - start;
2513 }
2514 else
2515 {
2516 start = str_len + start;
2517 if (start <= 0)
2518 {
2519 if (start + length <= 0)
2520 return o;
2521 o = helper_insert_pad (o, -start, pad, pad_len);
2522 return variable_buffer_output (o, str, length + length);
2523 }
2524 if (length == 0)
2525 length = str_len - start;
2526 }
2527 if (start + length <= str_len)
2528 o = variable_buffer_output (o, str + start, length);
2529 else
2530 {
2531 o = variable_buffer_output (o, str + start, str_len - start);
2532 o = helper_insert_pad (o, start + length - start, pad, pad_len);
2533 }
2534 }
2535
2536 return o;
2537}
2538
2539
2540/*
2541 $(translate string, new-set[, old-set[, pad-char]])
2542
2543 XXX: This doesn't take multibyte locales into account.
2544 */
2545static char *
2546func_translate (char *o, char **argv, const char *funcname UNUSED)
2547{
2548 const unsigned char *str = argv[0];
2549 const unsigned char *new_set = argv[1];
2550 const char *old_set = argv[2];
2551 char trans_tab[1 << CHAR_BIT];
2552 int i;
2553 char ch;
2554
2555 /* init the array. */
2556 for (i = 0; i < (1 << CHAR_BIT); i++)
2557 trans_tab[i] = i;
2558
2559 while ( (i = *old_set) != '\0'
2560 && (ch = *new_set) != '\0')
2561 {
2562 trans_tab[i] = ch;
2563 old_set++;
2564 new_set++;
2565 }
2566
2567 if (i != '\0')
2568 {
2569 ch = '\0'; /* no padding == remove char */
2570 if (argv[2] != NULL && argv[3] != NULL)
2571 {
2572 ch = argv[3][0];
2573 if (ch && argv[3][1])
2574 fatal (NILF, _("$(translate ): pad=`%s' expected a single char\n"), argv[3]);
2575 if (ch == '\0') /* no char == space */
2576 ch = ' ';
2577 }
2578 while ((i = *old_set++) != '\0')
2579 trans_tab[i] = ch;
2580 }
2581
2582 /* do the translation */
2583 while ((i = *str++) != '\0')
2584 {
2585 ch = trans_tab[i];
2586 if (ch)
2587 o = variable_buffer_output (o, &ch, 1);
2588 }
2589
2590 return o;
2591}
2592#endif /* CONFIG_WITH_STRING_FUNCTIONS */
2593
2594
2595#ifdef CONFIG_WITH_LAZY_DEPS_VARS
2596
2597/* This is also in file.c (bad). */
2598# if VMS
2599# define FILE_LIST_SEPARATOR ','
2600# else
2601# define FILE_LIST_SEPARATOR ' '
2602# endif
2603
2604/* Implements $^ and $+.
2605
2606 The first is somes with with FUNCNAME 'deps', the second as 'deps-all'.
2607
2608 If no second argument is given, or if it's empty, or if it's zero,
2609 all dependencies will be returned. If the second argument is non-zero
2610 the dependency at that position will be returned. If the argument is
2611 negative a fatal error is thrown. */
2612static char *
2613func_deps (char *o, char **argv, const char *funcname)
2614{
2615 unsigned int idx = 0;
2616 struct file *file;
2617
2618 /* Handle the argument if present. */
2619
2620 if (argv[1])
2621 {
2622 char *p = argv[1];
2623 while (isspace ((unsigned int)*p))
2624 p++;
2625 if (*p != '\0')
2626 {
2627 char *n;
2628 long l = strtol (p, &n, 0);
2629 while (isspace ((unsigned int)*n))
2630 n++;
2631 idx = l;
2632 if (*n != '\0' || l < 0 || (long)idx != l)
2633 fatal (NILF, _("%s: invalid index value: `%s'\n"), funcname, p);
2634 }
2635 }
2636
2637 /* Find the file and select the list corresponding to FUNCNAME. */
2638
2639 file = lookup_file (argv[0]);
2640 if (file)
2641 {
2642 struct dep *deps = funcname[4] != '\0' && file->org_deps
2643 ? file->org_deps : file->deps;
2644 struct dep *d;
2645
2646 if ( file->double_colon
2647 && ( file->double_colon != file
2648 || file->last != file))
2649 error (NILF, _("$(%s ) cannot be used on files with multiple double colon rules like `%s'\n"),
2650 funcname, file->name);
2651
2652 if (idx == 0 /* all */)
2653 {
2654 unsigned int total_len = 0;
2655
2656 /* calc the result length. */
2657
2658 for (d = deps; d; d = d->next)
2659 if (!d->ignore_mtime)
2660 {
2661 const char *c = dep_name (d);
2662
2663#ifndef NO_ARCHIVES
2664 if (ar_name (c))
2665 {
2666 c = strchr (c, '(') + 1;
2667 total_len += strlen (c);
2668 }
2669 else
2670#elif defined (CONFIG_WITH_STRCACHE2)
2671 total_len += strcache2_get_len (&file_strcache, c) + 1;
2672#else
2673 total_len += strlen (c) + 1;
2674#endif
2675 }
2676
2677 if (total_len)
2678 {
2679 /* prepare the variable buffer dude wrt to the output size and
2680 pass along the strings. */
2681
2682 o = variable_buffer_output (o + total_len, "", 0) - total_len; /* a hack */
2683
2684 for (d = deps; d; d = d->next)
2685 if (!d->ignore_mtime)
2686 {
2687 unsigned int len;
2688 const char *c = dep_name (d);
2689
2690#ifndef NO_ARCHIVES
2691 if (ar_name (c))
2692 {
2693 c = strchr (c, '(') + 1;
2694 len = strlen (c);
2695 }
2696 else
2697#elif defined (CONFIG_WITH_STRCACHE2)
2698 len = strcache2_get_len (&file_strcache, c) + 1;
2699#else
2700 len = strlen (c) + 1;
2701#endif
2702 o = variable_buffer_output (o, c, len);
2703 o[-1] = FILE_LIST_SEPARATOR;
2704 }
2705
2706 --o; /* nuke the last list separator */
2707 *o = '\0';
2708 }
2709 }
2710 else
2711 {
2712 /* Dependency given by index. */
2713
2714 for (d = deps; d; d = d->next)
2715 if (!d->ignore_mtime)
2716 {
2717 if (--idx == 0) /* 1 based indexing */
2718 {
2719 unsigned int len;
2720 const char *c = dep_name (d);
2721
2722#ifndef NO_ARCHIVES
2723 if (ar_name (c))
2724 {
2725 c = strchr (c, '(') + 1;
2726 len = strlen (c) - 1;
2727 }
2728 else
2729#elif defined (CONFIG_WITH_STRCACHE2)
2730 len = strcache2_get_len (&file_strcache, c);
2731#else
2732 len = strlen (c);
2733#endif
2734 o = variable_buffer_output (o, c, len);
2735 break;
2736 }
2737 }
2738 }
2739 }
2740
2741 return o;
2742}
2743
2744/* Implements $?.
2745
2746 If no second argument is given, or if it's empty, or if it's zero,
2747 all dependencies will be returned. If the second argument is non-zero
2748 the dependency at that position will be returned. If the argument is
2749 negative a fatal error is thrown. */
2750static char *
2751func_deps_newer (char *o, char **argv, const char *funcname)
2752{
2753 unsigned int idx = 0;
2754 struct file *file;
2755
2756 /* Handle the argument if present. */
2757
2758 if (argv[1])
2759 {
2760 char *p = argv[1];
2761 while (isspace ((unsigned int)*p))
2762 p++;
2763 if (*p != '\0')
2764 {
2765 char *n;
2766 long l = strtol (p, &n, 0);
2767 while (isspace ((unsigned int)*n))
2768 n++;
2769 idx = l;
2770 if (*n != '\0' || l < 0 || (long)idx != l)
2771 fatal (NILF, _("%s: invalid index value: `%s'\n"), funcname, p);
2772 }
2773 }
2774
2775 /* Find the file. */
2776
2777 file = lookup_file (argv[0]);
2778 if (file)
2779 {
2780 struct dep *deps = file->deps;
2781 struct dep *d;
2782
2783 if ( file->double_colon
2784 && ( file->double_colon != file
2785 || file->last != file))
2786 error (NILF, _("$(%s ) cannot be used on files with multiple double colon rules like `%s'\n"),
2787 funcname, file->name);
2788
2789 if (idx == 0 /* all */)
2790 {
2791 unsigned int total_len = 0;
2792
2793 /* calc the result length. */
2794
2795 for (d = deps; d; d = d->next)
2796 if (!d->ignore_mtime && d->changed)
2797 {
2798 const char *c = dep_name (d);
2799
2800#ifndef NO_ARCHIVES
2801 if (ar_name (c))
2802 {
2803 c = strchr (c, '(') + 1;
2804 total_len += strlen (c);
2805 }
2806 else
2807#elif defined (CONFIG_WITH_STRCACHE2)
2808 total_len += strcache2_get_len (&file_strcache, c) + 1;
2809#else
2810 total_len += strlen (c) + 1;
2811#endif
2812 }
2813
2814 if (total_len)
2815 {
2816 /* prepare the variable buffer dude wrt to the output size and
2817 pass along the strings. */
2818
2819 o = variable_buffer_output (o + total_len, "", 0) - total_len; /* a hack */
2820
2821 for (d = deps; d; d = d->next)
2822 if (!d->ignore_mtime && d->changed)
2823 {
2824 unsigned int len;
2825 const char *c = dep_name (d);
2826
2827#ifndef NO_ARCHIVES
2828 if (ar_name (c))
2829 {
2830 c = strchr (c, '(') + 1;
2831 len = strlen (c);
2832 }
2833 else
2834#elif defined (CONFIG_WITH_STRCACHE2)
2835 len = strcache2_get_len (&file_strcache, c) + 1;
2836#else
2837 len = strlen (c) + 1;
2838#endif
2839 o = variable_buffer_output (o, c, len);
2840 o[-1] = FILE_LIST_SEPARATOR;
2841 }
2842
2843 --o; /* nuke the last list separator */
2844 *o = '\0';
2845 }
2846 }
2847 else
2848 {
2849 /* Dependency given by index. */
2850
2851 for (d = deps; d; d = d->next)
2852 if (!d->ignore_mtime && d->changed)
2853 {
2854 if (--idx == 0) /* 1 based indexing */
2855 {
2856 unsigned int len;
2857 const char *c = dep_name (d);
2858
2859#ifndef NO_ARCHIVES
2860 if (ar_name (c))
2861 {
2862 c = strchr (c, '(') + 1;
2863 len = strlen (c) - 1;
2864 }
2865 else
2866#elif defined (CONFIG_WITH_STRCACHE2)
2867 len = strcache2_get_len (&file_strcache, c);
2868#else
2869 len = strlen (c);
2870#endif
2871 o = variable_buffer_output (o, c, len);
2872 break;
2873 }
2874 }
2875 }
2876 }
2877
2878 return o;
2879}
2880
2881/* Implements $|, the order only dependency list.
2882
2883 If no second argument is given, or if it's empty, or if it's zero,
2884 all dependencies will be returned. If the second argument is non-zero
2885 the dependency at that position will be returned. If the argument is
2886 negative a fatal error is thrown. */
2887static char *
2888func_deps_order_only (char *o, char **argv, const char *funcname)
2889{
2890 unsigned int idx = 0;
2891 struct file *file;
2892
2893 /* Handle the argument if present. */
2894
2895 if (argv[1])
2896 {
2897 char *p = argv[1];
2898 while (isspace ((unsigned int)*p))
2899 p++;
2900 if (*p != '\0')
2901 {
2902 char *n;
2903 long l = strtol (p, &n, 0);
2904 while (isspace ((unsigned int)*n))
2905 n++;
2906 idx = l;
2907 if (*n != '\0' || l < 0 || (long)idx != l)
2908 fatal (NILF, _("%s: invalid index value: `%s'\n"), funcname, p);
2909 }
2910 }
2911
2912 /* Find the file. */
2913
2914 file = lookup_file (argv[0]);
2915 if (file)
2916 {
2917 struct dep *deps = file->deps;
2918 struct dep *d;
2919
2920 if ( file->double_colon
2921 && ( file->double_colon != file
2922 || file->last != file))
2923 error (NILF, _("$(%s ) cannot be used on files with multiple double colon rules like `%s'\n"),
2924 funcname, file->name);
2925
2926 if (idx == 0 /* all */)
2927 {
2928 unsigned int total_len = 0;
2929
2930 /* calc the result length. */
2931
2932 for (d = deps; d; d = d->next)
2933 if (d->ignore_mtime)
2934 {
2935 const char *c = dep_name (d);
2936
2937#ifndef NO_ARCHIVES
2938 if (ar_name (c))
2939 {
2940 c = strchr (c, '(') + 1;
2941 total_len += strlen (c);
2942 }
2943 else
2944#elif defined (CONFIG_WITH_STRCACHE2)
2945 total_len += strcache2_get_len (&file_strcache, c) + 1;
2946#else
2947 total_len += strlen (c) + 1;
2948#endif
2949 }
2950
2951 if (total_len)
2952 {
2953 /* prepare the variable buffer dude wrt to the output size and
2954 pass along the strings. */
2955
2956 o = variable_buffer_output (o + total_len, "", 0) - total_len; /* a hack */
2957
2958 for (d = deps; d; d = d->next)
2959 if (d->ignore_mtime)
2960 {
2961 unsigned int len;
2962 const char *c = dep_name (d);
2963
2964#ifndef NO_ARCHIVES
2965 if (ar_name (c))
2966 {
2967 c = strchr (c, '(') + 1;
2968 len = strlen (c);
2969 }
2970 else
2971#elif defined (CONFIG_WITH_STRCACHE2)
2972 len = strcache2_get_len (&file_strcache, c) + 1;
2973#else
2974 len = strlen (c) + 1;
2975#endif
2976 o = variable_buffer_output (o, c, len);
2977 o[-1] = FILE_LIST_SEPARATOR;
2978 }
2979
2980 --o; /* nuke the last list separator */
2981 *o = '\0';
2982 }
2983 }
2984 else
2985 {
2986 /* Dependency given by index. */
2987
2988 for (d = deps; d; d = d->next)
2989 if (d->ignore_mtime)
2990 {
2991 if (--idx == 0) /* 1 based indexing */
2992 {
2993 unsigned int len;
2994 const char *c = dep_name (d);
2995
2996#ifndef NO_ARCHIVES
2997 if (ar_name (c))
2998 {
2999 c = strchr (c, '(') + 1;
3000 len = strlen (c) - 1;
3001 }
3002 else
3003#elif defined (CONFIG_WITH_STRCACHE2)
3004 len = strcache2_get_len (&file_strcache, c);
3005#else
3006 len = strlen (c);
3007#endif
3008 o = variable_buffer_output (o, c, len);
3009 break;
3010 }
3011 }
3012 }
3013 }
3014
3015 return o;
3016}
3017#endif /* CONFIG_WITH_LAZY_DEPS_VARS */
3018
3019
3020
3021#ifdef CONFIG_WITH_DEFINED
3022/* Similar to ifdef. */
3023static char *
3024func_defined (char *o, char **argv, const char *funcname UNUSED)
3025{
3026 struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
3027 int result = v != NULL && *v->value != '\0';
3028 o = variable_buffer_output (o, result ? "1" : "", result);
3029 return o;
3030}
3031#endif /* CONFIG_WITH_DEFINED*/
3032
3033
3034
3035/* Return the absolute name of file NAME which does not contain any `.',
3036 `..' components nor any repeated path separators ('/'). */
3037#ifdef KMK
3038char *
3039#else
3040static char *
3041#endif
3042abspath (const char *name, char *apath)
3043{
3044 char *dest;
3045 const char *start, *end, *apath_limit;
3046
3047 if (name[0] == '\0' || apath == NULL)
3048 return NULL;
3049
3050#ifdef WINDOWS32 /* bird */
3051 dest = w32ify((char *)name, 1);
3052 if (!dest)
3053 return NULL;
3054 {
3055 size_t len = strlen(dest);
3056 memcpy(apath, dest, len);
3057 dest = apath + len;
3058 }
3059
3060 (void)end; (void)start; (void)apath_limit;
3061
3062#elif defined __OS2__ /* bird */
3063 if (_abspath(apath, name, GET_PATH_MAX))
3064 return NULL;
3065 dest = strchr(apath, '\0');
3066
3067 (void)end; (void)start; (void)apath_limit; (void)dest;
3068
3069#else /* !WINDOWS32 && !__OS2__ */
3070 apath_limit = apath + GET_PATH_MAX;
3071
3072#ifdef HAVE_DOS_PATHS /* bird added this */
3073 if (isalpha(name[0]) && name[1] == ':')
3074 {
3075 /* drive spec */
3076 apath[0] = toupper(name[0]);
3077 apath[1] = ':';
3078 apath[2] = '/';
3079 name += 2;
3080 }
3081 else
3082#endif /* HAVE_DOS_PATHS */
3083 if (name[0] != '/')
3084 {
3085 /* It is unlikely we would make it until here but just to make sure. */
3086 if (!starting_directory)
3087 return NULL;
3088
3089 strcpy (apath, starting_directory);
3090
3091 dest = strchr (apath, '\0');
3092 }
3093 else
3094 {
3095 apath[0] = '/';
3096 dest = apath + 1;
3097 }
3098
3099 for (start = end = name; *start != '\0'; start = end)
3100 {
3101 unsigned long len;
3102
3103 /* Skip sequence of multiple path-separators. */
3104 while (*start == '/')
3105 ++start;
3106
3107 /* Find end of path component. */
3108 for (end = start; *end != '\0' && *end != '/'; ++end)
3109 ;
3110
3111 len = end - start;
3112
3113 if (len == 0)
3114 break;
3115 else if (len == 1 && start[0] == '.')
3116 /* nothing */;
3117 else if (len == 2 && start[0] == '.' && start[1] == '.')
3118 {
3119 /* Back up to previous component, ignore if at root already. */
3120 if (dest > apath + 1)
3121 while ((--dest)[-1] != '/');
3122 }
3123 else
3124 {
3125 if (dest[-1] != '/')
3126 *dest++ = '/';
3127
3128 if (dest + len >= apath_limit)
3129 return NULL;
3130
3131 dest = memcpy (dest, start, len);
3132 dest += len;
3133 *dest = '\0';
3134 }
3135 }
3136#endif /* !WINDOWS32 && !__OS2__ */
3137
3138 /* Unless it is root strip trailing separator. */
3139#ifdef HAVE_DOS_PATHS /* bird (is this correct? what about UNC?) */
3140 if (dest > apath + 1 + (apath[0] != '/') && dest[-1] == '/')
3141#else
3142 if (dest > apath + 1 && dest[-1] == '/')
3143#endif
3144 --dest;
3145
3146 *dest = '\0';
3147
3148 return apath;
3149}
3150
3151
3152static char *
3153func_realpath (char *o, char **argv, const char *funcname UNUSED)
3154{
3155 /* Expand the argument. */
3156 const char *p = argv[0];
3157 const char *path = 0;
3158 int doneany = 0;
3159 unsigned int len = 0;
3160 PATH_VAR (in);
3161 PATH_VAR (out);
3162
3163 while ((path = find_next_token (&p, &len)) != 0)
3164 {
3165 if (len < GET_PATH_MAX)
3166 {
3167 strncpy (in, path, len);
3168 in[len] = '\0';
3169
3170 if (
3171#ifdef HAVE_REALPATH
3172 realpath (in, out)
3173#else
3174 abspath (in, out)
3175#endif
3176 )
3177 {
3178 o = variable_buffer_output (o, out, strlen (out));
3179 o = variable_buffer_output (o, " ", 1);
3180 doneany = 1;
3181 }
3182 }
3183 }
3184
3185 /* Kill last space. */
3186 if (doneany)
3187 --o;
3188
3189 return o;
3190}
3191
3192static char *
3193func_abspath (char *o, char **argv, const char *funcname UNUSED)
3194{
3195 /* Expand the argument. */
3196 const char *p = argv[0];
3197 const char *path = 0;
3198 int doneany = 0;
3199 unsigned int len = 0;
3200 PATH_VAR (in);
3201 PATH_VAR (out);
3202
3203 while ((path = find_next_token (&p, &len)) != 0)
3204 {
3205 if (len < GET_PATH_MAX)
3206 {
3207 strncpy (in, path, len);
3208 in[len] = '\0';
3209
3210 if (abspath (in, out))
3211 {
3212 o = variable_buffer_output (o, out, strlen (out));
3213 o = variable_buffer_output (o, " ", 1);
3214 doneany = 1;
3215 }
3216 }
3217 }
3218
3219 /* Kill last space. */
3220 if (doneany)
3221 --o;
3222
3223 return o;
3224}
3225
3226#ifdef CONFIG_WITH_ABSPATHEX
3227/* Same as abspath except that the current path may be given as the
3228 2nd argument. */
3229static char *
3230func_abspathex (char *o, char **argv, const char *funcname UNUSED)
3231{
3232 char *cwd = argv[1];
3233
3234 /* cwd needs leading spaces chopped and may be optional,
3235 in which case we're exactly like $(abspath ). */
3236 while (isblank(*cwd))
3237 cwd++;
3238 if (!*cwd)
3239 o = func_abspath (o, argv, funcname);
3240 else
3241 {
3242 /* Expand the argument. */
3243 const char *p = argv[0];
3244 unsigned int cwd_len = ~0U;
3245 char *path = 0;
3246 int doneany = 0;
3247 unsigned int len = 0;
3248 PATH_VAR (in);
3249 PATH_VAR (out);
3250
3251 while ((path = find_next_token (&p, &len)) != 0)
3252 {
3253 if (len < GET_PATH_MAX)
3254 {
3255#ifdef HAVE_DOS_PATHS
3256 if (path[0] != '/' && path[0] != '\\' && (len < 2 || path[1] != ':') && cwd)
3257#else
3258 if (path[0] != '/' && cwd)
3259#endif
3260 {
3261 /* relative path, prefix with cwd. */
3262 if (cwd_len == ~0U)
3263 cwd_len = strlen (cwd);
3264 if (cwd_len + len + 1 >= GET_PATH_MAX)
3265 continue;
3266 memcpy (in, cwd, cwd_len);
3267 in[cwd_len] = '/';
3268 memcpy (in + cwd_len + 1, path, len);
3269 in[cwd_len + len + 1] = '\0';
3270 }
3271 else
3272 {
3273 /* absolute path pass it as-is. */
3274 memcpy (in, path, len);
3275 in[len] = '\0';
3276 }
3277
3278 if (abspath (in, out))
3279 {
3280 o = variable_buffer_output (o, out, strlen (out));
3281 o = variable_buffer_output (o, " ", 1);
3282 doneany = 1;
3283 }
3284 }
3285 }
3286
3287 /* Kill last space. */
3288 if (doneany)
3289 --o;
3290 }
3291
3292 return o;
3293}
3294#endif
3295
3296#ifdef CONFIG_WITH_XARGS
3297/* Create one or more command lines avoiding the max argument
3298 length restriction of the host OS.
3299
3300 The last argument is the list of arguments that the normal
3301 xargs command would be fed from stdin.
3302
3303 The first argument is initial command and it's arguments.
3304
3305 If there are three or more arguments, the 2nd argument is
3306 the command and arguments to be used on subsequent
3307 command lines. Defaults to the initial command.
3308
3309 If there are four or more arguments, the 3rd argument is
3310 the command to be used at the final command line. Defaults
3311 to the sub sequent or initial command .
3312
3313 A future version of this function may define more arguments
3314 and therefor anyone specifying six or more arguments will
3315 cause fatal errors.
3316
3317 Typical usage is:
3318 $(xargs ar cas mylib.a,$(objects))
3319 or
3320 $(xargs ar cas mylib.a,ar as mylib.a,$(objects))
3321
3322 It will then create one or more "ar mylib.a ..." command
3323 lines with proper \n\t separation so it can be used when
3324 writing rules. */
3325static char *
3326func_xargs (char *o, char **argv, const char *funcname UNUSED)
3327{
3328 int argc;
3329 const char *initial_cmd;
3330 size_t initial_cmd_len;
3331 const char *subsequent_cmd;
3332 size_t subsequent_cmd_len;
3333 const char *final_cmd;
3334 size_t final_cmd_len;
3335 const char *args;
3336 size_t max_args;
3337 int i;
3338
3339#ifdef ARG_MAX
3340 /* ARG_MAX is a bit unreliable (environment), so drop 25% of the max. */
3341# define XARGS_MAX (ARG_MAX - (ARG_MAX / 4))
3342#else /* FIXME: update configure with a command line length test. */
3343# define XARGS_MAX 10240
3344#endif
3345
3346 argc = 0;
3347 while (argv[argc])
3348 argc++;
3349 if (argc > 4)
3350 fatal (NILF, _("Too many arguments for $(xargs)!\n"));
3351
3352 /* first: the initial / default command.*/
3353 initial_cmd = argv[0];
3354 while (isspace ((unsigned char)*initial_cmd))
3355 initial_cmd++;
3356 max_args = initial_cmd_len = strlen (initial_cmd);
3357
3358 /* second: the command for the subsequent command lines. defaults to the initial cmd. */
3359 subsequent_cmd = argc > 2 && argv[1][0] != '\0' ? argv[1] : "";
3360 while (isspace ((unsigned char)*subsequent_cmd))
3361 subsequent_cmd++;
3362 if (*subsequent_cmd)
3363 {
3364 subsequent_cmd_len = strlen (subsequent_cmd);
3365 if (subsequent_cmd_len > max_args)
3366 max_args = subsequent_cmd_len;
3367 }
3368 else
3369 {
3370 subsequent_cmd = initial_cmd;
3371 subsequent_cmd_len = initial_cmd_len;
3372 }
3373
3374 /* third: the final command. defaults to the subseq cmd. */
3375 final_cmd = argc > 3 && argv[2][0] != '\0' ? argv[2] : "";
3376 while (isspace ((unsigned char)*final_cmd))
3377 final_cmd++;
3378 if (*final_cmd)
3379 {
3380 final_cmd_len = strlen (final_cmd);
3381 if (final_cmd_len > max_args)
3382 max_args = final_cmd_len;
3383 }
3384 else
3385 {
3386 final_cmd = subsequent_cmd;
3387 final_cmd_len = subsequent_cmd_len;
3388 }
3389
3390 /* last: the arguments to split up into sensible portions. */
3391 args = argv[argc - 1];
3392
3393 /* calc the max argument length. */
3394 if (XARGS_MAX <= max_args + 2)
3395 fatal (NILF, _("$(xargs): the commands are longer than the max exec argument length. (%lu <= %lu)\n"),
3396 (unsigned long)XARGS_MAX, (unsigned long)max_args + 2);
3397 max_args = XARGS_MAX - max_args - 1;
3398
3399 /* generate the commands. */
3400 i = 0;
3401 for (i = 0; ; i++)
3402 {
3403 unsigned int len;
3404 const char *iterator = args;
3405 const char *end = args;
3406 const char *cur;
3407 const char *tmp;
3408
3409 /* scan the arguments till we reach the end or the max length. */
3410 while ((cur = find_next_token(&iterator, &len))
3411 && (size_t)((cur + len) - args) < max_args)
3412 end = cur + len;
3413 if (cur && end == args)
3414 fatal (NILF, _("$(xargs): command + one single arg is too much. giving up.\n"));
3415
3416 /* emit the command. */
3417 if (i == 0)
3418 {
3419 o = variable_buffer_output (o, (char *)initial_cmd, initial_cmd_len);
3420 o = variable_buffer_output (o, " ", 1);
3421 }
3422 else if (cur)
3423 {
3424 o = variable_buffer_output (o, "\n\t", 2);
3425 o = variable_buffer_output (o, (char *)subsequent_cmd, subsequent_cmd_len);
3426 o = variable_buffer_output (o, " ", 1);
3427 }
3428 else
3429 {
3430 o = variable_buffer_output (o, "\n\t", 2);
3431 o = variable_buffer_output (o, (char *)final_cmd, final_cmd_len);
3432 o = variable_buffer_output (o, " ", 1);
3433 }
3434
3435 tmp = end;
3436 while (tmp > args && isspace ((unsigned char)tmp[-1])) /* drop trailing spaces. */
3437 tmp--;
3438 o = variable_buffer_output (o, (char *)args, tmp - args);
3439
3440
3441 /* next */
3442 if (!cur)
3443 break;
3444 args = end;
3445 while (isspace ((unsigned char)*args))
3446 args++;
3447 }
3448
3449 return o;
3450}
3451#endif
3452
3453#ifdef CONFIG_WITH_TOUPPER_TOLOWER
3454static char *
3455func_toupper_tolower (char *o, char **argv, const char *funcname)
3456{
3457 /* Expand the argument. */
3458 const char *p = argv[0];
3459 while (*p)
3460 {
3461 /* convert to temporary buffer */
3462 char tmp[256];
3463 unsigned int i;
3464 if (!strcmp(funcname, "toupper"))
3465 for (i = 0; i < sizeof(tmp) && *p; i++, p++)
3466 tmp[i] = toupper(*p);
3467 else
3468 for (i = 0; i < sizeof(tmp) && *p; i++, p++)
3469 tmp[i] = tolower(*p);
3470 o = variable_buffer_output (o, tmp, i);
3471 }
3472
3473 return o;
3474}
3475#endif /* CONFIG_WITH_TOUPPER_TOLOWER */
3476
3477#if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
3478
3479/* Strip leading spaces and other things off a command. */
3480static const char *
3481comp_cmds_strip_leading (const char *s, const char *e)
3482{
3483 while (s < e)
3484 {
3485 const char ch = *s;
3486 if (!isblank (ch)
3487 && ch != '@'
3488#ifdef CONFIG_WITH_COMMANDS_FUNC
3489 && ch != '%'
3490#endif
3491 && ch != '+'
3492 && ch != '-')
3493 break;
3494 s++;
3495 }
3496 return s;
3497}
3498
3499/* Worker for func_comp_vars() which is called if the comparision failed.
3500 It will do the slow command by command comparision of the commands
3501 when there invoked as comp-cmds. */
3502static char *
3503comp_vars_ne (char *o, const char *s1, const char *e1, const char *s2, const char *e2,
3504 char *ne_retval, const char *funcname)
3505{
3506 /* give up at once if not comp-cmds or comp-cmds-ex. */
3507 if (strcmp (funcname, "comp-cmds") != 0
3508 && strcmp (funcname, "comp-cmds-ex") != 0)
3509 o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
3510 else
3511 {
3512 const char * const s1_start = s1;
3513 int new_cmd = 1;
3514 int diff;
3515 for (;;)
3516 {
3517 /* if it's a new command, strip leading stuff. */
3518 if (new_cmd)
3519 {
3520 s1 = comp_cmds_strip_leading (s1, e1);
3521 s2 = comp_cmds_strip_leading (s2, e2);
3522 new_cmd = 0;
3523 }
3524 if (s1 >= e1 || s2 >= e2)
3525 break;
3526
3527 /*
3528 * Inner compare loop which compares one line.
3529 * FIXME: parse quoting!
3530 */
3531 for (;;)
3532 {
3533 const char ch1 = *s1;
3534 const char ch2 = *s2;
3535 diff = ch1 - ch2;
3536 if (diff)
3537 break;
3538 if (ch1 == '\n')
3539 break;
3540 assert (ch1 != '\r');
3541
3542 /* next */
3543 s1++;
3544 s2++;
3545 if (s1 >= e1 || s2 >= e2)
3546 break;
3547 }
3548
3549 /*
3550 * If we exited because of a difference try to end-of-command
3551 * comparision, e.g. ignore trailing spaces.
3552 */
3553 if (diff)
3554 {
3555 /* strip */
3556 while (s1 < e1 && isblank (*s1))
3557 s1++;
3558 while (s2 < e2 && isblank (*s2))
3559 s2++;
3560 if (s1 >= e1 || s2 >= e2)
3561 break;
3562
3563 /* compare again and check that it's a newline. */
3564 if (*s2 != '\n' || *s1 != '\n')
3565 break;
3566 }
3567 /* Break out if we exited because of EOS. */
3568 else if (s1 >= e1 || s2 >= e2)
3569 break;
3570
3571 /*
3572 * Detect the end of command lines.
3573 */
3574 if (*s1 == '\n')
3575 new_cmd = s1 == s1_start || s1[-1] != '\\';
3576 s1++;
3577 s2++;
3578 }
3579
3580 /*
3581 * Ignore trailing empty lines.
3582 */
3583 if (s1 < e1 || s2 < e2)
3584 {
3585 while (s1 < e1 && (isblank (*s1) || *s1 == '\n'))
3586 if (*s1++ == '\n')
3587 s1 = comp_cmds_strip_leading (s1, e1);
3588 while (s2 < e2 && (isblank (*s2) || *s2 == '\n'))
3589 if (*s2++ == '\n')
3590 s2 = comp_cmds_strip_leading (s2, e2);
3591 }
3592
3593 /* emit the result. */
3594 if (s1 == e1 && s2 == e2)
3595 o = variable_buffer_output (o, "", 1) - 1; /** @todo check why this was necessary back the... */
3596 else
3597 o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
3598 }
3599 return o;
3600}
3601
3602/*
3603 $(comp-vars var1,var2,not-equal-return)
3604 or
3605 $(comp-cmds cmd-var1,cmd-var2,not-equal-return)
3606
3607 Compares the two variables (that's given by name to avoid unnecessary
3608 expanding) and return the string in the third argument if not equal.
3609 If equal, nothing is returned.
3610
3611 comp-vars will to an exact comparision only stripping leading and
3612 trailing spaces.
3613
3614 comp-cmds will compare command by command, ignoring not only leading
3615 and trailing spaces on each line but also leading one leading '@',
3616 '-', '+' and '%'
3617*/
3618static char *
3619func_comp_vars (char *o, char **argv, const char *funcname)
3620{
3621 const char *s1, *e1, *x1, *s2, *e2, *x2;
3622 char *a1 = NULL, *a2 = NULL;
3623 size_t l, l1, l2;
3624 struct variable *var1 = lookup_variable (argv[0], strlen (argv[0]));
3625 struct variable *var2 = lookup_variable (argv[1], strlen (argv[1]));
3626
3627 /* the simple cases */
3628 if (var1 == var2)
3629 return variable_buffer_output (o, "", 0); /* eq */
3630 if (!var1 || !var2)
3631 return variable_buffer_output (o, argv[2], strlen(argv[2]));
3632 if (var1->value == var2->value)
3633 return variable_buffer_output (o, "", 0); /* eq */
3634 if (!var1->recursive && !var2->recursive)
3635 {
3636 if ( var1->value_length == var2->value_length
3637 && !memcmp (var1->value, var2->value, var1->value_length))
3638 return variable_buffer_output (o, "", 0); /* eq */
3639
3640 /* ignore trailing and leading blanks */
3641 s1 = var1->value;
3642 e1 = s1 + var1->value_length;
3643 while (isblank ((unsigned char) *s1))
3644 s1++;
3645 while (e1 > s1 && isblank ((unsigned char) e1[-1]))
3646 e1--;
3647
3648 s2 = var2->value;
3649 e2 = s2 + var2->value_length;
3650 while (isblank ((unsigned char) *s2))
3651 s2++;
3652 while (e2 > s2 && isblank ((unsigned char) e2[-1]))
3653 e2--;
3654
3655 if (e1 - s1 != e2 - s2)
3656 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3657 if (!memcmp (s1, s2, e1 - s1))
3658 return variable_buffer_output (o, "", 0); /* eq */
3659 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3660 }
3661
3662 /* ignore trailing and leading blanks */
3663 s1 = var1->value;
3664 e1 = s1 + var1->value_length;
3665 while (isblank ((unsigned char) *s1))
3666 s1++;
3667 while (e1 > s1 && isblank ((unsigned char) e1[-1]))
3668 e1--;
3669
3670 s2 = var2->value;
3671 e2 = s2 + var2->value_length;
3672 while (isblank((unsigned char)*s2))
3673 s2++;
3674 while (e2 > s2 && isblank ((unsigned char) e2[-1]))
3675 e2--;
3676
3677 /* both empty after stripping? */
3678 if (s1 == e1 && s2 == e2)
3679 return variable_buffer_output (o, "", 0); /* eq */
3680
3681 /* optimist. */
3682 if ( e1 - s1 == e2 - s2
3683 && !memcmp(s1, s2, e1 - s1))
3684 return variable_buffer_output (o, "", 0); /* eq */
3685
3686 /* compare up to the first '$' or the end. */
3687 x1 = var1->recursive ? memchr (s1, '$', e1 - s1) : NULL;
3688 x2 = var2->recursive ? memchr (s2, '$', e2 - s2) : NULL;
3689 if (!x1 && !x2)
3690 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3691
3692 l1 = x1 ? x1 - s1 : e1 - s1;
3693 l2 = x2 ? x2 - s2 : e2 - s2;
3694 l = l1 <= l2 ? l1 : l2;
3695 if (l && memcmp (s1, s2, l))
3696 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3697
3698 /* one or both buffers now require expanding. */
3699 if (!x1)
3700 s1 += l;
3701 else
3702 {
3703 s1 = a1 = allocated_variable_expand ((char *)s1 + l);
3704 if (!l)
3705 while (isblank ((unsigned char) *s1))
3706 s1++;
3707 e1 = strchr (s1, '\0');
3708 while (e1 > s1 && isblank ((unsigned char) e1[-1]))
3709 e1--;
3710 }
3711
3712 if (!x2)
3713 s2 += l;
3714 else
3715 {
3716 s2 = a2 = allocated_variable_expand ((char *)s2 + l);
3717 if (!l)
3718 while (isblank ((unsigned char) *s2))
3719 s2++;
3720 e2 = strchr (s2, '\0');
3721 while (e2 > s2 && isblank ((unsigned char) e2[-1]))
3722 e2--;
3723 }
3724
3725 /* the final compare */
3726 if ( e1 - s1 != e2 - s2
3727 || memcmp (s1, s2, e1 - s1))
3728 o = comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3729 else
3730 o = variable_buffer_output (o, "", 1) - 1; /* eq */ /** @todo check why this was necessary back the... */
3731 if (a1)
3732 free (a1);
3733 if (a2)
3734 free (a2);
3735 return o;
3736}
3737
3738/*
3739 $(comp-cmds-ex cmds1,cmds2,not-equal-return)
3740
3741 Compares the two strings and return the string in the third argument
3742 if not equal. If equal, nothing is returned.
3743
3744 The comparision will be performed command by command, ignoring not
3745 only leading and trailing spaces on each line but also leading one
3746 leading '@', '-', '+' and '%'.
3747*/
3748static char *
3749func_comp_cmds_ex (char *o, char **argv, const char *funcname)
3750{
3751 const char *s1, *e1, *s2, *e2;
3752 size_t l1, l2;
3753
3754 /* the simple cases */
3755 s1 = argv[0];
3756 s2 = argv[1];
3757 if (s1 == s2)
3758 return variable_buffer_output (o, "", 0); /* eq */
3759 l1 = strlen (argv[0]);
3760 l2 = strlen (argv[1]);
3761
3762 if ( l1 == l2
3763 && !memcmp (s1, s2, l1))
3764 return variable_buffer_output (o, "", 0); /* eq */
3765
3766 /* ignore trailing and leading blanks */
3767 e1 = s1 + l1;
3768 s1 = comp_cmds_strip_leading (s1, e1);
3769
3770 e2 = s2 + l2;
3771 s2 = comp_cmds_strip_leading (s2, e2);
3772
3773 if (e1 - s1 != e2 - s2)
3774 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3775 if (!memcmp (s1, s2, e1 - s1))
3776 return variable_buffer_output (o, "", 0); /* eq */
3777 return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
3778}
3779#endif
3780
3781#ifdef CONFIG_WITH_DATE
3782# if defined (_MSC_VER) /* FIXME: !defined (HAVE_STRPTIME) */
3783char *strptime(const char *s, const char *format, struct tm *tm)
3784{
3785 return (char *)"strptime is not implemented";
3786}
3787# endif
3788/* Check if the string is all blanks or not. */
3789static int
3790all_blanks (const char *s)
3791{
3792 if (!s)
3793 return 1;
3794 while (isspace ((unsigned char)*s))
3795 s++;
3796 return *s == '\0';
3797}
3798
3799/* The first argument is the strftime format string, a iso
3800 timestamp is the default if nothing is given.
3801
3802 The second argument is a time value if given. The format
3803 is either the format from the first argument or given as
3804 an additional third argument. */
3805static char *
3806func_date (char *o, char **argv, const char *funcname)
3807{
3808 char *p;
3809 char *buf;
3810 size_t buf_size;
3811 struct tm t;
3812 const char *format;
3813
3814 /* determin the format - use a single word as the default. */
3815 format = !strcmp (funcname, "date-utc")
3816 ? "%Y-%m-%dT%H:%M:%SZ"
3817 : "%Y-%m-%dT%H:%M:%S";
3818 if (!all_blanks (argv[0]))
3819 format = argv[0];
3820
3821 /* get the time. */
3822 memset (&t, 0, sizeof(t));
3823 if (argv[0] && !all_blanks (argv[1]))
3824 {
3825 const char *input_format = !all_blanks (argv[2]) ? argv[2] : format;
3826 p = strptime (argv[1], input_format, &t);
3827 if (!p || *p != '\0')
3828 {
3829 error (NILF, _("$(%s): strptime(%s,%s,) -> %s\n"), funcname,
3830 argv[1], input_format, p ? p : "<null>");
3831 return variable_buffer_output (o, "", 0);
3832 }
3833 }
3834 else
3835 {
3836 time_t tval;
3837 time (&tval);
3838 if (!strcmp (funcname, "date-utc"))
3839 t = *gmtime (&tval);
3840 else
3841 t = *localtime (&tval);
3842 }
3843
3844 /* format it. note that zero isn't necessarily an error, so we'll
3845 have to keep shut about failures. */
3846 buf_size = 64;
3847 buf = xmalloc (buf_size);
3848 while (strftime (buf, buf_size, format, &t) == 0)
3849 {
3850 if (buf_size >= 4096)
3851 {
3852 *buf = '\0';
3853 break;
3854 }
3855 buf = xrealloc (buf, buf_size <<= 1);
3856 }
3857 o = variable_buffer_output (o, buf, strlen (buf));
3858 free (buf);
3859 return o;
3860}
3861#endif
3862
3863#ifdef CONFIG_WITH_FILE_SIZE
3864/* Prints the size of the specified file. Only one file is
3865 permitted, notthing is stripped. -1 is returned if stat
3866 fails. */
3867static char *
3868func_file_size (char *o, char **argv, const char *funcname UNUSED)
3869{
3870 struct stat st;
3871 if (stat (argv[0], &st))
3872 return variable_buffer_output (o, "-1", 2);
3873 return math_int_to_variable_buffer (o, st.st_size);
3874}
3875#endif
3876
3877#ifdef CONFIG_WITH_WHICH
3878/* Checks if the specified file exists an is executable.
3879 On systems employing executable extensions, the name may
3880 be modified to include the extension. */
3881static int func_which_test_x (char *file)
3882{
3883 struct stat st;
3884# if defined(WINDOWS32) || defined(__OS2__)
3885 char *ext;
3886 char *slash;
3887
3888 /* fix slashes first. */
3889 slash = file;
3890 while ((slash = strchr (slash, '\\')) != NULL)
3891 *slash++ = '/';
3892
3893 /* straight */
3894 if (stat (file, &st) == 0
3895 && S_ISREG (st.st_mode))
3896 return 1;
3897
3898 /* don't try add an extension if there already is one */
3899 ext = strchr (file, '\0');
3900 if (ext - file >= 4
3901 && ( !stricmp (ext - 4, ".exe")
3902 || !stricmp (ext - 4, ".cmd")
3903 || !stricmp (ext - 4, ".bat")
3904 || !stricmp (ext - 4, ".com")))
3905 return 0;
3906
3907 /* try the extensions. */
3908 strcpy (ext, ".exe");
3909 if (stat (file, &st) == 0
3910 && S_ISREG (st.st_mode))
3911 return 1;
3912
3913 strcpy (ext, ".cmd");
3914 if (stat (file, &st) == 0
3915 && S_ISREG (st.st_mode))
3916 return 1;
3917
3918 strcpy (ext, ".bat");
3919 if (stat (file, &st) == 0
3920 && S_ISREG (st.st_mode))
3921 return 1;
3922
3923 strcpy (ext, ".com");
3924 if (stat (file, &st) == 0
3925 && S_ISREG (st.st_mode))
3926 return 1;
3927
3928 return 0;
3929
3930# else
3931
3932 return access (file, X_OK) == 0
3933 && stat (file, &st) == 0
3934 && S_ISREG (st.st_mode);
3935# endif
3936}
3937
3938/* Searches for the specified programs in the PATH and print
3939 their full location if found. Prints nothing if not found. */
3940static char *
3941func_which (char *o, char **argv, const char *funcname UNUSED)
3942{
3943 const char *path;
3944 struct variable *path_var;
3945 unsigned i;
3946 int first = 1;
3947 PATH_VAR (buf);
3948
3949 path_var = lookup_variable ("PATH", 4);
3950 if (path_var)
3951 path = path_var->value;
3952 else
3953 path = ".";
3954
3955 /* iterate input */
3956 for (i = 0; argv[i]; i++)
3957 {
3958 unsigned int len;
3959 const char *iterator = argv[i];
3960 char *cur;
3961
3962 while ((cur = find_next_token (&iterator, &len)))
3963 {
3964 /* if there is a separator, don't walk the path. */
3965 if (memchr (cur, '/', len)
3966#ifdef HAVE_DOS_PATHS
3967 || memchr (cur, '\\', len)
3968 || memchr (cur, ':', len)
3969#endif
3970 )
3971 {
3972 if (len + 1 + 4 < GET_PATH_MAX) /* +4 for .exe */
3973 {
3974 memcpy (buf, cur, len);
3975 buf[len] = '\0';
3976 if (func_which_test_x (buf))
3977 o = variable_buffer_output (o, buf, strlen (buf));
3978 }
3979 }
3980 else
3981 {
3982 const char *comp = path;
3983 for (;;)
3984 {
3985 const char *src = comp;
3986 const char *end = strchr (comp, PATH_SEPARATOR_CHAR);
3987 size_t comp_len = end ? (size_t)(end - comp) : strlen (comp);
3988 if (!comp_len)
3989 {
3990 comp_len = 1;
3991 src = ".";
3992 }
3993 if (len + comp_len + 2 + 4 < GET_PATH_MAX) /* +4 for .exe */
3994 {
3995 memcpy (buf, comp, comp_len);
3996 buf [comp_len] = '/';
3997 memcpy (&buf[comp_len + 1], cur, len);
3998 buf[comp_len + 1 + len] = '\0';
3999
4000 if (func_which_test_x (buf))
4001 {
4002 if (!first)
4003 o = variable_buffer_output (o, " ", 1);
4004 o = variable_buffer_output (o, buf, strlen (buf));
4005 first = 0;
4006 break;
4007 }
4008 }
4009
4010 /* next */
4011 if (!end)
4012 break;
4013 comp = end + 1;
4014 }
4015 }
4016 }
4017 }
4018
4019 return variable_buffer_output (o, "", 0);
4020}
4021#endif /* CONFIG_WITH_WHICH */
4022
4023#ifdef CONFIG_WITH_IF_CONDITIONALS
4024
4025/* Evaluates the expression given in the argument using the
4026 same evaluator as for the new 'if' statements, except now
4027 we don't force the result into a boolean like for 'if' and
4028 '$(if-expr ,,)'. */
4029static char *
4030func_expr (char *o, char **argv, const char *funcname UNUSED)
4031{
4032 o = expr_eval_to_string (o, argv[0]);
4033 return o;
4034}
4035
4036/* Same as '$(if ,,)' except the first argument is evaluated
4037 using the same evaluator as for the new 'if' statements. */
4038static char *
4039func_if_expr (char *o, char **argv, const char *funcname UNUSED)
4040{
4041 int rc;
4042 char *to_expand;
4043
4044 /* Evaluate the condition in argv[0] and expand the 2nd or
4045 3rd argument according to the result. */
4046 rc = expr_eval_if_conditionals (argv[0], NULL);
4047 to_expand = rc == 0 ? argv[1] : argv[2];
4048 if (*to_expand)
4049 {
4050 char *expansion = expand_argument (to_expand, NULL);
4051
4052 o = variable_buffer_output (o, expansion, strlen (expansion));
4053
4054 free (expansion);
4055 }
4056
4057 return o;
4058}
4059
4060#endif /* CONFIG_WITH_IF_CONDITIONALS */
4061
4062#ifdef CONFIG_WITH_SET_CONDITIONALS
4063static char *
4064func_set_intersects (char *o, char **argv, const char *funcname UNUSED)
4065{
4066 const char *s1_cur;
4067 unsigned int s1_len;
4068 const char *s1_iterator = argv[0];
4069
4070 while ((s1_cur = find_next_token (&s1_iterator, &s1_len)) != 0)
4071 {
4072 const char *s2_cur;
4073 unsigned int s2_len;
4074 const char *s2_iterator = argv[1];
4075 while ((s2_cur = find_next_token (&s2_iterator, &s2_len)) != 0)
4076 if (s2_len == s1_len
4077 && strneq (s2_cur, s1_cur, s1_len) )
4078 return variable_buffer_output (o, "1", 1); /* found intersection */
4079 }
4080
4081 return o; /* no intersection */
4082}
4083#endif /* CONFIG_WITH_SET_CONDITIONALS */
4084
4085#ifdef CONFIG_WITH_STACK
4086
4087/* Push an item (string without spaces). */
4088static char *
4089func_stack_push (char *o, char **argv, const char *funcname UNUSED)
4090{
4091 do_variable_definition(NILF, argv[0], argv[1], o_file, f_append, 0 /* !target_var */);
4092 return o;
4093}
4094
4095/* Pops an item off the stack / get the top stack element.
4096 (This is what's tricky to do in pure GNU make syntax.) */
4097static char *
4098func_stack_pop_top (char *o, char **argv, const char *funcname)
4099{
4100 struct variable *stack_var;
4101 const char *stack = argv[0];
4102
4103 stack_var = lookup_variable (stack, strlen (stack) );
4104 if (stack_var)
4105 {
4106 unsigned int len;
4107 const char *iterator = stack_var->value;
4108 char *lastitem = NULL;
4109 char *cur;
4110
4111 while ((cur = find_next_token (&iterator, &len)))
4112 lastitem = cur;
4113
4114 if (lastitem != NULL)
4115 {
4116 if (strcmp (funcname, "stack-popv") != 0)
4117 o = variable_buffer_output (o, lastitem, len);
4118 if (strcmp (funcname, "stack-top") != 0)
4119 {
4120 *lastitem = '\0';
4121 while (lastitem > stack_var->value && isspace (lastitem[-1]))
4122 *--lastitem = '\0';
4123#ifdef CONFIG_WITH_VALUE_LENGTH
4124 stack_var->value_length = lastitem - stack_var->value;
4125#endif
4126 }
4127 }
4128 }
4129 return o;
4130}
4131#endif /* CONFIG_WITH_STACK */
4132
4133#if defined (CONFIG_WITH_MATH) || defined (CONFIG_WITH_NANOTS) || defined (CONFIG_WITH_FILE_SIZE)
4134/* outputs the number (as a string) into the variable buffer. */
4135static char *
4136math_int_to_variable_buffer (char *o, math_int num)
4137{
4138 static const char xdigits[17] = "0123456789abcdef";
4139 int negative;
4140 char strbuf[24]; /* 16 hex + 2 prefix + sign + term => 20
4141 or 20 dec + sign + term => 22 */
4142 char *str = &strbuf[sizeof (strbuf) - 1];
4143
4144 negative = num < 0;
4145 if (negative)
4146 num = -num;
4147
4148 *str = '\0';
4149
4150 do
4151 {
4152#ifdef HEX_MATH_NUMBERS
4153 *--str = xdigits[num & 0xf];
4154 num >>= 4;
4155#else
4156 *--str = xdigits[num % 10];
4157 num /= 10;
4158#endif
4159 }
4160 while (num);
4161
4162#ifdef HEX_MATH_NUMBERS
4163 *--str = 'x';
4164 *--str = '0';
4165#endif
4166
4167 if (negative)
4168 *--str = '-';
4169
4170 return variable_buffer_output (o, str, &strbuf[sizeof (strbuf) - 1] - str);
4171}
4172#endif /* CONFIG_WITH_MATH || CONFIG_WITH_NANOTS */
4173
4174#ifdef CONFIG_WITH_MATH
4175
4176/* Converts a string to an integer, causes an error if the format is invalid. */
4177static math_int
4178math_int_from_string (const char *str)
4179{
4180 const char *start;
4181 unsigned base = 0;
4182 int negative = 0;
4183 math_int num = 0;
4184
4185 /* strip spaces */
4186 while (isspace (*str))
4187 str++;
4188 if (!*str)
4189 {
4190 error (NILF, _("bad number: empty\n"));
4191 return 0;
4192 }
4193 start = str;
4194
4195 /* check for +/- */
4196 while (*str == '+' || *str == '-' || isspace (*str))
4197 if (*str++ == '-')
4198 negative = !negative;
4199
4200 /* check for prefix - we do not accept octal numbers, sorry. */
4201 if (*str == '0' && (str[1] == 'x' || str[1] == 'X'))
4202 {
4203 base = 16;
4204 str += 2;
4205 }
4206 else
4207 {
4208 /* look for a hex digit, if not found treat it as decimal */
4209 const char *p2 = str;
4210 for ( ; *p2; p2++)
4211 if (isxdigit (*p2) && !isdigit (*p2) && isascii (*p2) )
4212 {
4213 base = 16;
4214 break;
4215 }
4216 if (base == 0)
4217 base = 10;
4218 }
4219
4220 /* must have at least one digit! */
4221 if ( !isascii (*str)
4222 || !(base == 16 ? isxdigit (*str) : isdigit (*str)) )
4223 {
4224 error (NILF, _("bad number: '%s'\n"), start);
4225 return 0;
4226 }
4227
4228 /* convert it! */
4229 while (*str && !isspace (*str))
4230 {
4231 int ch = *str++;
4232 if (ch >= '0' && ch <= '9')
4233 ch -= '0';
4234 else if (base == 16 && ch >= 'a' && ch <= 'f')
4235 ch -= 'a' - 10;
4236 else if (base == 16 && ch >= 'A' && ch <= 'F')
4237 ch -= 'A' - 10;
4238 else
4239 {
4240 error (NILF, _("bad number: '%s' (base=%d, pos=%d)\n"), start, base, str - start);
4241 return 0;
4242 }
4243 num *= base;
4244 num += ch;
4245 }
4246
4247 /* check trailing spaces. */
4248 while (isspace (*str))
4249 str++;
4250 if (*str)
4251 {
4252 error (NILF, _("bad number: '%s'\n"), start);
4253 return 0;
4254 }
4255
4256 return negative ? -num : num;
4257}
4258
4259/* Add two or more integer numbers. */
4260static char *
4261func_int_add (char *o, char **argv, const char *funcname UNUSED)
4262{
4263 math_int num;
4264 int i;
4265
4266 num = math_int_from_string (argv[0]);
4267 for (i = 1; argv[i]; i++)
4268 num += math_int_from_string (argv[i]);
4269
4270 return math_int_to_variable_buffer (o, num);
4271}
4272
4273/* Subtract two or more integer numbers. */
4274static char *
4275func_int_sub (char *o, char **argv, const char *funcname UNUSED)
4276{
4277 math_int num;
4278 int i;
4279
4280 num = math_int_from_string (argv[0]);
4281 for (i = 1; argv[i]; i++)
4282 num -= math_int_from_string (argv[i]);
4283
4284 return math_int_to_variable_buffer (o, num);
4285}
4286
4287/* Multiply two or more integer numbers. */
4288static char *
4289func_int_mul (char *o, char **argv, const char *funcname UNUSED)
4290{
4291 math_int num;
4292 int i;
4293
4294 num = math_int_from_string (argv[0]);
4295 for (i = 1; argv[i]; i++)
4296 num *= math_int_from_string (argv[i]);
4297
4298 return math_int_to_variable_buffer (o, num);
4299}
4300
4301/* Divide an integer number by one or more divisors. */
4302static char *
4303func_int_div (char *o, char **argv, const char *funcname UNUSED)
4304{
4305 math_int num;
4306 math_int divisor;
4307 int i;
4308
4309 num = math_int_from_string (argv[0]);
4310 for (i = 1; argv[i]; i++)
4311 {
4312 divisor = math_int_from_string (argv[i]);
4313 if (!divisor)
4314 {
4315 error (NILF, _("divide by zero ('%s')\n"), argv[i]);
4316 return math_int_to_variable_buffer (o, 0);
4317 }
4318 num /= divisor;
4319 }
4320
4321 return math_int_to_variable_buffer (o, num);
4322}
4323
4324
4325/* Divide and return the remainder. */
4326static char *
4327func_int_mod (char *o, char **argv, const char *funcname UNUSED)
4328{
4329 math_int num;
4330 math_int divisor;
4331
4332 num = math_int_from_string (argv[0]);
4333 divisor = math_int_from_string (argv[1]);
4334 if (!divisor)
4335 {
4336 error (NILF, _("divide by zero ('%s')\n"), argv[1]);
4337 return math_int_to_variable_buffer (o, 0);
4338 }
4339 num %= divisor;
4340
4341 return math_int_to_variable_buffer (o, num);
4342}
4343
4344/* 2-complement. */
4345static char *
4346func_int_not (char *o, char **argv, const char *funcname UNUSED)
4347{
4348 math_int num;
4349
4350 num = math_int_from_string (argv[0]);
4351 num = ~num;
4352
4353 return math_int_to_variable_buffer (o, num);
4354}
4355
4356/* Bitwise AND (two or more numbers). */
4357static char *
4358func_int_and (char *o, char **argv, const char *funcname UNUSED)
4359{
4360 math_int num;
4361 int i;
4362
4363 num = math_int_from_string (argv[0]);
4364 for (i = 1; argv[i]; i++)
4365 num &= math_int_from_string (argv[i]);
4366
4367 return math_int_to_variable_buffer (o, num);
4368}
4369
4370/* Bitwise OR (two or more numbers). */
4371static char *
4372func_int_or (char *o, char **argv, const char *funcname UNUSED)
4373{
4374 math_int num;
4375 int i;
4376
4377 num = math_int_from_string (argv[0]);
4378 for (i = 1; argv[i]; i++)
4379 num |= math_int_from_string (argv[i]);
4380
4381 return math_int_to_variable_buffer (o, num);
4382}
4383
4384/* Bitwise XOR (two or more numbers). */
4385static char *
4386func_int_xor (char *o, char **argv, const char *funcname UNUSED)
4387{
4388 math_int num;
4389 int i;
4390
4391 num = math_int_from_string (argv[0]);
4392 for (i = 1; argv[i]; i++)
4393 num ^= math_int_from_string (argv[i]);
4394
4395 return math_int_to_variable_buffer (o, num);
4396}
4397
4398/* Compare two integer numbers. Returns make boolean (true="1"; false=""). */
4399static char *
4400func_int_cmp (char *o, char **argv, const char *funcname)
4401{
4402 math_int num1;
4403 math_int num2;
4404 int rc;
4405
4406 num1 = math_int_from_string (argv[0]);
4407 num2 = math_int_from_string (argv[1]);
4408
4409 funcname += sizeof ("int-") - 1;
4410 if (!strcmp (funcname, "eq"))
4411 rc = num1 == num2;
4412 else if (!strcmp (funcname, "ne"))
4413 rc = num1 != num2;
4414 else if (!strcmp (funcname, "gt"))
4415 rc = num1 > num2;
4416 else if (!strcmp (funcname, "ge"))
4417 rc = num1 >= num2;
4418 else if (!strcmp (funcname, "lt"))
4419 rc = num1 < num2;
4420 else /*if (!strcmp (funcname, "le"))*/
4421 rc = num1 <= num2;
4422
4423 return variable_buffer_output (o, rc ? "1" : "", rc);
4424}
4425
4426#endif /* CONFIG_WITH_MATH */
4427
4428#ifdef CONFIG_WITH_NANOTS
4429/* Returns the current timestamp as nano seconds. The time
4430 source is a high res monotone one if the platform provides
4431 this (and we know about it).
4432
4433 Tip. Use this with int-sub to profile makefile reading
4434 and similar. */
4435static char *
4436func_nanots (char *o, char **argv UNUSED, const char *funcname UNUSED)
4437{
4438 return math_int_to_variable_buffer (o, nano_timestamp ());
4439}
4440#endif
4441
4442#ifdef CONFIG_WITH_OS2_LIBPATH
4443/* Sets or gets the OS/2 libpath variables.
4444
4445 The first argument indicates which variable - BEGINLIBPATH,
4446 ENDLIBPATH, LIBPATHSTRICT or LIBPATH.
4447
4448 The second indicates whether this is a get (not present) or
4449 set (present) operation. When present it is the new value for
4450 the variable. */
4451static char *
4452func_os2_libpath (char *o, char **argv, const char *funcname UNUSED)
4453{
4454 char buf[4096];
4455 ULONG fVar;
4456 APIRET rc;
4457
4458 /* translate variable name (first arg) */
4459 if (!strcmp (argv[0], "BEGINLIBPATH"))
4460 fVar = BEGIN_LIBPATH;
4461 else if (!strcmp (argv[0], "ENDLIBPATH"))
4462 fVar = END_LIBPATH;
4463 else if (!strcmp (argv[0], "LIBPATHSTRICT"))
4464 fVar = LIBPATHSTRICT;
4465 else if (!strcmp (argv[0], "LIBPATH"))
4466 fVar = 0;
4467 else
4468 {
4469 error (NILF, _("$(libpath): unknown variable `%s'"), argv[0]);
4470 return variable_buffer_output (o, "", 0);
4471 }
4472
4473 if (!argv[1])
4474 {
4475 /* get the variable value. */
4476 if (fVar != 0)
4477 {
4478 buf[0] = buf[1] = buf[2] = buf[3] = '\0';
4479 rc = DosQueryExtLIBPATH (buf, fVar);
4480 }
4481 else
4482 rc = DosQueryHeaderInfo (NULLHANDLE, 0, buf, sizeof(buf), QHINF_LIBPATH);
4483 if (rc != NO_ERROR)
4484 {
4485 error (NILF, _("$(libpath): failed to query `%s', rc=%d"), argv[0], rc);
4486 return variable_buffer_output (o, "", 0);
4487 }
4488 o = variable_buffer_output (o, buf, strlen (buf));
4489 }
4490 else
4491 {
4492 /* set the variable value. */
4493 size_t len;
4494 size_t len_max = sizeof (buf) < 2048 ? sizeof (buf) : 2048;
4495 const char *val;
4496 const char *end;
4497
4498 if (fVar == 0)
4499 {
4500 error (NILF, _("$(libpath): LIBPATH is read-only"));
4501 return variable_buffer_output (o, "", 0);
4502 }
4503
4504 /* strip leading and trailing spaces and check for max length. */
4505 val = argv[1];
4506 while (isspace (*val))
4507 val++;
4508 end = strchr (val, '\0');
4509 while (end > val && isspace (end[-1]))
4510 end--;
4511
4512 len = end - val;
4513 if (len >= len_max)
4514 {
4515 error (NILF, _("$(libpath): The new `%s' value is too long (%d bytes, max %d)"),
4516 argv[0], len, len_max);
4517 return variable_buffer_output (o, "", 0);
4518 }
4519
4520 /* make a stripped copy in low memory and try set it. */
4521 memcpy (buf, val, len);
4522 buf[len] = '\0';
4523 rc = DosSetExtLIBPATH (buf, fVar);
4524 if (rc != NO_ERROR)
4525 {
4526 error (NILF, _("$(libpath): failed to set `%s' to `%s', rc=%d"), argv[0], buf, rc);
4527 return variable_buffer_output (o, "", 0);
4528 }
4529
4530 o = variable_buffer_output (o, "", 0);
4531 }
4532 return o;
4533}
4534#endif /* CONFIG_WITH_OS2_LIBPATH */
4535
4536#if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
4537/* Retrieve make statistics. */
4538static char *
4539func_make_stats (char *o, char **argv, const char *funcname UNUSED)
4540{
4541 char buf[512];
4542 int len;
4543
4544 if (!argv[0] || (!argv[0][0] && !argv[1]))
4545 {
4546# ifdef CONFIG_WITH_MAKE_STATS
4547 len = sprintf (buf, "alloc-cur: %5ld/%3ld %3luMB hash: %5lu %2lu%%",
4548 make_stats_allocations,
4549 make_stats_reallocations,
4550 make_stats_allocated / (1024*1024),
4551 make_stats_ht_lookups,
4552 (make_stats_ht_collisions * 100) / make_stats_ht_lookups);
4553 o = variable_buffer_output (o, buf, len);
4554#endif
4555 }
4556 else
4557 {
4558 /* selective */
4559 int i;
4560 for (i = 0; argv[i]; i++)
4561 {
4562 unsigned long val;
4563 if (i != 0)
4564 o = variable_buffer_output (o, " ", 1);
4565 if (0)
4566 continue;
4567# ifdef CONFIG_WITH_MAKE_STATS
4568 else if (!strcmp(argv[i], "allocations"))
4569 val = make_stats_allocations;
4570 else if (!strcmp(argv[i], "reallocations"))
4571 val = make_stats_reallocations;
4572 else if (!strcmp(argv[i], "allocated"))
4573 val = make_stats_allocated;
4574 else if (!strcmp(argv[i], "ht_lookups"))
4575 val = make_stats_ht_lookups;
4576 else if (!strcmp(argv[i], "ht_collisions"))
4577 val = make_stats_ht_collisions;
4578 else if (!strcmp(argv[i], "ht_collisions_pct"))
4579 val = (make_stats_ht_collisions * 100) / make_stats_ht_lookups;
4580#endif
4581 else
4582 {
4583 o = variable_buffer_output (o, argv[i], strlen (argv[i]));
4584 continue;
4585 }
4586
4587 len = sprintf (buf, "%ld", val);
4588 o = variable_buffer_output (o, buf, len);
4589 }
4590 }
4591
4592 return o;
4593}
4594#endif /* CONFIG_WITH_MAKE_STATS */
4595
4596#ifdef CONFIG_WITH_COMMANDS_FUNC
4597/* Gets all the commands for a target, separated by newlines.
4598
4599 This is useful when creating and checking target dependencies since
4600 it reduces the amount of work and the memory consuption. A new prefix
4601 character '%' has been introduced for skipping certain lines, like
4602 for instance the one calling this function and pushing to a dep file.
4603 Blank lines are also skipped.
4604
4605 The commands function takes exactly one argument, which is the name of
4606 the target which commands should be returned.
4607
4608 The commands-sc is identical to commands except that it uses a ';' to
4609 separate the commands.
4610
4611 The commands-usr is similar to commands except that it takes a 2nd
4612 argument that is used to separate the commands. */
4613char *
4614func_commands (char *o, char **argv, const char *funcname)
4615{
4616 struct file *file;
4617 static int recursive = 0;
4618
4619 if (recursive)
4620 {
4621 error (reading_file, _("$(%s ) was invoked recursivly"), funcname);
4622 return variable_buffer_output (o, "recursive", sizeof ("recursive") - 1);
4623 }
4624 if (*argv[0] == '\0')
4625 {
4626 error (reading_file, _("$(%s ) was invoked with an empty target name"), funcname);
4627 return o;
4628 }
4629 recursive = 1;
4630
4631 file = lookup_file (argv[0]);
4632 if (file && file->cmds)
4633 {
4634 unsigned int i;
4635 int cmd_sep_len;
4636 struct commands *cmds = file->cmds;
4637 const char *cmd_sep;
4638
4639 if (!strcmp (funcname, "commands"))
4640 {
4641 cmd_sep = "\n";
4642 cmd_sep_len = 1;
4643 }
4644 else if (!strcmp (funcname, "commands-sc"))
4645 {
4646 cmd_sep = ";";
4647 cmd_sep_len = 1;
4648 }
4649 else /*if (!strcmp (funcname, "commands-usr"))*/
4650 {
4651 cmd_sep = argv[1];
4652 cmd_sep_len = strlen (cmd_sep);
4653 }
4654
4655 initialize_file_variables (file, 1 /* don't search for pattern vars */);
4656 set_file_variables (file, 1 /* early call */);
4657 chop_commands (cmds);
4658
4659 for (i = 0; i < cmds->ncommand_lines; i++)
4660 {
4661 char *p;
4662 char *in, *out, *ref;
4663
4664 /* Skip it if it has a '%' prefix or is blank. */
4665 if (cmds->lines_flags[i] & COMMAND_GETTER_SKIP_IT)
4666 continue;
4667 p = cmds->command_lines[i];
4668 while (isblank ((unsigned char)*p))
4669 p++;
4670 if (*p == '\0')
4671 continue;
4672
4673 /* --- copied from new_job() in job.c --- */
4674
4675 /* Collapse backslash-newline combinations that are inside variable
4676 or function references. These are left alone by the parser so
4677 that they will appear in the echoing of commands (where they look
4678 nice); and collapsed by construct_command_argv when it tokenizes.
4679 But letting them survive inside function invocations loses because
4680 we don't want the functions to see them as part of the text. */
4681
4682 /* IN points to where in the line we are scanning.
4683 OUT points to where in the line we are writing.
4684 When we collapse a backslash-newline combination,
4685 IN gets ahead of OUT. */
4686
4687 in = out = p;
4688 while ((ref = strchr (in, '$')) != 0)
4689 {
4690 ++ref; /* Move past the $. */
4691
4692 if (out != in)
4693 /* Copy the text between the end of the last chunk
4694 we processed (where IN points) and the new chunk
4695 we are about to process (where REF points). */
4696 memmove (out, in, ref - in);
4697
4698 /* Move both pointers past the boring stuff. */
4699 out += ref - in;
4700 in = ref;
4701
4702 if (*ref == '(' || *ref == '{')
4703 {
4704 char openparen = *ref;
4705 char closeparen = openparen == '(' ? ')' : '}';
4706 int count;
4707 char *p2;
4708
4709 *out++ = *in++; /* Copy OPENPAREN. */
4710 /* IN now points past the opening paren or brace.
4711 Count parens or braces until it is matched. */
4712 count = 0;
4713 while (*in != '\0')
4714 {
4715 if (*in == closeparen && --count < 0)
4716 break;
4717 else if (*in == '\\' && in[1] == '\n')
4718 {
4719 /* We have found a backslash-newline inside a
4720 variable or function reference. Eat it and
4721 any following whitespace. */
4722
4723 int quoted = 0;
4724 for (p2 = in - 1; p2 > ref && *p2 == '\\'; --p2)
4725 quoted = !quoted;
4726
4727 if (quoted)
4728 /* There were two or more backslashes, so this is
4729 not really a continuation line. We don't collapse
4730 the quoting backslashes here as is done in
4731 collapse_continuations, because the line will
4732 be collapsed again after expansion. */
4733 *out++ = *in++;
4734 else
4735 {
4736 /* Skip the backslash, newline and
4737 any following whitespace. */
4738 in = next_token (in + 2);
4739
4740 /* Discard any preceding whitespace that has
4741 already been written to the output. */
4742 while (out > ref
4743 && isblank ((unsigned char)out[-1]))
4744 --out;
4745
4746 /* Replace it all with a single space. */
4747 *out++ = ' ';
4748 }
4749 }
4750 else
4751 {
4752 if (*in == openparen)
4753 ++count;
4754
4755 *out++ = *in++;
4756 }
4757 }
4758 }
4759 /* Some of these can be amended ($< perhaps), but we're likely to be called while the
4760 dep expansion happens, so it would have to be on a hackish basis. sad... */
4761 else if (*ref == '<' || *ref == '*' || *ref == '%' || *ref == '^' || *ref == '+')
4762 error (reading_file, _("$(%s ) does not work reliably with $%c in all cases"), funcname, *ref);
4763 }
4764
4765 /* There are no more references in this line to worry about.
4766 Copy the remaining uninteresting text to the output. */
4767 if (out != in)
4768 strcpy (out, in);
4769
4770 /* --- copied from new_job() in job.c --- */
4771
4772 /* Finally, expand the line. */
4773 if (i)
4774 o = variable_buffer_output (o, cmd_sep, cmd_sep_len);
4775 o = variable_expand_for_file_2 (o, cmds->command_lines[i], ~0U, file, NULL);
4776
4777 /* Skip it if it has a '%' prefix or is blank. */
4778 p = o;
4779 while (isblank ((unsigned char)*o)
4780 || *o == '@'
4781 || *o == '-'
4782 || *o == '+')
4783 o++;
4784 if (*o != '\0' && *o != '%')
4785 o = strchr (o, '\0');
4786 else if (i)
4787 o = p - cmd_sep_len;
4788 else
4789 o = p;
4790 } /* for each command line */
4791 }
4792 /* else FIXME: bitch about it? */
4793
4794 recursive = 0;
4795 return o;
4796}
4797#endif /* CONFIG_WITH_COMMANDS_FUNC */
4798
4799#ifdef KMK
4800/* Useful when debugging kmk and/or makefiles. */
4801char *
4802func_breakpoint (char *o, char **argv UNUSED, const char *funcname UNUSED)
4803{
4804#ifdef _MSC_VER
4805 __debugbreak();
4806#elif defined(__i386__) || defined(__x86__) || defined(__X86__) || defined(_M_IX86) || defined(__i386) \
4807 || defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || defined(_M_X64) || defined(__amd64)
4808 __asm__ __volatile__ ("int3\n\t");
4809#else
4810 char *p = (char *)0;
4811 *p = '\0';
4812#endif
4813 return o;
4814}
4815#endif /* KMK */
4816
4817
4818/* Lookup table for builtin functions.
4819
4820 This doesn't have to be sorted; we use a straight lookup. We might gain
4821 some efficiency by moving most often used functions to the start of the
4822 table.
4823
4824 If MAXIMUM_ARGS is 0, that means there is no maximum and all
4825 comma-separated values are treated as arguments.
4826
4827 EXPAND_ARGS means that all arguments should be expanded before invocation.
4828 Functions that do namespace tricks (foreach) don't automatically expand. */
4829
4830static char *func_call (char *o, char **argv, const char *funcname);
4831
4832
4833static struct function_table_entry function_table_init[] =
4834{
4835 /* Name/size */ /* MIN MAX EXP? Function */
4836 { STRING_SIZE_TUPLE("abspath"), 0, 1, 1, func_abspath},
4837 { STRING_SIZE_TUPLE("addprefix"), 2, 2, 1, func_addsuffix_addprefix},
4838 { STRING_SIZE_TUPLE("addsuffix"), 2, 2, 1, func_addsuffix_addprefix},
4839 { STRING_SIZE_TUPLE("basename"), 0, 1, 1, func_basename_dir},
4840 { STRING_SIZE_TUPLE("dir"), 0, 1, 1, func_basename_dir},
4841 { STRING_SIZE_TUPLE("notdir"), 0, 1, 1, func_notdir_suffix},
4842 { STRING_SIZE_TUPLE("subst"), 3, 3, 1, func_subst},
4843 { STRING_SIZE_TUPLE("suffix"), 0, 1, 1, func_notdir_suffix},
4844 { STRING_SIZE_TUPLE("filter"), 2, 2, 1, func_filter_filterout},
4845 { STRING_SIZE_TUPLE("filter-out"), 2, 2, 1, func_filter_filterout},
4846 { STRING_SIZE_TUPLE("findstring"), 2, 2, 1, func_findstring},
4847 { STRING_SIZE_TUPLE("firstword"), 0, 1, 1, func_firstword},
4848 { STRING_SIZE_TUPLE("flavor"), 0, 1, 1, func_flavor},
4849 { STRING_SIZE_TUPLE("join"), 2, 2, 1, func_join},
4850 { STRING_SIZE_TUPLE("lastword"), 0, 1, 1, func_lastword},
4851 { STRING_SIZE_TUPLE("patsubst"), 3, 3, 1, func_patsubst},
4852 { STRING_SIZE_TUPLE("realpath"), 0, 1, 1, func_realpath},
4853#ifdef CONFIG_WITH_RSORT
4854 { STRING_SIZE_TUPLE("rsort"), 0, 1, 1, func_sort},
4855#endif
4856 { STRING_SIZE_TUPLE("shell"), 0, 1, 1, func_shell},
4857 { STRING_SIZE_TUPLE("sort"), 0, 1, 1, func_sort},
4858 { STRING_SIZE_TUPLE("strip"), 0, 1, 1, func_strip},
4859 { STRING_SIZE_TUPLE("wildcard"), 0, 1, 1, func_wildcard},
4860 { STRING_SIZE_TUPLE("word"), 2, 2, 1, func_word},
4861 { STRING_SIZE_TUPLE("wordlist"), 3, 3, 1, func_wordlist},
4862 { STRING_SIZE_TUPLE("words"), 0, 1, 1, func_words},
4863 { STRING_SIZE_TUPLE("origin"), 0, 1, 1, func_origin},
4864 { STRING_SIZE_TUPLE("foreach"), 3, 3, 0, func_foreach},
4865 { STRING_SIZE_TUPLE("call"), 1, 0, 1, func_call},
4866 { STRING_SIZE_TUPLE("info"), 0, 1, 1, func_error},
4867 { STRING_SIZE_TUPLE("error"), 0, 1, 1, func_error},
4868 { STRING_SIZE_TUPLE("warning"), 0, 1, 1, func_error},
4869 { STRING_SIZE_TUPLE("if"), 2, 3, 0, func_if},
4870 { STRING_SIZE_TUPLE("or"), 1, 0, 0, func_or},
4871 { STRING_SIZE_TUPLE("and"), 1, 0, 0, func_and},
4872 { STRING_SIZE_TUPLE("value"), 0, 1, 1, func_value},
4873 { STRING_SIZE_TUPLE("eval"), 0, 1, 1, func_eval},
4874#ifdef CONFIG_WITH_EVALPLUS
4875 { STRING_SIZE_TUPLE("evalctx"), 0, 1, 1, func_evalctx},
4876 { STRING_SIZE_TUPLE("evalval"), 1, 1, 1, func_evalval},
4877 { STRING_SIZE_TUPLE("evalvalctx"), 1, 1, 1, func_evalval},
4878 { STRING_SIZE_TUPLE("evalcall"), 1, 0, 1, func_call},
4879 { STRING_SIZE_TUPLE("evalcall2"), 1, 0, 1, func_call},
4880 { STRING_SIZE_TUPLE("eval-opt-var"), 1, 0, 1, func_eval_optimize_variable},
4881#endif
4882#ifdef EXPERIMENTAL
4883 { STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq},
4884 { STRING_SIZE_TUPLE("not"), 0, 1, 1, func_not},
4885#endif
4886#ifdef CONFIG_WITH_STRING_FUNCTIONS
4887 { STRING_SIZE_TUPLE("length"), 1, 1, 1, func_length},
4888 { STRING_SIZE_TUPLE("length-var"), 1, 1, 1, func_length_var},
4889 { STRING_SIZE_TUPLE("insert"), 3, 3, 1, func_insert},
4890 { STRING_SIZE_TUPLE("pos"), 2, 3, 1, func_pos},
4891 { STRING_SIZE_TUPLE("lastpos"), 2, 3, 1, func_pos},
4892 { STRING_SIZE_TUPLE("substr"), 2, 4, 1, func_substr},
4893 { STRING_SIZE_TUPLE("translate"), 2, 4, 1, func_translate},
4894#endif
4895#ifdef CONFIG_WITH_PRINTF
4896 { STRING_SIZE_TUPLE("printf"), 1, 0, 1, kmk_builtin_func_printf},
4897#endif
4898#ifdef CONFIG_WITH_LAZY_DEPS_VARS
4899 { STRING_SIZE_TUPLE("deps"), 1, 2, 1, func_deps},
4900 { STRING_SIZE_TUPLE("deps-all"), 1, 2, 1, func_deps},
4901 { STRING_SIZE_TUPLE("deps-newer"), 1, 2, 1, func_deps_newer},
4902 { STRING_SIZE_TUPLE("deps-oo"), 1, 2, 1, func_deps_order_only},
4903#endif
4904#ifdef CONFIG_WITH_DEFINED
4905 { STRING_SIZE_TUPLE("defined"), 1, 1, 1, func_defined},
4906#endif
4907#ifdef CONFIG_WITH_TOUPPER_TOLOWER
4908 { STRING_SIZE_TUPLE("toupper"), 0, 1, 1, func_toupper_tolower},
4909 { STRING_SIZE_TUPLE("tolower"), 0, 1, 1, func_toupper_tolower},
4910#endif
4911#ifdef CONFIG_WITH_ABSPATHEX
4912 { STRING_SIZE_TUPLE("abspathex"), 0, 2, 1, func_abspathex},
4913#endif
4914#ifdef CONFIG_WITH_XARGS
4915 { STRING_SIZE_TUPLE("xargs"), 2, 0, 1, func_xargs},
4916#endif
4917#if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
4918 { STRING_SIZE_TUPLE("comp-vars"), 3, 3, 1, func_comp_vars},
4919 { STRING_SIZE_TUPLE("comp-cmds"), 3, 3, 1, func_comp_vars},
4920 { STRING_SIZE_TUPLE("comp-cmds-ex"), 3, 3, 1, func_comp_cmds_ex},
4921#endif
4922#ifdef CONFIG_WITH_DATE
4923 { STRING_SIZE_TUPLE("date"), 0, 1, 1, func_date},
4924 { STRING_SIZE_TUPLE("date-utc"), 0, 3, 1, func_date},
4925#endif
4926#ifdef CONFIG_WITH_FILE_SIZE
4927 { STRING_SIZE_TUPLE("file-size"), 1, 1, 1, func_file_size},
4928#endif
4929#ifdef CONFIG_WITH_WHICH
4930 { STRING_SIZE_TUPLE("which"), 0, 0, 1, func_which},
4931#endif
4932#ifdef CONFIG_WITH_IF_CONDITIONALS
4933 { STRING_SIZE_TUPLE("expr"), 1, 1, 0, func_expr},
4934 { STRING_SIZE_TUPLE("if-expr"), 2, 3, 0, func_if_expr},
4935#endif
4936#ifdef CONFIG_WITH_SET_CONDITIONALS
4937 { STRING_SIZE_TUPLE("intersects"), 2, 2, 1, func_set_intersects},
4938#endif
4939#ifdef CONFIG_WITH_STACK
4940 { STRING_SIZE_TUPLE("stack-push"), 2, 2, 1, func_stack_push},
4941 { STRING_SIZE_TUPLE("stack-pop"), 1, 1, 1, func_stack_pop_top},
4942 { STRING_SIZE_TUPLE("stack-popv"), 1, 1, 1, func_stack_pop_top},
4943 { STRING_SIZE_TUPLE("stack-top"), 1, 1, 1, func_stack_pop_top},
4944#endif
4945#ifdef CONFIG_WITH_MATH
4946 { STRING_SIZE_TUPLE("int-add"), 2, 0, 1, func_int_add},
4947 { STRING_SIZE_TUPLE("int-sub"), 2, 0, 1, func_int_sub},
4948 { STRING_SIZE_TUPLE("int-mul"), 2, 0, 1, func_int_mul},
4949 { STRING_SIZE_TUPLE("int-div"), 2, 0, 1, func_int_div},
4950 { STRING_SIZE_TUPLE("int-mod"), 2, 2, 1, func_int_mod},
4951 { STRING_SIZE_TUPLE("int-not"), 1, 1, 1, func_int_not},
4952 { STRING_SIZE_TUPLE("int-and"), 2, 0, 1, func_int_and},
4953 { STRING_SIZE_TUPLE("int-or"), 2, 0, 1, func_int_or},
4954 { STRING_SIZE_TUPLE("int-xor"), 2, 0, 1, func_int_xor},
4955 { STRING_SIZE_TUPLE("int-eq"), 2, 2, 1, func_int_cmp},
4956 { STRING_SIZE_TUPLE("int-ne"), 2, 2, 1, func_int_cmp},
4957 { STRING_SIZE_TUPLE("int-gt"), 2, 2, 1, func_int_cmp},
4958 { STRING_SIZE_TUPLE("int-ge"), 2, 2, 1, func_int_cmp},
4959 { STRING_SIZE_TUPLE("int-lt"), 2, 2, 1, func_int_cmp},
4960 { STRING_SIZE_TUPLE("int-le"), 2, 2, 1, func_int_cmp},
4961#endif
4962#ifdef CONFIG_WITH_NANOTS
4963 { STRING_SIZE_TUPLE("nanots"), 0, 0, 0, func_nanots},
4964#endif
4965#ifdef CONFIG_WITH_OS2_LIBPATH
4966 { STRING_SIZE_TUPLE("libpath"), 1, 2, 1, func_os2_libpath},
4967#endif
4968#if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
4969 { STRING_SIZE_TUPLE("make-stats"), 0, 0, 0, func_make_stats},
4970#endif
4971#ifdef CONFIG_WITH_COMMANDS_FUNC
4972 { STRING_SIZE_TUPLE("commands"), 1, 1, 1, func_commands},
4973 { STRING_SIZE_TUPLE("commands-sc"), 1, 1, 1, func_commands},
4974 { STRING_SIZE_TUPLE("commands-usr"), 2, 2, 1, func_commands},
4975#endif
4976#ifdef KMK_HELPERS
4977 { STRING_SIZE_TUPLE("kb-src-tool"), 1, 1, 0, func_kbuild_source_tool},
4978 { STRING_SIZE_TUPLE("kb-obj-base"), 1, 1, 0, func_kbuild_object_base},
4979 { STRING_SIZE_TUPLE("kb-obj-suff"), 1, 1, 0, func_kbuild_object_suffix},
4980 { STRING_SIZE_TUPLE("kb-src-prop"), 3, 4, 0, func_kbuild_source_prop},
4981 { STRING_SIZE_TUPLE("kb-src-one"), 0, 1, 0, func_kbuild_source_one},
4982 { STRING_SIZE_TUPLE("kb-exp-tmpl"), 6, 6, 1, func_kbuild_expand_template},
4983#endif
4984#ifdef KMK
4985 { STRING_SIZE_TUPLE("breakpoint"), 0, 0, 0, func_breakpoint},
4986#endif
4987};
4988
4989#define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry))
4990
4991
4992
4993/* These must come after the definition of function_table. */
4994
4995static char *
4996expand_builtin_function (char *o, int argc, char **argv,
4997 const struct function_table_entry *entry_p)
4998{
4999 if (argc < (int)entry_p->minimum_args)
5000 fatal (*expanding_var,
5001 _("insufficient number of arguments (%d) to function `%s'"),
5002 argc, entry_p->name);
5003
5004 /* I suppose technically some function could do something with no
5005 arguments, but so far none do, so just test it for all functions here
5006 rather than in each one. We can change it later if necessary. */
5007
5008 if (!argc)
5009 return o;
5010
5011 if (!entry_p->func_ptr)
5012 fatal (*expanding_var,
5013 _("unimplemented on this platform: function `%s'"), entry_p->name);
5014
5015 return entry_p->func_ptr (o, argv, entry_p->name);
5016}
5017
5018/* Check for a function invocation in *STRINGP. *STRINGP points at the
5019 opening ( or { and is not null-terminated. If a function invocation
5020 is found, expand it into the buffer at *OP, updating *OP, incrementing
5021 *STRINGP past the reference and returning nonzero. If not, return zero. */
5022
5023static int
5024handle_function2 (const struct function_table_entry *entry_p, char **op, const char **stringp) /* bird split it up. */
5025{
5026 char openparen = (*stringp)[0];
5027 char closeparen = openparen == '(' ? ')' : '}';
5028 const char *beg;
5029 const char *end;
5030 int count = 0;
5031 char *abeg = NULL;
5032 char **argv, **argvp;
5033 int nargs;
5034
5035 beg = *stringp + 1;
5036
5037 /* We found a builtin function. Find the beginning of its arguments (skip
5038 whitespace after the name). */
5039
5040 beg = next_token (beg + entry_p->len);
5041
5042 /* Find the end of the function invocation, counting nested use of
5043 whichever kind of parens we use. Since we're looking, count commas
5044 to get a rough estimate of how many arguments we might have. The
5045 count might be high, but it'll never be low. */
5046
5047 for (nargs=1, end=beg; *end != '\0'; ++end)
5048 if (*end == ',')
5049 ++nargs;
5050 else if (*end == openparen)
5051 ++count;
5052 else if (*end == closeparen && --count < 0)
5053 break;
5054
5055 if (count >= 0)
5056 fatal (*expanding_var,
5057 _("unterminated call to function `%s': missing `%c'"),
5058 entry_p->name, closeparen);
5059
5060 *stringp = end;
5061
5062 /* Get some memory to store the arg pointers. */
5063 argvp = argv = alloca (sizeof (char *) * (nargs + 2));
5064
5065 /* Chop the string into arguments, then a nul. As soon as we hit
5066 MAXIMUM_ARGS (if it's >0) assume the rest of the string is part of the
5067 last argument.
5068
5069 If we're expanding, store pointers to the expansion of each one. If
5070 not, make a duplicate of the string and point into that, nul-terminating
5071 each argument. */
5072
5073 if (entry_p->expand_args)
5074 {
5075 const char *p;
5076 for (p=beg, nargs=0; p <= end; ++argvp)
5077 {
5078 const char *next;
5079
5080 ++nargs;
5081
5082 if (nargs == entry_p->maximum_args
5083 || (! (next = find_next_argument (openparen, closeparen, p, end))))
5084 next = end;
5085
5086 *argvp = expand_argument (p, next);
5087 p = next + 1;
5088 }
5089 }
5090 else
5091 {
5092 int len = end - beg;
5093 char *p, *aend;
5094
5095 abeg = xmalloc (len+1);
5096 memcpy (abeg, beg, len);
5097 abeg[len] = '\0';
5098 aend = abeg + len;
5099
5100 for (p=abeg, nargs=0; p <= aend; ++argvp)
5101 {
5102 char *next;
5103
5104 ++nargs;
5105
5106 if (nargs == entry_p->maximum_args
5107 || (! (next = find_next_argument (openparen, closeparen, p, aend))))
5108 next = aend;
5109
5110 *argvp = p;
5111 *next = '\0';
5112 p = next + 1;
5113 }
5114 }
5115 *argvp = NULL;
5116
5117 /* Finally! Run the function... */
5118 *op = expand_builtin_function (*op, nargs, argv, entry_p);
5119
5120 /* Free memory. */
5121 if (entry_p->expand_args)
5122 for (argvp=argv; *argvp != 0; ++argvp)
5123 free (*argvp);
5124 if (abeg)
5125 free (abeg);
5126
5127 return 1;
5128}
5129
5130
5131int /* bird split it up and hacked it. */
5132#ifndef CONFIG_WITH_VALUE_LENGTH
5133handle_function (char **op, const char **stringp)
5134{
5135 const struct function_table_entry *entry_p = lookup_function (*stringp + 1);
5136 if (!entry_p)
5137 return 0;
5138 return handle_function2 (entry_p, op, stringp);
5139}
5140#else /* CONFIG_WITH_VALUE_LENGTH */
5141handle_function (char **op, const char **stringp, const char *nameend, const char *eol UNUSED)
5142{
5143 const char *fname = *stringp + 1;
5144 const struct function_table_entry *entry_p =
5145 lookup_function_in_hash_tab (fname, nameend - fname);
5146 if (!entry_p)
5147 return 0;
5148 return handle_function2 (entry_p, op, stringp);
5149}
5150#endif /* CONFIG_WITH_VALUE_LENGTH */
5151
5152
5153
5154/* User-defined functions. Expand the first argument as either a builtin
5155 function or a make variable, in the context of the rest of the arguments
5156 assigned to $1, $2, ... $N. $0 is the name of the function. */
5157
5158static char *
5159func_call (char *o, char **argv, const char *funcname UNUSED)
5160{
5161 static int max_args = 0;
5162 char *fname;
5163 char *cp;
5164 char *body;
5165 int flen;
5166 int i;
5167 int saved_args;
5168 const struct function_table_entry *entry_p;
5169 struct variable *v;
5170#ifdef CONFIG_WITH_EVALPLUS
5171 char *buf;
5172 unsigned int len;
5173#endif
5174
5175 /* There is no way to define a variable with a space in the name, so strip
5176 leading and trailing whitespace as a favor to the user. */
5177 fname = argv[0];
5178 while (*fname != '\0' && isspace ((unsigned char)*fname))
5179 ++fname;
5180
5181 cp = fname + strlen (fname) - 1;
5182 while (cp > fname && isspace ((unsigned char)*cp))
5183 --cp;
5184 cp[1] = '\0';
5185
5186 /* Calling nothing is a no-op */
5187 if (*fname == '\0')
5188 return o;
5189
5190 /* Are we invoking a builtin function? */
5191
5192#ifndef CONFIG_WITH_VALUE_LENGTH
5193 entry_p = lookup_function (fname);
5194#else
5195 entry_p = lookup_function (fname, cp - fname + 1);
5196#endif
5197 if (entry_p)
5198 {
5199 /* How many arguments do we have? */
5200 for (i=0; argv[i+1]; ++i)
5201 ;
5202 return expand_builtin_function (o, i, argv+1, entry_p);
5203 }
5204
5205 /* Not a builtin, so the first argument is the name of a variable to be
5206 expanded and interpreted as a function. Find it. */
5207 flen = strlen (fname);
5208
5209 v = lookup_variable (fname, flen);
5210
5211 if (v == 0)
5212 warn_undefined (fname, flen);
5213
5214 if (v == 0 || *v->value == '\0')
5215 return o;
5216
5217 body = alloca (flen + 4);
5218 body[0] = '$';
5219 body[1] = '(';
5220 memcpy (body + 2, fname, flen);
5221 body[flen+2] = ')';
5222 body[flen+3] = '\0';
5223
5224 /* Set up arguments $(1) .. $(N). $(0) is the function name. */
5225
5226 push_new_variable_scope ();
5227
5228 for (i=0; *argv; ++i, ++argv)
5229 {
5230 char num[11];
5231
5232 sprintf (num, "%d", i);
5233 define_variable (num, strlen (num), *argv, o_automatic, 0);
5234 }
5235
5236 /* If the number of arguments we have is < max_args, it means we're inside
5237 a recursive invocation of $(call ...). Fill in the remaining arguments
5238 in the new scope with the empty value, to hide them from this
5239 invocation. */
5240
5241 for (; i < max_args; ++i)
5242 {
5243 char num[11];
5244
5245#ifndef CONFIG_WITH_VALUE_LENGTH
5246 sprintf (num, "%d", i);
5247 define_variable (num, strlen (num), "", o_automatic, 0);
5248#else
5249 define_variable (num, sprintf (num, "%d", i), "", o_automatic, 0);
5250#endif
5251 }
5252
5253 saved_args = max_args;
5254 max_args = i;
5255
5256#ifdef CONFIG_WITH_EVALPLUS
5257 if (!strcmp (funcname, "call"))
5258 {
5259#endif
5260 /* Expand the body in the context of the arguments, adding the result to
5261 the variable buffer. */
5262
5263 v->exp_count = EXP_COUNT_MAX;
5264#ifndef CONFIG_WITH_VALUE_LENGTH
5265 o = variable_expand_string (o, body, flen+3);
5266 v->exp_count = 0;
5267
5268 o += strlen (o);
5269#else /* CONFIG_WITH_VALUE_LENGTH */
5270 variable_expand_string_2 (o, body, flen+3, &o);
5271 v->exp_count = 0;
5272#endif /* CONFIG_WITH_VALUE_LENGTH */
5273#ifdef CONFIG_WITH_EVALPLUS
5274 }
5275 else
5276 {
5277 const struct floc *reading_file_saved = reading_file;
5278 char *eos;
5279
5280 if (!strcmp (funcname, "evalcall"))
5281 {
5282 /* Evaluate the variable value without expanding it. We
5283 need a copy since eval_buffer is destructive. */
5284
5285 size_t off = o - variable_buffer;
5286 eos = variable_buffer_output (o, v->value, v->value_length + 1) - 1;
5287 o = variable_buffer + off;
5288 if (v->fileinfo.filenm)
5289 reading_file = &v->fileinfo;
5290 }
5291 else
5292 {
5293 /* Expand the body first and then evaluate the output. */
5294
5295 v->exp_count = EXP_COUNT_MAX;
5296 o = variable_expand_string_2 (o, body, flen+3, &eos);
5297 v->exp_count = 0;
5298 }
5299
5300 install_variable_buffer (&buf, &len);
5301 eval_buffer (o, eos);
5302 restore_variable_buffer (buf, len);
5303 reading_file = reading_file_saved;
5304 }
5305#endif /* CONFIG_WITH_EVALPLUS */
5306
5307 max_args = saved_args;
5308
5309 pop_variable_scope ();
5310
5311 return o;
5312}
5313
5314void
5315hash_init_function_table (void)
5316{
5317 hash_init (&function_table, FUNCTION_TABLE_ENTRIES * 2,
5318 function_table_entry_hash_1, function_table_entry_hash_2,
5319 function_table_entry_hash_cmp);
5320 hash_load (&function_table, function_table_init,
5321 FUNCTION_TABLE_ENTRIES, sizeof (struct function_table_entry));
5322#if defined (CONFIG_WITH_OPTIMIZATION_HACKS) || defined (CONFIG_WITH_VALUE_LENGTH)
5323 {
5324 unsigned int i;
5325 for (i = 0; i < FUNCTION_TABLE_ENTRIES; i++)
5326 {
5327 const char *fn = function_table_init[i].name;
5328 while (*fn)
5329 {
5330 func_char_map[(int)*fn] = 1;
5331 fn++;
5332 }
5333 assert (function_table_init[i].len <= MAX_FUNCTION_LENGTH);
5334 assert (function_table_init[i].len >= MIN_FUNCTION_LENGTH);
5335 }
5336 }
5337#endif
5338}
Note: See TracBrowser for help on using the repository browser.