1/*	$NetBSD: unexpand.c,v 1.18 2019/09/13 17:32:29 dyoung Exp $	*/
2
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#include <sys/cdefs.h>
33#ifndef lint
34__COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California.  All rights reserved.");
36#endif /* not lint */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)unexpand.c	8.1 (Berkeley) 6/6/93";
41#endif
42__RCSID("$NetBSD: unexpand.c,v 1.18 2019/09/13 17:32:29 dyoung Exp $");
43#endif /* not lint */
44
45/*
46 * unexpand - put tabs into a file replacing blanks
47 */
48#include <limits.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53#include <errno.h>
54#include <err.h>
55#include <util.h>
56
57
58#define DSTOP	8
59static int	all;
60static size_t	nstops;
61static size_t	maxstops;
62static size_t	*tabstops;
63
64static void	tabify(const char *, size_t);
65static void	usage(void) __attribute__((__noreturn__));
66
67static void
68usage(void)
69{
70	(void)fprintf(stderr, "Usage: %s [-a] [-t tabstop] [file ...]\n",
71	    getprogname());
72	exit(EXIT_FAILURE);
73}
74
75int
76main(int argc, char **argv)
77{
78	int c;
79	char *ep, *tab;
80	char *line;
81	size_t len;
82	unsigned long i;
83
84	setprogname(argv[0]);
85
86	while ((c = getopt(argc, argv, "at:")) != -1) {
87		switch (c) {
88		case 'a':
89			all = 1;
90			break;
91		case 't':
92			while ((tab = strsep(&optarg, ", \t")) != NULL) {
93				if (*tab == '\0')
94					continue;
95				errno = 0;
96				i = strtoul(tab, &ep, 0);
97				if (*ep || (errno == ERANGE && i == ULONG_MAX))
98					errx(EXIT_FAILURE,
99					    "Invalid tabstop `%s'", tab);
100				if (nstops >= maxstops) {
101					maxstops += 20;
102					tabstops = erealloc(tabstops, maxstops);
103				}
104				if (nstops && tabstops[nstops - 1] >= (size_t)i)
105					errx(EXIT_FAILURE,
106					    "Bad tabstop spec `%s', must be "
107					    "greater than the previous `%zu'",
108					    tab, tabstops[nstops - 1]);
109				tabstops[nstops++] = i;
110			}
111			break;
112		case '?':
113		default:
114			usage();
115		}
116	}
117	argc -= optind;
118	argv += optind;
119
120	do {
121		if (argc > 0) {
122			if (freopen(argv[0], "r", stdin) == NULL)
123				err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
124			argc--, argv++;
125		}
126		while ((line = fgetln(stdin, &len)) != NULL)
127			tabify(line, len);
128	} while (argc > 0);
129	return EXIT_SUCCESS;
130}
131
132static void
133tabify(const char *line, size_t len)
134{
135	const size_t dstop = (nstops == 0) ? DSTOP : tabstops[0];
136	const char *e, *p;
137	size_t dcol, ocol, limit, n;
138
139	dcol = ocol = 0;
140	limit = nstops <= 1 ? UINT_MAX : tabstops[nstops - 1] - 1;
141	e = line + len;
142	for (p = line; p < e; p++) {
143		if (*p == ' ') {
144			dcol++;
145			continue;
146		} else if (*p == '\t') {
147			if (nstops <= 1) {
148				dcol = (1 + dcol / dstop) * dstop;
149				continue;
150			}
151			for (n = 0; n < nstops && tabstops[n] <= dcol; n++)
152				continue;
153			if (n < nstops - 1 && tabstops[n] - 1 < limit) {
154				dcol = tabstops[n];
155				continue;
156			}
157		}
158
159		/* Output our tabs */
160		if (nstops <= 1) {
161			while (((ocol + dstop) / dstop) <= (dcol / dstop)) {
162				if (dcol - ocol < 2)
163					break;
164				if (putchar('\t') == EOF)
165					goto out;
166				ocol = (1 + ocol / dstop) * dstop;
167			}
168		} else {
169			for (n = 0; n < nstops && tabstops[n] <= ocol; n++)
170				continue;
171			while (n < nstops && tabstops[n] <= dcol && ocol < dcol
172			    && ocol < limit) {
173				if (putchar('\t') == EOF)
174					goto out;
175				ocol = tabstops[n++];
176			}
177		}
178
179		/* Output remaining spaces */
180		while (ocol < dcol && ocol < limit) {
181			if (putchar(' ') == EOF)
182				goto out;
183			ocol++;
184		}
185
186		/* Output our char */
187		if (putchar(*p) == EOF)
188			goto out;
189		if (*p == '\b') {
190			if (ocol > 0) {
191				ocol--;
192				dcol--;
193			}
194		} else {
195			ocol++;
196			dcol++;
197		}
198
199		if (all && dcol < limit)
200			continue; /* Collapse the rest of the line. */
201
202		/* Output remainder of line */
203		for (p++; p < e; p++) {
204			if (putchar(*p) == EOF)
205				goto out;
206		}
207		return;
208	}
209	return;
210out:
211	err(EXIT_FAILURE, "write failed");
212}
213