Author: Eric S. Raymond <esr@snark.thyrsus.com> 1993
Thomas E. Dickey (beginning revision 1.27 in 1996).
-$Id: ncurses.c,v 1.324 2008/08/04 16:27:54 tom Exp $
+$Id: ncurses.c,v 1.332 2008/11/29 20:08:42 tom Exp $
***************************************************************************/
#endif
+#if HAVE_WCSRTOMBS
+#define count_wchars(src, len, state) wcsrtombs(0, &src, len, state)
+#define trans_wchars(dst, src, len, state) wcsrtombs(dst, &src, len, state)
+#define reset_wchars(state) memset(&state, 0, sizeof(state))
+#elif HAVE_WCSTOMBS && HAVE_MBTOWC && HAVE_MBLEN
+#define count_wchars(src, len, state) wcstombs(0, src, len)
+#define trans_wchars(dst, src, len, state) wcstombs(dst, src, len)
+#define reset_wchars(state) mblen(NULL, 0), mbtowc(NULL, NULL, 0)
+#define state_unused
+#endif
+
+#if HAVE_MBSRTOWCS
+#define count_mbytes(src, len, state) mbsrtowcs(0, &src, len, state)
+#define trans_mbytes(dst, src, len, state) mbsrtowcs(dst, &src, len, state)
+#define reset_mbytes(state) memset(&state, 0, sizeof(state))
+#elif HAVE_MBSTOWCS && HAVE_MBTOWC && HAVE_MBLEN
+#define count_mbytes(src, len, state) mbstowcs(0, src, len)
+#define trans_mbytes(dst, src, len, state) mbstowcs(dst, src, len)
+#define reset_mbytes(state) mblen(NULL, 0), mbtowc(NULL, NULL, 0)
+#define state_unused
+#endif
+
#define ToggleAcs(temp,real) temp = ((temp == real) ? 0 : real)
#define P(string) printw("%s\n", string)
*
****************************************************************************/
+#define NUM_GETCH_FLAGS 256
+typedef bool GetchFlags[NUM_GETCH_FLAGS];
+
static void
-setup_getch(WINDOW *win, bool flags[])
+setup_getch(WINDOW *win, GetchFlags flags)
{
keypad(win, flags['k']); /* should be redundant, but for testing */
meta(win, flags['m']); /* force this to a known state */
}
static void
-wgetch_help(WINDOW *win, bool flags[])
+init_getch(WINDOW *win, GetchFlags flags)
+{
+ memset(flags, FALSE, NUM_GETCH_FLAGS);
+ flags[UChar('k')] = (win == stdscr);
+ flags[UChar('m')] = TRUE;
+
+ setup_getch(win, flags);
+}
+
+static void
+wgetch_help(WINDOW *win, GetchFlags flags)
{
static const char *help[] =
{
int first_y, first_x;
int c;
int incount = 0;
- bool flags[256];
+ GetchFlags flags;
bool blocking = (delay < 0);
- memset(flags, FALSE, sizeof(flags));
- flags[UChar('k')] = (win == stdscr);
-
- setup_getch(win, flags);
+ init_getch(win, flags);
wtimeout(win, delay);
getyx(win, first_y, first_x);
}
#endif
(void) waddstr(win, keyname(c));
- } else if (c > 0x80) {
- unsigned c2 = (unsigned) (c & 0x7f);
- if (isprint(c2))
- (void) wprintw(win, "M-%c", UChar(c2));
- else
+ } else if (c >= 0x80) {
+ unsigned c2 = (unsigned) c;
+#if !(defined(NCURSES_VERSION) || defined(_XOPEN_CURSES))
+ /* at least Solaris SVR4 curses breaks unctrl(128), etc. */
+ c2 &= 0x7f;
+#endif
+ if (isprint(c))
+ (void) wprintw(win, "%c", UChar(c));
+ else if (c2 != UChar(c))
(void) wprintw(win, "M-%s", unctrl(c2));
+ else
+ (void) wprintw(win, "%s", unctrl(c2));
waddstr(win, " (high-half character)");
} else {
if (isprint(c))
}
wtimeout(win, -1);
+
+ if (!level)
+ init_getch(win, flags);
}
static int
wcstos(const wchar_t *src)
{
int need;
- mbstate_t state;
char *result = 0;
const wchar_t *tmp = src;
+#ifndef state_unused
+ mbstate_t state;
+#endif
- memset(&state, 0, sizeof(state));
- if ((need = (int) wcsrtombs(0, &tmp, 0, &state)) > 0) {
+ reset_wchars(state);
+ if ((need = (int) count_wchars(tmp, 0, &state)) > 0) {
unsigned have = (unsigned) need;
if ((result = typeCalloc(char, have + 1)) != 0) {
tmp = src;
- if (wcsrtombs(result, &tmp, have, &state) != have) {
+ if (trans_wchars(result, tmp, have, &state) != have) {
free(result);
result = 0;
}
int first_y, first_x;
wint_t c;
int incount = 0;
- bool flags[256];
+ GetchFlags flags;
bool blocking = (delay < 0);
int y, x, code;
char *temp;
- memset(flags, FALSE, sizeof(flags));
- flags[UChar('k')] = (win == stdscr);
-
- setup_getch(win, flags);
+ init_getch(win, flags);
wtimeout(win, delay);
getyx(win, first_y, first_x);
resize_wide_boxes(level, win);
}
#endif
- (void) waddstr(win, key_name((wchar_t) c));
+ (void) waddstr(win, keyname((wchar_t) c));
} else {
+ (void) waddstr(win, key_name((wchar_t) c));
if (c < 256 && iscntrl(c)) {
- (void) wprintw(win, "%s (control character)", unctrl(c));
+ (void) wprintw(win, " (control character)");
} else {
- wchar_t c2 = (wchar_t) c;
- waddnwstr(win, &c2, 1);
- (void) wprintw(win, " = %#x (printable character)", c);
+ (void) wprintw(win, " = %#x (printable character)",
+ (unsigned) c);
}
}
wgetch_wrap(win, first_y);
}
wtimeout(win, -1);
+
+ if (!level)
+ init_getch(win, flags);
}
static void
#if HAVE_SLK_COLOR
,"F/B -- cycle through foreground/background colors"
#endif
- ,"ESC -- return to main menu"
+ ,"ESC -- return to main menu"
,""
,"Note: if activating the soft keys causes your terminal to scroll up"
,"one line, your terminal auto-scrolls when anything is written to the"
size_t used = strlen(temp);
size_t want = SLKLEN;
size_t test;
+#ifndef state_unused
mbstate_t state;
+#endif
buf[0] = L'\0';
while (want > 0 && used != 0) {
const char *base = s;
- memset(&state, 0, sizeof(state));
- test = mbsrtowcs(0, &base, 0, &state);
+ reset_mbytes(state);
+ test = count_mbytes(base, 0, &state);
if (test == (size_t) -1) {
temp[--used] = 0;
} else if (test > want) {
temp[--used] = 0;
} else {
- memset(&state, 0, sizeof(state));
- mbsrtowcs(buf, &base, want, &state);
+ reset_mbytes(state);
+ trans_mbytes(buf, base, want, &state);
break;
}
}
int col = (n / height) * COLS / 2;
mvprintw(row, col, "%*s : ", COLS / 4, name);
- while (repeat-- >= 0) {
+ while (--repeat >= 0) {
add_wch(code);
}
return n + 1;