ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/conf/lexer.l
Revision: 179
Committed: Sat Oct 22 11:23:29 2005 UTC (18 years, 5 months ago) by adx
File size: 5670 byte(s)
Log Message:
- rearranged new conf headers a bit

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * lexer.l: A set of rules for flex.
4 *
5 * Copyright (C) 2003 by Piotr Nizynski, Advanced IRC Services Project
6 * Copyright (C) 2005 by the Hybrid Development Team.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 *
23 * $Id: conf_lexer.l,v 1.5 2003/10/14 16:18:56 adx Exp $
24 */
25
26 %option case-insensitive
27 %option noyywrap
28 %option nounput
29
30 %{
31
32 #include "stdinc.h"
33 #include "conf/conf.h"
34 #include "y.tab.h"
35
36 struct ConfParserContext conf_curctx;
37 char conf_linebuf[CONF_BUFSIZE];
38 int conf_include_sptr = 0;
39
40 #undef YY_INPUT
41 #define YY_INPUT(buf, res, siz) res = conf_yy_input(buf, siz)
42 /* flex's default is to print a message and exit(0) */
43 #define YY_FATAL_ERROR(msg) conf_yy_fatal_error(msg)
44
45 #define MAX_INCLUDE_DEPTH 10
46
47 YY_BUFFER_STATE conf_include_yystack[MAX_INCLUDE_DEPTH];
48 struct ConfParserContext conf_include_ctxstack[MAX_INCLUDE_DEPTH];
49
50 static void conf_include(void);
51 static int conf_eof(void);
52 static int conf_yy_fatal_error(const char *msg)
53 {
54 return(0);
55 }
56
57 %}
58
59 ws [ \t]*
60 include \.include{ws}(\<.*\>|\".*\")
61 qstring \"[^\"]*\"
62 identifier [a-zA-Z_][a-zA-Z0-9_]*
63
64 %%
65
66 {include} { conf_include(); }
67 "/*" {
68 int c;
69
70 while (1)
71 {
72 while ((c = input()) != '*' && c != EOF)
73 if (c == '\n')
74 {
75 conf_curctx.lineno++;
76 conf_linebuf[0] = '\0';
77 }
78 while (c == '*')
79 c = input();
80 if (c == '/')
81 break;
82 if (c == EOF)
83 {
84 yyerror("EOF in comment");
85 break;
86 }
87 }
88 }
89 \n.* {
90 conf_curctx.lineno++;
91 strlcpy(conf_linebuf, yytext + 1, CONF_BUFSIZE);
92 yyless(1);
93 }
94 {ws} ;
95 #.* {
96 if (!strncasecmp(yytext, "#include", 8))
97 yyerror("You probably meant '.include', ignoring");
98 }
99 [0-9]+ { yylval.number = atoi(yytext); return NUMBER; }
100 {qstring} {
101 yytext[yyleng - 1] = '\0';
102 yylval.string = yytext + 1;
103 return QSTRING;
104 }
105 b { return BYTES; }
106 byte { return BYTES; }
107 bytes { return BYTES; }
108 d { return DAYS; }
109 day { return DAYS; }
110 days { return DAYS; }
111 false { yylval.number = 0; return BOOL; }
112 h { return HOURS; }
113 hour { return HOURS; }
114 hours { return HOURS; }
115 kb { return KBYTES; }
116 kbyte { return KBYTES; }
117 kbytes { return KBYTES; }
118 kilobyte { return KBYTES; }
119 kilobytes { return KBYTES; }
120 mb { return MBYTES; }
121 mbyte { return MBYTES; }
122 mbytes { return MBYTES; }
123 megabyte { return MBYTES; }
124 megabytes { return MBYTES; }
125 m { return MINUTES; }
126 min { return MINUTES; }
127 mins { return MINUTES; }
128 minute { return MINUTES; }
129 minutes { return MINUTES; }
130 no { yylval.number = 0; return BOOL; }
131 off { yylval.number = 0; return BOOL; }
132 on { yylval.number = 1; return BOOL; }
133 s { return SECONDS; }
134 sec { return SECONDS; }
135 secs { return SECONDS; }
136 second { return SECONDS; }
137 seconds { return SECONDS; }
138 true { yylval.number = 1; return BOOL; }
139 w { return WEEKS; }
140 week { return WEEKS; }
141 weeks { return WEEKS; }
142 yes { yylval.number = 1; return BOOL; }
143 {identifier} { yylval.string = yytext; return IDENTIFIER; }
144 . { return yytext[0]; }
145 <<EOF>> { if (conf_eof()) yyterminate(); }
146
147 %%
148
149 /*
150 * conf_include()
151 *
152 * Enters a new configuration file.
153 *
154 * inputs: none
155 * output: none
156 */
157 static void
158 conf_include(void)
159 {
160 char *fname, *buf;
161 FBFILE *f;
162
163 if (conf_include_sptr == MAX_INCLUDE_DEPTH)
164 {
165 yyerror("includes nested too deep");
166 return;
167 }
168
169 if ((fname = strchr(yytext, '"')) == NULL)
170 *strchr(fname = strchr(yytext, '<') + 1, '>') = '\0';
171 else
172 *strchr(++fname, '"') = '\0';
173
174 if (fname[0] == '/')
175 f = fbopen(fname, "r");
176 else
177 {
178 buf = MyMalloc(strlen(ETCPATH) + 1 + strlen(fname) + 1);
179 sprintf(buf, "%s/%s", ETCPATH, fname);
180 f = fbopen(buf, "r");
181 MyFree(buf);
182 }
183
184 if (f == NULL)
185 {
186 yyerror("cannot open file to include");
187 return;
188 }
189
190 /* save current state */
191 memcpy(&conf_include_ctxstack[conf_include_sptr], &conf_curctx,
192 sizeof(struct ConfParserContext));
193 conf_include_yystack[conf_include_sptr++] = YY_CURRENT_BUFFER;
194
195 /* switch lexer context */
196 conf_linebuf[0] = '\0';
197 conf_curctx.f = f;
198 DupString(conf_curctx.filename, fname);
199 conf_curctx.lineno = 1;
200 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
201 }
202
203 /*
204 * conf_eof()
205 *
206 * Ends processing of a configuration file.
207 *
208 * inputs: none
209 * output: 1 if the parser should be exited, 0 otherwise
210 */
211 static int
212 conf_eof(void)
213 {
214 if (conf_include_sptr == 0)
215 return 1;
216
217 /* destroy current buffer */
218 yy_delete_buffer(YY_CURRENT_BUFFER);
219 fbclose(conf_curctx.f);
220
221 /* restore old context */
222 conf_linebuf[0] = '\0';
223 memcpy(&conf_curctx, &conf_include_ctxstack[--conf_include_sptr],
224 sizeof(struct ConfParserContext));
225 yy_switch_to_buffer(conf_include_yystack[conf_include_sptr]);
226
227 return 0;
228 yy_fatal_error(NULL); /* use it somewhere to avoid the warning */
229 }