xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/getopt1.c (revision 5173eb0a33e5d83890ba976253e703be4c92557c)
198b9484cSchristos /* getopt_long and getopt_long_only entry points for GNU getopt.
2*5173eb0aSchristos    Copyright (C) 1987-2024 Free Software Foundation, Inc.
398b9484cSchristos 
498b9484cSchristos    NOTE: This source is derived from an old version taken from the GNU C
598b9484cSchristos    Library (glibc).
698b9484cSchristos 
798b9484cSchristos    This program is free software; you can redistribute it and/or modify it
898b9484cSchristos    under the terms of the GNU General Public License as published by the
998b9484cSchristos    Free Software Foundation; either version 2, or (at your option) any
1098b9484cSchristos    later version.
1198b9484cSchristos 
1298b9484cSchristos    This program is distributed in the hope that it will be useful,
1398b9484cSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1498b9484cSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1598b9484cSchristos    GNU General Public License for more details.
1698b9484cSchristos 
1798b9484cSchristos    You should have received a copy of the GNU General Public License
1898b9484cSchristos    along with this program; if not, write to the Free Software
1998b9484cSchristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
2098b9484cSchristos    USA.  */
2198b9484cSchristos 
2298b9484cSchristos #ifdef HAVE_CONFIG_H
2398b9484cSchristos #include <config.h>
2498b9484cSchristos #endif
2598b9484cSchristos 
2698b9484cSchristos #if !defined __STDC__ || !__STDC__
2798b9484cSchristos /* This is a separate conditional since some stdc systems
2898b9484cSchristos    reject `defined (const)'.  */
2998b9484cSchristos #ifndef const
3098b9484cSchristos #define const
3198b9484cSchristos #endif
3298b9484cSchristos #endif
3398b9484cSchristos 
3498b9484cSchristos #include <stdio.h>
3598b9484cSchristos 
3698b9484cSchristos #include "getopt.h"
3798b9484cSchristos 
3898b9484cSchristos /* Comment out all this code if we are using the GNU C Library, and are not
3998b9484cSchristos    actually compiling the library itself.  This code is part of the GNU C
4098b9484cSchristos    Library, but also included in many other GNU distributions.  Compiling
4198b9484cSchristos    and linking in this code is a waste when using the GNU C library
4298b9484cSchristos    (especially if it is a shared library).  Rather than having every GNU
4398b9484cSchristos    program understand `configure --with-gnu-libc' and omit the object files,
4498b9484cSchristos    it is simpler to just do this in the source for each such file.  */
4598b9484cSchristos 
4698b9484cSchristos #define GETOPT_INTERFACE_VERSION 2
4798b9484cSchristos #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
4898b9484cSchristos #include <gnu-versions.h>
4998b9484cSchristos #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
5098b9484cSchristos #define ELIDE_CODE
5198b9484cSchristos #endif
5298b9484cSchristos #endif
5398b9484cSchristos 
5498b9484cSchristos #ifndef ELIDE_CODE
5598b9484cSchristos 
5698b9484cSchristos 
5798b9484cSchristos /* This needs to come after some library #include
5898b9484cSchristos    to get __GNU_LIBRARY__ defined.  */
5998b9484cSchristos #ifdef __GNU_LIBRARY__
6098b9484cSchristos #include <stdlib.h>
6198b9484cSchristos #endif
6298b9484cSchristos 
6398b9484cSchristos #ifndef	NULL
6498b9484cSchristos #define NULL 0
6598b9484cSchristos #endif
6698b9484cSchristos 
6798b9484cSchristos int
6898b9484cSchristos getopt_long (int argc,  char *const *argv,  const char *options,
6998b9484cSchristos              const struct option *long_options, int *opt_index)
7098b9484cSchristos {
7198b9484cSchristos   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
7298b9484cSchristos }
7398b9484cSchristos 
7498b9484cSchristos /* Like getopt_long, but '-' as well as '--' can indicate a long option.
7598b9484cSchristos    If an option that starts with '-' (not '--') doesn't match a long option,
7698b9484cSchristos    but does match a short option, it is parsed as a short option
7798b9484cSchristos    instead.  */
7898b9484cSchristos 
7998b9484cSchristos int
8098b9484cSchristos getopt_long_only (int argc, char *const *argv, const char *options,
8198b9484cSchristos                   const struct option *long_options, int *opt_index)
8298b9484cSchristos {
8398b9484cSchristos   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
8498b9484cSchristos }
8598b9484cSchristos 
8698b9484cSchristos 
8798b9484cSchristos #endif	/* Not ELIDE_CODE.  */
8898b9484cSchristos 
8998b9484cSchristos #ifdef TEST
9098b9484cSchristos 
9198b9484cSchristos #include <stdio.h>
9298b9484cSchristos 
9398b9484cSchristos int
9498b9484cSchristos main (int argc, char **argv)
9598b9484cSchristos {
9698b9484cSchristos   int c;
9798b9484cSchristos   int digit_optind = 0;
9898b9484cSchristos 
9998b9484cSchristos   while (1)
10098b9484cSchristos     {
10198b9484cSchristos       int this_option_optind = optind ? optind : 1;
10298b9484cSchristos       int option_index = 0;
10398b9484cSchristos       static struct option long_options[] =
10498b9484cSchristos       {
10598b9484cSchristos 	{"add", 1, 0, 0},
10698b9484cSchristos 	{"append", 0, 0, 0},
10798b9484cSchristos 	{"delete", 1, 0, 0},
10898b9484cSchristos 	{"verbose", 0, 0, 0},
10998b9484cSchristos 	{"create", 0, 0, 0},
11098b9484cSchristos 	{"file", 1, 0, 0},
11198b9484cSchristos 	{0, 0, 0, 0}
11298b9484cSchristos       };
11398b9484cSchristos 
11498b9484cSchristos       c = getopt_long (argc, argv, "abc:d:0123456789",
11598b9484cSchristos 		       long_options, &option_index);
11698b9484cSchristos       if (c == -1)
11798b9484cSchristos 	break;
11898b9484cSchristos 
11998b9484cSchristos       switch (c)
12098b9484cSchristos 	{
12198b9484cSchristos 	case 0:
12298b9484cSchristos 	  printf ("option %s", long_options[option_index].name);
12398b9484cSchristos 	  if (optarg)
12498b9484cSchristos 	    printf (" with arg %s", optarg);
12598b9484cSchristos 	  printf ("\n");
12698b9484cSchristos 	  break;
12798b9484cSchristos 
12898b9484cSchristos 	case '0':
12998b9484cSchristos 	case '1':
13098b9484cSchristos 	case '2':
13198b9484cSchristos 	case '3':
13298b9484cSchristos 	case '4':
13398b9484cSchristos 	case '5':
13498b9484cSchristos 	case '6':
13598b9484cSchristos 	case '7':
13698b9484cSchristos 	case '8':
13798b9484cSchristos 	case '9':
13898b9484cSchristos 	  if (digit_optind != 0 && digit_optind != this_option_optind)
13998b9484cSchristos 	    printf ("digits occur in two different argv-elements.\n");
14098b9484cSchristos 	  digit_optind = this_option_optind;
14198b9484cSchristos 	  printf ("option %c\n", c);
14298b9484cSchristos 	  break;
14398b9484cSchristos 
14498b9484cSchristos 	case 'a':
14598b9484cSchristos 	  printf ("option a\n");
14698b9484cSchristos 	  break;
14798b9484cSchristos 
14898b9484cSchristos 	case 'b':
14998b9484cSchristos 	  printf ("option b\n");
15098b9484cSchristos 	  break;
15198b9484cSchristos 
15298b9484cSchristos 	case 'c':
15398b9484cSchristos 	  printf ("option c with value `%s'\n", optarg);
15498b9484cSchristos 	  break;
15598b9484cSchristos 
15698b9484cSchristos 	case 'd':
15798b9484cSchristos 	  printf ("option d with value `%s'\n", optarg);
15898b9484cSchristos 	  break;
15998b9484cSchristos 
16098b9484cSchristos 	case '?':
16198b9484cSchristos 	  break;
16298b9484cSchristos 
16398b9484cSchristos 	default:
16498b9484cSchristos 	  printf ("?? getopt returned character code 0%o ??\n", c);
16598b9484cSchristos 	}
16698b9484cSchristos     }
16798b9484cSchristos 
16898b9484cSchristos   if (optind < argc)
16998b9484cSchristos     {
17098b9484cSchristos       printf ("non-option ARGV-elements: ");
17198b9484cSchristos       while (optind < argc)
17298b9484cSchristos 	printf ("%s ", argv[optind++]);
17398b9484cSchristos       printf ("\n");
17498b9484cSchristos     }
17598b9484cSchristos 
17698b9484cSchristos   exit (0);
17798b9484cSchristos }
17898b9484cSchristos 
17998b9484cSchristos #endif /* TEST */
180