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

Line Branch Exec Source
1
/* $OpenBSD: lib_get_wch.c,v 1.1 2010/09/06 17:26:17 nicm Exp $ */
2
3
/****************************************************************************
4
 * Copyright (c) 2002-2007,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 2002-on                                        *
33
 ****************************************************************************/
34
35
/*
36
**	lib_get_wch.c
37
**
38
**	The routine get_wch().
39
**
40
*/
41
42
#include <curses.priv.h>
43
#include <ctype.h>
44
45
MODULE_ID("$Id: lib_get_wch.c,v 1.1 2010/09/06 17:26:17 nicm Exp $")
46
47
#if HAVE_MBTOWC && HAVE_MBLEN
48
#define reset_mbytes(state) mblen(NULL, 0), mbtowc(NULL, NULL, 0)
49
#define count_mbytes(buffer,length,state) mblen(buffer,length)
50
#define check_mbytes(wch,buffer,length,state) \
51
	(int) mbtowc(&wch, buffer, length)
52
#define state_unused
53
#elif HAVE_MBRTOWC && HAVE_MBRLEN
54
#define reset_mbytes(state) init_mb(state)
55
#define count_mbytes(buffer,length,state) mbrlen(buffer,length,&state)
56
#define check_mbytes(wch,buffer,length,state) \
57
	(int) mbrtowc(&wch, buffer, length, &state)
58
#else
59
make an error
60
#endif
61
62
NCURSES_EXPORT(int)
63
wget_wch(WINDOW *win, wint_t *result)
64
{
65
    SCREEN *sp;
66
    int code;
67
    char buffer[(MB_LEN_MAX * 9) + 1];	/* allow some redundant shifts */
68
    int status;
69
    size_t count = 0;
70
    unsigned long value;
71
    wchar_t wch;
72
#ifndef state_unused
73
    mbstate_t state;
74
#endif
75
76
    T((T_CALLED("wget_wch(%p)"), win));
77
78
    /*
79
     * We can get a stream of single-byte characters and KEY_xxx codes from
80
     * _nc_wgetch(), while we want to return a wide character or KEY_xxx code.
81
     */
82
    _nc_lock_global(curses);
83
    sp = _nc_screen_of(win);
84
    if (sp != 0) {
85
	for (;;) {
86
	    T(("reading %d of %d", (int) count + 1, (int) sizeof(buffer)));
87
	    code = _nc_wgetch(win, &value, TRUE EVENTLIST_2nd((_nc_eventlist
88
							       *) 0));
89
	    if (code == ERR) {
90
		break;
91
	    } else if (code == KEY_CODE_YES) {
92
		/*
93
		 * If we were processing an incomplete multibyte character,
94
		 * return an error since we have a KEY_xxx code which
95
		 * interrupts it.  For some cases, we could improve this by
96
		 * writing a new version of lib_getch.c(!), but it is not clear
97
		 * whether the improvement would be worth the effort.
98
		 */
99
		if (count != 0) {
100
		    _nc_ungetch(sp, (int) value);
101
		    code = ERR;
102
		}
103
		break;
104
	    } else if (count + 1 >= sizeof(buffer)) {
105
		_nc_ungetch(sp, (int) value);
106
		code = ERR;
107
		break;
108
	    } else {
109
		buffer[count++] = (char) UChar(value);
110
		reset_mbytes(state);
111
		status = count_mbytes(buffer, count, state);
112
		if (status >= 0) {
113
		    reset_mbytes(state);
114
		    if (check_mbytes(wch, buffer, count, state) != status) {
115
			code = ERR;	/* the two calls should match */
116
			_nc_ungetch(sp, (int) value);
117
		    }
118
		    value = wch;
119
		    break;
120
		}
121
	    }
122
	}
123
    } else {
124
	code = ERR;
125
    }
126
    *result = value;
127
    _nc_unlock_global(curses);
128
    T(("result %#lo", value));
129
    returnCode(code);
130
}