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