18eecd77aSHartmut Brandt /*
26f557cf7SHartmut Brandt * Copyright (C) 2004-2006
38eecd77aSHartmut Brandt * Hartmut Brandt.
48eecd77aSHartmut Brandt * All rights reserved.
58eecd77aSHartmut Brandt *
68eecd77aSHartmut Brandt * Author: Harti Brandt <harti@freebsd.org>
78eecd77aSHartmut Brandt *
8896052c1SHartmut Brandt * Redistribution and use in source and binary forms, with or without
9896052c1SHartmut Brandt * modification, are permitted provided that the following conditions
10896052c1SHartmut Brandt * are met:
11896052c1SHartmut Brandt * 1. Redistributions of source code must retain the above copyright
12896052c1SHartmut Brandt * notice, this list of conditions and the following disclaimer.
138eecd77aSHartmut Brandt * 2. Redistributions in binary form must reproduce the above copyright
148eecd77aSHartmut Brandt * notice, this list of conditions and the following disclaimer in the
158eecd77aSHartmut Brandt * documentation and/or other materials provided with the distribution.
168eecd77aSHartmut Brandt *
17896052c1SHartmut Brandt * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18896052c1SHartmut Brandt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19896052c1SHartmut Brandt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20896052c1SHartmut Brandt * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21896052c1SHartmut Brandt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22896052c1SHartmut Brandt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23896052c1SHartmut Brandt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24896052c1SHartmut Brandt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25896052c1SHartmut Brandt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26896052c1SHartmut Brandt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27896052c1SHartmut Brandt * SUCH DAMAGE.
288eecd77aSHartmut Brandt *
296f557cf7SHartmut Brandt * $Begemot: gensnmpdef.c 383 2006-05-30 07:40:49Z brandt_h $
308eecd77aSHartmut Brandt */
316f557cf7SHartmut Brandt #include <sys/queue.h>
326f557cf7SHartmut Brandt
338eecd77aSHartmut Brandt #include <stdio.h>
348eecd77aSHartmut Brandt #include <stdlib.h>
358eecd77aSHartmut Brandt #include <string.h>
368eecd77aSHartmut Brandt #include <unistd.h>
378eecd77aSHartmut Brandt #include <errno.h>
388eecd77aSHartmut Brandt #include <err.h>
398eecd77aSHartmut Brandt #include <assert.h>
408eecd77aSHartmut Brandt #include <smi.h>
418eecd77aSHartmut Brandt
428eecd77aSHartmut Brandt static const char usgtxt[] =
436f557cf7SHartmut Brandt "Usage: gensnmpdef [-hEe] [-c <cut>] MIB [MIB ...]\n"
448eecd77aSHartmut Brandt "Options:\n"
458eecd77aSHartmut Brandt " -c specify the number of initial sub-oids to cut from the oids\n"
466f557cf7SHartmut Brandt " -E extract named enum types. Print a typedef for all enums defined\n"
476f557cf7SHartmut Brandt " in syntax clauses of normal objects. Suppress normal output.\n"
486f557cf7SHartmut Brandt " -e extract unnamed enum types. Print a typedef for all enums defined\n"
496f557cf7SHartmut Brandt " as textual conventions. Suppress normal output.\n"
508eecd77aSHartmut Brandt " -h print this help\n"
518eecd77aSHartmut Brandt "MIBs are searched according to the libsmi(3) search rules and can\n"
528eecd77aSHartmut Brandt "be specified either by path or module name\n";
538eecd77aSHartmut Brandt
548eecd77aSHartmut Brandt static SmiNode *last_node;
558eecd77aSHartmut Brandt static u_int cut = 3;
568eecd77aSHartmut Brandt
576f557cf7SHartmut Brandt struct tdef {
586f557cf7SHartmut Brandt char *name;
596f557cf7SHartmut Brandt SLIST_ENTRY(tdef) link;
606f557cf7SHartmut Brandt };
616f557cf7SHartmut Brandt
625a64472bSAntoine Brodin static SLIST_HEAD(, tdef) tdefs = SLIST_HEAD_INITIALIZER(tdefs);
636f557cf7SHartmut Brandt static int do_typedef = 0;
646f557cf7SHartmut Brandt
658eecd77aSHartmut Brandt static void print_node(SmiNode *n, u_int level);
668eecd77aSHartmut Brandt
678eecd77aSHartmut Brandt static void
save_node(SmiNode * n)688eecd77aSHartmut Brandt save_node(SmiNode *n)
698eecd77aSHartmut Brandt {
708eecd77aSHartmut Brandt if (n != NULL)
718eecd77aSHartmut Brandt last_node = n;
728eecd77aSHartmut Brandt }
738eecd77aSHartmut Brandt
748eecd77aSHartmut Brandt static void
pindent(u_int level)758eecd77aSHartmut Brandt pindent(u_int level)
768eecd77aSHartmut Brandt {
778eecd77aSHartmut Brandt if (level >= cut)
788eecd77aSHartmut Brandt printf("%*s", (level - cut) * 2, "");
798eecd77aSHartmut Brandt }
808eecd77aSHartmut Brandt
818eecd77aSHartmut Brandt static void
print_name(SmiNode * n)828eecd77aSHartmut Brandt print_name(SmiNode *n)
838eecd77aSHartmut Brandt {
848eecd77aSHartmut Brandt char *p;
858eecd77aSHartmut Brandt
868eecd77aSHartmut Brandt for (p = n->name; *p != '\0'; p++) {
878eecd77aSHartmut Brandt if (*p == '-')
888eecd77aSHartmut Brandt printf("_");
898eecd77aSHartmut Brandt else
908eecd77aSHartmut Brandt printf("%c", *p);
918eecd77aSHartmut Brandt }
928eecd77aSHartmut Brandt }
938eecd77aSHartmut Brandt
948eecd77aSHartmut Brandt static u_int
close_node(u_int n,u_int level)958eecd77aSHartmut Brandt close_node(u_int n, u_int level)
968eecd77aSHartmut Brandt {
978eecd77aSHartmut Brandt while (n--) {
988eecd77aSHartmut Brandt pindent(level);
998eecd77aSHartmut Brandt level--;
1008eecd77aSHartmut Brandt if (level >= cut)
1018eecd77aSHartmut Brandt printf(")\n");
1028eecd77aSHartmut Brandt }
1038eecd77aSHartmut Brandt return (level);
1048eecd77aSHartmut Brandt }
1058eecd77aSHartmut Brandt
1068eecd77aSHartmut Brandt static u_int
open_node(const SmiNode * n,u_int level,SmiNode ** last)1078eecd77aSHartmut Brandt open_node(const SmiNode *n, u_int level, SmiNode **last)
1088eecd77aSHartmut Brandt {
1098eecd77aSHartmut Brandt SmiNode *n1;
1108eecd77aSHartmut Brandt u_int i;
1118eecd77aSHartmut Brandt
1128eecd77aSHartmut Brandt if (*last != NULL) {
1138eecd77aSHartmut Brandt for (i = 0; i < (*last)->oidlen - 1; i++) {
1148eecd77aSHartmut Brandt if (i >= n->oidlen) {
1158eecd77aSHartmut Brandt level = close_node((*last)->oidlen -
1168eecd77aSHartmut Brandt n->oidlen, level);
1178eecd77aSHartmut Brandt break;
1188eecd77aSHartmut Brandt }
1198eecd77aSHartmut Brandt if ((*last)->oid[i] != n->oid[i])
1208eecd77aSHartmut Brandt break;
1218eecd77aSHartmut Brandt }
1228eecd77aSHartmut Brandt if (i < (*last)->oidlen - 1)
1238eecd77aSHartmut Brandt level = close_node((*last)->oidlen - 1 - i,
1248eecd77aSHartmut Brandt level - 1) + 1;
1258eecd77aSHartmut Brandt }
1268eecd77aSHartmut Brandt
1278eecd77aSHartmut Brandt while (level < n->oidlen - 1) {
1288eecd77aSHartmut Brandt if (level >= cut) {
129c5f8d751SEnji Cooper n1 = smiGetNodeByOID(level + 1, n->oid);
130c5f8d751SEnji Cooper if (n1 == NULL)
131c5f8d751SEnji Cooper continue;
1328eecd77aSHartmut Brandt pindent(level);
1338eecd77aSHartmut Brandt printf("(%u", n->oid[level]);
1348eecd77aSHartmut Brandt printf(" ");
1358eecd77aSHartmut Brandt print_name(n1);
1368eecd77aSHartmut Brandt printf("\n");
1378eecd77aSHartmut Brandt }
1388eecd77aSHartmut Brandt level++;
1398eecd77aSHartmut Brandt }
1408eecd77aSHartmut Brandt return (level);
1418eecd77aSHartmut Brandt }
1428eecd77aSHartmut Brandt
1438eecd77aSHartmut Brandt static const char *const type_names[] = {
1448eecd77aSHartmut Brandt [SMI_BASETYPE_UNKNOWN] = "UNKNOWN_TYPE",
1458eecd77aSHartmut Brandt [SMI_BASETYPE_INTEGER32] = "INTEGER",
1468eecd77aSHartmut Brandt [SMI_BASETYPE_OCTETSTRING] = "OCTETSTRING",
1478eecd77aSHartmut Brandt [SMI_BASETYPE_OBJECTIDENTIFIER] = "OID",
1488eecd77aSHartmut Brandt [SMI_BASETYPE_UNSIGNED32] = "UNSIGNED32",
1498eecd77aSHartmut Brandt [SMI_BASETYPE_INTEGER64] = "INTEGER64",
1508eecd77aSHartmut Brandt [SMI_BASETYPE_UNSIGNED64] = "UNSIGNED64",
1518eecd77aSHartmut Brandt [SMI_BASETYPE_FLOAT32] = "FLOAT32",
1528eecd77aSHartmut Brandt [SMI_BASETYPE_FLOAT64] = "FLOAT64",
1538eecd77aSHartmut Brandt [SMI_BASETYPE_FLOAT128] = "FLOAT128",
1546f557cf7SHartmut Brandt [SMI_BASETYPE_ENUM] = "ENUM",
1558eecd77aSHartmut Brandt [SMI_BASETYPE_BITS] = "BITS",
1568eecd77aSHartmut Brandt };
1578eecd77aSHartmut Brandt
1588eecd77aSHartmut Brandt static const char *const type_map[] = {
1598eecd77aSHartmut Brandt "Gauge32", "GAUGE",
1608eecd77aSHartmut Brandt "Gauge", "GAUGE",
1618eecd77aSHartmut Brandt "TimeTicks", "TIMETICKS",
1628eecd77aSHartmut Brandt "Counter32", "COUNTER",
1638eecd77aSHartmut Brandt "Counter", "COUNTER",
1648eecd77aSHartmut Brandt "Counter64", "COUNTER64",
1658eecd77aSHartmut Brandt "Integer32", "INTEGER32",
1668eecd77aSHartmut Brandt "IpAddress", "IPADDRESS",
1678eecd77aSHartmut Brandt NULL
1688eecd77aSHartmut Brandt };
1698eecd77aSHartmut Brandt
1708eecd77aSHartmut Brandt static void
print_enum(SmiType * t)1716f557cf7SHartmut Brandt print_enum(SmiType *t)
1726f557cf7SHartmut Brandt {
1736f557cf7SHartmut Brandt SmiNamedNumber *nnum;
1746f557cf7SHartmut Brandt
1756f557cf7SHartmut Brandt printf(" (");
1766f557cf7SHartmut Brandt for (nnum = smiGetFirstNamedNumber(t); nnum != NULL;
1776f557cf7SHartmut Brandt nnum = smiGetNextNamedNumber(nnum))
1786f557cf7SHartmut Brandt printf(" %ld %s", nnum->value.value.integer32, nnum->name);
1796f557cf7SHartmut Brandt printf(" )");
1806f557cf7SHartmut Brandt }
1816f557cf7SHartmut Brandt
1826f557cf7SHartmut Brandt static void
print_type(SmiNode * n)1838eecd77aSHartmut Brandt print_type(SmiNode *n)
1848eecd77aSHartmut Brandt {
1858eecd77aSHartmut Brandt SmiType *type;
1868eecd77aSHartmut Brandt u_int m;
1878eecd77aSHartmut Brandt
1888eecd77aSHartmut Brandt type = smiGetNodeType(n);
1898eecd77aSHartmut Brandt assert(type != NULL);
1908eecd77aSHartmut Brandt
1918eecd77aSHartmut Brandt if (type->name != NULL) {
1928eecd77aSHartmut Brandt for (m = 0; type_map[m] != NULL; m += 2)
1938eecd77aSHartmut Brandt if (strcmp(type_map[m], type->name) == 0) {
1948eecd77aSHartmut Brandt printf("%s", type_map[m + 1]);
1958eecd77aSHartmut Brandt return;
1968eecd77aSHartmut Brandt }
1978eecd77aSHartmut Brandt }
1988eecd77aSHartmut Brandt printf("%s", type_names[type->basetype]);
1996f557cf7SHartmut Brandt
2006f557cf7SHartmut Brandt if (type->basetype == SMI_BASETYPE_ENUM ||
2016f557cf7SHartmut Brandt type->basetype == SMI_BASETYPE_BITS)
2026f557cf7SHartmut Brandt print_enum(type);
2036f557cf7SHartmut Brandt
2046f557cf7SHartmut Brandt else if (type->basetype == SMI_BASETYPE_OCTETSTRING &&
2056f557cf7SHartmut Brandt type->name != NULL)
2066f557cf7SHartmut Brandt printf(" | %s", type->name);
2078eecd77aSHartmut Brandt }
2088eecd77aSHartmut Brandt
2098eecd77aSHartmut Brandt static void
print_access(SmiAccess a)2108eecd77aSHartmut Brandt print_access(SmiAccess a)
2118eecd77aSHartmut Brandt {
2128eecd77aSHartmut Brandt if (a == SMI_ACCESS_READ_ONLY)
2138eecd77aSHartmut Brandt printf(" GET");
2148eecd77aSHartmut Brandt else if (a == SMI_ACCESS_READ_WRITE)
2158eecd77aSHartmut Brandt printf(" GET SET");
2168eecd77aSHartmut Brandt }
2178eecd77aSHartmut Brandt
2188eecd77aSHartmut Brandt static void
print_scalar(SmiNode * n,u_int level)2198eecd77aSHartmut Brandt print_scalar(SmiNode *n, u_int level)
2208eecd77aSHartmut Brandt {
2218eecd77aSHartmut Brandt SmiNode *p;
2228eecd77aSHartmut Brandt
2238eecd77aSHartmut Brandt assert (n->nodekind == SMI_NODEKIND_SCALAR);
2248eecd77aSHartmut Brandt
2258eecd77aSHartmut Brandt save_node(n);
2268eecd77aSHartmut Brandt
2278eecd77aSHartmut Brandt pindent(level);
2288eecd77aSHartmut Brandt printf("(%u ", n->oid[level]);
2298eecd77aSHartmut Brandt print_name(n);
2308eecd77aSHartmut Brandt printf(" ");
2318eecd77aSHartmut Brandt print_type(n);
2328eecd77aSHartmut Brandt
2338eecd77aSHartmut Brandt /* generate the operation from the parent node name */
2348eecd77aSHartmut Brandt p = smiGetParentNode(n);
2358eecd77aSHartmut Brandt printf(" op_%s", p->name);
2368eecd77aSHartmut Brandt
2378eecd77aSHartmut Brandt print_access(n->access);
2388eecd77aSHartmut Brandt
2398eecd77aSHartmut Brandt printf(")\n");
2408eecd77aSHartmut Brandt }
2418eecd77aSHartmut Brandt
2428eecd77aSHartmut Brandt static void
print_notification(SmiNode * n,u_int level)2438eecd77aSHartmut Brandt print_notification(SmiNode *n, u_int level)
2448eecd77aSHartmut Brandt {
2458eecd77aSHartmut Brandt
2468eecd77aSHartmut Brandt assert (n->nodekind == SMI_NODEKIND_NOTIFICATION);
2478eecd77aSHartmut Brandt
2488eecd77aSHartmut Brandt save_node(n);
2498eecd77aSHartmut Brandt
2508eecd77aSHartmut Brandt pindent(level);
2518eecd77aSHartmut Brandt printf("(%u ", n->oid[level]);
2528eecd77aSHartmut Brandt print_name(n);
2538eecd77aSHartmut Brandt printf(" OID");
2548eecd77aSHartmut Brandt
2558eecd77aSHartmut Brandt printf(" op_%s)\n", n->name);
2568eecd77aSHartmut Brandt }
2578eecd77aSHartmut Brandt
2588eecd77aSHartmut Brandt static void
print_col(SmiNode * n,u_int level)2598eecd77aSHartmut Brandt print_col(SmiNode *n, u_int level)
2608eecd77aSHartmut Brandt {
2618eecd77aSHartmut Brandt assert (n->nodekind == SMI_NODEKIND_COLUMN);
2628eecd77aSHartmut Brandt
2638eecd77aSHartmut Brandt save_node(n);
2648eecd77aSHartmut Brandt
2658eecd77aSHartmut Brandt pindent(level);
2668eecd77aSHartmut Brandt printf("(%u ", n->oid[level]);
2678eecd77aSHartmut Brandt print_name(n);
2688eecd77aSHartmut Brandt printf(" ");
2698eecd77aSHartmut Brandt print_type(n);
2708eecd77aSHartmut Brandt print_access(n->access);
2718eecd77aSHartmut Brandt printf(")\n");
2728eecd77aSHartmut Brandt }
2738eecd77aSHartmut Brandt
2748eecd77aSHartmut Brandt static void
print_index(SmiNode * row)2758eecd77aSHartmut Brandt print_index(SmiNode *row)
2768eecd77aSHartmut Brandt {
2778eecd77aSHartmut Brandt SmiElement *e;
2788eecd77aSHartmut Brandt
2798eecd77aSHartmut Brandt e = smiGetFirstElement(row);
2808eecd77aSHartmut Brandt while (e != NULL) {
2818eecd77aSHartmut Brandt printf(" ");
2828eecd77aSHartmut Brandt print_type(smiGetElementNode(e));
2838eecd77aSHartmut Brandt e = smiGetNextElement(e);
2848eecd77aSHartmut Brandt }
2858eecd77aSHartmut Brandt }
2868eecd77aSHartmut Brandt
2878eecd77aSHartmut Brandt static void
print_table(SmiNode * n,u_int level)2888eecd77aSHartmut Brandt print_table(SmiNode *n, u_int level)
2898eecd77aSHartmut Brandt {
2908eecd77aSHartmut Brandt SmiNode *row, *col, *rel;
2918eecd77aSHartmut Brandt
2928eecd77aSHartmut Brandt assert (n->nodekind == SMI_NODEKIND_TABLE);
2938eecd77aSHartmut Brandt
2948eecd77aSHartmut Brandt save_node(n);
2958eecd77aSHartmut Brandt
2968eecd77aSHartmut Brandt pindent(level);
2978eecd77aSHartmut Brandt printf("(%u ", n->oid[level]);
2988eecd77aSHartmut Brandt print_name(n);
2998eecd77aSHartmut Brandt printf("\n");
3008eecd77aSHartmut Brandt
3018eecd77aSHartmut Brandt row = smiGetFirstChildNode(n);
3028eecd77aSHartmut Brandt if (row->nodekind != SMI_NODEKIND_ROW)
3038eecd77aSHartmut Brandt errx(1, "%s: kind %u, not row", __func__, row->nodekind);
3048eecd77aSHartmut Brandt
3058eecd77aSHartmut Brandt save_node(n);
3068eecd77aSHartmut Brandt
3078eecd77aSHartmut Brandt pindent(level + 1);
3088eecd77aSHartmut Brandt printf("(%u ", row->oid[level + 1]);
3098eecd77aSHartmut Brandt print_name(row);
3108eecd77aSHartmut Brandt printf(" :");
3118eecd77aSHartmut Brandt
3128eecd77aSHartmut Brandt /* index */
3138eecd77aSHartmut Brandt rel = smiGetRelatedNode(row);
3148eecd77aSHartmut Brandt switch (row->indexkind) {
3158eecd77aSHartmut Brandt
3168eecd77aSHartmut Brandt case SMI_INDEX_INDEX:
3178eecd77aSHartmut Brandt print_index(row);
3188eecd77aSHartmut Brandt break;
3198eecd77aSHartmut Brandt
3208eecd77aSHartmut Brandt case SMI_INDEX_AUGMENT:
3218eecd77aSHartmut Brandt if (rel == NULL)
3228eecd77aSHartmut Brandt errx(1, "%s: cannot find augemented table", row->name);
3238eecd77aSHartmut Brandt print_index(rel);
3248eecd77aSHartmut Brandt break;
3258eecd77aSHartmut Brandt
3268eecd77aSHartmut Brandt default:
3278eecd77aSHartmut Brandt errx(1, "%s: cannot handle index kind %u", row->name,
3288eecd77aSHartmut Brandt row->indexkind);
3298eecd77aSHartmut Brandt }
3308eecd77aSHartmut Brandt
3318eecd77aSHartmut Brandt printf(" op_%s", n->name);
3328eecd77aSHartmut Brandt printf("\n");
3338eecd77aSHartmut Brandt
3348eecd77aSHartmut Brandt col = smiGetFirstChildNode(row);
3358eecd77aSHartmut Brandt while (col != NULL) {
3368eecd77aSHartmut Brandt print_col(col, level + 2);
3378eecd77aSHartmut Brandt col = smiGetNextChildNode(col);
3388eecd77aSHartmut Brandt }
3398eecd77aSHartmut Brandt pindent(level + 1);
3408eecd77aSHartmut Brandt printf(")\n");
3418eecd77aSHartmut Brandt
3428eecd77aSHartmut Brandt pindent(level);
3438eecd77aSHartmut Brandt printf(")\n");
3448eecd77aSHartmut Brandt }
3458eecd77aSHartmut Brandt
3468eecd77aSHartmut Brandt static void
print_it(SmiNode * n,u_int level)3478eecd77aSHartmut Brandt print_it(SmiNode *n, u_int level)
3488eecd77aSHartmut Brandt {
3498eecd77aSHartmut Brandt switch (n->nodekind) {
3508eecd77aSHartmut Brandt
3518eecd77aSHartmut Brandt case SMI_NODEKIND_NODE:
3528eecd77aSHartmut Brandt print_node(n, level);
3538eecd77aSHartmut Brandt break;
3548eecd77aSHartmut Brandt
3558eecd77aSHartmut Brandt case SMI_NODEKIND_SCALAR:
3568eecd77aSHartmut Brandt print_scalar(n, level);
3578eecd77aSHartmut Brandt break;
3588eecd77aSHartmut Brandt
3598eecd77aSHartmut Brandt case SMI_NODEKIND_TABLE:
3608eecd77aSHartmut Brandt print_table(n, level);
3618eecd77aSHartmut Brandt break;
3628eecd77aSHartmut Brandt
3638eecd77aSHartmut Brandt case SMI_NODEKIND_COMPLIANCE:
3648eecd77aSHartmut Brandt case SMI_NODEKIND_GROUP:
3658eecd77aSHartmut Brandt save_node(n);
3668eecd77aSHartmut Brandt break;
3678eecd77aSHartmut Brandt
3688eecd77aSHartmut Brandt case SMI_NODEKIND_NOTIFICATION:
3698eecd77aSHartmut Brandt print_notification(n, level);
3708eecd77aSHartmut Brandt break;
3718eecd77aSHartmut Brandt
3728eecd77aSHartmut Brandt default:
3738eecd77aSHartmut Brandt errx(1, "cannot handle %u nodes", n->nodekind);
3748eecd77aSHartmut Brandt }
3758eecd77aSHartmut Brandt }
3768eecd77aSHartmut Brandt
3778eecd77aSHartmut Brandt static void
print_node(SmiNode * n,u_int level)3788eecd77aSHartmut Brandt print_node(SmiNode *n, u_int level)
3798eecd77aSHartmut Brandt {
3808eecd77aSHartmut Brandt assert (n->nodekind == SMI_NODEKIND_NODE);
3818eecd77aSHartmut Brandt
3828eecd77aSHartmut Brandt save_node(n);
3838eecd77aSHartmut Brandt
3848eecd77aSHartmut Brandt pindent(level);
3858eecd77aSHartmut Brandt printf("(%u ", n->oid[level]);
3868eecd77aSHartmut Brandt print_name(n);
3878eecd77aSHartmut Brandt printf("\n");
3888eecd77aSHartmut Brandt
3898eecd77aSHartmut Brandt n = smiGetFirstChildNode(n);
3908eecd77aSHartmut Brandt while (n != NULL) {
3918eecd77aSHartmut Brandt print_it(n, level + 1);
3928eecd77aSHartmut Brandt n = smiGetNextChildNode(n);
3938eecd77aSHartmut Brandt }
3948eecd77aSHartmut Brandt pindent(level);
3958eecd77aSHartmut Brandt printf(")\n");
3968eecd77aSHartmut Brandt }
3978eecd77aSHartmut Brandt
3986f557cf7SHartmut Brandt static void
save_typdef(char * name)3996f557cf7SHartmut Brandt save_typdef(char *name)
4006f557cf7SHartmut Brandt {
4016f557cf7SHartmut Brandt struct tdef *t;
4026f557cf7SHartmut Brandt
40384064233SEnji Cooper t = calloc(1, sizeof(struct tdef));
4046f557cf7SHartmut Brandt if (t == NULL)
4056f557cf7SHartmut Brandt err(1, NULL);
4066f557cf7SHartmut Brandt
4076f557cf7SHartmut Brandt t->name = name;
4086f557cf7SHartmut Brandt SLIST_INSERT_HEAD(&tdefs, t, link);
4096f557cf7SHartmut Brandt }
4106f557cf7SHartmut Brandt
4116f557cf7SHartmut Brandt static void
tdefs_cleanup(void)4126f557cf7SHartmut Brandt tdefs_cleanup(void)
4136f557cf7SHartmut Brandt {
4146f557cf7SHartmut Brandt struct tdef *t;
4156f557cf7SHartmut Brandt
4166f557cf7SHartmut Brandt while ((t = SLIST_FIRST(&tdefs)) != NULL) {
4176f557cf7SHartmut Brandt SLIST_REMOVE_HEAD(&tdefs, link);
4186f557cf7SHartmut Brandt free(t);
4196f557cf7SHartmut Brandt }
4206f557cf7SHartmut Brandt }
4216f557cf7SHartmut Brandt
4226f557cf7SHartmut Brandt static void
print_enum_typedef(SmiType * t)4236f557cf7SHartmut Brandt print_enum_typedef(SmiType *t)
4246f557cf7SHartmut Brandt {
4256f557cf7SHartmut Brandt SmiNamedNumber *nnum;
4266f557cf7SHartmut Brandt
4276f557cf7SHartmut Brandt for (nnum = smiGetFirstNamedNumber(t); nnum != NULL;
4286f557cf7SHartmut Brandt nnum = smiGetNextNamedNumber(nnum)) {
4296f557cf7SHartmut Brandt printf("\t%ld %s\n" , nnum->value.value.integer32, nnum->name);
4306f557cf7SHartmut Brandt }
4316f557cf7SHartmut Brandt }
4326f557cf7SHartmut Brandt
4336f557cf7SHartmut Brandt static void
print_stype(SmiNode * n)4346f557cf7SHartmut Brandt print_stype(SmiNode *n)
4356f557cf7SHartmut Brandt {
4366f557cf7SHartmut Brandt SmiType *type;
4376f557cf7SHartmut Brandt struct tdef *t = NULL;
4386f557cf7SHartmut Brandt
4396f557cf7SHartmut Brandt type = smiGetNodeType(n);
4406f557cf7SHartmut Brandt assert(type != NULL);
4416f557cf7SHartmut Brandt
4426f557cf7SHartmut Brandt if (type->basetype == SMI_BASETYPE_ENUM) {
4436f557cf7SHartmut Brandt if (do_typedef == 'e' && type->name != NULL) {
4446f557cf7SHartmut Brandt SLIST_FOREACH(t, &tdefs, link) {
4456f557cf7SHartmut Brandt if (strcmp(t->name, type->name) == 0)
4466f557cf7SHartmut Brandt return;
4476f557cf7SHartmut Brandt }
4486f557cf7SHartmut Brandt save_typdef(type->name);
4496f557cf7SHartmut Brandt printf("typedef %s ENUM (\n", type->name);
4506f557cf7SHartmut Brandt } else if (do_typedef == 'E' && type->name == NULL)
4516f557cf7SHartmut Brandt printf("typedef %sType ENUM (\n", n->name);
4526f557cf7SHartmut Brandt else
4536f557cf7SHartmut Brandt return;
4546f557cf7SHartmut Brandt
4556f557cf7SHartmut Brandt print_enum_typedef(type);
4566f557cf7SHartmut Brandt printf(")\n\n");
4576f557cf7SHartmut Brandt
4586f557cf7SHartmut Brandt } else if (type->basetype == SMI_BASETYPE_BITS) {
4596f557cf7SHartmut Brandt if (do_typedef == 'e' && type->name != NULL) {
4606f557cf7SHartmut Brandt SLIST_FOREACH(t, &tdefs, link) {
4616f557cf7SHartmut Brandt if (strcmp(t->name, type->name) == 0)
4626f557cf7SHartmut Brandt return;
4636f557cf7SHartmut Brandt }
4646f557cf7SHartmut Brandt save_typdef(type->name);
4656f557cf7SHartmut Brandt printf("typedef %s BITS (\n", type->name);
4666f557cf7SHartmut Brandt } else if (do_typedef == 'E' && type->name == NULL)
4676f557cf7SHartmut Brandt printf("typedef %sType BITS (\n", n->name);
4686f557cf7SHartmut Brandt else
4696f557cf7SHartmut Brandt return;
4706f557cf7SHartmut Brandt
4716f557cf7SHartmut Brandt print_enum_typedef(type);
4726f557cf7SHartmut Brandt printf(")\n\n");
4736f557cf7SHartmut Brandt }
4746f557cf7SHartmut Brandt }
4756f557cf7SHartmut Brandt
4766f557cf7SHartmut Brandt static void
print_typdefs(SmiNode * n)4776f557cf7SHartmut Brandt print_typdefs(SmiNode *n)
4786f557cf7SHartmut Brandt {
4796f557cf7SHartmut Brandt SmiNode *p;
4806f557cf7SHartmut Brandt
4816f557cf7SHartmut Brandt p = n;
4826f557cf7SHartmut Brandt n = smiGetFirstChildNode(n);
4836f557cf7SHartmut Brandt while (n != NULL) {
4846f557cf7SHartmut Brandt switch (n->nodekind) {
4856f557cf7SHartmut Brandt case SMI_NODEKIND_SCALAR:
4866f557cf7SHartmut Brandt case SMI_NODEKIND_COLUMN:
4876f557cf7SHartmut Brandt print_stype(n);
4886f557cf7SHartmut Brandt break;
4896f557cf7SHartmut Brandt case SMI_NODEKIND_COMPLIANCE:
4906f557cf7SHartmut Brandt case SMI_NODEKIND_GROUP:
4916f557cf7SHartmut Brandt save_node(n);
4926f557cf7SHartmut Brandt return;
4936f557cf7SHartmut Brandt default:
4946f557cf7SHartmut Brandt break;
4956f557cf7SHartmut Brandt }
4966f557cf7SHartmut Brandt n = smiGetNextChildNode(n);
4976f557cf7SHartmut Brandt }
4986f557cf7SHartmut Brandt
4996f557cf7SHartmut Brandt save_node(p);
5006f557cf7SHartmut Brandt }
5016f557cf7SHartmut Brandt
5028eecd77aSHartmut Brandt int
main(int argc,char * argv[])5038eecd77aSHartmut Brandt main(int argc, char *argv[])
5048eecd77aSHartmut Brandt {
5058eecd77aSHartmut Brandt int opt;
5068eecd77aSHartmut Brandt int flags;
5078eecd77aSHartmut Brandt SmiModule **mods;
5088eecd77aSHartmut Brandt char *name;
5098eecd77aSHartmut Brandt SmiNode *n, *last;
5108eecd77aSHartmut Brandt u_int level;
5118eecd77aSHartmut Brandt long u;
5128eecd77aSHartmut Brandt char *end;
5138eecd77aSHartmut Brandt
5148eecd77aSHartmut Brandt smiInit(NULL);
5158eecd77aSHartmut Brandt
5166f557cf7SHartmut Brandt while ((opt = getopt(argc, argv, "c:Eeh")) != -1)
5178eecd77aSHartmut Brandt switch (opt) {
5188eecd77aSHartmut Brandt
5198eecd77aSHartmut Brandt case 'c':
5208eecd77aSHartmut Brandt errno = 0;
5218eecd77aSHartmut Brandt u = strtol(optarg, &end, 10);
5228eecd77aSHartmut Brandt if (errno != 0)
5238eecd77aSHartmut Brandt err(1, "argument to -c");
5248eecd77aSHartmut Brandt if (*end != '\0')
5258eecd77aSHartmut Brandt err(1, "%s: not a number", optarg);
5268eecd77aSHartmut Brandt if (u < 0 || u > 5)
5278eecd77aSHartmut Brandt err(1, "%s: out of range", optarg);
5288eecd77aSHartmut Brandt cut = (u_int)u;
5298eecd77aSHartmut Brandt break;
5308eecd77aSHartmut Brandt
5316f557cf7SHartmut Brandt case 'E':
5326f557cf7SHartmut Brandt do_typedef = 'E';
5336f557cf7SHartmut Brandt break;
5346f557cf7SHartmut Brandt
5356f557cf7SHartmut Brandt case 'e':
5366f557cf7SHartmut Brandt do_typedef = 'e';
5376f557cf7SHartmut Brandt break;
5386f557cf7SHartmut Brandt
5398eecd77aSHartmut Brandt case 'h':
5408eecd77aSHartmut Brandt fprintf(stderr, usgtxt);
5418eecd77aSHartmut Brandt exit(0);
5428eecd77aSHartmut Brandt }
5438eecd77aSHartmut Brandt
5448eecd77aSHartmut Brandt argc -= optind;
5458eecd77aSHartmut Brandt argv += optind;
5468eecd77aSHartmut Brandt
5478eecd77aSHartmut Brandt flags = smiGetFlags();
5488eecd77aSHartmut Brandt flags |= SMI_FLAG_ERRORS;
5498eecd77aSHartmut Brandt smiSetFlags(flags);
5508eecd77aSHartmut Brandt
5518eecd77aSHartmut Brandt mods = malloc(sizeof(mods[0]) * argc);
5528eecd77aSHartmut Brandt if (mods == NULL)
5538eecd77aSHartmut Brandt err(1, NULL);
5548eecd77aSHartmut Brandt
5558eecd77aSHartmut Brandt for (opt = 0; opt < argc; opt++) {
5568eecd77aSHartmut Brandt if ((name = smiLoadModule(argv[opt])) == NULL)
5578eecd77aSHartmut Brandt err(1, "%s: cannot load", argv[opt]);
5588eecd77aSHartmut Brandt mods[opt] = smiGetModule(name);
5598eecd77aSHartmut Brandt }
5608eecd77aSHartmut Brandt level = 0;
5618eecd77aSHartmut Brandt last = NULL;
5628eecd77aSHartmut Brandt for (opt = 0; opt < argc; opt++) {
563*b55d259aSEnji Cooper if (mods[opt] == NULL) /* smiGetModule failed above */
564*b55d259aSEnji Cooper continue;
5658eecd77aSHartmut Brandt n = smiGetFirstNode(mods[opt], SMI_NODEKIND_ANY);
566c5f8d751SEnji Cooper if (n == NULL)
567c5f8d751SEnji Cooper continue;
5688eecd77aSHartmut Brandt for (;;) {
5696f557cf7SHartmut Brandt if (do_typedef == 0) {
5708eecd77aSHartmut Brandt level = open_node(n, level, &last);
5718eecd77aSHartmut Brandt print_it(n, level);
5728eecd77aSHartmut Brandt last = n;
5736f557cf7SHartmut Brandt } else
5746f557cf7SHartmut Brandt print_typdefs(n);
5758eecd77aSHartmut Brandt
5768eecd77aSHartmut Brandt if (last_node == NULL ||
5778eecd77aSHartmut Brandt (n = smiGetNextNode(last_node, SMI_NODEKIND_ANY))
5788eecd77aSHartmut Brandt == NULL)
5798eecd77aSHartmut Brandt break;
5808eecd77aSHartmut Brandt }
5818eecd77aSHartmut Brandt }
5826f557cf7SHartmut Brandt if (last != NULL && do_typedef == 0)
5838eecd77aSHartmut Brandt level = close_node(last->oidlen - 1, level - 1);
5846f557cf7SHartmut Brandt else if (do_typedef != 0)
5856f557cf7SHartmut Brandt tdefs_cleanup();
5866f557cf7SHartmut Brandt
5878eecd77aSHartmut Brandt return (0);
5888eecd77aSHartmut Brandt }
589