458e571cdd867cd1a4052c7725514d3524e2e7f0
[ircu2.10.12-pk.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$
21  */
22
23 %{
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include "config.h"
28 #include "ircd.h"
29 #include "ircd_string.h"
30 #include "s_debug.h"
31 #include "y.tab.h"
32
33 extern int lineno;
34
35 static struct lexer_token {
36   const char *string;
37   int value;
38 } tokens[] = {
39 #define TOKEN(NAME) { #NAME, NAME }
40   TOKEN(ADMIN),
41   TOKEN(GENERAL),
42   TOKEN(LOCATION),
43   TOKEN(CONTACT),
44   TOKEN(CLASS),
45   TOKEN(PINGFREQ),
46   TOKEN(CONNECT),
47   TOKEN(CONNECTFREQ),
48   TOKEN(MAXLINKS),
49   TOKEN(SENDQ),
50   TOKEN(NAME),
51   TOKEN(HOST),
52   TOKEN(PASS),
53   TOKEN(SECONDS),
54   TOKEN(MINUTES),
55   TOKEN(HOURS),
56   TOKEN(DAYS),
57   TOKEN(WEEKS),
58   TOKEN(MONTHS),
59   TOKEN(YEARS),
60   TOKEN(DECADES),
61   TOKEN(BYTES),
62   TOKEN(KBYTES),
63   TOKEN(MBYTES),
64   TOKEN(GBYTES),
65   TOKEN(TBYTES),
66   TOKEN(PORT),
67   TOKEN(SERVER),
68   TOKEN(YES),
69   TOKEN(NO),
70   TOKEN(HUB),
71   TOKEN(LEAF),
72   TOKEN(UWORLD),
73   TOKEN(OPER),
74   TOKEN(LOCAL),
75   TOKEN(VHOST),
76   TOKEN(MASK),
77   TOKEN(HIDDEN),
78   TOKEN(MOTD),
79   TOKEN(NUMERIC),
80   TOKEN(NICK),
81   TOKEN(JUPE),
82   TOKEN(DESCRIPTION),
83   TOKEN(CLIENT),
84   TOKEN(REAL),
85   TOKEN(REASON),
86   TOKEN(RULE),
87   TOKEN(ALL),
88   TOKEN(IP),
89   TOKEN(CRULE),
90   TOKEN(KILL),
91   TOKEN(QUARANTINE),
92   TOKEN(IAUTH),
93   TOKEN(TIMEOUT),
94   TOKEN(FEATURES),
95   TOKEN(CHANNEL),
96   TOKEN(PSEUDO),
97   TOKEN(PREPEND),
98   TOKEN(USERMODE),
99 #undef TOKEN
100   { "administrator", ADMIN },
101   { "b", BYTES },
102   { "badchan", TPRIV_BADCHAN },
103   { "chan_limit", TPRIV_CHAN_LIMIT },
104   { "deop_lchan", TPRIV_DEOP_LCHAN },
105   { "die", TPRIV_DIE },
106   { "display", TPRIV_DISPLAY },
107   { "file", TFILE },
108   { "force_local_opmode", TPRIV_FORCE_LOCAL_OPMODE },
109   { "force_opmode", TPRIV_FORCE_OPMODE },
110   { "gline", TPRIV_GLINE },
111   { "kb", KBYTES },
112   { "kilobytes", KBYTES },
113   { "local_badchan", TPRIV_LOCAL_BADCHAN },
114   { "local_gline", TPRIV_LOCAL_GLINE },
115   { "local_jupe", TPRIV_LOCAL_JUPE },
116   { "local_kill", TPRIV_LOCAL_KILL },
117   { "mb", MBYTES },
118   { "megabytes", MBYTES },
119   { "mode_lchan", TPRIV_MODE_LCHAN },
120   { "gb", GBYTES },
121   { "gigabytes", GBYTES },
122   { "operator", OPER },
123   { "opmode", TPRIV_OPMODE },
124   { "password", PASS },
125   { "propagate", TPRIV_PROPAGATE },
126   { "realname", REAL },
127   { "rehash", TPRIV_REHASH },
128   { "restart", TPRIV_RESTART },
129   { "see_chan", TPRIV_SEE_CHAN },
130   { "see_opers", TPRIV_SEE_OPERS },
131   { "set", TPRIV_SET },
132   { "show_all_invis", TPRIV_SHOW_ALL_INVIS },
133   { "show_invis", TPRIV_SHOW_INVIS },
134   { "tb", TBYTES },
135   { "terabytes", TBYTES },
136   { "unlimit_query", TPRIV_UNLIMIT_QUERY },
137   { "walk_lchan", TPRIV_WALK_LCHAN },
138   { "wide_gline", TPRIV_WIDE_GLINE },
139   { "whox", TPRIV_WHOX },
140   { NULL, 0 }
141 };
142 static int ntokens;
143
144 static int
145 token_compare(const void *pa, const void *pb)
146 {
147   const struct lexer_token *ta = pa;
148   const struct lexer_token *tb = pb;
149   unsigned int ii = 0;
150   int res;
151   while (ta->string[ii] && (ToLower(ta->string[ii]) == ToLower(tb->string[ii])))
152     ii++;
153   res = ToLower(tb->string[ii]) - ToLower(ta->string[ii]);
154   return res;
155 }
156
157 static void
158 init_ntokens(void)
159 {
160   for (ntokens = 0; tokens[ntokens].string; ++ntokens) ;
161   qsort(tokens, ntokens, sizeof(tokens[0]), token_compare);
162 }
163
164 static int
165 find_token(char *token)
166 {
167   struct lexer_token *tok;
168   if (!ntokens)
169     init_ntokens();
170   tok = bsearch(&token, tokens, ntokens, sizeof(tokens[0]), token_compare);
171   return tok ? tok->value : 0;
172 }
173
174 void
175 init_lexer(void)
176 {
177   yyin = fopen(configfile, "r");
178   if (yyin == NULL)
179   {
180 #ifdef YY_FATAL_ERROR
181     YY_FATAL_ERROR("Could not open the configuration file.");
182 #else
183     fprintf(stderr, "Could not open the configuration file.");
184 #endif
185   }
186 #ifdef YY_NEW_FILE
187   YY_NEW_FILE;
188 #endif
189   lineno = 1;
190 }
191
192 %}
193
194 WHITE [ \t\r]+
195 SHCOMMENT #[^\n]*
196 NUMBER [0-9]+
197 QSTRING \"[^"\n]+[\"\n]
198 %%
199
200 {QSTRING} {yytext[yyleng-1] = 0; yylval.text = yytext+1; return QSTRING;}
201 {NUMBER} {yylval.num = strtoul(yytext, NULL, 10); return NUMBER;}
202 {WHITE} ;
203 {SHCOMMENT} ;
204
205 [a-zA-Z_]+ { int res = find_token(yytext); if (res) return res; else REJECT; }
206 \n lineno++;
207 . return yytext[0];