]> jfr.im git - irc/quakenet/snircd.git/blob - ircd/ircd_lexer.l
import of 2.10.12.07
[irc/quakenet/snircd.git] / ircd / ircd_lexer.l
1 /*
2 * ircd_lexer.l: A lexical scanner for ircd config files.
3 * This is part of ircu, an Internet Relay Chat server.
4 * The contents of this file are Copyright(C) 2001 by Andrew Miller, the
5 * ircd-hybrid team and the ircu team.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
20 * $Id: ircd_lexer.l,v 1.21.2.2 2006/06/08 02:11:21 entrope Exp $
21 */
22
23 %{
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "config.h"
29 #include "ircd.h"
30 #include "ircd_alloc.h"
31 #include "ircd_string.h"
32 #include "s_debug.h"
33 #include "y.tab.h"
34
35 extern int lineno;
36
37 static struct lexer_token {
38 const char *string;
39 int value;
40 } tokens[] = {
41 #define TOKEN(NAME) { #NAME, NAME }
42 TOKEN(ADMIN),
43 TOKEN(GENERAL),
44 TOKEN(LOCATION),
45 TOKEN(CONTACT),
46 TOKEN(CLASS),
47 TOKEN(PINGFREQ),
48 TOKEN(CONNECT),
49 TOKEN(CONNECTFREQ),
50 TOKEN(MAXLINKS),
51 TOKEN(MAXHOPS),
52 TOKEN(SENDQ),
53 TOKEN(NAME),
54 TOKEN(HOST),
55 TOKEN(IP),
56 TOKEN(USERNAME),
57 TOKEN(PASS),
58 TOKEN(SECONDS),
59 TOKEN(MINUTES),
60 TOKEN(HOURS),
61 TOKEN(DAYS),
62 TOKEN(WEEKS),
63 TOKEN(MONTHS),
64 TOKEN(YEARS),
65 TOKEN(DECADES),
66 TOKEN(BYTES),
67 TOKEN(KBYTES),
68 TOKEN(MBYTES),
69 TOKEN(GBYTES),
70 TOKEN(TBYTES),
71 TOKEN(PORT),
72 TOKEN(SERVER),
73 TOKEN(YES),
74 TOKEN(NO),
75 TOKEN(HUB),
76 TOKEN(LEAF),
77 TOKEN(UWORLD),
78 TOKEN(OPER),
79 TOKEN(LOCAL),
80 TOKEN(VHOST),
81 TOKEN(MASK),
82 TOKEN(HIDDEN),
83 TOKEN(MOTD),
84 TOKEN(NUMERIC),
85 TOKEN(NICK),
86 TOKEN(JUPE),
87 TOKEN(DESCRIPTION),
88 TOKEN(CLIENT),
89 TOKEN(REAL),
90 TOKEN(REASON),
91 TOKEN(RULE),
92 TOKEN(ALL),
93 TOKEN(CRULE),
94 TOKEN(KILL),
95 TOKEN(QUARANTINE),
96 TOKEN(IAUTH),
97 TOKEN(TIMEOUT),
98 TOKEN(FEATURES),
99 TOKEN(CHANNEL),
100 TOKEN(PSEUDO),
101 TOKEN(PREPEND),
102 TOKEN(USERMODE),
103 TOKEN(FAST),
104 TOKEN(AUTOCONNECT),
105 TOKEN(PROGRAM),
106 #undef TOKEN
107 { "administrator", ADMIN },
108 { "apass_opmode", TPRIV_APASS_OPMODE },
109 { "auto", AUTOCONNECT },
110 { "b", BYTES },
111 { "badchan", TPRIV_BADCHAN },
112 { "chan_limit", TPRIV_CHAN_LIMIT },
113 { "deop_lchan", TPRIV_DEOP_LCHAN },
114 { "die", TPRIV_DIE },
115 { "display", TPRIV_DISPLAY },
116 { "file", TFILE },
117 { "force_local_opmode", TPRIV_FORCE_LOCAL_OPMODE },
118 { "force_opmode", TPRIV_FORCE_OPMODE },
119 { "gline", TPRIV_GLINE },
120 { "kb", KBYTES },
121 { "kilobytes", KBYTES },
122 { "list_chan", TPRIV_LIST_CHAN },
123 { "local_badchan", TPRIV_LOCAL_BADCHAN },
124 { "local_gline", TPRIV_LOCAL_GLINE },
125 { "local_jupe", TPRIV_LOCAL_JUPE },
126 { "local_kill", TPRIV_LOCAL_KILL },
127 { "local_opmode", TPRIV_LOCAL_OPMODE },
128 { "mb", MBYTES },
129 { "megabytes", MBYTES },
130 { "mode_lchan", TPRIV_MODE_LCHAN },
131 { "gb", GBYTES },
132 { "gigabytes", GBYTES },
133 { "operator", OPER },
134 { "opmode", TPRIV_OPMODE },
135 { "password", PASS },
136 { "propagate", TPRIV_PROPAGATE },
137 { "realname", REAL },
138 { "rehash", TPRIV_REHASH },
139 { "restart", TPRIV_RESTART },
140 { "see_chan", TPRIV_SEE_CHAN },
141 { "see_opers", TPRIV_SEE_OPERS },
142 { "set", TPRIV_SET },
143 { "show_all_invis", TPRIV_SHOW_ALL_INVIS },
144 { "show_invis", TPRIV_SHOW_INVIS },
145 { "tb", TBYTES },
146 { "terabytes", TBYTES },
147 { "unlimit_query", TPRIV_UNLIMIT_QUERY },
148 { "walk_lchan", TPRIV_WALK_LCHAN },
149 { "wide_gline", TPRIV_WIDE_GLINE },
150 { "whox", TPRIV_WHOX },
151 { NULL, 0 }
152 };
153 static int ntokens;
154
155 static int
156 token_compare(const void *pa, const void *pb)
157 {
158 const struct lexer_token *ta = pa;
159 const struct lexer_token *tb = pb;
160 unsigned int ii = 0;
161 int res;
162 while (ta->string[ii] && (ToLower(ta->string[ii]) == ToLower(tb->string[ii])))
163 ii++;
164 res = ToLower(tb->string[ii]) - ToLower(ta->string[ii]);
165 return res;
166 }
167
168 static void
169 init_ntokens(void)
170 {
171 for (ntokens = 0; tokens[ntokens].string; ++ntokens) ;
172 qsort(tokens, ntokens, sizeof(tokens[0]), token_compare);
173 }
174
175 static int
176 find_token(char *token)
177 {
178 struct lexer_token *tok;
179 if (!ntokens)
180 init_ntokens();
181 tok = bsearch(&token, tokens, ntokens, sizeof(tokens[0]), token_compare);
182 return tok ? tok->value : 0;
183 }
184
185 void
186 init_lexer(void)
187 {
188 yyin = fopen(configfile, "r");
189 if (yyin == NULL)
190 {
191 #ifdef YY_FATAL_ERROR
192 YY_FATAL_ERROR("Could not open the configuration file.");
193 #else
194 fprintf(stderr, "Could not open the configuration file.");
195 #endif
196 }
197 #ifdef YY_NEW_FILE
198 YY_NEW_FILE;
199 #endif
200 lineno = 1;
201 }
202
203 %}
204
205 WHITE [ \t\r]+
206 SHCOMMENT #[^\n]*
207 NUMBER [0-9]+
208 QSTRING \"[^"\n]+[\"\n]
209 %%
210
211 {QSTRING} {yytext[yyleng-1] = 0; DupString(yylval.text, yytext+1); return QSTRING;}
212 {NUMBER} {yylval.num = strtoul(yytext, NULL, 10); return NUMBER;}
213 {WHITE} ;
214 {SHCOMMENT} ;
215
216 [a-zA-Z_]+ { int res = find_token(yytext); if (res) return res; else REJECT; }
217 \n lineno++;
218 . return yytext[0];