From 76855d711d440dd6a00f4b4c7c2556f45384691e Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Fri, 8 Jun 2012 12:13:49 +0100 Subject: [PATCH] Initial hack at a trivial terminal parser dumper program --- .bzrignore | 3 ++ Makefile | 4 ++ bin/vterm-dump.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 bin/vterm-dump.c diff --git a/.bzrignore b/.bzrignore index 0300f520..e58c0366 100644 --- a/.bzrignore +++ b/.bzrignore @@ -2,6 +2,9 @@ *.lo *.la +bin/* +!bin/*.c + pangoterm t/test t/suites.h diff --git a/Makefile b/Makefile index 5043832a..b4f3a617 100644 --- a/Makefile +++ b/Makefile @@ -57,6 +57,10 @@ src/encoding/%.inc: src/encoding/%.tbl src/encoding.lo: $(INCFILES) +bin/%: bin/%.c $(LIBRARY) + @echo CC $< + @$(LIBTOOL) --mode=link --tag=CC $(CC) $(CFLAGS) -o $@ $< -lvterm + t/harness.lo: t/harness.c $(HFILES) @echo CC $< @$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -o $@ -c $< diff --git a/bin/vterm-dump.c b/bin/vterm-dump.c new file mode 100644 index 00000000..561f1f94 --- /dev/null +++ b/bin/vterm-dump.c @@ -0,0 +1,116 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include "vterm.h" + +static int parser_text(const char bytes[], size_t len, void *user) +{ + int i; + for(i = 0; i < len; i++) + if(bytes[i] < 0x20 || (bytes[i] >= 0x80 && bytes[i] < 0xa0)) + break; + + printf("TEXT %.*s\n", i, bytes); + return i; +} + +static int parser_control(unsigned char control, void *user) +{ + printf("CONTROL 0x%02x\n", control); + return 1; +} + +static int parser_escape(const char bytes[], size_t len, void *user) +{ + if(bytes[0] >= 0x20 && bytes[0] < 0x30) { + if(len < 2) + return -1; + len = 2; + } + else { + len = 1; + } + + printf("ESC "); + for(int i = 0; i < len; i++) + printf("%c ", bytes[i]); + printf("\n"); + + return len; +} + +static int parser_csi(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user) +{ + printf("CSI "); + + if(leader && leader[0]) + printf("%s ", leader); + + for(int i = 0; i < argcount; i++) { + if(i) + printf(","); + + if(args[i] == CSI_ARG_MISSING) + printf("*"); + else + printf("%ld%s", CSI_ARG(args[i]), CSI_ARG_HAS_MORE(args[i]) ? "+" : " "); + } + + if(intermed && intermed[0]) + printf("%s ", intermed); + + printf("%c\n", command); + + return 1; +} + +static int parser_osc(const char *command, size_t cmdlen, void *user) +{ + printf("OSC %.*s\n", (int)cmdlen, command); + + return 1; +} + +static int parser_dcs(const char *command, size_t cmdlen, void *user) +{ + printf("DCS %.*s\n", (int)cmdlen, command); + + return 1; +} + +static VTermParserCallbacks parser_cbs = { + .text = &parser_text, + .control = &parser_control, + .escape = &parser_escape, + .csi = &parser_csi, + .osc = &parser_osc, + .dcs = &parser_dcs, +}; + +int main(int argc, char *argv[]) +{ + int fd = open(argv[1], O_RDONLY); + if(fd == -1) { + fprintf(stderr, "Cannot open %s - %s\n", argv[1], strerror(errno)); + exit(1); + } + + /* Size matters not for the parser */ + VTerm *vt = vterm_new(25, 80); + vterm_set_parser_callbacks(vt, &parser_cbs, NULL); + + int len; + char buffer[1024]; + while((len = read(fd, buffer, sizeof(buffer))) > 0) { + vterm_push_bytes(vt, buffer, len); + } + + close(fd); + vterm_free(vt); +}