blob: d89b80bec104cf069a86dbb10a94f4822a39fb9f [file] [log] [blame]
Erik Andersenfb002d02000-03-05 08:07:00 +00001/* vi: set sw=4 ts=4: */
2/*
Erik Andersen8f8d6d52000-05-01 22:30:37 +00003 * Mini tr implementation for busybox
Erik Andersenfb002d02000-03-05 08:07:00 +00004 *
Rob Landleycd545282006-06-30 16:35:40 +00005 ** 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 *
Erik Andersen5afc8642000-05-02 00:07:56 +000011 * Copyright (c) Michiel Huisjes
12 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000013 * This version of tr is adapted from Minix tr and was modified
Eric Andersencb81e642003-07-14 21:21:08 +000014 * by Erik Andersen <andersen@codepoet.org> to be used in busybox.
Erik Andersenfb002d02000-03-05 08:07:00 +000015 *
Rob Landleycd545282006-06-30 16:35:40 +000016 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersenfb002d02000-03-05 08:07:00 +000017 */
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000018/* http://www.opengroup.org/onlinepubs/009695399/utilities/tr.html
Denis Vlasenko491d6e32009-03-02 12:05:57 +000019 * TODO: graph, print
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000020 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000021#include "libbb.h"
Erik Andersen330fd2b2000-05-19 05:35:19 +000022
Denis Vlasenko491d6e32009-03-02 12:05:57 +000023enum {
24 ASCII = 256,
25 /* string buffer needs to be at least as big as the whole "alphabet".
26 * BUFSIZ == ASCII is ok, but we will realloc in expand
27 * even for smallest patterns, let's avoid that by using *2:
28 */
29 TR_BUFSIZ = (BUFSIZ > ASCII*2) ? BUFSIZ : ASCII*2,
30};
Erik Andersenfb002d02000-03-05 08:07:00 +000031
Denis Vlasenko74324c82007-06-04 10:16:52 +000032static void map(char *pvector,
Denis Vlasenko491d6e32009-03-02 12:05:57 +000033 char *string1, unsigned string1_len,
34 char *string2, unsigned string2_len)
Erik Andersen8f8d6d52000-05-01 22:30:37 +000035{
Rob Landleyab58d5c2006-06-30 19:04:09 +000036 char last = '0';
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +000037 unsigned i, j;
Erik Andersen8f8d6d52000-05-01 22:30:37 +000038
Eric Andersen00143ba2000-07-13 16:40:41 +000039 for (j = 0, i = 0; i < string1_len; i++) {
40 if (string2_len <= j)
Denis Vlasenko491d6e32009-03-02 12:05:57 +000041 pvector[(unsigned char)(string1[i])] = last;
Erik Andersen8f8d6d52000-05-01 22:30:37 +000042 else
Denis Vlasenko491d6e32009-03-02 12:05:57 +000043 pvector[(unsigned char)(string1[i])] = last = string2[j++];
Erik Andersen8f8d6d52000-05-01 22:30:37 +000044 }
45}
46
Mark Whitley8b7a0d82001-05-24 21:31:09 +000047/* supported constructs:
Denis Vlasenko3d461672007-11-13 22:22:29 +000048 * Ranges, e.g., 0-9 ==> 0123456789
Denis Vlasenko3d461672007-11-13 22:22:29 +000049 * Escapes, e.g., \a ==> Control-G
50 * Character classes, e.g. [:upper:] ==> A...Z
51 * Equiv classess, e.g. [=A=] ==> A (hmmmmmmm?)
Denis Vlasenko9210a362009-03-15 16:41:55 +000052 * not supported:
53 * \ooo-\ooo - octal ranges
54 * [x*N] - repeat char x N times
55 * [x*] - repeat char x until it fills STRING2:
56 * # echo qwe123 | /usr/bin/tr 123456789 '[d]'
57 * qwe[d]
58 * # echo qwe123 | /usr/bin/tr 123456789 '[d*]'
59 * qweddd
Mark Whitley8b7a0d82001-05-24 21:31:09 +000060 */
Denis Vlasenko491d6e32009-03-02 12:05:57 +000061static unsigned expand(const char *arg, char **buffer_p)
Erik Andersen8f8d6d52000-05-01 22:30:37 +000062{
Denis Vlasenko491d6e32009-03-02 12:05:57 +000063 char *buffer = *buffer_p;
64 unsigned pos = 0;
65 unsigned size = TR_BUFSIZ;
Denis Vlasenko3d461672007-11-13 22:22:29 +000066 unsigned i; /* can't be unsigned char: must be able to hold 256 */
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000067 unsigned char ac;
Denis Vlasenko3d461672007-11-13 22:22:29 +000068
69 while (*arg) {
Denis Vlasenko491d6e32009-03-02 12:05:57 +000070 if (pos + ASCII > size) {
71 size += ASCII;
72 *buffer_p = buffer = xrealloc(buffer, size);
73 }
Denis Vlasenko3d461672007-11-13 22:22:29 +000074 if (*arg == '\\') {
75 arg++;
Denis Vlasenko491d6e32009-03-02 12:05:57 +000076 buffer[pos++] = bb_process_escape_sequence(&arg);
Denis Vlasenko3d461672007-11-13 22:22:29 +000077 continue;
78 }
79 if (arg[1] == '-') { /* "0-9..." */
80 ac = arg[2];
81 if (ac == '\0') { /* "0-": copy verbatim */
Denis Vlasenko491d6e32009-03-02 12:05:57 +000082 buffer[pos++] = *arg++; /* copy '0' */
Denis Vlasenko3d461672007-11-13 22:22:29 +000083 continue; /* next iter will copy '-' and stop */
84 }
Denis Vlasenko491d6e32009-03-02 12:05:57 +000085 i = (unsigned char) *arg;
Denis Vlasenko3d461672007-11-13 22:22:29 +000086 while (i <= ac) /* ok: i is unsigned _int_ */
Denis Vlasenko491d6e32009-03-02 12:05:57 +000087 buffer[pos++] = i++;
Denis Vlasenko3d461672007-11-13 22:22:29 +000088 arg += 3; /* skip 0-9 */
89 continue;
90 }
Denis Vlasenko491d6e32009-03-02 12:05:57 +000091 if ((ENABLE_FEATURE_TR_CLASSES || ENABLE_FEATURE_TR_EQUIV)
92 && *arg == '['
93 ) {
Denis Vlasenko3d461672007-11-13 22:22:29 +000094 arg++;
Denis Vlasenko491d6e32009-03-02 12:05:57 +000095 i = (unsigned char) *arg++;
96 /* "[xyz...". i=x, arg points to y */
97 if (ENABLE_FEATURE_TR_CLASSES && i == ':') { /* [:class:] */
Denis Vlasenko990d0f62007-07-24 15:54:42 +000098#define CLO ":]\0"
Denis Vlasenko3d461672007-11-13 22:22:29 +000099 static const char classes[] ALIGN1 =
100 "alpha"CLO "alnum"CLO "digit"CLO
101 "lower"CLO "upper"CLO "space"CLO
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000102 "blank"CLO "punct"CLO "cntrl"CLO
103 "xdigit"CLO;
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000104 enum {
105 CLASS_invalid = 0, /* we increment the retval */
106 CLASS_alpha = 1,
107 CLASS_alnum = 2,
108 CLASS_digit = 3,
109 CLASS_lower = 4,
110 CLASS_upper = 5,
111 CLASS_space = 6,
112 CLASS_blank = 7,
113 CLASS_punct = 8,
114 CLASS_cntrl = 9,
115 CLASS_xdigit = 10,
116 //CLASS_graph = 11,
117 //CLASS_print = 12,
118 };
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000119 smalluint j;
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000120 char *tmp;
121
122 /* xdigit needs 8, not 7 */
123 i = 7 + (arg[0] == 'x');
124 tmp = xstrndup(arg, i);
125 j = index_in_strings(classes, tmp) + 1;
126 free(tmp);
127
128 if (j == CLASS_invalid)
129 goto skip_bracket;
130
131 arg += i;
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000132 if (j == CLASS_alnum || j == CLASS_digit || j == CLASS_xdigit) {
Rob Landley998dbee2006-04-19 22:22:06 +0000133 for (i = '0'; i <= '9'; i++)
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000134 buffer[pos++] = i;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000135 }
136 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
Rob Landleyf1048142005-10-08 21:21:08 +0000137 for (i = 'A'; i <= 'Z'; i++)
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000138 buffer[pos++] = i;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000139 }
140 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
Rob Landleyf1048142005-10-08 21:21:08 +0000141 for (i = 'a'; i <= 'z'; i++)
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000142 buffer[pos++] = i;
Rob Landleyf1048142005-10-08 21:21:08 +0000143 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000144 if (j == CLASS_space || j == CLASS_blank) {
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000145 buffer[pos++] = '\t';
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000146 if (j == CLASS_space) {
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000147 buffer[pos++] = '\n';
148 buffer[pos++] = '\v';
149 buffer[pos++] = '\f';
150 buffer[pos++] = '\r';
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000151 }
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000152 buffer[pos++] = ' ';
Rob Landley998dbee2006-04-19 22:22:06 +0000153 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000154 if (j == CLASS_punct || j == CLASS_cntrl) {
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000155 for (i = '\0'; i < ASCII; i++) {
Denis Vlasenko3d461672007-11-13 22:22:29 +0000156 if ((j == CLASS_punct && isprint(i) && !isalnum(i) && !isspace(i))
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000157 || (j == CLASS_cntrl && iscntrl(i))
158 ) {
159 buffer[pos++] = i;
160 }
161 }
Rob Landleyf1048142005-10-08 21:21:08 +0000162 }
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000163 if (j == CLASS_xdigit) {
164 for (i = 'A'; i <= 'F'; i++) {
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000165 buffer[pos + 6] = i | 0x20;
166 buffer[pos++] = i;
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000167 }
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000168 pos += 6;
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000169 }
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000170 continue;
Rob Landleyf1048142005-10-08 21:21:08 +0000171 }
Denis Vlasenko3d461672007-11-13 22:22:29 +0000172 /* "[xyz...", i=x, arg points to y */
173 if (ENABLE_FEATURE_TR_EQUIV && i == '=') { /* [=CHAR=] */
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000174 buffer[pos++] = *arg; /* copy CHAR */
175 if (!arg[0] || arg[1] != '=' || arg[2] != ']')
Denis Vlasenko043d15d2008-07-28 23:49:42 +0000176 bb_show_usage();
Denis Vlasenko3d461672007-11-13 22:22:29 +0000177 arg += 3; /* skip CHAR=] */
Rob Landleyf1048142005-10-08 21:21:08 +0000178 continue;
179 }
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000180 /* The rest of "[xyz..." cases is treated as normal
181 * string, "[" has no special meaning here:
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000182 * tr "[a-z]" "[A-Z]" can be written as tr "a-z" "A-Z",
183 * also try tr "[a-z]" "_A-Z+" and you'll see that
184 * [] is not special here.
185 */
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000186 skip_bracket:
187 arg -= 2; /* points to "[" in "[xyz..." */
Denis Vlasenko3d461672007-11-13 22:22:29 +0000188 }
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000189 buffer[pos++] = *arg++;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000190 }
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000191 return pos;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000192}
193
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000194/* NB: buffer is guaranteed to be at least TR_BUFSIZE
195 * (which is >= ASCII) big.
196 */
Rob Landleyab58d5c2006-06-30 19:04:09 +0000197static int complement(char *buffer, int buffer_len)
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000198{
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000199 int len;
200 char conv[ASCII];
201 unsigned char ch;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000202
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000203 len = 0;
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000204 ch = '\0';
205 while (1) {
206 if (memchr(buffer, ch, buffer_len) == NULL)
207 conv[len++] = ch;
208 if (++ch == '\0')
209 break;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000210 }
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000211 memcpy(buffer, conv, len);
212 return len;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000213}
214
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000215int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000216int tr_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000217{
Eric Andersenfad04fd2000-07-14 06:49:52 +0000218 int i;
Denis Vlasenko9210a362009-03-15 16:41:55 +0000219 smalluint opts;
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000220 ssize_t read_chars;
221 size_t in_index, out_index;
Denis Vlasenko3d461672007-11-13 22:22:29 +0000222 unsigned last = UCHAR_MAX + 1; /* not equal to any char */
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000223 unsigned char coded, c;
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000224 char *str1 = xmalloc(TR_BUFSIZ);
225 char *str2 = xmalloc(TR_BUFSIZ);
226 int str2_length;
227 int str1_length;
228 char *vector = xzalloc(ASCII * 3);
229 char *invec = vector + ASCII;
230 char *outvec = vector + ASCII * 2;
Mark Whitley59ab0252001-01-23 22:30:04 +0000231
Denis Vlasenko9210a362009-03-15 16:41:55 +0000232#define TR_OPT_complement (3 << 0)
233#define TR_OPT_delete (1 << 2)
234#define TR_OPT_squeeze_reps (1 << 3)
Denis Vlasenko8d0a7342007-11-13 22:23:57 +0000235
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000236 for (i = 0; i < ASCII; i++) {
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000237 vector[i] = i;
Denis Vlasenko8d0a7342007-11-13 22:23:57 +0000238 /*invec[i] = outvec[i] = FALSE; - done by xzalloc */
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000239 }
240
Denis Vlasenko9210a362009-03-15 16:41:55 +0000241 /* -C/-c difference is that -C complements "characters",
242 * and -c complements "values" (binary bytes I guess).
243 * In POSIX locale, these are the same.
244 */
245
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000246 opt_complementary = "-1";
Denis Vlasenko9210a362009-03-15 16:41:55 +0000247 opts = getopt32(argv, "+Ccds"); /* '+': stop at first non-option */
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000248 argv += optind;
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000249
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000250 str1_length = expand(*argv++, &str1);
251 str2_length = 0;
Denis Vlasenko9210a362009-03-15 16:41:55 +0000252 if (opts & TR_OPT_complement)
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000253 str1_length = complement(str1, str1_length);
254 if (*argv) {
255 if (argv[0][0] == '\0')
256 bb_error_msg_and_die("STRING2 cannot be empty");
257 str2_length = expand(*argv, &str2);
258 map(vector, str1, str1_length,
259 str2, str2_length);
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000260 }
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000261 for (i = 0; i < str1_length; i++)
262 invec[(unsigned char)(str1[i])] = TRUE;
263 for (i = 0; i < str2_length; i++)
264 outvec[(unsigned char)(str2[i])] = TRUE;
Denis Vlasenko74324c82007-06-04 10:16:52 +0000265
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000266 goto start_from;
267
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000268 /* In this loop, str1 space is reused as input buffer,
269 * str2 - as output one. */
Denis Vlasenko74324c82007-06-04 10:16:52 +0000270 for (;;) {
271 /* If we're out of input, flush output and read more input. */
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000272 if ((ssize_t)in_index == read_chars) {
Denis Vlasenko74324c82007-06-04 10:16:52 +0000273 if (out_index) {
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000274 xwrite(STDOUT_FILENO, str2, out_index);
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000275 start_from:
Denis Vlasenko74324c82007-06-04 10:16:52 +0000276 out_index = 0;
277 }
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000278 read_chars = safe_read(STDIN_FILENO, str1, TR_BUFSIZ);
Denis Vlasenko74324c82007-06-04 10:16:52 +0000279 if (read_chars <= 0) {
Denis Vlasenkoa89d50f2007-11-13 17:51:40 +0000280 if (read_chars < 0)
281 bb_perror_msg_and_die(bb_msg_read_error);
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000282 break;
Denis Vlasenko74324c82007-06-04 10:16:52 +0000283 }
284 in_index = 0;
285 }
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000286 c = str1[in_index++];
Denis Vlasenko9210a362009-03-15 16:41:55 +0000287 if ((opts & TR_OPT_delete) && invec[c])
Denis Vlasenko74324c82007-06-04 10:16:52 +0000288 continue;
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000289 coded = vector[c];
Denis Vlasenko9210a362009-03-15 16:41:55 +0000290 if ((opts & TR_OPT_squeeze_reps) && last == coded
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000291 && (invec[c] || outvec[coded])
292 ) {
Denis Vlasenko74324c82007-06-04 10:16:52 +0000293 continue;
Denis Vlasenko491d6e32009-03-02 12:05:57 +0000294 }
295 str2[out_index++] = last = coded;
Denis Vlasenko74324c82007-06-04 10:16:52 +0000296 }
Denis Vlasenko1bfcc8b2009-03-01 04:50:18 +0000297
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000298 return EXIT_SUCCESS;
Erik Andersenfb002d02000-03-05 08:07:00 +0000299}