]>
jfr.im git - irc/quakenet/newserv.git/blob - parser/parser.c
4 #include "../lib/sstring.h"
5 #include "../lib/irc_string.h"
6 #include "../core/error.h"
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
);
21 * This creates a new command tree.
23 * Uses malloc() as it's a pretty rare event.
26 CommandTree
*newcommandtree() {
29 nct
=(void *)malloc(sizeof(CommandTree
));
30 memset(nct
,0,sizeof(CommandTree
));
35 /* destroycommandtree:
37 * This frees a tree and all it's subtrees
40 void destroycommandtree(CommandTree
*ct
) {
45 destroycommandtree((CommandTree
*)ct
->next
[i
]);
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.
59 int countcommandtree(CommandTree
*ct
) {
71 sum
+=countcommandtree((CommandTree
*)ct
->next
[i
]);
76 /* sanitisecommandname:
78 * Converts the given command name to uppercase and checks for bad chars.
80 * Returns 1 if bad chars were found
82 static int sanitisecommandname(const char *cmdname
, char *cmdbuf
) {
85 strncpy(cmdbuf
,cmdname
,MAX_COMMAND_LEN
);
86 cmdbuf
[MAX_COMMAND_LEN
-1]='\0';
90 /* Sanity check the string */
92 cmdbuf
[i
]=toupper(cmdbuf
[i
]);
93 if (cmdbuf
[i
]<'A' || cmdbuf
[i
]>'Z') {
94 /* Someone tried to register an invalid command name */
103 * addcommandhelptotree:
105 * This installs a specific command in a tree, with a help paramater.
107 * This function builds the Command structure in addition to
108 * installing it in the tree
111 Command
*addcommandhelptotree(CommandTree
*ct
, const char *cmdname
, int level
, int maxparams
, CommandHandler handler
, const char *help
) {
113 char cmdbuf
[MAX_COMMAND_LEN
];
115 if (sanitisecommandname(cmdname
, cmdbuf
))
118 /* Generate the struct.. */
119 nc
=(void *)malloc(sizeof(Command
));
120 nc
->command
=getsstring(cmdbuf
, MAX_COMMAND_LEN
);
122 nc
->maxparams
=maxparams
;
127 int len
=strlen(help
);
128 nc
->help
=(char *)malloc(len
+1);
130 strncpy(nc
->help
, help
, len
);
131 nc
->help
[len
] = '\0';
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
);
155 * insertcommand: internal recursive function to do actual command insertion
158 int insertcommand(Command
*c
, CommandTree
*ct
, int depth
) {
159 int nextcharindex
=c
->command
->content
[depth
]-'A';
161 if ((c
->command
->length
==depth
) || (countcommandtree(ct
)==0)) {
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 */
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");
174 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
176 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
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
]);
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 */
193 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
195 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
200 if (ct
->next
[nextcharindex
]==NULL
) {
201 ct
->next
[nextcharindex
]=(struct CommandTree
*)newcommandtree();
203 return insertcommand(c
,(CommandTree
*)ct
->next
[nextcharindex
],depth
+1);
207 int deletecommandfromtree(CommandTree
*ct
, const char *cmdname
, CommandHandler handler
) {
209 char cmdbuf
[MAX_COMMAND_LEN
+1];
212 if (sanitisecommandname(cmdname
, cmdbuf
))
215 tmpstr
=getsstring(cmdbuf
, MAX_COMMAND_LEN
);
216 i
=deletecommand(tmpstr
,ct
,0,handler
);
222 int deletecommand(sstring
*cmdname
, CommandTree
*ct
, int depth
, CommandHandler handler
) {
224 int nextcharindex
=(cmdname
->content
[depth
])-'A';
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
))) {
234 for(ch
=&(ct
->cmd
);*ch
;ch
=(Command
**)&((*ch
)->next
)) {
235 if ((*ch
)->handler
==handler
) {
237 (*ch
)=(Command
*)((*ch
)->next
);
238 freesstring(c
->command
);
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
))) {
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
) {
256 (*ch
)=(Command
*)((*ch
)->next
);
257 freesstring(c
->command
);
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.*/
266 ct
->final
=&(ct
->cmd
->command
->content
[depth
]);
275 /* We're going to have to recurse.. */
276 if (ct
->next
[nextcharindex
]==NULL
) {
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
;
290 * findcommandintree: Takes a command string and returns the relevant
291 * structure, if found.
294 Command
*findcommandintree(CommandTree
*ct
, const char *command
, int strictcheck
) {
297 c
=findcommand(ct
,command
,0);
303 (ircd_strncmp(command
,c
->command
->content
,c
->command
->length
) ||
304 (c
->command
->length
!= strlen(command
))))
311 * findcommand: Internal recursive function to find a command
314 Command
*findcommand(CommandTree
*ct
, const char *command
, int depth
) {
315 int nextchar
=toupper(command
[depth
]);
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') {
323 if ((ct
->cmd
!=NULL
) && ct
->final
[0]==nextchar
) {
327 if (nextchar
<'A' || nextchar
>'Z') {
331 if (ct
->next
[nextchar
-'A']==NULL
) {
334 return findcommand((CommandTree
*)ct
->next
[nextchar
-'A'], command
, depth
+1);
339 * getcommandlist: Returns the contents of a CommandTree in a user-supplied Command * array
342 int getcommandlist(CommandTree
*ct
, Command
**commandlist
, int maxcommands
) {
350 commandlist
[count
++]=ct
->cmd
;
355 count
+=getcommandlist(ct
->next
[i
], &commandlist
[count
], maxcommands
-count
);
362 /* Returns the command name given a handler */
363 sstring
*getcommandname(CommandTree
*ct
, CommandHandler handler
) {
367 if(ct
->cmd
&& ct
->cmd
->handler
== handler
) {
368 return ct
->cmd
->command
;
373 s
=getcommandname(ct
->next
[i
], handler
);