Vendor patch: add support for the BITS construct and enumerations in both

gensnmpdef and gensnmptree. Add include and typedef directives to gensnmptree.

Submitted by:	soc-sheryana (partly)
This commit is contained in:
harti 2006-05-30 07:46:52 +00:00
parent 9b4fc3d8f1
commit f467eea07a
5 changed files with 915 additions and 114 deletions

View File

@ -1,3 +1,7 @@
1.12a
Support for ENUM and BITS in gensnmp{tree,def}. Include directives
and typedefs.
1.12
A couple of man page fixes from various submitters.

View File

@ -1,5 +1,5 @@
.\"
.\" Copyright (C) 2004-2005
.\" Copyright (C) 2004-2006
.\" Hartmut Brandt.
.\" All rights reserved.
.\"
@ -26,9 +26,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $Begemot: bsnmp/gensnmpdef/gensnmpdef.1,v 1.5 2005/10/04 08:46:46 brandt_h Exp $
.\" $Begemot: gensnmpdef.1 383 2006-05-30 07:40:49Z brandt_h $
.\"
.Dd June 14, 2005
.Dd May 28, 2006
.Dt GENSNMPDEF 1
.Os
.Sh NAME
@ -36,7 +36,7 @@
.Nd "generate a MIB description file from MIBs"
.Sh SYNOPSIS
.Nm
.Op Fl h
.Op Fl hEe
.Op Fl c Ar cut
.Ar name Op Ar ...
.Sh DESCRIPTION
@ -48,13 +48,28 @@ The description file must be edited to be actually useful
for feeding it into
.Xr gensnmptree 1 .
.Pp
The
.Fl c
option specifies the number of initial sub-oids that should be omitted
from the tree.
The following options are available:
.Bl -tag -width indent
.It Fl c Ar cut
Specify the number of initial sub-oids that should be omitted
from the tree in the output.
.Xr gensnmptree 1
automatically adds 1.3.6 in front of all OIDs so the default value
of 3 is just correct in most cases.
.It Fl E
Generate typedefs for named enumerations.
These are enumerations defined via the TEXTUAL-CONVENTION macro.
The normal tree output is suppressed.
.It Fl e
Generate typedefs for unnamed enumerations.
These are enumerations defined in the SYNTAX clause of an OBJECT-TYPE macro.
The name of the enumeration is formed by appending the string
.Ql Type
to the name of the object.
The normal tree output is suppressed.
.It Fl h
Print a short help text and exit.
.El
.Pp
.Nm
does no attempt on sorting the OID tree so in case of complex and

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2004
* Copyright (C) 2004-2006
* Hartmut Brandt.
* All rights reserved.
*
@ -26,8 +26,10 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Begemot: bsnmp/gensnmpdef/gensnmpdef.c,v 1.3 2004/08/06 08:46:45 brandt Exp $
* $Begemot: gensnmpdef.c 383 2006-05-30 07:40:49Z brandt_h $
*/
#include <sys/queue.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -38,9 +40,13 @@
#include <smi.h>
static const char usgtxt[] =
"Usage: gensnmpdef [-h] [-c <cut>] MIB [MIB ...]\n"
"Usage: gensnmpdef [-hEe] [-c <cut>] MIB [MIB ...]\n"
"Options:\n"
" -c specify the number of initial sub-oids to cut from the oids\n"
" -E extract named enum types. Print a typedef for all enums defined\n"
" in syntax clauses of normal objects. Suppress normal output.\n"
" -e extract unnamed enum types. Print a typedef for all enums defined\n"
" as textual conventions. Suppress normal output.\n"
" -h print this help\n"
"MIBs are searched according to the libsmi(3) search rules and can\n"
"be specified either by path or module name\n";
@ -48,6 +54,14 @@ static const char usgtxt[] =
static SmiNode *last_node;
static u_int cut = 3;
struct tdef {
char *name;
SLIST_ENTRY(tdef) link;
};
static SLIST_HEAD(, tdef) tdefs = SLIST_HEAD_INITIALIZER(tdef);
static int do_typedef = 0;
static void print_node(SmiNode *n, u_int level);
static void
@ -135,7 +149,7 @@ static const char *const type_names[] = {
[SMI_BASETYPE_FLOAT32] = "FLOAT32",
[SMI_BASETYPE_FLOAT64] = "FLOAT64",
[SMI_BASETYPE_FLOAT128] = "FLOAT128",
[SMI_BASETYPE_ENUM] = "INTEGER",
[SMI_BASETYPE_ENUM] = "ENUM",
[SMI_BASETYPE_BITS] = "BITS",
};
@ -151,6 +165,18 @@ static const char *const type_map[] = {
NULL
};
static void
print_enum(SmiType *t)
{
SmiNamedNumber *nnum;
printf(" (");
for (nnum = smiGetFirstNamedNumber(t); nnum != NULL;
nnum = smiGetNextNamedNumber(nnum))
printf(" %ld %s", nnum->value.value.integer32, nnum->name);
printf(" )");
}
static void
print_type(SmiNode *n)
{
@ -168,6 +194,14 @@ print_type(SmiNode *n)
}
}
printf("%s", type_names[type->basetype]);
if (type->basetype == SMI_BASETYPE_ENUM ||
type->basetype == SMI_BASETYPE_BITS)
print_enum(type);
else if (type->basetype == SMI_BASETYPE_OCTETSTRING &&
type->name != NULL)
printf(" | %s", type->name);
}
static void
@ -359,6 +393,111 @@ print_node(SmiNode *n, u_int level)
printf(")\n");
}
static void
save_typdef(char *name)
{
struct tdef *t;
t = malloc(sizeof(struct tdef));
if (t == NULL)
err(1, NULL);
memset(t, 0 , sizeof(struct tdef));
t->name = name;
SLIST_INSERT_HEAD(&tdefs, t, link);
}
static void
tdefs_cleanup(void)
{
struct tdef *t;
while ((t = SLIST_FIRST(&tdefs)) != NULL) {
SLIST_REMOVE_HEAD(&tdefs, link);
free(t);
}
}
static void
print_enum_typedef(SmiType *t)
{
SmiNamedNumber *nnum;
for (nnum = smiGetFirstNamedNumber(t); nnum != NULL;
nnum = smiGetNextNamedNumber(nnum)) {
printf("\t%ld %s\n" , nnum->value.value.integer32, nnum->name);
}
}
static void
print_stype(SmiNode *n)
{
SmiType *type;
struct tdef *t = NULL;
type = smiGetNodeType(n);
assert(type != NULL);
if (type->basetype == SMI_BASETYPE_ENUM) {
if (do_typedef == 'e' && type->name != NULL) {
SLIST_FOREACH(t, &tdefs, link) {
if (strcmp(t->name, type->name) == 0)
return;
}
save_typdef(type->name);
printf("typedef %s ENUM (\n", type->name);
} else if (do_typedef == 'E' && type->name == NULL)
printf("typedef %sType ENUM (\n", n->name);
else
return;
print_enum_typedef(type);
printf(")\n\n");
} else if (type->basetype == SMI_BASETYPE_BITS) {
if (do_typedef == 'e' && type->name != NULL) {
SLIST_FOREACH(t, &tdefs, link) {
if (strcmp(t->name, type->name) == 0)
return;
}
save_typdef(type->name);
printf("typedef %s BITS (\n", type->name);
} else if (do_typedef == 'E' && type->name == NULL)
printf("typedef %sType BITS (\n", n->name);
else
return;
print_enum_typedef(type);
printf(")\n\n");
}
}
static void
print_typdefs(SmiNode *n)
{
SmiNode *p;
p = n;
n = smiGetFirstChildNode(n);
while (n != NULL) {
switch (n->nodekind) {
case SMI_NODEKIND_SCALAR:
case SMI_NODEKIND_COLUMN:
print_stype(n);
break;
case SMI_NODEKIND_COMPLIANCE:
case SMI_NODEKIND_GROUP:
save_node(n);
return;
default:
break;
}
n = smiGetNextChildNode(n);
}
save_node(p);
}
int
main(int argc, char *argv[])
{
@ -373,7 +512,7 @@ main(int argc, char *argv[])
smiInit(NULL);
while ((opt = getopt(argc, argv, "c:h")) != -1)
while ((opt = getopt(argc, argv, "c:Eeh")) != -1)
switch (opt) {
case 'c':
@ -388,6 +527,14 @@ main(int argc, char *argv[])
cut = (u_int)u;
break;
case 'E':
do_typedef = 'E';
break;
case 'e':
do_typedef = 'e';
break;
case 'h':
fprintf(stderr, usgtxt);
exit(0);
@ -414,9 +561,12 @@ main(int argc, char *argv[])
for (opt = 0; opt < argc; opt++) {
n = smiGetFirstNode(mods[opt], SMI_NODEKIND_ANY);
for (;;) {
level = open_node(n, level, &last);
print_it(n, level);
last = n;
if (do_typedef == 0) {
level = open_node(n, level, &last);
print_it(n, level);
last = n;
} else
print_typdefs(n);
if (last_node == NULL ||
(n = smiGetNextNode(last_node, SMI_NODEKIND_ANY))
@ -424,6 +574,10 @@ main(int argc, char *argv[])
break;
}
}
level = close_node(last->oidlen - 1, level - 1);
if (last != NULL && do_typedef == 0)
level = close_node(last->oidlen - 1, level - 1);
else if (do_typedef != 0)
tdefs_cleanup();
return (0);
}

View File

@ -2,6 +2,9 @@
.\" Copyright (c) 2001-2005
.\" Fraunhofer Institute for Open Communication Systems (FhG Fokus).
.\" All rights reserved.
.\" Copyright (c) 2006
.\" Hartmut Brandt
.\" All rights reserved.
.\"
.\" Author: Harti Brandt <harti@freebsd.org>
.\"
@ -26,9 +29,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $Begemot: bsnmp/gensnmptree/gensnmptree.1,v 1.7 2006/02/27 09:52:08 brandt_h Exp $
.\" $Begemot: gensnmptree.1 383 2006-05-30 07:40:49Z brandt_h $
.\"
.Dd February 27, 2006
.Dd May 26, 2006
.Dt GENSNMPTREE 1
.Os
.Sh NAME
@ -36,7 +39,9 @@
.Nd "generate C and header files from a MIB description file"
.Sh SYNOPSIS
.Nm
.Op Fl helt
.Op Fl dEehlt
.Op Fl I Ar directory
.Op Fl i Ar infile
.Op Fl p Ar prefix
.Op Ar name Ar ...
.Sh DESCRIPTION
@ -49,9 +54,12 @@ The first form is used only for maintaining the
daemon or for module writers.
The second form may be used by SNMP client program writers.
.Pp
If the
.Fl e
option is not used
If none of the options
.Fl e ,
.Fl E
or
.FL t
are used
.Nm
reads a MIB description from its standard input and creates two files: a
C-file
@ -61,12 +69,20 @@ containing a table used by
during PDU processing
and a header file
.Ar prefix Ns tree.h
containing appropriate declarations of the callback functions used in this table
and the table itself.
containing appropriate declarations of the callback functions used in this
table, the table itself and definitions for all enums.
.Pp
If the
.Fl e
option is specified
The following options are available:
.Bl -tag -width ".Fl E"
.It Fl d
Switch on debugging.
.It Fl E
Extract enumerations and bit constructs.
In this mode the tool emits
a header file that contains for each type given on the command line a
C-enum definition and a preprocessor define that may be used to map
values to strings.
.It Fl e
.Nm
expects MIB variable names (only the last component) on its command line.
It reads a MIB specification from standard input and for each MIB variable
@ -83,13 +99,13 @@ is the length of the OID.
.It Va OID_ Ns Ar name
is the last component of the OID.
.El
.Pp
The options are as follows:
.Bl -tag -width ".Fl d Ar argument"
.It Fl h
Print a short help page.
.It Fl e
Enter extract mode.
.It Fl I Ar directory
Add the named directory to the include path just before the standard include
directories.
.It Fl i Ar infile
Read from the named file instead of standard input.
.It Fl l
Generate local preprocessor includes.
This is used for bootstrapping
@ -103,26 +119,44 @@ Prefix the file names and the table name with
.Sh MIBS
The syntax of the MIB description file can formally be specified as follows:
.Bd -unfilled -offset indent
file := tree | tree file
file := top | top file
tree := head elements ')'
top := tree | typedef | include
entry := head ':' index STRING elements ')'
tree := head elements ')'
leaf := head TYPE STRING ACCESS ')'
entry := head ':' index STRING elements ')'
column := head TYPE ACCESS ')'
leaf := head type STRING ACCESS ')'
head := '(' INT STRING
column := head type ACCESS ')'
elements := EMPTY | elements element
type := BASETYPE | BASETYPE '|' subtype | enum | bits
element := tree | leaf
subtype := STRING
index := TYPE | index TYPE
enum := ENUM '(' value ')'
bits := BITS '(' value ')'
value := INT STRING | INT STRING value
head := '(' INT STRING
elements := EMPTY | elements element
element := tree | leaf | column
index := type | index type
typedef := 'typedef' STRING type
include := 'include' filespec
filespec := '"' STRING '"' | '<' STRING '>'
.Ed
.Pp
.Ar TYPE
.Ar BASETYPE
specifies a SNMP data type and may be one of
.Bl -bullet -offset indent -compact
.It
@ -163,10 +197,25 @@ SET
is a decimal integer and
.Ar STRING
is any string starting with a letter or underscore and consisting of
letters, digits and underscores, that is not one of the keywords.
letters, digits, underscores and minuses, that is not one of the keywords.
.Pp
The
.Ar typedef
directive associates a type with a single name.
.Pp
The
.Ar include
directive is replaced by the contents of the named file.
.Sh EXAMPLES
The following MIB description describes the system group:
.Bd -literal -offset indent
include "tc.def"
typedef AdminStatus ENUM (
1 up
2 down
)
(1 internet
(2 mgmt
(1 mibII

File diff suppressed because it is too large Load Diff