Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright (c) 2002-2007,2008 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.17 2008/08/16 19:22:55 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 | SCREEN *sp; |
| 64 | int code; |
| 65 | char buffer[(MB_LEN_MAX * 9) + 1]; /* allow some redundant shifts */ |
| 66 | int status; |
| 67 | size_t count = 0; |
| 68 | unsigned long value; |
| 69 | wchar_t wch; |
| 70 | #ifndef state_unused |
| 71 | mbstate_t state; |
| 72 | #endif |
| 73 | |
| 74 | T((T_CALLED("wget_wch(%p)"), win)); |
| 75 | |
| 76 | /* |
| 77 | * We can get a stream of single-byte characters and KEY_xxx codes from |
| 78 | * _nc_wgetch(), while we want to return a wide character or KEY_xxx code. |
| 79 | */ |
| 80 | _nc_lock_global(curses); |
| 81 | sp = _nc_screen_of(win); |
| 82 | if (sp != 0) { |
| 83 | for (;;) { |
| 84 | T(("reading %d of %d", (int) count + 1, (int) sizeof(buffer))); |
| 85 | code = _nc_wgetch(win, &value, TRUE EVENTLIST_2nd((_nc_eventlist |
| 86 | *) 0)); |
| 87 | if (code == ERR) { |
| 88 | break; |
| 89 | } else if (code == KEY_CODE_YES) { |
| 90 | /* |
| 91 | * If we were processing an incomplete multibyte character, |
| 92 | * return an error since we have a KEY_xxx code which |
| 93 | * interrupts it. For some cases, we could improve this by |
| 94 | * writing a new version of lib_getch.c(!), but it is not clear |
| 95 | * whether the improvement would be worth the effort. |
| 96 | */ |
| 97 | if (count != 0) { |
| 98 | _nc_ungetch(sp, (int) value); |
| 99 | code = ERR; |
| 100 | } |
| 101 | break; |
| 102 | } else if (count + 1 >= sizeof(buffer)) { |
| 103 | _nc_ungetch(sp, (int) value); |
| 104 | code = ERR; |
| 105 | break; |
| 106 | } else { |
| 107 | buffer[count++] = (char) UChar(value); |
| 108 | reset_mbytes(state); |
| 109 | status = count_mbytes(buffer, count, state); |
| 110 | if (status >= 0) { |
| 111 | reset_mbytes(state); |
| 112 | if (check_mbytes(wch, buffer, count, state) != status) { |
| 113 | code = ERR; /* the two calls should match */ |
| 114 | _nc_ungetch(sp, (int) value); |
| 115 | } |
| 116 | value = wch; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } else { |
| 122 | code = ERR; |
| 123 | } |
| 124 | *result = value; |
| 125 | _nc_unlock_global(curses); |
| 126 | T(("result %#lo", value)); |
| 127 | returnCode(code); |
| 128 | } |