[BACK]Return to ttyio.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/ttyio.c, Revision 1.20

1.20    ! millert     1: /*     $OpenBSD: ttyio.c,v 1.19 2002/03/27 17:42:37 millert Exp $      */
1.13      niklas      2:
1.1       deraadt     3: /*
1.5       millert     4:  * POSIX terminal I/O.
1.1       deraadt     5:  *
                      6:  * The functions in this file
                      7:  * negotiate with the operating system for
                      8:  * keyboard characters, and write characters to
                      9:  * the display in a barely buffered fashion.
                     10:  */
                     11: #include       "def.h"
                     12:
                     13: #include       <sys/types.h>
1.5       millert    14: #include       <sys/time.h>
                     15: #include       <sys/ioctl.h>
1.1       deraadt    16: #include       <fcntl.h>
1.2       deraadt    17: #include       <termios.h>
1.4       millert    18: #include       <term.h>
1.1       deraadt    19:
1.8       millert    20: #define NOBUF  512                     /* Output buffer size.          */
1.1       deraadt    21:
1.7       millert    22: #ifndef TCSASOFT
                     23: #define TCSASOFT       0
                     24: #endif
                     25:
1.1       deraadt    26: char   obuf[NOBUF];                    /* Output buffer.               */
1.8       millert    27: int    nobuf;                          /* Buffer count.                */
                     28: struct termios oldtty;                 /* POSIX tty settings.          */
                     29: struct termios newtty;
1.1       deraadt    30: int    nrow;                           /* Terminal size, rows.         */
                     31: int    ncol;                           /* Terminal size, columns.      */
                     32:
1.8       millert    33: /*
1.10      millert    34:  * This function gets called once, to set up the terminal.
1.8       millert    35:  * On systems w/o TCSASOFT we turn off off flow control,
                     36:  * which isn't really the right thing to do.
1.1       deraadt    37:  */
1.8       millert    38: void
1.1       deraadt    39: ttopen()
                     40: {
                     41:
1.12      millert    42:        if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO))
1.16      mickey     43:                panic("standard input and output must be a terminal");
1.12      millert    44:
1.8       millert    45:        if (ttraw() == FALSE)
                     46:                panic("aborting due to terminal initialize failure");
                     47: }
1.1       deraadt    48:
1.8       millert    49: /*
                     50:  * This function sets the terminal to RAW mode, as defined for the current
                     51:  * shell.  This is called both by ttopen() above and by spawncli() to
                     52:  * get the current terminal settings and then change them to what
                     53:  * mg expects. Thus, tty changes done while spawncli() is in effect
                     54:  * will be reflected in mg.
                     55:  */
                     56: int
                     57: ttraw()
                     58: {
                     59:
                     60:        if (tcgetattr(0, &oldtty) < 0) {
                     61:                ewprintf("ttopen can't get terminal attributes");
                     62:                return(FALSE);
                     63:        }
                     64:        (void)memcpy(&newtty, &oldtty, sizeof(newtty));
                     65:        /* Set terminal to 'raw' mode and ignore a 'break' */
                     66:        newtty.c_cc[VMIN] = 1;
                     67:        newtty.c_cc[VTIME] = 0;
                     68:        newtty.c_iflag |= IGNBRK;
                     69:        newtty.c_iflag &= ~(BRKINT | PARMRK | INLCR | IGNCR | ICRNL | IXON);
                     70:        newtty.c_oflag &= ~OPOST;
                     71:        newtty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
1.7       millert    72:
                     73: #if !TCSASOFT
1.8       millert    74:        /*
                     75:         * If we don't have TCSASOFT, force terminal to
                     76:         * 8 bits, no parity.
                     77:         */
                     78:        newtty.c_iflag &= ~ISTRIP;
                     79:        newtty.c_cflag &= ~(CSIZE | PARENB);
                     80:        newtty.c_cflag |= CS8;
1.7       millert    81: #endif
1.8       millert    82:        if (tcsetattr(0, TCSASOFT | TCSADRAIN, &newtty) < 0) {
                     83:                ewprintf("ttopen can't tcsetattr");
                     84:                return(FALSE);
                     85:        }
1.10      millert    86:        return(TRUE);
1.8       millert    87: }
1.1       deraadt    88:
1.8       millert    89: /*
                     90:  * This function gets called just before we go back home to the shell.
                     91:  * Put all of the terminal parameters back.
                     92:  * Under UN*X this just calls ttcooked(), but the ttclose() hook is in
                     93:  * because vttidy() in display.c expects it for portability reasons.
                     94:  */
                     95: void
                     96: ttclose()
                     97: {
1.1       deraadt    98:
1.8       millert    99:        if (ttcooked() == FALSE)
                    100:                panic("");              /* ttcooked() already printf'd */
1.1       deraadt   101: }
                    102:
                    103: /*
1.8       millert   104:  * This function restores all terminal settings to their default values,
                    105:  * in anticipation of exiting or suspending the editor.
1.1       deraadt   106:  */
1.8       millert   107: int
                    108: ttcooked()
