2013-06-22  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* src/luaconf.h [LUA_USE_DJGPP]: Define macro LUA_TMPNAMBUFSIZE
	to FILENAME_MAX.

	* src/loslib.c [LUA_USE_MKSTEMP]: If LUA_TMPNAME_TEMPLATE undefined
	use standard value (aka 32) for LUA_TMPNAMBUFSIZE else use the
	provided one from luaconf.h


2013-06-17  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* src/loadlib.c [LUA_DL_DXE3, LUA_COMPAT_MODULE]:  Keep luaL_register
	for compatibility with old module system.
	lua_createtable and luaL_setfuncs added to DXE_EXPORT table.

	* src/loslib.c [LUA_USE_MKSTEMP]: If LUA_TMPNAME_TEMPLATE undefined use
	standard tmpname template (aka /tmp/lua_XXXXXX) else use the provided
	one from luaconf.h.

	* src/luaconf.h [LUA_USE_DJGPP]: Define new macro LUA_TMPNAME_TEMPLATE.
	For DJGPP it creates a file name template for temporary files checking
	the environment variables TMPDIR, TMP and TEMP, in that sequence.  For
	the first variable that is not empty, the returned string is tested to
	check if it points to an existing directory.  If it is false the value
	of P_tmpdir is used.  The template "luXXXXXX" is appended to the path.
	[LUA_USE_DJGPP]: Define LUA_USE_MKSTEMP, LUA_USE_ISATTY, LUA_USE_POPEN,
	LUA_USE_DXE3, LUA_USE_READLINE, LUA_USE_STRTODHEX, LUA_USE_AFORMAT and
	LUA_USE_LONGLONG.
	[LUA_USE_DXE3]: Define LUA_DL_DXE3.
	[__DJGPP__]: Define LUA_ROOT, LUA_LDIR, LUA_CDIR, LUA_PATH_DEFAULT,
	LUA_CPATH_DEFAULT and  LUA_VDI.

	* src/loadlib.c [LUA_DL_DXE3]:  Use DJGPP's DXE3 functionality to
	implement loadlib.


2013-06-16  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* Makefile: Set INSTALL_TOP to /dev/envDJDIR.
	Add SHELL variable.
	Add EXEEXT variable.
	Add djgpp to the known platforms.
	Added test_djgpp target.

	* djgpp/libtest1.c:  Function to create a .so library.

	* djgpp/libtest1.lua:  Function to load a .so library.

	* djgpp/libtest2.c:  Function to create a .so library.

	* djgpp/libtest2.lua:  Function to load a .so library.

	* djgpp/libtest3.c:  Function to create a .so library.

	* djgpp/libtest3.lua:  Function to load a .so library.

	* src/Makefile: Add djgpp target.
	Add EXEEXT variable.
	Add djgpp to the known platforms.
	Add link option for readline.






diff -aprNU5 lua-5.2.2.orig/Makefile lua-5.2.2/Makefile
--- lua-5.2.2.orig/Makefile	2012-05-17 14:05:54 +0100
+++ lua-5.2.2/Makefile	2013-06-23 14:43:28 +0100
@@ -1,18 +1,19 @@
 # Makefile for installing Lua
 # See doc/readme.html for installation and customization instructions.
 
 # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
+SHELL= /bin/sh
 
 # Your platform. See PLATS for possible values.
 PLAT= none
 
 # Where to install. The installation starts in the src and doc directories,
 # so take care if INSTALL_TOP is not an absolute path. See the local target.
 # You may want to make INSTALL_LMOD and INSTALL_CMOD consistent with
 # LUA_ROOT, LUA_LDIR, and LUA_CDIR in luaconf.h.
-INSTALL_TOP= /usr/local
+INSTALL_TOP= /dev/env/DJDIR
 INSTALL_BIN= $(INSTALL_TOP)/bin
 INSTALL_INC= $(INSTALL_TOP)/include
 INSTALL_LIB= $(INSTALL_TOP)/lib
 INSTALL_MAN= $(INSTALL_TOP)/man/man1
 INSTALL_LMOD= $(INSTALL_TOP)/share/lua/$V
