summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anylike.org>2009-12-27 07:27:06 +0000
committerChristian Pointner <equinox@anylike.org>2009-12-27 07:27:06 +0000
commit6fd5495cf8019307e463fa3e2deeb0cd1eb68298 (patch)
treec817e6d4f68c51124291dfda38ae4e8ed3be6782
parentmoved echo server to seperate file (diff)
moved from loadfile to loadbuffer
bytecode gets embedded in c binary
-rw-r--r--src/Makefile30
-rw-r--r--src/anylike.c14
-rw-r--r--src/bin2c.lua53
-rwxr-xr-xsrc/configure5
4 files changed, 86 insertions, 16 deletions
diff --git a/src/Makefile b/src/Makefile
index c32fbbe..29e95aa 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -29,16 +29,17 @@ endif
EXECUTABLE := anylike
LUA_OBJ := $(EXECUTABLE).lc
+LUA_OBJ_INCLUDE := $(EXECUTABLE)_lua_bytecode.h
# sig_handler.o \
# sysexec.o \
C_OBJS := log.o \
- l_log.o \
- options.o \
- string_list.o \
- l_crypt.o \
- anylike.o
+ l_log.o \
+ options.o \
+ string_list.o \
+ l_crypt.o \
+ anylike.o
C_SRCS := $(C_OBJS:%.o=%.c)
@@ -49,11 +50,18 @@ LUA_SRCS := main_loop.lua \
all: $(EXECUTABLE) $(LUA_OBJ)
+define create-d-file
+ @set -e; rm -f $1; \
+ $(CC) -MM $(CFLAGS) $2 > $1.$$$$; \
+ sed 's,\($*\)\.o[ :]*,\1.o $1 : ,g' < $1.$$$$ > $1; \
+ rm -f $1.$$$$; echo '(re)building $1'
+endef
+
+anylike.d: anylike.c $(LUA_OBJ_INCLUDE)
+ $(call create-d-file,$@,$<)
+
%.d: %.c
- @set -e; rm -f $@; \
- $(CC) -MM $(CFLAGS) $< > $@.$$$$; \
- sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
- rm -f $@.$$$$; echo '(re)building $@'
+ $(call create-d-file,$@,$<)
ifneq ($(MAKECMDGOALS),distclean)
-include $(C_SRCS:%.c=%.d)
@@ -68,6 +76,9 @@ $(EXECUTABLE): $(C_OBJS)
$(LUA_OBJ): $(LUA_SRCS)
$(LUAC) -o $@ $(LUA_SRCS)
+$(LUA_OBJ_INCLUDE): $(LUA_OBJ)
+ $(LUA) bin2c.lua $(LUA_OBJ) $(LUA_OBJ_INCLUDE:%.h=%) > $@
+
strip: $(EXECUTABLE) $(LUA_OBJ)
$(STRIP) -s $(EXECUTABLE)
$(LUAC) -s -o $(LUA_OBJ) $(LUA_OBJ)
@@ -83,6 +94,7 @@ clean:
rm -f *.d
rm -f *.d.*
rm -f *.lc
+ rm -f $(LUA_OBJ_INCLUDE)
rm -f $(EXECUTABLE)
cleanall: clean
diff --git a/src/anylike.c b/src/anylike.c
index bd5ed4b..8ee0a61 100644
--- a/src/anylike.c
+++ b/src/anylike.c
@@ -73,8 +73,8 @@ int init_libgcrypt()
}
#endif
+#include "anylike_lua_bytecode.h"
-#define LUA_OBJ_FILE "anylike.lc"
#define LUA_MAIN_LOOP_FUNC "main_loop"
static const luaL_Reg anylike_lualibs[] = {
@@ -97,14 +97,14 @@ int init_main_loop(lua_State *L)
lua_call(L, 1, 0);
}
- int ret = luaL_loadfile(L, LUA_OBJ_FILE);
+ int ret = luaL_loadbuffer(L, anylike_lua_bytecode, sizeof(anylike_lua_bytecode), "anylike");
if(ret) {
const char* err_str = luaL_checkstring(L, -1);
switch(ret) {
- case LUA_ERRSYNTAX: log_printf(ERROR, "luaL_loadfile(%s) syntax error: %s", LUA_OBJ_FILE, err_str); break;
- case LUA_ERRMEM: log_printf(ERROR, "luaL_loadfile(%s) malloc error: %s", LUA_OBJ_FILE, err_str); break;
- case LUA_ERRFILE: log_printf(ERROR, "luaL_loadfile(%s) file access error: %s", LUA_OBJ_FILE, err_str); break;
- default: log_printf(ERROR, "luaL_loadfile(%s) unknown error: %s", LUA_OBJ_FILE, err_str); break;
+ case LUA_ERRSYNTAX: log_printf(ERROR, "luaL_loadbuffer() syntax error: %s", err_str); break;
+ case LUA_ERRMEM: log_printf(ERROR, "luaL_loadbuffer() malloc error: %s", err_str); break;
+ case LUA_ERRFILE: log_printf(ERROR, "luaL_loadbuffer() file access error: %s", err_str); break;
+ default: log_printf(ERROR, "luaL_loadbuffer() unknown error: %s", err_str); break;
}
return -1;
}
@@ -127,7 +127,7 @@ int call_main_loop(lua_State* L, options_t* opt)
{
lua_getglobal(L, LUA_MAIN_LOOP_FUNC);
if(!lua_isfunction(L, -1)) {
- log_printf(ERROR, "there is no function '%s' in '%s'", LUA_MAIN_LOOP_FUNC, LUA_OBJ_FILE);
+ log_printf(ERROR, "there is no function '%s' inside anylike bytecode", LUA_MAIN_LOOP_FUNC);
return -1;
};
diff --git a/src/bin2c.lua b/src/bin2c.lua
new file mode 100644
index 0000000..0adf427
--- /dev/null
+++ b/src/bin2c.lua
@@ -0,0 +1,53 @@
+--
+-- anylike
+--
+-- anylike is a ...
+--
+--
+-- Copyright (C) 2007-2008 Markus Grueneis <gimpf@anylike.org>
+-- Christian Pointner <equinox@anylike.org>
+--
+-- This file is part of anylike.
+--
+-- anylike is free software: you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation, either version 3 of the License, or
+-- any later version.
+--
+-- anylike is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with anylike. If not, see <http://www.gnu.org/licenses/>.
+--
+--
+-- the following is inspired by BinTwoCee from the lua-users wiki.
+-- http://lua-users.org/wiki/BinToCee
+--
+
+if not arg or not arg[1] or not arg[2] then
+ io.stderr:write("Usage: lua bin2c.lua filename c_var_name\n")
+ return
+end
+
+local filename, c_var_name = ...
+
+local content = assert(io.open(filename,"rb")):read"*a"
+
+local dump do
+ local numtab={}; for i=0,255 do numtab[string.char(i)]=("%3d,"):format(i) end
+ function dump(str)
+ return (str:gsub(".", numtab):gsub(("."):rep(80), "%0\n"))
+ end
+end
+
+io.write([=[
+/* code automatically generated by bin2c -- DO NOT EDIT */
+
+static const u_int8_t ]=] , c_var_name, [=[[]={
+]=], dump(content), [=[
+
+};
+]=])
diff --git a/src/configure b/src/configure
index 49562cf..ab40134 100755
--- a/src/configure
+++ b/src/configure
@@ -31,6 +31,7 @@ LDFLAGS='-g -Wall -O2 -ldl -lm'
CRYPTO_LIB='gcrypt'
LUA_DIR=''
+LUA=''
LUAC=''
PREFIX='/usr/local'
@@ -159,6 +160,7 @@ if [ -z "$LUA_DIR" ]; then
echo "using Lua $LUA_VERSION ($LUA_RELEASE) found at $prefix/include"
CFLAGS="$CFLAGS -I$prefix/include"
LDFLAGS="$LDFLAGS -L$prefix/lib -llua"
+ LUA=$prefix/lua
LUAC=$prefix/luac
break
fi
@@ -170,6 +172,7 @@ if [ -z "$LUA_DIR" ]; then
echo "using Lua $LUA_VERSION ($LUA_RELEASE) found at $dir"
CFLAGS="$CFLAGS -I$dir"
LDFLAGS="$LDFLAGS -L$prefix/lib -llua$LUA_VERSION"
+ LUA=$prefix/bin/lua$LUA_VERSION
LUAC=$prefix/bin/luac$LUA_VERSION
break
fi
@@ -189,6 +192,7 @@ if [ -z "$LUA_DIR" ]; then
else
CFLAGS="$CFLAGS -I$LUA_DIR/include"
LDFLAGS="$LDFLAGS $LUA_DIR/lib/liblua.a"
+ LUA=$LUA_DIR/bin/lua
LUAC=$LUA_DIR/bin/luac
fi
@@ -217,6 +221,7 @@ TARGET := $TARGET
CC := gcc
CFLAGS := $CFLAGS
LDFLAGS := $LDFLAGS
+LUA := $LUA
LUAC := $LUAC
STRIP := strip
INSTALL := install