Fix PROCDURE -> PROCEDURE.
[dragonfly.git] / contrib / ncurses-5.4 / ncurses / widechar / lib_get_wch.c
1 /****************************************************************************
2  * Copyright (c) 2002,2004 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: Thomas E. Dickey 2002,2004                                      *
31  ****************************************************************************/
32
33 /*
34 **      lib_get_wch.c
35 **
36 **      The routine get_wch().
37 **
38 */
39
40 #include <curses.priv.h>
41
42 MODULE_ID("$Id: lib_get_wch.c,v 1.6 2004/01/18 01:18:17 tom Exp $")
43
44 #if HAVE_MBTOWC && HAVE_MBLEN
45 #define reset_mbytes(state) mblen(NULL, 0), mbtowc(NULL, NULL, 0)
46 #define count_mbytes(buffer,length,state) mblen(buffer,length)
47 #define check_mbytes(wch,buffer,length,state) \
48         (int) mbtowc(&wch, buffer, count)
49 #elif HAVE_MBRTOWC && HAVE_MBRLEN
50 #define reset_mbytes(state) memset(&state, 0, sizeof(state))
51 #define count_mbytes(buffer,length,state) mbrlen(buffer,length,&state)
52 #define check_mbytes(wch,buffer,length,state) \
53         (int) mbrtowc(&wch, buffer, count, &state)
54 #else
55 make an error
56 #endif
57
58 NCURSES_EXPORT(int)
59 wget_wch(WINDOW *win, wint_t * result)
60 {
61     int code;
62     char buffer[(MB_LEN_MAX * 9) + 1];  /* allow some redundant shifts */
63     int status;
64     mbstate_t state;
65     size_t count = 0;
66     unsigned long value;
67     wchar_t wch;
68
69     T((T_CALLED("wget_wch(%p)"), win));
70     (void) state;
71
72     /*
73      * We can get a stream of single-byte characters and KEY_xxx codes from
74      * _nc_wgetch(), while we want to return a wide character or KEY_xxx code.
75      */
76     for (;;) {
77         T(("reading %d of %d", count + 1, sizeof(buffer)));
78         code = _nc_wgetch(win, &value, TRUE);
79         if (code == ERR) {
80             break;
81         } else if (code == KEY_CODE_YES) {
82             /*
83              * If we were processing an incomplete multibyte character, return
84              * an error since we have a KEY_xxx code which interrupts it.  For
85              * some cases, we could improve this by writing a new version of
86              * lib_getch.c(!), but it is not clear whether the improvement
87              * would be worth the effort.
88              */
89             if (count != 0) {
90                 ungetch(value);
91                 code = ERR;
92             }
93             break;
94         } else if (count + 1 >= sizeof(buffer)) {
95             ungetch(value);
96             code = ERR;
97             break;
98         } else {
99             buffer[count++] = UChar(value);
100             reset_mbytes(state);
101             status = count_mbytes(buffer, count, state);
102             if (status >= 0) {
103                 reset_mbytes(state);
104                 if (check_mbytes(wch, buffer, count, state) != status) {
105                     code = ERR; /* the two calls should match */
106                 }
107                 value = wch;
108                 break;
109             }
110         }
111     }
112     *result = value;
113     T(("result %#lo", value));
114     returnCode(code);
115 }