changeset 11:b9b046818b0c

pasterd: support configuration
author David Demelier <markand@malikania.fr>
date Wed, 05 Feb 2020 17:28:53 +0100
parents 75cfe3795de3
children 93f0440d452e
files Makefile config.h pasterd.c
diffstat 3 files changed, 34 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Wed Feb 05 15:57:33 2020 +0100
+++ b/Makefile	Wed Feb 05 17:28:53 2020 +0100
@@ -22,7 +22,7 @@
 CFLAGS=         -std=c18 -pedantic -D_XOPEN_SOURCE=700 -g
 # Release
 # CFLAGS=         -std=c18 -Wall -Wextra -pedantic -O3 -DNDEBUG -D_XOPEN_SOURCE=700
-LDFLAGS=        -static -lkcgi -lz
+LDFLAGS=        -lkcgi -lz
 
 SRCS=           config.c database.c http.c log.c pasterd.c paste.c util.c
 OBJS=           ${SRCS:.c=.o}
--- a/config.h	Wed Feb 05 15:57:33 2020 +0100
+++ b/config.h	Wed Feb 05 17:28:53 2020 +0100
@@ -23,6 +23,7 @@
 
 extern struct config {
 	char themedir[PATH_MAX];
+	char databasepath[PATH_MAX];
 	int verbosity;
 } config;
 
--- a/pasterd.c	Wed Feb 05 15:57:33 2020 +0100
+++ b/pasterd.c	Wed Feb 05 17:28:53 2020 +0100
@@ -16,20 +16,28 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <stdio.h>
 #include <stdlib.h>
+#include <stdnoreturn.h>
 #include <time.h>
 #include <unistd.h>
 
+#include "config.h"
 #include "database.h"
 #include "http.h"
 #include "log.h"
+#include "util.h"
 
 static void
 init(void)
 {
 	srand(time(NULL));
 	log_open();
-	database_open("test.db");
+
+	if (!config.databasepath[0])
+		die("abort: no database specified\n");
+	if (!database_open(config.databasepath))
+		die("abort: could not open database\n");
 }
 
 static void
@@ -38,25 +46,45 @@
 	database_finish();
 	log_finish();
 }
+
+static noreturn void
+usage(void)
+{
+	fprintf(stderr, "usage: paster [-f] [-d database-path] [-t theme-directory]\n");
+	exit(1);
+}
  
 int
 main(int argc, char **argv)
 {
-	init();
-
+	const char *value;
 	int opt;
 	void (*run)(void) = &(http_cgi_run);
 
-	while ((opt = getopt(argc, argv, "f")) != -1) {
+	/* Seek environment variables before options. */
+	if ((value = getenv("PASTERD_DATABASE_PATH")))
+		snprintf(config.databasepath, sizeof (config.databasepath), "%s", value);
+	if ((value = getenv("PASTERD_THEME_DIR")))
+		snprintf(config.themedir, sizeof (config.themedir), "%s", value);
+
+	while ((opt = getopt(argc, argv, "d:ft:")) != -1) {
 		switch (opt) {
+		case 'd':
+			snprintf(config.databasepath, sizeof (config.databasepath), "%s", optarg);
+			break;
+		case 't':
+			snprintf(config.themedir, sizeof (config.themedir), "%s", optarg);
+			break;
 		case 'f':
 			run = &(http_fcgi_run);
 			break;
 		default:
+			usage();
 			break;
 		}
 	}
 
+	init();
 	run();
 	quit();
 }