home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / mytinfo / part03 / tset.c < prev   
Encoding:
C/C++ Source or Header  |  1992-12-26  |  15.0 KB  |  895 lines

  1. /*
  2.  * tset.c
  3.  * 
  4.  * By Ross Ridge
  5.  * Public Domain
  6.  * 92/02/19 18:53:12
  7.  *
  8.  */
  9.  
  10. #define NOTLIB
  11.  
  12. #include "defs.h"
  13.  
  14. static const char SCCSid[] = "@(#) mytinfo tset.c 3.3 92/02/19 public domain, By Ross Ridge";
  15.  
  16. #define SINGLE
  17. #include "term.h"
  18.  
  19. #include <ctype.h>
  20.  
  21. #ifdef USE_TERMIO
  22.  
  23. #ifndef ICANON
  24. #include <termio.h>
  25. #endif
  26.  
  27. #undef USE_SUSPEND
  28. #ifdef VSUSP
  29. #define USE_SUSPEND
  30. #endif
  31.  
  32. #define USE_INTERRUPT
  33.  
  34. #ifdef TCSETS
  35. #define USE_TERMIOS
  36. #else
  37. #undef USE_SUSPEND
  38. #endif
  39.  
  40. #else /* USE_TERMIO */
  41. #ifdef USE_SGTTY
  42.  
  43. #ifndef CBREAK
  44. #include <sgtty.h>
  45. #endif
  46.  
  47. #undef USE_INTERRUPT
  48. #ifdef TIOCGETC
  49. #define USE_INTERRUPT
  50. #endif
  51.  
  52. #undef USE_SUSPEND
  53. #ifdef TIOCGLTC
  54. #define USE_SUSPEND
  55. #endif
  56.  
  57. #undef USE_NEWBSDTTY
  58. #ifdef TIOCSETD
  59. #ifdef NTTYDISC
  60. #define USE_NEWBSDTTY
  61. #endif
  62. #endif
  63.  
  64. #else /* USE_SGTTY */
  65.  
  66. #undef USE_SUSPEND
  67. #define USE_NOTTY
  68.  
  69. #endif /* else USE_SGTTY */
  70. #endif /* else USE_TERMIO */
  71.  
  72. #ifndef key_interrupt_char
  73. #undef USE_INTERRUPT
  74. #endif
  75.  
  76. #ifndef key_suspend_char
  77. #undef USE_SUSPEND
  78. #endif
  79.  
  80. char *term;
  81. int asked = 0;
  82. long baudrate;
  83.  
  84. #ifndef USE_STDLIB
  85. #ifdef USE_PROTOTYPES
  86. long atol(char const *);
  87. #else
  88. long atol();
  89. #endif
  90. #endif
  91.  
  92. #define OUTPUT_TERM    1
  93. #define OUTPUT_SHELL    2
  94. #define OUTPUT_TERMCAP    3
  95.  
  96. #define PUTS(s)        tputs(s, 1, putch)
  97. #define PUTCHAR(c)    putc(c, stderr)
  98. #define FLUSH        fflush(stderr)
  99.  
  100. #ifdef USE_SMALLMEM
  101. extern unsigned short _baud_tbl[];
  102. #else
  103. extern long _baud_tbl[];
  104. #endif
  105.  
  106. extern void (*cleanup)();
  107.  
  108. static void
  109. clean(e)
  110. int e; {
  111.     return;
  112. }
  113.  
  114. static int
  115. putch(c)
  116. int c; {
  117.     return putc(c, stderr);
  118. }
  119.  
  120. void
  121. set_term(name, query)
  122. char *name;
  123. int query; {
  124.     static char buf[MAX_LINE];
  125.     char *s, *t;
  126.  
  127.     if (query) {
  128.         fprintf(stderr, "TERM = (%s) ", name);
  129.         fflush(stderr);
  130.         asked = 1;
  131.         if (gets(buf) != NULL) {
  132.             s = buf;
  133.             while(*s != '\0' && isspace(*s))
  134.                 s++;
  135.             t = s;
  136.             while(*s != '\0' && !isspace(*s))
  137.                 s++;
  138.             *s = '\0';
  139.             if (*t != '\0') {
  140.                 term = t;
  141.                 return;
  142.             }
  143.         }
  144.     }
  145.     term = strcpy(buf, name);
  146. }
  147.  
  148. int
  149. map_term(map, ask)
  150. char *map;
  151. int ask; {
  152.     char *type;
  153.     char test = 0;
  154.     char *baud = NULL;
  155.  
  156.     type = map;
  157. #ifndef USE_NOTTY
  158.     while(*map && strchr(">@<!:?", *map) == NULL)
  159.         map++;
  160.     if (*map != '\0' && *map != ':' && *map != '?') {
  161.         if (*map == '!') {
  162.             switch(*++map) {
  163.             case '>': test = 'g'; break;
  164.             case '@': test = 'n'; break;
  165.             case '<': test = 'l'; break;
  166.             default:
  167.                 quit(-1, "bad argument to 'm' switch");
  168.             }
  169.         } else
  170.             test = *map;
  171.         *map++ = '\0';
  172.         baud = map;
  173.         while(*map && *map != ':')
  174.             map++;
  175.     }
  176.  
  177. #else
  178.     while(*map && *map != ':' && *map != '?')
  179.         map++;
  180. #endif
  181.  
  182.     if (*map == ':')
  183.         *map++ = '\0';
  184.     if (*map == '?') {
  185.         ask = 1;
  186.         *map++ = '\0';
  187.     }
  188.     if (*map == '\0') 
  189.         quit(-1, "bad argument to 'm' switch");
  190.  
  191.     if (type[0] != '\0' && strcmp(type, term) != 0) {
  192.         return 0;
  193.     }
  194.  
  195. #ifndef USE_NOTTY
  196.     switch(test) {
  197.     case '>':
  198.         if (baudrate <= atol(baud))
  199.             return 0;
  200.         break;
  201.     case '<':
  202.         if (baudrate >= atol(baud))
  203.             return 0;
  204.         break;
  205.     case '@':
  206.         if (baudrate != atol(baud))
  207.             return 0;
  208.         break;
  209.     case 'l':
  210.         if (baudrate < atol(baud))
  211.             return 0;
  212.         break;
  213.     case 'g':
  214.         if (baudrate > atol(baud))
  215.             return 0;
  216.         break;
  217.     case 'n':
  218.         if (baudrate == atol(baud))
  219.             return 0;
  220.         break;
  221.     }
  222. #endif
  223.  
  224.     set_term(map, ask);
  225.     return 1;
  226. }
  227.  
  228. int
  229. conv_char(s)
  230. char *s; {
  231.     if (s[0] == '^' && s[1] >= '@' && s[1] <= '\177')
  232.         return s[1] & 31;
  233.     else if (s[0] == '^' && s[1] == '?')
  234.         return '\177';
  235.     else if (s[0] != '\0')
  236.         return s[0];
  237.     else
  238.         return -2;
  239. }
  240.  
  241. char *
  242. expand_char(c)
  243. int c; {
  244.     static char buf[5];
  245.  
  246.     if (c < 0 || c > 127) {
  247.         sprintf(buf, "\\%03o", c & 0177);
  248.     } else if (c == 127) {
  249.         return "DEL";
  250.     } else if (c < 32) {
  251.         buf[0] = '^';
  252.         buf[1] = c + 64;
  253.         buf[2] = '\0';
  254.     } else {
  255.         buf[0] = c;
  256.         buf[1] = '\0';
  257.     }
  258.  
  259.     return buf;
  260. }
  261.  
  262. #define START     1
  263. #define COPY    2
  264. #define ESCAPE    3
  265.  
  266. void
  267. compress_buf(buf)
  268. char *buf; {
  269.     char *s, *d;
  270.     int state = START;
  271.  
  272.     d = s = buf;
  273.  
  274.     while(*s) {
  275.         switch(state) {
  276.         case START:
  277.             if (isspace(*s) || *s == ':') {
  278.                 s++;
  279.                 break;
  280.             }
  281.             state = COPY;
  282.             /* FALLTHROUGH */
  283.         case COPY:
  284.             switch(*s) {
  285.             case '^':
  286.             case '\\':
  287.                 state = ESCAPE;
  288.                 break;
  289.             case ':':
  290.                 state = START;
  291.                 break;
  292.             }
  293.             *d++ = *s++;
  294.             break;
  295.  
  296.         case ESCAPE:
  297.             *d++ = *s++;
  298.             state = COPY;
  299.             break;
  300.         }
  301.     }
  302. }
  303.  
  304. static void
  305. usage(e)
  306. int e; {
  307. #ifdef USE_ANSIC
  308.     fprintf(stderr, "usage: %s [-] [-"
  309. #define ARG(s)    s
  310. #else
  311.     fprintf(stderr, "usage: %s [-] [-", prg_name);
  312. #define ARG(s)    fputs(s, stderr);
  313. #endif
  314.             ARG("l")
  315. #ifndef USE_NOTTY
  316. #ifdef USE_NEWBSDTTY
  317.             ARG("n")
  318. #endif
  319. #endif
  320.             ARG("rsAI")
  321. #ifndef USE_NOTTY
  322.             ARG("Q")
  323. #endif
  324.             ARG("S")
  325. #ifndef USE_NOTTY
  326.             ARG("T")
  327. #endif
  328.             ARG("]")
  329. #ifndef USE_NOTTY
  330.             ARG(" [-e[c]] [-E[c]] [-k[c]]")
  331. #ifdef USE_INTERRUPT
  332.             ARG(" [-i[c]]")
  333. #endif
  334. #ifdef USE_SUSPEND
  335.             ARG(" [-z[c]]")
  336. #endif
  337. #endif
  338.             ARG("\n\t[-m ident")
  339. #ifndef USE_NOTTY
  340.             ARG("[[!][<@>]speed]")
  341. #endif
  342.             ARG(":[?]term] [term]\n")
  343. #ifdef USE_ANSIC
  344.             , prg_name);
  345. #endif
  346.  
  347. #undef ARG
  348.  
  349.     return;
  350. }
  351.  
  352. int
  353. main(argc, argv)
  354. int argc;
  355. char **argv; {
  356.     char *s;
  357.     int i, j, r, c;
  358.     int ask = 0;
  359. #ifndef USE_NOTTY
  360.     int erase_char = -1;
  361.     int erase_if_bs = 0;
  362.     int kill_char = -1;
  363. #ifdef USE_INTERRUPT
  364.     int interrupt_char = -1;
  365. #endif
  366. #ifdef USE_SUSPEND
  367.     int suspend_char = -1;
  368. #endif
  369. #ifdef USE_NEWBSDTTY
  370.     int newbsdtty = 0;
  371. #endif
  372. #endif /* !USE_NOTTY */
  373.     int output_type = 0;
  374.     int no_term_init = 0;
  375.     int term_type_is = 0;
  376.     int no_set_to = 0;
  377.     int reset = 0;
  378.     int matched = 0;
  379.     int expand_tabs = -1;
  380.     int is_csh;
  381. #ifndef USE_NOTTTY
  382. #ifdef USE_TERMIO
  383. #ifdef USE_TERMIOS
  384.     struct termios tty;
  385. #else
  386.     struct termio tty;
  387. #endif
  388. #else
  389. #ifdef USE_SGTTY
  390.     struct sgttyb tty;
  391. #ifdef USE_INTERRUPT
  392.     struct tchars tty2;
  393. #endif
  394. #ifdef USE_SUSPEND
  395.     struct ltchars tty3;
  396. #endif
  397. #endif /* USE_SGTTY */
  398. #endif /* else USE_TERMIO */
  399. #endif /* !USE_NOTTY */
  400.     struct term_path *path;
  401.     char buf[MAX_BUF];
  402.     FILE *f;
  403.     int datatype;
  404.  
  405.     prg_name = argv[0];
  406.     s = strrchr(prg_name, '/');
  407.     if (s != NULL && *++s != '\0') {
  408.         if (strcmp(s, "reset") == 0) {
  409.             reset = 1;
  410.         }
  411.         prg_name = s;
  412.     }
  413.  
  414.     cleanup = clean;
  415.  
  416. #ifndef USE_NOTTY
  417. #ifdef USE_TERMIO
  418. #ifdef USE_TERMIOS
  419.     if (ioctl(2, TCGETS, &tty) == -1)
  420. #else
  421.     if (ioctl(2, TCGETA, &tty) == -1)
  422. #endif
  423.     {
  424.         quit(errno, "ioctl failed");
  425.     }
  426.     baudrate = _baud_tbl[tty.c_cflag & CBAUD];
  427. #else
  428. #ifdef USE_SGTTY
  429.     if (gtty(2, &tty) == -1) {
  430.         quit(errno, "gtty failed");
  431.     }
  432.     baudrate = _baud_tbl[tty.sg_ospeed];
  433. #ifdef USE_INTERRUPT
  434.     if (ioctl(2, TIOCGETC, &tty2) == -1) {
  435.         quit(errno, "ioctl failed");
  436.     }
  437. #endif
  438. #ifdef USE_SUSPEND
  439.     if (ioctl(2, TIOCGLTC, &tty3) == -1) {
  440.         quit(errno, "ioctl failed");
  441.     }
  442. #endif
  443. #endif /* USE_SGTTY */
  444. #endif /* else USE_TERMIO */
  445. #endif /* !USE_NOTTY */
  446.  
  447.     term = getenv("TERM");
  448.  
  449.     cleanup = usage;
  450.  
  451.     for(i = 1; i < argc; i++) {
  452.         if (argv[i][0] != '-') {
  453.             if (term == NULL) {
  454.                 term = argv[i];
  455.             }
  456.             continue;
  457.         }
  458.         s = argv[i] + 1;
  459.         if (*s == '\0') {
  460.             output_type = OUTPUT_TERM;
  461.             continue;
  462.         }
  463.         while (*s != '\0') {
  464.             switch(*s++) {
  465.             case 'A':
  466.                 ask = 1;
  467.                 continue;
  468. #ifndef USE_NOTTY
  469.             case 'E':
  470.                 erase_if_bs = 1;
  471.                 /* FALLTHROUGH */
  472.             case 'e':
  473.                 erase_char = conv_char(s);
  474.                 break;
  475.             case 'k':
  476.                 kill_char = conv_char(s);
  477.                 break;
  478. #ifdef USE_INTERRUPT
  479.             case 'i':
  480.                 interrupt_char = conv_char(s);
  481.                 break;
  482. #endif
  483. #ifdef USE_SUSPEND
  484.             case 'z':
  485.                 suspend_char = conv_char(s);
  486.                 break;
  487. #endif
  488.             case 'T':
  489.                 erase_char = kill_char
  490. #ifdef USE_INTERRUPT
  491.                     = interrupt_char
  492. #endif
  493. #ifdef USE_SUSPEND
  494.                     = suspend_char
  495. #endif
  496.                     = -2;
  497.                 continue;
  498. #ifdef USE_NEWBSDTTY
  499.             case 'n':
  500.                 newbsdtty = 1;
  501.                 continue;
  502. #endif
  503.             case 'Q':
  504.                 no_set_to = 1;
  505.                 continue;
  506. #endif /* !USE_NOTTY */
  507.             case 'l':
  508.             case 'I':
  509.                 no_term_init = 1;
  510.                 continue;
  511.             case 'r':
  512.                 term_type_is = 1;
  513.                 continue;
  514.             case 's':
  515.                 output_type = OUTPUT_SHELL;
  516.                 continue;
  517.             case 'S':
  518.                 output_type = OUTPUT_TERMCAP;
  519.                 continue;
  520.             case 'm':
  521.                 if (*s == '\0') {
  522.                     if (i == argc - 1) {
  523.                         quit(-1, "'m' switch requires an argument.");
  524.                     }
  525.                     s = argv[++i];
  526.                 }
  527.                 if (!matched) {
  528.                     matched = map_term(s, ask);
  529.                 }
  530.                 break;
  531.             default:
  532.                 quit(-1, "unknown switch '%c'", s[-1]);
  533.                 break;
  534.             }
  535.             break;
  536.         }
  537.     }
  538.     
  539.     cleanup = clean;
  540.  
  541.     path = _buildpath("$MYTERMINFO", 2,
  542.               "$TERMINFO", 2,
  543.               "$TERMCAP", 1,
  544. #ifdef TERMINFODIR
  545.               TERMINFODIR, 0,
  546. #endif
  547. #ifdef TERMCAPFILE
  548.               TERMCAPFILE, 0,
  549. #endif
  550. #ifdef TERMINFOSRC
  551.               TERMINFOSRC, 0,
  552. #endif
  553.               NULL, -1);
  554.  
  555.     if (path == NULL) {
  556.         quit(-1, "malloc error");
  557.     }
  558.  
  559.     do {
  560.         if (term == NULL) {
  561.             term = "unknown";
  562.         }
  563.  
  564.         if (ask && !asked) {
  565.             set_term(term, 1);
  566.         }
  567.  
  568.         datatype = _fillterm(term, path, buf);
  569.  
  570.         switch(datatype) {
  571.         case -3:
  572.             quit(-1, "malloc error");
  573.             /* NOTREACHED */
  574.         case -2:
  575.             quit(-1, "database in bad format");
  576.             /* NOTREACHED */
  577.         case -1:
  578.             /* quit(-1, "database not found"); */
  579.         case 0:
  580.             if (ask || asked) {
  581.                 fprintf(stderr, "terminal type '%s' unknown.\n",
  582.                     term);
  583.                 term = NULL;
  584.                 ask = 1;
  585.                 asked = 0;
  586.             } else {
  587.                 quit(-1, "terminal type '%s' unknown.\n", term);
  588.             }
  589.             break;
  590.         case 1:
  591.         case 2:
  592.         case 3:
  593.             break;
  594.         default:
  595.             quit(-1, "oops...");
  596.         }
  597.     } while(term == NULL);
  598.  
  599.     _delpath(path);
  600.  
  601.     cur_term->baudrate = baudrate;
  602.  
  603.     if (!no_term_init) {
  604.  
  605.         if (init_prog != NULL) {
  606.             system(init_prog);
  607.         }
  608.         FLUSH;
  609.  
  610.         if (reset && reset_1string != NULL) {
  611.             PUTS(reset_1string);
  612.         } else if (init_1string != NULL) {
  613.             PUTS(init_1string);
  614.         }
  615.         FLUSH;
  616.  
  617.         if (reset && reset_2string != NULL) {
  618.             PUTS(reset_2string);
  619.         } else if (init_2string != NULL) {
  620.             PUTS(init_2string);
  621.         }
  622.         FLUSH;
  623.  
  624.         if (set_lr_margin != NULL) {
  625.             PUTS(tparm(set_lr_margin, 0, columns - 1));
  626.         } else if (set_left_margin_parm != NULL
  627.                && set_right_margin_parm != NULL) {
  628.             PUTS(tparm(set_left_margin_parm, 0));
  629.             PUTS(tparm(set_right_margin_parm, columns - 1));
  630.         } else if (clear_margins != NULL && set_left_margin != NULL
  631.                && set_right_margin != NULL) {
  632.             PUTS(clear_margins);
  633.             if (carriage_return != NULL) {
  634.                 PUTS(carriage_return);
  635.             } else {
  636.                 PUTCHAR('\r');
  637.             }
  638.             PUTS(set_left_margin);
  639.             if (parm_right_cursor) {
  640.                 PUTS(tparm(parm_right_cursor, columns - 1));
  641.             } else {
  642.                 for(i = 0; i < columns - 1; i++) {
  643.                     PUTCHAR(' ');
  644.                 }
  645.             }
  646.             PUTS(set_right_margin);
  647.             if (carriage_return != NULL) {
  648.                 PUTS(carriage_return);
  649.             } else {
  650.                 PUTCHAR('\r');
  651.             }
  652.         }
  653.         FLUSH;
  654.  
  655.         if (init_tabs != 8) {
  656.             if (clear_all_tabs != NULL && set_tab != NULL) {
  657.                 if (carriage_return != NULL) {
  658.                     PUTS(carriage_return);
  659.                 } else {
  660.                     PUTCHAR('\r');
  661.                 }
  662.                 PUTS(clear_all_tabs);
  663.                 PUTS(set_tab);
  664.                 for(i = 8; i < columns - 1; i += 8) {
  665.                     if (parm_right_cursor) {
  666.                         PUTS(tparm(parm_right_cursor,
  667.                              8));
  668.                     } else {
  669.                         for(j = 0; j < 8; j++) {
  670.                             PUTCHAR(' ');
  671.                         }
  672.                     }
  673.                     PUTS(set_tab);
  674.                 }
  675.                 if (carriage_return != NULL) {
  676.                     PUTS(carriage_return);
  677.                 } else {
  678.                     PUTCHAR('\r');
  679.                 }
  680.                 expand_tabs = 0;
  681.                 FLUSH;
  682.             } else {
  683.                 expand_tabs = 1;
  684.             }
  685.         } else {
  686.             expand_tabs = 0;
  687.         }
  688.  
  689.         if (reset && reset_file != NULL) {
  690.             f = fopen(reset_file, "r");
  691.             if (f == NULL) {
  692.                 quit(errno, "Can't open reset_file: '%s'",
  693.                      reset_file);
  694.             }
  695.             while((c = fgetc(f)) != EOF) {
  696.                 PUTCHAR(c);
  697.             }
  698.             fclose(f);
  699.         } else if (init_file != NULL) {
  700.             f = fopen(init_file, "r");
  701.             if (f == NULL) {
  702.                 quit(errno, "Can't open init_file: '%s'",
  703.                      init_file);
  704.             }
  705.             while((c = fgetc(f)) != EOF) {
  706.                 PUTCHAR(c);
  707.             }
  708.             fclose(f);
  709.         }
  710.         FLUSH;
  711.  
  712.         if (reset && reset_3string != NULL) {
  713.             PUTS(reset_3string);
  714.         } else if (init_2string != NULL) {
  715.             PUTS(init_3string);
  716.         }
  717.         FLUSH;
  718.     }
  719.  
  720.     if (term_type_is) {
  721.         fprintf(stderr, "Terminal type is %s\n", term);
  722.     }
  723.  
  724. #ifndef USE_NOTTY
  725. #ifdef USE_TERMIO
  726.  
  727.     if (expand_tabs == 0 && (tty.c_cflag & TABDLY) == TAB3) {
  728.         tty.c_cflag = tty.c_cflag & ~TABDLY;
  729.     } else if (expand_tabs == 1) {
  730.         tty.c_cflag = (tty.c_cflag & ~TABDLY) | TAB3;
  731.     }
  732.  
  733. #define SETTO(v, m, t, c) (c != tty.c_cc[v] ? (tty.c_cc[v] = c,          \
  734.                !no_set_to ? fprintf(stderr, "%s set to %s\n", t, \
  735.                          expand_char(c)) : 0) : 0)
  736.  
  737. #endif
  738. #ifdef USE_SGTTY
  739.     
  740. #ifdef USE_NEWBSDTTY
  741.     if (newbsdtty) {
  742.         if (ioctl(2, TIOCSETD, NTTYDISC) == -1) {
  743.             quit(errno, "Can't switch tty to new line disc.");
  744.         }
  745.     }
  746. #endif
  747.  
  748.     if (expand_tabs == 0) {
  749.         tty.sg_flags &= ~XTABS;
  750.     } else if (expand_tabs == 1) {
  751.         tty.sg_flags |= XTABS;
  752.     }
  753.  
  754. #define SETTO(v, m, t, c) (c != m ? (m = c,                  \
  755.                !no_set_to ? fprintf(stderr, "%s set to %s\n", t, \
  756.                          expand_char(c)) : 0) : 0)
  757. #endif
  758.  
  759.     if (erase_char != -1
  760.             && (!erase_if_bs || backspaces_with_bs
  761.             || (cursor_left == NULL && cursor_left[0] != '\b'))) {
  762.         if (erase_char != -2) {
  763.             c = erase_char;
  764.         } else if (cursor_left == NULL || cursor_left[0] == '\0'
  765.              || cursor_left[1] != '\0') {
  766.             c = '\b';
  767.         } else {
  768.             c = cursor_left[0];
  769.         }
  770.         SETTO(VERASE, tty.sg_erase, "Erase", c);
  771.     }
  772.  
  773.     if (kill_char != -1) {
  774.         if (kill_char != -2) {
  775.             c = kill_char;
  776.         } else if (key_kill_char == NULL || key_kill_char[0] == '\0'
  777.                || key_kill_char[1] != '\0') {
  778.             c = '\025';
  779.         } else {
  780.             c = key_kill_char[0];
  781.         }
  782.         SETTO(VKILL, tty.sg_kill, "Kill", c);
  783.     }
  784.  
  785.     
  786. #ifdef USE_INTERRUPT
  787.     if (interrupt_char != -1) {
  788.         if (interrupt_char != -2) {
  789.             c = interrupt_char;
  790.         } else if (key_interrupt_char == NULL
  791.                || key_interrupt_char[0] == '\0'
  792.                || key_interrupt_char[1] != '\0') {
  793.             c = '\177';
  794.         } else {
  795.             c = key_interrupt_char[0];
  796.         }
  797.         SETTO(VINTR, tty2.t_intrc, "Interrupt", c);
  798.     }
  799. #endif
  800.  
  801. #ifdef USE_SUSPEND
  802.     if (suspend_char != -1) {
  803.         if (suspend_char != -2) {
  804.             c = suspend_char;
  805.         } else if (key_suspend_char == NULL
  806.                || key_suspend_char[0] == '\0'
  807.                || key_suspend_char[1] != '\0') {
  808.             c = '\032';
  809.         } else {
  810.             c = key_suspend_char[0];
  811.         }
  812.         SETTO(VSUSP, tty3.t_suspc, "Suspend", c);
  813.     }
  814. #endif
  815.  
  816. #ifdef USE_TERMIO
  817. #ifdef USE_TERMIOS
  818.     if (ioctl(2, TCSETS, &tty) == -1)
  819. #else
  820.     if (ioctl(2, TCSETA, &tty) == -1)
  821. #endif
  822.     {
  823.         quit(errno, "ioctl failed");
  824.     }
  825. #else
  826. #ifdef USE_SGTTY
  827.     if (stty(2, &tty) == -1) {
  828.         quit(errno, "stty failed");
  829.     }
  830. #ifdef USE_INTERRUPT
  831.     if (ioctl(2, TIOCSETC, &tty2) == -1) {
  832.         quit(errno, "ioctl failed");
  833.     }
  834. #endif
  835. #ifdef USE_SUSPEND
  836.     if (ioctl(2, TIOCSLTC, &tty3) == -1) {
  837.         quit(errno, "ioctl failed");
  838.     }
  839. #endif
  840. #endif /* USE_SGTTY */
  841. #endif /* else USE_TERMIO */
  842. #endif /* !USE_NOTTY */
  843.  
  844.     s = getenv("SHELL");
  845.     r = strlen(s);
  846.  
  847.     if (r >= 3 && strcmp("csh", s + r - 3) == 0) {
  848.         is_csh = 1;
  849.     } else {
  850.         is_csh = 0;
  851.     }
  852.  
  853.     switch(output_type) {
  854.     case OUTPUT_TERM:
  855.         fprintf(stdout, "%s\n", term);
  856.         break;
  857.  
  858.     case OUTPUT_TERMCAP:
  859.         if (is_csh) {
  860.             if (datatype == 1) {
  861.                 compress_buf(buf);
  862.                 fprintf(stdout, "%s %s", term, buf);
  863.             } else {
  864.                 s = getenv("TERMCAP");
  865.                 if (s == NULL || *s == '\0') {
  866.                     s = ":";
  867.                 }
  868.                 fprintf(stdout, "%s %s", term, s);
  869.             }
  870.             break;
  871.         }
  872.         /* FALLTHROUGH */
  873.     case OUTPUT_SHELL:
  874.         if (is_csh) {
  875.             fprintf(stdout, "set noglob;\n");
  876.             fprintf(stdout, "setenv TERM '%s';\n", term);
  877.             if (datatype == 1) {
  878.                 compress_buf(buf);
  879.                 fprintf(stdout, "setenv TERMCAP '%s';\n", buf);
  880.             }
  881.             fprintf(stdout, "unset noglob;\n");
  882.         } else {
  883.             fprintf(stdout, "export TERM TERMCAP;\n");
  884.             fprintf(stdout, "TERM='%s';\n", term);
  885.             if (datatype == 1) {
  886.                 compress_buf(buf);
  887.                 fprintf(stdout, "TERMCAP='%s';\n", buf);
  888.             }
  889.         }
  890.         break;
  891.     }
  892.     
  893.     return 0;
  894. }
  895.