2 * Copyright (C) 2004-2006
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: gensnmpdef.c 383 2006-05-30 07:40:49Z brandt_h $
31 #include <sys/queue.h>
42 static const char usgtxt[] =
43 "Usage: gensnmpdef [-hEe] [-c <cut>] MIB [MIB ...]\n"
45 " -c specify the number of initial sub-oids to cut from the oids\n"
46 " -E extract named enum types. Print a typedef for all enums defined\n"
47 " in syntax clauses of normal objects. Suppress normal output.\n"
48 " -e extract unnamed enum types. Print a typedef for all enums defined\n"
49 " as textual conventions. Suppress normal output.\n"
50 " -h print this help\n"
51 "MIBs are searched according to the libsmi(3) search rules and can\n"
52 "be specified either by path or module name\n";
54 static SmiNode *last_node;
59 SLIST_ENTRY(tdef) link;
62 static SLIST_HEAD(, tdef) tdefs = SLIST_HEAD_INITIALIZER(tdef);
63 static int do_typedef = 0;
65 static void print_node(SmiNode *n, u_int level);
78 printf("%*s", (level - cut) * 2, "");
82 print_name(SmiNode *n)
86 for (p = n->name; *p != '\0'; p++) {
95 close_node(u_int n, u_int level)
107 open_node(const SmiNode *n, u_int level, SmiNode **last)
113 for (i = 0; i < (*last)->oidlen - 1; i++) {
114 if (i >= n->oidlen) {
115 level = close_node((*last)->oidlen -
119 if ((*last)->oid[i] != n->oid[i])
122 if (i < (*last)->oidlen - 1)
123 level = close_node((*last)->oidlen - 1 - i,
127 while (level < n->oidlen - 1) {
130 printf("(%u", n->oid[level]);
131 n1 = smiGetNodeByOID(level + 1, n->oid);
141 static const char *const type_names[] = {
142 [SMI_BASETYPE_UNKNOWN] = "UNKNOWN_TYPE",
143 [SMI_BASETYPE_INTEGER32] = "INTEGER",
144 [SMI_BASETYPE_OCTETSTRING] = "OCTETSTRING",
145 [SMI_BASETYPE_OBJECTIDENTIFIER] = "OID",
146 [SMI_BASETYPE_UNSIGNED32] = "UNSIGNED32",
147 [SMI_BASETYPE_INTEGER64] = "INTEGER64",
148 [SMI_BASETYPE_UNSIGNED64] = "UNSIGNED64",
149 [SMI_BASETYPE_FLOAT32] = "FLOAT32",
150 [SMI_BASETYPE_FLOAT64] = "FLOAT64",
151 [SMI_BASETYPE_FLOAT128] = "FLOAT128",
152 [SMI_BASETYPE_ENUM] = "ENUM",
153 [SMI_BASETYPE_BITS] = "BITS",
156 static const char *const type_map[] = {
159 "TimeTicks", "TIMETICKS",
160 "Counter32", "COUNTER",
161 "Counter", "COUNTER",
162 "Counter64", "COUNTER64",
163 "Integer32", "INTEGER32",
164 "IpAddress", "IPADDRESS",
169 print_enum(SmiType *t)
171 SmiNamedNumber *nnum;
174 for (nnum = smiGetFirstNamedNumber(t); nnum != NULL;
175 nnum = smiGetNextNamedNumber(nnum))
176 printf(" %ld %s", nnum->value.value.integer32, nnum->name);
181 print_type(SmiNode *n)
186 type = smiGetNodeType(n);
187 assert(type != NULL);
189 if (type->name != NULL) {
190 for (m = 0; type_map[m] != NULL; m += 2)
191 if (strcmp(type_map[m], type->name) == 0) {
192 printf("%s", type_map[m + 1]);
196 printf("%s", type_names[type->basetype]);
198 if (type->basetype == SMI_BASETYPE_ENUM ||
199 type->basetype == SMI_BASETYPE_BITS)
202 else if (type->basetype == SMI_BASETYPE_OCTETSTRING &&
204 printf(" | %s", type->name);
208 print_access(SmiAccess a)
210 if (a == SMI_ACCESS_READ_ONLY)
212 else if (a == SMI_ACCESS_READ_WRITE)
217 print_scalar(SmiNode *n, u_int level)
221 assert (n->nodekind == SMI_NODEKIND_SCALAR);
226 printf("(%u ", n->oid[level]);
231 /* generate the operation from the parent node name */
232 p = smiGetParentNode(n);
233 printf(" op_%s", p->name);
235 print_access(n->access);
241 print_notification(SmiNode *n, u_int level)
244 assert (n->nodekind == SMI_NODEKIND_NOTIFICATION);
249 printf("(%u ", n->oid[level]);
253 printf(" op_%s)\n", n->name);
257 print_col(SmiNode *n, u_int level)
259 assert (n->nodekind == SMI_NODEKIND_COLUMN);
264 printf("(%u ", n->oid[level]);
268 print_access(n->access);
273 print_index(SmiNode *row)
277 e = smiGetFirstElement(row);
280 print_type(smiGetElementNode(e));
281 e = smiGetNextElement(e);
286 print_table(SmiNode *n, u_int level)
288 SmiNode *row, *col, *rel;
290 assert (n->nodekind == SMI_NODEKIND_TABLE);
295 printf("(%u ", n->oid[level]);
299 row = smiGetFirstChildNode(n);
300 if (row->nodekind != SMI_NODEKIND_ROW)
301 errx(1, "%s: kind %u, not row", __func__, row->nodekind);
306 printf("(%u ", row->oid[level + 1]);
311 rel = smiGetRelatedNode(row);
312 switch (row->indexkind) {
314 case SMI_INDEX_INDEX:
318 case SMI_INDEX_AUGMENT:
320 errx(1, "%s: cannot find augemented table", row->name);
325 errx(1, "%s: cannot handle index kind %u", row->name,
329 printf(" op_%s", n->name);
332 col = smiGetFirstChildNode(row);
333 while (col != NULL) {
334 print_col(col, level + 2);
335 col = smiGetNextChildNode(col);
345 print_it(SmiNode *n, u_int level)
347 switch (n->nodekind) {
349 case SMI_NODEKIND_NODE:
350 print_node(n, level);
353 case SMI_NODEKIND_SCALAR:
354 print_scalar(n, level);
357 case SMI_NODEKIND_TABLE:
358 print_table(n, level);
361 case SMI_NODEKIND_COMPLIANCE:
362 case SMI_NODEKIND_GROUP:
366 case SMI_NODEKIND_NOTIFICATION:
367 print_notification(n, level);
371 errx(1, "cannot handle %u nodes", n->nodekind);
376 print_node(SmiNode *n, u_int level)
378 assert (n->nodekind == SMI_NODEKIND_NODE);
383 printf("(%u ", n->oid[level]);
387 n = smiGetFirstChildNode(n);
389 print_it(n, level + 1);
390 n = smiGetNextChildNode(n);
397 save_typdef(char *name)
400 t = malloc(sizeof(struct tdef));
405 memset(t, 0 , sizeof(struct tdef));
407 SLIST_INSERT_HEAD(&tdefs, t, link);
415 while ((t = SLIST_FIRST(&tdefs)) != NULL) {
416 SLIST_REMOVE_HEAD(&tdefs, link);
422 print_enum_typedef(SmiType *t)
424 SmiNamedNumber *nnum;
426 for (nnum = smiGetFirstNamedNumber(t); nnum != NULL;
427 nnum = smiGetNextNamedNumber(nnum)) {
428 printf("\t%ld %s\n" , nnum->value.value.integer32, nnum->name);
433 print_stype(SmiNode *n)
436 struct tdef *t = NULL;
438 type = smiGetNodeType(n);
439 assert(type != NULL);
441 if (type->basetype == SMI_BASETYPE_ENUM) {
442 if (do_typedef == 'e' && type->name != NULL) {
443 SLIST_FOREACH(t, &tdefs, link) {
444 if (strcmp(t->name, type->name) == 0)
447 save_typdef(type->name);
448 printf("typedef %s ENUM (\n", type->name);
449 } else if (do_typedef == 'E' && type->name == NULL)
450 printf("typedef %sType ENUM (\n", n->name);
454 print_enum_typedef(type);
457 } else if (type->basetype == SMI_BASETYPE_BITS) {
458 if (do_typedef == 'e' && type->name != NULL) {
459 SLIST_FOREACH(t, &tdefs, link) {
460 if (strcmp(t->name, type->name) == 0)
463 save_typdef(type->name);
464 printf("typedef %s BITS (\n", type->name);
465 } else if (do_typedef == 'E' && type->name == NULL)
466 printf("typedef %sType BITS (\n", n->name);
470 print_enum_typedef(type);
476 print_typdefs(SmiNode *n)
481 n = smiGetFirstChildNode(n);
483 switch (n->nodekind) {
484 case SMI_NODEKIND_SCALAR:
485 case SMI_NODEKIND_COLUMN:
488 case SMI_NODEKIND_COMPLIANCE:
489 case SMI_NODEKIND_GROUP:
495 n = smiGetNextChildNode(n);
502 main(int argc, char *argv[])
515 while ((opt = getopt(argc, argv, "c:Eeh")) != -1)
520 u = strtol(optarg, &end, 10);
522 err(1, "argument to -c");
524 err(1, "%s: not a number", optarg);
526 err(1, "%s: out of range", optarg);
539 fprintf(stderr, usgtxt);
546 flags = smiGetFlags();
547 flags |= SMI_FLAG_ERRORS;
550 mods = malloc(sizeof(mods[0]) * argc);
554 for (opt = 0; opt < argc; opt++) {
555 if ((name = smiLoadModule(argv[opt])) == NULL)
556 err(1, "%s: cannot load", argv[opt]);
557 mods[opt] = smiGetModule(name);
561 for (opt = 0; opt < argc; opt++) {
562 n = smiGetFirstNode(mods[opt], SMI_NODEKIND_ANY);
564 if (do_typedef == 0) {
565 level = open_node(n, level, &last);
571 if (last_node == NULL ||
572 (n = smiGetNextNode(last_node, SMI_NODEKIND_ANY))
577 if (last != NULL && do_typedef == 0)
578 level = close_node(last->oidlen - 1, level - 1);
579 else if (do_typedef != 0)