2012-07-28  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.

	* 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/liolib.c [LUA_USE_POPEN]: For DJGPP include libsupp.h for popen
	prototype.

	* 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.

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

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





diff -aprNU5 lua-5.2.1.orig/Makefile lua-5.2.1/Makefile
--- lua-5.2.1.orig/Makefile	2012-05-17 14:05:54 +0000
+++ lua-5.2.1/Makefile	2012-07-28 16:20:20 +0000
@@ -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,26 @@ 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
+	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
+	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
 
 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.1.orig/djgpp/libtest1.c lua-5.2.1/djgpp/libtest1.c
--- lua-5.2.1.orig/djgpp/libtest1.c	1970-01-01 00:00:00 +0000
+++ lua-5.2.1/djgpp/libtest1.c	2012-07-28 16:20:20 +0000
@@ -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.1.orig/djgpp/libtest1.lua lua-5.2.1/djgpp/libtest1.lua
--- lua-5.2.1.orig/djgpp/libtest1.lua	1970-01-01 00:00:00 +0000
+++ lua-5.2.1/djgpp/libtest1.lua	2012-07-28 16:20:20 +0000
@@ -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.1.orig/djgpp/libtest2.c lua-5.2.1/djgpp/libtest2.c
--- lua-5.2.1.orig/djgpp/libtest2.c	1970-01-01 00:00:00 +0000
+++ lua-5.2.1/djgpp/libtest2.c	2012-07-28 18:35:54 +0000
@@ -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.1.orig/djgpp/libtest2.lua lua-5.2.1/djgpp/libtest2.lua
--- lua-5.2.1.orig/djgpp/libtest2.lua	1970-01-01 00:00:00 +0000
+++ lua-5.2.1/djgpp/libtest2.lua	2012-07-28 16:20:20 +0000
@@ -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.1.orig/src/Makefile lua-5.2.1/src/Makefile
--- lua-5.2.1.orig/src/Makefile	2012-03-09 16:32:16 +0000
+++ lua-5.2.1/src/Makefile	2012-07-28 16:20:20 +0000
@@ -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,14 @@ 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"
+#	$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_DJGPP" SYSLIBS="-lreadline -lhistory -lsupp"
+
 freebsd:
 	$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -lreadline"
 
 generic: $(ALL)
 
diff -aprNU5 lua-5.2.1.orig/src/liolib.c lua-5.2.1/src/liolib.c
--- lua-5.2.1.orig/src/liolib.c	2011-11-25 12:50:02 +0000
+++ lua-5.2.1/src/liolib.c	2012-07-28 16:20:20 +0000
@@ -39,10 +39,15 @@
 
 #if !defined(lua_popen)	/* { */
 
 #if defined(LUA_USE_POPEN)	/* { */
 
+#if 0
+#if defined(__DJGPP__)
+#include <libsupp.h>
+#endif
+#endif
 #define lua_popen(L,c,m)	((void)L, fflush(NULL), popen(c,m))
 #define lua_pclose(L,file)	((void)L, pclose(file))
 
 #elif defined(LUA_WIN)		/* }{ */
 
diff -aprNU5 lua-5.2.1.orig/src/loadlib.c lua-5.2.1/src/loadlib.c
--- lua-5.2.1.orig/src/loadlib.c	2012-05-30 12:33:44 +0000
+++ lua-5.2.1/src/loadlib.c	2012-07-28 16:44:48 +0000
@@ -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	"_"
 
 
@@ -209,10 +214,142 @@ static lua_CFunction ll_sym (lua_State *
   return f;
 }
 
 /* }====================================================== */
 
+
+#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)
+
+
+/*
+** 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.  */
+
+/* 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.1.orig/src/loslib.c lua-5.2.1/src/loslib.c
--- lua-5.2.1.orig/src/loslib.c	2012-05-23 15:37:08 +0000
+++ lua-5.2.1/src/loslib.c	2012-07-28 16:20:20 +0000
@@ -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"
+# 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.1.orig/src/luaconf.h lua-5.2.1/src/luaconf.h
--- lua-5.2.1.orig/src/luaconf.h	2012-05-11 14:14:42 +0000
+++ lua-5.2.1/src/luaconf.h	2012-07-28 16:20:20 +0000
@@ -38,10 +38,45 @@
 #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)
+#define LUA_TMPNAME_TEMPLATE                                           \
+  ({                                                                   \
+      unsigned int last;                                               \
+      char template[FILENAME_MAX], *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(template, P_tmpdir);                                \
+            tmp_dir = template;                                        \
+          }                                                            \
+      last = strlen(tmp_dir) - 1;                                      \
+      if (tmp_dir[last] == '/' || tmp_dir[last] == '\\')               \
+        tmp_dir[last] = '\0';                                          \
+      snprintf(template, FILENAME_MAX, "%s%s", tmp_dir, "/luXXXXXX");  \
+      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 hexa formats */
@@ -95,10 +130,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 +153,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).
