Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / snmp_enum.c
1 #include <net-snmp/net-snmp-config.h>
2
3 #ifdef HAVE_STDLIB_H
4 #include <stdlib.h>
5 #endif
6 #if HAVE_STRING_H
7 #include <string.h>
8 #else
9 #include <strings.h>
10 #endif
11
12 #if HAVE_DMALLOC_H
13 #include <dmalloc.h>
14 #endif
15
16 #include <net-snmp/library/snmp_enum.h>
17
18 struct snmp_enum_list_str {
19     char           *name;
20     struct snmp_enum_list *list;
21     struct snmp_enum_list_str *next;
22 };
23
24 static struct snmp_enum_list ***snmp_enum_lists;
25 unsigned int    current_maj_num;
26 unsigned int    current_min_num;
27 struct snmp_enum_list_str *sliststorage;
28
29 int
30 init_snmp_enum(void)
31 {
32     int             i;
33
34     if (!snmp_enum_lists)
35         snmp_enum_lists = (struct snmp_enum_list ***)
36             malloc(sizeof(struct snmp_enum_list **) * SE_MAX_IDS);
37     if (!snmp_enum_lists)
38         return SE_NOMEM;
39     current_maj_num = SE_MAX_IDS;
40
41     for (i = 0; i < SE_MAX_IDS; i++) {
42         if (!snmp_enum_lists[i])
43             snmp_enum_lists[i] = (struct snmp_enum_list **)
44                 malloc(sizeof(struct snmp_enum_list *) * SE_MAX_SUBIDS);
45         if (!snmp_enum_lists[i])
46             return SE_NOMEM;
47     }
48     current_min_num = SE_MAX_SUBIDS;
49
50     if (!sliststorage)
51         sliststorage = NULL;
52     return SE_OK;
53 }
54
55 int
56 se_store_list(struct snmp_enum_list *new_list,
57               unsigned int major, unsigned int minor)
58 {
59     int             ret = SE_OK;
60
61     if (major > current_maj_num || minor > current_min_num) {
62         /*
63          * XXX: realloc 
64          */
65         return SE_NOMEM;
66     }
67
68
69     if (snmp_enum_lists[major][minor] != NULL)
70         ret = SE_ALREADY_THERE;
71
72     snmp_enum_lists[major][minor] = new_list;
73
74     return ret;
75 }
76
77 struct snmp_enum_list *
78 se_find_list(unsigned int major, unsigned int minor)
79 {
80     if (major > current_maj_num || minor > current_min_num)
81         return NULL;
82
83     return snmp_enum_lists[major][minor];
84 }
85
86 int
87 se_find_value_in_list(struct snmp_enum_list *list, char *label)
88 {
89     if (!list)
90         return SE_DNE;          /* XXX: um, no good solution here */
91     while (list) {
92         if (strcmp(list->label, label) == 0)
93             return (list->value);
94         list = list->next;
95     }
96
97     return SE_DNE;              /* XXX: um, no good solution here */
98 }
99
100 int
101 se_find_value(unsigned int major, unsigned int minor, char *label)
102 {
103     return se_find_value_in_list(se_find_list(major, minor), label);
104 }
105
106 char           *
107 se_find_label_in_list(struct snmp_enum_list *list, int value)
108 {
109     if (!list)
110         return NULL;
111     while (list) {
112         if (list->value == value)
113             return (list->label);
114         list = list->next;
115     }
116     return NULL;
117 }
118
119 char           *
120 se_find_label(unsigned int major, unsigned int minor, int value)
121 {
122     return se_find_label_in_list(se_find_list(major, minor), value);
123 }
124
125 int
126 se_add_pair_to_list(struct snmp_enum_list **list, char *label, int value)
127 {
128     struct snmp_enum_list *lastnode = NULL;
129
130     if (!list)
131         return SE_DNE;
132
133     while (*list) {
134         if ((*list)->value == value)
135             return (SE_ALREADY_THERE);
136         lastnode = (*list);
137         (*list) = (*list)->next;
138     }
139
140     if (lastnode) {
141         lastnode->next = (struct snmp_enum_list *)
142             malloc(sizeof(struct snmp_enum_list));
143         lastnode = lastnode->next;
144     } else {
145         (*list) = (struct snmp_enum_list *)
146             malloc(sizeof(struct snmp_enum_list));
147         lastnode = (*list);
148     }
149     if (!lastnode)
150         return (SE_NOMEM);
151     lastnode->label = label;
152     lastnode->value = value;
153     lastnode->next = NULL;
154     return (SE_OK);
155 }
156
157 int
158 se_add_pair(unsigned int major, unsigned int minor, char *label, int value)
159 {
160     struct snmp_enum_list *list = se_find_list(major, minor);
161     int             created = (list) ? 1 : 0;
162     int             ret = se_add_pair_to_list(&list, label, value);
163     if (!created)
164         se_store_list(list, major, minor);
165     return ret;
166 }
167
168 /*
169  * remember a list of enums based on a lookup name.
170  */
171 struct snmp_enum_list *
172 se_find_slist(const char *listname)
173 {
174     struct snmp_enum_list_str *sptr, *lastp = NULL;
175     if (!listname)
176         return NULL;
177
178     for (sptr = sliststorage;
179          sptr != NULL; lastp = sptr, sptr = sptr->next)
180         if (sptr->name && strcmp(sptr->name, listname) == 0)
181             return sptr->list;
182
183     if (lastp) {
184         lastp->next = (struct snmp_enum_list_str *)
185             malloc(sizeof(struct snmp_enum_list_str));
186         sptr = lastp->next;
187     } else {
188         sptr = (struct snmp_enum_list_str *)
189             malloc(sizeof(struct snmp_enum_list_str));
190         sliststorage = sptr;
191     }
192     sptr->list = NULL;
193     sptr->name = strdup(listname);
194     sptr->next = NULL;
195     return sptr->list;
196 }
197
198
199 char           *
200 se_find_label_in_slist(const char *listname, int value)
201 {
202     return (se_find_label_in_list(se_find_slist(listname), value));
203 }
204
205
206 int
207 se_find_value_in_slist(const char *listname, char *label)
208 {
209     return (se_find_value_in_list(se_find_slist(listname), label));
210 }
211
212 int
213 se_add_pair_to_slist(const char *listname, char *label, int value)
214 {
215     struct snmp_enum_list *list = se_find_slist(listname);
216     int             created = (list) ? 1 : 0;
217     int             ret = se_add_pair_to_list(&list, label, value);
218
219     if (!created) {
220         struct snmp_enum_list_str *sptr = (struct snmp_enum_list_str *)
221             malloc(sizeof(struct snmp_enum_list_str));
222         if (!sptr)
223             return SE_NOMEM;
224         sptr->next = sliststorage;
225         sptr->name = strdup(listname);
226         sptr->list = list;
227         sliststorage = sptr;
228     }
229     return ret;
230 }
231
232 #ifdef TESTING
233 main()
234 {
235     init_snmp_enum();
236     se_add_pair(1, 1, "hi", 1);
237     se_add_pair(1, 1, "there", 2);
238     printf("hi: %d\n", se_find_value(1, 1, "hi"));
239     printf("2: %s\n", se_find_label(1, 1, 2));
240
241     se_add_pair_to_slist("testing", "life, and everything", 42);
242     se_add_pair_to_slist("testing", "resturant at the end of the universe",
243                          2);
244
245     printf("life, and everything: %d\n",
246            se_find_value_in_slist("testing", "life, and everything"));
247     printf("2: %s\n", se_find_label_in_slist("testing", 2));
248 }
249 #endif                          /* TESTING */