#include <sys/cdefs.h>
#ifndef lint
#if 0
static char yysccsid[] = "@(#)yaccpar	1.9 (Berkeley) 02/21/93";
#else
__IDSTRING(yyrcsid, "$NetBSD: skeleton.c,v 1.14 1997/10/20 03:41:16 lukem Exp $");
#endif
#endif
#include <stdlib.h>
#define YYBYACC 1
#define YYMAJOR 1
#define YYMINOR 9
#define YYLEX yylex()
#define YYEMPTY -1
#define yyclearin (yychar=(YYEMPTY))
#define yyerrok (yyerrflag=0)
#define YYRECOVERING (yyerrflag!=0)
#define yyparse conf_parse
#define yylex conf_lex
#define yyerror conf_error
#define yychar conf_char
#define yyval conf_val
#define yylval conf_lval
#define yydebug conf_debug
#define yynerrs conf_nerrs
#define yyerrflag conf_errflag
#define yyss conf_ss
#define yysslim conf_sslim
#define yyssp conf_ssp
#define yyvs conf_vs
#define yyvsp conf_vsp
#define yystacksize conf_stacksize
#define yylhs conf_lhs
#define yylen conf_len
#define yydefred conf_defred
#define yydgoto conf_dgoto
#define yysindex conf_sindex
#define yyrindex conf_rindex
#define yygindex conf_gindex
#define yytable conf_table
#define yycheck conf_check
#define yyname conf_name
#define yyrule conf_rule
#define YYPREFIX "conf_"
#line 19 "conf_parse.y"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "types.h"
#include "conf_parse.h"
#include "conf_variable.h"



extern int conf_lineno;


int conf_lex(void);
int conf_parse(void);
void conf_restart(FILE *input_file);
void conf_error(const char *str);

variable_list_t variable_list;

