2002-10-27 12:24:47 +03:00
|
|
|
/*
|
|
|
|
* ldisc.c: PuTTY line discipline. Sits between the input coming
|
|
|
|
* from keypresses in the window, and the output channel leading to
|
|
|
|
* the back end. Implements echo and/or local line editing,
|
|
|
|
* depending on what's currently configured.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "putty.h"
|
|
|
|
#include "terminal.h"
|
|
|
|
#include "ldisc.h"
|
|
|
|
|
2018-09-11 17:02:59 +03:00
|
|
|
void lpage_send(Ldisc *ldisc,
|
2015-05-15 13:15:42 +03:00
|
|
|
int codepage, const char *buf, int len, int interactive)
|
2002-10-27 12:24:47 +03:00
|
|
|
{
|
|
|
|
wchar_t *widebuffer = 0;
|
|
|
|
int widesize = 0;
|
|
|
|
int wclen;
|
|
|
|
|
|
|
|
if (codepage < 0) {
|
|
|
|
ldisc_send(ldisc, buf, len, interactive);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
widesize = len * 2;
|
2003-03-29 19:14:26 +03:00
|
|
|
widebuffer = snewn(widesize, wchar_t);
|
2002-10-27 12:24:47 +03:00
|
|
|
|
|
|
|
wclen = mb_to_wc(codepage, 0, buf, len, widebuffer, widesize);
|
|
|
|
luni_send(ldisc, widebuffer, wclen, interactive);
|
|
|
|
|
|
|
|
sfree(widebuffer);
|
|
|
|
}
|
|
|
|
|
2018-09-11 17:02:59 +03:00
|
|
|
void luni_send(Ldisc *ldisc, const wchar_t *widebuf, int len, int interactive)
|
2002-10-27 12:24:47 +03:00
|
|
|
{
|
|
|
|
int ratio = (in_utf(ldisc->term))?3:1;
|
|
|
|
char *linebuffer;
|
|
|
|
int linesize;
|
|
|
|
int i;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
linesize = len * ratio * 2;
|
2003-03-29 19:14:26 +03:00
|
|
|
linebuffer = snewn(linesize, char);
|
2002-10-27 12:24:47 +03:00
|
|
|
|
|
|
|
if (in_utf(ldisc->term)) {
|
|
|
|
/* UTF is a simple algorithm */
|
|
|
|
for (p = linebuffer, i = 0; i < len; i++) {
|
2009-03-25 01:24:31 +03:00
|
|
|
unsigned long ch = widebuf[i];
|
|
|
|
|
2012-02-17 23:28:55 +04:00
|
|
|
if (IS_SURROGATE(ch)) {
|
2009-03-25 01:24:31 +03:00
|
|
|
#ifdef PLATFORM_IS_UTF16
|
|
|
|
if (i+1 < len) {
|
|
|
|
unsigned long ch2 = widebuf[i+1];
|
2012-02-17 23:28:55 +04:00
|
|
|
if (IS_SURROGATE_PAIR(ch, ch2)) {
|
|
|
|
ch = FROM_SURROGATES(ch, ch2);
|
2009-03-25 01:24:31 +03:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Unrecognised UTF-16 sequence */
|
|
|
|
ch = '.';
|
|
|
|
}
|
|
|
|
}
|
2002-10-27 12:24:47 +03:00
|
|
|
|
|
|
|
if (ch < 0x80) {
|
|
|
|
*p++ = (char) (ch);
|
|
|
|
} else if (ch < 0x800) {
|
2011-07-12 22:13:33 +04:00
|
|
|
*p++ = (char) (0xC0 | (ch >> 6));
|
|
|
|
*p++ = (char) (0x80 | (ch & 0x3F));
|
2009-03-25 01:24:31 +03:00
|
|
|
} else if (ch < 0x10000) {
|
2011-07-12 22:13:33 +04:00
|
|
|
*p++ = (char) (0xE0 | (ch >> 12));
|
|
|
|
*p++ = (char) (0x80 | ((ch >> 6) & 0x3F));
|
|
|
|
*p++ = (char) (0x80 | (ch & 0x3F));
|
2009-03-25 01:24:31 +03:00
|
|
|
} else {
|
2011-07-12 22:13:33 +04:00
|
|
|
*p++ = (char) (0xF0 | (ch >> 18));
|
|
|
|
*p++ = (char) (0x80 | ((ch >> 12) & 0x3F));
|
|
|
|
*p++ = (char) (0x80 | ((ch >> 6) & 0x3F));
|
|
|
|
*p++ = (char) (0x80 | (ch & 0x3F));
|
2002-10-27 12:24:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int rv;
|
2003-01-14 21:28:23 +03:00
|
|
|
rv = wc_to_mb(ldisc->term->ucsdata->line_codepage, 0, widebuf, len,
|
2018-10-06 13:45:26 +03:00
|
|
|
linebuffer, linesize, NULL, ldisc->term->ucsdata);
|
2002-10-27 12:24:47 +03:00
|
|
|
if (rv >= 0)
|
|
|
|
p = linebuffer + rv;
|
|
|
|
else
|
|
|
|
p = linebuffer;
|
|
|
|
}
|
|
|
|
if (p > linebuffer)
|
|
|
|
ldisc_send(ldisc, linebuffer, p - linebuffer, interactive);
|
|
|
|
|
|
|
|
sfree(linebuffer);
|
|
|
|
}
|