]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / Edit / Edit.c
1 /** @file
2 Main file for Edit shell Debug1 function.
3
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved. <BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "UefiShellDebug1CommandsLib.h"
11 #include "TextEditor.h"
12
13 /**
14 Function for 'edit' command.
15
16 @param[in] ImageHandle Handle to the Image (NULL if Internal).
17 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
18 **/
19 SHELL_STATUS
20 EFIAPI
21 ShellCommandRunEdit (
22 IN EFI_HANDLE ImageHandle,
23 IN EFI_SYSTEM_TABLE *SystemTable
24 )
25 {
26 EFI_STATUS Status;
27 CHAR16 *Buffer;
28 CHAR16 *ProblemParam;
29 SHELL_STATUS ShellStatus;
30 LIST_ENTRY *Package;
31 CONST CHAR16 *Cwd;
32 CHAR16 *Nfs;
33 CHAR16 *Spot;
34 CONST CHAR16 *TempParam;
35
36 // SHELL_FILE_HANDLE TempHandle;
37
38 Buffer = NULL;
39 ShellStatus = SHELL_SUCCESS;
40 Nfs = NULL;
41
42 //
43 // initialize the shell lib (we must be in non-auto-init...)
44 //
45 Status = ShellInitialize ();
46 ASSERT_EFI_ERROR (Status);
47
48 Status = CommandInit ();
49 ASSERT_EFI_ERROR (Status);
50
51 //
52 // parse the command line
53 //
54 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
55 if (EFI_ERROR (Status)) {
56 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
57 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"edit", ProblemParam);
58 FreePool (ProblemParam);
59 ShellStatus = SHELL_INVALID_PARAMETER;
60 } else {
61 ASSERT (FALSE);
62 }
63 } else {
64 if (ShellCommandLineGetCount (Package) > 2) {
65 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"edit");
66 ShellStatus = SHELL_INVALID_PARAMETER;
67 } else {
68 Cwd = gEfiShellProtocol->GetCurDir (NULL);
69 if (Cwd == NULL) {
70 Cwd = ShellGetEnvironmentVariable (L"path");
71 if (Cwd != NULL) {
72 Nfs = StrnCatGrow (&Nfs, NULL, Cwd+3, 0);
73 if (Nfs != NULL) {
74 Spot = StrStr (Nfs, L";");
75 if (Spot != NULL) {
76 *Spot = CHAR_NULL;
77 }
78
79 Spot = StrStr (Nfs, L"\\");
80 if (Spot != NULL) {
81 Spot[1] = CHAR_NULL;
82 }
83
84 gEfiShellProtocol->SetCurDir (NULL, Nfs);
85 FreePool (Nfs);
86 }
87 }
88 }
89
90 Status = MainEditorInit ();
91
92 if (EFI_ERROR (Status)) {
93 gST->ConOut->ClearScreen (gST->ConOut);
94 gST->ConOut->EnableCursor (gST->ConOut, TRUE);
95 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_EDIT_MAIN_INIT_FAILED), gShellDebug1HiiHandle);
96 } else {
97 MainEditorBackup ();
98
99 //
100 // if editor launched with file named
101 //
102 if (ShellCommandLineGetCount (Package) == 2) {
103 TempParam = ShellCommandLineGetRawValue (Package, 1);
104 ASSERT (TempParam != NULL);
105 FileBufferSetFileName (TempParam);
106 // if (EFI_ERROR(ShellFileExists(MainEditor.FileBuffer->FileName))) {
107 // Status = ShellOpenFileByName(MainEditor.FileBuffer->FileName, &TempHandle, EFI_FILE_MODE_CREATE|EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0);
108 // if (!EFI_ERROR(Status)) {
109 // ShellCloseFile(&TempHandle);
110 // }
111 // }
112 }
113
114 Status = FileBufferRead (MainEditor.FileBuffer->FileName, FALSE);
115 if (!EFI_ERROR (Status)) {
116 MainEditorRefresh ();
117
118 Status = MainEditorKeyInput ();
119 }
120
121 if (Status != EFI_OUT_OF_RESOURCES) {
122 //
123 // back up the status string
124 //
125 Buffer = CatSPrint (NULL, L"%s", StatusBarGetString ());
126 }
127
128 MainEditorCleanup ();
129
130 //
131 // print editor exit code on screen
132 //
133 if (Status == EFI_SUCCESS) {
134 } else if (Status == EFI_OUT_OF_RESOURCES) {
135 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"edit");
136 } else {
137 if (Buffer != NULL) {
138 if (StrCmp (Buffer, L"") != 0) {
139 //
140 // print out the status string
141 //
142 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_EDIT_MAIN_BUFFER), gShellDebug1HiiHandle, Buffer);
143 } else {
144 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_EDIT_MAIN_UNKNOWN_EDITOR_ERR), gShellDebug1HiiHandle);
145 }
146 } else {
147 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_EDIT_MAIN_UNKNOWN_EDITOR_ERR), gShellDebug1HiiHandle);
148 }
149 }
150
151 if (Status != EFI_OUT_OF_RESOURCES) {
152 SHELL_FREE_NON_NULL (Buffer);
153 }
154 }
155 }
156
157 ShellCommandLineFreeVarList (Package);
158 }
159
160 return ShellStatus;
161 }