141353Sbostic /* 241353Sbostic * regsub 341353Sbostic * 441353Sbostic * Copyright (c) 1986 by University of Toronto. 541353Sbostic * Written by Henry Spencer. Not derived from licensed software. 641353Sbostic * 741353Sbostic * Permission is granted to anyone to use this software for any 841353Sbostic * purpose on any computer system, and to redistribute it freely, 941353Sbostic * subject to the following restrictions: 1041353Sbostic * 1141353Sbostic * 1. The author is not responsible for the consequences of use of 1241353Sbostic * this software, no matter how awful, even if they arise 1341353Sbostic * from defects in it. 1441353Sbostic * 1541353Sbostic * 2. The origin of this software must not be misrepresented, either 1641353Sbostic * by explicit claim or by omission. 1741353Sbostic * 1841353Sbostic * 3. Altered versions must be plainly marked as such, and must not 1941353Sbostic * be misrepresented as being the original software. 2041353Sbostic */ 21*46619Sbostic #include <regexp.h> 2241353Sbostic #include <stdio.h> 23*46619Sbostic #include <string.h> 2441353Sbostic #include "regmagic.h" 2541353Sbostic 2641353Sbostic #ifndef CHARBITS 2741353Sbostic #define UCHARAT(p) ((int)*(unsigned char *)(p)) 2841353Sbostic #else 2941353Sbostic #define UCHARAT(p) ((int)*(p)&CHARBITS) 3041353Sbostic #endif 3141353Sbostic 3241353Sbostic /* 3341353Sbostic - regsub - perform substitutions after a regexp match 3441353Sbostic */ 3541353Sbostic void 3641353Sbostic regsub(prog, source, dest) 37*46619Sbostic const regexp *prog; 38*46619Sbostic const char *source; 3941353Sbostic char *dest; 4041353Sbostic { 4141353Sbostic register char *src; 4241353Sbostic register char *dst; 4341353Sbostic register char c; 4441353Sbostic register int no; 4541353Sbostic register int len; 4641353Sbostic extern char *strncpy(); 4741353Sbostic 4841353Sbostic if (prog == NULL || source == NULL || dest == NULL) { 4941353Sbostic regerror("NULL parm to regsub"); 5041353Sbostic return; 5141353Sbostic } 5241353Sbostic if (UCHARAT(prog->program) != MAGIC) { 5341353Sbostic regerror("damaged regexp fed to regsub"); 5441353Sbostic return; 5541353Sbostic } 5641353Sbostic 57*46619Sbostic src = (char *)source; 5841353Sbostic dst = dest; 5941353Sbostic while ((c = *src++) != '\0') { 6041353Sbostic if (c == '&') 6141353Sbostic no = 0; 6241353Sbostic else if (c == '\\' && '0' <= *src && *src <= '9') 6341353Sbostic no = *src++ - '0'; 6441353Sbostic else 6541353Sbostic no = -1; 6641353Sbostic if (no < 0) { /* Ordinary character. */ 6741353Sbostic if (c == '\\' && (*src == '\\' || *src == '&')) 6841353Sbostic c = *src++; 6941353Sbostic *dst++ = c; 7041353Sbostic } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) { 7141353Sbostic len = prog->endp[no] - prog->startp[no]; 7241353Sbostic (void) strncpy(dst, prog->startp[no], len); 7341353Sbostic dst += len; 7441353Sbostic if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */ 7541353Sbostic regerror("damaged match string"); 7641353Sbostic return; 7741353Sbostic } 7841353Sbostic } 7941353Sbostic } 8041353Sbostic *dst++ = '\0'; 8141353Sbostic } 82