88{
90
91
92 int has_arg;
93 int *flag;
94 int val;
95};
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
119
120
121
122
123
124
125
126
127
128
129
130
131
132
134
135
136
137
138
139
140
141
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
175
176
178
179#if defined(__GNU_LIBRARY__) || defined(__GLIBC__) || defined(DARWIN)
180
181
182
183
184#include <string.h>
185#define my_index strchr
186#else
187
188
189
190
191char *
193
194static char *
196{
197 while (*str)
198 {
199 if (*str == chr)
200 return (char *) str;
201 str++;
202 }
203 return 0;
204}
205
206
207
208
209#ifdef __GNUC__
210
211
212#if ! defined(__STDC__) || ! __STDC__
213
214
215extern int
216strlen (const char *);
217
218#endif
219#endif
220
221#endif
222
223
224
225
226
227
228
231
232#define SWAP_FLAGS(ch1, ch2)
233
234
235
236
237
238
239
240
241
242
243#if defined(__STDC__) && __STDC__
244static void
246
247#endif
248
249static void
251{
255 char *tem;
256
257
258
259
260
261
262 while (top > middle && middle > bottom)
263 {
264 if (top - middle > middle - bottom)
265 {
266
267 int len = middle - bottom;
268 register int i;
269
270
271 for (i = 0; i < len; i++)
272 {
273 tem = argv[bottom + i];
274 argv[bottom + i] = argv[top - (middle - bottom) + i];
275 argv[top - (middle - bottom) + i] = tem;
276 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
277 }
278
279 top -= len;
280 }
281 else
282 {
283
284 int len = top - middle;
285 register int i;
286
287
288 for (i = 0; i < len; i++)
289 {
290 tem = argv[bottom + i];
291 argv[bottom + i] = argv[middle + i];
292 argv[middle + i] = tem;
294 }
295
296 bottom += len;
297 }
298 }
299
300
301
304}
305
306
307
308
309#if defined(__STDC__) && __STDC__
310static const char *
312
313#endif
314static const char *
316{
317
318
319
320
322
324
326
327
328
329 if (optstring[0] == '-')
330 {
332 ++optstring;
333 }
334 else if (optstring[0] == '+')
335 {
337 ++optstring;
338 }
341 else
343
344 return optstring;
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
404static int
406 char *const *argv,
407 const char *optstring,
409 int *longind,
410 int long_only)
411{
412 static int __getopt_initialized = 0;
413 static int GNopterr = 1;
414
416
417 if ((
GNoptind == 0) || ! __getopt_initialized)
418 {
422 __getopt_initialized = 1;
423 }
424
425
426
427
428
429#define NONOPTION_P (argv[GNoptind][0] != '-' || argv[GNoptind][1] == '\0')
430
432 {
433
434
435
436
441
443 {
444
445
446
451
452
453
454
458 }
459
460
461
462
463
465 {
467
473
475 }
476
477
478
479
481 {
482
483
486 return -1;
487 }
488
489
490
491
493 {
495 return -1;
497 return 1;
498 }
499
500
501
502
505 }
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520if ((longopts != NULL) &&
522 (long_only &&
524 {
525 char *nameend;
527 const struct GNoption *pfound = NULL;
528 int exact = 0;
529 int ambig = 0;
530 int indfound = -1;
531 int option_index;
532
533 for (nameend =
nextchar; *nameend && *nameend !=
'='; nameend++)
534 ;
535
536
537
538 for (
p = longopts, option_index = 0;
p->name;
p++, option_index++)
540 {
541 if ((
unsigned int) (nameend -
nextchar) ==
542 (
unsigned int) strlen (
p->name))
543 {
544
546 indfound = option_index;
547 exact = 1;
548 break;
549 }
550 else if (pfound == NULL)
551 {
552
554 indfound = option_index;
555 }
556 else
557
558 ambig = 1;
559 }
560
561 if (ambig && ! exact)
562 {
563 if (GNopterr)
564 fprintf (stderr,
565 _ (
"%s: option `%s' is ambiguous\n"),
566 argv[0],
570 return '?';
571 }
572
573 if (pfound != NULL)
574 {
575 option_index = indfound;
577 if (*nameend)
578 {
579
580
583 else
584 {
585 if (GNopterr)
586 {
588
589 fprintf (stderr,
590 _ (
"%s: option `--%s' does not allow an argument\n"),
591 argv[0],
593 else
594
595 fprintf (stderr,
596 _ (
"%s: option `%c%s' does not allow an argument\n"),
597 argv[0],
600 }
602 return '?';
603 }
604 }
606 {
608 {
610 }
611 else
612 {
613 if (GNopterr)
614 {
615 fprintf (stderr,
616 _ (
"%s: option `%s' requires an argument\n"),
617 argv[0],
619 }
621 return (optstring[0] == ':') ? ':' : '?';
622 }
623 }
625 if (longind != NULL)
626 *longind = option_index;
628 {
630 return 0;
631 }
633 }
634
635
636
637
638
639 if (! long_only || (argv[
GNoptind][1] ==
'-') ||
641 {
642 if (GNopterr)
643 {
645
646 fprintf (stderr,
647 _ (
"%s: unrecognized option `--%s'\n"),
648 argv[0],
650 else
651
652 fprintf (stderr,
653 _ (
"%s: unrecognized option `%c%s'\n"),
654 argv[0],
657 }
660 return '?';
661 }
662 }
663
664
665
666 {
668 char *temp =
my_index (optstring, c);
669
670
673
674 if ((temp == NULL) || (c == ':'))
675 {
676 if (GNopterr)
677 {
679
680 fprintf (stderr,
_ (
"%s: illegal option -- %c\n"), argv[0], c);
681 else
682 fprintf (stderr,
_ (
"%s: invalid option -- %c\n"), argv[0], c);
683 }
684 return '?';
685 }
686
687 if ((temp[0] == 'W') && (temp[1] == ';'))
688 {
689 char *nameend;
691 const struct GNoption *pfound = NULL;
692 int exact = 0;
693 int ambig = 0;
694 int indfound = 0;
695 int option_index;
696
697
699 {
701
702
704 }
706 {
707 if (GNopterr)
708 {
709
710 fprintf (stderr,
711 _ (
"%s: option requires an argument -- %c\n"),
712 argv[0],
713 c);
714 }
715 if (optstring[0] == ':')
716 c = ':';
717 else
718 c = '?';
719 return c;
720 }
721 else
722
723
725
726
727
728
730 nameend++)
731 ;
732
733
734
735 if (longopts != NULL)
736 for (
p = longopts, option_index = 0;
p->name;
p++, option_index++)
738 {
739 if ((
unsigned int) (nameend -
nextchar) == strlen (
p->name))
740 {
741
743 indfound = option_index;
744 exact = 1;
745 break;
746 }
747 else if (pfound == NULL)
748 {
749
751 indfound = option_index;
752 }
753 else
754
755 ambig = 1;
756 }
757 if (ambig && ! exact)
758 {
759 if (GNopterr)
760 fprintf (stderr,
761 _ (
"%s: option `-W %s' is ambiguous\n"),
762 argv[0],
766 return '?';
767 }
768 if (pfound != NULL)
769 {
770 option_index = indfound;
771 if (*nameend)
772 {
773
774
777 else
778 {
779 if (GNopterr)
780 fprintf (stderr,
781 _ (
"%s: option `-W %s' does not allow an argument\n"),
782 argv[0],
784
786 return '?';
787 }
788 }
790 {
793 else
794 {
795 if (GNopterr)
796 fprintf (stderr,
797 _ (
"%s: option `%s' requires an argument\n"),
798 argv[0],
801 return optstring[0] == ':' ? ':' : '?';
802 }
803 }
805 if (longind != NULL)
806 *longind = option_index;
808 {
810 return 0;
811 }
813 }
815 return 'W';
816 }
817 if (temp[1] == ':')
818 {
819 if (temp[2] == ':')
820 {
821
823 {
826 }
827 else
830 }
831 else
832 {
833
835 {
837
838
840 }
842 {
843 if (GNopterr)
844 {
845
846 fprintf (stderr,
847 _ (
"%s: option requires an argument -- %c\n"),
848 argv[0],
849 c);
850 }
851 if (optstring[0] == ':')
852 c = ':';
853 else
854 c = '?';
855 }
856 else
857
858
861 }
862 }
863 return c;
864 }
865}
866
867
868static int
870 char *const *argv,
872 const struct GNoption *long_options,
873 int *opt_index)
874{
876}
877
878
879
880
881
882int
885 unsigned int argc,
886 char *const *argv)
887{
890 int count;
891 char *shorts;
892 int spos;
894 uint8_t *seen;
895 unsigned int optmatch = 0;
896 const char *have_exclusive = NULL;
897
900 clpc.binaryName =
argv[0];
906 ;
907
908
909
913 spos = 0;
914 for (unsigned i = 0; i < count; i++)
915 {
918 long_options[i].
flag = NULL;
922 shorts[spos++] = ':';
923 }
924 long_options[count].
name = NULL;
925 long_options[count].
has_arg = 0;
926 long_options[count].
flag = NULL;
927 long_options[count].
val =
'\0';
928 shorts[spos] = '\0';
930
931
932 while (1)
933 {
934 int option_index = 0;
935 unsigned int i;
936 int c;
937
940 shorts,
941 long_options,
942 &option_index);
944 break;
945
946
947 for (i = 0; i < count; i++)
948 {
949 clpc.currentArgument =
GNoptind - 1;
951 {
952 optmatch++;
956 {
957
962 }
963 seen[i] = 1;
964 break;
965 }
966 }
967 if (i == count)
968 {
969 fprintf (stderr,
970 _ (
"Use %s to get a list of options.\n"),
971 "--help");
973 }
974 }
977
978
979
980 if ((NULL != have_exclusive) && (optmatch > 1))
981 {
982 fprintf (stderr,
983 _ (
"Option `%s' can't be used with other options.\n"),
984 have_exclusive);
986 }
988 {
989
991 {
992 if ((0 == seen[count]) && (
allOptions[count].option_mandatory))
993 {
994 fprintf (stderr,
995 _ (
"Missing mandatory option `%s'.\n"),
998 }
999 }
1000 }
1002
1003
1007
1009 return cont;
1011}
1012
1013
1014
struct GNUNET_GETOPT_CommandLineOption options[]
static int GNgetopt_long(int argc, char *const *argv, const char *options, const struct GNoption *long_options, int *opt_index)
static void exchange(char **argv)
static const char * _getopt_initialize(int argc, char *const *argv, const char *optstring)
static char * my_index(const char *str, int chr)
static int GN_getopt_internal(int argc, char *const *argv, const char *optstring, const struct GNoption *longopts, int *longind, int long_only)
#define SWAP_FLAGS(ch1, ch2)
static char * posixly_correct
static char * name
Name (label) of the records to list.
static struct GNUNET_OS_Process * p
Helper process we started.
int GNUNET_GETOPT_run(const char *binaryOptions, const struct GNUNET_GETOPT_CommandLineOption *allOptions, unsigned int argc, char *const *argv)
Parse the command line.
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
Definition of a command line option.
int require_argument
Is an argument required? GNUNET_NO (includes optional) or GNUNET_YES (required)
const char * name
Long name of the option (may not be NULL)
GNUNET_GETOPT_CommandLineOptionProcessor processor
Handler for the option.
const char shortName
Short name of the option.
void(* cleaner)(void *cls)
Function to call on scls to clean up after processing all the arguments.
General context for command line processors.
const char * binaryOptions
Name of application with option summary.
const struct GNUNET_GETOPT_CommandLineOption * allOptions
Array with all command line options.
char *const * argv
Original command line.
unsigned int argc
Total number of argv's.