summaryrefslogtreecommitdiffstats
path: root/usr.bin/tr
diff options
context:
space:
mode:
authortjr <tjr@FreeBSD.org>2002-06-14 08:58:30 +0000
committertjr <tjr@FreeBSD.org>2002-06-14 08:58:30 +0000
commit29924b60f3b7940b5cf7fa3619ff014480bbce61 (patch)
tree638747761abbc2a1b1267762bef9ac020d7326c7 /usr.bin/tr
parentd895407bfb783edba3e36e85e1abc98c8070eaec (diff)
downloadFreeBSD-src-29924b60f3b7940b5cf7fa3619ff014480bbce61.zip
FreeBSD-src-29924b60f3b7940b5cf7fa3619ff014480bbce61.tar.gz
Add the P1003.1-2001 -C option which complements the set of characters
(not byte values) specified by the first string argument.
Diffstat (limited to 'usr.bin/tr')
-rw-r--r--usr.bin/tr/tr.132
-rw-r--r--usr.bin/tr/tr.c55
2 files changed, 58 insertions, 29 deletions
diff --git a/usr.bin/tr/tr.1 b/usr.bin/tr/tr.1
index b1ac00c..671dd76 100644
--- a/usr.bin/tr/tr.1
+++ b/usr.bin/tr/tr.1
@@ -43,18 +43,18 @@
.Nd translate characters
.Sh SYNOPSIS
.Nm
-.Op Fl csu
+.Op Fl Ccsu
.Ar string1 string2
.Nm
-.Op Fl cu
+.Op Fl Ccu
.Fl d
.Ar string1
.Nm
-.Op Fl cu
+.Op Fl Ccu
.Fl s
.Ar string1
.Nm
-.Op Fl cu
+.Op Fl Ccu
.Fl ds
.Ar string1 string2
.Sh DESCRIPTION
@@ -65,10 +65,20 @@ or deletion of selected characters.
.Pp
The following options are available:
.Bl -tag -width Ds
-.It Fl c
-Complements the set of characters in
+.It Fl C
+Complement the set of characters in
.Ar string1 ,
-that is ``-c ab'' includes every character except for ``a'' and ``b''.
+that is
+.Dq "-C ab"
+includes every character except for
+.Dq a
+and
+.Dq b .
+.It Fl c
+Same as
+.Fl C
+but complement the set of byte values in
+.Ar string1 .
.It Fl d
The
.Fl d
@@ -293,9 +303,9 @@ This implementation will not permit illegal syntax.
.Sh STANDARDS
The
.Nm
-utility is expected to be
-.St -p1003.2
-compatible.
+utility conforms to
+.St -p1003.1-2001 .
+.Pp
It should be noted that the feature wherein the last character of
.Ar string2
is duplicated if
@@ -308,5 +318,5 @@ the ``[#*]'' convention instead of relying on this behavior.
The
.Fl u
option is an extension to the
-.St -p1003.2
+.St -p1003.1-2001
standard.
diff --git a/usr.bin/tr/tr.c b/usr.bin/tr/tr.c
index c74e1d0..524f66b 100644
--- a/usr.bin/tr/tr.c
+++ b/usr.bin/tr/tr.c
@@ -47,6 +47,7 @@ static const char sccsid[] = "@(#)tr.c 8.2 (Berkeley) 5/4/95";
#include <sys/types.h>
+#include <ctype.h>
#include <err.h>
#include <locale.h>
#include <stdio.h>
@@ -56,6 +57,12 @@ static const char sccsid[] = "@(#)tr.c 8.2 (Berkeley) 5/4/95";
#include "extern.h"
+/*
+ * For -C option: determine whether a byte is a valid character in the
+ * current character set (as defined by LC_CTYPE).
+ */
+#define ISCHAR(c) (iscntrl(c) || isprint(c))
+
static int string1[NCHARS] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* ASCII */
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
@@ -94,7 +101,7 @@ static int string1[NCHARS] = {
STR s1 = { STRING1, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL };
STR s2 = { STRING2, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL };
-static void setup(int *, char *, STR *, int);
+static void setup(int *, char *, STR *, int, int);
static void usage(void);
int
@@ -103,15 +110,20 @@ main(argc, argv)
char **argv;
{
int ch, cnt, lastch, *p;
- int cflag, dflag, sflag, isstring2;
+ int Cflag, cflag, dflag, sflag, isstring2;
(void)setlocale(LC_ALL, "");
- cflag = dflag = sflag = 0;
- while ((ch = getopt(argc, argv, "cdsu")) != -1)
+ 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;
@@ -143,7 +155,7 @@ main(argc, argv)
}
/*
- * tr -ds [-c] string1 string2
+ * tr -ds [-Cc] string1 string2
* Delete all characters (or complemented characters) in string1.
* Squeeze all characters in string2.
*/
@@ -151,8 +163,8 @@ main(argc, argv)
if (!isstring2)
usage();
- setup(string1, argv[0], &s1, cflag);
- setup(string2, argv[1], &s2, 0);
+ setup(string1, argv[0], &s1, cflag, Cflag);
+ setup(string2, argv[1], &s2, 0, 0);
for (lastch = OOBCH; (ch = getchar()) != EOF;)
if (!string1[ch] && (!string2[ch] || lastch != ch)) {
@@ -163,14 +175,14 @@ main(argc, argv)
}
/*
- * tr -d [-c] string1
+ * tr -d [-Cc] string1
* Delete all characters (or complemented characters) in string1.
*/
if (dflag) {
if (isstring2)
usage();
- setup(string1, argv[0], &s1, cflag);
+ setup(string1, argv[0], &s1, cflag, Cflag);
while ((ch = getchar()) != EOF)
if (!string1[ch])
@@ -179,11 +191,11 @@ main(argc, argv)
}
/*
- * tr -s [-c] string1
+ * tr -s [-Cc] string1
* Squeeze all characters (or complemented characters) in string1.
*/
if (sflag && !isstring2) {
- setup(string1, argv[0], &s1, cflag);
+ setup(string1, argv[0], &s1, cflag, Cflag);
for (lastch = OOBCH; (ch = getchar()) != EOF;)
if (!string1[ch] || lastch != ch) {
@@ -194,7 +206,7 @@ main(argc, argv)
}
/*
- * tr [-cs] string1 string2
+ * 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.
@@ -205,7 +217,7 @@ main(argc, argv)
s1.str = argv[0];
s2.str = argv[1];
- if (cflag)
+ if (cflag || Cflag)
for (cnt = NCHARS, p = string1; cnt--;)
*p++ = OOBCH;
@@ -229,6 +241,9 @@ main(argc, argv)
if (cflag)
for (cnt = 0, p = string1; cnt < NCHARS; ++p, ++cnt)
*p = *p == OOBCH ? ch : cnt;
+ else if (Cflag)
+ for (cnt = 0, p = string1; cnt < NCHARS; ++p, ++cnt)
+ *p = *p == OOBCH && ISCHAR(cnt) ? ch : cnt;
if (sflag)
for (lastch = OOBCH; (ch = getchar()) != EOF;) {
@@ -245,11 +260,12 @@ main(argc, argv)
}
static void
-setup(string, arg, str, cflag)
+setup(string, arg, str, cflag, Cflag)
int *string;
char *arg;
STR *str;
int cflag;
+ int Cflag;
{
int cnt, *p;
@@ -260,15 +276,18 @@ setup(string, arg, str, cflag)
if (cflag)
for (p = string, cnt = NCHARS; cnt--; ++p)
*p = !*p;
+ else if (Cflag)
+ for (cnt = 0; cnt < NCHARS; cnt++)
+ string[cnt] = !string[cnt] && ISCHAR(cnt);
}
static void
usage()
{
(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
- "usage: tr [-csu] string1 string2",
- " tr [-cu] -d string1",
- " tr [-cu] -s string1",
- " tr [-cu] -ds string1 string2");
+ "usage: tr [-Ccsu] string1 string2",
+ " tr [-Ccu] -d string1",
+ " tr [-Ccu] -s string1",
+ " tr [-Ccu] -ds string1 string2");
exit(1);
}
OpenPOWER on IntegriCloud