home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / util / jade-3.0.lha / Jade / src / regexp / regsub.c < prev   
Encoding:
C/C++ Source or Header  |  1993-10-30  |  3.2 KB  |  136 lines

  1. /*
  2.  * regsub @(#)regsub.c    1.3 of 2 April 86
  3.  *
  4.  * Copyright (c) 1986 by University of Toronto. Written by Henry Spencer.  Not
  5.  * derived from licensed software.
  6.  *
  7.  * Permission is granted to anyone to use this software for any purpose on any
  8.  * computer system, and to redistribute it freely, subject to the following
  9.  * restrictions:
  10.  *
  11.  * 1. The author is not responsible for the consequences of use of this
  12.  * software, no matter how awful, even if they arise from defects in it.
  13.  *
  14.  * 2. The origin of this software must not be misrepresented, either by explicit
  15.  * claim or by omission.
  16.  *
  17.  * 3. Altered versions must be plainly marked as such, and must not be
  18.  * misrepresented as being the original software.
  19.  */
  20.  
  21. /*
  22.  * CHANGED, 14-Jan-93, by J.Harper,
  23.  * added #ifdef __STDC__ prototype sections so I can use registerized
  24.  * arguments
  25.  *
  26.  * also, I added the regsublen() function for safety & general usefulness
  27.  * (regsub() has no checks for overstepping its dest string)
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include "regexp.h"
  32. #include "regmagic.h"
  33.  
  34. #ifndef CHARBITS
  35. #define UCHARAT(p)    ((int)*(unsigned char *)(p))
  36. #else
  37. #define UCHARAT(p)    ((int)*(p)&CHARBITS)
  38. #endif
  39.  
  40. #ifdef __STDC__
  41. #include <string.h>
  42. void    regerror(char *);
  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.  
  60.     if (prog == NULL || source == NULL || dest == NULL) {
  61.     regerror("NULL parm to regsub");
  62.     return;
  63.     }
  64.     if (UCHARAT(prog->program) != MAGIC) {
  65.     regerror("damaged regexp fed to regsub");
  66.     return;
  67.     }
  68.     src = source;
  69.     dst = dest;
  70.     while ((c = *src++) != '\0') {
  71.     if (c == '&')
  72.         no = 0;
  73.     else if (c == '\\' && '0' <= *src && *src <= '9')
  74.         no = *src++ - '0';
  75.     else
  76.         no = -1;
  77.  
  78.     if (no < 0) {        /* Ordinary character. */
  79.         if (c == '\\' && (*src == '\\' || *src == '&'))
  80.         c = *src++;
  81.         *dst++ = c;
  82.     } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
  83.         len = prog->endp[no] - prog->startp[no];
  84.         (void) strncpy(dst, prog->startp[no], len);
  85.         dst += len;
  86.         if (len != 0 && *(dst - 1) == '\0') {       /* strncpy hit NUL. */
  87.         regerror("damaged match string");
  88.         return;
  89.         }
  90.     }
  91.     }
  92.     *dst++ = '\0';
  93. }
  94.  
  95. /*
  96.  * - regsublen - dummy regsub() returning length of contructed string,
  97.  * including terminating '\0'
  98.  */
  99. int
  100. regsublen(prog, source)
  101.     regexp       *prog;
  102.     char       *source;
  103. {
  104.     register char  *src;
  105.     register char   c;
  106.     register int    no;
  107.     register int    dstlen = 1;
  108.  
  109.     if (prog == NULL || source == NULL) {
  110.     regerror("NULL parm to regsublen");
  111.     return(0);
  112.     }
  113.     if (UCHARAT(prog->program) != MAGIC) {
  114.     regerror("damaged regexp fed to regsublen");
  115.     return(0);
  116.     }
  117.     src = source;
  118.     while ((c = *src++) != '\0') {
  119.     if (c == '&')
  120.         no = 0;
  121.     else if (c == '\\' && '0' <= *src && *src <= '9')
  122.         no = *src++ - '0';
  123.     else
  124.         no = -1;
  125.  
  126.     if (no < 0) {        /* Ordinary character. */
  127.         if (c == '\\' && (*src == '\\' || *src == '&'))
  128.         c = *src++;
  129.         dstlen++;
  130.     } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
  131.         dstlen += prog->endp[no] - prog->startp[no];
  132.     }
  133.     }
  134.     return(dstlen);
  135. }
  136.