summaryrefslogtreecommitdiffstats
path: root/lib/libmytinfo/readcaps.c
blob: b78bdc1917065d4b96f95b2beec11f03cf95c360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * readcaps.c
 *
 * By Ross Ridge
 * Public Domain
 * 92/02/01 07:30:15
 *
 * Read in the cap_list file
 *
 */

#define NOTLIB
#include "defs.h"

#include <ctype.h>

#ifdef USE_SCCS_IDS
static const char SCCSid[] = "@(#) mytinfo readcaps.c 3.2 92/02/01 public domain, By Ross Ridge";
#endif

#ifdef __GNUC__
__inline__
#endif
static int
skipline(f)
register FILE *f; {
	register int c;

	do {
		c = getc(f);
		if (c == EOF)
			return EOF;
#ifdef TEST
		putchar(c);
#endif
	} while (c != '\n');

	return 0;
}

#ifdef __GNUC__
__inline__
#endif
static int
getfield(f, s, len)
register FILE *f;
register char *s;
int len; {
	register int c;
	int i;
#ifdef TEST
	char *start = s;
#endif

	do {
		c = getc(f);
		if (c == EOF)
			return EOF;
	} while (c != '\n' && isspace(c));
	if (c == '\n')
		return 0;

	i = 0;
	while(!isspace(c)) {
		if (i++ < len)
			*s++ = c;
		c = getc(f);
		if (c == EOF)
			return EOF;

	}
	*s = '\0';
#ifdef TEST
	printf(" %s", start);
#endif
	return c;
}

int
readcaps(f, buf, max)
FILE *f;
register struct caplist *buf;
int max; {
	int type;
	register int count;
	int c;
	static char dummy;

	count = 0;
	type = getc(f);
	while(type != EOF) {
		if (type == '$' || type == '!' || type == '#') {
			if (count >= max)
				return count + 1;
#ifdef TEST
			putchar(type);
#endif
			buf[count].type = type;

			if (type == '$') {
				c = getc(f);
				if (c == EOF)
					break;
				if (c == 'G')
					buf[count].flag = 'G';
				else if (c == 'K')
					buf[count].flag = 'K';
				else
					buf[count].flag = ' ';
			}

			c = getfield(f, buf[count].var, MAX_VARNAME);
			if (c == EOF || c == '\n' || c == 0)
				return -1;
			c = getfield(f, buf[count].tinfo, MAX_TINFONAME);
			if (c == EOF || c == '\n' || c == 0)
				return -1;
			c = getfield(f, buf[count].tcap, MAX_TCAPNAME);
			if (c == EOF || c == 0)
				return -1;
			if (c != '\n')
				if (getfield(f, &dummy, 1) != 0)
					return -1;
			count++;
#ifdef TEST
			putchar('\n');
#endif
		} else {
#ifdef TEST
			putchar(type);
#endif
			if (type != '\n' && skipline(f) == EOF)
				return -1;
		}
		type = getc(f);
	}
	return count;
}

#ifdef TEST
struct caplist list[1000];

int
main() {
	int ret;

	ret = readcaps(stdin, list, 1000);
	printf("ret = %d\n", ret);
	return 0;
}
#endif
OpenPOWER on IntegriCloud