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