home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / shellforms / part02 / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-09  |  4.0 KB  |  178 lines

  1. /* Last update: 01/13/88  11:26 AM  (Edition: 5) */
  2. #include    <stdio.h>
  3. #include    <ctype.h>
  4. #include    <sys/file.h>
  5.  
  6. #ifndef        _SGTTYB_
  7. #include    <sgtty.h>
  8. #endif
  9.  
  10. int        Term_input;
  11. int        Term_output;
  12. extern    int    Debug;
  13. /*-------------------------------------------------------------05/07/86-+
  14. |                                    |
  15. |        put_string : write out a string to Term_output        |
  16. |                                    |
  17. +----------------------------------------------------------------------*/
  18. put_string (s, len)
  19. char        *s;
  20. unsigned    len;
  21.     {
  22.     if (len == 0) len = strlen (s);
  23.     if (Debug) {
  24.         register int    i;
  25.         register char    *p = s;
  26.  
  27.         for (i=0; i<len; i++) put_char (*p++);
  28.         }
  29.     else write (Term_output, s, (int)len);
  30.     }
  31.  
  32. /*-------------------------------------------------------------05/07/86-+
  33. |                                    |
  34. |        put_char : write out one char to Term_output        |
  35. |                                    |
  36. +----------------------------------------------------------------------*/
  37. put_char (c)
  38. char    c;
  39.     {
  40.     if (Debug) {
  41.         if (isprint(c)) write (Term_output, &c, 1);
  42.         else    {
  43.             char    buf [2];
  44.             buf[0] = (c < ' ') ? '^' : '~';
  45.             buf[1] = (c | 0x40) & 0x5f;
  46.             write (Term_output, buf, 2);
  47.             }
  48.         }
  49.     else write (Term_output, &c, 1);
  50.     }
  51.  
  52. static    char    Pushbuf [80];
  53. static    int    Pushi = 0;
  54. /*-------------------------------------------------------------05/07/86-+
  55. |                                    |
  56. |        get_char : get a char from Term_input            |
  57. |                                    |
  58. +----------------------------------------------------------------------*/
  59. char
  60. get_char ()
  61.     {
  62.     char        c;
  63.     register int    i;
  64.  
  65.     if (Pushi > 0) {        /* push buffer not empty */
  66.         c = Pushbuf[0];
  67.         Pushi--;
  68.         for (i=0; i<Pushi; i++) {
  69.             Pushbuf[i] = Pushbuf[i+1];
  70.             }
  71.         return (c);
  72.         }
  73.     read (Term_input, &c, 1);
  74.     c &= 0x7f;            /* make it ASCII */
  75.     return (c);
  76.     }
  77.  
  78. pushback (buf, len)
  79. char    *buf;
  80. int    len;
  81.     {
  82.     register char    *p = &Pushbuf [Pushi];
  83.  
  84.     Pushi += len;
  85.     while (len-- > 0) {
  86.         *p++ = *buf++;
  87.         }
  88.     }
  89.  
  90. static    int        Old_flags;
  91. static    struct    sgttyb    Mode_tty;
  92. /*-------------------------------------------------------------05/07/86-+
  93. |                                    |
  94. |         cbreakio : enter/exit cbreak I/O mode            |
  95. |                                    |
  96. +----------------------------------------------------------------------*/
  97. cbreakio (n)
  98. int    n;                /* 1 -- enter, 0 -- exit */
  99.     {
  100.     static    int    cbreak_io = 0;
  101.     int        ostate;
  102.  
  103.     ostate = cbreak_io;
  104.     if (n) {
  105.         if (!cbreak_io) {
  106.             open_tty ();
  107.             cbreak_io = 1;
  108.             }
  109.         }
  110.     else    {
  111.         if (cbreak_io) {
  112.             close_tty ();
  113.             cbreak_io = 0;
  114.             }
  115.         }
  116.     return (ostate);
  117.     }
  118.  
  119. /*-------------------------------------------------------------01/12/88-+
  120. |                                    |
  121. |        term_init : open terminal for read/write        |
  122. |                                    |
  123. +----------------------------------------------------------------------*/
  124. term_init ()
  125.     {
  126.     extern    char    *Prgname;
  127.  
  128.     if ((Term_input = open ("/dev/tty", O_RDONLY)) < 0) {
  129.         perror ("term_init: open(/dev/tty,r)");
  130.         exit (1);
  131.         }
  132.     if ((Term_output = open ("/dev/tty", O_WRONLY)) < 0) {
  133.         perror ("term_init: open(/dev/tty,r)");
  134.         exit (1);
  135.         }
  136.  
  137.     if (!isatty(Term_input) || !isatty(Term_output)) {
  138.         fprintf (stderr, "You have to run %s interactively\n",
  139.              Prgname);
  140.         exit (1);
  141.         }
  142.     }
  143.  
  144. /*-------------------------------------------------------------01/12/88-+
  145. |                                    |
  146. |         term_close : close terminal descriptors        |
  147. |                                    |
  148. +----------------------------------------------------------------------*/
  149. term_close ()
  150.     {
  151.     close (Term_input);
  152.     close (Term_output);
  153.     }
  154. /*------------------------------------------------------------07/10/84--+
  155. |                                    |
  156. |     open_tty : open terminal in CBREAK mode without ECHO        |
  157. |                                    |
  158. +----------------------------------------------------------------------*/
  159. static    open_tty ()
  160.     {
  161.     gtty (Term_input, &Mode_tty);
  162.     Old_flags = Mode_tty.sg_flags;        /* save old setting */
  163.     Mode_tty.sg_flags |= CBREAK;
  164.     Mode_tty.sg_flags &= ~(ECHO | CRMOD);
  165.     stty (Term_input, &Mode_tty);
  166.     }
  167.  
  168. /*------------------------------------------------------------07/10/84--+
  169. |                                    |
  170. |    close_tty : close terminal and restore original setting        |
  171. |                                    |
  172. +----------------------------------------------------------------------*/
  173. static    close_tty ()
  174.     {
  175.     Mode_tty.sg_flags = Old_flags;
  176.     stty (Term_input, &Mode_tty);        /* restore original setting */
  177.     }
  178.