1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-12-11 23:36:25 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
1997-08-18 07:24:58 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1988, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
2001-12-11 23:36:25 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#ifndef lint
|
2001-12-11 23:36:25 +00:00
|
|
|
static const char sccsid[] = "@(#)tr.c 8.2 (Berkeley) 5/4/95";
|
1997-08-18 07:24:58 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2016-09-19 20:43:03 +00:00
|
|
|
#include <sys/capsicum.h>
|
1997-03-11 13:43:33 +00:00
|
|
|
|
2016-10-07 17:56:57 +00:00
|
|
|
#include <capsicum_helpers.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <ctype.h>
|
1997-08-18 07:24:58 +00:00
|
|
|
#include <err.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <limits.h>
|
2001-12-11 23:36:25 +00:00
|
|
|
#include <locale.h>
|
2013-01-22 05:39:34 +00:00
|
|
|
#include <stdint.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-03-11 13:43:33 +00:00
|
|
|
#include <unistd.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
1997-03-11 13:43:33 +00:00
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
#include "cmap.h"
|
|
|
|
#include "cset.h"
|
1994-05-27 12:33:43 +00:00
|
|
|
#include "extern.h"
|
|
|
|
|
2011-11-06 08:17:42 +00:00
|
|
|
static STR s1 = { STRING1, NORMAL, 0, OOBCH, 0, { 0, OOBCH }, NULL, NULL };
|
|
|
|
static STR s2 = { STRING2, NORMAL, 0, OOBCH, 0, { 0, OOBCH }, NULL, NULL };
|
2004-07-09 02:08:07 +00:00
|
|
|
|
|
|
|
static struct cset *setup(char *, STR *, int, int);
|
2002-03-22 01:42:45 +00:00
|
|
|
static void usage(void);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2002-09-04 23:29:10 +00:00
|
|
|
main(int argc, char **argv)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2016-07-14 09:19:53 +00:00
|
|
|
static int carray[NCHARS_SB];
|
2004-07-09 02:08:07 +00:00
|
|
|
struct cmap *map;
|
|
|
|
struct cset *delete, *squeeze;
|
2016-07-14 09:19:53 +00:00
|
|
|
int n, *p;
|
2002-06-14 08:58:30 +00:00
|
|
|
int Cflag, cflag, dflag, sflag, isstring2;
|
2005-04-09 14:31:41 +00:00
|
|
|
wint_t ch, cnt, lastch;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-06-14 07:37:08 +00:00
|
|
|
(void)setlocale(LC_ALL, "");
|
1995-10-28 22:27:03 +00:00
|
|
|
|
2016-10-07 17:56:57 +00:00
|
|
|
if (caph_limit_stdio() == -1)
|
|
|
|
err(1, "unable to limit stdio");
|
2016-09-19 16:14:38 +00:00
|
|
|
|
|
|
|
if (cap_enter() < 0 && errno != ENOSYS)
|
|
|
|
err(1, "unable to enter capability mode");
|
|
|
|
|
2002-06-14 08:58:30 +00:00
|
|
|
Cflag = cflag = dflag = sflag = 0;
|
|
|
|
while ((ch = getopt(argc, argv, "Ccdsu")) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch((char)ch) {
|
2002-06-14 08:58:30 +00:00
|
|
|
case 'C':
|
|
|
|
Cflag = 1;
|
|
|
|
cflag = 0;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'c':
|
|
|
|
cflag = 1;
|
2002-06-14 08:58:30 +00:00
|
|
|
Cflag = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
1997-10-12 09:52:49 +00:00
|
|
|
case 'u':
|
|
|
|
setbuf(stdout, (char *)NULL);
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
switch(argc) {
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
case 1:
|
|
|
|
isstring2 = 0;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
isstring2 = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-06-14 08:58:30 +00:00
|
|
|
* tr -ds [-Cc] string1 string2
|
1994-05-27 12:33:43 +00:00
|
|
|
* Delete all characters (or complemented characters) in string1.
|
|
|
|
* Squeeze all characters in string2.
|
|
|
|
*/
|
|
|
|
if (dflag && sflag) {
|
|
|
|
if (!isstring2)
|
|
|
|
usage();
|
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
delete = setup(argv[0], &s1, cflag, Cflag);
|
|
|
|
squeeze = setup(argv[1], &s2, 0, 0);
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
for (lastch = OOBCH; (ch = getwchar()) != WEOF;)
|
|
|
|
if (!cset_in(delete, ch) &&
|
|
|
|
(lastch != ch || !cset_in(squeeze, ch))) {
|
1994-05-27 12:33:43 +00:00
|
|
|
lastch = ch;
|
2004-07-09 02:08:07 +00:00
|
|
|
(void)putwchar(ch);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2004-07-09 05:15:46 +00:00
|
|
|
if (ferror(stdin))
|
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-06-14 08:58:30 +00:00
|
|
|
* tr -d [-Cc] string1
|
1994-05-27 12:33:43 +00:00
|
|
|
* Delete all characters (or complemented characters) in string1.
|
|
|
|
*/
|
|
|
|
if (dflag) {
|
|
|
|
if (isstring2)
|
|
|
|
usage();
|
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
delete = setup(argv[0], &s1, cflag, Cflag);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
while ((ch = getwchar()) != WEOF)
|
|
|
|
if (!cset_in(delete, ch))
|
|
|
|
(void)putwchar(ch);
|
2004-07-09 05:15:46 +00:00
|
|
|
if (ferror(stdin))
|
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-06-14 08:58:30 +00:00
|
|
|
* tr -s [-Cc] string1
|
1994-05-27 12:33:43 +00:00
|
|
|
* Squeeze all characters (or complemented characters) in string1.
|
|
|
|
*/
|
|
|
|
if (sflag && !isstring2) {
|
2004-07-09 02:08:07 +00:00
|
|
|
squeeze = setup(argv[0], &s1, cflag, Cflag);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
for (lastch = OOBCH; (ch = getwchar()) != WEOF;)
|
|
|
|
if (lastch != ch || !cset_in(squeeze, ch)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
lastch = ch;
|
2004-07-09 02:08:07 +00:00
|
|
|
(void)putwchar(ch);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2004-07-09 05:15:46 +00:00
|
|
|
if (ferror(stdin))
|
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-06-14 08:58:30 +00:00
|
|
|
* tr [-Ccs] string1 string2
|
1994-05-27 12:33:43 +00:00
|
|
|
* 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();
|
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
map = cmap_alloc();
|
|
|
|
if (map == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
squeeze = cset_alloc();
|
|
|
|
if (squeeze == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
s1.str = argv[0];
|
2004-07-09 02:08:07 +00:00
|
|
|
|
|
|
|
if (Cflag || cflag) {
|
|
|
|
cmap_default(map, OOBCH);
|
2003-08-03 22:19:43 +00:00
|
|
|
if ((s2.str = strdup(argv[1])) == NULL)
|
|
|
|
errx(1, "strdup(argv[1])");
|
|
|
|
} else
|
|
|
|
s2.str = argv[1];
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (!next(&s2))
|
1997-08-18 07:24:58 +00:00
|
|
|
errx(1, "empty string2");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2003-08-03 02:23:39 +00:00
|
|
|
/*
|
|
|
|
* For -s result will contain only those characters defined
|
|
|
|
* as the second characters in each of the toupper or tolower
|
|
|
|
* pairs.
|
|
|
|
*/
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/* If string2 runs out of characters, use the last one specified. */
|
2003-08-03 02:23:39 +00:00
|
|
|
while (next(&s1)) {
|
|
|
|
again:
|
2004-07-09 02:08:07 +00:00
|
|
|
if (s1.state == CCLASS_LOWER &&
|
|
|
|
s2.state == CCLASS_UPPER &&
|
2003-08-03 02:23:39 +00:00
|
|
|
s1.cnt == 1 && s2.cnt == 1) {
|
|
|
|
do {
|
2004-07-09 02:08:07 +00:00
|
|
|
ch = towupper(s1.lastch);
|
|
|
|
cmap_add(map, s1.lastch, ch);
|
|
|
|
if (sflag && iswupper(ch))
|
|
|
|
cset_add(squeeze, ch);
|
2003-08-03 02:23:39 +00:00
|
|
|
if (!next(&s1))
|
|
|
|
goto endloop;
|
2004-07-09 02:08:07 +00:00
|
|
|
} while (s1.state == CCLASS_LOWER && s1.cnt > 1);
|
2003-08-03 02:23:39 +00:00
|
|
|
/* skip upper set */
|
|
|
|
do {
|
|
|
|
if (!next(&s2))
|
|
|
|
break;
|
2004-07-09 02:08:07 +00:00
|
|
|
} while (s2.state == CCLASS_UPPER && s2.cnt > 1);
|
2003-08-03 02:23:39 +00:00
|
|
|
goto again;
|
2004-07-09 02:08:07 +00:00
|
|
|
} else if (s1.state == CCLASS_UPPER &&
|
|
|
|
s2.state == CCLASS_LOWER &&
|
2003-08-03 02:23:39 +00:00
|
|
|
s1.cnt == 1 && s2.cnt == 1) {
|
|
|
|
do {
|
2004-07-09 02:08:07 +00:00
|
|
|
ch = towlower(s1.lastch);
|
|
|
|
cmap_add(map, s1.lastch, ch);
|
|
|
|
if (sflag && iswlower(ch))
|
|
|
|
cset_add(squeeze, ch);
|
2003-08-03 02:23:39 +00:00
|
|
|
if (!next(&s1))
|
|
|
|
goto endloop;
|
2004-07-09 02:08:07 +00:00
|
|
|
} while (s1.state == CCLASS_UPPER && s1.cnt > 1);
|
2003-08-03 02:23:39 +00:00
|
|
|
/* skip lower set */
|
|
|
|
do {
|
|
|
|
if (!next(&s2))
|
|
|
|
break;
|
2004-07-09 02:08:07 +00:00
|
|
|
} while (s2.state == CCLASS_LOWER && s2.cnt > 1);
|
2003-08-03 02:23:39 +00:00
|
|
|
goto again;
|
|
|
|
} else {
|
2004-07-09 02:08:07 +00:00
|
|
|
cmap_add(map, s1.lastch, s2.lastch);
|
2003-08-03 02:23:39 +00:00
|
|
|
if (sflag)
|
2004-07-09 02:08:07 +00:00
|
|
|
cset_add(squeeze, s2.lastch);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2003-08-03 02:23:39 +00:00
|
|
|
(void)next(&s2);
|
|
|
|
}
|
|
|
|
endloop:
|
2016-07-14 09:19:53 +00:00
|
|
|
if (cflag || (Cflag && MB_CUR_MAX > 1)) {
|
2004-07-09 02:08:07 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2016-07-13 07:56:01 +00:00
|
|
|
for (cnt = 0; cnt < WINT_MAX; cnt++) {
|
2004-07-09 02:08:07 +00:00
|
|
|
if (Cflag && !iswrune(cnt))
|
|
|
|
continue;
|
|
|
|
if (cmap_lookup(map, cnt) == OOBCH) {
|
2016-07-14 09:26:53 +00:00
|
|
|
if (next(&s2)) {
|
2004-07-09 02:08:07 +00:00
|
|
|
cmap_add(map, cnt, s2.lastch);
|
2016-07-14 09:26:53 +00:00
|
|
|
if (sflag)
|
|
|
|
cset_add(squeeze, s2.lastch);
|
|
|
|
}
|
2004-07-09 02:08:07 +00:00
|
|
|
} else
|
|
|
|
cmap_add(map, cnt, cnt);
|
|
|
|
if ((s2.state == EOS || s2.state == INFINITE) &&
|
|
|
|
cnt >= cmap_max(map))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cmap_default(map, s2.lastch);
|
2016-07-14 09:19:53 +00:00
|
|
|
} 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);
|
|
|
|
}
|
2002-07-29 14:50:54 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
cset_cache(squeeze);
|
|
|
|
cmap_cache(map);
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if (sflag)
|
2004-07-09 02:08:07 +00:00
|
|
|
for (lastch = OOBCH; (ch = getwchar()) != WEOF;) {
|
|
|
|
if (!Cflag || iswrune(ch))
|
|
|
|
ch = cmap_lookup(map, ch);
|
|
|
|
if (lastch != ch || !cset_in(squeeze, ch)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
lastch = ch;
|
2004-07-09 02:08:07 +00:00
|
|
|
(void)putwchar(ch);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2004-07-09 02:08:07 +00:00
|
|
|
while ((ch = getwchar()) != WEOF) {
|
|
|
|
if (!Cflag || iswrune(ch))
|
|
|
|
ch = cmap_lookup(map, ch);
|
|
|
|
(void)putwchar(ch);
|
|
|
|
}
|
2004-07-09 05:15:46 +00:00
|
|
|
if (ferror(stdin))
|
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
static struct cset *
|
|
|
|
setup(char *arg, STR *str, int cflag, int Cflag)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-07-09 02:08:07 +00:00
|
|
|
struct cset *cs;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
cs = cset_alloc();
|
|
|
|
if (cs == NULL)
|
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
str->str = arg;
|
|
|
|
while (next(str))
|
2004-07-09 02:08:07 +00:00
|
|
|
cset_add(cs, str->lastch);
|
|
|
|
if (Cflag)
|
|
|
|
cset_addclass(cs, wctype("rune"), true);
|
|
|
|
if (cflag || Cflag)
|
|
|
|
cset_invert(cs);
|
|
|
|
cset_cache(cs);
|
|
|
|
return (cs);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 09:19:53 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
1997-08-18 07:24:58 +00:00
|
|
|
(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
|
2002-06-14 08:58:30 +00:00
|
|
|
"usage: tr [-Ccsu] string1 string2",
|
|
|
|
" tr [-Ccu] -d string1",
|
|
|
|
" tr [-Ccu] -s string1",
|
|
|
|
" tr [-Ccu] -ds string1 string2");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|