xref: /titanic_44/usr/src/cmd/tnf/tnfdump/main.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  *	Copyright (c) 1994, by Sun Microsytems, Inc.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include <unistd.h>
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
31*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
35*7c478bd9Sstevel@tonic-gate #include <string.h>
36*7c478bd9Sstevel@tonic-gate #include <tnf/tnf.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
39*7c478bd9Sstevel@tonic-gate #include <libintl.h>
40*7c478bd9Sstevel@tonic-gate #include <locale.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include "state.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static caddr_t	g_file_base;	/* base address of file 	*/
45*7c478bd9Sstevel@tonic-gate static char	*g_cmdname;	/* name of this command		*/
46*7c478bd9Sstevel@tonic-gate static int	g_raw = B_FALSE;	/* output format */
47*7c478bd9Sstevel@tonic-gate static int	g_status = EXIT_SUCCESS; /* exit status (from stdlib.h) */
48*7c478bd9Sstevel@tonic-gate static const char	*print_unsigned = "%u";
49*7c478bd9Sstevel@tonic-gate static const char	*print_unsigned64 = "%llu";
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #define	OFF(p)	(p - g_file_base)
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static void describe_array		(tnf_datum_t);
54*7c478bd9Sstevel@tonic-gate static void describe_brief		(tnf_datum_t);
55*7c478bd9Sstevel@tonic-gate static void describe_record		(tnf_datum_t);
56*7c478bd9Sstevel@tonic-gate static void describe_struct		(tnf_datum_t);
57*7c478bd9Sstevel@tonic-gate static void describe_type		(tnf_datum_t);
58*7c478bd9Sstevel@tonic-gate static void read_tnf_file		(int, char *);
59*7c478bd9Sstevel@tonic-gate static void usage			(void);
60*7c478bd9Sstevel@tonic-gate static void scanargs			(int, char **, int *, char ***);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate int
main(int ac,char * av[])63*7c478bd9Sstevel@tonic-gate main(int ac, char *av[])
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	int 	numfiles;	/* number of files to be printed */
66*7c478bd9Sstevel@tonic-gate 	char 	**filenames;	/* start of file names list 	*/
67*7c478bd9Sstevel@tonic-gate 	int	i;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	/* internationalization stuff */
70*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
71*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
72*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
73*7c478bd9Sstevel@tonic-gate #endif
74*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	g_cmdname = av[0];
77*7c478bd9Sstevel@tonic-gate 	scanargs(ac, av, &numfiles, &filenames);
78*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numfiles; i++) {
79*7c478bd9Sstevel@tonic-gate 		read_tnf_file(g_raw, filenames[i]);
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	if (!g_raw) {
83*7c478bd9Sstevel@tonic-gate 		if (table_get_num_elements() > 0) {
84*7c478bd9Sstevel@tonic-gate 			print_c_header();
85*7c478bd9Sstevel@tonic-gate 			print_sorted_events();
86*7c478bd9Sstevel@tonic-gate 		}
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	exit(g_status);
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	return (0);
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate static void
scanargs(int argc,char ** argv,int * nfiles,char *** files)95*7c478bd9Sstevel@tonic-gate scanargs(int argc, char **argv, int *nfiles, char ***files)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	int c;
98*7c478bd9Sstevel@tonic-gate 	int errflg = B_FALSE;
99*7c478bd9Sstevel@tonic-gate 	char *optstr = "rx";
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, optstr)) != EOF) {
102*7c478bd9Sstevel@tonic-gate 		switch (c) {
103*7c478bd9Sstevel@tonic-gate 		case 'r':
104*7c478bd9Sstevel@tonic-gate 			g_raw = B_TRUE;
105*7c478bd9Sstevel@tonic-gate 			break;
106*7c478bd9Sstevel@tonic-gate 		case 'x':
107*7c478bd9Sstevel@tonic-gate 			print_unsigned = "0x%x";
108*7c478bd9Sstevel@tonic-gate 			print_unsigned64 = "0x%llx";
109*7c478bd9Sstevel@tonic-gate 			break;
110*7c478bd9Sstevel@tonic-gate 		case '?':
111*7c478bd9Sstevel@tonic-gate 			errflg = B_TRUE;
112*7c478bd9Sstevel@tonic-gate 			break;
113*7c478bd9Sstevel@tonic-gate 		}
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 	*files = &argv[optind];
116*7c478bd9Sstevel@tonic-gate 	*nfiles = argc - optind;
117*7c478bd9Sstevel@tonic-gate 	if (*nfiles <= 0) {
118*7c478bd9Sstevel@tonic-gate 	    errflg = B_TRUE;
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate 	if (errflg) {
121*7c478bd9Sstevel@tonic-gate 		usage();
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate static void
read_tnf_file(int raw,char * path)127*7c478bd9Sstevel@tonic-gate read_tnf_file(int raw, char *path)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	int 		fd;
130*7c478bd9Sstevel@tonic-gate 	struct stat 	st;
131*7c478bd9Sstevel@tonic-gate 	caddr_t 	p, curr_p, end_p;
132*7c478bd9Sstevel@tonic-gate 	TNF		*tnf;
133*7c478bd9Sstevel@tonic-gate 	tnf_errcode_t	err;
134*7c478bd9Sstevel@tonic-gate 	tnf_datum_t	record;
135*7c478bd9Sstevel@tonic-gate 	void (*desc_func)(tnf_datum_t) = describe_c_record;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if ((fd = open(path, O_RDONLY, 0777)) == -1) {
138*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: cannot open %s\n"),
139*7c478bd9Sstevel@tonic-gate 		    g_cmdname, path);
140*7c478bd9Sstevel@tonic-gate 		g_status = EXIT_FAILURE;
141*7c478bd9Sstevel@tonic-gate 		return;
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 	if (fstat(fd, &st) != 0) {
144*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: fstat error on %s\n"),
145*7c478bd9Sstevel@tonic-gate 		    g_cmdname, path);
146*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
147*7c478bd9Sstevel@tonic-gate 		g_status = EXIT_FAILURE;
148*7c478bd9Sstevel@tonic-gate 		return;
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 	if (st.st_size == 0) {
151*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: %s is empty\n"),
152*7c478bd9Sstevel@tonic-gate 		    g_cmdname, path);
153*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
154*7c478bd9Sstevel@tonic-gate 		g_status = EXIT_FAILURE;
155*7c478bd9Sstevel@tonic-gate 		return;
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 	if ((p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0))
158*7c478bd9Sstevel@tonic-gate 	    == (caddr_t)-1) {
159*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: mmap error on %s\n"),
160*7c478bd9Sstevel@tonic-gate 		    g_cmdname, path);
161*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
162*7c478bd9Sstevel@tonic-gate 		g_status = EXIT_FAILURE;
163*7c478bd9Sstevel@tonic-gate 		return;
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	if (raw)
167*7c478bd9Sstevel@tonic-gate 		g_file_base = p;		/* for OFF() */
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	if (*p == 0) {
170*7c478bd9Sstevel@tonic-gate 		/*
171*7c478bd9Sstevel@tonic-gate 		 * magic word is 0 - catch the error if entire file is zero.
172*7c478bd9Sstevel@tonic-gate 		 * tnf_reader_begin() will catch the "not a TNF file" error.
173*7c478bd9Sstevel@tonic-gate 		 */
174*7c478bd9Sstevel@tonic-gate 		curr_p = p;
175*7c478bd9Sstevel@tonic-gate 		end_p = p + st.st_size;
176*7c478bd9Sstevel@tonic-gate 		while ((curr_p < end_p) && (*curr_p == 0))
177*7c478bd9Sstevel@tonic-gate 			curr_p++;
178*7c478bd9Sstevel@tonic-gate 		if (curr_p == end_p) {
179*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
180*7c478bd9Sstevel@tonic-gate 				gettext("%s: %s is an empty TNF file\n"),
181*7c478bd9Sstevel@tonic-gate 				g_cmdname, path);
182*7c478bd9Sstevel@tonic-gate 			(void) munmap(p, st.st_size);
183*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
184*7c478bd9Sstevel@tonic-gate 			return;
185*7c478bd9Sstevel@tonic-gate 		}
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	if ((err = tnf_reader_begin(p, st.st_size, &tnf)) != TNF_ERR_NONE) {
189*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: error in %s: %s\n"),
190*7c478bd9Sstevel@tonic-gate 		    g_cmdname, path, tnf_error_message(err));
191*7c478bd9Sstevel@tonic-gate 		(void) munmap(p, st.st_size);
192*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
193*7c478bd9Sstevel@tonic-gate 		g_status = EXIT_FAILURE;
194*7c478bd9Sstevel@tonic-gate 		return;
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	/* Describe file header */
198*7c478bd9Sstevel@tonic-gate 	record = tnf_get_file_header(tnf);
199*7c478bd9Sstevel@tonic-gate 	if (raw) {
200*7c478bd9Sstevel@tonic-gate 		describe_record(record);
201*7c478bd9Sstevel@tonic-gate 		desc_func = describe_record;
202*7c478bd9Sstevel@tonic-gate 	}
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	/* Describe all other records */
205*7c478bd9Sstevel@tonic-gate 	while ((record = tnf_get_next_record(record)) != TNF_DATUM_NULL)
206*7c478bd9Sstevel@tonic-gate 		desc_func(record);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	/* Don't munmap for cooked output because we access records later */
209*7c478bd9Sstevel@tonic-gate 	if (raw)
210*7c478bd9Sstevel@tonic-gate 		(void) munmap(p, st.st_size);
211*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate static void
describe_record(tnf_datum_t datum)215*7c478bd9Sstevel@tonic-gate describe_record(tnf_datum_t datum)
216*7c478bd9Sstevel@tonic-gate {
217*7c478bd9Sstevel@tonic-gate 	(void) printf("0x%-8x: {\n", OFF(tnf_get_raw(datum)));
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	switch (tnf_get_kind(datum)) {
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	case TNF_K_STRUCT:
222*7c478bd9Sstevel@tonic-gate 		describe_struct(datum);
223*7c478bd9Sstevel@tonic-gate 		break;
224*7c478bd9Sstevel@tonic-gate 	case TNF_K_STRING:
225*7c478bd9Sstevel@tonic-gate 	case TNF_K_ARRAY:
226*7c478bd9Sstevel@tonic-gate 		describe_array(datum);
227*7c478bd9Sstevel@tonic-gate 		break;
228*7c478bd9Sstevel@tonic-gate 	case TNF_K_TYPE:
229*7c478bd9Sstevel@tonic-gate 		describe_type(datum);
230*7c478bd9Sstevel@tonic-gate 		break;
231*7c478bd9Sstevel@tonic-gate 	default:
232*7c478bd9Sstevel@tonic-gate 		fail(0, gettext("illegal record at %x (%d)"),
233*7c478bd9Sstevel@tonic-gate 			tnf_get_raw(datum), tnf_get_kind(datum));
234*7c478bd9Sstevel@tonic-gate 		break;
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	(void) printf("\t}\n");
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate void
describe_scalar(tnf_datum_t datum)241*7c478bd9Sstevel@tonic-gate describe_scalar(tnf_datum_t datum)
242*7c478bd9Sstevel@tonic-gate {
243*7c478bd9Sstevel@tonic-gate 	switch (tnf_get_kind(datum)) {
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	case TNF_K_CHAR:
246*7c478bd9Sstevel@tonic-gate 		(void) printf("%c", tnf_get_char(datum));
247*7c478bd9Sstevel@tonic-gate 		break;
248*7c478bd9Sstevel@tonic-gate 	case TNF_K_INT8:
249*7c478bd9Sstevel@tonic-gate 		(void) printf("%d", tnf_get_int8(datum));
250*7c478bd9Sstevel@tonic-gate 		break;
251*7c478bd9Sstevel@tonic-gate 	case TNF_K_UINT8:
252*7c478bd9Sstevel@tonic-gate 		(void) printf(print_unsigned, (tnf_uint8_t)tnf_get_int8(datum));
253*7c478bd9Sstevel@tonic-gate 		break;
254*7c478bd9Sstevel@tonic-gate 	case TNF_K_INT16:
255*7c478bd9Sstevel@tonic-gate 		(void) printf("%d", tnf_get_int16(datum));
256*7c478bd9Sstevel@tonic-gate 		break;
257*7c478bd9Sstevel@tonic-gate 	case TNF_K_UINT16:
258*7c478bd9Sstevel@tonic-gate 		(void) printf(print_unsigned,
259*7c478bd9Sstevel@tonic-gate 			(tnf_uint16_t)tnf_get_int16(datum));
260*7c478bd9Sstevel@tonic-gate 		break;
261*7c478bd9Sstevel@tonic-gate 	case TNF_K_INT32:
262*7c478bd9Sstevel@tonic-gate 		(void) printf("%d", (int)tnf_get_int32(datum));
263*7c478bd9Sstevel@tonic-gate 		break;
264*7c478bd9Sstevel@tonic-gate 	case TNF_K_UINT32:
265*7c478bd9Sstevel@tonic-gate 		if ((tnf_type_get_property(tnf_get_type(datum), TNF_N_OPAQUE))
266*7c478bd9Sstevel@tonic-gate 				!= TNF_DATUM_NULL) {
267*7c478bd9Sstevel@tonic-gate 			/* XXX */
268*7c478bd9Sstevel@tonic-gate 			(void) printf("0x%x",
269*7c478bd9Sstevel@tonic-gate 				(tnf_uint32_t)tnf_get_int32(datum));
270*7c478bd9Sstevel@tonic-gate 		} else {
271*7c478bd9Sstevel@tonic-gate 			(void) printf(print_unsigned,
272*7c478bd9Sstevel@tonic-gate 				(tnf_uint32_t)tnf_get_int32(datum));
273*7c478bd9Sstevel@tonic-gate 		}
274*7c478bd9Sstevel@tonic-gate 		break;
275*7c478bd9Sstevel@tonic-gate 	case TNF_K_INT64:
276*7c478bd9Sstevel@tonic-gate 		/* lint not updated, it complains: malformed format string */
277*7c478bd9Sstevel@tonic-gate 		(void) printf("%lld", 	tnf_get_int64(datum));
278*7c478bd9Sstevel@tonic-gate 		break;
279*7c478bd9Sstevel@tonic-gate 	case TNF_K_UINT64:
280*7c478bd9Sstevel@tonic-gate 		if ((tnf_type_get_property(tnf_get_type(datum), TNF_N_OPAQUE))
281*7c478bd9Sstevel@tonic-gate 			!= TNF_DATUM_NULL) {
282*7c478bd9Sstevel@tonic-gate 			(void) printf("0x%llx",
283*7c478bd9Sstevel@tonic-gate 				(tnf_uint64_t)tnf_get_int64(datum));
284*7c478bd9Sstevel@tonic-gate 		} else {
285*7c478bd9Sstevel@tonic-gate 		/* lint not updated, it complains: malformed format string */
286*7c478bd9Sstevel@tonic-gate 			(void) printf(print_unsigned64,
287*7c478bd9Sstevel@tonic-gate 					(tnf_uint64_t)tnf_get_int64(datum));
288*7c478bd9Sstevel@tonic-gate 		}
289*7c478bd9Sstevel@tonic-gate 		break;
290*7c478bd9Sstevel@tonic-gate 	case TNF_K_FLOAT32:
291*7c478bd9Sstevel@tonic-gate 		(void) printf("%f", tnf_get_float32(datum));
292*7c478bd9Sstevel@tonic-gate 		break;
293*7c478bd9Sstevel@tonic-gate 	case TNF_K_FLOAT64:
294*7c478bd9Sstevel@tonic-gate 		(void) printf("%f", tnf_get_float64(datum));
295*7c478bd9Sstevel@tonic-gate 		break;
296*7c478bd9Sstevel@tonic-gate 	case TNF_K_SCALAR:
297*7c478bd9Sstevel@tonic-gate 		(void) printf("unhandled scalar");
298*7c478bd9Sstevel@tonic-gate 		break;
299*7c478bd9Sstevel@tonic-gate 	default:
300*7c478bd9Sstevel@tonic-gate 		fail(0, gettext("not a scalar"));
301*7c478bd9Sstevel@tonic-gate 		break;
302*7c478bd9Sstevel@tonic-gate 	}
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate static void
describe_struct(tnf_datum_t datum)306*7c478bd9Sstevel@tonic-gate describe_struct(tnf_datum_t datum)
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate 	unsigned n, i;
309*7c478bd9Sstevel@tonic-gate 	char *slotname;
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	n = tnf_get_slot_count(datum);
312*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
313*7c478bd9Sstevel@tonic-gate 		slotname = tnf_get_slot_name(datum, i);
314*7c478bd9Sstevel@tonic-gate 		(void) printf("%24s ", slotname);
315*7c478bd9Sstevel@tonic-gate 		describe_brief(tnf_get_slot_indexed(datum, i));
316*7c478bd9Sstevel@tonic-gate 		(void) printf("\n");
317*7c478bd9Sstevel@tonic-gate 		/* tag_arg heuristic */
318*7c478bd9Sstevel@tonic-gate 		if ((i == 0) && tnf_is_record(datum)) {
319*7c478bd9Sstevel@tonic-gate 			tnf_datum_t tag_arg;
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 			if ((tag_arg = tnf_get_tag_arg(datum))
322*7c478bd9Sstevel@tonic-gate 			    != TNF_DATUM_NULL) {
323*7c478bd9Sstevel@tonic-gate 				(void) printf("%24s ", TNF_N_TAG_ARG);
324*7c478bd9Sstevel@tonic-gate 				describe_brief(tag_arg);
325*7c478bd9Sstevel@tonic-gate 				(void) printf("\n");
326*7c478bd9Sstevel@tonic-gate 			}
327*7c478bd9Sstevel@tonic-gate 		}
328*7c478bd9Sstevel@tonic-gate 	}
329*7c478bd9Sstevel@tonic-gate }
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate static void
describe_array(tnf_datum_t datum)332*7c478bd9Sstevel@tonic-gate describe_array(tnf_datum_t datum)
333*7c478bd9Sstevel@tonic-gate {
334*7c478bd9Sstevel@tonic-gate 	unsigned n, i;
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate 	describe_struct(datum);	/* XXX */
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	if (tnf_is_string(datum))
339*7c478bd9Sstevel@tonic-gate 		(void) printf("%24s \"%s\"\n", "chars", tnf_get_chars(datum));
340*7c478bd9Sstevel@tonic-gate 	else {
341*7c478bd9Sstevel@tonic-gate 		n = tnf_get_element_count(datum);
342*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
343*7c478bd9Sstevel@tonic-gate 			(void) printf("%24d ", i);
344*7c478bd9Sstevel@tonic-gate 			describe_brief(tnf_get_element(datum, i));
345*7c478bd9Sstevel@tonic-gate 			(void) printf("\n");
346*7c478bd9Sstevel@tonic-gate 		}
347*7c478bd9Sstevel@tonic-gate 	}
348*7c478bd9Sstevel@tonic-gate }
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate static void
describe_type(tnf_datum_t datum)351*7c478bd9Sstevel@tonic-gate describe_type(tnf_datum_t datum)
352*7c478bd9Sstevel@tonic-gate {
353*7c478bd9Sstevel@tonic-gate 	describe_struct(datum);
354*7c478bd9Sstevel@tonic-gate }
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate static void
describe_brief(tnf_datum_t datum)357*7c478bd9Sstevel@tonic-gate describe_brief(tnf_datum_t datum)
358*7c478bd9Sstevel@tonic-gate {
359*7c478bd9Sstevel@tonic-gate 	if (datum == TNF_DATUM_NULL) /* allowed */
360*7c478bd9Sstevel@tonic-gate 		(void) printf("0x%-8x <NULL>", 0);
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	else if (tnf_is_scalar(datum))
363*7c478bd9Sstevel@tonic-gate 		describe_scalar(datum);
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	else if (tnf_is_record(datum)) {
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 		(void) printf("0x%-8x ",
368*7c478bd9Sstevel@tonic-gate 			OFF(tnf_get_raw(datum))); /* common */
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 		switch (tnf_get_kind(datum)) {
371*7c478bd9Sstevel@tonic-gate 		case TNF_K_TYPE:
372*7c478bd9Sstevel@tonic-gate 			(void) printf("%s", tnf_type_get_name(datum));
373*7c478bd9Sstevel@tonic-gate 			break;
374*7c478bd9Sstevel@tonic-gate 		case TNF_K_STRING:
375*7c478bd9Sstevel@tonic-gate 			(void) printf("\"%s\"", tnf_get_chars(datum));
376*7c478bd9Sstevel@tonic-gate 			break;
377*7c478bd9Sstevel@tonic-gate 		default:
378*7c478bd9Sstevel@tonic-gate 			(void) printf("<%s>", tnf_get_type_name(datum));
379*7c478bd9Sstevel@tonic-gate 		}
380*7c478bd9Sstevel@tonic-gate 	} else
381*7c478bd9Sstevel@tonic-gate 		fail(0, gettext("inline aggregate slots/elements unhandled"));
382*7c478bd9Sstevel@tonic-gate }
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate void
fail(int do_perror,char * message,...)385*7c478bd9Sstevel@tonic-gate fail(int do_perror, char *message, ...)
386*7c478bd9Sstevel@tonic-gate {
387*7c478bd9Sstevel@tonic-gate 	va_list args;
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	va_start(args, message);
390*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%s: "), g_cmdname);
391*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, message, args);
392*7c478bd9Sstevel@tonic-gate 	va_end(args);
393*7c478bd9Sstevel@tonic-gate 	if (do_perror)
394*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(": %s"), strerror(errno));
395*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\n"));
396*7c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
397*7c478bd9Sstevel@tonic-gate }
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate static void
usage(void)400*7c478bd9Sstevel@tonic-gate usage(void)
401*7c478bd9Sstevel@tonic-gate {
402*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
403*7c478bd9Sstevel@tonic-gate 			gettext("Usage: %s [-r] <tnf_file> [<tnf_file> ...]\n"),
404*7c478bd9Sstevel@tonic-gate 			g_cmdname);
405*7c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
406*7c478bd9Sstevel@tonic-gate }
407