]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | #include <assert.h> |
2 | #include <string.h> | |
3 | #include <stdlib.h> | |
4 | ||
c86edd1d Q |
5 | #include "helpmod.h" |
6 | #include "hcommand.h" | |
7 | #include "hgen.h" | |
8 | ||
9 | hcommand* hcommand_add(const char *str, hlevel lvl, hcommand_function func, const char *command_help) | |
10 | { | |
11 | hcommand *tmp, **ptr = &hcommands; | |
12 | ||
13 | assert(hcommand_get(str, lvl) == NULL); | |
14 | ||
04f3fbba | 15 | /* Find the position */ |
16 | for (;*ptr && (*ptr)->level < lvl;ptr = &(*ptr)->next); | |
17 | for (;*ptr && (*ptr)->level <= lvl && strcmp(str, (*ptr)->name->content) > 0;ptr = &(*ptr)->next); | |
c86edd1d Q |
18 | |
19 | tmp = *ptr; | |
20 | *ptr = (hcommand*)malloc(sizeof (hcommand)); | |
21 | ||
22 | (*ptr)->name = getsstring(str, strlen(str)); | |
23 | (*ptr)->help = getsstring(command_help, strlen(command_help)); | |
24 | (*ptr)->level = lvl; | |
25 | (*ptr)->function = func; | |
26 | (*ptr)->next = tmp; | |
27 | ||
28 | return *ptr; | |
29 | } | |
30 | ||
31 | int hcommand_del(const char *str) | |
32 | { | |
33 | hcommand **ptr = &hcommands; | |
34 | ||
35 | assert(hcommand_get(str, H_NONE) != NULL); | |
36 | ||
37 | for (;*ptr;ptr = &(*ptr)->next) | |
38 | if (!ci_strcmp(str, (*ptr)->name->content)) | |
39 | { | |
40 | hcommand *tmp = (*ptr)->next; | |
41 | freesstring((*ptr)->name); | |
42 | free(*ptr); | |
43 | *ptr = tmp; | |
44 | ||
45 | return 0; | |
46 | } | |
47 | return 0; | |
48 | } | |
49 | ||
50 | int hcommand_del_all(void) | |
51 | { | |
52 | while (hcommands) | |
53 | hcommand_del(hcommands->name->content); | |
54 | return 0; | |
55 | } | |
56 | ||
57 | ||
58 | hcommand* hcommand_get(const char *str, hlevel lvl) | |
59 | { | |
60 | hcommand *ptr = hcommands; | |
61 | ||
62 | for (;ptr && ptr->level <= lvl;ptr = ptr->next) | |
63 | if (!ci_strcmp(str, ptr->name->content)) | |
64 | return ptr; | |
65 | ||
66 | return NULL; | |
67 | } | |
68 | ||
69 | hcommand* hcommand_list(hlevel lvl) | |
70 | { | |
71 | static hcommand *position = NULL; | |
72 | hcommand *tmp; | |
73 | ||
74 | if (position == NULL || position->level > lvl) | |
75 | { | |
76 | position = hcommands; | |
77 | return NULL; | |
78 | } | |
79 | ||
80 | tmp = position; | |
81 | position = position->next; | |
82 | ||
83 | return tmp; | |
84 | } | |
85 | ||
86 | int hcommand_is_command(const char *str) | |
87 | { | |
88 | if (*str == '-' || *str == '?') | |
89 | return 1; | |
90 | if ((!strncmp(str, helpmodnick->nick, strlen(helpmodnick->nick)) && str[strlen(helpmodnick->nick)] == ' ')) | |
91 | return 1; | |
92 | return 0; | |
93 | } |