1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 1995, by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27 /*
28 * wgetch.c
29 *
30 * XCurses Library
31 *
32 * Copyright 1990, 1995 by Mortice Kern Systems Inc. All rights reserved.
33 *
34 */
35
36 #ifdef M_RCSID
37 #ifndef lint
38 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/wgetch.c 1.5 1995/06/19 16:12:13 ant Exp $";
39 #endif
40 #endif
41
42 #include <private.h>
43
44 /*
45 * Push single-byte character back onto the input queue.
46 *
47 * MKS EXTENSION permits the return value of wgetch(), which
48 * can be a KEY_ value, to be pushed back.
49 */
50 int
ungetch(ch)51 ungetch(ch)
52 int ch;
53 {
54 int code;
55
56 #ifdef M_CURSES_TRACE
57 __m_trace("ungetch(%d)", ch);
58 #endif
59 code = __xc_ungetc(ch, (WINDOW *) 0) == EOF ? ERR : OK;
60
61 return __m_return_code("ungetch", code);
62 }
63
64 /*
65 * Push a single-byte character or KEY_ value but onto the
66 * input queue. Ignore the window parameter.
67 */
68 int
__xc_ungetc(int ch,void * w)69 __xc_ungetc(int ch, void *w)
70 {
71 if (ISFULL())
72 return EOF;
73
74 PUSH(ch);
75
76 return 0;
77 }
78
79 /*
80 * Return true if the SCREEN's stream has an I/O error.
81 * Ignore the window parameter.
82 */
83 int
__xc_ferror(void * w)84 __xc_ferror(void *w)
85 {
86 return ferror(__m_screen->_if);
87 }
88
89 /*
90 * Return true if the SCREEN's stream has seen EOF.
91 * Ignore the window parameter.
92 */
93 int
__xc_feof(void * w)94 __xc_feof(void *w)
95 {
96 return feof(__m_screen->_if);
97 }
98
99 /*
100 * Clear the error and eof flags of the SCREEN's stream.
101 * Ignore the window parameter.
102 */
103 void
__xc_clearerr(void * w)104 __xc_clearerr(void *w)
105 {
106 clearerr(__m_screen->_if);
107 }
108
109 int
wgetch(w)110 wgetch(w)
111 WINDOW *w;
112 {
113 t_decode *node;
114 int ch, i, j, timeout;
115
116 #ifdef M_CURSES_TRACE
117 __m_trace("wgetch(%p) at (%d, %d).", w, w->_cury, w->_curx);
118 #endif
119
120 (void) wrefresh(w);
121
122 if (!ISEMPTY())
123 return __m_return_int("wgetch", POP());
124
125 /* Only change the terminal's input method if the window
126 * requires different settings from what is currently set.
127 * We do this because tcsetattr() on some systems can be
128 * _really_ slow to do for each character.
129 *
130 * NOTE that halfdelay() overrides nodelay() and wtimeout().
131 */
132 if (!(cur_term->_flags & __TERM_HALF_DELAY)
133 && (cur_term->_prog.c_cc[VMIN] != w->_vmin
134 || cur_term->_prog.c_cc[VTIME] != w->_vtime)) {
135 cur_term->_prog.c_cc[VMIN] = w->_vmin;
136 cur_term->_prog.c_cc[VTIME] = w->_vtime;
137
138 if (__m_tty_set(&cur_term->_prog) == ERR)
139 return __m_return_int("wgetch", EOF);
140 }
141
142 if (req_for_input != (char *) 0)
143 (void) tputs(req_for_input, 1, __m_outc);
144
145 clearerr(__m_screen->_if);
146 ch = fgetc(__m_screen->_if);
147
148 /* Only check for function keys if keypad is true and we
149 * did not read a KEY_ value (which are < 0), nor EOF.
150 * It is conceivable that a KEY_ was pushed back with
151 * ungetch().
152 */
153 if ((w->_flags & W_USE_KEYPAD) && 0 <= ch && ch != EOF) {
154 /* Treat the termios ERASE key the same as key_backspace.
155 *
156 * We used to change the key_backspace entry to be a string
157 * containing the ERASE key in setupterm(), but this would
158 * then disable the real terminfo entry for the backspace key.
159 * Apparently VT300 terminals change the key code/sequence
160 * of the backspace key in application keypad mode.
161 * See SR 6014.
162 *
163 * Refer to _shell instead of _prog, since _shell will
164 * correctly reflect the user's prefered settings, whereas
165 * _prog may not have been initialised if both input and
166 * output have been redirected.
167 */
168 #ifdef _POSIX_VDISABLE
169 if (cur_term->_shell.c_cc[VERASE] != _POSIX_VDISABLE)
170 #endif
171 if (ch == cur_term->_shell.c_cc[VERASE])
172 return __m_return_int("wgetch", KEY_BACKSPACE);
173
174 /* Begin check for function key. */
175 node = (t_decode *) __m_screen->_decode;
176
177 /* Use input stack as a queue. */
178 timeout = w->_flags & W_USE_TIMEOUT;
179 for (RESET(); !ISFULL(); ) {
180 PUSH(ch);
181
182 while (node->ch != ch) {
183 node = node->sibling;
184 if (node == (t_decode *) 0)
185 goto invalid;
186 }
187
188 /* Found funuction key? */
189 if (node->key != 0) {
190 RESET();
191 return __m_return_int("wgetch", node->key);
192 }
193
194 /* Setup interbyte timer (once only). fgetc() will
195 * return EOF if no input received, which may not be
196 * a true EOF.
197 */
198 if (timeout) {
199 cur_term->_prog.c_cc[VMIN] = 0;
200 cur_term->_prog.c_cc[VTIME] =
201 M_CURSES_INTERBYTE_TIME;
202 (void) __m_tty_set(&cur_term->_prog);
203 }
204 timeout = 0;
205
206 if ((ch = fgetc(__m_screen->_if)) == EOF)
207 /* Timeout or real eof. */
208 break;
209
210 /* Incomplete sequence, continue. */
211 node = node->child;
212 }
213 invalid:
214 /* Reverse contents of the input queue to form a stack. */
215 for (i = 0, j = __m_screen->_unget._count; i < --j; ++i) {
216 ch = __m_screen->_unget._stack[i];
217 __m_screen->_unget._stack[i] =
218 __m_screen->_unget._stack[j];
219 __m_screen->_unget._stack[j] = ch;
220 }
221
222 /* Return first byte received or EOF. */
223 ch = POP();
224 }
225
226 if ((__m_screen->_flags & S_ECHO) && 0 <= ch && ch != EOF) {
227 (void) waddch(w, ch);
228 (void) wrefresh(w);
229 }
230
231 return __m_return_int("wgetch", ch);
232 }
233
234