1 /* $NetBSD: conf_parse.y,v 1.1.1.3 2015/01/17 16:34:15 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1997-2014 Erez Zadok 5 * Copyright (c) 1989 Jan-Simon Pendry 6 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine 7 * Copyright (c) 1989 The Regents of the University of California. 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * Jan-Simon Pendry at Imperial College, London. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * 38 * File: am-utils/amd/conf_parse.y 39 * 40 */ 41 42 %{ 43 #ifdef HAVE_CONFIG_H 44 # include <config.h> 45 #endif /* HAVE_CONFIG_H */ 46 #include <am_defs.h> 47 #include <amd.h> 48 49 extern char *yytext; 50 extern int ayylineno; 51 extern int yylex(void); 52 53 static int yyerror(const char *s); 54 static int retval; 55 static char *header_section = NULL; /* start with no header section */ 56 57 #define YYDEBUG 1 58 59 #define PARSE_DEBUG 0 60 61 #if PARSE_DEBUG 62 # define dprintf(f,s) fprintf(stderr, (f), ayylineno, (s)) 63 # define amu_return(v) 64 #else /* not PARSE_DEBUG */ 65 # define dprintf(f,s) 66 # define amu_return(v) return((v)) 67 #endif /* not PARSE_DEBUG */ 68 69 %} 70 71 %union { 72 char *strtype; 73 } 74 75 %token LEFT_BRACKET RIGHT_BRACKET EQUAL 76 %token NEWLINE 77 %token <strtype> NONWS_STRING 78 %token <strtype> NONWSEQ_STRING 79 %token <strtype> QUOTED_NONWSEQ_STRING 80 81 %start file 82 %% 83 84 /****************************************************************************/ 85 file : { yydebug = PARSE_DEBUG; } newlines map_sections 86 | { yydebug = PARSE_DEBUG; } map_sections 87 ; 88 89 newlines : NEWLINE 90 | NEWLINE newlines 91 ; 92 93 map_sections : map_section 94 | map_section map_sections 95 ; 96 97 map_section : sec_header kv_pairs 98 ; 99 100 sec_header : LEFT_BRACKET NONWS_STRING RIGHT_BRACKET NEWLINE 101 { 102 if (yydebug) 103 fprintf(stderr, "sec_header1 = \"%s\"\n", $2); 104 header_section = $2; 105 } 106 ; 107 108 kv_pairs : kv_pair 109 | kv_pair kv_pairs 110 ; 111 112 kv_pair : NONWS_STRING EQUAL NONWS_STRING NEWLINE 113 { 114 if (yydebug) 115 fprintf(stderr,"parse1: key=\"%s\", val=\"%s\"\n", $1, $3); 116 retval = set_conf_kv(header_section, $1, $3); 117 if (retval != 0) { 118 yyerror("syntax error"); 119 YYABORT; 120 } 121 } 122 | NONWS_STRING EQUAL NONWSEQ_STRING NEWLINE 123 { 124 if (yydebug) 125 fprintf(stderr,"parse2: key=\"%s\", val=\"%s\"\n", $1, $3); 126 retval = set_conf_kv(header_section, $1, $3); 127 if (retval != 0) { 128 yyerror("syntax error"); 129 YYABORT; 130 } 131 } 132 | NONWS_STRING EQUAL QUOTED_NONWSEQ_STRING NEWLINE 133 { 134 if (yydebug) 135 fprintf(stderr,"parse3: key=\"%s\", val=\"%s\"\n", $1, $3); 136 retval = set_conf_kv(header_section, $1, $3); 137 if (retval != 0) { 138 yyerror("syntax error"); 139 YYABORT; 140 } 141 } 142 | NEWLINE 143 ; 144 145 /****************************************************************************/ 146 %% 147 148 static int 149 yyerror(const char *s) 150 { 151 fprintf(stderr, "AMDCONF: %s on line %d (section %s)\n", 152 s, ayylineno, 153 (header_section ? header_section : "null")); 154 exit(1); 155 return 1; /* to full compilers that insist on a return statement */ 156 } 157