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();
90 static int parse_args(int argc, char **argv)
96 for(i=1; i<argc; i++) {
97 if(argv[i][0] == '-') {
98 fprintf(stderr, "invalid option: %s\n", argv[i]);
101 argv[++num_fpaths] = argv[i];
107 static int init(void)
111 if(term_init(0) == -1) {
116 if(!(vi = vi_create(&alloc))) {
119 vi_set_fileops(vi, &fops);
120 vi_set_ttyops(vi, &ttyops);
122 for(i=0; i<num_fpaths; i++) {
123 if(!vi_new_buf(vi, fpaths[i])) {
128 term_resize_func(resized);
132 static void cleanup(void)
140 static void resized(int x, int y)
142 vi_term_size(vi, x, y);
145 static vi_file *file_open(const char *path, unsigned int flags)
149 if(!(file = calloc(1, sizeof *file))) {
152 if((file->fd = open(path, flags)) == -1) {
156 return (vi_file*)file;
159 static void file_close(vi_file *vif)
161 struct file *file = vif;
173 static long file_size(vi_file *vif)
175 struct file *file = vif;
178 if(fstat(file->fd, &st) == -1) {
184 static void *file_map(vi_file *vif)
186 struct file *file = vif;
189 if((sz = file_size(file)) == -1) {
192 if((file->maddr = mmap(0, sz, PROT_READ, MAP_PRIVATE, file->fd, 0)) == (void*)-1) {
199 static void file_unmap(vi_file *vif)
201 struct file *file = vif;
203 munmap(file->maddr, file->msize);
208 static long file_read(vi_file *vif, void *buf, long count)
210 struct file *file = vif;
211 return read(file->fd, buf, count);
214 static long file_write(vi_file *vif, void *buf, long count)
216 struct file *file = vif;
217 return write(file->fd, buf, count);
220 static long file_seek(vi_file *vif, long offs, int whence)
222 struct file *file = vif;
223 return lseek(file->fd, offs, whence);
228 static void tty_clear(void *cls)
233 static void tty_clear_line(void *cls)
238 static void tty_clear_line_at(int y, void *cls)
240 term_setcursor(y, 0);
244 static void tty_setcursor(int x, int y, void *cls)
246 term_setcursor(y, x);
249 static void tty_putchar(char c, void *cls)
254 static void tty_putchar_at(int x, int y, char c, void *cls)
256 term_setcursor(y, x);
260 static void tty_scroll(int nlines, void *cls)
265 static void tty_del_back(void *cls)
270 static void tty_del_fwd(void *cls)
275 static void tty_status(char *s, void *cls)
280 static void tty_flush(void *cls)