Merge upstream PDCurses

This commit is contained in:
casey langen 2020-04-01 09:46:32 -07:00
parent edd0e2cb9b
commit 7ff32bea9d
2 changed files with 28 additions and 21 deletions

View File

@ -1,6 +1,7 @@
/* PDCurses */ /* PDCurses */
#include <curspriv.h> #include <curspriv.h>
#include <assert.h>
/*man-start************************************************************** /*man-start**************************************************************
@ -227,48 +228,52 @@ wchar_t *wunctrl(cchar_t *wc)
return strbuf; return strbuf;
} }
#define IS_CONTINUATION_BYTE( c) (((c) & 0xc0) == 0x80)
int PDC_mbtowc(wchar_t *pwc, const char *s, size_t n) int PDC_mbtowc(wchar_t *pwc, const char *s, size_t n)
{ {
# ifdef PDC_FORCE_UTF8 # ifdef PDC_FORCE_UTF8
wchar_t key; wchar_t key;
int i = -1; int i = 0;
const unsigned char *string; const unsigned char *string;
assert( s);
if (!s || (n < 1)) if (!s || (n < 1))
return -1; return -1;
if (!*s)
return 0;
string = (const unsigned char *)s; string = (const unsigned char *)s;
key = string[0]; key = string[0];
/* Simplistic UTF-8 decoder -- only does the BMP, minimal validation */ /* Simplistic UTF-8 decoder -- a little validation */
if (key & 0x80) if ((key & 0xc0) == 0xc0 && IS_CONTINUATION_BYTE( string[1]))
{ {
if ((key & 0xe0) == 0xc0) if ((key & 0xe0) == 0xc0 && 1 < n)
{ {
if (1 < n) key = ((key & 0x1f) << 6) | (string[1] & 0x3f);
{ i = 2; /* two-byte sequence : U+0080 to U+07FF */
key = ((key & 0x1f) << 6) | (string[1] & 0x3f);
i = 2;
}
} }
else if ((key & 0xe0) == 0xe0) else if ((key & 0xf0) == 0xe0 && 2 < n
&& IS_CONTINUATION_BYTE( string[2]))
{ {
if (2 < n) key = ((key & 0x0f) << 12) | ((string[1] & 0x3f) << 6) |
{ (string[2] & 0x3f);
key = ((key & 0x0f) << 12) | ((string[1] & 0x3f) << 6) | i = 3; /* three-byte sequence : U+0800 to U+FFFF */
(string[2] & 0x3f); }
i = 3; else if ((key & 0xf8) == 0xf0 && 3 < n /* SMP: Unicode past 64K */
} && IS_CONTINUATION_BYTE( string[2])
&& IS_CONTINUATION_BYTE( string[3]))
{
key = ((key & 0x07) << 18) | ((string[1] & 0x3f) << 12) |
((string[2] & 0x3f) << 6) | (string[2] & 0x3f);
if( key <= 0x10ffff)
i = 4; /* four-byte sequence : U+10000 to U+10FFFF */
} }
} }
else else /* 'ordinary' 7-bit ASCII */
i = 1; i = 1;
assert( i);
if (i) if (i)
*pwc = key; *pwc = key;

View File

@ -1943,6 +1943,8 @@ static LRESULT ALIGN_STACK CALLBACK WndProc (const HWND hwnd,
break; break;
case WM_CHAR: /* _Don't_ add Shift-Tab; it's handled elsewhere */ case WM_CHAR: /* _Don't_ add Shift-Tab; it's handled elsewhere */
if( wParam == 3 && !SP->raw_inp) /* Ctrl-C hit */
exit( 0);
if( wParam != 9 || !(GetKeyState( VK_SHIFT) & 0x8000)) if( wParam != 9 || !(GetKeyState( VK_SHIFT) & 0x8000))
if( !key_already_handled || last_key_handled != (int)wParam ) if( !key_already_handled || last_key_handled != (int)wParam )
add_key_to_queue( (int)wParam ); add_key_to_queue( (int)wParam );