#line 51 "conf_parse.y"
typedef union {
	char *string;
	struct variable* v;
} YYSTYPE;
#line 73 "yacc.conf_parse.tab.c"
#define VARIABLE 257
#define STRING 258
#define CIDR 259
#define RANGE 260
#define SINGLEIP 261
#define HOSTNAME 262
#define QUOTE 263
#define OBRACE 264
#define EBRACE 265
#define OPAREN 266
#define EPAREN 267
#define SEMICOLON 268
#define COMMA 269
#define ASSIGN 270
#define STDINTOK 271
#define STDOUTTOK 272
#define TEXTTOK 273
#define CIDRTOK 274
#define RANGETOK 275
#define SINGLETOK 276
#define LOADTOK 277
#define SAVETOK 278
#define UNIONTOK 279
#define DIFFERENCETOK 280
#define INTERSECTTOK 281
#define INVERTTOK 282
#define P2BTOK 283
#define YYERRCODE 256
short conf_lhs[] = {                                        -1,
    0,    0,   10,   10,    2,    1,    1,    1,    1,    1,
    1,    1,    1,   11,    4,    4,    4,    4,    4,    4,
    4,    4,    3,    3,    5,    5,    5,    5,    6,    7,
    8,    9,   12,   12,   12,   12,   12,   12,
};
short conf_len[] = {                                         2,
    3,    0,    1,    1,    3,    1,    1,    1,    1,    1,
    1,    1,    1,    1,    1,    1,    1,    1,    3,    3,
    3,    3,    2,    3,    6,    6,    6,    6,    6,    6,
    6,    4,    8,    8,    8,    8,    8,    8,
};
short conf_defred[] = {                                      2,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    3,
    7,    8,    9,   10,   11,   12,   13,    0,    4,   14,
    0,   15,   16,   17,   18,   23,    0,    0,    0,    0,
    0,    0,    0,    1,    5,   24,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,   19,   20,   21,   22,
    0,    0,    0,    0,    0,    0,    0,    0,   32,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,   25,   27,   26,   28,    0,    0,    0,    0,
    0,    0,   29,   30,   31,    0,    0,    0,    0,    0,
    0,   33,   36,   34,   37,   35,   38,
};
short conf_dgoto[] = {                                       1,
   10,   11,   12,   27,   13,   14,   15,   16,   17,   18,
   19,   20,
};
short conf_sindex[] = {                                      0,
 -256, -268, -221, -232, -215, -211, -201, -200, -199,    0,
    0,    0,    0,    0,    0,    0,    0, -225,    0,    0,
 -250,    0,    0,    0,    0,    0, -223, -267, -222, -250,
 -250, -250, -250,    0,    0,    0, -212, -224, -198, -197,
 -196, -195, -194, -193, -192, -189,    0,    0,    0,    0,
 -243, -238, -255, -254, -253, -250, -250, -250,    0, -188,
 -187, -186, -185, -184, -183, -182, -181, -180, -179, -176,
 -175, -174,    0,    0,    0,    0, -250, -250, -250, -250,
 -250, -250,    0,    0,    0, -173, -172, -171, -170, -169,
 -168,    0,    0,    0,    0,    0,    0,
};
short conf_rindex[] = {                                      0,
    0, -205,    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,
};
short conf_gindex[] = {                                      0,
  -21,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,
};
#define YYTABLESIZE 99
short conf_table[] = {                                      35,
    2,   21,   64,   66,   68,   38,    2,    3,   43,   44,
   45,   46,    0,    3,   60,   39,   65,   67,   69,   62,
    4,    5,    6,    7,    8,    9,    4,   61,    6,    7,
    8,    9,   63,   28,   70,   71,   72,   22,   23,   24,
   25,   36,   34,   26,   51,   37,   47,   48,   49,   50,
   29,   40,   41,   42,   30,   86,   87,   88,   89,   90,
   91,    6,    6,    6,   31,   32,   33,    0,    0,    0,
   52,   53,   54,   55,   56,   57,   58,   59,   73,   74,
   75,   76,    0,    0,   77,   78,   79,   80,   81,   82,
   83,   84,   85,   92,   93,   94,   95,   96,   97,
};
short conf_check[] = {                                      21,
  257,  270,  258,  258,  258,  273,  257,  264,   30,   31,
   32,   33,   -1,  264,  258,  283,  272,  272,  272,  258,
  277,  278,  279,  280,  281,  282,  277,  271,  279,  280,
  281,  282,  271,  266,   56,   57,   58,  259,  260,  261,
  262,  265,  268,  265,  269,  269,  259,  260,  261,  262,
  266,  274,  275,  276,  266,   77,   78,   79,   80,   81,
   82,  267,  268,  269,  266,  266,  266,   -1,   -1,   -1,
  269,  269,  269,  269,  269,  269,  269,  267,  267,  267,
  267,  267,   -1,   -1,  269,  269,  269,  269,  269,  269,
  267,  267,  267,  267,  267,  267,  267,  267,  267,
};
#define YYFINAL 1
#ifndef YYDEBUG
#define YYDEBUG 0
#endif
#define YYMAXTOKEN 283
#if YYDEBUG
char *conf_name[] = {
"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,0,0,0,0,"VARIABLE","STRING","CIDR",
"RANGE","SINGLEIP","HOSTNAME","QUOTE","OBRACE","EBRACE","OPAREN","EPAREN",
"SEMICOLON","COMMA","ASSIGN","STDINTOK","STDOUTTOK","TEXTTOK","CIDRTOK",
"RANGETOK","SINGLETOK","LOADTOK","SAVETOK","UNIONTOK","DIFFERENCETOK",
"INTERSECTTOK","INVERTTOK","P2BTOK",
};
char *conf_rule[] = {
"$accept : statements",
"statements : statements statement SEMICOLON",
"statements :",
"statement : table_statement",
"statement : void_statement",
"assign : VARIABLE ASSIGN table_statement",
"table_statement : VARIABLE",
"table_statement : assign",
"table_statement : table_literal",
"table_statement : load_statement",
"table_statement : union_statement",
"table_statement : difference_statement",
"table_statement : intersection_statement",
"table_statement : invert_statement",
"void_statement : save_statement",
"table_literal_entry : CIDR",
"table_literal_entry : RANGE",
"table_literal_entry : SINGLEIP",
"table_literal_entry : HOSTNAME",
"table_literal_entry : table_literal_entry COMMA CIDR",
"table_literal_entry : table_literal_entry COMMA RANGE",
"table_literal_entry : table_literal_entry COMMA SINGLEIP",
"table_literal_entry : table_literal_entry COMMA HOSTNAME",
"table_literal : OBRACE EBRACE",
"table_literal : OBRACE table_literal_entry EBRACE",
"load_statement : LOADTOK OPAREN TEXTTOK COMMA STRING EPAREN",
"load_statement : LOADTOK OPAREN P2BTOK COMMA STRING EPAREN",
"load_statement : LOADTOK OPAREN TEXTTOK COMMA STDINTOK EPAREN",
"load_statement : LOADTOK OPAREN P2BTOK COMMA STDINTOK EPAREN",
"union_statement : UNIONTOK OPAREN table_statement COMMA table_statement EPAREN",
"difference_statement : DIFFERENCETOK OPAREN table_statement COMMA table_statement EPAREN",
"intersection_statement : INTERSECTTOK OPAREN table_statement COMMA table_statement EPAREN",
"invert_statement : INVERTTOK OPAREN table_statement EPAREN",
"save_statement : SAVETOK OPAREN CIDRTOK COMMA STRING COMMA table_statement EPAREN",
"save_statement : SAVETOK OPAREN RANGETOK COMMA STRING COMMA table_statement EPAREN",
"save_statement : SAVETOK OPAREN SINGLETOK COMMA STRING COMMA table_statement EPAREN",
"save_statement : SAVETOK OPAREN CIDRTOK COMMA STDOUTTOK COMMA table_statement EPAREN",
"save_statement : SAVETOK OPAREN RANGETOK COMMA STDOUTTOK COMMA table_statement EPAREN",
"save_statement : SAVETOK OPAREN SINGLETOK COMMA STDOUTTOK COMMA table_statement EPAREN",
};
#endif
#ifdef YYSTACKSIZE
#undef YYMAXDEPTH
#define YYMAXDEPTH YYSTACKSIZE
#else
#ifdef YYMAXDEPTH
#define YYSTACKSIZE YYMAXDEPTH
#else
#define YYSTACKSIZE 10000
#define YYMAXDEPTH 10000
#endif
#endif
#define YYINITSTACKSIZE 200
int yydebug;
int yynerrs;
int yyerrflag;
int yychar;
short *yyssp;
YYSTYPE *yyvsp;
YYSTYPE yyval;
YYSTYPE yylval;
short *yyss;
short *yysslim;
YYSTYPE *yyvs;
int yystacksize;
#line 138 "conf_parse.y"

