Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 3 | * Mini tr implementation for busybox |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 4 | * |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 5 | * This version of tr is adapted from Minix tr |
| 6 | * Author: Michiel Huisjes |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 7 | * |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 8 | * 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 Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 25 | #include "internal.h" |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 27 | #include <string.h> |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 28 | #include <stdlib.h> |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 29 | #include <unistd.h> |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 30 | #include <sys/types.h> |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 31 | |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 32 | |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 33 | |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 34 | #ifdef TRUE |
| 35 | #undef TRUE |
| 36 | #undef FALSE |
| 37 | #define TRUE 1 |
| 38 | #define FALSE 0 |
| 39 | #endif |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 40 | |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 41 | #define ASCII 0377 |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 42 | |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 43 | /* some glabals shared across this file */ |
| 44 | static char com_fl, del_fl, sq_fl; |
| 45 | static unsigned char output[BUFSIZ], input[BUFSIZ]; |
| 46 | static unsigned char vector[ASCII + 1]; |
| 47 | static char invec[ASCII + 1], outvec[ASCII + 1]; |
| 48 | static short in_index, out_index; |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 49 | |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 50 | |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 51 | static void convert() |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 52 | { |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 53 | short read_chars = 0; |
| 54 | short c, coded; |
| 55 | short last = -1; |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 56 | |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 57 | 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 Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 63 | } |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 64 | 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 Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 77 | } |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 78 | out_index = 0; |
| 79 | } |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 82 | /* NOTREACHED */ |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 83 | } |
| 84 | |
| 85 | static 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 | |
| 98 | static 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 | |
| 131 | static 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 | |
| 149 | extern 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 Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 201 | return (0); |
| 202 | } |
| 203 | |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 204 | /* |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame^] | 205 | * 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 Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 238 | */ |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 239 | |