@@ -31,17 +32,26 @@ INSTALL_DATA= $(INSTALL) -m 0644
 
 # Other utilities.
 MKDIR= mkdir -p
 RM= rm -f
 
+# Define the program extension.
+ifeq ($(PLAT), djgpp)
+  EXEEXT= .exe
+else ifneq ($(DJGPP),)
+  EXEEXT= .exe
+else
+  EXEEXT=
+endif
+
 # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE =======
 
 # Convenience platforms targets.
-PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris
+PLATS= aix ansi bsd djgpp freebsd generic linux macosx mingw posix solaris
 
 # What to install.
-TO_BIN= lua luac
+TO_BIN= lua$(EXEEXT) luac$(EXEEXT)
 TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp
 TO_LIB= liblua.a
 TO_MAN= lua.1 luac.1
 
 # Lua version and release.
@@ -53,11 +63,39 @@ all:	$(PLAT)
 
 $(PLATS) clean:
 	cd src && $(MAKE) $@
 
 test:	dummy
-	src/lua -v
+	src/lua$(EXEEXT) -v
+
+djgpp_clean: clean
+	cd djgpp && rm -f *.o *.so
+
+test_djgpp:	dummy
+	# Check #1
+	gcc -O2 -Wall -c -o ./djgpp/libtest1.o ./djgpp/libtest1.c
+	# Linked against libc to resolve cos and sin.
+	# Is OK because it does not introduce new unresolved symbols.
+	dxe3gen -U -o ./djgpp/libtest1.so ./djgpp/libtest1.o -lc -E _lib_
+	src/lua$(EXEEXT) djgpp/libtest1.lua
+	#================================================================
+	#
+	# Check #2
+	gcc -O2 -Wall -c -o ./djgpp/libtest2.o ./djgpp/libtest2.c
+	# Linked against libc to resolve strcmp.
+	# Is OK because it does not introduce new unresolved symbols.
+	dxe3gen -U -o ./djgpp/libtest2.so ./djgpp/libtest2.o -lc -E _luaopen_
+	LUA_CPATH="./djgpp/?.so"; export LUA_CPATH; src/lua$(EXEEXT) djgpp/libtest2.lua
+	#================================================================
+	#
+	# Check #3
+	gcc -O2 -Wall -c -o ./djgpp/libtest3.o ./djgpp/libtest3.c
+	# Linked against libc to resolve strcmp.
+	# Is OK because it does not introduce new unresolved symbols.
+	dxe3gen -U -o ./djgpp/libtest3.so ./djgpp/libtest3.o -lc -E _luaopen_
+	LUA_CPATH="./djgpp/?.so"; export LUA_CPATH; src/lua$(EXEEXT) djgpp/libtest3.lua
+	#================================================================
 
 install: dummy
 	cd src && $(MKDIR) $(INSTALL_BIN) $(INSTALL_INC) $(INSTALL_LIB) $(INSTALL_MAN) $(INSTALL_LMOD) $(INSTALL_CMOD)
 	cd src && $(INSTALL_EXEC) $(TO_BIN) $(INSTALL_BIN)
 	cd src && $(INSTALL_DATA) $(TO_INC) $(INSTALL_INC)
