Imported Upstream version 0.4.8
[platform/upstream/libsmi.git] / tools / dump-identifiers.c
1 /*
2  * dump-identifiers.c --
3  *
4  *      Operations to dump flat identifier lists for SMI modules.
5  *
6  * Copyright (c) 2000 Frank Strauss, Technical University of Braunschweig.
7  * Copyright (c) 2000 J. Schoenwaelder, Technical University of Braunschweig.
8  *
9  * See the file "COPYING" for information on usage and redistribution
10  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11  *
12  * @(#) $Id: dump-identifiers.c 5758 2006-08-16 21:10:05Z schoenw $
13  */
14
15 #include <config.h>
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <time.h>
23
24 #include "smi.h"
25 #include "smidump.h"
26
27
28 static int moduleLen = 0;
29 static int identifierLen = 0;
30
31 static int showlines = 0;
32 static int showpath = 0;
33 static int ctagfmt = 0;
34
35
36 static char *smiStringNodekind(SmiNodekind nodekind)
37 {
38     return
39         (nodekind == SMI_NODEKIND_UNKNOWN)      ? "<unknown>" :
40         (nodekind == SMI_NODEKIND_NODE)         ? "node" :
41         (nodekind == SMI_NODEKIND_SCALAR)       ? "scalar" :
42         (nodekind == SMI_NODEKIND_TABLE)        ? "table" :
43         (nodekind == SMI_NODEKIND_ROW)          ? "row" :
44         (nodekind == SMI_NODEKIND_COLUMN)       ? "column" :
45         (nodekind == SMI_NODEKIND_NOTIFICATION) ? "notification" :
46         (nodekind == SMI_NODEKIND_GROUP)        ? "group" :
47         (nodekind == SMI_NODEKIND_COMPLIANCE)   ? "compliance" :
48         (nodekind == SMI_NODEKIND_CAPABILITIES) ? "capabilities" :
49                                                   "<UNDEFINED>";
50 }
51
52
53
54 static void fprintNodeIdentifiers(FILE *f, int modc, SmiModule **modv)
55 {
56     SmiNode      *smiNode;
57     unsigned int j;
58     int          i;
59
60     for (i = 0; i < modc; i++) {
61         for (smiNode = smiGetFirstNode(modv[i], SMI_NODEKIND_ANY);
62              smiNode;
63              smiNode = smiGetNextNode(smiNode, SMI_NODEKIND_ANY)) {
64             if (smiNode->name) {
65                 if (ctagfmt) {
66                     fprintf(f, "%*s", -identifierLen, smiNode->name);
67                     fprintf(f, " %d", smiGetNodeLine(smiNode));
68                     fprintf(f, " %*s", -moduleLen, modv[i]->path);
69                     fprintf(f, " %s OBJECT-TYPE -- %s\n", smiNode->name,
70                             smiStringNodekind(smiNode->nodekind));
71                 } else {
72                     fprintf(f, "%*s",
73                             -moduleLen, showpath ? modv[i]->path : modv[i]->name);
74                     if (showlines) {
75                             fprintf(f, ":%d:", smiGetNodeLine(smiNode));
76                     }
77                     fprintf(f, " %*s %-12s ", -identifierLen, smiNode->name,
78                             smiStringNodekind(smiNode->nodekind));
79                     for (j = 0; j < smiNode->oidlen; j++) {
80                             fprintf(f, j ? ".%u" : "%u", smiNode->oid[j]);
81                     }
82                     fprintf(f, "\n");
83                 }
84             }
85         }
86     }
87 }
88
89
90
91 static void fprintTypeIdentifiers(FILE *f, int modc, SmiModule **modv)
92 {
93     SmiType   *smiType;
94     int       i;
95
96     for (i = 0; i < modc; i++) {
97         for (smiType = smiGetFirstType(modv[i]);
98              smiType;
99              smiType = smiGetNextType(smiType)) {
100             if (smiType->name) {
101                 if (ctagfmt) {
102                     fprintf(f, "%*s", -identifierLen, smiType->name);
103                     fprintf(f, " %d", smiGetTypeLine(smiType));
104                     fprintf(f, " %*s", -moduleLen, modv[i]->path);
105                     fprintf(f, " %s TEXTUAL-CONVENTION\n", smiType->name);
106                 } else {
107                 fprintf(f, "%*s",
108                         -moduleLen, showpath ? modv[i]->path : modv[i]->name);
109                 if (showlines) {
110                     fprintf(f, ":%d:", smiGetTypeLine(smiType));
111                 }
112                 fprintf(f, " %*s %-12s\n", -identifierLen, smiType->name,
113                         "type");
114                     }
115             }
116         }
117     }
118 }
119
120
121
122 static void dumpIdentifiers(int modc, SmiModule **modv, int flags,
123                             char *output)
124 {
125     SmiNode   *smiNode;
126     int       i, len;
127     FILE      *f = stdout;
128
129     if (output) {
130         f = fopen(output, "w");
131         if (!f) {
132             fprintf(stderr, "smidump: cannot open %s for writing: ", output);
133             perror(NULL);
134             exit(1);
135         }
136     }
137
138     for (moduleLen = 0, identifierLen = 0, i = 0; i < modc; i++) {
139         if (showpath) {
140             len = strlen(modv[i]->path);
141         } else {
142             len = strlen(modv[i]->name);
143         }
144         if (len > moduleLen) moduleLen = len;
145         for (smiNode = smiGetFirstNode(modv[i], SMI_NODEKIND_ANY);
146              smiNode;
147              smiNode = smiGetNextNode(smiNode, SMI_NODEKIND_ANY)) {
148             if (smiNode->name) {
149                 len = strlen(smiNode->name);
150                 if (len > identifierLen) identifierLen = len;
151             }
152         }
153     }
154
155     if (flags & SMIDUMP_FLAG_UNITE) {
156
157         if (! (flags & SMIDUMP_FLAG_SILENT)) {
158             fprintf(f, "# united list of identifiers (generated by smidump "
159                     SMI_VERSION_STRING ")\n\n");
160         }
161
162         if (! (flags & SMIDUMP_FLAG_SILENT) && (flags & SMIDUMP_FLAG_ERROR)) {
163             fprintf(f, "# WARNING: this output may be incorrect due to "
164                     "significant parse errors\n\n");
165         }
166
167         fprintTypeIdentifiers(f, modc, modv);
168         fprintNodeIdentifiers(f, modc, modv);
169
170     } else {
171
172         for (i = 0; i < modc; i++) {
173
174             if (! (flags & SMIDUMP_FLAG_SILENT)) {
175                 fprintf(f, "# %s list of identifiers (generated by smidump "
176                         SMI_VERSION_STRING ")\n\n",
177                         modv[i]->name);
178             }
179
180             if (! (flags & SMIDUMP_FLAG_SILENT) && (flags & SMIDUMP_FLAG_ERROR)) {
181                 fprintf(f, "# WARNING: this output may be incorrect due to "
182                         "significant parse errors\n\n");
183             }
184             
185             fprintTypeIdentifiers(f, 1, &(modv[i]));
186             fprintNodeIdentifiers(f, 1, &(modv[i]));
187         }
188     }
189
190     if (fflush(f) || ferror(f)) {
191         perror("smidump: write error");
192         exit(1);
193     }
194
195     if (output) {
196         fclose(f);
197     }
198 }
199
200
201
202 void initIdentifiers()
203 {
204     
205     static SmidumpDriverOption opt[] = {
206         { "lines", OPT_FLAG, &showlines, 0,
207           "show line numbers"},
208         { "path", OPT_FLAG, &showpath, 0,
209           "show file path instead of module name"},
210         { "ctag", OPT_FLAG, &ctagfmt, 0,
211           "show symbols in [g]ctag format"},
212         { 0, OPT_END, 0, 0 }
213     };
214
215     static SmidumpDriver driver = {
216         "identifiers",
217         dumpIdentifiers,
218         SMI_FLAG_NODESCR,
219         0,
220         "list of all identifiers",
221         opt,
222         NULL
223     };
224     
225     smidumpRegisterDriver(&driver);
226 }