1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* regsub
|
|
|
|
*
|
|
|
|
* Copyright (c) 1986 by University of Toronto.
|
|
|
|
* Written by Henry Spencer. Not derived from licensed software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any
|
|
|
|
* purpose on any computer system, and to redistribute it freely,
|
|
|
|
* subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The author is not responsible for the consequences of use of
|
|
|
|
* this software, no matter how awful, even if they arise
|
|
|
|
* from defects in it.
|
|
|
|
*
|
|
|
|
* 2. The origin of this software must not be misrepresented, either
|
|
|
|
* by explicit claim or by omission.
|
|
|
|
*
|
|
|
|
* 3. Altered versions must be plainly marked as such, and must not
|
|
|
|
* be misrepresented as being the original software.
|
|
|
|
*/
|
1993-08-02 21:48:44 +04:00
|
|
|
|
1997-10-09 14:20:07 +04:00
|
|
|
#include <sys/cdefs.h>
|
2000-09-14 05:24:31 +04:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
|
|
|
__RCSID("$NetBSD: regsub.c,v 1.9 2000/09/14 01:24:32 msaitoh Exp $");
|
|
|
|
#endif /* LIBC_SCCS and not lint */
|
1993-08-02 21:48:44 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <regexp.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "regmagic.h"
|
|
|
|
|
|
|
|
#ifndef CHARBITS
|
|
|
|
#define UCHARAT(p) ((int)*(unsigned char *)(p))
|
|
|
|
#else
|
|
|
|
#define UCHARAT(p) ((int)*(p)&CHARBITS)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
- regsub - perform substitutions after a regexp match
|
|
|
|
*/
|
|
|
|
void
|
1998-09-15 00:25:02 +04:00
|
|
|
__compat_regsub(prog, source, dest)
|
1993-03-21 12:45:37 +03:00
|
|
|
const regexp *prog;
|
|
|
|
const char *source;
|
|
|
|
char *dest;
|
|
|
|
{
|
1998-02-03 21:49:08 +03:00
|
|
|
char *src;
|
|
|
|
char *dst;
|
|
|
|
char c;
|
|
|
|
int no;
|
|
|
|
int len;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (prog == NULL || source == NULL || dest == NULL) {
|
|
|
|
regerror("NULL parm to regsub");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (UCHARAT(prog->program) != MAGIC) {
|
|
|
|
regerror("damaged regexp fed to regsub");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-05-06 21:09:31 +04:00
|
|
|
/* LINTED const castaway */
|
1993-03-21 12:45:37 +03:00
|
|
|
src = (char *)source;
|
|
|
|
dst = dest;
|
|
|
|
while ((c = *src++) != '\0') {
|
|
|
|
if (c == '&')
|
|
|
|
no = 0;
|
|
|
|
else if (c == '\\' && '0' <= *src && *src <= '9')
|
|
|
|
no = *src++ - '0';
|
|
|
|
else
|
|
|
|
no = -1;
|
|
|
|
if (no < 0) { /* Ordinary character. */
|
|
|
|
if (c == '\\' && (*src == '\\' || *src == '&'))
|
|
|
|
c = *src++;
|
|
|
|
*dst++ = c;
|
|
|
|
} else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
|
|
|
|
len = prog->endp[no] - prog->startp[no];
|
1999-05-06 21:09:31 +04:00
|
|
|
(void) strncpy(dst, prog->startp[no], (size_t)len);
|
1993-03-21 12:45:37 +03:00
|
|
|
dst += len;
|
|
|
|
if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
|
|
|
|
regerror("damaged match string");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dst++ = '\0';
|
|
|
|
}
|