| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
#include <ctype.h> |
| 14 |
#include <word.h> |
| 15 |
|
| 16 |
|
| 17 |
#ifdef linux |
| 18 |
#ifdef isalnum |
| 19 |
#undef isalnum |
| 20 |
#define isalnum(c) (isalpha(c)||isdigit(c)) |
| 21 |
#endif |
| 22 |
#endif |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#define MAXDELIM 256 |
| 34 |
#define MAXDTABLES 1024 |
| 35 |
#define BUFINC 5000 |
| 36 |
|
| 37 |
static char lastd; |
| 38 |
static char dtable[MAXDELIM]; |
| 39 |
static char *dtables[MAXDTABLES]; |
| 40 |
static int ndtable=0; |
| 41 |
|
| 42 |
|
| 43 |
#define ALL '*' |
| 44 |
#define ANY '?' |
| 45 |
#define RANGE '[' |
| 46 |
#define ENDRANGE ']' |
| 47 |
#define RANGEDELIM '-' |
| 48 |
#define NOTRANGE '~' |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#ifdef ANSI_FUNC |
| 62 |
static int |
| 63 |
checkrange (char *xtemplate, int *ptr, int c) |
| 64 |
#else |
| 65 |
static int checkrange(xtemplate, ptr, c) |
| 66 |
char *xtemplate; |
| 67 |
int *ptr; |
| 68 |
int c; |
| 69 |
#endif |
| 70 |
{ |
| 71 |
int inrange, notrange; |
| 72 |
char lorange, hirange; |
| 73 |
int tptr; |
| 74 |
|
| 75 |
tptr = *ptr; |
| 76 |
|
| 77 |
if( strchr(&xtemplate[tptr], ENDRANGE) == NULL ) |
| 78 |
return(0); |
| 79 |
|
| 80 |
if( xtemplate [tptr+1] == NOTRANGE ){ |
| 81 |
notrange = 1; tptr++; |
| 82 |
} |
| 83 |
else |
| 84 |
notrange = 0; |
| 85 |
|
| 86 |
inrange = 0; |
| 87 |
|
| 88 |
tptr++; |
| 89 |
while( xtemplate[tptr] != ENDRANGE ){ |
| 90 |
|
| 91 |
lorange = xtemplate[tptr]; |
| 92 |
|
| 93 |
tptr++; |
| 94 |
if( xtemplate[tptr] != RANGEDELIM ) |
| 95 |
hirange = lorange; |
| 96 |
else{ |
| 97 |
tptr++;hirange = xtemplate[tptr];tptr++; |
| 98 |
} |
| 99 |
if( (c>=lorange) && (c<=hirange) ){ |
| 100 |
inrange = 1; break; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if( (inrange ^ notrange) ==0 ) |
| 105 |
return(0); |
| 106 |
else{ |
| 107 |
*ptr = strchr(&xtemplate[tptr],']') - xtemplate + 1; |
| 108 |
return(1); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
#ifdef ANSI_FUNC |
| 124 |
static void |
| 125 |
addstring (char **buf, int *blen, int *maxlen, char *str) |
| 126 |
#else |
| 127 |
static void addstring(buf, blen, maxlen, str) |
| 128 |
char **buf; |
| 129 |
int *blen; |
| 130 |
int *maxlen; |
| 131 |
char *str; |
| 132 |
#endif |
| 133 |
{ |
| 134 |
int slen; |
| 135 |
|
| 136 |
slen = strlen(str) + 1; |
| 137 |
while( (*blen + slen) >= *maxlen ){ |
| 138 |
*maxlen += BUFINC; |
| 139 |
*buf = (char *)xrealloc(*buf, *maxlen); |
| 140 |
} |
| 141 |
strcat(*buf, str); |
| 142 |
*blen += slen; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
#ifdef ANSI_FUNC |
| 157 |
static void |
| 158 |
addchar (char **buf, int *blen, int *maxlen, int c) |
| 159 |
#else |
| 160 |
static void addchar(buf, blen, maxlen, c) |
| 161 |
char **buf; |
| 162 |
int *blen; |
| 163 |
int *maxlen; |
| 164 |
int c; |
| 165 |
#endif |
| 166 |
{ |
| 167 |
char tbuf[2]; |
| 168 |
|
| 169 |
tbuf[0] = c; |
| 170 |
tbuf[1] = '\0'; |
| 171 |
addstring(buf, blen, maxlen, tbuf); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
#ifdef ANSI_FUNC |
| 186 |
static char * |
| 187 |
lookupkeywords (char *name, char **keyword, char **value, int nkey) |
| 188 |
#else |
| 189 |
static char *lookupkeywords(name, keyword, value, nkey) |
| 190 |
char *name; |
| 191 |
char **keyword; |
| 192 |
char **value; |
| 193 |
int nkey; |
| 194 |
#endif |
| 195 |
{ |
| 196 |
int i; |
| 197 |
for(i=0; i<nkey; i++){ |
| 198 |
if( !strcmp(name, keyword[i]) ) |
| 199 |
return(value[i]); |
| 200 |
} |
| 201 |
return(NULL); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
#ifdef ANSI_FUNC |
| 216 |
static int |
| 217 |
hexval (int c) |
| 218 |
#else |
| 219 |
static int hexval(c) |
| 220 |
int c; |
| 221 |
#endif |
| 222 |
{ |
| 223 |
switch(c){ |
| 224 |
case '0': return 0; |
| 225 |
case '1': return 1; |
| 226 |
case '2': return 2; |
| 227 |
case '3': return 3; |
| 228 |
case '4': return 4; |
| 229 |
case '5': return 5; |
| 230 |
case '6': return 6; |
| 231 |
case '7': return 7; |
| 232 |
case '8': return 8; |
| 233 |
case '9': return 9; |
| 234 |
case 'A': return 10; |
| 235 |
case 'a': return 10; |
| 236 |
case 'B': return 11; |
| 237 |
case 'b': return 11; |
| 238 |
case 'C': return 12; |
| 239 |
case 'c': return 12; |
| 240 |
case 'D': return 13; |
| 241 |
case 'd': return 13; |
| 242 |
case 'E': return 14; |
| 243 |
case 'e': return 14; |
| 244 |
case 'F': return 15; |
| 245 |
case 'f': return 15; |
| 246 |
default: return -1; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
#ifdef ANSI_FUNC |
| 270 |
int |
| 271 |
word (char *lbuf, char *tbuf, int *lptr) |
| 272 |
#else |
| 273 |
int word(lbuf, tbuf, lptr) |
| 274 |
char *lbuf; |
| 275 |
char *tbuf; |
| 276 |
int *lptr; |
| 277 |
#endif |
| 278 |
{ |
| 279 |
int ip; |
| 280 |
int i; |
| 281 |
char quotes; |
| 282 |
|
| 283 |
|
| 284 |
lastd='\0'; |
| 285 |
|
| 286 |
|
| 287 |
*tbuf = '\0'; |
| 288 |
|
| 289 |
|
| 290 |
if( lbuf == NULL ) |
| 291 |
return(0); |
| 292 |
|
| 293 |
|
| 294 |
ip = *lptr; |
| 295 |
|
| 296 |
|
| 297 |
if( lbuf[ip] == '\0' ) |
| 298 |
return(0); |
| 299 |
|
| 300 |
|
| 301 |
while( isspace((int)lbuf[ip]) || (dtable[(int)lbuf[ip]]>0) ){ |
| 302 |
if( lbuf[ip] == '\0' ){ |
| 303 |
*lptr = ip; |
| 304 |
return(0); |
| 305 |
} |
| 306 |
else |
| 307 |
ip++; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
quotes = '\0'; |
| 312 |
if( lbuf[ip] == '"' ){ |
| 313 |
quotes = '"'; |
| 314 |
lastd = '"'; |
| 315 |
} |
| 316 |
if( lbuf[ip] == '\'' ){ |
| 317 |
quotes = '\''; |
| 318 |
lastd = '\''; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
if( quotes != '\0' ){ |
| 323 |
|
| 324 |
ip++; |
| 325 |
|
| 326 |
for(i=0; lbuf[ip] != '\0'; i++, ip++){ |
| 327 |
if( (lbuf[ip] == quotes) && (lbuf[ip-1] != '\\') ) |
| 328 |
break; |
| 329 |
else |
| 330 |
tbuf[i] = lbuf[ip]; |
| 331 |
} |
| 332 |
} |
| 333 |
else{ |
| 334 |
|
| 335 |
for(i=0; |
| 336 |
lbuf[ip] && !isspace((int)lbuf[ip]) && (dtable[(int)lbuf[ip]]==0); |
| 337 |
i++, ip++) |
| 338 |
tbuf[i] = lbuf[ip]; |
| 339 |
|
| 340 |
lastd = lbuf[ip]; |
| 341 |
} |
| 342 |
|
| 343 |
if( lbuf[ip] ) |
| 344 |
ip++; |
| 345 |
|
| 346 |
|
| 347 |
tbuf[i] = '\0'; |
| 348 |
|
| 349 |
|
| 350 |
*lptr = ip; |
| 351 |
return(1); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
#ifdef ANSI_FUNC |
| 366 |
int |
| 367 |
newdtable (char *s) |
| 368 |
#else |
| 369 |
int newdtable(s) |
| 370 |
char *s; |
| 371 |
#endif |
| 372 |
{ |
| 373 |
int i; |
| 374 |
char *cur; |
| 375 |
|
| 376 |
if( ndtable >= MAXDTABLES ){ |
| 377 |
fprintf(stderr, "ERROR: no more delimiter tables available\n"); |
| 378 |
return(0); |
| 379 |
} |
| 380 |
|
| 381 |
ndtable++; |
| 382 |
|
| 383 |
dtables[ndtable-1] = (char *)xmalloc(MAXDELIM); |
| 384 |
cur = dtables[ndtable-1]; |
| 385 |
|
| 386 |
for(i=0; i<MAXDELIM; i++){ |
| 387 |
cur[i] = dtable[i]; |
| 388 |
dtable[i] = 0; |
| 389 |
} |
| 390 |
|
| 391 |
if( s != NULL ){ |
| 392 |
for(; *s; s++) |
| 393 |
dtable[(int)*s] = 1; |
| 394 |
} |
| 395 |
return(1); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
#ifdef ANSI_FUNC |
| 410 |
int |
| 411 |
freedtable (void) |
| 412 |
#else |
| 413 |
int freedtable() |
| 414 |
#endif |
| 415 |
{ |
| 416 |
int i; |
| 417 |
char *cur; |
| 418 |
|
| 419 |
if( ndtable <= 0 ){ |
| 420 |
fprintf(stderr, "ERROR: no delimiter tables to restore\n"); |
| 421 |
return(0); |
| 422 |
} |
| 423 |
cur = dtables[ndtable-1]; |
| 424 |
|
| 425 |
for(i=0; i<MAXDELIM; i++){ |
| 426 |
dtable[i] = cur[i]; |
| 427 |
} |
| 428 |
|
| 429 |
xfree((void *)cur); |
| 430 |
|
| 431 |
ndtable--; |
| 432 |
return(1); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
#ifdef ANSI_FUNC |
| 447 |
void |
| 448 |
newdelim (char *s) |
| 449 |
#else |
| 450 |
void newdelim(s) |
| 451 |
char *s; |
| 452 |
#endif |
| 453 |
{ |
| 454 |
if( s != NULL ){ |
| 455 |
for(; *s; s++) |
| 456 |
dtable[(int)*s] = 1; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
#ifdef ANSI_FUNC |
| 472 |
void |
| 473 |
freedelim (char *s) |
| 474 |
#else |
| 475 |
void freedelim(s) |
| 476 |
char *s; |
| 477 |
#endif |
| 478 |
{ |
| 479 |
int i; |
| 480 |
|
| 481 |
if( s ){ |
| 482 |
for(; *s; s++) |
| 483 |
if( dtable[(int)*s] > 0 ) |
| 484 |
dtable[(int)*s] -= 1; |
| 485 |
} |
| 486 |
else{ |
| 487 |
for(i=0; i<MAXDELIM; i++) |
| 488 |
if( dtable[i] > 0 ) |
| 489 |
dtable[i] -= 1; |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
#ifdef ANSI_FUNC |
| 505 |
int |
| 506 |
lastdelim (void) |
| 507 |
#else |
| 508 |
int lastdelim() |
| 509 |
#endif |
| 510 |
{ |
| 511 |
return((int)lastd); |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
#ifdef ANSI_FUNC |
| 534 |
int |
| 535 |
tmatch (char *string, char *xtemplate) |
| 536 |
#else |
| 537 |
int tmatch(string, xtemplate) |
| 538 |
char *string; |
| 539 |
char *xtemplate; |
| 540 |
#endif |
| 541 |
{ |
| 542 |
char *lastmeta=0; |
| 543 |
char *nonabsorbed=0; |
| 544 |
int sptr=0; |
| 545 |
int tptr=0; |
| 546 |
|
| 547 |
|
| 548 |
while( (xtemplate[tptr] != '\0') || (string[sptr] != '\0') ){ |
| 549 |
|
| 550 |
if( string[sptr] == xtemplate[tptr] ){ |
| 551 |
sptr++; tptr++; continue; |
| 552 |
} |
| 553 |
|
| 554 |
if( xtemplate[tptr] == RANGE ){ |
| 555 |
if( checkrange(xtemplate, &tptr, string[sptr]) == 0 ){ |
| 556 |
|
| 557 |
if( lastmeta == 0 ) return(0); |
| 558 |
|
| 559 |
xtemplate = lastmeta; tptr=0; |
| 560 |
|
| 561 |
string = nonabsorbed; sptr=0; |
| 562 |
continue; |
| 563 |
} |
| 564 |
|
| 565 |
else{ |
| 566 |
sptr++; continue; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
if( xtemplate[tptr] == ANY ){ |
| 571 |
if( string[sptr] == '\0' ) |
| 572 |
return(0); |
| 573 |
else{ |
| 574 |
sptr++; tptr++; continue; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
if( xtemplate[tptr] == ALL ){ |
| 579 |
|
| 580 |
lastmeta = &xtemplate[tptr]; |
| 581 |
tptr++; |
| 582 |
|
| 583 |
if( xtemplate[tptr] == '\0' ) return(1); |
| 584 |
|
| 585 |
|
| 586 |
if( xtemplate[tptr] == RANGE){ |
| 587 |
while( checkrange(xtemplate, &tptr, string[sptr]) == 0 ){ |
| 588 |
|
| 589 |
if( string[sptr] == '\0' ) return(0); |
| 590 |
sptr++; |
| 591 |
} |
| 592 |
|
| 593 |
nonabsorbed = &string[sptr];nonabsorbed++; |
| 594 |
sptr++; |
| 595 |
continue; |
| 596 |
} |
| 597 |
|
| 598 |
else if( xtemplate[tptr] != ANY && xtemplate[tptr] != ALL ){ |
| 599 |
while(string[sptr] != xtemplate[tptr]){ |
| 600 |
|
| 601 |
|
| 602 |
if( string[sptr] == '\0' ) return(0); |
| 603 |
sptr++; |
| 604 |
} |
| 605 |
|
| 606 |
nonabsorbed = &string[sptr];nonabsorbed++; |
| 607 |
continue; |
| 608 |
} |
| 609 |
else{ |
| 610 |
|
| 611 |
nonabsorbed = &string[sptr];nonabsorbed++; |
| 612 |
continue; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
else{ |
| 617 |
if( lastmeta == 0 ) return(0); |
| 618 |
|
| 619 |
xtemplate = lastmeta; tptr=0; |
| 620 |
|
| 621 |
string = nonabsorbed; sptr=0; |
| 622 |
continue; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
return(1); |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
#ifdef ANSI_FUNC |
| 644 |
int |
| 645 |
keyword (char *ibuf, char *key, char *obuf, int maxlen) |
| 646 |
#else |
| 647 |
int keyword(ibuf, key, obuf, maxlen) |
| 648 |
char *ibuf; |
| 649 |
char *key; |
| 650 |
char *obuf; |
| 651 |
int maxlen; |
| 652 |
#endif |
| 653 |
{ |
| 654 |
int len; |
| 655 |
int qlev; |
| 656 |
char *s; |
| 657 |
char *t; |
| 658 |
char *u; |
| 659 |
char *v; |
| 660 |
char *iptr; |
| 661 |
char quote; |
| 662 |
|
| 663 |
|
| 664 |
if( (ibuf == NULL) || (*ibuf == '\0') ){ |
| 665 |
return(0); |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
*obuf = '\0'; |
| 670 |
iptr = ibuf; |
| 671 |
|
| 672 |
|
| 673 |
maxlen--; |
| 674 |
|
| 675 |
|
| 676 |
while( *iptr ){ |
| 677 |
|
| 678 |
if( (s = (char *)strstr(iptr, key)) == NULL ) |
| 679 |
return(0); |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
if( (s == ibuf) || (*(s-1) == ',') || (*(s-1) == '[') ){ |
| 684 |
|
| 685 |
t = s + strlen(key); |
| 686 |
while( isspace((int)*t) ) |
| 687 |
t++; |
| 688 |
|
| 689 |
if( *t == '=' ){ |
| 690 |
t++; |
| 691 |
|
| 692 |
while( isspace((int)*t) ) |
| 693 |
t++; |
| 694 |
|
| 695 |
u = t; |
| 696 |
|
| 697 |
v = t; |
| 698 |
|
| 699 |
if( (*t == '"') || (*t == '\'') || (*t == '(') || (*t == '[') ){ |
| 700 |
switch(*t){ |
| 701 |
case '"': |
| 702 |
case '\'': |
| 703 |
quote = *t; |
| 704 |
break; |
| 705 |
case '(': |
| 706 |
quote = ')'; |
| 707 |
break; |
| 708 |
case '[': |
| 709 |
quote = ']'; |
| 710 |
break; |
| 711 |
} |
| 712 |
|
| 713 |
t++; u++; v++; |
| 714 |
while( *t && (*t != quote) ){ |
| 715 |
t++; v++; |
| 716 |
} |
| 717 |
if( *t == quote ){ |
| 718 |
t++; |
| 719 |
} |
| 720 |
} |
| 721 |
else{ |
| 722 |
qlev = 0; |
| 723 |
while( *t && |
| 724 |
((qlev != 0) || (*t != ',')) && |
| 725 |
((qlev != 0) || (*t != ']')) ){ |
| 726 |
if( *t == '[' ) |
| 727 |
qlev++; |
| 728 |
else if( *t == ']' ) |
| 729 |
qlev--; |
| 730 |
t++; v++; |
| 731 |
} |
| 732 |
} |
| 733 |
len = MIN(maxlen, v - u); |
| 734 |
strncpy(obuf, u, len); |
| 735 |
obuf[len] = '\0'; |
| 736 |
|
| 737 |
|
| 738 |
if( (s > ibuf) && (*(s-1) == ',') ) |
| 739 |
s--; |
| 740 |
|
| 741 |
else if( *t == ',' ) |
| 742 |
t++; |
| 743 |
|
| 744 |
memmove(s, t, strlen(t)+1); |
| 745 |
return(len); |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
iptr = s+1; |
| 750 |
} |
| 751 |
return(0); |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
#ifdef ANSI_FUNC |
| 766 |
char * |
| 767 |
macro (char *icmd, char **keyword, char **value, int nkey, |
| 768 |
MacroCB client_callback, void *client_data) |
| 769 |
#else |
| 770 |
char *macro(icmd, keyword, value, nkey, client_callback, client_data) |
| 771 |
char *icmd; |
| 772 |
char **keyword; |
| 773 |
char **value; |
| 774 |
int nkey; |
| 775 |
MacroCB client_callback; |
| 776 |
void *client_data; |
| 777 |
#endif |
| 778 |
{ |
| 779 |
int i, j; |
| 780 |
int maxlen; |
| 781 |
char brace; |
| 782 |
char *result; |
| 783 |
char tbuf[1000]; |
| 784 |
char tbuf1[1000]; |
| 785 |
char *s; |
| 786 |
char *ip; |
| 787 |
char *mip; |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
result = (char *)xmalloc(BUFINC+1); |
| 792 |
maxlen = BUFINC; |
| 793 |
*result = '\0'; |
| 794 |
for(i=0, ip=icmd; *ip; ip++){ |
| 795 |
if( *ip != '$' ){ |
| 796 |
addchar(&result, &i, &maxlen, *ip); |
| 797 |
} |
| 798 |
else{ |
| 799 |
|
| 800 |
mip = ip; |
| 801 |
|
| 802 |
ip++; |
| 803 |
|
| 804 |
if( *ip == '{' ){ |
| 805 |
brace = '{'; |
| 806 |
ip++; |
| 807 |
} |
| 808 |
else if( *ip == '(' ){ |
| 809 |
brace = '('; |
| 810 |
ip++; |
| 811 |
} |
| 812 |
else |
| 813 |
brace = '\0'; |
| 814 |
|
| 815 |
for(*tbuf='\0', j=0; *ip; ip++ ){ |
| 816 |
|
| 817 |
if( brace && *ip == (brace == '(' ? ')' : '}') ){ |
| 818 |
ip++; |
| 819 |
break; |
| 820 |
} |
| 821 |
|
| 822 |
else if( !isalnum((int)*ip) && *ip != '_'){ |
| 823 |
break; |
| 824 |
} |
| 825 |
else{ |
| 826 |
tbuf[j++] = *ip; |
| 827 |
tbuf[j] = '\0'; |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
ip--; |
| 832 |
|
| 833 |
if( (nkey > 0) && |
| 834 |
(s=lookupkeywords(tbuf, keyword, value, nkey)) != NULL ){ |
| 835 |
addstring(&result, &i, &maxlen, s); |
| 836 |
} |
| 837 |
|
| 838 |
else if( (client_callback != NULL) && |
| 839 |
((s=(*client_callback)(tbuf, client_data)) != NULL) ){ |
| 840 |
addstring(&result, &i, &maxlen, s); |
| 841 |
} |
| 842 |
|
| 843 |
else if( (s = (char *)getenv(tbuf)) != NULL ){ |
| 844 |
addstring(&result, &i, &maxlen, s); |
| 845 |
} |
| 846 |
|
| 847 |
else{ |
| 848 |
int len; |
| 849 |
len = ip - mip + 1; |
| 850 |
strncpy(tbuf1, mip, len); |
| 851 |
tbuf1[len] = '\0'; |
| 852 |
addstring(&result, &i, &maxlen, tbuf1); |
| 853 |
} |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
result[i] = '\0'; |
| 858 |
result = (char *)xrealloc(result, i+1); |
| 859 |
return(result); |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
#ifdef ANSI_FUNC |
| 880 |
void |
| 881 |
cluc (char *s) |
| 882 |
#else |
| 883 |
void cluc(s) |
| 884 |
char *s; |
| 885 |
#endif |
| 886 |
{ |
| 887 |
while(*s){ |
| 888 |
if( islower((int)*s) ) |
| 889 |
*s = toupper(*s); |
| 890 |
s++; |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
#ifdef ANSI_FUNC |
| 906 |
void |
| 907 |
culc (char *s) |
| 908 |
#else |
| 909 |
void culc(s) |
| 910 |
char *s; |
| 911 |
#endif |
| 912 |
{ |
| 913 |
while(*s){ |
| 914 |
if( isupper((int)*s) ) |
| 915 |
*s = tolower(*s); |
| 916 |
s++; |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
#ifdef ANSI_FUNC |
| 932 |
int |
| 933 |
nowhite ( |
| 934 |
char *c, |
| 935 |
char *cr |
| 936 |
) |
| 937 |
#else |
| 938 |
int nowhite(c,cr) |
| 939 |
char *c; |
| 940 |
char *cr; |
| 941 |
#endif |
| 942 |
{ |
| 943 |
char *cr0; |
| 944 |
int n; |
| 945 |
|
| 946 |
|
| 947 |
while(*c && isspace((int)*c)) |
| 948 |
c++; |
| 949 |
|
| 950 |
cr0 = cr; |
| 951 |
while(*c) |
| 952 |
*cr++ = *c++; |
| 953 |
n = cr - cr0; |
| 954 |
*cr-- = '\0'; |
| 955 |
|
| 956 |
while( n && isspace((int)*cr) ){ |
| 957 |
*cr-- = '\0'; |
| 958 |
n--; |
| 959 |
} |
| 960 |
return(n); |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
#ifdef ANSI_FUNC |
| 975 |
void |
| 976 |
nocr (char *s) |
| 977 |
#else |
| 978 |
void nocr(s) |
| 979 |
char *s; |
| 980 |
#endif |
| 981 |
{ |
| 982 |
int len; |
| 983 |
|
| 984 |
if( (s==NULL) || (*s=='\0') ) |
| 985 |
return; |
| 986 |
len = strlen(s); |
| 987 |
if( s[len-1] == '\n' ) |
| 988 |
s[len-1] = '\0'; |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
#ifdef ANSI_FUNC |
| 1003 |
int |
| 1004 |
istrue (char *s) |
| 1005 |
#else |
| 1006 |
int istrue(s) |
| 1007 |
char *s; |
| 1008 |
#endif |
| 1009 |
{ |
| 1010 |
char *t; |
| 1011 |
int result; |
| 1012 |
|
| 1013 |
if( (s==NULL) || (*s=='\0') ) |
| 1014 |
return(0); |
| 1015 |
t = (char *)xmalloc(strlen(s)+1); |
| 1016 |
nowhite(s, t); |
| 1017 |
culc(t); |
| 1018 |
result = (!strcmp(t, "true") || !strcmp(t, "yes") || |
| 1019 |
!strcmp(t, "on") || !strcmp(t, "1") ); |
| 1020 |
xfree(t); |
| 1021 |
return(result); |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
#ifdef ANSI_FUNC |
| 1037 |
int |
| 1038 |
isfalse (char *s) |
| 1039 |
#else |
| 1040 |
int isfalse(s) |
| 1041 |
char *s; |
| 1042 |
#endif |
| 1043 |
{ |
| 1044 |
char *t; |
| 1045 |
int result; |
| 1046 |
|
| 1047 |
if( (s==NULL) || (*s=='\0') ) |
| 1048 |
return(0); |
| 1049 |
t = (char *)xmalloc(strlen(s)+1); |
| 1050 |
nowhite(s, t); |
| 1051 |
culc(t); |
| 1052 |
result = (!strcmp(t, "false") || !strcmp(t, "no") || |
| 1053 |
!strcmp(t, "off") || !strcmp(t, "0") ); |
| 1054 |
xfree(t); |
| 1055 |
return(result); |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
#ifdef ANSI_FUNC |
| 1071 |
unsigned long strtoul16 (char *s, char **t) |
| 1072 |
#else |
| 1073 |
unsigned long strtoul16(s, t) |
| 1074 |
char *s; |
| 1075 |
char **t; |
| 1076 |
#endif |
| 1077 |
{ |
| 1078 |
unsigned long v=0; |
| 1079 |
int h; |
| 1080 |
|
| 1081 |
while ( *s != ' ' && |
| 1082 |
*s != '\n' && |
| 1083 |
*s != '\r' && |
| 1084 |
*s != '\0' ){ |
| 1085 |
v *= 16; |
| 1086 |
if( (h = hexval(*s)) >= 0 ){ |
| 1087 |
v += h; |
| 1088 |
s++; |
| 1089 |
} |
| 1090 |
else{ |
| 1091 |
break; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
if( t != NULL ) |
| 1095 |
*t = s; |
| 1096 |
return(v); |
| 1097 |
} |