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