1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 2002-2009,2011 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30 /****************************************************************************
31 * Author: Thomas E. Dickey *
32 ****************************************************************************/
33
34 /*
35 ** lib_get_wstr.c
36 **
37 ** The routine wgetn_wstr().
38 **
39 */
40
41 #include <curses.priv.h>
42
43 MODULE_ID("$Id: lib_get_wstr.c,v 1.19 2021/09/04 10:29:59 tom Exp $")
44
45 static int
wadd_wint(WINDOW * win,wint_t * src)46 wadd_wint(WINDOW *win, wint_t *src)
47 {
48 cchar_t tmp;
49 wchar_t wch[2];
50
51 wch[0] = (wchar_t) (*src);
52 wch[1] = 0;
53 setcchar(&tmp, wch, A_NORMAL, (short) 0, NULL);
54 return wadd_wch(win, &tmp);
55 }
56
57 /*
58 * This wipes out the last character, no matter whether it was a tab, control
59 * or other character, and handles reverse wraparound.
60 */
61 static wint_t *
WipeOut(WINDOW * win,int y,int x,wint_t * first,wint_t * last,int echoed)62 WipeOut(WINDOW *win, int y, int x, wint_t *first, wint_t *last, int echoed)
63 {
64 if (last > first) {
65 *--last = '\0';
66 if (echoed) {
67 int y1 = win->_cury;
68 int x1 = win->_curx;
69 int n;
70
71 wmove(win, y, x);
72 for (n = 0; first[n] != 0; ++n) {
73 wadd_wint(win, first + n);
74 }
75 getyx(win, y, x);
76 while (win->_cury < y1
77 || (win->_cury == y1 && win->_curx < x1))
78 waddch(win, (chtype) ' ');
79
80 wmove(win, y, x);
81 }
82 }
83 return last;
84 }
85
86 NCURSES_EXPORT(int)
wgetn_wstr(WINDOW * win,wint_t * str,int maxlen)87 wgetn_wstr(WINDOW *win, wint_t *str, int maxlen)
88 {
89 SCREEN *sp = _nc_screen_of(win);
90 TTY buf;
91 bool oldnl, oldecho, oldraw, oldcbreak;
92 wchar_t erasec = 0;
93 wchar_t killc = 0;
94 wint_t *oldstr = str;
95 wint_t *tmpstr = str;
96 wint_t ch;
97 int y, x, code;
98
99 T((T_CALLED("wgetn_wstr(%p,%p, %d)"), (void *) win, (void *) str, maxlen));
100
101 if (!win)
102 returnCode(ERR);
103
104 maxlen = _nc_getstr_limit(maxlen);
105
106 _nc_get_tty_mode(&buf);
107
108 oldnl = sp->_nl;
109 oldecho = sp->_echo;
110 oldraw = sp->_raw;
111 oldcbreak = sp->_cbreak;
112 NCURSES_SP_NAME(nl) (NCURSES_SP_ARG);
113 NCURSES_SP_NAME(noecho) (NCURSES_SP_ARG);
114 NCURSES_SP_NAME(raw) (NCURSES_SP_ARG);
115
116 NCURSES_SP_NAME(erasewchar) (NCURSES_SP_ARGx &erasec);
117 NCURSES_SP_NAME(killwchar) (NCURSES_SP_ARGx &killc);
118
119 getyx(win, y, x);
120
121 if (is_wintouched(win) || (win->_flags & _HASMOVED))
122 wrefresh(win);
123
124 while ((code = wget_wch(win, &ch)) != ERR) {
125 /*
126 * Map special characters into key-codes.
127 */
128 if (ch == '\r')
129 ch = '\n';
130 if (ch == '\n') {
131 code = KEY_CODE_YES;
132 ch = KEY_ENTER;
133 }
134 if (ch != 0 && ch < KEY_MIN) {
135 if (ch == (wint_t) erasec) {
136 ch = KEY_BACKSPACE;
137 code = KEY_CODE_YES;
138 }
139 if (ch == (wint_t) killc) {
140 ch = KEY_EOL;
141 code = KEY_CODE_YES;
142 }
143 }
144 if (code == KEY_CODE_YES) {
145 /*
146 * Some terminals (the Wyse-50 is the most common) generate a \n
147 * from the down-arrow key. With this logic, it is the user's
148 * choice whether to set kcud=\n for wget_wch(); terminating
149 * *getn_wstr() with \n should work either way.
150 */
151 if (ch == KEY_DOWN || ch == KEY_ENTER) {
152 if (oldecho == TRUE
153 && win->_cury == win->_maxy
154 && win->_scroll)
155 wechochar(win, (chtype) '\n');
156 break;
157 }
158 if (ch == KEY_LEFT || ch == KEY_BACKSPACE) {
159 if (tmpstr > oldstr) {
160 tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
161 }
162 } else if (ch == KEY_EOL) {
163 while (tmpstr > oldstr) {
164 tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
165 }
166 } else {
167 beep();
168 }
169 } else if (tmpstr - oldstr >= maxlen) {
170 beep();
171 } else {
172 *tmpstr++ = ch;
173 *tmpstr = 0;
174 if (oldecho == TRUE) {
175 int oldy = win->_cury;
176
177 if (wadd_wint(win, tmpstr - 1) == ERR) {
178 /*
179 * We can't really use the lower-right corner for input,
180 * since it'll mess up bookkeeping for erases.
181 */
182 win->_flags &= ~_WRAPPED;
183 waddch(win, (chtype) ' ');
184 tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
185 continue;
186 } else if (win->_flags & _WRAPPED) {
187 /*
188 * If the last waddch forced a wrap & scroll, adjust our
189 * reference point for erasures.
190 */
191 if (win->_scroll
192 && oldy == win->_maxy
193 && win->_cury == win->_maxy) {
194 if (--y <= 0) {
195 y = 0;
196 }
197 }
198 win->_flags &= ~_WRAPPED;
199 }
200 wrefresh(win);
201 }
202 }
203 }
204
205 win->_curx = 0;
206 win->_flags &= ~_WRAPPED;
207 if (win->_cury < win->_maxy)
208 win->_cury++;
209 wrefresh(win);
210
211 /* Restore with a single I/O call, to fix minor asymmetry between
212 * raw/noraw, etc.
213 */
214 sp->_nl = oldnl;
215 sp->_echo = oldecho;
216 sp->_raw = oldraw;
217 sp->_cbreak = oldcbreak;
218
219 (void) _nc_set_tty_mode(&buf);
220
221 *tmpstr = 0;
222 if (code == ERR) {
223 if (tmpstr == oldstr) {
224 *tmpstr++ = WEOF;
225 *tmpstr = 0;
226 }
227 returnCode(ERR);
228 }
229
230 T(("wgetn_wstr returns %s", _nc_viswibuf(oldstr)));
231
232 returnCode(OK);
233 }
234