tfirst - sex - libtermbox based text editor
HTML git clone git://z3bra.org/sex
DIR Log
DIR Files
DIR Refs
DIR README
---
DIR commit c75274dbbe78021049d9fdd35cbcb8f74ffd0ca2
DIR parent 8ade86c5cd8c8229076f86b75bf4d6d5bbd4406a
HTML Author: dcat <dcat@iotek.org>
Date: Thu, 19 Nov 2015 00:25:38 +0100
first
Diffstat:
M makefile | 4 ++++
M sex.c | 105 +++++++++++++++++++++++++++++++
M sex.h | 4 ++++
3 files changed, 113 insertions(+), 0 deletions(-)
---
DIR diff --git a/makefile b/makefile
t@@ -0,0 +1,4 @@
+include config.mk
+
+sex: sex.c
+
DIR diff --git a/sex.c b/sex.c
t@@ -0,0 +1,105 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <termbox.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <err.h>
+
+#include "arg.h"
+#include "sex.h"
+
+struct ln_s {
+ char *p;
+ size_t len;
+ struct ln_s *prev;
+ struct ln_s *next;
+};
+
+struct file_s {
+ char *path;
+ char *map;
+ char *p;
+ size_t size;
+ int d;
+ struct ln_s *ln;
+};
+
+static struct file_s f;
+
+void
+cleanup(void) {
+ munmap(f.map, f.size);
+ close(f.d);
+ tb_shutdown();
+}
+
+void
+init_termbox(void)
+{
+ int r;
+
+ r = tb_init();
+ if (r)
+ err(1, "tb_init()");
+}
+
+void
+open_file(char *path)
+{
+ size_t i;
+ int d;
+ struct stat s;
+
+ f.path = path;
+
+ d = open(f.path, O_RDWR);
+ if (d < 0)
+ err(1, "open()");
+
+ if (fstat(d, &s))
+ err(1, "fstat()");
+
+ f.size = (size_t)s.st_size;
+
+ f.map = mmap(NULL, f.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, d, 0);
+ if (f.map == MAP_FAILED)
+ err(1, "mmap()");
+
+ i = f.size;
+ f.p = f.map;
+
+ while (--i)
+ if (*++f.map == '\n') {
+ *f.map = '\0';
+ }
+}
+
+void
+edit(char *path) {
+ open_file(path);
+}
+
+int
+main(int argc, char **argv)
+{
+ char *argv0; /* shadow */
+
+ ARGBEGIN {
+ case 'V':
+ /* print version */
+ break;
+ } ARGEND
+
+ init_termbox();
+ atexit(cleanup);
+
+ while (*argv)
+ edit(*argv++);
+
+ return 0;
+}
+
DIR diff --git a/sex.h b/sex.h
t@@ -0,0 +1,4 @@
+int init(void);
+void cleanup(void);
+void open_file(char *);
+