lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Attached is a simple patch to lua.c that adds a switch -t (think Perl) to ignore LUA_INIT and set the _PATH variables to their built-in defaults. Unlike Perl's -t, there's no general taint support, but this patch has much the same aim in mind, to allow for safer, more controlled use of standalone Lua. It is handy in any application where the user's potentially non-standard settings could interfere with a script's operation.

--
http://rrt.sc3d.org/ | computation, n.  automated pedantry
diff -Nur lua-5.1.2/src/lua.c lua-5.1.2-no-init-or-paths/src/lua.c
--- lua-5.1.2/src/lua.c	2006-06-02 16:34:00.000000000 +0100
+++ lua-5.1.2-no-init-or-paths/src/lua.c	2007-11-15 20:10:10.000000000 +0000
@@ -46,6 +46,7 @@
   "  -e stat  execute string " LUA_QL("stat") "\n"
   "  -l name  require library " LUA_QL("name") "\n"
   "  -i       enter interactive mode after executing " LUA_QL("script") "\n"
+  "  -t       use built-in defaults for LUA_INIT, LUA_PATH and LUA_CPATH\n"
   "  -v       show version information\n"
   "  --       stop handling options\n"
   "  -        execute stdin and stop handling options\n"
@@ -256,7 +257,7 @@
 #define notail(x)	{if ((x)[2] != '\0') return -1;}
 
 
-static int collectargs (char **argv, int *pi, int *pv, int *pe) {
+static int collectargs (char **argv, int *pi, int *pv, int *pe, int *pq) {
   int i;
   for (i = 1; argv[i] != NULL; i++) {
     if (argv[i][0] != '-')  /* not an option? */
@@ -282,6 +283,9 @@
           if (argv[i] == NULL) return -1;
         }
         break;
+      case 't':
+        *pq = 1;
+        break;
       default: return -1;  /* invalid option */
     }
   }
@@ -339,20 +343,31 @@
   struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
   char **argv = s->argv;
   int script;
-  int has_i = 0, has_v = 0, has_e = 0;
+  int has_i = 0, has_v = 0, has_e = 0, has_t = 0;
   globalL = L;
   if (argv[0] && argv[0][0]) progname = argv[0];
   lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */
   luaL_openlibs(L);  /* open libraries */
   lua_gc(L, LUA_GCRESTART, 0);
-  s->status = handle_luainit(L);
-  if (s->status != 0) return 0;
-  script = collectargs(argv, &has_i, &has_v, &has_e);
+  script = collectargs(argv, &has_i, &has_v, &has_e, &has_t);
   if (script < 0) {  /* invalid args? */
     print_usage();
     s->status = 1;
     return 0;
   }
+  if (has_t) {
+    lua_getglobal(L, LUA_LOADLIBNAME);
+    if (lua_istable(L, -1)) {
+      lua_pushstring(L, LUA_PATH_DEFAULT);
+      lua_setfield(L, -2, "path");
+      lua_pushstring(L, LUA_CPATH_DEFAULT);
+      lua_setfield(L, -2, "cpath");
+    }
+    lua_pop(L, 1);
+  } else {
+    s->status = handle_luainit(L);
+    if (s->status != 0) return 0;
+  }
   if (has_v) print_version();
   s->status = runargs(L, argv, (script > 0) ? script : s->argc);
   if (s->status != 0) return 0;