]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/EditTitleBar.c
pointer verification (not NULL) and buffer overrun fixes.
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / EditTitleBar.c
1 /** @file
2 Implements titlebar interface functions.
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 "EditTitleBar.h"
16 #include "UefiShellDebug1CommandsLib.h"
17
18 CHAR16 *Title = NULL;
19
20 /**
21 Initialize a title bar.
22
23 @param[in] Prompt The prompt to print in the title bar.
24
25 @retval EFI_SUCCESS The initialization was successful.
26 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
27 **/
28 EFI_STATUS
29 EFIAPI
30 MainTitleBarInit (
31 CONST CHAR16 *Prompt
32 )
33 {
34 SHELL_FREE_NON_NULL (Title);
35 if (Prompt == NULL) {
36 Title = CatSPrint (NULL, L"");
37 } else {
38 //
39 // set Title
40 //
41 Title = CatSPrint (NULL, L"%s", Prompt);
42 }
43 if (Title == NULL) {
44 return EFI_OUT_OF_RESOURCES;
45 }
46
47 return EFI_SUCCESS;
48 }
49
50 /**
51 Clean up the memory used.
52 **/
53 VOID
54 EFIAPI
55 MainTitleBarCleanup (
56 VOID
57 )
58 {
59 SHELL_FREE_NON_NULL (Title);
60 Title = NULL;
61 }
62
63 typedef struct {
64 UINT32 Foreground : 4;
65 UINT32 Background : 4;
66 } TITLE_BAR_COLOR_ATTRIBUTES;
67
68 typedef union {
69 TITLE_BAR_COLOR_ATTRIBUTES Colors;
70 UINTN Data;
71 } TITLE_BAR_COLOR_UNION;
72
73 /**
74 Refresh function for MainTitleBar
75
76 @param[in] FileName The open file's name (or NULL).
77 @param[in] FileType The type fo the file.
78 @param[in] ReadOnly TRUE if the file is read only. FALSE otherwise.
79 @param[in] Modified TRUE if the file was modified. FALSE otherwise.
80 @param[in] LastCol The last printable column.
81 @param[in] LastRow The last printable row.
82 @param[in] Offset The offset into the file. (only for mem/disk)
83 @param[in] Size The file's size. (only for mem/disk)
84
85 @retval EFI_SUCCESS The operation was successful.
86 **/
87 EFI_STATUS
88 EFIAPI
89 MainTitleBarRefresh (
90 IN CONST CHAR16 *FileName OPTIONAL,
91 IN CONST EDIT_FILE_TYPE FileType,
92 IN CONST BOOLEAN ReadOnly,
93 IN CONST BOOLEAN Modified,
94 IN CONST UINTN LastCol,
95 IN CONST UINTN LastRow,
96 IN CONST UINTN Offset,
97 IN CONST UINTN Size
98 )
99 {
100 TITLE_BAR_COLOR_UNION Orig;
101 TITLE_BAR_COLOR_UNION New;
102 CONST CHAR16 *FileNameTmp;
103 INTN TempInteger;
104
105
106 //
107 // backup the old screen attributes
108 //
109 Orig.Data = gST->ConOut->Mode->Attribute;
110 New.Colors.Foreground = Orig.Colors.Background;
111 New.Colors.Background = Orig.Colors.Foreground;
112
113 gST->ConOut->SetAttribute (gST->ConOut, New.Data);
114
115 //
116 // clear the title line
117 //
118 EditorClearLine (1, LastCol, LastRow);
119
120 if (Title != NULL) {
121 //
122 // print the new title bar prefix
123 //
124 ShellPrintEx (
125 0,
126 0,
127 L"%s ",
128 Title
129 );
130 }
131 if (FileName == NULL) {
132 gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);
133 return EFI_SUCCESS;
134 }
135 //
136 // First Extract the FileName from fullpath
137 //
138 FileNameTmp = FileName;
139 for (TempInteger = StrLen (FileNameTmp) - 1; TempInteger >= 0; TempInteger--) {
140 if (FileNameTmp[TempInteger] == L'\\') {
141 break;
142 }
143 }
144
145 FileNameTmp = FileNameTmp + TempInteger + 1;
146
147 //
148 // the space for file name is 20 characters
149 //
150 if (StrLen (FileNameTmp) <= 20) {
151 ShellPrintEx (-1,-1, L"%s ", FileNameTmp);
152 for (TempInteger = StrLen (FileNameTmp); TempInteger < 20; TempInteger++) {
153 ShellPrintEx (-1,-1, L" ");
154 }
155
156 } else {
157 for (TempInteger = 0; TempInteger < 17; TempInteger++) {
158 ShellPrintEx (-1,-1, L"%c", FileNameTmp[TempInteger]);
159 }
160 //
161 // print "..."
162 //
163 ShellPrintEx (-1,-1, L"... ");
164 }
165 //
166 // print file type field
167 //
168 switch (FileType){
169 case FileTypeAscii:
170 case FileTypeUnicode:
171 if (FileType == FileTypeAscii){
172 ShellPrintEx (-1,-1, L" UNICODE ");
173 }
174 //
175 // print read-only field for text files
176 //
177 if (ReadOnly) {
178 ShellPrintEx (-1,-1, L"ReadOnly ");
179 } else {
180 ShellPrintEx (-1,-1, L" ");
181 }
182 break;
183 case FileTypeDiskBuffer:
184 case FileTypeMemBuffer:
185 //
186 // Print the offset.
187 //
188 ASSERT(FALSE);
189 case FileTypeFileBuffer:
190 break;
191 }
192 //
193 // print modified field
194 //
195 if (Modified) {
196 ShellPrintEx (-1,-1, L"Modified");
197 }
198 //
199 // restore the old attribute
200 //
201 gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);
202
203 return EFI_SUCCESS;
204 }