Port lua to work on Fuchsia

Ported the lua interpreter and compiler work on Fuchsia:

  * Added a new LICENSE file and a new README.md that documents support
    status for builtin lua modules.

  * Add a BUILD.gn file.

  * Added a compile-time flag to use linenoise for the REPL.
    Conditionally compiled out handling for SIGINT in the REPL.

Change-Id: Ic6ca098bf9811b42c771fa868d0bd8a082952350
diff --git a/BUILD.gn b/BUILD.gn
new file mode 100644
index 0000000..c600c92
--- /dev/null
+++ b/BUILD.gn
@@ -0,0 +1,79 @@
+# Copyright 2016 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("lua_core") {
+  sources = [
+    "src/lapi.c",
+    "src/lcode.c",
+    "src/lctype.c",
+    "src/ldebug.c",
+    "src/ldo.c",
+    "src/ldump.c",
+    "src/lfunc.c",
+    "src/lgc.c",
+    "src/llex.c",
+    "src/lmem.c",
+    "src/lobject.c",
+    "src/lopcodes.c",
+    "src/lparser.c",
+    "src/lstate.c",
+    "src/lstring.c",
+    "src/ltable.c",
+    "src/ltm.c",
+    "src/lundump.c",
+    "src/lvm.c",
+    "src/lzio.c",
+  ]
+}
+
+source_set("lua_lib") {
+  sources = [
+    "src/lauxlib.c",
+    "src/lbaselib.c",
+    "src/lbitlib.c",
+    "src/lcorolib.c",
+    "src/ldblib.c",
+    "src/liolib.c",
+    "src/lmathlib.c",
+    "src/loslib.c",
+    "src/lstrlib.c",
+    "src/ltablib.c",
+    "src/lutf8lib.c",
+    "src/loadlib.c",
+    "src/linit.c",
+  ]
+}
+
+static_library("liblua") {
+  deps = [
+    ":lua_core",
+    ":lua_lib",
+  ]
+}
+
+executable("lua") {
+  sources = [
+    "src/lua.c",
+  ]
+
+  deps = [
+    ":liblua",
+    "//third_party/linenoise",
+  ]
+
+  defines = [
+    "LUA_USE_POSIX",
+    "LUA_USE_LINENOISE",
+  ]
+}
+
+executable("luac") {
+  sources = [
+    "src/luac.c",
+  ]
+
+  deps = [ ":liblua" ]
+
+  defines = [ "LUA_USE_POSIX" ]
+}
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..9b4e37c
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,18 @@
+Copyright © 1994–2016 Lua.org, PUC-Rio.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README b/OLD_README
similarity index 100%
rename from README
rename to OLD_README
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..937926f
--- /dev/null
+++ b/README.md
@@ -0,0 +1,39 @@
+# Lua 5.3.3
+
+This is a fork of Lua 5.3.3, released on 30 May 2016. This project makes the
+smallest number of modifications to support running Lua as part of the
+[Fuchsia](https://fuchsia.googlesource.com/) repository.
+
+For installation instructions, license details, and
+further information about Lua, see doc/readme.html.
+
+Please refer to the [Lua website](http://www.lua.org/about.html) for information
+about the Lua project.
+
+# Support status
+
+The following is a list of builtin library functions that don't work due to
+missing features in Fuchsia (e.g. unimplemented libc functions):
+
+## io
+
+  * **io.popen**: Returns not supported
+  * **io.tmpfile**: Crashes with due to unsupported syscall (tmpfile)
+  * **io.read**: Reading from stdin doesn't block. Reading from regular files is OK.
+
+## os
+
+  * **os.time**: Returns the same thing as os.clock, which is incorrect
+  * **os.date**: Unsupported syscall: localtime, gmtime, localtime_r, gmtime_r (#257)
+  * **os.execute**: Unsupported syscall: sigaction (#13)
+  * **os.getenv**: Unsupported syscall: sigaction (#13)
+  * **os.rename**: Returns "nil     Function not Implemented38"
+  * **os.tmpname**: Unsupported syscall: tmpnam (#262)
+
+## package
+
+  * **package.cpath**: Need to configure this away from the default cpath on
+    Fuchsia?
+  * **package.loadlib**: Returns *"Dynamic libraries not enabled"*, which is
+    probably what we want right now.
+  * **package.path**: Need to map this to something meaningful.
diff --git a/src/lua.c b/src/lua.c
index 545d23d..9342081 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -82,6 +82,21 @@
 #define lua_readline(L,b,p)	((void)L, ((b)=readline(p)) != NULL)
 #define lua_saveline(L,line)	((void)L, add_history(line))
 #define lua_freeline(L,b)	((void)L, free(b))
+#define lua_loadhistory()	/* no-op */
+
+#elif defined(LUA_USE_LINENOISE)/* }{ */
+
+#include <linenoise.h>
+#define HISTORY_FILE		"/tmp/lua-history.txt"
+#define lua_readline(L,b,p)	((void)L, ((b)=linenoise(p)) != NULL)
+#define lua_saveline(L,line)	\
+do { \
+	(void)L; \
+	linenoiseHistoryAdd(line); \
+	linenoiseHistorySave(HISTORY_FILE); \
+} while(0);
+#define lua_freeline(L,b)	((void)L, linenoiseFree(b))
+#define lua_loadhistory()	linenoiseHistoryLoad(HISTORY_FILE)
 
 #else				/* }{ */
 
@@ -90,6 +105,7 @@
         fgets(b, LUA_MAXINPUT, stdin) != NULL)  /* get line */
 #define lua_saveline(L,line)	{ (void)L; (void)line; }
 #define lua_freeline(L,b)	{ (void)L; (void)b; }
+#define lua_loadhistory()	/* no-op */
 
 #endif				/* } */
 
@@ -102,6 +118,7 @@
 
 static const char *progname = LUA_PROGNAME;
 
+#ifndef __Fuchsia__
 
 /*
 ** Hook set by signal function to stop the interpreter.
@@ -124,6 +141,8 @@
   lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
 }
 
+#endif
+
 
 static void print_usage (const char *badoption) {
   lua_writestringerror("%s: ", progname);
@@ -199,9 +218,13 @@
   lua_pushcfunction(L, msghandler);  /* push message handler */
   lua_insert(L, base);  /* put it under function and args */
   globalL = L;  /* to be available to 'laction' */
+#ifndef __Fuchsia__
   signal(SIGINT, laction);  /* set C-signal handler */
+#endif
   status = lua_pcall(L, narg, nres, base);
+#ifndef __Fuchsia__
   signal(SIGINT, SIG_DFL); /* reset C-signal handler */
+#endif
   lua_remove(L, base);  /* remove message handler from the stack */
   return status;
 }
@@ -559,6 +582,7 @@
     print_usage(argv[script]);  /* 'script' has index of bad arg. */
     return 0;
   }
+  lua_loadhistory();
   if (args & has_v)  /* option '-v'? */
     print_version();
   if (args & has_E) {  /* option '-E'? */