Back to index

tetex-bin  3.0
lib_get_wch.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 2002,2004 Free Software Foundation, Inc.                   *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Thomas E. Dickey 2002,2004                                      *
00031  ****************************************************************************/
00032 
00033 /*
00034 **     lib_get_wch.c
00035 **
00036 **     The routine get_wch().
00037 **
00038 */
00039 
00040 #include <curses.priv.h>
00041 #include <ctype.h>
00042 
00043 MODULE_ID("$Id: lib_get_wch.c,v 1.9 2004/09/19 01:13:44 tom Exp $")
00044 
00045 #if HAVE_MBTOWC && HAVE_MBLEN
00046 #define reset_mbytes(state) mblen(NULL, 0), mbtowc(NULL, NULL, 0)
00047 #define count_mbytes(buffer,length,state) mblen(buffer,length)
00048 #define check_mbytes(wch,buffer,length,state) \
00049        (int) mbtowc(&wch, buffer, length)
00050 #elif HAVE_MBRTOWC && HAVE_MBRLEN
00051 #define reset_mbytes(state) memset(&state, 0, sizeof(state))
00052 #define count_mbytes(buffer,length,state) mbrlen(buffer,length,&state)
00053 #define check_mbytes(wch,buffer,length,state) \
00054        (int) mbrtowc(&wch, buffer, length, &state)
00055 #else
00056 make an error
00057 #endif
00058 
00059 NCURSES_EXPORT(int)
00060 wget_wch(WINDOW *win, wint_t * result)
00061 {
00062     int code;
00063     char buffer[(MB_LEN_MAX * 9) + 1];    /* allow some redundant shifts */
00064     int status;
00065     mbstate_t state;
00066     size_t count = 0;
00067     unsigned long value;
00068     wchar_t wch;
00069 
00070     T((T_CALLED("wget_wch(%p)"), win));
00071     (void) state;
00072 
00073     /*
00074      * We can get a stream of single-byte characters and KEY_xxx codes from
00075      * _nc_wgetch(), while we want to return a wide character or KEY_xxx code.
00076      */
00077     for (;;) {
00078        T(("reading %d of %d", count + 1, sizeof(buffer)));
00079        code = _nc_wgetch(win, &value, TRUE);
00080        if (code == ERR) {
00081            break;
00082        } else if (code == KEY_CODE_YES) {
00083            /*
00084             * If we were processing an incomplete multibyte character, return
00085             * an error since we have a KEY_xxx code which interrupts it.  For
00086             * some cases, we could improve this by writing a new version of
00087             * lib_getch.c(!), but it is not clear whether the improvement
00088             * would be worth the effort.
00089             */
00090            if (count != 0) {
00091               ungetch(value);
00092               code = ERR;
00093            }
00094            break;
00095        } else if (count + 1 >= sizeof(buffer)) {
00096            ungetch(value);
00097            code = ERR;
00098            break;
00099        } else {
00100            buffer[count++] = UChar(value);
00101            reset_mbytes(state);
00102            status = count_mbytes(buffer, count, state);
00103            if (status >= 0) {
00104               reset_mbytes(state);
00105               if (check_mbytes(wch, buffer, count, state) != status) {
00106                   code = ERR;      /* the two calls should match */
00107                   ungetch(value);
00108               }
00109               value = wch;
00110               break;
00111            }
00112        }
00113     }
00114     *result = value;
00115     T(("result %#lo", value));
00116     returnCode(code);
00117 }