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