4 * Operations to dump import hierarchies in a human readable 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-imports.c 5758 2006-08-16 21:10:05Z schoenw $
24 typedef struct Imports {
27 struct Imports *nextPtr;
32 static Imports *getImports(SmiModule *smiModule, int *n)
39 for (smiImport = smiGetFirstImport(smiModule), *n = 0;
40 smiImport; smiImport = smiGetNextImport(smiImport)) {
44 size = (*n + 1) * sizeof(Imports);
45 imports = xmalloc(size);
46 memset(imports, 0, size);
48 for (smiImport = smiGetFirstImport(smiModule), *n = 0;
49 smiImport; smiImport = smiGetNextImport(smiImport)) {
51 if (!smiImport->module) continue;
53 for (i = 0; i < *n; i++) {
54 if (strcmp(smiImport->module, imports[i].module) == 0) {
60 imports[i].module = xstrdup(smiImport->module);
61 if (imports[i].module) {
74 static void freeImports(Imports *imports, int n)
78 for (i = 0; i < n; i++) {
79 xfree(imports[i].module);
87 static int fprintImports(FILE *f, SmiModule *smiModule, char *prefix,
90 SmiModule *smiModule2;
91 Imports *imports, *imp;
92 int i, n, recurse = 0, done = 0;
94 for (imp = backtrace; imp; imp = imp->nextPtr) {
95 if (strcmp(imp->module, smiModule->name) == 0) {
96 fprintf(stderr, "%s (recursion - aborted)\n", prefix);
101 imp = (Imports *) xmalloc(sizeof(Imports));
102 imp->module = smiModule->name;
103 imp->nextPtr = backtrace;
106 imports = getImports(smiModule, &n);
108 for (i = 0; i < n; i++) {
111 smiModule2 = smiGetModule(imports[i].module);
112 recurse = (NULL == smiGetFirstImport(smiModule2));
114 fprintf(f, "%s |\n", prefix);
116 fprintf(f, "%s +--%s [%d identifier%s]\n", prefix, imports[i].module,
117 imports[i].count, imports[i].count > 1 ? "s" : "");
118 newprefix = xmalloc(strlen(prefix)+10);
119 strcpy(newprefix, prefix);
121 strcat(newprefix, " ");
123 strcat(newprefix, " |");
125 done = fprintImports(f, smiModule2, newprefix, backtrace);
126 if (! recurse && done) {
128 fprintf(f, "%s \n", prefix);
130 fprintf(f, "%s |\n", prefix);
136 freeImports(imports, n);
144 static void dumpImports(int modc, SmiModule **modv, int flags, char *output)
150 f = fopen(output, "w");
152 fprintf(stderr, "smidump: cannot open %s for writing: ", output);
158 for (i = 0; i < modc; i++) {
159 if (! (flags & SMIDUMP_FLAG_SILENT)) {
160 fprintf(f, "# %s imports tree (generated by smidump "
161 SMI_VERSION_STRING ")\n\n", modv[i]->name);
164 if (! (flags & SMIDUMP_FLAG_SILENT) && (flags & SMIDUMP_FLAG_ERROR)) {
165 fprintf(f, "# WARNING: this output may be incorrect due to "
166 "significant parse errors\n\n");
169 fprintf(f, "%s\n", modv[i]->name);
170 fprintImports(f, modv[i], "", NULL);
173 if (fflush(f) || ferror(f)) {
174 perror("smidump: write error");
188 static SmidumpDriver driver = {
192 SMIDUMP_DRIVER_CANT_UNITE,
193 "recursive list of all imports",
198 smidumpRegisterDriver(&driver);