| 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 Free Software
|
|---|
| 4 | 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 2, or (at your option) any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|---|
| 13 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public License along with
|
|---|
| 16 | GNU Make; see the file COPYING. If not, write to the Free Software
|
|---|
| 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
|
|---|
| 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_XARGS /* bird */
|
|---|
| 39 | # ifdef HAVE_LIMITS_H
|
|---|
| 40 | # include <limits.h>
|
|---|
| 41 | # endif
|
|---|
| 42 | #endif
|
|---|
| 43 | #include <assert.h> /* bird */
|
|---|
| 44 |
|
|---|
| 45 | #if defined (CONFIG_WITH_MATH) || defined (CONFIG_WITH_NANOTS) || defined (CONFIG_WITH_FILE_SIZE) /* bird */
|
|---|
| 46 | # include <ctype.h>
|
|---|
| 47 | # ifdef _MSC_VER
|
|---|
| 48 | typedef __int64 math_int;
|
|---|
| 49 | # else
|
|---|
| 50 | # include <stdint.h>
|
|---|
| 51 | typedef int64_t math_int;
|
|---|
| 52 | # endif
|
|---|
| 53 | static char *math_int_to_variable_buffer (char *, math_int);
|
|---|
| 54 | #endif
|
|---|
| 55 |
|
|---|
| 56 | #ifdef CONFIG_WITH_NANOTS /* bird */
|
|---|
| 57 | # ifdef WINDOWS32
|
|---|
| 58 | # include <Windows.h>
|
|---|
| 59 | # endif
|
|---|
| 60 | #endif
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | struct function_table_entry
|
|---|
| 64 | {
|
|---|
| 65 | const char *name;
|
|---|
| 66 | unsigned char len;
|
|---|
| 67 | unsigned char minimum_args;
|
|---|
| 68 | unsigned char maximum_args;
|
|---|
| 69 | char expand_args;
|
|---|
| 70 | char *(*func_ptr) (char *output, char **argv, const char *fname);
|
|---|
| 71 | };
|
|---|
| 72 |
|
|---|
| 73 | static unsigned long
|
|---|
| 74 | function_table_entry_hash_1 (const void *keyv)
|
|---|
| 75 | {
|
|---|
| 76 | const struct function_table_entry *key = keyv;
|
|---|
| 77 | return_STRING_N_HASH_1 (key->name, key->len);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | static unsigned long
|
|---|
| 81 | function_table_entry_hash_2 (const void *keyv)
|
|---|
| 82 | {
|
|---|
| 83 | const struct function_table_entry *key = keyv;
|
|---|
| 84 | return_STRING_N_HASH_2 (key->name, key->len);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | static int
|
|---|
| 88 | function_table_entry_hash_cmp (const void *xv, const void *yv)
|
|---|
| 89 | {
|
|---|
| 90 | const struct function_table_entry *x = xv;
|
|---|
| 91 | const struct function_table_entry *y = yv;
|
|---|
| 92 | int result = x->len - y->len;
|
|---|
| 93 | if (result)
|
|---|
| 94 | return result;
|
|---|
| 95 | return_STRING_N_COMPARE (x->name, y->name, x->len);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | static struct hash_table function_table;
|
|---|
| 99 | |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
|
|---|
| 103 | each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
|
|---|
| 104 | the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
|
|---|
| 105 | nonzero, substitutions are done only on matches which are complete
|
|---|
| 106 | whitespace-delimited words. */
|
|---|
| 107 |
|
|---|
| 108 | char *
|
|---|
| 109 | subst_expand (char *o, const char *text, const char *subst, const char *replace,
|
|---|
| 110 | unsigned int slen, unsigned int rlen, int by_word)
|
|---|
| 111 | {
|
|---|
| 112 | const char *t = text;
|
|---|
| 113 | const char *p;
|
|---|
| 114 |
|
|---|
| 115 | if (slen == 0 && !by_word)
|
|---|
| 116 | {
|
|---|
| 117 | /* The first occurrence of "" in any string is its end. */
|
|---|
| 118 | o = variable_buffer_output (o, t, strlen (t));
|
|---|
| 119 | if (rlen > 0)
|
|---|
| 120 | o = variable_buffer_output (o, replace, rlen);
|
|---|
| 121 | return o;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | do
|
|---|
| 125 | {
|
|---|
| 126 | if (by_word && slen == 0)
|
|---|
| 127 | /* When matching by words, the empty string should match
|
|---|
| 128 | the end of each word, rather than the end of the whole text. */
|
|---|
| 129 | p = end_of_token (next_token (t));
|
|---|
| 130 | else
|
|---|
| 131 | {
|
|---|
| 132 | p = strstr (t, subst);
|
|---|
| 133 | if (p == 0)
|
|---|
| 134 | {
|
|---|
| 135 | /* No more matches. Output everything left on the end. */
|
|---|
| 136 | o = variable_buffer_output (o, t, strlen (t));
|
|---|
| 137 | return o;
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /* Output everything before this occurrence of the string to replace. */
|
|---|
| 142 | if (p > t)
|
|---|
| 143 | o = variable_buffer_output (o, t, p - t);
|
|---|
| 144 |
|
|---|
| 145 | /* If we're substituting only by fully matched words,
|
|---|
| 146 | or only at the ends of words, check that this case qualifies. */
|
|---|
| 147 | if (by_word
|
|---|
| 148 | && ((p > text && !isblank ((unsigned char)p[-1]))
|
|---|
| 149 | || (p[slen] != '\0' && !isblank ((unsigned char)p[slen]))))
|
|---|
| 150 | /* Struck out. Output the rest of the string that is
|
|---|
| 151 | no longer to be replaced. */
|
|---|
| 152 | o = variable_buffer_output (o, subst, slen);
|
|---|
| 153 | else if (rlen > 0)
|
|---|
| 154 | /* Output the replacement string. */
|
|---|
| 155 | o = variable_buffer_output (o, replace, rlen);
|
|---|
| 156 |
|
|---|
| 157 | /* Advance T past the string to be replaced. */
|
|---|
| 158 | t = p + slen;
|
|---|
| 159 | } while (*t != '\0');
|
|---|
| 160 |
|
|---|
| 161 | return o;
|
|---|
| 162 | }
|
|---|
| 163 | |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
|
|---|
| 167 | and replacing strings matching PATTERN with REPLACE.
|
|---|
| 168 | If PATTERN_PERCENT is not nil, PATTERN has already been
|
|---|
| 169 | run through find_percent, and PATTERN_PERCENT is the result.
|
|---|
| 170 | If REPLACE_PERCENT is not nil, REPLACE has already been
|
|---|
| 171 | run through find_percent, and REPLACE_PERCENT is the result.
|
|---|
| 172 | Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
|
|---|
| 173 | character _AFTER_ the %, not to the % itself.
|
|---|
| 174 | */
|
|---|
| 175 |
|
|---|
| 176 | char *
|
|---|
| 177 | patsubst_expand_pat (char *o, const char *text,
|
|---|
| 178 | const char *pattern, const char *replace,
|
|---|
| 179 | const char *pattern_percent, const char *replace_percent)
|
|---|
| 180 | {
|
|---|
| 181 | unsigned int pattern_prepercent_len, pattern_postpercent_len;
|
|---|
| 182 | unsigned int replace_prepercent_len, replace_postpercent_len;
|
|---|
| 183 | const char *t;
|
|---|
| 184 | unsigned int len;
|
|---|
| 185 | int doneany = 0;
|
|---|
| 186 |
|
|---|
| 187 | /* Record the length of REPLACE before and after the % so we don't have to
|
|---|
| 188 | compute these lengths more than once. */
|
|---|
| 189 | if (replace_percent)
|
|---|
| 190 | {
|
|---|
| 191 | replace_prepercent_len = replace_percent - replace - 1;
|
|---|
| 192 | replace_postpercent_len = strlen (replace_percent);
|
|---|
| 193 | }
|
|---|
| 194 | else
|
|---|
| 195 | {
|
|---|
| 196 | replace_prepercent_len = strlen (replace);
|
|---|
| 197 | replace_postpercent_len = 0;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | if (!pattern_percent)
|
|---|
| 201 | /* With no % in the pattern, this is just a simple substitution. */
|
|---|
| 202 | return subst_expand (o, text, pattern, replace,
|
|---|
| 203 | strlen (pattern), strlen (replace), 1);
|
|---|
| 204 |
|
|---|
| 205 | /* Record the length of PATTERN before and after the %
|
|---|
| 206 | so we don't have to compute it more than once. */
|
|---|
| 207 | pattern_prepercent_len = pattern_percent - pattern - 1;
|
|---|
| 208 | pattern_postpercent_len = strlen (pattern_percent);
|
|---|
| 209 |
|
|---|
| 210 | while ((t = find_next_token (&text, &len)) != 0)
|
|---|
| 211 | {
|
|---|
| 212 | int fail = 0;
|
|---|
| 213 |
|
|---|
| 214 | /* Is it big enough to match? */
|
|---|
| 215 | if (len < pattern_prepercent_len + pattern_postpercent_len)
|
|---|
| 216 | fail = 1;
|
|---|
| 217 |
|
|---|
| 218 | /* Does the prefix match? */
|
|---|
| 219 | if (!fail && pattern_prepercent_len > 0
|
|---|
| 220 | && (*t != *pattern
|
|---|
| 221 | || t[pattern_prepercent_len - 1] != pattern_percent[-2]
|
|---|
| 222 | || !strneq (t + 1, pattern + 1, pattern_prepercent_len - 1)))
|
|---|
| 223 | fail = 1;
|
|---|
| 224 |
|
|---|
| 225 | /* Does the suffix match? */
|
|---|
| 226 | if (!fail && pattern_postpercent_len > 0
|
|---|
| 227 | && (t[len - 1] != pattern_percent[pattern_postpercent_len - 1]
|
|---|
| 228 | || t[len - pattern_postpercent_len] != *pattern_percent
|
|---|
| 229 | || !strneq (&t[len - pattern_postpercent_len],
|
|---|
| 230 | pattern_percent, pattern_postpercent_len - 1)))
|
|---|
| 231 | fail = 1;
|
|---|
| 232 |
|
|---|
| 233 | if (fail)
|
|---|
| 234 | /* It didn't match. Output the string. */
|
|---|
| 235 | o = variable_buffer_output (o, t, len);
|
|---|
| 236 | else
|
|---|
| 237 | {
|
|---|
| 238 | /* It matched. Output the replacement. */
|
|---|
| 239 |
|
|---|
| 240 | /* Output the part of the replacement before the %. */
|
|---|
| 241 | o = variable_buffer_output (o, replace, replace_prepercent_len);
|
|---|
| 242 |
|
|---|
| 243 | if (replace_percent != 0)
|
|---|
| 244 | {
|
|---|
| 245 | /* Output the part of the matched string that
|
|---|
| 246 | matched the % in the pattern. */
|
|---|
| 247 | o = variable_buffer_output (o, t + pattern_prepercent_len,
|
|---|
| 248 | len - (pattern_prepercent_len
|
|---|
| 249 | + pattern_postpercent_len));
|
|---|
| 250 | /* Output the part of the replacement after the %. */
|
|---|
| 251 | o = variable_buffer_output (o, replace_percent,
|
|---|
| 252 | replace_postpercent_len);
|
|---|
| 253 | }
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /* Output a space, but not if the replacement is "". */
|
|---|
| 257 | if (fail || replace_prepercent_len > 0
|
|---|
| 258 | || (replace_percent != 0 && len + replace_postpercent_len > 0))
|
|---|
| 259 | {
|
|---|
| 260 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 261 | doneany = 1;
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|
| 264 | if (doneany)
|
|---|
| 265 | /* Kill the last space. */
|
|---|
| 266 | --o;
|
|---|
| 267 |
|
|---|
| 268 | return o;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
|
|---|
| 272 | and replacing strings matching PATTERN with REPLACE.
|
|---|
| 273 | If PATTERN_PERCENT is not nil, PATTERN has already been
|
|---|
| 274 | run through find_percent, and PATTERN_PERCENT is the result.
|
|---|
| 275 | If REPLACE_PERCENT is not nil, REPLACE has already been
|
|---|
| 276 | run through find_percent, and REPLACE_PERCENT is the result.
|
|---|
| 277 | Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
|
|---|
| 278 | character _AFTER_ the %, not to the % itself.
|
|---|
| 279 | */
|
|---|
| 280 |
|
|---|
| 281 | char *
|
|---|
| 282 | patsubst_expand (char *o, const char *text, char *pattern, char *replace)
|
|---|
| 283 | {
|
|---|
| 284 | const char *pattern_percent = find_percent (pattern);
|
|---|
| 285 | const char *replace_percent = find_percent (replace);
|
|---|
| 286 |
|
|---|
| 287 | /* If there's a percent in the pattern or replacement skip it. */
|
|---|
| 288 | if (replace_percent)
|
|---|
| 289 | ++replace_percent;
|
|---|
| 290 | if (pattern_percent)
|
|---|
| 291 | ++pattern_percent;
|
|---|
| 292 |
|
|---|
| 293 | return patsubst_expand_pat (o, text, pattern, replace,
|
|---|
| 294 | pattern_percent, replace_percent);
|
|---|
| 295 | }
|
|---|
| 296 | |
|---|
| 297 |
|
|---|
| 298 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
|---|
| 299 | /* The maximum length of a function, once reached there is
|
|---|
| 300 | it can't be function and we can skip the hash lookup drop out. */
|
|---|
| 301 |
|
|---|
| 302 | # ifdef KMK
|
|---|
| 303 | # define MAX_FUNCTION_LENGTH 12
|
|---|
| 304 | # else
|
|---|
| 305 | # define MAX_FUNCTION_LENGTH 10
|
|---|
| 306 | # endif
|
|---|
| 307 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 308 |
|
|---|
| 309 | /* Look up a function by name. */
|
|---|
| 310 |
|
|---|
| 311 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
|---|
| 312 | __inline
|
|---|
| 313 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 314 | static const struct function_table_entry *
|
|---|
| 315 | lookup_function (const char *s)
|
|---|
| 316 | {
|
|---|
| 317 | const char *e = s;
|
|---|
| 318 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
|---|
| 319 | int left = MAX_FUNCTION_LENGTH;
|
|---|
| 320 | int ch;
|
|---|
| 321 | while (((ch = *e) >= 'a' && ch <='z') || ch == '-')
|
|---|
| 322 | {
|
|---|
| 323 | if (!left--)
|
|---|
| 324 | return 0;
|
|---|
| 325 | e++;
|
|---|
| 326 | }
|
|---|
| 327 | #else
|
|---|
| 328 | while (*e && ( (*e >= 'a' && *e <= 'z') || *e == '-'))
|
|---|
| 329 | e++;
|
|---|
| 330 | #endif
|
|---|
| 331 | if (*e == '\0' || isblank ((unsigned char) *e))
|
|---|
| 332 | {
|
|---|
| 333 | struct function_table_entry function_table_entry_key;
|
|---|
| 334 | function_table_entry_key.name = s;
|
|---|
| 335 | function_table_entry_key.len = e - s;
|
|---|
| 336 |
|
|---|
| 337 | return hash_find_item (&function_table, &function_table_entry_key);
|
|---|
| 338 | }
|
|---|
| 339 | return 0;
|
|---|
| 340 | }
|
|---|
| 341 | |
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 | /* Return 1 if PATTERN matches STR, 0 if not. */
|
|---|
| 345 |
|
|---|
| 346 | int
|
|---|
| 347 | pattern_matches (const char *pattern, const char *percent, const char *str)
|
|---|
| 348 | {
|
|---|
| 349 | unsigned int sfxlen, strlength;
|
|---|
| 350 |
|
|---|
| 351 | if (percent == 0)
|
|---|
| 352 | {
|
|---|
| 353 | unsigned int len = strlen (pattern) + 1;
|
|---|
| 354 | char *new_chars = alloca (len);
|
|---|
| 355 | memcpy (new_chars, pattern, len);
|
|---|
| 356 | percent = find_percent (new_chars);
|
|---|
| 357 | if (percent == 0)
|
|---|
| 358 | return streq (new_chars, str);
|
|---|
| 359 | pattern = new_chars;
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | sfxlen = strlen (percent + 1);
|
|---|
| 363 | strlength = strlen (str);
|
|---|
| 364 |
|
|---|
| 365 | if (strlength < (percent - pattern) + sfxlen
|
|---|
| 366 | || !strneq (pattern, str, percent - pattern))
|
|---|
| 367 | return 0;
|
|---|
| 368 |
|
|---|
| 369 | return !strcmp (percent + 1, str + (strlength - sfxlen));
|
|---|
| 370 | }
|
|---|
| 371 | |
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 | /* Find the next comma or ENDPAREN (counting nested STARTPAREN and
|
|---|
| 375 | ENDPARENtheses), starting at PTR before END. Return a pointer to
|
|---|
| 376 | next character.
|
|---|
| 377 |
|
|---|
| 378 | If no next argument is found, return NULL.
|
|---|
| 379 | */
|
|---|
| 380 |
|
|---|
| 381 | static char *
|
|---|
| 382 | find_next_argument (char startparen, char endparen,
|
|---|
| 383 | const char *ptr, const char *end)
|
|---|
| 384 | {
|
|---|
| 385 | int count = 0;
|
|---|
| 386 |
|
|---|
| 387 | for (; ptr < end; ++ptr)
|
|---|
| 388 | if (*ptr == startparen)
|
|---|
| 389 | ++count;
|
|---|
| 390 |
|
|---|
| 391 | else if (*ptr == endparen)
|
|---|
| 392 | {
|
|---|
| 393 | --count;
|
|---|
| 394 | if (count < 0)
|
|---|
| 395 | return NULL;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | else if (*ptr == ',' && !count)
|
|---|
| 399 | return (char *)ptr;
|
|---|
| 400 |
|
|---|
| 401 | /* We didn't find anything. */
|
|---|
| 402 | return NULL;
|
|---|
| 403 | }
|
|---|
| 404 | |
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 | /* Glob-expand LINE. The returned pointer is
|
|---|
| 408 | only good until the next call to string_glob. */
|
|---|
| 409 |
|
|---|
| 410 | static char *
|
|---|
| 411 | string_glob (char *line)
|
|---|
| 412 | {
|
|---|
| 413 | static char *result = 0;
|
|---|
| 414 | static unsigned int length;
|
|---|
| 415 | struct nameseq *chain;
|
|---|
| 416 | unsigned int idx;
|
|---|
| 417 |
|
|---|
| 418 | chain = multi_glob (parse_file_seq
|
|---|
| 419 | (&line, '\0', sizeof (struct nameseq),
|
|---|
| 420 | /* We do not want parse_file_seq to strip `./'s.
|
|---|
| 421 | That would break examples like:
|
|---|
| 422 | $(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
|
|---|
| 423 | 0),
|
|---|
| 424 | sizeof (struct nameseq));
|
|---|
| 425 |
|
|---|
| 426 | if (result == 0)
|
|---|
| 427 | {
|
|---|
| 428 | length = 100;
|
|---|
| 429 | result = xmalloc (100);
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 | idx = 0;
|
|---|
| 433 | while (chain != 0)
|
|---|
| 434 | {
|
|---|
| 435 | const char *name = chain->name;
|
|---|
| 436 | unsigned int len = strlen (name);
|
|---|
| 437 |
|
|---|
| 438 | struct nameseq *next = chain->next;
|
|---|
| 439 | free (chain);
|
|---|
| 440 | chain = next;
|
|---|
| 441 |
|
|---|
| 442 | /* multi_glob will pass names without globbing metacharacters
|
|---|
| 443 | through as is, but we want only files that actually exist. */
|
|---|
| 444 | if (file_exists_p (name))
|
|---|
| 445 | {
|
|---|
| 446 | if (idx + len + 1 > length)
|
|---|
| 447 | {
|
|---|
| 448 | length += (len + 1) * 2;
|
|---|
| 449 | result = xrealloc (result, length);
|
|---|
| 450 | }
|
|---|
| 451 | memcpy (&result[idx], name, len);
|
|---|
| 452 | idx += len;
|
|---|
| 453 | result[idx++] = ' ';
|
|---|
| 454 | }
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | /* Kill the last space and terminate the string. */
|
|---|
| 458 | if (idx == 0)
|
|---|
| 459 | result[0] = '\0';
|
|---|
| 460 | else
|
|---|
| 461 | result[idx - 1] = '\0';
|
|---|
| 462 |
|
|---|
| 463 | return result;
|
|---|
| 464 | }
|
|---|
| 465 | |
|---|
| 466 |
|
|---|
| 467 | /*
|
|---|
| 468 | Builtin functions
|
|---|
| 469 | */
|
|---|
| 470 |
|
|---|
| 471 | static char *
|
|---|
| 472 | func_patsubst (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 473 | {
|
|---|
| 474 | o = patsubst_expand (o, argv[2], argv[0], argv[1]);
|
|---|
| 475 | return o;
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 | static char *
|
|---|
| 480 | func_join (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 481 | {
|
|---|
| 482 | int doneany = 0;
|
|---|
| 483 |
|
|---|
| 484 | /* Write each word of the first argument directly followed
|
|---|
| 485 | by the corresponding word of the second argument.
|
|---|
| 486 | If the two arguments have a different number of words,
|
|---|
| 487 | the excess words are just output separated by blanks. */
|
|---|
| 488 | const char *tp;
|
|---|
| 489 | const char *pp;
|
|---|
| 490 | const char *list1_iterator = argv[0];
|
|---|
| 491 | const char *list2_iterator = argv[1];
|
|---|
| 492 | do
|
|---|
| 493 | {
|
|---|
| 494 | unsigned int len1, len2;
|
|---|
| 495 |
|
|---|
| 496 | tp = find_next_token (&list1_iterator, &len1);
|
|---|
| 497 | if (tp != 0)
|
|---|
| 498 | o = variable_buffer_output (o, tp, len1);
|
|---|
| 499 |
|
|---|
| 500 | pp = find_next_token (&list2_iterator, &len2);
|
|---|
| 501 | if (pp != 0)
|
|---|
| 502 | o = variable_buffer_output (o, pp, len2);
|
|---|
| 503 |
|
|---|
| 504 | if (tp != 0 || pp != 0)
|
|---|
| 505 | {
|
|---|
| 506 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 507 | doneany = 1;
|
|---|
| 508 | }
|
|---|
| 509 | }
|
|---|
| 510 | while (tp != 0 || pp != 0);
|
|---|
| 511 | if (doneany)
|
|---|
| 512 | /* Kill the last blank. */
|
|---|
| 513 | --o;
|
|---|
| 514 |
|
|---|
| 515 | return o;
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 | static char *
|
|---|
| 520 | func_origin (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 521 | {
|
|---|
| 522 | /* Expand the argument. */
|
|---|
| 523 | struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
|---|
| 524 | if (v == 0)
|
|---|
| 525 | o = variable_buffer_output (o, "undefined", 9);
|
|---|
| 526 | else
|
|---|
| 527 | switch (v->origin)
|
|---|
| 528 | {
|
|---|
| 529 | default:
|
|---|
| 530 | case o_invalid:
|
|---|
| 531 | abort ();
|
|---|
| 532 | break;
|
|---|
| 533 | case o_default:
|
|---|
| 534 | o = variable_buffer_output (o, "default", 7);
|
|---|
| 535 | break;
|
|---|
| 536 | case o_env:
|
|---|
| 537 | o = variable_buffer_output (o, "environment", 11);
|
|---|
| 538 | break;
|
|---|
| 539 | case o_file:
|
|---|
| 540 | o = variable_buffer_output (o, "file", 4);
|
|---|
| 541 | break;
|
|---|
| 542 | case o_env_override:
|
|---|
| 543 | o = variable_buffer_output (o, "environment override", 20);
|
|---|
| 544 | break;
|
|---|
| 545 | case o_command:
|
|---|
| 546 | o = variable_buffer_output (o, "command line", 12);
|
|---|
| 547 | break;
|
|---|
| 548 | case o_override:
|
|---|
| 549 | o = variable_buffer_output (o, "override", 8);
|
|---|
| 550 | break;
|
|---|
| 551 | case o_automatic:
|
|---|
| 552 | o = variable_buffer_output (o, "automatic", 9);
|
|---|
| 553 | break;
|
|---|
| 554 | }
|
|---|
| 555 |
|
|---|
| 556 | return o;
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | static char *
|
|---|
| 560 | func_flavor (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 561 | {
|
|---|
| 562 | struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
|---|
| 563 |
|
|---|
| 564 | if (v == 0)
|
|---|
| 565 | o = variable_buffer_output (o, "undefined", 9);
|
|---|
| 566 | else
|
|---|
| 567 | if (v->recursive)
|
|---|
| 568 | o = variable_buffer_output (o, "recursive", 9);
|
|---|
| 569 | else
|
|---|
| 570 | o = variable_buffer_output (o, "simple", 6);
|
|---|
| 571 |
|
|---|
| 572 | return o;
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | #ifdef VMS
|
|---|
| 576 | # define IS_PATHSEP(c) ((c) == ']')
|
|---|
| 577 | #else
|
|---|
| 578 | # ifdef HAVE_DOS_PATHS
|
|---|
| 579 | # define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
|
|---|
| 580 | # else
|
|---|
| 581 | # define IS_PATHSEP(c) ((c) == '/')
|
|---|
| 582 | # endif
|
|---|
| 583 | #endif
|
|---|
| 584 |
|
|---|
| 585 |
|
|---|
| 586 | static char *
|
|---|
| 587 | func_notdir_suffix (char *o, char **argv, const char *funcname)
|
|---|
| 588 | {
|
|---|
| 589 | /* Expand the argument. */
|
|---|
| 590 | const char *list_iterator = argv[0];
|
|---|
| 591 | const char *p2;
|
|---|
| 592 | int doneany =0;
|
|---|
| 593 | unsigned int len=0;
|
|---|
| 594 |
|
|---|
| 595 | int is_suffix = streq (funcname, "suffix");
|
|---|
| 596 | int is_notdir = !is_suffix;
|
|---|
| 597 | while ((p2 = find_next_token (&list_iterator, &len)) != 0)
|
|---|
| 598 | {
|
|---|
| 599 | const char *p = p2 + len;
|
|---|
| 600 |
|
|---|
| 601 |
|
|---|
| 602 | while (p >= p2 && (!is_suffix || *p != '.'))
|
|---|
| 603 | {
|
|---|
| 604 | if (IS_PATHSEP (*p))
|
|---|
| 605 | break;
|
|---|
| 606 | --p;
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | if (p >= p2)
|
|---|
| 610 | {
|
|---|
| 611 | if (is_notdir)
|
|---|
| 612 | ++p;
|
|---|
| 613 | else if (*p != '.')
|
|---|
| 614 | continue;
|
|---|
| 615 | o = variable_buffer_output (o, p, len - (p - p2));
|
|---|
| 616 | }
|
|---|
| 617 | #ifdef HAVE_DOS_PATHS
|
|---|
| 618 | /* Handle the case of "d:foo/bar". */
|
|---|
| 619 | else if (streq (funcname, "notdir") && p2[0] && p2[1] == ':')
|
|---|
| 620 | {
|
|---|
| 621 | p = p2 + 2;
|
|---|
| 622 | o = variable_buffer_output (o, p, len - (p - p2));
|
|---|
| 623 | }
|
|---|
| 624 | #endif
|
|---|
| 625 | else if (is_notdir)
|
|---|
| 626 | o = variable_buffer_output (o, p2, len);
|
|---|
| 627 |
|
|---|
| 628 | if (is_notdir || p >= p2)
|
|---|
| 629 | {
|
|---|
| 630 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 631 | doneany = 1;
|
|---|
| 632 | }
|
|---|
| 633 | }
|
|---|
| 634 |
|
|---|
| 635 | if (doneany)
|
|---|
| 636 | /* Kill last space. */
|
|---|
| 637 | --o;
|
|---|
| 638 |
|
|---|
| 639 | return o;
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 | static char *
|
|---|
| 644 | func_basename_dir (char *o, char **argv, const char *funcname)
|
|---|
| 645 | {
|
|---|
| 646 | /* Expand the argument. */
|
|---|
| 647 | const char *p3 = argv[0];
|
|---|
| 648 | const char *p2;
|
|---|
| 649 | int doneany=0;
|
|---|
| 650 | unsigned int len=0;
|
|---|
| 651 |
|
|---|
| 652 | int is_basename= streq (funcname, "basename");
|
|---|
| 653 | int is_dir= !is_basename;
|
|---|
| 654 |
|
|---|
| 655 | while ((p2 = find_next_token (&p3, &len)) != 0)
|
|---|
| 656 | {
|
|---|
| 657 | const char *p = p2 + len;
|
|---|
| 658 | while (p >= p2 && (!is_basename || *p != '.'))
|
|---|
| 659 | {
|
|---|
| 660 | if (IS_PATHSEP (*p))
|
|---|
| 661 | break;
|
|---|
| 662 | --p;
|
|---|
| 663 | }
|
|---|
| 664 |
|
|---|
| 665 | if (p >= p2 && (is_dir))
|
|---|
| 666 | o = variable_buffer_output (o, p2, ++p - p2);
|
|---|
| 667 | else if (p >= p2 && (*p == '.'))
|
|---|
| 668 | o = variable_buffer_output (o, p2, p - p2);
|
|---|
| 669 | #ifdef HAVE_DOS_PATHS
|
|---|
| 670 | /* Handle the "d:foobar" case */
|
|---|
| 671 | else if (p2[0] && p2[1] == ':' && is_dir)
|
|---|
| 672 | o = variable_buffer_output (o, p2, 2);
|
|---|
| 673 | #endif
|
|---|
| 674 | else if (is_dir)
|
|---|
| 675 | #ifdef VMS
|
|---|
| 676 | o = variable_buffer_output (o, "[]", 2);
|
|---|
| 677 | #else
|
|---|
| 678 | #ifndef _AMIGA
|
|---|
| 679 | o = variable_buffer_output (o, "./", 2);
|
|---|
| 680 | #else
|
|---|
| 681 | ; /* Just a nop... */
|
|---|
| 682 | #endif /* AMIGA */
|
|---|
| 683 | #endif /* !VMS */
|
|---|
| 684 | else
|
|---|
| 685 | /* The entire name is the basename. */
|
|---|
| 686 | o = variable_buffer_output (o, p2, len);
|
|---|
| 687 |
|
|---|
| 688 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 689 | doneany = 1;
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 | if (doneany)
|
|---|
| 693 | /* Kill last space. */
|
|---|
| 694 | --o;
|
|---|
| 695 |
|
|---|
| 696 | return o;
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 | static char *
|
|---|
| 700 | func_addsuffix_addprefix (char *o, char **argv, const char *funcname)
|
|---|
| 701 | {
|
|---|
| 702 | int fixlen = strlen (argv[0]);
|
|---|
| 703 | const char *list_iterator = argv[1];
|
|---|
| 704 | int is_addprefix = streq (funcname, "addprefix");
|
|---|
| 705 | int is_addsuffix = !is_addprefix;
|
|---|
| 706 |
|
|---|
| 707 | int doneany = 0;
|
|---|
| 708 | const char *p;
|
|---|
| 709 | unsigned int len;
|
|---|
| 710 |
|
|---|
| 711 | while ((p = find_next_token (&list_iterator, &len)) != 0)
|
|---|
| 712 | {
|
|---|
| 713 | if (is_addprefix)
|
|---|
| 714 | o = variable_buffer_output (o, argv[0], fixlen);
|
|---|
| 715 | o = variable_buffer_output (o, p, len);
|
|---|
| 716 | if (is_addsuffix)
|
|---|
| 717 | o = variable_buffer_output (o, argv[0], fixlen);
|
|---|
| 718 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 719 | doneany = 1;
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | if (doneany)
|
|---|
| 723 | /* Kill last space. */
|
|---|
| 724 | --o;
|
|---|
| 725 |
|
|---|
| 726 | return o;
|
|---|
| 727 | }
|
|---|
| 728 |
|
|---|
| 729 | static char *
|
|---|
| 730 | func_subst (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 731 | {
|
|---|
| 732 | o = subst_expand (o, argv[2], argv[0], argv[1], strlen (argv[0]),
|
|---|
| 733 | strlen (argv[1]), 0);
|
|---|
| 734 |
|
|---|
| 735 | return o;
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 |
|
|---|
| 739 | static char *
|
|---|
| 740 | func_firstword (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 741 | {
|
|---|
| 742 | unsigned int i;
|
|---|
| 743 | const char *words = argv[0]; /* Use a temp variable for find_next_token */
|
|---|
| 744 | const char *p = find_next_token (&words, &i);
|
|---|
| 745 |
|
|---|
| 746 | if (p != 0)
|
|---|
| 747 | o = variable_buffer_output (o, p, i);
|
|---|
| 748 |
|
|---|
| 749 | return o;
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | static char *
|
|---|
| 753 | func_lastword (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 754 | {
|
|---|
| 755 | unsigned int i;
|
|---|
| 756 | const char *words = argv[0]; /* Use a temp variable for find_next_token */
|
|---|
| 757 | const char *p = NULL;
|
|---|
| 758 | const char *t;
|
|---|
| 759 |
|
|---|
| 760 | while ((t = find_next_token (&words, &i)))
|
|---|
| 761 | p = t;
|
|---|
| 762 |
|
|---|
| 763 | if (p != 0)
|
|---|
| 764 | o = variable_buffer_output (o, p, i);
|
|---|
| 765 |
|
|---|
| 766 | return o;
|
|---|
| 767 | }
|
|---|
| 768 |
|
|---|
| 769 | static char *
|
|---|
| 770 | func_words (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 771 | {
|
|---|
| 772 | int i = 0;
|
|---|
| 773 | const char *word_iterator = argv[0];
|
|---|
| 774 | char buf[20];
|
|---|
| 775 |
|
|---|
| 776 | while (find_next_token (&word_iterator, (unsigned int *) 0) != 0)
|
|---|
| 777 | ++i;
|
|---|
| 778 |
|
|---|
| 779 | sprintf (buf, "%d", i);
|
|---|
| 780 | o = variable_buffer_output (o, buf, strlen (buf));
|
|---|
| 781 |
|
|---|
| 782 | return o;
|
|---|
| 783 | }
|
|---|
| 784 |
|
|---|
| 785 | /* Set begpp to point to the first non-whitespace character of the string,
|
|---|
| 786 | * and endpp to point to the last non-whitespace character of the string.
|
|---|
| 787 | * If the string is empty or contains nothing but whitespace, endpp will be
|
|---|
| 788 | * begpp-1.
|
|---|
| 789 | */
|
|---|
| 790 | char *
|
|---|
| 791 | strip_whitespace (const char **begpp, const char **endpp)
|
|---|
| 792 | {
|
|---|
| 793 | while (*begpp <= *endpp && isspace ((unsigned char)**begpp))
|
|---|
| 794 | (*begpp) ++;
|
|---|
| 795 | while (*endpp >= *begpp && isspace ((unsigned char)**endpp))
|
|---|
| 796 | (*endpp) --;
|
|---|
| 797 | return (char *)*begpp;
|
|---|
| 798 | }
|
|---|
| 799 |
|
|---|
| 800 | static void
|
|---|
| 801 | check_numeric (const char *s, const char *msg)
|
|---|
| 802 | {
|
|---|
| 803 | const char *end = s + strlen (s) - 1;
|
|---|
| 804 | const char *beg = s;
|
|---|
| 805 | strip_whitespace (&s, &end);
|
|---|
| 806 |
|
|---|
| 807 | for (; s <= end; ++s)
|
|---|
| 808 | if (!ISDIGIT (*s)) /* ISDIGIT only evals its arg once: see make.h. */
|
|---|
| 809 | break;
|
|---|
| 810 |
|
|---|
| 811 | if (s <= end || end - beg < 0)
|
|---|
| 812 | fatal (*expanding_var, "%s: '%s'", msg, beg);
|
|---|
| 813 | }
|
|---|
| 814 |
|
|---|
| 815 |
|
|---|
| 816 |
|
|---|
| 817 | static char *
|
|---|
| 818 | func_word (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 819 | {
|
|---|
| 820 | const char *end_p;
|
|---|
| 821 | const char *p;
|
|---|
| 822 | int i;
|
|---|
| 823 |
|
|---|
| 824 | /* Check the first argument. */
|
|---|
| 825 | check_numeric (argv[0], _("non-numeric first argument to `word' function"));
|
|---|
| 826 | i = atoi (argv[0]);
|
|---|
| 827 |
|
|---|
| 828 | if (i == 0)
|
|---|
| 829 | fatal (*expanding_var,
|
|---|
| 830 | _("first argument to `word' function must be greater than 0"));
|
|---|
| 831 |
|
|---|
| 832 | end_p = argv[1];
|
|---|
| 833 | while ((p = find_next_token (&end_p, 0)) != 0)
|
|---|
| 834 | if (--i == 0)
|
|---|
| 835 | break;
|
|---|
| 836 |
|
|---|
| 837 | if (i == 0)
|
|---|
| 838 | o = variable_buffer_output (o, p, end_p - p);
|
|---|
| 839 |
|
|---|
| 840 | return o;
|
|---|
| 841 | }
|
|---|
| 842 |
|
|---|
| 843 | static char *
|
|---|
| 844 | func_wordlist (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 845 | {
|
|---|
| 846 | int start, count;
|
|---|
| 847 |
|
|---|
| 848 | /* Check the arguments. */
|
|---|
| 849 | check_numeric (argv[0],
|
|---|
| 850 | _("non-numeric first argument to `wordlist' function"));
|
|---|
| 851 | check_numeric (argv[1],
|
|---|
| 852 | _("non-numeric second argument to `wordlist' function"));
|
|---|
| 853 |
|
|---|
| 854 | start = atoi (argv[0]);
|
|---|
| 855 | if (start < 1)
|
|---|
| 856 | fatal (*expanding_var,
|
|---|
| 857 | "invalid first argument to `wordlist' function: `%d'", start);
|
|---|
| 858 |
|
|---|
| 859 | count = atoi (argv[1]) - start + 1;
|
|---|
| 860 |
|
|---|
| 861 | if (count > 0)
|
|---|
| 862 | {
|
|---|
| 863 | const char *p;
|
|---|
| 864 | const char *end_p = argv[2];
|
|---|
| 865 |
|
|---|
| 866 | /* Find the beginning of the "start"th word. */
|
|---|
| 867 | while (((p = find_next_token (&end_p, 0)) != 0) && --start)
|
|---|
| 868 | ;
|
|---|
| 869 |
|
|---|
| 870 | if (p)
|
|---|
| 871 | {
|
|---|
| 872 | /* Find the end of the "count"th word from start. */
|
|---|
| 873 | while (--count && (find_next_token (&end_p, 0) != 0))
|
|---|
| 874 | ;
|
|---|
| 875 |
|
|---|
| 876 | /* Return the stuff in the middle. */
|
|---|
| 877 | o = variable_buffer_output (o, p, end_p - p);
|
|---|
| 878 | }
|
|---|
| 879 | }
|
|---|
| 880 |
|
|---|
| 881 | return o;
|
|---|
| 882 | }
|
|---|
| 883 |
|
|---|
| 884 | static char *
|
|---|
| 885 | func_findstring (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 886 | {
|
|---|
| 887 | /* Find the first occurrence of the first string in the second. */
|
|---|
| 888 | if (strstr (argv[1], argv[0]) != 0)
|
|---|
| 889 | o = variable_buffer_output (o, argv[0], strlen (argv[0]));
|
|---|
| 890 |
|
|---|
| 891 | return o;
|
|---|
| 892 | }
|
|---|
| 893 |
|
|---|
| 894 | static char *
|
|---|
| 895 | func_foreach (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 896 | {
|
|---|
| 897 | /* expand only the first two. */
|
|---|
| 898 | char *varname = expand_argument (argv[0], NULL);
|
|---|
| 899 | char *list = expand_argument (argv[1], NULL);
|
|---|
| 900 | const char *body = argv[2];
|
|---|
| 901 |
|
|---|
| 902 | int doneany = 0;
|
|---|
| 903 | const char *list_iterator = list;
|
|---|
| 904 | const char *p;
|
|---|
| 905 | unsigned int len;
|
|---|
| 906 | struct variable *var;
|
|---|
| 907 |
|
|---|
| 908 | push_new_variable_scope ();
|
|---|
| 909 | var = define_variable (varname, strlen (varname), "", o_automatic, 0);
|
|---|
| 910 |
|
|---|
| 911 | /* loop through LIST, put the value in VAR and expand BODY */
|
|---|
| 912 | while ((p = find_next_token (&list_iterator, &len)) != 0)
|
|---|
| 913 | {
|
|---|
| 914 | char *result = 0;
|
|---|
| 915 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
|---|
| 916 | if (len >= (unsigned int)var->value_alloc_len)
|
|---|
| 917 | {
|
|---|
| 918 | free (var->value);
|
|---|
| 919 | var->value_alloc_len = (len + 32) & ~31;
|
|---|
| 920 | var->value = xmalloc (var->value_alloc_len);
|
|---|
| 921 | }
|
|---|
| 922 | memcpy (var->value, p, len);
|
|---|
| 923 | var->value[len] = '\0';
|
|---|
| 924 | var->value_length = len;
|
|---|
| 925 | #else
|
|---|
| 926 | free (var->value);
|
|---|
| 927 | var->value = savestring (p, len);
|
|---|
| 928 | #endif
|
|---|
| 929 |
|
|---|
| 930 | result = allocated_variable_expand (body);
|
|---|
| 931 |
|
|---|
| 932 | o = variable_buffer_output (o, result, strlen (result));
|
|---|
| 933 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 934 | doneany = 1;
|
|---|
| 935 | free (result);
|
|---|
| 936 | }
|
|---|
| 937 |
|
|---|
| 938 | if (doneany)
|
|---|
| 939 | /* Kill the last space. */
|
|---|
| 940 | --o;
|
|---|
| 941 |
|
|---|
| 942 | pop_variable_scope ();
|
|---|
| 943 | free (varname);
|
|---|
| 944 | free (list);
|
|---|
| 945 |
|
|---|
| 946 | return o;
|
|---|
| 947 | }
|
|---|
| 948 |
|
|---|
| 949 | struct a_word
|
|---|
| 950 | {
|
|---|
| 951 | struct a_word *next;
|
|---|
| 952 | struct a_word *chain;
|
|---|
| 953 | char *str;
|
|---|
| 954 | int length;
|
|---|
| 955 | int matched;
|
|---|
| 956 | };
|
|---|
| 957 |
|
|---|
| 958 | static unsigned long
|
|---|
| 959 | a_word_hash_1 (const void *key)
|
|---|
| 960 | {
|
|---|
| 961 | return_STRING_HASH_1 (((struct a_word const *) key)->str);
|
|---|
| 962 | }
|
|---|
| 963 |
|
|---|
| 964 | static unsigned long
|
|---|
| 965 | a_word_hash_2 (const void *key)
|
|---|
| 966 | {
|
|---|
| 967 | return_STRING_HASH_2 (((struct a_word const *) key)->str);
|
|---|
| 968 | }
|
|---|
| 969 |
|
|---|
| 970 | static int
|
|---|
| 971 | a_word_hash_cmp (const void *x, const void *y)
|
|---|
| 972 | {
|
|---|
| 973 | int result = ((struct a_word const *) x)->length - ((struct a_word const *) y)->length;
|
|---|
| 974 | if (result)
|
|---|
| 975 | return result;
|
|---|
| 976 | return_STRING_COMPARE (((struct a_word const *) x)->str,
|
|---|
| 977 | ((struct a_word const *) y)->str);
|
|---|
| 978 | }
|
|---|
| 979 |
|
|---|
| 980 | struct a_pattern
|
|---|
| 981 | {
|
|---|
| 982 | struct a_pattern *next;
|
|---|
| 983 | char *str;
|
|---|
| 984 | char *percent;
|
|---|
| 985 | int length;
|
|---|
| 986 | int save_c;
|
|---|
| 987 | };
|
|---|
| 988 |
|
|---|
| 989 | static char *
|
|---|
| 990 | func_filter_filterout (char *o, char **argv, const char *funcname)
|
|---|
| 991 | {
|
|---|
| 992 | struct a_word *wordhead;
|
|---|
| 993 | struct a_word **wordtail;
|
|---|
| 994 | struct a_word *wp;
|
|---|
| 995 | struct a_pattern *pathead;
|
|---|
| 996 | struct a_pattern **pattail;
|
|---|
| 997 | struct a_pattern *pp;
|
|---|
| 998 |
|
|---|
| 999 | struct hash_table a_word_table;
|
|---|
| 1000 | int is_filter = streq (funcname, "filter");
|
|---|
| 1001 | const char *pat_iterator = argv[0];
|
|---|
| 1002 | const char *word_iterator = argv[1];
|
|---|
| 1003 | int literals = 0;
|
|---|
| 1004 | int words = 0;
|
|---|
| 1005 | int hashing = 0;
|
|---|
| 1006 | char *p;
|
|---|
| 1007 | unsigned int len;
|
|---|
| 1008 |
|
|---|
| 1009 | /* Chop ARGV[0] up into patterns to match against the words. */
|
|---|
| 1010 |
|
|---|
| 1011 | pattail = &pathead;
|
|---|
| 1012 | while ((p = find_next_token (&pat_iterator, &len)) != 0)
|
|---|
| 1013 | {
|
|---|
| 1014 | struct a_pattern *pat = alloca (sizeof (struct a_pattern));
|
|---|
| 1015 |
|
|---|
| 1016 | *pattail = pat;
|
|---|
| 1017 | pattail = &pat->next;
|
|---|
| 1018 |
|
|---|
| 1019 | if (*pat_iterator != '\0')
|
|---|
| 1020 | ++pat_iterator;
|
|---|
| 1021 |
|
|---|
| 1022 | pat->str = p;
|
|---|
| 1023 | pat->length = len;
|
|---|
| 1024 | pat->save_c = p[len];
|
|---|
| 1025 | p[len] = '\0';
|
|---|
| 1026 | pat->percent = find_percent (p);
|
|---|
| 1027 | if (pat->percent == 0)
|
|---|
| 1028 | literals++;
|
|---|
| 1029 | }
|
|---|
| 1030 | *pattail = 0;
|
|---|
| 1031 |
|
|---|
| 1032 | /* Chop ARGV[1] up into words to match against the patterns. */
|
|---|
| 1033 |
|
|---|
| 1034 | wordtail = &wordhead;
|
|---|
| 1035 | while ((p = find_next_token (&word_iterator, &len)) != 0)
|
|---|
| 1036 | {
|
|---|
| 1037 | struct a_word *word = alloca (sizeof (struct a_word));
|
|---|
| 1038 |
|
|---|
| 1039 | *wordtail = word;
|
|---|
| 1040 | wordtail = &word->next;
|
|---|
| 1041 |
|
|---|
| 1042 | if (*word_iterator != '\0')
|
|---|
| 1043 | ++word_iterator;
|
|---|
| 1044 |
|
|---|
| 1045 | p[len] = '\0';
|
|---|
| 1046 | word->str = p;
|
|---|
| 1047 | word->length = len;
|
|---|
| 1048 | word->matched = 0;
|
|---|
| 1049 | word->chain = 0;
|
|---|
| 1050 | words++;
|
|---|
| 1051 | }
|
|---|
| 1052 | *wordtail = 0;
|
|---|
| 1053 |
|
|---|
| 1054 | /* Only use a hash table if arg list lengths justifies the cost. */
|
|---|
| 1055 | hashing = (literals >= 2 && (literals * words) >= 10);
|
|---|
| 1056 | if (hashing)
|
|---|
| 1057 | {
|
|---|
| 1058 | hash_init (&a_word_table, words, a_word_hash_1, a_word_hash_2,
|
|---|
| 1059 | a_word_hash_cmp);
|
|---|
| 1060 | for (wp = wordhead; wp != 0; wp = wp->next)
|
|---|
| 1061 | {
|
|---|
| 1062 | struct a_word *owp = hash_insert (&a_word_table, wp);
|
|---|
| 1063 | if (owp)
|
|---|
| 1064 | wp->chain = owp;
|
|---|
| 1065 | }
|
|---|
| 1066 | }
|
|---|
| 1067 |
|
|---|
| 1068 | if (words)
|
|---|
| 1069 | {
|
|---|
| 1070 | int doneany = 0;
|
|---|
| 1071 |
|
|---|
| 1072 | /* Run each pattern through the words, killing words. */
|
|---|
| 1073 | for (pp = pathead; pp != 0; pp = pp->next)
|
|---|
| 1074 | {
|
|---|
| 1075 | if (pp->percent)
|
|---|
| 1076 | for (wp = wordhead; wp != 0; wp = wp->next)
|
|---|
| 1077 | wp->matched |= pattern_matches (pp->str, pp->percent, wp->str);
|
|---|
| 1078 | else if (hashing)
|
|---|
| 1079 | {
|
|---|
| 1080 | struct a_word a_word_key;
|
|---|
| 1081 | a_word_key.str = pp->str;
|
|---|
| 1082 | a_word_key.length = pp->length;
|
|---|
| 1083 | wp = hash_find_item (&a_word_table, &a_word_key);
|
|---|
| 1084 | while (wp)
|
|---|
| 1085 | {
|
|---|
| 1086 | wp->matched |= 1;
|
|---|
| 1087 | wp = wp->chain;
|
|---|
| 1088 | }
|
|---|
| 1089 | }
|
|---|
| 1090 | else
|
|---|
| 1091 | for (wp = wordhead; wp != 0; wp = wp->next)
|
|---|
| 1092 | wp->matched |= (wp->length == pp->length
|
|---|
| 1093 | && strneq (pp->str, wp->str, wp->length));
|
|---|
| 1094 | }
|
|---|
| 1095 |
|
|---|
| 1096 | /* Output the words that matched (or didn't, for filter-out). */
|
|---|
| 1097 | for (wp = wordhead; wp != 0; wp = wp->next)
|
|---|
| 1098 | if (is_filter ? wp->matched : !wp->matched)
|
|---|
| 1099 | {
|
|---|
| 1100 | o = variable_buffer_output (o, wp->str, strlen (wp->str));
|
|---|
| 1101 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 1102 | doneany = 1;
|
|---|
| 1103 | }
|
|---|
| 1104 |
|
|---|
| 1105 | if (doneany)
|
|---|
| 1106 | /* Kill the last space. */
|
|---|
| 1107 | --o;
|
|---|
| 1108 | }
|
|---|
| 1109 |
|
|---|
| 1110 | for (pp = pathead; pp != 0; pp = pp->next)
|
|---|
| 1111 | pp->str[pp->length] = pp->save_c;
|
|---|
| 1112 |
|
|---|
| 1113 | if (hashing)
|
|---|
| 1114 | hash_free (&a_word_table, 0);
|
|---|
| 1115 |
|
|---|
| 1116 | return o;
|
|---|
| 1117 | }
|
|---|
| 1118 |
|
|---|
| 1119 |
|
|---|
| 1120 | static char *
|
|---|
| 1121 | func_strip (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 1122 | {
|
|---|
| 1123 | const char *p = argv[0];
|
|---|
| 1124 | int doneany = 0;
|
|---|
| 1125 |
|
|---|
| 1126 | while (*p != '\0')
|
|---|
| 1127 | {
|
|---|
| 1128 | int i=0;
|
|---|
| 1129 | const char *word_start;
|
|---|
| 1130 |
|
|---|
| 1131 | while (isspace ((unsigned char)*p))
|
|---|
| 1132 | ++p;
|
|---|
| 1133 | word_start = p;
|
|---|
| 1134 | for (i=0; *p != '\0' && !isspace ((unsigned char)*p); ++p, ++i)
|
|---|
| 1135 | {}
|
|---|
| 1136 | if (!i)
|
|---|
| 1137 | break;
|
|---|
| 1138 | o = variable_buffer_output (o, word_start, i);
|
|---|
| 1139 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 1140 | doneany = 1;
|
|---|
| 1141 | }
|
|---|
| 1142 |
|
|---|
| 1143 | if (doneany)
|
|---|
| 1144 | /* Kill the last space. */
|
|---|
| 1145 | --o;
|
|---|
| 1146 |
|
|---|
| 1147 | return o;
|
|---|
| 1148 | }
|
|---|
| 1149 |
|
|---|
| 1150 | /*
|
|---|
| 1151 | Print a warning or fatal message.
|
|---|
| 1152 | */
|
|---|
| 1153 | static char *
|
|---|
| 1154 | func_error (char *o, char **argv, const char *funcname)
|
|---|
| 1155 | {
|
|---|
| 1156 | char **argvp;
|
|---|
| 1157 | char *msg, *p;
|
|---|
| 1158 | int len;
|
|---|
| 1159 |
|
|---|
| 1160 | /* The arguments will be broken on commas. Rather than create yet
|
|---|
| 1161 | another special case where function arguments aren't broken up,
|
|---|
| 1162 | just create a format string that puts them back together. */
|
|---|
| 1163 | for (len=0, argvp=argv; *argvp != 0; ++argvp)
|
|---|
| 1164 | len += strlen (*argvp) + 2;
|
|---|
| 1165 |
|
|---|
| 1166 | p = msg = alloca (len + 1);
|
|---|
| 1167 |
|
|---|
| 1168 | for (argvp=argv; argvp[1] != 0; ++argvp)
|
|---|
| 1169 | {
|
|---|
| 1170 | strcpy (p, *argvp);
|
|---|
| 1171 | p += strlen (*argvp);
|
|---|
| 1172 | *(p++) = ',';
|
|---|
| 1173 | *(p++) = ' ';
|
|---|
| 1174 | }
|
|---|
| 1175 | strcpy (p, *argvp);
|
|---|
| 1176 |
|
|---|
| 1177 | switch (*funcname) {
|
|---|
| 1178 | case 'e':
|
|---|
| 1179 | fatal (reading_file, "%s", msg);
|
|---|
| 1180 |
|
|---|
| 1181 | case 'w':
|
|---|
| 1182 | error (reading_file, "%s", msg);
|
|---|
| 1183 | break;
|
|---|
| 1184 |
|
|---|
| 1185 | case 'i':
|
|---|
| 1186 | printf ("%s\n", msg);
|
|---|
| 1187 | fflush(stdout);
|
|---|
| 1188 | break;
|
|---|
| 1189 |
|
|---|
| 1190 | default:
|
|---|
| 1191 | fatal (*expanding_var, "Internal error: func_error: '%s'", funcname);
|
|---|
| 1192 | }
|
|---|
| 1193 |
|
|---|
| 1194 | /* The warning function expands to the empty string. */
|
|---|
| 1195 | return o;
|
|---|
| 1196 | }
|
|---|
| 1197 |
|
|---|
| 1198 |
|
|---|
| 1199 | /*
|
|---|
| 1200 | chop argv[0] into words, and sort them.
|
|---|
| 1201 | */
|
|---|
| 1202 | static char *
|
|---|
| 1203 | func_sort (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 1204 | {
|
|---|
| 1205 | const char *t;
|
|---|
| 1206 | char **words;
|
|---|
| 1207 | int wordi;
|
|---|
| 1208 | char *p;
|
|---|
| 1209 | unsigned int len;
|
|---|
| 1210 | int i;
|
|---|
| 1211 |
|
|---|
| 1212 | /* Find the maximum number of words we'll have. */
|
|---|
| 1213 | t = argv[0];
|
|---|
| 1214 | wordi = 1;
|
|---|
| 1215 | while (*t != '\0')
|
|---|
| 1216 | {
|
|---|
| 1217 | char c = *(t++);
|
|---|
| 1218 |
|
|---|
| 1219 | if (! isspace ((unsigned char)c))
|
|---|
| 1220 | continue;
|
|---|
| 1221 |
|
|---|
| 1222 | ++wordi;
|
|---|
| 1223 |
|
|---|
| 1224 | while (isspace ((unsigned char)*t))
|
|---|
| 1225 | ++t;
|
|---|
| 1226 | }
|
|---|
| 1227 |
|
|---|
| 1228 | words = xmalloc (wordi * sizeof (char *));
|
|---|
| 1229 |
|
|---|
| 1230 | /* Now assign pointers to each string in the array. */
|
|---|
| 1231 | t = argv[0];
|
|---|
| 1232 | wordi = 0;
|
|---|
| 1233 | while ((p = find_next_token (&t, &len)) != 0)
|
|---|
| 1234 | {
|
|---|
| 1235 | ++t;
|
|---|
| 1236 | p[len] = '\0';
|
|---|
| 1237 | words[wordi++] = p;
|
|---|
| 1238 | }
|
|---|
| 1239 |
|
|---|
| 1240 | if (wordi)
|
|---|
| 1241 | {
|
|---|
| 1242 | /* Now sort the list of words. */
|
|---|
| 1243 | qsort (words, wordi, sizeof (char *), alpha_compare);
|
|---|
| 1244 |
|
|---|
| 1245 | /* Now write the sorted list, uniquified. */
|
|---|
| 1246 | #ifdef CONFIG_WITH_RSORT
|
|---|
| 1247 | if (strcmp (funcname, "rsort"))
|
|---|
| 1248 | {
|
|---|
| 1249 | /* sort */
|
|---|
| 1250 | #endif
|
|---|
| 1251 | for (i = 0; i < wordi; ++i)
|
|---|
| 1252 | {
|
|---|
| 1253 | len = strlen (words[i]);
|
|---|
| 1254 | if (i == wordi - 1 || strlen (words[i + 1]) != len
|
|---|
| 1255 | || strcmp (words[i], words[i + 1]))
|
|---|
| 1256 | {
|
|---|
| 1257 | o = variable_buffer_output (o, words[i], len);
|
|---|
| 1258 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 1259 | }
|
|---|
| 1260 | }
|
|---|
| 1261 | #ifdef CONFIG_WITH_RSORT
|
|---|
| 1262 | }
|
|---|
| 1263 | else
|
|---|
| 1264 | {
|
|---|
| 1265 | /* rsort - reverse the result */
|
|---|
| 1266 | i = wordi;
|
|---|
| 1267 | while (i-- > 0)
|
|---|
| 1268 | {
|
|---|
| 1269 | len = strlen (words[i]);
|
|---|
| 1270 | if (i == 0 || strlen (words[i - 1]) != len
|
|---|
| 1271 | || strcmp (words[i], words[i - 1]))
|
|---|
| 1272 | {
|
|---|
| 1273 | o = variable_buffer_output (o, words[i], len);
|
|---|
| 1274 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 1275 | }
|
|---|
| 1276 | }
|
|---|
| 1277 | }
|
|---|
| 1278 | #endif
|
|---|
| 1279 |
|
|---|
| 1280 | /* Kill the last space. */
|
|---|
| 1281 | --o;
|
|---|
| 1282 | }
|
|---|
| 1283 |
|
|---|
| 1284 | free (words);
|
|---|
| 1285 |
|
|---|
| 1286 | return o;
|
|---|
| 1287 | }
|
|---|
| 1288 |
|
|---|
| 1289 | /*
|
|---|
| 1290 | $(if condition,true-part[,false-part])
|
|---|
| 1291 |
|
|---|
| 1292 | CONDITION is false iff it evaluates to an empty string. White
|
|---|
| 1293 | space before and after condition are stripped before evaluation.
|
|---|
| 1294 |
|
|---|
| 1295 | If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is
|
|---|
| 1296 | evaluated (if it exists). Because only one of the two PARTs is evaluated,
|
|---|
| 1297 | you can use $(if ...) to create side-effects (with $(shell ...), for
|
|---|
| 1298 | example).
|
|---|
| 1299 | */
|
|---|
| 1300 |
|
|---|
| 1301 | static char *
|
|---|
| 1302 | func_if (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 1303 | {
|
|---|
| 1304 | const char *begp = argv[0];
|
|---|
| 1305 | const char *endp = begp + strlen (argv[0]) - 1;
|
|---|
| 1306 | int result = 0;
|
|---|
| 1307 |
|
|---|
| 1308 | /* Find the result of the condition: if we have a value, and it's not
|
|---|
| 1309 | empty, the condition is true. If we don't have a value, or it's the
|
|---|
| 1310 | empty string, then it's false. */
|
|---|
| 1311 |
|
|---|
| 1312 | strip_whitespace (&begp, &endp);
|
|---|
| 1313 |
|
|---|
| 1314 | if (begp <= endp)
|
|---|
| 1315 | {
|
|---|
| 1316 | char *expansion = expand_argument (begp, endp+1);
|
|---|
| 1317 |
|
|---|
| 1318 | result = strlen (expansion);
|
|---|
| 1319 | free (expansion);
|
|---|
| 1320 | }
|
|---|
| 1321 |
|
|---|
| 1322 | /* If the result is true (1) we want to eval the first argument, and if
|
|---|
| 1323 | it's false (0) we want to eval the second. If the argument doesn't
|
|---|
| 1324 | exist we do nothing, otherwise expand it and add to the buffer. */
|
|---|
| 1325 |
|
|---|
| 1326 | argv += 1 + !result;
|
|---|
| 1327 |
|
|---|
| 1328 | if (*argv)
|
|---|
| 1329 | {
|
|---|
| 1330 | char *expansion = expand_argument (*argv, NULL);
|
|---|
| 1331 |
|
|---|
| 1332 | o = variable_buffer_output (o, expansion, strlen (expansion));
|
|---|
| 1333 |
|
|---|
| 1334 | free (expansion);
|
|---|
| 1335 | }
|
|---|
| 1336 |
|
|---|
| 1337 | return o;
|
|---|
| 1338 | }
|
|---|
| 1339 |
|
|---|
| 1340 | /*
|
|---|
| 1341 | $(or condition1[,condition2[,condition3[...]]])
|
|---|
| 1342 |
|
|---|
| 1343 | A CONDITION is false iff it evaluates to an empty string. White
|
|---|
| 1344 | space before and after CONDITION are stripped before evaluation.
|
|---|
| 1345 |
|
|---|
| 1346 | CONDITION1 is evaluated. If it's true, then this is the result of
|
|---|
| 1347 | expansion. If it's false, CONDITION2 is evaluated, and so on. If none of
|
|---|
| 1348 | the conditions are true, the expansion is the empty string.
|
|---|
| 1349 |
|
|---|
| 1350 | Once a CONDITION is true no further conditions are evaluated
|
|---|
| 1351 | (short-circuiting).
|
|---|
| 1352 | */
|
|---|
| 1353 |
|
|---|
| 1354 | static char *
|
|---|
| 1355 | func_or (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 1356 | {
|
|---|
| 1357 | for ( ; *argv ; ++argv)
|
|---|
| 1358 | {
|
|---|
| 1359 | const char *begp = *argv;
|
|---|
| 1360 | const char *endp = begp + strlen (*argv) - 1;
|
|---|
| 1361 | char *expansion;
|
|---|
| 1362 | int result = 0;
|
|---|
| 1363 |
|
|---|
| 1364 | /* Find the result of the condition: if it's false keep going. */
|
|---|
| 1365 |
|
|---|
| 1366 | strip_whitespace (&begp, &endp);
|
|---|
| 1367 |
|
|---|
| 1368 | if (begp > endp)
|
|---|
| 1369 | continue;
|
|---|
| 1370 |
|
|---|
| 1371 | expansion = expand_argument (begp, endp+1);
|
|---|
| 1372 | result = strlen (expansion);
|
|---|
| 1373 |
|
|---|
| 1374 | /* If the result is false keep going. */
|
|---|
| 1375 | if (!result)
|
|---|
| 1376 | {
|
|---|
| 1377 | free (expansion);
|
|---|
| 1378 | continue;
|
|---|
| 1379 | }
|
|---|
| 1380 |
|
|---|
| 1381 | /* It's true! Keep this result and return. */
|
|---|
| 1382 | o = variable_buffer_output (o, expansion, result);
|
|---|
| 1383 | free (expansion);
|
|---|
| 1384 | break;
|
|---|
| 1385 | }
|
|---|
| 1386 |
|
|---|
| 1387 | return o;
|
|---|
| 1388 | }
|
|---|
| 1389 |
|
|---|
| 1390 | /*
|
|---|
| 1391 | $(and condition1[,condition2[,condition3[...]]])
|
|---|
| 1392 |
|
|---|
| 1393 | A CONDITION is false iff it evaluates to an empty string. White
|
|---|
| 1394 | space before and after CONDITION are stripped before evaluation.
|
|---|
| 1395 |
|
|---|
| 1396 | CONDITION1 is evaluated. If it's false, then this is the result of
|
|---|
| 1397 | expansion. If it's true, CONDITION2 is evaluated, and so on. If all of
|
|---|
| 1398 | the conditions are true, the expansion is the result of the last condition.
|
|---|
| 1399 |
|
|---|
| 1400 | Once a CONDITION is false no further conditions are evaluated
|
|---|
| 1401 | (short-circuiting).
|
|---|
| 1402 | */
|
|---|
| 1403 |
|
|---|
| 1404 | static char *
|
|---|
| 1405 | func_and (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 1406 | {
|
|---|
| 1407 | char *expansion;
|
|---|
| 1408 | int result;
|
|---|
| 1409 |
|
|---|
| 1410 | while (1)
|
|---|
| 1411 | {
|
|---|
| 1412 | const char *begp = *argv;
|
|---|
| 1413 | const char *endp = begp + strlen (*argv) - 1;
|
|---|
| 1414 |
|
|---|
| 1415 | /* An empty condition is always false. */
|
|---|
| 1416 | strip_whitespace (&begp, &endp);
|
|---|
| 1417 | if (begp > endp)
|
|---|
| 1418 | return o;
|
|---|
| 1419 |
|
|---|
| 1420 | expansion = expand_argument (begp, endp+1);
|
|---|
| 1421 | result = strlen (expansion);
|
|---|
| 1422 |
|
|---|
| 1423 | /* If the result is false, stop here: we're done. */
|
|---|
| 1424 | if (!result)
|
|---|
| 1425 | break;
|
|---|
| 1426 |
|
|---|
| 1427 | /* Otherwise the result is true. If this is the last one, keep this
|
|---|
| 1428 | result and quit. Otherwise go on to the next one! */
|
|---|
| 1429 |
|
|---|
| 1430 | if (*(++argv))
|
|---|
| 1431 | free (expansion);
|
|---|
| 1432 | else
|
|---|
| 1433 | {
|
|---|
| 1434 | o = variable_buffer_output (o, expansion, result);
|
|---|
| 1435 | break;
|
|---|
| 1436 | }
|
|---|
| 1437 | }
|
|---|
| 1438 |
|
|---|
| 1439 | free (expansion);
|
|---|
| 1440 |
|
|---|
| 1441 | return o;
|
|---|
| 1442 | }
|
|---|
| 1443 |
|
|---|
| 1444 | static char *
|
|---|
| 1445 | func_wildcard (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 1446 | {
|
|---|
| 1447 | #ifdef _AMIGA
|
|---|
| 1448 | o = wildcard_expansion (argv[0], o);
|
|---|
| 1449 | #else
|
|---|
| 1450 | char *p = string_glob (argv[0]);
|
|---|
| 1451 | o = variable_buffer_output (o, p, strlen (p));
|
|---|
| 1452 | #endif
|
|---|
| 1453 | return o;
|
|---|
| 1454 | }
|
|---|
| 1455 |
|
|---|
| 1456 | /*
|
|---|
| 1457 | $(eval <makefile string>)
|
|---|
| 1458 |
|
|---|
| 1459 | Always resolves to the empty string.
|
|---|
| 1460 |
|
|---|
| 1461 | Treat the arguments as a segment of makefile, and parse them.
|
|---|
| 1462 | */
|
|---|
| 1463 |
|
|---|
| 1464 | static char *
|
|---|
| 1465 | func_eval (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 1466 | {
|
|---|
| 1467 | char *buf;
|
|---|
| 1468 | unsigned int len;
|
|---|
| 1469 |
|
|---|
| 1470 | /* Eval the buffer. Pop the current variable buffer setting so that the
|
|---|
| 1471 | eval'd code can use its own without conflicting. */
|
|---|
| 1472 |
|
|---|
| 1473 | install_variable_buffer (&buf, &len);
|
|---|
| 1474 |
|
|---|
| 1475 | eval_buffer (argv[0]);
|
|---|
| 1476 |
|
|---|
| 1477 | restore_variable_buffer (buf, len);
|
|---|
| 1478 |
|
|---|
| 1479 | return o;
|
|---|
| 1480 | }
|
|---|
| 1481 |
|
|---|
| 1482 |
|
|---|
| 1483 | static char *
|
|---|
| 1484 | func_value (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 1485 | {
|
|---|
| 1486 | /* Look up the variable. */
|
|---|
| 1487 | struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
|
|---|
| 1488 |
|
|---|
| 1489 | /* Copy its value into the output buffer without expanding it. */
|
|---|
| 1490 | if (v)
|
|---|
| 1491 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
|---|
| 1492 | o = variable_buffer_output (o, v->value,
|
|---|
| 1493 | v->value_length >= 0 ? v->value_length : strlen(v->value));
|
|---|
| 1494 | #else
|
|---|
| 1495 | o = variable_buffer_output (o, v->value, strlen(v->value));
|
|---|
| 1496 | #endif
|
|---|
| 1497 |
|
|---|
| 1498 | return o;
|
|---|
| 1499 | }
|
|---|
| 1500 |
|
|---|
| 1501 | /*
|
|---|
| 1502 | \r is replaced on UNIX as well. Is this desirable?
|
|---|
| 1503 | */
|
|---|
| 1504 | static void
|
|---|
| 1505 | fold_newlines (char *buffer, unsigned int *length)
|
|---|
| 1506 | {
|
|---|
| 1507 | char *dst = buffer;
|
|---|
| 1508 | char *src = buffer;
|
|---|
| 1509 | char *last_nonnl = buffer -1;
|
|---|
| 1510 | src[*length] = 0;
|
|---|
| 1511 | for (; *src != '\0'; ++src)
|
|---|
| 1512 | {
|
|---|
| 1513 | if (src[0] == '\r' && src[1] == '\n')
|
|---|
| 1514 | continue;
|
|---|
| 1515 | if (*src == '\n')
|
|---|
| 1516 | {
|
|---|
| 1517 | *dst++ = ' ';
|
|---|
| 1518 | }
|
|---|
| 1519 | else
|
|---|
| 1520 | {
|
|---|
| 1521 | last_nonnl = dst;
|
|---|
| 1522 | *dst++ = *src;
|
|---|
| 1523 | }
|
|---|
| 1524 | }
|
|---|
| 1525 | *(++last_nonnl) = '\0';
|
|---|
| 1526 | *length = last_nonnl - buffer;
|
|---|
| 1527 | }
|
|---|
| 1528 |
|
|---|
| 1529 |
|
|---|
| 1530 |
|
|---|
| 1531 | int shell_function_pid = 0, shell_function_completed;
|
|---|
| 1532 |
|
|---|
| 1533 |
|
|---|
| 1534 | #ifdef WINDOWS32
|
|---|
| 1535 | /*untested*/
|
|---|
| 1536 |
|
|---|
| 1537 | #include <windows.h>
|
|---|
| 1538 | #include <io.h>
|
|---|
| 1539 | #include "sub_proc.h"
|
|---|
| 1540 |
|
|---|
| 1541 |
|
|---|
| 1542 | void
|
|---|
| 1543 | windows32_openpipe (int *pipedes, int *pid_p, char **command_argv, char **envp)
|
|---|
| 1544 | {
|
|---|
| 1545 | SECURITY_ATTRIBUTES saAttr;
|
|---|
| 1546 | HANDLE hIn;
|
|---|
| 1547 | HANDLE hErr;
|
|---|
| 1548 | HANDLE hChildOutRd;
|
|---|
| 1549 | HANDLE hChildOutWr;
|
|---|
| 1550 | HANDLE hProcess;
|
|---|
| 1551 |
|
|---|
| 1552 |
|
|---|
| 1553 | saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
|
|---|
| 1554 | saAttr.bInheritHandle = TRUE;
|
|---|
| 1555 | saAttr.lpSecurityDescriptor = NULL;
|
|---|
| 1556 |
|
|---|
| 1557 | if (DuplicateHandle (GetCurrentProcess(),
|
|---|
| 1558 | GetStdHandle(STD_INPUT_HANDLE),
|
|---|
| 1559 | GetCurrentProcess(),
|
|---|
| 1560 | &hIn,
|
|---|
| 1561 | 0,
|
|---|
| 1562 | TRUE,
|
|---|
| 1563 | DUPLICATE_SAME_ACCESS) == FALSE) {
|
|---|
| 1564 | fatal (NILF, _("create_child_process: DuplicateHandle(In) failed (e=%ld)\n"),
|
|---|
| 1565 | GetLastError());
|
|---|
| 1566 |
|
|---|
| 1567 | }
|
|---|
| 1568 | if (DuplicateHandle(GetCurrentProcess(),
|
|---|
| 1569 | GetStdHandle(STD_ERROR_HANDLE),
|
|---|
| 1570 | GetCurrentProcess(),
|
|---|
| 1571 | &hErr,
|
|---|
| 1572 | 0,
|
|---|
| 1573 | TRUE,
|
|---|
| 1574 | DUPLICATE_SAME_ACCESS) == FALSE) {
|
|---|
| 1575 | fatal (NILF, _("create_child_process: DuplicateHandle(Err) failed (e=%ld)\n"),
|
|---|
| 1576 | GetLastError());
|
|---|
| 1577 | }
|
|---|
| 1578 |
|
|---|
| 1579 | if (!CreatePipe(&hChildOutRd, &hChildOutWr, &saAttr, 0))
|
|---|
| 1580 | fatal (NILF, _("CreatePipe() failed (e=%ld)\n"), GetLastError());
|
|---|
| 1581 |
|
|---|
| 1582 | hProcess = process_init_fd(hIn, hChildOutWr, hErr);
|
|---|
| 1583 |
|
|---|
| 1584 | if (!hProcess)
|
|---|
| 1585 | fatal (NILF, _("windows32_openpipe (): process_init_fd() failed\n"));
|
|---|
| 1586 |
|
|---|
| 1587 | /* make sure that CreateProcess() has Path it needs */
|
|---|
| 1588 | sync_Path_environment();
|
|---|
| 1589 |
|
|---|
| 1590 | if (!process_begin(hProcess, command_argv, envp, command_argv[0], NULL)) {
|
|---|
| 1591 | /* register process for wait */
|
|---|
| 1592 | process_register(hProcess);
|
|---|
| 1593 |
|
|---|
| 1594 | /* set the pid for returning to caller */
|
|---|
| 1595 | *pid_p = (int) hProcess;
|
|---|
| 1596 |
|
|---|
| 1597 | /* set up to read data from child */
|
|---|
| 1598 | pipedes[0] = _open_osfhandle((long) hChildOutRd, O_RDONLY);
|
|---|
| 1599 |
|
|---|
| 1600 | /* this will be closed almost right away */
|
|---|
| 1601 | pipedes[1] = _open_osfhandle((long) hChildOutWr, O_APPEND);
|
|---|
| 1602 | } else {
|
|---|
| 1603 | /* reap/cleanup the failed process */
|
|---|
| 1604 | process_cleanup(hProcess);
|
|---|
| 1605 |
|
|---|
| 1606 | /* close handles which were duplicated, they weren't used */
|
|---|
| 1607 | CloseHandle(hIn);
|
|---|
| 1608 | CloseHandle(hErr);
|
|---|
| 1609 |
|
|---|
| 1610 | /* close pipe handles, they won't be used */
|
|---|
| 1611 | CloseHandle(hChildOutRd);
|
|---|
| 1612 | CloseHandle(hChildOutWr);
|
|---|
| 1613 |
|
|---|
| 1614 | /* set status for return */
|
|---|
| 1615 | pipedes[0] = pipedes[1] = -1;
|
|---|
| 1616 | *pid_p = -1;
|
|---|
| 1617 | }
|
|---|
| 1618 | }
|
|---|
| 1619 | #endif
|
|---|
| 1620 |
|
|---|
| 1621 |
|
|---|
| 1622 | #ifdef __MSDOS__
|
|---|
| 1623 | FILE *
|
|---|
| 1624 | msdos_openpipe (int* pipedes, int *pidp, char *text)
|
|---|
| 1625 | {
|
|---|
| 1626 | FILE *fpipe=0;
|
|---|
| 1627 | /* MSDOS can't fork, but it has `popen'. */
|
|---|
| 1628 | struct variable *sh = lookup_variable ("SHELL", 5);
|
|---|
| 1629 | int e;
|
|---|
| 1630 | extern int dos_command_running, dos_status;
|
|---|
| 1631 |
|
|---|
| 1632 | /* Make sure not to bother processing an empty line. */
|
|---|
| 1633 | while (isblank ((unsigned char)*text))
|
|---|
| 1634 | ++text;
|
|---|
| 1635 | if (*text == '\0')
|
|---|
| 1636 | return 0;
|
|---|
| 1637 |
|
|---|
| 1638 | if (sh)
|
|---|
| 1639 | {
|
|---|
| 1640 | char buf[PATH_MAX + 7];
|
|---|
| 1641 | /* This makes sure $SHELL value is used by $(shell), even
|
|---|
| 1642 | though the target environment is not passed to it. */
|
|---|
| 1643 | sprintf (buf, "SHELL=%s", sh->value);
|
|---|
| 1644 | putenv (buf);
|
|---|
| 1645 | }
|
|---|
| 1646 |
|
|---|
| 1647 | e = errno;
|
|---|
| 1648 | errno = 0;
|
|---|
| 1649 | dos_command_running = 1;
|
|---|
| 1650 | dos_status = 0;
|
|---|
| 1651 | /* If dos_status becomes non-zero, it means the child process
|
|---|
| 1652 | was interrupted by a signal, like SIGINT or SIGQUIT. See
|
|---|
| 1653 | fatal_error_signal in commands.c. */
|
|---|
| 1654 | fpipe = popen (text, "rt");
|
|---|
| 1655 | dos_command_running = 0;
|
|---|
| 1656 | if (!fpipe || dos_status)
|
|---|
| 1657 | {
|
|---|
| 1658 | pipedes[0] = -1;
|
|---|
| 1659 | *pidp = -1;
|
|---|
| 1660 | if (dos_status)
|
|---|
| 1661 | errno = EINTR;
|
|---|
| 1662 | else if (errno == 0)
|
|---|
| 1663 | errno = ENOMEM;
|
|---|
| 1664 | shell_function_completed = -1;
|
|---|
| 1665 | }
|
|---|
| 1666 | else
|
|---|
| 1667 | {
|
|---|
| 1668 | pipedes[0] = fileno (fpipe);
|
|---|
| 1669 | *pidp = 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
|
|---|
| 1670 | errno = e;
|
|---|
| 1671 | shell_function_completed = 1;
|
|---|
| 1672 | }
|
|---|
| 1673 | return fpipe;
|
|---|
| 1674 | }
|
|---|
| 1675 | #endif
|
|---|
| 1676 |
|
|---|
| 1677 | /*
|
|---|
| 1678 | Do shell spawning, with the naughty bits for different OSes.
|
|---|
| 1679 | */
|
|---|
| 1680 |
|
|---|
| 1681 | #ifdef VMS
|
|---|
| 1682 |
|
|---|
| 1683 | /* VMS can't do $(shell ...) */
|
|---|
| 1684 | #define func_shell 0
|
|---|
| 1685 |
|
|---|
| 1686 | #else
|
|---|
| 1687 | #ifndef _AMIGA
|
|---|
| 1688 | static char *
|
|---|
| 1689 | func_shell (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 1690 | {
|
|---|
| 1691 | char *batch_filename = NULL;
|
|---|
| 1692 |
|
|---|
| 1693 | #ifdef __MSDOS__
|
|---|
| 1694 | FILE *fpipe;
|
|---|
| 1695 | #endif
|
|---|
| 1696 | char **command_argv;
|
|---|
| 1697 | const char *error_prefix;
|
|---|
| 1698 | char **envp;
|
|---|
| 1699 | int pipedes[2];
|
|---|
| 1700 | int pid;
|
|---|
| 1701 |
|
|---|
| 1702 | #ifndef __MSDOS__
|
|---|
| 1703 | /* Construct the argument list. */
|
|---|
| 1704 | command_argv = construct_command_argv (argv[0], NULL, NULL, &batch_filename);
|
|---|
| 1705 | if (command_argv == 0)
|
|---|
| 1706 | return o;
|
|---|
| 1707 | #endif
|
|---|
| 1708 |
|
|---|
| 1709 | /* Using a target environment for `shell' loses in cases like:
|
|---|
| 1710 | export var = $(shell echo foobie)
|
|---|
| 1711 | because target_environment hits a loop trying to expand $(var)
|
|---|
| 1712 | to put it in the environment. This is even more confusing when
|
|---|
| 1713 | var was not explicitly exported, but just appeared in the
|
|---|
| 1714 | calling environment.
|
|---|
| 1715 |
|
|---|
| 1716 | See Savannah bug #10593.
|
|---|
| 1717 |
|
|---|
| 1718 | envp = target_environment (NILF);
|
|---|
| 1719 | */
|
|---|
| 1720 |
|
|---|
| 1721 | envp = environ;
|
|---|
| 1722 |
|
|---|
| 1723 | /* For error messages. */
|
|---|
| 1724 | if (reading_file && reading_file->filenm)
|
|---|
| 1725 | {
|
|---|
| 1726 | char *p = alloca (strlen (reading_file->filenm)+11+4);
|
|---|
| 1727 | sprintf (p, "%s:%lu: ", reading_file->filenm, reading_file->lineno);
|
|---|
| 1728 | error_prefix = p;
|
|---|
| 1729 | }
|
|---|
| 1730 | else
|
|---|
| 1731 | error_prefix = "";
|
|---|
| 1732 |
|
|---|
| 1733 | #if defined(__MSDOS__)
|
|---|
| 1734 | fpipe = msdos_openpipe (pipedes, &pid, argv[0]);
|
|---|
| 1735 | if (pipedes[0] < 0)
|
|---|
| 1736 | {
|
|---|
| 1737 | perror_with_name (error_prefix, "pipe");
|
|---|
| 1738 | return o;
|
|---|
| 1739 | }
|
|---|
| 1740 | #elif defined(WINDOWS32)
|
|---|
| 1741 | windows32_openpipe (pipedes, &pid, command_argv, envp);
|
|---|
| 1742 | if (pipedes[0] < 0)
|
|---|
| 1743 | {
|
|---|
| 1744 | /* open of the pipe failed, mark as failed execution */
|
|---|
| 1745 | shell_function_completed = -1;
|
|---|
| 1746 |
|
|---|
| 1747 | return o;
|
|---|
| 1748 | }
|
|---|
| 1749 | else
|
|---|
| 1750 | #else
|
|---|
| 1751 | if (pipe (pipedes) < 0)
|
|---|
| 1752 | {
|
|---|
| 1753 | perror_with_name (error_prefix, "pipe");
|
|---|
| 1754 | return o;
|
|---|
| 1755 | }
|
|---|
| 1756 |
|
|---|
| 1757 | # ifdef __EMX__
|
|---|
| 1758 | /* close some handles that are unnecessary for the child process */
|
|---|
| 1759 | CLOSE_ON_EXEC(pipedes[1]);
|
|---|
| 1760 | CLOSE_ON_EXEC(pipedes[0]);
|
|---|
| 1761 | /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
|
|---|
| 1762 | pid = child_execute_job (0, pipedes[1], command_argv, envp);
|
|---|
| 1763 | if (pid < 0)
|
|---|
| 1764 | perror_with_name (error_prefix, "spawn");
|
|---|
| 1765 | # else /* ! __EMX__ */
|
|---|
| 1766 | pid = vfork ();
|
|---|
| 1767 | if (pid < 0)
|
|---|
| 1768 | perror_with_name (error_prefix, "fork");
|
|---|
| 1769 | else if (pid == 0)
|
|---|
| 1770 | child_execute_job (0, pipedes[1], command_argv, envp);
|
|---|
| 1771 | else
|
|---|
| 1772 | # endif
|
|---|
| 1773 | #endif
|
|---|
| 1774 | {
|
|---|
| 1775 | /* We are the parent. */
|
|---|
| 1776 | char *buffer;
|
|---|
| 1777 | unsigned int maxlen, i;
|
|---|
| 1778 | int cc;
|
|---|
| 1779 |
|
|---|
| 1780 | /* Record the PID for reap_children. */
|
|---|
| 1781 | shell_function_pid = pid;
|
|---|
| 1782 | #ifndef __MSDOS__
|
|---|
| 1783 | shell_function_completed = 0;
|
|---|
| 1784 |
|
|---|
| 1785 | /* Free the storage only the child needed. */
|
|---|
| 1786 | free (command_argv[0]);
|
|---|
| 1787 | free (command_argv);
|
|---|
| 1788 |
|
|---|
| 1789 | /* Close the write side of the pipe. */
|
|---|
| 1790 | # ifdef _MSC_VER /* Avoid annoying msvcrt when debugging. (bird) */
|
|---|
| 1791 | if (pipedes[1] != -1)
|
|---|
| 1792 | # endif
|
|---|
| 1793 | close (pipedes[1]);
|
|---|
| 1794 | #endif
|
|---|
| 1795 |
|
|---|
| 1796 | /* Set up and read from the pipe. */
|
|---|
| 1797 |
|
|---|
| 1798 | maxlen = 200;
|
|---|
| 1799 | buffer = xmalloc (maxlen + 1);
|
|---|
| 1800 |
|
|---|
| 1801 | /* Read from the pipe until it gets EOF. */
|
|---|
| 1802 | for (i = 0; ; i += cc)
|
|---|
| 1803 | {
|
|---|
| 1804 | if (i == maxlen)
|
|---|
| 1805 | {
|
|---|
| 1806 | maxlen += 512;
|
|---|
| 1807 | buffer = xrealloc (buffer, maxlen + 1);
|
|---|
| 1808 | }
|
|---|
| 1809 |
|
|---|
| 1810 | EINTRLOOP (cc, read (pipedes[0], &buffer[i], maxlen - i));
|
|---|
| 1811 | if (cc <= 0)
|
|---|
| 1812 | break;
|
|---|
| 1813 | }
|
|---|
| 1814 | buffer[i] = '\0';
|
|---|
| 1815 |
|
|---|
| 1816 | /* Close the read side of the pipe. */
|
|---|
| 1817 | #ifdef __MSDOS__
|
|---|
| 1818 | if (fpipe)
|
|---|
| 1819 | (void) pclose (fpipe);
|
|---|
| 1820 | #else
|
|---|
| 1821 | # ifdef _MSC_VER /* Avoid annoying msvcrt when debugging. (bird) */
|
|---|
| 1822 | if (pipedes[0] != -1)
|
|---|
| 1823 | # endif
|
|---|
| 1824 | (void) close (pipedes[0]);
|
|---|
| 1825 | #endif
|
|---|
| 1826 |
|
|---|
| 1827 | /* Loop until child_handler or reap_children() sets
|
|---|
| 1828 | shell_function_completed to the status of our child shell. */
|
|---|
| 1829 | while (shell_function_completed == 0)
|
|---|
| 1830 | reap_children (1, 0);
|
|---|
| 1831 |
|
|---|
| 1832 | if (batch_filename) {
|
|---|
| 1833 | DB (DB_VERBOSE, (_("Cleaning up temporary batch file %s\n"),
|
|---|
| 1834 | batch_filename));
|
|---|
| 1835 | remove (batch_filename);
|
|---|
| 1836 | free (batch_filename);
|
|---|
| 1837 | }
|
|---|
| 1838 | shell_function_pid = 0;
|
|---|
| 1839 |
|
|---|
| 1840 | /* The child_handler function will set shell_function_completed
|
|---|
| 1841 | to 1 when the child dies normally, or to -1 if it
|
|---|
| 1842 | dies with status 127, which is most likely an exec fail. */
|
|---|
| 1843 |
|
|---|
| 1844 | if (shell_function_completed == -1)
|
|---|
| 1845 | {
|
|---|
| 1846 | /* This likely means that the execvp failed, so we should just
|
|---|
| 1847 | write the error message in the pipe from the child. */
|
|---|
| 1848 | fputs (buffer, stderr);
|
|---|
| 1849 | fflush (stderr);
|
|---|
| 1850 | }
|
|---|
| 1851 | else
|
|---|
| 1852 | {
|
|---|
| 1853 | /* The child finished normally. Replace all newlines in its output
|
|---|
| 1854 | with spaces, and put that in the variable output buffer. */
|
|---|
| 1855 | fold_newlines (buffer, &i);
|
|---|
| 1856 | o = variable_buffer_output (o, buffer, i);
|
|---|
| 1857 | }
|
|---|
| 1858 |
|
|---|
| 1859 | free (buffer);
|
|---|
| 1860 | }
|
|---|
| 1861 |
|
|---|
| 1862 | return o;
|
|---|
| 1863 | }
|
|---|
| 1864 |
|
|---|
| 1865 | #else /* _AMIGA */
|
|---|
| 1866 |
|
|---|
| 1867 | /* Do the Amiga version of func_shell. */
|
|---|
| 1868 |
|
|---|
| 1869 | static char *
|
|---|
| 1870 | func_shell (char *o, char **argv, const char *funcname)
|
|---|
| 1871 | {
|
|---|
| 1872 | /* Amiga can't fork nor spawn, but I can start a program with
|
|---|
| 1873 | redirection of my choice. However, this means that we
|
|---|
| 1874 | don't have an opportunity to reopen stdout to trap it. Thus,
|
|---|
| 1875 | we save our own stdout onto a new descriptor and dup a temp
|
|---|
| 1876 | file's descriptor onto our stdout temporarily. After we
|
|---|
| 1877 | spawn the shell program, we dup our own stdout back to the
|
|---|
| 1878 | stdout descriptor. The buffer reading is the same as above,
|
|---|
| 1879 | except that we're now reading from a file. */
|
|---|
| 1880 |
|
|---|
| 1881 | #include <dos/dos.h>
|
|---|
| 1882 | #include <proto/dos.h>
|
|---|
| 1883 |
|
|---|
| 1884 | BPTR child_stdout;
|
|---|
| 1885 | char tmp_output[FILENAME_MAX];
|
|---|
| 1886 | unsigned int maxlen = 200, i;
|
|---|
| 1887 | int cc;
|
|---|
| 1888 | char * buffer, * ptr;
|
|---|
| 1889 | char ** aptr;
|
|---|
| 1890 | int len = 0;
|
|---|
| 1891 | char* batch_filename = NULL;
|
|---|
| 1892 |
|
|---|
| 1893 | /* Construct the argument list. */
|
|---|
| 1894 | command_argv = construct_command_argv (argv[0], (char **) NULL,
|
|---|
| 1895 | (struct file *) 0, &batch_filename);
|
|---|
| 1896 | if (command_argv == 0)
|
|---|
| 1897 | return o;
|
|---|
| 1898 |
|
|---|
| 1899 | /* Note the mktemp() is a security hole, but this only runs on Amiga.
|
|---|
| 1900 | Ideally we would use main.c:open_tmpfile(), but this uses a special
|
|---|
| 1901 | Open(), not fopen(), and I'm not familiar enough with the code to mess
|
|---|
| 1902 | with it. */
|
|---|
| 1903 | strcpy (tmp_output, "t:MakeshXXXXXXXX");
|
|---|
| 1904 | mktemp (tmp_output);
|
|---|
| 1905 | child_stdout = Open (tmp_output, MODE_NEWFILE);
|
|---|
| 1906 |
|
|---|
| 1907 | for (aptr=command_argv; *aptr; aptr++)
|
|---|
| 1908 | len += strlen (*aptr) + 1;
|
|---|
| 1909 |
|
|---|
| 1910 | buffer = xmalloc (len + 1);
|
|---|
| 1911 | ptr = buffer;
|
|---|
| 1912 |
|
|---|
| 1913 | for (aptr=command_argv; *aptr; aptr++)
|
|---|
| 1914 | {
|
|---|
| 1915 | strcpy (ptr, *aptr);
|
|---|
| 1916 | ptr += strlen (ptr) + 1;
|
|---|
| 1917 | *ptr ++ = ' ';
|
|---|
| 1918 | *ptr = 0;
|
|---|
| 1919 | }
|
|---|
| 1920 |
|
|---|
| 1921 | ptr[-1] = '\n';
|
|---|
| 1922 |
|
|---|
| 1923 | Execute (buffer, NULL, child_stdout);
|
|---|
| 1924 | free (buffer);
|
|---|
| 1925 |
|
|---|
| 1926 | Close (child_stdout);
|
|---|
| 1927 |
|
|---|
| 1928 | child_stdout = Open (tmp_output, MODE_OLDFILE);
|
|---|
| 1929 |
|
|---|
| 1930 | buffer = xmalloc (maxlen);
|
|---|
| 1931 | i = 0;
|
|---|
| 1932 | do
|
|---|
| 1933 | {
|
|---|
| 1934 | if (i == maxlen)
|
|---|
| 1935 | {
|
|---|
| 1936 | maxlen += 512;
|
|---|
| 1937 | buffer = xrealloc (buffer, maxlen + 1);
|
|---|
| 1938 | }
|
|---|
| 1939 |
|
|---|
| 1940 | cc = Read (child_stdout, &buffer[i], maxlen - i);
|
|---|
| 1941 | if (cc > 0)
|
|---|
| 1942 | i += cc;
|
|---|
| 1943 | } while (cc > 0);
|
|---|
| 1944 |
|
|---|
| 1945 | Close (child_stdout);
|
|---|
| 1946 |
|
|---|
| 1947 | fold_newlines (buffer, &i);
|
|---|
| 1948 | o = variable_buffer_output (o, buffer, i);
|
|---|
| 1949 | free (buffer);
|
|---|
| 1950 | return o;
|
|---|
| 1951 | }
|
|---|
| 1952 | #endif /* _AMIGA */
|
|---|
| 1953 | #endif /* !VMS */
|
|---|
| 1954 |
|
|---|
| 1955 | #ifdef EXPERIMENTAL
|
|---|
| 1956 |
|
|---|
| 1957 | /*
|
|---|
| 1958 | equality. Return is string-boolean, ie, the empty string is false.
|
|---|
| 1959 | */
|
|---|
| 1960 | static char *
|
|---|
| 1961 | func_eq (char *o, char **argv, const char *funcname)
|
|---|
| 1962 | {
|
|---|
| 1963 | int result = ! strcmp (argv[0], argv[1]);
|
|---|
| 1964 | o = variable_buffer_output (o, result ? "1" : "", result);
|
|---|
| 1965 | return o;
|
|---|
| 1966 | }
|
|---|
| 1967 |
|
|---|
| 1968 |
|
|---|
| 1969 | /*
|
|---|
| 1970 | string-boolean not operator.
|
|---|
| 1971 | */
|
|---|
| 1972 | static char *
|
|---|
| 1973 | func_not (char *o, char **argv, const char *funcname)
|
|---|
| 1974 | {
|
|---|
| 1975 | const char *s = argv[0];
|
|---|
| 1976 | int result = 0;
|
|---|
| 1977 | while (isspace ((unsigned char)*s))
|
|---|
| 1978 | s++;
|
|---|
| 1979 | result = ! (*s);
|
|---|
| 1980 | o = variable_buffer_output (o, result ? "1" : "", result);
|
|---|
| 1981 | return o;
|
|---|
| 1982 | }
|
|---|
| 1983 | #endif
|
|---|
| 1984 | |
|---|
| 1985 |
|
|---|
| 1986 |
|
|---|
| 1987 | /* Return the absolute name of file NAME which does not contain any `.',
|
|---|
| 1988 | `..' components nor any repeated path separators ('/'). */
|
|---|
| 1989 | #ifdef KMK
|
|---|
| 1990 | char *
|
|---|
| 1991 | #else
|
|---|
| 1992 | static char *
|
|---|
| 1993 | #endif
|
|---|
| 1994 | abspath (const char *name, char *apath)
|
|---|
| 1995 | {
|
|---|
| 1996 | char *dest;
|
|---|
| 1997 | const char *start, *end, *apath_limit;
|
|---|
| 1998 |
|
|---|
| 1999 | if (name[0] == '\0' || apath == NULL)
|
|---|
| 2000 | return NULL;
|
|---|
| 2001 |
|
|---|
| 2002 | #ifdef WINDOWS32 /* bird */
|
|---|
| 2003 | dest = w32ify((char *)name, 1);
|
|---|
| 2004 | if (!dest)
|
|---|
| 2005 | return NULL;
|
|---|
| 2006 | {
|
|---|
| 2007 | size_t len = strlen(dest);
|
|---|
| 2008 | memcpy(apath, dest, len);
|
|---|
| 2009 | dest = apath + len;
|
|---|
| 2010 | }
|
|---|
| 2011 |
|
|---|
| 2012 | (void)end; (void)start; (void)apath_limit;
|
|---|
| 2013 |
|
|---|
| 2014 | #elif defined __OS2__ /* bird */
|
|---|
| 2015 | if (_abspath(apath, name, GET_PATH_MAX))
|
|---|
| 2016 | return NULL;
|
|---|
| 2017 | dest = strchr(apath, '\0');
|
|---|
| 2018 |
|
|---|
| 2019 | (void)end; (void)start; (void)apath_limit; (void)dest;
|
|---|
| 2020 |
|
|---|
| 2021 | #else /* !WINDOWS32 && !__OS2__ */
|
|---|
| 2022 | apath_limit = apath + GET_PATH_MAX;
|
|---|
| 2023 |
|
|---|
| 2024 | #ifdef HAVE_DOS_PATHS /* bird added this */
|
|---|
| 2025 | if (isalpha(name[0]) && name[1] == ':')
|
|---|
| 2026 | {
|
|---|
| 2027 | /* drive spec */
|
|---|
| 2028 | apath[0] = toupper(name[0]);
|
|---|
| 2029 | apath[1] = ':';
|
|---|
| 2030 | apath[2] = '/';
|
|---|
| 2031 | name += 2;
|
|---|
| 2032 | }
|
|---|
| 2033 | else
|
|---|
| 2034 | #endif /* HAVE_DOS_PATHS */
|
|---|
| 2035 | if (name[0] != '/')
|
|---|
| 2036 | {
|
|---|
| 2037 | /* It is unlikely we would make it until here but just to make sure. */
|
|---|
| 2038 | if (!starting_directory)
|
|---|
| 2039 | return NULL;
|
|---|
| 2040 |
|
|---|
| 2041 | strcpy (apath, starting_directory);
|
|---|
| 2042 |
|
|---|
| 2043 | dest = strchr (apath, '\0');
|
|---|
| 2044 | }
|
|---|
| 2045 | else
|
|---|
| 2046 | {
|
|---|
| 2047 | apath[0] = '/';
|
|---|
| 2048 | dest = apath + 1;
|
|---|
| 2049 | }
|
|---|
| 2050 |
|
|---|
| 2051 | for (start = end = name; *start != '\0'; start = end)
|
|---|
| 2052 | {
|
|---|
| 2053 | unsigned long len;
|
|---|
| 2054 |
|
|---|
| 2055 | /* Skip sequence of multiple path-separators. */
|
|---|
| 2056 | while (*start == '/')
|
|---|
| 2057 | ++start;
|
|---|
| 2058 |
|
|---|
| 2059 | /* Find end of path component. */
|
|---|
| 2060 | for (end = start; *end != '\0' && *end != '/'; ++end)
|
|---|
| 2061 | ;
|
|---|
| 2062 |
|
|---|
| 2063 | len = end - start;
|
|---|
| 2064 |
|
|---|
| 2065 | if (len == 0)
|
|---|
| 2066 | break;
|
|---|
| 2067 | else if (len == 1 && start[0] == '.')
|
|---|
| 2068 | /* nothing */;
|
|---|
| 2069 | else if (len == 2 && start[0] == '.' && start[1] == '.')
|
|---|
| 2070 | {
|
|---|
| 2071 | /* Back up to previous component, ignore if at root already. */
|
|---|
| 2072 | if (dest > apath + 1)
|
|---|
| 2073 | while ((--dest)[-1] != '/');
|
|---|
| 2074 | }
|
|---|
| 2075 | else
|
|---|
| 2076 | {
|
|---|
| 2077 | if (dest[-1] != '/')
|
|---|
| 2078 | *dest++ = '/';
|
|---|
| 2079 |
|
|---|
| 2080 | if (dest + len >= apath_limit)
|
|---|
| 2081 | return NULL;
|
|---|
| 2082 |
|
|---|
| 2083 | dest = memcpy (dest, start, len);
|
|---|
| 2084 | dest += len;
|
|---|
| 2085 | *dest = '\0';
|
|---|
| 2086 | }
|
|---|
| 2087 | }
|
|---|
| 2088 | #endif /* !WINDOWS32 && !__OS2__ */
|
|---|
| 2089 |
|
|---|
| 2090 | /* Unless it is root strip trailing separator. */
|
|---|
| 2091 | #ifdef HAVE_DOS_PATHS /* bird (is this correct? what about UNC?) */
|
|---|
| 2092 | if (dest > apath + 1 + (apath[0] != '/') && dest[-1] == '/')
|
|---|
| 2093 | #else
|
|---|
| 2094 | if (dest > apath + 1 && dest[-1] == '/')
|
|---|
| 2095 | #endif
|
|---|
| 2096 | --dest;
|
|---|
| 2097 |
|
|---|
| 2098 | *dest = '\0';
|
|---|
| 2099 |
|
|---|
| 2100 | return apath;
|
|---|
| 2101 | }
|
|---|
| 2102 |
|
|---|
| 2103 |
|
|---|
| 2104 | static char *
|
|---|
| 2105 | func_realpath (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 2106 | {
|
|---|
| 2107 | /* Expand the argument. */
|
|---|
| 2108 | const char *p = argv[0];
|
|---|
| 2109 | const char *path = 0;
|
|---|
| 2110 | int doneany = 0;
|
|---|
| 2111 | unsigned int len = 0;
|
|---|
| 2112 | PATH_VAR (in);
|
|---|
| 2113 | PATH_VAR (out);
|
|---|
| 2114 |
|
|---|
| 2115 | while ((path = find_next_token (&p, &len)) != 0)
|
|---|
| 2116 | {
|
|---|
| 2117 | if (len < GET_PATH_MAX)
|
|---|
| 2118 | {
|
|---|
| 2119 | strncpy (in, path, len);
|
|---|
| 2120 | in[len] = '\0';
|
|---|
| 2121 |
|
|---|
| 2122 | if (
|
|---|
| 2123 | #ifdef HAVE_REALPATH
|
|---|
| 2124 | realpath (in, out)
|
|---|
| 2125 | #else
|
|---|
| 2126 | abspath (in, out)
|
|---|
| 2127 | #endif
|
|---|
| 2128 | )
|
|---|
| 2129 | {
|
|---|
| 2130 | o = variable_buffer_output (o, out, strlen (out));
|
|---|
| 2131 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 2132 | doneany = 1;
|
|---|
| 2133 | }
|
|---|
| 2134 | }
|
|---|
| 2135 | }
|
|---|
| 2136 |
|
|---|
| 2137 | /* Kill last space. */
|
|---|
| 2138 | if (doneany)
|
|---|
| 2139 | --o;
|
|---|
| 2140 |
|
|---|
| 2141 | return o;
|
|---|
| 2142 | }
|
|---|
| 2143 |
|
|---|
| 2144 | static char *
|
|---|
| 2145 | func_abspath (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 2146 | {
|
|---|
| 2147 | /* Expand the argument. */
|
|---|
| 2148 | const char *p = argv[0];
|
|---|
| 2149 | const char *path = 0;
|
|---|
| 2150 | int doneany = 0;
|
|---|
| 2151 | unsigned int len = 0;
|
|---|
| 2152 | PATH_VAR (in);
|
|---|
| 2153 | PATH_VAR (out);
|
|---|
| 2154 |
|
|---|
| 2155 | while ((path = find_next_token (&p, &len)) != 0)
|
|---|
| 2156 | {
|
|---|
| 2157 | if (len < GET_PATH_MAX)
|
|---|
| 2158 | {
|
|---|
| 2159 | strncpy (in, path, len);
|
|---|
| 2160 | in[len] = '\0';
|
|---|
| 2161 |
|
|---|
| 2162 | if (abspath (in, out))
|
|---|
| 2163 | {
|
|---|
| 2164 | o = variable_buffer_output (o, out, strlen (out));
|
|---|
| 2165 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 2166 | doneany = 1;
|
|---|
| 2167 | }
|
|---|
| 2168 | }
|
|---|
| 2169 | }
|
|---|
| 2170 |
|
|---|
| 2171 | /* Kill last space. */
|
|---|
| 2172 | if (doneany)
|
|---|
| 2173 | --o;
|
|---|
| 2174 |
|
|---|
| 2175 | return o;
|
|---|
| 2176 | }
|
|---|
| 2177 |
|
|---|
| 2178 | #ifdef CONFIG_WITH_ABSPATHEX
|
|---|
| 2179 | /* same as abspath except that the current path is given as the 2nd argument. */
|
|---|
| 2180 | static char *
|
|---|
| 2181 | func_abspathex (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 2182 | {
|
|---|
| 2183 | /* Expand the argument. */
|
|---|
| 2184 | const char *p = argv[0];
|
|---|
| 2185 | char *cwd = argv[1];
|
|---|
| 2186 | unsigned int cwd_len = ~0U;
|
|---|
| 2187 | char *path = 0;
|
|---|
| 2188 | int doneany = 0;
|
|---|
| 2189 | unsigned int len = 0;
|
|---|
| 2190 | PATH_VAR (in);
|
|---|
| 2191 | PATH_VAR (out);
|
|---|
| 2192 |
|
|---|
| 2193 | while ((path = find_next_token (&p, &len)) != 0)
|
|---|
| 2194 | {
|
|---|
| 2195 | if (len < GET_PATH_MAX)
|
|---|
| 2196 | {
|
|---|
| 2197 | #ifdef HAVE_DOS_PATHS
|
|---|
| 2198 | if (path[0] != '/' && path[0] != '\\' && (len < 2 || path[1] != ':') && cwd)
|
|---|
| 2199 | #else
|
|---|
| 2200 | if (path[0] != '/' && cwd)
|
|---|
| 2201 | #endif
|
|---|
| 2202 | {
|
|---|
| 2203 | /* relative path, prefix with cwd. */
|
|---|
| 2204 | if (cwd_len == ~0U)
|
|---|
| 2205 | cwd_len = strlen (cwd);
|
|---|
| 2206 | if (cwd_len + len + 1 >= GET_PATH_MAX)
|
|---|
| 2207 | continue;
|
|---|
| 2208 | memcpy (in, cwd, cwd_len);
|
|---|
| 2209 | in[cwd_len] = '/';
|
|---|
| 2210 | memcpy (in + cwd_len + 1, path, len);
|
|---|
| 2211 | in[cwd_len + len + 1] = '\0';
|
|---|
| 2212 | }
|
|---|
| 2213 | else
|
|---|
| 2214 | {
|
|---|
| 2215 | /* absolute path pass it as-is. */
|
|---|
| 2216 | memcpy (in, path, len);
|
|---|
| 2217 | in[len] = '\0';
|
|---|
| 2218 | }
|
|---|
| 2219 |
|
|---|
| 2220 | if (abspath (in, out))
|
|---|
| 2221 | {
|
|---|
| 2222 | o = variable_buffer_output (o, out, strlen (out));
|
|---|
| 2223 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 2224 | doneany = 1;
|
|---|
| 2225 | }
|
|---|
| 2226 | }
|
|---|
| 2227 | }
|
|---|
| 2228 |
|
|---|
| 2229 | /* Kill last space. */
|
|---|
| 2230 | if (doneany)
|
|---|
| 2231 | --o;
|
|---|
| 2232 |
|
|---|
| 2233 | return o;
|
|---|
| 2234 | }
|
|---|
| 2235 | #endif
|
|---|
| 2236 |
|
|---|
| 2237 | #ifdef CONFIG_WITH_XARGS
|
|---|
| 2238 | /* Create one or more command lines avoiding the max argument
|
|---|
| 2239 | lenght restriction of the host OS.
|
|---|
| 2240 |
|
|---|
| 2241 | The last argument is the list of arguments that the normal
|
|---|
| 2242 | xargs command would be fed from stdin.
|
|---|
| 2243 |
|
|---|
| 2244 | The first argument is initial command and it's arguments.
|
|---|
| 2245 |
|
|---|
| 2246 | If there are three or more arguments, the 2nd argument is
|
|---|
| 2247 | the command and arguments to be used on subsequent
|
|---|
| 2248 | command lines. Defaults to the initial command.
|
|---|
| 2249 |
|
|---|
| 2250 | If there are four or more arguments, the 3rd argument is
|
|---|
| 2251 | the command to be used at the final command line. Defaults
|
|---|
| 2252 | to the sub sequent or initial command .
|
|---|
| 2253 |
|
|---|
| 2254 | A future version of this function may define more arguments
|
|---|
| 2255 | and therefor anyone specifying six or more arguments will
|
|---|
| 2256 | cause fatal errors.
|
|---|
| 2257 |
|
|---|
| 2258 | Typical usage is:
|
|---|
| 2259 | $(xargs ar cas mylib.a,$(objects))
|
|---|
| 2260 | or
|
|---|
| 2261 | $(xargs ar cas mylib.a,ar as mylib.a,$(objects))
|
|---|
| 2262 |
|
|---|
| 2263 | It will then create one or more "ar mylib.a ..." command
|
|---|
| 2264 | lines with proper \n\t separation so it can be used when
|
|---|
| 2265 | writing rules. */
|
|---|
| 2266 | static char *
|
|---|
| 2267 | func_xargs (char *o, char **argv, const char *funcname)
|
|---|
| 2268 | {
|
|---|
| 2269 | int argc;
|
|---|
| 2270 | const char *initial_cmd;
|
|---|
| 2271 | size_t initial_cmd_len;
|
|---|
| 2272 | const char *subsequent_cmd;
|
|---|
| 2273 | size_t subsequent_cmd_len;
|
|---|
| 2274 | const char *final_cmd;
|
|---|
| 2275 | size_t final_cmd_len;
|
|---|
| 2276 | const char *args;
|
|---|
| 2277 | size_t max_args;
|
|---|
| 2278 | int i;
|
|---|
| 2279 |
|
|---|
| 2280 | #ifdef ARG_MAX
|
|---|
| 2281 | /* ARG_MAX is a bit unreliable (environment), so drop 25% of the max. */
|
|---|
| 2282 | # define XARGS_MAX (ARG_MAX - (ARG_MAX / 4))
|
|---|
| 2283 | #else /* FIXME: update configure with a command line length test. */
|
|---|
| 2284 | # define XARGS_MAX 10240
|
|---|
| 2285 | #endif
|
|---|
| 2286 |
|
|---|
| 2287 | argc = 0;
|
|---|
| 2288 | while (argv[argc])
|
|---|
| 2289 | argc++;
|
|---|
| 2290 | if (argc > 4)
|
|---|
| 2291 | fatal (NILF, _("Too many arguments for $(xargs)!\n"));
|
|---|
| 2292 |
|
|---|
| 2293 | /* first: the initial / default command.*/
|
|---|
| 2294 | initial_cmd = argv[0];
|
|---|
| 2295 | while (isspace ((unsigned char)*initial_cmd))
|
|---|
| 2296 | initial_cmd++;
|
|---|
| 2297 | max_args = initial_cmd_len = strlen (initial_cmd);
|
|---|
| 2298 |
|
|---|
| 2299 | /* second: the command for the subsequent command lines. defaults to the initial cmd. */
|
|---|
| 2300 | subsequent_cmd = argc > 2 && argv[1][0] != '\0' ? argv[1] : "";
|
|---|
| 2301 | while (isspace ((unsigned char)*subsequent_cmd))
|
|---|
| 2302 | subsequent_cmd++;
|
|---|
| 2303 | if (*subsequent_cmd)
|
|---|
| 2304 | {
|
|---|
| 2305 | subsequent_cmd_len = strlen (subsequent_cmd);
|
|---|
| 2306 | if (subsequent_cmd_len > max_args)
|
|---|
| 2307 | max_args = subsequent_cmd_len;
|
|---|
| 2308 | }
|
|---|
| 2309 | else
|
|---|
| 2310 | {
|
|---|
| 2311 | subsequent_cmd = initial_cmd;
|
|---|
| 2312 | subsequent_cmd_len = initial_cmd_len;
|
|---|
| 2313 | }
|
|---|
| 2314 |
|
|---|
| 2315 | /* third: the final command. defaults to the subseq cmd. */
|
|---|
| 2316 | final_cmd = argc > 3 && argv[2][0] != '\0' ? argv[2] : "";
|
|---|
| 2317 | while (isspace ((unsigned char)*final_cmd))
|
|---|
| 2318 | final_cmd++;
|
|---|
| 2319 | if (*final_cmd)
|
|---|
| 2320 | {
|
|---|
| 2321 | final_cmd_len = strlen (final_cmd);
|
|---|
| 2322 | if (final_cmd_len > max_args)
|
|---|
| 2323 | max_args = final_cmd_len;
|
|---|
| 2324 | }
|
|---|
| 2325 | else
|
|---|
| 2326 | {
|
|---|
| 2327 | final_cmd = subsequent_cmd;
|
|---|
| 2328 | final_cmd_len = subsequent_cmd_len;
|
|---|
| 2329 | }
|
|---|
| 2330 |
|
|---|
| 2331 | /* last: the arguments to split up into sensible portions. */
|
|---|
| 2332 | args = argv[argc - 1];
|
|---|
| 2333 |
|
|---|
| 2334 | /* calc the max argument length. */
|
|---|
| 2335 | if (XARGS_MAX <= max_args + 2)
|
|---|
| 2336 | fatal (NILF, _("$(xargs): the commands are longer than the max exec argument length. (%lu <= %lu)\n"),
|
|---|
| 2337 | (unsigned long)XARGS_MAX, (unsigned long)max_args + 2);
|
|---|
| 2338 | max_args = XARGS_MAX - max_args - 1;
|
|---|
| 2339 |
|
|---|
| 2340 | /* generate the commands. */
|
|---|
| 2341 | i = 0;
|
|---|
| 2342 | for (i = 0; ; i++)
|
|---|
| 2343 | {
|
|---|
| 2344 | unsigned int len;
|
|---|
| 2345 | const char *iterator = args;
|
|---|
| 2346 | const char *end = args;
|
|---|
| 2347 | const char *cur;
|
|---|
| 2348 | const char *tmp;
|
|---|
| 2349 |
|
|---|
| 2350 | /* scan the arguments till we reach the end or the max length. */
|
|---|
| 2351 | while ((cur = find_next_token(&iterator, &len))
|
|---|
| 2352 | && (size_t)((cur + len) - args) < max_args)
|
|---|
| 2353 | end = cur + len;
|
|---|
| 2354 | if (cur && end == args)
|
|---|
| 2355 | fatal (NILF, _("$(xargs): command + one single arg is too much. giving up.\n"));
|
|---|
| 2356 |
|
|---|
| 2357 | /* emit the command. */
|
|---|
| 2358 | if (i == 0)
|
|---|
| 2359 | {
|
|---|
| 2360 | o = variable_buffer_output (o, (char *)initial_cmd, initial_cmd_len);
|
|---|
| 2361 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 2362 | }
|
|---|
| 2363 | else if (cur)
|
|---|
| 2364 | {
|
|---|
| 2365 | o = variable_buffer_output (o, "\n\t", 2);
|
|---|
| 2366 | o = variable_buffer_output (o, (char *)subsequent_cmd, subsequent_cmd_len);
|
|---|
| 2367 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 2368 | }
|
|---|
| 2369 | else
|
|---|
| 2370 | {
|
|---|
| 2371 | o = variable_buffer_output (o, "\n\t", 2);
|
|---|
| 2372 | o = variable_buffer_output (o, (char *)final_cmd, final_cmd_len);
|
|---|
| 2373 | o = variable_buffer_output (o, " ", 1);
|
|---|
| 2374 | }
|
|---|
| 2375 |
|
|---|
| 2376 | tmp = end;
|
|---|
| 2377 | while (tmp > args && isspace ((unsigned char)tmp[-1])) /* drop trailing spaces. */
|
|---|
| 2378 | tmp--;
|
|---|
| 2379 | o = variable_buffer_output (o, (char *)args, tmp - args);
|
|---|
| 2380 |
|
|---|
| 2381 |
|
|---|
| 2382 | /* next */
|
|---|
| 2383 | if (!cur)
|
|---|
| 2384 | break;
|
|---|
| 2385 | args = end;
|
|---|
| 2386 | while (isspace ((unsigned char)*args))
|
|---|
| 2387 | args++;
|
|---|
| 2388 | }
|
|---|
| 2389 |
|
|---|
| 2390 | return o;
|
|---|
| 2391 | }
|
|---|
| 2392 | #endif
|
|---|
| 2393 |
|
|---|
| 2394 | #ifdef CONFIG_WITH_TOUPPER_TOLOWER
|
|---|
| 2395 | static char *
|
|---|
| 2396 | func_toupper_tolower (char *o, char **argv, const char *funcname)
|
|---|
| 2397 | {
|
|---|
| 2398 | /* Expand the argument. */
|
|---|
| 2399 | const char *p = argv[0];
|
|---|
| 2400 | while (*p)
|
|---|
| 2401 | {
|
|---|
| 2402 | /* convert to temporary buffer */
|
|---|
| 2403 | char tmp[256];
|
|---|
| 2404 | unsigned int i;
|
|---|
| 2405 | if (!strcmp(funcname, "toupper"))
|
|---|
| 2406 | for (i = 0; i < sizeof(tmp) && *p; i++, p++)
|
|---|
| 2407 | tmp[i] = toupper(*p);
|
|---|
| 2408 | else
|
|---|
| 2409 | for (i = 0; i < sizeof(tmp) && *p; i++, p++)
|
|---|
| 2410 | tmp[i] = tolower(*p);
|
|---|
| 2411 | o = variable_buffer_output (o, tmp, i);
|
|---|
| 2412 | }
|
|---|
| 2413 |
|
|---|
| 2414 | return o;
|
|---|
| 2415 | }
|
|---|
| 2416 | #endif /* CONFIG_WITH_TOUPPER_TOLOWER */
|
|---|
| 2417 |
|
|---|
| 2418 | #if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
|
|---|
| 2419 |
|
|---|
| 2420 | /* Strip leading spaces and other things off a command. */
|
|---|
| 2421 | static const char *
|
|---|
| 2422 | comp_cmds_strip_leading (const char *s, const char *e)
|
|---|
| 2423 | {
|
|---|
| 2424 | while (s < e)
|
|---|
| 2425 | {
|
|---|
| 2426 | const char ch = *s;
|
|---|
| 2427 | if (!isblank (ch)
|
|---|
| 2428 | && ch != '@'
|
|---|
| 2429 | && ch != '+'
|
|---|
| 2430 | && ch != '-')
|
|---|
| 2431 | break;
|
|---|
| 2432 | s++;
|
|---|
| 2433 | }
|
|---|
| 2434 | return s;
|
|---|
| 2435 | }
|
|---|
| 2436 |
|
|---|
| 2437 | /* Worker for func_comp_vars() which is called if the comparision failed.
|
|---|
| 2438 | It will do the slow command by command comparision of the commands
|
|---|
| 2439 | when there invoked as comp-cmds. */
|
|---|
| 2440 | static char *
|
|---|
| 2441 | comp_vars_ne (char *o, const char *s1, const char *e1, const char *s2, const char *e2,
|
|---|
| 2442 | char *ne_retval, const char *funcname)
|
|---|
| 2443 | {
|
|---|
| 2444 | /* give up at once if not comp-cmds. */
|
|---|
| 2445 | if (strcmp (funcname, "comp-cmds") != 0)
|
|---|
| 2446 | o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
|
|---|
| 2447 | else
|
|---|
| 2448 | {
|
|---|
| 2449 | const char * const s1_start = s1;
|
|---|
| 2450 | int new_cmd = 1;
|
|---|
| 2451 | int diff;
|
|---|
| 2452 | for (;;)
|
|---|
| 2453 | {
|
|---|
| 2454 | /* if it's a new command, strip leading stuff. */
|
|---|
| 2455 | if (new_cmd)
|
|---|
| 2456 | {
|
|---|
| 2457 | s1 = comp_cmds_strip_leading (s1, e1);
|
|---|
| 2458 | s2 = comp_cmds_strip_leading (s2, e2);
|
|---|
| 2459 | new_cmd = 0;
|
|---|
| 2460 | }
|
|---|
| 2461 | if (s1 >= e1 || s2 >= e2)
|
|---|
| 2462 | break;
|
|---|
| 2463 |
|
|---|
| 2464 | /*
|
|---|
| 2465 | * Inner compare loop which compares one line.
|
|---|
| 2466 | * FIXME: parse quoting!
|
|---|
| 2467 | */
|
|---|
| 2468 | for (;;)
|
|---|
| 2469 | {
|
|---|
| 2470 | const char ch1 = *s1;
|
|---|
| 2471 | const char ch2 = *s2;
|
|---|
| 2472 | diff = ch1 - ch2;
|
|---|
| 2473 | if (diff)
|
|---|
| 2474 | break;
|
|---|
| 2475 | if (ch1 == '\n')
|
|---|
| 2476 | break;
|
|---|
| 2477 | assert (ch1 != '\r');
|
|---|
| 2478 |
|
|---|
| 2479 | /* next */
|
|---|
| 2480 | s1++;
|
|---|
| 2481 | s2++;
|
|---|
| 2482 | if (s1 >= e1 || s2 >= e2)
|
|---|
| 2483 | break;
|
|---|
| 2484 | }
|
|---|
| 2485 |
|
|---|
| 2486 | /*
|
|---|
| 2487 | * If we exited because of a difference try to end-of-command
|
|---|
| 2488 | * comparision, e.g. ignore trailing spaces.
|
|---|
| 2489 | */
|
|---|
| 2490 | if (diff)
|
|---|
| 2491 | {
|
|---|
| 2492 | /* strip */
|
|---|
| 2493 | while (s1 < e1 && isblank (*s1))
|
|---|
| 2494 | s1++;
|
|---|
| 2495 | while (s2 < e2 && isblank (*s2))
|
|---|
| 2496 | s2++;
|
|---|
| 2497 | if (s1 >= e1 || s2 >= e2)
|
|---|
| 2498 | break;
|
|---|
| 2499 |
|
|---|
| 2500 | /* compare again and check that it's a newline. */
|
|---|
| 2501 | if (*s2 != '\n' || *s1 != '\n')
|
|---|
| 2502 | break;
|
|---|
| 2503 | }
|
|---|
| 2504 | /* Break out if we exited because of EOS. */
|
|---|
| 2505 | else if (s1 >= e1 || s2 >= e2)
|
|---|
| 2506 | break;
|
|---|
| 2507 |
|
|---|
| 2508 | /*
|
|---|
| 2509 | * Detect the end of command lines.
|
|---|
| 2510 | */
|
|---|
| 2511 | if (*s1 == '\n')
|
|---|
| 2512 | new_cmd = s1 == s1_start || s1[-1] != '\\';
|
|---|
| 2513 | s1++;
|
|---|
| 2514 | s2++;
|
|---|
| 2515 | }
|
|---|
| 2516 |
|
|---|
| 2517 | /*
|
|---|
| 2518 | * Ignore trailing empty lines.
|
|---|
| 2519 | */
|
|---|
| 2520 | if (s1 < e1 || s2 < e2)
|
|---|
| 2521 | {
|
|---|
| 2522 | while (s1 < e1 && (isblank (*s1) || *s1 == '\n'))
|
|---|
| 2523 | if (*s1++ == '\n')
|
|---|
| 2524 | s1 = comp_cmds_strip_leading (s1, e1);
|
|---|
| 2525 | while (s2 < e2 && (isblank (*s2) || *s2 == '\n'))
|
|---|
| 2526 | if (*s2++ == '\n')
|
|---|
| 2527 | s2 = comp_cmds_strip_leading (s2, e2);
|
|---|
| 2528 | }
|
|---|
| 2529 |
|
|---|
| 2530 | /* emit the result. */
|
|---|
| 2531 | if (s1 == e1 && s2 == e2)
|
|---|
| 2532 | o = variable_buffer_output (o, "", 1);
|
|---|
| 2533 | else
|
|---|
| 2534 | o = variable_buffer_output (o, ne_retval, strlen (ne_retval));
|
|---|
| 2535 | }
|
|---|
| 2536 | return o;
|
|---|
| 2537 | }
|
|---|
| 2538 |
|
|---|
| 2539 | /*
|
|---|
| 2540 | $(comp-vars var1,var2,not-equal-return)
|
|---|
| 2541 | or
|
|---|
| 2542 | $(comp-cmds cmd-var1,cmd-var2,not-equal-return)
|
|---|
| 2543 |
|
|---|
| 2544 | Compares the two variables (that's given by name to avoid unnecessary
|
|---|
| 2545 | expanding) and return the string in the third argument if not equal.
|
|---|
| 2546 | If equal, nothing is returned.
|
|---|
| 2547 |
|
|---|
| 2548 | comp-vars will to an exact comparision only stripping leading and
|
|---|
| 2549 | trailing spaces.
|
|---|
| 2550 |
|
|---|
| 2551 | comp-cmds will compare command by command, ignoring not only leading
|
|---|
| 2552 | and trailing spaces on each line but also leading one leading '@' and '-'.
|
|---|
| 2553 | */
|
|---|
| 2554 | static char *
|
|---|
| 2555 | func_comp_vars (char *o, char **argv, const char *funcname)
|
|---|
| 2556 | {
|
|---|
| 2557 | const char *s1, *e1, *x1, *s2, *e2, *x2;
|
|---|
| 2558 | char *a1 = NULL, *a2 = NULL;
|
|---|
| 2559 | size_t l, l1, l2;
|
|---|
| 2560 | struct variable *var1 = lookup_variable (argv[0], strlen (argv[0]));
|
|---|
| 2561 | struct variable *var2 = lookup_variable (argv[1], strlen (argv[1]));
|
|---|
| 2562 |
|
|---|
| 2563 | /* the simple cases */
|
|---|
| 2564 | if (var1 == var2)
|
|---|
| 2565 | return variable_buffer_output (o, "", 1); /* eq */
|
|---|
| 2566 | if (!var1 || !var2)
|
|---|
| 2567 | return variable_buffer_output (o, argv[2], strlen(argv[2]));
|
|---|
| 2568 | if (var1->value == var2->value)
|
|---|
| 2569 | return variable_buffer_output (o, "", 1); /* eq */
|
|---|
| 2570 | if (!var1->recursive && !var2->recursive)
|
|---|
| 2571 | {
|
|---|
| 2572 | if ( var1->value_length == var2->value_length
|
|---|
| 2573 | && !memcmp (var1->value, var2->value, var1->value_length))
|
|---|
| 2574 | return variable_buffer_output (o, "", 1); /* eq */
|
|---|
| 2575 |
|
|---|
| 2576 | /* ignore trailing and leading blanks */
|
|---|
| 2577 | s1 = var1->value;
|
|---|
| 2578 | while (isblank ((unsigned char) *s1))
|
|---|
| 2579 | s1++;
|
|---|
| 2580 | e1 = s1 + var1->value_length;
|
|---|
| 2581 | while (e1 > s1 && isblank ((unsigned char) e1[-1]))
|
|---|
| 2582 | e1--;
|
|---|
| 2583 |
|
|---|
| 2584 | s2 = var2->value;
|
|---|
| 2585 | while (isblank ((unsigned char) *s2))
|
|---|
| 2586 | s2++;
|
|---|
| 2587 | e2 = s2 + var2->value_length;
|
|---|
| 2588 | while (e2 > s2 && isblank ((unsigned char) e2[-1]))
|
|---|
| 2589 | e2--;
|
|---|
| 2590 |
|
|---|
| 2591 | if (e1 - s1 != e2 - s2)
|
|---|
| 2592 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
|---|
| 2593 | l_simple_compare:
|
|---|
| 2594 | if (!memcmp (s1, s2, e1 - s1))
|
|---|
| 2595 | return variable_buffer_output (o, "", 1); /* eq */
|
|---|
| 2596 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
|---|
| 2597 | }
|
|---|
| 2598 |
|
|---|
| 2599 | /* ignore trailing and leading blanks */
|
|---|
| 2600 | s1 = var1->value;
|
|---|
| 2601 | e1 = s1 + var1->value_length;
|
|---|
| 2602 | while (isblank ((unsigned char) *s1))
|
|---|
| 2603 | s1++;
|
|---|
| 2604 | while (e1 > s1 && isblank ((unsigned char) e1[-1]))
|
|---|
| 2605 | e1--;
|
|---|
| 2606 |
|
|---|
| 2607 | s2 = var2->value;
|
|---|
| 2608 | e2 = s2 + var2->value_length;
|
|---|
| 2609 | while (isblank((unsigned char)*s2))
|
|---|
| 2610 | s2++;
|
|---|
| 2611 | while (e2 > s2 && isblank ((unsigned char) e2[-1]))
|
|---|
| 2612 | e2--;
|
|---|
| 2613 |
|
|---|
| 2614 | /* both empty after stripping? */
|
|---|
| 2615 | if (s1 == e1 && s2 == e2)
|
|---|
| 2616 | return variable_buffer_output (o, "", 1); /* eq */
|
|---|
| 2617 |
|
|---|
| 2618 | /* optimist. */
|
|---|
| 2619 | if ( e1 - s1 == e2 - s2
|
|---|
| 2620 | && !memcmp(s1, s2, e1 - s1))
|
|---|
| 2621 | return variable_buffer_output (o, "", 1); /* eq */
|
|---|
| 2622 |
|
|---|
| 2623 | /* compare up to the first '$' or the end. */
|
|---|
| 2624 | x1 = var1->recursive ? memchr (s1, '$', e1 - s1) : NULL;
|
|---|
| 2625 | x2 = var2->recursive ? memchr (s2, '$', e2 - s2) : NULL;
|
|---|
| 2626 | if (!x1 && !x2)
|
|---|
| 2627 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
|---|
| 2628 |
|
|---|
| 2629 | l1 = x1 ? x1 - s1 : e1 - s1;
|
|---|
| 2630 | l2 = x2 ? x2 - s2 : e2 - s2;
|
|---|
| 2631 | l = l1 <= l2 ? l1 : l2;
|
|---|
| 2632 | if (l && memcmp (s1, s2, l))
|
|---|
| 2633 | return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
|---|
| 2634 |
|
|---|
| 2635 | /* one or both buffers now require expanding. */
|
|---|
| 2636 | if (!x1)
|
|---|
| 2637 | s1 += l;
|
|---|
| 2638 | else
|
|---|
| 2639 | {
|
|---|
| 2640 | s1 = a1 = allocated_variable_expand ((char *)s1 + l);
|
|---|
| 2641 | if (!l)
|
|---|
| 2642 | while (isblank ((unsigned char) *s1))
|
|---|
| 2643 | s1++;
|
|---|
| 2644 | e1 = strchr (s1, '\0');
|
|---|
| 2645 | while (e1 > s1 && isblank ((unsigned char) e1[-1]))
|
|---|
| 2646 | e1--;
|
|---|
| 2647 | }
|
|---|
| 2648 |
|
|---|
| 2649 | if (!x2)
|
|---|
| 2650 | s2 += l;
|
|---|
| 2651 | else
|
|---|
| 2652 | {
|
|---|
| 2653 | s2 = a2 = allocated_variable_expand ((char *)s2 + l);
|
|---|
| 2654 | if (!l)
|
|---|
| 2655 | while (isblank ((unsigned char) *s2))
|
|---|
| 2656 | s2++;
|
|---|
| 2657 | e2 = strchr (s2, '\0');
|
|---|
| 2658 | while (e2 > s2 && isblank ((unsigned char) e2[-1]))
|
|---|
| 2659 | e2--;
|
|---|
| 2660 | }
|
|---|
| 2661 |
|
|---|
| 2662 | /* the final compare */
|
|---|
| 2663 | if ( e1 - s1 != e2 - s2
|
|---|
| 2664 | || memcmp (s1, s2, e1 - s1))
|
|---|
| 2665 | o = comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
|
|---|
| 2666 | else
|
|---|
| 2667 | o = variable_buffer_output (o, "", 1); /* eq */
|
|---|
| 2668 | if (a1)
|
|---|
| 2669 | free (a1);
|
|---|
| 2670 | if (a2)
|
|---|
| 2671 | free (a2);
|
|---|
| 2672 | return o;
|
|---|
| 2673 | }
|
|---|
| 2674 | #endif
|
|---|
| 2675 |
|
|---|
| 2676 | #ifdef CONFIG_WITH_DATE
|
|---|
| 2677 | # if defined (_MSC_VER) /* FIXME: !defined (HAVE_STRPTIME) */
|
|---|
| 2678 | char *strptime(const char *s, const char *format, struct tm *tm)
|
|---|
| 2679 | {
|
|---|
| 2680 | return (char *)"strptime is not implemented";
|
|---|
| 2681 | }
|
|---|
| 2682 | # endif
|
|---|
| 2683 | /* Check if the string is all blanks or not. */
|
|---|
| 2684 | static int
|
|---|
| 2685 | all_blanks (const char *s)
|
|---|
| 2686 | {
|
|---|
| 2687 | if (!s)
|
|---|
| 2688 | return 1;
|
|---|
| 2689 | while (isspace ((unsigned char)*s))
|
|---|
| 2690 | s++;
|
|---|
| 2691 | return *s == '\0';
|
|---|
| 2692 | }
|
|---|
| 2693 |
|
|---|
| 2694 | /* The first argument is the strftime format string, a iso
|
|---|
| 2695 | timestamp is the default if nothing is given.
|
|---|
| 2696 |
|
|---|
| 2697 | The second argument is a time value if given. The format
|
|---|
| 2698 | is either the format from the first argument or given as
|
|---|
| 2699 | an additional third argument. */
|
|---|
| 2700 | static char *
|
|---|
| 2701 | func_date (char *o, char **argv, const char *funcname)
|
|---|
| 2702 | {
|
|---|
| 2703 | char *p;
|
|---|
| 2704 | char *buf;
|
|---|
| 2705 | size_t buf_size;
|
|---|
| 2706 | struct tm t;
|
|---|
| 2707 | const char *format;
|
|---|
| 2708 |
|
|---|
| 2709 | /* determin the format - use a single word as the default. */
|
|---|
| 2710 | format = !strcmp (funcname, "date-utc")
|
|---|
| 2711 | ? "%Y-%m-%dT%H:%M:%SZ"
|
|---|
| 2712 | : "%Y-%m-%dT%H:%M:%S";
|
|---|
| 2713 | if (!all_blanks (argv[0]))
|
|---|
| 2714 | format = argv[0];
|
|---|
| 2715 |
|
|---|
| 2716 | /* get the time. */
|
|---|
| 2717 | memset (&t, 0, sizeof(t));
|
|---|
| 2718 | if (argv[0] && !all_blanks (argv[1]))
|
|---|
| 2719 | {
|
|---|
| 2720 | const char *input_format = !all_blanks (argv[2]) ? argv[2] : format;
|
|---|
| 2721 | p = strptime (argv[1], input_format, &t);
|
|---|
| 2722 | if (!p || *p != '\0')
|
|---|
| 2723 | {
|
|---|
| 2724 | error (NILF, _("$(%s): strptime(%s,%s,) -> %s\n"), funcname,
|
|---|
| 2725 | argv[1], input_format, p ? p : "<null>");
|
|---|
| 2726 | return variable_buffer_output (o, "", 1);
|
|---|
| 2727 | }
|
|---|
| 2728 | }
|
|---|
| 2729 | else
|
|---|
| 2730 | {
|
|---|
| 2731 | time_t tval;
|
|---|
| 2732 | time (&tval);
|
|---|
| 2733 | if (!strcmp (funcname, "date-utc"))
|
|---|
| 2734 | t = *gmtime (&tval);
|
|---|
| 2735 | else
|
|---|
| 2736 | t = *localtime (&tval);
|
|---|
| 2737 | }
|
|---|
| 2738 |
|
|---|
| 2739 | /* format it. note that zero isn't necessarily an error, so we'll
|
|---|
| 2740 | have to keep shut about failures. */
|
|---|
| 2741 | buf_size = 64;
|
|---|
| 2742 | buf = xmalloc (buf_size);
|
|---|
| 2743 | while (strftime (buf, buf_size, format, &t) == 0)
|
|---|
| 2744 | {
|
|---|
| 2745 | if (buf_size >= 4096)
|
|---|
| 2746 | {
|
|---|
| 2747 | *buf = '\0';
|
|---|
| 2748 | break;
|
|---|
| 2749 | }
|
|---|
| 2750 | buf = xrealloc (buf, buf_size <<= 1);
|
|---|
| 2751 | }
|
|---|
| 2752 | o = variable_buffer_output (o, buf, strlen (buf));
|
|---|
| 2753 | free (buf);
|
|---|
| 2754 | return o;
|
|---|
| 2755 | }
|
|---|
| 2756 | #endif
|
|---|
| 2757 |
|
|---|
| 2758 | #ifdef CONFIG_WITH_FILE_SIZE
|
|---|
| 2759 | /* Prints the size of the specified file. Only one file is
|
|---|
| 2760 | permitted, notthing is stripped. -1 is returned if stat
|
|---|
| 2761 | fails. */
|
|---|
| 2762 | static char *
|
|---|
| 2763 | func_file_size (char *o, char **argv, const char *funcname)
|
|---|
| 2764 | {
|
|---|
| 2765 | struct stat st;
|
|---|
| 2766 | if (stat (argv[0], &st))
|
|---|
| 2767 | return variable_buffer_output (o, "-1", 2);
|
|---|
| 2768 | return math_int_to_variable_buffer (o, st.st_size);
|
|---|
| 2769 | }
|
|---|
| 2770 | #endif
|
|---|
| 2771 |
|
|---|
| 2772 | #ifdef CONFIG_WITH_WHICH
|
|---|
| 2773 | /* Checks if the specified file exists an is executable.
|
|---|
| 2774 | On systems employing executable extensions, the name may
|
|---|
| 2775 | be modified to include the extension. */
|
|---|
| 2776 | static int func_which_test_x (char *file)
|
|---|
| 2777 | {
|
|---|
| 2778 | struct stat st;
|
|---|
| 2779 | # if defined(WINDOWS32) || defined(__OS2__)
|
|---|
| 2780 | char *ext;
|
|---|
| 2781 | char *slash;
|
|---|
| 2782 |
|
|---|
| 2783 | /* fix slashes first. */
|
|---|
| 2784 | slash = file;
|
|---|
| 2785 | while ((slash = strchr (slash, '\\')) != NULL)
|
|---|
| 2786 | *slash++ = '/';
|
|---|
| 2787 |
|
|---|
| 2788 | /* straight */
|
|---|
| 2789 | if (stat (file, &st) == 0
|
|---|
| 2790 | && S_ISREG (st.st_mode))
|
|---|
| 2791 | return 1;
|
|---|
| 2792 |
|
|---|
| 2793 | /* don't try add an extension if there already is one */
|
|---|
| 2794 | ext = strchr (file, '\0');
|
|---|
| 2795 | if (ext - file >= 4
|
|---|
| 2796 | && ( !stricmp (ext - 4, ".exe")
|
|---|
| 2797 | || !stricmp (ext - 4, ".cmd")
|
|---|
| 2798 | || !stricmp (ext - 4, ".bat")
|
|---|
| 2799 | || !stricmp (ext - 4, ".com")))
|
|---|
| 2800 | return 0;
|
|---|
| 2801 |
|
|---|
| 2802 | /* try the extensions. */
|
|---|
| 2803 | strcpy (ext, ".exe");
|
|---|
| 2804 | if (stat (file, &st) == 0
|
|---|
| 2805 | && S_ISREG (st.st_mode))
|
|---|
| 2806 | return 1;
|
|---|
| 2807 |
|
|---|
| 2808 | strcpy (ext, ".cmd");
|
|---|
| 2809 | if (stat (file, &st) == 0
|
|---|
| 2810 | && S_ISREG (st.st_mode))
|
|---|
| 2811 | return 1;
|
|---|
| 2812 |
|
|---|
| 2813 | strcpy (ext, ".bat");
|
|---|
| 2814 | if (stat (file, &st) == 0
|
|---|
| 2815 | && S_ISREG (st.st_mode))
|
|---|
| 2816 | return 1;
|
|---|
| 2817 |
|
|---|
| 2818 | strcpy (ext, ".com");
|
|---|
| 2819 | if (stat (file, &st) == 0
|
|---|
| 2820 | && S_ISREG (st.st_mode))
|
|---|
| 2821 | return 1;
|
|---|
| 2822 |
|
|---|
| 2823 | return 0;
|
|---|
| 2824 |
|
|---|
| 2825 | # else
|
|---|
| 2826 |
|
|---|
| 2827 | return access (file, X_OK) == 0
|
|---|
| 2828 | && stat (file, &st) == 0
|
|---|
| 2829 | && S_ISREG (st.st_mode);
|
|---|
| 2830 | # endif
|
|---|
| 2831 | }
|
|---|
| 2832 |
|
|---|
| 2833 | /* Searches for the specified programs in the PATH and print
|
|---|
| 2834 | their full location if found. Prints nothing if not found. */
|
|---|
| 2835 | static char *
|
|---|
| 2836 | func_which (char *o, char **argv, const char *funcname)
|
|---|
| 2837 | {
|
|---|
| 2838 | const char *path;
|
|---|
| 2839 | struct variable *path_var;
|
|---|
| 2840 | unsigned i;
|
|---|
| 2841 | PATH_VAR (buf);
|
|---|
| 2842 |
|
|---|
| 2843 | path_var = lookup_variable ("PATH", 4);
|
|---|
| 2844 | if (path_var)
|
|---|
| 2845 | path = path_var->value;
|
|---|
| 2846 | else
|
|---|
| 2847 | path = ".";
|
|---|
| 2848 |
|
|---|
| 2849 | /* iterate input */
|
|---|
| 2850 | for (i = 0; argv[i]; i++)
|
|---|
| 2851 | {
|
|---|
| 2852 | unsigned int len;
|
|---|
| 2853 | const char *iterator = argv[i];
|
|---|
| 2854 | char *cur;
|
|---|
| 2855 |
|
|---|
| 2856 | while ((cur = find_next_token (&iterator, &len)))
|
|---|
| 2857 | {
|
|---|
| 2858 | /* if there is a separator, don't walk the path. */
|
|---|
| 2859 | if (memchr (cur, '/', len)
|
|---|
| 2860 | #ifdef HAVE_DOS_PATHS
|
|---|
| 2861 | || memchr (cur, '\\', len)
|
|---|
| 2862 | || memchr (cur, ':', len)
|
|---|
| 2863 | #endif
|
|---|
| 2864 | )
|
|---|
| 2865 | {
|
|---|
| 2866 | if (len + 1 + 4 < GET_PATH_MAX) /* +4 for .exe */
|
|---|
| 2867 | {
|
|---|
| 2868 | memcpy (buf, cur, len);
|
|---|
| 2869 | buf[len] = '\0';
|
|---|
| 2870 | if (func_which_test_x (buf))
|
|---|
| 2871 | o = variable_buffer_output (o, buf, strlen (buf));
|
|---|
| 2872 | }
|
|---|
| 2873 | }
|
|---|
| 2874 | else
|
|---|
| 2875 | {
|
|---|
| 2876 | const char *comp = path;
|
|---|
| 2877 | for (;;)
|
|---|
| 2878 | {
|
|---|
| 2879 | const char *src = comp;
|
|---|
| 2880 | const char *end = strchr (comp, PATH_SEPARATOR_CHAR);
|
|---|
| 2881 | size_t comp_len = end ? end - comp : strlen (comp);
|
|---|
| 2882 | if (!comp_len)
|
|---|
| 2883 | {
|
|---|
| 2884 | comp_len = 1;
|
|---|
| 2885 | src = ".";
|
|---|
| 2886 | }
|
|---|
| 2887 | if (len + comp_len + 2 + 4 < GET_PATH_MAX) /* +4 for .exe */
|
|---|
| 2888 | {
|
|---|
| 2889 | memcpy (buf, comp, comp_len);
|
|---|
| 2890 | buf [comp_len] = '/';
|
|---|
| 2891 | memcpy (&buf[comp_len + 1], cur, len);
|
|---|
| 2892 | buf[comp_len + 1 + len] = '\0';
|
|---|
| 2893 |
|
|---|
| 2894 | if (func_which_test_x (buf))
|
|---|
| 2895 | {
|
|---|
| 2896 | o = variable_buffer_output (o, buf, strlen (buf));
|
|---|
| 2897 | break;
|
|---|
| 2898 | }
|
|---|
| 2899 | }
|
|---|
| 2900 |
|
|---|
| 2901 | /* next */
|
|---|
| 2902 | if (!end)
|
|---|
| 2903 | break;
|
|---|
| 2904 | comp = end + 1;
|
|---|
| 2905 | }
|
|---|
| 2906 | }
|
|---|
| 2907 | }
|
|---|
| 2908 | }
|
|---|
| 2909 |
|
|---|
| 2910 | return variable_buffer_output (o, "", 1);
|
|---|
| 2911 | }
|
|---|
| 2912 | #endif
|
|---|
| 2913 |
|
|---|
| 2914 | #ifdef CONFIG_WITH_STACK
|
|---|
| 2915 |
|
|---|
| 2916 | /* Push an item (string without spaces). */
|
|---|
| 2917 | static char *
|
|---|
| 2918 | func_stack_push (char *o, char **argv, const char *funcname)
|
|---|
| 2919 | {
|
|---|
| 2920 | do_variable_definition(NILF, argv[0], argv[1], o_file, f_append, 0 /* !target_var */);
|
|---|
| 2921 | return o;
|
|---|
| 2922 | }
|
|---|
| 2923 |
|
|---|
| 2924 | /* Pops an item off the stack / get the top stack element.
|
|---|
| 2925 | (This is what's tricky to do in pure GNU make syntax.) */
|
|---|
| 2926 | static char *
|
|---|
| 2927 | func_stack_pop_top (char *o, char **argv, const char *funcname)
|
|---|
| 2928 | {
|
|---|
| 2929 | struct variable *stack_var;
|
|---|
| 2930 | const char *stack = argv[0];
|
|---|
| 2931 | const int return_item = argv[0][sizeof("stack-pop") - 1] == '\0';
|
|---|
| 2932 |
|
|---|
| 2933 | stack_var = lookup_variable (stack, strlen (stack) );
|
|---|
| 2934 | if (stack_var)
|
|---|
| 2935 | {
|
|---|
| 2936 | unsigned int len;
|
|---|
| 2937 | const char *iterator = stack_var->value;
|
|---|
| 2938 | char *lastitem = NULL;
|
|---|
| 2939 | char *cur;
|
|---|
| 2940 |
|
|---|
| 2941 | while ((cur = find_next_token (&iterator, &len)))
|
|---|
| 2942 | lastitem = cur;
|
|---|
| 2943 |
|
|---|
| 2944 | if (lastitem != NULL)
|
|---|
| 2945 | {
|
|---|
| 2946 | if (strcmp (funcname, "stack-popv") != 0)
|
|---|
| 2947 | o = variable_buffer_output (o, lastitem, len);
|
|---|
| 2948 | if (strcmp (funcname, "stack-top") != 0)
|
|---|
| 2949 | {
|
|---|
| 2950 | *lastitem = '\0';
|
|---|
| 2951 | while (lastitem > stack_var->value && isspace (lastitem[-1]))
|
|---|
| 2952 | *--lastitem = '\0';
|
|---|
| 2953 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
|---|
| 2954 | stack_var->value_length = lastitem - stack_var->value;
|
|---|
| 2955 | #endif
|
|---|
| 2956 | }
|
|---|
| 2957 | }
|
|---|
| 2958 | }
|
|---|
| 2959 | return o;
|
|---|
| 2960 | }
|
|---|
| 2961 | #endif /* CONFIG_WITH_STACK */
|
|---|
| 2962 |
|
|---|
| 2963 | #if defined (CONFIG_WITH_MATH) || defined (CONFIG_WITH_NANOTS) || defined (CONFIG_WITH_FILE_SIZE)
|
|---|
| 2964 | /* outputs the number (as a string) into the variable buffer. */
|
|---|
| 2965 | static char *
|
|---|
| 2966 | math_int_to_variable_buffer (char *o, math_int num)
|
|---|
| 2967 | {
|
|---|
| 2968 | static const char xdigits[17] = "0123456789abcdef";
|
|---|
| 2969 | int negative;
|
|---|
| 2970 | char strbuf[24]; /* 16 hex + 2 prefix + sign + term => 20
|
|---|
| 2971 | or 20 dec + sign + term => 22 */
|
|---|
| 2972 | char *str = &strbuf[sizeof (strbuf) - 1];
|
|---|
| 2973 |
|
|---|
| 2974 | negative = num < 0;
|
|---|
| 2975 | if (negative)
|
|---|
| 2976 | num = -num;
|
|---|
| 2977 |
|
|---|
| 2978 | *str = '\0';
|
|---|
| 2979 |
|
|---|
| 2980 | do
|
|---|
| 2981 | {
|
|---|
| 2982 | #ifdef HEX_MATH_NUMBERS
|
|---|
| 2983 | *--str = xdigits[num & 0xf];
|
|---|
| 2984 | num >>= 4;
|
|---|
| 2985 | #else
|
|---|
| 2986 | *--str = xdigits[num % 10];
|
|---|
| 2987 | num /= 10;
|
|---|
| 2988 | #endif
|
|---|
| 2989 | }
|
|---|
| 2990 | while (num);
|
|---|
| 2991 |
|
|---|
| 2992 | #ifdef HEX_MATH_NUMBERS
|
|---|
| 2993 | *--str = 'x';
|
|---|
| 2994 | *--str = '0';
|
|---|
| 2995 | #endif
|
|---|
| 2996 |
|
|---|
| 2997 | if (negative)
|
|---|
| 2998 | *--str = '-';
|
|---|
| 2999 |
|
|---|
| 3000 | return variable_buffer_output (o, str, &strbuf[sizeof (strbuf) - 1] - str);
|
|---|
| 3001 | }
|
|---|
| 3002 | #endif /* CONFIG_WITH_MATH || CONFIG_WITH_NANOTS */
|
|---|
| 3003 |
|
|---|
| 3004 | #ifdef CONFIG_WITH_MATH
|
|---|
| 3005 |
|
|---|
| 3006 | /* Converts a string to an integer, causes an error if the format is invalid. */
|
|---|
| 3007 | static math_int
|
|---|
| 3008 | math_int_from_string (const char *str)
|
|---|
| 3009 | {
|
|---|
| 3010 | const char *start;
|
|---|
| 3011 | unsigned base = 0;
|
|---|
| 3012 | int negative = 0;
|
|---|
| 3013 | math_int num = 0;
|
|---|
| 3014 |
|
|---|
| 3015 | /* strip spaces */
|
|---|
| 3016 | while (isspace (*str))
|
|---|
| 3017 | str++;
|
|---|
| 3018 | if (!*str)
|
|---|
| 3019 | {
|
|---|
| 3020 | error (NILF, _("bad number: empty\n"));
|
|---|
| 3021 | return 0;
|
|---|
| 3022 | }
|
|---|
| 3023 | start = str;
|
|---|
| 3024 |
|
|---|
| 3025 | /* check for +/- */
|
|---|
| 3026 | while (*str == '+' || *str == '-' || isspace (*str))
|
|---|
| 3027 | if (*str++ == '-')
|
|---|
| 3028 | negative = !negative;
|
|---|
| 3029 |
|
|---|
| 3030 | /* check for prefix - we do not accept octal numbers, sorry. */
|
|---|
| 3031 | if (*str == '0' && (str[1] == 'x' || str[1] == 'X'))
|
|---|
| 3032 | {
|
|---|
| 3033 | base = 16;
|
|---|
| 3034 | str += 2;
|
|---|
| 3035 | }
|
|---|
| 3036 | else
|
|---|
| 3037 | {
|
|---|
| 3038 | /* look for a hex digit, if not found treat it as decimal */
|
|---|
| 3039 | const char *p2 = str;
|
|---|
| 3040 | for ( ; *p2; p2++)
|
|---|
| 3041 | if (isxdigit (*p2) && !isdigit (*p2) && isascii (*p2) )
|
|---|
| 3042 | {
|
|---|
| 3043 | base = 16;
|
|---|
| 3044 | break;
|
|---|
| 3045 | }
|
|---|
| 3046 | if (base == 0)
|
|---|
| 3047 | base = 10;
|
|---|
| 3048 | }
|
|---|
| 3049 |
|
|---|
| 3050 | /* must have at least one digit! */
|
|---|
| 3051 | if ( !isascii (*str)
|
|---|
| 3052 | || !(base == 16 ? isxdigit (*str) : isdigit (*str)) )
|
|---|
| 3053 | {
|
|---|
| 3054 | error (NILF, _("bad number: '%s'\n"), start);
|
|---|
| 3055 | return 0;
|
|---|
| 3056 | }
|
|---|
| 3057 |
|
|---|
| 3058 | /* convert it! */
|
|---|
| 3059 | while (*str && !isspace (*str))
|
|---|
| 3060 | {
|
|---|
| 3061 | int ch = *str++;
|
|---|
| 3062 | if (ch >= '0' && ch <= '9')
|
|---|
| 3063 | ch -= '0';
|
|---|
| 3064 | else if (base == 16 && ch >= 'a' && ch <= 'f')
|
|---|
| 3065 | ch -= 'a' - 10;
|
|---|
| 3066 | else if (base == 16 && ch >= 'A' && ch <= 'F')
|
|---|
| 3067 | ch -= 'A' - 10;
|
|---|
| 3068 | else
|
|---|
| 3069 | {
|
|---|
| 3070 | error (NILF, _("bad number: '%s' (base=%d, pos=%d)\n"), start, base, str - start);
|
|---|
| 3071 | return 0;
|
|---|
| 3072 | }
|
|---|
| 3073 | num *= base;
|
|---|
| 3074 | num += ch;
|
|---|
| 3075 | }
|
|---|
| 3076 |
|
|---|
| 3077 | /* check trailing spaces. */
|
|---|
| 3078 | while (isspace (*str))
|
|---|
| 3079 | str++;
|
|---|
| 3080 | if (*str)
|
|---|
| 3081 | {
|
|---|
| 3082 | error (NILF, _("bad number: '%s'\n"), start);
|
|---|
| 3083 | return 0;
|
|---|
| 3084 | }
|
|---|
| 3085 |
|
|---|
| 3086 | return negative ? -num : num;
|
|---|
| 3087 | }
|
|---|
| 3088 |
|
|---|
| 3089 | /* Add two or more integer numbers. */
|
|---|
| 3090 | static char *
|
|---|
| 3091 | func_int_add (char *o, char **argv, const char *funcname)
|
|---|
| 3092 | {
|
|---|
| 3093 | math_int num;
|
|---|
| 3094 | int i;
|
|---|
| 3095 |
|
|---|
| 3096 | num = math_int_from_string (argv[0]);
|
|---|
| 3097 | for (i = 1; argv[i]; i++)
|
|---|
| 3098 | num += math_int_from_string (argv[i]);
|
|---|
| 3099 |
|
|---|
| 3100 | return math_int_to_variable_buffer (o, num);
|
|---|
| 3101 | }
|
|---|
| 3102 |
|
|---|
| 3103 | /* Subtract two or more integer numbers. */
|
|---|
| 3104 | static char *
|
|---|
| 3105 | func_int_sub (char *o, char **argv, const char *funcname)
|
|---|
| 3106 | {
|
|---|
| 3107 | math_int num;
|
|---|
| 3108 | int i;
|
|---|
| 3109 |
|
|---|
| 3110 | num = math_int_from_string (argv[0]);
|
|---|
| 3111 | for (i = 1; argv[i]; i++)
|
|---|
| 3112 | num -= math_int_from_string (argv[i]);
|
|---|
| 3113 |
|
|---|
| 3114 | return math_int_to_variable_buffer (o, num);
|
|---|
| 3115 | }
|
|---|
| 3116 |
|
|---|
| 3117 | /* Multiply two or more integer numbers. */
|
|---|
| 3118 | static char *
|
|---|
| 3119 | func_int_mul (char *o, char **argv, const char *funcname)
|
|---|
| 3120 | {
|
|---|
| 3121 | math_int num;
|
|---|
| 3122 | int i;
|
|---|
| 3123 |
|
|---|
| 3124 | num = math_int_from_string (argv[0]);
|
|---|
| 3125 | for (i = 1; argv[i]; i++)
|
|---|
| 3126 | num *= math_int_from_string (argv[i]);
|
|---|
| 3127 |
|
|---|
| 3128 | return math_int_to_variable_buffer (o, num);
|
|---|
| 3129 | }
|
|---|
| 3130 |
|
|---|
| 3131 | /* Divide an integer number by one or more divisors. */
|
|---|
| 3132 | static char *
|
|---|
| 3133 | func_int_div (char *o, char **argv, const char *funcname)
|
|---|
| 3134 | {
|
|---|
| 3135 | math_int num;
|
|---|
| 3136 | math_int divisor;
|
|---|
| 3137 | int i;
|
|---|
| 3138 |
|
|---|
| 3139 | num = math_int_from_string (argv[0]);
|
|---|
| 3140 | for (i = 1; argv[i]; i++)
|
|---|
| 3141 | {
|
|---|
| 3142 | divisor = math_int_from_string (argv[i]);
|
|---|
| 3143 | if (!divisor)
|
|---|
| 3144 | {
|
|---|
| 3145 | error (NILF, _("divide by zero ('%s')\n"), argv[i]);
|
|---|
| 3146 | return math_int_to_variable_buffer (o, 0);
|
|---|
| 3147 | }
|
|---|
| 3148 | num /= divisor;
|
|---|
| 3149 | }
|
|---|
| 3150 |
|
|---|
| 3151 | return math_int_to_variable_buffer (o, num);
|
|---|
| 3152 | }
|
|---|
| 3153 |
|
|---|
| 3154 |
|
|---|
| 3155 | /* Divide and return the remainder. */
|
|---|
| 3156 | static char *
|
|---|
| 3157 | func_int_mod (char *o, char **argv, const char *funcname)
|
|---|
| 3158 | {
|
|---|
| 3159 | math_int num;
|
|---|
| 3160 | math_int divisor;
|
|---|
| 3161 |
|
|---|
| 3162 | num = math_int_from_string (argv[0]);
|
|---|
| 3163 | divisor = math_int_from_string (argv[1]);
|
|---|
| 3164 | if (!divisor)
|
|---|
| 3165 | {
|
|---|
| 3166 | error (NILF, _("divide by zero ('%s')\n"), argv[1]);
|
|---|
| 3167 | return math_int_to_variable_buffer (o, 0);
|
|---|
| 3168 | }
|
|---|
| 3169 | num %= divisor;
|
|---|
| 3170 |
|
|---|
| 3171 | return math_int_to_variable_buffer (o, num);
|
|---|
| 3172 | }
|
|---|
| 3173 |
|
|---|
| 3174 | /* 2-complement. */
|
|---|
| 3175 | static char *
|
|---|
| 3176 | func_int_not (char *o, char **argv, const char *funcname)
|
|---|
| 3177 | {
|
|---|
| 3178 | math_int num;
|
|---|
| 3179 |
|
|---|
| 3180 | num = math_int_from_string (argv[0]);
|
|---|
| 3181 | num = ~num;
|
|---|
| 3182 |
|
|---|
| 3183 | return math_int_to_variable_buffer (o, num);
|
|---|
| 3184 | }
|
|---|
| 3185 |
|
|---|
| 3186 | /* Bitwise AND (two or more numbers). */
|
|---|
| 3187 | static char *
|
|---|
| 3188 | func_int_and (char *o, char **argv, const char *funcname)
|
|---|
| 3189 | {
|
|---|
| 3190 | math_int num;
|
|---|
| 3191 | int i;
|
|---|
| 3192 |
|
|---|
| 3193 | num = math_int_from_string (argv[0]);
|
|---|
| 3194 | for (i = 1; argv[i]; i++)
|
|---|
| 3195 | num &= math_int_from_string (argv[i]);
|
|---|
| 3196 |
|
|---|
| 3197 | return math_int_to_variable_buffer (o, num);
|
|---|
| 3198 | }
|
|---|
| 3199 |
|
|---|
| 3200 | /* Bitwise OR (two or more numbers). */
|
|---|
| 3201 | static char *
|
|---|
| 3202 | func_int_or (char *o, char **argv, const char *funcname)
|
|---|
| 3203 | {
|
|---|
| 3204 | math_int num;
|
|---|
| 3205 | int i;
|
|---|
| 3206 |
|
|---|
| 3207 | num = math_int_from_string (argv[0]);
|
|---|
| 3208 | for (i = 1; argv[i]; i++)
|
|---|
| 3209 | num |= math_int_from_string (argv[i]);
|
|---|
| 3210 |
|
|---|
| 3211 | return math_int_to_variable_buffer (o, num);
|
|---|
| 3212 | }
|
|---|
| 3213 |
|
|---|
| 3214 | /* Bitwise XOR (two or more numbers). */
|
|---|
| 3215 | static char *
|
|---|
| 3216 | func_int_xor (char *o, char **argv, const char *funcname)
|
|---|
| 3217 | {
|
|---|
| 3218 | math_int num;
|
|---|
| 3219 | int i;
|
|---|
| 3220 |
|
|---|
| 3221 | num = math_int_from_string (argv[0]);
|
|---|
| 3222 | for (i = 1; argv[i]; i++)
|
|---|
| 3223 | num ^= math_int_from_string (argv[i]);
|
|---|
| 3224 |
|
|---|
| 3225 | return math_int_to_variable_buffer (o, num);
|
|---|
| 3226 | }
|
|---|
| 3227 |
|
|---|
| 3228 | /* Compare two integer numbers. Returns make boolean (true="1"; false=""). */
|
|---|
| 3229 | static char *
|
|---|
| 3230 | func_int_cmp (char *o, char **argv, const char *funcname)
|
|---|
| 3231 | {
|
|---|
| 3232 | math_int num1;
|
|---|
| 3233 | math_int num2;
|
|---|
| 3234 | int rc;
|
|---|
| 3235 |
|
|---|
| 3236 | num1 = math_int_from_string (argv[0]);
|
|---|
| 3237 | num2 = math_int_from_string (argv[1]);
|
|---|
| 3238 |
|
|---|
| 3239 | funcname += sizeof ("int-") - 1;
|
|---|
| 3240 | if (!strcmp (funcname, "eq"))
|
|---|
| 3241 | rc = num1 == num2;
|
|---|
| 3242 | else if (!strcmp (funcname, "ne"))
|
|---|
| 3243 | rc = num1 != num2;
|
|---|
| 3244 | else if (!strcmp (funcname, "gt"))
|
|---|
| 3245 | rc = num1 > num2;
|
|---|
| 3246 | else if (!strcmp (funcname, "ge"))
|
|---|
| 3247 | rc = num1 >= num2;
|
|---|
| 3248 | else if (!strcmp (funcname, "lt"))
|
|---|
| 3249 | rc = num1 < num2;
|
|---|
| 3250 | else /*if (!strcmp (funcname, "le"))*/
|
|---|
| 3251 | rc = num1 <= num2;
|
|---|
| 3252 |
|
|---|
| 3253 | return variable_buffer_output (o, rc ? "1" : "", rc);
|
|---|
| 3254 | }
|
|---|
| 3255 |
|
|---|
| 3256 | #endif /* CONFIG_WITH_MATH */
|
|---|
| 3257 |
|
|---|
| 3258 | #ifdef CONFIG_WITH_NANOTS
|
|---|
| 3259 | /* Returns the current timestamp as nano seconds. The time
|
|---|
| 3260 | source is a high res monotone one if the platform provides
|
|---|
| 3261 | this (and we know about it).
|
|---|
| 3262 |
|
|---|
| 3263 | Tip. Use this with int-sub to profile makefile reading
|
|---|
| 3264 | and similar. */
|
|---|
| 3265 | static char *
|
|---|
| 3266 | func_nanots (char *o, char **argv, const char *funcname)
|
|---|
| 3267 | {
|
|---|
| 3268 | math_int ts;
|
|---|
| 3269 |
|
|---|
| 3270 | #if defined (WINDOWS32)
|
|---|
| 3271 | static int s_state = -1;
|
|---|
| 3272 | static LARGE_INTEGER s_freq;
|
|---|
| 3273 |
|
|---|
| 3274 | if (s_state == -1)
|
|---|
| 3275 | s_state = QueryPerformanceFrequency (&s_freq);
|
|---|
| 3276 | if (s_state)
|
|---|
| 3277 | {
|
|---|
| 3278 | LARGE_INTEGER pc;
|
|---|
| 3279 | if (!QueryPerformanceCounter (&pc))
|
|---|
| 3280 | {
|
|---|
| 3281 | s_state = 0;
|
|---|
| 3282 | return func_nanots (o, argv, funcname);
|
|---|
| 3283 | }
|
|---|
| 3284 | ts = (math_int)((long double)pc.QuadPart / (long double)s_freq.QuadPart * 1000000000);
|
|---|
| 3285 | }
|
|---|
| 3286 | else
|
|---|
| 3287 | {
|
|---|
| 3288 | /* fall back to low resolution system time. */
|
|---|
| 3289 | LARGE_INTEGER bigint;
|
|---|
| 3290 | FILETIME ft = {0,0};
|
|---|
| 3291 | GetSystemTimeAsFileTime (&ft);
|
|---|
| 3292 | bigint.u.LowPart = ft.dwLowDateTime;
|
|---|
| 3293 | bigint.u.HighPart = ft.dwLowDateTime;
|
|---|
| 3294 | ts = bigint.QuadPart * 100;
|
|---|
| 3295 | }
|
|---|
| 3296 |
|
|---|
| 3297 | /* FIXME: Linux and others have the realtime clock_* api, detect and use it. */
|
|---|
| 3298 |
|
|---|
| 3299 | #elif HAVE_GETTIMEOFDAY
|
|---|
| 3300 | struct timeval tv;
|
|---|
| 3301 | if (!gettimeofday (&tv, NULL))
|
|---|
| 3302 | ts = (math_int)tv.tv_sec * 1000000000
|
|---|
| 3303 | + tv.tv_usec * 1000;
|
|---|
| 3304 | else
|
|---|
| 3305 | {
|
|---|
| 3306 | error (NILF, _("$(nanots): gettimeofday failed"));
|
|---|
| 3307 | ts = 0;
|
|---|
| 3308 | }
|
|---|
| 3309 |
|
|---|
| 3310 | #else
|
|---|
| 3311 | # error "PORTME"
|
|---|
| 3312 | #endif
|
|---|
| 3313 |
|
|---|
| 3314 | return math_int_to_variable_buffer (o, ts);
|
|---|
| 3315 | }
|
|---|
| 3316 | #endif
|
|---|
| 3317 |
|
|---|
| 3318 |
|
|---|
| 3319 | /* Lookup table for builtin functions.
|
|---|
| 3320 |
|
|---|
| 3321 | This doesn't have to be sorted; we use a straight lookup. We might gain
|
|---|
| 3322 | some efficiency by moving most often used functions to the start of the
|
|---|
| 3323 | table.
|
|---|
| 3324 |
|
|---|
| 3325 | If MAXIMUM_ARGS is 0, that means there is no maximum and all
|
|---|
| 3326 | comma-separated values are treated as arguments.
|
|---|
| 3327 |
|
|---|
| 3328 | EXPAND_ARGS means that all arguments should be expanded before invocation.
|
|---|
| 3329 | Functions that do namespace tricks (foreach) don't automatically expand. */
|
|---|
| 3330 |
|
|---|
| 3331 | static char *func_call (char *o, char **argv, const char *funcname);
|
|---|
| 3332 |
|
|---|
| 3333 |
|
|---|
| 3334 | static struct function_table_entry function_table_init[] =
|
|---|
| 3335 | {
|
|---|
| 3336 | /* Name/size */ /* MIN MAX EXP? Function */
|
|---|
| 3337 | { STRING_SIZE_TUPLE("abspath"), 0, 1, 1, func_abspath},
|
|---|
| 3338 | { STRING_SIZE_TUPLE("addprefix"), 2, 2, 1, func_addsuffix_addprefix},
|
|---|
| 3339 | { STRING_SIZE_TUPLE("addsuffix"), 2, 2, 1, func_addsuffix_addprefix},
|
|---|
| 3340 | { STRING_SIZE_TUPLE("basename"), 0, 1, 1, func_basename_dir},
|
|---|
| 3341 | { STRING_SIZE_TUPLE("dir"), 0, 1, 1, func_basename_dir},
|
|---|
| 3342 | { STRING_SIZE_TUPLE("notdir"), 0, 1, 1, func_notdir_suffix},
|
|---|
| 3343 | { STRING_SIZE_TUPLE("subst"), 3, 3, 1, func_subst},
|
|---|
| 3344 | { STRING_SIZE_TUPLE("suffix"), 0, 1, 1, func_notdir_suffix},
|
|---|
| 3345 | { STRING_SIZE_TUPLE("filter"), 2, 2, 1, func_filter_filterout},
|
|---|
| 3346 | { STRING_SIZE_TUPLE("filter-out"), 2, 2, 1, func_filter_filterout},
|
|---|
| 3347 | { STRING_SIZE_TUPLE("findstring"), 2, 2, 1, func_findstring},
|
|---|
| 3348 | { STRING_SIZE_TUPLE("firstword"), 0, 1, 1, func_firstword},
|
|---|
| 3349 | { STRING_SIZE_TUPLE("flavor"), 0, 1, 1, func_flavor},
|
|---|
| 3350 | { STRING_SIZE_TUPLE("join"), 2, 2, 1, func_join},
|
|---|
| 3351 | { STRING_SIZE_TUPLE("lastword"), 0, 1, 1, func_lastword},
|
|---|
| 3352 | { STRING_SIZE_TUPLE("patsubst"), 3, 3, 1, func_patsubst},
|
|---|
| 3353 | { STRING_SIZE_TUPLE("realpath"), 0, 1, 1, func_realpath},
|
|---|
| 3354 | #ifdef CONFIG_WITH_RSORT
|
|---|
| 3355 | { STRING_SIZE_TUPLE("rsort"), 0, 1, 1, func_sort},
|
|---|
| 3356 | #endif
|
|---|
| 3357 | { STRING_SIZE_TUPLE("shell"), 0, 1, 1, func_shell},
|
|---|
| 3358 | { STRING_SIZE_TUPLE("sort"), 0, 1, 1, func_sort},
|
|---|
| 3359 | { STRING_SIZE_TUPLE("strip"), 0, 1, 1, func_strip},
|
|---|
| 3360 | { STRING_SIZE_TUPLE("wildcard"), 0, 1, 1, func_wildcard},
|
|---|
| 3361 | { STRING_SIZE_TUPLE("word"), 2, 2, 1, func_word},
|
|---|
| 3362 | { STRING_SIZE_TUPLE("wordlist"), 3, 3, 1, func_wordlist},
|
|---|
| 3363 | { STRING_SIZE_TUPLE("words"), 0, 1, 1, func_words},
|
|---|
| 3364 | { STRING_SIZE_TUPLE("origin"), 0, 1, 1, func_origin},
|
|---|
| 3365 | { STRING_SIZE_TUPLE("foreach"), 3, 3, 0, func_foreach},
|
|---|
| 3366 | { STRING_SIZE_TUPLE("call"), 1, 0, 1, func_call},
|
|---|
| 3367 | { STRING_SIZE_TUPLE("info"), 0, 1, 1, func_error},
|
|---|
| 3368 | { STRING_SIZE_TUPLE("error"), 0, 1, 1, func_error},
|
|---|
| 3369 | { STRING_SIZE_TUPLE("warning"), 0, 1, 1, func_error},
|
|---|
| 3370 | { STRING_SIZE_TUPLE("if"), 2, 3, 0, func_if},
|
|---|
| 3371 | { STRING_SIZE_TUPLE("or"), 1, 0, 0, func_or},
|
|---|
| 3372 | { STRING_SIZE_TUPLE("and"), 1, 0, 0, func_and},
|
|---|
| 3373 | { STRING_SIZE_TUPLE("value"), 0, 1, 1, func_value},
|
|---|
| 3374 | { STRING_SIZE_TUPLE("eval"), 0, 1, 1, func_eval},
|
|---|
| 3375 | #ifdef EXPERIMENTAL
|
|---|
| 3376 | { STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq},
|
|---|
| 3377 | { STRING_SIZE_TUPLE("not"), 0, 1, 1, func_not},
|
|---|
| 3378 | #endif
|
|---|
| 3379 | #ifdef CONFIG_WITH_TOUPPER_TOLOWER
|
|---|
| 3380 | { STRING_SIZE_TUPLE("toupper"), 0, 1, 1, func_toupper_tolower},
|
|---|
| 3381 | { STRING_SIZE_TUPLE("tolower"), 0, 1, 1, func_toupper_tolower},
|
|---|
| 3382 | #endif
|
|---|
| 3383 | #ifdef CONFIG_WITH_ABSPATHEX
|
|---|
| 3384 | { STRING_SIZE_TUPLE("abspathex"), 0, 2, 1, func_abspathex},
|
|---|
| 3385 | #endif
|
|---|
| 3386 | #ifdef CONFIG_WITH_XARGS
|
|---|
| 3387 | { STRING_SIZE_TUPLE("xargs"), 2, 0, 1, func_xargs},
|
|---|
| 3388 | #endif
|
|---|
| 3389 | #if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
|
|---|
| 3390 | { STRING_SIZE_TUPLE("comp-vars"), 3, 3, 1, func_comp_vars},
|
|---|
| 3391 | { STRING_SIZE_TUPLE("comp-cmds"), 3, 3, 1, func_comp_vars},
|
|---|
| 3392 | #endif
|
|---|
| 3393 | #ifdef CONFIG_WITH_DATE
|
|---|
| 3394 | { STRING_SIZE_TUPLE("date"), 0, 1, 1, func_date},
|
|---|
| 3395 | { STRING_SIZE_TUPLE("date-utc"), 0, 3, 1, func_date},
|
|---|
| 3396 | #endif
|
|---|
| 3397 | #ifdef CONFIG_WITH_FILE_SIZE
|
|---|
| 3398 | { STRING_SIZE_TUPLE("file-size"), 1, 1, 1, func_file_size},
|
|---|
| 3399 | #endif
|
|---|
| 3400 | #ifdef CONFIG_WITH_WHICH
|
|---|
| 3401 | { STRING_SIZE_TUPLE("which"), 0, 0, 1, func_which},
|
|---|
| 3402 | #endif
|
|---|
| 3403 | #ifdef CONFIG_WITH_STACK
|
|---|
| 3404 | { STRING_SIZE_TUPLE("stack-push"), 2, 2, 1, func_stack_push},
|
|---|
| 3405 | { STRING_SIZE_TUPLE("stack-pop"), 1, 1, 1, func_stack_pop_top},
|
|---|
| 3406 | { STRING_SIZE_TUPLE("stack-popv"), 1, 1, 1, func_stack_pop_top},
|
|---|
| 3407 | { STRING_SIZE_TUPLE("stack-top"), 1, 1, 1, func_stack_pop_top},
|
|---|
| 3408 | #endif
|
|---|
| 3409 | #ifdef CONFIG_WITH_MATH
|
|---|
| 3410 | { STRING_SIZE_TUPLE("int-add"), 2, 0, 1, func_int_add},
|
|---|
| 3411 | { STRING_SIZE_TUPLE("int-sub"), 2, 0, 1, func_int_sub},
|
|---|
| 3412 | { STRING_SIZE_TUPLE("int-mul"), 2, 0, 1, func_int_mul},
|
|---|
| 3413 | { STRING_SIZE_TUPLE("int-div"), 2, 0, 1, func_int_div},
|
|---|
| 3414 | { STRING_SIZE_TUPLE("int-mod"), 2, 2, 1, func_int_mod},
|
|---|
| 3415 | { STRING_SIZE_TUPLE("int-not"), 1, 1, 1, func_int_not},
|
|---|
| 3416 | { STRING_SIZE_TUPLE("int-and"), 2, 0, 1, func_int_and},
|
|---|
| 3417 | { STRING_SIZE_TUPLE("int-or"), 2, 0, 1, func_int_or},
|
|---|
| 3418 | { STRING_SIZE_TUPLE("int-xor"), 2, 0, 1, func_int_xor},
|
|---|
| 3419 | { STRING_SIZE_TUPLE("int-eq"), 2, 2, 1, func_int_cmp},
|
|---|
| 3420 | { STRING_SIZE_TUPLE("int-ne"), 2, 2, 1, func_int_cmp},
|
|---|
| 3421 | { STRING_SIZE_TUPLE("int-gt"), 2, 2, 1, func_int_cmp},
|
|---|
| 3422 | { STRING_SIZE_TUPLE("int-ge"), 2, 2, 1, func_int_cmp},
|
|---|
| 3423 | { STRING_SIZE_TUPLE("int-lt"), 2, 2, 1, func_int_cmp},
|
|---|
| 3424 | { STRING_SIZE_TUPLE("int-le"), 2, 2, 1, func_int_cmp},
|
|---|
| 3425 | #endif
|
|---|
| 3426 | #ifdef CONFIG_WITH_NANOTS
|
|---|
| 3427 | { STRING_SIZE_TUPLE("nanots"), 0, 0, 0, func_nanots},
|
|---|
| 3428 | #endif
|
|---|
| 3429 | #ifdef KMK_HELPERS
|
|---|
| 3430 | { STRING_SIZE_TUPLE("kb-src-tool"), 1, 1, 0, func_kbuild_source_tool},
|
|---|
| 3431 | { STRING_SIZE_TUPLE("kb-obj-base"), 1, 1, 0, func_kbuild_object_base},
|
|---|
| 3432 | { STRING_SIZE_TUPLE("kb-obj-suff"), 1, 1, 0, func_kbuild_object_suffix},
|
|---|
| 3433 | { STRING_SIZE_TUPLE("kb-src-prop"), 4, 4, 0, func_kbuild_source_prop},
|
|---|
| 3434 | { STRING_SIZE_TUPLE("kb-src-one"), 0, 1, 0, func_kbuild_source_one},
|
|---|
| 3435 | #endif
|
|---|
| 3436 | };
|
|---|
| 3437 |
|
|---|
| 3438 | #define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry))
|
|---|
| 3439 | |
|---|
| 3440 |
|
|---|
| 3441 |
|
|---|
| 3442 | /* These must come after the definition of function_table. */
|
|---|
| 3443 |
|
|---|
| 3444 | static char *
|
|---|
| 3445 | expand_builtin_function (char *o, int argc, char **argv,
|
|---|
| 3446 | const struct function_table_entry *entry_p)
|
|---|
| 3447 | {
|
|---|
| 3448 | if (argc < (int)entry_p->minimum_args)
|
|---|
| 3449 | fatal (*expanding_var,
|
|---|
| 3450 | _("insufficient number of arguments (%d) to function `%s'"),
|
|---|
| 3451 | argc, entry_p->name);
|
|---|
| 3452 |
|
|---|
| 3453 | /* I suppose technically some function could do something with no
|
|---|
| 3454 | arguments, but so far none do, so just test it for all functions here
|
|---|
| 3455 | rather than in each one. We can change it later if necessary. */
|
|---|
| 3456 |
|
|---|
| 3457 | if (!argc)
|
|---|
| 3458 | return o;
|
|---|
| 3459 |
|
|---|
| 3460 | if (!entry_p->func_ptr)
|
|---|
| 3461 | fatal (*expanding_var,
|
|---|
| 3462 | _("unimplemented on this platform: function `%s'"), entry_p->name);
|
|---|
| 3463 |
|
|---|
| 3464 | return entry_p->func_ptr (o, argv, entry_p->name);
|
|---|
| 3465 | }
|
|---|
| 3466 |
|
|---|
| 3467 | /* Check for a function invocation in *STRINGP. *STRINGP points at the
|
|---|
| 3468 | opening ( or { and is not null-terminated. If a function invocation
|
|---|
| 3469 | is found, expand it into the buffer at *OP, updating *OP, incrementing
|
|---|
| 3470 | *STRINGP past the reference and returning nonzero. If not, return zero. */
|
|---|
| 3471 |
|
|---|
| 3472 | static int
|
|---|
| 3473 | handle_function2 (const struct function_table_entry *entry_p, char **op, const char **stringp) /* bird split it up. */
|
|---|
| 3474 | {
|
|---|
| 3475 | char openparen = (*stringp)[0];
|
|---|
| 3476 | char closeparen = openparen == '(' ? ')' : '}';
|
|---|
| 3477 | const char *beg;
|
|---|
| 3478 | const char *end;
|
|---|
| 3479 | int count = 0;
|
|---|
| 3480 | char *abeg = NULL;
|
|---|
| 3481 | char **argv, **argvp;
|
|---|
| 3482 | int nargs;
|
|---|
| 3483 |
|
|---|
| 3484 | beg = *stringp + 1;
|
|---|
| 3485 |
|
|---|
| 3486 | /* We found a builtin function. Find the beginning of its arguments (skip
|
|---|
| 3487 | whitespace after the name). */
|
|---|
| 3488 |
|
|---|
| 3489 | beg = next_token (beg + entry_p->len);
|
|---|
| 3490 |
|
|---|
| 3491 | /* Find the end of the function invocation, counting nested use of
|
|---|
| 3492 | whichever kind of parens we use. Since we're looking, count commas
|
|---|
| 3493 | to get a rough estimate of how many arguments we might have. The
|
|---|
| 3494 | count might be high, but it'll never be low. */
|
|---|
| 3495 |
|
|---|
| 3496 | for (nargs=1, end=beg; *end != '\0'; ++end)
|
|---|
| 3497 | if (*end == ',')
|
|---|
| 3498 | ++nargs;
|
|---|
| 3499 | else if (*end == openparen)
|
|---|
| 3500 | ++count;
|
|---|
| 3501 | else if (*end == closeparen && --count < 0)
|
|---|
| 3502 | break;
|
|---|
| 3503 |
|
|---|
| 3504 | if (count >= 0)
|
|---|
| 3505 | fatal (*expanding_var,
|
|---|
| 3506 | _("unterminated call to function `%s': missing `%c'"),
|
|---|
| 3507 | entry_p->name, closeparen);
|
|---|
| 3508 |
|
|---|
| 3509 | *stringp = end;
|
|---|
| 3510 |
|
|---|
| 3511 | /* Get some memory to store the arg pointers. */
|
|---|
| 3512 | argvp = argv = alloca (sizeof (char *) * (nargs + 2));
|
|---|
| 3513 |
|
|---|
| 3514 | /* Chop the string into arguments, then a nul. As soon as we hit
|
|---|
| 3515 | MAXIMUM_ARGS (if it's >0) assume the rest of the string is part of the
|
|---|
| 3516 | last argument.
|
|---|
| 3517 |
|
|---|
| 3518 | If we're expanding, store pointers to the expansion of each one. If
|
|---|
| 3519 | not, make a duplicate of the string and point into that, nul-terminating
|
|---|
| 3520 | each argument. */
|
|---|
| 3521 |
|
|---|
| 3522 | if (entry_p->expand_args)
|
|---|
| 3523 | {
|
|---|
| 3524 | const char *p;
|
|---|
| 3525 | for (p=beg, nargs=0; p <= end; ++argvp)
|
|---|
| 3526 | {
|
|---|
| 3527 | const char *next;
|
|---|
| 3528 |
|
|---|
| 3529 | ++nargs;
|
|---|
| 3530 |
|
|---|
| 3531 | if (nargs == entry_p->maximum_args
|
|---|
| 3532 | || (! (next = find_next_argument (openparen, closeparen, p, end))))
|
|---|
| 3533 | next = end;
|
|---|
| 3534 |
|
|---|
| 3535 | *argvp = expand_argument (p, next);
|
|---|
| 3536 | p = next + 1;
|
|---|
| 3537 | }
|
|---|
| 3538 | }
|
|---|
| 3539 | else
|
|---|
| 3540 | {
|
|---|
| 3541 | int len = end - beg;
|
|---|
| 3542 | char *p, *aend;
|
|---|
| 3543 |
|
|---|
| 3544 | abeg = xmalloc (len+1);
|
|---|
| 3545 | memcpy (abeg, beg, len);
|
|---|
| 3546 | abeg[len] = '\0';
|
|---|
| 3547 | aend = abeg + len;
|
|---|
| 3548 |
|
|---|
| 3549 | for (p=abeg, nargs=0; p <= aend; ++argvp)
|
|---|
| 3550 | {
|
|---|
| 3551 | char *next;
|
|---|
| 3552 |
|
|---|
| 3553 | ++nargs;
|
|---|
| 3554 |
|
|---|
| 3555 | if (nargs == entry_p->maximum_args
|
|---|
| 3556 | || (! (next = find_next_argument (openparen, closeparen, p, aend))))
|
|---|
| 3557 | next = aend;
|
|---|
| 3558 |
|
|---|
| 3559 | *argvp = p;
|
|---|
| 3560 | *next = '\0';
|
|---|
| 3561 | p = next + 1;
|
|---|
| 3562 | }
|
|---|
| 3563 | }
|
|---|
| 3564 | *argvp = NULL;
|
|---|
| 3565 |
|
|---|
| 3566 | /* Finally! Run the function... */
|
|---|
| 3567 | *op = expand_builtin_function (*op, nargs, argv, entry_p);
|
|---|
| 3568 |
|
|---|
| 3569 | /* Free memory. */
|
|---|
| 3570 | if (entry_p->expand_args)
|
|---|
| 3571 | for (argvp=argv; *argvp != 0; ++argvp)
|
|---|
| 3572 | free (*argvp);
|
|---|
| 3573 | if (abeg)
|
|---|
| 3574 | free (abeg);
|
|---|
| 3575 |
|
|---|
| 3576 | return 1;
|
|---|
| 3577 | }
|
|---|
| 3578 |
|
|---|
| 3579 | int
|
|---|
| 3580 | handle_function (char **op, const char **stringp) /* bird split it up */
|
|---|
| 3581 | {
|
|---|
| 3582 | const struct function_table_entry *entry_p = lookup_function (*stringp + 1);
|
|---|
| 3583 | if (!entry_p)
|
|---|
| 3584 | return 0;
|
|---|
| 3585 | return handle_function2 (entry_p, op, stringp);
|
|---|
| 3586 | }
|
|---|
| 3587 | |
|---|
| 3588 |
|
|---|
| 3589 |
|
|---|
| 3590 | /* User-defined functions. Expand the first argument as either a builtin
|
|---|
| 3591 | function or a make variable, in the context of the rest of the arguments
|
|---|
| 3592 | assigned to $1, $2, ... $N. $0 is the name of the function. */
|
|---|
| 3593 |
|
|---|
| 3594 | static char *
|
|---|
| 3595 | func_call (char *o, char **argv, const char *funcname UNUSED)
|
|---|
| 3596 | {
|
|---|
| 3597 | static int max_args = 0;
|
|---|
| 3598 | char *fname;
|
|---|
| 3599 | char *cp;
|
|---|
| 3600 | char *body;
|
|---|
| 3601 | int flen;
|
|---|
| 3602 | int i;
|
|---|
| 3603 | int saved_args;
|
|---|
| 3604 | const struct function_table_entry *entry_p;
|
|---|
| 3605 | struct variable *v;
|
|---|
| 3606 |
|
|---|
| 3607 | /* There is no way to define a variable with a space in the name, so strip
|
|---|
| 3608 | leading and trailing whitespace as a favor to the user. */
|
|---|
| 3609 | fname = argv[0];
|
|---|
| 3610 | while (*fname != '\0' && isspace ((unsigned char)*fname))
|
|---|
| 3611 | ++fname;
|
|---|
| 3612 |
|
|---|
| 3613 | cp = fname + strlen (fname) - 1;
|
|---|
| 3614 | while (cp > fname && isspace ((unsigned char)*cp))
|
|---|
| 3615 | --cp;
|
|---|
| 3616 | cp[1] = '\0';
|
|---|
| 3617 |
|
|---|
| 3618 | /* Calling nothing is a no-op */
|
|---|
| 3619 | if (*fname == '\0')
|
|---|
| 3620 | return o;
|
|---|
| 3621 |
|
|---|
| 3622 | /* Are we invoking a builtin function? */
|
|---|
| 3623 |
|
|---|
| 3624 | entry_p = lookup_function (fname);
|
|---|
| 3625 | if (entry_p)
|
|---|
| 3626 | {
|
|---|
| 3627 | /* How many arguments do we have? */
|
|---|
| 3628 | for (i=0; argv[i+1]; ++i)
|
|---|
| 3629 | ;
|
|---|
| 3630 | return expand_builtin_function (o, i, argv+1, entry_p);
|
|---|
| 3631 | }
|
|---|
| 3632 |
|
|---|
| 3633 | /* Not a builtin, so the first argument is the name of a variable to be
|
|---|
| 3634 | expanded and interpreted as a function. Find it. */
|
|---|
| 3635 | flen = strlen (fname);
|
|---|
| 3636 |
|
|---|
| 3637 | v = lookup_variable (fname, flen);
|
|---|
| 3638 |
|
|---|
| 3639 | if (v == 0)
|
|---|
| 3640 | warn_undefined (fname, flen);
|
|---|
| 3641 |
|
|---|
| 3642 | if (v == 0 || *v->value == '\0')
|
|---|
| 3643 | return o;
|
|---|
| 3644 |
|
|---|
| 3645 | body = alloca (flen + 4);
|
|---|
| 3646 | body[0] = '$';
|
|---|
| 3647 | body[1] = '(';
|
|---|
| 3648 | memcpy (body + 2, fname, flen);
|
|---|
| 3649 | body[flen+2] = ')';
|
|---|
| 3650 | body[flen+3] = '\0';
|
|---|
| 3651 |
|
|---|
| 3652 | /* Set up arguments $(1) .. $(N). $(0) is the function name. */
|
|---|
| 3653 |
|
|---|
| 3654 | push_new_variable_scope ();
|
|---|
| 3655 |
|
|---|
| 3656 | for (i=0; *argv; ++i, ++argv)
|
|---|
| 3657 | {
|
|---|
| 3658 | char num[11];
|
|---|
| 3659 |
|
|---|
| 3660 | sprintf (num, "%d", i);
|
|---|
| 3661 | define_variable (num, strlen (num), *argv, o_automatic, 0);
|
|---|
| 3662 | }
|
|---|
| 3663 |
|
|---|
| 3664 | /* If the number of arguments we have is < max_args, it means we're inside
|
|---|
| 3665 | a recursive invocation of $(call ...). Fill in the remaining arguments
|
|---|
| 3666 | in the new scope with the empty value, to hide them from this
|
|---|
| 3667 | invocation. */
|
|---|
| 3668 |
|
|---|
| 3669 | for (; i < max_args; ++i)
|
|---|
| 3670 | {
|
|---|
| 3671 | char num[11];
|
|---|
| 3672 |
|
|---|
| 3673 | sprintf (num, "%d", i);
|
|---|
| 3674 | define_variable (num, strlen (num), "", o_automatic, 0);
|
|---|
| 3675 | }
|
|---|
| 3676 |
|
|---|
| 3677 | /* Expand the body in the context of the arguments, adding the result to
|
|---|
| 3678 | the variable buffer. */
|
|---|
| 3679 |
|
|---|
| 3680 | v->exp_count = EXP_COUNT_MAX;
|
|---|
| 3681 |
|
|---|
| 3682 | saved_args = max_args;
|
|---|
| 3683 | max_args = i;
|
|---|
| 3684 | o = variable_expand_string (o, body, flen+3);
|
|---|
| 3685 | max_args = saved_args;
|
|---|
| 3686 |
|
|---|
| 3687 | v->exp_count = 0;
|
|---|
| 3688 |
|
|---|
| 3689 | pop_variable_scope ();
|
|---|
| 3690 |
|
|---|
| 3691 | return o + strlen (o);
|
|---|
| 3692 | }
|
|---|
| 3693 |
|
|---|
| 3694 | void
|
|---|
| 3695 | hash_init_function_table (void)
|
|---|
| 3696 | {
|
|---|
| 3697 | hash_init (&function_table, FUNCTION_TABLE_ENTRIES * 2,
|
|---|
| 3698 | function_table_entry_hash_1, function_table_entry_hash_2,
|
|---|
| 3699 | function_table_entry_hash_cmp);
|
|---|
| 3700 | hash_load (&function_table, function_table_init,
|
|---|
| 3701 | FUNCTION_TABLE_ENTRIES, sizeof (struct function_table_entry));
|
|---|
| 3702 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
|---|
| 3703 | {
|
|---|
| 3704 | unsigned i;
|
|---|
| 3705 | for (i = 0; i < FUNCTION_TABLE_ENTRIES; i++)
|
|---|
| 3706 | assert (function_table_init[i].len <= MAX_FUNCTION_LENGTH);
|
|---|
| 3707 | }
|
|---|
| 3708 | #endif
|
|---|
| 3709 | }
|
|---|