Patch from Matt Kraai to fix 'echo "1 2 3" | tr -s " "'
[oweals/busybox.git] / coreutils / tr.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini tr implementation for busybox
4  *
5  * Copyright (c) Michiel Huisjes
6  *
7  * This version of tr is adapted from Minix tr and was modified 
8  * by Erik Andersen <andersee@debian.org> to be used in busybox.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  * 
24  * Original copyright notice is retained at the end of this file.
25  */
26
27 #include "busybox.h"
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #define BB_DECLARE_EXTERN
34 #define bb_need_write_error
35 #include "messages.c"
36
37 #ifdef TRUE
38 #undef TRUE
39 #undef FALSE
40 #define TRUE    1
41 #define FALSE   0
42 #endif
43
44 #define ASCII           0377
45
46 /* some glabals shared across this file */
47 static char com_fl, del_fl, sq_fl;
48 static unsigned char output[BUFSIZ], input[BUFSIZ];
49 static unsigned char vector[ASCII + 1];
50 static char invec[ASCII + 1], outvec[ASCII + 1];
51 static short in_index, out_index;
52
53
54 static void convert()
55 {
56         short read_chars = 0;
57         short c, coded;
58         short last = -1;
59
60         for (;;) {
61                 if (in_index == read_chars) {
62                         if ((read_chars = read(0, (char *) input, BUFSIZ)) <= 0) {
63                                 if (write(1, (char *) output, out_index) != out_index)
64                                         write(2, write_error, strlen(write_error));
65                                 exit(0);
66                         }
67                         in_index = 0;
68                 }
69                 c = input[in_index++];
70                 coded = vector[c];
71                 if (del_fl && invec[c])
72                         continue;
73                 if (sq_fl && last == coded && (invec[c] || outvec[coded]))
74                         continue;
75                 output[out_index++] = last = coded;
76                 if (out_index == BUFSIZ) {
77                         if (write(1, (char *) output, out_index) != out_index) {
78                                 write(2, write_error, strlen(write_error));
79                                 exit(1);
80                         }
81                         out_index = 0;
82                 }
83         }
84
85         /* NOTREACHED */
86 }
87
88 static void map(register unsigned char *string1, unsigned int string1_len,
89                 register unsigned char *string2, unsigned int string2_len)
90 {
91         unsigned char last = '0';
92         unsigned int i, j;
93
94         for (j = 0, i = 0; i < string1_len; i++) {
95                 if (string2_len <= j)
96                         vector[string1[i]] = last;
97                 else
98                         vector[string1[i]] = last = string2[j++];
99         }
100 }
101
102 static unsigned int expand(char *arg, register unsigned char *buffer)
103 {
104         unsigned char *buffer_start = buffer;
105         int i, ac;
106
107         while (*arg) {
108                 if (*arg == '\\') {
109                         arg++;
110                         *buffer++ = process_escape_sequence(&arg);
111                 } else if (*arg == '[') {
112                         arg++;
113                         i = *arg++;
114                         if (*arg++ != '-') {
115                                 *buffer++ = '[';
116                                 arg -= 2;
117                                 continue;
118                         }
119                         ac = *arg++;
120                         while (i <= ac)
121                                 *buffer++ = i++;
122                         arg++;                          /* Skip ']' */
123                 } else
124                         *buffer++ = *arg++;
125         }
126
127         return (buffer - buffer_start);
128 }
129
130 static int complement(unsigned char *buffer, int buffer_len)
131 {
132         register short i, j, index;
133         char conv[ASCII + 2];
134
135         index = 0;
136         for (i = 0; i <= ASCII; i++) {
137                 for (j = 0; j < buffer_len; j++)
138                         if (buffer[j] == i)
139                                 break;
140                 if (j == buffer_len)
141                         conv[index++] = i & ASCII;
142         }
143         memcpy(buffer, conv, index);
144         return index;
145 }
146
147 extern int tr_main(int argc, char **argv)
148 {
149         register unsigned char *ptr;
150         int output_length=0, input_length;
151         int index = 1;
152         int i;
153
154         if (argc > 1 && argv[index][0] == '-') {
155                 for (ptr = (unsigned char *) &argv[index][1]; *ptr; ptr++) {
156                         switch (*ptr) {
157                         case 'c':
158                                 com_fl = TRUE;
159                                 break;
160                         case 'd':
161                                 del_fl = TRUE;
162                                 break;
163                         case 's':
164                                 sq_fl = TRUE;
165                                 break;
166                         default:
167                                 usage(tr_usage);
168                         }
169                 }
170                 index++;
171         }
172         for (i = 0; i <= ASCII; i++) {
173                 vector[i] = i;
174                 invec[i] = outvec[i] = FALSE;
175         }
176
177         if (argv[index] != NULL) {
178                 input_length = expand(argv[index++], input);
179                 if (com_fl)
180                         input_length = complement(input, input_length);
181                 if (argv[index] != NULL) {
182                         if (*argv[index] == '\0')
183                                 fatalError("STRING2 cannot be empty\n");
184                         output_length = expand(argv[index], output);
185                         map(input, input_length, output, output_length);
186                 }
187                 for (i = 0; i < input_length; i++)
188                         invec[input[i]] = TRUE;
189                 for (i = 0; i < output_length; i++)
190                         outvec[output[i]] = TRUE;
191         }
192         convert();
193         return (0);
194 }
195
196 /*
197  * Copyright (c) 1987,1997, Prentice Hall
198  * All rights reserved.
199  * 
200  * Redistribution and use of the MINIX operating system in source and
201  * binary forms, with or without modification, are permitted provided
202  * that the following conditions are met:
203  * 
204  * Redistributions of source code must retain the above copyright
205  * notice, this list of conditions and the following disclaimer.
206  * 
207  * Redistributions in binary form must reproduce the above
208  * copyright notice, this list of conditions and the following
209  * disclaimer in the documentation and/or other materials provided
210  * with the distribution.
211  * 
212  * Neither the name of Prentice Hall nor the names of the software
213  * authors or contributors may be used to endorse or promote
214  * products derived from this software without specific prior
215  * written permission.
216  * 
217  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
218  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
219  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
220  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
221  * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
222  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
223  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
224  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
225  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
226  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
227  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
228  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
229  *
230  */
231