Allow optional summary and description texts before and after the option
[platform/upstream/glib.git] / glib / goption.c
1 /* goption.c - Option parser
2  *
3  *  Copyright (C) 1999, 2003 Red Hat Software
4  *  Copyright (C) 2004       Anders Carlsson <andersca@gnome.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include "goption.h"
25 #include "glib.h"
26 #include "glibintl.h"
27
28 #include "galias.h"
29
30 #include <string.h>
31 #include <stdlib.h>
32 #include <errno.h>
33
34 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
35
36 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE ||       \
37                        ((entry)->arg == G_OPTION_ARG_CALLBACK &&  \
38                         ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
39
40 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK &&  \
41                        (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
42
43 typedef struct 
44 {
45   GOptionArg arg_type;
46   gpointer arg_data;  
47   union 
48   {
49     gboolean bool;
50     gint integer;
51     gchar *str;
52     gchar **array;
53     gdouble dbl;
54     gint64 int64;
55   } prev;
56   union 
57   {
58     gchar *str;
59     struct 
60     {
61       gint len;
62       gchar **data;
63     } array;
64   } allocated;
65 } Change;
66
67 typedef struct
68 {
69   gchar **ptr;
70   gchar *value;
71 } PendingNull;
72
73 struct _GOptionContext
74 {
75   GList           *groups;
76
77   gchar           *parameter_string;
78   gchar           *summary;
79   gchar           *description;
80
81   GTranslateFunc   translate_func;
82   GDestroyNotify   translate_notify;
83   gpointer         translate_data;
84
85   guint            help_enabled   : 1;
86   guint            ignore_unknown : 1;
87   
88   GOptionGroup    *main_group;
89
90   /* We keep a list of change so we can revert them */
91   GList           *changes;
92   
93   /* We also keep track of all argv elements 
94    * that should be NULLed or modified.
95    */
96   GList           *pending_nulls;
97 };
98
99 struct _GOptionGroup
100 {
101   gchar           *name;
102   gchar           *description;
103   gchar           *help_description;
104
105   GDestroyNotify   destroy_notify;
106   gpointer         user_data;
107
108   GTranslateFunc   translate_func;
109   GDestroyNotify   translate_notify;
110   gpointer         translate_data;
111
112   GOptionEntry    *entries;
113   gint             n_entries;
114
115   GOptionParseFunc pre_parse_func;
116   GOptionParseFunc post_parse_func;
117   GOptionErrorFunc error_func;
118 };
119
120 static void free_changes_list (GOptionContext *context,
121                                gboolean        revert);
122 static void free_pending_nulls (GOptionContext *context,
123                                 gboolean        perform_nulls);
124
125 GQuark
126 g_option_error_quark (void)
127 {
128   return g_quark_from_static_string ("g-option-context-error-quark");
129 }
130
131 /**
132  * g_option_context_new:
133  * @parameter_string: a string which is displayed in
134  *    the first line of <option>--help</option> output, after the
135  *    usage summary 
136  *    <literal><replaceable>programname</replaceable> [OPTION...]</literal>
137  *
138  * Creates a new option context. 
139  *
140  * The @parameter_string can serve multiple purposes. It can be used
141  * to add descriptions for "rest" arguments, which are not parsed by
142  * the #GOptionContext, typically something like "FILES" or
143  * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
144  * collecting "rest" arguments, GLib handles this automatically by
145  * using the @arg_description of the corresponding #GOptionEntry in
146  * the usage summary.
147  *
148  * Another usage is to give a short summary of the program
149  * functionality, like " - frob the strings", which will be displayed
150  * in the same line as the usage. For a longer description of the
151  * program functionality that should be displayed as a paragraph
152  * below the usage line, use g_option_context_set_summary().
153  *
154  * Note that the @parameter_string is translated (see 
155  * g_option_context_set_translate_func()).
156  *
157  * Returns: a newly created #GOptionContext, which must be
158  *    freed with g_option_context_free() after use.
159  *
160  * Since: 2.6
161  */
162 GOptionContext *
163 g_option_context_new (const gchar *parameter_string)
164
165 {
166   GOptionContext *context;
167
168   context = g_new0 (GOptionContext, 1);
169
170   context->parameter_string = g_strdup (parameter_string);
171   context->help_enabled = TRUE;
172   context->ignore_unknown = FALSE;
173
174   return context;
175 }
176
177 /**
178  * g_option_context_free:
179  * @context: a #GOptionContext 
180  *
181  * Frees context and all the groups which have been 
182  * added to it.
183  *
184  * Since: 2.6
185  */
186 void g_option_context_free (GOptionContext *context) 
187 {
188   g_return_if_fail (context != NULL);
189
190   g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
191   g_list_free (context->groups);
192
193   if (context->main_group) 
194     g_option_group_free (context->main_group);
195
196   free_changes_list (context, FALSE);
197   free_pending_nulls (context, FALSE);
198   
199   g_free (context->parameter_string);
200   g_free (context->summary);
201   g_free (context->description);
202   
203   if (context->translate_notify)
204     (* context->translate_notify) (context->translate_data);
205
206   g_free (context);
207 }
208
209
210 /**
211  * g_option_context_set_help_enabled:
212  * @context: a #GOptionContext
213  * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
214  *
215  * Enables or disables automatic generation of <option>--help</option> 
216  * output. By default, g_option_context_parse() recognizes
217  * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
218  * and <option>--help-</option><replaceable>groupname</replaceable> and creates
219  * suitable output to stdout. 
220  *
221  * Since: 2.6
222  */
223 void g_option_context_set_help_enabled (GOptionContext *context,
224                                         gboolean        help_enabled)
225
226 {
227   g_return_if_fail (context != NULL);
228
229   context->help_enabled = help_enabled;
230 }
231
232 /**
233  * g_option_context_get_help_enabled:
234  * @context: a #GOptionContext
235  * 
236  * Returns whether automatic <option>--help</option> generation
237  * is turned on for @context. See g_option_context_set_help_enabled().
238  * 
239  * Returns: %TRUE if automatic help generation is turned on.
240  *
241  * Since: 2.6
242  */
243 gboolean 
244 g_option_context_get_help_enabled (GOptionContext *context) 
245 {
246   g_return_val_if_fail (context != NULL, FALSE);
247   
248   return context->help_enabled;
249 }
250
251 /**
252  * g_option_context_set_ignore_unknown_options:
253  * @context: a #GOptionContext
254  * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
255  *    an error when unknown options are met
256  * 
257  * Sets whether to ignore unknown options or not. If an argument is 
258  * ignored, it is left in the @argv array after parsing. By default, 
259  * g_option_context_parse() treats unknown options as error.
260  * 
261  * This setting does not affect non-option arguments (i.e. arguments 
262  * which don't start with a dash). But note that GOption cannot reliably
263  * determine whether a non-option belongs to a preceding unknown option.
264  *
265  * Since: 2.6
266  **/
267 void
268 g_option_context_set_ignore_unknown_options (GOptionContext *context,
269                                              gboolean        ignore_unknown)
270 {
271   g_return_if_fail (context != NULL);
272
273   context->ignore_unknown = ignore_unknown;
274 }
275
276 /**
277  * g_option_context_get_ignore_unknown_options:
278  * @context: a #GOptionContext
279  * 
280  * Returns whether unknown options are ignored or not. See
281  * g_option_context_set_ignore_unknown_options().
282  * 
283  * Returns: %TRUE if unknown options are ignored.
284  * 
285  * Since: 2.6
286  **/
287 gboolean
288 g_option_context_get_ignore_unknown_options (GOptionContext *context)
289 {
290   g_return_val_if_fail (context != NULL, FALSE);
291
292   return context->ignore_unknown;
293 }
294
295 /**
296  * g_option_context_add_group:
297  * @context: a #GOptionContext
298  * @group: the group to add
299  * 
300  * Adds a #GOptionGroup to the @context, so that parsing with @context
301  * will recognize the options in the group. Note that the group will
302  * be freed together with the context when g_option_context_free() is
303  * called, so you must not free the group yourself after adding it
304  * to a context.
305  *
306  * Since: 2.6
307  **/
308 void
309 g_option_context_add_group (GOptionContext *context,
310                             GOptionGroup   *group)
311 {
312   GList *list;
313
314   g_return_if_fail (context != NULL);
315   g_return_if_fail (group != NULL);
316   g_return_if_fail (group->name != NULL);
317   g_return_if_fail (group->description != NULL);
318   g_return_if_fail (group->help_description != NULL);
319
320   for (list = context->groups; list; list = list->next)
321     {
322       GOptionGroup *g = (GOptionGroup *)list->data;
323
324       if ((group->name == NULL && g->name == NULL) ||
325           (group->name && g->name && strcmp (group->name, g->name) == 0))
326         g_warning ("A group named \"%s\" is already part of this GOptionContext", 
327                    group->name);
328     }
329
330   context->groups = g_list_append (context->groups, group);
331 }
332
333 /**
334  * g_option_context_set_main_group:
335  * @context: a #GOptionContext
336  * @group: the group to set as main group
337  * 
338  * Sets a #GOptionGroup as main group of the @context. 
339  * This has the same effect as calling g_option_context_add_group(), 
340  * the only difference is that the options in the main group are 
341  * treated differently when generating <option>--help</option> output.
342  *
343  * Since: 2.6
344  **/
345 void
346 g_option_context_set_main_group (GOptionContext *context,
347                                  GOptionGroup   *group)
348 {
349   g_return_if_fail (context != NULL);
350   g_return_if_fail (group != NULL);
351
352   if (context->main_group)
353     {
354       g_warning ("This GOptionContext already has a main group");
355
356       return;
357     }
358   
359   context->main_group = group;
360 }
361
362 /**
363  * g_option_context_get_main_group:
364  * @context: a #GOptionContext
365  * 
366  * Returns a pointer to the main group of @context.
367  * 
368  * Return value: the main group of @context, or %NULL if @context doesn't
369  *  have a main group. Note that group belongs to @context and should
370  *  not be modified or freed.
371  *
372  * Since: 2.6
373  **/
374 GOptionGroup *
375 g_option_context_get_main_group (GOptionContext *context)
376 {
377   g_return_val_if_fail (context != NULL, NULL);
378
379   return context->main_group;
380 }
381
382 /**
383  * g_option_context_add_main_entries:
384  * @context: a #GOptionContext
385  * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
386  * @translation_domain: a translation domain to use for translating
387  *    the <option>--help</option> output for the options in @entries
388  *    with gettext(), or %NULL
389  * 
390  * A convenience function which creates a main group if it doesn't 
391  * exist, adds the @entries to it and sets the translation domain.
392  * 
393  * Since: 2.6
394  **/
395 void
396 g_option_context_add_main_entries (GOptionContext      *context,
397                                    const GOptionEntry  *entries,
398                                    const gchar         *translation_domain)
399 {
400   g_return_if_fail (entries != NULL);
401
402   if (!context->main_group)
403     context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
404   
405   g_option_group_add_entries (context->main_group, entries);
406   g_option_group_set_translation_domain (context->main_group, translation_domain);
407 }
408
409 static gint
410 calculate_max_length (GOptionGroup *group)
411 {
412   GOptionEntry *entry;
413   gint i, len, max_length;
414
415   max_length = 0;
416
417   for (i = 0; i < group->n_entries; i++)
418     {
419       entry = &group->entries[i];
420
421       if (entry->flags & G_OPTION_FLAG_HIDDEN)
422         continue;
423
424       len = g_utf8_strlen (entry->long_name, -1);
425       
426       if (entry->short_name)
427         len += 4;
428       
429       if (!NO_ARG (entry) && entry->arg_description)
430         len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
431       
432       max_length = MAX (max_length, len);
433     }
434
435   return max_length;
436 }
437
438 static void
439 print_entry (GOptionGroup       *group,
440              gint                max_length,
441              const GOptionEntry *entry)
442 {
443   GString *str;
444
445   if (entry->flags & G_OPTION_FLAG_HIDDEN)
446     return;
447
448   if (entry->long_name[0] == 0)
449     return;
450
451   str = g_string_new (NULL);
452   
453   if (entry->short_name)
454     g_string_append_printf (str, "  -%c, --%s", entry->short_name, entry->long_name);
455   else
456     g_string_append_printf (str, "  --%s", entry->long_name);
457   
458   if (entry->arg_description)
459     g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
460   
461   g_print ("%-*s %s\n", max_length + 4, str->str,
462            entry->description ? TRANSLATE (group, entry->description) : "");
463   g_string_free (str, TRUE);  
464 }
465
466 static void
467 print_help (GOptionContext *context,
468             gboolean        main_help,
469             GOptionGroup   *group) 
470 {
471   GList *list;
472   gint max_length, len;
473   gint i;
474   GOptionEntry *entry;
475   GHashTable *shadow_map;
476   gboolean seen[256];
477   const gchar *rest_description;
478   
479   rest_description = NULL;
480   if (context->main_group)
481     {
482
483       for (i = 0; i < context->main_group->n_entries; i++)
484         {
485           entry = &context->main_group->entries[i];
486           if (entry->long_name[0] == 0)
487             {
488               rest_description = TRANSLATE (context->main_group, entry->arg_description);
489               break;
490             }
491         }
492     }
493
494   g_print ("%s\n  %s %s%s%s%s%s\n\n", 
495            _("Usage:"), g_get_prgname(), _("[OPTION...]"),
496            rest_description ? " " : "",
497            rest_description ? rest_description : "",
498            context->parameter_string ? " " : "",
499            context->parameter_string ? TRANSLATE (context, context->parameter_string) : "");
500
501   if (context->summary)
502     g_print ("%s\n\n", TRANSLATE (context, context->summary));
503
504   memset (seen, 0, sizeof (gboolean) * 256);
505   shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
506
507   if (context->main_group)
508     {
509       for (i = 0; i < context->main_group->n_entries; i++)
510         {
511           entry = &context->main_group->entries[i];
512           g_hash_table_insert (shadow_map, 
513                                (gpointer)entry->long_name, 
514                                entry);
515           
516           if (seen[(guchar)entry->short_name])
517             entry->short_name = 0;
518           else
519             seen[(guchar)entry->short_name] = TRUE;
520         }
521     }
522
523   list = context->groups;
524   while (list != NULL)
525     {
526       GOptionGroup *group = list->data;
527       for (i = 0; i < group->n_entries; i++)
528         {
529           entry = &group->entries[i];
530           if (g_hash_table_lookup (shadow_map, entry->long_name) && 
531               !(entry->flags && G_OPTION_FLAG_NOALIAS))
532             entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
533           else  
534             g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
535
536           if (seen[(guchar)entry->short_name] && 
537               !(entry->flags && G_OPTION_FLAG_NOALIAS))
538             entry->short_name = 0;
539           else
540             seen[(guchar)entry->short_name] = TRUE;
541         }
542       list = list->next;
543     }
544
545   g_hash_table_destroy (shadow_map);
546
547   list = context->groups;
548
549   max_length = g_utf8_strlen ("-?, --help", -1);
550
551   if (list)
552     {
553       len = g_utf8_strlen ("--help-all", -1);
554       max_length = MAX (max_length, len);
555     }
556
557   if (context->main_group)
558     {
559       len = calculate_max_length (context->main_group);
560       max_length = MAX (max_length, len);
561     }
562
563   while (list != NULL)
564     {
565       GOptionGroup *group = list->data;
566       
567       /* First, we check the --help-<groupname> options */
568       len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
569       max_length = MAX (max_length, len);
570
571       /* Then we go through the entries */
572       len = calculate_max_length (group);
573       max_length = MAX (max_length, len);
574       
575       list = list->next;
576     }
577
578   /* Add a bit of padding */
579   max_length += 4;
580
581   if (!group)
582     {
583       list = context->groups;
584       
585       g_print ("%s\n  -%c, --%-*s %s\n", 
586                _("Help Options:"), '?', max_length - 4, "help", 
587                _("Show help options"));
588       
589       /* We only want --help-all when there are groups */
590       if (list)
591         g_print ("  --%-*s %s\n", max_length, "help-all", 
592                  _("Show all help options"));
593       
594       while (list)
595         {
596           GOptionGroup *group = list->data;
597           
598           g_print ("  --help-%-*s %s\n", max_length - 5, group->name, 
599                    TRANSLATE (group, group->help_description));
600           
601           list = list->next;
602         }
603
604       g_print ("\n");
605     }
606
607   if (group)
608     {
609       /* Print a certain group */
610       
611       g_print ("%s\n", TRANSLATE (group, group->description));
612       for (i = 0; i < group->n_entries; i++)
613         print_entry (group, max_length, &group->entries[i]);
614       g_print ("\n");
615     }
616   else if (!main_help)
617     {
618       /* Print all groups */
619
620       list = context->groups;
621
622       while (list)
623         {
624           GOptionGroup *group = list->data;
625
626           g_print ("%s\n", group->description);
627
628           for (i = 0; i < group->n_entries; i++)
629             if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
630               print_entry (group, max_length, &group->entries[i]);
631           
632           g_print ("\n");
633           list = list->next;
634         }
635     }
636   
637   /* Print application options if --help or --help-all has been specified */
638   if (main_help || !group)
639     {
640       list = context->groups;
641
642       g_print ("%s\n", _("Application Options:"));
643
644       if (context->main_group)
645         for (i = 0; i < context->main_group->n_entries; i++) 
646           print_entry (context->main_group, max_length, 
647                        &context->main_group->entries[i]);
648
649       while (list != NULL)
650         {
651           GOptionGroup *group = list->data;
652
653           /* Print main entries from other groups */
654           for (i = 0; i < group->n_entries; i++)
655             if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
656               print_entry (group, max_length, &group->entries[i]);
657           
658           list = list->next;
659         }
660
661       g_print ("\n");
662     }
663  
664   if (context->description)
665     g_print ("%s\n", TRANSLATE (context, context->description));
666  
667   exit (0);
668 }
669
670 static gboolean
671 parse_int (const gchar *arg_name,
672            const gchar *arg,
673            gint        *result,
674            GError     **error)
675 {
676   gchar *end;
677   glong tmp;
678
679   errno = 0;
680   tmp = strtol (arg, &end, 0);
681   
682   if (*arg == '\0' || *end != '\0')
683     {
684       g_set_error (error,
685                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
686                    _("Cannot parse integer value '%s' for %s"),
687                    arg, arg_name);
688       return FALSE;
689     }
690
691   *result = tmp;
692   if (*result != tmp || errno == ERANGE)
693     {
694       g_set_error (error,
695                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
696                    _("Integer value '%s' for %s out of range"),
697                    arg, arg_name);
698       return FALSE;
699     }
700
701   return TRUE;
702 }
703
704
705 static gboolean
706 parse_double (const gchar *arg_name,
707            const gchar *arg,
708            gdouble        *result,
709            GError     **error)
710 {
711   gchar *end;
712   gdouble tmp;
713
714   errno = 0;
715   tmp = g_strtod (arg, &end);
716   
717   if (*arg == '\0' || *end != '\0')
718     {
719       g_set_error (error,
720                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
721                    _("Cannot parse double value '%s' for %s"),
722                    arg, arg_name);
723       return FALSE;
724     }
725   if (errno == ERANGE)
726     {
727       g_set_error (error,
728                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
729                    _("Double value '%s' for %s out of range"),
730                    arg, arg_name);
731       return FALSE;
732     }
733
734   *result = tmp;
735   
736   return TRUE;
737 }
738
739
740 static gboolean
741 parse_int64 (const gchar *arg_name,
742              const gchar *arg,
743              gint64      *result,
744              GError     **error)
745 {
746   gchar *end;
747   gint64 tmp;
748
749   errno = 0;
750   tmp = strtoll (arg, &end, 0);
751
752   if (*arg == '\0' || *end != '\0')
753     {
754       g_set_error (error,
755                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
756                    _("Cannot parse integer value '%s' for %s"),
757                    arg, arg_name);
758       return FALSE;
759     }
760   if (errno == ERANGE)
761     {
762       g_set_error (error,
763                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
764                    _("Integer value '%s' for %s out of range"),
765                    arg, arg_name);
766       return FALSE;
767     }
768
769   *result = tmp;
770   
771   return TRUE;
772 }
773
774
775 static Change *
776 get_change (GOptionContext *context,
777             GOptionArg      arg_type,
778             gpointer        arg_data)
779 {
780   GList *list;
781   Change *change = NULL;
782   
783   for (list = context->changes; list != NULL; list = list->next)
784     {
785       change = list->data;
786
787       if (change->arg_data == arg_data)
788         goto found;
789     }
790
791   change = g_new0 (Change, 1);
792   change->arg_type = arg_type;
793   change->arg_data = arg_data;
794   
795   context->changes = g_list_prepend (context->changes, change);
796   
797  found:
798
799   return change;
800 }
801
802 static void
803 add_pending_null (GOptionContext *context,
804                   gchar         **ptr,
805                   gchar          *value)
806 {
807   PendingNull *n;
808
809   n = g_new0 (PendingNull, 1);
810   n->ptr = ptr;
811   n->value = value;
812
813   context->pending_nulls = g_list_prepend (context->pending_nulls, n);
814 }
815                   
816 static gboolean
817 parse_arg (GOptionContext *context,
818            GOptionGroup   *group,
819            GOptionEntry   *entry,
820            const gchar    *value,
821            const gchar    *option_name,
822            GError        **error)
823      
824 {
825   Change *change;
826
827   g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
828
829   switch (entry->arg)
830     {
831     case G_OPTION_ARG_NONE:
832       {
833         change = get_change (context, G_OPTION_ARG_NONE,
834                              entry->arg_data);
835
836         *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
837         break;
838       }      
839     case G_OPTION_ARG_STRING:
840       {
841         gchar *data;
842         
843         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
844
845         if (!data)
846           return FALSE;
847
848         change = get_change (context, G_OPTION_ARG_STRING,
849                              entry->arg_data);
850         g_free (change->allocated.str);
851         
852         change->prev.str = *(gchar **)entry->arg_data;
853         change->allocated.str = data;
854         
855         *(gchar **)entry->arg_data = data;
856         break;
857       }
858     case G_OPTION_ARG_STRING_ARRAY:
859       {
860         gchar *data;
861
862         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
863
864         if (!data)
865           return FALSE;
866
867         change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
868                              entry->arg_data);
869
870         if (change->allocated.array.len == 0)
871           {
872             change->prev.array = *(gchar ***)entry->arg_data;
873             change->allocated.array.data = g_new (gchar *, 2);
874           }
875         else
876           change->allocated.array.data =
877             g_renew (gchar *, change->allocated.array.data,
878                      change->allocated.array.len + 2);
879
880         change->allocated.array.data[change->allocated.array.len] = data;
881         change->allocated.array.data[change->allocated.array.len + 1] = NULL;
882
883         change->allocated.array.len ++;
884
885         *(gchar ***)entry->arg_data = change->allocated.array.data;
886
887         break;
888       }
889       
890     case G_OPTION_ARG_FILENAME:
891       {
892         gchar *data;
893
894 #ifdef G_OS_WIN32
895         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
896         
897         if (!data)
898           return FALSE;
899 #else
900         data = g_strdup (value);
901 #endif
902         change = get_change (context, G_OPTION_ARG_FILENAME,
903                              entry->arg_data);
904         g_free (change->allocated.str);
905         
906         change->prev.str = *(gchar **)entry->arg_data;
907         change->allocated.str = data;
908
909         *(gchar **)entry->arg_data = data;
910         break;
911       }
912
913     case G_OPTION_ARG_FILENAME_ARRAY:
914       {
915         gchar *data;
916         
917 #ifdef G_OS_WIN32
918         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
919         
920         if (!data)
921           return FALSE;
922 #else
923         data = g_strdup (value);
924 #endif
925         change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
926                              entry->arg_data);
927
928         if (change->allocated.array.len == 0)
929           {
930             change->prev.array = *(gchar ***)entry->arg_data;
931             change->allocated.array.data = g_new (gchar *, 2);
932           }
933         else
934           change->allocated.array.data =
935             g_renew (gchar *, change->allocated.array.data,
936                      change->allocated.array.len + 2);
937
938         change->allocated.array.data[change->allocated.array.len] = data;
939         change->allocated.array.data[change->allocated.array.len + 1] = NULL;
940
941         change->allocated.array.len ++;
942
943         *(gchar ***)entry->arg_data = change->allocated.array.data;
944
945         break;
946       }
947       
948     case G_OPTION_ARG_INT:
949       {
950         gint data;
951
952         if (!parse_int (option_name, value,
953                         &data,
954                         error))
955           return FALSE;
956
957         change = get_change (context, G_OPTION_ARG_INT,
958                              entry->arg_data);
959         change->prev.integer = *(gint *)entry->arg_data;
960         *(gint *)entry->arg_data = data;
961         break;
962       }
963     case G_OPTION_ARG_CALLBACK:
964       {
965         gchar *data;
966         gboolean retval;
967
968         if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
969           data = NULL;
970         else if (entry->flags & G_OPTION_FLAG_NO_ARG)
971           data = NULL;
972         else if (entry->flags & G_OPTION_FLAG_FILENAME)
973           {
974 #ifdef G_OS_WIN32
975             data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
976 #else
977             data = g_strdup (value);
978 #endif
979           }
980         else
981           data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
982
983         if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) && 
984             !data)
985           return FALSE;
986
987         retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
988         
989         g_free (data);
990         
991         return retval;
992         
993         break;
994       }
995     case G_OPTION_ARG_DOUBLE:
996       {
997         gdouble data;
998
999         if (!parse_double (option_name, value,
1000                         &data,
1001                         error))
1002           {
1003             return FALSE;
1004           }
1005
1006         change = get_change (context, G_OPTION_ARG_DOUBLE,
1007                              entry->arg_data);
1008         change->prev.dbl = *(gdouble *)entry->arg_data;
1009         *(gdouble *)entry->arg_data = data;
1010         break;
1011       }
1012     case G_OPTION_ARG_INT64:
1013       {
1014         gint64 data;
1015
1016         if (!parse_int64 (option_name, value,
1017                          &data,
1018                          error))
1019           {
1020             return FALSE;
1021           }
1022
1023         change = get_change (context, G_OPTION_ARG_INT64,
1024                              entry->arg_data);
1025         change->prev.int64 = *(gint64 *)entry->arg_data;
1026         *(gint64 *)entry->arg_data = data;
1027         break;
1028       }
1029     default:
1030       g_assert_not_reached ();
1031     }
1032
1033   return TRUE;
1034 }
1035
1036 static gboolean
1037 parse_short_option (GOptionContext *context,
1038                     GOptionGroup   *group,
1039                     gint            index,
1040                     gint           *new_index,
1041                     gchar           arg,
1042                     gint           *argc,
1043                     gchar        ***argv,
1044                     GError        **error,
1045                     gboolean       *parsed)
1046 {
1047   gint j;
1048     
1049   for (j = 0; j < group->n_entries; j++)
1050     {
1051       if (arg == group->entries[j].short_name)
1052         {
1053           gchar *option_name;
1054           gchar *value = NULL;
1055           
1056           option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1057
1058           if (NO_ARG (&group->entries[j]))
1059             value = NULL;
1060           else
1061             {
1062               if (*new_index > index)
1063                 {
1064                   g_set_error (error, 
1065                                G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1066                                _("Error parsing option %s"), option_name);
1067                   g_free (option_name);
1068                   return FALSE;
1069                 }
1070
1071               if (index < *argc - 1)
1072                 {
1073                   if (!OPTIONAL_ARG (&group->entries[j]))       
1074                     {    
1075                       value = (*argv)[index + 1];
1076                       add_pending_null (context, &((*argv)[index + 1]), NULL);
1077                       *new_index = index+1;
1078                     }
1079                   else
1080                     {
1081                       if ((*argv)[index + 1][0] == '-') 
1082                         value = NULL;
1083                       else
1084                         {
1085                           value = (*argv)[index + 1];
1086                           add_pending_null (context, &((*argv)[index + 1]), NULL);
1087                           *new_index = index + 1;
1088                         }
1089                     }
1090                 }
1091               else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1092                 value = NULL;
1093               else
1094                 {
1095                   g_set_error (error, 
1096                                G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1097                                _("Missing argument for %s"), option_name);
1098                   g_free (option_name);
1099                   return FALSE;
1100                 }
1101             }
1102
1103           if (!parse_arg (context, group, &group->entries[j], 
1104                           value, option_name, error))
1105             {
1106               g_free (option_name);
1107               return FALSE;
1108             }
1109           
1110           g_free (option_name);
1111           *parsed = TRUE;
1112         }
1113     }
1114
1115   return TRUE;
1116 }
1117
1118 static gboolean
1119 parse_long_option (GOptionContext *context,
1120                    GOptionGroup   *group,
1121                    gint           *index,
1122                    gchar          *arg,
1123                    gboolean        aliased,
1124                    gint           *argc,
1125                    gchar        ***argv,
1126                    GError        **error,
1127                    gboolean       *parsed)
1128 {
1129   gint j;
1130
1131   for (j = 0; j < group->n_entries; j++)
1132     {
1133       if (*index >= *argc)
1134         return TRUE;
1135
1136       if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1137         continue;
1138
1139       if (NO_ARG (&group->entries[j]) &&
1140           strcmp (arg, group->entries[j].long_name) == 0)
1141         {
1142           gchar *option_name;
1143
1144           option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1145           parse_arg (context, group, &group->entries[j],
1146                      NULL, option_name, error);
1147           g_free(option_name);
1148           
1149           add_pending_null (context, &((*argv)[*index]), NULL);
1150           *parsed = TRUE;
1151         }
1152       else
1153         {
1154           gint len = strlen (group->entries[j].long_name);
1155           
1156           if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1157               (arg[len] == '=' || arg[len] == 0))
1158             {
1159               gchar *value = NULL;
1160               gchar *option_name;
1161
1162               add_pending_null (context, &((*argv)[*index]), NULL);
1163               option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1164
1165               if (arg[len] == '=')
1166                 value = arg + len + 1;
1167               else if (*index < *argc - 1) 
1168                 {
1169                   if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))  
1170                     {    
1171                       value = (*argv)[*index + 1];
1172                       add_pending_null (context, &((*argv)[*index + 1]), NULL);
1173                       (*index)++;
1174                     }
1175                   else
1176                     {
1177                       if ((*argv)[*index + 1][0] == '-') 
1178                         {
1179                           gboolean retval;
1180                           retval = parse_arg (context, group, &group->entries[j],
1181                                               NULL, option_name, error);
1182                           *parsed = TRUE;
1183                           g_free (option_name);
1184                           return retval;
1185                         }
1186                       else
1187                         {
1188                           value = (*argv)[*index + 1];
1189                           add_pending_null (context, &((*argv)[*index + 1]), NULL);
1190                           (*index)++;
1191                         }
1192                     }
1193                 }
1194               else if (*index >= *argc - 1 &&
1195                        group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1196                 {
1197                     gboolean retval;
1198                     retval = parse_arg (context, group, &group->entries[j],
1199                                         NULL, option_name, error);
1200                     *parsed = TRUE;
1201                     g_free (option_name);
1202                     return retval;
1203                 }
1204               else
1205                 {
1206                   g_set_error (error, 
1207                                G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1208                                _("Missing argument for %s"), option_name);
1209                   g_free (option_name);
1210                   return FALSE;
1211                 }
1212
1213               if (!parse_arg (context, group, &group->entries[j], 
1214                               value, option_name, error))
1215                 {
1216                   g_free (option_name);
1217                   return FALSE;
1218                 }
1219
1220               g_free (option_name);
1221               *parsed = TRUE;
1222             } 
1223         }
1224     }
1225   
1226   return TRUE;
1227 }
1228
1229 static gboolean
1230 parse_remaining_arg (GOptionContext *context,
1231                      GOptionGroup   *group,
1232                      gint           *index,
1233                      gint           *argc,
1234                      gchar        ***argv,
1235                      GError        **error,
1236                      gboolean       *parsed)
1237 {
1238   gint j;
1239
1240   for (j = 0; j < group->n_entries; j++)
1241     {
1242       if (*index >= *argc)
1243         return TRUE;
1244
1245       if (group->entries[j].long_name[0])
1246         continue;
1247
1248       g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1249                             group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1250       
1251       add_pending_null (context, &((*argv)[*index]), NULL);
1252       
1253       if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1254         return FALSE;
1255       
1256       *parsed = TRUE;
1257       return TRUE;
1258     }
1259
1260   return TRUE;
1261 }
1262
1263 static void
1264 free_changes_list (GOptionContext *context,
1265                    gboolean        revert)
1266 {
1267   GList *list;
1268
1269   for (list = context->changes; list != NULL; list = list->next)
1270     {
1271       Change *change = list->data;
1272
1273       if (revert)
1274         {
1275           switch (change->arg_type)
1276             {
1277             case G_OPTION_ARG_NONE:
1278               *(gboolean *)change->arg_data = change->prev.bool;
1279               break;
1280             case G_OPTION_ARG_INT:
1281               *(gint *)change->arg_data = change->prev.integer;
1282               break;
1283             case G_OPTION_ARG_STRING:
1284             case G_OPTION_ARG_FILENAME:
1285               g_free (change->allocated.str);
1286               *(gchar **)change->arg_data = change->prev.str;
1287               break;
1288             case G_OPTION_ARG_STRING_ARRAY:
1289             case G_OPTION_ARG_FILENAME_ARRAY:
1290               g_strfreev (change->allocated.array.data);
1291               *(gchar ***)change->arg_data = change->prev.array;
1292               break;
1293             case G_OPTION_ARG_DOUBLE:
1294               *(gdouble *)change->arg_data = change->prev.dbl;
1295               break;
1296             case G_OPTION_ARG_INT64:
1297               *(gint64 *)change->arg_data = change->prev.int64;
1298               break;
1299             default:
1300               g_assert_not_reached ();
1301             }
1302         }
1303       
1304       g_free (change);
1305     }
1306
1307   g_list_free (context->changes);
1308   context->changes = NULL;
1309 }
1310
1311 static void
1312 free_pending_nulls (GOptionContext *context,
1313                     gboolean        perform_nulls)
1314 {
1315   GList *list;
1316
1317   for (list = context->pending_nulls; list != NULL; list = list->next)
1318     {
1319       PendingNull *n = list->data;
1320
1321       if (perform_nulls)
1322         {
1323           if (n->value)
1324             {
1325               /* Copy back the short options */
1326               *(n->ptr)[0] = '-';             
1327               strcpy (*n->ptr + 1, n->value);
1328             }
1329           else
1330             *n->ptr = NULL;
1331         }
1332       
1333       g_free (n->value);
1334       g_free (n);
1335     }
1336
1337   g_list_free (context->pending_nulls);
1338   context->pending_nulls = NULL;
1339 }
1340
1341 /**
1342  * g_option_context_parse:
1343  * @context: a #GOptionContext
1344  * @argc: a pointer to the number of command line arguments.
1345  * @argv: a pointer to the array of command line arguments.
1346  * @error: a return location for errors 
1347  * 
1348  * Parses the command line arguments, recognizing options
1349  * which have been added to @context. A side-effect of 
1350  * calling this function is that g_set_prgname() will be
1351  * called.
1352  *
1353  * If the parsing is successful, any parsed arguments are
1354  * removed from the array and @argc and @argv are updated 
1355  * accordingly. A '--' option is stripped from @argv
1356  * unless there are unparsed options before and after it, 
1357  * or some of the options after it start with '-'. In case 
1358  * of an error, @argc and @argv are left unmodified. 
1359  *
1360  * If automatic <option>--help</option> support is enabled
1361  * (see g_option_context_set_help_enabled()), and the 
1362  * @argv array contains one of the recognized help options,
1363  * this function will produce help output to stdout and
1364  * call <literal>exit (0)</literal>.
1365  * 
1366  * Return value: %TRUE if the parsing was successful, 
1367  *               %FALSE if an error occurred
1368  *
1369  * Since: 2.6
1370  **/
1371 gboolean
1372 g_option_context_parse (GOptionContext   *context,
1373                         gint             *argc,
1374                         gchar          ***argv,
1375                         GError          **error)
1376 {
1377   gint i, j, k;
1378   GList *list;
1379
1380   /* Set program name */
1381   if (!g_get_prgname())
1382     {
1383       if (argc && argv && *argc)
1384         {
1385           gchar *prgname;
1386           
1387           prgname = g_path_get_basename ((*argv)[0]);
1388           g_set_prgname (prgname);
1389           g_free (prgname);
1390         }
1391       else
1392         g_set_prgname ("<unknown>");
1393     }
1394
1395   /* Call pre-parse hooks */
1396   list = context->groups;
1397   while (list)
1398     {
1399       GOptionGroup *group = list->data;
1400       
1401       if (group->pre_parse_func)
1402         {
1403           if (!(* group->pre_parse_func) (context, group,
1404                                           group->user_data, error))
1405             goto fail;
1406         }
1407       
1408       list = list->next;
1409     }
1410
1411   if (context->main_group && context->main_group->pre_parse_func)
1412     {
1413       if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1414                                                     context->main_group->user_data, error))
1415         goto fail;
1416     }
1417
1418   if (argc && argv)
1419     {
1420       gboolean stop_parsing = FALSE;
1421       gboolean has_unknown = FALSE;
1422       gint separator_pos = 0;
1423
1424       for (i = 1; i < *argc; i++)
1425         {
1426           gchar *arg, *dash;
1427           gboolean parsed = FALSE;
1428
1429           if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1430             {
1431               if ((*argv)[i][1] == '-')
1432                 {
1433                   /* -- option */
1434
1435                   arg = (*argv)[i] + 2;
1436
1437                   /* '--' terminates list of arguments */
1438                   if (*arg == 0)
1439                     {
1440                       separator_pos = i;
1441                       stop_parsing = TRUE;
1442                       continue;
1443                     }
1444
1445                   /* Handle help options */
1446                   if (context->help_enabled)
1447                     {
1448                       if (strcmp (arg, "help") == 0)
1449                         print_help (context, TRUE, NULL);
1450                       else if (strcmp (arg, "help-all") == 0)
1451                         print_help (context, FALSE, NULL);                    
1452                       else if (strncmp (arg, "help-", 5) == 0)
1453                         {
1454                           GList *list;
1455                           
1456                           list = context->groups;
1457                           
1458                           while (list)
1459                             {
1460                               GOptionGroup *group = list->data;
1461                               
1462                               if (strcmp (arg + 5, group->name) == 0)
1463                                 print_help (context, FALSE, group);                                           
1464                               
1465                               list = list->next;
1466                             }
1467                         }
1468                     }
1469
1470                   if (context->main_group &&
1471                       !parse_long_option (context, context->main_group, &i, arg,
1472                                           FALSE, argc, argv, error, &parsed))
1473                     goto fail;
1474
1475                   if (parsed)
1476                     continue;
1477                   
1478                   /* Try the groups */
1479                   list = context->groups;
1480                   while (list)
1481                     {
1482                       GOptionGroup *group = list->data;
1483                       
1484                       if (!parse_long_option (context, group, &i, arg, 
1485                                               FALSE, argc, argv, error, &parsed))
1486                         goto fail;
1487                       
1488                       if (parsed)
1489                         break;
1490                       
1491                       list = list->next;
1492                     }
1493                   
1494                   if (parsed)
1495                     continue;
1496
1497                   /* Now look for --<group>-<option> */
1498                   dash = strchr (arg, '-');
1499                   if (dash)
1500                     {
1501                       /* Try the groups */
1502                       list = context->groups;
1503                       while (list)
1504                         {
1505                           GOptionGroup *group = list->data;
1506                           
1507                           if (strncmp (group->name, arg, dash - arg) == 0)
1508                             {
1509                               if (!parse_long_option (context, group, &i, dash + 1,
1510                                                       TRUE, argc, argv, error, &parsed))
1511                                 goto fail;
1512                               
1513                               if (parsed)
1514                                 break;
1515                             }
1516                           
1517                           list = list->next;
1518                         }
1519                     }
1520                   
1521                   if (context->ignore_unknown)
1522                     continue;
1523                 }
1524               else
1525                 { /* short option */
1526                   gint j, new_i = i, arg_length;
1527                   gboolean *nulled_out = NULL;
1528                   arg = (*argv)[i] + 1;
1529                   arg_length = strlen (arg);
1530                   nulled_out = g_newa (gboolean, arg_length);
1531                   memset (nulled_out, 0, arg_length * sizeof (gboolean));
1532                   for (j = 0; j < arg_length; j++)
1533                     {
1534                       if (context->help_enabled && arg[j] == '?')
1535                         print_help (context, TRUE, NULL);
1536                       parsed = FALSE;
1537                       if (context->main_group &&
1538                           !parse_short_option (context, context->main_group,
1539                                                i, &new_i, arg[j],
1540                                                argc, argv, error, &parsed))
1541                         goto fail;
1542                       if (!parsed)
1543                         {
1544                           /* Try the groups */
1545                           list = context->groups;
1546                           while (list)
1547                             {
1548                               GOptionGroup *group = list->data;
1549                               if (!parse_short_option (context, group, i, &new_i, arg[j],
1550                                                        argc, argv, error, &parsed))
1551                                 goto fail;
1552                               if (parsed)
1553                                 break;
1554                               list = list->next;
1555                             }
1556                         }
1557                       
1558                       if (context->ignore_unknown && parsed)
1559                         nulled_out[j] = TRUE;
1560                       else if (context->ignore_unknown)
1561                         continue;
1562                       else if (!parsed)
1563                         break;
1564                       /* !context->ignore_unknown && parsed */
1565                     }
1566                   if (context->ignore_unknown)
1567                     {
1568                       gchar *new_arg = NULL; 
1569                       gint arg_index = 0;
1570                       for (j = 0; j < arg_length; j++)
1571                         {
1572                           if (!nulled_out[j])
1573                             {
1574                               if (!new_arg)
1575                                 new_arg = g_malloc (arg_length + 1);
1576                               new_arg[arg_index++] = arg[j];
1577                             }
1578                         }
1579                       if (new_arg)
1580                         new_arg[arg_index] = '\0';
1581                       add_pending_null (context, &((*argv)[i]), new_arg);
1582                     }
1583                   else if (parsed)
1584                     {
1585                       add_pending_null (context, &((*argv)[i]), NULL);
1586                       i = new_i;
1587                     }
1588                 }
1589               
1590               if (!parsed)
1591                 has_unknown = TRUE;
1592
1593               if (!parsed && !context->ignore_unknown)
1594                 {
1595                   g_set_error (error,
1596                                G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1597                                    _("Unknown option %s"), (*argv)[i]);
1598                   goto fail;
1599                 }
1600             }
1601           else
1602             {
1603               /* Collect remaining args */
1604               if (context->main_group &&
1605                   !parse_remaining_arg (context, context->main_group, &i,
1606                                         argc, argv, error, &parsed))
1607                 goto fail;
1608               
1609               if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1610                 separator_pos = 0;
1611             }
1612         }
1613
1614       if (separator_pos > 0)
1615         add_pending_null (context, &((*argv)[separator_pos]), NULL);
1616         
1617     }
1618
1619   /* Call post-parse hooks */
1620   list = context->groups;
1621   while (list)
1622     {
1623       GOptionGroup *group = list->data;
1624       
1625       if (group->post_parse_func)
1626         {
1627           if (!(* group->post_parse_func) (context, group,
1628                                            group->user_data, error))
1629             goto fail;
1630         }
1631       
1632       list = list->next;
1633     }
1634   
1635   if (context->main_group && context->main_group->post_parse_func)
1636     {
1637       if (!(* context->main_group->post_parse_func) (context, context->main_group,
1638                                                      context->main_group->user_data, error))
1639         goto fail;
1640     }
1641   
1642   if (argc && argv)
1643     {
1644       free_pending_nulls (context, TRUE);
1645       
1646       for (i = 1; i < *argc; i++)
1647         {
1648           for (k = i; k < *argc; k++)
1649             if ((*argv)[k] != NULL)
1650               break;
1651           
1652           if (k > i)
1653             {
1654               k -= i;
1655               for (j = i + k; j < *argc; j++)
1656                 {
1657                   (*argv)[j-k] = (*argv)[j];
1658                   (*argv)[j] = NULL;
1659                 }
1660               *argc -= k;
1661             }
1662         }      
1663     }
1664
1665   return TRUE;
1666
1667  fail:
1668   
1669   /* Call error hooks */
1670   list = context->groups;
1671   while (list)
1672     {
1673       GOptionGroup *group = list->data;
1674       
1675       if (group->error_func)
1676         (* group->error_func) (context, group,
1677                                group->user_data, error);
1678       
1679       list = list->next;
1680     }
1681
1682   if (context->main_group && context->main_group->error_func)
1683     (* context->main_group->error_func) (context, context->main_group,
1684                                          context->main_group->user_data, error);
1685   
1686   free_changes_list (context, TRUE);
1687   free_pending_nulls (context, FALSE);
1688
1689   return FALSE;
1690 }
1691                                    
1692 /**
1693  * g_option_group_new:
1694  * @name: the name for the option group, this is used to provide
1695  *   help for the options in this group with <option>--help-</option>@name
1696  * @description: a description for this group to be shown in 
1697  *   <option>--help</option>. This string is translated using the translation
1698  *   domain or translation function of the group
1699  * @help_description: a description for the <option>--help-</option>@name option.
1700  *   This string is translated using the translation domain or translation function
1701  *   of the group
1702  * @user_data: user data that will be passed to the pre- and post-parse hooks,
1703  *   the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1704  * @destroy: a function that will be called to free @user_data, or %NULL
1705  * 
1706  * Creates a new #GOptionGroup.
1707  * 
1708  * Return value: a newly created option group. It should be added 
1709  *   to a #GOptionContext or freed with g_option_group_free().
1710  *
1711  * Since: 2.6
1712  **/
1713 GOptionGroup *
1714 g_option_group_new (const gchar    *name,
1715                     const gchar    *description,
1716                     const gchar    *help_description,
1717                     gpointer        user_data,
1718                     GDestroyNotify  destroy)
1719
1720 {
1721   GOptionGroup *group;
1722
1723   group = g_new0 (GOptionGroup, 1);
1724   group->name = g_strdup (name);
1725   group->description = g_strdup (description);
1726   group->help_description = g_strdup (help_description);
1727   group->user_data = user_data;
1728   group->destroy_notify = destroy;
1729   
1730   return group;
1731 }
1732
1733
1734 /**
1735  * g_option_group_free:
1736  * @group: a #GOptionGroup
1737  * 
1738  * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1739  * free groups which have been added to a #GOptionContext.
1740  *
1741  * Since: 2.6
1742  **/
1743 void
1744 g_option_group_free (GOptionGroup *group)
1745 {
1746   g_return_if_fail (group != NULL);
1747
1748   g_free (group->name);
1749   g_free (group->description);
1750   g_free (group->help_description);
1751
1752   g_free (group->entries);
1753   
1754   if (group->destroy_notify)
1755     (* group->destroy_notify) (group->user_data);
1756
1757   if (group->translate_notify)
1758     (* group->translate_notify) (group->translate_data);
1759   
1760   g_free (group);
1761 }
1762
1763
1764 /**
1765  * g_option_group_add_entries:
1766  * @group: a #GOptionGroup
1767  * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1768  * 
1769  * Adds the options specified in @entries to @group.
1770  *
1771  * Since: 2.6
1772  **/
1773 void
1774 g_option_group_add_entries (GOptionGroup       *group,
1775                             const GOptionEntry *entries)
1776 {
1777   gint i, n_entries;
1778   
1779   g_return_if_fail (entries != NULL);
1780
1781   for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1782
1783   group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1784
1785   memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1786
1787   for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1788     {
1789       gchar c = group->entries[i].short_name;
1790
1791       if (c)
1792         {
1793           if (c == '-' || !g_ascii_isprint (c))
1794             {
1795               g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1796               group->entries[i].short_name = 0;
1797             }
1798         }
1799     }
1800
1801   group->n_entries += n_entries;
1802 }
1803
1804 /**
1805  * g_option_group_set_parse_hooks:
1806  * @group: a #GOptionGroup
1807  * @pre_parse_func: a function to call before parsing, or %NULL
1808  * @post_parse_func: a function to call after parsing, or %NULL
1809  * 
1810  * Associates two functions with @group which will be called 
1811  * from g_option_context_parse() before the first option is parsed
1812  * and after the last option has been parsed, respectively.
1813  *
1814  * Note that the user data to be passed to @pre_parse_func and
1815  * @post_parse_func can be specified when constructing the group
1816  * with g_option_group_new().
1817  *
1818  * Since: 2.6
1819  **/
1820 void
1821 g_option_group_set_parse_hooks (GOptionGroup     *group,
1822                                 GOptionParseFunc  pre_parse_func,
1823                                 GOptionParseFunc  post_parse_func)
1824 {
1825   g_return_if_fail (group != NULL);
1826
1827   group->pre_parse_func = pre_parse_func;
1828   group->post_parse_func = post_parse_func;  
1829 }
1830
1831 /**
1832  * g_option_group_set_error_hook:
1833  * @group: a #GOptionGroup
1834  * @error_func: a function to call when an error occurs
1835  * 
1836  * Associates a function with @group which will be called 
1837  * from g_option_context_parse() when an error occurs.
1838  *
1839  * Note that the user data to be passed to @pre_parse_func and
1840  * @post_parse_func can be specified when constructing the group
1841  * with g_option_group_new().
1842  *
1843  * Since: 2.6
1844  **/
1845 void
1846 g_option_group_set_error_hook (GOptionGroup     *group,
1847                                GOptionErrorFunc  error_func)
1848 {
1849   g_return_if_fail (group != NULL);
1850
1851   group->error_func = error_func;  
1852 }
1853
1854
1855 /**
1856  * g_option_group_set_translate_func:
1857  * @group: a #GOptionGroup
1858  * @func: the #GTranslateFunc, or %NULL 
1859  * @data: user data to pass to @func, or %NULL
1860  * @destroy_notify: a function which gets called to free @data, or %NULL
1861  * 
1862  * Sets the function which is used to translate user-visible
1863  * strings, for <option>--help</option> output. Different
1864  * groups can use different #GTranslateFunc<!-- -->s. If @func
1865  * is %NULL, strings are not translated.
1866  *
1867  * If you are using gettext(), you only need to set the translation
1868  * domain, see g_option_group_set_translation_domain().
1869  *
1870  * Since: 2.6
1871  **/
1872 void
1873 g_option_group_set_translate_func (GOptionGroup   *group,
1874                                    GTranslateFunc  func,
1875                                    gpointer        data,
1876                                    GDestroyNotify  destroy_notify)
1877 {
1878   g_return_if_fail (group != NULL);
1879   
1880   if (group->translate_notify)
1881     group->translate_notify (group->translate_data);
1882       
1883   group->translate_func = func;
1884   group->translate_data = data;
1885   group->translate_notify = destroy_notify;
1886 }
1887
1888 static gchar *
1889 dgettext_swapped (const gchar *msgid, 
1890                   const gchar *domainname)
1891 {
1892   return dgettext (domainname, msgid);
1893 }
1894
1895 /**
1896  * g_option_group_set_translation_domain:
1897  * @group: a #GOptionGroup
1898  * @domain: the domain to use
1899  * 
1900  * A convenience function to use gettext() for translating
1901  * user-visible strings. 
1902  * 
1903  * Since: 2.6
1904  **/
1905 void
1906 g_option_group_set_translation_domain (GOptionGroup *group,
1907                                        const gchar  *domain)
1908 {
1909   g_return_if_fail (group != NULL);
1910
1911   g_option_group_set_translate_func (group, 
1912                                      (GTranslateFunc)dgettext_swapped,
1913                                      g_strdup (domain),
1914                                      g_free);
1915
1916
1917 /**
1918  * g_option_context_set_translate_func:
1919  * @context: a #GOptionContext
1920  * @func: the #GTranslateFunc, or %NULL 
1921  * @data: user data to pass to @func, or %NULL
1922  * @destroy_notify: a function which gets called to free @data, or %NULL
1923  * 
1924  * Sets the function which is used to translate the contexts 
1925  * user-visible strings, for <option>--help</option> output. 
1926  * If @func is %NULL, strings are not translated.
1927  *
1928  * Note that option groups have their own translation functions, 
1929  * this function only affects the @parameter_string (see g_option_context_nex()), 
1930  * the summary (see g_option_context_set_summary()) and the description 
1931  * (see g_option_context_set_description()).
1932  *
1933  * If you are using gettext(), you only need to set the translation
1934  * domain, see g_context_group_set_translation_domain().
1935  *
1936  * Since: 2.12
1937  **/
1938 void
1939 g_option_context_set_translate_func (GOptionContext *context,
1940                                      GTranslateFunc func,
1941                                      gpointer       data,
1942                                      GDestroyNotify destroy_notify)
1943 {
1944   g_return_if_fail (context != NULL);
1945   
1946   if (context->translate_notify)
1947     context->translate_notify (context->translate_data);
1948       
1949   context->translate_func = func;
1950   context->translate_data = data;
1951   context->translate_notify = destroy_notify;
1952 }
1953
1954 /**
1955  * g_option_context_set_translation_domain:
1956  * @context: a #GOptionContext
1957  * @domain: the domain to use
1958  * 
1959  * A convenience function to use gettext() for translating
1960  * user-visible strings. 
1961  * 
1962  * Since: 2.12
1963  **/
1964 void
1965 g_option_context_set_translation_domain (GOptionContext *context,
1966                                          const gchar     *domain)
1967 {
1968   g_return_if_fail (context != NULL);
1969
1970   g_option_context_set_translate_func (context, 
1971                                        (GTranslateFunc)dgettext_swapped,
1972                                        g_strdup (domain),
1973                                        g_free);
1974 }
1975
1976 /**
1977  * g_option_context_set_summary:
1978  * @context: a #GOptionContext
1979  * @summary: a string to be shown in <option>--help</option> output 
1980  *  before the list of options, or %NULL
1981  * 
1982  * Adds a string to be displayed in <option>--help</option> output
1983  * before the list of options. This is typically a summary of the
1984  * program functionality. 
1985  *
1986  * Note that the summary is translated (see 
1987  * g_option_context_set_translate_func()).
1988  *
1989  * Since: 2.12
1990  */
1991 void
1992 g_option_context_set_summary (GOptionContext *context,
1993                               const gchar    *summary)
1994 {
1995   g_return_if_fail (context != NULL);
1996
1997   g_free (context->summary);
1998   context->summary = g_strdup (summary);
1999 }
2000
2001
2002 /**
2003  * g_option_context_get_summary:
2004  * @context: a #GOptionContext
2005  * 
2006  * Returns the summary. See g_option_context_set_summary().
2007  *
2008  * Returns: the summary
2009  *
2010  * Since: 2.12
2011  */
2012 G_CONST_RETURN gchar *
2013 g_option_context_get_summary (GOptionContext *context)
2014 {
2015   g_return_val_if_fail (context != NULL, NULL);
2016
2017   return context->summary;
2018 }
2019
2020 /**
2021  * g_option_context_set_description:
2022  * @context: a #GOptionContext
2023  * @description: a string to be shown in <option>--help</option> output 
2024  *   after the list of options, or %NULL
2025  * 
2026  * Adds a string to be displayed in <option>--help</option> output
2027  * after the list of options. This text often includes a bug reporting
2028  * address.
2029  *
2030  * Note that the summary is translated (see 
2031  * g_option_context_set_translate_func()).
2032  *
2033  * Since: 2.12
2034  */
2035 void
2036 g_option_context_set_description (GOptionContext *context,
2037                                   const gchar    *description)
2038 {
2039   g_return_if_fail (context != NULL);
2040
2041   g_free (context->description);
2042   context->description = g_strdup (description);
2043 }
2044
2045
2046 /**
2047  * g_option_context_get_description:
2048  * @context: a #GOptionContext
2049  * 
2050  * Returns the description. See g_option_context_set_description().
2051  *
2052  * Returns: the description
2053  *
2054  * Since: 2.12
2055  */
2056 G_CONST_RETURN gchar *
2057 g_option_context_get_description (GOptionContext *context)
2058 {
2059   g_return_val_if_fail (context != NULL, NULL);
2060
2061   return context->description;
2062 }
2063
2064
2065 #define __G_OPTION_C__
2066 #include "galiasdef.c"