comparison extensions/dbg_loglevel/dbg_loglevel_conf.y @ 1345:51a0521cd065

dbg_loglevel: new extension that allows changing the log level at runtime
author Thomas Klausner <tk@giga.or.at>
date Sun, 12 May 2019 10:35:46 +0200
parents
children
comparison
equal deleted inserted replaced
1344:a5c072798f1a 1345:51a0521cd065
1 /*********************************************************************************************************
2 * Software License Agreement (BSD License) *
3 * Author: Thomas Klausner <tk@giga.or.at> *
4 * *
5 * Copyright (c) 2019, Thomas Klausner *
6 * All rights reserved. *
7 * *
8 * Written under contract by Effortel Technologies SA, http://effortel.com/ *
9 * *
10 * Redistribution and use of this software in source and binary forms, with or without modification, are *
11 * permitted provided that the following conditions are met: *
12 * *
13 * * Redistributions of source code must retain the above *
14 * copyright notice, this list of conditions and the *
15 * following disclaimer. *
16 * *
17 * * Redistributions in binary form must reproduce the above *
18 * copyright notice, this list of conditions and the *
19 * following disclaimer in the documentation and/or other *
20 * materials provided with the distribution. *
21 * *
22 * * Neither the name of the WIDE Project or NICT nor the *
23 * names of its contributors may be used to endorse or *
24 * promote products derived from this software without *
25 * specific prior written permission of WIDE Project and *
26 * NICT. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
30 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
31 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
36 *********************************************************************************************************/
37
38 /* Yacc extension's configuration parser.
39 */
40
41 /* For development only : */
42 %debug
43 %error-verbose
44
45 /* The parser receives the configuration file filename as parameter */
46 %parse-param {char * conffile}
47
48 /* Keep track of location */
49 %locations
50 %pure-parser
51
52 %{
53 #include "dbg_loglevel.h"
54 #include "dbg_loglevel_conf.tab.h"
55
56 /* Forward declaration */
57 int dbg_loglevel_confparse(char *conffile);
58 void dbg_loglevel_confrestart(FILE *input_file);
59
60 /* Parse the configuration file */
61 int dbg_loglevel_conf_handle(char * conffile)
62 {
63 extern FILE *dbg_loglevel_confin;
64 int ret;
65
66 TRACE_ENTRY("%p", conffile);
67
68 TRACE_DEBUG(FULL, "Parsing configuration file: %s...", conffile);
69
70 dbg_loglevel_confin = fopen(conffile, "r");
71 if (dbg_loglevel_confin == NULL) {
72 ret = errno;
73 TRACE_ERROR("Unable to open extension configuration file %s for reading: %s", conffile, strerror(ret));
74 return ret;
75 }
76
77 dbg_loglevel_confrestart(dbg_loglevel_confin);
78 ret = dbg_loglevel_confparse(conffile);
79
80 fclose(dbg_loglevel_confin);
81
82 if (ret != 0) {
83 TRACE_ERROR( "Unable to parse the configuration file %s.", conffile);
84 return EINVAL;
85 } else {
86 TRACE_DEBUG(FULL, "[dbg_loglevel] LogLevel: %d.", fd_g_debug_lvl);
87 }
88
89 return 0;
90 }
91
92 /* The Lex parser prototype */
93 int dbg_loglevel_conflex(YYSTYPE *lvalp, YYLTYPE *llocp);
94
95 /* Function to report the errors */
96 void 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_error("%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_error("%s:%d.%d-%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_column, s);
104 else
105 fd_log_error("%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;
113 }
114
115 /* In case of error in the lexical analysis */
116 %token LEX_ERROR
117
118 /* A (de)quoted string (malloc'd in lex parser; it must be freed after use) */
119 %token <integer> INTEGER
120
121 /* Tokens */
122 %token LOGLEVEL
123
124
125 /* -------------------------------------- */
126 %%
127
128 /* The grammar definition */
129 conffile: /* empty is OK */
130 | conffile size
131 | conffile errors
132 {
133 yyerror(&yylloc, conffile, "An error occurred while parsing the configuration file");
134 YYABORT;
135 }
136 ;
137
138 /* Lexical or syntax error */
139 errors: LEX_ERROR
140 | error
141 ;
142
143 size: LOGLEVEL '=' INTEGER ';'
144 {
145 fd_g_debug_lvl=$3;
146 }
147 ;
"Welcome to our mercurial repository"