130fdc8d8SChris Lattner //===-- CommandObjectHelp.cpp -----------------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 330fdc8d8SChris Lattner // The LLVM Compiler Infrastructure 430fdc8d8SChris Lattner // 530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source 630fdc8d8SChris Lattner // License. See LICENSE.TXT for details. 730fdc8d8SChris Lattner // 830fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 930fdc8d8SChris Lattner 10*93a64300SDaniel Malea #include "lldb/lldb-python.h" 11*93a64300SDaniel Malea 1230fdc8d8SChris Lattner #include "CommandObjectHelp.h" 1330fdc8d8SChris Lattner 1430fdc8d8SChris Lattner // C Includes 1530fdc8d8SChris Lattner // C++ Includes 1630fdc8d8SChris Lattner // Other libraries and framework includes 1730fdc8d8SChris Lattner // Project includes 1830fdc8d8SChris Lattner #include "lldb/Interpreter/CommandObjectMultiword.h" 1930fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h" 2040af72e1SJim Ingham #include "lldb/Interpreter/Options.h" 2130fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 2230fdc8d8SChris Lattner 2330fdc8d8SChris Lattner using namespace lldb; 2430fdc8d8SChris Lattner using namespace lldb_private; 2530fdc8d8SChris Lattner 2630fdc8d8SChris Lattner //------------------------------------------------------------------------- 2730fdc8d8SChris Lattner // CommandObjectHelp 2830fdc8d8SChris Lattner //------------------------------------------------------------------------- 2930fdc8d8SChris Lattner 30a7015092SGreg Clayton CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) : 315a988416SJim Ingham CommandObjectParsed (interpreter, 32a7015092SGreg Clayton "help", 333f4c09c1SCaroline Tice "Show a list of all debugger commands, or give details about specific commands.", 3408633eeaSEnrico Granata "help [<cmd-name>]"), m_options (interpreter) 3530fdc8d8SChris Lattner { 36405fe67fSCaroline Tice CommandArgumentEntry arg; 37405fe67fSCaroline Tice CommandArgumentData command_arg; 38405fe67fSCaroline Tice 39405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 40405fe67fSCaroline Tice command_arg.arg_type = eArgTypeCommandName; 41405fe67fSCaroline Tice command_arg.arg_repetition = eArgRepeatStar; 42405fe67fSCaroline Tice 43405fe67fSCaroline Tice // There is only one variant this argument could be; put it into the argument entry. 44405fe67fSCaroline Tice arg.push_back (command_arg); 45405fe67fSCaroline Tice 46405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 47405fe67fSCaroline Tice m_arguments.push_back (arg); 4830fdc8d8SChris Lattner } 4930fdc8d8SChris Lattner 5030fdc8d8SChris Lattner CommandObjectHelp::~CommandObjectHelp() 5130fdc8d8SChris Lattner { 5230fdc8d8SChris Lattner } 5330fdc8d8SChris Lattner 5408633eeaSEnrico Granata OptionDefinition 5508633eeaSEnrico Granata CommandObjectHelp::CommandOptions::g_option_table[] = 5608633eeaSEnrico Granata { 5708633eeaSEnrico Granata { LLDB_OPT_SET_ALL, false, "show-aliases", 'a', no_argument, NULL, 0, eArgTypeNone, "Show aliases in the command list."}, 5808633eeaSEnrico Granata { LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', no_argument, NULL, 0, eArgTypeNone, "Hide user-defined commands from the list."}, 5908633eeaSEnrico Granata { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } 6008633eeaSEnrico Granata }; 6108633eeaSEnrico Granata 6230fdc8d8SChris Lattner bool 635a988416SJim Ingham CommandObjectHelp::DoExecute (Args& command, CommandReturnObject &result) 6430fdc8d8SChris Lattner { 6530fdc8d8SChris Lattner CommandObject::CommandMap::iterator pos; 6630fdc8d8SChris Lattner CommandObject *cmd_obj; 6730fdc8d8SChris Lattner const int argc = command.GetArgumentCount (); 6830fdc8d8SChris Lattner 6908633eeaSEnrico Granata // 'help' doesn't take any arguments, other than command names. If argc is 0, we show the user 7008633eeaSEnrico Granata // all commands (aliases and user commands if asked for). Otherwise every argument must be the name of a command or a sub-command. 7130fdc8d8SChris Lattner if (argc == 0) 7230fdc8d8SChris Lattner { 7308633eeaSEnrico Granata uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin; 7408633eeaSEnrico Granata if (m_options.m_show_aliases) 7508633eeaSEnrico Granata cmd_types |= CommandInterpreter::eCommandTypesAliases; 7608633eeaSEnrico Granata if (m_options.m_show_user_defined) 7708633eeaSEnrico Granata cmd_types |= CommandInterpreter::eCommandTypesUserDef; 7808633eeaSEnrico Granata 7930fdc8d8SChris Lattner result.SetStatus (eReturnStatusSuccessFinishNoResult); 8008633eeaSEnrico Granata m_interpreter.GetHelp (result, cmd_types); // General help 8130fdc8d8SChris Lattner } 8230fdc8d8SChris Lattner else 8330fdc8d8SChris Lattner { 8430fdc8d8SChris Lattner // Get command object for the first command argument. Only search built-in command dictionary. 85279a6c26SJim Ingham StringList matches; 86a7015092SGreg Clayton cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches); 87e7941795SCaroline Tice bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0)); 88e7941795SCaroline Tice std::string alias_name = command.GetArgumentAtIndex(0); 8930fdc8d8SChris Lattner 9030fdc8d8SChris Lattner if (cmd_obj != NULL) 9130fdc8d8SChris Lattner { 92271ad29aSJim Ingham StringList matches; 9330fdc8d8SChris Lattner bool all_okay = true; 9430fdc8d8SChris Lattner CommandObject *sub_cmd_obj = cmd_obj; 9530fdc8d8SChris Lattner // Loop down through sub_command dictionaries until we find the command object that corresponds 9630fdc8d8SChris Lattner // to the help command entered. 9730fdc8d8SChris Lattner for (int i = 1; i < argc && all_okay; ++i) 9830fdc8d8SChris Lattner { 9930fdc8d8SChris Lattner std::string sub_command = command.GetArgumentAtIndex(i); 100271ad29aSJim Ingham matches.Clear(); 10130fdc8d8SChris Lattner if (! sub_cmd_obj->IsMultiwordObject ()) 10230fdc8d8SChris Lattner { 10330fdc8d8SChris Lattner all_okay = false; 10430fdc8d8SChris Lattner } 10530fdc8d8SChris Lattner else 10630fdc8d8SChris Lattner { 107271ad29aSJim Ingham CommandObject *found_cmd; 108998255bfSGreg Clayton found_cmd = sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches); 109271ad29aSJim Ingham if (found_cmd == NULL) 11030fdc8d8SChris Lattner all_okay = false; 11197253e62SJim Ingham else if (matches.GetSize() > 1) 112271ad29aSJim Ingham all_okay = false; 113271ad29aSJim Ingham else 114271ad29aSJim Ingham sub_cmd_obj = found_cmd; 11530fdc8d8SChris Lattner } 11630fdc8d8SChris Lattner } 11730fdc8d8SChris Lattner 11830fdc8d8SChris Lattner if (!all_okay || (sub_cmd_obj == NULL)) 11930fdc8d8SChris Lattner { 12030fdc8d8SChris Lattner std::string cmd_string; 12130fdc8d8SChris Lattner command.GetCommandString (cmd_string); 122271ad29aSJim Ingham if (matches.GetSize() < 2) 123271ad29aSJim Ingham { 124271ad29aSJim Ingham result.AppendErrorWithFormat("'%s' is not a known command.\n" 125271ad29aSJim Ingham "Try 'help' to see a current list of commands.\n", 12630fdc8d8SChris Lattner cmd_string.c_str()); 127271ad29aSJim Ingham } 128271ad29aSJim Ingham else 129271ad29aSJim Ingham { 130271ad29aSJim Ingham StreamString s; 131271ad29aSJim Ingham s.Printf ("ambiguous command %s", cmd_string.c_str()); 132271ad29aSJim Ingham size_t num_matches = matches.GetSize(); 133271ad29aSJim Ingham for (size_t match_idx = 0; match_idx < num_matches; match_idx++) 134271ad29aSJim Ingham { 135271ad29aSJim Ingham s.Printf ("\n\t%s", matches.GetStringAtIndex(match_idx)); 136271ad29aSJim Ingham } 137271ad29aSJim Ingham s.Printf ("\n"); 138271ad29aSJim Ingham result.AppendError(s.GetData()); 139271ad29aSJim Ingham } 140271ad29aSJim Ingham 14130fdc8d8SChris Lattner result.SetStatus (eReturnStatusFailed); 14230fdc8d8SChris Lattner } 14330fdc8d8SChris Lattner else 14430fdc8d8SChris Lattner { 14530fdc8d8SChris Lattner Stream &output_strm = result.GetOutputStream(); 14630fdc8d8SChris Lattner if (sub_cmd_obj->GetOptions() != NULL) 14730fdc8d8SChris Lattner { 148e139cf23SCaroline Tice if (sub_cmd_obj->WantsRawCommandString()) 149e139cf23SCaroline Tice { 150e139cf23SCaroline Tice std::string help_text (sub_cmd_obj->GetHelp()); 151e139cf23SCaroline Tice help_text.append (" This command takes 'raw' input (no need to quote stuff)."); 152e139cf23SCaroline Tice m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); 153e139cf23SCaroline Tice } 154e139cf23SCaroline Tice else 155a7015092SGreg Clayton m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 15630fdc8d8SChris Lattner output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); 157eb0103f2SGreg Clayton sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj); 15830fdc8d8SChris Lattner const char *long_help = sub_cmd_obj->GetHelpLong(); 15930fdc8d8SChris Lattner if ((long_help != NULL) 16030fdc8d8SChris Lattner && (strlen (long_help) > 0)) 16130fdc8d8SChris Lattner output_strm.Printf ("\n%s", long_help); 162921ca5d4SJohnny Chen if (sub_cmd_obj->WantsRawCommandString() && !sub_cmd_obj->WantsCompletion()) 163d9d63369SCaroline Tice { 164c007e846SJim Ingham // Emit the message about using ' -- ' between the end of the command options and the raw input 165c007e846SJim Ingham // conditionally, i.e., only if the command object does not want completion. 166c007e846SJim Ingham m_interpreter.OutputFormattedHelpText (output_strm, "", "", 167c007e846SJim Ingham "\nIMPORTANT NOTE: Because this command takes 'raw' input, if you use any command options" 168c007e846SJim Ingham " you must use ' -- ' between the end of the command options and the beginning of the raw input.", 1); 169d9d63369SCaroline Tice } 170c007e846SJim Ingham else if (sub_cmd_obj->GetNumArgumentEntries() > 0 171c007e846SJim Ingham && sub_cmd_obj->GetOptions() 172c007e846SJim Ingham && sub_cmd_obj->GetOptions()->NumCommandOptions() > 0) 173c007e846SJim Ingham { 174c007e846SJim Ingham // Also emit a warning about using "--" in case you are using a command that takes options and arguments. 175c007e846SJim Ingham m_interpreter.OutputFormattedHelpText (output_strm, "", "", 176c007e846SJim Ingham "\nThis command takes options and arguments, if your arguments look like option specifiers" 177c007e846SJim Ingham " you must use '--' to terminate the options before starting to give the arguments.", 1); 178c007e846SJim Ingham } 179c007e846SJim Ingham 180921ca5d4SJohnny Chen // Mark this help command with a success status. 18165045f21SJohnny Chen result.SetStatus (eReturnStatusSuccessFinishNoResult); 18230fdc8d8SChris Lattner } 18330fdc8d8SChris Lattner else if (sub_cmd_obj->IsMultiwordObject()) 18430fdc8d8SChris Lattner { 185e139cf23SCaroline Tice if (sub_cmd_obj->WantsRawCommandString()) 186e139cf23SCaroline Tice { 187e139cf23SCaroline Tice std::string help_text (sub_cmd_obj->GetHelp()); 188e139cf23SCaroline Tice help_text.append (" This command takes 'raw' input (no need to quote stuff)."); 189e139cf23SCaroline Tice m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); 190e139cf23SCaroline Tice } 191e139cf23SCaroline Tice else 192a7015092SGreg Clayton m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 193998255bfSGreg Clayton sub_cmd_obj->GenerateHelpText (result); 19430fdc8d8SChris Lattner } 19530fdc8d8SChris Lattner else 19630fdc8d8SChris Lattner { 19730fdc8d8SChris Lattner const char *long_help = sub_cmd_obj->GetHelpLong(); 19830fdc8d8SChris Lattner if ((long_help != NULL) 19930fdc8d8SChris Lattner && (strlen (long_help) > 0)) 20087637809SJohnny Chen output_strm.Printf ("%s", long_help); 201e139cf23SCaroline Tice else if (sub_cmd_obj->WantsRawCommandString()) 202e139cf23SCaroline Tice { 203e139cf23SCaroline Tice std::string help_text (sub_cmd_obj->GetHelp()); 204e139cf23SCaroline Tice help_text.append (" This command takes 'raw' input (no need to quote stuff)."); 205e139cf23SCaroline Tice m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); 206e139cf23SCaroline Tice } 20730fdc8d8SChris Lattner else 208a7015092SGreg Clayton m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 20930fdc8d8SChris Lattner output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); 21065045f21SJohnny Chen // Mark this help command with a success status. 21165045f21SJohnny Chen result.SetStatus (eReturnStatusSuccessFinishNoResult); 21230fdc8d8SChris Lattner } 21330fdc8d8SChris Lattner } 214e7941795SCaroline Tice 215e7941795SCaroline Tice if (is_alias_command) 216e7941795SCaroline Tice { 217e7941795SCaroline Tice StreamString sstr; 218e7941795SCaroline Tice m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr); 219e7941795SCaroline Tice result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData()); 220e7941795SCaroline Tice } 22130fdc8d8SChris Lattner } 222279a6c26SJim Ingham else if (matches.GetSize() > 0) 223279a6c26SJim Ingham { 224279a6c26SJim Ingham Stream &output_strm = result.GetOutputStream(); 225279a6c26SJim Ingham output_strm.Printf("Help requested with ambiguous command name, possible completions:\n"); 226c982c768SGreg Clayton const uint32_t match_count = matches.GetSize(); 227c982c768SGreg Clayton for (uint32_t i = 0; i < match_count; i++) 228279a6c26SJim Ingham { 229279a6c26SJim Ingham output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); 230279a6c26SJim Ingham } 231279a6c26SJim Ingham } 23230fdc8d8SChris Lattner else 23330fdc8d8SChris Lattner { 234e139cf23SCaroline Tice // Maybe the user is asking for help about a command argument rather than a command. 235e139cf23SCaroline Tice const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0)); 236e139cf23SCaroline Tice if (arg_type != eArgTypeLastArg) 237e139cf23SCaroline Tice { 238e139cf23SCaroline Tice Stream &output_strm = result.GetOutputStream (); 239e139cf23SCaroline Tice CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter); 240e139cf23SCaroline Tice result.SetStatus (eReturnStatusSuccessFinishNoResult); 241e139cf23SCaroline Tice } 242e139cf23SCaroline Tice else 243e139cf23SCaroline Tice { 24430fdc8d8SChris Lattner result.AppendErrorWithFormat 24530fdc8d8SChris Lattner ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", 24630fdc8d8SChris Lattner command.GetArgumentAtIndex(0)); 24730fdc8d8SChris Lattner result.SetStatus (eReturnStatusFailed); 24830fdc8d8SChris Lattner } 24930fdc8d8SChris Lattner } 250e139cf23SCaroline Tice } 25130fdc8d8SChris Lattner 25230fdc8d8SChris Lattner return result.Succeeded(); 25330fdc8d8SChris Lattner } 25430fdc8d8SChris Lattner 25530fdc8d8SChris Lattner int 25630fdc8d8SChris Lattner CommandObjectHelp::HandleCompletion 25730fdc8d8SChris Lattner ( 25830fdc8d8SChris Lattner Args &input, 25930fdc8d8SChris Lattner int &cursor_index, 26030fdc8d8SChris Lattner int &cursor_char_position, 26130fdc8d8SChris Lattner int match_start_point, 26230fdc8d8SChris Lattner int max_return_elements, 263558ce124SJim Ingham bool &word_complete, 26430fdc8d8SChris Lattner StringList &matches 26530fdc8d8SChris Lattner ) 26630fdc8d8SChris Lattner { 26730fdc8d8SChris Lattner // Return the completions of the commands in the help system: 26830fdc8d8SChris Lattner if (cursor_index == 0) 26930fdc8d8SChris Lattner { 270a7015092SGreg Clayton return m_interpreter.HandleCompletionMatches (input, 271a7015092SGreg Clayton cursor_index, 272a7015092SGreg Clayton cursor_char_position, 273a7015092SGreg Clayton match_start_point, 274a7015092SGreg Clayton max_return_elements, 275a7015092SGreg Clayton word_complete, 276a7015092SGreg Clayton matches); 27730fdc8d8SChris Lattner } 27830fdc8d8SChris Lattner else 27930fdc8d8SChris Lattner { 280a7015092SGreg Clayton CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0)); 2811d18ebf6SJim Ingham 2821d18ebf6SJim Ingham // The command that they are getting help on might be ambiguous, in which case we should complete that, 2831d18ebf6SJim Ingham // otherwise complete with the command the user is getting help on... 2841d18ebf6SJim Ingham 2851d18ebf6SJim Ingham if (cmd_obj) 2861d18ebf6SJim Ingham { 28730fdc8d8SChris Lattner input.Shift(); 28830fdc8d8SChris Lattner cursor_index--; 289a7015092SGreg Clayton return cmd_obj->HandleCompletion (input, 290a7015092SGreg Clayton cursor_index, 291a7015092SGreg Clayton cursor_char_position, 292a7015092SGreg Clayton match_start_point, 293a7015092SGreg Clayton max_return_elements, 294a7015092SGreg Clayton word_complete, 295a7015092SGreg Clayton matches); 29630fdc8d8SChris Lattner } 2971d18ebf6SJim Ingham else 2981d18ebf6SJim Ingham { 2991d18ebf6SJim Ingham return m_interpreter.HandleCompletionMatches (input, 3001d18ebf6SJim Ingham cursor_index, 3011d18ebf6SJim Ingham cursor_char_position, 3021d18ebf6SJim Ingham match_start_point, 3031d18ebf6SJim Ingham max_return_elements, 3041d18ebf6SJim Ingham word_complete, 3051d18ebf6SJim Ingham matches); 3061d18ebf6SJim Ingham } 3071d18ebf6SJim Ingham } 30830fdc8d8SChris Lattner } 309