dte test coverage


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