xref: /openbsd-src/gnu/usr.bin/cvs/lib/getopt1.c (revision 461cc63e7458ce60db55037c1a7656349538b52f)
11e72d8d2Sderaadt /* getopt_long and getopt_long_only entry points for GNU getopt.
21e72d8d2Sderaadt    Copyright (C) 1987, 88, 89, 90, 91, 92, 1993
31e72d8d2Sderaadt 	Free Software Foundation, Inc.
41e72d8d2Sderaadt 
51e72d8d2Sderaadt    This program is free software; you can redistribute it and/or modify it
61e72d8d2Sderaadt    under the terms of the GNU General Public License as published by the
71e72d8d2Sderaadt    Free Software Foundation; either version 2, or (at your option) any
81e72d8d2Sderaadt    later version.
91e72d8d2Sderaadt 
101e72d8d2Sderaadt    This program is distributed in the hope that it will be useful,
111e72d8d2Sderaadt    but WITHOUT ANY WARRANTY; without even the implied warranty of
121e72d8d2Sderaadt    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*461cc63eStholo    GNU General Public License for more details.  */
141e72d8d2Sderaadt 
151e72d8d2Sderaadt #ifdef HAVE_CONFIG_H
161e72d8d2Sderaadt #if defined (emacs) || defined (CONFIG_BROKETS)
171e72d8d2Sderaadt /* We use <config.h> instead of "config.h" so that a compilation
181e72d8d2Sderaadt    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
191e72d8d2Sderaadt    (which it would do because it found this file in $srcdir).  */
201e72d8d2Sderaadt #include <config.h>
211e72d8d2Sderaadt #else
221e72d8d2Sderaadt #include "config.h"
231e72d8d2Sderaadt #endif
241e72d8d2Sderaadt #endif
251e72d8d2Sderaadt 
261e72d8d2Sderaadt #include "getopt.h"
271e72d8d2Sderaadt 
281e72d8d2Sderaadt #ifndef __STDC__
291e72d8d2Sderaadt /* This is a separate conditional since some stdc systems
301e72d8d2Sderaadt    reject `defined (const)'.  */
311e72d8d2Sderaadt #ifndef const
321e72d8d2Sderaadt #define const
331e72d8d2Sderaadt #endif
341e72d8d2Sderaadt #endif
351e72d8d2Sderaadt 
361e72d8d2Sderaadt #include <stdio.h>
371e72d8d2Sderaadt 
381e72d8d2Sderaadt /* Comment out all this code if we are using the GNU C Library, and are not
391e72d8d2Sderaadt    actually compiling the library itself.  This code is part of the GNU C
401e72d8d2Sderaadt    Library, but also included in many other GNU distributions.  Compiling
411e72d8d2Sderaadt    and linking in this code is a waste when using the GNU C library
421e72d8d2Sderaadt    (especially if it is a shared library).  Rather than having every GNU
431e72d8d2Sderaadt    program understand `configure --with-gnu-libc' and omit the object files,
441e72d8d2Sderaadt    it is simpler to just do this in the source for each such file.  */
451e72d8d2Sderaadt 
461e72d8d2Sderaadt #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
471e72d8d2Sderaadt 
481e72d8d2Sderaadt 
491e72d8d2Sderaadt /* This needs to come after some library #include
501e72d8d2Sderaadt    to get __GNU_LIBRARY__ defined.  */
511e72d8d2Sderaadt #ifdef __GNU_LIBRARY__
521e72d8d2Sderaadt #include <stdlib.h>
531e72d8d2Sderaadt #else
541e72d8d2Sderaadt char *getenv ();
551e72d8d2Sderaadt #endif
561e72d8d2Sderaadt 
571e72d8d2Sderaadt #ifndef	NULL
581e72d8d2Sderaadt #define NULL 0
591e72d8d2Sderaadt #endif
601e72d8d2Sderaadt 
611e72d8d2Sderaadt int
getopt_long(argc,argv,options,long_options,opt_index)621e72d8d2Sderaadt getopt_long (argc, argv, options, long_options, opt_index)
631e72d8d2Sderaadt      int argc;
641e72d8d2Sderaadt      char *const *argv;
651e72d8d2Sderaadt      const char *options;
661e72d8d2Sderaadt      const struct option *long_options;
671e72d8d2Sderaadt      int *opt_index;
681e72d8d2Sderaadt {
691e72d8d2Sderaadt   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
701e72d8d2Sderaadt }
711e72d8d2Sderaadt 
721e72d8d2Sderaadt /* Like getopt_long, but '-' as well as '--' can indicate a long option.
731e72d8d2Sderaadt    If an option that starts with '-' (not '--') doesn't match a long option,
741e72d8d2Sderaadt    but does match a short option, it is parsed as a short option
751e72d8d2Sderaadt    instead.  */
761e72d8d2Sderaadt 
771e72d8d2Sderaadt int
getopt_long_only(argc,argv,options,long_options,opt_index)781e72d8d2Sderaadt getopt_long_only (argc, argv, options, long_options, opt_index)
791e72d8d2Sderaadt      int argc;
801e72d8d2Sderaadt      char *const *argv;
811e72d8d2Sderaadt      const char *options;
821e72d8d2Sderaadt      const struct option *long_options;
831e72d8d2Sderaadt      int *opt_index;
841e72d8d2Sderaadt {
851e72d8d2Sderaadt   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
861e72d8d2Sderaadt }
871e72d8d2Sderaadt 
881e72d8d2Sderaadt 
891e72d8d2Sderaadt #endif	/* _LIBC or not __GNU_LIBRARY__.  */
901e72d8d2Sderaadt 
911e72d8d2Sderaadt #ifdef TEST
921e72d8d2Sderaadt 
931e72d8d2Sderaadt #include <stdio.h>
941e72d8d2Sderaadt 
951e72d8d2Sderaadt int
main(argc,argv)961e72d8d2Sderaadt main (argc, argv)
971e72d8d2Sderaadt      int argc;
981e72d8d2Sderaadt      char **argv;
991e72d8d2Sderaadt {
1001e72d8d2Sderaadt   int c;
1011e72d8d2Sderaadt   int digit_optind = 0;
1021e72d8d2Sderaadt 
1031e72d8d2Sderaadt   while (1)
1041e72d8d2Sderaadt     {
1051e72d8d2Sderaadt       int this_option_optind = optind ? optind : 1;
1061e72d8d2Sderaadt       int option_index = 0;
1071e72d8d2Sderaadt       static struct option long_options[] =
1081e72d8d2Sderaadt       {
1091e72d8d2Sderaadt 	{"add", 1, 0, 0},
1101e72d8d2Sderaadt 	{"append", 0, 0, 0},
1111e72d8d2Sderaadt 	{"delete", 1, 0, 0},
1121e72d8d2Sderaadt 	{"verbose", 0, 0, 0},
1131e72d8d2Sderaadt 	{"create", 0, 0, 0},
1141e72d8d2Sderaadt 	{"file", 1, 0, 0},
1151e72d8d2Sderaadt 	{0, 0, 0, 0}
1161e72d8d2Sderaadt       };
1171e72d8d2Sderaadt 
1181e72d8d2Sderaadt       c = getopt_long (argc, argv, "abc:d:0123456789",
1191e72d8d2Sderaadt 		       long_options, &option_index);
1201e72d8d2Sderaadt       if (c == EOF)
1211e72d8d2Sderaadt 	break;
1221e72d8d2Sderaadt 
1231e72d8d2Sderaadt       switch (c)
1241e72d8d2Sderaadt 	{
1251e72d8d2Sderaadt 	case 0:
1261e72d8d2Sderaadt 	  printf ("option %s", long_options[option_index].name);
1271e72d8d2Sderaadt 	  if (optarg)
1281e72d8d2Sderaadt 	    printf (" with arg %s", optarg);
1291e72d8d2Sderaadt 	  printf ("\n");
1301e72d8d2Sderaadt 	  break;
1311e72d8d2Sderaadt 
1321e72d8d2Sderaadt 	case '0':
1331e72d8d2Sderaadt 	case '1':
1341e72d8d2Sderaadt 	case '2':
1351e72d8d2Sderaadt 	case '3':
1361e72d8d2Sderaadt 	case '4':
1371e72d8d2Sderaadt 	case '5':
1381e72d8d2Sderaadt 	case '6':
1391e72d8d2Sderaadt 	case '7':
1401e72d8d2Sderaadt 	case '8':
1411e72d8d2Sderaadt 	case '9':
1421e72d8d2Sderaadt 	  if (digit_optind != 0 && digit_optind != this_option_optind)
1431e72d8d2Sderaadt 	    printf ("digits occur in two different argv-elements.\n");
1441e72d8d2Sderaadt 	  digit_optind = this_option_optind;
1451e72d8d2Sderaadt 	  printf ("option %c\n", c);
1461e72d8d2Sderaadt 	  break;
1471e72d8d2Sderaadt 
1481e72d8d2Sderaadt 	case 'a':
1491e72d8d2Sderaadt 	  printf ("option a\n");
1501e72d8d2Sderaadt 	  break;
1511e72d8d2Sderaadt 
1521e72d8d2Sderaadt 	case 'b':
1531e72d8d2Sderaadt 	  printf ("option b\n");
1541e72d8d2Sderaadt 	  break;
1551e72d8d2Sderaadt 
1561e72d8d2Sderaadt 	case 'c':
1571e72d8d2Sderaadt 	  printf ("option c with value `%s'\n", optarg);
1581e72d8d2Sderaadt 	  break;
1591e72d8d2Sderaadt 
1601e72d8d2Sderaadt 	case 'd':
1611e72d8d2Sderaadt 	  printf ("option d with value `%s'\n", optarg);
1621e72d8d2Sderaadt 	  break;
1631e72d8d2Sderaadt 
1641e72d8d2Sderaadt 	case '?':
1651e72d8d2Sderaadt 	  break;
1661e72d8d2Sderaadt 
1671e72d8d2Sderaadt 	default:
1681e72d8d2Sderaadt 	  printf ("?? getopt returned character code 0%o ??\n", c);
1691e72d8d2Sderaadt 	}
1701e72d8d2Sderaadt     }
1711e72d8d2Sderaadt 
1721e72d8d2Sderaadt   if (optind < argc)
1731e72d8d2Sderaadt     {
1741e72d8d2Sderaadt       printf ("non-option ARGV-elements: ");
1751e72d8d2Sderaadt       while (optind < argc)
1761e72d8d2Sderaadt 	printf ("%s ", argv[optind++]);
1771e72d8d2Sderaadt       printf ("\n");
1781e72d8d2Sderaadt     }
1791e72d8d2Sderaadt 
1801e72d8d2Sderaadt   exit (0);
1811e72d8d2Sderaadt }
1821e72d8d2Sderaadt 
1831e72d8d2Sderaadt #endif /* TEST */
184