xref: /onnv-gate/usr/src/cmd/expand/expand.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 1989 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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include <stdio.h>
43*0Sstevel@tonic-gate #include <libintl.h>
44*0Sstevel@tonic-gate #include <locale.h>
45*0Sstevel@tonic-gate #include <stdlib.h>
46*0Sstevel@tonic-gate #include <string.h>
47*0Sstevel@tonic-gate #include <ctype.h>
48*0Sstevel@tonic-gate #include <limits.h>
49*0Sstevel@tonic-gate #include <wchar.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /*
52*0Sstevel@tonic-gate  * expand - expand tabs to equivalent spaces
53*0Sstevel@tonic-gate  */
54*0Sstevel@tonic-gate static int		nstops = 0;
55*0Sstevel@tonic-gate static int		tabstops[100];
56*0Sstevel@tonic-gate static int		isClocale;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate static void getstops(const char *);
59*0Sstevel@tonic-gate static void usage(void);
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate int
main(argc,argv)62*0Sstevel@tonic-gate main(argc, argv)
63*0Sstevel@tonic-gate int argc;
64*0Sstevel@tonic-gate char *argv[];
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate 	static char	ibuf[BUFSIZ];
67*0Sstevel@tonic-gate 	register int	c, column;
68*0Sstevel@tonic-gate 	register int	n;
69*0Sstevel@tonic-gate 	register int	i, j;
70*0Sstevel@tonic-gate 	char		*locale;
71*0Sstevel@tonic-gate 	int		flag, tflag = 0;
72*0Sstevel@tonic-gate 	int		len;
73*0Sstevel@tonic-gate 	int		p_col;
74*0Sstevel@tonic-gate 	wchar_t		wc;
75*0Sstevel@tonic-gate 	char		*p1, *p2;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
78*0Sstevel@tonic-gate 	locale = setlocale(LC_CTYPE, NULL);
79*0Sstevel@tonic-gate 	isClocale = (strcmp(locale, "C") == 0);
80*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
81*0Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
82*0Sstevel@tonic-gate #endif
83*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	/*
86*0Sstevel@tonic-gate 	 * First, look for and extract any "-<number>" args then pass
87*0Sstevel@tonic-gate 	 * them to getstops().
88*0Sstevel@tonic-gate 	 */
89*0Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
90*0Sstevel@tonic-gate 		if (strcmp(argv[i], "--") == 0)
91*0Sstevel@tonic-gate 			break;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 		if (*argv[i] != '-')
94*0Sstevel@tonic-gate 			continue;
95*0Sstevel@tonic-gate 		if (!isdigit(*(argv[i]+1)))
96*0Sstevel@tonic-gate 			continue;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 		getstops(argv[i]+1);
99*0Sstevel@tonic-gate 		tflag++;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 		/* Pull this arg from list */
102*0Sstevel@tonic-gate 		for (j = i; j < (argc-1); j++)
103*0Sstevel@tonic-gate 			argv[j] = argv[j+1];
104*0Sstevel@tonic-gate 		argc--;
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	while ((flag = getopt(argc, argv, "t:")) != EOF) {
108*0Sstevel@tonic-gate 		switch (flag) {
109*0Sstevel@tonic-gate 		case 't':
110*0Sstevel@tonic-gate 			if (tflag)
111*0Sstevel@tonic-gate 				usage();
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 			getstops(optarg);
114*0Sstevel@tonic-gate 			break;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 		default:
117*0Sstevel@tonic-gate 			usage();
118*0Sstevel@tonic-gate 			break;
119*0Sstevel@tonic-gate 		}
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	argc -= optind;
123*0Sstevel@tonic-gate 	argv = &argv[optind];
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	do {
126*0Sstevel@tonic-gate 		if (argc > 0) {
127*0Sstevel@tonic-gate 			if (freopen(argv[0], "r", stdin) == NULL) {
128*0Sstevel@tonic-gate 				perror(argv[0]);
129*0Sstevel@tonic-gate 				exit(1);
130*0Sstevel@tonic-gate 				/* NOTREACHED */
131*0Sstevel@tonic-gate 			}
132*0Sstevel@tonic-gate 			argc--;
133*0Sstevel@tonic-gate 			argv++;
134*0Sstevel@tonic-gate 		}
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 		column = 0;
137*0Sstevel@tonic-gate 		p1 = p2 = ibuf;
138*0Sstevel@tonic-gate 		for (;;) {
139*0Sstevel@tonic-gate 			if (p1 >= p2) {
140*0Sstevel@tonic-gate 				p1 = ibuf;
141*0Sstevel@tonic-gate 				if ((len = fread(p1, 1, BUFSIZ, stdin)) <= 0)
142*0Sstevel@tonic-gate 					break;
143*0Sstevel@tonic-gate 				p2 = p1 + len;
144*0Sstevel@tonic-gate 			}
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 			c = *p1++;
147*0Sstevel@tonic-gate 			switch (c) {
148*0Sstevel@tonic-gate 			case '\t':
149*0Sstevel@tonic-gate 				if (nstops == 0) {
150*0Sstevel@tonic-gate 					do {
151*0Sstevel@tonic-gate 						(void) putchar(' ');
152*0Sstevel@tonic-gate 						column++;
153*0Sstevel@tonic-gate 					} while (column & 07);
154*0Sstevel@tonic-gate 					continue;
155*0Sstevel@tonic-gate 				}
156*0Sstevel@tonic-gate 				if (nstops == 1) {
157*0Sstevel@tonic-gate 					do {
158*0Sstevel@tonic-gate 						(void) putchar(' ');
159*0Sstevel@tonic-gate 						column++;
160*0Sstevel@tonic-gate 					} while (
161*0Sstevel@tonic-gate 					    ((column - 1) % tabstops[0]) !=
162*0Sstevel@tonic-gate 						(tabstops[0] - 1));
163*0Sstevel@tonic-gate 					continue;
164*0Sstevel@tonic-gate 				}
165*0Sstevel@tonic-gate 				for (n = 0; n < nstops; n++)
166*0Sstevel@tonic-gate 					if (tabstops[n] > column)
167*0Sstevel@tonic-gate 						break;
168*0Sstevel@tonic-gate 				if (n == nstops) {
169*0Sstevel@tonic-gate 					(void) putchar(' ');
170*0Sstevel@tonic-gate 					column++;
171*0Sstevel@tonic-gate 					continue;
172*0Sstevel@tonic-gate 				}
173*0Sstevel@tonic-gate 				while (column < tabstops[n]) {
174*0Sstevel@tonic-gate 					(void) putchar(' ');
175*0Sstevel@tonic-gate 					column++;
176*0Sstevel@tonic-gate 				}
177*0Sstevel@tonic-gate 				continue;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 			case '\b':
180*0Sstevel@tonic-gate 				if (column)
181*0Sstevel@tonic-gate 					column--;
182*0Sstevel@tonic-gate 				(void) putchar('\b');
183*0Sstevel@tonic-gate 				continue;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 			default:
186*0Sstevel@tonic-gate 				if (isClocale) {
187*0Sstevel@tonic-gate 					(void) putchar(c);
188*0Sstevel@tonic-gate 					column++;
189*0Sstevel@tonic-gate 					continue;
190*0Sstevel@tonic-gate 				}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 				if (isascii(c)) {
193*0Sstevel@tonic-gate 					(void) putchar(c);
194*0Sstevel@tonic-gate 					column++;
195*0Sstevel@tonic-gate 					continue;
196*0Sstevel@tonic-gate 				}
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 				p1--;
199*0Sstevel@tonic-gate 				if ((len = (p2 - p1)) <
200*0Sstevel@tonic-gate 					(unsigned int)MB_CUR_MAX) {
201*0Sstevel@tonic-gate 					for (n = 0; n < len; n++)
202*0Sstevel@tonic-gate 						ibuf[n] = *p1++;
203*0Sstevel@tonic-gate 					p1 = ibuf;
204*0Sstevel@tonic-gate 					p2 = p1 + n;
205*0Sstevel@tonic-gate 					if ((len = fread(p2, 1, BUFSIZ - n,
206*0Sstevel@tonic-gate 							stdin)) > 0)
207*0Sstevel@tonic-gate 						p2 += len;
208*0Sstevel@tonic-gate 				}
209*0Sstevel@tonic-gate 				if ((len = (p2 - p1)) >
210*0Sstevel@tonic-gate 					(unsigned int)MB_CUR_MAX)
211*0Sstevel@tonic-gate 					len = (unsigned int)MB_CUR_MAX;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 				if ((len = mbtowc(&wc, p1, len)) <= 0) {
214*0Sstevel@tonic-gate 					(void) putchar(c);
215*0Sstevel@tonic-gate 					column++;
216*0Sstevel@tonic-gate 					p1++;
217*0Sstevel@tonic-gate 					continue;
218*0Sstevel@tonic-gate 				}
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 				if ((p_col = wcwidth(wc)) < 0)
221*0Sstevel@tonic-gate 					p_col = len;
222*0Sstevel@tonic-gate 				p1 += len;
223*0Sstevel@tonic-gate 				(void) putwchar(wc);
224*0Sstevel@tonic-gate 				column += p_col;
225*0Sstevel@tonic-gate 				continue;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 			case '\n':
228*0Sstevel@tonic-gate 				(void) putchar(c);
229*0Sstevel@tonic-gate 				column = 0;
230*0Sstevel@tonic-gate 				continue;
231*0Sstevel@tonic-gate 			}
232*0Sstevel@tonic-gate 		}
233*0Sstevel@tonic-gate 	} while (argc > 0);
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	return (0);
236*0Sstevel@tonic-gate 	/* NOTREACHED */
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate static void
getstops(const char * cp)240*0Sstevel@tonic-gate getstops(const char *cp)
241*0Sstevel@tonic-gate {
242*0Sstevel@tonic-gate 	register int i;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	for (;;) {
245*0Sstevel@tonic-gate 		i = 0;
246*0Sstevel@tonic-gate 		while (*cp >= '0' && *cp <= '9')
247*0Sstevel@tonic-gate 			i = i * 10 + *cp++ - '0';
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 		if (i <= 0 || i > INT_MAX) {
250*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
251*0Sstevel@tonic-gate 				"expand: invalid tablist\n"));
252*0Sstevel@tonic-gate 			usage();
253*0Sstevel@tonic-gate 		}
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 		if (nstops > 0 && i <= tabstops[nstops-1]) {
256*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
257*0Sstevel@tonic-gate 				"expand: tablist must be increasing\n"));
258*0Sstevel@tonic-gate 			usage();
259*0Sstevel@tonic-gate 		}
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 		tabstops[nstops++] = i;
262*0Sstevel@tonic-gate 		if (*cp == 0)
263*0Sstevel@tonic-gate 			break;
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 		if (*cp != ',' && *cp != ' ') {
266*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
267*0Sstevel@tonic-gate 				"expand: invalid tablist\n"));
268*0Sstevel@tonic-gate 			usage();
269*0Sstevel@tonic-gate 		}
270*0Sstevel@tonic-gate 		cp++;
271*0Sstevel@tonic-gate 	}
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate static void
usage(void)275*0Sstevel@tonic-gate usage(void)
276*0Sstevel@tonic-gate {
277*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
278*0Sstevel@tonic-gate 		"usage: expand [-t tablist] [file ...]\n"
279*0Sstevel@tonic-gate 		"       expand [-tabstop] [-tab1,tab2,...,tabn] [file ...]\n"));
280*0Sstevel@tonic-gate 	exit(2);
281*0Sstevel@tonic-gate 	/* NOTREACHED */
282*0Sstevel@tonic-gate }
283