xref: /openbsd-src/gnu/lib/libiberty/src/getopt1.c (revision 20fce977aadac3358da45d5027d7d19cdc03b0fe)
100bf4279Sespie /* getopt_long and getopt_long_only entry points for GNU getopt.
2*20fce977Smiod    Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2005
300bf4279Sespie      Free Software Foundation, Inc.
400bf4279Sespie 
52e0724c7Sespie    NOTE: This source is derived from an old version taken from the GNU C
62e0724c7Sespie    Library (glibc).
700bf4279Sespie 
800bf4279Sespie    This program is free software; you can redistribute it and/or modify it
900bf4279Sespie    under the terms of the GNU General Public License as published by the
1000bf4279Sespie    Free Software Foundation; either version 2, or (at your option) any
1100bf4279Sespie    later version.
1200bf4279Sespie 
1300bf4279Sespie    This program is distributed in the hope that it will be useful,
1400bf4279Sespie    but WITHOUT ANY WARRANTY; without even the implied warranty of
1500bf4279Sespie    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1600bf4279Sespie    GNU General Public License for more details.
1700bf4279Sespie 
1800bf4279Sespie    You should have received a copy of the GNU General Public License
1900bf4279Sespie    along with this program; if not, write to the Free Software
20*20fce977Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
2100bf4279Sespie    USA.  */
2200bf4279Sespie 
2300bf4279Sespie #ifdef HAVE_CONFIG_H
2400bf4279Sespie #include <config.h>
2500bf4279Sespie #endif
2600bf4279Sespie 
2700bf4279Sespie #if !defined __STDC__ || !__STDC__
2800bf4279Sespie /* This is a separate conditional since some stdc systems
2900bf4279Sespie    reject `defined (const)'.  */
3000bf4279Sespie #ifndef const
3100bf4279Sespie #define const
3200bf4279Sespie #endif
3300bf4279Sespie #endif
3400bf4279Sespie 
3500bf4279Sespie #include <stdio.h>
3600bf4279Sespie 
37*20fce977Smiod #include "getopt.h"
38*20fce977Smiod 
3900bf4279Sespie /* Comment out all this code if we are using the GNU C Library, and are not
4000bf4279Sespie    actually compiling the library itself.  This code is part of the GNU C
4100bf4279Sespie    Library, but also included in many other GNU distributions.  Compiling
4200bf4279Sespie    and linking in this code is a waste when using the GNU C library
4300bf4279Sespie    (especially if it is a shared library).  Rather than having every GNU
4400bf4279Sespie    program understand `configure --with-gnu-libc' and omit the object files,
4500bf4279Sespie    it is simpler to just do this in the source for each such file.  */
4600bf4279Sespie 
4700bf4279Sespie #define GETOPT_INTERFACE_VERSION 2
4800bf4279Sespie #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
4900bf4279Sespie #include <gnu-versions.h>
5000bf4279Sespie #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
5100bf4279Sespie #define ELIDE_CODE
5200bf4279Sespie #endif
5300bf4279Sespie #endif
5400bf4279Sespie 
5500bf4279Sespie #ifndef ELIDE_CODE
5600bf4279Sespie 
5700bf4279Sespie 
5800bf4279Sespie /* This needs to come after some library #include
5900bf4279Sespie    to get __GNU_LIBRARY__ defined.  */
6000bf4279Sespie #ifdef __GNU_LIBRARY__
6100bf4279Sespie #include <stdlib.h>
6200bf4279Sespie #endif
6300bf4279Sespie 
6400bf4279Sespie #ifndef	NULL
6500bf4279Sespie #define NULL 0
6600bf4279Sespie #endif
6700bf4279Sespie 
6800bf4279Sespie int
getopt_long(int argc,char * const * argv,const char * options,const struct option * long_options,int * opt_index)69*20fce977Smiod getopt_long (int argc,  char *const *argv,  const char *options,
70*20fce977Smiod              const struct option *long_options, int *opt_index)
7100bf4279Sespie {
7200bf4279Sespie   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
7300bf4279Sespie }
7400bf4279Sespie 
7500bf4279Sespie /* Like getopt_long, but '-' as well as '--' can indicate a long option.
7600bf4279Sespie    If an option that starts with '-' (not '--') doesn't match a long option,
7700bf4279Sespie    but does match a short option, it is parsed as a short option
7800bf4279Sespie    instead.  */
7900bf4279Sespie 
8000bf4279Sespie int
getopt_long_only(int argc,char * const * argv,const char * options,const struct option * long_options,int * opt_index)81*20fce977Smiod getopt_long_only (int argc, char *const *argv, const char *options,
82*20fce977Smiod                   const struct option *long_options, int *opt_index)
8300bf4279Sespie {
8400bf4279Sespie   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
8500bf4279Sespie }
8600bf4279Sespie 
8700bf4279Sespie 
8800bf4279Sespie #endif	/* Not ELIDE_CODE.  */
8900bf4279Sespie 
9000bf4279Sespie #ifdef TEST
9100bf4279Sespie 
9200bf4279Sespie #include <stdio.h>
9300bf4279Sespie 
9400bf4279Sespie int
main(int argc,char ** argv)95*20fce977Smiod main (int argc, char **argv)
9600bf4279Sespie {
9700bf4279Sespie   int c;
9800bf4279Sespie   int digit_optind = 0;
9900bf4279Sespie 
10000bf4279Sespie   while (1)
10100bf4279Sespie     {
10200bf4279Sespie       int this_option_optind = optind ? optind : 1;
10300bf4279Sespie       int option_index = 0;
10400bf4279Sespie       static struct option long_options[] =
10500bf4279Sespie       {
10600bf4279Sespie 	{"add", 1, 0, 0},
10700bf4279Sespie 	{"append", 0, 0, 0},
10800bf4279Sespie 	{"delete", 1, 0, 0},
10900bf4279Sespie 	{"verbose", 0, 0, 0},
11000bf4279Sespie 	{"create", 0, 0, 0},
11100bf4279Sespie 	{"file", 1, 0, 0},
11200bf4279Sespie 	{0, 0, 0, 0}
11300bf4279Sespie       };
11400bf4279Sespie 
11500bf4279Sespie       c = getopt_long (argc, argv, "abc:d:0123456789",
11600bf4279Sespie 		       long_options, &option_index);
11700bf4279Sespie       if (c == -1)
11800bf4279Sespie 	break;
11900bf4279Sespie 
12000bf4279Sespie       switch (c)
12100bf4279Sespie 	{
12200bf4279Sespie 	case 0:
12300bf4279Sespie 	  printf ("option %s", long_options[option_index].name);
12400bf4279Sespie 	  if (optarg)
12500bf4279Sespie 	    printf (" with arg %s", optarg);
12600bf4279Sespie 	  printf ("\n");
12700bf4279Sespie 	  break;
12800bf4279Sespie 
12900bf4279Sespie 	case '0':
13000bf4279Sespie 	case '1':
13100bf4279Sespie 	case '2':
13200bf4279Sespie 	case '3':
13300bf4279Sespie 	case '4':
13400bf4279Sespie 	case '5':
13500bf4279Sespie 	case '6':
13600bf4279Sespie 	case '7':
13700bf4279Sespie 	case '8':
13800bf4279Sespie 	case '9':
13900bf4279Sespie 	  if (digit_optind != 0 && digit_optind != this_option_optind)
14000bf4279Sespie 	    printf ("digits occur in two different argv-elements.\n");
14100bf4279Sespie 	  digit_optind = this_option_optind;
14200bf4279Sespie 	  printf ("option %c\n", c);
14300bf4279Sespie 	  break;
14400bf4279Sespie 
14500bf4279Sespie 	case 'a':
14600bf4279Sespie 	  printf ("option a\n");
14700bf4279Sespie 	  break;
14800bf4279Sespie 
14900bf4279Sespie 	case 'b':
15000bf4279Sespie 	  printf ("option b\n");
15100bf4279Sespie 	  break;
15200bf4279Sespie 
15300bf4279Sespie 	case 'c':
15400bf4279Sespie 	  printf ("option c with value `%s'\n", optarg);
15500bf4279Sespie 	  break;
15600bf4279Sespie 
15700bf4279Sespie 	case 'd':
15800bf4279Sespie 	  printf ("option d with value `%s'\n", optarg);
15900bf4279Sespie 	  break;
16000bf4279Sespie 
16100bf4279Sespie 	case '?':
16200bf4279Sespie 	  break;
16300bf4279Sespie 
16400bf4279Sespie 	default:
16500bf4279Sespie 	  printf ("?? getopt returned character code 0%o ??\n", c);
16600bf4279Sespie 	}
16700bf4279Sespie     }
16800bf4279Sespie 
16900bf4279Sespie   if (optind < argc)
17000bf4279Sespie     {
17100bf4279Sespie       printf ("non-option ARGV-elements: ");
17200bf4279Sespie       while (optind < argc)
17300bf4279Sespie 	printf ("%s ", argv[optind++]);
17400bf4279Sespie       printf ("\n");
17500bf4279Sespie     }
17600bf4279Sespie 
17700bf4279Sespie   exit (0);
17800bf4279Sespie }
17900bf4279Sespie 
18000bf4279Sespie #endif /* TEST */
181