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 static int add_span(struct vi_buffer *vb, int src, vi_addr start, unsigned long size)
184 struct visor *vi = vb->vi;
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);
192 vb->max_spans = newmax;
195 sp = vb->spans + vb->num_spans++;
201 void vi_buf_reset(struct vi_buffer *vb)
203 struct visor *vi = vb->vi;
204 struct vi_buffer *prev, *next;
209 if(vb->file_mapped) vi_unmap(vb->fp);
218 memset(vb, 0, sizeof *vb);
223 int vi_buf_read(struct vi_buffer *vb, const char *path)
225 struct visor *vi = vb->vi;
232 if(!(fp = vi_open(path))) {
236 if(!(vb->path = vi_malloc(plen + 1))) {
237 vi_error(vi, "failed to allocate path name buffer\n");
241 memcpy(vb->path, path, plen + 1);
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))) {
256 if(add_span(vb, SPAN_ORIG, 0, fsz) == -1) {
257 vi_error(vi, "failed to allocate span\n");