FreeCalypso > hg > freecalypso-reveng
comparison blobstat/readclass.c @ 293:23e5b940cb8b
blobstat: mostly complete
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 21 Sep 2019 21:07:45 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
292:6791499809e0 | 293:23e5b940cb8b |
---|---|
1 #include <sys/types.h> | |
2 #include <ctype.h> | |
3 #include <string.h> | |
4 #include <strings.h> | |
5 #include <stdio.h> | |
6 #include <stdlib.h> | |
7 #include "struct.h" | |
8 | |
9 extern struct category *category_list; | |
10 extern struct libentry *libentry_list; | |
11 | |
12 static struct category * | |
13 find_or_create_cat(catname) | |
14 char *catname; | |
15 { | |
16 struct category *p, **pp; | |
17 char *buf; | |
18 | |
19 for (pp = &category_list; p = *pp; pp = &p->next) | |
20 if (!strcmp(p->name, catname)) | |
21 return(p); | |
22 buf = malloc(sizeof(struct category) + strlen(catname) + 1); | |
23 if (!buf) { | |
24 perror("malloc"); | |
25 exit(1); | |
26 } | |
27 p = (struct category *) buf; | |
28 p->name = buf + sizeof(struct category); | |
29 strcpy(p->name, catname); | |
30 p->accum = 0; | |
31 p->next = 0; | |
32 *pp = p; | |
33 return(p); | |
34 } | |
35 | |
36 static | |
37 add_whole_lib(libname, cat) | |
38 char *libname; | |
39 struct category *cat; | |
40 { | |
41 struct libentry *p, **pp; | |
42 char *buf; | |
43 | |
44 for (pp = &libentry_list; p = *pp; pp = &p->next) | |
45 if (!strcmp(p->libname, libname)) | |
46 return(-1); | |
47 buf = malloc(sizeof(struct libentry) + strlen(libname) + 1); | |
48 if (!buf) { | |
49 perror("malloc"); | |
50 exit(1); | |
51 } | |
52 p = (struct libentry *) buf; | |
53 p->libname = buf + sizeof(struct libentry); | |
54 strcpy(p->libname, libname); | |
55 p->member = 0; | |
56 p->cat = cat; | |
57 p->next = 0; | |
58 *pp = p; | |
59 return(0); | |
60 } | |
61 | |
62 static | |
63 add_lib_member(libname, member, cat) | |
64 char *libname, *member; | |
65 struct category *cat; | |
66 { | |
67 struct libentry *p, **pp; | |
68 char *buf; | |
69 | |
70 for (pp = &libentry_list; p = *pp; pp = &p->next) { | |
71 if (strcmp(p->libname, libname)) | |
72 continue; | |
73 if (!p->member || !strcmp(p->member, member)) | |
74 return(-1); | |
75 } | |
76 buf = malloc(sizeof(struct libentry) + strlen(libname) + | |
77 strlen(member) + 2); | |
78 if (!buf) { | |
79 perror("malloc"); | |
80 exit(1); | |
81 } | |
82 p = (struct libentry *) buf; | |
83 buf += sizeof(struct libentry); | |
84 p->libname = buf; | |
85 strcpy(p->libname, libname); | |
86 buf += strlen(libname) + 1; | |
87 p->member = buf; | |
88 strcpy(p->member, member); | |
89 p->cat = cat; | |
90 p->next = 0; | |
91 *pp = p; | |
92 return(0); | |
93 } | |
94 | |
95 static void | |
96 process_line(linebuf, lineno, filename_for_errs) | |
97 char *linebuf, *filename_for_errs; | |
98 int lineno; | |
99 { | |
100 char *cp, *libname, *member, *catname; | |
101 struct category *cat; | |
102 int rc; | |
103 | |
104 for (cp = linebuf; isspace(*cp); cp++) | |
105 ; | |
106 if (*cp == '\0' || *cp == '#') | |
107 return; | |
108 for (libname = cp; *cp && !isspace(*cp); cp++) | |
109 ; | |
110 if (!*cp) { | |
111 inv: fprintf(stderr, "%s line %d: invalid syntax\n", | |
112 filename_for_errs, lineno); | |
113 exit(1); | |
114 } | |
115 *cp++ = '\0'; | |
116 while (isspace(*cp)) | |
117 cp++; | |
118 if (*cp == '\0' || *cp == '#') | |
119 goto inv; | |
120 for (catname = cp; *cp && !isspace(*cp); cp++) | |
121 ; | |
122 if (*cp) | |
123 *cp++ = '\0'; | |
124 while (isspace(*cp)) | |
125 cp++; | |
126 if (*cp != '\0' && *cp != '#') | |
127 goto inv; | |
128 member = index(libname, ':'); | |
129 if (member) | |
130 *member++ = '\0'; | |
131 cat = find_or_create_cat(catname); | |
132 if (member) | |
133 rc = add_lib_member(libname, member, cat); | |
134 else | |
135 rc = add_whole_lib(libname, cat); | |
136 if (rc < 0) { | |
137 fprintf(stderr, | |
138 "%s line %d: conflict with earlier declaration\n", | |
139 filename_for_errs, lineno); | |
140 exit(1); | |
141 } | |
142 } | |
143 | |
144 read_class_file(filename) | |
145 char *filename; | |
146 { | |
147 FILE *inf; | |
148 int lineno; | |
149 char linebuf[512]; | |
150 | |
151 inf = fopen(filename, "r"); | |
152 if (!inf) { | |
153 perror(filename); | |
154 exit(1); | |
155 } | |
156 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) | |
157 process_line(linebuf, lineno, filename); | |
158 fclose(inf); | |
159 return(0); | |
160 } |