xref: /minix3/usr.bin/unexpand/unexpand.c (revision 3dab66925c6b0cd3628a3acbddb9007aaf157f95)
1*3dab6692SLionel Sambuc /*	$NetBSD: unexpand.c,v 1.14 2008/12/21 02:33:13 christos Exp $	*/
2*3dab6692SLionel Sambuc 
3*3dab6692SLionel Sambuc /*-
4*3dab6692SLionel Sambuc  * Copyright (c) 1980, 1993
5*3dab6692SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*3dab6692SLionel Sambuc  *
7*3dab6692SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*3dab6692SLionel Sambuc  * modification, are permitted provided that the following conditions
9*3dab6692SLionel Sambuc  * are met:
10*3dab6692SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*3dab6692SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*3dab6692SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*3dab6692SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*3dab6692SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*3dab6692SLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
16*3dab6692SLionel Sambuc  *    may be used to endorse or promote products derived from this software
17*3dab6692SLionel Sambuc  *    without specific prior written permission.
18*3dab6692SLionel Sambuc  *
19*3dab6692SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*3dab6692SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*3dab6692SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*3dab6692SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*3dab6692SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*3dab6692SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*3dab6692SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*3dab6692SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*3dab6692SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*3dab6692SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*3dab6692SLionel Sambuc  * SUCH DAMAGE.
30*3dab6692SLionel Sambuc  */
31*3dab6692SLionel Sambuc 
32*3dab6692SLionel Sambuc #include <sys/cdefs.h>
33*3dab6692SLionel Sambuc #ifndef lint
34*3dab6692SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35*3dab6692SLionel Sambuc  The Regents of the University of California.  All rights reserved.");
36*3dab6692SLionel Sambuc #endif /* not lint */
37*3dab6692SLionel Sambuc 
38*3dab6692SLionel Sambuc #ifndef lint
39*3dab6692SLionel Sambuc #if 0
40*3dab6692SLionel Sambuc static char sccsid[] = "@(#)unexpand.c	8.1 (Berkeley) 6/6/93";
41*3dab6692SLionel Sambuc #endif
42*3dab6692SLionel Sambuc __RCSID("$NetBSD: unexpand.c,v 1.14 2008/12/21 02:33:13 christos Exp $");
43*3dab6692SLionel Sambuc #endif /* not lint */
44*3dab6692SLionel Sambuc 
45*3dab6692SLionel Sambuc /*
46*3dab6692SLionel Sambuc  * unexpand - put tabs into a file replacing blanks
47*3dab6692SLionel Sambuc  */
48*3dab6692SLionel Sambuc #include <limits.h>
49*3dab6692SLionel Sambuc #include <stdio.h>
50*3dab6692SLionel Sambuc #include <stdlib.h>
51*3dab6692SLionel Sambuc #include <string.h>
52*3dab6692SLionel Sambuc #include <unistd.h>
53*3dab6692SLionel Sambuc #include <errno.h>
54*3dab6692SLionel Sambuc #include <err.h>
55*3dab6692SLionel Sambuc #include <util.h>
56*3dab6692SLionel Sambuc 
57*3dab6692SLionel Sambuc 
58*3dab6692SLionel Sambuc #define DSTOP	8
59*3dab6692SLionel Sambuc static int	all;
60*3dab6692SLionel Sambuc static size_t	nstops;
61*3dab6692SLionel Sambuc static size_t	maxstops;
62*3dab6692SLionel Sambuc static size_t	*tabstops;
63*3dab6692SLionel Sambuc 
64*3dab6692SLionel Sambuc static void	tabify(const char *, size_t);
65*3dab6692SLionel Sambuc static void	usage(void) __attribute__((__noreturn__));
66*3dab6692SLionel Sambuc 
67*3dab6692SLionel Sambuc static void
usage(void)68*3dab6692SLionel Sambuc usage(void)
69*3dab6692SLionel Sambuc {
70*3dab6692SLionel Sambuc     (void)fprintf(stderr, "Usage: %s [-a] [-t tabstop] [file ...]\n",
71*3dab6692SLionel Sambuc 	getprogname());
72*3dab6692SLionel Sambuc     exit(EXIT_FAILURE);
73*3dab6692SLionel Sambuc }
74*3dab6692SLionel Sambuc 
75*3dab6692SLionel Sambuc int
main(int argc,char ** argv)76*3dab6692SLionel Sambuc main(int argc, char **argv)
77*3dab6692SLionel Sambuc {
78*3dab6692SLionel Sambuc 	int c;
79*3dab6692SLionel Sambuc 	char *ep, *tab;
80*3dab6692SLionel Sambuc 	char *line;
81*3dab6692SLionel Sambuc 	size_t len;
82*3dab6692SLionel Sambuc 	unsigned long i;
83*3dab6692SLionel Sambuc 
84*3dab6692SLionel Sambuc 	setprogname(argv[0]);
85*3dab6692SLionel Sambuc 
86*3dab6692SLionel Sambuc 	while ((c = getopt(argc, argv, "at:")) != -1) {
87*3dab6692SLionel Sambuc 		switch (c) {
88*3dab6692SLionel Sambuc 		case 'a':
89*3dab6692SLionel Sambuc 			if (nstops)
90*3dab6692SLionel Sambuc 				usage();
91*3dab6692SLionel Sambuc 			all++;
92*3dab6692SLionel Sambuc 			break;
93*3dab6692SLionel Sambuc 		case 't':
94*3dab6692SLionel Sambuc 			if (all)
95*3dab6692SLionel Sambuc 				usage();
96*3dab6692SLionel Sambuc 			while ((tab = strsep(&optarg, ", \t")) != NULL) {
97*3dab6692SLionel Sambuc 				if (*tab == '\0')
98*3dab6692SLionel Sambuc 					continue;
99*3dab6692SLionel Sambuc 				errno = 0;
100*3dab6692SLionel Sambuc 				i = strtoul(tab, &ep, 0);
101*3dab6692SLionel Sambuc 				if (*ep || (errno == ERANGE && i == ULONG_MAX))
102*3dab6692SLionel Sambuc 					errx(EXIT_FAILURE,
103*3dab6692SLionel Sambuc 					    "Invalid tabstop `%s'", tab);
104*3dab6692SLionel Sambuc 				if (nstops >= maxstops) {
105*3dab6692SLionel Sambuc 					maxstops += 20;
106*3dab6692SLionel Sambuc 					tabstops = erealloc(tabstops, maxstops);
107*3dab6692SLionel Sambuc 				}
108*3dab6692SLionel Sambuc 				if (nstops && tabstops[nstops - 1] >= (size_t)i)
109*3dab6692SLionel Sambuc 					errx(EXIT_FAILURE,
110*3dab6692SLionel Sambuc 					    "Bad tabstop spec `%s', must be "
111*3dab6692SLionel Sambuc 					    "greater than the previous `%zu'",
112*3dab6692SLionel Sambuc 					    tab, tabstops[nstops - 1]);
113*3dab6692SLionel Sambuc 				tabstops[nstops++] = i;
114*3dab6692SLionel Sambuc 			}
115*3dab6692SLionel Sambuc 			break;
116*3dab6692SLionel Sambuc 		case '?':
117*3dab6692SLionel Sambuc 		default:
118*3dab6692SLionel Sambuc 			usage();
119*3dab6692SLionel Sambuc 		}
120*3dab6692SLionel Sambuc 	}
121*3dab6692SLionel Sambuc 	argc -= optind;
122*3dab6692SLionel Sambuc 	argv += optind;
123*3dab6692SLionel Sambuc 
124*3dab6692SLionel Sambuc 	for (i = 0; i < nstops; i++)
125*3dab6692SLionel Sambuc 		fprintf(stderr, "%lu %zu\n", i, tabstops[i]);
126*3dab6692SLionel Sambuc 
127*3dab6692SLionel Sambuc 	do {
128*3dab6692SLionel Sambuc 		if (argc > 0) {
129*3dab6692SLionel Sambuc 			if (freopen(argv[0], "r", stdin) == NULL)
130*3dab6692SLionel Sambuc 				err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
131*3dab6692SLionel Sambuc 			argc--, argv++;
132*3dab6692SLionel Sambuc 		}
133*3dab6692SLionel Sambuc 		while ((line = fgetln(stdin, &len)) != NULL)
134*3dab6692SLionel Sambuc 			tabify(line, len);
135*3dab6692SLionel Sambuc 	} while (argc > 0);
136*3dab6692SLionel Sambuc 	return EXIT_SUCCESS;
137*3dab6692SLionel Sambuc }
138*3dab6692SLionel Sambuc 
139*3dab6692SLionel Sambuc static void
tabify(const char * line,size_t len)140*3dab6692SLionel Sambuc tabify(const char *line, size_t len)
141*3dab6692SLionel Sambuc {
142*3dab6692SLionel Sambuc 	const char *e, *p;
143*3dab6692SLionel Sambuc 	size_t dcol, ocol, limit, n;
144*3dab6692SLionel Sambuc 
145*3dab6692SLionel Sambuc 	dcol = ocol = 0;
146*3dab6692SLionel Sambuc 	limit = nstops == 0 ? UINT_MAX : tabstops[nstops - 1] - 1;
147*3dab6692SLionel Sambuc 	e = line + len;
148*3dab6692SLionel Sambuc 	for (p = line; p < e; p++) {
149*3dab6692SLionel Sambuc 		if (*p == ' ') {
150*3dab6692SLionel Sambuc 			dcol++;
151*3dab6692SLionel Sambuc 			continue;
152*3dab6692SLionel Sambuc 		} else if (*p == '\t') {
153*3dab6692SLionel Sambuc 			if (nstops == 0) {
154*3dab6692SLionel Sambuc 				dcol = (1 + dcol / DSTOP) * DSTOP;
155*3dab6692SLionel Sambuc 				continue;
156*3dab6692SLionel Sambuc 			} else {
157*3dab6692SLionel Sambuc 				for (n = 0; tabstops[n] - 1 < dcol &&
158*3dab6692SLionel Sambuc 				    n < nstops; n++)
159*3dab6692SLionel Sambuc 					continue;
160*3dab6692SLionel Sambuc 				if (n < nstops - 1 && tabstops[n] - 1 < limit) {
161*3dab6692SLionel Sambuc 					dcol = tabstops[n];
162*3dab6692SLionel Sambuc 					continue;
163*3dab6692SLionel Sambuc 				}
164*3dab6692SLionel Sambuc 			}
165*3dab6692SLionel Sambuc 		}
166*3dab6692SLionel Sambuc 
167*3dab6692SLionel Sambuc 		/* Output our tabs */
168*3dab6692SLionel Sambuc 		if (nstops == 0) {
169*3dab6692SLionel Sambuc 			while (((ocol + DSTOP) / DSTOP) <= (dcol / DSTOP)) {
170*3dab6692SLionel Sambuc 				if (dcol - ocol < 2)
171*3dab6692SLionel Sambuc 					break;
172*3dab6692SLionel Sambuc 				if (putchar('\t') == EOF)
173*3dab6692SLionel Sambuc 					goto out;
174*3dab6692SLionel Sambuc 				ocol = (1 + ocol / DSTOP) * DSTOP;
175*3dab6692SLionel Sambuc 			}
176*3dab6692SLionel Sambuc 		} else {
177*3dab6692SLionel Sambuc 			for (n = 0; tabstops[n] <= ocol && n < nstops; n++)
178*3dab6692SLionel Sambuc 				continue;
179*3dab6692SLionel Sambuc 			while (tabstops[n] <= dcol && ocol < dcol &&
180*3dab6692SLionel Sambuc 			    n < nstops && ocol < limit) {
181*3dab6692SLionel Sambuc 				if (putchar('\t') == EOF)
182*3dab6692SLionel Sambuc 					goto out;
183*3dab6692SLionel Sambuc 				ocol = tabstops[n++];
184*3dab6692SLionel Sambuc 			}
185*3dab6692SLionel Sambuc 		}
186*3dab6692SLionel Sambuc 
187*3dab6692SLionel Sambuc 		/* Output remaining spaces */
188*3dab6692SLionel Sambuc 		while (ocol < dcol && ocol < limit) {
189*3dab6692SLionel Sambuc 			if (putchar(' ') == EOF)
190*3dab6692SLionel Sambuc 				goto out;
191*3dab6692SLionel Sambuc 			ocol++;
192*3dab6692SLionel Sambuc 		}
193*3dab6692SLionel Sambuc 
194*3dab6692SLionel Sambuc 		/* Output our char */
195*3dab6692SLionel Sambuc 		if (putchar(*p) == EOF)
196*3dab6692SLionel Sambuc 			goto out;
197*3dab6692SLionel Sambuc 		if (*p == '\b') {
198*3dab6692SLionel Sambuc 			if (ocol > 0) {
199*3dab6692SLionel Sambuc 				ocol--;
200*3dab6692SLionel Sambuc 				dcol--;
201*3dab6692SLionel Sambuc 			}
202*3dab6692SLionel Sambuc 		} else {
203*3dab6692SLionel Sambuc 			ocol++;
204*3dab6692SLionel Sambuc 			dcol++;
205*3dab6692SLionel Sambuc 		}
206*3dab6692SLionel Sambuc 
207*3dab6692SLionel Sambuc 		/* Output remainder of line */
208*3dab6692SLionel Sambuc 		if (!all || dcol >= limit) {
209*3dab6692SLionel Sambuc 			for (p++; p < e; p++)
210*3dab6692SLionel Sambuc 				if (putchar(*p) == EOF)
211*3dab6692SLionel Sambuc 					goto out;
212*3dab6692SLionel Sambuc 			return;
213*3dab6692SLionel Sambuc 		}
214*3dab6692SLionel Sambuc 	}
215*3dab6692SLionel Sambuc 	return;
216*3dab6692SLionel Sambuc out:
217*3dab6692SLionel Sambuc 	err(EXIT_FAILURE, "write failed");
218*3dab6692SLionel Sambuc }
219