]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | /* |
2 | * parser.h | |
3 | * Definitions etc. for the common parser engine. | |
4 | * | |
5 | * The aim is to use this code for incoming server messages, | |
6 | * and also for any service modules which accept user commands | |
7 | * | |
8 | * This is heavily inspired by the ircu code. | |
9 | */ | |
10 | ||
11 | #ifndef __PARSER_H | |
12 | #define __PARSER_H | |
13 | ||
14 | #include "../lib/sstring.h" | |
15 | ||
16 | /* Maximum allowed command length */ | |
17 | /* Not actually used for any static buffers hence fairly large */ | |
18 | ||
19 | #define MAX_COMMAND_LEN 100 | |
20 | ||
21 | #define CMD_OK 0 | |
22 | #define CMD_ERROR 1 | |
23 | #define CMD_LAST 2 | |
38cee035 | 24 | #define CMD_USAGE 3 |
c86edd1d Q |
25 | |
26 | /* Generic CommandHandler type | |
27 | * void * = pointer to some relevant object to identify where the message came from | |
28 | * int = number of parameters | |
29 | * char ** = parameter vector | |
30 | */ | |
31 | ||
32 | typedef int (*CommandHandler)(void *, int, char**); | |
6ebc9d2f | 33 | typedef void (*DestroyExt)(void *); |
c86edd1d Q |
34 | |
35 | typedef struct Command { | |
36 | sstring *command; /* Name of the command/token/thing */ | |
37 | int level; /* "level" required to use the command/token/thing */ | |
38 | int maxparams; /* Maximum number of parameters for the command/token/thing */ | |
39 | CommandHandler handler; /* Function to deal with the message */ | |
40 | void *ext; /* Pointer to some arbitrary other data */ | |
6ebc9d2f | 41 | DestroyExt destroyext; /* Function to destroy ->ext on destroycommandtree (if necessary) */ |
c86edd1d Q |
42 | struct Command *next; /* Next handler chained onto this command */ |
43 | } Command; | |
44 | ||
45 | typedef struct CommandTree { | |
46 | Command *cmd; | |
47 | char *final; | |
48 | struct CommandTree *next[26]; | |
49 | } CommandTree; | |
50 | ||
51 | CommandTree *newcommandtree(); | |
52 | void destroycommandtree(CommandTree *ct); | |
6ebc9d2f | 53 | Command *addcommandexttotree(CommandTree *ct, const char *cmdname, int level, int maxparams, CommandHandler handler, void *ext); |
c86edd1d Q |
54 | int deletecommandfromtree(CommandTree *ct, const char *cmdname, CommandHandler handler); |
55 | Command *findcommandintree(CommandTree *ct, const char *cmdname, int strictcheck); | |
56 | int getcommandlist(CommandTree *ct, Command **commandlist, int maxcommands); | |
0a659cde | 57 | sstring *getcommandname(CommandTree *ct, CommandHandler handler); |
c86edd1d | 58 | |
6ebc9d2f | 59 | #define addcommandtotree(a, b, c, d, e) addcommandexttotree(a, b, c, d, e, NULL) |
38cee035 | 60 | |
c86edd1d | 61 | #endif |