Blame tools/smidump.c

Packit 022b05
/*
Packit 022b05
 * smidump.c --
Packit 022b05
 *
Packit 022b05
 *      Dump a MIB module conforming to a given format.
Packit 022b05
 *
Packit 022b05
 * Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig.
Packit 022b05
 * Copyright (c) 1999 J. Schoenwaelder, Technical University of Braunschweig.
Packit 022b05
 *
Packit 022b05
 * See the file "COPYING" for information on usage and redistribution
Packit 022b05
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Packit 022b05
 *
Packit 022b05
 * @(#) $Id: smidump.c 7870 2008-03-11 19:29:58Z schoenw $
Packit 022b05
 */
Packit 022b05
Packit 022b05
#include <config.h>
Packit 022b05
Packit 022b05
#include <stdio.h>
Packit 022b05
#include <stdlib.h>
Packit 022b05
#include <string.h>
Packit 022b05
#ifdef HAVE_UNISTD_H
Packit 022b05
#include <unistd.h>
Packit 022b05
#endif
Packit 022b05
#ifdef HAVE_WIN_H
Packit 022b05
#include "win.h"
Packit 022b05
#endif
Packit 022b05
Packit 022b05
#include "smi.h"
Packit 022b05
#include "shhopt.h"
Packit 022b05
#include "smidump.h"
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
static void help(void);
Packit 022b05
static void usage(void);
Packit 022b05
static void version(void);
Packit 022b05
static void config(char *filename);
Packit 022b05
static void level(int lev);
Packit 022b05
static void quiet(void);
Packit 022b05
static void preload(char *module);
Packit 022b05
static void unified(void);
Packit 022b05
static void format(char *form);
Packit 022b05
Packit 022b05
Packit 022b05
static int flags;
Packit 022b05
static int mFlag = 0;	/* show the name for error messages */
Packit 022b05
static int sFlag = 0;	/* show the severity for error messages */
Packit 022b05
static int kFlag = 0;	/* keep going after severe errors */
Packit 022b05
static SmidumpDriver *driver;
Packit 022b05
static SmidumpDriver *firstDriver;
Packit 022b05
static SmidumpDriver *lastDriver;
Packit 022b05
static SmidumpDriver *defaultDriver;
Packit 022b05
static char *output;
Packit 022b05
Packit 022b05
static int opts;
Packit 022b05
static optStruct *opt;
Packit 022b05
static optStruct genericOpt[] = {
Packit 022b05
    /* short long              type        var/func       special       */
Packit 022b05
    { 'h', "help",           OPT_FLAG,   help,          OPT_CALLFUNC },
Packit 022b05
    { 'V', "version",        OPT_FLAG,   version,       OPT_CALLFUNC },
Packit 022b05
    { 'c', "config",         OPT_STRING, config,        OPT_CALLFUNC },
Packit 022b05
    { 'l', "level",          OPT_INT,    level,         OPT_CALLFUNC },
Packit 022b05
    { 'p', "preload",        OPT_STRING, preload,       OPT_CALLFUNC },
Packit 022b05
    { 'l', "level",          OPT_INT,    level,         OPT_CALLFUNC },
Packit 022b05
    { 'q', "quiet",          OPT_FLAG,   quiet,         OPT_CALLFUNC },
Packit 022b05
    { 'm', "error-names",    OPT_FLAG,   &mFlag,        0 },
Packit 022b05
    { 's', "severity",       OPT_FLAG,   &sFlag,        0 },
Packit 022b05
    { 'u', "unified",        OPT_FLAG,   unified,       OPT_CALLFUNC },
Packit 022b05
    { 'f', "format",         OPT_STRING, format,        OPT_CALLFUNC },
Packit 022b05
    { 'o', "output",         OPT_STRING, &output,       0            },
Packit 022b05
    { 'k', "keep-going",     OPT_FLAG,	 &kFlag,	0 },
Packit 022b05
    { 0, 0, OPT_END, 0, 0 }  /* no more options */
Packit 022b05
};
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
void *xmalloc(size_t size)
Packit 022b05
{
Packit 022b05
    char *m = malloc(size);
Packit 022b05
    if (! m) {
Packit 022b05
	fprintf(stderr, "smidump: malloc failed - running out of memory\n");
Packit 022b05
	exit(1);
Packit 022b05
    }
Packit 022b05
Packit 022b05
    return m;
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
void *xrealloc(void *ptr, size_t size)
Packit 022b05
{
Packit 022b05
    char *m = realloc(ptr, size);
Packit 022b05
    if (! m) {
Packit 022b05
	fprintf(stderr, "smidump: realloc failed - running out of memory\n");
Packit 022b05
	exit(1);
Packit 022b05
    }
Packit 022b05
    return m;
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
void *xcalloc(size_t nmemb, size_t size)
Packit 022b05
{
Packit 022b05
    char *m = calloc(nmemb, size);
Packit 022b05
    if (! m) {
Packit 022b05
	fprintf(stderr, "smidump: calloc failed - running out of memory\n");
Packit 022b05
	exit(1);
Packit 022b05
    }
Packit 022b05
    return m;
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
char *xstrdup(const char *s)
Packit 022b05
{
Packit 022b05
    char *m = strdup(s);
Packit 022b05
    if (! m) {
Packit 022b05
	fprintf(stderr, "smidump: strdup failed - running out of memory\n");
Packit 022b05
	exit(1);
Packit 022b05
    }
Packit 022b05
    return m;
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
void xfree(void *ptr)
Packit 022b05
{
Packit 022b05
    free(ptr);
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
void smidumpRegisterDriver(SmidumpDriver *driver)
Packit 022b05
{
Packit 022b05
    int i;
Packit 022b05
    
Packit 022b05
    if (!firstDriver) {
Packit 022b05
	firstDriver = driver;
Packit 022b05
	lastDriver = driver;
Packit 022b05
    } else {
Packit 022b05
	lastDriver->next = driver;
Packit 022b05
	lastDriver = driver;
Packit 022b05
    }
Packit 022b05
Packit 022b05
    for (i = 0; driver->opt && driver->opt[i].type != OPT_END; i++) {
Packit 022b05
	opt = xrealloc(opt, (opts+1) * sizeof(optStruct));
Packit 022b05
	memcpy(&opt[opts], &opt[opts-1], sizeof(optStruct));
Packit 022b05
	opt[opts-1].shortName = 0;
Packit 022b05
	opt[opts-1].longName  = xmalloc(strlen(driver->name) +
Packit 022b05
				 strlen(driver->opt[i].name) + 2);
Packit 022b05
	sprintf(opt[opts-1].longName, "%s-%s",
Packit 022b05
		driver->name, driver->opt[i].name);
Packit 022b05
	opt[opts-1].type      = driver->opt[i].type;
Packit 022b05
	opt[opts-1].arg       = driver->opt[i].arg;
Packit 022b05
	opt[opts-1].flags     = driver->opt[i].flags;
Packit 022b05
	opts++;
Packit 022b05
    }
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
static void formats()
Packit 022b05
{
Packit 022b05
    SmidumpDriver *driver = firstDriver;
Packit 022b05
    
Packit 022b05
    for (driver = firstDriver; driver; driver = driver->next) {
Packit 022b05
	fprintf(stderr, "  %-14s: %s\n", driver->name,
Packit 022b05
 		driver->descr ? driver->descr : "...");
Packit 022b05
    }
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
static void usage()
Packit 022b05
{
Packit 022b05
    int i;
Packit 022b05
    SmidumpDriver *driver;
Packit 022b05
    char *value = NULL;
Packit 022b05
#ifdef _MSC_VER
Packit 022b05
    #if _MSC_VER >= 1400
Packit 022b05
/*
Packit 022b05
   %n in printf is a security vulnerability. Ref:
Packit 022b05
   http://en.wikipedia.org/wiki/Format_string_vulnerabilities
Packit 022b05
   MS decided it was important enough to disble it by default. Ref:
Packit 022b05
   "ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vccrt/html/77a854ae-5b48-4865-89f4-f2dc5cf80f52.htm
Packit 022b05
   Calling _set_printf_count_output() stops an invalid parameter crash.
Packit 022b05
*/
Packit 022b05
    int printf_state;
Packit 022b05
    #endif
Packit 022b05
#endif
Packit 022b05
    
Packit 022b05
    fprintf(stderr,
Packit 022b05
	    "Usage: smidump [options] [module or path ...]\n"
Packit 022b05
	    "  -V, --version        show version and license information\n"
Packit 022b05
	    "  -h, --help           show usage information\n"
Packit 022b05
	    "  -q, --quiet          do not generate any comments\n"
Packit 022b05
	    "  -c, --config=file    load a specific configuration file\n"
Packit 022b05
	    "  -p, --preload=module preload <module>\n"
Packit 022b05
	    "  -l, --level=level    set maximum level of errors and warnings\n"
Packit 022b05
	    "  -m, --error-names    print the name of errors in braces\n"
Packit 022b05
	    "  -s, --severity       print the severity of errors in brackets\n"
Packit 022b05
	    "  -f, --format=format  use <format> when dumping (default %s)\n"
Packit 022b05
	    "  -o, --output=name    use <name> when creating names for output files\n"
Packit 022b05
	    "  -u, --unified        print a single unified output of all modules\n"
Packit 022b05
	    "  -k, --keep-going     continue after serious parse errors\n\n",
Packit 022b05
	    defaultDriver ? defaultDriver->name : "none");
Packit 022b05
Packit 022b05
    fprintf(stderr, "Supported formats are:\n");
Packit 022b05
    formats();
Packit 022b05
Packit 022b05
    for (driver = firstDriver; driver; driver = driver->next) {
Packit 022b05
	if (! driver->opt) continue;
Packit 022b05
	fprintf(stderr, "\nSpecific option for the \"%s\" format:\n",
Packit 022b05
		driver->name);
Packit 022b05
	for (i = 0; driver->opt && driver->opt[i].type != OPT_END; i++) {
Packit 022b05
	    int n;
Packit 022b05
	    switch (driver->opt[i].type) {
Packit 022b05
	    case OPT_END:
Packit 022b05
	    case OPT_FLAG:
Packit 022b05
		value = NULL;
Packit 022b05
		break;
Packit 022b05
	    case OPT_STRING:
Packit 022b05
		value = "string";
Packit 022b05
		break;
Packit 022b05
	    case OPT_INT:
Packit 022b05
	    case OPT_UINT:
Packit 022b05
	    case OPT_LONG:
Packit 022b05
	    case OPT_ULONG:
Packit 022b05
		value = "number";
Packit 022b05
		break;
Packit 022b05
	    }
Packit 022b05
#ifdef _MSC_VER
Packit 022b05
    #if _MSC_VER >= 1400
Packit 022b05
	    printf_state=_set_printf_count_output(1);
Packit 022b05
    #endif
Packit 022b05
#endif
Packit 022b05
	    fprintf(stderr, "  --%s-%s%s%s%n",
Packit 022b05
		    driver->name, driver->opt[i].name,
Packit 022b05
		    value ? "=" : "",
Packit 022b05
		    value ? value : "",
Packit 022b05
		    &n);
Packit 022b05
#ifdef _MSC_VER
Packit 022b05
    #if _MSC_VER >= 1400
Packit 022b05
	    (void)_set_printf_count_output(printf_state);
Packit 022b05
    #endif
Packit 022b05
#endif
Packit 022b05
	    fprintf(stderr, "%*s%s\n",
Packit 022b05
		    30-n, "",
Packit 022b05
		    driver->opt[i].descr ? driver->opt[i].descr : "...");
Packit 022b05
	}
Packit 022b05
    }
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
static void help() { usage(); exit(0); }
Packit 022b05
static void version() { printf("smidump " SMI_VERSION_STRING "\n"); exit(0); }
Packit 022b05
static void config(char *filename) { smiReadConfig(filename, "smidump"); }
Packit 022b05
static void level(int lev) { smiSetErrorLevel(lev); }
Packit 022b05
static void quiet() { flags |= SMIDUMP_FLAG_SILENT; }
Packit 022b05
static void preload(char *module) { smiLoadModule(module); }
Packit 022b05
static void unified() { flags |= SMIDUMP_FLAG_UNITE; }
Packit 022b05
Packit 022b05
static void format(char *form)
Packit 022b05
{
Packit 022b05
    for (driver = firstDriver; driver; driver = driver->next) {
Packit 022b05
	if (strcasecmp(driver->name, form) == 0) {
Packit 022b05
	    break;
Packit 022b05
	}
Packit 022b05
    }
Packit 022b05
    if (!driver) {
Packit 022b05
	fprintf(stderr, "smidump: invalid dump format `%s'"
Packit 022b05
		" - supported formats are:\n", form);
Packit 022b05
	formats();
Packit 022b05
	exit(1);
Packit 022b05
    }
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
static void
Packit 022b05
errorHandler(char *path, int line, int severity, char *msg, char *tag)
Packit 022b05
{
Packit 022b05
    if (path) {
Packit 022b05
	fprintf(stderr, "%s:%d: ", path, line);
Packit 022b05
    }
Packit 022b05
    if (sFlag) {
Packit 022b05
	fprintf(stderr, "[%d] ", severity);
Packit 022b05
    }
Packit 022b05
    if (mFlag) {
Packit 022b05
	fprintf(stderr, "{%s} ", tag);
Packit 022b05
    }
Packit 022b05
    switch (severity) {
Packit 022b05
    case 4:
Packit 022b05
    case 5:
Packit 022b05
	fprintf(stderr, "warning: ");
Packit 022b05
	break;
Packit 022b05
    case 6:	
Packit 022b05
	fprintf(stderr, "info: ");
Packit 022b05
	break;
Packit 022b05
    }
Packit 022b05
    fprintf(stderr, "%s\n", msg);
Packit 022b05
Packit 022b05
    if (severity <= 0) {
Packit 022b05
	exit(1);
Packit 022b05
    }
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
int main(int argc, char *argv[])
Packit 022b05
{
Packit 022b05
    char *modulename;
Packit 022b05
    SmiModule *smiModule;
Packit 022b05
    int smiflags, i;
Packit 022b05
    SmiModule **modv = NULL;
Packit 022b05
    int modc = 0;
Packit 022b05
Packit 022b05
    output = NULL;
Packit 022b05
    firstDriver = lastDriver = defaultDriver = NULL;
Packit 022b05
Packit 022b05
    opts = sizeof(genericOpt) / sizeof(optStruct);
Packit 022b05
    opt = xmalloc(sizeof(genericOpt));
Packit 022b05
    memcpy(opt, genericOpt, sizeof(genericOpt));
Packit 022b05
Packit 022b05
    initCm();
Packit 022b05
    initCorba();
Packit 022b05
#if 0
Packit 022b05
    initFig();
Packit 022b05
#endif
Packit 022b05
    initIdentifiers();
Packit 022b05
    initImports();
Packit 022b05
    initJax();
Packit 022b05
    initMetrics();
Packit 022b05
    initMosy();
Packit 022b05
    initNetsnmp();
Packit 022b05
    initPerl();
Packit 022b05
    initPython();
Packit 022b05
    initSming();
Packit 022b05
    initSmi();                defaultDriver = lastDriver;
Packit 022b05
    initSppi();
Packit 022b05
#if 0
Packit 022b05
    initSql();
Packit 022b05
#endif
Packit 022b05
    initScli();
Packit 022b05
    initSvg();
Packit 022b05
    initTree();
Packit 022b05
    initTypes();
Packit 022b05
    initSizes();
Packit 022b05
    initXml();
Packit 022b05
    initXsd();
Packit 022b05
    initCompliances();
Packit 022b05
    initYang();
Packit 022b05
    initBoilerplate();
Packit 022b05
    
Packit 022b05
    for (i = 1; i < argc; i++)
Packit 022b05
	if ((strstr(argv[i], "-c") == argv[i]) ||
Packit 022b05
	    (strstr(argv[i], "--config") == argv[i])) break;
Packit 022b05
    if (i == argc) 
Packit 022b05
	smiInit("smidump");
Packit 022b05
    else
Packit 022b05
	smiInit(NULL);
Packit 022b05
Packit 022b05
    flags = 0;
Packit 022b05
    driver = defaultDriver;
Packit 022b05
Packit 022b05
    optParseOptions(&argc, argv, opt, 0);
Packit 022b05
Packit 022b05
    if (!driver) {
Packit 022b05
	fprintf(stderr, "smidump: no dump formats registered\n");
Packit 022b05
	smiExit();
Packit 022b05
	exit(1);
Packit 022b05
    }
Packit 022b05
    
Packit 022b05
    if (sFlag || mFlag) {
Packit 022b05
	smiSetErrorHandler(errorHandler);
Packit 022b05
    }
Packit 022b05
Packit 022b05
    smiflags = smiGetFlags();
Packit 022b05
    smiflags |= SMI_FLAG_ERRORS;
Packit 022b05
    smiflags |= driver->smiflags;
Packit 022b05
    smiSetFlags(smiflags);
Packit 022b05
Packit 022b05
    if (flags & SMIDUMP_FLAG_UNITE && driver->ignflags & SMIDUMP_DRIVER_CANT_UNITE) {
Packit 022b05
	fprintf(stderr, "smidump: %s format does not support united output:"
Packit 022b05
		" ignoring -u\n", driver->name);
Packit 022b05
	flags = (flags & ~SMIDUMP_FLAG_UNITE);
Packit 022b05
    }
Packit 022b05
Packit 022b05
    if (output && driver->ignflags & SMIDUMP_DRIVER_CANT_OUTPUT) {
Packit 022b05
	fprintf(stderr, "smidump: %s format does not support output option:"
Packit 022b05
		" ignoring -o %s\n", driver->name, output);
Packit 022b05
	output = NULL;
Packit 022b05
    }
Packit 022b05
Packit 022b05
    modv = (SmiModule **) xmalloc((argc) * sizeof(SmiModule *));
Packit 022b05
    modc = 0;
Packit 022b05
    
Packit 022b05
    for (i = 1; i < argc; i++) {
Packit 022b05
	modulename = smiLoadModule(argv[i]);
Packit 022b05
	smiModule = modulename ? smiGetModule(modulename) : NULL;
Packit 022b05
	if (smiModule) {
Packit 022b05
	    if ((smiModule->conformance) && (smiModule->conformance < 3)) {
Packit 022b05
		flags |= SMIDUMP_FLAG_ERROR;
Packit 022b05
		if (! (flags & SMIDUMP_FLAG_SILENT)) {
Packit 022b05
		    fprintf(stderr,
Packit 022b05
			    "smidump: module `%s' contains errors, "
Packit 022b05
			    "expect flawed output\n",
Packit 022b05
			    argv[i]);
Packit 022b05
		}
Packit 022b05
	    }
Packit 022b05
	    modv[modc++] = smiModule;
Packit 022b05
	} else {
Packit 022b05
	    fprintf(stderr, "smidump: cannot locate module `%s'\n",
Packit 022b05
		    argv[i]);
Packit 022b05
	}
Packit 022b05
    }
Packit 022b05
Packit 022b05
    if (! (flags & SMIDUMP_FLAG_ERROR) || kFlag) {
Packit 022b05
	(driver->func)(modc, modv, flags, output);
Packit 022b05
    } else {
Packit 022b05
	if (! (flags & SMIDUMP_FLAG_SILENT)) {
Packit 022b05
	    fprintf(stderr,
Packit 022b05
		    "smidump: aborting due to severe parsing errors\n"
Packit 022b05
		    "smidump: use the -k option to force continuation\n");
Packit 022b05
	}
Packit 022b05
    }
Packit 022b05
Packit 022b05
    smiExit();
Packit 022b05
Packit 022b05
    if (modv) xfree(modv);
Packit 022b05
    
Packit 022b05
    return 0;
Packit 022b05
}