home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / less / regsub.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-27  |  2.0 KB  |  84 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. #include <stdio.h>
  23. #include <string.h>
  24. #include <regexp.h>
  25. #include "regmagic.h"
  26.  
  27. #ifndef CHARBITS
  28. #define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  29. #else
  30. #define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  31. #endif
  32.  
  33. /*
  34.  - regsub - perform substitutions after a regexp match
  35.  */
  36. void
  37. regsub(prog, source, dest)
  38. regexp *prog;
  39. char *source;
  40. char *dest;
  41. {
  42.     register char *src;
  43.     register char *dst;
  44.     register char c;
  45.     register int no;
  46.     register int len;
  47.     extern char *strncpy();
  48.  
  49.     if (prog == NULL || source == NULL || dest == NULL) {
  50.         regerror("NULL parm to regsub");
  51.         return;
  52.     }
  53.     if (UCHARAT(prog->program) != MAGIC) {
  54.         regerror("damaged regexp fed to regsub");
  55.         return;
  56.     }
  57.  
  58.     src = source;
  59.     dst = dest;
  60.     while ((c = *src++) != '\0') {
  61.         if (c == '&')
  62.             no = 0;
  63.         else if (c == '\\' && '0' <= *src && *src <= '9')
  64.             no = *src++ - '0';
  65.         else
  66.             no = -1;
  67.  
  68.         if (no < 0) {    /* Ordinary character. */
  69.             if (c == '\\' && (*src == '\\' || *src == '&'))
  70.                 c = *src++;
  71.             *dst++ = c;
  72.         } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
  73.             len = prog->endp[no] - prog->startp[no];
  74.             (void) strncpy(dst, prog->startp[no], len);
  75.             dst += len;
  76.             if (len != 0 && *(dst-1) == '\0') {    /* strncpy hit NUL. */
  77.                 regerror("damaged match string");
  78.                 return;
  79.             }
  80.         }
  81.     }
  82.     *dst++ = '\0';
  83. }
  84.