From Matt Kraai <kraai@alumni.carnegiemellon.edu>

> Here is a patch so that tr can handle NULL characters as well.  Give it
> a shot and let me know what you think.  It is against the latest CVS
> version.
>
> In my tests, the following now works as expected.
>
> tr '\0' '\n'
>
> Later,
> Matt
1_00_stable_10817
Eric Andersen 2000-07-13 16:40:41 +00:00
parent ac48461da9
commit 00143ba921
2 changed files with 56 additions and 48 deletions

View File

@ -98,21 +98,23 @@ static void convert()
/* NOTREACHED */ /* NOTREACHED */
} }
static void map(register unsigned char *string1, register unsigned char *string2) static void map(register unsigned char *string1, unsigned int string1_len,
register unsigned char *string2, unsigned int string2_len)
{ {
unsigned char last = '0'; unsigned char last = '0';
unsigned int i, j;
while (*string1) { for (j = 0, i = 0; i < string1_len; i++) {
if (*string2 == '\0') if (string2_len <= j)
vector[*string1] = last; vector[string1[i]] = last;
else else
vector[*string1] = last = *string2++; vector[string1[i]] = last = string2[j++];
string1++;
} }
} }
static void expand(char *arg, register unsigned char *buffer) static unsigned int expand(char *arg, register unsigned char *buffer)
{ {
unsigned char *buffer_start = buffer;
int i, ac; int i, ac;
while (*arg) { while (*arg) {
@ -134,29 +136,31 @@ static void expand(char *arg, register unsigned char *buffer)
} else } else
*buffer++ = *arg++; *buffer++ = *arg++;
} }
return (buffer - buffer_start);
} }
static void complement(unsigned char *buffer) static int complement(unsigned char *buffer, unsigned int buffer_len)
{ {
register unsigned char *ptr; register short i, j, index;
register short i, index;
unsigned char conv[ASCII + 2]; unsigned char conv[ASCII + 2];
index = 0; index = 0;
for (i = 1; i <= ASCII; i++) { for (i = 0; i <= ASCII; i++) {
for (ptr = buffer; *ptr; ptr++) for (j = 0; j < buffer_len; j++)
if (*ptr == i) if (buffer[j] == i)
break; break;
if (*ptr == '\0') if (j == buffer_len)
conv[index++] = i & ASCII; conv[index++] = i & ASCII;
} }
conv[index] = '\0'; memcpy(buffer, conv, index);
strcpy((char *) buffer, (char *) conv); return index;
} }
extern int tr_main(int argc, char **argv) extern int tr_main(int argc, char **argv)
{ {
register unsigned char *ptr; register unsigned char *ptr;
unsigned int output_length, input_length;
int index = 1; int index = 1;
short i; short i;
@ -184,19 +188,19 @@ extern int tr_main(int argc, char **argv)
} }
if (argv[index] != NULL) { if (argv[index] != NULL) {
expand(argv[index++], input); input_length = expand(argv[index++], input);
if (com_fl) if (com_fl)
complement(input); input_length = complement(input, input_length);
if (argv[index] != NULL) { if (argv[index] != NULL) {
if (*argv[index] == '\0') if (*argv[index] == '\0')
fatalError("STRING2 cannot be empty\n"); fatalError("STRING2 cannot be empty\n");
expand(argv[index], output); output_length = expand(argv[index], output);
map(input, output); map(input, input_length, output, output_length);
} }
for (ptr = input; *ptr; ptr++) for (i = 0; i < input_length; i++)
invec[*ptr] = TRUE; invec[input[i]] = TRUE;
for (ptr = output; *ptr; ptr++) for (i = 0; i < output_length; i++)
outvec[*ptr] = TRUE; outvec[output[i]] = TRUE;
} }
convert(); convert();
return (0); return (0);

52
tr.c
View File

@ -98,21 +98,23 @@ static void convert()
/* NOTREACHED */ /* NOTREACHED */
} }
static void map(register unsigned char *string1, register unsigned char *string2) static void map(register unsigned char *string1, unsigned int string1_len,
register unsigned char *string2, unsigned int string2_len)
{ {
unsigned char last = '0'; unsigned char last = '0';
unsigned int i, j;
while (*string1) { for (j = 0, i = 0; i < string1_len; i++) {
if (*string2 == '\0') if (string2_len <= j)
vector[*string1] = last; vector[string1[i]] = last;
else else
vector[*string1] = last = *string2++; vector[string1[i]] = last = string2[j++];
string1++;
} }
} }
static void expand(char *arg, register unsigned char *buffer) static unsigned int expand(char *arg, register unsigned char *buffer)
{ {
unsigned char *buffer_start = buffer;
int i, ac; int i, ac;
while (*arg) { while (*arg) {
@ -134,29 +136,31 @@ static void expand(char *arg, register unsigned char *buffer)
} else } else
*buffer++ = *arg++; *buffer++ = *arg++;
} }
return (buffer - buffer_start);
} }
static void complement(unsigned char *buffer) static int complement(unsigned char *buffer, unsigned int buffer_len)
{ {
register unsigned char *ptr; register short i, j, index;
register short i, index;
unsigned char conv[ASCII + 2]; unsigned char conv[ASCII + 2];
index = 0; index = 0;
for (i = 1; i <= ASCII; i++) { for (i = 0; i <= ASCII; i++) {
for (ptr = buffer; *ptr; ptr++) for (j = 0; j < buffer_len; j++)
if (*ptr == i) if (buffer[j] == i)
break; break;
if (*ptr == '\0') if (j == buffer_len)
conv[index++] = i & ASCII; conv[index++] = i & ASCII;
} }
conv[index] = '\0'; memcpy(buffer, conv, index);
strcpy((char *) buffer, (char *) conv); return index;
} }
extern int tr_main(int argc, char **argv) extern int tr_main(int argc, char **argv)
{ {
register unsigned char *ptr; register unsigned char *ptr;
unsigned int output_length, input_length;
int index = 1; int index = 1;
short i; short i;
@ -184,19 +188,19 @@ extern int tr_main(int argc, char **argv)
} }
if (argv[index] != NULL) { if (argv[index] != NULL) {
expand(argv[index++], input); input_length = expand(argv[index++], input);
if (com_fl) if (com_fl)
complement(input); input_length = complement(input, input_length);
if (argv[index] != NULL) { if (argv[index] != NULL) {
if (*argv[index] == '\0') if (*argv[index] == '\0')
fatalError("STRING2 cannot be empty\n"); fatalError("STRING2 cannot be empty\n");
expand(argv[index], output); output_length = expand(argv[index], output);
map(input, output); map(input, input_length, output, output_length);
} }
for (ptr = input; *ptr; ptr++) for (i = 0; i < input_length; i++)
invec[*ptr] = TRUE; invec[input[i]] = TRUE;
for (ptr = output; *ptr; ptr++) for (i = 0; i < output_length; i++)
outvec[*ptr] = TRUE; outvec[output[i]] = TRUE;
} }
convert(); convert();
return (0); return (0);