-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
83 lines (80 loc) · 2.7 KB
/
main.c
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
#include <stdio.h>
#include "ArgsLib/ArgsParse.h"
#include "CommandLib/CommandHelper.h"
#include "CommandLib/Console.h"
int main()
{
Console * console = SetupConsole();
unsigned char ch;
ConsoleAction status;
bool shouldExit = false;
while(shouldExit == false){
// Read a user input character
ch = _getch();
// Process the input
status = console->onKeyPress(ch);
// If user wants to execute a command
if(status == CONSOLE_EXEC)
{
// Unix like parsing for command line.
CommandLineArgs * args = ShlexParse(console->buffer);
if(args->argc > 0)
{
printArgs(args);
// Find command from registry
CommandRegistry * command = findCommand(args->argv[0]);
if(command)
{
// Add to history.
console->AddToHistory(console->buffer);
// Call command specific handler.
command->CallBack(args);
// TODO: Check for return code.
}
// Clean up arguments
cleanArgs(args);
}
// Housekeeping after command has executed.
console->onCommandExec();
}
// Break using Ctrl + C
else if (status == CONSOLE_BREAK)
shouldExit = true;
// Handle Tabbed key
else if (status == CONSOLE_COMMAND_SUGGEST)
{
// If the user has not yet typed first word.
if(!console->isSpacePressed)
PrintRegistry(console->buffer);
// Command specific usage.
else
{
CommandLineArgs * args = ShlexParse(console->buffer);
if(args->argc > 0)
{
CommandRegistry * command = findCommand(args->argv[0]);
if(command) CommandSuggestions(command->usagePattern, console->isSpacePressed);
else printf("\a"); // System bell.
fflush(stdout);
}
}
// Command has not yet executed, so restore user typed buffer after showing suggestions.
console->onHintShow();
}
// User is trying to input a file path, give suggestions.
else if(status == CONSOLE_FILE_SUGGEST)
{
console->showCompletion();
console->onHintShow();
}
// Load from history.
else if (status == CONSOLE_HISTORY_LOAD )
{
console->LoadFromHistory();
console->onHintShow();
}
}
printf("\nPress any key to exit ...");
getchar();
return 0;
}