Commit 6b58ddf8 authored by Vicent Marti's avatar Vicent Marti

lua: Proper style for main.c

parent b3dd6731
...@@ -29,46 +29,43 @@ ...@@ -29,46 +29,43 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lua.h"
#include "lualib.h" #include "lualib.h"
static lua_State *globalL = NULL; static lua_State *globalL = NULL;
static const char *progname = NULL; static const char *progname = NULL;
static void lstop(lua_State *L, lua_Debug *ar) static void lstop(lua_State *L, lua_Debug *ar) {
{
(void)ar; /* unused arg. */ (void)ar; /* unused arg. */
lua_sethook(L, NULL, 0, 0); lua_sethook(L, NULL, 0, 0);
luaL_error(L, "interrupted!"); luaL_error(L, "interrupted!");
} }
static void laction(int i) static void laction(int i) {
{
signal(i, SIG_DFL); signal(i, SIG_DFL);
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
} }
static void l_message(const char *pname, const char *msg) static void l_message(const char *pname, const char *msg) {
{ if (pname)
if (pname) fprintf(stderr, "%s: ", pname); fprintf(stderr, "%s: ", pname);
fprintf(stderr, "%s\n", msg); fprintf(stderr, "%s\n", msg);
fflush(stderr); fflush(stderr);
} }
static int report(lua_State *L, int status) static int report(lua_State *L, int status) {
{
if (status && !lua_isnil(L, -1)) { if (status && !lua_isnil(L, -1)) {
const char *msg = lua_tostring(L, -1); const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "(error object is not a string)"; if (msg == NULL)
msg = "(error object is not a string)";
l_message(progname, msg); l_message(progname, msg);
lua_pop(L, 1); lua_pop(L, 1);
} }
return status; return status;
} }
static int traceback(lua_State *L) static int traceback(lua_State *L) {
{
if (!lua_isstring(L, 1)) /* 'message' not a string? */ if (!lua_isstring(L, 1)) /* 'message' not a string? */
return 1; /* keep it intact */ return 1; /* keep it intact */
lua_getfield(L, LUA_GLOBALSINDEX, "debug"); lua_getfield(L, LUA_GLOBALSINDEX, "debug");
...@@ -87,8 +84,7 @@ static int traceback(lua_State *L) ...@@ -87,8 +84,7 @@ static int traceback(lua_State *L)
return 1; return 1;
} }
static int docall(lua_State *L, int narg, int clear) static int docall(lua_State *L, int narg, int clear) {
{
int status; int status;
int base = lua_gettop(L) - narg; /* function index */ int base = lua_gettop(L) - narg; /* function index */
lua_pushcfunction(L, traceback); /* push traceback function */ lua_pushcfunction(L, traceback); /* push traceback function */
...@@ -98,12 +94,12 @@ static int docall(lua_State *L, int narg, int clear) ...@@ -98,12 +94,12 @@ static int docall(lua_State *L, int narg, int clear)
signal(SIGINT, SIG_DFL); signal(SIGINT, SIG_DFL);
lua_remove(L, base); /* remove traceback function */ lua_remove(L, base); /* remove traceback function */
/* force a complete garbage collection in case of errors */ /* force a complete garbage collection in case of errors */
if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0); if (status != 0)
lua_gc(L, LUA_GCCOLLECT, 0);
return status; return status;
} }
static int dolibrary(lua_State *L, const char *name, int clear) static int dolibrary(lua_State *L, const char *name, int clear) {
{
lua_getglobal(L, "require"); lua_getglobal(L, "require");
lua_pushstring(L, name); lua_pushstring(L, name);
return report(L, docall(L, 1, clear)); return report(L, docall(L, 1, clear));
...@@ -115,8 +111,7 @@ struct Smain { ...@@ -115,8 +111,7 @@ struct Smain {
int status; int status;
}; };
static void pushargv(lua_State *L, char **argv, int argc, int offset) static void pushargv(lua_State *L, char **argv, int argc, int offset) {
{
int i, j; int i, j;
lua_createtable(L, argc, 0); lua_createtable(L, argc, 0);
for (i = offset, j = 1; i < argc; i++, j++) { for (i = offset, j = 1; i < argc; i++, j++) {
...@@ -125,8 +120,7 @@ static void pushargv(lua_State *L, char **argv, int argc, int offset) ...@@ -125,8 +120,7 @@ static void pushargv(lua_State *L, char **argv, int argc, int offset)
} }
} }
static int pmain(lua_State *L) static int pmain(lua_State *L) {
{
struct Smain *s = (struct Smain *)lua_touserdata(L, 1); struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
globalL = L; globalL = L;
...@@ -148,8 +142,7 @@ static int pmain(lua_State *L) ...@@ -148,8 +142,7 @@ static int pmain(lua_State *L)
return 0; return 0;
} }
int main(int argc, char **argv) int main(int argc, char **argv) {
{
int status; int status;
struct Smain s; struct Smain s;
lua_State *L = lua_open(); /* create state */ lua_State *L = lua_open(); /* create state */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment