dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 85.1% 417 / 5 / 495
Functions: 91.3% 63 / 0 / 69
Branches: 72.4% 199 / 78 / 353

src/options.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "options.h"
5 #include "buffer.h"
6 #include "command/serialize.h"
7 #include "editor.h"
8 #include "file-option.h"
9 #include "filetype.h"
10 #include "status.h"
11 #include "util/align.h"
12 #include "util/arith.h"
13 #include "util/bsearch.h"
14 #include "util/intern.h"
15 #include "util/numtostr.h"
16 #include "util/str-array.h"
17 #include "util/str-util.h"
18 #include "util/strtonum.h"
19 #include "util/xmalloc.h"
20 #include "util/xstring.h"
21
22 typedef enum {
23 OPT_STR,
24 OPT_UINT,
25 OPT_UINT8,
26 OPT_ENUM,
27 OPT_BOOL,
28 OPT_FLAG,
29 OPT_REGEX,
30 OPT_FILESIZE,
31 } OptionType;
32
33 typedef union {
34 const char *str_val; // OPT_STR, OPT_REGEX
35 unsigned int uint_val; // OPT_UINT, OPT_UINT8, OPT_ENUM, OPT_FLAG
36 uint_least64_t u64_val; // OPT_FILESIZE
37 bool bool_val; // OPT_BOOL
38 } OptionValue;
39
40 typedef union {
41 struct {bool (*validate)(ErrorBuffer *ebuf, const char *value);} str_opt; // OPT_STR (optional)
42 struct {unsigned int min, max;} uint_opt; // OPT_UINT, OPT_UINT8
43 struct {const char *const *values;} enum_opt; // OPT_ENUM, OPT_FLAG, OPT_BOOL
44 } OptionConstraint;
45
46 typedef struct {
47 const char name[22];
48 bool local;
49 bool global;
50 unsigned int offset;
51 OptionType type;
52 OptionConstraint u;
53 void (*on_change)(EditorState *e, bool global); // Optional
54 } OptionDesc;
55
56 #define STR_OPT(_name, OLG, _validate, _on_change) { \
57 OLG \
58 .name = _name, \
59 .type = OPT_STR, \
60 .u = {.str_opt = {.validate = _validate}}, \
61 .on_change = _on_change, \
62 }
63
64 #define UINT_OPT(_name, OLG, _min, _max, _on_change) { \
65 OLG \
66 .name = _name, \
67 .type = OPT_UINT, \
68 .u = {.uint_opt = {.min = _min, .max = _max}}, \
69 .on_change = _on_change, \
70 }
71
72 #define UINT8_OPT(_name, OLG, _min, _max, _on_change) { \
73 OLG \
74 .name = _name, \
75 .type = OPT_UINT8, \
76 .u = {.uint_opt = {.min = _min, .max = _max}}, \
77 .on_change = _on_change, \
78 }
79
80 #define ENUM_OPT(_name, OLG, _values, _on_change) { \
81 OLG \
82 .name = _name, \
83 .type = OPT_ENUM, \
84 .u = {.enum_opt = {.values = _values}}, \
85 .on_change = _on_change, \
86 }
87
88 #define FLAG_OPT(_name, OLG, _values, _on_change) { \
89 OLG \
90 .name = _name, \
91 .type = OPT_FLAG, \
92 .u = {.enum_opt = {.values = _values}}, \
93 .on_change = _on_change, \
94 }
95
96 #define BOOL_OPT(_name, OLG, _on_change) { \
97 OLG \
98 .name = _name, \
99 .type = OPT_BOOL, \
100 .u = {.enum_opt = {.values = bool_enum}}, \
101 .on_change = _on_change, \
102 }
103
104 #define REGEX_OPT(_name, OLG, _on_change) { \
105 OLG \
106 .name = _name, \
107 .type = OPT_REGEX, \
108 .on_change = _on_change, \
109 }
110
111 #define FSIZE_OPT(_name, OLG, _on_change) { \
112 OLG \
113 .name = _name, \
114 .type = OPT_FILESIZE, \
115 .on_change = _on_change, \
116 }
117
118 #define OLG(_offset, _local, _global) \
119 .offset = _offset, \
120 .local = _local, \
121 .global = _global, \
122
123 #define L(member) OLG(offsetof(LocalOptions, member), true, false)
124 #define G(member) OLG(offsetof(GlobalOptions, member), false, true)
125 #define C(member) OLG(offsetof(CommonOptions, member), true, true)
126
127 ✗ static void filetype_changed(EditorState *e, bool global)
128 {
129 ✗ BUG_ON(!e->buffer);
130 ✗ BUG_ON(global);
131 ✗ set_file_options(e, e->buffer);
132 ✗ buffer_update_syntax(e, e->buffer);
133 ✗ }
134
135 3 static void set_window_title_changed(EditorState *e, bool global)
136 {
137 3 BUG_ON(!global);
138 3 e->screen_update |= UPDATE_TERM_TITLE;
139 3 }
140
141 1 static void syntax_changed(EditorState *e, bool global)
142 {
143
2/4
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 5 not taken.
1 if (e->buffer && !global) {
144 1 buffer_update_syntax(e, e->buffer);
145 }
146 1 }
147
148 ✗ static void overwrite_changed(EditorState *e, bool global)
149 {
150 ✗ if (!global) {
151 ✗ e->screen_update |= UPDATE_CURSOR_STYLE;
152 }
153 ✗ }
154
155 ✗ static void window_separator_changed(EditorState *e, bool global)
156 {
157 ✗ BUG_ON(!global);
158 ✗ if (e->root_frame && !e->root_frame->window) {
159 ✗ e->screen_update |= UPDATE_WINDOW_SEPARATORS;
160 }
161 ✗ }
162
163 5 static void redraw_buffer(EditorState *e, bool global)
164 {
165
2/4
✓ Branch 2 → 3 taken 5 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 5 times.
✗ Branch 3 → 5 not taken.
5 if (e->buffer && !global) {
166 5 mark_all_lines_changed(e->buffer);
167 }
168 5 }
169
170 3 static void redraw_screen(EditorState *e, bool global)
171 {
172 3 BUG_ON(!global);
173 3 e->screen_update |= UPDATE_ALL_WINDOWS;
174 3 }
175
176 28 static bool validate_statusline_format(ErrorBuffer *ebuf, const char *value)
177 {
178 28 size_t errpos = statusline_format_find_error(value);
179
2/2
✓ Branch 3 → 4 taken 4 times.
✓ Branch 3 → 7 taken 24 times.
28 if (likely(errpos == 0)) {
180 return true;
181 }
182
183 4 char ch = value[errpos];
184
2/2
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 2 times.
4 if (ch == '\0') {
185 2 return error_msg(ebuf, "Format character expected after '%%'");
186 }
187 2 return error_msg(ebuf, "Invalid format character '%c'", ch);
188 }
189
190 64 static bool validate_filetype(ErrorBuffer *ebuf, const char *value)
191 {
192
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 62 times.
64 if (!is_valid_filetype_name(value)) {
193 2 return error_msg(ebuf, "Invalid filetype name '%s'", value);
194 }
195 return true;
196 }
197
198 90 static OptionValue str_get(void *ptr)
199 {
200 90 const char *const *strp = ptr;
201 90 return (OptionValue){.str_val = *strp};
202 }
203
204 1 static void str_set(void *ptr, OptionValue v)
205 {
206 1 const char **strp = ptr;
207 1 *strp = str_intern(v.str_val);
208 1 }
209
210 9 static bool str_parse(const OptionDesc *d, ErrorBuffer *ebuf, const char *str, OptionValue *v)
211 {
212
3/4
✓ Branch 2 → 3 taken 9 times.
✗ Branch 2 → 6 not taken.
✓ Branch 4 → 5 taken 6 times.
✓ Branch 4 → 6 taken 3 times.
9 bool valid = !d->u.str_opt.validate || d->u.str_opt.validate(ebuf, str);
213 9 v->str_val = valid ? str : NULL;
214 9 return valid;
215 }
216
217 8 static const char *str_string(const OptionDesc* UNUSED_ARG(d), OptionValue v)
218 {
219 8 const char *s = v.str_val;
220
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 6 times.
8 return s ? s : "";
221 }
222
223 65 static OptionValue re_get(void *ptr)
224 {
225 65 const InternedRegexp *const *irp = ptr;
226
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 63 times.
65 return (OptionValue){.str_val = *irp ? (*irp)->str : NULL};
227 }
228
229 2 static void re_set(void *ptr, OptionValue v)
230 {
231 // Note that this function is only ever called if re_parse() has already
232 // validated the pattern
233 2 const InternedRegexp **irp = ptr;
234
1/2
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 4 not taken.
2 *irp = v.str_val ? regexp_intern(NULL, v.str_val) : NULL;
235 2 }
236
237 56 static bool re_parse(const OptionDesc* UNUSED_ARG(d), ErrorBuffer *ebuf, const char *str, OptionValue *v)
238 {
239
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 56 times.
56 if (str[0] == '\0') {
240 ✗ v->str_val = NULL;
241 ✗ return true;
242 }
243
244
3/4
✓ Branch 5 → 6 taken 55 times.
✓ Branch 5 → 9 taken 1 time.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 55 times.
56 bool valid = regexp_is_interned(str) || regexp_is_valid(ebuf, str, REG_NEWLINE);
245 56 v->str_val = valid ? str : NULL;
246 56 return valid;
247 }
248
249 252 static OptionValue uint_get(void *ptr)
250 {
251 252 const unsigned int *valp = ptr;
252 252 return (OptionValue){.uint_val = *valp};
253 }
254
255 3 static void uint_set(void *ptr, OptionValue v)
256 {
257 3 unsigned int *valp = ptr;
258 3 *valp = v.uint_val;
259 3 }
260
261 28 static bool uint_parse(const OptionDesc *d, ErrorBuffer *ebuf, const char *str, OptionValue *v)
262 {
263 28 unsigned int val;
264
2/2
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 26 times.
28 if (!str_to_uint(str, &val)) {
265 2 return error_msg(ebuf, "Integer value for %s expected", d->name);
266 }
267
268 26 const unsigned int min = d->u.uint_opt.min;
269 26 const unsigned int max = d->u.uint_opt.max;
270
4/4
✓ Branch 5 → 6 taken 25 times.
✓ Branch 5 → 7 taken 1 time.
✓ Branch 6 → 7 taken 3 times.
✓ Branch 6 → 8 taken 22 times.
26 if (val < min || val > max) {
271 4 return error_msg(ebuf, "Value for %s must be in %u-%u range", d->name, min, max);
272 }
273
274 22 v->uint_val = val;
275 22 return true;
276 }
277
278 12 static const char *uint_string(const OptionDesc* UNUSED_ARG(desc), OptionValue value)
279 {
280 12 return uint_to_str(value.uint_val);
281 }
282
283 317 static OptionValue uint8_get(void *ptr)
284 {
285 317 const uint8_t *valp = ptr;
286 317 return (OptionValue){.uint_val = (unsigned int)(*valp)};
287 }
288
289 4 static void uint8_set(void *ptr, OptionValue value)
290 {
291 4 uint8_t *valp = ptr;
292 4 *valp = (uint8_t)value.uint_val;
293 4 }
294
295 850 static OptionValue bool_get(void *ptr)
296 {
297 850 const bool *valp = ptr;
298 850 return (OptionValue){.bool_val = *valp};
299 }
300
301 24 static void bool_set(void *ptr, OptionValue v)
302 {
303 24 bool *valp = ptr;
304 24 *valp = v.bool_val;
305 24 }
306
307 115 static bool bool_parse(const OptionDesc *d, ErrorBuffer *ebuf, const char *str, OptionValue *v)
308 {
309 115 bool t = streq(str, "true");
310
4/4
✓ Branch 2 → 3 taken 41 times.
✓ Branch 2 → 4 taken 74 times.
✓ Branch 3 → 4 taken 38 times.
✓ Branch 3 → 5 taken 3 times.
115 if (t || streq(str, "false")) {
311 112 v->bool_val = t;
312 112 return true;
313 }
314 3 return error_msg(ebuf, "Invalid value for %s; expected: true, false", d->name);
315 }
316
317 37 static const char *bool_string(const OptionDesc* UNUSED_ARG(d), OptionValue v)
318 {
319
2/2
✓ Branch 2 → 3 taken 22 times.
✓ Branch 2 → 4 taken 15 times.
37 return v.bool_val ? "true" : "false";
320 }
321
322 12 static bool enum_parse(const OptionDesc *d, ErrorBuffer *ebuf, const char *str, OptionValue *v)
323 {
324 12 const char *const *values = d->u.enum_opt.values;
325 12 unsigned int n;
326
2/2
✓ Branch 6 → 3 taken 32 times.
✓ Branch 6 → 7 taken 12 times.
44 for (n = 0; values[n]; n++) {
327
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 32 times.
32 if (streq(values[n], str)) {
328 ✗ v->uint_val = n;
329 ✗ return true;
330 }
331 }
332
333 12 char expected[64];
334 12 string_array_concat(expected, sizeof(expected), values, n, strview(", "));
335 12 return error_msg(ebuf, "Invalid value for %s; expected: %s", d->name, expected);
336 }
337
338 12 static const char *enum_string(const OptionDesc *desc, OptionValue value)
339 {
340 12 return desc->u.enum_opt.values[value.uint_val];
341 }
342
343 83 static bool flag_parse(const OptionDesc *d, ErrorBuffer *ebuf, const char *str, OptionValue *v)
344 {
345 // "0" is allowed for compatibility and is the same as ""
346
2/2
✓ Branch 2 → 3 taken 9 times.
✓ Branch 2 → 4 taken 74 times.
83 if (streq(str, "0")) {
347 9 v->uint_val = 0;
348 9 return true;
349 }
350
351 74 const char *const *values = d->u.enum_opt.values;
352 74 StringView sv = strview(str);
353 74 unsigned int flags = 0;
354
355
2/2
✓ Branch 17 → 5 taken 464 times.
✓ Branch 17 → 18 taken 70 times.
534 for (size_t pos = 0; pos < sv.length; ) {
356 464 const StringView flag = get_delim(sv, &pos, ',');
357 464 size_t n;
358
2/2
✓ Branch 11 → 7 taken 2114 times.
✓ Branch 11 → 12 taken 4 times.
2582 for (n = 0; values[n]; n++) {
359
2/2
✓ Branch 8 → 9 taken 460 times.
✓ Branch 8 → 10 taken 1654 times.
2114 if (strview_equal_cstring(flag, values[n])) {
360 460 flags |= 1u << n;
361 460 break;
362 }
363 }
364
2/2
✓ Branch 12 → 13 taken 4 times.
✓ Branch 12 → 16 taken 460 times.
464 if (unlikely(!values[n])) {
365 4 char expected[128];
366 4 string_array_concat(expected, sizeof(expected), values, n, strview(", "));
367 4 return error_msg (
368 ebuf,
369 "Invalid flag '%.*s' for %s; expected: %s",
370 4 SV_FMT(flag), d->name, expected
371 );
372 }
373 }
374
375 70 v->uint_val = flags;
376 70 return true;
377 }
378
379 52 static const char *flag_string(const OptionDesc *desc, OptionValue value)
380 {
381 52 const char *const *values = desc->u.enum_opt.values;
382 52 const char *tmp[16];
383 52 const unsigned int flags = value.uint_val;
384 52 size_t nstrs = 0;
385
386 // Collect any `values` strings that correspond to set `flags` into
387 // a temporary array
388
2/2
✓ Branch 8 → 3 taken 416 times.
✓ Branch 8 → 9 taken 52 times.
468 for (size_t i = 0; values[i]; i++) {
389 416 BUG_ON(i >= ARRAYLEN(tmp));
390
2/2
✓ Branch 5 → 6 taken 386 times.
✓ Branch 5 → 7 taken 30 times.
416 if (flags & (1u << i)) {
391 386 tmp[nstrs++] = values[i];
392 }
393 }
394
395 // ...so that string_array_concat() can be used to concatenate them
396 // into a comma-separated list
397 52 static char buf[128];
398 52 string_array_concat(buf, sizeof(buf), tmp, nstrs, strview(","));
399 52 return buf;
400 }
401
402 39 static OptionValue fsize_get(void *ptr)
403 {
404 39 const uint_least64_t *valp = ptr;
405 39 return (OptionValue){.u64_val = *valp};
406 }
407
408 ✗ static void fsize_set(void *ptr, OptionValue v)
409 {
410 ✗ uint_least64_t *valp = ptr;
411 ✗ *valp = v.u64_val;
412 ✗ }
413
414 6 static bool fsize_parse(const OptionDesc* UNUSED_ARG(d), ErrorBuffer *ebuf, const char *str, OptionValue *value)
415 {
416 6 intmax_t bytes = parse_filesize(str);
417
1/2
✓ Branch 3 → 4 taken 6 times.
✗ Branch 3 → 8 not taken.
6 if (bytes < 0 || (uintmax_t)bytes > (uintmax_t)UINT_LEAST64_MAX) {
418 6 int errnum = (bytes < 0) ? -bytes : EOVERFLOW;
419
1/2
✓ Branch 4 → 5 taken 6 times.
✗ Branch 4 → 6 not taken.
6 if (errnum == EINVAL) {
420 // No need to append `strerror(EINVAL)` ("invalid argument") here
421 6 return error_msg(ebuf, "invalid filesize '%s'", str);
422 }
423 ✗ const char *err = strerror(errnum);
424 ✗ return error_msg(ebuf, "invalid filesize '%s': %s", str, err);
425 }
426
427 ✗ value->u64_val = bytes;
428 ✗ return true;
429 }
430
431 6 static const char *fsize_string(const OptionDesc* UNUSED_ARG(d), OptionValue value)
432 {
433 6 static char buf[PRECISE_FILESIZE_STR_MAX];
434 6 return filesize_to_str_precise(value.u64_val, buf);
435 }
436
437 static const struct {
438 OptionValue (*get)(void *ptr);
439 void (*set)(void *ptr, OptionValue value);
440 bool (*parse)(const OptionDesc *desc, ErrorBuffer *ebuf, const char *str, OptionValue *value);
441 const char *(*string)(const OptionDesc *desc, OptionValue value);
442 } option_ops[] = {
443 [OPT_STR] = {str_get, str_set, str_parse, str_string},
444 [OPT_UINT] = {uint_get, uint_set, uint_parse, uint_string},
445 [OPT_UINT8] = {uint8_get, uint8_set, uint_parse, uint_string},
446 [OPT_ENUM] = {uint8_get, uint8_set, enum_parse, enum_string},
447 [OPT_BOOL] = {bool_get, bool_set, bool_parse, bool_string},
448 [OPT_FLAG] = {uint_get, uint_set, flag_parse, flag_string},
449 [OPT_REGEX] = {re_get, re_set, re_parse, str_string},
450 [OPT_FILESIZE] = {fsize_get, fsize_set, fsize_parse, fsize_string},
451 };
452
453 static const char *const bool_enum[] = {"false", "true", NULL};
454 static const char *const msg_enum[] = {"A", "B", "C", NULL};
455 static const char *const newline_enum[] = {"unix", "dos", NULL};
456 static const char *const tristate_enum[] = {"false", "true", "auto", NULL};
457 static const char *const save_unmodified_enum[] = {"none", "touch", "full", NULL};
458 static const char *const window_separator_enum[] = {"blank", "bar", NULL};
459
460 static const char *const detect_indent_values[] = {
461 "1", "2", "3", "4", "5", "6", "7", "8",
462 NULL
463 };
464
465 // Note: this must be kept in sync with WhitespaceErrorFlags
466 static const char *const ws_error_values[] = {
467 "space-indent",
468 "space-align",
469 "tab-indent",
470 "tab-after-indent",
471 "special",
472 "auto-indent",
473 "trailing",
474 "all-trailing",
475 NULL
476 };
477
478 static const OptionDesc option_desc[] = {
479 BOOL_OPT("auto-indent", C(auto_indent), NULL),
480 BOOL_OPT("brace-indent", L(brace_indent), NULL),
481 ENUM_OPT("case-sensitive-search", G(case_sensitive_search), tristate_enum, NULL),
482 BOOL_OPT("center-at-eof", G(center_at_eof), NULL),
483 FLAG_OPT("detect-indent", C(detect_indent), detect_indent_values, NULL),
484 BOOL_OPT("display-special", G(display_special), redraw_screen),
485 BOOL_OPT("editorconfig", C(editorconfig), NULL),
486 BOOL_OPT("emulate-tab", C(emulate_tab), NULL),
487 UINT_OPT("esc-timeout", G(esc_timeout), 0, 2000, NULL),
488 BOOL_OPT("expand-tab", C(expand_tab), redraw_buffer),
489 BOOL_OPT("file-history", C(file_history), NULL),
490 FSIZE_OPT("filesize-limit", G(filesize_limit), NULL),
491 STR_OPT("filetype", L(filetype), validate_filetype, filetype_changed),
492 BOOL_OPT("fsync", C(fsync), NULL),
493 REGEX_OPT("indent-regex", L(indent_regex), NULL),
494 UINT8_OPT("indent-width", C(indent_width), 1, INDENT_WIDTH_MAX, NULL),
495 BOOL_OPT("lock-files", G(lock_files), NULL),
496 ENUM_OPT("msg-compile", G(msg_compile), msg_enum, NULL),
497 ENUM_OPT("msg-tag", G(msg_tag), msg_enum, NULL),
498 ENUM_OPT("newline", G(crlf_newlines), newline_enum, NULL),
499 BOOL_OPT("optimize-true-color", G(optimize_true_color), redraw_screen),
500 BOOL_OPT("overwrite", C(overwrite), overwrite_changed),
501 ENUM_OPT("save-unmodified", C(save_unmodified), save_unmodified_enum, NULL),
502 UINT8_OPT("scroll-margin", G(scroll_margin), 0, 100, redraw_screen),
503 BOOL_OPT("select-cursor-char", G(select_cursor_char), redraw_screen),
504 BOOL_OPT("set-window-title", G(set_window_title), set_window_title_changed),
505 BOOL_OPT("show-line-numbers", G(show_line_numbers), redraw_screen),
506 STR_OPT("statusline-left", G(statusline_left), validate_statusline_format, NULL),
507 STR_OPT("statusline-right", G(statusline_right), validate_statusline_format, NULL),
508 BOOL_OPT("syntax", C(syntax), syntax_changed),
509 FSIZE_OPT("syntax-line-limit", G(syntax_line_limit), NULL),
510 FSIZE_OPT("syntax-size-limit", G(syntax_size_limit), NULL),
511 BOOL_OPT("tab-bar", G(tab_bar), redraw_screen),
512 UINT8_OPT("tab-width", C(tab_width), 1, TAB_WIDTH_MAX, redraw_buffer),
513 UINT_OPT("text-width", C(text_width), 1, TEXT_WIDTH_MAX, NULL),
514 BOOL_OPT("utf8-bom", G(utf8_bom), NULL),
515 ENUM_OPT("window-separator", G(window_separator), window_separator_enum, window_separator_changed),
516 FLAG_OPT("ws-error", C(ws_error), ws_error_values, redraw_buffer),
517 };
518
519 473 static char *local_ptr(const OptionDesc *desc, LocalOptions *opt)
520 {
521 473 BUG_ON(!desc->local);
522 473 return (char*)opt + desc->offset;
523 }
524
525 923 static char *global_ptr(const OptionDesc *desc, GlobalOptions *opt)
526 {
527 923 BUG_ON(!desc->global);
528 923 return (char*)opt + desc->offset;
529 }
530
531 19 static char *get_option_ptr(EditorState *e, const OptionDesc *d, bool global)
532 {
533
2/2
✓ Branch 2 → 3 taken 10 times.
✓ Branch 2 → 4 taken 9 times.
19 return global ? global_ptr(d, &e->options) : local_ptr(d, &e->buffer->options);
534 }
535
536 463 static size_t count_enum_values(const OptionDesc *desc)
537 {
538 463 OptionType type = desc->type;
539 463 BUG_ON(type != OPT_ENUM && type != OPT_FLAG && type != OPT_BOOL);
540 463 const char *const *values = desc->u.enum_opt.values;
541 463 BUG_ON(!values);
542 463 return string_array_length((char**)values);
543 }
544
545 24 UNITTEST {
546 24 static const struct {
547 size_t alignment;
548 size_t size;
549 } map[] = {
550 [OPT_STR] = {ALIGNOF(const char*), sizeof(const char*)},
551 [OPT_UINT] = {ALIGNOF(unsigned int), sizeof(unsigned int)},
552 [OPT_UINT8] = {ALIGNOF(uint8_t), sizeof(uint8_t)},
553 [OPT_ENUM] = {ALIGNOF(uint8_t), sizeof(uint8_t)},
554 [OPT_BOOL] = {ALIGNOF(bool), sizeof(bool)},
555 [OPT_FLAG] = {ALIGNOF(unsigned int), sizeof(unsigned int)},
556 [OPT_REGEX] = {ALIGNOF(const InternedRegexp*), sizeof(const InternedRegexp*)},
557 [OPT_FILESIZE] = {ALIGNOF(uint_least64_t), sizeof(uint_least64_t)},
558 };
559
560 24 static_assert(ARRAYLEN(map) == ARRAYLEN(option_ops));
561 24 static_assert(offsetof(CommonOptions, syntax) == offsetof(GlobalOptions, syntax));
562 24 static_assert(offsetof(CommonOptions, syntax) == offsetof(LocalOptions, syntax));
563 24 GlobalOptions gopts = {.tab_bar = true};
564 24 LocalOptions lopts = {.filetype = NULL};
565
566
2/2
✓ Branch 48 → 3 taken 912 times.
✓ Branch 48 → 49 taken 24 times.
936 for (size_t i = 0; i < ARRAYLEN(option_desc); i++) {
567 912 const OptionDesc *desc = &option_desc[i];
568 912 const OptionType type = desc->type;
569 912 BUG_ON(type >= ARRAYLEN(map));
570 912 size_t alignment = map[type].alignment;
571 912 size_t end = desc->offset + map[type].size;
572
573
2/2
✓ Branch 5 → 6 taken 840 times.
✓ Branch 5 → 11 taken 72 times.
912 if (desc->global) {
574 840 const char *ptr = global_ptr(desc, &gopts);
575 840 uintptr_t ptr_val = (uintptr_t)ptr;
576 840 BUG_ON(ptr_val % alignment != 0);
577 840 BUG_ON(end > sizeof(GlobalOptions));
578 }
579
580
2/2
✓ Branch 11 → 12 taken 408 times.
✓ Branch 11 → 17 taken 504 times.
912 if (desc->local) {
581 408 const char *ptr = local_ptr(desc, &lopts);
582 408 uintptr_t ptr_val = (uintptr_t)ptr;
583 408 BUG_ON(ptr_val % alignment != 0);
584 408 BUG_ON(end > sizeof(LocalOptions));
585 }
586
587
4/4
✓ Branch 17 → 18 taken 840 times.
✓ Branch 17 → 21 taken 72 times.
✓ Branch 18 → 19 taken 336 times.
✓ Branch 18 → 21 taken 504 times.
912 if (desc->global && desc->local) {
588 336 BUG_ON(end > sizeof(CommonOptions));
589 }
590
591
6/6
✓ Branch 21 → 22 taken 72 times.
✓ Branch 21 → 24 taken 48 times.
✓ Branch 21 → 26 taken 432 times.
✓ Branch 21 → 28 taken 144 times.
✓ Branch 21 → 31 taken 48 times.
✓ Branch 21 → 47 taken 168 times.
912 switch (type) {
592 72 case OPT_UINT8:
593 72 BUG_ON(desc->u.uint_opt.max > UINT8_MAX);
594 // Fallthrough
595 case OPT_UINT:
596 120 BUG_ON(desc->u.uint_opt.min >= desc->u.uint_opt.max);
597 break;
598 432 case OPT_BOOL:
599 432 BUG_ON(desc->u.enum_opt.values != bool_enum);
600 break;
601 144 case OPT_ENUM:
602 144 BUG_ON(count_enum_values(desc) < 2); // NOLINT(*-assert-side-effect)
603 break;
604 48 case OPT_FLAG: {
605 48 size_t nvals = count_enum_values(desc);
606 48 OptionValue val = {.uint_val = -1};
607 48 BUG_ON(nvals < 2);
608 48 BUG_ON(nvals >= BITSIZE(val.uint_val));
609 48 const char *str = flag_string(desc, val);
610 48 BUG_ON(!str);
611 48 BUG_ON(str[0] == '\0');
612
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 48 times.
48 if (!flag_parse(desc, NULL, str, &val)) {
613 − BUG("flag_parse() failed for string: %s", str);
614 }
615 48 unsigned int mask = (1u << nvals) - 1;
616
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 48 times.
48 if (val.uint_val != mask) {
617 − BUG("values not equal: %u, %u", val.uint_val, mask);
618 }
619 48 } break;
620 case OPT_REGEX:
621 case OPT_STR:
622 case OPT_FILESIZE:
623 break;
624 }
625 }
626
627 // Ensure option_desc[] is properly sorted
628 24 CHECK_BSEARCH_ARRAY(option_desc, name);
629 24 }
630
631 1613 static OptionValue desc_get(const OptionDesc *desc, void *ptr)
632 {
633 1613 return option_ops[desc->type].get(ptr);
634 }
635
636 68 static bool desc_equals(const OptionDesc *desc, void *ptr, OptionValue ref)
637 {
638 68 OptionValue current = desc_get(desc, ptr);
639
3/5
✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 5 taken 46 times.
✓ Branch 3 → 6 taken 19 times.
✗ Branch 3 → 7 not taken.
✗ Branch 3 → 8 not taken.
68 switch (desc->type) {
640 3 case OPT_STR:
641 case OPT_REGEX:
642 3 return xstreq(current.str_val, ref.str_val);
643 46 case OPT_BOOL:
644 46 return current.bool_val == ref.bool_val;
645 19 case OPT_UINT:
646 case OPT_UINT8:
647 case OPT_ENUM:
648 case OPT_FLAG:
649 19 return current.uint_val == ref.uint_val;
650 ✗ case OPT_FILESIZE:
651 ✗ return current.u64_val == ref.u64_val;
652 }
653
654 − BUG("unhandled option type");
655 }
656
657 40 static void desc_set(EditorState *e, const OptionDesc *desc, void *ptr, bool global, OptionValue value)
658 {
659
2/2
✓ Branch 3 → 4 taken 34 times.
✓ Branch 3 → 7 taken 6 times.
40 if (desc_equals(desc, ptr, value)) {
660 return;
661 }
662
663 34 option_ops[desc->type].set(ptr, value);
664
2/2
✓ Branch 5 → 6 taken 12 times.
✓ Branch 5 → 7 taken 22 times.
34 if (desc->on_change) {
665 12 desc->on_change(e, global);
666 }
667 }
668
669 261 static bool desc_parse(const OptionDesc *desc, ErrorBuffer *ebuf, const char *str, OptionValue *value)
670 {
671 261 return option_ops[desc->type].parse(desc, ebuf, str, value);
672 }
673
674 79 static const char *desc_string(const OptionDesc *desc, OptionValue value)
675 {
676 79 return option_ops[desc->type].string(desc, value);
677 }
678
679 17790 static int option_cmp(const void *key, const void *elem)
680 {
681 17790 const char *name = key;
682 17790 const OptionDesc *desc = elem;
683 17790 return strcmp(name, desc->name);
684 }
685
686 3489 static const OptionDesc *find_option(const char *name)
687 {
688 3489 return BSEARCH(name, option_desc, option_cmp);
689 }
690
691 3192 bool is_option(const char *name)
692 {
693 3192 return !!find_option(name);
694 }
695
696 287 static const OptionDesc *must_find_option(ErrorBuffer *ebuf, const char *name)
697 {
698 287 const OptionDesc *desc = find_option(name);
699
2/2
✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 5 taken 284 times.
287 if (!desc) {
700 3 error_msg(ebuf, "No such option %s", name);
701 }
702 287 return desc;
703 }
704
705 ✗ static const OptionDesc *must_find_global_option(ErrorBuffer *ebuf, const char *name)
706 {
707 ✗ const OptionDesc *desc = must_find_option(ebuf, name);
708 ✗ if (desc && !desc->global) {
709 ✗ error_msg(ebuf, "Option %s is not global", name);
710 ✗ return NULL;
711 }
712 return desc;
713 }
714
715 61 static bool do_set_option (
716 EditorState *e,
717 const OptionDesc *desc,
718 const char *value,
719 bool local,
720 bool global,
721 bool quiet
722 ) {
723
2/2
✓ Branch 2 → 3 taken 45 times.
✓ Branch 2 → 4 taken 16 times.
61 ErrorBuffer *ebuf = quiet ? NULL : &e->err;
724
6/6
✓ Branch 4 → 5 taken 24 times.
✓ Branch 4 → 8 taken 37 times.
✓ Branch 5 → 6 taken 3 times.
✓ Branch 5 → 8 taken 21 times.
✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 8 taken 1 time.
61 if (local && !desc->local && !quiet) {
725 2 return error_msg(ebuf, "Option %s is not local", desc->name);
726 }
727
5/6
✓ Branch 8 → 9 taken 2 times.
✓ Branch 8 → 12 taken 57 times.
✓ Branch 9 → 10 taken 2 times.
✗ Branch 9 → 12 not taken.
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 12 taken 1 time.
59 if (global && !desc->global && !quiet) {
728 1 return error_msg(ebuf, "Option %s is not global", desc->name);
729 }
730
731 58 OptionValue val;
732
2/2
✓ Branch 13 → 14 taken 26 times.
✓ Branch 13 → 27 taken 32 times.
58 if (!desc_parse(desc, ebuf, value, &val)) {
733 return quiet;
734 }
735
736 26 BUG_ON(!desc->local && !desc->global);
737
738
2/2
✓ Branch 17 → 18 taken 3 times.
✓ Branch 17 → 19 taken 23 times.
26 if (!local && !global) {
739 // Set both by default
740 3 local = desc->local;
741 3 global = desc->global;
742 }
743
744
4/4
✓ Branch 19 → 20 taken 23 times.
✓ Branch 19 → 23 taken 3 times.
✓ Branch 20 → 21 taken 22 times.
✓ Branch 20 → 23 taken 1 time.
26 if (local && desc->local) {
745 22 desc_set(e, desc, local_ptr(desc, &e->buffer->options), false, val);
746 }
747
4/4
✓ Branch 23 → 24 taken 4 times.
✓ Branch 23 → 27 taken 22 times.
✓ Branch 24 → 25 taken 3 times.
✓ Branch 24 → 27 taken 1 time.
26 if (global && desc->global) {
748 3 desc_set(e, desc, global_ptr(desc, &e->options), true, val);
749 }
750
751 return true;
752 }
753
754 63 bool set_option(EditorState *e, const char *name, const char *value, bool local, bool global, bool quiet)
755 {
756
2/2
✓ Branch 2 → 3 taken 46 times.
✓ Branch 2 → 4 taken 17 times.
63 ErrorBuffer *ebuf = quiet ? NULL : &e->err;
757 63 const OptionDesc *desc = must_find_option(ebuf, name);
758
2/2
✓ Branch 5 → 6 taken 61 times.
✓ Branch 5 → 7 taken 2 times.
63 if (!desc) {
759 return quiet;
760 }
761
762 61 return do_set_option(e, desc, value, local, global, quiet);
763 }
764
765 2 bool set_bool_option(EditorState *e, const char *name, bool local, bool global, bool quiet)
766 {
767
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 1 time.
2 ErrorBuffer *ebuf = quiet ? NULL : &e->err;
768 2 const OptionDesc *desc = must_find_option(ebuf, name);
769
1/2
✓ Branch 5 → 6 taken 2 times.
✗ Branch 5 → 13 not taken.
2 if (!desc) {
770 return quiet;
771 }
772
773
1/2
✓ Branch 6 → 7 taken 2 times.
✗ Branch 6 → 12 not taken.
2 if (desc->type != OPT_BOOL) {
774
3/4
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 11 taken 1 time.
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 11 not taken.
3 return quiet || error_msg(ebuf, "Option %s is not boolean", desc->name);
775 }
776
777 ✗ return do_set_option(e, desc, "true", local, global, quiet);
778 }
779
780 16 static const OptionDesc *find_toggle_option(ErrorBuffer *ebuf, const char *name, bool *global)
781 {
782
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 16 times.
16 if (*global) {
783 ✗ return must_find_global_option(ebuf, name);
784 }
785
786 // Toggle local value by default if option has both values
787 16 const OptionDesc *desc = must_find_option(ebuf, name);
788
3/4
✓ Branch 5 → 6 taken 16 times.
✗ Branch 5 → 8 not taken.
✓ Branch 6 → 7 taken 7 times.
✓ Branch 6 → 8 taken 9 times.
16 if (desc && !desc->local) {
789 7 *global = true;
790 }
791 return desc;
792 }
793
794 16 bool toggle_option(EditorState *e, const char *name, bool global, bool verbose)
795 {
796 16 const OptionDesc *desc = find_toggle_option(&e->err, name, &global);
797
1/2
✓ Branch 3 → 4 taken 16 times.
✗ Branch 3 → 20 not taken.
16 if (!desc) {
798 return false;
799 }
800
801 16 char *ptr = get_option_ptr(e, desc, global);
802 16 OptionValue value = desc_get(desc, ptr);
803 16 OptionType type = desc->type;
804
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 10 taken 16 times.
16 if (type == OPT_ENUM) {
805 ✗ if (desc->u.enum_opt.values[value.uint_val + 1]) {
806 ✗ value.uint_val++;
807 } else {
808 ✗ value.uint_val = 0;
809 }
810
2/2
✓ Branch 10 → 11 taken 15 times.
✓ Branch 10 → 12 taken 1 time.
16 } else if (type == OPT_BOOL) {
811 15 value.bool_val = !value.bool_val;
812 } else {
813 1 return error_msg(&e->err, "Toggling %s requires arguments", name);
814 }
815
816 15 desc_set(e, desc, ptr, global, value);
817
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 20 taken 15 times.
15 if (!verbose) {
818 return true;
819 }
820
821 ✗ const char *prefix = (global && desc->local) ? "[global] " : "";
822 ✗ const char *str = desc_string(desc, value);
823 ✗ return info_msg(&e->err, "%s%s = %s", prefix, desc->name, str);
824 }
825
826 ✗ bool toggle_option_values (
827 EditorState *e,
828 const char *name,
829 bool global,
830 bool verbose,
831 char **values,
832 size_t count
833 ) {
834 ✗ const OptionDesc *desc = find_toggle_option(&e->err, name, &global);
835 ✗ if (!desc) {
836 return false;
837 }
838
839 ✗ BUG_ON(count == 0);
840 ✗ size_t current = 0;
841 ✗ bool error = false;
842 ✗ char *ptr = get_option_ptr(e, desc, global);
843 ✗ OptionValue *parsed_values = xmallocarray(count, sizeof(*parsed_values));
844
845 ✗ for (size_t i = 0; i < count; i++) {
846 ✗ if (desc_parse(desc, &e->err, values[i], &parsed_values[i])) {
847 ✗ if (desc_equals(desc, ptr, parsed_values[i])) {
848 ✗ current = i;
849 }
850 } else {
851 error = true;
852 }
853 }
854
855 ✗ if (!error) {
856 ✗ size_t i = wrapping_increment(current, count);
857 ✗ desc_set(e, desc, ptr, global, parsed_values[i]);
858 ✗ if (verbose) {
859 ✗ const char *prefix = (global && desc->local) ? "[global] " : "";
860 ✗ const char *str = desc_string(desc, parsed_values[i]);
861 ✗ info_msg(&e->err, "%s%s = %s", prefix, desc->name, str);
862 }
863 }
864
865 ✗ free(parsed_values);
866 ✗ return !error;
867 }
868
869 143 bool validate_local_options(ErrorBuffer *ebuf, char **strs)
870 {
871 143 size_t invalid = 0;
872
2/2
✓ Branch 17 → 3 taken 206 times.
✓ Branch 17 → 18 taken 143 times.
349 for (size_t i = 0; strs[i]; i += 2) {
873 206 const char *name = strs[i];
874 206 const char *value = strs[i + 1];
875 206 const OptionDesc *desc = must_find_option(ebuf, name);
876
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 205 times.
206 if (unlikely(!desc)) {
877 1 invalid++;
878
2/2
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 9 taken 204 times.
205 } else if (unlikely(!desc->local)) {
879 1 error_msg(ebuf, "%s is not local", name);
880 1 invalid++;
881
2/2
✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 12 taken 203 times.
204 } else if (unlikely(desc->on_change == filetype_changed)) {
882 1 error_msg(ebuf, "filetype cannot be set via option command");
883 1 invalid++;
884 } else {
885 203 OptionValue val;
886
2/2
✓ Branch 13 → 14 taken 5 times.
✓ Branch 13 → 15 taken 198 times.
203 if (unlikely(!desc_parse(desc, ebuf, value, &val))) {
887 5 invalid++;
888 }
889 }
890 }
891 143 return !invalid;
892 }
893
894 #if DEBUG_ASSERTIONS_ENABLED
895 1422 static void sanity_check_option_value(const OptionDesc *desc, OptionValue val)
896 {
897 // NOLINTBEGIN(bugprone-assert-side-effect)
898
6/7
✓ Branch 2 → 3 taken 83 times.
✓ Branch 2 → 12 taken 238 times.
✓ Branch 2 → 16 taken 127 times.
✓ Branch 2 → 19 taken 144 times.
✓ Branch 2 → 24 taken 61 times.
✗ Branch 2 → 30 not taken.
✓ Branch 2 → 31 taken 769 times.
1422 switch (desc->type) {
899 83 case OPT_STR:
900 83 BUG_ON(!val.str_val);
901 83 BUG_ON(val.str_val != str_intern(val.str_val));
902
1/2
✓ Branch 8 → 9 taken 83 times.
✗ Branch 8 → 31 not taken.
83 if (desc->u.str_opt.validate) {
903 83 BUG_ON(!desc->u.str_opt.validate(NULL, val.str_val));
904 }
905 return;
906 238 case OPT_UINT:
907 case OPT_UINT8:
908 238 BUG_ON(val.uint_val < desc->u.uint_opt.min);
909 238 BUG_ON(val.uint_val > desc->u.uint_opt.max);
910 return;
911 127 case OPT_ENUM:
912 127 BUG_ON(val.uint_val >= count_enum_values(desc));
913 return;
914 144 case OPT_FLAG: {
915 144 size_t nvals = count_enum_values(desc);
916 144 BUG_ON(nvals >= 32);
917 144 unsigned int mask = (1u << nvals) - 1;
918 144 unsigned int uint_val = val.uint_val;
919 144 BUG_ON((uint_val & mask) != uint_val);
920 }
921 return;
922 61 case OPT_REGEX:
923
2/2
✓ Branch 24 → 25 taken 2 times.
✓ Branch 24 → 31 taken 59 times.
61 if (val.str_val) {
924 2 const InternedRegexp *ir = regexp_intern(NULL, val.str_val);
925 2 BUG_ON(!ir);
926 2 BUG_ON(ir->str != val.str_val);
927 }
928 return;
929 case OPT_BOOL:
930 case OPT_FILESIZE:
931 return;
932 }
933 // NOLINTEND(bugprone-assert-side-effect)
934
935 − BUG("unhandled option type");
936 }
937
938 72 static void sanity_check_options(const void *opts, bool global)
939 {
940
2/2
✓ Branch 12 → 3 taken 2736 times.
✓ Branch 12 → 13 taken 72 times.
2808 for (size_t i = 0; i < ARRAYLEN(option_desc); i++) {
941 2736 const OptionDesc *desc = &option_desc[i];
942 2736 BUG_ON(desc->type >= ARRAYLEN(option_ops));
943
6/6
✓ Branch 5 → 6 taken 2520 times.
✓ Branch 5 → 7 taken 216 times.
✓ Branch 6 → 7 taken 1512 times.
✓ Branch 6 → 8 taken 1008 times.
✓ Branch 7 → 8 taken 414 times.
✓ Branch 7 → 11 taken 1314 times.
2736 if ((desc->global && desc->local) || global == desc->global) {
944 1422 OptionValue val = desc_get(desc, (char*)opts + desc->offset);
945 1422 sanity_check_option_value(desc, val);
946 }
947 }
948 72 }
949
950 11 void sanity_check_global_options(const GlobalOptions *gopts)
951 {
952 11 sanity_check_options(gopts, true);
953 11 }
954
955 61 void sanity_check_local_options(const LocalOptions *lopts)
956 {
957 61 sanity_check_options(lopts, false);
958 61 }
959 #endif
960
961 2 void collect_options(PointerArray *a, StringView prefix, bool local, bool global)
962 {
963
2/2
✓ Branch 12 → 3 taken 76 times.
✓ Branch 12 → 13 taken 2 times.
78 for (size_t i = 0; i < ARRAYLEN(option_desc); i++) {
964 76 const OptionDesc *desc = &option_desc[i];
965
2/8
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 76 times.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 7 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 76 times.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
76 if ((local && !desc->local) || (global && !desc->global)) {
966 ✗ continue;
967 }
968
2/2
✓ Branch 8 → 9 taken 39 times.
✓ Branch 8 → 11 taken 37 times.
76 if (str_has_sv_prefix(desc->name, prefix)) {
969 39 ptr_array_append(a, xstrdup(desc->name));
970 }
971 }
972 2 }
973
974 // Collect options that can be set via the "option" command
975 1 void collect_auto_options(PointerArray *a, StringView prefix)
976 {
977
2/2
✓ Branch 10 → 3 taken 38 times.
✓ Branch 10 → 11 taken 1 time.
39 for (size_t i = 0; i < ARRAYLEN(option_desc); i++) {
978 38 const OptionDesc *desc = &option_desc[i];
979
4/4
✓ Branch 3 → 4 taken 17 times.
✓ Branch 3 → 5 taken 21 times.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 16 times.
38 if (!desc->local || desc->on_change == filetype_changed) {
980 22 continue;
981 }
982
1/2
✓ Branch 6 → 7 taken 16 times.
✗ Branch 6 → 9 not taken.
16 if (str_has_sv_prefix(desc->name, prefix)) {
983 16 ptr_array_append(a, xstrdup(desc->name));
984 }
985 }
986 1 }
987
988 2 void collect_toggleable_options(PointerArray *a, StringView prefix, bool global)
989 {
990
2/2
✓ Branch 11 → 3 taken 76 times.
✓ Branch 11 → 12 taken 2 times.
78 for (size_t i = 0; i < ARRAYLEN(option_desc); i++) {
991 76 const OptionDesc *desc = &option_desc[i];
992
1/4
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 76 times.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
76 if (global && !desc->global) {
993 ✗ continue;
994 }
995 76 OptionType type = desc->type;
996 76 bool toggleable = (type == OPT_ENUM || type == OPT_BOOL);
997
4/4
✓ Branch 6 → 7 taken 48 times.
✓ Branch 6 → 10 taken 28 times.
✓ Branch 7 → 8 taken 25 times.
✓ Branch 7 → 10 taken 23 times.
76 if (toggleable && str_has_sv_prefix(desc->name, prefix)) {
998 25 ptr_array_append(a, xstrdup(desc->name));
999 }
1000 }
1001 2 }
1002
1003 4 static void collect_enum_option_values (
1004 const char *const *values,
1005 PointerArray *a,
1006 StringView prefix
1007 ) {
1008
2/2
✓ Branch 7 → 3 taken 10 times.
✓ Branch 7 → 8 taken 4 times.
14 for (size_t i = 0; values[i]; i++) {
1009
2/2
✓ Branch 3 → 4 taken 7 times.
✓ Branch 3 → 6 taken 3 times.
10 if (str_has_sv_prefix(values[i], prefix)) {
1010 7 ptr_array_append(a, xstrdup(values[i]));
1011 }
1012 }
1013 4 }
1014
1015 2 static void collect_flag_option_values (
1016 const char *const *values,
1017 PointerArray *a,
1018 StringView prefix
1019 ) {
1020 2 ssize_t comma_idx = strview_memrchr_idx(prefix, ',');
1021 2 size_t tail_idx = (comma_idx < 0) ? 0 : comma_idx + 1;
1022 2 StringView tail = strview_suffix(prefix, tail_idx);
1023
1024
2/2
✓ Branch 9 → 4 taken 16 times.
✓ Branch 9 → 10 taken 2 times.
20 for (size_t i = 0; values[i]; i++) {
1025 16 StringView val = strview(values[i]);
1026
2/2
✓ Branch 5 → 6 taken 3 times.
✓ Branch 5 → 8 taken 13 times.
16 if (strview_has_sv_prefix(val, tail)) {
1027 3 ptr_array_append(a, xmemjoin(prefix.data, tail_idx, val.data, val.length + 1));
1028 }
1029 }
1030 2 }
1031
1032 6 void collect_option_values (
1033 EditorState *e,
1034 PointerArray *a,
1035 const char *option,
1036 StringView prefix
1037 ) {
1038 6 const OptionDesc *desc = find_option(option);
1039
1/2
✓ Branch 3 → 4 taken 6 times.
✗ Branch 3 → 12 not taken.
6 if (!desc) {
1040 return;
1041 }
1042
1043
2/5
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 7 taken 4 times.
✓ Branch 4 → 8 taken 2 times.
✗ Branch 4 → 10 not taken.
✗ Branch 4 → 12 not taken.
6 switch (desc->type) {
1044 case OPT_UINT:
1045 case OPT_UINT8:
1046 case OPT_REGEX:
1047 case OPT_FILESIZE:
1048 // Not enumerated; nothing to collect
1049 return;
1050 ✗ case OPT_STR:
1051 ✗ if (desc->on_change == filetype_changed) {
1052 ✗ collect_ft(&e->filetypes, a, prefix);
1053 }
1054 return;
1055 4 case OPT_ENUM:
1056 case OPT_BOOL:
1057 4 collect_enum_option_values(desc->u.enum_opt.values, a, prefix);
1058 4 return;
1059 2 case OPT_FLAG:
1060 2 collect_flag_option_values(desc->u.enum_opt.values, a, prefix);
1061 2 return;
1062 }
1063
1064 − BUG("unhandled OptionType value");
1065 }
1066
1067 76 static void append_option(String *s, const OptionDesc *desc, void *ptr)
1068 {
1069 76 const OptionValue value = desc_get(desc, ptr);
1070 76 const char *value_str = desc_string(desc, value);
1071
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 76 times.
76 if (unlikely(value_str[0] == '-')) {
1072 ✗ string_append_literal(s, "-- ");
1073 }
1074 76 string_append_cstring(s, desc->name);
1075 76 string_append_byte(s, ' ');
1076 76 string_append_escaped_arg(s, value_str, true);
1077 76 string_append_byte(s, '\n');
1078 76 }
1079
1080 2 String dump_options(GlobalOptions *gopts, LocalOptions *lopts)
1081 {
1082 2 String buf = string_new(4096);
1083
2/2
✓ Branch 24 → 4 taken 76 times.
✓ Branch 24 → 25 taken 2 times.
80 for (size_t i = 0; i < ARRAYLEN(option_desc); i++) {
1084 76 const OptionDesc *desc = &option_desc[i];
1085
2/2
✓ Branch 4 → 5 taken 34 times.
✓ Branch 4 → 6 taken 42 times.
76 void *local = desc->local ? local_ptr(desc, lopts) : NULL;
1086
2/2
✓ Branch 6 → 7 taken 70 times.
✓ Branch 6 → 8 taken 6 times.
76 void *global = desc->global ? global_ptr(desc, gopts) : NULL;
1087
2/2
✓ Branch 8 → 9 taken 28 times.
✓ Branch 8 → 19 taken 48 times.
76 if (local && global) {
1088 28 const OptionValue global_value = desc_get(desc, global);
1089
1/2
✓ Branch 11 → 12 taken 28 times.
✗ Branch 11 → 14 not taken.
28 if (desc_equals(desc, local, global_value)) {
1090 28 string_append_literal(&buf, "set ");
1091 28 append_option(&buf, desc, local);
1092 } else {
1093 ✗ string_append_literal(&buf, "set -g ");
1094 ✗ append_option(&buf, desc, global);
1095 ✗ string_append_literal(&buf, "set -l ");
1096 ✗ append_option(&buf, desc, local);
1097 }
1098 } else {
1099 48 string_append_literal(&buf, "set ");
1100
2/2
✓ Branch 20 → 21 taken 42 times.
✓ Branch 20 → 22 taken 6 times.
90 append_option(&buf, desc, local ? local : global);
1101 }
1102 }
1103 2 return buf;
1104 }
1105
1106 4 const char *get_option_value_string(EditorState *e, const char *name)
1107 {
1108 4 const OptionDesc *desc = find_option(name);
1109
2/2
✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 7 taken 1 time.
4 if (!desc) {
1110 return NULL;
1111 }
1112 3 char *ptr = get_option_ptr(e, desc, !desc->local);
1113 3 return desc_string(desc, desc_get(desc, ptr));
1114 }
1115