2 Main file for Help shell level 3 function.
4 Copyright (c) 2009 - 2011, 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>
19 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
20 {L
"-usage", TypeFlag
},
21 {L
"-section", TypeMaxValue
},
22 {L
"-verbose", TypeFlag
},
28 Function for 'help' command.
30 @param[in] ImageHandle Handle to the Image (NULL if Internal).
31 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
36 IN EFI_HANDLE ImageHandle
,
37 IN EFI_SYSTEM_TABLE
*SystemTable
43 SHELL_STATUS ShellStatus
;
45 CONST COMMAND_LIST
*CommandList
;
46 CONST COMMAND_LIST
*Node
;
47 CHAR16
*CommandToGetHelpOn
;
48 CHAR16
*SectionToGetHelpOn
;
55 ShellStatus
= SHELL_SUCCESS
;
57 CommandToGetHelpOn
= NULL
;
58 SectionToGetHelpOn
= NULL
;
62 // initialize the shell lib (we must be in non-auto-init...)
64 Status
= ShellInitialize();
65 ASSERT_EFI_ERROR(Status
);
67 Status
= CommandInit();
68 ASSERT_EFI_ERROR(Status
);
71 // parse the command line
73 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
74 if (EFI_ERROR(Status
)) {
75 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
76 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, ProblemParam
);
77 FreePool(ProblemParam
);
78 ShellStatus
= SHELL_INVALID_PARAMETER
;
84 // Check for conflicting parameters.
86 if (ShellCommandLineGetFlag(Package
, L
"-usage")
87 &&ShellCommandLineGetFlag(Package
, L
"-section")
88 &&(ShellCommandLineGetFlag(Package
, L
"-verbose") || ShellCommandLineGetFlag(Package
, L
"-v"))
90 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_CON
), gShellLevel3HiiHandle
);
91 ShellStatus
= SHELL_INVALID_PARAMETER
;
92 } else if (ShellCommandLineGetRawValue(Package
, 2) != NULL
) {
93 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel3HiiHandle
);
94 ShellStatus
= SHELL_INVALID_PARAMETER
;
97 // Get the command name we are getting help on
99 ASSERT(CommandToGetHelpOn
== NULL
);
100 StrnCatGrow(&CommandToGetHelpOn
, NULL
, ShellCommandLineGetRawValue(Package
, 1), 0);
101 if (CommandToGetHelpOn
== NULL
&& ShellCommandLineGetFlag(Package
, L
"-?")) {
103 // If we dont have a command and we got a simple -?
104 // we are looking for help on help command.
106 StrnCatGrow(&CommandToGetHelpOn
, NULL
, L
"help", 0);
109 if (CommandToGetHelpOn
== NULL
) {
110 StrnCatGrow(&CommandToGetHelpOn
, NULL
, L
"*", 0);
111 ASSERT(SectionToGetHelpOn
== NULL
);
112 StrnCatGrow(&SectionToGetHelpOn
, NULL
, L
"NAME", 0);
115 ASSERT(SectionToGetHelpOn
== NULL
);
117 // Get the section name for the given command name
119 if (ShellCommandLineGetFlag(Package
, L
"-section")) {
120 StrnCatGrow(&SectionToGetHelpOn
, NULL
, ShellCommandLineGetValue(Package
, L
"-section"), 0);
121 } else if (ShellCommandLineGetFlag(Package
, L
"-usage")) {
122 StrnCatGrow(&SectionToGetHelpOn
, NULL
, L
"NAME,SYNOPSIS", 0);
123 } else if (ShellCommandLineGetFlag(Package
, L
"-verbose") || ShellCommandLineGetFlag(Package
, L
"-v")) {
126 // The output of help <command> will display NAME, SYNOPSIS, OPTIONS, DESCRIPTION, and EXAMPLES sections.
128 StrnCatGrow (&SectionToGetHelpOn
, NULL
, L
"NAME,SYNOPSIS,OPTIONS,DESCRIPTION,EXAMPLES", 0);
132 if (gUnicodeCollation
->StriColl(gUnicodeCollation
, CommandToGetHelpOn
, L
"special") == 0) {
134 // we need info on the special characters
136 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_SC_HEADER
), gShellLevel3HiiHandle
);
137 HiiString
= HiiGetString(gShellLevel3HiiHandle
, STRING_TOKEN(STR_HELP_SC_DATA
), NULL
);
138 ShellPrintEx(-1, -1, L
"%s", HiiString
);
142 CommandList
= ShellCommandGetCommandList(TRUE
);
143 ASSERT(CommandList
!= NULL
);
144 for ( Node
= (COMMAND_LIST
*)GetFirstNode(&CommandList
->Link
)
145 ; CommandList
!= NULL
&& !IsListEmpty(&CommandList
->Link
) && !IsNull(&CommandList
->Link
, &Node
->Link
)
146 ; Node
= (COMMAND_LIST
*)GetNextNode(&CommandList
->Link
, &Node
->Link
)
149 // Checking execution break flag when print multiple command help information.
151 if (ShellGetExecutionBreakFlag ()) {
154 if ((gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Node
->CommandString
, CommandToGetHelpOn
)) ||
155 (gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
) != NULL
&& (gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Node
->CommandString
, (CHAR16
*)(gEfiShellProtocol
->GetAlias(CommandToGetHelpOn
, NULL
)))))) {
157 // We have a command to look for help on.
159 Status
= gEfiShellProtocol
->GetHelpText(Node
->CommandString
, SectionToGetHelpOn
, &OutText
);
160 if (EFI_ERROR(Status
) || OutText
== NULL
) {
161 if (Status
== EFI_DEVICE_ERROR
) {
162 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_INV
), gShellLevel3HiiHandle
, Node
->CommandString
);
164 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_NF
), gShellLevel3HiiHandle
, Node
->CommandString
);
166 ShellStatus
= SHELL_NOT_FOUND
;
169 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_PAGE_COMMAND
), gShellLevel3HiiHandle
, OutText
);
171 while (OutText
[StrLen(OutText
)-1] == L
'\r' || OutText
[StrLen(OutText
)-1] == L
'\n' || OutText
[StrLen(OutText
)-1] == L
' ') {
172 OutText
[StrLen(OutText
)-1] = CHAR_NULL
;
174 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_COMMAND
), gShellLevel3HiiHandle
, Node
->CommandString
, OutText
);
183 // Search the .man file for Shell applications (Shell external commands).
186 Status
= gEfiShellProtocol
->GetHelpText (CommandToGetHelpOn
, SectionToGetHelpOn
, &OutText
);
187 if (EFI_ERROR(Status
) || OutText
== NULL
) {
188 if (Status
== EFI_DEVICE_ERROR
) {
189 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_HELP_INV
), gShellLevel3HiiHandle
, CommandToGetHelpOn
);
191 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_HELP_NF
), gShellLevel3HiiHandle
, CommandToGetHelpOn
);
193 ShellStatus
= SHELL_NOT_FOUND
;
195 while (OutText
[StrLen (OutText
) - 1] == L
'\r' || OutText
[StrLen (OutText
) - 1] == L
'\n' || OutText
[StrLen (OutText
) - 1] == L
' ') {
196 OutText
[StrLen (OutText
)-1] = CHAR_NULL
;
198 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_HELP_COMMAND
), gShellLevel3HiiHandle
, CommandToGetHelpOn
, OutText
);
199 if (OutText
!= NULL
) {
208 if (!Found
&& ShellStatus
== SHELL_SUCCESS
) {
209 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_HELP_NF
), gShellLevel3HiiHandle
, CommandToGetHelpOn
);
210 ShellStatus
= SHELL_NOT_FOUND
;
214 // free the command line package
216 ShellCommandLineFreeVarList (Package
);
219 if (CommandToGetHelpOn
!= NULL
) {
220 FreePool(CommandToGetHelpOn
);
222 if (SectionToGetHelpOn
!= NULL
) {
223 FreePool(SectionToGetHelpOn
);
226 return (ShellStatus
);