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