parse.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* Copyright %G% Sun Microsystems, Inc. All Rights Reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/******************************************************************
Copyright 1989, 1991, 1992 by Carnegie Mellon University
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/
/*
* parse.c
*/
/* HISTORY
* Jerry Yeung 6-6-96
* Jerry Yeung 1-9-97 fix 4019317
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "parse.h"
/* A quoted string value-- too long for a general "token" */
char *quoted_string_buffer;
/*
* This is one element of an object identifier with either an integer
* subidentifier, or a textual string label, or both.
* The subid is -1 if not present, and label is NULL if not present.
*/
struct subid {
int subid;
char *label;
};
#define MAXTC 128
struct tc { /* textual conventions */
int type;
char descriptor[MAXTOKEN];
int Line = 1;
#define SYNTAX_MASK 0x80
/* types of tokens
Tokens wiht the SYNTAX_MASK bit set are syntax tokens */
#define CONTINUE -1
#define ENDOFFILE 0
#define LABEL 1
#define SUBTREE 2
#define SYNTAX 3
#define SEQUENCE 14
#define OBJTYPE 16
#define ACCESS 17
#define READONLY 18
#define READWRITE 19
#define WRITEONLY 20
#define NOACCESS 21
#define STATUS 22
#define MANDATORY 23
#define OPTIONAL 24
#define OBSOLETE 25
/* #define RECOMMENDED 26 */
#define PUNCT 27
#define EQUALS 28
#define NUMBER 29
#define LEFTBRACKET 30
#define RIGHTBRACKET 31
#define LEFTPAREN 32
#define RIGHTPAREN 33
#define COMMA 34
#define DESCRIPTION 35
#define QUOTESTRING 36
#define INDEX 37
#define DEFVAL 38
#define DEPRECATED 39
#define SIZE 40
#define OBJGROUP 44
#define NOTIFTYPE 45
#define AUGMENTS 46
#define COMPLIANCE 47
#define READCREATE 48
#define UNITS 49
#define REFERENCE 50
#define NUM_ENTRIES 51
#define MODULEIDENTITY 52
#define LASTUPDATED 53
#define ORGANIZATION 54
#define CONTACTINFO 55
#define CURRENT 57
#define DEFINITIONS 58
#define END 59
#define SEMI 60
/*
-- Olivier Reisacher 95/2/14
*/
#define PSEUDO_TOKEN_TABLE 61
#define PSEUDO_TOKEN_ENTRY 62
/* Jerry Yeung 6-6-96 (trap-support) */
#define TRAPTYPE 63
#define ENTERPRISE 64
#define VARIABLES 65
struct tok {
char *name; /* token name */
int len; /* length not counting nul */
int token; /* value */
int hash; /* hash of name */
};
/* { "recommended", sizeof("recommended")-1, RECOMMENDED }, */
/*
* This CONTINUE appends the next word onto OBJECT,
* hopefully matching OBJECTIDENTIFIER above.
*/
{ "OCTET", sizeof ("OCTET")-1, -1 },
{ NULL }
};
#define HASHSIZE 32
#define BUCKET(x) (x & 0x01F)
static void
static int
static int
static int
int number_value;
static void
{
register char *cp;
register int h;
register int b;
/*
-- Olivier Reisacher 95/2/14
bzero((char *)buckets, sizeof(buckets));
*/
h += *cp;
b = BUCKET(h);
if (buckets[b])
}
}
#define NHASHSIZE 128
#define NBUCKET(x) (x & 0x7F)
void init_node_hash(nodes)
{
register char *cp;
register int hash;
/*
-- Olivier Reisacher 95/2/14
bzero((char *)nbuckets,sizeof(nbuckets));
*/
hash = 0;
}
}
static char *
unsigned num;
{
char *buf;
/* this is to fix (what seems to be) a problem with the IBM RT C
library malloc */
if (num < 16)
num = 16;
exit(1);
}
return (char *)buf;
}
static void
char *string;
char *token;
int type;
{
else if (token)
else
}
/*
-- Olivier Reisacher 95/2/14
#ifdef TEST
print_subtree(tree, count)
*/
int count;
{
int i;
for(i = 0; i < count; i++)
printf(" ");
count++;
for(i = 0; i < count; i++)
printf(" ");
}
}
}
/*
-- Olivier Reisacher 95/2/14
#endif
*/
int translation_table[256];
void build_translation_table(){
int count;
switch(count){
case PARSE_OBJID:
break;
case OCTETSTR:
break;
case PARSE_INTEGER:
break;
case NETADDR:
break;
case IPADDR:
break;
case PARSE_COUNTER:
break;
case PARSE_GUAGE:
break;
case PARSE_TIMETICKS:
break;
case PARSE_OPAQUE:
break;
case NUL:
break;
case PARSE_COUNTER64:
break;
case BITSTRING:
break;
case NSAPADDRESS:
break;
case UINTEGER32:
break;
/*
-- Olivier Reisacher 95/2/14
*/
case PSEUDO_TOKEN_TABLE:
break;
case PSEUDO_TOKEN_ENTRY:
break;
default:
break;
}
}
}
/*
-- Olivier Reisacher 95/2/14
static struct tree *
*/
struct tree *
{
int bucket, nodes_left = 0;
/* grow tree from this root node */
/*
-- Olivier Reisacher 95/2/14
build root node
tp = (struct tree *)Malloc(sizeof(struct tree));
tp->parent = NULL;
tp->next_peer = NULL;
tp->child_list = NULL;
tp->enums = NULL;
strcpy(tp->label, "joint-iso-ccitt");
tp->subid = 2;
tp->type = 0;
tp->description = 0;
XXX nodes isn't needed in do_subtree() ???
do_subtree(tp, &nodes);
lasttp = tp;
build root node
tp = (struct tree *)Malloc(sizeof(struct tree));
tp->parent = NULL;
tp->next_peer = lasttp;
tp->child_list = NULL;
tp->enums = NULL;
strcpy(tp->label, "ccitt");
tp->subid = 0;
tp->type = 0;
tp->description = 0;
XXX nodes isn't needed in do_subtree() ???
do_subtree(tp, &nodes);
lasttp = tp;
*/
/* build root node */
/*
-- Olivier Reisacher 95/2/14
tp->next_peer = lasttp;
*/
tp->description = 0;
/* XXX nodes isn't needed in do_subtree() ??? */
/*
-- Olivier Reisacher 95/2/14
*/
#ifdef TEST
print_subtree(tp, 0);
#endif /* TEST */
/* If any nodes are left, the tree is probably inconsistent */
nodes_left = 1;
break;
}
}
if (nodes_left){
}
}
return tp;
}
/*
* Find all the children of root in the list of nodes. Link them into the
* tree and out of the nodes list.
*/
static void
{
char *cp;
int hash;
hash = 0;
/*
* Search each of the nodes for one whose parent is root, and
* move each into a separate list.
*/
/* if there is another node with the same label, assume that
* any children after this point in the list belong to the other node.
* This adds some scoping to the table and allows vendors to
* reuse names such as "ip".
*/
break;
}
} else {
if (child_list == NULL){
} else {
}
/* take this node out of the node list */
} else {
}
}
}
if (childp)
/*
* Take each element in the child list and place it into the tree.
*/
/*
-- Olivier Reisacher 95/2/14
*/
/*
-- Olivier Reisacher 95/2/14
-- The goal of this modification is to order the
-- tree according to the subid
if (root->child_list == NULL){
root->child_list = tp;
} else {
peer->next_peer = tp;
}
peer = tp;
*/
{
}
else
{
while(t)
{
{
break;
}
l = t;
t = t->next_peer;
}
if(l == NULL)
{
}
else
{
}
}
/* if (tp->type == TYPE_OTHER) */
an end node */
}
/* free all nodes that were copied into tree */
if (oldnp)
}
if (oldnp)
}
/*
* Takes a list of the form:
* { iso org(3) dod(6) 1 }
* and creates several nodes, one for each parent-child pair.
* Returns NULL on error.
*/
static int
int length; /* the length of the array */
{
register int count;
int type;
register char *cp;
#ifdef TRACE_PROC
printf("getoid() invoked\n");
#endif
return NULL;
}
if (type == RIGHTBRACKET){
return count;
return NULL;
}
/* this entry has a label */
return NULL;
}
} else {
return NULL;
}
} else {
continue;
}
} else {
/* this entry has just an integer sub-identifier */
}
}
return count;
}
static void
{
/*
-- Olivier Reisacher 95/2/14
*/
while(ip)
{
}
while(ep){
}
}
char *name;
{
int type;
struct index_list *ip;
#ifdef TRACE_PROC
printf("parse_traptype() invoked\n");
#endif
if( type == ENTERPRISE){
/* create a trap item */
return 0;
}
switch(type){
case VARIABLES:
if(type != LEFTBRACKET){
return 0;
}
while(type != RIGHTBRACKET)
{
{
return 0;
}
(ti->n_variables)++;
{
Malloc(sizeof(struct index_list));
}
else
{
}
switch(type)
{
case COMMA:
break;
case RIGHTBRACKET:
break;
default:
return 0;
}
}
break;
case DESCRIPTION:
if(type != QUOTESTRING){
return 0;
}
if(quoted_string_buffer==NULL){
return 0;
}
break;
case REFERENCE:
if(type != QUOTESTRING){
return 0;
}
break;
default:
/* NOTHING*/
break;
}
}
/* get the integer */
/* attach the item to list */
}else{
return 0;
}
}else{
return 0;
}
}else{
return 0;
}
}else{
last_trap_item = ti;
}
return 1;
}
/*
* Parse an entry of the form:
* label OBJECT IDENTIFIER ::= { parent 2 }
* The "label OBJECT IDENTIFIER" portion has already been parsed.
* Returns 0 on error.
*/
static struct node *
char *name;
{
int type;
register int count;
int length;
return 0;
}
/*
-- Olivier Reisacher 95/2/14
bzero((char *)np, sizeof(struct node));
*/
/*
* For each parent-child subid pair in the subid array,
* create a node and link it into the node list.
*/
/* every node must have parent's name and child's name or number */
/*
-- Olivier Reisacher 95/2/14
*/
/* set up next entry */
/*
-- Olivier Reisacher 95/2/14
bzero((char *)np->next, sizeof(struct node));
*/
}
}
/*
* The above loop took care of all but the last pair. This pair is taken
* care of here. The name for this node is taken from the label for this
* entry.
* np still points to an unused entry.
*/
else
print_error("Warning: This entry is pretty silly",
} else {
if (oldnp)
else
return NULL;
}
} else {
if (oldnp)
return NULL;
}
/* free the oid array */
}
return root;
} else {
return 0;
}
}
static int
char *descriptor;
{
int i;
for(i = 0; i < MAXTC; i++){
break;
}
}
return LABEL;
}
/*
* Parses an asn type. Structures are ignored by this parser.
* Returns NULL on error.
*/
static int
char *name;
int *ntype;
char *ntoken;
{
int type, i;
int level;
#ifdef TRACE_PROC
printf("parse_asntype() invoked\n");
#endif
if (type == RIGHTBRACKET){
return 1;
}
}
return 0;
} else {
}
/* textual convention */
for(i = 0; i < MAXTC; i++){
break;
}
if (i == MAXTC){
return 0;
}
if (!(type & SYNTAX_MASK)){
/*
-- Olivier Reisacher 95/2/14
print_error("Textual convention doesn't map to real type.", token,
type);
return 0;
}
*/
int w;
if(!(w & SYNTAX_MASK))
{
return 0;
}
type = w;
}
level = 1;
/* don't record any constraints for now */
while(level > 0){
level++;
if (*ntype == RIGHTPAREN)
level--;
}
} else if (*ntype == LEFTBRACKET) {
/* if there is an enumeration list, parse it */
if (type == RIGHTBRACKET)
break;
/* this is an enumerated label */
} else {
}
/* a reasonable approximation for the length */
/* free_node(np); */
return 0;
}
/* free_node(np); */
return 0;
}
if (type != RIGHTPAREN){
/* free_node(np); */
return 0;
}
}
}
/* free_node(np); */
return 0;
}
}
return 1;
}
}
/*
* Parses an OBJECT TYPE macro.
* Returns 0 on error.
*/
static struct node *
char *name;
{
register int type;
/*
-- Olivier Reisacher 95/2/14
*/
struct index_list *ip;
return 0;
}
/*
-- Olivier Reisacher 95/2/14
*/
#if 0
return 0;
}
#endif
}
np->oct_str_len = 0;
switch(type){
case SEQUENCE:
/*
-- Olivier Reisacher 95/2/14
*/
}
break;
case PARSE_INTEGER:
case UINTEGER32:
if (nexttype == LEFTBRACKET) {
/* if there is an enumeration list, parse it */
if (type == RIGHTBRACKET)
break;
/* this is an enumerated label */
} else {
}
/* a reasonable approximation for the length */
return 0;
}
return 0;
}
if (type != RIGHTPAREN){
return 0;
}
}
}
return 0;
}
/* ignore the "constrained integer" for now */
}
break;
case BITSTRING:
if (nexttype == LEFTBRACKET) {
/* if there is an enumeration list, parse it */
if (type == RIGHTBRACKET)
break;
/* this is an enumerated label */
} else {
}
/* a reasonable approximation for the length */
return 0;
}
return 0;
}
if (type != RIGHTPAREN){
return 0;
}
}
}
return 0;
}
/* ignore the "constrained integer" for now */
}
break;
case OCTETSTR:
/* ignore the "constrained octet string" for now */
number_value = 0;
if (nexttype == RIGHTPAREN)
{
break;
}
}
}
return 0;
}
break;
case PARSE_OBJID:
case NETADDR:
case IPADDR:
case PARSE_COUNTER:
case PARSE_GUAGE:
case PARSE_TIMETICKS:
case PARSE_OPAQUE:
case NUL:
case LABEL:
case NSAPADDRESS:
case PARSE_COUNTER64:
break;
default:
return 0;
}
if (type != QUOTESTRING) {
return 0;
}
}
return 0;
}
return 0;
}
/*
-- Olivier Reisacher 95/2/14
*/
switch(type)
{
case READONLY:
break;
case READWRITE:
break;
case WRITEONLY:
break;
case NOACCESS:
break;
case READCREATE:
break;
}
return 0;
}
if (type != MANDATORY && type != CURRENT && type != OPTIONAL && type != OBSOLETE && type != DEPRECATED){
return 0;
}
/*
* Optional parts of the OBJECT-TYPE macro
*/
switch (type) {
case DESCRIPTION:
if (type != QUOTESTRING) {
return 0;
}
#ifdef TEST
#endif
if (!quoted_string_buffer){
exit(1);
}
break;
case REFERENCE:
if (type != QUOTESTRING) {
return 0;
}
break;
/*
-- Olivier Reisacher 95/2/14
*/
case INDEX:
if(type != LEFTBRACKET)
{
return 0;
}
while(type != RIGHTBRACKET)
{
{
return 0;
}
{
Malloc(sizeof(struct index_list));
}
else
{
}
switch(type)
{
case COMMA:
break;
case RIGHTBRACKET:
break;
default:
return 0;
}
}
break;
case DEFVAL:
case AUGMENTS:
case NUM_ENTRIES:
return 0;
}
break;
default:
return 0;
}
}
return 0;
}
/* just take the last pair in the oid list */
else
} else {
np = 0;
}
/* free oid array */
}
return np;
}
/*
* Parses an OBJECT GROUP macro.
* Returns 0 on error.
*/
static struct node *
char *name;
{
register int type;
/*
-- Olivier Reisacher 95/2/14
*/
switch (type) {
case DESCRIPTION:
if (type != QUOTESTRING) {
return 0;
}
#ifdef TEST
#endif
if (!quoted_string_buffer){
exit(1);
}
break;
default:
/* NOTHING */
break;
}
}
/* just take the last pair in the oid list */
else
} else {
np = 0;
}
/* free oid array */
}
return np;
}
/*
* Parses a NOTIFICATION-TYPE macro.
* Returns 0 on error.
*/
static struct node *
char *name;
{
register int type;
/*
-- Olivier Reisacher 95/2/14
*/
switch (type) {
case DESCRIPTION:
if (type != QUOTESTRING) {
return 0;
}
#ifdef TEST
#endif
if (!quoted_string_buffer){
exit(1);
}
break;
default:
/* NOTHING */
break;
}
}
/* just take the last pair in the oid list */
else
} else {
np = 0;
}
/* free oid array */
}
return np;
}
/*
* Parses a compliance macro
* Returns 0 on error.
*/
static struct node *
char *name;
{
register int type;
/*
-- Olivier Reisacher 95/2/14
*/
}
/* just take the last pair in the oid list */
else
} else {
np = 0;
}
/* free oid array */
}
return np;
}
/*
* Parses a module identity macro
* Returns 0 on error.
*/
static struct node *
char *name;
{
register int type;
/*
-- Olivier Reisacher 95/2/14
*/
}
/* just take the last pair in the oid list */
else
} else {
np = 0;
}
/* free oid array */
}
return np;
}
char *name;
{
int type = DEFINITIONS;
#ifdef TRACE_PROC
printf("parse_mib_header() invoked\n");
#endif
/* This probably isn't good enough. If there is no
imports clause we can't go around waiting (forever) for a semicolon.
We need to check for semi following an EXPORTS clause or an IMPORTS
clause of both. Look for BEGIN; in my initial MIBs to see those
that I needed to hack to get to parse because they didn't have
an IMPORTS or and EXPORTS clause.
*/
}
return 1;
}
/*
-- Olivier Reisacher 95/2/14
*/
void parse_init()
{
hash_init();
}
/*
* Parses a mib file and returns a linked list of nodes found in the file.
* Returns NULL on error.
*/
/*
-- Olivier Reisacher 95/2/14
static struct node *
*/
struct node *
{
int type = 1;
#define BETWEEN_MIBS 1
#define IN_MIB 2
int state = BETWEEN_MIBS;
#ifdef TRACE_PROC
printf("parse() invoked\n");
#endif
/*
-- Olivier Reisacher 95/2/14
hash_init();
*/
Line = 1;
if (!quoted_string_buffer){
exit(1);
}
/*
-- Olivier Reisacher 95/2/14
bzero(tclist, 64 * sizeof(struct tc));
*/
return NULL;
}
continue;
return root;
}
return NULL;
}
if (type == DEFINITIONS){
if (state != BETWEEN_MIBS){
return NULL;
}
return NULL;
}
/* first link in chain */
type);
return NULL;
}
} else {
type);
return NULL;
}
}
/* now find end of chain */
/* first link in chain */
type);
return NULL;
}
} else {
type);
return NULL;
}
}
/* now find end of chain */
/* first link in chain */
print_error("Bad parse of notification definition",
return NULL;
}
} else {
print_error("Bad parse of notification definition",
return NULL;
}
}
/* now find end of chain */
} else if (type == COMPLIANCE){
/* first link in chain */
type);
return NULL;
}
} else {
type);
return NULL;
}
}
/* now find end of chain */
} else if (type == MODULEIDENTITY){
/* first link in chain */
type);
return NULL;
}
} else {
type);
return NULL;
}
}
/* now find end of chain */
} else if (type == PARSE_OBJID){
/* first link in chain */
return NULL;
}
} else {
type);
return NULL;
}
}
/* now find end of chain */
return NULL;
}
goto skipget;
return NULL;
}
break;
} else {
return NULL;
}
}
#ifdef TEST
{
printf("Enums: \n");
}
}
}
}
#endif /* TEST */
return root;
}
/*
* Parses a token from the file. The type of the token parsed is returned,
* and the text is placed in the string pointed to by token.
*/
static int
register char *token;
{
static char last = ' ';
register int ch;
register int hash = 0;
*cp = 0;
/* skip all white space */
if (ch == '\n')
Line++;
}
if (ch == -1) {
#ifdef TRACE_GET_TOKEN
#endif
return ENDOFFILE;
} else if (ch == '"') {
}
/*
* Accumulate characters until end of token is found. Then attempt to
* match this token as a reserved word. If a match is found, return the
* type. Else it is a label.
*/
do {
if (ch == '\n')
Line++;
last = ' ';
} else {
}
*cp = '\0';
break;
}
if (tp){
continue;
#ifdef TRACE_GET_TOKEN
#endif
}
/* strip comment */
if (ch != '\n'){
if (ch == '\n'){
Line++;
break;
}
}
if (ch == -1)
{
#ifdef TRACE_GET_TOKEN
#endif
return ENDOFFILE;
}
}
{
#ifdef TRACE_GET_TOKEN
#endif
return LABEL;
}
#ifdef TRACE_GET_TOKEN
#endif
return NUMBER;
} else {
if (ch == '\n')
Line++;
}
#ifdef TRACE_GET_TOKEN
#endif
return ENDOFFILE;
}
struct tree *
char *filename;
{
return NULL;
if (!nodes){
exit(1);
}
return tree;
}
#ifdef TEST
int argc;
char *argv[];
{
return 1;
}
if (!nodes){
return (1);
}
print_subtree(tp, 0);
}
#endif /* TEST */
static int
register char *token;
{
register int ch;
register int len = 0;
ch = ' ';
if (ptr)
*ptr = 0;
while(ch != -1) {
if (ch != '"') {
if (ptr) {
len ++;
if (len % MAXQUOTESTR == 0) {
if (!ptr1){
exit(1);
}
}
}
} else {
if (ptr)
* ptr = 0;
}
if (ch == '\n')
Line++;
else if (ch == '"') {
#ifdef TRACE_GET_TOKEN
#endif
return QUOTESTRING;
}
}
#ifdef TRACE_GET_TOKEN
#endif
return NULL;
}
/*
* This routine parses a string like { blah blah blah } and returns PARSE_OBJID if
* it is well formed, and NULL if not.
*/
static int
{
register int ch;
/* ch = last; = ' '? */
/* skip all white space */
if (ch == '\n')
Line++;
}
if (ch != '{')
return NULL;
while(ch != -1) {
if (ch == '\n')
Line++;
else if (ch == '}')
return PARSE_OBJID;
}
/* last = ch;*/
return NULL;
}