]> jfr.im git - irc/quakenet/newserv.git/blob - parser/parser.c
76d9e3a4230266cd79bb918a062a2e05fe380821
[irc/quakenet/newserv.git] / parser / parser.c
1 /* parser.c */
2
3 #include "parser.h"
4 #include "../lib/sstring.h"
5 #include "../lib/irc_string.h"
6 #include "../core/error.h"
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdio.h>
12
13 /* Local functions */
14 int insertcommand(Command *c, CommandTree *ct, int depth);
15 int deletecommand(sstring *cmdname, CommandTree *ct, int depth, CommandHandler handler);
16 Command *findcommand(CommandTree *ct, const char *command, int depth);
17 int countcommandtree(CommandTree *ct);
18
19 /* newcommandtree:
20 *
21 * This creates a new command tree.
22 *
23 * Uses malloc() as it's a pretty rare event.
24 */
25
26 CommandTree *newcommandtree() {
27 CommandTree *nct;
28
29 nct=(void *)malloc(sizeof(CommandTree));
30 memset(nct,0,sizeof(CommandTree));
31
32 return nct;
33 }
34
35 /* destroycommandtree:
36 *
37 * This frees a tree and all it's subtrees
38 */
39
40 void destroycommandtree(CommandTree *ct) {
41 int i;
42
43 for (i=0;i<26;i++) {
44 if (ct->next[i]) {
45 destroycommandtree((CommandTree *)ct->next[i]);
46 }
47 }
48
49 free(ct);
50 }
51
52 /* countcommandtree:
53 *
54 * This returns the number of commands registered in
55 * a particular (sub-)tree. This will come in handy
56 * later for deleting commands etc.
57 */
58
59 int countcommandtree(CommandTree *ct) {
60 int i;
61 int sum;
62
63 if (ct->cmd!=NULL) {
64 sum=1;
65 } else {
66 sum=0;
67 }
68
69 for(i=0;i<26;i++)
70 if (ct->next[i])
71 sum+=countcommandtree((CommandTree *)ct->next[i]);
72
73 return sum;
74 }
75
76 /* sanitisecommandname:
77 *
78 * Converts the given command name to uppercase and checks for bad chars.
79 *
80 * Returns 1 if bad chars were found
81 */
82 static int sanitisecommandname(const char *cmdname, char *cmdbuf) {
83 int len,i;
84
85 strncpy(cmdbuf,cmdname,MAX_COMMAND_LEN);
86 cmdbuf[MAX_COMMAND_LEN-1]='\0';
87
88 len=strlen(cmdbuf);
89
90 /* Sanity check the string */
91 for (i=0;i<len;i++) {
92 cmdbuf[i]=toupper(cmdbuf[i]);
93 if (cmdbuf[i]<'A' || cmdbuf[i]>'Z') {
94 /* Someone tried to register an invalid command name */
95 return 1;
96 }
97 }
98
99 return 0;
100 }
101
102 /*
103 * addcommandhelptotree:
104 *
105 * This installs a specific command in a tree, with a help paramater.
106 *
107 * This function builds the Command structure in addition to
108 * installing it in the tree
109 */
110
111 Command *addcommandhelptotree(CommandTree *ct, const char *cmdname, int level, int maxparams, CommandHandler handler, const char *help) {
112 Command *nc, *c;
113 char cmdbuf[MAX_COMMAND_LEN];
114
115 if (sanitisecommandname(cmdname, cmdbuf))
116 return NULL;
117
118 /* Generate the struct.. */
119 nc=(void *)malloc(sizeof(Command));
120 nc->command=getsstring(cmdbuf, MAX_COMMAND_LEN);
121 nc->level=level;
122 nc->maxparams=maxparams;
123 nc->handler=handler;
124 nc->ext=NULL;
125 nc->next=NULL;
126 if (help) {
127 int len=strlen(help);
128 nc->help=(char *)malloc(len+1);
129 if(nc->help) {
130 strncpy(nc->help, help, len);
131 nc->help[len] = '\0';
132 }
133 } else {
134 nc->help=NULL;
135 }
136
137 if ((c=findcommandintree(ct,cmdname,1))!=NULL) {
138 /* Found something already. Append our entry to the end */
139 while (c->next!=NULL)
140 c=(Command *)c->next;
141 c->next=(struct Command *)nc;
142 } else if (insertcommand(nc,ct,0)) {
143 /* Erk, that didn't work.. */
144 freesstring(nc->command);
145 if(nc->help)
146 free(nc->help);
147 free(nc);
148 return NULL;
149 }
150
151 return nc;
152 }
153
154 /*
155 * insertcommand: internal recursive function to do actual command insertion
156 */
157
158 int insertcommand(Command *c, CommandTree *ct, int depth) {
159 int nextcharindex=c->command->content[depth]-'A';
160
161 if ((c->command->length==depth) || (countcommandtree(ct)==0)) {
162 if(ct->cmd!=NULL) {
163 int oldcharindex;
164 /* There is already another command at this level */
165 if(ct->final[0]=='\0') {
166 /* It's a conflict with us, shouldn't happen */
167 return 1;
168 }
169 oldcharindex=ct->final[0]-'A';
170 if (ct->next[oldcharindex] != NULL) {
171 /* Shouldn't happen */
172 Error("parser",ERR_ERROR,"Unexpected command subtree conflicts with final value");
173 } else {
174 ct->next[oldcharindex]=(struct CommandTree *)newcommandtree();
175 }
176 insertcommand(ct->cmd,(CommandTree *)ct->next[oldcharindex],depth+1);
177 }
178 ct->cmd=c;
179 /* Use a static NUL string rather than the allocated one if possible. */
180 if (c->command->length > depth)
181 ct->final=&(c->command->content[depth]);
182 else
183 ct->final="";
184 return 0;
185 } else {
186 if ((ct->cmd!=NULL) && (ct->final[0]!='\0')) {
187 int oldcharindex=ct->final[0]-'A';
188 /* Someone marked this node as final, we have to undo that since we're now here too */
189 if (ct->next[oldcharindex] != NULL) {
190 Error("parser",ERR_ERROR,"Unexpected command subtree conflicts with final value");
191 /* Shouldn't happen */
192 } else {
193 ct->next[oldcharindex]=(struct CommandTree *)newcommandtree();
194 }
195 insertcommand(ct->cmd,(CommandTree *)ct->next[oldcharindex],depth+1);
196 ct->cmd=NULL;
197 ct->final="";
198 }
199
200 if (ct->next[nextcharindex]==NULL) {
201 ct->next[nextcharindex]=(struct CommandTree *)newcommandtree();
202 }
203 return insertcommand(c,(CommandTree *)ct->next[nextcharindex],depth+1);
204 }
205 }
206
207 int deletecommandfromtree(CommandTree *ct, const char *cmdname, CommandHandler handler) {
208 int i;
209 char cmdbuf[MAX_COMMAND_LEN+1];
210 sstring *tmpstr;
211
212 if (sanitisecommandname(cmdname, cmdbuf))
213 return 1;
214
215 tmpstr=getsstring(cmdbuf, MAX_COMMAND_LEN);
216 i=deletecommand(tmpstr,ct,0,handler);
217 freesstring(tmpstr);
218
219 return i;
220 }
221
222 int deletecommand(sstring *cmdname, CommandTree *ct, int depth, CommandHandler handler) {
223 Command **ch, *c;
224 int nextcharindex=(cmdname->content[depth])-'A';
225 int i;
226
227 if (depth==cmdname->length) {
228 /* Hit the end of the string.. the command should be in this node */
229 if ((ct->cmd==NULL) ||
230 (ct->cmd->command->length != cmdname->length) ||
231 (strncmp(ct->cmd->command->content,cmdname->content,cmdname->length))) {
232 return 1;
233 }
234 for(ch=&(ct->cmd);*ch;ch=(Command **)&((*ch)->next)) {
235 if ((*ch)->handler==handler) {
236 c=*ch;
237 (*ch)=(Command *)((*ch)->next);
238 freesstring(c->command);
239 if(c->help)
240 free(c->help);
241 free(c);
242 return 0;
243 }
244 }
245 return 1;
246 } else if ((ct->final) && (ct->final[0]==cmdname->content[depth])) {
247 /* We have a potentially matching final string here, double check */
248 if ((ct->cmd->command->length != cmdname->length) ||
249 (strncmp(ct->cmd->command->content,cmdname->content,cmdname->length))) {
250 return 1;
251 }
252 /* Find the command in the potential chain and remove it */
253 for(ch=&(ct->cmd);*ch;ch=(Command **)&((*ch)->next)) {
254 if ((*ch)->handler==handler) {
255 c=*ch;
256 (*ch)=(Command *)((*ch)->next);
257 freesstring(c->command);
258 if(c->help)
259 free(c->help);
260 free(c);
261
262 /* We need to regenerate the final pointer if needed;
263 * if ct->cmd is still pointing to a command it has the same name.
264 * Otherwise we should clear it.*/
265 if (ct->cmd)
266 ct->final=&(ct->cmd->command->content[depth]);
267 else
268 ct->final="";
269
270 return 0;
271 }
272 }
273 return 1;
274 } else {
275 /* We're going to have to recurse.. */
276 if (ct->next[nextcharindex]==NULL) {
277 return 1;
278 } else {
279 i=deletecommand(cmdname,(CommandTree *)ct->next[nextcharindex],depth+1,handler);
280 if (countcommandtree((CommandTree *)ct->next[nextcharindex])==0) {
281 free(ct->next[nextcharindex]);
282 ct->next[nextcharindex]=NULL;
283 }
284 return i;
285 }
286 }
287 }
288
289 /*
290 * findcommandintree: Takes a command string and returns the relevant
291 * structure, if found.
292 */
293
294 Command *findcommandintree(CommandTree *ct, const char *command, int strictcheck) {
295 Command *c;
296
297 c=findcommand(ct,command,0);
298
299 if (c==NULL)
300 return NULL;
301
302 if (strictcheck &&
303 (ircd_strncmp(command,c->command->content,c->command->length) ||
304 (c->command->length != strlen(command))))
305 return NULL;
306
307 return c;
308 }
309
310 /*
311 * findcommand: Internal recursive function to find a command
312 */
313
314 Command *findcommand(CommandTree *ct, const char *command, int depth) {
315 int nextchar=toupper(command[depth]);
316
317 /* If we've run out of string, we return whatever we find in this node,
318 * be it NULL or otherwise. */
319 if (nextchar=='\0') {
320 return ct->cmd;
321 }
322
323 if ((ct->cmd!=NULL) && ct->final[0]==nextchar) {
324 return ct->cmd;
325 }
326
327 if (nextchar<'A' || nextchar>'Z') {
328 return NULL;
329 }
330
331 if (ct->next[nextchar-'A']==NULL) {
332 return NULL;
333 } else {
334 return findcommand((CommandTree *)ct->next[nextchar-'A'], command, depth+1);
335 }
336 }
337
338 /*
339 * getcommandlist: Returns the contents of a CommandTree in a user-supplied Command * array
340 */
341
342 int getcommandlist(CommandTree *ct, Command **commandlist, int maxcommands) {
343 int i=0,count=0;
344
345 if (maxcommands<0) {
346 return 0;
347 }
348
349 if (ct->cmd) {
350 commandlist[count++]=ct->cmd;
351 }
352
353 for (i=0;i<26;i++) {
354 if(ct->next[i]) {
355 count+=getcommandlist(ct->next[i], &commandlist[count], maxcommands-count);
356 }
357 }
358
359 return count;
360 }
361
362 /* Returns the command name given a handler */
363 sstring *getcommandname(CommandTree *ct, CommandHandler handler) {
364 int i;
365 sstring *s;
366
367 if(ct->cmd && ct->cmd->handler == handler) {
368 return ct->cmd->command;
369 }
370
371 for (i=0;i<26;i++) {
372 if(ct->next[i]) {
373 s=getcommandname(ct->next[i], handler);
374 if(s)
375 return s;
376 }
377 }
378
379 return NULL;
380 }