home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume25 / ted / part03 / build.c next >
Encoding:
C/C++ Source or Header  |  1991-11-06  |  12.5 KB  |  347 lines

  1. /*
  2. ** This software is Copyright (c) 1991 by Daniel Weaver.
  3. **
  4. ** Permission is hereby granted to copy, distribute or otherwise
  5. ** use any part of this package as long as you do not try to make
  6. ** money from it or pretend that you wrote it.  This copyright
  7. ** notice must be maintained in any copy made.
  8. **
  9. ** Use of this software constitutes acceptance for use in an AS IS
  10. ** condition. There are NO warranties with regard to this software.
  11. ** In no event shall the author be liable for any damages whatsoever
  12. ** arising out of or in connection with the use or performance of this
  13. ** software.  Any use of this software is at the user's own risk.
  14. **
  15. **  If you make modifications to this software that you feel
  16. **  increases it usefulness for the rest of the community, please
  17. **  email the changes, enhancements, bug fixes as well as any and
  18. **  all ideas to me. This software is going to be maintained and
  19. **  enhanced as deemed necessary by the community.
  20. */
  21. #include <stdio.h>
  22.  
  23. /* Create the files needed to fetch the termcap.
  24.    This program is designed to run under System V. */
  25.  
  26. extern char *boolfnames[], *boolcodes[], *boolnames[];
  27. extern char *numfnames[], *numcodes[], *numnames[];
  28. extern char *strfnames[], *strcodes[], *strnames[];
  29.  
  30. static int wc, cpl;
  31. static FILE *cp, *hp;
  32.  
  33. static char tc_key[] =
  34. "kz00kb01cr02bk03ta04bt05kl06kr07ku08kd09kh10EN11PU12PD13ki14kk15LI16LD17kp18ek19ck20pk21mk22nk23ok24";
  35.  
  36. #define CAP_BOOL 1
  37. #define CAP_NUM 2
  38. #define CAP_STR 3
  39. #define CAP_KEY 4
  40.  
  41. struct remap_list {
  42.    int type;
  43.    char *cap_name, *info_name, *address;
  44. };
  45.  
  46. struct remap_list tc_remap[] = {
  47.    CAP_STR, "CF", /* vi */ "civis", "cursor_invisible",
  48.    CAP_STR, "CO",                0, "cursor_on",
  49.    CAP_STR, "GS", /* as */ "smacs", "enter_alt_charset_mode",
  50.    CAP_STR, "GE", /* ae */ "rmacs", "exit_alt_charset_mode",
  51.    CAP_STR, "ct",                0, "color_terminal",
  52.    CAP_STR,    0,                0, "clear_all_tabs",
  53.    0, 0, 0, 0};
  54.  
  55. struct remap_list xenix_remap[] = {
  56.    CAP_STR, "GS", /* as */ "smacs", "enter_alt_charset_mode",
  57.    CAP_STR, "GE", /* ae */ "rmacs", "exit_alt_charset_mode",
  58.    CAP_STR,    0,      /* rs1 */ 0, "reset_1string",
  59.    CAP_STR,    0,      /* rs3 */ 0, "reset_3string",
  60.    CAP_STR,    0,      /* is1 */ 0, "init_1string",
  61.    CAP_STR,    0,      /* is3 */ 0, "init_3string",
  62.    CAP_KEY, "CL",                0, "key_char_left",
  63.    CAP_KEY, "CW",                0, "key_change_window",
  64.    CAP_KEY, "EN",                0, "key_send",
  65.    CAP_KEY, "HM",                0, "key_Home",
  66.    CAP_KEY, "LD",                0, "key_line_delete",
  67.    CAP_KEY, "LF",                0, "key_linefeed",
  68.    CAP_KEY, "NU",                0, "key_next_unlocked_cell",
  69.    CAP_KEY, "PN",                0, "key_start_print",
  70.    CAP_KEY, "PD",                0, "key_page_down",
  71.    CAP_KEY, "PU",                0, "key_page_up",
  72.    CAP_KEY, "PS",                0, "key_stop_print",
  73.    CAP_KEY, "RC",                0, "key_recalc",
  74.    CAP_KEY, "RF",                0, "key_toggle_ref",
  75.    CAP_KEY, "RT",                0, "key_return",
  76.    CAP_KEY, "UP",                0, "key_up_arrow",
  77.    CAP_KEY, "WL",                0, "key_word_left",
  78.    CAP_KEY, "WR",                0, "key_word_right",
  79. #ifdef HUH
  80.    CAP_STR, "MON", /* SA */ "smam", "enter_am_mode",
  81.    CAP_STR, "MOF", /* RA */ "rmam", "exit_am_mode",
  82. #endif
  83.    0, 0, 0, 0};
  84.  
  85.  
  86. send(s)
  87. char *s;
  88.    {  /* write the data for the .h file */
  89.       int ls = strlen(s);
  90.  
  91.       if (wc) fprintf(hp, ",");
  92.       ++wc;
  93.       if (wc + ls > 70) {
  94.             fprintf(hp, "\n");  wc = 0;
  95.          }
  96.       fprintf(hp, " %s", s);  wc += ls + 1;
  97.    }
  98.  
  99.  
  100. send_names(info, cap)
  101. char *info, *cap;
  102.    {  /* build a string array with the cap and info names */
  103.       cpl += strlen(info) + strlen(cap) + 5;
  104.       if (cpl > 70)
  105.          {
  106.             fprintf(cp, "\n\t");
  107.             cpl = strlen(info) + strlen(cap) + 12;
  108.          }
  109.       else fprintf(cp, " ");
  110.       fprintf(cp, "\"%s:%s\",", info, cap);
  111.    }
  112.  
  113.  
  114. remap_keys(def, key)
  115. char *def;
  116. struct remap_list key[];
  117.    {  /* expand a termcap personality */
  118.       int i, j;
  119.  
  120.       fprintf(cp, "#ifdef %s\n", def);
  121.          for (i = j = 0; key[i].type; ++i) {
  122.             if (key[i].cap_name) {
  123.                switch (key[i].type) {
  124.                case CAP_BOOL:
  125.                   fprintf(cp, "\t%s = tgetflag(\"%s\");\n",
  126.                      key[i].address, key[i].cap_name);
  127.                   break;
  128.                case CAP_NUM:
  129.                   fprintf(cp, "\t%s = tgetnum(\"%s\");\n",
  130.                      key[i].address, key[i].cap_name);
  131.                   break;
  132.                case CAP_STR:
  133.                   fprintf(cp, "\t%s = tgetstr(\"%s\", &bb);\n",
  134.                      key[i].address, key[i].cap_name);
  135.                   break;
  136.                case CAP_KEY:
  137.                   fprintf(cp,
  138.                   "    enter_key(\"%s\", %s = tgetstr(\"%s\", &bb));\n",
  139.                      key[i].cap_name, key[i].address, key[i].cap_name);
  140.                   break;
  141.                }
  142.                if (key[i].info_name) fprintf(cp,
  143.                   "\tcaptrans[cap_index(\"%s\", %d)] = \"%s:%s\";\n",
  144.                   key[i].info_name, strlen(key[i].info_name),
  145.                   key[i].info_name, key[i].cap_name);
  146.                else {
  147.                   if (j++ == 0) fprintf(hp, "\n#ifdef %s\n", def);
  148.                   switch (key[i].type) {
  149.                   case CAP_BOOL:
  150.                      fprintf(hp, "EXTERN char %s;\n", key[i].address);
  151.                      break;
  152.                   case CAP_NUM:
  153.                      fprintf(hp, "EXTERN int %s;\n", key[i].address);
  154.                      break;
  155.                   case CAP_KEY:
  156.                   case CAP_STR:
  157.                      fprintf(hp, "EXTERN CHARSTAR %s;\n", key[i].address);
  158.                      break;
  159.                   }
  160.                }
  161.             }
  162.             else /* name is missing.  Clear the value */
  163.             switch (key[i].type) {
  164.             case CAP_BOOL:
  165.                fprintf(cp, "\t%s = 0;\n", key[i].address);
  166.                break;
  167.             case CAP_NUM:
  168.                fprintf(cp, "\t%s = -1;\n", key[i].address);
  169.                break;
  170.             case CAP_KEY:
  171.             case CAP_STR:
  172.                fprintf(cp, "\t%s = (char *) 0;\n", key[i].address);
  173.                break;
  174.             }
  175.          }
  176.       fprintf(cp, "#endif\n");
  177.       if (j) fprintf(hp, "#endif\n");
  178.    }
  179.  
  180.  
  181. main()
  182.    {
  183.       int i, j, k, max_key, tc_def, more_keys;
  184.  
  185.       if (!(hp = fopen("bsd.h", "w")))
  186.          {
  187.             fprintf(stdout, "Can't open bsd.h\n");
  188.             exit(1);
  189.          }
  190.       if (!(cp = fopen("getcaps.c", "w")))
  191.          {
  192.             fprintf(stdout, "Can't open getcaps.c\n");
  193.             exit(1);
  194.          }
  195.  
  196.       fprintf(hp, "/* termcap to terminfo conversion */\n\n");
  197.       fprintf(hp, "#ifdef MAIN\n#define EXTERN\n");
  198.       fprintf(hp, "#else\n#define EXTERN extern\n#endif\n\n");
  199.       fprintf(hp, "typedef char *CHARSTAR;\n");
  200.  
  201.       fprintf(cp, "/* termcap to terminfo conversion */\n");
  202.       fprintf(cp, "#ifdef TESTCAP\n");
  203.       fprintf(cp, "#include \"bsd.h\"\n");
  204.       fprintf(cp, "#ifdef LIBTC\n");
  205.       fprintf(cp, "#define tgetent tcgetent\n");
  206.       fprintf(cp, "#define tgetnum tcgetnum\n");
  207.       fprintf(cp, "#define tgetflag tcgetflag\n");
  208.       fprintf(cp, "#define tgetstr tcgetstr\n");
  209.       fprintf(cp, "char ttytype[256];\n");
  210.       fprintf(cp, "static char *s, *t;\n");
  211.       fprintf(cp, "extern char *gtname();\n");
  212.       fprintf(cp, "static int l;\n");
  213.       fprintf(cp, "#endif\n");
  214.       fprintf(cp, "extern char *tgetstr();\n");
  215.       fprintf(cp, "char cap_entry[4096];\n");
  216.       fprintf(cp, "static char buf[4096];\n");
  217.       fprintf(cp, "static char *bb = buf;\n");
  218.       fprintf(cp, "char *captrans[] = {");
  219.       cpl = 1024;
  220.          for (i = 0; boolfnames[i]; ++i)
  221.             send_names(boolnames[i], boolcodes[i]);
  222.          for (i = 0; numfnames[i]; ++i)
  223.             send_names(numnames[i], numcodes[i]);
  224.          for (i = 0; strfnames[i]; ++i)
  225.             send_names(strnames[i], strcodes[i]);
  226.       fprintf(cp, " 0};\n");
  227.       fprintf(cp, "readcaps() {\n\n");
  228.       fprintf(cp, "    tgetent(cap_entry, getenv(\"TERM\"));\n");
  229.       fprintf(cp, "#ifdef LIBTC\n");
  230.       fprintf(cp, "    for (s = cap_entry; *s != '|'; s++)\n");
  231.       fprintf(cp, "        if (*s == '\\0' | *s == ':') { s = cap_entry; break; }\n");
  232.       fprintf(cp, "    if (*s == '|') s++;\n");
  233.       fprintf(cp, "    for (t = ttytype; *t = *s; t++, s++)\n");
  234.       fprintf(cp, "        if (*t == ':') { *t = '\\0'; break; }\n");
  235.       fprintf(cp, "#endif\n");
  236.       fprintf(hp, "\nEXTERN char\n");  wc = 0;
  237.          for (i = 0; boolfnames[i]; ++i) {
  238.             send(boolfnames[i]);
  239.             fprintf(cp, "    %s = tgetflag(\"%s\");\n",
  240.                boolfnames[i], boolcodes[i]);
  241.          }
  242.       send("physical_tabs");  /* no terminfo equivalent */
  243.       fprintf(cp, "    physical_tabs = tgetflag(\"pt\");\n");
  244.       fprintf(hp, ";\n\nEXTERN int\n");  wc = 0;
  245.          for (i = 0; numfnames[i]; ++i) {
  246.             send(numfnames[i]);
  247.             if (strcmp(numcodes[i], "it"))
  248.                {
  249.                   fprintf(cp, "    %s = tgetnum(\"%s\");\n",
  250.                   numfnames[i], numcodes[i]);
  251.                }
  252.             else
  253.                {
  254.                   fprintf(cp, "#ifndef TC\n");
  255.                   fprintf(cp, "    %s = tgetnum(\"%s\");\n",
  256.                   numfnames[i], numcodes[i]);
  257.                   fprintf(cp, "#endif\n");
  258.                }
  259.          }
  260.       fprintf(hp, ";\n\nEXTERN CHARSTAR\n");  wc = 0;
  261.       fprintf(cp, "#ifdef TC\n");
  262.          for (i = 0; i < 140; ++i) {
  263.             j = i / 10;  k = '0' + i % 10;
  264.             fprintf(cp,
  265.             "    enter_lab(\"%c%c\", tgetstr(\"%c%c\", &bb), \"%c%c\", tgetstr(\"%c%c\", &bb));\n",
  266.                'm' + j, k, 'm' + j, k, 'M' + j, k, 'M' + j, k);
  267.          }
  268.          for (i = 0; tc_key[i]; i += 4) {
  269.             fprintf(cp,
  270.             "    enter_lab(\"%c%c\", tgetstr(\"%c%c\", &bb), \"%c%c\", tgetstr(\"%c%c\", &bb));\n",
  271.                tc_key[i], tc_key[i + 1], tc_key[i], tc_key[i + 1],
  272.                tc_key[i + 2], tc_key[i + 3],
  273.                tc_key[i + 2], tc_key[i + 3]);
  274.          }
  275.       fprintf(cp, "#endif\n");
  276.       max_key = tc_def = 0;
  277.       more_keys = 1;
  278.          for (i = 0; strfnames[i]; ++i) {
  279.             send(strfnames[i]);
  280.             k = strcodes[i][0];
  281.             j = strcodes[i][1];
  282.             if (k == 'k' && ((j >= '0' && j <= '9') || j == ';'))
  283.                {
  284.                   if (tc_def) { fprintf(cp, "#endif\n"); tc_def = 0; }
  285.                   max_key = i;
  286.                   k = (j == ';') ? 10 : j - '0';
  287.                   fprintf(cp,
  288.                   "    enter_lab(\"k%c\", key_f%d = tgetstr(\"k%c\", &bb),\n\t\t\"l%c\", lab_f%d = tgetstr(\"l%c\", &bb));\n",
  289.                      j, k, j, j, k, j);
  290.                }
  291.             else
  292.             if (more_keys && (strnames[i][0] == 'k' ||
  293.                (k == 't' && j == 'a')))
  294.                {
  295.                   if (tc_def) { fprintf(cp, "#endif\n"); tc_def = 0; }
  296.                   max_key = i;
  297.                   fprintf(cp,
  298.                   "    enter_key(\"%s\", %s = tgetstr(\"%s\", &bb));\n",
  299.                   strcodes[i], strfnames[i], strcodes[i]);
  300.                   if (strcmp("tab", strfnames[i]) == 0) more_keys = 0;
  301.                }
  302.             else
  303.                {
  304.                   /* watch out for TC function keys */
  305.                   if ((k >= 'm' & k <= 'z') && (j >= '0' & j <= '9'))
  306.                      {
  307.                         if (!tc_def) fprintf(cp, "#ifndef TC\n");
  308.                         tc_def = 1;
  309.                      }
  310.                   else
  311.                   if (tc_def)
  312.                      {
  313.                         fprintf(cp, "#endif\n");
  314.                         tc_def = 0;
  315.                      }
  316.                   fprintf(cp, "    %s = tgetstr(\"%s\", &bb);\n",
  317.                      strfnames[i], strcodes[i]);
  318.                }
  319.          }
  320.       if (tc_def) fprintf(cp, "#endif\n");
  321.       send("reset_string");  /* terminfo maps it wrong */
  322.       fprintf(cp, "    reset_string = tgetstr(\"rs\");\n");
  323.       fprintf(hp, ";\n");
  324.       remap_keys("TC", tc_remap);
  325.       remap_keys("XENIX", xenix_remap);
  326.       fprintf(hp, "\n#define MAX_STRINGS %d\n", i);
  327.       fprintf(cp, "}\n#endif\n");
  328.       fprintf(cp, "#ifndef SVR3\n");
  329.       fprintf(cp, "#ifndef SVR3_2\n");
  330.       fprintf(cp, "char *strnames[] = {\n\t");
  331.       j = 8;
  332.          for (i = 0; i < max_key; ++i) {
  333.             j += strlen(strnames[i]) + 4;
  334.             fprintf(cp, "\"%s\"", strnames[i]);
  335.             if (j > 65)
  336.                {
  337.                   fprintf(cp, ",\n    ");
  338.                   j = 8;
  339.                }
  340.             else fprintf(cp, ", ");
  341.          }
  342.       fprintf(cp, "0};\n#endif\n");
  343.       fprintf(cp, "#endif\n");
  344.       fclose(cp);
  345.       fclose(hp);
  346.    }
  347.