]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
add way to detect if an option was set or not
authorWilliam Jon McCann <jmccann@redhat.com>
Tue, 24 Feb 2009 01:09:25 +0000 (20:09 -0500)
committerWilliam Jon McCann <jmccann@redhat.com>
Thu, 26 Feb 2009 14:41:57 +0000 (09:41 -0500)
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
src/libply/ply-command-parser.h
src/main.c

index 0d1f379fae967c7204cd762f30501ce5b4cf1650..240fcfafd6277023316227fb743790ad70e6922b 100644 (file)
@@ -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;
 }
index 38078e40dc8a6f8728170f132065dfec0e3678ae..e37b5fccf05a148742b510fe3dc2c39c4ebbbfd0 100644 (file)
@@ -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);
index 5941c6d3a637d5fd32e101aed6022fcf3f4ae23a..566aaec364c4a4410e57159894ca44bfd539e0e3 100644 (file)
@@ -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))
         {