str.c revision 1.26 1 1.26 dholland /* $NetBSD: str.c,v 1.26 2013/08/11 01:29:28 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.26 dholland __RCSID("$NetBSD: str.c,v 1.26 2013/08/11 01:29:28 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.25 dholland 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.26 dholland static void genclass(STR *, const char *, size_t);
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.26 dholland ch = (unsigned char)s->str[0];
116 1.26 dholland switch (ch) {
117 1.1 glass case '\0':
118 1.1 glass s->state = EOS;
119 1.20 dholland *ret = s->lastch;
120 1.16 christos return 0;
121 1.1 glass case '\\':
122 1.1 glass s->lastch = backslash(s);
123 1.1 glass break;
124 1.1 glass case '[':
125 1.26 dholland if (bracket(s)) {
126 1.20 dholland return next(s, ret);
127 1.26 dholland }
128 1.1 glass /* FALLTHROUGH */
129 1.1 glass default:
130 1.1 glass ++s->str;
131 1.1 glass s->lastch = ch;
132 1.1 glass break;
133 1.1 glass }
134 1.1 glass
135 1.1 glass /* We can start a range at any time. */
136 1.20 dholland if (s->str[0] == '-' && genrange(s)) {
137 1.20 dholland return next(s, ret);
138 1.20 dholland }
139 1.20 dholland *ret = s->lastch;
140 1.16 christos return 1;
141 1.1 glass case RANGE:
142 1.26 dholland if (s->cnt == 0) {
143 1.1 glass s->state = NORMAL;
144 1.20 dholland return next(s, ret);
145 1.1 glass }
146 1.26 dholland s->cnt--;
147 1.1 glass ++s->lastch;
148 1.20 dholland *ret = s->lastch;
149 1.16 christos return 1;
150 1.1 glass case SEQUENCE:
151 1.26 dholland if (s->cnt == 0) {
152 1.1 glass s->state = NORMAL;
153 1.20 dholland return next(s, ret);
154 1.1 glass }
155 1.26 dholland s->cnt--;
156 1.20 dholland *ret = s->lastch;
157 1.16 christos return 1;
158 1.1 glass case SET:
159 1.26 dholland s->lastch = s->set[s->cnt++];
160 1.26 dholland if (s->lastch == OOBCH) {
161 1.1 glass s->state = NORMAL;
162 1.26 dholland if (s->set != s->equiv) {
163 1.26 dholland free(s->set);
164 1.26 dholland }
165 1.26 dholland s->set = NULL;
166 1.20 dholland return next(s, ret);
167 1.1 glass }
168 1.20 dholland *ret = s->lastch;
169 1.16 christos return 1;
170 1.1 glass }
171 1.1 glass /* NOTREACHED */
172 1.20 dholland assert(0);
173 1.20 dholland *ret = s->lastch;
174 1.16 christos return 0;
175 1.1 glass }
176 1.1 glass
177 1.1 glass static int
178 1.13 joerg bracket(STR *s)
179 1.1 glass {
180 1.26 dholland const char *p;
181 1.1 glass
182 1.1 glass switch (s->str[1]) {
183 1.1 glass case ':': /* "[:class:]" */
184 1.1 glass if ((p = strstr(s->str + 2, ":]")) == NULL)
185 1.16 christos return 0;
186 1.1 glass s->str += 2;
187 1.26 dholland genclass(s, s->str, p - s->str);
188 1.1 glass s->str = p + 2;
189 1.16 christos return 1;
190 1.1 glass case '=': /* "[=equiv=]" */
191 1.1 glass if ((p = strstr(s->str + 2, "=]")) == NULL)
192 1.16 christos return 0;
193 1.1 glass s->str += 2;
194 1.1 glass genequiv(s);
195 1.16 christos return 1;
196 1.1 glass default: /* "[\###*n]" or "[#*n]" */
197 1.1 glass if ((p = strpbrk(s->str + 2, "*]")) == NULL)
198 1.16 christos return 0;
199 1.8 lukem if (p[0] != '*' || strchr(p, ']') == NULL)
200 1.16 christos return 0;
201 1.1 glass s->str += 1;
202 1.1 glass genseq(s);
203 1.16 christos return 1;
204 1.1 glass }
205 1.1 glass /* NOTREACHED */
206 1.1 glass }
207 1.1 glass
208 1.1 glass typedef struct {
209 1.12 lukem const char *name;
210 1.13 joerg int (*func)(int);
211 1.1 glass } CLASS;
212 1.1 glass
213 1.16 christos static const CLASS classes[] = {
214 1.16 christos { "alnum", isalnum },
215 1.16 christos { "alpha", isalpha },
216 1.16 christos { "blank", isblank },
217 1.16 christos { "cntrl", iscntrl },
218 1.16 christos { "digit", isdigit },
219 1.16 christos { "graph", isgraph },
220 1.16 christos { "lower", islower },
221 1.16 christos { "print", isprint },
222 1.16 christos { "punct", ispunct },
223 1.16 christos { "space", isspace },
224 1.16 christos { "upper", isupper },
225 1.16 christos { "xdigit", isxdigit },
226 1.1 glass };
227 1.1 glass
228 1.26 dholland typedef struct {
229 1.26 dholland const char *name;
230 1.26 dholland size_t len;
231 1.26 dholland } CLASSKEY;
232 1.26 dholland
233 1.1 glass static void
234 1.26 dholland genclass(STR *s, const char *class, size_t len)
235 1.1 glass {
236 1.26 dholland int ch;
237 1.16 christos const CLASS *cp;
238 1.26 dholland CLASSKEY key;
239 1.1 glass int *p;
240 1.26 dholland unsigned pos, num;
241 1.1 glass
242 1.26 dholland /* Find the class */
243 1.26 dholland key.name = class;
244 1.26 dholland key.len = len;
245 1.26 dholland cp = bsearch(&key, classes, __arraycount(classes), sizeof(classes[0]),
246 1.26 dholland c_class);
247 1.26 dholland if (cp == NULL) {
248 1.26 dholland errx(1, "unknown class %.*s", (int)len, class);
249 1.26 dholland }
250 1.1 glass
251 1.26 dholland /*
252 1.26 dholland * Figure out what characters are in the class
253 1.26 dholland */
254 1.26 dholland
255 1.26 dholland num = NCHARS + 1;
256 1.26 dholland p = malloc(num * sizeof(*p));
257 1.26 dholland if (p == NULL) {
258 1.8 lukem err(1, "malloc");
259 1.26 dholland }
260 1.26 dholland
261 1.26 dholland pos = 0;
262 1.26 dholland for (ch = 0; ch < NCHARS; ch++) {
263 1.26 dholland if (cp->func(ch)) {
264 1.26 dholland p[pos++] = ch;
265 1.26 dholland }
266 1.26 dholland }
267 1.26 dholland
268 1.26 dholland p[pos++] = OOBCH;
269 1.26 dholland for (; pos < num; pos++) {
270 1.26 dholland p[pos] = 0;
271 1.26 dholland }
272 1.19 christos
273 1.26 dholland /* Update the state */
274 1.1 glass
275 1.26 dholland s->set = p;
276 1.1 glass s->cnt = 0;
277 1.1 glass s->state = SET;
278 1.1 glass }
279 1.1 glass
280 1.1 glass static int
281 1.26 dholland c_class(const void *av, const void *bv)
282 1.1 glass {
283 1.26 dholland const CLASSKEY *a = av;
284 1.26 dholland const CLASS *b = bv;
285 1.26 dholland size_t blen;
286 1.26 dholland int r;
287 1.26 dholland
288 1.26 dholland blen = strlen(b->name);
289 1.26 dholland r = strncmp(a->name, b->name, a->len);
290 1.26 dholland if (r != 0) {
291 1.26 dholland return r;
292 1.26 dholland }
293 1.26 dholland if (a->len < blen) {
294 1.26 dholland /* someone gave us a prefix of the right name */
295 1.26 dholland return -1;
296 1.26 dholland }
297 1.26 dholland assert(a-> len == blen);
298 1.26 dholland return 0;
299 1.1 glass }
300 1.1 glass
301 1.1 glass /*
302 1.1 glass * English doesn't have any equivalence classes, so for now
303 1.1 glass * we just syntax check and grab the character.
304 1.1 glass */
305 1.1 glass static void
306 1.13 joerg genequiv(STR *s)
307 1.1 glass {
308 1.1 glass if (*s->str == '\\') {
309 1.1 glass s->equiv[0] = backslash(s);
310 1.26 dholland if (*s->str != '=') {
311 1.26 dholland errx(1, "Misplaced equivalence equals sign");
312 1.26 dholland }
313 1.1 glass } else {
314 1.25 dholland s->equiv[0] = (unsigned char)s->str[0];
315 1.26 dholland if (s->str[1] != '=') {
316 1.26 dholland errx(1, "Misplaced equivalence equals sign");
317 1.26 dholland }
318 1.1 glass }
319 1.1 glass s->str += 2;
320 1.1 glass s->cnt = 0;
321 1.1 glass s->state = SET;
322 1.1 glass s->set = s->equiv;
323 1.1 glass }
324 1.1 glass
325 1.1 glass static int
326 1.13 joerg genrange(STR *s)
327 1.1 glass {
328 1.1 glass int stopval;
329 1.22 dholland const char *savestart;
330 1.1 glass
331 1.24 dholland savestart = s->str++;
332 1.26 dholland stopval = s->str[0] == '\\' ? backslash(s) : (unsigned char)*s->str++;
333 1.26 dholland if (stopval < (unsigned char)s->lastch) {
334 1.1 glass s->str = savestart;
335 1.16 christos return 0;
336 1.1 glass }
337 1.1 glass s->cnt = stopval - s->lastch + 1;
338 1.1 glass s->state = RANGE;
339 1.1 glass --s->lastch;
340 1.16 christos return 1;
341 1.1 glass }
342 1.1 glass
343 1.1 glass static void
344 1.13 joerg genseq(STR *s)
345 1.1 glass {
346 1.1 glass char *ep;
347 1.1 glass
348 1.26 dholland if (s->which == STRING1) {
349 1.26 dholland errx(1, "Sequences only valid in string2");
350 1.26 dholland }
351 1.1 glass
352 1.26 dholland if (*s->str == '\\') {
353 1.1 glass s->lastch = backslash(s);
354 1.26 dholland } else {
355 1.25 dholland s->lastch = (unsigned char)*s->str++;
356 1.26 dholland }
357 1.26 dholland if (*s->str != '*') {
358 1.26 dholland errx(1, "Misplaced sequence asterisk");
359 1.26 dholland }
360 1.1 glass
361 1.26 dholland s->str++;
362 1.26 dholland switch (s->str[0]) {
363 1.1 glass case '\\':
364 1.1 glass s->cnt = backslash(s);
365 1.1 glass break;
366 1.1 glass case ']':
367 1.1 glass s->cnt = 0;
368 1.1 glass ++s->str;
369 1.1 glass break;
370 1.1 glass default:
371 1.26 dholland if (isdigit((unsigned char)s->str[0])) {
372 1.1 glass s->cnt = strtol(s->str, &ep, 0);
373 1.1 glass if (*ep == ']') {
374 1.1 glass s->str = ep + 1;
375 1.1 glass break;
376 1.1 glass }
377 1.1 glass }
378 1.8 lukem errx(1, "illegal sequence count");
379 1.1 glass /* NOTREACHED */
380 1.1 glass }
381 1.1 glass
382 1.1 glass s->state = s->cnt ? SEQUENCE : INFINITE;
383 1.1 glass }
384 1.1 glass
385 1.1 glass /*
386 1.1 glass * Translate \??? into a character. Up to 3 octal digits, if no digits either
387 1.1 glass * an escape code or a literal character.
388 1.1 glass */
389 1.1 glass static int
390 1.13 joerg backslash(STR *s)
391 1.1 glass {
392 1.8 lukem int ch, cnt, val;
393 1.1 glass
394 1.1 glass for (cnt = val = 0;;) {
395 1.26 dholland s->str++;
396 1.26 dholland ch = (unsigned char)s->str[0];
397 1.26 dholland if (!isascii(ch) || !isdigit(ch)) {
398 1.1 glass break;
399 1.26 dholland }
400 1.1 glass val = val * 8 + ch - '0';
401 1.1 glass if (++cnt == 3) {
402 1.1 glass ++s->str;
403 1.1 glass break;
404 1.1 glass }
405 1.1 glass }
406 1.26 dholland if (cnt) {
407 1.16 christos return val;
408 1.26 dholland }
409 1.26 dholland if (ch != '\0') {
410 1.1 glass ++s->str;
411 1.26 dholland }
412 1.1 glass switch (ch) {
413 1.17 christos case 'a': /* escape characters */
414 1.17 christos return '\7';
415 1.17 christos case 'b':
416 1.17 christos return '\b';
417 1.17 christos case 'e':
418 1.17 christos return '\033';
419 1.17 christos case 'f':
420 1.17 christos return '\f';
421 1.17 christos case 'n':
422 1.17 christos return '\n';
423 1.17 christos case 'r':
424 1.17 christos return '\r';
425 1.17 christos case 't':
426 1.17 christos return '\t';
427 1.17 christos case 'v':
428 1.17 christos return '\13';
429 1.17 christos case '\0': /* \" -> \ */
430 1.17 christos s->state = EOS;
431 1.17 christos return '\\';
432 1.17 christos default: /* \x" -> x */
433 1.17 christos return ch;
434 1.1 glass }
435 1.1 glass }
436