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