-
Notifications
You must be signed in to change notification settings - Fork 0
/
edlib.c
160 lines (140 loc) · 3.45 KB
/
edlib.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
/*
* Copyright Neil Brown ©2015-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* main loop for edlib.
*/
#include <unistd.h>
#include <stdlib.h>
#include <locale.h>
#include <fcntl.h>
#include <wchar.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include "core.h"
#include "misc.h"
static const char shortopt[] = "gtx";
int main(int argc, char *argv[])
{
struct pane *ed;
struct pane *first_window = NULL;
struct pane *p, *doc = NULL;
bool gtk = False, term = False, x11 = False;
int opt;
char *base = NULL;
if (argv[0]) {
base = strrchr(argv[0], '/');
if (base)
base += 1;
else
base = argv[0];
}
ed = editor_new(base);
if (!ed)
exit(1);
while ((opt = getopt(argc, argv, shortopt)) != EOF) {
switch (opt) {
case 'g': gtk = True;
break;
case 't': term = True;
break;
case 'x': x11 = True;
break;
default:
fprintf(stderr, "Usage: edlib [-g] [-t] [file ...]\n");
exit(2);
}
}
if (!gtk && !term && !x11)
term = 1;
setlocale(LC_ALL, "");
setlocale(LC_CTYPE, "enUS.UTF-8");
call("global-load-module", ed, 0, NULL, "lib-config");
call("config-load", ed, 0, NULL, "{COMM}.ini");
call("attach-doc-docs", ed);
while (optind < argc) {
char *file = argv[optind++];
int fd = open(file, O_RDONLY);
if (fd < 0) {
/* '4' says 'allow create' */
doc = call_ret(pane, "doc:open", ed, -1, NULL, file, 4);
} else {
doc = call_ret(pane, "doc:open", ed, fd, NULL, file);
close(fd);
}
}
if (!doc) {
char *welcome_file = call_ret(str, "xdg-find-edlib-file", ed,
0, NULL, "Welcome-{COMM}.txt",
0, NULL, "data");
char *WelcomeText = NULL;
if (welcome_file) {
int fd = open(welcome_file, O_RDONLY);
if (fd >= 0) {
int len = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
if (len > 0 && len < 10000) {
WelcomeText = malloc(len+1);
read(fd, WelcomeText, len);
WelcomeText[len] = 0;
}
close(fd);
}
free(welcome_file);
}
if (!WelcomeText)
WelcomeText = strdup("Welcome.\n");
doc = call_ret(pane, "doc:from-text", ed, 0, NULL,
"*Welcome*", 0, NULL, WelcomeText);
free(WelcomeText);
}
if (!doc) {
fprintf(stderr, "edlib: cannot create default document.\n");
exit(1);
}
if (term) {
char *TERM = getenv("TERM");
p = call_ret(pane, "attach-display-ncurses", ed,
0, NULL, "-", 0, NULL, TERM);
if (p) {
char *e;
e = getenv("SSH_CONNECTION");
if (e && *e)
call("Window:set:REMOTE_SESSION", p,
0, NULL, "yes");
call("Window:set:DISPLAY", p,
0, NULL, getenv("DISPLAY"));
call("Window:set:XAUTHORITY", p,
0, NULL, getenv("XAUTHORITY"));
if (!first_window)
first_window = p;
call("Window:set:no-close", p, 1, NULL,
"Cannot close primary display");
home_call(p, "Window:activate-display", doc);
}
}
if (gtk) {
p = call_ret(pane, "attach-display-gtk",
ed, 0, NULL, getenv("DISPLAY"));
home_call(p, "Window:activate-display", doc);
if (!first_window)
first_window = p;
}
if (x11) {
p = call_ret(pane, "attach-display-x11",
ed, 0, NULL, getenv("DISPLAY"),
0, NULL, getenv("XAUTHORITY"));
home_call(p, "Window:activate-display", doc);
if (!first_window)
first_window = p;
}
if (first_window) {
call("global-multicall-startup-", first_window);
while (call("event:run", ed) == 1)
;
} else
fprintf(stderr, "edlib: cannot create a display\n");
pane_close(ed);
exit(0);
}