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