diff -aprNU5 lua-5.2.2.orig/djgpp/libtest1.c lua-5.2.2/djgpp/libtest1.c
--- lua-5.2.2.orig/djgpp/libtest1.c	1970-01-01 01:00:00 +0100
+++ lua-5.2.2/djgpp/libtest1.c	2013-06-23 14:43:28 +0100
@@ -0,0 +1,18 @@
+#include <math.h>
+#include "../src/lua.h"
+
+int lib_compute_sin(lua_State *L)
+{
+  double argument = lua_tonumber(L, 1);
+  lua_pushnumber(L, sin(argument));
+
+  return 1;  /* One element returned on stack. */
+}
+
+int lib_compute_cos(lua_State *L)
+{
+  double argument = lua_tonumber(L, 1);
+  lua_pushnumber(L, cos(argument));
+
+  return 1;  /* One element returned on stack. */
+}
diff -aprNU5 lua-5.2.2.orig/djgpp/libtest1.lua lua-5.2.2/djgpp/libtest1.lua
--- lua-5.2.2.orig/djgpp/libtest1.lua	1970-01-01 01:00:00 +0100
+++ lua-5.2.2/djgpp/libtest1.lua	2013-06-23 14:43:28 +0100
@@ -0,0 +1,17 @@
+-- lua program to test dynamic library support with DJGPP.
+
+so_library = "./djgpp/libtest1.so"
+sin_function = "_lib_compute_sin"
+cos_function = "_lib_compute_cos"
+print(string.format('Test of package.loadlib() for \"%s\" and functions\n\"%s\" and \"%s\".', so_library, sin_function, cos_function))
+print("Computes the sin and cos values 0, 30, 60 and 90 degrees." )
+sin_function = package.loadlib(so_library, sin_function)
+cos_function = package.loadlib(so_library, cos_function)
+pi_half = 1.5707963267948966192313216916398
+step = pi_half / 3
+for x = 0, pi_half, step do
+  local sin_value = sin_function(x)
+  local cos_value = cos_function(x)
+  local arg = x * 90 / pi_half
+  print(string.format('sin(%f) = %f  cos(%f) = %f', arg, sin_value, arg, cos_value))
+end
diff -aprNU5 lua-5.2.2.orig/djgpp/libtest2.c lua-5.2.2/djgpp/libtest2.c
--- lua-5.2.2.orig/djgpp/libtest2.c	1970-01-01 01:00:00 +0100
+++ lua-5.2.2/djgpp/libtest2.c	2013-06-23 14:43:28 +0100
@@ -0,0 +1,59 @@
+#include <math.h>
+#include <string.h>
+
+#define LUA_COMPAT_ALL
+#include "../src/lauxlib.h"
+
+static double compute_sin(const double x)
+{
+  return sin(x);
+}
+
+static double compute_cos(const double x)
+{
+  return cos(x);
+}
+
+static int compute_trig_function(lua_State *L)
+{
+  const double argument = lua_tonumber(L, 1);
+  const char *selected_function = lua_tostring(L, 2);
+  double result;
+
+  if (strcmp(selected_function, "sin") == 0)
+    result = compute_sin(argument);
+  else if (strcmp(selected_function, "cos") == 0)
+    result = compute_cos(argument);
+  else
+    return 0;
+
+  lua_pushnumber(L, result);
+
+  return 1;  /* One element returned on stack. */
+}
+
+static const struct luaL_Reg djgpp_DXE3_lib[] = {
+  {"compute_trig_function", compute_trig_function},
+  {NULL, NULL}
+};
+
+int luaopen_libtest2(lua_State *L)
+{
+#if 1
+  /*  Deprecated since 5.2.0.  Available if Lua is compiled with LUA_COMPAT_MODULE.  */
+  luaL_register(L, "djgpp_DXE3_lib", djgpp_DXE3_lib);  /* Register all functions belonging to djgpp_DXE3_lib module. */
+#else
+#if 0
+  luaL_newlib(L, djgpp_DXE3_lib);  /* Register all functions belonging to djgpp_DXE3_lib module. */
+#else
+  // get global table onto the stack
+  lua_pushglobaltable(L);
+  // populate the table on top of the stack with module functions
+  luaL_setfuncs(L, djgpp_DXE3_lib, 0);
+  // since we don't have a module table, don't return anything
+  // require() will return true instead
+  return 0;
+#endif
+#endif
+  return 1;
+}
diff -aprNU5 lua-5.2.2.orig/djgpp/libtest2.lua lua-5.2.2/djgpp/libtest2.lua
--- lua-5.2.2.orig/djgpp/libtest2.lua	1970-01-01 01:00:00 +0100
+++ lua-5.2.2/djgpp/libtest2.lua	2013-06-23 14:43:28 +0100
@@ -0,0 +1,13 @@
+-- lua program to test dynamic library support with DJGPP.
+
+require "libtest2"
+print(string.format('Test of require \"libtest2.so\"\nThe library registers the function names contained in the\n\"djgpp_DXE3_lib\" table allowing to use \"compute_trig_function\"\nto access \"compute_cos\" and \"compute_sin\".'))
+print("Computes the sin and cos values 0, 30, 60 and 90 degrees." )
+pi_half = 1.5707963267948966192313216916398
+step = pi_half / 3
+for x = 0, pi_half, step do
+  local sin_value = djgpp_DXE3_lib.compute_trig_function(x, "sin")
+  local cos_value = djgpp_DXE3_lib.compute_trig_function(x, "cos")
+  local arg = x * 90 / pi_half
+  print(string.format('sin(%f) = %f  cos(%f) = %f', arg, sin_value, arg, cos_value))
+end
diff -aprNU5 lua-5.2.2.orig/djgpp/libtest3.c lua-5.2.2/djgpp/libtest3.c
--- lua-5.2.2.orig/djgpp/libtest3.c	1970-01-01 01:00:00 +0100
+++ lua-5.2.2/djgpp/libtest3.c	2013-08-18 17:18:50 +0100
@@ -0,0 +1,107 @@
+#define LUA_COMPAT_ALL
+#include "../src/lauxlib.h"
+
+
+
+/*  Definition of 3D vector.  */
+typedef struct vector_3D_t {
+  double x;
+  double y;
+  double z;
+} vector_3D;
+
+
+/*  Function defining a new vector.  */
+static int define(lua_State *L)
+{
+  vector_3D *vec3D;
+
+  /*  Check if there are valid vector components on the stack.  */
+  if (lua_isnumber(L, 1) && lua_isnumber(L, 2) && lua_isnumber(L, 3))
+  {
+    /*  Allocate a space for a new vector.  */
+    vec3D = (vector_3D *)lua_newuserdata(L, sizeof(vector_3D));
+
+    /*  Read vector components from stack.  */
+    vec3D->x = lua_tonumber(L, 1);
+    vec3D->y = lua_tonumber(L, 2);
+    vec3D->z = lua_tonumber(L, 3);
+
+    /*  Assign userdata to metatable.  */
+    luaL_getmetatable(L, "meta_table.vector_3D");
+    lua_setmetatable(L, -2);  
+
+    return 1; /*  Number of elements returned on the stack.  */
+  }
+  else
+    return luaL_error(L, "error defining new vector_3D");
+}
+
+
+/*  Add two vectors.  */
+static int add(lua_State *L)
+{
+  vector_3D *sum;
+
+  /*  Check if there are valid vectors on the stack.  */
+  vector_3D *a = luaL_checkudata(L, 1, "meta_table.vector_3D");
+  vector_3D *b = luaL_checkudata(L, 2, "meta_table.vector_3D");
+  
+  /*  Compute result */
+  sum = (vector_3D *)lua_newuserdata(L, sizeof(vector_3D));
+  sum->x = a->x + b->x;
+  sum->y = a->y + b->y;
+  sum->z = a->z + b->z;  
+  
+  /*  Assign userdata to metatable.  */
+  luaL_getmetatable(L, "meta_table.vector_3D");
+  lua_setmetatable(L, -2);  
+
+  return 1; /*  Number of elements returned on the stack.  */
+}
+
+
+/*  Generate string representation of the vector.  */
+static int tostring(lua_State *L)
+{
+  /*  Check if there are valid vector components on the stack.  */
+  vector_3D *vec3D = luaL_checkudata(L, 1, "meta_table.vector_3D");
+  
+  /*  Push result on stack.  */
+  lua_pushfstring(L, "(%f, %f, %f)", vec3D->x, vec3D->y, vec3D->z);
+  
+  return 1; /*  Number of elements returned on the stack.  */
+}
+
+
+/* Table entries that will be accessible by lua.  */
+/* Array of paires of function names and function pointers.  */
+static const struct luaL_Reg vector_3D_func[] = {
+  {"define", define},  /*  Define new vector.  */
+  {NULL, NULL}         /*  End of register.  */
+};
+
+
+/* Meta table entries.  */
+/* Array of paires of function names and function pointers.  */
+static const struct luaL_Reg vector_3D_meta[] = {
+  {"__add", add},           /* Metamethode zum addieren */
+  {"__tostring", tostring}, /* Metamethode um Stringreprsentation zu erzeugen */
+  {NULL, NULL}              /*  End of register.  */
+};
+
+
+/* Will be called when library is opened.  */
+int luaopen_libtest3(lua_State *L)
+{
+  /*  Create a meta table with name "meta_table.vector_3D" in the registery.  */
+  luaL_newmetatable(L, "meta_table.vector_3D");
+  
+  /*  Create entries in meta table.  */
+  luaL_register(L, NULL, vector_3D_meta);
+  
+  /*  Create entries for the vector operation table (accessible from lua).  */
+  luaL_register(L, "vec3D", vector_3D_func);
+  
+  return 1;
+}
diff -aprNU5 lua-5.2.2.orig/djgpp/libtest3.lua lua-5.2.2/djgpp/libtest3.lua
--- lua-5.2.2.orig/djgpp/libtest3.lua	1970-01-01 01:00:00 +0100
+++ lua-5.2.2/djgpp/libtest3.lua	2013-08-18 17:36:42 +0100
@@ -0,0 +1,17 @@
+-- lua program to test dynamic library support with DJGPP.
+
+require "libtest3"
+
+print(string.format('Test of \"user data\" and \"meta table\".\nThe library registers the function names contained in the\n\"vector_3D_meta\" and \"vector_3D_func\" tables allowing to use\nthe functions defined there to perform vector operations.'))
+print("Computes the sum of two 3D vectors previously defined." )
+
+a = vec3D.define(1, 2, 3)
+b = vec3D.define(3, 2 ,1)
+c = a + b
+
+print(a)
+print("+")
+print(b)
+print("=")
+print(c)
+
diff -aprNU5 lua-5.2.2.orig/src/Makefile lua-5.2.2/src/Makefile
--- lua-5.2.2.orig/src/Makefile	2012-12-27 10:51:42 +0100
+++ lua-5.2.2/src/Makefile	2013-06-23 14:43:28 +0100
@@ -24,28 +24,36 @@ MYLDFLAGS=
 MYLIBS=
 MYOBJS=
 
 # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE =======
 
-PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris
+PLATS= aix ansi bsd djgpp freebsd generic linux macosx mingw posix solaris
 
 LUA_A=	liblua.a
 CORE_O=	lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \
 	lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \
 	ltm.o lundump.o lvm.o lzio.o
 LIB_O=	lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o liolib.o \
 	lmathlib.o loslib.o lstrlib.o ltablib.o loadlib.o linit.o
 BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS)
 
+ifeq ($(PLAT), djgpp)
+  EXEEXT= .exe
+else ifneq ($(DJGPP),)
+  EXEEXT= .exe
+else
+  EXEEXT=
+endif
+
 LUA_T=	lua
 LUA_O=	lua.o
 
 LUAC_T=	luac
 LUAC_O=	luac.o
 
 ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O)
-ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T)
+ALL_T= $(LUA_A) $(LUA_T)$(EXEEXT) $(LUAC_T)$(EXEEXT)
 ALL_A= $(LUA_A)
 
 # Targets start here.
 default: $(PLAT)
 
@@ -57,14 +65,14 @@ a:	$(ALL_A)
 
 $(LUA_A): $(BASE_O)
 	$(AR) $@ $(BASE_O)
 	$(RANLIB) $@
 
-$(LUA_T): $(LUA_O) $(LUA_A)
+$(LUA_T)$(EXEEXT): $(LUA_O) $(LUA_A)
 	$(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)
 
