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