2019-12-29 01:54:02 -07:00
|
|
|
/* $XTermId: ptydata.c,v 1.121 2019/09/18 23:28:41 tom Exp $ */
|
2011-09-17 05:51:57 -06:00
|
|
|
|
|
|
|
/*
|
2019-12-29 01:54:02 -07:00
|
|
|
* Copyright 1999-2018,2019 by Thomas E. Dickey
|
2011-09-17 05:51:57 -06:00
|
|
|
*
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* Except as contained in this notice, the name(s) of the above copyright
|
|
|
|
* holders shall not be used in advertising or otherwise to promote the
|
|
|
|
* sale, use or other dealings in this Software without prior written
|
|
|
|
* authorization.
|
|
|
|
*/
|
2006-11-26 04:11:12 -07:00
|
|
|
|
|
|
|
#include <data.h>
|
|
|
|
|
|
|
|
#if OPT_WIDE_CHARS
|
|
|
|
#include <menu.h>
|
2017-07-15 13:20:51 -06:00
|
|
|
#include <wcwidth.h>
|
2006-11-26 04:11:12 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for both EAGAIN and EWOULDBLOCK, because some supposedly POSIX
|
|
|
|
* systems are broken and return EWOULDBLOCK when they should return EAGAIN.
|
|
|
|
* Note that this macro may evaluate its argument more than once.
|
|
|
|
*/
|
|
|
|
#if defined(EAGAIN) && defined(EWOULDBLOCK)
|
|
|
|
#define E_TEST(err) ((err) == EAGAIN || (err) == EWOULDBLOCK)
|
|
|
|
#else
|
|
|
|
#ifdef EAGAIN
|
|
|
|
#define E_TEST(err) ((err) == EAGAIN)
|
|
|
|
#else
|
|
|
|
#define E_TEST(err) ((err) == EWOULDBLOCK)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if OPT_WIDE_CHARS
|
|
|
|
/*
|
|
|
|
* Convert the 8-bit codes in data->buffer[] into Unicode in data->utf_data.
|
|
|
|
* The number of bytes converted will be nonzero iff there is data.
|
|
|
|
*/
|
2007-06-15 13:29:55 -06:00
|
|
|
Bool
|
2014-07-14 02:30:10 -06:00
|
|
|
decodeUtf8(TScreen *screen, PtyData *data)
|
2006-11-26 04:11:12 -07:00
|
|
|
{
|
|
|
|
int i;
|
2010-05-08 06:20:46 -06:00
|
|
|
int length = (int) (data->last - data->next);
|
2006-11-26 04:11:12 -07:00
|
|
|
int utf_count = 0;
|
2009-09-05 09:58:09 -06:00
|
|
|
unsigned utf_char = 0;
|
2006-11-26 04:11:12 -07:00
|
|
|
|
|
|
|
data->utf_size = 0;
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
unsigned c = data->next[i];
|
|
|
|
|
|
|
|
/* Combine UTF-8 into Unicode */
|
|
|
|
if (c < 0x80) {
|
|
|
|
/* We received an ASCII character */
|
|
|
|
if (utf_count > 0) {
|
|
|
|
data->utf_data = UCS_REPL; /* prev. sequence incomplete */
|
2011-03-07 13:41:27 -07:00
|
|
|
data->utf_size = i;
|
2006-11-26 04:11:12 -07:00
|
|
|
} else {
|
2009-09-05 09:58:09 -06:00
|
|
|
data->utf_data = (IChar) c;
|
2006-11-26 04:11:12 -07:00
|
|
|
data->utf_size = 1;
|
|
|
|
}
|
|
|
|
break;
|
2019-02-24 04:41:42 -07:00
|
|
|
} else if (screen->vt100_graphics
|
|
|
|
&& (c < 0x100)
|
|
|
|
&& (utf_count == 0)
|
|
|
|
&& screen->gsets[(int) screen->curgr] != nrc_ASCII) {
|
|
|
|
data->utf_data = (IChar) c;
|
|
|
|
data->utf_size = 1;
|
|
|
|
break;
|
2006-11-26 04:11:12 -07:00
|
|
|
} else if (c < 0xc0) {
|
|
|
|
/* We received a continuation byte */
|
|
|
|
if (utf_count < 1) {
|
|
|
|
/*
|
|
|
|
* We received a continuation byte before receiving a sequence
|
|
|
|
* state. Or an attempt to use a C1 control string. Either
|
2014-07-14 02:30:10 -06:00
|
|
|
* way, it is mapped to the replacement character, unless
|
|
|
|
* allowed by optional feature.
|
2006-11-26 04:11:12 -07:00
|
|
|
*/
|
2014-07-14 02:30:10 -06:00
|
|
|
data->utf_data = (IChar) (screen->c1_printable ? c : UCS_REPL);
|
2006-11-26 04:11:12 -07:00
|
|
|
data->utf_size = (i + 1);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* Check for overlong UTF-8 sequences for which a shorter
|
|
|
|
* encoding would exist and replace them with UCS_REPL.
|
|
|
|
* An overlong UTF-8 sequence can have any of the following
|
|
|
|
* forms:
|
|
|
|
* 1100000x 10xxxxxx
|
|
|
|
* 11100000 100xxxxx 10xxxxxx
|
|
|
|
* 11110000 1000xxxx 10xxxxxx 10xxxxxx
|
|
|
|
* 11111000 10000xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
|
|
|
* 11111100 100000xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
|
|
|
*/
|
|
|
|
if (!utf_char && !((c & 0x7f) >> (7 - utf_count))) {
|
|
|
|
utf_char = UCS_REPL;
|
|
|
|
}
|
2008-03-19 15:15:46 -06:00
|
|
|
utf_char <<= 6;
|
|
|
|
utf_char |= (c & 0x3f);
|
2006-11-26 04:11:12 -07:00
|
|
|
if ((utf_char >= 0xd800 &&
|
|
|
|
utf_char <= 0xdfff) ||
|
|
|
|
(utf_char == 0xfffe) ||
|
2007-08-25 12:53:27 -06:00
|
|
|
(utf_char == HIDDEN_CHAR)) {
|
2006-11-26 04:11:12 -07:00
|
|
|
utf_char = UCS_REPL;
|
|
|
|
}
|
|
|
|
utf_count--;
|
|
|
|
if (utf_count == 0) {
|
2009-09-05 09:58:09 -06:00
|
|
|
#if !OPT_WIDER_ICHAR
|
2008-03-19 15:15:46 -06:00
|
|
|
/* characters outside UCS-2 become UCS_REPL */
|
2019-12-29 01:54:02 -07:00
|
|
|
if (utf_char > NARROW_ICHAR) {
|
2008-03-19 15:15:46 -06:00
|
|
|
TRACE(("using replacement for %#x\n", utf_char));
|
|
|
|
utf_char = UCS_REPL;
|
|
|
|
}
|
2009-09-05 09:58:09 -06:00
|
|
|
#endif
|
|
|
|
data->utf_data = (IChar) utf_char;
|
2006-11-26 04:11:12 -07:00
|
|
|
data->utf_size = (i + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* We received a sequence start byte */
|
|
|
|
if (utf_count > 0) {
|
|
|
|
data->utf_data = UCS_REPL; /* prev. sequence incomplete */
|
|
|
|
data->utf_size = (i + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c < 0xe0) {
|
|
|
|
utf_count = 1;
|
|
|
|
utf_char = (c & 0x1f);
|
2011-03-07 13:41:27 -07:00
|
|
|
if (!(c & 0x1e)) {
|
2006-11-26 04:11:12 -07:00
|
|
|
utf_char = UCS_REPL; /* overlong sequence */
|
2011-03-07 13:41:27 -07:00
|
|
|
}
|
2006-11-26 04:11:12 -07:00
|
|
|
} else if (c < 0xf0) {
|
|
|
|
utf_count = 2;
|
|
|
|
utf_char = (c & 0x0f);
|
|
|
|
} else if (c < 0xf8) {
|
|
|
|
utf_count = 3;
|
|
|
|
utf_char = (c & 0x07);
|
|
|
|
} else if (c < 0xfc) {
|
|
|
|
utf_count = 4;
|
|
|
|
utf_char = (c & 0x03);
|
|
|
|
} else if (c < 0xfe) {
|
|
|
|
utf_count = 5;
|
|
|
|
utf_char = (c & 0x01);
|
|
|
|
} else {
|
|
|
|
data->utf_data = UCS_REPL;
|
|
|
|
data->utf_size = (i + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if OPT_TRACE > 1
|
|
|
|
TRACE(("UTF-8 char %04X [%d..%d]\n",
|
|
|
|
data->utf_data,
|
2014-07-14 02:30:10 -06:00
|
|
|
(int) (data->next - data->buffer),
|
|
|
|
(int) (data->next - data->buffer + data->utf_size - 1)));
|
2006-11-26 04:11:12 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return (data->utf_size != 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
2014-07-14 02:30:10 -06:00
|
|
|
readPtyData(XtermWidget xw, PtySelect * select_mask, PtyData *data)
|
2006-11-26 04:11:12 -07:00
|
|
|
{
|
2010-08-22 07:38:11 -06:00
|
|
|
TScreen *screen = TScreenOf(xw);
|
2006-11-26 04:11:12 -07:00
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
#ifdef VMS
|
|
|
|
if (*select_mask & pty_mask) {
|
2010-08-22 07:38:11 -06:00
|
|
|
trimPtyData(xw, data);
|
2006-11-26 04:11:12 -07:00
|
|
|
if (read_queue.flink != 0) {
|
|
|
|
size = tt_read(data->next);
|
|
|
|
if (size == 0) {
|
|
|
|
Panic("input: read returned zero\n", 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sys$hiber();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* !VMS */
|
|
|
|
if (FD_ISSET(screen->respond, select_mask)) {
|
2009-09-05 09:58:09 -06:00
|
|
|
int save_err;
|
2010-08-22 07:38:11 -06:00
|
|
|
trimPtyData(xw, data);
|
2006-11-26 04:11:12 -07:00
|
|
|
|
2010-05-08 06:20:46 -06:00
|
|
|
size = (int) read(screen->respond, (char *) data->last, (size_t) FRG_SIZE);
|
2009-09-05 09:58:09 -06:00
|
|
|
save_err = errno;
|
2006-11-26 04:11:12 -07:00
|
|
|
#if (defined(i386) && defined(SVR4) && defined(sun)) || defined(__CYGWIN__)
|
2009-09-05 09:58:09 -06:00
|
|
|
/*
|
|
|
|
* Yes, I know this is a majorly f*ugly hack, however it seems to
|
|
|
|
* be necessary for Solaris x86. DWH 11/15/94
|
|
|
|
* Dunno why though..
|
|
|
|
* (and now CYGWIN, alanh@xfree86.org 08/15/01
|
|
|
|
*/
|
|
|
|
if (size <= 0) {
|
|
|
|
if (save_err == EIO || save_err == 0)
|
2013-03-09 07:45:38 -07:00
|
|
|
NormalExit();
|
2009-09-05 09:58:09 -06:00
|
|
|
else if (!E_TEST(save_err))
|
|
|
|
Panic("input: read returned unexpected error (%d)\n", save_err);
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
#else /* !f*ugly */
|
|
|
|
if (size < 0) {
|
|
|
|
if (save_err == EIO)
|
2013-03-09 07:45:38 -07:00
|
|
|
NormalExit();
|
2009-09-05 09:58:09 -06:00
|
|
|
else if (!E_TEST(save_err))
|
|
|
|
Panic("input: read returned unexpected error (%d)\n", save_err);
|
2006-11-26 04:11:12 -07:00
|
|
|
size = 0;
|
|
|
|
} else if (size == 0) {
|
2014-01-18 07:06:23 -07:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
2013-03-09 07:45:38 -07:00
|
|
|
NormalExit();
|
2006-11-26 04:11:12 -07:00
|
|
|
#else
|
|
|
|
Panic("input: read returned zero\n", 0);
|
|
|
|
#endif
|
|
|
|
}
|
2009-09-05 09:58:09 -06:00
|
|
|
#endif /* f*ugly */
|
2006-11-26 04:11:12 -07:00
|
|
|
}
|
|
|
|
#endif /* VMS */
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
#if OPT_TRACE
|
|
|
|
int i;
|
|
|
|
|
|
|
|
TRACE(("read %d bytes from pty\n", size));
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
if (!(i % 16))
|
|
|
|
TRACE(("%s", i ? "\n " : "READ"));
|
|
|
|
TRACE((" %02X", data->last[i]));
|
|
|
|
}
|
|
|
|
TRACE(("\n"));
|
|
|
|
#endif
|
|
|
|
data->last += size;
|
|
|
|
#ifdef ALLOWLOGGING
|
2010-03-25 16:03:51 -06:00
|
|
|
TScreenOf(term)->logstart = VTbuffer->next;
|
2006-11-26 04:11:12 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the next value from the input buffer. Note that morePtyData() is
|
|
|
|
* always called before this function, so we can do the UTF-8 input conversion
|
|
|
|
* in that function and simply return the result here.
|
|
|
|
*/
|
|
|
|
#if OPT_WIDE_CHARS
|
|
|
|
IChar
|
2014-07-14 02:30:10 -06:00
|
|
|
nextPtyData(TScreen *screen, PtyData *data)
|
2006-11-26 04:11:12 -07:00
|
|
|
{
|
|
|
|
IChar result;
|
|
|
|
if (screen->utf8_inparse) {
|
2019-02-24 04:41:42 -07:00
|
|
|
skipPtyData(data, result);
|
2006-11-26 04:11:12 -07:00
|
|
|
} else {
|
|
|
|
result = *((data)->next++);
|
2009-09-05 09:58:09 -06:00
|
|
|
if (!screen->output_eight_bits) {
|
|
|
|
result = (IChar) (result & 0x7f);
|
|
|
|
}
|
2006-11-26 04:11:12 -07:00
|
|
|
}
|
|
|
|
TRACE2(("nextPtyData returns %#x\n", result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if OPT_WIDE_CHARS
|
|
|
|
/*
|
|
|
|
* Called when UTF-8 mode has been turned on/off.
|
|
|
|
*/
|
|
|
|
void
|
2014-07-14 02:30:10 -06:00
|
|
|
switchPtyData(TScreen *screen, int flag)
|
2006-11-26 04:11:12 -07:00
|
|
|
{
|
|
|
|
if (screen->utf8_mode != flag) {
|
|
|
|
screen->utf8_mode = flag;
|
2009-03-09 16:13:04 -06:00
|
|
|
screen->utf8_inparse = (Boolean) (flag != 0);
|
2017-07-15 13:20:51 -06:00
|
|
|
mk_wcwidth_init(screen->utf8_mode);
|
2006-11-26 04:11:12 -07:00
|
|
|
|
|
|
|
TRACE(("turning UTF-8 mode %s\n", BtoS(flag)));
|
|
|
|
update_font_utf8_mode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-06-15 13:29:55 -06:00
|
|
|
/*
|
|
|
|
* Allocate a buffer.
|
|
|
|
*/
|
2006-11-26 04:11:12 -07:00
|
|
|
void
|
2014-07-14 02:30:10 -06:00
|
|
|
initPtyData(PtyData **result)
|
2006-11-26 04:11:12 -07:00
|
|
|
{
|
|
|
|
PtyData *data;
|
|
|
|
|
|
|
|
TRACE(("initPtyData given minBufSize %d, maxBufSize %d\n",
|
|
|
|
FRG_SIZE, BUF_SIZE));
|
|
|
|
|
|
|
|
if (FRG_SIZE < 64)
|
|
|
|
FRG_SIZE = 64;
|
|
|
|
if (BUF_SIZE < FRG_SIZE)
|
|
|
|
BUF_SIZE = FRG_SIZE;
|
|
|
|
if (BUF_SIZE % FRG_SIZE)
|
|
|
|
BUF_SIZE = BUF_SIZE + FRG_SIZE - (BUF_SIZE % FRG_SIZE);
|
|
|
|
|
|
|
|
TRACE(("initPtyData using minBufSize %d, maxBufSize %d\n",
|
|
|
|
FRG_SIZE, BUF_SIZE));
|
|
|
|
|
2011-03-07 13:41:27 -07:00
|
|
|
data = TypeXtMallocX(PtyData, (BUF_SIZE + FRG_SIZE));
|
2006-11-26 04:11:12 -07:00
|
|
|
|
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
data->next = data->buffer;
|
|
|
|
data->last = data->buffer;
|
|
|
|
*result = data;
|
|
|
|
}
|
|
|
|
|
2007-06-15 13:29:55 -06:00
|
|
|
/*
|
2010-03-25 16:03:51 -06:00
|
|
|
* Initialize a buffer for the caller, using its data in 'next'.
|
2007-06-15 13:29:55 -06:00
|
|
|
*/
|
|
|
|
#if OPT_WIDE_CHARS
|
|
|
|
PtyData *
|
2014-07-14 02:30:10 -06:00
|
|
|
fakePtyData(PtyData *result, Char *next, Char *last)
|
2007-06-15 13:29:55 -06:00
|
|
|
{
|
|
|
|
PtyData *data = result;
|
|
|
|
|
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
data->next = next;
|
|
|
|
data->last = last;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-11-26 04:11:12 -07:00
|
|
|
/*
|
|
|
|
* Remove used data by shifting the buffer down, to make room for more data,
|
|
|
|
* e.g., a continuation-read.
|
|
|
|
*/
|
|
|
|
void
|
2014-07-14 02:30:10 -06:00
|
|
|
trimPtyData(XtermWidget xw GCC_UNUSED, PtyData *data)
|
2006-11-26 04:11:12 -07:00
|
|
|
{
|
2010-08-22 07:38:11 -06:00
|
|
|
FlushLog(xw);
|
2006-11-26 04:11:12 -07:00
|
|
|
|
|
|
|
if (data->next != data->buffer) {
|
2016-08-07 15:27:36 -06:00
|
|
|
int i;
|
2010-05-08 06:20:46 -06:00
|
|
|
int n = (int) (data->last - data->next);
|
2006-11-26 04:11:12 -07:00
|
|
|
|
|
|
|
TRACE(("shifting buffer down by %d\n", n));
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
data->buffer[i] = data->next[i];
|
|
|
|
}
|
|
|
|
data->next = data->buffer;
|
|
|
|
data->last = data->next + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert new data into the input buffer so the next calls to morePtyData()
|
|
|
|
* and nextPtyData() will return that.
|
|
|
|
*/
|
|
|
|
void
|
2014-07-14 02:30:10 -06:00
|
|
|
fillPtyData(XtermWidget xw, PtyData *data, const char *value, int length)
|
2006-11-26 04:11:12 -07:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* remove the used portion of the buffer */
|
2010-08-22 07:38:11 -06:00
|
|
|
trimPtyData(xw, data);
|
2006-11-26 04:11:12 -07:00
|
|
|
|
|
|
|
VTbuffer->last += length;
|
2010-05-08 06:20:46 -06:00
|
|
|
size = (int) (VTbuffer->last - VTbuffer->next);
|
2006-11-26 04:11:12 -07:00
|
|
|
|
|
|
|
/* shift the unused portion up to make room */
|
|
|
|
for (n = size; n >= length; --n)
|
|
|
|
VTbuffer->next[n] = VTbuffer->next[n - length];
|
|
|
|
|
|
|
|
/* insert the new bytes to interpret */
|
|
|
|
for (n = 0; n < length; n++)
|
|
|
|
VTbuffer->next[n] = CharOf(value[n]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if OPT_WIDE_CHARS
|
2019-12-29 01:54:02 -07:00
|
|
|
/*
|
|
|
|
* Convert an ISO-8859-1 code 'c' to UTF-8, storing the result in the target
|
|
|
|
* 'lp', and returning a pointer past the converted character.
|
|
|
|
*/
|
2006-11-26 04:11:12 -07:00
|
|
|
Char *
|
2014-07-14 02:30:10 -06:00
|
|
|
convertToUTF8(Char *lp, unsigned c)
|
2006-11-26 04:11:12 -07:00
|
|
|
{
|
2010-03-25 16:03:51 -06:00
|
|
|
#define CH(n) (Char)((c) >> ((n) * 8))
|
|
|
|
if (c < 0x80) {
|
|
|
|
/* 0******* */
|
|
|
|
*lp++ = (Char) CH(0);
|
|
|
|
} else if (c < 0x800) {
|
|
|
|
/* 110***** 10****** */
|
|
|
|
*lp++ = (Char) (0xc0 | (CH(0) >> 6) | ((CH(1) & 0x07) << 2));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(0) & 0x3f));
|
|
|
|
} else if (c < 0x00010000) {
|
|
|
|
/* 1110**** 10****** 10****** */
|
|
|
|
*lp++ = (Char) (0xe0 | ((int) (CH(1) & 0xf0) >> 4));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(0) & 0x3f));
|
|
|
|
} else if (c < 0x00200000) {
|
|
|
|
*lp++ = (Char) (0xf0 | ((int) (CH(2) & 0x1f) >> 2));
|
|
|
|
*lp++ = (Char) (0x80 |
|
|
|
|
((int) (CH(1) & 0xf0) >> 4) |
|
|
|
|
((int) (CH(2) & 0x03) << 4));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(0) & 0x3f));
|
|
|
|
} else if (c < 0x04000000) {
|
|
|
|
*lp++ = (Char) (0xf8 | (CH(3) & 0x03));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(2) >> 2));
|
|
|
|
*lp++ = (Char) (0x80 |
|
|
|
|
((int) (CH(1) & 0xf0) >> 4) |
|
|
|
|
((int) (CH(2) & 0x03) << 4));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(0) & 0x3f));
|
|
|
|
} else {
|
|
|
|
*lp++ = (Char) (0xfc | ((int) (CH(3) & 0x40) >> 6));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(3) & 0x3f));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(2) >> 2));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(1) >> 4) | ((CH(2) & 0x03) << 4));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
|
|
|
|
*lp++ = (Char) (0x80 | (CH(0) & 0x3f));
|
2006-11-26 04:11:12 -07:00
|
|
|
}
|
|
|
|
return lp;
|
2010-03-25 16:03:51 -06:00
|
|
|
#undef CH
|
2006-11-26 04:11:12 -07:00
|
|
|
}
|
|
|
|
|
2019-12-29 01:54:02 -07:00
|
|
|
/*
|
|
|
|
* Convert a UTF-8 multibyte character to an Unicode value, returning a pointer
|
|
|
|
* past the converted UTF-8 input. The first 256 values align with ISO-8859-1,
|
|
|
|
* making it possible to use this to convert to Latin-1.
|
|
|
|
*
|
|
|
|
* If the conversion fails, return null.
|
|
|
|
*/
|
|
|
|
Char *
|
|
|
|
convertFromUTF8(Char *lp, unsigned *cp)
|
|
|
|
{
|
|
|
|
int want;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the number of bytes we will need from the source.
|
|
|
|
*/
|
|
|
|
if ((*lp & 0x80) == 0) {
|
|
|
|
want = 1;
|
|
|
|
} else if ((*lp & 0xe0) == 0xc0) {
|
|
|
|
want = 2;
|
|
|
|
} else if ((*lp & 0xf0) == 0xe0) {
|
|
|
|
want = 3;
|
|
|
|
} else if ((*lp & 0xf8) == 0xf0) {
|
|
|
|
want = 4;
|
|
|
|
} else if ((*lp & 0xfc) == 0xf8) {
|
|
|
|
want = 5;
|
|
|
|
} else if ((*lp & 0xfe) == 0xfc) {
|
|
|
|
want = 6;
|
|
|
|
} else {
|
|
|
|
want = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (want) {
|
|
|
|
int have = 1;
|
|
|
|
|
|
|
|
while (lp[have] != '\0') {
|
|
|
|
if ((lp[have] & 0xc0) != 0x80)
|
|
|
|
break;
|
|
|
|
++have;
|
|
|
|
}
|
|
|
|
if (want == have) {
|
|
|
|
unsigned mask = 0;
|
|
|
|
int j;
|
|
|
|
int shift = 0;
|
|
|
|
|
|
|
|
*cp = 0;
|
|
|
|
switch (want) {
|
|
|
|
case 1:
|
|
|
|
mask = (*lp);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mask = (*lp & 0x1f);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
mask = (*lp & 0x0f);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
mask = (*lp & 0x07);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
mask = (*lp & 0x03);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
mask = (*lp & 0x01);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mask = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 1; j < want; j++) {
|
|
|
|
*cp |= (unsigned) ((lp[want - j] & 0x3f) << shift);
|
|
|
|
shift += 6;
|
|
|
|
}
|
|
|
|
*cp |= mask << shift;
|
|
|
|
lp += want;
|
|
|
|
} else {
|
|
|
|
*cp = BAD_ASCII;
|
|
|
|
lp = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*cp = BAD_ASCII;
|
|
|
|
lp = NULL;
|
|
|
|
}
|
|
|
|
return lp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if the entire string is valid UTF-8.
|
|
|
|
*/
|
|
|
|
Boolean
|
|
|
|
isValidUTF8(Char *lp)
|
|
|
|
{
|
|
|
|
Boolean result = True;
|
|
|
|
while (*lp) {
|
|
|
|
unsigned ch;
|
|
|
|
Char *next = convertFromUTF8(lp, &ch);
|
|
|
|
if (next == NULL || ch == 0) {
|
|
|
|
result = False;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lp = next;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-11-26 04:11:12 -07:00
|
|
|
/*
|
|
|
|
* Write data back to the PTY
|
|
|
|
*/
|
|
|
|
void
|
2014-07-14 02:30:10 -06:00
|
|
|
writePtyData(int f, IChar *d, unsigned len)
|
2006-11-26 04:11:12 -07:00
|
|
|
{
|
|
|
|
unsigned n = (len << 1);
|
|
|
|
|
|
|
|
if (VTbuffer->write_len <= len) {
|
|
|
|
VTbuffer->write_len = n;
|
|
|
|
VTbuffer->write_buf = (Char *) XtRealloc((char *)
|
|
|
|
VTbuffer->write_buf, VTbuffer->write_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < len; n++)
|
2009-03-09 16:13:04 -06:00
|
|
|
VTbuffer->write_buf[n] = (Char) d[n];
|
2006-11-26 04:11:12 -07:00
|
|
|
|
|
|
|
TRACE(("writePtyData %d:%s\n", n,
|
2009-09-05 09:58:09 -06:00
|
|
|
visibleChars(VTbuffer->write_buf, n)));
|
2006-11-26 04:11:12 -07:00
|
|
|
v_write(f, VTbuffer->write_buf, n);
|
|
|
|
}
|
|
|
|
#endif /* OPT_WIDE_CHARS */
|
|
|
|
|
|
|
|
#ifdef NO_LEAKS
|
|
|
|
void
|
|
|
|
noleaks_ptydata(void)
|
|
|
|
{
|
|
|
|
if (VTbuffer != 0) {
|
|
|
|
#if OPT_WIDE_CHARS
|
|
|
|
if (VTbuffer->write_buf != 0)
|
|
|
|
free(VTbuffer->write_buf);
|
|
|
|
#endif
|
|
|
|
free(VTbuffer);
|
|
|
|
VTbuffer = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-02-24 04:41:42 -07:00
|
|
|
|
|
|
|
#ifdef TEST_PTYDATA
|
|
|
|
void
|
|
|
|
test_ptydata(XtermWidget xw)
|
|
|
|
{
|
|
|
|
TScreen *screen = TScreenOf(xw);
|
|
|
|
unsigned code;
|
|
|
|
PtyData *data;
|
|
|
|
Char *next;
|
|
|
|
|
|
|
|
for (code = 0; code < 256; code++) {
|
|
|
|
initPtyData(&data);
|
|
|
|
next = convertToUTF8(data->buffer, code);
|
|
|
|
*next = 0;
|
|
|
|
data->next = data->buffer;
|
|
|
|
data->last = next;
|
|
|
|
decodeUtf8(screen, data);
|
|
|
|
TRACE(("TEST %04X (%d:%s) ->%04X\n", code,
|
|
|
|
(int) (next - data->buffer),
|
|
|
|
data->buffer,
|
|
|
|
data->utf_data));
|
|
|
|
if (code != data->utf_data) {
|
|
|
|
fprintf(stderr, "Mismatch: %04X vs %04X\n", code, data->utf_data);
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* TEST_PTYDATA */
|
2019-12-29 01:54:02 -07:00
|
|
|
|
|
|
|
#ifdef TEST_UTF8_CONVERT
|
|
|
|
void
|
|
|
|
test_utf8_convert(void)
|
|
|
|
{
|
|
|
|
unsigned c_in, c_out;
|
|
|
|
Char buffer[10];
|
|
|
|
Char *result;
|
|
|
|
unsigned limit = 1 << 24;
|
|
|
|
unsigned success = 0;
|
|
|
|
unsigned bucket[256];
|
|
|
|
|
|
|
|
memset(bucket, 0, sizeof(bucket));
|
|
|
|
for (c_in = 0; c_in < limit; ++c_in) {
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
if ((result = convertToUTF8(buffer, c_in)) == 0) {
|
|
|
|
TRACE(("conversion of U+%04X to UTF-8 failed\n", c_in));
|
|
|
|
} else {
|
|
|
|
if ((result = convertFromUTF8(buffer, &c_out)) == 0) {
|
|
|
|
TRACE(("conversion of U+%04X from UTF-8 failed\n", c_in));
|
|
|
|
} else if (c_in != c_out) {
|
|
|
|
TRACE(("conversion of U+%04X to/from UTF-8 gave U+%04X\n",
|
|
|
|
c_in, c_out));
|
|
|
|
} else {
|
|
|
|
while (result-- != buffer) {
|
|
|
|
bucket[*result]++;
|
|
|
|
}
|
|
|
|
++success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TRACE(("test_utf8_convert: %u/%u successful\n", success, limit));
|
|
|
|
for (c_in = 0; c_in < 256; ++c_in) {
|
|
|
|
if ((c_in % 8) == 0) {
|
|
|
|
TRACE((" %02X:", c_in));
|
|
|
|
}
|
|
|
|
TRACE((" %8X", bucket[c_in]));
|
|
|
|
if (((c_in + 1) % 8) == 0) {
|
|
|
|
TRACE(("\n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|