2024-08-03 14:12:16 -07:00
|
|
|
/* $OpenBSD: b.c,v 1.54 2024/08/03 21:12:16 millert Exp $ */
|
1996-07-04 13:34:34 -07:00
|
|
|
/****************************************************************
|
1997-08-25 09:17:07 -07:00
|
|
|
Copyright (C) Lucent Technologies 1997
|
1996-07-04 13:34:34 -07:00
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and
|
|
|
|
its documentation for any purpose and without fee is hereby
|
|
|
|
granted, provided that the above copyright notice appear in all
|
|
|
|
copies and that both that the copyright notice and this
|
|
|
|
permission notice and warranty disclaimer appear in supporting
|
1997-08-25 09:17:07 -07:00
|
|
|
documentation, and that the name Lucent Technologies or any of
|
|
|
|
its entities not be used in advertising or publicity pertaining
|
|
|
|
to distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
|
|
|
|
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
|
|
|
|
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
|
|
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
|
|
THIS SOFTWARE.
|
1996-07-04 13:34:34 -07:00
|
|
|
****************************************************************/
|
|
|
|
|
2008-10-06 13:38:33 -07:00
|
|
|
/* lasciate ogne speranza, voi ch'intrate. */
|
1996-07-04 13:34:34 -07:00
|
|
|
|
|
|
|
#define DEBUG
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2020-06-10 14:01:50 -07:00
|
|
|
#include <limits.h>
|
1996-07-04 13:34:34 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "awk.h"
|
2020-07-30 10:45:44 -07:00
|
|
|
#include "awkgram.tab.h"
|
1996-07-04 13:34:34 -07:00
|
|
|
|
|
|
|
#define MAXLIN 22
|
|
|
|
|
1999-04-20 10:31:25 -07:00
|
|
|
#define type(v) (v)->nobj /* badly overloaded here */
|
|
|
|
#define info(v) (v)->ntype /* badly overloaded here */
|
1996-07-04 13:34:34 -07:00
|
|
|
#define left(v) (v)->narg[0]
|
|
|
|
#define right(v) (v)->narg[1]
|
|
|
|
#define parent(v) (v)->nnext
|
|
|
|
|
|
|
|
#define LEAF case CCL: case NCCL: case CHAR: case DOT: case FINAL: case ALL:
|
2008-10-06 13:38:33 -07:00
|
|
|
#define ELEAF case EMPTYRE: /* empty string in regexp */
|
1996-07-04 13:34:34 -07:00
|
|
|
#define UNARY case STAR: case PLUS: case QUEST:
|
|
|
|
|
|
|
|
/* encoding in tree Nodes:
|
2008-10-06 13:38:33 -07:00
|
|
|
leaf (CCL, NCCL, CHAR, DOT, FINAL, ALL, EMPTYRE):
|
1996-07-04 13:34:34 -07:00
|
|
|
left is index, right contains value or pointer to value
|
|
|
|
unary (STAR, PLUS, QUEST): left is child, right is null
|
|
|
|
binary (CAT, OR): left and right are children
|
|
|
|
parent contains pointer to parent
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
int *setvec;
|
|
|
|
int *tmpset;
|
|
|
|
int maxsetvec = 0;
|
|
|
|
|
|
|
|
int rtok; /* next token in current re */
|
1997-04-07 08:59:53 -07:00
|
|
|
int rlxval;
|
2020-06-10 14:03:36 -07:00
|
|
|
static const uschar *rlxstr;
|
|
|
|
static const uschar *prestr; /* current position in current re */
|
|
|
|
static const uschar *lastre; /* origin of last re */
|
|
|
|
static const uschar *lastatom; /* origin of last Atom */
|
|
|
|
static const uschar *starttok;
|
|
|
|
static const uschar *basestr; /* starts with original, replaced during
|
2020-06-10 14:01:50 -07:00
|
|
|
repetition processing */
|
2020-06-10 14:03:36 -07:00
|
|
|
static const uschar *firstbasestr;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
1997-04-07 08:59:53 -07:00
|
|
|
static int setcnt;
|
|
|
|
static int poscnt;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
2020-06-10 14:03:36 -07:00
|
|
|
const char *patbeg;
|
1996-07-04 13:34:34 -07:00
|
|
|
int patlen;
|
|
|
|
|
2020-06-10 14:03:12 -07:00
|
|
|
#define NFA 128 /* cache this many dynamic fa's */
|
1996-07-04 13:34:34 -07:00
|
|
|
fa *fatab[NFA];
|
|
|
|
int nfatab = 0; /* entries in fatab */
|
|
|
|
|
2023-09-17 07:49:44 -07:00
|
|
|
/* utf-8 mechanism:
|
|
|
|
|
|
|
|
For most of Awk, utf-8 strings just "work", since they look like
|
|
|
|
null-terminated sequences of 8-bit bytes.
|
|
|
|
|
|
|
|
Functions like length(), index(), and substr() have to operate
|
|
|
|
in units of utf-8 characters. The u8_* functions in run.c
|
|
|
|
handle this.
|
|
|
|
|
|
|
|
Regular expressions are more complicated, since the basic
|
|
|
|
mechanism of the goto table used 8-bit byte indices into the
|
|
|
|
gototab entries to compute the next state. Unicode is a lot
|
|
|
|
bigger, so the gototab entries are now structs with a character
|
2023-11-25 08:31:33 -08:00
|
|
|
and a next state. These are sorted by code point and binary
|
|
|
|
searched.
|
2023-09-17 07:49:44 -07:00
|
|
|
|
|
|
|
Throughout the RE mechanism in b.c, utf-8 characters are
|
|
|
|
converted to their utf-32 value. This mostly shows up in
|
|
|
|
cclenter, which expands character class ranges like a-z and now
|
|
|
|
alpha-omega. The size of a gototab array is still about 256.
|
|
|
|
This should be dynamic, but for now things work ok for a single
|
|
|
|
code page of Unicode, which is the most likely case.
|
|
|
|
|
|
|
|
The code changes are localized in run.c and b.c. I have added a
|
|
|
|
handful of functions to somewhat better hide the implementation,
|
|
|
|
but a lot more could be done.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2023-11-25 08:31:33 -08:00
|
|
|
static int entry_cmp(const void *l, const void *r);
|
2023-09-17 07:49:44 -07:00
|
|
|
static int get_gototab(fa*, int, int);
|
|
|
|
static int set_gototab(fa*, int, int, int);
|
2023-11-25 08:31:33 -08:00
|
|
|
static void clear_gototab(fa*, int);
|
2023-09-17 07:49:44 -07:00
|
|
|
|
2020-06-10 14:03:12 -07:00
|
|
|
static int *
|
|
|
|
intalloc(size_t n, const char *f)
|
|
|
|
{
|
2020-12-09 12:00:11 -08:00
|
|
|
int *p = (int *) calloc(n, sizeof(int));
|
2020-06-10 14:03:12 -07:00
|
|
|
if (p == NULL)
|
|
|
|
overflo(f);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
allocsetvec(const char *f)
|
|
|
|
{
|
|
|
|
maxsetvec = MAXLIN;
|
2020-12-09 12:00:11 -08:00
|
|
|
setvec = (int *) reallocarray(setvec, maxsetvec, sizeof(*setvec));
|
|
|
|
tmpset = (int *) reallocarray(tmpset, maxsetvec, sizeof(*tmpset));
|
2020-06-10 14:03:12 -07:00
|
|
|
if (setvec == NULL || tmpset == NULL)
|
|
|
|
overflo(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
resizesetvec(const char *f)
|
|
|
|
{
|
2020-12-09 12:00:11 -08:00
|
|
|
setvec = (int *) reallocarray(setvec, maxsetvec, 4 * sizeof(*setvec));
|
|
|
|
tmpset = (int *) reallocarray(tmpset, maxsetvec, 4 * sizeof(*tmpset));
|
2020-06-10 14:03:12 -07:00
|
|
|
if (setvec == NULL || tmpset == NULL)
|
|
|
|
overflo(f);
|
|
|
|
maxsetvec *= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
resize_state(fa *f, int state)
|
|
|
|
{
|
2023-11-25 08:31:33 -08:00
|
|
|
gtt *p;
|
2020-12-09 12:00:11 -08:00
|
|
|
uschar *p2;
|
|
|
|
int **p3;
|
2020-06-10 14:03:12 -07:00
|
|
|
int i, new_count;
|
|
|
|
|
|
|
|
if (++state < f->state_count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
new_count = state + 10; /* needs to be tuned */
|
|
|
|
|
2023-11-25 08:31:33 -08:00
|
|
|
p = (gtt *) reallocarray(f->gototab, new_count, sizeof(gtt));
|
2020-06-10 14:03:12 -07:00
|
|
|
if (p == NULL)
|
|
|
|
goto out;
|
|
|
|
f->gototab = p;
|
|
|
|
|
2020-12-09 12:00:11 -08:00
|
|
|
p2 = (uschar *) reallocarray(f->out, new_count, sizeof(f->out[0]));
|
|
|
|
if (p2 == NULL)
|
2020-06-10 14:03:12 -07:00
|
|
|
goto out;
|
2020-12-09 12:00:11 -08:00
|
|
|
f->out = p2;
|
2020-06-10 14:03:12 -07:00
|
|
|
|
2020-12-09 12:00:11 -08:00
|
|
|
p3 = (int **) reallocarray(f->posns, new_count, sizeof(f->posns[0]));
|
|
|
|
if (p3 == NULL)
|
2020-06-10 14:03:12 -07:00
|
|
|
goto out;
|
2020-12-09 12:00:11 -08:00
|
|
|
f->posns = p3;
|
2020-06-10 14:03:12 -07:00
|
|
|
|
|
|
|
for (i = f->state_count; i < new_count; ++i) {
|
2023-11-25 08:31:33 -08:00
|
|
|
f->gototab[i].entries = (gtte *) calloc(NCHARS, sizeof(gtte));
|
|
|
|
if (f->gototab[i].entries == NULL)
|
2020-06-10 14:03:12 -07:00
|
|
|
goto out;
|
2023-11-25 08:31:33 -08:00
|
|
|
f->gototab[i].allocated = NCHARS;
|
|
|
|
f->gototab[i].inuse = 0;
|
|
|
|
f->out[i] = 0;
|
2020-06-10 14:03:12 -07:00
|
|
|
f->posns[i] = NULL;
|
|
|
|
}
|
|
|
|
f->state_count = new_count;
|
|
|
|
return;
|
|
|
|
out:
|
|
|
|
overflo(__func__);
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:03:56 -07:00
|
|
|
fa *makedfa(const char *s, bool anchor) /* returns dfa for reg expr s */
|
1996-07-04 13:34:34 -07:00
|
|
|
{
|
|
|
|
int i, use, nuse;
|
|
|
|
fa *pfa;
|
1999-04-18 10:06:29 -07:00
|
|
|
static int now = 1;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
2020-06-10 14:02:33 -07:00
|
|
|
if (setvec == NULL) { /* first time through any RE */
|
2020-06-10 14:03:12 -07:00
|
|
|
allocsetvec(__func__);
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
|
2020-06-10 14:03:56 -07:00
|
|
|
if (compile_time != RUNNING) /* a constant for sure */
|
1996-07-04 13:34:34 -07:00
|
|
|
return mkdfa(s, anchor);
|
|
|
|
for (i = 0; i < nfatab; i++) /* is it there already? */
|
|
|
|
if (fatab[i]->anchor == anchor
|
2002-12-19 13:24:28 -08:00
|
|
|
&& strcmp((const char *) fatab[i]->restr, s) == 0) {
|
1999-04-18 10:06:29 -07:00
|
|
|
fatab[i]->use = now++;
|
1996-07-04 13:34:34 -07:00
|
|
|
return fatab[i];
|
2001-09-07 17:12:40 -07:00
|
|
|
}
|
1996-07-04 13:34:34 -07:00
|
|
|
pfa = mkdfa(s, anchor);
|
|
|
|
if (nfatab < NFA) { /* room for another */
|
|
|
|
fatab[nfatab] = pfa;
|
1999-04-18 10:06:29 -07:00
|
|
|
fatab[nfatab]->use = now++;
|
1996-07-04 13:34:34 -07:00
|
|
|
nfatab++;
|
|
|
|
return pfa;
|
|
|
|
}
|
|
|
|
use = fatab[0]->use; /* replace least-recently used */
|
|
|
|
nuse = 0;
|
|
|
|
for (i = 1; i < nfatab; i++)
|
|
|
|
if (fatab[i]->use < use) {
|
|
|
|
use = fatab[i]->use;
|
|
|
|
nuse = i;
|
|
|
|
}
|
|
|
|
freefa(fatab[nuse]);
|
|
|
|
fatab[nuse] = pfa;
|
1999-04-18 10:06:29 -07:00
|
|
|
pfa->use = now++;
|
1996-07-04 13:34:34 -07:00
|
|
|
return pfa;
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:03:56 -07:00
|
|
|
fa *mkdfa(const char *s, bool anchor) /* does the real work of making a dfa */
|
2020-06-10 14:04:40 -07:00
|
|
|
/* anchor = true for anchored matches, else false */
|
1996-07-04 13:34:34 -07:00
|
|
|
{
|
|
|
|
Node *p, *p1;
|
|
|
|
fa *f;
|
|
|
|
|
2020-06-10 14:03:36 -07:00
|
|
|
firstbasestr = (const uschar *) s;
|
2020-06-10 14:01:50 -07:00
|
|
|
basestr = firstbasestr;
|
1996-07-04 13:34:34 -07:00
|
|
|
p = reparse(s);
|
|
|
|
p1 = op2(CAT, op2(STAR, op2(ALL, NIL, NIL), NIL), p);
|
|
|
|
/* put ALL STAR in front of reg. exp. */
|
|
|
|
p1 = op2(CAT, p1, op2(FINAL, NIL, NIL));
|
|
|
|
/* put FINAL after reg. exp. */
|
|
|
|
|
|
|
|
poscnt = 0;
|
|
|
|
penter(p1); /* enter parent pointers and leaf indices */
|
2020-12-09 12:00:11 -08:00
|
|
|
if ((f = (fa *) calloc(1, sizeof(fa) + poscnt * sizeof(rrow))) == NULL)
|
2020-06-10 14:03:12 -07:00
|
|
|
overflo(__func__);
|
1996-07-04 13:34:34 -07:00
|
|
|
f->accept = poscnt-1; /* penter has computed number of positions in re */
|
|
|
|
cfoll(f, p1); /* set up follow sets */
|
|
|
|
freetr(p1);
|
2020-06-10 14:03:12 -07:00
|
|
|
resize_state(f, 1);
|
|
|
|
f->posns[0] = intalloc(*(f->re[0].lfollow), __func__);
|
|
|
|
f->posns[1] = intalloc(1, __func__);
|
1996-07-04 13:34:34 -07:00
|
|
|
*f->posns[1] = 0;
|
|
|
|
f->initstat = makeinit(f, anchor);
|
|
|
|
f->anchor = anchor;
|
2001-09-07 17:12:40 -07:00
|
|
|
f->restr = (uschar *) tostring(s);
|
2020-06-10 14:01:50 -07:00
|
|
|
if (firstbasestr != basestr) {
|
|
|
|
if (basestr)
|
|
|
|
xfree(basestr);
|
|
|
|
}
|
1996-07-04 13:34:34 -07:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:03:56 -07:00
|
|
|
int makeinit(fa *f, bool anchor)
|
1996-07-04 13:34:34 -07:00
|
|
|
{
|
|
|
|
int i, k;
|
|
|
|
|
|
|
|
f->curstat = 2;
|
|
|
|
f->out[2] = 0;
|
|
|
|
k = *(f->re[0].lfollow);
|
2020-06-10 14:02:33 -07:00
|
|
|
xfree(f->posns[2]);
|
2020-06-10 14:03:12 -07:00
|
|
|
f->posns[2] = intalloc(k + 1, __func__);
|
2020-06-10 14:04:40 -07:00
|
|
|
for (i = 0; i <= k; i++) {
|
1996-07-04 13:34:34 -07:00
|
|
|
(f->posns[2])[i] = (f->re[0].lfollow)[i];
|
|
|
|
}
|
|
|
|
if ((f->posns[2])[1] == f->accept)
|
|
|
|
f->out[2] = 1;
|
2023-11-25 08:31:33 -08:00
|
|
|
clear_gototab(f, 2);
|
1996-07-04 13:34:34 -07:00
|
|
|
f->curstat = cgoto(f, 2, HAT);
|
|
|
|
if (anchor) {
|
|
|
|
*f->posns[2] = k-1; /* leave out position 0 */
|
2020-06-10 14:04:40 -07:00
|
|
|
for (i = 0; i < k; i++) {
|
1996-07-04 13:34:34 -07:00
|
|
|
(f->posns[0])[i] = (f->posns[2])[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
f->out[0] = f->out[2];
|
|
|
|
if (f->curstat != 2)
|
|
|
|
--(*f->posns[f->curstat]);
|
|
|
|
}
|
|
|
|
return f->curstat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void penter(Node *p) /* set up parent pointers and leaf indices */
|
|
|
|
{
|
|
|
|
switch (type(p)) {
|
2008-10-06 13:38:33 -07:00
|
|
|
ELEAF
|
1996-07-04 13:34:34 -07:00
|
|
|
LEAF
|
1999-04-20 10:31:25 -07:00
|
|
|
info(p) = poscnt;
|
1996-07-04 13:34:34 -07:00
|
|
|
poscnt++;
|
|
|
|
break;
|
|
|
|
UNARY
|
|
|
|
penter(left(p));
|
|
|
|
parent(left(p)) = p;
|
|
|
|
break;
|
|
|
|
case CAT:
|
|
|
|
case OR:
|
|
|
|
penter(left(p));
|
|
|
|
penter(right(p));
|
|
|
|
parent(left(p)) = p;
|
|
|
|
parent(right(p)) = p;
|
|
|
|
break;
|
2020-06-10 14:05:02 -07:00
|
|
|
case ZERO:
|
|
|
|
break;
|
1996-07-04 13:34:34 -07:00
|
|
|
default: /* can't happen */
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("can't happen: unknown type %d in penter", type(p));
|
1996-07-04 13:34:34 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void freetr(Node *p) /* free parse tree */
|
|
|
|
{
|
|
|
|
switch (type(p)) {
|
2008-10-06 13:38:33 -07:00
|
|
|
ELEAF
|
1996-07-04 13:34:34 -07:00
|
|
|
LEAF
|
|
|
|
xfree(p);
|
|
|
|
break;
|
|
|
|
UNARY
|
2020-06-10 14:05:02 -07:00
|
|
|
case ZERO:
|
1996-07-04 13:34:34 -07:00
|
|
|
freetr(left(p));
|
|
|
|
xfree(p);
|
|
|
|
break;
|
|
|
|
case CAT:
|
|
|
|
case OR:
|
|
|
|
freetr(left(p));
|
|
|
|
freetr(right(p));
|
|
|
|
xfree(p);
|
|
|
|
break;
|
|
|
|
default: /* can't happen */
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("can't happen: unknown type %d in freetr", type(p));
|
1996-07-04 13:34:34 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* in the parsing of regular expressions, metacharacters like . have */
|
|
|
|
/* to be seen literally; \056 is not a metacharacter. */
|
|
|
|
|
2024-06-02 17:55:05 -07:00
|
|
|
static int
|
|
|
|
hexstr(const uschar **pp, int max) /* find and eval hex string at pp, return new p */
|
1997-01-20 11:43:15 -08:00
|
|
|
{ /* only pick up one 8-bit byte (2 chars) */
|
2020-06-10 14:03:36 -07:00
|
|
|
const uschar *p;
|
1996-07-04 13:34:34 -07:00
|
|
|
int n = 0;
|
1997-01-20 11:43:15 -08:00
|
|
|
int i;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
2023-09-17 07:49:44 -07:00
|
|
|
for (i = 0, p = *pp; i < max && isxdigit(*p); i++, p++) {
|
1996-07-04 13:34:34 -07:00
|
|
|
if (isdigit(*p))
|
|
|
|
n = 16 * n + *p - '0';
|
|
|
|
else if (*p >= 'a' && *p <= 'f')
|
|
|
|
n = 16 * n + *p - 'a' + 10;
|
|
|
|
else if (*p >= 'A' && *p <= 'F')
|
|
|
|
n = 16 * n + *p - 'A' + 10;
|
|
|
|
}
|
2020-06-10 14:03:36 -07:00
|
|
|
*pp = p;
|
1996-07-04 13:34:34 -07:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
1997-01-20 11:43:15 -08:00
|
|
|
#define isoctdigit(c) ((c) >= '0' && (c) <= '7') /* multiple use of arg */
|
1996-07-04 13:34:34 -07:00
|
|
|
|
2020-06-10 14:03:36 -07:00
|
|
|
int quoted(const uschar **pp) /* pick up next thing after a \\ */
|
1996-07-04 13:34:34 -07:00
|
|
|
/* and increment *pp */
|
|
|
|
{
|
2020-06-10 14:03:36 -07:00
|
|
|
const uschar *p = *pp;
|
1996-07-04 13:34:34 -07:00
|
|
|
int c;
|
|
|
|
|
2023-09-17 07:49:44 -07:00
|
|
|
/* BUG: should advance by utf-8 char even if makes no sense */
|
|
|
|
|
2024-08-03 14:12:16 -07:00
|
|
|
switch ((c = *p++)) {
|
|
|
|
case 't':
|
1996-07-04 13:34:34 -07:00
|
|
|
c = '\t';
|
2024-08-03 14:12:16 -07:00
|
|
|
break;
|
|
|
|
case 'n':
|
1996-07-04 13:34:34 -07:00
|
|
|
c = '\n';
|
2024-08-03 14:12:16 -07:00
|
|
|
break;
|
|
|
|
case 'f':
|
1996-07-04 13:34:34 -07:00
|
|
|
c = '\f';
|
2024-08-03 14:12:16 -07:00
|
|
|
break;
|
|
|
|
case 'r':
|
1996-07-04 13:34:34 -07:00
|
|
|
c = '\r';
|
2024-08-03 14:12:16 -07:00
|
|
|
break;
|
|
|
|
case 'b':
|
1996-07-04 13:34:34 -07:00
|
|
|
c = '\b';
|
2024-08-03 14:12:16 -07:00
|
|
|
break;
|
|
|
|
case 'v':
|
2020-06-10 14:02:33 -07:00
|
|
|
c = '\v';
|
2024-08-03 14:12:16 -07:00
|
|
|
break;
|
|
|
|
case 'a':
|
2020-06-10 14:02:33 -07:00
|
|
|
c = '\a';
|
2024-08-03 14:12:16 -07:00
|
|
|
break;
|
|
|
|
case '\\':
|
1996-07-04 13:34:34 -07:00
|
|
|
c = '\\';
|
2024-08-03 14:12:16 -07:00
|
|
|
break;
|
|
|
|
case 'x': /* 2 hex digits follow */
|
|
|
|
c = hexstr(&p, 2); /* this adds a null if number is invalid */
|
|
|
|
break;
|
|
|
|
case 'u': /* unicode char number up to 8 hex digits */
|
2023-09-17 07:49:44 -07:00
|
|
|
c = hexstr(&p, 8);
|
2024-08-03 14:12:16 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (isoctdigit(c)) { /* \d \dd \ddd */
|
|
|
|
int n = c - '0';
|
|
|
|
if (isoctdigit(*p)) {
|
1996-07-04 13:34:34 -07:00
|
|
|
n = 8 * n + *p++ - '0';
|
2024-08-03 14:12:16 -07:00
|
|
|
if (isoctdigit(*p))
|
|
|
|
n = 8 * n + *p++ - '0';
|
|
|
|
}
|
|
|
|
c = n;
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
2024-08-03 14:12:16 -07:00
|
|
|
}
|
|
|
|
|
1996-07-04 13:34:34 -07:00
|
|
|
*pp = p;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2023-09-17 07:49:44 -07:00
|
|
|
int *cclenter(const char *argp) /* add a character class */
|
1996-07-04 13:34:34 -07:00
|
|
|
{
|
|
|
|
int i, c, c2;
|
2023-09-17 07:49:44 -07:00
|
|
|
int n;
|
|
|
|
const uschar *p = (const uschar *) argp;
|
|
|
|
int *bp, *retp;
|
|
|
|
static int *buf = NULL;
|
1997-08-25 09:17:07 -07:00
|
|
|
static int bufsz = 100;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
2023-09-17 07:49:44 -07:00
|
|
|
if (buf == NULL && (buf = (int *) calloc(bufsz, sizeof(int))) == NULL)
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("out of space for character class [%.10s...] 1", p);
|
1997-08-25 09:17:07 -07:00
|
|
|
bp = buf;
|
2023-09-17 07:49:44 -07:00
|
|
|
for (i = 0; *p != 0; ) {
|
2024-01-25 08:40:51 -08:00
|
|
|
n = u8_rune(&c, (const char *) p);
|
2023-09-17 07:49:44 -07:00
|
|
|
p += n;
|
1996-07-04 13:34:34 -07:00
|
|
|
if (c == '\\') {
|
2011-09-28 12:27:18 -07:00
|
|
|
c = quoted(&p);
|
1997-08-25 09:17:07 -07:00
|
|
|
} else if (c == '-' && i > 0 && bp[-1] != 0) {
|
1996-07-04 13:34:34 -07:00
|
|
|
if (*p != 0) {
|
1997-08-25 09:17:07 -07:00
|
|
|
c = bp[-1];
|
2023-09-17 07:49:44 -07:00
|
|
|
/* c2 = *p++; */
|
2024-01-25 08:40:51 -08:00
|
|
|
n = u8_rune(&c2, (const char *) p);
|
2023-09-17 07:49:44 -07:00
|
|
|
p += n;
|
1996-07-04 13:34:34 -07:00
|
|
|
if (c2 == '\\')
|
2023-09-17 07:49:44 -07:00
|
|
|
c2 = quoted(&p); /* BUG: sets p, has to be u8 size */
|
1996-07-04 13:34:34 -07:00
|
|
|
if (c > c2) { /* empty; ignore */
|
1997-08-25 09:17:07 -07:00
|
|
|
bp--;
|
1996-07-04 13:34:34 -07:00
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (c < c2) {
|
2023-09-17 07:49:44 -07:00
|
|
|
if (i >= bufsz) {
|
2023-10-30 10:52:54 -07:00
|
|
|
buf = (int *) reallocarray(buf, bufsz, 2 * sizeof(int));
|
2023-09-17 07:49:44 -07:00
|
|
|
if (buf == NULL)
|
|
|
|
FATAL("out of space for character class [%.10s...] 2", p);
|
|
|
|
bufsz *= 2;
|
|
|
|
bp = buf + i;
|
|
|
|
}
|
1997-08-25 09:17:07 -07:00
|
|
|
*bp++ = ++c;
|
1996-07-04 13:34:34 -07:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-09-17 07:49:44 -07:00
|
|
|
if (i >= bufsz) {
|
2023-10-30 10:52:54 -07:00
|
|
|
buf = (int *) reallocarray(buf, bufsz, 2 * sizeof(int));
|
2023-09-17 07:49:44 -07:00
|
|
|
if (buf == NULL)
|
|
|
|
FATAL("out of space for character class [%.10s...] 2", p);
|
|
|
|
bufsz *= 2;
|
|
|
|
bp = buf + i;
|
|
|
|
}
|
1997-08-25 09:17:07 -07:00
|
|
|
*bp++ = c;
|
1996-07-04 13:34:34 -07:00
|
|
|
i++;
|
|
|
|
}
|
1997-08-25 09:17:07 -07:00
|
|
|
*bp = 0;
|
2023-09-17 07:49:44 -07:00
|
|
|
/* DPRINTF("cclenter: in = |%s|, out = |%s|\n", op, buf); BUG: can't print array of int */
|
|
|
|
/* xfree(op); BUG: what are we freeing here? */
|
|
|
|
retp = (int *) calloc(bp-buf+1, sizeof(int));
|
|
|
|
for (i = 0; i < bp-buf+1; i++)
|
|
|
|
retp[i] = buf[i];
|
|
|
|
return retp;
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
|
2002-12-19 13:24:28 -08:00
|
|
|
void overflo(const char *s)
|
1996-07-04 13:34:34 -07:00
|
|
|
{
|
2020-06-10 14:03:12 -07:00
|
|
|
FATAL("regular expression too big: out of space in %.30s...", s);
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void cfoll(fa *f, Node *v) /* enter follow set of each leaf of vertex v into lfollow[leaf] */
|
|
|
|
{
|
|
|
|
int i;
|
1997-04-07 08:59:53 -07:00
|
|
|
int *p;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
|
|
|
switch (type(v)) {
|
2008-10-06 13:38:33 -07:00
|
|
|
ELEAF
|
1996-07-04 13:34:34 -07:00
|
|
|
LEAF
|
1999-04-20 10:31:25 -07:00
|
|
|
f->re[info(v)].ltype = type(v);
|
|
|
|
f->re[info(v)].lval.np = right(v);
|
1996-07-04 13:34:34 -07:00
|
|
|
while (f->accept >= maxsetvec) { /* guessing here! */
|
2020-06-10 14:03:12 -07:00
|
|
|
resizesetvec(__func__);
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
for (i = 0; i <= f->accept; i++)
|
|
|
|
setvec[i] = 0;
|
|
|
|
setcnt = 0;
|
|
|
|
follow(v); /* computes setvec and setcnt */
|
2020-06-10 14:03:12 -07:00
|
|
|
p = intalloc(setcnt + 1, __func__);
|
1999-04-20 10:31:25 -07:00
|
|
|
f->re[info(v)].lfollow = p;
|
1996-07-04 13:34:34 -07:00
|
|
|
*p = setcnt;
|
|
|
|
for (i = f->accept; i >= 0; i--)
|
|
|
|
if (setvec[i] == 1)
|
|
|
|
*++p = i;
|
|
|
|
break;
|
|
|
|
UNARY
|
|
|
|
cfoll(f,left(v));
|
|
|
|
break;
|
|
|
|
case CAT:
|
|
|
|
case OR:
|
|
|
|
cfoll(f,left(v));
|
|
|
|
cfoll(f,right(v));
|
|
|
|
break;
|
2020-06-10 14:05:02 -07:00
|
|
|
case ZERO:
|
|
|
|
break;
|
1996-07-04 13:34:34 -07:00
|
|
|
default: /* can't happen */
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("can't happen: unknown type %d in cfoll", type(v));
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int first(Node *p) /* collects initially active leaves of p into setvec */
|
2008-10-06 13:38:33 -07:00
|
|
|
/* returns 0 if p matches empty string */
|
1996-07-04 13:34:34 -07:00
|
|
|
{
|
1997-04-07 08:59:53 -07:00
|
|
|
int b, lp;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
|
|
|
switch (type(p)) {
|
2008-10-06 13:38:33 -07:00
|
|
|
ELEAF
|
1996-07-04 13:34:34 -07:00
|
|
|
LEAF
|
1999-04-20 10:31:25 -07:00
|
|
|
lp = info(p); /* look for high-water mark of subscripts */
|
1996-07-04 13:34:34 -07:00
|
|
|
while (setcnt >= maxsetvec || lp >= maxsetvec) { /* guessing here! */
|
2020-06-10 14:03:12 -07:00
|
|
|
resizesetvec(__func__);
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
2008-10-06 13:38:33 -07:00
|
|
|
if (type(p) == EMPTYRE) {
|
|
|
|
setvec[lp] = 0;
|
|
|
|
return(0);
|
|
|
|
}
|
1996-07-04 13:34:34 -07:00
|
|
|
if (setvec[lp] != 1) {
|
|
|
|
setvec[lp] = 1;
|
|
|
|
setcnt++;
|
|
|
|
}
|
2023-09-18 18:14:05 -07:00
|
|
|
if (type(p) == CCL && (*(int *) right(p)) == 0)
|
1996-07-04 13:34:34 -07:00
|
|
|
return(0); /* empty CCL */
|
2020-06-10 14:04:40 -07:00
|
|
|
return(1);
|
1996-07-04 13:34:34 -07:00
|
|
|
case PLUS:
|
2020-06-10 14:04:40 -07:00
|
|
|
if (first(left(p)) == 0)
|
|
|
|
return(0);
|
1996-07-04 13:34:34 -07:00
|
|
|
return(1);
|
|
|
|
case STAR:
|
|
|
|
case QUEST:
|
|
|
|
first(left(p));
|
|
|
|
return(0);
|
|
|
|
case CAT:
|
|
|
|
if (first(left(p)) == 0 && first(right(p)) == 0) return(0);
|
|
|
|
return(1);
|
|
|
|
case OR:
|
|
|
|
b = first(right(p));
|
|
|
|
if (first(left(p)) == 0 || b == 0) return(0);
|
|
|
|
return(1);
|
2020-06-10 14:05:02 -07:00
|
|
|
case ZERO:
|
|
|
|
return 0;
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("can't happen: unknown type %d in first", type(p)); /* can't happen */
|
1996-07-04 13:34:34 -07:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void follow(Node *v) /* collects leaves that can follow v into setvec */
|
|
|
|
{
|
|
|
|
Node *p;
|
|
|
|
|
|
|
|
if (type(v) == FINAL)
|
|
|
|
return;
|
|
|
|
p = parent(v);
|
|
|
|
switch (type(p)) {
|
|
|
|
case STAR:
|
|
|
|
case PLUS:
|
|
|
|
first(v);
|
|
|
|
follow(p);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case OR:
|
|
|
|
case QUEST:
|
|
|
|
follow(p);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CAT:
|
|
|
|
if (v == left(p)) { /* v is left child of p */
|
|
|
|
if (first(right(p)) == 0) {
|
|
|
|
follow(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else /* v is right child */
|
|
|
|
follow(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 07:49:44 -07:00
|
|
|
int member(int c, int *sarg) /* is c in s? */
|
1996-07-04 13:34:34 -07:00
|
|
|
{
|
2023-09-17 07:49:44 -07:00
|
|
|
int *s = (int *) sarg;
|
2001-09-07 17:12:40 -07:00
|
|
|
|
1996-07-04 13:34:34 -07:00
|
|
|
while (*s)
|
|
|
|
if (c == *s++)
|
|
|
|
return(1);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2023-11-25 08:31:33 -08:00
|
|
|
static void resize_gototab(fa *f, int state)
|
|
|
|
{
|
|
|
|
size_t new_size = f->gototab[state].allocated * 2;
|
|
|
|
gtte *p = (gtte *) realloc(f->gototab[state].entries, new_size * sizeof(gtte));
|
|
|
|
if (p == NULL)
|
|
|
|
overflo(__func__);
|
|
|
|
|
|
|
|
// need to initialized the new memory to zero
|
|
|
|
size_t orig_size = f->gototab[state].allocated; // 2nd half of new mem is this size
|
|
|
|
memset(p + orig_size, 0, orig_size * sizeof(gtte)); // clean it out
|
|
|
|
|
2024-04-25 11:33:53 -07:00
|
|
|
f->gototab[state].allocated = new_size; // update gototab info
|
2023-11-25 08:31:33 -08:00
|
|
|
f->gototab[state].entries = p;
|
|
|
|
}
|
|
|
|
|
2024-04-25 11:33:53 -07:00
|
|
|
static int get_gototab(fa *f, int state, int ch) /* hide gototab implementation */
|
2023-09-17 07:49:44 -07:00
|
|
|
{
|
2023-11-25 08:31:33 -08:00
|
|
|
gtte key;
|
|
|
|
gtte *item;
|
|
|
|
|
|
|
|
key.ch = ch;
|
|
|
|
key.state = 0; /* irrelevant */
|
2024-01-25 08:40:51 -08:00
|
|
|
item = (gtte *) bsearch(& key, f->gototab[state].entries,
|
2023-11-25 08:31:33 -08:00
|
|
|
f->gototab[state].inuse, sizeof(gtte),
|
|
|
|
entry_cmp);
|
|
|
|
|
|
|
|
if (item == NULL)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return item->state;
|
2023-09-17 07:49:44 -07:00
|
|
|
}
|
|
|
|
|
2023-11-25 08:31:33 -08:00
|
|
|
static int entry_cmp(const void *l, const void *r)
|
2023-10-06 15:31:21 -07:00
|
|
|
{
|
2023-11-25 08:31:33 -08:00
|
|
|
const gtte *left, *right;
|
|
|
|
|
|
|
|
left = (const gtte *) l;
|
|
|
|
right = (const gtte *) r;
|
|
|
|
|
|
|
|
return left->ch - right->ch;
|
2023-10-06 15:31:21 -07:00
|
|
|
}
|
|
|
|
|
2024-04-25 11:33:53 -07:00
|
|
|
static int set_gototab(fa *f, int state, int ch, int val) /* hide gototab implementation */
|
2023-09-17 07:49:44 -07:00
|
|
|
{
|
2023-11-25 08:31:33 -08:00
|
|
|
if (f->gototab[state].inuse == 0) {
|
|
|
|
f->gototab[state].entries[0].ch = ch;
|
|
|
|
f->gototab[state].entries[0].state = val;
|
|
|
|
f->gototab[state].inuse++;
|
|
|
|
return val;
|
2024-05-04 15:59:21 -07:00
|
|
|
} else if ((unsigned)ch > f->gototab[state].entries[f->gototab[state].inuse-1].ch) {
|
2023-11-25 08:31:33 -08:00
|
|
|
// not seen yet, insert and return
|
|
|
|
gtt *tab = & f->gototab[state];
|
|
|
|
if (tab->inuse + 1 >= tab->allocated)
|
|
|
|
resize_gototab(f, state);
|
|
|
|
|
2024-04-25 11:33:53 -07:00
|
|
|
f->gototab[state].entries[f->gototab[state].inuse].ch = ch;
|
|
|
|
f->gototab[state].entries[f->gototab[state].inuse].state = val;
|
2023-11-25 08:31:33 -08:00
|
|
|
f->gototab[state].inuse++;
|
|
|
|
return val;
|
|
|
|
} else {
|
|
|
|
// maybe we have it, maybe we don't
|
|
|
|
gtte key;
|
|
|
|
gtte *item;
|
|
|
|
|
|
|
|
key.ch = ch;
|
|
|
|
key.state = 0; /* irrelevant */
|
2024-01-25 08:40:51 -08:00
|
|
|
item = (gtte *) bsearch(& key, f->gototab[state].entries,
|
2023-11-25 08:31:33 -08:00
|
|
|
f->gototab[state].inuse, sizeof(gtte),
|
|
|
|
entry_cmp);
|
|
|
|
|
|
|
|
if (item != NULL) {
|
|
|
|
// we have it, update state and return
|
|
|
|
item->state = val;
|
|
|
|
return item->state;
|
2023-09-17 07:49:44 -07:00
|
|
|
}
|
2023-11-25 08:31:33 -08:00
|
|
|
// otherwise, fall through to insert and reallocate.
|
2023-09-17 07:49:44 -07:00
|
|
|
}
|
2023-11-25 08:31:33 -08:00
|
|
|
|
|
|
|
gtt *tab = & f->gototab[state];
|
|
|
|
if (tab->inuse + 1 >= tab->allocated)
|
|
|
|
resize_gototab(f, state);
|
|
|
|
f->gototab[state].entries[tab->inuse].ch = ch;
|
|
|
|
f->gototab[state].entries[tab->inuse].state = val;
|
2024-04-25 11:33:53 -07:00
|
|
|
++tab->inuse;
|
2023-11-25 08:31:33 -08:00
|
|
|
|
|
|
|
qsort(f->gototab[state].entries,
|
|
|
|
f->gototab[state].inuse, sizeof(gtte), entry_cmp);
|
|
|
|
|
2023-09-17 07:49:44 -07:00
|
|
|
return val; /* not used anywhere at the moment */
|
|
|
|
}
|
|
|
|
|
2023-11-25 08:31:33 -08:00
|
|
|
static void clear_gototab(fa *f, int state)
|
|
|
|
{
|
|
|
|
memset(f->gototab[state].entries, 0,
|
|
|
|
f->gototab[state].allocated * sizeof(gtte));
|
|
|
|
f->gototab[state].inuse = 0;
|
|
|
|
}
|
|
|
|
|
2002-12-19 13:24:28 -08:00
|
|
|
int match(fa *f, const char *p0) /* shortest match ? */
|
1996-07-04 13:34:34 -07:00
|
|
|
{
|
|
|
|
int s, ns;
|
2023-09-17 07:49:44 -07:00
|
|
|
int n;
|
|
|
|
int rune;
|
2020-06-10 14:03:36 -07:00
|
|
|
const uschar *p = (const uschar *) p0;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
2023-09-17 07:49:44 -07:00
|
|
|
/* return pmatch(f, p0); does it matter whether longest or shortest? */
|
|
|
|
|
2020-06-10 14:03:12 -07:00
|
|
|
s = f->initstat;
|
|
|
|
assert (s < f->state_count);
|
|
|
|
|
1996-07-04 13:34:34 -07:00
|
|
|
if (f->out[s])
|
|
|
|
return(1);
|
|
|
|
do {
|
2008-10-06 13:38:33 -07:00
|
|
|
/* assert(*p < NCHARS); */
|
2024-01-25 08:40:51 -08:00
|
|
|
n = u8_rune(&rune, (const char *) p);
|
2023-09-17 07:49:44 -07:00
|
|
|
if ((ns = get_gototab(f, s, rune)) != 0)
|
1996-07-04 13:34:34 -07:00
|
|
|
s = ns;
|
|
|
|
else
|
2023-09-17 07:49:44 -07:00
|
|
|
s = cgoto(f, s, rune);
|
1996-07-04 13:34:34 -07:00
|
|
|
if (f->out[s])
|
|
|
|
return(1);
|
2023-09-17 07:49:44 -07:00
|
|
|
if (*p == 0)
|
|
|
|
break;
|
|
|
|
p += n;
|
|
|
|
} while (1); /* was *p++ != 0 */
|
1996-07-04 13:34:34 -07:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2002-12-19 13:24:28 -08:00
|
|
|
int pmatch(fa *f, const char *p0) /* longest match, for sub */
|
1996-07-04 13:34:34 -07:00
|
|
|
{
|
|
|
|
int s, ns;
|
2023-09-17 07:49:44 -07:00
|
|
|
int n;
|
|
|
|
int rune;
|
2020-06-10 14:03:36 -07:00
|
|
|
const uschar *p = (const uschar *) p0;
|
|
|
|
const uschar *q;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
2020-06-10 14:03:12 -07:00
|
|
|
s = f->initstat;
|
|
|
|
assert(s < f->state_count);
|
|
|
|
|
2020-06-10 14:03:36 -07:00
|
|
|
patbeg = (const char *)p;
|
1996-07-04 13:34:34 -07:00
|
|
|
patlen = -1;
|
|
|
|
do {
|
|
|
|
q = p;
|
|
|
|
do {
|
|
|
|
if (f->out[s]) /* final state */
|
|
|
|
patlen = q-p;
|
2008-10-06 13:38:33 -07:00
|
|
|
/* assert(*q < NCHARS); */
|
2024-01-25 08:40:51 -08:00
|
|
|
n = u8_rune(&rune, (const char *) q);
|
2023-09-17 07:49:44 -07:00
|
|
|
if ((ns = get_gototab(f, s, rune)) != 0)
|
1996-07-04 13:34:34 -07:00
|
|
|
s = ns;
|
|
|
|
else
|
2023-09-17 07:49:44 -07:00
|
|
|
s = cgoto(f, s, rune);
|
2020-06-10 14:03:12 -07:00
|
|
|
|
|
|
|
assert(s < f->state_count);
|
|
|
|
|
2001-07-11 22:16:53 -07:00
|
|
|
if (s == 1) { /* no transition */
|
1996-07-04 13:34:34 -07:00
|
|
|
if (patlen >= 0) {
|
2020-06-10 14:03:36 -07:00
|
|
|
patbeg = (const char *) p;
|
1996-07-04 13:34:34 -07:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto nextin; /* no match */
|
2001-07-11 22:16:53 -07:00
|
|
|
}
|
2023-09-17 07:49:44 -07:00
|
|
|
if (*q == 0)
|
|
|
|
break;
|
|
|
|
q += n;
|
|
|
|
} while (1);
|
|
|
|
q++; /* was *q++ */
|
1996-07-04 13:34:34 -07:00
|
|
|
if (f->out[s])
|
|
|
|
patlen = q-p-1; /* don't count $ */
|
|
|
|
if (patlen >= 0) {
|
2020-06-10 14:03:36 -07:00
|
|
|
patbeg = (const char *) p;
|
1996-07-04 13:34:34 -07:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
nextin:
|
|
|
|
s = 2;
|
2023-09-17 07:49:44 -07:00
|
|
|
if (*p == 0)
|
|
|
|
break;
|
2024-01-25 08:40:51 -08:00
|
|
|
n = u8_rune(&rune, (const char *) p);
|
2023-09-17 07:49:44 -07:00
|
|
|
p += n;
|
|
|
|
} while (1); /* was *p++ */
|
1996-07-04 13:34:34 -07:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-12-19 13:24:28 -08:00
|
|
|
int nematch(fa *f, const char *p0) /* non-empty match, for sub */
|
1996-07-04 13:34:34 -07:00
|
|
|
{
|
|
|
|
int s, ns;
|
2023-09-17 07:49:44 -07:00
|
|
|
int n;
|
|
|
|
int rune;
|
2020-06-10 14:03:36 -07:00
|
|
|
const uschar *p = (const uschar *) p0;
|
|
|
|
const uschar *q;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
2020-06-10 14:03:12 -07:00
|
|
|
s = f->initstat;
|
|
|
|
assert(s < f->state_count);
|
|
|
|
|
2020-06-10 14:03:36 -07:00
|
|
|
patbeg = (const char *)p;
|
1996-07-04 13:34:34 -07:00
|
|
|
patlen = -1;
|
|
|
|
while (*p) {
|
|
|
|
q = p;
|
|
|
|
do {
|
|
|
|
if (f->out[s]) /* final state */
|
|
|
|
patlen = q-p;
|
2008-10-06 13:38:33 -07:00
|
|
|
/* assert(*q < NCHARS); */
|
2024-01-25 08:40:51 -08:00
|
|
|
n = u8_rune(&rune, (const char *) q);
|
2023-09-17 07:49:44 -07:00
|
|
|
if ((ns = get_gototab(f, s, rune)) != 0)
|
1996-07-04 13:34:34 -07:00
|
|
|
s = ns;
|
|
|
|
else
|
2023-09-17 07:49:44 -07:00
|
|
|
s = cgoto(f, s, rune);
|
2001-07-11 22:16:53 -07:00
|
|
|
if (s == 1) { /* no transition */
|
1996-07-04 13:34:34 -07:00
|
|
|
if (patlen > 0) {
|
2020-06-10 14:03:36 -07:00
|
|
|
patbeg = (const char *) p;
|
1996-07-04 13:34:34 -07:00
|
|
|
return(1);
|
|
|
|
} else
|
|
|
|
goto nnextin; /* no nonempty match */
|
2001-07-11 22:16:53 -07:00
|
|
|
}
|
2023-09-17 07:49:44 -07:00
|
|
|
if (*q == 0)
|
|
|
|
break;
|
|
|
|
q += n;
|
|
|
|
} while (1);
|
|
|
|
q++;
|
1996-07-04 13:34:34 -07:00
|
|
|
if (f->out[s])
|
|
|
|
patlen = q-p-1; /* don't count $ */
|
|
|
|
if (patlen > 0 ) {
|
2020-06-10 14:03:36 -07:00
|
|
|
patbeg = (const char *) p;
|
1996-07-04 13:34:34 -07:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
nnextin:
|
|
|
|
s = 2;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-10-06 15:29:24 -07:00
|
|
|
|
2020-06-10 14:02:53 -07:00
|
|
|
/*
|
|
|
|
* NAME
|
|
|
|
* fnematch
|
|
|
|
*
|
|
|
|
* DESCRIPTION
|
|
|
|
* A stream-fed version of nematch which transfers characters to a
|
|
|
|
* null-terminated buffer. All characters up to and including the last
|
|
|
|
* character of the matching text or EOF are placed in the buffer. If
|
|
|
|
* a match is found, patbeg and patlen are set appropriately.
|
|
|
|
*
|
|
|
|
* RETURN VALUES
|
2020-06-10 14:03:56 -07:00
|
|
|
* false No match found.
|
|
|
|
* true Match found.
|
2020-06-10 14:02:53 -07:00
|
|
|
*/
|
|
|
|
|
2020-06-10 14:03:56 -07:00
|
|
|
bool fnematch(fa *pfa, FILE *f, char **pbuf, int *pbufsize, int quantum)
|
2020-06-10 14:02:53 -07:00
|
|
|
{
|
2023-11-21 17:01:21 -08:00
|
|
|
char *i, *j, *k, *buf = *pbuf;
|
2020-06-10 14:02:53 -07:00
|
|
|
int bufsize = *pbufsize;
|
2023-11-21 17:01:21 -08:00
|
|
|
int c, n, ns, s;
|
2020-06-10 14:02:53 -07:00
|
|
|
|
|
|
|
s = pfa->initstat;
|
|
|
|
patlen = 0;
|
|
|
|
|
|
|
|
/*
|
2023-11-21 17:01:21 -08:00
|
|
|
* buf <= i <= j <= k <= buf+bufsize
|
2020-06-10 14:02:53 -07:00
|
|
|
*
|
2023-11-21 17:01:21 -08:00
|
|
|
* i: origin of active substring
|
|
|
|
* j: current character
|
|
|
|
* k: destination of the next getc
|
2020-06-10 14:02:53 -07:00
|
|
|
*/
|
2023-11-21 17:01:21 -08:00
|
|
|
|
|
|
|
i = j = k = buf;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/*
|
2024-04-25 11:33:53 -07:00
|
|
|
* Call u8_rune with at least awk_mb_cur_max ahead in
|
2023-11-21 17:01:21 -08:00
|
|
|
* the buffer until EOF interferes.
|
|
|
|
*/
|
2024-05-04 15:59:21 -07:00
|
|
|
if (k - j < (int)awk_mb_cur_max) {
|
2024-04-25 11:33:53 -07:00
|
|
|
if (k + awk_mb_cur_max > buf + bufsize) {
|
|
|
|
char *obuf = buf;
|
2023-11-21 17:01:21 -08:00
|
|
|
adjbuf(&buf, &bufsize,
|
2024-04-25 11:33:53 -07:00
|
|
|
bufsize + awk_mb_cur_max,
|
2023-11-21 17:01:21 -08:00
|
|
|
quantum, 0, "fnematch");
|
2024-04-25 11:33:53 -07:00
|
|
|
|
|
|
|
/* buf resized, maybe moved. update pointers */
|
|
|
|
*pbufsize = bufsize;
|
|
|
|
if (obuf != buf) {
|
|
|
|
i = buf + (i - obuf);
|
|
|
|
j = buf + (j - obuf);
|
|
|
|
k = buf + (k - obuf);
|
|
|
|
*pbuf = buf;
|
|
|
|
if (patlen)
|
|
|
|
patbeg = buf + (patbeg - obuf);
|
|
|
|
}
|
2023-11-15 10:48:13 -08:00
|
|
|
}
|
2024-04-25 11:33:53 -07:00
|
|
|
for (n = awk_mb_cur_max ; n > 0; n--) {
|
2023-11-21 17:01:21 -08:00
|
|
|
*k++ = (c = getc(f)) != EOF ? c : 0;
|
|
|
|
if (c == EOF) {
|
|
|
|
if (ferror(f))
|
|
|
|
FATAL("fnematch: getc error");
|
|
|
|
break;
|
|
|
|
}
|
2023-09-17 07:49:44 -07:00
|
|
|
}
|
2023-11-21 17:01:21 -08:00
|
|
|
}
|
2020-06-10 14:02:53 -07:00
|
|
|
|
2024-01-25 08:40:51 -08:00
|
|
|
j += u8_rune(&c, j);
|
2020-06-10 14:02:53 -07:00
|
|
|
|
2023-11-21 17:01:21 -08:00
|
|
|
if ((ns = get_gototab(pfa, s, c)) != 0)
|
|
|
|
s = ns;
|
|
|
|
else
|
|
|
|
s = cgoto(pfa, s, c);
|
|
|
|
|
|
|
|
if (pfa->out[s]) { /* final state */
|
|
|
|
patbeg = i;
|
|
|
|
patlen = j - i;
|
|
|
|
if (c == 0) /* don't count $ */
|
|
|
|
patlen--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c && s != 1)
|
|
|
|
continue; /* origin i still viable, next j */
|
|
|
|
if (patlen)
|
|
|
|
break; /* best match found */
|
|
|
|
|
|
|
|
/* no match at origin i, next i and start over */
|
2024-01-25 08:40:51 -08:00
|
|
|
i += u8_rune(&c, i);
|
2023-11-21 17:01:21 -08:00
|
|
|
if (c == 0)
|
|
|
|
break; /* no match */
|
|
|
|
j = i;
|
2020-06-10 14:02:53 -07:00
|
|
|
s = 2;
|
2023-11-21 17:01:21 -08:00
|
|
|
} while (1);
|
2020-06-10 14:02:53 -07:00
|
|
|
|
|
|
|
if (patlen) {
|
|
|
|
/*
|
|
|
|
* Under no circumstances is the last character fed to
|
|
|
|
* the automaton part of the match. It is EOF's nullbyte,
|
|
|
|
* or it sent the automaton into a state with no further
|
|
|
|
* transitions available (s==1), or both. Room for a
|
|
|
|
* terminating nullbyte is guaranteed.
|
|
|
|
*
|
|
|
|
* ungetc any chars after the end of matching text
|
|
|
|
* (except for EOF's nullbyte, if present) and null
|
|
|
|
* terminate the buffer.
|
|
|
|
*/
|
2023-11-21 17:01:21 -08:00
|
|
|
do
|
|
|
|
if (*--k && ungetc(*k, f) == EOF)
|
|
|
|
FATAL("unable to ungetc '%c'", *k);
|
|
|
|
while (k > patbeg + patlen);
|
|
|
|
*k = '\0';
|
2020-06-10 14:03:56 -07:00
|
|
|
return true;
|
2020-06-10 14:02:53 -07:00
|
|
|
}
|
|
|
|
else
|
2020-06-10 14:03:56 -07:00
|
|
|
return false;
|
2020-06-10 14:02:53 -07:00
|
|
|
}
|
|
|
|
|
2002-12-19 13:24:28 -08:00
|
|
|
Node *reparse(const char *p) /* parses regular expression pointed to by p */
|
1996-07-04 13:34:34 -07:00
|
|
|
{ /* uses relex() to scan regular expression */
|
|
|
|
Node *np;
|
|
|
|
|
2020-06-26 08:57:39 -07:00
|
|
|
DPRINTF("reparse <%s>\n", p);
|
2020-06-10 14:03:36 -07:00
|
|
|
lastre = prestr = (const uschar *) p; /* prestr points to string to be parsed */
|
1996-07-04 13:34:34 -07:00
|
|
|
rtok = relex();
|
2002-12-19 13:24:28 -08:00
|
|
|
/* GNU compatibility: an empty regexp matches anything */
|
2008-10-06 13:38:33 -07:00
|
|
|
if (rtok == '\0') {
|
2002-12-19 13:24:28 -08:00
|
|
|
/* FATAL("empty regular expression"); previous */
|
2008-10-06 13:38:33 -07:00
|
|
|
return(op2(EMPTYRE, NIL, NIL));
|
|
|
|
}
|
1996-07-04 13:34:34 -07:00
|
|
|
np = regexp();
|
|
|
|
if (rtok != '\0')
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("syntax error in regular expression %s at %s", lastre, prestr);
|
1996-07-04 13:34:34 -07:00
|
|
|
return(np);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *regexp(void) /* top-level parse of reg expr */
|
|
|
|
{
|
|
|
|
return (alt(concat(primary())));
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *primary(void)
|
|
|
|
{
|
|
|
|
Node *np;
|
2020-06-10 14:01:50 -07:00
|
|
|
int savelastatom;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
|
|
|
switch (rtok) {
|
|
|
|
case CHAR:
|
2020-06-10 14:01:50 -07:00
|
|
|
lastatom = starttok;
|
1999-04-20 10:31:25 -07:00
|
|
|
np = op2(CHAR, NIL, itonp(rlxval));
|
1996-07-04 13:34:34 -07:00
|
|
|
rtok = relex();
|
|
|
|
return (unary(np));
|
|
|
|
case ALL:
|
|
|
|
rtok = relex();
|
|
|
|
return (unary(op2(ALL, NIL, NIL)));
|
2008-10-06 13:38:33 -07:00
|
|
|
case EMPTYRE:
|
|
|
|
rtok = relex();
|
2020-06-10 14:01:50 -07:00
|
|
|
return (unary(op2(EMPTYRE, NIL, NIL)));
|
1996-07-04 13:34:34 -07:00
|
|
|
case DOT:
|
2020-06-10 14:01:50 -07:00
|
|
|
lastatom = starttok;
|
1996-07-04 13:34:34 -07:00
|
|
|
rtok = relex();
|
|
|
|
return (unary(op2(DOT, NIL, NIL)));
|
|
|
|
case CCL:
|
2020-06-10 14:03:36 -07:00
|
|
|
np = op2(CCL, NIL, (Node*) cclenter((const char *) rlxstr));
|
2020-06-10 14:01:50 -07:00
|
|
|
lastatom = starttok;
|
1996-07-04 13:34:34 -07:00
|
|
|
rtok = relex();
|
|
|
|
return (unary(np));
|
|
|
|
case NCCL:
|
2020-06-10 14:03:36 -07:00
|
|
|
np = op2(NCCL, NIL, (Node *) cclenter((const char *) rlxstr));
|
2020-06-10 14:01:50 -07:00
|
|
|
lastatom = starttok;
|
1996-07-04 13:34:34 -07:00
|
|
|
rtok = relex();
|
|
|
|
return (unary(np));
|
|
|
|
case '^':
|
|
|
|
rtok = relex();
|
1999-04-20 10:31:25 -07:00
|
|
|
return (unary(op2(CHAR, NIL, itonp(HAT))));
|
1996-07-04 13:34:34 -07:00
|
|
|
case '$':
|
|
|
|
rtok = relex();
|
|
|
|
return (unary(op2(CHAR, NIL, NIL)));
|
|
|
|
case '(':
|
2020-06-10 14:01:50 -07:00
|
|
|
lastatom = starttok;
|
|
|
|
savelastatom = starttok - basestr; /* Retain over recursion */
|
1996-07-04 13:34:34 -07:00
|
|
|
rtok = relex();
|
|
|
|
if (rtok == ')') { /* special pleading for () */
|
|
|
|
rtok = relex();
|
2023-09-21 10:19:06 -07:00
|
|
|
return unary(op2(CCL, NIL, (Node *) cclenter("")));
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
np = regexp();
|
|
|
|
if (rtok == ')') {
|
2020-06-10 14:01:50 -07:00
|
|
|
lastatom = basestr + savelastatom; /* Restore */
|
1996-07-04 13:34:34 -07:00
|
|
|
rtok = relex();
|
|
|
|
return (unary(np));
|
|
|
|
}
|
|
|
|
else
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("syntax error in regular expression %s at %s", lastre, prestr);
|
1996-07-04 13:34:34 -07:00
|
|
|
default:
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("illegal primary in regular expression %s at %s", lastre, prestr);
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
return 0; /*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *concat(Node *np)
|
|
|
|
{
|
|
|
|
switch (rtok) {
|
2020-06-10 14:01:50 -07:00
|
|
|
case CHAR: case DOT: case ALL: case CCL: case NCCL: case '$': case '(':
|
1996-07-04 13:34:34 -07:00
|
|
|
return (concat(op2(CAT, np, primary())));
|
2020-06-10 14:01:50 -07:00
|
|
|
case EMPTYRE:
|
|
|
|
rtok = relex();
|
2023-09-21 10:19:06 -07:00
|
|
|
return (concat(op2(CAT, op2(CCL, NIL, (Node *) cclenter("")),
|
2020-06-10 14:01:50 -07:00
|
|
|
primary())));
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
return (np);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *alt(Node *np)
|
|
|
|
{
|
|
|
|
if (rtok == OR) {
|
|
|
|
rtok = relex();
|
|
|
|
return (alt(op2(OR, np, concat(primary()))));
|
|
|
|
}
|
|
|
|
return (np);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *unary(Node *np)
|
|
|
|
{
|
|
|
|
switch (rtok) {
|
|
|
|
case STAR:
|
|
|
|
rtok = relex();
|
|
|
|
return (unary(op2(STAR, np, NIL)));
|
|
|
|
case PLUS:
|
|
|
|
rtok = relex();
|
|
|
|
return (unary(op2(PLUS, np, NIL)));
|
|
|
|
case QUEST:
|
|
|
|
rtok = relex();
|
|
|
|
return (unary(op2(QUEST, np, NIL)));
|
2020-06-10 14:05:02 -07:00
|
|
|
case ZERO:
|
|
|
|
rtok = relex();
|
|
|
|
return (unary(op2(ZERO, np, NIL)));
|
1996-07-04 13:34:34 -07:00
|
|
|
default:
|
|
|
|
return (np);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-19 13:24:28 -08:00
|
|
|
/*
|
|
|
|
* Character class definitions conformant to the POSIX locale as
|
|
|
|
* defined in IEEE P1003.1 draft 7 of June 2001, assuming the source
|
|
|
|
* and operating character sets are both ASCII (ISO646) or supersets
|
|
|
|
* thereof.
|
|
|
|
*
|
|
|
|
* Note that to avoid overflowing the temporary buffer used in
|
|
|
|
* relex(), the expanded character class (prior to range expansion)
|
|
|
|
* must be less than twice the size of their full name.
|
|
|
|
*/
|
2004-12-29 17:52:48 -08:00
|
|
|
|
|
|
|
/* Because isblank doesn't show up in any of the header files on any
|
|
|
|
* system i use, it's defined here. if some other locale has a richer
|
|
|
|
* definition of "blank", define HAS_ISBLANK and provide your own
|
|
|
|
* version.
|
|
|
|
* the parentheses here are an attempt to find a path through the maze
|
|
|
|
* of macro definition and/or function and/or version provided. thanks
|
|
|
|
* to nelson beebe for the suggestion; let's see if it works everywhere.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HAS_ISBLANK
|
|
|
|
|
2011-09-28 12:27:18 -07:00
|
|
|
int (xisblank)(int c)
|
2004-12-29 17:52:48 -08:00
|
|
|
{
|
|
|
|
return c==' ' || c=='\t';
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-06-10 14:05:02 -07:00
|
|
|
static const struct charclass {
|
2002-12-19 13:24:28 -08:00
|
|
|
const char *cc_name;
|
|
|
|
int cc_namelen;
|
2004-12-29 17:52:48 -08:00
|
|
|
int (*cc_func)(int);
|
2002-12-19 13:24:28 -08:00
|
|
|
} charclasses[] = {
|
2004-12-29 17:52:48 -08:00
|
|
|
{ "alnum", 5, isalnum },
|
|
|
|
{ "alpha", 5, isalpha },
|
2011-09-28 12:27:18 -07:00
|
|
|
#ifndef HAS_ISBLANK
|
2020-06-10 14:00:01 -07:00
|
|
|
{ "blank", 5, xisblank },
|
2011-09-28 12:27:18 -07:00
|
|
|
#else
|
2004-12-29 17:52:48 -08:00
|
|
|
{ "blank", 5, isblank },
|
2011-09-28 12:27:18 -07:00
|
|
|
#endif
|
2004-12-29 17:52:48 -08:00
|
|
|
{ "cntrl", 5, iscntrl },
|
|
|
|
{ "digit", 5, isdigit },
|
|
|
|
{ "graph", 5, isgraph },
|
|
|
|
{ "lower", 5, islower },
|
|
|
|
{ "print", 5, isprint },
|
|
|
|
{ "punct", 5, ispunct },
|
|
|
|
{ "space", 5, isspace },
|
|
|
|
{ "upper", 5, isupper },
|
|
|
|
{ "xdigit", 6, isxdigit },
|
2002-12-19 13:24:28 -08:00
|
|
|
{ NULL, 0, NULL },
|
|
|
|
};
|
|
|
|
|
2020-06-10 14:01:50 -07:00
|
|
|
#define REPEAT_SIMPLE 0
|
|
|
|
#define REPEAT_PLUS_APPENDED 1
|
|
|
|
#define REPEAT_WITH_Q 2
|
|
|
|
#define REPEAT_ZERO 3
|
|
|
|
|
|
|
|
static int
|
|
|
|
replace_repeat(const uschar *reptok, int reptoklen, const uschar *atom,
|
|
|
|
int atomlen, int firstnum, int secondnum, int special_case)
|
|
|
|
{
|
|
|
|
int i, j;
|
2020-06-10 14:02:53 -07:00
|
|
|
uschar *buf = NULL;
|
2020-06-10 14:01:50 -07:00
|
|
|
int ret = 1;
|
2020-06-10 14:05:02 -07:00
|
|
|
int init_q = (firstnum == 0); /* first added char will be ? */
|
2020-06-10 14:01:50 -07:00
|
|
|
int n_q_reps = secondnum-firstnum; /* m>n, so reduce until {1,m-n} left */
|
|
|
|
int prefix_length = reptok - basestr; /* prefix includes first rep */
|
2020-06-10 14:03:36 -07:00
|
|
|
int suffix_length = strlen((const char *) reptok) - reptoklen; /* string after rep specifier */
|
2020-06-10 14:01:50 -07:00
|
|
|
int size = prefix_length + suffix_length;
|
|
|
|
|
|
|
|
if (firstnum > 1) { /* add room for reps 2 through firstnum */
|
|
|
|
size += atomlen*(firstnum-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust size of buffer for special cases */
|
|
|
|
if (special_case == REPEAT_PLUS_APPENDED) {
|
|
|
|
size++; /* for the final + */
|
|
|
|
} else if (special_case == REPEAT_WITH_Q) {
|
2021-03-02 12:41:42 -08:00
|
|
|
size += init_q + (atomlen+1)* (n_q_reps-init_q);
|
2020-06-10 14:01:50 -07:00
|
|
|
} else if (special_case == REPEAT_ZERO) {
|
|
|
|
size += 2; /* just a null ERE: () */
|
|
|
|
}
|
2020-12-09 12:00:11 -08:00
|
|
|
if ((buf = (uschar *) malloc(size + 1)) == NULL)
|
2020-06-10 14:01:50 -07:00
|
|
|
FATAL("out of space in reg expr %.10s..", lastre);
|
|
|
|
memcpy(buf, basestr, prefix_length); /* copy prefix */
|
|
|
|
j = prefix_length;
|
|
|
|
if (special_case == REPEAT_ZERO) {
|
|
|
|
j -= atomlen;
|
|
|
|
buf[j++] = '(';
|
|
|
|
buf[j++] = ')';
|
|
|
|
}
|
2020-06-10 14:04:40 -07:00
|
|
|
for (i = 1; i < firstnum; i++) { /* copy x reps */
|
2020-06-10 14:01:50 -07:00
|
|
|
memcpy(&buf[j], atom, atomlen);
|
|
|
|
j += atomlen;
|
|
|
|
}
|
|
|
|
if (special_case == REPEAT_PLUS_APPENDED) {
|
|
|
|
buf[j++] = '+';
|
|
|
|
} else if (special_case == REPEAT_WITH_Q) {
|
2020-06-10 14:03:56 -07:00
|
|
|
if (init_q)
|
|
|
|
buf[j++] = '?';
|
2020-06-10 14:04:40 -07:00
|
|
|
for (i = init_q; i < n_q_reps; i++) { /* copy x? reps */
|
2020-06-10 14:01:50 -07:00
|
|
|
memcpy(&buf[j], atom, atomlen);
|
|
|
|
j += atomlen;
|
|
|
|
buf[j++] = '?';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(&buf[j], reptok+reptoklen, suffix_length);
|
2021-03-02 12:41:42 -08:00
|
|
|
j += suffix_length;
|
|
|
|
buf[j] = '\0';
|
2020-06-10 14:01:50 -07:00
|
|
|
/* free old basestr */
|
|
|
|
if (firstbasestr != basestr) {
|
|
|
|
if (basestr)
|
|
|
|
xfree(basestr);
|
|
|
|
}
|
|
|
|
basestr = buf;
|
|
|
|
prestr = buf + prefix_length;
|
|
|
|
if (special_case == REPEAT_ZERO) {
|
|
|
|
prestr -= atomlen;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int repeat(const uschar *reptok, int reptoklen, const uschar *atom,
|
|
|
|
int atomlen, int firstnum, int secondnum)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
In general, the repetition specifier or "bound" is replaced here
|
|
|
|
by an equivalent ERE string, repeating the immediately previous atom
|
|
|
|
and appending ? and + as needed. Note that the first copy of the
|
|
|
|
atom is left in place, except in the special_case of a zero-repeat
|
|
|
|
(i.e., {0}).
|
|
|
|
*/
|
|
|
|
if (secondnum < 0) { /* means {n,} -> repeat n-1 times followed by PLUS */
|
|
|
|
if (firstnum < 2) {
|
|
|
|
/* 0 or 1: should be handled before you get here */
|
|
|
|
FATAL("internal error");
|
|
|
|
} else {
|
|
|
|
return replace_repeat(reptok, reptoklen, atom, atomlen,
|
|
|
|
firstnum, secondnum, REPEAT_PLUS_APPENDED);
|
|
|
|
}
|
|
|
|
} else if (firstnum == secondnum) { /* {n} or {n,n} -> simply repeat n-1 times */
|
|
|
|
if (firstnum == 0) { /* {0} or {0,0} */
|
|
|
|
/* This case is unusual because the resulting
|
|
|
|
replacement string might actually be SMALLER than
|
|
|
|
the original ERE */
|
|
|
|
return replace_repeat(reptok, reptoklen, atom, atomlen,
|
|
|
|
firstnum, secondnum, REPEAT_ZERO);
|
|
|
|
} else { /* (firstnum >= 1) */
|
|
|
|
return replace_repeat(reptok, reptoklen, atom, atomlen,
|
|
|
|
firstnum, secondnum, REPEAT_SIMPLE);
|
|
|
|
}
|
|
|
|
} else if (firstnum < secondnum) { /* {n,m} -> repeat n-1 times then alternate */
|
|
|
|
/* x{n,m} => xx...x{1, m-n+1} => xx...x?x?x?..x? */
|
|
|
|
return replace_repeat(reptok, reptoklen, atom, atomlen,
|
|
|
|
firstnum, secondnum, REPEAT_WITH_Q);
|
|
|
|
} else { /* Error - shouldn't be here (n>m) */
|
|
|
|
FATAL("internal error");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2002-12-19 13:24:28 -08:00
|
|
|
|
1996-07-04 13:34:34 -07:00
|
|
|
int relex(void) /* lexical analyzer for reparse */
|
|
|
|
{
|
1997-08-25 09:17:07 -07:00
|
|
|
int c, n;
|
1996-07-04 13:34:34 -07:00
|
|
|
int cflag;
|
2020-06-10 14:02:33 -07:00
|
|
|
static uschar *buf = NULL;
|
1997-08-25 09:17:07 -07:00
|
|
|
static int bufsz = 100;
|
2001-09-07 17:12:40 -07:00
|
|
|
uschar *bp;
|
2020-06-10 14:05:02 -07:00
|
|
|
const struct charclass *cc;
|
2004-12-29 17:52:48 -08:00
|
|
|
int i;
|
2020-06-10 14:03:56 -07:00
|
|
|
int num, m;
|
|
|
|
bool commafound, digitfound;
|
2020-06-10 14:01:50 -07:00
|
|
|
const uschar *startreptok;
|
2020-06-10 14:02:19 -07:00
|
|
|
static int parens = 0;
|
2020-06-10 14:01:50 -07:00
|
|
|
|
|
|
|
rescan:
|
|
|
|
starttok = prestr;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
2024-01-25 08:40:51 -08:00
|
|
|
if ((n = u8_rune(&rlxval, (const char *) prestr)) > 1) {
|
2023-09-17 07:49:44 -07:00
|
|
|
prestr += n;
|
|
|
|
starttok = prestr;
|
|
|
|
return CHAR;
|
|
|
|
}
|
|
|
|
|
1996-07-04 13:34:34 -07:00
|
|
|
switch (c = *prestr++) {
|
|
|
|
case '|': return OR;
|
|
|
|
case '*': return STAR;
|
|
|
|
case '+': return PLUS;
|
|
|
|
case '?': return QUEST;
|
|
|
|
case '.': return DOT;
|
|
|
|
case '\0': prestr--; return '\0';
|
|
|
|
case '^':
|
|
|
|
case '$':
|
2020-06-10 14:02:19 -07:00
|
|
|
return c;
|
1996-07-04 13:34:34 -07:00
|
|
|
case '(':
|
2020-06-10 14:02:19 -07:00
|
|
|
parens++;
|
1996-07-04 13:34:34 -07:00
|
|
|
return c;
|
2020-06-10 14:02:19 -07:00
|
|
|
case ')':
|
|
|
|
if (parens) {
|
|
|
|
parens--;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
/* unmatched close parenthesis; per POSIX, treat as literal */
|
|
|
|
rlxval = c;
|
|
|
|
return CHAR;
|
1996-07-04 13:34:34 -07:00
|
|
|
case '\\':
|
2011-09-28 12:27:18 -07:00
|
|
|
rlxval = quoted(&prestr);
|
1996-07-04 13:34:34 -07:00
|
|
|
return CHAR;
|
|
|
|
default:
|
|
|
|
rlxval = c;
|
|
|
|
return CHAR;
|
2020-06-10 14:02:33 -07:00
|
|
|
case '[':
|
2020-12-09 12:00:11 -08:00
|
|
|
if (buf == NULL && (buf = (uschar *) malloc(bufsz)) == NULL)
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("out of space in reg expr %.10s..", lastre);
|
1997-08-25 09:17:07 -07:00
|
|
|
bp = buf;
|
1996-07-04 13:34:34 -07:00
|
|
|
if (*prestr == '^') {
|
|
|
|
cflag = 1;
|
|
|
|
prestr++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cflag = 0;
|
2023-09-17 07:49:44 -07:00
|
|
|
n = 5 * strlen((const char *) prestr)+1; /* BUG: was 2. what value? */
|
2008-10-06 13:38:33 -07:00
|
|
|
if (!adjbuf((char **) &buf, &bufsz, n, n, (char **) &bp, "relex1"))
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("out of space for reg expr %.10s...", lastre);
|
1996-07-04 13:34:34 -07:00
|
|
|
for (; ; ) {
|
2024-01-25 08:40:51 -08:00
|
|
|
if ((n = u8_rune(&rlxval, (const char *) prestr)) > 1) {
|
2023-09-17 07:49:44 -07:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
*bp++ = *prestr++;
|
|
|
|
continue;
|
|
|
|
}
|
1996-07-04 13:34:34 -07:00
|
|
|
if ((c = *prestr++) == '\\') {
|
1997-08-25 09:17:07 -07:00
|
|
|
*bp++ = '\\';
|
1996-07-04 13:34:34 -07:00
|
|
|
if ((c = *prestr++) == '\0')
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("nonterminated character class %.20s...", lastre);
|
1997-08-25 09:17:07 -07:00
|
|
|
*bp++ = c;
|
2001-09-07 17:12:40 -07:00
|
|
|
/* } else if (c == '\n') { */
|
|
|
|
/* FATAL("newline in character class %.20s...", lastre); */
|
2002-12-19 13:24:28 -08:00
|
|
|
} else if (c == '[' && *prestr == ':') {
|
|
|
|
/* POSIX char class names, Dag-Erling Smorgrav, des@ofug.org */
|
|
|
|
for (cc = charclasses; cc->cc_name; cc++)
|
|
|
|
if (strncmp((const char *) prestr + 1, (const char *) cc->cc_name, cc->cc_namelen) == 0)
|
|
|
|
break;
|
|
|
|
if (cc->cc_name != NULL && prestr[1 + cc->cc_namelen] == ':' &&
|
|
|
|
prestr[2 + cc->cc_namelen] == ']') {
|
|
|
|
prestr += cc->cc_namelen + 3;
|
2020-06-10 14:01:32 -07:00
|
|
|
/*
|
|
|
|
* BUG: We begin at 1, instead of 0, since we
|
|
|
|
* would otherwise prematurely terminate the
|
|
|
|
* string for classes like [[:cntrl:]]. This
|
|
|
|
* means that we can't match the NUL character,
|
|
|
|
* not without first adapting the entire
|
|
|
|
* program to track each string's length.
|
|
|
|
*/
|
2020-06-10 14:01:50 -07:00
|
|
|
for (i = 1; i <= UCHAR_MAX; i++) {
|
2021-07-08 14:26:39 -07:00
|
|
|
if (!adjbuf((char **) &buf, &bufsz, bp-buf+2, 100, (char **) &bp, "relex2"))
|
2004-12-29 17:52:48 -08:00
|
|
|
FATAL("out of space for reg expr %.10s...", lastre);
|
|
|
|
if (cc->cc_func(i)) {
|
2020-06-26 08:57:39 -07:00
|
|
|
/* escape backslash */
|
|
|
|
if (i == '\\') {
|
|
|
|
*bp++ = '\\';
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2004-12-29 17:52:48 -08:00
|
|
|
*bp++ = i;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
2002-12-19 13:24:28 -08:00
|
|
|
} else
|
|
|
|
*bp++ = c;
|
2020-06-10 14:01:50 -07:00
|
|
|
} else if (c == '[' && *prestr == '.') {
|
|
|
|
char collate_char;
|
|
|
|
prestr++;
|
|
|
|
collate_char = *prestr++;
|
|
|
|
if (*prestr == '.' && prestr[1] == ']') {
|
|
|
|
prestr += 2;
|
|
|
|
/* Found it: map via locale TBD: for
|
|
|
|
now, simply return this char. This
|
|
|
|
is sufficient to pass conformance
|
|
|
|
test awk.ex 156
|
|
|
|
*/
|
|
|
|
if (*prestr == ']') {
|
|
|
|
prestr++;
|
|
|
|
rlxval = collate_char;
|
|
|
|
return CHAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (c == '[' && *prestr == '=') {
|
|
|
|
char equiv_char;
|
|
|
|
prestr++;
|
|
|
|
equiv_char = *prestr++;
|
|
|
|
if (*prestr == '=' && prestr[1] == ']') {
|
|
|
|
prestr += 2;
|
|
|
|
/* Found it: map via locale TBD: for now
|
|
|
|
simply return this char. This is
|
|
|
|
sufficient to pass conformance test
|
|
|
|
awk.ex 156
|
|
|
|
*/
|
|
|
|
if (*prestr == ']') {
|
|
|
|
prestr++;
|
|
|
|
rlxval = equiv_char;
|
|
|
|
return CHAR;
|
|
|
|
}
|
|
|
|
}
|
1996-07-04 13:34:34 -07:00
|
|
|
} else if (c == '\0') {
|
1999-12-08 15:09:45 -08:00
|
|
|
FATAL("nonterminated character class %.20s", lastre);
|
1997-08-25 09:17:07 -07:00
|
|
|
} else if (bp == buf) { /* 1st char is special */
|
|
|
|
*bp++ = c;
|
1996-07-04 13:34:34 -07:00
|
|
|
} else if (c == ']') {
|
1997-08-25 09:17:07 -07:00
|
|
|
*bp++ = 0;
|
2001-09-07 17:12:40 -07:00
|
|
|
rlxstr = (uschar *) tostring((char *) buf);
|
1996-07-04 13:34:34 -07:00
|
|
|
if (cflag == 0)
|
|
|
|
return CCL;
|
|
|
|
else
|
|
|
|
return NCCL;
|
|
|
|
} else
|
1997-08-25 09:17:07 -07:00
|
|
|
*bp++ = c;
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
2020-06-10 14:01:50 -07:00
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
if (isdigit(*(prestr))) {
|
|
|
|
num = 0; /* Process as a repetition */
|
|
|
|
n = -1; m = -1;
|
2020-06-10 14:03:56 -07:00
|
|
|
commafound = false;
|
|
|
|
digitfound = false;
|
2020-06-10 14:01:50 -07:00
|
|
|
startreptok = prestr-1;
|
|
|
|
/* Remember start of previous atom here ? */
|
|
|
|
} else { /* just a { char, not a repetition */
|
|
|
|
rlxval = c;
|
|
|
|
return CHAR;
|
|
|
|
}
|
|
|
|
for (; ; ) {
|
|
|
|
if ((c = *prestr++) == '}') {
|
|
|
|
if (commafound) {
|
|
|
|
if (digitfound) { /* {n,m} */
|
|
|
|
m = num;
|
2020-06-10 14:04:40 -07:00
|
|
|
if (m < n)
|
2020-06-10 14:01:50 -07:00
|
|
|
FATAL("illegal repetition expression: class %.20s",
|
|
|
|
lastre);
|
2020-06-10 14:04:40 -07:00
|
|
|
if (n == 0 && m == 1) {
|
2020-06-10 14:01:50 -07:00
|
|
|
return QUEST;
|
|
|
|
}
|
|
|
|
} else { /* {n,} */
|
2020-06-10 14:04:40 -07:00
|
|
|
if (n == 0)
|
|
|
|
return STAR;
|
|
|
|
else if (n == 1)
|
|
|
|
return PLUS;
|
2020-06-10 14:01:50 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (digitfound) { /* {n} same as {n,n} */
|
|
|
|
n = num;
|
|
|
|
m = num;
|
|
|
|
} else { /* {} */
|
|
|
|
FATAL("illegal repetition expression: class %.20s",
|
|
|
|
lastre);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (repeat(starttok, prestr-starttok, lastatom,
|
|
|
|
startreptok - lastatom, n, m) > 0) {
|
2020-06-10 14:04:40 -07:00
|
|
|
if (n == 0 && m == 0) {
|
2020-06-10 14:05:02 -07:00
|
|
|
return ZERO;
|
2020-06-10 14:01:50 -07:00
|
|
|
}
|
|
|
|
/* must rescan input for next token */
|
|
|
|
goto rescan;
|
|
|
|
}
|
|
|
|
/* Failed to replace: eat up {...} characters
|
|
|
|
and treat like just PLUS */
|
|
|
|
return PLUS;
|
|
|
|
} else if (c == '\0') {
|
|
|
|
FATAL("nonterminated character class %.20s",
|
|
|
|
lastre);
|
|
|
|
} else if (isdigit(c)) {
|
|
|
|
num = 10 * num + c - '0';
|
2020-06-10 14:03:56 -07:00
|
|
|
digitfound = true;
|
2020-06-10 14:01:50 -07:00
|
|
|
} else if (c == ',') {
|
|
|
|
if (commafound)
|
|
|
|
FATAL("illegal repetition expression: class %.20s",
|
|
|
|
lastre);
|
|
|
|
/* looking for {n,} or {n,m} */
|
2020-06-10 14:03:56 -07:00
|
|
|
commafound = true;
|
2020-06-10 14:01:50 -07:00
|
|
|
n = num;
|
2020-06-10 14:03:56 -07:00
|
|
|
digitfound = false; /* reset */
|
2020-06-10 14:01:50 -07:00
|
|
|
num = 0;
|
|
|
|
} else {
|
|
|
|
FATAL("illegal repetition expression: class %.20s",
|
|
|
|
lastre);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int cgoto(fa *f, int s, int c)
|
|
|
|
{
|
1997-04-07 08:59:53 -07:00
|
|
|
int *p, *q;
|
2020-06-10 14:03:12 -07:00
|
|
|
int i, j, k;
|
1996-07-04 13:34:34 -07:00
|
|
|
|
2023-09-17 07:49:44 -07:00
|
|
|
/* assert(c == HAT || c < NCHARS); BUG: seg fault if disable test */
|
1996-07-04 13:34:34 -07:00
|
|
|
while (f->accept >= maxsetvec) { /* guessing here! */
|
2020-06-10 14:03:12 -07:00
|
|
|
resizesetvec(__func__);
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
for (i = 0; i <= f->accept; i++)
|
|
|
|
setvec[i] = 0;
|
|
|
|
setcnt = 0;
|
2020-06-10 14:03:12 -07:00
|
|
|
resize_state(f, s);
|
1996-07-04 13:34:34 -07:00
|
|
|
/* compute positions of gototab[s,c] into setvec */
|
|
|
|
p = f->posns[s];
|
|
|
|
for (i = 1; i <= *p; i++) {
|
|
|
|
if ((k = f->re[p[i]].ltype) != FINAL) {
|
1999-04-20 10:31:25 -07:00
|
|
|
if ((k == CHAR && c == ptoi(f->re[p[i]].lval.np))
|
1996-07-04 13:34:34 -07:00
|
|
|
|| (k == DOT && c != 0 && c != HAT)
|
|
|
|
|| (k == ALL && c != 0)
|
2008-10-06 13:38:33 -07:00
|
|
|
|| (k == EMPTYRE && c != 0)
|
2023-09-17 07:49:44 -07:00
|
|
|
|| (k == CCL && member(c, (int *) f->re[p[i]].lval.rp))
|
|
|
|
|| (k == NCCL && !member(c, (int *) f->re[p[i]].lval.rp) && c != 0 && c != HAT)) {
|
1996-07-04 13:34:34 -07:00
|
|
|
q = f->re[p[i]].lfollow;
|
|
|
|
for (j = 1; j <= *q; j++) {
|
|
|
|
if (q[j] >= maxsetvec) {
|
2020-06-10 14:03:12 -07:00
|
|
|
resizesetvec(__func__);
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
if (setvec[q[j]] == 0) {
|
|
|
|
setcnt++;
|
|
|
|
setvec[q[j]] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* determine if setvec is a previous state */
|
|
|
|
tmpset[0] = setcnt;
|
|
|
|
j = 1;
|
|
|
|
for (i = f->accept; i >= 0; i--)
|
|
|
|
if (setvec[i]) {
|
|
|
|
tmpset[j++] = i;
|
|
|
|
}
|
2020-06-10 14:03:12 -07:00
|
|
|
resize_state(f, f->curstat > s ? f->curstat : s);
|
1996-07-04 13:34:34 -07:00
|
|
|
/* tmpset == previous state? */
|
|
|
|
for (i = 1; i <= f->curstat; i++) {
|
|
|
|
p = f->posns[i];
|
|
|
|
if ((k = tmpset[0]) != p[0])
|
|
|
|
goto different;
|
|
|
|
for (j = 1; j <= k; j++)
|
|
|
|
if (tmpset[j] != p[j])
|
|
|
|
goto different;
|
|
|
|
/* setvec is state i */
|
2020-06-10 14:04:40 -07:00
|
|
|
if (c != HAT)
|
2023-09-17 07:49:44 -07:00
|
|
|
set_gototab(f, s, c, i);
|
1996-07-04 13:34:34 -07:00
|
|
|
return i;
|
|
|
|
different:;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add tmpset to current set of states */
|
2020-06-10 14:03:12 -07:00
|
|
|
++(f->curstat);
|
|
|
|
resize_state(f, f->curstat);
|
2023-11-25 08:31:33 -08:00
|
|
|
clear_gototab(f, f->curstat);
|
1996-07-04 13:34:34 -07:00
|
|
|
xfree(f->posns[f->curstat]);
|
2020-06-10 14:03:12 -07:00
|
|
|
p = intalloc(setcnt + 1, __func__);
|
1996-07-04 13:34:34 -07:00
|
|
|
|
|
|
|
f->posns[f->curstat] = p;
|
2020-06-10 14:04:40 -07:00
|
|
|
if (c != HAT)
|
2023-09-17 07:49:44 -07:00
|
|
|
set_gototab(f, s, c, f->curstat);
|
1996-07-04 13:34:34 -07:00
|
|
|
for (i = 0; i <= setcnt; i++)
|
|
|
|
p[i] = tmpset[i];
|
|
|
|
if (setvec[f->accept])
|
|
|
|
f->out[f->curstat] = 1;
|
|
|
|
else
|
|
|
|
f->out[f->curstat] = 0;
|
|
|
|
return f->curstat;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void freefa(fa *f) /* free a finite automaton */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
return;
|
2020-06-10 14:03:12 -07:00
|
|
|
for (i = 0; i < f->state_count; i++)
|
2023-11-25 08:31:33 -08:00
|
|
|
xfree(f->gototab[i].entries);
|
|
|
|
xfree(f->gototab);
|
1996-07-04 13:34:34 -07:00
|
|
|
for (i = 0; i <= f->curstat; i++)
|
|
|
|
xfree(f->posns[i]);
|
|
|
|
for (i = 0; i <= f->accept; i++) {
|
|
|
|
xfree(f->re[i].lfollow);
|
|
|
|
if (f->re[i].ltype == CCL || f->re[i].ltype == NCCL)
|
2020-06-10 14:04:40 -07:00
|
|
|
xfree(f->re[i].lval.np);
|
1996-07-04 13:34:34 -07:00
|
|
|
}
|
|
|
|
xfree(f->restr);
|
2020-06-10 14:03:12 -07:00
|
|
|
xfree(f->out);
|
|
|
|
xfree(f->posns);
|
|
|
|
xfree(f->gototab);
|
1996-07-04 13:34:34 -07:00
|
|
|
xfree(f);
|
|
|
|
}
|