home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume24
/
gnudiff1.15
/
part06
/
regex.c2
Wrap
Text File
|
1991-03-05
|
38KB
|
1,267 lines
/* Like re_search_2, below, but only one string is specified, and
doesn't let you say where to stop matching. */
int
re_search (pbufp, string, size, startpos, range, regs)
struct re_pattern_buffer *pbufp;
char *string;
int size, startpos, range;
struct re_registers *regs;
{
return re_search_2 (pbufp, (char *) 0, 0, string, size, startpos, range,
regs, size);
}
/* Using the compiled pattern in PBUFP->buffer, first tries to match the
virtual concatenation of STRING1 and STRING2, starting first at index
STARTPOS, then at STARTPOS + 1, and so on. RANGE is the number of
places to try before giving up. If RANGE is negative, it searches
backwards, i.e., the starting positions tried are STARTPOS, STARTPOS
- 1, etc. STRING1 and STRING2 are of SIZE1 and SIZE2, respectively.
In REGS, return the indices of the virtual concatenation of STRING1
and STRING2 that matched the entire PBUFP->buffer and its contained
subexpressions. Do not consider matching one past the index MSTOP in
the virtual concatenation of STRING1 and STRING2.
The value returned is the position in the strings at which the match
was found, or -1 if no match was found, or -2 if error (such as
failure stack overflow). */
int
re_search_2 (pbufp, string1, size1, string2, size2, startpos, range,
regs, mstop)
struct re_pattern_buffer *pbufp;
char *string1, *string2;
int size1, size2;
int startpos;
register int range;
struct re_registers *regs;
int mstop;
{
register char *fastmap = pbufp->fastmap;
register unsigned char *translate = (unsigned char *) pbufp->translate;
int total_size = size1 + size2;
int endpos = startpos + range;
int val;
/* Check for out-of-range starting position. */
if (startpos < 0 || startpos > total_size)
return -1;
/* Fix up range if it would eventually take startpos outside of the
virtual concatenation of string1 and string2. */
if (endpos < -1)
range = -1 - startpos;
else if (endpos > total_size)
range = total_size - startpos;
/* Update the fastmap now if not correct already. */
if (fastmap && !pbufp->fastmap_accurate)
re_compile_fastmap (pbufp);
/* If the search isn't to be a backwards one, don't waste time in a
long search for a pattern that says it is anchored. */
if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
&& range > 0)
{
if (startpos > 0)
return -1;
else
range = 1;
}
while (1)
{
/* If a fastmap is supplied, skip quickly over characters that
cannot possibly be the start of a match. Note, however, that
if the pattern can possibly match the null string, we must
test it at each starting point so that we take the first null
string we get. */
if (fastmap && startpos < total_size && pbufp->can_be_null != 1)
{
if (range > 0) /* Searching forwards. */
{
register int lim = 0;
register unsigned char *p;
int irange = range;
if (startpos < size1 && startpos + range >= size1)
lim = range - (size1 - startpos);
p = ((unsigned char *)
&(startpos >= size1 ? string2 - size1 : string1)[startpos]);
while (range > lim && !fastmap[translate
? translate[*p++]
: *p++])
range--;
startpos += irange - range;
}
else /* Searching backwards. */
{
register unsigned char c;
if (string1 == 0 || startpos >= size1)
c = string2[startpos - size1];
else
c = string1[startpos];
c &= 0xff;
if (translate ? !fastmap[translate[c]] : !fastmap[c])
goto advance;
}
}
if (range >= 0 && startpos == total_size
&& fastmap && pbufp->can_be_null == 0)
return -1;
val = re_match_2 (pbufp, string1, size1, string2, size2, startpos,
regs, mstop);
if (val >= 0)
return startpos;
if (val == -2)
return -2;
#ifdef C_ALLOCA
alloca (0);
#endif /* C_ALLOCA */
advance:
if (!range)
break;
else if (range > 0)
{
range--;
startpos++;
}
else
{
range++;
startpos--;
}
}
return -1;
}
#ifndef emacs /* emacs never uses this. */
int
re_match (pbufp, string, size, pos, regs)
struct re_pattern_buffer *pbufp;
char *string;
int size, pos;
struct re_registers *regs;
{
return re_match_2 (pbufp, (char *) 0, 0, string, size, pos, regs, size);
}
#endif /* not emacs */
/* The following are used for re_match_2, defined below: */
/* Roughly the maximum number of failure points on the stack. Would be
exactly that if always pushed MAX_NUM_FAILURE_ITEMS each time we failed. */
int re_max_failures = 2000;
/* Routine used by re_match_2. */
static int bcmp_translate ();
/* Structure and accessing macros used in re_match_2: */
struct register_info
{
unsigned is_active : 1;
unsigned matched_something : 1;
};
#define IS_ACTIVE(R) ((R).is_active)
#define MATCHED_SOMETHING(R) ((R).matched_something)
/* Macros used by re_match_2: */
/* I.e., regstart, regend, and reg_info. */
#define NUM_REG_ITEMS 3
/* We push at most this many things on the stack whenever we
fail. The `+ 2' refers to PATTERN_PLACE and STRING_PLACE, which are
arguments to the PUSH_FAILURE_POINT macro. */
#define MAX_NUM_FAILURE_ITEMS (RE_NREGS * NUM_REG_ITEMS + 2)
/* We push this many things on the stack whenever we fail. */
#define NUM_FAILURE_ITEMS (last_used_reg * NUM_REG_ITEMS + 2)
/* This pushes most of the information about the current state we will want
if we ever fail back to it. */
#define PUSH_FAILURE_POINT(pattern_place, string_place) \
{ \
short last_used_reg, this_reg; \
\
/* Find out how many registers are active or have been matched. \
(Aside from register zero, which is only set at the end.) */ \
for (last_used_reg = RE_NREGS - 1; last_used_reg > 0; last_used_reg--)\
if (regstart[last_used_reg] != (unsigned char *) -1) \
break; \
\
if (stacke - stackp < NUM_FAILURE_ITEMS) \
{ \
unsigned char **stackx; \
if (stacke - stackb > re_max_failures * MAX_NUM_FAILURE_ITEMS) \
return -2; \
\
/* Roughly double the size of the stack. */ \
stackx = (unsigned char **) alloca (2 * MAX_NUM_FAILURE_ITEMS \
* (stacke - stackb) \
* sizeof (unsigned char *));\
/* Only copy what is in use. */ \
bcopy (stackb, stackx, (stackp - stackb) * sizeof (char *)); \
stackp = stackx + (stackp - stackb); \
stackb = stackx; \
stacke = stackb + 2 * MAX_NUM_FAILURE_ITEMS * (stacke - stackb);\
} \
\
/* Now push the info for each of those registers. */ \
for (this_reg = 1; this_reg <= last_used_reg; this_reg++) \
{ \
*stackp++ = regstart[this_reg]; \
*stackp++ = regend[this_reg]; \
*stackp++ = (unsigned char *) ®_info[this_reg]; \
} \
\
/* Push how many registers we saved. */ \
*stackp++ = (unsigned char *) last_used_reg; \
\
*stackp++ = pattern_place; \
*stackp++ = string_place; \
}
/* This pops what PUSH_FAILURE_POINT pushes. */
#define POP_FAILURE_POINT() \
{ \
int temp; \
stackp -= 2; /* Remove failure points. */ \
temp = (int) *--stackp; /* How many regs pushed. */ \
temp *= NUM_REG_ITEMS; /* How much to take off the stack. */ \
stackp -= temp; /* Remove the register info. */ \
}
#define MATCHING_IN_FIRST_STRING (dend == end_match_1)
/* Is true if there is a first string and if PTR is pointing anywhere
inside it or just past the end. */
#define IS_IN_FIRST_STRING(ptr) \
(size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
/* Call before fetching a character with *d. This switches over to
string2 if necessary. */
#define PREFETCH \
while (d == dend) \
{ \
/* end of string2 => fail. */ \
if (dend == end_match_2) \
goto fail; \
/* end of string1 => advance to string2. */ \
d = string2; \
dend = end_match_2; \
}
/* Call this when have matched something; it sets `matched' flags for the
registers corresponding to the subexpressions of which we currently
are inside. */
#define SET_REGS_MATCHED \
{ unsigned this_reg; \
for (this_reg = 0; this_reg < RE_NREGS; this_reg++) \
{ \
if (IS_ACTIVE(reg_info[this_reg])) \
MATCHED_SOMETHING(reg_info[this_reg]) = 1; \
else \
MATCHED_SOMETHING(reg_info[this_reg]) = 0; \
} \
}
/* Test if at very beginning or at very end of the virtual concatenation
of string1 and string2. If there is only one string, we've put it in
string2. */
#define AT_STRINGS_BEG (d == (size1 ? string1 : string2) || !size2)
#define AT_STRINGS_END (d == end2)
#define AT_WORD_BOUNDARY \
(AT_STRINGS_BEG || AT_STRINGS_END || IS_A_LETTER (d - 1) != IS_A_LETTER (d))
/* We have two special cases to check for:
1) if we're past the end of string1, we have to look at the first
character in string2;
2) if we're before the beginning of string2, we have to look at the
last character in string1; we assume there is a string1, so use
this in conjunction with AT_STRINGS_BEG. */
#define IS_A_LETTER(d) \
(SYNTAX ((d) == end1 ? *string2 : (d) == string2 - 1 ? *(end1 - 1) : *(d))\
== Sword)
/* Match the pattern described by PBUFP against the virtual
concatenation of STRING1 and STRING2, which are of SIZE1 and SIZE2,
respectively. Start the match at index POS in the virtual
concatenation of STRING1 and STRING2. In REGS, return the indices of
the virtual concatenation of STRING1 and STRING2 that matched the
entire PBUFP->buffer and its contained subexpressions. Do not
consider matching one past the index MSTOP in the virtual
concatenation of STRING1 and STRING2.
If pbufp->fastmap is nonzero, then it had better be up to date.
The reason that the data to match are specified as two components
which are to be regarded as concatenated is so this function can be
used directly on the contents of an Emacs buffer.
-1 is returned if there is no match. -2 is returned if there is an
error (such as match stack overflow). Otherwise the value is the
length of the substring which was matched. */
int
re_match_2 (pbufp, string1_arg, size1, string2_arg, size2, pos, regs, mstop)
struct re_pattern_buffer *pbufp;
char *string1_arg, *string2_arg;
int size1, size2;
int pos;
struct re_registers *regs;
int mstop;
{
register unsigned char *p = (unsigned char *) pbufp->buffer;
/* Pointer to beyond end of buffer. */
register unsigned char *pend = p + pbufp->used;
unsigned char *string1 = (unsigned char *) string1_arg;
unsigned char *string2 = (unsigned char *) string2_arg;
unsigned char *end1; /* Just past end of first string. */
unsigned char *end2; /* Just past end of second string. */
/* Pointers into string1 and string2, just past the last characters in
each to consider matching. */
unsigned char *end_match_1, *end_match_2;
register unsigned char *d, *dend;
register int mcnt; /* Multipurpose. */
unsigned char *translate = (unsigned char *) pbufp->translate;
unsigned is_a_jump_n = 0;
/* Failure point stack. Each place that can handle a failure further
down the line pushes a failure point on this stack. It consists of
restart, regend, and reg_info for all registers corresponding to the
subexpressions we're currently inside, plus the number of such
registers, and, finally, two char *'s. The first char * is where to
resume scanning the pattern; the second one is where to resume
scanning the strings. If the latter is zero, the failure point is a
``dummy''; if a failure happens and the failure point is a dummy, it
gets discarded and the next next one is tried. */
unsigned char *initial_stack[MAX_NUM_FAILURE_ITEMS * NFAILURES];
unsigned char **stackb = initial_stack;
unsigned char **stackp = stackb;
unsigned char **stacke = &stackb[MAX_NUM_FAILURE_ITEMS * NFAILURES];
/* Information on the contents of registers. These are pointers into
the input strings; they record just what was matched (on this
attempt) by a subexpression part of the pattern, that is, the
regnum-th regstart pointer points to where in the pattern we began
matching and the regnum-th regend points to right after where we
stopped matching the regnum-th subexpression. (The zeroth register
keeps track of what the whole pattern matches.) */
unsigned char *regstart[RE_NREGS];
unsigned char *regend[RE_NREGS];
/* The is_active field of reg_info helps us keep track of which (possibly
nested) subexpressions we are currently in. The matched_something
field of reg_info[reg_num] helps us tell whether or not we have
matched any of the pattern so far this time through the reg_num-th
subexpression. These two fields get reset each time through any
loop their register is in. */
struct register_info reg_info[RE_NREGS];
/* The following record the register info as found in the above
variables when we find a match better than any we've seen before.
This happens as we backtrack through the failure points, which in
turn happens only if we have not yet matched the entire string. */
unsigned best_regs_set = 0;
unsigned char *best_regstart[RE_NREGS];
unsigned char *best_regend[RE_NREGS];
/* Initialize subexpression text positions to -1 to mark ones that no
\( or ( and \) or ) has been seen for. Also set all registers to
inactive and mark them as not having matched anything or ever
failed. */
for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
{
regstart[mcnt] = regend[mcnt] = (unsigned char *) -1;
IS_ACTIVE (reg_info[mcnt]) = 0;
MATCHED_SOMETHING (reg_info[mcnt]) = 0;
}
if (regs)
for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
regs->start[mcnt] = regs->end[mcnt] = -1;
/* Set up pointers to ends of strings.
Don't allow the second string to be empty unless both are empty. */
if (size2 == 0)
{
string2 = string1;
size2 = size1;
string1 = 0;
size1 = 0;
}
end1 = string1 + size1;
end2 = string2 + size2;
/* Compute where to stop matching, within the two strings. */
if (mstop <= size1)
{
end_match_1 = string1 + mstop;
end_match_2 = string2;
}
else
{
end_match_1 = end1;
end_match_2 = string2 + mstop - size1;
}
/* `p' scans through the pattern as `d' scans through the data. `dend'
is the end of the input string that `d' points within. `d' is
advanced into the following input string whenever necessary, but
this happens before fetching; therefore, at the beginning of the
loop, `d' can be pointing at the end of a string, but it cannot
equal string2. */
if (size1 != 0 && pos <= size1)
d = string1 + pos, dend = end_match_1;
else
d = string2 + pos - size1, dend = end_match_2;
/* This loops over pattern commands. It exits by returning from the
function if match is complete, or it drops through if match fails
at this starting point in the input data. */
while (1)
{
is_a_jump_n = 0;
/* End of pattern means we might have succeeded. */
if (p == pend)
{
/* If not end of string, try backtracking. Otherwise done. */
if (d != end_match_2)
{
if (stackp != stackb)
{
/* More failure points to try. */
unsigned in_same_string =
IS_IN_FIRST_STRING (best_regend[0])
== MATCHING_IN_FIRST_STRING;
/* If exceeds best match so far, save it. */
if (! best_regs_set
|| (in_same_string && d > best_regend[0])
|| (! in_same_string && ! MATCHING_IN_FIRST_STRING))
{
best_regs_set = 1;
best_regend[0] = d; /* Never use regstart[0]. */
for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
{
best_regstart[mcnt] = regstart[mcnt];
best_regend[mcnt] = regend[mcnt];
}
}
goto fail;
}
/* If no failure points, don't restore garbage. */
else if (best_regs_set)
{
restore_best_regs:
/* Restore best match. */
d = best_regend[0];
for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
{
regstart[mcnt] = best_regstart[mcnt];
regend[mcnt] = best_regend[mcnt];
}
}
}
/* If caller wants register contents data back, convert it
to indices. */
if (regs)
{
regs->start[0] = pos;
if (MATCHING_IN_FIRST_STRING)
regs->end[0] = d - string1;
else
regs->end[0] = d - string2 + size1;
for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
{
if (regend[mcnt] == (unsigned char *) -1)
{
regs->start[mcnt] = -1;
regs->end[mcnt] = -1;
continue;
}
if (IS_IN_FIRST_STRING (regstart[mcnt]))
regs->start[mcnt] = regstart[mcnt] - string1;
else
regs->start[mcnt] = regstart[mcnt] - string2 + size1;
if (IS_IN_FIRST_STRING (regend[mcnt]))
regs->end[mcnt] = regend[mcnt] - string1;
else
regs->end[mcnt] = regend[mcnt] - string2 + size1;
}
}
return d - pos - (MATCHING_IN_FIRST_STRING
? string1
: string2 - size1);
}
/* Otherwise match next pattern command. */
#ifdef SWITCH_ENUM_BUG
switch ((int) ((enum regexpcode) *p++))
#else
switch ((enum regexpcode) *p++)
#endif
{
/* \( [or `(', as appropriate] is represented by start_memory,
\) by stop_memory. Both of those commands are followed by
a register number in the next byte. The text matched
within the \( and \) is recorded under that number. */
case start_memory:
regstart[*p] = d;
IS_ACTIVE (reg_info[*p]) = 1;
MATCHED_SOMETHING (reg_info[*p]) = 0;
p++;
break;
case stop_memory:
regend[*p] = d;
IS_ACTIVE (reg_info[*p]) = 0;
/* If just failed to match something this time around with a sub-
expression that's in a loop, try to force exit from the loop. */
if ((! MATCHED_SOMETHING (reg_info[*p])
|| (enum regexpcode) p[-3] == start_memory)
&& (p + 1) != pend)
{
register unsigned char *p2 = p + 1;
mcnt = 0;
switch (*p2++)
{
case jump_n:
is_a_jump_n = 1;
case finalize_jump:
case maybe_finalize_jump:
case jump:
case dummy_failure_jump:
EXTRACT_NUMBER_AND_INCR (mcnt, p2);
if (is_a_jump_n)
p2 += 2;
break;
}
p2 += mcnt;
/* If the next operation is a jump backwards in the pattern
to an on_failure_jump, exit from the loop by forcing a
failure after pushing on the stack the on_failure_jump's
jump in the pattern, and d. */
if (mcnt < 0 && (enum regexpcode) *p2++ == on_failure_jump)
{
EXTRACT_NUMBER_AND_INCR (mcnt, p2);
PUSH_FAILURE_POINT (p2 + mcnt, d);
goto fail;
}
}
p++;
break;
/* \<digit> has been turned into a `duplicate' command which is
followed by the numeric value of <digit> as the register number. */
case duplicate:
{
int regno = *p++; /* Get which register to match against */
register unsigned char *d2, *dend2;
/* Where in input to try to start matching. */
d2 = regstart[regno];
/* Where to stop matching; if both the place to start and
the place to stop matching are in the same string, then
set to the place to stop, otherwise, for now have to use
the end of the first string. */
dend2 = ((IS_IN_FIRST_STRING (regstart[regno])
== IS_IN_FIRST_STRING (regend[regno]))
? regend[regno] : end_match_1);
while (1)
{
/* If necessary, advance to next segment in register
contents. */
while (d2 == dend2)
{
if (dend2 == end_match_2) break;
if (dend2 == regend[regno]) break;
d2 = string2, dend2 = regend[regno]; /* end of string1 => advance to string2. */
}
/* At end of register contents => success */
if (d2 == dend2) break;
/* If necessary, advance to next segment in data. */
PREFETCH;
/* How many characters left in this segment to match. */
mcnt = dend - d;
/* Want how many consecutive characters we can match in
one shot, so, if necessary, adjust the count. */
if (mcnt > dend2 - d2)
mcnt = dend2 - d2;
/* Compare that many; failure if mismatch, else move
past them. */
if (translate
? bcmp_translate (d, d2, mcnt, translate)
: bcmp (d, d2, mcnt))
goto fail;
d += mcnt, d2 += mcnt;
}
}
break;
case anychar:
PREFETCH; /* Fetch a data character. */
/* Match anything but a newline, maybe even a null. */
if ((translate ? translate[*d] : *d) == '\n'
|| ((obscure_syntax & RE_DOT_NOT_NULL)
&& (translate ? translate[*d] : *d) == '\000'))
goto fail;
SET_REGS_MATCHED;
d++;
break;
case charset:
case charset_not:
{
int not = 0; /* Nonzero for charset_not. */
register int c;
if (*(p - 1) == (unsigned char) charset_not)
not = 1;
PREFETCH; /* Fetch a data character. */
if (translate)
c = translate[*d];
else
c = *d;
if (c < *p * BYTEWIDTH
&& p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
not = !not;
p += 1 + *p;
if (!not) goto fail;
SET_REGS_MATCHED;
d++;
break;
}
case begline:
if ((size1 != 0 && d == string1)
|| (size1 == 0 && size2 != 0 && d == string2)
|| (d && d[-1] == '\n')
|| (size1 == 0 && size2 == 0))
break;
else
goto fail;
case endline:
if (d == end2
|| (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
break;
goto fail;
/* `or' constructs are handled by starting each alternative with
an on_failure_jump that points to the start of the next
alternative. Each alternative except the last ends with a
jump to the joining point. (Actually, each jump except for
the last one really jumps to the following jump, because
tensioning the jumps is a hassle.) */
/* The start of a stupid repeat has an on_failure_jump that points
past the end of the repeat text. This makes a failure point so
that on failure to match a repetition, matching restarts past
as many repetitions have been found with no way to fail and
look for another one. */
/* A smart repeat is similar but loops back to the on_failure_jump
so that each repetition makes another failure point. */
case on_failure_jump:
on_failure:
EXTRACT_NUMBER_AND_INCR (mcnt, p);
PUSH_FAILURE_POINT (p + mcnt, d);
break;
/* The end of a smart repeat has a maybe_finalize_jump back.
Change it either to a finalize_jump or an ordinary jump. */
case maybe_finalize_jump:
EXTRACT_NUMBER_AND_INCR (mcnt, p);
{
register unsigned char *p2 = p;
/* Compare what follows with the beginning of the repeat.
If we can establish that there is nothing that they would
both match, we can change to finalize_jump. */
while (p2 + 1 != pend
&& (*p2 == (unsigned char) stop_memory
|| *p2 == (unsigned char) start_memory))
p2 += 2; /* Skip over reg number. */
if (p2 == pend)
p[-3] = (unsigned char) finalize_jump;
else if (*p2 == (unsigned char) exactn
|| *p2 == (unsigned char) endline)
{
register int c = *p2 == (unsigned char) endline ? '\n' : p2[2];
register unsigned char *p1 = p + mcnt;
/* p1[0] ... p1[2] are an on_failure_jump.
Examine what follows that. */
if (p1[3] == (unsigned char) exactn && p1[5] != c)
p[-3] = (unsigned char) finalize_jump;
else if (p1[3] == (unsigned char) charset
|| p1[3] == (unsigned char) charset_not)
{
int not = p1[3] == (unsigned char) charset_not;
if (c < p1[4] * BYTEWIDTH
&& p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
not = !not;
/* `not' is 1 if c would match. */
/* That means it is not safe to finalize. */
if (!not)
p[-3] = (unsigned char) finalize_jump;
}
}
}
p -= 2; /* Point at relative address again. */
if (p[-1] != (unsigned char) finalize_jump)
{
p[-1] = (unsigned char) jump;
goto nofinalize;
}
/* Note fall through. */
/* The end of a stupid repeat has a finalize_jump back to the
start, where another failure point will be made which will
point to after all the repetitions found so far. */
/* Take off failure points put on by matching on_failure_jump
because didn't fail. Also remove the register information
put on by the on_failure_jump. */
case finalize_jump:
POP_FAILURE_POINT ();
/* Note fall through. */
/* Jump without taking off any failure points. */
case jump:
nofinalize:
EXTRACT_NUMBER_AND_INCR (mcnt, p);
p += mcnt;
break;
case dummy_failure_jump:
/* Normally, the on_failure_jump pushes a failure point, which
then gets popped at finalize_jump. We will end up at
finalize_jump, also, and with a pattern of, say, `a+', we
are skipping over the on_failure_jump, so we have to push
something meaningless for finalize_jump to pop. */
PUSH_FAILURE_POINT (0, 0);
goto nofinalize;
/* Have to succeed matching what follows at least n times. Then
just handle like an on_failure_jump. */
case succeed_n:
EXTRACT_NUMBER (mcnt, p + 2);
/* Originally, this is how many times we HAVE to succeed. */
if (mcnt)
{
mcnt--;
p += 2;
STORE_NUMBER_AND_INCR (p, mcnt);
}
else if (mcnt == 0)
{
p[2] = unused;
p[3] = unused;
goto on_failure;
}
else
{
fprintf (stderr, "regex: the succeed_n's n is not set.\n");
exit (1);
}
break;
case jump_n:
EXTRACT_NUMBER (mcnt, p + 2);
/* Originally, this is how many times we CAN jump. */
if (mcnt)
{
mcnt--;
STORE_NUMBER(p + 2, mcnt);
goto nofinalize; /* Do the jump without taking off
any failure points. */
}
/* If don't have to jump any more, skip over the rest of command. */
else
p += 4;
break;
case set_number_at:
{
register unsigned char *p1;
EXTRACT_NUMBER_AND_INCR (mcnt, p);
p1 = p + mcnt;
EXTRACT_NUMBER_AND_INCR (mcnt, p);
STORE_NUMBER (p1, mcnt);
break;
}
/* Ignore these. Used to ignore the n of succeed_n's which
currently have n == 0. */
case unused:
break;
case wordbound:
if (AT_WORD_BOUNDARY)
break;
goto fail;
case notwordbound:
if (AT_WORD_BOUNDARY)
goto fail;
break;
case wordbeg:
if (IS_A_LETTER (d) && (!IS_A_LETTER (d - 1) || AT_STRINGS_BEG))
break;
goto fail;
case wordend:
/* Have to check if AT_STRINGS_BEG before looking at d - 1. */
if (!AT_STRINGS_BEG && IS_A_LETTER (d - 1)
&& (!IS_A_LETTER (d) || AT_STRINGS_END))
break;
goto fail;
#ifdef emacs
case before_dot:
if (PTR_CHAR_POS (d) >= point)
goto fail;
break;
case at_dot:
if (PTR_CHAR_POS (d) != point)
goto fail;
break;
case after_dot:
if (PTR_CHAR_POS (d) <= point)
goto fail;
break;
case wordchar:
mcnt = (int) Sword;
goto matchsyntax;
case syntaxspec:
mcnt = *p++;
matchsyntax:
PREFETCH;
if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
SET_REGS_MATCHED;
break;
case notwordchar:
mcnt = (int) Sword;
goto matchnotsyntax;
case notsyntaxspec:
mcnt = *p++;
matchnotsyntax:
PREFETCH;
if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
SET_REGS_MATCHED;
break;
#else /* not emacs */
case wordchar:
PREFETCH;
if (!IS_A_LETTER (d))
goto fail;
SET_REGS_MATCHED;
break;
case notwordchar:
PREFETCH;
if (IS_A_LETTER (d))
goto fail;
SET_REGS_MATCHED;
break;
#endif /* not emacs */
case begbuf:
if (AT_STRINGS_BEG)
break;
goto fail;
case endbuf:
if (AT_STRINGS_END)
break;
goto fail;
case exactn:
/* Match the next few pattern characters exactly.
mcnt is how many characters to match. */
mcnt = *p++;
/* This is written out as an if-else so we don't waste time
testing `translate' inside the loop. */
if (translate)
{
do
{
PREFETCH;
if (translate[*d++] != *p++) goto fail;
}
while (--mcnt);
}
else
{
do
{
PREFETCH;
if (*d++ != *p++) goto fail;
}
while (--mcnt);
}
SET_REGS_MATCHED;
break;
}
continue; /* Successfully executed one pattern command; keep going. */
/* Jump here if any matching operation fails. */
fail:
if (stackp != stackb)
/* A restart point is known. Restart there and pop it. */
{
short last_used_reg, this_reg;
/* If this failure point is from a dummy_failure_point, just
skip it. */
if (!stackp[-2])
{
POP_FAILURE_POINT ();
goto fail;
}
d = *--stackp;
p = *--stackp;
if (d >= string1 && d <= end1)
dend = end_match_1;
/* Restore register info. */
last_used_reg = (short) *--stackp;
/* Make the ones that weren't saved -1 or 0 again. */
for (this_reg = RE_NREGS - 1; this_reg > last_used_reg; this_reg--)
{
regend[this_reg] = (unsigned char *) -1;
regstart[this_reg] = (unsigned char *) -1;
IS_ACTIVE (reg_info[this_reg]) = 0;
MATCHED_SOMETHING (reg_info[this_reg]) = 0;
}
/* And restore the rest from the stack. */
for ( ; this_reg > 0; this_reg--)
{
reg_info[this_reg] = *(struct register_info *) *--stackp;
regend[this_reg] = *--stackp;
regstart[this_reg] = *--stackp;
}
}
else
break; /* Matching at this starting point really fails. */
}
if (best_regs_set)
goto restore_best_regs;
return -1; /* Failure to match. */
}
static int
bcmp_translate (s1, s2, len, translate)
unsigned char *s1, *s2;
register int len;
unsigned char *translate;
{
register unsigned char *p1 = s1, *p2 = s2;
while (len)
{
if (translate [*p1++] != translate [*p2++]) return 1;
len--;
}
return 0;
}
/* Entry points compatible with 4.2 BSD regex library. */
#ifndef emacs
static struct re_pattern_buffer re_comp_buf;
char *
re_comp (s)
char *s;
{
if (!s)
{
if (!re_comp_buf.buffer)
return "No previous regular expression";
return 0;
}
if (!re_comp_buf.buffer)
{
if (!(re_comp_buf.buffer = (char *) malloc (200)))
return "Memory exhausted";
re_comp_buf.allocated = 200;
if (!(re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH)))
return "Memory exhausted";
}
return re_compile_pattern (s, strlen (s), &re_comp_buf);
}
int
re_exec (s)
char *s;
{
int len = strlen (s);
return 0 <= re_search (&re_comp_buf, s, len, 0, len,
(struct re_registers *) 0);
}
#endif /* not emacs */
#ifdef test
#include <stdio.h>
/* Indexed by a character, gives the upper case equivalent of the
character. */
char upcase[0400] =
{ 000, 001, 002, 003, 004, 005, 006, 007,
010, 011, 012, 013, 014, 015, 016, 017,
020, 021, 022, 023, 024, 025, 026, 027,
030, 031, 032, 033, 034, 035, 036, 037,
040, 041, 042, 043, 044, 045, 046, 047,
050, 051, 052, 053, 054, 055, 056, 057,
060, 061, 062, 063, 064, 065, 066, 067,
070, 071, 072, 073, 074, 075, 076, 077,
0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
};
#ifdef canned
#include "tests.h"
typedef enum { extended_test, basic_test } test_type;
/* Use this to run the tests we've thought of. */
void
main ()
{
test_type t = extended_test;
if (t == basic_test)
{
printf ("Running basic tests:\n\n");
test_posix_basic ();
}
else if (t == extended_test)
{
printf ("Running extended tests:\n\n");
test_posix_extended ();
}
}
#else /* not canned */
/* Use this to run interactive tests. */
void
main (argc, argv)
int argc;
char **argv;
{
char pat[80];
struct re_pattern_buffer buf;
int i;
char c;
char fastmap[(1 << BYTEWIDTH)];
/* Allow a command argument to specify the style of syntax. */
if (argc > 1)
obscure_syntax = atoi (argv[1]);
buf.allocated = 40;
buf.buffer = (char *) malloc (buf.allocated);
buf.fastmap = fastmap;
buf.translate = upcase;
while (1)
{
gets (pat);
if (*pat)
{
re_compile_pattern (pat, strlen(pat), &buf);
for (i = 0; i < buf.used; i++)
printchar (buf.buffer[i]);
putchar ('\n');
printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
re_compile_fastmap (&buf);
printf ("Allowed by fastmap: ");
for (i = 0; i < (1 << BYTEWIDTH); i++)
if (fastmap[i]) printchar (i);
putchar ('\n');
}
gets (pat); /* Now read the string to match against */
i = re_match (&buf, pat, strlen (pat), 0, 0);
printf ("Match value %d.\n", i);
}
}
#endif
#ifdef NOTDEF
print_buf (bufp)
struct re_pattern_buffer *bufp;
{
int i;
printf ("buf is :\n----------------\n");
for (i = 0; i < bufp->used; i++)
printchar (bufp->buffer[i]);
printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
printf ("Allowed by fastmap: ");
for (i = 0; i < (1 << BYTEWIDTH); i++)
if (bufp->fastmap[i])
printchar (i);
printf ("\nAllowed by translate: ");
if (bufp->translate)
for (i = 0; i < (1 << BYTEWIDTH); i++)
if (bufp->translate[i])
printchar (i);
printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
}
#endif /* NOTDEF */
printchar (c)
char c;
{
if (c < 040 || c >= 0177)
{
putchar ('\\');
putchar (((c >> 6) & 3) + '0');
putchar (((c >> 3) & 7) + '0');
putchar ((c & 7) + '0');
}
else
putchar (c);
}
error (string)
char *string;
{
puts (string);
exit (1);
}
#endif /* test */