dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 84.9% 422 / 5 / 502
Functions: 91.2% 62 / 0 / 68
Branches: 72.0% 201 / 78 / 357

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