build: ensure sys/select.h is included
[coreutils.git] / src / tr.c
blob43c5b4ef84ed6f01da766df030f37d58dbc49072
1 /* tr -- a filter to translate characters
2 Copyright (C) 1991-2019 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 */
19 #include <config.h>
21 #include <stdio.h>
22 #include <assert.h>
23 #include <sys/types.h>
24 #include <getopt.h>
26 #include "system.h"
27 #include "die.h"
28 #include "error.h"
29 #include "fadvise.h"
30 #include "quote.h"
31 #include "safe-read.h"
32 #include "xbinary-io.h"
33 #include "xstrtol.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
47 uintmax_t, though. */
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
61 initializations. */
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. */
70 enum Char_class
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,
75 CC_NO_CLASS = 9999
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
83 and string2. */
84 enum Upper_Lower_class
86 UL_LOWER,
87 UL_UPPER,
88 UL_NONE
91 /* The type of a List_element. See build_spec_list for more details. */
92 enum Range_element_type
94 RE_NORMAL_CHAR,
95 RE_RANGE,
96 RE_CHAR_CLASS,
97 RE_EQUIV_CLASS,
98 RE_REPEATED_CHAR
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. */
106 struct List_element
108 enum Range_element_type type;
109 struct List_element *next;
110 union
112 unsigned char normal_char;
113 struct /* unnamed */
115 unsigned char first_char;
116 unsigned char last_char;
118 range;
119 enum Char_class char_class;
120 unsigned char equiv_code;
121 struct /* unnamed */
123 unsigned char the_repeated_char;
124 count repeat_count;
126 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. */
140 struct Spec_list
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. */
153 count state;
155 /* Length, in the sense that length ('a-z[:digit:]123abc')
156 is 42 ( = 26 + 10 + 6). */
157 count length;
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:]. */
173 bool has_char_class;
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. */
184 struct E_string
186 char *s;
187 bool *escaped;
188 size_t len;
191 /* Return nonzero if the Ith character of escaped string ES matches C
192 and is not escaped itself. */
193 static inline bool
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},
277 {NULL, 0, NULL, 0}
280 void
281 usage (int status)
283 if (status != EXIT_SUCCESS)
284 emit_try_help ();
285 else
287 printf (_("\
288 Usage: %s [OPTION]... SET1 [SET2]\n\
290 program_name);
291 fputs (_("\
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\
301 "), stdout);
302 fputs (HELP_OPTION_DESCRIPTION, stdout);
303 fputs (VERSION_OPTION_DESCRIPTION, stdout);
304 fputs (_("\
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\
310 \\\\ backslash\n\
311 \\a audible BEL\n\
312 \\b backspace\n\
313 \\f form feed\n\
314 \\n new line\n\
315 \\r return\n\
316 \\t horizontal tab\n\
317 "), stdout);
318 fputs (_("\
319 \\v vertical 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\
328 "), stdout);
329 fputs (_("\
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\
338 "), stdout);
339 fputs (_("\
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\
348 "), stdout);
349 emit_ancillary_info (PROGRAM_NAME);
351 exit (status);
354 /* Return nonzero if the character C is a member of the
355 equivalence class containing the character EQUIV_CLASS. */
357 static inline bool
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)
369 int result;
371 switch (char_class)
373 case CC_ALNUM:
374 result = isalnum (c);
375 break;
376 case CC_ALPHA:
377 result = isalpha (c);
378 break;
379 case CC_BLANK:
380 result = isblank (c);
381 break;
382 case CC_CNTRL:
383 result = iscntrl (c);
384 break;
385 case CC_DIGIT:
386 result = isdigit (c);
387 break;
388 case CC_GRAPH:
389 result = isgraph (c);
390 break;
391 case CC_LOWER:
392 result = islower (c);
393 break;
394 case CC_PRINT:
395 result = isprint (c);
396 break;
397 case CC_PUNCT:
398 result = ispunct (c);
399 break;
400 case CC_SPACE:
401 result = isspace (c);
402 break;
403 case CC_UPPER:
404 result = isupper (c);
405 break;
406 case CC_XDIGIT:
407 result = isxdigit (c);
408 break;
409 default:
410 abort ();
413 return !! result;
416 static void
417 es_free (struct E_string *es)
419 free (es->s);
420 free (es->escaped);
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. */
431 static bool
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]);
439 unsigned int j = 0;
440 for (unsigned int i = 0; s[i]; i++)
442 unsigned char c;
443 int oct_digit;
445 switch (s[i])
447 case '\\':
448 es->escaped[j] = true;
449 switch (s[i + 1])
451 case '\\':
452 c = '\\';
453 break;
454 case 'a':
455 c = '\a';
456 break;
457 case 'b':
458 c = '\b';
459 break;
460 case 'f':
461 c = '\f';
462 break;
463 case 'n':
464 c = '\n';
465 break;
466 case 'r':
467 c = '\r';
468 break;
469 case 't':
470 c = '\t';
471 break;
472 case 'v':
473 c = '\v';
474 break;
475 case '0':
476 case '1':
477 case '2':
478 case '3':
479 case '4':
480 case '5':
481 case '6':
482 case '7':
483 c = s[i + 1] - '0';
484 oct_digit = s[i + 2] - '0';
485 if (0 <= oct_digit && oct_digit <= 7)
487 c = 8 * c + oct_digit;
488 ++i;
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;
495 ++i;
497 else
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
506 a warning. */
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]);
514 break;
515 case '\0':
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;
520 i--;
521 c = '\\';
522 break;
523 default:
524 c = s[i + 1];
525 break;
527 ++i;
528 es->s[j++] = c;
529 break;
530 default:
531 es->s[j++] = s[i];
532 break;
535 es->len = j;
536 return true;
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)
545 enum Char_class i;
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)
550 return i;
551 return CC_NO_CLASS;
554 /* Return a newly allocated string with a printable version of C.
555 This function is used solely for formatting error messages. */
557 static char *
558 make_printable_char (unsigned char c)
560 char *buf = xmalloc (5);
562 if (isprint (c))
564 buf[0] = c;
565 buf[1] = '\0';
567 else
569 sprintf (buf, "\\%03o", c);
571 return buf;
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. */
581 static char *
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++)
591 char buf[5];
592 char const *tmp = NULL;
593 unsigned char c = s[i];
595 switch (c)
597 case '\\':
598 tmp = "\\";
599 break;
600 case '\a':
601 tmp = "\\a";
602 break;
603 case '\b':
604 tmp = "\\b";
605 break;
606 case '\f':
607 tmp = "\\f";
608 break;
609 case '\n':
610 tmp = "\\n";
611 break;
612 case '\r':
613 tmp = "\\r";
614 break;
615 case '\t':
616 tmp = "\\t";
617 break;
618 case '\v':
619 tmp = "\\v";
620 break;
621 default:
622 if (isprint (c))
624 buf[0] = c;
625 buf[1] = '\0';
627 else
628 sprintf (buf, "\\%03o", c);
629 tmp = buf;
630 break;
632 p = stpcpy (p, tmp);
634 return printable_buf;
637 /* Append a newly allocated structure representing a
638 character C to the specification list LIST. */
640 static void
641 append_normal_char (struct Spec_list *list, unsigned char c)
643 struct List_element *new = xmalloc (sizeof *new);
644 new->next = NULL;
645 new->type = RE_NORMAL_CHAR;
646 new->u.normal_char = c;
647 assert (list->tail);
648 list->tail->next = new;
649 list->tail = 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. */
657 static bool
658 append_range (struct Spec_list *list, unsigned char first, unsigned char last)
660 if (last < first)
662 char *tmp1 = make_printable_char (first);
663 char *tmp2 = make_printable_char (last);
665 error (0, 0,
666 _("range-endpoints of '%s-%s' are in reverse collating sequence order"),
667 tmp1, tmp2);
668 free (tmp1);
669 free (tmp2);
670 return false;
672 struct List_element *new = xmalloc (sizeof *new);
673 new->next = NULL;
674 new->type = RE_RANGE;
675 new->u.range.first_char = first;
676 new->u.range.last_char = last;
677 assert (list->tail);
678 list->tail->next = new;
679 list->tail = new;
680 return true;
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. */
688 static bool
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)
694 return false;
695 struct List_element *new = xmalloc (sizeof *new);
696 new->next = NULL;
697 new->type = RE_CHAR_CLASS;
698 new->u.char_class = char_class;
699 assert (list->tail);
700 list->tail->next = new;
701 list->tail = new;
702 return true;
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. */
710 static void
711 append_repeated_char (struct Spec_list *list, unsigned char the_char,
712 count repeat_count)
714 struct List_element *new = xmalloc (sizeof *new);
715 new->next = NULL;
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;
719 assert (list->tail);
720 list->tail->next = new;
721 list->tail = 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. */
730 static bool
731 append_equiv_class (struct Spec_list *list,
732 char const *equiv_class_str, size_t len)
734 if (len != 1)
735 return false;
737 struct List_element *new = xmalloc (sizeof *new);
738 new->next = NULL;
739 new->type = RE_EQUIV_CLASS;
740 new->u.equiv_code = *equiv_class_str;
741 assert (list->tail);
742 list->tail->next = new;
743 list->tail = new;
744 return true;
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
751 zero bytes. */
753 static bool
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])
761 *result_idx = i;
762 return true;
764 return false;
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
775 and return -2. */
777 static int
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, '*'))
784 return -1;
786 for (size_t i = start_idx + 2; i < es->len && !es->escaped[i]; i++)
788 if (es->s[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. */
796 *repeat_count = 0;
798 else
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];
803 char *d_end;
804 if ((xstrtoumax (digit_str, &d_end, *digit_str == '0' ? 8 : 10,
805 repeat_count, NULL)
806 != LONGINT_OK)
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);
811 error (0, 0,
812 _("invalid repeat count %s in [c*n] construct"),
813 quote (tmp));
814 free (tmp);
815 return -2;
818 *closing_bracket_idx = i;
819 return 0;
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, '*'))
833 return false;
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, ']');
838 return false;
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
848 decimal integer.
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. */
853 static bool
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. */
864 size_t i;
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;
872 count repeat_count;
873 int err;
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)
887 if (p[i + 1] == ':')
888 error (0, 0, _("missing character class name '[::]'"));
889 else
890 error (0, 0,
891 _("missing equivalence class character '[==]'"));
892 return false;
895 if (p[i + 1] == ':')
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;
902 else
904 char *tmp = make_printable_str (opnd_str,
905 opnd_str_len);
906 error (0, 0, _("invalid character class %s"),
907 quote (tmp));
908 free (tmp);
909 return false;
913 else
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;
920 else
922 char *tmp = make_printable_str (opnd_str,
923 opnd_str_len);
924 error (0, 0,
925 _("%s: equivalence class operand must be a single character"),
926 tmp);
927 free (tmp);
928 return false;
933 i = closing_delim_idx + 2;
934 continue;
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,
944 &repeat_count,
945 &closing_bracket_idx);
946 if (err == 0)
948 append_repeated_char (result, char_to_repeat, repeat_count);
949 i = closing_bracket_idx + 1;
951 else if (err == -1)
953 matched_multi_char_construct = false;
955 else
957 /* Found a string that looked like [c*n] but the
958 numeric part was invalid. */
959 return false;
962 if (matched_multi_char_construct)
963 continue;
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]))
974 return false;
975 i += 3;
977 else
979 append_normal_char (result, p[i]);
980 ++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]);
988 return true;
991 /* Advance past the current construct.
992 S->tail must be non-NULL. */
993 static void
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
1012 positions. */
1014 static int
1015 get_next (struct Spec_list *s, enum Upper_Lower_class *class)
1017 struct List_element *p;
1018 int return_val;
1019 int i;
1021 if (class)
1022 *class = UL_NONE;
1024 if (s->state == BEGIN_STATE)
1026 s->tail = s->head->next;
1027 s->state = NEW_ELEMENT;
1030 p = s->tail;
1031 if (p == NULL)
1032 return -1;
1034 switch (p->type)
1036 case RE_NORMAL_CHAR:
1037 return_val = p->u.normal_char;
1038 s->state = NEW_ELEMENT;
1039 s->tail = p->next;
1040 break;
1042 case RE_RANGE:
1043 if (s->state == NEW_ELEMENT)
1044 s->state = p->u.range.first_char;
1045 else
1046 ++(s->state);
1047 return_val = s->state;
1048 if (s->state == p->u.range.last_char)
1050 s->tail = p->next;
1051 s->state = NEW_ELEMENT;
1053 break;
1055 case RE_CHAR_CLASS:
1056 if (class)
1058 switch (p->u.char_class)
1060 case CC_LOWER:
1061 *class = UL_LOWER;
1062 break;
1063 case CC_UPPER:
1064 *class = UL_UPPER;
1065 break;
1066 default:
1067 break;
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))
1075 break;
1076 assert (i < N_CHARS);
1077 s->state = i;
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))
1083 break;
1084 if (i < N_CHARS)
1085 s->state = i;
1086 else
1088 s->tail = p->next;
1089 s->state = NEW_ELEMENT;
1091 break;
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;
1101 s->tail = p->next;
1102 break;
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)
1108 s->tail = p->next;
1109 s->state = NEW_ELEMENT;
1110 return_val = get_next (s, class);
1112 else
1114 if (s->state == NEW_ELEMENT)
1116 s->state = 0;
1118 ++(s->state);
1119 return_val = p->u.repeated_char.the_repeated_char;
1120 if (s->state == p->u.repeated_char.repeat_count)
1122 s->tail = p->next;
1123 s->state = NEW_ELEMENT;
1126 break;
1128 default:
1129 abort ();
1132 return return_val;
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. */
1140 static int
1141 card_of_complement (struct Spec_list *s)
1143 int c;
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]);
1151 in_set[c] = true;
1153 return cardinality;
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. */
1165 static void
1166 validate_case_classes (struct Spec_list *s1, struct Spec_list *s2)
1168 size_t n_upper = 0;
1169 size_t n_lower = 0;
1170 int c1 = 0;
1171 int c2 = 0;
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)
1180 return;
1182 for (int i = 0; i < N_CHARS; i++)
1184 if (isupper (i))
1185 n_upper++;
1186 if (islower (i))
1187 n_lower++;
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);
1223 s1->tail = s1_tail;
1224 s2->tail = s2_tail;
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'. */
1240 static void
1241 get_spec_stats (struct Spec_list *s)
1243 struct List_element *p;
1244 count length = 0;
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)
1252 count len = 0;
1253 count new_length;
1255 switch (p->type)
1257 case RE_NORMAL_CHAR:
1258 len = 1;
1259 break;
1261 case RE_RANGE:
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;
1264 break;
1266 case RE_CHAR_CLASS:
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))
1270 ++len;
1271 switch (p->u.char_class)
1273 case CC_UPPER:
1274 case CC_LOWER:
1275 break;
1276 default:
1277 s->has_restricted_char_class = true;
1278 break;
1280 break;
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))
1285 ++len;
1286 s->has_equiv_class = true;
1287 break;
1289 case RE_REPEATED_CHAR:
1290 if (p->u.repeated_char.repeat_count > 0)
1291 len = p->u.repeated_char.repeat_count;
1292 else
1294 s->indefinite_repeat_element = p;
1295 ++(s->n_indefinite_repeats);
1297 break;
1299 default:
1300 abort ();
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;
1313 s->length = length;
1316 static void
1317 get_s1_spec_stats (struct Spec_list *s1)
1319 get_spec_stats (s1);
1320 if (complement)
1321 s1->length = card_of_complement (s1);
1324 static void
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;
1336 static void
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. */
1350 static bool
1351 parse_str (char const *s, struct Spec_list *spec_list)
1353 struct E_string es;
1354 bool ok = unquote (s, &es) && build_spec_list (&es, spec_list);
1355 es_free (&es);
1356 return ok;
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. */
1368 static void
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);
1378 p = s2->tail;
1379 switch (p->type)
1381 case RE_NORMAL_CHAR:
1382 char_to_repeat = p->u.normal_char;
1383 break;
1384 case RE_RANGE:
1385 char_to_repeat = p->u.range.last_char;
1386 break;
1387 case RE_CHAR_CLASS:
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;
1398 break;
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. */
1403 abort ();
1405 default:
1406 abort ();
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. */
1417 static bool
1418 homogeneous_spec_list (struct Spec_list *s)
1420 int b, c;
1422 s->state = BEGIN_STATE;
1424 if ((b = get_next (s, NULL)) == -1)
1425 return false;
1427 while ((c = get_next (s, NULL)) != -1)
1428 if (c != b)
1429 return false;
1431 return true;
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. */
1442 static void
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"));
1452 if (s2)
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"));
1462 if (translating)
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)
1482 if (!truncate_set1)
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"));
1502 else
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. */
1521 static void
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;
1529 size_t i = 0;
1530 size_t nr = 0;
1532 while (true)
1534 if (i >= nr)
1536 nr = reader (buf, size);
1537 if (nr == 0)
1538 break;
1539 i = 0;
1542 size_t begin = i;
1544 if (char_to_squeeze == NOT_A_CHAR)
1546 size_t out_len;
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%
1557 of the input. */
1558 for (; i < nr && !in_squeeze_set[to_uchar (buf[i])]; i += 2)
1559 continue;
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
1563 the squeeze set. */
1564 if (i == nr && in_squeeze_set[to_uchar (buf[i - 1])])
1565 --i;
1567 if (i >= nr)
1568 out_len = nr - begin;
1569 else
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)
1578 --out_len;
1580 /* Advance i to the index of first character to be
1581 considered when looking for a char different from
1582 char_to_squeeze. */
1583 ++i;
1585 if (out_len > 0
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++)
1596 continue;
1597 if (i < nr)
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. */
1606 static size_t
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"));
1612 return nr;
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
1619 or 0 upon EOF. */
1621 static size_t
1622 read_and_delete (char *buf, size_t size)
1624 size_t n_saved;
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);
1633 if (nr == 0)
1634 return 0;
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. */
1641 size_t i;
1642 for (i = 0; i < nr && !in_delete_set[to_uchar (buf[i])]; i++)
1643 continue;
1644 n_saved = 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);
1652 return n_saved;
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. */
1659 static size_t
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])];
1667 return bytes_read;
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. */
1676 static void
1677 set_initialize (struct Spec_list *s, bool complement_this_set, bool *in_set)
1679 int c;
1681 s->state = BEGIN_STATE;
1682 while ((c = get_next (s, NULL)) != -1)
1683 in_set[c] = true;
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)
1692 int c;
1693 int non_option_args;
1694 int min_operands;
1695 int max_operands;
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, "+AcCdst", long_options, NULL)) != -1)
1710 switch (c)
1712 case 'A':
1713 /* Undocumented option, for compatibility with AIX. */
1714 setlocale (LC_COLLATE, "C");
1715 setlocale (LC_CTYPE, "C");
1716 break;
1718 case 'c':
1719 case 'C':
1720 complement = true;
1721 break;
1723 case 'd':
1724 delete = true;
1725 break;
1727 case 's':
1728 squeeze_repeats = true;
1729 break;
1731 case 't':
1732 truncate_set1 = true;
1733 break;
1735 case_GETOPT_HELP_CHAR;
1737 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1739 default:
1740 usage (EXIT_FAILURE);
1741 break;
1745 non_option_args = argc - optind;
1746 translating = (non_option_args == 2 && !delete);
1747 min_operands = 1 + (delete == squeeze_repeats);
1748 max_operands = 1 + (delete <= squeeze_repeats);
1750 if (non_option_args < min_operands)
1752 if (non_option_args == 0)
1753 error (0, 0, _("missing operand"));
1754 else
1756 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
1757 fprintf (stderr, "%s\n",
1758 _(squeeze_repeats
1759 ? N_("Two strings must be given when "
1760 "both deleting and squeezing repeats.")
1761 : N_("Two strings must be given when translating.")));
1763 usage (EXIT_FAILURE);
1766 if (max_operands < non_option_args)
1768 error (0, 0, _("extra operand %s"), quote (argv[optind + max_operands]));
1769 if (non_option_args == 2)
1770 fprintf (stderr, "%s\n",
1771 _("Only one string may be given when "
1772 "deleting without squeezing repeats."));
1773 usage (EXIT_FAILURE);
1776 spec_init (s1);
1777 if (!parse_str (argv[optind], s1))
1778 return EXIT_FAILURE;
1780 if (non_option_args == 2)
1782 spec_init (s2);
1783 if (!parse_str (argv[optind + 1], s2))
1784 return EXIT_FAILURE;
1786 else
1787 s2 = NULL;
1789 validate (s1, s2);
1791 /* Use binary I/O, since 'tr' is sometimes used to transliterate
1792 non-printable characters, or characters which are stripped away
1793 by text-mode reads (like CR and ^Z). */
1794 xset_binary_mode (STDIN_FILENO, O_BINARY);
1795 xset_binary_mode (STDOUT_FILENO, O_BINARY);
1796 fadvise (stdin, FADVISE_SEQUENTIAL);
1798 if (squeeze_repeats && non_option_args == 1)
1800 set_initialize (s1, complement, in_squeeze_set);
1801 squeeze_filter (io_buf, sizeof io_buf, plain_read);
1803 else if (delete && non_option_args == 1)
1805 set_initialize (s1, complement, in_delete_set);
1807 while (true)
1809 size_t nr = read_and_delete (io_buf, sizeof io_buf);
1810 if (nr == 0)
1811 break;
1812 if (fwrite (io_buf, 1, nr, stdout) != nr)
1813 die (EXIT_FAILURE, errno, _("write error"));
1816 else if (squeeze_repeats && delete && non_option_args == 2)
1818 set_initialize (s1, complement, in_delete_set);
1819 set_initialize (s2, false, in_squeeze_set);
1820 squeeze_filter (io_buf, sizeof io_buf, read_and_delete);
1822 else if (translating)
1824 if (complement)
1826 bool *in_s1 = in_delete_set;
1828 set_initialize (s1, false, in_s1);
1829 s2->state = BEGIN_STATE;
1830 for (int i = 0; i < N_CHARS; i++)
1831 xlate[i] = i;
1832 for (int i = 0; i < N_CHARS; i++)
1834 if (!in_s1[i])
1836 int ch = get_next (s2, NULL);
1837 assert (ch != -1 || truncate_set1);
1838 if (ch == -1)
1840 /* This will happen when tr is invoked like e.g.
1841 tr -cs A-Za-z0-9 '\012'. */
1842 break;
1844 xlate[i] = ch;
1848 else
1850 int c1, c2;
1851 enum Upper_Lower_class class_s1;
1852 enum Upper_Lower_class class_s2;
1854 for (int i = 0; i < N_CHARS; i++)
1855 xlate[i] = i;
1856 s1->state = BEGIN_STATE;
1857 s2->state = BEGIN_STATE;
1858 while (true)
1860 c1 = get_next (s1, &class_s1);
1861 c2 = get_next (s2, &class_s2);
1863 if (class_s1 == UL_LOWER && class_s2 == UL_UPPER)
1865 for (int i = 0; i < N_CHARS; i++)
1866 if (islower (i))
1867 xlate[i] = toupper (i);
1869 else if (class_s1 == UL_UPPER && class_s2 == UL_LOWER)
1871 for (int i = 0; i < N_CHARS; i++)
1872 if (isupper (i))
1873 xlate[i] = tolower (i);
1875 else
1877 /* The following should have been checked by validate... */
1878 if (c1 == -1 || c2 == -1)
1879 break;
1880 xlate[c1] = c2;
1883 /* When case-converting, skip the elements as an optimization. */
1884 if (class_s2 != UL_NONE)
1886 skip_construct (s1);
1887 skip_construct (s2);
1890 assert (c1 == -1 || truncate_set1);
1892 if (squeeze_repeats)
1894 set_initialize (s2, false, in_squeeze_set);
1895 squeeze_filter (io_buf, sizeof io_buf, read_and_xlate);
1897 else
1899 while (true)
1901 size_t bytes_read = read_and_xlate (io_buf, sizeof io_buf);
1902 if (bytes_read == 0)
1903 break;
1904 if (fwrite (io_buf, 1, bytes_read, stdout) != bytes_read)
1905 die (EXIT_FAILURE, errno, _("write error"));
1910 if (close (STDIN_FILENO) != 0)
1911 die (EXIT_FAILURE, errno, _("standard input"));
1913 return EXIT_SUCCESS;