Commit | Line | Data |
---|---|---|
22e155fc DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1980 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
13 | #ifndef lint | |
14 | static char sccsid[] = "@(#)expand.c 5.1 (Berkeley) %G%"; | |
15 | #endif not lint | |
16 | ||
fb2da2bb BJ |
17 | #include <stdio.h> |
18 | /* | |
19 | * expand - expand tabs to equivalent spaces | |
20 | */ | |
fb2da2bb BJ |
21 | int nstops; |
22 | int tabstops[100]; | |
23 | ||
24 | main(argc, argv) | |
25 | int argc; | |
26 | char *argv[]; | |
27 | { | |
28 | register int c, column; | |
29 | register int n; | |
30 | ||
fb2da2bb BJ |
31 | argc--, argv++; |
32 | do { | |
33 | while (argc > 0 && argv[0][0] == '-') { | |
34 | getstops(argv[0]); | |
35 | argc--, argv++; | |
36 | } | |
37 | if (argc > 0) { | |
38 | if (freopen(argv[0], "r", stdin) == NULL) { | |
39 | perror(argv[0]); | |
40 | exit(1); | |
41 | } | |
42 | argc--, argv++; | |
43 | } | |
44 | column = 0; | |
45 | for (;;) { | |
46 | c = getc(stdin); | |
47 | if (c == -1) | |
48 | break; | |
49 | switch (c) { | |
50 | ||
51 | case '\t': | |
52 | if (nstops == 0) { | |
53 | do { | |
54 | putchar(' '); | |
55 | column++; | |
56 | } while (column & 07); | |
57 | continue; | |
58 | } | |
59 | if (nstops == 1) { | |
60 | do { | |
61 | putchar(' '); | |
62 | column++; | |
63 | } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1)); | |
64 | continue; | |
65 | } | |
66 | for (n = 0; n < nstops; n++) | |
67 | if (tabstops[n] > column) | |
68 | break; | |
69 | if (n == nstops) { | |
70 | putchar(' '); | |
71 | column++; | |
72 | continue; | |
73 | } | |
74 | while (column < tabstops[n]) { | |
75 | putchar(' '); | |
76 | column++; | |
77 | } | |
78 | continue; | |
79 | ||
80 | case '\b': | |
81 | if (column) | |
82 | column--; | |
83 | putchar('\b'); | |
84 | continue; | |
85 | ||
86 | default: | |
87 | putchar(c); | |
88 | column++; | |
89 | continue; | |
90 | ||
91 | case '\n': | |
92 | putchar(c); | |
93 | column = 0; | |
94 | continue; | |
95 | } | |
96 | } | |
97 | } while (argc > 0); | |
98 | exit(0); | |
99 | } | |
100 | ||
101 | getstops(cp) | |
102 | register char *cp; | |
103 | { | |
104 | register int i; | |
105 | ||
106 | nstops = 0; | |
107 | cp++; | |
108 | for (;;) { | |
109 | i = 0; | |
110 | while (*cp >= '0' && *cp <= '9') | |
111 | i = i * 10 + *cp++ - '0'; | |
112 | if (i <= 0 || i > 256) { | |
113 | bad: | |
114 | fprintf(stderr, "Bad tab stop spec\n"); | |
115 | exit(1); | |
116 | } | |
73c2cdfe | 117 | if (nstops > 0 && i <= tabstops[nstops-1]) |
fb2da2bb BJ |
118 | goto bad; |
119 | tabstops[nstops++] = i; | |
120 | if (*cp == 0) | |
121 | break; | |
122 | if (*cp++ != ',') | |
123 | goto bad; | |
124 | } | |
125 | } |