X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=glib%2Fgoption.c;h=b9b23ba29133c03e72eea1b479d50a07da76f0c7;hb=20f6cc2a10ba26860e7a6d27c100deadb5497772;hp=9d251ff20725377e05f300144939d02af54f1fad;hpb=df990914cf28a4b8417597d5b2208aa2bf34e694;p=platform%2Fupstream%2Fglib.git diff --git a/glib/goption.c b/glib/goption.c index 9d251ff..b9b23ba 100644 --- a/glib/goption.c +++ b/glib/goption.c @@ -120,7 +120,7 @@ * exit (1); * } * - * /* ... */ + * ... * * } * ]| @@ -161,18 +161,18 @@ * args = g_strdupv (argv); * #endif * - * /* ... setup context ... */ + * // set up context * * if (!g_option_context_parse_strv (context, &args, &error)) * { - * /* ... error ... */ + * // error happened * } * - * /* ... */ + * ... * * g_strfreev (args); * - * /* ... */ + * ... * } * ]| */ @@ -185,9 +185,7 @@ #include #if defined __OpenBSD__ -#include #include -#include #include #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) { @@ -1814,12 +1875,11 @@ platform_get_argv0 (void) * this function will produce help output to stdout and * call `exit (0)`. * - * Note that function depends on the - * current locale for + * Note that function depends on the [current locale][setlocale] for * 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 @@ -2038,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) { @@ -2059,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, @@ -2164,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