home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / conf / part04 / confstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-13  |  3.3 KB  |  206 lines

  1. /*
  2.  *    conf - An interactive multi-user chat program.
  3.  *
  4.  *    conf Copyright (c) 1986, 1987 by Keith Gabryelski
  5.  *
  6.  *    Conf is quasi-public domain software; it may be used and copied
  7.  *    freely and may be modified to suit the indivuals needs as long
  8.  *    as:
  9.  *
  10.  *    [1] It is not sold.
  11.  *    [2] It is not made part of any licensed product.
  12.  *    [3] This header and others like it are not modified or removed.
  13.  *    [4] You allow others to use and copy without charge.
  14.  *    
  15.  *    without expressed written permission from the original
  16.  *    author (Keith Gabryelski).
  17.  *
  18.  */
  19.  
  20. #include "conf.h"
  21.  
  22. unsigned int wordlen;
  23.  
  24. char *
  25. parsestr(string, length, flags)
  26. char *string;
  27. int length, flags;
  28. {
  29.     static char *retbuf;
  30.     static int buflen = 0;
  31.     static char *prevs;
  32.     static int prevl;
  33.     char *p;
  34.  
  35.     if (buflen == 0)
  36.     retbuf = mymalloc((unsigned)(buflen = PAGESIZ));
  37.  
  38.     p = retbuf;
  39.  
  40.     if (!length)
  41.     {
  42.     string = prevs;
  43.     length = prevl;
  44.     }
  45.  
  46.     while (length && isspace(*string))
  47.     {
  48.         ++string;
  49.     --length;
  50.     }
  51.  
  52.     prevs = string;
  53.     prevl = length;
  54.  
  55.     if (!length)
  56.     {
  57.     wordlen = linelen = 0;
  58.     return NULL;
  59.     }
  60.  
  61.     while (length && ((flags&THEREST) || (!isspace(*string)&&(*string != ',')&&(*string != '='))))
  62.     {
  63.     if (*string == '"')
  64.     {
  65.         ++string;  --length;
  66.  
  67.         while (length && (*string != '"'))
  68.         {
  69.         if (*string == '\\')
  70.         {
  71.             ++string; --length;
  72.  
  73.             if (length && isdigit(*string))
  74.             {
  75.             int x, c;
  76.  
  77.             x = (*string++ - '0');  --length;
  78.  
  79.             if (length && isdigit(*string))
  80.             {
  81.                 x = (x * 10) + (*string++ - '0');  --length;
  82.  
  83.                 if (length && isdigit(*string))
  84.                 {
  85.                 c = (x * 10) + (*string - '0');
  86.                 if (c < 256)
  87.                 {
  88.                     x = c;
  89.                     ++string;  --length;
  90.                 }
  91.                 }
  92.             }
  93.  
  94.             *p++ = x;
  95.             }
  96.             else
  97.             if (length)
  98.             {
  99.                 *p++ = *string++;  --length;
  100.             }
  101.         }
  102.         else
  103.         {
  104.             *p++ = *string++; --length;
  105.         }
  106.         }
  107.     }
  108.     else
  109.     {
  110.         if (*string == '\\')
  111.         {
  112.         ++string; --length;
  113.  
  114.         if (length && isdigit(*string))
  115.         {
  116.             int x, c;
  117.  
  118.             x = (*string++ - '0');  --length;
  119.  
  120.             if (length && isdigit(*string))
  121.             {
  122.             x = (x * 10) + (*string++ - '0');  --length;
  123.  
  124.             if (length && isdigit(*string))
  125.             {
  126.                 c = (x * 10) + (*string - '0');
  127.                 if (c < 256)
  128.                 {
  129.                 x = c;
  130.                 ++string;  --length;
  131.                 }
  132.             }
  133.             }
  134.  
  135.             *p++ = x;
  136.         }
  137.         else
  138.             if (length)
  139.             {
  140.             *p++ = *string++;  --length;
  141.             }
  142.         }
  143.         else
  144.         {
  145.         *p++ = *string++; --length;
  146.         }
  147.     }
  148.     }
  149.  
  150.     *p = '\0';
  151.  
  152.     while (length && isspace(*string))
  153.     {
  154.         ++string;  --length;
  155.     }
  156.  
  157.     if (length && ((*string == ',') || (*string == '=')))
  158.     {
  159.     ++string;  --length;
  160.     } 
  161.  
  162.     prevs = string;
  163.     prevl = length;
  164.  
  165.     linelen = length;
  166.     wordlen = p - retbuf;
  167.  
  168.     return wordlen ? retbuf : NULL;
  169. }
  170.  
  171. cpystr(to, from, length)
  172. register char *to, *from;
  173. register unsigned int length;
  174. {
  175.    while(length--) *to++ = *from++;
  176. }
  177.  
  178. zbuf(ptr, length)
  179. register char *ptr;
  180. register unsigned int length;
  181. {
  182.     while(length--) *ptr = '\0';
  183. }
  184.  
  185. cmpdat(dat1, dat2, length)
  186. register char *dat1, *dat2;
  187. register int length;
  188. {
  189.     while (length--)
  190.     if (*dat1++ != *dat2++)
  191.         return FALSE;
  192.  
  193.     return TRUE;
  194. }
  195.  
  196. char *
  197. puterr(error)
  198. int error;
  199. {
  200.     static char qwerty[42];
  201.  
  202.     (void)sprintf(qwerty, "Unknown error %d", error);
  203.  
  204.     return ((unsigned)error >= sys_nerr) ? qwerty : sys_errlist[error];
  205. }
  206.