-
Notifications
You must be signed in to change notification settings - Fork 5
/
lua.c
230 lines (194 loc) · 4.96 KB
/
lua.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
#include <assert.h>
#include <err.h>
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#if LUA_VERSION_NUM < 502
#error "huxdemp requires Lua >= v5.2."
#endif
static lua_State *L = NULL;
static void
luau_sdump(lua_State *pL)
{
fprintf(stderr, "---------- STACK DUMP ----------\n");
for (int i = lua_gettop(pL); i; --i) {
int t = lua_type(pL, i);
switch (t) {
break; case LUA_TSTRING:
fprintf(stderr, "%4d: [string] '%s'\n", i, lua_tostring(pL, i));
break; case LUA_TBOOLEAN:
fprintf(stderr, "%4d: [bool] '%s'\n", i,
lua_toboolean(pL, i) ? "true" : "false");
break; case LUA_TNUMBER:
fprintf(stderr, "%4d: [number] '%g'\n", i, lua_tonumber(pL, i));
break; case LUA_TNIL:
fprintf(stderr, "%4d: [nil]\n", i);
break; default:
fprintf(stderr, "%4d: [%s] #%d <%p>\n", i, lua_typename(pL, t),
(int)lua_rawlen(pL, i), lua_topointer(pL, i));
break;
}
}
fprintf(stderr, "---------- STACK END ----------\n");
}
static int
luau_panic(lua_State *pL)
{
char *err = (char *)lua_tostring(pL, -1);
luaL_traceback(pL, pL, err, 0);
fprintf(stderr, "Lua error: %s\n\n", lua_tostring(pL, -1));
errx(1, "Fatal Lua error, exiting.");
return 0;
}
static void
luau_init(lua_State **pL)
{
*pL = luaL_newstate();
assert(*pL);
luaL_openlibs(*pL);
luaopen_table(*pL);
luaopen_string(*pL);
luaopen_math(*pL);
luaopen_io(*pL);
lua_atpanic(*pL, luau_panic);
}
static void
luau_call(lua_State *pL, const char *namespace, const char *fnname, size_t nargs, size_t nret)
{
/* get function from namespace. */
if (namespace) {
lua_getglobal(pL, namespace);
lua_getfield(pL, -1, fnname);
lua_remove(pL, -2);
} else {
lua_getglobal(pL, fnname);
}
/* assert function isn't nil */
assert(lua_type(L, lua_gettop(L)) != LUA_TNIL);
/* move function before args. */
lua_insert(pL, -nargs - 1);
/* move error func before function and args. */
size_t errfn_pos = (size_t)lua_gettop(pL) - nargs - 1;
lua_pushcfunction(pL, luau_panic);
lua_insert(pL, -nargs - 2);
if (lua_pcall(pL, nargs, nret, -nargs - 2) == LUA_OK) {
lua_remove(pL, errfn_pos);
}
}
static int
fake_pclose(lua_State *pL)
{
lua_pop(pL, -1);
return 0;
}
static void
call_plugin(size_t func_index, byte_t *buf, size_t buf_sz, size_t offset, FILE *out)
{
char *func_name = strdup(options.dfunc_names[func_index]);
char *plugin_name = func_name;
char *plugin_func = "main";
char *dash = strchr(func_name, '-');
if (dash) {
*dash = '\0';
plugin_func = dash + 1;
}
lua_newtable(L);
for (size_t i = 0; i < buf_sz; ++i) {
lua_pushinteger(L, (lua_Integer)(i + 1));
lua_pushinteger(L, (lua_Integer)buf[i]);
lua_settable(L, -3);
}
lua_pushinteger(L, (lua_Integer)offset);
luaL_Stream *p = (luaL_Stream *)lua_newuserdata(L, sizeof(luaL_Stream));
p->closef = &fake_pclose;
p->f = out;
luaL_setmetatable(L, LUA_FILEHANDLE);
luau_call(L, plugin_name, plugin_func, 3, 0);
free(func_name);
}
// ---
struct ReaderState {
char *data;
size_t size;
};
static const char *
luau_reader(lua_State *pL, void *ud, size_t *size)
{
(void)pL;
struct ReaderState *state = (struct ReaderState *)ud;
if (state->size == 0) return NULL;
*size = state->size;
state->size = 0;
return state->data;
}
static void
luau_evalstring(lua_State *pL, char *name, char *origin, char *stuff)
{
struct ReaderState state = { stuff, strlen(stuff) };
int rload = lua_load(pL, luau_reader, (void *)&state, origin, NULL);
if (rload != LUA_OK) luau_panic(pL);
int reval = lua_pcall(L, 0, LUA_MULTRET, 0);
if (reval != LUA_OK) luau_panic(pL);
lua_setglobal(pL, name);
}
// ---
static int
api_option_linewidth(lua_State *pL)
{
lua_pushinteger(pL, (lua_Integer)options.linelen);
return 1;
}
/*
* Ideally api_color_for_* would be merged into this, too lazy to do that right
* now.
*/
static int
api_color_for(lua_State *pL)
{
lua_Integer a = luaL_checkinteger(pL, 1);
if ((byte_t)a != a) {
lua_pushnil(pL);
} else {
lua_pushinteger(pL, styles[(int)a]);
}
return 1;
}
static int
api_color_for_offset(lua_State *pL)
{
UNUSED(pL);
return style_offset;
}
static int
api_color_for_ascii_borders(lua_State *pL)
{
UNUSED(pL);
return style_lines;
}
static int
api_colors_enabled(lua_State *pL)
{
lua_pushboolean(pL, options._color);
return 1;
}
static const struct luaL_Reg huxdemp_lib[] = {
{ "linewidth", api_option_linewidth },
{ "color_for", api_color_for },
{ "color_for_offset", api_color_for_offset },
{ "color_for_ascii_borders", api_color_for_ascii_borders },
{ "colors_enabled", api_colors_enabled },
{ NULL, NULL },
};
static int
luau_openlib(lua_State *pL)
{
char *lib = (char *) luaL_checkstring(pL, 1);
lua_newtable(L);
if (!strcmp(lib, "huxdemp")) {
luaL_setfuncs(pL, huxdemp_lib, 0);
}
return 1;
}