1: #ifndef lint
2: static char *sccsid = "@(#)expand.c 4.2 (Berkeley) 2/7/81";
3: #endif
4: #include <stdio.h>
5: /*
6: * expand - expand tabs to equivalent spaces
7: */
8: char obuf[BUFSIZ];
9: int nstops;
10: int tabstops[100];
11:
12: main(argc, argv)
13: int argc;
14: char *argv[];
15: {
16: register int c, column;
17: register int n;
18:
19: setbuf(stdout, obuf);
20: argc--, argv++;
21: do {
22: while (argc > 0 && argv[0][0] == '-') {
23: getstops(argv[0]);
24: argc--, argv++;
25: }
26: if (argc > 0) {
27: if (freopen(argv[0], "r", stdin) == NULL) {
28: perror(argv[0]);
29: exit(1);
30: }
31: argc--, argv++;
32: }
33: column = 0;
34: for (;;) {
35: c = getc(stdin);
36: if (c == -1)
37: break;
38: switch (c) {
39:
40: case '\t':
41: if (nstops == 0) {
42: do {
43: putchar(' ');
44: column++;
45: } while (column & 07);
46: continue;
47: }
48: if (nstops == 1) {
49: do {
50: putchar(' ');
51: column++;
52: } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1));
53: continue;
54: }
55: for (n = 0; n < nstops; n++)
56: if (tabstops[n] > column)
57: break;
58: if (n == nstops) {
59: putchar(' ');
60: column++;
61: continue;
62: }
63: while (column < tabstops[n]) {
64: putchar(' ');
65: column++;
66: }
67: continue;
68:
69: case '\b':
70: if (column)
71: column--;
72: putchar('\b');
73: continue;
74:
75: default:
76: putchar(c);
77: column++;
78: continue;
79:
80: case '\n':
81: putchar(c);
82: column = 0;
83: continue;
84: }
85: }
86: } while (argc > 0);
87: exit(0);
88: }
89:
90: getstops(cp)
91: register char *cp;
92: {
93: register int i;
94:
95: nstops = 0;
96: cp++;
97: for (;;) {
98: i = 0;
99: while (*cp >= '0' && *cp <= '9')
100: i = i * 10 + *cp++ - '0';
101: if (i <= 0 || i > 256) {
102: bad:
103: fprintf(stderr, "Bad tab stop spec\n");
104: exit(1);
105: }
106: if (nstops > 0 && i <= tabstops[nstops-1])
107: goto bad;
108: tabstops[nstops++] = i;
109: if (*cp == 0)
110: break;
111: if (*cp++ != ',')
112: goto bad;
113: }
114: }
Defined functions
main
defined in line
12;
never used
Defined variables
nstops
defined in line
9; used 8 times
obuf
defined in line
8; used 1 times
sccsid
defined in line
2;
never used