NetBSD/games/hack/makedefs.c

282 lines
5.3 KiB
C
Raw Normal View History

/* $NetBSD: makedefs.c,v 1.7 2001/03/25 20:44:04 jsm Exp $ */
1997-10-19 20:56:41 +04:00
/*
* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
*/
#ifndef lint
static const char rcsid[] =
"$NetBSD: makedefs.c,v 1.7 2001/03/25 20:44:04 jsm Exp $";
1997-10-19 20:56:41 +04:00
#endif /* not lint */
1993-03-21 12:45:37 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
1997-10-19 20:56:41 +04:00
#include <fcntl.h>
#include <unistd.h>
1993-03-21 12:45:37 +03:00
/* construct definitions of object constants */
#define LINSZ 1000
#define STRSZ 40
1997-10-19 20:56:41 +04:00
int fd;
char string[STRSZ];
static void readline(void);
static char nextchar(void);
static int skipuntil(const char *);
static int getentry(void);
static void capitalize(char *);
static int letter(int);
static int digit(int);
1997-10-19 20:56:41 +04:00
int main(int, char **);
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
main(argc, argv)
1997-10-19 20:56:41 +04:00
int argc;
char **argv;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
int i = 0;
int propct = 0;
char *sp;
1993-03-21 12:45:37 +03:00
if (argc != 2) {
1997-10-19 20:56:41 +04:00
(void) fprintf(stderr, "usage: makedefs file\n");
1993-03-21 12:45:37 +03:00
exit(1);
}
if ((fd = open(argv[1], O_RDONLY)) < 0) {
1993-03-21 12:45:37 +03:00
perror(argv[1]);
exit(1);
}
skipuntil("objects[] = {");
1997-10-19 20:56:41 +04:00
while (getentry()) {
if (!*string) {
i++;
1993-03-21 12:45:37 +03:00
continue;
}
1997-10-19 20:56:41 +04:00
for (sp = string; *sp; sp++)
if (*sp == ' ' || *sp == '\t' || *sp == '-')
1993-03-21 12:45:37 +03:00
*sp = '_';
1997-10-19 20:56:41 +04:00
if (!strncmp(string, "RIN_", 4)) {
capitalize(string + 4);
1993-03-21 12:45:37 +03:00
printf("#define %s u.uprops[%d].p_flgs\n",
1997-10-19 20:56:41 +04:00
string + 4, propct++);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
for (sp = string; *sp; sp++)
capitalize(sp);
1993-03-21 12:45:37 +03:00
/* avoid trouble with stupid C preprocessors */
1997-10-19 20:56:41 +04:00
if (!strncmp(string, "WORTHLESS_PIECE_OF_", 19))
printf("/* #define %s %d */\n", string, i);
1993-03-21 12:45:37 +03:00
else
1997-10-19 20:56:41 +04:00
printf("#define %s %d\n", string, i);
i++;
1993-03-21 12:45:37 +03:00
}
printf("\n#define CORPSE DEAD_HUMAN\n");
printf("#define LAST_GEM (JADE+1)\n");
printf("#define LAST_RING %d\n", propct);
1997-10-19 20:56:41 +04:00
printf("#define NROFOBJECTS %d\n", i - 1);
fflush(stdout);
if (ferror(stdout)) {
perror("standard output");
exit(1);
}
1993-03-21 12:45:37 +03:00
exit(0);
}
1997-10-19 20:56:41 +04:00
char line[LINSZ], *lp = line, *lp0 = line, *lpe = line;
int eof;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
static void
readline()
{
int n = read(fd, lp0, (line + LINSZ) - lp0);
if (n < 0) {
1993-03-21 12:45:37 +03:00
printf("Input error.\n");
exit(1);
}
1997-10-19 20:56:41 +04:00
if (n == 0)
eof++;
lpe = lp0 + n;
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
static char
nextchar()
{
if (lp == lpe) {
1993-03-21 12:45:37 +03:00
readline();
lp = lp0;
}
1997-10-19 20:56:41 +04:00
return ((lp == lpe) ? 0 : *lp++);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
static int
skipuntil(s)
const char *s;
1997-10-19 20:56:41 +04:00
{
const char *sp0;
char *sp1;
1993-03-21 12:45:37 +03:00
loop:
1997-10-19 20:56:41 +04:00
while (*s != nextchar())
if (eof) {
1993-03-21 12:45:37 +03:00
printf("Cannot skipuntil %s\n", s);
exit(1);
}
1997-10-19 20:56:41 +04:00
if (strlen(s) > lpe - lp + 1) {
char *lp1, *lp2;
1993-03-21 12:45:37 +03:00
lp2 = lp;
lp1 = lp = lp0;
1997-10-19 20:56:41 +04:00
while (lp2 != lpe)
*lp1++ = *lp2++;
1993-03-21 12:45:37 +03:00
lp2 = lp0; /* save value */
lp0 = lp1;
readline();
lp0 = lp2;
1997-10-19 20:56:41 +04:00
if (strlen(s) > lpe - lp + 1) {
1993-03-21 12:45:37 +03:00
printf("error in skipuntil");
exit(1);
}
}
1997-10-19 20:56:41 +04:00
sp0 = s + 1;
1993-03-21 12:45:37 +03:00
sp1 = lp;
1997-10-19 20:56:41 +04:00
while (*sp0 && *sp0 == *sp1)
sp0++, sp1++;
if (!*sp0) {
1993-03-21 12:45:37 +03:00
lp = sp1;
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
goto loop;
}
1997-10-19 20:56:41 +04:00
static int
getentry()
{
int inbraces = 0, inparens = 0, stringseen = 0, commaseen = 0;
int prefix = 0;
char ch;
1993-03-21 12:45:37 +03:00
#define NSZ 10
1997-10-19 20:56:41 +04:00
char identif[NSZ], *ip;
1993-03-21 12:45:37 +03:00
string[0] = string[4] = 0;
1997-10-19 20:56:41 +04:00
/*
* read until {...} or XXX(...) followed by , skip comment and
* #define lines deliver 0 on failure
1993-03-21 12:45:37 +03:00
*/
1997-10-19 20:56:41 +04:00
while (1) {
1993-03-21 12:45:37 +03:00
ch = nextchar();
1997-10-19 20:56:41 +04:00
swi:
if (letter(ch)) {
1993-03-21 12:45:37 +03:00
ip = identif;
do {
1997-10-19 20:56:41 +04:00
if (ip < identif + NSZ - 1)
*ip++ = ch;
1993-03-21 12:45:37 +03:00
ch = nextchar();
1997-10-19 20:56:41 +04:00
} while (letter(ch) || digit(ch));
1993-03-21 12:45:37 +03:00
*ip = 0;
1997-10-19 20:56:41 +04:00
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;
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
switch (ch) {
1993-03-21 12:45:37 +03:00
case '/':
/* watch for comment */
1997-10-19 20:56:41 +04:00
if ((ch = nextchar()) == '*')
1993-03-21 12:45:37 +03:00
skipuntil("*/");
goto swi;
case '{':
inbraces++;
continue;
case '(':
inparens++;
continue;
case '}':
inbraces--;
1997-10-19 20:56:41 +04:00
if (inbraces < 0)
return (0);
1993-03-21 12:45:37 +03:00
continue;
case ')':
inparens--;
1997-10-19 20:56:41 +04:00
if (inparens < 0) {
1993-03-21 12:45:37 +03:00
printf("too many ) ?");
exit(1);
}
continue;
case '\n':
/* watch for #define at begin of line */
1997-10-19 20:56:41 +04:00
if ((ch = nextchar()) == '#') {
char pch;
1993-03-21 12:45:37 +03:00
/* skip until '\n' not preceded by '\\' */
do {
pch = ch;
ch = nextchar();
1997-10-19 20:56:41 +04:00
} while (ch != '\n' || pch == '\\');
1993-03-21 12:45:37 +03:00
continue;
}
goto swi;
case ',':
1997-10-19 20:56:41 +04:00
if (!inparens && !inbraces) {
if (prefix && !string[prefix])
1993-03-21 12:45:37 +03:00
string[0] = 0;
1997-10-19 20:56:41 +04:00
if (stringseen)
return (1);
1993-03-21 12:45:37 +03:00
printf("unexpected ,\n");
exit(1);
}
commaseen++;
continue;
case '\'':
1997-10-19 20:56:41 +04:00
if ((ch = nextchar()) == '\\')
ch = nextchar();
if (nextchar() != '\'') {
1993-03-21 12:45:37 +03:00
printf("strange character denotation?\n");
exit(1);
}
continue;
case '"':
{
1997-10-19 20:56:41 +04:00
char *sp = string + prefix;
char pch;
int store = (inbraces || inparens)
&& !stringseen++ && !commaseen;
1993-03-21 12:45:37 +03:00
do {
pch = ch;
ch = nextchar();
1997-10-19 20:56:41 +04:00
if (store && sp < string + STRSZ)
1993-03-21 12:45:37 +03:00
*sp++ = ch;
1997-10-19 20:56:41 +04:00
} while (ch != '"' || pch == '\\');
if (store)
*--sp = 0;
1993-03-21 12:45:37 +03:00
continue;
}
}
}
}
1997-10-19 20:56:41 +04:00
static void
capitalize(sp)
char *sp;
{
if ('a' <= *sp && *sp <= 'z')
*sp += 'A' - 'a';
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
static int
letter(ch)
char ch;
{
return (('a' <= ch && ch <= 'z') ||
('A' <= ch && ch <= 'Z'));
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
static int
digit(ch)
char ch;
{
return ('0' <= ch && ch <= '9');
1993-03-21 12:45:37 +03:00
}