Annotation of src/usr.bin/expand/expand.c, Revision 1.10
1.10 ! moritz 1: /* $OpenBSD: expand.c,v 1.9 2005/03/08 23:30:50 cloder 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
1.8 mickey 34: static const char copyright[] =
1.1 deraadt 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
1.8 mickey 41: static const char sccsid[] = "@(#)expand.c 8.1 (Berkeley) 6/9/93";
1.1 deraadt 42: #endif
1.10 ! moritz 43: static const char rcsid[] = "$OpenBSD: expand.c,v 1.9 2005/03/08 23:30:50 cloder 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>
1.8 mickey 50: #include <err.h>
1.1 deraadt 51:
52: /*
53: * expand - expand tabs to equivalent spaces
54: */
55: int nstops;
56: int tabstops[100];
57:
1.7 deraadt 58: static void getstops(char *);
59: static void usage(void);
1.1 deraadt 60:
61: int
1.6 deraadt 62: main(int argc, char *argv[])
1.1 deraadt 63: {
1.4 mpech 64: int c, column;
65: int n;
1.1 deraadt 66:
67: /* handle obsolete syntax */
1.3 deraadt 68: while (argc > 1 && argv[1][0] == '-' && isdigit(argv[1][1])) {
1.1 deraadt 69: getstops(&argv[1][1]);
70: argc--; argv++;
71: }
72:
73: while ((c = getopt (argc, argv, "t:")) != -1) {
74: switch (c) {
75: case 't':
76: getstops(optarg);
77: break;
78: case '?':
79: default:
80: usage();
81: /* NOTREACHED */
82: }
83: }
84: argc -= optind;
85: argv += optind;
86:
87: do {
88: if (argc > 0) {
1.8 mickey 89: if (freopen(argv[0], "r", stdin) == NULL)
1.9 cloder 90: err(1, "%s", argv[0]);
1.1 deraadt 91: argc--, argv++;
92: }
93: column = 0;
94: while ((c = getchar()) != EOF) {
95: switch (c) {
96: case '\t':
97: if (nstops == 0) {
98: do {
99: putchar(' ');
100: column++;
101: } while (column & 07);
102: continue;
103: }
104: if (nstops == 1) {
105: do {
106: putchar(' ');
107: column++;
1.6 deraadt 108: } while (((column - 1) %
109: tabstops[0]) != (tabstops[0] - 1));
1.1 deraadt 110: continue;
111: }
112: for (n = 0; n < nstops; n++)
113: if (tabstops[n] > column)
114: break;
115: if (n == nstops) {
116: putchar(' ');
117: column++;
118: continue;
119: }
120: while (column < tabstops[n]) {
121: putchar(' ');
122: column++;
123: }
124: continue;
125:
126: case '\b':
127: if (column)
128: column--;
129: putchar('\b');
130: continue;
131:
132: default:
133: putchar(c);
134: column++;
135: continue;
136:
137: case '\n':
138: putchar(c);
139: column = 0;
140: continue;
141: }
142: }
143: } while (argc > 0);
144: exit(0);
145: }
146:
147: static void
1.6 deraadt 148: getstops(char *cp)
1.1 deraadt 149: {
1.4 mpech 150: int i;
1.1 deraadt 151:
152: nstops = 0;
153: for (;;) {
154: i = 0;
155: while (*cp >= '0' && *cp <= '9')
156: i = i * 10 + *cp++ - '0';
157: if (i <= 0 || i > 256) {
158: bad:
1.8 mickey 159: errx(1, "Bad tab stop spec");
1.1 deraadt 160: }
161: if (nstops > 0 && i <= tabstops[nstops-1])
162: goto bad;
1.10 ! moritz 163: if (nstops >= sizeof(tabstops) / sizeof(tabstops[0]))
! 164: errx(1, "Too many tab stops");
1.1 deraadt 165: tabstops[nstops++] = i;
166: if (*cp == 0)
167: break;
168: if (*cp != ',' && *cp != ' ')
169: goto bad;
170: cp++;
171: }
172: }
173:
174: static void
1.6 deraadt 175: usage(void)
1.1 deraadt 176: {
1.8 mickey 177: extern char *__progname;
178: fprintf (stderr, "usage: %s [-t tablist] [file ...]\n", __progname);
1.1 deraadt 179: exit(1);
180: }