xref: /illumos-gate/usr/src/cmd/expand/unexpand.c (revision 2a8bcb4e)
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 /*
41*7c478bd9Sstevel@tonic-gate  * unexpand - put tabs into a file replacing blanks
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate #include <stdio.h>
44*7c478bd9Sstevel@tonic-gate #include <limits.h>
45*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
46*7c478bd9Sstevel@tonic-gate #include <locale.h>
47*7c478bd9Sstevel@tonic-gate #include <libintl.h>
48*7c478bd9Sstevel@tonic-gate #include <wchar.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #define	INPUT_SIZ	LINE_MAX	/* POSIX.2 */
51*7c478bd9Sstevel@tonic-gate #define	MAX_TABS	100		/* maximum number of tabstops */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static int	nstops = 0;		/* total number of tabstops */
54*7c478bd9Sstevel@tonic-gate static int	tabstops[MAX_TABS];	/* the tabstops themselves */
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static void tabify(wchar_t *, int);
57*7c478bd9Sstevel@tonic-gate static void getstops(const char *);
58*7c478bd9Sstevel@tonic-gate static void usage(void);
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate int
main(argc,argv)61*7c478bd9Sstevel@tonic-gate main(argc, argv)
62*7c478bd9Sstevel@tonic-gate int argc;
63*7c478bd9Sstevel@tonic-gate char *argv[];
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	int		flag;		/* option flag read by getopt() */
66*7c478bd9Sstevel@tonic-gate 	int		all = 0;	/* -a flag */
67*7c478bd9Sstevel@tonic-gate 	int		status = 0;
68*7c478bd9Sstevel@tonic-gate 	wchar_t		input_buf[INPUT_SIZ+1];
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
71*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
72*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
73*7c478bd9Sstevel@tonic-gate #endif
74*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	while ((flag = getopt(argc, argv, "at:")) != EOF) {
77*7c478bd9Sstevel@tonic-gate 		switch (flag) {
78*7c478bd9Sstevel@tonic-gate 		case 'a':
79*7c478bd9Sstevel@tonic-gate 			all++;
80*7c478bd9Sstevel@tonic-gate 			break;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 		case 't':		/* POSIX.2 */
83*7c478bd9Sstevel@tonic-gate 			all++;		/* -t turns on -a */
84*7c478bd9Sstevel@tonic-gate 			getstops(optarg);
85*7c478bd9Sstevel@tonic-gate 			break;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 		default:
88*7c478bd9Sstevel@tonic-gate 			usage();
89*7c478bd9Sstevel@tonic-gate 			break;
90*7c478bd9Sstevel@tonic-gate 		}
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	argc -= optind;
94*7c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	do {
97*7c478bd9Sstevel@tonic-gate 		if (argc > 0) {
98*7c478bd9Sstevel@tonic-gate 			if (freopen(argv[0], "r", stdin) == NULL) {
99*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "unexpand: ");
100*7c478bd9Sstevel@tonic-gate 				perror(argv[0]);
101*7c478bd9Sstevel@tonic-gate 				status++;
102*7c478bd9Sstevel@tonic-gate 			}
103*7c478bd9Sstevel@tonic-gate 			argc--, argv++;
104*7c478bd9Sstevel@tonic-gate 		}
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 		while (fgetws(input_buf, INPUT_SIZ, stdin) != NULL) {
107*7c478bd9Sstevel@tonic-gate 			input_buf[INPUT_SIZ] = 0;
108*7c478bd9Sstevel@tonic-gate 			tabify(input_buf, all);
109*7c478bd9Sstevel@tonic-gate 		}
110*7c478bd9Sstevel@tonic-gate 	} while (argc > 0);
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	return (status);
113*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate void
tabify(wchar_t * ibuf,int all)117*7c478bd9Sstevel@tonic-gate tabify(wchar_t *ibuf, int all)
118*7c478bd9Sstevel@tonic-gate {
119*7c478bd9Sstevel@tonic-gate 	wchar_t *cp;		/* current position in ibuf */
120*7c478bd9Sstevel@tonic-gate 	int ocol = 0;		/* current output column */
121*7c478bd9Sstevel@tonic-gate 	int cstop = 0;		/* current tabstop */
122*7c478bd9Sstevel@tonic-gate 	int spaces = 0;		/* spaces to convert to tab */
123*7c478bd9Sstevel@tonic-gate 	int	p_col;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	cp = ibuf;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	for (;;) {
128*7c478bd9Sstevel@tonic-gate 		switch (*cp) {
129*7c478bd9Sstevel@tonic-gate 		case ' ':
130*7c478bd9Sstevel@tonic-gate 			cp++;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 			spaces++;
133*7c478bd9Sstevel@tonic-gate 			ocol++;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 			if (nstops == 0) {		/* default tab = 8 */
136*7c478bd9Sstevel@tonic-gate 				if ((ocol & 7) != 0)
137*7c478bd9Sstevel@tonic-gate 					break;
138*7c478bd9Sstevel@tonic-gate 			} else if (nstops == 1) {	/* tab width */
139*7c478bd9Sstevel@tonic-gate 				if ((ocol % tabstops[0]) != 0)
140*7c478bd9Sstevel@tonic-gate 					break;
141*7c478bd9Sstevel@tonic-gate 			} else {			/* explicit tabstops */
142*7c478bd9Sstevel@tonic-gate 				while (cstop < nstops &&
143*7c478bd9Sstevel@tonic-gate 				    ocol > tabstops[cstop])
144*7c478bd9Sstevel@tonic-gate 					cstop++;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 				if (cstop >= nstops) {
147*7c478bd9Sstevel@tonic-gate 					(void) putchar(' ');
148*7c478bd9Sstevel@tonic-gate 					spaces = 0;
149*7c478bd9Sstevel@tonic-gate 					break;
150*7c478bd9Sstevel@tonic-gate 				}
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 				if (ocol != tabstops[cstop])
153*7c478bd9Sstevel@tonic-gate 					break;
154*7c478bd9Sstevel@tonic-gate 				cstop++;
155*7c478bd9Sstevel@tonic-gate 			}
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 			/*
158*7c478bd9Sstevel@tonic-gate 			 * if we get to this point, we must be at a
159*7c478bd9Sstevel@tonic-gate 			 * tab stop.  if spaces, then write out a tab.
160*7c478bd9Sstevel@tonic-gate 			 */
161*7c478bd9Sstevel@tonic-gate 			if (spaces > 0) {
162*7c478bd9Sstevel@tonic-gate 				(void) putchar(((spaces > 1) ? '\t' : ' '));
163*7c478bd9Sstevel@tonic-gate 				spaces = 0;
164*7c478bd9Sstevel@tonic-gate 			}
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 			break;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 		case '\b':		/* POSIX.2 */
169*7c478bd9Sstevel@tonic-gate 			while (spaces-- > 0)
170*7c478bd9Sstevel@tonic-gate 				(void) putchar(' ');
171*7c478bd9Sstevel@tonic-gate 			spaces = 0;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 			cp++;
174*7c478bd9Sstevel@tonic-gate 			(void) putchar('\b');
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 			if (--ocol < 0)
177*7c478bd9Sstevel@tonic-gate 				ocol = 0;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 			/* just in case */
180*7c478bd9Sstevel@tonic-gate 			cstop = 0;
181*7c478bd9Sstevel@tonic-gate 			break;
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 		case '\t':
184*7c478bd9Sstevel@tonic-gate 			cp++;
185*7c478bd9Sstevel@tonic-gate 			(void) putchar('\t');
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 			/* adjust ocol to current tabstop */
188*7c478bd9Sstevel@tonic-gate 			if (nstops == 0) {
189*7c478bd9Sstevel@tonic-gate 				ocol = (ocol + 8) & ~07;
190*7c478bd9Sstevel@tonic-gate 			} else if (nstops == 1) {
191*7c478bd9Sstevel@tonic-gate 				ocol += ocol % tabstops[0];
192*7c478bd9Sstevel@tonic-gate 			} else {
193*7c478bd9Sstevel@tonic-gate 				if (cstop < nstops &&
194*7c478bd9Sstevel@tonic-gate 				    ocol < tabstops[cstop])
195*7c478bd9Sstevel@tonic-gate 					ocol = tabstops[cstop++];
196*7c478bd9Sstevel@tonic-gate 				else
197*7c478bd9Sstevel@tonic-gate 					ocol++;
198*7c478bd9Sstevel@tonic-gate 			}
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 			spaces = 0;
201*7c478bd9Sstevel@tonic-gate 			break;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 		default:
204*7c478bd9Sstevel@tonic-gate 			while (spaces-- > 0)
205*7c478bd9Sstevel@tonic-gate 				(void) putchar(' ');
206*7c478bd9Sstevel@tonic-gate 			spaces = 0;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 			if (*cp == 0 || *cp == '\n' || all == 0) {
209*7c478bd9Sstevel@tonic-gate 				/*
210*7c478bd9Sstevel@tonic-gate 				 * either end of input line or -a not set
211*7c478bd9Sstevel@tonic-gate 				 */
212*7c478bd9Sstevel@tonic-gate 				while (*cp != 0)
213*7c478bd9Sstevel@tonic-gate 					(void) putwchar(*cp++);
214*7c478bd9Sstevel@tonic-gate 				return;
215*7c478bd9Sstevel@tonic-gate 			}
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 			(void) putwchar(*cp++);
218*7c478bd9Sstevel@tonic-gate 			if ((p_col = wcwidth(*cp)) < 0)
219*7c478bd9Sstevel@tonic-gate 				p_col = 0;
220*7c478bd9Sstevel@tonic-gate 			ocol += p_col;
221*7c478bd9Sstevel@tonic-gate 			break;
222*7c478bd9Sstevel@tonic-gate 		}
223*7c478bd9Sstevel@tonic-gate 	}
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate static void
getstops(const char * cp)227*7c478bd9Sstevel@tonic-gate getstops(const char *cp)
228*7c478bd9Sstevel@tonic-gate {
229*7c478bd9Sstevel@tonic-gate 	register int i;
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	for (;;) {
232*7c478bd9Sstevel@tonic-gate 		i = 0;
233*7c478bd9Sstevel@tonic-gate 		while (*cp >= '0' && *cp <= '9')
234*7c478bd9Sstevel@tonic-gate 			i = i * 10 + *cp++ - '0';
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 		if (i <= 0 || i > INT_MAX) {
237*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
238*7c478bd9Sstevel@tonic-gate 			    "unexpand: invalid tablist item\n"));
239*7c478bd9Sstevel@tonic-gate 			usage();
240*7c478bd9Sstevel@tonic-gate 		}
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 		if (nstops > 0 && i <= tabstops[nstops-1]) {
243*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
244*7c478bd9Sstevel@tonic-gate 			    "unexpand: tablist must be increasing\n"));
245*7c478bd9Sstevel@tonic-gate 			usage();
246*7c478bd9Sstevel@tonic-gate 		}
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 		if (nstops == MAX_TABS) {
249*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
250*7c478bd9Sstevel@tonic-gate 			    "unexpand: number of tabstops limited to %d\n"),
251*7c478bd9Sstevel@tonic-gate 				MAX_TABS);
252*7c478bd9Sstevel@tonic-gate 			usage();
253*7c478bd9Sstevel@tonic-gate 		}
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 		tabstops[nstops++] = i;
256*7c478bd9Sstevel@tonic-gate 		if (*cp == 0)
257*7c478bd9Sstevel@tonic-gate 			break;
258*7c478bd9Sstevel@tonic-gate 		if (*cp != ',' && *cp != ' ') {
259*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
260*7c478bd9Sstevel@tonic-gate 			    "unexpand: invalid tablist separator\n"));
261*7c478bd9Sstevel@tonic-gate 			usage();
262*7c478bd9Sstevel@tonic-gate 		}
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 		cp++;
265*7c478bd9Sstevel@tonic-gate 	}
266*7c478bd9Sstevel@tonic-gate }
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate static void
usage(void)269*7c478bd9Sstevel@tonic-gate usage(void)
270*7c478bd9Sstevel@tonic-gate {
271*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
272*7c478bd9Sstevel@tonic-gate 	    "usage: unexpand [-a ] [-t tablist] [file ...]\n"));
273*7c478bd9Sstevel@tonic-gate 	exit(2);
274*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
275*7c478bd9Sstevel@tonic-gate }
276