Cleaned up argument parsing to match what was in the docs

This commit is contained in:
Jordan Bancino 2022-09-24 19:41:39 -04:00
parent dac0633c0b
commit b5d538f2ce
5 changed files with 29 additions and 41 deletions

View file

@ -50,7 +50,7 @@ Phase 2: Building a foundation
[x] Handle requests [x] Handle requests
[ ] Data abstraction layer [ ] Data abstraction layer
[x] Error generation [x] Error generation
[ ] Properly implement the command line options as stated in telodendria(8) [x] Properly implement the command line options as stated in telodendria(8)
Phase 3: Welcome to Matrix Phase 3: Welcome to Matrix

View file

@ -1,4 +1,4 @@
.Dd $Mdocdate: September 22 2022 $ .Dd $Mdocdate: September 24 2022 $
.Dt TELODENDRIA 8 .Dt TELODENDRIA 8
.Os Telodendria Project .Os Telodendria Project
.Sh NAME .Sh NAME
@ -6,7 +6,7 @@
.Nd Matrix homeserver daemon .Nd Matrix homeserver daemon
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl hnVv .Op Fl nVv
.Op Fl f Ar file .Op Fl f Ar file
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
@ -18,9 +18,6 @@ The options are as follows:
.It Fl f Ar file .It Fl f Ar file
Specify an alternate configuration file. The default is Specify an alternate configuration file. The default is
.Pa /etc/telodendria.conf . .Pa /etc/telodendria.conf .
.It Fl h
Show a brief help message. This message will be very short; for
full documentation, consult this manual page.
.It Fl n .It Fl n
Configtest mode. Only check the configuration file for validity. Configtest mode. Only check the configuration file for validity.
.It Fl V .It Fl V

View file

@ -52,7 +52,8 @@ TelodendriaSignalHandler(int signalNo)
typedef enum ArgFlag typedef enum ArgFlag
{ {
ARG_VERSION = (1 << 0), ARG_VERSION = (1 << 0),
ARG_USAGE = (1 << 1) ARG_CONFIGTEST = (1 << 1),
ARG_VERBOSE = (1 << 2)
} ArgFlag; } ArgFlag;
static void static void
@ -77,15 +78,6 @@ TelodendriaPrintHeader(LogConfig * lc)
Log(lc, LOG_MESSAGE, ""); Log(lc, LOG_MESSAGE, "");
} }
static void
TelodendriaPrintUsage(LogConfig * lc)
{
Log(lc, LOG_MESSAGE, "Usage:");
Log(lc, LOG_MESSAGE, " -c <file> Configuration file ('-' for stdin).");
Log(lc, LOG_MESSAGE, " -V Print the header, then exit.");
Log(lc, LOG_MESSAGE, " -h Print this usage, then exit.");
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -95,7 +87,7 @@ main(int argc, char **argv)
/* Arg parsing */ /* Arg parsing */
int opt; int opt;
int flags = 0; int flags = 0;
char *configArg = NULL; char *configArg = "/etc/telodendria.conf";
/* Config file */ /* Config file */
FILE *configFile = NULL; FILE *configFile = NULL;
@ -135,18 +127,25 @@ main(int argc, char **argv)
} }
#endif #endif
while ((opt = getopt(argc, argv, "c:Vh")) != -1) while ((opt = getopt(argc, argv, "f:Vvn")) != -1)
{ {
switch (opt) switch (opt)
{ {
case 'c': case 'f':
configArg = optarg; configArg = optarg;
break; break;
case 'V': case 'V':
flags |= ARG_VERSION; flags |= ARG_VERSION;
break; break;
case 'h': case 'v':
flags |= ARG_USAGE; flags |= ARG_VERBOSE;
break;
case 'n':
flags |= ARG_CONFIGTEST;
break;
case '?':
exit = EXIT_FAILURE;
goto finish;
default: default:
break; break;
} }
@ -157,20 +156,6 @@ main(int argc, char **argv)
goto finish; goto finish;
} }
if (flags & ARG_USAGE)
{
TelodendriaPrintUsage(lc);
goto finish;
}
if (!configArg)
{
Log(lc, LOG_ERROR, "No configuration file specified.");
TelodendriaPrintUsage(lc);
exit = EXIT_FAILURE;
goto finish;
}
if (strcmp(configArg, "-") == 0) if (strcmp(configArg, "-") == 0)
{ {
configFile = stdin; configFile = stdin;
@ -219,6 +204,12 @@ main(int argc, char **argv)
ConfigFree(config); ConfigFree(config);
if (flags & ARG_CONFIGTEST)
{
Log(lc, LOG_MESSAGE, "Configuration is OK.");
goto finish;
}
#ifdef __OpenBSD__ #ifdef __OpenBSD__
if (unveil(tConfig->chroot, "rwc") != 0) if (unveil(tConfig->chroot, "rwc") != 0)
{ {
@ -238,7 +229,7 @@ main(int argc, char **argv)
LogConfigFlagClear(lc, LOG_FLAG_COLOR); LogConfigFlagClear(lc, LOG_FLAG_COLOR);
} }
LogConfigLevelSet(lc, tConfig->logLevel); LogConfigLevelSet(lc, flags & ARG_VERBOSE ? LOG_DEBUG : tConfig->logLevel);
if (tConfig->logOut) if (tConfig->logOut)
{ {

View file

@ -100,7 +100,7 @@ recipe_build() {
recipe_run() { recipe_run() {
if [ -f "build/$PROG" ]; then if [ -f "build/$PROG" ]; then
"build/$PROG" -c contrib/development.conf "build/$PROG" -f contrib/development.conf
else else
echo "build/$PROG does not exist; build it first." echo "build/$PROG does not exist; build it first."
fi fi