Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)expand.c 8.1 (Berkeley) 6/9/93
31  * $FreeBSD: src/usr.bin/expand/expand.c,v 1.5.2.6 2002/07/09 10:47:59 tjr Exp $
32  * $DragonFly: src/usr.bin/expand/expand.c,v 1.4 2005/04/10 20:55:38 drhodus Exp $
33  */
34
35 #include <ctype.h>
36 #include <err.h>
37 #include <locale.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 /*
43  * expand - expand tabs to equivalent spaces
44  */
45 int     nstops;
46 int     tabstops[100];
47
48 static void getstops(char *);
49 static void usage(void);
50
51 int
52 main(int argc, char **argv)
53 {
54         int c, column;
55         int n;
56         int rval;
57
58         setlocale(LC_CTYPE, "");
59
60         /* handle obsolete syntax */
61         while (argc > 1 && argv[1][0] == '-' &&
62             isdigit((unsigned char)argv[1][1])) {
63                 getstops(&argv[1][1]);
64                 argc--; argv++;
65         }
66
67         while ((c = getopt (argc, argv, "t:")) != -1) {
68                 switch (c) {
69                 case 't':
70                         getstops(optarg);
71                         break;
72                 case '?':
73                 default:
74                         usage();
75                         /* NOTREACHED */
76                 }
77         }
78         argc -= optind;
79         argv += optind;
80
81         rval = 0;
82         do {
83                 if (argc > 0) {
84                         if (freopen(argv[0], "r", stdin) == NULL) {
85                                 warn("%s", argv[0]);
86                                 rval = 1;
87                                 argc--, argv++;
88                                 continue;
89                         }
90                         argc--, argv++;
91                 }
92                 column = 0;
93                 while ((c = getchar()) != EOF) {
94                         switch (c) {
95                         case '\t':
96                                 if (nstops == 0) {
97                                         do {
98                                                 putchar(' ');
99                                                 column++;
100                                         } while (column & 07);
101                                         continue;
102                                 }
103                                 if (nstops == 1) {
104                                         do {
105                                                 putchar(' ');
106                                                 column++;
107                                         } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1));
108                                         continue;
109                                 }
110                                 for (n = 0; n < nstops; n++)
111                                         if (tabstops[n] > column)
112                                                 break;
113                                 if (n == nstops) {
114                                         putchar(' ');
115                                         column++;
116                                         continue;
117                                 }
118                                 while (column < tabstops[n]) {
119                                         putchar(' ');
120                                         column++;
121                                 }
122                                 continue;
123
124                         case '\b':
125                                 if (column)
126                                         column--;
127                                 putchar('\b');
128                                 continue;
129
130                         default:
131                                 putchar(c);
132                                 if (isprint(c))
133                                         column++;
134                                 continue;
135
136                         case '\n':
137                                 putchar(c);
138                                 column = 0;
139                                 continue;
140                         }
141                 }
142         } while (argc > 0);
143         exit(rval);
144 }
145
146 static void
147 getstops(char *cp)
148 {
149         int i;
150
151         nstops = 0;
152         for (;;) {
153                 i = 0;
154                 while (*cp >= '0' && *cp <= '9')
155                         i = i * 10 + *cp++ - '0';
156                 if (i <= 0)
157                         errx(1, "bad tab stop spec");
158                 if (nstops > 0 && i <= tabstops[nstops-1])
159                         errx(1, "bad tab stop spec");
160                 if (nstops == sizeof(tabstops) / sizeof(*tabstops))
161                         errx(1, "too many tab stops");
162                 tabstops[nstops++] = i;
163                 if (*cp == 0)
164                         break;
165                 if (*cp != ',' && !isblank((unsigned char)*cp))
166                         errx(1, "bad tab stop spec");
167                 cp++;
168         }
169 }
170
171 static void
172 usage(void)
173 {
174         (void)fprintf (stderr, "usage: expand [-t tablist] [file ...]\n");
175         exit(1);
176 }