This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
/
goption.c
2235 lines (1883 loc) · 54.2 KB
/
goption.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* goption.c - Option parser
*
* Copyright (C) 1999, 2003 Red Hat Software
* Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "goption.h"
#include "glib.h"
#include "glibintl.h"
#include "gprintf.h"
#include "galias.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
#define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
((entry)->arg == G_OPTION_ARG_CALLBACK && \
((entry)->flags & G_OPTION_FLAG_NO_ARG)))
#define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
(entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
typedef struct
{
GOptionArg arg_type;
gpointer arg_data;
union
{
gboolean bool;
gint integer;
gchar *str;
gchar **array;
gdouble dbl;
gint64 int64;
} prev;
union
{
gchar *str;
struct
{
gint len;
gchar **data;
} array;
} allocated;
} Change;
typedef struct
{
gchar **ptr;
gchar *value;
} PendingNull;
struct _GOptionContext
{
GList *groups;
gchar *parameter_string;
gchar *summary;
gchar *description;
GTranslateFunc translate_func;
GDestroyNotify translate_notify;
gpointer translate_data;
guint help_enabled : 1;
guint ignore_unknown : 1;
GOptionGroup *main_group;
/* We keep a list of change so we can revert them */
GList *changes;
/* We also keep track of all argv elements
* that should be NULLed or modified.
*/
GList *pending_nulls;
};
struct _GOptionGroup
{
gchar *name;
gchar *description;
gchar *help_description;
GDestroyNotify destroy_notify;
gpointer user_data;
GTranslateFunc translate_func;
GDestroyNotify translate_notify;
gpointer translate_data;
GOptionEntry *entries;
gint n_entries;
GOptionParseFunc pre_parse_func;
GOptionParseFunc post_parse_func;
GOptionErrorFunc error_func;
};
static void free_changes_list (GOptionContext *context,
gboolean revert);
static void free_pending_nulls (GOptionContext *context,
gboolean perform_nulls);
static int
_g_unichar_get_width (gunichar c)
{
if (G_UNLIKELY (g_unichar_iszerowidth (c)))
return 0;
/* we ignore the fact that we should call g_unichar_iswide_cjk() under
* some locales (legacy East Asian ones) */
if (g_unichar_iswide (c))
return 2;
return 1;
}
static glong
_g_utf8_strwidth (const gchar *p,
gssize max)
{
glong len = 0;
const gchar *start = p;
g_return_val_if_fail (p != NULL || max == 0, 0);
if (max < 0)
{
while (*p)
{
len += _g_unichar_get_width (g_utf8_get_char (p));
p = g_utf8_next_char (p);
}
}
else
{
if (max == 0 || !*p)
return 0;
/* this case may not be quite correct */
len += _g_unichar_get_width (g_utf8_get_char (p));
p = g_utf8_next_char (p);
while (p - start < max && *p)
{
len += _g_unichar_get_width (g_utf8_get_char (p));
p = g_utf8_next_char (p);
}
}
return len;
}
GQuark
g_option_error_quark (void)
{
return g_quark_from_static_string ("g-option-context-error-quark");
}
/**
* g_option_context_new:
* @parameter_string: a string which is displayed in
* the first line of <option>--help</option> output, after the
* usage summary
* <literal><replaceable>programname</replaceable> [OPTION...]</literal>
*
* Creates a new option context.
*
* The @parameter_string can serve multiple purposes. It can be used
* to add descriptions for "rest" arguments, which are not parsed by
* the #GOptionContext, typically something like "FILES" or
* "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
* collecting "rest" arguments, GLib handles this automatically by
* using the @arg_description of the corresponding #GOptionEntry in
* the usage summary.
*
* Another usage is to give a short summary of the program
* functionality, like " - frob the strings", which will be displayed
* in the same line as the usage. For a longer description of the
* program functionality that should be displayed as a paragraph
* below the usage line, use g_option_context_set_summary().
*
* Note that the @parameter_string is translated using the
* function set with g_option_context_set_translate_func(), so
* it should normally be passed untranslated.
*
* Returns: a newly created #GOptionContext, which must be
* freed with g_option_context_free() after use.
*
* Since: 2.6
*/
GOptionContext *
g_option_context_new (const gchar *parameter_string)
{
GOptionContext *context;
context = g_new0 (GOptionContext, 1);
context->parameter_string = g_strdup (parameter_string);
context->help_enabled = TRUE;
context->ignore_unknown = FALSE;
return context;
}
/**
* g_option_context_free:
* @context: a #GOptionContext
*
* Frees context and all the groups which have been
* added to it.
*
* Since: 2.6
*/
void g_option_context_free (GOptionContext *context)
{
g_return_if_fail (context != NULL);
g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
g_list_free (context->groups);
if (context->main_group)
g_option_group_free (context->main_group);
free_changes_list (context, FALSE);
free_pending_nulls (context, FALSE);
g_free (context->parameter_string);
g_free (context->summary);
g_free (context->description);
if (context->translate_notify)
(* context->translate_notify) (context->translate_data);
g_free (context);
}
/**
* g_option_context_set_help_enabled:
* @context: a #GOptionContext
* @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
*
* Enables or disables automatic generation of <option>--help</option>
* output. By default, g_option_context_parse() recognizes
* <option>--help</option>, <option>-?</option>, <option>--help-all</option>
* and <option>--help-</option><replaceable>groupname</replaceable> and creates
* suitable output to stdout.
*
* Since: 2.6
*/
void g_option_context_set_help_enabled (GOptionContext *context,
gboolean help_enabled)
{
g_return_if_fail (context != NULL);
context->help_enabled = help_enabled;
}
/**
* g_option_context_get_help_enabled:
* @context: a #GOptionContext
*
* Returns whether automatic <option>--help</option> generation
* is turned on for @context. See g_option_context_set_help_enabled().
*
* Returns: %TRUE if automatic help generation is turned on.
*
* Since: 2.6
*/
gboolean
g_option_context_get_help_enabled (GOptionContext *context)
{
g_return_val_if_fail (context != NULL, FALSE);
return context->help_enabled;
}
/**
* g_option_context_set_ignore_unknown_options:
* @context: a #GOptionContext
* @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
* an error when unknown options are met
*
* Sets whether to ignore unknown options or not. If an argument is
* ignored, it is left in the @argv array after parsing. By default,
* g_option_context_parse() treats unknown options as error.
*
* This setting does not affect non-option arguments (i.e. arguments
* which don't start with a dash). But note that GOption cannot reliably
* determine whether a non-option belongs to a preceding unknown option.
*
* Since: 2.6
**/
void
g_option_context_set_ignore_unknown_options (GOptionContext *context,
gboolean ignore_unknown)
{
g_return_if_fail (context != NULL);
context->ignore_unknown = ignore_unknown;
}
/**
* g_option_context_get_ignore_unknown_options:
* @context: a #GOptionContext
*
* Returns whether unknown options are ignored or not. See
* g_option_context_set_ignore_unknown_options().
*
* Returns: %TRUE if unknown options are ignored.
*
* Since: 2.6
**/
gboolean
g_option_context_get_ignore_unknown_options (GOptionContext *context)
{
g_return_val_if_fail (context != NULL, FALSE);
return context->ignore_unknown;
}
/**
* g_option_context_add_group:
* @context: a #GOptionContext
* @group: the group to add
*
* Adds a #GOptionGroup to the @context, so that parsing with @context
* will recognize the options in the group. Note that the group will
* be freed together with the context when g_option_context_free() is
* called, so you must not free the group yourself after adding it
* to a context.
*
* Since: 2.6
**/
void
g_option_context_add_group (GOptionContext *context,
GOptionGroup *group)
{
GList *list;
g_return_if_fail (context != NULL);
g_return_if_fail (group != NULL);
g_return_if_fail (group->name != NULL);
g_return_if_fail (group->description != NULL);
g_return_if_fail (group->help_description != NULL);
for (list = context->groups; list; list = list->next)
{
GOptionGroup *g = (GOptionGroup *)list->data;
if ((group->name == NULL && g->name == NULL) ||
(group->name && g->name && strcmp (group->name, g->name) == 0))
g_warning ("A group named \"%s\" is already part of this GOptionContext",
group->name);
}
context->groups = g_list_append (context->groups, group);
}
/**
* g_option_context_set_main_group:
* @context: a #GOptionContext
* @group: the group to set as main group
*
* Sets a #GOptionGroup as main group of the @context.
* This has the same effect as calling g_option_context_add_group(),
* the only difference is that the options in the main group are
* treated differently when generating <option>--help</option> output.
*
* Since: 2.6
**/
void
g_option_context_set_main_group (GOptionContext *context,
GOptionGroup *group)
{
g_return_if_fail (context != NULL);
g_return_if_fail (group != NULL);
if (context->main_group)
{
g_warning ("This GOptionContext already has a main group");
return;
}
context->main_group = group;
}
/**
* g_option_context_get_main_group:
* @context: a #GOptionContext
*
* Returns a pointer to the main group of @context.
*
* Return value: the main group of @context, or %NULL if @context doesn't
* have a main group. Note that group belongs to @context and should
* not be modified or freed.
*
* Since: 2.6
**/
GOptionGroup *
g_option_context_get_main_group (GOptionContext *context)
{
g_return_val_if_fail (context != NULL, NULL);
return context->main_group;
}
/**
* g_option_context_add_main_entries:
* @context: a #GOptionContext
* @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
* @translation_domain: a translation domain to use for translating
* the <option>--help</option> output for the options in @entries
* with gettext(), or %NULL
*
* A convenience function which creates a main group if it doesn't
* exist, adds the @entries to it and sets the translation domain.
*
* Since: 2.6
**/
void
g_option_context_add_main_entries (GOptionContext *context,
const GOptionEntry *entries,
const gchar *translation_domain)
{
g_return_if_fail (entries != NULL);
if (!context->main_group)
context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
g_option_group_add_entries (context->main_group, entries);
g_option_group_set_translation_domain (context->main_group, translation_domain);
}
static gint
calculate_max_length (GOptionGroup *group)
{
GOptionEntry *entry;
gint i, len, max_length;
max_length = 0;
for (i = 0; i < group->n_entries; i++)
{
entry = &group->entries[i];
if (entry->flags & G_OPTION_FLAG_HIDDEN)
continue;
len = _g_utf8_strwidth (entry->long_name, -1);
if (entry->short_name)
len += 4;
if (!NO_ARG (entry) && entry->arg_description)
len += 1 + _g_utf8_strwidth (TRANSLATE (group, entry->arg_description), -1);
max_length = MAX (max_length, len);
}
return max_length;
}
static void
print_entry (GOptionGroup *group,
gint max_length,
const GOptionEntry *entry,
GString *string)
{
GString *str;
if (entry->flags & G_OPTION_FLAG_HIDDEN)
return;
if (entry->long_name[0] == 0)
return;
str = g_string_new (NULL);
if (entry->short_name)
g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
else
g_string_append_printf (str, " --%s", entry->long_name);
if (entry->arg_description)
g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
g_string_append_printf (string, "%s%*s %s\n", str->str,
(int) (max_length + 4 - _g_utf8_strwidth (str->str, -1)), "",
entry->description ? TRANSLATE (group, entry->description) : "");
g_string_free (str, TRUE);
}
static gboolean
group_has_visible_entries (GOptionContext *context,
GOptionGroup *group,
gboolean main_entries)
{
GOptionFlags reject_filter = G_OPTION_FLAG_HIDDEN;
GOptionEntry *entry;
gint i, l;
gboolean main_group = group == context->main_group;
if (!main_entries)
reject_filter |= G_OPTION_FLAG_IN_MAIN;
for (i = 0, l = (group ? group->n_entries : 0); i < l; i++)
{
entry = &group->entries[i];
if (main_entries && !main_group && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
continue;
if (!(entry->flags & reject_filter))
return TRUE;
}
return FALSE;
}
static gboolean
group_list_has_visible_entires (GOptionContext *context,
GList *group_list,
gboolean main_entries)
{
while (group_list)
{
if (group_has_visible_entries (context, group_list->data, main_entries))
return TRUE;
group_list = group_list->next;
}
return FALSE;
}
/**
* g_option_context_get_help:
* @context: a #GOptionContext
* @main_help: if %TRUE, only include the main group
* @group: the #GOptionGroup to create help for, or %NULL
*
* Returns a formatted, translated help text for the given context.
* To obtain the text produced by <option>--help</option>, call
* <literal>g_option_context_get_help (context, TRUE, NULL)</literal>.
* To obtain the text produced by <option>--help-all</option>, call
* <literal>g_option_context_get_help (context, FALSE, NULL)</literal>.
* To obtain the help text for an option group, call
* <literal>g_option_context_get_help (context, FALSE, group)</literal>.
*
* Returns: A newly allocated string containing the help text
*
* Since: 2.14
*/
gchar *
g_option_context_get_help (GOptionContext *context,
gboolean main_help,
GOptionGroup *group)
{
GList *list;
gint max_length, len;
gint i;
GOptionEntry *entry;
GHashTable *shadow_map;
gboolean seen[256];
const gchar *rest_description;
GString *string;
string = g_string_sized_new (1024);
rest_description = NULL;
if (context->main_group)
{
for (i = 0; i < context->main_group->n_entries; i++)
{
entry = &context->main_group->entries[i];
if (entry->long_name[0] == 0)
{
rest_description = TRANSLATE (context->main_group, entry->arg_description);
break;
}
}
}
g_string_append_printf (string, "%s\n %s %s",
_("Usage:"), g_get_prgname(), _("[OPTION...]"));
if (rest_description)
{
g_string_append (string, " ");
g_string_append (string, rest_description);
}
if (context->parameter_string)
{
g_string_append (string, " ");
g_string_append (string, TRANSLATE (context, context->parameter_string));
}
g_string_append (string, "\n\n");
if (context->summary)
{
g_string_append (string, TRANSLATE (context, context->summary));
g_string_append (string, "\n\n");
}
memset (seen, 0, sizeof (gboolean) * 256);
shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
if (context->main_group)
{
for (i = 0; i < context->main_group->n_entries; i++)
{
entry = &context->main_group->entries[i];
g_hash_table_insert (shadow_map,
(gpointer)entry->long_name,
entry);
if (seen[(guchar)entry->short_name])
entry->short_name = 0;
else
seen[(guchar)entry->short_name] = TRUE;
}
}
list = context->groups;
while (list != NULL)
{
GOptionGroup *group = list->data;
for (i = 0; i < group->n_entries; i++)
{
entry = &group->entries[i];
if (g_hash_table_lookup (shadow_map, entry->long_name) &&
!(entry->flags & G_OPTION_FLAG_NOALIAS))
entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
else
g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
if (seen[(guchar)entry->short_name] &&
!(entry->flags & G_OPTION_FLAG_NOALIAS))
entry->short_name = 0;
else
seen[(guchar)entry->short_name] = TRUE;
}
list = list->next;
}
g_hash_table_destroy (shadow_map);
list = context->groups;
max_length = _g_utf8_strwidth ("-?, --help", -1);
if (list)
{
len = _g_utf8_strwidth ("--help-all", -1);
max_length = MAX (max_length, len);
}
if (context->main_group)
{
len = calculate_max_length (context->main_group);
max_length = MAX (max_length, len);
}
while (list != NULL)
{
GOptionGroup *group = list->data;
/* First, we check the --help-<groupname> options */
len = _g_utf8_strwidth ("--help-", -1) + _g_utf8_strwidth (group->name, -1);
max_length = MAX (max_length, len);
/* Then we go through the entries */
len = calculate_max_length (group);
max_length = MAX (max_length, len);
list = list->next;
}
/* Add a bit of padding */
max_length += 4;
if (!group)
{
list = context->groups;
g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
_("Help Options:"), '?', max_length - 4, "help",
_("Show help options"));
/* We only want --help-all when there are groups */
if (list)
g_string_append_printf (string, " --%-*s %s\n",
max_length, "help-all",
_("Show all help options"));
while (list)
{
GOptionGroup *group = list->data;
if (group_has_visible_entries (context, group, FALSE))
g_string_append_printf (string, " --help-%-*s %s\n",
max_length - 5, group->name,
TRANSLATE (group, group->help_description));
list = list->next;
}
g_string_append (string, "\n");
}
if (group)
{
/* Print a certain group */
if (group_has_visible_entries (context, group, FALSE))
{
g_string_append (string, TRANSLATE (group, group->description));
g_string_append (string, "\n");
for (i = 0; i < group->n_entries; i++)
print_entry (group, max_length, &group->entries[i], string);
g_string_append (string, "\n");
}
}
else if (!main_help)
{
/* Print all groups */
list = context->groups;
while (list)
{
GOptionGroup *group = list->data;
if (group_has_visible_entries (context, group, FALSE))
{
g_string_append (string, group->description);
g_string_append (string, "\n");
for (i = 0; i < group->n_entries; i++)
if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
print_entry (group, max_length, &group->entries[i], string);
g_string_append (string, "\n");
}
list = list->next;
}
}
/* Print application options if --help or --help-all has been specified */
if ((main_help || !group) &&
(group_has_visible_entries (context, context->main_group, TRUE) ||
group_list_has_visible_entires (context, context->groups, TRUE)))
{
list = context->groups;
g_string_append (string, _("Application Options:"));
g_string_append (string, "\n");
if (context->main_group)
for (i = 0; i < context->main_group->n_entries; i++)
print_entry (context->main_group, max_length,
&context->main_group->entries[i], string);
while (list != NULL)
{
GOptionGroup *group = list->data;
/* Print main entries from other groups */
for (i = 0; i < group->n_entries; i++)
if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
print_entry (group, max_length, &group->entries[i], string);
list = list->next;
}
g_string_append (string, "\n");
}
if (context->description)
{
g_string_append (string, TRANSLATE (context, context->description));
g_string_append (string, "\n");
}
return g_string_free (string, FALSE);
}
static void
print_help (GOptionContext *context,
gboolean main_help,
GOptionGroup *group)
{
gchar *help;
help = g_option_context_get_help (context, main_help, group);
g_print ("%s", help);
g_free (help);
exit (0);
}
static gboolean
parse_int (const gchar *arg_name,
const gchar *arg,
gint *result,
GError **error)
{
gchar *end;
glong tmp;
errno = 0;
tmp = strtol (arg, &end, 0);
if (*arg == '\0' || *end != '\0')
{
g_set_error (error,
G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
_("Cannot parse integer value '%s' for %s"),
arg, arg_name);
return FALSE;
}
*result = tmp;
if (*result != tmp || errno == ERANGE)
{
g_set_error (error,
G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
_("Integer value '%s' for %s out of range"),
arg, arg_name);
return FALSE;
}
return TRUE;
}
static gboolean
parse_double (const gchar *arg_name,
const gchar *arg,
gdouble *result,
GError **error)
{
gchar *end;
gdouble tmp;
errno = 0;
tmp = g_strtod (arg, &end);
if (*arg == '\0' || *end != '\0')
{
g_set_error (error,
G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
_("Cannot parse double value '%s' for %s"),
arg, arg_name);
return FALSE;
}
if (errno == ERANGE)
{
g_set_error (error,
G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
_("Double value '%s' for %s out of range"),
arg, arg_name);
return FALSE;
}
*result = tmp;
return TRUE;
}
static gboolean
parse_int64 (const gchar *arg_name,
const gchar *arg,
gint64 *result,
GError **error)
{
gchar *end;
gint64 tmp;
errno = 0;
tmp = g_ascii_strtoll (arg, &end, 0);
if (*arg == '\0' || *end != '\0')
{
g_set_error (error,
G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
_("Cannot parse integer value '%s' for %s"),
arg, arg_name);
return FALSE;
}
if (errno == ERANGE)
{
g_set_error (error,
G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
_("Integer value '%s' for %s out of range"),
arg, arg_name);
return FALSE;
}
*result = tmp;
return TRUE;
}
static Change *
get_change (GOptionContext *context,
GOptionArg arg_type,
gpointer arg_data)
{
GList *list;
Change *change = NULL;
for (list = context->changes; list != NULL; list = list->next)
{
change = list->data;
if (change->arg_data == arg_data)
goto found;
}
change = g_new0 (Change, 1);
change->arg_type = arg_type;
change->arg_data = arg_data;
context->changes = g_list_prepend (context->changes, change);
found:
return change;
}
static void
add_pending_null (GOptionContext *context,
gchar **ptr,
gchar *value)
{
PendingNull *n;
n = g_new0 (PendingNull, 1);
n->ptr = ptr;
n->value = value;
context->pending_nulls = g_list_prepend (context->pending_nulls, n);
}
static gboolean
parse_arg (GOptionContext *context,
GOptionGroup *group,
GOptionEntry *entry,
const gchar *value,
const gchar *option_name,
GError **error)
{
Change *change;
g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
switch (entry->arg)
{
case G_OPTION_ARG_NONE:
{
change = get_change (context, G_OPTION_ARG_NONE,
entry->arg_data);
*(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
break;