aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/server_main.c
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2022-09-11 20:23:24 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2022-09-11 20:23:24 +0300
commit23f9521d1abdeb599fbe81d0c196a9ee92f5fd8a (patch)
tree5ee5e59339bf2af98039ec4584f8a01410a95c11 /src/server_main.c
parentlog: refactoring (diff)
downloadcimple-23f9521d1abdeb599fbe81d0c196a9ee92f5fd8a.tar.gz
cimple-23f9521d1abdeb599fbe81d0c196a9ee92f5fd8a.zip
create SQLite database on startup
Diffstat (limited to '')
-rw-r--r--src/server_main.c42
1 files changed, 31 insertions, 11 deletions
diff --git a/src/server_main.c b/src/server_main.c
index b77a681..e2d811c 100644
--- a/src/server_main.c
+++ b/src/server_main.c
@@ -7,18 +7,32 @@
static struct settings default_settings()
{
- struct settings settings = {DEFAULT_PORT};
+ struct settings settings = {DEFAULT_PORT, NULL};
return settings;
}
-static void print_usage(const char *argv0)
+static void exit_with_usage(int ec, const char *argv0)
{
- printf("usage: %s [-h|--help] [-V|--version] [-p|--port PORT]\n", argv0);
+ FILE *dest = stdout;
+ if (ec)
+ dest = stderr;
+
+ fprintf(dest, "usage: %s [-h|--help] [-V|--version] [-p|--port PORT] [-s|--sqlite PATH]\n",
+ argv0);
+ exit(ec);
+}
+
+static void exit_with_usage_err(const char *argv0, const char *msg)
+{
+ if (msg)
+ fprintf(stderr, "usage error: %s\n", msg);
+ exit_with_usage(1, argv0);
}
-static void print_version()
+static void exit_with_version()
{
printf("%s\n", VERSION);
+ exit(0);
}
static int parse_settings(struct settings *settings, int argc, char *argv[])
@@ -31,29 +45,35 @@ static int parse_settings(struct settings *settings, int argc, char *argv[])
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"port", required_argument, 0, 'p'},
+ {"sqlite", required_argument, 0, 's'},
{0, 0, 0, 0},
};
- while ((opt = getopt_long(argc, argv, "hVp:", long_options, &longind)) != -1) {
+ while ((opt = getopt_long(argc, argv, "hVp:s:", long_options, &longind)) != -1) {
switch (opt) {
case 'h':
- print_usage(argv[0]);
- exit(0);
+ exit_with_usage(0, argv[0]);
break;
case 'V':
- print_version();
- exit(0);
+ exit_with_version();
break;
case 'p':
settings->port = optarg;
break;
+ case 's':
+ settings->sqlite_path = optarg;
+ break;
default:
- print_usage(argv[0]);
- exit(1);
+ exit_with_usage(1, argv[0]);
break;
}
}
+ if (!settings->sqlite_path) {
+ exit_with_usage_err(argv[0], "must specify the path to a SQLite database");
+ return -1;
+ }
+
return 0;
}