1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* makedefs.c - version 1.0.2 */
3 /* $FreeBSD: src/games/hack/makedefs.c,v 1.4 1999/11/16 02:57:15 billf Exp $ */
4 /* $DragonFly: src/games/hack/makedefs.c,v 1.3 2006/08/21 19:45:32 pavalos Exp $ */
13 /* construct definitions of object constants */
20 static void readline(void);
21 static char nextchar(void);
22 static bool skipuntil(const char *);
23 static bool getentry(void);
24 static void capitalize(char *);
25 static bool letter(char);
26 static bool digit(char);
29 main(int argc, char **argv)
36 fprintf(stderr, "usage: makedefs file\n");
39 if ((fd = open(argv[1], O_RDONLY)) < 0) {
43 skipuntil("objects[] = {");
49 for (sp = string; *sp; sp++)
50 if (*sp == ' ' || *sp == '\t' || *sp == '-')
52 if (!strncmp(string, "RIN_", 4)) {
53 capitalize(string + 4);
54 printf("#define %s u.uprops[%d].p_flgs\n",
55 string + 4, propct++);
57 for (sp = string; *sp; sp++)
59 /* avoid trouble with stupid C preprocessors */
60 if (!strncmp(string, "WORTHLESS_PIECE_OF_", 19))
61 printf("/* #define %s %d */\n", string, idx);
63 printf("#define %s %d\n", string, idx);
66 printf("\n#define CORPSE DEAD_HUMAN\n");
67 printf("#define LAST_GEM (JADE+1)\n");
68 printf("#define LAST_RING %d\n", propct);
69 printf("#define NROFOBJECTS %d\n", idx - 1);
73 char line[LINSZ], *lp = line, *lp0 = line, *lpe = line;
79 int n = read(fd, lp0, (line + LINSZ) - lp0);
82 printf("Input error.\n");
97 return ((lp == lpe) ? 0 : *lp++);
101 skipuntil(const char *s)
106 while (*s != nextchar())
108 printf("Cannot skipuntil %s\n", s);
111 if ((int)strlen(s) > lpe - lp + 1) {
117 lp2 = lp0; /* save value */
121 if ((int)strlen(s) > lpe - lp + 1) {
122 printf("error in skipuntil");
128 while (*sp0 && *sp0 == *sp1)
140 int inbraces = 0, inparens = 0, stringseen = 0, commaseen = 0;
144 char identif[NSZ], *ip;
146 string[0] = string[4] = 0;
147 /* read until {...} or XXX(...) followed by ,
148 * skip comment and #define lines
149 * deliver 0 on failure
157 if (ip < identif + NSZ - 1)
160 } while (letter(ch) || digit(ch));
162 while (ch == ' ' || ch == '\t')
164 if (ch == '(' && !inparens && !stringseen)
165 if (!strcmp(identif, "WAND") ||
166 !strcmp(identif, "RING") ||
167 !strcmp(identif, "POTION") ||
168 !strcmp(identif, "SCROLL"))
169 strncpy(string, identif, 3),
175 /* watch for comment */
176 if ((ch = nextchar()) == '*')
193 printf("too many ) ?");
198 /* watch for #define at begin of line */
199 if ((ch = nextchar()) == '#') {
201 /* skip until '\n' not preceded by '\\' */
205 } while (ch != '\n' || pch == '\\');
210 if (!inparens && !inbraces) {
211 if (prefix && !string[prefix])
215 printf("unexpected ,\n");
221 if ((ch = nextchar()) == '\\')
223 if (nextchar() != '\'') {
224 printf("strange character denotation?\n");
230 char *sp = string + prefix;
232 int store = (inbraces || inparens)
233 && !stringseen++ && !commaseen;
237 if (store && sp < string + STRSZ)
239 } while (ch != '"' || pch == '\\');
251 if ('a' <= *sp && *sp <= 'z')
258 return (('a' <= ch && ch <= 'z') ||
259 ('A' <= ch && ch <= 'Z'));
265 return ('0' <= ch && ch <= '9');