xref: /freebsd/usr.bin/unexpand/unexpand.c (revision 675aae732d3dd2ec0f12f39df3a1fbfe693a4ebd)
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 #include <sys/cdefs.h>
33 
34 __FBSDID("$FreeBSD$");
35 
36 #ifndef lint
37 static const char copyright[] =
38 "@(#) Copyright (c) 1980, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif
41 
42 #ifndef lint
43 static const char sccsid[] = "@(#)unexpand.c	8.1 (Berkeley) 6/6/93";
44 #endif
45 
46 /*
47  * unexpand - put tabs into a file replacing blanks
48  */
49 #include <ctype.h>
50 #include <err.h>
51 #include <limits.h>
52 #include <locale.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <wchar.h>
58 #include <wctype.h>
59 
60 static int	all;
61 static int	nstops;
62 static int	tabstops[100];
63 
64 static void getstops(const char *);
65 static void usage(void);
66 static int tabify(const char *);
67 
68 int
69 main(int argc, char *argv[])
70 {
71 	int ch, failed;
72 	char *filename;
73 
74 	setlocale(LC_CTYPE, "");
75 
76 	nstops = 1;
77 	tabstops[0] = 8;
78 	while ((ch = getopt(argc, argv, "at:")) != -1) {
79 		switch (ch) {
80 		case 'a':	/* Un-expand all spaces, not just leading. */
81 			all = 1;
82 			break;
83 		case 't':	/* Specify tab list, implies -a. */
84 			getstops(optarg);
85 			all = 1;
86 			break;
87 		default:
88 			usage();
89 			/*NOTREACHED*/
90 		}
91 	}
92 	argc -= optind;
93 	argv += optind;
94 
95 	failed = 0;
96 	if (argc == 0)
97 		failed |= tabify("stdin");
98 	else {
99 		while ((filename = *argv++) != NULL) {
100 			if (freopen(filename, "r", stdin) == NULL) {
101 				warn("%s", filename);
102 				failed = 1;
103 			} else
104 				failed |= tabify(filename);
105 		}
106 	}
107 	exit(failed != 0);
108 }
109 
110 static void
111 usage(void)
112 {
113 	fprintf(stderr, "usage: unexpand [-a | -t tablist] [file ...]\n");
114 	exit(1);
115 }
116 
117 static int
118 tabify(const char *curfile)
119 {
120 	int dcol, doneline, limit, n, ocol, width;
121 	wint_t ch;
122 
123 	limit = nstops == 1 ? INT_MAX : tabstops[nstops - 1] - 1;
124 
125 	doneline = ocol = dcol = 0;
126 	while ((ch = getwchar()) != WEOF) {
127 		if (ch == ' ' && !doneline) {
128 			if (++dcol >= limit)
129 				doneline = 1;
130 			continue;
131 		} else if (ch == '\t') {
132 			if (nstops == 1) {
133 				dcol = (1 + dcol / tabstops[0]) *
134 				    tabstops[0];
135 				continue;
136 			} else {
137 				for (n = 0; n < nstops &&
138 				    tabstops[n] - 1 < dcol; n++)
139 					;
140 				if (n < nstops - 1 && tabstops[n] - 1 < limit) {
141 					dcol = tabstops[n];
142 					continue;
143 				}
144 				doneline = 1;
145 			}
146 		}
147 
148 		/* Output maximal number of tabs. */
149 		if (nstops == 1) {
150 			while (((ocol + tabstops[0]) / tabstops[0])
151 			    <= (dcol / tabstops[0])) {
152 				if (dcol - ocol < 2)
153 					break;
154 				putwchar('\t');
155 				ocol = (1 + ocol / tabstops[0]) *
156 				    tabstops[0];
157 			}
158 		} else {
159 			for (n = 0; n < nstops && tabstops[n] - 1 < ocol; n++)
160 				;
161 			while (ocol < dcol && n < nstops && ocol < limit) {
162 				putwchar('\t');
163 				ocol = tabstops[n++];
164 			}
165 		}
166 
167 		/* Then spaces. */
168 		while (ocol < dcol && ocol < limit) {
169 			putwchar(' ');
170 			ocol++;
171 		}
172 
173 		if (ch == '\b') {
174 			putwchar('\b');
175 			if (ocol > 0)
176 				ocol--, dcol--;
177 		} else if (ch == '\n') {
178 			putwchar('\n');
179 			doneline = ocol = dcol = 0;
180 			continue;
181 		} else if (ch != ' ' || dcol > limit) {
182 			putwchar(ch);
183 			if ((width = wcwidth(ch)) > 0)
184 				ocol += width, dcol += width;
185 		}
186 
187 		/*
188 		 * Only processing leading blanks or we've gone past the
189 		 * last tab stop. Emit remainder of this line unchanged.
190 		 */
191 		if (!all || dcol >= limit) {
192 			while ((ch = getwchar()) != '\n' && ch != WEOF)
193 				putwchar(ch);
194 			if (ch == '\n')
195 				putwchar('\n');
196 			doneline = ocol = dcol = 0;
197 		}
198 	}
199 	if (ferror(stdin)) {
200 		warn("%s", curfile);
201 		return (1);
202 	}
203 	return (0);
204 }
205 
206 static void
207 getstops(const char *cp)
208 {
209 	int i;
210 
211 	nstops = 0;
212 	for (;;) {
213 		i = 0;
214 		while (*cp >= '0' && *cp <= '9')
215 			i = i * 10 + *cp++ - '0';
216 		if (i <= 0)
217 			errx(1, "bad tab stop spec");
218 		if (nstops > 0 && i <= tabstops[nstops-1])
219 			errx(1, "bad tab stop spec");
220 		if (nstops == sizeof(tabstops) / sizeof(*tabstops))
221 			errx(1, "too many tab stops");
222 		tabstops[nstops++] = i;
223 		if (*cp == 0)
224 			break;
225 		if (*cp != ',' && !isblank((unsigned char)*cp))
226 			errx(1, "bad tab stop spec");
227 		cp++;
228 	}
229 }
230