2#include "../src/vterm_internal.h"
8#define streq(a,b) (!strcmp(a,b))
9#define strstartswith(a,b) (!strncmp(a,b,strlen(b)))
13 char *inpos = s, *outpos = s;
17 if(sscanf(inpos,
"%2x", &ch) < 1)
20 outpos += 1; inpos += 2;
31 switch(((*strp)++)[0]) {
35 default:
return state;
56 for(
int i = 0; keys[i].name; i++) {
67 printf(
"rgb(%d,%d,%d", col->
rgb.red, col->
rgb.green, col->
rgb.blue);
70 printf(
"idx(%d", col->
indexed.idx);
73 printf(
"invalid(%d", col->
type);
76 printf(
",is_default_fg");
79 printf(
",is_default_bg");
90 if(sscanf(*strp,
"rgb(%hhu,%hhu,%hhu)%n", &r, &g, &b, &len) == 3 && len > 0) {
94 else if(sscanf(*strp,
"idx(%hhu)%n", &idx, &len) == 1 && len > 0) {
113 for(
int i = 0; i < len; i++)
114 printf(
"%x%s", (
unsigned char)s[i], i < len-1 ?
"," :
"\n");
120 printf(
"%02x", (
uint8_t)(s++)[0]);
127 for(i = 0; i < len; i++) {
128 unsigned char b = bytes[i];
129 if(b < 0x20 || b == 0x7f || (b >= 0x80 && b < 0xa0))
131 printf(i ?
",%x" :
"%x", b);
140 printf(
"control %02x\n", control);
147 if(bytes[0] >= 0x20 && bytes[0] < 0x30) {
163static int parser_csi(
const char *leader,
const long args[],
int argcount,
const char *intermed,
char command,
void *user)
167 if(leader && leader[0]) {
169 for(
int i = 0; leader[i]; i++)
170 printf(
"%02x", leader[i]);
173 for(
int i = 0; i < argcount; i++) {
174 char sep = i ?
',' :
' ';
182 if(intermed && intermed[0]) {
184 for(
int i = 0; intermed[i]; i++)
185 printf(
"%02x", intermed[i]);
220 for(
int i = 0; i < commandlen; i++)
316 printf(
"movecursor %d,%d\n", pos.
row, pos.
col);
327 printf(
"scrollrect %d..%d,%d..%d => %+d,%+d\n",
329 downward, rightward);
340 printf(
"moverect %d..%d,%d..%d -> %d..%d,%d..%d\n",
356 printf(
"settermprop %d %s\n", prop, val->
boolean ?
"true" :
"false");
359 printf(
"settermprop %d %d\n", prop, val->
number);
362 printf(
"settermprop %d %s\"%.*s\"%s\n", prop,
366 printf(
"settermprop %d ", prop);
388 printf(i ?
",%x" :
"%x",
info->chars[i]);
389 printf(
" %d %d,%d",
info->width, pos.
row, pos.
col);
390 if(
info->protected_cell)
395 printf(
" dhl-%s",
info->dhl == 1 ?
"top" :
info->dhl == 2 ?
"bottom" :
"?" );
407 printf(
"erase %d..%d,%d..%d%s\n",
409 selective ?
" selective" :
"");
485 printf(
"sb_clear\n");
503 printf(
"selection-set mask=%04X ", mask);
516 printf(
"selection-query mask=%04X\n", mask);
533 printf(
"damage %d..%d,%d..%d",
554 printf(
" ="), equals =
true;
557 for(
int col = rect.
start_col; col < eol; col++) {
578 while(eol && !cells[eol-1].chars[0])
581 printf(
"sb_pushline %d =",
cols);
582 for(
int c = 0; c < eol; c++)
583 printf(
" %02X", cells[c].chars[0]);
595 for(
int col = 0; col <
cols; col++) {
597 cells[col].
chars[0] =
'A' + col;
599 cells[col].
chars[0] = 0;
601 cells[col].
width = 1;
604 printf(
"sb_popline %d\n",
cols);
613 printf(
"sb_clear\n");
629 char line[1024] = {0};
634 setvbuf(stdout, NULL, _IONBF, 0);
636 while(fgets(line,
sizeof line, stdin)) {
640 if((nl = strchr(line,
'\n')))
643 if(
streq(line,
"INIT")) {
650 else if(
streq(line,
"WANTPARSER")) {
655 else if(
strstartswith(line,
"WANTSTATE") && (line[9] ==
'\0' || line[9] ==
' ')) {
670 while(line[i] ==
' ')
702 fprintf(stderr,
"Unrecognised WANTSTATE flag '%c'\n", line[i]);
706 else if(
strstartswith(line,
"WANTSCREEN") && (line[10] ==
'\0' || line[10] ==
' ')) {
714 while(line[i] ==
' ')
747 fprintf(stderr,
"Unrecognised WANTSCREEN flag '%c'\n", line[i]);
751 else if(sscanf(line,
"UTF8 %d", &flag)) {
755 else if(
streq(line,
"RESET")) {
767 char *linep = line + 7;
768 while(linep[0] ==
' ')
770 sscanf(linep,
"%d, %d", &
rows, &
cols);
775 char *bytes = line + 5;
781 fprintf(stderr,
"! short write\n");
784 else if(
streq(line,
"WANTENCODING")) {
794 char *bytes = line + 6;
803 cp, &cpi, len, bytes, &pos, len);
807 for(
int i = 0; i < cpi; i++) {
808 printf(i ?
",%x" :
"%x", cp[i]);
815 char *linep = line + 7;
817 while(linep[0] ==
' ')
820 sscanf(linep,
" %x", &c);
826 char *linep = line + 6;
827 while(linep[0] ==
' ')
830 while(linep[0] ==
' ')
838 char *linep = line + 6;
839 if(
streq(linep,
"START"))
841 else if(
streq(linep,
"END"))
849 char *linep = line + 6;
850 if(
streq(linep,
"IN"))
852 else if(
streq(linep,
"OUT"))
859 char *linep = line + 10;
861 while(linep[0] ==
' ')
863 sscanf(linep,
"%d,%d%n", &row, &col, &len);
865 while(linep[0] ==
' ')
872 char *linep = line + 9;
875 while(linep[0] ==
' ')
877 sscanf(linep,
"%c %d%n", &press, &button, &len);
879 while(linep[0] ==
' ')
887 char *linep = line + 10;
891 sscanf(linep,
"%x%n", &mask, &len);
893 while(linep[0] ==
' ')
895 if(linep[0] ==
'[') {
898 while(linep[0] ==
' ')
905 linep += frag.
len * 2;
906 while(linep[0] ==
' ')
908 if(linep[0] ==
']') {
916 char *linep = line + 12;
917 while(linep[0] ==
' ')
919 if(
streq(linep,
"CELL"))
921 else if(
streq(linep,
"ROW"))
923 else if(
streq(linep,
"SCREEN"))
925 else if(
streq(linep,
"SCROLL"))
936 char *linep = line + 14;
937 while(linep[0] ==
' ')
941 while(linep[0] ==
' ')
951 else if(line[0] ==
'?') {
952 if(
streq(line,
"?cursor")) {
957 printf(
"! row mismatch: state=%d,%d event=%d,%d\n",
960 printf(
"! col mismatch: state=%d,%d event=%d,%d\n",
967 char *linep = line + 5;
968 while(linep[0] ==
' ')
972#define BOOLSTR(v) ((v) ? "on" : "off")
974 if(
streq(linep,
"bold")) {
977 printf(
"! pen bold mismatch; state=%s, event=%s\n",
982 else if(
streq(linep,
"underline")) {
985 printf(
"! pen underline mismatch; state=%d, event=%d\n",
990 else if(
streq(linep,
"italic")) {
993 printf(
"! pen italic mismatch; state=%s, event=%s\n",
998 else if(
streq(linep,
"blink")) {
1001 printf(
"! pen blink mismatch; state=%s, event=%s\n",
1006 else if(
streq(linep,
"reverse")) {
1009 printf(
"! pen reverse mismatch; state=%s, event=%s\n",
1014 else if(
streq(linep,
"font")) {
1017 printf(
"! pen font mismatch; state=%d, event=%d\n",
1022 else if(
streq(linep,
"small")) {
1025 printf(
"! pen small mismatch; state=%s, event=%s\n",
1030 else if(
streq(linep,
"baseline")) {
1033 printf(
"! pen baseline mismatch: state=%d, event=%d\n",
1040 else if(
streq(linep,
"foreground")) {
1044 else if(
streq(linep,
"background")) {
1053 char *linep = line + 10;
1056 while(linep[0] ==
' ')
1058 if(sscanf(linep,
"%d", &row) < 1) {
1059 printf(
"! lineinfo unrecognised input\n");
1063 if(
info->doublewidth)
1065 if(
info->doubleheight)
1067 if(
info->continuation)
1073 char *linep = line + 13;
1076 while(linep[0] ==
' ')
1080 else if(sscanf(linep,
"%d", &rect.
start_row) == 1) {
1086 printf(
"! screen_chars unrecognised input\n");
1090 if(len == (
size_t)-1)
1091 printf(
"! screen_chars error\n");
1097 for(
size_t i = 0; i < len; i++) {
1098 printf(
"0x%02x%s", chars[i], i < len-1 ?
"," :
"\n");
1105 char *linep = line + 12;
1108 while(linep[0] ==
' ')
1111 printf(
"! screen_text unrecognised input\n");
1115 if(len == (
size_t)-1)
1116 printf(
"! screen_text error\n");
1121 unsigned char *buffer =
malloc(len + 4);
1122 unsigned char *text = buffer + 2;
1123 text[-2] = 0x55; text[-1] = 0xAA;
1124 text[len] = 0x55; text[len+1] = 0xAA;
1128 if(text[-2] != 0x55 || text[-1] != 0xAA)
1129 printf(
"! screen_get_text buffer overrun left [%02x,%02x]\n", text[-2], text[-1]);
1130 else if(text[len] != 0x55 || text[len+1] != 0xAA)
1131 printf(
"! screen_get_text buffer overrun right [%02x,%02x]\n", text[len], text[len+1]);
1133 for(
size_t i = 0; i < len; i++) {
1134 printf(
"0x%02x%s", text[i], i < len-1 ?
"," :
"\n");
1142 char *linep = line + 12;
1144 while(linep[0] ==
' ')
1146 if(sscanf(linep,
"%d,%d\n", &pos.
row, &pos.
col) < 2) {
1147 printf(
"! screen_cell unrecognised input\n");
1155 printf(
"%s0x%x", i ?
"," :
"", cell.
chars[i]);
1157 printf(
"} width=%d attrs={", cell.
width);
1170 if(cell.
attrs.
dhl) printf(
"dhl-%s ", cell.
attrs.
dhl == 2 ?
"bottom" :
"top");
1181 char *linep = line + 12;
1182 while(linep[0] ==
' ')
1185 if(sscanf(linep,
"%d,%d\n", &pos.
row, &pos.
col) < 2) {
1186 printf(
"! screen_eol unrecognised input\n");
1193 char *linep = line + 21;
1194 while(linep[0] ==
' ')
1197 if(sscanf(linep,
"%d,%d\n", &pos.
row, &pos.
col) < 2) {
1198 printf(
"! screen_attrs_extent unrecognised input\n");
1206 printf(
"! screen_attrs_extent failed\n");
1214 memset(line, 0,
sizeof line);
1219 abort_line: err = 1;
1223 char outbuff[outlen];
1229 printf(err ?
"?\n" :
"DONE\n");
VTermEncoding * vterm_lookup_encoding(VTermEncodingType type, char designation)
static int state_erase(VTermRect rect, int selective, void *user)
static int screen_sb_pushline(int cols, const VTermScreenCell *cells, void *user)
static size_t inplace_hex2bytes(char *s)
static int scrollrect(VTermRect rect, int downward, int rightward, void *user)
static VTermModifier strpe_modifiers(char **strp)
static int want_screen_scrollback
static int state_sb_clear(void *user)
static int parser_control(unsigned char control, void *user)
static int parser_pm(VTermStringFragment frag, void *user)
static VTermScreen * screen
static struct @29 state_pen
int main(int argc, char **argv)
static int state_putglyph(VTermGlyphInfo *info, VTermPos pos, void *user)
static VTermState * state
static int parser_text(const char bytes[], size_t len, void *user)
static int parser_osc(int command, VTermStringFragment frag, void *user)
static int state_setpenattr(VTermAttr attr, VTermValue *val, void *user)
static int want_state_scrollback
static void printhex(const char *s, size_t len)
static int screen_sb_popline(int cols, VTermScreenCell *cells, void *user)
static VTermColor strpe_color(char **strp)
static VTermEncodingInstance encoding
static void term_output(const char *s, size_t len, void *user)
static int want_settermprop
static int parser_csi(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user)
static int movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user)
static int want_state_erase
static int screen_damage(VTermRect rect, void *user)
static int selection_set(VTermSelectionMask mask, VTermStringFragment frag, void *user)
static int screen_sb_clear(void *user)
static int selection_query(VTermSelectionMask mask, void *user)
static void print_color(const VTermColor *col)
static VTermParserCallbacks parser_cbs
VTermStateCallbacks state_cbs
static int state_setlineinfo(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user)
static int want_movecursor
static int want_state_putglyph
static VTermStateFallbacks fallbacks
static int parser_sos(VTermStringFragment frag, void *user)
static int settermprop(VTermProp prop, VTermValue *val, void *user)
static VTermKey strp_key(char *str)
static VTermPos state_pos
static int moverect(VTermRect dest, VTermRect src, void *user)
#define strstartswith(a, b)
static int parser_escape(const char bytes[], size_t len, void *user)
static int want_scrollrect
static int parser_apc(VTermStringFragment frag, void *user)
VTermSelectionCallbacks selection_cbs
VTermScreenCallbacks screen_cbs
static int parser_dcs(const char *command, size_t commandlen, VTermStringFragment frag, void *user)
static int want_screen_damage_cells
static int want_screen_damage
void * memset(void *dest, int c, size_t size)
void * malloc(size_t size)
char name[256]
Zero terminated string with the name of the file.
enum MessageType type
Type of the message.
void(* init)(VTermEncoding *enc, void *data)
void(* decode)(VTermEncoding *enc, void *data, uint32_t cp[], int *cpi, int cplen, const char bytes[], size_t *pos, size_t len)
int(* text)(const char *bytes, size_t len, void *user)
int(* damage)(VTermRect rect, void *user)
int(* set)(VTermSelectionMask mask, VTermStringFragment frag, void *user)
int(* putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user)
int(* control)(unsigned char control, void *user)
void vterm_set_utf8(VTerm *vt, int is_utf8)
#define VTERM_MAX_CHARS_PER_CELL
void vterm_state_send_selection(VTermState *state, VTermSelectionMask mask, VTermStringFragment frag)
struct VTermColor::@11 indexed
void vterm_screen_enable_reflow(VTermScreen *screen, bool reflow)
void vterm_screen_flush_damage(VTermScreen *screen)
void vterm_state_set_selection_callbacks(VTermState *state, const VTermSelectionCallbacks *callbacks, void *user, char *buffer, size_t buflen)
void vterm_screen_set_default_colors(VTermScreen *screen, const VTermColor *default_fg, const VTermColor *default_bg)
void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user)
void vterm_keyboard_end_paste(VTerm *vt)
void vterm_screen_reset(VTermScreen *screen, int hard)
size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect)
void vterm_screen_convert_color_to_rgb(const VTermScreen *screen, VTermColor *col)
void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright)
#define VTERM_COLOR_IS_INDEXED(col)
static void vterm_color_rgb(VTermColor *col, uint8_t red, uint8_t green, uint8_t blue)
void vterm_mouse_move(VTerm *vt, int row, int col, VTermModifier mod)
#define CSI_ARG_HAS_MORE(a)
void vterm_state_reset(VTermState *state, int hard)
int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos)
uint32_t chars[VTERM_MAX_CHARS_PER_CELL]
void vterm_parser_set_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user)
void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos)
#define VTERM_COLOR_IS_DEFAULT_FG(col)
int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell)
#define VTERM_COLOR_IS_RGB(col)
size_t vterm_output_read(VTerm *vt, char *buffer, size_t len)
size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect)
void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user)
void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod)
const VTermLineInfo * vterm_state_get_lineinfo(const VTermState *state, int row)
size_t vterm_output_get_buffer_current(const VTerm *vt)
VTermScreenCellAttrs attrs
void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp)
void vterm_state_focus_out(VTermState *state)
VTermStringFragment string
void vterm_keyboard_start_paste(VTerm *vt)
VTermState * vterm_obtain_state(VTerm *vt)
VTermScreen * vterm_obtain_screen(VTerm *vt)
void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod)
void vterm_state_focus_in(VTermState *state)
void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen)
int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val)
VTermValueType vterm_get_prop_type(VTermProp prop)
void vterm_output_set_callback(VTerm *vt, VTermOutputCallback *func, void *user)
int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs)
void vterm_set_size(VTerm *vt, int rows, int cols)
static void vterm_color_indexed(VTermColor *col, uint8_t idx)
#define VTERM_COLOR_IS_DEFAULT_BG(col)
void vterm_state_set_unrecognised_fallbacks(VTermState *state, const VTermStateFallbacks *fallbacks, void *user)
void vterm_free(VTerm *vt)
void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size)
VTerm * vterm_new(int rows, int cols)
void vterm_mouse_button(VTerm *vt, int button, bool pressed, VTermModifier mod)
size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len)
struct VTermColor::@10 rgb
char data[4 *sizeof(uint32_t)]
#define VTERM_KEY_FUNCTION(n)