2 visor - lightweight system-independent embeddable text editor framework
3 Copyright (C) 2019 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
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/>.
23 #define vi_malloc vi->mm.malloc
24 #define vi_free vi->mm.free
25 #define vi_realloc vi->mm.realloc
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
37 static const struct vi_alloc stdalloc = { malloc, free, realloc };
40 struct visor *vi_create(struct vi_alloc *mm)
45 if(!mm) mm = &stdalloc;
50 if(!(vi = mm->malloc(sizeof *vi))) {
53 memset(vi, 0, sizeof *vi);
59 void vi_destroy(struct visor *vi)
62 vi_delete_buf(vi, vi->buflist);
67 void vi_set_fileops(struct visor *vi, struct vi_fileops *fop)
72 void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty)
77 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path)
81 if(!(nb = vi_malloc(sizeof *nb))) {
82 vi_error(vi, "failed to allocate new buffer\n");
85 memset(nb, 0, sizeof *nb);
89 if(vi_buf_read(nb, path) == -1) {
96 struct vi_buffer *last = vi->buflist->prev;
98 nb->next = vi->buflist;
100 vi->buflist->prev = nb;
102 nb->next = nb->prev = nb;
108 static int remove_buf(struct visor *vi, struct vi_buffer *vb)
111 vi_error(vi, "failed to remove a buffer which doesn't exist\n");
116 if(vi->buflist != vb) {
117 vi_error(vi, "failed to remove buffer, buffer list inconsistency\n");
124 if(vi->buflist == vb) {
125 vi->buflist = vb->next;
127 vb->prev->next = vb->next;
128 vb->next->prev = vb->prev;
129 vb->next = vb->prev = vb;
133 int vi_delete_buf(struct visor *vi, struct vi_buffer *vb)
135 if(remove_buf(vi, vb) == -1) {
146 int vi_num_buf(struct visor *vi)
149 struct vi_buffer *vb;
151 if(!vi->buflist) return 0;
154 vb = vi->buflist->next;
155 while(vb != vi->buflist) {
162 struct vi_buffer *vi_getcur_buf(struct visor *vi)
167 void vi_setcur_buf(struct visor *vi, struct vi_buffer *vb)
172 struct vi_buffer *vi_next_buf(struct visor *vi)
174 return vi->buflist ? vi->buflist->next : 0;
177 struct vi_buffer *vi_prev_buf(struct visor *vi)
179 return vi->buflist ? vi->buflist->prev : 0;
182 /* split_span splits the span sp. if size > 0 it moves the second part to sp+2,
183 * leaving an empty place at sp+1 for the new span. The start point of the
184 * second part is adjusted by size.
186 * It can't fail, because it's always called with the span array having at
187 * least two empty slots (see: add_span).
189 void split_span(struct vi_buffer *vb, struct vi_span *sp, vi_addr spoffs, unsigned long size)
191 int newseg = size > 0 ? 1 : 0;
192 struct vi_span *tail = sp + newseg + 1;
193 int num_move = vb->spans + vb->num_spans - sp - 1;
195 memmove(tail + 1, sp + 1, num_move * sizeof *sp);
196 vb->num_spans += tail - sp;
200 tail->start += spoffs;
201 tail->size -= spoffs;
205 if(size <= tail->size) {
215 /* we produced one or more zero-sized spans, drop them */
216 num_move = vb->num_spans - (tail - sp);
217 memmove(sp, tail, num_move * sizeof *sp);
218 vb->num_spans -= num_move;
222 static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size)
224 struct visor *vi = vb->vi;
228 /* make sure we have space for at least two new spans (split + add) */
229 if(vb->num_spans + 1 >= vb->max_spans) {
230 int newmax = vb->max_spans > 0 ? (vb->max_spans << 1) : 16;
231 struct vi_span *tmp = vi_realloc(vb->spans, newmax * sizeof *tmp);
234 vb->max_spans = newmax;
237 if((sp = vi_buf_find_span(vb, at, &spoffs))) {
239 split_span(vb, sp++, spoffs, 1);
241 split_span(vb, sp++, 0, 0);
244 sp = vb->spans + vb->num_spans;
254 void vi_buf_reset(struct vi_buffer *vb)
256 struct visor *vi = vb->vi;
257 struct vi_buffer *prev, *next;
262 if(vb->file_mapped) vi_unmap(vb->fp);
271 memset(vb, 0, sizeof *vb);
277 int vi_buf_read(struct vi_buffer *vb, const char *path)
279 struct visor *vi = vb->vi;
286 if(!(fp = vi_open(path, VI_RDONLY | VI_CREAT))) {
290 if(!(vb->path = vi_malloc(plen + 1))) {
291 vi_error(vi, "failed to allocate path name buffer\n");
295 memcpy(vb->path, path, plen + 1);
299 if((fsz = vi_size(fp))) {
300 /* existing file, map it into memory, or failing that read it */
301 if(!vi->fop.map || !(vb->orig = vi_map(fp))) {
302 if(!(vb->orig = vi_malloc(fsz))) {
310 if(add_span(vb, 0, SPAN_ORIG, 0, fsz) == -1) {
311 vi_error(vi, "failed to allocate span\n");
320 int vi_buf_write(struct vi_buffer *vb, const char *path)
323 struct visor *vi = vb->vi;
325 static char wbuf[512];
327 if(!path) path = vb->path;
329 vi_error(vi, "failed to write buffer, unknown path\n");
333 if(!(fp = vi_open(path, VI_WRONLY | VI_CREAT))) {
334 vi_error(vi, "failed to open %s for writing\n", path);
339 for(i=0; i<vb->num_spans; i++) {
340 struct vi_span *sp = vb->spans + i;
341 const char *sptxt = vi_buf_span_text(vb, sp);
343 while(count < sp->size) {
344 n = sp->size - count;
345 if(n > sizeof wbuf - wbuf_count) {
346 n = sizeof wbuf - wbuf_count;
348 memcpy(wbuf + wbuf_count, sptxt + count, n);
353 if(wbuf_count >= sizeof wbuf) {
354 vi_write(fp, wbuf, wbuf_count);
359 vi_write(fp, wbuf, wbuf_count);
365 long vi_buf_size(struct vi_buffer *vb)
370 for(i=0; i<vb->num_spans; i++) {
371 sz += vb->spans[i].size;
376 struct vi_span *vi_buf_find_span(struct vi_buffer *vb, vi_addr at, vi_addr *soffs)
379 long sz = 0, prev_sz;
381 for(i=0; i<vb->num_spans; i++) {
383 sz += vb->spans[i].size;
385 if(soffs) *soffs = at - prev_sz;
386 return vb->spans + i;
392 const char *vi_buf_span_text(struct vi_buffer *vb, struct vi_span *sp)
394 const char *buf = sp->src == SPAN_ORIG ? vb->orig : vb->add;
395 return buf + sp->start;