]>
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
]);
79 * This installs a specific command in a tree.
81 * This function builds the Command structure in addition to
82 * installing it in the tree
85 Command
*addcommandtotree(CommandTree
*ct
, const char *cmdname
, int level
, int maxparams
, CommandHandler handler
) {
89 /* Generate the struct.. */
90 nc
=(void *)malloc(sizeof(Command
));
91 nc
->command
=getsstring(cmdname
, MAX_COMMAND_LEN
);
93 nc
->maxparams
=maxparams
;
97 /* Sanity check the string */
98 for (i
=0;i
<nc
->command
->length
;i
++) {
99 nc
->command
->content
[i
]=toupper(nc
->command
->content
[i
]);
100 if (nc
->command
->content
[i
]<'A' || nc
->command
->content
[i
]>'Z') {
101 /* Someone tried to register an invalid command name */
102 freesstring(nc
->command
);
108 if ((c
=findcommandintree(ct
,cmdname
,1))!=NULL
) {
109 /* Found something already. Append our entry to the end */
110 while (c
->next
!=NULL
)
111 c
=(Command
*)c
->next
;
112 c
->next
=(struct Command
*)nc
;
113 } else if (insertcommand(nc
,ct
,0)) {
114 /* Erk, that didn't work.. */
115 freesstring(nc
->command
);
124 * insertcommand: internal recursive function to do actual command insertion
127 int insertcommand(Command
*c
, CommandTree
*ct
, int depth
) {
128 int nextcharindex
=c
->command
->content
[depth
]-'A';
130 if ((c
->command
->length
==depth
) || (countcommandtree(ct
)==0)) {
133 /* There is already another command at this level */
134 if(ct
->final
[0]=='\0') {
135 /* It's a conflict with us, shouldn't happen */
138 oldcharindex
=ct
->final
[0]-'A';
139 if (ct
->next
[oldcharindex
] != NULL
) {
140 /* Shouldn't happen */
141 Error("parser",ERR_ERROR
,"Unexpected command subtree conflicts with final value");
143 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
145 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
148 ct
->final
=&(c
->command
->content
[depth
]);
151 if ((ct
->cmd
!=NULL
) && (ct
->final
[0]!='\0')) {
152 int oldcharindex
=ct
->final
[0]-'A';
153 /* Someone marked this node as final, we have to undo that since we're now here too */
154 if (ct
->next
[oldcharindex
] != NULL
) {
155 Error("parser",ERR_ERROR
,"Unexpected command subtree conflicts with final value");
156 /* Shouldn't happen */
158 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
160 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
165 if (ct
->next
[nextcharindex
]==NULL
) {
166 ct
->next
[nextcharindex
]=(struct CommandTree
*)newcommandtree();
168 return insertcommand(c
,(CommandTree
*)ct
->next
[nextcharindex
],depth
+1);
172 int deletecommandfromtree(CommandTree
*ct
, const char *cmdname
, CommandHandler handler
) {
176 tmpstr
=getsstring(cmdname
,MAX_COMMAND_LEN
);
178 /* Sanity check input string */
179 for (i
=0;i
<tmpstr
->length
;i
++) {
180 tmpstr
->content
[i
]=toupper(tmpstr
->content
[i
]);
181 if (tmpstr
->content
[i
]<'A' || tmpstr
->content
[i
]>'Z') {
182 /* Someone tried to delete an invalid command name */
187 i
=deletecommand(tmpstr
,ct
,0,handler
);
192 int deletecommand(sstring
*cmdname
, CommandTree
*ct
, int depth
, CommandHandler handler
) {
194 int nextcharindex
=(cmdname
->content
[depth
])-'A';
197 if (depth
==cmdname
->length
) {
198 /* Hit the end of the string.. the command should be in this node */
199 if ((ct
->cmd
==NULL
) ||
200 (ct
->cmd
->command
->length
!= cmdname
->length
) ||
201 (strncmp(ct
->cmd
->command
->content
,cmdname
->content
,cmdname
->length
))) {
204 for(ch
=&(ct
->cmd
);*ch
;ch
=(Command
**)&((*ch
)->next
)) {
205 if ((*ch
)->handler
==handler
) {
207 (*ch
)=(Command
*)((*ch
)->next
);
208 freesstring(c
->command
);
214 } else if ((ct
->final
) && (ct
->final
[0]==cmdname
->content
[depth
])) {
215 /* We have a potentially matching final string here, double check */
216 if ((ct
->cmd
->command
->length
!= cmdname
->length
) ||
217 (strncmp(ct
->cmd
->command
->content
,cmdname
->content
,cmdname
->length
))) {
220 /* Find the command in the potential chain and remove it */
221 for(ch
=&(ct
->cmd
);*ch
;ch
=(Command
**)&((*ch
)->next
)) {
222 if ((*ch
)->handler
==handler
) {
224 (*ch
)=(Command
*)((*ch
)->next
);
225 freesstring(c
->command
);
232 /* We're going to have to recurse.. */
233 if (ct
->next
[nextcharindex
]==NULL
) {
236 i
=deletecommand(cmdname
,(CommandTree
*)ct
->next
[nextcharindex
],depth
+1,handler
);
237 if (countcommandtree((CommandTree
*)ct
->next
[nextcharindex
])==0) {
238 free(ct
->next
[nextcharindex
]);
239 ct
->next
[nextcharindex
]=NULL
;
247 * findcommandintree: Takes a command string and returns the relevant
248 * structure, if found.
251 Command
*findcommandintree(CommandTree
*ct
, const char *command
, int strictcheck
) {
254 c
=findcommand(ct
,command
,0);
260 (ircd_strncmp(command
,c
->command
->content
,c
->command
->length
) ||
261 (c
->command
->length
!= strlen(command
))))
268 * findcommand: Internal recursive function to find a command
271 Command
*findcommand(CommandTree
*ct
, const char *command
, int depth
) {
272 int nextchar
=toupper(command
[depth
]);
274 /* If we've run out of string, we return whatever we find in this node,
275 * be it NULL or otherwise. */
276 if (nextchar
=='\0') {
280 if ((ct
->cmd
!=NULL
) && ct
->final
[0]==nextchar
) {
284 if (nextchar
<'A' || nextchar
>'Z') {
288 if (ct
->next
[nextchar
-'A']==NULL
) {
291 return findcommand((CommandTree
*)ct
->next
[nextchar
-'A'], command
, depth
+1);
296 * getcommandlist: Returns the contents of a CommandTree in a user-supplied Command * array
299 int getcommandlist(CommandTree
*ct
, Command
**commandlist
, int maxcommands
) {
307 commandlist
[count
++]=ct
->cmd
;
312 count
+=getcommandlist(ct
->next
[i
], &commandlist
[count
], maxcommands
-count
);