Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / expand / expand.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
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)expand.c    8.1 (Berkeley) 6/9/93";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD: src/usr.bin/expand/expand.c,v 1.5.2.6 2002/07/09 10:47:59 tjr Exp $");
47
48 #include <ctype.h>
49 #include <err.h>
50 #include <locale.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54
55 /*
56  * expand - expand tabs to equivalent spaces
57  */
58 int     nstops;
59 int     tabstops[100];
60
61 static void getstops(char *);
62 static void usage(void);
63
64 int
65 main(argc, argv)
66         int argc;
67         char *argv[];
68 {
69         register int c, column;
70         register int n;
71         int rval;
72
73         setlocale(LC_CTYPE, "");
74
75         /* handle obsolete syntax */
76         while (argc > 1 && argv[1][0] == '-' &&
77             isdigit((unsigned char)argv[1][1])) {
78                 getstops(&argv[1][1]);
79                 argc--; argv++;
80         }
81
82         while ((c = getopt (argc, argv, "t:")) != -1) {
83                 switch (c) {
84                 case 't':
85                         getstops(optarg);
86                         break;
87                 case '?':
88                 default:
89                         usage();
90                         /* NOTREACHED */
91                 }
92         }
93         argc -= optind;
94         argv += optind;
95
96         rval = 0;
97         do {
98                 if (argc > 0) {
99                         if (freopen(argv[0], "r", stdin) == NULL) {
100                                 warn("%s", argv[0]);
101                                 rval = 1;
102                                 argc--, argv++;
103                                 continue;
104                         }
105                         argc--, argv++;
106                 }
107                 column = 0;
108                 while ((c = getchar()) != EOF) {
109                         switch (c) {
110                         case '\t':
111                                 if (nstops == 0) {
112                                         do {
113                                                 putchar(' ');
114                                                 column++;
115                                         } while (column & 07);
116                                         continue;
117                                 }
118                                 if (nstops == 1) {
119                                         do {
120                                                 putchar(' ');
121                                                 column++;
122                                         } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1));
123                                         continue;
124                                 }
125                                 for (n = 0; n < nstops; n++)
126                                         if (tabstops[n] > column)
127                                                 break;
128                                 if (n == nstops) {
129                                         putchar(' ');
130                                         column++;
131                                         continue;
132                                 }
133                                 while (column < tabstops[n]) {
134                                         putchar(' ');
135                                         column++;
136                                 }
137                                 continue;
138
139                         case '\b':
140                                 if (column)
141                                         column--;
142                                 putchar('\b');
143                                 continue;
144
145                         default:
146                                 putchar(c);
147                                 if (isprint(c))
148                                         column++;
149                                 continue;
150
151                         case '\n':
152                                 putchar(c);
153                                 column = 0;
154                                 continue;
155                         }
156                 }
157         } while (argc > 0);
158         exit(rval);
159 }
160
161 static void
162 getstops(cp)
163         register char *cp;
164 {
165         register int i;
166
167         nstops = 0;
168         for (;;) {
169                 i = 0;
170                 while (*cp >= '0' && *cp <= '9')
171                         i = i * 10 + *cp++ - '0';
172                 if (i <= 0)
173                         errx(1, "bad tab stop spec");
174                 if (nstops > 0 && i <= tabstops[nstops-1])
175                         errx(1, "bad tab stop spec");
176                 if (nstops == sizeof(tabstops) / sizeof(*tabstops))
177                         errx(1, "too many tab stops");
178                 tabstops[nstops++] = i;
179                 if (*cp == 0)
180                         break;
181                 if (*cp != ',' && !isblank((unsigned char)*cp))
182                         errx(1, "bad tab stop spec");
183                 cp++;
184         }
185 }
186
187 static void
188 usage()
189 {
190         (void)fprintf (stderr, "usage: expand [-t tablist] [file ...]\n");
191         exit(1);
192 }