JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Ignore all control characters not handled
[st.git] / st.c
diff --git a/st.c b/st.c
index 2d080e5..515dfb1 100644 (file)
--- a/st.c
+++ b/st.c
@@ -278,7 +278,7 @@ static void tmoveto(int, int);
 static void tnew(int, int);
 static void tnewline(int);
 static void tputtab(bool);
-static void tputc(char*);
+static void tputc(char*, int);
 static void treset(void);
 static int tresize(int, int);
 static void tscrollup(int, int);
@@ -884,7 +884,7 @@ ttyread(void) {
        while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
                charsize = utf8decode(ptr, &utf8c);
                utf8encode(&utf8c, s);
-               tputc(s);
+               tputc(s, charsize);
                ptr    += charsize;
                buflen -= charsize;
        }
@@ -1094,6 +1094,27 @@ tmoveto(int x, int y) {
 
 void
 tsetchar(char *c) {
+       /*
+        * The table is proudly stolen from rxvt.
+        */
+       if(term.c.attr.mode & ATTR_GFX) {
+               char *vt100_0[62] = { /* 0x41 - 0x7e */
+                       "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
+                       0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
+                       0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
+                       0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
+                       "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
+                       "␤", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
+                       "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
+                       "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
+               };
+
+               if(c[0] >= 0x41 && c[0] <= 0x7e
+                               && vt100_0[c[0] - 0x41]) {
+                       c = vt100_0[c[0] - 0x41];
+               }
+       }
+
        term.dirty[term.c.y] = 1;
        term.line[term.c.y][term.c.x] = term.c.attr;
        memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
@@ -1641,11 +1662,11 @@ tputtab(bool forward) {
 }
 
 void
-tputc(char *c) {
-       char ascii = *c;
+tputc(char *c, int len) {
+       uchar ascii = *c;
 
        if(iofd != -1)
-               write(iofd, c, 1);
+               write(iofd, c, len);
 
        if(term.esc & ESC_START) {
                if(term.esc & ESC_CSI) {
@@ -1676,12 +1697,18 @@ tputc(char *c) {
                                strhandle();
                } else if(term.esc & ESC_ALTCHARSET) {
                        switch(ascii) {
-                       case '0': /* Line drawing crap */
+                       case '0': /* Line drawing set */
                                term.c.attr.mode |= ATTR_GFX;
                                break;
-                       case 'B': /* Back to regular text */
+                       case 'B': /* USASCII */
                                term.c.attr.mode &= ~ATTR_GFX;
                                break;
+                       case 'A': /* UK (IGNORED) */
+                       case '<': /* multinational charset (IGNORED) */
+                       case '5': /* Finnish (IGNORED) */
+                       case 'C': /* Finnish (IGNORED) */
+                       case 'K': /* German (IGNORED) */
+                               break;
                        default:
                                fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
                        }
@@ -1700,9 +1727,14 @@ tputc(char *c) {
                                strescseq.type = ascii;
                                term.esc |= ESC_STR;
                                break;
-                       case '(':
+                       case '(': /* set primary charset G0 */
                                term.esc |= ESC_ALTCHARSET;
                                break;
+                       case ')': /* set secondary charset G1 (IGNORED) */
+                       case '*': /* set tertiary charset G2 (IGNORED) */
+                       case '+': /* set quaternary charset G3 (IGNORED) */
+                               term.esc = 0;
+                               break;
                        case 'D': /* IND -- Linefeed */
                                if(term.c.y == term.bot)
                                        tscrollup(term.top, 1);
@@ -1760,8 +1792,6 @@ tputc(char *c) {
                if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
                        sel.bx = -1;
                switch(ascii) {
-               case '\0': /* padding character, do nothing */
-                       break;
                case '\t':
                        tputtab(1);
                        break;
@@ -1786,13 +1816,15 @@ tputc(char *c) {
                        term.esc = ESC_START;
                        break;
                default:
-                       if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
-                               tnewline(1); /* always go to first col */
-                       tsetchar(c);
-                       if(term.c.x+1 < term.col)
-                               tmoveto(term.c.x+1, term.c.y);
-                       else
-                               term.c.state |= CURSOR_WRAPNEXT;
+                       if(ascii >= '\020' || term.c.attr.mode & ATTR_GFX) {
+                               if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
+                                       tnewline(1); /* always go to first col */
+                               tsetchar(c);
+                               if(term.c.x+1 < term.col)
+                                       tmoveto(term.c.x+1, term.c.y);
+                               else
+                                       term.c.state |= CURSOR_WRAPNEXT;
+                       }
                }
        }
 }
@@ -2066,7 +2098,6 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
        int winx = BORDER+x*xw.cw, winy = BORDER+y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
        Font *font = &dc.font;
        XGlyphInfo extents;
-       int i;
 
        /* only switch default fg/bg if term is in RV mode */
        if(IS_SET(MODE_REVERSE)) {
@@ -2092,16 +2123,6 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
        XSetBackground(xw.dpy, dc.gc, dc.col[bg]);
        XSetForeground(xw.dpy, dc.gc, dc.col[fg]);
 
-       if(base.mode & ATTR_GFX) {
-               for(i = 0; i < bytelen; i++) {
-                       char c = gfx[(uint)s[i] % 256];
-                       if(c)
-                               s[i] = c;
-                       else if(s[i] > 0x5f)
-                               s[i] -= 0x5f;
-               }
-       }
-
        XftTextExtentsUtf8(xw.dpy, font->xft_set, (FcChar8 *)s, bytelen, &extents);
        width = extents.xOff;
        XftDrawRect(xw.xft_draw, &dc.xft_col[bg], winx, winy - font->ascent, width, xw.ch);