sex

simple editor extended using libtermbox
git clone git://z3bra.org/sex
Log | Files | Refs | README

commit c75274dbbe78021049d9fdd35cbcb8f74ffd0ca2
parent 8ade86c5cd8c8229076f86b75bf4d6d5bbd4406a
Author: dcat <dcat@iotek.org>
Date:   Thu Nov 19 00:25:38 2015

first

Diffstat:
 makefile |   4 ++-
 sex.c    | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 sex.h    |   4 ++-
 3 files changed, 113 insertions(+), 0 deletions(-)

diff --git a/makefile b/makefile @@ -0,0 +1,4 @@ +include config.mk + +sex: sex.c + diff --git a/sex.c b/sex.c @@ -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; +} + diff --git a/sex.h b/sex.h @@ -0,0 +1,4 @@ +int init(void); +void cleanup(void); +void open_file(char *); +