8000 minix/commands/elvis/regexp.c at R3.2.0 · dtytomi/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
934 lines (817 loc) · 18.3 KB

regexp.c

File metadata and controls

934 lines (817 loc) · 18.3 KB
}
re->minlen = 0;
re->first = 0;
re->bol = 0;
/* copy the string into it, translating ^ and $ as needed */
for (src = exp, dest = re->program + 1; *src; src++)
{
switch (*src)
{
case '^':
if (src == exp)
{
re->bol += 1;
}
else
{
*dest++ = '^';
re->minlen++;
}
break;
case '$':
if (!src[1])
{
re->bol += 2;
}
else
{
*dest++ = '$';
re->minlen++;
}
break;
case '\\':
if (src[1])
{
*dest++ = *++src;
re->minlen++;
}
else
{
regerror("extra \\ at end of regular expression");
}
break;
default:
*dest++ = *src;
re->minlen++;
}
}
*dest = '\0';
return re;
}
/* This "helper" function checks for a match at a given location. It returns
* 1 if it matches, 0 if it doesn't match here but might match later on in the
* string, or -1 if it could not possibly match
*/
static int reghelp(prog, string, bolflag)
struct regexp *prog;
char *string;
int bolflag;
{
char *scan;
char *str;
/* if ^, then require bolflag */
if ((prog->bol & 1) && !bolflag)
{
return -1;
}
/* if it matches, then it will start here */
prog->startp[0] = string;
/* compare, possibly ignoring case */
if (*o_ignorecase)
{
for (scan = &prog->program[1]; *scan; scan++, string++)
if (tolower(*scan) != tolower(*string))
return *string ? 0 : -1;
}
else
{
for (scan = &prog->program[1]; *scan; scan++, string++)
if (*scan != *string)
return *string ? 0 : -1;
}
/* if $, then require string to end here, too */
if ((prog->bol & 2) && *string)
{
return 0;
}
/* if we get to here, it matches */
prog->endp[0] = string;
return 1;
}
int regexec(prog, string, bolflag)
struct regexp *prog;
char *string;
int bolflag;
{
int rc;
/* keep trying to match it */
for (rc = reghelp(prog, string, bolflag); rc == 0; rc = reghelp(prog, string, 0))
{
string++;
}
/* did we match? */
return rc == 1;
}
#endif
0