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.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* 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/types.h>
|
|
|
|
|
1997-08-18 07:24:58 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
2004-07-09 02:08:07 +00:00
|
|
|
#include <errno.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2004-07-09 02:08:07 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include "extern.h"
|
|
|
|
|
2003-08-04 04:20:04 +00:00
|
|
|
static int backslash(STR *, int *);
|
2002-03-22 01:42:45 +00:00
|
|
|
static int bracket(STR *);
|
|
|
|
static void genclass(STR *);
|
|
|
|
static void genequiv(STR *);
|
2016-07-14 09:19:53 +00:00
|
|
|
static int genrange(STR *, int);
|
2002-03-22 01:42:45 +00:00
|
|
|
static void genseq(STR *);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
wint_t
|
2011-10-14 07:25:20 +00:00
|
|
|
next(STR *s)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-07-09 02:08:07 +00:00
|
|
|
int is_octal;
|
|
|
|
wint_t ch;
|
|
|
|
wchar_t wch;
|
|
|
|
size_t clen;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
switch (s->state) {
|
|
|
|
case EOS:
|
|
|
|
return (0);
|
|
|
|
case INFINITE:
|
|
|
|
return (1);
|
|
|
|
case NORMAL:
|
2004-07-09 02:08:07 +00:00
|
|
|
switch (*s->str) {
|
1994-05-27 12:33:43 +00:00
|
|
|
case '\0':
|
|
|
|
s->state = EOS;
|
|
|
|
return (0);
|
|
|
|
case '\\':
|
2003-08-04 04:20:04 +00:00
|
|
|
s->lastch = backslash(s, &is_octal);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
if (bracket(s))
|
|
|
|
return (next(s));
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
2004-07-09 02:08:07 +00:00
|
|
|
clen = mbrtowc(&wch, s->str, MB_LEN_MAX, NULL);
|
|
|
|
if (clen == (size_t)-1 || clen == (size_t)-2 ||
|
|
|
|
clen == 0)
|
|
|
|
errc(1, EILSEQ, NULL);
|
2003-08-04 04:20:04 +00:00
|
|
|
is_octal = 0;
|
2004-07-09 02:08:07 +00:00
|
|
|
s->lastch = wch;
|
|
|
|
s->str += clen;
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can start a range at any time. */
|
2016-07-14 09:19:53 +00:00
|
|
|
if (s->str[0] == '-' && genrange(s, is_octal))
|
1994-05-27 12:33:43 +00:00
|
|
|
return (next(s));
|
|
|
|
return (1);
|
2003-08-04 05:22:06 +00:00
|
|
|
case RANGE:
|
|
|
|
if (s->cnt-- == 0) {
|
|
|
|
s->state = NORMAL;
|
|
|
|
return (next(s));
|
|
|
|
}
|
|
|
|
++s->lastch;
|
|
|
|
return (1);
|
1994-05-27 12:33:43 +00:00
|
|
|
case SEQUENCE:
|
|
|
|
if (s->cnt-- == 0) {
|
|
|
|
s->state = NORMAL;
|
|
|
|
return (next(s));
|
|
|
|
}
|
|
|
|
return (1);
|
2004-07-09 02:08:07 +00:00
|
|
|
case CCLASS:
|
|
|
|
case CCLASS_UPPER:
|
|
|
|
case CCLASS_LOWER:
|
|
|
|
s->cnt++;
|
|
|
|
ch = nextwctype(s->lastch, s->cclass);
|
|
|
|
if (ch == -1) {
|
|
|
|
s->state = NORMAL;
|
|
|
|
return (next(s));
|
|
|
|
}
|
|
|
|
s->lastch = ch;
|
|
|
|
return (1);
|
1994-05-27 12:33:43 +00:00
|
|
|
case SET:
|
2003-08-03 22:02:49 +00:00
|
|
|
if ((ch = s->set[s->cnt++]) == OOBCH) {
|
1994-05-27 12:33:43 +00:00
|
|
|
s->state = NORMAL;
|
|
|
|
return (next(s));
|
|
|
|
}
|
2003-08-03 22:02:49 +00:00
|
|
|
s->lastch = ch;
|
1994-05-27 12:33:43 +00:00
|
|
|
return (1);
|
2001-12-11 23:36:25 +00:00
|
|
|
default:
|
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-10-14 07:25:20 +00:00
|
|
|
bracket(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:]" */
|
2002-06-15 07:38:27 +00:00
|
|
|
if ((p = strchr(s->str + 2, ']')) == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
return (0);
|
2002-06-15 07:38:27 +00:00
|
|
|
if (*(p - 1) != ':' || p - s->str < 4)
|
|
|
|
goto repeat;
|
|
|
|
*(p - 1) = '\0';
|
1994-05-27 12:33:43 +00:00
|
|
|
s->str += 2;
|
|
|
|
genclass(s);
|
2002-06-15 07:38:27 +00:00
|
|
|
s->str = p + 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
return (1);
|
|
|
|
case '=': /* "[=equiv=]" */
|
2010-09-29 22:24:18 +00:00
|
|
|
if (s->str[2] == '\0' || (p = strchr(s->str + 3, ']')) == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
return (0);
|
2002-06-15 07:38:27 +00:00
|
|
|
if (*(p - 1) != '=' || p - s->str < 4)
|
|
|
|
goto repeat;
|
1994-05-27 12:33:43 +00:00
|
|
|
s->str += 2;
|
|
|
|
genequiv(s);
|
|
|
|
return (1);
|
|
|
|
default: /* "[\###*n]" or "[#*n]" */
|
2002-06-15 07:38:27 +00:00
|
|
|
repeat:
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((p = strpbrk(s->str + 2, "*]")) == NULL)
|
|
|
|
return (0);
|
2012-01-03 18:51:58 +00:00
|
|
|
if (p[0] != '*' || strchr(p, ']') == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
return (0);
|
|
|
|
s->str += 1;
|
|
|
|
genseq(s);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-14 07:25:20 +00:00
|
|
|
genclass(STR *s)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
2004-07-09 02:08:07 +00:00
|
|
|
if ((s->cclass = wctype(s->str)) == 0)
|
1997-08-18 07:24:58 +00:00
|
|
|
errx(1, "unknown class %s", s->str);
|
1994-05-27 12:33:43 +00:00
|
|
|
s->cnt = 0;
|
2004-07-09 02:08:07 +00:00
|
|
|
s->lastch = -1; /* incremented before check in next() */
|
2003-08-03 02:23:39 +00:00
|
|
|
if (strcmp(s->str, "upper") == 0)
|
2004-07-09 02:08:07 +00:00
|
|
|
s->state = CCLASS_UPPER;
|
2003-08-05 07:59:46 +00:00
|
|
|
else if (strcmp(s->str, "lower") == 0)
|
2004-07-09 02:08:07 +00:00
|
|
|
s->state = CCLASS_LOWER;
|
2003-08-05 07:59:46 +00:00
|
|
|
else
|
2004-07-09 02:08:07 +00:00
|
|
|
s->state = CCLASS;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-14 07:25:20 +00:00
|
|
|
genequiv(STR *s)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-06-14 07:37:08 +00:00
|
|
|
int i, p, pri;
|
|
|
|
char src[2], dst[3];
|
2004-07-09 02:08:07 +00:00
|
|
|
size_t clen;
|
|
|
|
wchar_t wc;
|
2002-06-14 07:37:08 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if (*s->str == '\\') {
|
2003-08-04 04:20:04 +00:00
|
|
|
s->equiv[0] = backslash(s, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
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 {
|
2004-07-09 02:08:07 +00:00
|
|
|
clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
|
|
|
|
if (clen == (size_t)-1 || clen == (size_t)-2 || clen == 0)
|
|
|
|
errc(1, EILSEQ, NULL);
|
|
|
|
s->equiv[0] = wc;
|
|
|
|
if (s->str[clen] != '=')
|
1997-08-18 07:24:58 +00:00
|
|
|
errx(1, "misplaced equivalence equals sign");
|
2004-07-09 02:08:07 +00:00
|
|
|
s->str += clen + 2;
|
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.
|
2004-07-09 02:08:07 +00:00
|
|
|
* XXX Equivalence classes not supported in multibyte locales.
|
2002-06-14 07:37:08 +00:00
|
|
|
*/
|
2004-07-09 02:08:07 +00:00
|
|
|
src[0] = (char)s->equiv[0];
|
2002-06-14 07:37:08 +00:00
|
|
|
src[1] = '\0';
|
2004-07-09 02:08:07 +00:00
|
|
|
if (MB_CUR_MAX == 1 && strxfrm(dst, src, sizeof(dst)) == 1) {
|
2002-06-14 07:37:08 +00:00
|
|
|
pri = (unsigned char)*dst;
|
2004-07-09 02:08:07 +00:00
|
|
|
for (p = 1, i = 1; i < NCHARS_SB; i++) {
|
2002-06-14 07:37:08 +00:00
|
|
|
*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
|
2016-07-14 09:19:53 +00:00
|
|
|
genrange(STR *s, int was_octal)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2016-07-14 09:19:53 +00:00
|
|
|
int stopval, octal;
|
1994-05-27 12:33:43 +00:00
|
|
|
char *savestart;
|
2016-07-14 09:19:53 +00:00
|
|
|
int n, cnt, *p;
|
2004-07-09 02:08:07 +00:00
|
|
|
size_t clen;
|
|
|
|
wchar_t wc;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2016-07-14 09:19:53 +00:00
|
|
|
octal = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
savestart = s->str;
|
2004-07-09 02:08:07 +00:00
|
|
|
if (*++s->str == '\\')
|
2016-07-14 09:19:53 +00:00
|
|
|
stopval = backslash(s, &octal);
|
2004-07-09 02:08:07 +00:00
|
|
|
else {
|
|
|
|
clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
|
|
|
|
if (clen == (size_t)-1 || clen == (size_t)-2)
|
|
|
|
errc(1, EILSEQ, NULL);
|
|
|
|
stopval = wc;
|
|
|
|
s->str += clen;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2016-07-14 09:19:53 +00:00
|
|
|
/*
|
|
|
|
* XXX Characters are not ordered according to collating sequence in
|
|
|
|
* multibyte locales.
|
|
|
|
*/
|
|
|
|
if (octal || was_octal || MB_CUR_MAX > 1) {
|
|
|
|
if (stopval < s->lastch) {
|
|
|
|
s->str = savestart;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
s->cnt = stopval - s->lastch + 1;
|
|
|
|
s->state = RANGE;
|
|
|
|
--s->lastch;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (charcoll((const void *)&stopval, (const void *)&(s->lastch)) < 0) {
|
2004-07-09 02:08:07 +00:00
|
|
|
s->str = savestart;
|
|
|
|
return (0);
|
|
|
|
}
|
2016-07-14 09:19:53 +00:00
|
|
|
if ((s->set = p = malloc((NCHARS_SB + 1) * sizeof(int))) == NULL)
|
|
|
|
err(1, "genrange() malloc");
|
|
|
|
for (cnt = 0; cnt < NCHARS_SB; cnt++)
|
|
|
|
if (charcoll((const void *)&cnt, (const void *)&(s->lastch)) >= 0 &&
|
|
|
|
charcoll((const void *)&cnt, (const void *)&stopval) <= 0)
|
|
|
|
*p++ = cnt;
|
|
|
|
*p = OOBCH;
|
|
|
|
n = p - s->set;
|
|
|
|
|
|
|
|
s->cnt = 0;
|
|
|
|
s->state = SET;
|
|
|
|
if (n > 1)
|
|
|
|
mergesort(s->set, n, sizeof(*(s->set)), charcoll);
|
1994-05-27 12:33:43 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-14 07:25:20 +00:00
|
|
|
genseq(STR *s)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
char *ep;
|
2004-07-09 02:08:07 +00:00
|
|
|
wchar_t wc;
|
|
|
|
size_t clen;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
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 == '\\')
|
2003-08-04 04:20:04 +00:00
|
|
|
s->lastch = backslash(s, NULL);
|
2004-07-09 02:08:07 +00:00
|
|
|
else {
|
|
|
|
clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
|
|
|
|
if (clen == (size_t)-1 || clen == (size_t)-2)
|
|
|
|
errc(1, EILSEQ, NULL);
|
|
|
|
s->lastch = wc;
|
|
|
|
s->str += clen;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
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 '\\':
|
2003-08-04 04:20:04 +00:00
|
|
|
s->cnt = backslash(s, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
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
|
2003-08-04 04:20:04 +00:00
|
|
|
backslash(STR *s, int *is_octal)
|
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
|
|
|
|
2003-08-04 04:20:04 +00:00
|
|
|
if (is_octal != NULL)
|
|
|
|
*is_octal = 0;
|
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;
|
2004-11-14 05:15:25 +00:00
|
|
|
if (!isdigit(ch) || ch > '7')
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
val = val * 8 + ch - '0';
|
|
|
|
if (++cnt == 3) {
|
|
|
|
++s->str;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-08-04 04:20:04 +00:00
|
|
|
if (cnt) {
|
|
|
|
if (is_octal != NULL)
|
|
|
|
*is_octal = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
return (val);
|
2003-08-04 04:20:04 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|