str.c revision 1590
11901Swollman/*-
274462Salfred * Copyright (c) 1991, 1993
350476Speter *	The Regents of the University of California.  All rights reserved.
448794Snik *
574462Salfred * Redistribution and use in source and binary forms, with or without
620779Smpp * modification, are permitted provided that the following conditions
720779Smpp * are met:
820779Smpp * 1. Redistributions of source code must retain the above copyright
956629Sshin *    notice, this list of conditions and the following disclaimer.
1056660Sbde * 2. Redistributions in binary form must reproduce the above copyright
1168947Sru *    notice, this list of conditions and the following disclaimer in the
1274462Salfred *    documentation and/or other materials provided with the distribution.
1374462Salfred * 3. All advertising materials mentioning features or use of this software
1420779Smpp *    must display the following acknowledgement:
1584306Sru *	This product includes software developed by the University of
1684306Sru *	California, Berkeley and its contributors.
1720779Smpp * 4. Neither the name of the University nor the names of its contributors
1856629Sshin *    may be used to endorse or promote products derived from this software
1956629Sshin *    without specific prior written permission.
2056629Sshin *
2120779Smpp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22108037Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2356660Sbde * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2456629Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2556660Sbde * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26108037Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2756629Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2821064Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291901Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301901Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3120779Smpp * SUCH DAMAGE.
3274462Salfred */
3374462Salfred
3474462Salfred#ifndef lint
3574462Salfredstatic char sccsid[] = "@(#)str.c	8.1 (Berkeley) 6/6/93";
3674462Salfred#endif /* not lint */
3774462Salfred
3874462Salfred#include <sys/cdefs.h>
3956629Sshin#include <sys/types.h>
4074462Salfred
4174462Salfred#include <errno.h>
4274462Salfred#include <stddef.h>
4374462Salfred#include <stdio.h>
4474462Salfred#include <stdlib.h>
4574462Salfred#include <string.h>
4674462Salfred
4774462Salfred#include "extern.h"
4874462Salfred
4974462Salfredstatic int	backslash __P((STR *));
5074462Salfredstatic int	bracket __P((STR *));
5156629Sshinstatic int	c_class __P((const void *, const void *));
5274462Salfredstatic void	genclass __P((STR *));
5374462Salfredstatic void	genequiv __P((STR *));
5474462Salfredstatic int	genrange __P((STR *));
5574462Salfredstatic void	genseq __P((STR *));
5674462Salfred
5774462Salfredint
5874462Salfrednext(s)
5956629Sshin	register STR *s;
6074462Salfred{
6156629Sshin	register int ch;
6274462Salfred
6374462Salfred	switch (s->state) {
6474462Salfred	case EOS:
6556629Sshin		return (0);
6656629Sshin	case INFINITE:
6756629Sshin		return (1);
6856629Sshin	case NORMAL:
6956629Sshin		switch (ch = *s->str) {
70108037Sru		case '\0':
7156629Sshin			s->state = EOS;
72108037Sru			return (0);
7356629Sshin		case '\\':
7456629Sshin			s->lastch = backslash(s);
7556629Sshin			break;
7656629Sshin		case '[':
7756629Sshin			if (bracket(s))
7856629Sshin				return (next(s));
7956629Sshin			/* FALLTHROUGH */
8056629Sshin		default:
8174462Salfred			++s->str;
8256629Sshin			s->lastch = ch;
8356629Sshin			break;
8456629Sshin		}
8574462Salfred
8674462Salfred		/* We can start a range at any time. */
8756629Sshin		if (s->str[0] == '-' && genrange(s))
8874462Salfred			return (next(s));
89108037Sru		return (1);
9074462Salfred	case RANGE:
91108037Sru		if (s->cnt-- == 0) {
9274462Salfred			s->state = NORMAL;
9374462Salfred			return (next(s));
9474462Salfred		}
9556629Sshin		++s->lastch;
9674462Salfred		return (1);
9779754Sdd	case SEQUENCE:
9874462Salfred		if (s->cnt-- == 0) {
9974462Salfred			s->state = NORMAL;
10074462Salfred			return (next(s));
10174462Salfred		}
10274462Salfred		return (1);
10374462Salfred	case SET:
104		if ((s->lastch = s->set[s->cnt++]) == OOBCH) {
105			s->state = NORMAL;
106			return (next(s));
107		}
108		return (1);
109	}
110	/* NOTREACHED */
111}
112
113static int
114bracket(s)
115	register STR *s;
116{
117	register char *p;
118
119	switch (s->str[1]) {
120	case ':':				/* "[:class:]" */
121		if ((p = strstr(s->str + 2, ":]")) == NULL)
122			return (0);
123		*p = '\0';
124		s->str += 2;
125		genclass(s);
126		s->str = p + 2;
127		return (1);
128	case '=':				/* "[=equiv=]" */
129		if ((p = strstr(s->str + 2, "=]")) == NULL)
130			return (0);
131		s->str += 2;
132		genequiv(s);
133		return (1);
134	default:				/* "[\###*n]" or "[#*n]" */
135		if ((p = strpbrk(s->str + 2, "*]")) == NULL)
136			return (0);
137		if (p[0] != '*' || index(p, ']') == NULL)
138			return (0);
139		s->str += 1;
140		genseq(s);
141		return (1);
142	}
143	/* NOTREACHED */
144}
145
146int isalnum __P((int)),
147    isalpha __P((int)),
148    isblank __P((int)),
149    isspace __P((int)),
150    iscntrl __P((int)),
151    isdigit __P((int)),
152    isgraph __P((int)),
153    islower __P((int)),
154    isprint __P((int)),
155    ispunct __P((int)),
156    isupper __P((int)),
157    isxdigit __P((int));
158
159typedef struct {
160	char *name;
161	int (*func) __P((int));
162	int *set;
163} CLASS;
164
165static CLASS classes[] = {
166	{ "alnum",  isalnum,  },
167	{ "alpha",  isalpha,  },
168	{ "blank",  isblank,  },
169	{ "cntrl",  iscntrl,  },
170	{ "digit",  isdigit,  },
171	{ "graph",  isgraph,  },
172	{ "lower",  islower,  },
173	{ "print",  isupper,  },
174	{ "punct",  ispunct,  },
175	{ "space",  isspace,  },
176	{ "upper",  isupper,  },
177	{ "xdigit", isxdigit, },
178};
179
180static void
181genclass(s)
182	STR *s;
183{
184	register int cnt, (*func) __P((int));
185	CLASS *cp, tmp;
186	int *p;
187
188	tmp.name = s->str;
189	if ((cp = (CLASS *)bsearch(&tmp, classes, sizeof(classes) /
190	    sizeof(CLASS), sizeof(CLASS), c_class)) == NULL)
191		err("unknown class %s", s->str);
192
193	if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL)
194		err("%s", strerror(errno));
195	bzero(p, (NCHARS + 1) * sizeof(int));
196	for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt)
197		if ((func)(cnt))
198			*p++ = cnt;
199	*p = OOBCH;
200
201	s->cnt = 0;
202	s->state = SET;
203	s->set = cp->set;
204}
205
206static int
207c_class(a, b)
208	const void *a, *b;
209{
210	return (strcmp(((CLASS *)a)->name, ((CLASS *)b)->name));
211}
212
213/*
214 * English doesn't have any equivalence classes, so for now
215 * we just syntax check and grab the character.
216 */
217static void
218genequiv(s)
219	STR *s;
220{
221	if (*s->str == '\\') {
222		s->equiv[0] = backslash(s);
223		if (*s->str != '=')
224			err("misplaced equivalence equals sign");
225	} else {
226		s->equiv[0] = s->str[0];
227		if (s->str[1] != '=')
228			err("misplaced equivalence equals sign");
229	}
230	s->str += 2;
231	s->cnt = 0;
232	s->state = SET;
233	s->set = s->equiv;
234}
235
236static int
237genrange(s)
238	STR *s;
239{
240	int stopval;
241	char *savestart;
242
243	savestart = s->str;
244	stopval = *++s->str == '\\' ? backslash(s) : *s->str;
245	if (stopval < s->lastch) {
246		s->str = savestart;
247		return (0);
248	}
249	s->cnt = stopval - s->lastch + 1;
250	s->state = RANGE;
251	--s->lastch;
252	return (1);
253}
254
255static void
256genseq(s)
257	STR *s;
258{
259	char *ep;
260
261	if (s->which == STRING1)
262		err("sequences only valid in string2");
263
264	if (*s->str == '\\')
265		s->lastch = backslash(s);
266	else
267		s->lastch = *s->str++;
268	if (*s->str != '*')
269		err("misplaced sequence asterisk");
270
271	switch (*++s->str) {
272	case '\\':
273		s->cnt = backslash(s);
274		break;
275	case ']':
276		s->cnt = 0;
277		++s->str;
278		break;
279	default:
280		if (isdigit(*s->str)) {
281			s->cnt = strtol(s->str, &ep, 0);
282			if (*ep == ']') {
283				s->str = ep + 1;
284				break;
285			}
286		}
287		err("illegal sequence count");
288		/* NOTREACHED */
289	}
290
291	s->state = s->cnt ? SEQUENCE : INFINITE;
292}
293
294/* Use the #defines isXXX() here, DON'T use them above. */
295#include <ctype.h>
296
297/*
298 * Translate \??? into a character.  Up to 3 octal digits, if no digits either
299 * an escape code or a literal character.
300 */
301static int
302backslash(s)
303	register STR *s;
304{
305	register int ch, cnt, val;
306
307	for (cnt = val = 0;;) {
308		ch = *++s->str;
309		if (!isascii(ch) || !isdigit(ch))
310			break;
311		val = val * 8 + ch - '0';
312		if (++cnt == 3) {
313			++s->str;
314			break;
315		}
316	}
317	if (cnt)
318		return (val);
319	if (ch != '\0')
320		++s->str;
321	switch (ch) {
322		case 'a':			/* escape characters */
323			return ('\7');
324		case 'b':
325			return ('\b');
326		case 'f':
327			return ('\f');
328		case 'n':
329			return ('\n');
330		case 'r':
331			return ('\r');
332		case 't':
333			return ('\t');
334		case 'v':
335			return ('\13');
336		case '\0':			/*  \" -> \ */
337			s->state = EOS;
338			return ('\\');
339		default:			/* \x" -> x */
340			return (ch);
341	}
342}
343