Commit | Line | Data |
---|---|---|
4ca85ebb WJ |
1 | /* |
2 | * Copyright (c) 1988 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | char copyright[] = | |
36 | "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ | |
37 | All rights reserved.\n"; | |
38 | #endif /* not lint */ | |
39 | ||
40 | #ifndef lint | |
41 | static char sccsid[] = "@(#)tr.c 4.7 (Berkeley) 7/23/90"; | |
42 | #endif /* not lint */ | |
43 | ||
44 | #include <sys/types.h> | |
45 | #include <stdio.h> | |
46 | #include <ctype.h> | |
47 | ||
48 | #define NCHARS 256 /* size of u_char */ | |
49 | #define OOBCH 257 /* out of band value */ | |
50 | ||
51 | typedef struct { | |
52 | char *str; | |
53 | int lastch, endrange; | |
54 | enum { NORM, INRANGE, EOS } state; | |
55 | } STR; | |
56 | ||
57 | main(argc, argv) | |
58 | int argc; | |
59 | char **argv; | |
60 | { | |
61 | extern int optind; | |
62 | STR s1, s2; | |
63 | register int ch, indx, lastch; | |
64 | int cflag, dflag, sflag; | |
65 | u_char *tp, tab[NCHARS], squeeze[NCHARS]; | |
66 | ||
67 | cflag = dflag = sflag = 0; | |
68 | while ((ch = getopt(argc, argv, "cds")) != EOF) | |
69 | switch((char)ch) { | |
70 | case 'c': | |
71 | cflag = 1; | |
72 | break; | |
73 | case 'd': | |
74 | dflag = 1; | |
75 | break; | |
76 | case 's': | |
77 | sflag = 1; | |
78 | break; | |
79 | case '?': | |
80 | default: | |
81 | fprintf(stderr, | |
82 | "usage: tr [-cds] [string1 [string2]]\n"); | |
83 | exit(1); | |
84 | } | |
85 | argc -= optind; | |
86 | argv += optind; | |
87 | ||
88 | /* | |
89 | * the original tr was amazingly tolerant of the command line. | |
90 | * Neither -c or -s have any effect unless there are two strings. | |
91 | * Extra arguments are silently ignored. Bag this noise, they | |
92 | * should all be errors. | |
93 | */ | |
94 | if (argc < 2 && !dflag) { | |
95 | while ((ch = getchar()) != EOF) | |
96 | putchar(ch); | |
97 | exit(0); | |
98 | } | |
99 | ||
100 | bzero(tab, NCHARS); | |
101 | if (sflag) { | |
102 | s1.str = argv[1]; | |
103 | s1.state = NORM; | |
104 | s1.lastch = OOBCH; | |
105 | while (next(&s1)) | |
106 | squeeze[s1.lastch] = 1; | |
107 | } | |
108 | if (dflag) { | |
109 | s1.str = argv[0]; | |
110 | s1.state = NORM; | |
111 | s1.lastch = OOBCH; | |
112 | while (next(&s1)) | |
113 | tab[s1.lastch] = 1; | |
114 | if (cflag) | |
115 | for (tp = tab, indx = 0; indx < NCHARS; ++tp, ++indx) | |
116 | *tp = !*tp; | |
117 | if (sflag) | |
118 | for (lastch = OOBCH; (ch = getchar()) != EOF;) { | |
119 | if (tab[ch] || (squeeze[ch] && lastch == ch)) | |
120 | continue; | |
121 | lastch = ch; | |
122 | putchar(ch); | |
123 | } | |
124 | else | |
125 | while ((ch = getchar()) != EOF) | |
126 | if (!tab[ch]) | |
127 | putchar(ch); | |
128 | } else { | |
129 | s1.str = argv[0]; | |
130 | s2.str = argv[1]; | |
131 | s1.state = s2.state = NORM; | |
132 | s1.lastch = s2.lastch = OOBCH; | |
133 | if (cflag) { | |
134 | /* | |
135 | * if cflag is set, tr just pretends it only got one | |
136 | * character in string2. As reasonable as anything | |
137 | * else. Should really be an error. | |
138 | */ | |
139 | while (next(&s2)); | |
140 | lastch = s2.lastch; | |
141 | for (tp = tab, indx = 0; indx < NCHARS; ++tp, ++indx) | |
142 | *tp = lastch; | |
143 | while (next(&s1)) | |
144 | tab[s1.lastch] = s1.lastch; | |
145 | } else { | |
146 | for (tp = tab, indx = 0; indx < NCHARS; ++tp, ++indx) | |
147 | *tp = indx; | |
148 | while (next(&s1)) { | |
149 | (void)next(&s2); | |
150 | tab[s1.lastch] = s2.lastch; | |
151 | } | |
152 | } | |
153 | if (sflag) | |
154 | for (lastch = OOBCH; (ch = getchar()) != EOF;) { | |
155 | ch = tab[ch]; | |
156 | if (squeeze[ch] && lastch == ch) | |
157 | continue; | |
158 | lastch = ch; | |
159 | putchar(ch); | |
160 | } | |
161 | else | |
162 | while ((ch = getchar()) != EOF) | |
163 | putchar((int)tab[ch]); | |
164 | } | |
165 | exit(0); | |
166 | } | |
167 | ||
168 | next(s) | |
169 | register STR *s; | |
170 | { | |
171 | register int ch; | |
172 | ||
173 | if (s->state == EOS) | |
174 | return(0); | |
175 | if (s->state == INRANGE) { | |
176 | if (++s->lastch == s->endrange) | |
177 | s->state = NORM; | |
178 | return(1); | |
179 | } | |
180 | if (!(ch = *s->str++)) { | |
181 | s->state = EOS; | |
182 | return(0); | |
183 | } | |
184 | if (ch == '\\') { /* \### */ | |
185 | s->lastch = tran(s); | |
186 | return(1); | |
187 | } | |
188 | if (ch == '-') { /* ranges */ | |
189 | if (s->lastch == OOBCH) /* "-a" */ | |
190 | goto fail2; | |
191 | if (!(ch = *s->str++)) /* "a-" */ | |
192 | goto fail1; | |
193 | if (ch == '\\') /* \### */ | |
194 | ch = tran(s); | |
195 | if (s->lastch > ch) { /* "z-a" */ | |
196 | fail1: --s->str; | |
197 | fail2: s->lastch = '-'; | |
198 | return(1); | |
199 | } | |
200 | if (s->lastch == ch) /* "a-a" */ | |
201 | return(next(s)); | |
202 | s->state = INRANGE; /* "a-z" */ | |
203 | s->endrange = ch; | |
204 | return(1); | |
205 | } | |
206 | s->lastch = ch; | |
207 | return(1); | |
208 | } | |
209 | ||
210 | /* | |
211 | * Translate \-escapes. Up to 3 octal digits => char; no digits => literal. | |
212 | * Unadorned backslash "\" is like \000. | |
213 | */ | |
214 | tran(s) | |
215 | register STR *s; | |
216 | { | |
217 | register int ch, cnt = 0, val = 0; | |
218 | ||
219 | for (;;) { | |
220 | ch = *s->str++; | |
221 | if (!isascii(ch) || !isdigit(ch) || ++cnt > 3) | |
222 | break; | |
223 | val = val * 8 + ch - '0'; | |
224 | } | |
225 | if (cnt || ch == 0) | |
226 | s->str--; | |
227 | return (cnt ? val : ch); | |
228 | } |