1 /* tr -- a filter to translate characters
2 Copyright (C) 1991-2017 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 3 of the License, or
7 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
17 /* Written by Jim Meyering */
23 #include <sys/types.h>
31 #include "safe-read.h"
32 #include "xbinary-io.h"
35 /* The official name of this program (e.g., no 'g' prefix). */
36 #define PROGRAM_NAME "tr"
38 #define AUTHORS proper_name ("Jim Meyering")
40 enum { N_CHARS
= UCHAR_MAX
+ 1 };
42 /* An unsigned integer type big enough to hold a repeat count or an
43 unsigned character. POSIX requires support for repeat counts as
44 high as 2**31 - 1. Since repeat counts might need to expand to
45 match the length of an argument string, we need at least size_t to
46 avoid arbitrary internal limits. It doesn't cost much to use
48 typedef uintmax_t count
;
50 /* The value for Spec_list->state that indicates to
51 get_next that it should initialize the tail pointer.
52 Its value should be as large as possible to avoid conflict
53 a valid value for the state field -- and that may be as
54 large as any valid repeat_count. */
55 #define BEGIN_STATE (UINTMAX_MAX - 1)
57 /* The value for Spec_list->state that indicates to
58 get_next that the element pointed to by Spec_list->tail is
59 being considered for the first time on this pass through the
60 list -- it indicates that get_next should make any necessary
62 #define NEW_ELEMENT (BEGIN_STATE + 1)
64 /* The maximum possible repeat count. Due to how the states are
65 implemented, it can be as much as BEGIN_STATE. */
66 #define REPEAT_COUNT_MAXIMUM BEGIN_STATE
68 /* The following (but not CC_NO_CLASS) are indices into the array of
69 valid character class strings. */
72 CC_ALNUM
= 0, CC_ALPHA
= 1, CC_BLANK
= 2, CC_CNTRL
= 3,
73 CC_DIGIT
= 4, CC_GRAPH
= 5, CC_LOWER
= 6, CC_PRINT
= 7,
74 CC_PUNCT
= 8, CC_SPACE
= 9, CC_UPPER
= 10, CC_XDIGIT
= 11,
78 /* Character class to which a character (returned by get_next) belonged;
79 but it is set only if the construct from which the character was obtained
80 was one of the character classes [:upper:] or [:lower:]. The value
81 is used only when translating and then, only to make sure that upper
82 and lower class constructs have the same relative positions in string1
84 enum Upper_Lower_class
91 /* The type of a List_element. See build_spec_list for more details. */
92 enum Range_element_type
101 /* One construct in one of tr's argument strings.
102 For example, consider the POSIX version of the classic tr command:
103 tr -cs 'a-zA-Z_' '[\n*]'
104 String1 has 3 constructs, two of which are ranges (a-z and A-Z),
105 and a single normal character, '_'. String2 has one construct. */
108 enum Range_element_type type
;
109 struct List_element
*next
;
112 unsigned char normal_char
;
115 unsigned char first_char
;
116 unsigned char last_char
;
119 enum Char_class char_class
;
120 unsigned char equiv_code
;
123 unsigned char the_repeated_char
;
131 /* Each of tr's argument strings is parsed into a form that is easier
132 to work with: a linked list of constructs (struct List_element).
133 Each Spec_list structure also encapsulates various attributes of
134 the corresponding argument string. The attributes are used mainly
135 to verify that the strings are valid in the context of any options
136 specified (like -s, -d, or -c). The main exception is the member
137 'tail', which is first used to construct the list. After construction,
138 it is used by get_next to save its state when traversing the list.
139 The member 'state' serves a similar function. */
142 /* Points to the head of the list of range elements.
143 The first struct is a dummy; its members are never used. */
144 struct List_element
*head
;
146 /* When appending, points to the last element. When traversing via
147 get_next(), points to the element to process next. Setting
148 Spec_list.state to the value BEGIN_STATE before calling get_next
149 signals get_next to initialize tail to point to head->next. */
150 struct List_element
*tail
;
152 /* Used to save state between calls to get_next. */
155 /* Length, in the sense that length ('a-z[:digit:]123abc')
156 is 42 ( = 26 + 10 + 6). */
159 /* The number of [c*] and [c*0] constructs that appear in this spec. */
160 size_t n_indefinite_repeats
;
162 /* If n_indefinite_repeats is nonzero, this points to the List_element
163 corresponding to the last [c*] or [c*0] construct encountered in
164 this spec. Otherwise it is undefined. */
165 struct List_element
*indefinite_repeat_element
;
167 /* True if this spec contains at least one equivalence
168 class construct e.g. [=c=]. */
169 bool has_equiv_class
;
171 /* True if this spec contains at least one character class
172 construct. E.g. [:digit:]. */
175 /* True if this spec contains at least one of the character class
176 constructs (all but upper and lower) that aren't allowed in s2. */
177 bool has_restricted_char_class
;
180 /* A representation for escaped string1 or string2. As a string is parsed,
181 any backslash-escaped characters (other than octal or \a, \b, \f, \n,
182 etc.) are marked as such in this structure by setting the corresponding
183 entry in the ESCAPED vector. */
191 /* Return nonzero if the Ith character of escaped string ES matches C
192 and is not escaped itself. */
194 es_match (struct E_string
const *es
, size_t i
, char c
)
196 return es
->s
[i
] == c
&& !es
->escaped
[i
];
199 /* When true, each sequence in the input of a repeated character
200 (call it c) is replaced (in the output) by a single occurrence of c
201 for every c in the squeeze set. */
202 static bool squeeze_repeats
= false;
204 /* When true, removes characters in the delete set from input. */
205 static bool delete = false;
207 /* Use the complement of set1 in place of set1. */
208 static bool complement
= false;
210 /* When tr is performing translation and string1 is longer than string2,
211 POSIX says that the result is unspecified. That gives the implementor
212 of a POSIX conforming version of tr two reasonable choices for the
213 semantics of this case.
215 * The BSD tr pads string2 to the length of string1 by
216 repeating the last character in string2.
218 * System V tr ignores characters in string1 that have no
219 corresponding character in string2. That is, string1 is effectively
220 truncated to the length of string2.
222 When nonzero, this flag causes GNU tr to imitate the behavior
223 of System V tr when translating with string1 longer than string2.
224 The default is to emulate BSD tr. This flag is ignored in modes where
225 no translation is performed. Emulating the System V tr
226 in this exceptional case causes the relatively common BSD idiom:
228 tr -cs A-Za-z0-9 '\012'
230 to break (it would convert only zero bytes, rather than all
231 non-alphanumerics, to newlines).
233 WARNING: This switch does not provide general BSD or System V
234 compatibility. For example, it doesn't disable the interpretation
235 of the POSIX constructs [:alpha:], [=c=], and [c*10], so if by
236 some unfortunate coincidence you use such constructs in scripts
237 expecting to use some other version of tr, the scripts will break. */
238 static bool truncate_set1
= false;
240 /* An alias for (!delete && non_option_args == 2).
241 It is set in main and used there and in validate(). */
242 static bool translating
;
244 static char io_buf
[BUFSIZ
];
246 static char const *const char_class_name
[] =
248 "alnum", "alpha", "blank", "cntrl", "digit", "graph",
249 "lower", "print", "punct", "space", "upper", "xdigit"
252 /* Array of boolean values. A character 'c' is a member of the
253 squeeze set if and only if in_squeeze_set[c] is true. The squeeze
254 set is defined by the last (possibly, the only) string argument
255 on the command line when the squeeze option is given. */
256 static bool in_squeeze_set
[N_CHARS
];
258 /* Array of boolean values. A character 'c' is a member of the
259 delete set if and only if in_delete_set[c] is true. The delete
260 set is defined by the first (or only) string argument on the
261 command line when the delete option is given. */
262 static bool in_delete_set
[N_CHARS
];
264 /* Array of character values defining the translation (if any) that
265 tr is to perform. Translation is performed only when there are
266 two specification strings and the delete switch is not given. */
267 static char xlate
[N_CHARS
];
269 static struct option
const long_options
[] =
271 {"complement", no_argument
, NULL
, 'c'},
272 {"delete", no_argument
, NULL
, 'd'},
273 {"squeeze-repeats", no_argument
, NULL
, 's'},
274 {"truncate-set1", no_argument
, NULL
, 't'},
275 {GETOPT_HELP_OPTION_DECL
},
276 {GETOPT_VERSION_OPTION_DECL
},
283 if (status
!= EXIT_SUCCESS
)
288 Usage: %s [OPTION]... SET1 [SET2]\n\
292 Translate, squeeze, and/or delete characters from standard input,\n\
293 writing to standard output.\n\
295 -c, -C, --complement use the complement of SET1\n\
296 -d, --delete delete characters in SET1, do not translate\n\
297 -s, --squeeze-repeats replace each sequence of a repeated character\n\
298 that is listed in the last specified SET,\n\
299 with a single occurrence of that character\n\
300 -t, --truncate-set1 first truncate SET1 to length of SET2\n\
302 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
303 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
306 SETs are specified as strings of characters. Most represent themselves.\n\
307 Interpreted sequences are:\n\
309 \\NNN character with octal value NNN (1 to 3 octal digits)\n\
316 \\t horizontal tab\n\
320 CHAR1-CHAR2 all characters from CHAR1 to CHAR2 in ascending order\n\
321 [CHAR*] in SET2, copies of CHAR until length of SET1\n\
322 [CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0\n\
323 [:alnum:] all letters and digits\n\
324 [:alpha:] all letters\n\
325 [:blank:] all horizontal whitespace\n\
326 [:cntrl:] all control characters\n\
327 [:digit:] all digits\n\
330 [:graph:] all printable characters, not including space\n\
331 [:lower:] all lower case letters\n\
332 [:print:] all printable characters, including space\n\
333 [:punct:] all punctuation characters\n\
334 [:space:] all horizontal or vertical whitespace\n\
335 [:upper:] all upper case letters\n\
336 [:xdigit:] all hexadecimal digits\n\
337 [=CHAR=] all characters which are equivalent to CHAR\n\
341 Translation occurs if -d is not given and both SET1 and SET2 appear.\n\
342 -t may be used only when translating. SET2 is extended to length of\n\
343 SET1 by repeating its last character as necessary. Excess characters\n\
344 of SET2 are ignored. Only [:lower:] and [:upper:] are guaranteed to\n\
345 expand in ascending order; used in SET2 while translating, they may\n\
346 only be used in pairs to specify case conversion. -s uses the last\n\
347 specified SET, and occurs after translation or deletion.\n\
349 emit_ancillary_info (PROGRAM_NAME
);
354 /* Return nonzero if the character C is a member of the
355 equivalence class containing the character EQUIV_CLASS. */
358 is_equiv_class_member (unsigned char equiv_class
, unsigned char c
)
360 return (equiv_class
== c
);
363 /* Return true if the character C is a member of the
364 character class CHAR_CLASS. */
366 static bool _GL_ATTRIBUTE_PURE
367 is_char_class_member (enum Char_class char_class
, unsigned char c
)
374 result
= isalnum (c
);
377 result
= isalpha (c
);
380 result
= isblank (c
);
383 result
= iscntrl (c
);
386 result
= isdigit (c
);
389 result
= isgraph (c
);
392 result
= islower (c
);
395 result
= isprint (c
);
398 result
= ispunct (c
);
401 result
= isspace (c
);
404 result
= isupper (c
);
407 result
= isxdigit (c
);
417 es_free (struct E_string
*es
)
423 /* Perform the first pass over each range-spec argument S, converting all
424 \c and \ddd escapes to their one-byte representations. If an invalid
425 quote sequence is found print an error message and return false;
426 Otherwise set *ES to the resulting string and return true.
427 The resulting array of characters may contain zero-bytes;
428 however, on input, S is assumed to be null-terminated, and hence
429 cannot contain actual (non-escaped) zero bytes. */
432 unquote (char const *s
, struct E_string
*es
)
434 size_t len
= strlen (s
);
436 es
->s
= xmalloc (len
);
437 es
->escaped
= xcalloc (len
, sizeof es
->escaped
[0]);
440 for (unsigned int i
= 0; s
[i
]; i
++)
448 es
->escaped
[j
] = true;
484 oct_digit
= s
[i
+ 2] - '0';
485 if (0 <= oct_digit
&& oct_digit
<= 7)
487 c
= 8 * c
+ oct_digit
;
489 oct_digit
= s
[i
+ 2] - '0';
490 if (0 <= oct_digit
&& oct_digit
<= 7)
492 if (8 * c
+ oct_digit
< N_CHARS
)
494 c
= 8 * c
+ oct_digit
;
499 /* A 3-digit octal number larger than \377 won't
500 fit in 8 bits. So we stop when adding the
501 next digit would put us over the limit and
502 give a warning about the ambiguity. POSIX
503 isn't clear on this, and we interpret this
504 lack of clarity as meaning the resulting behavior
505 is undefined, which means we're allowed to issue
507 error (0, 0, _("warning: the ambiguous octal escape\
508 \\%c%c%c is being\n\tinterpreted as the 2-byte sequence \\0%c%c, %c"),
509 s
[i
], s
[i
+ 1], s
[i
+ 2],
510 s
[i
], s
[i
+ 1], s
[i
+ 2]);
516 error (0, 0, _("warning: an unescaped backslash "
517 "at end of string is not portable"));
518 /* POSIX is not clear about this. */
519 es
->escaped
[j
] = false;
539 /* If CLASS_STR is a valid character class string, return its index
540 in the global char_class_name array. Otherwise, return CC_NO_CLASS. */
542 static enum Char_class _GL_ATTRIBUTE_PURE
543 look_up_char_class (char const *class_str
, size_t len
)
547 for (i
= 0; i
< ARRAY_CARDINALITY (char_class_name
); i
++)
548 if (STREQ_LEN (class_str
, char_class_name
[i
], len
)
549 && strlen (char_class_name
[i
]) == len
)
554 /* Return a newly allocated string with a printable version of C.
555 This function is used solely for formatting error messages. */
558 make_printable_char (unsigned char c
)
560 char *buf
= xmalloc (5);
569 sprintf (buf
, "\\%03o", c
);
574 /* Return a newly allocated copy of S which is suitable for printing.
575 LEN is the number of characters in S. Most non-printing
576 (isprint) characters are represented by a backslash followed by
577 3 octal digits. However, the characters represented by \c escapes
578 where c is one of [abfnrtv] are represented by their 2-character \c
579 sequences. This function is used solely for printing error messages. */
582 make_printable_str (char const *s
, size_t len
)
584 /* Worst case is that every character expands to a backslash
585 followed by a 3-character octal escape sequence. */
586 char *printable_buf
= xnmalloc (len
+ 1, 4);
587 char *p
= printable_buf
;
589 for (size_t i
= 0; i
< len
; i
++)
592 char const *tmp
= NULL
;
593 unsigned char c
= s
[i
];
628 sprintf (buf
, "\\%03o", c
);
634 return printable_buf
;
637 /* Append a newly allocated structure representing a
638 character C to the specification list LIST. */
641 append_normal_char (struct Spec_list
*list
, unsigned char c
)
643 struct List_element
*new = xmalloc (sizeof *new);
645 new->type
= RE_NORMAL_CHAR
;
646 new->u
.normal_char
= c
;
648 list
->tail
->next
= new;
652 /* Append a newly allocated structure representing the range
653 of characters from FIRST to LAST to the specification list LIST.
654 Return false if LAST precedes FIRST in the collating sequence,
655 true otherwise. This means that '[c-c]' is acceptable. */
658 append_range (struct Spec_list
*list
, unsigned char first
, unsigned char last
)
662 char *tmp1
= make_printable_char (first
);
663 char *tmp2
= make_printable_char (last
);
666 _("range-endpoints of '%s-%s' are in reverse collating sequence order"),
672 struct List_element
*new = xmalloc (sizeof *new);
674 new->type
= RE_RANGE
;
675 new->u
.range
.first_char
= first
;
676 new->u
.range
.last_char
= last
;
678 list
->tail
->next
= new;
683 /* If CHAR_CLASS_STR is a valid character class string, append a
684 newly allocated structure representing that character class to the end
685 of the specification list LIST and return true. If CHAR_CLASS_STR is not
686 a valid string return false. */
689 append_char_class (struct Spec_list
*list
,
690 char const *char_class_str
, size_t len
)
692 enum Char_class char_class
= look_up_char_class (char_class_str
, len
);
693 if (char_class
== CC_NO_CLASS
)
695 struct List_element
*new = xmalloc (sizeof *new);
697 new->type
= RE_CHAR_CLASS
;
698 new->u
.char_class
= char_class
;
700 list
->tail
->next
= new;
705 /* Append a newly allocated structure representing a [c*n]
706 repeated character construct to the specification list LIST.
707 THE_CHAR is the single character to be repeated, and REPEAT_COUNT
708 is a non-negative repeat count. */
711 append_repeated_char (struct Spec_list
*list
, unsigned char the_char
,
714 struct List_element
*new = xmalloc (sizeof *new);
716 new->type
= RE_REPEATED_CHAR
;
717 new->u
.repeated_char
.the_repeated_char
= the_char
;
718 new->u
.repeated_char
.repeat_count
= repeat_count
;
720 list
->tail
->next
= new;
724 /* Given a string, EQUIV_CLASS_STR, from a [=str=] context and
725 the length of that string, LEN, if LEN is exactly one, append
726 a newly allocated structure representing the specified
727 equivalence class to the specification list, LIST and return true.
728 If LEN is not 1, return false. */
731 append_equiv_class (struct Spec_list
*list
,
732 char const *equiv_class_str
, size_t len
)
737 struct List_element
*new = xmalloc (sizeof *new);
739 new->type
= RE_EQUIV_CLASS
;
740 new->u
.equiv_code
= *equiv_class_str
;
742 list
->tail
->next
= new;
747 /* Search forward starting at START_IDX for the 2-char sequence
748 (PRE_BRACKET_CHAR,']') in the string P of length P_LEN. If such
749 a sequence is found, set *RESULT_IDX to the index of the first
750 character and return true. Otherwise return false. P may contain
754 find_closing_delim (const struct E_string
*es
, size_t start_idx
,
755 char pre_bracket_char
, size_t *result_idx
)
757 for (size_t i
= start_idx
; i
< es
->len
- 1; i
++)
758 if (es
->s
[i
] == pre_bracket_char
&& es
->s
[i
+ 1] == ']'
759 && !es
->escaped
[i
] && !es
->escaped
[i
+ 1])
767 /* Parse the bracketed repeat-char syntax. If the P_LEN characters
768 beginning with P[ START_IDX ] comprise a valid [c*n] construct,
769 then set *CHAR_TO_REPEAT, *REPEAT_COUNT, and *CLOSING_BRACKET_IDX
770 and return zero. If the second character following
771 the opening bracket is not '*' or if no closing bracket can be
772 found, return -1. If a closing bracket is found and the
773 second char is '*', but the string between the '*' and ']' isn't
774 empty, an octal number, or a decimal number, print an error message
778 find_bracketed_repeat (const struct E_string
*es
, size_t start_idx
,
779 unsigned char *char_to_repeat
, count
*repeat_count
,
780 size_t *closing_bracket_idx
)
782 assert (start_idx
+ 1 < es
->len
);
783 if (!es_match (es
, start_idx
+ 1, '*'))
786 for (size_t i
= start_idx
+ 2; i
< es
->len
&& !es
->escaped
[i
]; i
++)
790 size_t digit_str_len
= i
- start_idx
- 2;
792 *char_to_repeat
= es
->s
[start_idx
];
793 if (digit_str_len
== 0)
795 /* We've matched [c*] -- no explicit repeat count. */
800 /* Here, we have found [c*s] where s should be a string
801 of octal (if it starts with '0') or decimal digits. */
802 char const *digit_str
= &es
->s
[start_idx
+ 2];
804 if ((xstrtoumax (digit_str
, &d_end
, *digit_str
== '0' ? 8 : 10,
807 || REPEAT_COUNT_MAXIMUM
< *repeat_count
808 || digit_str
+ digit_str_len
!= d_end
)
810 char *tmp
= make_printable_str (digit_str
, digit_str_len
);
812 _("invalid repeat count %s in [c*n] construct"),
818 *closing_bracket_idx
= i
;
822 return -1; /* No bracket found. */
825 /* Return true if the string at ES->s[IDX] matches the regular
826 expression '\*[0-9]*\]', false otherwise. The string does not
827 match if any of its characters are escaped. */
829 static bool _GL_ATTRIBUTE_PURE
830 star_digits_closebracket (const struct E_string
*es
, size_t idx
)
832 if (!es_match (es
, idx
, '*'))
835 for (size_t i
= idx
+ 1; i
< es
->len
; i
++)
836 if (!ISDIGIT (to_uchar (es
->s
[i
])) || es
->escaped
[i
])
837 return es_match (es
, i
, ']');
841 /* Convert string UNESCAPED_STRING (which has been preprocessed to
842 convert backslash-escape sequences) of length LEN characters into
843 a linked list of the following 5 types of constructs:
844 - [:str:] Character class where 'str' is one of the 12 valid strings.
845 - [=c=] Equivalence class where 'c' is any single character.
846 - [c*n] Repeat the single character 'c' 'n' times. n may be omitted.
847 However, if 'n' is present, it must be a non-negative octal or
849 - r-s Range of characters from 'r' to 's'. The second endpoint must
850 not precede the first in the current collating sequence.
851 - c Any other character is interpreted as itself. */
854 build_spec_list (const struct E_string
*es
, struct Spec_list
*result
)
856 char const *p
= es
->s
;
858 /* The main for-loop below recognizes the 4 multi-character constructs.
859 A character that matches (in its context) none of the multi-character
860 constructs is classified as 'normal'. Since all multi-character
861 constructs have at least 3 characters, any strings of length 2 or
862 less are composed solely of normal characters. Hence, the index of
863 the outer for-loop runs only as far as LEN-2. */
865 for (i
= 0; i
+ 2 < es
->len
; /* empty */)
867 if (es_match (es
, i
, '['))
869 bool matched_multi_char_construct
;
870 size_t closing_bracket_idx
;
871 unsigned char char_to_repeat
;
875 matched_multi_char_construct
= true;
876 if (es_match (es
, i
+ 1, ':') || es_match (es
, i
+ 1, '='))
878 size_t closing_delim_idx
;
880 if (find_closing_delim (es
, i
+ 2, p
[i
+ 1], &closing_delim_idx
))
882 size_t opnd_str_len
= closing_delim_idx
- 1 - (i
+ 2) + 1;
883 char const *opnd_str
= p
+ i
+ 2;
885 if (opnd_str_len
== 0)
888 error (0, 0, _("missing character class name '[::]'"));
891 _("missing equivalence class character '[==]'"));
897 /* FIXME: big comment. */
898 if (!append_char_class (result
, opnd_str
, opnd_str_len
))
900 if (star_digits_closebracket (es
, i
+ 2))
901 goto try_bracketed_repeat
;
904 char *tmp
= make_printable_str (opnd_str
,
906 error (0, 0, _("invalid character class %s"),
915 /* FIXME: big comment. */
916 if (!append_equiv_class (result
, opnd_str
, opnd_str_len
))
918 if (star_digits_closebracket (es
, i
+ 2))
919 goto try_bracketed_repeat
;
922 char *tmp
= make_printable_str (opnd_str
,
925 _("%s: equivalence class operand must be a single character"),
933 i
= closing_delim_idx
+ 2;
936 /* Else fall through. This could be [:*] or [=*]. */
939 try_bracketed_repeat
:
941 /* Determine whether this is a bracketed repeat range
942 matching the RE \[.\*(dec_or_oct_number)?\]. */
943 err
= find_bracketed_repeat (es
, i
+ 1, &char_to_repeat
,
945 &closing_bracket_idx
);
948 append_repeated_char (result
, char_to_repeat
, repeat_count
);
949 i
= closing_bracket_idx
+ 1;
953 matched_multi_char_construct
= false;
957 /* Found a string that looked like [c*n] but the
958 numeric part was invalid. */
962 if (matched_multi_char_construct
)
965 /* We reach this point if P does not match [:str:], [=c=],
966 [c*n], or [c*]. Now, see if P looks like a range '[-c'
967 (from '[' to 'c'). */
970 /* Look ahead one char for ranges like a-z. */
971 if (es_match (es
, i
+ 1, '-'))
973 if (!append_range (result
, p
[i
], p
[i
+ 2]))
979 append_normal_char (result
, p
[i
]);
984 /* Now handle the (2 or fewer) remaining characters p[i]..p[es->len - 1]. */
985 for (; i
< es
->len
; i
++)
986 append_normal_char (result
, p
[i
]);
991 /* Advance past the current construct.
992 S->tail must be non-NULL. */
994 skip_construct (struct Spec_list
*s
)
996 s
->tail
= s
->tail
->next
;
997 s
->state
= NEW_ELEMENT
;
1000 /* Given a Spec_list S (with its saved state implicit in the values
1001 of its members 'tail' and 'state'), return the next single character
1002 in the expansion of S's constructs. If the last character of S was
1003 returned on the previous call or if S was empty, this function
1004 returns -1. For example, successive calls to get_next where S
1005 represents the spec-string 'a-d[y*3]' will return the sequence
1006 of values a, b, c, d, y, y, y, -1. Finally, if the construct from
1007 which the returned character comes is [:upper:] or [:lower:], the
1008 parameter CLASS is given a value to indicate which it was. Otherwise
1009 CLASS is set to UL_NONE. This value is used only when constructing
1010 the translation table to verify that any occurrences of upper and
1011 lower class constructs in the spec-strings appear in the same relative
1015 get_next (struct Spec_list
*s
, enum Upper_Lower_class
*class)
1017 struct List_element
*p
;
1024 if (s
->state
== BEGIN_STATE
)
1026 s
->tail
= s
->head
->next
;
1027 s
->state
= NEW_ELEMENT
;
1036 case RE_NORMAL_CHAR
:
1037 return_val
= p
->u
.normal_char
;
1038 s
->state
= NEW_ELEMENT
;
1043 if (s
->state
== NEW_ELEMENT
)
1044 s
->state
= p
->u
.range
.first_char
;
1047 return_val
= s
->state
;
1048 if (s
->state
== p
->u
.range
.last_char
)
1051 s
->state
= NEW_ELEMENT
;
1058 switch (p
->u
.char_class
)
1071 if (s
->state
== NEW_ELEMENT
)
1073 for (i
= 0; i
< N_CHARS
; i
++)
1074 if (is_char_class_member (p
->u
.char_class
, i
))
1076 assert (i
< N_CHARS
);
1079 assert (is_char_class_member (p
->u
.char_class
, s
->state
));
1080 return_val
= s
->state
;
1081 for (i
= s
->state
+ 1; i
< N_CHARS
; i
++)
1082 if (is_char_class_member (p
->u
.char_class
, i
))
1089 s
->state
= NEW_ELEMENT
;
1093 case RE_EQUIV_CLASS
:
1094 /* FIXME: this assumes that each character is alone in its own
1095 equivalence class (which appears to be correct for my
1096 LC_COLLATE. But I don't know of any function that allows
1097 one to determine a character's equivalence class. */
1099 return_val
= p
->u
.equiv_code
;
1100 s
->state
= NEW_ELEMENT
;
1104 case RE_REPEATED_CHAR
:
1105 /* Here, a repeat count of n == 0 means don't repeat at all. */
1106 if (p
->u
.repeated_char
.repeat_count
== 0)
1109 s
->state
= NEW_ELEMENT
;
1110 return_val
= get_next (s
, class);
1114 if (s
->state
== NEW_ELEMENT
)
1119 return_val
= p
->u
.repeated_char
.the_repeated_char
;
1120 if (s
->state
== p
->u
.repeated_char
.repeat_count
)
1123 s
->state
= NEW_ELEMENT
;
1135 /* This is a minor kludge. This function is called from
1136 get_spec_stats to determine the cardinality of a set derived
1137 from a complemented string. It's a kludge in that some of the
1138 same operations are (duplicated) performed in set_initialize. */
1141 card_of_complement (struct Spec_list
*s
)
1144 int cardinality
= N_CHARS
;
1145 bool in_set
[N_CHARS
] = { 0, };
1147 s
->state
= BEGIN_STATE
;
1148 while ((c
= get_next (s
, NULL
)) != -1)
1150 cardinality
-= (!in_set
[c
]);
1156 /* Discard the lengths associated with a case conversion,
1157 as using the actual number of upper or lower case characters
1158 is problematic when they don't match in some locales.
1159 Also ensure the case conversion classes in string2 are
1160 aligned correctly with those in string1.
1161 Note POSIX says the behavior of 'tr "[:upper:]" "[:upper:]"'
1162 is undefined. Therefore we allow it (unlike Solaris)
1163 and treat it as a no-op. */
1166 validate_case_classes (struct Spec_list
*s1
, struct Spec_list
*s2
)
1172 count old_s1_len
= s1
->length
;
1173 count old_s2_len
= s2
->length
;
1174 struct List_element
*s1_tail
= s1
->tail
;
1175 struct List_element
*s2_tail
= s2
->tail
;
1176 bool s1_new_element
= true;
1177 bool s2_new_element
= true;
1179 if (!s2
->has_char_class
)
1182 for (int i
= 0; i
< N_CHARS
; i
++)
1190 s1
->state
= BEGIN_STATE
;
1191 s2
->state
= BEGIN_STATE
;
1193 while (c1
!= -1 && c2
!= -1)
1195 enum Upper_Lower_class class_s1
, class_s2
;
1197 c1
= get_next (s1
, &class_s1
);
1198 c2
= get_next (s2
, &class_s2
);
1200 /* If c2 transitions to a new case class, then
1201 c1 must also transition at the same time. */
1202 if (s2_new_element
&& class_s2
!= UL_NONE
1203 && !(s1_new_element
&& class_s1
!= UL_NONE
))
1204 die (EXIT_FAILURE
, 0,
1205 _("misaligned [:upper:] and/or [:lower:] construct"));
1207 /* If case converting, quickly skip over the elements. */
1208 if (class_s2
!= UL_NONE
)
1210 skip_construct (s1
);
1211 skip_construct (s2
);
1212 /* Discount insignificant/problematic lengths. */
1213 s1
->length
-= (class_s1
== UL_UPPER
? n_upper
: n_lower
) - 1;
1214 s2
->length
-= (class_s2
== UL_UPPER
? n_upper
: n_lower
) - 1;
1217 s1_new_element
= s1
->state
== NEW_ELEMENT
; /* Next element is new. */
1218 s2_new_element
= s2
->state
== NEW_ELEMENT
; /* Next element is new. */
1221 assert (old_s1_len
>= s1
->length
&& old_s2_len
>= s2
->length
);
1227 /* Gather statistics about the spec-list S in preparation for the tests
1228 in validate that determine the consistency of the specs. This function
1229 is called at most twice; once for string1, and again for any string2.
1230 LEN_S1 < 0 indicates that this is the first call and that S represents
1231 string1. When LEN_S1 >= 0, it is the length of the expansion of the
1232 constructs in string1, and we can use its value to resolve any
1233 indefinite repeat construct in S (which represents string2). Hence,
1234 this function has the side-effect that it converts a valid [c*]
1235 construct in string2 to [c*n] where n is large enough (or 0) to give
1236 string2 the same length as string1. For example, with the command
1237 tr a-z 'A[\n*]Z' on the second call to get_spec_stats, LEN_S1 would
1238 be 26 and S (representing string2) would be converted to 'A[\n*24]Z'. */
1241 get_spec_stats (struct Spec_list
*s
)
1243 struct List_element
*p
;
1246 s
->n_indefinite_repeats
= 0;
1247 s
->has_equiv_class
= false;
1248 s
->has_restricted_char_class
= false;
1249 s
->has_char_class
= false;
1250 for (p
= s
->head
->next
; p
; p
= p
->next
)
1257 case RE_NORMAL_CHAR
:
1262 assert (p
->u
.range
.last_char
>= p
->u
.range
.first_char
);
1263 len
= p
->u
.range
.last_char
- p
->u
.range
.first_char
+ 1;
1267 s
->has_char_class
= true;
1268 for (int i
= 0; i
< N_CHARS
; i
++)
1269 if (is_char_class_member (p
->u
.char_class
, i
))
1271 switch (p
->u
.char_class
)
1277 s
->has_restricted_char_class
= true;
1282 case RE_EQUIV_CLASS
:
1283 for (int i
= 0; i
< N_CHARS
; i
++)
1284 if (is_equiv_class_member (p
->u
.equiv_code
, i
))
1286 s
->has_equiv_class
= true;
1289 case RE_REPEATED_CHAR
:
1290 if (p
->u
.repeated_char
.repeat_count
> 0)
1291 len
= p
->u
.repeated_char
.repeat_count
;
1294 s
->indefinite_repeat_element
= p
;
1295 ++(s
->n_indefinite_repeats
);
1303 /* Check for arithmetic overflow in computing length. Also, reject
1304 any length greater than the maximum repeat count, in case the
1305 length is later used to compute the repeat count for an
1306 indefinite element. */
1307 new_length
= length
+ len
;
1308 if (! (length
<= new_length
&& new_length
<= REPEAT_COUNT_MAXIMUM
))
1309 die (EXIT_FAILURE
, 0, _("too many characters in set"));
1310 length
= new_length
;
1317 get_s1_spec_stats (struct Spec_list
*s1
)
1319 get_spec_stats (s1
);
1321 s1
->length
= card_of_complement (s1
);
1325 get_s2_spec_stats (struct Spec_list
*s2
, count len_s1
)
1327 get_spec_stats (s2
);
1328 if (len_s1
>= s2
->length
&& s2
->n_indefinite_repeats
== 1)
1330 s2
->indefinite_repeat_element
->u
.repeated_char
.repeat_count
=
1331 len_s1
- s2
->length
;
1332 s2
->length
= len_s1
;
1337 spec_init (struct Spec_list
*spec_list
)
1339 struct List_element
*new = xmalloc (sizeof *new);
1340 spec_list
->head
= spec_list
->tail
= new;
1341 spec_list
->head
->next
= NULL
;
1344 /* This function makes two passes over the argument string S. The first
1345 one converts all \c and \ddd escapes to their one-byte representations.
1346 The second constructs a linked specification list, SPEC_LIST, of the
1347 characters and constructs that comprise the argument string. If either
1348 of these passes detects an error, this function returns false. */
1351 parse_str (char const *s
, struct Spec_list
*spec_list
)
1354 bool ok
= unquote (s
, &es
) && build_spec_list (&es
, spec_list
);
1359 /* Given two specification lists, S1 and S2, and assuming that
1360 S1->length > S2->length, append a single [c*n] element to S2 where c
1361 is the last character in the expansion of S2 and n is the difference
1362 between the two lengths.
1363 Upon successful completion, S2->length is set to S1->length. The only
1364 way this function can fail to make S2 as long as S1 is when S2 has
1365 zero-length, since in that case, there is no last character to repeat.
1366 So S2->length is required to be at least 1. */
1369 string2_extend (const struct Spec_list
*s1
, struct Spec_list
*s2
)
1371 struct List_element
*p
;
1372 unsigned char char_to_repeat
;
1374 assert (translating
);
1375 assert (s1
->length
> s2
->length
);
1376 assert (s2
->length
> 0);
1381 case RE_NORMAL_CHAR
:
1382 char_to_repeat
= p
->u
.normal_char
;
1385 char_to_repeat
= p
->u
.range
.last_char
;
1388 /* Note BSD allows extending of classes in string2. For example:
1389 tr '[:upper:]0-9' '[:lower:]'
1390 That's not portable however, contradicts POSIX and is dependent
1391 on your collating sequence. */
1392 die (EXIT_FAILURE
, 0,
1393 _("when translating with string1 longer than string2,\nthe\
1394 latter string must not end with a character class"));
1396 case RE_REPEATED_CHAR
:
1397 char_to_repeat
= p
->u
.repeated_char
.the_repeated_char
;
1400 case RE_EQUIV_CLASS
:
1401 /* This shouldn't happen, because validate exits with an error
1402 if it finds an equiv class in string2 when translating. */
1409 append_repeated_char (s2
, char_to_repeat
, s1
->length
- s2
->length
);
1410 s2
->length
= s1
->length
;
1413 /* Return true if S is a non-empty list in which exactly one
1414 character (but potentially, many instances of it) appears.
1415 E.g., [X*] or xxxxxxxx. */
1418 homogeneous_spec_list (struct Spec_list
*s
)
1422 s
->state
= BEGIN_STATE
;
1424 if ((b
= get_next (s
, NULL
)) == -1)
1427 while ((c
= get_next (s
, NULL
)) != -1)
1434 /* Die with an error message if S1 and S2 describe strings that
1435 are not valid with the given command line switches.
1436 A side effect of this function is that if a valid [c*] or
1437 [c*0] construct appears in string2, it is converted to [c*n]
1438 with a value for n that makes s2->length == s1->length. By
1439 the same token, if the --truncate-set1 option is not
1440 given, S2 may be extended. */
1443 validate (struct Spec_list
*s1
, struct Spec_list
*s2
)
1445 get_s1_spec_stats (s1
);
1446 if (s1
->n_indefinite_repeats
> 0)
1448 die (EXIT_FAILURE
, 0,
1449 _("the [c*] repeat construct may not appear in string1"));
1454 get_s2_spec_stats (s2
, s1
->length
);
1456 if (s2
->n_indefinite_repeats
> 1)
1458 die (EXIT_FAILURE
, 0,
1459 _("only one [c*] repeat construct may appear in string2"));
1464 if (s2
->has_equiv_class
)
1466 die (EXIT_FAILURE
, 0,
1467 _("[=c=] expressions may not appear in string2\
1468 when translating"));
1471 if (s2
->has_restricted_char_class
)
1473 die (EXIT_FAILURE
, 0,
1474 _("when translating, the only character classes that may\
1475 appear in\nstring2 are 'upper' and 'lower'"));
1478 validate_case_classes (s1
, s2
);
1480 if (s1
->length
> s2
->length
)
1484 /* string2 must be non-empty unless --truncate-set1 is
1485 given or string1 is empty. */
1487 if (s2
->length
== 0)
1488 die (EXIT_FAILURE
, 0,
1489 _("when not truncating set1, string2 must be non-empty"));
1490 string2_extend (s1
, s2
);
1494 if (complement
&& s1
->has_char_class
1495 && ! (s2
->length
== s1
->length
&& homogeneous_spec_list (s2
)))
1497 die (EXIT_FAILURE
, 0,
1498 _("when translating with complemented character classes,\
1499 \nstring2 must map all characters in the domain to one"));
1503 /* Not translating. */
1505 if (s2
->n_indefinite_repeats
> 0)
1506 die (EXIT_FAILURE
, 0,
1507 _("the [c*] construct may appear in string2 only\
1508 when translating"));
1513 /* Read buffers of SIZE bytes via the function READER (if READER is
1514 NULL, read from stdin) until EOF. When non-NULL, READER is either
1515 read_and_delete or read_and_xlate. After each buffer is read, it is
1516 processed and written to stdout. The buffers are processed so that
1517 multiple consecutive occurrences of the same character in the input
1518 stream are replaced by a single occurrence of that character if the
1519 character is in the squeeze set. */
1522 squeeze_filter (char *buf
, size_t size
, size_t (*reader
) (char *, size_t))
1524 /* A value distinct from any character that may have been stored in a
1525 buffer as the result of a block-read in the function squeeze_filter. */
1526 const int NOT_A_CHAR
= INT_MAX
;
1528 int char_to_squeeze
= NOT_A_CHAR
;
1536 nr
= reader (buf
, size
);
1544 if (char_to_squeeze
== NOT_A_CHAR
)
1547 /* Here, by being a little tricky, we can get a significant
1548 performance increase in most cases when the input is
1549 reasonably large. Since tr will modify the input only
1550 if two consecutive (and identical) input characters are
1551 in the squeeze set, we can step by two through the data
1552 when searching for a character in the squeeze set. This
1553 means there may be a little more work in a few cases and
1554 perhaps twice as much work in the worst cases where most
1555 of the input is removed by squeezing repeats. But most
1556 uses of this functionality seem to remove less than 20-30%
1558 for (; i
< nr
&& !in_squeeze_set
[to_uchar (buf
[i
])]; i
+= 2)
1561 /* There is a special case when i == nr and we've just
1562 skipped a character (the last one in buf) that is in
1564 if (i
== nr
&& in_squeeze_set
[to_uchar (buf
[i
- 1])])
1568 out_len
= nr
- begin
;
1571 char_to_squeeze
= buf
[i
];
1572 /* We're about to output buf[begin..i]. */
1573 out_len
= i
- begin
+ 1;
1575 /* But since we stepped by 2 in the loop above,
1576 out_len may be one too large. */
1577 if (i
> 0 && buf
[i
- 1] == char_to_squeeze
)
1580 /* Advance i to the index of first character to be
1581 considered when looking for a char different from
1586 && fwrite (&buf
[begin
], 1, out_len
, stdout
) != out_len
)
1587 die (EXIT_FAILURE
, errno
, _("write error"));
1590 if (char_to_squeeze
!= NOT_A_CHAR
)
1592 /* Advance i to index of first char != char_to_squeeze
1593 (or to nr if all the rest of the characters in this
1594 buffer are the same as char_to_squeeze). */
1595 for (; i
< nr
&& buf
[i
] == char_to_squeeze
; i
++)
1598 char_to_squeeze
= NOT_A_CHAR
;
1599 /* If (i >= nr) we've squeezed the last character in this buffer.
1600 So now we have to read a new buffer and continue comparing
1601 characters against char_to_squeeze. */
1607 plain_read (char *buf
, size_t size
)
1609 size_t nr
= safe_read (STDIN_FILENO
, buf
, size
);
1610 if (nr
== SAFE_READ_ERROR
)
1611 die (EXIT_FAILURE
, errno
, _("read error"));
1615 /* Read buffers of SIZE bytes from stdin until one is found that
1616 contains at least one character not in the delete set. Store
1617 in the array BUF, all characters from that buffer that are not
1618 in the delete set, and return the number of characters saved
1622 read_and_delete (char *buf
, size_t size
)
1626 /* This enclosing do-while loop is to make sure that
1627 we don't return zero (indicating EOF) when we've
1628 just deleted all the characters in a buffer. */
1631 size_t nr
= plain_read (buf
, size
);
1636 /* This first loop may be a waste of code, but gives much
1637 better performance when no characters are deleted in
1638 the beginning of a buffer. It just avoids the copying
1639 of buf[i] into buf[n_saved] when it would be a NOP. */
1642 for (i
= 0; i
< nr
&& !in_delete_set
[to_uchar (buf
[i
])]; i
++)
1646 for (++i
; i
< nr
; i
++)
1647 if (!in_delete_set
[to_uchar (buf
[i
])])
1648 buf
[n_saved
++] = buf
[i
];
1650 while (n_saved
== 0);
1655 /* Read at most SIZE bytes from stdin into the array BUF. Then
1656 perform the in-place and one-to-one mapping specified by the global
1657 array 'xlate'. Return the number of characters read, or 0 upon EOF. */
1660 read_and_xlate (char *buf
, size_t size
)
1662 size_t bytes_read
= plain_read (buf
, size
);
1664 for (size_t i
= 0; i
< bytes_read
; i
++)
1665 buf
[i
] = xlate
[to_uchar (buf
[i
])];
1670 /* Initialize a boolean membership set, IN_SET, with the character
1671 values obtained by traversing the linked list of constructs S
1672 using the function 'get_next'. IN_SET is expected to have been
1673 initialized to all zeros by the caller. If COMPLEMENT_THIS_SET
1674 is true the resulting set is complemented. */
1677 set_initialize (struct Spec_list
*s
, bool complement_this_set
, bool *in_set
)
1681 s
->state
= BEGIN_STATE
;
1682 while ((c
= get_next (s
, NULL
)) != -1)
1684 if (complement_this_set
)
1685 for (size_t i
= 0; i
< N_CHARS
; i
++)
1686 in_set
[i
] = (!in_set
[i
]);
1690 main (int argc
, char **argv
)
1693 int non_option_args
;
1696 struct Spec_list buf1
, buf2
;
1697 struct Spec_list
*s1
= &buf1
;
1698 struct Spec_list
*s2
= &buf2
;
1700 initialize_main (&argc
, &argv
);
1701 set_program_name (argv
[0]);
1702 setlocale (LC_ALL
, "");
1703 bindtextdomain (PACKAGE
, LOCALEDIR
);
1704 textdomain (PACKAGE
);
1706 atexit (close_stdout
);
1708 while ((c
= getopt_long (argc
, argv
, "+cCdst", long_options
, NULL
)) != -1)
1722 squeeze_repeats
= true;
1726 truncate_set1
= true;
1729 case_GETOPT_HELP_CHAR
;
1731 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
1734 usage (EXIT_FAILURE
);
1739 non_option_args
= argc
- optind
;
1740 translating
= (non_option_args
== 2 && !delete);
1741 min_operands
= 1 + (delete == squeeze_repeats
);
1742 max_operands
= 1 + (delete <= squeeze_repeats
);
1744 if (non_option_args
< min_operands
)
1746 if (non_option_args
== 0)
1747 error (0, 0, _("missing operand"));
1750 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
1751 fprintf (stderr
, "%s\n",
1753 ? N_("Two strings must be given when "
1754 "both deleting and squeezing repeats.")
1755 : N_("Two strings must be given when translating.")));
1757 usage (EXIT_FAILURE
);
1760 if (max_operands
< non_option_args
)
1762 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ max_operands
]));
1763 if (non_option_args
== 2)
1764 fprintf (stderr
, "%s\n",
1765 _("Only one string may be given when "
1766 "deleting without squeezing repeats."));
1767 usage (EXIT_FAILURE
);
1771 if (!parse_str (argv
[optind
], s1
))
1772 return EXIT_FAILURE
;
1774 if (non_option_args
== 2)
1777 if (!parse_str (argv
[optind
+ 1], s2
))
1778 return EXIT_FAILURE
;
1785 /* Use binary I/O, since 'tr' is sometimes used to transliterate
1786 non-printable characters, or characters which are stripped away
1787 by text-mode reads (like CR and ^Z). */
1788 xset_binary_mode (STDIN_FILENO
, O_BINARY
);
1789 xset_binary_mode (STDOUT_FILENO
, O_BINARY
);
1790 fadvise (stdin
, FADVISE_SEQUENTIAL
);
1792 if (squeeze_repeats
&& non_option_args
== 1)
1794 set_initialize (s1
, complement
, in_squeeze_set
);
1795 squeeze_filter (io_buf
, sizeof io_buf
, plain_read
);
1797 else if (delete && non_option_args
== 1)
1799 set_initialize (s1
, complement
, in_delete_set
);
1803 size_t nr
= read_and_delete (io_buf
, sizeof io_buf
);
1806 if (fwrite (io_buf
, 1, nr
, stdout
) != nr
)
1807 die (EXIT_FAILURE
, errno
, _("write error"));
1810 else if (squeeze_repeats
&& delete && non_option_args
== 2)
1812 set_initialize (s1
, complement
, in_delete_set
);
1813 set_initialize (s2
, false, in_squeeze_set
);
1814 squeeze_filter (io_buf
, sizeof io_buf
, read_and_delete
);
1816 else if (translating
)
1820 bool *in_s1
= in_delete_set
;
1822 set_initialize (s1
, false, in_s1
);
1823 s2
->state
= BEGIN_STATE
;
1824 for (int i
= 0; i
< N_CHARS
; i
++)
1826 for (int i
= 0; i
< N_CHARS
; i
++)
1830 int ch
= get_next (s2
, NULL
);
1831 assert (ch
!= -1 || truncate_set1
);
1834 /* This will happen when tr is invoked like e.g.
1835 tr -cs A-Za-z0-9 '\012'. */
1845 enum Upper_Lower_class class_s1
;
1846 enum Upper_Lower_class class_s2
;
1848 for (int i
= 0; i
< N_CHARS
; i
++)
1850 s1
->state
= BEGIN_STATE
;
1851 s2
->state
= BEGIN_STATE
;
1854 c1
= get_next (s1
, &class_s1
);
1855 c2
= get_next (s2
, &class_s2
);
1857 if (class_s1
== UL_LOWER
&& class_s2
== UL_UPPER
)
1859 for (int i
= 0; i
< N_CHARS
; i
++)
1861 xlate
[i
] = toupper (i
);
1863 else if (class_s1
== UL_UPPER
&& class_s2
== UL_LOWER
)
1865 for (int i
= 0; i
< N_CHARS
; i
++)
1867 xlate
[i
] = tolower (i
);
1871 /* The following should have been checked by validate... */
1872 if (c1
== -1 || c2
== -1)
1877 /* When case-converting, skip the elements as an optimization. */
1878 if (class_s2
!= UL_NONE
)
1880 skip_construct (s1
);
1881 skip_construct (s2
);
1884 assert (c1
== -1 || truncate_set1
);
1886 if (squeeze_repeats
)
1888 set_initialize (s2
, false, in_squeeze_set
);
1889 squeeze_filter (io_buf
, sizeof io_buf
, read_and_xlate
);
1895 size_t bytes_read
= read_and_xlate (io_buf
, sizeof io_buf
);
1896 if (bytes_read
== 0)
1898 if (fwrite (io_buf
, 1, bytes_read
, stdout
) != bytes_read
)
1899 die (EXIT_FAILURE
, errno
, _("write error"));
1904 if (close (STDIN_FILENO
) != 0)
1905 die (EXIT_FAILURE
, errno
, _("standard input"));
1907 return EXIT_SUCCESS
;