home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / regsup.c < prev    next >
C/C++ Source or Header  |  1992-09-17  |  2KB  |  91 lines

  1. /* ALTERED VERSION -- see below */
  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.  * Modified by Eric Smith for use with the MiNT library.
  22.  */
  23.  
  24. #include <string.h>
  25. #include <regexp.h>
  26. #include <stdio.h>
  27.  
  28. /* The first byte of the regexp internal "program" is actually this magic
  29.  * number; the start node begins in the second byte.
  30.  */
  31. #define    MAGIC    0234
  32.  
  33. #ifndef __GNUC__
  34. #define CHARBITS 0377
  35. #endif
  36.  
  37. #ifndef CHARBITS
  38. #define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  39. #else
  40. #define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  41. #endif
  42.  
  43. /*
  44.  - regsub - perform substitutions after a regexp match
  45.  */
  46. void regsub(prog, source, dest)
  47. regexp *prog;
  48. char *source;
  49. char *dest;
  50. {
  51.   register char *src;
  52.   register char *dst;
  53.   register char c;
  54.   register int no;
  55.   register int len;
  56.  
  57.   if (prog == (regexp *)NULL || source == (char *)NULL || dest == (char *)NULL) {
  58.     regerror("NULL parm to regsub");
  59.     return;
  60.   }
  61.   if (UCHARAT(prog->program) != MAGIC) {
  62.     regerror("damaged regexp fed to regsub");
  63.     return;
  64.   }
  65.   src = source;
  66.   dst = dest;
  67.   while ((c = *src++) != '\0') {
  68.     if (c == '&')
  69.         no = 0;
  70.     else if (c == '\\' && '0' <= *src && *src <= '9')
  71.         no = *src++ - '0';
  72.     else
  73.         no = -1;
  74.  
  75.     if (no < 0) {        /* Ordinary character. */
  76.         if (c == '\\' && (*src == '\\' || *src == '&')) c = *src++;
  77.         *dst++ = c;
  78.     } else
  79.     if (prog->startp[no] != (char *)NULL && prog->endp[no] != (char *)NULL) {
  80.         len = (int) (prog->endp[no] - prog->startp[no]);
  81.         strncpy(dst, prog->startp[no], len);
  82.         dst += len;
  83.         if (len != 0 && *(dst - 1) == '\0') {    /* strncpy hit NUL. */
  84.             regerror("damaged match string");
  85.             return;
  86.         }
  87.     }
  88.   }
  89.   *dst++ = '\0';
  90. }
  91.