Move short_month_names and long_month_names to bss.
[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   switch (entry->arg)
772     {
773     case G_OPTION_ARG_NONE:
774       {
775         change = get_change (context, G_OPTION_ARG_NONE,
776                              entry->arg_data);
777
778         *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
779         break;
780       }      
781     case G_OPTION_ARG_STRING:
782       {
783         gchar *data;
784         
785         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
786
787         if (!data)
788           return FALSE;
789
790         change = get_change (context, G_OPTION_ARG_STRING,
791                              entry->arg_data);
792         g_free (change->allocated.str);
793         
794         change->prev.str = *(gchar **)entry->arg_data;
795         change->allocated.str = data;
796         
797         *(gchar **)entry->arg_data = data;
798         break;
799       }
800     case G_OPTION_ARG_STRING_ARRAY:
801       {
802         gchar *data;
803
804         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
805
806         if (!data)
807           return FALSE;
808
809         change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
810                              entry->arg_data);
811
812         if (change->allocated.array.len == 0)
813           {
814             change->prev.array = *(gchar ***)entry->arg_data;
815             change->allocated.array.data = g_new (gchar *, 2);
816           }
817         else
818           change->allocated.array.data =
819             g_renew (gchar *, change->allocated.array.data,
820                      change->allocated.array.len + 2);
821
822         change->allocated.array.data[change->allocated.array.len] = data;
823         change->allocated.array.data[change->allocated.array.len + 1] = NULL;
824
825         change->allocated.array.len ++;
826
827         *(gchar ***)entry->arg_data = change->allocated.array.data;
828
829         break;
830       }
831       
832     case G_OPTION_ARG_FILENAME:
833       {
834         gchar *data;
835
836 #ifdef G_OS_WIN32
837         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
838         
839         if (!data)
840           return FALSE;
841 #else
842         data = g_strdup (value);
843 #endif
844         change = get_change (context, G_OPTION_ARG_FILENAME,
845                              entry->arg_data);
846         g_free (change->allocated.str);
847         
848         change->prev.str = *(gchar **)entry->arg_data;
849         change->allocated.str = data;
850
851         *(gchar **)entry->arg_data = data;
852         break;
853       }
854
855     case G_OPTION_ARG_FILENAME_ARRAY:
856       {
857         gchar *data;
858         
859 #ifdef G_OS_WIN32
860         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
861         
862         if (!data)
863           return FALSE;
864 #else
865         data = g_strdup (value);
866 #endif
867         change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
868                              entry->arg_data);
869
870         if (change->allocated.array.len == 0)
871           {
872             change->prev.array = *(gchar ***)entry->arg_data;
873             change->allocated.array.data = g_new (gchar *, 2);
874           }
875         else
876           change->allocated.array.data =
877             g_renew (gchar *, change->allocated.array.data,
878                      change->allocated.array.len + 2);
879
880         change->allocated.array.data[change->allocated.array.len] = data;
881         change->allocated.array.data[change->allocated.array.len + 1] = NULL;
882
883         change->allocated.array.len ++;
884
885         *(gchar ***)entry->arg_data = change->allocated.array.data;
886
887         break;
888       }
889       
890     case G_OPTION_ARG_INT:
891       {
892         gint data;
893
894         if (!parse_int (option_name, value,
895                         &data,
896                         error))
897           return FALSE;
898
899         change = get_change (context, G_OPTION_ARG_INT,
900                              entry->arg_data);
901         change->prev.integer = *(gint *)entry->arg_data;
902         *(gint *)entry->arg_data = data;
903         break;
904       }
905     case G_OPTION_ARG_CALLBACK:
906       {
907         gchar *data;
908         gboolean retval;
909
910         if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
911           data = NULL;
912         else if (entry->flags & G_OPTION_FLAG_NO_ARG)
913           data = NULL;
914         else if (entry->flags & G_OPTION_FLAG_FILENAME)
915           {
916 #ifdef G_OS_WIN32
917             data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
918 #else
919             data = g_strdup (value);
920 #endif
921           }
922         else
923           data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
924
925         if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) && 
926             !data)
927           return FALSE;
928
929         retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
930         
931         g_free (data);
932         
933         return retval;
934         
935         break;
936       }
937     case G_OPTION_ARG_DOUBLE:
938       {
939         gdouble data;
940
941         if (!parse_double (option_name, value,
942                         &data,
943                         error))
944           {
945             return FALSE;
946           }
947
948         change = get_change (context, G_OPTION_ARG_DOUBLE,
949                              entry->arg_data);
950         change->prev.dbl = *(gdouble *)entry->arg_data;
951         *(gdouble *)entry->arg_data = data;
952         break;
953       }
954     default:
955       g_assert_not_reached ();
956     }
957
958   return TRUE;
959 }
960
961 static gboolean
962 parse_short_option (GOptionContext *context,
963                     GOptionGroup   *group,
964                     gint            index,
965                     gint           *new_index,
966                     gchar           arg,
967                     gint           *argc,
968                     gchar        ***argv,
969                     GError        **error,
970                     gboolean       *parsed)
971 {
972   gint j;
973     
974   for (j = 0; j < group->n_entries; j++)
975     {
976       if (arg == group->entries[j].short_name)
977         {
978           gchar *option_name;
979           gchar *value = NULL;
980           
981           option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
982
983           if (NO_ARG (&group->entries[j]))
984             value = NULL;
985           else
986             {
987               if (*new_index > index)
988                 {
989                   g_set_error (error, 
990                                G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
991                                _("Error parsing option %s"), option_name);
992                   g_free (option_name);
993                   return FALSE;
994                 }
995
996               if (index < *argc - 1)
997                 {
998                   if (!OPTIONAL_ARG (&group->entries[j]))       
999                     {    
1000                       value = (*argv)[index + 1];
1001                       add_pending_null (context, &((*argv)[index + 1]), NULL);
1002                       *new_index = index+1;
1003                     }
1004                   else
1005                     {
1006                       if ((*argv)[index + 1][0] == '-') 
1007                         value = NULL;
1008                       else
1009                         {
1010                           value = (*argv)[index + 1];
1011                           add_pending_null (context, &((*argv)[index + 1]), NULL);
1012                           *new_index = index + 1;
1013                         }
1014                     }
1015                 }
1016               else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1017                 value = NULL;
1018               else
1019                 {
1020                   g_set_error (error, 
1021                                G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1022                                _("Missing argument for %s"), option_name);
1023                   g_free (option_name);
1024                   return FALSE;
1025                 }
1026             }
1027
1028           if (!parse_arg (context, group, &group->entries[j], 
1029                           value, option_name, error))
1030             {
1031               g_free (option_name);
1032               return FALSE;
1033             }
1034           
1035           g_free (option_name);
1036           *parsed = TRUE;
1037         }
1038     }
1039
1040   return TRUE;
1041 }
1042
1043 static gboolean
1044 parse_long_option (GOptionContext *context,
1045                    GOptionGroup   *group,
1046                    gint           *index,
1047                    gchar          *arg,
1048                    gboolean        aliased,
1049                    gint           *argc,
1050                    gchar        ***argv,
1051                    GError        **error,
1052                    gboolean       *parsed)
1053 {
1054   gint j;
1055
1056   for (j = 0; j < group->n_entries; j++)
1057     {
1058       if (*index >= *argc)
1059         return TRUE;
1060
1061       if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1062         continue;
1063
1064       if (NO_ARG (&group->entries[j]) &&
1065           strcmp (arg, group->entries[j].long_name) == 0)
1066         {
1067           gchar *option_name;
1068
1069           option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1070           parse_arg (context, group, &group->entries[j],
1071                      NULL, option_name, error);
1072           g_free(option_name);
1073           
1074           add_pending_null (context, &((*argv)[*index]), NULL);
1075           *parsed = TRUE;
1076         }
1077       else
1078         {
1079           gint len = strlen (group->entries[j].long_name);
1080           
1081           if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1082               (arg[len] == '=' || arg[len] == 0))
1083             {
1084               gchar *value = NULL;
1085               gchar *option_name;
1086
1087               add_pending_null (context, &((*argv)[*index]), NULL);
1088               option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1089
1090               if (arg[len] == '=')
1091                 value = arg + len + 1;
1092               else if (*index < *argc - 1) 
1093                 {
1094                   if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))  
1095                     {    
1096                       value = (*argv)[*index + 1];
1097                       add_pending_null (context, &((*argv)[*index + 1]), NULL);
1098                       (*index)++;
1099                     }
1100                   else
1101                     {
1102                       if ((*argv)[*index + 1][0] == '-') 
1103                         {
1104                           gboolean retval;
1105                           retval = parse_arg (context, group, &group->entries[j],
1106                                               NULL, option_name, error);
1107                           *parsed = TRUE;
1108                           g_free (option_name);
1109                           return retval;
1110                         }
1111                       else
1112                         {
1113                           value = (*argv)[*index + 1];
1114                           add_pending_null (context, &((*argv)[*index + 1]), NULL);
1115                           (*index)++;
1116                         }
1117                     }
1118                 }
1119               else if (*index >= *argc - 1 &&
1120                        group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1121                 {
1122                     gboolean retval;
1123                     retval = parse_arg (context, group, &group->entries[j],
1124                                         NULL, option_name, error);
1125                     *parsed = TRUE;
1126                     g_free (option_name);
1127                     return retval;
1128                 }
1129               else
1130                 {
1131                   g_set_error (error, 
1132                                G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1133                                _("Missing argument for %s"), option_name);
1134                   g_free (option_name);
1135                   return FALSE;
1136                 }
1137
1138               if (!parse_arg (context, group, &group->entries[j], 
1139                               value, option_name, error))
1140                 {
1141                   g_free (option_name);
1142                   return FALSE;
1143                 }
1144
1145               g_free (option_name);
1146               *parsed = TRUE;
1147             } 
1148         }
1149     }
1150   
1151   return TRUE;
1152 }
1153
1154 static gboolean
1155 parse_remaining_arg (GOptionContext *context,
1156                      GOptionGroup   *group,
1157                      gint           *index,
1158                      gint           *argc,
1159                      gchar        ***argv,
1160                      GError        **error,
1161                      gboolean       *parsed)
1162 {
1163   gint j;
1164
1165   for (j = 0; j < group->n_entries; j++)
1166     {
1167       if (*index >= *argc)
1168         return TRUE;
1169
1170       if (group->entries[j].long_name[0])
1171         continue;
1172
1173       g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1174                             group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1175       
1176       add_pending_null (context, &((*argv)[*index]), NULL);
1177       
1178       if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1179         return FALSE;
1180       
1181       *parsed = TRUE;
1182       return TRUE;
1183     }
1184
1185   return TRUE;
1186 }
1187
1188 static void
1189 free_changes_list (GOptionContext *context,
1190                    gboolean        revert)
1191 {
1192   GList *list;
1193
1194   for (list = context->changes; list != NULL; list = list->next)
1195     {
1196       Change *change = list->data;
1197
1198       if (revert)
1199         {
1200           switch (change->arg_type)
1201             {
1202             case G_OPTION_ARG_NONE:
1203               *(gboolean *)change->arg_data = change->prev.bool;
1204               break;
1205             case G_OPTION_ARG_INT:
1206               *(gint *)change->arg_data = change->prev.integer;
1207               break;
1208             case G_OPTION_ARG_STRING:
1209             case G_OPTION_ARG_FILENAME:
1210               g_free (change->allocated.str);
1211               *(gchar **)change->arg_data = change->prev.str;
1212               break;
1213             case G_OPTION_ARG_STRING_ARRAY:
1214             case G_OPTION_ARG_FILENAME_ARRAY:
1215               g_strfreev (change->allocated.array.data);
1216               *(gchar ***)change->arg_data = change->prev.array;
1217               break;
1218             case G_OPTION_ARG_DOUBLE:
1219               *(gdouble *)change->arg_data = change->prev.dbl;
1220               break;
1221             default:
1222               g_assert_not_reached ();
1223             }
1224         }
1225       
1226       g_free (change);
1227     }
1228
1229   g_list_free (context->changes);
1230   context->changes = NULL;
1231 }
1232
1233 static void
1234 free_pending_nulls (GOptionContext *context,
1235                     gboolean        perform_nulls)
1236 {
1237   GList *list;
1238
1239   for (list = context->pending_nulls; list != NULL; list = list->next)
1240     {
1241       PendingNull *n = list->data;
1242
1243       if (perform_nulls)
1244         {
1245           if (n->value)
1246             {
1247               /* Copy back the short options */
1248               *(n->ptr)[0] = '-';             
1249               strcpy (*n->ptr + 1, n->value);
1250             }
1251           else
1252             *n->ptr = NULL;
1253         }
1254       
1255       g_free (n->value);
1256       g_free (n);
1257     }
1258
1259   g_list_free (context->pending_nulls);
1260   context->pending_nulls = NULL;
1261 }
1262
1263 /**
1264  * g_option_context_parse:
1265  * @context: a #GOptionContext
1266  * @argc: a pointer to the number of command line arguments.
1267  * @argv: a pointer to the array of command line arguments.
1268  * @error: a return location for errors 
1269  * 
1270  * Parses the command line arguments, recognizing options
1271  * which have been added to @context. A side-effect of 
1272  * calling this function is that g_set_prgname() will be
1273  * called.
1274  *
1275  * If the parsing is successful, any parsed arguments are
1276  * removed from the array and @argc and @argv are updated 
1277  * accordingly. A '--' option is stripped from @argv
1278  * unless there are unparsed options before and after it, 
1279  * or some of the options after it start with '-'. In case 
1280  * of an error, @argc and @argv are left unmodified. 
1281  *
1282  * If automatic <option>--help</option> support is enabled
1283  * (see g_option_context_set_help_enabled()), and the 
1284  * @argv array contains one of the recognized help options,
1285  * this function will produce help output to stdout and
1286  * call <literal>exit (0)</literal>.
1287  * 
1288  * Return value: %TRUE if the parsing was successful, 
1289  *               %FALSE if an error occurred
1290  *
1291  * Since: 2.6
1292  **/
1293 gboolean
1294 g_option_context_parse (GOptionContext   *context,
1295                         gint             *argc,
1296                         gchar          ***argv,
1297                         GError          **error)
1298 {
1299   gint i, j, k;
1300   GList *list;
1301
1302   /* Set program name */
1303   if (!g_get_prgname())
1304     {
1305       if (argc && argv && *argc)
1306         {
1307           gchar *prgname;
1308           
1309           prgname = g_path_get_basename ((*argv)[0]);
1310           g_set_prgname (prgname);
1311           g_free (prgname);
1312         }
1313       else
1314         g_set_prgname ("<unknown>");
1315     }
1316
1317   /* Call pre-parse hooks */
1318   list = context->groups;
1319   while (list)
1320     {
1321       GOptionGroup *group = list->data;
1322       
1323       if (group->pre_parse_func)
1324         {
1325           if (!(* group->pre_parse_func) (context, group,
1326                                           group->user_data, error))
1327             goto fail;
1328         }
1329       
1330       list = list->next;
1331     }
1332
1333   if (context->main_group && context->main_group->pre_parse_func)
1334     {
1335       if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1336                                                     context->main_group->user_data, error))
1337         goto fail;
1338     }
1339
1340   if (argc && argv)
1341     {
1342       gboolean stop_parsing = FALSE;
1343       gboolean has_unknown = FALSE;
1344       gint separator_pos = 0;
1345
1346       for (i = 1; i < *argc; i++)
1347         {
1348           gchar *arg, *dash;
1349           gboolean parsed = FALSE;
1350
1351           if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1352             {
1353               if ((*argv)[i][1] == '-')
1354                 {
1355                   /* -- option */
1356
1357                   arg = (*argv)[i] + 2;
1358
1359                   /* '--' terminates list of arguments */
1360                   if (*arg == 0)
1361                     {
1362                       separator_pos = i;
1363                       stop_parsing = TRUE;
1364                       continue;
1365                     }
1366
1367                   /* Handle help options */
1368                   if (context->help_enabled)
1369                     {
1370                       if (strcmp (arg, "help") == 0)
1371                         print_help (context, TRUE, NULL);
1372                       else if (strcmp (arg, "help-all") == 0)
1373                         print_help (context, FALSE, NULL);                    
1374                       else if (strncmp (arg, "help-", 5) == 0)
1375                         {
1376                           GList *list;
1377                           
1378                           list = context->groups;
1379                           
1380                           while (list)
1381                             {
1382                               GOptionGroup *group = list->data;
1383                               
1384                               if (strcmp (arg + 5, group->name) == 0)
1385                                 print_help (context, FALSE, group);                                           
1386                               
1387                               list = list->next;
1388                             }
1389                         }
1390                     }
1391
1392                   if (context->main_group &&
1393                       !parse_long_option (context, context->main_group, &i, arg,
1394                                           FALSE, argc, argv, error, &parsed))
1395                     goto fail;
1396
1397                   if (parsed)
1398                     continue;
1399                   
1400                   /* Try the groups */
1401                   list = context->groups;
1402                   while (list)
1403                     {
1404                       GOptionGroup *group = list->data;
1405                       
1406                       if (!parse_long_option (context, group, &i, arg, 
1407                                               FALSE, argc, argv, error, &parsed))
1408                         goto fail;
1409                       
1410                       if (parsed)
1411                         break;
1412                       
1413                       list = list->next;
1414                     }
1415                   
1416                   if (parsed)
1417                     continue;
1418
1419                   /* Now look for --<group>-<option> */
1420                   dash = strchr (arg, '-');
1421                   if (dash)
1422                     {
1423                       /* Try the groups */
1424                       list = context->groups;
1425                       while (list)
1426                         {
1427                           GOptionGroup *group = list->data;
1428                           
1429                           if (strncmp (group->name, arg, dash - arg) == 0)
1430                             {
1431                               if (!parse_long_option (context, group, &i, dash + 1,
1432                                                       TRUE, argc, argv, error, &parsed))
1433                                 goto fail;
1434                               
1435                               if (parsed)
1436                                 break;
1437                             }
1438                           
1439                           list = list->next;
1440                         }
1441                     }
1442                   
1443                   if (context->ignore_unknown)
1444                     continue;
1445                 }
1446               else
1447                 {
1448                   /* short option */
1449
1450                   gint new_i, j;
1451                   gboolean *nulled_out = NULL;
1452                   
1453                   arg = (*argv)[i] + 1;
1454
1455                   new_i = i;
1456
1457                   if (context->ignore_unknown)
1458                     nulled_out = g_new0 (gboolean, strlen (arg));
1459                   
1460                   for (j = 0; j < strlen (arg); j++)
1461                     {
1462                       if (context->help_enabled && arg[j] == '?')
1463                         print_help (context, TRUE, NULL);
1464                       
1465                       parsed = FALSE;
1466                       
1467                       if (context->main_group &&
1468                           !parse_short_option (context, context->main_group,
1469                                                i, &new_i, arg[j],
1470                                                argc, argv, error, &parsed))
1471                         {
1472
1473                           g_free (nulled_out);
1474                           goto fail;
1475                         }
1476
1477                       if (!parsed)
1478                         {
1479                           /* Try the groups */
1480                           list = context->groups;
1481                           while (list)
1482                             {
1483                               GOptionGroup *group = list->data;
1484                               
1485                               if (!parse_short_option (context, group, i, &new_i, arg[j],
1486                                                        argc, argv, error, &parsed))
1487                                 goto fail;
1488                               
1489                               if (parsed)
1490                                 break;
1491                           
1492                               list = list->next;
1493                             }
1494                         }
1495
1496                       if (context->ignore_unknown)
1497                         {
1498                           if (parsed)
1499                             nulled_out[j] = TRUE;
1500                           else
1501                             continue;
1502                         }
1503
1504                       if (!parsed)
1505                         break;
1506                     }
1507
1508                   if (context->ignore_unknown)
1509                     {
1510                       gchar *new_arg = NULL; 
1511                       gint arg_index = 0;
1512                       
1513                       for (j = 0; j < strlen (arg); j++)
1514                         {
1515                           if (!nulled_out[j])
1516                             {
1517                               if (!new_arg)
1518                                 new_arg = g_malloc (strlen (arg) + 1);
1519                               new_arg[arg_index++] = arg[j];
1520                             }
1521                         }
1522                       if (new_arg)
1523                         new_arg[arg_index] = '\0';
1524                       
1525                       add_pending_null (context, &((*argv)[i]), new_arg);
1526                     }
1527                   else if (parsed)
1528                     {
1529                       add_pending_null (context, &((*argv)[i]), NULL);
1530                       i = new_i;
1531                     }
1532                 }
1533               
1534               if (!parsed)
1535                 has_unknown = TRUE;
1536
1537               if (!parsed && !context->ignore_unknown)
1538                 {
1539                   g_set_error (error,
1540                                G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1541                                    _("Unknown option %s"), (*argv)[i]);
1542                   goto fail;
1543                 }
1544             }
1545           else
1546             {
1547               /* Collect remaining args */
1548               if (context->main_group &&
1549                   !parse_remaining_arg (context, context->main_group, &i,
1550                                         argc, argv, error, &parsed))
1551                 goto fail;
1552               
1553               if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1554                 separator_pos = 0;
1555             }
1556         }
1557
1558       if (separator_pos > 0)
1559         add_pending_null (context, &((*argv)[separator_pos]), NULL);
1560         
1561     }
1562
1563   /* Call post-parse hooks */
1564   list = context->groups;
1565   while (list)
1566     {
1567       GOptionGroup *group = list->data;
1568       
1569       if (group->post_parse_func)
1570         {
1571           if (!(* group->post_parse_func) (context, group,
1572                                            group->user_data, error))
1573             goto fail;
1574         }
1575       
1576       list = list->next;
1577     }
1578   
1579   if (context->main_group && context->main_group->post_parse_func)
1580     {
1581       if (!(* context->main_group->post_parse_func) (context, context->main_group,
1582                                                      context->main_group->user_data, error))
1583         goto fail;
1584     }
1585   
1586   if (argc && argv)
1587     {
1588       free_pending_nulls (context, TRUE);
1589       
1590       for (i = 1; i < *argc; i++)
1591         {
1592           for (k = i; k < *argc; k++)
1593             if ((*argv)[k] != NULL)
1594               break;
1595           
1596           if (k > i)
1597             {
1598               k -= i;
1599               for (j = i + k; j < *argc; j++)
1600                 {
1601                   (*argv)[j-k] = (*argv)[j];
1602                   (*argv)[j] = NULL;
1603                 }
1604               *argc -= k;
1605             }
1606         }      
1607     }
1608
1609   return TRUE;
1610
1611  fail:
1612   
1613   /* Call error hooks */
1614   list = context->groups;
1615   while (list)
1616     {
1617       GOptionGroup *group = list->data;
1618       
1619       if (group->error_func)
1620         (* group->error_func) (context, group,
1621                                group->user_data, error);
1622       
1623       list = list->next;
1624     }
1625
1626   if (context->main_group && context->main_group->error_func)
1627     (* context->main_group->error_func) (context, context->main_group,
1628                                          context->main_group->user_data, error);
1629   
1630   free_changes_list (context, TRUE);
1631   free_pending_nulls (context, FALSE);
1632
1633   return FALSE;
1634 }
1635                                    
1636 /**
1637  * g_option_group_new:
1638  * @name: the name for the option group, this is used to provide
1639  *   help for the options in this group with <option>--help-</option>@name
1640  * @description: a description for this group to be shown in 
1641  *   <option>--help</option>. This string is translated using the translation
1642  *   domain or translation function of the group
1643  * @help_description: a description for the <option>--help-</option>@name option.
1644  *   This string is translated using the translation domain or translation function
1645  *   of the group
1646  * @user_data: user data that will be passed to the pre- and post-parse hooks,
1647  *   the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1648  * @destroy: a function that will be called to free @user_data, or %NULL
1649  * 
1650  * Creates a new #GOptionGroup.
1651  * 
1652  * Return value: a newly created option group. It should be added 
1653  *   to a #GOptionContext or freed with g_option_group_free().
1654  *
1655  * Since: 2.6
1656  **/
1657 GOptionGroup *
1658 g_option_group_new (const gchar    *name,
1659                     const gchar    *description,
1660                     const gchar    *help_description,
1661                     gpointer        user_data,
1662                     GDestroyNotify  destroy)
1663
1664 {
1665   GOptionGroup *group;
1666
1667   group = g_new0 (GOptionGroup, 1);
1668   group->name = g_strdup (name);
1669   group->description = g_strdup (description);
1670   group->help_description = g_strdup (help_description);
1671   group->user_data = user_data;
1672   group->destroy_notify = destroy;
1673   
1674   return group;
1675 }
1676
1677
1678 /**
1679  * g_option_group_free:
1680  * @group: a #GOptionGroup
1681  * 
1682  * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1683  * free groups which have been added to a #GOptionContext.
1684  *
1685  * Since: 2.6
1686  **/
1687 void
1688 g_option_group_free (GOptionGroup *group)
1689 {
1690   g_return_if_fail (group != NULL);
1691
1692   g_free (group->name);
1693   g_free (group->description);
1694   g_free (group->help_description);
1695
1696   g_free (group->entries);
1697   
1698   if (group->destroy_notify)
1699     (* group->destroy_notify) (group->user_data);
1700
1701   if (group->translate_notify)
1702     (* group->translate_notify) (group->translate_data);
1703   
1704   g_free (group);
1705 }
1706
1707
1708 /**
1709  * g_option_group_add_entries:
1710  * @group: a #GOptionGroup
1711  * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1712  * 
1713  * Adds the options specified in @entries to @group.
1714  *
1715  * Since: 2.6
1716  **/
1717 void
1718 g_option_group_add_entries (GOptionGroup       *group,
1719                             const GOptionEntry *entries)
1720 {
1721   gint i, n_entries;
1722   
1723   g_return_if_fail (entries != NULL);
1724
1725   for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1726
1727   group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1728
1729   memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1730
1731   for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1732     {
1733       gchar c = group->entries[i].short_name;
1734
1735       if (c)
1736         {
1737           if (c == '-' || !g_ascii_isprint (c))
1738             {
1739               g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1740               group->entries[i].short_name = 0;
1741             }
1742         }
1743     }
1744
1745   group->n_entries += n_entries;
1746 }
1747
1748 /**
1749  * g_option_group_set_parse_hooks:
1750  * @group: a #GOptionGroup
1751  * @pre_parse_func: a function to call before parsing, or %NULL
1752  * @post_parse_func: a function to call after parsing, or %NULL
1753  * 
1754  * Associates two functions with @group which will be called 
1755  * from g_option_context_parse() before the first option is parsed
1756  * and after the last option has been parsed, respectively.
1757  *
1758  * Note that the user data to be passed to @pre_parse_func and
1759  * @post_parse_func can be specified when constructing the group
1760  * with g_option_group_new().
1761  *
1762  * Since: 2.6
1763  **/
1764 void
1765 g_option_group_set_parse_hooks (GOptionGroup     *group,
1766                                 GOptionParseFunc  pre_parse_func,
1767                                 GOptionParseFunc  post_parse_func)
1768 {
1769   g_return_if_fail (group != NULL);
1770
1771   group->pre_parse_func = pre_parse_func;
1772   group->post_parse_func = post_parse_func;  
1773 }
1774
1775 /**
1776  * g_option_group_set_error_hook:
1777  * @group: a #GOptionGroup
1778  * @error_func: a function to call when an error occurs
1779  * 
1780  * Associates a function with @group which will be called 
1781  * from g_option_context_parse() when an error occurs.
1782  *
1783  * Note that the user data to be passed to @pre_parse_func and
1784  * @post_parse_func can be specified when constructing the group
1785  * with g_option_group_new().
1786  *
1787  * Since: 2.6
1788  **/
1789 void
1790 g_option_group_set_error_hook (GOptionGroup     *group,
1791                                GOptionErrorFunc  error_func)
1792 {
1793   g_return_if_fail (group != NULL);
1794
1795   group->error_func = error_func;  
1796 }
1797
1798
1799 /**
1800  * g_option_group_set_translate_func:
1801  * @group: a #GOptionGroup
1802  * @func: the #GTranslateFunc, or %NULL 
1803  * @data: user data to pass to @func, or %NULL
1804  * @destroy_notify: a function which gets called to free @data, or %NULL
1805  * 
1806  * Sets the function which is used to translate user-visible
1807  * strings, for <option>--help</option> output. Different
1808  * groups can use different #GTranslateFunc<!-- -->s. If @func
1809  * is %NULL, strings are not translated.
1810  *
1811  * If you are using gettext(), you only need to set the translation
1812  * domain, see g_option_group_set_translation_domain().
1813  *
1814  * Since: 2.6
1815  **/
1816 void
1817 g_option_group_set_translate_func (GOptionGroup   *group,
1818                                    GTranslateFunc  func,
1819                                    gpointer        data,
1820                                    GDestroyNotify  destroy_notify)
1821 {
1822   g_return_if_fail (group != NULL);
1823   
1824   if (group->translate_notify)
1825     group->translate_notify (group->translate_data);
1826       
1827   group->translate_func = func;
1828   group->translate_data = data;
1829   group->translate_notify = destroy_notify;
1830 }
1831
1832 static gchar *
1833 dgettext_swapped (const gchar *msgid, 
1834                   const gchar *domainname)
1835 {
1836   return dgettext (domainname, msgid);
1837 }
1838
1839 /**
1840  * g_option_group_set_translation_domain:
1841  * @group: a #GOptionGroup
1842  * @domain: the domain to use
1843  * 
1844  * A convenience function to use gettext() for translating
1845  * user-visible strings. 
1846  * 
1847  * Since: 2.6
1848  **/
1849 void
1850 g_option_group_set_translation_domain (GOptionGroup *group,
1851                                        const gchar  *domain)
1852 {
1853   g_return_if_fail (group != NULL);
1854
1855   g_option_group_set_translate_func (group, 
1856                                      (GTranslateFunc)dgettext_swapped,
1857                                      g_strdup (domain),
1858                                      g_free);
1859
1860
1861 #define __G_OPTION_C__
1862 #include "galiasdef.c"