// ========== Copyright Header Begin ==========================================
// OpenSPARC T2 Processor File: command_opts.cc
// Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
// The above named program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License version 2 as published by the Free Software Foundation.
// The above named program is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// You should have received a copy of the GNU General Public
// License along with this work; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
// ========== Copyright Header End ============================================
/************************************************************************
** Copyright (C) 2007, Sun Microsystems, Inc.
** Sun considers its source code as an unpublished, proprietary
** trade secret and it is available only under strict license provisions.
** This copyright notice is placed here only to protect Sun in the event
** the source is deemed a published work. Disassembly, decompilation,
** or other means of reducing the object code to human readable form
** is prohibited by the license agreement under which this code is
** provided to the user or company in possession of this copy.
*************************************************************************/
#include "command_opts.h"
Option::Result
Option::parse(const std::string
& cmd_line_arg
,
Option::Result result
= NOT_FOUND
;
if (cmd_line_arg
.substr(1) == name
)
if (seen
&& repeatable
== NOT_REPEATABLE
)
error_msg
= "Duplicate instance of option -" + name
;
error_msg
= "Missing argument for option -" + name
;
if (parse_value(val_str
) != VALUE_FOUND
)
for (std::list
<ExclusiveOptions
*>::iterator excl_opt_iter
=
excl_opt_iter
!= excl_opts
.end();
ExclusiveOptions
* excl_opt
= *excl_opt_iter
;
if (!excl_opt
->update(*this))
error_msg
= excl_opt
->get_error_msg();
bool ExclusiveOptions::update(Option
& found_opt
)
for (std::map
<std::string
,Option
*>::iterator opt_iter
= options
.begin();
opt_iter
!= options
.end();
Option
* opt
= opt_iter
->second
;
if (compatible
== INCOMPATIBLE
&& opt
->is_on())
error_msg
= "Incompatible options -" + opt
->get_name() + " and -" +
found_opt
.get_name() + " set";
opt
->on_off
= Option::OFF
;
bool CommandOptions::parse(int argc
,
std::vector
<std::string
>& positional_args
)
for (ndx
= 0; ndx
< argc
; ++ndx
)
if (argv
[ndx
][1] == '-' && argv
[ndx
][2] == '\0')
const char* val_arg
= (ndx
!= argc
- 1) ? argv
[ndx
+1] : NULL
;
for (std::map
<std::string
,Option
*>::iterator opt_iter
= options
.begin();
opt_iter
!= options
.end();
Option
* opt
= opt_iter
->second
;
Option::Result result
= opt
->parse(argv
[ndx
], val_arg
);
case Option::VALUE_FOUND
:
case Option::ON_OFF_FOUND
:
case Option::PARSE_ERROR
:
error_msg
= opt
->get_error_msg();
ui
->fatal("Internal error -- bad result code: %d\n",
error_msg
= "Illegal option: ";
for (; ndx
< argc
; ++ndx
)
positional_args
.push_back(argv
[ndx
]);
int command_opts_test_max_cpu_ndx
= 15;
int& CpuSet::max_cpu_ndx
= command_opts_test_max_cpu_ndx
;
class CommandOptionsTestCase
virtual ~CommandOptionsTestCase() {}
for (int i
= 0; i
< argc
; ++i
)
ui
->output("%s ", argv
[i
]);
cmd_options
.add(verbose
);
cmd_options
.add(cpu_opt
);
cmd_options
.add(file_opt
);
cmd_options
.add(pa_opt
).add(va_opt
);
excl_opts
.add(pa_opt
).add(va_opt
);
bool result
= cmd_options
.parse(argc
, argv
, pos_args
);
ui
->error("parse failed\n");
ui
->error("verify failed\n");
virtual void print_results()
ui
->error("command line: ");
for (std::vector
<std::string
>::iterator pos_arg_iter
= pos_args
.begin();
pos_arg_iter
!= pos_args
.end();
ui
->output("Arg[%d]: %s\n", pos_ndx
++, pos_arg_iter
->c_str());
virtual bool verify_test() = 0;
CommandOptionsTestCase(std::string args
,
CpuOption::Singleton singleton
= CpuOption::SINGLETON
)
cpu_opt(CpuSet(5), singleton
),
file_opt("file", "a.out"),
std::istringstream
args_stream(args
);
while (args_stream
>> buf
)
argv
[argc
++] = strdup(buf
.c_str());
CommandOptions cmd_options
;
ExclusiveOptions excl_opts
;
std::vector
<std::string
> pos_args
;
class CommandOptionsTestCase1
: public CommandOptionsTestCase
CommandOptionsTestCase1() :
CommandOptionsTestCase("-cpu 0")
virtual ~CommandOptionsTestCase1() {}
virtual bool verify_test()
CpuSet
expected_cpu_set(0);
if (cpu_opt
.get_value() != expected_cpu_set
)
if (file_opt
.get_value() != "a.out")
class CommandOptionsTestCase2
: public CommandOptionsTestCase
CommandOptionsTestCase2() :
CommandOptionsTestCase("-cpu 0", CpuOption::MULTITUDE
)
virtual ~CommandOptionsTestCase2() {}
virtual bool verify_test()
CpuSet
expected_cpu_set(0);
if (cpu_opt
.get_value() != expected_cpu_set
)
if (file_opt
.get_value() != "a.out")
class CommandOptionsTestCase3
: public CommandOptionsTestCase
CommandOptionsTestCase3() :
CommandOptionsTestCase("-cpu 0,2", CpuOption::MULTITUDE
)
virtual ~CommandOptionsTestCase3() {}
virtual bool verify_test()
CpuSet
expected_cpu_set(0);
expected_cpu_set
.insert(2);
if (cpu_opt
.get_value() != expected_cpu_set
)
if (file_opt
.get_value() != "a.out")
class CommandOptionsTestCase4
: public CommandOptionsTestCase
CommandOptionsTestCase4() :
CommandOptionsTestCase("-pa")
virtual ~CommandOptionsTestCase4() {}
virtual bool verify_test()
CpuSet
expected_cpu_set(5);
if (cpu_opt
.get_value() != expected_cpu_set
)
if (file_opt
.get_value() != "a.out")
class CommandOptionsTestCase5
: public CommandOptionsTestCase
CommandOptionsTestCase5() :
CommandOptionsTestCase("-pa -va")
virtual ~CommandOptionsTestCase5() {}
virtual bool verify_test()
CpuSet
expected_cpu_set(5);
if (cpu_opt
.get_value() != expected_cpu_set
)
if (file_opt
.get_value() != "a.out")
class CommandOptionsTestCase6
: public CommandOptionsTestCase
CommandOptionsTestCase6()
CommandOptionsTestCase("-unsigned 0x1234"),
unsigned_opt("unsigned", 10)
cmd_options
.add(unsigned_opt
);
virtual ~CommandOptionsTestCase6() {}
virtual bool verify_test()
CpuSet
expected_cpu_set(5);
if (cpu_opt
.get_value() != expected_cpu_set
)
if (file_opt
.get_value() != "a.out")
if (unsigned_opt
.get_value() != 0x1234)
UnsignedOption unsigned_opt
;
class CommandOptionsTestCase7
: public CommandOptionsTestCase
CommandOptionsTestCase7()
CommandOptionsTestCase("-signed -0x1234"),
cmd_options
.add(signed_opt
);
virtual ~CommandOptionsTestCase7() {}
virtual bool verify_test()
CpuSet
expected_cpu_set(5);
if (cpu_opt
.get_value() != expected_cpu_set
)
if (file_opt
.get_value() != "a.out")
if (signed_opt
.get_value() != -0x1234)
main(int ac
, const char *argv
[])
CommandOptionsTestCase1().run_test();
CommandOptionsTestCase2().run_test();
CommandOptionsTestCase3().run_test();
CommandOptionsTestCase4().run_test();
CommandOptionsTestCase5().run_test();
CommandOptionsTestCase6().run_test();
CommandOptionsTestCase7().run_test();
#endif /* COMMAND_OPTS_TEST */