2010-12-27  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* 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_POPEN]:  For DJGPP include libsupp.h for popen prototype.

	* src/Makefile: Link with libsupp.a


2010-12-26  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

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

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


2010-12-24  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* src/luaconf.h [LUA_USE_DJGPP]: Define LUA_USE_DXE3.
	[LUA_USE_DXE3]:  Define LUA_DL_DXE3.

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


2010-12-23  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* Makefile: Added test_djgpp target.

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

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


2010-12-02  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* src/Makefile: Add link option for readline.


2010-12-01  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* src/luaconf.h [LUA_USE_DJGPP]:  Define new macro LUA_TMPNAME_TEMPLATE
	to "/dev/env/TMPDIR/luXXXXXX".
	[LUA_USE_MKSTEMP]:  If LUA_TMPNAME_TEMPLATE not defined, define it to
	the standard string "/tmp/lua_XXXXXX".


2010-11-27  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* Makefile: Add SHELL variable.  Add djgpp to the known platforms.

	* src/Makefile: Add djgpp target.

	* src/luaconf.h [LUA_USE_DJGPP]: Define LUA_USE_MKSTEMP, LUA_USE_ISATTY
	and LUA_USE_POPEN.
	[__DJGPP__]: Define LUA_ROOT, LUA_LDIR, LUA_CDIR, LUA_PATH_DEFAULT and
	LUA_CPATH_DEFAULT.






diff -aprNU5 lua-5.1.4.orig/Makefile lua-5.1.4/Makefile
--- lua-5.1.4.orig/Makefile	2008-08-12 00:40:48 +0000
+++ lua-5.1.4/Makefile	2010-12-27 18:40:32 +0000
@@ -1,10 +1,11 @@
 # makefile for installing Lua
 # see INSTALL for installation instructions
 # see src/Makefile and src/luaconf.h for further customization
 
 # == 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,
@@ -36,11 +37,11 @@ MKDIR= mkdir -p
 RANLIB= ranlib
 
 # == 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_INC= lua.h luaconf.h lualib.h lauxlib.h ../etc/lua.hpp
 TO_LIB= liblua.a
@@ -56,10 +57,22 @@ $(PLATS) clean:
 	cd src && $(MAKE) $@
 
 test:	dummy
 	src/lua test/hello.lua
 
+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 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 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)
 	cd src && $(INSTALL_DATA) $(TO_LIB) $(INSTALL_LIB)
diff -aprNU5 lua-5.1.4.orig/djgpp/libtest1.c lua-5.1.4/djgpp/libtest1.c
--- lua-5.1.4.orig/djgpp/libtest1.c	1970-01-01 00:00:00 +0000
+++ lua-5.1.4/djgpp/libtest1.c	2010-12-27 18:40:32 +0000
@@ -0,0 +1,19 @@
+#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.1.4.orig/djgpp/libtest1.lua lua-5.1.4/djgpp/libtest1.lua
--- lua-5.1.4.orig/djgpp/libtest1.lua	1970-01-01 00:00:00 +0000
+++ lua-5.1.4/djgpp/libtest1.lua	2010-12-27 18:40:32 +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 \"%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.1.4.orig/djgpp/libtest2.c lua-5.1.4/djgpp/libtest2.c
--- lua-5.1.4.orig/djgpp/libtest2.c	1970-01-01 00:00:00 +0000
+++ lua-5.1.4/djgpp/libtest2.c	2010-12-27 18:40:32 +0000
@@ -0,0 +1,42 @@
+#include <math.h>
+#include <string.h>
+#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 my_lib[] = {
+  {"compute_trig_function", compute_trig_function},
+  {NULL, NULL}
+};
+
+int luaopen_libtest2(lua_State *L)
+{
+  luaL_register(L, "my_lib", my_lib);  /* Register all functions belonging to my_lib module. */
+  return 1;
+}
diff -aprNU5 lua-5.1.4.orig/djgpp/libtest2.lua lua-5.1.4/djgpp/libtest2.lua
--- lua-5.1.4.orig/djgpp/libtest2.lua	1970-01-01 00:00:00 +0000
+++ lua-5.1.4/djgpp/libtest2.lua	2010-12-27 18:40:32 +0000
@@ -0,0 +1,14 @@
+-- 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 containt in the \"my_lib\" table\nallowing to use \"compute_trig_function\" to 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 = my_lib.compute_trig_function(x, "sin")
+  local cos_value = my_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.1.4.orig/src/Makefile lua-5.1.4/src/Makefile
--- lua-5.1.4.orig/src/Makefile	2008-01-19 19:37:58 +0000
+++ lua-5.1.4/src/Makefile	2010-12-27 18:42:28 +0000
@@ -87,10 +87,13 @@ ansi:
 	$(MAKE) all MYCFLAGS=-DLUA_ANSI
 
 bsd:
 	$(MAKE) all MYCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-Wl,-E"
 
+djgpp:
+	$(MAKE) all MYCFLAGS=-DLUA_USE_DJGPP MYLIBS="-lreadline -lhistory -lsupp"
+
 freebsd:
 	$(MAKE) all MYCFLAGS="-DLUA_USE_LINUX" MYLIBS="-Wl,-E -lreadline"
 
 generic:
 	$(MAKE) all MYCFLAGS=
