16 static int parse_args(int argc, char **argv);
17 static int init(void);
18 static void cleanup(void);
20 static vi_file *file_open(const char *path, unsigned int flags);
21 static void file_close(vi_file *file);
22 static long file_size(vi_file *file);
23 static void *file_map(vi_file *file);
24 static void file_unmap(vi_file *file);
25 static long file_read(vi_file *file, void *buf, long count);
26 static long file_write(vi_file *file, void *buf, long count);
27 static long file_seek(vi_file *file, long offs, int whence);
29 static struct visor *vi;
31 static int num_fpaths;
34 static struct vi_alloc alloc = {
38 static struct vi_fileops fops = {
39 file_open, file_close, file_size,
41 file_read, file_write, file_seek
45 static struct vi_ttyops ttyops = {
46 tty_clear, tty_clear_line, tty_clear_line_at,
47 tty_setcursor, tty_putchar, tty_putchar_at,
48 tty_scroll, tty_del_back, tty_del_fwd, tty_status
52 int main(int argc, char **argv)
54 if(parse_args(argc, argv) == -1) {
62 int c = term_getchar();
76 static int parse_args(int argc, char **argv)
82 for(i=1; i<argc; i++) {
83 if(argv[i][0] == '-') {
84 fprintf(stderr, "invalid option: %s\n", argv[i]);
87 argv[++num_fpaths] = argv[i];
97 if(term_init(0) == -1) {
102 if(!(vi = vi_create(&alloc))) {
105 vi_set_fileops(vi, &fops);
107 for(i=0; i<num_fpaths; i++) {
108 if(!vi_new_buf(vi, fpaths[i])) {
111 printf("loaded file: %s\n", fpaths[i]);
116 static void cleanup(void)
124 static vi_file *file_open(const char *path, unsigned int flags)
128 if(!(file = calloc(1, sizeof *file))) {
131 if((file->fd = open(path, flags)) == -1) {
135 return (vi_file*)file;
138 static void file_close(vi_file *vif)
140 struct file *file = vif;
152 static long file_size(vi_file *vif)
154 struct file *file = vif;
157 if(fstat(file->fd, &st) == -1) {
163 static void *file_map(vi_file *vif)
165 struct file *file = vif;
168 if((sz = file_size(file)) == -1) {
171 if((file->maddr = mmap(0, sz, PROT_READ, MAP_PRIVATE, file->fd, 0)) == (void*)-1) {
178 static void file_unmap(vi_file *vif)
180 struct file *file = vif;
182 munmap(file->maddr, file->msize);
187 static long file_read(vi_file *vif, void *buf, long count)
189 struct file *file = vif;
190 return read(file->fd, buf, count);
193 static long file_write(vi_file *vif, void *buf, long count)
195 struct file *file = vif;
196 return write(file->fd, buf, count);
199 static long file_seek(vi_file *vif, long offs, int whence)
201 struct file *file = vif;
202 return lseek(file->fd, offs, whence);