1141cc406Sopenharmony_ci/* getopt_long and getopt_long_only entry points for GNU getopt.
2141cc406Sopenharmony_ci   Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98
3141cc406Sopenharmony_ci     Free Software Foundation, Inc.
4141cc406Sopenharmony_ci   This file is part of the GNU C Library.
5141cc406Sopenharmony_ci
6141cc406Sopenharmony_ci   The GNU C Library is free software; you can redistribute it and/or
7141cc406Sopenharmony_ci   modify it under the terms of the GNU Lesser General Public
8141cc406Sopenharmony_ci   License as published by the Free Software Foundation; either
9141cc406Sopenharmony_ci   version 2.1 of the License, or (at your option) any later version.
10141cc406Sopenharmony_ci
11141cc406Sopenharmony_ci   The GNU C Library is distributed in the hope that it will be useful,
12141cc406Sopenharmony_ci   but WITHOUT ANY WARRANTY; without even the implied warranty of
13141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14141cc406Sopenharmony_ci   Lesser General Public License for more details.
15141cc406Sopenharmony_ci
16141cc406Sopenharmony_ci   You should have received a copy of the GNU Lesser General Public
17141cc406Sopenharmony_ci   License along with the GNU C Library.
18141cc406Sopenharmony_ci   If not, see <https://www.gnu.org/licenses/>.  */
19141cc406Sopenharmony_ci#include <config.h>
20141cc406Sopenharmony_ci#if !defined(HAVE_GETOPT_H) || !defined(HAVE_GETOPT_LONG)
21141cc406Sopenharmony_ci
22141cc406Sopenharmony_ci#ifdef HAVE_CONFIG_H
23141cc406Sopenharmony_ci#include <config.h>
24141cc406Sopenharmony_ci#endif
25141cc406Sopenharmony_ci
26141cc406Sopenharmony_ci#ifdef _LIBC
27141cc406Sopenharmony_ci# include <getopt.h>
28141cc406Sopenharmony_ci#else
29141cc406Sopenharmony_ci# include "../include/lgetopt.h"
30141cc406Sopenharmony_ci#endif
31141cc406Sopenharmony_ci
32141cc406Sopenharmony_ci#if !defined __STDC__ || !__STDC__
33141cc406Sopenharmony_ci/* This is a separate conditional since some stdc systems
34141cc406Sopenharmony_ci   reject `defined (const)'.  */
35141cc406Sopenharmony_ci#ifndef const
36141cc406Sopenharmony_ci#define const
37141cc406Sopenharmony_ci#endif
38141cc406Sopenharmony_ci#endif
39141cc406Sopenharmony_ci
40141cc406Sopenharmony_ci#include <stdio.h>
41141cc406Sopenharmony_ci
42141cc406Sopenharmony_ci/* Comment out all this code if we are using the GNU C Library, and are not
43141cc406Sopenharmony_ci   actually compiling the library itself.  This code is part of the GNU C
44141cc406Sopenharmony_ci   Library, but also included in many other GNU distributions.  Compiling
45141cc406Sopenharmony_ci   and linking in this code is a waste when using the GNU C library
46141cc406Sopenharmony_ci   (especially if it is a shared library).  Rather than having every GNU
47141cc406Sopenharmony_ci   program understand `configure --with-gnu-libc' and omit the object files,
48141cc406Sopenharmony_ci   it is simpler to just do this in the source for each such file.  */
49141cc406Sopenharmony_ci
50141cc406Sopenharmony_ci#define GETOPT_INTERFACE_VERSION 2
51141cc406Sopenharmony_ci#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
52141cc406Sopenharmony_ci#include <gnu-versions.h>
53141cc406Sopenharmony_ci#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
54141cc406Sopenharmony_ci#define ELIDE_CODE
55141cc406Sopenharmony_ci#endif
56141cc406Sopenharmony_ci#endif
57141cc406Sopenharmony_ci
58141cc406Sopenharmony_ci#ifndef ELIDE_CODE
59141cc406Sopenharmony_ci
60141cc406Sopenharmony_ci
61141cc406Sopenharmony_ci/* This needs to come after some library #include
62141cc406Sopenharmony_ci   to get __GNU_LIBRARY__ defined.  */
63141cc406Sopenharmony_ci#ifdef __GNU_LIBRARY__
64141cc406Sopenharmony_ci#include <stdlib.h>
65141cc406Sopenharmony_ci#endif
66141cc406Sopenharmony_ci
67141cc406Sopenharmony_ci#ifndef	NULL
68141cc406Sopenharmony_ci#define NULL 0
69141cc406Sopenharmony_ci#endif
70141cc406Sopenharmony_ci
71141cc406Sopenharmony_ciint
72141cc406Sopenharmony_cigetopt_long (argc, argv, options, long_options, opt_index)
73141cc406Sopenharmony_ci     int argc;
74141cc406Sopenharmony_ci     char *const *argv;
75141cc406Sopenharmony_ci     const char *options;
76141cc406Sopenharmony_ci     const struct option *long_options;
77141cc406Sopenharmony_ci     int *opt_index;
78141cc406Sopenharmony_ci{
79141cc406Sopenharmony_ci  return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
80141cc406Sopenharmony_ci}
81141cc406Sopenharmony_ci
82141cc406Sopenharmony_ci/* Like getopt_long, but '-' as well as '--' can indicate a long option.
83141cc406Sopenharmony_ci   If an option that starts with '-' (not '--') doesn't match a long option,
84141cc406Sopenharmony_ci   but does match a short option, it is parsed as a short option
85141cc406Sopenharmony_ci   instead.  */
86141cc406Sopenharmony_ci
87141cc406Sopenharmony_ciint
88141cc406Sopenharmony_cigetopt_long_only (argc, argv, options, long_options, opt_index)
89141cc406Sopenharmony_ci     int argc;
90141cc406Sopenharmony_ci     char *const *argv;
91141cc406Sopenharmony_ci     const char *options;
92141cc406Sopenharmony_ci     const struct option *long_options;
93141cc406Sopenharmony_ci     int *opt_index;
94141cc406Sopenharmony_ci{
95141cc406Sopenharmony_ci  return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
96141cc406Sopenharmony_ci}
97141cc406Sopenharmony_ci
98141cc406Sopenharmony_ci# ifdef _LIBC
99141cc406Sopenharmony_cilibc_hidden_def (getopt_long)
100141cc406Sopenharmony_cilibc_hidden_def (getopt_long_only)
101141cc406Sopenharmony_ci# endif
102141cc406Sopenharmony_ci
103141cc406Sopenharmony_ci#endif	/* Not ELIDE_CODE.  */
104141cc406Sopenharmony_ci
105141cc406Sopenharmony_ci#ifdef TEST
106141cc406Sopenharmony_ci
107141cc406Sopenharmony_ci#include <stdio.h>
108141cc406Sopenharmony_ci
109141cc406Sopenharmony_ciint
110141cc406Sopenharmony_cimain (argc, argv)
111141cc406Sopenharmony_ci     int argc;
112141cc406Sopenharmony_ci     char **argv;
113141cc406Sopenharmony_ci{
114141cc406Sopenharmony_ci  int c;
115141cc406Sopenharmony_ci  int digit_optind = 0;
116141cc406Sopenharmony_ci
117141cc406Sopenharmony_ci  while (1)
118141cc406Sopenharmony_ci    {
119141cc406Sopenharmony_ci      int this_option_optind = optind ? optind : 1;
120141cc406Sopenharmony_ci      int option_index = 0;
121141cc406Sopenharmony_ci      static struct option long_options[] =
122141cc406Sopenharmony_ci      {
123141cc406Sopenharmony_ci	{"add", 1, 0, 0},
124141cc406Sopenharmony_ci	{"append", 0, 0, 0},
125141cc406Sopenharmony_ci	{"delete", 1, 0, 0},
126141cc406Sopenharmony_ci	{"verbose", 0, 0, 0},
127141cc406Sopenharmony_ci	{"create", 0, 0, 0},
128141cc406Sopenharmony_ci	{"file", 1, 0, 0},
129141cc406Sopenharmony_ci	{0, 0, 0, 0}
130141cc406Sopenharmony_ci      };
131141cc406Sopenharmony_ci
132141cc406Sopenharmony_ci      c = getopt_long (argc, argv, "abc:d:0123456789",
133141cc406Sopenharmony_ci		       long_options, &option_index);
134141cc406Sopenharmony_ci      if (c == -1)
135141cc406Sopenharmony_ci	break;
136141cc406Sopenharmony_ci
137141cc406Sopenharmony_ci      switch (c)
138141cc406Sopenharmony_ci	{
139141cc406Sopenharmony_ci	case 0:
140141cc406Sopenharmony_ci	  printf ("option %s", long_options[option_index].name);
141141cc406Sopenharmony_ci	  if (optarg)
142141cc406Sopenharmony_ci	    printf (" with arg %s", optarg);
143141cc406Sopenharmony_ci	  printf ("\n");
144141cc406Sopenharmony_ci	  break;
145141cc406Sopenharmony_ci
146141cc406Sopenharmony_ci	case '0':
147141cc406Sopenharmony_ci	case '1':
148141cc406Sopenharmony_ci	case '2':
149141cc406Sopenharmony_ci	case '3':
150141cc406Sopenharmony_ci	case '4':
151141cc406Sopenharmony_ci	case '5':
152141cc406Sopenharmony_ci	case '6':
153141cc406Sopenharmony_ci	case '7':
154141cc406Sopenharmony_ci	case '8':
155141cc406Sopenharmony_ci	case '9':
156141cc406Sopenharmony_ci	  if (digit_optind != 0 && digit_optind != this_option_optind)
157141cc406Sopenharmony_ci	    printf ("digits occur in two different argv-elements.\n");
158141cc406Sopenharmony_ci	  digit_optind = this_option_optind;
159141cc406Sopenharmony_ci	  printf ("option %c\n", c);
160141cc406Sopenharmony_ci	  break;
161141cc406Sopenharmony_ci
162141cc406Sopenharmony_ci	case 'a':
163141cc406Sopenharmony_ci	  printf ("option a\n");
164141cc406Sopenharmony_ci	  break;
165141cc406Sopenharmony_ci
166141cc406Sopenharmony_ci	case 'b':
167141cc406Sopenharmony_ci	  printf ("option b\n");
168141cc406Sopenharmony_ci	  break;
169141cc406Sopenharmony_ci
170141cc406Sopenharmony_ci	case 'c':
171141cc406Sopenharmony_ci	  printf ("option c with value `%s'\n", optarg);
172141cc406Sopenharmony_ci	  break;
173141cc406Sopenharmony_ci
174141cc406Sopenharmony_ci	case 'd':
175141cc406Sopenharmony_ci	  printf ("option d with value `%s'\n", optarg);
176141cc406Sopenharmony_ci	  break;
177141cc406Sopenharmony_ci
178141cc406Sopenharmony_ci	case '?':
179141cc406Sopenharmony_ci	  break;
180141cc406Sopenharmony_ci
181141cc406Sopenharmony_ci	default:
182141cc406Sopenharmony_ci	  printf ("?? getopt returned character code 0%o ??\n", c);
183141cc406Sopenharmony_ci	}
184141cc406Sopenharmony_ci    }
185141cc406Sopenharmony_ci
186141cc406Sopenharmony_ci  if (optind < argc)
187141cc406Sopenharmony_ci    {
188141cc406Sopenharmony_ci      printf ("non-option ARGV-elements: ");
189141cc406Sopenharmony_ci      while (optind < argc)
190141cc406Sopenharmony_ci	printf ("%s ", argv[optind++]);
191141cc406Sopenharmony_ci      printf ("\n");
192141cc406Sopenharmony_ci    }
193141cc406Sopenharmony_ci
194141cc406Sopenharmony_ci  exit (0);
195141cc406Sopenharmony_ci}
196141cc406Sopenharmony_ci
197141cc406Sopenharmony_ci#endif /* TEST */
198141cc406Sopenharmony_ci#endif /* HAVE_GETOPT_LONG */
199