home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d023 / microemacs.lha / Ver30 / Sys / Ultrix / ttyio.c < prev   
Encoding:
C/C++ Source or Header  |  1986-05-10  |  4.7 KB  |  197 lines

  1. /*
  2.  * Name:    MicroEMACS
  3.  *        Ultrix-32 and Unix terminal I/O.
  4.  * Version:    29 -- osu 2
  5.  * Last edit:    22-Apr-86
  6.  * By:        paul@ohio-state
  7.  *        cbosgd!osu-eddie!paul
  8.  *
  9.  * The functions in this file
  10.  * negotiate with the operating system for
  11.  * keyboard characters, and write characters to
  12.  * the display in a barely buffered fashion.
  13.  */
  14. #include    "def.h"
  15.  
  16. #include    <sgtty.h>
  17.  
  18. #define    NOBUF    512            /* Output buffer size.        */
  19.  
  20. char    obuf[NOBUF];            /* Output buffer.        */
  21. int    nobuf;
  22. struct    sgttyb    oldtty;            /* V6/V7 stty data.        */
  23. struct    sgttyb    newtty;
  24. struct    tchars    oldtchars;        /* V7 editing.            */
  25. struct    tchars    newtchars;
  26. struct    ltchars oldltchars;        /* 4.2 BSD editing.        */
  27. struct    ltchars    newltchars;
  28. int    nrow;                /* Terminal size, rows.        */
  29. int    ncol;                /* Terminal size, columns.    */
  30.  
  31. /*
  32.  * This function gets called once, to set up
  33.  * the terminal channel. On Ultrix is's tricky, since
  34.  * we want flow control, but we don't want any characters
  35.  * stolen to send signals. Use CBREAK mode, and set all
  36.  * characters but start and stop to 0xFF.
  37.  */
  38. ttopen()
  39. {
  40.     register char    *cp;
  41.     extern char    *getenv();
  42. #ifdef TERMCAP
  43.         char *t, *p, *tgetstr();
  44.         char tcbuf[1024];
  45.         char *tv_stype;
  46.         char err_str[72];
  47. #endif
  48.  
  49.     if (ioctl(0, TIOCGETP, &oldtty) < 0)
  50.         abort();
  51.     newtty.sg_ospeed = oldtty.sg_ospeed;
  52.     newtty.sg_ispeed = oldtty.sg_ispeed;
  53.     newtty.sg_erase  = oldtty.sg_erase;
  54.     newtty.sg_kill   = oldtty.sg_kill;
  55.     newtty.sg_flags  = oldtty.sg_flags;
  56.     newtty.sg_flags &= ~(ECHO|CRMOD);    /* Kill echo, CR=>NL.    */
  57. #if FLOWCONTROL
  58.     newtty.sg_flags |= CBREAK;        /* Half-cooked mode.    */
  59. #else
  60.     newtty.sg_flags |= RAW|ANYP;        /* raw mode for 8 bit path.*/
  61. #endif
  62.     if (ioctl(0, TIOCSETP, &newtty) < 0)
  63.         abort();
  64.     if (ioctl(0, TIOCGETC, &oldtchars) < 0)
  65.         abort();
  66.     newtchars.t_intrc  = 0xFF;        /* Interrupt.        */
  67.     newtchars.t_quitc  = 0xFF;        /* Quit.        */
  68. #if FLOWCONTROL
  69.     newtchars.t_startc = 0x11;        /* ^Q, for terminal.    */
  70.     newtchars.t_stopc  = 0x13;        /* ^S, for terminal.    */
  71. #else
  72.     newtchars.t_startc = 0xFF;        /* ^Q, for terminal.    */
  73.     newtchars.t_stopc  = 0xFF;        /* ^S, for terminal.    */
  74. #endif
  75.     newtchars.t_eofc   = 0xFF;
  76.     newtchars.t_brkc   = 0xFF;
  77.     if (ioctl(0, TIOCSETC, &newtchars) < 0)
  78.         abort();
  79.     if (ioctl(0, TIOCGLTC, &oldltchars) < 0)
  80.         abort();
  81.     newltchars.t_suspc  = 0xFF;        /* Suspend #1.        */
  82.     newltchars.t_dsuspc = 0xFF;        /* Suspend #2.        */
  83.     newltchars.t_rprntc = 0xFF;
  84.     newltchars.t_flushc = 0xFF;        /* Output flush.    */
  85.     newltchars.t_werasc = 0xFF;
  86.     newltchars.t_lnextc = 0xFF;        /* Literal next.    */
  87.     if (ioctl(0, TIOCSLTC, &newltchars) < 0)
  88.         abort();
  89. #ifndef TERMCAP
  90.     if ((cp=getenv("TERMCAP")) == NULL
  91.     || (nrow=getvalue(cp, "li")) <= 0
  92.     || (ncol=getvalue(cp, "co")) <= 0) {
  93. #else
  94.  
  95. /* do this the REAL way */
  96.         if ((tv_stype = getenv("TERM")) == NULL)
  97.         {
  98.                 puts("Environment variable TERM not defined!");
  99.                 exit(1);
  100.         }
  101.  
  102.         if((tgetent(tcbuf, tv_stype)) != 1)
  103.         {
  104.                 sprintf(err_str, "Unknown terminal type %s!", tv_stype);
  105.                 puts(err_str);
  106.                 exit(1);
  107.         }
  108.  
  109.     if ((nrow=tgetnum ("li")) <= 0
  110.     || (ncol=tgetnum ("co")) <= 0) {
  111. #endif TERMCAP
  112.         nrow = 24;
  113.         ncol = 80;
  114.     }
  115.     if (nrow > NROW)            /* Don't crash if the    */
  116.         nrow = NROW;            /* termcap entry is    */
  117.     if (ncol > NCOL)            /* too big.        */
  118.         ncol = NCOL;
  119. }
  120.  
  121. #ifndef TERMCAP
  122. /*
  123.  * This routine scans a string, which is
  124.  * actually the return value of a getenv call for the TERMCAP
  125.  * variable, looking for numeric parameter "name". Return the value
  126.  * if found. Return -1 if not there. Assume that "name" is 2
  127.  * characters long. This limited use of the TERMCAP lets us find
  128.  * out the size of a window on the X display.
  129.  */
  130. getvalue(cp, name)
  131. register char    *cp;
  132. register char    *name;
  133. {
  134.     for (;;) {
  135.         while (*cp!=0 && *cp!=':')
  136.             ++cp;
  137.         if (*cp++ == 0)            /* Not found.        */
  138.             return (-1);
  139.         if (cp[0]==name[0] && cp[1]==name[1] && cp[2]=='#')
  140.             return (atoi(cp+3));    /* Stops on ":".    */
  141.     }
  142. }
  143. #endif    /* TERMCAP */
  144.  
  145. /*
  146.  * This function gets called just
  147.  * before we go back home to the shell. Put all of
  148.  * the terminal parameters back.
  149.  */
  150. ttclose()
  151. {
  152.     ttflush();
  153.     if (ioctl(0, TIOCSLTC, &oldltchars) < 0)
  154.         abort();
  155.     if (ioctl(0, TIOCSETC, &oldtchars) < 0)
  156.         abort();
  157.     if (ioctl(0, TIOCSETP, &oldtty) < 0)
  158.         abort();
  159. }
  160.  
  161. /*
  162.  * Write character to the display.
  163.  * Characters are buffered up, to make things
  164.  * a little bit more efficient.
  165.  */
  166. ttputc(c)
  167. {
  168.     if (nobuf >= NOBUF)
  169.         ttflush();
  170.     obuf[nobuf++] = c;
  171. }
  172.  
  173. /*
  174.  * Flush output.
  175.  */
  176. ttflush()
  177. {
  178.     if (nobuf != 0) {
  179.         write(1, obuf, nobuf);
  180.         nobuf = 0;
  181.     }
  182. }
  183.  
  184. /*
  185.  * Read character from terminal.
  186.  * All 8 bits are returned, so that you can use
  187.  * a multi-national terminal.
  188.  */
  189. ttgetc()
  190. {
  191.     char    buf[1];
  192.  
  193.     while (read(0, &buf[0], 1) != 1)
  194.         ;
  195.     return (buf[0] & 0xFF);
  196. }
  197.