From dcad7974ced286ce477e86f6f9fd77d8c79ebfa3 Mon Sep 17 00:00:00 2001 From: brain Date: Sun, 24 Dec 2006 11:13:31 +0000 Subject: Change argument parsing to use getopt_long_only(). git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@6093 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/inspircd.cpp | 74 +++++++++++++++++++++++++++++--------------------------- 1 file 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 #include #include +#include #include "modules.h" #include "mode.h" #include "xline.h" @@ -28,6 +29,10 @@ #include "exitcodes.h" #include +#ifdef _GNU_SOURCE +#include +#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 ]\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 ]\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); -- cgit v1.2.3