-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuLoop.c
More file actions
270 lines (264 loc) · 11.6 KB
/
Copy pathMenuLoop.c
File metadata and controls
270 lines (264 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <Library/BaseLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Protocol/SimpleTextInEx.h>
#include <Uefi.h>
#include <stdbool.h>
#include "Errors.h"
#include "FileEditor.h"
#include "FileUtils.h"
#include "MenuLoop.h"
#include "Utils.h"
/**
Main interaction loop.
@param[in,out] Notes Dynamic array containing the notes to be displayed to
the user. Updated when the user creates or deletes a file.
@param[in,out] NotesCount The amount of notes used when displaying the notes to
the user and interacting with the array. Updated when
the user creates or deletes a file.
@retval ERROR_UNKNOWN An unknown error was occured.
@retval other An error was encountered.
**/
EFI_STATUS
MenuLoop
(
IN OUT CHAR16 **Notes,
IN OUT UINTN NotesCount
) {
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *ConInEx;
EFI_STATUS err;
UINTN i;
UINTN bufferCount = 1;
UINTN bufferLen = 0;
CHAR16* buffer;
err = gBS->HandleProtocol(
gST->ConsoleInHandle,
&gEfiSimpleTextInputExProtocolGuid,
(void **)&ConInEx
);
if(EFI_ERROR(err)) {
Print(L"Unable to get EfiSimpleTextInputExProtocol %r __LINE__=%d __FILE__=%a\n", err, __LINE__, __FILE__);
pauseForKey();
return EFI_UNSUPPORTED;
}
err = EFI_SUCCESS;
buffer = AllocatePool(bufferCount * sizeof(CHAR16));
if(buffer == NULL) {
Print(L"Out Of Memory.");
pauseForKey();
return EFI_OUT_OF_RESOURCES;
}
buffer[0] = L'\0';
gST->ConOut->ClearScreen(gST->ConOut);
gST->ConOut->EnableCursor(gST->ConOut, true);
while(true) {
EFI_KEY_DATA keyData;
CHAR16 ch;
UINTN eventIndex;
gBS->WaitForEvent(1, &gST->ConIn->WaitForKey, &eventIndex);
err = ConInEx->ReadKeyStrokeEx(ConInEx, &keyData);
if(err == EFI_NOT_READY) {err = EFI_SUCCESS; continue;} // normal
if(EFI_ERROR(err)) {
Print(L"ReadKeyStrokeEx error %r __LINE__=%d __FILE__=%a\n", err, __LINE__, __FILE__);
pauseForKey();
return err;
}
ch = keyData.Key.UnicodeChar;
if (ch == CHAR_BACKSPACE) {
if (bufferLen > 0) {
buffer[--bufferLen] = L'\0';
}
} else if (ch == CHAR_CARRIAGE_RETURN) {
Print(L"\n");
if(StrCmp(buffer, L"exit") == 0) {
gST->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
}
if(StrCmp(buffer, L"reboot") == 0) {
gST->RuntimeServices->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
}
if(StrCmp(buffer, L"EfiResetWarm") == 0) {
gST->RuntimeServices->ResetSystem(EfiResetWarm, EFI_SUCCESS, 0, NULL);
}
if(StrCmp(buffer, L"help") == 0) {
Print(L"exit - Shutdown the system\n");
Print(L"reboot - Reboot the system\n");
Print(L"EfiResetWarm - Reboot the system using EfiResetWarm\n");
Print(L"help - Display this help message\n");
Print(L"e, edit <file> - Open the text editor\n");
Print(L"c, create <file> - Create a file\n");
Print(L"delete <file> - Delete a file\n");
Print(L"Press any key to go back to the prompt");
pauseForKey();
goto crCleanup;
}
CHAR16 **split;
UINTN SplitCount;
err = SplitString(buffer, SPACE, &split, &SplitCount);
if(EFI_ERROR(err)) {
Print(L"SplitString error: %r __LINE__=%d __FILE__=%a\n", err, __LINE__, __FILE__);
pauseForKey();
goto crCleanup;
}
if(SplitCount == 0) goto crCleanup;
if(SplitCount < 2) {
Print(L"Not enough arguments for %s\n", split[0]);
Print(L"Press any key to go back to the prompt");
pauseForKey();
goto crCleanup;
}
if(StrCmp(split[0], L"e") == 0 || StrCmp(split[0], L"edit") == 0) {
UINTN RawFileIndex = StrDecimalToUintn(split[1]);
if(RawFileIndex == 0 || RawFileIndex > NotesCount) {
Print(L"File does not exist"); // StrDecimalToUintn returns 0 when the string is not a string form of a UINTN but 0 is never going to be a file anyway so this handles all cases
// i guess if there are enough notes a integer overflow could happen, but on a 64 bit system (this is made for one) there would most likely be fat32 limits before it is even that close to the integer limit
Print(L"Press any key to go back to the prompt");
pauseForKey();
goto crCleanup;
}
err = OpenEditor(Notes[RawFileIndex - 1]);
if(EFI_ERROR(err)) {
Print(L"Error in OpenEditor %r", err);
pauseForKey();
}
err = EFI_SUCCESS; // this is most likely not really needed but it became convention in this repo before i thought about it
goto crCleanup;
}
if(StrCmp(split[0], L"c") == 0 || StrCmp(split[0], L"create") == 0) {
#define NOTES_DIR_PREFIX_LENGTH 8
#define TXT_EXTENTION_LENGTH 4
UINTN FilenameBufferLen = NOTES_DIR_PREFIX_LENGTH + TXT_EXTENTION_LENGTH + StrLen(split[1]);
CHAR16 *FilenameBuffer = AllocatePool(FilenameBufferLen * sizeof(CHAR16));
if (FilenameBuffer == NULL) {
Print(L"Out Of Memory.");
pauseForKey();
return EFI_OUT_OF_RESOURCES;
}
err = StrCpyS(FilenameBuffer, FilenameBufferLen, L"\\notes\\");
if(EFI_ERROR(err)) {
Print(L"Error in StrCpyS %r __LINE__=%d __FILE__=%a\n", err, __LINE__, __FILE__);
pauseForKey();
goto crCleanup;
}
err = StrCatS(FilenameBuffer, FilenameBufferLen, split[1]);
if(EFI_ERROR(err)) {
Print(L"Error in StrCatS %r __LINE__=%d __FILE__=%a\n", err, __LINE__, __FILE__);
pauseForKey();
goto crCleanup;
}
err = StrCatS(FilenameBuffer, FilenameBufferLen, L".txt");
if(EFI_ERROR(err)) {
Print(L"Error in StrCatS %r __LINE__=%d __FILE__=%a\n", err, __LINE__, __FILE__);
pauseForKey();
goto crCleanup;
}
err = EFI_SUCCESS;
WriteFile(FilenameBuffer, L"", 1);
FreePool(FilenameBuffer);
FilenameBuffer = NULL;
// Update Notes and NotesCount
if(NotesCount != 0) {
for(i=0; i < NotesCount; i++) {
FreePool(Notes[i]);
}
}
if(Notes != NULL) {
FreePool(Notes);
}
ListFiles(L"\\notes", &Notes, &NotesCount);
goto crCleanup;
}
if(StrCmp(split[0], L"delete") == 0) {
UINTN RawFileIndex = StrDecimalToUintn(split[1]);
if(RawFileIndex == 0 || RawFileIndex > NotesCount) {
Print(L"File does not exist"); // StrDecimalToUintn returns 0 when the string is not a string form of a UINTN but 0 is never going to be a file anyway so this handles all cases
Print(L"Press any key to go back to the prompt");
pauseForKey();
goto crCleanup;
}
#define NOTES_DIR_PREFIX_LENGTH 8
#define TXT_EXTENTION_LENGTH 4
UINTN FilenameBufferLen = NOTES_DIR_PREFIX_LENGTH + TXT_EXTENTION_LENGTH + StrLen(Notes[RawFileIndex - 1]);
CHAR16 *FilenameBuffer = AllocatePool(FilenameBufferLen * sizeof(CHAR16));
err = StrCpyS(FilenameBuffer, FilenameBufferLen, L"\\notes\\");
if(EFI_ERROR(err)) {
Print(L"Error in StrCpyS %r __LINE__=%d __FILE__=%a\n", err, __LINE__, __FILE__);
pauseForKey();
goto crCleanup;
}
err = StrCatS(FilenameBuffer, FilenameBufferLen, Notes[RawFileIndex - 1]);
if(EFI_ERROR(err)) {
Print(L"Error in StrCatS %r __LINE__=%d __FILE__=%a\n", err, __LINE__, __FILE__);
pauseForKey();
goto crCleanup;
}
err = DeleteFile(FilenameBuffer);
if(EFI_ERROR(err)) {
Print(L"Error in DeleteFile %r __LINE__=%d __FILE__=%a\n", err, __LINE__, __FILE__);
pauseForKey();
goto crCleanup;
}
FreePool(FilenameBuffer);
// Update Notes and NotesCount
if(NotesCount != 0) {
for(i=0; i < NotesCount; i++) {
FreePool(Notes[i]);
}
}
if(Notes != NULL) {
FreePool(Notes);
}
ListFiles(L"\\notes", &Notes, &NotesCount);
goto crCleanup;
}
// error case (fall through all commands)
Print(L"Error: %s is not a command. For a list of commands run 'help'\n", split[0]);
Print(L"Press any key to go back to the prompt");
pauseForKey();
crCleanup:
// reset buffer to empty
FreePool(buffer);
bufferCount = 1;
bufferLen = 0;
buffer = AllocatePool(bufferCount * sizeof(CHAR16));
if(buffer == NULL) {
Print(L"Out Of Memory.");
pauseForKey();
return EFI_OUT_OF_RESOURCES;
}
buffer[0] = L'\0';
} else if (ch == 0 || ch == CHAR_TAB) {
continue;
} else {
// grow buffer if needed (keep room for null terminator)
if (bufferLen + 2 > bufferCount) {
buffer = ReallocatePool(
(bufferCount * sizeof(CHAR16)),
(bufferCount * 2 * sizeof(CHAR16)),
buffer
);
if(buffer == NULL) {
Print(L"Out Of Memory.");
pauseForKey();
return EFI_OUT_OF_RESOURCES;
}
bufferCount *= 2;
}
buffer[bufferLen++] = ch;
buffer[bufferLen] = L'\0';
}
gST->ConOut->ClearScreen(gST->ConOut);
for (i=0; i < NotesCount; i++) {
CHAR16 *NoteWithoutExtention = RemoveExtension(Notes[i]);
if(NoteWithoutExtention == NULL) {
Print(L"Out Of Memory.");
pauseForKey();
return EFI_OUT_OF_RESOURCES;
}
Print(L"%d: %s\n", (i + 1), NoteWithoutExtention); // i + 1 is here since users might get confused with a zero based index
FreePool(NoteWithoutExtention);
}
Print(L"> %s", buffer);
}
return ERROR_UNKNOWN;
}