Index: smiquery.c =================================================================== --- smiquery.c (revision 57131) +++ smiquery.c (working copy) @@ -28,29 +28,47 @@ #include "smi.h" #include "shhopt.h" +static const int indent = 12; -static char *format(const char *s) +static void +print_key(const char *s) { - static char ss[20000]; - int i, j; + if (s) { + printf("%*s: ", indent, s); + } else { + printf("%*s ", indent, ""); + } +} + +static void +print_val(const char *s) +{ + const char *p; + if (!s) { - sprintf(ss, "-"); + printf("-"); } else { - for(i = 0, j = 0; s[i]; i++) { - ss[j++] = s[i]; - if (s[i] == '\n') { - sprintf(&ss[j], " "); - j += 14; + for (p = s; *p; p++) { + putc(*p, stdout); + if (*p == '\n') { + print_key(NULL); } } - ss[j] = 0; } - return ss; + putc('\n', stdout); } +static void +print_key_val(const char *key, const char *val) +{ + print_key(key); + print_val(val); +} + + static void usage() { fprintf(stderr, @@ -128,43 +146,43 @@ module = smiGetModule(name); if (module) { node = smiGetModuleIdentityNode(module); - printf(" Module: %s\n", format(module->name)); + print_key_val("Module", module->name); if (module->path) - printf(" Pathname: %s\n", module->path); + print_key_val("Pathname", module->path); if (node) - printf(" Object: %s\n", - smiRenderNode(node, SMI_RENDER_ALL)); + print_key_val("Object", + smiRenderNode(node, SMI_RENDER_ALL)); if (module->organization) - printf("Organization: %s\n", format(module->organization)); + print_key_val("Organization", module->organization); if (module->contactinfo) - printf(" ContactInfo: %s\n", format(module->contactinfo)); + print_key_val("ContactInfo", module->contactinfo); if (module->description) - printf(" Description: %s\n", format(module->description)); + print_key_val("Description", module->description); if (module->reference) - printf(" Reference: %s\n", format(module->reference)); - printf(" Language: %s\n", smiLanguageAsString(module->language)); - printf(" Conformance: %d\n", module->conformance); - printf(" Loaded: %s\n", smiIsLoaded(name) ? "yes" : "no"); + print_key_val("Reference", module->reference); + print_key_val("Language", + smiLanguageAsString(module->language)); + print_key("Conformance"); printf("%d\n", module->conformance); + print_key_val("Loaded", smiIsLoaded(name) ? "yes" : "no"); for(revision = smiGetFirstRevision(module); revision ; revision = smiGetNextRevision(revision)) { - printf(" Revision: %s", ctime(&revision->date)); + print_key_val("Revision", ctime(&revision->date)); if (revision->description) - printf(" Description: %s\n", format(revision->description)); + print_key_val("Description", revision->description); } } } else if (!strcmp(command, "imports")) { module = smiGetModule(name); if (module && smiGetFirstImport(module)) { - printf(" Imports:"); - for(import = smiGetFirstImport(module); import ; ) { - printf(" %s::%s", import->module, import->name); + print_key("Imports"); + for(import = smiGetFirstImport(module); import; ) { + printf("%s::%s\n", import->module, import->name); import = smiGetNextImport(import); if (import) { - printf("\n "); + print_key(NULL); } } - printf("\n"); } } else if (!strcmp(command, "node")) { node = smiGetNode(NULL, name); @@ -173,30 +191,29 @@ } if (node) { type = smiGetNodeType(node); - printf(" MibNode: %s\n", smiRenderNode(node, SMI_RENDER_ALL)); - printf(" OID: %s\n", smiRenderOID(node->oidlen, node->oid, - 0)); + print_key_val("MibNode", smiRenderNode(node, SMI_RENDER_ALL)); + print_key_val("OID", smiRenderOID(node->oidlen, node->oid, 0)); for (node2 = smiGetFirstAlias(node); node2; node2 = smiGetNextAlias(node2)) { if (node2 == node) continue; - printf(" Alias: %s\n", smiRenderNode(node2, SMI_RENDER_ALL)); + print_key_val("Alias", + smiRenderNode(node2, SMI_RENDER_ALL)); } if (parent) - printf(" ParentNode: %s\n", - smiRenderNode(parent, SMI_RENDER_ALL)); + print_key_val("ParentNode", + smiRenderNode(parent, SMI_RENDER_ALL)); if (type) - printf(" Type: %s\n", - smiRenderType(type, SMI_RENDER_ALL)); + print_key_val("Type", smiRenderType(type, SMI_RENDER_ALL)); if (node->value.basetype != SMI_BASETYPE_UNKNOWN) - printf(" Default: %s\n", smiRenderValue(&node->value, type, - SMI_RENDER_ALL)); + print_key_val("Default", + smiRenderValue(&node->value, type, SMI_RENDER_ALL)); if (node->decl != SMI_DECL_UNKNOWN) - printf(" Declaration: %s\n", smiDeclAsString(node->decl)); - printf(" NodeKind: %s\n", smiNodekindAsString(node->nodekind)); + print_key_val("Declaration", smiDeclAsString(node->decl)); + print_key_val("NodeKind", smiNodekindAsString(node->nodekind)); if (node->nodekind == SMI_NODEKIND_ROW) { - printf (" Creatable: %s\n", node->create ? "yes" : "no"); - printf (" Implied: %s\n", node->implied ? "yes" : "no"); + print_key_val("Creatable", node->create ? "yes" : "no"); + print_key_val("Implied", node->implied ? "yes" : "no"); } switch (node->nodekind) { case SMI_NODEKIND_ROW: p = "Index"; break; @@ -207,55 +224,53 @@ default: p = "Elements"; } if (smiGetFirstElement(node)) { - printf("%12s:", p); + print_key(p); for(element = smiGetFirstElement(node); - element ; ) { + element; ) { node2 = smiGetElementNode(element); - printf(" %s", smiRenderNode(node2, SMI_RENDER_ALL)); + print_val(smiRenderNode(node2, SMI_RENDER_ALL)); element = smiGetNextElement(element); if (element) { - printf("\n "); + print_key(NULL); } } - printf("\n"); } if (node->access != SMI_ACCESS_UNKNOWN) - printf(" Access: %s\n", smiAccessAsString(node->access)); + print_key_val("Access", smiAccessAsString(node->access)); if (node->status != SMI_STATUS_UNKNOWN) - printf(" Status: %s\n", smiStatusAsString(node->status)); + print_key_val("Status", smiStatusAsString(node->status)); if (node->format) - printf(" Format: %s\n", format(node->format)); + print_key_val("Format", node->format); if (node->units) - printf(" Units: %s\n", format(node->units)); + print_key_val("Units", node->units); if (node->description) - printf(" Description: %s\n", format(node->description)); + print_key_val("Description", node->description); if (node->reference) - printf(" Reference: %s\n", format(node->reference)); + print_key_val("Reference", node->reference); } } else if (!strcmp(command, "compliance")) { node = smiGetNode(NULL, name); if (node) { if (smiGetFirstElement(node)) { - printf(" Mandatory:"); + print_key("Mandatory"); for(element = smiGetFirstElement(node); element ; ) { node2 = smiGetElementNode(element); - printf(" %s", smiRenderNode(node2, SMI_RENDER_ALL)); + print_val(smiRenderNode(node2, SMI_RENDER_ALL)); element = smiGetNextElement(element); if (element) { - printf("\n "); + print_key(NULL); } } - printf("\n"); } if (smiGetFirstOption(node)) { for(option = smiGetFirstOption(node); option ;) { node2 = smiGetOptionNode(option); - printf(" Option: %s\n", - smiRenderNode(node2, SMI_RENDER_ALL)); + print_key_val("Option", + smiRenderNode(node2, SMI_RENDER_ALL)); if (option->description) - printf(" Description: %s\n", - format(option->description)); + print_key_val("Description", + option->description); option = smiGetNextOption(option); } } @@ -264,42 +279,40 @@ refinement ; refinement = smiGetNextRefinement(refinement)) { node2 = smiGetRefinementNode(refinement); - printf(" Refinement: %s\n", - smiRenderNode(node2, SMI_RENDER_ALL)); + print_key_val("Refinement", + smiRenderNode(node2, SMI_RENDER_ALL)); type = smiGetRefinementType(refinement); if (type) { - printf(" Type: %s\n", - smiRenderType(type, SMI_RENDER_ALL)); + print_key_val("Type", + smiRenderType(type, SMI_RENDER_ALL)); } type = smiGetRefinementWriteType(refinement); if (type) { module = smiGetTypeModule(type); - printf(" Write-Type: %s\n", - smiRenderType(type, SMI_RENDER_ALL)); + print_key_val("Write-Type", + smiRenderType(type, SMI_RENDER_ALL)); } if (refinement->access != SMI_ACCESS_UNKNOWN) { - printf(" Access: %s\n", - smiAccessAsString(refinement->access)); + print_key_val("Access", + smiAccessAsString(refinement->access)); } if (refinement->description) - printf(" Description: %s\n", - format(refinement->description)); + print_key_val("Description", + refinement->description); } } } } else if (!strcmp(command, "children")) { node = smiGetNode(NULL, name); if (node && smiGetFirstChildNode(node)) { - printf(" Children:"); - for(child = smiGetFirstChildNode(node); - child ; ) { - printf(" %s", smiRenderNode(child, SMI_RENDER_ALL)); + print_key("Children"); + for(child = smiGetFirstChildNode(node); child ; ) { + print_val(smiRenderNode(child, SMI_RENDER_ALL)); child = smiGetNextChildNode(child); if (child) { - printf("\n "); + print_key(NULL); } } - printf("\n"); } } else if (!strcmp(command, "type")) { p = strrchr(name, ':'); @@ -323,60 +336,65 @@ } if (type) { parenttype = smiGetParentType(type); - printf(" Type: %s\n", smiRenderType(type, SMI_RENDER_ALL)); - printf(" Basetype: %s\n", smiBasetypeAsString(type->basetype)); + print_key_val("Type", smiRenderType(type, SMI_RENDER_ALL)); + print_key_val("Basetype", smiBasetypeAsString(type->basetype)); if (parenttype) - printf(" Parent Type: %s\n", - smiRenderType(parenttype, SMI_RENDER_ALL)); + print_key_val("Parent Type", + smiRenderType(parenttype, SMI_RENDER_ALL)); if (type->value.basetype != SMI_BASETYPE_UNKNOWN) - printf(" Default: %s\n", smiRenderValue(&type->value, type, - SMI_RENDER_ALL)); + print_key_val("Default", + smiRenderValue(&type->value, type, SMI_RENDER_ALL)); if ((type->basetype == SMI_BASETYPE_ENUM) || (type->basetype == SMI_BASETYPE_BITS)) { if (smiGetFirstNamedNumber(type)) { - printf(" Numbers:"); - for(nn = smiGetFirstNamedNumber(type); - nn ; nn = smiGetNextNamedNumber(nn)) { - printf(" %s(%ld)", + print_key("Numbers"); + for(nn = smiGetFirstNamedNumber(type); nn; ) { + printf("%s(%ld)\n", nn->name, nn->value.value.integer32); + nn = smiGetNextNamedNumber(nn); + if (nn) { + print_key(NULL); + } } - printf("\n"); } } else { if (smiGetFirstRange(type)) { - printf(" Ranges:"); - for(range = smiGetFirstRange(type); - range ; range = smiGetNextRange(range)) { + print_key("Ranges"); + for(range = smiGetFirstRange(type); range; ) { strcpy(s1, smiRenderValue(&range->minValue, type, 0)); strcpy(s2, smiRenderValue(&range->maxValue, type, 0)); - printf(" %s", s1); + printf("%s", s1); if (strcmp(s1, s2)) printf("..%s", s2); + printf("\n"); + range = smiGetNextRange(range); + if (range) { + print_key(NULL); + } } - printf("\n"); } } - printf(" Declaration: %s\n", smiDeclAsString(type->decl)); + print_key_val("Declaration", smiDeclAsString(type->decl)); if (type->status != SMI_STATUS_UNKNOWN) - printf(" Status: %s\n", smiStatusAsString(type->status)); + print_key_val("Status", smiStatusAsString(type->status)); if (type->format) - printf(" Format: %s\n", format(type->format)); + print_key_val("Format", type->format); if (type->units) - printf(" Units: %s\n", format(type->units)); + print_key_val("Units", type->units); if (type->description) - printf(" Description: %s\n", format(type->description)); + print_key_val("Description", type->description); if (type->reference) - printf(" Reference: %s\n", format(type->reference)); + print_key_val("Reference", type->reference); } } else if (!strcmp(command, "macro")) { macro = smiGetMacro(NULL, name); if (macro) { - printf(" Macro: %s\n", format(macro->name)); + print_key_val("Macro", macro->name); if (macro->status != SMI_STATUS_UNKNOWN) - printf(" Status: %s\n", smiStatusAsString(macro->status)); + print_key_val("Status", smiStatusAsString(macro->status)); if (macro->description) - printf(" Description: %s\n", format(macro->description)); + print_key_val("Description", macro->description); if (macro->reference) - printf(" Reference: %s\n", format(macro->reference)); + print_key_val("Reference", macro->reference); } } else { usage();