2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-22 10:00:20 +05:30
|
|
|
/* regexp.c */
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "regexp.h"
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
|
2000-06-29 03:29:31 +05:30
|
|
|
#define NSUBEXP 10
|
|
|
|
typedef struct regexp {
|
|
|
|
char *startp[NSUBEXP];
|
|
|
|
char *endp[NSUBEXP];
|
|
|
|
int minlen; /* length of shortest possible match */
|
|
|
|
char first; /* first character, if known; else \0 */
|
|
|
|
char bol; /* boolean: must start at beginning of line? */
|
|
|
|
char program[1]; /* Unwarranted chumminess with compiler. */
|
|
|
|
} regexp;
|
|
|
|
|
|
|
|
|
|
|
|
static regexp *regcomp(char* text);
|
|
|
|
static int regexec(struct regexp* re, char* str, int bol, int ignoreCase);
|
|
|
|
static void regsub(struct regexp* re, char* src, char* dst);
|
|
|
|
|
1999-12-03 14:49:54 +05:30
|
|
|
#if ( defined BB_GREP || defined BB_SED)
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
/* This also tries to find a needle in a haystack, but uses
|
|
|
|
* real regular expressions.... The fake regular expression
|
|
|
|
* version of find_match lives in utility.c. Using this version
|
|
|
|
* will add 3.9k to busybox...
|
|
|
|
* -Erik Andersen
|
|
|
|
*/
|
|
|
|
extern int find_match(char *haystack, char *needle, int ignoreCase)
|
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
int status;
|
|
|
|
struct regexp *re;
|
|
|
|
|
|
|
|
re = regcomp(needle);
|
|
|
|
status = regexec(re, haystack, FALSE, ignoreCase);
|
|
|
|
free(re);
|
|
|
|
return (status);
|
1999-10-22 10:00:20 +05:30
|
|
|
}
|
|
|
|
|
1999-10-30 04:39:13 +05:30
|
|
|
#if defined BB_SED
|
1999-10-29 05:37:31 +05:30
|
|
|
/* This performs substitutions after a regexp match has been found.
|
|
|
|
* The new string is returned. It is malloc'ed, and do must be freed. */
|
2000-02-09 01:28:47 +05:30
|
|
|
extern int replace_match(char *haystack, char *needle, char *newNeedle,
|
|
|
|
int ignoreCase)
|
1999-10-29 05:37:31 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
int status;
|
|
|
|
struct regexp *re;
|
|
|
|
char *s, buf[BUF_SIZE], *d = buf;
|
|
|
|
|
|
|
|
re = regcomp(needle);
|
|
|
|
status = regexec(re, haystack, FALSE, ignoreCase);
|
|
|
|
if (status == TRUE) {
|
|
|
|
s = haystack;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* copy stuff from before the match */
|
|
|
|
while (s < re->startp[0])
|
|
|
|
*d++ = *s++;
|
|
|
|
/* substitute for the matched part */
|
|
|
|
regsub(re, newNeedle, d);
|
|
|
|
s = re->endp[0];
|
|
|
|
d += strlen(d);
|
|
|
|
} while (regexec(re, s, FALSE, ignoreCase) == TRUE);
|
|
|
|
/* copy stuff from after the match */
|
|
|
|
while ((*d++ = *s++)) {
|
|
|
|
}
|
|
|
|
d[0] = '\0';
|
|
|
|
strcpy(haystack, buf);
|
|
|
|
}
|
|
|
|
free(re);
|
|
|
|
return (status);
|
1999-10-29 05:37:31 +05:30
|
|
|
}
|
1999-10-30 04:39:13 +05:30
|
|
|
#endif
|
1999-10-29 05:37:31 +05:30
|
|
|
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
/* code swiped from elvis-tiny 1.4 (a clone of vi) and adjusted to
|
|
|
|
* suit the needs of busybox by Erik Andersen.
|
|
|
|
*
|
|
|
|
* From the README:
|
|
|
|
* "Elvis is freely redistributable, in either source form or executable form.
|
|
|
|
* There are no restrictions on how you may use it".
|
|
|
|
* Elvis was written by Steve Kirkendall <kirkenda@cs.pdx.edu>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This file contains the code that compiles regular expressions and executes
|
|
|
|
* them. It supports the same syntax and features as vi's regular expression
|
|
|
|
* code. Specifically, the meta characters are:
|
|
|
|
* ^ matches the beginning of a line
|
|
|
|
* $ matches the end of a line
|
|
|
|
* \< matches the beginning of a word
|
|
|
|
* \> matches the end of a word
|
|
|
|
* . matches any single character
|
|
|
|
* [] matches any character in a character class
|
|
|
|
* \( delimits the start of a subexpression
|
|
|
|
* \) delimits the end of a subexpression
|
|
|
|
* * repeats the preceding 0 or more times
|
|
|
|
* NOTE: You cannot follow a \) with a *.
|
|
|
|
*
|
|
|
|
* The physical structure of a compiled RE is as follows:
|
|
|
|
* - First, there is a one-byte value that says how many character classes
|
|
|
|
* are used in this regular expression
|
|
|
|
* - Next, each character class is stored as a bitmap that is 256 bits
|
|
|
|
* (32 bytes) long.
|
|
|
|
* - A mixture of literal characters and compiled meta characters follows.
|
|
|
|
* This begins with M_BEGIN(0) and ends with M_END(0). All meta chars
|
|
|
|
* are stored as a \n followed by a one-byte code, so they take up two
|
|
|
|
* bytes apiece. Literal characters take up one byte apiece. \n can't
|
|
|
|
* be used as a literal character.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
static char *previous; /* the previous regexp, used when null regexp is given */
|
|
|
|
|
1999-12-09 04:49:36 +05:30
|
|
|
#if defined BB_SED
|
2000-02-09 01:28:47 +05:30
|
|
|
static char *previous1; /* a copy of the text from the previous substitution for regsub() */
|
1999-12-09 04:49:36 +05:30
|
|
|
#endif
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
|
|
|
|
/* These are used to classify or recognize meta-characters */
|
|
|
|
#define META '\0'
|
|
|
|
#define BASE_META(m) ((m) - 256)
|
|
|
|
#define INT_META(c) ((c) + 256)
|
|
|
|
#define IS_META(m) ((m) >= 256)
|
|
|
|
#define IS_CLASS(m) ((m) >= M_CLASS(0) && (m) <= M_CLASS(9))
|
|
|
|
#define IS_START(m) ((m) >= M_START(0) && (m) <= M_START(9))
|
|
|
|
#define IS_END(m) ((m) >= M_END(0) && (m) <= M_END(9))
|
|
|
|
#define IS_CLOSURE(m) ((m) >= M_SPLAT && (m) <= M_QMARK)
|
|
|
|
#define ADD_META(s,m) (*(s)++ = META, *(s)++ = BASE_META(m))
|
|
|
|
#define GET_META(s) (*(s) == META ? INT_META(*++(s)) : *s)
|
|
|
|
|
|
|
|
/* These are the internal codes used for each type of meta-character */
|
2000-02-09 01:28:47 +05:30
|
|
|
#define M_BEGLINE 256 /* internal code for ^ */
|
|
|
|
#define M_ENDLINE 257 /* internal code for $ */
|
|
|
|
#define M_BEGWORD 258 /* internal code for \< */
|
|
|
|
#define M_ENDWORD 259 /* internal code for \> */
|
|
|
|
#define M_ANY 260 /* internal code for . */
|
|
|
|
#define M_SPLAT 261 /* internal code for * */
|
|
|
|
#define M_PLUS 262 /* internal code for \+ */
|
|
|
|
#define M_QMARK 263 /* internal code for \? */
|
1999-10-22 10:00:20 +05:30
|
|
|
#define M_CLASS(n) (264+(n)) /* internal code for [] */
|
|
|
|
#define M_START(n) (274+(n)) /* internal code for \( */
|
|
|
|
#define M_END(n) (284+(n)) /* internal code for \) */
|
|
|
|
|
|
|
|
/* These are used during compilation */
|
2000-02-09 01:28:47 +05:30
|
|
|
static int class_cnt; /* used to assign class IDs */
|
|
|
|
static int start_cnt; /* used to assign start IDs */
|
|
|
|
static int end_stk[NSUBEXP]; /* used to assign end IDs */
|
|
|
|
static int end_sp;
|
|
|
|
static char *retext; /* points to the text being compiled */
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
/* error-handling stuff */
|
2000-02-09 01:28:47 +05:30
|
|
|
jmp_buf errorhandler;
|
|
|
|
|
1999-11-13 10:17:09 +05:30
|
|
|
#define FAIL(why) do {fprintf(stderr, why); longjmp(errorhandler, 1);} while (0)
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This function builds a bitmap for a particular class */
|
|
|
|
/* text -- start of the class */
|
|
|
|
/* bmap -- the bitmap */
|
2000-02-09 01:28:47 +05:30
|
|
|
static char *makeclass(char *text, char *bmap)
|
1999-10-22 10:00:20 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
int i;
|
|
|
|
int complement = 0;
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
|
|
|
|
/* zero the bitmap */
|
2000-02-09 01:28:47 +05:30
|
|
|
for (i = 0; bmap && i < 32; i++) {
|
1999-10-22 10:00:20 +05:30
|
|
|
bmap[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* see if we're going to complement this class */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (*text == '^') {
|
1999-10-22 10:00:20 +05:30
|
|
|
text++;
|
|
|
|
complement = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add in the characters */
|
2000-02-09 01:28:47 +05:30
|
|
|
while (*text && *text != ']') {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* is this a span of characters? */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (text[1] == '-' && text[2]) {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* spans can't be backwards */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (text[0] > text[2]) {
|
1999-10-22 10:00:20 +05:30
|
|
|
FAIL("Backwards span in []");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add each character in the span to the bitmap */
|
2000-02-09 01:28:47 +05:30
|
|
|
for (i = text[0]; bmap && i <= text[2]; i++) {
|
1999-10-22 10:00:20 +05:30
|
|
|
bmap[i >> 3] |= (1 << (i & 7));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move past this span */
|
|
|
|
text += 3;
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* add this single character to the span */
|
|
|
|
i = *text++;
|
2000-02-09 01:28:47 +05:30
|
|
|
if (bmap) {
|
1999-10-22 10:00:20 +05:30
|
|
|
bmap[i >> 3] |= (1 << (i & 7));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure the closing ] is missing */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (*text++ != ']') {
|
1999-10-22 10:00:20 +05:30
|
|
|
FAIL("] missing");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we're supposed to complement this class, then do so */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (complement && bmap) {
|
|
|
|
for (i = 0; i < 32; i++) {
|
1999-10-22 10:00:20 +05:30
|
|
|
bmap[i] = ~bmap[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This function gets the next character or meta character from a string.
|
|
|
|
* The pointer is incremented by 1, or by 2 for \-quoted characters. For [],
|
|
|
|
* a bitmap is generated via makeclass() (if re is given), and the
|
|
|
|
* character-class text is skipped.
|
|
|
|
*/
|
|
|
|
static int gettoken(sptr, re)
|
2000-02-09 01:28:47 +05:30
|
|
|
char **sptr;
|
|
|
|
regexp *re;
|
1999-10-22 10:00:20 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
int c;
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
c = **sptr;
|
|
|
|
++*sptr;
|
2000-02-09 01:28:47 +05:30
|
|
|
if (c == '\\') {
|
1999-10-22 10:00:20 +05:30
|
|
|
c = **sptr;
|
|
|
|
++*sptr;
|
2000-02-09 01:28:47 +05:30
|
|
|
switch (c) {
|
|
|
|
case '<':
|
1999-10-22 10:00:20 +05:30
|
|
|
return M_BEGWORD;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case '>':
|
1999-10-22 10:00:20 +05:30
|
|
|
return M_ENDWORD;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case '(':
|
|
|
|
if (start_cnt >= NSUBEXP) {
|
1999-10-22 10:00:20 +05:30
|
|
|
FAIL("Too many \\(s");
|
|
|
|
}
|
|
|
|
end_stk[end_sp++] = start_cnt;
|
|
|
|
return M_START(start_cnt++);
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case ')':
|
|
|
|
if (end_sp <= 0) {
|
1999-10-22 10:00:20 +05:30
|
|
|
FAIL("Mismatched \\)");
|
|
|
|
}
|
|
|
|
return M_END(end_stk[--end_sp]);
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case '*':
|
1999-10-22 10:00:20 +05:30
|
|
|
return M_SPLAT;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case '.':
|
1999-10-22 10:00:20 +05:30
|
|
|
return M_ANY;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case '+':
|
1999-10-22 10:00:20 +05:30
|
|
|
return M_PLUS;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case '?':
|
1999-10-22 10:00:20 +05:30
|
|
|
return M_QMARK;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
default:
|
1999-10-22 10:00:20 +05:30
|
|
|
return c;
|
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
|
|
|
switch (c) {
|
|
|
|
case '^':
|
|
|
|
if (*sptr == retext + 1) {
|
1999-10-22 10:00:20 +05:30
|
|
|
return M_BEGLINE;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case '$':
|
|
|
|
if (!**sptr) {
|
1999-10-22 10:00:20 +05:30
|
|
|
return M_ENDLINE;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case '.':
|
1999-10-22 10:00:20 +05:30
|
|
|
return M_ANY;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case '*':
|
1999-10-22 10:00:20 +05:30
|
|
|
return M_SPLAT;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case '[':
|
1999-10-22 10:00:20 +05:30
|
|
|
/* make sure we don't have too many classes */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (class_cnt >= 10) {
|
1999-10-22 10:00:20 +05:30
|
|
|
FAIL("Too many []s");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* process the character list for this class */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (re) {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* generate the bitmap for this class */
|
|
|
|
*sptr = makeclass(*sptr, re->program + 1 + 32 * class_cnt);
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* skip to end of the class */
|
2000-02-09 01:28:47 +05:30
|
|
|
*sptr = makeclass(*sptr, (char *) 0);
|
1999-10-22 10:00:20 +05:30
|
|
|
}
|
|
|
|
return M_CLASS(class_cnt++);
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
default:
|
1999-10-22 10:00:20 +05:30
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
/*NOTREACHED*/}
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This function calculates the number of bytes that will be needed for a
|
|
|
|
* compiled RE. Its argument is the uncompiled version. It is not clever
|
|
|
|
* about catching syntax errors; that is done in a later pass.
|
|
|
|
*/
|
|
|
|
static unsigned calcsize(text)
|
2000-02-09 01:28:47 +05:30
|
|
|
char *text;
|
1999-10-22 10:00:20 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
unsigned size;
|
|
|
|
int token;
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
retext = text;
|
|
|
|
class_cnt = 0;
|
|
|
|
start_cnt = 1;
|
|
|
|
end_sp = 0;
|
|
|
|
size = 5;
|
2000-02-09 01:28:47 +05:30
|
|
|
while ((token = gettoken(&text, (regexp *) 0)) != 0) {
|
|
|
|
if (IS_CLASS(token)) {
|
1999-10-22 10:00:20 +05:30
|
|
|
size += 34;
|
2000-02-09 01:28:47 +05:30
|
|
|
} else if (IS_META(token)) {
|
1999-10-22 10:00:20 +05:30
|
|
|
size += 2;
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
1999-10-22 10:00:20 +05:30
|
|
|
size++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
/* This function checks for a match between a character and a token which is
|
|
|
|
* known to represent a single character. It returns 0 if they match, or
|
|
|
|
* 1 if they don't.
|
|
|
|
*/
|
2000-02-09 01:28:47 +05:30
|
|
|
static int match1(regexp * re, char ch, int token, int ignoreCase)
|
1999-10-22 10:00:20 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
if (!ch) {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* the end of a line can't match any RE of width 1 */
|
|
|
|
return 1;
|
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
if (token == M_ANY) {
|
1999-10-22 10:00:20 +05:30
|
|
|
return 0;
|
2000-02-09 01:28:47 +05:30
|
|
|
} else if (IS_CLASS(token)) {
|
2000-05-02 03:38:54 +05:30
|
|
|
if (re->program[1 + 32 * (token - M_CLASS(0)) + (ch >> 3)] & (1 << (ch & 7)))
|
1999-10-22 10:00:20 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2000-01-05 11:19:51 +05:30
|
|
|
//fprintf(stderr, "match1: ch='%c' token='%c': ", ch, token);
|
2000-05-02 03:38:54 +05:30
|
|
|
if (ch == token || (ignoreCase == TRUE && tolower(ch) == tolower(token))) {
|
2000-01-05 11:19:51 +05:30
|
|
|
//fprintf(stderr, "match\n");
|
1999-10-22 10:00:20 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2000-01-05 11:19:51 +05:30
|
|
|
//fprintf(stderr, "no match\n");
|
1999-10-22 10:00:20 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This function checks characters up to and including the next closure, at
|
|
|
|
* which point it does a recursive call to check the rest of it. This function
|
|
|
|
* returns 0 if everything matches, or 1 if something doesn't match.
|
|
|
|
*/
|
|
|
|
/* re -- the regular expression */
|
|
|
|
/* str -- the string */
|
|
|
|
/* prog -- a portion of re->program, an compiled RE */
|
|
|
|
/* here -- a portion of str, the string to compare it to */
|
2000-02-09 01:28:47 +05:30
|
|
|
static int match(regexp * re, char *str, char *prog, char *here,
|
|
|
|
int ignoreCase)
|
1999-10-22 10:00:20 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
int token;
|
|
|
|
int nmatched;
|
|
|
|
int closure;
|
|
|
|
|
|
|
|
for (token = GET_META(prog); !IS_CLOSURE(token);
|
|
|
|
prog++, token = GET_META(prog)) {
|
|
|
|
switch (token) {
|
|
|
|
/*case M_BEGLINE: can't happen; re->bol is used instead */
|
|
|
|
case M_ENDLINE:
|
1999-10-22 10:00:20 +05:30
|
|
|
if (*here)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case M_BEGWORD:
|
1999-10-22 10:00:20 +05:30
|
|
|
if (here != str &&
|
2000-02-09 01:28:47 +05:30
|
|
|
(here[-1] == '_' ||
|
|
|
|
(isascii(here[-1]) && isalnum(here[-1])))) return 1;
|
1999-10-22 10:00:20 +05:30
|
|
|
break;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case M_ENDWORD:
|
1999-10-22 10:00:20 +05:30
|
|
|
if ((here[0] == '_' || isascii(here[0])) && isalnum(here[0]))
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case M_START(0):
|
|
|
|
case M_START(1):
|
|
|
|
case M_START(2):
|
|
|
|
case M_START(3):
|
|
|
|
case M_START(4):
|
|
|
|
case M_START(5):
|
|
|
|
case M_START(6):
|
|
|
|
case M_START(7):
|
|
|
|
case M_START(8):
|
|
|
|
case M_START(9):
|
|
|
|
re->startp[token - M_START(0)] = (char *) here;
|
1999-10-22 10:00:20 +05:30
|
|
|
break;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case M_END(0):
|
|
|
|
case M_END(1):
|
|
|
|
case M_END(2):
|
|
|
|
case M_END(3):
|
|
|
|
case M_END(4):
|
|
|
|
case M_END(5):
|
|
|
|
case M_END(6):
|
|
|
|
case M_END(7):
|
|
|
|
case M_END(8):
|
|
|
|
case M_END(9):
|
|
|
|
re->endp[token - M_END(0)] = (char *) here;
|
|
|
|
if (token == M_END(0)) {
|
1999-10-22 10:00:20 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
default: /* literal, M_CLASS(n), or M_ANY */
|
1999-10-22 10:00:20 +05:30
|
|
|
if (match1(re, *here, token, ignoreCase) != 0)
|
|
|
|
return 1;
|
|
|
|
here++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* C L O S U R E */
|
|
|
|
|
|
|
|
/* step 1: see what we have to match against, and move "prog" to point
|
|
|
|
* the the remainder of the compiled RE.
|
|
|
|
*/
|
|
|
|
closure = token;
|
|
|
|
prog++, token = GET_META(prog);
|
|
|
|
prog++;
|
|
|
|
|
|
|
|
/* step 2: see how many times we can match that token against the string */
|
|
|
|
for (nmatched = 0;
|
2000-02-09 01:28:47 +05:30
|
|
|
(closure != M_QMARK || nmatched < 1) && *here
|
|
|
|
&& match1(re, *here, token, ignoreCase) == 0; nmatched++, here++) {
|
1999-10-22 10:00:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* step 3: try to match the remainder, and back off if it doesn't */
|
2000-02-09 01:28:47 +05:30
|
|
|
while (nmatched >= 0 && match(re, str, prog, here, ignoreCase) != 0) {
|
1999-10-22 10:00:20 +05:30
|
|
|
nmatched--;
|
|
|
|
here--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* so how did it work out? */
|
|
|
|
if (nmatched >= ((closure == M_PLUS) ? 1 : 0))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This function compiles a regexp. */
|
2000-06-29 03:29:31 +05:30
|
|
|
static regexp *regcomp(char *text)
|
1999-10-22 10:00:20 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
int needfirst;
|
|
|
|
unsigned size;
|
|
|
|
int token;
|
|
|
|
int peek;
|
|
|
|
char *build;
|
2000-05-19 11:05:19 +05:30
|
|
|
regexp *re;
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
|
|
|
|
/* prepare for error handling */
|
2000-02-09 01:28:47 +05:30
|
|
|
re = (regexp *) 0;
|
|
|
|
if (setjmp(errorhandler)) {
|
|
|
|
if (re) {
|
1999-10-22 10:00:20 +05:30
|
|
|
free(re);
|
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
return (regexp *) 0;
|
1999-10-22 10:00:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* if an empty regexp string was given, use the previous one */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (*text == 0) {
|
|
|
|
if (!previous) {
|
1999-10-22 10:00:20 +05:30
|
|
|
FAIL("No previous RE");
|
|
|
|
}
|
|
|
|
text = previous;
|
2000-02-09 01:28:47 +05:30
|
|
|
} else { /* non-empty regexp given, so remember it */
|
|
|
|
|
1999-10-22 10:00:20 +05:30
|
|
|
if (previous)
|
|
|
|
free(previous);
|
2000-02-09 01:28:47 +05:30
|
|
|
previous = (char *) malloc((unsigned) (strlen(text) + 1));
|
1999-10-22 10:00:20 +05:30
|
|
|
if (previous)
|
|
|
|
strcpy(previous, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate memory */
|
|
|
|
class_cnt = 0;
|
|
|
|
start_cnt = 1;
|
|
|
|
end_sp = 0;
|
|
|
|
retext = text;
|
|
|
|
size = calcsize(text) + sizeof(regexp);
|
2000-02-09 01:28:47 +05:30
|
|
|
re = (regexp *) malloc((unsigned) size);
|
1999-10-22 10:00:20 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
if (!re) {
|
1999-10-22 10:00:20 +05:30
|
|
|
FAIL("Not enough memory for this RE");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compile it */
|
|
|
|
build = &re->program[1 + 32 * class_cnt];
|
|
|
|
re->program[0] = class_cnt;
|
2000-02-09 01:28:47 +05:30
|
|
|
for (token = 0; token < NSUBEXP; token++) {
|
|
|
|
re->startp[token] = re->endp[token] = (char *) 0;
|
1999-10-22 10:00:20 +05:30
|
|
|
}
|
|
|
|
re->first = 0;
|
|
|
|
re->bol = 0;
|
|
|
|
re->minlen = 0;
|
|
|
|
needfirst = 1;
|
|
|
|
class_cnt = 0;
|
|
|
|
start_cnt = 1;
|
|
|
|
end_sp = 0;
|
|
|
|
retext = text;
|
|
|
|
for (token = M_START(0), peek = gettoken(&text, re);
|
2000-02-09 01:28:47 +05:30
|
|
|
token; token = peek, peek = gettoken(&text, re)) {
|
2000-05-02 03:38:54 +05:30
|
|
|
|
1999-10-22 10:00:20 +05:30
|
|
|
/* special processing for the closure operator */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (IS_CLOSURE(peek)) {
|
2000-05-02 03:38:54 +05:30
|
|
|
|
1999-10-22 10:00:20 +05:30
|
|
|
/* detect misuse of closure operator */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (IS_START(token)) {
|
1999-10-22 10:00:20 +05:30
|
|
|
FAIL("* or \\+ or \\? follows nothing");
|
2000-05-02 03:38:54 +05:30
|
|
|
} else if (IS_META(token) && token != M_ANY && !IS_CLASS(token)) {
|
|
|
|
FAIL("* or \\+ or \\? can only follow a normal character or . or []");
|
1999-10-22 10:00:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* it is okay -- make it prefix instead of postfix */
|
|
|
|
ADD_META(build, peek);
|
|
|
|
|
|
|
|
/* take care of "needfirst" - is this the first char? */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (needfirst && peek == M_PLUS && !IS_META(token)) {
|
1999-10-22 10:00:20 +05:30
|
|
|
re->first = token;
|
|
|
|
}
|
|
|
|
needfirst = 0;
|
|
|
|
|
|
|
|
/* we used "peek" -- need to refill it */
|
|
|
|
peek = gettoken(&text, re);
|
2000-02-09 01:28:47 +05:30
|
|
|
if (IS_CLOSURE(peek)) {
|
1999-10-22 10:00:20 +05:30
|
|
|
FAIL("* or \\+ or \\? doubled up");
|
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
} else if (!IS_META(token)) {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* normal char is NOT argument of closure */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (needfirst) {
|
1999-10-22 10:00:20 +05:30
|
|
|
re->first = token;
|
|
|
|
needfirst = 0;
|
|
|
|
}
|
|
|
|
re->minlen++;
|
2000-02-09 01:28:47 +05:30
|
|
|
} else if (token == M_ANY || IS_CLASS(token)) {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* . or [] is NOT argument of closure */
|
|
|
|
needfirst = 0;
|
|
|
|
re->minlen++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the "token" character is not closure -- process it normally */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (token == M_BEGLINE) {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* set the BOL flag instead of storing M_BEGLINE */
|
|
|
|
re->bol = 1;
|
2000-02-09 01:28:47 +05:30
|
|
|
} else if (IS_META(token)) {
|
1999-10-22 10:00:20 +05:30
|
|
|
ADD_META(build, token);
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
1999-10-22 10:00:20 +05:30
|
|
|
*build++ = token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end it with a \) which MUST MATCH the opening \( */
|
|
|
|
ADD_META(build, M_END(0));
|
2000-02-09 01:28:47 +05:30
|
|
|
if (end_sp > 0) {
|
1999-10-22 10:00:20 +05:30
|
|
|
FAIL("Not enough \\)s");
|
|
|
|
}
|
|
|
|
|
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This function searches through a string for text that matches an RE. */
|
|
|
|
/* re -- the compiled regexp to search for */
|
|
|
|
/* str -- the string to search through */
|
|
|
|
/* bol -- does str start at the beginning of a line? (boolean) */
|
|
|
|
/* ignoreCase -- ignoreCase or not */
|
2000-06-29 03:29:31 +05:30
|
|
|
static int regexec(struct regexp *re, char *str, int bol, int ignoreCase)
|
1999-10-22 10:00:20 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
char *prog; /* the entry point of re->program */
|
|
|
|
int len; /* length of the string */
|
|
|
|
char *here;
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
/* if must start at the beginning of a line, and this isn't, then fail */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (re->bol && bol == TRUE) {
|
1999-10-22 10:00:20 +05:30
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
prog = re->program + 1 + 32 * re->program[0];
|
|
|
|
|
|
|
|
/* search for the RE in the string */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (re->bol) {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* must occur at BOL */
|
2000-02-09 01:28:47 +05:30
|
|
|
if ((re->first && match1(re, *(char *) str, re->first, ignoreCase)) /* wrong first letter? */
|
|
|
|
||len < re->minlen /* not long enough? */
|
|
|
|
|| match(re, (char *) str, prog, str, ignoreCase)) /* doesn't match? */
|
|
|
|
return FALSE; /* THEN FAIL! */
|
|
|
|
} else if (ignoreCase == FALSE) {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* can occur anywhere in the line, noignorecase */
|
2000-02-09 01:28:47 +05:30
|
|
|
for (here = (char *) str; (re->first && re->first != *here)
|
|
|
|
|| match(re, (char *) str, prog, here, ignoreCase);
|
|
|
|
here++, len--) {
|
1999-10-22 10:00:20 +05:30
|
|
|
if (len < re->minlen)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* can occur anywhere in the line, ignorecase */
|
2000-02-09 01:28:47 +05:30
|
|
|
for (here = (char *) str;
|
|
|
|
(re->first && match1(re, *here, (int) re->first, ignoreCase))
|
|
|
|
|| match(re, (char *) str, prog, here, ignoreCase);
|
|
|
|
here++, len--) {
|
1999-10-22 10:00:20 +05:30
|
|
|
if (len < re->minlen)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we didn't fail, then we must have succeeded */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-10-30 04:39:13 +05:30
|
|
|
#if defined BB_SED
|
1999-10-22 10:00:20 +05:30
|
|
|
/* This performs substitutions after a regexp match has been found. */
|
2000-06-29 03:29:31 +05:30
|
|
|
static void regsub(regexp * re, char *src, char *dst)
|
1999-10-22 10:00:20 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
char *cpy;
|
|
|
|
char *end;
|
|
|
|
char c;
|
|
|
|
char *start;
|
|
|
|
int mod;
|
1999-10-22 10:00:20 +05:30
|
|
|
|
|
|
|
mod = 0;
|
|
|
|
|
|
|
|
start = src;
|
2000-02-09 01:28:47 +05:30
|
|
|
while ((c = *src++) != '\0') {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* recognize any meta characters */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (c == '&') {
|
1999-10-22 10:00:20 +05:30
|
|
|
cpy = re->startp[0];
|
|
|
|
end = re->endp[0];
|
2000-02-09 01:28:47 +05:30
|
|
|
} else if (c == '~') {
|
1999-10-22 10:00:20 +05:30
|
|
|
cpy = previous1;
|
|
|
|
if (cpy)
|
|
|
|
end = cpy + strlen(cpy);
|
2000-02-09 01:28:47 +05:30
|
|
|
} else if (c == '\\') {
|
1999-10-22 10:00:20 +05:30
|
|
|
c = *src++;
|
2000-02-09 01:28:47 +05:30
|
|
|
switch (c) {
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
1999-10-22 10:00:20 +05:30
|
|
|
/* \0 thru \9 mean "copy subexpression" */
|
|
|
|
c -= '0';
|
2000-02-09 01:28:47 +05:30
|
|
|
cpy = re->startp[(int) c];
|
|
|
|
end = re->endp[(int) c];
|
1999-10-22 10:00:20 +05:30
|
|
|
break;
|
2000-02-09 01:28:47 +05:30
|
|
|
case 'U':
|
|
|
|
case 'u':
|
|
|
|
case 'L':
|
|
|
|
case 'l':
|
1999-10-22 10:00:20 +05:30
|
|
|
/* \U and \L mean "convert to upper/lowercase" */
|
|
|
|
mod = c;
|
|
|
|
continue;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case 'E':
|
|
|
|
case 'e':
|
1999-10-22 10:00:20 +05:30
|
|
|
/* \E ends the \U or \L */
|
|
|
|
mod = 0;
|
|
|
|
continue;
|
2000-02-09 01:28:47 +05:30
|
|
|
case '&':
|
1999-10-22 10:00:20 +05:30
|
|
|
/* "\&" means "original text" */
|
|
|
|
*dst++ = c;
|
|
|
|
continue;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case '~':
|
1999-10-22 10:00:20 +05:30
|
|
|
/* "\~" means "previous text, if any" */
|
|
|
|
*dst++ = c;
|
|
|
|
continue;
|
2000-02-09 01:28:47 +05:30
|
|
|
default:
|
1999-10-22 10:00:20 +05:30
|
|
|
/* ordinary char preceded by backslash */
|
|
|
|
*dst++ = c;
|
|
|
|
continue;
|
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
1999-10-22 10:00:20 +05:30
|
|
|
/* ordinary character, so just copy it */
|
|
|
|
*dst++ = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: to reach this point in the code, we must have evaded
|
|
|
|
* all "continue" statements. To do that, we must have hit
|
|
|
|
* a metacharacter that involves copying.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* if there is nothing to copy, loop */
|
|
|
|
if (!cpy)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* copy over a portion of the original */
|
2000-02-09 01:28:47 +05:30
|
|
|
while (cpy < end) {
|
|
|
|
switch (mod) {
|
|
|
|
case 'U':
|
|
|
|
case 'u':
|
1999-10-22 10:00:20 +05:30
|
|
|
/* convert to uppercase */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (isascii(*cpy) && islower(*cpy)) {
|
1999-10-22 10:00:20 +05:30
|
|
|
*dst++ = toupper(*cpy);
|
|
|
|
cpy++;
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
1999-10-22 10:00:20 +05:30
|
|
|
*dst++ = *cpy++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
case 'L':
|
|
|
|
case 'l':
|
1999-10-22 10:00:20 +05:30
|
|
|
/* convert to lowercase */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (isascii(*cpy) && isupper(*cpy)) {
|
1999-10-22 10:00:20 +05:30
|
|
|
*dst++ = tolower(*cpy);
|
|
|
|
cpy++;
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
1999-10-22 10:00:20 +05:30
|
|
|
*dst++ = *cpy++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
default:
|
1999-10-22 10:00:20 +05:30
|
|
|
/* copy without any conversion */
|
|
|
|
*dst++ = *cpy++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* \u and \l end automatically after the first char */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (mod && (mod == 'u' || mod == 'l')) {
|
1999-10-22 10:00:20 +05:30
|
|
|
mod = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
|
|
|
|
|
|
|
/* remember what text we inserted this time */
|
|
|
|
if (previous1)
|
|
|
|
free(previous1);
|
2000-02-09 01:28:47 +05:30
|
|
|
previous1 = (char *) malloc((unsigned) (strlen(start) + 1));
|
1999-10-22 10:00:20 +05:30
|
|
|
if (previous1)
|
|
|
|
strcpy(previous1, start);
|
|
|
|
}
|
1999-10-30 04:39:13 +05:30
|
|
|
#endif
|
1999-10-22 10:00:20 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
#endif /* BB_REGEXP */
|