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