]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Application/Shell/Shell.c
ShellPkg: Create a homefilesystem environment variable
[mirror_edk2.git] / ShellPkg / Application / Shell / Shell.c
CommitLineData
a405b86d 1/** @file\r
2 This is THE shell (application)\r
3\r
ba0014b9 4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
c011b6c9 5 (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.<BR>\r
a405b86d 6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "Shell.h"\r
17\r
18//\r
19// Initialize the global structure\r
20//\r
21SHELL_INFO ShellInfoObject = {\r
22 NULL,\r
23 NULL,\r
24 FALSE,\r
25 FALSE,\r
26 {\r
ce68d3bc
SZ
27 {{\r
28 0,\r
29 0,\r
30 0,\r
31 0,\r
32 0,\r
33 0,\r
34 0,\r
35 0,\r
dcbdb8bf 36 0,\r
ce68d3bc
SZ
37 0\r
38 }},\r
a405b86d 39 0,\r
40 NULL,\r
41 NULL\r
42 },\r
ce68d3bc 43 {{NULL, NULL}, NULL},\r
a405b86d 44 {\r
ce68d3bc 45 {{NULL, NULL}, NULL},\r
a405b86d 46 0,\r
47 0,\r
48 TRUE\r
49 },\r
50 NULL,\r
51 0,\r
52 NULL,\r
53 NULL,\r
54 NULL,\r
55 NULL,\r
56 NULL,\r
ce68d3bc
SZ
57 {{NULL, NULL}, NULL, NULL},\r
58 {{NULL, NULL}, NULL, NULL},\r
59 NULL,\r
60 NULL,\r
61 NULL,\r
62 NULL,\r
8be0ba36 63 NULL,\r
64 NULL,\r
65 NULL,\r
a49f6a2f 66 NULL,\r
67 FALSE\r
a405b86d 68};\r
69\r
70STATIC CONST CHAR16 mScriptExtension[] = L".NSH";\r
71STATIC CONST CHAR16 mExecutableExtensions[] = L".NSH;.EFI";\r
3c865f20 72STATIC CONST CHAR16 mStartupScript[] = L"startup.nsh";\r
dcbdb8bf
QS
73CONST CHAR16 mNoNestingEnvVarName[] = L"nonesting";\r
74CONST CHAR16 mNoNestingTrue[] = L"True";\r
75CONST CHAR16 mNoNestingFalse[] = L"False";\r
a405b86d 76\r
ad2bc854 77/**\r
51429264 78 Cleans off leading and trailing spaces and tabs.\r
ad2bc854 79\r
51429264 80 @param[in] String pointer to the string to trim them off.\r
ad2bc854
JC
81**/\r
82EFI_STATUS\r
ad2bc854
JC
83TrimSpaces(\r
84 IN CHAR16 **String\r
85 )\r
86{\r
87 ASSERT(String != NULL);\r
88 ASSERT(*String!= NULL);\r
89 //\r
90 // Remove any spaces and tabs at the beginning of the (*String).\r
91 //\r
92 while (((*String)[0] == L' ') || ((*String)[0] == L'\t')) {\r
93 CopyMem((*String), (*String)+1, StrSize((*String)) - sizeof((*String)[0]));\r
94 }\r
95\r
96 //\r
404b3f43 97 // Remove any spaces and tabs at the end of the (*String).\r
ad2bc854 98 //\r
404b3f43 99 while ((StrLen (*String) > 0) && (((*String)[StrLen((*String))-1] == L' ') || ((*String)[StrLen((*String))-1] == L'\t'))) {\r
ad2bc854
JC
100 (*String)[StrLen((*String))-1] = CHAR_NULL;\r
101 }\r
102\r
103 return (EFI_SUCCESS);\r
104}\r
105\r
a95cf8f0 106/**\r
ba0014b9 107 Parse for the next instance of one string within another string. Can optionally make sure that\r
a95cf8f0
JC
108 the string was not escaped (^ character) per the shell specification.\r
109\r
110 @param[in] SourceString The string to search within\r
111 @param[in] FindString The string to look for\r
112 @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances\r
113**/\r
114CHAR16*\r
a95cf8f0
JC
115FindNextInstance(\r
116 IN CONST CHAR16 *SourceString,\r
117 IN CONST CHAR16 *FindString,\r
118 IN CONST BOOLEAN CheckForEscapeCharacter\r
119 )\r
120{\r
121 CHAR16 *Temp;\r
122 if (SourceString == NULL) {\r
123 return (NULL);\r
124 }\r
125 Temp = StrStr(SourceString, FindString);\r
126\r
127 //\r
d08a5464 128 // If nothing found, or we don't care about escape characters\r
a95cf8f0
JC
129 //\r
130 if (Temp == NULL || !CheckForEscapeCharacter) {\r
131 return (Temp);\r
132 }\r
133\r
134 //\r
135 // If we found an escaped character, try again on the remainder of the string\r
136 //\r
137 if ((Temp > (SourceString)) && *(Temp-1) == L'^') {\r
138 return FindNextInstance(Temp+1, FindString, CheckForEscapeCharacter);\r
139 }\r
140\r
141 //\r
142 // we found the right character\r
143 //\r
144 return (Temp);\r
145}\r
146\r
42435671 147/**\r
d08a5464 148 Check whether the string between a pair of % is a valid environment variable name.\r
42435671
QS
149\r
150 @param[in] BeginPercent pointer to the first percent.\r
151 @param[in] EndPercent pointer to the last percent.\r
152\r
153 @retval TRUE is a valid environment variable name.\r
154 @retval FALSE is NOT a valid environment variable name.\r
155**/\r
156BOOLEAN\r
157IsValidEnvironmentVariableName(\r
158 IN CONST CHAR16 *BeginPercent,\r
159 IN CONST CHAR16 *EndPercent\r
160 )\r
161{\r
162 CONST CHAR16 *Walker;\r
ba0014b9 163\r
42435671
QS
164 Walker = NULL;\r
165\r
166 ASSERT (BeginPercent != NULL);\r
167 ASSERT (EndPercent != NULL);\r
168 ASSERT (BeginPercent < EndPercent);\r
ba0014b9 169\r
42435671
QS
170 if ((BeginPercent + 1) == EndPercent) {\r
171 return FALSE;\r
172 }\r
173\r
174 for (Walker = BeginPercent + 1; Walker < EndPercent; Walker++) {\r
175 if (\r
176 (*Walker >= L'0' && *Walker <= L'9') ||\r
177 (*Walker >= L'A' && *Walker <= L'Z') ||\r
178 (*Walker >= L'a' && *Walker <= L'z') ||\r
179 (*Walker == L'_')\r
180 ) {\r
181 if (Walker == BeginPercent + 1 && (*Walker >= L'0' && *Walker <= L'9')) {\r
182 return FALSE;\r
183 } else {\r
184 continue;\r
185 }\r
186 } else {\r
187 return FALSE;\r
188 }\r
189 }\r
190\r
191 return TRUE;\r
192}\r
193\r
518c8cdc
JC
194/**\r
195 Determine if a command line contains a split operation\r
196\r
197 @param[in] CmdLine The command line to parse.\r
198\r
199 @retval TRUE CmdLine has a valid split.\r
200 @retval FALSE CmdLine does not have a valid split.\r
201**/\r
202BOOLEAN\r
518c8cdc
JC
203ContainsSplit(\r
204 IN CONST CHAR16 *CmdLine\r
205 )\r
206{\r
207 CONST CHAR16 *TempSpot;\r
6f6792b8
QS
208 CONST CHAR16 *FirstQuote;\r
209 CONST CHAR16 *SecondQuote;\r
210\r
a95cf8f0 211 FirstQuote = FindNextInstance (CmdLine, L"\"", TRUE);\r
6f6792b8 212 SecondQuote = NULL;\r
1219c85d 213 TempSpot = FindFirstCharacter(CmdLine, L"|", L'^');\r
6f6792b8 214\r
ba0014b9
LG
215 if (FirstQuote == NULL ||\r
216 TempSpot == NULL ||\r
217 TempSpot == CHAR_NULL ||\r
6f6792b8
QS
218 FirstQuote > TempSpot\r
219 ) {\r
220 return (BOOLEAN) ((TempSpot != NULL) && (*TempSpot != CHAR_NULL));\r
221 }\r
222\r
223 while ((TempSpot != NULL) && (*TempSpot != CHAR_NULL)) {\r
fe8ec3dd 224 if (FirstQuote == NULL || FirstQuote > TempSpot) {\r
6f6792b8 225 break;\r
ba0014b9 226 }\r
a95cf8f0 227 SecondQuote = FindNextInstance (FirstQuote + 1, L"\"", TRUE);\r
fe8ec3dd 228 if (SecondQuote == NULL) {\r
6f6792b8
QS
229 break;\r
230 }\r
231 if (SecondQuote < TempSpot) {\r
a95cf8f0 232 FirstQuote = FindNextInstance (SecondQuote + 1, L"\"", TRUE);\r
6f6792b8
QS
233 continue;\r
234 } else {\r
a95cf8f0 235 FirstQuote = FindNextInstance (SecondQuote + 1, L"\"", TRUE);\r
1219c85d 236 TempSpot = FindFirstCharacter(TempSpot + 1, L"|", L'^');\r
6f6792b8 237 continue;\r
ba0014b9 238 }\r
6f6792b8 239 }\r
ba0014b9 240\r
8dcd84b9 241 return (BOOLEAN) ((TempSpot != NULL) && (*TempSpot != CHAR_NULL));\r
19a44972
JC
242}\r
243\r
733f138d 244/**\r
ba0014b9 245 Function to start monitoring for CTRL-S using SimpleTextInputEx. This\r
733f138d 246 feature's enabled state was not known when the shell initially launched.\r
247\r
248 @retval EFI_SUCCESS The feature is enabled.\r
d08a5464 249 @retval EFI_OUT_OF_RESOURCES There is not enough memory available.\r
733f138d 250**/\r
251EFI_STATUS\r
733f138d 252InternalEfiShellStartCtrlSMonitor(\r
253 VOID\r
254 )\r
255{\r
256 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;\r
257 EFI_KEY_DATA KeyData;\r
258 EFI_STATUS Status;\r
259\r
260 Status = gBS->OpenProtocol(\r
261 gST->ConsoleInHandle,\r
262 &gEfiSimpleTextInputExProtocolGuid,\r
263 (VOID**)&SimpleEx,\r
264 gImageHandle,\r
265 NULL,\r
266 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
267 if (EFI_ERROR(Status)) {\r
268 ShellPrintHiiEx(\r
ba0014b9
LG
269 -1,\r
270 -1,\r
733f138d 271 NULL,\r
272 STRING_TOKEN (STR_SHELL_NO_IN_EX),\r
273 ShellInfoObject.HiiHandle);\r
274 return (EFI_SUCCESS);\r
275 }\r
276\r
277 KeyData.KeyState.KeyToggleState = 0;\r
278 KeyData.Key.ScanCode = 0;\r
279 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;\r
280 KeyData.Key.UnicodeChar = L's';\r
281\r
282 Status = SimpleEx->RegisterKeyNotify(\r
283 SimpleEx,\r
284 &KeyData,\r
285 NotificationFunction,\r
286 &ShellInfoObject.CtrlSNotifyHandle1);\r
ba0014b9 287\r
733f138d 288 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r
289 if (!EFI_ERROR(Status)) {\r
290 Status = SimpleEx->RegisterKeyNotify(\r
291 SimpleEx,\r
292 &KeyData,\r
293 NotificationFunction,\r
294 &ShellInfoObject.CtrlSNotifyHandle2);\r
295 }\r
296 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;\r
297 KeyData.Key.UnicodeChar = 19;\r
298\r
299 if (!EFI_ERROR(Status)) {\r
300 Status = SimpleEx->RegisterKeyNotify(\r
301 SimpleEx,\r
302 &KeyData,\r
303 NotificationFunction,\r
ed8b5297 304 &ShellInfoObject.CtrlSNotifyHandle3);\r
ba0014b9 305 }\r
733f138d 306 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r
307 if (!EFI_ERROR(Status)) {\r
308 Status = SimpleEx->RegisterKeyNotify(\r
309 SimpleEx,\r
310 &KeyData,\r
311 NotificationFunction,\r
ed8b5297 312 &ShellInfoObject.CtrlSNotifyHandle4);\r
733f138d 313 }\r
314 return (Status);\r
315}\r
316\r
317\r
318\r
a405b86d 319/**\r
320 The entry point for the application.\r
321\r
322 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
323 @param[in] SystemTable A pointer to the EFI System Table.\r
324\r
325 @retval EFI_SUCCESS The entry point is executed successfully.\r
326 @retval other Some error occurs when executing this entry point.\r
327\r
328**/\r
329EFI_STATUS\r
330EFIAPI\r
331UefiMain (\r
332 IN EFI_HANDLE ImageHandle,\r
333 IN EFI_SYSTEM_TABLE *SystemTable\r
334 )\r
335{\r
a49f6a2f 336 EFI_STATUS Status;\r
337 CHAR16 *TempString;\r
338 UINTN Size;\r
339 EFI_HANDLE ConInHandle;\r
340 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *OldConIn;\r
bd3fc813 341 SPLIT_LIST *Split;\r
a405b86d 342\r
343 if (PcdGet8(PcdShellSupportLevel) > 3) {\r
344 return (EFI_UNSUPPORTED);\r
345 }\r
346\r
347 //\r
348 // Clear the screen\r
349 //\r
350 Status = gST->ConOut->ClearScreen(gST->ConOut);\r
3c865f20 351 if (EFI_ERROR(Status)) {\r
352 return (Status);\r
353 }\r
a405b86d 354\r
355 //\r
356 // Populate the global structure from PCDs\r
357 //\r
358 ShellInfoObject.ImageDevPath = NULL;\r
359 ShellInfoObject.FileDevPath = NULL;\r
360 ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault);\r
361 ShellInfoObject.ViewingSettings.InsertMode = PcdGetBool(PcdShellInsertModeDefault);\r
362 ShellInfoObject.LogScreenCount = PcdGet8 (PcdShellScreenLogCount );\r
363\r
364 //\r
365 // verify we dont allow for spec violation\r
366 //\r
367 ASSERT(ShellInfoObject.LogScreenCount >= 3);\r
368\r
369 //\r
370 // Initialize the LIST ENTRY objects...\r
371 //\r
372 InitializeListHead(&ShellInfoObject.BufferToFreeList.Link);\r
373 InitializeListHead(&ShellInfoObject.ViewingSettings.CommandHistory.Link);\r
374 InitializeListHead(&ShellInfoObject.SplitList.Link);\r
375\r
376 //\r
377 // Check PCDs for optional features that are not implemented yet.\r
378 //\r
379 if ( PcdGetBool(PcdShellSupportOldProtocols)\r
380 || !FeaturePcdGet(PcdShellRequireHiiPlatform)\r
381 || FeaturePcdGet(PcdShellSupportFrameworkHii)\r
382 ) {\r
383 return (EFI_UNSUPPORTED);\r
384 }\r
385\r
386 //\r
387 // turn off the watchdog timer\r
388 //\r
389 gBS->SetWatchdogTimer (0, 0, 0, NULL);\r
390\r
391 //\r
392 // install our console logger. This will keep a log of the output for back-browsing\r
393 //\r
394 Status = ConsoleLoggerInstall(ShellInfoObject.LogScreenCount, &ShellInfoObject.ConsoleInfo);\r
3c865f20 395 if (!EFI_ERROR(Status)) {\r
396 //\r
397 // Enable the cursor to be visible\r
398 //\r
399 gST->ConOut->EnableCursor (gST->ConOut, TRUE);\r
a405b86d 400\r
3c865f20 401 //\r
402 // If supporting EFI 1.1 we need to install HII protocol\r
403 // only do this if PcdShellRequireHiiPlatform == FALSE\r
404 //\r
405 // remove EFI_UNSUPPORTED check above when complete.\r
406 ///@todo add support for Framework HII\r
a405b86d 407\r
3c865f20 408 //\r
409 // install our (solitary) HII package\r
410 //\r
411 ShellInfoObject.HiiHandle = HiiAddPackages (&gEfiCallerIdGuid, gImageHandle, ShellStrings, NULL);\r
a405b86d 412 if (ShellInfoObject.HiiHandle == NULL) {\r
3c865f20 413 if (PcdGetBool(PcdShellSupportFrameworkHii)) {\r
414 ///@todo Add our package into Framework HII\r
415 }\r
416 if (ShellInfoObject.HiiHandle == NULL) {\r
0477054b
BJ
417 Status = EFI_NOT_STARTED;\r
418 goto FreeResources;\r
3c865f20 419 }\r
a405b86d 420 }\r
a405b86d 421\r
3c865f20 422 //\r
423 // create and install the EfiShellParametersProtocol\r
424 //\r
425 Status = CreatePopulateInstallShellParametersProtocol(&ShellInfoObject.NewShellParametersProtocol, &ShellInfoObject.RootShellInstance);\r
426 ASSERT_EFI_ERROR(Status);\r
427 ASSERT(ShellInfoObject.NewShellParametersProtocol != NULL);\r
a405b86d 428\r
3c865f20 429 //\r
430 // create and install the EfiShellProtocol\r
431 //\r
432 Status = CreatePopulateInstallShellProtocol(&ShellInfoObject.NewEfiShellProtocol);\r
433 ASSERT_EFI_ERROR(Status);\r
434 ASSERT(ShellInfoObject.NewEfiShellProtocol != NULL);\r
a405b86d 435\r
3c865f20 436 //\r
437 // Now initialize the shell library (it requires Shell Parameters protocol)\r
438 //\r
439 Status = ShellInitialize();\r
440 ASSERT_EFI_ERROR(Status);\r
a405b86d 441\r
3c865f20 442 Status = CommandInit();\r
443 ASSERT_EFI_ERROR(Status);\r
a405b86d 444\r
b62bb885
QS
445 Status = ShellInitEnvVarList ();\r
446\r
3c865f20 447 //\r
448 // Check the command line\r
449 //\r
da92bf85
BJ
450 Status = ProcessCommandLine ();\r
451 if (EFI_ERROR (Status)) {\r
452 goto FreeResources;\r
453 }\r
a405b86d 454\r
3c865f20 455 //\r
456 // If shell support level is >= 1 create the mappings and paths\r
457 //\r
458 if (PcdGet8(PcdShellSupportLevel) >= 1) {\r
459 Status = ShellCommandCreateInitialMappingsAndPaths();\r
460 }\r
a405b86d 461\r
dcbdb8bf
QS
462 //\r
463 // Set the environment variable for nesting support\r
464 //\r
465 Size = 0;\r
466 TempString = NULL;\r
467 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest) {\r
468 //\r
469 // No change. require nesting in Shell Protocol Execute()\r
470 //\r
471 StrnCatGrow(&TempString,\r
472 &Size,\r
473 L"False",\r
474 0);\r
475 } else {\r
476 StrnCatGrow(&TempString,\r
477 &Size,\r
478 mNoNestingTrue,\r
479 0);\r
480 }\r
481 Status = InternalEfiShellSetEnv(mNoNestingEnvVarName, TempString, TRUE);\r
482 SHELL_FREE_NON_NULL(TempString);\r
483 Size = 0;\r
484\r
3c865f20 485 //\r
486 // save the device path for the loaded image and the device path for the filepath (under loaded image)\r
487 // These are where to look for the startup.nsh file\r
488 //\r
489 Status = GetDevicePathsForImageAndFile(&ShellInfoObject.ImageDevPath, &ShellInfoObject.FileDevPath);\r
a405b86d 490 ASSERT_EFI_ERROR(Status);\r
a405b86d 491\r
3c865f20 492 //\r
493 // Display the version\r
494 //\r
495 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion) {\r
496 ShellPrintHiiEx (\r
497 0,\r
498 gST->ConOut->Mode->CursorRow,\r
499 NULL,\r
284e034f 500 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL),\r
3c865f20 501 ShellInfoObject.HiiHandle,\r
502 SupportLevel[PcdGet8(PcdShellSupportLevel)],\r
503 gEfiShellProtocol->MajorVersion,\r
284e034f
CP
504 gEfiShellProtocol->MinorVersion\r
505 );\r
506\r
507 ShellPrintHiiEx (\r
508 -1,\r
509 -1,\r
510 NULL,\r
511 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER),\r
512 ShellInfoObject.HiiHandle,\r
513 (CHAR16 *) PcdGetPtr (PcdShellSupplier)\r
514 );\r
515\r
516 ShellPrintHiiEx (\r
517 -1,\r
518 -1,\r
519 NULL,\r
520 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI),\r
521 ShellInfoObject.HiiHandle,\r
3c865f20 522 (gST->Hdr.Revision&0xffff0000)>>16,\r
523 (gST->Hdr.Revision&0x0000ffff),\r
524 gST->FirmwareVendor,\r
525 gST->FirmwareRevision\r
526 );\r
527 }\r
a405b86d 528\r
3c865f20 529 //\r
530 // Display the mapping\r
531 //\r
532 if (PcdGet8(PcdShellSupportLevel) >= 2 && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap) {\r
a308e058 533 Status = RunCommand(L"map");\r
3c865f20 534 ASSERT_EFI_ERROR(Status);\r
535 }\r
a405b86d 536\r
3c865f20 537 //\r
538 // init all the built in alias'\r
539 //\r
540 Status = SetBuiltInAlias();\r
541 ASSERT_EFI_ERROR(Status);\r
a405b86d 542\r
3c865f20 543 //\r
544 // Initialize environment variables\r
545 //\r
546 if (ShellCommandGetProfileList() != NULL) {\r
547 Status = InternalEfiShellSetEnv(L"profiles", ShellCommandGetProfileList(), TRUE);\r
548 ASSERT_EFI_ERROR(Status);\r
549 }\r
a405b86d 550\r
3c865f20 551 Size = 100;\r
552 TempString = AllocateZeroPool(Size);\r
a405b86d 553\r
3c865f20 554 UnicodeSPrint(TempString, Size, L"%d", PcdGet8(PcdShellSupportLevel));\r
555 Status = InternalEfiShellSetEnv(L"uefishellsupport", TempString, TRUE);\r
556 ASSERT_EFI_ERROR(Status);\r
a405b86d 557\r
3c865f20 558 UnicodeSPrint(TempString, Size, L"%d.%d", ShellInfoObject.NewEfiShellProtocol->MajorVersion, ShellInfoObject.NewEfiShellProtocol->MinorVersion);\r
559 Status = InternalEfiShellSetEnv(L"uefishellversion", TempString, TRUE);\r
560 ASSERT_EFI_ERROR(Status);\r
a405b86d 561\r
3c865f20 562 UnicodeSPrint(TempString, Size, L"%d.%d", (gST->Hdr.Revision & 0xFFFF0000) >> 16, gST->Hdr.Revision & 0x0000FFFF);\r
563 Status = InternalEfiShellSetEnv(L"uefiversion", TempString, TRUE);\r
564 ASSERT_EFI_ERROR(Status);\r
a405b86d 565\r
3c865f20 566 FreePool(TempString);\r
a405b86d 567\r
3c865f20 568 if (!EFI_ERROR(Status)) {\r
569 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {\r
a405b86d 570 //\r
3c865f20 571 // Set up the event for CTRL-C monitoring...\r
a405b86d 572 //\r
8be0ba36 573 Status = InernalEfiShellStartMonitor();\r
3c865f20 574 }\r
575\r
733f138d 576 if (!EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
577 //\r
578 // Set up the event for CTRL-S monitoring...\r
579 //\r
580 Status = InternalEfiShellStartCtrlSMonitor();\r
581 }\r
582\r
a49f6a2f 583 if (!EFI_ERROR(Status) && ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
584 //\r
585 // close off the gST->ConIn\r
586 //\r
587 OldConIn = gST->ConIn;\r
588 ConInHandle = gST->ConsoleInHandle;\r
589 gST->ConIn = CreateSimpleTextInOnFile((SHELL_FILE_HANDLE)&FileInterfaceNulFile, &gST->ConsoleInHandle);\r
590 } else {\r
591 OldConIn = NULL;\r
592 ConInHandle = NULL;\r
593 }\r
594\r
3c865f20 595 if (!EFI_ERROR(Status) && PcdGet8(PcdShellSupportLevel) >= 1) {\r
a405b86d 596 //\r
3c865f20 597 // process the startup script or launch the called app.\r
a405b86d 598 //\r
a308e058 599 Status = DoStartupScript(ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath);\r
3c865f20 600 }\r
a405b86d 601\r
ca79c798 602 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit && !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
a405b86d 603 //\r
3c865f20 604 // begin the UI waiting loop\r
a405b86d 605 //\r
3c865f20 606 do {\r
607 //\r
608 // clean out all the memory allocated for CONST <something> * return values\r
609 // between each shell prompt presentation\r
610 //\r
611 if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){\r
612 FreeBufferList(&ShellInfoObject.BufferToFreeList);\r
613 }\r
614\r
615 //\r
616 // Reset page break back to default.\r
617 //\r
618 ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault);\r
81cd2f53 619 ASSERT (ShellInfoObject.ConsoleInfo != NULL);\r
3c865f20 620 ShellInfoObject.ConsoleInfo->Enabled = TRUE;\r
621 ShellInfoObject.ConsoleInfo->RowCounter = 0;\r
622\r
623 //\r
624 // Display Prompt\r
625 //\r
626 Status = DoShellPrompt();\r
627 } while (!ShellCommandGetExit());\r
628 }\r
a49f6a2f 629 if (OldConIn != NULL && ConInHandle != NULL) {\r
630 CloseSimpleTextInOnFile (gST->ConIn);\r
631 gST->ConIn = OldConIn;\r
632 gST->ConsoleInHandle = ConInHandle;\r
633 }\r
a405b86d 634 }\r
635 }\r
3c865f20 636\r
0477054b 637FreeResources:\r
a405b86d 638 //\r
639 // uninstall protocols / free memory / etc...\r
640 //\r
641 if (ShellInfoObject.UserBreakTimer != NULL) {\r
642 gBS->CloseEvent(ShellInfoObject.UserBreakTimer);\r
643 DEBUG_CODE(ShellInfoObject.UserBreakTimer = NULL;);\r
644 }\r
a405b86d 645 if (ShellInfoObject.ImageDevPath != NULL) {\r
646 FreePool(ShellInfoObject.ImageDevPath);\r
647 DEBUG_CODE(ShellInfoObject.ImageDevPath = NULL;);\r
648 }\r
649 if (ShellInfoObject.FileDevPath != NULL) {\r
650 FreePool(ShellInfoObject.FileDevPath);\r
651 DEBUG_CODE(ShellInfoObject.FileDevPath = NULL;);\r
652 }\r
653 if (ShellInfoObject.NewShellParametersProtocol != NULL) {\r
654 CleanUpShellParametersProtocol(ShellInfoObject.NewShellParametersProtocol);\r
655 DEBUG_CODE(ShellInfoObject.NewShellParametersProtocol = NULL;);\r
656 }\r
657 if (ShellInfoObject.NewEfiShellProtocol != NULL){\r
8be0ba36 658 if (ShellInfoObject.NewEfiShellProtocol->IsRootShell()){\r
92670471 659 InternalEfiShellSetEnv(L"cwd", NULL, TRUE);\r
8be0ba36 660 }\r
03140346 661 CleanUpShellEnvironment (ShellInfoObject.NewEfiShellProtocol);\r
a405b86d 662 DEBUG_CODE(ShellInfoObject.NewEfiShellProtocol = NULL;);\r
663 }\r
664\r
665 if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){\r
666 FreeBufferList(&ShellInfoObject.BufferToFreeList);\r
667 }\r
668\r
669 if (!IsListEmpty(&ShellInfoObject.SplitList.Link)){\r
bd3fc813
MH
670 ASSERT(FALSE); ///@todo finish this de-allocation (free SplitStdIn/Out when needed).\r
671\r
672 for ( Split = (SPLIT_LIST*)GetFirstNode (&ShellInfoObject.SplitList.Link)\r
673 ; !IsNull (&ShellInfoObject.SplitList.Link, &Split->Link)\r
674 ; Split = (SPLIT_LIST *)GetNextNode (&ShellInfoObject.SplitList.Link, &Split->Link)\r
675 ) {\r
676 RemoveEntryList (&Split->Link);\r
677 FreePool (Split);\r
678 }\r
679\r
680 DEBUG_CODE (InitializeListHead (&ShellInfoObject.SplitList.Link););\r
a405b86d 681 }\r
682\r
683 if (ShellInfoObject.ShellInitSettings.FileName != NULL) {\r
684 FreePool(ShellInfoObject.ShellInitSettings.FileName);\r
685 DEBUG_CODE(ShellInfoObject.ShellInitSettings.FileName = NULL;);\r
686 }\r
687\r
688 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r
689 FreePool(ShellInfoObject.ShellInitSettings.FileOptions);\r
690 DEBUG_CODE(ShellInfoObject.ShellInitSettings.FileOptions = NULL;);\r
691 }\r
692\r
693 if (ShellInfoObject.HiiHandle != NULL) {\r
694 HiiRemovePackages(ShellInfoObject.HiiHandle);\r
695 DEBUG_CODE(ShellInfoObject.HiiHandle = NULL;);\r
696 }\r
697\r
698 if (!IsListEmpty(&ShellInfoObject.ViewingSettings.CommandHistory.Link)){\r
699 FreeBufferList(&ShellInfoObject.ViewingSettings.CommandHistory);\r
700 }\r
701\r
702 ASSERT(ShellInfoObject.ConsoleInfo != NULL);\r
703 if (ShellInfoObject.ConsoleInfo != NULL) {\r
704 ConsoleLoggerUninstall(ShellInfoObject.ConsoleInfo);\r
705 FreePool(ShellInfoObject.ConsoleInfo);\r
706 DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;);\r
707 }\r
708\r
b62bb885
QS
709 ShellFreeEnvVarList ();\r
710\r
a308e058
RN
711 if (ShellCommandGetExit()) {\r
712 return ((EFI_STATUS)ShellCommandGetExitCode());\r
b6b22b13 713 }\r
a308e058 714 return (Status);\r
a405b86d 715}\r
716\r
717/**\r
718 Sets all the alias' that were registered with the ShellCommandLib library.\r
719\r
d08a5464 720 @retval EFI_SUCCESS all init commands were run successfully.\r
a405b86d 721**/\r
722EFI_STATUS\r
a405b86d 723SetBuiltInAlias(\r
e7275d3f 724 VOID\r
a405b86d 725 )\r
726{\r
727 EFI_STATUS Status;\r
728 CONST ALIAS_LIST *List;\r
729 ALIAS_LIST *Node;\r
730\r
731 //\r
732 // Get all the commands we want to run\r
733 //\r
734 List = ShellCommandGetInitAliasList();\r
735\r
736 //\r
737 // for each command in the List\r
738 //\r
739 for ( Node = (ALIAS_LIST*)GetFirstNode(&List->Link)\r
740 ; !IsNull (&List->Link, &Node->Link)\r
741 ; Node = (ALIAS_LIST *)GetNextNode(&List->Link, &Node->Link)\r
742 ){\r
743 //\r
744 // install the alias'\r
745 //\r
746 Status = InternalSetAlias(Node->CommandString, Node->Alias, TRUE);\r
747 ASSERT_EFI_ERROR(Status);\r
748 }\r
749 return (EFI_SUCCESS);\r
750}\r
751\r
752/**\r
753 Internal function to determine if 2 command names are really the same.\r
754\r
755 @param[in] Command1 The pointer to the first command name.\r
756 @param[in] Command2 The pointer to the second command name.\r
757\r
758 @retval TRUE The 2 command names are the same.\r
759 @retval FALSE The 2 command names are not the same.\r
760**/\r
761BOOLEAN\r
a405b86d 762IsCommand(\r
763 IN CONST CHAR16 *Command1,\r
764 IN CONST CHAR16 *Command2\r
765 )\r
766{\r
767 if (StringNoCaseCompare(&Command1, &Command2) == 0) {\r
768 return (TRUE);\r
769 }\r
770 return (FALSE);\r
771}\r
772\r
773/**\r
774 Internal function to determine if a command is a script only command.\r
775\r
776 @param[in] CommandName The pointer to the command name.\r
777\r
778 @retval TRUE The command is a script only command.\r
779 @retval FALSE The command is not a script only command.\r
780**/\r
781BOOLEAN\r
a405b86d 782IsScriptOnlyCommand(\r
783 IN CONST CHAR16 *CommandName\r
784 )\r
785{\r
786 if (IsCommand(CommandName, L"for")\r
787 ||IsCommand(CommandName, L"endfor")\r
788 ||IsCommand(CommandName, L"if")\r
789 ||IsCommand(CommandName, L"else")\r
790 ||IsCommand(CommandName, L"endif")\r
791 ||IsCommand(CommandName, L"goto")) {\r
792 return (TRUE);\r
793 }\r
794 return (FALSE);\r
795}\r
796\r
a405b86d 797/**\r
798 This function will populate the 2 device path protocol parameters based on the\r
799 global gImageHandle. The DevPath will point to the device path for the handle that has\r
800 loaded image protocol installed on it. The FilePath will point to the device path\r
801 for the file that was loaded.\r
802\r
d08a5464
JP
803 @param[in, out] DevPath On a successful return the device path to the loaded image.\r
804 @param[in, out] FilePath On a successful return the device path to the file.\r
a405b86d 805\r
d08a5464 806 @retval EFI_SUCCESS The 2 device paths were successfully returned.\r
a405b86d 807 @retval other A error from gBS->HandleProtocol.\r
808\r
809 @sa HandleProtocol\r
810**/\r
811EFI_STATUS\r
a405b86d 812GetDevicePathsForImageAndFile (\r
813 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevPath,\r
814 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath\r
815 )\r
816{\r
817 EFI_STATUS Status;\r
818 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
819 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;\r
820\r
821 ASSERT(DevPath != NULL);\r
822 ASSERT(FilePath != NULL);\r
823\r
824 Status = gBS->OpenProtocol (\r
825 gImageHandle,\r
826 &gEfiLoadedImageProtocolGuid,\r
827 (VOID**)&LoadedImage,\r
828 gImageHandle,\r
829 NULL,\r
830 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
831 );\r
832 if (!EFI_ERROR (Status)) {\r
833 Status = gBS->OpenProtocol (\r
834 LoadedImage->DeviceHandle,\r
835 &gEfiDevicePathProtocolGuid,\r
836 (VOID**)&ImageDevicePath,\r
837 gImageHandle,\r
838 NULL,\r
839 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
840 );\r
841 if (!EFI_ERROR (Status)) {\r
842 *DevPath = DuplicateDevicePath (ImageDevicePath);\r
843 *FilePath = DuplicateDevicePath (LoadedImage->FilePath);\r
a49f6a2f 844 gBS->CloseProtocol(\r
845 LoadedImage->DeviceHandle,\r
846 &gEfiDevicePathProtocolGuid,\r
847 gImageHandle,\r
848 NULL);\r
a405b86d 849 }\r
850 gBS->CloseProtocol(\r
851 gImageHandle,\r
852 &gEfiLoadedImageProtocolGuid,\r
853 gImageHandle,\r
854 NULL);\r
855 }\r
856 return (Status);\r
857}\r
858\r
a405b86d 859/**\r
860 Process all Uefi Shell 2.0 command line options.\r
861\r
862 see Uefi Shell 2.0 section 3.2 for full details.\r
863\r
864 the command line must resemble the following:\r
865\r
866 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]\r
867\r
868 ShellOpt-options Options which control the initialization behavior of the shell.\r
869 These options are read from the EFI global variable "ShellOpt"\r
870 and are processed before options or file-name.\r
871\r
872 options Options which control the initialization behavior of the shell.\r
873\r
874 file-name The name of a UEFI shell application or script to be executed\r
875 after initialization is complete. By default, if file-name is\r
876 specified, then -nostartup is implied. Scripts are not supported\r
877 by level 0.\r
878\r
879 file-name-options The command-line options that are passed to file-name when it\r
880 is invoked.\r
881\r
882 This will initialize the ShellInfoObject.ShellInitSettings global variable.\r
883\r
884 @retval EFI_SUCCESS The variable is initialized.\r
885**/\r
886EFI_STATUS\r
a405b86d 887ProcessCommandLine(\r
888 VOID\r
889 )\r
890{\r
23385d63
BJ
891 UINTN Size;\r
892 UINTN LoopVar;\r
893 CHAR16 *CurrentArg;\r
894 CHAR16 *DelayValueStr;\r
895 UINT64 DelayValue;\r
896 EFI_STATUS Status;\r
897 EFI_UNICODE_COLLATION_PROTOCOL *UnicodeCollation;\r
898\r
899 // `file-name-options` will contain arguments to `file-name` that we don't\r
900 // know about. This would cause ShellCommandLineParse to error, so we parse\r
901 // arguments manually, ignoring those after the first thing that doesn't look\r
902 // like a shell option (which is assumed to be `file-name`).\r
903\r
904 Status = gBS->LocateProtocol (\r
f3a51e98 905 &gEfiUnicodeCollation2ProtocolGuid,\r
23385d63
BJ
906 NULL,\r
907 (VOID **) &UnicodeCollation\r
908 );\r
909 if (EFI_ERROR (Status)) {\r
f3a51e98
MH
910 Status = gBS->LocateProtocol (\r
911 &gEfiUnicodeCollationProtocolGuid,\r
912 NULL,\r
913 (VOID **) &UnicodeCollation\r
914 );\r
915 if (EFI_ERROR (Status)) {\r
916 return Status;\r
917 }\r
23385d63
BJ
918 }\r
919\r
920 // Set default options\r
921 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = FALSE;\r
922 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = FALSE;\r
923 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = FALSE;\r
924 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = FALSE;\r
925 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = FALSE;\r
926 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = FALSE;\r
927 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = FALSE;\r
928 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = FALSE;\r
929 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = FALSE;\r
dcbdb8bf 930 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest = FALSE;\r
23385d63
BJ
931 ShellInfoObject.ShellInitSettings.Delay = 5;\r
932\r
e9d19a80
CP
933 //\r
934 // Start LoopVar at 0 to parse only optional arguments at Argv[0]\r
935 // and parse other parameters from Argv[1]. This is for use case that\r
936 // UEFI Shell boot option is created, and OptionalData is provided\r
937 // that starts with shell command-line options.\r
938 //\r
939 for (LoopVar = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {\r
23385d63
BJ
940 CurrentArg = gEfiShellParametersProtocol->Argv[LoopVar];\r
941 if (UnicodeCollation->StriColl (\r
942 UnicodeCollation,\r
943 L"-startup",\r
944 CurrentArg\r
945 ) == 0) {\r
946 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = TRUE;\r
3c865f20 947 }\r
23385d63
BJ
948 else if (UnicodeCollation->StriColl (\r
949 UnicodeCollation,\r
950 L"-nostartup",\r
951 CurrentArg\r
952 ) == 0) {\r
953 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = TRUE;\r
954 }\r
955 else if (UnicodeCollation->StriColl (\r
956 UnicodeCollation,\r
957 L"-noconsoleout",\r
958 CurrentArg\r
959 ) == 0) {\r
960 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = TRUE;\r
961 }\r
962 else if (UnicodeCollation->StriColl (\r
963 UnicodeCollation,\r
964 L"-noconsolein",\r
965 CurrentArg\r
966 ) == 0) {\r
967 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = TRUE;\r
968 }\r
969 else if (UnicodeCollation->StriColl (\r
970 UnicodeCollation,\r
971 L"-nointerrupt",\r
972 CurrentArg\r
973 ) == 0) {\r
974 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = TRUE;\r
975 }\r
976 else if (UnicodeCollation->StriColl (\r
977 UnicodeCollation,\r
978 L"-nomap",\r
979 CurrentArg\r
980 ) == 0) {\r
981 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = TRUE;\r
982 }\r
983 else if (UnicodeCollation->StriColl (\r
984 UnicodeCollation,\r
985 L"-noversion",\r
986 CurrentArg\r
987 ) == 0) {\r
988 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = TRUE;\r
989 }\r
dcbdb8bf
QS
990 else if (UnicodeCollation->StriColl (\r
991 UnicodeCollation,\r
992 L"-nonest",\r
993 CurrentArg\r
994 ) == 0) {\r
995 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest = TRUE;\r
996 }\r
23385d63
BJ
997 else if (UnicodeCollation->StriColl (\r
998 UnicodeCollation,\r
999 L"-delay",\r
1000 CurrentArg\r
1001 ) == 0) {\r
1002 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = TRUE;\r
1003 // Check for optional delay value following "-delay"\r
1004 DelayValueStr = gEfiShellParametersProtocol->Argv[LoopVar + 1];\r
1005 if (DelayValueStr != NULL){\r
1006 if (*DelayValueStr == L':') {\r
1007 DelayValueStr++;\r
1008 }\r
1009 if (!EFI_ERROR(ShellConvertStringToUint64 (\r
1010 DelayValueStr,\r
1011 &DelayValue,\r
1012 FALSE,\r
1013 FALSE\r
1014 ))) {\r
1015 ShellInfoObject.ShellInitSettings.Delay = (UINTN)DelayValue;\r
1016 LoopVar++;\r
1017 }\r
1018 }\r
1019 } else if (UnicodeCollation->StriColl (\r
1020 UnicodeCollation,\r
6ca29fa3 1021 L"-exit",\r
23385d63
BJ
1022 CurrentArg\r
1023 ) == 0) {\r
1024 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = TRUE;\r
1025 } else if (StrnCmp (L"-", CurrentArg, 1) == 0) {\r
d08a5464 1026 // Unrecognized option\r
23385d63
BJ
1027 ShellPrintHiiEx(-1, -1, NULL,\r
1028 STRING_TOKEN (STR_GEN_PROBLEM),\r
1029 ShellInfoObject.HiiHandle,\r
1030 CurrentArg\r
1031 );\r
1032 return EFI_INVALID_PARAMETER;\r
1033 } else {\r
e9d19a80
CP
1034 //\r
1035 // First argument should be Shell.efi image name\r
1036 //\r
1037 if (LoopVar == 0) {\r
1038 continue;\r
1039 }\r
1040\r
e044364b
MK
1041 ShellInfoObject.ShellInitSettings.FileName = NULL;\r
1042 Size = 0;\r
1043 //\r
1044 // If first argument contains a space, then add double quotes before the argument\r
1045 //\r
1046 if (StrStr (CurrentArg, L" ") != NULL) {\r
1047 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileName, &Size, L"\"", 0);\r
1048 if (ShellInfoObject.ShellInitSettings.FileName == NULL) {\r
1049 return (EFI_OUT_OF_RESOURCES);\r
1050 }\r
1051 }\r
1052 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileName, &Size, CurrentArg, 0);\r
23385d63
BJ
1053 if (ShellInfoObject.ShellInitSettings.FileName == NULL) {\r
1054 return (EFI_OUT_OF_RESOURCES);\r
1055 }\r
1056 //\r
e044364b
MK
1057 // If first argument contains a space, then add double quotes after the argument\r
1058 //\r
1059 if (StrStr (CurrentArg, L" ") != NULL) {\r
1060 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileName, &Size, L"\"", 0);\r
1061 if (ShellInfoObject.ShellInitSettings.FileName == NULL) {\r
1062 return (EFI_OUT_OF_RESOURCES);\r
1063 }\r
1064 }\r
1065 //\r
23385d63
BJ
1066 // We found `file-name`.\r
1067 //\r
1068 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = 1;\r
23385d63
BJ
1069 LoopVar++;\r
1070\r
1071 // Add `file-name-options`\r
ba71f790 1072 for (Size = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {\r
23385d63 1073 ASSERT((ShellInfoObject.ShellInitSettings.FileOptions == NULL && Size == 0) || (ShellInfoObject.ShellInitSettings.FileOptions != NULL));\r
e044364b
MK
1074 //\r
1075 // Add a space between arguments\r
1076 //\r
1077 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r
1078 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions, &Size, L" ", 0);\r
1079 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {\r
1080 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);\r
1081 return (EFI_OUT_OF_RESOURCES);\r
1082 }\r
1083 }\r
1084 //\r
1085 // If an argumnent contains a space, then add double quotes before the argument\r
1086 //\r
1087 if (StrStr (gEfiShellParametersProtocol->Argv[LoopVar], L" ") != NULL) {\r
1088 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,\r
1089 &Size,\r
1090 L"\"",\r
1091 0);\r
1092 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {\r
1093 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);\r
1094 return (EFI_OUT_OF_RESOURCES);\r
1095 }\r
23385d63
BJ
1096 }\r
1097 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,\r
1098 &Size,\r
1099 gEfiShellParametersProtocol->Argv[LoopVar],\r
1100 0);\r
1101 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {\r
1102 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);\r
1103 return (EFI_OUT_OF_RESOURCES);\r
a405b86d 1104 }\r
e044364b
MK
1105 //\r
1106 // If an argumnent contains a space, then add double quotes after the argument\r
1107 //\r
1108 if (StrStr (gEfiShellParametersProtocol->Argv[LoopVar], L" ") != NULL) {\r
1109 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,\r
1110 &Size,\r
1111 L"\"",\r
1112 0);\r
1113 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {\r
1114 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);\r
1115 return (EFI_OUT_OF_RESOURCES);\r
1116 }\r
1117 }\r
a405b86d 1118 }\r
a405b86d 1119 }\r
1120 }\r
1121\r
23385d63 1122 // "-nointerrupt" overrides "-delay"\r
733f138d 1123 if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {\r
1124 ShellInfoObject.ShellInitSettings.Delay = 0;\r
a405b86d 1125 }\r
a405b86d 1126\r
23385d63 1127 return EFI_SUCCESS;\r
a405b86d 1128}\r
1129\r
aaf51f08
CC
1130/**\r
1131 Function try to find location of the Startup.nsh file.\r
ba0014b9 1132\r
aaf51f08
CC
1133 The buffer is callee allocated and should be freed by the caller.\r
1134\r
3efa3f3d
DB
1135 @param ImageDevicePath The path to the image for shell. first place to look for the startup script\r
1136 @param FileDevicePath The path to the file for shell. second place to look for the startup script.\r
aaf51f08
CC
1137\r
1138 @retval NULL No Startup.nsh file was found.\r
1139 @return !=NULL Pointer to NULL-terminated path.\r
1140**/\r
1141CHAR16 *\r
1142LocateStartupScript (\r
1143 IN EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath,\r
1144 IN EFI_DEVICE_PATH_PROTOCOL *FileDevicePath\r
1145 )\r
1146{\r
1147 CHAR16 *StartupScriptPath;\r
1148 CHAR16 *TempSpot;\r
1149 CONST CHAR16 *MapName;\r
1150 UINTN Size;\r
1151\r
1152 StartupScriptPath = NULL;\r
1153 Size = 0;\r
1154\r
1155 //\r
1156 // Try to find 'Startup.nsh' in the directory where the shell itself was launched.\r
1157 //\r
1158 MapName = ShellInfoObject.NewEfiShellProtocol->GetMapFromDevicePath (&ImageDevicePath);\r
ba0014b9 1159 if (MapName != NULL) {\r
aaf51f08 1160 StartupScriptPath = StrnCatGrow (&StartupScriptPath, &Size, MapName, 0);\r
15de94cf
RN
1161 if (StartupScriptPath == NULL) {\r
1162 //\r
1163 // Do not locate the startup script in sys path when out of resource.\r
1164 //\r
1165 return NULL;\r
1166 }\r
aaf51f08
CC
1167 TempSpot = StrStr (StartupScriptPath, L";");\r
1168 if (TempSpot != NULL) {\r
1169 *TempSpot = CHAR_NULL;\r
1170 }\r
1171\r
c0b1f749
JD
1172 InternalEfiShellSetEnv(L"homefilesystem", StartupScriptPath, TRUE);\r
1173\r
aaf51f08
CC
1174 StartupScriptPath = StrnCatGrow (&StartupScriptPath, &Size, ((FILEPATH_DEVICE_PATH *)FileDevicePath)->PathName, 0);\r
1175 PathRemoveLastItem (StartupScriptPath);\r
1176 StartupScriptPath = StrnCatGrow (&StartupScriptPath, &Size, mStartupScript, 0);\r
1177 }\r
1178\r
1179 //\r
1180 // Try to find 'Startup.nsh' in the execution path defined by the envrionment variable PATH.\r
1181 //\r
1182 if ((StartupScriptPath == NULL) || EFI_ERROR (ShellIsFile (StartupScriptPath))) {\r
1183 SHELL_FREE_NON_NULL (StartupScriptPath);\r
1184 StartupScriptPath = ShellFindFilePath (mStartupScript);\r
1185 }\r
1186\r
1187 return StartupScriptPath;\r
1188}\r
1189\r
a405b86d 1190/**\r
1191 Handles all interaction with the default startup script.\r
1192\r
1193 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.\r
1194\r
1195 @param ImagePath the path to the image for shell. first place to look for the startup script\r
1196 @param FilePath the path to the file for shell. second place to look for the startup script.\r
1197\r
1198 @retval EFI_SUCCESS the variable is initialized.\r
1199**/\r
1200EFI_STATUS\r
a405b86d 1201DoStartupScript(\r
a308e058
RN
1202 IN EFI_DEVICE_PATH_PROTOCOL *ImagePath,\r
1203 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
a405b86d 1204 )\r
1205{\r
1206 EFI_STATUS Status;\r
490ce43d 1207 EFI_STATUS CalleeStatus;\r
a405b86d 1208 UINTN Delay;\r
1209 EFI_INPUT_KEY Key;\r
a405b86d 1210 CHAR16 *FileStringPath;\r
1211 UINTN NewSize;\r
1212\r
1213 Key.UnicodeChar = CHAR_NULL;\r
1214 Key.ScanCode = 0;\r
a405b86d 1215\r
1216 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup && ShellInfoObject.ShellInitSettings.FileName != NULL) {\r
1217 //\r
1218 // launch something else instead\r
1219 //\r
1220 NewSize = StrSize(ShellInfoObject.ShellInitSettings.FileName);\r
1221 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r
1222 NewSize += StrSize(ShellInfoObject.ShellInitSettings.FileOptions) + sizeof(CHAR16);\r
1223 }\r
1224 FileStringPath = AllocateZeroPool(NewSize);\r
3c865f20 1225 if (FileStringPath == NULL) {\r
1226 return (EFI_OUT_OF_RESOURCES);\r
1227 }\r
e75390f0 1228 StrCpyS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileName);\r
a405b86d 1229 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r
e75390f0
QS
1230 StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), L" ", NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);\r
1231 StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileOptions, NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);\r
a405b86d 1232 }\r
490ce43d
QS
1233 Status = RunShellCommand(FileStringPath, &CalleeStatus);\r
1234 if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit == TRUE) {\r
01e3a976 1235 ShellCommandRegisterExit(gEfiShellProtocol->BatchIsActive(), (UINT64)CalleeStatus);\r
490ce43d 1236 }\r
a405b86d 1237 FreePool(FileStringPath);\r
1238 return (Status);\r
1239\r
1240 }\r
1241\r
1242 //\r
1243 // for shell level 0 we do no scripts\r
1244 // Without the Startup bit overriding we allow for nostartup to prevent scripts\r
1245 //\r
1246 if ( (PcdGet8(PcdShellSupportLevel) < 1)\r
1247 || (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup)\r
1248 ){\r
1249 return (EFI_SUCCESS);\r
1250 }\r
1251\r
5559a41a 1252 gST->ConOut->EnableCursor(gST->ConOut, FALSE);\r
a405b86d 1253 //\r
1254 // print out our warning and see if they press a key\r
1255 //\r
284e034f 1256 for ( Status = EFI_UNSUPPORTED, Delay = ShellInfoObject.ShellInitSettings.Delay\r
733f138d 1257 ; Delay != 0 && EFI_ERROR(Status)\r
a405b86d 1258 ; Delay--\r
1259 ){\r
284e034f
CP
1260 ShellPrintHiiEx(0, gST->ConOut->Mode->CursorRow, NULL, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION), ShellInfoObject.HiiHandle, Delay);\r
1261 gBS->Stall (1000000);\r
733f138d 1262 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
1263 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
1264 }\r
a405b86d 1265 }\r
1266 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CRLF), ShellInfoObject.HiiHandle);\r
5559a41a 1267 gST->ConOut->EnableCursor(gST->ConOut, TRUE);\r
a405b86d 1268\r
1269 //\r
1270 // ESC was pressed\r
1271 //\r
1272 if (Status == EFI_SUCCESS && Key.UnicodeChar == 0 && Key.ScanCode == SCAN_ESC) {\r
1273 return (EFI_SUCCESS);\r
1274 }\r
1275\r
aaf51f08
CC
1276 FileStringPath = LocateStartupScript (ImagePath, FilePath);\r
1277 if (FileStringPath != NULL) {\r
1278 Status = RunScriptFile (FileStringPath, NULL, L"", ShellInfoObject.NewShellParametersProtocol);\r
1279 FreePool (FileStringPath);\r
35b523c2
RN
1280 } else {\r
1281 //\r
1282 // we return success since startup script is not mandatory.\r
1283 //\r
1284 Status = EFI_SUCCESS;\r
a405b86d 1285 }\r
1286\r
a405b86d 1287 return (Status);\r
1288}\r
1289\r
1290/**\r
1291 Function to perform the shell prompt looping. It will do a single prompt,\r
1292 dispatch the result, and then return. It is expected that the caller will\r
1293 call this function in a loop many times.\r
1294\r
1295 @retval EFI_SUCCESS\r
1296 @retval RETURN_ABORTED\r
1297**/\r
1298EFI_STATUS\r
a405b86d 1299DoShellPrompt (\r
1300 VOID\r
1301 )\r
1302{\r
1303 UINTN Column;\r
1304 UINTN Row;\r
1305 CHAR16 *CmdLine;\r
1306 CONST CHAR16 *CurDir;\r
1307 UINTN BufferSize;\r
1308 EFI_STATUS Status;\r
594e780b 1309 LIST_ENTRY OldBufferList;\r
a405b86d 1310\r
1311 CurDir = NULL;\r
1312\r
1313 //\r
1314 // Get screen setting to decide size of the command line buffer\r
1315 //\r
1316 gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &Column, &Row);\r
1317 BufferSize = Column * Row * sizeof (CHAR16);\r
1318 CmdLine = AllocateZeroPool (BufferSize);\r
1319 if (CmdLine == NULL) {\r
1320 return EFI_OUT_OF_RESOURCES;\r
1321 }\r
1322\r
594e780b 1323 SaveBufferList(&OldBufferList);\r
a405b86d 1324 CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");\r
1325\r
1326 //\r
1327 // Prompt for input\r
1328 //\r
1329 gST->ConOut->SetCursorPosition (gST->ConOut, 0, gST->ConOut->Mode->CursorRow);\r
1330\r
1331 if (CurDir != NULL && StrLen(CurDir) > 1) {\r
1332 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);\r
1333 } else {\r
1334 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);\r
1335 }\r
1336\r
1337 //\r
1338 // Read a line from the console\r
1339 //\r
1340 Status = ShellInfoObject.NewEfiShellProtocol->ReadFile(ShellInfoObject.NewShellParametersProtocol->StdIn, &BufferSize, CmdLine);\r
1341\r
1342 //\r
1343 // Null terminate the string and parse it\r
1344 //\r
1345 if (!EFI_ERROR (Status)) {\r
0c83ac57
RN
1346 //\r
1347 // Reset the CTRL-C event just before running the command (yes we ignore the return values)\r
1348 //\r
1349 Status = gBS->CheckEvent (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak);\r
1350\r
a405b86d 1351 CmdLine[BufferSize / sizeof (CHAR16)] = CHAR_NULL;\r
a308e058 1352 Status = RunCommand(CmdLine);\r
0c83ac57 1353 }\r
a405b86d 1354\r
1355 //\r
1356 // Done with this command\r
1357 //\r
594e780b 1358 RestoreBufferList(&OldBufferList);\r
a405b86d 1359 FreePool (CmdLine);\r
1360 return Status;\r
1361}\r
1362\r
1363/**\r
1364 Add a buffer to the Buffer To Free List for safely returning buffers to other\r
1365 places without risking letting them modify internal shell information.\r
1366\r
1367 @param Buffer Something to pass to FreePool when the shell is exiting.\r
1368**/\r
1369VOID*\r
b2c036a7 1370AddBufferToFreeList (\r
a405b86d 1371 VOID *Buffer\r
1372 )\r
1373{\r
1374 BUFFER_LIST *BufferListEntry;\r
1375\r
1376 if (Buffer == NULL) {\r
1377 return (NULL);\r
1378 }\r
1379\r
b2c036a7
RN
1380 BufferListEntry = AllocateZeroPool (sizeof (BUFFER_LIST));\r
1381 if (BufferListEntry == NULL) {\r
1382 return NULL;\r
1383 }\r
1384\r
a405b86d 1385 BufferListEntry->Buffer = Buffer;\r
b2c036a7 1386 InsertTailList (&ShellInfoObject.BufferToFreeList.Link, &BufferListEntry->Link);\r
a405b86d 1387 return (Buffer);\r
1388}\r
1389\r
14810d6b
QS
1390\r
1391/**\r
ba0014b9 1392 Create a new buffer list and stores the old one to OldBufferList\r
14810d6b
QS
1393\r
1394 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.\r
1395**/\r
1396VOID\r
1397SaveBufferList (\r
1398 OUT LIST_ENTRY *OldBufferList\r
1399 )\r
1400{\r
1401 CopyMem (OldBufferList, &ShellInfoObject.BufferToFreeList.Link, sizeof (LIST_ENTRY));\r
1402 InitializeListHead (&ShellInfoObject.BufferToFreeList.Link);\r
1403}\r
1404\r
1405/**\r
1406 Restore previous nodes into BufferToFreeList .\r
1407\r
1408 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.\r
1409**/\r
1410VOID\r
1411RestoreBufferList (\r
1412 IN OUT LIST_ENTRY *OldBufferList\r
1413 )\r
1414{\r
1415 FreeBufferList (&ShellInfoObject.BufferToFreeList);\r
1416 CopyMem (&ShellInfoObject.BufferToFreeList.Link, OldBufferList, sizeof (LIST_ENTRY));\r
1417}\r
1418\r
1419\r
a405b86d 1420/**\r
1421 Add a buffer to the Line History List\r
1422\r
1423 @param Buffer The line buffer to add.\r
1424**/\r
1425VOID\r
a405b86d 1426AddLineToCommandHistory(\r
1427 IN CONST CHAR16 *Buffer\r
1428 )\r
1429{\r
1430 BUFFER_LIST *Node;\r
a4f138a4
QS
1431 BUFFER_LIST *Walker;\r
1432 UINT16 MaxHistoryCmdCount;\r
1433 UINT16 Count;\r
dcbdb8bf 1434\r
a4f138a4
QS
1435 Count = 0;\r
1436 MaxHistoryCmdCount = PcdGet16(PcdShellMaxHistoryCommandCount);\r
ba0014b9 1437\r
a4f138a4
QS
1438 if (MaxHistoryCmdCount == 0) {\r
1439 return ;\r
1440 }\r
1441\r
a405b86d 1442\r
1443 Node = AllocateZeroPool(sizeof(BUFFER_LIST));\r
b2c036a7
RN
1444 if (Node == NULL) {\r
1445 return;\r
1446 }\r
1447\r
1448 Node->Buffer = AllocateCopyPool (StrSize (Buffer), Buffer);\r
1449 if (Node->Buffer == NULL) {\r
1450 FreePool (Node);\r
1451 return;\r
1452 }\r
a405b86d 1453\r
a4f138a4
QS
1454 for ( Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link)\r
1455 ; !IsNull(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link)\r
1456 ; Walker = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link)\r
1457 ){\r
1458 Count++;\r
1459 }\r
1460 if (Count < MaxHistoryCmdCount){\r
1461 InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);\r
1462 } else {\r
1463 Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link);\r
1464 RemoveEntryList(&Walker->Link);\r
1465 if (Walker->Buffer != NULL) {\r
1466 FreePool(Walker->Buffer);\r
1467 }\r
1468 FreePool(Walker);\r
1469 InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);\r
1470 }\r
a405b86d 1471}\r
1472\r
1473/**\r
1474 Checks if a string is an alias for another command. If yes, then it replaces the alias name\r
1475 with the correct command name.\r
1476\r
4ff7e37b
ED
1477 @param[in, out] CommandString Upon entry the potential alias. Upon return the\r
1478 command name if it was an alias. If it was not\r
1479 an alias it will be unchanged. This function may\r
1480 change the buffer to fit the command name.\r
a405b86d 1481\r
1482 @retval EFI_SUCCESS The name was changed.\r
1483 @retval EFI_SUCCESS The name was not an alias.\r
1484 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
1485**/\r
1486EFI_STATUS\r
a405b86d 1487ShellConvertAlias(\r
1488 IN OUT CHAR16 **CommandString\r
1489 )\r
1490{\r
1491 CONST CHAR16 *NewString;\r
1492\r
1493 NewString = ShellInfoObject.NewEfiShellProtocol->GetAlias(*CommandString, NULL);\r
1494 if (NewString == NULL) {\r
1495 return (EFI_SUCCESS);\r
1496 }\r
1497 FreePool(*CommandString);\r
7f79b01e 1498 *CommandString = AllocateCopyPool(StrSize(NewString), NewString);\r
a405b86d 1499 if (*CommandString == NULL) {\r
1500 return (EFI_OUT_OF_RESOURCES);\r
1501 }\r
a405b86d 1502 return (EFI_SUCCESS);\r
1503}\r
1504\r
d5b5440b
JC
1505/**\r
1506 This function will eliminate unreplaced (and therefore non-found) environment variables.\r
1507\r
1508 @param[in,out] CmdLine The command line to update.\r
1509**/\r
1510EFI_STATUS\r
d5b5440b
JC
1511StripUnreplacedEnvironmentVariables(\r
1512 IN OUT CHAR16 *CmdLine\r
1513 )\r
1514{\r
1515 CHAR16 *FirstPercent;\r
1516 CHAR16 *FirstQuote;\r
1517 CHAR16 *SecondPercent;\r
1518 CHAR16 *SecondQuote;\r
1519 CHAR16 *CurrentLocator;\r
1520\r
1521 for (CurrentLocator = CmdLine ; CurrentLocator != NULL ; ) {\r
a95cf8f0
JC
1522 FirstQuote = FindNextInstance(CurrentLocator, L"\"", TRUE);\r
1523 FirstPercent = FindNextInstance(CurrentLocator, L"%", TRUE);\r
1524 SecondPercent = FirstPercent!=NULL?FindNextInstance(FirstPercent+1, L"%", TRUE):NULL;\r
d5b5440b
JC
1525 if (FirstPercent == NULL || SecondPercent == NULL) {\r
1526 //\r
d08a5464 1527 // If we ever don't have 2 % we are done.\r
d5b5440b
JC
1528 //\r
1529 break;\r
1530 }\r
1531\r
fdd52bde 1532 if (FirstQuote!= NULL && FirstQuote < FirstPercent) {\r
a95cf8f0 1533 SecondQuote = FindNextInstance(FirstQuote+1, L"\"", TRUE);\r
d5b5440b
JC
1534 //\r
1535 // Quote is first found\r
1536 //\r
a95cf8f0 1537\r
d5b5440b
JC
1538 if (SecondQuote < FirstPercent) {\r
1539 //\r
1540 // restart after the pair of "\r
1541 //\r
1542 CurrentLocator = SecondQuote + 1;\r
1543 } else /* FirstPercent < SecondQuote */{\r
1544 //\r
1545 // Restart on the first percent\r
1546 //\r
1547 CurrentLocator = FirstPercent;\r
1548 }\r
1549 continue;\r
1550 }\r
ba0014b9 1551\r
fe8ec3dd 1552 if (FirstQuote == NULL || SecondPercent < FirstQuote) {\r
42435671
QS
1553 if (IsValidEnvironmentVariableName(FirstPercent, SecondPercent)) {\r
1554 //\r
1555 // We need to remove from FirstPercent to SecondPercent\r
1556 //\r
1557 CopyMem(FirstPercent, SecondPercent + 1, StrSize(SecondPercent + 1));\r
1558 //\r
d08a5464 1559 // don't need to update the locator. both % characters are gone.\r
42435671
QS
1560 //\r
1561 } else {\r
1562 CurrentLocator = SecondPercent + 1;\r
1563 }\r
d5b5440b
JC
1564 continue;\r
1565 }\r
d5b5440b
JC
1566 CurrentLocator = FirstQuote;\r
1567 }\r
1568 return (EFI_SUCCESS);\r
1569}\r
1570\r
a405b86d 1571/**\r
1572 Function allocates a new command line and replaces all instances of environment\r
1573 variable names that are correctly preset to their values.\r
1574\r
1575 If the return value is not NULL the memory must be caller freed.\r
1576\r
1577 @param[in] OriginalCommandLine The original command line\r
1578\r
d08a5464 1579 @retval NULL An error occurred.\r
a405b86d 1580 @return The new command line with no environment variables present.\r
1581**/\r
1582CHAR16*\r
a405b86d 1583ShellConvertVariables (\r
1584 IN CONST CHAR16 *OriginalCommandLine\r
1585 )\r
1586{\r
1587 CONST CHAR16 *MasterEnvList;\r
1588 UINTN NewSize;\r
1589 CHAR16 *NewCommandLine1;\r
1590 CHAR16 *NewCommandLine2;\r
1591 CHAR16 *Temp;\r
1592 UINTN ItemSize;\r
1593 CHAR16 *ItemTemp;\r
1594 SCRIPT_FILE *CurrentScriptFile;\r
1595 ALIAS_LIST *AliasListNode;\r
1596\r
1597 ASSERT(OriginalCommandLine != NULL);\r
1598\r
1599 ItemSize = 0;\r
1600 NewSize = StrSize(OriginalCommandLine);\r
1601 CurrentScriptFile = ShellCommandGetCurrentScriptFile();\r
1602 Temp = NULL;\r
1603\r
1604 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?\r
1605\r
1606 //\r
1607 // calculate the size required for the post-conversion string...\r
1608 //\r
1609 if (CurrentScriptFile != NULL) {\r
1610 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)\r
1611 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1612 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1613 ){\r
1614 for (Temp = StrStr(OriginalCommandLine, AliasListNode->Alias)\r
1615 ; Temp != NULL\r
1616 ; Temp = StrStr(Temp+1, AliasListNode->Alias)\r
1617 ){\r
1618 //\r
d08a5464 1619 // we need a preceding and if there is space no ^ preceding (if no space ignore)\r
a405b86d 1620 //\r
1621 if ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2)) {\r
1622 NewSize += StrSize(AliasListNode->CommandString);\r
1623 }\r
1624 }\r
1625 }\r
1626 }\r
1627\r
1628 for (MasterEnvList = EfiShellGetEnv(NULL)\r
1629 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL\r
1630 ; MasterEnvList += StrLen(MasterEnvList) + 1\r
1631 ){\r
1632 if (StrSize(MasterEnvList) > ItemSize) {\r
1633 ItemSize = StrSize(MasterEnvList);\r
1634 }\r
1635 for (Temp = StrStr(OriginalCommandLine, MasterEnvList)\r
1636 ; Temp != NULL\r
1637 ; Temp = StrStr(Temp+1, MasterEnvList)\r
1638 ){\r
1639 //\r
d08a5464 1640 // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)\r
a405b86d 1641 //\r
1642 if (*(Temp-1) == L'%' && *(Temp+StrLen(MasterEnvList)) == L'%' &&\r
1643 ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2))) {\r
1644 NewSize+=StrSize(EfiShellGetEnv(MasterEnvList));\r
1645 }\r
1646 }\r
1647 }\r
1648\r
a405b86d 1649 //\r
1650 // now do the replacements...\r
1651 //\r
2a6ede28 1652 NewCommandLine1 = AllocateZeroPool (NewSize);\r
a405b86d 1653 NewCommandLine2 = AllocateZeroPool(NewSize);\r
1654 ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16)));\r
3c865f20 1655 if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) {\r
1656 SHELL_FREE_NON_NULL(NewCommandLine1);\r
1657 SHELL_FREE_NON_NULL(NewCommandLine2);\r
1658 SHELL_FREE_NON_NULL(ItemTemp);\r
1659 return (NULL);\r
1660 }\r
2a6ede28
JW
1661 CopyMem (NewCommandLine1, OriginalCommandLine, StrSize (OriginalCommandLine));\r
1662\r
a405b86d 1663 for (MasterEnvList = EfiShellGetEnv(NULL)\r
7f79b01e 1664 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL\r
a405b86d 1665 ; MasterEnvList += StrLen(MasterEnvList) + 1\r
1666 ){\r
ba0014b9
LG
1667 StrCpyS( ItemTemp,\r
1668 ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)),\r
e75390f0
QS
1669 L"%"\r
1670 );\r
ba0014b9
LG
1671 StrCatS( ItemTemp,\r
1672 ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)),\r
e75390f0
QS
1673 MasterEnvList\r
1674 );\r
ba0014b9
LG
1675 StrCatS( ItemTemp,\r
1676 ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)),\r
e75390f0
QS
1677 L"%"\r
1678 );\r
a405b86d 1679 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, ItemTemp, EfiShellGetEnv(MasterEnvList), TRUE, FALSE);\r
e75390f0 1680 StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);\r
a405b86d 1681 }\r
1682 if (CurrentScriptFile != NULL) {\r
1683 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)\r
1684 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1685 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1686 ){\r
1687 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE);\r
e75390f0 1688 StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);\r
a405b86d 1689 }\r
1690 }\r
1691\r
fdd52bde 1692 //\r
d08a5464 1693 // Remove non-existent environment variables\r
fdd52bde
JC
1694 //\r
1695 StripUnreplacedEnvironmentVariables(NewCommandLine1);\r
1696\r
df07baea
JC
1697 //\r
1698 // Now cleanup any straggler intentionally ignored "%" characters\r
1699 //\r
1700 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE);\r
e75390f0 1701 StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);\r
ba0014b9 1702\r
a405b86d 1703 FreePool(NewCommandLine2);\r
1704 FreePool(ItemTemp);\r
1705\r
1706 return (NewCommandLine1);\r
1707}\r
1708\r
1709/**\r
1710 Internal function to run a command line with pipe usage.\r
1711\r
1712 @param[in] CmdLine The pointer to the command line.\r
1713 @param[in] StdIn The pointer to the Standard input.\r
1714 @param[in] StdOut The pointer to the Standard output.\r
1715\r
1716 @retval EFI_SUCCESS The split command is executed successfully.\r
1717 @retval other Some error occurs when executing the split command.\r
1718**/\r
1719EFI_STATUS\r
a405b86d 1720RunSplitCommand(\r
1721 IN CONST CHAR16 *CmdLine,\r
1bd0bf15
LE
1722 IN SHELL_FILE_HANDLE StdIn,\r
1723 IN SHELL_FILE_HANDLE StdOut\r
a405b86d 1724 )\r
1725{\r
1726 EFI_STATUS Status;\r
1727 CHAR16 *NextCommandLine;\r
1728 CHAR16 *OurCommandLine;\r
1729 UINTN Size1;\r
1730 UINTN Size2;\r
1731 SPLIT_LIST *Split;\r
1bd0bf15 1732 SHELL_FILE_HANDLE TempFileHandle;\r
a405b86d 1733 BOOLEAN Unicode;\r
1734\r
1735 ASSERT(StdOut == NULL);\r
1736\r
1737 ASSERT(StrStr(CmdLine, L"|") != NULL);\r
1738\r
1739 Status = EFI_SUCCESS;\r
1740 NextCommandLine = NULL;\r
1741 OurCommandLine = NULL;\r
1742 Size1 = 0;\r
1743 Size2 = 0;\r
1744\r
1745 NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0);\r
1746 OurCommandLine = StrnCatGrow(&OurCommandLine , &Size2, CmdLine , StrStr(CmdLine, L"|") - CmdLine);\r
532691c8 1747\r
1748 if (NextCommandLine == NULL || OurCommandLine == NULL) {\r
1749 SHELL_FREE_NON_NULL(OurCommandLine);\r
1750 SHELL_FREE_NON_NULL(NextCommandLine);\r
1751 return (EFI_OUT_OF_RESOURCES);\r
19216573 1752 } else if (StrStr(OurCommandLine, L"|") != NULL || Size1 == 0 || Size2 == 0) {\r
1753 SHELL_FREE_NON_NULL(OurCommandLine);\r
1754 SHELL_FREE_NON_NULL(NextCommandLine);\r
1755 return (EFI_INVALID_PARAMETER);\r
a5382ce0
QS
1756 } else if (NextCommandLine[0] == L'a' &&\r
1757 (NextCommandLine[1] == L' ' || NextCommandLine[1] == CHAR_NULL)\r
1758 ){\r
a405b86d 1759 CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));\r
a5382ce0
QS
1760 while (NextCommandLine[0] == L' ') {\r
1761 CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));\r
1762 }\r
1763 if (NextCommandLine[0] == CHAR_NULL) {\r
1764 SHELL_FREE_NON_NULL(OurCommandLine);\r
1765 SHELL_FREE_NON_NULL(NextCommandLine);\r
1766 return (EFI_INVALID_PARAMETER);\r
1767 }\r
a405b86d 1768 Unicode = FALSE;\r
1769 } else {\r
1770 Unicode = TRUE;\r
1771 }\r
1772\r
1773\r
1774 //\r
1775 // make a SPLIT_LIST item and add to list\r
1776 //\r
1777 Split = AllocateZeroPool(sizeof(SPLIT_LIST));\r
b2c036a7
RN
1778 if (Split == NULL) {\r
1779 return EFI_OUT_OF_RESOURCES;\r
1780 }\r
a405b86d 1781 Split->SplitStdIn = StdIn;\r
1782 Split->SplitStdOut = ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode), NULL);\r
1783 ASSERT(Split->SplitStdOut != NULL);\r
1784 InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link);\r
1785\r
a308e058 1786 Status = RunCommand(OurCommandLine);\r
a405b86d 1787\r
1788 //\r
1789 // move the output from the first to the in to the second.\r
1790 //\r
1791 TempFileHandle = Split->SplitStdOut;\r
1792 if (Split->SplitStdIn == StdIn) {\r
1793 Split->SplitStdOut = NULL;\r
1794 } else {\r
1795 Split->SplitStdOut = Split->SplitStdIn;\r
1796 }\r
1797 Split->SplitStdIn = TempFileHandle;\r
1bd0bf15 1798 ShellInfoObject.NewEfiShellProtocol->SetFilePosition (Split->SplitStdIn, 0);\r
a405b86d 1799\r
1800 if (!EFI_ERROR(Status)) {\r
a308e058 1801 Status = RunCommand(NextCommandLine);\r
a405b86d 1802 }\r
1803\r
1804 //\r
1805 // remove the top level from the ScriptList\r
1806 //\r
1807 ASSERT((SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link) == Split);\r
1808 RemoveEntryList(&Split->Link);\r
1809\r
1810 //\r
1811 // Note that the original StdIn is now the StdOut...\r
1812 //\r
bd3fc813 1813 if (Split->SplitStdOut != NULL) {\r
1bd0bf15 1814 ShellInfoObject.NewEfiShellProtocol->CloseFile (Split->SplitStdOut);\r
a405b86d 1815 }\r
1816 if (Split->SplitStdIn != NULL) {\r
1bd0bf15 1817 ShellInfoObject.NewEfiShellProtocol->CloseFile (Split->SplitStdIn);\r
a405b86d 1818 }\r
1819\r
1820 FreePool(Split);\r
1821 FreePool(NextCommandLine);\r
1822 FreePool(OurCommandLine);\r
1823\r
1824 return (Status);\r
1825}\r
1826\r
1ef61d03 1827/**\r
ba0014b9 1828 Take the original command line, substitute any variables, free\r
51429264 1829 the original string, return the modified copy.\r
1ef61d03 1830\r
51429264 1831 @param[in] CmdLine pointer to the command line to update.\r
1ef61d03 1832\r
51429264
SQ
1833 @retval EFI_SUCCESS the function was successful.\r
1834 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r
1ef61d03
JC
1835**/\r
1836EFI_STATUS\r
1ef61d03
JC
1837ShellSubstituteVariables(\r
1838 IN CHAR16 **CmdLine\r
1839 )\r
1840{\r
1841 CHAR16 *NewCmdLine;\r
1842 NewCmdLine = ShellConvertVariables(*CmdLine);\r
1843 SHELL_FREE_NON_NULL(*CmdLine);\r
1844 if (NewCmdLine == NULL) {\r
1845 return (EFI_OUT_OF_RESOURCES);\r
1846 }\r
1847 *CmdLine = NewCmdLine;\r
1848 return (EFI_SUCCESS);\r
1849}\r
1850\r
ca53c0af 1851/**\r
ba0014b9 1852 Take the original command line, substitute any alias in the first group of space delimited characters, free\r
51429264 1853 the original string, return the modified copy.\r
ca53c0af 1854\r
51429264 1855 @param[in] CmdLine pointer to the command line to update.\r
ca53c0af 1856\r
51429264
SQ
1857 @retval EFI_SUCCESS the function was successful.\r
1858 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r
ca53c0af
JC
1859**/\r
1860EFI_STATUS\r
ca53c0af
JC
1861ShellSubstituteAliases(\r
1862 IN CHAR16 **CmdLine\r
1863 )\r
1864{\r
1865 CHAR16 *NewCmdLine;\r
1866 CHAR16 *CommandName;\r
1867 EFI_STATUS Status;\r
1868 UINTN PostAliasSize;\r
1869 ASSERT(CmdLine != NULL);\r
1870 ASSERT(*CmdLine!= NULL);\r
1871\r
1872\r
1873 CommandName = NULL;\r
1874 if (StrStr((*CmdLine), L" ") == NULL){\r
1875 StrnCatGrow(&CommandName, NULL, (*CmdLine), 0);\r
1876 } else {\r
1877 StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine));\r
1878 }\r
1879\r
1880 //\r
1881 // This cannot happen 'inline' since the CmdLine can need extra space.\r
1882 //\r
1883 NewCmdLine = NULL;\r
1884 if (!ShellCommandIsCommandOnList(CommandName)) {\r
1885 //\r
1886 // Convert via alias\r
1887 //\r
1888 Status = ShellConvertAlias(&CommandName);\r
1889 if (EFI_ERROR(Status)){\r
1890 return (Status);\r
1891 }\r
1892 PostAliasSize = 0;\r
1893 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0);\r
1894 if (NewCmdLine == NULL) {\r
1895 SHELL_FREE_NON_NULL(CommandName);\r
1896 SHELL_FREE_NON_NULL(*CmdLine);\r
1897 return (EFI_OUT_OF_RESOURCES);\r
1898 }\r
1899 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0);\r
1900 if (NewCmdLine == NULL) {\r
1901 SHELL_FREE_NON_NULL(CommandName);\r
1902 SHELL_FREE_NON_NULL(*CmdLine);\r
1903 return (EFI_OUT_OF_RESOURCES);\r
1904 }\r
1905 } else {\r
1906 NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0);\r
1907 }\r
1908\r
1909 SHELL_FREE_NON_NULL(*CmdLine);\r
1910 SHELL_FREE_NON_NULL(CommandName);\r
ba0014b9 1911\r
ca53c0af
JC
1912 //\r
1913 // re-assign the passed in double pointer to point to our newly allocated buffer\r
1914 //\r
1915 *CmdLine = NewCmdLine;\r
1916\r
1917 return (EFI_SUCCESS);\r
1918}\r
1919\r
6ba2921d
JC
1920/**\r
1921 Takes the Argv[0] part of the command line and determine the meaning of it.\r
e7831c90 1922\r
51429264 1923 @param[in] CmdName pointer to the command line to update.\r
ba0014b9 1924\r
51429264
SQ
1925 @retval Internal_Command The name is an internal command.\r
1926 @retval File_Sys_Change the name is a file system change.\r
1927 @retval Script_File_Name the name is a NSH script file.\r
1928 @retval Unknown_Invalid the name is unknown.\r
1929 @retval Efi_Application the name is an application (.EFI).\r
6ba2921d
JC
1930**/\r
1931SHELL_OPERATION_TYPES\r
6ba2921d
JC
1932GetOperationType(\r
1933 IN CONST CHAR16 *CmdName\r
1934 )\r
1935{\r
1936 CHAR16* FileWithPath;\r
1937 CONST CHAR16* TempLocation;\r
1938 CONST CHAR16* TempLocation2;\r
1939\r
1940 FileWithPath = NULL;\r
1941 //\r
1942 // test for an internal command.\r
1943 //\r
1944 if (ShellCommandIsCommandOnList(CmdName)) {\r
51429264 1945 return (Internal_Command);\r
6ba2921d
JC
1946 }\r
1947\r
1948 //\r
fc4c7b30 1949 // Test for file system change request. anything ending with first : and cant have spaces.\r
6ba2921d
JC
1950 //\r
1951 if (CmdName[(StrLen(CmdName)-1)] == L':') {\r
ba0014b9 1952 if ( StrStr(CmdName, L" ") != NULL\r
fc4c7b30
JC
1953 || StrLen(StrStr(CmdName, L":")) > 1\r
1954 ) {\r
51429264 1955 return (Unknown_Invalid);\r
6ba2921d 1956 }\r
51429264 1957 return (File_Sys_Change);\r
6ba2921d
JC
1958 }\r
1959\r
1960 //\r
1961 // Test for a file\r
1962 //\r
1963 if ((FileWithPath = ShellFindFilePathEx(CmdName, mExecutableExtensions)) != NULL) {\r
1964 //\r
1965 // See if that file has a script file extension\r
1966 //\r
1967 if (StrLen(FileWithPath) > 4) {\r
1968 TempLocation = FileWithPath+StrLen(FileWithPath)-4;\r
1969 TempLocation2 = mScriptExtension;\r
1970 if (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0) {\r
1971 SHELL_FREE_NON_NULL(FileWithPath);\r
51429264 1972 return (Script_File_Name);\r
6ba2921d
JC
1973 }\r
1974 }\r
1975\r
1976 //\r
1977 // Was a file, but not a script. we treat this as an application.\r
1978 //\r
1979 SHELL_FREE_NON_NULL(FileWithPath);\r
51429264 1980 return (Efi_Application);\r
6ba2921d 1981 }\r
ba0014b9 1982\r
6ba2921d
JC
1983 SHELL_FREE_NON_NULL(FileWithPath);\r
1984 //\r
1985 // No clue what this is... return invalid flag...\r
1986 //\r
51429264 1987 return (Unknown_Invalid);\r
6ba2921d
JC
1988}\r
1989\r
de4ebdcf
SQ
1990/**\r
1991 Determine if the first item in a command line is valid.\r
1992\r
1993 @param[in] CmdLine The command line to parse.\r
1994\r
1995 @retval EFI_SUCCESS The item is valid.\r
1996 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
1997 @retval EFI_NOT_FOUND The operation type is unknown or invalid.\r
1998**/\r
ba0014b9 1999EFI_STATUS\r
6bd64463
JC
2000IsValidSplit(\r
2001 IN CONST CHAR16 *CmdLine\r
2002 )\r
2003{\r
2004 CHAR16 *Temp;\r
2005 CHAR16 *FirstParameter;\r
2006 CHAR16 *TempWalker;\r
2007 EFI_STATUS Status;\r
2008\r
2009 Temp = NULL;\r
2010\r
2011 Temp = StrnCatGrow(&Temp, NULL, CmdLine, 0);\r
2012 if (Temp == NULL) {\r
2013 return (EFI_OUT_OF_RESOURCES);\r
2014 }\r
2015\r
2016 FirstParameter = StrStr(Temp, L"|");\r
2017 if (FirstParameter != NULL) {\r
2018 *FirstParameter = CHAR_NULL;\r
2019 }\r
2020\r
2021 FirstParameter = NULL;\r
2022\r
2023 //\r
2024 // Process the command line\r
2025 //\r
2026 Status = ProcessCommandLineToFinal(&Temp);\r
2027\r
2028 if (!EFI_ERROR(Status)) {\r
2029 FirstParameter = AllocateZeroPool(StrSize(CmdLine));\r
2030 if (FirstParameter == NULL) {\r
2031 SHELL_FREE_NON_NULL(Temp);\r
2032 return (EFI_OUT_OF_RESOURCES);\r
2033 }\r
2034 TempWalker = (CHAR16*)Temp;\r
1219c85d 2035 if (!EFI_ERROR(GetNextParameter(&TempWalker, &FirstParameter, StrSize(CmdLine), TRUE))) {\r
14030c5c
JC
2036 if (GetOperationType(FirstParameter) == Unknown_Invalid) {\r
2037 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
2038 SetLastError(SHELL_NOT_FOUND);\r
2039 Status = EFI_NOT_FOUND;\r
2040 }\r
6bd64463
JC
2041 }\r
2042 }\r
2043\r
2044 SHELL_FREE_NON_NULL(Temp);\r
2045 SHELL_FREE_NON_NULL(FirstParameter);\r
2046 return Status;\r
2047}\r
2048\r
2049/**\r
51429264 2050 Determine if a command line contains with a split contains only valid commands.\r
6bd64463
JC
2051\r
2052 @param[in] CmdLine The command line to parse.\r
2053\r
2054 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.\r
2055 @retval EFI_ABORTED CmdLine has at least one invalid command or application.\r
2056**/\r
2057EFI_STATUS\r
6bd64463
JC
2058VerifySplit(\r
2059 IN CONST CHAR16 *CmdLine\r
2060 )\r
2061{\r
2062 CONST CHAR16 *TempSpot;\r
2063 EFI_STATUS Status;\r
2064\r
2065 //\r
a5382ce0 2066 // If this was the only item, then get out\r
6bd64463 2067 //\r
a5382ce0
QS
2068 if (!ContainsSplit(CmdLine)) {\r
2069 return (EFI_SUCCESS);\r
6bd64463
JC
2070 }\r
2071\r
2072 //\r
a5382ce0 2073 // Verify up to the pipe or end character\r
6bd64463 2074 //\r
a5382ce0
QS
2075 Status = IsValidSplit(CmdLine);\r
2076 if (EFI_ERROR(Status)) {\r
2077 return (Status);\r
6bd64463
JC
2078 }\r
2079\r
2080 //\r
2081 // recurse to verify the next item\r
2082 //\r
1219c85d 2083 TempSpot = FindFirstCharacter(CmdLine, L"|", L'^') + 1;\r
ba0014b9 2084 if (*TempSpot == L'a' &&\r
a5382ce0
QS
2085 (*(TempSpot + 1) == L' ' || *(TempSpot + 1) == CHAR_NULL)\r
2086 ) {\r
2087 // If it's an ASCII pipe '|a'\r
2088 TempSpot += 1;\r
2089 }\r
ba0014b9 2090\r
6bd64463
JC
2091 return (VerifySplit(TempSpot));\r
2092}\r
2093\r
680db511 2094/**\r
e7831c90
JC
2095 Process a split based operation.\r
2096\r
a308e058 2097 @param[in] CmdLine pointer to the command line to process\r
e7831c90
JC
2098\r
2099 @retval EFI_SUCCESS The operation was successful\r
d08a5464 2100 @return an error occurred.\r
680db511
JC
2101**/\r
2102EFI_STATUS\r
680db511 2103ProcessNewSplitCommandLine(\r
a308e058 2104 IN CONST CHAR16 *CmdLine\r
680db511
JC
2105 )\r
2106{\r
2107 SPLIT_LIST *Split;\r
2108 EFI_STATUS Status;\r
2109\r
6bd64463
JC
2110 Status = VerifySplit(CmdLine);\r
2111 if (EFI_ERROR(Status)) {\r
2112 return (Status);\r
2113 }\r
2114\r
680db511
JC
2115 Split = NULL;\r
2116\r
2117 //\r
2118 // are we in an existing split???\r
2119 //\r
2120 if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {\r
2121 Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);\r
2122 }\r
2123\r
2124 if (Split == NULL) {\r
a308e058 2125 Status = RunSplitCommand(CmdLine, NULL, NULL);\r
680db511 2126 } else {\r
a308e058 2127 Status = RunSplitCommand(CmdLine, Split->SplitStdIn, Split->SplitStdOut);\r
680db511
JC
2128 }\r
2129 if (EFI_ERROR(Status)) {\r
2130 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CmdLine);\r
2131 }\r
2132 return (Status);\r
2133}\r
daf70584
JC
2134\r
2135/**\r
51429264 2136 Handle a request to change the current file system.\r
daf70584 2137\r
51429264 2138 @param[in] CmdLine The passed in command line.\r
daf70584 2139\r
51429264 2140 @retval EFI_SUCCESS The operation was successful.\r
daf70584
JC
2141**/\r
2142EFI_STATUS\r
daf70584
JC
2143ChangeMappedDrive(\r
2144 IN CONST CHAR16 *CmdLine\r
2145 )\r
2146{\r
2147 EFI_STATUS Status;\r
2148 Status = EFI_SUCCESS;\r
2149\r
2150 //\r
2151 // make sure we are the right operation\r
2152 //\r
2153 ASSERT(CmdLine[(StrLen(CmdLine)-1)] == L':' && StrStr(CmdLine, L" ") == NULL);\r
ba0014b9 2154\r
daf70584
JC
2155 //\r
2156 // Call the protocol API to do the work\r
2157 //\r
2158 Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, CmdLine);\r
2159\r
2160 //\r
2161 // Report any errors\r
2162 //\r
2163 if (EFI_ERROR(Status)) {\r
2164 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, CmdLine);\r
2165 }\r
2166\r
2167 return (Status);\r
2168}\r
2169\r
5a5eb806
JC
2170/**\r
2171 Reprocess the command line to direct all -? to the help command.\r
2172\r
2173 if found, will add "help" as argv[0], and move the rest later.\r
2174\r
806c49db 2175 @param[in,out] CmdLine pointer to the command line to update\r
5a5eb806
JC
2176**/\r
2177EFI_STATUS\r
806c49db
JC
2178DoHelpUpdate(\r
2179 IN OUT CHAR16 **CmdLine\r
5a5eb806
JC
2180 )\r
2181{\r
806c49db
JC
2182 CHAR16 *CurrentParameter;\r
2183 CHAR16 *Walker;\r
806c49db
JC
2184 CHAR16 *NewCommandLine;\r
2185 EFI_STATUS Status;\r
e75390f0 2186 UINTN NewCmdLineSize;\r
806c49db
JC
2187\r
2188 Status = EFI_SUCCESS;\r
2189\r
2190 CurrentParameter = AllocateZeroPool(StrSize(*CmdLine));\r
2191 if (CurrentParameter == NULL) {\r
2192 return (EFI_OUT_OF_RESOURCES);\r
2193 }\r
2194\r
2195 Walker = *CmdLine;\r
2196 while(Walker != NULL && *Walker != CHAR_NULL) {\r
1219c85d 2197 if (!EFI_ERROR(GetNextParameter(&Walker, &CurrentParameter, StrSize(*CmdLine), TRUE))) {\r
14030c5c 2198 if (StrStr(CurrentParameter, L"-?") == CurrentParameter) {\r
a8f98806
JC
2199 CurrentParameter[0] = L' ';\r
2200 CurrentParameter[1] = L' ';\r
e75390f0
QS
2201 NewCmdLineSize = StrSize(L"help ") + StrSize(*CmdLine);\r
2202 NewCommandLine = AllocateZeroPool(NewCmdLineSize);\r
14030c5c
JC
2203 if (NewCommandLine == NULL) {\r
2204 Status = EFI_OUT_OF_RESOURCES;\r
2205 break;\r
2206 }\r
2207\r
2208 //\r
2209 // We know the space is sufficient since we just calculated it.\r
2210 //\r
e75390f0
QS
2211 StrnCpyS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), L"help ", 5);\r
2212 StrnCatS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), *CmdLine, StrLen(*CmdLine));\r
14030c5c
JC
2213 SHELL_FREE_NON_NULL(*CmdLine);\r
2214 *CmdLine = NewCommandLine;\r
806c49db
JC
2215 break;\r
2216 }\r
806c49db
JC
2217 }\r
2218 }\r
2219\r
2220 SHELL_FREE_NON_NULL(CurrentParameter);\r
2221\r
2222 return (Status);\r
2223}\r
2224\r
2225/**\r
51429264 2226 Function to update the shell variable "lasterror".\r
806c49db 2227\r
51429264 2228 @param[in] ErrorCode the error code to put into lasterror.\r
806c49db
JC
2229**/\r
2230EFI_STATUS\r
806c49db 2231SetLastError(\r
6bd64463 2232 IN CONST SHELL_STATUS ErrorCode\r
806c49db
JC
2233 )\r
2234{\r
2235 CHAR16 LeString[19];\r
2236 if (sizeof(EFI_STATUS) == sizeof(UINT64)) {\r
2237 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ErrorCode);\r
2238 } else {\r
2239 UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", ErrorCode);\r
2240 }\r
2241 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r
2242 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r
2243\r
2244 return (EFI_SUCCESS);\r
2245}\r
2246\r
2247/**\r
2248 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.\r
2249\r
2250 @param[in,out] CmdLine pointer to the command line to update\r
2251\r
2252 @retval EFI_SUCCESS The operation was successful\r
2253 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
d08a5464 2254 @return some other error occurred\r
806c49db
JC
2255**/\r
2256EFI_STATUS\r
12a27a6d 2257ProcessCommandLineToFinal(\r
806c49db
JC
2258 IN OUT CHAR16 **CmdLine\r
2259 )\r
2260{\r
2261 EFI_STATUS Status;\r
2262 TrimSpaces(CmdLine);\r
2263\r
2264 Status = ShellSubstituteAliases(CmdLine);\r
2265 if (EFI_ERROR(Status)) {\r
2266 return (Status);\r
2267 }\r
2268\r
2269 TrimSpaces(CmdLine);\r
2270\r
2271 Status = ShellSubstituteVariables(CmdLine);\r
2272 if (EFI_ERROR(Status)) {\r
2273 return (Status);\r
2274 }\r
81cd2f53 2275 ASSERT (*CmdLine != NULL);\r
806c49db
JC
2276\r
2277 TrimSpaces(CmdLine);\r
2278\r
2279 //\r
2280 // update for help parsing\r
2281 //\r
2282 if (StrStr(*CmdLine, L"?") != NULL) {\r
2283 //\r
2284 // This may do nothing if the ? does not indicate help.\r
2285 // Save all the details for in the API below.\r
2286 //\r
2287 Status = DoHelpUpdate(CmdLine);\r
2288 }\r
2289\r
2290 TrimSpaces(CmdLine);\r
2291\r
2292 return (EFI_SUCCESS);\r
2293}\r
2294\r
2295/**\r
2296 Run an internal shell command.\r
2297\r
d08a5464 2298 This API will update the shell's environment since these commands are libraries.\r
ba0014b9 2299\r
806c49db
JC
2300 @param[in] CmdLine the command line to run.\r
2301 @param[in] FirstParameter the first parameter on the command line\r
2302 @param[in] ParamProtocol the shell parameters protocol pointer\r
490ce43d 2303 @param[out] CommandStatus the status from the command line.\r
806c49db
JC
2304\r
2305 @retval EFI_SUCCESS The command was completed.\r
2306 @retval EFI_ABORTED The command's operation was aborted.\r
2307**/\r
2308EFI_STATUS\r
806c49db
JC
2309RunInternalCommand(\r
2310 IN CONST CHAR16 *CmdLine,\r
2311 IN CHAR16 *FirstParameter,\r
490ce43d
QS
2312 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r
2313 OUT EFI_STATUS *CommandStatus\r
806c49db
JC
2314)\r
2315{\r
2316 EFI_STATUS Status;\r
2317 UINTN Argc;\r
2318 CHAR16 **Argv;\r
2319 SHELL_STATUS CommandReturnedStatus;\r
2320 BOOLEAN LastError;\r
4b6b543e 2321 CHAR16 *Walker;\r
ba0014b9 2322 CHAR16 *NewCmdLine;\r
4b6b543e
QS
2323\r
2324 NewCmdLine = AllocateCopyPool (StrSize (CmdLine), CmdLine);\r
2325 if (NewCmdLine == NULL) {\r
2326 return EFI_OUT_OF_RESOURCES;\r
2327 }\r
2328\r
2329 for (Walker = NewCmdLine; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {\r
2330 if (*Walker == L'^' && *(Walker+1) == L'#') {\r
2331 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));\r
2332 }\r
2333 }\r
806c49db 2334\r
5a5eb806 2335 //\r
806c49db 2336 // get the argc and argv updated for internal commands\r
5a5eb806 2337 //\r
d1c275c6 2338 Status = UpdateArgcArgv(ParamProtocol, NewCmdLine, Internal_Command, &Argv, &Argc);\r
806c49db 2339 if (!EFI_ERROR(Status)) {\r
5a5eb806 2340 //\r
806c49db 2341 // Run the internal command.\r
5a5eb806 2342 //\r
806c49db
JC
2343 Status = ShellCommandRunCommandHandler(FirstParameter, &CommandReturnedStatus, &LastError);\r
2344\r
2345 if (!EFI_ERROR(Status)) {\r
490ce43d
QS
2346 if (CommandStatus != NULL) {\r
2347 if (CommandReturnedStatus != SHELL_SUCCESS) {\r
2348 *CommandStatus = (EFI_STATUS)(CommandReturnedStatus | MAX_BIT);\r
2349 } else {\r
2350 *CommandStatus = EFI_SUCCESS;\r
2351 }\r
2352 }\r
2353\r
5a5eb806 2354 //\r
806c49db
JC
2355 // Update last error status.\r
2356 // some commands do not update last error.\r
5a5eb806 2357 //\r
806c49db
JC
2358 if (LastError) {\r
2359 SetLastError(CommandReturnedStatus);\r
5a5eb806 2360 }\r
806c49db
JC
2361\r
2362 //\r
2363 // Pass thru the exitcode from the app.\r
2364 //\r
2365 if (ShellCommandGetExit()) {\r
e3eb7d82
JC
2366 //\r
2367 // An Exit was requested ("exit" command), pass its value up.\r
2368 //\r
806c49db 2369 Status = CommandReturnedStatus;\r
e3eb7d82
JC
2370 } else if (CommandReturnedStatus != SHELL_SUCCESS && IsScriptOnlyCommand(FirstParameter)) {\r
2371 //\r
2372 // Always abort when a script only command fails for any reason\r
2373 //\r
2374 Status = EFI_ABORTED;\r
2375 } else if (ShellCommandGetCurrentScriptFile() != NULL && CommandReturnedStatus == SHELL_ABORTED) {\r
2376 //\r
2377 // Abort when in a script and a command aborted\r
2378 //\r
806c49db 2379 Status = EFI_ABORTED;\r
5a5eb806 2380 }\r
806c49db
JC
2381 }\r
2382 }\r
2383\r
2384 //\r
d08a5464 2385 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.\r
806c49db
JC
2386 // This is safe even if the update API failed. In this case, it may be a no-op.\r
2387 //\r
2388 RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r
2389\r
e3eb7d82 2390 //\r
d08a5464 2391 // If a script is running and the command is not a script only command, then\r
e3eb7d82
JC
2392 // change return value to success so the script won't halt (unless aborted).\r
2393 //\r
2394 // Script only commands have to be able halt the script since the script will\r
2395 // not operate if they are failing.\r
2396 //\r
2397 if ( ShellCommandGetCurrentScriptFile() != NULL\r
2398 && !IsScriptOnlyCommand(FirstParameter)\r
2399 && Status != EFI_ABORTED\r
2400 ) {\r
806c49db
JC
2401 Status = EFI_SUCCESS;\r
2402 }\r
2403\r
4b6b543e 2404 FreePool (NewCmdLine);\r
806c49db
JC
2405 return (Status);\r
2406}\r
2407\r
2408/**\r
2409 Function to run the command or file.\r
2410\r
2411 @param[in] Type the type of operation being run.\r
2412 @param[in] CmdLine the command line to run.\r
2413 @param[in] FirstParameter the first parameter on the command line\r
2414 @param[in] ParamProtocol the shell parameters protocol pointer\r
490ce43d 2415 @param[out] CommandStatus the status from the command line.\r
806c49db
JC
2416\r
2417 @retval EFI_SUCCESS The command was completed.\r
2418 @retval EFI_ABORTED The command's operation was aborted.\r
2419**/\r
2420EFI_STATUS\r
806c49db
JC
2421RunCommandOrFile(\r
2422 IN SHELL_OPERATION_TYPES Type,\r
2423 IN CONST CHAR16 *CmdLine,\r
2424 IN CHAR16 *FirstParameter,\r
490ce43d
QS
2425 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r
2426 OUT EFI_STATUS *CommandStatus\r
806c49db
JC
2427)\r
2428{\r
2429 EFI_STATUS Status;\r
cd39fe08 2430 EFI_STATUS StartStatus;\r
806c49db
JC
2431 CHAR16 *CommandWithPath;\r
2432 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
5223c121 2433 SHELL_STATUS CalleeExitStatus;\r
806c49db
JC
2434\r
2435 Status = EFI_SUCCESS;\r
2436 CommandWithPath = NULL;\r
2437 DevPath = NULL;\r
71c49eaf 2438 CalleeExitStatus = SHELL_INVALID_PARAMETER;\r
806c49db
JC
2439\r
2440 switch (Type) {\r
51429264 2441 case Internal_Command:\r
490ce43d 2442 Status = RunInternalCommand(CmdLine, FirstParameter, ParamProtocol, CommandStatus);\r
806c49db 2443 break;\r
51429264
SQ
2444 case Script_File_Name:\r
2445 case Efi_Application:\r
806c49db
JC
2446 //\r
2447 // Process a fully qualified path\r
2448 //\r
2449 if (StrStr(FirstParameter, L":") != NULL) {\r
2450 ASSERT (CommandWithPath == NULL);\r
2451 if (ShellIsFile(FirstParameter) == EFI_SUCCESS) {\r
2452 CommandWithPath = StrnCatGrow(&CommandWithPath, NULL, FirstParameter, 0);\r
2453 }\r
2454 }\r
2455\r
2456 //\r
2457 // Process a relative path and also check in the path environment variable\r
2458 //\r
2459 if (CommandWithPath == NULL) {\r
2460 CommandWithPath = ShellFindFilePathEx(FirstParameter, mExecutableExtensions);\r
2461 }\r
2462\r
2463 //\r
2464 // This should be impossible now.\r
2465 //\r
2466 ASSERT(CommandWithPath != NULL);\r
2467\r
2468 //\r
2469 // Make sure that path is not just a directory (or not found)\r
2470 //\r
2471 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath))) {\r
2472 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
6bd64463 2473 SetLastError(SHELL_NOT_FOUND);\r
806c49db
JC
2474 }\r
2475 switch (Type) {\r
51429264 2476 case Script_File_Name:\r
a308e058 2477 Status = RunScriptFile (CommandWithPath, NULL, CmdLine, ParamProtocol);\r
806c49db 2478 break;\r
51429264 2479 case Efi_Application:\r
806c49db
JC
2480 //\r
2481 // Get the device path of the application image\r
2482 //\r
2483 DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CommandWithPath);\r
2484 if (DevPath == NULL){\r
2485 Status = EFI_OUT_OF_RESOURCES;\r
2486 break;\r
2487 }\r
2488\r
2489 //\r
2490 // Execute the device path\r
2491 //\r
2492 Status = InternalShellExecuteDevicePath(\r
2493 &gImageHandle,\r
2494 DevPath,\r
2495 CmdLine,\r
2496 NULL,\r
a308e058 2497 &StartStatus\r
806c49db
JC
2498 );\r
2499\r
2500 SHELL_FREE_NON_NULL(DevPath);\r
2501\r
71c49eaf
SQ
2502 if(EFI_ERROR (Status)) {\r
2503 CalleeExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
2504 } else {\r
cd39fe08 2505 CalleeExitStatus = (SHELL_STATUS) StartStatus;\r
71c49eaf
SQ
2506 }\r
2507\r
490ce43d
QS
2508 if (CommandStatus != NULL) {\r
2509 *CommandStatus = CalleeExitStatus;\r
2510 }\r
2511\r
806c49db
JC
2512 //\r
2513 // Update last error status.\r
2514 //\r
5223c121 2515 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS\r
71c49eaf 2516 SetLastError(CalleeExitStatus);\r
806c49db 2517 break;\r
6fa0da7d
LE
2518 default:\r
2519 //\r
2520 // Do nothing.\r
2521 //\r
2522 break;\r
5a5eb806
JC
2523 }\r
2524 break;\r
6fa0da7d
LE
2525 default:\r
2526 //\r
2527 // Do nothing.\r
2528 //\r
2529 break;\r
5a5eb806 2530 }\r
806c49db
JC
2531\r
2532 SHELL_FREE_NON_NULL(CommandWithPath);\r
2533\r
2534 return (Status);\r
2535}\r
2536\r
2537/**\r
2538 Function to setup StdIn, StdErr, StdOut, and then run the command or file.\r
2539\r
2540 @param[in] Type the type of operation being run.\r
2541 @param[in] CmdLine the command line to run.\r
2542 @param[in] FirstParameter the first parameter on the command line.\r
2543 @param[in] ParamProtocol the shell parameters protocol pointer\r
490ce43d 2544 @param[out] CommandStatus the status from the command line.\r
806c49db
JC
2545\r
2546 @retval EFI_SUCCESS The command was completed.\r
2547 @retval EFI_ABORTED The command's operation was aborted.\r
2548**/\r
2549EFI_STATUS\r
806c49db 2550SetupAndRunCommandOrFile(\r
490ce43d
QS
2551 IN SHELL_OPERATION_TYPES Type,\r
2552 IN CHAR16 *CmdLine,\r
2553 IN CHAR16 *FirstParameter,\r
2554 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r
dcbdb8bf 2555 OUT EFI_STATUS *CommandStatus\r
806c49db
JC
2556)\r
2557{\r
2558 EFI_STATUS Status;\r
2559 SHELL_FILE_HANDLE OriginalStdIn;\r
2560 SHELL_FILE_HANDLE OriginalStdOut;\r
2561 SHELL_FILE_HANDLE OriginalStdErr;\r
2562 SYSTEM_TABLE_INFO OriginalSystemTableInfo;\r
08222019 2563 CONST SCRIPT_FILE *ConstScriptFile;\r
806c49db
JC
2564\r
2565 //\r
2566 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII\r
2567 //\r
2568 Status = UpdateStdInStdOutStdErr(ParamProtocol, CmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r
2569\r
2570 //\r
2571 // The StdIn, StdOut, and StdErr are set up.\r
2572 // Now run the command, script, or application\r
2573 //\r
2574 if (!EFI_ERROR(Status)) {\r
75eb337f 2575 TrimSpaces(&CmdLine);\r
490ce43d 2576 Status = RunCommandOrFile(Type, CmdLine, FirstParameter, ParamProtocol, CommandStatus);\r
806c49db
JC
2577 }\r
2578\r
2579 //\r
2580 // Now print errors\r
2581 //\r
2582 if (EFI_ERROR(Status)) {\r
08222019
JC
2583 ConstScriptFile = ShellCommandGetCurrentScriptFile();\r
2584 if (ConstScriptFile == NULL || ConstScriptFile->CurrentCommand == NULL) {\r
063aa89b
JC
2585 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));\r
2586 } else {\r
08222019 2587 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR_SCRIPT), ShellInfoObject.HiiHandle, (VOID*)(Status), ConstScriptFile->CurrentCommand->Line);\r
063aa89b 2588 }\r
806c49db
JC
2589 }\r
2590\r
2591 //\r
2592 // put back the original StdIn, StdOut, and StdErr\r
2593 //\r
2594 RestoreStdInStdOutStdErr(ParamProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r
2595\r
2596 return (Status);\r
5a5eb806
JC
2597}\r
2598\r
a405b86d 2599/**\r
2600 Function will process and run a command line.\r
2601\r
ba0014b9 2602 This will determine if the command line represents an internal shell\r
a405b86d 2603 command or dispatch an external application.\r
2604\r
2605 @param[in] CmdLine The command line to parse.\r
490ce43d 2606 @param[out] CommandStatus The status from the command line.\r
a405b86d 2607\r
2608 @retval EFI_SUCCESS The command was completed.\r
2609 @retval EFI_ABORTED The command's operation was aborted.\r
2610**/\r
2611EFI_STATUS\r
490ce43d
QS
2612RunShellCommand(\r
2613 IN CONST CHAR16 *CmdLine,\r
2614 OUT EFI_STATUS *CommandStatus\r
a405b86d 2615 )\r
2616{\r
2617 EFI_STATUS Status;\r
a405b86d 2618 CHAR16 *CleanOriginal;\r
806c49db
JC
2619 CHAR16 *FirstParameter;\r
2620 CHAR16 *TempWalker;\r
2621 SHELL_OPERATION_TYPES Type;\r
f72df356 2622 CONST CHAR16 *CurDir;\r
a405b86d 2623\r
2624 ASSERT(CmdLine != NULL);\r
2625 if (StrLen(CmdLine) == 0) {\r
2626 return (EFI_SUCCESS);\r
2627 }\r
2628\r
a405b86d 2629 Status = EFI_SUCCESS;\r
2630 CleanOriginal = NULL;\r
a405b86d 2631\r
2632 CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0);\r
532691c8 2633 if (CleanOriginal == NULL) {\r
1219c85d 2634 return (EFI_OUT_OF_RESOURCES);\r
532691c8 2635 }\r
284e034f 2636\r
ad2bc854 2637 TrimSpaces(&CleanOriginal);\r
a405b86d 2638\r
284e034f 2639 //\r
d41a79a0
CP
2640 // NULL out comments (leveraged from RunScriptFileHandle() ).\r
2641 // The # character on a line is used to denote that all characters on the same line\r
2642 // and to the right of the # are to be ignored by the shell.\r
d08a5464 2643 // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.\r
d41a79a0
CP
2644 //\r
2645 for (TempWalker = CleanOriginal; TempWalker != NULL && *TempWalker != CHAR_NULL; TempWalker++) {\r
2646 if (*TempWalker == L'^') {\r
2647 if (*(TempWalker + 1) == L'#') {\r
4b6b543e 2648 TempWalker++;\r
d41a79a0
CP
2649 }\r
2650 } else if (*TempWalker == L'#') {\r
2651 *TempWalker = CHAR_NULL;\r
2652 }\r
2653 }\r
2654\r
2655 TrimSpaces(&CleanOriginal);\r
2656\r
2657 //\r
284e034f
CP
2658 // Handle case that passed in command line is just 1 or more " " characters.\r
2659 //\r
2660 if (StrLen (CleanOriginal) == 0) {\r
1219c85d
RN
2661 SHELL_FREE_NON_NULL(CleanOriginal);\r
2662 return (EFI_SUCCESS);\r
284e034f
CP
2663 }\r
2664\r
12a27a6d 2665 Status = ProcessCommandLineToFinal(&CleanOriginal);\r
ca53c0af 2666 if (EFI_ERROR(Status)) {\r
1219c85d
RN
2667 SHELL_FREE_NON_NULL(CleanOriginal);\r
2668 return (Status);\r
a405b86d 2669 }\r
2670\r
a405b86d 2671 //\r
d08a5464 2672 // We don't do normal processing with a split command line (output from one command input to another)\r
a405b86d 2673 //\r
1ef61d03 2674 if (ContainsSplit(CleanOriginal)) {\r
a308e058 2675 Status = ProcessNewSplitCommandLine(CleanOriginal);\r
1219c85d
RN
2676 SHELL_FREE_NON_NULL(CleanOriginal);\r
2677 return (Status);\r
ba0014b9 2678 }\r
5b5cd144 2679\r
806c49db
JC
2680 //\r
2681 // We need the first parameter information so we can determine the operation type\r
2682 //\r
2683 FirstParameter = AllocateZeroPool(StrSize(CleanOriginal));\r
2684 if (FirstParameter == NULL) {\r
1219c85d
RN
2685 SHELL_FREE_NON_NULL(CleanOriginal);\r
2686 return (EFI_OUT_OF_RESOURCES);\r
806c49db
JC
2687 }\r
2688 TempWalker = CleanOriginal;\r
1219c85d 2689 if (!EFI_ERROR(GetNextParameter(&TempWalker, &FirstParameter, StrSize(CleanOriginal), TRUE))) {\r
14030c5c
JC
2690 //\r
2691 // Depending on the first parameter we change the behavior\r
2692 //\r
2693 switch (Type = GetOperationType(FirstParameter)) {\r
2694 case File_Sys_Change:\r
2695 Status = ChangeMappedDrive (FirstParameter);\r
2696 break;\r
2697 case Internal_Command:\r
2698 case Script_File_Name:\r
2699 case Efi_Application:\r
490ce43d 2700 Status = SetupAndRunCommandOrFile(Type, CleanOriginal, FirstParameter, ShellInfoObject.NewShellParametersProtocol, CommandStatus);\r
14030c5c
JC
2701 break;\r
2702 default:\r
2703 //\r
2704 // Whatever was typed, it was invalid.\r
2705 //\r
2706 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
2707 SetLastError(SHELL_NOT_FOUND);\r
2708 break;\r
2709 }\r
2710 } else {\r
2711 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
2712 SetLastError(SHELL_NOT_FOUND);\r
a405b86d 2713 }\r
f72df356
HL
2714 //\r
2715 // Check whether the current file system still exists. If not exist, we need update "cwd" and gShellCurMapping.\r
2716 //\r
2717 CurDir = EfiShellGetCurDir (NULL);\r
2718 if (CurDir != NULL) {\r
2719 if (EFI_ERROR(ShellFileExists (CurDir))) {\r
2720 //\r
2721 // EfiShellSetCurDir() cannot set current directory to NULL.\r
2722 // EfiShellSetEnv() is not allowed to set the "cwd" variable.\r
2723 // Only InternalEfiShellSetEnv () is allowed setting the "cwd" variable.\r
2724 //\r
2725 InternalEfiShellSetEnv (L"cwd", NULL, TRUE);\r
2726 gShellCurMapping = NULL;\r
2727 }\r
2728 }\r
2729\r
1219c85d
RN
2730 SHELL_FREE_NON_NULL(CleanOriginal);\r
2731 SHELL_FREE_NON_NULL(FirstParameter);\r
a405b86d 2732\r
2733 return (Status);\r
2734}\r
2735\r
490ce43d
QS
2736/**\r
2737 Function will process and run a command line.\r
2738\r
ba0014b9 2739 This will determine if the command line represents an internal shell\r
490ce43d
QS
2740 command or dispatch an external application.\r
2741\r
2742 @param[in] CmdLine The command line to parse.\r
2743\r
2744 @retval EFI_SUCCESS The command was completed.\r
2745 @retval EFI_ABORTED The command's operation was aborted.\r
2746**/\r
2747EFI_STATUS\r
490ce43d
QS
2748RunCommand(\r
2749 IN CONST CHAR16 *CmdLine\r
2750 )\r
2751{\r
2752 return (RunShellCommand(CmdLine, NULL));\r
2753}\r
2754\r
a405b86d 2755/**\r
2756 Function to process a NSH script file via SHELL_FILE_HANDLE.\r
2757\r
2758 @param[in] Handle The handle to the already opened file.\r
2759 @param[in] Name The name of the script file.\r
2760\r
d08a5464 2761 @retval EFI_SUCCESS the script completed successfully\r
a405b86d 2762**/\r
2763EFI_STATUS\r
a405b86d 2764RunScriptFileHandle (\r
a308e058
RN
2765 IN SHELL_FILE_HANDLE Handle,\r
2766 IN CONST CHAR16 *Name\r
a405b86d 2767 )\r
2768{\r
2769 EFI_STATUS Status;\r
2770 SCRIPT_FILE *NewScriptFile;\r
2771 UINTN LoopVar;\r
4dc0d578 2772 UINTN PrintBuffSize;\r
a405b86d 2773 CHAR16 *CommandLine;\r
2774 CHAR16 *CommandLine2;\r
2775 CHAR16 *CommandLine3;\r
2776 SCRIPT_COMMAND_LIST *LastCommand;\r
2777 BOOLEAN Ascii;\r
2778 BOOLEAN PreScriptEchoState;\r
848997b5 2779 BOOLEAN PreCommandEchoState;\r
a405b86d 2780 CONST CHAR16 *CurDir;\r
2781 UINTN LineCount;\r
b6b22b13 2782 CHAR16 LeString[50];\r
14810d6b 2783 LIST_ENTRY OldBufferList;\r
a405b86d 2784\r
2785 ASSERT(!ShellCommandGetScriptExit());\r
2786\r
2787 PreScriptEchoState = ShellCommandGetEchoState();\r
4dc0d578 2788 PrintBuffSize = PcdGet16(PcdShellPrintBufferSize);\r
a405b86d 2789\r
2790 NewScriptFile = (SCRIPT_FILE*)AllocateZeroPool(sizeof(SCRIPT_FILE));\r
3c865f20 2791 if (NewScriptFile == NULL) {\r
2792 return (EFI_OUT_OF_RESOURCES);\r
2793 }\r
a405b86d 2794\r
2795 //\r
2796 // Set up the name\r
2797 //\r
2798 ASSERT(NewScriptFile->ScriptName == NULL);\r
2799 NewScriptFile->ScriptName = StrnCatGrow(&NewScriptFile->ScriptName, NULL, Name, 0);\r
3c865f20 2800 if (NewScriptFile->ScriptName == NULL) {\r
2801 DeleteScriptFileStruct(NewScriptFile);\r
2802 return (EFI_OUT_OF_RESOURCES);\r
2803 }\r
a405b86d 2804\r
2805 //\r
2806 // Save the parameters (used to replace %0 to %9 later on)\r
2807 //\r
2808 NewScriptFile->Argc = ShellInfoObject.NewShellParametersProtocol->Argc;\r
2809 if (NewScriptFile->Argc != 0) {\r
2810 NewScriptFile->Argv = (CHAR16**)AllocateZeroPool(NewScriptFile->Argc * sizeof(CHAR16*));\r
3c865f20 2811 if (NewScriptFile->Argv == NULL) {\r
2812 DeleteScriptFileStruct(NewScriptFile);\r
2813 return (EFI_OUT_OF_RESOURCES);\r
2814 }\r
a405b86d 2815 for (LoopVar = 0 ; LoopVar < 10 && LoopVar < NewScriptFile->Argc; LoopVar++) {\r
2816 ASSERT(NewScriptFile->Argv[LoopVar] == NULL);\r
2817 NewScriptFile->Argv[LoopVar] = StrnCatGrow(&NewScriptFile->Argv[LoopVar], NULL, ShellInfoObject.NewShellParametersProtocol->Argv[LoopVar], 0);\r
3c865f20 2818 if (NewScriptFile->Argv[LoopVar] == NULL) {\r
2819 DeleteScriptFileStruct(NewScriptFile);\r
2820 return (EFI_OUT_OF_RESOURCES);\r
2821 }\r
a405b86d 2822 }\r
2823 } else {\r
2824 NewScriptFile->Argv = NULL;\r
2825 }\r
2826\r
2827 InitializeListHead(&NewScriptFile->CommandList);\r
2828 InitializeListHead(&NewScriptFile->SubstList);\r
2829\r
2830 //\r
2831 // Now build the list of all script commands.\r
2832 //\r
2833 LineCount = 0;\r
2834 while(!ShellFileHandleEof(Handle)) {\r
2835 CommandLine = ShellFileHandleReturnLine(Handle, &Ascii);\r
2836 LineCount++;\r
4922715d
JC
2837 if (CommandLine == NULL || StrLen(CommandLine) == 0 || CommandLine[0] == '#') {\r
2838 SHELL_FREE_NON_NULL(CommandLine);\r
a405b86d 2839 continue;\r
2840 }\r
2841 NewScriptFile->CurrentCommand = AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST));\r
3c865f20 2842 if (NewScriptFile->CurrentCommand == NULL) {\r
4922715d 2843 SHELL_FREE_NON_NULL(CommandLine);\r
3c865f20 2844 DeleteScriptFileStruct(NewScriptFile);\r
2845 return (EFI_OUT_OF_RESOURCES);\r
2846 }\r
a405b86d 2847\r
2848 NewScriptFile->CurrentCommand->Cl = CommandLine;\r
2849 NewScriptFile->CurrentCommand->Data = NULL;\r
2850 NewScriptFile->CurrentCommand->Line = LineCount;\r
2851\r
2852 InsertTailList(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r
2853 }\r
2854\r
2855 //\r
2856 // Add this as the topmost script file\r
2857 //\r
2858 ShellCommandSetNewScript (NewScriptFile);\r
2859\r
2860 //\r
2861 // Now enumerate through the commands and run each one.\r
2862 //\r
4dc0d578 2863 CommandLine = AllocateZeroPool(PrintBuffSize);\r
3c865f20 2864 if (CommandLine == NULL) {\r
2865 DeleteScriptFileStruct(NewScriptFile);\r
2866 return (EFI_OUT_OF_RESOURCES);\r
2867 }\r
4dc0d578 2868 CommandLine2 = AllocateZeroPool(PrintBuffSize);\r
3c865f20 2869 if (CommandLine2 == NULL) {\r
2870 FreePool(CommandLine);\r
2871 DeleteScriptFileStruct(NewScriptFile);\r
2872 return (EFI_OUT_OF_RESOURCES);\r
2873 }\r
a405b86d 2874\r
2875 for ( NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&NewScriptFile->CommandList)\r
2876 ; !IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)\r
2877 ; // conditional increment in the body of the loop\r
2878 ){\r
3c865f20 2879 ASSERT(CommandLine2 != NULL);\r
ba0014b9
LG
2880 StrnCpyS( CommandLine2,\r
2881 PrintBuffSize/sizeof(CHAR16),\r
4dc0d578
QS
2882 NewScriptFile->CurrentCommand->Cl,\r
2883 PrintBuffSize/sizeof(CHAR16) - 1\r
e75390f0 2884 );\r
a405b86d 2885\r
14810d6b
QS
2886 SaveBufferList(&OldBufferList);\r
2887\r
a405b86d 2888 //\r
2889 // NULL out comments\r
2890 //\r
2891 for (CommandLine3 = CommandLine2 ; CommandLine3 != NULL && *CommandLine3 != CHAR_NULL ; CommandLine3++) {\r
2892 if (*CommandLine3 == L'^') {\r
4b6b543e 2893 if ( *(CommandLine3+1) == L':') {\r
a405b86d 2894 CopyMem(CommandLine3, CommandLine3+1, StrSize(CommandLine3) - sizeof(CommandLine3[0]));\r
4b6b543e
QS
2895 } else if (*(CommandLine3+1) == L'#') {\r
2896 CommandLine3++;\r
a405b86d 2897 }\r
2898 } else if (*CommandLine3 == L'#') {\r
2899 *CommandLine3 = CHAR_NULL;\r
2900 }\r
2901 }\r
2902\r
2903 if (CommandLine2 != NULL && StrLen(CommandLine2) >= 1) {\r
2904 //\r
2905 // Due to variability in starting the find and replace action we need to have both buffers the same.\r
2906 //\r
ba0014b9
LG
2907 StrnCpyS( CommandLine,\r
2908 PrintBuffSize/sizeof(CHAR16),\r
4dc0d578
QS
2909 CommandLine2,\r
2910 PrintBuffSize/sizeof(CHAR16) - 1\r
e75390f0 2911 );\r
a405b86d 2912\r
2913 //\r
2914 // Remove the %0 to %9 from the command line (if we have some arguments)\r
2915 //\r
2916 if (NewScriptFile->Argv != NULL) {\r
2917 switch (NewScriptFile->Argc) {\r
2918 default:\r
d1c275c6 2919 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", NewScriptFile->Argv[9], FALSE, FALSE);\r
a405b86d 2920 ASSERT_EFI_ERROR(Status);\r
2921 case 9:\r
d1c275c6 2922 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", NewScriptFile->Argv[8], FALSE, FALSE);\r
a405b86d 2923 ASSERT_EFI_ERROR(Status);\r
2924 case 8:\r
d1c275c6 2925 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", NewScriptFile->Argv[7], FALSE, FALSE);\r
a405b86d 2926 ASSERT_EFI_ERROR(Status);\r
2927 case 7:\r
d1c275c6 2928 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", NewScriptFile->Argv[6], FALSE, FALSE);\r
a405b86d 2929 ASSERT_EFI_ERROR(Status);\r
2930 case 6:\r
d1c275c6 2931 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", NewScriptFile->Argv[5], FALSE, FALSE);\r
a405b86d 2932 ASSERT_EFI_ERROR(Status);\r
2933 case 5:\r
d1c275c6 2934 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", NewScriptFile->Argv[4], FALSE, FALSE);\r
a405b86d 2935 ASSERT_EFI_ERROR(Status);\r
2936 case 4:\r
d1c275c6 2937 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", NewScriptFile->Argv[3], FALSE, FALSE);\r
a405b86d 2938 ASSERT_EFI_ERROR(Status);\r
2939 case 3:\r
d1c275c6 2940 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", NewScriptFile->Argv[2], FALSE, FALSE);\r
a405b86d 2941 ASSERT_EFI_ERROR(Status);\r
2942 case 2:\r
d1c275c6 2943 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", NewScriptFile->Argv[1], FALSE, FALSE);\r
a405b86d 2944 ASSERT_EFI_ERROR(Status);\r
2945 case 1:\r
d1c275c6 2946 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%0", NewScriptFile->Argv[0], FALSE, FALSE);\r
a405b86d 2947 ASSERT_EFI_ERROR(Status);\r
2948 break;\r
2949 case 0:\r
2950 break;\r
2951 }\r
2952 }\r
4dc0d578
QS
2953 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", L"\"\"", FALSE, FALSE);\r
2954 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", L"\"\"", FALSE, FALSE);\r
2955 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", L"\"\"", FALSE, FALSE);\r
2956 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", L"\"\"", FALSE, FALSE);\r
2957 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", L"\"\"", FALSE, FALSE);\r
2958 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", L"\"\"", FALSE, FALSE);\r
2959 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", L"\"\"", FALSE, FALSE);\r
2960 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", L"\"\"", FALSE, FALSE);\r
2961 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", L"\"\"", FALSE, FALSE);\r
2962\r
ba0014b9
LG
2963 StrnCpyS( CommandLine2,\r
2964 PrintBuffSize/sizeof(CHAR16),\r
4dc0d578
QS
2965 CommandLine,\r
2966 PrintBuffSize/sizeof(CHAR16) - 1\r
e75390f0 2967 );\r
a405b86d 2968\r
2969 LastCommand = NewScriptFile->CurrentCommand;\r
2970\r
2971 for (CommandLine3 = CommandLine2 ; CommandLine3[0] == L' ' ; CommandLine3++);\r
2972\r
3c865f20 2973 if (CommandLine3 != NULL && CommandLine3[0] == L':' ) {\r
a405b86d 2974 //\r
2975 // This line is a goto target / label\r
2976 //\r
2977 } else {\r
2978 if (CommandLine3 != NULL && StrLen(CommandLine3) > 0) {\r
848997b5 2979 if (CommandLine3[0] == L'@') {\r
2980 //\r
2981 // We need to save the current echo state\r
2982 // and disable echo for just this command.\r
2983 //\r
2984 PreCommandEchoState = ShellCommandGetEchoState();\r
2985 ShellCommandSetEchoState(FALSE);\r
a308e058 2986 Status = RunCommand(CommandLine3+1);\r
848997b5 2987\r
2988 //\r
284e034f 2989 // If command was "@echo -off" or "@echo -on" then don't restore echo state\r
848997b5 2990 //\r
284e034f
CP
2991 if (StrCmp (L"@echo -off", CommandLine3) != 0 &&\r
2992 StrCmp (L"@echo -on", CommandLine3) != 0) {\r
2993 //\r
2994 // Now restore the pre-'@' echo state.\r
2995 //\r
2996 ShellCommandSetEchoState(PreCommandEchoState);\r
2997 }\r
848997b5 2998 } else {\r
0d11446d 2999 if (ShellCommandGetEchoState()) {\r
3000 CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");\r
3001 if (CurDir != NULL && StrLen(CurDir) > 1) {\r
3002 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);\r
3003 } else {\r
3004 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);\r
3005 }\r
3006 ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);\r
3007 }\r
a308e058 3008 Status = RunCommand(CommandLine3);\r
848997b5 3009 }\r
a405b86d 3010 }\r
3011\r
3012 if (ShellCommandGetScriptExit()) {\r
5b5cd144
CP
3013 //\r
3014 // ShellCommandGetExitCode() always returns a UINT64\r
3015 //\r
a308e058 3016 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellCommandGetExitCode());\r
5b5cd144
CP
3017 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r
3018 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r
b6b22b13 3019\r
3020 ShellCommandRegisterExit(FALSE, 0);\r
a405b86d 3021 Status = EFI_SUCCESS;\r
14810d6b 3022 RestoreBufferList(&OldBufferList);\r
a405b86d 3023 break;\r
3024 }\r
c6a7fef8 3025 if (ShellGetExecutionBreakFlag()) {\r
14810d6b 3026 RestoreBufferList(&OldBufferList);\r
c6a7fef8
ED
3027 break;\r
3028 }\r
a405b86d 3029 if (EFI_ERROR(Status)) {\r
14810d6b 3030 RestoreBufferList(&OldBufferList);\r
a405b86d 3031 break;\r
3032 }\r
3033 if (ShellCommandGetExit()) {\r
14810d6b 3034 RestoreBufferList(&OldBufferList);\r
a405b86d 3035 break;\r
3036 }\r
3037 }\r
3038 //\r
3039 // If that commend did not update the CurrentCommand then we need to advance it...\r
3040 //\r
3041 if (LastCommand == NewScriptFile->CurrentCommand) {\r
3042 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r
3043 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {\r
3044 NewScriptFile->CurrentCommand->Reset = TRUE;\r
3045 }\r
3046 }\r
3047 } else {\r
3048 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r
3049 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {\r
3050 NewScriptFile->CurrentCommand->Reset = TRUE;\r
3051 }\r
3052 }\r
14810d6b 3053 RestoreBufferList(&OldBufferList);\r
a405b86d 3054 }\r
3055\r
a405b86d 3056\r
3057 FreePool(CommandLine);\r
3058 FreePool(CommandLine2);\r
3059 ShellCommandSetNewScript (NULL);\r
733f138d 3060\r
3061 //\r
3062 // Only if this was the last script reset the state.\r
3063 //\r
3064 if (ShellCommandGetCurrentScriptFile()==NULL) {\r
3065 ShellCommandSetEchoState(PreScriptEchoState);\r
3066 }\r
a405b86d 3067 return (EFI_SUCCESS);\r
3068}\r
3069\r
3070/**\r
3071 Function to process a NSH script file.\r
3072\r
3073 @param[in] ScriptPath Pointer to the script file name (including file system path).\r
e958b946
JC
3074 @param[in] Handle the handle of the script file already opened.\r
3075 @param[in] CmdLine the command line to run.\r
3076 @param[in] ParamProtocol the shell parameters protocol pointer\r
a405b86d 3077\r
d08a5464 3078 @retval EFI_SUCCESS the script completed successfully\r
a405b86d 3079**/\r
3080EFI_STATUS\r
a405b86d 3081RunScriptFile (\r
a308e058
RN
3082 IN CONST CHAR16 *ScriptPath,\r
3083 IN SHELL_FILE_HANDLE Handle OPTIONAL,\r
3084 IN CONST CHAR16 *CmdLine,\r
3085 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol\r
a405b86d 3086 )\r
3087{\r
3088 EFI_STATUS Status;\r
3089 SHELL_FILE_HANDLE FileHandle;\r
e958b946
JC
3090 UINTN Argc;\r
3091 CHAR16 **Argv;\r
a405b86d 3092\r
3093 if (ShellIsFile(ScriptPath) != EFI_SUCCESS) {\r
3094 return (EFI_INVALID_PARAMETER);\r
3095 }\r
3096\r
e958b946
JC
3097 //\r
3098 // get the argc and argv updated for scripts\r
3099 //\r
d1c275c6 3100 Status = UpdateArgcArgv(ParamProtocol, CmdLine, Script_File_Name, &Argv, &Argc);\r
e958b946 3101 if (!EFI_ERROR(Status)) {\r
a405b86d 3102\r
e958b946
JC
3103 if (Handle == NULL) {\r
3104 //\r
3105 // open the file\r
3106 //\r
3107 Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r
3108 if (!EFI_ERROR(Status)) {\r
3109 //\r
3110 // run it\r
3111 //\r
a308e058 3112 Status = RunScriptFileHandle(FileHandle, ScriptPath);\r
a405b86d 3113\r
e958b946
JC
3114 //\r
3115 // now close the file\r
3116 //\r
3117 ShellCloseFile(&FileHandle);\r
3118 }\r
3119 } else {\r
a308e058 3120 Status = RunScriptFileHandle(Handle, ScriptPath);\r
e958b946
JC
3121 }\r
3122 }\r
3123\r
3124 //\r
d08a5464 3125 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.\r
e958b946
JC
3126 // This is safe even if the update API failed. In this case, it may be a no-op.\r
3127 //\r
3128 RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r
a405b86d 3129\r
3130 return (Status);\r
3131}\r
00534bc3 3132\r
1219c85d
RN
3133/**\r
3134 Return the pointer to the first occurrence of any character from a list of characters.\r
3135\r
3136 @param[in] String the string to parse\r
3137 @param[in] CharacterList the list of character to look for\r
3138 @param[in] EscapeCharacter An escape character to skip\r
3139\r
3140 @return the location of the first character in the string\r
3141 @retval CHAR_NULL no instance of any character in CharacterList was found in String\r
3142**/\r
3143CONST CHAR16*\r
1219c85d
RN
3144FindFirstCharacter(\r
3145 IN CONST CHAR16 *String,\r
3146 IN CONST CHAR16 *CharacterList,\r
3147 IN CONST CHAR16 EscapeCharacter\r
3148 )\r
3149{\r
3150 UINT32 WalkChar;\r
3151 UINT32 WalkStr;\r
3152\r
3153 for (WalkStr = 0; WalkStr < StrLen(String); WalkStr++) {\r
3154 if (String[WalkStr] == EscapeCharacter) {\r
3155 WalkStr++;\r
3156 continue;\r
3157 }\r
3158 for (WalkChar = 0; WalkChar < StrLen(CharacterList); WalkChar++) {\r
3159 if (String[WalkStr] == CharacterList[WalkChar]) {\r
3160 return (&String[WalkStr]);\r
3161 }\r
3162 }\r
3163 }\r
3164 return (String + StrLen(String));\r
3165}\r