Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / ircd_lexer.l
bloba5c6497677191ef0a5244dff1770645f7214b724
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  */
19 %option case-insensitive
20 %option noyywrap
21 %option nounput
24 #include <sys/types.h>
25 #include <sys/stat.h>
27 #include <netinet/in.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <limits.h>
33 #define WE_ARE_MEMORY_C
35 #include "stdinc.h"
36 #include "ircd_defs.h"
37 #include "common.h"
38 #include "config.h"
39 #include "s_log.h"
40 #include "s_conf.h"
41 #include "newconf.h"
43 #include "y.tab.h"
45 int yylex(void);
47 /* here to fixup gcc 3.3 errors */
48 int yyget_lineno(void);
49 FILE *yyget_in(void);
50 FILE *yyget_out(void);
51 int yyget_leng(void);
52 char *yyget_text(void);
53 void yyset_lineno(int  line_number);
54 void yyset_in(FILE *  in_str);
55 void yyset_out(FILE *  out_str);
56 int yyget_debug(void);
57 void yyset_debug(int  bdebug);
58 int yylex_destroy(void);
61 #define MAX_INCLUDE_DEPTH 10
63 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
64 int include_stack_ptr=0;
65 int lineno = 1;
66 void ccomment(void);
67 void cinclude(void);
68 void hashcomment(void);
69 int ieof(void);
70 int lineno_stack[MAX_INCLUDE_DEPTH];
71 char conffile_stack[MAX_INCLUDE_DEPTH][IRCD_BUFSIZE];
72 char conffilebuf[IRCD_BUFSIZE+1];
73 char *current_file = conffilebuf;
75 FILE *inc_fbfile_in[MAX_INCLUDE_DEPTH];
77 char linebuf[512];
79 #undef YY_INPUT
81 #define YY_FATAL_ERROR(msg) conf_yy_fatal_error(msg)
83 #define YY_INPUT(buf,result,max_size) \
84   if (!(result = conf_fgets(buf, max_size, conf_fbfile_in))) \
85     YY_FATAL_ERROR("input in flex scanner failed"); 
88 ws        [ \t]*
89 digit     [0-9]
90 comment   #.*
91 qstring   \"[^\"\n]*[\"\n]
92 string    [a-zA-Z_\~][a-zA-Z0-9_]*
93 include   \.include{ws}(\<.*\>|\".*\")
96 {include}       { cinclude(); }
97 "/*"            { ccomment(); }
98 \n.*            { strcpy(linebuf, yytext+1); lineno++; yyless(1); }
100 {ws}            ;
101 {comment}       { hashcomment(); }
103 {digit}+        { yylval.number = atoi(yytext); return NUMBER; }
105 {qstring}       {
106                   if(yytext[yyleng-2] == '\\')
107                     {
108                       yyless(yyleng-1); /* return last quote */
109                       yymore();         /* append next string */
110                     }
111                   else
112                     {
113                       strcpy(yylval.string, yytext + 1);
114                       if(yylval.string[yyleng-2] != '"')
115                         ilog(L_MAIN, "Unterminated character string");
116                       else
117                         {
118                           int i,j;
119                           yylval.string[yyleng-2] = '\0'; /* remove close
120                                                            *  quote 
121                                                            */
122                           
123                           for (j=i=0 ;yylval.string[i] != '\0'; i++,j++)
124                             {
125                               if (yylval.string[i] != '\\')
126                                 {
127                                   yylval.string[j] = yylval.string[i];
128                                 }
129                               else
130                                 {
131                                   i++;
132                                   if (yylval.string[i] == '\0') /* XXX 
133                                                                  * should not
134                                                                  * happen
135                                                                  */
136                                     {
137                                       ilog(L_MAIN,
138                                            "Unterminated character string");
139                                       break;
140                                     }
141                                   yylval.string[j] = yylval.string[i];
142                                 }
143                             }
144                           yylval.string[j] = '\0';
145                           return QSTRING;
146                         }
147                     }
148                 }
151 loadmodule      { return LOADMODULE; }
152 {string}        { 
153                   strcpy(yylval.string, yytext);
154                   yylval.string[yyleng] = '\0';
155                   return STRING;
156                 }
158 \.\.            { return TWODOTS; }
159 .               { return yytext[0]; }
160 <<EOF>>         { if (ieof()) yyterminate(); }
163 /* C-comment ignoring routine -kre*/
164 void ccomment()
166   int c;
167   
168   /* log(L_NOTICE, "got comment"); */
169   while (1)
170     {
171       while ((c = input()) != '*' && c != EOF)
172         if (c == '\n') ++lineno;
173       if (c == '*')
174         {
175           while ((c = input()) == '*');
176           if (c == '/') 
177             break;
178         }
179       if (c == EOF)
180         {
181           YY_FATAL_ERROR("EOF in comment");
182           /* XXX hack alert this disables
183            * the stupid unused function warning
184            * gcc generates
185            */
186           yy_fatal_error("EOF in comment");
187           break;
188         }
189     }
192 void cinclude(void)
194   char *c;
195   if ((c = strchr(yytext, '<')) == NULL)
196     *strchr(c = strchr(yytext, '"') + 1, '"') = 0;
197   else
198     *strchr(++c, '>') = 0;
200   /* do stacking and co. */ 
201   if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
202     conf_report_error("Includes nested too deep (max is %d)", MAX_INCLUDE_DEPTH);
203   else
204   {
205     FILE *tmp_fbfile_in;
206     
207     tmp_fbfile_in = fopen(c, "r");
208     
209     if (tmp_fbfile_in == NULL)
210     {
211       /* if its not found in PREFIX, look in ETCPATH */
212       char fnamebuf[IRCD_BUFSIZE];
214       snprintf(fnamebuf, sizeof(fnamebuf), "%s/%s", ETCPATH, c);
215       tmp_fbfile_in = fopen(fnamebuf, "r");
217       /* wasnt found there either.. error. */
218       if(tmp_fbfile_in == NULL)
219       {
220         conf_report_error("Include %s: %s.", c, strerror(errno));
221         return;
222       }
223     }
224     lineno_stack[include_stack_ptr] = lineno;
225     lineno = 1;
226     inc_fbfile_in[include_stack_ptr] = conf_fbfile_in;
227     strcpy(conffile_stack[include_stack_ptr], c);
228     current_file = conffile_stack[include_stack_ptr];
229     include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
230     conf_fbfile_in = tmp_fbfile_in;
231     yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
232   }
235 int ieof(void)
237   if (include_stack_ptr)
238     fclose(conf_fbfile_in);
239   if (--include_stack_ptr < 0)
240   {
241     /* We will now exit the lexer - restore init values if we get /rehash
242      * later and reenter lexer -kre */
243     include_stack_ptr = 0;
244     lineno = 1;
245     return 1;
246   }
247   /* switch buffer */
248   yy_delete_buffer(YY_CURRENT_BUFFER);
249   lineno = lineno_stack[include_stack_ptr];
250   conf_fbfile_in = inc_fbfile_in[include_stack_ptr];
252   if(include_stack_ptr)
253     current_file = conffile_stack[include_stack_ptr];
254   else
255     current_file = conffilebuf;
257   yy_switch_to_buffer(include_stack[include_stack_ptr]);
258   return 0;
261 /* #-comment style, look for #include */
262 #define INCLUDE "#include"
264 void hashcomment(void)
266   if (strlen(yytext) < sizeof(INCLUDE) - 1)
267     return;
269   if (!strncasecmp(yytext, INCLUDE, sizeof(INCLUDE) - 1))
270       yyerror("You probably meant '.include', skipping");