From 26d9f676dd487abb19dca741664805409a16f3b4 Mon Sep 17 00:00:00 2001 From: William Jon McCann Date: Mon, 23 Feb 2009 20:09:25 -0500 Subject: [PATCH] add way to detect if an option was set or not With integer options there's no obvious value to mean "unset" like there is for string types (and to a lesser extent boolean types), so this commit adds a mechanism. --- src/libply/ply-command-parser.c | 146 ++++++++++++++++++++++---------- src/libply/ply-command-parser.h | 13 ++- src/main.c | 12 ++- 3 files changed, 123 insertions(+), 48 deletions(-) diff --git a/src/libply/ply-command-parser.c b/src/libply/ply-command-parser.c index 0d1f379f..240fcfaf 100644 --- a/src/libply/ply-command-parser.c +++ b/src/libply/ply-command-parser.c @@ -45,7 +45,7 @@ typedef struct char *name; char *description; ply_command_option_type_t type; - + uint32_t was_set : 1; ply_command_option_result_t result; } ply_command_option_t; @@ -463,6 +463,52 @@ ply_command_parser_get_command (ply_command_parser_t *parser, return NULL; } +static void +ply_command_parser_get_option_for_command (ply_command_parser_t *parser, + ply_command_t *command, + const char *option_name, + void *option_result, + bool *option_is_set) +{ + ply_command_option_t *option; + + option = ply_command_get_option (command, option_name); + + if (option == NULL) + return; + + if (option_result != NULL) + { + switch (option->type) + { + case PLY_COMMAND_OPTION_TYPE_FLAG: + case PLY_COMMAND_OPTION_TYPE_BOOLEAN: + { + *(bool *)option_result = option->result.as_boolean; + } + break; + + case PLY_COMMAND_OPTION_TYPE_STRING: + { + if (option->result.as_string != NULL) + *(char **)option_result = strdup (option->result.as_string); + else + *(char **)option_result = NULL; + } + break; + + case PLY_COMMAND_OPTION_TYPE_INTEGER: + { + *(int *)option_result = option->result.as_integer; + } + break; + } + } + + if (option_is_set != NULL) + *option_is_set = option->was_set; +} + static void ply_command_parser_get_options_for_command (ply_command_parser_t *parser, ply_command_t *command, @@ -476,57 +522,41 @@ ply_command_parser_get_options_for_command (ply_command_parser_t *parser, while (option_name != NULL) { - ply_command_option_t *option; + void *option_result; - option = ply_command_get_option (command, option_name); + option_result = va_arg (args, void *); - if (option != NULL) - { - switch (option->type) - { - case PLY_COMMAND_OPTION_TYPE_FLAG: - case PLY_COMMAND_OPTION_TYPE_BOOLEAN: - { - bool *option_result; - - option_result = va_arg (args, bool *); - - *option_result = option->result.as_boolean; - } - break; - - case PLY_COMMAND_OPTION_TYPE_STRING: - { - char **option_result; - - option_result = va_arg (args, char **); - if (option->result.as_string != NULL) - *option_result = strdup (option->result.as_string); - else - *option_result = NULL; - } - break; - - case PLY_COMMAND_OPTION_TYPE_INTEGER: - { - int *option_result; - - option_result = va_arg (args, int *); - - *option_result = option->result.as_integer; - } - break; - } - } + ply_command_parser_get_option_for_command (parser, + command, + option_name, + option_result, + NULL); option_name = va_arg (args, const char *); } } +void +ply_command_parser_get_option (ply_command_parser_t *parser, + const char *option_name, + void *option_result, + bool *option_is_set) +{ + assert (parser != NULL); + assert (option_name != NULL); + + ply_command_parser_get_option_for_command (parser, + parser->main_command, + option_name, + option_result, + option_is_set); +} + void ply_command_parser_get_options (ply_command_parser_t *parser, const char *option_name, /* - void * option_result */ + void * option_result, + bool * option_was_set */ ...) { va_list args; @@ -539,11 +569,38 @@ ply_command_parser_get_options (ply_command_parser_t *parser, va_end (args); } +void +ply_command_parser_get_command_option (ply_command_parser_t *parser, + const char *command_name, + const char *option_name, + void *option_result, + bool *option_is_set) +{ + ply_command_t *command; + + assert (parser != NULL); + assert (command_name != NULL); + assert (option_name != NULL); + + command = ply_command_parser_get_command (parser, command_name); + + if (command == NULL) + return; + + ply_command_parser_get_option_for_command (parser, + parser->main_command, + option_name, + option_result, + option_is_set); +} + + void ply_command_parser_get_command_options (ply_command_parser_t *parser, const char *command_name, const char *option_name, /* - void * option_result */ + void * option_result, + bool * option_was_set */ ...) { ply_command_t *command; @@ -726,7 +783,8 @@ ply_command_read_option (ply_command_t *command, ply_list_remove_node (arguments, node); - ply_command_option_read_arguments (option, arguments); + if (ply_command_option_read_arguments (option, arguments)) + option->was_set = true; return option; } diff --git a/src/libply/ply-command-parser.h b/src/libply/ply-command-parser.h index 38078e40..e37b5fcc 100644 --- a/src/libply/ply-command-parser.h +++ b/src/libply/ply-command-parser.h @@ -59,12 +59,21 @@ void ply_command_parser_get_options (ply_command_parser_t *parser, const char *option_name, /* void * option_result */ ...); - +void ply_command_parser_get_option (ply_command_parser_t *parser, + const char *option_name, + void *option_result, + bool *option_is_set); void ply_command_parser_get_command_options (ply_command_parser_t *parser, const char *command_name, const char *option_name, /* - void * option_result */ + void * option_result, + bool * option_was_set */ ...); +void ply_command_parser_get_command_option (ply_command_parser_t *parser, + const char *command_name, + const char *option_name, + void *option_result, + bool *option_is_set); void ply_command_parser_stop_parsing_arguments (ply_command_parser_t *parser); char *ply_command_parser_get_help_string (ply_command_parser_t *parser); diff --git a/src/main.c b/src/main.c index 5941c6d3..566aaec3 100644 --- a/src/main.c +++ b/src/main.c @@ -1230,6 +1230,8 @@ main (int argc, bool should_help = false; bool no_daemon = false; bool debug = false; + bool was_set = false; + int attach_to_session; ply_daemon_handle_t *daemon_handle; char *mode_string = NULL; @@ -1262,8 +1264,11 @@ main (int argc, "mode", &mode_string, "no-daemon", &no_daemon, "debug", &debug, - "attach-to-session", &state.ptmx, NULL); + ply_command_parser_get_option (state.command_parser, + "attach-to-session", + &attach_to_session, + &was_set); if (should_help) { char *help_string; @@ -1292,6 +1297,9 @@ main (int argc, free (mode_string); } + if (was_set) + state.ptmx = attach_to_session; + if (geteuid () != 0) { ply_error ("plymouthd must be run as root user"); @@ -1335,7 +1343,7 @@ main (int argc, state.boot_buffer = ply_buffer_new (); - if (state.ptmx != 0) + if (state.ptmx != -1) { if (!attach_to_running_session (&state)) { -- 2.47.3