1169695Skan/* getopt_long and getopt_long_only entry points for GNU getopt.
2169695Skan   Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2005
3169695Skan     Free Software Foundation, Inc.
4169695Skan
5169695Skan   NOTE: This source is derived from an old version taken from the GNU C
6169695Skan   Library (glibc).
7169695Skan
8169695Skan   This program is free software; you can redistribute it and/or modify it
9169695Skan   under the terms of the GNU General Public License as published by the
10169695Skan   Free Software Foundation; either version 2, or (at your option) any
11169695Skan   later version.
12169695Skan
13169695Skan   This program is distributed in the hope that it will be useful,
14169695Skan   but WITHOUT ANY WARRANTY; without even the implied warranty of
15169695Skan   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16169695Skan   GNU General Public License for more details.
17169695Skan
18169695Skan   You should have received a copy of the GNU General Public License
19169695Skan   along with this program; if not, write to the Free Software
20169695Skan   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
21169695Skan   USA.  */
22169695Skan
23169695Skan#ifdef HAVE_CONFIG_H
24169695Skan#include <config.h>
25169695Skan#endif
26169695Skan
27169695Skan#if !defined __STDC__ || !__STDC__
28169695Skan/* This is a separate conditional since some stdc systems
29169695Skan   reject `defined (const)'.  */
30169695Skan#ifndef const
31169695Skan#define const
32169695Skan#endif
33169695Skan#endif
34169695Skan
35169695Skan#include <stdio.h>
36169695Skan
37169695Skan#include "getopt.h"
38169695Skan
39169695Skan/* Comment out all this code if we are using the GNU C Library, and are not
40169695Skan   actually compiling the library itself.  This code is part of the GNU C
41169695Skan   Library, but also included in many other GNU distributions.  Compiling
42169695Skan   and linking in this code is a waste when using the GNU C library
43169695Skan   (especially if it is a shared library).  Rather than having every GNU
44169695Skan   program understand `configure --with-gnu-libc' and omit the object files,
45169695Skan   it is simpler to just do this in the source for each such file.  */
46169695Skan
47169695Skan#define GETOPT_INTERFACE_VERSION 2
48169695Skan#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
49169695Skan#include <gnu-versions.h>
50169695Skan#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
51169695Skan#define ELIDE_CODE
52169695Skan#endif
53169695Skan#endif
54169695Skan
55169695Skan#ifndef ELIDE_CODE
56169695Skan
57169695Skan
58169695Skan/* This needs to come after some library #include
59169695Skan   to get __GNU_LIBRARY__ defined.  */
60169695Skan#ifdef __GNU_LIBRARY__
61169695Skan#include <stdlib.h>
62169695Skan#endif
63169695Skan
64169695Skan#ifndef	NULL
65169695Skan#define NULL 0
66169695Skan#endif
67169695Skan
68169695Skanint
69169695Skangetopt_long (int argc,  char *const *argv,  const char *options,
70169695Skan             const struct option *long_options, int *opt_index)
71169695Skan{
72169695Skan  return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
73169695Skan}
74169695Skan
75169695Skan/* Like getopt_long, but '-' as well as '--' can indicate a long option.
76169695Skan   If an option that starts with '-' (not '--') doesn't match a long option,
77169695Skan   but does match a short option, it is parsed as a short option
78169695Skan   instead.  */
79169695Skan
80169695Skanint
81169695Skangetopt_long_only (int argc, char *const *argv, const char *options,
82169695Skan                  const struct option *long_options, int *opt_index)
83169695Skan{
84169695Skan  return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
85169695Skan}
86169695Skan
87169695Skan
88169695Skan#endif	/* Not ELIDE_CODE.  */
89169695Skan
90169695Skan#ifdef TEST
91169695Skan
92169695Skan#include <stdio.h>
93169695Skan
94169695Skanint
95169695Skanmain (int argc, char **argv)
96169695Skan{
97169695Skan  int c;
98169695Skan  int digit_optind = 0;
99169695Skan
100169695Skan  while (1)
101169695Skan    {
102169695Skan      int this_option_optind = optind ? optind : 1;
103169695Skan      int option_index = 0;
104169695Skan      static struct option long_options[] =
105169695Skan      {
106169695Skan	{"add", 1, 0, 0},
107169695Skan	{"append", 0, 0, 0},
108169695Skan	{"delete", 1, 0, 0},
109169695Skan	{"verbose", 0, 0, 0},
110169695Skan	{"create", 0, 0, 0},
111169695Skan	{"file", 1, 0, 0},
112169695Skan	{0, 0, 0, 0}
113169695Skan      };
114169695Skan
115169695Skan      c = getopt_long (argc, argv, "abc:d:0123456789",
116169695Skan		       long_options, &option_index);
117169695Skan      if (c == -1)
118169695Skan	break;
119169695Skan
120169695Skan      switch (c)
121169695Skan	{
122169695Skan	case 0:
123169695Skan	  printf ("option %s", long_options[option_index].name);
124169695Skan	  if (optarg)
125169695Skan	    printf (" with arg %s", optarg);
126169695Skan	  printf ("\n");
127169695Skan	  break;
128169695Skan
129169695Skan	case '0':
130169695Skan	case '1':
131169695Skan	case '2':
132169695Skan	case '3':
133169695Skan	case '4':
134169695Skan	case '5':
135169695Skan	case '6':
136169695Skan	case '7':
137169695Skan	case '8':
138169695Skan	case '9':
139169695Skan	  if (digit_optind != 0 && digit_optind != this_option_optind)
140169695Skan	    printf ("digits occur in two different argv-elements.\n");
141169695Skan	  digit_optind = this_option_optind;
142169695Skan	  printf ("option %c\n", c);
143169695Skan	  break;
144169695Skan
145169695Skan	case 'a':
146169695Skan	  printf ("option a\n");
147169695Skan	  break;
148169695Skan
149169695Skan	case 'b':
150169695Skan	  printf ("option b\n");
151169695Skan	  break;
152169695Skan
153169695Skan	case 'c':
154169695Skan	  printf ("option c with value `%s'\n", optarg);
155169695Skan	  break;
156169695Skan
157169695Skan	case 'd':
158169695Skan	  printf ("option d with value `%s'\n", optarg);
159169695Skan	  break;
160169695Skan
161169695Skan	case '?':
162169695Skan	  break;
163169695Skan
164169695Skan	default:
165169695Skan	  printf ("?? getopt returned character code 0%o ??\n", c);
166169695Skan	}
167169695Skan    }
168169695Skan
169169695Skan  if (optind < argc)
170169695Skan    {
171169695Skan      printf ("non-option ARGV-elements: ");
172169695Skan      while (optind < argc)
173169695Skan	printf ("%s ", argv[optind++]);
174169695Skan      printf ("\n");
175169695Skan    }
176169695Skan
177169695Skan  exit (0);
178169695Skan}
179169695Skan
180169695Skan#endif /* TEST */
181