4 /* FreeS/WAN config file parser (parser.l)
5 * Copyright (C) 2001 Mathieu Lafon - Arkoon Network Security
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 #define MAX_INCLUDE_DEPTH 20
29 extern void yyerror(const char *);
30 extern int yylex(void);
31 extern int yylex_destroy(void);
35 YY_BUFFER_STATE stack[MAX_INCLUDE_DEPTH];
36 FILE *file[MAX_INCLUDE_DEPTH];
37 unsigned int line[MAX_INCLUDE_DEPTH];
38 char *filename[MAX_INCLUDE_DEPTH];
41 void _parser_y_error(char *b, int size, const char *s);
42 void _parser_y_init (const char *f);
43 void _parser_y_fini (void);
44 int _parser_y_include (const char *filename);
46 void _parser_y_error(char *b, int size, const char *s)
48 extern char *yytext; // was: char yytext[];
50 snprintf(b, size, "%s:%d: %s [%s]",
51 __parser_y_private.filename[__parser_y_private.stack_ptr],
52 __parser_y_private.line[__parser_y_private.stack_ptr],
56 void _parser_y_init (const char *f)
58 memset(&__parser_y_private, 0, sizeof(__parser_y_private));
59 __parser_y_private.line[0] = 1;
60 __parser_y_private.filename[0] = strdup(f);
63 void _parser_y_fini (void)
67 for (i = 0; i < MAX_INCLUDE_DEPTH; i++)
69 if (__parser_y_private.filename[i])
70 free(__parser_y_private.filename[i]);
71 if (__parser_y_private.file[i])
72 fclose(__parser_y_private.file[i]);
74 memset(&__parser_y_private, 0, sizeof(__parser_y_private));
79 * parse the file located at filename
81 int include_file(char *filename)
83 unsigned int p = __parser_y_private.stack_ptr + 1;
86 if (p >= MAX_INCLUDE_DEPTH)
88 yyerror("max inclusion depth reached");
92 f = fopen(filename, "r");
95 yyerror("can't open include filename");
96 return 0; /* ignore this error */
99 __parser_y_private.stack_ptr++;
100 __parser_y_private.file[p] = f;
101 __parser_y_private.stack[p] = YY_CURRENT_BUFFER;
102 __parser_y_private.line[p] = 1;
103 __parser_y_private.filename[p] = strdup(filename);
105 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
109 int _parser_y_include (const char *filename)
117 ret = glob(filename, GLOB_ERR, NULL, &files);
125 err = "include files ran out of memory";
128 err = "include files aborted due to read error";
131 err = "include files found no matches";
134 err = "unknown include files error";
141 for (i = 0; i < files.gl_pathc; i++)
143 if ((ret = include_file(files.gl_pathv[i])))
150 #else /* HAVE_GLOB_H */
151 /* if glob(3) is not available, try to load pattern directly */
152 ret = include_file(filename);
153 #endif /* HAVE_GLOB_H */
162 if (__parser_y_private.filename[__parser_y_private.stack_ptr]) {
163 free(__parser_y_private.filename[__parser_y_private.stack_ptr]);
164 __parser_y_private.filename[__parser_y_private.stack_ptr] = NULL;
166 if (__parser_y_private.file[__parser_y_private.stack_ptr]) {
167 fclose(__parser_y_private.file[__parser_y_private.stack_ptr]);
168 __parser_y_private.file[__parser_y_private.stack_ptr] = NULL;
169 yy_delete_buffer (YY_CURRENT_BUFFER);
171 (__parser_y_private.stack[__parser_y_private.stack_ptr]);
173 if (--__parser_y_private.stack_ptr < 0) {
178 ^[\t ]+ return FIRST_SPACES;
180 [\t ]+ /* ignore spaces in line */ ;
185 __parser_y_private.line[__parser_y_private.stack_ptr]++;
189 config return CONFIG;
193 include return INCLUDE;
194 version return FILE_VERSION;
197 yylval.s = strdup(yytext);
202 yylval.s = strdup(yytext+1);
203 if (yylval.s) yylval.s[strlen(yylval.s)-1]='\0';