#include <stdio.h>
-#include <signal.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <termios.h>
-#include <sys/ioctl.h>
+#include <stdlib.h>
+#include "term.h"
+#include "visor.h"
+struct file {
+ FILE *fp;
+ void *maddr;
+ size_t msize;
+};
+
+static int parse_args(int argc, char **argv);
static int init(void);
static void cleanup(void);
-static void sighandler(int s);
-
-int term_width, term_height;
-int ttyfd;
-struct termios saved_term;
+/* 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)
{
- int res;
- char c;
-
+ if(parse_args(argc, argv) == -1) {
+ return 1;
+ }
if(init() == -1) {
return 1;
}
for(;;) {
- if((res = read(ttyfd, &c, 1)) == 0 || (res < 0 && errno != EINTR)) {
- break;
+ int c = term_getchar();
+
+ switch(c) {
+ case 27:
+ case 'q':
+ goto end;
}
- /* proc input */
}
+end:
cleanup();
return 0;
}
+static int parse_args(int argc, char **argv)
+{
+ int i;
+
+ fpaths = argv + 1;
+ num_fpaths = 0;
+ for(i=1; i<argc; i++) {
+ if(argv[i][0] == '-') {
+ fprintf(stderr, "invalid option: %s\n", argv[i]);
+ return -1;
+ } else {
+ argv[++num_fpaths] = argv[i];
+ }
+ }
+ return 0;
+}
+
static int init(void)
{
- struct termios term;
- struct winsize winsz;
+ int i;
- if((ttyfd = open("/dev/tty", O_RDWR)) == -1) {
- perror("failed to open /dev/tty");
+ if(term_init(0) == -1) {
return -1;
}
- if(tcgetattr(ttyfd, &term) == -1) {
- perror("failed to get terminal attr");
- return -1;
- }
- saved_term = term;
- term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
- term.c_oflag &= ~OPOST;
- term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
- term.c_cflag = (term.c_cflag & ~(CSIZE | PARENB)) | CS8;
-
- if(tcsetattr(ttyfd, TCSAFLUSH, &term) == -1) {
- perror("failed to change terminal attributes");
+ term_clear();
+
+ if(!(vi = vi_create(&alloc))) {
return -1;
}
+ vi_set_fileops(vi, &fops);
- ioctl(1, TIOCGWINSZ, &winsz);
- term_width = winsz.ws_col;
- term_height = winsz.ws_row;
-
- signal(SIGWINCH, sighandler);
-
- write(ttyfd, "\033[2J", 4);
-
+ for(i=0; i<num_fpaths; i++) {
+ /* open fpaths[i] */
+ }
return 0;
}
static void cleanup(void)
{
- tcsetattr(ttyfd, TCSAFLUSH, &saved_term);
- close(ttyfd);
+ if(vi) {
+ vi_destroy(vi);
+ }
+ term_cleanup();
}
-static void sighandler(int s)
+static vi_file *file_open(const char *path, unsigned int flags)
{
- struct winsize winsz;
+ struct file *file;
+ const char *attr;
- signal(s, sighandler);
+ switch(flags & 0xff) {
+ case VI_RDONLY:
+ attr = "rb";
+ break;
- switch(s) {
- case SIGWINCH:
- ioctl(1, TIOCGWINSZ, &winsz);
- term_width = winsz.ws_col;
- term_height = winsz.ws_row;
- /* redraw */
+ case VI_WRONLY:
+ attr = (flags & VI_CREAT) ? "w+b" : "wb";
break;
- default:
+ case VI_RDWR:
+ attr = (flags & VI_CREAT) ? "w+b" : "r+b";
break;
+
+ default:
+ return 0;
+ }
+
+
+ if(!(file = calloc(1, sizeof *file))) {
+ return 0;
}
+ if(!(file->fp = fopen(path, attr))) {
+ free(file);
+ return 0;
+ }
+ return file;
+}
+
+static void file_close(vi_file *file)
+{
+}
+
+static long file_size(vi_file *file)
+{
+ return -1;
+}
+
+static void *file_map(vi_file *file)
+{
+ return 0;
+}
+
+static void file_unmap(vi_file *file)
+{
+}
+
+static long file_read(vi_file *file, void *buf, long count)
+{
+ return -1;
+}
+
+static long file_write(vi_file *file, void *buf, long count)
+{
+ return -1;
+}
+
+static long file_seek(vi_file *file, long offs, int whence)
+{
+ return -1;
}