From cd1a55a0d1f77aedb078ab9fef96dac94b41c86b Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Wed, 11 Feb 2015 02:07:52 +0100 Subject: Import option handler from ponymap --- kike.c | 82 +++++++++++++++++++++++++++--------------------------------------- 1 file changed, 33 insertions(+), 49 deletions(-) (limited to 'kike.c') diff --git a/kike.c b/kike.c index f9eb5c3..422d732 100644 --- a/kike.c +++ b/kike.c @@ -1,7 +1,7 @@ /* * kike.c: the experimental IRC daemon * - * Copyright (c) 2014, Přemysl Janouch + * Copyright (c) 2014 - 2015, Přemysl Janouch * All rights reserved. * * Permission to use, copy, modify, and/or distribute this software for any @@ -3072,63 +3072,47 @@ daemonize (void) exit_fatal ("failed to reopen FD's: %s", strerror (errno)); } -static void -print_usage (const char *program_name) -{ - fprintf (stderr, - "Usage: %s [OPTION]...\n" - "Experimental IRC server.\n" - "\n" - " -d, --debug run in debug mode (do not daemonize)\n" - " -h, --help display this help and exit\n" - " -V, --version output version information and exit\n" - " --write-default-cfg [filename]\n" - " write a default configuration file and exit\n", - program_name); -} - int main (int argc, char *argv[]) { - const char *invocation_name = argv[0]; - - static struct option opts[] = + static const struct opt opts[] = { - { "debug", no_argument, NULL, 'd' }, - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' }, - { "write-default-cfg", optional_argument, NULL, 'w' }, - { NULL, 0, NULL, 0 } + { 'd', "debug", NULL, 0, "run in debug mode (do not daemonize)" }, + { 'h', "help", NULL, 0, "display this help and exit" }, + { 'V', "version", NULL, 0, "output version information and exit" }, + { 'w', "write-default-cfg", "FILENAME", + OPT_OPTIONAL_ARG | OPT_LONG_ONLY, + "write a default configuration file and exit" }, + { 0, NULL, NULL, 0, NULL } }; - while (1) - { - int c, opt_index; + struct opt_handler oh; + opt_handler_init (&oh, argc, argv, opts, NULL, "Experimental IRC daemon."); - c = getopt_long (argc, argv, "dhV", opts, &opt_index); - if (c == -1) - break; - - switch (c) - { - case 'd': - g_debug_mode = true; - break; - case 'h': - print_usage (invocation_name); - exit (EXIT_SUCCESS); - case 'V': - printf (PROGRAM_NAME " " PROGRAM_VERSION "\n"); - exit (EXIT_SUCCESS); - case 'w': - call_write_default_config (optarg, g_config_table); - exit (EXIT_SUCCESS); - default: - print_error ("wrong options"); - exit (EXIT_FAILURE); - } + int c; + while ((c = opt_handler_get (&oh)) != -1) + switch (c) + { + case 'd': + g_debug_mode = true; + break; + case 'h': + opt_handler_usage (&oh, stdout); + exit (EXIT_SUCCESS); + case 'V': + printf (PROGRAM_NAME " " PROGRAM_VERSION "\n"); + exit (EXIT_SUCCESS); + case 'w': + call_write_default_config (optarg, g_config_table); + exit (EXIT_SUCCESS); + default: + print_error ("wrong options"); + opt_handler_usage (&oh, stderr); + exit (EXIT_FAILURE); } + opt_handler_free (&oh); + print_status (PROGRAM_NAME " " PROGRAM_VERSION " starting"); setup_signal_handlers (); -- cgit v1.2.3-54-g00ecf