File: dump-types.c

package info (click to toggle)
libsmi 0.1.7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 10,364 kB
  • ctags: 2,173
  • sloc: ansic: 10,484; sh: 7,338; yacc: 6,150; lex: 1,524; makefile: 162
file content (461 lines) | stat: -rw-r--r-- 10,603 bytes parent folder | download
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
/*
 * dump-types.c --
 *
 *      Operations to dump the type hierarchy in a human readable format.
 *
 * Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig.
 * Copyright (c) 1999 J. Schoenwaelder, 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: dump-types.c,v 1.1 1999/10/05 15:52:21 strauss Exp $
 */

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

#include "smi.h"
#include "smidump.h"

typedef struct BaseTypeCount {
    SmiBasetype basetype;
    int         counter;
} BaseTypeCount;

static BaseTypeCount basetypes[] = {
    { SMI_BASETYPE_INTEGER32, 0 },
    { SMI_BASETYPE_OCTETSTRING, 0 },
    { SMI_BASETYPE_OBJECTIDENTIFIER, 0 },
    { SMI_BASETYPE_UNSIGNED32, 0 },
    { SMI_BASETYPE_INTEGER64, 0 },
    { SMI_BASETYPE_UNSIGNED64, 0 },
    { SMI_BASETYPE_FLOAT32, 0 },
    { SMI_BASETYPE_FLOAT64, 0 },
    { SMI_BASETYPE_FLOAT128, 0 },
    { SMI_BASETYPE_ENUM, 0 },
    { SMI_BASETYPE_BITS, 0 },
    { SMI_BASETYPE_UNKNOWN, 0 }
};


typedef struct TypeNode {
    char        *typemodule;
    char        *typename;
    SmiBasetype basetype;
    struct TypeNode *nextNodePtr;
    struct TypeNode *childNodePtr;
} TypeNode;


static TypeNode _Bits = {
    NULL, "Bits", SMI_BASETYPE_BITS, NULL, NULL
};
static TypeNode _Enumeration = {
    NULL, "Enumeration", SMI_BASETYPE_ENUM, &_Bits, NULL
};
static TypeNode _Float128 = {
    NULL, "Float128", SMI_BASETYPE_FLOAT128, &_Enumeration, NULL
};
static TypeNode _Float64 = {
    NULL, "Float64", SMI_BASETYPE_FLOAT64, &_Float128, NULL
};
static TypeNode _Float32 = {
    NULL, "Float32", SMI_BASETYPE_FLOAT32, &_Float64, NULL
};
static TypeNode _Unsigned64 = {
    NULL, "Unsigned64", SMI_BASETYPE_UNSIGNED64, &_Float32, NULL
};
static TypeNode _Unsigned32 = {
    NULL, "Unsigned32", SMI_BASETYPE_UNSIGNED32, &_Unsigned64, NULL
};
static TypeNode _Integer64 = {
    NULL, "Integer64", SMI_BASETYPE_INTEGER64, &_Unsigned32, NULL
};
static TypeNode _Integer32 = {
    NULL, "Integer32", SMI_BASETYPE_INTEGER32, &_Integer64, NULL
};
static TypeNode _ObjectIdentifier = {
    NULL, "ObjectIdentifier", SMI_BASETYPE_OBJECTIDENTIFIER, &_Integer32, NULL
};
static TypeNode _OctetString = {
    NULL, "OctetString", SMI_BASETYPE_OCTETSTRING, &_ObjectIdentifier, NULL
};
static TypeNode typeRoot = {
    NULL, "", SMI_BASETYPE_UNKNOWN, NULL, &_OctetString
};



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 void initBaseTypeCount()
{
    int i;

    for (i = 0; i < sizeof(basetypes)/sizeof(BaseTypeCount); i++) {
	basetypes[i].counter = 0;
    }
}



static void incrBaseTypeCount(SmiBasetype basetype)
{
    int i;

    for (i = 0; i < sizeof(basetypes)/sizeof(BaseTypeCount); i++) {
	if (basetypes[i].basetype == basetype) {
	    basetypes[i].counter++;
	}
    }
}



static int getBaseTypeCount(SmiBasetype basetype)
{
    int i;

    for (i = 0; i < sizeof(basetypes)/sizeof(BaseTypeCount); i++) {
	if (basetypes[i].basetype == basetype) {
	    return basetypes[i].counter;
	}
    }

    return -1;
}



