blob: 5d3dd4cd8e68c417c4bbbd4549982898290a3088 [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 */
Eric Andersencbe31da2001-02-20 06:14:08 +000021#include "busybox.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)
Mark Whitley8b7a0d82001-05-24 21:31:09 +000028/* some "globals" shared across this file */
Mark Whitley59ab0252001-01-23 22:30:04 +000029/* these last are pointers to static buffers declared in tr_main */
Rob Landleyab58d5c2006-06-30 19:04:09 +000030static char *poutput, *pvector, *pinvec, *poutvec;
Erik Andersenfb002d02000-03-05 08:07:00 +000031
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000032static void ATTRIBUTE_NORETURN convert(const smalluint flags)
Erik Andersenfb002d02000-03-05 08:07:00 +000033{
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000034 size_t read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1;
Erik Andersenfb002d02000-03-05 08:07:00 +000035
Erik Andersen8f8d6d52000-05-01 22:30:37 +000036 for (;;) {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000037 /* If we're out of input, flush output and read more input. */
Erik Andersen8f8d6d52000-05-01 22:30:37 +000038 if (in_index == read_chars) {
Rob Landleycd545282006-06-30 16:35:40 +000039 if (out_index) {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000040 xwrite(STDOUT_FILENO, (char *)poutput, out_index);
Rob Landleycd545282006-06-30 16:35:40 +000041 out_index = 0;
42 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000043 if ((read_chars = read(STDIN_FILENO, bb_common_bufsiz1, BUFSIZ)) <= 0) {
44 if (write(STDOUT_FILENO, (char *)poutput, out_index) != out_index)
45 bb_perror_msg(bb_msg_write_error);
46 exit(EXIT_SUCCESS);
Erik Andersenfb002d02000-03-05 08:07:00 +000047 }
Erik Andersen8f8d6d52000-05-01 22:30:37 +000048 in_index = 0;
49 }
Rob Landleycd545282006-06-30 16:35:40 +000050 c = bb_common_bufsiz1[in_index++];
Mark Whitley59ab0252001-01-23 22:30:04 +000051 coded = pvector[c];
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000052 if ((flags & TR_OPT_delete) && pinvec[c])
Erik Andersen8f8d6d52000-05-01 22:30:37 +000053 continue;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000054 if ((flags & TR_OPT_squeeze_reps) && last == coded &&
55 (pinvec[c] || poutvec[coded]))
Erik Andersen8f8d6d52000-05-01 22:30:37 +000056 continue;
Mark Whitley59ab0252001-01-23 22:30:04 +000057 poutput[out_index++] = last = coded;
Erik Andersenfb002d02000-03-05 08:07:00 +000058 }
Erik Andersenfb002d02000-03-05 08:07:00 +000059 /* NOTREACHED */
Erik Andersen8f8d6d52000-05-01 22:30:37 +000060}
61
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000062static void map(unsigned char *string1, unsigned int string1_len,
63 unsigned char *string2, unsigned int string2_len)
Erik Andersen8f8d6d52000-05-01 22:30:37 +000064{
Rob Landleyab58d5c2006-06-30 19:04:09 +000065 char last = '0';
Eric Andersen00143ba2000-07-13 16:40:41 +000066 unsigned int i, j;
Erik Andersen8f8d6d52000-05-01 22:30:37 +000067
Eric Andersen00143ba2000-07-13 16:40:41 +000068 for (j = 0, i = 0; i < string1_len; i++) {
69 if (string2_len <= j)
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000070 pvector[string1[i]] = last;
Erik Andersen8f8d6d52000-05-01 22:30:37 +000071 else
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000072 pvector[string1[i]] = last = string2[j++];
Erik Andersen8f8d6d52000-05-01 22:30:37 +000073 }
74}
75
Mark Whitley8b7a0d82001-05-24 21:31:09 +000076/* supported constructs:
77 * Ranges, e.g., [0-9] ==> 0123456789
78 * Escapes, e.g., \a ==> Control-G
Rob Landleyf1048142005-10-08 21:21:08 +000079 * Character classes, e.g. [:upper:] ==> A ... Z
Mark Whitley8b7a0d82001-05-24 21:31:09 +000080 */
Rob Landleyab58d5c2006-06-30 19:04:09 +000081static unsigned int expand(const char *arg, char *buffer)
Erik Andersen8f8d6d52000-05-01 22:30:37 +000082{
Rob Landleyab58d5c2006-06-30 19:04:09 +000083 char *buffer_start = buffer;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000084 unsigned i; /* XXX: FIXME: use unsigned char? */
85 unsigned char ac;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000086#define CLO ":]"
Denis Vlasenko240a1cf2007-04-08 16:07:02 +000087 static const char * const classes[] = {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000088 "alpha"CLO, "alnum"CLO, "digit"CLO, "lower"CLO, "upper"CLO, "space"CLO,
89 "blank"CLO, "punct"CLO, "cntrl"CLO, NULL
90 };
91#define CLASS_invalid 0 /* we increment the retval */
92#define CLASS_alpha 1
93#define CLASS_alnum 2
94#define CLASS_digit 3
95#define CLASS_lower 4
96#define CLASS_upper 5
97#define CLASS_space 6
98#define CLASS_blank 7
99#define CLASS_punct 8
100#define CLASS_cntrl 9
101//#define CLASS_xdigit 10
102//#define CLASS_graph 11
103//#define CLASS_print 12
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000104 while (*arg) {
105 if (*arg == '\\') {
106 arg++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107 *buffer++ = bb_process_escape_sequence(&arg);
Eric Andersen5a4a46a2001-07-09 21:32:29 +0000108 } else if (*(arg+1) == '-') {
109 ac = *(arg+2);
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000110 if (ac == 0) {
Eric Andersen5a4a46a2001-07-09 21:32:29 +0000111 *buffer++ = *arg++;
112 continue;
113 }
114 i = *arg;
115 while (i <= ac)
116 *buffer++ = i++;
117 arg += 3; /* Skip the assumed a-z */
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000118 } else if (*arg == '[') {
119 arg++;
Rob Landley998dbee2006-04-19 22:22:06 +0000120 i = *arg++;
121 if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000122 smalluint j;
123 { /* not really pretty.. */
124 char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7
125 j = index_in_str_array(classes, tmp) + 1;
126 free(tmp);
Rob Landleyf1048142005-10-08 21:21:08 +0000127 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000128 if (j == CLASS_alnum || j == CLASS_digit) {
Rob Landley998dbee2006-04-19 22:22:06 +0000129 for (i = '0'; i <= '9'; i++)
130 *buffer++ = i;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000131 }
132 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
Rob Landleyf1048142005-10-08 21:21:08 +0000133 for (i = 'A'; i <= 'Z'; i++)
134 *buffer++ = i;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000135 }
136 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
Rob Landleyf1048142005-10-08 21:21:08 +0000137 for (i = 'a'; i <= 'z'; i++)
138 *buffer++ = i;
Rob Landleyf1048142005-10-08 21:21:08 +0000139 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000140 if (j == CLASS_space || j == CLASS_blank) {
Rob Landley998dbee2006-04-19 22:22:06 +0000141 *buffer++ = '\t';
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000142 if (j == CLASS_space) {
143 *buffer++ = '\n';
144 *buffer++ = '\v';
145 *buffer++ = '\f';
146 *buffer++ = '\r';
147 }
Rob Landley998dbee2006-04-19 22:22:06 +0000148 *buffer++ = ' ';
149 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000150 if (j == CLASS_punct || j == CLASS_cntrl) {
Rob Landleyf1048142005-10-08 21:21:08 +0000151 for (i = 0; i <= ASCII; i++)
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000152 if ((j == CLASS_punct &&
153 isprint(i) && (!isalnum(i)) && (!isspace(i))) ||
154 (j == CLASS_cntrl && iscntrl(i)))
Rob Landleyf1048142005-10-08 21:21:08 +0000155 *buffer++ = i;
156 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000157 if (j == CLASS_invalid) {
Rob Landley998dbee2006-04-19 22:22:06 +0000158 *buffer++ = '[';
159 *buffer++ = ':';
Rob Landleyf1048142005-10-08 21:21:08 +0000160 continue;
161 }
162 break;
163 }
Rob Landley998dbee2006-04-19 22:22:06 +0000164 if (ENABLE_FEATURE_TR_EQUIV && i == '=') {
Rob Landleyf1048142005-10-08 21:21:08 +0000165 *buffer++ = *arg;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000166 arg += 3; /* Skip the closing =] */
Rob Landleyf1048142005-10-08 21:21:08 +0000167 continue;
168 }
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000169 if (*arg++ != '-') {
170 *buffer++ = '[';
171 arg -= 2;
172 continue;
173 }
174 ac = *arg++;
175 while (i <= ac)
176 *buffer++ = i++;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000177 arg++; /* Skip the assumed ']' */
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000178 } else
179 *buffer++ = *arg++;
180 }
Eric Andersen00143ba2000-07-13 16:40:41 +0000181 return (buffer - buffer_start);
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000182}
183
Rob Landleyab58d5c2006-06-30 19:04:09 +0000184static int complement(char *buffer, int buffer_len)
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000185{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000186 short i, j, ix;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000187 char conv[ASCII + 2];
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000188
Eric Andersen1ca20a72001-03-21 07:34:27 +0000189 ix = 0;
Eric Andersen00143ba2000-07-13 16:40:41 +0000190 for (i = 0; i <= ASCII; i++) {
191 for (j = 0; j < buffer_len; j++)
192 if (buffer[j] == i)
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000193 break;
Eric Andersen00143ba2000-07-13 16:40:41 +0000194 if (j == buffer_len)
Eric Andersen1ca20a72001-03-21 07:34:27 +0000195 conv[ix++] = i & ASCII;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000196 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000197 memcpy(buffer, conv, ix);
198 return ix;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000199}
200
Denis Vlasenko06af2162007-02-03 17:28:39 +0000201int tr_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000202int tr_main(int argc, char **argv)
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000203{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000204 unsigned char *ptr;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000205 int output_length = 0, input_length;
Eric Andersenf6aa13d2001-03-23 17:08:21 +0000206 int idx = 1;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000207 int i;
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000208 smalluint flags = 0;
209 RESERVE_CONFIG_UBUFFER(output, BUFSIZ);
Rob Landley998f4492006-04-10 16:40:47 +0000210 RESERVE_CONFIG_BUFFER(vector, ASCII+1);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000211 RESERVE_CONFIG_BUFFER(invec, ASCII+1);
212 RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
Mark Whitley59ab0252001-01-23 22:30:04 +0000213
214 /* ... but make them available globally */
Rob Landleyab58d5c2006-06-30 19:04:09 +0000215 poutput = output;
216 pvector = vector;
217 pinvec = invec;
218 poutvec = outvec;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000219
Eric Andersenf6aa13d2001-03-23 17:08:21 +0000220 if (argc > 1 && argv[idx][0] == '-') {
221 for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000222 if (*ptr == 'c')
223 flags |= TR_OPT_complement;
224 else if (*ptr == 'd')
225 flags |= TR_OPT_delete;
226 else if (*ptr == 's')
227 flags |= TR_OPT_squeeze_reps;
228 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000229 bb_show_usage();
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000230 }
Eric Andersenf6aa13d2001-03-23 17:08:21 +0000231 idx++;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000232 }
233 for (i = 0; i <= ASCII; i++) {
234 vector[i] = i;
235 invec[i] = outvec[i] = FALSE;
236 }
237
Eric Andersenf6aa13d2001-03-23 17:08:21 +0000238 if (argv[idx] != NULL) {
Rob Landleycd545282006-06-30 16:35:40 +0000239 input_length = expand(argv[idx++], bb_common_bufsiz1);
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000240 if (flags & TR_OPT_complement)
Rob Landleycd545282006-06-30 16:35:40 +0000241 input_length = complement(bb_common_bufsiz1, input_length);
Eric Andersenf6aa13d2001-03-23 17:08:21 +0000242 if (argv[idx] != NULL) {
243 if (*argv[idx] == '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000244 bb_error_msg_and_die("STRING2 cannot be empty");
Rob Landleyab58d5c2006-06-30 19:04:09 +0000245 output_length = expand(argv[idx], output);
246 map(bb_common_bufsiz1, input_length, output, output_length);
Eric Andersena03d86c2000-07-10 16:38:50 +0000247 }
Eric Andersen00143ba2000-07-13 16:40:41 +0000248 for (i = 0; i < input_length; i++)
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000249 invec[(unsigned char)bb_common_bufsiz1[i]] = TRUE;
Eric Andersen00143ba2000-07-13 16:40:41 +0000250 for (i = 0; i < output_length; i++)
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000251 outvec[output[i]] = TRUE;
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000252 }
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +0000253 convert(flags);
254 return EXIT_SUCCESS;
Erik Andersenfb002d02000-03-05 08:07:00 +0000255}