1994-05-27 12:33:43 +00: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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. 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.
|
|
|
|
*/
|
|
|
|
|
2001-12-11 23:36:25 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
2001-12-11 23:36:25 +00:00
|
|
|
static const char sccsid[] = "@(#)str.c 8.2 (Berkeley) 4/28/95";
|
1997-08-18 07:24:58 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
1997-08-18 07:24:58 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "extern.h"
|
|
|
|
|
2002-03-22 01:42:45 +00: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 *);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
next(s)
|
2001-12-11 23:36:25 +00:00
|
|
|
STR *s;
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-11 23:36:25 +00:00
|
|
|
int ch;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
switch (s->state) {
|
|
|
|
case EOS:
|
|
|
|
return (0);
|
|
|
|
case INFINITE:
|
|
|
|
return (1);
|
|
|
|
case NORMAL:
|
1996-03-17 09:00:48 +00:00
|
|
|
switch (ch = (u_char)*s->str) {
|
1994-05-27 12:33:43 +00:00
|
|
|
case '\0':
|
|
|
|
s->state = EOS;
|
|
|
|
return (0);
|
|
|
|
case '\\':
|
|
|
|
s->lastch = backslash(s);
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
if (bracket(s))
|
|
|
|
return (next(s));
|
|
|
|
/* 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);
|
|
|
|
case RANGE:
|
|
|
|
if (s->cnt-- == 0) {
|
|
|
|
s->state = NORMAL;
|
|
|
|
return (next(s));
|
|
|
|
}
|
|
|
|
++s->lastch;
|
|
|
|
return (1);
|
|
|
|
case SEQUENCE:
|
|
|
|
if (s->cnt-- == 0) {
|
|
|
|
s->state = NORMAL;
|
|
|
|
return (next(s));
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
case SET:
|
|
|
|
if ((s->lastch = s->set[s->cnt++]) == OOBCH) {
|
|
|
|
s->state = NORMAL;
|
|
|
|
return (next(s));
|
|
|
|
}
|
|
|
|
return (1);
|
2001-12-11 23:36:25 +00:00
|
|
|
default:
|
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
bracket(s)
|
2001-12-11 23:36:25 +00:00
|
|
|
STR *s;
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-11 23:36:25 +00:00
|
|
|
char *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
switch (s->str[1]) {
|
|
|
|
case ':': /* "[:class:]" */
|
|
|
|
if ((p = strstr(s->str + 2, ":]")) == NULL)
|
|
|
|
return (0);
|
|
|
|
*p = '\0';
|
|
|
|
s->str += 2;
|
|
|
|
genclass(s);
|
|
|
|
s->str = p + 2;
|
|
|
|
return (1);
|
|
|
|
case '=': /* "[=equiv=]" */
|
|
|
|
if ((p = strstr(s->str + 2, "=]")) == NULL)
|
|
|
|
return (0);
|
|
|
|
s->str += 2;
|
|
|
|
genequiv(s);
|
|
|
|
return (1);
|
|
|
|
default: /* "[\###*n]" or "[#*n]" */
|
|
|
|
if ((p = strpbrk(s->str + 2, "*]")) == NULL)
|
|
|
|
return (0);
|
|
|
|
if (p[0] != '*' || index(p, ']') == NULL)
|
|
|
|
return (0);
|
|
|
|
s->str += 1;
|
|
|
|
genseq(s);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
2001-12-11 23:36:25 +00:00
|
|
|
const char *name;
|
2002-03-22 01:42:45 +00:00
|
|
|
int (*func)(int);
|
1994-05-27 12:33:43 +00:00
|
|
|
int *set;
|
|
|
|
} CLASS;
|
|
|
|
|
|
|
|
static CLASS classes[] = {
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef isalnum
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "alnum", isalnum, NULL },
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef isalpha
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "alpha", isalpha, NULL },
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef isblank
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "blank", isblank, NULL },
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef iscntrl
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "cntrl", iscntrl, NULL },
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef isdigit
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "digit", isdigit, NULL },
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef isgraph
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "graph", isgraph, NULL },
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef islower
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "lower", islower, NULL },
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef isprint
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "print", isprint, NULL },
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef ispunct
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "punct", ispunct, NULL },
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef isspace
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "space", isspace, NULL },
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef isupper
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "upper", isupper, NULL },
|
1995-10-28 22:27:03 +00:00
|
|
|
#undef isxdigit
|
2001-12-11 23:36:25 +00:00
|
|
|
{ "xdigit", isxdigit, NULL },
|
1994-05-27 12:33:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
genclass(s)
|
|
|
|
STR *s;
|
|
|
|
{
|
2002-03-22 01:42:45 +00:00
|
|
|
int cnt, (*func)(int);
|
1994-05-27 12:33:43 +00:00
|
|
|
CLASS *cp, tmp;
|
|
|
|
int *p;
|
|
|
|
|
|
|
|
tmp.name = s->str;
|
|
|
|
if ((cp = (CLASS *)bsearch(&tmp, classes, sizeof(classes) /
|
|
|
|
sizeof(CLASS), sizeof(CLASS), c_class)) == NULL)
|
1997-08-18 07:24:58 +00:00
|
|
|
errx(1, "unknown class %s", s->str);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL)
|
1997-08-18 07:24:58 +00:00
|
|
|
errx(1, "malloc");
|
1994-05-27 12:33:43 +00:00
|
|
|
bzero(p, (NCHARS + 1) * sizeof(int));
|
|
|
|
for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt)
|
|
|
|
if ((func)(cnt))
|
|
|
|
*p++ = cnt;
|
|
|
|
*p = OOBCH;
|
|
|
|
|
|
|
|
s->cnt = 0;
|
|
|
|
s->state = SET;
|
|
|
|
s->set = cp->set;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
c_class(a, b)
|
|
|
|
const void *a, *b;
|
|
|
|
{
|
2001-12-11 23:36:25 +00:00
|
|
|
return (strcmp(((const CLASS *)a)->name, ((const CLASS *)b)->name));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
genequiv(s)
|
|
|
|
STR *s;
|
|
|
|
{
|
2002-06-14 07:37:08 +00:00
|
|
|
int i, p, pri;
|
|
|
|
char src[2], dst[3];
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if (*s->str == '\\') {
|
|
|
|
s->equiv[0] = backslash(s);
|
|
|
|
if (*s->str != '=')
|
1997-08-18 07:24:58 +00:00
|
|
|
errx(1, "misplaced equivalence equals sign");
|
2002-06-14 09:53:11 +00:00
|
|
|
s->str += 2;
|
1994-05-27 12:33:43 +00:00
|
|
|
} else {
|
|
|
|
s->equiv[0] = s->str[0];
|
|
|
|
if (s->str[1] != '=')
|
1997-08-18 07:24:58 +00:00
|
|
|
errx(1, "misplaced equivalence equals sign");
|
2002-06-14 09:53:11 +00:00
|
|
|
s->str += 3;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-06-14 07:37:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the set of all characters in the same equivalence class
|
|
|
|
* as the specified character (they will have the same primary
|
|
|
|
* collation weights).
|
|
|
|
* XXX Knows too much about how strxfrm() is implemented. Assumes
|
|
|
|
* it fills the string with primary collation weight bytes. Only one-
|
|
|
|
* to-one mappings are supported.
|
|
|
|
*/
|
|
|
|
src[0] = s->equiv[0];
|
|
|
|
src[1] = '\0';
|
|
|
|
if (strxfrm(dst, src, sizeof(dst)) == 1) {
|
|
|
|
pri = (unsigned char)*dst;
|
|
|
|
for (p = 1, i = 1; i < NCHARS; i++) {
|
|
|
|
*src = i;
|
|
|
|
if (strxfrm(dst, src, sizeof(dst)) == 1 && pri &&
|
|
|
|
pri == (unsigned char)*dst)
|
|
|
|
s->equiv[p++] = i;
|
|
|
|
}
|
|
|
|
s->equiv[p] = OOBCH;
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
s->cnt = 0;
|
|
|
|
s->state = SET;
|
|
|
|
s->set = s->equiv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
genrange(s)
|
|
|
|
STR *s;
|
|
|
|
{
|
|
|
|
int stopval;
|
|
|
|
char *savestart;
|
|
|
|
|
|
|
|
savestart = s->str;
|
1996-03-17 09:00:48 +00:00
|
|
|
stopval = *++s->str == '\\' ? backslash(s) : (u_char)*s->str++;
|
1995-11-28 13:18:47 +00:00
|
|
|
if (stopval < (u_char)s->lastch) {
|
1994-05-27 12:33:43 +00:00
|
|
|
s->str = savestart;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
s->cnt = stopval - s->lastch + 1;
|
|
|
|
s->state = RANGE;
|
|
|
|
--s->lastch;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
genseq(s)
|
|
|
|
STR *s;
|
|
|
|
{
|
|
|
|
char *ep;
|
|
|
|
|
|
|
|
if (s->which == STRING1)
|
1997-08-18 07:24:58 +00:00
|
|
|
errx(1, "sequences only valid in string2");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (*s->str == '\\')
|
|
|
|
s->lastch = backslash(s);
|
|
|
|
else
|
|
|
|
s->lastch = *s->str++;
|
|
|
|
if (*s->str != '*')
|
1997-08-18 07:24:58 +00:00
|
|
|
errx(1, "misplaced sequence asterisk");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
switch (*++s->str) {
|
|
|
|
case '\\':
|
|
|
|
s->cnt = backslash(s);
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
s->cnt = 0;
|
|
|
|
++s->str;
|
|
|
|
break;
|
|
|
|
default:
|
1996-03-19 21:21:06 +00:00
|
|
|
if (isdigit((u_char)*s->str)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
s->cnt = strtol(s->str, &ep, 0);
|
|
|
|
if (*ep == ']') {
|
|
|
|
s->str = ep + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1997-08-18 07:24:58 +00:00
|
|
|
errx(1, "illegal sequence count");
|
1994-05-27 12:33:43 +00: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(s)
|
2001-12-11 23:36:25 +00:00
|
|
|
STR *s;
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-11 23:36:25 +00:00
|
|
|
int ch, cnt, val;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
for (cnt = val = 0;;) {
|
1996-03-19 21:21:06 +00:00
|
|
|
ch = (u_char)*++s->str;
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!isascii(ch) || !isdigit(ch))
|
|
|
|
break;
|
|
|
|
val = val * 8 + ch - '0';
|
|
|
|
if (++cnt == 3) {
|
|
|
|
++s->str;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cnt)
|
|
|
|
return (val);
|
|
|
|
if (ch != '\0')
|
|
|
|
++s->str;
|
|
|
|
switch (ch) {
|
|
|
|
case 'a': /* escape characters */
|
|
|
|
return ('\7');
|
|
|
|
case 'b':
|
|
|
|
return ('\b');
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|