Add GOption.
[platform/upstream/glib.git] / glib / goption.c
1 /* goption.c - Option parser
2  *
3  *  Copyright (C) 1999, 2003 Red Hat Software
4  *  Copyright (C) 2004       Anders Carlsson <andersca@gnome.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "goption.h"
23
24 #include "glib.h"
25 #include "gi18n.h"
26
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30
31 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
32
33 typedef struct {
34   GOptionArg arg_type;
35   gpointer arg_data;  
36   union {
37     gboolean bool;
38     gint integer;
39     gchar *str;
40     gchar **array;
41   } prev;
42   union {
43     gchar *str;
44     struct {
45       int len;
46       gchar **data;
47     } array;
48   } allocated;
49 } Change;
50
51 typedef struct
52 {
53   gchar **ptr;
54   gchar *value;
55 } PendingNull;
56
57 struct _GOptionContext
58 {
59   GList *groups;
60
61   gchar *parameter_string;
62
63   gboolean help_enabled;
64   gboolean ignore_unknown;
65   
66   GOptionGroup *main_group;
67
68   /* We keep a list of change so we can revert them */
69   GList *changes;
70   
71   /* We also keep track of all argv elements that should be NULLed or
72    * modified.
73    */
74   GList *pending_nulls;
75 };
76
77 struct _GOptionGroup
78 {
79   gchar *name;
80   gchar *description;
81   gchar *help_description;
82
83   GDestroyNotify  destroy_notify;
84   gpointer        user_data;
85
86   GTranslateFunc  translate_func;
87   GDestroyNotify  translate_notify;
88   gpointer        translate_data;
89
90   GOptionEntry *entries;
91   gint         n_entries;
92
93   GOptionParseFunc pre_parse_func;
94   GOptionParseFunc post_parse_func;
95   GOptionErrorFunc error_func;
96 };
97
98 static void free_changes_list (GOptionContext *context,
99                                gboolean        revert);
100 static void free_pending_nulls (GOptionContext *context,
101                                 gboolean        perform_nulls);
102
103 GQuark
104 g_option_context_error_quark (void)
105 {
106   static GQuark q = 0;
107   
108   if (q == 0)
109     q = g_quark_from_static_string ("g-option-context-error-quark");
110
111   return q;
112 }
113
114 GOptionContext *
115 g_option_context_new (const gchar *parameter_string)
116
117 {
118   GOptionContext *context;
119
120   context = g_new0 (GOptionContext, 1);
121
122   context->parameter_string = g_strdup (parameter_string);
123   context->help_enabled = TRUE;
124
125   return context;
126 }
127
128 void
129 g_option_context_free (GOptionContext *context)
130 {
131   g_return_if_fail (context != NULL);
132
133   g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
134   g_list_free (context->groups);
135
136   g_option_group_free (context->main_group);
137
138   free_changes_list (context, FALSE);
139   free_pending_nulls (context, FALSE);
140   
141   g_free (context->parameter_string);
142   
143   g_free (context);
144 }
145
146 void
147 g_option_context_set_help_enabled (GOptionContext *context,
148                                    gboolean        help_enabled)
149
150 {
151   g_return_if_fail (context != NULL);
152
153   context->help_enabled = help_enabled;
154 }
155
156 gboolean
157 g_option_context_get_help_enabled (GOptionContext *context)
158 {
159   g_return_val_if_fail (context != NULL, FALSE);
160
161   return context->help_enabled;
162 }
163
164 void
165 g_option_context_set_ignore_unknown_options (GOptionContext *context,
166                                              gboolean        ignore_unknown)
167 {
168   g_return_if_fail (context != NULL);
169
170   context->ignore_unknown = ignore_unknown;
171 }
172
173 gboolean
174 g_option_context_get_ignore_unknown_options (GOptionContext *context)
175 {
176   g_return_val_if_fail (context != NULL, FALSE);
177
178   return context->ignore_unknown;
179 }
180
181 void
182 g_option_context_add_group (GOptionContext *context,
183                             GOptionGroup   *group)
184 {
185   g_return_if_fail (context != NULL);
186   g_return_if_fail (group != NULL);
187   g_return_if_fail (group->name != NULL);
188   g_return_if_fail (group->description != NULL);
189   g_return_if_fail (group->help_description != NULL);
190
191   context->groups = g_list_prepend (context->groups, group);
192 }
193
194 void
195 g_option_context_set_main_group (GOptionContext *context,
196                                  GOptionGroup   *group)
197 {
198   g_return_if_fail (context != NULL);
199   g_return_if_fail (group != NULL);
200
201   context->main_group = group;
202 }
203
204 GOptionGroup *
205 g_option_context_get_main_group (GOptionContext *context)
206 {
207   g_return_val_if_fail (context != NULL, NULL);
208
209   return context->main_group;
210 }
211
212 void
213 g_option_context_add_main_entries (GOptionContext      *context,
214                                    const GOptionEntry  *entries,
215                                    const gchar         *translation_domain)
216 {
217   g_return_if_fail (entries != NULL);
218
219   if (!context->main_group)
220     context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
221   
222   g_option_group_add_entries (context->main_group, entries);
223   g_option_group_set_translation_domain (context->main_group, translation_domain);
224 }
225
226 static void
227 print_entry (GOptionGroup       *group,
228              gint                max_length,
229              const GOptionEntry *entry)
230 {
231   GString *str;
232
233   if (entry->flags & G_OPTION_FLAG_HIDDEN)
234     return;
235           
236   str = g_string_new (NULL);
237   
238   if (entry->short_name)
239     g_string_append_printf (str, "  -%c, --%s", entry->short_name, entry->long_name);
240   else
241     g_string_append_printf (str, "  --%s", entry->long_name);
242   
243   if (entry->arg_description)
244     g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
245   
246   g_print ("%-*s %s\n", max_length + 4, str->str,
247            entry->description ? TRANSLATE (group, entry->description) : "");
248   g_string_free (str, TRUE);  
249 }
250
251 static void
252 print_help (GOptionContext *context,
253             gboolean        main_help,
254             GOptionGroup   *group)
255 {
256   GList *list;
257   gint max_length, len;
258   gint i;
259
260   g_print ("Usage:\n");
261   g_print ("  %s [OPTION...] %s\n\n", g_get_prgname (),
262            context->parameter_string ? context->parameter_string : "");
263
264   list = context->groups;
265
266   max_length = g_utf8_strlen ("--help, -?", -1);
267
268   if (list)
269     {
270       len = g_utf8_strlen ("--help-all", -1);
271       max_length = MAX (max_length, len);
272     }
273
274   while (list != NULL)
275     {
276       GOptionGroup *group = list->data;
277       
278       /* First, we check the --help-<groupname> options */
279       len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
280       max_length = MAX (max_length, len);
281
282       /* Then we go through the entries */
283       for (i = 0; i < group->n_entries; i++)
284         {
285           len = g_utf8_strlen (group->entries[i].long_name, -1);
286
287           if (group->entries[i].short_name)
288             len += 4;
289
290           if (group->entries[i].arg != G_OPTION_ARG_NONE &&
291               group->entries[i].arg_description)
292             len += 1 + g_utf8_strlen (TRANSLATE (group, group->entries[i].arg_description), -1);
293
294           max_length = MAX (max_length, len);
295         }
296       
297       list = list->next;
298     }
299
300   /* Add a bit of padding */
301   max_length += 4;
302   
303   list = context->groups;
304
305   g_print ("Help Options:\n");
306   g_print ("  --%-*s %s\n", max_length, "help", "Show help options");
307
308   /* We only want --help-all when there are groups */
309   if (list)
310     g_print ("  --%-*s %s\n", max_length, "help-all", "Show all help options");
311
312   while (list)
313     {
314       GOptionGroup *group = list->data;
315
316       g_print ("  --help-%-*s %s\n", max_length - 5, group->name, TRANSLATE (group, group->help_description));
317       
318       list = list->next;
319     }
320
321   g_print ("\n");
322
323   if (group)
324     {
325       /* Print a certain group */
326       
327       g_print ("%s\n", TRANSLATE (group, group->description));
328       for (i = 0; i < group->n_entries; i++)
329         print_entry (group, max_length, &group->entries[i]);
330       g_print ("\n");
331     }
332   else if (!main_help)
333     {
334       /* Print all groups */
335
336       list = context->groups;
337
338       while (list)
339         {
340           GOptionGroup *group = list->data;
341
342           g_print ("%s\n", group->description);
343
344           for (i = 0; i < group->n_entries; i++)
345             if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
346               print_entry (group, max_length, &group->entries[i]);
347           
348           g_print ("\n");
349           list = list->next;
350         }
351     }
352   
353   /* Print application options if --help or --help-all has been specified */
354   if (main_help || !group)
355     {
356       list = context->groups;
357
358       g_print ("Application Options\n");
359
360       for (i = 0; i < context->main_group->n_entries; i++) 
361         print_entry (context->main_group, max_length, &context->main_group->entries[i]);
362
363       while (list != NULL)
364         {
365           GOptionGroup *group = list->data;
366
367           /* Print main entries from other groups */
368           for (i = 0; i < group->n_entries; i++)
369             if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
370               print_entry (group, max_length, &group->entries[i]);
371           
372           list = list->next;
373         }
374
375       g_print ("\n");
376     }
377
378   
379   exit (0);
380 }
381
382 static gboolean
383 parse_int (const char *arg_name,
384            const char *arg,
385            gint       *result,
386            GError    **error)
387 {
388   gchar *end;
389   glong tmp = strtol (arg, &end, 0);
390
391   errno = 0;
392   
393   if (*arg == '\0' || *end != '\0')
394     {
395       g_set_error (error,
396                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
397                    "Cannot parse integer value '%s' for --%s",
398                    arg, arg_name);
399       return FALSE;
400     }
401
402   *result = tmp;
403   if (*result != tmp || errno == ERANGE)
404     {
405       g_set_error (error,
406                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
407                    "Integer value '%s' for %s out of range",
408                    arg, arg_name);
409       return FALSE;
410     }
411
412   return TRUE;
413 }
414
415 static Change *
416 get_change (GOptionContext *context,
417             GOptionArg      arg_type,
418             gpointer        arg_data)
419 {
420   GList *list;
421   Change *change = NULL;
422   
423   for (list = context->changes; list != NULL; list = list->next)
424     {
425       change = list->data;
426
427       if (change->arg_data == arg_data)
428         break;
429     }
430
431   if (!change)
432     {
433       change = g_new0 (Change, 1);
434       change->arg_type = arg_type;
435       change->arg_data = arg_data;
436
437       context->changes = g_list_prepend (context->changes, change);
438     }
439   
440   return change;
441 }
442
443 static void
444 add_pending_null (GOptionContext *context,
445                   gchar         **ptr,
446                   gchar          *value)
447 {
448   PendingNull *n;
449
450   n = g_new0 (PendingNull, 1);
451   n->ptr = ptr;
452   n->value = value;
453
454   context->pending_nulls = g_list_prepend (context->pending_nulls, n);
455 }
456                   
457 static gboolean
458 parse_arg (GOptionContext *context,
459            GOptionGroup   *group,
460            GOptionEntry   *entry,
461            const gchar    *value,
462            const gchar    *option_name,
463            GError        **error)
464      
465 {
466   Change *change;
467   
468   switch (entry->arg)
469     {
470     case G_OPTION_ARG_NONE:
471       {
472         change = get_change (context, G_OPTION_ARG_NONE,
473                              entry->arg_data);
474
475         *(gboolean *)entry->arg_data = TRUE;
476         break;
477       }      
478     case G_OPTION_ARG_STRING:
479       {
480         gchar *data;
481
482         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
483
484         if (!data)
485           return FALSE;
486
487         change = get_change (context, G_OPTION_ARG_STRING,
488                              entry->arg_data);
489         g_free (change->allocated.str);
490         
491         change->prev.str = *(gchar **)entry->arg_data;
492         change->allocated.str = data;
493         
494         *(gchar **)entry->arg_data = data;
495         break;
496       }
497     case G_OPTION_ARG_STRING_ARRAY:
498       {
499         gchar *data;
500         
501         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
502
503         if (!data)
504           return FALSE;
505
506         change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
507                              entry->arg_data);
508
509         if (change->allocated.array.len == 0)
510           {
511             change->prev.array = entry->arg_data;
512             change->allocated.array.data = g_new (gchar *, 2);
513           }
514         else
515           change->allocated.array.data =
516             g_renew (gchar *, change->allocated.array.data,
517                      change->allocated.array.len + 2);
518
519         change->allocated.array.data[change->allocated.array.len] = data;
520         change->allocated.array.data[change->allocated.array.len + 1] = NULL;
521
522         change->allocated.array.len ++;
523
524         *(gchar ***)entry->arg_data = change->allocated.array.data;
525
526         break;
527       }
528       
529     case G_OPTION_ARG_FILENAME:
530       {
531         gchar *data;
532
533         data = g_strdup (value);
534
535         change = get_change (context, G_OPTION_ARG_FILENAME,
536                              entry->arg_data);
537         g_free (change->allocated.str);
538         
539         change->prev.str = *(gchar **)entry->arg_data;
540         change->allocated.str = data;
541
542         break;
543       }
544
545     case G_OPTION_ARG_FILENAME_ARRAY:
546       {
547         gchar *data;
548         
549         data = g_strdup (value);
550
551         change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
552                              entry->arg_data);
553
554         if (change->allocated.array.len == 0)
555           {
556             change->prev.array = entry->arg_data;
557             change->allocated.array.data = g_new (gchar *, 2);
558           }
559         else
560           change->allocated.array.data =
561             g_renew (gchar *, change->allocated.array.data,
562                      change->allocated.array.len + 2);
563
564         change->allocated.array.data[change->allocated.array.len] = data;
565         change->allocated.array.data[change->allocated.array.len + 1] = NULL;
566
567         change->allocated.array.len ++;
568
569         *(gchar ***)entry->arg_data = change->allocated.array.data;
570
571         break;
572       }
573       
574     case G_OPTION_ARG_INT:
575       {
576         int data;
577
578         if (!parse_int (option_name, value,
579                         &data,
580                         error))
581           return FALSE;
582
583         change = get_change (context, G_OPTION_ARG_INT,
584                              entry->arg_data);
585         change->prev.integer = *(gint *)entry->arg_data;
586         *(gint *)entry->arg_data = data;
587       }
588       break;
589     case G_OPTION_ARG_CALLBACK:
590       {
591         gchar *tmp;
592         gboolean retval;
593         
594         tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
595
596         if (!value)
597           return FALSE;
598
599         retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
600         
601         g_free (tmp);
602         
603         return retval;
604         
605         break;
606       }
607     default:
608       g_assert_not_reached ();
609     }
610
611   return TRUE;
612 }
613
614 static gboolean
615 parse_short_option (GOptionContext *context,
616                     GOptionGroup   *group,
617                     gint            index,
618                     gint           *new_index,
619                     gchar           arg,
620                     gint           *argc,
621                     gchar        ***argv,
622                     GError        **error,
623                     gboolean       *parsed)
624 {
625   int j;
626     
627   for (j = 0; j < group->n_entries; j++)
628     {
629       if (arg == group->entries[j].short_name)
630         {
631           if (group->entries[j].arg == G_OPTION_ARG_NONE)
632             {
633               parse_arg (context, group, &group->entries[j],
634                          NULL, NULL, error);
635               *parsed = TRUE;
636             }
637           else
638             {
639               gchar *value = NULL;
640               gchar *option_name;
641               
642               if (*new_index > index)
643                 {
644                   g_warning ("FIXME: figure out the correct error here");
645
646                   return FALSE;
647                 }
648               
649               if (index < *argc - 1)
650                 {
651                   value = (*argv)[index + 1];
652                   add_pending_null (context, &((*argv)[index + 1]), NULL);
653                   *new_index = index + 1;
654                 }
655               else
656                 value = "";
657
658
659               option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
660               
661               if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
662                 {
663                   g_free (option_name);
664                   return FALSE;
665                 }
666
667               g_free (option_name);
668               *parsed = TRUE;
669             }
670         }
671     }
672
673   return TRUE;
674 }
675
676 static gboolean
677 parse_long_option (GOptionContext *context,
678                    GOptionGroup   *group,
679                    gint           *index,
680                    gchar          *arg,
681                    gint           *argc,
682                    gchar        ***argv,
683                    GError        **error,
684                    gboolean       *parsed)
685 {
686   int j;
687
688   for (j = 0; j < group->n_entries; j++)
689     {
690       if (*index >= *argc)
691         return TRUE;
692
693       if (group->entries[j].arg == G_OPTION_ARG_NONE &&
694           strcmp (arg, group->entries[j].long_name) == 0)
695         {
696           parse_arg (context, group, &group->entries[j],
697                      NULL, NULL, error);
698           
699           add_pending_null (context, &((*argv)[*index]), NULL);
700         }
701       else
702         {
703           gint len = strlen (group->entries[j].long_name);
704           
705           if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
706               (arg[len] == '=' || arg[len] == 0))
707             {
708               gchar *value = NULL;
709               gchar *option_name;
710
711               add_pending_null (context, &((*argv)[*index + 1]), NULL);
712               
713               if (arg[len] == '=')
714                 value = arg + len + 1;
715               else if (*index < *argc - 1)
716                 {
717                   value = (*argv)[*index + 1];
718                   add_pending_null (context, &((*argv)[*index + 1]), NULL);
719                   (*index)++;
720                 }
721               else
722                 value = "";
723
724               option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
725               
726               if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
727                 {
728                   g_free (option_name);
729                   return FALSE;
730                 }
731
732               g_free (option_name);
733               *parsed = TRUE;
734             }
735         }
736     }
737   
738   return TRUE;
739 }
740
741 static void
742 free_changes_list (GOptionContext *context,
743                    gboolean        revert)
744 {
745   GList *list;
746
747   for (list = context->changes; list != NULL; list = list->next)
748     {
749       Change *change = list->data;
750
751       if (revert)
752         {
753           /* Free any allocated data */
754           g_free (change->allocated.str);
755           g_strfreev (change->allocated.array.data);
756           
757           switch (change->arg_type)
758             {
759             case G_OPTION_ARG_NONE:
760               *(gboolean *)change->arg_data = change->prev.bool;
761               break;
762             case G_OPTION_ARG_INT:
763               *(gint *)change->arg_data = change->prev.integer;
764               break;
765             case G_OPTION_ARG_STRING:
766             case G_OPTION_ARG_FILENAME:
767               *(gchar **)change->arg_data = change->prev.str;
768               break;
769             case G_OPTION_ARG_STRING_ARRAY:
770             case G_OPTION_ARG_FILENAME_ARRAY:
771               *(gchar ***)change->arg_data = change->prev.array;
772             default:
773               g_assert_not_reached ();
774             }
775         }
776       
777       g_free (change);
778     }
779
780   g_list_free (context->changes);
781   context->changes = NULL;
782 }
783
784 static void
785 free_pending_nulls (GOptionContext *context,
786                     gboolean        perform_nulls)
787 {
788   GList *list;
789
790   for (list = context->pending_nulls; list != NULL; list = list->next)
791     {
792       PendingNull *n = list->data;
793
794       if (perform_nulls)
795         {
796           if (n->value)
797             {
798               /* Copy back the short options */
799               *(n->ptr)[0] = '-';             
800               strcpy (*n->ptr + 1, n->value);
801             }
802           else
803             *n->ptr = NULL;
804         }
805       
806       g_free (n->value);
807       g_free (n);
808     }
809
810   g_list_free (context->pending_nulls);
811   context->pending_nulls = NULL;
812 }
813
814 gboolean
815 g_option_context_parse (GOptionContext   *context,
816                         gint             *argc,
817                         gchar          ***argv,
818                         GError          **error)
819 {
820   gint i, j, k;
821   GList *list;
822
823   /* Call pre-parse hooks */
824   list = context->groups;
825   while (list)
826     {
827       GOptionGroup *group = list->data;
828       
829       if (group->pre_parse_func)
830         {
831           if (!(* group->pre_parse_func) (context, group,
832                                           group->user_data, error))
833             goto fail;
834         }
835       
836       list = list->next;
837     }
838
839   if (context->main_group->pre_parse_func)
840     {
841       if (!(* context->main_group->pre_parse_func) (context, context->main_group,
842                                                     context->main_group->user_data, error))
843         goto fail;
844     }
845
846   if (argc && argv)
847     {
848       for (i = 1; i < *argc; i++)
849         {
850           gchar *arg;
851           gboolean parsed = FALSE;
852
853           if ((*argv)[i][0] == '-')
854             {
855               if ((*argv)[i][1] == '-')
856                 {
857                   /* -- option */
858
859                   arg = (*argv)[i] + 2;
860
861                   /* '--' terminates list of arguments */
862                   if (*arg == 0)
863                     {
864                       add_pending_null (context, &((*argv)[i]), NULL);
865                       break;
866                     }
867
868                   /* Handle help options */
869                   if (context->help_enabled)
870                     {
871                       if (strcmp (arg, "help") == 0)
872                         print_help (context, TRUE, NULL);
873                       else if (strcmp (arg, "help-all") == 0)
874                         print_help (context, FALSE, NULL);                    
875                       else if (strncmp (arg, "help-", 5) == 0)
876                         {
877                           GList *list;
878                           
879                           list = context->groups;
880                           
881                           while (list)
882                             {
883                               GOptionGroup *group = list->data;
884                               
885                               if (strcmp (arg + 5, group->name) == 0)
886                                 print_help (context, FALSE, group);                                           
887                               
888                               list = list->next;
889                             }
890                         }
891                     }
892
893                   if (!parse_long_option (context, context->main_group, &i, arg,
894                                           argc, argv, error, &parsed))
895                     goto fail;
896
897                   if (parsed)
898                     continue;
899                   
900                   /* Try the groups */
901                   list = context->groups;
902                   while (list)
903                     {
904                       GOptionGroup *group = list->data;
905                       
906                       if (!parse_long_option (context, group, &i, arg,
907                                               argc, argv, error, &parsed))
908                         goto fail;
909                       
910                       if (parsed)
911                         break;
912                       
913                       list = list->next;
914                     }
915
916                   if (context->ignore_unknown)
917                     continue;
918                 }
919               else
920                 {
921                   gint new_i, j;
922                   gboolean *nulled_out = NULL;
923                   
924                   arg = (*argv)[i] + 1;
925
926                   new_i = i;
927
928                   if (context->ignore_unknown)
929                     nulled_out = g_new0 (gboolean, strlen (arg));
930                   
931                   for (j = 0; j < strlen (arg); j++)
932                     {
933                       parsed = FALSE;
934                       
935                       if (!parse_short_option (context, context->main_group,
936                                                i, &new_i, arg[j],
937                                                argc, argv, error, &parsed))
938                         {
939
940                           g_free (nulled_out);
941                           goto fail;
942                         }
943
944                       if (!parsed)
945                         {
946                           /* Try the groups */
947                           list = context->groups;
948                           while (list)
949                             {
950                               GOptionGroup *group = list->data;
951                               
952                               if (!parse_short_option (context, group, i, &new_i, arg[j],
953                                                        argc, argv, error, &parsed))
954                                 goto fail;
955                               
956                               if (parsed)
957                                 break;
958                           
959                               list = list->next;
960                             }
961                         }
962
963                       if (context->ignore_unknown)
964                         {
965                           if (parsed)
966                             nulled_out[j] = TRUE;
967                           else
968                             continue;
969                         }
970
971                       if (!parsed)
972                         break;
973                     }
974
975                   if (context->ignore_unknown)
976                     {
977                       gchar *new_arg = NULL; 
978                       int arg_index = 0;
979                       
980                       for (j = 0; j < strlen (arg); j++)
981                         {
982                           if (!nulled_out[j])
983                             {
984                               if (!new_arg)
985                                 new_arg = g_malloc (strlen (arg));
986                               new_arg[arg_index++] = arg[j];
987                             }
988                         }
989                       if (new_arg)
990                         new_arg[arg_index] = '\0';
991                       
992                       add_pending_null (context, &((*argv)[i]), new_arg);
993                     }
994                   else if (parsed)
995                     {
996                       add_pending_null (context, &((*argv)[i]), NULL);
997                       i = new_i;
998                     }
999                 }
1000               
1001               if (!parsed && !context->ignore_unknown)
1002                 {
1003                   g_set_error (error,
1004                                G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1005                                _("Unknown option %s"), (*argv)[i]);
1006                   goto fail;
1007                 }
1008             }
1009         }
1010
1011       /* Call post-parse hooks */
1012       list = context->groups;
1013       while (list)
1014         {
1015           GOptionGroup *group = list->data;
1016
1017           if (group->post_parse_func)
1018             {
1019               if (!(* group->post_parse_func) (context, group,
1020                                                group->user_data, error))
1021                 goto fail;
1022             }
1023           
1024           list = list->next;
1025         }
1026
1027       if (context->main_group->post_parse_func)
1028         {
1029           if (!(* context->main_group->post_parse_func) (context, context->main_group,
1030                                                          context->main_group->user_data, error))
1031             goto fail;
1032         }
1033
1034       free_pending_nulls (context, TRUE);
1035       
1036       for (i = 1; i < *argc; i++)
1037         {
1038           for (k = i; k < *argc; k++)
1039             if ((*argv)[k] != NULL)
1040               break;
1041           
1042           if (k > i)
1043             {
1044               k -= i;
1045               for (j = i + k; j < *argc; j++)
1046                 (*argv)[j-k] = (*argv)[j];
1047               *argc -= k;
1048             }
1049         }
1050       
1051     }
1052
1053   return TRUE;
1054
1055  fail:
1056   
1057   /* Call error hooks */
1058   list = context->groups;
1059   while (list)
1060     {
1061       GOptionGroup *group = list->data;
1062       
1063       if (group->error_func)
1064         (* group->post_parse_func) (context, group,
1065                                     group->user_data, error);
1066       
1067       list = list->next;
1068     }
1069
1070   if (context->main_group->error_func)
1071     (* context->main_group->error_func) (context, context->main_group,
1072                                          context->main_group->user_data, error);
1073   
1074   free_changes_list (context, TRUE);
1075   free_pending_nulls (context, FALSE);
1076
1077   return FALSE;
1078 }
1079                                    
1080      
1081 GOptionGroup *
1082 g_option_group_new (const gchar    *name,
1083                     const gchar    *description,
1084                     const gchar    *help_description,
1085                     gpointer        user_data,
1086                     GDestroyNotify  destroy)
1087
1088 {
1089   GOptionGroup *group;
1090
1091   group = g_new0 (GOptionGroup, 1);
1092   group->name = g_strdup (name);
1093   group->description = g_strdup (description);
1094   group->help_description = g_strdup (help_description);
1095   group->user_data = user_data;
1096   group->destroy_notify = destroy;
1097   
1098   return group;
1099 }
1100
1101 void
1102 g_option_group_free (GOptionGroup *group)
1103 {
1104   g_return_if_fail (group != NULL);
1105
1106   g_free (group->name);
1107   g_free (group->description);
1108   g_free (group->help_description);
1109
1110   g_free (group->entries);
1111   
1112   if (group->destroy_notify)
1113     (* group->destroy_notify) (group->user_data);
1114
1115   if (group->translate_notify)
1116     (* group->translate_notify) (group->translate_data);
1117   
1118   g_free (group);
1119 }
1120
1121
1122 void
1123 g_option_group_add_entries (GOptionGroup       *group,
1124                             const GOptionEntry *entries)
1125 {
1126   int n_entries;
1127   int i;
1128   
1129   g_return_if_fail (entries != NULL);
1130
1131   for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1132
1133   group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1134
1135   memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1136
1137   group->n_entries += n_entries;
1138 }
1139
1140 void
1141 g_option_group_set_parse_hooks (GOptionGroup     *group,
1142                                 GOptionParseFunc  pre_parse_func,
1143                                 GOptionParseFunc  post_parse_func)
1144 {
1145   g_return_if_fail (group != NULL);
1146
1147   group->pre_parse_func = pre_parse_func;
1148   group->post_parse_func = post_parse_func;  
1149 }
1150
1151 void
1152 g_option_group_set_error_hook (GOptionGroup     *group,
1153                                GOptionErrorFunc  error_func)
1154 {
1155   g_return_if_fail (group != NULL);
1156
1157   group->error_func = error_func;  
1158 }
1159
1160
1161 void
1162 g_option_group_set_translate_func (GOptionGroup   *group,
1163                                    GTranslateFunc  func,
1164                                    gpointer        data,
1165                                    GDestroyNotify  notify)
1166 {
1167   g_return_if_fail (group != NULL);
1168   
1169   if (group->translate_notify)
1170     group->translate_notify (group->translate_data);
1171       
1172   group->translate_func = func;
1173   group->translate_data = data;
1174   group->translate_notify = notify;
1175 }
1176
1177 static gchar *
1178 dgettext_swapped (const gchar *msgid, 
1179                   const gchar *domainname)
1180 {
1181   return dgettext (domainname, msgid);
1182 }
1183
1184 void
1185 g_option_group_set_translation_domain (GOptionGroup *group,
1186                                        const gchar  *domain)
1187 {
1188   g_return_if_fail (group != NULL);
1189
1190   g_option_group_set_translate_func (group, 
1191                                      (GTranslateFunc)dgettext_swapped,
1192                                      g_strdup (domain),
1193                                      g_free);
1194
1195