| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "makeint.h" |
| 18 |
#include "filedef.h" |
| 19 |
#include "expand.h" |
| 20 |
#include "variable.h" |
| 21 |
#include "dep.h" |
| 22 |
#include "job.h" |
| 23 |
#include "os.h" |
| 24 |
#include "commands.h" |
| 25 |
#include "debug.h" |
| 26 |
|
| 27 |
|
| 28 |
#include "cmd.h" |
| 29 |
|
| 30 |
struct function_table_entry |
| 31 |
{ |
| 32 |
union { |
| 33 |
char *(*func_ptr) (char *output, char **argv, const char *fname); |
| 34 |
gmk_func_ptr alloc_func_ptr; |
| 35 |
} fptr; |
| 36 |
const char *name; |
| 37 |
unsigned char len; |
| 38 |
unsigned char minimum_args; |
| 39 |
unsigned char maximum_args; |
| 40 |
unsigned int expand_args:1; |
| 41 |
unsigned int alloc_fn:1; |
| 42 |
}; |
| 43 |
|
| 44 |
static unsigned long |
| 45 |
function_table_entry_hash_1 (const void *keyv) |
| 46 |
{ |
| 47 |
const struct function_table_entry *key = keyv; |
| 48 |
return_STRING_N_HASH_1 (key->name, key->len); |
| 49 |
} |
| 50 |
|
| 51 |
static unsigned long |
| 52 |
function_table_entry_hash_2 (const void *keyv) |
| 53 |
{ |
| 54 |
const struct function_table_entry *key = keyv; |
| 55 |
return_STRING_N_HASH_2 (key->name, key->len); |
| 56 |
} |
| 57 |
|
| 58 |
static int |
| 59 |
function_table_entry_hash_cmp (const void *xv, const void *yv) |
| 60 |
{ |
| 61 |
const struct function_table_entry *x = xv; |
| 62 |
const struct function_table_entry *y = yv; |
| 63 |
int result = x->len - y->len; |
| 64 |
if (result) |
| 65 |
return result; |
| 66 |
return_STRING_N_COMPARE (x->name, y->name, x->len); |
| 67 |
} |
| 68 |
|
| 69 |
static struct hash_table function_table; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
char * |
| 79 |
subst_expand (char *o, const char *text, const char *subst, const char *replace, |
| 80 |
size_t slen, size_t rlen, int by_word) |
| 81 |
{ |
| 82 |
const char *t = text; |
| 83 |
const char *p; |
| 84 |
|
| 85 |
if (slen == 0 && !by_word) |
| 86 |
{ |
| 87 |
|
| 88 |
o = variable_buffer_output (o, t, strlen (t)); |
| 89 |
if (rlen > 0) |
| 90 |
o = variable_buffer_output (o, replace, rlen); |
| 91 |
return o; |
| 92 |
} |
| 93 |
|
| 94 |
do |
| 95 |
{ |
| 96 |
if (by_word && slen == 0) |
| 97 |
|
| 98 |
|
| 99 |
p = end_of_token (next_token (t)); |
| 100 |
else |
| 101 |
{ |
| 102 |
p = strstr (t, subst); |
| 103 |
if (p == 0) |
| 104 |
{ |
| 105 |
|
| 106 |
o = variable_buffer_output (o, t, strlen (t)); |
| 107 |
return o; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
if (p > t) |
| 113 |
o = variable_buffer_output (o, t, p - t); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if (by_word |
| 118 |
&& ((p > text && !ISSPACE (p[-1])) |
| 119 |
|| ! STOP_SET (p[slen], MAP_SPACE|MAP_NUL))) |
| 120 |
|
| 121 |
|
| 122 |
o = variable_buffer_output (o, subst, slen); |
| 123 |
else if (rlen > 0) |
| 124 |
|
| 125 |
o = variable_buffer_output (o, replace, rlen); |
| 126 |
|
| 127 |
|
| 128 |
t = p + slen; |
| 129 |
} while (*t != '\0'); |
| 130 |
|
| 131 |
return o; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
char * |
| 146 |
patsubst_expand_pat (char *o, const char *text, |
| 147 |
const char *pattern, const char *replace, |
| 148 |
const char *pattern_percent, const char *replace_percent) |
| 149 |
{ |
| 150 |
size_t pattern_prepercent_len, pattern_postpercent_len; |
| 151 |
size_t replace_prepercent_len, replace_postpercent_len; |
| 152 |
const char *t; |
| 153 |
size_t len; |
| 154 |
int doneany = 0; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
if (replace_percent) |
| 159 |
{ |
| 160 |
replace_prepercent_len = replace_percent - replace - 1; |
| 161 |
replace_postpercent_len = strlen (replace_percent); |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
replace_prepercent_len = strlen (replace); |
| 166 |
replace_postpercent_len = 0; |
| 167 |
} |
| 168 |
|
| 169 |
if (!pattern_percent) |
| 170 |
|
| 171 |
return subst_expand (o, text, pattern, replace, |
| 172 |
strlen (pattern), strlen (replace), 1); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
pattern_prepercent_len = pattern_percent - pattern - 1; |
| 177 |
pattern_postpercent_len = strlen (pattern_percent); |
| 178 |
|
| 179 |
while ((t = find_next_token (&text, &len)) != 0) |
| 180 |
{ |
| 181 |
int fail = 0; |
| 182 |
|
| 183 |
|
| 184 |
if (len < pattern_prepercent_len + pattern_postpercent_len) |
| 185 |
fail = 1; |
| 186 |
|
| 187 |
|
| 188 |
if (!fail && pattern_prepercent_len > 0 |
| 189 |
&& (*t != *pattern |
| 190 |
|| t[pattern_prepercent_len - 1] != pattern_percent[-2] |
| 191 |
|| !strneq (t + 1, pattern + 1, pattern_prepercent_len - 1))) |
| 192 |
fail = 1; |
| 193 |
|
| 194 |
|
| 195 |
if (!fail && pattern_postpercent_len > 0 |
| 196 |
&& (t[len - 1] != pattern_percent[pattern_postpercent_len - 1] |
| 197 |
|| t[len - pattern_postpercent_len] != *pattern_percent |
| 198 |
|| !strneq (&t[len - pattern_postpercent_len], |
| 199 |
pattern_percent, pattern_postpercent_len - 1))) |
| 200 |
fail = 1; |
| 201 |
|
| 202 |
if (fail) |
| 203 |
|
| 204 |
o = variable_buffer_output (o, t, len); |
| 205 |
else |
| 206 |
{ |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
o = variable_buffer_output (o, replace, replace_prepercent_len); |
| 211 |
|
| 212 |
if (replace_percent != 0) |
| 213 |
{ |
| 214 |
|
| 215 |
|
| 216 |
o = variable_buffer_output (o, t + pattern_prepercent_len, |
| 217 |
len - (pattern_prepercent_len |
| 218 |
+ pattern_postpercent_len)); |
| 219 |
|
| 220 |
o = variable_buffer_output (o, replace_percent, |
| 221 |
replace_postpercent_len); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
if (fail || replace_prepercent_len > 0 |
| 227 |
|| (replace_percent != 0 && len + replace_postpercent_len > 0)) |
| 228 |
{ |
| 229 |
o = variable_buffer_output (o, " ", 1); |
| 230 |
doneany = 1; |
| 231 |
} |
| 232 |
} |
| 233 |
if (doneany) |
| 234 |
|
| 235 |
--o; |
| 236 |
|
| 237 |
return o; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
char * |
| 251 |
patsubst_expand (char *o, const char *text, char *pattern, char *replace) |
| 252 |
{ |
| 253 |
const char *pattern_percent = find_percent (pattern); |
| 254 |
const char *replace_percent = find_percent (replace); |
| 255 |
|
| 256 |
|
| 257 |
if (replace_percent) |
| 258 |
++replace_percent; |
| 259 |
if (pattern_percent) |
| 260 |
++pattern_percent; |
| 261 |
|
| 262 |
return patsubst_expand_pat (o, text, pattern, replace, |
| 263 |
pattern_percent, replace_percent); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
static const struct function_table_entry * |
| 270 |
lookup_function (const char *s) |
| 271 |
{ |
| 272 |
struct function_table_entry function_table_entry_key; |
| 273 |
const char *e = s; |
| 274 |
|
| 275 |
while (STOP_SET (*e, MAP_USERFUNC)) |
| 276 |
e++; |
| 277 |
|
| 278 |
if (e == s || !STOP_SET(*e, MAP_NUL|MAP_SPACE)) |
| 279 |
return NULL; |
| 280 |
|
| 281 |
function_table_entry_key.name = s; |
| 282 |
function_table_entry_key.len = (unsigned char) (e - s); |
| 283 |
|
| 284 |
return hash_find_item (&function_table, &function_table_entry_key); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
int |
| 291 |
pattern_matches (const char *pattern, const char *percent, const char *str) |
| 292 |
{ |
| 293 |
size_t sfxlen, strlength; |
| 294 |
|
| 295 |
if (percent == 0) |
| 296 |
{ |
| 297 |
size_t len = strlen (pattern) + 1; |
| 298 |
char *new_chars = alloca (len); |
| 299 |
memcpy (new_chars, pattern, len); |
| 300 |
percent = find_percent (new_chars); |
| 301 |
if (percent == 0) |
| 302 |
return streq (new_chars, str); |
| 303 |
pattern = new_chars; |
| 304 |
} |
| 305 |
|
| 306 |
sfxlen = strlen (percent + 1); |
| 307 |
strlength = strlen (str); |
| 308 |
|
| 309 |
if (strlength < (percent - pattern) + sfxlen |
| 310 |
|| !strneq (pattern, str, percent - pattern)) |
| 311 |
return 0; |
| 312 |
|
| 313 |
return !strcmp (percent + 1, str + (strlength - sfxlen)); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
static char * |
| 325 |
find_next_argument (char startparen, char endparen, |
| 326 |
const char *ptr, const char *end) |
| 327 |
{ |
| 328 |
int count = 0; |
| 329 |
|
| 330 |
for (; ptr < end; ++ptr) |
| 331 |
if (!STOP_SET (*ptr, MAP_VARSEP|MAP_COMMA)) |
| 332 |
continue; |
| 333 |
|
| 334 |
else if (*ptr == startparen) |
| 335 |
++count; |
| 336 |
|
| 337 |
else if (*ptr == endparen) |
| 338 |
{ |
| 339 |
--count; |
| 340 |
if (count < 0) |
| 341 |
return NULL; |
| 342 |
} |
| 343 |
|
| 344 |
else if (*ptr == ',' && !count) |
| 345 |
return (char *)ptr; |
| 346 |
|
| 347 |
|
| 348 |
return NULL; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
static char * |
| 356 |
string_glob (char *line) |
| 357 |
{ |
| 358 |
static char *result = 0; |
| 359 |
static size_t length; |
| 360 |
struct nameseq *chain; |
| 361 |
size_t idx; |
| 362 |
|
| 363 |
chain = PARSE_FILE_SEQ (&line, struct nameseq, MAP_NUL, NULL, |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
PARSEFS_NOSTRIP|PARSEFS_NOCACHE|PARSEFS_EXISTS); |
| 368 |
|
| 369 |
if (result == 0) |
| 370 |
{ |
| 371 |
length = 100; |
| 372 |
result = xmalloc (100); |
| 373 |
} |
| 374 |
|
| 375 |
idx = 0; |
| 376 |
while (chain != 0) |
| 377 |
{ |
| 378 |
struct nameseq *next = chain->next; |
| 379 |
size_t len = strlen (chain->name); |
| 380 |
|
| 381 |
if (idx + len + 1 > length) |
| 382 |
{ |
| 383 |
length += (len + 1) * 2; |
| 384 |
result = xrealloc (result, length); |
| 385 |
} |
| 386 |
memcpy (&result[idx], chain->name, len); |
| 387 |
idx += len; |
| 388 |
result[idx++] = ' '; |
| 389 |
|
| 390 |
|
| 391 |
free ((char *)chain->name); |
| 392 |
free (chain); |
| 393 |
chain = next; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
if (idx == 0) |
| 398 |
result[0] = '\0'; |
| 399 |
else |
| 400 |
result[idx - 1] = '\0'; |
| 401 |
|
| 402 |
return result; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
static char * |
| 410 |
func_patsubst (char *o, char **argv, const char *funcname UNUSED) |
| 411 |
{ |
| 412 |
o = patsubst_expand (o, argv[2], argv[0], argv[1]); |
| 413 |
return o; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
static char * |
| 418 |
func_join (char *o, char **argv, const char *funcname UNUSED) |
| 419 |
{ |
| 420 |
int doneany = 0; |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
const char *tp; |
| 427 |
const char *pp; |
| 428 |
const char *list1_iterator = argv[0]; |
| 429 |
const char *list2_iterator = argv[1]; |
| 430 |
do |
| 431 |
{ |
| 432 |
size_t len1, len2; |
| 433 |
|
| 434 |
tp = find_next_token (&list1_iterator, &len1); |
| 435 |
if (tp != 0) |
| 436 |
o = variable_buffer_output (o, tp, len1); |
| 437 |
|
| 438 |
pp = find_next_token (&list2_iterator, &len2); |
| 439 |
if (pp != 0) |
| 440 |
o = variable_buffer_output (o, pp, len2); |
| 441 |
|
| 442 |
if (tp != 0 || pp != 0) |
| 443 |
{ |
| 444 |
o = variable_buffer_output (o, " ", 1); |
| 445 |
doneany = 1; |
| 446 |
} |
| 447 |
} |
| 448 |
while (tp != 0 || pp != 0); |
| 449 |
if (doneany) |
| 450 |
|
| 451 |
--o; |
| 452 |
|
| 453 |
return o; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
static char * |
| 458 |
func_origin (char *o, char **argv, const char *funcname UNUSED) |
| 459 |
{ |
| 460 |
|
| 461 |
struct variable *v = lookup_variable (argv[0], strlen (argv[0])); |
| 462 |
if (v == 0) |
| 463 |
o = variable_buffer_output (o, "undefined", 9); |
| 464 |
else |
| 465 |
switch (v->origin) |
| 466 |
{ |
| 467 |
default: |
| 468 |
case o_invalid: |
| 469 |
abort (); |
| 470 |
break; |
| 471 |
case o_default: |
| 472 |
o = variable_buffer_output (o, "default", 7); |
| 473 |
break; |
| 474 |
case o_env: |
| 475 |
o = variable_buffer_output (o, "environment", 11); |
| 476 |
break; |
| 477 |
case o_file: |
| 478 |
o = variable_buffer_output (o, "file", 4); |
| 479 |
break; |
| 480 |
case o_env_override: |
| 481 |
o = variable_buffer_output (o, "environment override", 20); |
| 482 |
break; |
| 483 |
case o_command: |
| 484 |
o = variable_buffer_output (o, "command line", 12); |
| 485 |
break; |
| 486 |
case o_override: |
| 487 |
o = variable_buffer_output (o, "override", 8); |
| 488 |
break; |
| 489 |
case o_automatic: |
| 490 |
o = variable_buffer_output (o, "automatic", 9); |
| 491 |
break; |
| 492 |
} |
| 493 |
|
| 494 |
return o; |
| 495 |
} |
| 496 |
|
| 497 |
static char * |
| 498 |
func_flavor (char *o, char **argv, const char *funcname UNUSED) |
| 499 |
{ |
| 500 |
struct variable *v = lookup_variable (argv[0], strlen (argv[0])); |
| 501 |
|
| 502 |
if (v == 0) |
| 503 |
o = variable_buffer_output (o, "undefined", 9); |
| 504 |
else |
| 505 |
if (v->recursive) |
| 506 |
o = variable_buffer_output (o, "recursive", 9); |
| 507 |
else |
| 508 |
o = variable_buffer_output (o, "simple", 6); |
| 509 |
|
| 510 |
return o; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
static char * |
| 515 |
func_notdir_suffix (char *o, char **argv, const char *funcname) |
| 516 |
{ |
| 517 |
|
| 518 |
const char *list_iterator = argv[0]; |
| 519 |
const char *p2; |
| 520 |
int doneany =0; |
| 521 |
size_t len=0; |
| 522 |
|
| 523 |
int is_suffix = funcname[0] == 's'; |
| 524 |
int is_notdir = !is_suffix; |
| 525 |
int stop = MAP_DIRSEP | (is_suffix ? MAP_DOT : 0); |
| 526 |
while ((p2 = find_next_token (&list_iterator, &len)) != 0) |
| 527 |
{ |
| 528 |
const char *p = p2 + len - 1; |
| 529 |
|
| 530 |
while (p >= p2 && ! STOP_SET (*p, stop)) |
| 531 |
--p; |
| 532 |
|
| 533 |
if (p >= p2) |
| 534 |
{ |
| 535 |
if (is_notdir) |
| 536 |
++p; |
| 537 |
else if (*p != '.') |
| 538 |
continue; |
| 539 |
o = variable_buffer_output (o, p, len - (p - p2)); |
| 540 |
} |
| 541 |
else if (is_notdir) |
| 542 |
o = variable_buffer_output (o, p2, len); |
| 543 |
|
| 544 |
if (is_notdir || p >= p2) |
| 545 |
{ |
| 546 |
o = variable_buffer_output (o, " ", 1); |
| 547 |
|
| 548 |
doneany = 1; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
if (doneany) |
| 553 |
|
| 554 |
--o; |
| 555 |
|
| 556 |
return o; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
static char * |
| 561 |
func_basename_dir (char *o, char **argv, const char *funcname) |
| 562 |
{ |
| 563 |
|
| 564 |
const char *p3 = argv[0]; |
| 565 |
const char *p2; |
| 566 |
int doneany = 0; |
| 567 |
size_t len = 0; |
| 568 |
|
| 569 |
int is_basename = funcname[0] == 'b'; |
| 570 |
int is_dir = !is_basename; |
| 571 |
int stop = MAP_DIRSEP | (is_basename ? MAP_DOT : 0) | MAP_NUL; |
| 572 |
while ((p2 = find_next_token (&p3, &len)) != 0) |
| 573 |
{ |
| 574 |
const char *p = p2 + len - 1; |
| 575 |
while (p >= p2 && ! STOP_SET (*p, stop)) |
| 576 |
--p; |
| 577 |
|
| 578 |
if (p >= p2 && (is_dir)) |
| 579 |
o = variable_buffer_output (o, p2, ++p - p2); |
| 580 |
else if (p >= p2 && (*p == '.')) |
| 581 |
o = variable_buffer_output (o, p2, p - p2); |
| 582 |
else if (is_dir) |
| 583 |
o = variable_buffer_output (o, "./", 2); |
| 584 |
else |
| 585 |
|
| 586 |
o = variable_buffer_output (o, p2, len); |
| 587 |
|
| 588 |
o = variable_buffer_output (o, " ", 1); |
| 589 |
doneany = 1; |
| 590 |
} |
| 591 |
|
| 592 |
if (doneany) |
| 593 |
|
| 594 |
--o; |
| 595 |
|
| 596 |
return o; |
| 597 |
} |
| 598 |
|
| 599 |
static char * |
| 600 |
func_addsuffix_addprefix (char *o, char **argv, const char *funcname) |
| 601 |
{ |
| 602 |
size_t fixlen = strlen (argv[0]); |
| 603 |
const char *list_iterator = argv[1]; |
| 604 |
int is_addprefix = funcname[3] == 'p'; |
| 605 |
int is_addsuffix = !is_addprefix; |
| 606 |
|
| 607 |
int doneany = 0; |
| 608 |
const char *p; |
| 609 |
size_t len; |
| 610 |
|
| 611 |
while ((p = find_next_token (&list_iterator, &len)) != 0) |
| 612 |
{ |
| 613 |
if (is_addprefix) |
| 614 |
o = variable_buffer_output (o, argv[0], fixlen); |
| 615 |
o = variable_buffer_output (o, p, len); |
| 616 |
if (is_addsuffix) |
| 617 |
o = variable_buffer_output (o, argv[0], fixlen); |
| 618 |
o = variable_buffer_output (o, " ", 1); |
| 619 |
doneany = 1; |
| 620 |
} |
| 621 |
|
| 622 |
if (doneany) |
| 623 |
|
| 624 |
--o; |
| 625 |
|
| 626 |
return o; |
| 627 |
} |
| 628 |
|
| 629 |
static char * |
| 630 |
func_subst (char *o, char **argv, const char *funcname UNUSED) |
| 631 |
{ |
| 632 |
o = subst_expand (o, argv[2], argv[0], argv[1], strlen (argv[0]), |
| 633 |
strlen (argv[1]), 0); |
| 634 |
|
| 635 |
return o; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
static char * |
| 640 |
func_firstword (char *o, char **argv, const char *funcname UNUSED) |
| 641 |
{ |
| 642 |
size_t i; |
| 643 |
const char *words = argv[0]; |
| 644 |
const char *p = find_next_token (&words, &i); |
| 645 |
|
| 646 |
if (p != 0) |
| 647 |
o = variable_buffer_output (o, p, i); |
| 648 |
|
| 649 |
return o; |
| 650 |
} |
| 651 |
|
| 652 |
static char * |
| 653 |
func_lastword (char *o, char **argv, const char *funcname UNUSED) |
| 654 |
{ |
| 655 |
size_t i; |
| 656 |
const char *words = argv[0]; |
| 657 |
const char *p = NULL; |
| 658 |
const char *t; |
| 659 |
|
| 660 |
while ((t = find_next_token (&words, &i)) != NULL) |
| 661 |
p = t; |
| 662 |
|
| 663 |
if (p != 0) |
| 664 |
o = variable_buffer_output (o, p, i); |
| 665 |
|
| 666 |
return o; |
| 667 |
} |
| 668 |
|
| 669 |
static char * |
| 670 |
func_words (char *o, char **argv, const char *funcname UNUSED) |
| 671 |
{ |
| 672 |
int i = 0; |
| 673 |
const char *word_iterator = argv[0]; |
| 674 |
char buf[20]; |
| 675 |
|
| 676 |
while (find_next_token (&word_iterator, NULL) != 0) |
| 677 |
++i; |
| 678 |
|
| 679 |
sprintf (buf, "%d", i); |
| 680 |
o = variable_buffer_output (o, buf, strlen (buf)); |
| 681 |
|
| 682 |
return o; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
char * |
| 691 |
strip_whitespace (const char **begpp, const char **endpp) |
| 692 |
{ |
| 693 |
while (*begpp <= *endpp && ISSPACE (**begpp)) |
| 694 |
(*begpp) ++; |
| 695 |
while (*endpp >= *begpp && ISSPACE (**endpp)) |
| 696 |
(*endpp) --; |
| 697 |
return (char *)*begpp; |
| 698 |
} |
| 699 |
|
| 700 |
static void |
| 701 |
check_numeric (const char *s, const char *msg) |
| 702 |
{ |
| 703 |
const char *end = s + strlen (s) - 1; |
| 704 |
const char *beg = s; |
| 705 |
strip_whitespace (&s, &end); |
| 706 |
|
| 707 |
for (; s <= end; ++s) |
| 708 |
if (!ISDIGIT (*s)) |
| 709 |
break; |
| 710 |
|
| 711 |
if (s <= end || end - beg < 0) |
| 712 |
OSS (fatal, *expanding_var, "%s: '%s'", msg, beg); |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
static char * |
| 718 |
func_word (char *o, char **argv, const char *funcname UNUSED) |
| 719 |
{ |
| 720 |
const char *end_p; |
| 721 |
const char *p; |
| 722 |
int i; |
| 723 |
|
| 724 |
|
| 725 |
check_numeric (argv[0], _("non-numeric first argument to 'word' function")); |
| 726 |
i = atoi (argv[0]); |
| 727 |
|
| 728 |
if (i == 0) |
| 729 |
O (fatal, *expanding_var, |
| 730 |
_("first argument to 'word' function must be greater than 0")); |
| 731 |
|
| 732 |
end_p = argv[1]; |
| 733 |
while ((p = find_next_token (&end_p, 0)) != 0) |
| 734 |
if (--i == 0) |
| 735 |
break; |
| 736 |
|
| 737 |
if (i == 0) |
| 738 |
o = variable_buffer_output (o, p, end_p - p); |
| 739 |
|
| 740 |
return o; |
| 741 |
} |
| 742 |
|
| 743 |
static char * |
| 744 |
func_wordlist (char *o, char **argv, const char *funcname UNUSED) |
| 745 |
{ |
| 746 |
int start, count; |
| 747 |
|
| 748 |
|
| 749 |
check_numeric (argv[0], |
| 750 |
_("non-numeric first argument to 'wordlist' function")); |
| 751 |
check_numeric (argv[1], |
| 752 |
_("non-numeric second argument to 'wordlist' function")); |
| 753 |
|
| 754 |
start = atoi (argv[0]); |
| 755 |
if (start < 1) |
| 756 |
ON (fatal, *expanding_var, |
| 757 |
"invalid first argument to 'wordlist' function: '%d'", start); |
| 758 |
|
| 759 |
count = atoi (argv[1]) - start + 1; |
| 760 |
|
| 761 |
if (count > 0) |
| 762 |
{ |
| 763 |
const char *p; |
| 764 |
const char *end_p = argv[2]; |
| 765 |
|
| 766 |
|
| 767 |
while (((p = find_next_token (&end_p, 0)) != 0) && --start) |
| 768 |
; |
| 769 |
|
| 770 |
if (p) |
| 771 |
{ |
| 772 |
|
| 773 |
while (--count && (find_next_token (&end_p, 0) != 0)) |
| 774 |
; |
| 775 |
|
| 776 |
|
| 777 |
o = variable_buffer_output (o, p, end_p - p); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
return o; |
| 782 |
} |
| 783 |
|
| 784 |
static char * |
| 785 |
func_findstring (char *o, char **argv, const char *funcname UNUSED) |
| 786 |
{ |
| 787 |
|
| 788 |
if (strstr (argv[1], argv[0]) != 0) |
| 789 |
o = variable_buffer_output (o, argv[0], strlen (argv[0])); |
| 790 |
|
| 791 |
return o; |
| 792 |
} |
| 793 |
|
| 794 |
static char * |
| 795 |
func_foreach (char *o, char **argv, const char *funcname UNUSED) |
| 796 |
{ |
| 797 |
|
| 798 |
char *varname = expand_argument (argv[0], NULL); |
| 799 |
char *list = expand_argument (argv[1], NULL); |
| 800 |
const char *body = argv[2]; |
| 801 |
|
| 802 |
int doneany = 0; |
| 803 |
const char *list_iterator = list; |
| 804 |
const char *p; |
| 805 |
size_t len; |
| 806 |
struct variable *var; |
| 807 |
|
| 808 |
|
| 809 |
char *vp = next_token (varname); |
| 810 |
end_of_token (vp)[0] = '\0'; |
| 811 |
|
| 812 |
push_new_variable_scope (); |
| 813 |
var = define_variable (vp, strlen (vp), "", o_automatic, 0); |
| 814 |
|
| 815 |
|
| 816 |
while ((p = find_next_token (&list_iterator, &len)) != 0) |
| 817 |
{ |
| 818 |
char *result = 0; |
| 819 |
|
| 820 |
free (var->value); |
| 821 |
var->value = xstrndup (p, len); |
| 822 |
|
| 823 |
result = allocated_variable_expand (body); |
| 824 |
|
| 825 |
o = variable_buffer_output (o, result, strlen (result)); |
| 826 |
o = variable_buffer_output (o, " ", 1); |
| 827 |
doneany = 1; |
| 828 |
free (result); |
| 829 |
} |
| 830 |
|
| 831 |
if (doneany) |
| 832 |
|
| 833 |
--o; |
| 834 |
|
| 835 |
pop_variable_scope (); |
| 836 |
free (varname); |
| 837 |
free (list); |
| 838 |
|
| 839 |
return o; |
| 840 |
} |
| 841 |
|
| 842 |
struct a_word |
| 843 |
{ |
| 844 |
struct a_word *next; |
| 845 |
struct a_word *chain; |
| 846 |
char *str; |
| 847 |
size_t length; |
| 848 |
int matched; |
| 849 |
}; |
| 850 |
|
| 851 |
static unsigned long |
| 852 |
a_word_hash_1 (const void *key) |
| 853 |
{ |
| 854 |
return_STRING_HASH_1 (((struct a_word const *) key)->str); |
| 855 |
} |
| 856 |
|
| 857 |
static unsigned long |
| 858 |
a_word_hash_2 (const void *key) |
| 859 |
{ |
| 860 |
return_STRING_HASH_2 (((struct a_word const *) key)->str); |
| 861 |
} |
| 862 |
|
| 863 |
static int |
| 864 |
a_word_hash_cmp (const void *x, const void *y) |
| 865 |
{ |
| 866 |
int result = (int) ((struct a_word const *) x)->length - ((struct a_word const *) y)->length; |
| 867 |
if (result) |
| 868 |
return result; |
| 869 |
return_STRING_COMPARE (((struct a_word const *) x)->str, |
| 870 |
((struct a_word const *) y)->str); |
| 871 |
} |
| 872 |
|
| 873 |
struct a_pattern |
| 874 |
{ |
| 875 |
struct a_pattern *next; |
| 876 |
char *str; |
| 877 |
char *percent; |
| 878 |
size_t length; |
| 879 |
}; |
| 880 |
|
| 881 |
static char * |
| 882 |
func_filter_filterout (char *o, char **argv, const char *funcname) |
| 883 |
{ |
| 884 |
struct a_word *wordhead; |
| 885 |
struct a_word **wordtail; |
| 886 |
struct a_word *wp; |
| 887 |
struct a_pattern *pathead; |
| 888 |
struct a_pattern **pattail; |
| 889 |
struct a_pattern *pp; |
| 890 |
|
| 891 |
struct hash_table a_word_table; |
| 892 |
int is_filter = funcname[CSTRLEN ("filter")] == '\0'; |
| 893 |
const char *pat_iterator = argv[0]; |
| 894 |
const char *word_iterator = argv[1]; |
| 895 |
int literals = 0; |
| 896 |
int words = 0; |
| 897 |
int hashing = 0; |
| 898 |
char *p; |
| 899 |
size_t len; |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
pattail = &pathead; |
| 906 |
while ((p = find_next_token (&pat_iterator, &len)) != 0) |
| 907 |
{ |
| 908 |
struct a_pattern *pat = alloca (sizeof (struct a_pattern)); |
| 909 |
|
| 910 |
*pattail = pat; |
| 911 |
pattail = &pat->next; |
| 912 |
|
| 913 |
if (*pat_iterator != '\0') |
| 914 |
++pat_iterator; |
| 915 |
|
| 916 |
pat->str = p; |
| 917 |
p[len] = '\0'; |
| 918 |
pat->percent = find_percent (p); |
| 919 |
if (pat->percent == 0) |
| 920 |
literals++; |
| 921 |
|
| 922 |
|
| 923 |
pat->length = strlen (pat->str); |
| 924 |
} |
| 925 |
*pattail = 0; |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
wordtail = &wordhead; |
| 930 |
while ((p = find_next_token (&word_iterator, &len)) != 0) |
| 931 |
{ |
| 932 |
struct a_word *word = alloca (sizeof (struct a_word)); |
| 933 |
|
| 934 |
*wordtail = word; |
| 935 |
wordtail = &word->next; |
| 936 |
|
| 937 |
if (*word_iterator != '\0') |
| 938 |
++word_iterator; |
| 939 |
|
| 940 |
p[len] = '\0'; |
| 941 |
word->str = p; |
| 942 |
word->length = len; |
| 943 |
word->matched = 0; |
| 944 |
word->chain = 0; |
| 945 |
words++; |
| 946 |
} |
| 947 |
*wordtail = 0; |
| 948 |
|
| 949 |
|
| 950 |
hashing = (literals >= 2 && (literals * words) >= 10); |
| 951 |
if (hashing) |
| 952 |
{ |
| 953 |
hash_init (&a_word_table, words, a_word_hash_1, a_word_hash_2, |
| 954 |
a_word_hash_cmp); |
| 955 |
for (wp = wordhead; wp != 0; wp = wp->next) |
| 956 |
{ |
| 957 |
struct a_word *owp = hash_insert (&a_word_table, wp); |
| 958 |
if (owp) |
| 959 |
wp->chain = owp; |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
if (words) |
| 964 |
{ |
| 965 |
int doneany = 0; |
| 966 |
|
| 967 |
|
| 968 |
for (pp = pathead; pp != 0; pp = pp->next) |
| 969 |
{ |
| 970 |
if (pp->percent) |
| 971 |
for (wp = wordhead; wp != 0; wp = wp->next) |
| 972 |
wp->matched |= pattern_matches (pp->str, pp->percent, wp->str); |
| 973 |
else if (hashing) |
| 974 |
{ |
| 975 |
struct a_word a_word_key; |
| 976 |
a_word_key.str = pp->str; |
| 977 |
a_word_key.length = pp->length; |
| 978 |
wp = hash_find_item (&a_word_table, &a_word_key); |
| 979 |
while (wp) |
| 980 |
{ |
| 981 |
wp->matched |= 1; |
| 982 |
wp = wp->chain; |
| 983 |
} |
| 984 |
} |
| 985 |
else |
| 986 |
for (wp = wordhead; wp != 0; wp = wp->next) |
| 987 |
wp->matched |= (wp->length == pp->length |
| 988 |
&& strneq (pp->str, wp->str, wp->length)); |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
for (wp = wordhead; wp != 0; wp = wp->next) |
| 993 |
if (is_filter ? wp->matched : !wp->matched) |
| 994 |
{ |
| 995 |
o = variable_buffer_output (o, wp->str, strlen (wp->str)); |
| 996 |
o = variable_buffer_output (o, " ", 1); |
| 997 |
doneany = 1; |
| 998 |
} |
| 999 |
|
| 1000 |
if (doneany) |
| 1001 |
|
| 1002 |
--o; |
| 1003 |
} |
| 1004 |
|
| 1005 |
if (hashing) |
| 1006 |
hash_free (&a_word_table, 0); |
| 1007 |
|
| 1008 |
return o; |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
static char * |
| 1013 |
func_strip (char *o, char **argv, const char *funcname UNUSED) |
| 1014 |
{ |
| 1015 |
const char *p = argv[0]; |
| 1016 |
int doneany = 0; |
| 1017 |
|
| 1018 |
while (*p != '\0') |
| 1019 |
{ |
| 1020 |
int i=0; |
| 1021 |
const char *word_start; |
| 1022 |
|
| 1023 |
NEXT_TOKEN (p); |
| 1024 |
word_start = p; |
| 1025 |
for (i=0; *p != '\0' && !ISSPACE (*p); ++p, ++i) |
| 1026 |
{} |
| 1027 |
if (!i) |
| 1028 |
break; |
| 1029 |
o = variable_buffer_output (o, word_start, i); |
| 1030 |
o = variable_buffer_output (o, " ", 1); |
| 1031 |
doneany = 1; |
| 1032 |
} |
| 1033 |
|
| 1034 |
if (doneany) |
| 1035 |
|
| 1036 |
--o; |
| 1037 |
|
| 1038 |
return o; |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
static char * |
| 1045 |
func_error (char *o, char **argv, const char *funcname) |
| 1046 |
{ |
| 1047 |
char **argvp; |
| 1048 |
char *msg, *p; |
| 1049 |
size_t len; |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
for (len=0, argvp=argv; *argvp != 0; ++argvp) |
| 1055 |
len += strlen (*argvp) + 2; |
| 1056 |
|
| 1057 |
p = msg = alloca (len + 1); |
| 1058 |
msg[0] = '\0'; |
| 1059 |
|
| 1060 |
for (argvp=argv; argvp[1] != 0; ++argvp) |
| 1061 |
{ |
| 1062 |
strcpy (p, *argvp); |
| 1063 |
p += strlen (*argvp); |
| 1064 |
*(p++) = ','; |
| 1065 |
*(p++) = ' '; |
| 1066 |
} |
| 1067 |
strcpy (p, *argvp); |
| 1068 |
|
| 1069 |
switch (*funcname) |
| 1070 |
{ |
| 1071 |
case 'e': |
| 1072 |
OS (fatal, reading_file, "%s", msg); |
| 1073 |
|
| 1074 |
case 'w': |
| 1075 |
OS (error, reading_file, "%s", msg); |
| 1076 |
break; |
| 1077 |
|
| 1078 |
case 'i': |
| 1079 |
outputs (0, msg); |
| 1080 |
outputs (0, "\n"); |
| 1081 |
break; |
| 1082 |
|
| 1083 |
default: |
| 1084 |
OS (fatal, *expanding_var, "Internal error: func_error: '%s'", funcname); |
| 1085 |
} |
| 1086 |
|
| 1087 |
|
| 1088 |
return o; |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
static char * |
| 1096 |
func_sort (char *o, char **argv, const char *funcname UNUSED) |
| 1097 |
{ |
| 1098 |
const char *t; |
| 1099 |
char **words; |
| 1100 |
int wordi; |
| 1101 |
char *p; |
| 1102 |
size_t len; |
| 1103 |
|
| 1104 |
|
| 1105 |
t = argv[0]; |
| 1106 |
wordi = 0; |
| 1107 |
while ((p = find_next_token (&t, NULL)) != 0) |
| 1108 |
{ |
| 1109 |
++t; |
| 1110 |
++wordi; |
| 1111 |
} |
| 1112 |
|
| 1113 |
words = xmalloc ((wordi == 0 ? 1 : wordi) * sizeof (char *)); |
| 1114 |
|
| 1115 |
|
| 1116 |
t = argv[0]; |
| 1117 |
wordi = 0; |
| 1118 |
while ((p = find_next_token (&t, &len)) != 0) |
| 1119 |
{ |
| 1120 |
++t; |
| 1121 |
p[len] = '\0'; |
| 1122 |
words[wordi++] = p; |
| 1123 |
} |
| 1124 |
|
| 1125 |
if (wordi) |
| 1126 |
{ |
| 1127 |
int i; |
| 1128 |
|
| 1129 |
|
| 1130 |
qsort (words, wordi, sizeof (char *), alpha_compare); |
| 1131 |
|
| 1132 |
|
| 1133 |
for (i = 0; i < wordi; ++i) |
| 1134 |
{ |
| 1135 |
len = strlen (words[i]); |
| 1136 |
if (i == wordi - 1 || strlen (words[i + 1]) != len |
| 1137 |
|| strcmp (words[i], words[i + 1])) |
| 1138 |
{ |
| 1139 |
o = variable_buffer_output (o, words[i], len); |
| 1140 |
o = variable_buffer_output (o, " ", 1); |
| 1141 |
} |
| 1142 |
} |
| 1143 |
|
| 1144 |
|
| 1145 |
--o; |
| 1146 |
} |
| 1147 |
|
| 1148 |
free (words); |
| 1149 |
|
| 1150 |
return o; |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
static char * |
| 1166 |
func_if (char *o, char **argv, const char *funcname UNUSED) |
| 1167 |
{ |
| 1168 |
const char *begp = argv[0]; |
| 1169 |
const char *endp = begp + strlen (argv[0]) - 1; |
| 1170 |
int result = 0; |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
strip_whitespace (&begp, &endp); |
| 1177 |
|
| 1178 |
if (begp <= endp) |
| 1179 |
{ |
| 1180 |
char *expansion = expand_argument (begp, endp+1); |
| 1181 |
|
| 1182 |
result = expansion[0] != '\0'; |
| 1183 |
free (expansion); |
| 1184 |
} |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
argv += 1 + !result; |
| 1191 |
|
| 1192 |
if (*argv) |
| 1193 |
{ |
| 1194 |
char *expansion = expand_argument (*argv, NULL); |
| 1195 |
|
| 1196 |
o = variable_buffer_output (o, expansion, strlen (expansion)); |
| 1197 |
|
| 1198 |
free (expansion); |
| 1199 |
} |
| 1200 |
|
| 1201 |
return o; |
| 1202 |
} |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
static char * |
| 1219 |
func_or (char *o, char **argv, const char *funcname UNUSED) |
| 1220 |
{ |
| 1221 |
for ( ; *argv ; ++argv) |
| 1222 |
{ |
| 1223 |
const char *begp = *argv; |
| 1224 |
const char *endp = begp + strlen (*argv) - 1; |
| 1225 |
char *expansion; |
| 1226 |
size_t result = 0; |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
strip_whitespace (&begp, &endp); |
| 1231 |
|
| 1232 |
if (begp > endp) |
| 1233 |
continue; |
| 1234 |
|
| 1235 |
expansion = expand_argument (begp, endp+1); |
| 1236 |
result = strlen (expansion); |
| 1237 |
|
| 1238 |
|
| 1239 |
if (!result) |
| 1240 |
{ |
| 1241 |
free (expansion); |
| 1242 |
continue; |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
o = variable_buffer_output (o, expansion, result); |
| 1247 |
free (expansion); |
| 1248 |
break; |
| 1249 |
} |
| 1250 |
|
| 1251 |
return o; |
| 1252 |
} |
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
static char * |
| 1269 |
func_and (char *o, char **argv, const char *funcname UNUSED) |
| 1270 |
{ |
| 1271 |
char *expansion; |
| 1272 |
|
| 1273 |
while (1) |
| 1274 |
{ |
| 1275 |
const char *begp = *argv; |
| 1276 |
const char *endp = begp + strlen (*argv) - 1; |
| 1277 |
size_t result; |
| 1278 |
|
| 1279 |
|
| 1280 |
strip_whitespace (&begp, &endp); |
| 1281 |
if (begp > endp) |
| 1282 |
return o; |
| 1283 |
|
| 1284 |
expansion = expand_argument (begp, endp+1); |
| 1285 |
result = strlen (expansion); |
| 1286 |
|
| 1287 |
|
| 1288 |
if (!result) |
| 1289 |
break; |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
if (*(++argv)) |
| 1295 |
free (expansion); |
| 1296 |
else |
| 1297 |
{ |
| 1298 |
o = variable_buffer_output (o, expansion, result); |
| 1299 |
break; |
| 1300 |
} |
| 1301 |
} |
| 1302 |
|
| 1303 |
free (expansion); |
| 1304 |
|
| 1305 |
return o; |
| 1306 |
} |
| 1307 |
|
| 1308 |
static char * |
| 1309 |
func_wildcard (char *o, char **argv, const char *funcname UNUSED) |
| 1310 |
{ |
| 1311 |
char *p = string_glob (argv[0]); |
| 1312 |
o = variable_buffer_output (o, p, strlen (p)); |
| 1313 |
return o; |
| 1314 |
} |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
char * |
| 1325 |
func_eval (char *o, char **argv, const char *funcname UNUSED) |
| 1326 |
{ |
| 1327 |
char *buf; |
| 1328 |
size_t len; |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
install_variable_buffer (&buf, &len); |
| 1334 |
|
| 1335 |
eval_buffer (argv[0], NULL); |
| 1336 |
|
| 1337 |
restore_variable_buffer (buf, len); |
| 1338 |
|
| 1339 |
return o; |
| 1340 |
} |
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
static char * |
| 1352 |
func_debugger (char *o, char **argv, const char *funcname UNUSED) |
| 1353 |
{ |
| 1354 |
printf("debugger() function called with parameter %s\n", argv[0]); |
| 1355 |
(void) enter_debugger(p_stack_top, NULL, 0, DEBUG_EXPLICIT_CALL); |
| 1356 |
o = variable_buffer_output (o, "", 0); |
| 1357 |
return o; |
| 1358 |
} |
| 1359 |
|
| 1360 |
static char * |
| 1361 |
func_value (char *o, char **argv, const char *funcname UNUSED) |
| 1362 |
{ |
| 1363 |
|
| 1364 |
struct variable *v = lookup_variable (argv[0], strlen (argv[0])); |
| 1365 |
|
| 1366 |
|
| 1367 |
if (v) |
| 1368 |
o = variable_buffer_output (o, v->value, strlen (v->value)); |
| 1369 |
|
| 1370 |
return o; |
| 1371 |
} |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
static void |
| 1377 |
fold_newlines (char *buffer, size_t *length, int trim_newlines) |
| 1378 |
{ |
| 1379 |
char *dst = buffer; |
| 1380 |
char *src = buffer; |
| 1381 |
char *last_nonnl = buffer - 1; |
| 1382 |
src[*length] = 0; |
| 1383 |
for (; *src != '\0'; ++src) |
| 1384 |
{ |
| 1385 |
if (src[0] == '\r' && src[1] == '\n') |
| 1386 |
continue; |
| 1387 |
if (*src == '\n') |
| 1388 |
{ |
| 1389 |
*dst++ = ' '; |
| 1390 |
} |
| 1391 |
else |
| 1392 |
{ |
| 1393 |
last_nonnl = dst; |
| 1394 |
*dst++ = *src; |
| 1395 |
} |
| 1396 |
} |
| 1397 |
|
| 1398 |
if (!trim_newlines && (last_nonnl < (dst - 2))) |
| 1399 |
last_nonnl = dst - 2; |
| 1400 |
|
| 1401 |
*(++last_nonnl) = '\0'; |
| 1402 |
*length = last_nonnl - buffer; |
| 1403 |
} |
| 1404 |
|
| 1405 |
pid_t shell_function_pid = 0; |
| 1406 |
static int shell_function_completed; |
| 1407 |
|
| 1408 |
void |
| 1409 |
shell_completed (int exit_code, int exit_sig) |
| 1410 |
{ |
| 1411 |
char buf[256]; |
| 1412 |
|
| 1413 |
shell_function_pid = 0; |
| 1414 |
if (exit_sig == 0 && exit_code == 127) |
| 1415 |
shell_function_completed = -1; |
| 1416 |
else |
| 1417 |
shell_function_completed = 1; |
| 1418 |
|
| 1419 |
if (exit_code == 0 && exit_sig > 0) |
| 1420 |
exit_code = 128 + exit_sig; |
| 1421 |
|
| 1422 |
sprintf (buf, "%d", exit_code); |
| 1423 |
define_variable_cname (".SHELLSTATUS", buf, o_override, 0); |
| 1424 |
} |
| 1425 |
|
| 1426 |
#ifdef WINDOWS32 |
| 1427 |
|
| 1428 |
|
| 1429 |
#include <windows.h> |
| 1430 |
#include <io.h> |
| 1431 |
#include "sub_proc.h" |
| 1432 |
|
| 1433 |
|
| 1434 |
int |
| 1435 |
windows32_openpipe (int *pipedes, int errfd, pid_t *pid_p, char **command_argv, char **envp) |
| 1436 |
{ |
| 1437 |
SECURITY_ATTRIBUTES saAttr; |
| 1438 |
HANDLE hIn = INVALID_HANDLE_VALUE; |
| 1439 |
HANDLE hErr = INVALID_HANDLE_VALUE; |
| 1440 |
HANDLE hChildOutRd; |
| 1441 |
HANDLE hChildOutWr; |
| 1442 |
HANDLE hProcess, tmpIn, tmpErr; |
| 1443 |
DWORD e; |
| 1444 |
|
| 1445 |
|
| 1446 |
pipedes[0] = pipedes[1] = -1; |
| 1447 |
*pid_p = (pid_t)-1; |
| 1448 |
|
| 1449 |
saAttr.nLength = sizeof (SECURITY_ATTRIBUTES); |
| 1450 |
saAttr.bInheritHandle = TRUE; |
| 1451 |
saAttr.lpSecurityDescriptor = NULL; |
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
tmpIn = GetStdHandle (STD_INPUT_HANDLE); |
| 1458 |
if (DuplicateHandle (GetCurrentProcess (), tmpIn, |
| 1459 |
GetCurrentProcess (), &hIn, |
| 1460 |
0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE) |
| 1461 |
{ |
| 1462 |
e = GetLastError (); |
| 1463 |
if (e == ERROR_INVALID_HANDLE) |
| 1464 |
{ |
| 1465 |
tmpIn = CreateFile ("NUL", GENERIC_READ, |
| 1466 |
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1467 |
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 1468 |
if (tmpIn != INVALID_HANDLE_VALUE |
| 1469 |
&& DuplicateHandle (GetCurrentProcess (), tmpIn, |
| 1470 |
GetCurrentProcess (), &hIn, |
| 1471 |
0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE) |
| 1472 |
CloseHandle (tmpIn); |
| 1473 |
} |
| 1474 |
if (hIn == INVALID_HANDLE_VALUE) |
| 1475 |
{ |
| 1476 |
ON (error, NILF, |
| 1477 |
_("windows32_openpipe: DuplicateHandle(In) failed (e=%ld)\n"), e); |
| 1478 |
return -1; |
| 1479 |
} |
| 1480 |
} |
| 1481 |
tmpErr = (HANDLE)_get_osfhandle (errfd); |
| 1482 |
if (DuplicateHandle (GetCurrentProcess (), tmpErr, |
| 1483 |
GetCurrentProcess (), &hErr, |
| 1484 |
0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE) |
| 1485 |
{ |
| 1486 |
e = GetLastError (); |
| 1487 |
if (e == ERROR_INVALID_HANDLE) |
| 1488 |
{ |
| 1489 |
tmpErr = CreateFile ("NUL", GENERIC_WRITE, |
| 1490 |
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1491 |
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 1492 |
if (tmpErr != INVALID_HANDLE_VALUE |
| 1493 |
&& DuplicateHandle (GetCurrentProcess (), tmpErr, |
| 1494 |
GetCurrentProcess (), &hErr, |
| 1495 |
0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE) |
| 1496 |
CloseHandle (tmpErr); |
| 1497 |
} |
| 1498 |
if (hErr == INVALID_HANDLE_VALUE) |
| 1499 |
{ |
| 1500 |
ON (error, NILF, |
| 1501 |
_("windows32_openpipe: DuplicateHandle(Err) failed (e=%ld)\n"), e); |
| 1502 |
return -1; |
| 1503 |
} |
| 1504 |
} |
| 1505 |
|
| 1506 |
if (! CreatePipe (&hChildOutRd, &hChildOutWr, &saAttr, 0)) |
| 1507 |
{ |
| 1508 |
ON (error, NILF, _("CreatePipe() failed (e=%ld)\n"), GetLastError()); |
| 1509 |
return -1; |
| 1510 |
} |
| 1511 |
|
| 1512 |
hProcess = process_init_fd (hIn, hChildOutWr, hErr); |
| 1513 |
|
| 1514 |
if (!hProcess) |
| 1515 |
{ |
| 1516 |
O (error, NILF, _("windows32_openpipe(): process_init_fd() failed\n")); |
| 1517 |
return -1; |
| 1518 |
} |
| 1519 |
|
| 1520 |
|
| 1521 |
sync_Path_environment (); |
| 1522 |
|
| 1523 |
|
| 1524 |
envp = environ; |
| 1525 |
|
| 1526 |
if (! process_begin (hProcess, command_argv, envp, command_argv[0], NULL)) |
| 1527 |
{ |
| 1528 |
|
| 1529 |
process_register (hProcess); |
| 1530 |
|
| 1531 |
|
| 1532 |
*pid_p = (pid_t) hProcess; |
| 1533 |
|
| 1534 |
|
| 1535 |
pipedes[0] = _open_osfhandle ((intptr_t) hChildOutRd, O_RDONLY); |
| 1536 |
|
| 1537 |
|
| 1538 |
pipedes[1] = _open_osfhandle ((intptr_t) hChildOutWr, O_APPEND); |
| 1539 |
return 0; |
| 1540 |
} |
| 1541 |
else |
| 1542 |
{ |
| 1543 |
|
| 1544 |
process_cleanup (hProcess); |
| 1545 |
|
| 1546 |
|
| 1547 |
if (hIn != INVALID_HANDLE_VALUE) |
| 1548 |
CloseHandle (hIn); |
| 1549 |
if (hErr != INVALID_HANDLE_VALUE) |
| 1550 |
CloseHandle (hErr); |
| 1551 |
|
| 1552 |
|
| 1553 |
CloseHandle (hChildOutRd); |
| 1554 |
CloseHandle (hChildOutWr); |
| 1555 |
|
| 1556 |
return -1; |
| 1557 |
} |
| 1558 |
} |
| 1559 |
#endif |
| 1560 |
|
| 1561 |
|
| 1562 |
#ifdef __MSDOS__ |
| 1563 |
FILE * |
| 1564 |
msdos_openpipe (int* pipedes, int *pidp, char *text) |
| 1565 |
{ |
| 1566 |
FILE *fpipe=0; |
| 1567 |
|
| 1568 |
struct variable *sh = lookup_variable ("SHELL", 5); |
| 1569 |
int e; |
| 1570 |
extern int dos_command_running, dos_status; |
| 1571 |
|
| 1572 |
|
| 1573 |
NEXT_TOKEN (text); |
| 1574 |
if (*text == '\0') |
| 1575 |
return 0; |
| 1576 |
|
| 1577 |
if (sh) |
| 1578 |
{ |
| 1579 |
char buf[PATH_MAX + 7]; |
| 1580 |
|
| 1581 |
|
| 1582 |
sprintf (buf, "SHELL=%s", sh->value); |
| 1583 |
putenv (buf); |
| 1584 |
} |
| 1585 |
|
| 1586 |
e = errno; |
| 1587 |
errno = 0; |
| 1588 |
dos_command_running = 1; |
| 1589 |
dos_status = 0; |
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
fpipe = popen (text, "rt"); |
| 1594 |
dos_command_running = 0; |
| 1595 |
if (!fpipe || dos_status) |
| 1596 |
{ |
| 1597 |
pipedes[0] = -1; |
| 1598 |
*pidp = -1; |
| 1599 |
if (dos_status) |
| 1600 |
errno = EINTR; |
| 1601 |
else if (errno == 0) |
| 1602 |
errno = ENOMEM; |
| 1603 |
if (fpipe) |
| 1604 |
pclose (fpipe); |
| 1605 |
shell_completed (127, 0); |
| 1606 |
} |
| 1607 |
else |
| 1608 |
{ |
| 1609 |
pipedes[0] = fileno (fpipe); |
| 1610 |
*pidp = 42; |
| 1611 |
errno = e; |
| 1612 |
} |
| 1613 |
return fpipe; |
| 1614 |
} |
| 1615 |
#endif |
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
char * |
| 1622 |
func_shell_base (char *o, char **argv, int trim_newlines) |
| 1623 |
{ |
| 1624 |
char *batch_filename = NULL; |
| 1625 |
int errfd; |
| 1626 |
char **command_argv = NULL; |
| 1627 |
char **envp; |
| 1628 |
int pipedes[2]; |
| 1629 |
pid_t pid; |
| 1630 |
|
| 1631 |
#ifndef __MSDOS__ |
| 1632 |
#ifdef WINDOWS32 |
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
int j_p_f = just_print_flag; |
| 1637 |
just_print_flag = 0; |
| 1638 |
#endif |
| 1639 |
|
| 1640 |
|
| 1641 |
command_argv = construct_command_argv (argv[0], NULL, NULL, 0, |
| 1642 |
&batch_filename); |
| 1643 |
if (command_argv == 0) |
| 1644 |
{ |
| 1645 |
#ifdef WINDOWS32 |
| 1646 |
just_print_flag = j_p_f; |
| 1647 |
#endif |
| 1648 |
return o; |
| 1649 |
} |
| 1650 |
#endif |
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
envp = environ; |
| 1665 |
|
| 1666 |
|
| 1667 |
output_start (); |
| 1668 |
|
| 1669 |
errfd = (output_context && output_context->err >= 0 |
| 1670 |
? output_context->err : FD_STDERR); |
| 1671 |
|
| 1672 |
#if defined(__MSDOS__) |
| 1673 |
fpipe = msdos_openpipe (pipedes, &pid, argv[0]); |
| 1674 |
if (pipedes[0] < 0) |
| 1675 |
{ |
| 1676 |
OS (error, reading_file, "pipe: %s", strerror (errno)); |
| 1677 |
pid = -1; |
| 1678 |
goto done; |
| 1679 |
} |
| 1680 |
|
| 1681 |
#elif defined(WINDOWS32) |
| 1682 |
windows32_openpipe (pipedes, errfd, &pid, command_argv, envp); |
| 1683 |
|
| 1684 |
just_print_flag = j_p_f; |
| 1685 |
|
| 1686 |
if (pipedes[0] < 0) |
| 1687 |
{ |
| 1688 |
|
| 1689 |
shell_completed (127, 0); |
| 1690 |
OS (error, reading_file, "pipe: %s", strerror (errno)); |
| 1691 |
pid = -1; |
| 1692 |
goto done; |
| 1693 |
} |
| 1694 |
|
| 1695 |
#else |
| 1696 |
if (pipe (pipedes) < 0) |
| 1697 |
{ |
| 1698 |
OS (error, reading_file, "pipe: %s", strerror (errno)); |
| 1699 |
pid = -1; |
| 1700 |
goto done; |
| 1701 |
} |
| 1702 |
|
| 1703 |
|
| 1704 |
fd_noinherit (pipedes[1]); |
| 1705 |
fd_noinherit (pipedes[0]); |
| 1706 |
|
| 1707 |
{ |
| 1708 |
struct childbase child; |
| 1709 |
child.cmd_name = NULL; |
| 1710 |
child.output.syncout = 1; |
| 1711 |
child.output.out = pipedes[1]; |
| 1712 |
child.output.err = errfd; |
| 1713 |
child.environment = envp; |
| 1714 |
|
| 1715 |
pid = child_execute_job (&child, 1, command_argv); |
| 1716 |
|
| 1717 |
free (child.cmd_name); |
| 1718 |
} |
| 1719 |
|
| 1720 |
if (pid < 0) |
| 1721 |
{ |
| 1722 |
shell_completed (127, 0); |
| 1723 |
goto done; |
| 1724 |
} |
| 1725 |
#endif |
| 1726 |
|
| 1727 |
{ |
| 1728 |
char *buffer; |
| 1729 |
size_t maxlen, i; |
| 1730 |
int cc; |
| 1731 |
|
| 1732 |
|
| 1733 |
shell_function_pid = pid; |
| 1734 |
shell_function_completed = 0; |
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
if (pipedes[1] >= 0) |
| 1740 |
close (pipedes[1]); |
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
maxlen = 200; |
| 1745 |
buffer = xmalloc (maxlen + 1); |
| 1746 |
|
| 1747 |
|
| 1748 |
for (i = 0; ; i += cc) |
| 1749 |
{ |
| 1750 |
if (i == maxlen) |
| 1751 |
{ |
| 1752 |
maxlen += 512; |
| 1753 |
buffer = xrealloc (buffer, maxlen + 1); |
| 1754 |
} |
| 1755 |
|
| 1756 |
EINTRLOOP (cc, read (pipedes[0], &buffer[i], maxlen - i)); |
| 1757 |
if (cc <= 0) |
| 1758 |
break; |
| 1759 |
} |
| 1760 |
buffer[i] = '\0'; |
| 1761 |
|
| 1762 |
|
| 1763 |
(void) close (pipedes[0]); |
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
while (shell_function_completed == 0) |
| 1768 |
reap_children (1, 0, NULL); |
| 1769 |
|
| 1770 |
if (batch_filename) |
| 1771 |
{ |
| 1772 |
DB (DB_VERBOSE, (_("Cleaning up temporary batch file %s\n"), |
| 1773 |
batch_filename)); |
| 1774 |
remove (batch_filename); |
| 1775 |
free (batch_filename); |
| 1776 |
} |
| 1777 |
shell_function_pid = 0; |
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
if (shell_function_completed == -1) |
| 1784 |
{ |
| 1785 |
|
| 1786 |
|
| 1787 |
fputs (buffer, stderr); |
| 1788 |
fflush (stderr); |
| 1789 |
} |
| 1790 |
else |
| 1791 |
{ |
| 1792 |
|
| 1793 |
|
| 1794 |
fold_newlines (buffer, &i, trim_newlines); |
| 1795 |
o = variable_buffer_output (o, buffer, i); |
| 1796 |
} |
| 1797 |
|
| 1798 |
free (buffer); |
| 1799 |
} |
| 1800 |
|
| 1801 |
done: |
| 1802 |
if (command_argv) |
| 1803 |
{ |
| 1804 |
|
| 1805 |
free (command_argv[0]); |
| 1806 |
free (command_argv); |
| 1807 |
} |
| 1808 |
|
| 1809 |
return o; |
| 1810 |
} |
| 1811 |
|
| 1812 |
static char * |
| 1813 |
func_shell (char *o, char **argv, const char *funcname UNUSED) |
| 1814 |
{ |
| 1815 |
return func_shell_base (o, argv, 1); |
| 1816 |
} |
| 1817 |
|
| 1818 |
#ifdef EXPERIMENTAL |
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
static char * |
| 1824 |
func_eq (char *o, char **argv, char *funcname UNUSED) |
| 1825 |
{ |
| 1826 |
int result = ! strcmp (argv[0], argv[1]); |
| 1827 |
o = variable_buffer_output (o, result ? "1" : "", result); |
| 1828 |
return o; |
| 1829 |
} |
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
static char * |
| 1836 |
func_not (char *o, char **argv, char *funcname UNUSED) |
| 1837 |
{ |
| 1838 |
const char *s = argv[0]; |
| 1839 |
int result = 0; |
| 1840 |
NEXT_TOKEN (s); |
| 1841 |
result = ! (*s); |
| 1842 |
o = variable_buffer_output (o, result ? "1" : "", result); |
| 1843 |
return o; |
| 1844 |
} |
| 1845 |
#endif |
| 1846 |
|
| 1847 |
|
| 1848 |
#ifdef HAVE_DOS_PATHS |
| 1849 |
# ifdef __CYGWIN__ |
| 1850 |
# define IS_ABSOLUTE(n) ((n[0] && n[1] == ':') || STOP_SET (n[0], MAP_DIRSEP)) |
| 1851 |
# else |
| 1852 |
# define IS_ABSOLUTE(n) (n[0] && n[1] == ':') |
| 1853 |
# endif |
| 1854 |
# define ROOT_LEN 3 |
| 1855 |
#else |
| 1856 |
# define IS_ABSOLUTE(n) (n[0] == '/') |
| 1857 |
# define ROOT_LEN 1 |
| 1858 |
#endif |
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
static char * |
| 1864 |
abspath (const char *name, char *apath) |
| 1865 |
{ |
| 1866 |
char *dest; |
| 1867 |
const char *start, *end, *apath_limit; |
| 1868 |
unsigned long root_len = ROOT_LEN; |
| 1869 |
|
| 1870 |
if (name[0] == '\0') |
| 1871 |
return NULL; |
| 1872 |
|
| 1873 |
apath_limit = apath + GET_PATH_MAX; |
| 1874 |
|
| 1875 |
if (!IS_ABSOLUTE(name)) |
| 1876 |
{ |
| 1877 |
|
| 1878 |
if (!starting_directory) |
| 1879 |
return NULL; |
| 1880 |
|
| 1881 |
strcpy (apath, starting_directory); |
| 1882 |
|
| 1883 |
#ifdef HAVE_DOS_PATHS |
| 1884 |
if (STOP_SET (name[0], MAP_DIRSEP)) |
| 1885 |
{ |
| 1886 |
if (STOP_SET (name[1], MAP_DIRSEP)) |
| 1887 |
{ |
| 1888 |
|
| 1889 |
apath[0] = name[0]; |
| 1890 |
apath[1] = name[1]; |
| 1891 |
root_len = 2; |
| 1892 |
} |
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
apath[root_len] = '\0'; |
| 1898 |
} |
| 1899 |
#endif |
| 1900 |
|
| 1901 |
dest = strchr (apath, '\0'); |
| 1902 |
} |
| 1903 |
else |
| 1904 |
{ |
| 1905 |
#if defined(__CYGWIN__) && defined(HAVE_DOS_PATHS) |
| 1906 |
if (STOP_SET (name[0], MAP_DIRSEP)) |
| 1907 |
root_len = 1; |
| 1908 |
#endif |
| 1909 |
memcpy (apath, name, root_len); |
| 1910 |
apath[root_len] = '\0'; |
| 1911 |
dest = apath + root_len; |
| 1912 |
|
| 1913 |
name += root_len; |
| 1914 |
#ifdef HAVE_DOS_PATHS |
| 1915 |
if (! STOP_SET (apath[root_len - 1], MAP_DIRSEP)) |
| 1916 |
{ |
| 1917 |
|
| 1918 |
apath[2] = '.'; |
| 1919 |
apath[3] = '/'; |
| 1920 |
dest++; |
| 1921 |
root_len++; |
| 1922 |
|
| 1923 |
name--; |
| 1924 |
} |
| 1925 |
else |
| 1926 |
apath[root_len - 1] = '/'; |
| 1927 |
#endif |
| 1928 |
} |
| 1929 |
|
| 1930 |
for (start = end = name; *start != '\0'; start = end) |
| 1931 |
{ |
| 1932 |
size_t len; |
| 1933 |
|
| 1934 |
|
| 1935 |
while (STOP_SET (*start, MAP_DIRSEP)) |
| 1936 |
++start; |
| 1937 |
|
| 1938 |
|
| 1939 |
for (end = start; ! STOP_SET (*end, MAP_DIRSEP|MAP_NUL); ++end) |
| 1940 |
; |
| 1941 |
|
| 1942 |
len = end - start; |
| 1943 |
|
| 1944 |
if (len == 0) |
| 1945 |
break; |
| 1946 |
else if (len == 1 && start[0] == '.') |
| 1947 |
; |
| 1948 |
else if (len == 2 && start[0] == '.' && start[1] == '.') |
| 1949 |
{ |
| 1950 |
|
| 1951 |
if (dest > apath + root_len) |
| 1952 |
for (--dest; ! STOP_SET (dest[-1], MAP_DIRSEP); --dest) |
| 1953 |
; |
| 1954 |
} |
| 1955 |
else |
| 1956 |
{ |
| 1957 |
if (! STOP_SET (dest[-1], MAP_DIRSEP)) |
| 1958 |
*dest++ = '/'; |
| 1959 |
|
| 1960 |
if (dest + len >= apath_limit) |
| 1961 |
return NULL; |
| 1962 |
|
| 1963 |
dest = memcpy (dest, start, len); |
| 1964 |
dest += len; |
| 1965 |
*dest = '\0'; |
| 1966 |
} |
| 1967 |
} |
| 1968 |
|
| 1969 |
|
| 1970 |
if (dest > apath + root_len && STOP_SET (dest[-1], MAP_DIRSEP)) |
| 1971 |
--dest; |
| 1972 |
|
| 1973 |
*dest = '\0'; |
| 1974 |
|
| 1975 |
return apath; |
| 1976 |
} |
| 1977 |
|
| 1978 |
|
| 1979 |
static char * |
| 1980 |
func_realpath (char *o, char **argv, const char *funcname UNUSED) |
| 1981 |
{ |
| 1982 |
|
| 1983 |
const char *p = argv[0]; |
| 1984 |
const char *path = 0; |
| 1985 |
int doneany = 0; |
| 1986 |
size_t len = 0; |
| 1987 |
|
| 1988 |
while ((path = find_next_token (&p, &len)) != 0) |
| 1989 |
{ |
| 1990 |
if (len < GET_PATH_MAX) |
| 1991 |
{ |
| 1992 |
char *rp; |
| 1993 |
struct stat st; |
| 1994 |
PATH_VAR (in); |
| 1995 |
PATH_VAR (out); |
| 1996 |
|
| 1997 |
strncpy (in, path, len); |
| 1998 |
in[len] = '\0'; |
| 1999 |
|
| 2000 |
#ifdef HAVE_REALPATH |
| 2001 |
ENULLLOOP (rp, realpath (in, out)); |
| 2002 |
# if defined _AIX |
| 2003 |
|
| 2004 |
if (rp) |
| 2005 |
{ |
| 2006 |
char *ep = rp + strlen (rp) - 1; |
| 2007 |
while (ep > rp && ep[0] == '/') |
| 2008 |
*(ep--) = '\0'; |
| 2009 |
} |
| 2010 |
# endif |
| 2011 |
#else |
| 2012 |
rp = abspath (in, out); |
| 2013 |
#endif |
| 2014 |
|
| 2015 |
if (rp) |
| 2016 |
{ |
| 2017 |
int r; |
| 2018 |
EINTRLOOP (r, stat (out, &st)); |
| 2019 |
if (r == 0) |
| 2020 |
{ |
| 2021 |
o = variable_buffer_output (o, out, strlen (out)); |
| 2022 |
o = variable_buffer_output (o, " ", 1); |
| 2023 |
doneany = 1; |
| 2024 |
} |
| 2025 |
} |
| 2026 |
} |
| 2027 |
} |
| 2028 |
|
| 2029 |
|
| 2030 |
if (doneany) |
| 2031 |
--o; |
| 2032 |
|
| 2033 |
return o; |
| 2034 |
} |
| 2035 |
|
| 2036 |
static char * |
| 2037 |
func_file (char *o, char **argv, const char *funcname UNUSED) |
| 2038 |
{ |
| 2039 |
char *fn = argv[0]; |
| 2040 |
|
| 2041 |
if (fn[0] == '>') |
| 2042 |
{ |
| 2043 |
FILE *fp; |
| 2044 |
const char *mode = "w"; |
| 2045 |
|
| 2046 |
|
| 2047 |
++fn; |
| 2048 |
if (fn[0] == '>') |
| 2049 |
{ |
| 2050 |
mode = "a"; |
| 2051 |
++fn; |
| 2052 |
} |
| 2053 |
NEXT_TOKEN (fn); |
| 2054 |
|
| 2055 |
if (fn[0] == '\0') |
| 2056 |
O (fatal, *expanding_var, _("file: missing filename")); |
| 2057 |
|
| 2058 |
ENULLLOOP (fp, fopen (fn, mode)); |
| 2059 |
if (fp == NULL) |
| 2060 |
OSS (fatal, reading_file, _("open: %s: %s"), fn, strerror (errno)); |
| 2061 |
|
| 2062 |
if (argv[1]) |
| 2063 |
{ |
| 2064 |
size_t l = strlen (argv[1]); |
| 2065 |
int nl = l == 0 || argv[1][l-1] != '\n'; |
| 2066 |
|
| 2067 |
if (fputs (argv[1], fp) == EOF || (nl && fputc ('\n', fp) == EOF)) |
| 2068 |
OSS (fatal, reading_file, _("write: %s: %s"), fn, strerror (errno)); |
| 2069 |
} |
| 2070 |
if (fclose (fp)) |
| 2071 |
OSS (fatal, reading_file, _("close: %s: %s"), fn, strerror (errno)); |
| 2072 |
} |
| 2073 |
else if (fn[0] == '<') |
| 2074 |
{ |
| 2075 |
char *preo = o; |
| 2076 |
FILE *fp; |
| 2077 |
|
| 2078 |
++fn; |
| 2079 |
NEXT_TOKEN (fn); |
| 2080 |
if (fn[0] == '\0') |
| 2081 |
O (fatal, *expanding_var, _("file: missing filename")); |
| 2082 |
|
| 2083 |
if (argv[1]) |
| 2084 |
O (fatal, *expanding_var, _("file: too many arguments")); |
| 2085 |
|
| 2086 |
ENULLLOOP (fp, fopen (fn, "r")); |
| 2087 |
if (fp == NULL) |
| 2088 |
{ |
| 2089 |
if (errno == ENOENT) |
| 2090 |
return o; |
| 2091 |
OSS (fatal, reading_file, _("open: %s: %s"), fn, strerror (errno)); |
| 2092 |
} |
| 2093 |
|
| 2094 |
while (1) |
| 2095 |
{ |
| 2096 |
char buf[1024]; |
| 2097 |
size_t l = fread (buf, 1, sizeof (buf), fp); |
| 2098 |
if (l > 0) |
| 2099 |
o = variable_buffer_output (o, buf, l); |
| 2100 |
|
| 2101 |
if (ferror (fp)) |
| 2102 |
if (errno != EINTR) |
| 2103 |
OSS (fatal, reading_file, _("read: %s: %s"), fn, strerror (errno)); |
| 2104 |
if (feof (fp)) |
| 2105 |
break; |
| 2106 |
} |
| 2107 |
if (fclose (fp)) |
| 2108 |
OSS (fatal, reading_file, _("close: %s: %s"), fn, strerror (errno)); |
| 2109 |
|
| 2110 |
|
| 2111 |
if (o > preo && o[-1] == '\n') |
| 2112 |
if (--o > preo && o[-1] == '\r') |
| 2113 |
--o; |
| 2114 |
} |
| 2115 |
else |
| 2116 |
OS (fatal, *expanding_var, _("file: invalid file operation: %s"), fn); |
| 2117 |
|
| 2118 |
return o; |
| 2119 |
} |
| 2120 |
|
| 2121 |
static char * |
| 2122 |
func_abspath (char *o, char **argv, const char *funcname UNUSED) |
| 2123 |
{ |
| 2124 |
|
| 2125 |
const char *p = argv[0]; |
| 2126 |
const char *path = 0; |
| 2127 |
int doneany = 0; |
| 2128 |
size_t len = 0; |
| 2129 |
|
| 2130 |
while ((path = find_next_token (&p, &len)) != 0) |
| 2131 |
{ |
| 2132 |
if (len < GET_PATH_MAX) |
| 2133 |
{ |
| 2134 |
PATH_VAR (in); |
| 2135 |
PATH_VAR (out); |
| 2136 |
|
| 2137 |
strncpy (in, path, len); |
| 2138 |
in[len] = '\0'; |
| 2139 |
|
| 2140 |
if (abspath (in, out)) |
| 2141 |
{ |
| 2142 |
o = variable_buffer_output (o, out, strlen (out)); |
| 2143 |
o = variable_buffer_output (o, " ", 1); |
| 2144 |
doneany = 1; |
| 2145 |
} |
| 2146 |
} |
| 2147 |
} |
| 2148 |
|
| 2149 |
|
| 2150 |
if (doneany) |
| 2151 |
--o; |
| 2152 |
|
| 2153 |
return o; |
| 2154 |
} |
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
static char *func_call (char *o, char **argv, const char *funcname); |
| 2169 |
|
| 2170 |
#define FT_ENTRY(_name, _min, _max, _exp, _func) \ |
| 2171 |
{ { (_func) }, STRING_SIZE_TUPLE(_name), (_min), (_max), (_exp), 0 } |
| 2172 |
|
| 2173 |
static struct function_table_entry function_table_init[] = |
| 2174 |
{ |
| 2175 |
|
| 2176 |
FT_ENTRY ("abspath", 0, 1, 1, func_abspath), |
| 2177 |
FT_ENTRY ("addprefix", 2, 2, 1, func_addsuffix_addprefix), |
| 2178 |
FT_ENTRY ("addsuffix", 2, 2, 1, func_addsuffix_addprefix), |
| 2179 |
FT_ENTRY ("basename", 0, 1, 1, func_basename_dir), |
| 2180 |
FT_ENTRY ("dir", 0, 1, 1, func_basename_dir), |
| 2181 |
FT_ENTRY ("notdir", 0, 1, 1, func_notdir_suffix), |
| 2182 |
FT_ENTRY ("subst", 3, 3, 1, func_subst), |
| 2183 |
FT_ENTRY ("suffix", 0, 1, 1, func_notdir_suffix), |
| 2184 |
FT_ENTRY ("filter", 2, 2, 1, func_filter_filterout), |
| 2185 |
FT_ENTRY ("filter-out", 2, 2, 1, func_filter_filterout), |
| 2186 |
FT_ENTRY ("findstring", 2, 2, 1, func_findstring), |
| 2187 |
FT_ENTRY ("firstword", 0, 1, 1, func_firstword), |
| 2188 |
FT_ENTRY ("flavor", 0, 1, 1, func_flavor), |
| 2189 |
FT_ENTRY ("join", 2, 2, 1, func_join), |
| 2190 |
FT_ENTRY ("lastword", 0, 1, 1, func_lastword), |
| 2191 |
FT_ENTRY ("patsubst", 3, 3, 1, func_patsubst), |
| 2192 |
FT_ENTRY ("realpath", 0, 1, 1, func_realpath), |
| 2193 |
FT_ENTRY ("shell", 0, 1, 1, func_shell), |
| 2194 |
FT_ENTRY ("sort", 0, 1, 1, func_sort), |
| 2195 |
FT_ENTRY ("strip", 0, 1, 1, func_strip), |
| 2196 |
FT_ENTRY ("wildcard", 0, 1, 1, func_wildcard), |
| 2197 |
FT_ENTRY ("word", 2, 2, 1, func_word), |
| 2198 |
FT_ENTRY ("wordlist", 3, 3, 1, func_wordlist), |
| 2199 |
FT_ENTRY ("words", 0, 1, 1, func_words), |
| 2200 |
FT_ENTRY ("origin", 0, 1, 1, func_origin), |
| 2201 |
FT_ENTRY ("foreach", 3, 3, 0, func_foreach), |
| 2202 |
FT_ENTRY ("call", 1, 0, 1, func_call), |
| 2203 |
FT_ENTRY ("info", 0, 1, 1, func_error), |
| 2204 |
FT_ENTRY ("error", 0, 1, 1, func_error), |
| 2205 |
FT_ENTRY ("warning", 0, 1, 1, func_error), |
| 2206 |
FT_ENTRY ("if", 2, 3, 0, func_if), |
| 2207 |
FT_ENTRY ("or", 1, 0, 0, func_or), |
| 2208 |
FT_ENTRY ("and", 1, 0, 0, func_and), |
| 2209 |
FT_ENTRY ("value", 0, 1, 1, func_value), |
| 2210 |
FT_ENTRY ("eval", 0, 1, 1, func_eval), |
| 2211 |
FT_ENTRY ("file", 1, 2, 1, func_file), |
| 2212 |
FT_ENTRY ("debugger", 0, 1, 1, func_debugger), |
| 2213 |
#ifdef EXPERIMENTAL |
| 2214 |
FT_ENTRY ("eq", 2, 2, 1, func_eq), |
| 2215 |
FT_ENTRY ("not", 0, 1, 1, func_not), |
| 2216 |
#endif |
| 2217 |
}; |
| 2218 |
|
| 2219 |
#define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry)) |
| 2220 |
|
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
static char * |
| 2225 |
expand_builtin_function (char *o, int argc, char **argv, |
| 2226 |
const struct function_table_entry *entry_p) |
| 2227 |
{ |
| 2228 |
char *p; |
| 2229 |
|
| 2230 |
if (argc < (int)entry_p->minimum_args) |
| 2231 |
fatal (*expanding_var, strlen (entry_p->name), |
| 2232 |
_("insufficient number of arguments (%d) to function '%s'"), |
| 2233 |
argc, entry_p->name); |
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
if (!argc && !entry_p->alloc_fn) |
| 2240 |
return o; |
| 2241 |
|
| 2242 |
if (!entry_p->fptr.func_ptr) |
| 2243 |
OS (fatal, *expanding_var, |
| 2244 |
_("unimplemented on this platform: function '%s'"), entry_p->name); |
| 2245 |
|
| 2246 |
if (!entry_p->alloc_fn) |
| 2247 |
return entry_p->fptr.func_ptr (o, argv, entry_p->name); |
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
p = entry_p->fptr.alloc_func_ptr (entry_p->name, argc, argv); |
| 2253 |
if (p) |
| 2254 |
{ |
| 2255 |
o = variable_buffer_output (o, p, strlen (p)); |
| 2256 |
free (p); |
| 2257 |
} |
| 2258 |
|
| 2259 |
return o; |
| 2260 |
} |
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
int |
| 2268 |
handle_function (char **op, const char **stringp) |
| 2269 |
{ |
| 2270 |
const struct function_table_entry *entry_p; |
| 2271 |
char openparen = (*stringp)[0]; |
| 2272 |
char closeparen = openparen == '(' ? ')' : '}'; |
| 2273 |
const char *beg; |
| 2274 |
const char *end; |
| 2275 |
int count = 0; |
| 2276 |
char *abeg = NULL; |
| 2277 |
char **argv, **argvp; |
| 2278 |
int nargs; |
| 2279 |
|
| 2280 |
beg = *stringp + 1; |
| 2281 |
|
| 2282 |
entry_p = lookup_function (beg); |
| 2283 |
|
| 2284 |
if (!entry_p) |
| 2285 |
return 0; |
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
beg += entry_p->len; |
| 2291 |
NEXT_TOKEN (beg); |
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
for (nargs=1, end=beg; *end != '\0'; ++end) |
| 2299 |
if (!STOP_SET (*end, MAP_VARSEP|MAP_COMMA)) |
| 2300 |
continue; |
| 2301 |
else if (*end == ',') |
| 2302 |
++nargs; |
| 2303 |
else if (*end == openparen) |
| 2304 |
++count; |
| 2305 |
else if (*end == closeparen && --count < 0) |
| 2306 |
break; |
| 2307 |
|
| 2308 |
if (count >= 0) |
| 2309 |
fatal (*expanding_var, strlen (entry_p->name), |
| 2310 |
_("unterminated call to function '%s': missing '%c'"), |
| 2311 |
entry_p->name, closeparen); |
| 2312 |
|
| 2313 |
*stringp = end; |
| 2314 |
|
| 2315 |
|
| 2316 |
argvp = argv = alloca (sizeof (char *) * (nargs + 2)); |
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
|
| 2326 |
if (entry_p->expand_args) |
| 2327 |
{ |
| 2328 |
const char *p; |
| 2329 |
for (p=beg, nargs=0; p <= end; ++argvp) |
| 2330 |
{ |
| 2331 |
const char *next; |
| 2332 |
|
| 2333 |
++nargs; |
| 2334 |
|
| 2335 |
if (nargs == entry_p->maximum_args |
| 2336 |
|| ((next = find_next_argument (openparen, closeparen, p, end)) == NULL)) |
| 2337 |
next = end; |
| 2338 |
|
| 2339 |
*argvp = expand_argument (p, next); |
| 2340 |
p = next + 1; |
| 2341 |
} |
| 2342 |
} |
| 2343 |
else |
| 2344 |
{ |
| 2345 |
size_t len = end - beg; |
| 2346 |
char *p, *aend; |
| 2347 |
|
| 2348 |
abeg = xmalloc (len+1); |
| 2349 |
memcpy (abeg, beg, len); |
| 2350 |
abeg[len] = '\0'; |
| 2351 |
aend = abeg + len; |
| 2352 |
|
| 2353 |
for (p=abeg, nargs=0; p <= aend; ++argvp) |
| 2354 |
{ |
| 2355 |
char *next; |
| 2356 |
|
| 2357 |
++nargs; |
| 2358 |
|
| 2359 |
if (nargs == entry_p->maximum_args |
| 2360 |
|| ((next = find_next_argument (openparen, closeparen, p, aend)) == NULL)) |
| 2361 |
next = aend; |
| 2362 |
|
| 2363 |
*argvp = p; |
| 2364 |
*next = '\0'; |
| 2365 |
p = next + 1; |
| 2366 |
} |
| 2367 |
} |
| 2368 |
*argvp = NULL; |
| 2369 |
|
| 2370 |
|
| 2371 |
*op = expand_builtin_function (*op, nargs, argv, entry_p); |
| 2372 |
|
| 2373 |
|
| 2374 |
if (entry_p->expand_args) |
| 2375 |
for (argvp=argv; *argvp != 0; ++argvp) |
| 2376 |
free (*argvp); |
| 2377 |
else |
| 2378 |
free (abeg); |
| 2379 |
|
| 2380 |
return 1; |
| 2381 |
} |
| 2382 |
|
| 2383 |
|
| 2384 |
|
| 2385 |
|
| 2386 |
|
| 2387 |
|
| 2388 |
static char * |
| 2389 |
func_call (char *o, char **argv, const char *funcname UNUSED) |
| 2390 |
{ |
| 2391 |
static int max_args = 0; |
| 2392 |
char *fname; |
| 2393 |
char *body; |
| 2394 |
size_t flen; |
| 2395 |
int i; |
| 2396 |
int saved_args; |
| 2397 |
const struct function_table_entry *entry_p; |
| 2398 |
struct variable *v; |
| 2399 |
|
| 2400 |
|
| 2401 |
fname = next_token (argv[0]); |
| 2402 |
end_of_token (fname)[0] = '\0'; |
| 2403 |
|
| 2404 |
|
| 2405 |
if (*fname == '\0') |
| 2406 |
return o; |
| 2407 |
|
| 2408 |
|
| 2409 |
|
| 2410 |
entry_p = lookup_function (fname); |
| 2411 |
if (entry_p) |
| 2412 |
{ |
| 2413 |
|
| 2414 |
for (i=0; argv[i+1]; ++i) |
| 2415 |
; |
| 2416 |
return expand_builtin_function (o, i, argv+1, entry_p); |
| 2417 |
} |
| 2418 |
|
| 2419 |
|
| 2420 |
|
| 2421 |
flen = strlen (fname); |
| 2422 |
|
| 2423 |
v = lookup_variable (fname, flen); |
| 2424 |
|
| 2425 |
if (v == 0) |
| 2426 |
warn_undefined (fname, flen); |
| 2427 |
|
| 2428 |
if (v == 0 || *v->value == '\0') |
| 2429 |
return o; |
| 2430 |
|
| 2431 |
body = alloca (flen + 4); |
| 2432 |
body[0] = '$'; |
| 2433 |
body[1] = '('; |
| 2434 |
memcpy (body + 2, fname, flen); |
| 2435 |
body[flen+2] = ')'; |
| 2436 |
body[flen+3] = '\0'; |
| 2437 |
|
| 2438 |
|
| 2439 |
|
| 2440 |
push_new_variable_scope (); |
| 2441 |
|
| 2442 |
for (i=0; *argv; ++i, ++argv) |
| 2443 |
{ |
| 2444 |
char num[11]; |
| 2445 |
|
| 2446 |
sprintf (num, "%d", i); |
| 2447 |
define_variable (num, strlen (num), *argv, o_automatic, 0); |
| 2448 |
} |
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
|
| 2454 |
|
| 2455 |
for (; i < max_args; ++i) |
| 2456 |
{ |
| 2457 |
char num[11]; |
| 2458 |
|
| 2459 |
sprintf (num, "%d", i); |
| 2460 |
define_variable (num, strlen (num), "", o_automatic, 0); |
| 2461 |
} |
| 2462 |
|
| 2463 |
|
| 2464 |
|
| 2465 |
|
| 2466 |
v->exp_count = EXP_COUNT_MAX; |
| 2467 |
|
| 2468 |
saved_args = max_args; |
| 2469 |
max_args = i; |
| 2470 |
o = variable_expand_string (o, body, flen+3); |
| 2471 |
max_args = saved_args; |
| 2472 |
|
| 2473 |
v->exp_count = 0; |
| 2474 |
|
| 2475 |
pop_variable_scope (); |
| 2476 |
|
| 2477 |
return o + strlen (o); |
| 2478 |
} |
| 2479 |
|
| 2480 |
void |
| 2481 |
define_new_function (const gmk_floc *flocp, const char *name, |
| 2482 |
unsigned int min, unsigned int max, unsigned int flags, |
| 2483 |
gmk_func_ptr func) |
| 2484 |
{ |
| 2485 |
const char *e = name; |
| 2486 |
struct function_table_entry *ent; |
| 2487 |
size_t len; |
| 2488 |
|
| 2489 |
while (STOP_SET (*e, MAP_USERFUNC)) |
| 2490 |
e++; |
| 2491 |
len = e - name; |
| 2492 |
|
| 2493 |
if (len == 0) |
| 2494 |
O (fatal, flocp, _("Empty function name")); |
| 2495 |
if (*name == '.' || *e != '\0') |
| 2496 |
OS (fatal, flocp, _("Invalid function name: %s"), name); |
| 2497 |
if (len > 255) |
| 2498 |
OS (fatal, flocp, _("Function name too long: %s"), name); |
| 2499 |
if (min > 255) |
| 2500 |
ONS (fatal, flocp, |
| 2501 |
_("Invalid minimum argument count (%u) for function %s"), min, name); |
| 2502 |
if (max > 255 || (max && max < min)) |
| 2503 |
ONS (fatal, flocp, |
| 2504 |
_("Invalid maximum argument count (%u) for function %s"), max, name); |
| 2505 |
|
| 2506 |
ent = xmalloc (sizeof (struct function_table_entry)); |
| 2507 |
ent->name = name; |
| 2508 |
ent->len = (unsigned char) len; |
| 2509 |
ent->minimum_args = (unsigned char) min; |
| 2510 |
ent->maximum_args = (unsigned char) max; |
| 2511 |
ent->expand_args = ANY_SET(flags, GMK_FUNC_NOEXPAND) ? 0 : 1; |
| 2512 |
ent->alloc_fn = 1; |
| 2513 |
ent->fptr.alloc_func_ptr = func; |
| 2514 |
|
| 2515 |
hash_insert (&function_table, ent); |
| 2516 |
} |
| 2517 |
|
| 2518 |
void |
| 2519 |
hash_init_function_table (void) |
| 2520 |
{ |
| 2521 |
hash_init (&function_table, FUNCTION_TABLE_ENTRIES * 2, |
| 2522 |
function_table_entry_hash_1, function_table_entry_hash_2, |
| 2523 |
function_table_entry_hash_cmp); |
| 2524 |
hash_load (&function_table, function_table_init, |
| 2525 |
FUNCTION_TABLE_ENTRIES, sizeof (struct function_table_entry)); |
| 2526 |
} |