home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / se / part6 / libchangetty / changetty.c next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  5.3 KB  |  227 lines

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