]>
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
]);
51 freesstring(ct
->cmd
->command
);
61 * This returns the number of commands registered in
62 * a particular (sub-)tree. This will come in handy
63 * later for deleting commands etc.
66 int countcommandtree(CommandTree
*ct
) {
78 sum
+=countcommandtree((CommandTree
*)ct
->next
[i
]);
83 /* sanitisecommandname:
85 * Converts the given command name to uppercase and checks for bad chars.
87 * Returns 1 if bad chars were found
89 static int sanitisecommandname(const char *cmdname
, char *cmdbuf
) {
92 strncpy(cmdbuf
,cmdname
,MAX_COMMAND_LEN
);
93 cmdbuf
[MAX_COMMAND_LEN
-1]='\0';
97 /* Sanity check the string */
99 cmdbuf
[i
]=toupper(cmdbuf
[i
]);
100 if (cmdbuf
[i
]<'A' || cmdbuf
[i
]>'Z') {
101 /* Someone tried to register an invalid command name */
110 * addcommandhelptotree:
112 * This installs a specific command in a tree, with a help paramater.
114 * This function builds the Command structure in addition to
115 * installing it in the tree
118 Command
*addcommandhelptotree(CommandTree
*ct
, const char *cmdname
, int level
, int maxparams
, CommandHandler handler
, const char *help
) {
120 char cmdbuf
[MAX_COMMAND_LEN
];
122 if (sanitisecommandname(cmdname
, cmdbuf
))
125 /* Generate the struct.. */
126 nc
=(void *)malloc(sizeof(Command
));
127 nc
->command
=getsstring(cmdbuf
, MAX_COMMAND_LEN
);
129 nc
->maxparams
=maxparams
;
134 int len
=strlen(help
);
135 nc
->help
=(char *)malloc(len
+1);
137 strncpy(nc
->help
, help
, len
);
138 nc
->help
[len
] = '\0';
144 if ((c
=findcommandintree(ct
,cmdname
,1))!=NULL
) {
145 /* Found something already. Append our entry to the end */
146 while (c
->next
!=NULL
)
147 c
=(Command
*)c
->next
;
148 c
->next
=(struct Command
*)nc
;
149 } else if (insertcommand(nc
,ct
,0)) {
150 /* Erk, that didn't work.. */
151 freesstring(nc
->command
);
162 * insertcommand: internal recursive function to do actual command insertion
165 int insertcommand(Command
*c
, CommandTree
*ct
, int depth
) {
166 int nextcharindex
=c
->command
->content
[depth
]-'A';
168 if ((c
->command
->length
==depth
) || (countcommandtree(ct
)==0)) {
171 /* There is already another command at this level */
172 if(ct
->final
[0]=='\0') {
173 /* It's a conflict with us, shouldn't happen */
176 oldcharindex
=ct
->final
[0]-'A';
177 if (ct
->next
[oldcharindex
] != NULL
) {
178 /* Shouldn't happen */
179 Error("parser",ERR_ERROR
,"Unexpected command subtree conflicts with final value");
181 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
183 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
186 /* Use a static NUL string rather than the allocated one if possible. */
187 if (c
->command
->length
> depth
)
188 ct
->final
=&(c
->command
->content
[depth
]);
193 if ((ct
->cmd
!=NULL
) && (ct
->final
[0]!='\0')) {
194 int oldcharindex
=ct
->final
[0]-'A';
195 /* Someone marked this node as final, we have to undo that since we're now here too */
196 if (ct
->next
[oldcharindex
] != NULL
) {
197 Error("parser",ERR_ERROR
,"Unexpected command subtree conflicts with final value");
198 /* Shouldn't happen */
200 ct
->next
[oldcharindex
]=(struct CommandTree
*)newcommandtree();
202 insertcommand(ct
->cmd
,(CommandTree
*)ct
->next
[oldcharindex
],depth
+1);
207 if (ct
->next
[nextcharindex
]==NULL
) {
208 ct
->next
[nextcharindex
]=(struct CommandTree
*)newcommandtree();
210 return insertcommand(c
,(CommandTree
*)ct
->next
[nextcharindex
],depth
+1);
214 int deletecommandfromtree(CommandTree
*ct
, const char *cmdname
, CommandHandler handler
) {
216 char cmdbuf
[MAX_COMMAND_LEN
+1];
219 if (sanitisecommandname(cmdname
, cmdbuf
))
222 tmpstr
=getsstring(cmdbuf
, MAX_COMMAND_LEN
);
223 i
=deletecommand(tmpstr
,ct
,0,handler
);
229 int deletecommand(sstring
*cmdname
, CommandTree
*ct
, int depth
, CommandHandler handler
) {
231 int nextcharindex
=(cmdname
->content
[depth
])-'A';
234 if (depth
==cmdname
->length
) {
235 /* Hit the end of the string.. the command should be in this node */
236 if ((ct
->cmd
==NULL
) ||
237 (ct
->cmd
->command
->length
!= cmdname
->length
) ||
238 (strncmp(ct
->cmd
->command
->content
,cmdname
->content
,cmdname
->length
))) {
241 for(ch
=&(ct
->cmd
);*ch
;ch
=(Command
**)&((*ch
)->next
)) {
242 if ((*ch
)->handler
==handler
) {
244 (*ch
)=(Command
*)((*ch
)->next
);
245 freesstring(c
->command
);
253 } else if ((ct
->final
) && (ct
->final
[0]==cmdname
->content
[depth
])) {
254 /* We have a potentially matching final string here, double check */
255 if ((ct
->cmd
->command
->length
!= cmdname
->length
) ||
256 (strncmp(ct
->cmd
->command
->content
,cmdname
->content
,cmdname
->length
))) {
259 /* Find the command in the potential chain and remove it */
260 for(ch
=&(ct
->cmd
);*ch
;ch
=(Command
**)&((*ch
)->next
)) {
261 if ((*ch
)->handler
==handler
) {
263 (*ch
)=(Command
*)((*ch
)->next
);
264 freesstring(c
->command
);
269 /* We need to regenerate the final pointer if needed;
270 * if ct->cmd is still pointing to a command it has the same name.
271 * Otherwise we should clear it.*/
273 ct
->final
=&(ct
->cmd
->command
->content
[depth
]);
282 /* We're going to have to recurse.. */
283 if (ct
->next
[nextcharindex
]==NULL
) {
286 i
=deletecommand(cmdname
,(CommandTree
*)ct
->next
[nextcharindex
],depth
+1,handler
);
287 if (countcommandtree((CommandTree
*)ct
->next
[nextcharindex
])==0) {
288 free(ct
->next
[nextcharindex
]);
289 ct
->next
[nextcharindex
]=NULL
;
297 * findcommandintree: Takes a command string and returns the relevant
298 * structure, if found.
301 Command
*findcommandintree(CommandTree
*ct
, const char *command
, int strictcheck
) {
304 c
=findcommand(ct
,command
,0);
310 (ircd_strncmp(command
,c
->command
->content
,c
->command
->length
) ||
311 (c
->command
->length
!= strlen(command
))))
318 * findcommand: Internal recursive function to find a command
321 Command
*findcommand(CommandTree
*ct
, const char *command
, int depth
) {
322 int nextchar
=toupper(command
[depth
]);
324 /* If we've run out of string, we return whatever we find in this node,
325 * be it NULL or otherwise. */
326 if (nextchar
=='\0') {
330 if ((ct
->cmd
!=NULL
) && ct
->final
[0]==nextchar
) {
334 if (nextchar
<'A' || nextchar
>'Z') {
338 if (ct
->next
[nextchar
-'A']==NULL
) {
341 return findcommand((CommandTree
*)ct
->next
[nextchar
-'A'], command
, depth
+1);
346 * getcommandlist: Returns the contents of a CommandTree in a user-supplied Command * array
349 int getcommandlist(CommandTree
*ct
, Command
**commandlist
, int maxcommands
) {
357 commandlist
[count
++]=ct
->cmd
;
362 count
+=getcommandlist(ct
->next
[i
], &commandlist
[count
], maxcommands
-count
);
369 /* Returns the command name given a handler */
370 sstring
*getcommandname(CommandTree
*ct
, CommandHandler handler
) {
374 if(ct
->cmd
&& ct
->cmd
->handler
== handler
) {
375 return ct
->cmd
->command
;
380 s
=getcommandname(ct
->next
[i
], handler
);