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