16 static int parse_args(int argc, char **argv);
17 static int init(void);
18 static void cleanup(void);
19 static void resized(int x, int y);
21 static vi_file *file_open(const char *path, unsigned int flags);
22 static void file_close(vi_file *file);
23 static long file_size(vi_file *file);
24 static void *file_map(vi_file *file);
25 static void file_unmap(vi_file *file);
26 static long file_read(vi_file *file, void *buf, long count);
27 static long file_write(vi_file *file, void *buf, long count);
28 static long file_seek(vi_file *file, long offs, int whence);
30 static struct visor *vi;
32 static int num_fpaths;
35 static struct vi_alloc alloc = {
39 static struct vi_fileops fops = {
40 file_open, file_close, file_size,
42 file_read, file_write, file_seek
46 static struct vi_ttyops ttyops = {
47 tty_clear, tty_clear_line, tty_clear_line_at,
48 tty_setcursor, tty_putchar, tty_putchar_at,
49 tty_scroll, tty_del_back, tty_del_fwd, tty_status
53 int main(int argc, char **argv)
55 if(parse_args(argc, argv) == -1) {
65 int c = term_getchar();
79 static int parse_args(int argc, char **argv)
85 for(i=1; i<argc; i++) {
86 if(argv[i][0] == '-') {
87 fprintf(stderr, "invalid option: %s\n", argv[i]);
90 argv[++num_fpaths] = argv[i];
100 if(term_init(0) == -1) {
105 if(!(vi = vi_create(&alloc))) {
108 vi_set_fileops(vi, &fops);
110 for(i=0; i<num_fpaths; i++) {
111 if(!vi_new_buf(vi, fpaths[i])) {
118 static void cleanup(void)
126 static vi_file *file_open(const char *path, unsigned int flags)
130 if(!(file = calloc(1, sizeof *file))) {
133 if((file->fd = open(path, flags)) == -1) {
137 return (vi_file*)file;
140 static void file_close(vi_file *vif)
142 struct file *file = vif;
154 static long file_size(vi_file *vif)
156 struct file *file = vif;
159 if(fstat(file->fd, &st) == -1) {
165 static void *file_map(vi_file *vif)
167 struct file *file = vif;
170 if((sz = file_size(file)) == -1) {
173 if((file->maddr = mmap(0, sz, PROT_READ, MAP_PRIVATE, file->fd, 0)) == (void*)-1) {
180 static void file_unmap(vi_file *vif)
182 struct file *file = vif;
184 munmap(file->maddr, file->msize);
189 static long file_read(vi_file *vif, void *buf, long count)
191 struct file *file = vif;
192 return read(file->fd, buf, count);
195 static long file_write(vi_file *vif, void *buf, long count)
197 struct file *file = vif;
198 return write(file->fd, buf, count);
201 static long file_seek(vi_file *vif, long offs, int whence)
203 struct file *file = vif;
204 return lseek(file->fd, offs, whence);