]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* src/ircd_lexer.l |
2 | * | |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2 of the License, or | |
6 | * (at your option) any later version. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
16 | * USA | |
17 | * | |
212380e3 AC |
18 | */ |
19 | ||
20 | %option case-insensitive | |
21 | %option noyywrap | |
22 | %option nounput | |
23 | ||
24 | %{ | |
25 | #include <sys/types.h> | |
26 | #include <sys/stat.h> | |
27 | ||
28 | #include <netinet/in.h> | |
29 | ||
30 | #include <string.h> | |
31 | #include <errno.h> | |
32 | #include <limits.h> | |
33 | ||
34 | #define WE_ARE_MEMORY_C | |
35 | ||
36 | #include "stdinc.h" | |
37 | #include "ircd_defs.h" | |
38 | #include "common.h" | |
39 | #include "config.h" | |
93a426cb | 40 | #include "logger.h" |
212380e3 AC |
41 | #include "s_conf.h" |
42 | #include "newconf.h" | |
43 | ||
415b482c | 44 | #include "ircd_parser.h" |
212380e3 AC |
45 | |
46 | int yylex(void); | |
47 | ||
212380e3 AC |
48 | #define MAX_INCLUDE_DEPTH 10 |
49 | ||
50 | YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH]; | |
51 | int include_stack_ptr=0; | |
52 | int lineno = 1; | |
53 | void ccomment(void); | |
54 | void cinclude(void); | |
55 | void hashcomment(void); | |
56 | int ieof(void); | |
57 | int lineno_stack[MAX_INCLUDE_DEPTH]; | |
58 | char conffile_stack[MAX_INCLUDE_DEPTH][IRCD_BUFSIZE]; | |
59 | char conffilebuf[IRCD_BUFSIZE+1]; | |
60 | char *current_file = conffilebuf; | |
61 | ||
62 | FILE *inc_fbfile_in[MAX_INCLUDE_DEPTH]; | |
63 | ||
64 | char linebuf[512]; | |
65 | ||
66 | #undef YY_INPUT | |
67 | ||
68 | #define YY_FATAL_ERROR(msg) conf_yy_fatal_error(msg) | |
69 | ||
70 | #define YY_INPUT(buf,result,max_size) \ | |
71 | if (!(result = conf_fgets(buf, max_size, conf_fbfile_in))) \ | |
72 | YY_FATAL_ERROR("input in flex scanner failed"); | |
73 | %} | |
74 | ||
75 | ws [ \t]* | |
76 | digit [0-9] | |
77 | comment #.* | |
78 | qstring \"[^\"\n]*[\"\n] | |
3d8f7aa7 | 79 | string [a-zA-Z_\~\:][a-zA-Z0-9_\:]* |
212380e3 AC |
80 | include \.include{ws}(\<.*\>|\".*\") |
81 | ||
82 | %% | |
83 | {include} { cinclude(); } | |
84 | "/*" { ccomment(); } | |
85 | \n.* { strcpy(linebuf, yytext+1); lineno++; yyless(1); } | |
86 | ||
87 | {ws} ; | |
88 | {comment} { hashcomment(); } | |
89 | ||
90 | {digit}+ { yylval.number = atoi(yytext); return NUMBER; } | |
91 | ||
92 | {qstring} { | |
93 | if(yytext[yyleng-2] == '\\') | |
94 | { | |
95 | yyless(yyleng-1); /* return last quote */ | |
96 | yymore(); /* append next string */ | |
97 | } | |
98 | else | |
99 | { | |
100 | strcpy(yylval.string, yytext + 1); | |
101 | if(yylval.string[yyleng-2] != '"') | |
102 | ilog(L_MAIN, "Unterminated character string"); | |
103 | else | |
104 | { | |
105 | int i,j; | |
106 | yylval.string[yyleng-2] = '\0'; /* remove close | |
107 | * quote | |
108 | */ | |
109 | ||
110 | for (j=i=0 ;yylval.string[i] != '\0'; i++,j++) | |
111 | { | |
112 | if (yylval.string[i] != '\\') | |
113 | { | |
114 | yylval.string[j] = yylval.string[i]; | |
115 | } | |
116 | else | |
117 | { | |
118 | i++; | |
119 | if (yylval.string[i] == '\0') /* XXX | |
120 | * should not | |
121 | * happen | |
122 | */ | |
123 | { | |
124 | ilog(L_MAIN, | |
125 | "Unterminated character string"); | |
126 | break; | |
127 | } | |
128 | yylval.string[j] = yylval.string[i]; | |
129 | } | |
130 | } | |
131 | yylval.string[j] = '\0'; | |
132 | return QSTRING; | |
133 | } | |
134 | } | |
135 | } | |
136 | ||
137 | ||
138 | loadmodule { return LOADMODULE; } | |
139 | {string} { | |
140 | strcpy(yylval.string, yytext); | |
141 | yylval.string[yyleng] = '\0'; | |
142 | return STRING; | |
143 | } | |
144 | ||
145 | \.\. { return TWODOTS; } | |
146 | . { return yytext[0]; } | |
147 | <<EOF>> { if (ieof()) yyterminate(); } | |
148 | %% | |
149 | ||
150 | /* C-comment ignoring routine -kre*/ | |
151 | void ccomment() | |
152 | { | |
153 | int c; | |
154 | ||
155 | /* log(L_NOTICE, "got comment"); */ | |
156 | while (1) | |
157 | { | |
158 | while ((c = input()) != '*' && c != EOF) | |
159 | if (c == '\n') ++lineno; | |
160 | if (c == '*') | |
161 | { | |
162 | while ((c = input()) == '*'); | |
163 | if (c == '/') | |
164 | break; | |
4f3f411b | 165 | if (c == '\n') ++lineno; |
212380e3 AC |
166 | } |
167 | if (c == EOF) | |
168 | { | |
169 | YY_FATAL_ERROR("EOF in comment"); | |
170 | /* XXX hack alert this disables | |
171 | * the stupid unused function warning | |
172 | * gcc generates | |
173 | */ | |
174 | yy_fatal_error("EOF in comment"); | |
175 | break; | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
180 | void cinclude(void) | |
181 | { | |
182 | char *c; | |
183 | if ((c = strchr(yytext, '<')) == NULL) | |
184 | *strchr(c = strchr(yytext, '"') + 1, '"') = 0; | |
185 | else | |
186 | *strchr(++c, '>') = 0; | |
187 | ||
188 | /* do stacking and co. */ | |
189 | if (include_stack_ptr >= MAX_INCLUDE_DEPTH) | |
190 | conf_report_error("Includes nested too deep (max is %d)", MAX_INCLUDE_DEPTH); | |
191 | else | |
192 | { | |
193 | FILE *tmp_fbfile_in; | |
194 | ||
195 | tmp_fbfile_in = fopen(c, "r"); | |
196 | ||
197 | if (tmp_fbfile_in == NULL) | |
198 | { | |
199 | /* if its not found in PREFIX, look in ETCPATH */ | |
200 | char fnamebuf[IRCD_BUFSIZE]; | |
201 | ||
202 | snprintf(fnamebuf, sizeof(fnamebuf), "%s/%s", ETCPATH, c); | |
203 | tmp_fbfile_in = fopen(fnamebuf, "r"); | |
204 | ||
205 | /* wasnt found there either.. error. */ | |
206 | if(tmp_fbfile_in == NULL) | |
207 | { | |
208 | conf_report_error("Include %s: %s.", c, strerror(errno)); | |
209 | return; | |
210 | } | |
211 | } | |
212 | lineno_stack[include_stack_ptr] = lineno; | |
213 | lineno = 1; | |
214 | inc_fbfile_in[include_stack_ptr] = conf_fbfile_in; | |
215 | strcpy(conffile_stack[include_stack_ptr], c); | |
216 | current_file = conffile_stack[include_stack_ptr]; | |
217 | include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER; | |
218 | conf_fbfile_in = tmp_fbfile_in; | |
219 | yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); | |
220 | } | |
221 | } | |
222 | ||
223 | int ieof(void) | |
224 | { | |
225 | if (include_stack_ptr) | |
226 | fclose(conf_fbfile_in); | |
227 | if (--include_stack_ptr < 0) | |
228 | { | |
229 | /* We will now exit the lexer - restore init values if we get /rehash | |
230 | * later and reenter lexer -kre */ | |
231 | include_stack_ptr = 0; | |
232 | lineno = 1; | |
233 | return 1; | |
234 | } | |
235 | /* switch buffer */ | |
236 | yy_delete_buffer(YY_CURRENT_BUFFER); | |
237 | lineno = lineno_stack[include_stack_ptr]; | |
238 | conf_fbfile_in = inc_fbfile_in[include_stack_ptr]; | |
239 | ||
240 | if(include_stack_ptr) | |
241 | current_file = conffile_stack[include_stack_ptr]; | |
242 | else | |
243 | current_file = conffilebuf; | |
244 | ||
245 | yy_switch_to_buffer(include_stack[include_stack_ptr]); | |
246 | return 0; | |
247 | } | |
248 | ||
249 | /* #-comment style, look for #include */ | |
250 | #define INCLUDE "#include" | |
251 | ||
252 | void hashcomment(void) | |
253 | { | |
254 | if (strlen(yytext) < sizeof(INCLUDE) - 1) | |
255 | return; | |
256 | ||
257 | if (!strncasecmp(yytext, INCLUDE, sizeof(INCLUDE) - 1)) | |
258 | yyerror("You probably meant '.include', skipping"); | |
259 | } |