1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
25 * OBSOLETE Sfio_t buffer interface -- use regsubcomp(),regsubexec()
31 * do a single substitution
35 subold(register Sfio_t
* dp
, const char* op
, register const char* sp
, size_t nmatch
, register regmatch_t
* match
, register regflags_t flags
, int sre
)
43 f
= flags
&= (REG_SUB_LOWER
|REG_SUB_UPPER
);
51 if (!sre
|| *sp
!= '(')
62 sfputc(dp
, chresc(sp
- 1, &s
));
92 case REG_SUB_UPPER
|REG_SUB_LOWER
:
110 case '0': case '1': case '2': case '3': case '4':
111 case '5': case '6': case '7': case '8': case '9':
115 c
= c
* 10 + *sp
++ - '0';
118 if (sre
&& *sp
!= ')')
172 flags
= REG_SUB_LOWER
;
185 flags
= REG_SUB_UPPER
;
190 sfputc(dp
, chresc(sp
- 2, &s
));
200 if (c
< 0 || *sp
!= ')')
210 s
= (char*)op
+ match
[c
].rm_so
;
211 e
= (char*)op
+ match
[c
].rm_eo
;
225 case REG_SUB_UPPER
|REG_SUB_LOWER
:
238 * ed(1) style substitute using matches from last regexec()
242 regsub(const regex_t
* p
, Sfio_t
* dp
, const char* op
, const char* sp
, size_t nmatch
, regmatch_t
* match
, regflags_t flags
)
248 if ((p
->env
->flags
& REG_NOSUB
) || !nmatch
)
249 return fatal(p
->env
->disc
, REG_BADPAT
, NiL
);
250 m
= (flags
>> 16) & 0x3fff;
251 sre
= !!(p
->env
->flags
& REG_SHELL
);
256 sfwrite(dp
, op
, match
->rm_eo
);
259 sfwrite(dp
, op
, match
->rm_so
);
260 if (r
= subold(dp
, op
, sp
, nmatch
, match
, flags
, sre
))
261 return fatal(p
->env
->disc
, r
, NiL
);
264 } while ((m
> 0 || (flags
& REG_SUB_ALL
)) && !(r
= regexec(p
, op
, nmatch
, match
, p
->env
->flags
|(match
->rm_so
== match
->rm_eo
? REG_ADVANCE
: 0))));
265 if (r
&& r
!= REG_NOMATCH
)
266 return fatal(p
->env
->disc
, r
, NiL
);