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