1.1       deraadt   109: {
1.8       millert   110:
1.1       deraadt   111:        ttflush();
1.8       millert   112:        if (tcsetattr(0, TCSASOFT | TCSADRAIN, &oldtty) < 0) {
                    113:                ewprintf("ttclose can't tcsetattr");
                    114:                return(FALSE);
                    115:        }
                    116:        return(TRUE);
1.1       deraadt   117: }
                    118:
                    119: /*
1.8       millert   120:  * Write character to the display.  Characters are buffered up,
                    121:  * to make things a little bit more efficient.
1.1       deraadt   122:  */
1.11      millert   123: int
1.19      millert   124: ttputc(int c)
1.1       deraadt   125: {
1.5       millert   126:
1.1       deraadt   127:        if (nobuf >= NOBUF)
                    128:                ttflush();
                    129:        obuf[nobuf++] = c;
1.11      millert   130:        return(c);
1.1       deraadt   131: }
                    132:
                    133: /*
                    134:  * Flush output.
                    135:  */
1.8       millert   136: void
1.1       deraadt   137: ttflush()
                    138: {
1.19      millert   139:        ssize_t written;
1.20    ! millert   140:        char *buf = obuf;
1.19      millert   141:
                    142:        if (nobuf == 0)
                    143:                return;
1.5       millert   144:
1.20    ! millert   145:        while ((written = write(fileno(stdout), buf, nobuf)) != nobuf) {
1.19      millert   146:                if (written == -1)
1.8       millert   147:                        panic("ttflush write failed");
1.20    ! millert   148:                buf += written;
        !           149:                nobuf -= written;
1.1       deraadt   150:        }
                    151: }
                    152:
                    153: /*
                    154:  * Read character from terminal.
                    155:  * All 8 bits are returned, so that you can use
                    156:  * a multi-national terminal.
                    157:  */
1.8       millert   158: int
1.1       deraadt   159: ttgetc()
                    160: {
1.9       millert   161:        char    c;
1.18      deraadt   162:        int ret;
1.5       millert   163:
1.18      deraadt   164:        do {
                    165:                ret = read(0, &c, 1);
                    166:                if (ret == -1 && errno == EINTR) {
                    167:                        if (winch_flag) {
                    168:                                refresh(0, 0);
                    169:                                winch_flag = 0;
                    170:                        }
                    171:                } else if (ret == 1)
                    172:                        break;
                    173:        } while (1);
1.9       millert   174:        return ((int) c);
1.1       deraadt   175: }
                    176:
1.8       millert   177: /*
                    178:  * Returns TRUE if there are characters waiting to be read.
                    179:  */
                    180: int
                    181: typeahead()
                    182: {
                    183:        int     x;
                    184:
                    185:        return((ioctl(0, FIONREAD, (char *) &x) < 0) ? 0 : x);
                    186: }
                    187:
                    188: /*
                    189:  * panic - just exit, as quickly as we can.
                    190:  */
1.15      art       191: void
1.19      millert   192: panic(char *s)
1.8       millert   193: {
                    194:
                    195:        (void) fputs("panic: ", stderr);
                    196:        (void) fputs(s, stderr);
                    197:        (void) fputc('\n', stderr);
1.12      millert   198:        exit(1);
1.8       millert   199: }
                    200:
1.1       deraadt   201: /*
1.14      art       202:  * This function returns FALSE if any characters have showed up on the
                    203:  * tty before 'msec' miliseconds.
1.1       deraadt   204:  */
1.8       millert   205: int
1.14      art       206: ttwait(int msec)
1.1       deraadt   207: {
1.8       millert   208:        fd_set          readfds;
                    209:        struct timeval  tmout;
1.5       millert   210:
1.16      mickey    211:        FD_ZERO(&readfds);
1.5       millert   212:        FD_SET(0, &readfds);
                    213:
1.14      art       214:        tmout.tv_sec = msec/1000;
                    215:        tmout.tv_usec = msec - tmout.tv_sec * 1000;
1.5       millert   216:
                    217:        if ((select(1, &readfds, NULL, NULL, &tmout)) == 0)
1.8       millert   218:                return (TRUE);
                    219:        return (FALSE);
1.1       deraadt   220: }