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 → 23 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 | ✗ | const char *no_color = xgetenv("NO_COLOR"); // https://no-color.org/ | |
| 267 | ✗ | logflags |= no_color ? 0 : LOGOPEN_USE_COLOR; | |
| 268 | ✗ | LogLevel got_level = log_open(filename, req_level, logflags); | |
| 269 | ✗ | if (got_level == LOG_LEVEL_NONE) { | |
| 270 | ✗ | const char *err = strerror(errno); | |
| 271 | ✗ | fprintf(stderr, "Failed to open $DTE_LOG (%s): %s\n", filename, err); | |
| 272 | ✗ | return EC_IO_ERROR; | |
| 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 (no_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 = xstrcut(path.data, path.length); | |
| 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 → 121 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 |
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); |
| 498 |
3/4✓ Branch 31 → 32 taken 8 times.
✓ Branch 31 → 36 taken 1 time.
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 8 times.
|
9 | r = r ? r : init_logging(headless ? LOGOPEN_ALLOW_CTTY : 0); |
| 499 |
1/2✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 8 times.
|
8 | if (unlikely(r)) { |
| 500 | 1 | return r; | |
| 501 | } | ||
| 502 | |||
| 503 | 8 | struct utsname u; | |
| 504 |
1/2✓ Branch 38 → 39 taken 8 times.
✗ Branch 38 → 44 not taken.
|
8 | if (likely(uname(&u) >= 0)) { |
| 505 | 8 | LOG_INFO("system: %s/%s %s", u.sysname, u.machine, u.release); | |
| 506 |
1/2✗ Branch 40 → 41 not taken.
✓ Branch 40 → 45 taken 8 times.
|
8 | if ( |
| 507 | !headless | ||
| 508 | && !explicit_term_query_level | ||
| 509 | ✗ | && str_has_suffix(u.release, "-WSL2") | |
| 510 | ) { | ||
| 511 | // There appears to be an issue on WSL2 where the DA1 query | ||
| 512 | // response is interpreted as pasted text and then inserted | ||
| 513 | // into the buffer at startup. For now, we simply disable | ||
| 514 | // querying entirely on that platform, until the problem | ||
| 515 | // can be investigated. | ||
| 516 | ✗ | LOG_NOTICE("WSL2 detected; disabling all terminal queries"); | |
| 517 | ✗ | terminal_query_level = 0; | |
| 518 | } | ||
| 519 | } else { | ||
| 520 | ✗ | LOG_ERRNO("uname"); | |
| 521 | } | ||
| 522 | |||
| 523 | 8 | LOG_INFO("dte version: " VERSION); | |
| 524 | 8 | LOG_INFO("build vars:%s", buildvar_string); | |
| 525 | |||
| 526 |
1/2✓ Branch 47 → 48 taken 8 times.
✗ Branch 47 → 49 not taken.
|
8 | if (headless) { |
| 527 | 8 | set_signal_dispositions_headless(); | |
| 528 | } else { | ||
| 529 | ✗ | set_basic_signal_dispositions(); | |
| 530 | } | ||
| 531 | |||
| 532 | // Note that we don't unset EFLAG_HEADLESS in e->flags until later | ||
| 533 | // (regardless of whether -C was used), because the terminal isn't | ||
| 534 | // properly initialized yet and we don't want commands running via | ||
| 535 | // -c or -C -interacting with it | ||
| 536 | 8 | EditorState *e = init_editor_state(getenv("HOME"), getenv("DTE_HOME")); | |
| 537 | |||
| 538 | 8 | Terminal *term = &e->terminal; | |
| 539 |
1/2✗ Branch 53 → 54 not taken.
✓ Branch 53 → 57 taken 8 times.
|
8 | if (!headless) { |
| 540 | ✗ | term_init(term, getenv("TERM"), getenv("COLORTERM")); | |
| 541 | } | ||
| 542 | |||
| 543 | 8 | e->err.print_to_stderr = true; | |
| 544 | 8 | Buffer *std_buffer = init_std_buffer(e, std_fds); | |
| 545 |
2/4✓ Branch 58 → 59 taken 8 times.
✗ Branch 58 → 60 not taken.
✗ Branch 59 → 60 not taken.
✓ Branch 59 → 61 taken 8 times.
|
8 | bool have_stdout_buffer = std_buffer && std_buffer->stdout_buffer; |
| 546 | |||
| 547 | // Create this early (needed if "lock-files" is true) | ||
| 548 | 8 | const char *cfgdir = e->user_config_dir; | |
| 549 | 8 | BUG_ON(!cfgdir); | |
| 550 |
3/4✓ Branch 64 → 65 taken 7 times.
✓ Branch 64 → 69 taken 1 time.
✗ Branch 65 → 66 not taken.
✓ Branch 65 → 69 taken 7 times.
|
8 | if (mkdir(cfgdir, 0755) != 0 && errno != EEXIST) { |
| 551 | ✗ | error_msg(&e->err, "Error creating %s: %s", cfgdir, strerror(errno)); | |
| 552 | ✗ | histflags = 0; | |
| 553 | ✗ | e->options.lock_files = false; | |
| 554 | } | ||
| 555 | |||
| 556 | 8 | e->flags |= histflags; | |
| 557 | 8 | exec_rc_files(e, rc, read_rc); | |
| 558 | 8 | read_history_files(e, headless); | |
| 559 | |||
| 560 | 8 | e->window = new_window(e); | |
| 561 | 8 | e->root_frame = new_root_frame(e->window); | |
| 562 | 8 | e->status = EDITOR_RUNNING; | |
| 563 | 8 | e->err.print_to_stderr = headless; | |
| 564 | 8 | View *dview = open_initial_buffers(e, std_buffer, argv + optind, argc - optind); | |
| 565 | 8 | e->err.print_to_stderr = true; | |
| 566 | |||
| 567 |
1/2✗ Branch 74 → 75 not taken.
✓ Branch 74 → 78 taken 8 times.
|
8 | if (!headless) { |
| 568 | // This is done before executing `-c` commands, so that the | ||
| 569 | // `exec` command can set $LINES and $COLUMNS appropriately | ||
| 570 | ✗ | update_screen_size(term, e->root_frame); | |
| 571 | } | ||
| 572 | |||
| 573 |
2/2✓ Branch 79 → 76 taken 8 times.
✓ Branch 79 → 80 taken 8 times.
|
16 | for (size_t i = 0; i < nr_commands; i++) { |
| 574 | 8 | handle_normal_command(e, commands[i], false); | |
| 575 | } | ||
| 576 | |||
| 577 | 8 | bool fast_exit = e->status != EDITOR_RUNNING; | |
| 578 |
1/2✓ Branch 80 → 81 taken 8 times.
✗ Branch 80 → 82 not taken.
|
8 | if (headless || fast_exit) { |
| 579 | // Headless mode implicitly exits after commands have finished | ||
| 580 | // running. We may also exit here (without drawing the UI, | ||
| 581 | // emitting terminal queries or entering main_loop()) if a `-c` | ||
| 582 | // command has already quit the editor. This prevents commands | ||
| 583 | // like e.g. `dte -HR -cquit` from emitting terminal queries | ||
| 584 | // and exiting before the responses have been read (which would | ||
| 585 | // typically cause the parent shell process to read them | ||
| 586 | // instead). | ||
| 587 | 8 | goto exit; | |
| 588 | } | ||
| 589 | |||
| 590 | ✗ | e->err.print_to_stderr = false; | |
| 591 | ✗ | lookup_tags(e, tags, nr_tags, nr_commands ? NULL : dview); | |
| 592 | |||
| 593 | ✗ | set_fatal_signal_handlers(); | |
| 594 | ✗ | set_sigwinch_handler(); | |
| 595 | |||
| 596 | ✗ | bool init = term_mode_init(); | |
| 597 | ✗ | if (unlikely(!init || !term_raw())) { | |
| 598 | ✗ | e->status = ec_error(init ? "tcsetattr" : "tcgetattr", EC_IO_ERROR); | |
| 599 | ✗ | fast_exit = true; | |
| 600 | ✗ | goto exit; | |
| 601 | } | ||
| 602 | |||
| 603 | ✗ | need_term_reset_on_fatal_error = 1; | |
| 604 | |||
| 605 | ✗ | if (e->err.stderr_errors_printed) { | |
| 606 | // Display "press any key to continue" prompt for stderr errors | ||
| 607 | ✗ | any_key(term, e->options.esc_timeout); | |
| 608 | ✗ | clear_error(&e->err); | |
| 609 | } | ||
| 610 | |||
| 611 | // Enable main_loop() iteration timing if $DTE_LOG_TIMING is set | ||
| 612 | // and logging is enabled. In theory, this could be controlled by | ||
| 613 | // `TraceLoggingFlags`, but trace logging is only enabled in debug | ||
| 614 | // builds, whereas latency timing is most useful in release builds. | ||
| 615 | ✗ | bool timing = log_level_enabled(LOG_LEVEL_INFO) && xgetenv("DTE_LOG_TIMING"); | |
| 616 | |||
| 617 | ✗ | e->flags &= ~EFLAG_HEADLESS; // See comment for init_editor_state() call above | |
| 618 | ✗ | main_loop(e, terminal_query_level, timing); | |
| 619 | |||
| 620 | /* | ||
| 621 | * This is normally followed immediately by term_cooked() in other | ||
| 622 | * contexts, but in this case we want to switch back to cooked mode | ||
| 623 | * as late as possible. On underpowered machines, unlocking files and | ||
| 624 | * writing history may take some time. If cooked mode were switched | ||
| 625 | * to here it'd leave a window of time where we've switched back to | ||
| 626 | * the normal screen buffer and reset the termios ECHO flag while | ||
| 627 | * still being the foreground process, which would cause any input | ||
| 628 | * to be echoed to the terminal (before the shell takes over again | ||
| 629 | * and prints its prompt). | ||
| 630 | */ | ||
| 631 | ✗ | ui_end(&e->terminal, true); | |
| 632 | |||
| 633 | 8 | exit: | |
| 634 | 8 | e->err.print_to_stderr = true; | |
| 635 | 8 | frame_remove(e, e->root_frame); // Unlock files and add to file history | |
| 636 | 8 | write_history_files(e); | |
| 637 | 8 | int exit_code = MAX(e->status, EDITOR_EXIT_OK); | |
| 638 | |||
| 639 |
1/2✗ Branch 107 → 108 not taken.
✓ Branch 107 → 110 taken 8 times.
|
8 | if (!headless && !fast_exit) { |
| 640 | // This must be done before calling buffer_write_blocks_and_free(), | ||
| 641 | // since output modes need to be restored to get proper line ending | ||
| 642 | // translation and std_fds[STDOUT_FILENO] may be a pipe to the | ||
| 643 | // terminal | ||
| 644 | ✗ | term_cooked(); | |
| 645 | ✗ | need_term_reset_on_fatal_error = 0; | |
| 646 | } | ||
| 647 | |||
| 648 |
1/2✓ Branch 110 → 111 taken 8 times.
✗ Branch 110 → 117 not taken.
|
8 | if (have_stdout_buffer) { |
| 649 | 8 | SystemErrno err = buffer_write_blocks_and_free(std_buffer, std_fds[STDOUT_FILENO]); | |
| 650 |
1/2✗ Branch 112 → 113 not taken.
✓ Branch 112 → 117 taken 8 times.
|
8 | if (err != 0) { |
| 651 | ✗ | error_msg(&e->err, "failed to write (stdout) buffer: %s", strerror(err)); | |
| 652 | ✗ | if (exit_code == EDITOR_EXIT_OK) { | |
| 653 | ✗ | exit_code = EC_IO_ERROR; | |
| 654 | } | ||
| 655 | } | ||
| 656 | } | ||
| 657 | |||
| 658 | 8 | if (DEBUG >= 1 || ASAN_ENABLED || xgetenv(ld_preload_env_var())) { | |
| 659 | // Only free EditorState in debug builds or when a library/tool is | ||
| 660 | // checking for leaks; otherwise it's pointless to do so immediately | ||
| 661 | // before exiting | ||
| 662 | 8 | free_editor_state(e); | |
| 663 | } | ||
| 664 | |||
| 665 | 8 | LOG_INFO("exiting with status %d", exit_code); | |
| 666 | 8 | log_close(); | |
| 667 | 8 | return exit_code; | |
| 668 | } | ||
| 669 |