summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2006-12-24 11:13:31 +0000
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2006-12-24 11:13:31 +0000
commitdcad7974ced286ce477e86f6f9fd77d8c79ebfa3 (patch)
tree88dc888c6a9b0f1058b2820283b16f38f6933fad
parentce20c7be2f17472cfa38bb1a3c8f4458a8b16f5b (diff)
Change argument parsing to use getopt_long_only().
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@6093 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r--src/inspircd.cpp74
1 files changed, 38 insertions, 36 deletions
diff --git a/src/inspircd.cpp b/src/inspircd.cpp
index d5146e900..9c2eef295 100644
--- a/src/inspircd.cpp
+++ b/src/inspircd.cpp
@@ -17,6 +17,7 @@
#include <dirent.h>
#include <exception>
#include <fstream>
+#include <unistd.h>
#include "modules.h"
#include "mode.h"
#include "xline.h"
@@ -28,6 +29,10 @@
#include "exitcodes.h"
#include <dlfcn.h>
+#ifdef _GNU_SOURCE
+#include <getopt.h>
+#endif
+
using irc::sockets::NonBlocking;
using irc::sockets::Blocking;
using irc::sockets::insp_ntoa;
@@ -259,6 +264,8 @@ InspIRCd::InspIRCd(int argc, char** argv)
{
int found_ports = 0;
FailedPortList pl;
+ int do_nofork = 0, do_debug = 0, do_nolog = 0; /* flag variables */
+ char c = 0;
modules.resize(255);
factory.resize(255);
@@ -287,48 +294,43 @@ InspIRCd::InspIRCd(int argc, char** argv)
printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
Exit(EXIT_STATUS_CONFIG);
}
+
*this->LogFileName = 0;
- if (argc > 1) {
- for (int i = 1; i < argc; i++)
+
+ struct option longopts[] =
+ {
+ { "nofork", no_argument, &do_nofork, 1 },
+ { "logfile", required_argument, NULL, 'f' },
+ { "debug", no_argument, &do_debug, 1 },
+ { "nolog", no_argument, &do_nolog, 1 },
+ { 0, 0, 0, 0 }
+ };
+
+ while ((c = getopt_long_only(argc, argv, ":f:", longopts, NULL)) != -1)
+ {
+ switch (c)
{
- if (!strcmp(argv[i],"-nofork"))
- {
- Config->nofork = true;
- }
- else if(!strcmp(argv[i],"-debug"))
- {
- Config->forcedebug = true;
- }
- else if(!strcmp(argv[i],"-nolog"))
- {
- Config->writelog = false;
- }
- else if (!strcmp(argv[i],"-wait"))
- {
- sleep(6);
- }
- else if (!strcmp(argv[i],"-logfile"))
- {
- if (argc > i+1)
- {
- strlcpy(LogFileName,argv[i+1],MAXBUF);
- printf("LOG: Setting logfile to %s\n",LogFileName);
- }
- else
- {
- printf("ERROR: The -logfile parameter must be followed by a log file name and path.\n");
- Exit(EXIT_STATUS_CONFIG);
- }
- i++;
- }
- else
- {
- printf("Usage: %s [-nofork] [-nolog] [-debug] [-wait] [-logfile <filename>]\n",argv[0]);
+ case 'f':
+ /* Log filename was set */
+ strlcpy(LogFileName, optarg, MAXBUF);
+ printf("LOG: Setting logfile to %s\n", LogFileName);
+ break;
+ case 0:
+ /* getopt_long_only() set an int variable, just keep going */
+ break;
+ default:
+ /* Unknown parameter! DANGER, INTRUDER.... err.... yeah. */
+ printf("Usage: %s [--nofork] [--nolog] [--debug] [--logfile <filename>]\n", argv[0]);
Exit(EXIT_STATUS_ARGV);
- }
+ break;
}
}
+ /* Set the finished argument values */
+ Config->nofork = do_nofork;
+ Config->forcedebug = do_debug;
+ Config->writelog = !do_nolog;
+
strlcpy(Config->MyExecutable,argv[0],MAXBUF);
this->OpenLog(argv, argc);