--- make-3.81-orig/function.c	2006-03-31 22:36:40.000000000 -0800
+++ make-3.81/function.c	2006-10-15 11:42:36.000000000 -0700
@@ -23,6 +23,13 @@
 #include "job.h"
 #include "commands.h"
 #include "debug.h"
+#define LUA_SUPPORT
+#ifdef LUA_SUPPORT
+#  include <lua.h>
+#  include <luaconf.h>
+#  include <lualib.h>
+#  include <lauxlib.h>
+#endif /* LUA_SUPPORT */
 
 #ifdef _AMIGA
 #include "amiga.h"
@@ -2028,6 +2035,119 @@
  return o;
 }
 
+#ifdef LUA_SUPPORT
+int printmake_pipe[2] = {-1, -1};
+
+static int lua_printmake(lua_State* L)
+{
+  int i;
+  int top;
+  const char* tostring_res;
+  const char* str_to_print;
+
+  if (printmake_pipe[1] == -1)
+    {
+      return 0;
+    }
+
+  for (i=1; i<=lua_gettop(L); i++)
+    {
+      tostring_res = lua_tostring(L, i);
+      if (NULL == tostring_res)
+        {
+          lua_pushstring(L, "");
+          lua_replace(L, i);
+        }
+    }
+
+  lua_concat(L, lua_gettop(L));
+
+  str_to_print = lua_tostring(L,lua_gettop(L));
+  write(printmake_pipe[1], str_to_print, strlen(str_to_print));
+
+  return 0;
+}
+
+static int lua_printmake_unavailable(lua_State* L)
+{
+  fprintf(stderr,
+          "Lua Error: printmake function unavailable.  "
+          "Couldn't properly initialize pipe.\n");
+}
+
+static char *
+func_lua (char *o, char **argv, const char *funcname UNUSED)
+{
+  static lua_State* L = NULL;
+  char* script;
+  int readlen;
+  char readbuf[PIPE_BUF];
+  int load_result;
+
+  if (!L)
+    {
+      /* Start Lua */
+      L = lua_open();
+
+      /* Make all standard libraries available. */
+      luaL_openlibs(L);
+
+      /* Open a pipe for communication with Lua "printmake" function. */
+      if (pipe(printmake_pipe) != -1)
+        {
+          int write_side_flags;
+          int read_side_flags;
+
+          write_side_flags = fcntl(printmake_pipe[1], F_GETFL, 0);
+          write_side_flags |= O_NONBLOCK;
+          if (write_side_flags > 0)
+            fcntl(printmake_pipe[1], F_SETFL, write_side_flags);
+
+          read_side_flags = fcntl(printmake_pipe[0], F_GETFL, 0);
+          read_side_flags |= O_NONBLOCK;
+          if (read_side_flags > 0)
+            fcntl(printmake_pipe[0], F_SETFL, read_side_flags);
+
+          if (write_side_flags > 0 && read_side_flags > 0)
+            lua_register(L, "printmake", lua_printmake);
+          else
+            lua_register(L, "printmake", lua_printmake_unavailable);
+        }
+      else
+        {
+          lua_register(L, "printmake", lua_printmake_unavailable);
+        }
+    }
+
+  script = argv[0];
+  if (!script || strlen(script) == 0)
+    {
+      return o;
+    }
+
+  /* Run the Lua script. */
+  load_result = luaL_dostring(L, script);
+
+  /* Read the output from printmake if any. */
+  if (load_result == 0)
+    {
+      while ((readlen =
+              read(printmake_pipe[0], (void*) readbuf, sizeof(readbuf))) > 0)
+        {
+          o = variable_buffer_output(o, readbuf, readlen);
+        }
+    }
+  else
+    {
+      const char* lua_err = lua_tostring(L, lua_gettop(L));
+      fprintf(stderr, "Lua Error: could not run script: %s\n", script);
+      fprintf(stderr, "Lua Error: %s\n", lua_err);
+    }
+
+  return o;
+}
+#endif /* LUA_SUPPORT */
+
 /* Lookup table for builtin functions.
 
    This doesn't have to be sorted; we use a straight lookup.  We might gain
@@ -2081,6 +2201,9 @@
   { STRING_SIZE_TUPLE("and"),           1,  0,  0,  func_and},
   { STRING_SIZE_TUPLE("value"),         0,  1,  1,  func_value},
   { STRING_SIZE_TUPLE("eval"),          0,  1,  1,  func_eval},
+#ifdef LUA_SUPPORT
+  { STRING_SIZE_TUPLE("lua"),           0,  1,  1,  func_lua},
+#endif /* LUA_SUPPORT */
 #ifdef EXPERIMENTAL
   { STRING_SIZE_TUPLE("eq"),            2,  2,  1,  func_eq},
   { STRING_SIZE_TUPLE("not"),           0,  1,  1,  func_not},
