]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libncurses/lib_getstr.c
Make bdev userland access work like cdev userland access unless
[FreeBSD/FreeBSD.git] / lib / libncurses / lib_getstr.c
1
2 /* This work is copyrighted. See COPYRIGHT.OLD & COPYRIGHT.NEW for   *
3 *  details. If they are missing then this copy is in violation of    *
4 *  the copyright conditions.                                        */
5
6 /*
7 **      lib_getstr.c
8 **
9 **      The routine wgetstr().
10 **
11 */
12
13 #include "curses.priv.h"
14 #include "unctrl.h"
15
16 inline void backspace(WINDOW *win)
17 {
18         mvwaddstr(curscr, win->_begy + win->_cury, win->_begx + win->_curx,
19                  "\b \b");
20         waddstr(win, "\b \b");
21         fputs("\b \b", SP->_ofp);
22         fflush(SP->_ofp);
23         SP->_curscol--;
24 }
25
26 int wgetnstr(WINDOW *win, char *str, int maxlen)
27 {
28 bool    oldnl, oldecho, oldraw, oldcbreak, oldkeypad;
29 char    erasec;
30 char    killc;
31 char    *oldstr;
32 int ch;
33
34         T(("wgetnstr(%x,%x, %d) called", win, str, maxlen));
35
36         oldnl = SP->_nl;
37         oldecho = SP->_echo;
38         oldraw = SP->_raw;
39         oldcbreak = SP->_cbreak;
40         oldkeypad = win->_use_keypad;
41         nl();
42         noecho();
43         noraw();
44         cbreak();
45         keypad(win, TRUE);
46
47         erasec = erasechar();
48         killc = killchar();
49
50         oldstr = str;
51
52         vidattr(win->_attrs);
53         if (is_wintouched(win) || (win->_flags & _HASMOVED))
54                 wrefresh(win);
55
56         while ((ch = wgetch(win)) != ERR) {
57                 if (ch == '\n' || ch == '\r')
58                         break;
59                 if (ch == erasec || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
60                         if (str > oldstr) {
61                                 str--;
62                                 backspace(win);
63                         }
64                 } else if (ch == killc) {
65                         while (str > oldstr) {
66                                 str--;
67                                 backspace(win);
68                         }
69                 } else if (maxlen >= 0 && str - oldstr >= maxlen) {
70                     beep();
71                 } else {
72                         mvwaddstr(curscr, win->_begy + win->_cury,
73                                   win->_begx + win->_curx, unctrl(ch));
74                         waddstr(win, unctrl(ch));
75                         if (oldecho == TRUE) {
76                                 fputs(unctrl(ch), SP->_ofp);
77                                 fflush(SP->_ofp);
78                         }
79                         SP->_curscol++;
80                         *str++ = ch;
81                 }
82         }
83
84         win->_curx = 0;
85         if (win->_cury < win->_maxy)
86                 win->_cury++;
87         wrefresh(win);
88
89         if (oldnl == FALSE)
90             nonl();
91
92         if (oldecho == TRUE)
93             echo();
94
95         if (oldraw == TRUE)
96             raw();
97
98         if (oldcbreak == FALSE)
99             nocbreak();
100
101         if (oldkeypad == FALSE)
102                 keypad(win, FALSE);
103
104         if (ch == ERR) {
105                 *str = '\0';
106                 return ERR;
107         }
108         *str = '\0';
109
110         T(("wgetnstr returns \"%s\"", visbuf(oldstr)));
111
112         return(OK);
113 }