]>
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 nc
->help
=getsstring(help
, 512);
132 if ((c
=findcommandintree(ct
,cmdname
,1))!=NULL
) {
133 /* Found something already. Append our entry to the end */
134 while (c
->next
!=NULL
)
135 c
=(Command
*)c
->next
;
136 c
->next
=(struct Command
*)nc
;
137 } else if (insertcommand(nc
,ct
,0)) {
138 /* Erk, that didn't work.. */
139 freesstring(nc
->command
);
140 freesstring(nc
->help
);
149 * insertcommand: internal recursive function to do actual command insertion
152 int insertcommand(Command
*c
, CommandTree
*ct
, int depth
) {
153 int nextcharindex
=c
->command
->content
[depth
]-'A';
155 if ((c
->command
->length
==depth
) || (countcommandtree(ct
)==0)) {
158 /* There is already another command at this level */
159 if(ct
->final
[0]=='\0') {
160 /* It's a conflict with us, shouldn't happen */
163 oldcharindex
=ct
->final
[0]-'A';
164 if (ct
->next
[oldcharindex
] != NULL
) {
165 /* Shouldn't happen */
166 Error("parser",ERR_ERROR
,"Unexpected command subtree conflicts with final value");
168 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
170 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
173 /* Use a static NUL string rather than the allocated one if possible. */
174 if (c
->command
->length
> depth
)
175 ct
->final
=&(c
->command
->content
[depth
]);
180 if ((ct
->cmd
!=NULL
) && (ct
->final
[0]!='\0')) {
181 int oldcharindex
=ct
->final
[0]-'A';
182 /* Someone marked this node as final, we have to undo that since we're now here too */
183 if (ct
->next
[oldcharindex
] != NULL
) {
184 Error("parser",ERR_ERROR
,"Unexpected command subtree conflicts with final value");
185 /* Shouldn't happen */
187 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
189 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
194 if (ct
->next
[nextcharindex
]==NULL
) {
195 ct
->next
[nextcharindex
]=(struct CommandTree
*)newcommandtree();
197 return insertcommand(c
,(CommandTree
*)ct
->next
[nextcharindex
],depth
+1);
201 int deletecommandfromtree(CommandTree
*ct
, const char *cmdname
, CommandHandler handler
) {
203 char cmdbuf
[MAX_COMMAND_LEN
+1];
206 if (sanitisecommandname(cmdname
, cmdbuf
))
209 tmpstr
=getsstring(cmdbuf
, MAX_COMMAND_LEN
);
210 i
=deletecommand(tmpstr
,ct
,0,handler
);
216 int deletecommand(sstring
*cmdname
, CommandTree
*ct
, int depth
, CommandHandler handler
) {
218 int nextcharindex
=(cmdname
->content
[depth
])-'A';
221 if (depth
==cmdname
->length
) {
222 /* Hit the end of the string.. the command should be in this node */
223 if ((ct
->cmd
==NULL
) ||
224 (ct
->cmd
->command
->length
!= cmdname
->length
) ||
225 (strncmp(ct
->cmd
->command
->content
,cmdname
->content
,cmdname
->length
))) {
228 for(ch
=&(ct
->cmd
);*ch
;ch
=(Command
**)&((*ch
)->next
)) {
229 if ((*ch
)->handler
==handler
) {
231 (*ch
)=(Command
*)((*ch
)->next
);
232 freesstring(c
->command
);
233 freesstring(c
->help
);
239 } else if ((ct
->final
) && (ct
->final
[0]==cmdname
->content
[depth
])) {
240 /* We have a potentially matching final string here, double check */
241 if ((ct
->cmd
->command
->length
!= cmdname
->length
) ||
242 (strncmp(ct
->cmd
->command
->content
,cmdname
->content
,cmdname
->length
))) {
245 /* Find the command in the potential chain and remove it */
246 for(ch
=&(ct
->cmd
);*ch
;ch
=(Command
**)&((*ch
)->next
)) {
247 if ((*ch
)->handler
==handler
) {
249 (*ch
)=(Command
*)((*ch
)->next
);
250 freesstring(c
->command
);
251 freesstring(c
->help
);
254 /* We need to regenerate the final pointer if needed;
255 * if ct->cmd is still pointing to a command it has the same name.
256 * Otherwise we should clear it.*/
258 ct
->final
=&(ct
->cmd
->command
->content
[depth
]);
267 /* We're going to have to recurse.. */
268 if (ct
->next
[nextcharindex
]==NULL
) {
271 i
=deletecommand(cmdname
,(CommandTree
*)ct
->next
[nextcharindex
],depth
+1,handler
);
272 if (countcommandtree((CommandTree
*)ct
->next
[nextcharindex
])==0) {
273 free(ct
->next
[nextcharindex
]);
274 ct
->next
[nextcharindex
]=NULL
;
282 * findcommandintree: Takes a command string and returns the relevant
283 * structure, if found.
286 Command
*findcommandintree(CommandTree
*ct
, const char *command
, int strictcheck
) {
289 c
=findcommand(ct
,command
,0);
295 (ircd_strncmp(command
,c
->command
->content
,c
->command
->length
) ||
296 (c
->command
->length
!= strlen(command
))))
303 * findcommand: Internal recursive function to find a command
306 Command
*findcommand(CommandTree
*ct
, const char *command
, int depth
) {
307 int nextchar
=toupper(command
[depth
]);
309 /* If we've run out of string, we return whatever we find in this node,
310 * be it NULL or otherwise. */
311 if (nextchar
=='\0') {
315 if ((ct
->cmd
!=NULL
) && ct
->final
[0]==nextchar
) {
319 if (nextchar
<'A' || nextchar
>'Z') {
323 if (ct
->next
[nextchar
-'A']==NULL
) {
326 return findcommand((CommandTree
*)ct
->next
[nextchar
-'A'], command
, depth
+1);
331 * getcommandlist: Returns the contents of a CommandTree in a user-supplied Command * array
334 int getcommandlist(CommandTree
*ct
, Command
**commandlist
, int maxcommands
) {
342 commandlist
[count
++]=ct
->cmd
;
347 count
+=getcommandlist(ct
->next
[i
], &commandlist
[count
], maxcommands
-count
);
354 /* Returns the command name given a handler */
355 sstring
*getcommandname(CommandTree
*ct
, CommandHandler handler
) {
359 if(ct
->cmd
&& ct
->cmd
->handler
== handler
) {
360 return ct
->cmd
->command
;
365 s
=getcommandname(ct
->next
[i
], handler
);