blob: d6ed3298e6885b18b7ed8cc46142e4d52ca7633f [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2013, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36/* Yacc extension's configuration parser.
37 * See doc/rt_default.conf.sample for configuration file format
38 */
39
40/* For development only : */
41%debug
42%error-verbose
43
44/* The parser receives the configuration file filename as parameter */
45%parse-param {char * conffile}
46
47/* Keep track of location */
48%locations
49%pure-parser
50
51%{
52#include "rt_default.h"
53#include "rtd_conf.tab.h" /* bison is not smart enough to define the YYLTYPE before including this code, so... */
54
55/* Forward declaration */
56int yyparse(char * conffile);
57
58static int rules_added = 0;
59
60/* Parse the configuration file */
61int rtd_conf_handle(char * conffile)
62{
63 extern FILE * rtd_confin;
64 int ret;
65
66 TRACE_ENTRY("%p", conffile);
67
68 TRACE_DEBUG (FULL, "Parsing configuration file: %s...", conffile);
69
70 rtd_confin = fopen(conffile, "r");
71 if (rtd_confin == NULL) {
72 ret = errno;
73 fd_log_debug("Unable to open extension configuration file %s for reading: %s", conffile, strerror(ret));
74 TRACE_DEBUG (INFO, "Error occurred, message logged -- configuration file.");
75 return ret;
76 }
77
78 ret = yyparse(conffile);
79
80 fclose(rtd_confin);
81
82 if (ret != 0) {
83 TRACE_DEBUG (INFO, "Unable to parse the configuration file.");
84 return EINVAL;
85 } else {
86 TRACE_DEBUG(FULL, "Added %d RULES routing entries successfully.", rules_added);
87 }
88
89 return 0;
90}
91
92/* The Lex parser prototype */
93int rtd_conflex(YYSTYPE *lvalp, YYLTYPE *llocp);
94
95/* Function to report the errors */
96void yyerror (YYLTYPE *ploc, char * conffile, char const *s)
97{
98 TRACE_DEBUG(INFO, "Error in configuration parsing");
99
100 if (ploc->first_line != ploc->last_line)
101 fd_log_debug("%s:%d.%d-%d.%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_line, ploc->last_column, s);
102 else if (ploc->first_column != ploc->last_column)
103 fd_log_debug("%s:%d.%d-%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_column, s);
104 else
105 fd_log_debug("%s:%d.%d : %s", conffile, ploc->first_line, ploc->first_column, s);
106}
107
108%}
109
110/* Values returned by lex for token */
111%union {
112 int integer; /* Store integer values */
113 char *string; /* The string is allocated by strdup in lex.*/
114 struct {
115 char * str;
116 int regex;
117 } tstring; /* typed string */
118 struct {
119 char * str;
120 int regex;
121 enum rtd_crit_type type;
122 } criteria;
123 struct {
124 char * str;
125 int regex;
126 enum rtd_targ_type type;
127 } target;
128}
129
130/* In case of error in the lexical analysis */
131%token LEX_ERROR
132
133/* A (de)quoted string (malloc'd in lex parser; it must be freed after use) */
134%token <string> QSTRING
135/* A (de)bracket-quoted string (malloc'd in lex parser; it must be freed after use): ["blahblah"] */
136%token <string> BQSTRING
137
138/* An integer value */
139%token <integer> INTEGER
140
141/* The types for this gramar */
142%type <tstring> TSTRING
143%type <criteria> CRITERIA
144%type <target> TARGET
145%type <integer> EXPR_INT
146
147/* Tokens */
148%token OH
149%token OR
150%token DH
151%token DR
152%token UN
153%token SI
154
155%token REALM
156
157
158
159/* -------------------------------------- */
160%%
161
162 /* The grammar definition */
163conffile: /* empty grammar is OK */
164 | conffile rule
165 ;
166
167 /* a RULE entry */
168rule: CRITERIA ':' TARGET '+' '=' EXPR_INT ';'
169 {
170 int flag = 0;
171 if ($1.regex)
172 flag |= RTD_CRIT_REG;
173 if ($3.regex)
174 flag |= RTD_TARG_REG;
175
176 /* Add this rule to the repository */
177 CHECK_FCT_DO( rtd_add($1.type, $1.str, $3.type, $3.str, $6, flag),
178 {
179 yyerror (&yylloc, conffile, "An error occurred while adding a rule, aborting...");
180 YYERROR;
181 } );
182
183 rules_added++;
184 }
185 ;
186
187 /* QSTRING and BQSTRING are equivalent in the grammar */
188TSTRING: QSTRING
189 {
190 $$.str = $1;
191 $$.regex = 0;
192 }
193 | BQSTRING
194 {
195 $$.str = $1;
196 $$.regex = 1;
197 }
198 ;
199
200 /* Details of the CRITERIA type */
201CRITERIA: '*'
202 {
203 $$.str = NULL;
204 $$.regex = 0;
205 $$.type = RTD_CRI_ALL;
206 }
207 | OH '=' TSTRING
208 {
209 $$.str = $3.str;
210 $$.regex =$3.regex;
211 $$.type = RTD_CRI_OH;
212 }
213 | OR '=' TSTRING
214 {
215 $$.str = $3.str;
216 $$.regex =$3.regex;
217 $$.type = RTD_CRI_OR;
218 }
219 | DH '=' TSTRING
220 {
221 $$.str = $3.str;
222 $$.regex =$3.regex;
223 $$.type = RTD_CRI_DH;
224 }
225 | DR '=' TSTRING
226 {
227 $$.str = $3.str;
228 $$.regex =$3.regex;
229 $$.type = RTD_CRI_DR;
230 }
231 | UN '=' TSTRING
232 {
233 $$.str = $3.str;
234 $$.regex =$3.regex;
235 $$.type = RTD_CRI_UN;
236 }
237 | SI '=' TSTRING
238 {
239 $$.str = $3.str;
240 $$.regex =$3.regex;
241 $$.type = RTD_CRI_SI;
242 }
243 ;
244
245 /* Details of the TARGET type */
246TARGET: TSTRING
247 {
248 $$.str = $1.str;
249 $$.regex =$1.regex;
250 $$.type = RTD_TAR_ID;
251 }
252 | REALM '=' TSTRING
253 {
254 $$.str = $3.str;
255 $$.regex =$3.regex;
256 $$.type = RTD_TAR_REALM;
257 }
258 ;
259
260 /* An expression that has an integer value; we allow + and - operators cause it is convenient */
261EXPR_INT: INTEGER
262 {
263 $$ = $1;
264 }
265 | EXPR_INT '+' INTEGER
266 {
267 $$ = $1 + $3;
268 }
269 | EXPR_INT '-' INTEGER
270 {
271 $$ = $1 - $3;
272 }
273 ;