diff -aprNU5 lua-5.1.4.orig/src/loadlib.c lua-5.1.4/src/loadlib.c
--- lua-5.1.4.orig/src/loadlib.c	2008-08-06 13:29:28 +0000
+++ lua-5.1.4/src/loadlib.c	2010-12-27 18:40:32 +0000
@@ -220,10 +220,135 @@ 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 has 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>
+
+
+/* DJGPP appends a `_' before C function names */
+#undef POF
+#define POF	"_" LUA_POF
+
+
+DXE_EXPORT_TABLE(exported_symbols)
+/*
+** 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_equal)
+  DXE_EXPORT(lua_lessthan)
+  DXE_EXPORT(lua_tonumber)
+  DXE_EXPORT(lua_tointeger)
+  DXE_EXPORT(lua_toboolean)
+  DXE_EXPORT(lua_tolstring)
+  DXE_EXPORT(lua_objlen)
+  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)
+
+
+/*
+** function to register modules
+*/
+
+  DXE_EXPORT(luaL_register)
+
+
+/*
+** 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) {
+  void *lib = NULL;
+#if 0
+  /*  !!!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_GLOBAL);
+    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.1.4.orig/src/luaconf.h lua-5.1.4/src/luaconf.h
--- lua-5.1.4.orig/src/luaconf.h	2008-02-11 16:25:08 +0000
+++ lua-5.1.4/src/luaconf.h	2010-12-27 18:42:28 +0000
@@ -31,10 +31,36 @@
 
 #if !defined(LUA_ANSI) && defined(_WIN32)
 #define LUA_WIN
 #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 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_LINUX)
 #define LUA_USE_POSIX
 #define LUA_USE_DLOPEN		/* needs an extra library: -ldl */
 #define LUA_USE_READLINE	/* needs some extra libraries */
 #endif
@@ -91,10 +117,19 @@
 		".\\?.lua;"  LUA_LDIR"?.lua;"  LUA_LDIR"?\\init.lua;" \
 		             LUA_CDIR"?.lua;"  LUA_CDIR"?\\init.lua"
 #define LUA_CPATH_DEFAULT \
 	".\\?.dll;"  LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
 
+#elif defined(__DJGPP__)
+#define LUA_ROOT	"/dir/env/DJDIR/"
+#define LUA_LDIR	LUA_ROOT "share/lua/5.1/"
+#define LUA_CDIR	LUA_ROOT "lib/lua/5.1/"
+#define LUA_PATH_DEFAULT  \
+		"./?.lua;"  LUA_LDIR"?.lua;"  LUA_LDIR"?/init.lua;" \
+		            LUA_CDIR"?.lua;"  LUA_CDIR"?/init.lua"
+#define LUA_CPATH_DEFAULT \
+	"./?.so;"  LUA_CDIR"?.so;" LUA_CDIR"loadall.so"
 #else
 #define LUA_ROOT	"/usr/local/"
 #define LUA_LDIR	LUA_ROOT "share/lua/5.1/"
 #define LUA_CDIR	LUA_ROOT "lib/lua/5.1/"
 #define LUA_PATH_DEFAULT  \
@@ -642,14 +677,18 @@ union luai_Cast { double l_d; long l_l;
 ** uses tmpnam except when POSIX is available, where it uses mkstemp.
 */
 #if defined(loslib_c) || defined(luaall_c)
 
 #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); }
 
 #else
@@ -665,10 +704,14 @@ union luai_Cast { double l_d; long l_l;
 @* the file streams.
 ** CHANGE it if you have a way to implement it in your system.
 */
 #if defined(LUA_USE_POPEN)
 
+#if defined(__DJGPP__)
+#include <libsupp.h>
+#endif
+
 #define lua_popen(L,c,m)	((void)L, fflush(NULL), popen(c,m))
 #define lua_pclose(L,file)	((void)L, (pclose(file) != -1))
 
 #elif defined(LUA_WIN)
 
@@ -693,20 +736,24 @@ union luai_Cast { double l_d; long l_l;
 ** adding -ldl to the linker options), so Lua does not select it
 ** automatically.  (When you change the makefile to add -ldl, you must
 ** also add -DLUA_USE_DLOPEN.)
 ** If you do not want any kind of dynamic library, undefine all these
 ** options.
-** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD.
+** By default, _WIN32 gets LUA_DL_DLL, MAC OS X gets LUA_DL_DYLD and
+** DJGPP gets LUA_DL_DXE3.
 */
 #if defined(LUA_USE_DLOPEN)
 #define LUA_DL_DLOPEN
 #endif
 
 #if defined(LUA_WIN)
 #define LUA_DL_DLL
 #endif
 
+#if defined(LUA_USE_DXE3)
+#define LUA_DL_DXE3
+#endif
 
 /*
 @@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State
 @* (the data goes just *before* the lua_State pointer).
 ** CHANGE (define) this if you really need that. This value must be
