Use global applet_name instead of local versions.
[oweals/busybox.git] / tr.c
diff --git a/tr.c b/tr.c
index 079c252d3acc3a24318acbb5007aa8911aaea217..5a8116db06ccd9b4a0ec78e44766654433303337 100644 (file)
--- a/tr.c
+++ b/tr.c
@@ -2,8 +2,10 @@
 /*
  * Mini tr implementation for busybox
  *
- * This version of tr is adapted from Minix tr
- * Author: Michiel Huisjes
+ * Copyright (c) Michiel Huisjes
+ *
+ * This version of tr is adapted from Minix tr and was modified 
+ * by Erik Andersen <andersee@debian.org> to be used in busybox.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
+#define BB_DECLARE_EXTERN
+#define bb_need_write_error
+#include "messages.c"
+
+const char *tr_usage="tr [-cds] STRING1 [STRING2]\n"
+#ifndef BB_FEATURE_TRIVIAL_HELP
+       "\nTranslate, squeeze, and/or delete characters from\n"
+       "standard input, writing to standard output.\n\n"
+       "Options:\n"
+       "\t-c\ttake complement of STRING1\n"
+       "\t-d\tdelete input characters coded STRING1\n"
+       "\t-s\tsqueeze multiple output characters of STRING2 into one character\n"
+#endif
+;
 
 
 
@@ -58,7 +74,7 @@ static void convert()
                if (in_index == read_chars) {
                        if ((read_chars = read(0, (char *) input, BUFSIZ)) <= 0) {
                                if (write(1, (char *) output, out_index) != out_index)
-                                       write(2, "Bad write\n", 10);
+                                       write(2, write_error, strlen(write_error));
                                exit(0);
                        }
                        in_index = 0;
@@ -72,7 +88,7 @@ static void convert()
                output[out_index++] = last = coded;
                if (out_index == BUFSIZ) {
                        if (write(1, (char *) output, out_index) != out_index) {
-                               write(2, "Bad write\n", 10);
+                               write(2, write_error, strlen(write_error));
                                exit(1);
                        }
                        out_index = 0;
@@ -95,22 +111,14 @@ static void map(register unsigned char *string1, register unsigned char *string2
        }
 }
 
-static void expand(register char *arg, register unsigned char *buffer)
+static void expand(char *arg, register unsigned char *buffer)
 {
        int i, ac;
 
        while (*arg) {
                if (*arg == '\\') {
                        arg++;
-                       i = ac = 0;
-                       if (*arg >= '0' && *arg <= '7') {
-                               do {
-                                       ac = (ac << 3) + *arg++ - '0';
-                                       i++;
-                               } while (i < 4 && *arg >= '0' && *arg <= '7');
-                               *buffer++ = ac;
-                       } else if (*arg != '\0')
-                               *buffer++ = *arg++;
+                       *buffer++ = process_escape_sequence(&arg);
                } else if (*arg == '[') {
                        arg++;
                        i = *arg++;
@@ -165,16 +173,7 @@ extern int tr_main(int argc, char **argv)
                                sq_fl = TRUE;
                                break;
                        default:
-                               usage("tr [-cds] STRING1 [STRING2]\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
-                                         "\nTranslate, squeeze, and/or delete characters from\n"
-                                         "standard input, writing to standard output.\n\n"
-                                         "Options:\n"
-                                         "\t-c\ttake complement of STRING1\n"
-                                         "\t-d\tdelete input characters coded STRING1\n"
-                                         "\t-s\tsqueeze multiple output characters of STRING2 into one character\n"
-#endif
-                                         );
+                               usage(tr_usage);
                        }
                }
                index++;
@@ -188,10 +187,12 @@ extern int tr_main(int argc, char **argv)
                expand(argv[index++], input);
                if (com_fl)
                        complement(input);
-               if (argv[index] != NULL)
+               if (argv[index] != NULL) {
+                       if (*argv[index] == '\0')
+                               fatalError("tr: STRING2 cannot be empty\n");
                        expand(argv[index], output);
-               if (argv[index] != NULL)
                        map(input, output);
+               }
                for (ptr = input; *ptr; ptr++)
                        invec[*ptr] = TRUE;
                for (ptr = output; *ptr; ptr++)