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