2003-02-05 18:41:33 +01:00
|
|
|
/*
|
|
|
|
* lexical analyzer
|
|
|
|
* This file is #included by regcomp.c.
|
|
|
|
*
|
2014-05-06 18:12:18 +02:00
|
|
|
* Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
|
2003-08-04 02:43:34 +02:00
|
|
|
*
|
2003-02-05 18:41:33 +01:00
|
|
|
* Development of this software was funded, in part, by Cray Research Inc.,
|
|
|
|
* UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
|
|
|
|
* Corporation, none of whom are responsible for the results. The author
|
2003-08-04 02:43:34 +02:00
|
|
|
* thanks all of them.
|
|
|
|
*
|
2003-02-05 18:41:33 +01:00
|
|
|
* Redistribution and use in source and binary forms -- with or without
|
|
|
|
* modification -- are permitted for any purpose, provided that
|
|
|
|
* redistributions in source form retain this entire copyright notice and
|
|
|
|
* indicate the origin and nature of any modifications.
|
2003-08-04 02:43:34 +02:00
|
|
|
*
|
2003-02-05 18:41:33 +01:00
|
|
|
* I'd appreciate being given credit for this package in the documentation
|
|
|
|
* of software which uses it, but that is not a requirement.
|
2003-08-04 02:43:34 +02:00
|
|
|
*
|
2003-02-05 18:41:33 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
* HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/regex/regc_lex.c
|
2003-02-05 18:41:33 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* scanning macros (know about v) */
|
2003-08-04 02:43:34 +02:00
|
|
|
#define ATEOS() (v->now >= v->stop)
|
|
|
|
#define HAVE(n) (v->stop - v->now >= (n))
|
|
|
|
#define NEXT1(c) (!ATEOS() && *v->now == CHR(c))
|
|
|
|
#define NEXT2(a,b) (HAVE(2) && *v->now == CHR(a) && *(v->now+1) == CHR(b))
|
|
|
|
#define NEXT3(a,b,c) (HAVE(3) && *v->now == CHR(a) && \
|
2003-02-05 18:41:33 +01:00
|
|
|
*(v->now+1) == CHR(b) && \
|
|
|
|
*(v->now+2) == CHR(c))
|
2003-08-04 02:43:34 +02:00
|
|
|
#define SET(c) (v->nexttype = (c))
|
|
|
|
#define SETV(c, n) (v->nexttype = (c), v->nextvalue = (n))
|
|
|
|
#define RET(c) return (SET(c), 1)
|
|
|
|
#define RETV(c, n) return (SETV(c, n), 1)
|
|
|
|
#define FAILW(e) return (ERR(e), 0) /* ERR does SET(EOS) */
|
|
|
|
#define LASTTYPE(t) (v->lasttype == (t))
|
2003-02-05 18:41:33 +01:00
|
|
|
|
|
|
|
/* lexical contexts */
|
2003-08-04 02:43:34 +02:00
|
|
|
#define L_ERE 1 /* mainline ERE/ARE */
|
|
|
|
#define L_BRE 2 /* mainline BRE */
|
|
|
|
#define L_Q 3 /* REG_QUOTE */
|
|
|
|
#define L_EBND 4 /* ERE/ARE bound */
|
|
|
|
#define L_BBND 5 /* BRE bound */
|
|
|
|
#define L_BRACK 6 /* brackets */
|
|
|
|
#define L_CEL 7 /* collating element */
|
|
|
|
#define L_ECL 8 /* equivalence class */
|
|
|
|
#define L_CCL 9 /* character class */
|
|
|
|
#define INTOCON(c) (v->lexcon = (c))
|
|
|
|
#define INCON(con) (v->lexcon == (con))
|
2003-02-05 18:41:33 +01:00
|
|
|
|
|
|
|
/* construct pointer past end of chr array */
|
2003-08-04 02:43:34 +02:00
|
|
|
#define ENDOF(array) ((array) + sizeof(array)/sizeof(chr))
|
2003-02-05 18:41:33 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* lexstart - set up lexical stuff, scan leading options
|
|
|
|
*/
|
|
|
|
static void
|
2017-06-21 20:39:04 +02:00
|
|
|
lexstart(struct vars *v)
|
2003-02-05 18:41:33 +01:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
prefixes(v); /* may turn on new type bits etc. */
|
2003-02-05 18:41:33 +01:00
|
|
|
NOERR();
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
if (v->cflags & REG_QUOTE)
|
|
|
|
{
|
|
|
|
assert(!(v->cflags & (REG_ADVANCED | REG_EXPANDED | REG_NEWLINE)));
|
2003-02-05 18:41:33 +01:00
|
|
|
INTOCON(L_Q);
|
2003-08-04 02:43:34 +02:00
|
|
|
}
|
|
|
|
else if (v->cflags & REG_EXTENDED)
|
|
|
|
{
|
|
|
|
assert(!(v->cflags & REG_QUOTE));
|
2003-02-05 18:41:33 +01:00
|
|
|
INTOCON(L_ERE);
|
2003-08-04 02:43:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(!(v->cflags & (REG_QUOTE | REG_ADVF)));
|
2003-02-05 18:41:33 +01:00
|
|
|
INTOCON(L_BRE);
|
|
|
|
}
|
|
|
|
|
|
|
|
v->nexttype = EMPTY; /* remember we were at the start */
|
2003-08-04 02:43:34 +02:00
|
|
|
next(v); /* set up the first token */
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prefixes - implement various special prefixes
|
|
|
|
*/
|
|
|
|
static void
|
2017-06-21 20:39:04 +02:00
|
|
|
prefixes(struct vars *v)
|
2003-02-05 18:41:33 +01:00
|
|
|
{
|
|
|
|
/* literal string doesn't get any of this stuff */
|
2003-08-04 02:43:34 +02:00
|
|
|
if (v->cflags & REG_QUOTE)
|
2003-02-05 18:41:33 +01:00
|
|
|
return;
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
/* initial "***" gets special things */
|
2003-02-05 18:41:33 +01:00
|
|
|
if (HAVE(4) && NEXT3('*', '*', '*'))
|
2003-08-04 02:43:34 +02:00
|
|
|
switch (*(v->now + 3))
|
|
|
|
{
|
|
|
|
case CHR('?'): /* "***?" error, msg shows version */
|
|
|
|
ERR(REG_BADPAT);
|
|
|
|
return; /* proceed no further */
|
|
|
|
break;
|
|
|
|
case CHR('='): /* "***=" shifts to literal string */
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
v->cflags |= REG_QUOTE;
|
|
|
|
v->cflags &= ~(REG_ADVANCED | REG_EXPANDED | REG_NEWLINE);
|
|
|
|
v->now += 4;
|
|
|
|
return; /* and there can be no more prefixes */
|
|
|
|
break;
|
|
|
|
case CHR(':'): /* "***:" shifts to AREs */
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
v->cflags |= REG_ADVANCED;
|
|
|
|
v->now += 4;
|
|
|
|
break;
|
|
|
|
default: /* otherwise *** is just an error */
|
|
|
|
ERR(REG_BADRPT);
|
|
|
|
return;
|
|
|
|
break;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BREs and EREs don't get embedded options */
|
2003-08-04 02:43:34 +02:00
|
|
|
if ((v->cflags & REG_ADVANCED) != REG_ADVANCED)
|
2003-02-05 18:41:33 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* embedded options (AREs only) */
|
2003-08-04 02:43:34 +02:00
|
|
|
if (HAVE(3) && NEXT2('(', '?') && iscalpha(*(v->now + 2)))
|
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
v->now += 2;
|
|
|
|
for (; !ATEOS() && iscalpha(*v->now); v->now++)
|
2003-08-04 02:43:34 +02:00
|
|
|
switch (*v->now)
|
|
|
|
{
|
|
|
|
case CHR('b'): /* BREs (but why???) */
|
|
|
|
v->cflags &= ~(REG_ADVANCED | REG_QUOTE);
|
|
|
|
break;
|
|
|
|
case CHR('c'): /* case sensitive */
|
|
|
|
v->cflags &= ~REG_ICASE;
|
|
|
|
break;
|
|
|
|
case CHR('e'): /* plain EREs */
|
|
|
|
v->cflags |= REG_EXTENDED;
|
|
|
|
v->cflags &= ~(REG_ADVF | REG_QUOTE);
|
|
|
|
break;
|
|
|
|
case CHR('i'): /* case insensitive */
|
|
|
|
v->cflags |= REG_ICASE;
|
|
|
|
break;
|
|
|
|
case CHR('m'): /* Perloid synonym for n */
|
|
|
|
case CHR('n'): /* \n affects ^ $ . [^ */
|
|
|
|
v->cflags |= REG_NEWLINE;
|
|
|
|
break;
|
|
|
|
case CHR('p'): /* ~Perl, \n affects . [^ */
|
|
|
|
v->cflags |= REG_NLSTOP;
|
|
|
|
v->cflags &= ~REG_NLANCH;
|
|
|
|
break;
|
|
|
|
case CHR('q'): /* literal string */
|
|
|
|
v->cflags |= REG_QUOTE;
|
|
|
|
v->cflags &= ~REG_ADVANCED;
|
|
|
|
break;
|
|
|
|
case CHR('s'): /* single line, \n ordinary */
|
|
|
|
v->cflags &= ~REG_NEWLINE;
|
|
|
|
break;
|
|
|
|
case CHR('t'): /* tight syntax */
|
|
|
|
v->cflags &= ~REG_EXPANDED;
|
|
|
|
break;
|
|
|
|
case CHR('w'): /* weird, \n affects ^ $ only */
|
|
|
|
v->cflags &= ~REG_NLSTOP;
|
|
|
|
v->cflags |= REG_NLANCH;
|
|
|
|
break;
|
|
|
|
case CHR('x'): /* expanded syntax */
|
|
|
|
v->cflags |= REG_EXPANDED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERR(REG_BADOPT);
|
|
|
|
return;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
if (!NEXT1(')'))
|
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
ERR(REG_BADOPT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
v->now++;
|
2003-08-04 02:43:34 +02:00
|
|
|
if (v->cflags & REG_QUOTE)
|
|
|
|
v->cflags &= ~(REG_EXPANDED | REG_NEWLINE);
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lexnest - "call a subroutine", interpolating string at the lexical level
|
|
|
|
*
|
|
|
|
* Note, this is not a very general facility. There are a number of
|
|
|
|
* implicit assumptions about what sorts of strings can be subroutines.
|
|
|
|
*/
|
|
|
|
static void
|
2017-06-21 20:39:04 +02:00
|
|
|
lexnest(struct vars *v,
|
2008-02-14 18:33:37 +01:00
|
|
|
const chr *beginp, /* start of interpolation */
|
|
|
|
const chr *endp) /* one past end of interpolation */
|
2003-02-05 18:41:33 +01:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
assert(v->savenow == NULL); /* only one level of nesting */
|
2003-02-05 18:41:33 +01:00
|
|
|
v->savenow = v->now;
|
|
|
|
v->savestop = v->stop;
|
|
|
|
v->now = beginp;
|
|
|
|
v->stop = endp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* string constants to interpolate as expansions of things like \d
|
|
|
|
*/
|
2009-06-11 16:49:15 +02:00
|
|
|
static const chr backd[] = { /* \d */
|
2003-02-05 18:41:33 +01:00
|
|
|
CHR('['), CHR('['), CHR(':'),
|
|
|
|
CHR('d'), CHR('i'), CHR('g'), CHR('i'), CHR('t'),
|
|
|
|
CHR(':'), CHR(']'), CHR(']')
|
|
|
|
};
|
2009-06-11 16:49:15 +02:00
|
|
|
static const chr backD[] = { /* \D */
|
2003-02-05 18:41:33 +01:00
|
|
|
CHR('['), CHR('^'), CHR('['), CHR(':'),
|
|
|
|
CHR('d'), CHR('i'), CHR('g'), CHR('i'), CHR('t'),
|
|
|
|
CHR(':'), CHR(']'), CHR(']')
|
|
|
|
};
|
2009-06-11 16:49:15 +02:00
|
|
|
static const chr brbackd[] = { /* \d within brackets */
|
2003-02-05 18:41:33 +01:00
|
|
|
CHR('['), CHR(':'),
|
|
|
|
CHR('d'), CHR('i'), CHR('g'), CHR('i'), CHR('t'),
|
|
|
|
CHR(':'), CHR(']')
|
|
|
|
};
|
2009-06-11 16:49:15 +02:00
|
|
|
static const chr backs[] = { /* \s */
|
2003-02-05 18:41:33 +01:00
|
|
|
CHR('['), CHR('['), CHR(':'),
|
|
|
|
CHR('s'), CHR('p'), CHR('a'), CHR('c'), CHR('e'),
|
|
|
|
CHR(':'), CHR(']'), CHR(']')
|
|
|
|
};
|
2009-06-11 16:49:15 +02:00
|
|
|
static const chr backS[] = { /* \S */
|
2003-02-05 18:41:33 +01:00
|
|
|
CHR('['), CHR('^'), CHR('['), CHR(':'),
|
|
|
|
CHR('s'), CHR('p'), CHR('a'), CHR('c'), CHR('e'),
|
|
|
|
CHR(':'), CHR(']'), CHR(']')
|
|
|
|
};
|
2009-06-11 16:49:15 +02:00
|
|
|
static const chr brbacks[] = { /* \s within brackets */
|
2003-02-05 18:41:33 +01:00
|
|
|
CHR('['), CHR(':'),
|
|
|
|
CHR('s'), CHR('p'), CHR('a'), CHR('c'), CHR('e'),
|
|
|
|
CHR(':'), CHR(']')
|
|
|
|
};
|
2009-06-11 16:49:15 +02:00
|
|
|
static const chr backw[] = { /* \w */
|
2003-02-05 18:41:33 +01:00
|
|
|
CHR('['), CHR('['), CHR(':'),
|
|
|
|
CHR('a'), CHR('l'), CHR('n'), CHR('u'), CHR('m'),
|
|
|
|
CHR(':'), CHR(']'), CHR('_'), CHR(']')
|
|
|
|
};
|
2009-06-11 16:49:15 +02:00
|
|
|
static const chr backW[] = { /* \W */
|
2003-02-05 18:41:33 +01:00
|
|
|
CHR('['), CHR('^'), CHR('['), CHR(':'),
|
|
|
|
CHR('a'), CHR('l'), CHR('n'), CHR('u'), CHR('m'),
|
|
|
|
CHR(':'), CHR(']'), CHR('_'), CHR(']')
|
|
|
|
};
|
2009-06-11 16:49:15 +02:00
|
|
|
static const chr brbackw[] = { /* \w within brackets */
|
2003-02-05 18:41:33 +01:00
|
|
|
CHR('['), CHR(':'),
|
|
|
|
CHR('a'), CHR('l'), CHR('n'), CHR('u'), CHR('m'),
|
|
|
|
CHR(':'), CHR(']'), CHR('_')
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lexword - interpolate a bracket expression for word characters
|
|
|
|
* Possibly ought to inquire whether there is a "word" character class.
|
|
|
|
*/
|
|
|
|
static void
|
2017-06-21 20:39:04 +02:00
|
|
|
lexword(struct vars *v)
|
2003-02-05 18:41:33 +01:00
|
|
|
{
|
|
|
|
lexnest(v, backw, ENDOF(backw));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* next - get next token
|
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
static int /* 1 normal, 0 failure */
|
2017-06-21 20:39:04 +02:00
|
|
|
next(struct vars *v)
|
2003-02-05 18:41:33 +01:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
chr c;
|
2003-02-05 18:41:33 +01:00
|
|
|
|
|
|
|
/* errors yield an infinite sequence of failures */
|
|
|
|
if (ISERR())
|
2003-08-04 02:43:34 +02:00
|
|
|
return 0; /* the error has set nexttype to EOS */
|
2003-02-05 18:41:33 +01:00
|
|
|
|
|
|
|
/* remember flavor of last token */
|
|
|
|
v->lasttype = v->nexttype;
|
|
|
|
|
|
|
|
/* REG_BOSONLY */
|
2003-08-04 02:43:34 +02:00
|
|
|
if (v->nexttype == EMPTY && (v->cflags & REG_BOSONLY))
|
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
/* at start of a REG_BOSONLY RE */
|
|
|
|
RETV(SBEGIN, 0); /* same as \A */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we're nested and we've hit end, return to outer level */
|
2003-08-04 02:43:34 +02:00
|
|
|
if (v->savenow != NULL && ATEOS())
|
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
v->now = v->savenow;
|
|
|
|
v->stop = v->savestop;
|
|
|
|
v->savenow = v->savestop = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip white space etc. if appropriate (not in literal or []) */
|
2003-08-04 02:43:34 +02:00
|
|
|
if (v->cflags & REG_EXPANDED)
|
|
|
|
switch (v->lexcon)
|
|
|
|
{
|
|
|
|
case L_ERE:
|
|
|
|
case L_BRE:
|
|
|
|
case L_EBND:
|
|
|
|
case L_BBND:
|
|
|
|
skip(v);
|
|
|
|
break;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* handle EOS, depending on context */
|
2003-08-04 02:43:34 +02:00
|
|
|
if (ATEOS())
|
|
|
|
{
|
|
|
|
switch (v->lexcon)
|
|
|
|
{
|
|
|
|
case L_ERE:
|
|
|
|
case L_BRE:
|
|
|
|
case L_Q:
|
|
|
|
RET(EOS);
|
|
|
|
break;
|
|
|
|
case L_EBND:
|
|
|
|
case L_BBND:
|
|
|
|
FAILW(REG_EBRACE);
|
|
|
|
break;
|
|
|
|
case L_BRACK:
|
|
|
|
case L_CEL:
|
|
|
|
case L_ECL:
|
|
|
|
case L_CCL:
|
|
|
|
FAILW(REG_EBRACK);
|
|
|
|
break;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
assert(NOTREACHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* okay, time to actually get a character */
|
|
|
|
c = *v->now++;
|
|
|
|
|
|
|
|
/* deal with the easy contexts, punt EREs to code below */
|
2003-08-04 02:43:34 +02:00
|
|
|
switch (v->lexcon)
|
|
|
|
{
|
|
|
|
case L_BRE: /* punt BREs to separate function */
|
|
|
|
return brenext(v, c);
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
2003-08-04 02:43:34 +02:00
|
|
|
case L_ERE: /* see below */
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
2003-08-04 02:43:34 +02:00
|
|
|
case L_Q: /* literal strings are easy */
|
|
|
|
RETV(PLAIN, c);
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
2003-08-04 02:43:34 +02:00
|
|
|
case L_BBND: /* bounds are fairly simple */
|
|
|
|
case L_EBND:
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case CHR('0'):
|
|
|
|
case CHR('1'):
|
|
|
|
case CHR('2'):
|
|
|
|
case CHR('3'):
|
|
|
|
case CHR('4'):
|
|
|
|
case CHR('5'):
|
|
|
|
case CHR('6'):
|
|
|
|
case CHR('7'):
|
|
|
|
case CHR('8'):
|
|
|
|
case CHR('9'):
|
|
|
|
RETV(DIGIT, (chr) DIGITVAL(c));
|
|
|
|
break;
|
|
|
|
case CHR(','):
|
|
|
|
RET(',');
|
|
|
|
break;
|
|
|
|
case CHR('}'): /* ERE bound ends with } */
|
|
|
|
if (INCON(L_EBND))
|
|
|
|
{
|
|
|
|
INTOCON(L_ERE);
|
|
|
|
if ((v->cflags & REG_ADVF) && NEXT1('?'))
|
|
|
|
{
|
|
|
|
v->now++;
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
RETV('}', 0);
|
|
|
|
}
|
|
|
|
RETV('}', 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
FAILW(REG_BADBR);
|
|
|
|
break;
|
2017-06-21 20:39:04 +02:00
|
|
|
case CHR('\\'): /* BRE bound ends with \} */
|
2003-08-04 02:43:34 +02:00
|
|
|
if (INCON(L_BBND) && NEXT1('}'))
|
|
|
|
{
|
|
|
|
v->now++;
|
|
|
|
INTOCON(L_BRE);
|
|
|
|
RET('}');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
FAILW(REG_BADBR);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FAILW(REG_BADBR);
|
|
|
|
break;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
assert(NOTREACHED);
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
2003-08-04 02:43:34 +02:00
|
|
|
case L_BRACK: /* brackets are not too hard */
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case CHR(']'):
|
|
|
|
if (LASTTYPE('['))
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
INTOCON((v->cflags & REG_EXTENDED) ?
|
|
|
|
L_ERE : L_BRE);
|
|
|
|
RET(']');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CHR('\\'):
|
|
|
|
NOTE(REG_UBBS);
|
|
|
|
if (!(v->cflags & REG_ADVF))
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
if (ATEOS())
|
|
|
|
FAILW(REG_EESCAPE);
|
|
|
|
(DISCARD) lexescape(v);
|
|
|
|
switch (v->nexttype)
|
|
|
|
{ /* not all escapes okay here */
|
|
|
|
case PLAIN:
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case CCLASS:
|
|
|
|
switch (v->nextvalue)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
lexnest(v, brbackd, ENDOF(brbackd));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
lexnest(v, brbacks, ENDOF(brbacks));
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
lexnest(v, brbackw, ENDOF(brbackw));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FAILW(REG_EESCAPE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* lexnest done, back up and try again */
|
|
|
|
v->nexttype = v->lasttype;
|
|
|
|
return next(v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* not one of the acceptable escapes */
|
|
|
|
FAILW(REG_EESCAPE);
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
2003-08-04 02:43:34 +02:00
|
|
|
case CHR('-'):
|
|
|
|
if (LASTTYPE('[') || NEXT1(']'))
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
else
|
|
|
|
RETV(RANGE, c);
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
2003-08-04 02:43:34 +02:00
|
|
|
case CHR('['):
|
|
|
|
if (ATEOS())
|
|
|
|
FAILW(REG_EBRACK);
|
|
|
|
switch (*v->now++)
|
|
|
|
{
|
|
|
|
case CHR('.'):
|
|
|
|
INTOCON(L_CEL);
|
|
|
|
/* might or might not be locale-specific */
|
|
|
|
RET(COLLEL);
|
|
|
|
break;
|
|
|
|
case CHR('='):
|
|
|
|
INTOCON(L_ECL);
|
|
|
|
NOTE(REG_ULOCALE);
|
|
|
|
RET(ECLASS);
|
|
|
|
break;
|
|
|
|
case CHR(':'):
|
|
|
|
INTOCON(L_CCL);
|
|
|
|
NOTE(REG_ULOCALE);
|
|
|
|
RET(CCLASS);
|
|
|
|
break;
|
2017-06-21 20:39:04 +02:00
|
|
|
default: /* oops */
|
2003-08-04 02:43:34 +02:00
|
|
|
v->now--;
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(NOTREACHED);
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
|
|
|
default:
|
2003-08-04 02:43:34 +02:00
|
|
|
RETV(PLAIN, c);
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
assert(NOTREACHED);
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
2003-08-04 02:43:34 +02:00
|
|
|
case L_CEL: /* collating elements are easy */
|
|
|
|
if (c == CHR('.') && NEXT1(']'))
|
|
|
|
{
|
|
|
|
v->now++;
|
|
|
|
INTOCON(L_BRACK);
|
|
|
|
RETV(END, '.');
|
|
|
|
}
|
2003-02-05 18:41:33 +01:00
|
|
|
else
|
2003-08-04 02:43:34 +02:00
|
|
|
RETV(PLAIN, c);
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
2003-08-04 02:43:34 +02:00
|
|
|
case L_ECL: /* ditto equivalence classes */
|
|
|
|
if (c == CHR('=') && NEXT1(']'))
|
|
|
|
{
|
|
|
|
v->now++;
|
|
|
|
INTOCON(L_BRACK);
|
|
|
|
RETV(END, '=');
|
|
|
|
}
|
|
|
|
else
|
2003-02-05 18:41:33 +01:00
|
|
|
RETV(PLAIN, c);
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
|
|
|
case L_CCL: /* ditto character classes */
|
|
|
|
if (c == CHR(':') && NEXT1(']'))
|
|
|
|
{
|
|
|
|
v->now++;
|
|
|
|
INTOCON(L_BRACK);
|
|
|
|
RETV(END, ':');
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
else
|
|
|
|
RETV(PLAIN, c);
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
|
|
|
default:
|
2003-08-04 02:43:34 +02:00
|
|
|
assert(NOTREACHED);
|
2003-02-05 18:41:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* that got rid of everything except EREs and AREs */
|
|
|
|
assert(INCON(L_ERE));
|
|
|
|
|
|
|
|
/* deal with EREs and AREs, except for backslashes */
|
2003-08-04 02:43:34 +02:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case CHR('|'):
|
|
|
|
RET('|');
|
|
|
|
break;
|
|
|
|
case CHR('*'):
|
|
|
|
if ((v->cflags & REG_ADVF) && NEXT1('?'))
|
|
|
|
{
|
|
|
|
v->now++;
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
RETV('*', 0);
|
|
|
|
}
|
|
|
|
RETV('*', 1);
|
|
|
|
break;
|
|
|
|
case CHR('+'):
|
|
|
|
if ((v->cflags & REG_ADVF) && NEXT1('?'))
|
|
|
|
{
|
|
|
|
v->now++;
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
RETV('+', 0);
|
|
|
|
}
|
|
|
|
RETV('+', 1);
|
|
|
|
break;
|
|
|
|
case CHR('?'):
|
|
|
|
if ((v->cflags & REG_ADVF) && NEXT1('?'))
|
|
|
|
{
|
|
|
|
v->now++;
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
RETV('?', 0);
|
|
|
|
}
|
|
|
|
RETV('?', 1);
|
|
|
|
break;
|
|
|
|
case CHR('{'): /* bounds start or plain character */
|
|
|
|
if (v->cflags & REG_EXPANDED)
|
|
|
|
skip(v);
|
|
|
|
if (ATEOS() || !iscdigit(*v->now))
|
|
|
|
{
|
|
|
|
NOTE(REG_UBRACES);
|
|
|
|
NOTE(REG_UUNSPEC);
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NOTE(REG_UBOUNDS);
|
|
|
|
INTOCON(L_EBND);
|
|
|
|
RET('{');
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
assert(NOTREACHED);
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
|
|
|
case CHR('('): /* parenthesis, or advanced extension */
|
|
|
|
if ((v->cflags & REG_ADVF) && NEXT1('?'))
|
|
|
|
{
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
v->now++;
|
Implement lookbehind constraints in our regular-expression engine.
A lookbehind constraint is like a lookahead constraint in that it consumes
no text; but it checks for existence (or nonexistence) of a match *ending*
at the current point in the string, rather than one *starting* at the
current point. This is a long-requested feature since it exists in many
other regex libraries, but Henry Spencer had never got around to
implementing it in the code we use.
Just making it work is actually pretty trivial; but naive copying of the
logic for lookahead constraints leads to code that often spends O(N^2) time
to scan an N-character string, because we have to run the match engine
from string start to the current probe point each time the constraint is
checked. In typical use-cases a lookbehind constraint will be written at
the start of the regex and hence will need to be checked at every character
--- so O(N^2) work overall. To fix that, I introduced a third copy of the
core DFA matching loop, paralleling the existing longest() and shortest()
loops. This version, matchuntil(), can suspend and resume matching given
a couple of pointers' worth of storage space. So we need only run it
across the string once, stopping at each interesting probe point and then
resuming to advance to the next one.
I also put in an optimization that simplifies one-character lookahead and
lookbehind constraints, such as "(?=x)" or "(?<!\w)", into AHEAD and BEHIND
constraints, which already existed in the engine. This avoids the overhead
of the LACON machinery entirely for these rather common cases.
The net result is that lookbehind constraints run a factor of three or so
slower than Perl's for multi-character constraints, but faster than Perl's
for one-character constraints ... and they work fine for variable-length
constraints, which Perl gives up on entirely. So that's not bad from a
competitive perspective, and there's room for further optimization if
anyone cares. (In reality, raw scan rate across a large input string is
probably not that big a deal for Postgres usage anyway; so I'm happy if
it's linear.)
2015-10-31 00:14:19 +01:00
|
|
|
if (ATEOS())
|
|
|
|
FAILW(REG_BADRPT);
|
2003-08-04 02:43:34 +02:00
|
|
|
switch (*v->now++)
|
|
|
|
{
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
case CHR(':'): /* non-capturing paren */
|
2003-08-04 02:43:34 +02:00
|
|
|
RETV('(', 0);
|
|
|
|
break;
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
case CHR('#'): /* comment */
|
2003-08-04 02:43:34 +02:00
|
|
|
while (!ATEOS() && *v->now != CHR(')'))
|
|
|
|
v->now++;
|
|
|
|
if (!ATEOS())
|
|
|
|
v->now++;
|
|
|
|
assert(v->nexttype == v->lasttype);
|
|
|
|
return next(v);
|
|
|
|
break;
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
case CHR('='): /* positive lookahead */
|
Implement lookbehind constraints in our regular-expression engine.
A lookbehind constraint is like a lookahead constraint in that it consumes
no text; but it checks for existence (or nonexistence) of a match *ending*
at the current point in the string, rather than one *starting* at the
current point. This is a long-requested feature since it exists in many
other regex libraries, but Henry Spencer had never got around to
implementing it in the code we use.
Just making it work is actually pretty trivial; but naive copying of the
logic for lookahead constraints leads to code that often spends O(N^2) time
to scan an N-character string, because we have to run the match engine
from string start to the current probe point each time the constraint is
checked. In typical use-cases a lookbehind constraint will be written at
the start of the regex and hence will need to be checked at every character
--- so O(N^2) work overall. To fix that, I introduced a third copy of the
core DFA matching loop, paralleling the existing longest() and shortest()
loops. This version, matchuntil(), can suspend and resume matching given
a couple of pointers' worth of storage space. So we need only run it
across the string once, stopping at each interesting probe point and then
resuming to advance to the next one.
I also put in an optimization that simplifies one-character lookahead and
lookbehind constraints, such as "(?=x)" or "(?<!\w)", into AHEAD and BEHIND
constraints, which already existed in the engine. This avoids the overhead
of the LACON machinery entirely for these rather common cases.
The net result is that lookbehind constraints run a factor of three or so
slower than Perl's for multi-character constraints, but faster than Perl's
for one-character constraints ... and they work fine for variable-length
constraints, which Perl gives up on entirely. So that's not bad from a
competitive perspective, and there's room for further optimization if
anyone cares. (In reality, raw scan rate across a large input string is
probably not that big a deal for Postgres usage anyway; so I'm happy if
it's linear.)
2015-10-31 00:14:19 +01:00
|
|
|
NOTE(REG_ULOOKAROUND);
|
|
|
|
RETV(LACON, LATYPE_AHEAD_POS);
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
case CHR('!'): /* negative lookahead */
|
Implement lookbehind constraints in our regular-expression engine.
A lookbehind constraint is like a lookahead constraint in that it consumes
no text; but it checks for existence (or nonexistence) of a match *ending*
at the current point in the string, rather than one *starting* at the
current point. This is a long-requested feature since it exists in many
other regex libraries, but Henry Spencer had never got around to
implementing it in the code we use.
Just making it work is actually pretty trivial; but naive copying of the
logic for lookahead constraints leads to code that often spends O(N^2) time
to scan an N-character string, because we have to run the match engine
from string start to the current probe point each time the constraint is
checked. In typical use-cases a lookbehind constraint will be written at
the start of the regex and hence will need to be checked at every character
--- so O(N^2) work overall. To fix that, I introduced a third copy of the
core DFA matching loop, paralleling the existing longest() and shortest()
loops. This version, matchuntil(), can suspend and resume matching given
a couple of pointers' worth of storage space. So we need only run it
across the string once, stopping at each interesting probe point and then
resuming to advance to the next one.
I also put in an optimization that simplifies one-character lookahead and
lookbehind constraints, such as "(?=x)" or "(?<!\w)", into AHEAD and BEHIND
constraints, which already existed in the engine. This avoids the overhead
of the LACON machinery entirely for these rather common cases.
The net result is that lookbehind constraints run a factor of three or so
slower than Perl's for multi-character constraints, but faster than Perl's
for one-character constraints ... and they work fine for variable-length
constraints, which Perl gives up on entirely. So that's not bad from a
competitive perspective, and there's room for further optimization if
anyone cares. (In reality, raw scan rate across a large input string is
probably not that big a deal for Postgres usage anyway; so I'm happy if
it's linear.)
2015-10-31 00:14:19 +01:00
|
|
|
NOTE(REG_ULOOKAROUND);
|
|
|
|
RETV(LACON, LATYPE_AHEAD_NEG);
|
|
|
|
break;
|
|
|
|
case CHR('<'):
|
|
|
|
if (ATEOS())
|
|
|
|
FAILW(REG_BADRPT);
|
|
|
|
switch (*v->now++)
|
|
|
|
{
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
case CHR('='): /* positive lookbehind */
|
Implement lookbehind constraints in our regular-expression engine.
A lookbehind constraint is like a lookahead constraint in that it consumes
no text; but it checks for existence (or nonexistence) of a match *ending*
at the current point in the string, rather than one *starting* at the
current point. This is a long-requested feature since it exists in many
other regex libraries, but Henry Spencer had never got around to
implementing it in the code we use.
Just making it work is actually pretty trivial; but naive copying of the
logic for lookahead constraints leads to code that often spends O(N^2) time
to scan an N-character string, because we have to run the match engine
from string start to the current probe point each time the constraint is
checked. In typical use-cases a lookbehind constraint will be written at
the start of the regex and hence will need to be checked at every character
--- so O(N^2) work overall. To fix that, I introduced a third copy of the
core DFA matching loop, paralleling the existing longest() and shortest()
loops. This version, matchuntil(), can suspend and resume matching given
a couple of pointers' worth of storage space. So we need only run it
across the string once, stopping at each interesting probe point and then
resuming to advance to the next one.
I also put in an optimization that simplifies one-character lookahead and
lookbehind constraints, such as "(?=x)" or "(?<!\w)", into AHEAD and BEHIND
constraints, which already existed in the engine. This avoids the overhead
of the LACON machinery entirely for these rather common cases.
The net result is that lookbehind constraints run a factor of three or so
slower than Perl's for multi-character constraints, but faster than Perl's
for one-character constraints ... and they work fine for variable-length
constraints, which Perl gives up on entirely. So that's not bad from a
competitive perspective, and there's room for further optimization if
anyone cares. (In reality, raw scan rate across a large input string is
probably not that big a deal for Postgres usage anyway; so I'm happy if
it's linear.)
2015-10-31 00:14:19 +01:00
|
|
|
NOTE(REG_ULOOKAROUND);
|
|
|
|
RETV(LACON, LATYPE_BEHIND_POS);
|
|
|
|
break;
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
case CHR('!'): /* negative lookbehind */
|
Implement lookbehind constraints in our regular-expression engine.
A lookbehind constraint is like a lookahead constraint in that it consumes
no text; but it checks for existence (or nonexistence) of a match *ending*
at the current point in the string, rather than one *starting* at the
current point. This is a long-requested feature since it exists in many
other regex libraries, but Henry Spencer had never got around to
implementing it in the code we use.
Just making it work is actually pretty trivial; but naive copying of the
logic for lookahead constraints leads to code that often spends O(N^2) time
to scan an N-character string, because we have to run the match engine
from string start to the current probe point each time the constraint is
checked. In typical use-cases a lookbehind constraint will be written at
the start of the regex and hence will need to be checked at every character
--- so O(N^2) work overall. To fix that, I introduced a third copy of the
core DFA matching loop, paralleling the existing longest() and shortest()
loops. This version, matchuntil(), can suspend and resume matching given
a couple of pointers' worth of storage space. So we need only run it
across the string once, stopping at each interesting probe point and then
resuming to advance to the next one.
I also put in an optimization that simplifies one-character lookahead and
lookbehind constraints, such as "(?=x)" or "(?<!\w)", into AHEAD and BEHIND
constraints, which already existed in the engine. This avoids the overhead
of the LACON machinery entirely for these rather common cases.
The net result is that lookbehind constraints run a factor of three or so
slower than Perl's for multi-character constraints, but faster than Perl's
for one-character constraints ... and they work fine for variable-length
constraints, which Perl gives up on entirely. So that's not bad from a
competitive perspective, and there's room for further optimization if
anyone cares. (In reality, raw scan rate across a large input string is
probably not that big a deal for Postgres usage anyway; so I'm happy if
it's linear.)
2015-10-31 00:14:19 +01:00
|
|
|
NOTE(REG_ULOOKAROUND);
|
|
|
|
RETV(LACON, LATYPE_BEHIND_NEG);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FAILW(REG_BADRPT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(NOTREACHED);
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FAILW(REG_BADRPT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(NOTREACHED);
|
|
|
|
}
|
|
|
|
if (v->cflags & REG_NOSUB)
|
|
|
|
RETV('(', 0); /* all parens non-capturing */
|
|
|
|
else
|
|
|
|
RETV('(', 1);
|
|
|
|
break;
|
|
|
|
case CHR(')'):
|
|
|
|
if (LASTTYPE('('))
|
|
|
|
NOTE(REG_UUNSPEC);
|
|
|
|
RETV(')', c);
|
|
|
|
break;
|
|
|
|
case CHR('['): /* easy except for [[:<:]] and [[:>:]] */
|
|
|
|
if (HAVE(6) && *(v->now + 0) == CHR('[') &&
|
|
|
|
*(v->now + 1) == CHR(':') &&
|
|
|
|
(*(v->now + 2) == CHR('<') ||
|
|
|
|
*(v->now + 2) == CHR('>')) &&
|
|
|
|
*(v->now + 3) == CHR(':') &&
|
|
|
|
*(v->now + 4) == CHR(']') &&
|
|
|
|
*(v->now + 5) == CHR(']'))
|
|
|
|
{
|
|
|
|
c = *(v->now + 2);
|
|
|
|
v->now += 6;
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
RET((c == CHR('<')) ? '<' : '>');
|
|
|
|
}
|
|
|
|
INTOCON(L_BRACK);
|
|
|
|
if (NEXT1('^'))
|
|
|
|
{
|
|
|
|
v->now++;
|
|
|
|
RETV('[', 0);
|
|
|
|
}
|
|
|
|
RETV('[', 1);
|
|
|
|
break;
|
|
|
|
case CHR('.'):
|
|
|
|
RET('.');
|
|
|
|
break;
|
|
|
|
case CHR('^'):
|
|
|
|
RET('^');
|
|
|
|
break;
|
|
|
|
case CHR('$'):
|
|
|
|
RET('$');
|
|
|
|
break;
|
2017-06-21 20:39:04 +02:00
|
|
|
case CHR('\\'): /* mostly punt backslashes to code below */
|
2003-08-04 02:43:34 +02:00
|
|
|
if (ATEOS())
|
|
|
|
FAILW(REG_EESCAPE);
|
|
|
|
break;
|
|
|
|
default: /* ordinary character */
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
break;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ERE/ARE backslash handling; backslash already eaten */
|
|
|
|
assert(!ATEOS());
|
2003-08-04 02:43:34 +02:00
|
|
|
if (!(v->cflags & REG_ADVF))
|
|
|
|
{ /* only AREs have non-trivial escapes */
|
|
|
|
if (iscalnum(*v->now))
|
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
NOTE(REG_UBSALNUM);
|
|
|
|
NOTE(REG_UUNSPEC);
|
|
|
|
}
|
|
|
|
RETV(PLAIN, *v->now++);
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
(DISCARD) lexescape(v);
|
2003-02-05 18:41:33 +01:00
|
|
|
if (ISERR())
|
|
|
|
FAILW(REG_EESCAPE);
|
2003-08-04 02:43:34 +02:00
|
|
|
if (v->nexttype == CCLASS)
|
|
|
|
{ /* fudge at lexical level */
|
|
|
|
switch (v->nextvalue)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
lexnest(v, backd, ENDOF(backd));
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
lexnest(v, backD, ENDOF(backD));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
lexnest(v, backs, ENDOF(backs));
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
lexnest(v, backS, ENDOF(backS));
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
lexnest(v, backw, ENDOF(backw));
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
lexnest(v, backW, ENDOF(backW));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(NOTREACHED);
|
|
|
|
FAILW(REG_ASSERT);
|
|
|
|
break;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
/* lexnest done, back up and try again */
|
|
|
|
v->nexttype = v->lasttype;
|
|
|
|
return next(v);
|
|
|
|
}
|
|
|
|
/* otherwise, lexescape has already done the work */
|
|
|
|
return !ISERR();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lexescape - parse an ARE backslash escape (backslash already eaten)
|
|
|
|
* Note slightly nonstandard use of the CCLASS type code.
|
|
|
|
*/
|
2005-10-15 04:49:52 +02:00
|
|
|
static int /* not actually used, but convenient for RETV */
|
2017-06-21 20:39:04 +02:00
|
|
|
lexescape(struct vars *v)
|
2003-02-05 18:41:33 +01:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
chr c;
|
2014-01-18 22:04:11 +01:00
|
|
|
static const chr alert[] = {
|
2003-02-05 18:41:33 +01:00
|
|
|
CHR('a'), CHR('l'), CHR('e'), CHR('r'), CHR('t')
|
|
|
|
};
|
2014-01-18 22:04:11 +01:00
|
|
|
static const chr esc[] = {
|
2003-02-05 18:41:33 +01:00
|
|
|
CHR('E'), CHR('S'), CHR('C')
|
|
|
|
};
|
2008-02-14 18:33:37 +01:00
|
|
|
const chr *save;
|
2003-02-05 18:41:33 +01:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
assert(v->cflags & REG_ADVF);
|
2003-02-05 18:41:33 +01:00
|
|
|
|
|
|
|
assert(!ATEOS());
|
|
|
|
c = *v->now++;
|
|
|
|
if (!iscalnum(c))
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
|
|
|
|
NOTE(REG_UNONPOSIX);
|
2003-08-04 02:43:34 +02:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case CHR('a'):
|
|
|
|
RETV(PLAIN, chrnamed(v, alert, ENDOF(alert), CHR('\007')));
|
|
|
|
break;
|
|
|
|
case CHR('A'):
|
|
|
|
RETV(SBEGIN, 0);
|
|
|
|
break;
|
|
|
|
case CHR('b'):
|
|
|
|
RETV(PLAIN, CHR('\b'));
|
|
|
|
break;
|
|
|
|
case CHR('B'):
|
|
|
|
RETV(PLAIN, CHR('\\'));
|
|
|
|
break;
|
|
|
|
case CHR('c'):
|
|
|
|
NOTE(REG_UUNPORT);
|
|
|
|
if (ATEOS())
|
|
|
|
FAILW(REG_EESCAPE);
|
|
|
|
RETV(PLAIN, (chr) (*v->now++ & 037));
|
|
|
|
break;
|
|
|
|
case CHR('d'):
|
|
|
|
NOTE(REG_ULOCALE);
|
|
|
|
RETV(CCLASS, 'd');
|
|
|
|
break;
|
|
|
|
case CHR('D'):
|
|
|
|
NOTE(REG_ULOCALE);
|
|
|
|
RETV(CCLASS, 'D');
|
|
|
|
break;
|
|
|
|
case CHR('e'):
|
|
|
|
NOTE(REG_UUNPORT);
|
|
|
|
RETV(PLAIN, chrnamed(v, esc, ENDOF(esc), CHR('\033')));
|
|
|
|
break;
|
|
|
|
case CHR('f'):
|
|
|
|
RETV(PLAIN, CHR('\f'));
|
|
|
|
break;
|
|
|
|
case CHR('m'):
|
|
|
|
RET('<');
|
|
|
|
break;
|
|
|
|
case CHR('M'):
|
|
|
|
RET('>');
|
|
|
|
break;
|
|
|
|
case CHR('n'):
|
|
|
|
RETV(PLAIN, CHR('\n'));
|
|
|
|
break;
|
|
|
|
case CHR('r'):
|
|
|
|
RETV(PLAIN, CHR('\r'));
|
|
|
|
break;
|
|
|
|
case CHR('s'):
|
|
|
|
NOTE(REG_ULOCALE);
|
|
|
|
RETV(CCLASS, 's');
|
|
|
|
break;
|
|
|
|
case CHR('S'):
|
|
|
|
NOTE(REG_ULOCALE);
|
|
|
|
RETV(CCLASS, 'S');
|
|
|
|
break;
|
|
|
|
case CHR('t'):
|
|
|
|
RETV(PLAIN, CHR('\t'));
|
|
|
|
break;
|
|
|
|
case CHR('u'):
|
|
|
|
c = lexdigits(v, 16, 4, 4);
|
2016-02-15 23:11:51 +01:00
|
|
|
if (ISERR() || !CHR_IS_IN_RANGE(c))
|
2003-08-04 02:43:34 +02:00
|
|
|
FAILW(REG_EESCAPE);
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
break;
|
|
|
|
case CHR('U'):
|
|
|
|
c = lexdigits(v, 16, 8, 8);
|
2016-02-15 23:11:51 +01:00
|
|
|
if (ISERR() || !CHR_IS_IN_RANGE(c))
|
2003-08-04 02:43:34 +02:00
|
|
|
FAILW(REG_EESCAPE);
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
break;
|
|
|
|
case CHR('v'):
|
|
|
|
RETV(PLAIN, CHR('\v'));
|
|
|
|
break;
|
|
|
|
case CHR('w'):
|
|
|
|
NOTE(REG_ULOCALE);
|
|
|
|
RETV(CCLASS, 'w');
|
|
|
|
break;
|
|
|
|
case CHR('W'):
|
|
|
|
NOTE(REG_ULOCALE);
|
|
|
|
RETV(CCLASS, 'W');
|
|
|
|
break;
|
|
|
|
case CHR('x'):
|
|
|
|
NOTE(REG_UUNPORT);
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
c = lexdigits(v, 16, 1, 255); /* REs >255 long outside spec */
|
2016-02-15 23:11:51 +01:00
|
|
|
if (ISERR() || !CHR_IS_IN_RANGE(c))
|
2003-08-04 02:43:34 +02:00
|
|
|
FAILW(REG_EESCAPE);
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
break;
|
|
|
|
case CHR('y'):
|
|
|
|
NOTE(REG_ULOCALE);
|
|
|
|
RETV(WBDRY, 0);
|
|
|
|
break;
|
|
|
|
case CHR('Y'):
|
|
|
|
NOTE(REG_ULOCALE);
|
|
|
|
RETV(NWBDRY, 0);
|
|
|
|
break;
|
|
|
|
case CHR('Z'):
|
|
|
|
RETV(SEND, 0);
|
|
|
|
break;
|
|
|
|
case CHR('1'):
|
|
|
|
case CHR('2'):
|
|
|
|
case CHR('3'):
|
|
|
|
case CHR('4'):
|
|
|
|
case CHR('5'):
|
|
|
|
case CHR('6'):
|
|
|
|
case CHR('7'):
|
|
|
|
case CHR('8'):
|
|
|
|
case CHR('9'):
|
|
|
|
save = v->now;
|
|
|
|
v->now--; /* put first digit back */
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
c = lexdigits(v, 10, 1, 255); /* REs >255 long outside spec */
|
2003-08-04 02:43:34 +02:00
|
|
|
if (ISERR())
|
|
|
|
FAILW(REG_EESCAPE);
|
|
|
|
/* ugly heuristic (first test is "exactly 1 digit?") */
|
2010-10-29 21:51:52 +02:00
|
|
|
if (v->now == save || ((int) c > 0 && (int) c <= v->nsubexp))
|
2003-08-04 02:43:34 +02:00
|
|
|
{
|
|
|
|
NOTE(REG_UBACKREF);
|
2016-08-19 18:51:02 +02:00
|
|
|
RETV(BACKREF, c);
|
2003-08-04 02:43:34 +02:00
|
|
|
}
|
|
|
|
/* oops, doesn't look like it's a backref after all... */
|
|
|
|
v->now = save;
|
|
|
|
/* and fall through into octal number */
|
|
|
|
case CHR('0'):
|
|
|
|
NOTE(REG_UUNPORT);
|
|
|
|
v->now--; /* put first digit back */
|
|
|
|
c = lexdigits(v, 8, 1, 3);
|
|
|
|
if (ISERR())
|
|
|
|
FAILW(REG_EESCAPE);
|
2015-09-16 21:25:25 +02:00
|
|
|
if (c > 0xff)
|
|
|
|
{
|
|
|
|
/* out of range, so we handled one digit too much */
|
|
|
|
v->now--;
|
|
|
|
c >>= 3;
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
RETV(PLAIN, c);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(iscalpha(c));
|
|
|
|
FAILW(REG_EESCAPE); /* unknown alphabetic escape */
|
|
|
|
break;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
assert(NOTREACHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lexdigits - slurp up digits and return chr value
|
Fix some regex issues with out-of-range characters and large char ranges.
Previously, our regex code defined CHR_MAX as 0xfffffffe, which is a
bad choice because it is outside the range of type "celt" (int32).
Characters approaching that limit could lead to infinite loops in logic
such as "for (c = a; c <= b; c++)" where c is of type celt but the
range bounds are chr. Such loops will work safely only if CHR_MAX+1
is representable in celt, since c must advance to beyond b before the
loop will exit.
Fortunately, there seems no reason not to restrict CHR_MAX to 0x7ffffffe.
It's highly unlikely that Unicode will ever assign codes that high, and
none of our other backend encodings need characters beyond that either.
In addition to modifying the macro, we have to explicitly enforce character
range restrictions on the values of \u, \U, and \x escape sequences, else
the limit is trivially bypassed.
Also, the code for expanding case-independent character ranges in bracket
expressions had a potential integer overflow in its calculation of the
number of characters it could generate, which could lead to allocating too
small a character vector and then overwriting memory. An attacker with the
ability to supply arbitrary regex patterns could easily cause transient DOS
via server crashes, and the possibility for privilege escalation has not
been ruled out.
Quite aside from the integer-overflow problem, the range expansion code was
unnecessarily inefficient in that it always produced a result consisting of
individual characters, abandoning the knowledge that we had a range to
start with. If the input range is large, this requires excessive memory.
Change it so that the original range is reported as-is, and then we add on
any case-equivalent characters that are outside that range. With this
approach, we can bound the number of individual characters allowed without
sacrificing much. This patch allows at most 100000 individual characters,
which I believe to be more than the number of case pairs existing in
Unicode, so that the restriction will never be hit in practice.
It's still possible for range() to take awhile given a large character code
range, so also add statement-cancel detection to its loop. The downstream
function dovec() also lacked cancel detection, and could take a long time
given a large output from range().
Per fuzz testing by Greg Stark. Back-patch to all supported branches.
Security: CVE-2016-0773
2016-02-08 16:25:40 +01:00
|
|
|
*
|
|
|
|
* This does not account for overflow; callers should range-check the result
|
|
|
|
* if maxlen is large enough to make that possible.
|
2003-02-05 18:41:33 +01:00
|
|
|
*/
|
2003-08-08 23:42:59 +02:00
|
|
|
static chr /* chr value; errors signalled via ERR */
|
2017-06-21 20:39:04 +02:00
|
|
|
lexdigits(struct vars *v,
|
2003-02-05 18:41:33 +01:00
|
|
|
int base,
|
|
|
|
int minlen,
|
|
|
|
int maxlen)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
uchr n; /* unsigned to avoid overflow misbehavior */
|
|
|
|
int len;
|
|
|
|
chr c;
|
|
|
|
int d;
|
|
|
|
const uchr ub = (uchr) base;
|
2003-02-05 18:41:33 +01:00
|
|
|
|
|
|
|
n = 0;
|
2003-08-04 02:43:34 +02:00
|
|
|
for (len = 0; len < maxlen && !ATEOS(); len++)
|
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
c = *v->now++;
|
2003-08-04 02:43:34 +02:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case CHR('0'):
|
|
|
|
case CHR('1'):
|
|
|
|
case CHR('2'):
|
|
|
|
case CHR('3'):
|
|
|
|
case CHR('4'):
|
|
|
|
case CHR('5'):
|
|
|
|
case CHR('6'):
|
|
|
|
case CHR('7'):
|
|
|
|
case CHR('8'):
|
|
|
|
case CHR('9'):
|
|
|
|
d = DIGITVAL(c);
|
|
|
|
break;
|
|
|
|
case CHR('a'):
|
|
|
|
case CHR('A'):
|
|
|
|
d = 10;
|
|
|
|
break;
|
|
|
|
case CHR('b'):
|
|
|
|
case CHR('B'):
|
|
|
|
d = 11;
|
|
|
|
break;
|
|
|
|
case CHR('c'):
|
|
|
|
case CHR('C'):
|
|
|
|
d = 12;
|
|
|
|
break;
|
|
|
|
case CHR('d'):
|
|
|
|
case CHR('D'):
|
|
|
|
d = 13;
|
|
|
|
break;
|
|
|
|
case CHR('e'):
|
|
|
|
case CHR('E'):
|
|
|
|
d = 14;
|
|
|
|
break;
|
|
|
|
case CHR('f'):
|
|
|
|
case CHR('F'):
|
|
|
|
d = 15;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
v->now--; /* oops, not a digit at all */
|
|
|
|
d = -1;
|
|
|
|
break;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
if (d >= base)
|
|
|
|
{ /* not a plausible digit */
|
2003-02-05 18:41:33 +01:00
|
|
|
v->now--;
|
|
|
|
d = -1;
|
|
|
|
}
|
|
|
|
if (d < 0)
|
2003-08-04 02:43:34 +02:00
|
|
|
break; /* NOTE BREAK OUT */
|
|
|
|
n = n * ub + (uchr) d;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
if (len < minlen)
|
|
|
|
ERR(REG_EESCAPE);
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
return (chr) n;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* brenext - get next BRE token
|
|
|
|
*
|
|
|
|
* This is much like EREs except for all the stupid backslashes and the
|
|
|
|
* context-dependency of some things.
|
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
static int /* 1 normal, 0 failure */
|
2017-06-21 20:39:04 +02:00
|
|
|
brenext(struct vars *v,
|
2016-08-19 18:51:02 +02:00
|
|
|
chr c)
|
2003-02-05 18:41:33 +01:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case CHR('*'):
|
|
|
|
if (LASTTYPE(EMPTY) || LASTTYPE('(') || LASTTYPE('^'))
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
RET('*');
|
|
|
|
break;
|
|
|
|
case CHR('['):
|
|
|
|
if (HAVE(6) && *(v->now + 0) == CHR('[') &&
|
|
|
|
*(v->now + 1) == CHR(':') &&
|
|
|
|
(*(v->now + 2) == CHR('<') ||
|
|
|
|
*(v->now + 2) == CHR('>')) &&
|
|
|
|
*(v->now + 3) == CHR(':') &&
|
|
|
|
*(v->now + 4) == CHR(']') &&
|
|
|
|
*(v->now + 5) == CHR(']'))
|
|
|
|
{
|
|
|
|
c = *(v->now + 2);
|
|
|
|
v->now += 6;
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
RET((c == CHR('<')) ? '<' : '>');
|
|
|
|
}
|
|
|
|
INTOCON(L_BRACK);
|
|
|
|
if (NEXT1('^'))
|
|
|
|
{
|
|
|
|
v->now++;
|
|
|
|
RETV('[', 0);
|
|
|
|
}
|
|
|
|
RETV('[', 1);
|
|
|
|
break;
|
|
|
|
case CHR('.'):
|
|
|
|
RET('.');
|
|
|
|
break;
|
|
|
|
case CHR('^'):
|
|
|
|
if (LASTTYPE(EMPTY))
|
|
|
|
RET('^');
|
|
|
|
if (LASTTYPE('('))
|
|
|
|
{
|
|
|
|
NOTE(REG_UUNSPEC);
|
|
|
|
RET('^');
|
|
|
|
}
|
2003-02-05 18:41:33 +01:00
|
|
|
RETV(PLAIN, c);
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
|
|
|
case CHR('$'):
|
|
|
|
if (v->cflags & REG_EXPANDED)
|
|
|
|
skip(v);
|
|
|
|
if (ATEOS())
|
|
|
|
RET('$');
|
|
|
|
if (NEXT2('\\', ')'))
|
|
|
|
{
|
|
|
|
NOTE(REG_UUNSPEC);
|
|
|
|
RET('$');
|
|
|
|
}
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
break;
|
|
|
|
case CHR('\\'):
|
|
|
|
break; /* see below */
|
|
|
|
default:
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
break;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(c == CHR('\\'));
|
|
|
|
|
|
|
|
if (ATEOS())
|
|
|
|
FAILW(REG_EESCAPE);
|
|
|
|
|
|
|
|
c = *v->now++;
|
2003-08-04 02:43:34 +02:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case CHR('{'):
|
|
|
|
INTOCON(L_BBND);
|
|
|
|
NOTE(REG_UBOUNDS);
|
|
|
|
RET('{');
|
|
|
|
break;
|
|
|
|
case CHR('('):
|
|
|
|
RETV('(', 1);
|
|
|
|
break;
|
|
|
|
case CHR(')'):
|
|
|
|
RETV(')', c);
|
|
|
|
break;
|
|
|
|
case CHR('<'):
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
RET('<');
|
|
|
|
break;
|
|
|
|
case CHR('>'):
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
RET('>');
|
|
|
|
break;
|
|
|
|
case CHR('1'):
|
|
|
|
case CHR('2'):
|
|
|
|
case CHR('3'):
|
|
|
|
case CHR('4'):
|
|
|
|
case CHR('5'):
|
|
|
|
case CHR('6'):
|
|
|
|
case CHR('7'):
|
|
|
|
case CHR('8'):
|
|
|
|
case CHR('9'):
|
|
|
|
NOTE(REG_UBACKREF);
|
|
|
|
RETV(BACKREF, (chr) DIGITVAL(c));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (iscalnum(c))
|
|
|
|
{
|
|
|
|
NOTE(REG_UBSALNUM);
|
|
|
|
NOTE(REG_UUNSPEC);
|
|
|
|
}
|
|
|
|
RETV(PLAIN, c);
|
|
|
|
break;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(NOTREACHED);
|
2007-10-22 03:02:22 +02:00
|
|
|
return 0;
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* skip - skip white space and comments in expanded form
|
|
|
|
*/
|
|
|
|
static void
|
2017-06-21 20:39:04 +02:00
|
|
|
skip(struct vars *v)
|
2003-02-05 18:41:33 +01:00
|
|
|
{
|
2008-02-14 18:33:37 +01:00
|
|
|
const chr *start = v->now;
|
2003-02-05 18:41:33 +01:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
assert(v->cflags & REG_EXPANDED);
|
2003-02-05 18:41:33 +01:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
while (!ATEOS() && iscspace(*v->now))
|
|
|
|
v->now++;
|
|
|
|
if (ATEOS() || *v->now != CHR('#'))
|
|
|
|
break; /* NOTE BREAK OUT */
|
|
|
|
assert(NEXT1('#'));
|
|
|
|
while (!ATEOS() && *v->now != CHR('\n'))
|
|
|
|
v->now++;
|
|
|
|
/* leave the newline to be picked up by the iscspace loop */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->now != start)
|
|
|
|
NOTE(REG_UNONPOSIX);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* newline - return the chr for a newline
|
|
|
|
*
|
|
|
|
* This helps confine use of CHR to this source file.
|
|
|
|
*/
|
|
|
|
static chr
|
|
|
|
newline(void)
|
|
|
|
{
|
|
|
|
return CHR('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* chrnamed - return the chr known by a given (chr string) name
|
|
|
|
*
|
|
|
|
* The code is a bit clumsy, but this routine gets only such specialized
|
|
|
|
* use that it hardly matters.
|
|
|
|
*/
|
|
|
|
static chr
|
2017-06-21 20:39:04 +02:00
|
|
|
chrnamed(struct vars *v,
|
2008-02-14 18:33:37 +01:00
|
|
|
const chr *startp, /* start of name */
|
|
|
|
const chr *endp, /* just past end of name */
|
2003-02-05 18:41:33 +01:00
|
|
|
chr lastresort) /* what to return if name lookup fails */
|
|
|
|
{
|
2016-08-19 18:51:02 +02:00
|
|
|
chr c;
|
2003-08-04 02:43:34 +02:00
|
|
|
int errsave;
|
|
|
|
int e;
|
2003-02-05 18:41:33 +01:00
|
|
|
struct cvec *cv;
|
|
|
|
|
|
|
|
errsave = v->err;
|
|
|
|
v->err = 0;
|
|
|
|
c = element(v, startp, endp);
|
|
|
|
e = v->err;
|
|
|
|
v->err = errsave;
|
|
|
|
|
|
|
|
if (e != 0)
|
2016-08-19 18:51:02 +02:00
|
|
|
return lastresort;
|
2003-02-05 18:41:33 +01:00
|
|
|
|
|
|
|
cv = range(v, c, c, 0);
|
|
|
|
if (cv->nchrs == 0)
|
2016-08-19 18:51:02 +02:00
|
|
|
return lastresort;
|
2003-02-05 18:41:33 +01:00
|
|
|
return cv->chrs[0];
|
|
|
|
}
|