root readme, directory structure, and copyright headers
[visor] / libvisor / src / visor.c
1 /*
2 visor - lightweight system-independent embeddable text editor framework
3 Copyright (C)  2019 John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19 #include "vilibc.h"
20 #include "visor.h"
21 #include "vimpl.h"
22
23 #define vi_malloc       vi->mm.malloc
24 #define vi_free         vi->mm.free
25 #define vi_realloc      vi->mm.realloc
26
27 #define vi_open         vi->fop.open
28 #define vi_size         vi->fop.size
29 #define vi_close        vi->fop.close
30 #define vi_map          vi->fop.map
31 #define vi_unmap        vi->fop.unmap
32 #define vi_read         vi->fop.read
33 #define vi_write        vi->fop.write
34 #define vi_seek         vi->fop.seek
35
36 #ifdef HAVE_LIBC
37 static const struct vi_alloc stdalloc = { malloc, free, realloc };
38 #endif
39
40 struct visor *vi_create(struct vi_alloc *mm)
41 {
42         struct visor *vi;
43
44 #ifdef HAVE_LIBC
45         if(!mm) mm = &stdalloc;
46 #else
47         if(!mm) return 0;
48 #endif
49
50         if(!(vi = mm->malloc(sizeof *vi))) {
51                 return 0;
52         }
53         memset(vi, 0, sizeof *vi);
54         vi->mm = *mm;
55
56         return vi;
57 }
58
59 void vi_destroy(struct visor *vi)
60 {
61         while(vi->buflist) {
62                 vi_delete_buf(vi, vi->buflist);
63         }
64         vi_free(vi);
65 }
66
67 void vi_set_fileops(struct visor *vi, struct vi_fileops *fop)
68 {
69         vi->fop = *fop;
70 }
71
72 void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty)
73 {
74         vi->tty = *tty;
75 }
76
77 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path)
78 {
79         struct vi_buffer *nb;
80
81         if(!(nb = vi_malloc(sizeof *nb))) {
82                 vi_error(vi, "failed to allocate new buffer\n");
83                 return 0;
84         }
85         memset(nb, 0, sizeof *nb);
86         nb->vi = vi;
87
88         if(path) {
89                 if(vi_buf_read(nb, path) == -1) {
90                         vi_free(nb);
91                         return 0;
92                 }
93         }
94
95         if(vi->buflist) {
96                 struct vi_buffer *last = vi->buflist->prev;
97                 nb->prev = last;
98                 nb->next = vi->buflist;
99                 last->next = nb;
100                 vi->buflist->prev = nb;
101         } else {
102                 nb->next = nb->prev = nb;
103                 vi->buflist = nb;
104         }
105         return nb;
106 }
107
108 static int remove_buf(struct visor *vi, struct vi_buffer *vb)
109 {
110         if(!vi->buflist) {
111                 vi_error(vi, "failed to remove a buffer which doesn't exist\n");
112                 return -1;
113         }
114
115         if(vb->next == vb) {
116                 if(vi->buflist != vb) {
117                         vi_error(vi, "failed to remove buffer, buffer list inconsistency\n");
118                         return -1;
119                 }
120                 vi->buflist = 0;
121                 return 0;
122         }
123
124         if(vi->buflist == vb) {
125                 vi->buflist = vb->next;
126         }
127         vb->prev->next = vb->next;
128         vb->next->prev = vb->prev;
129         vb->next = vb->prev = vb;
130         return 0;
131 }
132
133 int vi_delete_buf(struct visor *vi, struct vi_buffer *vb)
134 {
135         if(remove_buf(vi, vb) == -1) {
136                 return -1;
137         }
138
139         vi_free(vb->path);
140         vi_free(vb->orig);
141         vi_free(vb->add);
142         vi_free(vb->spans);
143         return 0;
144 }
145
146 int vi_num_buf(struct visor *vi)
147 {
148         int count;
149         struct vi_buffer *vb;
150
151         if(!vi->buflist) return 0;
152
153         count = 1;
154         vb = vi->buflist->next;
155         while(vb != vi->buflist) {
156                 count++;
157                 vb = vb->next;
158         }
159         return count;
160 }
161
162 struct vi_buffer *vi_getcur_buf(struct visor *vi)
163 {
164         return vi->buflist;
165 }
166
167 void vi_setcur_buf(struct visor *vi, struct vi_buffer *vb)
168 {
169         vi->buflist = vb;
170 }
171
172 struct vi_buffer *vi_next_buf(struct visor *vi)
173 {
174         return vi->buflist ? vi->buflist->next : 0;
175 }
176
177 struct vi_buffer *vi_prev_buf(struct visor *vi)
178 {
179         return vi->buflist ? vi->buflist->prev : 0;
180 }
181
182 static int add_span(struct vi_buffer *vb, int src, vi_addr start, unsigned long size)
183 {
184         struct visor *vi = vb->vi;
185         struct vi_span *sp;
186
187         if(vb->num_spans >= vb->max_spans) {
188                 int newmax = vb->max_spans > 0 ? (vb->max_spans << 1) : 16;
189                 struct vi_span *tmp = vi_realloc(vb->spans, newmax * sizeof *tmp);
190                 if(!tmp) return -1;
191                 vb->spans = tmp;
192                 vb->max_spans = newmax;
193         }
194
195         sp = vb->spans + vb->num_spans++;
196         sp->beg = start;
197         sp->size = size;
198         return 0;
199 }
200
201 void vi_buf_reset(struct vi_buffer *vb)
202 {
203         struct visor *vi = vb->vi;
204         struct vi_buffer *prev, *next;
205
206         vi_free(vb->path);
207
208         if(vb->fp) {
209                 if(vb->file_mapped) vi_unmap(vb->fp);
210                 vi_close(vb->fp);
211         }
212         vi_free(vb->orig);
213         vi_free(vb->add);
214         vi_free(vb->spans);
215
216         prev = vb->prev;
217         next = vb->next;
218         memset(vb, 0, sizeof *vb);
219         vb->prev = prev;
220         vb->next = next;
221 }
222
223 int vi_buf_read(struct vi_buffer *vb, const char *path)
224 {
225         struct visor *vi = vb->vi;
226         vi_file *fp;
227         unsigned long fsz;
228         int plen;
229
230         vi_buf_reset(vb);
231
232         if(!(fp = vi_open(path))) {
233                 return -1;
234         }
235         plen = strlen(path);
236         if(!(vb->path = vi_malloc(plen + 1))) {
237                 vi_error(vi, "failed to allocate path name buffer\n");
238                 vi_buf_reset(vb);
239                 return -1;
240         }
241         memcpy(vb->path, path, plen + 1);
242
243         vb->num_spans = 0;
244
245         if((fsz = vi_size(fp))) {
246                 /* existing file, map it into memory, or failing that read it */
247                 if(!vi->fop.map || !(vb->orig = vi_map(fp))) {
248                         if(!(vb->orig = vi_malloc(fsz))) {
249                                 vi_buf_reset(vb);
250                                 return -1;
251                         }
252                 } else {
253                         vb->file_mapped = 1;
254                 }
255
256                 if(add_span(vb, SPAN_ORIG, 0, fsz) == -1) {
257                         vi_error(vi, "failed to allocate span\n");
258                         vi_buf_reset(vb);
259                         return -1;
260                 }
261         }
262         vb->orig_size = fsz;
263         return 0;
264 }