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