void
conf_parse_file(const char *file) {
	FILE *f = fopen(file, "r");
	if (f == NULL) {
		fprintf(stderr, "Error opening file %s.\n", file);
		return;
	}
	variable_list_init(&variable_list);
	conf_restart(f);
	conf_parse();
	variable_list_destroy(&variable_list);
	fclose(f);
}

void
conf_parse_str(const char *c) {
	int fd[2];
	FILE *f;
	int len;
	if (pipe(fd)) {
		fprintf(stderr, "Error opening pipe.\n");
		return;
	}
	len = strlen(c);
	if (write(fd[1], c, len) != len) {
		fprintf(stderr, "Error writing string to pipe.\n");
		return;
	}
	close(fd[1]);
	f = fdopen(fd[0], "r");
	variable_list_init(&variable_list);
	conf_restart(f);
	conf_parse();
	variable_list_destroy(&variable_list);
	fclose(f);
}

void
conf_error(const char *str) {
	fprintf(stderr,"error: %s on line: %d\n",str, conf_lineno);
}
#line 311 "yacc.conf_parse.tab.c"
/* allocate initial stack or double stack size, up to YYMAXDEPTH */
int yyparse __P((void));
static int yygrowstack __P((void));
static int yygrowstack()
{
    int newsize, i;
    short *newss;
    YYSTYPE *newvs;

    if ((newsize = yystacksize) == 0)
        newsize = YYINITSTACKSIZE;
    else if (newsize >= YYMAXDEPTH)
        return -1;
    else if ((newsize *= 2) > YYMAXDEPTH)
        newsize = YYMAXDEPTH;
    i = yyssp - yyss;
    if ((newss = (short *)realloc(yyss, newsize * sizeof *newss)) == NULL)
        return -1;
    yyss = newss;
    yyssp = newss + i;
    if ((newvs = (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs)) == NULL)
        return -1;
    yyvs = newvs;
    yyvsp = newvs + i;
    yystacksize = newsize;
    yysslim = yyss + newsize - 1;
    return 0;
}

