1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
|
%{
/* $Id: configfile.y 9364 2011-08-21 23:23:26Z eagle $
**
** A yacc input file for the innfeed config file.
**
** Written by James Brister <brister@vix.com>
**
** This file contains the heart of the innfeed configuration parser, written
** in yacc. It uses an external lexer generated by flex.
*/
#include "innfeed.h"
#include "config.h"
#include "clibrary.h"
#include <errno.h>
#include <ctype.h>
#include <syslog.h>
#if defined(_HPUX_SOURCE)
# include <alloca.h>
#endif
#include "inn/messages.h"
#include "inn/libinn.h"
#include "configfile.h"
#include "misc.h"
#define UNKNOWN_SCOPE_TYPE "line %d: unknown scope type: %s"
#define SYNTAX_ERROR "line %d: syntax error"
extern int lineCount ;
scope *topScope = NULL ;
static scope *currScope = NULL ;
char *errbuff = NULL ;
static void appendName (scope *s, char *p, size_t len) ;
static char *valueScopedName (value *v) ;
static void freeValue (value *v) ;
static char *checkName (scope *s, const char *name) ;
static void addValue (scope *s, value *v) ;
static char *addScope (scope *s, const char *name, scope *val) ;
static void printScope (FILE *fp, scope *s, int indent) ;
static void printValue (FILE *fp, value *v, int indent) ;
static scope *newScope (const char *type) ;
#if 0
static int strNCaseCmp (const char *a, const char *b, size_t len) ;
#endif
int yyerror (const char *s) ;
int yywrap (void) ;
int yyparse (void) ;
#if 0
int isString (scope *s, const char *name, int inherit)
{
value *v = findValue (s,name,inherit) ;
return (v != NULL && v->type == stringval) ;
}
#endif
int getBool (scope *s, const char *name, int *rval, int inherit)
{
value *v = findValue (s,name,inherit) ;
if (v == NULL)
return 0 ;
else if (v->type != boolval)
return 0 ;
*rval = v->v.bool_val ;
return 1 ;
}
int getString (scope *s, const char *name, char **rval, int inherit)
{
value *v = findValue (s,name,inherit) ;
if (v == NULL)
return 0 ;
else if (v->type != stringval)
return 0 ;
*rval = xstrdup (v->v.charp_val) ;
return 1 ;
}
int getReal (scope *s, const char *name, double *rval, int inherit)
{
value *v = findValue (s,name,inherit) ;
if (v == NULL)
return 0 ;
else if (v->type != realval)
return 0 ;
*rval = v->v.real_val ;
return 1 ;
}
int getInteger (scope *s, const char *name, long *rval, int inherit)
{
value *v = findValue (s,name,inherit) ;
if (v == NULL)
return 0 ;
else if (v->type != intval)
return 0 ;
*rval = v->v.int_val ;
return 1 ;
}
void freeScopeTree (scope *s)
{
int i ;
if (s == NULL)
return ;
if (s->parent == NULL && s->me != NULL)
{ /* top level scope */
free (s->me->name) ;
free (s->me) ;
}
for (i = 0 ; i < s->value_idx ; i++)
if (s->values[i] != NULL)
freeValue (s->values [i]) ;
free (s->values) ;
free (s->scope_type) ;
s->parent = NULL ;
s->values = NULL ;
free (s) ;
}
char *addInteger (scope *s, const char *name, long val)
{
value *v ;
char *error ;
if ((error = checkName (currScope,name)) != NULL)
return error ;
v = (value *) calloc (1,sizeof (value)) ;
v->name = xstrdup (name) ;
v->type = intval ;
v->v.int_val = val ;
addValue (s,v) ;
return NULL ;
}
char *addChar (scope *s, const char *name, char val)
{
value *v ;
char *error ;
if ((error = checkName (currScope,name)) != NULL)
return error ;
v = (value *) calloc (1,sizeof (value)) ;
v->name = xstrdup (name) ;
v->type = charval ;
v->v.char_val = val ;
addValue (s,v) ;
return NULL ;
}
char *addBoolean (scope *s, const char *name, int val)
{
value *v ;
char *error ;
if ((error = checkName (currScope,name)) != NULL)
return error ;
v = (value *) calloc (1,sizeof (value)) ;
v->name = xstrdup (name) ;
v->type = boolval ;
v->v.bool_val = val ;
addValue (s,v) ;
return NULL ;
}
char *addReal (scope *s, const char *name, double val)
{
value *v ;
char *error ;
if ((error = checkName (currScope,name)) != NULL)
return error ;
v = (value *) calloc (1,sizeof (value)) ;
v->name = xstrdup (name) ;
v->type = realval ;
v->v.real_val = val ;
addValue (s,v) ;
return NULL ;
}
char *addString (scope *s, const char *name, const char *val)
{
value *v ;
char *error ;
if ((error = checkName (currScope,name)) != NULL)
return error ;
v = (value *) calloc (1,sizeof (value)) ;
v->name = xstrdup (name) ;
v->type = stringval ;
v->v.charp_val = xstrdup (val) ;
addValue (s,v) ;
return NULL ;
}
value *findValue (scope *s, const char *name, int inherit)
{
const char *p ;
if (name == NULL || *name == '\0')
return NULL ;
if (*name == ':')
return findValue (topScope,name + 1,0) ;
else if (s == NULL)
return findValue (topScope,name,0) ;
else
{
int i ;
if ((p = strchr (name,':')) == NULL)
p = name + strlen (name) ;
for (i = 0 ; i < s->value_idx ; i++)
{
if (strlen (s->values[i]->name) == (size_t) (p - name) &&
strncmp (s->values[i]->name,name,p - name) == 0)
{
if (*p == '\0') /* last segment of name */
return s->values[i] ;
else if (s->values[i]->type != scopeval)
errbuff = xstrdup ("Component not a scope") ;
else
return findValue (s->values[i]->v.scope_val,p + 1,0) ;
}
}
/* not in this scope. Go up if inheriting values and only if no ':'
in name */
if (inherit && *p == '\0')
return findValue (s->parent,name,inherit) ;
}
return NULL ;
}
/* find the scope that name belongs to. If mustExist is true then the name
must be a fully scoped name of a value. relative scopes start at s. */
scope *findScope (scope *s, const char *name, int mustExist)
{
scope *p = NULL ;
char *q ;
int i ;
if ((q = strchr (name,':')) == NULL)
{
if (!mustExist)
p = s ;
else
for (i = 0 ; p == NULL && i < s->value_idx ; i++)
if (strcmp (s->values[i]->name,name) == 0)
p = s ;
return p ;
}
else if (*name == ':')
{
while (s->parent != NULL)
s = s->parent ;
return findScope (s,name + 1,mustExist) ;
}
else
{
for (i = 0 ; i < s->value_idx ; i++)
if (strncmp (s->values[i]->name,name,q - name) == 0)
if (s->values[i]->type == scopeval)
return findScope (s->values[i]->v.scope_val,q + 1,mustExist) ;
}
return NULL ;
}
/****************************************************************************/
/* */
/****************************************************************************/
static void appendName (scope *s, char *p, size_t len)
{
if (s == NULL)
return ;
else
{
appendName (s->parent,p,len) ;
strlcat (p,s->me->name,len) ;
strlcat (p,":",len) ;
}
}
static char *valueScopedName (value *v)
{
scope *p = v->myscope ;
int len = strlen (v->name) ;
char *q ;
while (p != NULL)
{
len += strlen (p->me->name) + 1 ;
p = p->parent ;
}
len++;
q = xmalloc (len) ;
q [0] = '\0' ;
appendName (v->myscope,q,len) ;
strlcat (q,v->name,len) ;
return q ;
}
static void freeValue (value *v)
{
free (v->name) ;
switch (v->type)
{
case scopeval:
freeScopeTree (v->v.scope_val) ;
break ;
case stringval:
free (v->v.charp_val) ;
break ;
default:
break ;
}
free (v) ;
}
static char *checkName (scope *s, const char *name)
{
int i ;
char *error = NULL ;
if (s == NULL)
return NULL ;
for (i = 0 ; i < s->value_idx ; i++)
{
char *n = NULL ;
if (strcmp (name,s->values [i]->name) == 0) {
n = valueScopedName (s->values[i]) ;
error = concat ("Two definitions of ", n, (char *) 0) ;
free (n) ;
return error ;
}
}
return error ;
}
static void addValue (scope *s, value *v)
{
v->myscope = s ;
if (s == NULL)
return ;
if (s->value_count == s->value_idx)
{
if (s->values == 0)
{
s->values = (value **) calloc (10,sizeof (value *)) ;
s->value_count = 10 ;
}
else
{
s->value_count += 10 ;
s->values = (value **) realloc (s->values,
sizeof (value *) * s->value_count);
}
}
s->values [s->value_idx++] = v ;
}
static char *addScope (scope *s, const char *name, scope *val)
{
value *v ;
char *error ;
if ((error = checkName (s,name)) != NULL)
return error ;
v = (value *) calloc (1,sizeof (value)) ;
v->name = xstrdup (name) ;
v->type = scopeval ;
v->v.scope_val = val ;
val->me = v ;
val->parent = s ;
addValue (s,v) ;
currScope = val ;
return NULL ;
}
static void printScope (FILE *fp, scope *s, int indent)
{
int i ;
for (i = 0 ; i < s->value_idx ; i++)
printValue (fp,s->values [i],indent + 5) ;
}
static void printValue (FILE *fp, value *v, int indent)
{
int i ;
for (i = 0 ; i < indent ; i++)
fputc (' ',fp) ;
switch (v->type)
{
case intval:
fprintf (fp,"%s : %ld # INTEGER\n",v->name,v->v.int_val) ;
break ;
case stringval:
fprintf (fp,"%s : \"",v->name) ;
{
char *p = v->v.charp_val ;
while (*p)
{
if (*p == '"' || *p == '\\')
fputc ('\\',fp) ;
fputc (*p,fp) ;
p++ ;
}
}
fprintf (fp,"\" # STRING\n") ;
break ;
case charval:
fprintf (fp,"%s : %c",v->name,047) ;
switch (v->v.char_val)
{
case '\\':
fprintf (fp,"\\\\") ;
break ;
default:
if (isprint((unsigned char) v->v.char_val))
fprintf (fp,"%c",v->v.char_val) ;
else
fprintf (fp,"\\%03o",v->v.char_val) ;
}
fprintf (fp,"%c # CHARACTER\n",047) ;
break ;
case realval:
fprintf (fp,"%s : %f # REAL\n",v->name,v->v.real_val) ;
break ;
case boolval:
fprintf (fp,"%s : %s # BOOLEAN\n",
v->name,(v->v.bool_val ? "true" : "false")) ;
break ;
case scopeval:
fprintf (fp,"%s %s { # SCOPE\n",v->v.scope_val->scope_type,v->name) ;
printScope (fp,v->v.scope_val,indent + 5) ;
for (i = 0 ; i < indent ; i++)
fputc (' ',fp) ;
fprintf (fp,"}\n") ;
break ;
default:
fprintf (fp,"UNKNOWN value type: %d\n",v->type) ;
exit (1) ;
}
}
static scope *newScope (const char *type)
{
scope *t ;
int i ;
t = (scope *) calloc (1,sizeof (scope)) ;
t->parent = NULL ;
t->scope_type = xstrdup (type) ;
for (i = 0 ; t->scope_type[i] != '\0' ; i++)
t->scope_type[i] = tolower ((unsigned char) t->scope_type[i]) ;
return t ;
}
#define BAD_KEY "line %d: illegal key name: %s"
#define NON_ALPHA "line %d: keys must start with a letter: %s"
static char *keyOk (const char *key)
{
const char *p = key ;
char *rval ;
if (key == NULL)
{
rval = xmalloc (strlen ("line : NULL key") + 15) ;
sprintf (rval,"line %d: NULL key", lineCount) ;
return rval ;
}
else if (*key == '\0')
{
rval = xmalloc (strlen ("line : EMPTY KEY") + 15) ;
sprintf (rval,"line %d: EMPTY KEY", lineCount) ;
return rval ;
}
if (!isalpha((unsigned char) *p))
{
rval = xmalloc (strlen (NON_ALPHA) + strlen (key) + 15) ;
sprintf (rval,NON_ALPHA,lineCount, key) ;
return rval ;
}
p++ ;
while (*p)
{
if (!(isalnum((unsigned char) *p) || *p == '_' || *p == '-'))
{
rval = xmalloc (strlen (BAD_KEY) + strlen (key) + 15) ;
sprintf (rval,BAD_KEY,lineCount,key) ;
return rval ;
}
p++ ;
}
return NULL ;
}
static PFIVP *funcs = NULL ;
static void **args = NULL ;
static int funcCount ;
static int funcIdx ;
void configAddLoadCallback (PFIVP func,void *arg)
{
if (func == NULL)
return ;
if (funcIdx == funcCount)
{
funcCount += 10 ;
if (funcs == NULL)
{
funcs = xmalloc (sizeof (PFIVP) * funcCount);
args = xmalloc (sizeof (void *) * funcCount) ;
}
else
{
funcs = xrealloc (funcs,sizeof (PFIVP) * funcCount);
args = xrealloc (args,sizeof (void *) * funcCount) ;
}
}
args [funcIdx] = arg ;
funcs [funcIdx++] = func ;
}
void configRemoveLoadCallback (PFIVP func)
{
int i, j ;
for (i = 0 ; i < funcIdx ; i++)
if (funcs [i] == func)
break ;
for (j = i ; j < funcIdx - 1 ; j++)
{
funcs [j] = funcs [j + 1] ;
args [j] = args [j + 1] ;
}
if (funcIdx > 1 && i < funcIdx)
{
funcs [i - 2] = funcs [i - 1] ;
args [i - 2] = args [i - 1] ;
}
if (funcIdx > 0 && i < funcIdx)
funcIdx-- ;
}
static int doCallbacks (void)
{
int i ;
int rval = 1 ;
for (i = 0 ; i < funcIdx ; i++)
if (funcs [i] != NULL)
rval = (funcs[i](args [i]) && rval) ;
return rval ;
}
static char *key ;
%}
%union{
scope *scp ;
value *val ;
char *name ;
int integer ;
double real ;
char *string ;
char chr ;
}
%token PEER
%token GROUP
%token IVAL
%token RVAL
%token NAME
%token XSTRING
%token SCOPE
%token COLON
%token LBRACE
%token RBRACE
%token TRUEBVAL
%token FALSEBVAL
%token CHAR
%token WORD
%token IP_ADDRESS
%type <integer> IVAL
%type <real> RVAL
%type <string> XSTRING
%type <chr> CHAR
%type <name> TRUEBVAL FALSEBVAL WORD
%%
input: {
lineCount = 1 ;
addScope (NULL,"",newScope ("")) ;
topScope = currScope ;
} entries { if (!doCallbacks()) YYABORT ; } ;
scope: entries ;
entries:
| entries entry
| entries error {
errbuff = xmalloc (strlen(SYNTAX_ERROR) + 12) ;
sprintf (errbuff,SYNTAX_ERROR,lineCount) ;
YYABORT ;
}
;
entry: PEER WORD LBRACE {
errbuff = addScope (currScope,$2,newScope ("peer")) ;
free ($2) ;
if (errbuff != NULL) YYABORT ;
} scope RBRACE {
currScope = currScope->parent ;
}
| GROUP WORD LBRACE {
errbuff = addScope (currScope,$2,newScope ("group")) ;
free ($2) ;
if (errbuff != NULL) YYABORT ;
} scope RBRACE {
currScope = currScope->parent ;
}
| WORD WORD LBRACE {
errbuff = xmalloc (strlen(UNKNOWN_SCOPE_TYPE) + 15 +
strlen ($1)) ;
sprintf (errbuff,UNKNOWN_SCOPE_TYPE,lineCount,$1) ;
free ($1) ;
free ($2) ;
YYABORT ;
}
| WORD {
if ((errbuff = keyOk($1)) != NULL) {
YYABORT ;
} else
key = $1 ;
} COLON value ;
value: WORD {
if ((errbuff = addString (currScope, key, $1)) != NULL)
YYABORT ;
free (key) ;
free ($1) ;
}
| IVAL {
if ((errbuff = addInteger(currScope, key, $1)) != NULL)
YYABORT;
free (key) ;
}
| TRUEBVAL {
if ((errbuff = addBoolean (currScope, key, 1)) != NULL)
YYABORT ;
free (key) ;
free ($1) ;
}
| FALSEBVAL {
if ((errbuff = addBoolean (currScope, key, 0)) != NULL)
YYABORT ;
free (key) ;
free ($1) ;
}
| RVAL {
if ((errbuff = addReal (currScope, key, $1)) != NULL)
YYABORT ;
free (key) ;
}
| XSTRING {
if ((errbuff = addString (currScope, key, $1)) != NULL)
YYABORT;
free (key) ;
}
| CHAR {
if ((errbuff = addChar (currScope, key, $1)) != NULL)
YYABORT ;
free (key) ;
}
;
%%
int yyerror (const char *s)
{
#undef FMT
#define FMT "line %d: %s"
errbuff = xmalloc (strlen (s) + strlen (FMT) + 20) ;
sprintf (errbuff,FMT,lineCount,s) ;
return 0 ;
}
int yywrap (void)
{
return 1 ;
}
extern FILE *yyin ;
int yydebug ;
#define NO_INHERIT 0
#if ! defined (WANT_MAIN)
struct peer_table_s
{
char *peerName ;
value *peerValue ;
} ;
static struct peer_table_s *peerTable ;
static int peerTableCount ;
static int peerTableIdx ;
void configCleanup (void)
{
int i ;
for (i = 0 ; i < peerTableIdx ; i++)
free (peerTable[i].peerName) ;
free (peerTable) ;
freeScopeTree (topScope);
free (funcs) ;
free (args) ;
}
int buildPeerTable (FILE *fp, scope *s)
{
int rval = 1 ;
int i, j ;
for (i = 0 ; i < s->value_idx ; i++)
{
if (ISSCOPE (s->values[i]) && ISPEER (s->values[i]))
{
for (j = 0 ; j < peerTableIdx ; j++)
{
if (strcmp (peerTable[j].peerName,s->values[i]->name) == 0)
{
logOrPrint (LOG_ERR,fp,
"ME config: two peers with the same name: %s",
peerTable[j].peerName) ;
rval = 0 ;
break ;
}
}
if (j == peerTableIdx)
{
if (peerTableCount == peerTableIdx)
{
peerTableCount += 10 ;
if (peerTable == NULL)
peerTable = xmalloc (sizeof(struct peer_table_s)
* peerTableCount) ;
else
peerTable = xrealloc (peerTable,
sizeof(struct peer_table_s)
* peerTableCount) ;
}
peerTable[peerTableIdx].peerName = xstrdup (s->values[i]->name);
peerTable[peerTableIdx].peerValue = s->values[i] ;
peerTableIdx++ ;
}
}
else if (ISSCOPE (s->values[i]))
rval = (buildPeerTable (fp,s->values[i]->v.scope_val) && rval) ;
}
return rval ;
}
/* read the config file. Any errors go to errorDest if it is non-NULL,
otherwise they are syslogged. If justCheck is true then return after
parsing */
static int inited = 0 ;
int readConfig (const char *file, FILE *errorDest, int justCheck, int dump)
{
scope *oldTop = topScope ;
FILE *fp ;
int rval ;
if (!inited)
{
inited = 1 ;
yydebug = (getenv ("YYDEBUG") == NULL ? 0 : 1) ;
if (yydebug)
atexit (configCleanup) ;
}
if (file == NULL || strlen (file) == 0 || !fileExistsP (file))
{
logOrPrint (LOG_ERR,errorDest,
"ME config aborting, no such config file: %s",
file ? file : "(null)") ;
d_printf (1,"No such config file: %s\n", file ? file : "(null)") ;
exit (1) ;
}
if ((fp = fopen (file,"r")) == NULL)
{
logOrPrint (LOG_ERR,errorDest, "ME config aborting fopen %s: %s",
file, strerror (errno)) ;
exit (1) ;
}
logOrPrint (LOG_NOTICE,errorDest,"loading %s", file) ;
yyin = fp ;
topScope = NULL ;
rval = yyparse () ;
fclose (fp) ;
if (rval != 0) /* failure */
{
freeScopeTree (topScope) ;
if (justCheck)
freeScopeTree (oldTop) ;
else
topScope = oldTop ;
topScope = NULL ;
if (errbuff != NULL)
{
if (errorDest != NULL)
fprintf (errorDest,"config file error: %s\n",errbuff) ;
else
warn ("ME config file error: %s", errbuff) ;
free (errbuff) ;
}
return 0 ;
}
if (dump)
{
fprintf (errorDest ? errorDest : stderr,"Parsed config file:\n") ;
printScope (errorDest ? errorDest : stderr,topScope,-5) ;
fprintf (errorDest ? errorDest : stderr,"\n") ;
}
if (justCheck)
{
freeScopeTree (topScope) ;
freeScopeTree (oldTop) ;
topScope = NULL ;
}
else
{
for (peerTableIdx-- ; peerTableIdx >= 0 ; peerTableIdx--)
{
free (peerTable [peerTableIdx].peerName) ;
peerTable [peerTableIdx].peerName = NULL ;
peerTable [peerTableIdx].peerValue = NULL ;
}
peerTableIdx = 0 ;
if (!buildPeerTable (errorDest,topScope))
logAndExit (1,"Failed to build list of peers") ;
}
return 1 ;
}
value *getNextPeer (int *cookie)
{
value *rval ;
if (*cookie < 0 || *cookie >= peerTableIdx)
return NULL ;
rval = peerTable[*cookie].peerValue ;
(*cookie)++ ;
return rval ;
}
value *findPeer (const char *name)
{
value *v = NULL ;
int i ;
for (i = 0 ; i < peerTableIdx ; i++)
if (strcmp (peerTable[i].peerName,name) == 0)
{
v = peerTable[i].peerValue ;
break ;
}
return v ;
}
#endif
#if defined (WANT_MAIN)
int main (int argc, char **argv) {
if ( yyparse() )
printf ("parsing failed: %s\n",errbuff ? errbuff : "NONE") ;
else
{
printScope (stdout,topScope,-5) ;
if (argc == 3)
{
#if 0
printf ("Looking for %s of type %s: ",argv[2],argv[1]) ;
if (strncmp (argv[1],"int",3) == 0)
{
int i = 0 ;
if (!getInteger (topScope,argv[2],&i))
printf ("wasn't found.\n") ;
else
printf (" %d\n",i) ;
}
else if (strncmp (argv[1],"real",4) == 0)
{
double d = 0.0 ;
if (!getReal (topScope,argv[2],&d))
printf ("wasn't found.\n") ;
else
printf (" %0.5f\n",d) ;
}
#else
value *v = findValue (topScope,argv[1],1) ;
if (v == NULL)
printf ("Can't find %s\n",argv[1]) ;
else
{
long ival = 987654 ;
if (getInteger (v->v.scope_val,argv[2],&ival,1))
printf ("Getting %s : %ld",argv[2],ival) ;
else
printf ("Name is not legal: %s\n",argv[2]) ;
}
#endif
}
else if (argc == 2)
{
#if 1
value *v = findValue (topScope,argv[1],1) ;
if (v == NULL)
printf ("Can't find %s\n",argv[1]) ;
else
{
printf ("Getting %s : ",argv[1]) ;
printValue (stdout,v,0) ;
}
#else
if (findScope (topScope,argv[1],1) == NULL)
printf ("Can't find the scope of %s\n",argv[1]) ;
#endif
}
}
freeScopeTree (topScope) ;
return 0 ;
}
#endif /* defined (WANT_MAIN) */
|