|
|
1.1 root 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 are permitted
6: * provided that: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #ifndef lint
21: char copyright[] =
22: "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
23: All rights reserved.\n";
24: #endif /* not lint */
25:
26: #ifndef lint
27: static char sccsid[] = "@(#)expand.c 5.3 (Berkeley) 6/1/90";
28: #endif /* not lint */
29:
30: #include <stdio.h>
31: /*
32: * expand - expand tabs to equivalent spaces
33: */
34: int nstops;
35: int tabstops[100];
36:
37: main(argc, argv)
38: int argc;
39: char *argv[];
40: {
41: register int c, column;
42: register int n;
43:
44: argc--, argv++;
45: do {
46: while (argc > 0 && argv[0][0] == '-') {
47: getstops(argv[0]);
48: argc--, argv++;
49: }
50: if (argc > 0) {
51: if (freopen(argv[0], "r", stdin) == NULL) {
52: perror(argv[0]);
53: exit(1);
54: }
55: argc--, argv++;
56: }
57: column = 0;
58: for (;;) {
59: c = getc(stdin);
60: if (c == -1)
61: break;
62: switch (c) {
63:
64: case '\t':
65: if (nstops == 0) {
66: do {
67: putchar(' ');
68: column++;
69: } while (column & 07);
70: continue;
71: }
72: if (nstops == 1) {
73: do {
74: putchar(' ');
75: column++;
76: } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1));
77: continue;
78: }
79: for (n = 0; n < nstops; n++)
80: if (tabstops[n] > column)
81: break;
82: if (n == nstops) {
83: putchar(' ');
84: column++;
85: continue;
86: }
87: while (column < tabstops[n]) {
88: putchar(' ');
89: column++;
90: }
91: continue;
92:
93: case '\b':
94: if (column)
95: column--;
96: putchar('\b');
97: continue;
98:
99: default:
100: putchar(c);
101: column++;
102: continue;
103:
104: case '\n':
105: putchar(c);
106: column = 0;
107: continue;
108: }
109: }
110: } while (argc > 0);
111: exit(0);
112: }
113:
114: getstops(cp)
115: register char *cp;
116: {
117: register int i;
118:
119: nstops = 0;
120: cp++;
121: for (;;) {
122: i = 0;
123: while (*cp >= '0' && *cp <= '9')
124: i = i * 10 + *cp++ - '0';
125: if (i <= 0 || i > 256) {
126: bad:
127: fprintf(stderr, "Bad tab stop spec\n");
128: exit(1);
129: }
130: if (nstops > 0 && i <= tabstops[nstops-1])
131: goto bad;
132: tabstops[nstops++] = i;
133: if (*cp == 0)
134: break;
135: if (*cp++ != ',')
136: goto bad;
137: }
138: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.