home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2872 / termio.c < prev    next >
C/C++ Source or Header  |  1991-02-27  |  3KB  |  120 lines

  1. /*
  2.  * The functions in this file negotiate with the operating system for
  3.  * characters, and write characters in a barely buffered fashion on the display.
  4.  * All operating systems.
  5.  */
  6.  
  7. #include    "def.h"
  8.  
  9. #if UNIX    /* System V */
  10.  
  11. #include    <stdio.h>
  12. #include    <signal.h>
  13. #include    <termio.h>
  14. #include    <errno.h>
  15. #include    <fcntl.h>
  16. int kbdflgs;            /* saved keyboard fd flags  */
  17. int kbdpoll;            /* in O_NDELAY mode         */
  18. int kbdqp;          /* there is a char in kbdq  */
  19. char kbdq;          /* char we've already read  */
  20. struct  termio  otermio;    /* original terminal characteristics */
  21. struct  termio  ntermio;    /* charactoristics to use inside */
  22.  
  23. int     nrow;                   /* Terminal size, rows.         */
  24. int     ncol;                   /* Terminal size, columns.      */
  25.  
  26. /*
  27.  * This function is called once to set up the terminal device streams.
  28.  * On VMS, it translates TT until it finds the terminal, then assigns
  29.  * a channel to it and sets it raw. On CPM it is a no-op.
  30.  */
  31. ttopen()
  32. {
  33.     ioctl(0, TCGETA, &otermio); /* save old settings */
  34.     ntermio.c_iflag = 0;        /* setup new settings */
  35.     ntermio.c_oflag = 0;
  36.     ntermio.c_cflag = otermio.c_cflag;
  37.     ntermio.c_lflag = 0;
  38.     ntermio.c_line = otermio.c_line;
  39.     ntermio.c_cc[VMIN] = 1;
  40.     ntermio.c_cc[VTIME] = 0;
  41.     ioctl(0, TCSETAW, &ntermio); /* and activate them */
  42.     kbdflgs = fcntl( 0, F_GETFL, 0 );
  43.     kbdpoll = FALSE;
  44.     /* on all screens we are not sure of the initial position
  45.        of the cursor                    */
  46.     ttrow = 999;
  47.     ttcol = 999;
  48.     nrow = NROW;
  49.     ncol = NCOL;
  50. }
  51.  
  52. /*
  53.  * This function gets called just before we go back home to the command
  54.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  55.  * Another no-operation on CPM.
  56.  */
  57. ttclose()
  58. {
  59.     if (ioctl(0, TCSETAW, &otermio) == -1) /* restore terminal settings */
  60.         printf ("closing ioctl on dev 0 failure, error = %d\n", errno);
  61.     if (fcntl(0, F_SETFL, kbdflgs) == -1)
  62.         printf ("closing fcntl on dev 0 failure, error = %d\n", errno);
  63. }
  64.  
  65. /*
  66.  * Write a character to the display. On VMS, terminal output is buffered, and
  67.  * we just put the characters in the big array, after checking for overflow.
  68.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  69.  * MS-DOS (use the very very raw console output routine).
  70.  */
  71. ttputc(c)
  72. {
  73.     fputc(c, stdout);
  74. }
  75.  
  76. /*
  77.  * Flush terminal buffer. Does real work where the terminal output is buffered
  78.  * up. A no-operation on systems where byte at a time terminal I/O is done.
  79.  */
  80. ttflush()
  81. {
  82.    fflush(stdout);
  83. }
  84.  
  85. /*
  86.  * Read a character from the terminal, performing no editing and doing no echo
  87.  * at all. More complex in VMS that almost anyplace else, which figures. Very
  88.  * simple on CPM, because the system can do exactly what you want.
  89.  */
  90. ttgetc()
  91. {
  92.     if( kbdqp )
  93.         kbdqp = FALSE;
  94.     else
  95.     {
  96.         if( kbdpoll && fcntl( 0, F_SETFL, kbdflgs ) < 0 )
  97.             return FALSE;
  98.         kbdpoll = FALSE;
  99.         while (read(0, &kbdq, 1) != 1)
  100.             ;
  101.     }
  102.     return ( kbdq & 127 );
  103. }
  104.  
  105. /* typahead():    Check to see if any characters are already in the
  106.         keyboard buffer
  107. */
  108. ttkeyready ()
  109. {
  110.     if( !kbdqp )
  111.     {
  112.         if( !kbdpoll && fcntl( 0, F_SETFL, kbdflgs | O_NDELAY ) < 0 )
  113.             return(FALSE);
  114.         kbdqp = (1 == read( 0, &kbdq, 1 ));
  115.     }
  116.     return ( kbdqp );
  117. }
  118. #endif
  119.  
  120.