str.c revision 11630
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)str.c	8.1 (Berkeley) 6/6/93";
36#endif /* not lint */
37
38#include <sys/cdefs.h>
39#include <sys/types.h>
40
41#include <errno.h>
42#include <stddef.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <ctype.h>
47
48#include "extern.h"
49
50static int	backslash __P((STR *));
51static int	bracket __P((STR *));
52static int	c_class __P((const void *, const void *));
53static void	genclass __P((STR *));
54static void	genequiv __P((STR *));
55static int	genrange __P((STR *));
56static void	genseq __P((STR *));
57
58int
59next(s)
60	register STR *s;
61{
62	register int ch;
63
64	switch (s->state) {
65	case EOS:
66		return (0);
67	case INFINITE:
68		return (1);
69	case NORMAL:
70		switch (ch = *s->str) {
71		case '\0':
72			s->state = EOS;
73			return (0);
74		case '\\':
75			s->lastch = backslash(s);
76			break;
77		case '[':
78			if (bracket(s))
79				return (next(s));
80			/* FALLTHROUGH */
81		default:
82			++s->str;
83			s->lastch = ch;
84			break;
85		}
86
87		/* We can start a range at any time. */
88		if (s->str[0] == '-' && genrange(s))
89			return (next(s));
90		return (1);
91	case RANGE:
92		if (s->cnt-- == 0) {
93			s->state = NORMAL;
94			return (next(s));
95		}
96		++s->lastch;
97		return (1);
98	case SEQUENCE:
99		if (s->cnt-- == 0) {
100			s->state = NORMAL;
101			return (next(s));
102		}
103		return (1);
104	case SET:
105		if ((s->lastch = s->set[s->cnt++]) == OOBCH) {
106			s->state = NORMAL;
107			return (next(s));
108		}
109		return (1);
110	}
111	/* NOTREACHED */
112}
113
114static int
115bracket(s)
116	register STR *s;
117{
118	register char *p;
119
120	switch (s->str[1]) {
121	case ':':				/* "[:class:]" */
122		if ((p = strstr(s->str + 2, ":]")) == NULL)
123			return (0);
124		*p = '\0';
125		s->str += 2;
126		genclass(s);
127		s->str = p + 2;
128		return (1);
129	case '=':				/* "[=equiv=]" */
130		if ((p = strstr(s->str + 2, "=]")) == NULL)
131			return (0);
132		s->str += 2;
133		genequiv(s);
134		return (1);
135	default:				/* "[\###*n]" or "[#*n]" */
136		if ((p = strpbrk(s->str + 2, "*]")) == NULL)
137			return (0);
138		if (p[0] != '*' || index(p, ']') == NULL)
139			return (0);
140		s->str += 1;
141		genseq(s);
142		return (1);
143	}
144	/* NOTREACHED */
145}
146
147typedef struct {
148	char *name;
149	int (*func) __P((int));
150	int *set;
151} CLASS;
152
153static CLASS classes[] = {
154	{ "alnum",  isalnum,  },
155	{ "alpha",  isalpha,  },
156	{ "blank",  isblank,  },
157	{ "cntrl",  iscntrl,  },
158	{ "digit",  isdigit,  },
159	{ "graph",  isgraph,  },
160	{ "lower",  islower,  },
161	{ "print",  isprint,  },
162	{ "punct",  ispunct,  },
163	{ "space",  isspace,  },
164	{ "upper",  isupper,  },
165	{ "xdigit", isxdigit, },
166};
167
168static void
169genclass(s)
170	STR *s;
171{
172	register int cnt, (*func) __P((int));
173	CLASS *cp, tmp;
174	int *p;
175
176	tmp.name = s->str;
177	if ((cp = (CLASS *)bsearch(&tmp, classes, sizeof(classes) /
178	    sizeof(CLASS), sizeof(CLASS), c_class)) == NULL)
179		err("unknown class %s", s->str);
180
181	if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL)
182		err("%s", strerror(errno));
183	bzero(p, (NCHARS + 1) * sizeof(int));
184	for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt)
185		if ((func)(cnt))
186			*p++ = cnt;
187	*p = OOBCH;
188
189	s->cnt = 0;
190	s->state = SET;
191	s->set = cp->set;
192}
193
194static int
195c_class(a, b)
196	const void *a, *b;
197{
198	return (strcmp(((CLASS *)a)->name, ((CLASS *)b)->name));
199}
200
201/*
202 * English doesn't have any equivalence classes, so for now
203 * we just syntax check and grab the character.
204 */
205static void
206genequiv(s)
207	STR *s;
208{
209	if (*s->str == '\\') {
210		s->equiv[0] = backslash(s);
211		if (*s->str != '=')
212			err("misplaced equivalence equals sign");
213	} else {
214		s->equiv[0] = s->str[0];
215		if (s->str[1] != '=')
216			err("misplaced equivalence equals sign");
217	}
218	s->str += 2;
219	s->cnt = 0;
220	s->state = SET;
221	s->set = s->equiv;
222}
223
224static int
225genrange(s)
226	STR *s;
227{
228	int stopval;
229	char *savestart;
230
231	savestart = s->str;
232	stopval = *++s->str == '\\' ? backslash(s) : *s->str;
233	if (stopval < s->lastch) {
234		s->str = savestart;
235		return (0);
236	}
237	s->cnt = stopval - s->lastch + 1;
238	s->state = RANGE;
239	--s->lastch;
240	return (1);
241}
242
243static void
244genseq(s)
245	STR *s;
246{
247	char *ep;
248
249	if (s->which == STRING1)
250		err("sequences only valid in string2");
251
252	if (*s->str == '\\')
253		s->lastch = backslash(s);
254	else
255		s->lastch = *s->str++;
256	if (*s->str != '*')
257		err("misplaced sequence asterisk");
258
259	switch (*++s->str) {
260	case '\\':
261		s->cnt = backslash(s);
262		break;
263	case ']':
264		s->cnt = 0;
265		++s->str;
266		break;
267	default:
268		if (isdigit(*s->str)) {
269			s->cnt = strtol(s->str, &ep, 0);
270			if (*ep == ']') {
271				s->str = ep + 1;
272				break;
273			}
274		}
275		err("illegal sequence count");
276		/* NOTREACHED */
277	}
278
279	s->state = s->cnt ? SEQUENCE : INFINITE;
280}
281
282/* Use the #defines isXXX() here, DON'T use them above. */
283#include <ctype.h>
284
285/*
286 * Translate \??? into a character.  Up to 3 octal digits, if no digits either
287 * an escape code or a literal character.
288 */
289static int
290backslash(s)
291	register STR *s;
292{
293	register int ch, cnt, val;
294
295	for (cnt = val = 0;;) {
296		ch = *++s->str;
297		if (!isascii(ch) || !isdigit(ch))
298			break;
299		val = val * 8 + ch - '0';
300		if (++cnt == 3) {
301			++s->str;
302			break;
303		}
304	}
305	if (cnt)
306		return (val);
307	if (ch != '\0')
308		++s->str;
309	switch (ch) {
310		case 'a':			/* escape characters */
311			return ('\7');
312		case 'b':
313			return ('\b');
314		case 'f':
315			return ('\f');
316		case 'n':
317			return ('\n');
318		case 'r':
319			return ('\r');
320		case 't':
321			return ('\t');
322		case 'v':
323			return ('\13');
324		case '\0':			/*  \" -> \ */
325			s->state = EOS;
326			return ('\\');
327		default:			/* \x" -> x */
328			return (ch);
329	}
330}
331