home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume16 / conf2 / part04 / confstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-14  |  2.5 KB  |  171 lines

  1. #include "conf.h"
  2.  
  3. unsigned int wordlen;
  4.  
  5. char *
  6. parsestr(string, length, flags)
  7. char *string;
  8. int length, flags;
  9. {
  10.     static char *retbuf;
  11.     static int buflen = 0;
  12.     static char *prevs;
  13.     static int prevl;
  14.     char *p;
  15.  
  16.     if (buflen == 0)
  17.     retbuf = mymalloc((unsigned)(buflen = PAGESIZ));
  18.  
  19.     p = retbuf;
  20.  
  21.     if (!length)
  22.     {
  23.     string = prevs;
  24.     length = prevl;
  25.     }
  26.  
  27.     while (length && isspace(*string))
  28.     {
  29.         ++string;
  30.     --length;
  31.     }
  32.  
  33.     prevs = string;
  34.     prevl = length;
  35.  
  36.     if (!length)
  37.     {
  38.     wordlen = linelen = 0;
  39.     return NULL;
  40.     }
  41.  
  42.     while (length &&
  43.        ((flags&THEREST) || (!isspace(*string) &&
  44.                 (*string != ',') && (*string != '='))))
  45.     {
  46.     if (*string == '"')
  47.     {
  48.         ++string;  --length;
  49.  
  50.         while (length && (*string != '"'))
  51.         {
  52.         if (*string == '\\')
  53.         {
  54.             ++string; --length;
  55.  
  56.             if (length && isdigit(*string))
  57.             {
  58.             int x, c;
  59.  
  60.             x = (*string++ - '0');  --length;
  61.  
  62.             if (length && isdigit(*string))
  63.             {
  64.                 x = (x * 10) + (*string++ - '0');  --length;
  65.  
  66.                 if (length && isdigit(*string))
  67.                 {
  68.                 c = (x * 10) + (*string - '0');
  69.                 if (c < 256)
  70.                 {
  71.                     x = c;
  72.                     ++string;  --length;
  73.                 }
  74.                 }
  75.             }
  76.  
  77.             *p++ = x;
  78.             }
  79.             else
  80.             if (length)
  81.             {
  82.                 *p++ = *string++;  --length;
  83.             }
  84.         }
  85.         else
  86.         {
  87.             *p++ = *string++; --length;
  88.         }
  89.         }
  90.     }
  91.     else
  92.     {
  93.         if (*string == '\\')
  94.         {
  95.         ++string; --length;
  96.  
  97.         if (length && isdigit(*string))
  98.         {
  99.             int x, c;
  100.  
  101.             x = (*string++ - '0');  --length;
  102.  
  103.             if (length && isdigit(*string))
  104.             {
  105.             x = (x * 10) + (*string++ - '0');  --length;
  106.  
  107.             if (length && isdigit(*string))
  108.             {
  109.                 c = (x * 10) + (*string - '0');
  110.                 if (c < 256)
  111.                 {
  112.                 x = c;
  113.                 ++string;  --length;
  114.                 }
  115.             }
  116.             }
  117.  
  118.             *p++ = x;
  119.         }
  120.         else
  121.             if (length)
  122.             {
  123.             *p++ = *string++;  --length;
  124.             }
  125.         }
  126.         else
  127.         {
  128.         *p++ = *string++; --length;
  129.         }
  130.     }
  131.     }
  132.  
  133.     *p = '\0';
  134.  
  135.     while (length && isspace(*string))
  136.     {
  137.         ++string;  --length;
  138.     }
  139.  
  140.     if (length && ((*string == ',') || (*string == '=')))
  141.     {
  142.     ++string;  --length;
  143.     } 
  144.  
  145.     prevs = string;
  146.     prevl = length;
  147.  
  148.     linelen = length;
  149.     wordlen = p - retbuf;
  150.  
  151.     return wordlen ? retbuf : NULL;
  152. }
  153.  
  154. cpystr(to, from, length)
  155. register char *to, *from;
  156. register unsigned int length;
  157. {
  158.    while(length--) *to++ = *from++;
  159. }
  160.  
  161. char *
  162. puterr(error)
  163. int error;
  164. {
  165.     static char qwerty[42];
  166.  
  167.     (void) sprintf(qwerty, "Unknown error %d", error);
  168.  
  169.     return ((unsigned)error >= sys_nerr) ? qwerty : sys_errlist[error];
  170. }
  171.