Home | History | Annotate | Line # | Download | only in tr
      1  1.20  dholland /*	$NetBSD: tr.c,v 1.20 2013/08/11 01:54:35 dholland 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.20  dholland __RCSID("$NetBSD: tr.c,v 1.20 2013/08/11 01:54:35 dholland 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.16  dholland static int string1[NCHARS], string2[NCHARS];
     56   1.1       cgd 
     57  1.17  dholland 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.15  dholland 	int ch, ch2, lastch;
     64   1.2     glass 	int cflag, dflag, sflag, isstring2;
     65  1.11  dholland 	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.10  dholland 		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.17  dholland 		setup(string1, argv[0], 1, cflag);
    109  1.17  dholland 		setup(string2, argv[1], 2, 0);
    110  1.20  dholland 
    111  1.20  dholland 		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.17  dholland 		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.17  dholland 		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.18  dholland 	/*
    160  1.18  dholland 	 * The first and second strings need to be matched up. This
    161  1.18  dholland 	 * means that if we are doing -c, we need to scan the first
    162  1.18  dholland 	 * string in advance, complement it, and match *that* against
    163  1.18  dholland 	 * the second string; otherwise we need to scan them together.
    164  1.18  dholland 	 */
    165  1.18  dholland 
    166  1.15  dholland 	if (cflag) {
    167  1.18  dholland 		/*
    168  1.18  dholland 		 * Scan string 1 and complement it. After this,
    169  1.18  dholland 		 * string1[] contains 0 for chars to leave alone and 1
    170  1.18  dholland 		 * for chars to translate.
    171  1.18  dholland 		 */
    172  1.17  dholland 		setup(string1, argv[0], 1, cflag);
    173  1.17  dholland 		s1 = NULL; /* for safety */
    174  1.18  dholland 		/* we will use ch to iterate over string1, so start it */
    175  1.15  dholland 		ch = -1;
    176  1.15  dholland 	} else {
    177  1.18  dholland 		/* Create the scanner for string 1. */
    178  1.17  dholland 		s1 = str_create(1, argv[0]);
    179  1.16  dholland 		for (ch = 0; ch < NCHARS; ch++) {
    180  1.16  dholland 			string1[ch] = ch;
    181  1.16  dholland 		}
    182  1.15  dholland 	}
    183  1.18  dholland 	/* Create the scanner for string 2. */
    184  1.17  dholland 	s2 = str_create(2, argv[1]);
    185   1.2     glass 
    186  1.18  dholland 	/* Read the first char of string 2 first to make sure there is one. */
    187  1.11  dholland 	if (!next(s2, &ch2))
    188   1.6     lukem 		errx(1, "empty string2");
    189   1.2     glass 
    190  1.18  dholland 	/*
    191  1.18  dholland 	 * Loop over the chars from string 1. After this loop string1[]
    192  1.18  dholland 	 * is a mapping from input to output chars.
    193  1.18  dholland 	 */
    194  1.15  dholland 	while (1) {
    195  1.15  dholland 		if (cflag) {
    196  1.18  dholland 			/*
    197  1.18  dholland 			 * Try each character in order. For characters we
    198  1.18  dholland 			 * skip over because we aren't translating them,
    199  1.18  dholland 			 * set the translation to the identity.
    200  1.18  dholland 			 */
    201  1.15  dholland 			ch++;
    202  1.15  dholland 			while (ch < NCHARS && string1[ch] == 0) {
    203  1.15  dholland 				if (string1[ch] == 0) {
    204  1.15  dholland 					string1[ch] = ch;
    205  1.15  dholland 				}
    206  1.15  dholland 				ch++;
    207  1.15  dholland 			}
    208  1.15  dholland 			if (ch == NCHARS) {
    209  1.15  dholland 				break;
    210  1.15  dholland 			}
    211  1.15  dholland 		}
    212  1.15  dholland 		else {
    213  1.18  dholland 			/* Get the next character from string 1. */
    214  1.15  dholland 			if (!next(s1, &ch)) {
    215  1.15  dholland 				break;
    216  1.15  dholland 			}
    217  1.15  dholland 		}
    218  1.15  dholland 
    219  1.18  dholland 		/* Set the translation to the character from string 2. */
    220  1.14  dholland 		string1[ch] = ch2;
    221  1.18  dholland 
    222  1.18  dholland 		/* Note the characters to squeeze in string2[]. */
    223  1.14  dholland 		if (sflag) {
    224  1.10  dholland 			string2[ch2] = 1;
    225   1.1       cgd 		}
    226  1.18  dholland 
    227  1.18  dholland 		/*
    228  1.18  dholland 		 * Get the next character from string 2. If it runs
    229  1.18  dholland 		 * out, this will keep returning the last character
    230  1.18  dholland 		 * over and over again.
    231  1.18  dholland 		 */
    232  1.14  dholland 		(void)next(s2, &ch2);
    233  1.14  dholland 	}
    234   1.2     glass 
    235  1.18  dholland 	/*
    236  1.18  dholland 	 * Now do it.
    237  1.18  dholland 	 */
    238  1.18  dholland 
    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.15  dholland 
    251  1.18  dholland 	/* Clean up and exit. */
    252  1.17  dholland 	if (s1 != NULL) {
    253  1.17  dholland 		str_destroy(s1);
    254  1.17  dholland 	}
    255  1.11  dholland 	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.17  dholland setup(int *string, const char *arg, int whichstring, int cflag)
    261   1.1       cgd {
    262   1.6     lukem 	int cnt, *p;
    263  1.10  dholland 	int ch;
    264  1.17  dholland 	STR *str;
    265   1.1       cgd 
    266  1.17  dholland 	str = str_create(whichstring, arg);
    267  1.10  dholland 	while (next(str, &ch))
    268  1.10  dholland 		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.17  dholland 	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