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