#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define LINSZ 1000
#define STRSZ 40
int fd;
char string[STRSZ];
void capitalize(char *sp);
int getentry(void);
int skipuntil(char *s);
char nextchar(void);
void readline(void);
int
main(int argc, char **argv)
{
int index = 0;
int propct = 0;
char *sp;
if (argc != 2) {
(void)fprintf(stderr, "usage: makedefs file\n");
return 1;
}
if ((fd = open(argv[1], O_RDONLY)) == -1) {
perror(argv[1]);
return 1;
}
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
skipuntil("objects[] = {");
while(getentry()) {
if(!*string){
index++;
continue;
}
for(sp = string; *sp; sp++)
if(*sp == ' ' || *sp == '\t' || *sp == '-')
*sp = '_';
if(!strncmp(string, "RIN_", 4)){
capitalize(string+4);
printf("#define %s u.uprops[%d].p_flgs\n",
string+4, propct++);
}
for(sp = string; *sp; sp++) capitalize(sp);
if(!strncmp(string, "WORTHLESS_PIECE_OF_", 19))
printf("/* #define %s %d */\n", string, index);
else
printf("#define %s %d\n", string, index);
index++;
}
printf("\n#define CORPSE DEAD_HUMAN\n");
printf("#define LAST_GEM (JADE+1)\n");
printf("#define LAST_RING %d\n", propct);
printf("#define NROFOBJECTS %d\n", index-1);
return 0;
}
char line[LINSZ], *lp = line, *lp0 = line, *lpe = line;
int eof;
void
readline(void)
{
int n = read(fd, lp0, (line+LINSZ)-lp0);
if(n == -1){
printf("Input error.\n");
exit(1);
}
if(n == 0) eof++;
lpe = lp0+n;
}
char
nextchar(void)
{
if(lp == lpe){
readline();
lp = lp0;
}
return((lp == lpe) ? 0 : *lp++);
}
int
skipuntil(char *s)
{
char *sp0, *sp1;
loop:
while(*s != nextchar())
if(eof) {
printf("Cannot skipuntil %s\n", s);
exit(1);
}
if(strlen(s) > lpe-lp+1){
char *lp1, *lp2;
lp2 = lp;
lp1 = lp = lp0;
while(lp2 != lpe) *lp1++ = *lp2++;
lp2 = lp0;
lp0 = lp1;
readline();
lp0 = lp2;
if(strlen(s) > lpe-lp+1) {
printf("error in skipuntil");
exit(1);
}
}
sp0 = s+1;
sp1 = lp;
while(*sp0 && *sp0 == *sp1) sp0++, sp1++;
if(!*sp0){
lp = sp1;
return(1);
}
goto loop;
}
int
getentry(void)
{
int inbraces = 0, inparens = 0, stringseen = 0, commaseen = 0;
int prefix = 0;
char ch;
#define NSZ 10
char identif[NSZ], *ip;
string[0] = string[4] = 0;
while(1) {
ch = nextchar();
swi:
if(isalpha((unsigned char)ch)){
ip = identif;
do {
if(ip < identif+NSZ-1) *ip++ = ch;
ch = nextchar();
} while(isalpha((unsigned char)ch) || isdigit((unsigned char)ch));
*ip = 0;
while(ch == ' ' || ch == '\t') ch = nextchar();
if(ch == '(' && !inparens && !stringseen)
if(!strcmp(identif, "WAND") ||
!strcmp(identif, "RING") ||
!strcmp(identif, "POTION") ||
!strcmp(identif, "SCROLL"))
(void) strncpy(string, identif, 3),
string[3] = '_',
prefix = 4;
}
switch(ch) {
case '/':
if((ch = nextchar()) == '*')
skipuntil("*/");
goto swi;
case '{':
inbraces++;
continue;
case '(':
inparens++;
continue;
case '}':
inbraces--;
if(inbraces < 0) return(0);
continue;
case ')':
inparens--;
if(inparens < 0) {
printf("too many ) ?");
exit(1);
}
continue;
case '\n':
if((ch = nextchar()) == '#'){
char pch;
do {
pch = ch;
ch = nextchar();
} while(ch != '\n' || pch == '\\');
continue;
}
goto swi;
case ',':
if(!inparens && !inbraces){
if(prefix && !string[prefix])
string[0] = 0;
if(stringseen) return(1);
printf("unexpected ,\n");
exit(1);
}
commaseen++;
continue;
case '\'':
if((ch = nextchar()) == '\\') ch = nextchar();
if(nextchar() != '\''){
printf("strange character denotation?\n");
exit(1);
}
continue;
case '"':
{
char *sp = string + prefix;
char pch;
int store = (inbraces || inparens)
&& !stringseen++ && !commaseen;
do {
pch = ch;
ch = nextchar();
if(store && sp < string+STRSZ)
*sp++ = ch;
} while(ch != '"' || pch == '\\');
if(store) *--sp = 0;
continue;
}
}
}
}
void
capitalize(char *sp)
{
*sp = (char)toupper((unsigned char)*sp);
}