static char *getValueString(SmiValue *valuePtr)
{
    static char s[100];
    char        **p;
    
    s[0] = 0;
    
    switch (valuePtr->basetype) {
    case SMI_BASETYPE_UNSIGNED32:
	sprintf(s, "%lu", valuePtr->value.unsigned32);
	break;
    case SMI_BASETYPE_INTEGER32:
	sprintf(s, "%ld", valuePtr->value.integer32);
	break;
    case SMI_BASETYPE_UNSIGNED64:
	sprintf(s, "%llu", valuePtr->value.unsigned64);
	break;
    case SMI_BASETYPE_INTEGER64:
	sprintf(s, "%lld", valuePtr->value.integer64);
	break;
    case SMI_BASETYPE_FLOAT32:
    case SMI_BASETYPE_FLOAT64:
    case SMI_BASETYPE_FLOAT128:
	break;
    case SMI_BASETYPE_ENUM:
	sprintf(s, "%s", valuePtr->value.ptr);
	break;
    case SMI_BASETYPE_OCTETSTRING:
	sprintf(s, "\"%s\"", valuePtr->value.ptr);
	break;
    case SMI_BASETYPE_BITS:
	sprintf(s, "(");
	if (valuePtr->value.bits) {
	    for(p = valuePtr->value.bits; *p; p++) {
		if (p != valuePtr->value.bits)
		    sprintf(&s[strlen(s)], ", ");
		sprintf(&s[strlen(s)], "%s", *p);
	    }
	}
	sprintf(&s[strlen(s)], ")");
	break;
    case SMI_BASETYPE_UNKNOWN:
	break;
    case SMI_BASETYPE_OBJECTIDENTIFIER:
        sprintf(s, "%s", valuePtr->value.ptr);
	break;
    }

    return s;
}



static TypeNode *createTypeNode(SmiType *smiType)
{
    TypeNode *newType;
    
    newType = xmalloc(sizeof(TypeNode));
    newType->typemodule = smiType->module;
    newType->typename = smiType->name;
    newType->basetype = smiType->basetype;
    newType->childNodePtr = NULL;
    newType->nextNodePtr = NULL;

    return newType;
}



static void addToTypeTree(TypeNode *root, SmiType *smiType)
{
    TypeNode *newType;

    if (! root) {
	return;
    }

    if ((!root->typemodule
	 || !smiType->parentmodule
	 || strcmp(smiType->parentmodule, root->typemodule) == 0)
	&& strcmp(smiType->parentname, root->typename) == 0) {
	newType = createTypeNode(smiType);
	newType->typemodule = smiType->module;
	if (root->childNodePtr) {
	    newType->nextNodePtr = root->childNodePtr;
	    root->childNodePtr = newType;
	} else {
	    root->childNodePtr = newType;
	}
	return;
    }

    if (root->nextNodePtr) {
	addToTypeTree(root->nextNodePtr, smiType);
    }

    if (root->childNodePtr) {
	addToTypeTree(root->childNodePtr, smiType);
    }
}


static void freeTypeTree(TypeNode *root)
{
    if (root->childNodePtr) {
	if (root->childNodePtr->typemodule) {
	    freeTypeTree(root->childNodePtr);
	    root->childNodePtr = NULL;
	} else {
	    freeTypeTree(root->childNodePtr);
	}
    }
    
    if (root->nextNodePtr) {
	if (root->nextNodePtr->typemodule) {
	    freeTypeTree(root->nextNodePtr);
	    root->nextNodePtr = NULL;
	} else {
	    freeTypeTree(root->nextNodePtr);
	}
    }

    if (root->typemodule) {
	xfree(root);
    }
}
 


static TypeNode *findInTypeTree(TypeNode *root, SmiType *smiType)
{
    TypeNode *result = NULL;
    
    if (root->typemodule
 	&& strcmp(root->typemodule, smiType->module) == 0
 	&& strcmp(root->typename, smiType->name) == 0) {
	result = root;
    }
    
    if (!result && root->childNodePtr) {
 	result = findInTypeTree(root->childNodePtr, smiType);
    }
    
    if (! result && root->nextNodePtr) {
 	result = findInTypeTree(root->nextNodePtr, smiType);
    }
    
    return result;
}



