1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/*
* smilint.c --
*
* MIB module checker main program.
*
* Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig.
*
* See the file "COPYING" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* @(#) $Id: smilint.c 1867 2004-10-06 13:45:31Z strauss $
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_WIN_H
#include "win.h"
#endif
#include "smi.h"
#include "shhopt.h"
/*
* These are functions that are not officially exported by the libsmi.
* See the original prototype definitions in lib/error.h.
*/
extern int smiGetErrorSeverity(int id);
extern char* smiGetErrorTag(int id);
extern char* smiGetErrorMsg(int id);
extern char* smiGetErrorDescription(int id);
static int mFlag = 0; /* show the name for error messages */
static int sFlag = 0; /* show the severity for error messages */
static int eFlag = 0; /* print the list of possible error messages */
static int flags;
typedef struct Error {
int id;
int severity;
char *tag;
char *msg;
char *description;
int used;
} Error;
static Error *errors = NULL;
static void fold(FILE *f, int indent, const char *msg)
{
const char *p, *s;
if (! msg) {
fprintf(f, "\n");
return;
}
for (s = msg; *s; s++) {
for (p = s; *p && *p != '\n'; p++) ;
if (*p) {
fprintf(f, "%.*s\n%*s", p - s, s, indent, "");
s = p;
} else {
fprintf(f, "%.*s\n", p - s, s);
break;
}
}
}
static int compare(const void *v1, const void *v2)
{
Error *err1 = (Error *) v1;
Error *err2 = (Error *) v2;
if (err1->severity < err2->severity) {
return -1;
}
if (err1->severity > err2->severity) {
return 1;
}
return strcmp(err1->msg, err2->msg);
}
static Error* errors_new()
{
int i, cnt;
Error *errors;
for (cnt = 0; smiGetErrorSeverity(cnt) >= 0; cnt++) ;
errors = malloc((cnt + 1) * sizeof(Error));
if (! errors) {
fprintf(stderr, "smilint: malloc failed - running out of memory\n");
exit(1);
}
memset(errors, 0, (cnt + 1) * sizeof(Error));
for (i = 0; i < cnt; i++) {
errors[i].id = i;
errors[i].severity = smiGetErrorSeverity(i);
errors[i].tag = smiGetErrorTag(i);
errors[i].msg = smiGetErrorMsg(i);
errors[i].description = smiGetErrorDescription(i);
}
qsort(errors, cnt, sizeof(Error), compare);
return errors;
}
static void display_one(FILE *f, Error *error)
{
const int indent = 12;
char *type, *tag;
type = (error->severity <= 3) ? "Error:" : "Warning:";
tag = (error->tag && strlen(error->tag))
? error->tag : "<xxx-missing-xxx>";
fprintf(f, "%-*s %s (level %d%s)\n",
indent, type, tag, error->severity & 127,
error->severity & 128 ? ", ignored" : "");
fprintf(f, "%-*s %s\n", indent, "Message:",
error->msg ? error->msg : "");
if (error->description) {
fprintf(f, "%-*s ", indent, "Description:");
fold(f, indent + 1, error->description);
}
}
static void display_all(Error *errors)
{
int i;
for (i = 0; errors[i].msg; i++) {
if (i) printf("\n");
display_one(stdout, errors + i);
}
}
static void display_used(Error *errors)
{
int i, n;
for (i = 0, n = 0; errors[i].msg; i++) {
if (errors[i].used && errors[i].description) n++;
}
if (! n) {
return;
}
fprintf(stderr,
"\nAdditional descriptions of some error/warning messages:\n");
for (i = 0; errors[i].msg; i++) {
if (! errors[i].used || !errors[i].description) continue;
if (i) fprintf(stderr, "\n");
display_one(stderr, errors + i);
}
}
static void usage()
{
fprintf(stderr,
"Usage: smilint [options] [module or path ...]\n"
" -V, --version show version and license information\n"
" -h, --help show usage information\n"
" -c, --config=file load a specific configuration file\n"
" -p, --preload=module preload <module>\n"
" -e, --error-list print list of known error messages\n"
" -m, --error-names print the name of errors in braces\n"
" -s, --severity print the severity of errors in brackets\n"
" -r, --recursive print errors also for imported modules\n"
" -l, --level=level set maximum level of errors and warnings\n"
" -i, --ignore=prefix ignore errors matching prefix pattern\n"
" -I, --noignore=prefix do not ignore errors matching prefix pattern\n");
}
static void help() { usage(); exit(0); }
static void version() { printf("smilint " SMI_VERSION_STRING "\n"); exit(0); }
static void config(char *filename) { smiReadConfig(filename, "smilint"); }
static void preload(char *module) { smiLoadModule(module); }
static void recursive() { flags |= SMI_FLAG_RECURSIVE; smiSetFlags(flags); }
static void level(int lev) { smiSetErrorLevel(lev); }
static void ignore(char *ign) { smiSetSeverity(ign, 128); }
static void noignore(char *ign) { smiSetSeverity(ign, -1); }
static void
errorHandler(char *path, int line, int severity, char *msg, char *tag)
{
int i;
if (path) {
fprintf(stderr, "%s:%d: ", path, line);
}
if (sFlag) {
fprintf(stderr, "[%d] ", severity);
}
if (mFlag) {
fprintf(stderr, "{%s} ", tag);
}
switch (severity) {
case 4:
case 5:
fprintf(stderr, "warning: ");
break;
case 6:
fprintf(stderr, "info: ");
break;
}
fprintf(stderr, "%s\n", msg);
if (severity <= 0) {
exit(1);
}
/* If we are supposed to generate error descriptions, locate this
* error in our error list and increment its usage counter. Note
* that we assume that error tags are unique (and we should better
* check for this somewhere). */
if (errors) {
for (i = 0; errors[i].msg; i++) {
if (strcmp(errors[i].tag, tag) == 0) {
errors[i].used++;
break;
}
}
}
}
int main(int argc, char *argv[])
{
int i;
static optStruct opt[] = {
/* short long type var/func special */
{ 'h', "help", OPT_FLAG, help, OPT_CALLFUNC },
{ 'V', "version", OPT_FLAG, version, OPT_CALLFUNC },
{ 'c', "config", OPT_STRING, config, OPT_CALLFUNC },
{ 'p', "preload", OPT_STRING, preload, OPT_CALLFUNC },
{ 'e', "error-list", OPT_FLAG, &eFlag, 0 },
{ 'm', "error-names", OPT_FLAG, &mFlag, 0 },
{ 's', "severity", OPT_FLAG, &sFlag, 0 },
{ 'r', "recursive", OPT_FLAG, recursive, OPT_CALLFUNC },
{ 'l', "level", OPT_INT, level, OPT_CALLFUNC },
{ 'i', "ignore", OPT_STRING, ignore, OPT_CALLFUNC },
{ 'I', "noignore", OPT_STRING, noignore, OPT_CALLFUNC },
{ 0, 0, OPT_END, 0, 0 } /* no more options */
};
for (i = 1; i < argc; i++)
if ((strstr(argv[i], "-c") == argv[i]) ||
(strstr(argv[i], "--config") == argv[i])) break;
if (i == argc)
smiInit("smilint");
else
smiInit(NULL);
flags = smiGetFlags();
flags |= SMI_FLAG_ERRORS;
flags |= SMI_FLAG_NODESCR;
smiSetFlags(flags);
optParseOptions(&argc, argv, opt, 0);
if (eFlag) {
mFlag = 1;
errors = errors_new();
}
if (sFlag || mFlag) {
smiSetErrorHandler(errorHandler);
}
if (eFlag && argc == 1) {
if (errors) {
display_all(errors);
free(errors);
}
smiExit();
return 0;
}
for (i = 1; i < argc; i++) {
if (smiLoadModule(argv[i]) == NULL) {
fprintf(stderr, "smilint: cannot locate module `%s'\n",
argv[i]);
smiExit();
exit(1);
}
}
if (eFlag) {
if (errors) {
display_used(errors);
free(errors);
}
}
smiExit();
return 0;
}
|