Index: head/usr.bin/tr/str.c =================================================================== --- head/usr.bin/tr/str.c (revision 302593) +++ head/usr.bin/tr/str.c (revision 302594) @@ -1,387 +1,361 @@ /*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifndef lint static const char sccsid[] = "@(#)str.c 8.2 (Berkeley) 4/28/95"; #endif #include #include #include #include #include #include #include #include #include #include #include "extern.h" static int backslash(STR *, int *); static int bracket(STR *); static void genclass(STR *); static void genequiv(STR *); -static int genrange(STR *, int); +static int genrange(STR *); static void genseq(STR *); wint_t next(STR *s) { int is_octal; wint_t ch; wchar_t wch; size_t clen; switch (s->state) { case EOS: return (0); case INFINITE: return (1); case NORMAL: switch (*s->str) { case '\0': s->state = EOS; return (0); case '\\': s->lastch = backslash(s, &is_octal); break; case '[': if (bracket(s)) return (next(s)); /* FALLTHROUGH */ default: clen = mbrtowc(&wch, s->str, MB_LEN_MAX, NULL); if (clen == (size_t)-1 || clen == (size_t)-2 || clen == 0) errc(1, EILSEQ, NULL); is_octal = 0; s->lastch = wch; s->str += clen; break; } /* We can start a range at any time. */ - if (s->str[0] == '-' && genrange(s, is_octal)) + if (s->str[0] == '-' && genrange(s)) return (next(s)); return (1); case RANGE: if (s->cnt-- == 0) { s->state = NORMAL; return (next(s)); } ++s->lastch; return (1); case SEQUENCE: if (s->cnt-- == 0) { s->state = NORMAL; return (next(s)); } return (1); case CCLASS: case CCLASS_UPPER: case CCLASS_LOWER: s->cnt++; ch = nextwctype(s->lastch, s->cclass); if (ch == -1) { s->state = NORMAL; return (next(s)); } s->lastch = ch; return (1); case SET: if ((ch = s->set[s->cnt++]) == OOBCH) { s->state = NORMAL; return (next(s)); } s->lastch = ch; return (1); default: return (0); } /* NOTREACHED */ } static int bracket(STR *s) { char *p; switch (s->str[1]) { case ':': /* "[:class:]" */ if ((p = strchr(s->str + 2, ']')) == NULL) return (0); if (*(p - 1) != ':' || p - s->str < 4) goto repeat; *(p - 1) = '\0'; s->str += 2; genclass(s); s->str = p + 1; return (1); case '=': /* "[=equiv=]" */ if (s->str[2] == '\0' || (p = strchr(s->str + 3, ']')) == NULL) return (0); if (*(p - 1) != '=' || p - s->str < 4) goto repeat; s->str += 2; genequiv(s); return (1); default: /* "[\###*n]" or "[#*n]" */ repeat: if ((p = strpbrk(s->str + 2, "*]")) == NULL) return (0); if (p[0] != '*' || strchr(p, ']') == NULL) return (0); s->str += 1; genseq(s); return (1); } /* NOTREACHED */ } static void genclass(STR *s) { if ((s->cclass = wctype(s->str)) == 0) errx(1, "unknown class %s", s->str); s->cnt = 0; s->lastch = -1; /* incremented before check in next() */ if (strcmp(s->str, "upper") == 0) s->state = CCLASS_UPPER; else if (strcmp(s->str, "lower") == 0) s->state = CCLASS_LOWER; else s->state = CCLASS; } static void genequiv(STR *s) { int i, p, pri; char src[2], dst[3]; size_t clen; wchar_t wc; if (*s->str == '\\') { s->equiv[0] = backslash(s, NULL); if (*s->str != '=') errx(1, "misplaced equivalence equals sign"); s->str += 2; } else { clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL); if (clen == (size_t)-1 || clen == (size_t)-2 || clen == 0) errc(1, EILSEQ, NULL); s->equiv[0] = wc; if (s->str[clen] != '=') errx(1, "misplaced equivalence equals sign"); s->str += clen + 2; } /* * Calculate the set of all characters in the same equivalence class * as the specified character (they will have the same primary * collation weights). * XXX Knows too much about how strxfrm() is implemented. Assumes * it fills the string with primary collation weight bytes. Only one- * to-one mappings are supported. * XXX Equivalence classes not supported in multibyte locales. */ src[0] = (char)s->equiv[0]; src[1] = '\0'; if (MB_CUR_MAX == 1 && strxfrm(dst, src, sizeof(dst)) == 1) { pri = (unsigned char)*dst; for (p = 1, i = 1; i < NCHARS_SB; i++) { *src = i; if (strxfrm(dst, src, sizeof(dst)) == 1 && pri && pri == (unsigned char)*dst) s->equiv[p++] = i; } s->equiv[p] = OOBCH; } s->cnt = 0; s->state = SET; s->set = s->equiv; } static int -genrange(STR *s, int was_octal) +genrange(STR *s) { - int stopval, octal; + int stopval; char *savestart; - int n, cnt, *p; size_t clen; wchar_t wc; - octal = 0; savestart = s->str; if (*++s->str == '\\') - stopval = backslash(s, &octal); + stopval = backslash(s, NULL); else { clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL); if (clen == (size_t)-1 || clen == (size_t)-2) errc(1, EILSEQ, NULL); stopval = wc; s->str += clen; } - /* - * XXX Characters are not ordered according to collating sequence in - * multibyte locales. - */ - if (octal || was_octal || MB_CUR_MAX > 1) { - if (stopval < s->lastch) { - s->str = savestart; - return (0); - } - s->cnt = stopval - s->lastch + 1; - s->state = RANGE; - --s->lastch; - return (1); - } - if (charcoll((const void *)&stopval, (const void *)&(s->lastch)) < 0) { + if (stopval < s->lastch) { s->str = savestart; return (0); } - if ((s->set = p = malloc((NCHARS_SB + 1) * sizeof(int))) == NULL) - err(1, "genrange() malloc"); - for (cnt = 0; cnt < NCHARS_SB; cnt++) - if (charcoll((const void *)&cnt, (const void *)&(s->lastch)) >= 0 && - charcoll((const void *)&cnt, (const void *)&stopval) <= 0) - *p++ = cnt; - *p = OOBCH; - n = p - s->set; - - s->cnt = 0; - s->state = SET; - if (n > 1) - mergesort(s->set, n, sizeof(*(s->set)), charcoll); + s->cnt = stopval - s->lastch + 1; + s->state = RANGE; + --s->lastch; return (1); } static void genseq(STR *s) { char *ep; wchar_t wc; size_t clen; if (s->which == STRING1) errx(1, "sequences only valid in string2"); if (*s->str == '\\') s->lastch = backslash(s, NULL); else { clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL); if (clen == (size_t)-1 || clen == (size_t)-2) errc(1, EILSEQ, NULL); s->lastch = wc; s->str += clen; } if (*s->str != '*') errx(1, "misplaced sequence asterisk"); switch (*++s->str) { case '\\': s->cnt = backslash(s, NULL); break; case ']': s->cnt = 0; ++s->str; break; default: if (isdigit((u_char)*s->str)) { s->cnt = strtol(s->str, &ep, 0); if (*ep == ']') { s->str = ep + 1; break; } } errx(1, "illegal sequence count"); /* NOTREACHED */ } s->state = s->cnt ? SEQUENCE : INFINITE; } /* * Translate \??? into a character. Up to 3 octal digits, if no digits either * an escape code or a literal character. */ static int backslash(STR *s, int *is_octal) { int ch, cnt, val; if (is_octal != NULL) *is_octal = 0; for (cnt = val = 0;;) { ch = (u_char)*++s->str; if (!isdigit(ch) || ch > '7') break; val = val * 8 + ch - '0'; if (++cnt == 3) { ++s->str; break; } } if (cnt) { if (is_octal != NULL) *is_octal = 1; return (val); } if (ch != '\0') ++s->str; switch (ch) { case 'a': /* escape characters */ return ('\7'); case 'b': return ('\b'); case 'f': return ('\f'); case 'n': return ('\n'); case 'r': return ('\r'); case 't': return ('\t'); case 'v': return ('\13'); case '\0': /* \" -> \ */ s->state = EOS; return ('\\'); default: /* \x" -> x */ return (ch); } } Index: head/usr.bin/tr/tr.1 =================================================================== --- head/usr.bin/tr/tr.1 (revision 302593) +++ head/usr.bin/tr/tr.1 (revision 302594) @@ -1,421 +1,410 @@ .\" Copyright (c) 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" the Institute of Electrical and Electronics Engineers, Inc. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)tr.1 8.1 (Berkeley) 6/6/93 .\" $FreeBSD$ .\" .Dd October 13, 2006 .Dt TR 1 .Os .Sh NAME .Nm tr .Nd translate characters .Sh SYNOPSIS .Nm .Op Fl Ccsu .Ar string1 string2 .Nm .Op Fl Ccu .Fl d .Ar string1 .Nm .Op Fl Ccu .Fl s .Ar string1 .Nm .Op Fl Ccu .Fl ds .Ar string1 string2 .Sh DESCRIPTION The .Nm utility copies the standard input to the standard output with substitution or deletion of selected characters. .Pp The following options are available: .Bl -tag -width Ds .It Fl C Complement the set of characters in .Ar string1 , that is .Dq Fl C Li ab includes every character except for .Ql a and .Ql b . .It Fl c Same as .Fl C but complement the set of values in .Ar string1 . .It Fl d Delete characters in .Ar string1 from the input. .It Fl s Squeeze multiple occurrences of the characters listed in the last operand (either .Ar string1 or .Ar string2 ) in the input into a single instance of the character. This occurs after all deletion and translation is completed. .It Fl u Guarantee that any output is unbuffered. .El .Pp In the first synopsis form, the characters in .Ar string1 are translated into the characters in .Ar string2 where the first character in .Ar string1 is translated into the first character in .Ar string2 and so on. If .Ar string1 is longer than .Ar string2 , the last character found in .Ar string2 is duplicated until .Ar string1 is exhausted. .Pp In the second synopsis form, the characters in .Ar string1 are deleted from the input. .Pp In the third synopsis form, the characters in .Ar string1 are compressed as described for the .Fl s option. .Pp In the fourth synopsis form, the characters in .Ar string1 are deleted from the input, and the characters in .Ar string2 are compressed as described for the .Fl s option. .Pp The following conventions can be used in .Ar string1 and .Ar string2 to specify sets of characters: .Bl -tag -width [:equiv:] .It character Any character not described by one of the following conventions represents itself. .It \eoctal A backslash followed by 1, 2 or 3 octal digits represents a character with that encoded value. To follow an octal sequence with a digit as a character, left zero-pad the octal sequence to the full 3 octal digits. .It \echaracter A backslash followed by certain special characters maps to special values. .Bl -column "\ea" .It "\ea " .It "\eb " .It "\ef " .It "\en " .It "\er " .It "\et " .It "\ev " .El .Pp A backslash followed by any other character maps to that character. .It c-c For non-octal range endpoints represents the range of characters between the range endpoints, inclusive, in ascending order, as defined by the collation sequence. If either or both of the range endpoints are octal sequences, it represents the range of specific coded values between the range endpoints, inclusive. -.Pp -.Bf Em -See the -.Sx COMPATIBILITY -section below for an important note regarding -differences in the way the current -implementation interprets range expressions differently from -previous implementations. .Ef .It [:class:] Represents all characters belonging to the defined character class. Class names are: .Bl -column "phonogram" .It "alnum " .It "alpha " .It "blank " .It "cntrl " .It "digit " .It "graph " .It "ideogram " .It "lower " .It "phonogram " .It "print " .It "punct " .It "rune " .It "space " .It "special " .It "upper " .It "xdigit " .El .Pp .\" All classes may be used in .\" .Ar string1 , .\" and in .\" .Ar string2 .\" when both the .\" .Fl d .\" and .\" .Fl s .\" options are specified. .\" Otherwise, only the classes ``upper'' and ``lower'' may be used in .\" .Ar string2 .\" and then only when the corresponding class (``upper'' for ``lower'' .\" and vice-versa) is specified in the same relative position in .\" .Ar string1 . .\" .Pp When .Dq Li [:lower:] appears in .Ar string1 and .Dq Li [:upper:] appears in the same relative position in .Ar string2 , it represents the characters pairs from the .Dv toupper mapping in the .Ev LC_CTYPE category of the current locale. When .Dq Li [:upper:] appears in .Ar string1 and .Dq Li [:lower:] appears in the same relative position in .Ar string2 , it represents the characters pairs from the .Dv tolower mapping in the .Ev LC_CTYPE category of the current locale. .Pp With the exception of case conversion, characters in the classes are in unspecified order. .Pp For specific information as to which .Tn ASCII characters are included in these classes, see .Xr ctype 3 and related manual pages. .It [=equiv=] Represents all characters belonging to the same equivalence class as .Ar equiv , ordered by their encoded values. .It [#*n] Represents .Ar n repeated occurrences of the character represented by .Ar # . This expression is only valid when it occurs in .Ar string2 . If .Ar n is omitted or is zero, it is be interpreted as large enough to extend .Ar string2 sequence to the length of .Ar string1 . If .Ar n has a leading zero, it is interpreted as an octal value, otherwise, it is interpreted as a decimal value. .El .Sh ENVIRONMENT The .Ev LANG , LC_ALL , LC_CTYPE and .Ev LC_COLLATE environment variables affect the execution of .Nm as described in .Xr environ 7 . .Sh EXIT STATUS .Ex -std .Sh EXAMPLES The following examples are shown as given to the shell: .Pp Create a list of the words in file1, one per line, where a word is taken to be a maximal string of letters. .Pp .D1 Li "tr -cs \*q[:alpha:]\*q \*q\en\*q < file1" .Pp Translate the contents of file1 to upper-case. .Pp .D1 Li "tr \*q[:lower:]\*q \*q[:upper:]\*q < file1" .Pp (This should be preferred over the traditional .Ux idiom of .Dq Li "tr a-z A-Z" , since it works correctly in all locales.) .Pp Strip out non-printable characters from file1. .Pp .D1 Li "tr -cd \*q[:print:]\*q < file1" .Pp Remove diacritical marks from all accented variants of the letter .Ql e : .Pp .Dl "tr \*q[=e=]\*q \*qe\*q" .Sh COMPATIBILITY -Previous .Fx implementations of .Nm did not order characters in range expressions according to the current -locale's collation order, making it possible to convert unaccented Latin -characters (esp.\& as found in English text) from upper to lower case using +locale's collation order, making it possible to convert accented Latin +characters from upper to lower case using the traditional .Ux idiom of .Dq Li "tr A-Z a-z" . -Since -.Nm -now obeys the locale's collation order, this idiom may not produce -correct results when there is not a 1:1 mapping between lower and -upper case, or when the order of characters within the two cases differs. As noted in the .Sx EXAMPLES section above, the character class expressions .Dq Li [:lower:] and .Dq Li [:upper:] should be used instead of explicit character ranges like .Dq Li a-z and .Dq Li A-Z . +.Pp +.Dq Li [=equiv=] +expression is implemented for single byte locales only. .Pp System V has historically implemented character ranges using the syntax .Dq Li [c-c] instead of the .Dq Li c-c used by historic .Bx implementations and standardized by POSIX. System V shell scripts should work under this implementation as long as the range is intended to map in another range, i.e., the command .Dq Li "tr [a-z] [A-Z]" will work as it will map the .Ql \&[ character in .Ar string1 to the .Ql \&[ character in .Ar string2 . However, if the shell script is deleting or squeezing characters as in the command .Dq Li "tr -d [a-z]" , the characters .Ql \&[ and .Ql \&] will be included in the deletion or compression list which would not have happened under a historic System V implementation. Additionally, any scripts that depended on the sequence .Dq Li a-z to represent the three characters .Ql a , .Ql \- and .Ql z will have to be rewritten as .Dq Li a\e-z . .Pp The .Nm utility has historically not permitted the manipulation of NUL bytes in its input and, additionally, stripped NUL's from its input stream. This implementation has removed this behavior as a bug. .Pp The .Nm utility has historically been extremely forgiving of syntax errors, for example, the .Fl c and .Fl s options were ignored unless two strings were specified. This implementation will not permit illegal syntax. .Sh STANDARDS The .Nm utility conforms to .St -p1003.1-2001 . The .Dq ideogram , .Dq phonogram , .Dq rune , and .Dq special character classes are extensions. .Pp It should be noted that the feature wherein the last character of .Ar string2 is duplicated if .Ar string2 has less characters than .Ar string1 is permitted by POSIX but is not required. Shell scripts attempting to be portable to other POSIX systems should use the .Dq Li [#*] convention instead of relying on this behavior. The .Fl u option is an extension to the .St -p1003.1-2001 standard. Index: head/usr.bin/tr/tr.c =================================================================== --- head/usr.bin/tr/tr.c (revision 302593) +++ head/usr.bin/tr/tr.c (revision 302594) @@ -1,373 +1,338 @@ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1988, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif #ifndef lint static const char sccsid[] = "@(#)tr.c 8.2 (Berkeley) 5/4/95"; #endif #include #include #include #include #include #include #include #include #include #include #include #include #include "cmap.h" #include "cset.h" #include "extern.h" static STR s1 = { STRING1, NORMAL, 0, OOBCH, 0, { 0, OOBCH }, NULL, NULL }; static STR s2 = { STRING2, NORMAL, 0, OOBCH, 0, { 0, OOBCH }, NULL, NULL }; static struct cset *setup(char *, STR *, int, int); static void usage(void); int main(int argc, char **argv) { - static int carray[NCHARS_SB]; struct cmap *map; struct cset *delete, *squeeze; - int n, *p; int Cflag, cflag, dflag, sflag, isstring2; wint_t ch, cnt, lastch; (void)setlocale(LC_ALL, ""); Cflag = cflag = dflag = sflag = 0; while ((ch = getopt(argc, argv, "Ccdsu")) != -1) switch((char)ch) { case 'C': Cflag = 1; cflag = 0; break; case 'c': cflag = 1; Cflag = 0; break; case 'd': dflag = 1; break; case 's': sflag = 1; break; case 'u': setbuf(stdout, (char *)NULL); break; case '?': default: usage(); } argc -= optind; argv += optind; switch(argc) { case 0: default: usage(); /* NOTREACHED */ case 1: isstring2 = 0; break; case 2: isstring2 = 1; break; } /* * tr -ds [-Cc] string1 string2 * Delete all characters (or complemented characters) in string1. * Squeeze all characters in string2. */ if (dflag && sflag) { if (!isstring2) usage(); delete = setup(argv[0], &s1, cflag, Cflag); squeeze = setup(argv[1], &s2, 0, 0); for (lastch = OOBCH; (ch = getwchar()) != WEOF;) if (!cset_in(delete, ch) && (lastch != ch || !cset_in(squeeze, ch))) { lastch = ch; (void)putwchar(ch); } if (ferror(stdin)) err(1, NULL); exit(0); } /* * tr -d [-Cc] string1 * Delete all characters (or complemented characters) in string1. */ if (dflag) { if (isstring2) usage(); delete = setup(argv[0], &s1, cflag, Cflag); while ((ch = getwchar()) != WEOF) if (!cset_in(delete, ch)) (void)putwchar(ch); if (ferror(stdin)) err(1, NULL); exit(0); } /* * tr -s [-Cc] string1 * Squeeze all characters (or complemented characters) in string1. */ if (sflag && !isstring2) { squeeze = setup(argv[0], &s1, cflag, Cflag); for (lastch = OOBCH; (ch = getwchar()) != WEOF;) if (lastch != ch || !cset_in(squeeze, ch)) { lastch = ch; (void)putwchar(ch); } if (ferror(stdin)) err(1, NULL); exit(0); } /* * tr [-Ccs] string1 string2 * Replace all characters (or complemented characters) in string1 with * the character in the same position in string2. If the -s option is * specified, squeeze all the characters in string2. */ if (!isstring2) usage(); map = cmap_alloc(); if (map == NULL) err(1, NULL); squeeze = cset_alloc(); if (squeeze == NULL) err(1, NULL); s1.str = argv[0]; if (Cflag || cflag) { cmap_default(map, OOBCH); if ((s2.str = strdup(argv[1])) == NULL) errx(1, "strdup(argv[1])"); } else s2.str = argv[1]; if (!next(&s2)) errx(1, "empty string2"); /* * For -s result will contain only those characters defined * as the second characters in each of the toupper or tolower * pairs. */ /* If string2 runs out of characters, use the last one specified. */ while (next(&s1)) { again: if (s1.state == CCLASS_LOWER && s2.state == CCLASS_UPPER && s1.cnt == 1 && s2.cnt == 1) { do { ch = towupper(s1.lastch); cmap_add(map, s1.lastch, ch); if (sflag && iswupper(ch)) cset_add(squeeze, ch); if (!next(&s1)) goto endloop; } while (s1.state == CCLASS_LOWER && s1.cnt > 1); /* skip upper set */ do { if (!next(&s2)) break; } while (s2.state == CCLASS_UPPER && s2.cnt > 1); goto again; } else if (s1.state == CCLASS_UPPER && s2.state == CCLASS_LOWER && s1.cnt == 1 && s2.cnt == 1) { do { ch = towlower(s1.lastch); cmap_add(map, s1.lastch, ch); if (sflag && iswlower(ch)) cset_add(squeeze, ch); if (!next(&s1)) goto endloop; } while (s1.state == CCLASS_UPPER && s1.cnt > 1); /* skip lower set */ do { if (!next(&s2)) break; } while (s2.state == CCLASS_LOWER && s2.cnt > 1); goto again; } else { cmap_add(map, s1.lastch, s2.lastch); if (sflag) cset_add(squeeze, s2.lastch); } (void)next(&s2); } endloop: - if (cflag || (Cflag && MB_CUR_MAX > 1)) { + if (cflag || Cflag) { /* * This is somewhat tricky: since the character set is * potentially huge, we need to avoid allocating a map * entry for every character. Our strategy is to set the * default mapping to the last character of string #2 * (= the one that gets automatically repeated), then to * add back identity mappings for characters that should * remain unchanged. We don't waste space on identity mappings * for non-characters with the -C option; those are simulated * in the I/O loop. */ s2.str = argv[1]; s2.state = NORMAL; - for (cnt = 0; cnt < WINT_MAX; cnt++) { + for (cnt = 0; cnt <= WCHAR_MAX; cnt++) { if (Cflag && !iswrune(cnt)) continue; if (cmap_lookup(map, cnt) == OOBCH) { - if (next(&s2)) + if (next(&s2)) { cmap_add(map, cnt, s2.lastch); - if (sflag) - cset_add(squeeze, s2.lastch); + if (sflag) + cset_add(squeeze, s2.lastch); + } } else cmap_add(map, cnt, cnt); if ((s2.state == EOS || s2.state == INFINITE) && cnt >= cmap_max(map)) break; } cmap_default(map, s2.lastch); - } else if (Cflag) { - for (p = carray, cnt = 0; cnt < NCHARS_SB; cnt++) { - if (cmap_lookup(map, cnt) == OOBCH && iswrune(cnt)) - *p++ = cnt; - else - cmap_add(map, cnt, cnt); - } - n = p - carray; - if (Cflag && n > 1) - (void)mergesort(carray, n, sizeof(*carray), charcoll); - - s2.str = argv[1]; - s2.state = NORMAL; - for (cnt = 0; cnt < n; cnt++) { - (void)next(&s2); - cmap_add(map, carray[cnt], s2.lastch); - /* - * Chars taken from s2 can be different this time - * due to lack of complex upper/lower processing, - * so fill string2 again to not miss some. - */ - if (sflag) - cset_add(squeeze, s2.lastch); - } } cset_cache(squeeze); cmap_cache(map); if (sflag) for (lastch = OOBCH; (ch = getwchar()) != WEOF;) { if (!Cflag || iswrune(ch)) ch = cmap_lookup(map, ch); if (lastch != ch || !cset_in(squeeze, ch)) { lastch = ch; (void)putwchar(ch); } } else while ((ch = getwchar()) != WEOF) { if (!Cflag || iswrune(ch)) ch = cmap_lookup(map, ch); (void)putwchar(ch); } if (ferror(stdin)) err(1, NULL); exit (0); } static struct cset * setup(char *arg, STR *str, int cflag, int Cflag) { struct cset *cs; cs = cset_alloc(); if (cs == NULL) err(1, NULL); str->str = arg; while (next(str)) cset_add(cs, str->lastch); if (Cflag) cset_addclass(cs, wctype("rune"), true); if (cflag || Cflag) cset_invert(cs); cset_cache(cs); return (cs); -} - -int -charcoll(const void *a, const void *b) -{ - static char sa[2], sb[2]; - - sa[0] = *(const int *)a; - sb[0] = *(const int *)b; - return (strcoll(sa, sb)); } static void usage(void) { (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", "usage: tr [-Ccsu] string1 string2", " tr [-Ccu] -d string1", " tr [-Ccu] -s string1", " tr [-Ccu] -ds string1 string2"); exit(1); }