home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / mytinfo / part01 / readcaps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-26  |  2.2 KB  |  152 lines

  1. /*
  2.  * readcaps.c
  3.  *
  4.  * By Ross Ridge
  5.  * Public Domain
  6.  * 92/02/01 07:30:15
  7.  *
  8.  * Read in the cap_list file
  9.  *
  10.  */
  11.  
  12. #define NOTLIB
  13. #include "defs.h"
  14.  
  15. #include <ctype.h>
  16.  
  17. #ifdef USE_SCCS_IDS
  18. static const char SCCSid[] = "@(#) mytinfo readcaps.c 3.2 92/02/01 public domain, By Ross Ridge";
  19. #endif
  20.  
  21. #ifdef __GNUC__
  22. __inline__
  23. #endif
  24. static int
  25. skipline(f)
  26. register FILE *f; {
  27.     register int c;
  28.  
  29.     do {
  30.         c = getc(f);
  31.         if (c == EOF)
  32.             return EOF;
  33. #ifdef TEST
  34.         putchar(c);
  35. #endif
  36.     } while (c != '\n');
  37.  
  38.     return 0;
  39. }
  40.  
  41. #ifdef __GNUC__
  42. __inline__
  43. #endif
  44. static int
  45. getfield(f, s, len)
  46. register FILE *f;
  47. register char *s;
  48. int len; {
  49.     register int c;
  50.     int i;
  51. #ifdef TEST
  52.     char *start = s;
  53. #endif
  54.  
  55.     do {
  56.         c = getc(f);
  57.         if (c == EOF)
  58.             return EOF;
  59.     } while (c != '\n' && isspace(c));
  60.     if (c == '\n')
  61.         return 0;
  62.  
  63.     i = 0;
  64.     while(!isspace(c)) {
  65.         if (i++ < len)
  66.             *s++ = c;
  67.         c = getc(f);
  68.         if (c == EOF)
  69.             return EOF;
  70.             
  71.     }
  72.     *s = '\0';
  73. #ifdef TEST
  74.     printf(" %s", start);
  75. #endif
  76.     return c;
  77. }
  78.  
  79. int
  80. readcaps(f, buf, max)
  81. FILE *f;
  82. register struct caplist *buf;
  83. int max; {
  84.     int type;
  85.     register int count;
  86.     int c;
  87.     static char dummy;
  88.  
  89.     count = 0;
  90.     type = getc(f);
  91.     while(type != EOF) {
  92.         if (type == '$' || type == '!' || type == '#') {
  93.             if (count >= max)
  94.                 return count + 1;
  95. #ifdef TEST
  96.             putchar(type);
  97. #endif
  98.             buf[count].type = type;
  99.  
  100.             if (type == '$') {
  101.                 c = getc(f);
  102.                 if (c == EOF)
  103.                     break;
  104.                 if (c == 'G')
  105.                     buf[count].flag = 'G';
  106.                 else if (c == 'K')
  107.                     buf[count].flag = 'K';
  108.                 else
  109.                     buf[count].flag = ' ';
  110.             }
  111.     
  112.             c = getfield(f, buf[count].var, MAX_VARNAME);
  113.             if (c == EOF || c == '\n' || c == 0)
  114.                 return -1;
  115.             c = getfield(f, buf[count].tinfo, MAX_TINFONAME);
  116.             if (c == EOF || c == '\n' || c == 0)
  117.                 return -1;
  118.             c = getfield(f, buf[count].tcap, MAX_TCAPNAME);
  119.             if (c == EOF || c == 0)
  120.                 return -1;
  121.             if (c != '\n')
  122.                 if (getfield(f, &dummy, 1) != 0)
  123.                     return -1;
  124.             count++;
  125. #ifdef TEST
  126.             putchar('\n');
  127. #endif
  128.         } else {
  129. #ifdef TEST
  130.             putchar(type);
  131. #endif
  132.             if (type != '\n' && skipline(f) == EOF)
  133.                 return -1;
  134.         }
  135.         type = getc(f);
  136.     }
  137.     return count;
  138. }
  139.  
  140. #ifdef TEST
  141. struct caplist list[1000];
  142.  
  143. int
  144. main() {
  145.     int ret;
  146.  
  147.     ret = readcaps(stdin, list, 1000);
  148.     printf("ret = %d\n", ret);
  149.     return 0;
  150. }
  151. #endif
  152.