dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 42.1% 104 / 2 / 249
Functions: 61.5% 8 / 0 / 13
Branches: 31.2% 43 / 18 / 156

src/exec.c
Line Branch Exec Source
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include "exec.h"
6 #include "block-iter.h"
7 #include "buffer.h"
8 #include "change.h"
9 #include "commands.h"
10 #include "ctags.h"
11 #include "editor.h"
12 #include "move.h"
13 #include "msg.h"
14 #include "selection.h"
15 #include "spawn.h"
16 #include "tag.h"
17 #include "terminal/mode.h"
18 #include "util/bsearch.h"
19 #include "util/debug.h"
20 #include "util/str-util.h"
21 #include "util/string.h"
22 #include "util/strtonum.h"
23 #include "util/xsnprintf.h"
24 #include "view.h"
25 #include "window.h"
26
27 enum {
28 IN = 1 << 0,
29 OUT = 1 << 1,
30 ERR = 1 << 2,
31 ALL = IN | OUT | ERR,
32 };
33
34 static const struct {
35 char name[11];
36 uint8_t flags;
37 } exec_map[] = {
38 [EXEC_BUFFER] = {"buffer", IN | OUT},
39 [EXEC_COMMAND] = {"command", IN},
40 [EXEC_ECHO] = {"echo", OUT},
41 [EXEC_ERRMSG] = {"errmsg", ERR},
42 [EXEC_EVAL] = {"eval", OUT},
43 [EXEC_LINE] = {"line", IN},
44 [EXEC_MSG] = {"msg", IN | OUT},
45 [EXEC_MSG_A] = {"msgA", IN | OUT},
46 [EXEC_MSG_B] = {"msgB", IN | OUT},
47 [EXEC_MSG_C] = {"msgC", IN | OUT},
48 [EXEC_NULL] = {"null", ALL},
49 [EXEC_OPEN] = {"open", IN | OUT},
50 [EXEC_OPEN_REL] = {"open-rel", IN},
51 [EXEC_SEARCH] = {"search", IN},
52 [EXEC_TAG] = {"tag", OUT},
53 [EXEC_TAG_A] = {"tagA", OUT},
54 [EXEC_TAG_B] = {"tagB", OUT},
55 [EXEC_TAG_C] = {"tagC", OUT},
56 [EXEC_TTY] = {"tty", ALL},
57 [EXEC_WORD] = {"word", IN},
58 };
59
60 24 UNITTEST {
61 24 CHECK_BSEARCH_ARRAY(exec_map, name);
62 24 }
63
64 18 ExecAction lookup_exec_action(const char *name, int fd)
65 {
66 18 BUG_ON(fd < 0 || fd > 2);
67 18 ssize_t i = BSEARCH_IDX(name, exec_map, vstrcmp);
68
3/4
✓ Branch 5 → 6 taken 15 times.
✓ Branch 5 → 8 taken 3 times.
✓ Branch 6 → 7 taken 15 times.
✗ Branch 6 → 8 not taken.
18 return (i >= 0 && (exec_map[i].flags & 1u << fd)) ? i : EXEC_INVALID;
69 }
70
71 1 void collect_exec_actions(PointerArray *a, StringView prefix, int fd)
72 {
73
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 10 not taken.
1 if (unlikely(fd < 0 || fd > 2)) {
74 return;
75 }
76
77 1 unsigned int flag = 1u << fd;
78
2/2
✓ Branch 9 → 4 taken 20 times.
✓ Branch 9 → 10 taken 1 time.
21 for (size_t i = 0; i < ARRAYLEN(exec_map); i++) {
79 20 const char *action = exec_map[i].name;
80
3/4
✓ Branch 4 → 5 taken 14 times.
✓ Branch 4 → 8 taken 6 times.
✓ Branch 5 → 6 taken 14 times.
✗ Branch 5 → 8 not taken.
20 if ((exec_map[i].flags & flag) && str_has_sv_prefix(action, prefix)) {
81 14 ptr_array_append(a, xstrdup(action));
82 }
83 }
84 }
85
86 static void open_files_from_string(EditorState *e, const String *str)
87 {
88 PointerArray filenames = ptr_array_new(0);
89 for (size_t pos = 0, size = str->len; pos < size; ) {
90 char *filename = buf_next_line(str->buffer, &pos, size); // Mutates `str->buffer`
91 if (filename[0] != '\0') {
92 ptr_array_append(&filenames, filename);
93 }
94 }
95
96 if (filenames.count == 0) {
97 return;
98 }
99
100 ptr_array_append(&filenames, NULL);
101 window_open_files(e->window, (char**)filenames.ptrs, NULL);
102
103 // TODO: re-enable this when the todo in allow_macro_recording() is done
104 // macro_command_hook(&e->macro, "open", (char**)filenames.ptrs);
105
106 ptr_array_free_array(&filenames);
107 }
108
109 static void parse_and_activate_message(EditorState *e, StringView str, ExecAction action)
110 {
111 if (unlikely(str.length == 0)) {
112 error_msg(&e->err, "child produced no output");
113 return;
114 }
115
116 size_t msgs_idx = (action == EXEC_MSG) ? 0 : action - EXEC_MSG_A;
117 BUG_ON(msgs_idx >= ARRAYLEN(e->messages));
118 MessageList *msgs = &e->messages[msgs_idx];
119 size_t count = msgs->array.count;
120 size_t x;
121
122 if (!count || !buf_parse_size(str, &x) || !x) {
123 return;
124 }
125
126 msgs->pos = MIN(x - 1, count - 1);
127 activate_current_message(msgs, e->window, &e->err);
128 }
129
130 static void parse_and_activate_tags(EditorState *e, StringView str, ExecAction action)
131 {
132 ErrorBuffer *ebuf = &e->err;
133 if (unlikely(str.length == 0)) {
134 error_msg(ebuf, "child produced no output");
135 return;
136 }
137
138 char cwd[8192];
139 if (unlikely(!getcwd(cwd, sizeof cwd))) {
140 error_msg_errno(ebuf, "getcwd() failed");
141 return;
142 }
143
144 const StringView dir = strview(cwd);
145 const char *buffer_filename = e->buffer->abs_filename;
146 TagFile *tf = &e->tagfile;
147 enum {NOT_LOADED, LOADED, FAILED} tf_status = NOT_LOADED;
148
149 size_t msgs_idx = (action == EXEC_TAG) ? e->options.msg_tag : action - EXEC_TAG_A;
150 BUG_ON(msgs_idx >= ARRAYLEN(e->messages));
151 MessageList *msgs = &e->messages[msgs_idx];
152 clear_messages(msgs);
153
154 for (size_t pos = 0; pos < str.length; ) {
155 Tag tag;
156 StringView line = buf_slice_next_line(str.data, &pos, str.length);
157 if (line.length == 0) {
158 continue;
159 }
160
161 bool parsed = parse_ctags_line(&tag, line);
162 if (parsed) {
163 // `line` is a valid tags(5) file entry; handle it directly
164 add_message_for_tag(msgs, &tag, dir);
165 continue;
166 }
167
168 // Treat `line` as a simple tag name (look it up in the tags(5) file)
169 switch (tf_status) {
170 case NOT_LOADED:
171 tf_status = load_tag_file(tf, ebuf) ? LOADED : FAILED;
172 if (tf_status == FAILED) {
173 continue;
174 }
175 // Fallthrough
176 case LOADED:
177 tag_lookup(tf, msgs, ebuf, line, buffer_filename);
178 continue;
179 case FAILED:
180 continue;
181 }
182 }
183
184 activate_current_message_save(msgs, &e->bookmarks, e->view, ebuf);
185 }
186
187 static void insert_to_selection (
188 View *view,
189 StringView text,
190 const SelectionInfo *info
191 ) {
192 size_t del_count = info->eo - info->so;
193 buffer_replace_bytes(view, del_count, text);
194
195 if (text.length == 0) {
196 // If the selection was replaced with 0 bytes then there's nothing
197 // new to select, so just unselect instead
198 unselect(view);
199 return;
200 }
201
202 // Keep the selection and adjust the size to the newly inserted text
203 size_t so = info->so;
204 size_t eo = so + (text.length - 1);
205 block_iter_goto_offset(&view->cursor, info->swapped ? so : eo);
206 view->sel_so = info->swapped ? eo : so;
207 view->sel_eo = SEL_EO_RECALC;
208 }
209
210 4 static void show_spawn_error_msg(ErrorBuffer *ebuf, StringView errstr, int err)
211 {
212
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 3 times.
4 if (err <= 0) {
213 // spawn() returned an error code instead of an exit code, which
214 // indicates that something failed before (or during) the child
215 // process exec(3p), or that an error occurred in wait_child().
216 // In this case, error_msg() has already been called.
217 1 return;
218 }
219
220 3 char msg[512];
221 3 msg[0] = '\0';
222
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 10 taken 2 times.
3 if (errstr.length) {
223 1 size_t pos = 0;
224 1 StringView line = buf_slice_next_line(errstr.data, &pos, errstr.length);
225 1 BUG_ON(pos == 0);
226 1 size_t len = MIN(line.length, sizeof(msg) - 8);
227 1 xsnprintf(msg, sizeof(msg), ": \"%.*s\"", (int)len, line.data);
228 }
229
230
2/2
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 15 taken 2 times.
3 if (err >= 256) {
231 1 int sig = err >> 8;
232 1 const char *str = strsignal(sig);
233
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 1 time.
1 error_msg(ebuf, "Child received signal %d (%s)%s", sig, str ? str : "??", msg);
234 2 } else if (err) {
235 2 error_msg(ebuf, "Child returned %d%s", err, msg);
236 }
237 }
238
239 static size_t msgs_idx_from_exec_action(ExecAction action)
240 {
241 BUG_ON(action < EXEC_MSG || action > EXEC_MSG_C);
242 return (action == EXEC_MSG) ? 0 : action - EXEC_MSG_A;
243 }
244
245 45 static SpawnAction spawn_action_from_exec_action(ExecAction action)
246 {
247 45 BUG_ON(action == EXEC_INVALID);
248
1/2
✓ Branch 4 → 5 taken 45 times.
✗ Branch 4 → 7 not taken.
45 if (action == EXEC_NULL) {
249 return SPAWN_NULL;
250
2/2
✓ Branch 5 → 6 taken 15 times.
✓ Branch 5 → 7 taken 30 times.
45 } else if (action == EXEC_TTY) {
251 return SPAWN_TTY;
252 } else {
253 15 return SPAWN_PIPE;
254 }
255 }
256
257 // NOLINTNEXTLINE(readability-function-size)
258 15 ssize_t handle_exec (
259 EditorState *e,
260 const char **argv,
261 ExecAction actions[3],
262 ExecFlags exec_flags
263 ) {
264 15 View *view = e->view;
265 15 const BlockIter saved_cursor = view->cursor;
266 15 const ssize_t saved_sel_so = view->sel_so;
267 15 const ssize_t saved_sel_eo = view->sel_eo;
268 15 String input = string_new(0);
269 15 size_t input_from_buffer_length = 0;
270 15 bool input_from_buffer = false;
271 15 bool output_to_buffer = (actions[STDOUT_FILENO] == EXEC_BUFFER);
272 15 bool quiet = (exec_flags & EXECFLAG_QUIET);
273
274 10 SpawnContext ctx = {
275 .argv = argv,
276 15 .outputs = {string_new(0), string_new(0)},
277 .quiet = quiet,
278 15 .ebuf = &e->err,
279
2/2
✓ Branch 8 → 9 taken 10 times.
✓ Branch 8 → 10 taken 5 times.
15 .lines = output_to_buffer ? view->window->edit_h : 0,
280
1/2
✓ Branch 10 → 11 taken 5 times.
✗ Branch 10 → 12 not taken.
5 .columns = output_to_buffer ? view->window->edit_w : 0,
281 .actions = {
282 15 spawn_action_from_exec_action(actions[0]),
283 15 spawn_action_from_exec_action(actions[1]),
284 15 spawn_action_from_exec_action(actions[2]),
285 },
286 };
287
288 15 ExecAction in_action = actions[STDIN_FILENO];
289
2/10
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 17 taken 3 times.
✗ Branch 12 → 22 not taken.
✗ Branch 12 → 30 not taken.
✗ Branch 12 → 32 not taken.
✗ Branch 12 → 34 not taken.
✗ Branch 12 → 36 not taken.
✗ Branch 12 → 38 not taken.
✗ Branch 12 → 40 not taken.
✓ Branch 12 → 42 taken 12 times.
15 switch (in_action) {
290 case EXEC_LINE:
291 input_from_buffer = true;
292 if (!view->selection) {
293 move_bol(view, BOL_SIMPLE);
294 input_from_buffer_length = block_iter_get_line(&view->cursor).length;
295 }
296 break;
297 3 case EXEC_BUFFER:
298 3 input_from_buffer = true;
299
1/2
✓ Branch 17 → 18 taken 3 times.
✗ Branch 17 → 42 not taken.
3 if (!view->selection) {
300 3 const Block *blk;
301
2/2
✓ Branch 20 → 19 taken 3 times.
✓ Branch 20 → 21 taken 3 times.
6 block_for_each(blk, &view->buffer->blocks) {
302 3 input_from_buffer_length += blk->size;
303 }
304 3 move_bof(view);
305 }
306 break;
307 case EXEC_WORD:
308 input_from_buffer = true;
309 if (!view->selection) {
310 CurrentLineRef lr = get_current_line_and_offset(view->cursor);
311 WordBounds wb = get_bounds_for_word_under_cursor(lr);
312 if (wb.end == 0) {
313 break;
314 }
315
316 // If `wb.start` is less than `lr.cursor_offset` here, the
317 // subtraction wraps but nevertheless works as intended.
318 // view->cursor.offset -= (lr.cursor_offset - wb.start) ==
319 view->cursor.offset += wb.start - lr.cursor_offset;
320
321 input_from_buffer_length = wb.end - wb.start;
322 BUG_ON(view->cursor.offset >= view->cursor.blk->size);
323 }
324 break;
325 case EXEC_MSG:
326 case EXEC_MSG_A:
327 case EXEC_MSG_B:
328 case EXEC_MSG_C:
329 input = dump_messages(&e->messages[msgs_idx_from_exec_action(in_action)]);
330 break;
331 case EXEC_COMMAND:
332 input = history_dump(&e->command_history);
333 break;
334 case EXEC_SEARCH:
335 input = history_dump(&e->search_history);
336 break;
337 case EXEC_OPEN:
338 input = file_history_dump(&e->file_history);
339 break;
340 case EXEC_OPEN_REL:
341 input = file_history_dump_relative(&e->file_history);
342 break;
343 case EXEC_NULL:
344 case EXEC_TTY:
345 break;
346 // These can't be used as input actions and should be prevented by
347 // the validity checks in cmd_exec():
348 case EXEC_TAG:
349 case EXEC_TAG_A:
350 case EXEC_TAG_B:
351 case EXEC_TAG_C:
352 case EXEC_ECHO:
353 case EXEC_EVAL:
354 case EXEC_ERRMSG:
355 case EXEC_INVALID:
356 default:
357 BUG("unhandled action");
358 return -1;
359 }
360
361 // This could be left uninitialized, but doing so makes some old compilers
362 // produce false-positive "-Wmaybe-uninitialized" warnings
363 15 SelectionInfo info = {.si = view->cursor};
364
365
1/4
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 47 taken 15 times.
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 49 not taken.
15 if (view->selection && (input_from_buffer || output_to_buffer)) {
366 info = init_selection(view);
367 view->cursor = info.si;
368 if (input_from_buffer) {
369 input_from_buffer_length = info.eo - info.so;
370 }
371 }
372
373
2/2
✓ Branch 47 → 48 taken 3 times.
✓ Branch 47 → 49 taken 12 times.
15 if (input_from_buffer) {
374 3 input = block_iter_get_bytes(view->cursor, input_from_buffer_length);
375 } else {
376 12 BUG_ON(input_from_buffer_length);
377 }
378
379 15 ctx.input = strview_from_string(&input);
380 15 yield_terminal(e, quiet);
381 15 int err = spawn(&ctx);
382
3/4
✓ Branch 53 → 54 taken 14 times.
✓ Branch 53 → 55 taken 1 time.
✓ Branch 54 → 55 taken 14 times.
✗ Branch 54 → 56 not taken.
15 bool prompt = (err >= 0) && (exec_flags & EXECFLAG_PROMPT);
383 15 resume_terminal(e, quiet, prompt);
384 15 string_free(&input);
385 15 ctx.input.length = 0;
386
387
2/2
✓ Branch 58 → 59 taken 4 times.
✓ Branch 58 → 63 taken 11 times.
15 if (err != 0) {
388 4 show_spawn_error_msg(&e->err, strview_from_string(&ctx.outputs[1]), err);
389 4 string_free(&ctx.outputs[0]);
390 4 string_free(&ctx.outputs[1]);
391 4 view->cursor = saved_cursor;
392 4 return -1;
393 }
394
395 11 string_free(&ctx.outputs[1]);
396 11 String *output = &ctx.outputs[0];
397 11 bool strip_trailing_newline = (exec_flags & EXECFLAG_STRIP_NL);
398 11 if (
399 strip_trailing_newline
400
1/2
✗ Branch 64 → 65 not taken.
✓ Branch 64 → 70 taken 11 times.
11 && output_to_buffer
401 && output->len > 0
402 && output->buffer[output->len - 1] == '\n'
403 ) {
404 output->len--;
405 if (output->len > 0 && output->buffer[output->len - 1] == '\r') {
406 output->len--;
407 }
408 }
409
410
2/2
✓ Branch 70 → 71 taken 6 times.
✓ Branch 70 → 72 taken 5 times.
11 if (!output_to_buffer) {
411 6 view->cursor = saved_cursor;
412 6 view->sel_so = saved_sel_so;
413 6 view->sel_eo = saved_sel_eo;
414 6 mark_all_lines_changed(view->buffer);
415 }
416
417 11 ExecAction out_action = actions[STDOUT_FILENO];
418
2/8
✓ Branch 72 → 73 taken 5 times.
✗ Branch 72 → 76 not taken.
✗ Branch 72 → 80 not taken.
✗ Branch 72 → 81 not taken.
✗ Branch 72 → 83 not taken.
✗ Branch 72 → 85 not taken.
✗ Branch 72 → 87 not taken.
✓ Branch 72 → 89 taken 6 times.
11 switch (out_action) {
419 5 case EXEC_BUFFER:
420
1/2
✗ Branch 73 → 74 not taken.
✓ Branch 73 → 75 taken 5 times.
5 if (view->selection) {
421 insert_to_selection(view, strview_from_string(output), &info);
422 } else {
423 // Replace any unselected text used as input, e.g. for `-i line`
424 5 size_t del_count = input_from_buffer_length;
425 5 buffer_replace_bytes(view, del_count, strview_from_string(output));
426 }
427 break;
428 case EXEC_ECHO:
429 if (output->len) {
430 size_t pos = 0;
431 StringView line = buf_slice_next_line(output->buffer, &pos, output->len);
432 info_msg(&e->err, "%.*s", (int)line.length, line.data);
433 }
434 break;
435 case EXEC_MSG:
436 case EXEC_MSG_A:
437 case EXEC_MSG_B:
438 case EXEC_MSG_C:
439 parse_and_activate_message(e, strview_from_string(output), out_action);
440 break;
441 case EXEC_OPEN:
442 open_files_from_string(e, output);
443 break;
444 case EXEC_TAG:
445 case EXEC_TAG_A:
446 case EXEC_TAG_B:
447 case EXEC_TAG_C:
448 parse_and_activate_tags(e, strview_from_string(output), out_action);
449 break;
450 case EXEC_EVAL:
451 exec_normal_config(e, strview_from_string(output));
452 break;
453 case EXEC_NULL:
454 case EXEC_TTY:
455 break;
456 // These can't be used as output actions
457 case EXEC_COMMAND:
458 case EXEC_ERRMSG:
459 case EXEC_LINE:
460 case EXEC_OPEN_REL:
461 case EXEC_SEARCH:
462 case EXEC_WORD:
463 case EXEC_INVALID:
464 default:
465 BUG("unhandled action");
466 return -1;
467 }
468
469 11 size_t output_len = output->len;
470 11 string_free(output);
471 11 return output_len;
472 }
473
474 15 void yield_terminal(EditorState *e, bool quiet)
475 {
476
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 7 taken 15 times.
15 if (e->flags & EFLAG_HEADLESS) {
477 return;
478 }
479
480 if (quiet) {
481 term_raw_isig();
482 } else {
483 need_term_reset_on_fatal_error = 0;
484 ui_end(&e->terminal, false);
485 term_cooked();
486 }
487 }
488
489 15 void resume_terminal(EditorState *e, bool quiet, bool prompt)
490 {
491
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 11 taken 15 times.
15 if (e->flags & EFLAG_HEADLESS) {
492 return;
493 }
494
495 term_raw();
496 if (!quiet) {
497 BUG_ON(need_term_reset_on_fatal_error);
498 if (prompt) {
499 any_key(&e->terminal, e->options.esc_timeout);
500 }
501 ui_start(e);
502 need_term_reset_on_fatal_error = 1;
503 }
504 }
505