138494Sobrien/*
2310490Scy * Copyright (c) 1997-2014 Erez Zadok
338494Sobrien * Copyright (c) 1989 Jan-Simon Pendry
438494Sobrien * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
538494Sobrien * Copyright (c) 1989 The Regents of the University of California.
638494Sobrien * All rights reserved.
738494Sobrien *
838494Sobrien * This code is derived from software contributed to Berkeley by
938494Sobrien * Jan-Simon Pendry at Imperial College, London.
1038494Sobrien *
1138494Sobrien * Redistribution and use in source and binary forms, with or without
1238494Sobrien * modification, are permitted provided that the following conditions
1338494Sobrien * are met:
1438494Sobrien * 1. Redistributions of source code must retain the above copyright
1538494Sobrien *    notice, this list of conditions and the following disclaimer.
1638494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1738494Sobrien *    notice, this list of conditions and the following disclaimer in the
1838494Sobrien *    documentation and/or other materials provided with the distribution.
19310490Scy * 3. Neither the name of the University nor the names of its contributors
2038494Sobrien *    may be used to endorse or promote products derived from this software
2138494Sobrien *    without specific prior written permission.
2238494Sobrien *
2338494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2438494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2538494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2638494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2738494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2838494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2938494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3038494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3138494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3238494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3338494Sobrien * SUCH DAMAGE.
3438494Sobrien *
3538494Sobrien *
36174294Sobrien * File: am-utils/amd/conf_parse.y
3738494Sobrien *
3838494Sobrien */
3938494Sobrien
4038494Sobrien%{
4138494Sobrien#ifdef HAVE_CONFIG_H
4238494Sobrien# include <config.h>
4338494Sobrien#endif /* HAVE_CONFIG_H */
4438494Sobrien#include <am_defs.h>
4538494Sobrien#include <amd.h>
4638494Sobrien
4738494Sobrienextern char *yytext;
48131702Smbrextern int ayylineno;
4938494Sobrienextern int yylex(void);
5038494Sobrien
5138494Sobrienstatic int yyerror(const char *s);
5238494Sobrienstatic int retval;
5338494Sobrienstatic char *header_section = NULL; /* start with no header section */
5438494Sobrien
5538494Sobrien#define YYDEBUG 1
5638494Sobrien
5738494Sobrien#define PARSE_DEBUG 0
5838494Sobrien
5938494Sobrien#if PARSE_DEBUG
60131702Smbr# define dprintf(f,s) fprintf(stderr, (f), ayylineno, (s))
6138494Sobrien# define amu_return(v)
62131702Smbr#else /* not PARSE_DEBUG */
6338494Sobrien# define dprintf(f,s)
6438494Sobrien# define amu_return(v) return((v))
65131702Smbr#endif /* not PARSE_DEBUG */
6638494Sobrien
6738494Sobrien%}
6838494Sobrien
6938494Sobrien%union {
7038494Sobrienchar *strtype;
7138494Sobrien}
7238494Sobrien
7338494Sobrien%token LEFT_BRACKET RIGHT_BRACKET EQUAL
7438494Sobrien%token NEWLINE
7538494Sobrien%token <strtype> NONWS_STRING
7638494Sobrien%token <strtype> NONWSEQ_STRING
7738494Sobrien%token <strtype> QUOTED_NONWSEQ_STRING
7838494Sobrien
7938494Sobrien%start file
8038494Sobrien%%
8138494Sobrien
8238494Sobrien/****************************************************************************/
8338494Sobrienfile		: { yydebug = PARSE_DEBUG; } newlines map_sections
8438494Sobrien		| { yydebug = PARSE_DEBUG; } map_sections
8538494Sobrien		;
8638494Sobrien
8738494Sobriennewlines	: NEWLINE
8838494Sobrien		| NEWLINE newlines
8938494Sobrien		;
9038494Sobrien
9138494Sobrienmap_sections	: map_section
9238494Sobrien		| map_section map_sections
9338494Sobrien		;
9438494Sobrien
9538494Sobrienmap_section	: sec_header kv_pairs
9638494Sobrien		;
9738494Sobrien
9838494Sobriensec_header	: LEFT_BRACKET NONWS_STRING RIGHT_BRACKET NEWLINE
9938494Sobrien		{
10038494Sobrien		  if (yydebug)
10138494Sobrien		    fprintf(stderr, "sec_header1 = \"%s\"\n", $2);
10238494Sobrien		  header_section = $2;
10338494Sobrien		}
10438494Sobrien		;
10538494Sobrien
10638494Sobrienkv_pairs	: kv_pair
10738494Sobrien		| kv_pair kv_pairs
10838494Sobrien		;
10938494Sobrien
11038494Sobrienkv_pair		: NONWS_STRING EQUAL NONWS_STRING NEWLINE
11138494Sobrien		{
11238494Sobrien		  if (yydebug)
11338494Sobrien		    fprintf(stderr,"parse1: key=\"%s\", val=\"%s\"\n", $1, $3);
11438494Sobrien		  retval = set_conf_kv(header_section, $1, $3);
11538494Sobrien		  if (retval != 0) {
11638494Sobrien		    yyerror("syntax error");
11738494Sobrien		    YYABORT;
11838494Sobrien		  }
11938494Sobrien		}
12038494Sobrien		| NONWS_STRING EQUAL NONWSEQ_STRING NEWLINE
12138494Sobrien		{
12238494Sobrien		  if (yydebug)
12338494Sobrien		    fprintf(stderr,"parse2: key=\"%s\", val=\"%s\"\n", $1, $3);
12438494Sobrien		  retval = set_conf_kv(header_section, $1, $3);
12538494Sobrien		  if (retval != 0) {
12638494Sobrien		    yyerror("syntax error");
12738494Sobrien		    YYABORT;
12838494Sobrien		  }
12938494Sobrien		}
13038494Sobrien		| NONWS_STRING EQUAL QUOTED_NONWSEQ_STRING NEWLINE
13138494Sobrien		{
13238494Sobrien		  if (yydebug)
13338494Sobrien		    fprintf(stderr,"parse3: key=\"%s\", val=\"%s\"\n", $1, $3);
13438494Sobrien		  retval = set_conf_kv(header_section, $1, $3);
13538494Sobrien		  if (retval != 0) {
13638494Sobrien		    yyerror("syntax error");
13738494Sobrien		    YYABORT;
13838494Sobrien		  }
13938494Sobrien		}
14038494Sobrien		| NEWLINE
14138494Sobrien		;
14238494Sobrien
14338494Sobrien/****************************************************************************/
14438494Sobrien%%
14538494Sobrien
14638494Sobrienstatic int
14738494Sobrienyyerror(const char *s)
14838494Sobrien{
14938494Sobrien  fprintf(stderr, "AMDCONF: %s on line %d (section %s)\n",
150131702Smbr	  s, ayylineno,
15138494Sobrien	  (header_section ? header_section : "null"));
15238494Sobrien  exit(1);
15338494Sobrien  return 1;	/* to full compilers that insist on a return statement */
15438494Sobrien}
155