blob: cac21fc85bd9fa9402aa74c0b0e591cd061e6bdb [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2021,2023 Thomas E. Dickey *
3 * Copyright 1998-2011,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 ****************************************************************************/
34
35/*
36** lib_getstr.c
37**
38** The routine wgetstr().
39**
40*/
41
micky3879b9f5e72025-07-08 18:04:53 -040042#define NEED_KEY_EVENT
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053043#include <curses.priv.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053044
micky3879b9f5e72025-07-08 18:04:53 -040045MODULE_ID("$Id: lib_getstr.c,v 1.39 2023/04/29 19:00:17 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053046
47/*
48 * This wipes out the last character, no matter whether it was a tab, control
49 * or other character, and handles reverse wraparound.
50 */
51static char *
Steve Kondikae271bc2015-11-15 02:50:53 +010052WipeOut(WINDOW *win, int y, int x, char *first, char *last, int echoed)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053053{
54 if (last > first) {
55 *--last = '\0';
56 if (echoed) {
57 int y1 = win->_cury;
58 int x1 = win->_curx;
59
60 wmove(win, y, x);
61 waddstr(win, first);
62 getyx(win, y, x);
63 while (win->_cury < y1
64 || (win->_cury == y1 && win->_curx < x1))
65 waddch(win, (chtype) ' ');
66
67 wmove(win, y, x);
68 }
69 }
70 return last;
71}
72
73NCURSES_EXPORT(int)
74wgetnstr_events(WINDOW *win,
75 char *str,
76 int maxlen,
77 EVENTLIST_1st(_nc_eventlist * evl))
78{
79 SCREEN *sp = _nc_screen_of(win);
80 TTY buf;
micky3879b9f5e72025-07-08 18:04:53 -040081 TTY_FLAGS save_flags;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053082 char erasec;
83 char killc;
84 char *oldstr;
85 int ch;
86 int y, x;
87
Steve Kondikae271bc2015-11-15 02:50:53 +010088 T((T_CALLED("wgetnstr(%p,%p,%d)"), (void *) win, (void *) str, maxlen));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053089
micky3879b9f5e72025-07-08 18:04:53 -040090 if (!win || !str)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053091 returnCode(ERR);
92
micky3879b9f5e72025-07-08 18:04:53 -040093 maxlen = _nc_getstr_limit(maxlen);
94
Steve Kondikae271bc2015-11-15 02:50:53 +010095 NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_ARGx &buf);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053096
micky3879b9f5e72025-07-08 18:04:53 -040097 save_flags = sp->_tty_flags;
Steve Kondikae271bc2015-11-15 02:50:53 +010098 NCURSES_SP_NAME(nl) (NCURSES_SP_ARG);
99 NCURSES_SP_NAME(noecho) (NCURSES_SP_ARG);
micky3879b9f5e72025-07-08 18:04:53 -0400100 if (!save_flags._raw)
101 NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530102
Steve Kondikae271bc2015-11-15 02:50:53 +0100103 erasec = NCURSES_SP_NAME(erasechar) (NCURSES_SP_ARG);
104 killc = NCURSES_SP_NAME(killchar) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530105
106 oldstr = str;
107 getyx(win, y, x);
108
109 if (is_wintouched(win) || (win->_flags & _HASMOVED))
110 wrefresh(win);
111
112 while ((ch = wgetch_events(win, evl)) != ERR) {
113 /*
114 * Some terminals (the Wyse-50 is the most common) generate
micky3879b9f5e72025-07-08 18:04:53 -0400115 * a \n from the down-arrow key. With this logic, it is the
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530116 * user's choice whether to set kcud=\n for wgetch();
117 * terminating *getstr() with \n should work either way.
118 */
119 if (ch == '\n'
120 || ch == '\r'
121 || ch == KEY_DOWN
122 || ch == KEY_ENTER) {
micky3879b9f5e72025-07-08 18:04:53 -0400123 if (save_flags._echo == TRUE
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530124 && win->_cury == win->_maxy
125 && win->_scroll)
126 wechochar(win, (chtype) '\n');
127 break;
128 }
129#ifdef KEY_EVENT
130 if (ch == KEY_EVENT)
131 break;
132#endif
133#ifdef KEY_RESIZE
134 if (ch == KEY_RESIZE)
135 break;
136#endif
137 if (ch == erasec || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
138 if (str > oldstr) {
micky3879b9f5e72025-07-08 18:04:53 -0400139 str = WipeOut(win, y, x, oldstr, str, save_flags._echo);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530140 }
141 } else if (ch == killc) {
142 while (str > oldstr) {
micky3879b9f5e72025-07-08 18:04:53 -0400143 str = WipeOut(win, y, x, oldstr, str, save_flags._echo);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530144 }
145 } else if (ch >= KEY_MIN
micky3879b9f5e72025-07-08 18:04:53 -0400146 || (str - oldstr >= maxlen)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100147 NCURSES_SP_NAME(beep) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530148 } else {
149 *str++ = (char) ch;
micky3879b9f5e72025-07-08 18:04:53 -0400150 if (save_flags._echo == TRUE) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530151 int oldy = win->_cury;
152 if (waddch(win, (chtype) ch) == ERR) {
153 /*
154 * We can't really use the lower-right
155 * corner for input, since it'll mess
156 * up bookkeeping for erases.
157 */
158 win->_flags &= ~_WRAPPED;
159 waddch(win, (chtype) ' ');
micky3879b9f5e72025-07-08 18:04:53 -0400160 str = WipeOut(win, y, x, oldstr, str, save_flags._echo);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530161 continue;
micky3879b9f5e72025-07-08 18:04:53 -0400162 } else if (IS_WRAPPED(win)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530163 /*
164 * If the last waddch forced a wrap &
165 * scroll, adjust our reference point
166 * for erasures.
167 */
168 if (win->_scroll
169 && oldy == win->_maxy
170 && win->_cury == win->_maxy) {
171 if (--y <= 0) {
172 y = 0;
173 }
174 }
175 win->_flags &= ~_WRAPPED;
176 }
177 wrefresh(win);
178 }
179 }
180 }
181
182 win->_curx = 0;
183 win->_flags &= ~_WRAPPED;
184 if (win->_cury < win->_maxy)
185 win->_cury++;
186 wrefresh(win);
187
188 /* Restore with a single I/O call, to fix minor asymmetry between
189 * raw/noraw, etc.
190 */
micky3879b9f5e72025-07-08 18:04:53 -0400191 sp->_tty_flags = save_flags;
Steve Kondikae271bc2015-11-15 02:50:53 +0100192 NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530193
194 *str = '\0';
195 if (ch == ERR)
196 returnCode(ch);
197
198 T(("wgetnstr returns %s", _nc_visbuf(oldstr)));
199
200#ifdef KEY_EVENT
201 if (ch == KEY_EVENT)
202 returnCode(ch);
203#endif
204#ifdef KEY_RESIZE
205 if (ch == KEY_RESIZE)
206 returnCode(ch);
207#endif
208
209 returnCode(OK);
210}
211
212#ifdef NCURSES_WGETCH_EVENTS
213NCURSES_EXPORT(int)
214wgetnstr(WINDOW *win, char *str, int maxlen)
215{
216 returnCode(wgetnstr_events(win,
217 str,
218 maxlen,
219 EVENTLIST_1st((_nc_eventlist *) 0)));
220}
221#endif