home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / progut~1 / stdwin.zoo / vtrm / uxtty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-17  |  4.8 KB  |  253 lines

  1. /* VTRM -- unix-dependent tty twiddling. */
  2.  
  3. #include <stdio.h>
  4. #ifndef TERMIO
  5. #include <sgtty.h>
  6. #else
  7. #include <termio.h>
  8. #endif TERMIO
  9. #include <signal.h>
  10.  
  11. #include "vtrm.h"
  12.  
  13. typedef char *string;
  14. typedef int bool;
  15. #define Yes 1
  16. #define No  0
  17.  
  18. extern short ospeed; /* Defined in vtrm.c, used by termcap's tgoto/tputs. */
  19.  
  20. /* tty modes */
  21. #ifndef TERMIO
  22.  
  23. /* v7/BSD tty control */
  24. static struct sgttyb oldtty, newtty;
  25. #ifdef TIOCSETN
  26. /* Redefine stty to uses TIOCSETN, so type-ahead is not flushed */
  27. #define stty(fd, bp) ioctl(fd, TIOCSETN, (char *) bp)
  28. #endif
  29.  
  30. #ifdef TIOCSLTC /* BSD -- local special chars, must all be turned off */
  31. static struct ltchars oldltchars;
  32. static struct ltchars newltchars= {-1, -1, -1, -1, -1, -1};
  33. #endif TIOCSLTC
  34.  
  35. #ifdef TIOCSETC /* V7 -- standard special chars, some must be turned off too */
  36. static struct tchars oldtchars;
  37. static struct tchars newtchars;
  38. #endif TIOCSETC
  39.  
  40. #else TERMIO
  41.  
  42. /* AT&T tty control */
  43. static struct termio oldtty, newtty;
  44. #define gtty(fd,bp) ioctl(fd, TCGETA, (char *) bp)
  45. #define stty(fd,bp) ioctl(fd, TCSETAW, (char *) bp)
  46.  
  47. #endif TERMIO
  48.  
  49. static bool know_ttys = No;
  50.  
  51. int
  52. setttymode()
  53. {
  54.     if (!know_ttys) {
  55.         if (gtty(0, &oldtty) != 0 || gtty(0, &newtty) != 0)
  56.             return TE_NOTTY;
  57. #ifndef TERMIO
  58.         ospeed = oldtty.sg_ospeed;
  59. #ifdef PWB
  60.         newtty.sg_flags = (newtty.sg_flags & ~ECHO & ~CRMOD & ~XTABS)
  61.                   | RAW;
  62. #else PWB
  63.         newtty.sg_flags = (newtty.sg_flags & ~ECHO & ~CRMOD & ~XTABS)
  64.                   | CBREAK;
  65. #endif PWB
  66. #ifdef TIOCSLTC
  67.     ioctl(0, TIOCGLTC, (char *) &oldltchars);
  68. #endif
  69. #ifdef TIOCSETC
  70.     ioctl(0, TIOCGETC, (char *) &oldtchars);
  71. #endif
  72.  
  73. #else TERMIO
  74.         ospeed= oldtty.c_lflag & CBAUD;
  75.         newtty.c_iflag &= ~ICRNL; /* No CR->NL mapping on input */
  76.         newtty.c_oflag &= ~ONLCR; /* NL doesn't output CR */
  77.         newtty.c_lflag &= ~(ICANON|ECHO); /* No line editing, no echo */
  78.         newtty.c_cc[VMIN]= 3; /* wait for 3 characters */
  79.         newtty.c_cc[VTIME]= 1; /* or 0.1 sec. */
  80.         /* Should set intrc and quitc and some others, too? */
  81. #endif TERMIO
  82.         know_ttys = Yes;
  83.     }
  84.     stty(0, &newtty);
  85. #ifndef TERMIO
  86. #ifdef TIOCSLTC
  87.     ioctl(0, TIOCSLTC, (char *) &newltchars);
  88. #endif TIOCSLTC
  89. #ifdef TIOCSETC
  90.     ioctl(0, TIOCGETC, (char *) &newtchars);
  91.     newtchars.t_intrc= -1;
  92. #ifdef NDEBUG
  93.     newtchars.t_quitc= -1;
  94. #endif
  95.     newtchars.t_eofc= -1;
  96.     newtchars.t_brkc= -1;
  97.     ioctl(0, TIOCSETC, (char *) &newtchars);
  98. #endif TIOCSETC
  99. #endif TERMIO
  100.     return TE_OK;
  101. }
  102.  
  103. resetttymode()
  104. {
  105.     if (know_ttys) {
  106.         stty(0, &oldtty);
  107. #ifndef TERMIO
  108. #ifdef TIOCSLTC
  109.         ioctl(0, TIOCSLTC, (char *) &oldltchars);
  110. #endif TIOCSLTC
  111. #ifdef TIOCSETC
  112.         ioctl(0, TIOCSETC, (char *) &oldtchars);
  113. #endif TIOCSETC
  114. #endif TERMIO
  115.         know_ttys= No;
  116.     }
  117. }
  118.  
  119.  
  120. /*
  121.  * Return the next input character, or -1 if read fails.
  122.  * Only the low 7 bits are returned, so reading in RAW mode is permissible
  123.  * (although CBREAK is preferred if implemented).
  124.  * To avoid having to peek in the input buffer for trmavail, we use the
  125.  * 'read' system call rather than getchar().
  126.  * (The interface allows 8-bit characters to be returned, to accomodate
  127.  * larger character sets!)
  128.  */
  129.  
  130. static int pushback= -1;
  131.  
  132. int
  133. trminput()
  134. {
  135.     char c;
  136.  
  137.     if (pushback >= 0) {
  138.         c= pushback;
  139.         pushback= -1;
  140.         return c;
  141.     }
  142.     if (read(0, &c, 1) <= 0)
  143.         return -1;
  144.     return c & 0177;
  145. }
  146.  
  147. trmpushback(c)
  148.     int c;
  149. {
  150.     pushback= c;
  151. }
  152.  
  153.  
  154. /*
  155.  * See if there's input available from the keyboard.
  156.  * The code to do this is dependent on the type of Unix you have
  157.  * (BSD, System V, ...).
  158.  * Return value: 0 -- no input; 1 -- input; -1 -- unimplementable.
  159.  * Note that each implementation form should first check pushback.
  160.  *
  161.  * TO DO:
  162.  *    - Implement it for other than 4.x BSD! (notably System 5)
  163.  */
  164.  
  165. #ifdef SELECT
  166.  
  167. #include <sys/time.h>
  168.  
  169. int
  170. trmavail()
  171. {
  172.     int nfound, nfds, readfds;
  173.     static struct timeval timeout= {0, 0};
  174.  
  175.     if (pushback >= 0)
  176.         return 1;
  177.     readfds= 1 << 0;
  178.     nfds= 0+1;
  179.     nfound= select(nfds, &readfds, (int*) NIL, (int*) NIL, &timeout);
  180.     return nfound > 0;
  181. }
  182.  
  183. #define TRMAVAIL_DEFINED
  184.  
  185. #endif SELECT
  186.  
  187. #if !defined(TRMAVAIL_DEFINED) && defined(FIONREAD)
  188.  
  189. int
  190. trmavail()
  191. {
  192.     long n;
  193.  
  194.     if (pushback >= 0)
  195.         return 1;
  196.     ioctl(0, FIONREAD, (char *) &n);
  197.     return n > 0;
  198. }
  199.  
  200. #define TRMAVAIL_DEFINED
  201.  
  202. #endif FIONREAD
  203.  
  204. #ifndef TRMAVAIL_DEFINED
  205.  
  206. int
  207. trmavail()
  208. {
  209.     if (pushback >= 0)
  210.         return 1;
  211.     return -1;
  212. }
  213.  
  214. #endif
  215.  
  216.  
  217. /*
  218.  * Suspend the editor.
  219.  * Should be called only after trmend and before trmstart!
  220.  */
  221.  
  222. trmsuspend()
  223. {
  224.     int (*oldsig)();
  225.     
  226.     oldsig= signal(SIGTSTP, SIG_IGN);
  227.     if (oldsig == SIG_IGN)
  228.         return; /* Could spawn a subshell here... */
  229.     trmend(); /* Safety net */
  230.     (void) signal(SIGTSTP, oldsig);
  231.     kill(0, SIGSTOP);
  232. }
  233.  
  234. /*
  235.  * Get the true window size.
  236.  * May return 0 if unknown.
  237.  */
  238.  
  239. gettruewinsize(plines, pcols)
  240.     int *plines, *pcols;
  241. {
  242. #ifdef TIOCGWINSZ
  243.     struct winsize win;
  244.     
  245.     if (ioctl(0, TIOCGWINSZ, (char *) &win) == 0) {
  246.         *plines= win.ws_row;
  247.         *pcols= win.ws_col;
  248.     }
  249.     else
  250. #endif
  251.         *plines= *pcols= 0;
  252. }
  253.