unexpand.c revision 1.12 1 /* $NetBSD: unexpand.c,v 1.12 2003/08/07 11:16:54 agc Exp $ */
2
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 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)unexpand.c 8.1 (Berkeley) 6/6/93";
41 #endif
42 __RCSID("$NetBSD: unexpand.c,v 1.12 2003/08/07 11:16:54 agc Exp $");
43 #endif /* not lint */
44
45 /*
46 * unexpand - put tabs into a file replacing blanks
47 */
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <errno.h>
53 #include <err.h>
54
55 char genbuf[BUFSIZ];
56 char linebuf[BUFSIZ];
57
58 int main(int, char **);
59 void tabify(int, uint);
60
61 int
62 main(int argc, char **argv)
63 {
64 int all, c;
65 uint tabsize;
66 ulong l;
67 char *ep;
68
69 setprogname(argv[0]);
70
71 all = 0;
72 tabsize = 8;
73 while ((c = getopt(argc, argv, "at:")) != -1) {
74 switch (c) {
75 case 'a':
76 all++;
77 break;
78 case 't':
79 errno = 0;
80 l = strtoul(optarg, &ep, 0);
81 /*
82 * If every input char is a tab, the line length
83 * must not exceed maxuint.
84 */
85 tabsize = (int)l * BUFSIZ;
86 tabsize /= BUFSIZ;
87 if (*ep != 0 || errno != 0 || (ulong)tabsize != l)
88 errx(EXIT_FAILURE, "Invalid tabstop \"%s\"",
89 optarg);
90 break;
91 case '?':
92 default:
93 fprintf(stderr, "usage: %s [-a] [-t tabstop] [file ...]\n",
94 getprogname());
95 exit(EXIT_FAILURE);
96 /* NOTREACHED */
97 }
98 }
99 argc -= optind;
100 argv += optind;
101
102 do {
103 if (argc > 0) {
104 if (freopen(argv[0], "r", stdin) == NULL) {
105 perror(argv[0]);
106 exit(EXIT_FAILURE);
107 }
108 argc--, argv++;
109 }
110 while (fgets(genbuf, BUFSIZ, stdin) != NULL) {
111 tabify(all, tabsize);
112 printf("%s", linebuf);
113 }
114 } while (argc > 0);
115 exit(EXIT_SUCCESS);
116 /* NOTREACHED */
117 }
118
119 void
120 tabify(int all, uint tabsize)
121 {
122 char *cp, *dp;
123 uint dcol;
124 uint ocol;
125 uint tcol;
126
127 ocol = 0;
128 dcol = 0;
129 cp = genbuf, dp = linebuf;
130 for (;;) {
131 switch (*cp) {
132
133 case ' ':
134 dcol++;
135 break;
136
137 case '\t':
138 dcol = (dcol + tabsize) / tabsize * tabsize;
139 break;
140
141 default:
142 if (dcol > ocol + 1) {
143 tcol = (ocol + tabsize) / tabsize * tabsize;
144 while (tcol <= dcol) {
145 *dp++ = '\t';
146 ocol = tcol;
147 tcol += tabsize;
148 }
149 }
150 while (ocol < dcol) {
151 *dp++ = ' ';
152 ocol++;
153 }
154 if (*cp == 0 || all == 0) {
155 strlcpy(dp, cp,
156 sizeof(linebuf) - (dp - linebuf));
157 return;
158 }
159 *dp++ = *cp;
160 dcol = ++ocol;
161 }
162 cp++;
163 }
164 }
165