lua-users home
lua-l archive

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


A 'return 1453' in a Lua main chunk doesn't set the error code that's
handed back to the OS. It would be nice if utility scripts could set
this value.

(Sure, os.exit(1453) does that... but it also skips normal program
termination. That means, among other things, that no full garbage
collection is performed at program exit. Not always desirable,
especially if one has OS resources in userdata.)

The following patch lets the value of a 'return <number>' in a lua main
chunk bubble up to the OS. It is pretty small and seems to have no dire
side effects (tested on a Windows box, should run everywhere though).

diff -ruN lua-5.1.2\src\lua.c lua-5.1.2-errcode-patch\src\lua.c
--- lua-5.1.2\src\lua.c	Sun Jan 01 00:00:00 2006
+++ lua-5.1.2-errcode-patch\src\lua.c	Wed Jun 13 22:01:06 2007
@@ -332,6 +332,7 @@
   int argc;
   char **argv;
   int status;
+  int errcode;
 };
 
 
@@ -368,6 +369,10 @@
     }
     else dofile(L, NULL);  /* executes stdin as a file */
   }
+  if (lua_gettop(L) == 2 && lua_type(L,-1) == LUA_TNUMBER) {
+    s->errcode = lua_tointeger(L, -1);
+    lua_pop(L, 1);
+  }
   return 0;
 }
 
@@ -382,9 +387,10 @@
   }
   s.argc = argc;
   s.argv = argv;
+  s.errcode = 0;
   status = lua_cpcall(L, &pmain, &s);
   report(L, status);
   lua_close(L);
-  return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
+  return s.errcode;
 }
 
-- 
cheers  thomasl

web : http://thomaslauer.com/start