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