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