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