Fix bug (wrong value computed) when reading file from stdin, implement
[oweals/busybox.git] / coreutils / tr.c
index ff0f331ffbb565260d96f1a2da7d8aaa37925436..a00e3613427318e10eabffa07c867c250cad5610 100644 (file)
 #include <sys/types.h>
 #include "busybox.h"
 
-static const int ASCII = 0377;
+/* This must be a #define, since when DODEBUG and BUFFERS_GO_IN_BSS are
+ * enabled, we otherwise get a "storage size isn't constant error. */
+#define ASCII 0377
 
-/* some glabals shared across this file */
+/* some "globals" shared across this file */
 static char com_fl, del_fl, sq_fl;
 static short in_index, out_index;
 /* these last are pointers to static buffers declared in tr_main */
@@ -42,7 +44,7 @@ static unsigned char *pvector;
 static char *pinvec, *poutvec;
 
 
-static void convert()
+static void convert(void)
 {
        short read_chars = 0;
        short c, coded;
@@ -52,7 +54,7 @@ static void convert()
                if (in_index == read_chars) {
                        if ((read_chars = read(0, (char *) pinput, BUFSIZ)) <= 0) {
                                if (write(1, (char *) poutput, out_index) != out_index)
-                                       write(2, write_error, strlen(write_error));
+                                       bb_error_msg(bb_msg_write_error);
                                exit(0);
                        }
                        in_index = 0;
@@ -65,10 +67,8 @@ static void convert()
                        continue;
                poutput[out_index++] = last = coded;
                if (out_index == BUFSIZ) {
-                       if (write(1, (char *) poutput, out_index) != out_index) {
-                               write(2, write_error, strlen(write_error));
-                               exit(1);
-                       }
+                       if (write(1, (char *) poutput, out_index) != out_index)
+                               bb_error_msg_and_die(bb_msg_write_error);
                        out_index = 0;
                }
        }
@@ -90,6 +90,10 @@ static void map(register unsigned char *string1, unsigned int string1_len,
        }
 }
 
+/* supported constructs:
+ *   Ranges,  e.g.,  [0-9]  ==>  0123456789
+ *   Escapes, e.g.,  \a     ==>  Control-G
+ */
 static unsigned int expand(const char *arg, register unsigned char *buffer)
 {
        unsigned char *buffer_start = buffer;
@@ -98,7 +102,17 @@ static unsigned int expand(const char *arg, register unsigned char *buffer)
        while (*arg) {
                if (*arg == '\\') {
                        arg++;
-                       *buffer++ = process_escape_sequence(&arg);
+                       *buffer++ = bb_process_escape_sequence(&arg);
+               } else if (*(arg+1) == '-') {
+                       ac = *(arg+2);
+                       if(ac == 0) {
+                               *buffer++ = *arg++;
+                               continue;
+                       }
+                       i = *arg;
+                       while (i <= ac)
+                               *buffer++ = i++;
+                       arg += 3; /* Skip the assumed a-z */
                } else if (*arg == '[') {
                        arg++;
                        i = *arg++;
@@ -110,7 +124,7 @@ static unsigned int expand(const char *arg, register unsigned char *buffer)
                        ac = *arg++;
                        while (i <= ac)
                                *buffer++ = i++;
-                       arg++;                          /* Skip ']' */
+                       arg++;                          /* Skip the assumed ']' */
                } else
                        *buffer++ = *arg++;
        }
@@ -141,11 +155,11 @@ extern int tr_main(int argc, char **argv)
        int output_length=0, input_length;
        int idx = 1;
        int i;
-       RESERVE_BB_BUFFER(output, BUFSIZ);
-       RESERVE_BB_BUFFER(input,  BUFSIZ);
-       RESERVE_BB_UBUFFER(vector, ASCII+1);
-       RESERVE_BB_BUFFER(invec,  ASCII+1);
-       RESERVE_BB_BUFFER(outvec, ASCII+1);
+       RESERVE_CONFIG_BUFFER(output, BUFSIZ);
+       RESERVE_CONFIG_BUFFER(input,  BUFSIZ);
+       RESERVE_CONFIG_UBUFFER(vector, ASCII+1);
+       RESERVE_CONFIG_BUFFER(invec,  ASCII+1);
+       RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
 
        /* ... but make them available globally */
        poutput = output;
@@ -167,7 +181,7 @@ extern int tr_main(int argc, char **argv)
                                sq_fl = TRUE;
                                break;
                        default:
-                               show_usage();
+                               bb_show_usage();
                        }
                }
                idx++;
@@ -183,7 +197,7 @@ extern int tr_main(int argc, char **argv)
                        input_length = complement(input, input_length);
                if (argv[idx] != NULL) {
                        if (*argv[idx] == '\0')
-                               error_msg_and_die("STRING2 cannot be empty");
+                               bb_error_msg_and_die("STRING2 cannot be empty");
                        output_length = expand(argv[idx], output);
                        map(input, input_length, output, output_length);
                }