]> jfr.im git - irc/quakenet/newserv.git/blob - helpmod2/hterm.c
GLINES: fix null pointer deref in trustgline / trustungline
[irc/quakenet/newserv.git] / helpmod2 / hterm.c
1 #include <string.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 #include "hterm.h"
7
8 #include "hgen.h"
9
10 hterm *hterm_add(hterm** ptr, const char *name, const char *desc)
11 {
12 hterm *htrm;
13
14 if (ptr == NULL)
15 ptr = &hterms;
16
17 if (name == NULL || desc == NULL || hterm_get(*ptr, name))
18 return NULL;
19
20 for (;*ptr && strcmp(name, (*ptr)->name->content) > 0;ptr = &(*ptr)->next);
21
22 htrm = (hterm*)malloc(sizeof(hterm));
23 htrm->name = getsstring(name, strlen(name));
24 htrm->description = getsstring(desc, strlen(desc));
25 htrm->usage = 0;
26
27 htrm->next = *ptr;
28 *ptr = htrm;
29
30 return htrm;
31 }
32
33 hterm *hterm_get(hterm *source, const char *str)
34 {
35 hterm *ptr = source;
36
37 for (;ptr;ptr = ptr->next)
38 if (!ci_strcmp(ptr->name->content, str))
39 return ptr;
40 return NULL;
41 }
42
43 hterm *hterm_find(hterm *source, const char *str)
44 {
45 hterm *ptr;
46 char buffer[512];
47
48 sprintf(buffer, "*%s*", str);
49
50 for (ptr = source;ptr;ptr = ptr->next)
51 if (strregexp(ptr->name->content, buffer))
52 return ptr;
53 for (ptr = source;ptr;ptr = ptr->next)
54 if (strregexp(ptr->description->content, buffer))
55 return ptr;
56 return NULL;
57 }
58
59 hterm *hterm_get_and_find(hterm *source, const char *str)
60 {
61 /* search order: get source, get hterms, find source, find hterms */
62 hterm *ptr;
63 ptr = hterm_get(source, str);
64 if (ptr != NULL)
65 {
66 ptr->usage++;
67 return ptr;
68 }
69 ptr = hterm_get(hterms, str);
70 if (ptr != NULL)
71 {
72 ptr->usage++;
73 return ptr;
74 }
75 ptr = hterm_find(source, str);
76 if (ptr != NULL)
77 {
78 ptr->usage++;
79 return ptr;
80 }
81 ptr = hterm_find(hterms, str);
82 if (ptr != NULL)
83 {
84 ptr->usage++;
85 return ptr;
86 }
87 return NULL;
88 }
89
90 hterm *hterm_del(hterm** start, hterm *htrm)
91 {
92 hterm** ptr = start;
93
94 for (;*ptr;ptr = &(*ptr)->next)
95 if (*ptr == htrm)
96 {
97 hterm *tmp = (*ptr)->next;
98 freesstring((*ptr)->name);
99 freesstring((*ptr)->description);
100 free(*ptr);
101 *ptr = tmp;
102
103 return NULL;
104 }
105
106 return htrm;
107 }
108
109 void hterm_del_all(hterm **source)
110 {
111 if (source == NULL)
112 source = &hterms;
113
114 while (*source)
115 hterm_del(source, *source);
116 }
117
118 int hterm_count(hterm* ptr)
119 {
120 int i = 0;
121 for (;ptr;ptr = ptr->next)
122 i++;
123 return i;
124 }