unexpand.c revision 1.18 1 1.18 dyoung /* $NetBSD: unexpand.c,v 1.18 2019/09/13 17:32:29 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.18 dyoung __RCSID("$NetBSD: unexpand.c,v 1.18 2019/09/13 17:32:29 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.18 dyoung (void)fprintf(stderr, "Usage: %s [-a] [-t tabstop] [file ...]\n",
71 1.18 dyoung getprogname());
72 1.18 dyoung 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.18 dyoung const size_t dstop = (nstops == 0) ? DSTOP : tabstops[0];
136 1.14 christos const char *e, *p;
137 1.14 christos size_t dcol, ocol, limit, n;
138 1.1 cgd
139 1.14 christos dcol = ocol = 0;
140 1.17 dyoung limit = nstops <= 1 ? UINT_MAX : tabstops[nstops - 1] - 1;
141 1.14 christos e = line + len;
142 1.14 christos for (p = line; p < e; p++) {
143 1.14 christos if (*p == ' ') {
144 1.1 cgd dcol++;
145 1.14 christos continue;
146 1.14 christos } else if (*p == '\t') {
147 1.16 dyoung if (nstops <= 1) {
148 1.18 dyoung dcol = (1 + dcol / dstop) * dstop;
149 1.18 dyoung continue;
150 1.18 dyoung }
151 1.18 dyoung for (n = 0; n < nstops && tabstops[n] <= dcol; n++)
152 1.18 dyoung continue;
153 1.18 dyoung if (n < nstops - 1 && tabstops[n] - 1 < limit) {
154 1.18 dyoung dcol = tabstops[n];
155 1.14 christos continue;
156 1.14 christos }
157 1.14 christos }
158 1.1 cgd
159 1.14 christos /* Output our tabs */
160 1.16 dyoung if (nstops <= 1) {
161 1.18 dyoung while (((ocol + dstop) / dstop) <= (dcol / dstop)) {
162 1.14 christos if (dcol - ocol < 2)
163 1.14 christos break;
164 1.14 christos if (putchar('\t') == EOF)
165 1.14 christos goto out;
166 1.18 dyoung ocol = (1 + ocol / dstop) * dstop;
167 1.1 cgd }
168 1.14 christos } else {
169 1.15 christos for (n = 0; n < nstops && tabstops[n] <= ocol; n++)
170 1.14 christos continue;
171 1.15 christos while (n < nstops && tabstops[n] <= dcol && ocol < dcol
172 1.15 christos && ocol < limit) {
173 1.14 christos if (putchar('\t') == EOF)
174 1.14 christos goto out;
175 1.14 christos ocol = tabstops[n++];
176 1.1 cgd }
177 1.14 christos }
178 1.14 christos
179 1.14 christos /* Output remaining spaces */
180 1.14 christos while (ocol < dcol && ocol < limit) {
181 1.14 christos if (putchar(' ') == EOF)
182 1.14 christos goto out;
183 1.14 christos ocol++;
184 1.14 christos }
185 1.14 christos
186 1.14 christos /* Output our char */
187 1.14 christos if (putchar(*p) == EOF)
188 1.14 christos goto out;
189 1.14 christos if (*p == '\b') {
190 1.14 christos if (ocol > 0) {
191 1.14 christos ocol--;
192 1.14 christos dcol--;
193 1.1 cgd }
194 1.14 christos } else {
195 1.14 christos ocol++;
196 1.14 christos dcol++;
197 1.14 christos }
198 1.14 christos
199 1.18 dyoung if (all && dcol < limit)
200 1.18 dyoung continue; /* Collapse the rest of the line. */
201 1.18 dyoung
202 1.14 christos /* Output remainder of line */
203 1.18 dyoung for (p++; p < e; p++) {
204 1.18 dyoung if (putchar(*p) == EOF)
205 1.18 dyoung goto out;
206 1.1 cgd }
207 1.18 dyoung return;
208 1.1 cgd }
209 1.14 christos return;
210 1.14 christos out:
211 1.14 christos err(EXIT_FAILURE, "write failed");
212 1.1 cgd }
213