1/* $OpenBSD: lib_get_wch.c,v 1.2 2023/10/17 09:52:09 nicm Exp $ */
2
3/****************************************************************************
4 * Copyright 2020,2021 Thomas E. Dickey                                     *
5 * Copyright 2002-2011,2016 Free Software Foundation, Inc.                  *
6 *                                                                          *
7 * Permission is hereby granted, free of charge, to any person obtaining a  *
8 * copy of this software and associated documentation files (the            *
9 * "Software"), to deal in the Software without restriction, including      *
10 * without limitation the rights to use, copy, modify, merge, publish,      *
11 * distribute, distribute with modifications, sublicense, and/or sell       *
12 * copies of the Software, and to permit persons to whom the Software is    *
13 * furnished to do so, subject to the following conditions:                 *
14 *                                                                          *
15 * The above copyright notice and this permission notice shall be included  *
16 * in all copies or substantial portions of the Software.                   *
17 *                                                                          *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25 *                                                                          *
26 * Except as contained in this notice, the name(s) of the above copyright   *
27 * holders shall not be used in advertising or otherwise to promote the     *
28 * sale, use or other dealings in this Software without prior written       *
29 * authorization.                                                           *
30 ****************************************************************************/
31
32/****************************************************************************
33 *  Author: Thomas E. Dickey 2002-on                                        *
34 ****************************************************************************/
35
36/*
37**	lib_get_wch.c
38**
39**	The routine get_wch().
40**
41*/
42
43#include <curses.priv.h>
44#include <ctype.h>
45
46MODULE_ID("$Id: lib_get_wch.c,v 1.2 2023/10/17 09:52:09 nicm Exp $")
47
48NCURSES_EXPORT(int)
49wget_wch(WINDOW *win, wint_t *result)
50{
51    SCREEN *sp;
52    int code;
53    int value = 0;
54#ifndef state_unused
55    mbstate_t state;
56#endif
57
58    T((T_CALLED("wget_wch(%p)"), (void *) win));
59
60    /*
61     * We can get a stream of single-byte characters and KEY_xxx codes from
62     * _nc_wgetch(), while we want to return a wide character or KEY_xxx code.
63     */
64    _nc_lock_global(curses);
65    sp = _nc_screen_of(win);
66
67    if (sp != 0) {
68	size_t count = 0;
69
70	for (;;) {
71	    char buffer[(MB_LEN_MAX * 9) + 1];	/* allow some redundant shifts */
72
73	    T(("reading %d of %d", (int) count + 1, (int) sizeof(buffer)));
74	    code = _nc_wgetch(win, &value, TRUE EVENTLIST_2nd((_nc_eventlist
75							       *) 0));
76	    if (code == ERR) {
77		break;
78	    } else if (code == KEY_CODE_YES) {
79		/*
80		 * If we were processing an incomplete multibyte character,
81		 * return an error since we have a KEY_xxx code which
82		 * interrupts it.  For some cases, we could improve this by
83		 * writing a new version of lib_getch.c(!), but it is not clear
84		 * whether the improvement would be worth the effort.
85		 */
86		if (count != 0) {
87		    safe_ungetch(SP_PARM, value);
88		    code = ERR;
89		}
90		break;
91	    } else if (count + 1 >= sizeof(buffer)) {
92		safe_ungetch(SP_PARM, value);
93		code = ERR;
94		break;
95	    } else {
96		int status;
97
98		buffer[count++] = (char) UChar(value);
99		reset_mbytes(state);
100		status = count_mbytes(buffer, count, state);
101		if (status >= 0) {
102		    wchar_t wch;
103		    reset_mbytes(state);
104		    if (check_mbytes(wch, buffer, count, state) != status) {
105			code = ERR;	/* the two calls should match */
106			safe_ungetch(SP_PARM, value);
107		    }
108		    value = wch;
109		    break;
110		}
111	    }
112	}
113    } else {
114	code = ERR;
115    }
116
117    if (result != 0)
118	*result = (wint_t) value;
119
120    _nc_unlock_global(curses);
121    T(("result %#o", value));
122    returnCode(code);
123}
124