blob: 079c252d3acc3a24318acbb5007aa8911aaea217 [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 *
Erik Andersen8f8d6d52000-05-01 22:30:37 +00005 * This version of tr is adapted from Minix tr
6 * Author: Michiel Huisjes
Erik Andersenfb002d02000-03-05 08:07:00 +00007 *
Erik Andersen8f8d6d52000-05-01 22:30:37 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Original copyright notice is retained at the end of this file.
Erik Andersenfb002d02000-03-05 08:07:00 +000023 */
24
Erik Andersenfb002d02000-03-05 08:07:00 +000025#include "internal.h"
Erik Andersenfb002d02000-03-05 08:07:00 +000026#include <stdio.h>
Erik Andersenfb002d02000-03-05 08:07:00 +000027#include <string.h>
Erik Andersen8f8d6d52000-05-01 22:30:37 +000028#include <stdlib.h>
Erik Andersenfb002d02000-03-05 08:07:00 +000029#include <unistd.h>
Erik Andersen8f8d6d52000-05-01 22:30:37 +000030#include <sys/types.h>
Erik Andersenfb002d02000-03-05 08:07:00 +000031
Erik Andersenfb002d02000-03-05 08:07:00 +000032
Erik Andersenfb002d02000-03-05 08:07:00 +000033
Erik Andersen8f8d6d52000-05-01 22:30:37 +000034#ifdef TRUE
35#undef TRUE
36#undef FALSE
37#define TRUE 1
38#define FALSE 0
39#endif
Erik Andersenfb002d02000-03-05 08:07:00 +000040
Erik Andersen8f8d6d52000-05-01 22:30:37 +000041#define ASCII 0377
Erik Andersenfb002d02000-03-05 08:07:00 +000042
Erik Andersen8f8d6d52000-05-01 22:30:37 +000043/* some glabals shared across this file */
44static char com_fl, del_fl, sq_fl;
45static unsigned char output[BUFSIZ], input[BUFSIZ];
46static unsigned char vector[ASCII + 1];
47static char invec[ASCII + 1], outvec[ASCII + 1];
48static short in_index, out_index;
Erik Andersenfb002d02000-03-05 08:07:00 +000049
Erik Andersenfb002d02000-03-05 08:07:00 +000050
Erik Andersen8f8d6d52000-05-01 22:30:37 +000051static void convert()
Erik Andersenfb002d02000-03-05 08:07:00 +000052{
Erik Andersen8f8d6d52000-05-01 22:30:37 +000053 short read_chars = 0;
54 short c, coded;
55 short last = -1;
Erik Andersenfb002d02000-03-05 08:07:00 +000056
Erik Andersen8f8d6d52000-05-01 22:30:37 +000057 for (;;) {
58 if (in_index == read_chars) {
59 if ((read_chars = read(0, (char *) input, BUFSIZ)) <= 0) {
60 if (write(1, (char *) output, out_index) != out_index)
61 write(2, "Bad write\n", 10);
62 exit(0);
Erik Andersenfb002d02000-03-05 08:07:00 +000063 }
Erik Andersen8f8d6d52000-05-01 22:30:37 +000064 in_index = 0;
65 }
66 c = input[in_index++];
67 coded = vector[c];
68 if (del_fl && invec[c])
69 continue;
70 if (sq_fl && last == coded && outvec[coded])
71 continue;
72 output[out_index++] = last = coded;
73 if (out_index == BUFSIZ) {
74 if (write(1, (char *) output, out_index) != out_index) {
75 write(2, "Bad write\n", 10);
76 exit(1);
Erik Andersenfb002d02000-03-05 08:07:00 +000077 }
Erik Andersen8f8d6d52000-05-01 22:30:37 +000078 out_index = 0;
79 }
Erik Andersenfb002d02000-03-05 08:07:00 +000080 }
81
Erik Andersenfb002d02000-03-05 08:07:00 +000082 /* NOTREACHED */
Erik Andersen8f8d6d52000-05-01 22:30:37 +000083}
84
85static void map(register unsigned char *string1, register unsigned char *string2)
86{
87 unsigned char last = '0';
88
89 while (*string1) {
90 if (*string2 == '\0')
91 vector[*string1] = last;
92 else
93 vector[*string1] = last = *string2++;
94 string1++;
95 }
96}
97
98static void expand(register char *arg, register unsigned char *buffer)
99{
100 int i, ac;
101
102 while (*arg) {
103 if (*arg == '\\') {
104 arg++;
105 i = ac = 0;
106 if (*arg >= '0' && *arg <= '7') {
107 do {
108 ac = (ac << 3) + *arg++ - '0';
109 i++;
110 } while (i < 4 && *arg >= '0' && *arg <= '7');
111 *buffer++ = ac;
112 } else if (*arg != '\0')
113 *buffer++ = *arg++;
114 } else if (*arg == '[') {
115 arg++;
116 i = *arg++;
117 if (*arg++ != '-') {
118 *buffer++ = '[';
119 arg -= 2;
120 continue;
121 }
122 ac = *arg++;
123 while (i <= ac)
124 *buffer++ = i++;
125 arg++; /* Skip ']' */
126 } else
127 *buffer++ = *arg++;
128 }
129}
130
131static void complement(unsigned char *buffer)
132{
133 register unsigned char *ptr;
134 register short i, index;
135 unsigned char conv[ASCII + 2];
136
137 index = 0;
138 for (i = 1; i <= ASCII; i++) {
139 for (ptr = buffer; *ptr; ptr++)
140 if (*ptr == i)
141 break;
142 if (*ptr == '\0')
143 conv[index++] = i & ASCII;
144 }
145 conv[index] = '\0';
146 strcpy((char *) buffer, (char *) conv);
147}
148
149extern int tr_main(int argc, char **argv)
150{
151 register unsigned char *ptr;
152 int index = 1;
153 short i;
154
155 if (argc > 1 && argv[index][0] == '-') {
156 for (ptr = (unsigned char *) &argv[index][1]; *ptr; ptr++) {
157 switch (*ptr) {
158 case 'c':
159 com_fl = TRUE;
160 break;
161 case 'd':
162 del_fl = TRUE;
163 break;
164 case 's':
165 sq_fl = TRUE;
166 break;
167 default:
168 usage("tr [-cds] STRING1 [STRING2]\n"
169#ifndef BB_FEATURE_TRIVIAL_HELP
170 "\nTranslate, squeeze, and/or delete characters from\n"
171 "standard input, writing to standard output.\n\n"
172 "Options:\n"
173 "\t-c\ttake complement of STRING1\n"
174 "\t-d\tdelete input characters coded STRING1\n"
175 "\t-s\tsqueeze multiple output characters of STRING2 into one character\n"
176#endif
177 );
178 }
179 }
180 index++;
181 }
182 for (i = 0; i <= ASCII; i++) {
183 vector[i] = i;
184 invec[i] = outvec[i] = FALSE;
185 }
186
187 if (argv[index] != NULL) {
188 expand(argv[index++], input);
189 if (com_fl)
190 complement(input);
191 if (argv[index] != NULL)
192 expand(argv[index], output);
193 if (argv[index] != NULL)
194 map(input, output);
195 for (ptr = input; *ptr; ptr++)
196 invec[*ptr] = TRUE;
197 for (ptr = output; *ptr; ptr++)
198 outvec[*ptr] = TRUE;
199 }
200 convert();
Erik Andersenfb002d02000-03-05 08:07:00 +0000201 return (0);
202}
203
Erik Andersenfb002d02000-03-05 08:07:00 +0000204/*
Erik Andersen8f8d6d52000-05-01 22:30:37 +0000205 * Copyright (c) 1987,1997, Prentice Hall
206 * All rights reserved.
207 *
208 * Redistribution and use of the MINIX operating system in source and
209 * binary forms, with or without modification, are permitted provided
210 * that the following conditions are met:
211 *
212 * Redistributions of source code must retain the above copyright
213 * notice, this list of conditions and the following disclaimer.
214 *
215 * Redistributions in binary form must reproduce the above
216 * copyright notice, this list of conditions and the following
217 * disclaimer in the documentation and/or other materials provided
218 * with the distribution.
219 *
220 * Neither the name of Prentice Hall nor the names of the software
221 * authors or contributors may be used to endorse or promote
222 * products derived from this software without specific prior
223 * written permission.
224 *
225 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
226 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
227 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
228 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
229 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
230 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
231 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
232 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
233 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
234 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
235 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
236 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237 *
Erik Andersenfb002d02000-03-05 08:07:00 +0000238 */
Erik Andersenfb002d02000-03-05 08:07:00 +0000239