home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / geterm.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  9KB  |  305 lines

  1. #ifdef VCSTR
  2.  static char GETERMC[]="@(#) geterm.c 1.6 91/02/15 12:56:55";  /*sccsid*/
  3. #endif
  4.  
  5. #ifdef DOCUMENTATION
  6.  ******************************* DOCZ Header *********************************
  7. .MODULE                         geterm
  8. .LIBRARY                        toolz
  9. .TYPE                           program
  10. .SYSTEM                         unix
  11. .AUTHOR                         Todd Merriman
  12. .LANGUAGE                       C
  13. .APPLICATION            terminal
  14. .DESCRIPTION            
  15.         Determine the terminal attached
  16. .ARGUMENTS                      
  17.         geterm
  18. .NARRATIVE                      
  19.         The geterm utility queries the terminal on standard input by commanding
  20.         the answerback sequence.  The following may be passed to standard output:
  21.  
  22.         vt320
  23.         vt220
  24.         vt100
  25.         vt102
  26.         z29
  27.         vt52
  28.         wy50
  29.         h19     
  30.         avt-4p-s
  31.         avt-8p-s
  32.         la120   
  33.         cit101e 
  34.         xt100+  
  35.         vt125   
  36.         vt200-sb
  37.         f220    
  38.         tvi9220 
  39.  
  40.         An empty line will be output if the terminal cannt be identified.
  41. .RETURNS                        
  42.         0 if the answerback is valid, 1 if not.
  43. .CAUTIONS
  44.         The answerback delay is currently only 3 seconds.
  45. .REVISIONS              9/20/90
  46.         Add wy50
  47. .REVISION               9/24/90
  48.         Added more terminals from similar Usenet program
  49. .REVISION               2/15/91
  50.         Strip 8th bit on characters returned from terminal
  51. .EXAMPLE
  52.         if [ "`tty`" != "/dev/console" ] && [ "`tty|cut -c1-7`" != "/dev/vt" ]
  53.         then
  54.                 TERM=`/u/geterm`
  55.                 if [ "$TERM" = "vt102" ]
  56.                 then
  57.                         TERM=vt220
  58.                 fi
  59.  
  60.                 if [ "x$TERM" = "x" ]
  61.                 then
  62.                         TERM=nansipc
  63.                 else
  64.                         if [ "$TERM" = "vt220" ]
  65.                         then
  66.                                 stty erase \^?
  67.                         fi
  68.                 fi
  69.         fi
  70.         export TERM
  71.         echo $LOGNAME has a `tput longname`
  72. .ENDOC                          END DOCUMENTATION
  73.  *****************************************************************************
  74. #endif  /* DOCUMENTATION */
  75.  
  76. #include <stdio.h>
  77. #include <ctype.h>
  78. #include <termio.h>
  79. #include <setjmp.h>
  80. #include <signal.h>
  81. #ifndef unix
  82. #include <stdlib.h>
  83. #endif
  84. #ifndef VMS
  85. #include <fcntl.h>
  86. #endif
  87.  
  88. #define SEC_WAIT        3               /* no. seconds to wait for response */
  89.  
  90. static char *escseq[] =
  91. {
  92.         "\033i0",
  93.         "\033 ",                                /* Wyse 50 */
  94.         "\033[c",
  95.         "\033Z",
  96.         NULL
  97. };
  98.  
  99. static struct
  100. {
  101.     char    ansseq [32],            /* answerback response */
  102.     termname [32];  /* terminal name in terminfo */
  103. } termtbl [] =
  104. {
  105.     {"\033[?65;","vt520"},
  106.     {"\033[?64;","vt420"},
  107.     {"\033[?63;","vt320"},
  108.     {"\033[?62;","vt220"},
  109.     {"\033[?1;","vt100"},
  110.     {"\033[?6","vt102"},
  111.     {"\033iB0","z29"},
  112.     {"\033K","vt52"},
  113.     {"50","wy50"},
  114.     {"60","wy60"},
  115.     {"30","wy30"},
  116.     {"/K","h19"},                                                                           /* Zenith z19 */
  117.     {"\033[?1;0c"},{"vt100"},                                               /* Base vt100 */
  118.     {"\033[?1;1c"},{"vt100"},                                               /* vt100 with STP */
  119.     {"\033[?1;2c"},{"vt100"},                                               /* ANSI/VT100 Clone */
  120.     {"\033[?1;3c"},{"vt100"},                                               /* vt100 with AVO and STP */
  121.     {"\033[?1;4c"},{"vt100"},                                               /* vt100 with GPO */
  122.     {"\033[?1;5c"},{"vt100"},                                               /* vt100 with GPO and STP */
  123.     {"\033[?1;6c"},{"vt100"},                                               /* vt100 with GPO and AVO */
  124.     {"\033[?1;7c"},{"vt100"},                                               /* vt100 with GPO, STP, and AVO */
  125.     {"\033[?6c"},{"vt102"},                                                 /* vt102 or MS-Kermit */
  126.     {"\033[?8c"},{"vt100"},                                                 /* TeleVideo 970 */
  127.     {"\033[0n"},{"vt100"},                                                  /* AT&T Unix PC 7300 */
  128.     {"\033[?l;0c"},{"vt100"},                                               /* AT&T Unix PC 7300 */
  129.     {"\033[?12c"},{"vt100"},                                                /* Concept from Pro 350/UNIX */
  130.     {"\033[?;c"},{"vt100"},                                                 /* Concept From Pro 350/UNIX */
  131.     {"\033[=1;1c"},{"avt-4p-s"},                                    /* Concept with 4 pages memory */
  132.     {"\033[=1;2c"},{"avt-8p-s"},                                    /* Concept with 8 pages memory */
  133.     {"\033/Z"},{"vt52"},                                                            /* Generic vt52 */
  134.     {"\033[?10c"},{"la120"},                                                /* DEC Writer III */
  135.     {"\033[?1;11c"},{"cit101e"},                                    /* CIE CIT-101 Enhanced w/Graphics */
  136.     {"\033[?12;7;0;102c"},{"vt125"},                                /* DEC Pro 350 in vt125 mode */
  137.     {"\033[?62;1;2;6;7;8;9c"},{"vt220"},            /* DEC VT220 */
  138.     {"\033[?62;1;4;6;7;8;9;15c"},{"vt200-sb"},/* Microvax II VMS */
  139.     {"\033[62;1;2;6;8c"},{"f220"},                          /* Freedom 220 DEC clone */
  140.     {"\033[?63;1;2;6;7;8c"},{"tvi9220"},            /* TeleVideo 9220 */
  141.     {NULL,NULL}
  142. };    
  143.  
  144. int     Odev;
  145. static void con__tim();
  146. void strascii();
  147. void termsetr();
  148.  
  149. /*****************************************************************************
  150.         Main entry
  151. *****************************************************************************/
  152. main(argc,argv)
  153.     int argc;
  154.     char *argv[];
  155. {
  156.     char    buff [256];
  157.     int     u,
  158.     ix = 0,
  159.     iy;
  160.  
  161.     if ((Odev = open("/dev/tty",O_RDWR | O_NDELAY)) != EOF)
  162.     {
  163.     while (escseq[ix])
  164.     {
  165.         if (write(Odev,escseq[ix],strlen(escseq[ix])) == -1)
  166.         exit(1);
  167.  
  168.         coninstr(buff,SEC_WAIT,0);
  169.         strascii(buff);
  170.  
  171.         if (*buff)
  172.         {
  173. #ifdef TESTING
  174.         u = 0;
  175.         while (buff[u])
  176.             printf("%02X ",buff[u++]);
  177.         line(1);
  178.  
  179. #endif
  180.         if (*buff)
  181.         {
  182.             iy = 0;
  183.             while (termtbl[iy].ansseq)
  184.             {
  185.             if (strncmp(termtbl[iy].ansseq,buff,
  186.                      strlen(termtbl[iy].ansseq)) == 0)
  187.             {
  188.                 puts(termtbl[iy].termname);
  189.                 exit(0);
  190.             }
  191.             ++iy;
  192.             }
  193.         }
  194.         }
  195.         ++ix;
  196.     }
  197.     close(Odev);
  198.     }
  199. #ifdef TESTING
  200.     puts("not found");
  201. #endif
  202.  
  203.     puts("");
  204.     exit(1);
  205. }           /* end of main */
  206.  
  207.  
  208. /*****************************************************************************
  209.         coninstr
  210. *****************************************************************************/
  211. coninstr(str,timo,echoflg)
  212.     char    *str;   /* (w) the input string */
  213.     int     timo,   /* (r) timeout value in seconds or 0 */
  214.     echoflg; /* (r) TRUE to echo input */
  215. {
  216.     int n = 0;
  217.     register ix;
  218.  
  219.     termsetr(0);
  220.     signal(SIGALRM,con__tim);                                 /* Timed read, so set up timer */
  221.     alarm(timo);
  222.     ix = 0;
  223.  
  224.     while ((n = read(0, &str[ix], 1)) > 0)
  225.     {
  226.     if (echoflg)
  227.         write (1,&str[ix],1);
  228.     if (str[ix] == '\r')
  229.         break;
  230.     ++ix;
  231.     }
  232.  
  233.     alarm(0);                                                                       /* Stop timing, we got our character */
  234.     signal(SIGALRM,SIG_DFL);
  235.     if (echoflg)
  236.     putchar('\n');
  237.     termsetr(1);
  238.     str[ix] = '\0';
  239.  
  240.     if (n < 0)
  241.     return(-1);
  242.     return 0;
  243. }       /* end coninstr */
  244.  
  245. /*****************************************************************************
  246.         com__tim
  247. *****************************************************************************/
  248. static void con__tim()
  249. {
  250.     return;
  251. } /* end com__tim */
  252.  
  253.  
  254. /*****************************************************************************
  255.         Strascii
  256. *****************************************************************************/
  257. void strascii(p)
  258.     register unsigned char *p;              /* (r/w) the string to convert */
  259. {
  260.  
  261.     while (*p)
  262.     {
  263.     *p &= 0x7F;
  264.     ++p;
  265.     }
  266.                 
  267.     return;
  268. }       /* end strascii */
  269.  
  270.  
  271. /*****************************************************************************
  272.         termsetr
  273. *****************************************************************************/
  274. void termsetr(func)
  275.     int     func;   /* (r) 0=raw, 1=cooked */
  276. {
  277.     static struct termio
  278.     oldmode;
  279.     struct termio
  280.     newmode;
  281.  
  282.     if (func)
  283.     ioctl(0,TCSETA,&oldmode);                 /* reset original mode */
  284.     else
  285.     {
  286.     ioctl(0,TCGETA,&oldmode);                       /* save mode */
  287.     memcpy(&newmode,&oldmode,sizeof(struct termio));
  288.     newmode.c_iflag |= (BRKINT|IGNPAR);
  289.     newmode.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IUCLC|IXON);
  290.     newmode.c_lflag &= ~(ISIG|ICANON|ECHO);
  291.     newmode.c_oflag &= ~(ONLCR|OCRNL|ONLRET);
  292.     newmode.c_cc[4] = 1;
  293.     newmode.c_cc[5] = 1;
  294.     ioctl(0,TCSETA,&newmode);                 /* set raw mode */
  295.     }
  296.  
  297.     return;
  298.  
  299. }       /* end termsetr */
  300.  
  301. /*****************************************************************************
  302.         End geterm.c
  303. *****************************************************************************/
  304.  
  305.