GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcurses/widechar/lib_get_wstr.c Lines: 0 98 0.0 %
Date: 2017-11-07 Branches: 0 78 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: lib_get_wstr.c,v 1.1 2010/09/06 17:26:17 nicm Exp $ */
2
3
/****************************************************************************
4
 * Copyright (c) 2002-2004,2008 Free Software Foundation, Inc.              *
5
 *                                                                          *
6
 * Permission is hereby granted, free of charge, to any person obtaining a  *
7
 * copy of this software and associated documentation files (the            *
8
 * "Software"), to deal in the Software without restriction, including      *
9
 * without limitation the rights to use, copy, modify, merge, publish,      *
10
 * distribute, distribute with modifications, sublicense, and/or sell       *
11
 * copies of the Software, and to permit persons to whom the Software is    *
12
 * furnished to do so, subject to the following conditions:                 *
13
 *                                                                          *
14
 * The above copyright notice and this permission notice shall be included  *
15
 * in all copies or substantial portions of the Software.                   *
16
 *                                                                          *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24
 *                                                                          *
25
 * Except as contained in this notice, the name(s) of the above copyright   *
26
 * holders shall not be used in advertising or otherwise to promote the     *
27
 * sale, use or other dealings in this Software without prior written       *
28
 * authorization.                                                           *
29
 ****************************************************************************/
30
31
/****************************************************************************
32
 *  Author: Thomas E. Dickey                                                *
33
 ****************************************************************************/
34
35
/*
36
**	lib_get_wstr.c
37
**
38
**	The routine wgetn_wstr().
39
**
40
*/
41
42
#include <curses.priv.h>
43
#include <term.h>
44
45
MODULE_ID("$Id: lib_get_wstr.c,v 1.1 2010/09/06 17:26:17 nicm Exp $")
46
47
static int
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, 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 *
64
WipeOut(WINDOW *win, int y, int x, wint_t *first, wint_t *last, bool 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)
89
wgetn_wstr(WINDOW *win, wint_t *str, int maxlen)
90
{
91
    SCREEN *sp = _nc_screen_of(win);
92
    TTY buf;
93
    bool oldnl, oldecho, oldraw, oldcbreak;
94
    wint_t erasec;
95
    wint_t killc;
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)"), win, str, maxlen));
102
103
    if (!win)
104
	returnCode(ERR);
105
106
    _nc_get_tty_mode(&buf);
107
108
    oldnl = sp->_nl;
109
    oldecho = sp->_echo;
110
    oldraw = sp->_raw;
111
    oldcbreak = sp->_cbreak;
112
    nl();
113
    noecho();
114
    noraw();
115
    cbreak();
116
117
    erasec = (wint_t) erasechar();
118
    killc = (wint_t) killchar();
119
120
    getyx(win, y, x);
121
122
    if (is_wintouched(win) || (win->_flags & _HASMOVED))
123
	wrefresh(win);
124
125
    while ((code = wget_wch(win, &ch)) != ERR) {
126
	/*
127
	 * Map special characters into key-codes.
128
	 */
129
	if (ch == '\r')
130
	    ch = '\n';
131
	if (ch == '\n') {
132
	    code = KEY_CODE_YES;
133
	    ch = KEY_ENTER;
134
	}
135
	if (ch < KEY_MIN) {
136
	    if (ch == erasec) {
137
		ch = KEY_BACKSPACE;
138
		code = KEY_CODE_YES;
139
	    }
140
	    if (ch == killc) {
141
		ch = KEY_EOL;
142
		code = KEY_CODE_YES;
143
	    }
144
	}
145
	if (code == KEY_CODE_YES) {
146
	    /*
147
	     * Some terminals (the Wyse-50 is the most common) generate a \n
148
	     * from the down-arrow key.  With this logic, it's the user's
149
	     * choice whether to set kcud=\n for wget_wch(); terminating
150
	     * *getn_wstr() with \n should work either way.
151
	     */
152
	    if (ch == KEY_DOWN || ch == KEY_ENTER) {
153
		if (oldecho == TRUE
154
		    && win->_cury == win->_maxy
155
		    && win->_scroll)
156
		    wechochar(win, (chtype) '\n');
157
		break;
158
	    }
159
	    if (ch == KEY_LEFT || ch == KEY_BACKSPACE) {
160
		if (tmpstr > oldstr) {
161
		    tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
162
		}
163
	    } else if (ch == KEY_EOL) {
164
		while (tmpstr > oldstr) {
165
		    tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
166
		}
167
	    } else {
168
		beep();
169
	    }
170
	} else if (maxlen >= 0 && tmpstr - oldstr >= maxlen) {
171
	    beep();
172
	} else {
173
	    *tmpstr++ = ch;
174
	    *tmpstr = 0;
175
	    if (oldecho == TRUE) {
176
		int oldy = win->_cury;
177
178
		if (wadd_wint(win, tmpstr - 1) == ERR) {
179
		    /*
180
		     * We can't really use the lower-right corner for input,
181
		     * since it'll mess up bookkeeping for erases.
182
		     */
183
		    win->_flags &= ~_WRAPPED;
184
		    waddch(win, (chtype) ' ');
185
		    tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
186
		    continue;
187
		} else if (win->_flags & _WRAPPED) {
188
		    /*
189
		     * If the last waddch forced a wrap & scroll, adjust our
190
		     * reference point for erasures.
191
		     */
192
		    if (win->_scroll
193
			&& oldy == win->_maxy
194
			&& win->_cury == win->_maxy) {
195
			if (--y <= 0) {
196
			    y = 0;
197
			}
198
		    }
199
		    win->_flags &= ~_WRAPPED;
200
		}
201
		wrefresh(win);
202
	    }
203
	}
204
    }
205
206
    win->_curx = 0;
207
    win->_flags &= ~_WRAPPED;
208
    if (win->_cury < win->_maxy)
209
	win->_cury++;
210
    wrefresh(win);
211
212
    /* Restore with a single I/O call, to fix minor asymmetry between
213
     * raw/noraw, etc.
214
     */
215
    sp->_nl = oldnl;
216
    sp->_echo = oldecho;
217
    sp->_raw = oldraw;
218
    sp->_cbreak = oldcbreak;
219
220
    (void) _nc_set_tty_mode(&buf);
221
222
    *tmpstr = 0;
223
    if (code == ERR) {
224
	if (tmpstr == oldstr) {
225
	    *tmpstr++ = WEOF;
226
	    *tmpstr = 0;
227
	}
228
	returnCode(ERR);
229
    }
230
231
    T(("wgetn_wstr returns %s", _nc_viswibuf(oldstr)));
232
233
    returnCode(OK);
234
}