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 void tty_clear(void *cls);
31 static void tty_clear_line(void *cls);
32 static void tty_clear_line_at(int y, void *cls);
33 static void tty_setcursor(int x, int y, void *cls);
34 static void tty_putchar(char c, void *cls);
35 static void tty_putchar_at(int x, int y, char c, void *cls);
36 static void tty_scroll(int nlines, void *cls);
37 static void tty_del_back(void *cls);
38 static void tty_del_fwd(void *cls);
39 static void tty_status(char *s, void *cls);
40 static void tty_flush(void *cls);
43 static struct visor *vi;
45 static int num_fpaths;
48 static struct vi_alloc alloc = {
52 static struct vi_fileops fops = {
53 file_open, file_close, file_size,
55 file_read, file_write, file_seek
58 static struct vi_ttyops ttyops = {
59 tty_clear, tty_clear_line, tty_clear_line_at,
60 tty_setcursor, tty_putchar, tty_putchar_at,
61 tty_scroll, tty_del_back, tty_del_fwd, tty_status, tty_flush
64 int main(int argc, char **argv)
66 if(parse_args(argc, argv) == -1) {
76 int c = term_getchar();
91 static int parse_args(int argc, char **argv)
97 for(i=1; i<argc; i++) {
98 if(argv[i][0] == '-') {
99 fprintf(stderr, "invalid option: %s\n", argv[i]);
102 argv[++num_fpaths] = argv[i];
108 static int init(void)
112 if(term_init(0) == -1) {
117 if(!(vi = vi_create(&alloc))) {
120 vi_set_fileops(vi, &fops);
121 vi_set_ttyops(vi, &ttyops);
123 for(i=0; i<num_fpaths; i++) {
124 if(!vi_new_buf(vi, fpaths[i])) {
129 term_resize_func(resized);
133 static void cleanup(void)
141 static void resized(int x, int y)
143 vi_term_size(vi, x, y);
146 static vi_file *file_open(const char *path, unsigned int flags)
150 if(!(file = calloc(1, sizeof *file))) {
153 if((file->fd = open(path, flags)) == -1) {
157 return (vi_file*)file;
160 static void file_close(vi_file *vif)
162 struct file *file = vif;
174 static long file_size(vi_file *vif)
176 struct file *file = vif;
179 if(fstat(file->fd, &st) == -1) {
185 static void *file_map(vi_file *vif)
187 struct file *file = vif;
190 if((sz = file_size(file)) == -1) {
193 if((file->maddr = mmap(0, sz, PROT_READ, MAP_PRIVATE, file->fd, 0)) == (void*)-1) {
200 static void file_unmap(vi_file *vif)
202 struct file *file = vif;
204 munmap(file->maddr, file->msize);
209 static long file_read(vi_file *vif, void *buf, long count)
211 struct file *file = vif;
212 return read(file->fd, buf, count);
215 static long file_write(vi_file *vif, void *buf, long count)
217 struct file *file = vif;
218 return write(file->fd, buf, count);
221 static long file_seek(vi_file *vif, long offs, int whence)
223 struct file *file = vif;
224 return lseek(file->fd, offs, whence);
229 static void tty_clear(void *cls)
234 static void tty_clear_line(void *cls)
239 static void tty_clear_line_at(int y, void *cls)
241 term_setcursor(y, 0);
245 static void tty_setcursor(int x, int y, void *cls)
247 term_setcursor(y, x);
250 static void tty_putchar(char c, void *cls)
255 static void tty_putchar_at(int x, int y, char c, void *cls)
257 term_setcursor(y, x);
261 static void tty_scroll(int nlines, void *cls)
266 static void tty_del_back(void *cls)
271 static void tty_del_fwd(void *cls)
276 static void tty_status(char *s, void *cls)
281 static void tty_flush(void *cls)