doc: refactor and update expand and unexpand --help
[coreutils.git] / src / tr.c
blob0c770d7a9bf3090bb900b30ea8c50880e637522a
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 <http://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 unsigned int i;
1171 int c1 = 0;
1172 int c2 = 0;
1173 count old_s1_len = s1->length;
1174 count old_s2_len = s2->length;
1175 struct List_element *s1_tail = s1->tail;
1176 struct List_element *s2_tail = s2->tail;
1177 bool s1_new_element = true;
1178 bool s2_new_element = true;
1180 if (!s2->has_char_class)
1181 return;
1183 for (i = 0; i < N_CHARS; i++)
1185 if (isupper (i))
1186 n_upper++;
1187 if (islower (i))
1188 n_lower++;
1191 s1->state = BEGIN_STATE;
1192 s2->state = BEGIN_STATE;
1194 while (c1 != -1 && c2 != -1)
1196 enum Upper_Lower_class class_s1, class_s2;
1198 c1 = get_next (s1, &class_s1);
1199 c2 = get_next (s2, &class_s2);
1201 /* If c2 transitions to a new case class, then
1202 c1 must also transition at the same time. */
1203 if (s2_new_element && class_s2 != UL_NONE
1204 && !(s1_new_element && class_s1 != UL_NONE))
1205 die (EXIT_FAILURE, 0,
1206 _("misaligned [:upper:] and/or [:lower:] construct"));
1208 /* If case converting, quickly skip over the elements. */
1209 if (class_s2 != UL_NONE)
1211 skip_construct (s1);
1212 skip_construct (s2);
1213 /* Discount insignificant/problematic lengths. */
1214 s1->length -= (class_s1 == UL_UPPER ? n_upper : n_lower) - 1;
1215 s2->length -= (class_s2 == UL_UPPER ? n_upper : n_lower) - 1;
1218 s1_new_element = s1->state == NEW_ELEMENT; /* Next element is new. */
1219 s2_new_element = s2->state == NEW_ELEMENT; /* Next element is new. */
1222 assert (old_s1_len >= s1->length && old_s2_len >= s2->length);
1224 s1->tail = s1_tail;
1225 s2->tail = s2_tail;
1228 /* Gather statistics about the spec-list S in preparation for the tests
1229 in validate that determine the consistency of the specs. This function
1230 is called at most twice; once for string1, and again for any string2.
1231 LEN_S1 < 0 indicates that this is the first call and that S represents
1232 string1. When LEN_S1 >= 0, it is the length of the expansion of the
1233 constructs in string1, and we can use its value to resolve any
1234 indefinite repeat construct in S (which represents string2). Hence,
1235 this function has the side-effect that it converts a valid [c*]
1236 construct in string2 to [c*n] where n is large enough (or 0) to give
1237 string2 the same length as string1. For example, with the command
1238 tr a-z 'A[\n*]Z' on the second call to get_spec_stats, LEN_S1 would
1239 be 26 and S (representing string2) would be converted to 'A[\n*24]Z'. */
1241 static void
1242 get_spec_stats (struct Spec_list *s)
1244 struct List_element *p;
1245 count length = 0;
1247 s->n_indefinite_repeats = 0;
1248 s->has_equiv_class = false;
1249 s->has_restricted_char_class = false;
1250 s->has_char_class = false;
1251 for (p = s->head->next; p; p = p->next)
1253 count len = 0;
1254 count new_length;
1256 switch (p->type)
1258 case RE_NORMAL_CHAR:
1259 len = 1;
1260 break;
1262 case RE_RANGE:
1263 assert (p->u.range.last_char >= p->u.range.first_char);
1264 len = p->u.range.last_char - p->u.range.first_char + 1;
1265 break;
1267 case RE_CHAR_CLASS:
1268 s->has_char_class = true;
1269 for (int i = 0; i < N_CHARS; i++)
1270 if (is_char_class_member (p->u.char_class, i))
1271 ++len;
1272 switch (p->u.char_class)
1274 case CC_UPPER:
1275 case CC_LOWER:
1276 break;
1277 default:
1278 s->has_restricted_char_class = true;
1279 break;
1281 break;
1283 case RE_EQUIV_CLASS:
1284 for (int i = 0; i < N_CHARS; i++)
1285 if (is_equiv_class_member (p->u.equiv_code, i))
1286 ++len;
1287 s->has_equiv_class = true;
1288 break;
1290 case RE_REPEATED_CHAR:
1291 if (p->u.repeated_char.repeat_count > 0)
1292 len = p->u.repeated_char.repeat_count;
1293 else
1295 s->indefinite_repeat_element = p;
1296 ++(s->n_indefinite_repeats);
1298 break;
1300 default:
1301 abort ();
1304 /* Check for arithmetic overflow in computing length. Also, reject
1305 any length greater than the maximum repeat count, in case the
1306 length is later used to compute the repeat count for an
1307 indefinite element. */
1308 new_length = length + len;
1309 if (! (length <= new_length && new_length <= REPEAT_COUNT_MAXIMUM))
1310 die (EXIT_FAILURE, 0, _("too many characters in set"));
1311 length = new_length;
1314 s->length = length;
1317 static void
1318 get_s1_spec_stats (struct Spec_list *s1)
1320 get_spec_stats (s1);
1321 if (complement)
1322 s1->length = card_of_complement (s1);
1325 static void
1326 get_s2_spec_stats (struct Spec_list *s2, count len_s1)
1328 get_spec_stats (s2);
1329 if (len_s1 >= s2->length && s2->n_indefinite_repeats == 1)
1331 s2->indefinite_repeat_element->u.repeated_char.repeat_count =
1332 len_s1 - s2->length;
1333 s2->length = len_s1;
1337 static void
1338 spec_init (struct Spec_list *spec_list)
1340 struct List_element *new = xmalloc (sizeof *new);
1341 spec_list->head = spec_list->tail = new;
1342 spec_list->head->next = NULL;
1345 /* This function makes two passes over the argument string S. The first
1346 one converts all \c and \ddd escapes to their one-byte representations.
1347 The second constructs a linked specification list, SPEC_LIST, of the
1348 characters and constructs that comprise the argument string. If either
1349 of these passes detects an error, this function returns false. */
1351 static bool
1352 parse_str (char const *s, struct Spec_list *spec_list)
1354 struct E_string es;
1355 bool ok = unquote (s, &es) && build_spec_list (&es, spec_list);
1356 es_free (&es);
1357 return ok;
1360 /* Given two specification lists, S1 and S2, and assuming that
1361 S1->length > S2->length, append a single [c*n] element to S2 where c
1362 is the last character in the expansion of S2 and n is the difference
1363 between the two lengths.
1364 Upon successful completion, S2->length is set to S1->length. The only
1365 way this function can fail to make S2 as long as S1 is when S2 has
1366 zero-length, since in that case, there is no last character to repeat.
1367 So S2->length is required to be at least 1. */
1369 static void
1370 string2_extend (const struct Spec_list *s1, struct Spec_list *s2)
1372 struct List_element *p;
1373 unsigned char char_to_repeat;
1375 assert (translating);
1376 assert (s1->length > s2->length);
1377 assert (s2->length > 0);
1379 p = s2->tail;
1380 switch (p->type)
1382 case RE_NORMAL_CHAR:
1383 char_to_repeat = p->u.normal_char;
1384 break;
1385 case RE_RANGE:
1386 char_to_repeat = p->u.range.last_char;
1387 break;
1388 case RE_CHAR_CLASS:
1389 /* Note BSD allows extending of classes in string2. For example:
1390 tr '[:upper:]0-9' '[:lower:]'
1391 That's not portable however, contradicts POSIX and is dependent
1392 on your collating sequence. */
1393 die (EXIT_FAILURE, 0,
1394 _("when translating with string1 longer than string2,\nthe\
1395 latter string must not end with a character class"));
1397 case RE_REPEATED_CHAR:
1398 char_to_repeat = p->u.repeated_char.the_repeated_char;
1399 break;
1401 case RE_EQUIV_CLASS:
1402 /* This shouldn't happen, because validate exits with an error
1403 if it finds an equiv class in string2 when translating. */
1404 abort ();
1406 default:
1407 abort ();
1410 append_repeated_char (s2, char_to_repeat, s1->length - s2->length);
1411 s2->length = s1->length;
1414 /* Return true if S is a non-empty list in which exactly one
1415 character (but potentially, many instances of it) appears.
1416 E.g., [X*] or xxxxxxxx. */
1418 static bool
1419 homogeneous_spec_list (struct Spec_list *s)
1421 int b, c;
1423 s->state = BEGIN_STATE;
1425 if ((b = get_next (s, NULL)) == -1)
1426 return false;
1428 while ((c = get_next (s, NULL)) != -1)
1429 if (c != b)
1430 return false;
1432 return true;
1435 /* Die with an error message if S1 and S2 describe strings that
1436 are not valid with the given command line switches.
1437 A side effect of this function is that if a valid [c*] or
1438 [c*0] construct appears in string2, it is converted to [c*n]
1439 with a value for n that makes s2->length == s1->length. By
1440 the same token, if the --truncate-set1 option is not
1441 given, S2 may be extended. */
1443 static void
1444 validate (struct Spec_list *s1, struct Spec_list *s2)
1446 get_s1_spec_stats (s1);
1447 if (s1->n_indefinite_repeats > 0)
1449 die (EXIT_FAILURE, 0,
1450 _("the [c*] repeat construct may not appear in string1"));
1453 if (s2)
1455 get_s2_spec_stats (s2, s1->length);
1457 if (s2->n_indefinite_repeats > 1)
1459 die (EXIT_FAILURE, 0,
1460 _("only one [c*] repeat construct may appear in string2"));
1463 if (translating)
1465 if (s2->has_equiv_class)
1467 die (EXIT_FAILURE, 0,
1468 _("[=c=] expressions may not appear in string2\
1469 when translating"));
1472 if (s2->has_restricted_char_class)
1474 die (EXIT_FAILURE, 0,
1475 _("when translating, the only character classes that may\
1476 appear in\nstring2 are 'upper' and 'lower'"));
1479 validate_case_classes (s1, s2);
1481 if (s1->length > s2->length)
1483 if (!truncate_set1)
1485 /* string2 must be non-empty unless --truncate-set1 is
1486 given or string1 is empty. */
1488 if (s2->length == 0)
1489 die (EXIT_FAILURE, 0,
1490 _("when not truncating set1, string2 must be non-empty"));
1491 string2_extend (s1, s2);
1495 if (complement && s1->has_char_class
1496 && ! (s2->length == s1->length && homogeneous_spec_list (s2)))
1498 die (EXIT_FAILURE, 0,
1499 _("when translating with complemented character classes,\
1500 \nstring2 must map all characters in the domain to one"));
1503 else
1504 /* Not translating. */
1506 if (s2->n_indefinite_repeats > 0)
1507 die (EXIT_FAILURE, 0,
1508 _("the [c*] construct may appear in string2 only\
1509 when translating"));
1514 /* Read buffers of SIZE bytes via the function READER (if READER is
1515 NULL, read from stdin) until EOF. When non-NULL, READER is either
1516 read_and_delete or read_and_xlate. After each buffer is read, it is
1517 processed and written to stdout. The buffers are processed so that
1518 multiple consecutive occurrences of the same character in the input
1519 stream are replaced by a single occurrence of that character if the
1520 character is in the squeeze set. */
1522 static void
1523 squeeze_filter (char *buf, size_t size, size_t (*reader) (char *, size_t))
1525 /* A value distinct from any character that may have been stored in a
1526 buffer as the result of a block-read in the function squeeze_filter. */
1527 const int NOT_A_CHAR = INT_MAX;
1529 int char_to_squeeze = NOT_A_CHAR;
1530 size_t i = 0;
1531 size_t nr = 0;
1533 while (true)
1535 if (i >= nr)
1537 nr = reader (buf, size);
1538 if (nr == 0)
1539 break;
1540 i = 0;
1543 size_t begin = i;
1545 if (char_to_squeeze == NOT_A_CHAR)
1547 size_t out_len;
1548 /* Here, by being a little tricky, we can get a significant
1549 performance increase in most cases when the input is
1550 reasonably large. Since tr will modify the input only
1551 if two consecutive (and identical) input characters are
1552 in the squeeze set, we can step by two through the data
1553 when searching for a character in the squeeze set. This
1554 means there may be a little more work in a few cases and
1555 perhaps twice as much work in the worst cases where most
1556 of the input is removed by squeezing repeats. But most
1557 uses of this functionality seem to remove less than 20-30%
1558 of the input. */
1559 for (; i < nr && !in_squeeze_set[to_uchar (buf[i])]; i += 2)
1560 continue;
1562 /* There is a special case when i == nr and we've just
1563 skipped a character (the last one in buf) that is in
1564 the squeeze set. */
1565 if (i == nr && in_squeeze_set[to_uchar (buf[i - 1])])
1566 --i;
1568 if (i >= nr)
1569 out_len = nr - begin;
1570 else
1572 char_to_squeeze = buf[i];
1573 /* We're about to output buf[begin..i]. */
1574 out_len = i - begin + 1;
1576 /* But since we stepped by 2 in the loop above,
1577 out_len may be one too large. */
1578 if (i > 0 && buf[i - 1] == char_to_squeeze)
1579 --out_len;
1581 /* Advance i to the index of first character to be
1582 considered when looking for a char different from
1583 char_to_squeeze. */
1584 ++i;
1586 if (out_len > 0
1587 && fwrite (&buf[begin], 1, out_len, stdout) != out_len)
1588 die (EXIT_FAILURE, errno, _("write error"));
1591 if (char_to_squeeze != NOT_A_CHAR)
1593 /* Advance i to index of first char != char_to_squeeze
1594 (or to nr if all the rest of the characters in this
1595 buffer are the same as char_to_squeeze). */
1596 for (; i < nr && buf[i] == char_to_squeeze; i++)
1597 continue;
1598 if (i < nr)
1599 char_to_squeeze = NOT_A_CHAR;
1600 /* If (i >= nr) we've squeezed the last character in this buffer.
1601 So now we have to read a new buffer and continue comparing
1602 characters against char_to_squeeze. */
1607 static size_t
1608 plain_read (char *buf, size_t size)
1610 size_t nr = safe_read (STDIN_FILENO, buf, size);
1611 if (nr == SAFE_READ_ERROR)
1612 die (EXIT_FAILURE, errno, _("read error"));
1613 return nr;
1616 /* Read buffers of SIZE bytes from stdin until one is found that
1617 contains at least one character not in the delete set. Store
1618 in the array BUF, all characters from that buffer that are not
1619 in the delete set, and return the number of characters saved
1620 or 0 upon EOF. */
1622 static size_t
1623 read_and_delete (char *buf, size_t size)
1625 size_t n_saved;
1627 /* This enclosing do-while loop is to make sure that
1628 we don't return zero (indicating EOF) when we've
1629 just deleted all the characters in a buffer. */
1632 size_t nr = plain_read (buf, size);
1634 if (nr == 0)
1635 return 0;
1637 /* This first loop may be a waste of code, but gives much
1638 better performance when no characters are deleted in
1639 the beginning of a buffer. It just avoids the copying
1640 of buf[i] into buf[n_saved] when it would be a NOP. */
1642 size_t i;
1643 for (i = 0; i < nr && !in_delete_set[to_uchar (buf[i])]; i++)
1644 continue;
1645 n_saved = i;
1647 for (++i; i < nr; i++)
1648 if (!in_delete_set[to_uchar (buf[i])])
1649 buf[n_saved++] = buf[i];
1651 while (n_saved == 0);
1653 return n_saved;
1656 /* Read at most SIZE bytes from stdin into the array BUF. Then
1657 perform the in-place and one-to-one mapping specified by the global
1658 array 'xlate'. Return the number of characters read, or 0 upon EOF. */
1660 static size_t
1661 read_and_xlate (char *buf, size_t size)
1663 size_t bytes_read = plain_read (buf, size);
1665 for (size_t i = 0; i < bytes_read; i++)
1666 buf[i] = xlate[to_uchar (buf[i])];
1668 return bytes_read;
1671 /* Initialize a boolean membership set, IN_SET, with the character
1672 values obtained by traversing the linked list of constructs S
1673 using the function 'get_next'. IN_SET is expected to have been
1674 initialized to all zeros by the caller. If COMPLEMENT_THIS_SET
1675 is true the resulting set is complemented. */
1677 static void
1678 set_initialize (struct Spec_list *s, bool complement_this_set, bool *in_set)
1680 int c;
1682 s->state = BEGIN_STATE;
1683 while ((c = get_next (s, NULL)) != -1)
1684 in_set[c] = true;
1685 if (complement_this_set)
1686 for (size_t i = 0; i < N_CHARS; i++)
1687 in_set[i] = (!in_set[i]);
1691 main (int argc, char **argv)
1693 int c;
1694 int non_option_args;
1695 int min_operands;
1696 int max_operands;
1697 struct Spec_list buf1, buf2;
1698 struct Spec_list *s1 = &buf1;
1699 struct Spec_list *s2 = &buf2;
1701 initialize_main (&argc, &argv);
1702 set_program_name (argv[0]);
1703 setlocale (LC_ALL, "");
1704 bindtextdomain (PACKAGE, LOCALEDIR);
1705 textdomain (PACKAGE);
1707 atexit (close_stdout);
1709 while ((c = getopt_long (argc, argv, "+cCdst", long_options, NULL)) != -1)
1711 switch (c)
1713 case 'c':
1714 case 'C':
1715 complement = true;
1716 break;
1718 case 'd':
1719 delete = true;
1720 break;
1722 case 's':
1723 squeeze_repeats = true;
1724 break;
1726 case 't':
1727 truncate_set1 = true;
1728 break;
1730 case_GETOPT_HELP_CHAR;
1732 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1734 default:
1735 usage (EXIT_FAILURE);
1736 break;
1740 non_option_args = argc - optind;
1741 translating = (non_option_args == 2 && !delete);
1742 min_operands = 1 + (delete == squeeze_repeats);
1743 max_operands = 1 + (delete <= squeeze_repeats);
1745 if (non_option_args < min_operands)
1747 if (non_option_args == 0)
1748 error (0, 0, _("missing operand"));
1749 else
1751 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
1752 fprintf (stderr, "%s\n",
1753 _(squeeze_repeats
1754 ? N_("Two strings must be given when "
1755 "both deleting and squeezing repeats.")
1756 : N_("Two strings must be given when translating.")));
1758 usage (EXIT_FAILURE);
1761 if (max_operands < non_option_args)
1763 error (0, 0, _("extra operand %s"), quote (argv[optind + max_operands]));
1764 if (non_option_args == 2)
1765 fprintf (stderr, "%s\n",
1766 _("Only one string may be given when "
1767 "deleting without squeezing repeats."));
1768 usage (EXIT_FAILURE);
1771 spec_init (s1);
1772 if (!parse_str (argv[optind], s1))
1773 return EXIT_FAILURE;
1775 if (non_option_args == 2)
1777 spec_init (s2);
1778 if (!parse_str (argv[optind + 1], s2))
1779 return EXIT_FAILURE;
1781 else
1782 s2 = NULL;
1784 validate (s1, s2);
1786 /* Use binary I/O, since 'tr' is sometimes used to transliterate
1787 non-printable characters, or characters which are stripped away
1788 by text-mode reads (like CR and ^Z). */
1789 xset_binary_mode (STDIN_FILENO, O_BINARY);
1790 xset_binary_mode (STDOUT_FILENO, O_BINARY);
1791 fadvise (stdin, FADVISE_SEQUENTIAL);
1793 if (squeeze_repeats && non_option_args == 1)
1795 set_initialize (s1, complement, in_squeeze_set);
1796 squeeze_filter (io_buf, sizeof io_buf, plain_read);
1798 else if (delete && non_option_args == 1)
1800 set_initialize (s1, complement, in_delete_set);
1802 while (true)
1804 size_t nr = read_and_delete (io_buf, sizeof io_buf);
1805 if (nr == 0)
1806 break;
1807 if (fwrite (io_buf, 1, nr, stdout) != nr)
1808 die (EXIT_FAILURE, errno, _("write error"));
1811 else if (squeeze_repeats && delete && non_option_args == 2)
1813 set_initialize (s1, complement, in_delete_set);
1814 set_initialize (s2, false, in_squeeze_set);
1815 squeeze_filter (io_buf, sizeof io_buf, read_and_delete);
1817 else if (translating)
1819 if (complement)
1821 bool *in_s1 = in_delete_set;
1823 set_initialize (s1, false, in_s1);
1824 s2->state = BEGIN_STATE;
1825 for (int i = 0; i < N_CHARS; i++)
1826 xlate[i] = i;
1827 for (int i = 0; i < N_CHARS; i++)
1829 if (!in_s1[i])
1831 int ch = get_next (s2, NULL);
1832 assert (ch != -1 || truncate_set1);
1833 if (ch == -1)
1835 /* This will happen when tr is invoked like e.g.
1836 tr -cs A-Za-z0-9 '\012'. */
1837 break;
1839 xlate[i] = ch;
1843 else
1845 int c1, c2;
1846 enum Upper_Lower_class class_s1;
1847 enum Upper_Lower_class class_s2;
1849 for (int i = 0; i < N_CHARS; i++)
1850 xlate[i] = i;
1851 s1->state = BEGIN_STATE;
1852 s2->state = BEGIN_STATE;
1853 while (true)
1855 c1 = get_next (s1, &class_s1);
1856 c2 = get_next (s2, &class_s2);
1858 if (class_s1 == UL_LOWER && class_s2 == UL_UPPER)
1860 for (int i = 0; i < N_CHARS; i++)
1861 if (islower (i))
1862 xlate[i] = toupper (i);
1864 else if (class_s1 == UL_UPPER && class_s2 == UL_LOWER)
1866 for (int i = 0; i < N_CHARS; i++)
1867 if (isupper (i))
1868 xlate[i] = tolower (i);
1870 else
1872 /* The following should have been checked by validate... */
1873 if (c1 == -1 || c2 == -1)
1874 break;
1875 xlate[c1] = c2;
1878 /* When case-converting, skip the elements as an optimization. */
1879 if (class_s2 != UL_NONE)
1881 skip_construct (s1);
1882 skip_construct (s2);
1885 assert (c1 == -1 || truncate_set1);
1887 if (squeeze_repeats)
1889 set_initialize (s2, false, in_squeeze_set);
1890 squeeze_filter (io_buf, sizeof io_buf, read_and_xlate);
1892 else
1894 while (true)
1896 size_t bytes_read = read_and_xlate (io_buf, sizeof io_buf);
1897 if (bytes_read == 0)
1898 break;
1899 if (fwrite (io_buf, 1, bytes_read, stdout) != bytes_read)
1900 die (EXIT_FAILURE, errno, _("write error"));
1905 if (close (STDIN_FILENO) != 0)
1906 die (EXIT_FAILURE, errno, _("standard input"));
1908 return EXIT_SUCCESS;