summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--main.c58
-rw-r--r--plop.c59
-rw-r--r--plop.h11
4 files changed, 78 insertions, 55 deletions
diff --git a/Makefile b/Makefile
index 3966232..8fba5b4 100644
--- a/Makefile
+++ b/Makefile
@@ -3,8 +3,9 @@ CFLAGS+=-I/usr/include/lua5.3
LDLIBS+=-llua5.3
PREFIX?=/usr/local
-plop: plop.o http.o
-plop.o http.o: http.h
+plop: main.o plop.o http.o
+main.o plop.o: plop.h
+main.o plop.o http.o: http.h
clean:
rm -f plop *.o
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..af36c5d
--- /dev/null
+++ b/main.c
@@ -0,0 +1,58 @@
+#include <poll.h>
+#include <string.h>
+
+#include <lauxlib.h>
+#include <lua.h>
+#include <lualib.h>
+
+#include "plop.h"
+
+/// Standard entry point for the program.
+/// \param argc Argument count
+/// \param argv Argument array
+/// \return Error code
+int main(int argc, char ** argv)
+{
+ lua_State * L = luaL_newstate();
+ luaL_openlibs(L);
+
+ if (3 != argc)
+ {
+ return 1; // TODO: Document error codes/print usage information
+ }
+
+ if (LUA_OK != luaL_dofile(L, argv[2]))
+ {
+ return 2; // TODO: Document error codes/print usage information
+ }
+
+ static const int max_clients = 200;
+
+ int fdc = 0;
+ struct request * requests[max_clients];
+ struct pollfd fdv[max_clients];
+
+ memset(requests, 0, sizeof(requests));
+ memset(fdv, 0, sizeof(fdv));
+
+ fdv[0].fd = make_server(NULL, argv[1]);
+ fdv[0].events = POLLIN;
+ fdc++;
+
+ if (-1 == fdv[0].fd)
+ {
+ return 1; // TODO: Document error codes/print usage information
+ }
+
+ while (0 < fdc && -1 != poll(fdv, fdc, -1))
+ {
+ int shift_by = 0;
+
+ for (int i = 1; i < fdc; ++i)
+ {
+ shift_by = handle_client(L, &fdv[i], &requests[i], shift_by);
+ }
+
+ fdc = handle_server(fdv, fdc - shift_by, max_clients);
+ }
+}
diff --git a/plop.c b/plop.c
index 7e53457..dc6f819 100644
--- a/plop.c
+++ b/plop.c
@@ -1,8 +1,7 @@
+#include "plop.h"
+
#include <errno.h>
#include <fcntl.h>
-#include <lauxlib.h>
-#include <lua.h>
-#include <lualib.h>
#include <netdb.h>
#include <poll.h>
#include <stdio.h>
@@ -12,6 +11,10 @@
#include <sys/types.h>
#include <unistd.h>
+#include <lauxlib.h>
+#include <lua.h>
+#include <lualib.h>
+
#include "http.h"
/// Tries to create, bind and start listening on INET server socket.
@@ -178,53 +181,3 @@ int handle_server(struct pollfd * fdv, int fdc, const int size)
return fdc;
}
-
-/// Standard entry point for the program.
-/// \param argc Argument count
-/// \param argv Argument array
-/// \return Error code
-int main(int argc, char ** argv)
-{
- lua_State * L = luaL_newstate();
- luaL_openlibs(L);
-
- if (3 != argc)
- {
- return 1; // TODO: Document error codes/print usage information
- }
-
- if (LUA_OK != luaL_dofile(L, argv[2]))
- {
- return 2; // TODO: Document error codes/print usage information
- }
-
- static const int max_clients = 200;
-
- int fdc = 0;
- struct request * requests[max_clients];
- struct pollfd fdv[max_clients];
-
- memset(requests, 0, sizeof(requests));
- memset(fdv, 0, sizeof(fdv));
-
- fdv[0].fd = make_server(NULL, argv[1]);
- fdv[0].events = POLLIN;
- fdc++;
-
- if (-1 == fdv[0].fd)
- {
- return 1; // TODO: Document error codes/print usage information
- }
-
- while (0 < fdc && -1 != poll(fdv, fdc, -1))
- {
- int shift_by = 0;
-
- for (int i = 1; i < fdc; ++i)
- {
- shift_by = handle_client(L, &fdv[i], &requests[i], shift_by);
- }
-
- fdc = handle_server(fdv, fdc - shift_by, max_clients);
- }
-}
diff --git a/plop.h b/plop.h
new file mode 100644
index 0000000..2ecd678
--- /dev/null
+++ b/plop.h
@@ -0,0 +1,11 @@
+#pragma once
+
+#include <poll.h>
+
+#include <lua.h>
+
+#include "http.h"
+
+int make_server(const char *, const char *);
+int handle_client(lua_State * L, struct pollfd *, struct request **, const int);
+int handle_server(struct pollfd *, int, const int);