/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2010 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* posix regex ed(1) style substitute execute
*/
#include "reglib.h"
#define NEED(p,b,n,r) \
do \
{ \
{ \
if (a < n) \
a = roundof(n, 128); \
a *= 2; \
{ \
c = REG_ESPACE; \
r; \
} \
} \
} while (0)
#define PUTC(p,b,x,r) \
do \
{ \
NEED(p, b, 1, r); \
*(b)->re_cur++ = (x); \
} while (0)
#define PUTS(p,b,x,z,r) \
do if (z) \
{ \
NEED(p, b, z, r); \
(b)->re_cur += (z); \
} while (0)
/*
* do a single substitution
*/
static int
sub(const regex_t* p, register regsub_t* b, const char* ss, register regsubop_t* op, size_t nmatch, register regmatch_t* match)
{
register char* s;
register char* e;
register int c;
for (;; op++)
{
{
case -1:
break;
case 0:
return REG_ESUBREG;
continue;
s = (char*)ss + c;
continue;
e = (char*)ss + c;
NEED(p, b, e - s, return c);
{
case REG_SUB_UPPER:
while (s < e)
{
c = *s++;
if (islower(c))
c = toupper(c);
*b->re_cur++ = c;
}
break;
case REG_SUB_LOWER:
while (s < e)
{
c = *s++;
if (isupper(c))
c = tolower(c);
*b->re_cur++ = c;
}
break;
case REG_SUB_UPPER|REG_SUB_LOWER:
while (s < e)
{
c = *s++;
if (isupper(c))
c = tolower(c);
else if (islower(c))
c = toupper(c);
*b->re_cur++ = c;
}
break;
default:
while (s < e)
*b->re_cur++ = *s++;
break;
}
continue;
default:
while (s < e)
*b->re_cur++ = *s++;
continue;
}
break;
}
return 0;
}
/*
* ed(1) style substitute using matches from last regexec()
*/
int
{
register int c;
register regsub_t* b;
const char* e;
int m;
b = p->re_sub;
m = b->re_min;
c = 0;
for (;;)
{
if (--m > 0)
else
{
}
if (m <= 0 && !(b->re_flags & REG_SUB_ALL) || !*s)
break;
if (c = regnexec(p, s, e - s, nmatch, match, p->env->flags|(match->rm_so == match->rm_eo ? REG_ADVANCE : 0)))
{
if (c != REG_NOMATCH)
break;
}
{
c = 1;
}
}
while (s < e)
{
c = *s++;
}
*b->re_cur = 0;
return 0;
}