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