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