1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Reorganised the Unicode layer somewhat: moved luni_send and

lpage_send out into the line discipline, making them _clients_ of
the Unicode layer rather than part of it. This means they can access
ldisc->term, which in turn means I've been able to remove the
temporary global variable `term'. We're slowly getting there.

[originally from svn r2143]
This commit is contained in:
Simon Tatham 2002-10-26 11:08:59 +00:00
parent 24530b945e
commit 948f95d5e8
10 changed files with 117 additions and 237 deletions

4
Recipe
View File

@ -89,7 +89,7 @@
# line.
# GUI front end and terminal emulator (putty, puttytel).
GUITERM = window windlg winctrls terminal sizetip wcwidth unicode
GUITERM = window windlg winctrls terminal sizetip wcwidth unicode ldiscucs
+ logging printing winutils
# Non-SSH back ends (putty, puttytel, plink).
@ -132,5 +132,5 @@ puttygen : [G] puttygen sshrsag sshdssg sshprime sshdes sshbn sshmd5 version
+ sshrand noise sshsha winstore misc winctrls sshrsa sshdss
+ sshpubk sshaes sshsh512 import winutils puttygen.res LIBS
pterm : [X] pterm terminal wcwidth uxucs uxmisc tree234 misc ldisc
pterm : [X] pterm terminal wcwidth uxucs uxmisc tree234 misc ldisc ldiscucs
+ logging uxprint settings pty be_none uxstore

11
ldisc.c
View File

@ -10,16 +10,7 @@
#include "putty.h"
#include "terminal.h"
typedef struct ldisc_tag {
Terminal *term;
Backend *back;
void *backhandle;
void *frontend;
char *buf;
int buflen, bufsiz, quotenext;
} *Ldisc;
#include "ldisc.h"
#define ECHOING (cfg.localecho == LD_YES || \
(cfg.localecho == LD_BACKEND && \

18
putty.h
View File

@ -383,8 +383,6 @@ GLOBAL Config cfg;
GLOBAL int default_protocol;
GLOBAL int default_port;
GLOBAL Terminal *term; /* temporary while changes are made */
struct RSAKey; /* be a little careful of scope */
/*
@ -520,10 +518,15 @@ extern Backend ssh_backend;
/*
* Exports from ldisc.c.
*/
void *ldisc_create(Terminal *, Backend *, void *, void *);
void ldisc_send(void *handle, char *buf, int len, int interactive);
/*
* Exports from ldiscucs.c.
*/
void lpage_send(void *, int codepage, char *buf, int len, int interactive);
void luni_send(void *, wchar_t * widebuf, int len, int interactive);
/*
* Exports from sshrand.c.
*/
@ -551,9 +554,12 @@ extern char ver[];
#ifndef CP_UTF8
#define CP_UTF8 65001
#endif
void init_ucs_tables(void);
void lpage_send(void *, int codepage, char *buf, int len, int interactive);
void luni_send(void *, wchar_t * widebuf, int len, int interactive);
void init_ucs(void);
int is_dbcs_leadbyte(int codepage, char byte);
int mb_to_wc(int codepage, int flags, char *mbstr, int mblen,
wchar_t *wcstr, int wclen);
int wc_to_mb(int codepage, int flags, wchar_t *wcstr, int wclen,
char *mbstr, int mblen, char *defchr, int *defused);
wchar_t xlat_uskbd2cyrllic(int ch);
int check_compose(int first, int second);
int decode_codepage(char *cp_name);

2
ssh.c
View File

@ -5975,8 +5975,6 @@ static void ssh_size(void *handle, int width, int height)
break;
case SSH_STATE_SESSION:
if (!cfg.nopty) {
if (!term)
return;
if (ssh->version == 1) {
send_packet(ssh, SSH1_CMSG_WINDOW_SIZE,
PKT_INT, ssh->term_height,

135
unicode.c
View File

@ -5,8 +5,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <assert.h>
#include "putty.h"
#include "terminal.h"
#include "misc.h"
@ -419,7 +420,7 @@ static struct cp_list_item cp_list[] = {
static void link_font(WCHAR * line_tbl, WCHAR * font_tbl, WCHAR attr);
void init_ucs_tables(void)
void init_ucs(void)
{
int i, j;
int used_dtf = 0;
@ -598,88 +599,6 @@ static void link_font(WCHAR * line_tbl, WCHAR * font_tbl, WCHAR attr)
}
}
void lpage_send(void *ldisc, int codepage, char *buf, int len, int interactive)
{
static wchar_t *widebuffer = 0;
static int widesize = 0;
int wclen;
if (codepage < 0) {
ldisc_send(ldisc, buf, len, interactive);
return;
}
if (len > widesize) {
sfree(widebuffer);
widebuffer = smalloc(len * 2 * sizeof(wchar_t));
widesize = len * 2;
}
wclen = mb_to_wc(codepage, 0, buf, len, widebuffer, widesize);
luni_send(ldisc, widebuffer, wclen, interactive);
}
void luni_send(void *ldisc, wchar_t * widebuf, int len, int interactive)
{
static char *linebuffer = 0;
static int linesize = 0;
int ratio = (in_utf(term))?3:1;
int i;
char *p;
if (len * ratio > linesize) {
sfree(linebuffer);
linebuffer = smalloc(len * ratio * 2 * sizeof(wchar_t));
linesize = len * ratio * 2;
}
if (in_utf(term)) {
/* UTF is a simple algorithm */
for (p = linebuffer, i = 0; i < len; i++) {
wchar_t ch = widebuf[i];
/* Windows wchar_t is UTF-16 */
if ((ch&0xF800) == 0xD800) ch = '.';
if (ch < 0x80) {
*p++ = (char) (ch);
} else if (ch < 0x800) {
*p++ = (0xC0 | (ch >> 6));
*p++ = (0x80 | (ch & 0x3F));
} else {
*p++ = (0xE0 | (ch >> 12));
*p++ = (0x80 | ((ch >> 6) & 0x3F));
*p++ = (0x80 | (ch & 0x3F));
}
}
} else if (!uni_tbl) {
int rv;
rv = wc_to_mb(line_codepage, 0, widebuf, len,
linebuffer, linesize, NULL, NULL);
if (rv >= 0)
p = linebuffer + rv;
else
p = linebuffer;
} else {
/* Others are a lookup in an array */
for (p = linebuffer, i = 0; i < len; i++) {
wchar_t ch = widebuf[i];
int by;
char *p1;
if (uni_tbl && (p1 = uni_tbl[(ch >> 8) & 0xFF])
&& (by = p1[ch & 0xFF]))
*p++ = by;
else if (ch < 0x80)
*p++ = (char) ch;
#if 1
else
*p++ = '.';
#endif
}
}
if (p > linebuffer)
ldisc_send(ldisc, linebuffer, p - linebuffer, interactive);
}
wchar_t xlat_uskbd2cyrllic(int ch)
{
static wchar_t cyrtab[] = {
@ -1255,3 +1174,51 @@ void get_unitab(int codepage, wchar_t * unitab, int ftype)
unitab[i] = cp_list[codepage & 0xFFFF].cp_table[i - j];
}
}
int wc_to_mb(int codepage, int flags, wchar_t *wcstr, int wclen,
char *mbstr, int mblen, char *defchr, int *defused)
{
char *p;
int i;
if (codepage == line_codepage && uni_tbl) {
/* Do this by array lookup if we can. */
if (wclen < 0) {
for (wclen = 0; wcstr[wclen++] ;); /* will include the NUL */
}
for (p = mbstr, i = 0; i < wclen; i++) {
wchar_t ch = wcstr[i];
int by;
char *p1;
if (uni_tbl && (p1 = uni_tbl[(ch >> 8) & 0xFF])
&& (by = p1[ch & 0xFF]))
*p++ = by;
else if (ch < 0x80)
*p++ = (char) ch;
else if (defchr) {
int j;
for (j = 0; defchr[j]; j++)
*p++ = defchr[j];
if (defused) *defused = 1;
}
#if 1
else
*p++ = '.';
#endif
assert(p - mbstr < mblen);
}
return p - mbstr;
} else
return WideCharToMultiByte(codepage, flags, wcstr, wclen,
mbstr, mblen, defchr, defused);
}
int mb_to_wc(int codepage, int flags, char *mbstr, int mblen,
wchar_t *wcstr, int wclen)
{
return MultiByteToWideChar(codepage, flags, mbstr, mblen, wcstr, wclen);
}
int is_dbcs_leadbyte(int codepage, char byte)
{
return IsDBCSLeadByteEx(codepage, byte);
}

View File

@ -56,6 +56,7 @@ struct gui_data {
void *ldisc;
Backend *back;
void *backhandle;
Terminal *term;
};
static struct gui_data the_inst;
@ -171,7 +172,7 @@ void set_zorder(int top)
*/
void refresh_window(void)
{
term_invalidate(term);
term_invalidate(inst->term);
}
/*
@ -296,7 +297,7 @@ gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
}
if (need_size) {
term_size(term, h, w, cfg.savelines);
term_size(inst->term, h, w, cfg.savelines);
}
return TRUE;
@ -420,11 +421,11 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
* at all.
*/
if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
term_scroll(term, 0, -cfg.height/2);
term_scroll(inst->term, 0, -cfg.height/2);
return TRUE;
}
if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
term_scroll(term, 0, +cfg.height/2);
term_scroll(inst->term, 0, +cfg.height/2);
return TRUE;
}
@ -527,7 +528,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
/*
* Application keypad mode.
*/
if (term->app_keypad_keys && !cfg.no_applic_k) {
if (inst->term->app_keypad_keys && !cfg.no_applic_k) {
int xkey = 0;
switch (event->keyval) {
case GDK_Num_Lock: xkey = 'P'; break;
@ -565,7 +566,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
}
if (xkey) {
if (term->vt52_mode) {
if (inst->term->vt52_mode) {
if (xkey >= 'P' && xkey <= 'S')
end = 1 + sprintf(output+1, "\033%c", xkey);
else
@ -673,7 +674,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
if (cfg.funky_type == 3 && code <= 6)
code = "\0\2\1\4\5\3\6"[code];
if (term->vt52_mode && code > 0 && code <= 6) {
if (inst->term->vt52_mode && code > 0 && code <= 6) {
end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
goto done;
}
@ -712,14 +713,14 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
}
goto done;
}
if ((term->vt52_mode || cfg.funky_type == 4) &&
if ((inst->term->vt52_mode || cfg.funky_type == 4) &&
code >= 11 && code <= 24) {
int offt = 0;
if (code > 15)
offt++;
if (code > 21)
offt++;
if (term->vt52_mode)
if (inst->term->vt52_mode)
end = 1 + sprintf(output+1,
"\x1B%c", code + 'P' - 11 - offt);
else
@ -732,7 +733,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
goto done;
}
if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
if (term->vt52_mode)
if (inst->term->vt52_mode)
end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
else
end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
@ -770,9 +771,9 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
* app cursor keys mode they do ESC O A instead.
* Ctrl toggles the two modes.
*/
if (term->vt52_mode) {
if (inst->term->vt52_mode) {
end = 1 + sprintf(output+1, "\033%c", xkey);
} else if (!term->app_cursor_keys ^
} else if (!inst->term->app_cursor_keys ^
!(event->state & GDK_CONTROL_MASK)) {
end = 1 + sprintf(output+1, "\033O%c", xkey);
} else {
@ -797,8 +798,8 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
ldisc_send(inst->ldisc, output+start, end-start, 1);
show_mouseptr(0);
term_seen_key_event(term);
term_out(term);
term_seen_key_event(inst->term);
term_out(inst->term);
}
return TRUE;
@ -812,11 +813,11 @@ gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
show_mouseptr(1);
if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
term_scroll(term, 0, -5);
term_scroll(inst->term, 0, -5);
return TRUE;
}
if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
term_scroll(term, 0, +5);
term_scroll(inst->term, 0, +5);
return TRUE;
}
@ -847,7 +848,7 @@ gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
x = (event->x - cfg.window_border) / inst->font_width;
y = (event->y - cfg.window_border) / inst->font_height;
term_mouse(term, button, act, x, y, shift, ctrl, alt);
term_mouse(inst->term, button, act, x, y, shift, ctrl, alt);
return TRUE;
}
@ -874,7 +875,7 @@ gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
x = (event->x - cfg.window_border) / inst->font_width;
y = (event->y - cfg.window_border) / inst->font_height;
term_mouse(term, button, MA_DRAG, x, y, shift, ctrl, alt);
term_mouse(inst->term, button, MA_DRAG, x, y, shift, ctrl, alt);
return TRUE;
}
@ -921,7 +922,7 @@ void done_with_pty(struct gui_data *inst)
" %d (%.400s)]\r\n", WTERMSIG(exitcode),
strsignal(WTERMSIG(exitcode)));
#endif
from_backend((void *)term, 0, message, strlen(message));
from_backend((void *)inst->term, 0, message, strlen(message));
}
inst->exited = 1;
}
@ -956,8 +957,8 @@ gint timer_func(gpointer data)
done_with_pty(inst);
}
term_update(term);
term_blink(term, 0);
term_update(inst->term);
term_blink(inst->term, 0);
return TRUE;
}
@ -980,9 +981,9 @@ void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
perror("read pty master");
exit(1);
} else if (ret > 0)
from_backend(term, 0, buf, ret);
term_blink(term, 1);
term_out(term);
from_backend(inst->term, 0, buf, ret);
term_blink(inst->term, 1);
term_out(inst->term);
}
void destroy(GtkWidget *widget, gpointer data)
@ -992,9 +993,9 @@ void destroy(GtkWidget *widget, gpointer data)
gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
{
term->has_focus = event->in;
term_out(term);
term_update(term);
inst->term->has_focus = event->in;
term_out(inst->term);
term_update(inst->term);
show_mouseptr(1);
return FALSE;
}
@ -1180,7 +1181,7 @@ void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
gpointer data)
{
term_deselect(term);
term_deselect(inst->term);
if (inst->pasteout_data)
sfree(inst->pasteout_data);
inst->pasteout_data = NULL;
@ -1216,9 +1217,9 @@ void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
mb_to_wc(0, 0, seldata->data, seldata->length,
inst->pastein_data, inst->pastein_data_len);
term_do_paste(term);
term_do_paste(inst->term);
if (term_paste_pending(term))
if (term_paste_pending(inst->term))
inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
}
@ -1226,8 +1227,8 @@ gint idle_paste_func(gpointer data)
{
struct gui_data *inst = (struct gui_data *)data;
if (term_paste_pending(term))
term_paste(term);
if (term_paste_pending(inst->term))
term_paste(inst->term);
else
gtk_idle_remove(inst->term_paste_idle_id);
@ -1277,7 +1278,7 @@ void scrollbar_moved(GtkAdjustment *adj, gpointer data)
if (!cfg.scrollbar)
return;
if (!inst->ignore_sbar)
term_scroll(term, 1, (int)adj->value);
term_scroll(inst->term, 1, (int)adj->value);
}
void sys_cursor(int x, int y)
@ -1360,10 +1361,10 @@ void do_text_internal(Context ctx, int x, int y, char *text, int len,
if (lattr != LATTR_NORM) {
x *= 2;
if (x >= term->cols)
if (x >= inst->term->cols)
return;
if (x + len*2 > term->cols)
len = (term->cols-x)/2; /* trim to LH half */
if (x + len*2 > inst->term->cols)
len = (inst->term->cols-x)/2; /* trim to LH half */
}
gdk_gc_set_foreground(gc, &inst->cols[nbg]);
@ -1442,10 +1443,10 @@ void do_text(Context ctx, int x, int y, char *text, int len,
if (lattr != LATTR_NORM) {
x *= 2;
if (x >= term->cols)
if (x >= inst->term->cols)
return;
if (x + len*2 > term->cols)
len = (term->cols-x)/2; /* trim to LH half */
if (x + len*2 > inst->term->cols)
len = (inst->term->cols-x)/2; /* trim to LH half */
len *= 2;
}
@ -1475,10 +1476,10 @@ void do_cursor(Context ctx, int x, int y, char *text, int len,
if (lattr != LATTR_NORM) {
x *= 2;
if (x >= term->cols)
if (x >= inst->term->cols)
return;
if (x + len*2 > term->cols)
len = (term->cols-x)/2; /* trim to LH half */
if (x + len*2 > inst->term->cols)
len = (inst->term->cols-x)/2; /* trim to LH half */
len *= 2;
}
@ -1962,16 +1963,16 @@ int main(int argc, char **argv)
inst->currcursor = inst->textcursor;
show_mouseptr(1);
term = term_init();
inst->term = term_init();
inst->back = &pty_backend;
inst->back->init((void *)term, &inst->backhandle, NULL, 0, NULL, 0);
inst->back->init((void *)inst->term, &inst->backhandle, NULL, 0, NULL, 0);
term_provide_resize_fn(term, inst->back->size, inst->backhandle);
term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
term_size(term, cfg.height, cfg.width, cfg.savelines);
term_size(inst->term, cfg.height, cfg.width, cfg.savelines);
inst->ldisc = ldisc_create(term, inst->back, inst->backhandle, inst);
inst->ldisc = ldisc_create(inst->term, inst->back, inst->backhandle, inst);
ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
inst->master_fd = pty_master_fd;

View File

@ -30,13 +30,6 @@ unsigned long getticks(void); /* based on gettimeofday(2) */
#define WCHAR wchar_t
#define BYTE unsigned char
int is_dbcs_leadbyte(int codepage, char byte);
int mb_to_wc(int codepage, int flags, char *mbstr, int mblen,
wchar_t *wcstr, int wclen);
int wc_to_mb(int codepage, int flags, wchar_t *wcstr, int wclen,
char *mbstr, int mblen, char *defchr, int *defused);
void init_ucs(void);
/* Things pty.c needs from pterm.c */
char *get_x_display(void);
int font_dimension(int which); /* 0 for width, 1 for height */

View File

@ -14,74 +14,6 @@
* are ISO8859-1.
*/
void lpage_send(void *ldisc, int codepage, char *buf, int len, int interactive)
{
ldisc_send(ldisc, buf, len, interactive);
}
void luni_send(void *ldisc, wchar_t * widebuf, int len, int interactive)
{
static char *linebuffer = 0;
static int linesize = 0;
int ratio = (in_utf(term))?6:1;
int i;
char *p;
if (len * ratio > linesize) {
sfree(linebuffer);
linebuffer = smalloc(len * ratio * 2 * sizeof(wchar_t));
linesize = len * ratio * 2;
}
if (in_utf(term)) {
/* UTF is a simple algorithm */
for (p = linebuffer, i = 0; i < len; i++) {
wchar_t ch = widebuf[i];
if ((ch&0xF800) == 0xD800) ch = '.';
if (ch < 0x80) {
*p++ = (char) (ch);
} else if (ch < 0x800) {
*p++ = (0xC0 | (ch >> 6));
*p++ = (0x80 | (ch & 0x3F));
} else if (ch < 0x10000) {
*p++ = (0xE0 | (ch >> 12));
*p++ = (0x80 | ((ch >> 6) & 0x3F));
*p++ = (0x80 | (ch & 0x3F));
} else if (ch < 0x200000) {
*p++ = (0xF0 | (ch >> 18));
*p++ = (0x80 | ((ch >> 12) & 0x3F));
*p++ = (0x80 | ((ch >> 6) & 0x3F));
*p++ = (0x80 | (ch & 0x3F));
} else if (ch < 0x4000000) {
*p++ = (0xF8 | (ch >> 24));
*p++ = (0x80 | ((ch >> 18) & 0x3F));
*p++ = (0x80 | ((ch >> 12) & 0x3F));
*p++ = (0x80 | ((ch >> 6) & 0x3F));
*p++ = (0x80 | (ch & 0x3F));
} else {
*p++ = (0xFC | (ch >> 30));
*p++ = (0x80 | ((ch >> 24) & 0x3F));
*p++ = (0x80 | ((ch >> 18) & 0x3F));
*p++ = (0x80 | ((ch >> 12) & 0x3F));
*p++ = (0x80 | ((ch >> 6) & 0x3F));
*p++ = (0x80 | (ch & 0x3F));
}
}
} else {
for (p = linebuffer, i = 0; i < len; i++) {
wchar_t ch = widebuf[i];
if (ch < 0x100)
*p++ = (char) ch;
else
*p++ = '.';
}
}
if (p > linebuffer)
ldisc_send(ldisc, linebuffer, p - linebuffer, interactive);
}
int is_dbcs_leadbyte(int codepage, char byte)
{
return 0; /* we don't do DBCS */
@ -109,7 +41,7 @@ int wc_to_mb(int codepage, int flags, wchar_t *wcstr, int wclen,
if (defchr)
*mbstr++ = *defchr;
else
*mbstr++ = '\xBF';
*mbstr++ = '.';
if (defused)
*defused = 1;
} else

View File

@ -118,6 +118,7 @@ static int caret_x = -1, caret_y = -1;
static void *ldisc;
static Backend *back;
static void *backhandle;
static Terminal *term;
#define FONT_NORMAL 0
#define FONT_BOLD 1
@ -1194,7 +1195,7 @@ static void init_fonts(int pick_width, int pick_height)
}
fontflag[0] = fontflag[1] = fontflag[2] = 1;
init_ucs_tables();
init_ucs();
}
static void another_font(int fontno)

View File

@ -173,13 +173,4 @@ void force_normal(HWND hwnd);
void UpdateSizeTip(HWND src, int cx, int cy);
void EnableSizeTip(int bEnable);
/*
* Unicode and multi-byte character handling stuff.
*/
#define is_dbcs_leadbyte(cp, c) IsDBCSLeadByteEx(cp, c)
#define mb_to_wc(cp, flags, mbstr, mblen, wcstr, wclen) \
MultiByteToWideChar(cp, flags, mbstr, mblen, wcstr, wclen)
#define wc_to_mb(cp, flags, wcstr, wclen, mbstr, mblen, def, defused) \
WideCharToMultiByte(cp, flags, wcstr, wclen, mbstr, mblen, def,defused)
#endif