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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/* tr.c - translate or delete characters
*
* Copyright 2014 Sandeep Sharma <sandeep.jack2756@gmail.com>
*
* See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/tr.html
USE_TR(NEWTOY(tr, "^>2<1Ccsd[+cC]", TOYFLAG_USR|TOYFLAG_BIN))
config TR
bool "tr"
default n
help
usage: tr [-cds] SET1 [SET2]
Translate, squeeze, or delete characters from stdin, writing to stdout
-c/-C Take complement of SET1
-d Delete input characters coded SET1
-s Squeeze multiple output characters of SET2 into one character
*/
#define FOR_tr
#include "toys.h"
GLOBALS(
short map[256]; //map of chars
int len1, len2;
)
enum {
class_alpha, class_alnum, class_digit,
class_lower,class_upper,class_space,class_blank,
class_punct,class_cntrl,class_xdigit,class_invalid
};
static void map_translation(char *set1 , char *set2)
{
int i = TT.len1, k = 0;
if (toys.optflags & FLAG_d)
for (; i; i--, k++) TT.map[set1[k]] = set1[k]|0x100; //set delete bit
if (toys.optflags & FLAG_s) {
for (i = TT.len1, k = 0; i; i--, k++)
TT.map[set1[k]] = TT.map[set1[k]]|0x200;
for (i = TT.len2, k = 0; i; i--, k++)
TT.map[set2[k]] = TT.map[set2[k]]|0x200;
}
i = k = 0;
while (!(toys.optflags & FLAG_d) && set2 && TT.len1--) { //ignore set2 if -d present
TT.map[set1[i]] = ((TT.map[set1[i]] & 0xFF00) | set2[k]);
if (set2[k + 1]) k++;
i++;
}
}
static int handle_escape_char(char **esc_val) //taken from printf
{
char *ptr = *esc_val;
int esc_length = 0;
unsigned base = 0, num = 0, result = 0, count = 0;
if (*ptr == 'x') {
ptr++;
esc_length++;
base = 16;
} else if (isdigit(*ptr)) base = 8;
while (esc_length < 3 && base) {
num = tolower(*ptr) - '0';
if (num > 10) num += ('0' - 'a' + 10);
if (num >= base) {
if (base == 16) {
esc_length--;
if (!esc_length) {// Invalid hex value eg. /xvd, print as it is /xvd
result = '\\';
ptr--;
}
}
break;
}
esc_length++;
count = result = (count * base) + num;
ptr++;
}
if (base) {
ptr--;
*esc_val = ptr;
return (char)result;
} else {
switch (*ptr) {
case 'n': result = '\n'; break;
case 't': result = '\t'; break;
case 'e': result = (char)27; break;
case 'b': result = '\b'; break;
case 'a': result = '\a'; break;
case 'f': result = '\f'; break;
case 'v': result = '\v'; break;
case 'r': result = '\r'; break;
case '\\': result = '\\'; break;
default :
result = '\\';
ptr--; // Let pointer pointing to / we will increment after returning.
break;
}
}
*esc_val = ptr;
return (char)result;
}
static int find_class(char *class_name)
{
int i;
static char *class[] = {
"[:alpha:]","[:alnum:]","[:digit:]",
"[:lower:]","[:upper:]","[:space:]",
"[:blank:]","[:punct:]","[:cntrl:]",
"[:xdigit:]","NULL"
};
for (i = 0; i != class_invalid; i++) {
if (!memcmp(class_name, class[i], (class_name[0] == 'x')?10:9)) break;
}
return i;
}
static char *expand_set(char *arg, int *len)
{
int i = 0, j, k, size = 256;
char *set = xzalloc(size*sizeof(char));
while (*arg) {
if (i >= size) {
size += 256;
set = xrealloc(set, size);
}
if (*arg == '\\') {
arg++;
set[i++] = (int)handle_escape_char(&arg);
arg++;
continue;
}
if (arg[1] == '-') {
if (arg[2] == '\0') goto save;
j = arg[0];
k = arg[2];
if (j > k) perror_exit("reverse colating order");
while (j <= k) set[i++] = j++;
arg += 3;
continue;
}
if (arg[0] == '[' && arg[1] == ':') {
if ((j = find_class(arg)) == class_invalid) goto save;
if ((j == class_alpha) || (j == class_upper) || (j == class_alnum)) {
for (k = 'A'; k <= 'Z'; k++) set[i++] = k;
}
if ((j == class_alpha) || (j == class_lower) || (j == class_alnum)) {
for (k = 'a'; k <= 'z'; k++) set[i++] = k;
}
if ((j == class_alnum) || (j == class_digit) || (j == class_xdigit)) {
for (k = '0'; k <= '9'; k++) set[i++] = k;
}
if (j == class_space || j == class_blank) {
set[i++] = '\t';
if (j == class_space) {
set[i++] = '\n';
set[i++] = '\f';
set[i++] = '\r';
set[i++] = '\v';
}
set[i++] = ' ';
}
if (j == class_punct) {
for (k = 0; k <= 255; k++)
if (ispunct(k)) set[i++] = k;
}
if (j == class_cntrl) {
for (k = 0; k <= 255; k++)
if (iscntrl(k)) set[i++] = k;
}
if (j == class_xdigit) {
for (k = 'A'; k <= 'F'; k++) {
set[i + 6] = k | 0x20;
set[i++] = k;
}
i += 6;
arg += 10;
continue;
}
arg += 9; //never here for class_xdigit.
continue;
}
if (arg[0] == '[' && arg[1] == '=') { //[=char=] only
arg += 2;
if (*arg) set[i++] = *arg;
if (!arg[1] || arg[1] != '=' || arg[2] != ']')
error_exit("bad equiv class");
continue;
}
save:
set[i++] = *arg++;
}
*len = i;
return set;
}
static void print_map(char *set1, char *set2)
{
int n, src, dst, prev = -1;
while ((n = read(0, toybuf, sizeof(toybuf)))) {
if (!FLAG(d) && !FLAG(s)) {
for (dst = 0; dst < n; dst++) toybuf[dst] = TT.map[toybuf[dst]];
} else {
for (src = dst = 0; src < n; src++) {
int ch = TT.map[toybuf[src]];
if (FLAG(d) && (ch & 0x100)) continue;
if (FLAG(s) && ((ch & 0x200) && prev == ch)) continue;
toybuf[dst++] = prev = ch;
}
}
xwrite(1, toybuf, dst);
}
}
static void do_complement(char **set)
{
int i, j;
char *comp = xmalloc(256);
for (i = 0, j = 0;i < 256; i++) {
if (memchr(*set, i, TT.len1)) continue;
else comp[j++] = (char)i;
}
free(*set);
TT.len1 = j;
*set = comp;
}
void tr_main(void)
{
char *set1, *set2 = NULL;
int i;
for (i = 0; i < 256; i++) TT.map[i] = i; //init map
set1 = expand_set(toys.optargs[0], &TT.len1);
if (toys.optflags & FLAG_c) do_complement(&set1);
if (toys.optargs[1]) {
if (toys.optargs[1][0] == '\0') error_exit("set2 can't be empty string");
set2 = expand_set(toys.optargs[1], &TT.len2);
}
map_translation(set1, set2);
print_map(set1, set2);
free(set1);
free(set2);
}
|