1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62#include "libbb.h"
63
64enum {
65 ASCII = 256,
66
67
68
69
70 TR_BUFSIZ = (BUFSIZ > ASCII*2) ? BUFSIZ : ASCII*2,
71};
72
73static void map(char *pvector,
74 char *string1, unsigned string1_len,
75 char *string2, unsigned string2_len)
76{
77 char last = '0';
78 unsigned i, j;
79
80 for (j = 0, i = 0; i < string1_len; i++) {
81 if (string2_len <= j)
82 pvector[(unsigned char)(string1[i])] = last;
83 else
84 pvector[(unsigned char)(string1[i])] = last = string2[j++];
85 }
86}
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102static unsigned expand(const char *arg, char **buffer_p)
103{
104 char *buffer = *buffer_p;
105 unsigned pos = 0;
106 unsigned size = TR_BUFSIZ;
107 unsigned i;
108 unsigned char ac;
109
110 while (*arg) {
111 if (pos + ASCII > size) {
112 size += ASCII;
113 *buffer_p = buffer = xrealloc(buffer, size);
114 }
115 if (*arg == '\\') {
116 arg++;
117 buffer[pos++] = bb_process_escape_sequence(&arg);
118 continue;
119 }
120 if (arg[1] == '-') {
121 ac = arg[2];
122 if (ac == '\0') {
123 buffer[pos++] = *arg++;
124 continue;
125 }
126 i = (unsigned char) *arg;
127 while (i <= ac)
128 buffer[pos++] = i++;
129 arg += 3;
130 continue;
131 }
132 if ((ENABLE_FEATURE_TR_CLASSES || ENABLE_FEATURE_TR_EQUIV)
133 && *arg == '['
134 ) {
135 arg++;
136 i = (unsigned char) *arg++;
137
138 if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
139#define CLO ":]\0"
140 static const char classes[] ALIGN1 =
141 "alpha"CLO "alnum"CLO "digit"CLO
142 "lower"CLO "upper"CLO "space"CLO
143 "blank"CLO "punct"CLO "cntrl"CLO
144 "xdigit"CLO;
145 enum {
146 CLASS_invalid = 0,
147 CLASS_alpha = 1,
148 CLASS_alnum = 2,
149 CLASS_digit = 3,
150 CLASS_lower = 4,
151 CLASS_upper = 5,
152 CLASS_space = 6,
153 CLASS_blank = 7,
154 CLASS_punct = 8,
155 CLASS_cntrl = 9,
156 CLASS_xdigit = 10,
157
158
159 };
160 smalluint j;
161 char *tmp;
162
163
164 i = 7 + (arg[0] == 'x');
165 tmp = xstrndup(arg, i);
166 j = index_in_strings(classes, tmp) + 1;
167 free(tmp);
168
169 if (j == CLASS_invalid)
170 goto skip_bracket;
171
172 arg += i;
173 if (j == CLASS_alnum || j == CLASS_digit || j == CLASS_xdigit) {
174 for (i = '0'; i <= '9'; i++)
175 buffer[pos++] = i;
176 }
177 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
178 for (i = 'A'; i <= 'Z'; i++)
179 buffer[pos++] = i;
180 }
181 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
182 for (i = 'a'; i <= 'z'; i++)
183 buffer[pos++] = i;
184 }
185 if (j == CLASS_space || j == CLASS_blank) {
186 buffer[pos++] = '\t';
187 if (j == CLASS_space) {
188 buffer[pos++] = '\n';
189 buffer[pos++] = '\v';
190 buffer[pos++] = '\f';
191 buffer[pos++] = '\r';
192 }
193 buffer[pos++] = ' ';
194 }
195 if (j == CLASS_punct || j == CLASS_cntrl) {
196 for (i = '\0'; i < ASCII; i++) {
197 if ((j == CLASS_punct && isprint_asciionly(i) && !isalnum(i) && !isspace(i))
198 || (j == CLASS_cntrl && iscntrl(i))
199 ) {
200 buffer[pos++] = i;
201 }
202 }
203 }
204 if (j == CLASS_xdigit) {
205 for (i = 'A'; i <= 'F'; i++) {
206 buffer[pos + 6] = i | 0x20;
207 buffer[pos++] = i;
208 }
209 pos += 6;
210 }
211 continue;
212 }
213
214 if (ENABLE_FEATURE_TR_EQUIV && i == '=') {
215 buffer[pos++] = *arg;
216 if (!arg[0] || arg[1] != '=' || arg[2] != ']')
217 bb_show_usage();
218 arg += 3;
219 continue;
220 }
221
222
223
224
225
226
227 skip_bracket:
228 arg -= 2;
229 }
230 buffer[pos++] = *arg++;
231 }
232 return pos;
233}
234
235
236
237
238static int complement(char *buffer, int buffer_len)
239{
240 int len;
241 char conv[ASCII];
242 unsigned char ch;
243
244 len = 0;
245 ch = '\0';
246 while (1) {
247 if (memchr(buffer, ch, buffer_len) == NULL)
248 conv[len++] = ch;
249 if (++ch == '\0')
250 break;
251 }
252 memcpy(buffer, conv, len);
253 return len;
254}
255
256int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
257int tr_main(int argc UNUSED_PARAM, char **argv)
258{
259 int i;
260 smalluint opts;
261 ssize_t read_chars;
262 size_t in_index, out_index;
263 unsigned last = UCHAR_MAX + 1;
264 unsigned char coded, c;
265 char *str1 = xmalloc(TR_BUFSIZ);
266 char *str2 = xmalloc(TR_BUFSIZ);
267 int str2_length;
268 int str1_length;
269 char *vector = xzalloc(ASCII * 3);
270 char *invec = vector + ASCII;
271 char *outvec = vector + ASCII * 2;
272
273#define TR_OPT_complement (3 << 0)
274#define TR_OPT_delete (1 << 2)
275#define TR_OPT_squeeze_reps (1 << 3)
276
277 for (i = 0; i < ASCII; i++) {
278 vector[i] = i;
279
280 }
281
282
283
284
285
286
287 opt_complementary = "-1";
288 opts = getopt32(argv, "+Ccds");
289 argv += optind;
290
291 str1_length = expand(*argv++, &str1);
292 str2_length = 0;
293 if (opts & TR_OPT_complement)
294 str1_length = complement(str1, str1_length);
295 if (*argv) {
296 if (argv[0][0] == '\0')
297 bb_error_msg_and_die("STRING2 cannot be empty");
298 str2_length = expand(*argv, &str2);
299 map(vector, str1, str1_length,
300 str2, str2_length);
301 }
302 for (i = 0; i < str1_length; i++)
303 invec[(unsigned char)(str1[i])] = TRUE;
304 for (i = 0; i < str2_length; i++)
305 outvec[(unsigned char)(str2[i])] = TRUE;
306
307 goto start_from;
308
309
310
311 for (;;) {
312
313 if ((ssize_t)in_index == read_chars) {
314 if (out_index) {
315 xwrite(STDOUT_FILENO, str2, out_index);
316 start_from:
317 out_index = 0;
318 }
319 read_chars = safe_read(STDIN_FILENO, str1, TR_BUFSIZ);
320 if (read_chars <= 0) {
321 if (read_chars < 0)
322 bb_perror_msg_and_die(bb_msg_read_error);
323 break;
324 }
325 in_index = 0;
326 }
327 c = str1[in_index++];
328 if ((opts & TR_OPT_delete) && invec[c])
329 continue;
330 coded = vector[c];
331 if ((opts & TR_OPT_squeeze_reps) && last == coded
332 && (invec[c] || outvec[coded])
333 ) {
334 continue;
335 }
336 str2[out_index++] = last = coded;
337 }
338
339 if (ENABLE_FEATURE_CLEAN_UP) {
340 free(vector);
341 free(str2);
342 free(str1);
343 }
344
345 return EXIT_SUCCESS;
346}
347