home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / src / baseline / jove-4.14.6.lha / jove-4.14.6 / setmaps.c < prev    next >
C/C++ Source or Header  |  1992-01-10  |  4KB  |  184 lines

  1. /***************************************************************************
  2.  * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
  3.  * is 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. #define TXT_TO_C    1    /* must be a number for MAC compiler */
  9.  
  10. #include <stdio.h>
  11.  
  12. #include "funcdefs.c"
  13.  
  14. #ifdef    MAC
  15. #include "vars.c"
  16. #endif
  17.  
  18. private int
  19. matchcmd(choices, what)
  20. register const struct cmd    choices[];
  21. register char    *what;
  22. {
  23.     register int    i;
  24.  
  25.     for (i = 0; choices[i].Name != NULL; i++) {
  26.         if (what[0] == choices[i].Name[0]
  27.         && strcmp(what, choices[i].Name) == 0)
  28.             return i;
  29.     }
  30.     return -1;
  31. }
  32.  
  33. #ifdef    MAC
  34. matchvar(choices, what)
  35. register struct variable choices[];
  36. register char    *what;
  37. {
  38.     register int    len;
  39.     int    i;
  40.  
  41.     len = strlen(what);
  42.     for (i = 0; choices[i].Name != NULL; i++) {
  43.         if (what[0] == choices[i].Name[0]
  44.         && strcmp(what, choices[i].Name) == 0)
  45.             return i;
  46.     }
  47.     return -1;
  48. }
  49. #endif
  50.  
  51. static int
  52. StartsWith(s, pre)
  53. const char *s, *pre;
  54. {
  55.     return strncmp(s, pre, strlen(pre)-1) == 0;
  56. }
  57.  
  58. private char *
  59. PPchar(c)
  60. int    c;
  61. {
  62.     static char    str[16];
  63.     char    *cp = str;
  64.  
  65.     if (c & 0200) {
  66.         c &= ~0200;
  67.         strcpy(cp, "M-");
  68.         cp += 2;
  69.     }
  70.     if (c == '\033')
  71.         strcpy(cp, "ESC");
  72.     else if (c < ' ')
  73.         (void) sprintf(cp, "C-%c", c + '@');
  74.     else if (c == '\177')
  75.         strcpy(cp, "^?");
  76.     else
  77.         (void) sprintf(cp, "%c", c);
  78.     return str;
  79. }
  80.  
  81. private void
  82. extract(into, from)
  83. char    *into,
  84.     *from;
  85. {
  86.     from += 2;    /* Past tab and first double quote. */
  87.     while ((*into = *from++) != '"')
  88.         into += 1;
  89.     *into = '\0';
  90. }
  91.  
  92.  
  93. void
  94.  
  95. #ifdef    MAC
  96. _main()        /* for Mac, so we can use redirection */
  97. #else
  98. main()
  99. #endif
  100. {
  101.     FILE    *ifile,
  102.         *of;
  103.     char    line[100],
  104.         comname[70];
  105.     int    comnum,
  106.         ch = 0,
  107.         savech = -1,
  108.         errors = 0;
  109. #ifdef    MAC
  110.     char    *which;
  111.     bool    inmenu = NO;
  112. #endif
  113.  
  114.     ifile = stdin;
  115.     of = stdout;
  116.     if (ifile == NULL || of == NULL) {
  117.         printf("Cannot read input or write output.\n");
  118.         exit(1);
  119.     }
  120.     while (fgets(line, sizeof line, ifile) != NULL) {
  121.         if (StartsWith(line, "#if")) {
  122.             savech = ch;
  123.             fprintf(of, line);
  124.             continue;
  125.         } else if (StartsWith(line, "#else")) {
  126.             if (savech == -1)
  127.                 fprintf(stderr, "WARNING: ifdef/endif mismatch!\n");
  128.             else
  129.                 ch = savech;
  130.             fprintf(of, line);
  131.             continue;
  132.         } else if (StartsWith(line, "#endif")) {
  133.             savech = -1;
  134.             fprintf(of, line);
  135.             continue;
  136. #ifdef    MAC
  137.         } else if (StartsWith(line, "#MENU")) {
  138.             inmenu = YES;
  139.             continue;
  140. #endif
  141.         } else if (!StartsWith(line, "\t\"")) {
  142.             /* If unrecognized, pass and prepare to start new table */
  143.             fprintf(of, line);
  144.             ch = 0;
  145.             continue;
  146.         }
  147.         extract(comname, line);
  148.         if (strcmp(comname, "unbound") == 0) {
  149.             comnum = -1;
  150.         } else {
  151.             comnum = matchcmd(commands, comname);
  152. #ifdef    MAC
  153.             which = "commands";
  154.             if (comnum < 0 && inmenu) {
  155.                 comnum = matchvar(variables, comname);
  156.                 which = "variables";
  157.             }
  158. #endif
  159.             if (comnum < 0) {
  160.                 fprintf(stderr, "Warning: cannot find \"%s\".\n", comname);
  161.                 errors += 1;
  162.             }
  163.         }
  164. #ifdef    MAC
  165.         if (inmenu) {
  166.             if (comnum < 0)
  167.                 fprintf(of, "\t(data_obj *) NULL,\n");
  168.             else
  169.                 fprintf(of, "\t(data_obj *) &%s[%d],\n",which, comnum);
  170.         } else /*...*/
  171. #endif
  172.         {
  173.             if (comnum < 0)
  174.                 fprintf(of, "\t(data_obj *) NULL,\t\t/* %s */\n", PPchar(ch));
  175.             else
  176.                 fprintf(of, "\t(data_obj *) &commands[%d],\t/* %s */\n", comnum, PPchar(ch));
  177.             ch += 1;
  178.         }
  179.     }
  180.     fclose(of);
  181.     fclose(ifile);
  182.     exit(errors);
  183. }
  184.