]> jfr.im git - irc/quakenet/snircd.git/blame - ircd/ircd_lexer.l
+2007-02-28 Michael Poole <mdpoole@troilus.org>
[irc/quakenet/snircd.git] / ircd / ircd_lexer.l
CommitLineData
189935b1 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.
951d56c0 20 * $Id: ircd_lexer.l,v 1.21.2.4 2007/02/25 15:41:48 entrope Exp $
189935b1 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
35extern int lineno;
36
37static 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),
9f8856e9 105 TOKEN(PROGRAM),
951d56c0 106 TOKEN(DNS),
189935b1 107#undef TOKEN
108 { "administrator", ADMIN },
109 { "apass_opmode", TPRIV_APASS_OPMODE },
110 { "auto", AUTOCONNECT },
111 { "b", BYTES },
112 { "badchan", TPRIV_BADCHAN },
113 { "chan_limit", TPRIV_CHAN_LIMIT },
114 { "deop_lchan", TPRIV_DEOP_LCHAN },
115 { "die", TPRIV_DIE },
116 { "display", TPRIV_DISPLAY },
117 { "file", TFILE },
118 { "force_local_opmode", TPRIV_FORCE_LOCAL_OPMODE },
119 { "force_opmode", TPRIV_FORCE_OPMODE },
0c466275 120 { "gb", GBYTES },
121 { "gigabytes", GBYTES },
189935b1 122 { "gline", TPRIV_GLINE },
0c466275 123 { "ipv4", TOK_IPV4 },
124 { "ipv6", TOK_IPV6 },
189935b1 125 { "kb", KBYTES },
126 { "kilobytes", KBYTES },
9f8856e9 127 { "list_chan", TPRIV_LIST_CHAN },
189935b1 128 { "local_badchan", TPRIV_LOCAL_BADCHAN },
129 { "local_gline", TPRIV_LOCAL_GLINE },
130 { "local_jupe", TPRIV_LOCAL_JUPE },
131 { "local_kill", TPRIV_LOCAL_KILL },
132 { "local_opmode", TPRIV_LOCAL_OPMODE },
133 { "mb", MBYTES },
134 { "megabytes", MBYTES },
135 { "mode_lchan", TPRIV_MODE_LCHAN },
189935b1 136 { "operator", OPER },
137 { "opmode", TPRIV_OPMODE },
138 { "password", PASS },
139 { "propagate", TPRIV_PROPAGATE },
140 { "realname", REAL },
141 { "rehash", TPRIV_REHASH },
142 { "restart", TPRIV_RESTART },
143 { "see_chan", TPRIV_SEE_CHAN },
144 { "see_opers", TPRIV_SEE_OPERS },
145 { "set", TPRIV_SET },
146 { "show_all_invis", TPRIV_SHOW_ALL_INVIS },
147 { "show_invis", TPRIV_SHOW_INVIS },
148 { "tb", TBYTES },
149 { "terabytes", TBYTES },
150 { "unlimit_query", TPRIV_UNLIMIT_QUERY },
151 { "walk_lchan", TPRIV_WALK_LCHAN },
152 { "wide_gline", TPRIV_WIDE_GLINE },
153 { "whox", TPRIV_WHOX },
154 { NULL, 0 }
155};
156static int ntokens;
157
158static int
159token_compare(const void *pa, const void *pb)
160{
161 const struct lexer_token *ta = pa;
162 const struct lexer_token *tb = pb;
163 unsigned int ii = 0;
164 int res;
165 while (ta->string[ii] && (ToLower(ta->string[ii]) == ToLower(tb->string[ii])))
166 ii++;
167 res = ToLower(tb->string[ii]) - ToLower(ta->string[ii]);
168 return res;
169}
170
171static void
172init_ntokens(void)
173{
174 for (ntokens = 0; tokens[ntokens].string; ++ntokens) ;
175 qsort(tokens, ntokens, sizeof(tokens[0]), token_compare);
176}
177
178static int
179find_token(char *token)
180{
181 struct lexer_token *tok;
182 if (!ntokens)
183 init_ntokens();
184 tok = bsearch(&token, tokens, ntokens, sizeof(tokens[0]), token_compare);
185 return tok ? tok->value : 0;
186}
187
188void
189init_lexer(void)
190{
191 yyin = fopen(configfile, "r");
192 if (yyin == NULL)
193 {
194#ifdef YY_FATAL_ERROR
195 YY_FATAL_ERROR("Could not open the configuration file.");
196#else
197 fprintf(stderr, "Could not open the configuration file.");
198#endif
199 }
200#ifdef YY_NEW_FILE
201 YY_NEW_FILE;
202#endif
203 lineno = 1;
204}
205
206%}
207
208WHITE [ \t\r]+
209SHCOMMENT #[^\n]*
210NUMBER [0-9]+
211QSTRING \"[^"\n]+[\"\n]
212%%
213
214{QSTRING} {yytext[yyleng-1] = 0; DupString(yylval.text, yytext+1); return QSTRING;}
215{NUMBER} {yylval.num = strtoul(yytext, NULL, 10); return NUMBER;}
216{WHITE} ;
217{SHCOMMENT} ;
218
0c466275 219[a-zA-Z_][a-zA-Z_0-9]* { int res = find_token(yytext); if (res) return res; else REJECT; }
189935b1 220\n lineno++;
221. return yytext[0];