From: Hugo Villeneuve Date: Wed, 20 Oct 2021 14:41:31 +0000 (-0400) Subject: Convert options processing to getopt X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=1e4369cf0e46a10987a65ae8e582bea24d15274c;p=dockapps%2Fhvclock.git Convert options processing to getopt --- diff --git a/src/clock.c b/src/clock.c index 60ebdd4..2548f34 100644 --- a/src/clock.c +++ b/src/clock.c @@ -33,6 +33,7 @@ #include #include "common.h" +#include "options.h" #include "clock.h" #include "hvclock.h" #include "dockapp.h" @@ -294,7 +295,7 @@ draw_clock_hands(GtkWidget *clock, cairo_t *cr) /* seconds hand: * operates identically to the minute hand */ - if (hvclock_infos.show_seconds) { + if (opts.show_seconds) { angle = M_PI / 30 * seconds; draw_clock_hand_common(clock, cr, angle, HAND_SECONDS_LENGTH1_RATIO, @@ -404,7 +405,7 @@ draw_handler(GtkWidget *clock, cairo_t *cr, gpointer d G_GNUC_UNUSED) event->area.width, event->area.height); #elif defined (HAVE_GTK3) gdk_cairo_get_clip_rectangle(cr, &rect); - if (hvclock_infos.debug) + if (opts.debug) { printf("Redrawing (%d,%d+%d+%d)\n", rect.x, rect.y, rect.width, rect.height); @@ -464,17 +465,17 @@ hvclock_button_release(GtkWidget *clock, GdkEventButton *event) { (void) event; /* Unused parameter. */ - if (hvclock_infos.debug) + if (opts.debug) printf("%s: button released\n", PACKAGE_NAME); /* single-click --> changing viewing mode */ if (hvclock_mode == CLOCK_MODE) { hvclock_mode = CALENDAR_MODE; - if (hvclock_infos.debug) + if (opts.debug) printf("%s: switching to calendar mode\n", PACKAGE_NAME); } else { hvclock_mode = CLOCK_MODE; - if (hvclock_infos.debug) + if (opts.debug) printf("%s: switching to clock mode\n", PACKAGE_NAME); } @@ -503,7 +504,7 @@ hvclock_update(gpointer data) /* If the seconds have changed, force update only if displaying of * the seconds hand is requested. */ - if (hvclock_infos.show_seconds && (now->tm_sec != previous.tm_sec)) { + if (opts.show_seconds && (now->tm_sec != previous.tm_sec)) { update = true; } diff --git a/src/hvclock.c b/src/hvclock.c index 3c32428..69781f4 100644 --- a/src/hvclock.c +++ b/src/hvclock.c @@ -6,9 +6,6 @@ * This file is released under the GPLv2 */ -/* Define filename_M */ -#define HVCLOCK_M 1 - #if HAVE_CONFIG_H # include "config.h" #endif @@ -20,8 +17,8 @@ #include #include "common.h" -#include "dockapp.h" #include "options.h" +#include "dockapp.h" #include "clock.h" #include "hvclock.h" @@ -36,7 +33,7 @@ void ErrorLocation(const char *file, int line) int main(int argc, char *argv[]) { /* Initialization */ - ParseCommandLineOptions(argc, argv); + parse_command_line_options(argc, argv); /* Initializing and creating a DockApp window. */ dockapp_init(argc, argv); diff --git a/src/options.c b/src/options.c index a3ac0ee..34d808e 100644 --- a/src/options.c +++ b/src/options.c @@ -6,6 +6,9 @@ * This file is released under the GPLv2 */ +/* For proper scope */ +#define OPTIONS_M 1 + #if HAVE_CONFIG_H # include "config.h" #endif @@ -13,6 +16,7 @@ #include #include #include +#include #if STDC_HEADERS # include @@ -24,87 +28,89 @@ #include "hvclock.h" #include "options.h" -/******************************************************************************* - * Display the help message and exit - ******************************************************************************/ -static void DisplayUsage(void) -{ - printf("Usage: %s [OPTIONS]...\n", PACKAGE_NAME); - printf("Dockable analog clock and calendar.\n\n"); - printf - (" -d display debugging messages.\n"); - printf(" -h display this help and exit\n"); - printf(" -s display seconds hand\n"); - printf(" -version display version information"); - printf(" and exit\n"); - printf("\n"); -} +#define PACKAGE_DESCRIPTION "Dockable analog clock and calendar" -/******************************************************************************* - * Display version information and exit - ******************************************************************************/ -static void DisplayVersion(void) +static const char *opt_string = "vh?ds"; + +static const struct option long_opts[] = { + {"help", no_argument, NULL, 'h'}, + {"version", no_argument, NULL, 'v'}, + {"debug", no_argument, NULL, 'd'}, + {"seconds", no_argument, NULL, 's'}, + {NULL, no_argument, NULL, 0} +}; + +/* + * Display the help message + */ +static void +display_help(void) { - printf("\n"); - printf(" %s, version %s\n", PACKAGE_NAME, PACKAGE_VERSION); - printf(" Written by Hugo Villeneuve\n\n"); + printf("%s\n\n", PACKAGE_NAME " - " PACKAGE_DESCRIPTION); + printf("Usage: %s [OPTION]...\n", PACKAGE_NAME); + printf(" -h, --help Display this help and exit\n"); + printf(" -v, --version Display version information and exit\n"); + printf(" -d, --debug Display debugging messages\n"); + printf(" -s, --seconds Display seconds hand\n"); + printf("\n"); } -static void InvalidOption(char *message, /*@null@ */ char *string) +/* + * Display version information and exit + */ +static void +display_version(void) { - if (string == NULL) - fprintf(stderr, "%s: %s\n", PACKAGE_NAME, message); - else - fprintf(stderr, "%s: %s %s\n", PACKAGE_NAME, message, string); - - fprintf(stderr, "Try `%s -h' for more information.\n", PACKAGE_NAME); - - exit(EXIT_FAILURE); + printf(PACKAGE_STRING); + printf("\n"); } /******************************************************************************* * Initializes the different options passed as arguments on the command line. ******************************************************************************/ -void ParseCommandLineOptions(int argc, char *argv[]) +void +parse_command_line_options(int argc, char *argv[]) { - int i; - char *token; + int opt; + int long_index = 0; + + /* + * Setting default values. + * + * You don't ordinarily need to copy an option string, since it is a + * pointer into the original argv array, not into a static area that might + * be overwritten. + */ + opts.debug = false; + opts.show_seconds = false; + + /* Parse our arguments. */ + opt = getopt_long(argc, argv, opt_string, long_opts, &long_index); - /* Default values. */ - hvclock_infos.debug = false; - hvclock_infos.show_seconds = false; + while (opt != -1) + { + switch (opt) + { + case 'd': + opts.debug = true; + break; + case 's': + opts.show_seconds = true; + break; + case 'h': + case '?': + display_help(); + exit(EXIT_SUCCESS); + break; + case 'v': + display_version(); + exit(EXIT_SUCCESS); + break; + default: + /* You won't actually get here. */ + break; + } - for (i = 1; i < argc; i++) { - token = argv[i]; - switch (token[0]) { - case '-': - /* Processing options names */ - switch (token[1]) { - case 'd': - hvclock_infos.debug = true; - break; - case 'h': - DisplayUsage(); - exit(EXIT_SUCCESS); - case 's': - hvclock_infos.show_seconds = true; - break; - case 'v': - if (STREQ("version", &token[1])) { - DisplayVersion(); - exit(EXIT_SUCCESS); - } - InvalidOption("invalid option", token); - break; - default: - InvalidOption("invalid option", token); - break; - } /* end switch( token[1] ) */ - break; - default: - /* Processing options arguments */ - InvalidOption("invalid option", token); - break; - } /* end switch( token[0] ) */ - } /* end for */ + opt = getopt_long(argc, argv, opt_string, long_opts, &long_index); + } } diff --git a/src/options.h b/src/options.h index 49e8263..a90dd10 100644 --- a/src/options.h +++ b/src/options.h @@ -9,7 +9,23 @@ #ifndef OPTIONS_H #define OPTIONS_H 1 +struct opts_t +{ + int debug; + int show_seconds; +}; + +/* Exported variables */ +#undef _SCOPE_ +#ifdef OPTIONS_M +# define _SCOPE_ /**/ +#else +# define _SCOPE_ extern +#endif + +_SCOPE_ struct opts_t opts; + void -ParseCommandLineOptions(int argc, char *argv[]); +parse_command_line_options(int argc, char *argv[]); #endif /* OPTIONS_H */