141279Sbostic /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
241279Sbostic /* makedefs.c - version 1.0.2 */
341279Sbostic
4*41283Sbostic #include <stdio.h>
5*41283Sbostic
641279Sbostic /* construct definitions of object constants */
741279Sbostic #define LINSZ 1000
841279Sbostic #define STRSZ 40
941279Sbostic
1041279Sbostic int fd;
1141279Sbostic char string[STRSZ];
1241279Sbostic
main(argc,argv)13*41283Sbostic main(argc, argv)
14*41283Sbostic int argc;
15*41283Sbostic char **argv;
16*41283Sbostic {
1741279Sbostic register int index = 0;
1841279Sbostic register int propct = 0;
1941279Sbostic register char *sp;
20*41283Sbostic if (argc != 2) {
21*41283Sbostic (void)fprintf(stderr, "usage: makedefs file\n");
2241279Sbostic exit(1);
2341279Sbostic }
24*41283Sbostic if ((fd = open(argv[1], 0)) < 0) {
25*41283Sbostic perror(argv[1]);
26*41283Sbostic exit(1);
27*41283Sbostic }
2841279Sbostic skipuntil("objects[] = {");
2941279Sbostic while(getentry()) {
3041279Sbostic if(!*string){
3141279Sbostic index++;
3241279Sbostic continue;
3341279Sbostic }
3441279Sbostic for(sp = string; *sp; sp++)
3541279Sbostic if(*sp == ' ' || *sp == '\t' || *sp == '-')
3641279Sbostic *sp = '_';
3741279Sbostic if(!strncmp(string, "RIN_", 4)){
3841279Sbostic capitalize(string+4);
3941279Sbostic printf("#define %s u.uprops[%d].p_flgs\n",
4041279Sbostic string+4, propct++);
4141279Sbostic }
4241279Sbostic for(sp = string; *sp; sp++) capitalize(sp);
4341279Sbostic /* avoid trouble with stupid C preprocessors */
4441279Sbostic if(!strncmp(string, "WORTHLESS_PIECE_OF_", 19))
4541279Sbostic printf("/* #define %s %d */\n", string, index);
4641279Sbostic else
4741279Sbostic printf("#define %s %d\n", string, index);
4841279Sbostic index++;
4941279Sbostic }
5041279Sbostic printf("\n#define CORPSE DEAD_HUMAN\n");
5141279Sbostic printf("#define LAST_GEM (JADE+1)\n");
5241279Sbostic printf("#define LAST_RING %d\n", propct);
5341279Sbostic printf("#define NROFOBJECTS %d\n", index-1);
5441279Sbostic exit(0);
5541279Sbostic }
5641279Sbostic
5741279Sbostic char line[LINSZ], *lp = line, *lp0 = line, *lpe = line;
5841279Sbostic int eof;
5941279Sbostic
readline()6041279Sbostic readline(){
6141279Sbostic register int n = read(fd, lp0, (line+LINSZ)-lp0);
6241279Sbostic if(n < 0){
6341279Sbostic printf("Input error.\n");
6441279Sbostic exit(1);
6541279Sbostic }
6641279Sbostic if(n == 0) eof++;
6741279Sbostic lpe = lp0+n;
6841279Sbostic }
6941279Sbostic
7041279Sbostic char
nextchar()7141279Sbostic nextchar(){
7241279Sbostic if(lp == lpe){
7341279Sbostic readline();
7441279Sbostic lp = lp0;
7541279Sbostic }
7641279Sbostic return((lp == lpe) ? 0 : *lp++);
7741279Sbostic }
7841279Sbostic
skipuntil(s)7941279Sbostic skipuntil(s) char *s; {
8041279Sbostic register char *sp0, *sp1;
8141279Sbostic loop:
8241279Sbostic while(*s != nextchar())
8341279Sbostic if(eof) {
8441279Sbostic printf("Cannot skipuntil %s\n", s);
8541279Sbostic exit(1);
8641279Sbostic }
8741279Sbostic if(strlen(s) > lpe-lp+1){
8841279Sbostic register char *lp1, *lp2;
8941279Sbostic lp2 = lp;
9041279Sbostic lp1 = lp = lp0;
9141279Sbostic while(lp2 != lpe) *lp1++ = *lp2++;
9241279Sbostic lp2 = lp0; /* save value */
9341279Sbostic lp0 = lp1;
9441279Sbostic readline();
9541279Sbostic lp0 = lp2;
9641279Sbostic if(strlen(s) > lpe-lp+1) {
9741279Sbostic printf("error in skipuntil");
9841279Sbostic exit(1);
9941279Sbostic }
10041279Sbostic }
10141279Sbostic sp0 = s+1;
10241279Sbostic sp1 = lp;
10341279Sbostic while(*sp0 && *sp0 == *sp1) sp0++, sp1++;
10441279Sbostic if(!*sp0){
10541279Sbostic lp = sp1;
10641279Sbostic return(1);
10741279Sbostic }
10841279Sbostic goto loop;
10941279Sbostic }
11041279Sbostic
getentry()11141279Sbostic getentry(){
11241279Sbostic int inbraces = 0, inparens = 0, stringseen = 0, commaseen = 0;
11341279Sbostic int prefix = 0;
11441279Sbostic char ch;
11541279Sbostic #define NSZ 10
11641279Sbostic char identif[NSZ], *ip;
11741279Sbostic string[0] = string[4] = 0;
11841279Sbostic /* read until {...} or XXX(...) followed by ,
11941279Sbostic skip comment and #define lines
12041279Sbostic deliver 0 on failure
12141279Sbostic */
12241279Sbostic while(1) {
12341279Sbostic ch = nextchar();
12441279Sbostic swi:
12541279Sbostic if(letter(ch)){
12641279Sbostic ip = identif;
12741279Sbostic do {
12841279Sbostic if(ip < identif+NSZ-1) *ip++ = ch;
12941279Sbostic ch = nextchar();
13041279Sbostic } while(letter(ch) || digit(ch));
13141279Sbostic *ip = 0;
13241279Sbostic while(ch == ' ' || ch == '\t') ch = nextchar();
13341279Sbostic if(ch == '(' && !inparens && !stringseen)
13441279Sbostic if(!strcmp(identif, "WAND") ||
13541279Sbostic !strcmp(identif, "RING") ||
13641279Sbostic !strcmp(identif, "POTION") ||
13741279Sbostic !strcmp(identif, "SCROLL"))
13841279Sbostic (void) strncpy(string, identif, 3),
13941279Sbostic string[3] = '_',
14041279Sbostic prefix = 4;
14141279Sbostic }
14241279Sbostic switch(ch) {
14341279Sbostic case '/':
14441279Sbostic /* watch for comment */
14541279Sbostic if((ch = nextchar()) == '*')
14641279Sbostic skipuntil("*/");
14741279Sbostic goto swi;
14841279Sbostic case '{':
14941279Sbostic inbraces++;
15041279Sbostic continue;
15141279Sbostic case '(':
15241279Sbostic inparens++;
15341279Sbostic continue;
15441279Sbostic case '}':
15541279Sbostic inbraces--;
15641279Sbostic if(inbraces < 0) return(0);
15741279Sbostic continue;
15841279Sbostic case ')':
15941279Sbostic inparens--;
16041279Sbostic if(inparens < 0) {
16141279Sbostic printf("too many ) ?");
16241279Sbostic exit(1);
16341279Sbostic }
16441279Sbostic continue;
16541279Sbostic case '\n':
16641279Sbostic /* watch for #define at begin of line */
16741279Sbostic if((ch = nextchar()) == '#'){
16841279Sbostic register char pch;
16941279Sbostic /* skip until '\n' not preceded by '\\' */
17041279Sbostic do {
17141279Sbostic pch = ch;
17241279Sbostic ch = nextchar();
17341279Sbostic } while(ch != '\n' || pch == '\\');
17441279Sbostic continue;
17541279Sbostic }
17641279Sbostic goto swi;
17741279Sbostic case ',':
17841279Sbostic if(!inparens && !inbraces){
17941279Sbostic if(prefix && !string[prefix])
18041279Sbostic string[0] = 0;
18141279Sbostic if(stringseen) return(1);
18241279Sbostic printf("unexpected ,\n");
18341279Sbostic exit(1);
18441279Sbostic }
18541279Sbostic commaseen++;
18641279Sbostic continue;
18741279Sbostic case '\'':
18841279Sbostic if((ch = nextchar()) == '\\') ch = nextchar();
18941279Sbostic if(nextchar() != '\''){
19041279Sbostic printf("strange character denotation?\n");
19141279Sbostic exit(1);
19241279Sbostic }
19341279Sbostic continue;
19441279Sbostic case '"':
19541279Sbostic {
19641279Sbostic register char *sp = string + prefix;
19741279Sbostic register char pch;
19841279Sbostic register int store = (inbraces || inparens)
19941279Sbostic && !stringseen++ && !commaseen;
20041279Sbostic do {
20141279Sbostic pch = ch;
20241279Sbostic ch = nextchar();
20341279Sbostic if(store && sp < string+STRSZ)
20441279Sbostic *sp++ = ch;
20541279Sbostic } while(ch != '"' || pch == '\\');
20641279Sbostic if(store) *--sp = 0;
20741279Sbostic continue;
20841279Sbostic }
20941279Sbostic }
21041279Sbostic }
21141279Sbostic }
21241279Sbostic
capitalize(sp)21341279Sbostic capitalize(sp) register char *sp; {
21441279Sbostic if('a' <= *sp && *sp <= 'z') *sp += 'A'-'a';
21541279Sbostic }
21641279Sbostic
letter(ch)21741279Sbostic letter(ch) register char ch; {
21841279Sbostic return( ('a' <= ch && ch <= 'z') ||
21941279Sbostic ('A' <= ch && ch <= 'Z') );
22041279Sbostic }
22141279Sbostic
digit(ch)22241279Sbostic digit(ch) register char ch; {
22341279Sbostic return( '0' <= ch && ch <= '9' );
22441279Sbostic }
225