386BSD 0.1 development
[unix-history] / usr / src / usr.bin / expand / expand.c
CommitLineData
a149d2af
WJ
1/*
2 * Copyright (c) 1980 The Regents of the University of California.
3 * 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
35char copyright[] =
36"@(#) Copyright (c) 1980 The Regents of the University of California.\n\
37 All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)expand.c 5.3 (Berkeley) 6/1/90";
42#endif /* not lint */
43
44#include <stdio.h>
45/*
46 * expand - expand tabs to equivalent spaces
47 */
48int nstops;
49int tabstops[100];
50
51main(argc, argv)
52 int argc;
53 char *argv[];
54{
55 register int c, column;
56 register int n;
57
58 argc--, argv++;
59 do {
60 while (argc > 0 && argv[0][0] == '-') {
61 getstops(argv[0]);
62 argc--, argv++;
63 }
64 if (argc > 0) {
65 if (freopen(argv[0], "r", stdin) == NULL) {
66 perror(argv[0]);
67 exit(1);
68 }
69 argc--, argv++;
70 }
71 column = 0;
72 for (;;) {
73 c = getc(stdin);
74 if (c == -1)
75 break;
76 switch (c) {
77
78 case '\t':
79 if (nstops == 0) {
80 do {
81 putchar(' ');
82 column++;
83 } while (column & 07);
84 continue;
85 }
86 if (nstops == 1) {
87 do {
88 putchar(' ');
89 column++;
90 } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1));
91 continue;
92 }
93 for (n = 0; n < nstops; n++)
94 if (tabstops[n] > column)
95 break;
96 if (n == nstops) {
97 putchar(' ');
98 column++;
99 continue;
100 }
101 while (column < tabstops[n]) {
102 putchar(' ');
103 column++;
104 }
105 continue;
106
107 case '\b':
108 if (column)
109 column--;
110 putchar('\b');
111 continue;
112
113 default:
114 putchar(c);
115 column++;
116 continue;
117
118 case '\n':
119 putchar(c);
120 column = 0;
121 continue;
122 }
123 }
124 } while (argc > 0);
125 exit(0);
126}
127
128getstops(cp)
129 register char *cp;
130{
131 register int i;
132
133 nstops = 0;
134 cp++;
135 for (;;) {
136 i = 0;
137 while (*cp >= '0' && *cp <= '9')
138 i = i * 10 + *cp++ - '0';
139 if (i <= 0 || i > 256) {
140bad:
141 fprintf(stderr, "Bad tab stop spec\n");
142 exit(1);
143 }
144 if (nstops > 0 && i <= tabstops[nstops-1])
145 goto bad;
146 tabstops[nstops++] = i;
147 if (*cp == 0)
148 break;
149 if (*cp++ != ',')
150 goto bad;
151 }
152}