Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "feature.h" | ||
2 | #include <errno.h> | ||
3 | #include <stdbool.h> | ||
4 | #include <stdlib.h> | ||
5 | #include <string.h> | ||
6 | #include <sys/types.h> | ||
7 | #include "config.h" | ||
8 | #include "command/cache.h" | ||
9 | #include "command/error.h" | ||
10 | #include "commands.h" | ||
11 | #include "compiler.h" | ||
12 | #include "editor.h" | ||
13 | #include "syntax/color.h" | ||
14 | #include "util/debug.h" | ||
15 | #include "util/hashmap.h" | ||
16 | #include "util/intmap.h" | ||
17 | #include "util/log.h" | ||
18 | #include "util/readfile.h" | ||
19 | #include "util/str-util.h" | ||
20 | #include "util/xsnprintf.h" | ||
21 | |||
22 | #if HAVE_EMBED | ||
23 | #include "builtin-config-embed.h" | ||
24 | #else | ||
25 | #include "builtin-config.h" | ||
26 | #endif | ||
27 | |||
28 | 24 | UNITTEST { | |
29 | // NOLINTBEGIN(bugprone-assert-side-effect) | ||
30 | 24 | BUG_ON(!get_builtin_config("rc")); | |
31 | 24 | BUG_ON(!get_builtin_config("color/reset")); | |
32 | // NOLINTEND(bugprone-assert-side-effect) | ||
33 | 24 | } | |
34 | |||
35 | // Odd number of backslashes at end of line? | ||
36 | 9814 | static bool has_line_continuation(StringView line) | |
37 | { | ||
38 | 9814 | ssize_t pos = line.length - 1; | |
39 |
4/4✓ Branch 0 (4→5) taken 9190 times.
✓ Branch 1 (4→6) taken 1514 times.
✓ Branch 2 (5→3) taken 890 times.
✓ Branch 3 (5→6) taken 8300 times.
|
10704 | while (pos >= 0 && line.data[pos] == '\\') { |
40 | 890 | pos--; | |
41 | } | ||
42 | 9814 | return (line.length - 1 - pos) & 1; | |
43 | } | ||
44 | |||
45 | 24 | UNITTEST { | |
46 | // NOLINTBEGIN(bugprone-assert-side-effect) | ||
47 | 24 | BUG_ON(has_line_continuation(string_view(NULL, 0))); | |
48 | 24 | BUG_ON(has_line_continuation(strview_from_cstring("0"))); | |
49 | 24 | BUG_ON(!has_line_continuation(strview_from_cstring("1 \\"))); | |
50 | 24 | BUG_ON(has_line_continuation(strview_from_cstring("2 \\\\"))); | |
51 | 24 | BUG_ON(!has_line_continuation(strview_from_cstring("3 \\\\\\"))); | |
52 | 24 | BUG_ON(has_line_continuation(strview_from_cstring("4 \\\\\\\\"))); | |
53 | // NOLINTEND(bugprone-assert-side-effect) | ||
54 | 24 | } | |
55 | |||
56 | 226 | bool exec_config(CommandRunner *runner, StringView config) | |
57 | { | ||
58 | 226 | EditorState *e = runner->e; | |
59 | 226 | ErrorBuffer *ebuf = runner->ebuf; | |
60 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 225 times.
|
226 | if (unlikely(e->include_recursion_count > 64)) { |
61 | 1 | return error_msg_for_cmd(ebuf, NULL, "config recursion limit reached"); | |
62 | } | ||
63 | |||
64 | 225 | bool stop_at_first_err = (runner->flags & CMDRUNNER_STOP_AT_FIRST_ERROR); | |
65 | 225 | size_t nfailed = 0; | |
66 | 225 | String buf = string_new(1024); | |
67 | 225 | e->include_recursion_count++; | |
68 | |||
69 |
2/2✓ Branch 0 (22→6) taken 9907 times.
✓ Branch 1 (22→23) taken 224 times.
|
10131 | for (size_t i = 0, n = config.length; i < n; ebuf->config_line++) { |
70 | 9907 | StringView line = buf_slice_next_line(config.data, &i, n); | |
71 | 9907 | strview_trim_left(&line); | |
72 |
4/4✓ Branch 0 (8→9) taken 9259 times.
✓ Branch 1 (8→12) taken 648 times.
✓ Branch 2 (10→11) taken 237 times.
✓ Branch 3 (10→12) taken 9022 times.
|
9907 | if (buf.len == 0 && strview_has_prefix(&line, "#")) { |
73 | // Comment line | ||
74 | 237 | continue; | |
75 | } | ||
76 |
2/2✓ Branch 0 (12→13) taken 650 times.
✓ Branch 1 (12→14) taken 9020 times.
|
9670 | if (has_line_continuation(line)) { |
77 | 650 | line.length--; | |
78 | 650 | string_append_strview(&buf, &line); | |
79 | } else { | ||
80 | 9020 | string_append_strview(&buf, &line); | |
81 | 9020 | bool r = handle_command(runner, string_borrow_cstring(&buf)); | |
82 | 9020 | string_clear(&buf); | |
83 | 9020 | nfailed += !r; | |
84 |
2/2✓ Branch 0 (18→19) taken 1 times.
✓ Branch 1 (18→20) taken 9019 times.
|
9020 | if (unlikely(!r && stop_at_first_err)) { |
85 | 1 | goto out; | |
86 | } | ||
87 | } | ||
88 | } | ||
89 | |||
90 |
2/2✓ Branch 0 (23→24) taken 222 times.
✓ Branch 1 (23→25) taken 2 times.
|
224 | if (unlikely(buf.len)) { |
91 | // This can only happen if the last line had a line continuation | ||
92 | 2 | bool r = handle_command(runner, string_borrow_cstring(&buf)); | |
93 | 2 | nfailed += !r; | |
94 | } | ||
95 | |||
96 | 222 | out: | |
97 | 225 | e->include_recursion_count--; | |
98 | 225 | string_free(&buf); | |
99 | 225 | return (nfailed == 0); | |
100 | } | ||
101 | |||
102 | 3 | String dump_builtin_configs(void) | |
103 | { | ||
104 | 3 | String str = string_new(1024); | |
105 |
2/2✓ Branch 0 (7→4) taken 198 times.
✓ Branch 1 (7→8) taken 3 times.
|
201 | for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) { |
106 | 198 | string_append_cstring(&str, builtin_configs[i].name); | |
107 | 198 | string_append_byte(&str, '\n'); | |
108 | } | ||
109 | 3 | return str; | |
110 | } | ||
111 | |||
112 | 149 | const BuiltinConfig *get_builtin_config(const char *name) | |
113 | { | ||
114 |
2/2✓ Branch 0 (6→3) taken 4439 times.
✓ Branch 1 (6→7) taken 58 times.
|
4497 | for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) { |
115 |
2/2✓ Branch 0 (3→4) taken 91 times.
✓ Branch 1 (3→5) taken 4348 times.
|
4439 | if (streq(name, builtin_configs[i].name)) { |
116 | 91 | return &builtin_configs[i]; | |
117 | } | ||
118 | } | ||
119 | return NULL; | ||
120 | } | ||
121 | |||
122 | 1 | const BuiltinConfig *get_builtin_configs_array(size_t *nconfigs) | |
123 | { | ||
124 | 1 | *nconfigs = ARRAYLEN(builtin_configs); | |
125 | 1 | return &builtin_configs[0]; | |
126 | } | ||
127 | |||
128 | 111 | static int do_read_config(CommandRunner *runner, const char *filename, ConfigFlags flags) | |
129 | { | ||
130 | 111 | ErrorBuffer *ebuf = runner->ebuf; | |
131 | 111 | bool must_exist = flags & CFG_MUST_EXIST; | |
132 | 111 | bool stop_at_first_err = runner->flags & CMDRUNNER_STOP_AT_FIRST_ERROR; | |
133 | |||
134 |
2/2✓ Branch 0 (2→3) taken 42 times.
✓ Branch 1 (2→11) taken 69 times.
|
111 | if (flags & CFG_BUILTIN) { |
135 | 42 | const BuiltinConfig *cfg = get_builtin_config(filename); | |
136 |
2/2✓ Branch 0 (3→4) taken 41 times.
✓ Branch 1 (3→8) taken 1 times.
|
42 | if (cfg) { |
137 | 41 | ebuf->config_filename = filename; | |
138 | 41 | ebuf->config_line = 1; | |
139 | 41 | bool r = exec_config(runner, cfg->text); | |
140 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 41 times.
|
41 | return (r || !stop_at_first_err) ? 0 : EINVAL; |
141 | } | ||
142 |
1/2✓ Branch 0 (8→9) taken 1 times.
✗ Branch 1 (8→10) not taken.
|
1 | if (must_exist) { |
143 | 1 | error_msg(ebuf, "no built-in config with name '%s'", filename); | |
144 | } | ||
145 | 1 | return ENOENT; | |
146 | } | ||
147 | |||
148 | 69 | char *buf; | |
149 | 69 | ssize_t size = read_file(filename, &buf, 0); | |
150 |
2/2✓ Branch 0 (12→13) taken 1 times.
✓ Branch 1 (12→17) taken 68 times.
|
69 | if (size < 0) { |
151 | 1 | int err = errno; | |
152 |
1/2✓ Branch 0 (13→14) taken 1 times.
✗ Branch 1 (13→16) not taken.
|
1 | if (err != ENOENT || must_exist) { |
153 | 1 | error_msg(ebuf, "Error reading %s: %s", filename, strerror(err)); | |
154 | } | ||
155 | 1 | return err; | |
156 | } | ||
157 | |||
158 | 68 | ebuf->config_filename = filename; | |
159 | 68 | ebuf->config_line = 1; | |
160 | 68 | bool r = exec_config(runner, string_view(buf, size)); | |
161 | 68 | free(buf); | |
162 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 68 times.
|
68 | return (r || !stop_at_first_err) ? 0 : EINVAL; |
163 | } | ||
164 | |||
165 | 111 | int read_config(CommandRunner *runner, const char *filename, ConfigFlags flags) | |
166 | { | ||
167 | 111 | ErrorBuffer *ebuf = runner->ebuf; | |
168 | 111 | const char *saved_file = ebuf->config_filename; | |
169 | 111 | const unsigned int saved_line = ebuf->config_line; | |
170 | 111 | int ret = do_read_config(runner, filename, flags); | |
171 | 111 | ebuf->config_filename = saved_file; | |
172 | 111 | ebuf->config_line = saved_line; | |
173 | 111 | return ret; | |
174 | } | ||
175 | |||
176 | 27 | static void exec_builtin_config(EditorState *e, StringView cfg, const char *name) | |
177 | { | ||
178 | 27 | ErrorBuffer *ebuf = &e->err; | |
179 | 27 | const char *saved_file = ebuf->config_filename; | |
180 | 27 | const unsigned int saved_line = ebuf->config_line; | |
181 | 27 | ebuf->config_filename = name; | |
182 | 27 | ebuf->config_line = 1; | |
183 | 27 | exec_normal_config(e, cfg); | |
184 | 27 | ebuf->config_filename = saved_file; | |
185 | 27 | ebuf->config_line = saved_line; | |
186 | 27 | } | |
187 | |||
188 | 18 | void exec_builtin_color_reset(EditorState *e) | |
189 | { | ||
190 | 18 | clear_hl_styles(&e->styles); | |
191 | 18 | StringView reset = string_view(builtin_color_reset, sizeof(builtin_color_reset) - 1); | |
192 | 18 | exec_builtin_config(e, reset, "color/reset"); | |
193 | 18 | } | |
194 | |||
195 | 9 | static void log_config_counts(const EditorState *e) | |
196 | { | ||
197 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→17) taken 8 times.
|
9 | if (!log_level_enabled(LOG_LEVEL_INFO)) { |
198 | return; | ||
199 | } | ||
200 | |||
201 | 1 | size_t nbinds = 0; | |
202 | 1 | size_t nbinds_cached = 0; | |
203 |
2/2✓ Branch 0 (11→5) taken 3 times.
✓ Branch 1 (11→12) taken 1 times.
|
4 | for (HashMapIter modeiter = hashmap_iter(&e->modes); hashmap_next(&modeiter); ) { |
204 | 3 | const ModeHandler *mode = modeiter.entry->value; | |
205 | 3 | const IntMap *binds = &mode->key_bindings; | |
206 | 3 | nbinds += binds->count; | |
207 |
2/2✓ Branch 0 (8→6) taken 209 times.
✓ Branch 1 (8→9) taken 3 times.
|
212 | for (IntMapIter binditer = intmap_iter(binds); intmap_next(&binditer); ) { |
208 | 209 | const CachedCommand *cc = binditer.entry->value; | |
209 | 209 | nbinds_cached += !!cc->cmd; | |
210 | } | ||
211 | } | ||
212 | |||
213 | 1 | size_t nerrorfmts = 0; | |
214 |
2/2✓ Branch 0 (15→13) taken 3 times.
✓ Branch 1 (15→16) taken 1 times.
|
4 | for (HashMapIter it = hashmap_iter(&e->compilers); hashmap_next(&it); ) { |
215 | 3 | const Compiler *compiler = it.entry->value; | |
216 | 3 | nerrorfmts += compiler->error_formats.count; | |
217 | } | ||
218 | |||
219 | 1 | LOG_INFO ( | |
220 | "bind=%zu(%zu) alias=%zu hi=%zu ft=%zu option=%zu errorfmt=%zu(%zu)", | ||
221 | nbinds, | ||
222 | nbinds_cached, | ||
223 | e->aliases.count, | ||
224 | e->styles.other.count + NR_BSE, | ||
225 | e->filetypes.count, | ||
226 | e->file_options.count, | ||
227 | e->compilers.count, | ||
228 | nerrorfmts | ||
229 | ); | ||
230 | } | ||
231 | |||
232 | 9 | void exec_rc_files(EditorState *e, const char *filename, bool read_user_rc) | |
233 | { | ||
234 | 9 | StringView rc = string_view(builtin_rc, sizeof(builtin_rc) - 1); | |
235 | 9 | exec_builtin_color_reset(e); | |
236 | 9 | exec_builtin_config(e, rc, "rc"); | |
237 | |||
238 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→10) taken 7 times.
|
9 | if (read_user_rc) { |
239 | 2 | ConfigFlags flags = CFG_NOFLAGS; | |
240 | 2 | char buf[8192]; | |
241 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 2 times.
|
2 | if (filename) { |
242 | flags |= CFG_MUST_EXIST; | ||
243 | } else { | ||
244 | ✗ | xsnprintf(buf, sizeof buf, "%s/%s", e->user_config_dir, "rc"); | |
245 | ✗ | filename = buf; | |
246 | } | ||
247 | 2 | LOG_INFO("loading configuration from %s", filename); | |
248 | 2 | read_normal_config(e, filename, flags); | |
249 | } | ||
250 | |||
251 | 9 | log_config_counts(e); | |
252 | 9 | update_all_syntax_styles(&e->syntaxes, &e->styles); | |
253 | 9 | } | |
254 | |||
255 | 1 | void collect_builtin_configs(PointerArray *a, const char *prefix) | |
256 | { | ||
257 | 1 | size_t prefix_len = strlen(prefix); | |
258 |
2/2✓ Branch 0 (7→3) taken 66 times.
✓ Branch 1 (7→8) taken 1 times.
|
67 | for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) { |
259 | 66 | const char *name = builtin_configs[i].name; | |
260 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→6) taken 65 times.
|
66 | if (str_has_strn_prefix(name, prefix, prefix_len)) { |
261 | 1 | ptr_array_append(a, xstrdup(name)); | |
262 | } | ||
263 | } | ||
264 | 1 | } | |
265 | |||
266 | 1 | void collect_builtin_includes(PointerArray *a, const char *prefix) | |
267 | { | ||
268 | 1 | size_t prefix_len = strlen(prefix); | |
269 |
2/2✓ Branch 0 (8→3) taken 66 times.
✓ Branch 1 (8→9) taken 1 times.
|
67 | for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) { |
270 | 66 | const char *name = builtin_configs[i].name; | |
271 | 66 | if ( | |
272 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→7) taken 65 times.
|
66 | str_has_strn_prefix(name, prefix, prefix_len) |
273 |
1/2✓ Branch 0 (4→5) taken 1 times.
✗ Branch 1 (4→7) not taken.
|
1 | && !str_has_prefix(name, "syntax/") |
274 | ) { | ||
275 | 1 | ptr_array_append(a, xstrdup(name)); | |
276 | } | ||
277 | } | ||
278 | 1 | } | |
279 |