aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
blob: b416d7acae6fcc9e899f4bc0b8cefdbeb6ccf245 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "config.h"

#include "buffers.h"
#include "ccdjit.h"
#include "common.h"
#include "ecex.h"
#include "eval.h"
#include "util.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int (*ecex_config_init_fn)(ecex_t *ed);

typedef struct host_symbol {
    const char *name;
    void *addr;
} host_symbol_t;

void ecex_print_ccdjit_error(ccdjit_context *ctx) {
    const ccdjit_diagnostic *diag = ccdjit_context_last_error(ctx);

    if (!diag) {
        fprintf(stderr, "ccdjit: unknown error\n");
        return;
    }

    fprintf(stderr,
            "ccdjit error [%s] %s:%d:%d: %s\n",
            diag->phase ? diag->phase : "?",
            diag->filename ? diag->filename : "<input>",
            diag->line,
            diag->column,
            diag->message ? diag->message : "(no message)");

    if (diag->source_line) fprintf(stderr, "%s\n", diag->source_line);
    if (diag->caret_line) fprintf(stderr, "%s\n", diag->caret_line);
}

#define HOST_SYMBOL(fn) { #fn, (void *)(fn) }

static const host_symbol_t host_symbols[] = {
    HOST_SYMBOL(ecex_current_buffer),
    HOST_SYMBOL(ecex_create_buffer),
    HOST_SYMBOL(ecex_find_buffer),
    HOST_SYMBOL(ecex_switch_buffer),
    HOST_SYMBOL(ecex_next_buffer),
    HOST_SYMBOL(ecex_previous_buffer),
    HOST_SYMBOL(ecex_current_window),
    HOST_SYMBOL(ecex_window_count),
    HOST_SYMBOL(ecex_split_window_vertically),
    HOST_SYMBOL(ecex_split_window_horizontally),
    HOST_SYMBOL(ecex_other_window),
    HOST_SYMBOL(ecex_previous_window),
    HOST_SYMBOL(ecex_delete_window),
    HOST_SYMBOL(ecex_delete_other_windows),
    HOST_SYMBOL(ecex_kill_buffer),

    HOST_SYMBOL(ecex_register_command),
    HOST_SYMBOL(ecex_execute_command),
    HOST_SYMBOL(ecex_bind_key),
    HOST_SYMBOL(ecex_lookup_key),
    HOST_SYMBOL(ecex_define_major_mode),
    HOST_SYMBOL(ecex_major_mode_by_name),
    HOST_SYMBOL(ecex_major_mode_name),
    HOST_SYMBOL(ecex_buffer_set_major_mode),
    HOST_SYMBOL(ecex_buffer_set_major_mode_by_name),
    HOST_SYMBOL(ecex_buffer_major_mode_name),
    HOST_SYMBOL(ecex_bind_mode_key),
    HOST_SYMBOL(ecex_lookup_key_for_buffer),
    HOST_SYMBOL(ecex_key_sequence_has_prefix_for_buffer),
    HOST_SYMBOL(ecex_auto_set_major_mode),
    HOST_SYMBOL(ecex_list_commands),
    HOST_SYMBOL(ecex_list_buffers),
    HOST_SYMBOL(ecex_create_interactive_buffer),
    HOST_SYMBOL(ecex_interactive_append_line),
    HOST_SYMBOL(ecex_interactive_activate_current_line),
    HOST_SYMBOL(ecex_find_file),
    HOST_SYMBOL(ecex_save_current_buffer),
    HOST_SYMBOL(ecex_write_current_buffer),
    HOST_SYMBOL(ecex_request_prompt),
    HOST_SYMBOL(ecex_clear_prompt_request),
    HOST_SYMBOL(ecex_complete_command),
    HOST_SYMBOL(ecex_eval_source),
    HOST_SYMBOL(ecex_eval_current_buffer),
    HOST_SYMBOL(ecex_eval_current_line),
    HOST_SYMBOL(ecex_eval_current_region),
    HOST_SYMBOL(ecex_eval_file),
    HOST_SYMBOL(ecex_eval_rerun_last),

    HOST_SYMBOL(ecex_set_font),
    HOST_SYMBOL(ecex_get_font_size),
    HOST_SYMBOL(ecex_set_font_size),
    HOST_SYMBOL(ecex_adjust_font_size),
    HOST_SYMBOL(ecex_set_bg_color),
    HOST_SYMBOL(ecex_set_fg_color),
    HOST_SYMBOL(ecex_set_status_bg_color),
    HOST_SYMBOL(ecex_set_status_fg_color),
    HOST_SYMBOL(ecex_set_status_border_color),
    HOST_SYMBOL(ecex_set_cursor_color),
    HOST_SYMBOL(ecex_set_region_bg_color),
    HOST_SYMBOL(ecex_set_minibuffer_bg_color),
    HOST_SYMBOL(ecex_set_minibuffer_fg_color),
    HOST_SYMBOL(ecex_set_completion_fg_color),
    HOST_SYMBOL(ecex_set_completion_enabled),
    HOST_SYMBOL(ecex_set_interactive_highlight_bg_color),
    HOST_SYMBOL(ecex_set_interactive_highlight_fg_color),

    HOST_SYMBOL(buffer_clear),
    HOST_SYMBOL(buffer_set_text),
    HOST_SYMBOL(buffer_insert),
    HOST_SYMBOL(buffer_insert_at),
    HOST_SYMBOL(buffer_append),
    HOST_SYMBOL(buffer_prepend),
    HOST_SYMBOL(buffer_insert_char),
    HOST_SYMBOL(buffer_insert_char_at),
    HOST_SYMBOL(buffer_delete_range),
    HOST_SYMBOL(buffer_delete_selection),
    HOST_SYMBOL(buffer_replace_selection),
    HOST_SYMBOL(buffer_backspace),
    HOST_SYMBOL(buffer_delete_forward),
    HOST_SYMBOL(buffer_kill_line),

    HOST_SYMBOL(buffer_set_point),
    HOST_SYMBOL(buffer_move_left),
    HOST_SYMBOL(buffer_move_right),
    HOST_SYMBOL(buffer_move_up),
    HOST_SYMBOL(buffer_move_down),
    HOST_SYMBOL(buffer_move_word_left),
    HOST_SYMBOL(buffer_move_word_right),
    HOST_SYMBOL(buffer_move_beginning_of_line),
    HOST_SYMBOL(buffer_move_end_of_line),
    HOST_SYMBOL(buffer_move_beginning_of_buffer),
    HOST_SYMBOL(buffer_move_end_of_buffer),

    HOST_SYMBOL(buffer_set_mark),
    HOST_SYMBOL(buffer_clear_mark),
    HOST_SYMBOL(buffer_has_selection),
    HOST_SYMBOL(buffer_selection_range),

    HOST_SYMBOL(buffer_line_start_at),
    HOST_SYMBOL(buffer_line_end_at),
    HOST_SYMBOL(buffer_current_line_start),
    HOST_SYMBOL(buffer_current_line_end),
    HOST_SYMBOL(buffer_current_column),
    HOST_SYMBOL(buffer_current_line_number),
    HOST_SYMBOL(buffer_line_count),
    HOST_SYMBOL(buffer_substring),
    HOST_SYMBOL(buffer_current_line_copy),

    HOST_SYMBOL(buffer_load_file),
    HOST_SYMBOL(buffer_save),
    HOST_SYMBOL(buffer_save_as),
    HOST_SYMBOL(buffer_set_interactive),
    HOST_SYMBOL(buffer_is_interactive),
    HOST_SYMBOL(buffer_clear_interactive_actions),
    HOST_SYMBOL(buffer_add_interactive_action),
    HOST_SYMBOL(buffer_interactive_action_at_line),
};

