xref: /csrg-svn/lib/libcompat/regexp/regsub.c (revision 41353)
1*41353Sbostic /*
2*41353Sbostic  * regsub
3*41353Sbostic  *
4*41353Sbostic  *	Copyright (c) 1986 by University of Toronto.
5*41353Sbostic  *	Written by Henry Spencer.  Not derived from licensed software.
6*41353Sbostic  *
7*41353Sbostic  *	Permission is granted to anyone to use this software for any
8*41353Sbostic  *	purpose on any computer system, and to redistribute it freely,
9*41353Sbostic  *	subject to the following restrictions:
10*41353Sbostic  *
11*41353Sbostic  *	1. The author is not responsible for the consequences of use of
12*41353Sbostic  *		this software, no matter how awful, even if they arise
13*41353Sbostic  *		from defects in it.
14*41353Sbostic  *
15*41353Sbostic  *	2. The origin of this software must not be misrepresented, either
16*41353Sbostic  *		by explicit claim or by omission.
17*41353Sbostic  *
18*41353Sbostic  *	3. Altered versions must be plainly marked as such, and must not
19*41353Sbostic  *		be misrepresented as being the original software.
20*41353Sbostic  */
21*41353Sbostic #include <stdio.h>
22*41353Sbostic #include <regexp.h>
23*41353Sbostic #include "regmagic.h"
24*41353Sbostic 
25*41353Sbostic #ifndef CHARBITS
26*41353Sbostic #define	UCHARAT(p)	((int)*(unsigned char *)(p))
27*41353Sbostic #else
28*41353Sbostic #define	UCHARAT(p)	((int)*(p)&CHARBITS)
29*41353Sbostic #endif
30*41353Sbostic 
31*41353Sbostic /*
32*41353Sbostic  - regsub - perform substitutions after a regexp match
33*41353Sbostic  */
34*41353Sbostic void
35*41353Sbostic regsub(prog, source, dest)
36*41353Sbostic regexp *prog;
37*41353Sbostic char *source;
38*41353Sbostic char *dest;
39*41353Sbostic {
40*41353Sbostic 	register char *src;
41*41353Sbostic 	register char *dst;
42*41353Sbostic 	register char c;
43*41353Sbostic 	register int no;
44*41353Sbostic 	register int len;
45*41353Sbostic 	extern char *strncpy();
46*41353Sbostic 
47*41353Sbostic 	if (prog == NULL || source == NULL || dest == NULL) {
48*41353Sbostic 		regerror("NULL parm to regsub");
49*41353Sbostic 		return;
50*41353Sbostic 	}
51*41353Sbostic 	if (UCHARAT(prog->program) != MAGIC) {
52*41353Sbostic 		regerror("damaged regexp fed to regsub");
53*41353Sbostic 		return;
54*41353Sbostic 	}
55*41353Sbostic 
56*41353Sbostic 	src = source;
57*41353Sbostic 	dst = dest;
58*41353Sbostic 	while ((c = *src++) != '\0') {
59*41353Sbostic 		if (c == '&')
60*41353Sbostic 			no = 0;
61*41353Sbostic 		else if (c == '\\' && '0' <= *src && *src <= '9')
62*41353Sbostic 			no = *src++ - '0';
63*41353Sbostic 		else
64*41353Sbostic 			no = -1;
65*41353Sbostic  		if (no < 0) {	/* Ordinary character. */
66*41353Sbostic  			if (c == '\\' && (*src == '\\' || *src == '&'))
67*41353Sbostic  				c = *src++;
68*41353Sbostic  			*dst++ = c;
69*41353Sbostic  		} else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
70*41353Sbostic 			len = prog->endp[no] - prog->startp[no];
71*41353Sbostic 			(void) strncpy(dst, prog->startp[no], len);
72*41353Sbostic 			dst += len;
73*41353Sbostic 			if (len != 0 && *(dst-1) == '\0') {	/* strncpy hit NUL. */
74*41353Sbostic 				regerror("damaged match string");
75*41353Sbostic 				return;
76*41353Sbostic 			}
77*41353Sbostic 		}
78*41353Sbostic 	}
79*41353Sbostic 	*dst++ = '\0';
80*41353Sbostic }
81