2 Main file for Help shell level 3 function.
4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved. <BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellLevel3CommandsLib.h"
17 #include <Library/ShellLib.h>
18 #include <Library/HandleParsingLib.h>
20 #include <Protocol/EfiShellDynamicCommand.h>
23 Attempt to print help from a dynamically added command.
25 @param[in] CommandToGetHelpOn The unicode name of the command that help is requested on.
27 @retval EFI_SUCCESS The help was displayed
28 @retval FI_NOT_FOUND The command name could not be found
32 PrintDynamicCommandHelp(
33 IN CHAR16
*CommandToGetHelpOn
38 EFI_HANDLE
*CommandHandleList
;
39 EFI_HANDLE
*NextCommand
;
40 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*DynamicCommand
;
44 CommandHandleList
= NULL
;
47 CommandHandleList
= GetHandleListByProtocol(&gEfiShellDynamicCommandProtocolGuid
);
49 if (CommandHandleList
== NULL
) {
51 // not found or out of resources
56 for (NextCommand
= CommandHandleList
; *NextCommand
!= NULL
; NextCommand
++) {
57 Status
= gBS
->HandleProtocol(
59 &gEfiShellDynamicCommandProtocolGuid
,
60 (VOID
**)&DynamicCommand
63 if (EFI_ERROR(Status
)) {
67 if ((gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, (CHAR16
*)DynamicCommand
->CommandName
, CommandToGetHelpOn
)) ||
68 (gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
) != NULL
&& (gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, (CHAR16
*)DynamicCommand
->CommandName
, (CHAR16
*)(gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
)))))) {
70 // TODO: how to get proper language?
72 OutText
= DynamicCommand
->GetHelp(DynamicCommand
, "en");
74 if (OutText
== NULL
) {
79 // Trim extra characters from the end the the string before printing
81 while (StrLen(OutText
) > 0
82 && (OutText
[StrLen(OutText
) - 1] == L
'\r' || OutText
[StrLen(OutText
) - 1] == L
'\n' || OutText
[StrLen(OutText
) - 1] == L
' ')) {
83 OutText
[StrLen(OutText
) - 1] = CHAR_NULL
;
87 // Make sure we have something to print still.
89 if (StrLen(OutText
) == 0) {
98 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_HELP_COMMAND
), gShellLevel3HiiHandle
, DynamicCommand
->CommandName
, OutText
);
107 FreePool(CommandHandleList
);
109 return Found
? EFI_SUCCESS
: EFI_NOT_FOUND
;
112 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
113 {L
"-usage", TypeFlag
},
114 {L
"-section", TypeMaxValue
},
115 {L
"-verbose", TypeFlag
},
121 Function for 'help' command.
123 @param[in] ImageHandle Handle to the Image (NULL if Internal).
124 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
128 ShellCommandRunHelp (
129 IN EFI_HANDLE ImageHandle
,
130 IN EFI_SYSTEM_TABLE
*SystemTable
135 CHAR16
*ProblemParam
;
136 SHELL_STATUS ShellStatus
;
137 CONST COMMAND_LIST
*CommandList
;
138 CONST COMMAND_LIST
*Node
;
139 CHAR16
*CommandToGetHelpOn
;
140 CHAR16
*SectionToGetHelpOn
;
143 BOOLEAN PrintCommandText
;
145 PrintCommandText
= TRUE
;
147 ShellStatus
= SHELL_SUCCESS
;
148 CommandToGetHelpOn
= NULL
;
149 SectionToGetHelpOn
= NULL
;
153 // initialize the shell lib (we must be in non-auto-init...)
155 Status
= ShellInitialize();
156 ASSERT_EFI_ERROR(Status
);
158 Status
= CommandInit();
159 ASSERT_EFI_ERROR(Status
);
162 // parse the command line
164 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
165 if (EFI_ERROR(Status
)) {
166 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
167 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, ProblemParam
);
168 FreePool(ProblemParam
);
169 ShellStatus
= SHELL_INVALID_PARAMETER
;
175 // Check for conflicting parameters.
177 if (ShellCommandLineGetFlag(Package
, L
"-usage")
178 &&ShellCommandLineGetFlag(Package
, L
"-section")
179 &&(ShellCommandLineGetFlag(Package
, L
"-verbose") || ShellCommandLineGetFlag(Package
, L
"-v"))
181 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_CON
), gShellLevel3HiiHandle
);
182 ShellStatus
= SHELL_INVALID_PARAMETER
;
183 } else if (ShellCommandLineGetRawValue(Package
, 2) != NULL
) {
184 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel3HiiHandle
);
185 ShellStatus
= SHELL_INVALID_PARAMETER
;
188 // Get the command name we are getting help on
190 ASSERT(CommandToGetHelpOn
== NULL
);
191 StrnCatGrow(&CommandToGetHelpOn
, NULL
, ShellCommandLineGetRawValue(Package
, 1), 0);
192 if (CommandToGetHelpOn
== NULL
&& ShellCommandLineGetFlag(Package
, L
"-?")) {
194 // If we dont have a command and we got a simple -?
195 // we are looking for help on help command.
197 StrnCatGrow(&CommandToGetHelpOn
, NULL
, L
"help", 0);
200 if (CommandToGetHelpOn
== NULL
) {
201 StrnCatGrow(&CommandToGetHelpOn
, NULL
, L
"*", 0);
202 ASSERT(SectionToGetHelpOn
== NULL
);
203 StrnCatGrow(&SectionToGetHelpOn
, NULL
, L
"NAME", 0);
205 PrintCommandText
= FALSE
;
206 ASSERT(SectionToGetHelpOn
== NULL
);
208 // Get the section name for the given command name
210 if (ShellCommandLineGetFlag(Package
, L
"-section")) {
211 StrnCatGrow(&SectionToGetHelpOn
, NULL
, ShellCommandLineGetValue(Package
, L
"-section"), 0);
212 } else if (ShellCommandLineGetFlag(Package
, L
"-usage")) {
213 StrnCatGrow(&SectionToGetHelpOn
, NULL
, L
"NAME,SYNOPSIS", 0);
214 } else if (ShellCommandLineGetFlag(Package
, L
"-verbose") || ShellCommandLineGetFlag(Package
, L
"-v")) {
217 // The output of help <command> will display NAME, SYNOPSIS, OPTIONS, DESCRIPTION, and EXAMPLES sections.
219 StrnCatGrow (&SectionToGetHelpOn
, NULL
, L
"NAME,SYNOPSIS,OPTIONS,DESCRIPTION,EXAMPLES", 0);
223 if (gUnicodeCollation
->StriColl(gUnicodeCollation
, CommandToGetHelpOn
, L
"special") == 0) {
225 // we need info on the special characters
227 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_SC_HEADER
), gShellLevel3HiiHandle
);
228 HiiString
= HiiGetString(gShellLevel3HiiHandle
, STRING_TOKEN(STR_HELP_SC_DATA
), NULL
);
229 ShellPrintEx(-1, -1, L
"%s", HiiString
);
233 CommandList
= ShellCommandGetCommandList(TRUE
);
234 ASSERT(CommandList
!= NULL
);
235 for ( Node
= (COMMAND_LIST
*)GetFirstNode(&CommandList
->Link
)
236 ; CommandList
!= NULL
&& !IsListEmpty(&CommandList
->Link
) && !IsNull(&CommandList
->Link
, &Node
->Link
)
237 ; Node
= (COMMAND_LIST
*)GetNextNode(&CommandList
->Link
, &Node
->Link
)
240 // Checking execution break flag when print multiple command help information.
242 if (ShellGetExecutionBreakFlag ()) {
245 if ((gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Node
->CommandString
, CommandToGetHelpOn
)) ||
246 (gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
) != NULL
&& (gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Node
->CommandString
, (CHAR16
*)(gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
)))))) {
248 // We have a command to look for help on.
250 Status
= ShellPrintHelp(Node
->CommandString
, SectionToGetHelpOn
, PrintCommandText
);
251 if (Status
== EFI_DEVICE_ERROR
) {
252 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_INV
), gShellLevel3HiiHandle
, Node
->CommandString
);
253 } else if (EFI_ERROR(Status
)) {
254 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_NF
), gShellLevel3HiiHandle
, Node
->CommandString
);
261 // Search the .man file for Shell applications (Shell external commands).
264 Status
= ShellPrintHelp(CommandToGetHelpOn
, SectionToGetHelpOn
, FALSE
);
265 if (Status
== EFI_DEVICE_ERROR
) {
266 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_INV
), gShellLevel3HiiHandle
, CommandToGetHelpOn
);
267 } else if (EFI_ERROR(Status
)) {
268 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_NF
), gShellLevel3HiiHandle
, CommandToGetHelpOn
);
275 // now try to match against the dynamic command list and print help
277 Status
= PrintDynamicCommandHelp(CommandToGetHelpOn
);
278 if (Status
== EFI_SUCCESS
) {
285 ShellStatus
= SHELL_NOT_FOUND
;
289 // free the command line package
291 ShellCommandLineFreeVarList (Package
);
295 if (CommandToGetHelpOn
!= NULL
&& StrCmp(CommandToGetHelpOn
, L
"*") == 0){
297 // If '*' then the command entered was 'Help' without qualifiers, This footer
298 // provides additional info on help switches
300 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_FOOTER
), gShellLevel3HiiHandle
);
302 if (CommandToGetHelpOn
!= NULL
) {
303 FreePool(CommandToGetHelpOn
);
305 if (SectionToGetHelpOn
!= NULL
) {
306 FreePool(SectionToGetHelpOn
);
309 return (ShellStatus
);