]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c
add Edit and HexEdit commands.
[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 // SHELL_FILE_HANDLE TempHandle;
40
41 Buffer = NULL;
42 ShellStatus = SHELL_SUCCESS;
43 Nfs = NULL;
44
45 //
46 // initialize the shell lib (we must be in non-auto-init...)
47 //
48 Status = ShellInitialize();
49 ASSERT_EFI_ERROR(Status);
50
51 Status = CommandInit();
52 ASSERT_EFI_ERROR(Status);
53
54 //
55 // parse the command line
56 //
57 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
58 if (EFI_ERROR(Status)) {
59 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
60 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, ProblemParam);
61 FreePool(ProblemParam);
62 ShellStatus = SHELL_INVALID_PARAMETER;
63 } else {
64 ASSERT(FALSE);
65 }
66 } else {
67 if (ShellCommandLineGetCount(Package) > 2) {
68 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
69 ShellStatus = SHELL_INVALID_PARAMETER;
70 } else {
71 Cwd = gEfiShellProtocol->GetCurDir(NULL);
72 if (Cwd == NULL) {
73 Cwd = ShellGetEnvironmentVariable(L"path");
74 if (Cwd != NULL) {
75 Nfs = StrnCatGrow(&Nfs, NULL, Cwd+3, 0);
76 if (Nfs != NULL) {
77 Spot = StrStr(Nfs, L";");
78 if (Spot != NULL) {
79 *Spot = CHAR_NULL;
80 }
81 Spot = StrStr(Nfs, L"\\");
82 if (Spot != NULL) {
83 Spot[1] = CHAR_NULL;
84 }
85 gEfiShellProtocol->SetCurDir(NULL, Nfs);
86 FreePool(Nfs);
87 }
88 }
89 }
90
91 Status = MainEditorInit ();
92
93 if (EFI_ERROR (Status)) {
94 gST->ConOut->ClearScreen (gST->ConOut);
95 gST->ConOut->EnableCursor (gST->ConOut, TRUE);
96 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_EDIT_MAIN_INIT_FAILED), gShellDebug1HiiHandle);
97 } else {
98 MainEditorBackup ();
99
100 //
101 // if editor launched with file named
102 //
103 if (ShellCommandLineGetCount(Package) == 2) {
104 FileBufferSetFileName (ShellCommandLineGetRawValue(Package, 1));
105 // if (EFI_ERROR(ShellFileExists(MainEditor.FileBuffer->FileName))) {
106 // Status = ShellOpenFileByName(MainEditor.FileBuffer->FileName, &TempHandle, EFI_FILE_MODE_CREATE|EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0);
107 // if (!EFI_ERROR(Status)) {
108 // ShellCloseFile(&TempHandle);
109 // }
110 // }
111 }
112
113 Status = FileBufferRead (MainEditor.FileBuffer->FileName, FALSE);
114 if (!EFI_ERROR (Status)) {
115 MainEditorRefresh ();
116
117 Status = MainEditorKeyInput ();
118 }
119
120 if (Status != EFI_OUT_OF_RESOURCES) {
121 //
122 // back up the status string
123 //
124 Buffer = CatSPrint (NULL, L"%s", StatusBarGetString());
125 }
126
127 MainEditorCleanup ();
128
129 //
130 // print editor exit code on screen
131 //
132 if (Status == EFI_SUCCESS) {
133 } else if (Status == EFI_OUT_OF_RESOURCES) {
134 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_GEN_OUT_MEM), gShellDebug1HiiHandle);
135 } else {
136 if (Buffer != NULL) {
137 if (StrCmp (Buffer, L"") != 0) {
138 //
139 // print out the status string
140 //
141 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_EDIT_MAIN_BUFFER), gShellDebug1HiiHandle, Buffer);
142 } else {
143 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_EDIT_MAIN_UNKNOWN_EDITOR_ERR), gShellDebug1HiiHandle);
144 }
145 } else {
146 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_EDIT_MAIN_UNKNOWN_EDITOR_ERR), gShellDebug1HiiHandle);
147 }
148 }
149
150 if (Status != EFI_OUT_OF_RESOURCES) {
151 SHELL_FREE_NON_NULL (Buffer);
152 }
153 }
154 }
155 ShellCommandLineFreeVarList (Package);
156 }
157 return ShellStatus;
158 }