110d565efSmrg /* getopt_long and getopt_long_only entry points for GNU getopt.
2*ec02198aSmrg    Copyright (C) 1987-2020 Free Software Foundation, Inc.
310d565efSmrg 
410d565efSmrg    NOTE: This source is derived from an old version taken from the GNU C
510d565efSmrg    Library (glibc).
610d565efSmrg 
710d565efSmrg    This program is free software; you can redistribute it and/or modify it
810d565efSmrg    under the terms of the GNU General Public License as published by the
910d565efSmrg    Free Software Foundation; either version 2, or (at your option) any
1010d565efSmrg    later version.
1110d565efSmrg 
1210d565efSmrg    This program is distributed in the hope that it will be useful,
1310d565efSmrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
1410d565efSmrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1510d565efSmrg    GNU General Public License for more details.
1610d565efSmrg 
1710d565efSmrg    You should have received a copy of the GNU General Public License
1810d565efSmrg    along with this program; if not, write to the Free Software
1910d565efSmrg    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
2010d565efSmrg    USA.  */
2110d565efSmrg 
2210d565efSmrg #ifdef HAVE_CONFIG_H
2310d565efSmrg #include <config.h>
2410d565efSmrg #endif
2510d565efSmrg 
2610d565efSmrg #if !defined __STDC__ || !__STDC__
2710d565efSmrg /* This is a separate conditional since some stdc systems
2810d565efSmrg    reject `defined (const)'.  */
2910d565efSmrg #ifndef const
3010d565efSmrg #define const
3110d565efSmrg #endif
3210d565efSmrg #endif
3310d565efSmrg 
3410d565efSmrg #include <stdio.h>
3510d565efSmrg 
3610d565efSmrg #include "getopt.h"
3710d565efSmrg 
3810d565efSmrg /* Comment out all this code if we are using the GNU C Library, and are not
3910d565efSmrg    actually compiling the library itself.  This code is part of the GNU C
4010d565efSmrg    Library, but also included in many other GNU distributions.  Compiling
4110d565efSmrg    and linking in this code is a waste when using the GNU C library
4210d565efSmrg    (especially if it is a shared library).  Rather than having every GNU
4310d565efSmrg    program understand `configure --with-gnu-libc' and omit the object files,
4410d565efSmrg    it is simpler to just do this in the source for each such file.  */
4510d565efSmrg 
4610d565efSmrg #define GETOPT_INTERFACE_VERSION 2
4710d565efSmrg #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
4810d565efSmrg #include <gnu-versions.h>
4910d565efSmrg #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
5010d565efSmrg #define ELIDE_CODE
5110d565efSmrg #endif
5210d565efSmrg #endif
5310d565efSmrg 
5410d565efSmrg #ifndef ELIDE_CODE
5510d565efSmrg 
5610d565efSmrg 
5710d565efSmrg /* This needs to come after some library #include
5810d565efSmrg    to get __GNU_LIBRARY__ defined.  */
5910d565efSmrg #ifdef __GNU_LIBRARY__
6010d565efSmrg #include <stdlib.h>
6110d565efSmrg #endif
6210d565efSmrg 
6310d565efSmrg #ifndef	NULL
6410d565efSmrg #define NULL 0
6510d565efSmrg #endif
6610d565efSmrg 
6710d565efSmrg int
getopt_long(int argc,char * const * argv,const char * options,const struct option * long_options,int * opt_index)6810d565efSmrg getopt_long (int argc,  char *const *argv,  const char *options,
6910d565efSmrg              const struct option *long_options, int *opt_index)
7010d565efSmrg {
7110d565efSmrg   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
7210d565efSmrg }
7310d565efSmrg 
7410d565efSmrg /* Like getopt_long, but '-' as well as '--' can indicate a long option.
7510d565efSmrg    If an option that starts with '-' (not '--') doesn't match a long option,
7610d565efSmrg    but does match a short option, it is parsed as a short option
7710d565efSmrg    instead.  */
7810d565efSmrg 
7910d565efSmrg int
getopt_long_only(int argc,char * const * argv,const char * options,const struct option * long_options,int * opt_index)8010d565efSmrg getopt_long_only (int argc, char *const *argv, const char *options,
8110d565efSmrg                   const struct option *long_options, int *opt_index)
8210d565efSmrg {
8310d565efSmrg   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
8410d565efSmrg }
8510d565efSmrg 
8610d565efSmrg 
8710d565efSmrg #endif	/* Not ELIDE_CODE.  */
8810d565efSmrg 
8910d565efSmrg #ifdef TEST
9010d565efSmrg 
9110d565efSmrg #include <stdio.h>
9210d565efSmrg 
9310d565efSmrg int
main(int argc,char ** argv)9410d565efSmrg main (int argc, char **argv)
9510d565efSmrg {
9610d565efSmrg   int c;
9710d565efSmrg   int digit_optind = 0;
9810d565efSmrg 
9910d565efSmrg   while (1)
10010d565efSmrg     {
10110d565efSmrg       int this_option_optind = optind ? optind : 1;
10210d565efSmrg       int option_index = 0;
10310d565efSmrg       static struct option long_options[] =
10410d565efSmrg       {
10510d565efSmrg 	{"add", 1, 0, 0},
10610d565efSmrg 	{"append", 0, 0, 0},
10710d565efSmrg 	{"delete", 1, 0, 0},
10810d565efSmrg 	{"verbose", 0, 0, 0},
10910d565efSmrg 	{"create", 0, 0, 0},
11010d565efSmrg 	{"file", 1, 0, 0},
11110d565efSmrg 	{0, 0, 0, 0}
11210d565efSmrg       };
11310d565efSmrg 
11410d565efSmrg       c = getopt_long (argc, argv, "abc:d:0123456789",
11510d565efSmrg 		       long_options, &option_index);
11610d565efSmrg       if (c == -1)
11710d565efSmrg 	break;
11810d565efSmrg 
11910d565efSmrg       switch (c)
12010d565efSmrg 	{
12110d565efSmrg 	case 0:
12210d565efSmrg 	  printf ("option %s", long_options[option_index].name);
12310d565efSmrg 	  if (optarg)
12410d565efSmrg 	    printf (" with arg %s", optarg);
12510d565efSmrg 	  printf ("\n");
12610d565efSmrg 	  break;
12710d565efSmrg 
12810d565efSmrg 	case '0':
12910d565efSmrg 	case '1':
13010d565efSmrg 	case '2':
13110d565efSmrg 	case '3':
13210d565efSmrg 	case '4':
13310d565efSmrg 	case '5':
13410d565efSmrg 	case '6':
13510d565efSmrg 	case '7':
13610d565efSmrg 	case '8':
13710d565efSmrg 	case '9':
13810d565efSmrg 	  if (digit_optind != 0 && digit_optind != this_option_optind)
13910d565efSmrg 	    printf ("digits occur in two different argv-elements.\n");
14010d565efSmrg 	  digit_optind = this_option_optind;
14110d565efSmrg 	  printf ("option %c\n", c);
14210d565efSmrg 	  break;
14310d565efSmrg 
14410d565efSmrg 	case 'a':
14510d565efSmrg 	  printf ("option a\n");
14610d565efSmrg 	  break;
14710d565efSmrg 
14810d565efSmrg 	case 'b':
14910d565efSmrg 	  printf ("option b\n");
15010d565efSmrg 	  break;
15110d565efSmrg 
15210d565efSmrg 	case 'c':
15310d565efSmrg 	  printf ("option c with value `%s'\n", optarg);
15410d565efSmrg 	  break;
15510d565efSmrg 
15610d565efSmrg 	case 'd':
15710d565efSmrg 	  printf ("option d with value `%s'\n", optarg);
15810d565efSmrg 	  break;
15910d565efSmrg 
16010d565efSmrg 	case '?':
16110d565efSmrg 	  break;
16210d565efSmrg 
16310d565efSmrg 	default:
16410d565efSmrg 	  printf ("?? getopt returned character code 0%o ??\n", c);
16510d565efSmrg 	}
16610d565efSmrg     }
16710d565efSmrg 
16810d565efSmrg   if (optind < argc)
16910d565efSmrg     {
17010d565efSmrg       printf ("non-option ARGV-elements: ");
17110d565efSmrg       while (optind < argc)
17210d565efSmrg 	printf ("%s ", argv[optind++]);
17310d565efSmrg       printf ("\n");
17410d565efSmrg     }
17510d565efSmrg 
17610d565efSmrg   exit (0);
17710d565efSmrg }
17810d565efSmrg 
17910d565efSmrg #endif /* TEST */
180