-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-editor.c
722 lines (657 loc) · 15.3 KB
/
core-editor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
* Copyright Neil Brown ©2015-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <stdarg.h>
#include <dlfcn.h>
#define PANE_DATA_TYPE struct ed_info
#include "core.h"
#include "misc.h"
#include "internal.h"
#define ED_MAGIC 0x4321fedcUL
static struct map *ed_map safe;
struct ed_info {
unsigned long magic;
struct pane *freelist;
struct mark *mark_free_list;
struct map *map safe;
struct lookup_cmd cmd;
/* These two paths contain nul-terminated strings,
* with a double-nul at the end.
*/
char *data_path;
char *config_path;
char *bin_path;
char *here;
bool testing;
struct store {
struct store *next;
int size;
char space[];
} *store;
};
#include "core-pane.h"
bool edlib_testing(struct pane *p safe)
{
struct ed_info *ei = pane_root(p)->data;
return ei->testing;
}
DEF_LOOKUP_CMD(ed_handle, ed_map);
DEF_CMD(global_set_attr)
{
char *v;
if (!ci->str)
return Enoarg;
if (!ci->num) {
attr_set_str(&ci->home->attrs, ci->str, ci->str2);
return 1;
}
/* Append */
if (!ci->str2)
return 1;
v = attr_find(ci->home->attrs, ci->str);
if (!v) {
attr_set_str(&ci->home->attrs, ci->str, ci->str2);
return 1;
}
v = strconcat(ci->home, v, ci->str2);
attr_set_str(&ci->home->attrs, ci->str, v);
return 1;
}
DEF_CMD(global_set_command)
{
struct ed_info *ei = ci->home->data;
struct map *map = ei->map;
bool prefix = strcmp(ci->key, "global-set-command-prefix") == 0;
if (!ci->str)
return Enoarg;
if (prefix) {
char *e = strconcat(NULL, ci->str, "\xFF\xFF\xFF\xFF");
key_add_range(map, ci->str, e, ci->comm2);
free(e);
} else if (ci->str2)
key_add_range(map, ci->str, ci->str2, ci->comm2);
else
key_add(map, ci->str, ci->comm2);
return 1;
}
DEF_CMD(global_get_command)
{
struct ed_info *ei = ci->home->data;
struct map *map = ei->map;
struct command *cm;
if (!ci->str ||
!(cm = key_lookup_cmd(map, ci->str)))
return Efail;
return comm_call(ci->comm2, "callback:comm", ci->focus,
0, NULL, ci->str,
0, NULL, NULL, 0,0, cm);
}
DEF_CMD(global_config_dir)
{
const char *var = ci->str;
char *dir; // ci->str2;
char *key, *val = NULL;
struct pane *p = ci->home;
char *end;
/* var might be different in different directories.
* Config setting are attributes stored on root that
* look like "config:var:dir".
* We find the best and return that with the dir
*/
if (!var || !ci->str2 || !ci->comm2)
return Enoarg;
key = strconcat(p, "config:", var, ":", ci->str2);
dir = key + 7 + strlen(var) + 1;
end = dir + strlen(dir);
while (!val && end > dir) {
end[0] = 0;
val = attr_find(p->attrs, key);
if (end[-1] == '/') {
while (end > dir && end[-1] == '/')
end -= 1;
} else {
while (end > dir && end[-1] != '/')
end -= 1;
}
}
if (!val)
return Efalse;
comm_call(ci->comm2, "cb", ci->focus, 0, NULL, val,
0, NULL, dir);
return 1;
}
#ifdef edlib_init
#include "O/mod-list-decl.h"
typedef void init_func(struct pane *ed);
static struct builtin {
char *name;
init_func *func;
} builtins[]={
#include "O/mod-list.h"
};
#endif
DEF_CMD(editor_load_module)
{
struct ed_info *ei = ci->home->data;
struct map *map = ei->map;
const char *name = ci->str;
char buf[PATH_MAX];
#ifndef edlib_init
void *h;
void (*s)(struct pane *p);
char **path;
char pbuf[PATH_MAX];
sprintf(buf, "edlib-%s.so", name);
/* RTLD_GLOBAL is needed for python, else we get
* errors about _Py_ZeroStruct which a python script
* tries "import gtk"
*
*/
h = dlopen(buf, RTLD_NOW | RTLD_GLOBAL);
if (h) {
path = dlsym(h, "edlib_module_path");
if (path) {
if (dlinfo(h, RTLD_DI_ORIGIN, pbuf) == 0)
*path = pbuf;
}
s = dlsym(h, "edlib_init");
if (s) {
char *v = dlsym(h, "edlib_version");
LOG("Loading %s - version %s", name, v ?: "not provided");
s(ci->home);
if (path)
*path = NULL;
return 1;
}
} else {
char *err = dlerror();
if (strstr(err, "No such file or directory") == NULL)
LOG("dlopen %s failed %s", buf, err);
}
#else
unsigned int i;
strcpy(buf, name);
for (i = 0; buf[i]; i++)
if (buf[i] == '-')
buf[i] = '_';
for (i = 0; i < sizeof(builtins)/sizeof(builtins[0]); i++)
if (strcmp(builtins[i].name, buf) == 0) {
builtins[i].func(ci->home);
return 1;
}
#endif
/* Multiple modules can support module lookup, such as
* global-load-modules:python or global-load-module:lua.
* So do a prefix lookup.
*/
if (key_lookup_prefix(map, ci, False) > 0)
return 1;
LOG("Failed to load module: %s", name);
return Efail;
}
DEF_CMD(editor_auto_event)
{
/* Event handlers register under a private name so we
* have to use key_lookup_prefix to find them.
* If nothing is found, autoload lib-libevent (hack?)
*/
struct ed_info *ei = ci->home->data;
struct map *map = ei->map;
int ret = key_lookup_prefix(map, ci, False);
if (ret)
return ret;
if (strcmp(ci->key, "event:refresh") == 0)
/* pointless to autoload for refresh */
return Efallthrough;
call("attach-libevent", ci->home);
return key_lookup_prefix(map, ci, False);
}
DEF_CMD(editor_multicall)
{
struct ed_info *ei = ci->home->data;
struct map *map = ei->map;
int ret;
const char *key = ci->key;
((struct cmd_info*)ci)->key = ksuffix(ci, "global-multicall-");
ret = key_lookup_prefix(map, ci, False);
((struct cmd_info*)ci)->key = key;
return ret;
}
DEF_CMD(editor_request_notify)
{
pane_add_notify(ci->focus, ci->home, ksuffix(ci, "editor:request:"));
return 1;
}
DEF_CMD(editor_send_notify)
{
/* editor:notify:... */
return home_pane_notify(ci->home, ksuffix(ci, "editor:notify:"),
ci->focus,
ci->num, ci->mark, ci->str,
ci->num2, ci->mark2, ci->str2, ci->comm2);
}
DEF_CMD(editor_free_panes)
{
struct ed_info *ei = ci->home->data;
while (ei->freelist) {
struct pane *p = ei->freelist;
ei->freelist = p->focus;
p->focus = NULL;
command_put(p->handle);
p->handle = NULL;
attr_free(&p->attrs);
pane_put(p);
}
return 1;
}
DEF_CMD(editor_free_marks)
{
struct ed_info *ei = ci->home->data;
while (ei->mark_free_list) {
struct mark *m = ei->mark_free_list;
ei->mark_free_list = (struct mark*)m->all.next;
do_mark_free(m);
}
return 1;
}
DEF_CMD(editor_free_store)
{
struct ed_info *ei = ci->home->data;
while (ei->store) {
struct store *s = ei->store;
ei->store = s->next;
free(s);
}
return 1;
}
/* FIXME I should be able to remove things from a keymap, not
* replace with this.
*/
DEF_EXTERN_CMD(edlib_noop)
{
return Efallthrough;
}
DEF_CMD_CLOSED(editor_close)
{
struct ed_info *ei = ci->home->data;
stat_free();
free(ei->here); ei->here = NULL;
free(ei->data_path); ei->data_path = NULL;
free(ei->config_path); ei->config_path = NULL;
return Efallthrough;
}
void * safe memsave(struct pane *p safe, const char *buf, int len)
{
struct ed_info *ei;
p = pane_root(p);
ei = p->data;
ASSERT(ei->magic==ED_MAGIC);
if (!ei->store)
call_comm("event:on-idle", p, &editor_free_store, 2);
if (ei->store == NULL || ei->store->size < len) {
struct store *s;
int l = 4096 - sizeof(*s);
while (l < len)
l += 4096;
s = malloc(l + sizeof(*s));
s->next = ei->store;
s->size = l;
ei->store = s;
}
ei->store->size -= len;
if (buf)
return memcpy(ei->store->space+ei->store->size, buf, len);
else
return ei->store->space+ei->store->size;
}
char *strsave(struct pane *p safe, const char *buf)
{
if (!buf)
return NULL;
return memsave(p, buf, strlen(buf)+1);
}
char *strnsave(struct pane *p safe, const char *buf, int len)
{
char *s;
if (!buf)
return NULL;
s = memsave(p, buf, len+1);
if (s)
s[len] = 0;
return s;
}
char * safe do_strconcat(struct pane *p, const char *s1 safe, ...)
{
va_list ap;
char *s;
int len = 0;
char *ret;
len = strlen(s1);
va_start(ap, s1);
while ((s = va_arg(ap, char*)) != NULL)
len += strlen(s);
va_end(ap);
if (p)
ret = memsave(p, NULL, len+1);
else
ret = malloc(len+1);
strcpy(ret, s1);
va_start(ap, s1);
while ((s = va_arg(ap, char*)) != NULL)
strcat(ret, s);
va_end(ap);
return ret;
}
void editor_delayed_free(struct pane *ed safe, struct pane *p safe)
{
struct ed_info *ei = ed->data;
if (!ei) {
command_put(p->handle);
p->handle = NULL;
attr_free(&p->attrs);
pane_free(p);
return;
}
ASSERT(ei->magic==ED_MAGIC);
if (!ei->freelist)
call_comm("event:on-idle", ed, &editor_free_panes, 2);
p->focus = ei->freelist;
ei->freelist = p;
}
void editor_delayed_mark_free(struct mark *m safe)
{
struct pane *ed = pane_root(m->owner);
struct ed_info *ei = ed->data;
ASSERT(ei->magic==ED_MAGIC);
if (!ei->mark_free_list)
call_comm("event:on-idle", ed, &editor_free_marks, 2);
m->all.next = (void*)ei->mark_free_list;
ei->mark_free_list = m;
}
static char *set_here(struct pane *p safe)
{
struct ed_info *ei = p->data;
Dl_info info;
if (ei->here)
;
else if (dladdr(&set_here, &info) == 0)
ei->here = strdup("");
else {
char *sl;
ei->here = strdup(info.dli_fname ?: "");
sl = strrchr(ei->here, '/');
if (sl)
*sl = 0;
}
return ei->here;
}
static char *set_data_path(struct pane *p safe)
{
struct ed_info *ei = p->data;
char *dh, *dd, *here;
struct buf b;
if (ei->data_path)
return ei->data_path;
buf_init(&b);
dh = getenv("XDG_DATA_HOME");
if (!dh) {
char *h = getenv("HOME");
if (h)
dh = strconcat(p, h, "/.local/share");
}
if (dh && *dh == '/') {
buf_concat(&b, dh);
buf_concat(&b, "/edlib/");
buf_append_byte(&b, 0);
}
here = set_here(p);
if (here && *here == '/') {
buf_concat(&b, here);
buf_concat(&b, "/edlib/");
buf_append_byte(&b, 0);
}
dd = getenv("XDG_DATA_DIRS");
if (!dd)
dd = "/usr/local/share:/usr/share";
while (*dd) {
char *c = strchrnul(dd, ':');
if (*dd == '/') {
buf_concat_len(&b, dd, c-dd);
buf_concat(&b, "/edlib/");
buf_append_byte(&b, 0);
}
if (*c)
c++;
dd = c;
}
if (b.len)
ei->data_path = buf_final(&b);
else
free(buf_final(&b));
return ei->data_path;
}
static char *set_config_path(struct pane *p safe)
{
struct ed_info *ei = p->data;
char *ch, *cd, *here;
struct buf b;
if (ei->config_path)
return ei->config_path;
buf_init(&b);
ch = getenv("XDG_CONFIG_HOME");
if (!ch) {
char *h = getenv("HOME");
if (h)
ch = strconcat(p, h, "/.config");
}
if (ch && *ch == '/') {
buf_concat(&b, ch);
buf_concat(&b, "/edlib/");
buf_append_byte(&b, 0);
}
here = set_here(p);
if (here && *here == '/') {
buf_concat(&b, here);
buf_concat(&b, "/edlib/");
buf_append_byte(&b, 0);
}
cd = getenv("XDG_CONFIG_DIRS");
if (!cd)
cd = "/etc/xdg";
while (*cd) {
char *c = strchrnul(cd, ':');
if (*cd == '/') {
buf_concat_len(&b, cd, c-cd);
buf_concat(&b, "/edlib/");
buf_append_byte(&b, 0);
}
if (*c)
c++;
cd = c;
}
if (b.len)
ei->config_path = buf_final(&b);
else
free(buf_final(&b));
return ei->config_path;
}
static char *set_bin_path(struct pane *p safe)
{
struct ed_info *ei = p->data;
char *bd, *here;
struct buf b;
if (ei->bin_path)
return ei->bin_path;
buf_init(&b);
here = set_here(p);
if (here && *here == '/') {
buf_concat(&b, here);
if (b.len > 4 &&
strncmp(b.b + b.len-4, "/lib", 4) == 0)
b.len -= 3;
else
buf_concat(&b, "/../");
buf_concat(&b, "bin/");
buf_append_byte(&b, 0);
}
bd = getenv("PATH");
if (!bd)
bd = "/usr/bin:/usr/local/bin";
while (*bd) {
char *c = strchrnul(bd, ':');
if (*bd == '/') {
buf_concat_len(&b, bd, c-bd);
buf_append_byte(&b, 0);
}
if (*c)
c++;
bd = c;
}
if (b.len)
ei->bin_path = buf_final(&b);
else
free(buf_final(&b));
return ei->bin_path;
}
DEF_CMD(global_find_file)
{
/*
* ->str is a file basename. If it contains {COMM}, that will
* be replaced with the "command-name" attr from root, or
* "edlib" if nothing can be found.
* ->str2 is one of "data", "config", "bin"
* We find a file with basename in a known location following
* the XDG Base Directory Specificaton.
* https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
* but also look in the directory containing this library $HERE
* For "data" we look in a directory "edlib" under:
* - $XDG_DATA_HOME, or $HOME/.local/share
* - $HERE
* - $XDG_DATA_DIRS, or /usr/local/share:/usr/share
*
* For config we look in a "edlib" under:
* - $XDG_CONFIG_HOME, or $HOME/.config
* - $HERE
* - $XDG_CONFIG_DIRS, or /etc/xdg
*
* For bin we look in $HERE/../bin and $PATH
*/
char *path = NULL;
const char *base[2] = {ci->str, NULL};
int i;
char *cn;
if (base[0] == NULL || ci->str2 == NULL)
return -Enoarg;
if (strcmp(ci->str2, "data") == 0)
path = set_data_path(ci->home);
else if (strcmp(ci->str2, "config") == 0)
path = set_config_path(ci->home);
else if (strcmp(ci->str2, "bin") == 0)
path = set_bin_path(ci->home);
if (!path)
return Einval;
cn = strstr(base[0], "{COMM}");
if (cn) {
char *p = strndup(base[0], cn - base[0]);
char *comm = attr_find(ci->home->attrs, "command-name");
if (!comm)
comm = "edlib";
base[0] = strconcat(ci->home, p, comm, cn+6);
if (strcmp(comm, "edlib") != 0)
base[1] = strconcat(ci->home, p, "edlib", cn+6);
}
for (i = 0; i < 2 && base[i] ; i++) {
char *pth;
for (pth = path; pth && *pth; pth += strlen(pth)+1) {
char *p = strconcat(NULL, pth, base[i]);
int fd;
if (!p)
continue;
fd = open(p, O_RDONLY);
if (fd < 0) {
free(p);
continue;
}
close(fd);
comm_call(ci->comm2, "cb", ci->focus, 0, NULL, p);
free(p);
return 1;
}
}
return Efalse;
}
static struct pane *editor;
static void catch(int sig)
{
struct cmd_info ci;
if (!editor)
return;
signal(sig, catch);
ci.home = editor;
ci.focus = editor;
ci.num = sig;
ci.num2 = 0;
ci.str = ci.str2 = NULL;
ci.mark = ci.mark2 = NULL;
ci.x = ci.y = 0;
switch (sig) {
case SIGALRM:
ci.key = "fast-alarm-";
key_lookup_prefix(editor->data->map, &ci, True);
break;
case SIGINT:
ci.key = "fast-interrupt-";
key_lookup_prefix(editor->data->map, &ci, True);
break;
}
}
struct pane *editor_new(const char *comm_name)
{
struct pane *ed;
struct ed_info *ei;
if (! (void*) ed_map) {
ed_map = key_alloc();
key_add(ed_map, "global-set-attr", &global_set_attr);
key_add(ed_map, "global-set-command", &global_set_command);
key_add(ed_map, "global-set-command-prefix", &global_set_command);
key_add(ed_map, "global-get-command", &global_get_command);
key_add(ed_map, "global-load-module", &editor_load_module);
key_add(ed_map, "global-config-dir", &global_config_dir);
key_add(ed_map, "xdg-find-edlib-file", &global_find_file);
key_add_prefix(ed_map, "event:", &editor_auto_event);
key_add_prefix(ed_map, "global-multicall-", &editor_multicall);
key_add_prefix(ed_map, "editor:request:",
&editor_request_notify);
key_add_prefix(ed_map, "editor:notify:",
&editor_send_notify);
key_add(ed_map, "Close", &editor_close);
}
ed = pane_register_root(&ed_handle.c, NULL, sizeof(*ei));
if (!ed)
return NULL;
ei = ed->data;
ei->magic = ED_MAGIC;
attr_set_str(&ed->attrs, "command-name", comm_name ?: "edlib");
ei->testing = (getenv("EDLIB_TESTING") != NULL);
ei->map = key_alloc();
key_add_chain(ei->map, ed_map);
ei->cmd = ed_handle;
ei->cmd.m = &ei->map;
/* This allows the pane to see registered commands */
pane_update_handle(ed, &ei->cmd.c);
doc_setup(ed);
log_setup(ed);
window_setup(ed);
signal(SIGINT, catch);
signal(SIGALRM, catch);
editor = ed;
return ed;
}