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