1 /* tr -- a filter to translate characters
2 Copyright (C) 91, 1995-2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Jim Meyering */
24 #include <sys/types.h>
30 #include "safe-read.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "tr"
36 #define AUTHORS "Jim Meyering"
38 enum { N_CHARS = UCHAR_MAX + 1 };
40 /* An unsigned integer type big enough to hold a repeat count or an
41 unsigned character. POSIX requires support for repeat counts as
42 high as 2**31 - 1. Since repeat counts might need to expand to
43 match the length of an argument string, we need at least size_t to
44 avoid arbitrary internal limits. It doesn't cost much to use
46 typedef uintmax_t count;
48 /* The value for Spec_list->state that indicates to
49 get_next that it should initialize the tail pointer.
50 Its value should be as large as possible to avoid conflict
51 a valid value for the state field -- and that may be as
52 large as any valid repeat_count. */
53 #define BEGIN_STATE (UINTMAX_MAX - 1)
55 /* The value for Spec_list->state that indicates to
56 get_next that the element pointed to by Spec_list->tail is
57 being considered for the first time on this pass through the
58 list -- it indicates that get_next should make any necessary
60 #define NEW_ELEMENT (BEGIN_STATE + 1)
62 /* The maximum possible repeat count. Due to how the states are
63 implemented, it can be as much as BEGIN_STATE. */
64 #define REPEAT_COUNT_MAXIMUM BEGIN_STATE
66 /* The following (but not CC_NO_CLASS) are indices into the array of
67 valid character class strings. */
70 CC_ALNUM = 0, CC_ALPHA = 1, CC_BLANK = 2, CC_CNTRL = 3,
71 CC_DIGIT = 4, CC_GRAPH = 5, CC_LOWER = 6, CC_PRINT = 7,
72 CC_PUNCT = 8, CC_SPACE = 9, CC_UPPER = 10, CC_XDIGIT = 11,
76 /* Character class to which a character (returned by get_next) belonged;
77 but it is set only if the construct from which the character was obtained
78 was one of the character classes [:upper:] or [:lower:]. The value
79 is used only when translating and then, only to make sure that upper
80 and lower class constructs have the same relative positions in string1
82 enum Upper_Lower_class
89 /* The type of a List_element. See build_spec_list for more details. */
90 enum Range_element_type
99 /* One construct in one of tr's argument strings.
100 For example, consider the POSIX version of the classic tr command:
101 tr -cs 'a-zA-Z_' '[\n*]'
102 String1 has 3 constructs, two of which are ranges (a-z and A-Z),
103 and a single normal character, `_'. String2 has one construct. */
106 enum Range_element_type type;
107 struct List_element *next;
110 unsigned char normal_char;
113 unsigned char first_char;
114 unsigned char last_char;
117 enum Char_class char_class;
118 unsigned char equiv_code;
121 unsigned char the_repeated_char;
129 /* Each of tr's argument strings is parsed into a form that is easier
130 to work with: a linked list of constructs (struct List_element).
131 Each Spec_list structure also encapsulates various attributes of
132 the corresponding argument string. The attributes are used mainly
133 to verify that the strings are valid in the context of any options
134 specified (like -s, -d, or -c). The main exception is the member
135 `tail', which is first used to construct the list. After construction,
136 it is used by get_next to save its state when traversing the list.
137 The member `state' serves a similar function. */
140 /* Points to the head of the list of range elements.
141 The first struct is a dummy; its members are never used. */
142 struct List_element *head;
144 /* When appending, points to the last element. When traversing via
145 get_next(), points to the element to process next. Setting
146 Spec_list.state to the value BEGIN_STATE before calling get_next
147 signals get_next to initialize tail to point to head->next. */
148 struct List_element *tail;
150 /* Used to save state between calls to get_next. */
153 /* Length, in the sense that length ('a-z[:digit:]123abc')
154 is 42 ( = 26 + 10 + 6). */
157 /* The number of [c*] and [c*0] constructs that appear in this spec. */
158 size_t n_indefinite_repeats;
160 /* If n_indefinite_repeats is nonzero, this points to the List_element
161 corresponding to the last [c*] or [c*0] construct encountered in
162 this spec. Otherwise it is undefined. */
163 struct List_element *indefinite_repeat_element;
165 /* True if this spec contains at least one equivalence
166 class construct e.g. [=c=]. */
167 bool has_equiv_class;
169 /* True if this spec contains at least one character class
170 construct. E.g. [:digit:]. */
173 /* True if this spec contains at least one of the character class
174 constructs (all but upper and lower) that aren't allowed in s2. */
175 bool has_restricted_char_class;
178 /* A representation for escaped string1 or string2. As a string is parsed,
179 any backslash-escaped characters (other than octal or \a, \b, \f, \n,
180 etc.) are marked as such in this structure by setting the corresponding
181 entry in the ESCAPED vector. */
189 /* Return nonzero if the Ith character of escaped string ES matches C
190 and is not escaped itself. */
192 es_match (struct E_string const *es, size_t i, char c)
194 return es->s[i] == c && !es->escaped[i];
197 /* The name by which this program was run. */
200 /* When true, each sequence in the input of a repeated character
201 (call it c) is replaced (in the output) by a single occurrence of c
202 for every c in the squeeze set. */
203 static bool squeeze_repeats = false;
205 /* When true, removes characters in the delete set from input. */
206 static bool delete = false;
208 /* Use the complement of set1 in place of set1. */
209 static bool complement = false;
211 /* When tr is performing translation and string1 is longer than string2,
212 POSIX says that the result is unspecified. That gives the implementor
213 of a POSIX conforming version of tr two reasonable choices for the
214 semantics of this case.
216 * The BSD tr pads string2 to the length of string1 by
217 repeating the last character in string2.
219 * System V tr ignores characters in string1 that have no
220 corresponding character in string2. That is, string1 is effectively
221 truncated to the length of string2.
223 When nonzero, this flag causes GNU tr to imitate the behavior
224 of System V tr when translating with string1 longer than string2.
225 The default is to emulate BSD tr. This flag is ignored in modes where
226 no translation is performed. Emulating the System V tr
227 in this exceptional case causes the relatively common BSD idiom:
229 tr -cs A-Za-z0-9 '\012'
231 to break (it would convert only zero bytes, rather than all
232 non-alphanumerics, to newlines).
234 WARNING: This switch does not provide general BSD or System V
235 compatibility. For example, it doesn't disable the interpretation
236 of the POSIX constructs [:alpha:], [=c=], and [c*10], so if by
237 some unfortunate coincidence you use such constructs in scripts
238 expecting to use some other version of tr, the scripts will break. */
239 static bool truncate_set1 = false;
241 /* An alias for (!delete && non_option_args == 2).
242 It is set in main and used there and in validate(). */
243 static bool translating;
245 static char io_buf[BUFSIZ];
247 static char const *const char_class_name[] =
249 "alnum", "alpha", "blank", "cntrl", "digit", "graph",
250 "lower", "print", "punct", "space", "upper", "xdigit"
252 enum { N_CHAR_CLASSES = sizeof char_class_name / sizeof char_class_name[0] };
254 /* Array of boolean values. A character `c' is a member of the
255 squeeze set if and only if in_squeeze_set[c] is true. The squeeze
256 set is defined by the last (possibly, the only) string argument
257 on the command line when the squeeze option is given. */
258 static bool in_squeeze_set[N_CHARS];
260 /* Array of boolean values. A character `c' is a member of the
261 delete set if and only if in_delete_set[c] is true. The delete
262 set is defined by the first (or only) string argument on the
263 command line when the delete option is given. */
264 static bool in_delete_set[N_CHARS];
266 /* Array of character values defining the translation (if any) that
267 tr is to perform. Translation is performed only when there are
268 two specification strings and the delete switch is not given. */
269 static char xlate[N_CHARS];
271 static struct option const long_options[] =
273 {"complement", no_argument, NULL, 'c'},
274 {"delete", no_argument, NULL, 'd'},
275 {"squeeze-repeats", no_argument, NULL, 's'},
276 {"truncate-set1", no_argument, NULL, 't'},
277 {GETOPT_HELP_OPTION_DECL},
278 {GETOPT_VERSION_OPTION_DECL},
285 if (status != EXIT_SUCCESS)
286 fprintf (stderr, _("Try `%s --help' for more information.\n"),
291 Usage: %s [OPTION]... SET1 [SET2]\n\
295 Translate, squeeze, and/or delete characters from standard input,\n\
296 writing to standard output.\n\
298 -c, -C, --complement first complement SET1\n\
299 -d, --delete delete characters in SET1, do not translate\n\
300 -s, --squeeze-repeats replace each input sequence of a repeated character\n\
301 that is listed in SET1 with a single occurrence\n\
303 -t, --truncate-set1 first truncate SET1 to length of SET2\n\
305 fputs (HELP_OPTION_DESCRIPTION, stdout);
306 fputs (VERSION_OPTION_DESCRIPTION, stdout);
309 SETs are specified as strings of characters. Most represent themselves.\n\
310 Interpreted sequences are:\n\
312 \\NNN character with octal value NNN (1 to 3 octal digits)\n\
319 \\t horizontal tab\n\
323 CHAR1-CHAR2 all characters from CHAR1 to CHAR2 in ascending order\n\
324 [CHAR*] in SET2, copies of CHAR until length of SET1\n\
325 [CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0\n\
326 [:alnum:] all letters and digits\n\
327 [:alpha:] all letters\n\
328 [:blank:] all horizontal whitespace\n\
329 [:cntrl:] all control characters\n\
330 [:digit:] all digits\n\
333 [:graph:] all printable characters, not including space\n\
334 [:lower:] all lower case letters\n\
335 [:print:] all printable characters, including space\n\
336 [:punct:] all punctuation characters\n\
337 [:space:] all horizontal or vertical whitespace\n\
338 [:upper:] all upper case letters\n\
339 [:xdigit:] all hexadecimal digits\n\
340 [=CHAR=] all characters which are equivalent to CHAR\n\
344 Translation occurs if -d is not given and both SET1 and SET2 appear.\n\
345 -t may be used only when translating. SET2 is extended to length of\n\
346 SET1 by repeating its last character as necessary. \
350 of SET2 are ignored. Only [:lower:] and [:upper:] are guaranteed to\n\
351 expand in ascending order; used in SET2 while translating, they may\n\
352 only be used in pairs to specify case conversion. \
355 -s uses SET1 if not\n\
356 translating nor deleting; else squeezing uses SET2 and occurs after\n\
357 translation or deletion.\n\
359 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
364 /* Return nonzero if the character C is a member of the
365 equivalence class containing the character EQUIV_CLASS. */
368 is_equiv_class_member (unsigned char equiv_class, unsigned char c)
370 return (equiv_class == c);
373 /* Return true if the character C is a member of the
374 character class CHAR_CLASS. */
377 is_char_class_member (enum Char_class char_class, unsigned char c)
384 result = isalnum (c);
387 result = isalpha (c);
390 result = isblank (c);
393 result = iscntrl (c);
396 result = isdigit (c);
399 result = isgraph (c);
402 result = islower (c);
405 result = isprint (c);
408 result = ispunct (c);
411 result = isspace (c);
414 result = isupper (c);
417 result = isxdigit (c);
428 es_free (struct E_string *es)
434 /* Perform the first pass over each range-spec argument S, converting all
435 \c and \ddd escapes to their one-byte representations. If an invalid
436 quote sequence is found print an error message and return false;
437 Otherwise set *ES to the resulting string and return true.
438 The resulting array of characters may contain zero-bytes;
439 however, on input, S is assumed to be null-terminated, and hence
440 cannot contain actual (non-escaped) zero bytes. */
443 unquote (char const *s, struct E_string *es)
446 size_t len = strlen (s);
448 es->s = xmalloc (len);
449 es->escaped = xcalloc (len, sizeof es->escaped[0]);
452 for (i = 0; s[i]; i++)
460 es->escaped[j] = true;
496 oct_digit = s[i + 2] - '0';
497 if (0 <= oct_digit && oct_digit <= 7)
499 c = 8 * c + oct_digit;
501 oct_digit = s[i + 2] - '0';
502 if (0 <= oct_digit && oct_digit <= 7)
504 if (8 * c + oct_digit < N_CHARS)
506 c = 8 * c + oct_digit;
511 /* A 3-digit octal number larger than \377 won't
512 fit in 8 bits. So we stop when adding the
513 next digit would put us over the limit and
514 give a warning about the ambiguity. POSIX
515 isn't clear on this, and we interpret this
516 lack of clarity as meaning the resulting behavior
517 is undefined, which means we're allowed to issue
519 error (0, 0, _("warning: the ambiguous octal escape \
520 \\%c%c%c is being\n\tinterpreted as the 2-byte sequence \\0%c%c, %c"),
521 s[i], s[i + 1], s[i + 2],
522 s[i], s[i + 1], s[i + 2]);
528 /* POSIX seems to require that a trailing backslash must
529 stand for itself. Weird. */
530 es->escaped[j] = false;
550 /* If CLASS_STR is a valid character class string, return its index
551 in the global char_class_name array. Otherwise, return CC_NO_CLASS. */
553 static enum Char_class
554 look_up_char_class (char const *class_str, size_t len)
558 for (i = 0; i < N_CHAR_CLASSES; i++)
559 if (strncmp (class_str, char_class_name[i], len) == 0
560 && strlen (char_class_name[i]) == len)
565 /* Return a newly allocated string with a printable version of C.
566 This function is used solely for formatting error messages. */
569 make_printable_char (unsigned char c)
571 char *buf = xmalloc (5);
580 sprintf (buf, "\\%03o", c);
585 /* Return a newly allocated copy of S which is suitable for printing.
586 LEN is the number of characters in S. Most non-printing
587 (isprint) characters are represented by a backslash followed by
588 3 octal digits. However, the characters represented by \c escapes
589 where c is one of [abfnrtv] are represented by their 2-character \c
590 sequences. This function is used solely for printing error messages. */
593 make_printable_str (char const *s, size_t len)
595 /* Worst case is that every character expands to a backslash
596 followed by a 3-character octal escape sequence. */
597 char *printable_buf = xnmalloc (len + 1, 4);
598 char *p = printable_buf;
601 for (i = 0; i < len; i++)
605 unsigned char c = s[i];
640 sprintf (buf, "\\%03o", c);
646 return printable_buf;
649 /* Append a newly allocated structure representing a
650 character C to the specification list LIST. */
653 append_normal_char (struct Spec_list *list, unsigned char c)
655 struct List_element *new;
657 new = xmalloc (sizeof *new);
659 new->type = RE_NORMAL_CHAR;
660 new->u.normal_char = c;
662 list->tail->next = new;
666 /* Append a newly allocated structure representing the range
667 of characters from FIRST to LAST to the specification list LIST.
668 Return false if LAST precedes FIRST in the collating sequence,
669 true otherwise. This means that '[c-c]' is acceptable. */
672 append_range (struct Spec_list *list, unsigned char first, unsigned char last)
674 struct List_element *new;
678 char *tmp1 = make_printable_char (first);
679 char *tmp2 = make_printable_char (last);
682 _("range-endpoints of `%s-%s' are in reverse collating sequence order"),
688 new = xmalloc (sizeof *new);
690 new->type = RE_RANGE;
691 new->u.range.first_char = first;
692 new->u.range.last_char = last;
694 list->tail->next = new;
699 /* If CHAR_CLASS_STR is a valid character class string, append a
700 newly allocated structure representing that character class to the end
701 of the specification list LIST and return true. If CHAR_CLASS_STR is not
702 a valid string return false. */
705 append_char_class (struct Spec_list *list,
706 char const *char_class_str, size_t len)
708 enum Char_class char_class;
709 struct List_element *new;
711 char_class = look_up_char_class (char_class_str, len);
712 if (char_class == CC_NO_CLASS)
714 new = xmalloc (sizeof *new);
716 new->type = RE_CHAR_CLASS;
717 new->u.char_class = char_class;
719 list->tail->next = new;
724 /* Append a newly allocated structure representing a [c*n]
725 repeated character construct to the specification list LIST.
726 THE_CHAR is the single character to be repeated, and REPEAT_COUNT
727 is a non-negative repeat count. */
730 append_repeated_char (struct Spec_list *list, unsigned char the_char,
733 struct List_element *new;
735 new = xmalloc (sizeof *new);
737 new->type = RE_REPEATED_CHAR;
738 new->u.repeated_char.the_repeated_char = the_char;
739 new->u.repeated_char.repeat_count = repeat_count;
741 list->tail->next = new;
745 /* Given a string, EQUIV_CLASS_STR, from a [=str=] context and
746 the length of that string, LEN, if LEN is exactly one, append
747 a newly allocated structure representing the specified
748 equivalence class to the specification list, LIST and return true.
749 If LEN is not 1, return false. */
752 append_equiv_class (struct Spec_list *list,
753 char const *equiv_class_str, size_t len)
755 struct List_element *new;
759 new = xmalloc (sizeof *new);
761 new->type = RE_EQUIV_CLASS;
762 new->u.equiv_code = *equiv_class_str;
764 list->tail->next = new;
769 /* Search forward starting at START_IDX for the 2-char sequence
770 (PRE_BRACKET_CHAR,']') in the string P of length P_LEN. If such
771 a sequence is found, set *RESULT_IDX to the index of the first
772 character and return true. Otherwise return false. P may contain
776 find_closing_delim (const struct E_string *es, size_t start_idx,
777 char pre_bracket_char, size_t *result_idx)
781 for (i = start_idx; i < es->len - 1; i++)
782 if (es->s[i] == pre_bracket_char && es->s[i + 1] == ']'
783 && !es->escaped[i] && !es->escaped[i + 1])
791 /* Parse the bracketed repeat-char syntax. If the P_LEN characters
792 beginning with P[ START_IDX ] comprise a valid [c*n] construct,
793 then set *CHAR_TO_REPEAT, *REPEAT_COUNT, and *CLOSING_BRACKET_IDX
794 and return zero. If the second character following
795 the opening bracket is not `*' or if no closing bracket can be
796 found, return -1. If a closing bracket is found and the
797 second char is `*', but the string between the `*' and `]' isn't
798 empty, an octal number, or a decimal number, print an error message
802 find_bracketed_repeat (const struct E_string *es, size_t start_idx,
803 unsigned char *char_to_repeat, count *repeat_count,
804 size_t *closing_bracket_idx)
808 assert (start_idx + 1 < es->len);
809 if (!es_match (es, start_idx + 1, '*'))
812 for (i = start_idx + 2; i < es->len && !es->escaped[i]; i++)
816 size_t digit_str_len = i - start_idx - 2;
818 *char_to_repeat = es->s[start_idx];
819 if (digit_str_len == 0)
821 /* We've matched [c*] -- no explicit repeat count. */
826 /* Here, we have found [c*s] where s should be a string
827 of octal (if it starts with `0') or decimal digits. */
828 char const *digit_str = &es->s[start_idx + 2];
830 if ((xstrtoumax (digit_str, &d_end, *digit_str == '0' ? 8 : 10,
833 || REPEAT_COUNT_MAXIMUM < *repeat_count
834 || digit_str + digit_str_len != d_end)
836 char *tmp = make_printable_str (digit_str, digit_str_len);
838 _("invalid repeat count %s in [c*n] construct"),
844 *closing_bracket_idx = i;
848 return -1; /* No bracket found. */
851 /* Return true if the string at ES->s[IDX] matches the regular
852 expression `\*[0-9]*\]', false otherwise. The string does not
853 match if any of its characters are escaped. */
856 star_digits_closebracket (const struct E_string *es, size_t idx)
860 if (!es_match (es, idx, '*'))
863 for (i = idx + 1; i < es->len; i++)
864 if (!ISDIGIT (to_uchar (es->s[i])) || es->escaped[i])
865 return es_match (es, i, ']');
869 /* Convert string UNESCAPED_STRING (which has been preprocessed to
870 convert backslash-escape sequences) of length LEN characters into
871 a linked list of the following 5 types of constructs:
872 - [:str:] Character class where `str' is one of the 12 valid strings.
873 - [=c=] Equivalence class where `c' is any single character.
874 - [c*n] Repeat the single character `c' `n' times. n may be omitted.
875 However, if `n' is present, it must be a non-negative octal or
877 - r-s Range of characters from `r' to `s'. The second endpoint must
878 not precede the first in the current collating sequence.
879 - c Any other character is interpreted as itself. */
882 build_spec_list (const struct E_string *es, struct Spec_list *result)
889 /* The main for-loop below recognizes the 4 multi-character constructs.
890 A character that matches (in its context) none of the multi-character
891 constructs is classified as `normal'. Since all multi-character
892 constructs have at least 3 characters, any strings of length 2 or
893 less are composed solely of normal characters. Hence, the index of
894 the outer for-loop runs only as far as LEN-2. */
896 for (i = 0; i + 2 < es->len; /* empty */)
898 if (es_match (es, i, '['))
900 bool matched_multi_char_construct;
901 size_t closing_bracket_idx;
902 unsigned char char_to_repeat;
906 matched_multi_char_construct = true;
907 if (es_match (es, i + 1, ':') || es_match (es, i + 1, '='))
909 size_t closing_delim_idx;
911 if (find_closing_delim (es, i + 2, p[i + 1], &closing_delim_idx))
913 size_t opnd_str_len = closing_delim_idx - 1 - (i + 2) + 1;
914 char const *opnd_str = p + i + 2;
916 if (opnd_str_len == 0)
919 error (0, 0, _("missing character class name `[::]'"));
922 _("missing equivalence class character `[==]'"));
928 /* FIXME: big comment. */
929 if (!append_char_class (result, opnd_str, opnd_str_len))
931 if (star_digits_closebracket (es, i + 2))
932 goto try_bracketed_repeat;
935 char *tmp = make_printable_str (opnd_str,
937 error (0, 0, _("invalid character class %s"),
946 /* FIXME: big comment. */
947 if (!append_equiv_class (result, opnd_str, opnd_str_len))
949 if (star_digits_closebracket (es, i + 2))
950 goto try_bracketed_repeat;
953 char *tmp = make_printable_str (opnd_str,
956 _("%s: equivalence class operand must be a single character"),
964 i = closing_delim_idx + 2;
967 /* Else fall through. This could be [:*] or [=*]. */
970 try_bracketed_repeat:
972 /* Determine whether this is a bracketed repeat range
973 matching the RE \[.\*(dec_or_oct_number)?\]. */
974 err = find_bracketed_repeat (es, i + 1, &char_to_repeat,
976 &closing_bracket_idx);
979 append_repeated_char (result, char_to_repeat, repeat_count);
980 i = closing_bracket_idx + 1;
984 matched_multi_char_construct = false;
988 /* Found a string that looked like [c*n] but the
989 numeric part was invalid. */
993 if (matched_multi_char_construct)
996 /* We reach this point if P does not match [:str:], [=c=],
997 [c*n], or [c*]. Now, see if P looks like a range `[-c'
998 (from `[' to `c'). */
1001 /* Look ahead one char for ranges like a-z. */
1002 if (es_match (es, i + 1, '-'))
1004 if (!append_range (result, p[i], p[i + 2]))
1010 append_normal_char (result, p[i]);
1015 /* Now handle the (2 or fewer) remaining characters p[i]..p[es->len - 1]. */
1016 for (; i < es->len; i++)
1017 append_normal_char (result, p[i]);
1022 /* Given a Spec_list S (with its saved state implicit in the values
1023 of its members `tail' and `state'), return the next single character
1024 in the expansion of S's constructs. If the last character of S was
1025 returned on the previous call or if S was empty, this function
1026 returns -1. For example, successive calls to get_next where S
1027 represents the spec-string 'a-d[y*3]' will return the sequence
1028 of values a, b, c, d, y, y, y, -1. Finally, if the construct from
1029 which the returned character comes is [:upper:] or [:lower:], the
1030 parameter CLASS is given a value to indicate which it was. Otherwise
1031 CLASS is set to UL_NONE. This value is used only when constructing
1032 the translation table to verify that any occurrences of upper and
1033 lower class constructs in the spec-strings appear in the same relative
1037 get_next (struct Spec_list *s, enum Upper_Lower_class *class)
1039 struct List_element *p;
1046 if (s->state == BEGIN_STATE)
1048 s->tail = s->head->next;
1049 s->state = NEW_ELEMENT;
1058 case RE_NORMAL_CHAR:
1059 return_val = p->u.normal_char;
1060 s->state = NEW_ELEMENT;
1065 if (s->state == NEW_ELEMENT)
1066 s->state = p->u.range.first_char;
1069 return_val = s->state;
1070 if (s->state == p->u.range.last_char)
1073 s->state = NEW_ELEMENT;
1080 bool upper_or_lower;
1081 switch (p->u.char_class)
1085 upper_or_lower = true;
1089 upper_or_lower = true;
1092 upper_or_lower = false;
1099 s->state = NEW_ELEMENT;
1105 if (s->state == NEW_ELEMENT)
1107 for (i = 0; i < N_CHARS; i++)
1108 if (is_char_class_member (p->u.char_class, i))
1110 assert (i < N_CHARS);
1113 assert (is_char_class_member (p->u.char_class, s->state));
1114 return_val = s->state;
1115 for (i = s->state + 1; i < N_CHARS; i++)
1116 if (is_char_class_member (p->u.char_class, i))
1123 s->state = NEW_ELEMENT;
1127 case RE_EQUIV_CLASS:
1128 /* FIXME: this assumes that each character is alone in its own
1129 equivalence class (which appears to be correct for my
1130 LC_COLLATE. But I don't know of any function that allows
1131 one to determine a character's equivalence class. */
1133 return_val = p->u.equiv_code;
1134 s->state = NEW_ELEMENT;
1138 case RE_REPEATED_CHAR:
1139 /* Here, a repeat count of n == 0 means don't repeat at all. */
1140 if (p->u.repeated_char.repeat_count == 0)
1143 s->state = NEW_ELEMENT;
1144 return_val = get_next (s, class);
1148 if (s->state == NEW_ELEMENT)
1153 return_val = p->u.repeated_char.the_repeated_char;
1154 if (s->state == p->u.repeated_char.repeat_count)
1157 s->state = NEW_ELEMENT;
1170 /* This is a minor kludge. This function is called from
1171 get_spec_stats to determine the cardinality of a set derived
1172 from a complemented string. It's a kludge in that some of the
1173 same operations are (duplicated) performed in set_initialize. */
1176 card_of_complement (struct Spec_list *s)
1179 int cardinality = N_CHARS;
1180 bool in_set[N_CHARS] = { 0, };
1182 s->state = BEGIN_STATE;
1183 while ((c = get_next (s, NULL)) != -1)
1185 cardinality -= (!in_set[c]);
1191 /* Gather statistics about the spec-list S in preparation for the tests
1192 in validate that determine the consistency of the specs. This function
1193 is called at most twice; once for string1, and again for any string2.
1194 LEN_S1 < 0 indicates that this is the first call and that S represents
1195 string1. When LEN_S1 >= 0, it is the length of the expansion of the
1196 constructs in string1, and we can use its value to resolve any
1197 indefinite repeat construct in S (which represents string2). Hence,
1198 this function has the side-effect that it converts a valid [c*]
1199 construct in string2 to [c*n] where n is large enough (or 0) to give
1200 string2 the same length as string1. For example, with the command
1201 tr a-z 'A[\n*]Z' on the second call to get_spec_stats, LEN_S1 would
1202 be 26 and S (representing string2) would be converted to 'A[\n*24]Z'. */
1205 get_spec_stats (struct Spec_list *s)
1207 struct List_element *p;
1210 s->n_indefinite_repeats = 0;
1211 s->has_equiv_class = false;
1212 s->has_restricted_char_class = false;
1213 s->has_char_class = false;
1214 for (p = s->head->next; p; p = p->next)
1222 case RE_NORMAL_CHAR:
1227 assert (p->u.range.last_char >= p->u.range.first_char);
1228 len = p->u.range.last_char - p->u.range.first_char + 1;
1232 s->has_char_class = true;
1233 for (i = 0; i < N_CHARS; i++)
1234 if (is_char_class_member (p->u.char_class, i))
1236 switch (p->u.char_class)
1242 s->has_restricted_char_class = true;
1247 case RE_EQUIV_CLASS:
1248 for (i = 0; i < N_CHARS; i++)
1249 if (is_equiv_class_member (p->u.equiv_code, i))
1251 s->has_equiv_class = true;
1254 case RE_REPEATED_CHAR:
1255 if (p->u.repeated_char.repeat_count > 0)
1256 len = p->u.repeated_char.repeat_count;
1259 s->indefinite_repeat_element = p;
1260 ++(s->n_indefinite_repeats);
1269 /* Check for arithmetic overflow in computing length. Also, reject
1270 any length greater than the maximum repeat count, in case the
1271 length is later used to compute the repeat count for an
1272 indefinite element. */
1273 new_length = length + len;
1274 if (! (length <= new_length && new_length <= REPEAT_COUNT_MAXIMUM))
1275 error (EXIT_FAILURE, 0, _("too many characters in set"));
1276 length = new_length;
1283 get_s1_spec_stats (struct Spec_list *s1)
1285 get_spec_stats (s1);
1287 s1->length = card_of_complement (s1);
1291 get_s2_spec_stats (struct Spec_list *s2, count len_s1)
1293 get_spec_stats (s2);
1294 if (len_s1 >= s2->length && s2->n_indefinite_repeats == 1)
1296 s2->indefinite_repeat_element->u.repeated_char.repeat_count =
1297 len_s1 - s2->length;
1298 s2->length = len_s1;
1303 spec_init (struct Spec_list *spec_list)
1305 struct List_element *new = xmalloc (sizeof *new);
1306 spec_list->head = spec_list->tail = new;
1307 spec_list->head->next = NULL;
1310 /* This function makes two passes over the argument string S. The first
1311 one converts all \c and \ddd escapes to their one-byte representations.
1312 The second constructs a linked specification list, SPEC_LIST, of the
1313 characters and constructs that comprise the argument string. If either
1314 of these passes detects an error, this function returns false. */
1317 parse_str (char const *s, struct Spec_list *spec_list)
1320 bool ok = unquote (s, &es) && build_spec_list (&es, spec_list);
1325 /* Given two specification lists, S1 and S2, and assuming that
1326 S1->length > S2->length, append a single [c*n] element to S2 where c
1327 is the last character in the expansion of S2 and n is the difference
1328 between the two lengths.
1329 Upon successful completion, S2->length is set to S1->length. The only
1330 way this function can fail to make S2 as long as S1 is when S2 has
1331 zero-length, since in that case, there is no last character to repeat.
1332 So S2->length is required to be at least 1.
1334 Providing this functionality allows the user to do some pretty
1335 non-BSD (and non-portable) things: For example, the command
1336 tr -cs '[:upper:]0-9' '[:lower:]'
1337 is almost guaranteed to give results that depend on your collating
1341 string2_extend (const struct Spec_list *s1, struct Spec_list *s2)
1343 struct List_element *p;
1344 unsigned char char_to_repeat;
1347 assert (translating);
1348 assert (s1->length > s2->length);
1349 assert (s2->length > 0);
1354 case RE_NORMAL_CHAR:
1355 char_to_repeat = p->u.normal_char;
1358 char_to_repeat = p->u.range.last_char;
1361 for (i = N_CHARS - 1; i >= 0; i--)
1362 if (is_char_class_member (p->u.char_class, i))
1368 case RE_REPEATED_CHAR:
1369 char_to_repeat = p->u.repeated_char.the_repeated_char;
1372 case RE_EQUIV_CLASS:
1373 /* This shouldn't happen, because validate exits with an error
1374 if it finds an equiv class in string2 when translating. */
1383 append_repeated_char (s2, char_to_repeat, s1->length - s2->length);
1384 s2->length = s1->length;
1387 /* Return true if S is a non-empty list in which exactly one
1388 character (but potentially, many instances of it) appears.
1389 E.g., [X*] or xxxxxxxx. */
1392 homogeneous_spec_list (struct Spec_list *s)
1396 s->state = BEGIN_STATE;
1398 if ((b = get_next (s, NULL)) == -1)
1401 while ((c = get_next (s, NULL)) != -1)
1408 /* Die with an error message if S1 and S2 describe strings that
1409 are not valid with the given command line switches.
1410 A side effect of this function is that if a valid [c*] or
1411 [c*0] construct appears in string2, it is converted to [c*n]
1412 with a value for n that makes s2->length == s1->length. By
1413 the same token, if the --truncate-set1 option is not
1414 given, S2 may be extended. */
1417 validate (struct Spec_list *s1, struct Spec_list *s2)
1419 get_s1_spec_stats (s1);
1420 if (s1->n_indefinite_repeats > 0)
1422 error (EXIT_FAILURE, 0,
1423 _("the [c*] repeat construct may not appear in string1"));
1428 get_s2_spec_stats (s2, s1->length);
1430 if (s2->n_indefinite_repeats > 1)
1432 error (EXIT_FAILURE, 0,
1433 _("only one [c*] repeat construct may appear in string2"));
1438 if (s2->has_equiv_class)
1440 error (EXIT_FAILURE, 0,
1441 _("[=c=] expressions may not appear in string2 \
1442 when translating"));
1445 if (s1->length > s2->length)
1449 /* string2 must be non-empty unless --truncate-set1 is
1450 given or string1 is empty. */
1452 if (s2->length == 0)
1453 error (EXIT_FAILURE, 0,
1454 _("when not truncating set1, string2 must be non-empty"));
1455 string2_extend (s1, s2);
1459 if (complement && s1->has_char_class
1460 && ! (s2->length == s1->length && homogeneous_spec_list (s2)))
1462 error (EXIT_FAILURE, 0,
1463 _("when translating with complemented character classes,\
1464 \nstring2 must map all characters in the domain to one"));
1467 if (s2->has_restricted_char_class)
1469 error (EXIT_FAILURE, 0,
1470 _("when translating, the only character classes that may \
1471 appear in\nstring2 are `upper' and `lower'"));
1475 /* Not translating. */
1477 if (s2->n_indefinite_repeats > 0)
1478 error (EXIT_FAILURE, 0,
1479 _("the [c*] construct may appear in string2 only \
1480 when translating"));
1485 /* Read buffers of SIZE bytes via the function READER (if READER is
1486 NULL, read from stdin) until EOF. When non-NULL, READER is either
1487 read_and_delete or read_and_xlate. After each buffer is read, it is
1488 processed and written to stdout. The buffers are processed so that
1489 multiple consecutive occurrences of the same character in the input
1490 stream are replaced by a single occurrence of that character if the
1491 character is in the squeeze set. */
1494 squeeze_filter (char *buf, size_t size, size_t (*reader) (char *, size_t))
1496 /* A value distinct from any character that may have been stored in a
1497 buffer as the result of a block-read in the function squeeze_filter. */
1498 enum { NOT_A_CHAR = CHAR_MAX + 1 };
1500 int char_to_squeeze = NOT_A_CHAR;
1510 nr = reader (buf, size);
1518 if (char_to_squeeze == NOT_A_CHAR)
1521 /* Here, by being a little tricky, we can get a significant
1522 performance increase in most cases when the input is
1523 reasonably large. Since tr will modify the input only
1524 if two consecutive (and identical) input characters are
1525 in the squeeze set, we can step by two through the data
1526 when searching for a character in the squeeze set. This
1527 means there may be a little more work in a few cases and
1528 perhaps twice as much work in the worst cases where most
1529 of the input is removed by squeezing repeats. But most
1530 uses of this functionality seem to remove less than 20-30%
1532 for (; i < nr && !in_squeeze_set[to_uchar (buf[i])]; i += 2)
1535 /* There is a special case when i == nr and we've just
1536 skipped a character (the last one in buf) that is in
1538 if (i == nr && in_squeeze_set[to_uchar (buf[i - 1])])
1542 out_len = nr - begin;
1545 char_to_squeeze = buf[i];
1546 /* We're about to output buf[begin..i]. */
1547 out_len = i - begin + 1;
1549 /* But since we stepped by 2 in the loop above,
1550 out_len may be one too large. */
1551 if (i > 0 && buf[i - 1] == char_to_squeeze)
1554 /* Advance i to the index of first character to be
1555 considered when looking for a char different from
1560 && fwrite (&buf[begin], 1, out_len, stdout) != out_len)
1561 error (EXIT_FAILURE, errno, _("write error"));
1564 if (char_to_squeeze != NOT_A_CHAR)
1566 /* Advance i to index of first char != char_to_squeeze
1567 (or to nr if all the rest of the characters in this
1568 buffer are the same as char_to_squeeze). */
1569 for (; i < nr && buf[i] == char_to_squeeze; i++)
1572 char_to_squeeze = NOT_A_CHAR;
1573 /* If (i >= nr) we've squeezed the last character in this buffer.
1574 So now we have to read a new buffer and continue comparing
1575 characters against char_to_squeeze. */
1581 plain_read (char *buf, size_t size)
1583 size_t nr = safe_read (STDIN_FILENO, buf, size);
1584 if (nr == SAFE_READ_ERROR)
1585 error (EXIT_FAILURE, errno, _("read error"));
1589 /* Read buffers of SIZE bytes from stdin until one is found that
1590 contains at least one character not in the delete set. Store
1591 in the array BUF, all characters from that buffer that are not
1592 in the delete set, and return the number of characters saved
1596 read_and_delete (char *buf, size_t size)
1600 /* This enclosing do-while loop is to make sure that
1601 we don't return zero (indicating EOF) when we've
1602 just deleted all the characters in a buffer. */
1606 size_t nr = plain_read (buf, size);
1611 /* This first loop may be a waste of code, but gives much
1612 better performance when no characters are deleted in
1613 the beginning of a buffer. It just avoids the copying
1614 of buf[i] into buf[n_saved] when it would be a NOP. */
1616 for (i = 0; i < nr && !in_delete_set[to_uchar (buf[i])]; i++)
1620 for (++i; i < nr; i++)
1621 if (!in_delete_set[to_uchar (buf[i])])
1622 buf[n_saved++] = buf[i];
1624 while (n_saved == 0);
1629 /* Read at most SIZE bytes from stdin into the array BUF. Then
1630 perform the in-place and one-to-one mapping specified by the global
1631 array `xlate'. Return the number of characters read, or 0 upon EOF. */
1634 read_and_xlate (char *buf, size_t size)
1636 size_t bytes_read = plain_read (buf, size);
1639 for (i = 0; i < bytes_read; i++)
1640 buf[i] = xlate[to_uchar (buf[i])];
1645 /* Initialize a boolean membership set, IN_SET, with the character
1646 values obtained by traversing the linked list of constructs S
1647 using the function `get_next'. IN_SET is expected to have been
1648 initialized to all zeros by the caller. If COMPLEMENT_THIS_SET
1649 is true the resulting set is complemented. */
1652 set_initialize (struct Spec_list *s, bool complement_this_set, bool *in_set)
1657 s->state = BEGIN_STATE;
1658 while ((c = get_next (s, NULL)) != -1)
1660 if (complement_this_set)
1661 for (i = 0; i < N_CHARS; i++)
1662 in_set[i] = (!in_set[i]);
1666 main (int argc, char **argv)
1669 int non_option_args;
1672 struct Spec_list buf1, buf2;
1673 struct Spec_list *s1 = &buf1;
1674 struct Spec_list *s2 = &buf2;
1676 initialize_main (&argc, &argv);
1677 program_name = argv[0];
1678 setlocale (LC_ALL, "");
1679 bindtextdomain (PACKAGE, LOCALEDIR);
1680 textdomain (PACKAGE);
1682 atexit (close_stdout);
1684 while ((c = getopt_long (argc, argv, "+cCdst", long_options, NULL)) != -1)
1698 squeeze_repeats = true;
1702 truncate_set1 = true;
1705 case_GETOPT_HELP_CHAR;
1707 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1710 usage (EXIT_FAILURE);
1715 non_option_args = argc - optind;
1716 translating = (non_option_args == 2 && !delete);
1717 min_operands = 1 + (delete == squeeze_repeats);
1718 max_operands = 1 + (delete <= squeeze_repeats);
1720 if (non_option_args < min_operands)
1722 if (non_option_args == 0)
1723 error (0, 0, _("missing operand"));
1726 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
1727 fprintf (stderr, "%s\n",
1729 ? ("Two strings must be given when "
1730 "both deleting and squeezing repeats.")
1731 : "Two strings must be given when translating."));
1733 usage (EXIT_FAILURE);
1736 if (max_operands < non_option_args)
1738 error (0, 0, _("extra operand %s"), quote (argv[optind + max_operands]));
1739 if (non_option_args == 2)
1740 fprintf (stderr, "%s\n",
1741 _("Only one string may be given when "
1742 "deleting without squeezing repeats."));
1743 usage (EXIT_FAILURE);
1747 if (!parse_str (argv[optind], s1))
1748 exit (EXIT_FAILURE);
1750 if (non_option_args == 2)
1753 if (!parse_str (argv[optind + 1], s2))
1754 exit (EXIT_FAILURE);
1761 /* Use binary I/O, since `tr' is sometimes used to transliterate
1762 non-printable characters, or characters which are stripped away
1763 by text-mode reads (like CR and ^Z). */
1764 if (O_BINARY && ! isatty (STDIN_FILENO))
1765 freopen (NULL, "rb", stdin);
1766 if (O_BINARY && ! isatty (STDOUT_FILENO))
1767 freopen (NULL, "wb", stdout);
1769 if (squeeze_repeats && non_option_args == 1)
1771 set_initialize (s1, complement, in_squeeze_set);
1772 squeeze_filter (io_buf, sizeof io_buf, plain_read);
1774 else if (delete && non_option_args == 1)
1776 set_initialize (s1, complement, in_delete_set);
1780 size_t nr = read_and_delete (io_buf, sizeof io_buf);
1783 if (fwrite (io_buf, 1, nr, stdout) != nr)
1784 error (EXIT_FAILURE, errno, _("write error"));
1787 else if (squeeze_repeats && delete && non_option_args == 2)
1789 set_initialize (s1, complement, in_delete_set);
1790 set_initialize (s2, false, in_squeeze_set);
1791 squeeze_filter (io_buf, sizeof io_buf, read_and_delete);
1793 else if (translating)
1798 bool *in_s1 = in_delete_set;
1800 set_initialize (s1, false, in_s1);
1801 s2->state = BEGIN_STATE;
1802 for (i = 0; i < N_CHARS; i++)
1804 for (i = 0; i < N_CHARS; i++)
1808 int ch = get_next (s2, NULL);
1809 assert (ch != -1 || truncate_set1);
1812 /* This will happen when tr is invoked like e.g.
1813 tr -cs A-Za-z0-9 '\012'. */
1819 assert (get_next (s2, NULL) == -1 || truncate_set1);
1825 enum Upper_Lower_class class_s1;
1826 enum Upper_Lower_class class_s2;
1828 for (i = 0; i < N_CHARS; i++)
1830 s1->state = BEGIN_STATE;
1831 s2->state = BEGIN_STATE;
1834 c1 = get_next (s1, &class_s1);
1835 c2 = get_next (s2, &class_s2);
1837 /* When constructing the translation array, either one of the
1838 values returned by paired calls to get_next must be from
1839 [:upper:] and the other is [:lower:], or neither can be from
1842 if ((class_s1 == UL_NONE) != (class_s2 == UL_NONE))
1843 error (EXIT_FAILURE, 0,
1844 _("misaligned [:upper:] and/or [:lower:] construct"));
1846 if (class_s1 == UL_LOWER && class_s2 == UL_UPPER)
1848 for (i = 0; i < N_CHARS; i++)
1850 xlate[i] = toupper (i);
1852 else if (class_s1 == UL_UPPER && class_s2 == UL_LOWER)
1854 for (i = 0; i < N_CHARS; i++)
1856 xlate[i] = tolower (i);
1858 else if ((class_s1 == UL_LOWER && class_s2 == UL_LOWER)
1859 || (class_s1 == UL_UPPER && class_s2 == UL_UPPER))
1861 /* POSIX says the behavior of `tr "[:upper:]" "[:upper:]"'
1862 is undefined. Treat it as a no-op. */
1866 /* The following should have been checked by validate... */
1867 if (c1 == -1 || c2 == -1)
1872 assert (c1 == -1 || truncate_set1);
1874 if (squeeze_repeats)
1876 set_initialize (s2, false, in_squeeze_set);
1877 squeeze_filter (io_buf, sizeof io_buf, read_and_xlate);
1883 size_t bytes_read = read_and_xlate (io_buf, sizeof io_buf);
1884 if (bytes_read == 0)
1886 if (fwrite (io_buf, 1, bytes_read, stdout) != bytes_read)
1887 error (EXIT_FAILURE, errno, _("write error"));
1892 if (close (STDIN_FILENO) != 0)
1893 error (EXIT_FAILURE, errno, _("standard input"));
1895 exit (EXIT_SUCCESS);