~ubuntu-branches/ubuntu/trusty/charybdis/trusty-proposed

« back to all changes in this revision

Viewing changes to src/ircd_lexer.l

  • Committer: Package Import Robot
  • Author(s): Antoine Beaupré
  • Date: 2011-11-10 23:07:37 UTC
  • Revision ID: package-import@ubuntu.com-20111110230737-kqo6qsglp5oh02hr
Tags: upstream-3.3.0
Import upstream version 3.3.0

Show diffs side-by-side

added added

removed removed

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