-$(LUAC_T): $(LUAC_O) $(LUA_A)
+$(LUAC_T)$(EXEEXT): $(LUAC_O) $(LUA_A)
 	$(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS)
 
 clean:
 	$(RM) $(ALL_T) $(ALL_O)
 
@@ -95,10 +103,13 @@ ansi:
 	$(MAKE) $(ALL) SYSCFLAGS="-DLUA_ANSI"
 
 bsd:
 	$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E"
 
+djgpp:
+	$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_DJGPP" SYSLIBS="-lreadline -lhistory"
+
 freebsd:
 	$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -lreadline"
 
 generic: $(ALL)
 
diff -aprNU5 lua-5.2.2.orig/src/loadlib.c lua-5.2.2/src/loadlib.c
--- lua-5.2.2.orig/src/loadlib.c	2012-05-30 12:33:44 +0100
+++ lua-5.2.2/src/loadlib.c	2013-08-18 17:06:16 +0100
@@ -84,11 +84,16 @@
 #define LUA_LSUBSEP		LUA_DIRSEP
 #endif
 
 
 /* prefix for open functions in C libraries */
+#if defined(LUA_DL_DXE3)
+/* DJGPP prepends a `_' before C function names */
+#define LUA_POF		"_luaopen_"
+#else
 #define LUA_POF		"luaopen_"
