dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 60.8% 192 / 0 / 316
Functions: 85.7% 12 / 0 / 14
Branches: 42.9% 88 / 10 / 215

src/main.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdbool.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/stat.h>
8 #include <sys/utsname.h>
9 #include <unistd.h>
10 #include "block.h"
11 #include "buffer.h"
12 #include "command/error.h"
13 #include "commands.h"
14 #include "compat.h"
15 #include "config.h"
16 #include "editor.h"
17 #include "encoding.h"
18 #include "file-history.h"
19 #include "frame.h"
20 #include "history.h"
21 #include "load-save.h"
22 #include "move.h"
23 #include "msg.h"
24 #include "palette.h"
25 #include "search.h"
26 #include "showkey.h"
27 #include "signals.h"
28 #include "syntax/state.h"
29 #include "syntax/syntax.h"
30 #include "tag.h"
31 #include "terminal/mode.h"
32 #include "terminal/terminal.h"
33 #include "trace.h"
34 #include "ui.h"
35 #include "util/debug.h"
36 #include "util/exitcode.h"
37 #include "util/fd.h"
38 #include "util/log.h"
39 #include "util/macros.h"
40 #include "util/path.h"
41 #include "util/progname.h"
42 #include "util/str-util.h"
43 #include "util/string-view.h"
44 #include "util/string.h"
45 #include "util/strtonum.h"
46 #include "util/xmalloc.h"
47 #include "util/xreadwrite.h"
48 #include "version.h"
49 #include "view.h"
50 #include "window.h"
51
52 1 static ExitCode list_builtin_configs(void)
53 {
54 1 String str = dump_builtin_configs();
55 1 BUG_ON(!str.buffer);
56 1 ExitCode e = ec_write_stdout(str.buffer, str.len);
57 1 string_free(&str);
58 1 return e;
59 }
60
61 2 static ExitCode dump_builtin_config(const char *name)
62 {
63 2 const BuiltinConfig *cfg = get_builtin_config(name);
64
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 1 time.
2 if (!cfg) {
65 1 return ec_usage_error("no built-in config with name '%s'", name);
66 }
67 1 return ec_write_stdout(cfg->text.data, cfg->text.length);
68 }
69
70 2 static ExitCode lint_syntax(const char *filename, SyntaxLoadFlags flags)
71 {
72 2 EditorState *e = init_editor_state(getenv("HOME"), getenv("DTE_HOME"));
73 2 e->err.print_to_stderr = true;
74 2 BUG_ON(e->status != EDITOR_INITIALIZING);
75
76 2 const Syntax *syntax = load_syntax_file(e, filename, flags | SYN_MUST_EXIST);
77
3/4
✓ Branch 8 → 9 taken 1 time.
✓ Branch 8 → 13 taken 1 time.
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 13 not taken.
2 bool ok = syntax && e->err.nr_errors == 0;
78
79 1 if (ok) {
80 1 const size_t n = syntax->states.count;
81
1/2
✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 12 not taken.
1 const char *plural = (n == 1) ? "" : "s";
82 1 const char *name = syntax->name;
83 1 printf("OK: loaded syntax '%s' with %zu state%s\n", name, n, plural);
84 }
85
86 2 free_editor_state(e);
87
2/2
✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 16 taken 1 time.
2 return ok ? EC_OK : EC_DATA_ERROR;
88 }
89
90 8 static ExitCode init_std_fds_headless(int std_fds[2])
91 {
92 8 FILE *streams[] = {stdin, stdout};
93
2/2
✓ Branch 16 → 3 taken 16 times.
✓ Branch 16 → 17 taken 8 times.
24 for (int i = 0; i < ARRAYLEN(streams); i++) {
94
1/2
✓ Branch 4 → 5 taken 16 times.
✗ Branch 4 → 10 not taken.
16 if (!isatty(i)) {
95 // Try to duplicate redirected stdin/stdout, as described
96 // in init_std_fds()
97 16 int fd = fcntl(i, F_DUPFD_CLOEXEC, 3);
98
1/4
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 16 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
16 if (fd == -1 && errno != EBADF) {
99 return ec_error("fcntl", EC_OS_ERROR);
100 }
101 16 std_fds[i] = fd;
102 }
103 // Always reopen stdin and stdout as /dev/null, regardless of
104 // whether they were duplicated above
105
3/4
✓ Branch 10 → 11 taken 8 times.
✓ Branch 10 → 12 taken 8 times.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 16 times.
24 if (!freopen("/dev/null", i ? "w" : "r", streams[i])) {
106 return ec_error("freopen", EC_IO_ERROR);
107 }
108 }
109
110
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 22 taken 8 times.
8 if (!fd_is_valid(STDERR_FILENO)) {
111 // If FD 2 isn't valid (e.g. because it was closed), point it
112 // to /dev/null to ensure open(3) doesn't allocate it to other
113 // opened files
114 if (!freopen("/dev/null", "w", stderr)) {
115 return ec_error("freopen", EC_IO_ERROR);
116 }
117 }
118
119 return EC_OK;
120 }
121
122 9 static ExitCode init_std_fds(int std_fds[2], bool headless)
123 {
124
2/2
✓ Branch 2 → 3 taken 8 times.
✓ Branch 2 → 4 taken 1 time.
9 if (headless) {
125 8 return init_std_fds_headless(std_fds);
126 }
127
128 1 FILE *streams[3] = {stdin, stdout, stderr};
129
1/2
✓ Branch 27 → 5 taken 1 time.
✗ Branch 27 → 28 not taken.
1 for (int i = 0; i < ARRAYLEN(streams); i++) {
130
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 1 time.
1 if (is_controlling_tty(i)) {
131 continue;
132 }
133
134
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 1 time.
1 if (i < STDERR_FILENO) {
135 // Try to create a duplicate fd for redirected stdin/stdout; to
136 // allow reading/writing after freopen(3) closes the original
137 1 int fd = fcntl(i, F_DUPFD_CLOEXEC, 3);
138
1/4
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 1 time.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
1 if (fd == -1 && errno != EBADF) {
139 return ec_error("fcntl", EC_OS_ERROR);
140 }
141 1 std_fds[i] = fd;
142 }
143
144 // Ensure standard streams are connected to the terminal during
145 // editor operation, regardless of how they were redirected
146
2/4
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 16 not taken.
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 20 not taken.
2 if (unlikely(!freopen("/dev/tty", i ? "w" : "r", streams[i]))) {
147 1 const char *err = strerror(errno);
148 1 return ec_io_error("Failed to open /dev/tty for fd %d: %s", i, err);
149 }
150
151 int new_fd = fileno(streams[i]);
152 if (unlikely(new_fd != i)) {
153 // This should never happen in a single-threaded program.
154 // freopen() should call fclose() followed by open() and
155 // POSIX requires a successful call to open() to return the
156 // lowest available file descriptor.
157 return ec_os_error("freopen() changed fd from %d to %d", i, new_fd);
158 }
159
160 if (unlikely(!is_controlling_tty(new_fd))) {
161 return ec_error("tcgetpgrp", EC_OS_ERROR);
162 }
163 }
164
165 return EC_OK;
166 }
167
168 // Open special Buffers for duplicated stdin and/or stdout FDs, as created
169 // by init_std_fds()
170 8 static Buffer *init_std_buffer(EditorState *e, int fds[2])
171 {
172 8 const char *name = NULL;
173 8 Buffer *buffer = NULL;
174
175
1/2
✓ Branch 2 → 3 taken 8 times.
✗ Branch 2 → 11 not taken.
8 if (fds[STDIN_FILENO] >= 3) {
176 8 ErrorBuffer *ebuf = &e->err;
177 8 size_t unused; // Set but unused out-param for longest line
178 8 buffer = buffer_new(&e->buffers, &e->options, encoding_from_type(UTF8));
179
1/2
✓ Branch 6 → 7 taken 8 times.
✗ Branch 6 → 8 not taken.
8 if (read_blocks(buffer, &e->options, fds[STDIN_FILENO], &unused)) {
180 8 name = "(stdin)";
181 8 buffer->temporary = true;
182 } else {
183 error_msg(ebuf, "Unable to read redirected stdin");
184 buffer_remove_unlock_and_free(&e->buffers, buffer, ebuf, &e->locks_ctx);
185 buffer = NULL;
186 }
187 }
188
189
1/2
✓ Branch 11 → 12 taken 8 times.
✗ Branch 11 → 15 not taken.
8 if (fds[STDOUT_FILENO] >= 3) {
190
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 8 times.
8 if (!buffer) {
191 buffer = open_empty_buffer(&e->buffers, &e->options);
192 name = "(stdout)";
193 } else {
194 name = "(stdin|stdout)";
195 }
196 8 buffer->stdout_buffer = true;
197 8 buffer->temporary = true;
198 }
199
200 8 BUG_ON(!buffer != !name);
201
1/2
✓ Branch 17 → 18 taken 8 times.
✗ Branch 17 → 20 not taken.
8 if (name) {
202 8 buffer_set_display_filename(buffer, xstrdup(name));
203 }
204
205 8 return buffer;
206 }
207
208 // Write the contents of the special `stdout` Buffer (as created by
209 // init_std_buffer()) to `stdout` and free its remaining allocations
210 8 static SystemErrno buffer_write_blocks_and_free(Buffer *buffer, int fd)
211 {
212 8 BUG_ON(!buffer->stdout_buffer);
213 8 const Block *blk;
214 8 SystemErrno err = 0;
215
216
2/2
✓ Branch 9 → 5 taken 8 times.
✓ Branch 9 → 10 taken 8 times.
16 block_for_each(blk, &buffer->blocks) {
217
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 8 times.
8 if (xwrite_all(fd, blk->data, blk->size) < 0) {
218 err = errno;
219 break;
220 }
221 }
222
223 // Other allocations for `buffer` should have already been freed
224 // by buffer_unlock_and_free()
225 8 free_blocks(buffer);
226 8 free(buffer);
227 8 return err;
228 }
229
230 static void init_trace_logging(LogLevel level)
231 {
232 if (!TRACE_LOGGING_ENABLED || level < LOG_LEVEL_TRACE) {
233 return;
234 }
235
236 const char *flagstr = xgetenv("DTE_LOG_TRACE");
237 if (!flagstr) {
238 LOG_NOTICE("DTE_LOG_LEVEL=trace used with no effect; DTE_LOG_TRACE also needed");
239 return;
240 }
241
242 TraceLoggingFlags flags = trace_flags_from_str(flagstr);
243 if (!flags) {
244 LOG_WARNING("no known flags in DTE_LOG_TRACE='%s'", flagstr);
245 return;
246 }
247
248 LOG_INFO("DTE_LOG_TRACE=%s", flagstr);
249 set_trace_logging_flags(flags);
250 }
251
252 8 static ExitCode init_logging(LogOpenFlags logflags)
253 {
254 8 const char *filename = xgetenv("DTE_LOG");
255
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 21 taken 8 times.
8 if (!filename) {
256 return EC_OK;
257 }
258
259 const char *req_level_str = getenv("DTE_LOG_LEVEL");
260 LogLevel req_level = log_level_from_str(req_level_str);
261 if (req_level == LOG_LEVEL_NONE) {
262 return EC_OK;
263 }
264 if (req_level == LOG_LEVEL_INVALID) {
265 return ec_usage_error("invalid $DTE_LOG_LEVEL value: '%s'", req_level_str);
266 }
267
268 LogLevel got_level = log_open(filename, req_level, logflags);
269 if (got_level == LOG_LEVEL_NONE) {
270 const char *err = strerror(errno);
271 return ec_io_error("Failed to open $DTE_LOG (%s): %s", filename, err);
272 }
273
274 const char *got_level_str = log_level_to_str(got_level);
275 if (got_level != req_level) {
276 const char *r = req_level_str;
277 const char *g = got_level_str;
278 LOG_WARNING("log level '%s' unavailable; falling back to '%s'", r, g);
279 }
280
281 LOG_INFO("logging to '%s' (level: %s)", filename, got_level_str);
282
283 if (!(logflags & LOGOPEN_USE_COLOR)) {
284 LOG_INFO("log colors disabled ($NO_COLOR)");
285 }
286
287 init_trace_logging(got_level);
288 return EC_OK;
289 }
290
291 8 static void read_history_files(EditorState *e, bool headless)
292 {
293 8 const size_t size_limit = 64u << 20; // 64 MiB
294 8 const char *dir = e->user_config_dir;
295 8 ErrorBuffer *ebuf = &e->err;
296
297
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 8 times.
8 if (e->flags & EFLAG_FILE_HIST) {
298 char *path = path_join(dir, "file-history");
299 file_history_load(&e->file_history, ebuf, path, size_limit);
300 }
301
302
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 8 times.
8 if (e->flags & EFLAG_CMD_HIST) {
303 char *path = path_join(dir, "command-history");
304 history_load(&e->command_history, ebuf, path, size_limit);
305 }
306
307
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 13 taken 8 times.
8 if (e->flags & EFLAG_SEARCH_HIST) {
308 char *path = path_join(dir, "search-history");
309 history_load(&e->search_history, ebuf, path, size_limit);
310 if (e->search_history.last) {
311 search_set_regexp(&e->search, e->search_history.last->text);
312 }
313 }
314
315 // There's not much sense in saving history for headless sessions, but
316 // we do still load it (above, if applicable), to make it available to
317 // e.g. `exec -i command`
318
1/2
✓ Branch 13 → 14 taken 8 times.
✗ Branch 13 → 15 not taken.
8 if (headless) {
319 8 e->flags &= ~EFLAG_ALL_HIST;
320 }
321 8 }
322
323 8 static void write_history_files(EditorState *e)
324 {
325 8 EditorFlags flags = e->flags;
326
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 8 times.
8 if (flags & EFLAG_CMD_HIST) {
327 history_save(&e->command_history, &e->err);
328 }
329
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 8 times.
8 if (flags & EFLAG_SEARCH_HIST) {
330 history_save(&e->search_history, &e->err);
331 }
332
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 8 times.
8 if (flags & EFLAG_FILE_HIST) {
333 file_history_save(&e->file_history, &e->err);
334 }
335 8 }
336
337 static void lookup_tags (
338 EditorState *e,
339 const char *tags[],
340 size_t nr_tags,
341 View *closeable_default_view
342 ) {
343 TagFile *tagfile = &e->tagfile;
344 ErrorBuffer *ebuf = &e->err;
345 if (nr_tags == 0 || !load_tag_file(tagfile, ebuf)) {
346 return;
347 }
348
349 MessageList *msgs = &e->messages[e->options.msg_tag];
350 clear_messages(msgs);
351
352 for (size_t i = 0; i < nr_tags; i++) {
353 StringView tagname = strview(tags[i]);
354 tag_lookup(tagfile, msgs, ebuf, tagname, NULL);
355 }
356
357 if (
358 activate_current_message(msgs, e->window, ebuf)
359 && closeable_default_view
360 && e->window->views.count >= 2
361 ) {
362 // Close the default/empty buffer, if another buffer is opened
363 // for a tag (and no commands were executed via `-c` or `-C`)
364 view_remove(closeable_default_view);
365 }
366 }
367
368 8 static View *open_initial_buffers (
369 EditorState *e,
370 Buffer *std_buffer,
371 char *args[],
372 size_t nr_args
373 ) {
374 // Open files specified as command-line arguments
375 8 Window *window = e->window;
376
1/2
✗ Branch 20 → 3 not taken.
✓ Branch 20 → 21 taken 8 times.
8 for (size_t i = 0, line = 0, col = 0; i < nr_args; i++) {
377 const char *arg = args[i];
378 if (line == 0 && *arg == '+' && str_to_filepos(arg + 1, &line, &col)) {
379 // +line[:col] argument parsed (affects next argument)
380 continue;
381 }
382
383 char *alloc = NULL;
384 if (line == 0) {
385 StringView path = parse_file_line_col(arg, &line, &col);
386 if (path.length) {
387 // file:line[:col] argument parsed; extract `file` and use below
388 arg = alloc = strview_clone_cstring(path);
389 }
390 }
391
392 View *view = window_open_buffer(window, arg, false, NULL);
393 free(alloc);
394 if (view && line) {
395 set_view(view);
396 move_to_filepos(view, line, col);
397 }
398
399 line = 0;
400 }
401
402
1/2
✓ Branch 21 → 22 taken 8 times.
✗ Branch 21 → 23 not taken.
8 if (std_buffer) {
403 // Open special buffer for redirected stdin/stdout
404 8 window_add_buffer(window, std_buffer);
405 }
406
407 // Open default buffer, if none were opened above
408
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 8 times.
8 View *dview = window->views.count ? NULL : window_open_empty_buffer(window);
409
410 8 set_view(window_get_first_view(window));
411 8 return dview;
412 }
413
414 static const char copyright[] =
415 "dte " VERSION "\n"
416 "(C) 2013-2026 Craig Barnes\n"
417 "(C) 2010-2015 Timo Hirvonen\n"
418 "This program is free software; you can redistribute and/or modify\n"
419 "it under the terms of the GNU General Public License version 2\n"
420 "<https://www.gnu.org/licenses/old-licenses/gpl-2.0.html>.\n"
421 "There is NO WARRANTY, to the extent permitted by law.\n";
422
423 static const char usage[] =
424 "Usage: %s [OPTIONS] [[+LINE] FILE]...\n\n"
425 "Options:\n"
426 " -c COMMAND Run COMMAND after editor starts\n"
427 " -t CTAG Jump to source location of CTAG\n"
428 " -r RCFILE Read user config from RCFILE instead of ~/.dte/rc\n"
429 " -s FILE Validate dte-syntax commands in FILE and exit\n"
430 " -b NAME Print built-in config matching NAME and exit\n"
431 " -B Print list of built-in config names and exit\n"
432 " -H Don't load or save history files\n"
433 " -R Don't read user config file\n"
434 " -K Start editor in \"showkey\" mode\n"
435 " -h Display help summary and exit\n"
436 " -V Display version number and exit\n"
437 "\n";
438
439 23 int main(int argc, char *argv[])
440 {
441 23 static const char optstring[] = "hBHKPRVZC:Q:S:b:c:t:r:s:";
442 23 const char *rc = NULL;
443 23 const char *commands[8];
444 23 const char *tags[8];
445 23 size_t nr_commands = 0;
446 23 size_t nr_tags = 0;
447 23 EditorFlags histflags = EFLAG_ALL_HIST;
448 23 bool headless = false;
449 23 bool read_rc = true;
450 23 bool explicit_term_query_level = false;
451 23 unsigned int terminal_query_level = 1;
452
453
2/2
✓ Branch 27 → 3 taken 60 times.
✓ Branch 27 → 28 taken 9 times.
69 for (int ch; (ch = getopt(argc, argv, optstring)) != -1; ) {
454
15/17
✓ Branch 3 → 4 taken 14 times.
✓ Branch 3 → 5 taken 5 times.
✓ Branch 3 → 8 taken 9 times.
✓ Branch 3 → 11 taken 2 times.
✓ Branch 3 → 14 taken 3 times.
✓ Branch 3 → 15 taken 7 times.
✓ Branch 3 → 16 taken 2 times.
✓ Branch 3 → 17 taken 1 time.
✓ Branch 3 → 18 taken 1 time.
✗ Branch 3 → 19 not taken.
✓ Branch 3 → 20 taken 1 time.
✓ Branch 3 → 21 taken 2 times.
✗ Branch 3 → 22 not taken.
✓ Branch 3 → 23 taken 1 time.
✓ Branch 3 → 24 taken 1 time.
✓ Branch 3 → 25 taken 9 times.
✓ Branch 3 → 122 taken 2 times.
60 switch (ch) {
455 14 case 'C':
456 14 headless = true;
457 // Fallthrough
458 19 case 'c':
459
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 7 taken 18 times.
19 if (unlikely(nr_commands >= ARRAYLEN(commands))) {
460 1 return ec_usage_error("too many -c or -C options used");
461 }
462 18 commands[nr_commands++] = optarg;
463 18 break;
464 9 case 't':
465
2/2
✓ Branch 8 → 9 taken 1 time.
✓ Branch 8 → 10 taken 8 times.
9 if (unlikely(nr_tags >= ARRAYLEN(tags))) {
466 1 return ec_usage_error("too many -t options used");
467 }
468 8 tags[nr_tags++] = optarg;
469 8 break;
470 2 case 'Q':
471
2/2
✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 25 taken 1 time.
2 if (!str_to_uint(optarg, &terminal_query_level)) {
472 1 return ec_usage_error("invalid argument for -Q: '%s'", optarg);
473 }
474 explicit_term_query_level = true;
475 break;
476 case 'H': histflags = 0; break;
477 3 case 'r': rc = optarg; break;
478 7 case 'R': read_rc = false; break;
479 2 case 'b': return dump_builtin_config(optarg);
480 1 case 'B': return list_builtin_configs();
481 1 case 'h': return ec_printf_ok(usage, progname(argc, argv, "dte"));
482 case 'K': return showkey_loop(terminal_query_level);
483 1 case 'P': return print_256_color_palette();
484 2 case 's': return lint_syntax(optarg, 0);
485 case 'S': return lint_syntax(optarg, SYN_LINT);
486 1 case 'V': return ec_write_stdout(copyright, sizeof(copyright));
487 1 case 'Z': return ec_printf_ok("features:%s\n", buildvar_string);
488 default: return EC_USAGE_ERROR;
489 }
490 }
491
492 // stdio(3) fds must be handled before calling init_logging(), otherwise
493 // an invocation like e.g. `DTE_LOG=/dev/pts/2 dte 0<&-` could cause the
494 // logging fd to be opened as STDIN_FILENO
495 9 int std_fds[2] = {-1, -1};
496 9 ExitCode r = init_std_fds(std_fds, headless);
497
498 9 bool no_color = !!xgetenv("NO_COLOR"); // https://no-color.org/
499
2/2
✓ Branch 30 → 31 taken 1 time.
✓ Branch 30 → 32 taken 8 times.
9 LogOpenFlags logflags = headless ? LOGOPEN_ALLOW_CTTY : 0;
500 9 logflags |= no_color ? 0 : LOGOPEN_USE_COLOR;
501
2/2
✓ Branch 32 → 33 taken 8 times.
✓ Branch 32 → 122 taken 1 time.
9 r = r ? r : init_logging(logflags);
502
1/2
✓ Branch 34 → 35 taken 8 times.
✗ Branch 34 → 122 not taken.
8 if (unlikely(r)) {
503 return r;
504 }
505
506 8 struct utsname u;
507
1/2
✓ Branch 36 → 37 taken 8 times.
✗ Branch 36 → 43 not taken.
8 if (likely(uname(&u) >= 0)) {
508 8 LOG_INFO("system: %s/%s %s", u.sysname, u.machine, u.release);
509
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 44 taken 8 times.
8 if (
510 !headless
511 && !explicit_term_query_level
512 && strview_has_suffix(strview(u.release), "-WSL2")
513 ) {
514 // There appears to be an issue on WSL2 where the DA1 query
515 // response is interpreted as pasted text and then inserted
516 // into the buffer at startup. For now, we simply disable
517 // querying entirely on that platform, until the problem
518 // can be investigated.
519 LOG_NOTICE("WSL2 detected; disabling all terminal queries");
520 terminal_query_level = 0;
521 }
522 } else {
523 LOG_ERRNO("uname");
524 }
525
526 8 LOG_INFO("dte version: " VERSION);
527 8 LOG_INFO("build vars:%s", buildvar_string);
528
529
1/2
✓ Branch 46 → 47 taken 8 times.
✗ Branch 46 → 48 not taken.
8 if (headless) {
530 8 set_signal_dispositions_headless();
531 } else {
532 set_basic_signal_dispositions();
533 }
534
535 // Note that we don't unset EFLAG_HEADLESS in e->flags until later
536 // (regardless of whether -C was used), because the terminal isn't
537 // properly initialized yet and we don't want commands running via
538 // -c or -C -interacting with it
539 8 EditorState *e = init_editor_state(getenv("HOME"), getenv("DTE_HOME"));
540
541
1/2
✗ Branch 52 → 53 not taken.
✓ Branch 52 → 54 taken 8 times.
8 if (no_color) {
542 // TODO: Also use a minimal color scheme when $NO_COLOR is set, to
543 // eliminate other colors like e.g. the `quit -p` dialog, etc.
544 e->options.syntax = false;
545 LOG_INFO("$NO_COLOR set; using 'set -g syntax false' by default");
546 }
547
548 8 Terminal *term = &e->terminal;
549
1/2
✗ Branch 54 → 55 not taken.
✓ Branch 54 → 58 taken 8 times.
8 if (!headless) {
550 term_init(term, getenv("TERM"), getenv("COLORTERM"));
551 }
552
553 8 e->err.print_to_stderr = true;
554 8 Buffer *std_buffer = init_std_buffer(e, std_fds);
555
2/4
✓ Branch 59 → 60 taken 8 times.
✗ Branch 59 → 61 not taken.
✗ Branch 60 → 61 not taken.
✓ Branch 60 → 62 taken 8 times.
8 bool have_stdout_buffer = std_buffer && std_buffer->stdout_buffer;
556
557 // Create this early (needed if "lock-files" is true)
558 8 const char *cfgdir = e->user_config_dir;
559 8 BUG_ON(!cfgdir);
560
3/4
✓ Branch 65 → 66 taken 7 times.
✓ Branch 65 → 70 taken 1 time.
✗ Branch 66 → 67 not taken.
✓ Branch 66 → 70 taken 7 times.
8 if (mkdir(cfgdir, 0755) != 0 && errno != EEXIST) {
561 error_msg(&e->err, "Error creating %s: %s", cfgdir, strerror(errno));
562 histflags = 0;
563 e->options.lock_files = false;
564 }
565
566 8 e->flags |= histflags;
567 8 exec_rc_files(e, rc, read_rc, no_color);
568 8 read_history_files(e, headless);
569
570 8 e->window = new_window(e);
571 8 e->root_frame = new_root_frame(e->window);
572 8 e->status = EDITOR_RUNNING;
573 8 e->err.print_to_stderr = headless;
574 8 View *dview = open_initial_buffers(e, std_buffer, argv + optind, argc - optind);
575 8 e->err.print_to_stderr = true;
576
577
1/2
✗ Branch 75 → 76 not taken.
✓ Branch 75 → 77 taken 8 times.
8 if (!headless) {
578 // This is done before executing `-c` commands, so that the
579 // `exec` command can set $LINES and $COLUMNS appropriately
580 update_screen_size(term, e->root_frame);
581 }
582
583
2/2
✓ Branch 80 → 78 taken 8 times.
✓ Branch 80 → 81 taken 8 times.
16 for (size_t i = 0; i < nr_commands; i++) {
584 8 handle_normal_command(e, commands[i], false);
585 }
586
587 8 bool fast_exit = e->status != EDITOR_RUNNING;
588
1/2
✓ Branch 81 → 82 taken 8 times.
✗ Branch 81 → 83 not taken.
8 if (headless || fast_exit) {
589 // Headless mode implicitly exits after commands have finished
590 // running. We may also exit here (without drawing the UI,
591 // emitting terminal queries or entering main_loop()) if a `-c`
592 // command has already quit the editor. This prevents commands
593 // like e.g. `dte -HR -cquit` from emitting terminal queries
594 // and exiting before the responses have been read (which would
595 // typically cause the parent shell process to read them
596 // instead).
597 8 goto exit;
598 }
599
600 e->err.print_to_stderr = false;
601 lookup_tags(e, tags, nr_tags, nr_commands ? NULL : dview);
602
603 set_fatal_signal_handlers();
604 set_sigwinch_handler();
605
606 bool init = term_mode_init();
607 if (unlikely(!init || !term_raw())) {
608 e->status = ec_error(init ? "tcsetattr" : "tcgetattr", EC_IO_ERROR);
609 fast_exit = true;
610 goto exit;
611 }
612
613 need_term_reset_on_fatal_error = 1;
614
615 if (e->err.stderr_errors_printed) {
616 // Display "press any key to continue" prompt for stderr errors
617 any_key(term, e->options.esc_timeout);
618 clear_error(&e->err);
619 }
620
621 // Enable main_loop() iteration timing if $DTE_LOG_TIMING is set
622 // and logging is enabled. In theory, this could be controlled by
623 // `TraceLoggingFlags`, but trace logging is only enabled in debug
624 // builds, whereas latency timing is most useful in release builds.
625 bool timing = log_level_enabled(LOG_LEVEL_INFO) && xgetenv("DTE_LOG_TIMING");
626
627 e->flags &= ~EFLAG_HEADLESS; // See comment for init_editor_state() call above
628 main_loop(e, terminal_query_level, timing);
629
630 /*
631 * This is normally followed immediately by term_cooked() in other
632 * contexts, but in this case we want to switch back to cooked mode
633 * as late as possible. On underpowered machines, unlocking files and
634 * writing history may take some time. If cooked mode were switched
635 * to here it'd leave a window of time where we've switched back to
636 * the normal screen buffer and reset the termios ECHO flag while
637 * still being the foreground process, which would cause any input
638 * to be echoed to the terminal (before the shell takes over again
639 * and prints its prompt).
640 */
641 ui_end(&e->terminal, true);
642
643 8 exit:
644 8 e->err.print_to_stderr = true;
645 8 frame_remove(e, e->root_frame); // Unlock files and add to file history
646 8 write_history_files(e);
647 8 int exit_code = MAX(e->status, EDITOR_EXIT_OK);
648
649
1/2
✗ Branch 108 → 109 not taken.
✓ Branch 108 → 111 taken 8 times.
8 if (!headless && !fast_exit) {
650 // This must be done before calling buffer_write_blocks_and_free(),
651 // since output modes need to be restored to get proper line ending
652 // translation and std_fds[STDOUT_FILENO] may be a pipe to the
653 // terminal
654 term_cooked();
655 need_term_reset_on_fatal_error = 0;
656 }
657
658
1/2
✓ Branch 111 → 112 taken 8 times.
✗ Branch 111 → 118 not taken.
8 if (have_stdout_buffer) {
659 8 SystemErrno err = buffer_write_blocks_and_free(std_buffer, std_fds[STDOUT_FILENO]);
660
1/2
✗ Branch 113 → 114 not taken.
✓ Branch 113 → 118 taken 8 times.
8 if (err != 0) {
661 error_msg(&e->err, "failed to write (stdout) buffer: %s", strerror(err));
662 if (exit_code == EDITOR_EXIT_OK) {
663 exit_code = EC_IO_ERROR;
664 }
665 }
666 }
667
668 8 if (DEBUG >= 1 || ASAN_ENABLED || xgetenv(ld_preload_env_var())) {
669 // Only free EditorState in debug builds or when a library/tool is
670 // checking for leaks; otherwise it's pointless to do so immediately
671 // before exiting
672 8 free_editor_state(e);
673 }
674
675 8 LOG_INFO("exiting with status %d", exit_code);
676 8 log_close();
677 8 return exit_code;
678 }
679