Bug 679138: argv past script argument should not be interpreted as options. (r=dvander)

This commit is contained in:
Chris Leary 2011-08-29 12:19:19 -07:00
Родитель 75dbd86553
Коммит ba7ac54f7f
4 изменённых файлов: 32 добавлений и 10 удалений

Просмотреть файл

@ -225,8 +225,8 @@ mjit::Compiler::performCompilation(JITScript **jitp)
JaegerSpew(JSpew_Prof, "compilation took %d us\n", prof.time_us()); JaegerSpew(JSpew_Prof, "compilation took %d us\n", prof.time_us());
#endif #endif
JaegerSpew(JSpew_Scripts, "successfully compiled (code \"%p\") (size \"%ld\")\n", JaegerSpew(JSpew_Scripts, "successfully compiled (code \"%p\") (size \"%u\")\n",
(*jitp)->code.m_code.executableAddress(), (*jitp)->code.m_size); (*jitp)->code.m_code.executableAddress(), unsigned((*jitp)->code.m_size));
return Compile_Okay; return Compile_Okay;
} }

Просмотреть файл

@ -5608,6 +5608,8 @@ main(int argc, char **argv, char **envp)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
op.setArgTerminatesOptions("script", true);
switch (op.parseArgs(argc, argv)) { switch (op.parseArgs(argc, argv)) {
case OptionParser::ParseHelp: case OptionParser::ParseHelp:
return EXIT_SUCCESS; return EXIT_SUCCESS;

Просмотреть файл

@ -84,6 +84,12 @@ OPTION_CONVERT_IMPL(String)
OPTION_CONVERT_IMPL(Int) OPTION_CONVERT_IMPL(Int)
OPTION_CONVERT_IMPL(MultiString) OPTION_CONVERT_IMPL(MultiString)
void
OptionParser::setArgTerminatesOptions(const char *name, bool enabled)
{
findArgument(name)->setTerminatesOptions(enabled);
}
OptionParser::Result OptionParser::Result
OptionParser::error(const char *fmt, ...) OptionParser::error(const char *fmt, ...)
{ {
@ -284,8 +290,11 @@ OptionParser::extractValue(size_t argc, char **argv, size_t *i, char **value)
} }
OptionParser::Result OptionParser::Result
OptionParser::handleOption(Option *opt, size_t argc, char **argv, size_t *i) OptionParser::handleOption(Option *opt, size_t argc, char **argv, size_t *i, bool *optionsAllowed)
{ {
if (opt->getTerminatesOptions())
*optionsAllowed = false;
switch (opt->kind) { switch (opt->kind) {
case OptionKindBool: case OptionKindBool:
{ {
@ -329,12 +338,16 @@ OptionParser::handleOption(Option *opt, size_t argc, char **argv, size_t *i)
} }
OptionParser::Result OptionParser::Result
OptionParser::handleArg(size_t argc, char **argv, size_t *i) OptionParser::handleArg(size_t argc, char **argv, size_t *i, bool *optionsAllowed)
{ {
if (nextArgument >= arguments.length()) if (nextArgument >= arguments.length())
return error("Too many arguments provided"); return error("Too many arguments provided");
Option *arg = arguments[nextArgument]; Option *arg = arguments[nextArgument];
if (arg->getTerminatesOptions())
*optionsAllowed = false;
switch (arg->kind) { switch (arg->kind) {
case OptionKindString: case OptionKindString:
arg->asStringOption()->value = argv[*i]; arg->asStringOption()->value = argv[*i];
@ -357,11 +370,13 @@ OptionParser::parseArgs(int inputArgc, char **argv)
{ {
JS_ASSERT(inputArgc >= 0); JS_ASSERT(inputArgc >= 0);
size_t argc = inputArgc; size_t argc = inputArgc;
/* Permit a "no more options" capability, like |--| offers in many shell interfaces. */
bool optionsAllowed = true;
for (size_t i = 1; i < argc; ++i) { for (size_t i = 1; i < argc; ++i) {
char *arg = argv[i]; char *arg = argv[i];
Result r; Result r;
if (arg[0] == '-') { if (arg[0] == '-' && optionsAllowed) {
/* Option. */ /* Option. */
size_t arglen = strlen(arg); size_t arglen = strlen(arg);
if (arglen < 2) /* Do not permit solo dash option. */ if (arglen < 2) /* Do not permit solo dash option. */
@ -382,10 +397,10 @@ OptionParser::parseArgs(int inputArgc, char **argv)
return error("Invalid short option: %s", arg); return error("Invalid short option: %s", arg);
} }
r = handleOption(opt, argc, argv, &i); r = handleOption(opt, argc, argv, &i, &optionsAllowed);
} else { } else {
/* Argument. */ /* Argument. */
r = handleArg(argc, argv, &i); r = handleArg(argc, argv, &i, &optionsAllowed);
} }
switch (r) { switch (r) {
case Okay: case Okay:

Просмотреть файл

@ -70,13 +70,17 @@ struct Option
const char *help; const char *help;
OptionKind kind; OptionKind kind;
char shortflag; char shortflag;
bool terminatesOptions;
Option(OptionKind kind, char shortflag, const char *longflag, const char *help) Option(OptionKind kind, char shortflag, const char *longflag, const char *help)
: longflag(longflag), help(help), kind(kind), shortflag(shortflag) : longflag(longflag), help(help), kind(kind), shortflag(shortflag), terminatesOptions(false)
{} {}
virtual ~Option() = 0; virtual ~Option() = 0;
void setTerminatesOptions(bool enabled) { terminatesOptions = enabled; }
bool getTerminatesOptions() const { return terminatesOptions; }
virtual bool isValued() const { return false; } virtual bool isValued() const { return false; }
/* Only some valued options are variadic (like MultiStringOptions). */ /* Only some valued options are variadic (like MultiStringOptions). */
@ -248,8 +252,8 @@ class OptionParser
Result error(const char *fmt, ...); Result error(const char *fmt, ...);
Result extractValue(size_t argc, char **argv, size_t *i, char **value); Result extractValue(size_t argc, char **argv, size_t *i, char **value);
Result handleArg(size_t argc, char **argv, size_t *i); Result handleArg(size_t argc, char **argv, size_t *i, bool *optsAllowed);
Result handleOption(Option *opt, size_t argc, char **argv, size_t *i); Result handleOption(Option *opt, size_t argc, char **argv, size_t *i, bool *optsAllowed);
public: public:
explicit OptionParser(const char *usage) explicit OptionParser(const char *usage)
@ -269,6 +273,7 @@ class OptionParser
void setDescriptionWidth(size_t width) { descrWidth = width; } void setDescriptionWidth(size_t width) { descrWidth = width; }
void setDescription(const char *description) { descr = description; } void setDescription(const char *description) { descr = description; }
void setHelpOption(char shortflag, const char *longflag, const char *help); void setHelpOption(char shortflag, const char *longflag, const char *help);
void setArgTerminatesOptions(const char *name, bool enabled);
/* Arguments: no further arguments may be added after a variadic argument. */ /* Arguments: no further arguments may be added after a variadic argument. */