]>
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
]);
77 * addcommandhelptotree:
79 * This installs a specific command in a tree, with a help paramater.
81 * This function builds the Command structure in addition to
82 * installing it in the tree
85 Command
*addcommandhelptotree(CommandTree
*ct
, const char *cmdname
, int level
, int maxparams
, CommandHandler handler
, const char *help
) {
89 /* Generate the struct.. */
90 nc
=(void *)malloc(sizeof(Command
));
91 nc
->command
=getsstring(cmdname
, MAX_COMMAND_LEN
);
93 nc
->maxparams
=maxparams
;
99 nc
->help
=(char *)malloc(len
+1);
101 strncpy(nc
->help
, help
, len
);
102 nc
->help
[len
] = '\0';
108 /* Sanity check the string */
109 for (i
=0;i
<nc
->command
->length
;i
++) {
110 nc
->command
->content
[i
]=toupper(nc
->command
->content
[i
]);
111 if (nc
->command
->content
[i
]<'A' || nc
->command
->content
[i
]>'Z') {
112 /* Someone tried to register an invalid command name */
113 freesstring(nc
->command
);
121 if ((c
=findcommandintree(ct
,cmdname
,1))!=NULL
) {
122 /* Found something already. Append our entry to the end */
123 while (c
->next
!=NULL
)
124 c
=(Command
*)c
->next
;
125 c
->next
=(struct Command
*)nc
;
126 } else if (insertcommand(nc
,ct
,0)) {
127 /* Erk, that didn't work.. */
128 freesstring(nc
->command
);
139 * insertcommand: internal recursive function to do actual command insertion
142 int insertcommand(Command
*c
, CommandTree
*ct
, int depth
) {
143 int nextcharindex
=c
->command
->content
[depth
]-'A';
145 if ((c
->command
->length
==depth
) || (countcommandtree(ct
)==0)) {
148 /* There is already another command at this level */
149 if(ct
->final
[0]=='\0') {
150 /* It's a conflict with us, shouldn't happen */
153 oldcharindex
=ct
->final
[0]-'A';
154 if (ct
->next
[oldcharindex
] != NULL
) {
155 /* Shouldn't happen */
156 Error("parser",ERR_ERROR
,"Unexpected command subtree conflicts with final value");
158 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
160 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
163 /* Use a static NUL string rather than the allocated one if possible. */
164 if (c
->command
->length
> depth
)
165 ct
->final
=&(c
->command
->content
[depth
]);
170 if ((ct
->cmd
!=NULL
) && (ct
->final
[0]!='\0')) {
171 int oldcharindex
=ct
->final
[0]-'A';
172 /* Someone marked this node as final, we have to undo that since we're now here too */
173 if (ct
->next
[oldcharindex
] != NULL
) {
174 Error("parser",ERR_ERROR
,"Unexpected command subtree conflicts with final value");
175 /* Shouldn't happen */
177 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
179 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
184 if (ct
->next
[nextcharindex
]==NULL
) {
185 ct
->next
[nextcharindex
]=(struct CommandTree
*)newcommandtree();
187 return insertcommand(c
,(CommandTree
*)ct
->next
[nextcharindex
],depth
+1);
191 int deletecommandfromtree(CommandTree
*ct
, const char *cmdname
, CommandHandler handler
) {
195 tmpstr
=getsstring(cmdname
,MAX_COMMAND_LEN
);
197 /* Sanity check input string */
198 for (i
=0;i
<tmpstr
->length
;i
++) {
199 tmpstr
->content
[i
]=toupper(tmpstr
->content
[i
]);
200 if (tmpstr
->content
[i
]<'A' || tmpstr
->content
[i
]>'Z') {
201 /* Someone tried to delete an invalid command name */
206 i
=deletecommand(tmpstr
,ct
,0,handler
);
211 int deletecommand(sstring
*cmdname
, CommandTree
*ct
, int depth
, CommandHandler handler
) {
213 int nextcharindex
=(cmdname
->content
[depth
])-'A';
216 if (depth
==cmdname
->length
) {
217 /* Hit the end of the string.. the command should be in this node */
218 if ((ct
->cmd
==NULL
) ||
219 (ct
->cmd
->command
->length
!= cmdname
->length
) ||
220 (strncmp(ct
->cmd
->command
->content
,cmdname
->content
,cmdname
->length
))) {
223 for(ch
=&(ct
->cmd
);*ch
;ch
=(Command
**)&((*ch
)->next
)) {
224 if ((*ch
)->handler
==handler
) {
226 (*ch
)=(Command
*)((*ch
)->next
);
227 freesstring(c
->command
);
235 } else if ((ct
->final
) && (ct
->final
[0]==cmdname
->content
[depth
])) {
236 /* We have a potentially matching final string here, double check */
237 if ((ct
->cmd
->command
->length
!= cmdname
->length
) ||
238 (strncmp(ct
->cmd
->command
->content
,cmdname
->content
,cmdname
->length
))) {
241 /* Find the command in the potential chain and remove it */
242 for(ch
=&(ct
->cmd
);*ch
;ch
=(Command
**)&((*ch
)->next
)) {
243 if ((*ch
)->handler
==handler
) {
245 (*ch
)=(Command
*)((*ch
)->next
);
246 freesstring(c
->command
);
251 /* We need to regenerate the final pointer if needed;
252 * if ct->cmd is still pointing to a command it has the same name.
253 * Otherwise we should clear it.*/
255 ct
->final
=&(ct
->cmd
->command
->content
[depth
]);
264 /* We're going to have to recurse.. */
265 if (ct
->next
[nextcharindex
]==NULL
) {
268 i
=deletecommand(cmdname
,(CommandTree
*)ct
->next
[nextcharindex
],depth
+1,handler
);
269 if (countcommandtree((CommandTree
*)ct
->next
[nextcharindex
])==0) {
270 free(ct
->next
[nextcharindex
]);
271 ct
->next
[nextcharindex
]=NULL
;
279 * findcommandintree: Takes a command string and returns the relevant
280 * structure, if found.
283 Command
*findcommandintree(CommandTree
*ct
, const char *command
, int strictcheck
) {
286 c
=findcommand(ct
,command
,0);
292 (ircd_strncmp(command
,c
->command
->content
,c
->command
->length
) ||
293 (c
->command
->length
!= strlen(command
))))
300 * findcommand: Internal recursive function to find a command
303 Command
*findcommand(CommandTree
*ct
, const char *command
, int depth
) {
304 int nextchar
=toupper(command
[depth
]);
306 /* If we've run out of string, we return whatever we find in this node,
307 * be it NULL or otherwise. */
308 if (nextchar
=='\0') {
312 if ((ct
->cmd
!=NULL
) && ct
->final
[0]==nextchar
) {
316 if (nextchar
<'A' || nextchar
>'Z') {
320 if (ct
->next
[nextchar
-'A']==NULL
) {
323 return findcommand((CommandTree
*)ct
->next
[nextchar
-'A'], command
, depth
+1);
328 * getcommandlist: Returns the contents of a CommandTree in a user-supplied Command * array
331 int getcommandlist(CommandTree
*ct
, Command
**commandlist
, int maxcommands
) {
339 commandlist
[count
++]=ct
->cmd
;
344 count
+=getcommandlist(ct
->next
[i
], &commandlist
[count
], maxcommands
-count
);
351 /* Returns the command name given a handler */
352 sstring
*getcommandname(CommandTree
*ct
, CommandHandler handler
) {
356 if(ct
->cmd
&& ct
->cmd
->handler
== handler
) {
357 return ct
->cmd
->command
;
362 s
=getcommandname(ct
->next
[i
], handler
);