/*
 * smisubtree - print the names of all nodes below a given OID.
 *
 * Build:  cc -o smisubtree example.c $(pkg-config --cflags --libs libsmi)
 * Usage:  ./smisubtree ifTable
 *         ./smisubtree 1.3.6.1.2.1.2
 */

#include <stdio.h>
#include <stdlib.h>
#include <smi.h>

int main(int argc, char *argv[])
{
    SmiNode *smiNode;
    unsigned int oidlen;

    if (argc != 2) {
        fprintf(stderr, "Usage: smisubtree oid\n");
        return EXIT_FAILURE;
    }

    smiInit("smisubtree");          /* reads smi.conf, [smisubtree] section */
    smiLoadModule("IF-MIB");        /* a few modules to search in */
    smiLoadModule("IP-MIB");

    smiNode = smiGetNode(NULL, argv[1]);
    if (!smiNode) {
        fprintf(stderr, "smisubtree: unknown node `%s'\n", argv[1]);
        smiExit();
        return EXIT_FAILURE;
    }

    oidlen = smiNode->oidlen;
    do {
        char *oid = smiRenderOID(smiNode->oidlen, smiNode->oid, SMI_RENDER_NUMERIC);
        int indent = (int)(smiNode->oidlen - oidlen) * 2;
        printf("%*s%-*s %s\n", indent, "", 32 - indent, smiNode->name, oid);
        smiFree(oid);
        smiNode = smiGetNextNode(smiNode, SMI_NODEKIND_ANY);
    } while (smiNode && smiNode->oidlen > oidlen);

    smiExit();
    return EXIT_SUCCESS;
}
