From c1bfcc911924192928b6528ce89d47b84c4398ce Mon Sep 17 00:00:00 2001 From: Ben Gras Date: Thu, 8 Apr 2010 15:08:31 +0000 Subject: [PATCH] port of netbsd's tr --- commands/simple/Makefile | 7 ++++ commands/simple/str.c | 83 +++++++++++++++++++++------------------- commands/simple/tr.c | 24 +++++++----- commands/simple/tr.h | 2 +- 4 files changed, 67 insertions(+), 49 deletions(-) diff --git a/commands/simple/Makefile b/commands/simple/Makefile index eabd90af2..69111c993 100644 --- a/commands/simple/Makefile +++ b/commands/simple/Makefile @@ -813,6 +813,9 @@ touch: touch.c top: top.c $(CCLD) -o $@ $< -lcurses +tr: tr.c str.c + $(CCLD) -o $@ tr.c str.c + tsort: tsort.c $(CCLD) -o $@ $< @install -S 4kw $@ @@ -1076,6 +1079,7 @@ install: \ /usr/bin/time \ /usr/bin/top \ /usr/bin/touch \ + /usr/bin/tr \ /usr/bin/truncate \ /usr/bin/tsort \ /usr/bin/ttt \ @@ -1622,6 +1626,9 @@ install: \ /usr/bin/touch: touch install -cs -o bin $> $@ +/usr/bin/tr: tr + install -cs -o bin $> $@ + /usr/bin/truncate: truncate install -cs -o bin $> $@ diff --git a/commands/simple/str.c b/commands/simple/str.c index 052e2b34e..88dcebbab 100644 --- a/commands/simple/str.c +++ b/commands/simple/str.c @@ -29,14 +29,6 @@ * SUCH DAMAGE. */ -#include -#ifndef lint -#if 0 -static char sccsid[] = "@(#)str.c 8.2 (Berkeley) 4/28/95"; -#endif -__RCSID("$NetBSD: str.c,v 1.12 2009/04/13 23:50:49 lukem Exp $"); -#endif /* not lint */ - #include #include @@ -47,15 +39,15 @@ __RCSID("$NetBSD: str.c,v 1.12 2009/04/13 23:50:49 lukem Exp $"); #include #include -#include "extern.h" +#include "tr.h" -static int backslash __P((STR *)); -static int bracket __P((STR *)); -static int c_class __P((const void *, const void *)); -static void genclass __P((STR *)); -static void genequiv __P((STR *)); -static int genrange __P((STR *)); -static void genseq __P((STR *)); +static int backslash (STR *); +static int bracket (STR *); +static int c_class (const void *, const void *); +static void genclass (STR *); +static void genequiv (STR *); +static int genrange (STR *); +static void genseq (STR *); int next(s) @@ -122,21 +114,21 @@ bracket(s) switch (s->str[1]) { case ':': /* "[:class:]" */ - if ((p = strstr(s->str + 2, ":]")) == NULL) + if ((p = strstr((char *) s->str + 2, ":]")) == NULL) return (0); *p = '\0'; s->str += 2; genclass(s); - s->str = p + 2; + s->str = (unsigned char *) p + 2; return (1); case '=': /* "[=equiv=]" */ - if ((p = strstr(s->str + 2, "=]")) == NULL) + if ((p = strstr((char *) s->str + 2, "=]")) == NULL) return (0); s->str += 2; genequiv(s); return (1); default: /* "[\###*n]" or "[#*n]" */ - if ((p = strpbrk(s->str + 2, "*]")) == NULL) + if ((p = strpbrk((char *) s->str + 2, "*]")) == NULL) return (0); if (p[0] != '*' || strchr(p, ']') == NULL) return (0); @@ -149,7 +141,7 @@ bracket(s) typedef struct { const char *name; - int (*func) __P((int)); + int (*func) (int); int *set; } CLASS; @@ -172,17 +164,21 @@ static void genclass(s) STR *s; { - int cnt, (*func) __P((int)); + int cnt, (*func) (int); CLASS *cp, tmp; int *p; - tmp.name = s->str; + tmp.name = (char *) s->str; if ((cp = (CLASS *)bsearch(&tmp, classes, sizeof(classes) / - sizeof(CLASS), sizeof(CLASS), c_class)) == NULL) - errx(1, "unknown class %s", s->str); + sizeof(CLASS), sizeof(CLASS), c_class)) == NULL) { + fprintf(stderr, "tr: unknown class %s\n", s->str); + exit(1); + } - if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL) - err(1, "malloc"); + if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL) { + perror("malloc"); + exit(1); + } memset(p, 0, (NCHARS + 1) * sizeof(int)); for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt) if ((func)(cnt)) @@ -211,12 +207,16 @@ genequiv(s) { if (*s->str == '\\') { s->equiv[0] = backslash(s); - if (*s->str != '=') - errx(1, "misplaced equivalence equals sign"); + if (*s->str != '=') { + fprintf(stderr, "tr: misplaced equivalence equals sign\n"); + exit(1); + } } else { s->equiv[0] = s->str[0]; - if (s->str[1] != '=') - errx(1, "misplaced equivalence equals sign"); + if (s->str[1] != '=') { + fprintf(stderr, "tr: misplaced equivalence equals sign\n"); + exit(1); + } } s->str += 2; s->cnt = 0; @@ -229,7 +229,7 @@ genrange(s) STR *s; { int stopval; - char *savestart; + unsigned char *savestart; savestart = s->str; stopval = *++s->str == '\\' ? backslash(s) : *s->str++; @@ -249,15 +249,19 @@ genseq(s) { char *ep; - if (s->which == STRING1) - errx(1, "sequences only valid in string2"); + if (s->which == STRING1) { + fprintf(stderr, "tr: sequences only valid in string2\n"); + exit(1); + } if (*s->str == '\\') s->lastch = backslash(s); else s->lastch = *s->str++; - if (*s->str != '*') - errx(1, "misplaced sequence asterisk"); + if (*s->str != '*') { + fprintf(stderr, "tr: misplaced sequence asterisk\n"); + exit(1); + } switch (*++s->str) { case '\\': @@ -269,13 +273,14 @@ genseq(s) break; default: if (isdigit(*s->str)) { - s->cnt = strtol(s->str, &ep, 0); + s->cnt = strtol((char *) s->str, &ep, 0); if (*ep == ']') { - s->str = ep + 1; + s->str = (unsigned char *) ep + 1; break; } } - errx(1, "illegal sequence count"); + fprintf(stderr, "tr: illegal sequence count\n"); + exit(1); /* NOTREACHED */ } diff --git a/commands/simple/tr.c b/commands/simple/tr.c index 814f245c9..c014b7cfa 100644 --- a/commands/simple/tr.c +++ b/commands/simple/tr.c @@ -29,18 +29,22 @@ * SUCH DAMAGE. */ +#if 0 #include #ifndef lint __COPYRIGHT("@(#) Copyright (c) 1988, 1993\ The Regents of the University of California. All rights reserved."); #endif /* not lint */ +#endif +#if 0 #ifndef lint #if 0 static char sccsid[] = "@(#)tr.c 8.2 (Berkeley) 5/4/95"; #endif __RCSID("$NetBSD: tr.c,v 1.8 2008/07/21 14:19:27 lukem Exp $"); #endif /* not lint */ +#endif #include @@ -50,7 +54,7 @@ __RCSID("$NetBSD: tr.c,v 1.8 2008/07/21 14:19:27 lukem Exp $"); #include #include -#include "extern.h" +#include "tr.h" static int string1[NCHARS] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* ASCII */ @@ -90,9 +94,9 @@ static int string1[NCHARS] = { STR s1 = { STRING1, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL }; STR s2 = { STRING2, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL }; -int main __P((int, char **)); -static void setup __P((int *, char *, STR *, int)); -static void usage __P((void)); +int main (int, char **); +static void setup (int *, char *, STR *, int); +static void usage (void); int main(argc, argv) @@ -194,15 +198,17 @@ main(argc, argv) if (!isstring2) usage(); - s1.str = argv[0]; - s2.str = argv[1]; + s1.str = (unsigned char *) argv[0]; + s2.str = (unsigned char *) argv[1]; if (cflag) for (cnt = NCHARS, p = string1; cnt--;) *p++ = OOBCH; - if (!next(&s2)) - errx(1, "empty string2"); + if (!next(&s2)) { + fprintf(stderr, "empty string2\n"); + exit(1); + } /* If string2 runs out of characters, use the last one specified. */ if (sflag) @@ -244,7 +250,7 @@ setup(string, arg, str, cflag) { int cnt, *p; - str->str = arg; + str->str = (unsigned char *) arg; memset(string, 0, NCHARS * sizeof(int)); while (next(str)) string[str->lastch] = 1; diff --git a/commands/simple/tr.h b/commands/simple/tr.h index b0bd04e43..1c5ef3438 100644 --- a/commands/simple/tr.h +++ b/commands/simple/tr.h @@ -45,4 +45,4 @@ typedef struct { #define NCHARS (UCHAR_MAX + 1) /* Number of possible characters. */ #define OOBCH (UCHAR_MAX + 1) /* Out of band character value. */ -int next __P((STR *)); +int next (STR *); -- 2.44.0