home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / REGEXP.ZIP / REGSUB.C < prev    next >
Text File  |  1986-04-26  |  2KB  |  82 lines

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