home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 100-199 / ff197.lzh / Stevie / regsub.c < prev    next >
C/C++ Source or Header  |  1989-03-28  |  3KB  |  107 lines

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