#undef HOST_SYMBOL

int ecex_register_host_symbols(ccdjit_context *ctx) {
    for (size_t i = 0; i < ECEX_ARRAY_LEN(host_symbols); i++) {
        if (ccdjit_context_register_symbol(ctx,
                                           host_symbols[i].name,
                                           host_symbols[i].addr) != 0) {
            fprintf(stderr, "ecex: failed to register symbol: %s\n", host_symbols[i].name);
            ecex_print_ccdjit_error(ctx);
            return ECEX_ERR;
        }
    }

    return ECEX_OK;
}

static int add_include_path(ccdjit_context *ctx, const char *path) {
    if (ccdjit_context_add_include_path(ctx, path) == 0) return ECEX_OK;

    fprintf(stderr, "ecex: failed to add include path: %s\n", path);
    ecex_print_ccdjit_error(ctx);
    return ECEX_ERR;
}

int ecex_add_ccdjit_include_paths(ccdjit_context *ctx) {
    if (add_include_path(ctx, "include") != ECEX_OK) return ECEX_ERR;
    if (add_include_path(ctx, "../include") != ECEX_OK) return ECEX_ERR;

    const char *env_include = getenv("ECEX_INCLUDE");
    if (env_include && env_include[0]) {
        if (add_include_path(ctx, env_include) != ECEX_OK) return ECEX_ERR;
    }

    return ECEX_OK;
}

