OLD | NEW |
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 /* ***** BEGIN LICENSE BLOCK ***** | 2 /* ***** BEGIN LICENSE BLOCK ***** |
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
4 * | 4 * |
5 * The contents of this file are subject to the Mozilla Public License Version | 5 * The contents of this file are subject to the Mozilla Public License Version |
6 * 1.1 (the "License"); you may not use this file except in compliance with | 6 * 1.1 (the "License"); you may not use this file except in compliance with |
7 * the License. You may obtain a copy of the License at | 7 * the License. You may obtain a copy of the License at |
8 * http://www.mozilla.org/MPL/ | 8 * http://www.mozilla.org/MPL/ |
9 * | 9 * |
10 * Software distributed under the License is distributed on an "AS IS" basis, | 10 * Software distributed under the License is distributed on an "AS IS" basis, |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 } | 170 } |
171 } | 171 } |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 /* | 175 /* |
176 ** If we already have a '-' or '--' in hand, xargv points to the next | 176 ** If we already have a '-' or '--' in hand, xargv points to the next |
177 ** option. See if we can find a match in the list of possible | 177 ** option. See if we can find a match in the list of possible |
178 ** options supplied. | 178 ** options supplied. |
179 */ | 179 */ |
180 | |
181 if (internal->minus == 2) | 180 if (internal->minus == 2) |
182 { | 181 { |
183 char * foundEqual = strchr(internal->xargv,'='); | 182 char * foundEqual = strchr(internal->xargv,'='); |
184 PRIntn optNameLen = foundEqual ? (foundEqual - internal->xargv) : | 183 PRIntn optNameLen = foundEqual ? (foundEqual - internal->xargv) : |
185 strlen(internal->xargv); | 184 strlen(internal->xargv); |
186 const PLLongOpt *longOpt = internal->longOpts; | 185 const PLLongOpt *longOpt = internal->longOpts; |
| 186 PLOptStatus result = PL_OPT_BAD; |
187 | 187 |
188 opt->option = 0; | 188 opt->option = 0; |
189 opt->value = NULL; | 189 opt->value = NULL; |
190 | 190 |
191 for (; longOpt->longOptName; ++longOpt) | 191 for (; longOpt->longOptName; ++longOpt) |
192 { | 192 { |
193 if (strncmp(longOpt->longOptName, internal->xargv, optNameLen)) | 193 if (strncmp(longOpt->longOptName, internal->xargv, optNameLen)) |
194 continue; /* not a possible match */ | 194 continue; /* not a possible match */ |
195 if (strlen(longOpt->longOptName) != optNameLen) | 195 if (strlen(longOpt->longOptName) != optNameLen) |
196 continue; /* not a match */ | 196 continue; /* not a match */ |
197 /* option name match */ | 197 /* option name match */ |
198 opt->longOptIndex = longOpt - internal->longOpts; | 198 opt->longOptIndex = longOpt - internal->longOpts; |
199 opt->longOption = longOpt->longOption; | 199 opt->longOption = longOpt->longOption; |
| 200 /* value is part of the current argv[] element if = was found */ |
| 201 /* note: this sets value even for long options that do not |
| 202 * require option if specified as --long=value */ |
200 if (foundEqual) | 203 if (foundEqual) |
201 { | 204 { |
202 opt->value = foundEqual[1] ? foundEqual + 1 : NULL; | 205 opt->value = foundEqual + 1; |
203 } | 206 } |
204 else if (longOpt->valueRequired) | 207 else if (longOpt->valueRequired) |
205 { | 208 { |
206 opt->value = internal->argv[++(internal->xargc)]; | 209 /* value is the next argv[] element, if any */ |
| 210 if (internal->xargc + 1 < internal->argc) |
| 211 { |
| 212 opt->value = internal->argv[++(internal->xargc)]; |
| 213 } |
| 214 /* missing value */ |
| 215 else |
| 216 { |
| 217 break; /* return PL_OPT_BAD */ |
| 218 } |
207 } | 219 } |
208 internal->xargv = &static_Nul; /* consume this */ | 220 result = PL_OPT_OK; |
209 return PL_OPT_OK; | 221 break; |
210 } | 222 } |
211 internal->xargv = &static_Nul; /* consume this */ | 223 internal->xargv = &static_Nul; /* consume this */ |
212 return PL_OPT_BAD; | 224 return result; |
213 } | 225 } |
214 if (internal->minus) | 226 if (internal->minus) |
215 { | 227 { |
216 PRIntn cop; | 228 PRIntn cop; |
217 PRIntn eoo = internal->optionsLen; | 229 PRIntn eoo = internal->optionsLen; |
218 for (cop = 0; cop < eoo; ++cop) | 230 for (cop = 0; cop < eoo; ++cop) |
219 { | 231 { |
220 if (internal->options[cop] == *internal->xargv) | 232 if (internal->options[cop] == *internal->xargv) |
221 { | 233 { |
222 opt->option = *internal->xargv++; | 234 opt->option = *internal->xargv++; |
223 opt->longOption = opt->option & 0xff; | 235 opt->longOption = opt->option & 0xff; |
224 /* | 236 /* |
225 ** if options indicates that there's an associated | 237 ** if options indicates that there's an associated |
226 ** value, this argv is finished and the next is the | 238 ** value, it must be provided, either as part of this |
227 ** option's value. | 239 ** argv[] element or as the next one |
228 */ | 240 */ |
229 if (':' == internal->options[cop + 1]) | 241 if (':' == internal->options[cop + 1]) |
230 { | 242 { |
231 if (0 != *internal->xargv) | 243 /* value is part of the current argv[] element */ |
| 244 if (0 != *internal->xargv) |
| 245 { |
| 246 opt->value = internal->xargv; |
| 247 } |
| 248 /* value is the next argv[] element, if any */ |
| 249 else if (internal->xargc + 1 < internal->argc) |
| 250 { |
| 251 opt->value = internal->argv[++(internal->xargc)]; |
| 252 } |
| 253 /* missing value */ |
| 254 else |
| 255 { |
232 return PL_OPT_BAD; | 256 return PL_OPT_BAD; |
233 opt->value = internal->argv[++(internal->xargc)]; | 257 } |
| 258 |
234 internal->xargv = &static_Nul; | 259 internal->xargv = &static_Nul; |
235 internal->minus = 0; | 260 internal->minus = 0; |
236 } | 261 } |
237 else | 262 else |
238 opt->value = NULL; | 263 opt->value = NULL; |
239 return PL_OPT_OK; | 264 return PL_OPT_OK; |
240 } | 265 } |
241 } | 266 } |
242 internal->xargv += 1; /* consume that option */ | 267 internal->xargv += 1; /* consume that option */ |
243 return PL_OPT_BAD; | 268 return PL_OPT_BAD; |
244 } | 269 } |
| 270 |
245 /* | 271 /* |
246 ** No '-', so it must be a standalone value. The option is nul. | 272 ** No '-', so it must be a standalone value. The option is nul. |
247 */ | 273 */ |
248 opt->value = internal->argv[internal->xargc]; | 274 opt->value = internal->argv[internal->xargc]; |
249 internal->xargv = &static_Nul; | 275 internal->xargv = &static_Nul; |
250 opt->option = 0; | 276 opt->option = 0; |
251 return PL_OPT_OK; | 277 return PL_OPT_OK; |
252 } /* PL_GetNextOpt */ | 278 } /* PL_GetNextOpt */ |
253 | 279 |
254 /* plgetopt.c */ | 280 /* plgetopt.c */ |
OLD | NEW |