+#endif
 
 /* separator for open functions in C libraries */
 #define LUA_OFSEP	"_"
 
 
@@ -210,10 +215,156 @@ static lua_CFunction ll_sym (lua_State *
 }
 
 /* }====================================================== */
 
 
+
+#elif defined(LUA_DL_DXE3)
+/*
+** {========================================================================
+** This is an implementation of loadlib based on DXE3 functionality provided
+** by DJGPP.  It emulates the dlfcn interface.  Due to the nature of DXE3 it
+** is necessary to register all unresolved symbols of all the libraries that
+** may be loaded by the user. This is impossible to anticipate, so all C-API
+** functions that are used to access (push and pop) the stack have been put
+** on the list.  Also luaL_register, lua_rawgeti and luaL_setfuncs have been
+** added to the list to allow to register modules.
+** If you use this DJGPP port of Lua and you note that functions are missed
+** in this list, add them to the list and recompile the sources.
+** You can send me a list of missed functions and I will add them in the
+** next release of this port.
+** Juan M. Guerrero  <juan.guerrero@gmx.de>
+** =========================================================================
+*/
+
+#include <dlfcn.h>
+#include <sys/dxe.h>
+
+
+DXE_EXPORT_TABLE(exported_symbols)
+/*
+** Functions lua_equal and lua_lessthan are deprecated. Use the new lua_compare with appropriate options instead. 
+** Function lua_objlen was renamed lua_rawlen. 
+** Function lua_tonumber is now a macro that expands to lua_tonumberx.
+** Function lua_tointeger is now a macro that expands to lua_tointegerx.
+** Function luaL_register is now a macro that expands to luaL_openlib.
+*/
+
+/*
+** access functions (stack -> C)
+*/
+
+  DXE_EXPORT(lua_type)
+  DXE_EXPORT(lua_typename)
+  DXE_EXPORT(lua_iscfunction)
+  DXE_EXPORT(lua_isnumber)
+  DXE_EXPORT(lua_isstring)
+  DXE_EXPORT(lua_isuserdata)
+  DXE_EXPORT(lua_rawequal)
+  DXE_EXPORT(lua_compare)
+  DXE_EXPORT(lua_tonumberx)
+  DXE_EXPORT(lua_tointegerx)
+  DXE_EXPORT(lua_toboolean)
+  DXE_EXPORT(lua_tolstring)
+  DXE_EXPORT(lua_rawlen)
+  DXE_EXPORT(lua_tocfunction)
+  DXE_EXPORT(lua_touserdata)
+  DXE_EXPORT(lua_tothread)
+  DXE_EXPORT(lua_topointer)
+
+
+/*
+** push functions (C -> stack)
+*/
+
+  DXE_EXPORT(lua_pushnil)
+  DXE_EXPORT(lua_pushnumber)
+  DXE_EXPORT(lua_pushinteger)
+  DXE_EXPORT(lua_pushlstring)
+  DXE_EXPORT(lua_pushstring)
+  DXE_EXPORT(lua_pushvfstring)
+  DXE_EXPORT(lua_pushfstring)
+  DXE_EXPORT(lua_pushcclosure)
+  DXE_EXPORT(lua_pushboolean)
+  DXE_EXPORT(lua_pushlightuserdata)
+  DXE_EXPORT(lua_pushthread)
+
+
+/*
+** some C API functions
+*/
+  DXE_EXPORT(lua_getfield)
+  DXE_EXPORT(lua_newuserdata)
+  DXE_EXPORT(lua_setmetatable)
+
+
+/*
+** functions to register modules
+*/
+  DXE_EXPORT(lua_createtable)  /*  used by luaL_newlib and luaL_newlibtable macros.  */
+  DXE_EXPORT(luaL_setfuncs)    /*  used by luaL_newlib macro.  */
+  DXE_EXPORT(lua_rawgeti)      /*  used by lua_pushglobaltable macro.  */
+  DXE_EXPORT(luaL_newmetatable)
+  DXE_EXPORT(luaL_checkudata)
+  DXE_EXPORT(luaL_error)
+
+
+/* compatibility with old module system */
+#if defined(LUA_COMPAT_MODULE)
+  DXE_EXPORT(luaL_openlib)     /*  used by luaL_register macro.  */
+#endif
+
+
+/*
+** libc functions
+*/
+
+  DXE_EXPORT(printf)
+  DXE_EXPORT(puts)
+DXE_EXPORT_END
+
+
+
+void *dxe_symbol_resolver (const char *symbol_name) {
+  printf ("%s: unresolved symbol in DXE module.\n", symbol_name);
+  return (void *)NULL;
+}
+
+
+static void ll_unloadlib (void *lib) {
+  dlclose(lib);
+}
+
+
+static void *ll_load (lua_State *L, const char *path, int seeglb) {
+  void *lib = NULL;
+#if 1
+  /*  !!!This has been renamed in the cvs repository.!!!  */
+  _dlsymresolver = dxe_symbol_resolver;   /* Set the error callback function. */
+#else
+  dlsymresolver = dxe_symbol_resolver;   /* Set the error callback function. */
+#endif
+  if (dlregsym(exported_symbols) == -1)  /* Register the symbols exported into dynamic modules. */
+    lua_pushliteral(L, "unable to allocate DXE symbol table.");
+  else {
+    lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : 0));
+    if (lib == NULL) lua_pushstring(L, dlerror());
+  }
+  return lib;
+}
+
+
+static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
+  lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
+  if (f == NULL) lua_pushstring(L, dlerror());
+  return f;
+}
+
+/* }====================================================== */
+
+
+
 #else
 /*
 ** {======================================================
 ** Fallback for other systems
 ** =======================================================
diff -aprNU5 lua-5.2.2.orig/src/loslib.c lua-5.2.2/src/loslib.c
--- lua-5.2.2.orig/src/loslib.c	2012-10-19 15:54:02 +0100
+++ lua-5.2.2/src/loslib.c	2013-06-23 14:43:28 +0100
@@ -41,14 +41,18 @@
 /*
 ** By default, Lua uses tmpnam except when POSIX is available, where it
 ** uses mkstemp.
 */
 #if defined(LUA_USE_MKSTEMP)
