renamed 'hf mfdes readdata, writedata' to 'read/write'
[RRG-proxmark3.git] / client / deps / liblua / ldblib.c
blobaed9708feeade07d4a7d8692533f1320fb718e02
1 /*
2 ** $Id: ldblib.c,v 1.132 2012/01/19 20:14:44 roberto Exp $
3 ** Interface from Lua to its debug API
4 ** See Copyright Notice in lua.h
5 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
12 #define ldblib_c
13 #define LUA_LIB
15 #include "lua.h"
17 #include "lauxlib.h"
18 #include "lualib.h"
21 #define HOOKKEY "_HKEY"
25 static int db_getregistry(lua_State *L) {
26 lua_pushvalue(L, LUA_REGISTRYINDEX);
27 return 1;
31 static int db_getmetatable(lua_State *L) {
32 luaL_checkany(L, 1);
33 if (!lua_getmetatable(L, 1)) {
34 lua_pushnil(L); /* no metatable */
36 return 1;
40 static int db_setmetatable(lua_State *L) {
41 int t = lua_type(L, 2);
42 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
43 "nil or table expected");
44 lua_settop(L, 2);
45 lua_setmetatable(L, 1);
46 return 1; /* return 1st argument */
50 static int db_getuservalue(lua_State *L) {
51 if (lua_type(L, 1) != LUA_TUSERDATA)
52 lua_pushnil(L);
53 else
54 lua_getuservalue(L, 1);
55 return 1;
59 static int db_setuservalue(lua_State *L) {
60 if (lua_type(L, 1) == LUA_TLIGHTUSERDATA)
61 luaL_argerror(L, 1, "full userdata expected, got light userdata");
62 luaL_checktype(L, 1, LUA_TUSERDATA);
63 if (!lua_isnoneornil(L, 2))
64 luaL_checktype(L, 2, LUA_TTABLE);
65 lua_settop(L, 2);
66 lua_setuservalue(L, 1);
67 return 1;
71 static void settabss(lua_State *L, const char *i, const char *v) {
72 lua_pushstring(L, v);
73 lua_setfield(L, -2, i);
77 static void settabsi(lua_State *L, const char *i, int v) {
78 lua_pushinteger(L, v);
79 lua_setfield(L, -2, i);
83 static void settabsb(lua_State *L, const char *i, int v) {
84 lua_pushboolean(L, v);
85 lua_setfield(L, -2, i);
89 static lua_State *getthread(lua_State *L, int *arg) {
90 if (lua_isthread(L, 1)) {
91 *arg = 1;
92 return lua_tothread(L, 1);
93 } else {
94 *arg = 0;
95 return L;
100 static void treatstackoption(lua_State *L, lua_State *L1, const char *fname) {
101 if (L == L1) {
102 lua_pushvalue(L, -2);
103 lua_remove(L, -3);
104 } else
105 lua_xmove(L1, L, 1);
106 lua_setfield(L, -2, fname);
110 static int db_getinfo(lua_State *L) {
111 lua_Debug ar;
112 int arg;
113 lua_State *L1 = getthread(L, &arg);
114 const char *options = luaL_optstring(L, arg + 2, "flnStu");
115 if (lua_isnumber(L, arg + 1)) {
116 if (!lua_getstack(L1, (int)lua_tointeger(L, arg + 1), &ar)) {
117 lua_pushnil(L); /* level out of range */
118 return 1;
120 } else if (lua_isfunction(L, arg + 1)) {
121 lua_pushfstring(L, ">%s", options);
122 options = lua_tostring(L, -1);
123 lua_pushvalue(L, arg + 1);
124 lua_xmove(L, L1, 1);
125 } else
126 return luaL_argerror(L, arg + 1, "function or level expected");
127 if (!lua_getinfo(L1, options, &ar))
128 return luaL_argerror(L, arg + 2, "invalid option");
129 lua_createtable(L, 0, 2);
130 if (strchr(options, 'S')) {
131 settabss(L, "source", ar.source);
132 settabss(L, "short_src", ar.short_src);
133 settabsi(L, "linedefined", ar.linedefined);
134 settabsi(L, "lastlinedefined", ar.lastlinedefined);
135 settabss(L, "what", ar.what);
137 if (strchr(options, 'l'))
138 settabsi(L, "currentline", ar.currentline);
139 if (strchr(options, 'u')) {
140 settabsi(L, "nups", ar.nups);
141 settabsi(L, "nparams", ar.nparams);
142 settabsb(L, "isvararg", ar.isvararg);
144 if (strchr(options, 'n')) {
145 settabss(L, "name", ar.name);
146 settabss(L, "namewhat", ar.namewhat);
148 if (strchr(options, 't'))
149 settabsb(L, "istailcall", ar.istailcall);
150 if (strchr(options, 'L'))
151 treatstackoption(L, L1, "activelines");
152 if (strchr(options, 'f'))
153 treatstackoption(L, L1, "func");
154 return 1; /* return table */
158 static int db_getlocal(lua_State *L) {
159 int arg;
160 lua_State *L1 = getthread(L, &arg);
161 lua_Debug ar;
162 const char *name;
163 int nvar = luaL_checkint(L, arg + 2); /* local-variable index */
164 if (lua_isfunction(L, arg + 1)) { /* function argument? */
165 lua_pushvalue(L, arg + 1); /* push function */
166 lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
167 return 1;
168 } else { /* stack-level argument */
169 if (!lua_getstack(L1, luaL_checkint(L, arg + 1), &ar)) /* out of range? */
170 return luaL_argerror(L, arg + 1, "level out of range");
171 name = lua_getlocal(L1, &ar, nvar);
172 if (name) {
173 lua_xmove(L1, L, 1); /* push local value */
174 lua_pushstring(L, name); /* push name */
175 lua_pushvalue(L, -2); /* re-order */
176 return 2;
177 } else {
178 lua_pushnil(L); /* no name (nor value) */
179 return 1;
185 static int db_setlocal(lua_State *L) {
186 int arg;
187 lua_State *L1 = getthread(L, &arg);
188 lua_Debug ar;
189 if (!lua_getstack(L1, luaL_checkint(L, arg + 1), &ar)) /* out of range? */
190 return luaL_argerror(L, arg + 1, "level out of range");
191 luaL_checkany(L, arg + 3);
192 lua_settop(L, arg + 3);
193 lua_xmove(L, L1, 1);
194 lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg + 2)));
195 return 1;
199 static int auxupvalue(lua_State *L, int get) {
200 const char *name;
201 int n = luaL_checkint(L, 2);
202 luaL_checktype(L, 1, LUA_TFUNCTION);
203 name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
204 if (name == NULL) return 0;
205 lua_pushstring(L, name);
206 lua_insert(L, -(get + 1));
207 return get + 1;
211 static int db_getupvalue(lua_State *L) {
212 return auxupvalue(L, 1);
216 static int db_setupvalue(lua_State *L) {
217 luaL_checkany(L, 3);
218 return auxupvalue(L, 0);
222 static int checkupval(lua_State *L, int argf, int argnup) {
223 lua_Debug ar;
224 int nup = luaL_checkint(L, argnup);
225 luaL_checktype(L, argf, LUA_TFUNCTION);
226 lua_pushvalue(L, argf);
227 lua_getinfo(L, ">u", &ar);
228 luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index");
229 return nup;
233 static int db_upvalueid(lua_State *L) {
234 int n = checkupval(L, 1, 2);
235 lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
236 return 1;
240 static int db_upvaluejoin(lua_State *L) {
241 int n1 = checkupval(L, 1, 2);
242 int n2 = checkupval(L, 3, 4);
243 luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
244 luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
245 lua_upvaluejoin(L, 1, n1, 3, n2);
246 return 0;
250 #define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
253 static void hookf(lua_State *L, lua_Debug *ar) {
254 static const char *const hooknames[] =
255 {"call", "return", "line", "count", "tail call"};
256 gethooktable(L);
257 lua_pushthread(L);
258 lua_rawget(L, -2);
259 if (lua_isfunction(L, -1)) {
260 lua_pushstring(L, hooknames[(int)ar->event]);
261 if (ar->currentline >= 0)
262 lua_pushinteger(L, ar->currentline);
263 else lua_pushnil(L);
264 lua_assert(lua_getinfo(L, "lS", ar));
265 lua_call(L, 2, 0);
270 static int makemask(const char *smask, int count) {
271 int mask = 0;
272 if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
273 if (strchr(smask, 'r')) mask |= LUA_MASKRET;
274 if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
275 if (count > 0) mask |= LUA_MASKCOUNT;
276 return mask;
280 static char *unmakemask(int mask, char *smask) {
281 int i = 0;
282 if (mask & LUA_MASKCALL) smask[i++] = 'c';
283 if (mask & LUA_MASKRET) smask[i++] = 'r';
284 if (mask & LUA_MASKLINE) smask[i++] = 'l';
285 smask[i] = '\0';
286 return smask;
290 static int db_sethook(lua_State *L) {
291 int arg, mask, count;
292 lua_Hook func;
293 lua_State *L1 = getthread(L, &arg);
294 if (lua_isnoneornil(L, arg + 1)) {
295 lua_settop(L, arg + 1);
296 func = NULL;
297 mask = 0;
298 count = 0; /* turn off hooks */
299 } else {
300 const char *smask = luaL_checkstring(L, arg + 2);
301 luaL_checktype(L, arg + 1, LUA_TFUNCTION);
302 count = luaL_optint(L, arg + 3, 0);
303 func = hookf;
304 mask = makemask(smask, count);
306 if (gethooktable(L) == 0) { /* creating hook table? */
307 lua_pushstring(L, "k");
308 lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
309 lua_pushvalue(L, -1);
310 lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
312 lua_pushthread(L1);
313 lua_xmove(L1, L, 1);
314 lua_pushvalue(L, arg + 1);
315 lua_rawset(L, -3); /* set new hook */
316 lua_sethook(L1, func, mask, count); /* set hooks */
317 return 0;
321 static int db_gethook(lua_State *L) {
322 int arg;
323 lua_State *L1 = getthread(L, &arg);
324 char buff[5];
325 int mask = lua_gethookmask(L1);
326 lua_Hook hook = lua_gethook(L1);
327 if (hook != NULL && hook != hookf) /* external hook? */
328 lua_pushliteral(L, "external hook");
329 else {
330 gethooktable(L);
331 lua_pushthread(L1);
332 lua_xmove(L1, L, 1);
333 lua_rawget(L, -2); /* get hook */
334 lua_remove(L, -2); /* remove hook table */
336 lua_pushstring(L, unmakemask(mask, buff));
337 lua_pushinteger(L, lua_gethookcount(L1));
338 return 3;
342 static int db_debug(lua_State *L) {
343 for (;;) {
344 char buffer[250];
345 luai_writestringerror("%s", "lua_debug> ");
346 if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
347 strcmp(buffer, "cont\n") == 0)
348 return 0;
349 if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
350 lua_pcall(L, 0, 0, 0))
351 luai_writestringerror("%s\n", lua_tostring(L, -1));
352 lua_settop(L, 0); /* remove eventual returns */
357 static int db_traceback(lua_State *L) {
358 int arg;
359 lua_State *L1 = getthread(L, &arg);
360 const char *msg = lua_tostring(L, arg + 1);
361 if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
362 lua_pushvalue(L, arg + 1); /* return it untouched */
363 else {
364 int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0);
365 luaL_traceback(L, L1, msg, level);
367 return 1;
371 static const luaL_Reg dblib[] = {
372 {"debug", db_debug},
373 {"getuservalue", db_getuservalue},
374 {"gethook", db_gethook},
375 {"getinfo", db_getinfo},
376 {"getlocal", db_getlocal},
377 {"getregistry", db_getregistry},
378 {"getmetatable", db_getmetatable},
379 {"getupvalue", db_getupvalue},
380 {"upvaluejoin", db_upvaluejoin},
381 {"upvalueid", db_upvalueid},
382 {"setuservalue", db_setuservalue},
383 {"sethook", db_sethook},
384 {"setlocal", db_setlocal},
385 {"setmetatable", db_setmetatable},
386 {"setupvalue", db_setupvalue},
387 {"traceback", db_traceback},
388 {NULL, NULL}
392 LUAMOD_API int luaopen_debug(lua_State *L) {
393 luaL_newlib(L, dblib);
394 return 1;