2016-11-26 13:23:44 +08:00
//===-- CommandObjectCommands.cpp -------------------------------*- C++ -*-===//
2010-07-07 11:36:20 +08:00
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// C Includes
// C++ Includes
// Other libraries and framework includes
2011-04-21 06:55:21 +08:00
# include "llvm/ADT/StringRef.h"
2010-07-07 11:36:20 +08:00
// Project includes
2016-02-23 07:46:47 +08:00
# include "CommandObjectCommands.h"
2016-03-01 07:22:53 +08:00
# include "CommandObjectHelp.h"
2010-07-07 11:36:20 +08:00
# include "lldb/Core/Debugger.h"
2014-01-28 07:43:24 +08:00
# include "lldb/Core/IOHandler.h"
2017-03-23 07:33:16 +08:00
# include "lldb/Host/OptionParser.h"
2013-06-18 06:51:50 +08:00
# include "lldb/Interpreter/CommandHistory.h"
2010-07-07 11:36:20 +08:00
# include "lldb/Interpreter/CommandInterpreter.h"
2011-04-21 00:37:46 +08:00
# include "lldb/Interpreter/CommandObjectRegexCommand.h"
2010-07-07 11:36:20 +08:00
# include "lldb/Interpreter/CommandReturnObject.h"
2018-04-10 17:03:59 +08:00
# include "lldb/Interpreter/OptionArgParser.h"
2013-06-11 09:26:35 +08:00
# include "lldb/Interpreter/OptionValueBoolean.h"
2016-03-31 09:10:54 +08:00
# include "lldb/Interpreter/OptionValueString.h"
2013-06-18 06:51:50 +08:00
# include "lldb/Interpreter/OptionValueUInt64.h"
2010-07-07 11:36:20 +08:00
# include "lldb/Interpreter/Options.h"
2011-08-17 09:30:04 +08:00
# include "lldb/Interpreter/ScriptInterpreter.h"
2018-04-18 02:53:35 +08:00
# include "lldb/Utility/Args.h"
2017-03-22 02:25:04 +08:00
# include "lldb/Utility/StringList.h"
2010-07-07 11:36:20 +08:00
using namespace lldb ;
using namespace lldb_private ;
2011-07-12 11:12:18 +08:00
//-------------------------------------------------------------------------
// CommandObjectCommandsSource
//-------------------------------------------------------------------------
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
static OptionDefinition g_history_options [ ] = {
// clang-format off
{ LLDB_OPT_SET_1 , false , " count " , ' c ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypeUnsignedInteger , " How many history commands to print. " } ,
{ LLDB_OPT_SET_1 , false , " start-index " , ' s ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypeUnsignedInteger , " Index at which to start printing history commands (or end to mean tail mode). " } ,
{ LLDB_OPT_SET_1 , false , " end-index " , ' e ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypeUnsignedInteger , " Index at which to stop printing history commands. " } ,
{ LLDB_OPT_SET_2 , false , " clear " , ' C ' , OptionParser : : eNoArgument , nullptr , nullptr , 0 , eArgTypeBoolean , " Clears the current command history. " } ,
// clang-format on
} ;
2016-09-07 04:57:50 +08:00
class CommandObjectCommandsHistory : public CommandObjectParsed {
2012-06-09 05:56:10 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectCommandsHistory ( CommandInterpreter & interpreter )
: CommandObjectParsed ( interpreter , " command history " ,
2017-04-20 07:21:04 +08:00
" Dump the history of commands in this session. \n "
" Commands in the history list can be run again "
" using \" !<INDEX> \" . \" !-<OFFSET> \" will re-run "
" the command that is <OFFSET> commands from the end "
" of the list (counting the current command). " ,
2016-02-23 07:46:47 +08:00
nullptr ) ,
2016-09-07 04:57:50 +08:00
m_options ( ) { }
2012-06-09 05:56:10 +08:00
2016-09-07 04:57:50 +08:00
~ CommandObjectCommandsHistory ( ) override = default ;
2012-06-09 05:56:10 +08:00
2016-09-07 04:57:50 +08:00
Options * GetOptions ( ) override { return & m_options ; }
2012-06-09 05:56:10 +08:00
protected :
2016-09-07 04:57:50 +08:00
class CommandOptions : public Options {
public :
CommandOptions ( )
: Options ( ) , m_start_idx ( 0 ) , m_stop_idx ( 0 ) , m_count ( 0 ) , m_clear ( false ) {
}
2011-07-12 11:12:18 +08:00
2016-09-07 04:57:50 +08:00
~ CommandOptions ( ) override = default ;
2017-05-12 12:51:55 +08:00
Status SetOptionValue ( uint32_t option_idx , llvm : : StringRef option_arg ,
ExecutionContext * execution_context ) override {
Status error ;
2016-09-07 04:57:50 +08:00
const int short_option = m_getopt_table [ option_idx ] . val ;
switch ( short_option ) {
case ' c ' :
2016-11-13 00:56:47 +08:00
error = m_count . SetValueFromString ( option_arg , eVarSetOperationAssign ) ;
2016-09-07 04:57:50 +08:00
break ;
case ' s ' :
2016-11-13 00:56:47 +08:00
if ( option_arg = = " end " ) {
2016-09-07 04:57:50 +08:00
m_start_idx . SetCurrentValue ( UINT64_MAX ) ;
m_start_idx . SetOptionWasSet ( ) ;
} else
2016-11-13 00:56:47 +08:00
error = m_start_idx . SetValueFromString ( option_arg ,
2016-09-07 04:57:50 +08:00
eVarSetOperationAssign ) ;
break ;
case ' e ' :
2016-11-13 00:56:47 +08:00
error =
m_stop_idx . SetValueFromString ( option_arg , eVarSetOperationAssign ) ;
2016-09-07 04:57:50 +08:00
break ;
case ' C ' :
m_clear . SetCurrentValue ( true ) ;
m_clear . SetOptionWasSet ( ) ;
break ;
default :
error . SetErrorStringWithFormat ( " unrecognized option '%c' " ,
short_option ) ;
break ;
}
return error ;
}
2011-07-12 11:12:18 +08:00
2016-09-07 04:57:50 +08:00
void OptionParsingStarting ( ExecutionContext * execution_context ) override {
m_start_idx . Clear ( ) ;
m_stop_idx . Clear ( ) ;
m_count . Clear ( ) ;
m_clear . Clear ( ) ;
}
2011-07-12 11:12:18 +08:00
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
llvm : : ArrayRef < OptionDefinition > GetDefinitions ( ) override {
2016-09-23 05:06:13 +08:00
return llvm : : makeArrayRef ( g_history_options ) ;
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
}
2016-09-07 04:57:50 +08:00
// Instance variables to hold the values for command options.
OptionValueUInt64 m_start_idx ;
OptionValueUInt64 m_stop_idx ;
OptionValueUInt64 m_count ;
OptionValueBoolean m_clear ;
} ;
bool DoExecute ( Args & command , CommandReturnObject & result ) override {
if ( m_options . m_clear . GetCurrentValue ( ) & &
m_options . m_clear . OptionWasSet ( ) ) {
m_interpreter . GetCommandHistory ( ) . Clear ( ) ;
result . SetStatus ( lldb : : eReturnStatusSuccessFinishNoResult ) ;
} else {
if ( m_options . m_start_idx . OptionWasSet ( ) & &
m_options . m_stop_idx . OptionWasSet ( ) & &
m_options . m_count . OptionWasSet ( ) ) {
result . AppendError ( " --count, --start-index and --end-index cannot be "
" all specified in the same invocation " ) ;
result . SetStatus ( lldb : : eReturnStatusFailed ) ;
} else {
std : : pair < bool , uint64_t > start_idx (
m_options . m_start_idx . OptionWasSet ( ) ,
m_options . m_start_idx . GetCurrentValue ( ) ) ;
std : : pair < bool , uint64_t > stop_idx (
m_options . m_stop_idx . OptionWasSet ( ) ,
m_options . m_stop_idx . GetCurrentValue ( ) ) ;
std : : pair < bool , uint64_t > count ( m_options . m_count . OptionWasSet ( ) ,
m_options . m_count . GetCurrentValue ( ) ) ;
const CommandHistory & history ( m_interpreter . GetCommandHistory ( ) ) ;
if ( start_idx . first & & start_idx . second = = UINT64_MAX ) {
if ( count . first ) {
start_idx . second = history . GetSize ( ) - count . second ;
stop_idx . second = history . GetSize ( ) - 1 ;
} else if ( stop_idx . first ) {
start_idx . second = stop_idx . second ;
stop_idx . second = history . GetSize ( ) - 1 ;
} else {
start_idx . second = 0 ;
stop_idx . second = history . GetSize ( ) - 1 ;
}
} else {
if ( ! start_idx . first & & ! stop_idx . first & & ! count . first ) {
start_idx . second = 0 ;
stop_idx . second = history . GetSize ( ) - 1 ;
} else if ( start_idx . first ) {
if ( count . first ) {
stop_idx . second = start_idx . second + count . second - 1 ;
} else if ( ! stop_idx . first ) {
stop_idx . second = history . GetSize ( ) - 1 ;
2013-06-18 06:51:50 +08:00
}
2016-09-07 04:57:50 +08:00
} else if ( stop_idx . first ) {
if ( count . first ) {
if ( stop_idx . second > = count . second )
start_idx . second = stop_idx . second - count . second + 1 ;
else
start_idx . second = 0 ;
2013-06-18 06:51:50 +08:00
}
2016-09-07 04:57:50 +08:00
} else /* if (count.first) */
{
start_idx . second = 0 ;
stop_idx . second = count . second - 1 ;
}
}
history . Dump ( result . GetOutputStream ( ) , start_idx . second ,
stop_idx . second ) ;
}
2011-07-12 11:12:18 +08:00
}
2016-09-07 04:57:50 +08:00
return result . Succeeded ( ) ;
}
2012-06-09 05:56:10 +08:00
2016-09-07 04:57:50 +08:00
CommandOptions m_options ;
2011-07-12 11:12:18 +08:00
} ;
2010-07-07 11:36:20 +08:00
//-------------------------------------------------------------------------
// CommandObjectCommandsSource
//-------------------------------------------------------------------------
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
static OptionDefinition g_source_options [ ] = {
// clang-format off
{ LLDB_OPT_SET_ALL , false , " stop-on-error " , ' e ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypeBoolean , " If true, stop executing commands on error. " } ,
{ LLDB_OPT_SET_ALL , false , " stop-on-continue " , ' c ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypeBoolean , " If true, stop executing commands on continue. " } ,
{ LLDB_OPT_SET_ALL , false , " silent-run " , ' s ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypeBoolean , " If true don't echo commands while executing. " } ,
// clang-format on
} ;
2016-09-07 04:57:50 +08:00
class CommandObjectCommandsSource : public CommandObjectParsed {
2012-06-09 05:56:10 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectCommandsSource ( CommandInterpreter & interpreter )
: CommandObjectParsed (
interpreter , " command source " ,
" Read and execute LLDB commands from the file <filename>. " ,
nullptr ) ,
m_options ( ) {
CommandArgumentEntry arg ;
CommandArgumentData file_arg ;
// Define the first (and only) variant of this arg.
file_arg . arg_type = eArgTypeFilename ;
file_arg . arg_repetition = eArgRepeatPlain ;
// There is only one variant this argument could be; put it into the
// argument entry.
arg . push_back ( file_arg ) ;
// Push the data for the first argument into the m_arguments vector.
m_arguments . push_back ( arg ) ;
}
~ CommandObjectCommandsSource ( ) override = default ;
const char * GetRepeatCommand ( Args & current_command_args ,
uint32_t index ) override {
return " " ;
}
Refactoring for for the internal command line completion API (NFC)
Summary:
This patch refactors the internal completion API. It now takes (as far as possible) a single
CompletionRequest object instead o half a dozen in/out/in-out parameters. The CompletionRequest
contains a common superset of the different parameters as far as it makes sense. This includes
the raw command line string and raw cursor position, which should make the `expr` command
possible to implement (at least without hacks that reconstruct the command line from the args).
This patch is not intended to change the observable behavior of lldb in any way. It's also as
minimal as possible and doesn't attempt to fix all the problems the API has.
Some Q&A:
Q: Why is this not fixing all the problems in the completion API?
A: Because is a blocker for the expr command completion which I want to get in ASAP. This is the
smallest patch that unblocks the expr completion patch and which allows trivial refactoring in the future.
The patch also doesn't really change the internal information flow in the API, so that hopefully
saves us from ever having to revert and resubmit this humongous patch.
Q: Can we merge all the copy-pasted code in the completion methods
(like computing the current incomplete arg) into CompletionRequest class?
A: Yes, but it's out of scope for this patch.
Q: Why the `word_complete = request.GetWordComplete(); ... ` pattern?
A: I don't want to add a getter that returns a reference to the internal integer. So we have
to use a temporary variable and the Getter/Setter instead. We don't throw exceptions
from what I can tell, so the behavior doesn't change.
Q: Why are we not owning the list of matches?
A: Because that's how the previous API works. But that should be fixed too (in another patch).
Q: Can we make the constructor simpler and compute some of the values from the plain command?
A: I think this works, but I rather want to have this in a follow up commit. Especially when making nested
request it's a bit awkward that the parsed arguments behave as both input/output (as we should in theory
propagate the changes on the nested request back to the parent request if we don't want to change the
behavior too much).
Q: Can't we pass one const request object and then just return another result object instead of mixing
them together in one in/out parameter?
A: It's hard to get keep the same behavior with that pattern, but I think we can also get a nice API with just
a single request object. If we make all input parameters read-only, we have a clear separation between what
is actually an input and what an output parameter (and hopefully we get rid of the in-out parameters).
Q: Can we throw out the 'match' variables that are not implemented according to the comment?
A: We currently just forward them as in the old code to the different methods, even though I think
they are really not used. We can easily remove and readd them once every single completion method just
takes a CompletionRequest, but for now I prefer NFC behavior from the perspective of the API user.
Reviewers: davide, jingham, labath
Reviewed By: jingham
Subscribers: mgorny, friss, lldb-commits
Differential Revision: https://reviews.llvm.org/D48796
llvm-svn: 336146
2018-07-03 05:29:56 +08:00
int HandleArgumentCompletion (
CompletionRequest & request ,
OptionElementVector & opt_element_vector ) override {
auto completion_str = request . GetParsedLine ( ) [ request . GetCursorIndex ( ) ] . ref ;
completion_str = completion_str . take_front ( request . GetCursorCharPosition ( ) ) ;
2016-09-07 04:57:50 +08:00
Refactoring for for the internal command line completion API (NFC)
Summary:
This patch refactors the internal completion API. It now takes (as far as possible) a single
CompletionRequest object instead o half a dozen in/out/in-out parameters. The CompletionRequest
contains a common superset of the different parameters as far as it makes sense. This includes
the raw command line string and raw cursor position, which should make the `expr` command
possible to implement (at least without hacks that reconstruct the command line from the args).
This patch is not intended to change the observable behavior of lldb in any way. It's also as
minimal as possible and doesn't attempt to fix all the problems the API has.
Some Q&A:
Q: Why is this not fixing all the problems in the completion API?
A: Because is a blocker for the expr command completion which I want to get in ASAP. This is the
smallest patch that unblocks the expr completion patch and which allows trivial refactoring in the future.
The patch also doesn't really change the internal information flow in the API, so that hopefully
saves us from ever having to revert and resubmit this humongous patch.
Q: Can we merge all the copy-pasted code in the completion methods
(like computing the current incomplete arg) into CompletionRequest class?
A: Yes, but it's out of scope for this patch.
Q: Why the `word_complete = request.GetWordComplete(); ... ` pattern?
A: I don't want to add a getter that returns a reference to the internal integer. So we have
to use a temporary variable and the Getter/Setter instead. We don't throw exceptions
from what I can tell, so the behavior doesn't change.
Q: Why are we not owning the list of matches?
A: Because that's how the previous API works. But that should be fixed too (in another patch).
Q: Can we make the constructor simpler and compute some of the values from the plain command?
A: I think this works, but I rather want to have this in a follow up commit. Especially when making nested
request it's a bit awkward that the parsed arguments behave as both input/output (as we should in theory
propagate the changes on the nested request back to the parent request if we don't want to change the
behavior too much).
Q: Can't we pass one const request object and then just return another result object instead of mixing
them together in one in/out parameter?
A: It's hard to get keep the same behavior with that pattern, but I think we can also get a nice API with just
a single request object. If we make all input parameters read-only, we have a clear separation between what
is actually an input and what an output parameter (and hopefully we get rid of the in-out parameters).
Q: Can we throw out the 'match' variables that are not implemented according to the comment?
A: We currently just forward them as in the old code to the different methods, even though I think
they are really not used. We can easily remove and readd them once every single completion method just
takes a CompletionRequest, but for now I prefer NFC behavior from the perspective of the API user.
Reviewers: davide, jingham, labath
Reviewed By: jingham
Subscribers: mgorny, friss, lldb-commits
Differential Revision: https://reviews.llvm.org/D48796
llvm-svn: 336146
2018-07-03 05:29:56 +08:00
bool word_complete = request . GetWordComplete ( ) ;
2016-09-07 04:57:50 +08:00
CommandCompletions : : InvokeCommonCompletionCallbacks (
GetCommandInterpreter ( ) , CommandCompletions : : eDiskFileCompletion ,
Refactoring for for the internal command line completion API (NFC)
Summary:
This patch refactors the internal completion API. It now takes (as far as possible) a single
CompletionRequest object instead o half a dozen in/out/in-out parameters. The CompletionRequest
contains a common superset of the different parameters as far as it makes sense. This includes
the raw command line string and raw cursor position, which should make the `expr` command
possible to implement (at least without hacks that reconstruct the command line from the args).
This patch is not intended to change the observable behavior of lldb in any way. It's also as
minimal as possible and doesn't attempt to fix all the problems the API has.
Some Q&A:
Q: Why is this not fixing all the problems in the completion API?
A: Because is a blocker for the expr command completion which I want to get in ASAP. This is the
smallest patch that unblocks the expr completion patch and which allows trivial refactoring in the future.
The patch also doesn't really change the internal information flow in the API, so that hopefully
saves us from ever having to revert and resubmit this humongous patch.
Q: Can we merge all the copy-pasted code in the completion methods
(like computing the current incomplete arg) into CompletionRequest class?
A: Yes, but it's out of scope for this patch.
Q: Why the `word_complete = request.GetWordComplete(); ... ` pattern?
A: I don't want to add a getter that returns a reference to the internal integer. So we have
to use a temporary variable and the Getter/Setter instead. We don't throw exceptions
from what I can tell, so the behavior doesn't change.
Q: Why are we not owning the list of matches?
A: Because that's how the previous API works. But that should be fixed too (in another patch).
Q: Can we make the constructor simpler and compute some of the values from the plain command?
A: I think this works, but I rather want to have this in a follow up commit. Especially when making nested
request it's a bit awkward that the parsed arguments behave as both input/output (as we should in theory
propagate the changes on the nested request back to the parent request if we don't want to change the
behavior too much).
Q: Can't we pass one const request object and then just return another result object instead of mixing
them together in one in/out parameter?
A: It's hard to get keep the same behavior with that pattern, but I think we can also get a nice API with just
a single request object. If we make all input parameters read-only, we have a clear separation between what
is actually an input and what an output parameter (and hopefully we get rid of the in-out parameters).
Q: Can we throw out the 'match' variables that are not implemented according to the comment?
A: We currently just forward them as in the old code to the different methods, even though I think
they are really not used. We can easily remove and readd them once every single completion method just
takes a CompletionRequest, but for now I prefer NFC behavior from the perspective of the API user.
Reviewers: davide, jingham, labath
Reviewed By: jingham
Subscribers: mgorny, friss, lldb-commits
Differential Revision: https://reviews.llvm.org/D48796
llvm-svn: 336146
2018-07-03 05:29:56 +08:00
completion_str , request . GetMatchStartPoint ( ) ,
request . GetMaxReturnElements ( ) , nullptr , word_complete ,
request . GetMatches ( ) ) ;
request . SetWordComplete ( word_complete ) ;
return request . GetMatches ( ) . GetSize ( ) ;
2016-09-07 04:57:50 +08:00
}
Options * GetOptions ( ) override { return & m_options ; }
2012-06-09 05:56:10 +08:00
2016-09-07 04:57:50 +08:00
protected :
class CommandOptions : public Options {
public :
CommandOptions ( )
: Options ( ) , m_stop_on_error ( true ) , m_silent_run ( false ) ,
m_stop_on_continue ( true ) { }
~ CommandOptions ( ) override = default ;
2017-05-12 12:51:55 +08:00
Status SetOptionValue ( uint32_t option_idx , llvm : : StringRef option_arg ,
ExecutionContext * execution_context ) override {
Status error ;
2016-09-07 04:57:50 +08:00
const int short_option = m_getopt_table [ option_idx ] . val ;
switch ( short_option ) {
case ' e ' :
2016-11-13 00:56:47 +08:00
error = m_stop_on_error . SetValueFromString ( option_arg ) ;
2016-09-07 04:57:50 +08:00
break ;
case ' c ' :
2016-11-13 00:56:47 +08:00
error = m_stop_on_continue . SetValueFromString ( option_arg ) ;
2016-09-07 04:57:50 +08:00
break ;
case ' s ' :
2016-11-13 00:56:47 +08:00
error = m_silent_run . SetValueFromString ( option_arg ) ;
2016-09-07 04:57:50 +08:00
break ;
default :
error . SetErrorStringWithFormat ( " unrecognized option '%c' " ,
short_option ) ;
break ;
}
return error ;
2012-06-09 05:56:10 +08:00
}
2016-09-07 04:57:50 +08:00
void OptionParsingStarting ( ExecutionContext * execution_context ) override {
m_stop_on_error . Clear ( ) ;
m_silent_run . Clear ( ) ;
m_stop_on_continue . Clear ( ) ;
2012-06-09 05:56:10 +08:00
}
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
llvm : : ArrayRef < OptionDefinition > GetDefinitions ( ) override {
2016-09-23 05:06:13 +08:00
return llvm : : makeArrayRef ( g_source_options ) ;
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
}
2016-09-07 04:57:50 +08:00
// Instance variables to hold the values for command options.
OptionValueBoolean m_stop_on_error ;
OptionValueBoolean m_silent_run ;
OptionValueBoolean m_stop_on_continue ;
} ;
bool DoExecute ( Args & command , CommandReturnObject & result ) override {
2016-12-08 09:31:04 +08:00
if ( command . GetArgumentCount ( ) ! = 1 ) {
2016-09-07 04:57:50 +08:00
result . AppendErrorWithFormat (
" '%s' takes exactly one executable filename argument. \n " ,
2016-10-06 05:14:38 +08:00
GetCommandName ( ) . str ( ) . c_str ( ) ) ;
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
2016-12-08 09:31:04 +08:00
return false ;
}
FileSpec cmd_file ( command [ 0 ] . ref , true ) ;
ExecutionContext * exe_ctx = nullptr ; // Just use the default context.
// If any options were set, then use them
if ( m_options . m_stop_on_error . OptionWasSet ( ) | |
m_options . m_silent_run . OptionWasSet ( ) | |
m_options . m_stop_on_continue . OptionWasSet ( ) ) {
// Use user set settings
CommandInterpreterRunOptions options ;
options . SetStopOnContinue ( m_options . m_stop_on_continue . GetCurrentValue ( ) ) ;
options . SetStopOnError ( m_options . m_stop_on_error . GetCurrentValue ( ) ) ;
options . SetEchoCommands ( ! m_options . m_silent_run . GetCurrentValue ( ) ) ;
options . SetPrintResults ( ! m_options . m_silent_run . GetCurrentValue ( ) ) ;
m_interpreter . HandleCommandsFromFile ( cmd_file , exe_ctx , options , result ) ;
} else {
2018-05-01 00:49:04 +08:00
// No options were set, inherit any settings from nested "command source"
// commands, or set to sane default settings...
2016-12-08 09:31:04 +08:00
CommandInterpreterRunOptions options ;
m_interpreter . HandleCommandsFromFile ( cmd_file , exe_ctx , options , result ) ;
2010-07-07 11:36:20 +08:00
}
2016-09-07 04:57:50 +08:00
return result . Succeeded ( ) ;
}
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
CommandOptions m_options ;
2010-07-07 11:36:20 +08:00
} ;
# pragma mark CommandObjectCommandsAlias
//-------------------------------------------------------------------------
// CommandObjectCommandsAlias
//-------------------------------------------------------------------------
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
static OptionDefinition g_alias_options [ ] = {
// clang-format off
{ LLDB_OPT_SET_ALL , false , " help " , ' h ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypeHelpText , " Help text for this command " } ,
{ LLDB_OPT_SET_ALL , false , " long-help " , ' H ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypeHelpText , " Long help text for this command " } ,
// clang-format on
} ;
2016-09-07 04:57:50 +08:00
static const char * g_python_command_instructions =
" Enter your Python command(s). Type 'DONE' to end. \n "
" You must define a Python function with this signature: \n "
" def my_command_impl(debugger, args, result, internal_dict): \n " ;
2011-08-17 00:49:25 +08:00
2016-09-07 04:57:50 +08:00
class CommandObjectCommandsAlias : public CommandObjectRaw {
2016-03-31 09:10:54 +08:00
protected :
2016-09-07 04:57:50 +08:00
class CommandOptions : public OptionGroup {
public :
CommandOptions ( ) : OptionGroup ( ) , m_help ( ) , m_long_help ( ) { }
~ CommandOptions ( ) override = default ;
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
llvm : : ArrayRef < OptionDefinition > GetDefinitions ( ) override {
2016-09-23 05:06:13 +08:00
return llvm : : makeArrayRef ( g_alias_options ) ;
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
}
2016-09-07 04:57:50 +08:00
2017-05-12 12:51:55 +08:00
Status SetOptionValue ( uint32_t option_idx , llvm : : StringRef option_value ,
ExecutionContext * execution_context ) override {
Status error ;
2016-09-07 04:57:50 +08:00
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
const int short_option = GetDefinitions ( ) [ option_idx ] . short_option ;
2016-09-24 01:48:13 +08:00
std : : string option_str ( option_value ) ;
2016-09-07 04:57:50 +08:00
switch ( short_option ) {
case ' h ' :
2016-09-24 01:48:13 +08:00
m_help . SetCurrentValue ( option_str ) ;
2016-09-07 04:57:50 +08:00
m_help . SetOptionWasSet ( ) ;
break ;
case ' H ' :
2016-09-24 01:48:13 +08:00
m_long_help . SetCurrentValue ( option_str ) ;
2016-09-07 04:57:50 +08:00
m_long_help . SetOptionWasSet ( ) ;
break ;
default :
error . SetErrorStringWithFormat ( " invalid short option character '%c' " ,
short_option ) ;
break ;
}
return error ;
2016-03-31 09:10:54 +08:00
}
2016-09-07 04:57:50 +08:00
void OptionParsingStarting ( ExecutionContext * execution_context ) override {
m_help . Clear ( ) ;
m_long_help . Clear ( ) ;
}
OptionValueString m_help ;
OptionValueString m_long_help ;
} ;
2016-03-31 09:10:54 +08:00
2016-09-07 04:57:50 +08:00
OptionGroupOptions m_option_group ;
CommandOptions m_command_options ;
public :
Options * GetOptions ( ) override { return & m_option_group ; }
CommandObjectCommandsAlias ( CommandInterpreter & interpreter )
: CommandObjectRaw (
interpreter , " command alias " ,
2016-10-06 05:14:38 +08:00
" Define a custom command in terms of an existing command. " ) ,
2016-09-07 04:57:50 +08:00
m_option_group ( ) , m_command_options ( ) {
m_option_group . Append ( & m_command_options ) ;
m_option_group . Finalize ( ) ;
SetHelpLong (
" 'alias' allows the user to create a short-cut or abbreviation for long \
2015-07-14 13:48:36 +08:00
commands , multi - word commands , and commands that take particular options . \
2016-09-07 04:57:50 +08:00
Below are some simple examples of how one might use the ' alias ' command : "
R " (
2015-07-14 13:48:36 +08:00
( lldb ) command alias sc script
Creates the abbreviation ' sc ' for the ' script ' command .
( lldb ) command alias bp breakpoint
2016-09-07 04:57:50 +08:00
) "
" Creates the abbreviation 'bp' for the 'breakpoint' command. Since \
2015-07-14 13:48:36 +08:00
breakpoint commands are two - word commands , the user would still need to \
2016-09-07 04:57:50 +08:00
enter the second word after ' bp ' , e . g . ' bp enable ' or ' bp delete ' . "
R " (
2015-07-14 13:48:36 +08:00
( lldb ) command alias bpl breakpoint list
Creates the abbreviation ' bpl ' for the two - word command ' breakpoint list ' .
2016-09-07 04:57:50 +08:00
) "
" An alias can include some options for the command, with the values either \
2015-07-14 13:48:36 +08:00
filled in at the time the alias is created , or specified as positional \
arguments , to be filled in when the alias is invoked . The following example \
2016-09-07 04:57:50 +08:00
shows how to create aliases with options : "
R " (
2015-07-14 13:48:36 +08:00
( lldb ) command alias bfl breakpoint set - f % 1 - l % 2
2016-09-07 04:57:50 +08:00
) "
" Creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \
2015-07-14 13:48:36 +08:00
options already part of the alias . So if the user wants to set a breakpoint \
by file and line without explicitly having to use the - f and - l options , the \
user can now use ' bfl ' instead . The ' % 1 ' and ' % 2 ' are positional placeholders \
for the actual arguments that will be passed when the alias command is used . \
The number in the placeholder refers to the position / order the actual value \
occupies when the alias is used . All the occurrences of ' % 1 ' in the alias \
will be replaced with the first argument , all the occurrences of ' % 2 ' in the \
alias will be replaced with the second argument , and so on . This also allows \
actual arguments to be used multiple times within an alias ( see ' process \
2016-09-07 04:57:50 +08:00
launch ' example below ) . "
R " (
2015-07-14 13:48:36 +08:00
2016-09-07 04:57:50 +08:00
) "
" Note: the positional arguments must substitute as whole words in the resultant \
2015-07-14 13:48:36 +08:00
command , so you can ' t at present do something like this to append the file extension \
2016-09-07 04:57:50 +08:00
\ " .cpp \" : "
R " (
2015-07-14 13:48:36 +08:00
( lldb ) command alias bcppfl breakpoint set - f % 1. cpp - l % 2
2016-09-07 04:57:50 +08:00
) "
" For more complex aliasing, use the \" command regex \" command instead. In the \
2015-07-14 13:48:36 +08:00
' bfl ' case above , the actual file value will be filled in with the first argument \
following ' bfl ' and the actual line number value will be filled in with the second \
2016-09-07 04:57:50 +08:00
argument . The user would use this alias as follows : "
R " (
2015-07-14 13:48:36 +08:00
( lldb ) command alias bfl breakpoint set - f % 1 - l % 2
( lldb ) bfl my - file . c 137
This would be the same as if the user had entered ' breakpoint set - f my - file . c - l 137 ' .
Another example :
( lldb ) command alias pltty process launch - s - o % 1 - e % 1
( lldb ) pltty / dev / tty0
Interpreted as ' process launch - s - o / dev / tty0 - e / dev / tty0 '
2016-09-07 04:57:50 +08:00
) "
" If the user always wanted to pass the same value to a particular option, the \
2015-07-14 13:48:36 +08:00
alias could be defined with that value directly in the alias as a constant , \
2016-09-07 04:57:50 +08:00
rather than using a positional placeholder : "
R " (
2015-07-14 13:48:36 +08:00
( lldb ) command alias bl3 breakpoint set - f % 1 - l 3
2016-09-07 04:57:50 +08:00
Always sets a breakpoint on line 3 of whatever file is indicated . ) " );
CommandArgumentEntry arg1 ;
CommandArgumentEntry arg2 ;
CommandArgumentEntry arg3 ;
CommandArgumentData alias_arg ;
CommandArgumentData cmd_arg ;
CommandArgumentData options_arg ;
// Define the first (and only) variant of this arg.
alias_arg . arg_type = eArgTypeAliasName ;
alias_arg . arg_repetition = eArgRepeatPlain ;
2010-07-07 11:36:20 +08:00
2016-09-07 04:57:50 +08:00
// There is only one variant this argument could be; put it into the
// argument entry.
arg1 . push_back ( alias_arg ) ;
// Define the first (and only) variant of this arg.
cmd_arg . arg_type = eArgTypeCommandName ;
cmd_arg . arg_repetition = eArgRepeatPlain ;
// There is only one variant this argument could be; put it into the
// argument entry.
arg2 . push_back ( cmd_arg ) ;
// Define the first (and only) variant of this arg.
options_arg . arg_type = eArgTypeAliasOptions ;
options_arg . arg_repetition = eArgRepeatOptional ;
// There is only one variant this argument could be; put it into the
// argument entry.
arg3 . push_back ( options_arg ) ;
// Push the data for the first argument into the m_arguments vector.
m_arguments . push_back ( arg1 ) ;
m_arguments . push_back ( arg2 ) ;
m_arguments . push_back ( arg3 ) ;
}
~ CommandObjectCommandsAlias ( ) override = default ;
2010-07-07 11:36:20 +08:00
2012-06-09 05:56:10 +08:00
protected :
2016-09-07 04:57:50 +08:00
bool DoExecute ( const char * raw_command_line ,
CommandReturnObject & result ) override {
if ( ! raw_command_line | | ! raw_command_line [ 0 ] ) {
result . AppendError ( " 'command alias' requires at least two arguments " ) ;
return false ;
}
2016-08-12 07:51:28 +08:00
2016-09-07 04:57:50 +08:00
ExecutionContext exe_ctx = GetCommandInterpreter ( ) . GetExecutionContext ( ) ;
m_option_group . NotifyOptionParsingStarting ( & exe_ctx ) ;
const char * remainder = nullptr ;
if ( raw_command_line [ 0 ] = = ' - ' ) {
// We have some options and these options MUST end with --.
const char * end_options = nullptr ;
const char * s = raw_command_line ;
while ( s & & s [ 0 ] ) {
end_options = : : strstr ( s , " -- " ) ;
if ( end_options ) {
end_options + = 2 ; // Get past the "--"
if ( : : isspace ( end_options [ 0 ] ) ) {
remainder = end_options ;
while ( : : isspace ( * remainder ) )
+ + remainder ;
break ;
}
}
s = end_options ;
}
if ( end_options ) {
Args args (
llvm : : StringRef ( raw_command_line , end_options - raw_command_line ) ) ;
if ( ! ParseOptions ( args , result ) )
return false ;
2017-05-12 12:51:55 +08:00
Status error ( m_option_group . NotifyOptionParsingFinished ( & exe_ctx ) ) ;
2016-09-07 04:57:50 +08:00
if ( error . Fail ( ) ) {
result . AppendError ( error . AsCString ( ) ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
}
2012-06-09 05:56:10 +08:00
}
2016-09-07 04:57:50 +08:00
if ( nullptr = = remainder )
remainder = raw_command_line ;
2012-06-09 05:56:10 +08:00
2016-10-06 05:14:56 +08:00
llvm : : StringRef raw_command_string ( remainder ) ;
Args args ( raw_command_string ) ;
2016-03-08 13:37:15 +08:00
2016-10-06 04:03:37 +08:00
if ( args . GetArgumentCount ( ) < 2 ) {
2016-09-07 04:57:50 +08:00
result . AppendError ( " 'command alias' requires at least two arguments " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
2010-12-10 06:52:49 +08:00
}
2010-07-07 11:36:20 +08:00
2016-09-07 04:57:50 +08:00
// Get the alias command.
2016-12-08 09:31:04 +08:00
auto alias_command = args [ 0 ] . ref ;
if ( alias_command . startswith ( " - " ) ) {
2016-09-07 04:57:50 +08:00
result . AppendError ( " aliases starting with a dash are not supported " ) ;
if ( alias_command = = " --help " | | alias_command = = " --long-help " ) {
result . AppendWarning ( " if trying to pass options to 'command alias' add "
" a -- at the end of the options " ) ;
}
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
// Strip the new alias name off 'raw_command_string' (leave it on args,
2018-05-01 00:49:04 +08:00
// which gets passed to 'Execute', which does the stripping itself.
2016-09-07 04:57:50 +08:00
size_t pos = raw_command_string . find ( alias_command ) ;
if ( pos = = 0 ) {
raw_command_string = raw_command_string . substr ( alias_command . size ( ) ) ;
pos = raw_command_string . find_first_not_of ( ' ' ) ;
if ( ( pos ! = std : : string : : npos ) & & ( pos > 0 ) )
raw_command_string = raw_command_string . substr ( pos ) ;
} else {
result . AppendError ( " Error parsing command string. No alias created. " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
// Verify that the command is alias-able.
2016-11-03 04:34:10 +08:00
if ( m_interpreter . CommandExists ( alias_command ) ) {
2016-09-07 04:57:50 +08:00
result . AppendErrorWithFormat (
" '%s' is a permanent debugger command and cannot be redefined. \n " ,
2016-12-08 09:31:04 +08:00
args [ 0 ] . c_str ( ) ) ;
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
2010-07-07 11:36:20 +08:00
2016-09-07 04:57:50 +08:00
// Get CommandObject that is being aliased. The command name is read from
2016-10-06 05:14:56 +08:00
// the front of raw_command_string. raw_command_string is returned with the
// name of the command object stripped off the front.
llvm : : StringRef original_raw_command_string = raw_command_string ;
2016-09-07 04:57:50 +08:00
CommandObject * cmd_obj =
m_interpreter . GetCommandObjectForCommand ( raw_command_string ) ;
if ( ! cmd_obj ) {
result . AppendErrorWithFormat ( " invalid command given to 'command alias'. "
" '%s' does not begin with a valid command. "
" No alias created. " ,
2016-10-06 05:14:56 +08:00
original_raw_command_string . str ( ) . c_str ( ) ) ;
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
} else if ( ! cmd_obj - > WantsRawCommandString ( ) ) {
// Note that args was initialized with the original command, and has not
2018-05-01 00:49:04 +08:00
// been updated to this point. Therefore can we pass it to the version of
// Execute that does not need/expect raw input in the alias.
2016-09-07 04:57:50 +08:00
return HandleAliasingNormalCommand ( args , result ) ;
} else {
return HandleAliasingRawCommand ( alias_command , raw_command_string ,
* cmd_obj , result ) ;
}
return result . Succeeded ( ) ;
}
2016-10-06 05:14:56 +08:00
bool HandleAliasingRawCommand ( llvm : : StringRef alias_command ,
llvm : : StringRef raw_command_string ,
2016-09-07 04:57:50 +08:00
CommandObject & cmd_obj ,
CommandReturnObject & result ) {
// Verify & handle any options/arguments passed to the alias command
OptionArgVectorSP option_arg_vector_sp =
OptionArgVectorSP ( new OptionArgVector ) ;
if ( CommandObjectSP cmd_obj_sp =
m_interpreter . GetCommandSPExact ( cmd_obj . GetCommandName ( ) , false ) ) {
2016-10-06 05:14:56 +08:00
if ( m_interpreter . AliasExists ( alias_command ) | |
m_interpreter . UserCommandExists ( alias_command ) ) {
2016-09-07 04:57:50 +08:00
result . AppendWarningWithFormat (
" Overwriting existing definition for '%s'. \n " ,
2016-10-06 05:14:56 +08:00
alias_command . str ( ) . c_str ( ) ) ;
2016-09-07 04:57:50 +08:00
}
if ( CommandAlias * alias = m_interpreter . AddAlias (
2016-10-06 05:14:56 +08:00
alias_command , cmd_obj_sp , raw_command_string ) ) {
2016-09-07 04:57:50 +08:00
if ( m_command_options . m_help . OptionWasSet ( ) )
alias - > SetHelp ( m_command_options . m_help . GetCurrentValue ( ) ) ;
if ( m_command_options . m_long_help . OptionWasSet ( ) )
alias - > SetHelpLong ( m_command_options . m_long_help . GetCurrentValue ( ) ) ;
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
} else {
result . AppendError ( " Unable to create requested alias. \n " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
}
} else {
result . AppendError ( " Unable to create requested alias. \n " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
}
2010-07-07 11:36:20 +08:00
2016-09-07 04:57:50 +08:00
return result . Succeeded ( ) ;
}
2010-07-07 11:36:20 +08:00
2016-09-07 04:57:50 +08:00
bool HandleAliasingNormalCommand ( Args & args , CommandReturnObject & result ) {
size_t argc = args . GetArgumentCount ( ) ;
2010-07-07 11:36:20 +08:00
2016-09-07 04:57:50 +08:00
if ( argc < 2 ) {
result . AppendError ( " 'command alias' requires at least two arguments " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
2010-07-07 11:36:20 +08:00
2016-12-08 09:31:04 +08:00
// Save these in std::strings since we're going to shift them off.
const std : : string alias_command ( args [ 0 ] . ref ) ;
const std : : string actual_command ( args [ 1 ] . ref ) ;
2016-09-07 04:57:50 +08:00
args . Shift ( ) ; // Shift the alias command word off the argument vector.
args . Shift ( ) ; // Shift the old command word off the argument vector.
// Verify that the command is alias'able, and get the appropriate command
// object.
2016-11-03 04:34:10 +08:00
if ( m_interpreter . CommandExists ( alias_command ) ) {
2016-09-07 04:57:50 +08:00
result . AppendErrorWithFormat (
" '%s' is a permanent debugger command and cannot be redefined. \n " ,
alias_command . c_str ( ) ) ;
result . SetStatus ( eReturnStatusFailed ) ;
2016-12-08 09:31:04 +08:00
return false ;
}
CommandObjectSP command_obj_sp (
m_interpreter . GetCommandSPExact ( actual_command , true ) ) ;
CommandObjectSP subcommand_obj_sp ;
bool use_subcommand = false ;
if ( ! command_obj_sp ) {
result . AppendErrorWithFormat ( " '%s' is not an existing command. \n " ,
actual_command . c_str ( ) ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
CommandObject * cmd_obj = command_obj_sp . get ( ) ;
CommandObject * sub_cmd_obj = nullptr ;
OptionArgVectorSP option_arg_vector_sp =
OptionArgVectorSP ( new OptionArgVector ) ;
while ( cmd_obj - > IsMultiwordObject ( ) & & ! args . empty ( ) ) {
auto sub_command = args [ 0 ] . ref ;
assert ( ! sub_command . empty ( ) ) ;
subcommand_obj_sp = cmd_obj - > GetSubcommandSP ( sub_command ) ;
if ( ! subcommand_obj_sp ) {
result . AppendErrorWithFormat (
" '%s' is not a valid sub-command of '%s'. "
" Unable to create alias. \n " ,
args [ 0 ] . c_str ( ) , actual_command . c_str ( ) ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
2016-09-07 04:57:50 +08:00
2016-12-08 09:31:04 +08:00
sub_cmd_obj = subcommand_obj_sp . get ( ) ;
use_subcommand = true ;
args . Shift ( ) ; // Shift the sub_command word off the argument vector.
cmd_obj = sub_cmd_obj ;
}
2016-09-07 04:57:50 +08:00
2016-12-08 09:31:04 +08:00
// Verify & handle any options/arguments passed to the alias command
2016-09-07 04:57:50 +08:00
2016-12-08 09:31:04 +08:00
std : : string args_string ;
2016-09-07 04:57:50 +08:00
2016-12-08 09:31:04 +08:00
if ( ! args . empty ( ) ) {
CommandObjectSP tmp_sp =
m_interpreter . GetCommandSPExact ( cmd_obj - > GetCommandName ( ) , false ) ;
if ( use_subcommand )
tmp_sp = m_interpreter . GetCommandSPExact ( sub_cmd_obj - > GetCommandName ( ) ,
false ) ;
2016-09-07 04:57:50 +08:00
2016-12-08 09:31:04 +08:00
args . GetCommandString ( args_string ) ;
}
2016-09-07 04:57:50 +08:00
2016-12-08 09:31:04 +08:00
if ( m_interpreter . AliasExists ( alias_command ) | |
m_interpreter . UserCommandExists ( alias_command ) ) {
result . AppendWarningWithFormat (
" Overwriting existing definition for '%s'. \n " , alias_command . c_str ( ) ) ;
}
if ( CommandAlias * alias = m_interpreter . AddAlias (
alias_command , use_subcommand ? subcommand_obj_sp : command_obj_sp ,
args_string ) ) {
if ( m_command_options . m_help . OptionWasSet ( ) )
alias - > SetHelp ( m_command_options . m_help . GetCurrentValue ( ) ) ;
if ( m_command_options . m_long_help . OptionWasSet ( ) )
alias - > SetHelpLong ( m_command_options . m_long_help . GetCurrentValue ( ) ) ;
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
} else {
result . AppendError ( " Unable to create requested alias. \n " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
2010-07-07 11:36:20 +08:00
}
2016-09-07 04:57:50 +08:00
return result . Succeeded ( ) ;
}
2010-07-07 11:36:20 +08:00
} ;
# pragma mark CommandObjectCommandsUnalias
//-------------------------------------------------------------------------
// CommandObjectCommandsUnalias
//-------------------------------------------------------------------------
2016-09-07 04:57:50 +08:00
class CommandObjectCommandsUnalias : public CommandObjectParsed {
2010-07-07 11:36:20 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectCommandsUnalias ( CommandInterpreter & interpreter )
: CommandObjectParsed (
interpreter , " command unalias " ,
" Delete one or more custom commands defined by 'command alias'. " ,
nullptr ) {
CommandArgumentEntry arg ;
CommandArgumentData alias_arg ;
2010-07-07 11:36:20 +08:00
2016-09-07 04:57:50 +08:00
// Define the first (and only) variant of this arg.
alias_arg . arg_type = eArgTypeAliasName ;
alias_arg . arg_repetition = eArgRepeatPlain ;
// There is only one variant this argument could be; put it into the
// argument entry.
arg . push_back ( alias_arg ) ;
// Push the data for the first argument into the m_arguments vector.
m_arguments . push_back ( arg ) ;
}
~ CommandObjectCommandsUnalias ( ) override = default ;
2010-07-07 11:36:20 +08:00
2012-06-09 05:56:10 +08:00
protected :
2016-09-07 04:57:50 +08:00
bool DoExecute ( Args & args , CommandReturnObject & result ) override {
CommandObject : : CommandMap : : iterator pos ;
CommandObject * cmd_obj ;
2016-10-06 04:03:37 +08:00
if ( args . empty ( ) ) {
result . AppendError ( " must call 'unalias' with a valid alias " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
2016-12-08 09:31:04 +08:00
auto command_name = args [ 0 ] . ref ;
2016-10-06 04:03:37 +08:00
cmd_obj = m_interpreter . GetCommandObject ( command_name ) ;
2016-12-08 09:31:04 +08:00
if ( ! cmd_obj ) {
2016-10-06 04:03:37 +08:00
result . AppendErrorWithFormat (
" '%s' is not a known command. \n Try 'help' to see a "
" current list of commands. \n " ,
2016-12-09 09:20:58 +08:00
args [ 0 ] . c_str ( ) ) ;
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
2016-12-08 09:31:04 +08:00
return false ;
2010-07-07 11:36:20 +08:00
}
2016-09-07 04:57:50 +08:00
2016-12-08 09:31:04 +08:00
if ( m_interpreter . CommandExists ( command_name ) ) {
if ( cmd_obj - > IsRemovable ( ) ) {
result . AppendErrorWithFormat (
" '%s' is not an alias, it is a debugger command which can be "
" removed using the 'command delete' command. \n " ,
2016-12-09 09:20:58 +08:00
args [ 0 ] . c_str ( ) ) ;
2016-12-08 09:31:04 +08:00
} else {
result . AppendErrorWithFormat (
" '%s' is a permanent debugger command and cannot be removed. \n " ,
2016-12-09 09:20:58 +08:00
args [ 0 ] . c_str ( ) ) ;
2016-12-08 09:31:04 +08:00
}
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
if ( ! m_interpreter . RemoveAlias ( command_name ) ) {
if ( m_interpreter . AliasExists ( command_name ) )
result . AppendErrorWithFormat (
2016-12-09 09:20:58 +08:00
" Error occurred while attempting to unalias '%s'. \n " ,
args [ 0 ] . c_str ( ) ) ;
2016-12-08 09:31:04 +08:00
else
result . AppendErrorWithFormat ( " '%s' is not an existing alias. \n " ,
2016-12-09 09:20:58 +08:00
args [ 0 ] . c_str ( ) ) ;
2016-12-08 09:31:04 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
2016-09-07 04:57:50 +08:00
return result . Succeeded ( ) ;
}
2010-07-07 11:36:20 +08:00
} ;
2015-01-10 03:08:20 +08:00
# pragma mark CommandObjectCommandsDelete
//-------------------------------------------------------------------------
// CommandObjectCommandsDelete
//-------------------------------------------------------------------------
2016-09-07 04:57:50 +08:00
class CommandObjectCommandsDelete : public CommandObjectParsed {
2015-01-10 03:08:20 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectCommandsDelete ( CommandInterpreter & interpreter )
: CommandObjectParsed (
interpreter , " command delete " ,
" Delete one or more custom commands defined by 'command regex'. " ,
nullptr ) {
CommandArgumentEntry arg ;
CommandArgumentData alias_arg ;
2015-01-10 03:08:20 +08:00
2016-09-07 04:57:50 +08:00
// Define the first (and only) variant of this arg.
alias_arg . arg_type = eArgTypeCommandName ;
alias_arg . arg_repetition = eArgRepeatPlain ;
2015-01-10 03:08:20 +08:00
2016-09-07 04:57:50 +08:00
// There is only one variant this argument could be; put it into the
// argument entry.
arg . push_back ( alias_arg ) ;
2015-01-10 03:08:20 +08:00
2016-09-07 04:57:50 +08:00
// Push the data for the first argument into the m_arguments vector.
m_arguments . push_back ( arg ) ;
}
2015-01-10 03:08:20 +08:00
2016-09-07 04:57:50 +08:00
~ CommandObjectCommandsDelete ( ) override = default ;
2015-01-10 03:08:20 +08:00
protected :
2016-09-07 04:57:50 +08:00
bool DoExecute ( Args & args , CommandReturnObject & result ) override {
CommandObject : : CommandMap : : iterator pos ;
2016-10-06 04:03:37 +08:00
if ( args . empty ( ) ) {
result . AppendErrorWithFormat ( " must call '%s' with one or more valid user "
" defined regular expression command names " ,
2016-10-06 05:14:38 +08:00
GetCommandName ( ) . str ( ) . c_str ( ) ) ;
2016-10-06 04:03:37 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
}
2016-12-08 09:31:04 +08:00
auto command_name = args [ 0 ] . ref ;
if ( ! m_interpreter . CommandExists ( command_name ) ) {
2016-10-06 04:03:37 +08:00
StreamString error_msg_stream ;
const bool generate_apropos = true ;
const bool generate_type_lookup = false ;
CommandObjectHelp : : GenerateAdditionalHelpAvenuesMessage (
2016-12-08 09:31:04 +08:00
& error_msg_stream , command_name , llvm : : StringRef ( ) , llvm : : StringRef ( ) ,
generate_apropos , generate_type_lookup ) ;
2016-11-17 05:15:24 +08:00
result . AppendError ( error_msg_stream . GetString ( ) ) ;
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
2016-12-08 09:31:04 +08:00
return false ;
2015-01-10 03:08:20 +08:00
}
2016-09-07 04:57:50 +08:00
2016-12-08 09:31:04 +08:00
if ( ! m_interpreter . RemoveCommand ( command_name ) ) {
result . AppendErrorWithFormat (
" '%s' is a permanent debugger command and cannot be removed. \n " ,
2016-12-09 09:20:58 +08:00
args [ 0 ] . c_str ( ) ) ;
2016-12-08 09:31:04 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
return true ;
2016-09-07 04:57:50 +08:00
}
2015-01-10 03:08:20 +08:00
} ;
2011-04-21 00:37:46 +08:00
//-------------------------------------------------------------------------
// CommandObjectCommandsAddRegex
//-------------------------------------------------------------------------
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
static OptionDefinition g_regex_options [ ] = {
// clang-format off
{ LLDB_OPT_SET_1 , false , " help " , ' h ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypeNone , " The help text to display for this command. " } ,
{ LLDB_OPT_SET_1 , false , " syntax " , ' s ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypeNone , " A syntax string showing the typical usage syntax. " } ,
// clang-format on
} ;
2012-06-09 05:56:10 +08:00
# pragma mark CommandObjectCommandsAddRegex
2011-04-21 00:37:46 +08:00
2016-09-07 04:57:50 +08:00
class CommandObjectCommandsAddRegex : public CommandObjectParsed ,
public IOHandlerDelegateMultiline {
2011-04-21 00:37:46 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectCommandsAddRegex ( CommandInterpreter & interpreter )
: CommandObjectParsed (
interpreter , " command regex " , " Define a custom command in terms of "
" existing commands by matching "
" regular expressions. " ,
" command regex <cmd-name> [s/<regex>/<subst>/ ...] " ) ,
IOHandlerDelegateMultiline ( " " ,
IOHandlerDelegate : : Completion : : LLDBCommand ) ,
m_options ( ) {
SetHelpLong (
R " (
) "
" This command allows the user to create powerful regular expression commands \
2015-07-14 13:48:36 +08:00
with substitutions . The regular expressions and substitutions are specified \
2016-09-07 04:57:50 +08:00
using the regular expression substitution format of : "
R " (
2015-07-14 13:48:36 +08:00
s / < regex > / < subst > /
2016-09-07 04:57:50 +08:00
) "
" <regex> is a regular expression that can use parenthesis to capture regular \
2015-07-14 13:48:36 +08:00
expression input and substitute the captured matches in the output using % 1 \
2016-09-07 04:57:50 +08:00
for the first match , % 2 for the second , and so on . "
R " (
2015-07-14 13:48:36 +08:00
2016-09-07 04:57:50 +08:00
) "
" The regular expressions can all be specified on the command line if more than \
2015-07-14 13:48:36 +08:00
one argument is provided . If just the command name is provided on the command \
line , then the regular expressions and substitutions can be entered on separate \
2016-09-07 04:57:50 +08:00
lines , followed by an empty line to terminate the command definition . "
R " (
2015-07-14 13:48:36 +08:00
EXAMPLES
2016-09-07 04:57:50 +08:00
) "
" The following example will define a regular expression command named 'f' that \
2015-07-14 13:48:36 +08:00
will call ' finish ' if there are no arguments , or ' frame select < frame - idx > ' if \
2016-09-07 04:57:50 +08:00
a number follows ' f ' : "
R " (
2015-07-14 13:48:36 +08:00
2016-09-07 04:57:50 +08:00
( lldb ) command regex f s / ^ $ / finish / ' s / ( [ 0 - 9 ] + ) / frame select % 1 / ' ) " );
}
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
~ CommandObjectCommandsAddRegex ( ) override = default ;
2016-02-23 07:46:47 +08:00
2012-06-09 05:56:10 +08:00
protected :
2016-09-07 04:57:50 +08:00
void IOHandlerActivated ( IOHandler & io_handler ) override {
StreamFileSP output_sp ( io_handler . GetOutputStreamFile ( ) ) ;
if ( output_sp ) {
output_sp - > PutCString ( " Enter one of more sed substitution commands in "
" the form: 's/<regex>/<subst>/'. \n Terminate the "
" substitution list with an empty line. \n " ) ;
output_sp - > Flush ( ) ;
2014-01-28 07:43:24 +08:00
}
2016-09-07 04:57:50 +08:00
}
void IOHandlerInputComplete ( IOHandler & io_handler ,
std : : string & data ) override {
io_handler . SetIsDone ( true ) ;
if ( m_regex_cmd_ap ) {
StringList lines ;
if ( lines . SplitIntoLines ( data ) ) {
const size_t num_lines = lines . GetSize ( ) ;
bool check_only = false ;
for ( size_t i = 0 ; i < num_lines ; + + i ) {
llvm : : StringRef bytes_strref ( lines [ i ] ) ;
2017-05-12 12:51:55 +08:00
Status error = AppendRegexSubstitution ( bytes_strref , check_only ) ;
2016-09-07 04:57:50 +08:00
if ( error . Fail ( ) ) {
if ( ! m_interpreter . GetDebugger ( )
. GetCommandInterpreter ( )
. GetBatchCommandMode ( ) ) {
StreamSP out_stream =
m_interpreter . GetDebugger ( ) . GetAsyncOutputStream ( ) ;
out_stream - > Printf ( " error: %s \n " , error . AsCString ( ) ) ;
2014-01-28 07:43:24 +08:00
}
2016-09-07 04:57:50 +08:00
}
2014-01-28 07:43:24 +08:00
}
2016-09-07 04:57:50 +08:00
}
if ( m_regex_cmd_ap - > HasRegexEntries ( ) ) {
CommandObjectSP cmd_sp ( m_regex_cmd_ap . release ( ) ) ;
m_interpreter . AddCommand ( cmd_sp - > GetCommandName ( ) , cmd_sp , true ) ;
}
}
}
bool DoExecute ( Args & command , CommandReturnObject & result ) override {
const size_t argc = command . GetArgumentCount ( ) ;
if ( argc = = 0 ) {
result . AppendError ( " usage: 'command regex <command-name> "
" [s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]' \n " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
2016-10-06 04:03:37 +08:00
return false ;
}
2016-09-07 04:57:50 +08:00
2017-05-12 12:51:55 +08:00
Status error ;
2016-12-08 09:31:04 +08:00
auto name = command [ 0 ] . ref ;
m_regex_cmd_ap = llvm : : make_unique < CommandObjectRegexCommand > (
m_interpreter , name , m_options . GetHelp ( ) , m_options . GetSyntax ( ) , 10 , 0 ,
true ) ;
2016-10-06 04:03:37 +08:00
if ( argc = = 1 ) {
Debugger & debugger = m_interpreter . GetDebugger ( ) ;
bool color_prompt = debugger . GetUseColor ( ) ;
const bool multiple_lines = true ; // Get multiple lines
IOHandlerSP io_handler_sp ( new IOHandlerEditline (
debugger , IOHandler : : Type : : Other ,
" lldb-regex " , // Name of input reader for history
llvm : : StringRef ( " > " ) , // Prompt
llvm : : StringRef ( ) , // Continuation prompt
multiple_lines , color_prompt ,
0 , // Don't show line numbers
* this ) ) ;
if ( io_handler_sp ) {
debugger . PushIOHandler ( io_handler_sp ) ;
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
2016-09-07 04:57:50 +08:00
}
2016-10-06 04:03:37 +08:00
} else {
2016-10-06 07:40:23 +08:00
for ( auto & entry : command . entries ( ) . drop_front ( ) ) {
2016-10-06 04:03:37 +08:00
bool check_only = false ;
2016-10-06 07:40:23 +08:00
error = AppendRegexSubstitution ( entry . ref , check_only ) ;
2016-10-06 04:03:37 +08:00
if ( error . Fail ( ) )
break ;
}
if ( error . Success ( ) ) {
AddRegexCommandToInterpreter ( ) ;
2016-09-07 04:57:50 +08:00
}
2014-01-28 07:43:24 +08:00
}
2016-10-06 04:03:37 +08:00
if ( error . Fail ( ) ) {
result . AppendError ( error . AsCString ( ) ) ;
result . SetStatus ( eReturnStatusFailed ) ;
}
2014-01-28 07:43:24 +08:00
2016-09-07 04:57:50 +08:00
return result . Succeeded ( ) ;
}
2017-05-12 12:51:55 +08:00
Status AppendRegexSubstitution ( const llvm : : StringRef & regex_sed ,
bool check_only ) {
Status error ;
2011-04-21 06:55:21 +08:00
2016-09-07 04:57:50 +08:00
if ( ! m_regex_cmd_ap ) {
error . SetErrorStringWithFormat (
" invalid regular expression command object for: '%.*s' " ,
( int ) regex_sed . size ( ) , regex_sed . data ( ) ) ;
return error ;
2011-04-21 00:37:46 +08:00
}
2011-04-21 06:55:21 +08:00
2016-09-07 04:57:50 +08:00
size_t regex_sed_size = regex_sed . size ( ) ;
2011-04-21 06:55:21 +08:00
2016-09-07 04:57:50 +08:00
if ( regex_sed_size < = 1 ) {
error . SetErrorStringWithFormat (
" regular expression substitution string is too short: '%.*s' " ,
( int ) regex_sed . size ( ) , regex_sed . data ( ) ) ;
return error ;
}
2011-04-21 06:55:21 +08:00
2016-09-07 04:57:50 +08:00
if ( regex_sed [ 0 ] ! = ' s ' ) {
error . SetErrorStringWithFormat ( " regular expression substitution string "
" doesn't start with 's': '%.*s' " ,
( int ) regex_sed . size ( ) , regex_sed . data ( ) ) ;
return error ;
}
const size_t first_separator_char_pos = 1 ;
2018-05-01 00:49:04 +08:00
// use the char that follows 's' as the regex separator character so we can
// have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
2016-09-07 04:57:50 +08:00
const char separator_char = regex_sed [ first_separator_char_pos ] ;
const size_t second_separator_char_pos =
regex_sed . find ( separator_char , first_separator_char_pos + 1 ) ;
if ( second_separator_char_pos = = std : : string : : npos ) {
error . SetErrorStringWithFormat (
" missing second '%c' separator char after '%.*s' in '%.*s' " ,
separator_char ,
( int ) ( regex_sed . size ( ) - first_separator_char_pos - 1 ) ,
regex_sed . data ( ) + ( first_separator_char_pos + 1 ) ,
( int ) regex_sed . size ( ) , regex_sed . data ( ) ) ;
return error ;
}
2014-01-28 07:43:24 +08:00
2016-09-07 04:57:50 +08:00
const size_t third_separator_char_pos =
regex_sed . find ( separator_char , second_separator_char_pos + 1 ) ;
if ( third_separator_char_pos = = std : : string : : npos ) {
error . SetErrorStringWithFormat (
" missing third '%c' separator char after '%.*s' in '%.*s' " ,
separator_char ,
( int ) ( regex_sed . size ( ) - second_separator_char_pos - 1 ) ,
regex_sed . data ( ) + ( second_separator_char_pos + 1 ) ,
( int ) regex_sed . size ( ) , regex_sed . data ( ) ) ;
return error ;
}
if ( third_separator_char_pos ! = regex_sed_size - 1 ) {
2018-05-01 00:49:04 +08:00
// Make sure that everything that follows the last regex separator char
2016-09-07 04:57:50 +08:00
if ( regex_sed . find_first_not_of ( " \t \n \v \f \r " ,
third_separator_char_pos + 1 ) ! =
std : : string : : npos ) {
error . SetErrorStringWithFormat (
" extra data found after the '%.*s' regular expression substitution "
" string: '%.*s' " ,
( int ) third_separator_char_pos + 1 , regex_sed . data ( ) ,
( int ) ( regex_sed . size ( ) - third_separator_char_pos - 1 ) ,
regex_sed . data ( ) + ( third_separator_char_pos + 1 ) ) ;
2011-04-21 06:55:21 +08:00
return error ;
2016-09-07 04:57:50 +08:00
}
} else if ( first_separator_char_pos + 1 = = second_separator_char_pos ) {
error . SetErrorStringWithFormat (
" <regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s' " ,
separator_char , separator_char , separator_char , ( int ) regex_sed . size ( ) ,
regex_sed . data ( ) ) ;
return error ;
} else if ( second_separator_char_pos + 1 = = third_separator_char_pos ) {
error . SetErrorStringWithFormat (
" <subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s' " ,
separator_char , separator_char , separator_char , ( int ) regex_sed . size ( ) ,
regex_sed . data ( ) ) ;
return error ;
2011-04-21 00:37:46 +08:00
}
2016-09-07 04:57:50 +08:00
if ( ! check_only ) {
std : : string regex ( regex_sed . substr ( first_separator_char_pos + 1 ,
second_separator_char_pos -
first_separator_char_pos - 1 ) ) ;
std : : string subst ( regex_sed . substr ( second_separator_char_pos + 1 ,
third_separator_char_pos -
second_separator_char_pos - 1 ) ) ;
m_regex_cmd_ap - > AddRegexCommand ( regex . c_str ( ) , subst . c_str ( ) ) ;
}
return error ;
}
void AddRegexCommandToInterpreter ( ) {
if ( m_regex_cmd_ap ) {
if ( m_regex_cmd_ap - > HasRegexEntries ( ) ) {
CommandObjectSP cmd_sp ( m_regex_cmd_ap . release ( ) ) ;
m_interpreter . AddCommand ( cmd_sp - > GetCommandName ( ) , cmd_sp , true ) ;
}
2011-04-21 00:37:46 +08:00
}
2016-09-07 04:57:50 +08:00
}
2011-04-21 00:37:46 +08:00
private :
2016-09-07 04:57:50 +08:00
std : : unique_ptr < CommandObjectRegexCommand > m_regex_cmd_ap ;
class CommandOptions : public Options {
public :
CommandOptions ( ) : Options ( ) { }
~ CommandOptions ( ) override = default ;
2017-05-12 12:51:55 +08:00
Status SetOptionValue ( uint32_t option_idx , llvm : : StringRef option_arg ,
ExecutionContext * execution_context ) override {
Status error ;
2016-09-07 04:57:50 +08:00
const int short_option = m_getopt_table [ option_idx ] . val ;
switch ( short_option ) {
case ' h ' :
m_help . assign ( option_arg ) ;
break ;
case ' s ' :
m_syntax . assign ( option_arg ) ;
break ;
default :
error . SetErrorStringWithFormat ( " unrecognized option '%c' " ,
short_option ) ;
break ;
}
return error ;
}
void OptionParsingStarting ( ExecutionContext * execution_context ) override {
m_help . clear ( ) ;
m_syntax . clear ( ) ;
}
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
llvm : : ArrayRef < OptionDefinition > GetDefinitions ( ) override {
2016-09-23 05:06:13 +08:00
return llvm : : makeArrayRef ( g_regex_options ) ;
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
}
2016-09-07 04:57:50 +08:00
2016-10-06 04:03:37 +08:00
// TODO: Convert these functions to return StringRefs.
2016-09-07 04:57:50 +08:00
const char * GetHelp ( ) {
return ( m_help . empty ( ) ? nullptr : m_help . c_str ( ) ) ;
}
const char * GetSyntax ( ) {
return ( m_syntax . empty ( ) ? nullptr : m_syntax . c_str ( ) ) ;
}
protected :
// Instance variables to hold the values for command options.
std : : string m_help ;
std : : string m_syntax ;
} ;
Options * GetOptions ( ) override { return & m_options ; }
CommandOptions m_options ;
2011-04-21 00:37:46 +08:00
} ;
2016-09-07 04:57:50 +08:00
class CommandObjectPythonFunction : public CommandObjectRaw {
2011-08-17 07:24:13 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectPythonFunction ( CommandInterpreter & interpreter , std : : string name ,
std : : string funct , std : : string help ,
ScriptedCommandSynchronicity synch )
2016-10-06 05:14:38 +08:00
: CommandObjectRaw ( interpreter , name ) ,
2016-09-07 04:57:50 +08:00
m_function_name ( funct ) , m_synchro ( synch ) , m_fetched_help_long ( false ) {
if ( ! help . empty ( ) )
2016-11-13 04:41:02 +08:00
SetHelp ( help ) ;
2016-09-07 04:57:50 +08:00
else {
StreamString stream ;
stream . Printf ( " For more information run 'help %s' " , name . c_str ( ) ) ;
2016-11-17 05:15:24 +08:00
SetHelp ( stream . GetString ( ) ) ;
2011-08-17 07:24:13 +08:00
}
2016-09-07 04:57:50 +08:00
}
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
~ CommandObjectPythonFunction ( ) override = default ;
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
bool IsRemovable ( ) const override { return true ; }
2012-06-09 05:56:10 +08:00
2016-09-07 04:57:50 +08:00
const std : : string & GetFunctionName ( ) { return m_function_name ; }
2012-06-09 05:56:10 +08:00
2016-09-07 04:57:50 +08:00
ScriptedCommandSynchronicity GetSynchronicity ( ) { return m_synchro ; }
2016-11-13 04:41:02 +08:00
llvm : : StringRef GetHelpLong ( ) override {
if ( m_fetched_help_long )
return CommandObjectRaw : : GetHelpLong ( ) ;
ScriptInterpreter * scripter = m_interpreter . GetScriptInterpreter ( ) ;
if ( ! scripter )
return CommandObjectRaw : : GetHelpLong ( ) ;
std : : string docstring ;
m_fetched_help_long =
scripter - > GetDocumentationForItem ( m_function_name . c_str ( ) , docstring ) ;
if ( ! docstring . empty ( ) )
SetHelpLong ( docstring ) ;
2016-09-07 04:57:50 +08:00
return CommandObjectRaw : : GetHelpLong ( ) ;
}
2012-06-09 05:56:10 +08:00
protected :
2016-09-07 04:57:50 +08:00
bool DoExecute ( const char * raw_command_line ,
CommandReturnObject & result ) override {
ScriptInterpreter * scripter = m_interpreter . GetScriptInterpreter ( ) ;
2017-05-12 12:51:55 +08:00
Status error ;
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusInvalid ) ;
if ( ! scripter | |
! scripter - > RunScriptBasedCommand ( m_function_name . c_str ( ) ,
raw_command_line , m_synchro , result ,
error , m_exe_ctx ) ) {
result . AppendError ( error . AsCString ( ) ) ;
result . SetStatus ( eReturnStatusFailed ) ;
} else {
// Don't change the status if the command already set it...
if ( result . GetStatus ( ) = = eReturnStatusInvalid ) {
2016-11-17 05:15:24 +08:00
if ( result . GetOutputData ( ) . empty ( ) )
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
2011-08-17 07:24:13 +08:00
else
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusSuccessFinishResult ) ;
}
2011-08-17 07:24:13 +08:00
}
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
return result . Succeeded ( ) ;
}
2016-02-23 07:46:47 +08:00
private :
2016-09-07 04:57:50 +08:00
std : : string m_function_name ;
ScriptedCommandSynchronicity m_synchro ;
bool m_fetched_help_long ;
2012-06-09 05:56:10 +08:00
} ;
2011-11-08 06:57:04 +08:00
2016-09-07 04:57:50 +08:00
class CommandObjectScriptingObject : public CommandObjectRaw {
2015-03-13 10:20:41 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectScriptingObject ( CommandInterpreter & interpreter ,
std : : string name ,
StructuredData : : GenericSP cmd_obj_sp ,
ScriptedCommandSynchronicity synch )
2016-10-06 05:14:38 +08:00
: CommandObjectRaw ( interpreter , name ) ,
2016-09-07 04:57:50 +08:00
m_cmd_obj_sp ( cmd_obj_sp ) , m_synchro ( synch ) , m_fetched_help_short ( false ) ,
m_fetched_help_long ( false ) {
StreamString stream ;
stream . Printf ( " For more information run 'help %s' " , name . c_str ( ) ) ;
2016-11-17 05:15:24 +08:00
SetHelp ( stream . GetString ( ) ) ;
2016-09-07 04:57:50 +08:00
if ( ScriptInterpreter * scripter = m_interpreter . GetScriptInterpreter ( ) )
GetFlags ( ) . Set ( scripter - > GetFlagsForCommandObject ( cmd_obj_sp ) ) ;
}
~ CommandObjectScriptingObject ( ) override = default ;
bool IsRemovable ( ) const override { return true ; }
StructuredData : : GenericSP GetImplementingObject ( ) { return m_cmd_obj_sp ; }
ScriptedCommandSynchronicity GetSynchronicity ( ) { return m_synchro ; }
2016-11-13 04:41:02 +08:00
llvm : : StringRef GetHelp ( ) override {
if ( m_fetched_help_short )
return CommandObjectRaw : : GetHelp ( ) ;
ScriptInterpreter * scripter = m_interpreter . GetScriptInterpreter ( ) ;
if ( ! scripter )
return CommandObjectRaw : : GetHelp ( ) ;
std : : string docstring ;
m_fetched_help_short =
scripter - > GetShortHelpForCommandObject ( m_cmd_obj_sp , docstring ) ;
if ( ! docstring . empty ( ) )
SetHelp ( docstring ) ;
2016-09-07 04:57:50 +08:00
return CommandObjectRaw : : GetHelp ( ) ;
}
2016-11-13 04:41:02 +08:00
llvm : : StringRef GetHelpLong ( ) override {
if ( m_fetched_help_long )
return CommandObjectRaw : : GetHelpLong ( ) ;
ScriptInterpreter * scripter = m_interpreter . GetScriptInterpreter ( ) ;
if ( ! scripter )
return CommandObjectRaw : : GetHelpLong ( ) ;
std : : string docstring ;
m_fetched_help_long =
scripter - > GetLongHelpForCommandObject ( m_cmd_obj_sp , docstring ) ;
if ( ! docstring . empty ( ) )
SetHelpLong ( docstring ) ;
2016-09-07 04:57:50 +08:00
return CommandObjectRaw : : GetHelpLong ( ) ;
}
2015-03-14 06:22:28 +08:00
2015-03-13 10:20:41 +08:00
protected :
2016-09-07 04:57:50 +08:00
bool DoExecute ( const char * raw_command_line ,
CommandReturnObject & result ) override {
ScriptInterpreter * scripter = m_interpreter . GetScriptInterpreter ( ) ;
2017-05-12 12:51:55 +08:00
Status error ;
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusInvalid ) ;
if ( ! scripter | |
! scripter - > RunScriptBasedCommand ( m_cmd_obj_sp , raw_command_line ,
m_synchro , result , error , m_exe_ctx ) ) {
result . AppendError ( error . AsCString ( ) ) ;
result . SetStatus ( eReturnStatusFailed ) ;
} else {
// Don't change the status if the command already set it...
if ( result . GetStatus ( ) = = eReturnStatusInvalid ) {
2016-11-17 05:15:24 +08:00
if ( result . GetOutputData ( ) . empty ( ) )
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
2015-03-13 10:20:41 +08:00
else
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusSuccessFinishResult ) ;
}
2015-03-13 10:20:41 +08:00
}
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
return result . Succeeded ( ) ;
}
2016-02-23 07:46:47 +08:00
private :
2016-09-07 04:57:50 +08:00
StructuredData : : GenericSP m_cmd_obj_sp ;
ScriptedCommandSynchronicity m_synchro ;
bool m_fetched_help_short : 1 ;
bool m_fetched_help_long : 1 ;
2015-03-13 10:20:41 +08:00
} ;
2012-06-09 05:56:10 +08:00
//-------------------------------------------------------------------------
// CommandObjectCommandsScriptImport
//-------------------------------------------------------------------------
2011-11-08 06:57:04 +08:00
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
OptionDefinition g_script_import_options [ ] = {
// clang-format off
{ LLDB_OPT_SET_1 , false , " allow-reload " , ' r ' , OptionParser : : eNoArgument , nullptr , nullptr , 0 , eArgTypeNone , " Allow the script to be loaded even if it was already loaded before. This argument exists for backwards compatibility, but reloading is always allowed, whether you specify it or not. " } ,
// clang-format on
} ;
2016-09-07 04:57:50 +08:00
class CommandObjectCommandsScriptImport : public CommandObjectParsed {
2012-06-09 05:56:10 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectCommandsScriptImport ( CommandInterpreter & interpreter )
: CommandObjectParsed ( interpreter , " command script import " ,
" Import a scripting module in LLDB. " , nullptr ) ,
m_options ( ) {
CommandArgumentEntry arg1 ;
CommandArgumentData cmd_arg ;
// Define the first (and only) variant of this arg.
cmd_arg . arg_type = eArgTypeFilename ;
cmd_arg . arg_repetition = eArgRepeatPlus ;
// There is only one variant this argument could be; put it into the
// argument entry.
arg1 . push_back ( cmd_arg ) ;
// Push the data for the first argument into the m_arguments vector.
m_arguments . push_back ( arg1 ) ;
}
~ CommandObjectCommandsScriptImport ( ) override = default ;
Refactoring for for the internal command line completion API (NFC)
Summary:
This patch refactors the internal completion API. It now takes (as far as possible) a single
CompletionRequest object instead o half a dozen in/out/in-out parameters. The CompletionRequest
contains a common superset of the different parameters as far as it makes sense. This includes
the raw command line string and raw cursor position, which should make the `expr` command
possible to implement (at least without hacks that reconstruct the command line from the args).
This patch is not intended to change the observable behavior of lldb in any way. It's also as
minimal as possible and doesn't attempt to fix all the problems the API has.
Some Q&A:
Q: Why is this not fixing all the problems in the completion API?
A: Because is a blocker for the expr command completion which I want to get in ASAP. This is the
smallest patch that unblocks the expr completion patch and which allows trivial refactoring in the future.
The patch also doesn't really change the internal information flow in the API, so that hopefully
saves us from ever having to revert and resubmit this humongous patch.
Q: Can we merge all the copy-pasted code in the completion methods
(like computing the current incomplete arg) into CompletionRequest class?
A: Yes, but it's out of scope for this patch.
Q: Why the `word_complete = request.GetWordComplete(); ... ` pattern?
A: I don't want to add a getter that returns a reference to the internal integer. So we have
to use a temporary variable and the Getter/Setter instead. We don't throw exceptions
from what I can tell, so the behavior doesn't change.
Q: Why are we not owning the list of matches?
A: Because that's how the previous API works. But that should be fixed too (in another patch).
Q: Can we make the constructor simpler and compute some of the values from the plain command?
A: I think this works, but I rather want to have this in a follow up commit. Especially when making nested
request it's a bit awkward that the parsed arguments behave as both input/output (as we should in theory
propagate the changes on the nested request back to the parent request if we don't want to change the
behavior too much).
Q: Can't we pass one const request object and then just return another result object instead of mixing
them together in one in/out parameter?
A: It's hard to get keep the same behavior with that pattern, but I think we can also get a nice API with just
a single request object. If we make all input parameters read-only, we have a clear separation between what
is actually an input and what an output parameter (and hopefully we get rid of the in-out parameters).
Q: Can we throw out the 'match' variables that are not implemented according to the comment?
A: We currently just forward them as in the old code to the different methods, even though I think
they are really not used. We can easily remove and readd them once every single completion method just
takes a CompletionRequest, but for now I prefer NFC behavior from the perspective of the API user.
Reviewers: davide, jingham, labath
Reviewed By: jingham
Subscribers: mgorny, friss, lldb-commits
Differential Revision: https://reviews.llvm.org/D48796
llvm-svn: 336146
2018-07-03 05:29:56 +08:00
int HandleArgumentCompletion (
CompletionRequest & request ,
OptionElementVector & opt_element_vector ) override {
llvm : : StringRef completion_str =
request . GetParsedLine ( ) [ request . GetCursorIndex ( ) ] . ref ;
completion_str = completion_str . take_front ( request . GetCursorCharPosition ( ) ) ;
2016-09-07 04:57:50 +08:00
Refactoring for for the internal command line completion API (NFC)
Summary:
This patch refactors the internal completion API. It now takes (as far as possible) a single
CompletionRequest object instead o half a dozen in/out/in-out parameters. The CompletionRequest
contains a common superset of the different parameters as far as it makes sense. This includes
the raw command line string and raw cursor position, which should make the `expr` command
possible to implement (at least without hacks that reconstruct the command line from the args).
This patch is not intended to change the observable behavior of lldb in any way. It's also as
minimal as possible and doesn't attempt to fix all the problems the API has.
Some Q&A:
Q: Why is this not fixing all the problems in the completion API?
A: Because is a blocker for the expr command completion which I want to get in ASAP. This is the
smallest patch that unblocks the expr completion patch and which allows trivial refactoring in the future.
The patch also doesn't really change the internal information flow in the API, so that hopefully
saves us from ever having to revert and resubmit this humongous patch.
Q: Can we merge all the copy-pasted code in the completion methods
(like computing the current incomplete arg) into CompletionRequest class?
A: Yes, but it's out of scope for this patch.
Q: Why the `word_complete = request.GetWordComplete(); ... ` pattern?
A: I don't want to add a getter that returns a reference to the internal integer. So we have
to use a temporary variable and the Getter/Setter instead. We don't throw exceptions
from what I can tell, so the behavior doesn't change.
Q: Why are we not owning the list of matches?
A: Because that's how the previous API works. But that should be fixed too (in another patch).
Q: Can we make the constructor simpler and compute some of the values from the plain command?
A: I think this works, but I rather want to have this in a follow up commit. Especially when making nested
request it's a bit awkward that the parsed arguments behave as both input/output (as we should in theory
propagate the changes on the nested request back to the parent request if we don't want to change the
behavior too much).
Q: Can't we pass one const request object and then just return another result object instead of mixing
them together in one in/out parameter?
A: It's hard to get keep the same behavior with that pattern, but I think we can also get a nice API with just
a single request object. If we make all input parameters read-only, we have a clear separation between what
is actually an input and what an output parameter (and hopefully we get rid of the in-out parameters).
Q: Can we throw out the 'match' variables that are not implemented according to the comment?
A: We currently just forward them as in the old code to the different methods, even though I think
they are really not used. We can easily remove and readd them once every single completion method just
takes a CompletionRequest, but for now I prefer NFC behavior from the perspective of the API user.
Reviewers: davide, jingham, labath
Reviewed By: jingham
Subscribers: mgorny, friss, lldb-commits
Differential Revision: https://reviews.llvm.org/D48796
llvm-svn: 336146
2018-07-03 05:29:56 +08:00
bool word_complete = request . GetWordComplete ( ) ;
2016-09-07 04:57:50 +08:00
CommandCompletions : : InvokeCommonCompletionCallbacks (
GetCommandInterpreter ( ) , CommandCompletions : : eDiskFileCompletion ,
Refactoring for for the internal command line completion API (NFC)
Summary:
This patch refactors the internal completion API. It now takes (as far as possible) a single
CompletionRequest object instead o half a dozen in/out/in-out parameters. The CompletionRequest
contains a common superset of the different parameters as far as it makes sense. This includes
the raw command line string and raw cursor position, which should make the `expr` command
possible to implement (at least without hacks that reconstruct the command line from the args).
This patch is not intended to change the observable behavior of lldb in any way. It's also as
minimal as possible and doesn't attempt to fix all the problems the API has.
Some Q&A:
Q: Why is this not fixing all the problems in the completion API?
A: Because is a blocker for the expr command completion which I want to get in ASAP. This is the
smallest patch that unblocks the expr completion patch and which allows trivial refactoring in the future.
The patch also doesn't really change the internal information flow in the API, so that hopefully
saves us from ever having to revert and resubmit this humongous patch.
Q: Can we merge all the copy-pasted code in the completion methods
(like computing the current incomplete arg) into CompletionRequest class?
A: Yes, but it's out of scope for this patch.
Q: Why the `word_complete = request.GetWordComplete(); ... ` pattern?
A: I don't want to add a getter that returns a reference to the internal integer. So we have
to use a temporary variable and the Getter/Setter instead. We don't throw exceptions
from what I can tell, so the behavior doesn't change.
Q: Why are we not owning the list of matches?
A: Because that's how the previous API works. But that should be fixed too (in another patch).
Q: Can we make the constructor simpler and compute some of the values from the plain command?
A: I think this works, but I rather want to have this in a follow up commit. Especially when making nested
request it's a bit awkward that the parsed arguments behave as both input/output (as we should in theory
propagate the changes on the nested request back to the parent request if we don't want to change the
behavior too much).
Q: Can't we pass one const request object and then just return another result object instead of mixing
them together in one in/out parameter?
A: It's hard to get keep the same behavior with that pattern, but I think we can also get a nice API with just
a single request object. If we make all input parameters read-only, we have a clear separation between what
is actually an input and what an output parameter (and hopefully we get rid of the in-out parameters).
Q: Can we throw out the 'match' variables that are not implemented according to the comment?
A: We currently just forward them as in the old code to the different methods, even though I think
they are really not used. We can easily remove and readd them once every single completion method just
takes a CompletionRequest, but for now I prefer NFC behavior from the perspective of the API user.
Reviewers: davide, jingham, labath
Reviewed By: jingham
Subscribers: mgorny, friss, lldb-commits
Differential Revision: https://reviews.llvm.org/D48796
llvm-svn: 336146
2018-07-03 05:29:56 +08:00
completion_str , request . GetMatchStartPoint ( ) ,
request . GetMaxReturnElements ( ) , nullptr , word_complete ,
request . GetMatches ( ) ) ;
request . SetWordComplete ( word_complete ) ;
return request . GetMatches ( ) . GetSize ( ) ;
2016-09-07 04:57:50 +08:00
}
Options * GetOptions ( ) override { return & m_options ; }
protected :
class CommandOptions : public Options {
public :
CommandOptions ( ) : Options ( ) { }
~ CommandOptions ( ) override = default ;
2017-05-12 12:51:55 +08:00
Status SetOptionValue ( uint32_t option_idx , llvm : : StringRef option_arg ,
ExecutionContext * execution_context ) override {
Status error ;
2016-09-07 04:57:50 +08:00
const int short_option = m_getopt_table [ option_idx ] . val ;
switch ( short_option ) {
case ' r ' :
m_allow_reload = true ;
break ;
default :
error . SetErrorStringWithFormat ( " unrecognized option '%c' " ,
short_option ) ;
break ;
}
return error ;
2011-11-08 06:57:04 +08:00
}
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
void OptionParsingStarting ( ExecutionContext * execution_context ) override {
m_allow_reload = true ;
2011-11-08 06:57:04 +08:00
}
2016-09-07 04:57:50 +08:00
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
llvm : : ArrayRef < OptionDefinition > GetDefinitions ( ) override {
2016-09-23 05:06:13 +08:00
return llvm : : makeArrayRef ( g_script_import_options ) ;
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
}
2016-09-07 04:57:50 +08:00
// Instance variables to hold the values for command options.
bool m_allow_reload ;
} ;
bool DoExecute ( Args & command , CommandReturnObject & result ) override {
if ( m_interpreter . GetDebugger ( ) . GetScriptLanguage ( ) ! =
lldb : : eScriptLanguagePython ) {
result . AppendError ( " only scripting language supported for module "
" importing is currently Python " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
2012-06-09 05:56:10 +08:00
}
2011-10-18 05:45:27 +08:00
2016-10-06 04:03:37 +08:00
if ( command . empty ( ) ) {
2016-09-07 04:57:50 +08:00
result . AppendError ( " command script import needs one or more arguments " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
2016-02-23 07:46:47 +08:00
2016-10-06 04:03:37 +08:00
for ( auto & entry : command . entries ( ) ) {
2017-05-12 12:51:55 +08:00
Status error ;
2016-09-07 04:57:50 +08:00
const bool init_session = true ;
// FIXME: this is necessary because CommandObject::CheckRequirements()
2016-10-06 04:03:37 +08:00
// assumes that commands won't ever be recursively invoked, but it's
// actually possible to craft a Python script that does other "command
2018-05-01 00:49:04 +08:00
// script imports" in __lldb_init_module the real fix is to have
// recursive commands possible with a CommandInvocation object separate
// from the CommandObject itself, so that recursive command invocations
// won't stomp on each other (wrt to execution contents, options, and
// more)
2016-09-07 04:57:50 +08:00
m_exe_ctx . Clear ( ) ;
if ( m_interpreter . GetScriptInterpreter ( ) - > LoadScriptingModule (
2016-10-06 04:03:37 +08:00
entry . c_str ( ) , m_options . m_allow_reload , init_session , error ) ) {
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
} else {
result . AppendErrorWithFormat ( " module importing failed: %s " ,
error . AsCString ( ) ) ;
result . SetStatus ( eReturnStatusFailed ) ;
}
2011-10-18 05:45:27 +08:00
}
2016-09-07 04:57:50 +08:00
return result . Succeeded ( ) ;
}
CommandOptions m_options ;
2011-11-08 06:57:04 +08:00
} ;
2011-08-17 07:24:13 +08:00
//-------------------------------------------------------------------------
// CommandObjectCommandsScriptAdd
//-------------------------------------------------------------------------
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
static OptionEnumValueElement g_script_synchro_type [ ] = {
{ eScriptedCommandSynchronicitySynchronous , " synchronous " ,
" Run synchronous " } ,
{ eScriptedCommandSynchronicityAsynchronous , " asynchronous " ,
" Run asynchronous " } ,
{ eScriptedCommandSynchronicityCurrentValue , " current " ,
" Do not alter current setting " } ,
{ 0 , nullptr , nullptr } } ;
static OptionDefinition g_script_add_options [ ] = {
// clang-format off
{ LLDB_OPT_SET_1 , false , " function " , ' f ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypePythonFunction , " Name of the Python function to bind to this command name. " } ,
{ LLDB_OPT_SET_2 , false , " class " , ' c ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypePythonClass , " Name of the Python class to bind to this command name. " } ,
{ LLDB_OPT_SET_1 , false , " help " , ' h ' , OptionParser : : eRequiredArgument , nullptr , nullptr , 0 , eArgTypeHelpText , " The help text to display for this command. " } ,
{ LLDB_OPT_SET_ALL , false , " synchronicity " , ' s ' , OptionParser : : eRequiredArgument , nullptr , g_script_synchro_type , 0 , eArgTypeScriptedCommandSynchronicity , " Set the synchronicity of this command's executions with regard to LLDB event system. " } ,
// clang-format on
} ;
2016-09-07 04:57:50 +08:00
class CommandObjectCommandsScriptAdd : public CommandObjectParsed ,
public IOHandlerDelegateMultiline {
2012-06-09 05:56:10 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectCommandsScriptAdd ( CommandInterpreter & interpreter )
: CommandObjectParsed ( interpreter , " command script add " ,
2016-02-23 07:46:47 +08:00
" Add a scripted function as an LLDB command. " ,
nullptr ) ,
2016-09-07 04:57:50 +08:00
IOHandlerDelegateMultiline ( " DONE " ) , m_options ( ) {
CommandArgumentEntry arg1 ;
CommandArgumentData cmd_arg ;
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
// Define the first (and only) variant of this arg.
cmd_arg . arg_type = eArgTypeCommandName ;
cmd_arg . arg_repetition = eArgRepeatPlain ;
// There is only one variant this argument could be; put it into the
// argument entry.
arg1 . push_back ( cmd_arg ) ;
// Push the data for the first argument into the m_arguments vector.
m_arguments . push_back ( arg1 ) ;
}
~ CommandObjectCommandsScriptAdd ( ) override = default ;
Options * GetOptions ( ) override { return & m_options ; }
2016-02-23 07:46:47 +08:00
2012-06-09 05:56:10 +08:00
protected :
2016-09-07 04:57:50 +08:00
class CommandOptions : public Options {
public :
CommandOptions ( )
: Options ( ) , m_class_name ( ) , m_funct_name ( ) , m_short_help ( ) ,
m_synchronicity ( eScriptedCommandSynchronicitySynchronous ) { }
~ CommandOptions ( ) override = default ;
2017-05-12 12:51:55 +08:00
Status SetOptionValue ( uint32_t option_idx , llvm : : StringRef option_arg ,
ExecutionContext * execution_context ) override {
Status error ;
2016-09-07 04:57:50 +08:00
const int short_option = m_getopt_table [ option_idx ] . val ;
switch ( short_option ) {
case ' f ' :
2016-11-13 00:56:47 +08:00
if ( ! option_arg . empty ( ) )
m_funct_name = option_arg ;
2016-09-07 04:57:50 +08:00
break ;
case ' c ' :
2016-11-13 00:56:47 +08:00
if ( ! option_arg . empty ( ) )
m_class_name = option_arg ;
2016-09-07 04:57:50 +08:00
break ;
case ' h ' :
2016-11-13 00:56:47 +08:00
if ( ! option_arg . empty ( ) )
m_short_help = option_arg ;
2016-09-07 04:57:50 +08:00
break ;
case ' s ' :
m_synchronicity =
2018-04-10 17:03:59 +08:00
( ScriptedCommandSynchronicity ) OptionArgParser : : ToOptionEnum (
2016-11-13 00:56:47 +08:00
option_arg , GetDefinitions ( ) [ option_idx ] . enum_values , 0 , error ) ;
2016-09-07 04:57:50 +08:00
if ( ! error . Success ( ) )
error . SetErrorStringWithFormat (
2016-11-13 00:56:47 +08:00
" unrecognized value for synchronicity '%s' " ,
option_arg . str ( ) . c_str ( ) ) ;
2016-09-07 04:57:50 +08:00
break ;
default :
error . SetErrorStringWithFormat ( " unrecognized option '%c' " ,
short_option ) ;
break ;
}
return error ;
}
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
void OptionParsingStarting ( ExecutionContext * execution_context ) override {
m_class_name . clear ( ) ;
m_funct_name . clear ( ) ;
m_short_help . clear ( ) ;
m_synchronicity = eScriptedCommandSynchronicitySynchronous ;
2014-01-28 07:43:24 +08:00
}
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
llvm : : ArrayRef < OptionDefinition > GetDefinitions ( ) override {
2016-09-23 05:06:13 +08:00
return llvm : : makeArrayRef ( g_script_add_options ) ;
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
}
2014-01-28 07:43:24 +08:00
2016-09-07 04:57:50 +08:00
// Instance variables to hold the values for command options.
std : : string m_class_name ;
std : : string m_funct_name ;
std : : string m_short_help ;
ScriptedCommandSynchronicity m_synchronicity ;
} ;
void IOHandlerActivated ( IOHandler & io_handler ) override {
StreamFileSP output_sp ( io_handler . GetOutputStreamFile ( ) ) ;
if ( output_sp ) {
output_sp - > PutCString ( g_python_command_instructions ) ;
output_sp - > Flush ( ) ;
2014-01-28 07:43:24 +08:00
}
2016-09-07 04:57:50 +08:00
}
void IOHandlerInputComplete ( IOHandler & io_handler ,
std : : string & data ) override {
StreamFileSP error_sp = io_handler . GetErrorStreamFile ( ) ;
ScriptInterpreter * interpreter = m_interpreter . GetScriptInterpreter ( ) ;
if ( interpreter ) {
StringList lines ;
lines . SplitIntoLines ( data ) ;
if ( lines . GetSize ( ) > 0 ) {
std : : string funct_name_str ;
if ( interpreter - > GenerateScriptAliasFunction ( lines , funct_name_str ) ) {
if ( funct_name_str . empty ( ) ) {
error_sp - > Printf ( " error: unable to obtain a function name, didn't "
" add python command. \n " ) ;
error_sp - > Flush ( ) ;
} else {
// everything should be fine now, let's add this alias
CommandObjectSP command_obj_sp ( new CommandObjectPythonFunction (
2016-11-03 04:34:10 +08:00
m_interpreter , m_cmd_name , funct_name_str , m_short_help ,
2016-09-07 04:57:50 +08:00
m_synchronicity ) ) ;
if ( ! m_interpreter . AddUserCommand ( m_cmd_name , command_obj_sp ,
true ) ) {
error_sp - > Printf ( " error: unable to add selected command, didn't "
" add python command. \n " ) ;
error_sp - > Flush ( ) ;
}
}
} else {
error_sp - > Printf (
" error: unable to create function, didn't add python command. \n " ) ;
error_sp - > Flush ( ) ;
}
} else {
error_sp - > Printf ( " error: empty function, didn't add python command. \n " ) ;
error_sp - > Flush ( ) ;
}
} else {
error_sp - > Printf (
" error: script interpreter missing, didn't add python command. \n " ) ;
error_sp - > Flush ( ) ;
}
io_handler . SetIsDone ( true ) ;
}
2014-01-28 07:43:24 +08:00
2012-06-09 05:56:10 +08:00
protected :
2016-09-07 04:57:50 +08:00
bool DoExecute ( Args & command , CommandReturnObject & result ) override {
if ( m_interpreter . GetDebugger ( ) . GetScriptLanguage ( ) ! =
lldb : : eScriptLanguagePython ) {
result . AppendError ( " only scripting language supported for scripted "
" commands is currently Python " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
2011-08-17 07:24:13 +08:00
2016-10-06 04:03:37 +08:00
if ( command . GetArgumentCount ( ) ! = 1 ) {
2016-09-07 04:57:50 +08:00
result . AppendError ( " 'command script add' requires one argument " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
// Store the options in case we get multi-line input
2016-12-08 09:31:04 +08:00
m_cmd_name = command [ 0 ] . ref ;
2016-09-07 04:57:50 +08:00
m_short_help . assign ( m_options . m_short_help ) ;
m_synchronicity = m_options . m_synchronicity ;
if ( m_options . m_class_name . empty ( ) ) {
if ( m_options . m_funct_name . empty ( ) ) {
m_interpreter . GetPythonCommandsFromIOHandler (
" " , // Prompt
* this , // IOHandlerDelegate
true , // Run IOHandler in async mode
nullptr ) ; // Baton for the "io_handler" that will be passed back
// into our IOHandlerDelegate functions
} else {
CommandObjectSP new_cmd ( new CommandObjectPythonFunction (
m_interpreter , m_cmd_name , m_options . m_funct_name ,
m_options . m_short_help , m_synchronicity ) ) ;
if ( m_interpreter . AddUserCommand ( m_cmd_name , new_cmd , true ) ) {
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
} else {
result . AppendError ( " cannot add command " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
}
}
} else {
ScriptInterpreter * interpreter =
GetCommandInterpreter ( ) . GetScriptInterpreter ( ) ;
if ( ! interpreter ) {
result . AppendError ( " cannot find ScriptInterpreter " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
auto cmd_obj_sp = interpreter - > CreateScriptCommandObject (
m_options . m_class_name . c_str ( ) ) ;
if ( ! cmd_obj_sp ) {
result . AppendError ( " cannot create helper object " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
}
CommandObjectSP new_cmd ( new CommandObjectScriptingObject (
m_interpreter , m_cmd_name , cmd_obj_sp , m_synchronicity ) ) ;
if ( m_interpreter . AddUserCommand ( m_cmd_name , new_cmd , true ) ) {
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
} else {
result . AppendError ( " cannot add command " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
}
2011-08-17 07:24:13 +08:00
}
2016-09-07 04:57:50 +08:00
return result . Succeeded ( ) ;
}
CommandOptions m_options ;
std : : string m_cmd_name ;
std : : string m_short_help ;
ScriptedCommandSynchronicity m_synchronicity ;
2011-11-08 06:57:04 +08:00
} ;
2011-08-17 07:24:13 +08:00
//-------------------------------------------------------------------------
// CommandObjectCommandsScriptList
//-------------------------------------------------------------------------
2016-09-07 04:57:50 +08:00
class CommandObjectCommandsScriptList : public CommandObjectParsed {
2011-08-17 07:24:13 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectCommandsScriptList ( CommandInterpreter & interpreter )
: CommandObjectParsed ( interpreter , " command script list " ,
" List defined scripted commands. " , nullptr ) { }
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
~ CommandObjectCommandsScriptList ( ) override = default ;
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
bool DoExecute ( Args & command , CommandReturnObject & result ) override {
m_interpreter . GetHelp ( result , CommandInterpreter : : eCommandTypesUserDef ) ;
result . SetStatus ( eReturnStatusSuccessFinishResult ) ;
return true ;
}
2011-08-17 07:24:13 +08:00
} ;
//-------------------------------------------------------------------------
// CommandObjectCommandsScriptClear
//-------------------------------------------------------------------------
2016-09-07 04:57:50 +08:00
class CommandObjectCommandsScriptClear : public CommandObjectParsed {
2011-08-17 07:24:13 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectCommandsScriptClear ( CommandInterpreter & interpreter )
: CommandObjectParsed ( interpreter , " command script clear " ,
" Delete all scripted commands. " , nullptr ) { }
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
~ CommandObjectCommandsScriptClear ( ) override = default ;
2016-02-23 07:46:47 +08:00
2012-06-09 05:56:10 +08:00
protected :
2016-09-07 04:57:50 +08:00
bool DoExecute ( Args & command , CommandReturnObject & result ) override {
m_interpreter . RemoveAllUser ( ) ;
result . SetStatus ( eReturnStatusSuccessFinishResult ) ;
return true ;
}
2011-08-17 07:24:13 +08:00
} ;
//-------------------------------------------------------------------------
// CommandObjectCommandsScriptDelete
//-------------------------------------------------------------------------
2016-09-07 04:57:50 +08:00
class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
2011-08-17 07:24:13 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectCommandsScriptDelete ( CommandInterpreter & interpreter )
: CommandObjectParsed ( interpreter , " command script delete " ,
" Delete a scripted command. " , nullptr ) {
CommandArgumentEntry arg1 ;
CommandArgumentData cmd_arg ;
// Define the first (and only) variant of this arg.
cmd_arg . arg_type = eArgTypeCommandName ;
cmd_arg . arg_repetition = eArgRepeatPlain ;
2016-02-23 07:46:47 +08:00
2016-09-07 04:57:50 +08:00
// There is only one variant this argument could be; put it into the
// argument entry.
arg1 . push_back ( cmd_arg ) ;
// Push the data for the first argument into the m_arguments vector.
m_arguments . push_back ( arg1 ) ;
}
~ CommandObjectCommandsScriptDelete ( ) override = default ;
2016-02-23 07:46:47 +08:00
2012-06-09 05:56:10 +08:00
protected :
2016-09-07 04:57:50 +08:00
bool DoExecute ( Args & command , CommandReturnObject & result ) override {
2016-10-06 04:03:37 +08:00
if ( command . GetArgumentCount ( ) ! = 1 ) {
2016-09-07 04:57:50 +08:00
result . AppendError ( " 'command script delete' requires one argument " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
2011-08-17 07:24:13 +08:00
}
2016-09-07 04:57:50 +08:00
2016-12-08 09:31:04 +08:00
auto cmd_name = command [ 0 ] . ref ;
2016-09-07 04:57:50 +08:00
2016-12-08 09:31:04 +08:00
if ( cmd_name . empty ( ) | | ! m_interpreter . HasUserCommands ( ) | |
! m_interpreter . UserCommandExists ( cmd_name ) ) {
2016-12-09 09:20:58 +08:00
result . AppendErrorWithFormat ( " command %s not found " , command [ 0 ] . c_str ( ) ) ;
2016-09-07 04:57:50 +08:00
result . SetStatus ( eReturnStatusFailed ) ;
2016-12-08 09:31:04 +08:00
return false ;
2016-09-07 04:57:50 +08:00
}
2016-12-08 09:31:04 +08:00
m_interpreter . RemoveUser ( cmd_name ) ;
result . SetStatus ( eReturnStatusSuccessFinishResult ) ;
return true ;
2016-09-07 04:57:50 +08:00
}
2011-08-17 07:24:13 +08:00
} ;
# pragma mark CommandObjectMultiwordCommandsScript
//-------------------------------------------------------------------------
// CommandObjectMultiwordCommandsScript
//-------------------------------------------------------------------------
2016-09-07 04:57:50 +08:00
class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword {
2011-08-17 07:24:13 +08:00
public :
2016-09-07 04:57:50 +08:00
CommandObjectMultiwordCommandsScript ( CommandInterpreter & interpreter )
: CommandObjectMultiword (
interpreter , " command script " , " Commands for managing custom "
" commands implemented by "
" interpreter scripts. " ,
" command script <subcommand> [<subcommand-options>] " ) {
LoadSubCommand ( " add " , CommandObjectSP (
new CommandObjectCommandsScriptAdd ( interpreter ) ) ) ;
LoadSubCommand (
" delete " ,
CommandObjectSP ( new CommandObjectCommandsScriptDelete ( interpreter ) ) ) ;
LoadSubCommand (
" clear " ,
CommandObjectSP ( new CommandObjectCommandsScriptClear ( interpreter ) ) ) ;
LoadSubCommand ( " list " , CommandObjectSP ( new CommandObjectCommandsScriptList (
interpreter ) ) ) ;
LoadSubCommand (
" import " ,
CommandObjectSP ( new CommandObjectCommandsScriptImport ( interpreter ) ) ) ;
}
~ CommandObjectMultiwordCommandsScript ( ) override = default ;
2011-08-17 07:24:13 +08:00
} ;
2010-07-07 11:36:20 +08:00
# pragma mark CommandObjectMultiwordCommands
//-------------------------------------------------------------------------
// CommandObjectMultiwordCommands
//-------------------------------------------------------------------------
2016-09-07 04:57:50 +08:00
CommandObjectMultiwordCommands : : CommandObjectMultiwordCommands (
CommandInterpreter & interpreter )
: CommandObjectMultiword ( interpreter , " command " ,
" Commands for managing custom LLDB commands. " ,
" command <subcommand> [<subcommand-options>] " ) {
LoadSubCommand ( " source " ,
CommandObjectSP ( new CommandObjectCommandsSource ( interpreter ) ) ) ;
LoadSubCommand ( " alias " ,
CommandObjectSP ( new CommandObjectCommandsAlias ( interpreter ) ) ) ;
LoadSubCommand ( " unalias " , CommandObjectSP (
new CommandObjectCommandsUnalias ( interpreter ) ) ) ;
LoadSubCommand ( " delete " ,
CommandObjectSP ( new CommandObjectCommandsDelete ( interpreter ) ) ) ;
LoadSubCommand (
" regex " , CommandObjectSP ( new CommandObjectCommandsAddRegex ( interpreter ) ) ) ;
LoadSubCommand ( " history " , CommandObjectSP (
new CommandObjectCommandsHistory ( interpreter ) ) ) ;
LoadSubCommand (
" script " ,
CommandObjectSP ( new CommandObjectMultiwordCommandsScript ( interpreter ) ) ) ;
2010-07-07 11:36:20 +08:00
}
2016-02-23 07:46:47 +08:00
CommandObjectMultiwordCommands : : ~ CommandObjectMultiwordCommands ( ) = default ;