Mercurial > hg > xemacs-beta
annotate lib-src/getopt1.c @ 5899:32f4ae21abc5
Collect macro definitions in configure.ac.
author | Stephen J. Turnbull <stephen@xemacs.org> |
---|---|
date | Tue, 05 May 2015 12:45:20 +0900 |
parents | 061f4f90f874 |
children |
rev | line source |
---|---|
428 | 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 Free Software Foundation, Inc. | |
3 | |
4 NOTE: The canonical source of this file is maintained with the GNU C Library. | |
5 Bugs can be reported to bug-glibc@prep.ai.mit.edu. | |
6 | |
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
438
diff
changeset
|
7 This program is free software: you can redistribute it and/or modify it |
428 | 8 under the terms of the GNU General Public License as published by the |
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
438
diff
changeset
|
9 Free Software Foundation, either version 3 of the License, or (at your |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
438
diff
changeset
|
10 option) any later version. |
428 | 11 |
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
438
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
438
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
438
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
438
diff
changeset
|
15 for more details. |
428 | 16 |
17 You should have received a copy of the GNU General Public License | |
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
438
diff
changeset
|
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
428 | 19 |
20 #ifdef HAVE_CONFIG_H | |
438 | 21 #include <config.h> |
428 | 22 #endif |
23 | |
24 #include "getopt.h" | |
25 | |
26 #include <stdio.h> | |
27 | |
28 /* Comment out all this code if we are using the GNU C Library, and are not | |
29 actually compiling the library itself. This code is part of the GNU C | |
30 Library, but also included in many other GNU distributions. Compiling | |
31 and linking in this code is a waste when using the GNU C library | |
32 (especially if it is a shared library). Rather than having every GNU | |
33 program understand `configure --with-gnu-libc' and omit the object files, | |
34 it is simpler to just do this in the source for each such file. */ | |
35 | |
36 #define GETOPT_INTERFACE_VERSION 2 | |
37 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2 | |
38 #include <gnu-versions.h> | |
39 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION | |
40 #define ELIDE_CODE | |
41 #endif | |
42 #endif | |
43 | |
44 #ifndef ELIDE_CODE | |
45 | |
46 | |
47 /* This needs to come after some library #include | |
48 to get __GNU_LIBRARY__ defined. */ | |
49 #ifdef __GNU_LIBRARY__ | |
50 #include <stdlib.h> | |
51 #endif | |
52 | |
53 #ifndef NULL | |
54 #define NULL 0 | |
55 #endif | |
56 | |
57 int | |
58 getopt_long (int argc, char *const *argv, const char *options, | |
59 const struct option *long_options, int *opt_index) | |
60 { | |
61 return _getopt_internal (argc, argv, options, long_options, opt_index, 0); | |
62 } | |
63 | |
64 /* Like getopt_long, but '-' as well as '--' can indicate a long option. | |
65 If an option that starts with '-' (not '--') doesn't match a long option, | |
66 but does match a short option, it is parsed as a short option | |
67 instead. */ | |
68 | |
69 int | |
70 getopt_long_only (int argc, char *const *argv, const char *options, | |
71 const struct option *long_options, int *opt_index) | |
72 { | |
73 return _getopt_internal (argc, argv, options, long_options, opt_index, 1); | |
74 } | |
75 | |
76 | |
77 #endif /* Not ELIDE_CODE. */ | |
78 | |
79 #ifdef TEST | |
80 | |
81 #include <stdio.h> | |
82 | |
83 int | |
84 main (int argc, char **argv) | |
85 { | |
86 int c; | |
87 int digit_optind = 0; | |
88 | |
89 while (1) | |
90 { | |
91 int this_option_optind = optind ? optind : 1; | |
92 int option_index = 0; | |
93 static struct option long_options[] = | |
94 { | |
95 {"add", 1, 0, 0}, | |
96 {"append", 0, 0, 0}, | |
97 {"delete", 1, 0, 0}, | |
98 {"verbose", 0, 0, 0}, | |
99 {"create", 0, 0, 0}, | |
100 {"file", 1, 0, 0}, | |
101 {0, 0, 0, 0} | |
102 }; | |
103 | |
104 c = getopt_long (argc, argv, "abc:d:0123456789", | |
105 long_options, &option_index); | |
106 if (c == -1) | |
107 break; | |
108 | |
109 switch (c) | |
110 { | |
111 case 0: | |
112 printf ("option %s", long_options[option_index].name); | |
113 if (optarg) | |
114 printf (" with arg %s", optarg); | |
115 printf ("\n"); | |
116 break; | |
117 | |
118 case '0': | |
119 case '1': | |
120 case '2': | |
121 case '3': | |
122 case '4': | |
123 case '5': | |
124 case '6': | |
125 case '7': | |
126 case '8': | |
127 case '9': | |
128 if (digit_optind != 0 && digit_optind != this_option_optind) | |
129 printf ("digits occur in two different argv-elements.\n"); | |
130 digit_optind = this_option_optind; | |
131 printf ("option %c\n", c); | |
132 break; | |
133 | |
134 case 'a': | |
135 printf ("option a\n"); | |
136 break; | |
137 | |
138 case 'b': | |
139 printf ("option b\n"); | |
140 break; | |
141 | |
142 case 'c': | |
143 printf ("option c with value `%s'\n", optarg); | |
144 break; | |
145 | |
146 case 'd': | |
147 printf ("option d with value `%s'\n", optarg); | |
148 break; | |
149 | |
150 case '?': | |
151 break; | |
152 | |
153 default: | |
154 printf ("?? getopt returned character code 0%o ??\n", c); | |
155 } | |
156 } | |
157 | |
158 if (optind < argc) | |
159 { | |
160 printf ("non-option ARGV-elements: "); | |
161 while (optind < argc) | |
162 printf ("%s ", argv[optind++]); | |
163 printf ("\n"); | |
164 } | |
165 | |
166 exit (0); | |
167 } | |
168 | |
169 #endif /* TEST */ |