2 Main file for Alias shell level 3 function.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved. <BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "UefiShellLevel3CommandsLib.h"
18 #include <Library/ShellLib.h>
21 Print out single alias registered with the Shell.
23 @param[in] Alias Points to the NULL-terminated shell alias.
24 If this parameter is NULL, then all
25 aliases will be returned in ReturnedData.
26 @retval SHELL_SUCCESS the printout was sucessful
29 PrintSingleShellAlias(
30 IN CONST CHAR16
*Alias
33 CONST CHAR16
*ConstAliasVal
;
34 SHELL_STATUS ShellStatus
;
37 ShellStatus
= SHELL_SUCCESS
;
38 ConstAliasVal
= gEfiShellProtocol
->GetAlias (Alias
, &Volatile
);
39 if (ConstAliasVal
== NULL
) {
40 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellLevel3HiiHandle
, L
"alias", Alias
);
41 ShellStatus
= SHELL_INVALID_PARAMETER
;
43 if (ShellCommandIsOnAliasList (Alias
)) {
46 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_ALIAS_OUTPUT
), gShellLevel3HiiHandle
, !Volatile
? L
' ' : L
'*', Alias
, ConstAliasVal
);
52 Print out each alias registered with the Shell.
54 @retval STATUS_SUCCESS the printout was sucessful
55 @return any return code from GetNextVariableName except EFI_NOT_FOUND
62 CONST CHAR16
*ConstAllAliasList
;
66 ConstAllAliasList
= gEfiShellProtocol
->GetAlias(NULL
, NULL
);
67 if (ConstAllAliasList
== NULL
) {
68 return (SHELL_SUCCESS
);
70 Alias
= AllocateZeroPool(StrSize(ConstAllAliasList
));
72 return (SHELL_OUT_OF_RESOURCES
);
74 Walker
= (CHAR16
*)ConstAllAliasList
;
77 CopyMem(Alias
, Walker
, StrSize(Walker
));
78 Walker
= StrStr(Alias
, L
";");
80 Walker
[0] = CHAR_NULL
;
83 PrintSingleShellAlias(Alias
);
84 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
88 return (SHELL_SUCCESS
);
92 Changes a shell command alias.
94 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
97 @param[in] Command Points to the NULL-terminated shell command or existing alias.
98 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
99 Command refers to an alias, that alias will be deleted.
100 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If
101 FALSE and the alias already exists, then the existing alias is unchanged and
102 EFI_ACCESS_DENIED is returned.
103 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
104 Alias being set will be stored in a non-volatile fashion.
106 @retval SHELL_SUCCESS Alias created or deleted successfully.
107 @retval SHELL_NOT_FOUND the Alias intended to be deleted was not found
108 @retval SHELL_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to
110 @retval SHELL_DEVICE_ERROR Command is null or the empty string.
113 ShellLevel3CommandsLibSetAlias(
114 IN CONST CHAR16
*Command
,
115 IN CONST CHAR16
*Alias
,
120 SHELL_STATUS ShellStatus
;
123 ShellStatus
= SHELL_SUCCESS
;
124 Status
= gEfiShellProtocol
->SetAlias (Command
, Alias
, Replace
, Volatile
);
125 if (EFI_ERROR(Status
)) {
126 if (Status
== EFI_ACCESS_DENIED
) {
127 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_AD
), gShellLevel3HiiHandle
, L
"alias");
128 ShellStatus
= SHELL_ACCESS_DENIED
;
129 } else if (Status
== EFI_NOT_FOUND
) {
130 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_NOT_FOUND
), gShellLevel3HiiHandle
, L
"alias", Command
);
131 ShellStatus
= SHELL_NOT_FOUND
;
133 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_UK
), gShellLevel3HiiHandle
, L
"alias", Status
);
134 ShellStatus
= SHELL_DEVICE_ERROR
;
140 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
147 Function for 'alias' command.
149 @param[in] ImageHandle Handle to the Image (NULL if Internal).
150 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
154 ShellCommandRunAlias (
155 IN EFI_HANDLE ImageHandle
,
156 IN EFI_SYSTEM_TABLE
*SystemTable
161 CHAR16
*ProblemParam
;
162 SHELL_STATUS ShellStatus
;
163 CONST CHAR16
*Param1
;
164 CONST CHAR16
*Param2
;
165 CONST CHAR16
*ParamStrD
;
168 BOOLEAN VolatileFlag
;
171 ShellStatus
= SHELL_SUCCESS
;
175 // initialize the shell lib (we must be in non-auto-init...)
177 Status
= ShellInitialize();
178 ASSERT_EFI_ERROR(Status
);
180 Status
= CommandInit();
181 ASSERT_EFI_ERROR(Status
);
184 // parse the command line
186 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
187 if (EFI_ERROR(Status
)) {
188 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
189 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, L
"alias", ProblemParam
);
190 FreePool(ProblemParam
);
191 ShellStatus
= SHELL_INVALID_PARAMETER
;
196 Param1
= ShellCommandLineGetRawValue(Package
, 1);
197 Param2
= ShellCommandLineGetRawValue(Package
, 2);
199 DeleteFlag
= ShellCommandLineGetFlag (Package
, L
"-d");
200 VolatileFlag
= ShellCommandLineGetFlag (Package
, L
"-v");
202 if (Param2
!= NULL
) {
203 CleanParam2
= AllocateCopyPool (StrSize(Param2
), Param2
);
204 if (CleanParam2
== NULL
) {
205 ShellCommandLineFreeVarList (Package
);
206 return SHELL_OUT_OF_RESOURCES
;
209 if (CleanParam2
[0] == L
'\"' && CleanParam2
[StrLen(CleanParam2
)-1] == L
'\"') {
210 CleanParam2
[StrLen(CleanParam2
)-1] = L
'\0';
211 CopyMem (CleanParam2
, CleanParam2
+ 1, StrSize(CleanParam2
) - sizeof(CleanParam2
[0]));
215 if (!DeleteFlag
&& !VolatileFlag
) {
216 switch (ShellCommandLineGetCount (Package
)) {
221 ShellStatus
= PrintAllShellAlias ();
227 ShellStatus
= PrintSingleShellAlias (Param1
);
231 // "alias Param1 CleanParam2"
233 ShellStatus
= ShellLevel3CommandsLibSetAlias (CleanParam2
, Param1
, FALSE
, VolatileFlag
);
236 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel3HiiHandle
, L
"alias");
237 ShellStatus
= SHELL_INVALID_PARAMETER
;
239 } else if (DeleteFlag
) {
240 if (VolatileFlag
|| ShellCommandLineGetCount (Package
) > 1) {
241 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel3HiiHandle
, L
"alias");
242 ShellStatus
= SHELL_INVALID_PARAMETER
;
244 ParamStrD
= ShellCommandLineGetValue (Package
, L
"-d");
245 if (ParamStrD
== NULL
) {
246 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel3HiiHandle
, L
"alias");
247 ShellStatus
= SHELL_INVALID_PARAMETER
;
250 // Delete an alias: "alias -d ParamStrD"
252 ShellStatus
= ShellLevel3CommandsLibSetAlias (ParamStrD
, NULL
, TRUE
, FALSE
);
257 // Set volatile alias.
259 ASSERT (VolatileFlag
);
260 ASSERT (!DeleteFlag
);
261 switch (ShellCommandLineGetCount (Package
)) {
264 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel3HiiHandle
, L
"alias");
265 ShellStatus
= SHELL_INVALID_PARAMETER
;
269 // "alias -v Param1 CleanParam2"
271 ShellStatus
= ShellLevel3CommandsLibSetAlias (CleanParam2
, Param1
, FALSE
, VolatileFlag
);
274 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel3HiiHandle
, L
"alias");
275 ShellStatus
= SHELL_INVALID_PARAMETER
;
279 // free the command line package
281 ShellCommandLineFreeVarList (Package
);
284 SHELL_FREE_NON_NULL (CleanParam2
);
285 return (ShellStatus
);