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 at, unsigned long size)
191 struct vi_span *tail = sp + (size ? 2 : 1);
192 int num_move = vb->spans + vb->num_spans - sp - 1;
194 memmove(tail + 1, sp + 1, num_move);
195 vb->num_spans += tail - sp;
199 static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size)
201 struct visor *vi = vb->vi;
204 /* make sure we have space for at least two new spans (split + add) */
205 if(vb->num_spans + 1 >= vb->max_spans) {
206 int newmax = vb->max_spans > 0 ? (vb->max_spans << 1) : 16;
207 struct vi_span *tmp = vi_realloc(vb->spans, newmax * sizeof *tmp);
210 vb->max_spans = newmax;
213 if((sp = vi_buf_find_span(vb, at))) {
214 split_span(vb, sp++, at, size);
216 sp = vb->spans + vb->num_spans;
226 void vi_buf_reset(struct vi_buffer *vb)
228 struct visor *vi = vb->vi;
229 struct vi_buffer *prev, *next;
234 if(vb->file_mapped) vi_unmap(vb->fp);
243 memset(vb, 0, sizeof *vb);
248 int vi_buf_read(struct vi_buffer *vb, const char *path)
250 struct visor *vi = vb->vi;
257 if(!(fp = vi_open(path, VI_RDONLY | VI_CREAT))) {
261 if(!(vb->path = vi_malloc(plen + 1))) {
262 vi_error(vi, "failed to allocate path name buffer\n");
266 memcpy(vb->path, path, plen + 1);
270 if((fsz = vi_size(fp))) {
271 /* existing file, map it into memory, or failing that read it */
272 if(!vi->fop.map || !(vb->orig = vi_map(fp))) {
273 if(!(vb->orig = vi_malloc(fsz))) {
281 if(add_span(vb, 0, SPAN_ORIG, 0, fsz) == -1) {
282 vi_error(vi, "failed to allocate span\n");
291 int vi_buf_write(struct vi_buffer *vb, const char *path)
294 struct visor *vi = vb->vi;
296 static char wbuf[512];
298 if(!path) path = vb->path;
300 vi_error(vi, "failed to write buffer, unknown path\n");
304 if(!(fp = vi_open(path, VI_WRONLY | VI_CREAT))) {
305 vi_error(vi, "failed to open %s for writing\n", path);
310 for(i=0; i<vb->num_spans; i++) {
311 struct vi_span *sp = vb->spans + i;
312 const char *sptxt = vi_buf_span_text(vb, sp);
314 while(count < sp->size) {
315 n = sp->size - count;
316 if(n > sizeof wbuf - wbuf_count) {
317 n = sizeof wbuf - wbuf_count;
319 memcpy(wbuf + wbuf_count, sptxt + count, n);
324 if(wbuf_count >= sizeof wbuf) {
325 vi_write(fp, wbuf, wbuf_count);
330 vi_write(fp, wbuf, wbuf_count);
336 long vi_buf_size(struct vi_buffer *vb)
341 for(i=0; i<vb->num_spans; i++) {
342 sz += vb->spans[i].size;
347 struct vi_span *vi_buf_find_span(struct vi_buffer *vb, vi_addr at)
352 for(i=0; i<vb->num_spans; i++) {
353 sz += vb->spans[i].size;
355 return vb->spans + i;
361 const char *vi_buf_span_text(struct vi_buffer *vb, struct vi_span *sp)
363 const char *buf = sp->src == SPAN_ORIG ? vb->orig : vb->add;
364 return buf + sp->start;