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 1030fdc8d8SChris Lattner #include "CommandObjectHelp.h" 1130fdc8d8SChris Lattner 1230fdc8d8SChris Lattner // C Includes 1330fdc8d8SChris Lattner // C++ Includes 1430fdc8d8SChris Lattner // Other libraries and framework includes 1530fdc8d8SChris Lattner // Project includes 1630fdc8d8SChris Lattner #include "lldb/Interpreter/CommandObjectMultiword.h" 1730fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h" 1840af72e1SJim Ingham #include "lldb/Interpreter/Options.h" 1930fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 2030fdc8d8SChris Lattner 2130fdc8d8SChris Lattner using namespace lldb; 2230fdc8d8SChris Lattner using namespace lldb_private; 2330fdc8d8SChris Lattner 2430fdc8d8SChris Lattner //------------------------------------------------------------------------- 2530fdc8d8SChris Lattner // CommandObjectHelp 2630fdc8d8SChris Lattner //------------------------------------------------------------------------- 2730fdc8d8SChris Lattner 28a7015092SGreg Clayton CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) : 29a7015092SGreg Clayton CommandObject (interpreter, 30a7015092SGreg Clayton "help", 313f4c09c1SCaroline Tice "Show a list of all debugger commands, or give details about specific commands.", 3208633eeaSEnrico Granata "help [<cmd-name>]"), m_options (interpreter) 3330fdc8d8SChris Lattner { 34405fe67fSCaroline Tice CommandArgumentEntry arg; 35405fe67fSCaroline Tice CommandArgumentData command_arg; 36405fe67fSCaroline Tice 37405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 38405fe67fSCaroline Tice command_arg.arg_type = eArgTypeCommandName; 39405fe67fSCaroline Tice command_arg.arg_repetition = eArgRepeatStar; 40405fe67fSCaroline Tice 41405fe67fSCaroline Tice // There is only one variant this argument could be; put it into the argument entry. 42405fe67fSCaroline Tice arg.push_back (command_arg); 43405fe67fSCaroline Tice 44405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 45405fe67fSCaroline Tice m_arguments.push_back (arg); 4630fdc8d8SChris Lattner } 4730fdc8d8SChris Lattner 4830fdc8d8SChris Lattner CommandObjectHelp::~CommandObjectHelp() 4930fdc8d8SChris Lattner { 5030fdc8d8SChris Lattner } 5130fdc8d8SChris Lattner 5208633eeaSEnrico Granata OptionDefinition 5308633eeaSEnrico Granata CommandObjectHelp::CommandOptions::g_option_table[] = 5408633eeaSEnrico Granata { 5508633eeaSEnrico Granata { LLDB_OPT_SET_ALL, false, "show-aliases", 'a', no_argument, NULL, 0, eArgTypeNone, "Show aliases in the command list."}, 5608633eeaSEnrico Granata { LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', no_argument, NULL, 0, eArgTypeNone, "Hide user-defined commands from the list."}, 5708633eeaSEnrico Granata { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } 5808633eeaSEnrico Granata }; 5908633eeaSEnrico Granata 6030fdc8d8SChris Lattner bool 61a7015092SGreg Clayton CommandObjectHelp::Execute (Args& command, CommandReturnObject &result) 6230fdc8d8SChris Lattner { 6330fdc8d8SChris Lattner CommandObject::CommandMap::iterator pos; 6430fdc8d8SChris Lattner CommandObject *cmd_obj; 6530fdc8d8SChris Lattner const int argc = command.GetArgumentCount (); 6630fdc8d8SChris Lattner 6708633eeaSEnrico Granata // 'help' doesn't take any arguments, other than command names. If argc is 0, we show the user 6808633eeaSEnrico Granata // all commands (aliases and user commands if asked for). Otherwise every argument must be the name of a command or a sub-command. 6930fdc8d8SChris Lattner if (argc == 0) 7030fdc8d8SChris Lattner { 7108633eeaSEnrico Granata uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin; 7208633eeaSEnrico Granata if (m_options.m_show_aliases) 7308633eeaSEnrico Granata cmd_types |= CommandInterpreter::eCommandTypesAliases; 7408633eeaSEnrico Granata if (m_options.m_show_user_defined) 7508633eeaSEnrico Granata cmd_types |= CommandInterpreter::eCommandTypesUserDef; 7608633eeaSEnrico Granata 7730fdc8d8SChris Lattner result.SetStatus (eReturnStatusSuccessFinishNoResult); 7808633eeaSEnrico Granata m_interpreter.GetHelp (result, cmd_types); // General help 7930fdc8d8SChris Lattner } 8030fdc8d8SChris Lattner else 8130fdc8d8SChris Lattner { 8230fdc8d8SChris Lattner // Get command object for the first command argument. Only search built-in command dictionary. 83279a6c26SJim Ingham StringList matches; 84a7015092SGreg Clayton cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches); 85e7941795SCaroline Tice bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0)); 86e7941795SCaroline Tice std::string alias_name = command.GetArgumentAtIndex(0); 8730fdc8d8SChris Lattner 8830fdc8d8SChris Lattner if (cmd_obj != NULL) 8930fdc8d8SChris Lattner { 90271ad29aSJim Ingham StringList matches; 9130fdc8d8SChris Lattner bool all_okay = true; 9230fdc8d8SChris Lattner CommandObject *sub_cmd_obj = cmd_obj; 9330fdc8d8SChris Lattner // Loop down through sub_command dictionaries until we find the command object that corresponds 9430fdc8d8SChris Lattner // to the help command entered. 9530fdc8d8SChris Lattner for (int i = 1; i < argc && all_okay; ++i) 9630fdc8d8SChris Lattner { 9730fdc8d8SChris Lattner std::string sub_command = command.GetArgumentAtIndex(i); 98271ad29aSJim Ingham matches.Clear(); 9930fdc8d8SChris Lattner if (! sub_cmd_obj->IsMultiwordObject ()) 10030fdc8d8SChris Lattner { 10130fdc8d8SChris Lattner all_okay = false; 10230fdc8d8SChris Lattner } 10330fdc8d8SChris Lattner else 10430fdc8d8SChris Lattner { 105271ad29aSJim Ingham CommandObject *found_cmd; 106271ad29aSJim Ingham found_cmd = ((CommandObjectMultiword *) sub_cmd_obj)->GetSubcommandObject(sub_command.c_str(), 107271ad29aSJim Ingham &matches); 108271ad29aSJim Ingham if (found_cmd == NULL) 10930fdc8d8SChris Lattner all_okay = false; 11097253e62SJim Ingham else if (matches.GetSize() > 1) 111271ad29aSJim Ingham all_okay = false; 112271ad29aSJim Ingham else 113271ad29aSJim Ingham sub_cmd_obj = found_cmd; 11430fdc8d8SChris Lattner } 11530fdc8d8SChris Lattner } 11630fdc8d8SChris Lattner 11730fdc8d8SChris Lattner if (!all_okay || (sub_cmd_obj == NULL)) 11830fdc8d8SChris Lattner { 11930fdc8d8SChris Lattner std::string cmd_string; 12030fdc8d8SChris Lattner command.GetCommandString (cmd_string); 121271ad29aSJim Ingham if (matches.GetSize() < 2) 122271ad29aSJim Ingham { 123271ad29aSJim Ingham result.AppendErrorWithFormat("'%s' is not a known command.\n" 124271ad29aSJim Ingham "Try 'help' to see a current list of commands.\n", 12530fdc8d8SChris Lattner cmd_string.c_str()); 126271ad29aSJim Ingham } 127271ad29aSJim Ingham else 128271ad29aSJim Ingham { 129271ad29aSJim Ingham StreamString s; 130271ad29aSJim Ingham s.Printf ("ambiguous command %s", cmd_string.c_str()); 131271ad29aSJim Ingham size_t num_matches = matches.GetSize(); 132271ad29aSJim Ingham for (size_t match_idx = 0; match_idx < num_matches; match_idx++) 133271ad29aSJim Ingham { 134271ad29aSJim Ingham s.Printf ("\n\t%s", matches.GetStringAtIndex(match_idx)); 135271ad29aSJim Ingham } 136271ad29aSJim Ingham s.Printf ("\n"); 137271ad29aSJim Ingham result.AppendError(s.GetData()); 138271ad29aSJim Ingham } 139271ad29aSJim Ingham 14030fdc8d8SChris Lattner result.SetStatus (eReturnStatusFailed); 14130fdc8d8SChris Lattner } 14230fdc8d8SChris Lattner else 14330fdc8d8SChris Lattner { 14430fdc8d8SChris Lattner Stream &output_strm = result.GetOutputStream(); 14530fdc8d8SChris Lattner if (sub_cmd_obj->GetOptions() != NULL) 14630fdc8d8SChris Lattner { 147e139cf23SCaroline Tice if (sub_cmd_obj->WantsRawCommandString()) 148e139cf23SCaroline Tice { 149e139cf23SCaroline Tice std::string help_text (sub_cmd_obj->GetHelp()); 150e139cf23SCaroline Tice help_text.append (" This command takes 'raw' input (no need to quote stuff)."); 151e139cf23SCaroline Tice m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); 152e139cf23SCaroline Tice } 153e139cf23SCaroline Tice else 154a7015092SGreg Clayton m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 15530fdc8d8SChris Lattner output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); 156eb0103f2SGreg Clayton sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj); 15730fdc8d8SChris Lattner const char *long_help = sub_cmd_obj->GetHelpLong(); 15830fdc8d8SChris Lattner if ((long_help != NULL) 15930fdc8d8SChris Lattner && (strlen (long_help) > 0)) 16030fdc8d8SChris Lattner output_strm.Printf ("\n%s", long_help); 161921ca5d4SJohnny Chen if (sub_cmd_obj->WantsRawCommandString() && !sub_cmd_obj->WantsCompletion()) 162d9d63369SCaroline Tice { 163*c007e846SJim Ingham // Emit the message about using ' -- ' between the end of the command options and the raw input 164*c007e846SJim Ingham // conditionally, i.e., only if the command object does not want completion. 165*c007e846SJim Ingham m_interpreter.OutputFormattedHelpText (output_strm, "", "", 166*c007e846SJim Ingham "\nIMPORTANT NOTE: Because this command takes 'raw' input, if you use any command options" 167*c007e846SJim Ingham " you must use ' -- ' between the end of the command options and the beginning of the raw input.", 1); 168d9d63369SCaroline Tice } 169*c007e846SJim Ingham else if (sub_cmd_obj->GetNumArgumentEntries() > 0 170*c007e846SJim Ingham && sub_cmd_obj->GetOptions() 171*c007e846SJim Ingham && sub_cmd_obj->GetOptions()->NumCommandOptions() > 0) 172*c007e846SJim Ingham { 173*c007e846SJim Ingham // Also emit a warning about using "--" in case you are using a command that takes options and arguments. 174*c007e846SJim Ingham m_interpreter.OutputFormattedHelpText (output_strm, "", "", 175*c007e846SJim Ingham "\nThis command takes options and arguments, if your arguments look like option specifiers" 176*c007e846SJim Ingham " you must use '--' to terminate the options before starting to give the arguments.", 1); 177*c007e846SJim Ingham } 178*c007e846SJim Ingham 179921ca5d4SJohnny Chen // Mark this help command with a success status. 18065045f21SJohnny Chen result.SetStatus (eReturnStatusSuccessFinishNoResult); 18130fdc8d8SChris Lattner } 18230fdc8d8SChris Lattner else if (sub_cmd_obj->IsMultiwordObject()) 18330fdc8d8SChris Lattner { 184e139cf23SCaroline Tice if (sub_cmd_obj->WantsRawCommandString()) 185e139cf23SCaroline Tice { 186e139cf23SCaroline Tice std::string help_text (sub_cmd_obj->GetHelp()); 187e139cf23SCaroline Tice help_text.append (" This command takes 'raw' input (no need to quote stuff)."); 188e139cf23SCaroline Tice m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); 189e139cf23SCaroline Tice } 190e139cf23SCaroline Tice else 191a7015092SGreg Clayton m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 192a7015092SGreg Clayton ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result); 19330fdc8d8SChris Lattner } 19430fdc8d8SChris Lattner else 19530fdc8d8SChris Lattner { 19630fdc8d8SChris Lattner const char *long_help = sub_cmd_obj->GetHelpLong(); 19730fdc8d8SChris Lattner if ((long_help != NULL) 19830fdc8d8SChris Lattner && (strlen (long_help) > 0)) 19987637809SJohnny Chen output_strm.Printf ("%s", long_help); 200e139cf23SCaroline Tice else if (sub_cmd_obj->WantsRawCommandString()) 201e139cf23SCaroline Tice { 202e139cf23SCaroline Tice std::string help_text (sub_cmd_obj->GetHelp()); 203e139cf23SCaroline Tice help_text.append (" This command takes 'raw' input (no need to quote stuff)."); 204e139cf23SCaroline Tice m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); 205e139cf23SCaroline Tice } 20630fdc8d8SChris Lattner else 207a7015092SGreg Clayton m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 20830fdc8d8SChris Lattner output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); 20965045f21SJohnny Chen // Mark this help command with a success status. 21065045f21SJohnny Chen result.SetStatus (eReturnStatusSuccessFinishNoResult); 21130fdc8d8SChris Lattner } 21230fdc8d8SChris Lattner } 213e7941795SCaroline Tice 214e7941795SCaroline Tice if (is_alias_command) 215e7941795SCaroline Tice { 216e7941795SCaroline Tice StreamString sstr; 217e7941795SCaroline Tice m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr); 218e7941795SCaroline Tice result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData()); 219e7941795SCaroline Tice } 22030fdc8d8SChris Lattner } 221279a6c26SJim Ingham else if (matches.GetSize() > 0) 222279a6c26SJim Ingham { 223279a6c26SJim Ingham Stream &output_strm = result.GetOutputStream(); 224279a6c26SJim Ingham output_strm.Printf("Help requested with ambiguous command name, possible completions:\n"); 225c982c768SGreg Clayton const uint32_t match_count = matches.GetSize(); 226c982c768SGreg Clayton for (uint32_t i = 0; i < match_count; i++) 227279a6c26SJim Ingham { 228279a6c26SJim Ingham output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); 229279a6c26SJim Ingham } 230279a6c26SJim Ingham } 23130fdc8d8SChris Lattner else 23230fdc8d8SChris Lattner { 233e139cf23SCaroline Tice // Maybe the user is asking for help about a command argument rather than a command. 234e139cf23SCaroline Tice const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0)); 235e139cf23SCaroline Tice if (arg_type != eArgTypeLastArg) 236e139cf23SCaroline Tice { 237e139cf23SCaroline Tice Stream &output_strm = result.GetOutputStream (); 238e139cf23SCaroline Tice CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter); 239e139cf23SCaroline Tice result.SetStatus (eReturnStatusSuccessFinishNoResult); 240e139cf23SCaroline Tice } 241e139cf23SCaroline Tice else 242e139cf23SCaroline Tice { 24330fdc8d8SChris Lattner result.AppendErrorWithFormat 24430fdc8d8SChris Lattner ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", 24530fdc8d8SChris Lattner command.GetArgumentAtIndex(0)); 24630fdc8d8SChris Lattner result.SetStatus (eReturnStatusFailed); 24730fdc8d8SChris Lattner } 24830fdc8d8SChris Lattner } 249e139cf23SCaroline Tice } 25030fdc8d8SChris Lattner 25130fdc8d8SChris Lattner return result.Succeeded(); 25230fdc8d8SChris Lattner } 25330fdc8d8SChris Lattner 25430fdc8d8SChris Lattner int 25530fdc8d8SChris Lattner CommandObjectHelp::HandleCompletion 25630fdc8d8SChris Lattner ( 25730fdc8d8SChris Lattner Args &input, 25830fdc8d8SChris Lattner int &cursor_index, 25930fdc8d8SChris Lattner int &cursor_char_position, 26030fdc8d8SChris Lattner int match_start_point, 26130fdc8d8SChris Lattner int max_return_elements, 262558ce124SJim Ingham bool &word_complete, 26330fdc8d8SChris Lattner StringList &matches 26430fdc8d8SChris Lattner ) 26530fdc8d8SChris Lattner { 26630fdc8d8SChris Lattner // Return the completions of the commands in the help system: 26730fdc8d8SChris Lattner if (cursor_index == 0) 26830fdc8d8SChris Lattner { 269a7015092SGreg Clayton return m_interpreter.HandleCompletionMatches (input, 270a7015092SGreg Clayton cursor_index, 271a7015092SGreg Clayton cursor_char_position, 272a7015092SGreg Clayton match_start_point, 273a7015092SGreg Clayton max_return_elements, 274a7015092SGreg Clayton word_complete, 275a7015092SGreg Clayton matches); 27630fdc8d8SChris Lattner } 27730fdc8d8SChris Lattner else 27830fdc8d8SChris Lattner { 279a7015092SGreg Clayton CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0)); 2801d18ebf6SJim Ingham 2811d18ebf6SJim Ingham // The command that they are getting help on might be ambiguous, in which case we should complete that, 2821d18ebf6SJim Ingham // otherwise complete with the command the user is getting help on... 2831d18ebf6SJim Ingham 2841d18ebf6SJim Ingham if (cmd_obj) 2851d18ebf6SJim Ingham { 28630fdc8d8SChris Lattner input.Shift(); 28730fdc8d8SChris Lattner cursor_index--; 288a7015092SGreg Clayton return cmd_obj->HandleCompletion (input, 289a7015092SGreg Clayton cursor_index, 290a7015092SGreg Clayton cursor_char_position, 291a7015092SGreg Clayton match_start_point, 292a7015092SGreg Clayton max_return_elements, 293a7015092SGreg Clayton word_complete, 294a7015092SGreg Clayton matches); 29530fdc8d8SChris Lattner } 2961d18ebf6SJim Ingham else 2971d18ebf6SJim Ingham { 2981d18ebf6SJim Ingham return m_interpreter.HandleCompletionMatches (input, 2991d18ebf6SJim Ingham cursor_index, 3001d18ebf6SJim Ingham cursor_char_position, 3011d18ebf6SJim Ingham match_start_point, 3021d18ebf6SJim Ingham max_return_elements, 3031d18ebf6SJim Ingham word_complete, 3041d18ebf6SJim Ingham matches); 3051d18ebf6SJim Ingham } 3061d18ebf6SJim Ingham } 30730fdc8d8SChris Lattner } 308