blob: 9521bbcd507f5da3cf5b08b8e847aaaf56f39f41 [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
19 * TODO: xdigit, graph, print
20 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000021#include "libbb.h"
Erik Andersen330fd2b2000-05-19 05:35:19 +000022
Eric Andersen22ecf042001-07-02 17:32:40 +000023#define ASCII 0377
Erik Andersenfb002d02000-03-05 08:07:00 +000024
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000025#define TR_OPT_complement (1<<0)
26#define TR_OPT_delete (1<<1)
27#define TR_OPT_squeeze_reps (1<<2)
Erik Andersenfb002d02000-03-05 08:07:00 +000028
Denis Vlasenko74324c82007-06-04 10:16:52 +000029static void map(char *pvector,
30 unsigned char *string1, unsigned int string1_len,
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000031 unsigned char *string2, unsigned int string2_len)
Erik Andersen8f8d6d52000-05-01 22:30:37 +000032{
Rob Landleyab58d5c2006-06-30 19:04:09 +000033 char last = '0';
Eric Andersen00143ba2000-07-13 16:40:41 +000034 unsigned int i, j;
Erik Andersen8f8d6d52000-05-01 22:30:37 +000035
Eric Andersen00143ba2000-07-13 16:40:41 +000036 for (j = 0, i = 0; i < string1_len; i++) {
37 if (string2_len <= j)
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000038 pvector[string1[i]] = last;
Erik Andersen8f8d6d52000-05-01 22:30:37 +000039 else
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000040 pvector[string1[i]] = last = string2[j++];
Erik Andersen8f8d6d52000-05-01 22:30:37 +000041 }
42}
43
Mark Whitley8b7a0d82001-05-24 21:31:09 +000044/* supported constructs:
45 * Ranges, e.g., [0-9] ==> 0123456789
46 * Escapes, e.g., \a ==> Control-G
Rob Landleyf1048142005-10-08 21:21:08 +000047 * Character classes, e.g. [:upper:] ==> A ... Z
Mark Whitley8b7a0d82001-05-24 21:31:09 +000048 */
Rob Landleyab58d5c2006-06-30 19:04:09 +000049static unsigned int expand(const char *arg, char *buffer)
Erik Andersen8f8d6d52000-05-01 22:30:37 +000050{
Rob Landleyab58d5c2006-06-30 19:04:09 +000051 char *buffer_start = buffer;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000052 unsigned i; /* XXX: FIXME: use unsigned char? */
53 unsigned char ac;
Denis Vlasenko990d0f62007-07-24 15:54:42 +000054#define CLO ":]\0"
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000055 static const char classes[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +000056 "alpha"CLO "alnum"CLO "digit"CLO "lower"CLO "upper"CLO "space"CLO
57 "blank"CLO "punct"CLO "cntrl"CLO;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000058#define CLASS_invalid 0 /* we increment the retval */
59#define CLASS_alpha 1
60#define CLASS_alnum 2
61#define CLASS_digit 3
62#define CLASS_lower 4
63#define CLASS_upper 5
64#define CLASS_space 6
65#define CLASS_blank 7
66#define CLASS_punct 8
67#define CLASS_cntrl 9
68//#define CLASS_xdigit 10
69//#define CLASS_graph 11
70//#define CLASS_print 12
Erik Andersen8f8d6d52000-05-01 22:30:37 +000071 while (*arg) {
72 if (*arg == '\\') {
73 arg++;
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 *buffer++ = bb_process_escape_sequence(&arg);
Eric Andersen5a4a46a2001-07-09 21:32:29 +000075 } else if (*(arg+1) == '-') {
76 ac = *(arg+2);
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000077 if (ac == 0) {
Eric Andersen5a4a46a2001-07-09 21:32:29 +000078 *buffer++ = *arg++;
79 continue;
80 }
81 i = *arg;
82 while (i <= ac)
83 *buffer++ = i++;
84 arg += 3; /* Skip the assumed a-z */
Erik Andersen8f8d6d52000-05-01 22:30:37 +000085 } else if (*arg == '[') {
86 arg++;
Rob Landley998dbee2006-04-19 22:22:06 +000087 i = *arg++;
88 if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000089 smalluint j;
90 { /* not really pretty.. */
Denis Vlasenko74324c82007-06-04 10:16:52 +000091 char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7
Denis Vlasenko990d0f62007-07-24 15:54:42 +000092 j = index_in_strings(classes, tmp) + 1;
Denis Vlasenko74324c82007-06-04 10:16:52 +000093 free(tmp);
Rob Landleyf1048142005-10-08 21:21:08 +000094 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000095 if (j == CLASS_alnum || j == CLASS_digit) {
Rob Landley998dbee2006-04-19 22:22:06 +000096 for (i = '0'; i <= '9'; i++)
97 *buffer++ = i;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000098 }
99 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
Rob Landleyf1048142005-10-08 21:21:08 +0000100 for (i = 'A'; i <= 'Z'; i++)
101 *buffer++ = i;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000102 }
103 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
Rob Landleyf1048142005-10-08 21:21:08 +0000104 for (i = 'a'; i <= 'z'; i++)
105 *buffer++ = i;
Rob Landleyf1048142005-10-08 21:21:08 +0000106 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000107 if (j == CLASS_space || j == CLASS_blank) {
Rob Landley998dbee2006-04-19 22:22:06 +0000108 *buffer++ = '\t';
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000109 if (j == CLASS_space) {
110 *buffer++ = '\n';
111 *buffer++ = '\v';
112 *buffer++ = '\f';
113 *buffer++ = '\r';
114 }
Rob Landley998dbee2006-04-19 22:22:06 +0000115 *buffer++ = ' ';
116 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000117 if (j == CLASS_punct || j == CLASS_cntrl) {
Rob Landleyf1048142005-10-08 21:21:08 +0000118 for (i = 0; i <= ASCII; i++)
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000119 if ((j == CLASS_punct &&
120 isprint(i) && (!isalnum(i)) && (!isspace(i))) ||
121 (j == CLASS_cntrl && iscntrl(i)))
Rob Landleyf1048142005-10-08 21:21:08 +0000122 *buffer++ = i;
123 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000124 if (j == CLASS_invalid) {
Rob Landley998dbee2006-04-19 22:22:06 +0000125 *buffer++ = '[';
126 *buffer++ = ':';
Rob Landleyf1048142005-10-08 21:21:08 +0000127 continue;
128 }
129 break;
130 }
Rob Landley998dbee2006-04-19 22:22:06 +0000131 if (ENABLE_FEATURE_TR_EQUIV && i == '=') {
Rob Landleyf1048142005-10-08 21:21:08 +0000132 *buffer++ = *arg;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000133 arg += 3; /* Skip the closing =] */
Rob Landleyf1048142005-10-08 21:21:08 +0000134 continue;
135 }
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000136 if (*arg++ != '-') {
137 *buffer++ = '[';
138 arg -= 2;
139 continue;
140 }
141 ac = *arg++;
142 while (i <= ac)
143 *buffer++ = i++;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000144 arg++; /* Skip the assumed ']' */
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000145 } else
146 *buffer++ = *arg++;
147 }
Eric Andersen00143ba2000-07-13 16:40:41 +0000148 return (buffer - buffer_start);
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000149}
150
Rob Landleyab58d5c2006-06-30 19:04:09 +0000151static int complement(char *buffer, int buffer_len)
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000152{
Denis Vlasenko74324c82007-06-04 10:16:52 +0000153 int i, j, ix;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000154 char conv[ASCII + 2];
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000155
Eric Andersen1ca20a72001-03-21 07:34:27 +0000156 ix = 0;
Eric Andersen00143ba2000-07-13 16:40:41 +0000157 for (i = 0; i <= ASCII; i++) {
158 for (j = 0; j < buffer_len; j++)
159 if (buffer[j] == i)
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000160 break;
Eric Andersen00143ba2000-07-13 16:40:41 +0000161 if (j == buffer_len)
Eric Andersen1ca20a72001-03-21 07:34:27 +0000162 conv[ix++] = i & ASCII;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000163 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000164 memcpy(buffer, conv, ix);
165 return ix;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000166}
167
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000168int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000169int tr_main(int argc, char **argv)
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000170{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000171 unsigned char *ptr;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000172 int output_length = 0, input_length;
Eric Andersenf6aa13d2001-03-23 17:08:21 +0000173 int idx = 1;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000174 int i;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000175 smalluint flags = 0;
Denis Vlasenko74324c82007-06-04 10:16:52 +0000176 size_t read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000177 RESERVE_CONFIG_UBUFFER(output, BUFSIZ);
Rob Landley998f4492006-04-10 16:40:47 +0000178 RESERVE_CONFIG_BUFFER(vector, ASCII+1);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000179 RESERVE_CONFIG_BUFFER(invec, ASCII+1);
180 RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
Mark Whitley59ab0252001-01-23 22:30:04 +0000181
Eric Andersenf6aa13d2001-03-23 17:08:21 +0000182 if (argc > 1 && argv[idx][0] == '-') {
183 for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000184 if (*ptr == 'c')
185 flags |= TR_OPT_complement;
186 else if (*ptr == 'd')
187 flags |= TR_OPT_delete;
188 else if (*ptr == 's')
189 flags |= TR_OPT_squeeze_reps;
190 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191 bb_show_usage();
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000192 }
Eric Andersenf6aa13d2001-03-23 17:08:21 +0000193 idx++;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000194 }
195 for (i = 0; i <= ASCII; i++) {
196 vector[i] = i;
197 invec[i] = outvec[i] = FALSE;
198 }
199
Denis Vlasenko74324c82007-06-04 10:16:52 +0000200#define tr_buf bb_common_bufsiz1
Eric Andersenf6aa13d2001-03-23 17:08:21 +0000201 if (argv[idx] != NULL) {
Denis Vlasenko74324c82007-06-04 10:16:52 +0000202 input_length = expand(argv[idx++], tr_buf);
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000203 if (flags & TR_OPT_complement)
Denis Vlasenko74324c82007-06-04 10:16:52 +0000204 input_length = complement(tr_buf, input_length);
Eric Andersenf6aa13d2001-03-23 17:08:21 +0000205 if (argv[idx] != NULL) {
206 if (*argv[idx] == '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000207 bb_error_msg_and_die("STRING2 cannot be empty");
Rob Landleyab58d5c2006-06-30 19:04:09 +0000208 output_length = expand(argv[idx], output);
Denis Vlasenko74324c82007-06-04 10:16:52 +0000209 map(vector, tr_buf, input_length, output, output_length);
Eric Andersena03d86c2000-07-10 16:38:50 +0000210 }
Eric Andersen00143ba2000-07-13 16:40:41 +0000211 for (i = 0; i < input_length; i++)
Denis Vlasenko74324c82007-06-04 10:16:52 +0000212 invec[(unsigned char)tr_buf[i]] = TRUE;
Eric Andersen00143ba2000-07-13 16:40:41 +0000213 for (i = 0; i < output_length; i++)
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000214 outvec[output[i]] = TRUE;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000215 }
Denis Vlasenko74324c82007-06-04 10:16:52 +0000216
217 for (;;) {
218 /* If we're out of input, flush output and read more input. */
219 if (in_index == read_chars) {
220 if (out_index) {
221 xwrite(STDOUT_FILENO, (char *)output, out_index);
222 out_index = 0;
223 }
224 read_chars = read(STDIN_FILENO, tr_buf, BUFSIZ);
225 if (read_chars <= 0) {
226 if (write(STDOUT_FILENO, (char *)output, out_index) != out_index)
227 bb_perror_msg(bb_msg_write_error);
228 exit(EXIT_SUCCESS);
229 }
230 in_index = 0;
231 }
232 c = tr_buf[in_index++];
233 coded = vector[c];
234 if ((flags & TR_OPT_delete) && invec[c])
235 continue;
236 if ((flags & TR_OPT_squeeze_reps) && last == coded &&
237 (invec[c] || outvec[coded]))
238 continue;
239 output[out_index++] = last = coded;
240 }
241 /* NOTREACHED */
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000242 return EXIT_SUCCESS;
Erik Andersenfb002d02000-03-05 08:07:00 +0000243}