Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / unexpand / unexpand.c
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)unexpand.c       8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/unexpand/unexpand.c,v 1.5.2.3 2002/10/11 11:33:23 tjr Exp $
36  * $DragonFly: src/usr.bin/unexpand/unexpand.c,v 1.2 2003/06/17 04:29:33 dillon Exp $
37  */
38
39 /*
40  * unexpand - put tabs into a file replacing blanks
41  */
42 #include <ctype.h>
43 #include <err.h>
44 #include <limits.h>
45 #include <locale.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 int     all;
52 int     nstops;
53 int     tabstops[100];
54
55 static void getstops(const char *);
56 static void usage(void);
57 static void tabify(void);
58
59 int
60 main(argc, argv)
61         int argc;
62         char *argv[];
63 {
64         int ch, failed;
65         char *filename;
66
67         setlocale(LC_CTYPE, "");
68
69         nstops = 1;
70         tabstops[0] = 8;
71         while ((ch = getopt(argc, argv, "at:")) != -1) {
72                 switch (ch) {
73                 case 'a':       /* Un-expand all spaces, not just leading. */
74                         all = 1;
75                         break;
76                 case 't':       /* Specify tab list, implies -a. */
77                         getstops(optarg);
78                         all = 1;
79                         break;
80                 default:
81                         usage();
82                         /*NOTREACHED*/
83                 }
84         }
85         argc -= optind;
86         argv += optind;
87
88         failed = 0;
89         if (argc == 0)
90                 tabify();
91         else {
92                 while ((filename = *argv++) != NULL) {
93                         if (freopen(filename, "r", stdin) == NULL) {
94                                 warn("%s", filename);
95                                 failed++;
96                         } else
97                                 tabify();
98                 }
99         }
100         exit(failed != 0);
101 }
102
103 static void
104 usage()
105 {
106         fprintf(stderr, "usage: unexpand [-a] [-t tablist] [file ...]\n");
107         exit(1);
108 }
109
110 static void
111 tabify()
112 {
113         int ch, dcol, doneline, limit, n, ocol;
114
115         limit = nstops == 1 ? INT_MAX : tabstops[nstops - 1] - 1;
116
117         doneline = ocol = dcol = 0;
118         while ((ch = getchar()) != EOF) {
119                 if (ch == ' ' && !doneline) {
120                         if (++dcol >= limit)
121                                 doneline = 1;
122                         continue;
123                 } else if (ch == '\t') {
124                         if (nstops == 1) {
125                                 dcol = (1 + dcol / tabstops[0]) *
126                                     tabstops[0];
127                                 continue;
128                         } else {
129                                 for (n = 0; tabstops[n] - 1 < dcol &&
130                                     n < nstops; n++)
131                                         ;
132                                 if (n < nstops - 1 && tabstops[n] - 1 < limit) {
133                                         dcol = tabstops[n];
134                                         continue;
135                                 }
136                                 doneline = 1;
137                         }
138                 }
139
140                 /* Output maximal number of tabs. */
141                 if (nstops == 1) {
142                         while (((ocol + tabstops[0]) / tabstops[0])
143                             <= (dcol / tabstops[0])) {
144                                 if (dcol - ocol < 2)
145                                         break;
146                                 putchar('\t');
147                                 ocol = (1 + ocol / tabstops[0]) *
148                                     tabstops[0];
149                         }
150                 } else {
151                         for (n = 0; tabstops[n] - 1 < ocol && n < nstops; n++)
152                                 ;
153                         while (ocol < dcol && n < nstops && ocol < limit) {
154                                 putchar('\t');
155                                 ocol = tabstops[n++];
156                         }
157                 }
158
159                 /* Then spaces. */
160                 while (ocol < dcol && ocol < limit) {
161                         putchar(' ');
162                         ocol++;
163                 }
164
165                 if (ch == '\b') {
166                         putchar('\b');
167                         if (ocol > 0)
168                                 ocol--, dcol--;
169                 } else if (ch == '\n') {
170                         putchar('\n');
171                         doneline = ocol = dcol = 0;
172                         continue;
173                 } else if (ch != ' ' || dcol > limit) {
174                         putchar(ch);
175                         if (isprint(ch))
176                                 ocol++, dcol++;
177                 }
178
179                 /*
180                  * Only processing leading blanks or we've gone past the
181                  * last tab stop. Emit remainder of this line unchanged.
182                  */
183                 if (!all || dcol >= limit) {
184                         while ((ch = getchar()) != '\n' && ch != EOF)
185                                 putchar(ch);
186                         if (ch == '\n')
187                                 putchar('\n');
188                         doneline = ocol = dcol = 0;
189                 }
190         }
191 }
192
193 static void
194 getstops(cp)
195         const char *cp;
196 {
197         int i;
198
199         nstops = 0;
200         for (;;) {
201                 i = 0;
202                 while (*cp >= '0' && *cp <= '9')
203                         i = i * 10 + *cp++ - '0';
204                 if (i <= 0)
205                         errx(1, "bad tab stop spec");
206                 if (nstops > 0 && i <= tabstops[nstops-1])
207                         errx(1, "bad tab stop spec");
208                 if (nstops == sizeof(tabstops) / sizeof(*tabstops))
209                         errx(1, "too many tab stops");
210                 tabstops[nstops++] = i;
211                 if (*cp == 0)
212                         break;
213                 if (*cp != ',' && !isblank((unsigned char)*cp))
214                         errx(1, "bad tab stop spec");
215                 cp++;
216         }
217 }