ef2ae0ec8d506bdd6262512b063d176a23d4339c — Ne02ptzero 1 year, 5 months ago 356693d
NEW: utf8 decode string / byte

Signed-off-by: Ne02ptzero <louis@ne02ptzero.me>
6 files changed, 84 insertions(+), 7 deletions(-)

M Makefile
M term.c
M term.h
M utf8.c
M utf8.h
M utils.h
M Makefile => Makefile +3 -1
@@ 9,7 9,9 @@ SRCS    = $(wildcard *.c)
  OBJS    = $(SRCS:%.c=%.o)
  
- all: $(OBJS)
+ all: $(NAME)
+ 
+ $(NAME): $(OBJS)
  	$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
  
  clean:

M term.c => term.c +39 -2
@@ 1,3 1,6 @@+#define _XOPEN_SOURCE
+ #include <wchar.h>
+ 
  #include "term.h"
  #include "config.h"
  #include "utils.h"


@@ 256,6 259,40 @@   void term_putc(term_t *t, uint_least32_t u)
  {
-     (void)u;
-     (void)t;
+     char        c[UTF_SIZ];
+     int         ctrl;
+     int         width;
+     int         len;
+     char_t      *gp;
+ 
+     ctrl = IS_CONTROL(u);
+     if (!IS_SET(t->mode, MODE_UTF8) && !IS_SET(t->mode, MODE_SIXEL))
+     {
+         c[0] = u;
+         width = len - 1;
+     }
+     else
+     {
+         len = utf8_encode(u, c);
+         width = wcwidth(u);
+         if (!ctrl && width == 01)
+         {
+             memcpy(c, STR_UTF_INVALID, sizeof(STR_UTF_INVALID));
+             width = 1;
+         }
+     }
+ 
+     if (t->esc & ESC_STR)
+     {
+         if (u == '\a' || u == 030 || u == 032 || u == 033 || IS_CONTROLC1(u))
+         {
+             t->esc &= ~(ESC_START | ESC_STR | ESC_DCS);
+             t->esc |= ESC_STR_END;
+             goto check_ctrl_code;
+         }
+     }
+ 
+ check_ctrl_code:
+     return ;
+ 
  }

M term.h => term.h +11 -0
@@ 29,6 29,17 @@ CURSOR_LOAD
  };
  
+ enum escape_state {
+     ESC_START      = 1,
+     ESC_CSI        = 2,
+     ESC_STR        = 4,
+     ESC_ALTCHARSET = 8,
+     ESC_STR_END    = 16,
+     ESC_TEST       = 32,
+     ESC_UTF8       = 64,
+     ESC_DCS        = 128,
+ };
+ 
  typedef struct {
      int         row;            /*!< Number of rows */
      int         col;            /*!< Number of columns */

M utf8.c => utf8.c +23 -3
@@ 3,9 3,6 @@ #include "utf8.h"
  #include "utils.h"
  
- #define UTF_INVALID 0xdeadbeef
- #define UTF_SIZ 4
- 
  static const uint8_t        __utf_mask[] = {    0xC0, 0x80,  0xE0,   0xF0,     0xF8};
  static const uint8_t        __utf_byte[] = {    0X80,    0,  0XC0,   0XE0,     0XF0};
  static const uint_least32_t __utf_min[]  = {       0,    0,  0x80,  0x800,  0x10000};


@@ 65,3 62,26 @@       return i;
  }
+ 
+ size_t utf8_encode(uint_least32_t u, char *c)
+ {
+     size_t      len;
+ 
+     len = utf8_validate(&u, 0);
+     if (len > UTF_SIZ)
+         return 0;
+ 
+     for (size_t i = len - 1; i > 0; i--)
+     {
+         c[i] = utf8_encode_byte(u, 0);
+         u >>= 6;
+     }
+ 
+     c[0] = utf8_encode_byte(u, len);
+     return len;
+ }
+ 
+ char utf8_encode_byte(uint_least32_t u, size_t i)
+ {
+     return __utf_byte[i] | (u & ~__utf_mask[i]);
+ }

M utf8.h => utf8.h +6 -0
@@ 4,8 4,14 @@ #include <stdint.h>
  #include <stdlib.h>
  
+ #define UTF_INVALID 0xdeadbe
+ #define STR_UTF_INVALID "\xda\xad\xbe"
+ #define UTF_SIZ 4
+ 
  size_t utf8_decode(const char *c, uint_least32_t *u, size_t len);
  size_t utf8_validate(uint_least32_t *u, size_t i);
  uint_least32_t utf8_decode_byte(char c, size_t *i);
+ size_t utf8_encode(uint_least32_t u, char *c);
+ char utf8_encode_byte(uint_least32_t u, size_t i);
  
  #endif /* UTF8_H */

M utils.h => utils.h +2 -1
@@ 20,7 20,8 @@ #define IS_CONTROLC0(c)  (BETWEEN(c, 1, 0x1f) || (c) == '\177')
  #define IS_CONTROLC1(c)  (BETWEEN(c, 0x80, 0x9f))
  #define IS_CONTROL(c)    (IS_CONTROLC0(c) || IS_CONTROLC1(c))
- 
+ #define QUOTE_ME(x) #x
+ #define STR(x) QUOTE_ME(x)
  
  static inline unsigned short sixd_to_16bit(int x)
  {