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