1 /*********************************************************************************************************
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/dict_legacy_xml.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 "dict_lxml.h"
53 #include "dict_lxml.tab.h"	/* bison is not smart enough to define the YYLTYPE before including this code, so... */
54 
55 /* Forward declaration */
56 int yyparse(char * conffile);
57 
58 static int nb_files = 0;
59 static int nb_dict = 0;
60 
61 /* Parse the configuration file */
dict_lxml_handle(char * conffile)62 int dict_lxml_handle(char * conffile)
63 {
64 	extern FILE * dict_lxmlin;
65 	int ret;
66 
67 	TRACE_ENTRY("%p", conffile);
68 
69 	TRACE_DEBUG (FULL, "Parsing configuration file: %s...", conffile);
70 
71 	dict_lxmlin = fopen(conffile, "r");
72 	if (dict_lxmlin == NULL) {
73 		ret = errno;
74 		fd_log_debug("Unable to open extension configuration file %s for reading: %s", conffile, strerror(ret));
75 		TRACE_DEBUG (INFO, "Error occurred, message logged -- configuration file.");
76 		return ret;
77 	}
78 
79 	ret = yyparse(conffile);
80 
81 	fclose(dict_lxmlin);
82 
83 	if (ret != 0) {
84 		TRACE_DEBUG (INFO, "Unable to parse the configuration file.");
85 		return EINVAL;
86 	} else {
87 		TRACE_DEBUG(FULL, "%d XML dictionary files parsed successfully, %d dictionary objects added.", nb_files, nb_dict);
88 	}
89 
90 	return 0;
91 }
92 
93 /* The Lex parser prototype */
94 int dict_lxmllex(YYSTYPE *lvalp, YYLTYPE *llocp);
95 
96 /* Function to report the errors */
yyerror(YYLTYPE * ploc,char * conffile,char const * s)97 void yyerror (YYLTYPE *ploc, char * conffile, char const *s)
98 {
99 	TRACE_DEBUG(INFO, "Error in configuration parsing");
100 
101 	if (ploc->first_line != ploc->last_line)
102 		fd_log_debug("%s:%d.%d-%d.%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_line, ploc->last_column, s);
103 	else if (ploc->first_column != ploc->last_column)
104 		fd_log_debug("%s:%d.%d-%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_column, s);
105 	else
106 		fd_log_debug("%s:%d.%d : %s", conffile, ploc->first_line, ploc->first_column, s);
107 }
108 
109 %}
110 
111 /* Values returned by lex for token */
112 %union {
113 	char 		*string;	/* The string is allocated by strdup in lex.*/
114 }
115 
116 /* In case of error in the lexical analysis */
117 %token 		LEX_ERROR
118 
119 /* A (de)quoted string (malloc'd in lex parser; it must be freed after use) */
120 %token <string>	QSTRING
121 
122 /* -------------------------------------- */
123 %%
124 
125 	/* The grammar definition */
126 conffile:		/* empty grammar is OK */
127 			| conffile xmlfile
128 			;
129 
130 	/* a RULE entry */
131 xmlfile:		QSTRING ';'
132 			{
133 				int ret = dict_lxml_parse($1);
134 				if (ret < 0) {
135 					yyerror (&yylloc, conffile, "An error occurred while parsing a file, aborting...");
136 					YYERROR;
137 				}
138 				nb_files++;
139 				nb_dict += ret;
140 			}
141 			;
142