blob: 96a530366875967efd750bcbb7fb83ec0c375df2 [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 */
38
39/* For development only : */
40%debug
41%error-verbose
42
43/* The parser receives the configuration file filename as parameter */
44%parse-param {char * conffile}
45
46/* Keep track of location */
47%locations
48%pure-parser
49
50%{
51#include "rtbusy.h"
52#include "rtbusy_conf.tab.h"
53
54/* Forward declaration */
55int yyparse(char * conffile);
56
57/* Parse the configuration file */
58int rtbusy_conf_handle(char * conffile)
59{
60 extern FILE * rtbusy_confin;
61 int ret;
62
63 TRACE_ENTRY("%p", conffile);
64
65 TRACE_DEBUG (FULL, "Parsing configuration file: %s...", conffile);
66
67 rtbusy_confin = fopen(conffile, "r");
68 if (rtbusy_confin == NULL) {
69 ret = errno;
70 TRACE_ERROR("Unable to open extension configuration file %s for reading: %s", conffile, strerror(ret));
71 return ret;
72 }
73
74 ret = yyparse(conffile);
75
76 fclose(rtbusy_confin);
77
78 if (ret != 0) {
79 TRACE_ERROR( "Unable to parse the configuration file.");
80 return EINVAL;
81 } else {
82 TRACE_DEBUG(FULL, "[rt_busypeers] Configuration: %d-%d-%d-%d.", rtbusy_conf.SkipTooBusyErrors, rtbusy_conf.RetryDistantPeers, rtbusy_conf.RetryMaxPeers, rtbusy_conf.RelayTimeout);
83 }
84
85 return 0;
86}
87
88/* The Lex parser prototype */
89int rtbusy_conflex(YYSTYPE *lvalp, YYLTYPE *llocp);
90
91/* Function to report the errors */
92void yyerror (YYLTYPE *ploc, char * conffile, char const *s)
93{
94 TRACE_DEBUG(INFO, "Error in configuration parsing");
95
96 if (ploc->first_line != ploc->last_line)
97 fd_log_error("%s:%d.%d-%d.%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_line, ploc->last_column, s);
98 else if (ploc->first_column != ploc->last_column)
99 fd_log_error("%s:%d.%d-%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_column, s);
100 else
101 fd_log_error("%s:%d.%d : %s", conffile, ploc->first_line, ploc->first_column, s);
102}
103
104%}
105
106/* Values returned by lex for token */
107%union {
108 int integer;
109}
110
111/* In case of error in the lexical analysis */
112%token LEX_ERROR
113
114/* A (de)quoted string (malloc'd in lex parser; it must be freed after use) */
115%token <integer> INTEGER
116
117/* Tokens */
118%token SKIPTOOBUSYERRORS
119%token RETRYDISTANTPEERS
120%token RETRYMAXPEERS
121%token RELAYTIMEOUT
122
123
124/* -------------------------------------- */
125%%
126
127 /* The grammar definition */
128conffile: /* empty is OK */
129 | conffile toobusy
130 | conffile distant
131 | conffile maxretry
132 | conffile timeout
133 | conffile errors
134 {
135 yyerror(&yylloc, conffile, "An error occurred while parsing the configuration file");
136 return EINVAL;
137 }
138 ;
139
140 /* Lexical or syntax error */
141errors: LEX_ERROR
142 | error
143 ;
144
145toobusy: SKIPTOOBUSYERRORS ';'
146 {
147 rtbusy_conf.SkipTooBusyErrors=1;
148 }
149 ;
150
151distant: RETRYDISTANTPEERS ';'
152 {
153 rtbusy_conf.RetryDistantPeers=1;
154 }
155 ;
156
157maxretry: RETRYMAXPEERS '=' INTEGER ';'
158 {
159 rtbusy_conf.RetryMaxPeers=$3;
160 }
161 ;
162
163timeout: RELAYTIMEOUT '=' INTEGER ';'
164 {
165 rtbusy_conf.RelayTimeout=$3;
166 }
167 ;
168