diff options
author | Nathan Angelacos <nangel@alpinelinux.org> | 2008-12-17 01:34:15 +0000 |
---|---|---|
committer | Nathan Angelacos <nangel@alpinelinux.org> | 2008-12-17 01:34:15 +0000 |
commit | 64b823303af9dcb002370d0611c5783a8c610442 (patch) | |
tree | 883d4d70a5a4f3c593063db13c1e1c2e19b52129 /src/lua2c.c | |
parent | b6de69e02e1a4b2ffebc93cc609bbd000949591d (diff) | |
download | haserl-64b823303af9dcb002370d0611c5783a8c610442.tar.bz2 haserl-64b823303af9dcb002370d0611c5783a8c610442.tar.xz |
Reverted back to 0.9.24 codebase, releasing 0.9.25 off of it. 0.9.25 pre had new llist libraries that seem to be borked
Diffstat (limited to 'src/lua2c.c')
-rw-r--r-- | src/lua2c.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/lua2c.c b/src/lua2c.c new file mode 100644 index 0000000..f1f7abe --- /dev/null +++ b/src/lua2c.c @@ -0,0 +1,97 @@ +/* -------------------------------------------------------------------------- + * a simple lua 2 c function converter - a simple luac + bin2c + * Copyright (c) 2007 Nathan Angelacos (nangel@users.sourceforge.net) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, + * as published by the Free Software Foundation. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * ------------------------------------------------------------------------- */ + + +/* This program loads the source file, and then uses lua_dump to output it + * to a c const char array. Because lua_dump is used instead of the internal + * luaU_dump function, debugging info is in the output array. If you want + * to compile the array without debugging information, first use luac on the + * .lua source, and then run lua2c on that output: + * luac -s -o foo haserl_lualib.lua + * lua2c haserl_lualib foo >haserl_lualib.inc + */ + +#if HAVE_CONFIG_H +#include <config.h> +#endif + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include <lua.h> +#include <lauxlib.h> +#include <lualib.h> + +lua_State *lua_vm = NULL; + + +static void +loadit (char *filename) +{ + +lua_vm = luaL_newstate(); +luaL_openlibs (lua_vm); + +if (luaL_loadfile(lua_vm, filename)) { + puts (lua_tostring(lua_vm, -1)); + exit (-1); + } + +} + +static int writer (lua_State* L, const void* p, size_t size, void* u) +{ + static int count = 0; + int i; + for (i=0; i < size; i++ ) { + if ((count) && (count % 16) == 0 ) printf ("\n "); + printf ("%3d,", *((unsigned char *) (p+i))); + count++; + } + + return (0); +} + + +static void +dumpit() +{ +lua_dump (lua_vm, writer, NULL); +} + + +int +main (int argc, char *argv[]) { + if (argc != 3) { + printf("usage: %s varname luasource >output\n", argv[0]); + return (-1); + } + + loadit (argv[2]); + + + printf ("/* This file was automatically generated from %s. DO NOT EDIT */\n\n", argv[2]); + printf ("static const unsigned char %s[] = { \n ", argv[1]); + dumpit(); + printf ("\n};\n"); + + return (0); +} |