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