4 * Operations to dump MIB modules in the MOSY output format.
6 * Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig.
7 * Copyright (c) 1999 J. Schoenwaelder, Technical University of Braunschweig.
9 * See the file "COPYING" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 * @(#) $Id: dump-mosy.c 8090 2008-04-18 12:56:29Z strauss $
24 static char *ignoreTypeRanges[] = {
33 static char *getStatusString(SmiStatus status)
36 (status == SMI_STATUS_CURRENT) ? "current" :
37 (status == SMI_STATUS_DEPRECATED) ? "deprecated" :
38 (status == SMI_STATUS_OBSOLETE) ? "obsolete" :
39 (status == SMI_STATUS_MANDATORY) ? "mandatory" :
40 (status == SMI_STATUS_OPTIONAL) ? "optional" :
45 static char *getAccessString(SmiAccess access, int create)
48 (create && (access == SMI_ACCESS_READ_WRITE)) ? "read-create" :
49 (access == SMI_ACCESS_NOT_ACCESSIBLE) ? "not-accessible" :
50 (access == SMI_ACCESS_NOTIFY) ? "accessible-for-notify" :
51 (access == SMI_ACCESS_READ_ONLY) ? "read-only" :
52 (access == SMI_ACCESS_READ_WRITE) ? "read-write" :
57 static char *getBasetypeString(SmiBasetype basetype)
60 (basetype == SMI_BASETYPE_UNKNOWN) ? "<UNKNOWN>" :
61 (basetype == SMI_BASETYPE_OCTETSTRING) ? "OctetString" :
62 (basetype == SMI_BASETYPE_OBJECTIDENTIFIER) ? "ObjectID" :
63 (basetype == SMI_BASETYPE_UNSIGNED32) ? "Unsigned32" :
64 (basetype == SMI_BASETYPE_INTEGER32) ? "Integer32" :
65 (basetype == SMI_BASETYPE_UNSIGNED64) ? "Unsigned64" :
66 (basetype == SMI_BASETYPE_INTEGER64) ? "Integer64" :
67 (basetype == SMI_BASETYPE_FLOAT32) ? "Float32" :
68 (basetype == SMI_BASETYPE_FLOAT64) ? "Float64" :
69 (basetype == SMI_BASETYPE_FLOAT128) ? "Float128" :
70 (basetype == SMI_BASETYPE_ENUM) ? "INTEGER" :
71 (basetype == SMI_BASETYPE_BITS) ? "Bits" :
77 static char *getOidString(SmiNode *smiNode, int importedParent)
88 smiModule = smiGetNodeModule(smiNode);
92 if (parentNode->oidlen <= 1) {
96 /* prepend the cut-off subidentifier to `append'. */
98 sprintf(append, ".%u%s", parentNode->oid[parentNode->oidlen-1], s);
100 /* retrieve the parent SmiNode */
101 parentNode = smiGetParentNode(parentNode);
104 sprintf(s, "%s", append);
108 /* found an imported or a local parent node? */
109 if ((parentNode->decl != SMI_DECL_IMPL_OBJECT)
110 && ((parentNode->name && strlen(parentNode->name))
111 && (smiIsImported(smiModule, NULL, parentNode->name)
113 && (smiGetNodeModule(parentNode) == smiModule))
114 || (parentNode->oidlen == 1)))) {
115 sprintf(s, "%s%s", parentNode->name, append);
119 } while (parentNode);
122 for (i=0; i < smiNode->oidlen; i++) {
123 if (i) strcat(s, ".");
124 sprintf(&s[strlen(s)], "%u", smiNode->oid[i]);
131 static char *getValueString(SmiValue *valuePtr)
137 switch (valuePtr->basetype) {
138 case SMI_BASETYPE_UNSIGNED32:
139 sprintf(s, "%lu", valuePtr->value.unsigned32);
141 case SMI_BASETYPE_INTEGER32:
142 sprintf(s, "%ld", valuePtr->value.integer32);
144 case SMI_BASETYPE_UNSIGNED64:
145 sprintf(s, UINT64_FORMAT, valuePtr->value.unsigned64);
147 case SMI_BASETYPE_INTEGER64:
148 sprintf(s, INT64_FORMAT, valuePtr->value.integer64);
150 case SMI_BASETYPE_FLOAT32:
151 case SMI_BASETYPE_FLOAT64:
152 case SMI_BASETYPE_FLOAT128:
154 case SMI_BASETYPE_ENUM:
155 case SMI_BASETYPE_OCTETSTRING:
156 case SMI_BASETYPE_BITS:
157 case SMI_BASETYPE_OBJECTIDENTIFIER:
158 /* not required in MOSY format */
160 case SMI_BASETYPE_UNKNOWN:
162 case SMI_BASETYPE_POINTER:
171 static void printIndex(FILE *f, SmiNode *smiNode)
175 SmiElement *smiElement;
177 fprintf(f, "%%%-19s %-16s \"", "ei", smiNode->name);
179 for (i = -1, smiElement = smiGetFirstElement(smiNode);
180 smiElement; smiElement = smiGetNextElement(smiElement), i++) {
181 if (i > 0) fprintf(f, " ");
183 fprintf(f, "%s", indexname);
185 indexname = smiGetElementNode(smiElement)->name;
190 (smiNode->implied) ? "*" : "",
198 static void printAssignements(FILE *f, SmiModule *smiModule)
203 for (smiNode = smiGetFirstNode(smiModule, SMI_NODEKIND_NODE);
204 smiNode; smiNode = smiGetNextNode(smiNode, SMI_NODEKIND_NODE)) {
208 if (smiNode->status == SMI_STATUS_UNKNOWN &&
209 smiNode != smiGetModuleIdentityNode(smiModule)) {
210 fprintf(f, "%-20s %s\n", smiNode->name, getOidString(smiNode, 0));
211 fprintf(f, "%%n0 %-16s object-id\n", smiNode->name);
222 static void printTypedefs(FILE *f, SmiModule *smiModule)
225 SmiType *smiType, *smiParentType;
229 for (i = 0, smiType = smiGetFirstType(smiModule);
230 smiType; smiType = smiGetNextType(smiType)) {
232 smiParentType = smiGetParentType(smiType);
233 type_name = smiParentType->name;
235 if (smiParentType->decl == SMI_DECL_IMPLICIT_TYPE) {
236 smiParentType = smiGetParentType(smiParentType);
237 type_name = smiParentType->name;
239 if (smiParentType->basetype == SMI_BASETYPE_OBJECTIDENTIFIER) {
240 type_name = "ObjectID";
242 if (smiParentType->basetype == SMI_BASETYPE_ENUM) {
243 type_name = "INTEGER";
246 fprintf(f, "%%%-19s %-16s %-15s \"%s\"\n", "tc",
247 smiType->name, type_name,
248 smiType->format ? smiType->format : "");
250 for (i = 0, nn = smiGetFirstNamedNumber(smiType);
251 nn ; i++, nn = smiGetNextNamedNumber(nn)) {
252 fprintf(f, "%%%-19s %-16s %-15s %s\n", "es",
253 smiType->name, nn->name,
254 getValueString(&nn->value));
261 static void printObjects(FILE *f, SmiModule *smiModule)
263 int i, j, ignore, cnt = 0, aggregate, create;
265 SmiNode *smiNode, *relatedNode;
267 SmiNamedNumber *smiNamedNumber;
270 for (smiNode = smiGetFirstNode(smiModule, SMI_NODEKIND_ANY);
271 smiNode; smiNode = smiGetNextNode(smiNode, SMI_NODEKIND_ANY)) {
273 if (smiNode->nodekind != SMI_NODEKIND_NODE
274 && smiNode->nodekind != SMI_NODEKIND_SCALAR
275 && smiNode->nodekind != SMI_NODEKIND_TABLE
276 && smiNode->nodekind != SMI_NODEKIND_ROW
277 && smiNode->nodekind != SMI_NODEKIND_COLUMN) {
283 if (smiNode->nodekind == SMI_NODEKIND_NODE) {
284 if (smiNode->status != SMI_STATUS_UNKNOWN &&
285 smiNode != smiGetModuleIdentityNode(smiModule)) {
286 fprintf(f, "%-20s %s\n", smiNode->name,
287 getOidString(smiNode, 0));
288 fprintf(f, "%%n0 %-16s object-id\n", smiNode->name);
293 aggregate = smiNode->nodekind == SMI_NODEKIND_TABLE
294 || smiNode->nodekind == SMI_NODEKIND_ROW;
297 smiType = smiGetNodeType(smiNode);
302 type_name = getBasetypeString(smiType->basetype);
303 if (smiType && (smiType->decl != SMI_DECL_IMPLICIT_TYPE)) {
304 type_name = smiType->name;
305 if (!strcmp(type_name, "ObjectIdentifier")) {
306 type_name = "ObjectID";
310 if (smiType && smiType->decl == SMI_DECL_IMPLICIT_TYPE) {
311 type_name = smiGetParentType(smiType)->name;
312 if (smiType->basetype == SMI_BASETYPE_OBJECTIDENTIFIER) {
313 type_name = "ObjectID";
315 if (smiType->basetype == SMI_BASETYPE_ENUM) {
316 type_name = "INTEGER";
320 type_name = "Aggregate";
323 if (smiNode->nodekind == SMI_NODEKIND_COLUMN) {
324 create = smiGetParentNode(smiNode)->create;
329 fprintf(f, "%-20s %-16s ", smiNode->name, getOidString(smiNode, 0));
330 fprintf(f, "%-15s %-15s %s\n", type_name,
331 getAccessString(smiNode->access, create),
332 getStatusString(smiNode->status));
334 relatedNode = smiGetRelatedNode(smiNode);
335 switch (smiNode->indexkind) {
336 case SMI_INDEX_INDEX:
337 case SMI_INDEX_REORDER:
338 printIndex(f, smiNode);
340 case SMI_INDEX_EXPAND: /* TODO: we have to do more work here! */
342 case SMI_INDEX_AUGMENT:
344 fprintf(f, "%%%-19s %-16s %s\n", "ea",
345 smiNode->name, relatedNode->name);
348 case SMI_INDEX_SPARSE:
350 printIndex(f, relatedNode);
353 case SMI_INDEX_UNKNOWN:
357 if (smiType && smiType->decl == SMI_DECL_IMPLICIT_TYPE) {
358 for (i = 0, smiNamedNumber = smiGetFirstNamedNumber(smiType);
360 i++, smiNamedNumber = smiGetNextNamedNumber(smiNamedNumber)) {
361 fprintf(f, "%%%-19s %-16s %-15s %s\n", "ev",
362 smiNode->name, smiNamedNumber->name,
363 getValueString(&smiNamedNumber->value));
366 for (ignore = 0, j = 0; ignoreTypeRanges[j]; j++) {
367 if (strcmp(type_name, ignoreTypeRanges[j]) == 0) {
374 for (smiRange = smiGetFirstRange(smiType);
376 smiRange = smiGetNextRange(smiRange)) {
377 fprintf(f, "%%%-19s %-16s %-15s ", "er",
379 getValueString(&smiRange->minValue));
380 fprintf(f, "%s\n", getValueString(&smiRange->maxValue));
393 static void printNotifications(FILE *f, SmiModule *smiModule)
398 for (smiNode = smiGetFirstNode(smiModule, SMI_NODEKIND_NOTIFICATION);
400 smiNode = smiGetNextNode(smiNode, SMI_NODEKIND_NOTIFICATION)) {
404 fprintf(f, "%-20s %s\n", smiNode->name, getOidString(smiNode, 0));
405 fprintf(f, "%%n0 %-16s notification\n", smiNode->name);
415 static void printGroups(FILE *f, SmiModule *smiModule)
417 SmiNode *smiNode, *smiNodeMember;
418 SmiElement *smiElement;
419 int cnt = 0, objects, notifications;
421 for (smiNode = smiGetFirstNode(smiModule, SMI_NODEKIND_GROUP);
422 smiNode; smiNode = smiGetNextNode(smiNode, SMI_NODEKIND_GROUP)) {
426 for (objects = 0, notifications = 0,
427 smiElement = smiGetFirstElement(smiNode);
429 smiElement = smiGetNextElement(smiElement)) {
431 smiNodeMember = smiGetElementNode(smiElement);
434 (smiNodeMember->nodekind == SMI_NODEKIND_SCALAR)
435 || (smiNodeMember->nodekind == SMI_NODEKIND_COLUMN);
437 (smiNodeMember->nodekind == SMI_NODEKIND_NOTIFICATION);
440 fprintf(f, "%-20s %s\n", smiNode->name, getOidString(smiNode, 0));
441 fprintf(f, "%%n0 %-16s %s\n", smiNode->name,
442 (objects && ! notifications) ? "object-group" :
443 (! objects && notifications) ? "notification-group" : "group");
453 static void printCompliances(FILE *f, SmiModule *smiModule)
458 for (smiNode = smiGetFirstNode(smiModule, SMI_NODEKIND_COMPLIANCE);
459 smiNode; smiNode = smiGetNextNode(smiNode, SMI_NODEKIND_COMPLIANCE)) {
463 fprintf(f, "%-20s %s\n", smiNode->name, getOidString(smiNode, 0));
464 fprintf(f, "%%n0 %-16s module-compliance\n", smiNode->name);
474 static void dumpMosy(int modc, SmiModule **modv, int flags, char *output)
481 f = fopen(output, "w");
483 fprintf(stderr, "smidump: cannot open %s for writing: ", output);
489 for (i = 0; i < modc; i++) {
491 if (! (flags & SMIDUMP_FLAG_SILENT)) {
493 "-- automatically generated by smidump %s, do not edit!\n",
496 "\n-- object definitions compiled from %s\n\n",
500 if (! (flags & SMIDUMP_FLAG_SILENT) && (flags & SMIDUMP_FLAG_ERROR)) {
501 fprintf(f, "-- WARNING: this output may be incorrect due to "
502 "significant parse errors\n\n");
505 smiNode = smiGetModuleIdentityNode(modv[i]);
507 SmiNode *parent = smiGetParentNode(smiNode);
508 fprintf(f, "%-20s %s\n", smiNode->name, getOidString(smiNode, !parent || parent->nodekind == SMI_NODEKIND_UNKNOWN ));
509 fprintf(f, "%%n0 %-16s module-identity\n", smiNode->name);
513 printAssignements(f, modv[i]);
514 printTypedefs(f, modv[i]);
515 printObjects(f, modv[i]);
516 printNotifications(f, modv[i]);
517 printGroups(f, modv[i]);
518 printCompliances(f, modv[i]);
521 if (fflush(f) || ferror(f)) {
522 perror("smidump: write error");
536 static SmidumpDriver driver = {
540 SMIDUMP_DRIVER_CANT_UNITE,
541 "intermediate format generated by the mosy compiler",
546 smidumpRegisterDriver(&driver);