gcc says those values are out of bounds
[apps/madtty.git] / madtty / madtty.c
index ef1230b..b876a73 100644 (file)
@@ -24,6 +24,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <langinfo.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -36,7 +37,7 @@
 # include <pty.h>
 #elif defined(__FreeBSD__)
 # include <libutil.h>
-#elif defined(__OpenBSD__)
+#elif defined(__OpenBSD__) || defined(__NetBSD__)
 # include <util.h>
 #endif
 #include "madtty.h"
@@ -47,7 +48,7 @@
 
 #define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
 
-static int has_default = 0;
+static int has_default, is_utf8;
 
 enum {
     C0_NUL = 0x00,
@@ -105,6 +106,8 @@ struct madtty_t {
     char rbuf[BUFSIZ];
     char ebuf[BUFSIZ];
     int  rlen, elen;
+    madtty_handler_t handler;
+    void *data;
 };
 
 typedef struct t_row_t {
@@ -596,11 +599,17 @@ static void es_interpret_csi(madtty_t *t)
 static void try_interpret_escape_seq(madtty_t *t)
 {
     char lastchar  = t->ebuf[t->elen-1];
-
+    if(!*t->ebuf)
+       return;
+    if(t->handler){
+       switch((*(t->handler))(t, t->ebuf)){
+          case MADTTY_HANDLER_OK:
+            goto cancel;
+          case MADTTY_HANDLER_NOTYET:
+            return;
+       }
+    }
     switch (*t->ebuf) {
-      case '\0':
-        return;
-
       case 'M':
         interpret_csi_SR(t);
         cancel_escape_sequence(t);
@@ -687,13 +696,10 @@ 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);
+    const char *cset = nl_langinfo(CODESET) ?: "ANSI_X3.4-1968";
+    is_utf8 = !strcmp(cset, "UTF-8");
 }
 
 // vt100 special graphics and line drawing
@@ -732,13 +738,15 @@ void madtty_init_vt100_graphics(void)
     vt100['f' - 0x41] = ACS_DEGREE;
     vt100['g' - 0x41] = ACS_PLMINUS;
     vt100['~' - 0x41] = ACS_BULLET;
+#if 0 /* out of bounds */
     vt100[',' - 0x41] = ACS_LARROW;
     vt100['+' - 0x41] = ACS_RARROW;
     vt100['.' - 0x41] = ACS_DARROW;
     vt100['-' - 0x41] = ACS_UARROW;
+    vt100['0' - 0x41] = ACS_BLOCK;
+#endif
     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;
@@ -771,10 +779,9 @@ static void madtty_putc(madtty_t *t, wchar_t wc)
 
         if (t->graphmode) {
             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];
+                wchar_t gc = is_utf8 ? vt100_utf8[wc - 0x41] : vt100[wc - 0x41];
+                if (gc)
+                    wc = gc;
             }
             width = 1;
         } else {
@@ -968,7 +975,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 (is_utf8 && row->text[j] >= 128) {
+            if (row->text[j] >= 128) {
                 char buf[MB_CUR_MAX + 1];
                 int len;
 
@@ -1064,11 +1071,11 @@ void madtty_init_colors(void)
                               fg == COLOR_WHITE ? -1 : fg,
                               bg == COLOR_BLACK ? -1 : bg);
                 } else {
-                init_pair((7 - fg) * 8 + bg, fg, bg);
+                    init_pair((7 - fg) * 8 + bg, fg, bg);
+                }
             }
         }
     }
-    }
 }
 
 int madtty_color_pair(int fg, int bg)
@@ -1086,3 +1093,18 @@ int madtty_color_pair(int fg, int bg)
         bg = COLOR_BLACK;
     return COLOR_PAIR((7 - fg) * 8 + bg);
 }
+
+void madtty_set_handler(madtty_t *t, madtty_handler_t handler)
+{
+    t->handler = handler;
+}
+
+void madtty_set_data(madtty_t *t, void *data)
+{
+    t->data = data;
+}
+
+void *madtty_get_data(madtty_t *t)
+{
+    return t->data;
+}