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(tdefs);
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) {
129 n1 = smiGetNodeByOID(level + 1, n->oid);
133 printf("(%u", n->oid[level]);
143 static const char *const type_names[] = {
144 [SMI_BASETYPE_UNKNOWN] = "UNKNOWN_TYPE",
145 [SMI_BASETYPE_INTEGER32] = "INTEGER",
146 [SMI_BASETYPE_OCTETSTRING] = "OCTETSTRING",
147 [SMI_BASETYPE_OBJECTIDENTIFIER] = "OID",
148 [SMI_BASETYPE_UNSIGNED32] = "UNSIGNED32",
149 [SMI_BASETYPE_INTEGER64] = "INTEGER64",
150 [SMI_BASETYPE_UNSIGNED64] = "UNSIGNED64",
151 [SMI_BASETYPE_FLOAT32] = "FLOAT32",
152 [SMI_BASETYPE_FLOAT64] = "FLOAT64",
153 [SMI_BASETYPE_FLOAT128] = "FLOAT128",
154 [SMI_BASETYPE_ENUM] = "ENUM",
155 [SMI_BASETYPE_BITS] = "BITS",
158 static const char *const type_map[] = {
161 "TimeTicks", "TIMETICKS",
162 "Counter32", "COUNTER",
163 "Counter", "COUNTER",
164 "Counter64", "COUNTER64",
165 "Integer32", "INTEGER32",
166 "IpAddress", "IPADDRESS",
171 print_enum(SmiType *t)
173 SmiNamedNumber *nnum;
176 for (nnum = smiGetFirstNamedNumber(t); nnum != NULL;
177 nnum = smiGetNextNamedNumber(nnum))
178 printf(" %ld %s", nnum->value.value.integer32, nnum->name);
183 print_type(SmiNode *n)
188 type = smiGetNodeType(n);
189 assert(type != NULL);
191 if (type->name != NULL) {
192 for (m = 0; type_map[m] != NULL; m += 2)
193 if (strcmp(type_map[m], type->name) == 0) {
194 printf("%s", type_map[m + 1]);
198 printf("%s", type_names[type->basetype]);
200 if (type->basetype == SMI_BASETYPE_ENUM ||
201 type->basetype == SMI_BASETYPE_BITS)
204 else if (type->basetype == SMI_BASETYPE_OCTETSTRING &&
206 printf(" | %s", type->name);
210 print_access(SmiAccess a)
212 if (a == SMI_ACCESS_READ_ONLY)
214 else if (a == SMI_ACCESS_READ_WRITE)
219 print_scalar(SmiNode *n, u_int level)
223 assert (n->nodekind == SMI_NODEKIND_SCALAR);
228 printf("(%u ", n->oid[level]);
233 /* generate the operation from the parent node name */
234 p = smiGetParentNode(n);
235 printf(" op_%s", p->name);
237 print_access(n->access);
243 print_notification(SmiNode *n, u_int level)
246 assert (n->nodekind == SMI_NODEKIND_NOTIFICATION);
251 printf("(%u ", n->oid[level]);
255 printf(" op_%s)\n", n->name);
259 print_col(SmiNode *n, u_int level)
261 assert (n->nodekind == SMI_NODEKIND_COLUMN);
266 printf("(%u ", n->oid[level]);
270 print_access(n->access);
275 print_index(SmiNode *row)
279 e = smiGetFirstElement(row);
282 print_type(smiGetElementNode(e));
283 e = smiGetNextElement(e);
288 print_table(SmiNode *n, u_int level)
290 SmiNode *row, *col, *rel;
292 assert (n->nodekind == SMI_NODEKIND_TABLE);
297 printf("(%u ", n->oid[level]);
301 row = smiGetFirstChildNode(n);
302 if (row->nodekind != SMI_NODEKIND_ROW)
303 errx(1, "%s: kind %u, not row", __func__, row->nodekind);
308 printf("(%u ", row->oid[level + 1]);
313 rel = smiGetRelatedNode(row);
314 switch (row->indexkind) {
316 case SMI_INDEX_INDEX:
320 case SMI_INDEX_AUGMENT:
322 errx(1, "%s: cannot find augemented table", row->name);
327 errx(1, "%s: cannot handle index kind %u", row->name,
331 printf(" op_%s", n->name);
334 col = smiGetFirstChildNode(row);
335 while (col != NULL) {
336 print_col(col, level + 2);
337 col = smiGetNextChildNode(col);
347 print_it(SmiNode *n, u_int level)
349 switch (n->nodekind) {
351 case SMI_NODEKIND_NODE:
352 print_node(n, level);
355 case SMI_NODEKIND_SCALAR:
356 print_scalar(n, level);
359 case SMI_NODEKIND_TABLE:
360 print_table(n, level);
363 case SMI_NODEKIND_COMPLIANCE:
364 case SMI_NODEKIND_GROUP:
368 case SMI_NODEKIND_NOTIFICATION:
369 print_notification(n, level);
373 errx(1, "cannot handle %u nodes", n->nodekind);
378 print_node(SmiNode *n, u_int level)
380 assert (n->nodekind == SMI_NODEKIND_NODE);
385 printf("(%u ", n->oid[level]);
389 n = smiGetFirstChildNode(n);
391 print_it(n, level + 1);
392 n = smiGetNextChildNode(n);
399 save_typdef(char *name)
403 t = calloc(1, sizeof(struct tdef));
408 SLIST_INSERT_HEAD(&tdefs, t, link);
416 while ((t = SLIST_FIRST(&tdefs)) != NULL) {
417 SLIST_REMOVE_HEAD(&tdefs, link);
423 print_enum_typedef(SmiType *t)
425 SmiNamedNumber *nnum;
427 for (nnum = smiGetFirstNamedNumber(t); nnum != NULL;
428 nnum = smiGetNextNamedNumber(nnum)) {
429 printf("\t%ld %s\n" , nnum->value.value.integer32, nnum->name);
434 print_stype(SmiNode *n)
437 struct tdef *t = NULL;
439 type = smiGetNodeType(n);
440 assert(type != NULL);
442 if (type->basetype == SMI_BASETYPE_ENUM) {
443 if (do_typedef == 'e' && type->name != NULL) {
444 SLIST_FOREACH(t, &tdefs, link) {
445 if (strcmp(t->name, type->name) == 0)
448 save_typdef(type->name);
449 printf("typedef %s ENUM (\n", type->name);
450 } else if (do_typedef == 'E' && type->name == NULL)
451 printf("typedef %sType ENUM (\n", n->name);
455 print_enum_typedef(type);
458 } else if (type->basetype == SMI_BASETYPE_BITS) {
459 if (do_typedef == 'e' && type->name != NULL) {
460 SLIST_FOREACH(t, &tdefs, link) {
461 if (strcmp(t->name, type->name) == 0)
464 save_typdef(type->name);
465 printf("typedef %s BITS (\n", type->name);
466 } else if (do_typedef == 'E' && type->name == NULL)
467 printf("typedef %sType BITS (\n", n->name);
471 print_enum_typedef(type);
477 print_typdefs(SmiNode *n)
482 n = smiGetFirstChildNode(n);
484 switch (n->nodekind) {
485 case SMI_NODEKIND_SCALAR:
486 case SMI_NODEKIND_COLUMN:
489 case SMI_NODEKIND_COMPLIANCE:
490 case SMI_NODEKIND_GROUP:
496 n = smiGetNextChildNode(n);
503 main(int argc, char *argv[])
516 while ((opt = getopt(argc, argv, "c:Eeh")) != -1)
521 u = strtol(optarg, &end, 10);
523 err(1, "argument to -c");
525 err(1, "%s: not a number", optarg);
527 err(1, "%s: out of range", optarg);
540 fprintf(stderr, usgtxt);
547 flags = smiGetFlags();
548 flags |= SMI_FLAG_ERRORS;
551 mods = malloc(sizeof(mods[0]) * argc);
555 for (opt = 0; opt < argc; opt++) {
556 if ((name = smiLoadModule(argv[opt])) == NULL)
557 err(1, "%s: cannot load", argv[opt]);
558 mods[opt] = smiGetModule(name);
562 for (opt = 0; opt < argc; opt++) {
563 if (mods[opt] == NULL) /* smiGetModule failed above */
565 n = smiGetFirstNode(mods[opt], SMI_NODEKIND_ANY);
569 if (do_typedef == 0) {
570 level = open_node(n, level, &last);
576 if (last_node == NULL ||
577 (n = smiGetNextNode(last_node, SMI_NODEKIND_ANY))
582 if (last != NULL && do_typedef == 0)
583 level = close_node(last->oidlen - 1, level - 1);
584 else if (do_typedef != 0)