static void printRestrictions(SmiType *smiType)
{
    SmiNamedNumber *nn;
    SmiRange       *range;
    char           s1[40], s2[40];
    int            i;
    
    if ((smiType->basetype == SMI_BASETYPE_ENUM) ||
	(smiType->basetype == SMI_BASETYPE_BITS)) {
	for(i = 0, nn = smiGetFirstNamedNumber(smiType->module, smiType->name);
	    nn ; nn = smiGetNextNamedNumber(nn), i++) {
	    printf("%s%s(%ld)", (i == 0) ? " {" : ", ",
		   nn->name, nn->value.value.integer32);
	}
	if (i) printf("}");
    } else {
	for(i = 0, range = smiGetFirstRange(smiType->module, smiType->name);
	    range ; range = smiGetNextRange(range), i++) {
	    strcpy(s1, getValueString(&range->minValue));
	    strcpy(s2, getValueString(&range->maxValue));
	    printf("%s%s", (i == 0) ? " [" : ", ", s1);
	    if (strcmp(s1, s2)) printf("..%s", s2);
	}
	if (i) printf("]");
    }
}



static void printTypeTree(TypeNode *root, char *prefix)
{
    TypeNode *typeNode, *nextNode;
    int namelen = -1;
    
    if (root->typemodule) {
	printf("%s  |\n", prefix);
    }

    for (typeNode = root; typeNode; typeNode = typeNode->nextNodePtr) {
	int len = strlen(typeNode->typename);
	if (len > namelen) namelen = len;
    }
    
    for (typeNode = root; typeNode; typeNode = typeNode->nextNodePtr) {
 	if (typeNode != &typeRoot) {
	    SmiType *smiType;
	    char c = '+';
	    smiType = smiGetType(typeNode->typemodule, typeNode->typename);
	    if (smiType) {
		c = getStatusChar(smiType->status);
		if (getBaseTypeCount(typeNode->basetype)) {
		    printf("%s  %c--%-*s", prefix, c,
			   namelen, typeNode->typename);
		    printRestrictions(smiType);
		    if (smiType->format) {
			printf("\t\"%s\"", smiType->format);
		    }
		    printf("\n");
		}
		smiFreeType(smiType);
	    }
 	}
 	if (typeNode->childNodePtr) {
 	    char *newprefix;
 	    newprefix = xmalloc(strlen(prefix)+10);
 	    strcpy(newprefix, prefix);
 	    if (typeNode != &typeRoot) {
		for (nextNode = typeNode->nextNodePtr;
		     nextNode; nextNode = nextNode->nextNodePtr) {
		    if (getBaseTypeCount(nextNode->basetype)) {
			break;
		    }
		}
 		if (nextNode) {
  		    strcat(newprefix, "  |");
 		} else {
 		    strcat(newprefix, "   ");
 		}
 	    }
 	    printTypeTree(typeNode->childNodePtr, newprefix);
 	    xfree(newprefix);
	    for (nextNode = typeNode->nextNodePtr;
		 nextNode; nextNode = nextNode->nextNodePtr) {
		if (getBaseTypeCount(nextNode->basetype)) {
		    break;
		}
	    }
	    if (nextNode) {
		printf("%s  |\n", prefix);
	    }
 	}
    }
}



static void addType(SmiType *smiType)
{
    if (strlen(smiType->module) == 0) {
	return;
    }
    
    if (findInTypeTree(&typeRoot, smiType)) {
	return;
    }

    if (smiType->parentmodule && smiType->parentname) {
 	SmiType *parentType;
 	parentType = smiGetType(smiType->parentmodule, smiType->parentname);
 	if (parentType) {
 	    addType(parentType);
	    smiFreeType(parentType);
 	}
    }

    addToTypeTree(&typeRoot, smiType);
}



int dumpTypes(char *modulename)
{
    SmiType     *smiType;
    SmiNode     *smiNode;
    SmiNodekind nodekind = SMI_NODEKIND_SCALAR | SMI_NODEKIND_COLUMN;
    
    initBaseTypeCount();

    printf("# %s type derivation tree (generated by smidump "
	   VERSION ")\n\n", modulename);

    for (smiType = smiGetFirstType(modulename);
 	 smiType;
 	 smiType = smiGetNextType(smiType)) {
 	addType(smiType);
	incrBaseTypeCount(smiType->basetype);
    }
    
    for (smiNode = smiGetFirstNode(modulename, nodekind);
 	 smiNode;
 	 smiNode = smiGetNextNode(smiNode, nodekind)) {
 	smiType = smiGetType(smiNode->typemodule, smiNode->typename);
 	if (smiType) {
 	    if (smiType->decl == SMI_DECL_IMPLICIT_TYPE) {
 		addType(smiType);
 	    }
 	}
	incrBaseTypeCount(smiNode->basetype);
    }

    printTypeTree(&typeRoot, "");
    
    freeTypeTree(&typeRoot);
    return 0;
}