makedefs.c revision 1.6 1 1.6 simonb /* $NetBSD: makedefs.c,v 1.6 2000/07/31 11:35:03 simonb Exp $ */
2 1.5 christos
3 1.2 mycroft /*
4 1.2 mycroft * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
5 1.2 mycroft */
6 1.2 mycroft
7 1.2 mycroft #ifndef lint
8 1.6 simonb static char rcsid[] =
9 1.6 simonb "$NetBSD: makedefs.c,v 1.6 2000/07/31 11:35:03 simonb Exp $";
10 1.5 christos #endif /* not lint */
11 1.1 cgd
12 1.1 cgd #include <stdio.h>
13 1.4 cgd #include <string.h>
14 1.5 christos #include <fcntl.h>
15 1.5 christos #include <unistd.h>
16 1.1 cgd
17 1.1 cgd /* construct definitions of object constants */
18 1.1 cgd #define LINSZ 1000
19 1.1 cgd #define STRSZ 40
20 1.1 cgd
21 1.5 christos int fd;
22 1.5 christos char string[STRSZ];
23 1.5 christos
24 1.6 simonb static void readline(void);
25 1.6 simonb static char nextchar(void);
26 1.6 simonb static int skipuntil(char *);
27 1.6 simonb static int getentry(void);
28 1.6 simonb static void capitalize(char *);
29 1.6 simonb static int letter(int);
30 1.6 simonb static int digit(int);
31 1.5 christos
32 1.6 simonb int main(int, char **);
33 1.1 cgd
34 1.5 christos int
35 1.1 cgd main(argc, argv)
36 1.5 christos int argc;
37 1.5 christos char **argv;
38 1.1 cgd {
39 1.5 christos int i = 0;
40 1.5 christos int propct = 0;
41 1.5 christos char *sp;
42 1.1 cgd if (argc != 2) {
43 1.5 christos (void) fprintf(stderr, "usage: makedefs file\n");
44 1.1 cgd exit(1);
45 1.1 cgd }
46 1.1 cgd if ((fd = open(argv[1], 0)) < 0) {
47 1.1 cgd perror(argv[1]);
48 1.1 cgd exit(1);
49 1.1 cgd }
50 1.1 cgd skipuntil("objects[] = {");
51 1.5 christos while (getentry()) {
52 1.5 christos if (!*string) {
53 1.5 christos i++;
54 1.1 cgd continue;
55 1.1 cgd }
56 1.5 christos for (sp = string; *sp; sp++)
57 1.5 christos if (*sp == ' ' || *sp == '\t' || *sp == '-')
58 1.1 cgd *sp = '_';
59 1.5 christos if (!strncmp(string, "RIN_", 4)) {
60 1.5 christos capitalize(string + 4);
61 1.1 cgd printf("#define %s u.uprops[%d].p_flgs\n",
62 1.5 christos string + 4, propct++);
63 1.1 cgd }
64 1.5 christos for (sp = string; *sp; sp++)
65 1.5 christos capitalize(sp);
66 1.1 cgd /* avoid trouble with stupid C preprocessors */
67 1.5 christos if (!strncmp(string, "WORTHLESS_PIECE_OF_", 19))
68 1.5 christos printf("/* #define %s %d */\n", string, i);
69 1.1 cgd else
70 1.5 christos printf("#define %s %d\n", string, i);
71 1.5 christos i++;
72 1.1 cgd }
73 1.1 cgd printf("\n#define CORPSE DEAD_HUMAN\n");
74 1.1 cgd printf("#define LAST_GEM (JADE+1)\n");
75 1.1 cgd printf("#define LAST_RING %d\n", propct);
76 1.5 christos printf("#define NROFOBJECTS %d\n", i - 1);
77 1.1 cgd exit(0);
78 1.1 cgd }
79 1.1 cgd
80 1.5 christos char line[LINSZ], *lp = line, *lp0 = line, *lpe = line;
81 1.5 christos int eof;
82 1.1 cgd
83 1.5 christos static void
84 1.5 christos readline()
85 1.5 christos {
86 1.5 christos int n = read(fd, lp0, (line + LINSZ) - lp0);
87 1.5 christos if (n < 0) {
88 1.1 cgd printf("Input error.\n");
89 1.1 cgd exit(1);
90 1.1 cgd }
91 1.5 christos if (n == 0)
92 1.5 christos eof++;
93 1.5 christos lpe = lp0 + n;
94 1.1 cgd }
95 1.1 cgd
96 1.5 christos static char
97 1.5 christos nextchar()
98 1.5 christos {
99 1.5 christos if (lp == lpe) {
100 1.1 cgd readline();
101 1.1 cgd lp = lp0;
102 1.1 cgd }
103 1.5 christos return ((lp == lpe) ? 0 : *lp++);
104 1.1 cgd }
105 1.1 cgd
106 1.5 christos static int
107 1.5 christos skipuntil(s)
108 1.5 christos char *s;
109 1.5 christos {
110 1.5 christos char *sp0, *sp1;
111 1.1 cgd loop:
112 1.5 christos while (*s != nextchar())
113 1.5 christos if (eof) {
114 1.1 cgd printf("Cannot skipuntil %s\n", s);
115 1.1 cgd exit(1);
116 1.1 cgd }
117 1.5 christos if (strlen(s) > lpe - lp + 1) {
118 1.5 christos char *lp1, *lp2;
119 1.1 cgd lp2 = lp;
120 1.1 cgd lp1 = lp = lp0;
121 1.5 christos while (lp2 != lpe)
122 1.5 christos *lp1++ = *lp2++;
123 1.1 cgd lp2 = lp0; /* save value */
124 1.1 cgd lp0 = lp1;
125 1.1 cgd readline();
126 1.1 cgd lp0 = lp2;
127 1.5 christos if (strlen(s) > lpe - lp + 1) {
128 1.1 cgd printf("error in skipuntil");
129 1.1 cgd exit(1);
130 1.1 cgd }
131 1.1 cgd }
132 1.5 christos sp0 = s + 1;
133 1.1 cgd sp1 = lp;
134 1.5 christos while (*sp0 && *sp0 == *sp1)
135 1.5 christos sp0++, sp1++;
136 1.5 christos if (!*sp0) {
137 1.1 cgd lp = sp1;
138 1.5 christos return (1);
139 1.1 cgd }
140 1.1 cgd goto loop;
141 1.1 cgd }
142 1.1 cgd
143 1.5 christos static int
144 1.5 christos getentry()
145 1.5 christos {
146 1.5 christos int inbraces = 0, inparens = 0, stringseen = 0, commaseen = 0;
147 1.5 christos int prefix = 0;
148 1.5 christos char ch;
149 1.1 cgd #define NSZ 10
150 1.5 christos char identif[NSZ], *ip;
151 1.1 cgd string[0] = string[4] = 0;
152 1.5 christos /*
153 1.5 christos * read until {...} or XXX(...) followed by , skip comment and
154 1.5 christos * #define lines deliver 0 on failure
155 1.1 cgd */
156 1.5 christos while (1) {
157 1.1 cgd ch = nextchar();
158 1.5 christos swi:
159 1.5 christos if (letter(ch)) {
160 1.1 cgd ip = identif;
161 1.1 cgd do {
162 1.5 christos if (ip < identif + NSZ - 1)
163 1.5 christos *ip++ = ch;
164 1.1 cgd ch = nextchar();
165 1.5 christos } while (letter(ch) || digit(ch));
166 1.1 cgd *ip = 0;
167 1.5 christos while (ch == ' ' || ch == '\t')
168 1.5 christos ch = nextchar();
169 1.5 christos if (ch == '(' && !inparens && !stringseen)
170 1.5 christos if (!strcmp(identif, "WAND") ||
171 1.5 christos !strcmp(identif, "RING") ||
172 1.5 christos !strcmp(identif, "POTION") ||
173 1.5 christos !strcmp(identif, "SCROLL"))
174 1.5 christos (void) strncpy(string, identif, 3),
175 1.5 christos string[3] = '_',
176 1.5 christos prefix = 4;
177 1.1 cgd }
178 1.5 christos switch (ch) {
179 1.1 cgd case '/':
180 1.1 cgd /* watch for comment */
181 1.5 christos if ((ch = nextchar()) == '*')
182 1.1 cgd skipuntil("*/");
183 1.1 cgd goto swi;
184 1.1 cgd case '{':
185 1.1 cgd inbraces++;
186 1.1 cgd continue;
187 1.1 cgd case '(':
188 1.1 cgd inparens++;
189 1.1 cgd continue;
190 1.1 cgd case '}':
191 1.1 cgd inbraces--;
192 1.5 christos if (inbraces < 0)
193 1.5 christos return (0);
194 1.1 cgd continue;
195 1.1 cgd case ')':
196 1.1 cgd inparens--;
197 1.5 christos if (inparens < 0) {
198 1.1 cgd printf("too many ) ?");
199 1.1 cgd exit(1);
200 1.1 cgd }
201 1.1 cgd continue;
202 1.1 cgd case '\n':
203 1.1 cgd /* watch for #define at begin of line */
204 1.5 christos if ((ch = nextchar()) == '#') {
205 1.5 christos char pch;
206 1.1 cgd /* skip until '\n' not preceded by '\\' */
207 1.1 cgd do {
208 1.1 cgd pch = ch;
209 1.1 cgd ch = nextchar();
210 1.5 christos } while (ch != '\n' || pch == '\\');
211 1.1 cgd continue;
212 1.1 cgd }
213 1.1 cgd goto swi;
214 1.1 cgd case ',':
215 1.5 christos if (!inparens && !inbraces) {
216 1.5 christos if (prefix && !string[prefix])
217 1.1 cgd string[0] = 0;
218 1.5 christos if (stringseen)
219 1.5 christos return (1);
220 1.1 cgd printf("unexpected ,\n");
221 1.1 cgd exit(1);
222 1.1 cgd }
223 1.1 cgd commaseen++;
224 1.1 cgd continue;
225 1.1 cgd case '\'':
226 1.5 christos if ((ch = nextchar()) == '\\')
227 1.5 christos ch = nextchar();
228 1.5 christos if (nextchar() != '\'') {
229 1.1 cgd printf("strange character denotation?\n");
230 1.1 cgd exit(1);
231 1.1 cgd }
232 1.1 cgd continue;
233 1.1 cgd case '"':
234 1.1 cgd {
235 1.5 christos char *sp = string + prefix;
236 1.5 christos char pch;
237 1.5 christos int store = (inbraces || inparens)
238 1.5 christos && !stringseen++ && !commaseen;
239 1.1 cgd do {
240 1.1 cgd pch = ch;
241 1.1 cgd ch = nextchar();
242 1.5 christos if (store && sp < string + STRSZ)
243 1.1 cgd *sp++ = ch;
244 1.5 christos } while (ch != '"' || pch == '\\');
245 1.5 christos if (store)
246 1.5 christos *--sp = 0;
247 1.1 cgd continue;
248 1.1 cgd }
249 1.1 cgd }
250 1.1 cgd }
251 1.1 cgd }
252 1.1 cgd
253 1.5 christos static void
254 1.5 christos capitalize(sp)
255 1.5 christos char *sp;
256 1.5 christos {
257 1.5 christos if ('a' <= *sp && *sp <= 'z')
258 1.5 christos *sp += 'A' - 'a';
259 1.1 cgd }
260 1.1 cgd
261 1.5 christos static int
262 1.5 christos letter(ch)
263 1.5 christos char ch;
264 1.5 christos {
265 1.5 christos return (('a' <= ch && ch <= 'z') ||
266 1.5 christos ('A' <= ch && ch <= 'Z'));
267 1.1 cgd }
268 1.1 cgd
269 1.5 christos static int
270 1.5 christos digit(ch)
271 1.5 christos char ch;
272 1.5 christos {
273 1.5 christos return ('0' <= ch && ch <= '9');
274 1.1 cgd }
275