home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / HACKSRC.ZIP / MAKEDEFS.C < prev    next >
C/C++ Source or Header  |  1985-10-16  |  5KB  |  218 lines

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