+# if !defined(LUA_TMPNAME_TEMPLATE)
+#  define LUA_TMPNAME_TEMPLATE	"/tmp/lua_XXXXXX"
+#  define LUA_TMPNAMBUFSIZE	32
+# endif
+
 #include <unistd.h>
-#define LUA_TMPNAMBUFSIZE	32
 #define lua_tmpnam(b,e) { \
-        strcpy(b, "/tmp/lua_XXXXXX"); \
+        strcpy(b, LUA_TMPNAME_TEMPLATE); \
         e = mkstemp(b); \
         if (e != -1) close(e); \
         e = (e == -1); }
 
 #elif !defined(lua_tmpnam)
diff -aprNU5 lua-5.2.2.orig/src/luaconf.h lua-5.2.2/src/luaconf.h
--- lua-5.2.2.orig/src/luaconf.h	2013-03-16 21:10:18 +0100
+++ lua-5.2.2/src/luaconf.h	2013-06-23 14:43:28 +0100
@@ -38,10 +38,53 @@
 #define LUA_USE_AFORMAT		/* assume 'printf' handles 'aA' specifiers */
 #endif
 
 
 
+#if defined(LUA_USE_DJGPP)
+#define LUA_USE_MKSTEMP
+#define LUA_USE_ISATTY
+#define LUA_USE_POPEN
+#define LUA_USE_DXE3		/* use DJGPP's dxe3 functionality: dlopen, dlcose, dlsym,... */
+#define LUA_USE_READLINE	/* needs some extra libraries */
+#define LUA_USE_STRTODHEX	/* assume 'strtod' handles hexa formats */
+#define LUA_USE_AFORMAT		/* assume 'printf' handles 'aA' specifiers */
+#define LUA_USE_LONGLONG	/* assume support for long long */
+#define DIR_EXISTS(path)	(access((path), D_OK) == 0)
+#if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+# define __gnuc_extension__  __extension__
+#else
+# define __gnuc_extension__
+#endif
+#define LUA_TMPNAMBUFSIZE       FILENAME_MAX
+#define LUA_TMPNAME_TEMPLATE                                                     \
+(__gnuc_extension__                                                              \
+  ({                                                                             \
+      unsigned int last;                                                         \
+      char name_template[LUA_TMPNAMBUFSIZE], *tmp_dir;                           \
+      if (!((tmp_dir = getenv("TMPDIR")) && DIR_EXISTS(tmp_dir)))                \
+        if (!((tmp_dir = getenv("TEMP")) && DIR_EXISTS(tmp_dir)))                \
+          if (!((tmp_dir = getenv("TMP")) && DIR_EXISTS(tmp_dir)))               \
+          {                                                                      \
+            strcpy(name_template, P_tmpdir);                                     \
+            tmp_dir = name_template;                                             \
+          }                                                                      \
+      last = strlen(tmp_dir) - 1;                                                \
+      if (tmp_dir[last] == '/' || tmp_dir[last] == '\\')                         \
+        tmp_dir[last] = '\0';                                                    \
+      snprintf(name_template, LUA_TMPNAMBUFSIZE, "%s%s", tmp_dir, "/luXXXXXX");  \
+      name_template;                                                             \
+  })                                                                             \
+)
+#endif
+
+#if defined(LUA_USE_DXE3)
+#define LUA_DL_DXE3
+#endif
+
+
+
 #if defined(LUA_USE_LINUX)
 #define LUA_USE_POSIX
 #define LUA_USE_DLOPEN		/* needs an extra library: -ldl */
 #define LUA_USE_READLINE	/* needs some extra libraries */
 #define LUA_USE_STRTODHEX	/* assume 'strtod' handles hex formats */
