v8regsub.c

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

Generated on Fri Apr 14 22:57:27 2006 for minix by  doxygen 1.4.6