home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume36 / translit / part05 / reg_sub.c < prev    next >
C/C++ Source or Header  |  1993-03-22  |  2KB  |  88 lines

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