X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=visor;a=blobdiff_plain;f=visor%2Fsrc%2Fmain_unix.c;h=96d59cb7a09e85c596ccd4877208f71bdc404a10;hp=9db4e36315f408f5609529bbfa843609f485724a;hb=94c867869ece77f27cf3c8b637d44e13d86f5b88;hpb=24f7b6535c6c25040f4a2f2c655a913c177531eb diff --git a/visor/src/main_unix.c b/visor/src/main_unix.c index 9db4e36..96d59cb 100644 --- a/visor/src/main_unix.c +++ b/visor/src/main_unix.c @@ -1,13 +1,54 @@ #include +#include +#include +#include +#include +#include #include "term.h" +#include "visor.h" + +struct file { + int fd; + void *maddr; + size_t msize; +}; static int parse_args(int argc, char **argv); static int init(void); static void cleanup(void); +/* file operations */ +static vi_file *file_open(const char *path, unsigned int flags); +static void file_close(vi_file *file); +static long file_size(vi_file *file); +static void *file_map(vi_file *file); +static void file_unmap(vi_file *file); +static long file_read(vi_file *file, void *buf, long count); +static long file_write(vi_file *file, void *buf, long count); +static long file_seek(vi_file *file, long offs, int whence); + +static struct visor *vi; static int num_fpaths; static char **fpaths; +static struct vi_alloc alloc = { + malloc, free, realloc +}; + +static struct vi_fileops fops = { + file_open, file_close, file_size, + file_map, file_unmap, + file_read, file_write, file_seek +}; + +/* +static struct vi_ttyops ttyops = { + tty_clear, tty_clear_line, tty_clear_line_at, + tty_setcursor, tty_putchar, tty_putchar_at, + tty_scroll, tty_del_back, tty_del_fwd, tty_status +}; +*/ + int main(int argc, char **argv) { if(parse_args(argc, argv) == -1) { @@ -58,13 +99,105 @@ static int init(void) } term_clear(); + if(!(vi = vi_create(&alloc))) { + return -1; + } + vi_set_fileops(vi, &fops); + for(i=0; ifd = open(path, flags)) == -1) { + free(file); + return 0; + } + return (vi_file*)file; +} + +static void file_close(vi_file *vif) +{ + struct file *file = vif; + if(!file) return; + + if(file->fd >= 0) { + if(file->maddr) { + file_unmap(file); + } + close(file->fd); + } + free(file); +} + +static long file_size(vi_file *vif) +{ + struct file *file = vif; + struct stat st; + + if(fstat(file->fd, &st) == -1) { + return -1; + } + return st.st_size; +} + +static void *file_map(vi_file *vif) +{ + struct file *file = vif; + long sz; + + if((sz = file_size(file)) == -1) { + return 0; + } + if((file->maddr = mmap(0, sz, PROT_READ, MAP_PRIVATE, file->fd, 0)) == (void*)-1) { + return 0; + } + file->msize = sz; + return file->maddr; +} + +static void file_unmap(vi_file *vif) +{ + struct file *file = vif; + if(file->maddr) { + munmap(file->maddr, file->msize); + } + file->maddr = 0; +} + +static long file_read(vi_file *vif, void *buf, long count) +{ + struct file *file = vif; + return read(file->fd, buf, count); +} + +static long file_write(vi_file *vif, void *buf, long count) +{ + struct file *file = vif; + return write(file->fd, buf, count); +} + +static long file_seek(vi_file *vif, long offs, int whence) +{ + struct file *file = vif; + return lseek(file->fd, offs, whence); +}