1 /* $NetBSD: unexpand.c,v 1.14 2008/12/21 02:33:13 christos Exp $ */
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)unexpand.c 8.1 (Berkeley) 6/6/93";
42 __RCSID("$NetBSD: unexpand.c,v 1.14 2008/12/21 02:33:13 christos Exp $");
46 * unexpand - put tabs into a file replacing blanks
61 static size_t maxstops
;
62 static size_t *tabstops
;
64 static void tabify(const char *, size_t);
65 static void usage(void) __attribute__((__noreturn__
));
70 (void)fprintf(stderr
, "Usage: %s [-a] [-t tabstop] [file ...]\n",
76 main(int argc
, char **argv
)
86 while ((c
= getopt(argc
, argv
, "at:")) != -1) {
96 while ((tab
= strsep(&optarg
, ", \t")) != NULL
) {
100 i
= strtoul(tab
, &ep
, 0);
101 if (*ep
|| (errno
== ERANGE
&& i
== ULONG_MAX
))
103 "Invalid tabstop `%s'", tab
);
104 if (nstops
>= maxstops
) {
106 tabstops
= erealloc(tabstops
, maxstops
);
108 if (nstops
&& tabstops
[nstops
- 1] >= (size_t)i
)
110 "Bad tabstop spec `%s', must be "
111 "greater than the previous `%zu'",
112 tab
, tabstops
[nstops
- 1]);
113 tabstops
[nstops
++] = i
;
124 for (i
= 0; i
< nstops
; i
++)
125 fprintf(stderr
, "%lu %zu\n", i
, tabstops
[i
]);
129 if (freopen(argv
[0], "r", stdin
) == NULL
)
130 err(EXIT_FAILURE
, "Cannot open `%s'", argv
[0]);
133 while ((line
= fgetln(stdin
, &len
)) != NULL
)
140 tabify(const char *line
, size_t len
)
143 size_t dcol
, ocol
, limit
, n
;
146 limit
= nstops
== 0 ? UINT_MAX
: tabstops
[nstops
- 1] - 1;
148 for (p
= line
; p
< e
; p
++) {
152 } else if (*p
== '\t') {
154 dcol
= (1 + dcol
/ DSTOP
) * DSTOP
;
157 for (n
= 0; tabstops
[n
] - 1 < dcol
&&
160 if (n
< nstops
- 1 && tabstops
[n
] - 1 < limit
) {
167 /* Output our tabs */
169 while (((ocol
+ DSTOP
) / DSTOP
) <= (dcol
/ DSTOP
)) {
172 if (putchar('\t') == EOF
)
174 ocol
= (1 + ocol
/ DSTOP
) * DSTOP
;
177 for (n
= 0; tabstops
[n
] <= ocol
&& n
< nstops
; n
++)
179 while (tabstops
[n
] <= dcol
&& ocol
< dcol
&&
180 n
< nstops
&& ocol
< limit
) {
181 if (putchar('\t') == EOF
)
183 ocol
= tabstops
[n
++];
187 /* Output remaining spaces */
188 while (ocol
< dcol
&& ocol
< limit
) {
189 if (putchar(' ') == EOF
)
194 /* Output our char */
195 if (putchar(*p
) == EOF
)
207 /* Output remainder of line */
208 if (!all
|| dcol
>= limit
) {
209 for (p
++; p
< e
; p
++)
210 if (putchar(*p
) == EOF
)
217 err(EXIT_FAILURE
, "write failed");