home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / macps / macaux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-12  |  3.5 KB  |  178 lines

  1. /*
  2.  * Copyright (c) 1988, The Regents of the University of California.
  3.  * Edward Moy, Workstation Software Support Group, Workstation Support Serices,
  4.  * Information Systems and Technology.
  5.  *
  6.  * Permission is granted to any individual or institution to use, copy,
  7.  * or redistribute this software so long as it is not sold for profit,
  8.  * provided that this notice and the original copyright notices are
  9.  * retained.  The University of California makes no representations about the
  10.  * suitability of this software for any purpose.  It is provided "as is"
  11.  * without express or implied warranty.
  12.  */
  13.  
  14. #ifndef lint
  15. static char *SCCSid = "@(#)macaux.c    2.2 10/24/89";
  16. #endif lint
  17.  
  18. #include <ctype.h>
  19. #include <stdio.h>
  20. #include "str.h"
  21.  
  22. #define    FALSE        0
  23. #define    TRUE        1
  24.  
  25. extern char *myname;
  26. int rawmode = FALSE;
  27.  
  28. STR *
  29. STRalloc()
  30. {
  31.     register STR *str;
  32.     char *malloc();
  33.  
  34.     if((str = (STR *)malloc(sizeof(STR))) == NULL ||
  35.      (str->bufptr = (unsigned char *)malloc(STRSIZE)) == NULL) {
  36.         fprintf(stderr, "%s: STRalloc: Out of memory\n", myname);
  37.         exit(1);
  38.     }
  39.     str->curendptr = str->bufptr;
  40.     str->realendptr = str->bufptr + STRSIZE;
  41.     return(str);
  42. }
  43.  
  44. STRfree(str)
  45. STR *str;
  46. {
  47.     free((char *)str->bufptr);
  48.     free((char *)str);
  49. }
  50.  
  51. STRexpand(str)
  52. register STR *str;
  53. {
  54.     register int curend, realend;
  55.     char *realloc();
  56.  
  57.     curend = str->curendptr - str->bufptr;
  58.     realend = (str->realendptr - str->bufptr) + STRSIZEDELTA;
  59.     if((str->bufptr = (unsigned char *)realloc((char *)str->bufptr,
  60.      realend)) == NULL) {
  61.         fprintf(stderr, "%s: STRexpand: Out of memory\n", myname);
  62.         exit(1);
  63.     }
  64.     str->curendptr = str->bufptr + curend;
  65.     str->realendptr = str->bufptr + realend;
  66. }
  67.  
  68. STRgets(str, fp)
  69. register STR *str;
  70. register FILE *fp;
  71. {
  72.     register int c;
  73.  
  74.     str->curendptr = str->bufptr;
  75.     for( ; ; ) {
  76.         if((c = getc(fp)) == EOF)
  77.             return(str->curendptr > str->bufptr);
  78.         if(str->curendptr >= str->realendptr)
  79.             STRexpand(str);
  80.         *str->curendptr++ = c;
  81.         if(c == '\n' || c == '\r')
  82.             return(TRUE);
  83.     }
  84. }
  85.  
  86. STRputsptr(str, cp, fp)
  87. register STR *str;
  88. register unsigned char *cp;
  89. register FILE *fp;
  90. {
  91.     if(rawmode) {
  92.         for( ; cp < str->curendptr ; cp++)
  93.             putc(*cp, fp);
  94.         return;
  95.     }
  96.     for( ; cp < str->curendptr ; cp++) {
  97.         if(!isascii(*cp))
  98.             fprintf(fp, "\\%03o", *cp);
  99.         else if(isprint(*cp))
  100.             putc(*cp, fp);
  101.         else {
  102.             switch(*cp) {
  103.              case '\n':
  104.              case '\r':
  105.                 putc('\n', fp);
  106.                 continue;
  107.              case '\t':
  108.                 putc('\t', fp);
  109.                 continue;
  110.              default:
  111.                 fprintf(fp, "\\%03o", *str);
  112.                 continue;
  113.             }
  114.         }
  115.     }
  116. }
  117.  
  118. STRcompareptr(str, cp, sp)
  119. register STR *str;
  120. register unsigned char *cp, *sp;
  121. {
  122.     register int comp;
  123.  
  124.     for( ; ; ) {
  125.         if(*sp == 0)
  126.             return(cp >= str->curendptr ? 0 : 1);
  127.         if(cp >= str->curendptr)
  128.             return(-1);
  129.         if(*sp == '\n') {
  130.             if(*cp != '\n' && *cp != '\r')
  131.                 return((int)*cp - (int)*sp);
  132.         } else if((comp = (int)*cp - (int)*sp) != 0)
  133.             return(comp);
  134.         cp++;
  135.         sp++;
  136.     }
  137. }
  138.  
  139. STRheadcmpptr(str, cp, sp)
  140. register STR *str;
  141. register unsigned char *cp, *sp;
  142. {
  143.     register int comp;
  144.  
  145.     for( ; ; ) {
  146.         if(*sp == 0)
  147.             return(0);
  148.         if(cp >= str->curendptr)
  149.             return(-1);
  150.         if(*sp == '\n') {
  151.             if(*cp != '\n' && *cp != '\r')
  152.                 return((int)*cp - (int)*sp);
  153.         } else if((comp = (int)*cp - (int)*sp) != 0)
  154.             return(comp);
  155.         cp++;
  156.         sp++;
  157.     }
  158. }
  159.  
  160. unsigned char *
  161. STRmatch(str, sp)
  162. register STR *str;
  163. register unsigned char *sp;
  164. {
  165.     register unsigned char *mp, *last;
  166.     register int firstchar;
  167.  
  168.     firstchar = *sp;
  169.     last = str->curendptr - strlen(sp);
  170.     mp = str->bufptr;
  171.     while(mp <= last) {
  172.         if(*mp == firstchar && STRheadcmpptr(str, mp, sp) == 0)
  173.             return(mp);
  174.         mp++;
  175.     }
  176.     return(NULL);
  177. }
  178.