1 /* $OpenBSD: lib_get_wstr.c,v 1.2 2023/10/17 09:52:09 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright 2018-2021,2023 Thomas E. Dickey                                *
5  * Copyright 2002-2009,2011 Free Software Foundation, Inc.                  *
6  *                                                                          *
7  * Permission is hereby granted, free of charge, to any person obtaining a  *
8  * copy of this software and associated documentation files (the            *
9  * "Software"), to deal in the Software without restriction, including      *
10  * without limitation the rights to use, copy, modify, merge, publish,      *
11  * distribute, distribute with modifications, sublicense, and/or sell       *
12  * copies of the Software, and to permit persons to whom the Software is    *
13  * furnished to do so, subject to the following conditions:                 *
14  *                                                                          *
15  * The above copyright notice and this permission notice shall be included  *
16  * in all copies or substantial portions of the Software.                   *
17  *                                                                          *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25  *                                                                          *
26  * Except as contained in this notice, the name(s) of the above copyright   *
27  * holders shall not be used in advertising or otherwise to promote the     *
28  * sale, use or other dealings in this Software without prior written       *
29  * authorization.                                                           *
30  ****************************************************************************/
31 
32 /****************************************************************************
33  *  Author: Thomas E. Dickey                                                *
34  ****************************************************************************/
35 
36 /*
37 **	lib_get_wstr.c
38 **
39 **	The routine wgetn_wstr().
40 **
41 */
42 
43 #include <curses.priv.h>
44 
45 MODULE_ID("$Id: lib_get_wstr.c,v 1.2 2023/10/17 09:52:09 nicm Exp $")
46 
47 static int
wadd_wint(WINDOW * win,wint_t * src)48 wadd_wint(WINDOW *win, wint_t *src)
49 {
50     cchar_t tmp;
51     wchar_t wch[2];
52 
53     wch[0] = (wchar_t) (*src);
54     wch[1] = 0;
55     setcchar(&tmp, wch, A_NORMAL, (short) 0, NULL);
56     return wadd_wch(win, &tmp);
57 }
58 
59 /*
60  * This wipes out the last character, no matter whether it was a tab, control
61  * or other character, and handles reverse wraparound.
62  */
63 static wint_t *
WipeOut(WINDOW * win,int y,int x,wint_t * first,wint_t * last,int echoed)64 WipeOut(WINDOW *win, int y, int x, wint_t *first, wint_t *last, int echoed)
65 {
66     if (last > first) {
67 	*--last = '\0';
68 	if (echoed) {
69 	    int y1 = win->_cury;
70 	    int x1 = win->_curx;
71 	    int n;
72 
73 	    wmove(win, y, x);
74 	    for (n = 0; first[n] != 0; ++n) {
75 		wadd_wint(win, first + n);
76 	    }
77 	    getyx(win, y, x);
78 	    while (win->_cury < y1
79 		   || (win->_cury == y1 && win->_curx < x1))
80 		waddch(win, (chtype) ' ');
81 
82 	    wmove(win, y, x);
83 	}
84     }
85     return last;
86 }
87 
88 NCURSES_EXPORT(int)
wgetn_wstr(WINDOW * win,wint_t * str,int maxlen)89 wgetn_wstr(WINDOW *win, wint_t *str, int maxlen)
90 {
91     SCREEN *sp = _nc_screen_of(win);
92     TTY buf;
93     TTY_FLAGS save_flags;
94     wchar_t erasec = 0;
95     wchar_t killc = 0;
96     wint_t *oldstr = str;
97     wint_t *tmpstr = str;
98     wint_t ch;
99     int y, x, code;
100 
101     T((T_CALLED("wgetn_wstr(%p,%p, %d)"), (void *) win, (void *) str, maxlen));
102 
103     if (!win)
104 	returnCode(ERR);
105 
106     maxlen = _nc_getstr_limit(maxlen);
107 
108     _nc_get_tty_mode(&buf);
109 
110     save_flags = sp->_tty_flags;
111     NCURSES_SP_NAME(nl) (NCURSES_SP_ARG);
112     NCURSES_SP_NAME(noecho) (NCURSES_SP_ARG);
113     if (!save_flags._raw)
114 	NCURSES_SP_NAME(cbreak) (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 (save_flags._echo == 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, save_flags._echo);
161 		}
162 	    } else if (ch == KEY_EOL) {
163 		while (tmpstr > oldstr) {
164 		    tmpstr = WipeOut(win, y, x, oldstr, tmpstr, save_flags._echo);
165 		}
166 	    } else {
167 		beep();
168 	    }
169 	} else if (tmpstr - oldstr >= maxlen) {
170 	    beep();
171 	} else {
172 	    *tmpstr++ = ch;
173 	    *tmpstr = 0;
174 	    if (save_flags._echo == 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, save_flags._echo);
185 		    continue;
186 		} else if (IS_WRAPPED(win)) {
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->_tty_flags = save_flags;
215     (void) _nc_set_tty_mode(&buf);
216 
217     *tmpstr = 0;
218     if (code == ERR) {
219 	if (tmpstr == oldstr) {
220 	    *tmpstr++ = WEOF;
221 	    *tmpstr = 0;
222 	}
223 	returnCode(ERR);
224     }
225 
226     T(("wgetn_wstr returns %s", _nc_viswibuf(oldstr)));
227 
228     returnCode(OK);
229 }
230