Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | ** File: plgetopt.c |
michael@0 | 8 | ** Description: utilities to parse argc/argv |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "prmem.h" |
michael@0 | 12 | #include "prlog.h" |
michael@0 | 13 | #include "prerror.h" |
michael@0 | 14 | #include "plstr.h" |
michael@0 | 15 | #include "plgetopt.h" |
michael@0 | 16 | |
michael@0 | 17 | #include <string.h> |
michael@0 | 18 | |
michael@0 | 19 | static char static_Nul = 0; |
michael@0 | 20 | |
michael@0 | 21 | struct PLOptionInternal |
michael@0 | 22 | { |
michael@0 | 23 | const char *options; /* client options list specification */ |
michael@0 | 24 | PRIntn argc; /* original number of arguments */ |
michael@0 | 25 | char **argv; /* vector of pointers to arguments */ |
michael@0 | 26 | PRIntn xargc; /* which one we're processing now */ |
michael@0 | 27 | const char *xargv; /* where within *argv[xargc] */ |
michael@0 | 28 | PRIntn minus; /* do we already have the '-'? */ |
michael@0 | 29 | const PLLongOpt *longOpts; /* Caller's array */ |
michael@0 | 30 | PRBool endOfOpts; /* have reached a "--" argument */ |
michael@0 | 31 | PRIntn optionsLen; /* is strlen(options) */ |
michael@0 | 32 | }; |
michael@0 | 33 | |
michael@0 | 34 | /* |
michael@0 | 35 | ** Create the state in which to parse the tokens. |
michael@0 | 36 | ** |
michael@0 | 37 | ** argc the sum of the number of options and their values |
michael@0 | 38 | ** argv the options and their values |
michael@0 | 39 | ** options vector of single character options w/ | w/o ': |
michael@0 | 40 | */ |
michael@0 | 41 | PR_IMPLEMENT(PLOptState*) PL_CreateOptState( |
michael@0 | 42 | PRIntn argc, char **argv, const char *options) |
michael@0 | 43 | { |
michael@0 | 44 | return PL_CreateLongOptState( argc, argv, options, NULL); |
michael@0 | 45 | } /* PL_CreateOptState */ |
michael@0 | 46 | |
michael@0 | 47 | PR_IMPLEMENT(PLOptState*) PL_CreateLongOptState( |
michael@0 | 48 | PRIntn argc, char **argv, const char *options, |
michael@0 | 49 | const PLLongOpt *longOpts) |
michael@0 | 50 | { |
michael@0 | 51 | PLOptState *opt = NULL; |
michael@0 | 52 | PLOptionInternal *internal; |
michael@0 | 53 | |
michael@0 | 54 | if (NULL == options) |
michael@0 | 55 | { |
michael@0 | 56 | PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
michael@0 | 57 | return opt; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | opt = PR_NEWZAP(PLOptState); |
michael@0 | 61 | if (NULL == opt) |
michael@0 | 62 | { |
michael@0 | 63 | PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); |
michael@0 | 64 | return opt; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | internal = PR_NEW(PLOptionInternal); |
michael@0 | 68 | if (NULL == internal) |
michael@0 | 69 | { |
michael@0 | 70 | PR_DELETE(opt); |
michael@0 | 71 | PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); |
michael@0 | 72 | return NULL; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | opt->option = 0; |
michael@0 | 76 | opt->value = NULL; |
michael@0 | 77 | opt->internal = internal; |
michael@0 | 78 | opt->longOption = 0; |
michael@0 | 79 | opt->longOptIndex = -1; |
michael@0 | 80 | |
michael@0 | 81 | internal->argc = argc; |
michael@0 | 82 | internal->argv = argv; |
michael@0 | 83 | internal->xargc = 0; |
michael@0 | 84 | internal->xargv = &static_Nul; |
michael@0 | 85 | internal->minus = 0; |
michael@0 | 86 | internal->options = options; |
michael@0 | 87 | internal->longOpts = longOpts; |
michael@0 | 88 | internal->endOfOpts = PR_FALSE; |
michael@0 | 89 | internal->optionsLen = PL_strlen(options); |
michael@0 | 90 | |
michael@0 | 91 | return opt; |
michael@0 | 92 | } /* PL_CreateLongOptState */ |
michael@0 | 93 | |
michael@0 | 94 | /* |
michael@0 | 95 | ** Destroy object created by CreateOptState() |
michael@0 | 96 | */ |
michael@0 | 97 | PR_IMPLEMENT(void) PL_DestroyOptState(PLOptState *opt) |
michael@0 | 98 | { |
michael@0 | 99 | PR_DELETE(opt->internal); |
michael@0 | 100 | PR_DELETE(opt); |
michael@0 | 101 | } /* PL_DestroyOptState */ |
michael@0 | 102 | |
michael@0 | 103 | PR_IMPLEMENT(PLOptStatus) PL_GetNextOpt(PLOptState *opt) |
michael@0 | 104 | { |
michael@0 | 105 | PLOptionInternal *internal = opt->internal; |
michael@0 | 106 | |
michael@0 | 107 | opt->longOption = 0; |
michael@0 | 108 | opt->longOptIndex = -1; |
michael@0 | 109 | /* |
michael@0 | 110 | ** If the current xarg points to nul, advance to the next |
michael@0 | 111 | ** element of the argv vector. If the vector index is equal |
michael@0 | 112 | ** to argc, we're out of arguments, so return an EOL. |
michael@0 | 113 | ** Note whether the first character of the new argument is |
michael@0 | 114 | ** a '-' and skip by it if it is. |
michael@0 | 115 | */ |
michael@0 | 116 | while (0 == *internal->xargv) |
michael@0 | 117 | { |
michael@0 | 118 | internal->xargc += 1; |
michael@0 | 119 | if (internal->xargc >= internal->argc) |
michael@0 | 120 | { |
michael@0 | 121 | opt->option = 0; |
michael@0 | 122 | opt->value = NULL; |
michael@0 | 123 | return PL_OPT_EOL; |
michael@0 | 124 | } |
michael@0 | 125 | internal->xargv = internal->argv[internal->xargc]; |
michael@0 | 126 | internal->minus = 0; |
michael@0 | 127 | if (!internal->endOfOpts && ('-' == *internal->xargv)) |
michael@0 | 128 | { |
michael@0 | 129 | internal->minus++; |
michael@0 | 130 | internal->xargv++; /* and consume */ |
michael@0 | 131 | if ('-' == *internal->xargv && internal->longOpts) |
michael@0 | 132 | { |
michael@0 | 133 | internal->minus++; |
michael@0 | 134 | internal->xargv++; |
michael@0 | 135 | if (0 == *internal->xargv) |
michael@0 | 136 | { |
michael@0 | 137 | internal->endOfOpts = PR_TRUE; |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | } |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | /* |
michael@0 | 144 | ** If we already have a '-' or '--' in hand, xargv points to the next |
michael@0 | 145 | ** option. See if we can find a match in the list of possible |
michael@0 | 146 | ** options supplied. |
michael@0 | 147 | */ |
michael@0 | 148 | if (internal->minus == 2) |
michael@0 | 149 | { |
michael@0 | 150 | char * foundEqual = strchr(internal->xargv,'='); |
michael@0 | 151 | PRIntn optNameLen = foundEqual ? (foundEqual - internal->xargv) : |
michael@0 | 152 | strlen(internal->xargv); |
michael@0 | 153 | const PLLongOpt *longOpt = internal->longOpts; |
michael@0 | 154 | PLOptStatus result = PL_OPT_BAD; |
michael@0 | 155 | |
michael@0 | 156 | opt->option = 0; |
michael@0 | 157 | opt->value = NULL; |
michael@0 | 158 | |
michael@0 | 159 | for (; longOpt->longOptName; ++longOpt) |
michael@0 | 160 | { |
michael@0 | 161 | if (strncmp(longOpt->longOptName, internal->xargv, optNameLen)) |
michael@0 | 162 | continue; /* not a possible match */ |
michael@0 | 163 | if (strlen(longOpt->longOptName) != optNameLen) |
michael@0 | 164 | continue; /* not a match */ |
michael@0 | 165 | /* option name match */ |
michael@0 | 166 | opt->longOptIndex = longOpt - internal->longOpts; |
michael@0 | 167 | opt->longOption = longOpt->longOption; |
michael@0 | 168 | /* value is part of the current argv[] element if = was found */ |
michael@0 | 169 | /* note: this sets value even for long options that do not |
michael@0 | 170 | * require option if specified as --long=value */ |
michael@0 | 171 | if (foundEqual) |
michael@0 | 172 | { |
michael@0 | 173 | opt->value = foundEqual + 1; |
michael@0 | 174 | } |
michael@0 | 175 | else if (longOpt->valueRequired) |
michael@0 | 176 | { |
michael@0 | 177 | /* value is the next argv[] element, if any */ |
michael@0 | 178 | if (internal->xargc + 1 < internal->argc) |
michael@0 | 179 | { |
michael@0 | 180 | opt->value = internal->argv[++(internal->xargc)]; |
michael@0 | 181 | } |
michael@0 | 182 | /* missing value */ |
michael@0 | 183 | else |
michael@0 | 184 | { |
michael@0 | 185 | break; /* return PL_OPT_BAD */ |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | result = PL_OPT_OK; |
michael@0 | 189 | break; |
michael@0 | 190 | } |
michael@0 | 191 | internal->xargv = &static_Nul; /* consume this */ |
michael@0 | 192 | return result; |
michael@0 | 193 | } |
michael@0 | 194 | if (internal->minus) |
michael@0 | 195 | { |
michael@0 | 196 | PRIntn cop; |
michael@0 | 197 | PRIntn eoo = internal->optionsLen; |
michael@0 | 198 | for (cop = 0; cop < eoo; ++cop) |
michael@0 | 199 | { |
michael@0 | 200 | if (internal->options[cop] == *internal->xargv) |
michael@0 | 201 | { |
michael@0 | 202 | opt->option = *internal->xargv++; |
michael@0 | 203 | opt->longOption = opt->option & 0xff; |
michael@0 | 204 | /* |
michael@0 | 205 | ** if options indicates that there's an associated |
michael@0 | 206 | ** value, it must be provided, either as part of this |
michael@0 | 207 | ** argv[] element or as the next one |
michael@0 | 208 | */ |
michael@0 | 209 | if (':' == internal->options[cop + 1]) |
michael@0 | 210 | { |
michael@0 | 211 | /* value is part of the current argv[] element */ |
michael@0 | 212 | if (0 != *internal->xargv) |
michael@0 | 213 | { |
michael@0 | 214 | opt->value = internal->xargv; |
michael@0 | 215 | } |
michael@0 | 216 | /* value is the next argv[] element, if any */ |
michael@0 | 217 | else if (internal->xargc + 1 < internal->argc) |
michael@0 | 218 | { |
michael@0 | 219 | opt->value = internal->argv[++(internal->xargc)]; |
michael@0 | 220 | } |
michael@0 | 221 | /* missing value */ |
michael@0 | 222 | else |
michael@0 | 223 | { |
michael@0 | 224 | return PL_OPT_BAD; |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | internal->xargv = &static_Nul; |
michael@0 | 228 | internal->minus = 0; |
michael@0 | 229 | } |
michael@0 | 230 | else |
michael@0 | 231 | opt->value = NULL; |
michael@0 | 232 | return PL_OPT_OK; |
michael@0 | 233 | } |
michael@0 | 234 | } |
michael@0 | 235 | internal->xargv += 1; /* consume that option */ |
michael@0 | 236 | return PL_OPT_BAD; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | /* |
michael@0 | 240 | ** No '-', so it must be a standalone value. The option is nul. |
michael@0 | 241 | */ |
michael@0 | 242 | opt->value = internal->argv[internal->xargc]; |
michael@0 | 243 | internal->xargv = &static_Nul; |
michael@0 | 244 | opt->option = 0; |
michael@0 | 245 | return PL_OPT_OK; |
michael@0 | 246 | } /* PL_GetNextOpt */ |
michael@0 | 247 | |
michael@0 | 248 | /* plgetopt.c */ |