#define YYABORT goto yyabort
#define YYREJECT goto yyabort
#define YYACCEPT goto yyaccept
#define YYERROR goto yyerrlab
int
yyparse()
{
    int yym, yyn, yystate;
#if YYDEBUG
    char *yys;

    if ((yys = getenv("YYDEBUG")) != NULL)
    {
        yyn = *yys;
        if (yyn >= '0' && yyn <= '9')
            yydebug = yyn - '0';
    }
#endif

    yynerrs = 0;
    yyerrflag = 0;
    yychar = (-1);

    if (yyss == NULL && yygrowstack()) goto yyoverflow;
    yyssp = yyss;
    yyvsp = yyvs;
    *yyssp = yystate = 0;

yyloop:
    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
    if (yychar < 0)
    {
        if ((yychar = yylex()) < 0) yychar = 0;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, reading %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
    }
    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: state %d, shifting to state %d\n",
                    YYPREFIX, yystate, yytable[yyn]);
#endif
        if (yyssp >= yysslim && yygrowstack())
        {
            goto yyoverflow;
        }
        *++yyssp = yystate = yytable[yyn];
        *++yyvsp = yylval;
        yychar = (-1);
        if (yyerrflag > 0)  --yyerrflag;
        goto yyloop;
    }
    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
        yyn = yytable[yyn];
        goto yyreduce;
    }
    if (yyerrflag) goto yyinrecovery;
    goto yynewerror;
yynewerror:
    yyerror("syntax error");
    goto yyerrlab;
yyerrlab:
    ++yynerrs;
yyinrecovery:
    if (yyerrflag < 3)
    {
        yyerrflag = 3;
        for (;;)
        {
            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&
                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: state %d, error recovery shifting\
 to state %d\n", YYPREFIX, *yyssp, yytable[yyn]);
#endif
                if (yyssp >= yysslim && yygrowstack())
                {
                    goto yyoverflow;
                }
                *++yyssp = yystate = yytable[yyn];
                *++yyvsp = yylval;
                goto yyloop;
            }
            else
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: error recovery discarding state %d\n",
                            YYPREFIX, *yyssp);
#endif
                if (yyssp <= yyss) goto yyabort;
                --yyssp;
                --yyvsp;
            }
        }
    }
    else
    {
        if (yychar == 0) goto yyabort;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
        yychar = (-1);
        goto yyloop;
    }
yyreduce:
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: state %d, reducing by rule %d (%s)\n",
                YYPREFIX, yystate, yyn, yyrule[yyn]);
