home *** CD-ROM | disk | FTP | other *** search
- /*
- ** This software is Copyright (c) 1991 by Daniel Weaver.
- **
- ** Permission is hereby granted to copy, distribute or otherwise
- ** use any part of this package as long as you do not try to make
- ** money from it or pretend that you wrote it. This copyright
- ** notice must be maintained in any copy made.
- **
- ** Use of this software constitutes acceptance for use in an AS IS
- ** condition. There are NO warranties with regard to this software.
- ** In no event shall the author be liable for any damages whatsoever
- ** arising out of or in connection with the use or performance of this
- ** software. Any use of this software is at the user's own risk.
- **
- ** If you make modifications to this software that you feel
- ** increases it usefulness for the rest of the community, please
- ** email the changes, enhancements, bug fixes as well as any and
- ** all ideas to me. This software is going to be maintained and
- ** enhanced as deemed necessary by the community.
- */
- #include <stdio.h>
-
- /* Create the files needed to fetch the termcap.
- This program is designed to run under System V. */
-
- extern char *boolfnames[], *boolcodes[], *boolnames[];
- extern char *numfnames[], *numcodes[], *numnames[];
- extern char *strfnames[], *strcodes[], *strnames[];
-
- static int wc, cpl;
- static FILE *cp, *hp;
-
- static char tc_key[] =
- "kz00kb01cr02bk03ta04bt05kl06kr07ku08kd09kh10EN11PU12PD13ki14kk15LI16LD17kp18ek19ck20pk21mk22nk23ok24";
-
- #define CAP_BOOL 1
- #define CAP_NUM 2
- #define CAP_STR 3
- #define CAP_KEY 4
-
- struct remap_list {
- int type;
- char *cap_name, *info_name, *address;
- };
-
- struct remap_list tc_remap[] = {
- CAP_STR, "CF", /* vi */ "civis", "cursor_invisible",
- CAP_STR, "CO", 0, "cursor_on",
- CAP_STR, "GS", /* as */ "smacs", "enter_alt_charset_mode",
- CAP_STR, "GE", /* ae */ "rmacs", "exit_alt_charset_mode",
- CAP_STR, "ct", 0, "color_terminal",
- CAP_STR, 0, 0, "clear_all_tabs",
- 0, 0, 0, 0};
-
- struct remap_list xenix_remap[] = {
- CAP_STR, "GS", /* as */ "smacs", "enter_alt_charset_mode",
- CAP_STR, "GE", /* ae */ "rmacs", "exit_alt_charset_mode",
- CAP_STR, 0, /* rs1 */ 0, "reset_1string",
- CAP_STR, 0, /* rs3 */ 0, "reset_3string",
- CAP_STR, 0, /* is1 */ 0, "init_1string",
- CAP_STR, 0, /* is3 */ 0, "init_3string",
- CAP_KEY, "CL", 0, "key_char_left",
- CAP_KEY, "CW", 0, "key_change_window",
- CAP_KEY, "EN", 0, "key_send",
- CAP_KEY, "HM", 0, "key_Home",
- CAP_KEY, "LD", 0, "key_line_delete",
- CAP_KEY, "LF", 0, "key_linefeed",
- CAP_KEY, "NU", 0, "key_next_unlocked_cell",
- CAP_KEY, "PN", 0, "key_start_print",
- CAP_KEY, "PD", 0, "key_page_down",
- CAP_KEY, "PU", 0, "key_page_up",
- CAP_KEY, "PS", 0, "key_stop_print",
- CAP_KEY, "RC", 0, "key_recalc",
- CAP_KEY, "RF", 0, "key_toggle_ref",
- CAP_KEY, "RT", 0, "key_return",
- CAP_KEY, "UP", 0, "key_up_arrow",
- CAP_KEY, "WL", 0, "key_word_left",
- CAP_KEY, "WR", 0, "key_word_right",
- #ifdef HUH
- CAP_STR, "MON", /* SA */ "smam", "enter_am_mode",
- CAP_STR, "MOF", /* RA */ "rmam", "exit_am_mode",
- #endif
- 0, 0, 0, 0};
-
-
- send(s)
- char *s;
- { /* write the data for the .h file */
- int ls = strlen(s);
-
- if (wc) fprintf(hp, ",");
- ++wc;
- if (wc + ls > 70) {
- fprintf(hp, "\n"); wc = 0;
- }
- fprintf(hp, " %s", s); wc += ls + 1;
- }
-
-
- send_names(info, cap)
- char *info, *cap;
- { /* build a string array with the cap and info names */
- cpl += strlen(info) + strlen(cap) + 5;
- if (cpl > 70)
- {
- fprintf(cp, "\n\t");
- cpl = strlen(info) + strlen(cap) + 12;
- }
- else fprintf(cp, " ");
- fprintf(cp, "\"%s:%s\",", info, cap);
- }
-
-
- remap_keys(def, key)
- char *def;
- struct remap_list key[];
- { /* expand a termcap personality */
- int i, j;
-
- fprintf(cp, "#ifdef %s\n", def);
- for (i = j = 0; key[i].type; ++i) {
- if (key[i].cap_name) {
- switch (key[i].type) {
- case CAP_BOOL:
- fprintf(cp, "\t%s = tgetflag(\"%s\");\n",
- key[i].address, key[i].cap_name);
- break;
- case CAP_NUM:
- fprintf(cp, "\t%s = tgetnum(\"%s\");\n",
- key[i].address, key[i].cap_name);
- break;
- case CAP_STR:
- fprintf(cp, "\t%s = tgetstr(\"%s\", &bb);\n",
- key[i].address, key[i].cap_name);
- break;
- case CAP_KEY:
- fprintf(cp,
- " enter_key(\"%s\", %s = tgetstr(\"%s\", &bb));\n",
- key[i].cap_name, key[i].address, key[i].cap_name);
- break;
- }
- if (key[i].info_name) fprintf(cp,
- "\tcaptrans[cap_index(\"%s\", %d)] = \"%s:%s\";\n",
- key[i].info_name, strlen(key[i].info_name),
- key[i].info_name, key[i].cap_name);
- else {
- if (j++ == 0) fprintf(hp, "\n#ifdef %s\n", def);
- switch (key[i].type) {
- case CAP_BOOL:
- fprintf(hp, "EXTERN char %s;\n", key[i].address);
- break;
- case CAP_NUM:
- fprintf(hp, "EXTERN int %s;\n", key[i].address);
- break;
- case CAP_KEY:
- case CAP_STR:
- fprintf(hp, "EXTERN CHARSTAR %s;\n", key[i].address);
- break;
- }
- }
- }
- else /* name is missing. Clear the value */
- switch (key[i].type) {
- case CAP_BOOL:
- fprintf(cp, "\t%s = 0;\n", key[i].address);
- break;
- case CAP_NUM:
- fprintf(cp, "\t%s = -1;\n", key[i].address);
- break;
- case CAP_KEY:
- case CAP_STR:
- fprintf(cp, "\t%s = (char *) 0;\n", key[i].address);
- break;
- }
- }
- fprintf(cp, "#endif\n");
- if (j) fprintf(hp, "#endif\n");
- }
-
-
- main()
- {
- int i, j, k, max_key, tc_def, more_keys;
-
- if (!(hp = fopen("bsd.h", "w")))
- {
- fprintf(stdout, "Can't open bsd.h\n");
- exit(1);
- }
- if (!(cp = fopen("getcaps.c", "w")))
- {
- fprintf(stdout, "Can't open getcaps.c\n");
- exit(1);
- }
-
- fprintf(hp, "/* termcap to terminfo conversion */\n\n");
- fprintf(hp, "#ifdef MAIN\n#define EXTERN\n");
- fprintf(hp, "#else\n#define EXTERN extern\n#endif\n\n");
- fprintf(hp, "typedef char *CHARSTAR;\n");
-
- fprintf(cp, "/* termcap to terminfo conversion */\n");
- fprintf(cp, "#ifdef TESTCAP\n");
- fprintf(cp, "#include \"bsd.h\"\n");
- fprintf(cp, "#ifdef LIBTC\n");
- fprintf(cp, "#define tgetent tcgetent\n");
- fprintf(cp, "#define tgetnum tcgetnum\n");
- fprintf(cp, "#define tgetflag tcgetflag\n");
- fprintf(cp, "#define tgetstr tcgetstr\n");
- fprintf(cp, "char ttytype[256];\n");
- fprintf(cp, "static char *s, *t;\n");
- fprintf(cp, "extern char *gtname();\n");
- fprintf(cp, "static int l;\n");
- fprintf(cp, "#endif\n");
- fprintf(cp, "extern char *tgetstr();\n");
- fprintf(cp, "char cap_entry[4096];\n");
- fprintf(cp, "static char buf[4096];\n");
- fprintf(cp, "static char *bb = buf;\n");
- fprintf(cp, "char *captrans[] = {");
- cpl = 1024;
- for (i = 0; boolfnames[i]; ++i)
- send_names(boolnames[i], boolcodes[i]);
- for (i = 0; numfnames[i]; ++i)
- send_names(numnames[i], numcodes[i]);
- for (i = 0; strfnames[i]; ++i)
- send_names(strnames[i], strcodes[i]);
- fprintf(cp, " 0};\n");
- fprintf(cp, "readcaps() {\n\n");
- fprintf(cp, " tgetent(cap_entry, getenv(\"TERM\"));\n");
- fprintf(cp, "#ifdef LIBTC\n");
- fprintf(cp, " for (s = cap_entry; *s != '|'; s++)\n");
- fprintf(cp, " if (*s == '\\0' | *s == ':') { s = cap_entry; break; }\n");
- fprintf(cp, " if (*s == '|') s++;\n");
- fprintf(cp, " for (t = ttytype; *t = *s; t++, s++)\n");
- fprintf(cp, " if (*t == ':') { *t = '\\0'; break; }\n");
- fprintf(cp, "#endif\n");
- fprintf(hp, "\nEXTERN char\n"); wc = 0;
- for (i = 0; boolfnames[i]; ++i) {
- send(boolfnames[i]);
- fprintf(cp, " %s = tgetflag(\"%s\");\n",
- boolfnames[i], boolcodes[i]);
- }
- send("physical_tabs"); /* no terminfo equivalent */
- fprintf(cp, " physical_tabs = tgetflag(\"pt\");\n");
- fprintf(hp, ";\n\nEXTERN int\n"); wc = 0;
- for (i = 0; numfnames[i]; ++i) {
- send(numfnames[i]);
- if (strcmp(numcodes[i], "it"))
- {
- fprintf(cp, " %s = tgetnum(\"%s\");\n",
- numfnames[i], numcodes[i]);
- }
- else
- {
- fprintf(cp, "#ifndef TC\n");
- fprintf(cp, " %s = tgetnum(\"%s\");\n",
- numfnames[i], numcodes[i]);
- fprintf(cp, "#endif\n");
- }
- }
- fprintf(hp, ";\n\nEXTERN CHARSTAR\n"); wc = 0;
- fprintf(cp, "#ifdef TC\n");
- for (i = 0; i < 140; ++i) {
- j = i / 10; k = '0' + i % 10;
- fprintf(cp,
- " enter_lab(\"%c%c\", tgetstr(\"%c%c\", &bb), \"%c%c\", tgetstr(\"%c%c\", &bb));\n",
- 'm' + j, k, 'm' + j, k, 'M' + j, k, 'M' + j, k);
- }
- for (i = 0; tc_key[i]; i += 4) {
- fprintf(cp,
- " enter_lab(\"%c%c\", tgetstr(\"%c%c\", &bb), \"%c%c\", tgetstr(\"%c%c\", &bb));\n",
- tc_key[i], tc_key[i + 1], tc_key[i], tc_key[i + 1],
- tc_key[i + 2], tc_key[i + 3],
- tc_key[i + 2], tc_key[i + 3]);
- }
- fprintf(cp, "#endif\n");
- max_key = tc_def = 0;
- more_keys = 1;
- for (i = 0; strfnames[i]; ++i) {
- send(strfnames[i]);
- k = strcodes[i][0];
- j = strcodes[i][1];
- if (k == 'k' && ((j >= '0' && j <= '9') || j == ';'))
- {
- if (tc_def) { fprintf(cp, "#endif\n"); tc_def = 0; }
- max_key = i;
- k = (j == ';') ? 10 : j - '0';
- fprintf(cp,
- " enter_lab(\"k%c\", key_f%d = tgetstr(\"k%c\", &bb),\n\t\t\"l%c\", lab_f%d = tgetstr(\"l%c\", &bb));\n",
- j, k, j, j, k, j);
- }
- else
- if (more_keys && (strnames[i][0] == 'k' ||
- (k == 't' && j == 'a')))
- {
- if (tc_def) { fprintf(cp, "#endif\n"); tc_def = 0; }
- max_key = i;
- fprintf(cp,
- " enter_key(\"%s\", %s = tgetstr(\"%s\", &bb));\n",
- strcodes[i], strfnames[i], strcodes[i]);
- if (strcmp("tab", strfnames[i]) == 0) more_keys = 0;
- }
- else
- {
- /* watch out for TC function keys */
- if ((k >= 'm' & k <= 'z') && (j >= '0' & j <= '9'))
- {
- if (!tc_def) fprintf(cp, "#ifndef TC\n");
- tc_def = 1;
- }
- else
- if (tc_def)
- {
- fprintf(cp, "#endif\n");
- tc_def = 0;
- }
- fprintf(cp, " %s = tgetstr(\"%s\", &bb);\n",
- strfnames[i], strcodes[i]);
- }
- }
- if (tc_def) fprintf(cp, "#endif\n");
- send("reset_string"); /* terminfo maps it wrong */
- fprintf(cp, " reset_string = tgetstr(\"rs\");\n");
- fprintf(hp, ";\n");
- remap_keys("TC", tc_remap);
- remap_keys("XENIX", xenix_remap);
- fprintf(hp, "\n#define MAX_STRINGS %d\n", i);
- fprintf(cp, "}\n#endif\n");
- fprintf(cp, "#ifndef SVR3\n");
- fprintf(cp, "#ifndef SVR3_2\n");
- fprintf(cp, "char *strnames[] = {\n\t");
- j = 8;
- for (i = 0; i < max_key; ++i) {
- j += strlen(strnames[i]) + 4;
- fprintf(cp, "\"%s\"", strnames[i]);
- if (j > 65)
- {
- fprintf(cp, ",\n ");
- j = 8;
- }
- else fprintf(cp, ", ");
- }
- fprintf(cp, "0};\n#endif\n");
- fprintf(cp, "#endif\n");
- fclose(cp);
- fclose(hp);
- }
-