home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / vn.jan.88 / part03 / tty_set.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-30  |  2.5 KB  |  134 lines

  1. /*
  2. ** vn news reader.
  3. **
  4. ** tty_set.c - interface to ioctl (system tty interface)
  5. **
  6. ** see copyright disclaimer / history in vn.c source file
  7. */
  8.  
  9. #ifdef SYSV
  10. #include <termio.h>
  11. #else
  12. #include <sgtty.h>
  13. #endif
  14.  
  15. #include "tty.h"
  16.  
  17. extern char Erasekey,Killkey;
  18.  
  19. #ifdef SYSV
  20. static struct termio C_tp, O_tp;
  21. #else
  22. static struct sgttyb C_tp;
  23. static unsigned short O_lflag;
  24. #endif
  25.  
  26. static unsigned S_flag=0;
  27. static int R_ignore=0;        /* up/down counter of reset calls to ignore */
  28.  
  29. #define IO_GOT 1    /* have polled for original terminal mode */
  30. #define IO_RAW 2    /* in RAW (CBREAK actually) mode */
  31.  
  32. /*
  33.     tty_set handles ioctl calls.  SAVEMODE, RESTORE are used around
  34.     system calls and interrupts to assure cooked mode, and restore
  35.     raw if raw on SAVEMODE.  The pair results in no calls to ioctl
  36.     if we are cooked already when SAVEMODE is called, and may be nested,
  37.     provided we desire no "restore" of cooked mode after restoring raw.
  38.  
  39.     When we get the original terminal mode, we also save erase and kill.
  40.  
  41.     sig_set makes an ioctl call to get process group leader.  Otherwise
  42.     ioctl calls should come through here.
  43. */
  44. tty_set(cmd)
  45. int cmd;
  46. {
  47.     int rc;
  48.     unsigned mask;
  49.  
  50.     switch (cmd)
  51.     {
  52.     case BACKSTOP:
  53. #ifdef JOBCONTROL
  54.         if ((rc = ioctl(1,TIOCLGET,&mask)) != 0)
  55.             break;
  56.         mask |= LTOSTOP;
  57.         rc = ioctl(1,TIOCLSET,&mask);
  58. #else
  59.         rc = 0;
  60. #endif
  61.         break;
  62.     case RAWMODE:
  63.         if ((S_flag & IO_RAW) != 0)
  64.         {
  65.             rc = 0;
  66.             break;
  67.         }
  68.         if ((S_flag & IO_GOT) == 0)
  69.         {
  70.             /* Save original modes, get erase / kill */
  71. #ifdef SYSV
  72.             rc = ioctl(0,TCGETA,&C_tp);
  73.             O_tp = C_tp;
  74.             Erasekey = C_tp.c_cc[VERASE];
  75.             Killkey = C_tp.c_cc[VKILL];
  76. #else
  77.             rc = ioctl(0,TIOCGETP,&C_tp);
  78.             O_lflag = C_tp.sg_flags;
  79.             Erasekey = C_tp.sg_erase;
  80.             Killkey = C_tp.sg_kill;
  81. #endif
  82.         }
  83. #ifdef SYSV
  84.         C_tp.c_lflag &= ~(ECHO | ICANON);
  85.         C_tp.c_cc[VMIN] = 1;
  86.         rc = ioctl(0,TCSETAW,&C_tp);
  87. #else
  88.         C_tp.sg_flags |= CBREAK;
  89.         C_tp.sg_flags &= ~ECHO;
  90.         rc = ioctl(0,TIOCSETP,&C_tp);
  91. #endif
  92.         S_flag = IO_GOT|IO_RAW;
  93.         break;
  94.     case COOKED:
  95.         if ((S_flag & IO_RAW) != 0)
  96.         {
  97. #ifdef SYSV
  98.             C_tp = O_tp;
  99.             rc = ioctl(0,TCSETAW,&C_tp);
  100. #else
  101.             C_tp.sg_flags = O_lflag;
  102.             rc = ioctl(0,TIOCSETP,&C_tp);
  103. #endif
  104.             S_flag &= ~IO_RAW;
  105.         }
  106.         else
  107.             rc = 0;
  108.         break;
  109.     case SAVEMODE:
  110.         if ((S_flag & IO_RAW) != 0)
  111.         {
  112.             tty_set(COOKED);
  113.             R_ignore = 0;
  114.         }
  115.         else
  116.             ++R_ignore;
  117.         rc = 0;
  118.         break;
  119.     case RESTORE:
  120.         if (R_ignore <= 0)
  121.         {
  122.             tty_set(RAWMODE);
  123.         }
  124.         else
  125.             --R_ignore;
  126.         rc = 0;
  127.         break;
  128.     default:
  129.         rc = -1;
  130.     }
  131.     if (rc < 0)
  132.         printex ("ioctl failure, tty_set: %d",cmd);
  133. }
  134.