unexpand.c revision 1.17 1 1.17 dyoung /* $NetBSD: unexpand.c,v 1.17 2019/09/13 17:26:27 dyoung Exp $ */
2 1.3 jtc
3 1.1 cgd /*-
4 1.3 jtc * Copyright (c) 1980, 1993
5 1.3 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.12 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.6 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.13 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 1.13 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.3 jtc #if 0
40 1.3 jtc static char sccsid[] = "@(#)unexpand.c 8.1 (Berkeley) 6/6/93";
41 1.3 jtc #endif
42 1.17 dyoung __RCSID("$NetBSD: unexpand.c,v 1.17 2019/09/13 17:26:27 dyoung Exp $");
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd /*
46 1.1 cgd * unexpand - put tabs into a file replacing blanks
47 1.1 cgd */
48 1.14 christos #include <limits.h>
49 1.1 cgd #include <stdio.h>
50 1.7 kleink #include <stdlib.h>
51 1.5 cgd #include <string.h>
52 1.7 kleink #include <unistd.h>
53 1.10 dsl #include <errno.h>
54 1.10 dsl #include <err.h>
55 1.14 christos #include <util.h>
56 1.1 cgd
57 1.1 cgd
58 1.14 christos #define DSTOP 8
59 1.14 christos static int all;
60 1.14 christos static size_t nstops;
61 1.14 christos static size_t maxstops;
62 1.14 christos static size_t *tabstops;
63 1.14 christos
64 1.14 christos static void tabify(const char *, size_t);
65 1.14 christos static void usage(void) __attribute__((__noreturn__));
66 1.14 christos
67 1.14 christos static void
68 1.14 christos usage(void)
69 1.14 christos {
70 1.14 christos (void)fprintf(stderr, "Usage: %s [-a] [-t tabstop] [file ...]\n",
71 1.14 christos getprogname());
72 1.14 christos exit(EXIT_FAILURE);
73 1.14 christos }
74 1.4 jtc
75 1.4 jtc int
76 1.9 dsl main(int argc, char **argv)
77 1.1 cgd {
78 1.14 christos int c;
79 1.14 christos char *ep, *tab;
80 1.14 christos char *line;
81 1.14 christos size_t len;
82 1.14 christos unsigned long i;
83 1.10 dsl
84 1.10 dsl setprogname(argv[0]);
85 1.1 cgd
86 1.10 dsl while ((c = getopt(argc, argv, "at:")) != -1) {
87 1.7 kleink switch (c) {
88 1.7 kleink case 'a':
89 1.16 dyoung all = 1;
90 1.7 kleink break;
91 1.10 dsl case 't':
92 1.14 christos while ((tab = strsep(&optarg, ", \t")) != NULL) {
93 1.14 christos if (*tab == '\0')
94 1.14 christos continue;
95 1.14 christos errno = 0;
96 1.14 christos i = strtoul(tab, &ep, 0);
97 1.14 christos if (*ep || (errno == ERANGE && i == ULONG_MAX))
98 1.14 christos errx(EXIT_FAILURE,
99 1.14 christos "Invalid tabstop `%s'", tab);
100 1.14 christos if (nstops >= maxstops) {
101 1.14 christos maxstops += 20;
102 1.14 christos tabstops = erealloc(tabstops, maxstops);
103 1.14 christos }
104 1.14 christos if (nstops && tabstops[nstops - 1] >= (size_t)i)
105 1.14 christos errx(EXIT_FAILURE,
106 1.14 christos "Bad tabstop spec `%s', must be "
107 1.14 christos "greater than the previous `%zu'",
108 1.14 christos tab, tabstops[nstops - 1]);
109 1.14 christos tabstops[nstops++] = i;
110 1.14 christos }
111 1.10 dsl break;
112 1.7 kleink case '?':
113 1.7 kleink default:
114 1.14 christos usage();
115 1.1 cgd }
116 1.1 cgd }
117 1.7 kleink argc -= optind;
118 1.7 kleink argv += optind;
119 1.7 kleink
120 1.1 cgd do {
121 1.1 cgd if (argc > 0) {
122 1.14 christos if (freopen(argv[0], "r", stdin) == NULL)
123 1.14 christos err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
124 1.1 cgd argc--, argv++;
125 1.1 cgd }
126 1.14 christos while ((line = fgetln(stdin, &len)) != NULL)
127 1.14 christos tabify(line, len);
128 1.1 cgd } while (argc > 0);
129 1.14 christos return EXIT_SUCCESS;
130 1.1 cgd }
131 1.1 cgd
132 1.14 christos static void
133 1.14 christos tabify(const char *line, size_t len)
134 1.1 cgd {
135 1.14 christos const char *e, *p;
136 1.14 christos size_t dcol, ocol, limit, n;
137 1.1 cgd
138 1.14 christos dcol = ocol = 0;
139 1.17 dyoung limit = nstops <= 1 ? UINT_MAX : tabstops[nstops - 1] - 1;
140 1.14 christos e = line + len;
141 1.14 christos for (p = line; p < e; p++) {
142 1.14 christos if (*p == ' ') {
143 1.1 cgd dcol++;
144 1.14 christos continue;
145 1.14 christos } else if (*p == '\t') {
146 1.16 dyoung if (nstops <= 1) {
147 1.16 dyoung size_t stop = (nstops == 0)
148 1.16 dyoung ? DSTOP
149 1.16 dyoung : tabstops[0];
150 1.16 dyoung dcol = (1 + dcol / stop) * stop;
151 1.14 christos continue;
152 1.14 christos } else {
153 1.15 christos for (n = 0; n < nstops &&
154 1.15 christos tabstops[n] - 1 < dcol; n++)
155 1.14 christos continue;
156 1.14 christos if (n < nstops - 1 && tabstops[n] - 1 < limit) {
157 1.14 christos dcol = tabstops[n];
158 1.14 christos continue;
159 1.14 christos }
160 1.14 christos }
161 1.14 christos }
162 1.1 cgd
163 1.14 christos /* Output our tabs */
164 1.16 dyoung if (nstops <= 1) {
165 1.16 dyoung size_t stop = (nstops == 0)
166 1.16 dyoung ? DSTOP
167 1.16 dyoung : tabstops[0];
168 1.16 dyoung while (((ocol + stop) / stop) <= (dcol / stop)) {
169 1.14 christos if (dcol - ocol < 2)
170 1.14 christos break;
171 1.14 christos if (putchar('\t') == EOF)
172 1.14 christos goto out;
173 1.16 dyoung ocol = (1 + ocol / stop) * stop;
174 1.1 cgd }
175 1.14 christos } else {
176 1.15 christos for (n = 0; n < nstops && tabstops[n] <= ocol; n++)
177 1.14 christos continue;
178 1.15 christos while (n < nstops && tabstops[n] <= dcol && ocol < dcol
179 1.15 christos && ocol < limit) {
180 1.14 christos if (putchar('\t') == EOF)
181 1.14 christos goto out;
182 1.14 christos ocol = tabstops[n++];
183 1.1 cgd }
184 1.14 christos }
185 1.14 christos
186 1.14 christos /* Output remaining spaces */
187 1.14 christos while (ocol < dcol && ocol < limit) {
188 1.14 christos if (putchar(' ') == EOF)
189 1.14 christos goto out;
190 1.14 christos ocol++;
191 1.14 christos }
192 1.14 christos
193 1.14 christos /* Output our char */
194 1.14 christos if (putchar(*p) == EOF)
195 1.14 christos goto out;
196 1.14 christos if (*p == '\b') {
197 1.14 christos if (ocol > 0) {
198 1.14 christos ocol--;
199 1.14 christos dcol--;
200 1.1 cgd }
201 1.14 christos } else {
202 1.14 christos ocol++;
203 1.14 christos dcol++;
204 1.14 christos }
205 1.14 christos
206 1.14 christos /* Output remainder of line */
207 1.14 christos if (!all || dcol >= limit) {
208 1.14 christos for (p++; p < e; p++)
209 1.14 christos if (putchar(*p) == EOF)
210 1.14 christos goto out;
211 1.14 christos return;
212 1.1 cgd }
213 1.1 cgd }
214 1.14 christos return;
215 1.14 christos out:
216 1.14 christos err(EXIT_FAILURE, "write failed");
217 1.1 cgd }
218