Correctly display vt100 graphics in non UTF-8 locales.
[apps/madtty.git] / madtty / madtty.c
index 5b51618..e7f26f8 100644 (file)
@@ -24,7 +24,6 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <pty.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <termios.h>
 #include <wchar.h>
-
+#ifdef __linux__
+# include <pty.h>
+#elif defined(__FreeBSD__)
+# include <libutil.h>
+#elif defined(__OpenBSD__)
+# include <util.h>
+#endif
 #include "madtty.h"
 
+#ifndef NCURSES_ATTR_SHIFT
+# define NCURSES_ATTR_SHIFT 8
+#endif
+
 #define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
 
 static int has_default = 0;
@@ -612,6 +621,17 @@ static void try_interpret_escape_seq(madtty_t *t)
 
     if (t->elen + 1 >= (int)sizeof(t->ebuf)) {
 cancel:
+#ifndef NDEBUG
+        fprintf(stderr, "cancelled: \\033");
+        for (int i = 0; i < (int)t->elen; i++) {
+            if (isprint(t->ebuf[i])) {
+                fputc(t->ebuf[i], stderr);
+            } else {
+                fprintf(stderr, "\\%03o", t->ebuf[i]);
+            }
+        }
+        fputc('\n', stderr);
+#endif
         cancel_escape_sequence(t);
     }
 }
@@ -657,10 +677,19 @@ static void madtty_process_nonprinting(madtty_t *t, wchar_t wc)
     }
 }
 
+bool is_utf8 = true;
+
+static void is_utf8_locale(void)
+{
+    const char *l = getenv("LANG");
+    if (l)
+        is_utf8 = (strstr(l, "UTF-8") != NULL);
+}
+
 // vt100 special graphics and line drawing
 // 5f-7e standard vt100
 // 40-5e rxvt extension for extra curses acs chars
