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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
/*
* dump-tree.c --
*
* Operations to dump the OID tree in a human readable format.
*
* Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig.
* Copyright (c) 1999 J. Schoenwaelder, Technical University of Braunschweig.
* Copyright (c) 2002 J. Schoenwaelder, University of Osnabrueck.
*
* See the file "COPYING" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* @(#) $Id: dump-tree.c 8090 2008-04-18 12:56:29Z strauss $
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include "smi.h"
#include "smidump.h"
static int pmodc = 0;
static SmiModule **pmodv = NULL;
static int ignoreconformance = 0;
static int ignoreleafs = 0;
static int full = 0;
static int compact = 0;
static char *getFlags(SmiNode *smiNode)
{
switch (smiNode->access) {
case SMI_ACCESS_UNKNOWN:
return "---";
case SMI_ACCESS_NOT_ACCESSIBLE:
return "---";
case SMI_ACCESS_EVENT_ONLY:
return "---";
case SMI_ACCESS_NOTIFY:
return "--n";
case SMI_ACCESS_READ_ONLY:
return "r-n";
case SMI_ACCESS_READ_WRITE:
return "rwn";
case SMI_ACCESS_NOT_IMPLEMENTED:
return "---";
case SMI_ACCESS_INSTALL:
return "-i-";
case SMI_ACCESS_INSTALL_NOTIFY:
return "-in";
case SMI_ACCESS_REPORT_ONLY:
return "--r";
}
return "";
}
static char getStatusChar(SmiStatus status)
{
switch (status) {
case SMI_STATUS_UNKNOWN:
return '+';
case SMI_STATUS_CURRENT:
return '+';
case SMI_STATUS_DEPRECATED:
return 'x';
case SMI_STATUS_MANDATORY:
return '+';
case SMI_STATUS_OPTIONAL:
return '+';
case SMI_STATUS_OBSOLETE:
return 'o';
}
return ' ';
}
static char *getTypeName(SmiNode *smiNode)
{
char *type;
SmiType *smiType, *parentType;
smiType = smiGetNodeType(smiNode);
if (!smiType || smiNode->nodekind == SMI_NODEKIND_TABLE)
return NULL;
if (smiType->decl == SMI_DECL_IMPLICIT_TYPE) {
parentType = smiGetParentType(smiType);
if (!parentType)
return NULL;
smiType = parentType;
}
type = xstrdup(smiType->name);
return type;
}
static void fprintIndex(FILE *f, SmiNode *smiNode)
{
char *indexname;
int i;
SmiElement *smiElement;
indexname = NULL;
for (i = -1, smiElement = smiGetFirstElement(smiNode);
smiElement; smiElement = smiGetNextElement(smiElement), i++) {
if (i > 0) fprintf(f, ",");
if (indexname) {
fprintf(f, "%s", indexname);
}
indexname = smiGetElementNode(smiElement)->name;
}
if (indexname) {
fprintf(f, "%s%s%s",
(i > 0) ? "," : "",
(smiNode->implied) ? "*" : "",
indexname);
}
}
static void fprintObjects(FILE *f, SmiNode *smiNode)
{
char *objectname;
int i;
SmiElement *smiElement;
objectname = NULL;
for (i = -1, smiElement = smiGetFirstElement(smiNode);
smiElement;
smiElement = smiGetNextElement(smiElement), i++) {
if (i > 0) fprintf(f, ",");
if (objectname) {
fprintf(f, "%s", objectname);
}
objectname = smiGetElementNode(smiElement)->name;
}
if (objectname) {
fprintf(f, "%s%s", (i > 0) ? "," : "", objectname);
}
}
static int isPartOfLoadedModules(SmiNode *smiNode)
{
SmiModule *smiModule;
int i;
smiModule = smiGetNodeModule(smiNode);
for (i = 0; i < pmodc; i++) {
if (strcmp(pmodv[i]->name, smiModule->name) == 0) {
return 1;
}
}
return 0;
}
/*
* The following function pruneSubTree() is tricky. There are some
* interactions between the supported options. See the detailed
* comments below. Good examples to test the implemented behaviour
* are:
*
* smidump -u -f tree --tree-no-leaf IF-MIB ETHER-CHIPSET-MIB
*
* (And the example above does _not_ work in combination with
* --tree-no-conformance so the code below is still broken.)
*/
static int pruneSubTree(SmiNode *smiNode)
{
SmiNode *childNode;
const int confmask = (SMI_NODEKIND_GROUP | SMI_NODEKIND_COMPLIANCE);
const int leafmask = (SMI_NODEKIND_GROUP | SMI_NODEKIND_COMPLIANCE
| SMI_NODEKIND_COLUMN | SMI_NODEKIND_SCALAR
| SMI_NODEKIND_ROW | SMI_NODEKIND_NOTIFICATION);
if (! smiNode) {
return 1;
}
/*
* First, prune all nodes which the user has told us to ignore.
* In the case of ignoreleafs, we have to special case nodes with
* an unknown status (which actually represent OBJECT-IDENTITY
* definitions). More special case code is needed to exclude
* module identity nodes.
*/
if (ignoreconformance && (smiNode->nodekind & confmask)) {
return 1;
}
if (ignoreleafs) {
if (smiNode->nodekind & leafmask) {
return 1;
}
if (smiNode->nodekind == SMI_NODEKIND_NODE
&& smiNode->status != SMI_STATUS_UNKNOWN) {
SmiModule *smiModule = smiGetNodeModule(smiNode);
if (smiModule && smiNode != smiGetModuleIdentityNode(smiModule)) {
return 1;
}
}
}
/*
* Next, generally do not prune nodes that belong to the set of
* modules we are looking at.
*/
if (isPartOfLoadedModules(smiNode)) {
if (!ignoreconformance || !smiGetFirstChildNode(smiNode)) {
return 0;
}
}
/*
* Finally, prune all nodes where all child nodes are pruned.
*/
for (childNode = smiGetFirstChildNode(smiNode);
childNode;
childNode = smiGetNextChildNode(childNode)) {
/*
* In the case of ignoreleafs, we have to peek at the child
* nodes. Otherwise, we would prune too much. we still want to
* see the path to the leafs we have pruned away. This also
* interact with the semantics of ignoreconformance since we
* still want in combination with ignoreleafs to see the path
* to the pruned conformance leafs.
*/
if (ignoreleafs && (childNode->nodekind & leafmask)) {
if (isPartOfLoadedModules(childNode)) {
if (ignoreconformance && (childNode->nodekind & confmask)) {
return 1;
}
return 0;
}
}
if (! pruneSubTree(childNode)) {
return 0;
}
}
return 1;
}
static void fprintSubTree(FILE *f, SmiNode *smiNode,
char *prefix, size_t typefieldlen)
{
SmiNode *childNode, *indexNode;
SmiNodekind lastNodeKind = SMI_NODEKIND_UNKNOWN;
SmiType *type;
int i = 0, cnt, prefixlen;
size_t newtypefieldlen = 9;
char c = 0;
char *type_name;
if (smiNode) {
prefixlen = strlen(prefix);
switch (smiNode->nodekind) {
case SMI_NODEKIND_SCALAR:
case SMI_NODEKIND_COLUMN:
if (prefixlen > 0) {
c = prefix[prefixlen-1];
prefix[prefixlen-1] = getStatusChar(smiNode->status);
}
type_name = getTypeName(smiNode);
if (type_name) {
fprintf(f, "%s-- %s %-*s %s(%u)\n",
prefix,
getFlags(smiNode),
typefieldlen,
type_name,
smiNode->name,
smiNode->oid[smiNode->oidlen-1]);
xfree(type_name);
}
if (prefixlen > 0 && c) {
prefix[prefixlen-1] = c;
}
break;
case SMI_NODEKIND_ROW:
if (prefixlen > 0) {
c = prefix[prefixlen-1];
prefix[prefixlen-1] = getStatusChar(smiNode->status);
}
fprintf(f, "%s--%s(%u) [", prefix,
smiNode->name,
smiNode->oid[smiNode->oidlen-1]);
switch (smiNode->indexkind) {
case SMI_INDEX_INDEX:
case SMI_INDEX_REORDER:
fprintIndex(f, smiNode);
break;
case SMI_INDEX_EXPAND: /* TODO: we have to do more work here! */
break;
case SMI_INDEX_AUGMENT:
case SMI_INDEX_SPARSE:
indexNode = smiGetRelatedNode(smiNode);
if (indexNode) {
fprintIndex(f, indexNode);
}
break;
case SMI_INDEX_UNKNOWN:
break;
}
fprintf(f, "]\n");
if (prefixlen > 0 && c) {
prefix[prefixlen-1] = c;
}
break;
case SMI_NODEKIND_NOTIFICATION:
if (prefixlen > 0) {
c = prefix[prefixlen-1];
prefix[prefixlen-1] = getStatusChar(smiNode->status);
}
fprintf(f, "%s--%s(%u) [", prefix,
smiNode->name,
smiNode->oid[smiNode->oidlen-1]);
fprintObjects(f, smiNode);
fprintf(f, "]\n");
if (prefixlen > 0 && c) {
prefix[prefixlen-1] = c;
}
break;
default:
if (prefixlen > 0) {
c = prefix[prefixlen-1];
prefix[prefixlen-1] = getStatusChar(smiNode->status);
}
if (smiNode->oid)
if (prefixlen > 0) {
fprintf(f, "%s--%s(%u)\n", prefix,
smiNode->name ? smiNode->name : " ",
smiNode->oid[smiNode->oidlen-1]);
} else {
unsigned int j;
fprintf(f, "%s--%s(", prefix,
smiNode->name ? smiNode->name : " ");
for (j = 0; j < smiNode->oidlen; j++) {
fprintf(f, "%s%u", j ? "." : "", smiNode->oid[j]);
}
fprintf(f, ")\n");
}
else
fprintf(f, "%s--%s(?)\n", prefix,
smiNode->name ? smiNode->name : " ");
if (prefixlen > 0 && c) {
prefix[prefixlen-1] = c;
}
}
for (childNode = smiGetFirstChildNode(smiNode), cnt = 0;
childNode;
childNode = smiGetNextChildNode(childNode)) {
if (! pruneSubTree(childNode)) {
type = smiGetNodeType(childNode);
if (type) {
type_name = getTypeName(childNode);
if (type_name) {
if (strlen(type_name) > newtypefieldlen) {
newtypefieldlen = strlen(type_name);
}
xfree(type_name);
}
}
cnt++;
}
}
for (childNode = smiGetFirstChildNode(smiNode);
childNode;
childNode = smiGetNextChildNode(childNode)) {
char *newprefix;
if (pruneSubTree(childNode)) {
continue;
}
i++;
if (! compact &&
((childNode->nodekind != SMI_NODEKIND_COLUMN
&& childNode->nodekind != SMI_NODEKIND_SCALAR)
|| (lastNodeKind != childNode->nodekind))) {
fprintf(f, "%s |\n", prefix);
}
newprefix = xmalloc(strlen(prefix)+10);
strcpy(newprefix, prefix);
if (cnt == 1 || cnt == i) {
strcat(newprefix, " ");
} else {
strcat(newprefix, " |");
}
fprintSubTree(f, childNode, newprefix, newtypefieldlen);
xfree(newprefix);
lastNodeKind = childNode->nodekind;
}
}
}
static void fprintTree(FILE *f)
{
SmiNode *smiNode;
SmiNode *childNode;
SmiNode *nextNode;
int cnt;
smiNode = smiGetNode(NULL, "iso");
if (! full) {
do {
for (childNode = smiGetFirstChildNode(smiNode), cnt = 0, nextNode = NULL;
childNode;
childNode = smiGetNextChildNode(childNode)) {
if (! pruneSubTree(childNode)) {
cnt++;
if (! nextNode) {
nextNode = childNode;
}
}
}
if (cnt == 1) {
smiNode = nextNode;
}
} while (cnt == 1);
}
if (smiNode) {
fprintSubTree(f, smiNode, "", 0);
}
}
static void dumpTree(int modc, SmiModule **modv, int flags, char *output)
{
int i;
FILE *f = stdout;
if (output) {
f = fopen(output, "w");
if (!f) {
fprintf(stderr, "smidump: cannot open %s for writing: ", output);
perror(NULL);
exit(1);
}
}
if (flags & SMIDUMP_FLAG_UNITE) {
pmodc = modc;
pmodv = modv;
if (! (flags & SMIDUMP_FLAG_SILENT)) {
fprintf(f, "# united registration tree (generated by smidump "
SMI_VERSION_STRING ")\n\n");
}
if (! (flags & SMIDUMP_FLAG_SILENT) && (flags & SMIDUMP_FLAG_ERROR)) {
fprintf(f, "# WARNING: this output may be incorrect due to "
"significant parse errors\n\n");
}
fprintTree(f);
} else {
for (i = 0; i < modc; i++) {
pmodc = 1;
pmodv = &(modv[i]);
if (! (flags & SMIDUMP_FLAG_SILENT)) {
fprintf(f, "# %s registration tree (generated by smidump "
SMI_VERSION_STRING ")\n\n", modv[i]->name);
}
if (! (flags & SMIDUMP_FLAG_SILENT) && (flags & SMIDUMP_FLAG_ERROR)) {
fprintf(f, "# WARNING: this output may be incorrect due to "
"significant parse errors\n\n");
}
fprintTree(f);
}
}
if (fflush(f) || ferror(f)) {
perror("smidump: write error");
exit(1);
}
if (output) {
fclose(f);
}
}
void initTree()
{
static SmidumpDriverOption opt[] = {
{ "no-conformance", OPT_FLAG, &ignoreconformance, 0,
"do not show conformance nodes"},
{ "no-leafs", OPT_FLAG, &ignoreleafs, 0,
"do not show leaf nodes"},
{ "full-root", OPT_FLAG, &full, 0,
"generate the full path to the root"},
{ "compact", OPT_FLAG, &compact, 0,
"generate a more compact representation"},
{ 0, OPT_END, 0, 0 }
};
static SmidumpDriver driver = {
"tree",
dumpTree,
SMI_FLAG_NODESCR,
0,
"structure of the OID tree",
opt,
NULL
};
smidumpRegisterDriver(&driver);
}
|