Home | History | Annotate | Line # | Download | only in tr
tr.c revision 1.16
      1  1.16  dholland /*	$NetBSD: tr.c,v 1.16 2013/08/11 00:34:09 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.16  dholland __RCSID("$NetBSD: tr.c,v 1.16 2013/08/11 00:34:09 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.12  dholland static void setup(int *, const char *, STR *, 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.11  dholland 	s1 = str_create(1);
    100  1.11  dholland 	s2 = str_create(2);
    101  1.11  dholland 
    102   1.2     glass 	/*
    103   1.2     glass 	 * tr -ds [-c] string1 string2
    104   1.2     glass 	 * Delete all characters (or complemented characters) in string1.
    105   1.2     glass 	 * Squeeze all characters in string2.
    106   1.2     glass 	 */
    107   1.2     glass 	if (dflag && sflag) {
    108   1.2     glass 		if (!isstring2)
    109   1.2     glass 			usage();
    110   1.2     glass 
    111  1.11  dholland 		setup(string1, argv[0], s1, cflag);
    112  1.11  dholland 		setup(string2, argv[1], s2, 0);
    113   1.2     glass 
    114   1.2     glass 		for (lastch = OOBCH; (ch = getchar()) != EOF;)
    115   1.2     glass 			if (!string1[ch] && (!string2[ch] || lastch != ch)) {
    116   1.2     glass 				lastch = ch;
    117   1.2     glass 				(void)putchar(ch);
    118   1.2     glass 			}
    119  1.11  dholland 		str_destroy(s1);
    120  1.11  dholland 		str_destroy(s2);
    121   1.2     glass 		exit(0);
    122   1.2     glass 	}
    123   1.2     glass 
    124   1.1       cgd 	/*
    125   1.2     glass 	 * tr -d [-c] string1
    126   1.2     glass 	 * Delete all characters (or complemented characters) in string1.
    127   1.1       cgd 	 */
    128   1.2     glass 	if (dflag) {
    129   1.2     glass 		if (isstring2)
    130   1.2     glass 			usage();
    131   1.2     glass 
    132  1.11  dholland 		setup(string1, argv[0], s1, cflag);
    133   1.2     glass 
    134   1.1       cgd 		while ((ch = getchar()) != EOF)
    135   1.2     glass 			if (!string1[ch])
    136   1.2     glass 				(void)putchar(ch);
    137  1.11  dholland 		str_destroy(s1);
    138  1.11  dholland 		str_destroy(s2);
    139   1.1       cgd 		exit(0);
    140   1.1       cgd 	}
    141   1.1       cgd 
    142   1.2     glass 	/*
    143   1.2     glass 	 * tr -s [-c] string1
    144   1.2     glass 	 * Squeeze all characters (or complemented characters) in string1.
    145   1.2     glass 	 */
    146   1.2     glass 	if (sflag && !isstring2) {
    147  1.11  dholland 		setup(string1, argv[0], s1, cflag);
    148   1.2     glass 
    149   1.2     glass 		for (lastch = OOBCH; (ch = getchar()) != EOF;)
    150   1.2     glass 			if (!string1[ch] || lastch != ch) {
    151   1.1       cgd 				lastch = ch;
    152   1.2     glass 				(void)putchar(ch);
    153   1.1       cgd 			}
    154  1.11  dholland 		str_destroy(s1);
    155  1.11  dholland 		str_destroy(s2);
    156   1.2     glass 		exit(0);
    157   1.2     glass 	}
    158   1.2     glass 
    159   1.2     glass 	/*
    160   1.2     glass 	 * tr [-cs] string1 string2
    161   1.2     glass 	 * Replace all characters (or complemented characters) in string1 with
    162   1.2     glass 	 * the character in the same position in string2.  If the -s option is
    163   1.2     glass 	 * specified, squeeze all the characters in string2.
    164   1.2     glass 	 */
    165   1.2     glass 	if (!isstring2)
    166   1.2     glass 		usage();
    167   1.2     glass 
    168  1.15  dholland 	if (cflag) {
    169  1.15  dholland 		setup(string1, argv[0], s1, cflag);
    170  1.15  dholland 		ch = -1;
    171  1.15  dholland 	} else {
    172  1.15  dholland 		str_setstring(s1, argv[0]);
    173  1.16  dholland 		for (ch = 0; ch < NCHARS; ch++) {
    174  1.16  dholland 			string1[ch] = ch;
    175  1.16  dholland 		}
    176  1.15  dholland 	}
    177  1.11  dholland 	str_setstring(s2, argv[1]);
    178   1.2     glass 
    179  1.11  dholland 	if (!next(s2, &ch2))
    180   1.6     lukem 		errx(1, "empty string2");
    181   1.2     glass 
    182   1.2     glass 	/* If string2 runs out of characters, use the last one specified. */
    183  1.15  dholland 	while (1) {
    184  1.15  dholland 		if (cflag) {
    185  1.15  dholland 			ch++;
    186  1.15  dholland 			while (ch < NCHARS && string1[ch] == 0) {
    187  1.15  dholland 				if (string1[ch] == 0) {
    188  1.15  dholland 					string1[ch] = ch;
    189  1.15  dholland 				}
    190  1.15  dholland 				ch++;
    191  1.15  dholland 			}
    192  1.15  dholland 			if (ch == NCHARS) {
    193  1.15  dholland 				break;
    194  1.15  dholland 			}
    195  1.15  dholland 		}
    196  1.15  dholland 		else {
    197  1.15  dholland 			if (!next(s1, &ch)) {
    198  1.15  dholland 				break;
    199  1.15  dholland 			}
    200  1.15  dholland 		}
    201  1.15  dholland 
    202  1.14  dholland 		string1[ch] = ch2;
    203  1.14  dholland 		if (sflag) {
    204  1.10  dholland 			string2[ch2] = 1;
    205   1.1       cgd 		}
    206  1.14  dholland 		(void)next(s2, &ch2);
    207  1.14  dholland 	}
    208   1.2     glass 
    209   1.2     glass 	if (sflag)
    210   1.2     glass 		for (lastch = OOBCH; (ch = getchar()) != EOF;) {
    211   1.2     glass 			ch = string1[ch];
    212   1.2     glass 			if (!string2[ch] || lastch != ch) {
    213   1.1       cgd 				lastch = ch;
    214   1.2     glass 				(void)putchar(ch);
    215   1.1       cgd 			}
    216   1.2     glass 		}
    217   1.2     glass 	else
    218   1.2     glass 		while ((ch = getchar()) != EOF)
    219   1.2     glass 			(void)putchar(string1[ch]);
    220  1.15  dholland 
    221  1.11  dholland 	str_destroy(s1);
    222  1.11  dholland 	str_destroy(s2);
    223   1.2     glass 	exit (0);
    224   1.1       cgd }
    225   1.1       cgd 
    226   1.2     glass static void
    227  1.12  dholland setup(int *string, const char *arg, STR *str, int cflag)
    228   1.1       cgd {
    229   1.6     lukem 	int cnt, *p;
    230  1.10  dholland 	int ch;
    231   1.1       cgd 
    232  1.11  dholland 	str_setstring(str, arg);
    233   1.6     lukem 	memset(string, 0, NCHARS * sizeof(int));
    234  1.10  dholland 	while (next(str, &ch))
    235  1.10  dholland 		string[ch] = 1;
    236   1.2     glass 	if (cflag)
    237   1.2     glass 		for (p = string, cnt = NCHARS; cnt--; ++p)
    238   1.2     glass 			*p = !*p;
    239   1.1       cgd }
    240   1.1       cgd 
    241   1.2     glass static void
    242   1.9     joerg usage(void)
    243   1.1       cgd {
    244   1.2     glass 	(void)fprintf(stderr, "usage: tr [-cs] string1 string2\n");
    245   1.2     glass 	(void)fprintf(stderr, "       tr [-c] -d string1\n");
    246   1.2     glass 	(void)fprintf(stderr, "       tr [-c] -s string1\n");
    247   1.2     glass 	(void)fprintf(stderr, "       tr [-c] -ds string1 string2\n");
    248   1.2     glass 	exit(1);
    249   1.1       cgd }
    250