source: vendor/gawk/3.1.5/getopt1.c@ 3076

Last change on this file since 3076 was 3076, checked in by bird, 18 years ago

gawk 3.1.5

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