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
33 PrintDynamicCommandHelp(
34 IN CHAR16
*CommandToGetHelpOn
39 EFI_HANDLE
*CommandHandleList
;
40 EFI_HANDLE
*NextCommand
;
41 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*DynamicCommand
;
45 CommandHandleList
= NULL
;
48 CommandHandleList
= GetHandleListByProtocol(&gEfiShellDynamicCommandProtocolGuid
);
50 if (CommandHandleList
== NULL
) {
52 // not found or out of resources
57 for (NextCommand
= CommandHandleList
; *NextCommand
!= NULL
; NextCommand
++) {
58 Status
= gBS
->HandleProtocol(
60 &gEfiShellDynamicCommandProtocolGuid
,
61 (VOID
**)&DynamicCommand
64 if (EFI_ERROR(Status
)) {
68 if ((gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, (CHAR16
*)DynamicCommand
->CommandName
, CommandToGetHelpOn
)) ||
69 (gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
) != NULL
&& (gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, (CHAR16
*)DynamicCommand
->CommandName
, (CHAR16
*)(gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
)))))) {
71 // TODO: how to get proper language?
73 OutText
= DynamicCommand
->GetHelp(DynamicCommand
, "en");
75 if (OutText
== NULL
) {
80 // Trim extra characters from the end the the string before printing
82 while (StrLen(OutText
) > 0
83 && (OutText
[StrLen(OutText
) - 1] == L
'\r' || OutText
[StrLen(OutText
) - 1] == L
'\n' || OutText
[StrLen(OutText
) - 1] == L
' ')) {
84 OutText
[StrLen(OutText
) - 1] = CHAR_NULL
;
88 // Make sure we have something to print still.
90 if (StrLen(OutText
) == 0) {
99 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_HELP_COMMAND
), gShellLevel3HiiHandle
, DynamicCommand
->CommandName
, OutText
);
108 FreePool(CommandHandleList
);
110 return Found
? EFI_SUCCESS
: EFI_NOT_FOUND
;
113 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
114 {L
"-usage", TypeFlag
},
115 {L
"-section", TypeMaxValue
},
116 {L
"-verbose", TypeFlag
},
122 Function for 'help' command.
124 @param[in] ImageHandle Handle to the Image (NULL if Internal).
125 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
129 ShellCommandRunHelp (
130 IN EFI_HANDLE ImageHandle
,
131 IN EFI_SYSTEM_TABLE
*SystemTable
136 CHAR16
*ProblemParam
;
137 SHELL_STATUS ShellStatus
;
138 CONST COMMAND_LIST
*CommandList
;
139 CONST COMMAND_LIST
*Node
;
140 CHAR16
*CommandToGetHelpOn
;
141 CHAR16
*SectionToGetHelpOn
;
144 BOOLEAN PrintCommandText
;
146 PrintCommandText
= TRUE
;
148 ShellStatus
= SHELL_SUCCESS
;
149 CommandToGetHelpOn
= NULL
;
150 SectionToGetHelpOn
= NULL
;
154 // initialize the shell lib (we must be in non-auto-init...)
156 Status
= ShellInitialize();
157 ASSERT_EFI_ERROR(Status
);
159 Status
= CommandInit();
160 ASSERT_EFI_ERROR(Status
);
163 // parse the command line
165 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
166 if (EFI_ERROR(Status
)) {
167 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
168 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, ProblemParam
);
169 FreePool(ProblemParam
);
170 ShellStatus
= SHELL_INVALID_PARAMETER
;
176 // Check for conflicting parameters.
178 if (ShellCommandLineGetFlag(Package
, L
"-usage")
179 &&ShellCommandLineGetFlag(Package
, L
"-section")
180 &&(ShellCommandLineGetFlag(Package
, L
"-verbose") || ShellCommandLineGetFlag(Package
, L
"-v"))
182 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_CON
), gShellLevel3HiiHandle
);
183 ShellStatus
= SHELL_INVALID_PARAMETER
;
184 } else if (ShellCommandLineGetRawValue(Package
, 2) != NULL
) {
185 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel3HiiHandle
);
186 ShellStatus
= SHELL_INVALID_PARAMETER
;
189 // Get the command name we are getting help on
191 ASSERT(CommandToGetHelpOn
== NULL
);
192 StrnCatGrow(&CommandToGetHelpOn
, NULL
, ShellCommandLineGetRawValue(Package
, 1), 0);
193 if (CommandToGetHelpOn
== NULL
&& ShellCommandLineGetFlag(Package
, L
"-?")) {
195 // If we dont have a command and we got a simple -?
196 // we are looking for help on help command.
198 StrnCatGrow(&CommandToGetHelpOn
, NULL
, L
"help", 0);
201 if (CommandToGetHelpOn
== NULL
) {
202 StrnCatGrow(&CommandToGetHelpOn
, NULL
, L
"*", 0);
203 ASSERT(SectionToGetHelpOn
== NULL
);
204 StrnCatGrow(&SectionToGetHelpOn
, NULL
, L
"NAME", 0);
206 PrintCommandText
= FALSE
;
207 ASSERT(SectionToGetHelpOn
== NULL
);
209 // Get the section name for the given command name
211 if (ShellCommandLineGetFlag(Package
, L
"-section")) {
212 StrnCatGrow(&SectionToGetHelpOn
, NULL
, ShellCommandLineGetValue(Package
, L
"-section"), 0);
213 } else if (ShellCommandLineGetFlag(Package
, L
"-usage")) {
214 StrnCatGrow(&SectionToGetHelpOn
, NULL
, L
"NAME,SYNOPSIS", 0);
215 } else if (ShellCommandLineGetFlag(Package
, L
"-verbose") || ShellCommandLineGetFlag(Package
, L
"-v")) {
218 // The output of help <command> will display NAME, SYNOPSIS, OPTIONS, DESCRIPTION, and EXAMPLES sections.
220 StrnCatGrow (&SectionToGetHelpOn
, NULL
, L
"NAME,SYNOPSIS,OPTIONS,DESCRIPTION,EXAMPLES", 0);
224 if (gUnicodeCollation
->StriColl(gUnicodeCollation
, CommandToGetHelpOn
, L
"special") == 0) {
226 // we need info on the special characters
228 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_SC_HEADER
), gShellLevel3HiiHandle
);
229 HiiString
= HiiGetString(gShellLevel3HiiHandle
, STRING_TOKEN(STR_HELP_SC_DATA
), NULL
);
230 ShellPrintEx(-1, -1, L
"%s", HiiString
);
234 CommandList
= ShellCommandGetCommandList(TRUE
);
235 ASSERT(CommandList
!= NULL
);
236 for ( Node
= (COMMAND_LIST
*)GetFirstNode(&CommandList
->Link
)
237 ; CommandList
!= NULL
&& !IsListEmpty(&CommandList
->Link
) && !IsNull(&CommandList
->Link
, &Node
->Link
)
238 ; Node
= (COMMAND_LIST
*)GetNextNode(&CommandList
->Link
, &Node
->Link
)
241 // Checking execution break flag when print multiple command help information.
243 if (ShellGetExecutionBreakFlag ()) {
246 if ((gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Node
->CommandString
, CommandToGetHelpOn
)) ||
247 (gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
) != NULL
&& (gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Node
->CommandString
, (CHAR16
*)(gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
)))))) {
249 // We have a command to look for help on.
251 Status
= ShellPrintHelp(Node
->CommandString
, SectionToGetHelpOn
, PrintCommandText
);
252 if (Status
== EFI_DEVICE_ERROR
) {
253 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_INV
), gShellLevel3HiiHandle
, Node
->CommandString
);
254 } else if (EFI_ERROR(Status
)) {
255 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_NF
), gShellLevel3HiiHandle
, Node
->CommandString
);
262 // Search the .man file for Shell applications (Shell external commands).
265 Status
= ShellPrintHelp(CommandToGetHelpOn
, SectionToGetHelpOn
, FALSE
);
266 if (Status
== EFI_DEVICE_ERROR
) {
267 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_INV
), gShellLevel3HiiHandle
, CommandToGetHelpOn
);
268 } else if (EFI_ERROR(Status
)) {
269 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_NF
), gShellLevel3HiiHandle
, CommandToGetHelpOn
);
276 // now try to match against the dynamic command list and print help
278 Status
= PrintDynamicCommandHelp(CommandToGetHelpOn
);
279 if (Status
== EFI_SUCCESS
) {
286 ShellStatus
= SHELL_NOT_FOUND
;
290 // free the command line package
292 ShellCommandLineFreeVarList (Package
);
296 if (CommandToGetHelpOn
!= NULL
&& StrCmp(CommandToGetHelpOn
, L
"*") == 0){
298 // If '*' then the command entered was 'Help' without qualifiers, This footer
299 // provides additional info on help switches
301 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_FOOTER
), gShellLevel3HiiHandle
);
303 if (CommandToGetHelpOn
!= NULL
) {
304 FreePool(CommandToGetHelpOn
);
306 if (SectionToGetHelpOn
!= NULL
) {
307 FreePool(SectionToGetHelpOn
);
310 return (ShellStatus
);