fixed compiler warnings issued by:
[strongswan.git] / src / starter / parser.l
1 %{
2 /* FreeS/WAN config file parser (parser.l)
3  * Copyright (C) 2001 Mathieu Lafon - Arkoon Network Security
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  *
15  * RCSID $Id$
16  */
17
18 #include <string.h>
19 #include <stdlib.h>
20 #include <glob.h>
21
22 #include "y.tab.h"
23
24 #define MAX_INCLUDE_DEPTH  20
25
26 #define YY_NO_INPUT
27 #define YY_NO_UNPUT
28 extern void yyerror(const char *);
29 extern int yylex (void);
30
31 static struct {
32         int stack_ptr;
33         YY_BUFFER_STATE stack[MAX_INCLUDE_DEPTH];
34         FILE *file[MAX_INCLUDE_DEPTH];
35         unsigned int line[MAX_INCLUDE_DEPTH];
36         char *filename[MAX_INCLUDE_DEPTH];
37 } __parser_y_private;
38
39 void _parser_y_error(char *b, int size, const char *s);
40 void _parser_y_init (const char *f);
41 void _parser_y_fini (void);
42 int _parser_y_include (const char *filename);
43
44 void _parser_y_error(char *b, int size, const char *s)
45 {
46     extern char *yytext; // was: char yytext[];
47
48     snprintf(b, size, "%s:%d: %s [%s]",
49             __parser_y_private.filename[__parser_y_private.stack_ptr],
50             __parser_y_private.line[__parser_y_private.stack_ptr],
51             s, yytext);
52 }
53
54 void _parser_y_init (const char *f)
55 {
56     memset(&__parser_y_private, 0, sizeof(__parser_y_private));
57     __parser_y_private.line[0] = 1;
58     __parser_y_private.filename[0] = strdup(f);
59 }
60
61 void _parser_y_fini (void)
62 {
63     unsigned int i;
64
65     for (i = 0; i < MAX_INCLUDE_DEPTH; i++)
66     {
67         if (__parser_y_private.filename[i])
68             free(__parser_y_private.filename[i]);
69         if (__parser_y_private.file[i])
70             fclose(__parser_y_private.file[i]);
71     }
72     memset(&__parser_y_private, 0, sizeof(__parser_y_private));
73 }
74
75 int _parser_y_include (const char *filename)
76 {
77     glob_t files;
78     int i, ret;
79
80     ret = glob(filename, GLOB_ERR, NULL, &files);
81     if (ret)
82     {
83         const char *err;
84
85         switch (ret)
86         {
87         case GLOB_NOSPACE:
88             err = "include files ran out of memory";
89             break;
90         case GLOB_ABORTED:
91             err = "include files aborted due to read error";
92             break;
93         case GLOB_NOMATCH:
94             err = "include files found no matches";
95             break;
96         default:
97             err = "unknown include files error";
98         }
99         yyerror(err);
100         return 1;
101     }
102
103     for (i = 0; i < files.gl_pathc; i++)
104     {
105         FILE *f;
106         unsigned int p = __parser_y_private.stack_ptr + 1;
107
108         if (p >= MAX_INCLUDE_DEPTH)
109         {
110             yyerror("max inclusion depth reached");
111             return 1;
112         }
113
114         f = fopen(files.gl_pathv[i], "r");
115         if (!f)
116         {
117             yyerror("can't open include filename");
118             continue;
119         }
120
121         __parser_y_private.stack_ptr++;
122         __parser_y_private.file[p] = f;
123         __parser_y_private.stack[p] = YY_CURRENT_BUFFER;
124         __parser_y_private.line[p] = 1;
125         __parser_y_private.filename[p] = strdup(files.gl_pathv[i]);
126
127         yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
128     }
129     globfree(&files);
130     return 0;
131 }
132
133 %}
134
135 %%
136
137 <<EOF>> {
138         if (__parser_y_private.filename[__parser_y_private.stack_ptr]) {
139                 free(__parser_y_private.filename[__parser_y_private.stack_ptr]);
140                 __parser_y_private.filename[__parser_y_private.stack_ptr] = NULL;
141         }
142         if (__parser_y_private.file[__parser_y_private.stack_ptr]) {
143                 fclose(__parser_y_private.file[__parser_y_private.stack_ptr]);
144                 __parser_y_private.file[__parser_y_private.stack_ptr] = NULL;
145                 yy_delete_buffer (YY_CURRENT_BUFFER);
146                 yy_switch_to_buffer
147                         (__parser_y_private.stack[__parser_y_private.stack_ptr]);
148         }
149         if (--__parser_y_private.stack_ptr < 0) {
150                 yyterminate();
151         }
152 }
153
154 ^[\t ]+                 return FIRST_SPACES;
155
156 [\t ]+                  /* ignore spaces in line */ ;
157
158 =                               return EQUAL;
159
160 \n|#.*\n                {
161                                         __parser_y_private.line[__parser_y_private.stack_ptr]++;
162                                         return EOL;
163                                 }
164
165 config                  return CONFIG;
166 setup                   return SETUP;
167 conn                    return CONN;
168 ca                      return CA;
169 include                 return INCLUDE;
170 version                 return FILE_VERSION;
171
172 [^\"= \t\n]+    {
173                                         yylval.s = strdup(yytext);
174                                         return STRING;
175                                 }
176
177 \"[^\"\n]*\"    {
178                                         yylval.s = strdup(yytext+1);
179                                         if (yylval.s) yylval.s[strlen(yylval.s)-1]='\0';
180                                         return STRING;
181                                 }
182
183 .                               yyerror(yytext);
184
185 %%
186
187 int yywrap(void)
188 {
189     return 1;
190 }
191