Commit | Line | Data |
---|---|---|
f87489ac WJ |
1 | /* Getopt for GNU. |
2 | Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2, or (at your option) | |
7 | any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
17 | \f | |
18 | #include "getopt.h" | |
19 | ||
20 | #ifdef __STDC__ | |
21 | #define CONST const | |
22 | #else | |
23 | #define CONST | |
24 | #endif | |
25 | ||
26 | #if !defined (NULL) | |
27 | #define NULL 0 | |
28 | #endif | |
29 | ||
30 | int | |
31 | getopt_long (argc, argv, options, long_options, opt_index) | |
32 | int argc; | |
33 | char **argv; | |
34 | CONST char *options; | |
35 | CONST struct option *long_options; | |
36 | int *opt_index; | |
37 | { | |
38 | int val; | |
39 | ||
40 | _getopt_long_options = long_options; | |
41 | val = getopt (argc, argv, options); | |
42 | if (val == 0 && opt_index != NULL) | |
43 | *opt_index = option_index; | |
44 | return val; | |
45 | } | |
46 | ||
47 | /* Like getopt_long, but '-' as well as '+' can indicate a long option. | |
48 | If an option that starts with '-' doesn't match a long option, | |
49 | but does match a short option, it is parsed as a short option | |
50 | instead. */ | |
51 | ||
52 | int | |
53 | getopt_long_only (argc, argv, options, long_options, opt_index) | |
54 | int argc; | |
55 | char **argv; | |
56 | CONST char *options; | |
57 | CONST struct option *long_options; | |
58 | int *opt_index; | |
59 | { | |
60 | int val; | |
61 | ||
62 | _getopt_long_options = long_options; | |
63 | _getopt_long_only = 1; | |
64 | val = getopt (argc, argv, options); | |
65 | if (val == 0 && opt_index != NULL) | |
66 | *opt_index = option_index; | |
67 | return val; | |
68 | } | |
69 | \f | |
70 | ||
71 | #ifdef TEST | |
72 | ||
73 | #include <stdio.h> | |
74 | ||
75 | int | |
76 | main (argc, argv) | |
77 | int argc; | |
78 | char **argv; | |
79 | { | |
80 | int c; | |
81 | int digit_optind = 0; | |
82 | ||
83 | while (1) | |
84 | { | |
85 | int this_option_optind = optind ? optind : 1; | |
86 | char *name = '\0'; | |
87 | int option_index = 0; | |
88 | static struct option long_options[] = | |
89 | { | |
90 | {"add", 1, 0, 0}, | |
91 | {"append", 0, 0, 0}, | |
92 | {"delete", 1, 0, 0}, | |
93 | {"verbose", 0, 0, 0}, | |
94 | {"create", 0, 0, 0}, | |
95 | {"file", 1, 0, 0}, | |
96 | {0, 0, 0, 0} | |
97 | }; | |
98 | ||
99 | c = getopt_long (argc, argv, "abc:d:0123456789", | |
100 | long_options, &option_index); | |
101 | if (c == EOF) | |
102 | break; | |
103 | ||
104 | switch (c) | |
105 | { | |
106 | case 0: | |
107 | printf ("option %s", (long_options[option_index]).name); | |
108 | if (optarg) | |
109 | printf (" with arg %s", optarg); | |
110 | printf ("\n"); | |
111 | break; | |
112 | ||
113 | case '0': | |
114 | case '1': | |
115 | case '2': | |
116 | case '3': | |
117 | case '4': | |
118 | case '5': | |
119 | case '6': | |
120 | case '7': | |
121 | case '8': | |
122 | case '9': | |
123 | if (digit_optind != 0 && digit_optind != this_option_optind) | |
124 | printf ("digits occur in two different argv-elements.\n"); | |
125 | digit_optind = this_option_optind; | |
126 | printf ("option %c\n", c); | |
127 | break; | |
128 | ||
129 | case 'a': | |
130 | printf ("option a\n"); | |
131 | break; | |
132 | ||
133 | case 'b': | |
134 | printf ("option b\n"); | |
135 | break; | |
136 | ||
137 | case 'c': | |
138 | printf ("option c with value `%s'\n", optarg); | |
139 | break; | |
140 | ||
141 | case '?': | |
142 | break; | |
143 | ||
144 | default: | |
145 | printf ("?? getopt returned character code 0%o ??\n", c); | |
146 | } | |
147 | } | |
148 | ||
149 | if (optind < argc) | |
150 | { | |
151 | printf ("non-option ARGV-elements: "); | |
152 | while (optind < argc) | |
153 | printf ("%s ", argv[optind++]); | |
154 | printf ("\n"); | |
155 | } | |
156 | ||
157 | exit (0); | |
158 | } | |
159 | ||
160 | #endif /* TEST */ |