dte test coverage


Directory: ./
File: src/editor.c
Date: 2025-12-11 10:43:49
Coverage Exec Excl Total
Lines: 47.2% 92 1 196
Functions: 41.2% 7 0 17
Branches: 26.0% 13 0 50

Line Branch Exec Source
1 #include <errno.h>
2 #include <langinfo.h>
3 #include <locale.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8 #include "editor.h"
9 #include "bind.h"
10 #include "bookmark.h"
11 #include "compiler.h"
12 #include "encoding.h"
13 #include "file-option.h"
14 #include "filetype.h"
15 #include "lock.h"
16 #include "signals.h"
17 #include "syntax/syntax.h"
18 #include "terminal/color.h"
19 #include "terminal/input.h"
20 #include "terminal/key.h"
21 #include "terminal/output.h"
22 #include "terminal/paste.h"
23 #include "ui.h"
24 #include "util/exitcode.h"
25 #include "util/intern.h"
26 #include "util/intmap.h"
27 #include "util/log.h"
28 #include "util/time-util.h"
29 #include "util/xmalloc.h"
30 #include "util/xsnprintf.h"
31 #include "util/xstdio.h"
32 #include "version.h"
33
34 11 static void set_and_check_locale(void)
35 {
36 11 const char *default_locale = setlocale(LC_CTYPE, "");
37
1/2
✓ Branch 3 → 4 taken 11 times.
✗ Branch 3 → 8 not taken.
11 if (likely(default_locale)) {
38 11 const char *codeset = nl_langinfo(CODESET);
39 11 LOG_INFO("locale: %s (codeset: %s)", default_locale, codeset);
40
1/2
✗ Branch 7 → 11 not taken.
✓ Branch 7 → 19 taken 11 times.
11 if (likely(lookup_encoding(codeset) == UTF8)) {
41 return;
42 }
43 } else {
44 LOG_ERROR("failed to set default locale");
45 }
46
47 static const char fallbacks[][12] = {"C.UTF-8", "en_US.UTF-8"};
48 const char *fallback = NULL;
49 for (size_t i = 0; i < ARRAYLEN(fallbacks) && !fallback; i++) {
50 fallback = setlocale(LC_CTYPE, fallbacks[i]);
51 }
52 if (fallback) {
53 LOG_NOTICE("using fallback locale for LC_CTYPE: %s", fallback);
54 return;
55 }
56
57 LOG_ERROR("no UTF-8 fallback locales found");
58 fputs("setlocale() failed\n", stderr);
59 exit(EC_CONFIG_ERROR);
60 }
61
62 // Get permissions mask for new files
63 11 static mode_t get_umask(void)
64 {
65 11 mode_t old = umask(0); // Get by setting a dummy value
66 11 umask(old); // Restore previous value
67 11 return old;
68 }
69
70 11 static const char *get_user_config_dir(const char *home, const char *dte_home)
71 {
72
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 10 times.
11 if (dte_home) {
73 1 return str_intern(dte_home);
74 }
75
76 // TODO: Use "${XDG_CONFIG_HOME:-$HOME/.config}/dte", either if it
77 // exists or (as a default) if "$HOME/.dte" *doesn't* exist. The
78 // latter case may also require one or more calls to mkdir(3).
79
80 10 char buf[8192];
81 10 return mem_intern(buf, xsnprintf(buf, sizeof buf, "%s/.dte", home));
82 }
83
84 11 EditorState *init_editor_state(const char *home, const char *dte_home)
85 {
86 11 set_and_check_locale();
87
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 11 times.
11 home = home ? home : "";
88 11 EditorState *e = xmalloc(sizeof(*e));
89
90 22 *e = (EditorState) {
91 .status = EDITOR_INITIALIZING,
92 11 .home_dir = strview_intern(home),
93 11 .user_config_dir = get_user_config_dir(home, dte_home),
94 .flags = EFLAG_HEADLESS,
95 11 .regexp_word_tokens = regexp_get_word_boundary_tokens(),
96 .command_history = {
97 .max_entries = 512,
98 },
99 .search_history = {
100 .max_entries = 128,
101 },
102 .terminal = {
103 .obuf = TERM_OUTPUT_INIT,
104 },
105 .cursor_styles = {
106 [CURSOR_MODE_DEFAULT] = {.type = CURSOR_DEFAULT, .color = COLOR_DEFAULT},
107 [CURSOR_MODE_INSERT] = {.type = CURSOR_KEEP, .color = COLOR_KEEP},
108 [CURSOR_MODE_OVERWRITE] = {.type = CURSOR_KEEP, .color = COLOR_KEEP},
109 [CURSOR_MODE_CMDLINE] = {.type = CURSOR_KEEP, .color = COLOR_KEEP},
110 },
111 .options = {
112 .auto_indent = true,
113 .detect_indent = 0,
114 .editorconfig = false,
115 .emulate_tab = false,
116 .expand_tab = false,
117 .file_history = true,
118 .indent_width = 8,
119 .overwrite = false,
120 .save_unmodified = SAVE_FULL,
121 .syntax = true,
122 .tab_width = 8,
123 .text_width = 72,
124 .ws_error = WSE_SPECIAL,
125
126 // Global-only options
127 .case_sensitive_search = CSS_TRUE,
128 .crlf_newlines = false,
129 .display_special = false,
130 .esc_timeout = 100,
131 .filesize_limit = 250,
132 .lock_files = true,
133 .optimize_true_color = true,
134 .scroll_margin = 0,
135 .select_cursor_char = true,
136 .set_window_title = false,
137 .show_line_numbers = false,
138 11 .statusline_left = str_intern(" %f%s%m%s%r%s%M"),
139 11 .statusline_right = str_intern(" %y,%X %u %o %E%s%b%s%n %t %p "),
140 .tab_bar = true,
141 .utf8_bom = false,
142 .window_separator = WINSEP_BAR,
143 .msg_compile = 0,
144 .msg_tag = 0,
145 }
146 };
147
148 11 sanity_check_global_options(&e->err, &e->options);
149
150 11 pid_t pid = getpid();
151 11 bool leader = pid == getsid(0);
152 11 e->session_leader = leader;
153
1/2
✓ Branch 14 → 15 taken 11 times.
✗ Branch 14 → 16 not taken.
22 LOG_INFO("pid: %jd%s", (intmax_t)pid, leader ? " (session leader)" : "");
154
155 11 pid_t pgid = getpgrp();
156
1/2
✓ Branch 18 → 19 taken 11 times.
✗ Branch 18 → 20 not taken.
11 if (pgid != pid) {
157 11 LOG_INFO("pgid: %jd", (intmax_t)pgid);
158 }
159
160 11 mode_t mask = get_umask();
161 11 e->new_file_mode = 0666 & ~mask;
162 11 LOG_INFO("umask: %04o", 0777u & (unsigned int)mask);
163
164 11 init_file_locks_context(&e->locks_ctx, e->user_config_dir, pid);
165
166 // Allow child processes to detect that they're running under dte
167
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 11 times.
11 if (unlikely(setenv("DTE_VERSION", VERSION, 1) != 0)) {
168 // errno is almost certainly ENOMEM, if setenv() failed here
169 fatal_error("setenv", errno);
170 }
171
172 11 const RegexpWordBoundaryTokens *wb = &e->regexp_word_tokens;
173
1/2
✓ Branch 26 → 27 taken 11 times.
✗ Branch 26 → 28 not taken.
11 if (wb->len) {
174 11 LOG_INFO("regex word boundary tokens detected: %s %s", wb->start, wb->end);
175 } else {
176 LOG_WARNING("no regex word boundary tokens detected");
177 }
178
179 11 HashMap *modes = &e->modes;
180 11 e->normal_mode = new_mode(modes, xstrdup("normal"), &normal_commands);
181 11 e->command_mode = new_mode(modes, xstrdup("command"), &cmd_mode_commands);
182 11 e->search_mode = new_mode(modes, xstrdup("search"), &search_mode_commands);
183 11 e->mode = e->normal_mode;
184
185 // Pre-allocate space for key bindings and aliases, so that no
186 // predictable (and thus unnecessary) reallocs are needed when
187 // loading built-in configs
188 11 hashmap_init(&e->aliases, 32, HMAP_NO_FLAGS);
189 11 intmap_init(&e->normal_mode->key_bindings, 150);
190 11 intmap_init(&e->command_mode->key_bindings, 40);
191 11 intmap_init(&e->search_mode->key_bindings, 40);
192 11 hashset_init(&e->required_syntax_files, 0, false);
193 11 hashset_init(&e->required_syntax_builtins, 0, false);
194
195 11 return e;
196 }
197
198 34 static void free_mode_handler(ModeHandler *handler)
199 {
200 34 ptr_array_free_array(&handler->fallthrough_modes);
201 34 free_bindings(&handler->key_bindings);
202 34 free(handler);
203 34 }
204
205 12 void clear_all_messages(EditorState *e)
206 {
207
2/2
✓ Branch 5 → 3 taken 36 times.
✓ Branch 5 → 6 taken 12 times.
48 for (size_t i = 0; i < ARRAYLEN(e->messages); i++) {
208 36 clear_messages(&e->messages[i]);
209 }
210 12 }
211
212 11 void free_editor_state(EditorState *e)
213 {
214 11 size_t n = e->terminal.obuf.count;
215
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 11 times.
11 if (n) {
216 LOG_DEBUG("%zu unflushed bytes in terminal output buffer", n);
217 }
218 11 n = e->terminal.ibuf.len;
219
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 11 times.
11 if (n) {
220 LOG_DEBUG("%zu unprocessed bytes in terminal input buffer", n);
221 }
222
223 11 free(e->clipboard.buf);
224 11 free_file_options(&e->file_options);
225 11 free_filetypes(&e->filetypes);
226 11 free_syntaxes(&e->syntaxes);
227 11 file_history_free(&e->file_history);
228 11 history_free(&e->command_history);
229 11 history_free(&e->search_history);
230 11 search_free_regexp(&e->search);
231 11 clear_all_messages(e);
232 11 cmdline_free(&e->cmdline);
233 11 free_macro(&e->macro);
234 11 tag_file_free(&e->tagfile);
235 11 free_buffers(&e->buffers, &e->err, &e->locks_ctx);
236 11 free_file_locks_context(&e->locks_ctx);
237
238 11 ptr_array_free_cb(&e->bookmarks, FREE_FUNC(file_location_free));
239 11 hashmap_free(&e->compilers, FREE_FUNC(free_compiler));
240 11 hashmap_free(&e->modes, FREE_FUNC(free_mode_handler));
241 11 hashmap_free(&e->styles.other, free);
242 11 hashmap_free(&e->aliases, free);
243 11 hashset_free(&e->required_syntax_files);
244 11 hashset_free(&e->required_syntax_builtins);
245
246 11 free_interned_strings();
247 11 free_interned_regexps();
248 11 free(e);
249 11 }
250
251 static bool buffer_contains_block(const Buffer *buffer, const Block *ref)
252 {
253 const Block *blk;
254 block_for_each(blk, &buffer->blocks) {
255 if (blk == ref) {
256 return true;
257 }
258 }
259 return false;
260 }
261
262 static void sanity_check(const View *view)
263 {
264 if (!DEBUG_ASSERTIONS_ENABLED) {
265 return;
266 }
267 const BlockIter *cursor = &view->cursor;
268 BUG_ON(!buffer_contains_block(view->buffer, cursor->blk));
269 BUG_ON(cursor->offset > cursor->blk->size);
270 }
271
272 void any_key(Terminal *term, unsigned int esc_timeout)
273 {
274 KeyCode key;
275 xfputs("Press any key to continue\r\n", stderr);
276 while ((key = term_read_input(term, esc_timeout)) == KEY_NONE) {
277 ;
278 }
279 bool bracketed_paste = key == KEYCODE_BRACKETED_PASTE;
280 if (bracketed_paste || key == KEYCODE_DETECTED_PASTE) {
281 term_discard_paste(&term->ibuf, bracketed_paste);
282 }
283 }
284
285 NOINLINE
286 void ui_resize(EditorState *e)
287 {
288 BUG_ON(e->flags & EFLAG_HEADLESS);
289 resized = 0;
290 update_screen_size(&e->terminal, e->root_frame);
291
292 const ScreenState dummyval = {.id = 0};
293 e->screen_update |= UPDATE_ALL;
294 update_screen(e, &dummyval);
295 }
296
297 void ui_start(EditorState *e)
298 {
299 BUG_ON(e->flags & EFLAG_HEADLESS);
300 Terminal *term = &e->terminal;
301
302 // Note: the order of these calls is important - Kitty saves/restores
303 // some terminal state when switching buffers, so switching to the
304 // alternate screen buffer needs to happen before modes are enabled
305 term_use_alt_screen_buffer(term);
306 term_enable_private_modes(term);
307
308 term_restore_and_save_title(term);
309 ui_resize(e);
310 }
311
312 // Like ui_start(), but to be called only the first time the UI is started.
313 // Terminal queries are buffered before ui_resize() is called, so that only
314 // a single term_output_flush() is needed (i.e. as opposed to calling
315 // ui_start() + term_put_initial_queries() + term_output_flush()).
316 void ui_first_start(EditorState *e, unsigned int terminal_query_level)
317 {
318 BUG_ON(e->flags & EFLAG_HEADLESS);
319 Terminal *term = &e->terminal;
320
321 // The order of these calls is important; see ui_start()
322 term_use_alt_screen_buffer(term);
323 term_enable_private_modes(term);
324
325 term_save_title(term);
326 term_put_initial_queries(term, terminal_query_level);
327 ui_resize(e);
328 }
329
330 void ui_end(Terminal *term, bool final)
331 {
332 if (final) {
333 term_restore_title(term);
334 } else {
335 term_restore_and_save_title(term);
336 }
337
338 TermOutputBuffer *obuf = &term->obuf;
339 term_clear_screen(obuf);
340 term_move_cursor(obuf, 0, term->height - 1);
341 term_restore_cursor_style(term);
342 term_show_cursor(term);
343 term_restore_private_modes(term);
344 term_use_normal_screen_buffer(term);
345 term_end_sync_update(term);
346 term_output_flush(obuf);
347 }
348
349 static ScreenState get_screen_state(const EditorState *e)
350 {
351 const View *view = e->view;
352 return (ScreenState) {
353 .is_modified = buffer_modified(view->buffer),
354 .set_window_title = e->options.set_window_title,
355 .id = view->buffer->id,
356 .cy = view->cy,
357 .vx = view->vx,
358 .vy = view->vy
359 };
360 }
361
362 static void log_timing_info(const struct timespec *start, bool enabled)
363 {
364 struct timespec end;
365 if (likely(!enabled) || !xgettime(&end)) {
366 return;
367 }
368
369 double ms = timespec_to_fp_milliseconds(timespec_subtract(&end, start));
370 LOG_INFO("main loop time: %.3f ms", ms);
371 }
372
373 void main_loop(EditorState *e, bool timing)
374 {
375 BUG_ON(e->flags & EFLAG_HEADLESS);
376
377 while (e->status == EDITOR_RUNNING) {
378 if (unlikely(resized)) {
379 LOG_INFO("SIGWINCH received");
380 ui_resize(e);
381 }
382
383 KeyCode key = term_read_input(&e->terminal, e->options.esc_timeout);
384 if (unlikely(key == KEY_NONE)) {
385 continue;
386 }
387
388 struct timespec start;
389 timing = unlikely(timing) && xgettime(&start);
390
391 const ScreenState s = get_screen_state(e);
392 clear_error(&e->err);
393 handle_input(e, key);
394 sanity_check(e->view);
395 update_screen(e, &s);
396
397 log_timing_info(&start, timing);
398 }
399
400 BUG_ON(e->status < 0 || e->status > EDITOR_EXIT_MAX);
401 }
402