Annotation of src/usr.bin/expand/expand.c, Revision 1.5
1.5 ! millert 1: /* $OpenBSD: expand.c,v 1.4 2001/11/19 19:02:13 mpech Exp $ */
1.1 deraadt 2: /* $NetBSD: expand.c,v 1.5 1995/09/02 06:19:46 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1980, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.5 ! millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
34: static char copyright[] =
35: "@(#) Copyright (c) 1980, 1993\n\
36: The Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)expand.c 8.1 (Berkeley) 6/9/93";
42: #endif
1.5 ! millert 43: static char rcsid[] = "$OpenBSD: expand.c,v 1.4 2001/11/19 19:02:13 mpech Exp $";
1.1 deraadt 44: #endif /* not lint */
45:
46: #include <stdio.h>
47: #include <stdlib.h>
48: #include <ctype.h>
49: #include <unistd.h>
50:
51: /*
52: * expand - expand tabs to equivalent spaces
53: */
54: int nstops;
55: int tabstops[100];
56:
57: static void getstops();
58: static void usage();
59:
60: int
61: main(argc, argv)
62: int argc;
63: char *argv[];
64: {
1.4 mpech 65: int c, column;
66: int n;
1.1 deraadt 67:
68: /* handle obsolete syntax */
1.3 deraadt 69: while (argc > 1 && argv[1][0] == '-' && isdigit(argv[1][1])) {
1.1 deraadt 70: getstops(&argv[1][1]);
71: argc--; argv++;
72: }
73:
74: while ((c = getopt (argc, argv, "t:")) != -1) {
75: switch (c) {
76: case 't':
77: getstops(optarg);
78: break;
79: case '?':
80: default:
81: usage();
82: /* NOTREACHED */
83: }
84: }
85: argc -= optind;
86: argv += optind;
87:
88: do {
89: if (argc > 0) {
90: if (freopen(argv[0], "r", stdin) == NULL) {
91: perror(argv[0]);
92: exit(1);
93: }
94: argc--, argv++;
95: }
96: column = 0;
97: while ((c = getchar()) != EOF) {
98: switch (c) {
99: case '\t':
100: if (nstops == 0) {
101: do {
102: putchar(' ');
103: column++;
104: } while (column & 07);
105: continue;
106: }
107: if (nstops == 1) {
108: do {
109: putchar(' ');
110: column++;
111: } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1));
112: continue;
113: }
114: for (n = 0; n < nstops; n++)
115: if (tabstops[n] > column)
116: break;
117: if (n == nstops) {
118: putchar(' ');
119: column++;
120: continue;
121: }
122: while (column < tabstops[n]) {
123: putchar(' ');
124: column++;
125: }
126: continue;
127:
128: case '\b':
129: if (column)
130: column--;
131: putchar('\b');
132: continue;
133:
134: default:
135: putchar(c);
136: column++;
137: continue;
138:
139: case '\n':
140: putchar(c);
141: column = 0;
142: continue;
143: }
144: }
145: } while (argc > 0);
146: exit(0);
147: }
148:
149: static void
150: getstops(cp)
1.4 mpech 151: char *cp;
1.1 deraadt 152: {
1.4 mpech 153: int i;
1.1 deraadt 154:
155: nstops = 0;
156: for (;;) {
157: i = 0;
158: while (*cp >= '0' && *cp <= '9')
159: i = i * 10 + *cp++ - '0';
160: if (i <= 0 || i > 256) {
161: bad:
162: fprintf(stderr, "Bad tab stop spec\n");
163: exit(1);
164: }
165: if (nstops > 0 && i <= tabstops[nstops-1])
166: goto bad;
167: tabstops[nstops++] = i;
168: if (*cp == 0)
169: break;
170: if (*cp != ',' && *cp != ' ')
171: goto bad;
172: cp++;
173: }
174: }
175:
176: static void
177: usage()
178: {
179: (void)fprintf (stderr, "usage: expand [-t tablist] [file ...]\n");
180: exit(1);
181: }