added missing equals() method assignment for ID_ANY identities
[strongswan.git] / src / starter / parser.l
1 %option noinput
2 %option nounput
3 %{
4 /* FreeS/WAN config file parser (parser.l)
5  * Copyright (C) 2001 Mathieu Lafon - Arkoon Network Security
6  *
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>.
11  *
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
15  * for more details.
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 extern void yyerror(const char *);
27 extern int yylex (void);
28
29 static struct {
30         int stack_ptr;
31         YY_BUFFER_STATE stack[MAX_INCLUDE_DEPTH];
32         FILE *file[MAX_INCLUDE_DEPTH];
33         unsigned int line[MAX_INCLUDE_DEPTH];
34         char *filename[MAX_INCLUDE_DEPTH];
35 } __parser_y_private;
36
37 void _parser_y_error(char *b, int size, const char *s);
38 void _parser_y_init (const char *f);
39 void _parser_y_fini (void);
40 int _parser_y_include (const char *filename);
41
42 void _parser_y_error(char *b, int size, const char *s)
43 {
44         extern char *yytext; // was: char yytext[];
45
46         snprintf(b, size, "%s:%d: %s [%s]",
47                         __parser_y_private.filename[__parser_y_private.stack_ptr],
48                         __parser_y_private.line[__parser_y_private.stack_ptr],
49                         s, yytext);
50 }
51
52 void _parser_y_init (const char *f)
53 {
54         memset(&__parser_y_private, 0, sizeof(__parser_y_private));
55         __parser_y_private.line[0] = 1;
56         __parser_y_private.filename[0] = strdup(f);
57 }
58
59 void _parser_y_fini (void)
60 {
61         unsigned int i;
62
63         for (i = 0; i < MAX_INCLUDE_DEPTH; i++)
64         {
65                 if (__parser_y_private.filename[i])
66                         free(__parser_y_private.filename[i]);
67                 if (__parser_y_private.file[i])
68                         fclose(__parser_y_private.file[i]);
69         }
70         memset(&__parser_y_private, 0, sizeof(__parser_y_private));
71 }
72
73 int _parser_y_include (const char *filename)
74 {
75         glob_t files;
76         int i, ret;
77
78         ret = glob(filename, GLOB_ERR, NULL, &files);
79         if (ret)
80         {
81                 const char *err;
82
83                 switch (ret)
84                 {
85                 case GLOB_NOSPACE:
86                         err = "include files ran out of memory";
87                         break;
88                 case GLOB_ABORTED:
89                         err = "include files aborted due to read error";
90                         break;
91                 case GLOB_NOMATCH:
92                         err = "include files found no matches";
93                         break;
94                 default:
95                         err = "unknown include files error";
96                 }
97                 yyerror(err);
98                 return 1;
99         }
100
101         for (i = 0; i < files.gl_pathc; i++)
102         {
103                 FILE *f;
104                 unsigned int p = __parser_y_private.stack_ptr + 1;
105
106                 if (p >= MAX_INCLUDE_DEPTH)
107                 {
108                         yyerror("max inclusion depth reached");
109                         return 1;
110                 }
111
112                 f = fopen(files.gl_pathv[i], "r");
113                 if (!f)
114                 {
115                         yyerror("can't open include filename");
116                         continue;
117                 }
118
119                 __parser_y_private.stack_ptr++;
120                 __parser_y_private.file[p] = f;
121                 __parser_y_private.stack[p] = YY_CURRENT_BUFFER;
122                 __parser_y_private.line[p] = 1;
123                 __parser_y_private.filename[p] = strdup(files.gl_pathv[i]);
124
125                 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
126         }
127         globfree(&files);
128         return 0;
129 }
130
131 %}
132
133 %%
134
135 <<EOF>> {
136                 if (__parser_y_private.filename[__parser_y_private.stack_ptr]) {
137                                 free(__parser_y_private.filename[__parser_y_private.stack_ptr]);
138                                 __parser_y_private.filename[__parser_y_private.stack_ptr] = NULL;
139                 }
140                 if (__parser_y_private.file[__parser_y_private.stack_ptr]) {
141                                 fclose(__parser_y_private.file[__parser_y_private.stack_ptr]);
142                                 __parser_y_private.file[__parser_y_private.stack_ptr] = NULL;
143                                 yy_delete_buffer (YY_CURRENT_BUFFER);
144                                 yy_switch_to_buffer
145                                                 (__parser_y_private.stack[__parser_y_private.stack_ptr]);
146                 }
147                 if (--__parser_y_private.stack_ptr < 0) {
148                                 yyterminate();
149                 }
150 }
151
152 ^[\t ]+                 return FIRST_SPACES;
153
154 [\t ]+                  /* ignore spaces in line */ ;
155
156 =                               return EQUAL;
157
158 \n|#.*\n                {
159                                                                                 __parser_y_private.line[__parser_y_private.stack_ptr]++;
160                                                                                 return EOL;
161                                                                 }
162
163 config                  return CONFIG;
164 setup                   return SETUP;
165 conn                    return CONN;
166 ca                      return CA;
167 include                 return INCLUDE;
168 version                 return FILE_VERSION;
169
170 [^\"= \t\n]+    {
171                                                                                 yylval.s = strdup(yytext);
172                                                                                 return STRING;
173                                                                 }
174
175 \"[^\"\n]*\"    {
176                                                                                 yylval.s = strdup(yytext+1);
177                                                                                 if (yylval.s) yylval.s[strlen(yylval.s)-1]='\0';
178                                                                                 return STRING;
179                                                                 }
180
181 .                               yyerror(yytext);
182
183 %%
184
185 int yywrap(void)
186 {
187         return 1;
188 }
189