2 Main file for Help shell level 3 function.
4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved. <BR>
5 Copyright (c) 2014, ARM Limited. All rights reserved. <BR>
6 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "UefiShellLevel3CommandsLib.h"
20 #include <Library/ShellLib.h>
21 #include <Library/HandleParsingLib.h>
23 #include <Protocol/EfiShellDynamicCommand.h>
26 Attempt to print help from a dynamically added command.
28 @param[in] CommandToGetHelpOn The unicode name of the command that help is
30 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
31 @param[in] PrintCommandText Print the command followed by the help content
34 @retval EFI_SUCCESS The help was displayed
35 @retval EFI_NOT_FOUND The command name could not be found
36 @retval EFI_DEVICE_ERROR The help data format was incorrect.
40 PrintDynamicCommandHelp(
41 IN CHAR16
*CommandToGetHelpOn
,
42 IN CHAR16
*SectionToGetHelpOn
,
43 IN BOOLEAN PrintCommandText
48 EFI_HANDLE
*CommandHandleList
;
49 EFI_HANDLE
*NextCommand
;
50 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*DynamicCommand
;
52 Status
= EFI_NOT_FOUND
;
54 CommandHandleList
= NULL
;
56 CommandHandleList
= GetHandleListByProtocol(&gEfiShellDynamicCommandProtocolGuid
);
58 if (CommandHandleList
== NULL
) {
60 // not found or out of resources
65 for (NextCommand
= CommandHandleList
; *NextCommand
!= NULL
; NextCommand
++) {
66 Status
= gBS
->HandleProtocol(
68 &gEfiShellDynamicCommandProtocolGuid
,
69 (VOID
**)&DynamicCommand
72 if (EFI_ERROR(Status
)) {
77 // Check execution break flag when printing multiple command help information.
79 if (ShellGetExecutionBreakFlag ()) {
83 if ((gUnicodeCollation
->MetaiMatch (gUnicodeCollation
, (CHAR16
*)DynamicCommand
->CommandName
, CommandToGetHelpOn
)) ||
84 (gEfiShellProtocol
->GetAlias (CommandToGetHelpOn
, NULL
) != NULL
&& (gUnicodeCollation
->MetaiMatch (gUnicodeCollation
, (CHAR16
*)DynamicCommand
->CommandName
, (CHAR16
*)(gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
)))))) {
85 // Print as Shell Help if in ManPage format.
86 Status
= ShellPrintHelp (DynamicCommand
->CommandName
, SectionToGetHelpOn
,
88 if (Status
== EFI_DEVICE_ERROR
) {
89 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_HELP_INV
),
90 gShellLevel3HiiHandle
, DynamicCommand
->CommandName
);
91 } else if (EFI_ERROR(Status
)) {
92 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_HELP_NF
),
93 gShellLevel3HiiHandle
, DynamicCommand
->CommandName
);
100 return (Found
? EFI_SUCCESS
: Status
);
104 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
105 {L
"-usage", TypeFlag
},
106 {L
"-section", TypeMaxValue
},
107 {L
"-verbose", TypeFlag
},
113 Function for 'help' command.
115 @param[in] ImageHandle Handle to the Image (NULL if Internal).
116 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
120 ShellCommandRunHelp (
121 IN EFI_HANDLE ImageHandle
,
122 IN EFI_SYSTEM_TABLE
*SystemTable
127 CHAR16
*ProblemParam
;
128 SHELL_STATUS ShellStatus
;
129 CONST COMMAND_LIST
*CommandList
;
130 CONST COMMAND_LIST
*Node
;
131 CHAR16
*CommandToGetHelpOn
;
132 CHAR16
*SectionToGetHelpOn
;
135 BOOLEAN PrintCommandText
;
137 PrintCommandText
= TRUE
;
139 ShellStatus
= SHELL_SUCCESS
;
140 CommandToGetHelpOn
= NULL
;
141 SectionToGetHelpOn
= NULL
;
145 // initialize the shell lib (we must be in non-auto-init...)
147 Status
= ShellInitialize();
148 ASSERT_EFI_ERROR(Status
);
150 Status
= CommandInit();
151 ASSERT_EFI_ERROR(Status
);
154 // parse the command line
156 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
157 if (EFI_ERROR(Status
)) {
158 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
159 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, L
"help", ProblemParam
);
160 FreePool(ProblemParam
);
161 ShellStatus
= SHELL_INVALID_PARAMETER
;
167 // Check for conflicting parameters.
169 if (ShellCommandLineGetFlag(Package
, L
"-usage")
170 &&ShellCommandLineGetFlag(Package
, L
"-section")
171 &&(ShellCommandLineGetFlag(Package
, L
"-verbose") || ShellCommandLineGetFlag(Package
, L
"-v"))
173 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_CON
), gShellLevel3HiiHandle
, L
"help");
174 ShellStatus
= SHELL_INVALID_PARAMETER
;
175 } else if (ShellCommandLineGetRawValue(Package
, 2) != NULL
) {
176 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel3HiiHandle
, L
"help");
177 ShellStatus
= SHELL_INVALID_PARAMETER
;
180 // Get the command name we are getting help on
182 ASSERT(CommandToGetHelpOn
== NULL
);
183 StrnCatGrow(&CommandToGetHelpOn
, NULL
, ShellCommandLineGetRawValue(Package
, 1), 0);
184 if (CommandToGetHelpOn
== NULL
&& ShellCommandLineGetFlag(Package
, L
"-?")) {
186 // If we dont have a command and we got a simple -?
187 // we are looking for help on help command.
189 StrnCatGrow(&CommandToGetHelpOn
, NULL
, L
"help", 0);
192 if (CommandToGetHelpOn
== NULL
) {
193 StrnCatGrow(&CommandToGetHelpOn
, NULL
, L
"*", 0);
194 ASSERT(SectionToGetHelpOn
== NULL
);
195 StrnCatGrow(&SectionToGetHelpOn
, NULL
, L
"NAME", 0);
197 PrintCommandText
= FALSE
;
198 ASSERT(SectionToGetHelpOn
== NULL
);
200 // Get the section name for the given command name
202 if (ShellCommandLineGetFlag(Package
, L
"-section")) {
203 StrnCatGrow(&SectionToGetHelpOn
, NULL
, ShellCommandLineGetValue(Package
, L
"-section"), 0);
204 } else if (ShellCommandLineGetFlag(Package
, L
"-usage")) {
205 StrnCatGrow(&SectionToGetHelpOn
, NULL
, L
"NAME,SYNOPSIS", 0);
206 } else if (ShellCommandLineGetFlag(Package
, L
"-verbose") || ShellCommandLineGetFlag(Package
, L
"-v")) {
209 // The output of help <command> will display NAME, SYNOPSIS, OPTIONS, DESCRIPTION, and EXAMPLES sections.
211 StrnCatGrow (&SectionToGetHelpOn
, NULL
, L
"NAME,SYNOPSIS,OPTIONS,DESCRIPTION,EXAMPLES", 0);
215 if (gUnicodeCollation
->StriColl(gUnicodeCollation
, CommandToGetHelpOn
, L
"special") == 0) {
217 // we need info on the special characters
219 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_SC_HEADER
), gShellLevel3HiiHandle
);
220 HiiString
= HiiGetString(gShellLevel3HiiHandle
, STRING_TOKEN(STR_HELP_SC_DATA
), NULL
);
221 ShellPrintEx(-1, -1, L
"%s", HiiString
);
225 CommandList
= ShellCommandGetCommandList(TRUE
);
226 ASSERT(CommandList
!= NULL
);
227 for ( Node
= (COMMAND_LIST
*)GetFirstNode(&CommandList
->Link
)
228 ; CommandList
!= NULL
&& !IsListEmpty(&CommandList
->Link
) && !IsNull(&CommandList
->Link
, &Node
->Link
)
229 ; Node
= (COMMAND_LIST
*)GetNextNode(&CommandList
->Link
, &Node
->Link
)
232 // Checking execution break flag when print multiple command help information.
234 if (ShellGetExecutionBreakFlag ()) {
237 if ((gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Node
->CommandString
, CommandToGetHelpOn
)) ||
238 (gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
) != NULL
&& (gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Node
->CommandString
, (CHAR16
*)(gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
)))))) {
240 // We have a command to look for help on.
242 Status
= ShellPrintHelp(Node
->CommandString
, SectionToGetHelpOn
, PrintCommandText
);
243 if (Status
== EFI_DEVICE_ERROR
) {
244 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_INV
), gShellLevel3HiiHandle
, Node
->CommandString
);
245 } else if (EFI_ERROR(Status
)) {
246 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_NF
), gShellLevel3HiiHandle
, Node
->CommandString
);
254 // now try to match against the dynamic command list and print help
256 Status
= PrintDynamicCommandHelp (CommandToGetHelpOn
, SectionToGetHelpOn
,
258 if (!EFI_ERROR(Status
)) {
263 // Search the .man file for Shell applications (Shell external commands).
266 Status
= ShellPrintHelp(CommandToGetHelpOn
, SectionToGetHelpOn
, FALSE
);
267 if (Status
== EFI_DEVICE_ERROR
) {
268 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_INV
), gShellLevel3HiiHandle
, CommandToGetHelpOn
);
269 } else if (EFI_ERROR(Status
)) {
270 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_NF
), gShellLevel3HiiHandle
, CommandToGetHelpOn
);
278 ShellStatus
= SHELL_NOT_FOUND
;
282 // free the command line package
284 ShellCommandLineFreeVarList (Package
);
288 if (CommandToGetHelpOn
!= NULL
&& StrCmp(CommandToGetHelpOn
, L
"*") == 0){
290 // If '*' then the command entered was 'Help' without qualifiers, This footer
291 // provides additional info on help switches
293 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_FOOTER
), gShellLevel3HiiHandle
);
295 if (CommandToGetHelpOn
!= NULL
) {
296 FreePool(CommandToGetHelpOn
);
298 if (SectionToGetHelpOn
!= NULL
) {
299 FreePool(SectionToGetHelpOn
);
302 return (ShellStatus
);