minix/usr.bin/tr/str.c

325 lines
6.7 KiB
C
Raw Normal View History

/* $NetBSD: str.c,v 1.19 2011/09/08 12:00:26 christos Exp $ */
2010-04-08 17:04:57 +02:00
/*-
* Copyright (c) 1991, 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
* documentation and/or other materials provided with the distribution.
* 3. 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 <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)str.c 8.2 (Berkeley) 4/28/95";
#endif
__RCSID("$NetBSD: str.c,v 1.19 2011/09/08 12:00:26 christos Exp $");
#endif /* not lint */
2010-04-08 17:04:57 +02:00
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "extern.h"
2010-04-08 17:04:57 +02:00
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 *);
2010-04-08 17:04:57 +02:00
int
next(STR *s)
2010-04-08 17:04:57 +02:00
{
int ch;
switch (s->state) {
case EOS:
return 0;
2010-04-08 17:04:57 +02:00
case INFINITE:
return 1;
2010-04-08 17:04:57 +02:00
case NORMAL:
switch (ch = *s->str) {
case '\0':
s->state = EOS;
return 0;
2010-04-08 17:04:57 +02:00
case '\\':
s->lastch = backslash(s);
break;
case '[':
if (bracket(s))
return next(s);
2010-04-08 17:04:57 +02:00
/* FALLTHROUGH */
default:
++s->str;
s->lastch = ch;
break;
}
/* We can start a range at any time. */
if (s->str[0] == '-' && genrange(s))
return next(s);
return 1;
2010-04-08 17:04:57 +02:00
case RANGE:
if (s->cnt-- == 0) {
s->state = NORMAL;
return next(s);
2010-04-08 17:04:57 +02:00
}
++s->lastch;
return 1;
2010-04-08 17:04:57 +02:00
case SEQUENCE:
if (s->cnt-- == 0) {
s->state = NORMAL;
return next(s);
2010-04-08 17:04:57 +02:00
}
return 1;
2010-04-08 17:04:57 +02:00
case SET:
if ((s->lastch = s->set[s->cnt++]) == OOBCH) {
s->state = NORMAL;
return next(s);
2010-04-08 17:04:57 +02:00
}
return 1;
2010-04-08 17:04:57 +02:00
}
/* NOTREACHED */
return 0;
2010-04-08 17:04:57 +02:00
}
static int
bracket(STR *s)
2010-04-08 17:04:57 +02:00
{
char *p;
switch (s->str[1]) {
case ':': /* "[:class:]" */
if ((p = strstr(s->str + 2, ":]")) == NULL)
return 0;
2010-04-08 17:04:57 +02:00
*p = '\0';
s->str += 2;
genclass(s);
s->str = p + 2;
return 1;
2010-04-08 17:04:57 +02:00
case '=': /* "[=equiv=]" */
if ((p = strstr(s->str + 2, "=]")) == NULL)
return 0;
2010-04-08 17:04:57 +02:00
s->str += 2;
genequiv(s);
return 1;
2010-04-08 17:04:57 +02:00
default: /* "[\###*n]" or "[#*n]" */
if ((p = strpbrk(s->str + 2, "*]")) == NULL)
return 0;
2010-04-08 17:04:57 +02:00
if (p[0] != '*' || strchr(p, ']') == NULL)
return 0;
2010-04-08 17:04:57 +02:00
s->str += 1;
genseq(s);
return 1;
2010-04-08 17:04:57 +02:00
}
/* NOTREACHED */
}
typedef struct {
const char *name;
int (*func)(int);
2010-04-08 17:04:57 +02:00
} CLASS;
static const CLASS classes[] = {
{ "alnum", isalnum },
{ "alpha", isalpha },
{ "blank", isblank },
{ "cntrl", iscntrl },
{ "digit", isdigit },
{ "graph", isgraph },
{ "lower", islower },
{ "print", isprint },
{ "punct", ispunct },
{ "space", isspace },
{ "upper", isupper },
{ "xdigit", isxdigit },
2010-04-08 17:04:57 +02:00
};
static void
genclass(STR *s)
2010-04-08 17:04:57 +02:00
{
int cnt;
const CLASS *cp;
CLASS tmp;
2010-04-08 17:04:57 +02:00
int *p;
tmp.name = s->str;
if ((cp = bsearch(&tmp, classes, sizeof(classes) /
sizeof(*cp), sizeof(*cp), c_class)) == NULL)
errx(1, "unknown class %s", s->str);
2010-04-08 17:04:57 +02:00
if ((s->set = p = malloc((NCHARS + 1) * sizeof(*p))) == NULL)
err(1, "malloc");
for (cnt = 0; cnt < NCHARS; ++cnt)
if ((*cp->func)(cnt))
2010-04-08 17:04:57 +02:00
*p++ = cnt;
*p++ = OOBCH;
memset(p, 0, NCHARS + 1 - (p - s->set));
2010-04-08 17:04:57 +02:00
s->cnt = 0;
s->state = SET;
}
static int
c_class(const void *a, const void *b)
2010-04-08 17:04:57 +02:00
{
return strcmp(((const CLASS *)a)->name, ((const CLASS *)b)->name);
2010-04-08 17:04:57 +02:00
}
/*
* English doesn't have any equivalence classes, so for now
* we just syntax check and grab the character.
*/
static void
genequiv(STR *s)
2010-04-08 17:04:57 +02:00
{
if (*s->str == '\\') {
s->equiv[0] = backslash(s);
if (*s->str != '=')
errx(1, "misplaced equivalence equals sign");
2010-04-08 17:04:57 +02:00
} else {
s->equiv[0] = s->str[0];
if (s->str[1] != '=')
errx(1, "misplaced equivalence equals sign");
2010-04-08 17:04:57 +02:00
}
s->str += 2;
s->cnt = 0;
s->state = SET;
s->set = s->equiv;
}
static int
genrange(STR *s)
2010-04-08 17:04:57 +02:00
{
int stopval;
char *savestart;
2010-04-08 17:04:57 +02:00
savestart = s->str;
stopval = *++s->str == '\\' ? backslash(s) : *s->str++;
if (stopval < (u_char)s->lastch) {
s->str = savestart;
return 0;
2010-04-08 17:04:57 +02:00
}
s->cnt = stopval - s->lastch + 1;
s->state = RANGE;
--s->lastch;
return 1;
2010-04-08 17:04:57 +02:00
}
static void
genseq(STR *s)
2010-04-08 17:04:57 +02:00
{
char *ep;
if (s->which == STRING1)
errx(1, "sequences only valid in string2");
2010-04-08 17:04:57 +02:00
if (*s->str == '\\')
s->lastch = backslash(s);
else
s->lastch = *s->str++;
if (*s->str != '*')
errx(1, "misplaced sequence asterisk");
2010-04-08 17:04:57 +02:00
switch (*++s->str) {
case '\\':
s->cnt = backslash(s);
break;
case ']':
s->cnt = 0;
++s->str;
break;
default:
if (isdigit(*s->str)) {
s->cnt = strtol(s->str, &ep, 0);
2010-04-08 17:04:57 +02:00
if (*ep == ']') {
s->str = ep + 1;
2010-04-08 17:04:57 +02:00
break;
}
}
errx(1, "illegal sequence count");
2010-04-08 17:04:57 +02:00
/* NOTREACHED */
}
s->state = s->cnt ? SEQUENCE : INFINITE;
}
/*
* Translate \??? into a character. Up to 3 octal digits, if no digits either
* an escape code or a literal character.
*/
static int
backslash(STR *s)
2010-04-08 17:04:57 +02:00
{
int ch, cnt, val;
for (cnt = val = 0;;) {
ch = *++s->str;
if (!isascii(ch) || !isdigit(ch))
break;
val = val * 8 + ch - '0';
if (++cnt == 3) {
++s->str;
break;
}
}
if (cnt)
return val;
2010-04-08 17:04:57 +02:00
if (ch != '\0')
++s->str;
switch (ch) {
case 'a': /* escape characters */
return '\7';
case 'b':
return '\b';
case 'e':
return '\033';
case 'f':
return '\f';
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
case 'v':
return '\13';
case '\0': /* \" -> \ */
s->state = EOS;
return '\\';
default: /* \x" -> x */
return ch;
2010-04-08 17:04:57 +02:00
}
}