Mercurial > hg > octave-kai > gnulib-hg
annotate lib/getopt1.c @ 9084:2932e92d6e31
* lib/version-etc.c (version_etc_va): Default to GPLv3+.
* NEWS: Document this change.
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Tue, 10 Jul 2007 12:22:36 +0000 |
parents | 8a1a9361108c |
children | bbbbbf4cd1c5 |
rev | line source |
---|---|
99 | 1 /* getopt_long and getopt_long_only entry points for GNU getopt. |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
2 Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2004,2006 |
4970
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
3 Free Software Foundation, Inc. |
3337 | 4 This file is part of the GNU C Library. |
9 | 5 |
4020 | 6 This program is free software; you can redistribute it and/or modify |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
921
cb194d51a705
Update from glibc via patch-2.2.93.
Jim Meyering <jim@meyering.net>
parents:
867
diff
changeset
|
10 |
4020 | 11 This program is distributed in the hope that it will be useful, |
1639 | 12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
4020 | 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 GNU General Public License for more details. | |
867 | 15 |
4020 | 16 You should have received a copy of the GNU General Public License along |
17 with this program; if not, write to the Free Software Foundation, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5455
diff
changeset
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
9 | 19 |
3941 | 20 #ifdef _LIBC |
21 # include <getopt.h> | |
22 #else | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
23 # include <config.h> |
3941 | 24 # include "getopt.h" |
25 #endif | |
4970
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
26 #include "getopt_int.h" |
2930 | 27 |
99 | 28 #include <stdio.h> |
29 | |
30 /* This needs to come after some library #include | |
31 to get __GNU_LIBRARY__ defined. */ | |
32 #ifdef __GNU_LIBRARY__ | |
9 | 33 #include <stdlib.h> |
99 | 34 #endif |
9 | 35 |
36 #ifndef NULL | |
37 #define NULL 0 | |
38 #endif | |
39 | |
40 int | |
5455
4e3460b787b0
(getopt_long, getopt_long_only): Arg is char * const *
Paul Eggert <eggert@cs.ucla.edu>
parents:
5453
diff
changeset
|
41 getopt_long (int argc, char *__getopt_argv_const *argv, const char *options, |
4970
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
42 const struct option *long_options, int *opt_index) |
9 | 43 { |
5455
4e3460b787b0
(getopt_long, getopt_long_only): Arg is char * const *
Paul Eggert <eggert@cs.ucla.edu>
parents:
5453
diff
changeset
|
44 return _getopt_internal (argc, (char **) argv, options, long_options, |
4e3460b787b0
(getopt_long, getopt_long_only): Arg is char * const *
Paul Eggert <eggert@cs.ucla.edu>
parents:
5453
diff
changeset
|
45 opt_index, 0, 0); |
9 | 46 } |
47 | |
4970
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
48 int |
5453
d29dafc60cf3
Merge from coreutils to support a more-standard getopt.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5430
diff
changeset
|
49 _getopt_long_r (int argc, char **argv, const char *options, |
4970
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
50 const struct option *long_options, int *opt_index, |
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
51 struct _getopt_data *d) |
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
52 { |
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
53 return _getopt_internal_r (argc, argv, options, long_options, opt_index, |
5453
d29dafc60cf3
Merge from coreutils to support a more-standard getopt.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5430
diff
changeset
|
54 0, 0, d); |
4970
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
55 } |
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
56 |
9 | 57 /* Like getopt_long, but '-' as well as '--' can indicate a long option. |
58 If an option that starts with '-' (not '--') doesn't match a long option, | |
59 but does match a short option, it is parsed as a short option | |
60 instead. */ | |
61 | |
99 | 62 int |
5455
4e3460b787b0
(getopt_long, getopt_long_only): Arg is char * const *
Paul Eggert <eggert@cs.ucla.edu>
parents:
5453
diff
changeset
|
63 getopt_long_only (int argc, char *__getopt_argv_const *argv, |
4e3460b787b0
(getopt_long, getopt_long_only): Arg is char * const *
Paul Eggert <eggert@cs.ucla.edu>
parents:
5453
diff
changeset
|
64 const char *options, |
4970
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
65 const struct option *long_options, int *opt_index) |
9 | 66 { |
5455
4e3460b787b0
(getopt_long, getopt_long_only): Arg is char * const *
Paul Eggert <eggert@cs.ucla.edu>
parents:
5453
diff
changeset
|
67 return _getopt_internal (argc, (char **) argv, options, long_options, |
4e3460b787b0
(getopt_long, getopt_long_only): Arg is char * const *
Paul Eggert <eggert@cs.ucla.edu>
parents:
5453
diff
changeset
|
68 opt_index, 1, 0); |
9 | 69 } |
99 | 70 |
4970
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
71 int |
5453
d29dafc60cf3
Merge from coreutils to support a more-standard getopt.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5430
diff
changeset
|
72 _getopt_long_only_r (int argc, char **argv, const char *options, |
4970
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
73 const struct option *long_options, int *opt_index, |
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
74 struct _getopt_data *d) |
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
75 { |
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
76 return _getopt_internal_r (argc, argv, options, long_options, opt_index, |
5453
d29dafc60cf3
Merge from coreutils to support a more-standard getopt.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5430
diff
changeset
|
77 1, 0, d); |
4970
595e9138157c
Sync getopt and argp from libc CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4653
diff
changeset
|
78 } |
99 | 79 |
9 | 80 |
81 #ifdef TEST | |
82 | |
83 #include <stdio.h> | |
84 | |
85 int | |
4653
2f1666e5968d
Remove K&R cruft in getopt.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4020
diff
changeset
|
86 main (int argc, char **argv) |
9 | 87 { |
88 int c; | |
89 int digit_optind = 0; | |
90 | |
91 while (1) | |
92 { | |
93 int this_option_optind = optind ? optind : 1; | |
94 int option_index = 0; | |
95 static struct option long_options[] = | |
96 { | |
97 {"add", 1, 0, 0}, | |
98 {"append", 0, 0, 0}, | |
99 {"delete", 1, 0, 0}, | |
100 {"verbose", 0, 0, 0}, | |
101 {"create", 0, 0, 0}, | |
102 {"file", 1, 0, 0}, | |
103 {0, 0, 0, 0} | |
104 }; | |
105 | |
106 c = getopt_long (argc, argv, "abc:d:0123456789", | |
107 long_options, &option_index); | |
867 | 108 if (c == -1) |
9 | 109 break; |
110 | |
111 switch (c) | |
112 { | |
113 case 0: | |
114 printf ("option %s", long_options[option_index].name); | |
115 if (optarg) | |
116 printf (" with arg %s", optarg); | |
117 printf ("\n"); | |
118 break; | |
119 | |
120 case '0': | |
121 case '1': | |
122 case '2': | |
123 case '3': | |
124 case '4': | |
125 case '5': | |
126 case '6': | |
127 case '7': | |
128 case '8': | |
129 case '9': | |
130 if (digit_optind != 0 && digit_optind != this_option_optind) | |
131 printf ("digits occur in two different argv-elements.\n"); | |
132 digit_optind = this_option_optind; | |
133 printf ("option %c\n", c); | |
134 break; | |
135 | |
136 case 'a': | |
137 printf ("option a\n"); | |
138 break; | |
139 | |
140 case 'b': | |
141 printf ("option b\n"); | |
142 break; | |
143 | |
144 case 'c': | |
145 printf ("option c with value `%s'\n", optarg); | |
146 break; | |
147 | |
148 case 'd': | |
149 printf ("option d with value `%s'\n", optarg); | |
150 break; | |
151 | |
152 case '?': | |
153 break; | |
154 | |
155 default: | |
156 printf ("?? getopt returned character code 0%o ??\n", c); | |
157 } | |
158 } | |
159 | |
160 if (optind < argc) | |
161 { | |
162 printf ("non-option ARGV-elements: "); | |
163 while (optind < argc) | |
164 printf ("%s ", argv[optind++]); | |
165 printf ("\n"); | |
166 } | |
167 | |
168 exit (0); | |
169 } | |
170 | |
171 #endif /* TEST */ |