]> jfr.im git - solanum.git/blob - ircd/ircd_lexer.l
global masktrace doesn't need to be an operspy action
[solanum.git] / ircd / ircd_lexer.l
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 *
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 <string.h>
29 #include <errno.h>
30 #include <limits.h>
31
32 #define WE_ARE_MEMORY_C
33
34 #include "stdinc.h"
35 #include "ircd_defs.h"
36 #include "defaults.h"
37 #include "logger.h"
38 #include "s_conf.h"
39 #include "newconf.h"
40
41 #include "ircd_parser.h"
42
43 int yylex(void);
44
45 #define MAX_INCLUDE_DEPTH 10
46
47 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
48 int include_stack_ptr=0;
49 int lineno = 1;
50 void ccomment(void);
51 void cinclude(void);
52 void hashcomment(void);
53 int ieof(void);
54 int lineno_stack[MAX_INCLUDE_DEPTH];
55 char conffile_stack[MAX_INCLUDE_DEPTH][BUFSIZE];
56 char conffilebuf[BUFSIZE+1];
57 char *current_file = conffilebuf;
58
59 FILE *inc_fbfile_in[MAX_INCLUDE_DEPTH];
60
61 char yy_linebuf[16384];
62
63 #undef YY_INPUT
64
65 #define YY_FATAL_ERROR(msg) conf_yy_fatal_error(msg)
66
67 #define YY_INPUT(buf,result,max_size) \
68 if (!(result = conf_fgets(buf, max_size, conf_fbfile_in))) \
69 YY_FATAL_ERROR("input in flex scanner failed");
70 %}
71
72 ws [ \t]*
73 digit [0-9]
74 comment #.*
75 qstring \"[^\"\n]*[\"\n]
76 string [a-zA-Z_\~\:][a-zA-Z0-9_\:]*
77 include \.include{ws}(\<.*\>|\".*\")
78
79 %%
80 {include} { cinclude(); }
81 "/*" { ccomment(); }
82 \n.* { rb_strlcpy(yy_linebuf, yytext+1, sizeof(yy_linebuf)); lineno++; yyless(1); }
83
84 {ws} ;
85 {comment} { hashcomment(); }
86
87 {digit}+ { yylval.number = atoi(yytext); return NUMBER; }
88
89 {qstring} {
90 if(yytext[yyleng-2] == '\\')
91 {
92 yyless(yyleng-1); /* return last quote */
93 yymore(); /* append next string */
94 }
95 else
96 {
97 rb_strlcpy(yylval.string, yytext + 1, 1024);
98 if(yylval.string[yyleng-2] != '"')
99 ilog(L_MAIN, "Unterminated character string");
100 else
101 {
102 int i,j;
103 yylval.string[yyleng-2] = '\0'; /* remove close
104 * quote
105 */
106
107 for (j=i=0 ;yylval.string[i] != '\0'; i++,j++)
108 {
109 if (yylval.string[i] != '\\')
110 {
111 yylval.string[j] = yylval.string[i];
112 }
113 else
114 {
115 i++;
116 if (yylval.string[i] == '\0') /* XXX
117 * should not
118 * happen
119 */
120 {
121 ilog(L_MAIN,
122 "Unterminated character string");
123 break;
124 }
125 yylval.string[j] = yylval.string[i];
126 }
127 }
128 yylval.string[j] = '\0';
129 return QSTRING;
130 }
131 }
132 }
133
134
135 loadmodule { return LOADMODULE; }
136 {string} {
137 rb_strlcpy(yylval.string, yytext, 1024);
138 yylval.string[yyleng] = '\0';
139 return STRING;
140 }
141
142 \.\. { return TWODOTS; }
143 . { return yytext[0]; }
144 <<EOF>> { if (ieof()) yyterminate(); }
145 %%
146
147 /* C-comment ignoring routine -kre*/
148 void ccomment()
149 {
150 int c;
151
152 /* log(L_NOTICE, "got comment"); */
153 while (1)
154 {
155 while ((c = input()) != '*' && c != EOF)
156 if (c == '\n') ++lineno;
157 if (c == '*')
158 {
159 while ((c = input()) == '*');
160 if (c == '/')
161 break;
162 if (c == '\n') ++lineno;
163 }
164 if (c == EOF)
165 {
166 YY_FATAL_ERROR("EOF in comment");
167 /* XXX hack alert this disables
168 * the stupid unused function warning
169 * gcc generates
170 */
171 yy_fatal_error("EOF in comment");
172 break;
173 }
174 }
175 }
176
177 void cinclude(void)
178 {
179 char *c;
180 if ((c = strchr(yytext, '<')) == NULL)
181 *strchr(c = strchr(yytext, '"') + 1, '"') = 0;
182 else
183 *strchr(++c, '>') = 0;
184
185 /* do stacking and co. */
186 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
187 conf_report_error("Includes nested too deep (max is %d)", MAX_INCLUDE_DEPTH);
188 else
189 {
190 FILE *tmp_fbfile_in;
191
192 tmp_fbfile_in = fopen(c, "r");
193
194 if (tmp_fbfile_in == NULL)
195 {
196 /* if its not found in PREFIX, look in IRCD_PATH_ETC */
197 char fnamebuf[BUFSIZE];
198
199 snprintf(fnamebuf, sizeof(fnamebuf), "%s/%s", ircd_paths[IRCD_PATH_ETC], c);
200 tmp_fbfile_in = fopen(fnamebuf, "r");
201
202 /* wasnt found there either.. error. */
203 if(tmp_fbfile_in == NULL)
204 {
205 conf_report_error("Include %s: %s.", c, strerror(errno));
206 return;
207 }
208 }
209 lineno_stack[include_stack_ptr] = lineno;
210 lineno = 1;
211 inc_fbfile_in[include_stack_ptr] = conf_fbfile_in;
212 strcpy(conffile_stack[include_stack_ptr], c);
213 current_file = conffile_stack[include_stack_ptr];
214 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
215 conf_fbfile_in = tmp_fbfile_in;
216 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
217 }
218 }
219
220 int ieof(void)
221 {
222 if (include_stack_ptr)
223 fclose(conf_fbfile_in);
224 if (--include_stack_ptr < 0)
225 {
226 /* We will now exit the lexer - restore init values if we get /rehash
227 * later and reenter lexer -kre */
228 include_stack_ptr = 0;
229 lineno = 1;
230 return 1;
231 }
232 /* switch buffer */
233 yy_delete_buffer(YY_CURRENT_BUFFER);
234 lineno = lineno_stack[include_stack_ptr];
235 conf_fbfile_in = inc_fbfile_in[include_stack_ptr];
236
237 if(include_stack_ptr)
238 current_file = conffile_stack[include_stack_ptr];
239 else
240 current_file = conffilebuf;
241
242 yy_switch_to_buffer(include_stack[include_stack_ptr]);
243 return 0;
244 }
245
246 /* #-comment style, look for #include */
247 #define INCLUDE "#include"
248
249 void hashcomment(void)
250 {
251 if (strlen(yytext) < sizeof(INCLUDE) - 1)
252 return;
253
254 if (!rb_strncasecmp(yytext, INCLUDE, sizeof(INCLUDE) - 1))
255 yyerror("You probably meant '.include', skipping");
256 }