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