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