#endif
    yym = yylen[yyn];
    yyval = yyvsp[1-yym];
    switch (yyn)
    {
case 5:
#line 70 "conf_parse.y"
{ variable_assign(&variable_list, yyvsp[-2].string, yyvsp[0].v); yyval.v = yyvsp[0].v; }
break;
case 6:
#line 74 "conf_parse.y"
{ yyval.v = variable_get(&variable_list, yyvsp[0].string); }
break;
case 15:
#line 89 "conf_parse.y"
{ yyval.v = variable_create(); variable_insert_cidr(yyval.v, yyvsp[0].string); }
break;
case 16:
#line 90 "conf_parse.y"
{ yyval.v = variable_create(); variable_insert_range(yyval.v, yyvsp[0].string); }
break;
case 17:
#line 91 "conf_parse.y"
{ yyval.v = variable_create(); variable_insert_single(yyval.v, yyvsp[0].string); }
break;
case 18:
#line 92 "conf_parse.y"
{ yyval.v = variable_create(); variable_insert_hostname(yyval.v, yyvsp[0].string); }
break;
case 19:
#line 93 "conf_parse.y"
{ yyval.v = variable_insert_cidr(yyvsp[-2].v, yyvsp[0].string); }
break;
case 20:
#line 94 "conf_parse.y"
{ yyval.v = variable_insert_range(yyvsp[-2].v, yyvsp[0].string); }
break;
case 21:
#line 95 "conf_parse.y"
{ yyval.v = variable_insert_single(yyvsp[-2].v, yyvsp[0].string); }
break;
case 22:
#line 96 "conf_parse.y"
{ yyval.v = variable_insert_hostname(yyvsp[-2].v, yyvsp[0].string); }
break;
case 23:
#line 100 "conf_parse.y"
{ yyval.v = variable_create(); }
break;
case 24:
#line 101 "conf_parse.y"
{ yyval.v = yyvsp[-1].v; }
break;
case 25:
#line 105 "conf_parse.y"
{ yyval.v = variable_load_text(yyvsp[-1].string); }
break;
case 26:
#line 106 "conf_parse.y"
{ yyval.v = variable_load_p2b(yyvsp[-1].string); }
break;
case 27:
#line 107 "conf_parse.y"
{ yyval.v = variable_load_text(NULL); }
break;
case 28:
#line 108 "conf_parse.y"
{ yyval.v = variable_load_p2b(NULL); }
break;
case 29:
#line 112 "conf_parse.y"
{ yyval.v= variable_union(yyvsp[-3].v, yyvsp[-1].v); }
break;
case 30:
#line 116 "conf_parse.y"
{ yyval.v = variable_difference(yyvsp[-3].v, yyvsp[-1].v); }
break;
case 31:
#line 120 "conf_parse.y"
{ yyval.v = variable_intersect(yyvsp[-3].v, yyvsp[-1].v); }
break;
case 32:
#line 124 "conf_parse.y"
{ yyval.v = variable_invert(yyvsp[-1].v); }
break;
case 33:
#line 128 "conf_parse.y"
{ variable_save_cidr(yyvsp[-3].string, yyvsp[-1].v); }
break;
case 34:
#line 129 "conf_parse.y"
{ variable_save_range(yyvsp[-3].string, yyvsp[-1].v); }
break;
case 35:
#line 130 "conf_parse.y"
{ variable_save_single(yyvsp[-3].string, yyvsp[-1].v); }
break;
case 36:
#line 131 "conf_parse.y"
{ variable_save_cidr(NULL, yyvsp[-1].v); }
break;
case 37:
#line 132 "conf_parse.y"
{ variable_save_range(NULL, yyvsp[-1].v); }
break;
case 38:
#line 133 "conf_parse.y"
{ variable_save_single(NULL, yyvsp[-1].v); }
break;
#line 581 "yacc.conf_parse.tab.c"
    }
    yyssp -= yym;
    yystate = *yyssp;
    yyvsp -= yym;
    yym = yylhs[yyn];
    if (yystate == 0 && yym == 0)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: after reduction, shifting from state 0 to\
 state %d\n", YYPREFIX, YYFINAL);
#endif
        yystate = YYFINAL;
        *++yyssp = YYFINAL;
        *++yyvsp = yyval;
        if (yychar < 0)
        {
            if ((yychar = yylex()) < 0) yychar = 0;
#if YYDEBUG
            if (yydebug)
            {
                yys = 0;
                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
                if (!yys) yys = "illegal-symbol";
                printf("%sdebug: state %d, reading %d (%s)\n",
                        YYPREFIX, YYFINAL, yychar, yys);
            }
#endif
        }
        if (yychar == 0) goto yyaccept;
        goto yyloop;
    }
    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
        yystate = yytable[yyn];
    else
        yystate = yydgoto[yym];
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: after reduction, shifting from state %d \
to state %d\n", YYPREFIX, *yyssp, yystate);
#endif
    if (yyssp >= yysslim && yygrowstack())
    {
        goto yyoverflow;
    }
    *++yyssp = yystate;
    *++yyvsp = yyval;
    goto yyloop;
yyoverflow:
    yyerror("yacc stack overflow");
yyabort:
    return (1);
yyaccept:
    return (0);
}


syntax highlighted by Code2HTML, v. 0.9.1