xref: /illumos-gate/usr/src/cmd/dumpcs/dumpcs.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <ctype.h>
29*7c478bd9Sstevel@tonic-gate #include <string.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <limits.h>
32*7c478bd9Sstevel@tonic-gate #include <locale.h>
33*7c478bd9Sstevel@tonic-gate #include <wctype.h>
34*7c478bd9Sstevel@tonic-gate #include <getwidth.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define	SCRWID 80
37*7c478bd9Sstevel@tonic-gate #define	LC_NAMELEN 255
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #if !defined SS2
40*7c478bd9Sstevel@tonic-gate #define	SS2 0x8e
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate #if !defined SS3
43*7c478bd9Sstevel@tonic-gate #define	SS3 0x8f
44*7c478bd9Sstevel@tonic-gate #endif
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static unsigned int cpl;	/* current characters per line */
47*7c478bd9Sstevel@tonic-gate static unsigned int cplmax;	/* maximum characters per line */
48*7c478bd9Sstevel@tonic-gate static unsigned char codestr[MB_LEN_MAX + 1];
49*7c478bd9Sstevel@tonic-gate static char linebuf[SCRWID / 2 * (MB_LEN_MAX + 1)];
50*7c478bd9Sstevel@tonic-gate static int pinline;		/* any printable to be listed in the line? */
51*7c478bd9Sstevel@tonic-gate static int omitting;		/* omitting blank lines under no vflag? */
52*7c478bd9Sstevel@tonic-gate static int vflag = 0;
53*7c478bd9Sstevel@tonic-gate static int wflag = 0;
54*7c478bd9Sstevel@tonic-gate static int csprint();
55*7c478bd9Sstevel@tonic-gate static int prcode();
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate int
main(ac,av)58*7c478bd9Sstevel@tonic-gate main(ac, av)
59*7c478bd9Sstevel@tonic-gate int ac;
60*7c478bd9Sstevel@tonic-gate char *av[];
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate 	int c;
63*7c478bd9Sstevel@tonic-gate 	short int eucw[4];
64*7c478bd9Sstevel@tonic-gate 	short int scrw[4];
65*7c478bd9Sstevel@tonic-gate 	int csflag[4];
66*7c478bd9Sstevel@tonic-gate 	int cs;
67*7c478bd9Sstevel@tonic-gate 	int i;
68*7c478bd9Sstevel@tonic-gate 	eucwidth_t eucwidth;
69*7c478bd9Sstevel@tonic-gate 	char *lc_ctype;
70*7c478bd9Sstevel@tonic-gate 	char titlebar[LC_NAMELEN + 14];
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
73*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
74*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
75*7c478bd9Sstevel@tonic-gate #endif
76*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
77*7c478bd9Sstevel@tonic-gate 	lc_ctype = setlocale(LC_CTYPE, NULL);
78*7c478bd9Sstevel@tonic-gate 	getwidth(&eucwidth);
79*7c478bd9Sstevel@tonic-gate 	eucw[0] = 1;
80*7c478bd9Sstevel@tonic-gate 	eucw[1] = eucwidth._eucw1;
81*7c478bd9Sstevel@tonic-gate 	eucw[2] = eucwidth._eucw2;
82*7c478bd9Sstevel@tonic-gate 	eucw[3] = eucwidth._eucw3;
83*7c478bd9Sstevel@tonic-gate 	scrw[0] = 1;
84*7c478bd9Sstevel@tonic-gate 	scrw[1] = eucwidth._scrw1;
85*7c478bd9Sstevel@tonic-gate 	scrw[2] = eucwidth._scrw2;
86*7c478bd9Sstevel@tonic-gate 	scrw[3] = eucwidth._scrw3;
87*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= 3; i++)
88*7c478bd9Sstevel@tonic-gate 		csflag[i] = 0;
89*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < ac; i++)
90*7c478bd9Sstevel@tonic-gate 		if (*av[i] != '-')
91*7c478bd9Sstevel@tonic-gate 			goto usage;
92*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(ac, av, "0123vw")) != -1) {
93*7c478bd9Sstevel@tonic-gate 		switch (c) {
94*7c478bd9Sstevel@tonic-gate 			case '0':
95*7c478bd9Sstevel@tonic-gate 			case '1':
96*7c478bd9Sstevel@tonic-gate 			case '2':
97*7c478bd9Sstevel@tonic-gate 			case '3':
98*7c478bd9Sstevel@tonic-gate 				csflag[c - '0'] = 1;
99*7c478bd9Sstevel@tonic-gate 				break;
100*7c478bd9Sstevel@tonic-gate 			case 'v':
101*7c478bd9Sstevel@tonic-gate 				vflag++;
102*7c478bd9Sstevel@tonic-gate 				break;
103*7c478bd9Sstevel@tonic-gate 			case 'w':
104*7c478bd9Sstevel@tonic-gate 				wflag++;
105*7c478bd9Sstevel@tonic-gate 				break;
106*7c478bd9Sstevel@tonic-gate 			default:
107*7c478bd9Sstevel@tonic-gate 			usage:
108*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext("usage: %s [-0123vw]\n"), av[0]);
109*7c478bd9Sstevel@tonic-gate 				exit (1);
110*7c478bd9Sstevel@tonic-gate 		}
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 	if ((csflag[0] + csflag[1] + csflag[2] + csflag[3]) == 0) {
113*7c478bd9Sstevel@tonic-gate 		for (i = 0; i <= 3; i++)
114*7c478bd9Sstevel@tonic-gate 			csflag[i]++;
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 	(void) strcpy(titlebar, "");
117*7c478bd9Sstevel@tonic-gate 	for (i = strlen(lc_ctype) + 14; i; i--)
118*7c478bd9Sstevel@tonic-gate 		(void) strcat(titlebar, ":");
119*7c478bd9Sstevel@tonic-gate 	for (cs = 0; cs <= 3; cs++) {
120*7c478bd9Sstevel@tonic-gate 		if (csflag[cs] && eucw[cs] && scrw[cs]) {
121*7c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", titlebar);
122*7c478bd9Sstevel@tonic-gate 			(void) printf("LC_CTYPE:%s", lc_ctype);
123*7c478bd9Sstevel@tonic-gate 			(void) printf(" CS:%d\n", cs);
124*7c478bd9Sstevel@tonic-gate 			(void) printf("%s", titlebar);
125*7c478bd9Sstevel@tonic-gate 			(void) csprint(cs, (int) eucw[cs], (int) scrw[cs]);
126*7c478bd9Sstevel@tonic-gate 			(void) printf("\n");
127*7c478bd9Sstevel@tonic-gate 		}
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 	return (0);
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate int
csprint(cs,bytes,columns)133*7c478bd9Sstevel@tonic-gate csprint(cs, bytes, columns)
134*7c478bd9Sstevel@tonic-gate int cs, bytes, columns;
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate 	int col, i, position;
137*7c478bd9Sstevel@tonic-gate 	int bytelen;
138*7c478bd9Sstevel@tonic-gate 	int minvalue;
139*7c478bd9Sstevel@tonic-gate 	int maxvalue;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	col = SCRWID - bytes * 2 - 1;
142*7c478bd9Sstevel@tonic-gate 	cplmax = 1;
143*7c478bd9Sstevel@tonic-gate 	for (i = columns + 1; i <= col; i *= 2) cplmax *= 2;
144*7c478bd9Sstevel@tonic-gate 	cplmax /= 2;
145*7c478bd9Sstevel@tonic-gate 	bytelen = bytes;
146*7c478bd9Sstevel@tonic-gate 	minvalue = 0x20;
147*7c478bd9Sstevel@tonic-gate 	maxvalue = 0x7f;
148*7c478bd9Sstevel@tonic-gate 	position = 0;
149*7c478bd9Sstevel@tonic-gate 	if (cs > 0) {
150*7c478bd9Sstevel@tonic-gate 		minvalue = 0xa0;
151*7c478bd9Sstevel@tonic-gate 		maxvalue = 0xff;
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 	if (cs == 2) {
154*7c478bd9Sstevel@tonic-gate 		codestr[position++] = SS2;
155*7c478bd9Sstevel@tonic-gate 		bytelen++;
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 	if (cs == 3) {
158*7c478bd9Sstevel@tonic-gate 		codestr[position++] = SS3;
159*7c478bd9Sstevel@tonic-gate 		bytelen++;
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 	codestr[position] = '\0';
162*7c478bd9Sstevel@tonic-gate 	cpl = 0;
163*7c478bd9Sstevel@tonic-gate 	(void) strcpy(linebuf, "");
164*7c478bd9Sstevel@tonic-gate 	(void) prcode(bytelen, position, minvalue, maxvalue, columns);
165*7c478bd9Sstevel@tonic-gate 	if (pinline || vflag) {
166*7c478bd9Sstevel@tonic-gate 		(void) printf("\n%s", linebuf);
167*7c478bd9Sstevel@tonic-gate 	} else if (!omitting) {
168*7c478bd9Sstevel@tonic-gate 		(void) printf("\n*");
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate 	omitting = 0;
171*7c478bd9Sstevel@tonic-gate 	return (0);
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate /*
175*7c478bd9Sstevel@tonic-gate  * prcode() prints series of len-byte codes, of which each byte can
176*7c478bd9Sstevel@tonic-gate  * have a code value between min and max, in incremental code order.
177*7c478bd9Sstevel@tonic-gate  */
178*7c478bd9Sstevel@tonic-gate int
prcode(len,pos,min,max,col)179*7c478bd9Sstevel@tonic-gate prcode(len, pos, min, max, col)
180*7c478bd9Sstevel@tonic-gate int len, pos, min, max, col;
181*7c478bd9Sstevel@tonic-gate {
182*7c478bd9Sstevel@tonic-gate 	int byte, i, nextpos;
183*7c478bd9Sstevel@tonic-gate 	unsigned long widechar;	/* limitting wchar_t width - not good */
184*7c478bd9Sstevel@tonic-gate 	char *prep;
185*7c478bd9Sstevel@tonic-gate 	wchar_t wc;
186*7c478bd9Sstevel@tonic-gate 	int	mbl;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	if (len - pos > 1) {
189*7c478bd9Sstevel@tonic-gate 		for (byte = min; byte <= max; byte++) {
190*7c478bd9Sstevel@tonic-gate 			codestr[pos] = (unsigned char) byte;
191*7c478bd9Sstevel@tonic-gate 			nextpos = pos + 1;
192*7c478bd9Sstevel@tonic-gate 			codestr[nextpos] = '\0';
193*7c478bd9Sstevel@tonic-gate 			(void) prcode(len, nextpos, min, max, col);
194*7c478bd9Sstevel@tonic-gate 		}
195*7c478bd9Sstevel@tonic-gate 	} else {
196*7c478bd9Sstevel@tonic-gate 		for (byte = min; byte <= max; byte++) {
197*7c478bd9Sstevel@tonic-gate 			codestr[pos] = (unsigned char) byte;
198*7c478bd9Sstevel@tonic-gate 			nextpos = pos + 1;
199*7c478bd9Sstevel@tonic-gate 			codestr[nextpos] = '\0';
200*7c478bd9Sstevel@tonic-gate 			if (!cpl) {
201*7c478bd9Sstevel@tonic-gate 				widechar = 0;
202*7c478bd9Sstevel@tonic-gate 				i = 0;
203*7c478bd9Sstevel@tonic-gate 				while (codestr[i] != '\0') {
204*7c478bd9Sstevel@tonic-gate 					widechar = (widechar << 8) |
205*7c478bd9Sstevel@tonic-gate 						(unsigned long) codestr[i++];
206*7c478bd9Sstevel@tonic-gate 				}
207*7c478bd9Sstevel@tonic-gate 				if (*linebuf) {
208*7c478bd9Sstevel@tonic-gate 					if (pinline || vflag) {
209*7c478bd9Sstevel@tonic-gate 						(void) printf("\n%s", linebuf);
210*7c478bd9Sstevel@tonic-gate 						omitting = 0;
211*7c478bd9Sstevel@tonic-gate 					} else if (!omitting) {
212*7c478bd9Sstevel@tonic-gate 						(void) printf("\n*");
213*7c478bd9Sstevel@tonic-gate 						omitting++;
214*7c478bd9Sstevel@tonic-gate 					}
215*7c478bd9Sstevel@tonic-gate 				}
216*7c478bd9Sstevel@tonic-gate 				if (!wflag) {
217*7c478bd9Sstevel@tonic-gate 					(void) sprintf(linebuf, "%lx ",
218*7c478bd9Sstevel@tonic-gate 					    widechar);
219*7c478bd9Sstevel@tonic-gate 				} else {
220*7c478bd9Sstevel@tonic-gate 					(void) mbtowc(&wc, (char *) codestr,
221*7c478bd9Sstevel@tonic-gate 					    MB_CUR_MAX);
222*7c478bd9Sstevel@tonic-gate 					(void) sprintf(linebuf, "%lx ", wc);
223*7c478bd9Sstevel@tonic-gate 				}
224*7c478bd9Sstevel@tonic-gate 				pinline = 0;
225*7c478bd9Sstevel@tonic-gate 			}
226*7c478bd9Sstevel@tonic-gate 			prep = " ";
227*7c478bd9Sstevel@tonic-gate 			if ((mbl = mbtowc(&wc, (char *) codestr,
228*7c478bd9Sstevel@tonic-gate 			    MB_CUR_MAX)) < 0)
229*7c478bd9Sstevel@tonic-gate 				prep = "*";
230*7c478bd9Sstevel@tonic-gate 			if (mbl == 1) {
231*7c478bd9Sstevel@tonic-gate 				if (!(isprint(codestr[0]))) prep = "*";
232*7c478bd9Sstevel@tonic-gate 			} else if (!(iswprint(wc)))
233*7c478bd9Sstevel@tonic-gate 				prep = "*";
234*7c478bd9Sstevel@tonic-gate 			if (prep[0] == '*') {
235*7c478bd9Sstevel@tonic-gate 				(void) strcat(linebuf, prep);
236*7c478bd9Sstevel@tonic-gate 				for (i = 1; i <= col; i++)
237*7c478bd9Sstevel@tonic-gate 					(void) strcat(linebuf, " ");
238*7c478bd9Sstevel@tonic-gate 			} else {
239*7c478bd9Sstevel@tonic-gate 				(void) strcat(linebuf, prep);
240*7c478bd9Sstevel@tonic-gate 				(void) strcat(linebuf, (char *) codestr);
241*7c478bd9Sstevel@tonic-gate 				pinline = 1;
242*7c478bd9Sstevel@tonic-gate 			}
243*7c478bd9Sstevel@tonic-gate 			cpl++;
244*7c478bd9Sstevel@tonic-gate 			cpl = cpl % cplmax;
245*7c478bd9Sstevel@tonic-gate 		}
246*7c478bd9Sstevel@tonic-gate 	}
247*7c478bd9Sstevel@tonic-gate 	return (0);
248*7c478bd9Sstevel@tonic-gate }
249