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