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