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