Make tputc, tsetchar and techo accept unicode

master
noname@inventati.org 10 years ago committed by Roberto E. Vargas Caballero
parent 21f765426c
commit 0622ad9bad
  1. 107
      st.c

107
st.c

@ -383,20 +383,20 @@ static void tmoveato(int, int);
static void tnew(int, int); static void tnew(int, int);
static void tnewline(int); static void tnewline(int);
static void tputtab(int); static void tputtab(int);
static void tputc(char *, int); static void tputc(long);
static void treset(void); static void treset(void);
static void tresize(int, int); static void tresize(int, int);
static void tscrollup(int, int); static void tscrollup(int, int);
static void tscrolldown(int, int); static void tscrolldown(int, int);
static void tsetattr(int *, int); static void tsetattr(int *, int);
static void tsetchar(char *, Glyph *, int, int); static void tsetchar(long, Glyph *, int, int);
static void tsetscroll(int, int); static void tsetscroll(int, int);
static void tswapscreen(void); static void tswapscreen(void);
static void tsetdirt(int, int); static void tsetdirt(int, int);
static void tsetdirtattr(int); static void tsetdirtattr(int);
static void tsetmode(bool, bool, int *, int); static void tsetmode(bool, bool, int *, int);
static void tfulldirt(void); static void tfulldirt(void);
static void techo(char *, int); static void techo(long);
static void tcontrolcode(uchar ); static void tcontrolcode(uchar );
static void tdectest(char ); static void tdectest(char );
static int32_t tdefcolor(int *, int *, int); static int32_t tdefcolor(int *, int *, int);
@ -1332,7 +1332,6 @@ ttyread(void) {
static char buf[BUFSIZ]; static char buf[BUFSIZ];
static int buflen = 0; static int buflen = 0;
char *ptr; char *ptr;
char s[UTF_SIZ];
int charsize; /* size of utf8 char in bytes */ int charsize; /* size of utf8 char in bytes */
long unicodep; long unicodep;
int ret; int ret;
@ -1345,8 +1344,7 @@ ttyread(void) {
buflen += ret; buflen += ret;
ptr = buf; ptr = buf;
while((charsize = utf8decode(ptr, &unicodep, buflen))) { while((charsize = utf8decode(ptr, &unicodep, buflen))) {
utf8encode(unicodep, s); tputc(unicodep);
tputc(s, charsize);
ptr += charsize; ptr += charsize;
buflen -= charsize; buflen -= charsize;
} }
@ -1363,9 +1361,16 @@ ttywrite(const char *s, size_t n) {
void void
ttysend(char *s, size_t n) { ttysend(char *s, size_t n) {
int len;
long u;
ttywrite(s, n); ttywrite(s, n);
if(IS_SET(MODE_ECHO)) if(IS_SET(MODE_ECHO))
techo(s, n); while((len = utf8decode(s, &u, n)) > 0) {
techo(u);
n -= len;
s += len;
}
} }
void void
@ -1614,7 +1619,7 @@ tmoveto(int x, int y) {
} }
void void
tsetchar(char *c, Glyph *attr, int x, int y) { tsetchar(long u, Glyph *attr, int x, int y) {
static char *vt100_0[62] = { /* 0x41 - 0x7e */ static char *vt100_0[62] = { /* 0x41 - 0x7e */
"", "", "", "", "", "", "", /* A - G */ "", "", "", "", "", "", "", /* A - G */
0, 0, 0, 0, 0, 0, 0, 0, /* H - O */ 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
@ -1629,11 +1634,9 @@ tsetchar(char *c, Glyph *attr, int x, int y) {
/* /*
* The table is proudly stolen from rxvt. * The table is proudly stolen from rxvt.
*/ */
if(term.trantbl[term.charset] == CS_GRAPHIC0) { if(term.trantbl[term.charset] == CS_GRAPHIC0 &&
if(BETWEEN(c[0], 0x41, 0x7e) && vt100_0[c[0] - 0x41]) { BETWEEN(u, 0x41, 0x7e) && vt100_0[u - 0x41])
c = vt100_0[c[0] - 0x41]; utf8decode(vt100_0[u - 0x41], &u, UTF_SIZ);
}
}
if(term.line[y][x].mode & ATTR_WIDE) { if(term.line[y][x].mode & ATTR_WIDE) {
if(x+1 < term.col) { if(x+1 < term.col) {
@ -1647,7 +1650,7 @@ tsetchar(char *c, Glyph *attr, int x, int y) {
term.dirty[y] = 1; term.dirty[y] = 1;
term.line[y][x] = *attr; term.line[y][x] = *attr;
utf8decode(c, &term.line[y][x].u, UTF_SIZ); term.line[y][x].u = u;
} }
void void
@ -2431,26 +2434,18 @@ tputtab(int n) {
} }
void void
techo(char *buf, int len) { techo(long u) {
for(; len > 0; buf++, len--) { if(ISCONTROL(u)) { /* control code */
char c = *buf; if(u & 0x80) {
u &= 0x7f;
if(ISCONTROL((uchar) c)) { /* control code */ tputc('^');
if(c & 0x80) { tputc('[');
c &= 0x7f; } else if(u != '\n' && u != '\r' && u != '\t') {
tputc("^", 1); u ^= 0x40;
tputc("[", 1); tputc('^');
} else if(c != '\n' && c != '\r' && c != '\t') {
c ^= 0x40;
tputc("^", 1);
}
tputc(&c, 1);
} else {
break;
} }
} }
if(len) tputc(u);
tputc(buf, len);
} }
void void
@ -2468,13 +2463,12 @@ tdeftran(char ascii) {
void void
tdectest(char c) { tdectest(char c) {
static char E[UTF_SIZ] = "E";
int x, y; int x, y;
if(c == '8') { /* DEC screen alignment test. */ if(c == '8') { /* DEC screen alignment test. */
for(x = 0; x < term.col; ++x) { for(x = 0; x < term.col; ++x) {
for(y = 0; y < term.row; ++y) for(y = 0; y < term.row; ++y)
tsetchar(E, &term.c.attr, x, y); tsetchar('E', &term.c.attr, x, y);
} }
} }
} }
@ -2502,8 +2496,6 @@ tstrsequence(uchar c) {
void void
tcontrolcode(uchar ascii) { tcontrolcode(uchar ascii) {
static char question[UTF_SIZ] = "?";
switch(ascii) { switch(ascii) {
case '\t': /* HT */ case '\t': /* HT */
tputtab(1); tputtab(1);
@ -2541,7 +2533,7 @@ tcontrolcode(uchar ascii) {
term.charset = 1 - (ascii - '\016'); term.charset = 1 - (ascii - '\016');
return; return;
case '\032': /* SUB */ case '\032': /* SUB */
tsetchar(question, &term.c.attr, term.c.x, term.c.y); tsetchar('?', &term.c.attr, term.c.x, term.c.y);
case '\030': /* CAN */ case '\030': /* CAN */
csireset(); csireset();
break; break;
@ -2665,28 +2657,21 @@ eschandle(uchar ascii) {
} }
void void
tputc(char *c, int len) { tputc(long u) {
uchar ascii; char c[UTF_SIZ];
bool control; bool control;
long unicodep; int width, len;
int width;
Glyph *gp; Glyph *gp;
if(len == 1) { len = utf8encode(u, c);
if((width = wcwidth(u)) == -1) {
memcpy(c, "\357\277\275", 4); /* UTF_INVALID */
width = 1; width = 1;
unicodep = ascii = *c;
} else {
utf8decode(c, &unicodep, UTF_SIZ);
if ((width = wcwidth(unicodep)) == -1) {
c = "\357\277\275"; /* UTF_INVALID */
width = 1;
}
ascii = unicodep;
} }
if(IS_SET(MODE_PRINT)) if(IS_SET(MODE_PRINT))
tprinter(c, len); tprinter(c, len);
control = ISCONTROL(unicodep); control = ISCONTROL(u);
/* /*
* STR sequence must be checked before anything else * STR sequence must be checked before anything else
@ -2695,10 +2680,8 @@ tputc(char *c, int len) {
* character. * character.
*/ */
if(term.esc & ESC_STR) { if(term.esc & ESC_STR) {
if(len == 1 && if(u == '\a' || u == 030 || u == 032 || u == 033 ||
(ascii == '\a' || ascii == 030 || ISCONTROLC1(u)) {
ascii == 032 || ascii == 033 ||
ISCONTROLC1(unicodep))) {
term.esc &= ~(ESC_START|ESC_STR); term.esc &= ~(ESC_START|ESC_STR);
term.esc |= ESC_STR_END; term.esc |= ESC_STR_END;
} else if(strescseq.len + len < sizeof(strescseq.buf) - 1) { } else if(strescseq.len + len < sizeof(strescseq.buf) - 1) {
@ -2729,15 +2712,15 @@ tputc(char *c, int len) {
* they must not cause conflicts with sequences. * they must not cause conflicts with sequences.
*/ */
if(control) { if(control) {
tcontrolcode(ascii); tcontrolcode(u);
/* /*
* control codes are not shown ever * control codes are not shown ever
*/ */
return; return;
} else if(term.esc & ESC_START) { } else if(term.esc & ESC_START) {
if(term.esc & ESC_CSI) { if(term.esc & ESC_CSI) {
csiescseq.buf[csiescseq.len++] = ascii; csiescseq.buf[csiescseq.len++] = u;
if(BETWEEN(ascii, 0x40, 0x7E) if(BETWEEN(u, 0x40, 0x7E)
|| csiescseq.len >= \ || csiescseq.len >= \
sizeof(csiescseq.buf)-1) { sizeof(csiescseq.buf)-1) {
term.esc = 0; term.esc = 0;
@ -2746,11 +2729,11 @@ tputc(char *c, int len) {
} }
return; return;
} else if(term.esc & ESC_ALTCHARSET) { } else if(term.esc & ESC_ALTCHARSET) {
tdeftran(ascii); tdeftran(u);
} else if(term.esc & ESC_TEST) { } else if(term.esc & ESC_TEST) {
tdectest(ascii); tdectest(u);
} else { } else {
if (!eschandle(ascii)) if (!eschandle(u))
return; return;
/* sequence already finished */ /* sequence already finished */
} }
@ -2779,7 +2762,7 @@ tputc(char *c, int len) {
gp = &term.line[term.c.y][term.c.x]; gp = &term.line[term.c.y][term.c.x];
} }
tsetchar(c, &term.c.attr, term.c.x, term.c.y); tsetchar(u, &term.c.attr, term.c.x, term.c.y);
if(width == 2) { if(width == 2) {
gp->mode |= ATTR_WIDE; gp->mode |= ATTR_WIDE;

Loading…
Cancel
Save