C:/Users/Dennis/src/lang/Life_start/Life/life-1.02/source/regexp/regsub.c

Go to the documentation of this file.
00001 /*
00002  * regsub
00003  * @(#)regsub.c 1.3 of 2 April 86
00004  *
00005  *      Copyright (c) 1986 by University of Toronto.
00006  *      Written by Henry Spencer.  Not derived from licensed software.
00007  *
00008  *      Permission is granted to anyone to use this software for any
00009  *      purpose on any computer system, and to redistribute it freely,
00010  *      subject to the following restrictions:
00011  *
00012  *      1. The author is not responsible for the consequences of use of
00013  *              this software, no matter how awful, even if they arise
00014  *              from defects in it.
00015  *
00016  *      2. The origin of this software must not be misrepresented, either
00017  *              by explicit claim or by omission.
00018  *
00019  *      3. Altered versions must be plainly marked as such, and must not
00020  *              be misrepresented as being the original software.
00021  */
00022 #include <stdio.h>
00023 #include "regexp.h"
00024 #include "regmagic.h"
00025 
00026 #ifndef CHARBITS
00027 #define UCHARAT(p)      ((int)*(unsigned char *)(p))
00028 #else
00029 #define UCHARAT(p)      ((int)*(p)&CHARBITS)
00030 #endif
00031 
00032 /*
00033  - regsub - perform substitutions after a regexp match
00034  */
00035 void
00036 regsub(prog, source, dest)
00037 regexp *prog;
00038 char *source;
00039 char *dest;
00040 {
00041         register char *src;
00042         register char *dst;
00043         register char c;
00044         register int no;
00045         register int len;
00046         extern char *strncpy();
00047 
00048         if (prog == NULL || source == NULL || dest == NULL) {
00049                 regerror("NULL parm to regsub");
00050                 return;
00051         }
00052         if (UCHARAT(prog->program) != MAGIC) {
00053                 regerror("damaged regexp fed to regsub");
00054                 return;
00055         }
00056 
00057         src = source;
00058         dst = dest;
00059         while ((c = *src++) != '\0') {
00060                 if (c == '&')
00061                         no = 0;
00062                 else if (c == '\\' && '0' <= *src && *src <= '9')
00063                         no = *src++ - '0';
00064                 else
00065                         no = -1;
00066 
00067                 if (no < 0) {   /* Ordinary character. */
00068                         if (c == '\\' && (*src == '\\' || *src == '&'))
00069                                 c = *src++;
00070                         *dst++ = c;
00071                 } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
00072                         len = prog->endp[no] - prog->startp[no];
00073                         (void) strncpy(dst, prog->startp[no], len);
00074                         dst += len;
00075                         if (len != 0 && *(dst-1) == '\0') {     /* strncpy hit NUL. */
00076                                 regerror("damaged match string");
00077                                 return;
00078                         }
00079                 }
00080         }
00081         *dst++ = '\0';
00082 }

Generated on Sat Jan 26 08:48:07 2008 for WildLife by  doxygen 1.5.4