diff options
Diffstat (limited to 'ui.c')
-rw-r--r-- | ui.c | 104 |
1 files changed, 91 insertions, 13 deletions
@@ -1,3 +1,5 @@ +#include <chibi/sexp.h> +#include <string.h> #define _POSIX_C_SOURCE 200809L #include <ctype.h> @@ -11,7 +13,12 @@ #include <ncurses.h> -#include "format.h" +#include <chibi/eval.h> + +struct segment { + size_t start, end; + const char *description; +}; #define LINE_LEN 0x10 /* Number of bytes in a line */ @@ -51,7 +58,7 @@ void draw_hex(struct segment *current) { if (screen.hy == y && screen.hx == x) { wattron(screen.hex, A_REVERSE); } - mvwprintw(screen.hex, y, x * 2 + (x/2), "%02x", + mvwprintw(screen.hex, y, x * 2 + (x/2), "%02x", 0xFF & ((char*) screen.mem)[addr]); if (screen.hy == y && screen.hx == x) { wattroff(screen.hex, A_REVERSE); @@ -100,24 +107,72 @@ void draw_info(struct segment *current) { wrefresh(screen.info); } -void draw_screen() { - struct segment current = get_segment( - screen.mem, - screen.mem_len, - screen.top_address + screen.hy * LINE_LEN + screen.hx); +void draw_screen(sexp ctx, sexp bv) { + + sexp_gc_var3(proc_name, addr, ret); + sexp_gc_preserve3(ctx, proc_name, addr, ret); + proc_name = sexp_intern(ctx, "get-segment", -1); + size_t c_addr = screen.top_address + screen.hy * LINE_LEN + screen.hx; + addr = sexp_make_fixnum(c_addr); + struct segment current; + if (c_addr < 0 || c_addr >= screen.mem_len) { + current = (struct segment) { + .start = -1, + .end = -1, + .description = "No Segment", + }; + } else { + ret = sexp_eval(ctx, sexp_list3(ctx, proc_name, bv, addr), NULL); + + if (! sexp_vectorp(ret)) { + endwin(); + fprintf(stderr, "get-segment[%lu] failed\n", c_addr); + sexp_debug(ctx, "addr = ", addr); + if (sexp_exceptionp(ret)) { + printf("An exception was thrown\n"); + sexp_print_exception(ctx, ret, sexp_current_output_port(ctx)); + } else { + sexp_debug(ctx, "Unexpecetd return value: ", ret); + } + exit(1); + } + + current = (struct segment) { + .start = sexp_unbox_fixnum(sexp_vector_ref(ret, SEXP_ZERO)), + .end = sexp_unbox_fixnum(sexp_vector_ref(ret, SEXP_ONE)), + .description = sexp_string_data(sexp_vector_ref(ret, SEXP_TWO)) + }; + } draw_lines(); draw_hex(¤t); draw_chr(¤t); draw_info(¤t); /* TODO end at end of file */ + + sexp_gc_release3(ctx); } int main(int argc, char *argv[]) { + char *errmsg = NULL; + int errcode = 0; + /* Terminal supporting mouse movements */ setenv("TERM", "xterm-1003", true); + sexp_scheme_init(); + sexp ctx = sexp_make_eval_context(NULL, NULL, NULL, 0, 0); + sexp_load_standard_env(ctx, NULL, SEXP_SEVEN); + sexp_load_standard_ports(ctx, NULL, NULL, stdout, stderr, 1); + + sexp_gc_var2(obj1, bv); + sexp_gc_preserve2(ctx, obj1, bv); + obj1 = sexp_c_string(ctx, "gz.scm", -1); + sexp_load(ctx, obj1, NULL); + + // sexp_eval_string(ctx, "(display \"Hello, from Scheme!\n\")", -1, NULL); + initscr(); clear(); noecho(); @@ -147,19 +202,37 @@ int main(int argc, char *argv[]) { } int fd = open(filename, O_RDONLY); + if (fd == -1) { + errmsg = "Failed to open file"; + errcode = errno; + goto end; + } struct stat statbuf; if (fstat(fd, &statbuf) == -1) { + errmsg = "Failed to stat file"; + errcode = errno; goto end; } - screen.mem_len = statbuf.st_size; - screen.mem = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0); + + + uint8_t *mem = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0); close(fd); + screen.mem_len = statbuf.st_size; + screen.mem = mem; + + bv = sexp_make_bytes(ctx, sexp_make_fixnum(statbuf.st_size), SEXP_ZERO); + for (int i = 0; i < statbuf.st_size; i++) { + sexp_bytes_set(bv, + sexp_make_fixnum(i), + sexp_make_fixnum(mem[i])); + } + mousemask(ALL_MOUSE_EVENTS|REPORT_MOUSE_POSITION, NULL); refresh(); - draw_screen(); + draw_screen(ctx, bv); int ch; MEVENT event; @@ -193,21 +266,21 @@ int main(int argc, char *argv[]) { screen.hy = -1; } - draw_screen(); + draw_screen(ctx, bv); break; case ERR: // wprintw(win, "err"); break; } - draw_screen(); + draw_screen(ctx, bv); refresh(); break; case KEY_RESIZE: wresize(screen.lineno, LINES - INFO_HEIGHT, 0); wresize(screen.hex, LINES - INFO_HEIGHT, 0); wresize(screen.chr, LINES - INFO_HEIGHT, 0); - draw_screen(); + draw_screen(ctx, bv); // mvprintw(0, 0, "%ix%i", COLS, LINES); // refresh(); break; @@ -223,5 +296,10 @@ end: delwin(screen.chr); delwin(screen.info); endwin(); + + if (errcode) { + printf("%s: %s\n", errmsg, strerror(errcode)); + } + return 0; } |