home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / se / part05 / libchangetty / changetty.c next >
Encoding:
C/C++ Source or Header  |  1987-01-25  |  5.4 KB  |  237 lines

  1. #ifndef lint
  2. static char RCSid[] = "$Header: changetty.c,v 1.2 86/07/11 15:23:06 osadr Exp $";
  3. #endif
  4.  
  5. /*
  6.  * $Log:    changetty.c,v $
  7.  * Revision 1.2  86/07/11  15:23:06  osadr
  8.  * Removed Georgia Tech specific code.
  9.  * 
  10.  * Revision 1.1  86/05/06  13:31:06  osadr
  11.  * Initial revision
  12.  * 
  13.  * 
  14.  */
  15.  
  16. /*
  17. ** changetty.c
  18. **
  19. ** Localize in one place all the data and functions
  20. ** needed to change to and from cbreak mode for
  21. ** the se screen editor.
  22. **
  23. ** Only functions available to rest of se are:
  24. ** ttyedit(), ttynormal(), and getspeed().
  25. **
  26. ** If USG is defined, we use the System V TTY driver.
  27. ** Otherwise (the default) we use the Berkeley terminal driver.
  28. **
  29. ** If we are using System V, then the Release 2 version does not
  30. ** need ospeed.  If not release 2, we assume Release 1 that someone
  31. ** have moved the BSD termlib to.
  32. */
  33.  
  34. #include "../ascii.h"
  35. #include "../constdefs.h"
  36.  
  37. #ifdef USG
  38. #include <termio.h>
  39. #else
  40. #include <sgtty.h>
  41. #endif
  42.  
  43. #if defined (BSD) || ! defined (S5R2)
  44. extern short ospeed;        /* from the termlib library */
  45. static int set_ospeed = NO;
  46. #endif
  47.  
  48. #ifdef USG
  49. /* all the info needed for the System V terminal driver */
  50.  
  51. typedef struct termio TTYINFO;    /* S5 control flags */
  52. #else
  53. /* all the info needed for the Berkeley terminal driver */
  54.  
  55. typedef struct junk {        /* terminal information */
  56.     struct sgttyb sgttyb;    /* V7 control flags */
  57.     struct tchars tchars;    /* V7 control characters */
  58.     short local;        /* local mode settings */
  59.     struct ltchars ltchars;    /* local control characters */
  60.     } TTYINFO;
  61. #endif
  62.  
  63. static TTYINFO OldTtyInfo;    /* initial state of terminal */
  64. static TTYINFO NewTtyInfo;    /* modified state for editing */
  65.  
  66. static int first = YES;        /* first time anything called */
  67.  
  68.  
  69. static init()
  70. {
  71.     if (gttyinfo(1, &OldTtyInfo) == -1)    /* get current state */
  72.         error ("couldn't get TTY info from system. get help!\n");
  73.  
  74.     NewTtyInfo = OldTtyInfo;    /* copy it */
  75.     mttyinfo(&NewTtyInfo);        /* change, but don't reset terminal */
  76.     /* really should check the return value here ... */
  77. }
  78.  
  79.  
  80. ttyedit()    /* set the terminal to correct modes for editing */
  81. {
  82.     if (first == YES)
  83.     {
  84.         first = NO;
  85.         init();
  86.     }
  87.  
  88.     sttyinfo(1, &NewTtyInfo);    /* make the change */
  89.     /* really should check the return value here too ... */
  90. }
  91.  
  92. ttynormal()    /* set the terminal to correct modes for normal use */
  93. {
  94.     if (first)
  95.     {
  96.         first = NO;
  97.         init();
  98.     }
  99.  
  100.     sttyinfo(1, &OldTtyInfo);    /* make the change */
  101. }
  102.  
  103. /* getspeed --- find out the terminal speed in characters/second */
  104. /*        this routine only used if terminal types are hardwired */
  105. /*        into se, however, since it will be in an archive, the */
  106. /*        loader won't grab it if it isn't needed. */
  107.  
  108. int getspeed(fd)
  109. int fd;
  110. {
  111.     register int i;
  112.     TTYINFO ttybuf;
  113.     static struct brstruct {
  114.         int unixrate;
  115.         int cps;
  116.         int baudrate;
  117.         } stab[] = {
  118.             B0,    0,    0,
  119.             B50,    5,    50,
  120.             B75,    8,    75,
  121.             B110,    10,    110,
  122.             B134,    14,    134,
  123.             B150,    15,    150,
  124.             B200,    20,    200,
  125.             B300,    30,    300,
  126.             B600,    60,    600,
  127.             B1200,    120,    1200,
  128.             B1800,    180,    1800,
  129.             B2400,    240,    2400,
  130.             B4800,    480,    4800,
  131.             B9600,    960,    9600
  132.         };
  133.  
  134.     if (first)        /* might as well set it here, too */
  135.     {
  136.         first = NO;
  137.         init();
  138.     }
  139.  
  140.     if (gttyinfo(fd, &ttybuf) == -1)
  141.         return 960;
  142.  
  143.     for (i = 0; i < sizeof(stab) / sizeof(struct brstruct); i++)
  144. #ifdef USG
  145.         if (stab[i].unixrate == (ttybuf.c_cflag & 017))
  146. #else
  147.         if (stab[i].unixrate == (ttybuf.sgttyb.sg_ospeed))
  148. #endif
  149.             return stab[i].cps;
  150.  
  151.     return 960;
  152. }
  153.  
  154. /* gttyinfo --- make all necessary calls to obtain terminal status */
  155.  
  156. static int gttyinfo(fd, buf)
  157. int fd;        /* file descriptor of terminal */
  158. TTYINFO *buf;    /* terminal info buffer to be filled in */
  159. {
  160. #ifdef USG
  161.     if (ioctl(fd, TCGETA, buf) < 0)
  162. #else
  163.     if (gtty(fd, &(buf->sgttyb))  < 0
  164.         || ioctl(fd, TIOCGETC, &(buf->tchars)) < 0
  165.         || ioctl(fd, TIOCLGET, &(buf->local)) < 0
  166.         || ioctl(fd, TIOCGLTC, &(buf->ltchars)) < 0)
  167. #endif
  168.     {
  169.         return -1;
  170.     }
  171.  
  172. #if defined (BSD) || ! defined (S5R2)
  173.     if (set_ospeed == NO)
  174.     {
  175.         set_ospeed = YES;
  176. #ifdef BSD
  177.         ospeed = (buf->sgttyb).sg_ospeed;
  178. #else
  179.         ospeed = buf->c_cflag & 017;    /* tty speed, see termio(7) */
  180. #endif
  181.     }
  182. #endif
  183.  
  184.     return 0;
  185. }
  186.  
  187. /* mttyinfo --- modify a TTYINFO structure for interactive operation */
  188.  
  189. static int mttyinfo(buf)
  190. TTYINFO *buf;        /* buffer containing TTYINFO to be modified */
  191. {
  192. #ifdef USG
  193.     buf->c_cc[0] = DLE;    /* interrupt */
  194.     buf->c_cc[1] = -1;    /* ignore quit */
  195.     buf->c_cc[4] = 1;    /* min # chars to read */
  196.     buf->c_cc[5] = 1;    /* min time to wait */
  197.  
  198.     buf->c_iflag &= ~(INLCR|IGNCR|ICRNL);    /* allow CR to come thru */
  199.     buf->c_lflag |= ISIG|NOFLSH;    /* keep these bits */
  200.     buf->c_lflag &= ~(ICANON|XCASE|ECHO|ECHOE|ECHOK|ECHONL);
  201. #else
  202.     static struct tchars newtchars = {DLE, -1, DC1, DC3, EOT, -1};
  203.     static struct ltchars newltchars = {-1, -1, -1, -1, -1, -1};
  204.  
  205.     buf->sgttyb.sg_flags |= (CBREAK);    /* keep these bits */
  206.     buf->sgttyb.sg_flags &= ~(ECHO|CRMOD|LCASE|RAW|ALLDELAY);
  207.     buf->tchars = newtchars;
  208.     /* buf->local |= (LLITOUT);    /* Dan Forsyth says to comment out */
  209.     buf->local &= ~(LCRTBS|LCRTERA|LPRTERA|LTOSTOP|LFLUSHO|LCRTKIL|
  210. #ifndef BSD4_2
  211.                 LINTRUP|
  212. #endif
  213.                 LCTLECH|LPENDIN);
  214.     buf->ltchars = newltchars;
  215. #endif
  216.     return 0;
  217. }
  218.  
  219. /* sttyinfo --- make all necessary calls to set terminal status */
  220.  
  221. static int sttyinfo(fd, buf)
  222. int fd;        /* file descriptor of terminal */
  223. TTYINFO *buf;    /* terminal info buffer */
  224. {
  225. #ifdef USG
  226.     if (ioctl(fd, TCSETAW, buf) < 0)
  227. #else
  228.     if (ioctl(fd, TIOCSETN, &(buf->sgttyb)) < 0
  229.             || ioctl(fd, TIOCSETC, &(buf->tchars)) < 0
  230.             || ioctl(fd, TIOCLSET, &(buf->local)) < 0
  231.             || ioctl(fd, TIOCSLTC, &(buf->ltchars)) < 0)
  232. #endif
  233.         return    -1;
  234.  
  235.     return 0;
  236. }
  237.