-static uint16_t const vt100_0[62] = { // 41 .. 7e
+static uint16_t const vt100_utf8[62] = { // 41 .. 7e
             0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603, // 41-47 hi mr. snowman!
          0,      0,      0,      0,      0,      0,      0,      0, // 48-4f
          0,      0,      0,      0,      0,      0,      0,      0, // 50-57
@@ -671,7 +700,47 @@ static uint16_t const vt100_0[62] = { // 41 .. 7e
     0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7,         // 78-7e
 };
 
-void madtty_putc(madtty_t *t, wchar_t wc)
+static uint32_t vt100[62];
+
+void madtty_init_vt100_graphics(void)
+{
+    vt100['l' - 0x41] = ACS_ULCORNER;
+    vt100['m' - 0x41] = ACS_LLCORNER;
+    vt100['k' - 0x41] = ACS_URCORNER;
+    vt100['j' - 0x41] = ACS_LRCORNER;
+    vt100['u' - 0x41] = ACS_RTEE;
+    vt100['t' - 0x41] = ACS_LTEE;
+    vt100['v' - 0x41] = ACS_TTEE;
+    vt100['w' - 0x41] = ACS_BTEE;
+    vt100['q' - 0x41] = ACS_HLINE;
+    vt100['x' - 0x41] = ACS_VLINE;
+    vt100['n' - 0x41] = ACS_PLUS;
+    vt100['o' - 0x41] = ACS_S1;
+    vt100['s' - 0x41] = ACS_S9;
+    vt100['`' - 0x41] = ACS_DIAMOND;
+    vt100['a' - 0x41] = ACS_CKBOARD;
+    vt100['f' - 0x41] = ACS_DEGREE;
+    vt100['g' - 0x41] = ACS_PLMINUS;
+    vt100['~' - 0x41] = ACS_BULLET;
+    vt100[',' - 0x41] = ACS_LARROW;
+    vt100['+' - 0x41] = ACS_RARROW;
+    vt100['.' - 0x41] = ACS_DARROW;
+    vt100['-' - 0x41] = ACS_UARROW;
+    vt100['h' - 0x41] = ACS_BOARD;
+    vt100['i' - 0x41] = ACS_LANTERN;
+    vt100['0' - 0x41] = ACS_BLOCK;
+    /* these defaults were invented for ncurses */
+    vt100['p' - 0x41] = ACS_S3;
+    vt100['r' - 0x41] = ACS_S7;
+    vt100['y' - 0x41] = ACS_LEQUAL;
+    vt100['z' - 0x41] = ACS_GEQUAL;
+    vt100['{' - 0x41] = ACS_PI;
+    vt100['|' - 0x41] = ACS_NEQUAL;
+    vt100['}' - 0x41] = ACS_STERLING;
+    is_utf8_locale();
+}
+
+static void madtty_putc(madtty_t *t, wchar_t wc)
 {
     int width = 0;
 
@@ -691,8 +760,11 @@ void madtty_putc(madtty_t *t, wchar_t wc)
         t_row_t *tmp;
 
         if (t->graphmode) {
-            if (wc >= 0x41 && wc <= 0x7e && vt100_0[wc - 0x41]) {
-                wc = vt100_0[wc - 0x41];
+            if (wc >= 0x41 && wc <= 0x7e) {
+                if(is_utf8 && vt100_utf8[wc - 0x41])
+                    wc = vt100_utf8[wc - 0x41];
+                else if(!is_utf8 && vt100[wc - 0x41])
+                    wc = vt100[wc - 0x41];
             }
             width = 1;
         } else {
@@ -836,6 +908,8 @@ void madtty_resize(madtty_t *t, int rows, int cols)
             lines[row].attr = realloc(lines[row].attr, sizeof(uint16_t) * cols);
             if (t->cols < cols)
                 t_row_set(lines + row, t->cols, cols - t->cols, 0);
+            else
+                lines[row].dirty = true;
         }
         t->cols = cols;
     }
@@ -843,14 +917,13 @@ void madtty_resize(madtty_t *t, int rows, int cols)
     while (t->rows < rows) {
         lines[t->rows].text = (wchar_t *)calloc(sizeof(wchar_t), cols);
         lines[t->rows].attr = (uint16_t *)calloc(sizeof(uint16_t), cols);
+        t_row_set(lines + t->rows, 0, t->cols, 0);
         t->rows++;
     }
 
     t->curs_row   += lines - t->lines;
-    t->scroll_top += lines - t->lines;
-    t->scroll_bot += lines - t->lines;
-    if (t->scroll_bot > lines + t->rows)
-        t->scroll_bot = lines + t->rows;
+    t->scroll_top = lines;
+    t->scroll_bot = lines + rows;
     t->lines = lines;
     clamp_cursor_to_bounds(t);
     ioctl(t->pty, TIOCSWINSZ, &ws);
@@ -885,7 +958,7 @@ void madtty_draw(madtty_t *t, WINDOW *win, int srow, int scol)
         for (int j = 0; j < t->cols; j++) {
             if (!j || row->attr[j] != row->attr[j - 1])
                 wattrset(win, (attr_t)row->attr[j] << NCURSES_ATTR_SHIFT);
-            if (row->text[j] >= 128) {
+            if (is_utf8 && row->text[j] >= 128) {
                 char buf[MB_CUR_MAX + 1];
                 int len;
 
@@ -963,7 +1036,7 @@ void madtty_keypress(madtty_t *t, int keycode)
 
 void madtty_init_colors(void)
 {
-    if (COLORS > 8) {
+    if (COLOR_PAIRS > 64) {
         use_default_colors();
         assume_default_colors(-1, -1);
         has_default = 1;
@@ -984,5 +1057,16 @@ void madtty_init_colors(void)
 
 int madtty_color_pair(int fg, int bg)
 {
-    return has_default ? (fg + 1) * 16 + bg + 1 : (7 - fg) * 8 + bg;
+    if (has_default) {
+        if (fg < -1)
+            fg = -1;
+        if (bg < -1)
+            bg = -1;
+        return COLOR_PAIR((fg + 1) * 16 + bg + 1);
+    }
+    if (fg < 0)
+        fg = COLOR_WHITE;
+    if (bg < 0)
+        bg = COLOR_BLACK;
+    return COLOR_PAIR((7 - fg) * 8 + bg);
 }