]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
Lua stuff add.
authorMiek Gieben <miekg@NLnetLabs.nl>
Wed, 27 Jul 2005 13:57:59 +0000 (13:57 +0000)
committerMiek Gieben <miekg@NLnetLabs.nl>
Wed, 27 Jul 2005 13:57:59 +0000 (13:57 +0000)
I create a new exe which has access to lua and link that with ldns.
I need to stub some ldns functions to see if it will actually work.
You can now "program" ldns from a script which will be run when you
execute lua-rns.

Examples is added which calucales an average (not a ldns functions
of course).

tests/Makefile.in
tests/lua-rns.c [new file with mode: 0644]
tests/rns.lua [new file with mode: 0644]

index 3e9969079a53a4be0acd4de317def960dd76cd04..8d1c711e9a3d96cdb66ff248f5fd72836c9831a6 100644 (file)
@@ -25,6 +25,8 @@ TESTS         = run-test0 run-test7 run-test18 run-test19 run-test20 \
 
 all:   $(TESTS)
 
+lua:   lua-rns
+
 run-test0:       run-test0.o 
                $(LINK) -o $@ $+ 
 
@@ -52,6 +54,11 @@ dname-label-test:       dname-label-test.o
 notify:        notify.o
                $(LINK) -o $@ $+
 
+lua-rns:       lua-rns.o
+               $(LINK) `lua-config --libs` -o $@ $+
+
+lua-rns.o:     lua-rns.c
+               $(COMPILE) `lua-config --include` -c $<
 
 ## implicit rule
 %.o:    %.c $(HEADER)
@@ -60,11 +67,10 @@ notify:     notify.o
 clean:
        rm -f *.o
        rm -f $(TESTS)
+       rm -f lua-rns
 
-realclean:
-       rm -f *.o
+realclean: clean
        rm -rf autom4te.cache/
        rm -f config.log config.status aclocal.m4 config.h.in configure Makefile
        rm -f config.h
-       rm -f $(TESTS)
 
diff --git a/tests/lua-rns.c b/tests/lua-rns.c
new file mode 100644 (file)
index 0000000..6f76262
--- /dev/null
@@ -0,0 +1,113 @@
+/* 
+ * Lua stub to link lua to ldns
+ *
+ * This also exports functions for lua use
+ * partely based upon:
+ * http://tonyandpaige.com/tutorials/lua3.html
+ *
+ * (c) R. Gieben, NLnet Labs
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <stdbool.h>
+
+#include <stdint.h>
+
+/* lua includes */
+#include "lua50/lua.h"
+#include "lua50/lualib.h"
+
+/* ldns include */
+#include <ldns/dns.h>
+
+/* the Lua interpreter */
+lua_State* L;
+
+char *VERSION = "lua-rns 0.1";
+
+void
+usage(FILE *f, char *progname)
+{
+       fprintf(f, "Synopsis: %s lua-file\n", progname);
+       fprintf(f, "   Useless bunch of other options\n");
+}
+
+void
+version(FILE *f, char *progname)
+{
+       fprintf(f, "%s version %s\n", progname, VERSION);
+}
+
+/*
+ * Encapsulate our ldns function in a lua form
+ * Start of Defintions
+ */
+
+/* Test function which doesn't call ldns stuff yet */
+static int 
+lua_ldns_average(lua_State *L)
+{
+       int n = lua_gettop(L);
+       double sum = 0;
+       int i;
+
+       /* loop through each argument */
+       for (i = 1; i <= n; i++)
+       {
+               /* total the arguments */
+               sum += lua_tonumber(L, i);
+       }
+
+       /* push the average */
+       lua_pushnumber(L, sum / n);
+
+       /* push the sum */
+       lua_pushnumber(L, sum);
+
+       /* return the number of results */
+       return 2;
+}
+
+
+/*
+ * End of Defintions
+ * Encapsulate our ldns function in a lua form
+ */
+void
+register_ldns_functions(void)
+{
+        /* register our functions */
+
+       /* need to encap. all used functions in a
+        * still lua can understand
+        */
+        lua_register(L, "lua_ldns_average", lua_ldns_average);
+}
+
+int
+main(int argc, char *argv[])
+{
+       if (argc != 2) {
+               usage(stderr, argv[0]);
+               exit(EXIT_FAILURE);
+       }
+
+       if (access(argv[1], R_OK)) {
+               fprintf(stderr, "File %s is unavailable.\n", argv[1]);
+               exit(EXIT_FAILURE);
+       }
+       
+        L = lua_open();
+        lua_baselibopen(L);
+
+       register_ldns_functions();
+
+        /* run the script */
+        lua_dofile(L, argv[1]);
+
+        lua_close(L);
+        exit(EXIT_SUCCESS);
+}
diff --git a/tests/rns.lua b/tests/rns.lua
new file mode 100644 (file)
index 0000000..8da458d
--- /dev/null
@@ -0,0 +1,6 @@
+-- call a C function
+
+avg, sum = lua_ldns_average(10, 20, 30, 40, 50)
+
+print("The average is ", avg)
+print("The sum is ", sum)