Define ENOTSUP, not ENOSYS. Needed on OpenBSD 3.9.
[coreutils/ericb.git] / src / tr.c
blobdff602e6b5d748e683f7ef18c911ab80ecf1cc9b
1 /* tr -- a filter to translate characters
2 Copyright (C) 91, 1995-2007 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 "error.h"
28 #include "quote.h"
29 #include "safe-read.h"
30 #include "xstrtol.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "tr"
35 #define AUTHORS "Jim Meyering"
37 enum { N_CHARS = UCHAR_MAX + 1 };
39 /* An unsigned integer type big enough to hold a repeat count or an
40 unsigned character. POSIX requires support for repeat counts as
41 high as 2**31 - 1. Since repeat counts might need to expand to
42 match the length of an argument string, we need at least size_t to
43 avoid arbitrary internal limits. It doesn't cost much to use
44 uintmax_t, though. */
45 typedef uintmax_t count;
47 /* The value for Spec_list->state that indicates to
48 get_next that it should initialize the tail pointer.
49 Its value should be as large as possible to avoid conflict
50 a valid value for the state field -- and that may be as
51 large as any valid repeat_count. */
52 #define BEGIN_STATE (UINTMAX_MAX - 1)
54 /* The value for Spec_list->state that indicates to
55 get_next that the element pointed to by Spec_list->tail is
56 being considered for the first time on this pass through the
57 list -- it indicates that get_next should make any necessary
58 initializations. */
59 #define NEW_ELEMENT (BEGIN_STATE + 1)
61 /* The maximum possible repeat count. Due to how the states are
62 implemented, it can be as much as BEGIN_STATE. */
63 #define REPEAT_COUNT_MAXIMUM BEGIN_STATE
65 /* The following (but not CC_NO_CLASS) are indices into the array of
66 valid character class strings. */
67 enum Char_class
69 CC_ALNUM = 0, CC_ALPHA = 1, CC_BLANK = 2, CC_CNTRL = 3,
70 CC_DIGIT = 4, CC_GRAPH = 5, CC_LOWER = 6, CC_PRINT = 7,
71 CC_PUNCT = 8, CC_SPACE = 9, CC_UPPER = 10, CC_XDIGIT = 11,
72 CC_NO_CLASS = 9999
75 /* Character class to which a character (returned by get_next) belonged;
76 but it is set only if the construct from which the character was obtained
77 was one of the character classes [:upper:] or [:lower:]. The value
78 is used only when translating and then, only to make sure that upper
79 and lower class constructs have the same relative positions in string1
80 and string2. */
81 enum Upper_Lower_class
83 UL_LOWER,
84 UL_UPPER,
85 UL_NONE
88 /* The type of a List_element. See build_spec_list for more details. */
89 enum Range_element_type
91 RE_NORMAL_CHAR,
92 RE_RANGE,
93 RE_CHAR_CLASS,
94 RE_EQUIV_CLASS,
95 RE_REPEATED_CHAR
98 /* One construct in one of tr's argument strings.
99 For example, consider the POSIX version of the classic tr command:
100 tr -cs 'a-zA-Z_' '[\n*]'
101 String1 has 3 constructs, two of which are ranges (a-z and A-Z),
102 and a single normal character, `_'. String2 has one construct. */
103 struct List_element
105 enum Range_element_type type;
106 struct List_element *next;
107 union
109 unsigned char normal_char;
110 struct /* unnamed */
112 unsigned char first_char;
113 unsigned char last_char;
115 range;
116 enum Char_class char_class;
117 unsigned char equiv_code;
118 struct /* unnamed */
120 unsigned char the_repeated_char;
121 count repeat_count;
123 repeated_char;
128 /* Each of tr's argument strings is parsed into a form that is easier
129 to work with: a linked list of constructs (struct List_element).
130 Each Spec_list structure also encapsulates various attributes of
131 the corresponding argument string. The attributes are used mainly
132 to verify that the strings are valid in the context of any options
133 specified (like -s, -d, or -c). The main exception is the member
134 `tail', which is first used to construct the list. After construction,
135 it is used by get_next to save its state when traversing the list.
136 The member `state' serves a similar function. */
137 struct Spec_list
139 /* Points to the head of the list of range elements.
140 The first struct is a dummy; its members are never used. */
141 struct List_element *head;
143 /* When appending, points to the last element. When traversing via
144 get_next(), points to the element to process next. Setting
145 Spec_list.state to the value BEGIN_STATE before calling get_next
146 signals get_next to initialize tail to point to head->next. */
147 struct List_element *tail;
149 /* Used to save state between calls to get_next. */
150 count state;
152 /* Length, in the sense that length ('a-z[:digit:]123abc')
153 is 42 ( = 26 + 10 + 6). */
154 count length;
156 /* The number of [c*] and [c*0] constructs that appear in this spec. */
157 size_t n_indefinite_repeats;
159 /* If n_indefinite_repeats is nonzero, this points to the List_element
160 corresponding to the last [c*] or [c*0] construct encountered in
161 this spec. Otherwise it is undefined. */
162 struct List_element *indefinite_repeat_element;
164 /* True if this spec contains at least one equivalence
165 class construct e.g. [=c=]. */
166 bool has_equiv_class;
168 /* True if this spec contains at least one character class
169 construct. E.g. [:digit:]. */
170 bool has_char_class;
172 /* True if this spec contains at least one of the character class
173 constructs (all but upper and lower) that aren't allowed in s2. */
174 bool has_restricted_char_class;
177 /* A representation for escaped string1 or string2. As a string is parsed,
178 any backslash-escaped characters (other than octal or \a, \b, \f, \n,
179 etc.) are marked as such in this structure by setting the corresponding
180 entry in the ESCAPED vector. */
181 struct E_string
183 char *s;
184 bool *escaped;
185 size_t len;
188 /* Return nonzero if the Ith character of escaped string ES matches C
189 and is not escaped itself. */
190 static inline bool
191 es_match (struct E_string const *es, size_t i, char c)
193 return es->s[i] == c && !es->escaped[i];
196 /* The name by which this program was run. */
197 char *program_name;
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"
251 enum { N_CHAR_CLASSES = sizeof char_class_name / sizeof char_class_name[0] };
253 /* Array of boolean values. A character `c' is a member of the
254 squeeze set if and only if in_squeeze_set[c] is true. The squeeze
255 set is defined by the last (possibly, the only) string argument
256 on the command line when the squeeze option is given. */
257 static bool in_squeeze_set[N_CHARS];
259 /* Array of boolean values. A character `c' is a member of the
260 delete set if and only if in_delete_set[c] is true. The delete
261 set is defined by the first (or only) string argument on the
262 command line when the delete option is given. */
263 static bool in_delete_set[N_CHARS];
265 /* Array of character values defining the translation (if any) that
266 tr is to perform. Translation is performed only when there are
267 two specification strings and the delete switch is not given. */
268 static char xlate[N_CHARS];
270 static struct option const long_options[] =
272 {"complement", no_argument, NULL, 'c'},
273 {"delete", no_argument, NULL, 'd'},
274 {"squeeze-repeats", no_argument, NULL, 's'},
275 {"truncate-set1", no_argument, NULL, 't'},
276 {GETOPT_HELP_OPTION_DECL},
277 {GETOPT_VERSION_OPTION_DECL},
278 {NULL, 0, NULL, 0}
281 void
282 usage (int status)
284 if (status != EXIT_SUCCESS)
285 fprintf (stderr, _("Try `%s --help' for more information.\n"),
286 program_name);
287 else
289 printf (_("\
290 Usage: %s [OPTION]... SET1 [SET2]\n\
292 program_name);
293 fputs (_("\
294 Translate, squeeze, and/or delete characters from standard input,\n\
295 writing to standard output.\n\
297 -c, -C, --complement first complement SET1\n\
298 -d, --delete delete characters in SET1, do not translate\n\
299 -s, --squeeze-repeats replace each input sequence of a repeated character\n\
300 that is listed in SET1 with a single occurrence\n\
301 of that character\n\
302 -t, --truncate-set1 first truncate SET1 to length of SET2\n\
303 "), stdout);
304 fputs (HELP_OPTION_DESCRIPTION, stdout);
305 fputs (VERSION_OPTION_DESCRIPTION, stdout);
306 fputs (_("\
308 SETs are specified as strings of characters. Most represent themselves.\n\
309 Interpreted sequences are:\n\
311 \\NNN character with octal value NNN (1 to 3 octal digits)\n\
312 \\\\ backslash\n\
313 \\a audible BEL\n\
314 \\b backspace\n\
315 \\f form feed\n\
316 \\n new line\n\
317 \\r return\n\
318 \\t horizontal tab\n\
319 "), stdout);
320 fputs (_("\
321 \\v vertical tab\n\
322 CHAR1-CHAR2 all characters from CHAR1 to CHAR2 in ascending order\n\
323 [CHAR*] in SET2, copies of CHAR until length of SET1\n\
324 [CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0\n\
325 [:alnum:] all letters and digits\n\
326 [:alpha:] all letters\n\
327 [:blank:] all horizontal whitespace\n\
328 [:cntrl:] all control characters\n\
329 [:digit:] all digits\n\
330 "), stdout);
331 fputs (_("\
332 [:graph:] all printable characters, not including space\n\
333 [:lower:] all lower case letters\n\
334 [:print:] all printable characters, including space\n\
335 [:punct:] all punctuation characters\n\
336 [:space:] all horizontal or vertical whitespace\n\
337 [:upper:] all upper case letters\n\
338 [:xdigit:] all hexadecimal digits\n\
339 [=CHAR=] all characters which are equivalent to CHAR\n\
340 "), stdout);
341 fputs (_("\
343 Translation occurs if -d is not given and both SET1 and SET2 appear.\n\
344 -t may be used only when translating. SET2 is extended to length of\n\
345 SET1 by repeating its last character as necessary. \
346 "), stdout);
347 fputs (_("\
348 Excess characters\n\
349 of SET2 are ignored. Only [:lower:] and [:upper:] are guaranteed to\n\
350 expand in ascending order; used in SET2 while translating, they may\n\
351 only be used in pairs to specify case conversion. \
352 "), stdout);
353 fputs (_("\
354 -s uses SET1 if not\n\
355 translating nor deleting; else squeezing uses SET2 and occurs after\n\
356 translation or deletion.\n\
357 "), stdout);
358 emit_bug_reporting_address ();
360 exit (status);
363 /* Return nonzero if the character C is a member of the
364 equivalence class containing the character EQUIV_CLASS. */
366 static inline bool
367 is_equiv_class_member (unsigned char equiv_class, unsigned char c)
369 return (equiv_class == c);
372 /* Return true if the character C is a member of the
373 character class CHAR_CLASS. */
375 static bool
376 is_char_class_member (enum Char_class char_class, unsigned char c)
378 int result;
380 switch (char_class)
382 case CC_ALNUM:
383 result = isalnum (c);
384 break;
385 case CC_ALPHA:
386 result = isalpha (c);
387 break;
388 case CC_BLANK:
389 result = isblank (c);
390 break;
391 case CC_CNTRL:
392 result = iscntrl (c);
393 break;
394 case CC_DIGIT:
395 result = isdigit (c);
396 break;
397 case CC_GRAPH:
398 result = isgraph (c);
399 break;
400 case CC_LOWER:
401 result = islower (c);
402 break;
403 case CC_PRINT:
404 result = isprint (c);
405 break;
406 case CC_PUNCT:
407 result = ispunct (c);
408 break;
409 case CC_SPACE:
410 result = isspace (c);
411 break;
412 case CC_UPPER:
413 result = isupper (c);
414 break;
415 case CC_XDIGIT:
416 result = isxdigit (c);
417 break;
418 default:
419 abort ();
420 break;
423 return !! result;
426 static void
427 es_free (struct E_string *es)
429 free (es->s);
430 free (es->escaped);
433 /* Perform the first pass over each range-spec argument S, converting all
434 \c and \ddd escapes to their one-byte representations. If an invalid
435 quote sequence is found print an error message and return false;
436 Otherwise set *ES to the resulting string and return true.
437 The resulting array of characters may contain zero-bytes;
438 however, on input, S is assumed to be null-terminated, and hence
439 cannot contain actual (non-escaped) zero bytes. */
441 static bool
442 unquote (char const *s, struct E_string *es)
444 size_t i, j;
445 size_t len = strlen (s);
447 es->s = xmalloc (len);
448 es->escaped = xcalloc (len, sizeof es->escaped[0]);
450 j = 0;
451 for (i = 0; s[i]; i++)
453 unsigned char c;
454 int oct_digit;
456 switch (s[i])
458 case '\\':
459 es->escaped[j] = true;
460 switch (s[i + 1])
462 case '\\':
463 c = '\\';
464 break;
465 case 'a':
466 c = '\a';
467 break;
468 case 'b':
469 c = '\b';
470 break;
471 case 'f':
472 c = '\f';
473 break;
474 case 'n':
475 c = '\n';
476 break;
477 case 'r':
478 c = '\r';
479 break;
480 case 't':
481 c = '\t';
482 break;
483 case 'v':
484 c = '\v';
485 break;
486 case '0':
487 case '1':
488 case '2':
489 case '3':
490 case '4':
491 case '5':
492 case '6':
493 case '7':
494 c = s[i + 1] - '0';
495 oct_digit = s[i + 2] - '0';
496 if (0 <= oct_digit && oct_digit <= 7)
498 c = 8 * c + oct_digit;
499 ++i;
500 oct_digit = s[i + 2] - '0';
501 if (0 <= oct_digit && oct_digit <= 7)
503 if (8 * c + oct_digit < N_CHARS)
505 c = 8 * c + oct_digit;
506 ++i;
508 else
510 /* A 3-digit octal number larger than \377 won't
511 fit in 8 bits. So we stop when adding the
512 next digit would put us over the limit and
513 give a warning about the ambiguity. POSIX
514 isn't clear on this, and we interpret this
515 lack of clarity as meaning the resulting behavior
516 is undefined, which means we're allowed to issue
517 a warning. */
518 error (0, 0, _("warning: the ambiguous octal escape \
519 \\%c%c%c is being\n\tinterpreted as the 2-byte sequence \\0%c%c, %c"),
520 s[i], s[i + 1], s[i + 2],
521 s[i], s[i + 1], s[i + 2]);
525 break;
526 case '\0':
527 error (0, 0, _("warning: an unescaped backslash "
528 "at end of string is not portable"));
529 /* POSIX is not clear about this. */
530 es->escaped[j] = false;
531 i--;
532 c = '\\';
533 break;
534 default:
535 c = s[i + 1];
536 break;
538 ++i;
539 es->s[j++] = c;
540 break;
541 default:
542 es->s[j++] = s[i];
543 break;
546 es->len = j;
547 return true;
550 /* If CLASS_STR is a valid character class string, return its index
551 in the global char_class_name array. Otherwise, return CC_NO_CLASS. */
553 static enum Char_class
554 look_up_char_class (char const *class_str, size_t len)
556 enum Char_class i;
558 for (i = 0; i < N_CHAR_CLASSES; i++)
559 if (strncmp (class_str, char_class_name[i], len) == 0
560 && strlen (char_class_name[i]) == len)
561 return i;
562 return CC_NO_CLASS;
565 /* Return a newly allocated string with a printable version of C.
566 This function is used solely for formatting error messages. */
568 static char *
569 make_printable_char (unsigned char c)
571 char *buf = xmalloc (5);
573 if (isprint (c))
575 buf[0] = c;
576 buf[1] = '\0';
578 else
580 sprintf (buf, "\\%03o", c);
582 return buf;
585 /* Return a newly allocated copy of S which is suitable for printing.
586 LEN is the number of characters in S. Most non-printing
587 (isprint) characters are represented by a backslash followed by
588 3 octal digits. However, the characters represented by \c escapes
589 where c is one of [abfnrtv] are represented by their 2-character \c
590 sequences. This function is used solely for printing error messages. */
592 static char *
593 make_printable_str (char const *s, size_t len)
595 /* Worst case is that every character expands to a backslash
596 followed by a 3-character octal escape sequence. */
597 char *printable_buf = xnmalloc (len + 1, 4);
598 char *p = printable_buf;
599 size_t i;
601 for (i = 0; i < len; i++)
603 char buf[5];
604 char const *tmp = NULL;
605 unsigned char c = s[i];
607 switch (c)
609 case '\\':
610 tmp = "\\";
611 break;
612 case '\a':
613 tmp = "\\a";
614 break;
615 case '\b':
616 tmp = "\\b";
617 break;
618 case '\f':
619 tmp = "\\f";
620 break;
621 case '\n':
622 tmp = "\\n";
623 break;
624 case '\r':
625 tmp = "\\r";
626 break;
627 case '\t':
628 tmp = "\\t";
629 break;
630 case '\v':
631 tmp = "\\v";
632 break;
633 default:
634 if (isprint (c))
636 buf[0] = c;
637 buf[1] = '\0';
639 else
640 sprintf (buf, "\\%03o", c);
641 tmp = buf;
642 break;
644 p = stpcpy (p, tmp);
646 return printable_buf;
649 /* Append a newly allocated structure representing a
650 character C to the specification list LIST. */
652 static void
653 append_normal_char (struct Spec_list *list, unsigned char c)
655 struct List_element *new;
657 new = xmalloc (sizeof *new);
658 new->next = NULL;
659 new->type = RE_NORMAL_CHAR;
660 new->u.normal_char = c;
661 assert (list->tail);
662 list->tail->next = new;
663 list->tail = new;
666 /* Append a newly allocated structure representing the range
667 of characters from FIRST to LAST to the specification list LIST.
668 Return false if LAST precedes FIRST in the collating sequence,
669 true otherwise. This means that '[c-c]' is acceptable. */
671 static bool
672 append_range (struct Spec_list *list, unsigned char first, unsigned char last)
674 struct List_element *new;
676 if (last < first)
678 char *tmp1 = make_printable_char (first);
679 char *tmp2 = make_printable_char (last);
681 error (0, 0,
682 _("range-endpoints of `%s-%s' are in reverse collating sequence order"),
683 tmp1, tmp2);
684 free (tmp1);
685 free (tmp2);
686 return false;
688 new = xmalloc (sizeof *new);
689 new->next = NULL;
690 new->type = RE_RANGE;
691 new->u.range.first_char = first;
692 new->u.range.last_char = last;
693 assert (list->tail);
694 list->tail->next = new;
695 list->tail = new;
696 return true;
699 /* If CHAR_CLASS_STR is a valid character class string, append a
700 newly allocated structure representing that character class to the end
701 of the specification list LIST and return true. If CHAR_CLASS_STR is not
702 a valid string return false. */
704 static bool
705 append_char_class (struct Spec_list *list,
706 char const *char_class_str, size_t len)
708 enum Char_class char_class;
709 struct List_element *new;
711 char_class = look_up_char_class (char_class_str, len);
712 if (char_class == CC_NO_CLASS)
713 return false;
714 new = xmalloc (sizeof *new);
715 new->next = NULL;
716 new->type = RE_CHAR_CLASS;
717 new->u.char_class = char_class;
718 assert (list->tail);
719 list->tail->next = new;
720 list->tail = new;
721 return true;
724 /* Append a newly allocated structure representing a [c*n]
725 repeated character construct to the specification list LIST.
726 THE_CHAR is the single character to be repeated, and REPEAT_COUNT
727 is a non-negative repeat count. */
729 static void
730 append_repeated_char (struct Spec_list *list, unsigned char the_char,
731 count repeat_count)
733 struct List_element *new;
735 new = xmalloc (sizeof *new);
736 new->next = NULL;
737 new->type = RE_REPEATED_CHAR;
738 new->u.repeated_char.the_repeated_char = the_char;
739 new->u.repeated_char.repeat_count = repeat_count;
740 assert (list->tail);
741 list->tail->next = new;
742 list->tail = new;
745 /* Given a string, EQUIV_CLASS_STR, from a [=str=] context and
746 the length of that string, LEN, if LEN is exactly one, append
747 a newly allocated structure representing the specified
748 equivalence class to the specification list, LIST and return true.
749 If LEN is not 1, return false. */
751 static bool
752 append_equiv_class (struct Spec_list *list,
753 char const *equiv_class_str, size_t len)
755 struct List_element *new;
757 if (len != 1)
758 return false;
759 new = xmalloc (sizeof *new);
760 new->next = NULL;
761 new->type = RE_EQUIV_CLASS;
762 new->u.equiv_code = *equiv_class_str;
763 assert (list->tail);
764 list->tail->next = new;
765 list->tail = new;
766 return true;
769 /* Search forward starting at START_IDX for the 2-char sequence
770 (PRE_BRACKET_CHAR,']') in the string P of length P_LEN. If such
771 a sequence is found, set *RESULT_IDX to the index of the first
772 character and return true. Otherwise return false. P may contain
773 zero bytes. */
775 static bool
776 find_closing_delim (const struct E_string *es, size_t start_idx,
777 char pre_bracket_char, size_t *result_idx)
779 size_t i;
781 for (i = start_idx; i < es->len - 1; i++)
782 if (es->s[i] == pre_bracket_char && es->s[i + 1] == ']'
783 && !es->escaped[i] && !es->escaped[i + 1])
785 *result_idx = i;
786 return true;
788 return false;
791 /* Parse the bracketed repeat-char syntax. If the P_LEN characters
792 beginning with P[ START_IDX ] comprise a valid [c*n] construct,
793 then set *CHAR_TO_REPEAT, *REPEAT_COUNT, and *CLOSING_BRACKET_IDX
794 and return zero. If the second character following
795 the opening bracket is not `*' or if no closing bracket can be
796 found, return -1. If a closing bracket is found and the
797 second char is `*', but the string between the `*' and `]' isn't
798 empty, an octal number, or a decimal number, print an error message
799 and return -2. */
801 static int
802 find_bracketed_repeat (const struct E_string *es, size_t start_idx,
803 unsigned char *char_to_repeat, count *repeat_count,
804 size_t *closing_bracket_idx)
806 size_t i;
808 assert (start_idx + 1 < es->len);
809 if (!es_match (es, start_idx + 1, '*'))
810 return -1;
812 for (i = start_idx + 2; i < es->len && !es->escaped[i]; i++)
814 if (es->s[i] == ']')
816 size_t digit_str_len = i - start_idx - 2;
818 *char_to_repeat = es->s[start_idx];
819 if (digit_str_len == 0)
821 /* We've matched [c*] -- no explicit repeat count. */
822 *repeat_count = 0;
824 else
826 /* Here, we have found [c*s] where s should be a string
827 of octal (if it starts with `0') or decimal digits. */
828 char const *digit_str = &es->s[start_idx + 2];
829 char *d_end;
830 if ((xstrtoumax (digit_str, &d_end, *digit_str == '0' ? 8 : 10,
831 repeat_count, NULL)
832 != LONGINT_OK)
833 || REPEAT_COUNT_MAXIMUM < *repeat_count
834 || digit_str + digit_str_len != d_end)
836 char *tmp = make_printable_str (digit_str, digit_str_len);
837 error (0, 0,
838 _("invalid repeat count %s in [c*n] construct"),
839 quote (tmp));
840 free (tmp);
841 return -2;
844 *closing_bracket_idx = i;
845 return 0;
848 return -1; /* No bracket found. */
851 /* Return true if the string at ES->s[IDX] matches the regular
852 expression `\*[0-9]*\]', false otherwise. The string does not
853 match if any of its characters are escaped. */
855 static bool
856 star_digits_closebracket (const struct E_string *es, size_t idx)
858 size_t i;
860 if (!es_match (es, idx, '*'))
861 return false;
863 for (i = idx + 1; i < es->len; i++)
864 if (!ISDIGIT (to_uchar (es->s[i])) || es->escaped[i])
865 return es_match (es, i, ']');
866 return false;
869 /* Convert string UNESCAPED_STRING (which has been preprocessed to
870 convert backslash-escape sequences) of length LEN characters into
871 a linked list of the following 5 types of constructs:
872 - [:str:] Character class where `str' is one of the 12 valid strings.
873 - [=c=] Equivalence class where `c' is any single character.
874 - [c*n] Repeat the single character `c' `n' times. n may be omitted.
875 However, if `n' is present, it must be a non-negative octal or
876 decimal integer.
877 - r-s Range of characters from `r' to `s'. The second endpoint must
878 not precede the first in the current collating sequence.
879 - c Any other character is interpreted as itself. */
881 static bool
882 build_spec_list (const struct E_string *es, struct Spec_list *result)
884 char const *p;
885 size_t i;
887 p = es->s;
889 /* The main for-loop below recognizes the 4 multi-character constructs.
890 A character that matches (in its context) none of the multi-character
891 constructs is classified as `normal'. Since all multi-character
892 constructs have at least 3 characters, any strings of length 2 or
893 less are composed solely of normal characters. Hence, the index of
894 the outer for-loop runs only as far as LEN-2. */
896 for (i = 0; i + 2 < es->len; /* empty */)
898 if (es_match (es, i, '['))
900 bool matched_multi_char_construct;
901 size_t closing_bracket_idx;
902 unsigned char char_to_repeat;
903 count repeat_count;
904 int err;
906 matched_multi_char_construct = true;
907 if (es_match (es, i + 1, ':') || es_match (es, i + 1, '='))
909 size_t closing_delim_idx;
911 if (find_closing_delim (es, i + 2, p[i + 1], &closing_delim_idx))
913 size_t opnd_str_len = closing_delim_idx - 1 - (i + 2) + 1;
914 char const *opnd_str = p + i + 2;
916 if (opnd_str_len == 0)
918 if (p[i + 1] == ':')
919 error (0, 0, _("missing character class name `[::]'"));
920 else
921 error (0, 0,
922 _("missing equivalence class character `[==]'"));
923 return false;
926 if (p[i + 1] == ':')
928 /* FIXME: big comment. */
929 if (!append_char_class (result, opnd_str, opnd_str_len))
931 if (star_digits_closebracket (es, i + 2))
932 goto try_bracketed_repeat;
933 else
935 char *tmp = make_printable_str (opnd_str,
936 opnd_str_len);
937 error (0, 0, _("invalid character class %s"),
938 quote (tmp));
939 free (tmp);
940 return false;
944 else
946 /* FIXME: big comment. */
947 if (!append_equiv_class (result, opnd_str, opnd_str_len))
949 if (star_digits_closebracket (es, i + 2))
950 goto try_bracketed_repeat;
951 else
953 char *tmp = make_printable_str (opnd_str,
954 opnd_str_len);
955 error (0, 0,
956 _("%s: equivalence class operand must be a single character"),
957 tmp);
958 free (tmp);
959 return false;
964 i = closing_delim_idx + 2;
965 continue;
967 /* Else fall through. This could be [:*] or [=*]. */
970 try_bracketed_repeat:
972 /* Determine whether this is a bracketed repeat range
973 matching the RE \[.\*(dec_or_oct_number)?\]. */
974 err = find_bracketed_repeat (es, i + 1, &char_to_repeat,
975 &repeat_count,
976 &closing_bracket_idx);
977 if (err == 0)
979 append_repeated_char (result, char_to_repeat, repeat_count);
980 i = closing_bracket_idx + 1;
982 else if (err == -1)
984 matched_multi_char_construct = false;
986 else
988 /* Found a string that looked like [c*n] but the
989 numeric part was invalid. */
990 return false;
993 if (matched_multi_char_construct)
994 continue;
996 /* We reach this point if P does not match [:str:], [=c=],
997 [c*n], or [c*]. Now, see if P looks like a range `[-c'
998 (from `[' to `c'). */
1001 /* Look ahead one char for ranges like a-z. */
1002 if (es_match (es, i + 1, '-'))
1004 if (!append_range (result, p[i], p[i + 2]))
1005 return false;
1006 i += 3;
1008 else
1010 append_normal_char (result, p[i]);
1011 ++i;
1015 /* Now handle the (2 or fewer) remaining characters p[i]..p[es->len - 1]. */
1016 for (; i < es->len; i++)
1017 append_normal_char (result, p[i]);
1019 return true;
1022 /* Given a Spec_list S (with its saved state implicit in the values
1023 of its members `tail' and `state'), return the next single character
1024 in the expansion of S's constructs. If the last character of S was
1025 returned on the previous call or if S was empty, this function
1026 returns -1. For example, successive calls to get_next where S
1027 represents the spec-string 'a-d[y*3]' will return the sequence
1028 of values a, b, c, d, y, y, y, -1. Finally, if the construct from
1029 which the returned character comes is [:upper:] or [:lower:], the
1030 parameter CLASS is given a value to indicate which it was. Otherwise
1031 CLASS is set to UL_NONE. This value is used only when constructing
1032 the translation table to verify that any occurrences of upper and
1033 lower class constructs in the spec-strings appear in the same relative
1034 positions. */
1036 static int
1037 get_next (struct Spec_list *s, enum Upper_Lower_class *class)
1039 struct List_element *p;
1040 int return_val;
1041 int i;
1043 if (class)
1044 *class = UL_NONE;
1046 if (s->state == BEGIN_STATE)
1048 s->tail = s->head->next;
1049 s->state = NEW_ELEMENT;
1052 p = s->tail;
1053 if (p == NULL)
1054 return -1;
1056 switch (p->type)
1058 case RE_NORMAL_CHAR:
1059 return_val = p->u.normal_char;
1060 s->state = NEW_ELEMENT;
1061 s->tail = p->next;
1062 break;
1064 case RE_RANGE:
1065 if (s->state == NEW_ELEMENT)
1066 s->state = p->u.range.first_char;
1067 else
1068 ++(s->state);
1069 return_val = s->state;
1070 if (s->state == p->u.range.last_char)
1072 s->tail = p->next;
1073 s->state = NEW_ELEMENT;
1075 break;
1077 case RE_CHAR_CLASS:
1078 if (class)
1080 switch (p->u.char_class)
1082 case CC_LOWER:
1083 *class = UL_LOWER;
1084 break;
1085 case CC_UPPER:
1086 *class = UL_UPPER;
1087 break;
1088 default:
1089 break;
1093 if (s->state == NEW_ELEMENT)
1095 for (i = 0; i < N_CHARS; i++)
1096 if (is_char_class_member (p->u.char_class, i))
1097 break;
1098 assert (i < N_CHARS);
1099 s->state = i;
1101 assert (is_char_class_member (p->u.char_class, s->state));
1102 return_val = s->state;
1103 for (i = s->state + 1; i < N_CHARS; i++)
1104 if (is_char_class_member (p->u.char_class, i))
1105 break;
1106 if (i < N_CHARS)
1107 s->state = i;
1108 else
1110 s->tail = p->next;
1111 s->state = NEW_ELEMENT;
1113 break;
1115 case RE_EQUIV_CLASS:
1116 /* FIXME: this assumes that each character is alone in its own
1117 equivalence class (which appears to be correct for my
1118 LC_COLLATE. But I don't know of any function that allows
1119 one to determine a character's equivalence class. */
1121 return_val = p->u.equiv_code;
1122 s->state = NEW_ELEMENT;
1123 s->tail = p->next;
1124 break;
1126 case RE_REPEATED_CHAR:
1127 /* Here, a repeat count of n == 0 means don't repeat at all. */
1128 if (p->u.repeated_char.repeat_count == 0)
1130 s->tail = p->next;
1131 s->state = NEW_ELEMENT;
1132 return_val = get_next (s, class);
1134 else
1136 if (s->state == NEW_ELEMENT)
1138 s->state = 0;
1140 ++(s->state);
1141 return_val = p->u.repeated_char.the_repeated_char;
1142 if (s->state == p->u.repeated_char.repeat_count)
1144 s->tail = p->next;
1145 s->state = NEW_ELEMENT;
1148 break;
1150 default:
1151 abort ();
1152 break;
1155 return return_val;
1158 /* This is a minor kludge. This function is called from
1159 get_spec_stats to determine the cardinality of a set derived
1160 from a complemented string. It's a kludge in that some of the
1161 same operations are (duplicated) performed in set_initialize. */
1163 static int
1164 card_of_complement (struct Spec_list *s)
1166 int c;
1167 int cardinality = N_CHARS;
1168 bool in_set[N_CHARS] = { 0, };
1170 s->state = BEGIN_STATE;
1171 while ((c = get_next (s, NULL)) != -1)
1173 cardinality -= (!in_set[c]);
1174 in_set[c] = true;
1176 return cardinality;
1179 /* Gather statistics about the spec-list S in preparation for the tests
1180 in validate that determine the consistency of the specs. This function
1181 is called at most twice; once for string1, and again for any string2.
1182 LEN_S1 < 0 indicates that this is the first call and that S represents
1183 string1. When LEN_S1 >= 0, it is the length of the expansion of the
1184 constructs in string1, and we can use its value to resolve any
1185 indefinite repeat construct in S (which represents string2). Hence,
1186 this function has the side-effect that it converts a valid [c*]
1187 construct in string2 to [c*n] where n is large enough (or 0) to give
1188 string2 the same length as string1. For example, with the command
1189 tr a-z 'A[\n*]Z' on the second call to get_spec_stats, LEN_S1 would
1190 be 26 and S (representing string2) would be converted to 'A[\n*24]Z'. */
1192 static void
1193 get_spec_stats (struct Spec_list *s)
1195 struct List_element *p;
1196 count length = 0;
1198 s->n_indefinite_repeats = 0;
1199 s->has_equiv_class = false;
1200 s->has_restricted_char_class = false;
1201 s->has_char_class = false;
1202 for (p = s->head->next; p; p = p->next)
1204 int i;
1205 count len = 0;
1206 count new_length;
1208 switch (p->type)
1210 case RE_NORMAL_CHAR:
1211 len = 1;
1212 break;
1214 case RE_RANGE:
1215 assert (p->u.range.last_char >= p->u.range.first_char);
1216 len = p->u.range.last_char - p->u.range.first_char + 1;
1217 break;
1219 case RE_CHAR_CLASS:
1220 s->has_char_class = true;
1221 for (i = 0; i < N_CHARS; i++)
1222 if (is_char_class_member (p->u.char_class, i))
1223 ++len;
1224 switch (p->u.char_class)
1226 case CC_UPPER:
1227 case CC_LOWER:
1228 break;
1229 default:
1230 s->has_restricted_char_class = true;
1231 break;
1233 break;
1235 case RE_EQUIV_CLASS:
1236 for (i = 0; i < N_CHARS; i++)
1237 if (is_equiv_class_member (p->u.equiv_code, i))
1238 ++len;
1239 s->has_equiv_class = true;
1240 break;
1242 case RE_REPEATED_CHAR:
1243 if (p->u.repeated_char.repeat_count > 0)
1244 len = p->u.repeated_char.repeat_count;
1245 else
1247 s->indefinite_repeat_element = p;
1248 ++(s->n_indefinite_repeats);
1250 break;
1252 default:
1253 abort ();
1254 break;
1257 /* Check for arithmetic overflow in computing length. Also, reject
1258 any length greater than the maximum repeat count, in case the
1259 length is later used to compute the repeat count for an
1260 indefinite element. */
1261 new_length = length + len;
1262 if (! (length <= new_length && new_length <= REPEAT_COUNT_MAXIMUM))
1263 error (EXIT_FAILURE, 0, _("too many characters in set"));
1264 length = new_length;
1267 s->length = length;
1270 static void
1271 get_s1_spec_stats (struct Spec_list *s1)
1273 get_spec_stats (s1);
1274 if (complement)
1275 s1->length = card_of_complement (s1);
1278 static void
1279 get_s2_spec_stats (struct Spec_list *s2, count len_s1)
1281 get_spec_stats (s2);
1282 if (len_s1 >= s2->length && s2->n_indefinite_repeats == 1)
1284 s2->indefinite_repeat_element->u.repeated_char.repeat_count =
1285 len_s1 - s2->length;
1286 s2->length = len_s1;
1290 static void
1291 spec_init (struct Spec_list *spec_list)
1293 struct List_element *new = xmalloc (sizeof *new);
1294 spec_list->head = spec_list->tail = new;
1295 spec_list->head->next = NULL;
1298 /* This function makes two passes over the argument string S. The first
1299 one converts all \c and \ddd escapes to their one-byte representations.
1300 The second constructs a linked specification list, SPEC_LIST, of the
1301 characters and constructs that comprise the argument string. If either
1302 of these passes detects an error, this function returns false. */
1304 static bool
1305 parse_str (char const *s, struct Spec_list *spec_list)
1307 struct E_string es;
1308 bool ok = unquote (s, &es) && build_spec_list (&es, spec_list);
1309 es_free (&es);
1310 return ok;
1313 /* Given two specification lists, S1 and S2, and assuming that
1314 S1->length > S2->length, append a single [c*n] element to S2 where c
1315 is the last character in the expansion of S2 and n is the difference
1316 between the two lengths.
1317 Upon successful completion, S2->length is set to S1->length. The only
1318 way this function can fail to make S2 as long as S1 is when S2 has
1319 zero-length, since in that case, there is no last character to repeat.
1320 So S2->length is required to be at least 1.
1322 Providing this functionality allows the user to do some pretty
1323 non-BSD (and non-portable) things: For example, the command
1324 tr -cs '[:upper:]0-9' '[:lower:]'
1325 is almost guaranteed to give results that depend on your collating
1326 sequence. */
1328 static void
1329 string2_extend (const struct Spec_list *s1, struct Spec_list *s2)
1331 struct List_element *p;
1332 unsigned char char_to_repeat;
1333 int i;
1335 assert (translating);
1336 assert (s1->length > s2->length);
1337 assert (s2->length > 0);
1339 p = s2->tail;
1340 switch (p->type)
1342 case RE_NORMAL_CHAR:
1343 char_to_repeat = p->u.normal_char;
1344 break;
1345 case RE_RANGE:
1346 char_to_repeat = p->u.range.last_char;
1347 break;
1348 case RE_CHAR_CLASS:
1349 for (i = N_CHARS - 1; i >= 0; i--)
1350 if (is_char_class_member (p->u.char_class, i))
1351 break;
1352 assert (i >= 0);
1353 char_to_repeat = i;
1354 break;
1356 case RE_REPEATED_CHAR:
1357 char_to_repeat = p->u.repeated_char.the_repeated_char;
1358 break;
1360 case RE_EQUIV_CLASS:
1361 /* This shouldn't happen, because validate exits with an error
1362 if it finds an equiv class in string2 when translating. */
1363 abort ();
1364 break;
1366 default:
1367 abort ();
1368 break;
1371 append_repeated_char (s2, char_to_repeat, s1->length - s2->length);
1372 s2->length = s1->length;
1375 /* Return true if S is a non-empty list in which exactly one
1376 character (but potentially, many instances of it) appears.
1377 E.g., [X*] or xxxxxxxx. */
1379 static bool
1380 homogeneous_spec_list (struct Spec_list *s)
1382 int b, c;
1384 s->state = BEGIN_STATE;
1386 if ((b = get_next (s, NULL)) == -1)
1387 return false;
1389 while ((c = get_next (s, NULL)) != -1)
1390 if (c != b)
1391 return false;
1393 return true;
1396 /* Die with an error message if S1 and S2 describe strings that
1397 are not valid with the given command line switches.
1398 A side effect of this function is that if a valid [c*] or
1399 [c*0] construct appears in string2, it is converted to [c*n]
1400 with a value for n that makes s2->length == s1->length. By
1401 the same token, if the --truncate-set1 option is not
1402 given, S2 may be extended. */
1404 static void
1405 validate (struct Spec_list *s1, struct Spec_list *s2)
1407 get_s1_spec_stats (s1);
1408 if (s1->n_indefinite_repeats > 0)
1410 error (EXIT_FAILURE, 0,
1411 _("the [c*] repeat construct may not appear in string1"));
1414 if (s2)
1416 get_s2_spec_stats (s2, s1->length);
1418 if (s2->n_indefinite_repeats > 1)
1420 error (EXIT_FAILURE, 0,
1421 _("only one [c*] repeat construct may appear in string2"));
1424 if (translating)
1426 if (s2->has_equiv_class)
1428 error (EXIT_FAILURE, 0,
1429 _("[=c=] expressions may not appear in string2 \
1430 when translating"));
1433 if (s1->length > s2->length)
1435 if (!truncate_set1)
1437 /* string2 must be non-empty unless --truncate-set1 is
1438 given or string1 is empty. */
1440 if (s2->length == 0)
1441 error (EXIT_FAILURE, 0,
1442 _("when not truncating set1, string2 must be non-empty"));
1443 string2_extend (s1, s2);
1447 if (complement && s1->has_char_class
1448 && ! (s2->length == s1->length && homogeneous_spec_list (s2)))
1450 error (EXIT_FAILURE, 0,
1451 _("when translating with complemented character classes,\
1452 \nstring2 must map all characters in the domain to one"));
1455 if (s2->has_restricted_char_class)
1457 error (EXIT_FAILURE, 0,
1458 _("when translating, the only character classes that may \
1459 appear in\nstring2 are `upper' and `lower'"));
1462 else
1463 /* Not translating. */
1465 if (s2->n_indefinite_repeats > 0)
1466 error (EXIT_FAILURE, 0,
1467 _("the [c*] construct may appear in string2 only \
1468 when translating"));
1473 /* Read buffers of SIZE bytes via the function READER (if READER is
1474 NULL, read from stdin) until EOF. When non-NULL, READER is either
1475 read_and_delete or read_and_xlate. After each buffer is read, it is
1476 processed and written to stdout. The buffers are processed so that
1477 multiple consecutive occurrences of the same character in the input
1478 stream are replaced by a single occurrence of that character if the
1479 character is in the squeeze set. */
1481 static void
1482 squeeze_filter (char *buf, size_t size, size_t (*reader) (char *, size_t))
1484 /* A value distinct from any character that may have been stored in a
1485 buffer as the result of a block-read in the function squeeze_filter. */
1486 enum { NOT_A_CHAR = CHAR_MAX + 1 };
1488 int char_to_squeeze = NOT_A_CHAR;
1489 size_t i = 0;
1490 size_t nr = 0;
1492 for (;;)
1494 size_t begin;
1496 if (i >= nr)
1498 nr = reader (buf, size);
1499 if (nr == 0)
1500 break;
1501 i = 0;
1504 begin = i;
1506 if (char_to_squeeze == NOT_A_CHAR)
1508 size_t out_len;
1509 /* Here, by being a little tricky, we can get a significant
1510 performance increase in most cases when the input is
1511 reasonably large. Since tr will modify the input only
1512 if two consecutive (and identical) input characters are
1513 in the squeeze set, we can step by two through the data
1514 when searching for a character in the squeeze set. This
1515 means there may be a little more work in a few cases and
1516 perhaps twice as much work in the worst cases where most
1517 of the input is removed by squeezing repeats. But most
1518 uses of this functionality seem to remove less than 20-30%
1519 of the input. */
1520 for (; i < nr && !in_squeeze_set[to_uchar (buf[i])]; i += 2)
1521 continue;
1523 /* There is a special case when i == nr and we've just
1524 skipped a character (the last one in buf) that is in
1525 the squeeze set. */
1526 if (i == nr && in_squeeze_set[to_uchar (buf[i - 1])])
1527 --i;
1529 if (i >= nr)
1530 out_len = nr - begin;
1531 else
1533 char_to_squeeze = buf[i];
1534 /* We're about to output buf[begin..i]. */
1535 out_len = i - begin + 1;
1537 /* But since we stepped by 2 in the loop above,
1538 out_len may be one too large. */
1539 if (i > 0 && buf[i - 1] == char_to_squeeze)
1540 --out_len;
1542 /* Advance i to the index of first character to be
1543 considered when looking for a char different from
1544 char_to_squeeze. */
1545 ++i;
1547 if (out_len > 0
1548 && fwrite (&buf[begin], 1, out_len, stdout) != out_len)
1549 error (EXIT_FAILURE, errno, _("write error"));
1552 if (char_to_squeeze != NOT_A_CHAR)
1554 /* Advance i to index of first char != char_to_squeeze
1555 (or to nr if all the rest of the characters in this
1556 buffer are the same as char_to_squeeze). */
1557 for (; i < nr && buf[i] == char_to_squeeze; i++)
1558 continue;
1559 if (i < nr)
1560 char_to_squeeze = NOT_A_CHAR;
1561 /* If (i >= nr) we've squeezed the last character in this buffer.
1562 So now we have to read a new buffer and continue comparing
1563 characters against char_to_squeeze. */
1568 static size_t
1569 plain_read (char *buf, size_t size)
1571 size_t nr = safe_read (STDIN_FILENO, buf, size);
1572 if (nr == SAFE_READ_ERROR)
1573 error (EXIT_FAILURE, errno, _("read error"));
1574 return nr;
1577 /* Read buffers of SIZE bytes from stdin until one is found that
1578 contains at least one character not in the delete set. Store
1579 in the array BUF, all characters from that buffer that are not
1580 in the delete set, and return the number of characters saved
1581 or 0 upon EOF. */
1583 static size_t
1584 read_and_delete (char *buf, size_t size)
1586 size_t n_saved;
1588 /* This enclosing do-while loop is to make sure that
1589 we don't return zero (indicating EOF) when we've
1590 just deleted all the characters in a buffer. */
1593 size_t i;
1594 size_t nr = plain_read (buf, size);
1596 if (nr == 0)
1597 return 0;
1599 /* This first loop may be a waste of code, but gives much
1600 better performance when no characters are deleted in
1601 the beginning of a buffer. It just avoids the copying
1602 of buf[i] into buf[n_saved] when it would be a NOP. */
1604 for (i = 0; i < nr && !in_delete_set[to_uchar (buf[i])]; i++)
1605 continue;
1606 n_saved = i;
1608 for (++i; i < nr; i++)
1609 if (!in_delete_set[to_uchar (buf[i])])
1610 buf[n_saved++] = buf[i];
1612 while (n_saved == 0);
1614 return n_saved;
1617 /* Read at most SIZE bytes from stdin into the array BUF. Then
1618 perform the in-place and one-to-one mapping specified by the global
1619 array `xlate'. Return the number of characters read, or 0 upon EOF. */
1621 static size_t
1622 read_and_xlate (char *buf, size_t size)
1624 size_t bytes_read = plain_read (buf, size);
1625 size_t i;
1627 for (i = 0; i < bytes_read; i++)
1628 buf[i] = xlate[to_uchar (buf[i])];
1630 return bytes_read;
1633 /* Initialize a boolean membership set, IN_SET, with the character
1634 values obtained by traversing the linked list of constructs S
1635 using the function `get_next'. IN_SET is expected to have been
1636 initialized to all zeros by the caller. If COMPLEMENT_THIS_SET
1637 is true the resulting set is complemented. */
1639 static void
1640 set_initialize (struct Spec_list *s, bool complement_this_set, bool *in_set)
1642 int c;
1643 size_t i;
1645 s->state = BEGIN_STATE;
1646 while ((c = get_next (s, NULL)) != -1)
1647 in_set[c] = true;
1648 if (complement_this_set)
1649 for (i = 0; i < N_CHARS; i++)
1650 in_set[i] = (!in_set[i]);
1654 main (int argc, char **argv)
1656 int c;
1657 int non_option_args;
1658 int min_operands;
1659 int max_operands;
1660 struct Spec_list buf1, buf2;
1661 struct Spec_list *s1 = &buf1;
1662 struct Spec_list *s2 = &buf2;
1664 initialize_main (&argc, &argv);
1665 program_name = argv[0];
1666 setlocale (LC_ALL, "");
1667 bindtextdomain (PACKAGE, LOCALEDIR);
1668 textdomain (PACKAGE);
1670 atexit (close_stdout);
1672 while ((c = getopt_long (argc, argv, "+cCdst", long_options, NULL)) != -1)
1674 switch (c)
1676 case 'c':
1677 case 'C':
1678 complement = true;
1679 break;
1681 case 'd':
1682 delete = true;
1683 break;
1685 case 's':
1686 squeeze_repeats = true;
1687 break;
1689 case 't':
1690 truncate_set1 = true;
1691 break;
1693 case_GETOPT_HELP_CHAR;
1695 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1697 default:
1698 usage (EXIT_FAILURE);
1699 break;
1703 non_option_args = argc - optind;
1704 translating = (non_option_args == 2 && !delete);
1705 min_operands = 1 + (delete == squeeze_repeats);
1706 max_operands = 1 + (delete <= squeeze_repeats);
1708 if (non_option_args < min_operands)
1710 if (non_option_args == 0)
1711 error (0, 0, _("missing operand"));
1712 else
1714 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
1715 fprintf (stderr, "%s\n",
1716 _(squeeze_repeats
1717 ? ("Two strings must be given when "
1718 "both deleting and squeezing repeats.")
1719 : "Two strings must be given when translating."));
1721 usage (EXIT_FAILURE);
1724 if (max_operands < non_option_args)
1726 error (0, 0, _("extra operand %s"), quote (argv[optind + max_operands]));
1727 if (non_option_args == 2)
1728 fprintf (stderr, "%s\n",
1729 _("Only one string may be given when "
1730 "deleting without squeezing repeats."));
1731 usage (EXIT_FAILURE);
1734 spec_init (s1);
1735 if (!parse_str (argv[optind], s1))
1736 exit (EXIT_FAILURE);
1738 if (non_option_args == 2)
1740 spec_init (s2);
1741 if (!parse_str (argv[optind + 1], s2))
1742 exit (EXIT_FAILURE);
1744 else
1745 s2 = NULL;
1747 validate (s1, s2);
1749 /* Use binary I/O, since `tr' is sometimes used to transliterate
1750 non-printable characters, or characters which are stripped away
1751 by text-mode reads (like CR and ^Z). */
1752 if (O_BINARY && ! isatty (STDIN_FILENO))
1753 freopen (NULL, "rb", stdin);
1754 if (O_BINARY && ! isatty (STDOUT_FILENO))
1755 freopen (NULL, "wb", stdout);
1757 if (squeeze_repeats && non_option_args == 1)
1759 set_initialize (s1, complement, in_squeeze_set);
1760 squeeze_filter (io_buf, sizeof io_buf, plain_read);
1762 else if (delete && non_option_args == 1)
1764 set_initialize (s1, complement, in_delete_set);
1766 for (;;)
1768 size_t nr = read_and_delete (io_buf, sizeof io_buf);
1769 if (nr == 0)
1770 break;
1771 if (fwrite (io_buf, 1, nr, stdout) != nr)
1772 error (EXIT_FAILURE, errno, _("write error"));
1775 else if (squeeze_repeats && delete && non_option_args == 2)
1777 set_initialize (s1, complement, in_delete_set);
1778 set_initialize (s2, false, in_squeeze_set);
1779 squeeze_filter (io_buf, sizeof io_buf, read_and_delete);
1781 else if (translating)
1783 if (complement)
1785 int i;
1786 bool *in_s1 = in_delete_set;
1788 set_initialize (s1, false, in_s1);
1789 s2->state = BEGIN_STATE;
1790 for (i = 0; i < N_CHARS; i++)
1791 xlate[i] = i;
1792 for (i = 0; i < N_CHARS; i++)
1794 if (!in_s1[i])
1796 int ch = get_next (s2, NULL);
1797 assert (ch != -1 || truncate_set1);
1798 if (ch == -1)
1800 /* This will happen when tr is invoked like e.g.
1801 tr -cs A-Za-z0-9 '\012'. */
1802 break;
1804 xlate[i] = ch;
1808 else
1810 int c1, c2;
1811 int i;
1812 enum Upper_Lower_class class_s1;
1813 enum Upper_Lower_class class_s2;
1815 for (i = 0; i < N_CHARS; i++)
1816 xlate[i] = i;
1817 s1->state = BEGIN_STATE;
1818 s2->state = BEGIN_STATE;
1819 for (;;)
1821 c1 = get_next (s1, &class_s1);
1822 c2 = get_next (s2, &class_s2);
1824 /* When translating and there is an [:upper:] or [:lower:]
1825 class in SET2, then there must be a corresponding [:lower:]
1826 or [:upper:] class in SET1. */
1827 if (class_s1 == UL_NONE
1828 && (class_s2 == UL_LOWER || class_s2 == UL_UPPER))
1829 error (EXIT_FAILURE, 0,
1830 _("misaligned [:upper:] and/or [:lower:] construct"));
1832 if (class_s1 == UL_LOWER && class_s2 == UL_UPPER)
1834 for (i = 0; i < N_CHARS; i++)
1835 if (islower (i))
1836 xlate[i] = toupper (i);
1838 else if (class_s1 == UL_UPPER && class_s2 == UL_LOWER)
1840 for (i = 0; i < N_CHARS; i++)
1841 if (isupper (i))
1842 xlate[i] = tolower (i);
1844 else if ((class_s1 == UL_LOWER && class_s2 == UL_LOWER)
1845 || (class_s1 == UL_UPPER && class_s2 == UL_UPPER))
1847 /* POSIX says the behavior of `tr "[:upper:]" "[:upper:]"'
1848 is undefined. Treat it as a no-op. */
1850 else
1852 /* The following should have been checked by validate... */
1853 if (c1 == -1 || c2 == -1)
1854 break;
1855 xlate[c1] = c2;
1858 assert (c1 == -1 || truncate_set1);
1860 if (squeeze_repeats)
1862 set_initialize (s2, false, in_squeeze_set);
1863 squeeze_filter (io_buf, sizeof io_buf, read_and_xlate);
1865 else
1867 for (;;)
1869 size_t bytes_read = read_and_xlate (io_buf, sizeof io_buf);
1870 if (bytes_read == 0)
1871 break;
1872 if (fwrite (io_buf, 1, bytes_read, stdout) != bytes_read)
1873 error (EXIT_FAILURE, errno, _("write error"));
1878 if (close (STDIN_FILENO) != 0)
1879 error (EXIT_FAILURE, errno, _("standard input"));
1881 exit (EXIT_SUCCESS);