Home | History | Annotate | Line # | Download | only in tr
tr.c revision 1.9.8.1
      1  1.9.8.1    tls /*	$NetBSD: tr.c,v 1.9.8.1 2014/08/20 00:05:05 tls Exp $	*/
      2      1.4    jtc 
      3      1.1    cgd /*
      4      1.4    jtc  * Copyright (c) 1988, 1993
      5      1.4    jtc  *	The Regents of the University of California.  All rights reserved.
      6      1.1    cgd  *
      7      1.1    cgd  * Redistribution and use in source and binary forms, with or without
      8      1.1    cgd  * modification, are permitted provided that the following conditions
      9      1.1    cgd  * are met:
     10      1.1    cgd  * 1. Redistributions of source code must retain the above copyright
     11      1.1    cgd  *    notice, this list of conditions and the following disclaimer.
     12      1.1    cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1    cgd  *    notice, this list of conditions and the following disclaimer in the
     14      1.1    cgd  *    documentation and/or other materials provided with the distribution.
     15      1.7    agc  * 3. Neither the name of the University nor the names of its contributors
     16      1.1    cgd  *    may be used to endorse or promote products derived from this software
     17      1.1    cgd  *    without specific prior written permission.
     18      1.1    cgd  *
     19      1.1    cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20      1.1    cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21      1.1    cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22      1.1    cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23      1.1    cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24      1.1    cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25      1.1    cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26      1.1    cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27      1.1    cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28      1.1    cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29      1.1    cgd  * SUCH DAMAGE.
     30      1.1    cgd  */
     31      1.1    cgd 
     32      1.6  lukem #include <sys/cdefs.h>
     33      1.1    cgd #ifndef lint
     34      1.8  lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
     35      1.8  lukem  The Regents of the University of California.  All rights reserved.");
     36      1.1    cgd #endif /* not lint */
     37      1.1    cgd 
     38      1.1    cgd #ifndef lint
     39      1.4    jtc #if 0
     40      1.5    jtc static char sccsid[] = "@(#)tr.c	8.2 (Berkeley) 5/4/95";
     41      1.4    jtc #endif
     42  1.9.8.1    tls __RCSID("$NetBSD: tr.c,v 1.9.8.1 2014/08/20 00:05:05 tls Exp $");
     43      1.1    cgd #endif /* not lint */
     44      1.1    cgd 
     45      1.1    cgd #include <sys/types.h>
     46      1.5    jtc 
     47      1.6  lukem #include <err.h>
     48      1.1    cgd #include <stdio.h>
     49      1.2  glass #include <stdlib.h>
     50      1.2  glass #include <string.h>
     51      1.5    jtc #include <unistd.h>
     52      1.5    jtc 
     53      1.2  glass #include "extern.h"
     54      1.2  glass 
     55  1.9.8.1    tls static int string1[NCHARS], string2[NCHARS];
     56      1.1    cgd 
     57  1.9.8.1    tls static void setup(int *, const char *, int, int);
     58      1.9  joerg __dead static void usage(void);
     59      1.1    cgd 
     60      1.2  glass int
     61      1.9  joerg main(int argc, char **argv)
     62      1.1    cgd {
     63  1.9.8.1    tls 	int ch, ch2, lastch;
     64      1.2  glass 	int cflag, dflag, sflag, isstring2;
     65  1.9.8.1    tls 	STR *s1, *s2;
     66      1.1    cgd 
     67      1.1    cgd 	cflag = dflag = sflag = 0;
     68      1.6  lukem 	while ((ch = getopt(argc, argv, "cds")) != -1)
     69  1.9.8.1    tls 		switch (ch) {
     70      1.1    cgd 		case 'c':
     71      1.1    cgd 			cflag = 1;
     72      1.1    cgd 			break;
     73      1.1    cgd 		case 'd':
     74      1.1    cgd 			dflag = 1;
     75      1.1    cgd 			break;
     76      1.1    cgd 		case 's':
     77      1.1    cgd 			sflag = 1;
     78      1.1    cgd 			break;
     79      1.1    cgd 		case '?':
     80      1.1    cgd 		default:
     81      1.2  glass 			usage();
     82      1.1    cgd 		}
     83      1.1    cgd 	argc -= optind;
     84      1.1    cgd 	argv += optind;
     85      1.1    cgd 
     86      1.2  glass 	switch(argc) {
     87      1.2  glass 	case 0:
     88      1.2  glass 	default:
     89      1.2  glass 		usage();
     90      1.2  glass 		/* NOTREACHED */
     91      1.2  glass 	case 1:
     92      1.2  glass 		isstring2 = 0;
     93      1.2  glass 		break;
     94      1.2  glass 	case 2:
     95      1.2  glass 		isstring2 = 1;
     96      1.2  glass 		break;
     97      1.2  glass 	}
     98      1.2  glass 
     99      1.2  glass 	/*
    100      1.2  glass 	 * tr -ds [-c] string1 string2
    101      1.2  glass 	 * Delete all characters (or complemented characters) in string1.
    102      1.2  glass 	 * Squeeze all characters in string2.
    103      1.2  glass 	 */
    104      1.2  glass 	if (dflag && sflag) {
    105      1.2  glass 		if (!isstring2)
    106      1.2  glass 			usage();
    107      1.2  glass 
    108  1.9.8.1    tls 		setup(string1, argv[0], 1, cflag);
    109  1.9.8.1    tls 		setup(string2, argv[1], 2, 0);
    110  1.9.8.1    tls 
    111  1.9.8.1    tls 		for (lastch = OOBCH; (ch = getchar()) != EOF; )
    112      1.2  glass 			if (!string1[ch] && (!string2[ch] || lastch != ch)) {
    113      1.2  glass 				lastch = ch;
    114      1.2  glass 				(void)putchar(ch);
    115      1.2  glass 			}
    116      1.2  glass 		exit(0);
    117      1.2  glass 	}
    118      1.2  glass 
    119      1.1    cgd 	/*
    120      1.2  glass 	 * tr -d [-c] string1
    121      1.2  glass 	 * Delete all characters (or complemented characters) in string1.
    122      1.1    cgd 	 */
    123      1.2  glass 	if (dflag) {
    124      1.2  glass 		if (isstring2)
    125      1.2  glass 			usage();
    126      1.2  glass 
    127  1.9.8.1    tls 		setup(string1, argv[0], 1, cflag);
    128      1.2  glass 
    129      1.1    cgd 		while ((ch = getchar()) != EOF)
    130      1.2  glass 			if (!string1[ch])
    131      1.2  glass 				(void)putchar(ch);
    132      1.1    cgd 		exit(0);
    133      1.1    cgd 	}
    134      1.1    cgd 
    135      1.2  glass 	/*
    136      1.2  glass 	 * tr -s [-c] string1
    137      1.2  glass 	 * Squeeze all characters (or complemented characters) in string1.
    138      1.2  glass 	 */
    139      1.2  glass 	if (sflag && !isstring2) {
    140  1.9.8.1    tls 		setup(string1, argv[0], 1, cflag);
    141      1.2  glass 
    142      1.2  glass 		for (lastch = OOBCH; (ch = getchar()) != EOF;)
    143      1.2  glass 			if (!string1[ch] || lastch != ch) {
    144      1.1    cgd 				lastch = ch;
    145      1.2  glass 				(void)putchar(ch);
    146      1.1    cgd 			}
    147      1.2  glass 		exit(0);
    148      1.2  glass 	}
    149      1.2  glass 
    150      1.2  glass 	/*
    151      1.2  glass 	 * tr [-cs] string1 string2
    152      1.2  glass 	 * Replace all characters (or complemented characters) in string1 with
    153      1.2  glass 	 * the character in the same position in string2.  If the -s option is
    154      1.2  glass 	 * specified, squeeze all the characters in string2.
    155      1.2  glass 	 */
    156      1.2  glass 	if (!isstring2)
    157      1.2  glass 		usage();
    158      1.2  glass 
    159  1.9.8.1    tls 	/*
    160  1.9.8.1    tls 	 * The first and second strings need to be matched up. This
    161  1.9.8.1    tls 	 * means that if we are doing -c, we need to scan the first
    162  1.9.8.1    tls 	 * string in advance, complement it, and match *that* against
    163  1.9.8.1    tls 	 * the second string; otherwise we need to scan them together.
    164  1.9.8.1    tls 	 */
    165      1.2  glass 
    166  1.9.8.1    tls 	if (cflag) {
    167  1.9.8.1    tls 		/*
    168  1.9.8.1    tls 		 * Scan string 1 and complement it. After this,
    169  1.9.8.1    tls 		 * string1[] contains 0 for chars to leave alone and 1
    170  1.9.8.1    tls 		 * for chars to translate.
    171  1.9.8.1    tls 		 */
    172  1.9.8.1    tls 		setup(string1, argv[0], 1, cflag);
    173  1.9.8.1    tls 		s1 = NULL; /* for safety */
    174  1.9.8.1    tls 		/* we will use ch to iterate over string1, so start it */
    175  1.9.8.1    tls 		ch = -1;
    176  1.9.8.1    tls 	} else {
    177  1.9.8.1    tls 		/* Create the scanner for string 1. */
    178  1.9.8.1    tls 		s1 = str_create(1, argv[0]);
    179  1.9.8.1    tls 		for (ch = 0; ch < NCHARS; ch++) {
    180  1.9.8.1    tls 			string1[ch] = ch;
    181  1.9.8.1    tls 		}
    182  1.9.8.1    tls 	}
    183  1.9.8.1    tls 	/* Create the scanner for string 2. */
    184  1.9.8.1    tls 	s2 = str_create(2, argv[1]);
    185      1.2  glass 
    186  1.9.8.1    tls 	/* Read the first char of string 2 first to make sure there is one. */
    187  1.9.8.1    tls 	if (!next(s2, &ch2))
    188      1.6  lukem 		errx(1, "empty string2");
    189      1.2  glass 
    190  1.9.8.1    tls 	/*
    191  1.9.8.1    tls 	 * Loop over the chars from string 1. After this loop string1[]
    192  1.9.8.1    tls 	 * is a mapping from input to output chars.
    193  1.9.8.1    tls 	 */
    194  1.9.8.1    tls 	while (1) {
    195  1.9.8.1    tls 		if (cflag) {
    196  1.9.8.1    tls 			/*
    197  1.9.8.1    tls 			 * Try each character in order. For characters we
    198  1.9.8.1    tls 			 * skip over because we aren't translating them,
    199  1.9.8.1    tls 			 * set the translation to the identity.
    200  1.9.8.1    tls 			 */
    201  1.9.8.1    tls 			ch++;
    202  1.9.8.1    tls 			while (ch < NCHARS && string1[ch] == 0) {
    203  1.9.8.1    tls 				if (string1[ch] == 0) {
    204  1.9.8.1    tls 					string1[ch] = ch;
    205  1.9.8.1    tls 				}
    206  1.9.8.1    tls 				ch++;
    207  1.9.8.1    tls 			}
    208  1.9.8.1    tls 			if (ch == NCHARS) {
    209  1.9.8.1    tls 				break;
    210  1.9.8.1    tls 			}
    211      1.2  glass 		}
    212  1.9.8.1    tls 		else {
    213  1.9.8.1    tls 			/* Get the next character from string 1. */
    214  1.9.8.1    tls 			if (!next(s1, &ch)) {
    215  1.9.8.1    tls 				break;
    216  1.9.8.1    tls 			}
    217      1.1    cgd 		}
    218      1.2  glass 
    219  1.9.8.1    tls 		/* Set the translation to the character from string 2. */
    220  1.9.8.1    tls 		string1[ch] = ch2;
    221  1.9.8.1    tls 
    222  1.9.8.1    tls 		/* Note the characters to squeeze in string2[]. */
    223  1.9.8.1    tls 		if (sflag) {
    224  1.9.8.1    tls 			string2[ch2] = 1;
    225  1.9.8.1    tls 		}
    226  1.9.8.1    tls 
    227  1.9.8.1    tls 		/*
    228  1.9.8.1    tls 		 * Get the next character from string 2. If it runs
    229  1.9.8.1    tls 		 * out, this will keep returning the last character
    230  1.9.8.1    tls 		 * over and over again.
    231  1.9.8.1    tls 		 */
    232  1.9.8.1    tls 		(void)next(s2, &ch2);
    233  1.9.8.1    tls 	}
    234  1.9.8.1    tls 
    235  1.9.8.1    tls 	/*
    236  1.9.8.1    tls 	 * Now do it.
    237  1.9.8.1    tls 	 */
    238      1.2  glass 
    239      1.2  glass 	if (sflag)
    240      1.2  glass 		for (lastch = OOBCH; (ch = getchar()) != EOF;) {
    241      1.2  glass 			ch = string1[ch];
    242      1.2  glass 			if (!string2[ch] || lastch != ch) {
    243      1.1    cgd 				lastch = ch;
    244      1.2  glass 				(void)putchar(ch);
    245      1.1    cgd 			}
    246      1.2  glass 		}
    247      1.2  glass 	else
    248      1.2  glass 		while ((ch = getchar()) != EOF)
    249      1.2  glass 			(void)putchar(string1[ch]);
    250  1.9.8.1    tls 
    251  1.9.8.1    tls 	/* Clean up and exit. */
    252  1.9.8.1    tls 	if (s1 != NULL) {
    253  1.9.8.1    tls 		str_destroy(s1);
    254  1.9.8.1    tls 	}
    255  1.9.8.1    tls 	str_destroy(s2);
    256      1.2  glass 	exit (0);
    257      1.1    cgd }
    258      1.1    cgd 
    259      1.2  glass static void
    260  1.9.8.1    tls setup(int *string, const char *arg, int whichstring, int cflag)
    261      1.1    cgd {
    262      1.6  lukem 	int cnt, *p;
    263  1.9.8.1    tls 	int ch;
    264  1.9.8.1    tls 	STR *str;
    265      1.1    cgd 
    266  1.9.8.1    tls 	str = str_create(whichstring, arg);
    267  1.9.8.1    tls 	while (next(str, &ch))
    268  1.9.8.1    tls 		string[ch] = 1;
    269      1.2  glass 	if (cflag)
    270      1.2  glass 		for (p = string, cnt = NCHARS; cnt--; ++p)
    271      1.2  glass 			*p = !*p;
    272  1.9.8.1    tls 	str_destroy(str);
    273      1.1    cgd }
    274      1.1    cgd 
    275      1.2  glass static void
    276      1.9  joerg usage(void)
    277      1.1    cgd {
    278      1.2  glass 	(void)fprintf(stderr, "usage: tr [-cs] string1 string2\n");
    279      1.2  glass 	(void)fprintf(stderr, "       tr [-c] -d string1\n");
    280      1.2  glass 	(void)fprintf(stderr, "       tr [-c] -s string1\n");
    281      1.2  glass 	(void)fprintf(stderr, "       tr [-c] -ds string1 string2\n");
    282      1.2  glass 	exit(1);
    283      1.1    cgd }
    284