home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / STVI369G.ZIP / REGSUB.C < prev    next >
C/C++ Source or Header  |  1990-05-01  |  3KB  |  95 lines

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