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