summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2021-02-23 23:11:36 +0100
committerAki <please@ignore.pl>2021-02-23 23:11:36 +0100
commita8bcb7a654691a9a41ba193f16dca38bdd65bfa0 (patch)
treec0ac28b28f9377bc0f0e68aa1b7ee77044ff3772
parentc9c0384ebf1b882dd0035530e5949bfcbf979d2b (diff)
downloadplop-a8bcb7a654691a9a41ba193f16dca38bdd65bfa0.zip
plop-a8bcb7a654691a9a41ba193f16dca38bdd65bfa0.tar.gz
plop-a8bcb7a654691a9a41ba193f16dca38bdd65bfa0.tar.bz2
Moved main Lua initialization to a function
-rw-r--r--main.c12
-rw-r--r--plop.c17
-rw-r--r--plop.h1
3 files changed, 25 insertions, 5 deletions
diff --git a/main.c b/main.c
index 15747f1..1f932fc 100644
--- a/main.c
+++ b/main.c
@@ -2,9 +2,7 @@
#include <sys/epoll.h>
#include <unistd.h>
-#include <lauxlib.h>
#include <lua.h>
-#include <lualib.h>
#include "plop.h"
@@ -25,9 +23,6 @@ static void usage(const char * const name)
/// \return Error code
int main(int argc, char ** argv)
{
- lua_State * L = luaL_newstate();
- luaL_openlibs(L);
-
const char * service = "8080";
int opt;
@@ -54,6 +49,13 @@ int main(int argc, char ** argv)
return 8;
}
+ lua_State * L = plop_initialize_lua();
+
+ if (NULL == L)
+ {
+ return 9;
+ }
+
if (LUA_OK != plop_load_handler(L, argv[optind]))
{
return 2;
diff --git a/plop.c b/plop.c
index c64ad11..592fca0 100644
--- a/plop.c
+++ b/plop.c
@@ -13,11 +13,28 @@
#include <lauxlib.h>
#include <lua.h>
+#include <lualib.h>
#include "connection.h"
#include "request.h"
#include "response.h"
+/// Initializes new Lua state for the server.
+/// \return Lua state
+lua_State * plop_initialize_lua(void)
+{
+ lua_State * L = luaL_newstate();
+
+ if (NULL == L)
+ {
+ return NULL;
+ }
+
+ luaL_openlibs(L);
+
+ return L;
+}
+
/// Tries to create, bind and start listening on INET server socket.
/// \param node Hostname
/// \param service Port
diff --git a/plop.h b/plop.h
index 8003441..5735685 100644
--- a/plop.h
+++ b/plop.h
@@ -4,6 +4,7 @@
#include <lua.h>
+lua_State * plop_initialize_lua(void);
int plop_make_server(const char *, const char *);
int plop_load_handler(lua_State *, const char *);
int plop_handle_client(lua_State *, struct epoll_event *);