xref: /freebsd/usr.bin/unexpand/unexpand.c (revision 640235e2c2ba32947f7c59d168437ffa1280f1e6)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 
32 __FBSDID("$FreeBSD$");
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif
39 
40 #ifndef lint
41 static const char sccsid[] = "@(#)unexpand.c	8.1 (Berkeley) 6/6/93";
42 #endif
43 
44 /*
45  * unexpand - put tabs into a file replacing blanks
46  */
47 #include <ctype.h>
48 #include <err.h>
49 #include <limits.h>
50 #include <locale.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <wchar.h>
56 #include <wctype.h>
57 
58 static int	all;
59 static int	nstops;
60 static int	tabstops[100];
61 
62 static void getstops(const char *);
63 static void usage(void);
64 static int tabify(const char *);
65 
66 int
67 main(int argc, char *argv[])
68 {
69 	int ch, failed;
70 	char *filename;
71 
72 	setlocale(LC_CTYPE, "");
73 
74 	nstops = 1;
75 	tabstops[0] = 8;
76 	while ((ch = getopt(argc, argv, "at:")) != -1) {
77 		switch (ch) {
78 		case 'a':	/* Un-expand all spaces, not just leading. */
79 			all = 1;
80 			break;
81 		case 't':	/* Specify tab list, implies -a. */
82 			getstops(optarg);
83 			all = 1;
84 			break;
85 		default:
86 			usage();
87 			/*NOTREACHED*/
88 		}
89 	}
90 	argc -= optind;
91 	argv += optind;
92 
93 	failed = 0;
94 	if (argc == 0)
95 		failed |= tabify("stdin");
96 	else {
97 		while ((filename = *argv++) != NULL) {
98 			if (freopen(filename, "r", stdin) == NULL) {
99 				warn("%s", filename);
100 				failed = 1;
101 			} else
102 				failed |= tabify(filename);
103 		}
104 	}
105 	exit(failed != 0);
106 }
107 
108 static void
109 usage(void)
110 {
111 	fprintf(stderr, "usage: unexpand [-a | -t tablist] [file ...]\n");
112 	exit(1);
113 }
114 
115 static int
116 tabify(const char *curfile)
117 {
118 	int dcol, doneline, limit, n, ocol, width;
119 	wint_t ch;
120 
121 	limit = nstops == 1 ? INT_MAX : tabstops[nstops - 1] - 1;
122 
123 	doneline = ocol = dcol = 0;
124 	while ((ch = getwchar()) != WEOF) {
125 		if (ch == ' ' && !doneline) {
126 			if (++dcol >= limit)
127 				doneline = 1;
128 			continue;
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 				putwchar('\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 				putwchar('\t');
161 				ocol = tabstops[n++];
162 			}
163 		}
164 
165 		/* Then spaces. */
166 		while (ocol < dcol && ocol < limit) {
167 			putwchar(' ');
168 			ocol++;
169 		}
170 
171 		if (ch == '\b') {
172 			putwchar('\b');
173 			if (ocol > 0)
174 				ocol--, dcol--;
175 		} else if (ch == '\n') {
176 			putwchar('\n');
177 			doneline = ocol = dcol = 0;
178 			continue;
179 		} else if (ch != ' ' || dcol > limit) {
180 			putwchar(ch);
181 			if ((width = wcwidth(ch)) > 0)
182 				ocol += width, dcol += width;
183 		}
184 
185 		/*
186 		 * Only processing leading blanks or we've gone past the
187 		 * last tab stop. Emit remainder of this line unchanged.
188 		 */
189 		if (!all || dcol >= limit) {
190 			while ((ch = getwchar()) != '\n' && ch != WEOF)
191 				putwchar(ch);
192 			if (ch == '\n')
193 				putwchar('\n');
194 			doneline = ocol = dcol = 0;
195 		}
196 	}
197 	if (ferror(stdin)) {
198 		warn("%s", curfile);
199 		return (1);
200 	}
201 	return (0);
202 }
203 
204 static void
205 getstops(const char *cp)
206 {
207 	int i;
208 
209 	nstops = 0;
210 	for (;;) {
211 		i = 0;
212 		while (*cp >= '0' && *cp <= '9')
213 			i = i * 10 + *cp++ - '0';
214 		if (i <= 0)
215 			errx(1, "bad tab stop spec");
216 		if (nstops > 0 && i <= tabstops[nstops-1])
217 			errx(1, "bad tab stop spec");
218 		if (nstops == sizeof(tabstops) / sizeof(*tabstops))
219 			errx(1, "too many tab stops");
220 		tabstops[nstops++] = i;
221 		if (*cp == 0)
222 			break;
223 		if (*cp != ',' && !isblank((unsigned char)*cp))
224 			errx(1, "bad tab stop spec");
225 		cp++;
226 	}
227 }
228