Debian Codegraph @

src/config_file_yacc.y

raw
1 %{
2 /**
3 * This file is part of the Detox package.
4 *
5 * Copyright (c) Doug Harple <[email protected]>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "detox_struct.h"
16
17 #include "config_file.h"
18 #include "filelist.h"
19 #include "filter.h"
20 #include "sequence.h"
21 #include "wrapped.h"
22
23 /*
24 * I must apologize in advance for the cryptic, global variable names.
25 */
26
27 static sequence_t *cf_sequence_ret, *cf_sequence_current;
28 static filter_t *cf_filter_ret, *cf_filter_current;
29 static char *current_name = NULL;
30 static char *current_filename = NULL;
31 static filelist_t *files_to_ignore;
32
33 void cf_append_sequence_list(void);
34 void cf_append_filter(int cleaner, char *builtin, char *filename, int max_length, int remove_trailing);
35 void cf_append_ignore_entry(void *str);
36
37 void yyerror (char *s);
38
39 int yylex (void);
40
41 %}
42
43 %union {
44 char *string; /* string buffer */
45 int cmd; /* command value */
46 int nvalue; /* nvalue */
47 }
48
49 %token <cmd> BUILTIN
50 %token <cmd> CLOSE
51 %token <cmd> EOL
52 %token <cmd> FILENAME
53 %token <cmd> IGNORE
54 %token <cmd> ISO8859_1
55 %token <cmd> LENGTH
56 %token <cmd> LOWER
57 %token <cmd> MAX_LENGTH
58 %token <cmd> OPEN
59 %token <cmd> REMOVE_TRAILING
60 %token <cmd> SAFE
61 %token <cmd> SEQUENCE
62 %token <cmd> UNCGI
63 %token <cmd> UTF_8
64 %token <cmd> WIPEUP
65 %token <nvalue> NVALUE
66 %token <string> ID
67 %token <string> QSTRING
68
69 %type <string> string
70
71 %%
72
73 configfile:
74 |
75 configfile rule
76 ;
77
78 rule: sequence
79 |
80 ignore
81 ;
82
83 sequence: sequence_open method_list sequence_close
84 ;
85
86 sequence_open: SEQUENCE string OPEN { current_name = $2; }
87 ;
88
89 sequence_close: CLOSE EOL { cf_append_sequence_list(); }
90 ;
91
92 method_list: method |
93 method_list method
94 ;
95
96 method: UNCGI EOL { cf_append_filter(FILTER_UNCGI, NULL, NULL, 0, 0); }
97 |
98 LOWER EOL { cf_append_filter(FILTER_LOWER, NULL, NULL, 0, 0); }
99 |
100 wipeup EOL
101 |
102 iso8859_1 EOL
103 |
104 utf_8 EOL
105 |
106 safe EOL
107 |
108 max_length EOL
109 ;
110
111 iso8859_1: ISO8859_1 { cf_append_filter(FILTER_ISO8859_1, NULL, NULL, 0, 0); }
112 |
113 ISO8859_1 OPEN CLOSE { cf_append_filter(FILTER_ISO8859_1, NULL, NULL, 0, 0); }
114 |
115 ISO8859_1 OPEN FILENAME string EOL CLOSE { cf_append_filter(FILTER_ISO8859_1, NULL, $4, 0, 0); }
116 |
117 ISO8859_1 OPEN BUILTIN string EOL CLOSE { cf_append_filter(FILTER_ISO8859_1, $4, NULL, 0, 0); }
118 ;
119
120 utf_8: UTF_8 { cf_append_filter(FILTER_UTF_8, NULL, NULL, 0, 0); }
121 |
122 UTF_8 OPEN CLOSE { cf_append_filter(FILTER_UTF_8, NULL, NULL, 0, 0); }
123 |
124 UTF_8 OPEN FILENAME string EOL CLOSE { cf_append_filter(FILTER_UTF_8, NULL, $4, 0, 0); }
125 |
126 UTF_8 OPEN BUILTIN string EOL CLOSE { cf_append_filter(FILTER_UTF_8, $4, NULL, 0, 0); }
127 ;
128
129 safe: SAFE { cf_append_filter(FILTER_SAFE, NULL, NULL, 0, 0); }
130 |
131 SAFE OPEN CLOSE { cf_append_filter(FILTER_SAFE, NULL, NULL, 0, 0); }
132 |
133 SAFE OPEN FILENAME string EOL CLOSE { cf_append_filter(FILTER_SAFE, NULL, $4, 0, 0); }
134 |
135 SAFE OPEN BUILTIN string EOL CLOSE { cf_append_filter(FILTER_SAFE, $4, NULL, 0, 0); }
136 ;
137
138 wipeup: WIPEUP { cf_append_filter(FILTER_WIPEUP, NULL, NULL, 0, 0); }
139 |
140 WIPEUP OPEN CLOSE { cf_append_filter(FILTER_WIPEUP, NULL, NULL, 0, 0); }
141 |
142 WIPEUP OPEN REMOVE_TRAILING EOL CLOSE { cf_append_filter(FILTER_WIPEUP, NULL, NULL, 0, 1); }
143 ;
144
145 max_length: MAX_LENGTH { cf_append_filter(FILTER_MAX_LENGTH, NULL, NULL, 0, 0); }
146 |
147 MAX_LENGTH OPEN CLOSE { cf_append_filter(FILTER_MAX_LENGTH, NULL, NULL, 0, 0); }
148 |
149 MAX_LENGTH OPEN LENGTH NVALUE EOL CLOSE { cf_append_filter(FILTER_MAX_LENGTH, NULL, NULL, $4, 0); }
150 ;
151
152 ignore: ignore_open ignore_list ignore_close
153 ;
154
155 ignore_open: IGNORE OPEN
156 ;
157
158 ignore_close: CLOSE EOL
159 ;
160
161 ignore_list: ignore_filename |
162 ignore_list ignore_filename
163 ;
164
165 ignore_filename: FILENAME string EOL {
166 cf_append_ignore_entry($2);
167 }
168 ;
169
170 string: QSTRING { $$ = $1; }
171 |
172 ID { $$ = $1; }
173 ;
174
175 %%
176
177 extern FILE *yyin;
178 extern FILE *yyout;
179
180 config_file_t *parse_config_file(char *filename, config_file_t *previous_results, options_t *main_options) {
181 config_file_t *ret = NULL;
182
183 current_filename = filename;
184
185 /*
186 * XXX - Should we be closing the default yyin/yyout? If so, should we
187 * be setting them to NULL at the end of this function?
188 */
189
190 yyin = fopen(filename, "r");
191 if (yyin == NULL) {
192 return previous_results;
193 }
194 yyout = fopen("/dev/null", "w");
195
196 /*
197 * Initialize the return variable
198 */
199
200 if (previous_results) {
201 ret = previous_results;
202 }
203 else {
204 ret = config_file_init();
205 }
206
207 /*
208 * Initialize the sequence list
209 */
210
211 cf_sequence_ret = NULL;
212 cf_sequence_current = NULL;
213
214 if (previous_results && previous_results->sequences) {
215 cf_sequence_ret = previous_results->sequences;
216 cf_sequence_current = cf_sequence_ret;
217 while (cf_sequence_current->next != NULL) {
218 cf_sequence_current = cf_sequence_current->next;
219 }
220 }
221
222 /*
223 * Initialize the ignore list
224 */
225
226 if (previous_results && filelist_count(previous_results->files_to_ignore) > 0) {
227 files_to_ignore = previous_results->files_to_ignore;
228 } else {
229 files_to_ignore = filelist_init();
230 }
231
232 /*
233 * Reset the sequence entry holding vars
234 */
235
236 cf_filter_ret = NULL;
237 cf_filter_current = NULL;
238
239 do {
240 yyparse();
241 }
242 while (!feof(yyin));
243
244 fclose(yyin);
245 fclose(yyout);
246
247 /*
248 * Populate returns
249 */
250
251 ret->sequences = cf_sequence_ret;
252 ret->files_to_ignore = files_to_ignore;
253
254 return ret;
255 }
256
257 void yyerror(char *s) {
258 /*
259 * XXX - Is extern valid here? Does it do what I'm expecting?
260 */
261 extern char *yytext;
262
263 fprintf(stderr, "detox: error parsing config file %s: %s\n", current_filename, s);
264 fprintf(stderr, "\tline %d", config_file_lineno);
265 if (yytext != NULL) {
266 fprintf(stderr, ": %s", yytext);
267 }
268 fprintf(stderr, "\n");
269 exit(EXIT_FAILURE);
270 }
271
272
273 void cf_append_sequence_list(void) {
274 sequence_t *work;
275
276 if (current_name == NULL) {
277 current_name = wrapped_strdup("default");
278 }
279
280 work = NULL;
281
282 if (cf_sequence_ret != NULL) {
283 work = cf_sequence_ret;
284
285 while (work != NULL) {
286 if (strcmp(work->name, current_name) == 0) {
287 break;
288 }
289
290 work = work->next;
291 }
292
293 }
294
295 if (work != NULL) {
296 /*
297 * XXX - Free Old Tree
298 */
299 }
300 else {
301 work = sequence_init(current_name);
302
303 /*
304 * Append to the tree first. If we don't, we could create a
305 * circular reference.
306 */
307 if (cf_sequence_ret == NULL) {
308 cf_sequence_ret = cf_sequence_current = work;
309 }
310 else {
311 cf_sequence_current->next = work;
312 cf_sequence_current = work;
313 }
314
315 }
316
317 work->filters = cf_filter_ret;
318 work->source_filename = wrapped_strdup(current_filename);
319 cf_filter_ret = cf_filter_current = NULL;
320
321 }
322
323
324 void cf_append_filter(int cleaner, char *builtin, char *filename, int max_length, int remove_trailing)
325 {
326 filter_t *work;
327
328 work = filter_init(cleaner);
329 work->builtin = (builtin == NULL) ? NULL : wrapped_strdup(builtin);
330 work->filename = (filename == NULL) ? NULL : wrapped_strdup(filename);
331 work->max_length = (size_t) max_length;
332 work->remove_trailing = remove_trailing;
333
334 if (cf_filter_ret == NULL) {
335 cf_filter_ret = cf_filter_current = work;
336 }
337 else {
338 cf_filter_current->next = work;
339 cf_filter_current = work;
340 }
341 }
342
343
344 void cf_append_ignore_entry(void *str) {
345 filelist_put(files_to_ignore, str);
346 }