2013-08-11 05:54:35 +04:00
|
|
|
/* $NetBSD: str.c,v 1.29 2013/08/11 01:54:35 dholland Exp $ */
|
1994-12-07 11:35:02 +03:00
|
|
|
|
1993-04-27 08:52:26 +04:00
|
|
|
/*-
|
1994-12-07 11:35:02 +03:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-27 08:52:26 +04:00
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-04-27 08:52:26 +04: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.
|
|
|
|
*/
|
|
|
|
|
1997-10-20 04:56:04 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-04-27 08:52:26 +04:00
|
|
|
#ifndef lint
|
1994-12-07 11:35:02 +03:00
|
|
|
#if 0
|
1995-09-01 02:11:37 +04:00
|
|
|
static char sccsid[] = "@(#)str.c 8.2 (Berkeley) 4/28/95";
|
1994-12-07 11:35:02 +03:00
|
|
|
#endif
|
2013-08-11 05:54:35 +04:00
|
|
|
__RCSID("$NetBSD: str.c,v 1.29 2013/08/11 01:54:35 dholland Exp $");
|
1993-04-27 08:52:26 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
1997-10-20 04:56:04 +04:00
|
|
|
#include <err.h>
|
1993-04-27 08:52:26 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1993-09-14 05:17:46 +04:00
|
|
|
#include <ctype.h>
|
2013-08-11 03:54:41 +04:00
|
|
|
#include <assert.h>
|
1993-04-27 08:52:26 +04:00
|
|
|
|
|
|
|
#include "extern.h"
|
|
|
|
|
2013-08-11 04:04:14 +04:00
|
|
|
struct str {
|
|
|
|
enum { STRING1, STRING2 } which;
|
|
|
|
enum { EOS, INFINITE, NORMAL, RANGE, SEQUENCE, SET } state;
|
2013-08-11 05:54:35 +04:00
|
|
|
int cnt; /* character count */
|
|
|
|
int lastch; /* last character */
|
|
|
|
int equiv[2]; /* equivalence set */
|
|
|
|
int *set; /* set of characters */
|
2013-08-11 05:00:13 +04:00
|
|
|
const char *str; /* user's string */
|
2013-08-11 04:04:14 +04:00
|
|
|
};
|
|
|
|
|
2013-08-11 05:54:35 +04:00
|
|
|
static int backslash(STR *);
|
|
|
|
static int bracket(STR *);
|
|
|
|
static int c_class(const void *, const void *);
|
2013-08-11 05:42:35 +04:00
|
|
|
static int *genclass(const char *, size_t);
|
2013-08-11 05:54:35 +04:00
|
|
|
static void genequiv(STR *);
|
|
|
|
static int genrange(STR *);
|
|
|
|
static void genseq(STR *);
|
1993-04-27 08:52:26 +04:00
|
|
|
|
2013-08-11 04:04:14 +04:00
|
|
|
STR *
|
2013-08-11 04:39:22 +04:00
|
|
|
str_create(int whichstring, const char *txt)
|
2013-08-11 04:04:14 +04:00
|
|
|
{
|
|
|
|
STR *s;
|
|
|
|
|
|
|
|
s = malloc(sizeof(*s));
|
|
|
|
if (s == NULL) {
|
|
|
|
err(1, "Out of memory");
|
|
|
|
}
|
|
|
|
|
|
|
|
s->which = whichstring == 2 ? STRING2 : STRING1;
|
|
|
|
s->state = NORMAL;
|
|
|
|
s->cnt = 0;
|
|
|
|
s->lastch = OOBCH;
|
|
|
|
s->equiv[0] = 0;
|
|
|
|
s->equiv[1] = OOBCH;
|
|
|
|
s->set = NULL;
|
2013-08-11 04:39:22 +04:00
|
|
|
s->str = txt;
|
2013-08-11 04:04:14 +04:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
str_destroy(STR *s)
|
|
|
|
{
|
|
|
|
if (s->set != NULL && s->set != s->equiv) {
|
|
|
|
free(s->set);
|
|
|
|
}
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
1993-04-27 08:52:26 +04:00
|
|
|
int
|
2013-08-11 03:54:41 +04:00
|
|
|
next(STR *s, int *ret)
|
1993-04-27 08:52:26 +04:00
|
|
|
{
|
1997-10-20 04:56:04 +04:00
|
|
|
int ch;
|
1993-04-27 08:52:26 +04:00
|
|
|
|
|
|
|
switch (s->state) {
|
|
|
|
case EOS:
|
2013-08-11 03:54:41 +04:00
|
|
|
*ret = s->lastch;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 0;
|
1993-04-27 08:52:26 +04:00
|
|
|
case INFINITE:
|
2013-08-11 03:54:41 +04:00
|
|
|
*ret = s->lastch;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 1;
|
1993-04-27 08:52:26 +04:00
|
|
|
case NORMAL:
|
2013-08-11 05:29:28 +04:00
|
|
|
ch = (unsigned char)s->str[0];
|
|
|
|
switch (ch) {
|
1993-04-27 08:52:26 +04:00
|
|
|
case '\0':
|
|
|
|
s->state = EOS;
|
2013-08-11 03:54:41 +04:00
|
|
|
*ret = s->lastch;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 0;
|
1993-04-27 08:52:26 +04:00
|
|
|
case '\\':
|
|
|
|
s->lastch = backslash(s);
|
|
|
|
break;
|
|
|
|
case '[':
|
2013-08-11 05:29:28 +04:00
|
|
|
if (bracket(s)) {
|
2013-08-11 03:54:41 +04:00
|
|
|
return next(s, ret);
|
2013-08-11 05:29:28 +04:00
|
|
|
}
|
1993-04-27 08:52:26 +04:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
++s->str;
|
|
|
|
s->lastch = ch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can start a range at any time. */
|
2013-08-11 03:54:41 +04:00
|
|
|
if (s->str[0] == '-' && genrange(s)) {
|
|
|
|
return next(s, ret);
|
|
|
|
}
|
|
|
|
*ret = s->lastch;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 1;
|
1993-04-27 08:52:26 +04:00
|
|
|
case RANGE:
|
2013-08-11 05:29:28 +04:00
|
|
|
if (s->cnt == 0) {
|
1993-04-27 08:52:26 +04:00
|
|
|
s->state = NORMAL;
|
2013-08-11 03:54:41 +04:00
|
|
|
return next(s, ret);
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
2013-08-11 05:29:28 +04:00
|
|
|
s->cnt--;
|
1993-04-27 08:52:26 +04:00
|
|
|
++s->lastch;
|
2013-08-11 03:54:41 +04:00
|
|
|
*ret = s->lastch;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 1;
|
1993-04-27 08:52:26 +04:00
|
|
|
case SEQUENCE:
|
2013-08-11 05:29:28 +04:00
|
|
|
if (s->cnt == 0) {
|
1993-04-27 08:52:26 +04:00
|
|
|
s->state = NORMAL;
|
2013-08-11 03:54:41 +04:00
|
|
|
return next(s, ret);
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
2013-08-11 05:29:28 +04:00
|
|
|
s->cnt--;
|
2013-08-11 03:54:41 +04:00
|
|
|
*ret = s->lastch;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 1;
|
1993-04-27 08:52:26 +04:00
|
|
|
case SET:
|
2013-08-11 05:29:28 +04:00
|
|
|
s->lastch = s->set[s->cnt++];
|
|
|
|
if (s->lastch == OOBCH) {
|
1993-04-27 08:52:26 +04:00
|
|
|
s->state = NORMAL;
|
2013-08-11 05:29:28 +04:00
|
|
|
if (s->set != s->equiv) {
|
|
|
|
free(s->set);
|
|
|
|
}
|
|
|
|
s->set = NULL;
|
2013-08-11 03:54:41 +04:00
|
|
|
return next(s, ret);
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
2013-08-11 03:54:41 +04:00
|
|
|
*ret = s->lastch;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 1;
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
2013-08-11 03:54:41 +04:00
|
|
|
assert(0);
|
|
|
|
*ret = s->lastch;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 0;
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-09-06 22:33:46 +04:00
|
|
|
bracket(STR *s)
|
1993-04-27 08:52:26 +04:00
|
|
|
{
|
2013-08-11 05:29:28 +04:00
|
|
|
const char *p;
|
2013-08-11 05:42:35 +04:00
|
|
|
int *q;
|
1993-04-27 08:52:26 +04:00
|
|
|
|
|
|
|
switch (s->str[1]) {
|
|
|
|
case ':': /* "[:class:]" */
|
|
|
|
if ((p = strstr(s->str + 2, ":]")) == NULL)
|
2011-09-08 05:18:05 +04:00
|
|
|
return 0;
|
1993-04-27 08:52:26 +04:00
|
|
|
s->str += 2;
|
2013-08-11 05:42:35 +04:00
|
|
|
q = genclass(s->str, p - s->str);
|
|
|
|
s->state = SET;
|
|
|
|
s->set = q;
|
|
|
|
s->cnt = 0;
|
1993-04-27 08:52:26 +04:00
|
|
|
s->str = p + 2;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 1;
|
1993-04-27 08:52:26 +04:00
|
|
|
case '=': /* "[=equiv=]" */
|
|
|
|
if ((p = strstr(s->str + 2, "=]")) == NULL)
|
2011-09-08 05:18:05 +04:00
|
|
|
return 0;
|
1993-04-27 08:52:26 +04:00
|
|
|
s->str += 2;
|
|
|
|
genequiv(s);
|
2013-08-11 05:49:40 +04:00
|
|
|
s->str = p + 2;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 1;
|
1993-04-27 08:52:26 +04:00
|
|
|
default: /* "[\###*n]" or "[#*n]" */
|
|
|
|
if ((p = strpbrk(s->str + 2, "*]")) == NULL)
|
2011-09-08 05:18:05 +04:00
|
|
|
return 0;
|
1997-10-20 04:56:04 +04:00
|
|
|
if (p[0] != '*' || strchr(p, ']') == NULL)
|
2011-09-08 05:18:05 +04:00
|
|
|
return 0;
|
1993-04-27 08:52:26 +04:00
|
|
|
s->str += 1;
|
|
|
|
genseq(s);
|
2011-09-08 05:18:05 +04:00
|
|
|
return 1;
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
2009-04-14 03:50:49 +04:00
|
|
|
const char *name;
|
2011-09-06 22:33:46 +04:00
|
|
|
int (*func)(int);
|
1993-04-27 08:52:26 +04:00
|
|
|
} CLASS;
|
|
|
|
|
2011-09-08 05:18:05 +04:00
|
|
|
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 },
|
1993-04-27 08:52:26 +04:00
|
|
|
};
|
|
|
|
|
2013-08-11 05:29:28 +04:00
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
|
|
|
size_t len;
|
|
|
|
} CLASSKEY;
|
|
|
|
|
2013-08-11 05:42:35 +04:00
|
|
|
static int *
|
|
|
|
genclass(const char *class, size_t len)
|
1993-04-27 08:52:26 +04:00
|
|
|
{
|
2013-08-11 05:29:28 +04:00
|
|
|
int ch;
|
2011-09-08 05:18:05 +04:00
|
|
|
const CLASS *cp;
|
2013-08-11 05:29:28 +04:00
|
|
|
CLASSKEY key;
|
1993-04-27 08:52:26 +04:00
|
|
|
int *p;
|
2013-08-11 05:29:28 +04:00
|
|
|
unsigned pos, num;
|
|
|
|
|
|
|
|
/* Find the class */
|
|
|
|
key.name = class;
|
|
|
|
key.len = len;
|
|
|
|
cp = bsearch(&key, classes, __arraycount(classes), sizeof(classes[0]),
|
|
|
|
c_class);
|
|
|
|
if (cp == NULL) {
|
|
|
|
errx(1, "unknown class %.*s", (int)len, class);
|
|
|
|
}
|
1993-04-27 08:52:26 +04:00
|
|
|
|
2013-08-11 05:29:28 +04:00
|
|
|
/*
|
|
|
|
* Figure out what characters are in the class
|
|
|
|
*/
|
1993-04-27 08:52:26 +04:00
|
|
|
|
2013-08-11 05:29:28 +04:00
|
|
|
num = NCHARS + 1;
|
|
|
|
p = malloc(num * sizeof(*p));
|
|
|
|
if (p == NULL) {
|
1997-10-20 04:56:04 +04:00
|
|
|
err(1, "malloc");
|
2013-08-11 05:29:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
for (ch = 0; ch < NCHARS; ch++) {
|
|
|
|
if (cp->func(ch)) {
|
|
|
|
p[pos++] = ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p[pos++] = OOBCH;
|
|
|
|
for (; pos < num; pos++) {
|
|
|
|
p[pos] = 0;
|
|
|
|
}
|
2011-09-08 16:00:26 +04:00
|
|
|
|
2013-08-11 05:42:35 +04:00
|
|
|
return p;
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-08-11 05:29:28 +04:00
|
|
|
c_class(const void *av, const void *bv)
|
1993-04-27 08:52:26 +04:00
|
|
|
{
|
2013-08-11 05:29:28 +04:00
|
|
|
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;
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* English doesn't have any equivalence classes, so for now
|
|
|
|
* we just syntax check and grab the character.
|
|
|
|
*/
|
|
|
|
static void
|
2011-09-06 22:33:46 +04:00
|
|
|
genequiv(STR *s)
|
1993-04-27 08:52:26 +04:00
|
|
|
{
|
2013-08-11 05:42:35 +04:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
ch = (unsigned char)s->str[0];
|
|
|
|
if (ch == '\\') {
|
1993-04-27 08:52:26 +04:00
|
|
|
s->equiv[0] = backslash(s);
|
|
|
|
} else {
|
2013-08-11 05:42:35 +04:00
|
|
|
s->equiv[0] = ch;
|
2013-08-11 05:49:40 +04:00
|
|
|
s->str++;
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
2013-08-11 05:49:40 +04:00
|
|
|
if (s->str[0] != '=') {
|
|
|
|
errx(1, "Misplaced equivalence equals sign");
|
|
|
|
}
|
|
|
|
s->str++;
|
|
|
|
if (s->str[0] != ']') {
|
|
|
|
errx(1, "Misplaced equivalence right bracket");
|
|
|
|
}
|
|
|
|
s->str++;
|
|
|
|
|
1993-04-27 08:52:26 +04:00
|
|
|
s->cnt = 0;
|
|
|
|
s->state = SET;
|
|
|
|
s->set = s->equiv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-09-06 22:33:46 +04:00
|
|
|
genrange(STR *s)
|
1993-04-27 08:52:26 +04:00
|
|
|
{
|
|
|
|
int stopval;
|
2013-08-11 04:05:49 +04:00
|
|
|
const char *savestart;
|
1993-04-27 08:52:26 +04:00
|
|
|
|
2013-08-11 04:52:17 +04:00
|
|
|
savestart = s->str++;
|
2013-08-11 05:29:28 +04:00
|
|
|
stopval = s->str[0] == '\\' ? backslash(s) : (unsigned char)*s->str++;
|
|
|
|
if (stopval < (unsigned char)s->lastch) {
|
1993-04-27 08:52:26 +04:00
|
|
|
s->str = savestart;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 0;
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
|
|
|
s->cnt = stopval - s->lastch + 1;
|
|
|
|
s->state = RANGE;
|
|
|
|
--s->lastch;
|
2011-09-08 05:18:05 +04:00
|
|
|
return 1;
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-09-06 22:33:46 +04:00
|
|
|
genseq(STR *s)
|
1993-04-27 08:52:26 +04:00
|
|
|
{
|
|
|
|
char *ep;
|
|
|
|
|
2013-08-11 05:29:28 +04:00
|
|
|
if (s->which == STRING1) {
|
|
|
|
errx(1, "Sequences only valid in string2");
|
|
|
|
}
|
1993-04-27 08:52:26 +04:00
|
|
|
|
2013-08-11 05:29:28 +04:00
|
|
|
if (*s->str == '\\') {
|
1993-04-27 08:52:26 +04:00
|
|
|
s->lastch = backslash(s);
|
2013-08-11 05:29:28 +04:00
|
|
|
} else {
|
2013-08-11 05:00:13 +04:00
|
|
|
s->lastch = (unsigned char)*s->str++;
|
2013-08-11 05:29:28 +04:00
|
|
|
}
|
|
|
|
if (*s->str != '*') {
|
|
|
|
errx(1, "Misplaced sequence asterisk");
|
|
|
|
}
|
1993-04-27 08:52:26 +04:00
|
|
|
|
2013-08-11 05:29:28 +04:00
|
|
|
s->str++;
|
|
|
|
switch (s->str[0]) {
|
1993-04-27 08:52:26 +04:00
|
|
|
case '\\':
|
|
|
|
s->cnt = backslash(s);
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
s->cnt = 0;
|
|
|
|
++s->str;
|
|
|
|
break;
|
|
|
|
default:
|
2013-08-11 05:29:28 +04:00
|
|
|
if (isdigit((unsigned char)s->str[0])) {
|
1993-04-27 08:52:26 +04:00
|
|
|
s->cnt = strtol(s->str, &ep, 0);
|
|
|
|
if (*ep == ']') {
|
|
|
|
s->str = ep + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1997-10-20 04:56:04 +04:00
|
|
|
errx(1, "illegal sequence count");
|
1993-04-27 08:52:26 +04: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
|
2011-09-06 22:33:46 +04:00
|
|
|
backslash(STR *s)
|
1993-04-27 08:52:26 +04:00
|
|
|
{
|
1997-10-20 04:56:04 +04:00
|
|
|
int ch, cnt, val;
|
1993-04-27 08:52:26 +04:00
|
|
|
|
2013-08-11 05:42:35 +04:00
|
|
|
cnt = val = 0;
|
|
|
|
for (;;) {
|
|
|
|
/* Consume the character we're already on. */
|
2013-08-11 05:29:28 +04:00
|
|
|
s->str++;
|
2013-08-11 05:42:35 +04:00
|
|
|
|
|
|
|
/* Look at the next character. */
|
2013-08-11 05:29:28 +04:00
|
|
|
ch = (unsigned char)s->str[0];
|
|
|
|
if (!isascii(ch) || !isdigit(ch)) {
|
1993-04-27 08:52:26 +04:00
|
|
|
break;
|
2013-08-11 05:29:28 +04:00
|
|
|
}
|
1993-04-27 08:52:26 +04:00
|
|
|
val = val * 8 + ch - '0';
|
|
|
|
if (++cnt == 3) {
|
2013-08-11 05:42:35 +04:00
|
|
|
/* Enough digits; consume this one and stop */
|
1993-04-27 08:52:26 +04:00
|
|
|
++s->str;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-08-11 05:29:28 +04:00
|
|
|
if (cnt) {
|
2013-08-11 05:42:35 +04:00
|
|
|
/* We saw digits, so return their value */
|
2011-09-08 05:18:05 +04:00
|
|
|
return val;
|
2013-08-11 05:29:28 +04:00
|
|
|
}
|
2013-08-11 05:42:35 +04:00
|
|
|
if (ch == '\0') {
|
|
|
|
/* \<end> -> \ */
|
|
|
|
s->state = EOS;
|
|
|
|
return '\\';
|
2013-08-11 05:29:28 +04:00
|
|
|
}
|
2013-08-11 05:42:35 +04:00
|
|
|
|
|
|
|
/* Consume the escaped character */
|
|
|
|
s->str++;
|
|
|
|
|
1993-04-27 08:52:26 +04:00
|
|
|
switch (ch) {
|
2011-09-08 05:18:44 +04:00
|
|
|
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';
|
2013-08-11 05:42:35 +04:00
|
|
|
default: /* \q -> q */
|
2011-09-08 05:18:44 +04:00
|
|
|
return ch;
|
1993-04-27 08:52:26 +04:00
|
|
|
}
|
|
|
|
}
|