8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / lintdump / lintdump.c
blobcf00b76d4e2c4328ca5cc5b2573ea37988cd586a
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "@(#)lintdump.c 1.6 06/06/04 SMI (from meem)"
28 #pragma ident "%Z%%M% %I% %E% SMI"
31 * Tool for dumping lint libraries.
34 #include <ctype.h>
35 #include <errno.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
42 #include "lnstuff.h" /* silly header name from alint */
44 typedef struct lsu {
45 const char *name;
46 ATYPE atype;
47 struct lsu *next;
48 } lsu_t;
50 #define LSU_HASHSIZE 512
51 static lsu_t *lsu_table[LSU_HASHSIZE];
53 static boolean_t showids = B_TRUE;
54 static boolean_t justrelpaths = B_FALSE;
55 static int justpass = -1;
56 static int indentlevel = 9;
57 static const char *progname;
59 static void info(const char *, ...);
60 static void infohdr(const char *, const char *, ...);
61 static void warn(const char *, ...);
62 static void die(const char *, ...);
63 static void usage(void);
64 static void indent(void);
65 static void unindent(void);
66 static void print_lintmod(const char *, FILE *, FLENS *);
67 static void print_pass(const char *, FILE *);
68 static void print_atype(ATYPE *, int, ATYPE *, const char *);
69 static void print_mods(const char *, ATYPE *, int, ATYPE *, uint_t);
70 static void getstr(FILE *, char *, size_t);
71 static void lsu_build(FILE *);
72 static void lsu_empty(void);
73 static int lsu_add(const char *, ATYPE *);
74 static lsu_t *lsu_lookup(unsigned long);
76 int
77 main(int argc, char **argv)
79 int i, c, mod;
80 FILE *fp;
81 FLENS hdr;
82 const char *lnname;
84 progname = strrchr(argv[0], '/');
85 if (progname == NULL)
86 progname = argv[0];
87 else
88 progname++;
90 while ((c = getopt(argc, argv, "ip:r")) != EOF) {
91 switch (c) {
92 case 'i':
93 showids = B_FALSE;
94 break;
95 case 'p':
96 justpass = strtoul(optarg, NULL, 0);
97 if (justpass < 1 || justpass > 3)
98 usage();
99 break;
100 case 'r':
101 justrelpaths = B_TRUE;
102 break;
103 default:
104 usage();
108 if (optind == argc)
109 usage();
111 for (i = optind; i < argc; i++) {
112 fp = fopen(argv[i], "r");
113 if (fp == NULL) {
114 warn("cannot open \"%s\"", argv[i]);
115 continue;
118 lnname = argv[i];
119 if (justrelpaths && lnname[0] == '/')
120 lnname = strrchr(lnname, '/') + 1;
123 * Dump out all of the modules in the lint object.
125 for (mod = 1; fread(&hdr, sizeof (hdr), 1, fp) == 1; mod++) {
126 if (hdr.ver != LINTVER) {
127 warn("%s: unsupported lint object version "
128 "%d\n", argv[i], hdr.ver);
129 break;
132 if (mod == 1)
133 infohdr("LINTOBJ", "%s\n", lnname);
136 * First build the table of structure/union names,
137 * then print the lint module. Finally, empty the
138 * table out before dumping the next module.
140 lsu_build(fp);
141 print_lintmod(lnname, fp, &hdr);
142 lsu_empty();
144 (void) fclose(fp);
147 return (EXIT_SUCCESS);
151 * Print a lint module and advance past it in the stream.
153 static void
154 print_lintmod(const char *lnname, FILE *fp, FLENS *hp)
156 ulong_t psizes[5];
157 uint_t pass;
159 psizes[0] = 0;
160 psizes[1] = hp->f1;
161 psizes[2] = hp->f2;
162 psizes[3] = hp->f3;
163 psizes[4] = hp->f4;
165 infohdr("LINTMOD", "%hu: %lu+%lu+%lu+%lu = %lu bytes\n", hp->mno,
166 hp->f1, hp->f2, hp->f3, hp->f4, hp->f1 + hp->f2 + hp->f3 + hp->f4);
168 for (pass = 1; pass <= 4; pass++) {
169 if ((justpass < 0 || justpass == pass) && pass < 4) {
170 infohdr("SECTION", "PASS%u: %lu bytes\n", pass,
171 psizes[pass]);
172 print_pass(lnname, fp);
173 } else {
174 (void) fseek(fp, psizes[pass], SEEK_CUR);
180 * Print out a PASS section of a lint module.
182 static void
183 print_pass(const char *lnname, FILE *fp)
185 union rec rec;
186 int nargs;
187 char name[1024];
188 ATYPE atype, *args;
189 LINE line;
190 boolean_t wasfile = B_FALSE;
192 for (;;) {
193 if (fread(&rec, sizeof (rec), 1, fp) != 1)
194 die("%s: unexpected end of file\n", lnname);
196 line = rec.l;
197 if (line.decflag & LND) /* end-of-pass marker */
198 break;
200 getstr(fp, name, sizeof (name));
203 * Check if this is a file record.
205 if (line.decflag & LFN) {
206 if (wasfile || !justrelpaths)
207 infohdr("FILE", "%s\n", name);
208 wasfile = B_TRUE;
209 continue;
211 wasfile = B_FALSE;
214 * Check if this is a function or variable record.
216 nargs = line.nargs;
217 if (line.decflag & (LIB|LDS|LDI|LPR|LDX|LDC|LRV|LUE|LUV|LUM)) {
218 if (nargs < 0)
219 nargs = -nargs - 1;
221 if (line.decflag & LDS)
222 info("static ");
223 else if (line.decflag & (LPR|LDX|LDC))
224 info("extern ");
226 args = calloc(sizeof (atype), nargs);
227 if (args == NULL)
228 die("cannot allocate argument information");
230 if (fread(args, sizeof (atype), nargs, fp) != nargs)
231 die("%s: unexpected end of file\n", lnname);
233 print_atype(&line.type, line.nargs, args, name);
234 free(args);
236 if (line.decflag & LRV)
237 info(" <returns value>");
238 if (line.decflag & LUE)
239 info(" <use: side-effects context>");
240 if (line.decflag & LUV)
241 info(" <use: return value context>");
242 if (line.decflag & LUM)
243 info(" <use: unspecified context>");
245 if (line.decflag & LPF)
246 info(" <PRINTFLIKE%d>", nargs);
247 else if (line.decflag & LSF)
248 info(" <SCANFLIKE%d>", nargs);
250 if (line.decflag & LDI)
251 info(" { <definition> }");
252 else if (line.decflag & LDX)
253 info(" = <definition>");
255 info(";\n");
256 continue;
260 * Check if this is a structure or union record.
262 if (line.decflag & LSU) {
263 if (line.decflag & ~(LSU))
264 info("??? ");
266 info("struct ");
267 if (name[0] != '.')
268 info("%s ", name);
269 if (showids)
270 info("<tag %lu> ", line.type.extra.ty);
271 info("{ \n");
273 indent();
274 for (; nargs > 0; nargs--) {
275 if (fread(&atype, sizeof (atype), 1, fp) != 1) {
276 die("%s: unexpected end of file\n",
277 lnname);
279 getstr(fp, name, sizeof (name));
280 print_atype(&atype, 0, NULL, name);
281 info(";\n");
283 unindent();
284 info("};\n");
285 continue;
288 warn("%s: unknown record type 0%o\n", lnname, line.decflag);
293 * Print the C datatype or function `atp' named `name'. If `name' is a
294 * function, then `nargs' indicates the number of C datatypes pointed to
295 * by `args'.
297 static void
298 print_atype(ATYPE *atp, int nargs, ATYPE *args, const char *name)
300 static const char *basetypes[] = { "",
301 "char", "unsigned char", "signed char",
302 "short", "unsigned short", "signed short",
303 "int", "unsigned int", "signed int",
304 "long", "unsigned long", "signed long",
305 "long long", "unsigned long long", "signed long long",
306 "enum", "float", "double",
307 "long double", "void", "struct",
308 "union", "_Bool", "<genchar>",
309 "<genshort>", "<genint>", "<genlong>",
310 "<genlonglong>"
312 uint16_t basetype = atp->aty & LNQUAL;
313 lsu_t *lsup;
315 if (atp->aty & LCON)
316 info("const ");
317 if (atp->aty & LVOL)
318 info("volatile ");
319 if (atp->aty & LCONV)
320 info("integer const ");
322 if (basetype < 1 ||
323 basetype > (sizeof (basetypes) / sizeof (*basetypes)))
324 info("<unknown type %x>", basetype);
326 switch (basetype) {
327 case LN_UNION:
328 case LN_STRUCT:
329 lsup = lsu_lookup(atp->extra.ty);
330 if (lsup != NULL && lsup->name[0] != '.') {
331 info("%s %s", basetypes[basetype], lsup->name);
332 } else {
333 info("%s", basetypes[basetype]);
334 if (showids)
335 info(" <tag %lu>", atp->extra.ty);
336 else
337 info(" <anon>");
339 break;
340 default:
341 info("%s", basetypes[basetype]);
344 print_mods(name, atp, nargs, args, 14);
348 * Recursively print type modifiers.
350 static void
351 print_mods(const char *name, ATYPE *atp, int nargs, ATYPE *args, uint_t pos)
353 int arg;
354 int mods = atp->dcl_mod >> (pos * 2);
355 int lastmods = atp->dcl_mod >> ((pos + 1) * 2);
356 boolean_t isvarargs = B_FALSE;
358 if (LN_ISPTR(mods)) {
359 if (!LN_ISPTR(lastmods) && !LN_ISFTN(lastmods))
360 info(" ");
361 info("*");
364 if (atp->dcl_con & (1 << pos))
365 info(" const ");
366 if (atp->dcl_vol & (1 << pos))
367 info(" volatile ");
369 if (pos != 0) {
370 if (LN_ISFTN(mods))
371 info(" (");
372 print_mods(name, atp, nargs, args, pos - 1);
373 if (LN_ISFTN(mods))
374 info(")()");
375 return;
378 if (name[0] == '\0')
379 return;
381 if (!LN_ISPTR(lastmods) && !LN_ISPTR(mods))
382 info(" ");
383 info("%s", name);
385 if (LN_ISARY(mods)) {
386 info("[]");
387 } else if (LN_ISFTN(mods)) {
388 info("(");
390 if (nargs < 0) {
391 nargs = -nargs - 1;
392 isvarargs = B_TRUE;
395 if (nargs == 0) {
396 info("void");
397 } else {
398 for (arg = 0; arg < nargs; arg++) {
399 print_atype(&args[arg], 0, NULL, "");
400 if ((arg + 1) < nargs)
401 info(", ");
402 else if (isvarargs)
403 info(", ...");
406 info(")");
411 * Add an LSU entry to the LSU table.
413 static int
414 lsu_add(const char *name, ATYPE *atp)
416 unsigned int i = atp->extra.ty % LSU_HASHSIZE;
417 lsu_t *lsup;
419 lsup = malloc(sizeof (lsu_t));
420 if (lsup == NULL)
421 return (ENOMEM);
423 lsup->atype = *atp;
424 lsup->next = lsu_table[i];
425 lsup->name = strdup(name);
426 if (lsup->name == NULL) {
427 free(lsup);
428 return (ENOMEM);
431 lsu_table[i] = lsup;
432 return (0);
436 * Lookup an LSU entry by ID.
438 static lsu_t *
439 lsu_lookup(T1WORD ty)
441 unsigned int i = ty % LSU_HASHSIZE;
442 lsu_t *lsup;
444 for (lsup = lsu_table[i]; lsup != NULL; lsup = lsup->next) {
445 if (lsup->atype.extra.ty == ty)
446 return (lsup);
449 return (NULL);
453 * Read all LSU (structure and union definition) records in order to
454 * build a structure and union name table, called the LSU table.
455 * Although `fp' is read from, the original file offset is preserved.
457 static void
458 lsu_build(FILE *fp)
460 union rec rec;
461 char name[1024];
462 int nargs;
463 off_t curoff = ftello(fp);
465 for (;;) {
466 if (fread(&rec, sizeof (rec), 1, fp) != 1)
467 break;
469 if (rec.l.decflag & LND) /* end-of-pass marker */
470 break;
472 getstr(fp, name, sizeof (name));
473 nargs = rec.l.nargs;
475 if (rec.l.decflag & (LIB|LDS|LDI)) {
476 if (nargs < 0)
477 nargs = -nargs - 1;
479 (void) fseek(fp, sizeof (ATYPE) * nargs, SEEK_CUR);
480 continue;
483 if (rec.l.decflag & LSU) {
484 if (lsu_add(name, &rec.l.type) != 0)
485 warn("cannot allocate struct `%s' info", name);
487 for (; nargs > 0; nargs--) {
488 (void) fseek(fp, sizeof (ATYPE), SEEK_CUR);
489 getstr(fp, name, sizeof (name));
494 (void) fseek(fp, curoff, SEEK_SET);
498 * Empty the LSU table.
500 static void
501 lsu_empty(void)
503 lsu_t *lsup, *lsup_next;
504 unsigned int i;
506 for (i = 0; i < LSU_HASHSIZE; i++) {
507 for (lsup = lsu_table[i]; lsup != NULL; lsup = lsup_next) {
508 lsup_next = lsup->next;
509 free(lsup);
511 lsu_table[i] = NULL;
516 * Read the NUL-terminated string at `fp' into `buf', which is at most
517 * `bufsize' bytes.
519 static void
520 getstr(FILE *fp, char *buf, size_t bufsize)
522 int c;
523 size_t i;
525 for (i = 0; i < bufsize - 1; i++) {
526 c = fgetc(fp);
527 if (c == EOF || c == '\0' || !isascii(c))
528 break;
529 buf[i] = (char)c;
532 buf[i] = '\0';
535 static void
536 indent(void)
538 indentlevel += 4;
541 static void
542 unindent(void)
544 indentlevel -= 4;
547 static void
548 usage(void)
550 (void) fprintf(stderr, "usage: %s [-i] [-p 1|2|3] [-r] lintobj"
551 " [ lintobj ... ]\n", progname);
552 exit(EXIT_FAILURE);
555 /* PRINTFLIKE1 */
556 static void
557 info(const char *format, ...)
559 va_list alist;
560 static int complete = 1;
562 if (complete)
563 (void) printf("%*s", indentlevel, "");
565 va_start(alist, format);
566 (void) vprintf(format, alist);
567 va_end(alist);
569 complete = strrchr(format, '\n') != NULL;
572 /* PRINTFLIKE2 */
573 static void
574 infohdr(const char *hdr, const char *format, ...)
576 va_list alist;
577 static int complete = 1;
579 if (complete)
580 (void) printf("%7s: ", hdr);
582 va_start(alist, format);
583 (void) vprintf(format, alist);
584 va_end(alist);
586 complete = strrchr(format, '\n') != NULL;
589 /* PRINTFLIKE1 */
590 static void
591 warn(const char *format, ...)
593 va_list alist;
594 char *errstr = strerror(errno);
596 (void) fprintf(stderr, "%s: warning: ", progname);
598 va_start(alist, format);
599 (void) vfprintf(stderr, format, alist);
600 va_end(alist);
602 if (strrchr(format, '\n') == NULL)
603 (void) fprintf(stderr, ": %s\n", errstr);
606 /* PRINTFLIKE1 */
607 static void
608 die(const char *format, ...)
610 va_list alist;
611 char *errstr = strerror(errno);
613 (void) fprintf(stderr, "%s: fatal: ", progname);
615 va_start(alist, format);
616 (void) vfprintf(stderr, format, alist);
617 va_end(alist);
619 if (strrchr(format, '\n') == NULL)
620 (void) fprintf(stderr, ": %s\n", errstr);
622 exit(EXIT_FAILURE);