str.c revision 84cf253f8f6b41850d9fd9d5c853b30ce633bb40
/*
* 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
* 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.
*/
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <wchar.h>
#include <wctype.h>
#include "extern.h"
next(s)
STR *s;
{
int is_octal;
switch (s->state) {
case EOS:
return (0);
case INFINITE:
return (1);
case NORMAL:
switch (*s->str) {
case '\0':
return (0);
case '\\':
break;
case '[':
if (bracket(s))
return (next(s));
/* FALLTHROUGH */
default:
clen == 0) {
exit(1);
}
is_octal = 0;
break;
}
/* We can start a range at any time. */
return (next(s));
return (1);
case RANGE:
if (s->cnt-- == 0) {
return (next(s));
}
++s->lastch;
return (1);
case SEQUENCE:
if (s->cnt-- == 0) {
return (next(s));
}
return (1);
case CCLASS:
case CCLASS_UPPER:
case CCLASS_LOWER:
s->cnt++;
if (ch == -1) {
return (next(s));
}
return (1);
case SET:
return (next(s));
}
return (1);
default:
return (0);
}
/* NOTREACHED */
}
static int
bracket(s)
STR *s;
{
char *p;
switch (s->str[1]) {
case ':': /* "[:class:]" */
return (0);
goto repeat;
*(p - 1) = '\0';
s->str += 2;
genclass(s);
s->str = p + 1;
return (1);
case '=': /* "[=equiv=]" */
return (0);
goto repeat;
s->str += 2;
genequiv(s);
return (1);
default: /* "[\###*n]" or "[#*n]" */
return (0);
return (0);
s->str += 1;
genseq(s);
return (1);
}
/* NOTREACHED */
}
static void
genclass(s)
STR *s;
{
s->cnt = 0;
s->state = CCLASS_UPPER;
s->state = CCLASS_LOWER;
else
}
static void
genequiv(s)
STR *s;
{
int i, p, pri;
if (*s->str == '\\') {
if (*s->str != '=')
s->str += 2;
} else {
}
}
/*
* 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.
* XXX Equivalence classes not supported in multibyte locales.
*/
*src = i;
s->equiv[p++] = i;
}
}
s->cnt = 0;
}
static int
{
char *savestart;
int n, cnt, *p;
octal = 0;
if (*++s->str == '\\')
else {
}
}
/*
* XXX Characters are not ordered according to collating sequence in
* multibyte locales.
*/
return (0);
}
--s->lastch;
return (1);
}
return (0);
}
0 &&
*p++ = cnt;
*p = OOBCH;
n = (int *)p - (int *)s->set;
s->cnt = 0;
if (n > 1)
return (1);
}
static void
genseq(s)
STR *s;
{
char *ep;
if (*s->str == '\\')
else {
}
}
if (*s->str != '*')
switch (*++s->str) {
case '\\':
break;
case ']':
s->cnt = 0;
++s->str;
break;
default:
if (*ep == ']') {
break;
}
}
/* NOTREACHED */
}
}
/*
* Translate \??? into a character. Up to 3 octal digits, if no digits either
* an escape code or a literal character.
*/
static int
{
*is_octal = 0;
break;
if (++cnt == 3) {
++s->str;
break;
}
}
if (cnt) {
*is_octal = 1;
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': /* \" -> \ */
return ('\\');
default: /* \x" -> x */
return (ch);
}
}