andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** File: plgetopt.c andre@0: ** Description: utilities to parse argc/argv andre@0: */ andre@0: andre@0: #include "prmem.h" andre@0: #include "prlog.h" andre@0: #include "prerror.h" andre@0: #include "plstr.h" andre@0: #include "plgetopt.h" andre@0: andre@0: #include andre@0: andre@0: static char static_Nul = 0; andre@0: andre@0: struct PLOptionInternal andre@0: { andre@0: const char *options; /* client options list specification */ andre@0: PRIntn argc; /* original number of arguments */ andre@0: char **argv; /* vector of pointers to arguments */ andre@0: PRIntn xargc; /* which one we're processing now */ andre@0: const char *xargv; /* where within *argv[xargc] */ andre@0: PRIntn minus; /* do we already have the '-'? */ andre@0: const PLLongOpt *longOpts; /* Caller's array */ andre@0: PRBool endOfOpts; /* have reached a "--" argument */ andre@0: PRIntn optionsLen; /* is strlen(options) */ andre@0: }; andre@0: andre@0: /* andre@0: ** Create the state in which to parse the tokens. andre@0: ** andre@0: ** argc the sum of the number of options and their values andre@0: ** argv the options and their values andre@0: ** options vector of single character options w/ | w/o ': andre@0: */ andre@0: PR_IMPLEMENT(PLOptState*) PL_CreateOptState( andre@0: PRIntn argc, char **argv, const char *options) andre@0: { andre@0: return PL_CreateLongOptState( argc, argv, options, NULL); andre@0: } /* PL_CreateOptState */ andre@0: andre@0: PR_IMPLEMENT(PLOptState*) PL_CreateLongOptState( andre@0: PRIntn argc, char **argv, const char *options, andre@0: const PLLongOpt *longOpts) andre@0: { andre@0: PLOptState *opt = NULL; andre@0: PLOptionInternal *internal; andre@0: andre@0: if (NULL == options) andre@0: { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return opt; andre@0: } andre@0: andre@0: opt = PR_NEWZAP(PLOptState); andre@0: if (NULL == opt) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return opt; andre@0: } andre@0: andre@0: internal = PR_NEW(PLOptionInternal); andre@0: if (NULL == internal) andre@0: { andre@0: PR_DELETE(opt); andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return NULL; andre@0: } andre@0: andre@0: opt->option = 0; andre@0: opt->value = NULL; andre@0: opt->internal = internal; andre@0: opt->longOption = 0; andre@0: opt->longOptIndex = -1; andre@0: andre@0: internal->argc = argc; andre@0: internal->argv = argv; andre@0: internal->xargc = 0; andre@0: internal->xargv = &static_Nul; andre@0: internal->minus = 0; andre@0: internal->options = options; andre@0: internal->longOpts = longOpts; andre@0: internal->endOfOpts = PR_FALSE; andre@0: internal->optionsLen = PL_strlen(options); andre@0: andre@0: return opt; andre@0: } /* PL_CreateLongOptState */ andre@0: andre@0: /* andre@0: ** Destroy object created by CreateOptState() andre@0: */ andre@0: PR_IMPLEMENT(void) PL_DestroyOptState(PLOptState *opt) andre@0: { andre@0: PR_DELETE(opt->internal); andre@0: PR_DELETE(opt); andre@0: } /* PL_DestroyOptState */ andre@0: andre@0: PR_IMPLEMENT(PLOptStatus) PL_GetNextOpt(PLOptState *opt) andre@0: { andre@0: PLOptionInternal *internal = opt->internal; andre@0: andre@0: opt->longOption = 0; andre@0: opt->longOptIndex = -1; andre@0: /* andre@0: ** If the current xarg points to nul, advance to the next andre@0: ** element of the argv vector. If the vector index is equal andre@0: ** to argc, we're out of arguments, so return an EOL. andre@0: ** Note whether the first character of the new argument is andre@0: ** a '-' and skip by it if it is. andre@0: */ andre@0: while (0 == *internal->xargv) andre@0: { andre@0: internal->xargc += 1; andre@0: if (internal->xargc >= internal->argc) andre@0: { andre@0: opt->option = 0; andre@0: opt->value = NULL; andre@0: return PL_OPT_EOL; andre@0: } andre@0: internal->xargv = internal->argv[internal->xargc]; andre@0: internal->minus = 0; andre@0: if (!internal->endOfOpts && ('-' == *internal->xargv)) andre@0: { andre@0: internal->minus++; andre@0: internal->xargv++; /* and consume */ andre@0: if ('-' == *internal->xargv && internal->longOpts) andre@0: { andre@0: internal->minus++; andre@0: internal->xargv++; andre@0: if (0 == *internal->xargv) andre@0: { andre@0: internal->endOfOpts = PR_TRUE; andre@0: } andre@0: } andre@0: } andre@0: } andre@0: andre@0: /* andre@0: ** If we already have a '-' or '--' in hand, xargv points to the next andre@0: ** option. See if we can find a match in the list of possible andre@0: ** options supplied. andre@0: */ andre@0: if (internal->minus == 2) andre@0: { andre@0: char * foundEqual = strchr(internal->xargv,'='); andre@0: PRIntn optNameLen = foundEqual ? (foundEqual - internal->xargv) : andre@0: strlen(internal->xargv); andre@0: const PLLongOpt *longOpt = internal->longOpts; andre@0: PLOptStatus result = PL_OPT_BAD; andre@0: andre@0: opt->option = 0; andre@0: opt->value = NULL; andre@0: andre@0: for (; longOpt->longOptName; ++longOpt) andre@0: { andre@0: if (strncmp(longOpt->longOptName, internal->xargv, optNameLen)) andre@0: continue; /* not a possible match */ andre@0: if (strlen(longOpt->longOptName) != optNameLen) andre@0: continue; /* not a match */ andre@0: /* option name match */ andre@0: opt->longOptIndex = longOpt - internal->longOpts; andre@0: opt->longOption = longOpt->longOption; andre@0: /* value is part of the current argv[] element if = was found */ andre@0: /* note: this sets value even for long options that do not andre@0: * require option if specified as --long=value */ andre@0: if (foundEqual) andre@0: { andre@0: opt->value = foundEqual + 1; andre@0: } andre@0: else if (longOpt->valueRequired) andre@0: { andre@0: /* value is the next argv[] element, if any */ andre@0: if (internal->xargc + 1 < internal->argc) andre@0: { andre@0: opt->value = internal->argv[++(internal->xargc)]; andre@0: } andre@0: /* missing value */ andre@0: else andre@0: { andre@0: break; /* return PL_OPT_BAD */ andre@0: } andre@0: } andre@0: result = PL_OPT_OK; andre@0: break; andre@0: } andre@0: internal->xargv = &static_Nul; /* consume this */ andre@0: return result; andre@0: } andre@0: if (internal->minus) andre@0: { andre@0: PRIntn cop; andre@0: PRIntn eoo = internal->optionsLen; andre@0: for (cop = 0; cop < eoo; ++cop) andre@0: { andre@0: if (internal->options[cop] == *internal->xargv) andre@0: { andre@0: opt->option = *internal->xargv++; andre@0: opt->longOption = opt->option & 0xff; andre@0: /* andre@0: ** if options indicates that there's an associated andre@0: ** value, it must be provided, either as part of this andre@0: ** argv[] element or as the next one andre@0: */ andre@0: if (':' == internal->options[cop + 1]) andre@0: { andre@0: /* value is part of the current argv[] element */ andre@0: if (0 != *internal->xargv) andre@0: { andre@0: opt->value = internal->xargv; andre@0: } andre@0: /* value is the next argv[] element, if any */ andre@0: else if (internal->xargc + 1 < internal->argc) andre@0: { andre@0: opt->value = internal->argv[++(internal->xargc)]; andre@0: } andre@0: /* missing value */ andre@0: else andre@0: { andre@0: return PL_OPT_BAD; andre@0: } andre@0: andre@0: internal->xargv = &static_Nul; andre@0: internal->minus = 0; andre@0: } andre@0: else andre@0: opt->value = NULL; andre@0: return PL_OPT_OK; andre@0: } andre@0: } andre@0: internal->xargv += 1; /* consume that option */ andre@0: return PL_OPT_BAD; andre@0: } andre@0: andre@0: /* andre@0: ** No '-', so it must be a standalone value. The option is nul. andre@0: */ andre@0: opt->value = internal->argv[internal->xargc]; andre@0: internal->xargv = &static_Nul; andre@0: opt->option = 0; andre@0: return PL_OPT_OK; andre@0: } /* PL_GetNextOpt */ andre@0: andre@0: /* plgetopt.c */