2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * unexpand - put tabs into a file replacing blanks
48 static int tabstops[100];
50 static void getstops(const char *);
51 static void usage(void) __dead2;
52 static int tabify(const char *);
55 main(int argc, char *argv[])
60 setlocale(LC_CTYPE, "");
64 while ((ch = getopt(argc, argv, "at:")) != -1) {
66 case 'a': /* Un-expand all spaces, not just leading. */
69 case 't': /* Specify tab list, implies -a. */
83 failed |= tabify("stdin");
85 while ((filename = *argv++) != NULL) {
86 if (freopen(filename, "r", stdin) == NULL) {
90 failed |= tabify(filename);
99 fprintf(stderr, "usage: unexpand [-a | -t tablist] [file ...]\n");
104 tabify(const char *curfile)
106 int dcol, doneline, limit, n, ocol, width;
109 limit = nstops == 1 ? INT_MAX : tabstops[nstops - 1] - 1;
111 doneline = ocol = dcol = 0;
112 while ((ch = getwchar()) != WEOF) {
113 if (ch == ' ' && !doneline) {
117 } else if (ch == '\t') {
119 dcol = (1 + dcol / tabstops[0]) *
123 for (n = 0; n < nstops &&
124 tabstops[n] - 1 < dcol; n++)
126 if (n < nstops - 1 && tabstops[n] - 1 < limit) {
134 /* Output maximal number of tabs. */
136 while (((ocol + tabstops[0]) / tabstops[0])
137 <= (dcol / tabstops[0])) {
141 ocol = (1 + ocol / tabstops[0]) *
145 for (n = 0; n < nstops && tabstops[n] - 1 < ocol; n++)
147 while (ocol < dcol && n < nstops && ocol < limit) {
149 ocol = tabstops[n++];
154 while (ocol < dcol && ocol < limit) {
163 } else if (ch == '\n') {
165 doneline = ocol = dcol = 0;
167 } else if (ch != ' ' || dcol > limit) {
169 if ((width = wcwidth(ch)) > 0)
170 ocol += width, dcol += width;
174 * Only processing leading blanks or we've gone past the
175 * last tab stop. Emit remainder of this line unchanged.
177 if (!all || dcol >= limit) {
178 while ((ch = getwchar()) != '\n' && ch != WEOF)
182 doneline = ocol = dcol = 0;
193 getstops(const char *cp)
200 while (*cp >= '0' && *cp <= '9')
201 i = i * 10 + *cp++ - '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;
211 if (*cp != ',' && !isblank((unsigned char)*cp))
212 errx(1, "bad tab stop spec");