64302728b3d6b5c5da102ce0bf0031641f2899e3
[visor] / visor / src / main_unix.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "term.h"
4 #include "visor.h"
5
6 struct file {
7         FILE *fp;
8         void *maddr;
9         size_t msize;
10 };
11
12 static int parse_args(int argc, char **argv);
13 static int init(void);
14 static void cleanup(void);
15 /* file operations */
16 static vi_file *file_open(const char *path, unsigned int flags);
17 static void file_close(vi_file *file);
18 static long file_size(vi_file *file);
19 static void *file_map(vi_file *file);
20 static void file_unmap(vi_file *file);
21 static long file_read(vi_file *file, void *buf, long count);
22 static long file_write(vi_file *file, void *buf, long count);
23 static long file_seek(vi_file *file, long offs, int whence);
24
25 static struct visor *vi;
26
27 static int num_fpaths;
28 static char **fpaths;
29
30 static struct vi_alloc alloc = {
31         malloc, free, realloc
32 };
33
34 static struct vi_fileops fops = {
35         file_open, file_close, file_size,
36         file_map, file_unmap,
37         file_read, file_write, file_seek
38 };
39
40 /*
41 static struct vi_ttyops ttyops = {
42         tty_clear, tty_clear_line, tty_clear_line_at,
43         tty_setcursor, tty_putchar, tty_putchar_at,
44         tty_scroll, tty_del_back, tty_del_fwd, tty_status
45 };
46 */
47
48 int main(int argc, char **argv)
49 {
50         if(parse_args(argc, argv) == -1) {
51                 return 1;
52         }
53         if(init() == -1) {
54                 return 1;
55         }
56
57         for(;;) {
58                 int c = term_getchar();
59
60                 switch(c) {
61                 case 27:
62                 case 'q':
63                         goto end;
64                 }
65         }
66 end:
67
68         cleanup();
69         return 0;
70 }
71
72 static int parse_args(int argc, char **argv)
73 {
74         int i;
75
76         fpaths = argv + 1;
77         num_fpaths = 0;
78         for(i=1; i<argc; i++) {
79                 if(argv[i][0] == '-') {
80                         fprintf(stderr, "invalid option: %s\n", argv[i]);
81                         return -1;
82                 } else {
83                         argv[++num_fpaths] = argv[i];
84                 }
85         }
86         return 0;
87 }
88
89 static int init(void)
90 {
91         int i;
92
93         if(term_init(0) == -1) {
94                 return -1;
95         }
96         term_clear();
97
98         if(!(vi = vi_create(&alloc))) {
99                 return -1;
100         }
101         vi_set_fileops(vi, &fops);
102
103         for(i=0; i<num_fpaths; i++) {
104                 /* open fpaths[i] */
105         }
106         return 0;
107 }
108
109 static void cleanup(void)
110 {
111         if(vi) {
112                 vi_destroy(vi);
113         }
114         term_cleanup();
115 }
116
117 static vi_file *file_open(const char *path, unsigned int flags)
118 {
119         struct file *file;
120         const char *attr;
121
122         switch(flags & 0xff) {
123         case VI_RDONLY:
124                 attr = "rb";
125                 break;
126
127         case VI_WRONLY:
128                 attr = (flags & VI_CREAT) ? "w+b" : "wb";
129                 break;
130
131         case VI_RDWR:
132                 attr = (flags & VI_CREAT) ? "w+b" : "r+b";
133                 break;
134
135         default:
136                 return 0;
137         }
138
139
140         if(!(file = calloc(1, sizeof *file))) {
141                 return 0;
142         }
143         if(!(file->fp = fopen(path, attr))) {
144                 free(file);
145                 return 0;
146         }
147         return file;
148 }
149
150 static void file_close(vi_file *file)
151 {
152 }
153
154 static long file_size(vi_file *file)
155 {
156         return -1;
157 }
158
159 static void *file_map(vi_file *file)
160 {
161         return 0;
162 }
163
164 static void file_unmap(vi_file *file)
165 {
166 }
167
168 static long file_read(vi_file *file, void *buf, long count)
169 {
170         return -1;
171 }
172
173 static long file_write(vi_file *file, void *buf, long count)
174 {
175         return -1;
176 }
177
178 static long file_seek(vi_file *file, long offs, int whence)
179 {
180         return -1;
181 }