1 /****************************************************************************
2 * Copyright 2018,2020 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.16 2020/02/02 23:34:34 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 wint_t erasec;
93 wint_t killc;
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 nl();
113 noecho();
114 noraw();
115 cbreak();
116
117 erasec = (wint_t) erasechar();
118 killc = (wint_t) killchar();
119
120 getyx(win, y, x);
121
122 if (is_wintouched(win) || (win->_flags & _HASMOVED))
123 wrefresh(win);
124
125 while ((code = wget_wch(win, &ch)) != ERR) {
126 /*
127 * Map special characters into key-codes.
128 */
129 if (ch == '\r')
130 ch = '\n';
131 if (ch == '\n') {
132 code = KEY_CODE_YES;
133 ch = KEY_ENTER;
134 }
135 if (ch < KEY_MIN) {
136 if (ch == erasec) {
137 ch = KEY_BACKSPACE;
138 code = KEY_CODE_YES;
139 }
140 if (ch == killc) {
141 ch = KEY_EOL;
142 code = KEY_CODE_YES;
143 }
144 }
145 if (code == KEY_CODE_YES) {
146 /*
147 * Some terminals (the Wyse-50 is the most common) generate a \n
148 * from the down-arrow key. With this logic, it's the user's
149 * choice whether to set kcud=\n for wget_wch(); terminating
150 * *getn_wstr() with \n should work either way.
151 */
152 if (ch == KEY_DOWN || ch == KEY_ENTER) {
153 if (oldecho == TRUE
154 && win->_cury == win->_maxy
155 && win->_scroll)
156 wechochar(win, (chtype) '\n');
157 break;
158 }
159 if (ch == KEY_LEFT || ch == KEY_BACKSPACE) {
160 if (tmpstr > oldstr) {
161 tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
162 }
163 } else if (ch == KEY_EOL) {
164 while (tmpstr > oldstr) {
165 tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
166 }
167 } else {
168 beep();
169 }
170 } else if (tmpstr - oldstr >= maxlen) {
171 beep();
172 } else {
173 *tmpstr++ = ch;
174 *tmpstr = 0;
175 if (oldecho == TRUE) {
176 int oldy = win->_cury;
177
178 if (wadd_wint(win, tmpstr - 1) == ERR) {
179 /*
180 * We can't really use the lower-right corner for input,
181 * since it'll mess up bookkeeping for erases.
182 */
183 win->_flags &= ~_WRAPPED;
184 waddch(win, (chtype) ' ');
185 tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
186 continue;
187 } else if (win->_flags & _WRAPPED) {
188 /*
189 * If the last waddch forced a wrap & scroll, adjust our
190 * reference point for erasures.
191 */
192 if (win->_scroll
193 && oldy == win->_maxy
194 && win->_cury == win->_maxy) {
195 if (--y <= 0) {
196 y = 0;
197 }
198 }
199 win->_flags &= ~_WRAPPED;
200 }
201 wrefresh(win);
202 }
203 }
204 }
205
206 win->_curx = 0;
207 win->_flags &= ~_WRAPPED;
208 if (win->_cury < win->_maxy)
209 win->_cury++;
210 wrefresh(win);
211
212 /* Restore with a single I/O call, to fix minor asymmetry between
213 * raw/noraw, etc.
214 */
215 sp->_nl = oldnl;
216 sp->_echo = oldecho;
217 sp->_raw = oldraw;
218 sp->_cbreak = oldcbreak;
219
220 (void) _nc_set_tty_mode(&buf);
221
222 *tmpstr = 0;
223 if (code == ERR) {
224 if (tmpstr == oldstr) {
225 *tmpstr++ = WEOF;
226 *tmpstr = 0;
227 }
228 returnCode(ERR);
229 }
230
231 T(("wgetn_wstr returns %s", _nc_viswibuf(oldstr)));
232
233 returnCode(OK);
234 }
235