-
Notifications
You must be signed in to change notification settings - Fork 53
/
uscript.c
412 lines (353 loc) · 12.3 KB
/
uscript.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <inttypes.h>
#include "mle.h"
#define MLE_USCRIPT_KEY "_uscript"
#define MLE_USCRIPT_GET(pl, pu) do { \
lua_getglobal((pl), MLE_USCRIPT_KEY); \
(pu) = luaL_optpointer((pl), -1, NULL); \
if (!(pu)) return 0; \
} while(0)
#define luaL_pushkey(pL, pt, pk, pv) do { \
lua_pushstring((pL), (pk)); \
(lua_push ## pt) ((pL), (pv)); \
lua_settable((pL), -3); \
} while (0)
#define luaL_pushkey2(pL, pt, pk, pv1, pv2) do { \
lua_pushstring((pL), (pk)); \
(lua_push ## pt) ((pL), (pv1), (pv2)); \
lua_settable((pL), -3); \
} while (0)
static int _uscript_panic(lua_State *L);
static int _uscript_cmd_cb(cmd_context_t *ctx);
static int _uscript_observer_cb(char *event_name, void *event_data, void *udata);
static int _uscript_write(lua_State *L);
static void _uscript_push_event_map(uscript_t *uscript, char *event_name, void *event_data);
static void _uscript_push_cmd_map(lua_State *L, cmd_context_t *ctx);
static void _uscript_push_baction_map(lua_State *L, baction_t *baction);
static int _uscript_func_editor_prompt(lua_State *L);
static int _uscript_func_editor_register_cmd(lua_State *L);
static int _uscript_func_editor_register_observer(lua_State *L);
static int _uscript_func_util_escape_shell_arg(lua_State *L);
static int _uscript_func_util_shell_exec(lua_State *L);
static int _uscript_func_editor_get_input(lua_State *L);
static int _uscript_func_editor_menu(lua_State *L);
static int _uscript_func_bview_pop_kmap(lua_State *L);
static int _uscript_func_bview_push_kmap(lua_State *L);
static int _uscript_func_buffer_set_callback(lua_State *L);
static int _uscript_func_buffer_add_srule(lua_State *L);
static int _uscript_func_buffer_remove_srule(lua_State *L);
static int _uscript_func_buffer_write_to_file(lua_State *L);
static void *luaL_checkpointer(lua_State *L, int arg);
static void *luaL_optpointer(lua_State *L, int arg, void *def);
static void lua_pushpointer(lua_State *L, void *ptr);
static int luaL_checkfunction(lua_State *L, int arg);
static int luaopen_mle(lua_State *L);
#include "uscript.inc"
// Run uscript
uscript_t *uscript_run(editor_t *editor, char *path) {
lua_State *L;
uscript_t *uscript;
L = luaL_newstate();
luaL_openlibs(L);
luaL_requiref(L, "mle", luaopen_mle, 1);
uscript = calloc(1, sizeof(uscript_t));
uscript->editor = editor;
uscript->L = L;
lua_pushpointer(L, (void*)uscript);
lua_setglobal(L, MLE_USCRIPT_KEY);
lua_pop(L, 1);
lua_getglobal(L, "_G");
lua_pushcfunction(L, _uscript_write);
lua_setfield(L, -2, "print");
lua_pop(L, 1);
lua_atpanic(L, _uscript_panic);
luaL_loadfile(L, path); // TODO err
lua_pcall(L, 0, 0, 0);
return uscript;
}
// Destroy uscript
int uscript_destroy(uscript_t *uscript) {
lua_close(uscript->L);
return MLE_OK;
}
static int _uscript_panic(lua_State *L) {
MLE_SET_ERR(&_editor, "uscript panic: %s", lua_tostring(L, -1));
return 0;
}
// Invoke cmd in uscript
static int _uscript_cmd_cb(cmd_context_t *ctx) {
int rv;
lua_State *L;
uhandle_t *uhandle;
int top;
uhandle = (uhandle_t*)(ctx->cmd->udata);
L = uhandle->uscript->L;
top = lua_gettop(L);
lua_rawgeti(L, LUA_REGISTRYINDEX, uhandle->callback_ref);
_uscript_push_cmd_map(L, ctx);
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
printf("err[%s]\n", luaL_checkstring(L, -1));
rv = MLE_ERR;
} else if (lua_isboolean(L, -1) && !lua_toboolean(L, -1)) {
rv = MLE_ERR;
} else {
rv = MLE_OK;
}
lua_settop(L, top);
return rv;
}
static int _uscript_observer_cb(char *event_name, void *event_data, void *udata) {
int rv;
lua_State *L;
uhandle_t *uhandle;
uhandle = (uhandle_t*)(udata);
L = uhandle->uscript->L;
lua_rawgeti(L, LUA_REGISTRYINDEX, uhandle->callback_ref);
_uscript_push_event_map(uhandle->uscript, event_name, event_data);
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
printf("err[%s]\n", luaL_checkstring(L, -1));
rv = MLE_ERR;
} else if (lua_isboolean(L, -1) && !lua_toboolean(L, -1)) {
rv = MLE_ERR;
} else {
rv = MLE_OK;
}
return rv;
}
// Handle write from uscript
static int _uscript_write(lua_State *L) {
uscript_t *uscript;
char *str;
mark_t *mark;
int i, nargs;
nargs = lua_gettop(L);
MLE_USCRIPT_GET(L, uscript);
if (!uscript->editor->active_edit
|| !uscript->editor->active_edit->active_cursor
) {
return 0;
}
mark = uscript->editor->active_edit->active_cursor->mark;
for (i = 1; i <= nargs; i++) {
str = (char*)lua_tostring(L, i);
mark_insert_before(mark, str, strlen(str));
}
return 0;
}
static void _uscript_push_event_map(uscript_t *uscript, char *event_name, void *event_data) {
lua_State *L;
L = uscript->L;
if (strcmp(event_name, "buffer:baction") == 0) {
_uscript_push_baction_map(L, (baction_t*)event_data);
return;
} else if (strcmp(event_name, "buffer:save") == 0) {
lua_createtable(L, 0, 1);
luaL_pushkey(L, pointer, "bview", event_data);
return;
} else if (strncmp(event_name, "cmd:", 4) == 0) {
_uscript_push_cmd_map(L, (cmd_context_t*)event_data);
return;
}
lua_pushnil(uscript->L); // TODO
}
static void _uscript_push_cmd_map(lua_State *L, cmd_context_t *ctx) {
lua_createtable(L, 0, 1);
luaL_pushkey(L, pointer, "editor", (void*)ctx->editor);
luaL_pushkey(L, pointer, "loop_ctx", (void*)ctx->loop_ctx);
luaL_pushkey(L, pointer, "cmd", (void*)ctx->cmd);
luaL_pushkey(L, pointer, "buffer", (void*)ctx->buffer);
luaL_pushkey(L, pointer, "bview", (void*)ctx->bview);
luaL_pushkey(L, pointer, "cursor", (void*)ctx->cursor);
luaL_pushkey(L, pointer, "mark", (void*)ctx->cursor->mark);
luaL_pushkey(L, string, "static_param", (const char*)ctx->static_param);
}
static void _uscript_push_baction_map(lua_State *L, baction_t *baction) {
lua_createtable(L, 0, 1);
luaL_pushkey(L, integer, "type", baction->type);
luaL_pushkey(L, pointer, "buffer", (void*)baction->buffer);
luaL_pushkey(L, integer, "start_line_index", baction->start_line_index);
luaL_pushkey(L, integer, "start_col", baction->start_col);
luaL_pushkey(L, integer, "maybe_end_line_index", baction->maybe_end_line_index);
luaL_pushkey(L, integer, "maybe_end_col", baction->maybe_end_col);
luaL_pushkey(L, integer, "byte_delta", baction->byte_delta);
luaL_pushkey(L, integer, "char_delta", baction->char_delta);
luaL_pushkey(L, integer, "line_delta", baction->line_delta);
luaL_pushkey2(L, lstring, "data", (const char*)baction->data, baction->data_len);
}
// foreign static string _uscript_func_editor_prompt(prompt)
static int _uscript_func_editor_prompt(lua_State *L) {
uscript_t *uscript;
char *prompt;
char *answer = NULL;
MLE_USCRIPT_GET(L, uscript);
prompt = (char*)luaL_checkstring(L, 1);
if (editor_prompt(uscript->editor, prompt, NULL, &answer) == MLE_OK && answer) {
lua_pushstring(L, answer);
free(answer);
} else {
lua_pushnil(L);
}
return 1;
}
int editor_register_cmd(editor_t *editor, cmd_t *cmd);
// foreign static int _uscript_func_editor_register_cmd(cmd_name, fn_callback)
static int _uscript_func_editor_register_cmd(lua_State *L) {
uscript_t *uscript;
uhandle_t *uhandle;
int rv;
char *cmd_name;
int fn_callback;
cmd_t cmd = {0};
MLE_USCRIPT_GET(L, uscript);
cmd_name = (char*)luaL_checkstring(L, 1); // strdup'd by editor_register_cmd
fn_callback = luaL_checkfunction(L, 2);
uhandle = calloc(1, sizeof(uhandle_t));
uhandle->uscript = uscript;
uhandle->callback_ref = fn_callback;
DL_APPEND(uscript->uhandles, uhandle);
cmd.name = cmd_name;
cmd.func = _uscript_cmd_cb;
cmd.udata = (void*)uhandle;
rv = editor_register_cmd(uscript->editor, &cmd);
lua_createtable(L, 0, 1);
luaL_pushkey(L, integer, "rv", rv);
lua_pushvalue(L, -1);
return 1;
}
// foreign static int _uscript_func_editor_register_observer(event_name, fn_callback)
static int _uscript_func_editor_register_observer(lua_State *L) {
int rv;
char *event_name;
int fn_callback;
uscript_t *uscript;
uhandle_t *uhandle;
MLE_USCRIPT_GET(L, uscript);
event_name = (char*)luaL_checkstring(L, 1);
fn_callback = luaL_checkfunction(L, 2);
uhandle = calloc(1, sizeof(uhandle_t));
uhandle->uscript = uscript;
uhandle->callback_ref = fn_callback;
DL_APPEND(uscript->uhandles, uhandle);
rv = editor_register_observer(uscript->editor, event_name, (void*)uhandle, _uscript_observer_cb, NULL);
lua_createtable(L, 0, 1);
luaL_pushkey(L, integer, "rv", rv);
lua_pushvalue(L, -1);
return 1;
}
// foreign static int _uscript_func_util_escape_shell_arg(arg)
static int _uscript_func_util_escape_shell_arg(lua_State *L) {
char *arg, *arg_escaped;
uscript_t *uscript;
MLE_USCRIPT_GET(L, uscript);
arg = (char*)luaL_checkstring(L, 1);
arg_escaped = util_escape_shell_arg(arg, strlen(arg));
lua_createtable(L, 0, 1);
luaL_pushkey(L, integer, "rv", 0);
luaL_pushkey2(L, lstring, "output", arg_escaped, strlen(arg_escaped));
lua_pushvalue(L, -1);
free(arg_escaped);
return 1;
}
// foreign static int _uscript_func_util_shell_exec(cmd, timeout_s)
static int _uscript_func_util_shell_exec(lua_State *L) {
int rv;
char *cmd;
long timeout_s;
uscript_t *uscript;
char *output;
size_t output_len;
int exit_code;
MLE_USCRIPT_GET(L, uscript);
cmd = (char*)luaL_checkstring(L, 1);
timeout_s = (long)luaL_checkinteger(L, 2);
output = NULL;
output_len = 0;
exit_code = -1;
rv = util_shell_exec(uscript->editor, cmd, timeout_s, NULL, 0, 0, NULL, &output, &output_len, &exit_code);
lua_createtable(L, 0, 1);
luaL_pushkey(L, integer, "rv", rv);
luaL_pushkey(L, integer, "exit_code", exit_code);
luaL_pushkey2(L, lstring, "output", (output ? output : ""), output_len);
lua_pushvalue(L, -1);
if (output) free(output);
return 1;
}
// foreign static int _uscript_func_editor_get_input(x, y, z)
static int _uscript_func_editor_get_input(lua_State *L) {
// TODO
(void)L;
return 0;
}
// foreign static int _uscript_func_editor_menu(x, y, z)
static int _uscript_func_editor_menu(lua_State *L) {
// TODO
(void)L;
return 0;
}
// foreign static int _uscript_func_bview_pop_kmap(x, y, z)
static int _uscript_func_bview_pop_kmap(lua_State *L) {
// TODO
(void)L;
return 0;
}
// foreign static int _uscript_func_bview_push_kmap(x, y, z)
static int _uscript_func_bview_push_kmap(lua_State *L) {
// TODO
(void)L;
return 0;
}
// foreign static int _uscript_func_buffer_set_callback(x, y, z)
static int _uscript_func_buffer_set_callback(lua_State *L) {
// TODO
(void)L;
return 0;
}
// foreign static int _uscript_func_buffer_add_srule(x, y, z)
static int _uscript_func_buffer_add_srule(lua_State *L) {
// TODO
(void)L;
return 0;
}
// foreign static int _uscript_func_buffer_remove_srule(x, y, z)
static int _uscript_func_buffer_remove_srule(lua_State *L) {
// TODO
(void)L;
return 0;
}
// foreign static int _uscript_func_buffer_write_to_file(x, y, z)
static int _uscript_func_buffer_write_to_file(lua_State *L) {
// TODO
(void)L;
return 0;
}
static void *luaL_checkpointer(lua_State *L, int arg) {
luaL_checktype(L, arg, LUA_TSTRING);
return luaL_optpointer(L, arg, NULL);
}
static void *luaL_optpointer(lua_State *L, int arg, void *def) {
const char *ptr;
uintptr_t ptr_as_int;
ptr = luaL_optstring(L, arg, NULL);
if (ptr && strlen(ptr) > 0) {
ptr_as_int = (uintptr_t)strtoull(ptr, NULL, 16);
return (void*)ptr_as_int;
}
return def;
}
static void lua_pushpointer(lua_State *L, void *ptr) {
char ptrbuf[32];
if (ptr == NULL) {
lua_pushnil(L);
} else {
snprintf(ptrbuf, 32, "%" PRIxPTR, (uintptr_t)ptr);
lua_pushstring(L, ptrbuf);
}
}
static int luaL_checkfunction(lua_State *L, int arg) {
luaL_checktype(L, arg, LUA_TFUNCTION);
lua_pushvalue(L, arg);
return luaL_ref(L, LUA_REGISTRYINDEX);
}
static int luaopen_mle(lua_State *L) {
luaL_newlib(L, mle_lib);
return 1;
}