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