]>
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
;
98 /* Sanity check the string */
99 for (i
=0;i
<nc
->command
->length
;i
++) {
100 nc
->command
->content
[i
]=toupper(nc
->command
->content
[i
]);
101 if (nc
->command
->content
[i
]<'A' || nc
->command
->content
[i
]>'Z') {
102 /* Someone tried to register an invalid command name */
103 freesstring(nc
->command
);
109 if ((c
=findcommandintree(ct
,cmdname
,1))!=NULL
) {
110 /* Found something already. Append our entry to the end */
111 while (c
->next
!=NULL
)
112 c
=(Command
*)c
->next
;
113 c
->next
=(struct Command
*)nc
;
114 } else if (insertcommand(nc
,ct
,0)) {
115 /* Erk, that didn't work.. */
116 freesstring(nc
->command
);
125 * insertcommand: internal recursive function to do actual command insertion
128 int insertcommand(Command
*c
, CommandTree
*ct
, int depth
) {
129 int nextcharindex
=c
->command
->content
[depth
]-'A';
131 if ((c
->command
->length
==depth
) || (countcommandtree(ct
)==0)) {
134 /* There is already another command at this level */
135 if(ct
->final
[0]=='\0') {
136 /* It's a conflict with us, shouldn't happen */
139 oldcharindex
=ct
->final
[0]-'A';
140 if (ct
->next
[oldcharindex
] != NULL
) {
141 /* Shouldn't happen */
142 Error("parser",ERR_ERROR
,"Unexpected command subtree conflicts with final value");
144 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
146 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
149 ct
->final
=&(c
->command
->content
[depth
]);
152 if ((ct
->cmd
!=NULL
) && (ct
->final
[0]!='\0')) {
153 int oldcharindex
=ct
->final
[0]-'A';
154 /* Someone marked this node as final, we have to undo that since we're now here too */
155 if (ct
->next
[oldcharindex
] != NULL
) {
156 Error("parser",ERR_ERROR
,"Unexpected command subtree conflicts with final value");
157 /* Shouldn't happen */
159 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
161 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
166 if (ct
->next
[nextcharindex
]==NULL
) {
167 ct
->next
[nextcharindex
]=(struct CommandTree
*)newcommandtree();
169 return insertcommand(c
,(CommandTree
*)ct
->next
[nextcharindex
],depth
+1);
173 int deletecommandfromtree(CommandTree
*ct
, const char *cmdname
, CommandHandler handler
) {
177 tmpstr
=getsstring(cmdname
,MAX_COMMAND_LEN
);
179 /* Sanity check input string */
180 for (i
=0;i
<tmpstr
->length
;i
++) {
181 tmpstr
->content
[i
]=toupper(tmpstr
->content
[i
]);
182 if (tmpstr
->content
[i
]<'A' || tmpstr
->content
[i
]>'Z') {
183 /* Someone tried to delete an invalid command name */
188 i
=deletecommand(tmpstr
,ct
,0,handler
);
193 int deletecommand(sstring
*cmdname
, CommandTree
*ct
, int depth
, CommandHandler handler
) {
195 int nextcharindex
=(cmdname
->content
[depth
])-'A';
198 if (depth
==cmdname
->length
) {
199 /* Hit the end of the string.. the command should be in this node */
200 if ((ct
->cmd
==NULL
) ||
201 (ct
->cmd
->command
->length
!= cmdname
->length
) ||
202 (strncmp(ct
->cmd
->command
->content
,cmdname
->content
,cmdname
->length
))) {
205 for(ch
=&(ct
->cmd
);*ch
;ch
=(Command
**)&((*ch
)->next
)) {
206 if ((*ch
)->handler
==handler
) {
208 (*ch
)=(Command
*)((*ch
)->next
);
209 freesstring(c
->command
);
215 } else if ((ct
->final
) && (ct
->final
[0]==cmdname
->content
[depth
])) {
216 /* We have a potentially matching final string here, double check */
217 if ((ct
->cmd
->command
->length
!= cmdname
->length
) ||
218 (strncmp(ct
->cmd
->command
->content
,cmdname
->content
,cmdname
->length
))) {
221 /* Find the command in the potential chain and remove it */
222 for(ch
=&(ct
->cmd
);*ch
;ch
=(Command
**)&((*ch
)->next
)) {
223 if ((*ch
)->handler
==handler
) {
225 (*ch
)=(Command
*)((*ch
)->next
);
226 freesstring(c
->command
);
233 /* We're going to have to recurse.. */
234 if (ct
->next
[nextcharindex
]==NULL
) {
237 i
=deletecommand(cmdname
,(CommandTree
*)ct
->next
[nextcharindex
],depth
+1,handler
);
238 if (countcommandtree((CommandTree
*)ct
->next
[nextcharindex
])==0) {
239 free(ct
->next
[nextcharindex
]);
240 ct
->next
[nextcharindex
]=NULL
;
248 * findcommandintree: Takes a command string and returns the relevant
249 * structure, if found.
252 Command
*findcommandintree(CommandTree
*ct
, const char *command
, int strictcheck
) {
255 c
=findcommand(ct
,command
,0);
261 (ircd_strncmp(command
,c
->command
->content
,c
->command
->length
) ||
262 (c
->command
->length
!= strlen(command
))))
269 * findcommand: Internal recursive function to find a command
272 Command
*findcommand(CommandTree
*ct
, const char *command
, int depth
) {
273 int nextchar
=toupper(command
[depth
]);
275 /* If we've run out of string, we return whatever we find in this node,
276 * be it NULL or otherwise. */
277 if (nextchar
=='\0') {
281 if ((ct
->cmd
!=NULL
) && ct
->final
[0]==nextchar
) {
285 if (nextchar
<'A' || nextchar
>'Z') {
289 if (ct
->next
[nextchar
-'A']==NULL
) {
292 return findcommand((CommandTree
*)ct
->next
[nextchar
-'A'], command
, depth
+1);
297 * getcommandlist: Returns the contents of a CommandTree in a user-supplied Command * array
300 int getcommandlist(CommandTree
*ct
, Command
**commandlist
, int maxcommands
) {
308 commandlist
[count
++]=ct
->cmd
;
313 count
+=getcommandlist(ct
->next
[i
], &commandlist
[count
], maxcommands
-count
);