/*
* 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
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 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 <ctype.h>
#include <err.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
#include "cmap.h"
#include "cset.h"
#include "extern.h"
static void usage(void);
static wint_t
{
}
static wint_t
{
}
static inline bool
{
}
int
{
int n, *p;
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':
break;
case '?':
default:
usage();
}
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 (!isstring2)
usage();
}
exit(0);
}
/*
* tr -d [-Cc] string1
* Delete all characters (or complemented characters) in string1.
*/
if (dflag) {
if (isstring2)
usage();
exit(0);
}
/*
* tr -s [-Cc] string1
* Squeeze all characters (or complemented characters) in string1.
*/
}
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();
squeeze = cset_alloc();
} else
/*
* 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. */
do {
goto endloop;
/* skip upper set */
do {
break;
goto again;
do {
goto endloop;
/* skip lower set */
do {
break;
goto again;
} else {
if (sflag)
}
}
/*
* 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.
*/
continue;
if (sflag)
} else
break;
}
} else if (Cflag) {
*p++ = cnt;
else
}
n = p - carray;
if (Cflag && n > 1)
/*
* Chars taken from s2 can be different this time
* so fill string2 again to not miss some.
*/
if (sflag)
}
}
if (sflag)
}
}
else
}
exit(0);
}
static struct cset *
{
cs = cset_alloc();
if (Cflag)
cset_cache(cs);
return (cs);
}
int
charcoll(const void *a, const void *b)
{
sa[0] = *(const int *)a;
sb[0] = *(const int *)b;
}
static void
usage(void)
{
"usage: tr [-Ccsu] string1 string2",
" tr [-Ccu] -d string1",
" tr [-Ccu] -s string1",
" tr [-Ccu] -ds string1 string2");
exit(1);
}