-
Notifications
You must be signed in to change notification settings - Fork 33
/
athame.c
405 lines (363 loc) · 10.6 KB
/
athame.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
/* athame.c -- Full vim integration for your shell.*/
/* Copyright (C) 2015 James Kolb
This file is part of Athame.
Athame is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Athame is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Athame. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#ifdef BSD
// Includes OSX
#include <util.h>
typedef void (*sighandler_t) (int);
#else
#include <pty.h>
#endif
#include "athame.h"
#include "athame_intermediary.h"
#include "athame_util.h"
/* Forward declarations used in this file. */
void athame_init(int instream, FILE* outstream) {
if (!ap_is_catching_signals()) {
steal_signal_handler();
}
athame_init_sig(instream, outstream);
if (!ap_is_catching_signals()) {
return_signal_handler();
}
}
static void athame_init_sig(int instream, FILE* outstream) {
athame_outstream = outstream ? outstream : stderr;
cleaned = 0;
expr_pid = 0;
athame_dirty = 0;
updated = 1;
time_to_poll = -1;
vim_started = -1;
stale_polls = 0;
change_since_key = 0;
keys_since_change = 0;
athame_mode[0] = 'n';
athame_mode[1] = '\0';
athame_command[0] = '\0';
command_cursor = 0;
bottom_display[0] = '\0';
last_athame_mode[0] = '\0';
bottom_color = 0;
bottom_style = 0;
bottom_cursor = 0;
cs_confirmed = 0;
athame_failure = 0;
msg_sent = 0;
dev_null = 0;
fifo = 0;
servername = 0;
dir_name = 0;
slice_file_name = 0;
contents_file_name = 0;
update_file_name = 0;
messages_file_name = 0;
vimbed_file_name = 0;
fifo_name = 0;
msg_count_file_name = 0;
if (!athame_is_set("ATHAME_ENABLED", 1)) {
athame_failure = strdup("Athame was disabled on init");
return;
}
if (!athame_is_set("ATHAME_USE_JOBS", ATHAME_USE_JOBS_DEFAULT) && !getenv("DISPLAY")) {
athame_set_failure("Vim with +job required to use Athame without X");
return;
}
asprintf(&servername, "athame_%d", getpid());
char* tmpdir = getenv(temp_dir_loc());
if (tmpdir == NULL) {
char* failure;
asprintf(&failure, "%s environment variable not set", temp_dir_loc());
athame_set_failure(failure);
free(failure);
return;
}
char* parent_name;
asprintf(&parent_name, "%s/athame_vimbed", tmpdir);
asprintf(&dir_name, "%s/%s", parent_name, servername);
mkdir(parent_name, S_IRWXU);
mkdir(dir_name, S_IRWXU);
free(parent_name);
asprintf(&slice_file_name, "%s/slice.txt", dir_name);
asprintf(&contents_file_name, "%s/contents.txt", dir_name);
asprintf(&update_file_name, "%s/update.txt", dir_name);
asprintf(&messages_file_name, "%s/messages.txt", dir_name);
asprintf(&msg_count_file_name, "%s/messageCount.txt", dir_name);
if (getenv("ATHAME_VIMBED_LOCATION")) {
asprintf(&vimbed_file_name, "%s/vimbed.vim",
getenv("ATHAME_VIMBED_LOCATION"));
} else {
asprintf(&vimbed_file_name, "%s/vimbed.vim", VIMBED_LOCATION);
}
asprintf(&fifo_name, "%s/exprPipe", dir_name);
mkfifo(fifo_name, S_IRWXU);
dev_null = fopen("/dev/null", "w");
if (athame_setup_history()) {
return;
}
if (is_vim_alive() && athame_is_set("ATHAME_USE_JOBS", ATHAME_USE_JOBS_DEFAULT)) {
if (athame_setup_fifo()) {
// Vim is still alive but the fifo isn't so we have to kill it.
kill(vim_pid, SIGTERM);
close(vim_term);
wait_then_kill(vim_pid);
}
}
if (is_vim_alive()) {
vim_stage = VIM_NEEDS_RESET;
} else {
vim_stage = VIM_NOT_STARTED;
}
athame_ensure_vim(1, instream);
}
// Cleanup everything athame related.
// If locked is set, make sure not to let control leave athame during cleanup.
// (This is useful if the shell wants to shutdown as soon as it gets any control)
void athame_cleanup(int locked) {
if (cleaned) {
return;
}
cleaned = 1;
int persist = athame_is_set("ATHAME_VIM_PERSIST", 0) &&
vim_stage == VIM_RUNNING && is_vim_alive();
if (expr_pid > 0) {
kill(expr_pid, SIGTERM);
}
if (vim_pid && !persist) {
// forkpty will keep vim open on OSX if we don't close the fd
kill(vim_pid, SIGTERM);
athame_sleep(20, 0, 0);
close(vim_term);
}
if (dev_null) {
fclose(dev_null);
}
if (vimbed_file_name) {
free(vimbed_file_name);
}
if (servername) {
free(servername);
}
if (athame_failure) {
if(!locked) {
athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0);
}
free((char*)athame_failure);
} else if (athame_is_set("ATHAME_SHOW_MODE", 1)) {
if (!locked) {
athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0);
}
}
if (expr_pid > 0) {
wait_then_kill(expr_pid);
}
if (vim_pid && !persist) {
wait_then_kill(vim_pid);
vim_pid = 0;
}
if (fifo) {
close(fifo);
}
if (fifo_name) {
unlink(fifo_name);
free(fifo_name);
}
if (slice_file_name) {
unlink(slice_file_name);
free(slice_file_name);
}
if (contents_file_name) {
unlink(contents_file_name);
free(contents_file_name);
}
if (update_file_name) {
unlink(update_file_name);
free(update_file_name);
}
if (messages_file_name) {
unlink(messages_file_name);
free(messages_file_name);
}
if (msg_count_file_name) {
unlink(msg_count_file_name);
free(msg_count_file_name);
}
if (dir_name) {
rmdir(dir_name);
free(dir_name);
}
}
int athame_enabled() {
if (athame_is_set("ATHAME_ENABLED", 1)) {
if (athame_failure) {
athame_draw_failure();
return 0;
}
unsetenv("ATHAME_ERROR");
return !ap_temp_novim();
} else {
return 0;
}
}
char athame_loop(int instream) {
if (!ap_is_catching_signals()) {
steal_signal_handler();
}
char r = athame_loop_sig(instream);
if (!ap_is_catching_signals()) {
return_signal_handler();
}
return r;
}
static char athame_loop_sig(int instream) {
char returnVal = 0;
sent_to_vim = 0;
time_to_sync = get_time();
last_athame_mode[0] = '\0';
bottom_display[0] = '\0';
// This is a performance step that allows us to bypass starting up (or resetting) vim if we
// aren't going to talk to it.
char first_char =
(vim_stage != VIM_RUNNING) ? athame_get_first_char(instream) : 0;
if (first_char && strchr(ap_nl, first_char)) {
return first_char;
}
athame_ensure_vim(0, 0);
// If we have a character already, assume Vim hasn't had time to sync.
vim_sync = first_char ? VIM_SYNC_NO : VIM_SYNC_YES;
if (!updated) {
athame_update_vimline(athame_row, ap_get_cursor());
} else {
athame_redisplay();
}
if (first_char && !athame_failure) {
returnVal = athame_process_char(first_char);
first_char = 0;
}
while (!returnVal && !athame_failure) {
int selected = 0;
long timeout_msec = get_timeout_msec();
selected = athame_select(instream, vim_term, 0, timeout_msec, 0);
// nvim doesn't end the process when vim quits cleanly
int nvim_checked_quit = athame_nvim && athame_has_clean_quit();
if (!athame_failure && (!is_vim_alive() || nvim_checked_quit)) {
if (!nvim_checked_quit && !athame_has_clean_quit()) {
athame_set_failure("Vim quit unexpectedly");
break;
}
if (!sent_to_vim) {
// We never want to kill the user's shell without giving
// them a chance to type anything.
athame_set_failure("Vim quit");
break;
}
ap_set_line_buffer("");
athame_mode[0] = 'n';
athame_mode[1] = '\0';
athame_redisplay();
return ap_delete;
}
int sr = process_signals();
if (sr != ATHAME_CONTINUE) {
return (char)sr;
}
switch (selected) {
case 0: // Timeout
if (vim_sync >= VIM_SYNC_CHAR_BEHIND) {
vim_sync = VIM_SYNC_NEEDS_POLL;
}
break;
case 1: // Input received
returnVal = athame_process_input(instream);
break;
case 2: // Vim changed
read(vim_term, athame_buffer, DEFAULT_BUFFER_SIZE - 1);
if (!athame_get_vim_info()) {
request_poll();
}
break;
case -1: // Error (possibly from a signal)
// Make sure we keep the mode drawn on a resize.
// This must happen after process_signals so we don't change the
// value of the caught signal
athame_bottom_mode();
}
if (ap_needs_to_leave()) {
if (athame_is_set("ATHAME_SHOW_MODE", 1)) {
athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0);
}
return '\0';
}
if (time_to_poll >= 0 && time_to_poll < get_time()) {
athame_poll_vim(0);
}
// If vim isn't doing anything after pressing 20 keys, failover.
// This allows the user to get back to a normal shell if everything
// is working, but vim always starts up in a weird state
// (Infinte, loop/ex mode, etc)
// TODO: use an env variable instead of 20
if (keys_since_change > 20) {
athame_force_vim_sync();
if (keys_since_change > 0) {
athame_set_failure("20 keys pressed without change. To disable Athame use: AHTAME_ENABLED=0");
}
}
} // end while
if (athame_failure) {
// If we ate the first_char, we should return it.
return returnVal || first_char;
}
if (sent_to_vim) {
// Should already be synced here, but this is a no-op in that case
// and it makes the code less fragile to make sure.
athame_force_vim_sync();
if (strcmp(athame_mode, "i") == 0) {
athame_send_to_vim('\x1d'); //<C-]> Finish abbrevs/kill mappings
athame_force_vim_sync();
}
}
if (athame_is_set("ATHAME_SHOW_MODE", 1)) {
athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0);
}
updated = 0;
return returnVal;
}
void athame_after_bypass() {
if (athame_failure && athame_is_set("ATHAME_SHOW_ERROR", 1)) {
athame_bottom_display("", ATHAME_BOLD, ATHAME_DEFAULT, 0, 0);
}
}
// Redraw bottom stuff before handing off to a process that might block,
// like python.
void athame_char_handled() {
if (athame_is_set("ATHAME_ENABLED", 1) && !ap_needs_to_leave()) {
if (athame_failure) {
athame_draw_failure();
} else {
athame_bottom_mode();
}
}
}