#ifndef lint
char ConfParsersccsid[] = "@(#)yaccpar	1.4 (Berkeley) 02/25/90";
#endif
#line 4 "utils/ConfParser.ConfParser"
#include "dmem/dnew.hh"

#include <stdio.h>
#include <string.h>

#include "dummyConfLoader.hh"

#define CONF_ENV "AUV_CONFIG_DIR"

static Symbol s_fileName;
static dummyConfLoader *s_loader;
static ArgList s_args;
static Symbol s_errorBuff;

size_t ll_getLine();
void ll_reset();

int ConfParsererror(char *errorbuff);
int ConfParserlex();
int ConfParserparse();

Symbol &parsing_message(Symbol const &type);

#define warning(msg) parsing_message("warning")+=(msg)
#define error(msg) { parsing_message("error")+=(msg); return -1; }

#line 32 "utils/ConfParser.ConfParser"
typedef union {
  char *str;
  Argument *arg;
} YYSTYPE;
#line 37 "ConfParser.tab.c"
#define LSymbol 257
#define LString 258
#define LInteger 259
#define LFloat 260
#define YYERRCODE 256
short ConfParserlhs[] = {                                        -1,
    0,    3,    3,    4,    5,    5,    6,    1,    1,    2,
    2,    2,    2,
};
short ConfParserlen[] = {                                         2,
    1,    2,    0,    4,    2,    0,    4,    2,    1,    1,
    1,    1,    1,
};
short ConfParserdefred[] = {                                      0,
    0,    0,    1,    0,    0,    2,    0,    0,    0,    0,
    4,    5,   10,   11,   12,   13,    0,    0,    7,    8,
};
short ConfParserdgoto[] = {                                       2,
   17,   18,    3,    4,    8,    9,
};
short ConfParsersindex[] = {                                   -253,
 -118,    0,    0, -253, -251,    0,  -54, -117, -251, -257,
    0,    0,    0,    0,    0,    0,  -50, -257,    0,    0,
};
short ConfParserrindex[] = {                                     10,
    0,    0,    0,   10, -114,    0,    0,    0, -114,    0,
    0,    0,    0,    0,    0,    0,    0,  -47,    0,    0,
};
short ConfParsergindex[] = {                                      0,
   -5,    0,   11,    0,    5,    0,
};
#define YYTABLESIZE 15
short ConfParsertable[] = {                                      13,
   14,   15,   16,    1,    5,    7,   10,   11,   19,    3,
    6,    9,   20,   12,    6,
};
short ConfParsercheck[] = {                                     257,
  258,  259,  260,  257,  123,  257,   61,  125,   59,    0,
  125,   59,   18,    9,    4,
};
#define YYFINAL 2
#ifndef YYDEBUG
#define YYDEBUG 0
#endif
#define YYMAXTOKEN 260
#if YYDEBUG
char *ConfParsername[] = {
"end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"';'",0,"'='",0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,"'{'",0,"'}'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"LSymbol",
"LString","LInteger","LFloat",
};
char *ConfParserrule[] = {
"$accept : YConfigFile",
"YConfigFile : YArgLists",
"YArgLists : YArgList YArgLists",
"YArgLists :",
"YArgList : LSymbol '{' YAttributes '}'",
"YAttributes : YAttribute YAttributes",
"YAttributes :",
"YAttribute : LSymbol '=' YValues ';'",
"YValues : YValue YValues",
"YValues : YValue",
"YValue : LSymbol",
"YValue : LString",
"YValue : LInteger",
"YValue : LFloat",
};
#endif
#define ConfParserclearin (ConfParserchar=(-1))
#define ConfParsererrok (ConfParsererrflag=0)
#ifndef YYSTACKSIZE
#ifdef YYMAXDEPTH
#define YYSTACKSIZE YYMAXDEPTH
#else
#define YYSTACKSIZE 300
#endif
#endif
int ConfParserdebug;
int ConfParsernerrs;
int ConfParsererrflag;
int ConfParserchar;
short *ConfParserssp;
YYSTYPE *ConfParservsp;
YYSTYPE ConfParserval;
YYSTYPE ConfParserlval;
#define ConfParserstacksize YYSTACKSIZE
short ConfParserss[YYSTACKSIZE];
YYSTYPE ConfParservs[YYSTACKSIZE];
#line 121 "utils/ConfParser.ConfParser"

