195b7b453SJohn Marino /* getopt_long and getopt_long_only entry points for GNU getopt.
2*09d4459fSDaniel Fojt Copyright (C) 1987-2020 Free Software Foundation, Inc.
3*09d4459fSDaniel Fojt This file is part of the GNU C Library and is also part of gnulib.
4*09d4459fSDaniel Fojt Patches to this file should be submitted to both projects.
595b7b453SJohn Marino
6*09d4459fSDaniel Fojt The GNU C Library is free software; you can redistribute it and/or
7*09d4459fSDaniel Fojt modify it under the terms of the GNU General Public
8*09d4459fSDaniel Fojt License as published by the Free Software Foundation; either
9*09d4459fSDaniel Fojt version 3 of the License, or (at your option) any later version.
1095b7b453SJohn Marino
11*09d4459fSDaniel Fojt The GNU C Library is distributed in the hope that it will be useful,
1295b7b453SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
13*09d4459fSDaniel Fojt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14*09d4459fSDaniel Fojt General Public License for more details.
1595b7b453SJohn Marino
16*09d4459fSDaniel Fojt You should have received a copy of the GNU General Public
17*09d4459fSDaniel Fojt License along with the GNU C Library; if not, see
18*09d4459fSDaniel Fojt <https://www.gnu.org/licenses/>. */
1995b7b453SJohn Marino
20*09d4459fSDaniel Fojt #ifndef _LIBC
2195b7b453SJohn Marino # include <config.h>
22*09d4459fSDaniel Fojt #endif
23*09d4459fSDaniel Fojt
2495b7b453SJohn Marino #include "getopt.h"
2595b7b453SJohn Marino #include "getopt_int.h"
2695b7b453SJohn Marino
2795b7b453SJohn Marino int
getopt_long(int argc,char * __getopt_argv_const * argv,const char * options,const struct option * long_options,int * opt_index)2895b7b453SJohn Marino getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
2995b7b453SJohn Marino const struct option *long_options, int *opt_index)
3095b7b453SJohn Marino {
3195b7b453SJohn Marino return _getopt_internal (argc, (char **) argv, options, long_options,
3295b7b453SJohn Marino opt_index, 0, 0);
3395b7b453SJohn Marino }
3495b7b453SJohn Marino
3595b7b453SJohn Marino int
_getopt_long_r(int argc,char ** argv,const char * options,const struct option * long_options,int * opt_index,struct _getopt_data * d)3695b7b453SJohn Marino _getopt_long_r (int argc, char **argv, const char *options,
3795b7b453SJohn Marino const struct option *long_options, int *opt_index,
3895b7b453SJohn Marino struct _getopt_data *d)
3995b7b453SJohn Marino {
4095b7b453SJohn Marino return _getopt_internal_r (argc, argv, options, long_options, opt_index,
4195b7b453SJohn Marino 0, d, 0);
4295b7b453SJohn Marino }
4395b7b453SJohn Marino
4495b7b453SJohn Marino /* Like getopt_long, but '-' as well as '--' can indicate a long option.
4595b7b453SJohn Marino If an option that starts with '-' (not '--') doesn't match a long option,
4695b7b453SJohn Marino but does match a short option, it is parsed as a short option
4795b7b453SJohn Marino instead. */
4895b7b453SJohn Marino
4995b7b453SJohn Marino int
getopt_long_only(int argc,char * __getopt_argv_const * argv,const char * options,const struct option * long_options,int * opt_index)5095b7b453SJohn Marino getopt_long_only (int argc, char *__getopt_argv_const *argv,
5195b7b453SJohn Marino const char *options,
5295b7b453SJohn Marino const struct option *long_options, int *opt_index)
5395b7b453SJohn Marino {
5495b7b453SJohn Marino return _getopt_internal (argc, (char **) argv, options, long_options,
5595b7b453SJohn Marino opt_index, 1, 0);
5695b7b453SJohn Marino }
5795b7b453SJohn Marino
5895b7b453SJohn Marino int
_getopt_long_only_r(int argc,char ** argv,const char * options,const struct option * long_options,int * opt_index,struct _getopt_data * d)5995b7b453SJohn Marino _getopt_long_only_r (int argc, char **argv, const char *options,
6095b7b453SJohn Marino const struct option *long_options, int *opt_index,
6195b7b453SJohn Marino struct _getopt_data *d)
6295b7b453SJohn Marino {
6395b7b453SJohn Marino return _getopt_internal_r (argc, argv, options, long_options, opt_index,
6495b7b453SJohn Marino 1, d, 0);
6595b7b453SJohn Marino }
6695b7b453SJohn Marino
6795b7b453SJohn Marino
6895b7b453SJohn Marino #ifdef TEST
6995b7b453SJohn Marino
7095b7b453SJohn Marino #include <stdio.h>
71*09d4459fSDaniel Fojt #include <stdlib.h>
7295b7b453SJohn Marino
7395b7b453SJohn Marino int
main(int argc,char ** argv)7495b7b453SJohn Marino main (int argc, char **argv)
7595b7b453SJohn Marino {
7695b7b453SJohn Marino int c;
7795b7b453SJohn Marino int digit_optind = 0;
7895b7b453SJohn Marino
7995b7b453SJohn Marino while (1)
8095b7b453SJohn Marino {
8195b7b453SJohn Marino int this_option_optind = optind ? optind : 1;
8295b7b453SJohn Marino int option_index = 0;
8395b7b453SJohn Marino static const struct option long_options[] =
8495b7b453SJohn Marino {
8595b7b453SJohn Marino {"add", 1, 0, 0},
8695b7b453SJohn Marino {"append", 0, 0, 0},
8795b7b453SJohn Marino {"delete", 1, 0, 0},
8895b7b453SJohn Marino {"verbose", 0, 0, 0},
8995b7b453SJohn Marino {"create", 0, 0, 0},
9095b7b453SJohn Marino {"file", 1, 0, 0},
9195b7b453SJohn Marino {0, 0, 0, 0}
9295b7b453SJohn Marino };
9395b7b453SJohn Marino
9495b7b453SJohn Marino c = getopt_long (argc, argv, "abc:d:0123456789",
9595b7b453SJohn Marino long_options, &option_index);
9695b7b453SJohn Marino if (c == -1)
9795b7b453SJohn Marino break;
9895b7b453SJohn Marino
9995b7b453SJohn Marino switch (c)
10095b7b453SJohn Marino {
10195b7b453SJohn Marino case 0:
10295b7b453SJohn Marino printf ("option %s", long_options[option_index].name);
10395b7b453SJohn Marino if (optarg)
10495b7b453SJohn Marino printf (" with arg %s", optarg);
10595b7b453SJohn Marino printf ("\n");
10695b7b453SJohn Marino break;
10795b7b453SJohn Marino
10895b7b453SJohn Marino case '0':
10995b7b453SJohn Marino case '1':
11095b7b453SJohn Marino case '2':
11195b7b453SJohn Marino case '3':
11295b7b453SJohn Marino case '4':
11395b7b453SJohn Marino case '5':
11495b7b453SJohn Marino case '6':
11595b7b453SJohn Marino case '7':
11695b7b453SJohn Marino case '8':
11795b7b453SJohn Marino case '9':
11895b7b453SJohn Marino if (digit_optind != 0 && digit_optind != this_option_optind)
11995b7b453SJohn Marino printf ("digits occur in two different argv-elements.\n");
12095b7b453SJohn Marino digit_optind = this_option_optind;
12195b7b453SJohn Marino printf ("option %c\n", c);
12295b7b453SJohn Marino break;
12395b7b453SJohn Marino
12495b7b453SJohn Marino case 'a':
12595b7b453SJohn Marino printf ("option a\n");
12695b7b453SJohn Marino break;
12795b7b453SJohn Marino
12895b7b453SJohn Marino case 'b':
12995b7b453SJohn Marino printf ("option b\n");
13095b7b453SJohn Marino break;
13195b7b453SJohn Marino
13295b7b453SJohn Marino case 'c':
133cf28ed85SJohn Marino printf ("option c with value '%s'\n", optarg);
13495b7b453SJohn Marino break;
13595b7b453SJohn Marino
13695b7b453SJohn Marino case 'd':
137cf28ed85SJohn Marino printf ("option d with value '%s'\n", optarg);
13895b7b453SJohn Marino break;
13995b7b453SJohn Marino
14095b7b453SJohn Marino case '?':
14195b7b453SJohn Marino break;
14295b7b453SJohn Marino
14395b7b453SJohn Marino default:
14495b7b453SJohn Marino printf ("?? getopt returned character code 0%o ??\n", c);
14595b7b453SJohn Marino }
14695b7b453SJohn Marino }
14795b7b453SJohn Marino
14895b7b453SJohn Marino if (optind < argc)
14995b7b453SJohn Marino {
15095b7b453SJohn Marino printf ("non-option ARGV-elements: ");
15195b7b453SJohn Marino while (optind < argc)
15295b7b453SJohn Marino printf ("%s ", argv[optind++]);
15395b7b453SJohn Marino printf ("\n");
15495b7b453SJohn Marino }
15595b7b453SJohn Marino
15695b7b453SJohn Marino exit (0);
15795b7b453SJohn Marino }
15895b7b453SJohn Marino
15995b7b453SJohn Marino #endif /* TEST */
160