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