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