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