extern char *ConfParsertext;
extern FILE *ConfParserin;

int ConfParsererror(char *errorBuff) {
  parsing_message("error") += Symbol(errorBuff)+
    "\n\tOffending text: \""+ConfParsertext+"\"";
  return 0;
}

Symbol &parsing_message(Symbol const &type) {
  char lineVal[10];

  if( !s_errorBuff.empty() )
    s_errorBuff += ".\n";
  sprintf(lineVal, "%u", ll_getLine());
  s_errorBuff += s_fileName+":"+lineVal+":"+type+": ";
  return s_errorBuff;
}

/*
 * class ConfParser
 */
void ConfParser::parse(Symbol const &fileName, dummyConfLoader &loader) {
  int ret;
  
  ll_reset();
  s_loader = &loader;
  s_args.clear();
  s_errorBuff.clear();

  ConfParserin = fopen(fileName.c_str(), "r");

  if( NULL==ConfParserin ) {
    char *conf_dir;
    
    if( NULL!=(conf_dir = getenv(CONF_ENV)) ) {
      Symbol confFile(conf_dir);

      confFile += "/"+fileName;
      ConfParserin = fopen(confFile.c_str(), "r");
    } else 
      s_errorBuff += Symbol("warning: $")+CONF_ENV+" is undefined";
  }
  if( NULL==ConfParserin ) {
    s_errorBuff += "Unable to open "+fileName;
    throw ParseError(s_errorBuff);
  }
  s_fileName = fileName;
  ret = ConfParserparse();

  if( 0!=ret ) 
    throw ParseError(s_errorBuff);
  else if( !s_errorBuff.empty() )
    fprintf(stderr, "%s\n", s_errorBuff.c_str());
}
#line 188 "ConfParser.tab.c"
#define YYABORT goto ConfParserabort
#define YYACCEPT goto ConfParseraccept
#define YYERROR goto ConfParsererrlab
int
ConfParserparse()
{
    register int ConfParserm, ConfParsern, ConfParserstate;
#if YYDEBUG
    register char *ConfParsers;
    extern char *getenv();

    if (ConfParsers = getenv("YYDEBUG"))
    {
        ConfParsern = *ConfParsers;
        if (ConfParsern >= '0' && ConfParsern <= '9')
            ConfParserdebug = ConfParsern - '0';
    }
#endif

    ConfParsernerrs = 0;
    ConfParsererrflag = 0;
    ConfParserchar = (-1);

    ConfParserssp = ConfParserss;
    ConfParservsp = ConfParservs;
    *ConfParserssp = ConfParserstate = 0;

ConfParserloop:
    if (ConfParsern = ConfParserdefred[ConfParserstate]) goto ConfParserreduce;
    if (ConfParserchar < 0)
    {
        if ((ConfParserchar = ConfParserlex()) < 0) ConfParserchar = 0;
#if YYDEBUG
        if (ConfParserdebug)
        {
            ConfParsers = 0;
            if (ConfParserchar <= YYMAXTOKEN) ConfParsers = ConfParsername[ConfParserchar];
            if (!ConfParsers) ConfParsers = "illegal-symbol";
            printf("ConfParserdebug: state %d, reading %d (%s)\n", ConfParserstate,
                    ConfParserchar, ConfParsers);
        }
#endif
    }
    if ((ConfParsern = ConfParsersindex[ConfParserstate]) && (ConfParsern += ConfParserchar) >= 0 &&
            ConfParsern <= YYTABLESIZE && ConfParsercheck[ConfParsern] == ConfParserchar)
    {
#if YYDEBUG
        if (ConfParserdebug)
            printf("ConfParserdebug: state %d, shifting to state %d\n",
                    ConfParserstate, ConfParsertable[ConfParsern]);
#endif
        if (ConfParserssp >= ConfParserss + ConfParserstacksize - 1)
        {
            goto ConfParseroverflow;
        }
        *++ConfParserssp = ConfParserstate = ConfParsertable[ConfParsern];
        *++ConfParservsp = ConfParserlval;
        ConfParserchar = (-1);
        if (ConfParsererrflag > 0)  --ConfParsererrflag;
        goto ConfParserloop;
    }
    if ((ConfParsern = ConfParserrindex[ConfParserstate]) && (ConfParsern += ConfParserchar) >= 0 &&
            ConfParsern <= YYTABLESIZE && ConfParsercheck[ConfParsern] == ConfParserchar)
    {
        ConfParsern = ConfParsertable[ConfParsern];
        goto ConfParserreduce;
    }
    if (ConfParsererrflag) goto ConfParserinrecovery;
#ifdef lint
    goto ConfParsernewerror;
#endif
ConfParsernewerror:
    ConfParsererror("syntax error");
#ifdef lint
    goto ConfParsererrlab;
#endif
ConfParsererrlab:
    ++ConfParsernerrs;
ConfParserinrecovery:
    if (ConfParsererrflag < 3)
    {
        ConfParsererrflag = 3;
        for (;;)
        {
            if ((ConfParsern = ConfParsersindex[*ConfParserssp]) && (ConfParsern += YYERRCODE) >= 0 &&
                    ConfParsern <= YYTABLESIZE && ConfParsercheck[ConfParsern] == YYERRCODE)
            {
#if YYDEBUG
                if (ConfParserdebug)
                    printf("ConfParserdebug: state %d, error recovery shifting\
 to state %d\n", *ConfParserssp, ConfParsertable[ConfParsern]);
#endif
                if (ConfParserssp >= ConfParserss + ConfParserstacksize - 1)
                {
                    goto ConfParseroverflow;
                }
                *++ConfParserssp = ConfParserstate = ConfParsertable[ConfParsern];
                *++ConfParservsp = ConfParserlval;
                goto ConfParserloop;
            }
            else
            {
#if YYDEBUG
                if (ConfParserdebug)
                    printf("ConfParserdebug: error recovery discarding state %d\n",
                            *ConfParserssp);
#endif
                if (ConfParserssp <= ConfParserss) goto ConfParserabort;
                --ConfParserssp;
                --ConfParservsp;
            }
        }
    }
    else
    {
        if (ConfParserchar == 0) goto ConfParserabort;
#if YYDEBUG
        if (ConfParserdebug)
        {
            ConfParsers = 0;
            if (ConfParserchar <= YYMAXTOKEN) ConfParsers = ConfParsername[ConfParserchar];
            if (!ConfParsers) ConfParsers = "illegal-symbol";
            printf("ConfParserdebug: state %d, error recovery discards token %d (%s)\n",
                    ConfParserstate, ConfParserchar, ConfParsers);
        }
#endif
        ConfParserchar = (-1);
        goto ConfParserloop;
    }
ConfParserreduce:
#if YYDEBUG
    if (ConfParserdebug)
        printf("ConfParserdebug: state %d, reducing by rule %d (%s)\n",
                ConfParserstate, ConfParsern, ConfParserrule[ConfParsern]);
#endif
    ConfParserm = ConfParserlen[ConfParsern];
    ConfParserval = ConfParservsp[1-ConfParserm];
    switch (ConfParsern)
    {
case 1:
#line 43 "utils/ConfParser.ConfParser"
{}
break;
case 2:
#line 46 "utils/ConfParser.ConfParser"
{}
break;
case 3:
#line 49 "utils/ConfParser.ConfParser"
{}
break;
case 4:
#line 52 "utils/ConfParser.ConfParser"
{
  s_args.setId(ConfParservsp[-3].str );
  free(ConfParservsp[-3].str );
  try {
#ifdef DEBUG
    Syslog::write("loading %s {", s_args.getId().c_str());
    ArgList::range_type lst = s_args.getRange("");
    for( ; lst.first!=lst.second; ++lst.first ) {
      Syslog::write("  - %s = %s ...", lst.first->first.c_str(), 
		    lst.first->second->c_str());
    }
    Syslog::write("}");
#endif /**/
    s_loader->parseEvent(s_args);
  } catch(ParseError &pe) {
    error(pe.msg);
  } catch(Exception &e) {
    error(e.msg);
  }
  s_args.clear();
}
break;
case 5:
#line 75 "utils/ConfParser.ConfParser"
{}
break;
case 6:
#line 78 "utils/ConfParser.ConfParser"
{}
break;
case 7:
#line 81 "utils/ConfParser.ConfParser"
{
  if( !s_args.add(ConfParservsp[-3].str , *ConfParservsp[-1].arg , true) ) 
    warning(Symbol("Attribute ")+ConfParservsp[-3].str +" multiply defined");
  free(ConfParservsp[-3].str );
  delete ConfParservsp[-1].arg ;
}
break;
case 8:
#line 89 "utils/ConfParser.ConfParser"
{
  ConfParserval.arg  = ConfParservsp[-1].arg ;
  (*ConfParserval.arg ) += *ConfParservsp[0].arg ;
  delete ConfParservsp[0].arg ;
}
break;
case 9:
#line 96 "utils/ConfParser.ConfParser"
{
  ConfParserval.arg  = ConfParservsp[0].arg ;
}
break;
case 10:
#line 101 "utils/ConfParser.ConfParser"
{
  ConfParserval.arg  = new Argument(ConfParservsp[0].str );
}
break;
case 11:
#line 106 "utils/ConfParser.ConfParser"
{
  ConfParserval.arg  = new Argument(ConfParservsp[0].str );
}
break;
case 12:
#line 111 "utils/ConfParser.ConfParser"
{
  ConfParserval.arg  = new Argument(ConfParservsp[0].str );
}
break;
case 13:
#line 116 "utils/ConfParser.ConfParser"
{
  ConfParserval.arg  = new Argument(ConfParservsp[0].str );
}
break;
#line 419 "ConfParser.tab.c"
    }
    ConfParserssp -= ConfParserm;
    ConfParserstate = *ConfParserssp;
    ConfParservsp -= ConfParserm;
    ConfParserm = ConfParserlhs[ConfParsern];
    if (ConfParserstate == 0 && ConfParserm == 0)
    {
#ifdef YYDEBUG
        if (ConfParserdebug)
            printf("ConfParserdebug: after reduction, shifting from state 0 to\
 state %d\n", YYFINAL);
#endif
        ConfParserstate = YYFINAL;
        *++ConfParserssp = YYFINAL;
        *++ConfParservsp = ConfParserval;
        if (ConfParserchar < 0)
        {
            if ((ConfParserchar = ConfParserlex()) < 0) ConfParserchar = 0;
#if YYDEBUG
            if (ConfParserdebug)
            {
                ConfParsers = 0;
                if (ConfParserchar <= YYMAXTOKEN) ConfParsers = ConfParsername[ConfParserchar];
                if (!ConfParsers) ConfParsers = "illegal-symbol";
                printf("ConfParserdebug: state %d, reading %d (%s)\n",
                        YYFINAL, ConfParserchar, ConfParsers);
            }
#endif
        }
        if (ConfParserchar == 0) goto ConfParseraccept;
        goto ConfParserloop;
    }
    if ((ConfParsern = ConfParsergindex[ConfParserm]) && (ConfParsern += ConfParserstate) >= 0 &&
            ConfParsern <= YYTABLESIZE && ConfParsercheck[ConfParsern] == ConfParserstate)
        ConfParserstate = ConfParsertable[ConfParsern];
    else
        ConfParserstate = ConfParserdgoto[ConfParserm];
#ifdef YYDEBUG
    if (ConfParserdebug)
        printf("ConfParserdebug: after reduction, shifting from state %d \
to state %d\n", *ConfParserssp, ConfParserstate);
#endif
    if (ConfParserssp >= ConfParserss + ConfParserstacksize - 1)
    {
        goto ConfParseroverflow;
    }
    *++ConfParserssp = ConfParserstate;
    *++ConfParservsp = ConfParserval;
    goto ConfParserloop;
ConfParseroverflow:
    ConfParsererror("yacc stack overflow");
ConfParserabort:
    return (1);
ConfParseraccept:
    return (0);
}
