blob: 4a0d99af9a1832d3c553948db65b25f572a90908 [file] [log] [blame]
Elliott Hughes4fc93ff2019-02-13 14:16:47 -08001/* $NetBSD: regcomp.c,v 1.38 2019/02/07 22:22:31 christos Exp $ */
Elliott Hughescc213f82012-08-14 15:32:42 -07002
Colin Cross4fa7b102010-01-12 18:59:25 -08003/*-
Colin Cross4fa7b102010-01-12 18:59:25 -08004 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Henry Spencer.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
35 */
36
Elliott Hughescc213f82012-08-14 15:32:42 -070037/*-
38 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Henry Spencer.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
72 */
73
74#include <sys/cdefs.h>
75#if defined(LIBC_SCCS) && !defined(lint)
76#if 0
77static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
78#else
Elliott Hughes4fc93ff2019-02-13 14:16:47 -080079__RCSID("$NetBSD: regcomp.c,v 1.38 2019/02/07 22:22:31 christos Exp $");
Elliott Hughescc213f82012-08-14 15:32:42 -070080#endif
81#endif /* LIBC_SCCS and not lint */
82
83#include "namespace.h"
Colin Cross4fa7b102010-01-12 18:59:25 -080084#include <sys/types.h>
Elliott Hughescc213f82012-08-14 15:32:42 -070085
86#include <assert.h>
Colin Cross4fa7b102010-01-12 18:59:25 -080087#include <ctype.h>
88#include <limits.h>
Elliott Hughescc213f82012-08-14 15:32:42 -070089#include <stdio.h>
Colin Cross4fa7b102010-01-12 18:59:25 -080090#include <stdlib.h>
Elliott Hughescc213f82012-08-14 15:32:42 -070091#include <string.h>
Colin Cross4fa7b102010-01-12 18:59:25 -080092#include <regex.h>
93
Elliott Hughescc213f82012-08-14 15:32:42 -070094#ifdef __weak_alias
95__weak_alias(regcomp,_regcomp)
96#endif
97
Colin Cross4fa7b102010-01-12 18:59:25 -080098#include "utils.h"
99#include "regex2.h"
100
101#include "cclass.h"
102#include "cname.h"
103
104/*
105 * parse structure, passed up and down to avoid global variables and
106 * other clumsinesses
107 */
108struct parse {
Elliott Hughescc213f82012-08-14 15:32:42 -0700109 const char *next; /* next character in RE */
110 const char *end; /* end of string (-> NUL normally) */
Colin Cross4fa7b102010-01-12 18:59:25 -0800111 int error; /* has an error been seen? */
112 sop *strip; /* malloced strip */
113 sopno ssize; /* malloced strip size (allocated) */
114 sopno slen; /* malloced strip length (used) */
Elliott Hughescc213f82012-08-14 15:32:42 -0700115 size_t ncsalloc; /* number of csets allocated */
Colin Cross4fa7b102010-01-12 18:59:25 -0800116 struct re_guts *g;
117# define NPAREN 10 /* we need to remember () 1-9 for back refs */
118 sopno pbegin[NPAREN]; /* -> ( ([0] unused) */
119 sopno pend[NPAREN]; /* -> ) ([0] unused) */
120};
121
Elliott Hughescc213f82012-08-14 15:32:42 -0700122/* ========= begin header generated by ./mkh ========= */
123#ifdef __cplusplus
124extern "C" {
125#endif
126
127/* === regcomp.c === */
128static void p_ere(struct parse *p, int stop, size_t reclimit);
129static void p_ere_exp(struct parse *p, size_t reclimit);
130static void p_str(struct parse *p);
131static void p_bre(struct parse *p, int end1, int end2, size_t reclimit);
132static int p_simp_re(struct parse *p, int starordinary, size_t reclimit);
133static int p_count(struct parse *p);
134static void p_bracket(struct parse *p);
135static void p_b_term(struct parse *p, cset *cs);
136static void p_b_cclass(struct parse *p, cset *cs);
137static void p_b_eclass(struct parse *p, cset *cs);
138static char p_b_symbol(struct parse *p);
139static char p_b_coll_elem(struct parse *p, int endc);
140static int othercase(int ch);
141static void bothcases(struct parse *p, int ch);
142static void ordinary(struct parse *p, int ch);
143static void nonnewline(struct parse *p);
144static void repeat(struct parse *p, sopno start, int from, int to, size_t reclimit);
145static int seterr(struct parse *p, int e);
146static cset *allocset(struct parse *p);
147static void freeset(struct parse *p, cset *cs);
148static sopno freezeset(struct parse *p, cset *cs);
149static int firstch(struct parse *p, cset *cs);
150static int nch(struct parse *p, cset *cs);
151static void mcadd(struct parse *p, cset *cs, const char *cp);
152#if 0
153static void mcsub(cset *cs, char *cp);
154static int mcin(cset *cs, char *cp);
155static char *mcfind(cset *cs, char *cp);
156#endif
157static void mcinvert(struct parse *p, cset *cs);
158static void mccase(struct parse *p, cset *cs);
159static int isinsets(struct re_guts *g, int c);
160static int samesets(struct re_guts *g, int c1, int c2);
161static void categorize(struct parse *p, struct re_guts *g);
162static sopno dupl(struct parse *p, sopno start, sopno finish);
163static void doemit(struct parse *p, sop op, sopno opnd);
164static void doinsert(struct parse *p, sop op, sopno opnd, sopno pos);
165static void dofwd(struct parse *p, sopno pos, sopno value);
166static int enlarge(struct parse *p, sopno size);
167static void stripsnug(struct parse *p, struct re_guts *g);
168static void findmust(struct parse *p, struct re_guts *g);
169static sopno pluscount(struct parse *p, struct re_guts *g);
170
171#ifdef __cplusplus
172}
173#endif
174/* ========= end header generated by ./mkh ========= */
Colin Cross4fa7b102010-01-12 18:59:25 -0800175
176static char nuls[10]; /* place to point scanner in event of error */
177
178/*
179 * macros for use with parse structure
180 * BEWARE: these know that the parse structure is named `p' !!!
181 */
182#define PEEK() (*p->next)
183#define PEEK2() (*(p->next+1))
184#define MORE() (p->next < p->end)
185#define MORE2() (p->next+1 < p->end)
186#define SEE(c) (MORE() && PEEK() == (c))
187#define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
188#define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
189#define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
190#define NEXT() (p->next++)
191#define NEXT2() (p->next += 2)
192#define NEXTn(n) (p->next += (n))
193#define GETNEXT() (*p->next++)
194#define SETERROR(e) seterr(p, (e))
Elliott Hughescc213f82012-08-14 15:32:42 -0700195#define REQUIRE(co, e) (void) ((co) || SETERROR(e))
Colin Cross4fa7b102010-01-12 18:59:25 -0800196#define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
Elliott Hughescc213f82012-08-14 15:32:42 -0700197#define MUSTEAT(c, e) (void) (REQUIRE(MORE() && GETNEXT() == (c), e))
Colin Cross4fa7b102010-01-12 18:59:25 -0800198#define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
Elliott Hughescc213f82012-08-14 15:32:42 -0700199#define EMIT(op, sopnd) doemit(p, (sop)(op), sopnd)
Colin Cross4fa7b102010-01-12 18:59:25 -0800200#define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
201#define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
202#define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
203#define HERE() (p->slen)
204#define THERE() (p->slen - 1)
205#define THERETHERE() (p->slen - 2)
206#define DROP(n) (p->slen -= (n))
207
208#ifndef NDEBUG
209static int never = 0; /* for use in asserts; shuts lint up */
210#else
211#define never 0 /* some <assert.h>s have bugs too */
212#endif
213
Elliott Hughescc213f82012-08-14 15:32:42 -0700214#define MEMLIMIT 0x8000000
215#define MEMSIZE(p) \
216 ((p)->ncsalloc / CHAR_BIT * (p)->g->csetsize + \
217 (p)->ncsalloc * sizeof(cset) + \
218 (p)->ssize * sizeof(sop))
219#define RECLIMIT 256
220
Colin Cross4fa7b102010-01-12 18:59:25 -0800221/*
222 - regcomp - interface for parser and compilation
Elliott Hughescc213f82012-08-14 15:32:42 -0700223 = extern int regcomp(regex_t *, const char *, int);
224 = #define REG_BASIC 0000
225 = #define REG_EXTENDED 0001
226 = #define REG_ICASE 0002
227 = #define REG_NOSUB 0004
228 = #define REG_NEWLINE 0010
229 = #define REG_NOSPEC 0020
230 = #define REG_PEND 0040
231 = #define REG_DUMP 0200
Colin Cross4fa7b102010-01-12 18:59:25 -0800232 */
233int /* 0 success, otherwise REG_something */
Elliott Hughescc213f82012-08-14 15:32:42 -0700234regcomp(
235 regex_t *preg,
236 const char *pattern,
237 int cflags)
Colin Cross4fa7b102010-01-12 18:59:25 -0800238{
239 struct parse pa;
240 struct re_guts *g;
241 struct parse *p = &pa;
242 int i;
243 size_t len;
244#ifdef REDEBUG
245# define GOODFLAGS(f) (f)
246#else
247# define GOODFLAGS(f) ((f)&~REG_DUMP)
248#endif
249
Elliott Hughescc213f82012-08-14 15:32:42 -0700250 _DIAGASSERT(preg != NULL);
251 _DIAGASSERT(pattern != NULL);
252
Colin Cross4fa7b102010-01-12 18:59:25 -0800253 cflags = GOODFLAGS(cflags);
254 if ((cflags&REG_EXTENDED) && (cflags&REG_NOSPEC))
255 return(REG_INVARG);
256
257 if (cflags&REG_PEND) {
258 if (preg->re_endp < pattern)
259 return(REG_INVARG);
260 len = preg->re_endp - pattern;
261 } else
Elliott Hughescc213f82012-08-14 15:32:42 -0700262 len = strlen(pattern);
Colin Cross4fa7b102010-01-12 18:59:25 -0800263
264 /* do the mallocs early so failure handling is easy */
Elliott Hughes71927a82015-09-14 18:45:52 -0700265 g = malloc(sizeof(struct re_guts) + (NC - 1) * sizeof(cat_t));
Colin Cross4fa7b102010-01-12 18:59:25 -0800266 if (g == NULL)
267 return(REG_ESPACE);
268 p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
Elliott Hughes71927a82015-09-14 18:45:52 -0700269 p->strip = calloc(p->ssize, sizeof(sop));
Colin Cross4fa7b102010-01-12 18:59:25 -0800270 p->slen = 0;
271 if (p->strip == NULL) {
Elliott Hughescc213f82012-08-14 15:32:42 -0700272 free(g);
Colin Cross4fa7b102010-01-12 18:59:25 -0800273 return(REG_ESPACE);
274 }
275
276 /* set things up */
277 p->g = g;
Elliott Hughescc213f82012-08-14 15:32:42 -0700278 p->next = pattern;
Colin Cross4fa7b102010-01-12 18:59:25 -0800279 p->end = p->next + len;
280 p->error = 0;
281 p->ncsalloc = 0;
282 for (i = 0; i < NPAREN; i++) {
283 p->pbegin[i] = 0;
284 p->pend[i] = 0;
285 }
286 g->csetsize = NC;
287 g->sets = NULL;
288 g->setbits = NULL;
289 g->ncsets = 0;
290 g->cflags = cflags;
291 g->iflags = 0;
292 g->nbol = 0;
293 g->neol = 0;
294 g->must = NULL;
295 g->mlen = 0;
296 g->nsub = 0;
297 g->ncategories = 1; /* category 0 is "everything else" */
298 g->categories = &g->catspace[-(CHAR_MIN)];
299 (void) memset((char *)g->catspace, 0, NC*sizeof(cat_t));
300 g->backrefs = 0;
301
302 /* do it */
303 EMIT(OEND, 0);
304 g->firststate = THERE();
305 if (cflags&REG_EXTENDED)
Elliott Hughescc213f82012-08-14 15:32:42 -0700306 p_ere(p, OUT, 0);
Colin Cross4fa7b102010-01-12 18:59:25 -0800307 else if (cflags&REG_NOSPEC)
308 p_str(p);
309 else
Elliott Hughescc213f82012-08-14 15:32:42 -0700310 p_bre(p, OUT, OUT, 0);
Colin Cross4fa7b102010-01-12 18:59:25 -0800311 EMIT(OEND, 0);
312 g->laststate = THERE();
313
314 /* tidy up loose ends and fill things in */
315 categorize(p, g);
316 stripsnug(p, g);
317 findmust(p, g);
318 g->nplus = pluscount(p, g);
319 g->magic = MAGIC2;
320 preg->re_nsub = g->nsub;
321 preg->re_g = g;
322 preg->re_magic = MAGIC1;
323#ifndef REDEBUG
324 /* not debugging, so can't rely on the assert() in regexec() */
325 if (g->iflags&BAD)
326 SETERROR(REG_ASSERT);
327#endif
328
329 /* win or lose, we're done */
330 if (p->error != 0) /* lose */
331 regfree(preg);
332 return(p->error);
333}
334
335/*
336 - p_ere - ERE parser top level, concatenation and alternation
Elliott Hughescc213f82012-08-14 15:32:42 -0700337 == static void p_ere(struct parse *p, int stop, size_t reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -0800338 */
339static void
Elliott Hughescc213f82012-08-14 15:32:42 -0700340p_ere(
341 struct parse *p,
342 int stop, /* character this ERE should end at */
343 size_t reclimit)
Colin Cross4fa7b102010-01-12 18:59:25 -0800344{
345 char c;
Elliott Hughescc213f82012-08-14 15:32:42 -0700346 sopno prevback = 0; /* pacify gcc */
347 sopno prevfwd = 0; /* pacify gcc */
Colin Cross4fa7b102010-01-12 18:59:25 -0800348 sopno conc;
349 int first = 1; /* is this the first alternative? */
350
Elliott Hughescc213f82012-08-14 15:32:42 -0700351 _DIAGASSERT(p != NULL);
352
353 if (reclimit++ > RECLIMIT || p->error == REG_ESPACE) {
354 p->error = REG_ESPACE;
355 return;
356 }
357
Colin Cross4fa7b102010-01-12 18:59:25 -0800358 for (;;) {
359 /* do a bunch of concatenated expressions */
360 conc = HERE();
361 while (MORE() && (c = PEEK()) != '|' && c != stop)
Elliott Hughescc213f82012-08-14 15:32:42 -0700362 p_ere_exp(p, reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -0800363 REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
364
365 if (!EAT('|'))
366 break; /* NOTE BREAK OUT */
367
368 if (first) {
369 INSERT(OCH_, conc); /* offset is wrong */
370 prevfwd = conc;
371 prevback = conc;
372 first = 0;
373 }
374 ASTERN(OOR1, prevback);
375 prevback = THERE();
376 AHEAD(prevfwd); /* fix previous offset */
377 prevfwd = HERE();
378 EMIT(OOR2, 0); /* offset is very wrong */
379 }
380
381 if (!first) { /* tail-end fixups */
382 AHEAD(prevfwd);
383 ASTERN(O_CH, prevback);
384 }
385
386 assert(!MORE() || SEE(stop));
387}
388
389/*
390 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
Elliott Hughescc213f82012-08-14 15:32:42 -0700391 == static void p_ere_exp(struct parse *p, size_t reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -0800392 */
393static void
Elliott Hughescc213f82012-08-14 15:32:42 -0700394p_ere_exp(
395 struct parse *p,
396 size_t reclimit)
Colin Cross4fa7b102010-01-12 18:59:25 -0800397{
398 char c;
399 sopno pos;
400 int count;
401 int count2;
402 sopno subno;
403 int wascaret = 0;
404
Elliott Hughescc213f82012-08-14 15:32:42 -0700405 _DIAGASSERT(p != NULL);
406
Colin Cross4fa7b102010-01-12 18:59:25 -0800407 assert(MORE()); /* caller should have ensured this */
408 c = GETNEXT();
409
410 pos = HERE();
411 switch (c) {
412 case '(':
413 REQUIRE(MORE(), REG_EPAREN);
414 p->g->nsub++;
415 subno = p->g->nsub;
416 if (subno < NPAREN)
417 p->pbegin[subno] = HERE();
418 EMIT(OLPAREN, subno);
419 if (!SEE(')'))
Elliott Hughescc213f82012-08-14 15:32:42 -0700420 p_ere(p, ')', reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -0800421 if (subno < NPAREN) {
422 p->pend[subno] = HERE();
423 assert(p->pend[subno] != 0);
424 }
425 EMIT(ORPAREN, subno);
426 MUSTEAT(')', REG_EPAREN);
427 break;
428#ifndef POSIX_MISTAKE
429 case ')': /* happens only if no current unmatched ( */
430 /*
431 * You may ask, why the ifndef? Because I didn't notice
432 * this until slightly too late for 1003.2, and none of the
433 * other 1003.2 regular-expression reviewers noticed it at
434 * all. So an unmatched ) is legal POSIX, at least until
435 * we can get it fixed.
436 */
437 SETERROR(REG_EPAREN);
438 break;
439#endif
440 case '^':
441 EMIT(OBOL, 0);
442 p->g->iflags |= USEBOL;
443 p->g->nbol++;
444 wascaret = 1;
445 break;
446 case '$':
447 EMIT(OEOL, 0);
448 p->g->iflags |= USEEOL;
449 p->g->neol++;
450 break;
451 case '|':
452 SETERROR(REG_EMPTY);
453 break;
454 case '*':
455 case '+':
456 case '?':
457 SETERROR(REG_BADRPT);
458 break;
459 case '.':
460 if (p->g->cflags&REG_NEWLINE)
461 nonnewline(p);
462 else
463 EMIT(OANY, 0);
464 break;
465 case '[':
466 p_bracket(p);
467 break;
468 case '\\':
469 REQUIRE(MORE(), REG_EESCAPE);
470 c = GETNEXT();
471 ordinary(p, c);
472 break;
473 case '{': /* okay as ordinary except if digit follows */
Elliott Hughescc213f82012-08-14 15:32:42 -0700474 REQUIRE(!MORE() || !isdigit((unsigned char)PEEK()), REG_BADRPT);
Colin Cross4fa7b102010-01-12 18:59:25 -0800475 /* FALLTHROUGH */
476 default:
Elliott Hughes4fc93ff2019-02-13 14:16:47 -0800477 if (p->error != 0)
478 return;
Colin Cross4fa7b102010-01-12 18:59:25 -0800479 ordinary(p, c);
480 break;
481 }
482
483 if (!MORE())
484 return;
485 c = PEEK();
486 /* we call { a repetition if followed by a digit */
487 if (!( c == '*' || c == '+' || c == '?' ||
Elliott Hughescc213f82012-08-14 15:32:42 -0700488 (c == '{' && MORE2() && isdigit((unsigned char)PEEK2())) ))
Colin Cross4fa7b102010-01-12 18:59:25 -0800489 return; /* no repetition, we're done */
490 NEXT();
491
492 REQUIRE(!wascaret, REG_BADRPT);
493 switch (c) {
494 case '*': /* implemented as +? */
495 /* this case does not require the (y|) trick, noKLUDGE */
496 INSERT(OPLUS_, pos);
497 ASTERN(O_PLUS, pos);
498 INSERT(OQUEST_, pos);
499 ASTERN(O_QUEST, pos);
500 break;
501 case '+':
502 INSERT(OPLUS_, pos);
503 ASTERN(O_PLUS, pos);
504 break;
505 case '?':
506 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
507 INSERT(OCH_, pos); /* offset slightly wrong */
508 ASTERN(OOR1, pos); /* this one's right */
509 AHEAD(pos); /* fix the OCH_ */
510 EMIT(OOR2, 0); /* offset very wrong... */
511 AHEAD(THERE()); /* ...so fix it */
512 ASTERN(O_CH, THERETHERE());
513 break;
514 case '{':
515 count = p_count(p);
516 if (EAT(',')) {
Elliott Hughescc213f82012-08-14 15:32:42 -0700517 if (isdigit((unsigned char)PEEK())) {
Colin Cross4fa7b102010-01-12 18:59:25 -0800518 count2 = p_count(p);
519 REQUIRE(count <= count2, REG_BADBR);
520 } else /* single number with comma */
521 count2 = INFINITY;
522 } else /* just a single number */
523 count2 = count;
Elliott Hughescc213f82012-08-14 15:32:42 -0700524 repeat(p, pos, count, count2, 0);
Colin Cross4fa7b102010-01-12 18:59:25 -0800525 if (!EAT('}')) { /* error heuristics */
526 while (MORE() && PEEK() != '}')
527 NEXT();
528 REQUIRE(MORE(), REG_EBRACE);
529 SETERROR(REG_BADBR);
530 }
531 break;
532 }
533
534 if (!MORE())
535 return;
536 c = PEEK();
537 if (!( c == '*' || c == '+' || c == '?' ||
Elliott Hughescc213f82012-08-14 15:32:42 -0700538 (c == '{' && MORE2() && isdigit((unsigned char)PEEK2())) ) )
Colin Cross4fa7b102010-01-12 18:59:25 -0800539 return;
540 SETERROR(REG_BADRPT);
541}
542
543/*
544 - p_str - string (no metacharacters) "parser"
Elliott Hughescc213f82012-08-14 15:32:42 -0700545 == static void p_str(struct parse *p);
Colin Cross4fa7b102010-01-12 18:59:25 -0800546 */
547static void
Elliott Hughescc213f82012-08-14 15:32:42 -0700548p_str(
549 struct parse *p)
Colin Cross4fa7b102010-01-12 18:59:25 -0800550{
Elliott Hughescc213f82012-08-14 15:32:42 -0700551
552 _DIAGASSERT(p != NULL);
553
Colin Cross4fa7b102010-01-12 18:59:25 -0800554 REQUIRE(MORE(), REG_EMPTY);
555 while (MORE())
556 ordinary(p, GETNEXT());
557}
558
559/*
560 - p_bre - BRE parser top level, anchoring and concatenation
Elliott Hughescc213f82012-08-14 15:32:42 -0700561 == static void p_bre(struct parse *p, int end1, \
562 == int end2, size_t reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -0800563 * Giving end1 as OUT essentially eliminates the end1/end2 check.
564 *
565 * This implementation is a bit of a kludge, in that a trailing $ is first
566 * taken as an ordinary character and then revised to be an anchor. The
567 * only undesirable side effect is that '$' gets included as a character
568 * category in such cases. This is fairly harmless; not worth fixing.
569 * The amount of lookahead needed to avoid this kludge is excessive.
570 */
571static void
Elliott Hughescc213f82012-08-14 15:32:42 -0700572p_bre(
573 struct parse *p,
Colin Cross4fa7b102010-01-12 18:59:25 -0800574 int end1, /* first terminating character */
Elliott Hughescc213f82012-08-14 15:32:42 -0700575 int end2, /* second terminating character */
576 size_t reclimit)
Colin Cross4fa7b102010-01-12 18:59:25 -0800577{
Elliott Hughescc213f82012-08-14 15:32:42 -0700578 sopno start;
Colin Cross4fa7b102010-01-12 18:59:25 -0800579 int first = 1; /* first subexpression? */
580 int wasdollar = 0;
581
Elliott Hughescc213f82012-08-14 15:32:42 -0700582 _DIAGASSERT(p != NULL);
583
584 if (reclimit++ > RECLIMIT || p->error == REG_ESPACE) {
585 p->error = REG_ESPACE;
586 return;
587 }
588
589 start = HERE();
590
Colin Cross4fa7b102010-01-12 18:59:25 -0800591 if (EAT('^')) {
592 EMIT(OBOL, 0);
593 p->g->iflags |= USEBOL;
594 p->g->nbol++;
595 }
596 while (MORE() && !SEETWO(end1, end2)) {
Elliott Hughescc213f82012-08-14 15:32:42 -0700597 wasdollar = p_simp_re(p, first, reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -0800598 first = 0;
599 }
600 if (wasdollar) { /* oops, that was a trailing anchor */
601 DROP(1);
602 EMIT(OEOL, 0);
603 p->g->iflags |= USEEOL;
604 p->g->neol++;
605 }
606
607 REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */
608}
609
610/*
611 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
Elliott Hughescc213f82012-08-14 15:32:42 -0700612 == static int p_simp_re(struct parse *p, int starordinary, size_t reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -0800613 */
614static int /* was the simple RE an unbackslashed $? */
Elliott Hughescc213f82012-08-14 15:32:42 -0700615p_simp_re(
616 struct parse *p,
617 int starordinary, /* is a leading * an ordinary character? */
618 size_t reclimit)
Colin Cross4fa7b102010-01-12 18:59:25 -0800619{
620 int c;
621 int count;
622 int count2;
Elliott Hughescc213f82012-08-14 15:32:42 -0700623 sopno pos, i;
Colin Cross4fa7b102010-01-12 18:59:25 -0800624 sopno subno;
625# define BACKSL (1<<CHAR_BIT)
626
Elliott Hughescc213f82012-08-14 15:32:42 -0700627 _DIAGASSERT(p != NULL);
628
Colin Cross4fa7b102010-01-12 18:59:25 -0800629 pos = HERE(); /* repetion op, if any, covers from here */
630
631 assert(MORE()); /* caller should have ensured this */
632 c = GETNEXT();
633 if (c == '\\') {
634 REQUIRE(MORE(), REG_EESCAPE);
Elliott Hughescc213f82012-08-14 15:32:42 -0700635 c = BACKSL | (unsigned char)GETNEXT();
Colin Cross4fa7b102010-01-12 18:59:25 -0800636 }
637 switch (c) {
638 case '.':
639 if (p->g->cflags&REG_NEWLINE)
640 nonnewline(p);
641 else
642 EMIT(OANY, 0);
643 break;
644 case '[':
645 p_bracket(p);
646 break;
647 case BACKSL|'{':
648 SETERROR(REG_BADRPT);
649 break;
650 case BACKSL|'(':
651 p->g->nsub++;
652 subno = p->g->nsub;
653 if (subno < NPAREN)
654 p->pbegin[subno] = HERE();
655 EMIT(OLPAREN, subno);
656 /* the MORE here is an error heuristic */
657 if (MORE() && !SEETWO('\\', ')'))
Elliott Hughescc213f82012-08-14 15:32:42 -0700658 p_bre(p, '\\', ')', reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -0800659 if (subno < NPAREN) {
660 p->pend[subno] = HERE();
661 assert(p->pend[subno] != 0);
662 }
663 EMIT(ORPAREN, subno);
664 REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
665 break;
666 case BACKSL|')': /* should not get here -- must be user */
667 case BACKSL|'}':
668 SETERROR(REG_EPAREN);
669 break;
670 case BACKSL|'1':
671 case BACKSL|'2':
672 case BACKSL|'3':
673 case BACKSL|'4':
674 case BACKSL|'5':
675 case BACKSL|'6':
676 case BACKSL|'7':
677 case BACKSL|'8':
678 case BACKSL|'9':
679 i = (c&~BACKSL) - '0';
680 assert(i < NPAREN);
681 if (p->pend[i] != 0) {
682 assert(i <= p->g->nsub);
683 EMIT(OBACK_, i);
684 assert(p->pbegin[i] != 0);
685 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
686 assert(OP(p->strip[p->pend[i]]) == ORPAREN);
687 (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
688 EMIT(O_BACK, i);
689 } else
690 SETERROR(REG_ESUBREG);
691 p->g->backrefs = 1;
692 break;
693 case '*':
694 REQUIRE(starordinary, REG_BADRPT);
695 /* FALLTHROUGH */
696 default:
Elliott Hughes4fc93ff2019-02-13 14:16:47 -0800697 if (p->error != 0)
698 return(0);
Elliott Hughescc213f82012-08-14 15:32:42 -0700699 ordinary(p, c &~ BACKSL);
Colin Cross4fa7b102010-01-12 18:59:25 -0800700 break;
701 }
702
703 if (EAT('*')) { /* implemented as +? */
704 /* this case does not require the (y|) trick, noKLUDGE */
705 INSERT(OPLUS_, pos);
706 ASTERN(O_PLUS, pos);
707 INSERT(OQUEST_, pos);
708 ASTERN(O_QUEST, pos);
709 } else if (EATTWO('\\', '{')) {
710 count = p_count(p);
711 if (EAT(',')) {
Elliott Hughescc213f82012-08-14 15:32:42 -0700712 if (MORE() && isdigit((unsigned char)PEEK())) {
Colin Cross4fa7b102010-01-12 18:59:25 -0800713 count2 = p_count(p);
714 REQUIRE(count <= count2, REG_BADBR);
715 } else /* single number with comma */
716 count2 = INFINITY;
717 } else /* just a single number */
718 count2 = count;
Elliott Hughescc213f82012-08-14 15:32:42 -0700719 repeat(p, pos, count, count2, 0);
Colin Cross4fa7b102010-01-12 18:59:25 -0800720 if (!EATTWO('\\', '}')) { /* error heuristics */
721 while (MORE() && !SEETWO('\\', '}'))
722 NEXT();
723 REQUIRE(MORE(), REG_EBRACE);
724 SETERROR(REG_BADBR);
725 }
Elliott Hughescc213f82012-08-14 15:32:42 -0700726 } else if (c == (unsigned char)'$') /* $ (but not \$) ends it */
Colin Cross4fa7b102010-01-12 18:59:25 -0800727 return(1);
728
729 return(0);
730}
731
732/*
733 - p_count - parse a repetition count
Elliott Hughescc213f82012-08-14 15:32:42 -0700734 == static int p_count(struct parse *p);
Colin Cross4fa7b102010-01-12 18:59:25 -0800735 */
736static int /* the value */
Elliott Hughescc213f82012-08-14 15:32:42 -0700737p_count(
738 struct parse *p)
Colin Cross4fa7b102010-01-12 18:59:25 -0800739{
740 int count = 0;
741 int ndigits = 0;
742
Elliott Hughescc213f82012-08-14 15:32:42 -0700743 _DIAGASSERT(p != NULL);
744
745 while (MORE() && isdigit((unsigned char)PEEK()) && count <= DUPMAX) {
Colin Cross4fa7b102010-01-12 18:59:25 -0800746 count = count*10 + (GETNEXT() - '0');
747 ndigits++;
748 }
749
750 REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
751 return(count);
752}
753
754/*
755 - p_bracket - parse a bracketed character list
Elliott Hughescc213f82012-08-14 15:32:42 -0700756 == static void p_bracket(struct parse *p);
Colin Cross4fa7b102010-01-12 18:59:25 -0800757 *
758 * Note a significant property of this code: if the allocset() did SETERROR,
759 * no set operations are done.
760 */
761static void
Elliott Hughescc213f82012-08-14 15:32:42 -0700762p_bracket(
763 struct parse *p)
Colin Cross4fa7b102010-01-12 18:59:25 -0800764{
765 cset *cs;
766 int invert = 0;
Elliott Hughescc213f82012-08-14 15:32:42 -0700767 _DIAGASSERT(p != NULL);
768
769 cs = allocset(p);
770 if (cs == NULL)
771 return;
Colin Cross4fa7b102010-01-12 18:59:25 -0800772
773 /* Dept of Truly Sickening Special-Case Kludges */
Elliott Hughescc213f82012-08-14 15:32:42 -0700774 if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]",
775 (size_t)6) == 0) {
Colin Cross4fa7b102010-01-12 18:59:25 -0800776 EMIT(OBOW, 0);
777 NEXTn(6);
778 return;
779 }
Elliott Hughescc213f82012-08-14 15:32:42 -0700780 if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]",
781 (size_t)6) == 0) {
Colin Cross4fa7b102010-01-12 18:59:25 -0800782 EMIT(OEOW, 0);
783 NEXTn(6);
784 return;
785 }
786
Colin Cross4fa7b102010-01-12 18:59:25 -0800787 if (EAT('^'))
788 invert++; /* make note to invert set at end */
789 if (EAT(']'))
790 CHadd(cs, ']');
791 else if (EAT('-'))
792 CHadd(cs, '-');
793 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
794 p_b_term(p, cs);
795 if (EAT('-'))
796 CHadd(cs, '-');
797 MUSTEAT(']', REG_EBRACK);
798
Elliott Hughescc213f82012-08-14 15:32:42 -0700799 if (p->error != 0) /* don't mess things up further */
Colin Cross4fa7b102010-01-12 18:59:25 -0800800 return;
Colin Cross4fa7b102010-01-12 18:59:25 -0800801
802 if (p->g->cflags&REG_ICASE) {
Elliott Hughescc213f82012-08-14 15:32:42 -0700803 ssize_t i;
Colin Cross4fa7b102010-01-12 18:59:25 -0800804 int ci;
805
806 for (i = p->g->csetsize - 1; i >= 0; i--)
807 if (CHIN(cs, i) && isalpha(i)) {
Elliott Hughescc213f82012-08-14 15:32:42 -0700808 ci = othercase((int)i);
Colin Cross4fa7b102010-01-12 18:59:25 -0800809 if (ci != i)
810 CHadd(cs, ci);
811 }
812 if (cs->multis != NULL)
813 mccase(p, cs);
814 }
815 if (invert) {
Elliott Hughescc213f82012-08-14 15:32:42 -0700816 ssize_t i;
Colin Cross4fa7b102010-01-12 18:59:25 -0800817
818 for (i = p->g->csetsize - 1; i >= 0; i--)
819 if (CHIN(cs, i))
Elliott Hughescc213f82012-08-14 15:32:42 -0700820 CHsub(cs, (int)i);
Colin Cross4fa7b102010-01-12 18:59:25 -0800821 else
Elliott Hughescc213f82012-08-14 15:32:42 -0700822 CHadd(cs, (int)i);
Colin Cross4fa7b102010-01-12 18:59:25 -0800823 if (p->g->cflags&REG_NEWLINE)
824 CHsub(cs, '\n');
825 if (cs->multis != NULL)
826 mcinvert(p, cs);
827 }
828
829 assert(cs->multis == NULL); /* xxx */
830
831 if (nch(p, cs) == 1) { /* optimize singleton sets */
832 ordinary(p, firstch(p, cs));
833 freeset(p, cs);
834 } else
835 EMIT(OANYOF, freezeset(p, cs));
836}
837
838/*
839 - p_b_term - parse one term of a bracketed character list
Elliott Hughescc213f82012-08-14 15:32:42 -0700840 == static void p_b_term(struct parse *p, cset *cs);
Colin Cross4fa7b102010-01-12 18:59:25 -0800841 */
842static void
Elliott Hughescc213f82012-08-14 15:32:42 -0700843p_b_term(
844 struct parse *p,
845 cset *cs)
Colin Cross4fa7b102010-01-12 18:59:25 -0800846{
847 char c;
848 char start, finish;
849 int i;
850
Elliott Hughescc213f82012-08-14 15:32:42 -0700851 _DIAGASSERT(p != NULL);
852 _DIAGASSERT(cs != NULL);
853
Colin Cross4fa7b102010-01-12 18:59:25 -0800854 /* classify what we've got */
855 switch ((MORE()) ? PEEK() : '\0') {
856 case '[':
857 c = (MORE2()) ? PEEK2() : '\0';
858 break;
Elliott Hughescc213f82012-08-14 15:32:42 -0700859
Colin Cross4fa7b102010-01-12 18:59:25 -0800860 case '-':
861 SETERROR(REG_ERANGE);
862 return; /* NOTE RETURN */
Elliott Hughescc213f82012-08-14 15:32:42 -0700863
Colin Cross4fa7b102010-01-12 18:59:25 -0800864 default:
865 c = '\0';
866 break;
867 }
868
869 switch (c) {
870 case ':': /* character class */
871 NEXT2();
872 REQUIRE(MORE(), REG_EBRACK);
873 c = PEEK();
874 REQUIRE(c != '-' && c != ']', REG_ECTYPE);
875 p_b_cclass(p, cs);
876 REQUIRE(MORE(), REG_EBRACK);
877 REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
878 break;
879 case '=': /* equivalence class */
880 NEXT2();
881 REQUIRE(MORE(), REG_EBRACK);
882 c = PEEK();
883 REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
884 p_b_eclass(p, cs);
885 REQUIRE(MORE(), REG_EBRACK);
886 REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
887 break;
888 default: /* symbol, ordinary character, or range */
889/* xxx revision needed for multichar stuff */
890 start = p_b_symbol(p);
891 if (SEE('-') && MORE2() && PEEK2() != ']') {
892 /* range */
893 NEXT();
894 if (EAT('-'))
895 finish = '-';
896 else
897 finish = p_b_symbol(p);
898 } else
899 finish = start;
900/* xxx what about signed chars here... */
901 REQUIRE(start <= finish, REG_ERANGE);
902 for (i = start; i <= finish; i++)
903 CHadd(cs, i);
904 break;
905 }
906}
907
908/*
909 - p_b_cclass - parse a character-class name and deal with it
Elliott Hughescc213f82012-08-14 15:32:42 -0700910 == static void p_b_cclass(struct parse *p, cset *cs);
Colin Cross4fa7b102010-01-12 18:59:25 -0800911 */
912static void
Elliott Hughescc213f82012-08-14 15:32:42 -0700913p_b_cclass(
914 struct parse *p,
915 cset *cs)
Colin Cross4fa7b102010-01-12 18:59:25 -0800916{
Elliott Hughescc213f82012-08-14 15:32:42 -0700917 const char *sp;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700918 const struct cclass *cp;
Colin Cross4fa7b102010-01-12 18:59:25 -0800919 size_t len;
Elliott Hughescc213f82012-08-14 15:32:42 -0700920 const char *u;
Colin Cross4fa7b102010-01-12 18:59:25 -0800921 char c;
922
Elliott Hughescc213f82012-08-14 15:32:42 -0700923 _DIAGASSERT(p != NULL);
924 _DIAGASSERT(cs != NULL);
925
926 sp = p->next;
927
928 while (MORE() && isalpha((unsigned char)PEEK()))
Colin Cross4fa7b102010-01-12 18:59:25 -0800929 NEXT();
930 len = p->next - sp;
931 for (cp = cclasses; cp->name != NULL; cp++)
932 if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
933 break;
934 if (cp->name == NULL) {
935 /* oops, didn't find it */
936 SETERROR(REG_ECTYPE);
937 return;
938 }
939
940 u = cp->chars;
941 while ((c = *u++) != '\0')
942 CHadd(cs, c);
943 for (u = cp->multis; *u != '\0'; u += strlen(u) + 1)
944 MCadd(p, cs, u);
945}
946
947/*
948 - p_b_eclass - parse an equivalence-class name and deal with it
Elliott Hughescc213f82012-08-14 15:32:42 -0700949 == static void p_b_eclass(struct parse *p, cset *cs);
Colin Cross4fa7b102010-01-12 18:59:25 -0800950 *
951 * This implementation is incomplete. xxx
952 */
953static void
Elliott Hughescc213f82012-08-14 15:32:42 -0700954p_b_eclass(
955 struct parse *p,
956 cset *cs)
Colin Cross4fa7b102010-01-12 18:59:25 -0800957{
958 char c;
959
Elliott Hughescc213f82012-08-14 15:32:42 -0700960 _DIAGASSERT(p != NULL);
961 _DIAGASSERT(cs != NULL);
962
Colin Cross4fa7b102010-01-12 18:59:25 -0800963 c = p_b_coll_elem(p, '=');
964 CHadd(cs, c);
965}
966
967/*
968 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
Elliott Hughescc213f82012-08-14 15:32:42 -0700969 == static char p_b_symbol(struct parse *p);
Colin Cross4fa7b102010-01-12 18:59:25 -0800970 */
971static char /* value of symbol */
Elliott Hughescc213f82012-08-14 15:32:42 -0700972p_b_symbol(
973 struct parse *p)
Colin Cross4fa7b102010-01-12 18:59:25 -0800974{
975 char value;
976
Elliott Hughescc213f82012-08-14 15:32:42 -0700977 _DIAGASSERT(p != NULL);
978
Colin Cross4fa7b102010-01-12 18:59:25 -0800979 REQUIRE(MORE(), REG_EBRACK);
980 if (!EATTWO('[', '.'))
981 return(GETNEXT());
982
983 /* collating symbol */
984 value = p_b_coll_elem(p, '.');
985 REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
986 return(value);
987}
988
989/*
990 - p_b_coll_elem - parse a collating-element name and look it up
Elliott Hughescc213f82012-08-14 15:32:42 -0700991 == static char p_b_coll_elem(struct parse *p, int endc);
Colin Cross4fa7b102010-01-12 18:59:25 -0800992 */
993static char /* value of collating element */
Elliott Hughescc213f82012-08-14 15:32:42 -0700994p_b_coll_elem(
995 struct parse *p,
Colin Cross4fa7b102010-01-12 18:59:25 -0800996 int endc) /* name ended by endc,']' */
997{
Elliott Hughescc213f82012-08-14 15:32:42 -0700998 const char *sp;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700999 const struct cname *cp;
Elliott Hughescc213f82012-08-14 15:32:42 -07001000 size_t len;
1001
1002 _DIAGASSERT(p != NULL);
1003
1004 sp = p->next;
Colin Cross4fa7b102010-01-12 18:59:25 -08001005
1006 while (MORE() && !SEETWO(endc, ']'))
1007 NEXT();
1008 if (!MORE()) {
1009 SETERROR(REG_EBRACK);
1010 return(0);
1011 }
1012 len = p->next - sp;
1013 for (cp = cnames; cp->name != NULL; cp++)
Elliott Hughes4fc93ff2019-02-13 14:16:47 -08001014 if (strncmp(cp->name, sp, len) == 0 && strlen(cp->name) == len)
Colin Cross4fa7b102010-01-12 18:59:25 -08001015 return(cp->code); /* known name */
1016 if (len == 1)
1017 return(*sp); /* single character */
1018 SETERROR(REG_ECOLLATE); /* neither */
1019 return(0);
1020}
1021
1022/*
1023 - othercase - return the case counterpart of an alphabetic
Elliott Hughescc213f82012-08-14 15:32:42 -07001024 == static int othercase(int ch);
Colin Cross4fa7b102010-01-12 18:59:25 -08001025 */
Elliott Hughescc213f82012-08-14 15:32:42 -07001026static int /* if no counterpart, return ch */
1027othercase(
1028 int ch)
Colin Cross4fa7b102010-01-12 18:59:25 -08001029{
Colin Cross4fa7b102010-01-12 18:59:25 -08001030 assert(isalpha(ch));
1031 if (isupper(ch))
Elliott Hughescc213f82012-08-14 15:32:42 -07001032 return(tolower(ch));
Colin Cross4fa7b102010-01-12 18:59:25 -08001033 else if (islower(ch))
Elliott Hughescc213f82012-08-14 15:32:42 -07001034 return(toupper(ch));
Colin Cross4fa7b102010-01-12 18:59:25 -08001035 else /* peculiar, but could happen */
1036 return(ch);
1037}
1038
1039/*
1040 - bothcases - emit a dualcase version of a two-case character
Elliott Hughescc213f82012-08-14 15:32:42 -07001041 == static void bothcases(struct parse *p, int ch);
Colin Cross4fa7b102010-01-12 18:59:25 -08001042 *
1043 * Boy, is this implementation ever a kludge...
1044 */
1045static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001046bothcases(
1047 struct parse *p,
1048 int ch)
Colin Cross4fa7b102010-01-12 18:59:25 -08001049{
Elliott Hughescc213f82012-08-14 15:32:42 -07001050 const char *oldnext;
1051 const char *oldend;
Colin Cross4fa7b102010-01-12 18:59:25 -08001052 char bracket[3];
1053
Elliott Hughescc213f82012-08-14 15:32:42 -07001054 _DIAGASSERT(p != NULL);
1055
1056 oldnext = p->next;
1057 oldend = p->end;
1058
Colin Cross4fa7b102010-01-12 18:59:25 -08001059 assert(othercase(ch) != ch); /* p_bracket() would recurse */
1060 p->next = bracket;
1061 p->end = bracket+2;
1062 bracket[0] = ch;
1063 bracket[1] = ']';
1064 bracket[2] = '\0';
1065 p_bracket(p);
1066 assert(p->next == bracket+2);
1067 p->next = oldnext;
1068 p->end = oldend;
1069}
1070
1071/*
1072 - ordinary - emit an ordinary character
Elliott Hughescc213f82012-08-14 15:32:42 -07001073 == static void ordinary(struct parse *p, int ch);
Colin Cross4fa7b102010-01-12 18:59:25 -08001074 */
1075static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001076ordinary(
1077 struct parse *p,
1078 int ch)
Colin Cross4fa7b102010-01-12 18:59:25 -08001079{
Elliott Hughescc213f82012-08-14 15:32:42 -07001080 cat_t *cap;
Elliott Hughes71927a82015-09-14 18:45:52 -07001081 unsigned char uc = (unsigned char)ch;
Colin Cross4fa7b102010-01-12 18:59:25 -08001082
Elliott Hughescc213f82012-08-14 15:32:42 -07001083 _DIAGASSERT(p != NULL);
1084
1085 cap = p->g->categories;
Elliott Hughes71927a82015-09-14 18:45:52 -07001086 if ((p->g->cflags & REG_ICASE) && isalpha(uc) && othercase(uc) != uc)
1087 bothcases(p, uc);
Colin Cross4fa7b102010-01-12 18:59:25 -08001088 else {
Elliott Hughes71927a82015-09-14 18:45:52 -07001089 EMIT(OCHAR, (sopno)uc);
1090 if (cap[uc] == 0) {
Elliott Hughescc213f82012-08-14 15:32:42 -07001091 _DIAGASSERT(__type_fit(unsigned char,
1092 p->g->ncategories + 1));
Elliott Hughes71927a82015-09-14 18:45:52 -07001093 cap[uc] = (unsigned char)p->g->ncategories++;
Elliott Hughescc213f82012-08-14 15:32:42 -07001094 }
Colin Cross4fa7b102010-01-12 18:59:25 -08001095 }
1096}
1097
1098/*
1099 - nonnewline - emit REG_NEWLINE version of OANY
Elliott Hughescc213f82012-08-14 15:32:42 -07001100 == static void nonnewline(struct parse *p);
Colin Cross4fa7b102010-01-12 18:59:25 -08001101 *
1102 * Boy, is this implementation ever a kludge...
1103 */
1104static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001105nonnewline(
1106 struct parse *p)
Colin Cross4fa7b102010-01-12 18:59:25 -08001107{
Elliott Hughescc213f82012-08-14 15:32:42 -07001108 const char *oldnext;
1109 const char *oldend;
Colin Cross4fa7b102010-01-12 18:59:25 -08001110 char bracket[4];
1111
Elliott Hughescc213f82012-08-14 15:32:42 -07001112 _DIAGASSERT(p != NULL);
1113
1114 oldnext = p->next;
1115 oldend = p->end;
1116
Colin Cross4fa7b102010-01-12 18:59:25 -08001117 p->next = bracket;
1118 p->end = bracket+3;
1119 bracket[0] = '^';
1120 bracket[1] = '\n';
1121 bracket[2] = ']';
1122 bracket[3] = '\0';
1123 p_bracket(p);
1124 assert(p->next == bracket+3);
1125 p->next = oldnext;
1126 p->end = oldend;
1127}
1128
1129/*
1130 - repeat - generate code for a bounded repetition, recursively if needed
Elliott Hughescc213f82012-08-14 15:32:42 -07001131 == static void repeat(struct parse *p, sopno start, int from, int to,
1132 == size_t reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -08001133 */
1134static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001135repeat(
1136 struct parse *p,
Colin Cross4fa7b102010-01-12 18:59:25 -08001137 sopno start, /* operand from here to end of strip */
1138 int from, /* repeated from this number */
Elliott Hughescc213f82012-08-14 15:32:42 -07001139 int to, /* to this number of times (maybe INFINITY) */
1140 size_t reclimit)
Colin Cross4fa7b102010-01-12 18:59:25 -08001141{
Elliott Hughescc213f82012-08-14 15:32:42 -07001142 sopno finish;
Colin Cross4fa7b102010-01-12 18:59:25 -08001143# define N 2
1144# define INF 3
1145# define REP(f, t) ((f)*8 + (t))
1146# define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1147 sopno copy;
1148
Elliott Hughescc213f82012-08-14 15:32:42 -07001149 _DIAGASSERT(p != NULL);
1150
1151 if (reclimit++ > RECLIMIT)
1152 p->error = REG_ESPACE;
1153 if (p->error)
Colin Cross4fa7b102010-01-12 18:59:25 -08001154 return;
1155
Elliott Hughescc213f82012-08-14 15:32:42 -07001156 finish = HERE();
1157
Colin Cross4fa7b102010-01-12 18:59:25 -08001158 assert(from <= to);
1159
1160 switch (REP(MAP(from), MAP(to))) {
1161 case REP(0, 0): /* must be user doing this */
1162 DROP(finish-start); /* drop the operand */
1163 break;
1164 case REP(0, 1): /* as x{1,1}? */
1165 case REP(0, N): /* as x{1,n}? */
1166 case REP(0, INF): /* as x{1,}? */
1167 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1168 INSERT(OCH_, start); /* offset is wrong... */
Elliott Hughescc213f82012-08-14 15:32:42 -07001169 repeat(p, start+1, 1, to, reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -08001170 ASTERN(OOR1, start);
1171 AHEAD(start); /* ... fix it */
1172 EMIT(OOR2, 0);
1173 AHEAD(THERE());
1174 ASTERN(O_CH, THERETHERE());
1175 break;
1176 case REP(1, 1): /* trivial case */
1177 /* done */
1178 break;
1179 case REP(1, N): /* as x?x{1,n-1} */
1180 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1181 INSERT(OCH_, start);
1182 ASTERN(OOR1, start);
1183 AHEAD(start);
1184 EMIT(OOR2, 0); /* offset very wrong... */
1185 AHEAD(THERE()); /* ...so fix it */
1186 ASTERN(O_CH, THERETHERE());
1187 copy = dupl(p, start+1, finish+1);
1188 assert(copy == finish+4);
Elliott Hughescc213f82012-08-14 15:32:42 -07001189 repeat(p, copy, 1, to-1, reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -08001190 break;
1191 case REP(1, INF): /* as x+ */
1192 INSERT(OPLUS_, start);
1193 ASTERN(O_PLUS, start);
1194 break;
1195 case REP(N, N): /* as xx{m-1,n-1} */
1196 copy = dupl(p, start, finish);
Elliott Hughescc213f82012-08-14 15:32:42 -07001197 repeat(p, copy, from-1, to-1, reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -08001198 break;
1199 case REP(N, INF): /* as xx{n-1,INF} */
1200 copy = dupl(p, start, finish);
Elliott Hughescc213f82012-08-14 15:32:42 -07001201 repeat(p, copy, from-1, to, reclimit);
Colin Cross4fa7b102010-01-12 18:59:25 -08001202 break;
1203 default: /* "can't happen" */
1204 SETERROR(REG_ASSERT); /* just in case */
1205 break;
1206 }
1207}
1208
1209/*
1210 - seterr - set an error condition
Elliott Hughescc213f82012-08-14 15:32:42 -07001211 == static int seterr(struct parse *p, int e);
Colin Cross4fa7b102010-01-12 18:59:25 -08001212 */
1213static int /* useless but makes type checking happy */
Elliott Hughescc213f82012-08-14 15:32:42 -07001214seterr(
1215 struct parse *p,
1216 int e)
Colin Cross4fa7b102010-01-12 18:59:25 -08001217{
Elliott Hughescc213f82012-08-14 15:32:42 -07001218
1219 _DIAGASSERT(p != NULL);
1220
Colin Cross4fa7b102010-01-12 18:59:25 -08001221 if (p->error == 0) /* keep earliest error condition */
1222 p->error = e;
1223 p->next = nuls; /* try to bring things to a halt */
1224 p->end = nuls;
1225 return(0); /* make the return value well-defined */
1226}
1227
1228/*
1229 - allocset - allocate a set of characters for []
Elliott Hughescc213f82012-08-14 15:32:42 -07001230 == static cset *allocset(struct parse *p);
Colin Cross4fa7b102010-01-12 18:59:25 -08001231 */
1232static cset *
Elliott Hughescc213f82012-08-14 15:32:42 -07001233allocset(
1234 struct parse *p)
Colin Cross4fa7b102010-01-12 18:59:25 -08001235{
Elliott Hughescc213f82012-08-14 15:32:42 -07001236 size_t no;
Colin Cross4fa7b102010-01-12 18:59:25 -08001237 size_t nc;
1238 size_t nbytes;
1239 cset *cs;
Elliott Hughescc213f82012-08-14 15:32:42 -07001240 size_t css;
1241 size_t i;
Elliott Hughes71927a82015-09-14 18:45:52 -07001242 void *old_ptr;
Colin Cross4fa7b102010-01-12 18:59:25 -08001243
Elliott Hughescc213f82012-08-14 15:32:42 -07001244 _DIAGASSERT(p != NULL);
1245
1246 no = p->g->ncsets++;
1247 css = (size_t)p->g->csetsize;
Colin Cross4fa7b102010-01-12 18:59:25 -08001248 if (no >= p->ncsalloc) { /* need another column of space */
Colin Cross4fa7b102010-01-12 18:59:25 -08001249 p->ncsalloc += CHAR_BIT;
1250 nc = p->ncsalloc;
1251 assert(nc % CHAR_BIT == 0);
1252 nbytes = nc / CHAR_BIT * css;
Elliott Hughescc213f82012-08-14 15:32:42 -07001253 if (MEMSIZE(p) > MEMLIMIT)
1254 goto oomem;
Elliott Hughes71927a82015-09-14 18:45:52 -07001255 if (reallocarr(&p->g->sets, nc, sizeof(cset)))
1256 goto oomem;
1257 old_ptr = p->g->setbits;
1258 if (reallocarr(&p->g->setbits, nc / CHAR_BIT, css)) {
1259 free(old_ptr);
1260 goto oomem;
1261 }
1262 if (old_ptr != p->g->setbits) {
Elliott Hughescc213f82012-08-14 15:32:42 -07001263 for (i = 0; i < no; i++)
1264 p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT);
1265 }
Elliott Hughes71927a82015-09-14 18:45:52 -07001266 (void) memset((char *)p->g->setbits + (nbytes - css), 0, css);
Colin Cross4fa7b102010-01-12 18:59:25 -08001267 }
Colin Cross4fa7b102010-01-12 18:59:25 -08001268
1269 cs = &p->g->sets[no];
1270 cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
Elliott Hughescc213f82012-08-14 15:32:42 -07001271 cs->mask = 1 << (unsigned int)((no) % CHAR_BIT);
Colin Cross4fa7b102010-01-12 18:59:25 -08001272 cs->hash = 0;
1273 cs->smultis = 0;
1274 cs->multis = NULL;
1275
1276 return(cs);
Elliott Hughes71927a82015-09-14 18:45:52 -07001277
1278oomem:
1279 SETERROR(REG_ESPACE);
1280 /* caller's responsibility not to do set ops */
1281 return NULL;
Colin Cross4fa7b102010-01-12 18:59:25 -08001282}
1283
1284/*
1285 - freeset - free a now-unused set
Elliott Hughescc213f82012-08-14 15:32:42 -07001286 == static void freeset(struct parse *p, cset *cs);
Colin Cross4fa7b102010-01-12 18:59:25 -08001287 */
1288static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001289freeset(
1290 struct parse *p,
1291 cset *cs)
Colin Cross4fa7b102010-01-12 18:59:25 -08001292{
Elliott Hughescc213f82012-08-14 15:32:42 -07001293 size_t i;
1294 cset *top;
1295 size_t css;
Colin Cross4fa7b102010-01-12 18:59:25 -08001296
Elliott Hughescc213f82012-08-14 15:32:42 -07001297 _DIAGASSERT(p != NULL);
1298 _DIAGASSERT(cs != NULL);
1299
1300 top = &p->g->sets[p->g->ncsets];
1301 css = (size_t)p->g->csetsize;
1302
1303 for (i = 0; i < css; i++)
1304 CHsub(cs, (int)i);
Colin Cross4fa7b102010-01-12 18:59:25 -08001305 if (cs == top-1) /* recover only the easy case */
1306 p->g->ncsets--;
1307}
1308
1309/*
1310 - freezeset - final processing on a set of characters
Elliott Hughescc213f82012-08-14 15:32:42 -07001311 == static int freezeset(struct parse *p, cset *cs);
Colin Cross4fa7b102010-01-12 18:59:25 -08001312 *
1313 * The main task here is merging identical sets. This is usually a waste
1314 * of time (although the hash code minimizes the overhead), but can win
1315 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1316 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1317 * the same value!
1318 */
Elliott Hughescc213f82012-08-14 15:32:42 -07001319static sopno /* set number */
1320freezeset(
1321 struct parse *p,
1322 cset *cs)
Colin Cross4fa7b102010-01-12 18:59:25 -08001323{
Elliott Hughescc213f82012-08-14 15:32:42 -07001324 uch h;
1325 size_t i;
1326 cset *top;
Colin Cross4fa7b102010-01-12 18:59:25 -08001327 cset *cs2;
Elliott Hughescc213f82012-08-14 15:32:42 -07001328 size_t css;
1329
1330 _DIAGASSERT(p != NULL);
1331 _DIAGASSERT(cs != NULL);
1332
1333 h = cs->hash;
1334 top = &p->g->sets[p->g->ncsets];
1335 css = (size_t)p->g->csetsize;
Colin Cross4fa7b102010-01-12 18:59:25 -08001336
1337 /* look for an earlier one which is the same */
1338 for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1339 if (cs2->hash == h && cs2 != cs) {
1340 /* maybe */
Elliott Hughescc213f82012-08-14 15:32:42 -07001341 for (i = 0; i < css; i++)
Colin Cross4fa7b102010-01-12 18:59:25 -08001342 if (!!CHIN(cs2, i) != !!CHIN(cs, i))
1343 break; /* no */
Elliott Hughescc213f82012-08-14 15:32:42 -07001344 if (i == css)
Colin Cross4fa7b102010-01-12 18:59:25 -08001345 break; /* yes */
1346 }
1347
1348 if (cs2 < top) { /* found one */
1349 freeset(p, cs);
1350 cs = cs2;
1351 }
1352
Elliott Hughescc213f82012-08-14 15:32:42 -07001353 return (sopno)(cs - p->g->sets);
Colin Cross4fa7b102010-01-12 18:59:25 -08001354}
1355
1356/*
1357 - firstch - return first character in a set (which must have at least one)
Elliott Hughescc213f82012-08-14 15:32:42 -07001358 == static int firstch(struct parse *p, cset *cs);
Colin Cross4fa7b102010-01-12 18:59:25 -08001359 */
1360static int /* character; there is no "none" value */
Elliott Hughescc213f82012-08-14 15:32:42 -07001361firstch(
1362 struct parse *p,
1363 cset *cs)
Colin Cross4fa7b102010-01-12 18:59:25 -08001364{
Elliott Hughescc213f82012-08-14 15:32:42 -07001365 size_t i;
1366 size_t css;
Colin Cross4fa7b102010-01-12 18:59:25 -08001367
Elliott Hughescc213f82012-08-14 15:32:42 -07001368 _DIAGASSERT(p != NULL);
1369 _DIAGASSERT(cs != NULL);
1370
1371 css = (size_t)p->g->csetsize;
1372
1373 for (i = 0; i < css; i++)
Colin Cross4fa7b102010-01-12 18:59:25 -08001374 if (CHIN(cs, i))
1375 return((char)i);
1376 assert(never);
1377 return(0); /* arbitrary */
1378}
1379
1380/*
1381 - nch - number of characters in a set
Elliott Hughescc213f82012-08-14 15:32:42 -07001382 == static int nch(struct parse *p, cset *cs);
Colin Cross4fa7b102010-01-12 18:59:25 -08001383 */
1384static int
Elliott Hughescc213f82012-08-14 15:32:42 -07001385nch(
1386 struct parse *p,
1387 cset *cs)
Colin Cross4fa7b102010-01-12 18:59:25 -08001388{
Elliott Hughescc213f82012-08-14 15:32:42 -07001389 size_t i;
1390 size_t css;
Colin Cross4fa7b102010-01-12 18:59:25 -08001391 int n = 0;
1392
Elliott Hughescc213f82012-08-14 15:32:42 -07001393 _DIAGASSERT(p != NULL);
1394 _DIAGASSERT(cs != NULL);
1395
1396 css = (size_t)p->g->csetsize;
1397
1398 for (i = 0; i < css; i++)
Colin Cross4fa7b102010-01-12 18:59:25 -08001399 if (CHIN(cs, i))
1400 n++;
1401 return(n);
1402}
1403
1404/*
1405 - mcadd - add a collating element to a cset
Elliott Hughescc213f82012-08-14 15:32:42 -07001406 == static void mcadd(struct parse *p, cset *cs, \
1407 == char *cp);
Colin Cross4fa7b102010-01-12 18:59:25 -08001408 */
1409static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001410mcadd(
1411 struct parse *p,
1412 cset *cs,
1413 const char *cp)
Colin Cross4fa7b102010-01-12 18:59:25 -08001414{
Elliott Hughescc213f82012-08-14 15:32:42 -07001415 size_t oldend;
1416
1417 _DIAGASSERT(p != NULL);
1418 _DIAGASSERT(cs != NULL);
1419 _DIAGASSERT(cp != NULL);
1420
1421 oldend = cs->smultis;
Colin Cross4fa7b102010-01-12 18:59:25 -08001422
1423 cs->smultis += strlen(cp) + 1;
Elliott Hughescc213f82012-08-14 15:32:42 -07001424 if (cs->multis == NULL)
1425 cs->multis = malloc(cs->smultis);
1426 else
1427 cs->multis = realloc(cs->multis, cs->smultis);
1428 if (cs->multis == NULL) {
Colin Cross4fa7b102010-01-12 18:59:25 -08001429 SETERROR(REG_ESPACE);
1430 return;
1431 }
Colin Cross4fa7b102010-01-12 18:59:25 -08001432
Elliott Hughescc213f82012-08-14 15:32:42 -07001433 (void) strcpy(cs->multis + oldend - 1, cp);
1434 cs->multis[cs->smultis - 1] = '\0';
1435}
1436
1437#if 0
1438/*
1439 - mcsub - subtract a collating element from a cset
1440 == static void mcsub(cset *cs, char *cp);
1441 */
1442static void
1443mcsub(
1444 cset *cs,
1445 char *cp)
1446{
1447 char *fp;
1448 size_t len;
1449
1450 _DIAGASSERT(cs != NULL);
1451 _DIAGASSERT(cp != NULL);
1452
1453 fp = mcfind(cs, cp);
1454 len = strlen(fp);
1455
1456 assert(fp != NULL);
1457 (void) memmove(fp, fp + len + 1,
1458 cs->smultis - (fp + len + 1 - cs->multis));
1459 cs->smultis -= len;
1460
1461 if (cs->smultis == 0) {
1462 free(cs->multis);
1463 cs->multis = NULL;
1464 return;
1465 }
1466
1467 cs->multis = realloc(cs->multis, cs->smultis);
1468 assert(cs->multis != NULL);
Colin Cross4fa7b102010-01-12 18:59:25 -08001469}
1470
1471/*
Elliott Hughescc213f82012-08-14 15:32:42 -07001472 - mcin - is a collating element in a cset?
1473 == static int mcin(cset *cs, char *cp);
1474 */
1475static int
1476mcin(
1477 cset *cs,
1478 char *cp)
1479{
1480
1481 _DIAGASSERT(cs != NULL);
1482 _DIAGASSERT(cp != NULL);
1483
1484 return(mcfind(cs, cp) != NULL);
1485}
1486
1487/*
1488 - mcfind - find a collating element in a cset
1489 == static char *mcfind(cset *cs, char *cp);
1490 */
1491static char *
1492mcfind(
1493 cset *cs,
1494 char *cp)
1495{
1496 char *p;
1497
1498 _DIAGASSERT(cs != NULL);
1499 _DIAGASSERT(cp != NULL);
1500
1501 if (cs->multis == NULL)
1502 return(NULL);
1503 for (p = cs->multis; *p != '\0'; p += strlen(p) + 1)
1504 if (strcmp(cp, p) == 0)
1505 return(p);
1506 return(NULL);
1507}
1508#endif
1509
1510/*
Colin Cross4fa7b102010-01-12 18:59:25 -08001511 - mcinvert - invert the list of collating elements in a cset
Elliott Hughescc213f82012-08-14 15:32:42 -07001512 == static void mcinvert(struct parse *p, cset *cs);
Colin Cross4fa7b102010-01-12 18:59:25 -08001513 *
1514 * This would have to know the set of possibilities. Implementation
1515 * is deferred.
1516 */
1517/* ARGSUSED */
1518static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001519mcinvert(
1520 struct parse *p,
1521 cset *cs)
Colin Cross4fa7b102010-01-12 18:59:25 -08001522{
Elliott Hughescc213f82012-08-14 15:32:42 -07001523
1524 _DIAGASSERT(p != NULL);
1525 _DIAGASSERT(cs != NULL);
1526
Colin Cross4fa7b102010-01-12 18:59:25 -08001527 assert(cs->multis == NULL); /* xxx */
1528}
1529
1530/*
1531 - mccase - add case counterparts of the list of collating elements in a cset
Elliott Hughescc213f82012-08-14 15:32:42 -07001532 == static void mccase(struct parse *p, cset *cs);
Colin Cross4fa7b102010-01-12 18:59:25 -08001533 *
1534 * This would have to know the set of possibilities. Implementation
1535 * is deferred.
1536 */
1537/* ARGSUSED */
1538static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001539mccase(
1540 struct parse *p,
1541 cset *cs)
Colin Cross4fa7b102010-01-12 18:59:25 -08001542{
Elliott Hughescc213f82012-08-14 15:32:42 -07001543
1544 _DIAGASSERT(p != NULL);
1545 _DIAGASSERT(cs != NULL);
1546
Colin Cross4fa7b102010-01-12 18:59:25 -08001547 assert(cs->multis == NULL); /* xxx */
1548}
1549
1550/*
1551 - isinsets - is this character in any sets?
Elliott Hughescc213f82012-08-14 15:32:42 -07001552 == static int isinsets(struct re_guts *g, int c);
Colin Cross4fa7b102010-01-12 18:59:25 -08001553 */
1554static int /* predicate */
Elliott Hughescc213f82012-08-14 15:32:42 -07001555isinsets(
1556 struct re_guts *g,
1557 int c)
Colin Cross4fa7b102010-01-12 18:59:25 -08001558{
1559 uch *col;
Elliott Hughescc213f82012-08-14 15:32:42 -07001560 size_t i;
1561 size_t ncols;
1562 unsigned uc = (unsigned char)c;
1563
1564 _DIAGASSERT(g != NULL);
1565
1566 if (g->setbits == NULL)
1567 return 0;
1568
1569 ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
Colin Cross4fa7b102010-01-12 18:59:25 -08001570
1571 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1572 if (col[uc] != 0)
1573 return(1);
1574 return(0);
1575}
1576
1577/*
1578 - samesets - are these two characters in exactly the same sets?
Elliott Hughescc213f82012-08-14 15:32:42 -07001579 == static int samesets(struct re_guts *g, int c1, int c2);
Colin Cross4fa7b102010-01-12 18:59:25 -08001580 */
1581static int /* predicate */
Elliott Hughescc213f82012-08-14 15:32:42 -07001582samesets(
1583 struct re_guts *g,
1584 int c1,
1585 int c2)
Colin Cross4fa7b102010-01-12 18:59:25 -08001586{
1587 uch *col;
Elliott Hughescc213f82012-08-14 15:32:42 -07001588 size_t i;
1589 size_t ncols;
1590 unsigned uc1 = (unsigned char)c1;
1591 unsigned uc2 = (unsigned char)c2;
1592
1593 _DIAGASSERT(g != NULL);
1594
1595 ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
Colin Cross4fa7b102010-01-12 18:59:25 -08001596
1597 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1598 if (col[uc1] != col[uc2])
1599 return(0);
1600 return(1);
1601}
1602
1603/*
1604 - categorize - sort out character categories
Elliott Hughescc213f82012-08-14 15:32:42 -07001605 == static void categorize(struct parse *p, struct re_guts *g);
Colin Cross4fa7b102010-01-12 18:59:25 -08001606 */
1607static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001608categorize(
1609 struct parse *p,
1610 struct re_guts *g)
Colin Cross4fa7b102010-01-12 18:59:25 -08001611{
Elliott Hughescc213f82012-08-14 15:32:42 -07001612 cat_t *cats;
Colin Cross4fa7b102010-01-12 18:59:25 -08001613 int c;
1614 int c2;
1615 cat_t cat;
1616
Elliott Hughescc213f82012-08-14 15:32:42 -07001617 _DIAGASSERT(p != NULL);
1618 _DIAGASSERT(g != NULL);
1619
1620 cats = g->categories;
1621
Colin Cross4fa7b102010-01-12 18:59:25 -08001622 /* avoid making error situations worse */
1623 if (p->error != 0)
1624 return;
1625
1626 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1627 if (cats[c] == 0 && isinsets(g, c)) {
Elliott Hughescc213f82012-08-14 15:32:42 -07001628 _DIAGASSERT(__type_fit(unsigned char,
1629 g->ncategories + 1));
Colin Cross4fa7b102010-01-12 18:59:25 -08001630 cat = g->ncategories++;
1631 cats[c] = cat;
1632 for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1633 if (cats[c2] == 0 && samesets(g, c, c2))
1634 cats[c2] = cat;
1635 }
1636}
1637
1638/*
1639 - dupl - emit a duplicate of a bunch of sops
Elliott Hughescc213f82012-08-14 15:32:42 -07001640 == static sopno dupl(struct parse *p, sopno start, sopno finish);
Colin Cross4fa7b102010-01-12 18:59:25 -08001641 */
1642static sopno /* start of duplicate */
Elliott Hughescc213f82012-08-14 15:32:42 -07001643dupl(
1644 struct parse *p,
1645 sopno start, /* from here */
1646 sopno finish) /* to this less one */
Colin Cross4fa7b102010-01-12 18:59:25 -08001647{
Elliott Hughescc213f82012-08-14 15:32:42 -07001648 sopno ret;
Colin Cross4fa7b102010-01-12 18:59:25 -08001649 sopno len = finish - start;
1650
Elliott Hughescc213f82012-08-14 15:32:42 -07001651 _DIAGASSERT(p != NULL);
1652
1653 ret = HERE();
1654
Colin Cross4fa7b102010-01-12 18:59:25 -08001655 assert(finish >= start);
1656 if (len == 0)
1657 return(ret);
Elliott Hughescc213f82012-08-14 15:32:42 -07001658 if (!enlarge(p, p->ssize + len))/* this many unexpected additions */
1659 return ret;
1660 (void)memcpy(p->strip + p->slen, p->strip + start,
1661 (size_t)len * sizeof(sop));
Colin Cross4fa7b102010-01-12 18:59:25 -08001662 p->slen += len;
1663 return(ret);
1664}
1665
1666/*
1667 - doemit - emit a strip operator
Elliott Hughescc213f82012-08-14 15:32:42 -07001668 == static void doemit(struct parse *p, sop op, size_t opnd);
Colin Cross4fa7b102010-01-12 18:59:25 -08001669 *
1670 * It might seem better to implement this as a macro with a function as
1671 * hard-case backup, but it's just too big and messy unless there are
1672 * some changes to the data structures. Maybe later.
1673 */
1674static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001675doemit(
1676 struct parse *p,
1677 sop op,
1678 sopno opnd)
Colin Cross4fa7b102010-01-12 18:59:25 -08001679{
Elliott Hughescc213f82012-08-14 15:32:42 -07001680 _DIAGASSERT(p != NULL);
1681
Colin Cross4fa7b102010-01-12 18:59:25 -08001682 /* avoid making error situations worse */
1683 if (p->error != 0)
1684 return;
1685
1686 /* deal with oversize operands ("can't happen", more or less) */
1687 assert(opnd < 1<<OPSHIFT);
1688
1689 /* deal with undersized strip */
1690 if (p->slen >= p->ssize)
Elliott Hughescc213f82012-08-14 15:32:42 -07001691 if (!enlarge(p, (p->ssize+1) / 2 * 3)) /* +50% */
1692 return;
Colin Cross4fa7b102010-01-12 18:59:25 -08001693
1694 /* finally, it's all reduced to the easy case */
Elliott Hughescc213f82012-08-14 15:32:42 -07001695 p->strip[p->slen++] = (sop)SOP(op, opnd);
Colin Cross4fa7b102010-01-12 18:59:25 -08001696}
1697
1698/*
1699 - doinsert - insert a sop into the strip
Elliott Hughescc213f82012-08-14 15:32:42 -07001700 == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
Colin Cross4fa7b102010-01-12 18:59:25 -08001701 */
1702static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001703doinsert(
1704 struct parse *p,
1705 sop op,
1706 sopno opnd,
1707 sopno pos)
Colin Cross4fa7b102010-01-12 18:59:25 -08001708{
1709 sopno sn;
1710 sop s;
1711 int i;
1712
Elliott Hughescc213f82012-08-14 15:32:42 -07001713 _DIAGASSERT(p != NULL);
1714
Colin Cross4fa7b102010-01-12 18:59:25 -08001715 /* avoid making error situations worse */
1716 if (p->error != 0)
1717 return;
1718
1719 sn = HERE();
1720 EMIT(op, opnd); /* do checks, ensure space */
1721 assert(HERE() == sn+1);
1722 s = p->strip[sn];
1723
1724 /* adjust paren pointers */
1725 assert(pos > 0);
1726 for (i = 1; i < NPAREN; i++) {
1727 if (p->pbegin[i] >= pos) {
1728 p->pbegin[i]++;
1729 }
1730 if (p->pend[i] >= pos) {
1731 p->pend[i]++;
1732 }
1733 }
1734
Elliott Hughescc213f82012-08-14 15:32:42 -07001735 memmove(&p->strip[pos+1], &p->strip[pos], (HERE()-pos-1)*sizeof(sop));
Colin Cross4fa7b102010-01-12 18:59:25 -08001736 p->strip[pos] = s;
1737}
1738
1739/*
1740 - dofwd - complete a forward reference
Elliott Hughescc213f82012-08-14 15:32:42 -07001741 == static void dofwd(struct parse *p, sopno pos, sop value);
Colin Cross4fa7b102010-01-12 18:59:25 -08001742 */
1743static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001744dofwd(
1745 struct parse *p,
1746 sopno pos,
1747 sopno value)
Colin Cross4fa7b102010-01-12 18:59:25 -08001748{
Elliott Hughescc213f82012-08-14 15:32:42 -07001749
1750 _DIAGASSERT(p != NULL);
1751
Colin Cross4fa7b102010-01-12 18:59:25 -08001752 /* avoid making error situations worse */
1753 if (p->error != 0)
1754 return;
1755
1756 assert(value < 1<<OPSHIFT);
Elliott Hughescc213f82012-08-14 15:32:42 -07001757 p->strip[pos] = (sop)(OP(p->strip[pos]) | value);
Colin Cross4fa7b102010-01-12 18:59:25 -08001758}
1759
1760/*
1761 - enlarge - enlarge the strip
Elliott Hughescc213f82012-08-14 15:32:42 -07001762 == static void enlarge(struct parse *p, sopno size);
Colin Cross4fa7b102010-01-12 18:59:25 -08001763 */
Elliott Hughescc213f82012-08-14 15:32:42 -07001764static int
Elliott Hughes71927a82015-09-14 18:45:52 -07001765enlarge(struct parse *p, sopno size)
Colin Cross4fa7b102010-01-12 18:59:25 -08001766{
Elliott Hughescc213f82012-08-14 15:32:42 -07001767 _DIAGASSERT(p != NULL);
Colin Cross4fa7b102010-01-12 18:59:25 -08001768
1769 if (p->ssize >= size)
Elliott Hughescc213f82012-08-14 15:32:42 -07001770 return 1;
Colin Cross4fa7b102010-01-12 18:59:25 -08001771
Elliott Hughes71927a82015-09-14 18:45:52 -07001772 if (MEMSIZE(p) > MEMLIMIT || reallocarr(&p->strip, size, sizeof(sop))) {
Colin Cross4fa7b102010-01-12 18:59:25 -08001773 SETERROR(REG_ESPACE);
Elliott Hughescc213f82012-08-14 15:32:42 -07001774 return 0;
Colin Cross4fa7b102010-01-12 18:59:25 -08001775 }
Elliott Hughes71927a82015-09-14 18:45:52 -07001776 p->ssize = size;
Elliott Hughescc213f82012-08-14 15:32:42 -07001777 return 1;
Colin Cross4fa7b102010-01-12 18:59:25 -08001778}
1779
1780/*
1781 - stripsnug - compact the strip
Elliott Hughescc213f82012-08-14 15:32:42 -07001782 == static void stripsnug(struct parse *p, struct re_guts *g);
Colin Cross4fa7b102010-01-12 18:59:25 -08001783 */
1784static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001785stripsnug(
1786 struct parse *p,
1787 struct re_guts *g)
Colin Cross4fa7b102010-01-12 18:59:25 -08001788{
Elliott Hughescc213f82012-08-14 15:32:42 -07001789
1790 _DIAGASSERT(p != NULL);
1791 _DIAGASSERT(g != NULL);
1792
Colin Cross4fa7b102010-01-12 18:59:25 -08001793 g->nstates = p->slen;
Elliott Hughes71927a82015-09-14 18:45:52 -07001794 g->strip = p->strip;
1795 reallocarr(&g->strip, p->slen, sizeof(sop));
1796 /* Ignore error as tries to free memory only. */
Colin Cross4fa7b102010-01-12 18:59:25 -08001797}
1798
1799/*
1800 - findmust - fill in must and mlen with longest mandatory literal string
Elliott Hughescc213f82012-08-14 15:32:42 -07001801 == static void findmust(struct parse *p, struct re_guts *g);
Colin Cross4fa7b102010-01-12 18:59:25 -08001802 *
1803 * This algorithm could do fancy things like analyzing the operands of |
1804 * for common subsequences. Someday. This code is simple and finds most
1805 * of the interesting cases.
1806 *
1807 * Note that must and mlen got initialized during setup.
1808 */
1809static void
Elliott Hughescc213f82012-08-14 15:32:42 -07001810findmust(
1811 struct parse *p,
1812 struct re_guts *g)
Colin Cross4fa7b102010-01-12 18:59:25 -08001813{
1814 sop *scan;
Elliott Hughescc213f82012-08-14 15:32:42 -07001815 sop *start = NULL;
1816 sop *newstart = NULL;
Colin Cross4fa7b102010-01-12 18:59:25 -08001817 sopno newlen;
1818 sop s;
1819 char *cp;
1820 sopno i;
1821
Elliott Hughescc213f82012-08-14 15:32:42 -07001822 _DIAGASSERT(p != NULL);
1823 _DIAGASSERT(g != NULL);
1824
Colin Cross4fa7b102010-01-12 18:59:25 -08001825 /* avoid making error situations worse */
1826 if (p->error != 0)
1827 return;
1828
1829 /* find the longest OCHAR sequence in strip */
1830 newlen = 0;
1831 scan = g->strip + 1;
1832 do {
1833 s = *scan++;
1834 switch (OP(s)) {
1835 case OCHAR: /* sequence member */
1836 if (newlen == 0) /* new sequence */
1837 newstart = scan - 1;
1838 newlen++;
1839 break;
1840 case OPLUS_: /* things that don't break one */
1841 case OLPAREN:
1842 case ORPAREN:
1843 break;
1844 case OQUEST_: /* things that must be skipped */
1845 case OCH_:
1846 scan--;
1847 do {
1848 scan += OPND(s);
1849 s = *scan;
1850 /* assert() interferes w debug printouts */
1851 if (OP(s) != O_QUEST && OP(s) != O_CH &&
1852 OP(s) != OOR2) {
1853 g->iflags |= BAD;
1854 return;
1855 }
1856 } while (OP(s) != O_QUEST && OP(s) != O_CH);
Elliott Hughescc213f82012-08-14 15:32:42 -07001857 /* FALLTHROUGH */
Colin Cross4fa7b102010-01-12 18:59:25 -08001858 default: /* things that break a sequence */
1859 if (newlen > g->mlen) { /* ends one */
1860 start = newstart;
1861 g->mlen = newlen;
1862 }
1863 newlen = 0;
1864 break;
1865 }
1866 } while (OP(s) != OEND);
1867
Elliott Hughescc213f82012-08-14 15:32:42 -07001868 if (start == NULL)
1869 g->mlen = 0;
1870
1871 if (g->mlen == 0) /* there isn't one */
Colin Cross4fa7b102010-01-12 18:59:25 -08001872 return;
1873
1874 /* turn it into a character string */
1875 g->must = malloc((size_t)g->mlen + 1);
1876 if (g->must == NULL) { /* argh; just forget it */
1877 g->mlen = 0;
1878 return;
1879 }
1880 cp = g->must;
1881 scan = start;
1882 for (i = g->mlen; i > 0; i--) {
1883 while (OP(s = *scan++) != OCHAR)
1884 continue;
1885 assert(cp < g->must + g->mlen);
1886 *cp++ = (char)OPND(s);
1887 }
1888 assert(cp == g->must + g->mlen);
1889 *cp++ = '\0'; /* just on general principles */
1890}
1891
1892/*
1893 - pluscount - count + nesting
Elliott Hughescc213f82012-08-14 15:32:42 -07001894 == static sopno pluscount(struct parse *p, struct re_guts *g);
Colin Cross4fa7b102010-01-12 18:59:25 -08001895 */
1896static sopno /* nesting depth */
Elliott Hughescc213f82012-08-14 15:32:42 -07001897pluscount(
1898 struct parse *p,
1899 struct re_guts *g)
Colin Cross4fa7b102010-01-12 18:59:25 -08001900{
1901 sop *scan;
1902 sop s;
1903 sopno plusnest = 0;
1904 sopno maxnest = 0;
1905
Elliott Hughescc213f82012-08-14 15:32:42 -07001906 _DIAGASSERT(p != NULL);
1907 _DIAGASSERT(g != NULL);
1908
Colin Cross4fa7b102010-01-12 18:59:25 -08001909 if (p->error != 0)
1910 return(0); /* there may not be an OEND */
1911
1912 scan = g->strip + 1;
1913 do {
1914 s = *scan++;
1915 switch (OP(s)) {
1916 case OPLUS_:
1917 plusnest++;
1918 break;
1919 case O_PLUS:
1920 if (plusnest > maxnest)
1921 maxnest = plusnest;
1922 plusnest--;
1923 break;
1924 }
1925 } while (OP(s) != OEND);
1926 if (plusnest != 0)
1927 g->iflags |= BAD;
1928 return(maxnest);
1929}