2 Implements filebuffer interface functions.
4 Copyright (c) 2005 - 2016, 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
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.
15 #include "TextEditor.h"
16 #include <Guid/FileSystemInfo.h>
17 #include <Library/FileHandleLib.h>
19 EFI_EDITOR_FILE_BUFFER FileBuffer
;
20 EFI_EDITOR_FILE_BUFFER FileBufferBackupVar
;
23 // for basic initialization of FileBuffer
25 EFI_EDITOR_FILE_BUFFER FileBufferConst
= {
54 // the whole edit area needs to be refreshed
56 BOOLEAN FileBufferNeedRefresh
;
59 // only the current line in edit area needs to be refresh
61 BOOLEAN FileBufferOnlyLineNeedRefresh
;
63 BOOLEAN FileBufferMouseNeedRefresh
;
65 extern BOOLEAN EditorMouseAction
;
68 Initialization function for FileBuffer.
70 @param EFI_SUCCESS The initialization was successful.
71 @param EFI_LOAD_ERROR A default name could not be created.
72 @param EFI_OUT_OF_RESOURCES A memory allocation failed.
81 // basically initialize the FileBuffer
83 CopyMem (&FileBuffer
, &FileBufferConst
, sizeof (EFI_EDITOR_FILE_BUFFER
));
84 CopyMem (&FileBufferBackupVar
, &FileBufferConst
, sizeof (EFI_EDITOR_FILE_BUFFER
));
87 // set default FileName
89 FileBuffer
.FileName
= EditGetDefaultFileName (L
"txt");
90 if (FileBuffer
.FileName
== NULL
) {
91 return EFI_LOAD_ERROR
;
94 FileBuffer
.ListHead
= AllocateZeroPool (sizeof (LIST_ENTRY
));
95 if (FileBuffer
.ListHead
== NULL
) {
96 return EFI_OUT_OF_RESOURCES
;
99 InitializeListHead (FileBuffer
.ListHead
);
101 FileBuffer
.DisplayPosition
.Row
= 2;
102 FileBuffer
.DisplayPosition
.Column
= 1;
103 FileBuffer
.LowVisibleRange
.Row
= 2;
104 FileBuffer
.LowVisibleRange
.Column
= 1;
106 FileBufferNeedRefresh
= FALSE
;
107 FileBufferMouseNeedRefresh
= FALSE
;
108 FileBufferOnlyLineNeedRefresh
= FALSE
;
114 Backup function for FileBuffer. Only backup the following items:
115 Mouse/Cursor position
116 File Name, Type, ReadOnly, Modified
119 This is for making the file buffer refresh as few as possible.
121 @retval EFI_SUCCESS The backup operation was successful.
129 FileBufferBackupVar
.MousePosition
= FileBuffer
.MousePosition
;
131 SHELL_FREE_NON_NULL (FileBufferBackupVar
.FileName
);
132 FileBufferBackupVar
.FileName
= NULL
;
133 FileBufferBackupVar
.FileName
= StrnCatGrow (&FileBufferBackupVar
.FileName
, NULL
, FileBuffer
.FileName
, 0);
135 FileBufferBackupVar
.ModeInsert
= FileBuffer
.ModeInsert
;
136 FileBufferBackupVar
.FileType
= FileBuffer
.FileType
;
138 FileBufferBackupVar
.FilePosition
= FileBuffer
.FilePosition
;
139 FileBufferBackupVar
.LowVisibleRange
= FileBuffer
.LowVisibleRange
;
141 FileBufferBackupVar
.FileModified
= FileBuffer
.FileModified
;
142 FileBufferBackupVar
.ReadOnly
= FileBuffer
.ReadOnly
;
148 Advance to the next Count lines
150 @param[in] Count The line number to advance by.
151 @param[in] CurrentLine The pointer to the current line structure.
152 @param[in] LineList The pointer to the linked list of lines.
154 @retval NULL There was an error.
155 @return The line structure after the advance.
159 InternalEditorMiscLineAdvance (
160 IN CONST UINTN Count
,
161 IN CONST EFI_EDITOR_LINE
*CurrentLine
,
162 IN CONST LIST_ENTRY
*LineList
167 CONST EFI_EDITOR_LINE
*Line
;
169 if (CurrentLine
== NULL
|| LineList
== NULL
) {
173 for (Line
= CurrentLine
, Index
= 0; Index
< Count
; Index
++) {
175 // if already last line
177 if (Line
->Link
.ForwardLink
== LineList
) {
181 Line
= CR (Line
->Link
.ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
184 return ((EFI_EDITOR_LINE
*)Line
);
188 Retreat to the previous Count lines.
190 @param[in] Count The line number to retreat by.
191 @param[in] CurrentLine The pointer to the current line structure.
192 @param[in] LineList The pointer to the linked list of lines.
194 @retval NULL There was an error.
195 @return The line structure after the retreat.
199 InternalEditorMiscLineRetreat (
200 IN CONST UINTN Count
,
201 IN CONST EFI_EDITOR_LINE
*CurrentLine
,
202 IN CONST LIST_ENTRY
*LineList
207 CONST EFI_EDITOR_LINE
*Line
;
209 if (CurrentLine
== NULL
|| LineList
== NULL
) {
213 for (Line
= CurrentLine
, Index
= 0; Index
< Count
; Index
++) {
215 // already the first line
217 if (Line
->Link
.BackLink
== LineList
) {
221 Line
= CR (Line
->Link
.BackLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
224 return ((EFI_EDITOR_LINE
*)Line
);
228 Advance/Retreat lines
230 @param[in] Count line number to advance/retreat
234 @retval NULL An error occured.
235 @return The line after advance/retreat.
242 EFI_EDITOR_LINE
*Line
;
246 // if < 0, then retreat
247 // if > 0, the advance
250 AbsCount
= (UINTN
)ABS(Count
);
251 Line
= InternalEditorMiscLineRetreat (AbsCount
,MainEditor
.FileBuffer
->CurrentLine
,MainEditor
.FileBuffer
->ListHead
);
253 Line
= InternalEditorMiscLineAdvance ((UINTN
)Count
,MainEditor
.FileBuffer
->CurrentLine
,MainEditor
.FileBuffer
->ListHead
);
260 Decide if a point is in the already selected area.
262 @param[in] MouseRow The row of the point to test.
263 @param[in] MouseCol The col of the point to test.
265 @retval TRUE The point is in the selected area.
266 @retval FALSE The point is not in the selected area.
269 FileBufferIsInSelectedArea (
283 // judge mouse position whether is in selected area
288 if (MainEditor
.SelectStart
== 0 || MainEditor
.SelectEnd
== 0) {
292 // calculate the select area
294 RowStart
= (MainEditor
.SelectStart
- 1) / SHELL_EDIT_MAX_LINE_SIZE
+ 1;
295 RowEnd
= (MainEditor
.SelectEnd
- 1) / SHELL_EDIT_MAX_LINE_SIZE
+ 1;
297 ColStart
= (MainEditor
.SelectStart
- 1) % SHELL_EDIT_MAX_LINE_SIZE
+ 1;
298 ColEnd
= (MainEditor
.SelectEnd
- 1) % SHELL_EDIT_MAX_LINE_SIZE
+ 1;
300 FRow
= FileBuffer
.LowVisibleRange
.Row
+ MouseRow
- 2;
301 if (FRow
< RowStart
|| FRow
> RowEnd
) {
305 if (FRow
> RowStart
) {
310 ColEnd
= SHELL_EDIT_MAX_LINE_SIZE
;
313 MouseColStart
= ColStart
;
315 MouseColEnd
= ColEnd
;
317 if (MouseCol
< MouseColStart
|| MouseCol
> MouseColEnd
) {
325 Function to update the 'screen' to display the mouse position.
327 @retval EFI_SUCCESS The backup operation was successful.
331 FileBufferRestoreMousePosition (
335 EFI_EDITOR_COLOR_UNION Orig
;
336 EFI_EDITOR_COLOR_UNION New
;
339 BOOLEAN HasCharacter
;
340 EFI_EDITOR_LINE
*CurrentLine
;
341 EFI_EDITOR_LINE
*Line
;
345 // variable initialization
349 if (MainEditor
.MouseSupported
) {
351 if (FileBufferMouseNeedRefresh
) {
353 FileBufferMouseNeedRefresh
= FALSE
;
356 // if mouse position not moved and only mouse action
357 // so do not need to refresh mouse position
359 if ((FileBuffer
.MousePosition
.Row
== FileBufferBackupVar
.MousePosition
.Row
&&
360 FileBuffer
.MousePosition
.Column
== FileBufferBackupVar
.MousePosition
.Column
)
361 && EditorMouseAction
) {
365 // backup the old screen attributes
367 Orig
= MainEditor
.ColorAttributes
;
369 New
.Colors
.Foreground
= Orig
.Colors
.Background
& 0xF;
370 New
.Colors
.Background
= Orig
.Colors
.Foreground
& 0x7;
373 // clear the old mouse position
375 FRow
= FileBuffer
.LowVisibleRange
.Row
+ FileBufferBackupVar
.MousePosition
.Row
- 2;
377 FColumn
= FileBuffer
.LowVisibleRange
.Column
+ FileBufferBackupVar
.MousePosition
.Column
- 1;
379 if (FRow
<= FileBuffer
.NumLines
) {
380 CurrentLine
= FileBuffer
.CurrentLine
;
381 Line
= MoveLine (FRow
- FileBuffer
.FilePosition
.Row
);
382 FileBuffer
.CurrentLine
= CurrentLine
;
386 // if in selected area,
387 // so do not need to refresh mouse
389 if (!FileBufferIsInSelectedArea (
390 FileBufferBackupVar
.MousePosition
.Row
,
391 FileBufferBackupVar
.MousePosition
.Column
393 (Line
== NULL
|| FColumn
> Line
->Size
)
395 gST
->ConOut
->SetAttribute (gST
->ConOut
, Orig
.Data
);
397 gST
->ConOut
->SetAttribute (gST
->ConOut
, New
.Data
& 0x7F);
403 if (FRow
> FileBuffer
.NumLines
) {
404 HasCharacter
= FALSE
;
406 CurrentLine
= FileBuffer
.CurrentLine
;
407 Line
= MoveLine (FRow
- FileBuffer
.FilePosition
.Row
);
409 if (Line
== NULL
|| FColumn
> Line
->Size
) {
410 HasCharacter
= FALSE
;
413 FileBuffer
.CurrentLine
= CurrentLine
;
417 (INT32
)FileBufferBackupVar
.MousePosition
.Column
- 1,
418 (INT32
)FileBufferBackupVar
.MousePosition
.Row
- 1,
423 Value
= (Line
->Buffer
[FColumn
- 1]);
425 (INT32
)FileBufferBackupVar
.MousePosition
.Column
- 1,
426 (INT32
)FileBufferBackupVar
.MousePosition
.Row
- 1,
432 // set the new mouse position
434 if (!FileBufferIsInSelectedArea (
435 FileBuffer
.MousePosition
.Row
,
436 FileBuffer
.MousePosition
.Column
439 gST
->ConOut
->SetAttribute (gST
->ConOut
, New
.Data
& 0x7F);
441 gST
->ConOut
->SetAttribute (gST
->ConOut
, Orig
.Data
);
444 // clear the old mouse position
446 FRow
= FileBuffer
.LowVisibleRange
.Row
+ FileBuffer
.MousePosition
.Row
- 2;
447 FColumn
= FileBuffer
.LowVisibleRange
.Column
+ FileBuffer
.MousePosition
.Column
- 1;
450 if (FRow
> FileBuffer
.NumLines
) {
451 HasCharacter
= FALSE
;
453 CurrentLine
= FileBuffer
.CurrentLine
;
454 Line
= MoveLine (FRow
- FileBuffer
.FilePosition
.Row
);
456 if (Line
== NULL
|| FColumn
> Line
->Size
) {
457 HasCharacter
= FALSE
;
460 FileBuffer
.CurrentLine
= CurrentLine
;
464 (INT32
)FileBuffer
.MousePosition
.Column
- 1,
465 (INT32
)FileBuffer
.MousePosition
.Row
- 1,
470 Value
= Line
->Buffer
[FColumn
- 1];
472 (INT32
)FileBuffer
.MousePosition
.Column
- 1,
473 (INT32
)FileBuffer
.MousePosition
.Row
- 1,
479 // end of HasCharacter
481 gST
->ConOut
->SetAttribute (gST
->ConOut
, Orig
.Data
);
484 // end of MouseNeedRefresh
488 // end of MouseSupported
494 Free all the lines in FileBuffer
501 @retval EFI_SUCCESS The operation was successful.
505 FileBufferFreeLines (
510 EFI_EDITOR_LINE
*Line
;
513 // free all the lines
515 if (FileBuffer
.Lines
!= NULL
) {
517 Line
= FileBuffer
.Lines
;
518 Link
= &(Line
->Link
);
520 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
521 Link
= Link
->ForwardLink
;
524 // free line's buffer and line itself
527 } while (Link
!= FileBuffer
.ListHead
);
530 // clean the line list related structure
532 FileBuffer
.Lines
= NULL
;
533 FileBuffer
.CurrentLine
= NULL
;
534 FileBuffer
.NumLines
= 0;
536 FileBuffer
.ListHead
->ForwardLink
= FileBuffer
.ListHead
;
537 FileBuffer
.ListHead
->BackLink
= FileBuffer
.ListHead
;
543 Cleanup function for FileBuffer.
545 @retval EFI_SUCCESS The cleanup was successful.
555 SHELL_FREE_NON_NULL (FileBuffer
.FileName
);
558 // free all the lines
560 Status
= FileBufferFreeLines ();
562 SHELL_FREE_NON_NULL (FileBuffer
.ListHead
);
563 FileBuffer
.ListHead
= NULL
;
565 SHELL_FREE_NON_NULL (FileBufferBackupVar
.FileName
);
574 @param[in] Line The lline to print.
575 @param[in] Row The row on screen ( begin from 1 ).
576 @param[in] FRow The FRow.
577 @param[in] Orig The original color.
578 @param[in] New The color to print with.
580 @retval EFI_SUCCESS The operation was successful.
583 FileBufferPrintLine (
584 IN CONST EFI_EDITOR_LINE
*Line
,
587 IN EFI_EDITOR_COLOR_UNION Orig
,
588 IN EFI_EDITOR_COLOR_UNION New
610 // print the selected area in opposite color
612 if (MainEditor
.SelectStart
!= 0 && MainEditor
.SelectEnd
!= 0) {
613 RowStart
= (MainEditor
.SelectStart
- 1) / SHELL_EDIT_MAX_LINE_SIZE
+ 1;
614 RowEnd
= (MainEditor
.SelectEnd
- 1) / SHELL_EDIT_MAX_LINE_SIZE
+ 1;
616 ColStart
= (MainEditor
.SelectStart
- 1) % SHELL_EDIT_MAX_LINE_SIZE
+ 1;
617 ColEnd
= (MainEditor
.SelectEnd
- 1) % SHELL_EDIT_MAX_LINE_SIZE
+ 1;
619 if (FRow
>= RowStart
&& FRow
<= RowEnd
) {
623 if (FRow
> RowStart
) {
628 ColEnd
= Line
->Size
+ 1;
634 // print start from correct character
636 Buffer
= Line
->Buffer
+ FileBuffer
.LowVisibleRange
.Column
- 1;
638 Limit
= Line
->Size
- FileBuffer
.LowVisibleRange
.Column
+ 1;
639 if (Limit
> Line
->Size
) {
643 BufLen
= (MainEditor
.ScreenSize
.Column
+ 1) * sizeof (CHAR16
);
644 PrintLine
= AllocatePool (BufLen
);
645 ASSERT (PrintLine
!= NULL
);
647 StrnCpyS (PrintLine
, BufLen
/sizeof(CHAR16
), Buffer
, MIN(Limit
, MainEditor
.ScreenSize
.Column
));
648 for (; Limit
< MainEditor
.ScreenSize
.Column
; Limit
++) {
649 PrintLine
[Limit
] = L
' ';
652 PrintLine
[MainEditor
.ScreenSize
.Column
] = CHAR_NULL
;
654 PrintLine2
= AllocatePool (BufLen
* 2);
655 ASSERT (PrintLine2
!= NULL
);
657 ShellCopySearchAndReplace(PrintLine
, PrintLine2
, BufLen
* 2, L
"%", L
"^%", FALSE
, FALSE
);
668 // If the current line is selected.
671 gST
->ConOut
->SetAttribute (gST
->ConOut
, Orig
.Data
& 0x7F);
672 TempString
= AllocateCopyPool ((ColStart
- 1) * sizeof(CHAR16
), PrintLine2
);
673 ASSERT (TempString
!= NULL
);
680 FreePool (TempString
);
683 gST
->ConOut
->SetAttribute (gST
->ConOut
, New
.Data
& 0x7F);
684 TempString
= AllocateCopyPool (
685 (ColEnd
- ColStart
+ 1) * sizeof(CHAR16
),
686 PrintLine2
+ ColStart
- 1
688 ASSERT (TempString
!= NULL
);
695 FreePool (TempString
);
697 if (ColEnd
!= SHELL_EDIT_MAX_LINE_SIZE
) {
698 gST
->ConOut
->SetAttribute (gST
->ConOut
, Orig
.Data
& 0x7F);
699 TempString
= AllocateCopyPool (
700 (SHELL_EDIT_MAX_LINE_SIZE
- ColEnd
+ 1) * sizeof(CHAR16
),
701 PrintLine2
+ ColEnd
- 1
703 ASSERT (TempString
!= NULL
);
710 FreePool (TempString
);
714 FreePool (PrintLine
);
715 FreePool (PrintLine2
);
716 gST
->ConOut
->SetAttribute (gST
->ConOut
, Orig
.Data
& 0x7F);
722 Set the cursor position according to FileBuffer.DisplayPosition.
724 @retval EFI_SUCCESS The operation was successful.
728 FileBufferRestorePosition (
733 // set cursor position
735 return (gST
->ConOut
->SetCursorPosition (
737 FileBuffer
.DisplayPosition
.Column
- 1,
738 FileBuffer
.DisplayPosition
.Row
- 1
743 Refresh the screen with whats in the buffer.
745 @retval EFI_SUCCESS The refresh was successful.
746 @retval EFI_LOAD_ERROR There was an error finding what to write.
755 EFI_EDITOR_LINE
*Line
;
757 EFI_EDITOR_COLOR_UNION Orig
;
758 EFI_EDITOR_COLOR_UNION New
;
764 Orig
= MainEditor
.ColorAttributes
;
766 New
.Colors
.Foreground
= Orig
.Colors
.Background
;
767 New
.Colors
.Background
= Orig
.Colors
.Foreground
;
770 // if it's the first time after editor launch, so should refresh
774 // no definite required refresh
775 // and file position displayed on screen has not been changed
777 if (!FileBufferNeedRefresh
&&
778 !FileBufferOnlyLineNeedRefresh
&&
779 FileBufferBackupVar
.LowVisibleRange
.Row
== FileBuffer
.LowVisibleRange
.Row
781 FileBufferRestoreMousePosition ();
782 FileBufferRestorePosition ();
787 gST
->ConOut
->EnableCursor (gST
->ConOut
, FALSE
);
790 // only need to refresh current line
792 if (FileBufferOnlyLineNeedRefresh
&&
793 FileBufferBackupVar
.LowVisibleRange
.Row
== FileBuffer
.LowVisibleRange
.Row
796 EditorClearLine (FileBuffer
.DisplayPosition
.Row
, MainEditor
.ScreenSize
.Column
, MainEditor
.ScreenSize
.Row
);
797 FileBufferPrintLine (
798 FileBuffer
.CurrentLine
,
799 FileBuffer
.DisplayPosition
.Row
,
800 FileBuffer
.FilePosition
.Row
,
806 // the whole edit area need refresh
808 if (EditorMouseAction
&& MainEditor
.SelectStart
!= 0 && MainEditor
.SelectEnd
!= 0) {
809 if (MainEditor
.SelectStart
!= MainEditorBackupVar
.SelectStart
) {
810 if (MainEditor
.SelectStart
>= MainEditorBackupVar
.SelectStart
&& MainEditorBackupVar
.SelectStart
!= 0) {
811 StartRow
= (MainEditorBackupVar
.SelectStart
- 1) / SHELL_EDIT_MAX_LINE_SIZE
+ 1;
813 StartRow
= (MainEditor
.SelectStart
- 1) / SHELL_EDIT_MAX_LINE_SIZE
+ 1;
816 StartRow
= (MainEditor
.SelectStart
- 1) / SHELL_EDIT_MAX_LINE_SIZE
+ 1;
819 if (MainEditor
.SelectEnd
<= MainEditorBackupVar
.SelectEnd
) {
820 EndRow
= (MainEditorBackupVar
.SelectEnd
- 1) / SHELL_EDIT_MAX_LINE_SIZE
+ 1;
822 EndRow
= (MainEditor
.SelectEnd
- 1) / SHELL_EDIT_MAX_LINE_SIZE
+ 1;
828 if (StartRow
> EndRow
) {
834 StartRow
= 2 + StartRow
- FileBuffer
.LowVisibleRange
.Row
;
835 EndRow
= 2 + EndRow
- FileBuffer
.LowVisibleRange
.Row
;
839 // not mouse selection actions
842 EndRow
= (MainEditor
.ScreenSize
.Row
- 1);
848 if (FileBuffer
.Lines
== NULL
) {
849 FileBufferRestoreMousePosition ();
850 FileBufferRestorePosition ();
851 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
856 // get the first line that will be displayed
858 Line
= MoveLine (FileBuffer
.LowVisibleRange
.Row
- FileBuffer
.FilePosition
.Row
);
860 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
862 return EFI_LOAD_ERROR
;
865 Link
= &(Line
->Link
);
868 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
873 FileBufferPrintLine (Line
,
875 FileBuffer
.LowVisibleRange
.Row
+ Row
- 2,
880 Link
= Link
->ForwardLink
;
882 } while (Link
!= FileBuffer
.ListHead
&& Row
<= EndRow
);
884 // while not file end and not screen full
886 while (Row
<= EndRow
) {
887 EditorClearLine (Row
, MainEditor
.ScreenSize
.Column
, MainEditor
.ScreenSize
.Row
);
892 FileBufferRestoreMousePosition ();
893 FileBufferRestorePosition ();
895 FileBufferNeedRefresh
= FALSE
;
896 FileBufferOnlyLineNeedRefresh
= FALSE
;
898 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
903 Create a new line and append it to the line list.
908 @retval NULL The create line failed.
909 @return The line created.
913 FileBufferCreateLine (
917 EFI_EDITOR_LINE
*Line
;
920 // allocate a line structure
922 Line
= AllocateZeroPool (sizeof (EFI_EDITOR_LINE
));
927 // initialize the structure
929 Line
->Signature
= LINE_LIST_SIGNATURE
;
932 Line
->Type
= NewLineTypeDefault
;
935 // initial buffer of the line is "\0"
937 ASSERT(CHAR_NULL
== CHAR_NULL
);
938 Line
->Buffer
= CatSPrint (NULL
, L
"\0");
939 if (Line
->Buffer
== NULL
) {
943 FileBuffer
.NumLines
++;
946 // insert the line into line list
948 InsertTailList (FileBuffer
.ListHead
, &Line
->Link
);
950 if (FileBuffer
.Lines
== NULL
) {
951 FileBuffer
.Lines
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
958 Set FileName field in FileBuffer.
960 @param Str The file name to set.
962 @retval EFI_SUCCESS The filename was successfully set.
963 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
964 @retval EFI_INVALID_PARAMETER Str is not a valid filename.
968 FileBufferSetFileName (
973 // Verify the parameters
975 if (!IsValidFileName(Str
)) {
976 return (EFI_INVALID_PARAMETER
);
979 // free the old file name
981 SHELL_FREE_NON_NULL (FileBuffer
.FileName
);
984 // Allocate and set the new name
986 FileBuffer
.FileName
= CatSPrint (NULL
, L
"%s", Str
);
987 if (FileBuffer
.FileName
== NULL
) {
988 return EFI_OUT_OF_RESOURCES
;
994 Free the existing file lines and reset the modified flag.
996 @retval EFI_SUCCESS The operation was successful.
1005 // free all the lines
1007 FileBufferFreeLines ();
1008 FileBuffer
.FileModified
= FALSE
;
1015 Read a file from disk into the FileBuffer.
1017 @param[in] FileName The filename to read.
1018 @param[in] Recover TRUE if is for recover mode, no information printouts.
1020 @retval EFI_SUCCESS The load was successful.
1021 @retval EFI_LOAD_ERROR The load failed.
1022 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1023 @retval EFI_INVALID_PARAMETER FileName is a directory.
1028 IN CONST CHAR16
*FileName
,
1029 IN CONST BOOLEAN Recover
1032 EFI_EDITOR_LINE
*Line
;
1033 EE_NEWLINE_TYPE Type
;
1038 CHAR16
*UnicodeBuffer
;
1041 SHELL_FILE_HANDLE FileHandle
;
1044 UINTN LineSizeBackup
;
1045 EFI_FILE_INFO
*Info
;
1050 UnicodeBuffer
= NULL
;
1051 Type
= NewLineTypeDefault
;
1056 // in this function, when you return error ( except EFI_OUT_OF_RESOURCES )
1057 // you should set status string via StatusBarSetStatusString(L"blah")
1058 // since this function maybe called before the editorhandleinput loop
1059 // so any error will cause editor return
1060 // so if you want to print the error status
1061 // you should set the status string
1065 // try to open the file
1067 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
1069 if (!EFI_ERROR(Status
)) {
1071 if (FileHandle
== NULL
) {
1072 StatusBarSetStatusString (L
"Disk Error");
1073 return EFI_LOAD_ERROR
;
1076 Info
= ShellGetFileInfo(FileHandle
);
1078 if (Info
->Attribute
& EFI_FILE_DIRECTORY
) {
1079 StatusBarSetStatusString (L
"Directory Can Not Be Edited");
1081 return EFI_INVALID_PARAMETER
;
1084 if (Info
->Attribute
& EFI_FILE_READ_ONLY
) {
1085 FileBuffer
.ReadOnly
= TRUE
;
1087 FileBuffer
.ReadOnly
= FALSE
;
1092 FileSize
= (UINTN
) Info
->FileSize
;
1095 } else if (Status
== EFI_NOT_FOUND
) {
1097 // file not exists. add create and try again
1099 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
1100 if (EFI_ERROR (Status
)) {
1101 if (Status
== EFI_WRITE_PROTECTED
||
1102 Status
== EFI_ACCESS_DENIED
||
1103 Status
== EFI_NO_MEDIA
||
1104 Status
== EFI_MEDIA_CHANGED
1106 StatusBarSetStatusString (L
"Access Denied");
1107 } else if (Status
== EFI_DEVICE_ERROR
|| Status
== EFI_VOLUME_CORRUPTED
|| Status
== EFI_VOLUME_FULL
) {
1108 StatusBarSetStatusString (L
"Disk Error");
1110 StatusBarSetStatusString (L
"Invalid File Name or Current-working-directory");
1116 // it worked. now delete it and move on with the name (now validated)
1118 Status
= ShellDeleteFile (&FileHandle
);
1119 if (Status
== EFI_WARN_DELETE_FAILURE
) {
1120 Status
= EFI_ACCESS_DENIED
;
1123 if (EFI_ERROR (Status
)) {
1124 StatusBarSetStatusString (L
"Access Denied");
1129 // file doesn't exist, so set CreateFile to TRUE
1132 FileBuffer
.ReadOnly
= FALSE
;
1135 // all the check ends
1136 // so now begin to set file name, free lines
1138 if (StrCmp (FileName
, FileBuffer
.FileName
) != 0) {
1139 FileBufferSetFileName (FileName
);
1142 // free the old lines
1152 // allocate buffer to read file
1154 Buffer
= AllocateZeroPool (FileSize
);
1155 if (Buffer
== NULL
) {
1156 return EFI_OUT_OF_RESOURCES
;
1159 // read file into Buffer
1161 Status
= ShellReadFile (FileHandle
, &FileSize
, Buffer
);
1162 ShellCloseFile(&FileHandle
);
1164 if (EFI_ERROR (Status
)) {
1165 StatusBarSetStatusString (L
"Read File Failed");
1166 SHELL_FREE_NON_NULL (Buffer
);
1167 return EFI_LOAD_ERROR
;
1170 // nothing in this file
1172 if (FileSize
== 0) {
1173 SHELL_FREE_NON_NULL (Buffer
);
1175 // since has no head, so only can be an ASCII file
1177 FileBuffer
.FileType
= FileTypeAscii
;
1182 AsciiBuffer
= Buffer
;
1186 // size < Unicode file header, so only can be ASCII file
1188 FileBuffer
.FileType
= FileTypeAscii
;
1193 if (*(UINT16
*) Buffer
== EFI_UNICODE_BYTE_ORDER_MARK
) {
1195 // Unicode file's size should be even
1197 if ((FileSize
% 2) != 0) {
1198 StatusBarSetStatusString (L
"File Format Wrong");
1199 SHELL_FREE_NON_NULL (Buffer
);
1200 return EFI_LOAD_ERROR
;
1205 FileBuffer
.FileType
= FileTypeUnicode
;
1206 UnicodeBuffer
= Buffer
;
1209 // pass this 0xff and 0xfe
1214 FileBuffer
.FileType
= FileTypeAscii
;
1217 // end of AsciiBuffer ==
1221 // end of FileSize < 2
1222 // all the check ends
1223 // so now begin to set file name, free lines
1225 if (StrCmp (FileName
, FileBuffer
.FileName
) != 0) {
1226 FileBufferSetFileName (FileName
);
1230 // free the old lines
1235 // parse file content line by line
1237 for (LoopVar1
= 0; LoopVar1
< FileSize
; LoopVar1
++) {
1238 Type
= NewLineTypeUnknown
;
1240 for (LineSize
= LoopVar1
; LineSize
< FileSize
; LineSize
++) {
1241 if (FileBuffer
.FileType
== FileTypeAscii
) {
1242 if (AsciiBuffer
[LineSize
] == CHAR_CARRIAGE_RETURN
) {
1243 Type
= NewLineTypeCarriageReturn
;
1248 if (LineSize
< FileSize
- 1) {
1249 if (AsciiBuffer
[LineSize
+ 1] == CHAR_LINEFEED
) {
1250 Type
= NewLineTypeCarriageReturnLineFeed
;
1255 } else if (AsciiBuffer
[LineSize
] == CHAR_LINEFEED
) {
1256 Type
= NewLineTypeLineFeed
;
1261 if (LineSize
< FileSize
- 1) {
1262 if (AsciiBuffer
[LineSize
+ 1] == CHAR_CARRIAGE_RETURN
) {
1263 Type
= NewLineTypeLineFeedCarriageReturn
;
1270 if (UnicodeBuffer
[LineSize
] == CHAR_CARRIAGE_RETURN
) {
1271 Type
= NewLineTypeCarriageReturn
;
1276 if (LineSize
< FileSize
- 1) {
1277 if (UnicodeBuffer
[LineSize
+ 1] == CHAR_LINEFEED
) {
1278 Type
= NewLineTypeCarriageReturnLineFeed
;
1283 } else if (UnicodeBuffer
[LineSize
] == CHAR_LINEFEED
) {
1284 Type
= NewLineTypeLineFeed
;
1289 if (LineSize
< FileSize
- 1) {
1290 if (UnicodeBuffer
[LineSize
+ 1] == CHAR_CARRIAGE_RETURN
) {
1291 Type
= NewLineTypeLineFeedCarriageReturn
;
1303 // end of for LineSize
1305 // if the type is wrong, then exit
1307 if (Type
== NewLineTypeUnknown
) {
1309 // Now if Type is NewLineTypeUnknown, it should be file end
1311 Type
= NewLineTypeDefault
;
1314 LineSizeBackup
= LineSize
;
1317 // create a new line
1319 Line
= FileBufferCreateLine ();
1321 SHELL_FREE_NON_NULL (Buffer
);
1322 return EFI_OUT_OF_RESOURCES
;
1325 // calculate file length
1327 LineSize
-= LoopVar1
;
1330 // Unicode and one CHAR_NULL
1332 SHELL_FREE_NON_NULL (Line
->Buffer
);
1333 Line
->Buffer
= AllocateZeroPool (LineSize
* 2 + 2);
1335 if (Line
->Buffer
== NULL
) {
1336 RemoveEntryList (&Line
->Link
);
1337 return EFI_OUT_OF_RESOURCES
;
1340 // copy this line to Line->Buffer
1342 for (LoopVar2
= 0; LoopVar2
< LineSize
; LoopVar2
++) {
1343 if (FileBuffer
.FileType
== FileTypeAscii
) {
1344 Line
->Buffer
[LoopVar2
] = (CHAR16
) AsciiBuffer
[LoopVar1
];
1346 Line
->Buffer
[LoopVar2
] = UnicodeBuffer
[LoopVar1
];
1352 // LoopVar1 now points to where CHAR_CARRIAGE_RETURN or CHAR_LINEFEED;
1354 Line
->Buffer
[LineSize
] = 0;
1356 Line
->Size
= LineSize
;
1357 Line
->TotalSize
= LineSize
;
1360 if (Type
== NewLineTypeCarriageReturnLineFeed
|| Type
== NewLineTypeLineFeedCarriageReturn
) {
1365 // last character is a return, SO create a new line
1367 if (((Type
== NewLineTypeCarriageReturnLineFeed
|| Type
== NewLineTypeLineFeedCarriageReturn
) && LineSizeBackup
== FileSize
- 2) ||
1368 ((Type
== NewLineTypeLineFeed
|| Type
== NewLineTypeCarriageReturn
) && LineSizeBackup
== FileSize
- 1)
1370 Line
= FileBufferCreateLine ();
1372 SHELL_FREE_NON_NULL (Buffer
);
1373 return EFI_OUT_OF_RESOURCES
;
1383 SHELL_FREE_NON_NULL (Buffer
);
1387 // end of if CreateFile
1391 FileBuffer
.DisplayPosition
.Row
= 2;
1392 FileBuffer
.DisplayPosition
.Column
= 1;
1393 FileBuffer
.LowVisibleRange
.Row
= 1;
1394 FileBuffer
.LowVisibleRange
.Column
= 1;
1395 FileBuffer
.FilePosition
.Row
= 1;
1396 FileBuffer
.FilePosition
.Column
= 1;
1397 FileBuffer
.MousePosition
.Row
= 2;
1398 FileBuffer
.MousePosition
.Column
= 1;
1401 UnicodeBuffer
= CatSPrint (NULL
, L
"%d Lines Read", FileBuffer
.NumLines
);
1402 if (UnicodeBuffer
== NULL
) {
1403 return EFI_OUT_OF_RESOURCES
;
1406 StatusBarSetStatusString (UnicodeBuffer
);
1407 FreePool (UnicodeBuffer
);
1411 // check whether we have fs?: in filename
1414 FSMappingPtr = NULL;
1415 while (FileName[LoopVar1] != 0) {
1416 if (FileName[LoopVar1] == L':') {
1417 FSMappingPtr = &FileName[LoopVar1];
1424 if (FSMappingPtr == NULL) {
1425 CurDir = ShellGetCurrentDir (NULL);
1429 while (FileName[LoopVar1] != 0) {
1430 if (FileName[LoopVar1] == L':') {
1434 FSMapping[LoopVar2++] = FileName[LoopVar1];
1439 FSMapping[LoopVar2] = 0;
1440 CurDir = ShellGetCurrentDir (FSMapping);
1443 if (CurDir != NULL) {
1444 for (LoopVar1 = 0; LoopVar1 < StrLen (CurDir) && CurDir[LoopVar1] != ':'; LoopVar1++);
1446 CurDir[LoopVar1] = 0;
1447 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) ShellGetMap (CurDir);
1450 return EFI_LOAD_ERROR;
1453 Status = LibDevicePathToInterface (
1454 &gEfiSimpleFileSystemProtocolGuid,
1458 if (EFI_ERROR (Status)) {
1459 return EFI_LOAD_ERROR;
1462 Status = Vol->OpenVolume (Vol, &RootFs);
1463 if (EFI_ERROR (Status)) {
1464 return EFI_LOAD_ERROR;
1467 // Get volume information of file system
1469 Size = SIZE_OF_EFI_FILE_SYSTEM_INFO + 100;
1470 VolumeInfo = (EFI_FILE_SYSTEM_INFO *) AllocateZeroPool (Size);
1471 Status = RootFs->GetInfo (RootFs, &gEfiFileSystemInfoGuid, &Size, VolumeInfo);
1472 if (EFI_ERROR (Status)) {
1473 RootFs->Close (RootFs);
1474 return EFI_LOAD_ERROR;
1477 if (VolumeInfo->ReadOnly) {
1478 StatusBarSetStatusString (L"WARNING: Volume Read Only");
1481 FreePool (VolumeInfo);
1482 RootFs->Close (RootFs);
1489 if (FileBuffer
.Lines
!= 0) {
1490 FileBuffer
.CurrentLine
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1493 // create a dummy line
1495 Line
= FileBufferCreateLine ();
1497 return EFI_OUT_OF_RESOURCES
;
1500 FileBuffer
.CurrentLine
= Line
;
1503 FileBuffer
.FileModified
= FALSE
;
1504 FileBufferNeedRefresh
= TRUE
;
1505 FileBufferOnlyLineNeedRefresh
= FALSE
;
1506 FileBufferMouseNeedRefresh
= TRUE
;
1513 According to FileBuffer.NewLineType & FileBuffer.FileType,
1514 get the return buffer and size.
1516 @param[in] Type The type of line.
1517 @param[out] Buffer The buffer to fill.
1518 @param[out] Size The amount of the buffer used on return.
1523 IN CONST EE_NEWLINE_TYPE Type
,
1531 // give new line buffer,
1532 // and will judge unicode or ascii
1537 // not legal new line type
1539 if (Type
!= NewLineTypeLineFeed
&& Type
!= NewLineTypeCarriageReturn
&& Type
!= NewLineTypeCarriageReturnLineFeed
&& Type
!= NewLineTypeLineFeedCarriageReturn
) {
1544 // use_cr: give 0x0d
1546 if (Type
== NewLineTypeCarriageReturn
) {
1547 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1556 *Size
= NewLineSize
;
1560 // use_lf: give 0x0a
1562 if (Type
== NewLineTypeLineFeed
) {
1563 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1572 *Size
= NewLineSize
;
1576 // use_crlf: give 0x0d 0x0a
1578 if (Type
== NewLineTypeCarriageReturnLineFeed
) {
1579 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1592 *Size
= NewLineSize
;
1596 // use_lfcr: give 0x0a 0x0d
1598 if (Type
== NewLineTypeLineFeedCarriageReturn
) {
1599 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1612 *Size
= NewLineSize
;
1619 Change a Unicode string to an ASCII string.
1621 @param[in] UStr The Unicode string.
1622 @param[in] Length The maximum size of AStr.
1623 @param[out] AStr ASCII string to pass out.
1625 @return The actuall length.
1630 IN CONST CHAR16
*UStr
,
1631 IN CONST UINTN Length
,
1638 // just buffer copy, not character copy
1640 for (Index
= 0; Index
< Length
; Index
++) {
1641 *AStr
++ = (CHAR8
) *UStr
++;
1648 Save lines in FileBuffer to disk
1650 @param[in] FileName The file name for writing.
1652 @retval EFI_SUCCESS Data was written.
1653 @retval EFI_LOAD_ERROR
1654 @retval EFI_OUT_OF_RESOURCES There were not enough resources to write the file.
1659 IN CONST CHAR16
*FileName
1662 SHELL_FILE_HANDLE FileHandle
;
1664 EFI_EDITOR_LINE
*Line
;
1670 CHAR8 NewLineBuffer
[4];
1673 EFI_FILE_INFO
*Info
;
1677 EE_NEWLINE_TYPE Type
;
1692 TotalSize
= 0x200000;
1699 // if is the old file
1701 if (FileBuffer
.FileName
!= NULL
&& StrCmp (FileName
, FileBuffer
.FileName
) == 0) {
1703 // file has not been modified
1705 if (!FileBuffer
.FileModified
) {
1710 // if file is read-only, set error
1712 if (FileBuffer
.ReadOnly
) {
1713 StatusBarSetStatusString (L
"Read Only File Can Not Be Saved");
1718 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
, 0);
1720 if (!EFI_ERROR (Status
)) {
1721 Info
= ShellGetFileInfo(FileHandle
);
1723 if (Info
!= NULL
&& Info
->Attribute
& EFI_FILE_DIRECTORY
) {
1724 StatusBarSetStatusString (L
"Directory Can Not Be Saved");
1725 ShellCloseFile(FileHandle
);
1727 return EFI_LOAD_ERROR
;
1731 Attribute
= Info
->Attribute
& ~EFI_FILE_READ_ONLY
;
1736 // if file exits, so delete it
1738 Status
= ShellDeleteFile (&FileHandle
);
1739 if (EFI_ERROR (Status
) || Status
== EFI_WARN_DELETE_FAILURE
) {
1740 StatusBarSetStatusString (L
"Write File Failed");
1741 return EFI_LOAD_ERROR
;
1745 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, Attribute
);
1747 if (EFI_ERROR (Status
)) {
1748 StatusBarSetStatusString (L
"Create File Failed");
1749 return EFI_LOAD_ERROR
;
1753 // if file is Unicode file, write Unicode header to it.
1755 if (FileBuffer
.FileType
== FileTypeUnicode
) {
1757 Status
= ShellWriteFile (FileHandle
, &Length
, (VOID
*)&gUnicodeFileTag
);
1758 if (EFI_ERROR (Status
)) {
1759 ShellDeleteFile (&FileHandle
);
1760 return EFI_LOAD_ERROR
;
1764 Cache
= AllocateZeroPool (TotalSize
);
1765 if (Cache
== NULL
) {
1766 ShellDeleteFile (&FileHandle
);
1767 return EFI_OUT_OF_RESOURCES
;
1771 // write all the lines back to disk
1774 Type
= NewLineTypeCarriageReturnLineFeed
;
1777 LeftSize
= TotalSize
;
1779 for (Link
= FileBuffer
.ListHead
->ForwardLink
; Link
!= FileBuffer
.ListHead
; Link
= Link
->ForwardLink
) {
1780 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1782 if (Line
->Type
!= NewLineTypeDefault
) {
1786 // newline character is at most 4 bytes ( two Unicode characters )
1789 if (Line
->Buffer
!= NULL
&& Line
->Size
!= 0) {
1790 if (FileBuffer
.FileType
== FileTypeAscii
) {
1791 Length
+= Line
->Size
;
1793 Length
+= (Line
->Size
* 2);
1796 // end if FileTypeAscii
1801 // no cache room left, so write cache to disk
1803 if (LeftSize
< Length
) {
1804 Size
= TotalSize
- LeftSize
;
1805 Status
= ShellWriteFile (FileHandle
, &Size
, Cache
);
1806 if (EFI_ERROR (Status
)) {
1807 ShellDeleteFile (&FileHandle
);
1809 return EFI_LOAD_ERROR
;
1812 LeftSize
= TotalSize
;
1815 if (Line
->Buffer
!= NULL
&& Line
->Size
!= 0) {
1816 if (FileBuffer
.FileType
== FileTypeAscii
) {
1817 UnicodeToAscii (Line
->Buffer
, Line
->Size
, Ptr
);
1818 Length
= Line
->Size
;
1820 Length
= (Line
->Size
* 2);
1821 CopyMem (Ptr
, (CHAR8
*) Line
->Buffer
, Length
);
1824 // end if FileTypeAscii
1831 // end of if Line -> Buffer != NULL && Line -> Size != 0
1833 // if not the last line , write return buffer to disk
1835 if (Link
->ForwardLink
!= FileBuffer
.ListHead
) {
1836 GetNewLine (Type
, NewLineBuffer
, &NewLineSize
);
1837 CopyMem (Ptr
, (CHAR8
*) NewLineBuffer
, NewLineSize
);
1840 LeftSize
-= NewLineSize
;
1846 if (TotalSize
!= LeftSize
) {
1847 Size
= TotalSize
- LeftSize
;
1848 Status
= ShellWriteFile (FileHandle
, &Size
, Cache
);
1849 if (EFI_ERROR (Status
)) {
1850 ShellDeleteFile (&FileHandle
);
1852 return EFI_LOAD_ERROR
;
1858 ShellCloseFile(&FileHandle
);
1860 FileBuffer
.FileModified
= FALSE
;
1863 // set status string
1865 Str
= CatSPrint (NULL
, L
"%d Lines Wrote", NumLines
);
1867 return EFI_OUT_OF_RESOURCES
;
1870 StatusBarSetStatusString (Str
);
1871 SHELL_FREE_NON_NULL (Str
);
1874 // now everything is ready , you can set the new file name to filebuffer
1876 if (FileName
!= NULL
&& FileBuffer
.FileName
!= NULL
&& StrCmp (FileName
, FileBuffer
.FileName
) != 0) {
1880 FileBufferSetFileName (FileName
);
1881 if (FileBuffer
.FileName
== NULL
) {
1882 ShellDeleteFile (&FileHandle
);
1883 return EFI_OUT_OF_RESOURCES
;
1887 FileBuffer
.ReadOnly
= FALSE
;
1892 Scroll cursor to left 1 character position.
1894 @retval EFI_SUCCESS The operation was successful.
1898 FileBufferScrollLeft (
1902 EFI_EDITOR_LINE
*Line
;
1906 Line
= FileBuffer
.CurrentLine
;
1908 FRow
= FileBuffer
.FilePosition
.Row
;
1909 FCol
= FileBuffer
.FilePosition
.Column
;
1912 // if already at start of this line, so move to the end of previous line
1916 // has previous line
1918 if (Line
->Link
.BackLink
!= FileBuffer
.ListHead
) {
1920 Line
= CR (Line
->Link
.BackLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1921 FCol
= Line
->Size
+ 1;
1927 // if not at start of this line, just move to previous column
1932 FileBufferMovePosition (FRow
, FCol
);
1938 Delete a char in line
1940 @param[in, out] Line The line to delete in.
1941 @param[in] Pos Position to delete the char at ( start from 0 ).
1946 IN OUT EFI_EDITOR_LINE
*Line
,
1953 // move the latter characters front
1955 for (Index
= Pos
- 1; Index
< Line
->Size
; Index
++) {
1956 Line
->Buffer
[Index
] = Line
->Buffer
[Index
+ 1];
1963 Concatenate Src into Dest.
1965 @param[in, out] Dest Destination string
1966 @param[in] Src Src String.
1971 IN OUT EFI_EDITOR_LINE
*Dest
,
1972 IN EFI_EDITOR_LINE
*Src
1980 Dest
->Buffer
[Size
] = 0;
1983 // concatenate the two strings
1985 Str
= CatSPrint (NULL
, L
"%s%s", Dest
->Buffer
, Src
->Buffer
);
1987 Dest
->Buffer
= NULL
;
1991 Dest
->Size
= Size
+ Src
->Size
;
1992 Dest
->TotalSize
= Dest
->Size
;
1994 FreePool (Dest
->Buffer
);
1995 FreePool (Src
->Buffer
);
1998 // put str to dest->buffer
2004 Delete the previous character.
2006 @retval EFI_SUCCESS The delete was successful.
2007 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2011 FileBufferDoBackspace (
2015 EFI_EDITOR_LINE
*Line
;
2016 EFI_EDITOR_LINE
*End
;
2020 FileColumn
= FileBuffer
.FilePosition
.Column
;
2022 Line
= FileBuffer
.CurrentLine
;
2027 if (FileColumn
== 1) {
2031 if (FileBuffer
.FilePosition
.Row
== 1) {
2035 FileBufferScrollLeft ();
2037 Line
= FileBuffer
.CurrentLine
;
2038 Link
= Line
->Link
.ForwardLink
;
2039 End
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2042 // concatenate this line with previous line
2044 LineCat (Line
, End
);
2045 if (Line
->Buffer
== NULL
) {
2046 return EFI_OUT_OF_RESOURCES
;
2049 // remove End from line list
2051 RemoveEntryList (&End
->Link
);
2054 FileBuffer
.NumLines
--;
2056 FileBufferNeedRefresh
= TRUE
;
2057 FileBufferOnlyLineNeedRefresh
= FALSE
;
2061 // just delete the previous character
2063 LineDeleteAt (Line
, FileColumn
- 1);
2064 FileBufferScrollLeft ();
2065 FileBufferOnlyLineNeedRefresh
= TRUE
;
2068 if (!FileBuffer
.FileModified
) {
2069 FileBuffer
.FileModified
= TRUE
;
2076 Add a return into line at current position.
2078 @retval EFI_SUCCESS The insetrion of the character was successful.
2079 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2083 FileBufferDoReturn (
2087 EFI_EDITOR_LINE
*Line
;
2088 EFI_EDITOR_LINE
*NewLine
;
2095 FileBufferNeedRefresh
= TRUE
;
2096 FileBufferOnlyLineNeedRefresh
= FALSE
;
2098 Line
= FileBuffer
.CurrentLine
;
2100 FileColumn
= FileBuffer
.FilePosition
.Column
;
2102 NewLine
= AllocateZeroPool (sizeof (EFI_EDITOR_LINE
));
2103 if (NewLine
== NULL
) {
2104 return EFI_OUT_OF_RESOURCES
;
2107 NewLine
->Signature
= LINE_LIST_SIGNATURE
;
2108 NewLine
->Size
= Line
->Size
- FileColumn
+ 1;
2109 NewLine
->TotalSize
= NewLine
->Size
;
2110 NewLine
->Buffer
= CatSPrint (NULL
, L
"\0");
2111 if (NewLine
->Buffer
== NULL
) {
2112 return EFI_OUT_OF_RESOURCES
;
2115 NewLine
->Type
= NewLineTypeDefault
;
2117 if (NewLine
->Size
> 0) {
2119 // UNICODE + CHAR_NULL
2121 Buffer
= AllocateZeroPool (2 * (NewLine
->Size
+ 1));
2122 if (Buffer
== NULL
) {
2123 FreePool (NewLine
->Buffer
);
2125 return EFI_OUT_OF_RESOURCES
;
2128 FreePool (NewLine
->Buffer
);
2130 NewLine
->Buffer
= Buffer
;
2132 for (Index
= 0; Index
< NewLine
->Size
; Index
++) {
2133 NewLine
->Buffer
[Index
] = Line
->Buffer
[Index
+ FileColumn
- 1];
2136 NewLine
->Buffer
[NewLine
->Size
] = CHAR_NULL
;
2138 Line
->Buffer
[FileColumn
- 1] = CHAR_NULL
;
2139 Line
->Size
= FileColumn
- 1;
2142 // increase NumLines
2144 FileBuffer
.NumLines
++;
2147 // insert it into the correct position of line list
2149 NewLine
->Link
.BackLink
= &(Line
->Link
);
2150 NewLine
->Link
.ForwardLink
= Line
->Link
.ForwardLink
;
2151 Line
->Link
.ForwardLink
->BackLink
= &(NewLine
->Link
);
2152 Line
->Link
.ForwardLink
= &(NewLine
->Link
);
2155 // move cursor to the start of next line
2157 Row
= FileBuffer
.FilePosition
.Row
+ 1;
2160 FileBufferMovePosition (Row
, Col
);
2163 // set file is modified
2165 if (!FileBuffer
.FileModified
) {
2166 FileBuffer
.FileModified
= TRUE
;
2173 Delete current character from current line. This is the effect caused
2180 FileBufferDoDelete (
2184 EFI_EDITOR_LINE
*Line
;
2185 EFI_EDITOR_LINE
*Next
;
2189 Line
= FileBuffer
.CurrentLine
;
2190 FileColumn
= FileBuffer
.FilePosition
.Column
;
2195 if (FileColumn
>= Line
->Size
+ 1) {
2199 if (Line
->Link
.ForwardLink
== FileBuffer
.ListHead
) {
2203 // since last character,
2204 // so will add the next line to this line
2206 Link
= Line
->Link
.ForwardLink
;
2207 Next
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2208 LineCat (Line
, Next
);
2209 if (Line
->Buffer
== NULL
) {
2210 return EFI_OUT_OF_RESOURCES
;
2213 RemoveEntryList (&Next
->Link
);
2216 FileBuffer
.NumLines
--;
2218 FileBufferNeedRefresh
= TRUE
;
2219 FileBufferOnlyLineNeedRefresh
= FALSE
;
2223 // just delete current character
2225 LineDeleteAt (Line
, FileColumn
);
2226 FileBufferOnlyLineNeedRefresh
= TRUE
;
2229 if (!FileBuffer
.FileModified
) {
2230 FileBuffer
.FileModified
= TRUE
;
2237 Scroll cursor to right 1 character.
2239 @retval EFI_SUCCESS The operation was successful.
2243 FileBufferScrollRight (
2247 EFI_EDITOR_LINE
*Line
;
2251 Line
= FileBuffer
.CurrentLine
;
2252 if (Line
->Buffer
== NULL
) {
2256 FRow
= FileBuffer
.FilePosition
.Row
;
2257 FCol
= FileBuffer
.FilePosition
.Column
;
2260 // if already at end of this line, scroll it to the start of next line
2262 if (FCol
> Line
->Size
) {
2266 if (Line
->Link
.ForwardLink
!= FileBuffer
.ListHead
) {
2274 // if not at end of this line, just move to next column
2279 FileBufferMovePosition (FRow
, FCol
);
2285 Insert a char into line
2288 @param[in] Line The line to insert into.
2289 @param[in] Char The char to insert.
2290 @param[in] Pos The position to insert the char at ( start from 0 ).
2291 @param[in] StrSize The current string size ( include CHAR_NULL ),unit is Unicode character.
2293 @return The new string size ( include CHAR_NULL ) ( unit is Unicode character ).
2298 IN EFI_EDITOR_LINE
*Line
,
2305 CHAR16
*TempStringPtr
;
2308 Index
= (StrSize
) * 2;
2313 // do not have free space
2315 if (Line
->TotalSize
<= Line
->Size
) {
2316 Str
= ReallocatePool (Index
, Index
+ 16, Str
);
2321 Line
->TotalSize
+= 8;
2324 // move the later part of the string one character right
2326 TempStringPtr
= Str
;
2327 for (Index
= StrSize
; Index
> Pos
; Index
--) {
2328 TempStringPtr
[Index
] = TempStringPtr
[Index
- 1];
2331 // insert char into it.
2333 TempStringPtr
[Index
] = Char
;
2342 Add a character to the current line.
2344 @param[in] Char The Character to input.
2346 @retval EFI_SUCCESS The input was succesful.
2354 EFI_EDITOR_LINE
*Line
;
2357 Line
= FileBuffer
.CurrentLine
;
2360 // only needs to refresh current line
2362 FileBufferOnlyLineNeedRefresh
= TRUE
;
2365 // when is insert mode, or cursor is at end of this line,
2366 // so insert this character
2367 // or replace the character.
2369 FilePos
= FileBuffer
.FilePosition
.Column
- 1;
2370 if (FileBuffer
.ModeInsert
|| FilePos
+ 1 > Line
->Size
) {
2371 LineStrInsert (Line
, Char
, FilePos
, Line
->Size
+ 1);
2373 Line
->Buffer
[FilePos
] = Char
;
2376 // move cursor to right
2378 FileBufferScrollRight ();
2380 if (!FileBuffer
.FileModified
) {
2381 FileBuffer
.FileModified
= TRUE
;
2388 Handles inputs from characters (ASCII key + Backspace + return)
2390 @param[in] Char The input character.
2392 @retval EFI_SUCCESS The operation was successful.
2393 @retval EFI_LOAD_ERROR There was an error.
2394 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2398 FileBufferDoCharInput (
2399 IN CONST CHAR16 Char
2404 Status
= EFI_SUCCESS
;
2410 case CHAR_BACKSPACE
:
2411 Status
= FileBufferDoBackspace ();
2421 case CHAR_CARRIAGE_RETURN
:
2422 Status
= FileBufferDoReturn ();
2427 // DEAL WITH ASCII CHAR, filter out thing like ctrl+f
2429 if (Char
> 127 || Char
< 32) {
2430 Status
= StatusBarSetStatusString (L
"Unknown Command");
2432 Status
= FileBufferAddChar (Char
);
2443 Scroll cursor to the next line.
2445 @retval EFI_SUCCESS The operation was successful.
2449 FileBufferScrollDown (
2453 EFI_EDITOR_LINE
*Line
;
2457 Line
= FileBuffer
.CurrentLine
;
2458 if (Line
->Buffer
== NULL
) {
2462 FRow
= FileBuffer
.FilePosition
.Row
;
2463 FCol
= FileBuffer
.FilePosition
.Column
;
2468 if (Line
->Link
.ForwardLink
!= FileBuffer
.ListHead
) {
2470 Line
= CR (Line
->Link
.ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2473 // if the next line is not that long, so move to end of next line
2475 if (FCol
> Line
->Size
) {
2476 FCol
= Line
->Size
+ 1;
2483 FileBufferMovePosition (FRow
, FCol
);
2489 Scroll the cursor to previous line.
2491 @retval EFI_SUCCESS The operation was successful.
2495 FileBufferScrollUp (
2499 EFI_EDITOR_LINE
*Line
;
2503 Line
= FileBuffer
.CurrentLine
;
2505 FRow
= FileBuffer
.FilePosition
.Row
;
2506 FCol
= FileBuffer
.FilePosition
.Column
;
2509 // has previous line
2511 if (Line
->Link
.BackLink
!= FileBuffer
.ListHead
) {
2513 Line
= CR (Line
->Link
.BackLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2516 // if previous line is not that long, so move to the end of previous line
2518 if (FCol
> Line
->Size
) {
2519 FCol
= Line
->Size
+ 1;
2526 FileBufferMovePosition (FRow
, FCol
);
2532 Scroll cursor to next page.
2534 @retval EFI_SUCCESS The operation wa successful.
2538 FileBufferPageDown (
2542 EFI_EDITOR_LINE
*Line
;
2547 Line
= FileBuffer
.CurrentLine
;
2549 FRow
= FileBuffer
.FilePosition
.Row
;
2550 FCol
= FileBuffer
.FilePosition
.Column
;
2555 if (FileBuffer
.NumLines
>= FRow
+ (MainEditor
.ScreenSize
.Row
- 2)) {
2556 Gap
= (MainEditor
.ScreenSize
.Row
- 2);
2559 // MOVE CURSOR TO LAST LINE
2561 Gap
= FileBuffer
.NumLines
- FRow
;
2566 Line
= MoveLine (Gap
);
2569 // if that line, is not that long, so move to the end of that line
2571 if (Line
!= NULL
&& FCol
> Line
->Size
) {
2572 FCol
= Line
->Size
+ 1;
2577 FileBufferMovePosition (FRow
, FCol
);
2583 Scroll cursor to previous screen.
2585 @retval EFI_SUCCESS The operation was successful.
2593 EFI_EDITOR_LINE
*Line
;
2599 Line
= FileBuffer
.CurrentLine
;
2601 FRow
= FileBuffer
.FilePosition
.Row
;
2602 FCol
= FileBuffer
.FilePosition
.Column
;
2605 // has previous page
2607 if (FRow
> (MainEditor
.ScreenSize
.Row
- 2)) {
2608 Gap
= (MainEditor
.ScreenSize
.Row
- 2);
2611 // the first line of file will displayed on the first line of screen
2622 Line
= MoveLine (Retreat
);
2625 // if that line is not that long, so move to the end of that line
2627 if (Line
!= NULL
&& FCol
> Line
->Size
) {
2628 FCol
= Line
->Size
+ 1;
2633 FileBufferMovePosition (FRow
, FCol
);
2639 Scroll cursor to end of the current line.
2641 @retval EFI_SUCCESS The operation was successful.
2649 EFI_EDITOR_LINE
*Line
;
2653 Line
= FileBuffer
.CurrentLine
;
2655 FRow
= FileBuffer
.FilePosition
.Row
;
2658 // goto the last column of the line
2660 FCol
= Line
->Size
+ 1;
2662 FileBufferMovePosition (FRow
, FCol
);
2668 Dispatch input to different handler
2669 @param[in] Key The input key. One of:
2673 Direction key: up/down/left/right/pgup/pgdn
2677 @retval EFI_SUCCESS The dispatch was done successfully.
2678 @retval EFI_LOAD_ERROR The dispatch was not successful.
2679 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2683 FileBufferHandleInput (
2684 IN CONST EFI_INPUT_KEY
*Key
2689 Status
= EFI_SUCCESS
;
2691 switch (Key
->ScanCode
) {
2693 // ordinary key input
2696 if (!FileBuffer
.ReadOnly
) {
2697 Status
= FileBufferDoCharInput (Key
->UnicodeChar
);
2699 Status
= StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
2708 Status
= FileBufferScrollUp ();
2715 Status
= FileBufferScrollDown ();
2722 Status
= FileBufferScrollRight ();
2729 Status
= FileBufferScrollLeft ();
2736 Status
= FileBufferPageUp ();
2742 case SCAN_PAGE_DOWN
:
2743 Status
= FileBufferPageDown ();
2750 if (!FileBuffer
.ReadOnly
) {
2751 Status
= FileBufferDoDelete ();
2753 Status
= StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
2762 FileBufferMovePosition (FileBuffer
.FilePosition
.Row
, 1);
2763 Status
= EFI_SUCCESS
;
2770 Status
= FileBufferEnd ();
2777 FileBuffer
.ModeInsert
= (BOOLEAN
)!FileBuffer
.ModeInsert
;
2778 Status
= EFI_SUCCESS
;
2782 Status
= StatusBarSetStatusString (L
"Unknown Command");
2790 Check user specified FileRow is above current screen.
2792 @param[in] FileRow The row of file position ( start from 1 ).
2794 @retval TRUE It is above the current screen.
2795 @retval FALSE It is not above the current screen.
2799 AboveCurrentScreen (
2804 // if is to the above of the screen
2806 if (FileRow
< FileBuffer
.LowVisibleRange
.Row
) {
2814 Check user specified FileRow is under current screen.
2816 @param[in] FileRow The row of file position ( start from 1 ).
2818 @retval TRUE It is under the current screen.
2819 @retval FALSE It is not under the current screen.
2823 UnderCurrentScreen (
2828 // if is to the under of the screen
2830 if (FileRow
> FileBuffer
.LowVisibleRange
.Row
+ (MainEditor
.ScreenSize
.Row
- 2) - 1) {
2838 Check user specified FileCol is left to current screen.
2840 @param[in] FileCol The column of file position ( start from 1 ).
2842 @retval TRUE It is to the left.
2843 @retval FALSE It is not to the left.
2852 // if is to the left of the screen
2854 if (FileCol
< FileBuffer
.LowVisibleRange
.Column
) {
2862 Check user specified FileCol is right to current screen.
2864 @param[in] FileCol The column of file position ( start from 1 ).
2866 @retval TRUE It is to the right.
2867 @retval FALSE It is not to the right.
2871 RightCurrentScreen (
2876 // if is to the right of the screen
2878 if (FileCol
> FileBuffer
.LowVisibleRange
.Column
+ MainEditor
.ScreenSize
.Column
- 1) {
2886 Advance/Retreat lines and set CurrentLine in FileBuffer to it
2888 @param[in] Count The line number to advance/retreat
2892 @retval NULL An error occured.
2893 @return The line after advance/retreat.
2901 EFI_EDITOR_LINE
*Line
;
2905 AbsCount
= (UINTN
)ABS(Count
);
2906 Line
= InternalEditorMiscLineRetreat (AbsCount
,MainEditor
.FileBuffer
->CurrentLine
,MainEditor
.FileBuffer
->ListHead
);
2908 Line
= InternalEditorMiscLineAdvance ((UINTN
)Count
,MainEditor
.FileBuffer
->CurrentLine
,MainEditor
.FileBuffer
->ListHead
);
2915 MainEditor
.FileBuffer
->CurrentLine
= Line
;
2921 According to cursor's file position, adjust screen display
2923 @param[in] NewFilePosRow The row of file position ( start from 1 ).
2924 @param[in] NewFilePosCol The column of file position ( start from 1 ).
2928 FileBufferMovePosition (
2929 IN CONST UINTN NewFilePosRow
,
2930 IN CONST UINTN NewFilePosCol
2942 // CALCULATE gap between current file position and new file position
2944 RowGap
= NewFilePosRow
- FileBuffer
.FilePosition
.Row
;
2945 ColGap
= NewFilePosCol
- FileBuffer
.FilePosition
.Column
;
2947 Under
= UnderCurrentScreen (NewFilePosRow
);
2948 Above
= AboveCurrentScreen (NewFilePosRow
);
2950 // if is below current screen
2954 // display row will be unchanged
2956 FileBuffer
.FilePosition
.Row
= NewFilePosRow
;
2960 // has enough above line, so display row unchanged
2961 // not has enough above lines, so the first line is at the
2962 // first display line
2964 if (NewFilePosRow
< (FileBuffer
.DisplayPosition
.Row
- 1)) {
2965 FileBuffer
.DisplayPosition
.Row
= NewFilePosRow
+ 1;
2968 FileBuffer
.FilePosition
.Row
= NewFilePosRow
;
2971 // in current screen
2973 FileBuffer
.FilePosition
.Row
= NewFilePosRow
;
2975 Abs
= (UINTN
)ABS(RowGap
);
2976 FileBuffer
.DisplayPosition
.Row
-= Abs
;
2978 FileBuffer
.DisplayPosition
.Row
+= RowGap
;
2983 FileBuffer
.LowVisibleRange
.Row
= FileBuffer
.FilePosition
.Row
- (FileBuffer
.DisplayPosition
.Row
- 2);
2985 Right
= RightCurrentScreen (NewFilePosCol
);
2986 Left
= LeftCurrentScreen (NewFilePosCol
);
2989 // if right to current screen
2993 // display column will be changed to end
2995 FileBuffer
.DisplayPosition
.Column
= MainEditor
.ScreenSize
.Column
;
2996 FileBuffer
.FilePosition
.Column
= NewFilePosCol
;
3000 // has enough left characters , so display row unchanged
3001 // not has enough left characters,
3002 // so the first character is at the first display column
3004 if (NewFilePosCol
< (FileBuffer
.DisplayPosition
.Column
)) {
3005 FileBuffer
.DisplayPosition
.Column
= NewFilePosCol
;
3008 FileBuffer
.FilePosition
.Column
= NewFilePosCol
;
3011 // in current screen
3013 FileBuffer
.FilePosition
.Column
= NewFilePosCol
;
3015 Abs
= (UINTN
)(-ColGap
);
3016 FileBuffer
.DisplayPosition
.Column
-= Abs
;
3018 FileBuffer
.DisplayPosition
.Column
+= ColGap
;
3023 FileBuffer
.LowVisibleRange
.Column
= FileBuffer
.FilePosition
.Column
- (FileBuffer
.DisplayPosition
.Column
- 1);
3026 // let CurrentLine point to correct line;
3028 FileBuffer
.CurrentLine
= MoveCurrentLine (RowGap
);
3033 Cut current line out and return a pointer to it.
3035 @param[out] CutLine Upon a successful return pointer to the pointer to
3036 the allocated cut line.
3038 @retval EFI_SUCCESS The cut was successful.
3039 @retval EFI_NOT_FOUND There was no selection to cut.
3040 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3045 OUT EFI_EDITOR_LINE
**CutLine
3048 EFI_EDITOR_LINE
*Line
;
3049 EFI_EDITOR_LINE
*NewLine
;
3053 if (FileBuffer
.ReadOnly
) {
3054 StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
3058 Line
= FileBuffer
.CurrentLine
;
3061 // if is the last dummy line, SO CAN not cut
3063 if (StrCmp (Line
->Buffer
, L
"\0") == 0 && Line
->Link
.ForwardLink
== FileBuffer
.ListHead
3069 // LAST LINE AND NOTHING ON THIS LINE, SO CUT NOTHING
3071 StatusBarSetStatusString (L
"Nothing to Cut");
3072 return EFI_NOT_FOUND
;
3075 // if is the last line, so create a dummy line
3077 if (Line
->Link
.ForwardLink
== FileBuffer
.ListHead
) {
3080 // create a new line
3082 NewLine
= FileBufferCreateLine ();
3083 if (NewLine
== NULL
) {
3084 return EFI_OUT_OF_RESOURCES
;
3088 FileBuffer
.NumLines
--;
3089 Row
= FileBuffer
.FilePosition
.Row
;
3094 FileBuffer
.CurrentLine
= CR (
3095 FileBuffer
.CurrentLine
->Link
.ForwardLink
,
3101 RemoveEntryList (&Line
->Link
);
3103 FileBuffer
.Lines
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
3105 FileBufferMovePosition (Row
, Col
);
3107 FileBuffer
.FileModified
= TRUE
;
3108 FileBufferNeedRefresh
= TRUE
;
3109 FileBufferOnlyLineNeedRefresh
= FALSE
;
3117 Paste a line into line list.
3119 @retval EFI_SUCCESS The paste was successful.
3120 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3124 FileBufferPasteLine (
3128 EFI_EDITOR_LINE
*Line
;
3129 EFI_EDITOR_LINE
*NewLine
;
3134 // if nothing is on clip board
3137 if (MainEditor
.CutLine
== NULL
) {
3141 // read only file can not be pasted on
3143 if (FileBuffer
.ReadOnly
) {
3144 StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
3148 NewLine
= LineDup (MainEditor
.CutLine
);
3149 if (NewLine
== NULL
) {
3150 return EFI_OUT_OF_RESOURCES
;
3153 // insert it above current line
3155 Line
= FileBuffer
.CurrentLine
;
3156 NewLine
->Link
.BackLink
= Line
->Link
.BackLink
;
3157 NewLine
->Link
.ForwardLink
= &Line
->Link
;
3159 Line
->Link
.BackLink
->ForwardLink
= &NewLine
->Link
;
3160 Line
->Link
.BackLink
= &NewLine
->Link
;
3162 FileBuffer
.NumLines
++;
3163 FileBuffer
.CurrentLine
= NewLine
;
3165 FileBuffer
.Lines
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
3171 Row
= FileBuffer
.FilePosition
.Row
;
3173 FileBufferMovePosition (Row
, Col
);
3176 // after paste, set some value so that refresh knows to do something
3178 FileBuffer
.FileModified
= TRUE
;
3179 FileBufferNeedRefresh
= TRUE
;
3180 FileBufferOnlyLineNeedRefresh
= FALSE
;
3186 Search string from current position on in file
3188 @param[in] Str The search string.
3189 @param[in] Offset The offset from current position.
3191 @retval EFI_SUCCESS The operation was successful.
3192 @retval EFI_NOT_FOUND The string Str was not found.
3197 IN CONST CHAR16
*Str
,
3198 IN CONST UINTN Offset
3205 EFI_EDITOR_LINE
*Line
;
3214 // search if in current line
3216 Current
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
- 1 + Offset
;
3218 if (Current
>= (FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.CurrentLine
->Size
)) {
3222 Current
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.CurrentLine
->Size
;
3227 CharPos
= StrStr (Current
, Str
);
3228 if (CharPos
!= NULL
) {
3229 Position
= CharPos
- Current
+ 1;
3237 Column
= (Position
- 1) + FileBuffer
.FilePosition
.Column
+ Offset
;
3238 Row
= FileBuffer
.FilePosition
.Row
;
3241 // not found so find through next lines
3243 Link
= FileBuffer
.CurrentLine
->Link
.ForwardLink
;
3245 Row
= FileBuffer
.FilePosition
.Row
+ 1;
3246 while (Link
!= FileBuffer
.ListHead
) {
3247 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
3248 // Position = StrStr (Line->Buffer, Str);
3249 CharPos
= StrStr (Line
->Buffer
, Str
);
3250 if (CharPos
!= NULL
) {
3251 Position
= CharPos
- Line
->Buffer
+ 1;
3264 Link
= Link
->ForwardLink
;
3267 if (Link
== FileBuffer
.ListHead
) {
3275 return EFI_NOT_FOUND
;
3278 FileBufferMovePosition (Row
, Column
);
3281 // call refresh to fresh edit area,
3282 // because the outer may loop to find multiply occurrence of this string
3284 FileBufferRefresh ();
3290 Replace SearchLen characters from current position on with Replace.
3292 This will modify the current buffer at the current position.
3294 @param[in] Replace The string to replace.
3295 @param[in] SearchLen Search string's length.
3297 @retval EFI_SUCCESS The operation was successful.
3298 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3303 IN CONST CHAR16
*Replace
,
3304 IN CONST UINTN SearchLen
3314 ReplaceLen
= StrLen (Replace
);
3316 OldSize
= FileBuffer
.CurrentLine
->Size
+ 1;
3318 // include CHAR_NULL
3320 NewSize
= OldSize
+ (ReplaceLen
- SearchLen
);
3322 if (ReplaceLen
> SearchLen
) {
3324 // do not have the enough space
3326 if (FileBuffer
.CurrentLine
->TotalSize
+ 1 <= NewSize
) {
3327 FileBuffer
.CurrentLine
->Buffer
= ReallocatePool (
3330 FileBuffer
.CurrentLine
->Buffer
3332 FileBuffer
.CurrentLine
->TotalSize
= NewSize
- 1;
3335 if (FileBuffer
.CurrentLine
->Buffer
== NULL
) {
3336 return EFI_OUT_OF_RESOURCES
;
3339 // the end CHAR_NULL character;
3341 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ (NewSize
- 1);
3342 Gap
= ReplaceLen
- SearchLen
;
3345 // keep the latter part
3347 for (Index
= 0; Index
< (FileBuffer
.CurrentLine
->Size
- FileBuffer
.FilePosition
.Column
- SearchLen
+ 2); Index
++) {
3348 *Buffer
= *(Buffer
- Gap
);
3352 // set replace into it
3354 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
- 1;
3355 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3356 Buffer
[Index
] = Replace
[Index
];
3360 if (ReplaceLen
< SearchLen
) {
3361 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
- 1;
3363 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3364 Buffer
[Index
] = Replace
[Index
];
3367 Buffer
+= ReplaceLen
;
3368 Gap
= SearchLen
- ReplaceLen
;
3371 // set replace into it
3373 for (Index
= 0; Index
< (FileBuffer
.CurrentLine
->Size
- FileBuffer
.FilePosition
.Column
- ReplaceLen
+ 2); Index
++) {
3374 *Buffer
= *(Buffer
+ Gap
);
3379 if (ReplaceLen
== SearchLen
) {
3380 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
- 1;
3381 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3382 Buffer
[Index
] = Replace
[Index
];
3386 FileBuffer
.CurrentLine
->Size
+= (ReplaceLen
- SearchLen
);
3388 FileBufferOnlyLineNeedRefresh
= TRUE
;
3390 FileBuffer
.FileModified
= TRUE
;
3392 MainTitleBarRefresh (MainEditor
.FileBuffer
->FileName
, MainEditor
.FileBuffer
->FileType
, MainEditor
.FileBuffer
->ReadOnly
, MainEditor
.FileBuffer
->FileModified
, MainEditor
.ScreenSize
.Column
, MainEditor
.ScreenSize
.Row
, 0, 0);
3393 FileBufferRestorePosition ();
3394 FileBufferRefresh ();
3400 Move the mouse cursor position.
3402 @param[in] TextX The new x-coordinate.
3403 @param[in] TextY The new y-coordinate.
3407 FileBufferAdjustMousePosition (
3408 IN CONST INT32 TextX
,
3409 IN CONST INT32 TextY
3418 // TextX and TextY is mouse movement data returned by mouse driver
3419 // This function will change it to MousePosition
3422 // get absolute value
3428 CoordinateX
= FileBuffer
.MousePosition
.Column
;
3429 CoordinateY
= FileBuffer
.MousePosition
.Row
;
3432 CoordinateX
+= TextX
;
3434 if (CoordinateX
>= AbsX
) {
3435 CoordinateX
-= AbsX
;
3442 CoordinateY
+= TextY
;
3444 if (CoordinateY
>= AbsY
) {
3445 CoordinateY
-= AbsY
;
3451 // check whether new mouse column position is beyond screen
3452 // if not, adjust it
3454 if (CoordinateX
>= 1 && CoordinateX
<= MainEditor
.ScreenSize
.Column
) {
3455 FileBuffer
.MousePosition
.Column
= CoordinateX
;
3456 } else if (CoordinateX
< 1) {
3457 FileBuffer
.MousePosition
.Column
= 1;
3458 } else if (CoordinateX
> MainEditor
.ScreenSize
.Column
) {
3459 FileBuffer
.MousePosition
.Column
= MainEditor
.ScreenSize
.Column
;
3462 // check whether new mouse row position is beyond screen
3463 // if not, adjust it
3465 if (CoordinateY
>= 2 && CoordinateY
<= (MainEditor
.ScreenSize
.Row
- 1)) {
3466 FileBuffer
.MousePosition
.Row
= CoordinateY
;
3467 } else if (CoordinateY
< 2) {
3468 FileBuffer
.MousePosition
.Row
= 2;
3469 } else if (CoordinateY
> (MainEditor
.ScreenSize
.Row
- 1)) {
3470 FileBuffer
.MousePosition
.Row
= (MainEditor
.ScreenSize
.Row
- 1);
3476 Search and replace operation.
3478 @param[in] SearchStr The string to search for.
3479 @param[in] ReplaceStr The string to replace with.
3480 @param[in] Offset The column to start at.
3484 FileBufferReplaceAll (
3485 IN CHAR16
*SearchStr
,
3486 IN CHAR16
*ReplaceStr
,
3499 EFI_EDITOR_LINE
*Line
;
3503 SearchLen
= StrLen (SearchStr
);
3504 ReplaceLen
= StrLen (ReplaceStr
);
3506 Column
= FileBuffer
.FilePosition
.Column
+ Offset
- 1;
3508 if (Column
> FileBuffer
.CurrentLine
->Size
) {
3509 Column
= FileBuffer
.CurrentLine
->Size
;
3512 Link
= &(FileBuffer
.CurrentLine
->Link
);
3514 while (Link
!= FileBuffer
.ListHead
) {
3515 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
3516 CharPos
= StrStr (Line
->Buffer
+ Column
, SearchStr
);
3517 if (CharPos
!= NULL
) {
3518 Position
= CharPos
- Line
->Buffer
;// + Column;
3522 if (ReplaceLen
> SearchLen
) {
3523 OldSize
= Line
->Size
+ 1;
3525 // include CHAR_NULL
3527 NewSize
= OldSize
+ (ReplaceLen
- SearchLen
);
3530 // do not have the enough space
3532 if (Line
->TotalSize
+ 1 <= NewSize
) {
3533 Line
->Buffer
= ReallocatePool (
3538 Line
->TotalSize
= NewSize
- 1;
3541 if (Line
->Buffer
== NULL
) {
3542 return EFI_OUT_OF_RESOURCES
;
3545 // the end CHAR_NULL character;
3547 Buffer
= Line
->Buffer
+ (NewSize
- 1);
3548 Gap
= ReplaceLen
- SearchLen
;
3551 // keep the latter part
3553 for (Index
= 0; Index
< (Line
->Size
- Position
- SearchLen
+ 1); Index
++) {
3554 *Buffer
= *(Buffer
- Gap
);
3558 } else if (ReplaceLen
< SearchLen
){
3559 Buffer
= Line
->Buffer
+ Position
+ ReplaceLen
;
3560 Gap
= SearchLen
- ReplaceLen
;
3562 for (Index
= 0; Index
< (Line
->Size
- Position
- ReplaceLen
+ 1); Index
++) {
3563 *Buffer
= *(Buffer
+ Gap
);
3567 ASSERT(ReplaceLen
== SearchLen
);
3570 // set replace into it
3572 Buffer
= Line
->Buffer
+ Position
;
3573 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3574 Buffer
[Index
] = ReplaceStr
[Index
];
3577 Line
->Size
+= (ReplaceLen
- SearchLen
);
3578 Column
+= ReplaceLen
;
3584 Link
= Link
->ForwardLink
;
3588 // call refresh to fresh edit area
3590 FileBuffer
.FileModified
= TRUE
;
3591 FileBufferNeedRefresh
= TRUE
;
3592 FileBufferRefresh ();
3598 Set the modified state to TRUE.
3602 FileBufferSetModified (
3606 FileBuffer
.FileModified
= TRUE
;
3611 Get the size of the open buffer.
3613 @retval The size in bytes.
3616 FileBufferGetTotalSize (
3622 EFI_EDITOR_LINE
*Line
;
3625 // calculate the total size of whole line list's buffer
3627 if (FileBuffer
.Lines
== NULL
) {
3632 FileBuffer
.ListHead
->BackLink
,
3638 // one line at most 0x50
3640 Size
= SHELL_EDIT_MAX_LINE_SIZE
* (FileBuffer
.NumLines
- 1) + Line
->Size
;