Index: dump-python.c =================================================================== --- dump-python.c (revision 7637) +++ dump-python.c (working copy) @@ -21,6 +21,7 @@ #include +#include #include #include #include @@ -325,23 +326,71 @@ SmiRange *lastrange = NULL; SmiValue min, max; - min.basetype = SMI_BASETYPE_UNSIGNED32; - min.value.unsigned32 = 4294967295U; + min.basetype = smiType->basetype; + max.basetype = smiType->basetype; - max.basetype = SMI_BASETYPE_UNSIGNED32; - max.value.unsigned32 = 0; + switch (smiType->basetype) { + case SMI_BASETYPE_INTEGER32: + min.value.integer32 = INT32_MAX; + max.value.integer32 = INT32_MIN; + break; + case SMI_BASETYPE_INTEGER64: + min.value.integer64 = INT64_MAX; + max.value.integer64 = INT64_MIN; + break; + case SMI_BASETYPE_UNSIGNED32: + min.value.unsigned32 = UINT32_MAX; + max.value.unsigned32 = 0; + break; + case SMI_BASETYPE_UNSIGNED64: + min.value.unsigned64 = LIBSMI_UINT64_MAX; + max.value.unsigned64 = 0; + break; + default: + fprintf(stderr, "smidump: unhandled SMI basetype\n"); + } fprintSegment(f, indent, "\"ranges\" : [\n", 0); for (range = smiGetFirstRange(smiType); range; range = smiGetNextRange(range)) { lastrange = range; - if (range->minValue.value.unsigned32 < min.value.unsigned32) { - min.value.unsigned32 = range->minValue.value.unsigned32; + switch (smiType->basetype) { + case SMI_BASETYPE_INTEGER32: + if (range->minValue.value.integer32 < min.value.integer32) { + min.value.integer32 = range->minValue.value.integer32; + } + if (range->maxValue.value.integer32 > max.value.integer32) { + max.value.integer32 = range->maxValue.value.integer32; + } + break; + case SMI_BASETYPE_INTEGER64: + if (range->minValue.value.integer64 < min.value.integer64) { + min.value.integer64 = range->minValue.value.integer64; + } + if (range->maxValue.value.integer64 > max.value.integer64) { + max.value.integer64 = range->maxValue.value.integer64; + } + break; + case SMI_BASETYPE_UNSIGNED32: + if (range->minValue.value.unsigned32 < min.value.unsigned32) { + min.value.unsigned32 = range->minValue.value.unsigned32; + } + if (range->maxValue.value.unsigned32 > max.value.unsigned32) { + max.value.unsigned32 = range->maxValue.value.unsigned32; + } + break; + case SMI_BASETYPE_UNSIGNED64: + if (range->minValue.value.unsigned64 < min.value.unsigned64) { + min.value.unsigned64 = range->minValue.value.unsigned64; + } + if (range->maxValue.value.unsigned64 > max.value.unsigned64) { + max.value.unsigned64 = range->maxValue.value.unsigned64; + } + break; + default: + break; } - if (range->maxValue.value.unsigned32 > max.value.unsigned32) { - max.value.unsigned32 = range->maxValue.value.unsigned32; - } fprintSegment(f, indent, "{\n", 0); fprintSegment(f, indent + INDENT, "", 0); fprint(f, "\"min\" : \"%s\",\n", Index: dump-yang.c =================================================================== --- dump-yang.c (revision 7637) +++ dump-yang.c (working copy) @@ -28,7 +28,6 @@ * - fix the format strings to xsd pattern algorithm so that it * produces more accurate results * - compute proper boundaries for binary/string length restrictions - * - translate notifications properly (whatever that means ;-) * - handle opaque in a reasonable way (test case AGGREGATE-MIB) */ Index: dump-sming.c =================================================================== --- dump-sming.c (revision 7637) +++ dump-sming.c (working copy) @@ -22,12 +22,13 @@ #include "smi.h" #include "smidump.h" -#include "fprint.h" #define INDENT 2 /* indent factor */ #define INDENTVALUE 20 /* column to start values, except multiline */ +#define INDENTTEXTS 4 /* column to start multiline texts */ +#define INDENTMAX 64 /* max column to fill, break lines otherwise */ @@ -125,12 +126,14 @@ "RFC1213-MIB", "PhysAddress", "IRTF-NMRG-SMING", "PhysAddress", "RFC-1215", "TRAP-TYPE", NULL, NULL, + + /* TODO: how to convert more SMIv1 information? */ NULL, NULL, NULL, NULL }; - +static int current_column = 0; static int silent = 0; @@ -401,9 +404,76 @@ +static void fprint(FILE *f, char *fmt, ...) +{ + va_list ap; + char s[200]; + char *p; + + va_start(ap, fmt); +#ifdef HAVE_VSNPRINTF + current_column += vsnprintf(s, sizeof(s), fmt, ap); +#else + current_column += vsprintf(s, fmt, ap); /* buffer overwrite */ +#endif + va_end(ap); + + fputs(s, f); + + if ((p = strrchr(s, '\n'))) { + current_column = strlen(p) - 1; + } +} + + + +static void fprintSegment(FILE *f, int column, char *string, int length) +{ + fprint(f, "%*c%s", column, ' ', string); + if (length) { + fprint(f, "%*c", length - strlen(string) - column, ' '); + } +} + + + +static void fprintWrapped(FILE *f, int column, char *string) +{ + if ((current_column + strlen(string)) > INDENTMAX) { + putc('\n', f); + current_column = 0; + fprintSegment(f, column, "", 0); + } + fprint(f, "%s", string); +} + + + +static void fprintMultilineString(FILE *f, int column, const char *s) +{ + int i, len; + + fprintSegment(f, column - 1 + INDENTTEXTS, "\"", 0); + if (s) { + len = strlen(s); + for (i=0; i < len; i++) { + putc(s[i], f); + current_column++; + if (s[i] == '\n') { + current_column = 0; + fprintSegment(f, column + INDENTTEXTS, "", 0); + } + } + } + putc('\"', f); + current_column++; +} + + + static char *getValueString(SmiValue *valuePtr, SmiType *typePtr) { - static char s[1024]; + static char s[100]; char ss[9]; int n; unsigned int i; @@ -426,13 +496,19 @@ sprintf(s, INT64_FORMAT, valuePtr->value.integer64); break; case SMI_BASETYPE_FLOAT32: + sprintf(s, "%G", valuePtr->value.float32); + break; case SMI_BASETYPE_FLOAT64: + sprintf(s, "%LG", valuePtr->value.float64); + break; case SMI_BASETYPE_FLOAT128: + sprintf(s, "%LG", valuePtr->value.float128); + break; break; case SMI_BASETYPE_ENUM: for (nn = smiGetFirstNamedNumber(typePtr); nn; nn = smiGetNextNamedNumber(nn)) { - if (nn->value.value.unsigned32 == valuePtr->value.unsigned32) + if (nn->value.value.integer32 == valuePtr->value.integer32) break; } if (nn) { @@ -458,19 +534,20 @@ case SMI_BASETYPE_BITS: sprintf(s, "("); for (i = 0, n = 0; i < valuePtr->len * 8; i++) { - if (valuePtr->value.ptr[i/8] & (1 << i%8)) { + if (valuePtr->value.ptr[i/8] & (1 << (7-(i%8)))) { if (n) sprintf(&s[strlen(s)], ", "); n++; for (nn = smiGetFirstNamedNumber(typePtr); nn; nn = smiGetNextNamedNumber(nn)) { - if (nn->value.value.unsigned32 == i) + //if (nn->value.value.unsigned64 == ((i/8)*8 + (7-(i%8)))) + if (nn->value.value.unsigned64 == i) break; } if (nn) { sprintf(&s[strlen(s)], "%s", nn->name); } else { - sprintf(s, "%d", i); + sprintf(&s[strlen(s)], "%d", i); } } } @@ -479,7 +556,7 @@ case SMI_BASETYPE_UNKNOWN: break; case SMI_BASETYPE_OBJECTIDENTIFIER: - nodePtr = smiGetNodeByOID(valuePtr->len, valuePtr->value.oid); + /*nodePtr = smiGetNodeByOID(valuePtr->len, valuePtr->value.oid); if (nodePtr) { sprintf(s, "%s", nodePtr->name); } else { @@ -488,8 +565,25 @@ if (i) strcat(s, "."); sprintf(&s[strlen(s)], "%u", valuePtr->value.oid[i]); } - } + }*/ + sprintf(s, "%s", typePtr->value.value.ptr); break; + case SMI_BASETYPE_POINTER: + /*nodePtr = smiGetNodeByOID(valuePtr->len, valuePtr->value.oid); + if (nodePtr) { + sprintf(s, "%s", nodePtr->name); + } else { + strcpy(s, ""); + for (i=0; i < valuePtr->len; i++) { + if (i) strcat(s, "."); + sprintf(&s[strlen(s)], "%u", valuePtr->value.oid[i]); + } + }*/ + sprintf(s, "%s", valuePtr->value.ptr); + break; + default: + sprintf(s, ""); + break; } return s; @@ -501,7 +595,7 @@ { SmiRange *range; SmiNamedNumber *nn; - char s[1024]; + char s[100]; int i; if ((smiType->basetype == SMI_BASETYPE_ENUM) || @@ -520,6 +614,10 @@ if (i) { fprint(f, ")"); } + } else if(smiType->basetype == SMI_BASETYPE_POINTER) { + nn = smiGetFirstNamedNumber(smiType); + if(nn) + fprint(f, " (%s)",nn->name); } else { for(i = 0, range = smiGetFirstRange(smiType); range ; i++, range = smiGetNextRange(range)) { @@ -544,22 +642,73 @@ } } +static void fprintAttributeSubtype(FILE *f, SmiAttribute *smiAttribute) +{ + SmiRange *range; + SmiNamedNumber *nn; + char s[100]; + int i; + if ((smiAttribute->basetype == SMI_BASETYPE_ENUM) || + (smiAttribute->basetype == SMI_BASETYPE_BITS)) { + for(i = 0, nn = smiGetAttributeFirstNamedNumber(smiAttribute); + nn ; i++, nn = smiGetAttributeNextNamedNumber(nn)) { + if (i) { + fprint(f, ", "); + } else { + fprint(f, " ("); + } + sprintf(s, "%s(%s)", nn->name, + getValueString(&nn->value, smiGetAttributeParentType(smiAttribute))); + fprintWrapped(f, INDENTVALUE + INDENT, s); + } + if (i) { + fprint(f, ")"); + } + } else if(smiAttribute->basetype == SMI_BASETYPE_POINTER) { + nn = smiGetAttributeFirstNamedNumber(smiAttribute); + if(nn) + fprint(f, " (%s)",nn->name); + } else { + for(i = 0, range = smiGetAttributeFirstRange(smiAttribute); + range ; i++, range = smiGetAttributeNextRange(range)) { + if (i) { + fprint(f, " | "); + } else { + fprint(f, " ("); + } + if (memcmp(&range->minValue, &range->maxValue, + sizeof(SmiValue))) { + sprintf(s, "%s", getValueString(&range->minValue, smiGetAttributeParentType(smiAttribute))); + sprintf(&s[strlen(s)], "..%s", + getValueString(&range->maxValue, smiGetAttributeParentType(smiAttribute))); + } else { + sprintf(s, "%s", getValueString(&range->minValue, smiGetAttributeParentType(smiAttribute))); + } + fprintWrapped(f, INDENTVALUE + INDENT, s); + } + if (i) { + fprint(f, ")"); + } + } +} + + static void fprintImports(FILE *f, SmiModule *smiModule) { - Import *import; + SmiImport *import; char *lastModuleName = NULL; int pos = 0, len, maxlen = 0; createImportList(smiModule); - for (import = importList; import; import = import->nextPtr) { + for (import = smiGetFirstImport(smiModule); import; import = smiGetNextImport(import)) { len = strlen(import->module); maxlen = (len > maxlen) ? len : maxlen; } - for (import = importList; import; import = import->nextPtr) { + for (import = smiGetFirstImport(smiModule); import; import = smiGetNextImport(import)) { int yaba = !lastModuleName || strcmp(import->module, lastModuleName); if (yaba) { if (lastModuleName) { @@ -572,7 +721,7 @@ fprint(f, ", "); } len = strlen(import->name); - if (len + pos > fprint_indent_max) { + if (len + pos > INDENTMAX) { fprint(f, "\n"); fprintSegment(f, INDENT, "", 0); fprintSegment(f, INDENT, "", 0); @@ -662,8 +811,7 @@ fprintSegment(f, 2 * INDENT, "units", INDENTVALUE); fprint(f, "\"%s\";\n", smiType->units); } - if ((smiType->status != SMI_STATUS_CURRENT) && - (smiType->status != SMI_STATUS_UNKNOWN) && + if ((smiType->status != SMI_STATUS_UNKNOWN) && (smiType->status != SMI_STATUS_MANDATORY) && (smiType->status != SMI_STATUS_OPTIONAL)) { fprintSegment(f, 2 * INDENT, "status", INDENTVALUE); @@ -684,8 +832,283 @@ } } +static void fprintIdentities(FILE *f, SmiModule *smiModule) +{ + int i, j; + SmiIdentity *smiIdentity; + SmiIdentity *tmpIdentity; + + for(i = 0, smiIdentity = smiGetFirstIdentity(smiModule); + smiIdentity; smiIdentity = smiGetNextIdentity(smiIdentity)) { + + if (!i && !silent) { + fprint(f, "//\n// IDENTITY DEFINITIONS\n//\n\n"); + } + fprintSegment(f, INDENT, "", 0); + fprint(f, "identity %s {\n", smiIdentity->name); + + if(tmpIdentity = smiGetParentIdentity(smiIdentity)) { + fprintSegment(f, 2 * INDENT, "parent", INDENTVALUE); + fprint(f, "%s", tmpIdentity->name); + fprint(f, ";\n"); + } + if ((smiIdentity->status != SMI_STATUS_UNKNOWN) && + (smiIdentity->status != SMI_STATUS_MANDATORY) && + (smiIdentity->status != SMI_STATUS_OPTIONAL)) { + fprintSegment(f, 2 * INDENT, "status", INDENTVALUE); + fprint(f, "%s;\n", getStringStatus(smiIdentity->status)); + } + fprintSegment(f, 2 * INDENT, "description", INDENTVALUE); + fprint(f, "\n"); + fprintMultilineString(f, 2 * INDENT, smiIdentity->description); + fprint(f, ";\n"); + if (smiIdentity->reference) { + fprintSegment(f, 2 * INDENT, "reference", INDENTVALUE); + fprint(f, "\n"); + fprintMultilineString(f, 2 * INDENT, smiIdentity->reference); + fprint(f, ";\n"); + } + fprintSegment(f, INDENT, "};\n\n", 0); + i++; + } +} +static void fprintExtensions(FILE *f, SmiModule *smiModule) +{ + int i, j; + SmiMacro *smiMacro; + + for(i = 0, smiMacro = smiGetFirstMacro(smiModule); + smiMacro; smiMacro = smiGetNextMacro(smiMacro)) { + + if (!i && !silent) { + fprint(f, "//\n// EXTENSION DEFINITIONS\n//\n\n"); + } + fprintSegment(f, INDENT, "", 0); + fprint(f, "extension %s {\n", smiMacro->name); + + if ((smiMacro->status != SMI_STATUS_UNKNOWN) && + (smiMacro->status != SMI_STATUS_MANDATORY) && + (smiMacro->status != SMI_STATUS_OPTIONAL)) { + fprintSegment(f, 2 * INDENT, "status", INDENTVALUE); + fprint(f, "%s;\n", getStringStatus(smiMacro->status)); + } + fprintSegment(f, 2 * INDENT, "description", INDENTVALUE); + fprint(f, "\n"); + fprintMultilineString(f, 2 * INDENT, smiMacro->description); + fprint(f, ";\n"); + if (smiMacro->reference) { + fprintSegment(f, 2 * INDENT, "reference", INDENTVALUE); + fprint(f, "\n"); + fprintMultilineString(f, 2 * INDENT, smiMacro->reference); + fprint(f, ";\n"); + } + + if(smiMacro->abnf) { + fprintSegment(f, 2 * INDENT, "abnf", INDENTVALUE); + fprint(f, "\n"); + fprintMultilineString(f, 2 * INDENT, smiMacro->abnf); + fprint(f, ";\n"); + } + fprintSegment(f, INDENT, "};\n\n", 0); + i++; + } +} + +static void fprintUniqueStatement(FILE *f, SmiClass *smiClass) +{ + SmiAttribute *attributePtr; + int i; + + if(smiIsClassScalar(smiClass)) + { + fprintSegment(f, 2 * INDENT, "unique", INDENTVALUE); + fprint(f, "();\n"); + } + + attributePtr = smiGetFirstUniqueAttribute(smiClass); + if(attributePtr) + { + fprintSegment(f, 2 * INDENT, "unique", INDENTVALUE); + fprint(f, "("); + for(attributePtr, i=0; attributePtr; + attributePtr = smiGetNextUniqueAttribute(attributePtr)) + { + if(i)fprint(f, ", %s",attributePtr->name); + else fprint(f, "%s",attributePtr->name); + i++; + } + fprint(f, ");\n\n"); + } +} +static void fprintAttributes(FILE *f, SmiClass *smiClass) +{ + int i, j; + SmiAttribute *smiAttribute; + SmiType *tmpType; + SmiClass *tmpClass; + + for(i = 0, smiAttribute = smiGetFirstAttribute(smiClass); + smiAttribute; smiAttribute = smiGetNextAttribute(smiAttribute)) { + /* + if (!i && !silent) { + fprint(f,"\n"); + fprintSegment(f, 2 * INDENT, "// ATTRIBUTE DEFINITIONS\n\n",0); + }*/ + fprintSegment(f, 2*INDENT, "", 0); + fprint(f, "attribute %s {\n", smiAttribute->name); + + if(tmpType = smiGetAttributeParentType(smiAttribute)){ + fprintSegment(f, 3 * INDENT, "type", INDENTVALUE); + fprint(f, "%s ", tmpType->name); + fprintAttributeSubtype(f, smiAttribute); + fprint(f, ";\n"); + fprintSegment(f, 3 * INDENT, "access", INDENTVALUE); + switch (smiAttribute->access) + { + case SMI_ACCESS_READ_ONLY: + fprint(f, "readonly;\n"); + break; + case SMI_ACCESS_READ_WRITE: + fprint(f, "readwrite;\n"); + break; + case SMI_ACCESS_EVENT_ONLY: + fprint(f, "eventonly;\n"); + break; + default: + fprint(f, ";\n"); + break; + } + + if (smiAttribute->value.basetype != SMI_BASETYPE_UNKNOWN) { + fprintSegment(f, 3 * INDENT, "default", INDENTVALUE); + fprint(f, "%s", getValueString(&smiAttribute->value, smiGetAttributeParentType(smiAttribute))); + fprint(f, ";\n"); + } + + if (smiAttribute->format) { + fprintSegment(f, 3 * INDENT, "format", INDENTVALUE); + fprint(f, "\"%s\";\n", smiAttribute->format); + } + if (smiAttribute->units) { + fprintSegment(f, 3 * INDENT, "units", INDENTVALUE); + fprint(f, "\"%s\";\n", smiAttribute->units); + } + } + + if(tmpClass = smiGetAttributeParentClass(smiAttribute)){ + fprintSegment(f, 3 * INDENT, "type", INDENTVALUE); + fprint(f, "%s;\n", tmpClass->name); + } + + if ((smiAttribute->status != SMI_STATUS_UNKNOWN) && + (smiAttribute->status != SMI_STATUS_MANDATORY) && + (smiAttribute->status != SMI_STATUS_OPTIONAL)) { + fprintSegment(f, 3 * INDENT, "status", INDENTVALUE); + fprint(f, "%s;\n", getStringStatus(smiAttribute->status)); + } + fprintSegment(f, 3 * INDENT, "description", INDENTVALUE); + fprint(f, "\n"); + fprintMultilineString(f, 3 * INDENT, smiAttribute->description); + fprint(f, ";\n"); + if (smiAttribute->reference) { + fprintSegment(f, 3 * INDENT, "reference", INDENTVALUE); + fprint(f, "\n"); + fprintMultilineString(f, 3 * INDENT, smiAttribute->reference); + fprint(f, ";\n"); + } + + fprintSegment(f, 2*INDENT, "};\n\n", 0); + i++; + } +} + +static void fprintEvents(FILE *f, SmiClass *smiClass) +{ + int i, j; + SmiEvent *smiEvent; + + for(i = 0, smiEvent = smiGetFirstEvent(smiClass); + smiEvent; smiEvent = smiGetNextEvent(smiEvent)) { + /* + if (!i && !silent) { + fprint(f,"\n"); + fprintSegment(f, 2 * INDENT, "// ATTRIBUTE DEFINITIONS\n\n",0); + }*/ + fprintSegment(f, 2*INDENT, "", 0); + fprint(f, "event %s {\n", smiEvent->name); + + if ((smiEvent->status != SMI_STATUS_UNKNOWN) && + (smiEvent->status != SMI_STATUS_MANDATORY) && + (smiEvent->status != SMI_STATUS_OPTIONAL)) { + fprintSegment(f, 3 * INDENT, "status", INDENTVALUE); + fprint(f, "%s;\n", getStringStatus(smiEvent->status)); + } + fprintSegment(f, 3 * INDENT, "description", INDENTVALUE); + fprint(f, "\n"); + fprintMultilineString(f, 3 * INDENT, smiEvent->description); + fprint(f, ";\n"); + if (smiEvent->reference) { + fprintSegment(f, 3 * INDENT, "reference", INDENTVALUE); + fprint(f, "\n"); + fprintMultilineString(f, 3 * INDENT, smiEvent->reference); + fprint(f, ";\n"); + } + + fprintSegment(f, 2*INDENT, "};\n\n", 0); + i++; + } +} + +static void fprintClasses(FILE *f, SmiModule *smiModule) +{ + int i, j; + SmiClass *smiClass; + SmiClass *tmpClass; + + for(i = 0, smiClass = smiGetFirstClass(smiModule); + smiClass; smiClass = smiGetNextClass(smiClass)) { + + if (!i && !silent) { + fprint(f, "//\n// CLASS DEFINITIONS\n//\n\n"); + } + fprintSegment(f, INDENT, "", 0); + fprint(f, "class %s {\n", smiClass->name); + + if(tmpClass = smiGetParentClass(smiClass)) { + fprintSegment(f, 2 * INDENT, "extends", INDENTVALUE); + fprint(f, "%s;\n\n", tmpClass->name); + } + + fprintAttributes(f,smiClass); + + fprintUniqueStatement(f,smiClass); + + fprintEvents(f,smiClass); + + if ((smiClass->status != SMI_STATUS_UNKNOWN) && + (smiClass->status != SMI_STATUS_MANDATORY) && + (smiClass->status != SMI_STATUS_OPTIONAL)) { + fprintSegment(f, 2 * INDENT, "status", INDENTVALUE); + fprint(f, "%s;\n", getStringStatus(smiClass->status)); + } + fprintSegment(f, 2 * INDENT, "description", INDENTVALUE); + fprint(f, "\n"); + fprintMultilineString(f, 2 * INDENT, smiClass->description); + fprint(f, ";\n"); + if (smiClass->reference) { + fprintSegment(f, 2 * INDENT, "reference", INDENTVALUE); + fprint(f, "\n"); + fprintMultilineString(f, 2 * INDENT, smiClass->reference); + fprint(f, ";\n"); + } + + fprintSegment(f, INDENT, "};\n\n", 0); + i++; + } +} + static void fprintObjects(FILE *f, SmiModule *smiModule) { int i, j; @@ -1232,7 +1655,10 @@ fprint(f, "%s;\n\n", smiNode->name); } + fprintExtensions(f, smiModule); + fprintIdentities(f, smiModule); fprintTypedefs(f, smiModule); + fprintClasses(f, smiModule); fprintObjects(f, smiModule); fprintNotifications(f, smiModule); fprintGroups(f, smiModule);