home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ed.zip / regsub.c < prev   
C/C++ Source or Header  |  1994-08-26  |  3KB  |  88 lines

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