1 | /* |
---|
2 | * Replacement implementation of getopt. |
---|
3 | * |
---|
4 | * This is a replacement implementation for getopt based on the my_getopt |
---|
5 | * distribution by Benjamin Sittler. Only the getopt interface is included, |
---|
6 | * since remctl doesn't use GNU long options, and the code has been rearranged |
---|
7 | * and reworked somewhat to fit with my coding style. |
---|
8 | * |
---|
9 | * Copyright 1997, 2000, 2001, 2002 Benjamin Sittler |
---|
10 | * Copyright 2008 Russ Allbery <rra@stanford.edu> |
---|
11 | * |
---|
12 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
13 | * copy of this software and associated documentation files (the "Software"), |
---|
14 | * to deal in the Software without restriction, including without limitation |
---|
15 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
16 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
17 | * Software is furnished to do so, subject to the following conditions: |
---|
18 | * |
---|
19 | * The above copyright notice and this permission notice shall be included in |
---|
20 | * all copies or substantial portions of the Software. |
---|
21 | * |
---|
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
---|
25 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
28 | * DEALINGS IN THE SOFTWARE. |
---|
29 | */ |
---|
30 | |
---|
31 | #include <config.h> |
---|
32 | #include <portable/system.h> |
---|
33 | #include <portable/getopt.h> |
---|
34 | |
---|
35 | /* |
---|
36 | * If we're running the test suite, rename getopt and the global variables to |
---|
37 | * avoid conflicts with the system version. |
---|
38 | */ |
---|
39 | #if TESTING |
---|
40 | # define getopt test_getopt |
---|
41 | int test_getopt(int, char **, const char *); |
---|
42 | # define optind test_optind |
---|
43 | # define opterr test_opterr |
---|
44 | # define optopt test_optopt |
---|
45 | # define optarg test_optarg |
---|
46 | #endif |
---|
47 | |
---|
48 | /* Initialize global interface variables. */ |
---|
49 | int optind = 1; |
---|
50 | int opterr = 1; |
---|
51 | int optopt = 0; |
---|
52 | char *optarg = NULL; |
---|
53 | |
---|
54 | /* |
---|
55 | * This is the plain old UNIX getopt, with GNU-style extensions. If you're |
---|
56 | * porting some piece of UNIX software, this is all you need. It supports |
---|
57 | * GNU-style permutation and optional arguments, but does not support the GNU |
---|
58 | * -W extension. |
---|
59 | * |
---|
60 | * This function is not re-entrant or thread-safe, has static variables, and |
---|
61 | * generally isn't a great interface, but normally you only call it once. |
---|
62 | */ |
---|
63 | int |
---|
64 | getopt(int argc, char *argv[], const char *optstring) |
---|
65 | { |
---|
66 | const char *p; |
---|
67 | size_t offset = 0; |
---|
68 | char mode = '\0'; |
---|
69 | int colon_mode = 0; |
---|
70 | int option = -1; |
---|
71 | |
---|
72 | /* Holds the current position in the parameter being parsed. */ |
---|
73 | static int charind = 0; |
---|
74 | |
---|
75 | /* |
---|
76 | * By default, getopt permutes argv as it scans and leaves all non-options |
---|
77 | * at the end. This can be changed with the first character of optstring |
---|
78 | * or the environment variable POSIXLY_CORRECT. With a first character of |
---|
79 | * '+' or when POSIXLY_CORRECT is set, option processing stops at the |
---|
80 | * first non-option. If the first character is '-', each non-option argv |
---|
81 | * element is handled as if it were the argument of an option with |
---|
82 | * character code 1. mode holds this character. |
---|
83 | * |
---|
84 | * After the optional leading '+' and '-', optstring may contain ':'. If |
---|
85 | * present, missing arguments return ':' instead of '?'. colon_mode holds |
---|
86 | * this setting. |
---|
87 | */ |
---|
88 | if (getenv("POSIXLY_CORRECT") != NULL) { |
---|
89 | mode = '+'; |
---|
90 | colon_mode = '+'; |
---|
91 | } else { |
---|
92 | if (optstring[offset] == '+' || optstring[offset] == '-') { |
---|
93 | mode = optstring[offset]; |
---|
94 | offset++; |
---|
95 | } |
---|
96 | if (optstring[offset] == ':') { |
---|
97 | colon_mode = 1; |
---|
98 | offset++; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | /* |
---|
103 | * charind holds where we left off. If it's set, we were in the middle |
---|
104 | * of an argv element; if not, we pick up with the next element of |
---|
105 | * optind. |
---|
106 | */ |
---|
107 | optarg = NULL; |
---|
108 | if (charind == 0) { |
---|
109 | if (optind >= argc) |
---|
110 | option = -1; |
---|
111 | else if (strcmp(argv[optind], "--") == 0) { |
---|
112 | optind++; |
---|
113 | option = -1; |
---|
114 | } else if (argv[optind][0] != '-' || argv[optind][1] == '\0') { |
---|
115 | char *tmp; |
---|
116 | int i, j, k, end; |
---|
117 | |
---|
118 | if (mode == '+') |
---|
119 | option = -1; |
---|
120 | else if (mode == '-') { |
---|
121 | optarg = argv[optind]; |
---|
122 | optind++; |
---|
123 | option = 1; |
---|
124 | } else { |
---|
125 | for (i = optind + 1, j = optind; i < argc; i++) |
---|
126 | if ((argv[i][0] == '-') && (argv[i][1] != '\0')) { |
---|
127 | optind = i; |
---|
128 | option = getopt(argc, argv, optstring); |
---|
129 | while (i > j) { |
---|
130 | --i; |
---|
131 | tmp = argv[i]; |
---|
132 | end = (charind == 0) ? optind - 1 : optind; |
---|
133 | for (k = i; k + 1 <= end; k++) { |
---|
134 | argv[k] = argv[k + 1]; |
---|
135 | } |
---|
136 | argv[end] = tmp; |
---|
137 | --optind; |
---|
138 | } |
---|
139 | break; |
---|
140 | } |
---|
141 | if (i == argc) |
---|
142 | option = -1; |
---|
143 | } |
---|
144 | return option; |
---|
145 | } else { |
---|
146 | charind = 1; |
---|
147 | } |
---|
148 | } |
---|
149 | if (charind != 0) { |
---|
150 | optopt = argv[optind][charind]; |
---|
151 | for (p = optstring + offset; *p != '\0'; p++) |
---|
152 | if (optopt == *p) { |
---|
153 | p++; |
---|
154 | if (*p == ':') { |
---|
155 | if (argv[optind][charind + 1] != '\0') { |
---|
156 | optarg = &argv[optind][charind + 1]; |
---|
157 | optind++; |
---|
158 | charind = 0; |
---|
159 | } else { |
---|
160 | p++; |
---|
161 | if (*p != ':') { |
---|
162 | charind = 0; |
---|
163 | optind++; |
---|
164 | if (optind >= argc) { |
---|
165 | if (opterr) |
---|
166 | fprintf(stderr, "%s: option requires" |
---|
167 | " an argument -- %c\n", argv[0], |
---|
168 | optopt); |
---|
169 | option = colon_mode ? ':' : '?'; |
---|
170 | goto done; |
---|
171 | } else { |
---|
172 | optarg = argv[optind]; |
---|
173 | optind++; |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | option = optopt; |
---|
179 | } |
---|
180 | if (option == -1) { |
---|
181 | if (opterr) |
---|
182 | fprintf(stderr, "%s: illegal option -- %c\n", argv[0], optopt); |
---|
183 | option = '?'; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | done: |
---|
188 | if (charind != 0) { |
---|
189 | charind++; |
---|
190 | if (argv[optind][charind] == '\0') { |
---|
191 | optind++; |
---|
192 | charind = 0; |
---|
193 | } |
---|
194 | } |
---|
195 | if (optind > argc) |
---|
196 | optind = argc; |
---|
197 | return option; |
---|
198 | } |
---|