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