+
+static void resized(int x, int y)
+{
+ vi_term_size(vi, x, y);
+}
+
+static vi_file *file_open(const char *path, unsigned int flags)
+{
+ struct file *file;
+
+ if(!(file = calloc(1, sizeof *file))) {
+ return 0;
+ }
+ if((file->fd = 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);
+}
+
+/* tty operations */
+
+static void tty_clear(void *cls)
+{
+ term_clear();
+}
+
+static void tty_clear_line(void *cls)
+{
+ /* TODO */
+}
+
+static void tty_clear_line_at(int y, void *cls)
+{
+ term_setcursor(y, 0);
+ /* TODO */
+}
+
+static void tty_setcursor(int x, int y, void *cls)
+{
+ term_setcursor(y, x);
+}
+
+static void tty_putchar(char c, void *cls)
+{
+ term_putchar(c);
+}
+
+static void tty_putchar_at(int x, int y, char c, void *cls)
+{
+ term_setcursor(y, x);
+ term_putchar(c);
+}
+
+static void tty_scroll(int nlines, void *cls)
+{
+ /* TODO */
+}
+
+static void tty_del_back(void *cls)
+{
+ /* TODO */
+}
+
+static void tty_del_fwd(void *cls)
+{
+ /* TODO */
+}
+
+static void tty_status(char *s, void *cls)
+{
+ /* TODO */
+}
+
+static void tty_flush(void *cls)
+{
+ term_flush();
+}