Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/coreutils/tr.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 816 - (hide annotations) (download)
Fri Apr 24 18:33:46 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 6724 byte(s)
-updated to busybox-1.13.4
1 niro 532 /* vi: set sw=4 ts=4: */
2     /*
3     * Mini tr implementation for busybox
4     *
5     ** Copyright (c) 1987,1997, Prentice Hall All rights reserved.
6     *
7     * The name of Prentice Hall may not be used to endorse or promote
8     * products derived from this software without specific prior
9     * written permission.
10     *
11     * Copyright (c) Michiel Huisjes
12     *
13     * This version of tr is adapted from Minix tr and was modified
14     * by Erik Andersen <andersen@codepoet.org> to be used in busybox.
15     *
16     * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17     */
18 niro 816 /* http://www.opengroup.org/onlinepubs/009695399/utilities/tr.html
19     * TODO: xdigit, graph, print
20     */
21     #include "libbb.h"
22 niro 532
23     #define ASCII 0377
24    
25 niro 816 static void map(char *pvector,
26     unsigned char *string1, unsigned int string1_len,
27     unsigned char *string2, unsigned int string2_len)
28 niro 532 {
29     char last = '0';
30     unsigned int i, j;
31    
32     for (j = 0, i = 0; i < string1_len; i++) {
33     if (string2_len <= j)
34 niro 816 pvector[string1[i]] = last;
35 niro 532 else
36 niro 816 pvector[string1[i]] = last = string2[j++];
37 niro 532 }
38     }
39    
40     /* supported constructs:
41 niro 816 * Ranges, e.g., 0-9 ==> 0123456789
42     * Ranges, e.g., [0-9] ==> 0123456789
43     * Escapes, e.g., \a ==> Control-G
44     * Character classes, e.g. [:upper:] ==> A...Z
45     * Equiv classess, e.g. [=A=] ==> A (hmmmmmmm?)
46 niro 532 */
47     static unsigned int expand(const char *arg, char *buffer)
48     {
49     char *buffer_start = buffer;
50 niro 816 unsigned i; /* can't be unsigned char: must be able to hold 256 */
51     unsigned char ac;
52 niro 532
53     while (*arg) {
54     if (*arg == '\\') {
55     arg++;
56     *buffer++ = bb_process_escape_sequence(&arg);
57 niro 816 continue;
58     }
59     if (arg[1] == '-') { /* "0-9..." */
60     ac = arg[2];
61     if (ac == '\0') { /* "0-": copy verbatim */
62     *buffer++ = *arg++; /* copy '0' */
63     continue; /* next iter will copy '-' and stop */
64 niro 532 }
65     i = *arg;
66 niro 816 while (i <= ac) /* ok: i is unsigned _int_ */
67 niro 532 *buffer++ = i++;
68 niro 816 arg += 3; /* skip 0-9 */
69     continue;
70     }
71     if (*arg == '[') { /* "[xyz..." */
72 niro 532 arg++;
73     i = *arg++;
74 niro 816 /* "[xyz...", i=x, arg points to y */
75 niro 532 if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
76 niro 816 #define CLO ":]\0"
77     static const char classes[] ALIGN1 =
78     "alpha"CLO "alnum"CLO "digit"CLO
79     "lower"CLO "upper"CLO "space"CLO
80     "blank"CLO "punct"CLO "cntrl"CLO;
81     #define CLASS_invalid 0 /* we increment the retval */
82     #define CLASS_alpha 1
83     #define CLASS_alnum 2
84     #define CLASS_digit 3
85     #define CLASS_lower 4
86     #define CLASS_upper 5
87     #define CLASS_space 6
88     #define CLASS_blank 7
89     #define CLASS_punct 8
90     #define CLASS_cntrl 9
91     //#define CLASS_xdigit 10
92     //#define CLASS_graph 11
93     //#define CLASS_print 12
94     smalluint j;
95     { /* not really pretty.. */
96     char *tmp = xstrndup(arg, 7); // warning: xdigit would need 8, not 7
97     j = index_in_strings(classes, tmp) + 1;
98     free(tmp);
99 niro 532 }
100 niro 816 if (j == CLASS_alnum || j == CLASS_digit) {
101 niro 532 for (i = '0'; i <= '9'; i++)
102     *buffer++ = i;
103 niro 816 }
104     if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
105 niro 532 for (i = 'A'; i <= 'Z'; i++)
106     *buffer++ = i;
107     }
108 niro 816 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
109 niro 532 for (i = 'a'; i <= 'z'; i++)
110     *buffer++ = i;
111     }
112 niro 816 if (j == CLASS_space || j == CLASS_blank) {
113 niro 532 *buffer++ = '\t';
114 niro 816 if (j == CLASS_space) {
115     *buffer++ = '\n';
116     *buffer++ = '\v';
117     *buffer++ = '\f';
118     *buffer++ = '\r';
119     }
120 niro 532 *buffer++ = ' ';
121     }
122 niro 816 if (j == CLASS_punct || j == CLASS_cntrl) {
123     for (i = '\0'; i <= ASCII; i++)
124     if ((j == CLASS_punct && isprint(i) && !isalnum(i) && !isspace(i))
125     || (j == CLASS_cntrl && iscntrl(i)))
126 niro 532 *buffer++ = i;
127     }
128 niro 816 if (j == CLASS_invalid) {
129 niro 532 *buffer++ = '[';
130     *buffer++ = ':';
131     continue;
132     }
133     break;
134     }
135 niro 816 /* "[xyz...", i=x, arg points to y */
136     if (ENABLE_FEATURE_TR_EQUIV && i == '=') { /* [=CHAR=] */
137     *buffer++ = *arg; /* copy CHAR */
138     if (!*arg || arg[1] != '=' || arg[2] != ']')
139     bb_show_usage();
140     arg += 3; /* skip CHAR=] */
141 niro 532 continue;
142     }
143 niro 816 if (i == '\0' || *arg != '-') { /* not [x-...] - copy verbatim */
144 niro 532 *buffer++ = '[';
145 niro 816 arg--; /* points to x */
146     continue; /* copy all, including eventual ']' */
147 niro 532 }
148 niro 816 /* [x-z] */
149     arg++; /* skip - */
150     if (arg[0] == '\0' || arg[1] != ']')
151     bb_show_usage();
152 niro 532 ac = *arg++;
153     while (i <= ac)
154     *buffer++ = i++;
155 niro 816 arg++; /* skip ] */
156     continue;
157     }
158     *buffer++ = *arg++;
159 niro 532 }
160     return (buffer - buffer_start);
161     }
162    
163     static int complement(char *buffer, int buffer_len)
164     {
165 niro 816 int i, j, ix;
166 niro 532 char conv[ASCII + 2];
167    
168     ix = 0;
169 niro 816 for (i = '\0'; i <= ASCII; i++) {
170 niro 532 for (j = 0; j < buffer_len; j++)
171     if (buffer[j] == i)
172     break;
173     if (j == buffer_len)
174     conv[ix++] = i & ASCII;
175     }
176     memcpy(buffer, conv, ix);
177     return ix;
178     }
179    
180 niro 816 int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
181     int tr_main(int argc UNUSED_PARAM, char **argv)
182 niro 532 {
183 niro 816 int output_length = 0, input_length;
184 niro 532 int i;
185 niro 816 smalluint flags;
186     ssize_t read_chars = 0;
187     size_t in_index = 0, out_index = 0;
188     unsigned last = UCHAR_MAX + 1; /* not equal to any char */
189     unsigned char coded, c;
190     unsigned char *output = xmalloc(BUFSIZ);
191     char *vector = xzalloc((ASCII+1) * 3);
192     char *invec = vector + (ASCII+1);
193     char *outvec = vector + (ASCII+1) * 2;
194 niro 532
195 niro 816 #define TR_OPT_complement (1 << 0)
196     #define TR_OPT_delete (1 << 1)
197     #define TR_OPT_squeeze_reps (1 << 2)
198 niro 532
199 niro 816 flags = getopt32(argv, "+cds"); /* '+': stop at first non-option */
200     argv += optind;
201    
202 niro 532 for (i = 0; i <= ASCII; i++) {
203     vector[i] = i;
204 niro 816 /*invec[i] = outvec[i] = FALSE; - done by xzalloc */
205 niro 532 }
206    
207 niro 816 #define tr_buf bb_common_bufsiz1
208     if (*argv != NULL) {
209     input_length = expand(*argv++, tr_buf);
210     if (flags & TR_OPT_complement)
211     input_length = complement(tr_buf, input_length);
212     if (*argv) {
213     if (argv[0][0] == '\0')
214 niro 532 bb_error_msg_and_die("STRING2 cannot be empty");
215 niro 816 output_length = expand(*argv, (char *)output);
216     map(vector, (unsigned char *)tr_buf, input_length, output, output_length);
217 niro 532 }
218     for (i = 0; i < input_length; i++)
219 niro 816 invec[(unsigned char)tr_buf[i]] = TRUE;
220 niro 532 for (i = 0; i < output_length; i++)
221 niro 816 outvec[output[i]] = TRUE;
222 niro 532 }
223 niro 816
224     for (;;) {
225     /* If we're out of input, flush output and read more input. */
226     if ((ssize_t)in_index == read_chars) {
227     if (out_index) {
228     xwrite(STDOUT_FILENO, (char *)output, out_index);
229     out_index = 0;
230     }
231     read_chars = safe_read(STDIN_FILENO, tr_buf, BUFSIZ);
232     if (read_chars <= 0) {
233     if (read_chars < 0)
234     bb_perror_msg_and_die(bb_msg_read_error);
235     exit(EXIT_SUCCESS);
236     }
237     in_index = 0;
238     }
239     c = tr_buf[in_index++];
240     coded = vector[c];
241     if ((flags & TR_OPT_delete) && invec[c])
242     continue;
243     if ((flags & TR_OPT_squeeze_reps) && last == coded
244     && (invec[c] || outvec[coded]))
245     continue;
246     output[out_index++] = last = coded;
247     }
248     /* NOTREACHED */
249     return EXIT_SUCCESS;
250 niro 532 }