hook gvariant vectors up to kdbus
[platform/upstream/glib.git] / glib / goption.c
index c6497e5..b9b23ba 100644 (file)
 #include <errno.h>
 
 #if defined __OpenBSD__
-#include <sys/types.h>
 #include <unistd.h>
-#include <sys/param.h>
 #include <sys/sysctl.h>
 #endif
 
@@ -250,6 +248,7 @@ struct _GOptionContext
   guint            help_enabled   : 1;
   guint            ignore_unknown : 1;
   guint            strv_mode      : 1;
+  guint            strict_posix   : 1;
 
   GOptionGroup    *main_group;
 
@@ -360,6 +359,7 @@ g_option_context_new (const gchar *parameter_string)
   context = g_new0 (GOptionContext, 1);
 
   context->parameter_string = g_strdup (parameter_string);
+  context->strict_posix = FALSE;
   context->help_enabled = TRUE;
   context->ignore_unknown = FALSE;
 
@@ -486,6 +486,64 @@ g_option_context_get_ignore_unknown_options (GOptionContext *context)
 }
 
 /**
+ * g_option_context_set_strict_posix:
+ * @context: a #GoptionContext
+ *
+ * Sets strict POSIX mode.
+ *
+ * By default, this mode is disabled.
+ *
+ * In strict POSIX mode, the first non-argument parameter encountered
+ * (eg: filename) terminates argument processing.  Remaining arguments
+ * are treated as non-options and are not attempted to be parsed.
+ *
+ * If strict POSIX mode is disabled then parsing is done in the GNU way
+ * where option arguments can be freely mixed with non-options.
+ *
+ * As an example, consider "ls foo -l".  With GNU style parsing, this
+ * will list "foo" in long mode.  In strict POSIX style, this will list
+ * the files named "foo" and "-l".
+ *
+ * It may be useful to force strict POSIX mode when creating "verb
+ * style" command line tools.  For example, the "gsettings" command line
+ * tool supports the global option "--schemadir" as well as many
+ * subcommands ("get", "set", etc.) which each have their own set of
+ * arguments.  Using strict POSIX mode will allow parsing the global
+ * options up to the verb name while leaving the remaining options to be
+ * parsed by the relevant subcommand (which can be determined by
+ * examining the verb name, which should be present in argv[1] after
+ * parsing).
+ *
+ * Since: 2.44
+ **/
+void
+g_option_context_set_strict_posix (GOptionContext *context,
+                                   gboolean        strict_posix)
+{
+  g_return_if_fail (context != NULL);
+
+  context->strict_posix = strict_posix;
+}
+
+/**
+ * g_option_context_get_strict_posix:
+ * @context: a #GoptionContext
+ *
+ * Returns whether strict POSIX code is enabled.
+ *
+ * See g_option_context_set_strict_posix() for more information.
+ *
+ * Since: 2.44
+ **/
+gboolean
+g_option_context_get_strict_posix (GOptionContext *context)
+{
+  g_return_val_if_fail (context != NULL, FALSE);
+
+  return context->strict_posix;
+}
+
+/**
  * g_option_context_add_group:
  * @context: a #GOptionContext
  * @group: the group to add
@@ -558,7 +616,7 @@ g_option_context_set_main_group (GOptionContext *context,
  *
  * Returns a pointer to the main group of @context.
  *
- * Return value: the main group of @context, or %NULL if @context doesn't
+ * Returns: the main group of @context, or %NULL if @context doesn't
  *  have a main group. Note that group belongs to @context and should
  *  not be modified or freed.
  *
@@ -1190,8 +1248,8 @@ parse_arg (GOptionContext *context,
     {
     case G_OPTION_ARG_NONE:
       {
-        change = get_change (context, G_OPTION_ARG_NONE,
-                             entry->arg_data);
+        (void) get_change (context, G_OPTION_ARG_NONE,
+                           entry->arg_data);
 
         *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
         break;
@@ -1763,13 +1821,16 @@ platform_get_argv0 (void)
   g_free (cmdline);
   return base_arg0;
 #elif defined __OpenBSD__
-  char **cmdline = NULL;
+  char **cmdline;
   char *base_arg0;
-  gsize len = PATH_MAX;
+  gsize len;
 
   int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
 
-  cmdline = (char **) realloc (cmdline, len);
+  if (sysctl (mib, G_N_ELEMENTS (mib), NULL, &len, NULL, 0) == -1)
+      return NULL;
+
+  cmdline = g_malloc0 (len);
 
   if (sysctl (mib, G_N_ELEMENTS (mib), cmdline, &len, NULL, 0) == -1)
     {
@@ -1818,7 +1879,7 @@ platform_get_argv0 (void)
  * automatic character set conversion of string and filename
  * arguments.
  *
- * Return value: %TRUE if the parsing was successful,
+ * Returns: %TRUE if the parsing was successful,
  *               %FALSE if an error occurred
  *
  * Since: 2.6
@@ -2037,6 +2098,7 @@ g_option_context_parse (GOptionContext   *context,
                       if (new_arg)
                         new_arg[arg_index] = '\0';
                       add_pending_null (context, &((*argv)[i]), new_arg);
+                      i = new_i;
                     }
                   else if (parsed)
                     {
@@ -2058,6 +2120,9 @@ g_option_context_parse (GOptionContext   *context,
             }
           else
             {
+              if (context->strict_posix)
+                stop_parsing = TRUE;
+
               /* Collect remaining args */
               if (context->main_group &&
                   !parse_remaining_arg (context, context->main_group, &i,
@@ -2163,7 +2228,7 @@ g_option_context_parse (GOptionContext   *context,
  *
  * Creates a new #GOptionGroup.
  *
- * Return value: a newly created option group. It should be added
+ * Returns: a newly created option group. It should be added
  *   to a #GOptionContext or freed with g_option_group_free().
  *
  * Since: 2.6