home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Appls / miniedit / regsub.c < prev   
Encoding:
C/C++ Source or Header  |  1988-05-11  |  2.1 KB  |  92 lines  |  [TEXT/????]

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