don't crash with no buffers, and clear the screen on exit
[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 #define vi_clear()                      vi->tty.clear(vi->tty_cls)
37 #define vi_clear_line()         vi->tty.clear_line(vi->tty_cls)
38 #define vi_clear_line_at(y)     vi->tty.clear_line_at(y, vi->tty_cls)
39 #define vi_setcursor(x, y)      vi->tty.setcursor(x, y, vi->tty_cls)
40 #define vi_putchar(c)           vi->tty.putchar(c, vi->tty_cls)
41 #define vi_putchar_at(x, y, c)  v->tty.putchar_at(x, y, c, vi->tty_cls)
42 #define vi_scroll(n)            vi->tty.scroll(n, vi->tty_cls)
43 #define vi_del_back()           vi->tty.del_back(vi->tty_cls)
44 #define vi_del_fwd()            vi->tty.del_fwd(vi->tty_cls)
45 #define vi_status(s)            vi->tty.status(s, vi->tty_cls)
46 #define vi_flush()                      vi->tty.flush(vi->tty_cls)
47
48 static int remove_buf(struct visor *vi, struct vi_buffer *vb);
49 static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size);
50
51 #ifdef HAVE_LIBC
52 static const struct vi_alloc stdalloc = { malloc, free, realloc };
53 #endif
54
55 struct visor *vi_create(struct vi_alloc *mm)
56 {
57         struct visor *vi;
58
59 #ifdef HAVE_LIBC
60         if(!mm) mm = &stdalloc;
61 #else
62         if(!mm) return 0;
63 #endif
64
65         if(!(vi = mm->malloc(sizeof *vi))) {
66                 return 0;
67         }
68         memset(vi, 0, sizeof *vi);
69         vi->mm = *mm;
70
71         vi->term_width = 80;
72         vi->term_height = 24;
73
74         return vi;
75 }
76
77 void vi_destroy(struct visor *vi)
78 {
79         while(vi->buflist) {
80                 vi_delete_buf(vi, vi->buflist);
81         }
82         vi_free(vi);
83 }
84
85 void vi_set_fileops(struct visor *vi, struct vi_fileops *fop)
86 {
87         vi->fop = *fop;
88 }
89
90 void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty)
91 {
92         vi->tty = *tty;
93 }
94
95 void vi_term_size(struct visor *vi, int xsz, int ysz)
96 {
97         vi->term_width = xsz;
98         vi->term_height = ysz;
99 }
100
101 void vi_redraw(struct visor *vi)
102 {
103         int i = 0, col, cur_x = 0, cur_y = 0;
104         char c;
105         struct vi_buffer *vb;
106         struct vi_span *sp, *spans_end;
107         const char *tptr, *tend;
108         vi_addr spoffs, addr;
109
110         if(!vi->buflist) goto end;
111
112         vb = vi->buflist;
113         if(!(sp = vi_buf_find_span(vb, vb->view_start, &spoffs))) {
114                 sp = vb->spans;
115                 spoffs = 0;
116         }
117         spans_end = vb->spans + vb->num_spans;
118
119         tptr = vi_buf_span_text(vb, sp);
120         tend = tptr + sp->size;
121         tptr += spoffs;
122
123         vi_clear();
124
125         addr = vb->view_start;
126         for(i=0; i<vi->term_height; i++) {
127                 vi_setcursor(0, i);
128                 col = -vb->view_xscroll - 1;
129                 while(++col < vi->term_width && (c = (addr++, *tptr++)) != '\n') {
130                         if(addr == vb->cursor) {
131                                 cur_x = col;
132                                 cur_y = i;
133                         }
134
135                         if(col >= 0) {
136                                 vi_putchar(c);
137                         }
138                         if(tptr >= tend) {
139                                 if(++sp >= spans_end) {
140                                         goto end;
141                                 }
142                                 tptr = vi_buf_span_text(vb, sp);
143                                 tend = tptr + sp->size;
144                         }
145                 }
146         }
147 end:
148
149         while(i < vi->term_height) {
150                 vi_setcursor(0, i++);
151                 vi_putchar('~');
152         }
153
154         vi_setcursor(cur_x, cur_y);
155         vi_flush();
156 }
157
158 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path)
159 {
160         struct vi_buffer *nb;
161
162         if(!(nb = vi_malloc(sizeof *nb))) {
163                 vi_error(vi, "failed to allocate new buffer\n");
164                 return 0;
165         }
166         memset(nb, 0, sizeof *nb);
167         nb->vi = vi;
168
169         if(path) {
170                 if(vi_buf_read(nb, path) == -1) {
171                         vi_free(nb);
172                         return 0;
173                 }
174         }
175
176         if(vi->buflist) {
177                 struct vi_buffer *last = vi->buflist->prev;
178                 nb->prev = last;
179                 nb->next = vi->buflist;
180                 last->next = nb;
181                 vi->buflist->prev = nb;
182         } else {
183                 nb->next = nb->prev = nb;
184                 vi->buflist = nb;
185         }
186         return nb;
187 }
188
189 static int remove_buf(struct visor *vi, struct vi_buffer *vb)
190 {
191         if(!vi->buflist) {
192                 vi_error(vi, "failed to remove a buffer which doesn't exist\n");
193                 return -1;
194         }
195
196         if(vb->next == vb) {
197                 if(vi->buflist != vb) {
198                         vi_error(vi, "failed to remove buffer, buffer list inconsistency\n");
199                         return -1;
200                 }
201                 vi->buflist = 0;
202                 return 0;
203         }
204
205         if(vi->buflist == vb) {
206                 vi->buflist = vb->next;
207         }
208         vb->prev->next = vb->next;
209         vb->next->prev = vb->prev;
210         vb->next = vb->prev = vb;
211         return 0;
212 }
213
214 int vi_delete_buf(struct visor *vi, struct vi_buffer *vb)
215 {
216         if(remove_buf(vi, vb) == -1) {
217                 return -1;
218         }
219
220         if(vb->fp) {
221                 if(vb->file_mapped) {
222                         vi_unmap(vb->fp);
223                 } else {
224                         vi_free(vb->orig);
225                 }
226                 vi_close(vb->fp);
227         }
228
229         vi_free(vb->path);
230         vi_free(vb->add);
231         vi_free(vb->spans);
232         return 0;
233 }
234
235 int vi_num_buf(struct visor *vi)
236 {
237         int count;
238         struct vi_buffer *vb;
239
240         if(!vi->buflist) return 0;
241
242         count = 1;
243         vb = vi->buflist->next;
244         while(vb != vi->buflist) {
245                 count++;
246                 vb = vb->next;
247         }
248         return count;
249 }
250
251 struct vi_buffer *vi_getcur_buf(struct visor *vi)
252 {
253         return vi->buflist;
254 }
255
256 void vi_setcur_buf(struct visor *vi, struct vi_buffer *vb)
257 {
258         vi->buflist = vb;
259 }
260
261 struct vi_buffer *vi_next_buf(struct visor *vi)
262 {
263         return vi->buflist ? vi->buflist->next : 0;
264 }
265
266 struct vi_buffer *vi_prev_buf(struct visor *vi)
267 {
268         return vi->buflist ? vi->buflist->prev : 0;
269 }
270
271 /* split_span splits the span sp. if size > 0 it moves the second part to sp+2,
272  * leaving an empty place at sp+1 for the new span. The start point of the
273  * second part is adjusted by size.
274  *
275  * It can't fail, because it's always called with the span array having at
276  * least two empty slots (see: add_span).
277  */
278 void split_span(struct vi_buffer *vb, struct vi_span *sp, vi_addr spoffs, unsigned long size)
279 {
280         int newseg = size > 0 ? 1 : 0;
281         struct vi_span *tail = sp + newseg + 1;
282         int num_move = vb->spans + vb->num_spans - sp - 1;
283
284         memmove(tail + 1, sp + 1, num_move * sizeof *sp);
285         vb->num_spans += tail - sp;
286
287         *tail = *sp;
288         sp->size = spoffs;
289         tail->start += spoffs;
290         tail->size -= spoffs;
291
292         sp = tail;
293         for(;;) {
294                 if(size <= tail->size) {
295                         tail->size -= size;
296                         break;
297                 }
298                 size -= tail->size;
299                 tail->size = 0;
300                 tail++;
301         }
302
303         if(tail > sp) {
304                 /* we produced one or more zero-sized spans, drop them */
305                 num_move = vb->num_spans - (tail - sp);
306                 memmove(sp, tail, num_move * sizeof *sp);
307                 vb->num_spans -= num_move;
308         }
309 }
310
311 static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size)
312 {
313         struct visor *vi = vb->vi;
314         struct vi_span *sp;
315         vi_addr spoffs;
316
317         /* make sure we have space for at least two new spans (split + add) */
318         if(vb->num_spans + 1 >= vb->max_spans) {
319                 int newmax = vb->max_spans > 0 ? (vb->max_spans << 1) : 16;
320                 struct vi_span *tmp = vi_realloc(vb->spans, newmax * sizeof *tmp);
321                 if(!tmp) return -1;
322                 vb->spans = tmp;
323                 vb->max_spans = newmax;
324         }
325
326         if((sp = vi_buf_find_span(vb, at, &spoffs))) {
327                 if(spoffs > 0) {
328                         split_span(vb, sp++, spoffs, 1);
329                 } else {
330                         split_span(vb, sp++, 0, 0);
331                 }
332         } else {
333                 sp = vb->spans + vb->num_spans;
334         }
335
336         sp->src = src;
337         sp->start = start;
338         sp->size = size;
339         vb->num_spans++;
340         return 0;
341 }
342
343 void vi_buf_reset(struct vi_buffer *vb)
344 {
345         struct visor *vi = vb->vi;
346         struct vi_buffer *prev, *next;
347
348         vi_free(vb->path);
349
350         if(vb->fp) {
351                 if(vb->file_mapped) vi_unmap(vb->fp);
352                 vi_close(vb->fp);
353         }
354         vi_free(vb->orig);
355         vi_free(vb->add);
356         vi_free(vb->spans);
357
358         prev = vb->prev;
359         next = vb->next;
360         memset(vb, 0, sizeof *vb);
361         vb->prev = prev;
362         vb->next = next;
363         vb->vi = vi;
364 }
365
366 int vi_buf_read(struct vi_buffer *vb, const char *path)
367 {
368         struct visor *vi = vb->vi;
369         vi_file *fp;
370         unsigned long fsz;
371         int plen;
372
373         vi_buf_reset(vb);
374
375         if(!(fp = vi_open(path, VI_RDONLY | VI_CREAT))) {
376                 return -1;
377         }
378         plen = strlen(path);
379         if(!(vb->path = vi_malloc(plen + 1))) {
380                 vi_error(vi, "failed to allocate path name buffer\n");
381                 vi_buf_reset(vb);
382                 return -1;
383         }
384         memcpy(vb->path, path, plen + 1);
385
386         vb->num_spans = 0;
387
388         if((fsz = vi_size(fp))) {
389                 /* existing file, map it into memory, or failing that read it */
390                 if(!vi->fop.map || !(vb->orig = vi_map(fp))) {
391                         if(!(vb->orig = vi_malloc(fsz))) {
392                                 vi_buf_reset(vb);
393                                 return -1;
394                         }
395                 } else {
396                         vb->file_mapped = 1;
397                 }
398
399                 if(add_span(vb, 0, SPAN_ORIG, 0, fsz) == -1) {
400                         vi_error(vi, "failed to allocate span\n");
401                         vi_buf_reset(vb);
402                         return -1;
403                 }
404         }
405         vb->orig_size = fsz;
406         return 0;
407 }
408
409 int vi_buf_write(struct vi_buffer *vb, const char *path)
410 {
411         int i, wbuf_count;
412         struct visor *vi = vb->vi;
413         vi_file *fp;
414         static char wbuf[512];
415
416         if(!path) path = vb->path;
417         if(!path) {
418                 vi_error(vi, "failed to write buffer, unknown path\n");
419                 return -1;
420         }
421
422         if(!(fp = vi_open(path, VI_WRONLY | VI_CREAT))) {
423                 vi_error(vi, "failed to open %s for writing\n", path);
424                 return -1;
425         }
426
427         wbuf_count = 0;
428         for(i=0; i<vb->num_spans; i++) {
429                 struct vi_span *sp = vb->spans + i;
430                 const char *sptxt = vi_buf_span_text(vb, sp);
431                 int n, count = 0;
432                 while(count < sp->size) {
433                         n = sp->size - count;
434                         if(n > sizeof wbuf - wbuf_count) {
435                                 n = sizeof wbuf - wbuf_count;
436                         }
437                         memcpy(wbuf + wbuf_count, sptxt + count, n);
438                         count += n;
439                         wbuf_count += n;
440                 }
441
442                 if(wbuf_count >= sizeof wbuf) {
443                         vi_write(fp, wbuf, wbuf_count);
444                 }
445         }
446
447         if(wbuf_count > 0) {
448                 vi_write(fp, wbuf, wbuf_count);
449         }
450         vi_close(fp);
451         return 0;
452 }
453
454 long vi_buf_size(struct vi_buffer *vb)
455 {
456         int i;
457         long sz = 0;
458
459         for(i=0; i<vb->num_spans; i++) {
460                 sz += vb->spans[i].size;
461         }
462         return sz;
463 }
464
465 struct vi_span *vi_buf_find_span(struct vi_buffer *vb, vi_addr at, vi_addr *soffs)
466 {
467         int i;
468         long sz = 0, prev_sz;
469
470         for(i=0; i<vb->num_spans; i++) {
471                 prev_sz = sz;
472                 sz += vb->spans[i].size;
473                 if(sz > at) {
474                         if(soffs) *soffs = at - prev_sz;
475                         return vb->spans + i;
476                 }
477         }
478         return 0;
479 }
480
481 const char *vi_buf_span_text(struct vi_buffer *vb, struct vi_span *sp)
482 {
483         const char *buf = sp->src == SPAN_ORIG ? vb->orig : vb->add;
484         return buf + sp->start;
485 }