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