1ee3f1543SAxel Dörfler/* getopt_long and getopt_long_only entry points for GNU getopt.
2ee3f1543SAxel Dörfler   Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98
3ee3f1543SAxel Dörfler     Free Software Foundation, Inc.
4ee3f1543SAxel Dörfler   This file is part of the GNU C Library.
5ee3f1543SAxel Dörfler
6ee3f1543SAxel Dörfler   The GNU C Library is free software; you can redistribute it and/or
7ee3f1543SAxel Dörfler   modify it under the terms of the GNU Lesser General Public
8ee3f1543SAxel Dörfler   License as published by the Free Software Foundation; either
9ee3f1543SAxel Dörfler   version 2.1 of the License, or (at your option) any later version.
10ee3f1543SAxel Dörfler
11ee3f1543SAxel Dörfler   The GNU C Library is distributed in the hope that it will be useful,
12ee3f1543SAxel Dörfler   but WITHOUT ANY WARRANTY; without even the implied warranty of
13ee3f1543SAxel Dörfler   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14ee3f1543SAxel Dörfler   Lesser General Public License for more details.
15ee3f1543SAxel Dörfler
16ee3f1543SAxel Dörfler   You should have received a copy of the GNU Lesser General Public
17ee3f1543SAxel Dörfler   License along with the GNU C Library; if not, write to the Free
18ee3f1543SAxel Dörfler   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19ee3f1543SAxel Dörfler   02111-1307 USA.  */
20ee3f1543SAxel Dörfler
21ee3f1543SAxel Dörfler#ifdef HAVE_CONFIG_H
22ee3f1543SAxel Dörfler#include <config.h>
23ee3f1543SAxel Dörfler#endif
24ee3f1543SAxel Dörfler
25ee3f1543SAxel Dörfler#ifdef _LIBC
26ee3f1543SAxel Dörfler# include <getopt.h>
27ee3f1543SAxel Dörfler#else
28ee3f1543SAxel Dörfler# include "getopt.h"
29ee3f1543SAxel Dörfler#endif
30ee3f1543SAxel Dörfler
31ee3f1543SAxel Dörfler#if !defined __STDC__ || !__STDC__
32ee3f1543SAxel Dörfler/* This is a separate conditional since some stdc systems
33ee3f1543SAxel Dörfler   reject `defined (const)'.  */
34ee3f1543SAxel Dörfler#ifndef const
35ee3f1543SAxel Dörfler#define const
36ee3f1543SAxel Dörfler#endif
37ee3f1543SAxel Dörfler#endif
38ee3f1543SAxel Dörfler
39ee3f1543SAxel Dörfler#include <stdio.h>
40ee3f1543SAxel Dörfler
41ee3f1543SAxel Dörfler/* Comment out all this code if we are using the GNU C Library, and are not
42ee3f1543SAxel Dörfler   actually compiling the library itself.  This code is part of the GNU C
43ee3f1543SAxel Dörfler   Library, but also included in many other GNU distributions.  Compiling
44ee3f1543SAxel Dörfler   and linking in this code is a waste when using the GNU C library
45ee3f1543SAxel Dörfler   (especially if it is a shared library).  Rather than having every GNU
46ee3f1543SAxel Dörfler   program understand `configure --with-gnu-libc' and omit the object files,
47ee3f1543SAxel Dörfler   it is simpler to just do this in the source for each such file.  */
48ee3f1543SAxel Dörfler
49ee3f1543SAxel Dörfler#define GETOPT_INTERFACE_VERSION 2
50ee3f1543SAxel Dörfler#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
51ee3f1543SAxel Dörfler#include <gnu-versions.h>
52ee3f1543SAxel Dörfler#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
53ee3f1543SAxel Dörfler#define ELIDE_CODE
54ee3f1543SAxel Dörfler#endif
55ee3f1543SAxel Dörfler#endif
56ee3f1543SAxel Dörfler
57ee3f1543SAxel Dörfler#ifndef ELIDE_CODE
58ee3f1543SAxel Dörfler
59ee3f1543SAxel Dörfler
60ee3f1543SAxel Dörfler/* This needs to come after some library #include
61ee3f1543SAxel Dörfler   to get __GNU_LIBRARY__ defined.  */
62ee3f1543SAxel Dörfler#ifdef __GNU_LIBRARY__
63ee3f1543SAxel Dörfler#include <stdlib.h>
64ee3f1543SAxel Dörfler#endif
65ee3f1543SAxel Dörfler
66ee3f1543SAxel Dörfler#ifndef	NULL
67ee3f1543SAxel Dörfler#define NULL 0
68ee3f1543SAxel Dörfler#endif
69ee3f1543SAxel Dörfler
70ee3f1543SAxel Dörflerint
71ee3f1543SAxel Dörflergetopt_long (argc, argv, options, long_options, opt_index)
72ee3f1543SAxel Dörfler     int argc;
73ee3f1543SAxel Dörfler     char *const *argv;
74ee3f1543SAxel Dörfler     const char *options;
75ee3f1543SAxel Dörfler     const struct option *long_options;
76ee3f1543SAxel Dörfler     int *opt_index;
77ee3f1543SAxel Dörfler{
78ee3f1543SAxel Dörfler  return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
79ee3f1543SAxel Dörfler}
80ee3f1543SAxel Dörfler
81ee3f1543SAxel Dörfler/* Like getopt_long, but '-' as well as '--' can indicate a long option.
82ee3f1543SAxel Dörfler   If an option that starts with '-' (not '--') doesn't match a long option,
83ee3f1543SAxel Dörfler   but does match a short option, it is parsed as a short option
84ee3f1543SAxel Dörfler   instead.  */
85ee3f1543SAxel Dörfler
86ee3f1543SAxel Dörflerint
87ee3f1543SAxel Dörflergetopt_long_only (argc, argv, options, long_options, opt_index)
88ee3f1543SAxel Dörfler     int argc;
89ee3f1543SAxel Dörfler     char *const *argv;
90ee3f1543SAxel Dörfler     const char *options;
91ee3f1543SAxel Dörfler     const struct option *long_options;
92ee3f1543SAxel Dörfler     int *opt_index;
93ee3f1543SAxel Dörfler{
94ee3f1543SAxel Dörfler  return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
95ee3f1543SAxel Dörfler}
96ee3f1543SAxel Dörfler
97ee3f1543SAxel Dörfler# ifdef _LIBC
98ee3f1543SAxel Dörflerlibc_hidden_def (getopt_long)
99ee3f1543SAxel Dörflerlibc_hidden_def (getopt_long_only)
100ee3f1543SAxel Dörfler# endif
101ee3f1543SAxel Dörfler
102ee3f1543SAxel Dörfler#endif	/* Not ELIDE_CODE.  */
103ee3f1543SAxel Dörfler
104ee3f1543SAxel Dörfler#ifdef TEST
105ee3f1543SAxel Dörfler
106ee3f1543SAxel Dörfler#include <stdio.h>
107ee3f1543SAxel Dörfler
108ee3f1543SAxel Dörflerint
109ee3f1543SAxel Dörflermain (argc, argv)
110ee3f1543SAxel Dörfler     int argc;
111ee3f1543SAxel Dörfler     char **argv;
112ee3f1543SAxel Dörfler{
113ee3f1543SAxel Dörfler  int c;
114ee3f1543SAxel Dörfler  int digit_optind = 0;
115ee3f1543SAxel Dörfler
116ee3f1543SAxel Dörfler  while (1)
117ee3f1543SAxel Dörfler    {
118ee3f1543SAxel Dörfler      int this_option_optind = optind ? optind : 1;
119ee3f1543SAxel Dörfler      int option_index = 0;
120ee3f1543SAxel Dörfler      static struct option long_options[] =
121ee3f1543SAxel Dörfler      {
122ee3f1543SAxel Dörfler	{"add", 1, 0, 0},
123ee3f1543SAxel Dörfler	{"append", 0, 0, 0},
124ee3f1543SAxel Dörfler	{"delete", 1, 0, 0},
125ee3f1543SAxel Dörfler	{"verbose", 0, 0, 0},
126ee3f1543SAxel Dörfler	{"create", 0, 0, 0},
127ee3f1543SAxel Dörfler	{"file", 1, 0, 0},
128ee3f1543SAxel Dörfler	{0, 0, 0, 0}
129ee3f1543SAxel Dörfler      };
130ee3f1543SAxel Dörfler
131ee3f1543SAxel Dörfler      c = getopt_long (argc, argv, "abc:d:0123456789",
132ee3f1543SAxel Dörfler		       long_options, &option_index);
133ee3f1543SAxel Dörfler      if (c == -1)
134ee3f1543SAxel Dörfler	break;
135ee3f1543SAxel Dörfler
136ee3f1543SAxel Dörfler      switch (c)
137ee3f1543SAxel Dörfler	{
138ee3f1543SAxel Dörfler	case 0:
139ee3f1543SAxel Dörfler	  printf ("option %s", long_options[option_index].name);
140ee3f1543SAxel Dörfler	  if (optarg)
141ee3f1543SAxel Dörfler	    printf (" with arg %s", optarg);
142ee3f1543SAxel Dörfler	  printf ("\n");
143ee3f1543SAxel Dörfler	  break;
144ee3f1543SAxel Dörfler
145ee3f1543SAxel Dörfler	case '0':
146ee3f1543SAxel Dörfler	case '1':
147ee3f1543SAxel Dörfler	case '2':
148ee3f1543SAxel Dörfler	case '3':
149ee3f1543SAxel Dörfler	case '4':
150ee3f1543SAxel Dörfler	case '5':
151ee3f1543SAxel Dörfler	case '6':
152ee3f1543SAxel Dörfler	case '7':
153ee3f1543SAxel Dörfler	case '8':
154ee3f1543SAxel Dörfler	case '9':
155ee3f1543SAxel Dörfler	  if (digit_optind != 0 && digit_optind != this_option_optind)
156ee3f1543SAxel Dörfler	    printf ("digits occur in two different argv-elements.\n");
157ee3f1543SAxel Dörfler	  digit_optind = this_option_optind;
158ee3f1543SAxel Dörfler	  printf ("option %c\n", c);
159ee3f1543SAxel Dörfler	  break;
160ee3f1543SAxel Dörfler
161ee3f1543SAxel Dörfler	case 'a':
162ee3f1543SAxel Dörfler	  printf ("option a\n");
163ee3f1543SAxel Dörfler	  break;
164ee3f1543SAxel Dörfler
165ee3f1543SAxel Dörfler	case 'b':
166ee3f1543SAxel Dörfler	  printf ("option b\n");
167ee3f1543SAxel Dörfler	  break;
168ee3f1543SAxel Dörfler
169ee3f1543SAxel Dörfler	case 'c':
170ee3f1543SAxel Dörfler	  printf ("option c with value `%s'\n", optarg);
171ee3f1543SAxel Dörfler	  break;
172ee3f1543SAxel Dörfler
173ee3f1543SAxel Dörfler	case 'd':
174ee3f1543SAxel Dörfler	  printf ("option d with value `%s'\n", optarg);
175ee3f1543SAxel Dörfler	  break;
176ee3f1543SAxel Dörfler
177ee3f1543SAxel Dörfler	case '?':
178ee3f1543SAxel Dörfler	  break;
179ee3f1543SAxel Dörfler
180ee3f1543SAxel Dörfler	default:
181ee3f1543SAxel Dörfler	  printf ("?? getopt returned character code 0%o ??\n", c);
182ee3f1543SAxel Dörfler	}
183ee3f1543SAxel Dörfler    }
184ee3f1543SAxel Dörfler
185ee3f1543SAxel Dörfler  if (optind < argc)
186ee3f1543SAxel Dörfler    {
187ee3f1543SAxel Dörfler      printf ("non-option ARGV-elements: ");
188ee3f1543SAxel Dörfler      while (optind < argc)
189ee3f1543SAxel Dörfler	printf ("%s ", argv[optind++]);
190ee3f1543SAxel Dörfler      printf ("\n");
191ee3f1543SAxel Dörfler    }
192ee3f1543SAxel Dörfler
193ee3f1543SAxel Dörfler  exit (0);
194ee3f1543SAxel Dörfler}
195ee3f1543SAxel Dörfler
196ee3f1543SAxel Dörfler#endif /* TEST */
197