@@ -95,10 +138,22 @@
 		LUA_LDIR"?.lua;"  LUA_LDIR"?\\init.lua;" \
 		LUA_CDIR"?.lua;"  LUA_CDIR"?\\init.lua;" ".\\?.lua"
 #define LUA_CPATH_DEFAULT \
 		LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll;" ".\\?.dll"
 
+#elif defined(__DJGPP__)	/* }{ */
+
+#define LUA_VDIR	LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "/"
+#define LUA_ROOT	"/dir/env/DJDIR/"
+#define LUA_LDIR	LUA_ROOT "share/lua/" LUA_VDIR
+#define LUA_CDIR	LUA_ROOT "lib/lua/" LUA_VDIR
+#define LUA_PATH_DEFAULT  \
+		LUA_LDIR"?.lua;"  LUA_LDIR"?/init.lua;" \
+		LUA_CDIR"?.lua;"  LUA_CDIR"?/init.lua;" "./?.lua"
+#define LUA_CPATH_DEFAULT \
+		LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so"
+
 #else			/* }{ */
 
 #define LUA_VDIR	LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "/"
 #define LUA_ROOT	"/usr/local/"
 #define LUA_LDIR	LUA_ROOT "share/lua/" LUA_VDIR
@@ -106,10 +161,11 @@
 #define LUA_PATH_DEFAULT  \
 		LUA_LDIR"?.lua;"  LUA_LDIR"?/init.lua;" \
 		LUA_CDIR"?.lua;"  LUA_CDIR"?/init.lua;" "./?.lua"
 #define LUA_CPATH_DEFAULT \
 		LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so"
+
 #endif			/* } */
 
 
 /*
 @@ LUA_DIRSEP is the directory separator (for submodules).
