lt

simple terminal emulator
Log | Files | Refs | git clone https://git.ne02ptzero.me/git/lt

commit ef2ae0ec8d506bdd6262512b063d176a23d4339c
parent 356693dd2dc9b194791e10848aa7b6dcf2bec784
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Sun, 10 Jun 2018 03:17:33 +0200

NEW: utf8 decode string / byte

Signed-off-by: Ne02ptzero <louis@ne02ptzero.me>

Diffstat:
MMakefile | 4+++-
Mterm.c | 41+++++++++++++++++++++++++++++++++++++++--
Mterm.h | 11+++++++++++
Mutf8.c | 26+++++++++++++++++++++++---
Mutf8.h | 6++++++
Mutils.h | 3++-
6 files changed, 84 insertions(+), 7 deletions(-)

diff --git a/Makefile b/Makefile @@ -9,7 +9,9 @@ CFLAGS = $(SFLAGS) $(IFLAGS) $(LFLAGS) SRCS = $(wildcard *.c) OBJS = $(SRCS:%.c=%.o) -all: $(OBJS) +all: $(NAME) + +$(NAME): $(OBJS) $(CC) $(CFLAGS) $(OBJS) -o $(NAME) clean: diff --git a/term.c b/term.c @@ -1,3 +1,6 @@ +#define _XOPEN_SOURCE +#include <wchar.h> + #include "term.h" #include "config.h" #include "utils.h" @@ -256,6 +259,40 @@ int term_write(term_t *term, const char *buf, int len, bool show_ctrl) 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 ; + } diff --git a/term.h b/term.h @@ -29,6 +29,17 @@ enum cursor_mouvement { 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 */ diff --git a/utf8.c b/utf8.c @@ -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 @@ size_t utf8_validate(uint_least32_t *u, size_t i) 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]); +} diff --git a/utf8.h b/utf8.h @@ -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 */ diff --git a/utils.h b/utils.h @@ -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) {