static char *make_config_source_with_forced_main(const char *path) {
    size_t source_size = 0;
    char *source = ecex_read_entire_file(path, &source_size);
    if (!source) return NULL;

    const char *forced_main =
        "\n"
        "\n"
        "int main(int argc, char **argv) {\n"
        "    (void)argc;\n"
        "    (void)argv;\n"
        "    return ecex_config_init((ecex_t *)0);\n"
        "}\n";

    size_t forced_main_len = strlen(forced_main);
    char *combined = malloc(source_size + forced_main_len + 1);
    if (!combined) {
        free(source);
        return NULL;
    }

    memcpy(combined, source, source_size);
    memcpy(combined + source_size, forced_main, forced_main_len + 1);

    free(source);
    return combined;
}

int ecex_load_c_config(ecex_t *ed, const char *path) {
    if (!ed || !path) return ECEX_ERR;

    ccdjit_context *ctx = ccdjit_context_new(NULL);
    if (!ctx) {
        fprintf(stderr, "ecex: failed to create ccdjit context\n");
        return ECEX_ERR;
    }

    if (ecex_add_ccdjit_include_paths(ctx) != ECEX_OK || ecex_register_host_symbols(ctx) != ECEX_OK) {
        ccdjit_context_free(ctx);
        return ECEX_ERR;
    }

    char *source = make_config_source_with_forced_main(path);
    if (!source) {
        fprintf(stderr, "ecex: failed to read or prepare config: %s\n", path);
        ccdjit_context_free(ctx);
        return ECEX_ERR;
    }

    ccdjit_module *module = NULL;
    if (ccdjit_compile_string(ctx, source, path, &module) != 0) {
        fprintf(stderr, "ecex: failed to compile config: %s\n", path);
        ecex_print_ccdjit_error(ctx);
        free(source);
        ccdjit_context_free(ctx);
        return ECEX_ERR;
    }

    free(source);

    ecex_config_init_fn init =
        (ecex_config_init_fn)ccdjit_module_symbol(module, "ecex_config_init");

    if (!init) {
        fprintf(stderr, "ecex: config missing function: ecex_config_init\n");
        ecex_print_ccdjit_error(ctx);
        ccdjit_module_free(module);
        ccdjit_context_free(ctx);
        return ECEX_ERR;
    }

    int result = init(ed);
    if (result != 0) {
        fprintf(stderr, "ecex: config init failed with code: %d\n", result);
        ccdjit_module_free(module);
        ccdjit_context_free(ctx);
        return ECEX_ERR;
    }

    if (ecex_keep_jit_module(ed, module) != ECEX_OK) {
        fprintf(stderr, "ecex: failed to retain config module\n");
        ccdjit_module_free(module);
        ccdjit_context_free(ctx);
        return ECEX_ERR;
    }

    if (ecex_set_config_path(ed, path) != ECEX_OK) {
        fprintf(stderr, "ecex: failed to remember config path: %s\n", path);
        ccdjit_context_free(ctx);
        return ECEX_ERR;
    }

    ccdjit_context_free(ctx);
    fprintf(stderr, "ecex: loaded config: %s\n", path);
    return ECEX_OK;
}