dte test coverage


Directory: ./
File: src/exec.c
Date: 2025-07-05 13:32:55
Exec Total Coverage
Lines: 103 260 39.6%
Functions: 8 12 66.7%
Branches: 45 142 31.7%

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