]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Set.c
a3452f0b624cbb3261300fe3966be08c2ea9ac94
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Set.c
1 /** @file
2 Main file for attrib shell level 2 function.
3
4 Copyright (c) 2009 - 2010, 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
9
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.
12
13 **/
14
15 #include "UefiShellLevel2CommandsLib.h"
16
17 /**
18 Print out each environment variable registered with the Shell 2.0 GUID.
19
20 If you spawn a pre 2.0 shell from the Shell 2.0 the environment variable may not carry through.
21
22 @retval STATUS_SUCCESS the printout was sucessful
23 @return any return code from GetNextVariableName except EFI_NOT_FOUND
24 **/
25 SHELL_STATUS
26 EFIAPI
27 PrintAllShellEnvVars(
28 VOID
29 )
30 {
31 CONST CHAR16 *Value;
32 CONST CHAR16 *ConstEnvNameList;
33
34 ConstEnvNameList = gEfiShellProtocol->GetEnv(NULL);
35 if (ConstEnvNameList == NULL) {
36 return (SHELL_SUCCESS);
37 }
38 while (*ConstEnvNameList != CHAR_NULL){
39 Value = gEfiShellProtocol->GetEnv(ConstEnvNameList);
40 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SET_DISP), gShellLevel2HiiHandle, ConstEnvNameList, Value);
41 ConstEnvNameList += StrLen(ConstEnvNameList)+1;
42 }
43
44 return (SHELL_SUCCESS);
45 }
46
47 STATIC CONST SHELL_PARAM_ITEM SetParamList[] = {
48 {L"-d", TypeValue},
49 {L"-v", TypeFlag},
50 {NULL, TypeMax}
51 };
52
53 /**
54 Function for 'set' command.
55
56 @param[in] ImageHandle Handle to the Image (NULL if Internal).
57 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
58 **/
59 SHELL_STATUS
60 EFIAPI
61 ShellCommandRunSet (
62 IN EFI_HANDLE ImageHandle,
63 IN EFI_SYSTEM_TABLE *SystemTable
64 )
65 {
66 EFI_STATUS Status;
67 LIST_ENTRY *Package;
68 CONST CHAR16 *KeyName;
69 CONST CHAR16 *Value;
70 CHAR16 *ProblemParam;
71 SHELL_STATUS ShellStatus;
72
73 ProblemParam = NULL;
74 ShellStatus = SHELL_SUCCESS;
75
76 //
77 // initialize the shell lib (we must be in non-auto-init...)
78 //
79 Status = ShellInitialize();
80 ASSERT_EFI_ERROR(Status);
81
82 //
83 // Make sure globals are good...
84 //
85 Status = CommandInit();
86 ASSERT_EFI_ERROR(Status);
87
88 //
89 // parse the command line
90 //
91 Status = ShellCommandLineParse (SetParamList, &Package, &ProblemParam, TRUE);
92 if (EFI_ERROR(Status)) {
93 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
94 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);
95 FreePool(ProblemParam);
96 return (SHELL_INVALID_PARAMETER);
97 } else {
98 ASSERT(FALSE);
99 }
100 } else {
101 //
102 // check for "-?"
103 //
104 if (ShellCommandLineGetFlag(Package, L"-?")) {
105 ASSERT(FALSE);
106 } else if (ShellCommandLineGetRawValue(Package, 3) != NULL) {
107 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);
108 ShellStatus = SHELL_INVALID_PARAMETER;
109 } else if (ShellCommandLineGetRawValue(Package, 1) != NULL && ShellCommandLineGetFlag(Package, L"-d")) {
110 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);
111 ShellStatus = SHELL_INVALID_PARAMETER;
112 } else if (ShellCommandLineGetFlag(Package, L"-d")) {
113 //
114 // delete a environment variable
115 //
116 KeyName = ShellCommandLineGetValue(Package, L"-d");
117 if (KeyName == NULL) {
118 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellLevel2HiiHandle, L"-d");
119 ShellStatus = SHELL_INVALID_PARAMETER;
120 } else {
121 Status = ShellSetEnvironmentVariable(KeyName, L"", ShellCommandLineGetFlag(Package, L"-v"));
122 if (EFI_ERROR(Status)) {
123 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SET_ND), gShellLevel2HiiHandle, KeyName, Status);
124 ShellStatus = SHELL_DEVICE_ERROR;
125 }
126 }
127 } else if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
128 //
129 // print out all current environment variables
130 //
131 return(PrintAllShellEnvVars());
132 } else {
133 //
134 // we are either printing one or assigning one
135 //
136 KeyName = ShellCommandLineGetRawValue(Package, 1);
137 Value = ShellCommandLineGetRawValue(Package, 2);
138 if (KeyName != NULL && Value != NULL) {
139 //
140 // assigning one
141 //
142 Status = ShellSetEnvironmentVariable(KeyName, Value, ShellCommandLineGetFlag(Package, L"-v"));
143 } else {
144 if (KeyName != NULL) {
145 //
146 // print out value for this one only.
147 //
148 Value = ShellGetEnvironmentVariable(KeyName);
149 if (Value == NULL) {
150 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SET_NF), gShellLevel2HiiHandle, KeyName);
151 ShellStatus = SHELL_SUCCESS;
152 } else {
153 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SET_DISP), gShellLevel2HiiHandle, KeyName, Value);
154 ShellStatus = SHELL_SUCCESS;
155 }
156 } else {
157 ASSERT(FALSE);
158 }
159 }
160 }
161 }
162
163 //
164 // free the command line package
165 //
166 ShellCommandLineFreeVarList (Package);
167
168 return (ShellStatus);
169 }