home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / jove-4.16-src.tgz / tar.out / bsd / jove / setmaps.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  6KB  |  272 lines

  1. /************************************************************************
  2.  * This program is Copyright (C) 1986-1996 by Jonathan Payne.  JOVE is  *
  3.  * provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is *
  5.  * included in all the files.                                           *
  6.  ************************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include "jove.h"
  10. #include "chars.h"
  11. #include "commands.h"
  12. #include "vars.h"
  13.  
  14. #define LINESIZE    100    /* hope this is big enough */
  15. #define STACKLIMIT    10    /* max conditional depth */
  16.  
  17. #define    PROC(p)    NULL    /* discard function pointers */
  18. #include "commands.tab"
  19.  
  20. #define VAR(v)    NULL, (size_t)0    /* discard variable pointers */
  21. #include "vars.tab"
  22.  
  23. private int
  24. matchcmd(choices, what)
  25. register const struct cmd    choices[];
  26. register char    *what;
  27. {
  28.     register int    i;
  29.  
  30.     for (i = 0; choices[i].Name != NULL; i++) {
  31.         if (what[0] == choices[i].Name[0]
  32.         && strcmp(what, choices[i].Name) == 0)
  33.             return i;
  34.     }
  35.     return -1;
  36. }
  37.  
  38. #ifdef MAC
  39. matchvar(choices, what)
  40. register const struct variable choices[];
  41. register char    *what;
  42. {
  43.     register int    len;
  44.     int    i;
  45.  
  46.     len = strlen(what);
  47.     for (i = 0; choices[i].Name != NULL; i++) {
  48.         if (what[0] == choices[i].Name[0]
  49.         && strcmp(what, choices[i].Name) == 0)
  50.             return i;
  51.     }
  52.     return -1;
  53. }
  54. #endif
  55.  
  56. private int
  57. StartsWith(s, pre)
  58. const char *s, *pre;
  59. {
  60.     return strncmp(s, pre, strlen(pre)) == 0;
  61. }
  62.  
  63. private char *
  64. PPchar(c)
  65. int    c;
  66. {
  67.     static char    str[16];
  68.     char    *cp = str;
  69.  
  70.     if (c & 0200) {
  71.         c &= ~0200;
  72.         strcpy(cp, "M-");
  73.         cp += 2;
  74.     }
  75.     if (c == ESC)
  76.         strcpy(cp, "ESC");
  77.     else if (c < ' ')
  78.         (void) sprintf(cp, "^%c", c + '@');
  79.     else if (c == DEL)
  80.         strcpy(cp, "^?");
  81.     else
  82.         (void) sprintf(cp, "%c", c);
  83.     return str;
  84. }
  85.  
  86. private void
  87. extract(into, from)
  88. char    *into,
  89.     *from;
  90. {
  91.     from += 2;    /* Past tab and first double quote. */
  92.     while ((*into = *from++) != '"')
  93.         into += 1;
  94.     *into = '\0';
  95. }
  96.  
  97.  
  98. int
  99. main()
  100. {
  101.     FILE
  102.         *ifile,
  103.         *of;
  104.     char
  105.         line[LINESIZE],
  106.         comname[LINESIZE];
  107.     int
  108.         comnum,
  109.         lino,
  110.         ch;
  111.     struct {
  112.         int    first;
  113.         int    last;
  114.         char    condition[LINESIZE];
  115.     }
  116.         stackspace[STACKLIMIT],    /* first entry not used */
  117.         *sp = stackspace;
  118. #ifdef MAC
  119.     char    *which;
  120.     int    filecnt = 0;
  121.     bool    inmenu = NO;
  122.     struct fname {
  123.         char    *in, *out;
  124.     };
  125.     static const struct fname    fnt[] = {
  126.             { "keys.txt", "keys.c" },
  127.             { "menumaps.txt", "menumaps.c" },
  128.             { NULL, NULL }
  129.     };
  130.     const struct fname *fnp;
  131. #endif /* MAC */
  132.  
  133.     for (comnum = 1; commands[comnum].Name != NULL; comnum++) {
  134.         if (strcmp(commands[comnum-1].Name, commands[comnum].Name) >= 0) {
  135.             fprintf(stderr, "command %s is out of order\n",
  136.                 commands[comnum].Name);
  137.             exit(1);
  138.         }
  139.     }
  140.     for (comnum = 1; variables[comnum].Name != NULL; comnum++) {
  141.         if (strcmp(variables[comnum-1].Name, variables[comnum].Name) >= 0) {
  142.             fprintf(stderr, "variable %s is out of order\n", variables[comnum].Name);
  143.             exit(1);
  144.         }
  145.     }
  146. #ifdef MAC
  147.     /* don't know how to redirect, so we do tricks */
  148. for (fnp = fnt; fnp->in != NULL; fnp++) {
  149.     printf("setmaps <%s >%s\n", fnp->in, fnp->out);
  150.     ifile = fopen(fnp->in, "r");
  151.     if (ifile == NULL) {
  152.         perror(fnp->in);
  153.         exit(1);
  154.     }
  155.     of = fopen(fnp->out, "w");
  156.     if (of == NULL) {
  157.         perror(fnp->out);
  158.         exit(1);
  159.     }
  160. #else /* !MAC */
  161.     ifile = stdin;
  162.     of = stdout;
  163.     if (ifile == NULL || of == NULL) {
  164.         fprintf(stderr, "Cannot read input or write output.\n");
  165.         exit(1);
  166.     }
  167. #endif /* !MAC */
  168.     lino = 0;
  169.     ch = 0;
  170.     for (;;) {
  171.         if (fgets(line, sizeof line, ifile) == NULL) {
  172.             if (sp != stackspace) {
  173.                 fprintf(stderr, "EOF inside #if\n");
  174.                 exit(1);
  175.             }
  176.             fclose(of);
  177.             fclose(ifile);
  178.             break;
  179.         }
  180.         lino += 1;
  181.         if (StartsWith(line, "#if")) {
  182.             sp += 1;
  183.             if (sp == &stackspace[STACKLIMIT]) {
  184.                 fprintf(stderr,
  185.                     "conditionals nested too deeply at line %d\n",
  186.                     lino);
  187.                 exit(1);
  188.             }
  189.             sp->first = ch;
  190.             sp->last = -1;
  191.             strcpy(sp->condition, line);
  192.             fprintf(of, line);
  193.         } else if (StartsWith(line, "#else")) {
  194.             if (sp == stackspace || sp->last != -1) {
  195.                 fprintf(stderr, "ifdef/endif mismatch at line %d!\n",
  196.                     lino);
  197.                 exit(1);
  198.             }
  199.             sp->last = ch;
  200.             ch = sp->first;
  201.             fprintf(of, line);
  202.         } else if (StartsWith(line, "#endif")) {
  203.             if (sp == stackspace) {
  204.                 fprintf(stderr, "ifdef/endif mismatch at line %d!\n",
  205.                     lino);
  206.                 exit(1);
  207.             }
  208.             if (sp->last != -1 && ch != sp->last) {
  209.                 fprintf(stderr,
  210.                     "warning: unbalanced number of entries in #if ending at line %d",
  211.                     lino);
  212.             }
  213.             sp -= 1;
  214.             fprintf(of, line);
  215. #ifdef MAC
  216.         } else if (StartsWith(line, "#MENU")) {
  217.             inmenu = YES;
  218. #endif
  219.         } else if (StartsWith(line, "\t\"")) {
  220.             extract(comname, line);
  221.             if (strcmp(comname, "unbound") == 0) {
  222.                 comnum = -1;
  223.             } else {
  224.                 comnum = matchcmd(commands, comname);
  225. #ifdef MAC
  226.                 which = "commands";
  227.                 if (comnum < 0 && inmenu) {
  228.                     comnum = matchvar(variables, comname);
  229.                     which = "variables";
  230.                 }
  231. #endif
  232.                 if (comnum < 0) {
  233.                     fprintf(stderr,
  234.                         "warning: cannot find \"%s\", line %d",
  235.                         comname, lino);
  236.                     if (sp == stackspace) {
  237.                         fprintf(stderr, ".\n");
  238.                     } else {
  239.                         /* Note: condition ends with \n */
  240.                         fprintf(stderr, ", inside%s %s",
  241.                             sp->last == -1? "" : " else of",
  242.                             sp->condition);
  243.                     }
  244.                 }
  245.             }
  246. #ifdef MAC
  247.             if (inmenu) {
  248.                 if (comnum < 0)
  249.                     fprintf(of, "\t(data_obj *) NULL,\n");
  250.                 else
  251.                     fprintf(of, "\t(data_obj *) &%s[%d],\n",which, comnum);
  252.             } else /*...*/
  253. #endif
  254.             {
  255.                 if (comnum < 0)
  256.                     fprintf(of, "\t(data_obj *) NULL,\t\t/* %s */\n", PPchar(ch));
  257.                 else
  258.                     fprintf(of, "\t(data_obj *) &commands[%d],\t/* %s */\n", comnum, PPchar(ch));
  259.                 ch += 1;
  260.             }
  261.         } else {
  262.             /* If unrecognized, pass and prepare to start new table */
  263.             fprintf(of, line);
  264.             ch = 0;
  265.         }
  266.     }
  267. #ifdef MAC
  268. }
  269. #endif
  270.     return 0;
  271. }
  272.