1 /* Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 */
4
5 #include <dirent.h>
6 #include <lua.h>
7 #include <lauxlib.h>
8 #include <string.h>
9
10
lua_table_checkindices(lua_State * L,const char * keys[])11 const char * lua_table_checkindices(lua_State *L, const char *keys[])
12 {
13 /* Iterate over table at the top of the stack.
14 * http://www.lua.org/manual/5.1/manual.html#lua_next */
15 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
16 lua_pop(L, 1); /* we don't need the value */
17 /* We need to copy the key, as _tostring() confuses _next().
18 * https://www.lua.org/manual/5.1/manual.html#lua_tolstring */
19 lua_pushvalue(L, -1);
20 const char *key = lua_tostring(L, -1);
21 if (!key)
22 return "<NON-STRING_INDEX>";
23 for (const char **k = keys; ; ++k) {
24 if (*k == NULL)
25 return key;
26 if (strcmp(*k, key) == 0)
27 break;
28 }
29 }
30 return NULL;
31 }
32
33 /** Return table listing filenames in a given directory (ls -A). */
kluautil_list_dir(lua_State * L)34 static int kluautil_list_dir(lua_State *L)
35 {
36 lua_newtable(L); // empty table even on errors
37
38 const char *path = lua_tolstring(L, 1, NULL);
39 if (!path) return 1;
40 DIR *dir = opendir(path);
41 if (!dir) return 1;
42
43 struct dirent *entry;
44 int lua_i = 1;
45 while ((entry = readdir(dir)) != NULL) {
46 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
47 lua_pushstring(L, entry->d_name);
48 lua_rawseti(L, -2, lua_i++);
49 }
50 }
51
52 closedir(dir);
53 return 1;
54 }
55
56
57 /* Each of these just creates the correspondingly named lua table of functions. */
58 int kr_bindings_cache (lua_State *L); /* ./cache.c */
59 int kr_bindings_event (lua_State *L); /* ./event.c */
60 int kr_bindings_modules (lua_State *L); /* ./modules.c */
61 int kr_bindings_net (lua_State *L); /* ./net.c */
62 int kr_bindings_worker (lua_State *L); /* ./worker.c */
63
kr_bindings_register(lua_State * L)64 void kr_bindings_register(lua_State *L)
65 {
66 kr_bindings_cache(L);
67 kr_bindings_event(L);
68 kr_bindings_modules(L);
69 kr_bindings_net(L);
70 kr_bindings_worker(L);
71
72 /* Finally some lua utils *written in C*, not really a binding. */
73 lua_register(L, "kluautil_list_dir", kluautil_list_dir);
74 }
75
lua_error_p(lua_State * L,const char * fmt,...)76 void lua_error_p(lua_State *L, const char *fmt, ...)
77 {
78 /* Add a stack trace and throw the result as a lua error. */
79 luaL_traceback(L, L, "error occurred here (config filename:lineno is at the bottom, if config is involved):", 0);
80 /* Push formatted custom message, prepended with "ERROR: ". */
81 lua_pushliteral(L, "\nERROR: ");
82 {
83 va_list args;
84 va_start(args, fmt);
85 lua_pushvfstring(L, fmt, args);
86 va_end(args);
87 }
88 lua_concat(L, 3);
89 lua_error(L);
90 /* TODO: we might construct a little more friendly trace by using luaL_where().
91 * In particular, in case the error happens in a function that was called
92 * directly from a config file (the most common case), there isn't much need
93 * to format the trace in this heavy way. */
94 }
95
96