/*-
* 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.
*/
struct str {
enum { STRING1, STRING2 } which;
enum { EOS, INFINITE, NORMAL, RANGE, SEQUENCE, SET } state;
int cnt; /* character count */
int lastch; /* last character */
int equiv[2]; /* equivalence set */
int *set; /* set of characters */
const char *str; /* user's string */
};
static int backslash(STR *);
static int bracket(STR *);
static int c_class(const void *, const void *);
static int *genclass(const char *, size_t);
static void genequiv(STR *);
static int genrange(STR *);
static void genseq(STR *);
static int
c_class(const void *av, const void *bv)
{
const CLASSKEY *a = av;
const CLASS *b = bv;
size_t blen;
int r;
blen = strlen(b->name);
r = strncmp(a->name, b->name, a->len);
if (r != 0) {
return r;
}
if (a->len < blen) {
/* someone gave us a prefix of the right name */
return -1;
}
assert(a-> len == blen);
return 0;
}
/*
* English doesn't have any equivalence classes, so for now
* we just syntax check and grab the character.
*/
static void
genequiv(STR *s)
{
int ch;
s->str++;
switch (s->str[0]) {
case '\\':
s->cnt = backslash(s);
break;
case ']':
s->cnt = 0;
++s->str;
break;
default:
if (isdigit((unsigned char)s->str[0])) {
s->cnt = strtol(s->str, &ep, 0);
if (*ep == ']') {
s->str = ep + 1;
break;
}
}
errx(1, "illegal sequence count");
/* 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)
{
int ch, cnt, val;
cnt = val = 0;
for (;;) {
/* Consume the character we're already on. */
s->str++;
/* Look at the next character. */
ch = (unsigned char)s->str[0];
if (!isascii(ch) || !isdigit(ch)) {
break;
}
val = val * 8 + ch - '0';
if (++cnt == 3) {
/* Enough digits; consume this one and stop */
++s->str;
break;
}
}
if (cnt) {
/* We saw digits, so return their value */
if (val >= OOBCH)
errx(1, "Invalid octal character value");
return val;
}
if (ch == '\0') {
/* \<end> -> \ */
s->state = EOS;
return '\\';
}
/* Consume the escaped character */
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';
default: /* \q -> q */
return ch;
}
}