summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-08-24 18:22:36 +0200
committerAki <please@ignore.pl>2020-08-24 18:22:36 +0200
commit96b6d2a1cfccb060fadce11c50737000c7bf036c (patch)
treea484c2e7ca89322796976f3e9391b3c4c4d879a3
parentef5e5e6691f02844499c57f16b0bb470a83aa92f (diff)
downloadplop-96b6d2a1cfccb060fadce11c50737000c7bf036c.zip
plop-96b6d2a1cfccb060fadce11c50737000c7bf036c.tar.gz
plop-96b6d2a1cfccb060fadce11c50737000c7bf036c.tar.bz2
Added getopt to main
-rw-r--r--main.c36
-rw-r--r--plop.120
2 files changed, 42 insertions, 14 deletions
diff --git a/main.c b/main.c
index 4b5cb59..2259caa 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <sys/epoll.h>
+#include <unistd.h>
#include <lauxlib.h>
#include <lua.h>
@@ -9,11 +10,12 @@
/// Prints program usage to standard error.
/// \param name Name of the executable from argv
-static void usage(char * name)
+static void usage(const char * const name)
{
- // TODO: Extend command line interface with e.g. getopt.
dprintf(2,
- "Usage: %s <port> <handler>\n",
+ "Usage: %s [-p PORT] HANDLER\n"
+ "Starts plop server listening on PORT and serving HANDLER.\n\n"
+ " -p\tstart listening on PORT (default: 8080)\n",
name);
}
@@ -26,13 +28,33 @@ int main(int argc, char ** argv)
lua_State * L = luaL_newstate();
luaL_openlibs(L);
- if (3 != argc)
+ const char * service = "8080";
+ int opt;
+
+ while (-1 != (opt = getopt(argc, argv, "p:")))
+ {
+ switch (opt)
+ {
+ case 'p':
+ {
+ service = optarg;
+ break;
+ }
+ default:
+ {
+ usage(argv[0]);
+ return 1; // TODO: Extend error handling in main().
+ }
+ }
+ }
+
+ if (optind >= argc)
{
usage(argv[0]);
- return 1; // TODO: Handle errors properly in main().
+ return 8;
}
- if (LUA_OK != load_handler(L, argv[2]))
+ if (LUA_OK != load_handler(L, argv[optind]))
{
return 2;
}
@@ -47,7 +69,7 @@ int main(int argc, char ** argv)
struct epoll_event e;
e.events = EPOLLIN;
e.data.ptr = NULL; // TODO: Consider putting server's Lua state in here?
- const int server = make_server(NULL, argv[1]); // TODO: Check server's fd before ctl?
+ const int server = make_server(NULL, service); // TODO: Check server's fd before ctl?
if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, server, &e))
{
diff --git a/plop.1 b/plop.1
index 8d8eefd..4aea823 100644
--- a/plop.1
+++ b/plop.1
@@ -1,15 +1,21 @@
-.TH plop 1 "2020-08-15"
+.TH plop 1 "2020-08-24"
.SH NAME
-plop \- Hackable small standalone engine for Lua web applications
+plop \- Small hackable standalone engine for Lua web applications
.SH SYNOPSIS
-.B plop
-.IR port
-.IR handler
+.B plop [-p
+.IR PORT ]
+.IR HANDLER
.SH DESCRIPTION
.B plop
loads
-.IR handler
-Lua script and starts a HTTP/1.1 server listening on a designated port. The script is loaded as if
+.IR HADNLER
+Lua script and starts a HTTP/1.1 server listening on a designated
+.IR PORT .
+If
+.IR PORT
+is not specified server starts listening on
+.B 8080
+by default. The script is loaded as if
.B require
was used with module name
.B handler