Skip to content

Commit

Permalink
refactor(main): add handle_input to share command handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
FranckG28 committed Dec 29, 2023
1 parent 066d906 commit a7d4f05
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
8 changes: 7 additions & 1 deletion include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ void run_batch_mode(char *argv[], int argc);
*/
void run_interactive_mode();

#endif //MAIN_H
/**
* @brief Handle the input.
* @param input The input to handle.
*/
void handle_input(char *input);

#endif // MAIN_H
50 changes: 32 additions & 18 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
#include "main.h"

void run_interactive_mode() {
while (1) {
print_prompt();
void handle_input(char *input)
{
if (input == NULL || is_all_whitespace(input))
return;

add_to_command_history(input);
preprocess_input(input);
command_sequence_init(input);
}

char* input = read_input();
if (input == NULL || is_all_whitespace(input)) continue;
void run_interactive_mode()
{
while (1)
{
print_prompt();

add_to_command_history(input);
preprocess_input(input);
command_sequence_init(input);
char *input = read_input();
handle_input(input);
}
}

void run_batch_mode(char *argv[], int argc) {
char* input = argv[2];
for (int i = 3; i < argc; i++) {
void run_batch_mode(char *argv[], int argc)
{
char *input = argv[2];
for (int i = 3; i < argc; i++)
{
input = strcat(input, " ");
input = strcat(input, argv[i]);
}
add_to_command_history(input);
preprocess_input(input);
command_sequence_init(input);
handle_input(input);
}

int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
initialize_command_history();

if (argc > 2 && (strcmp(argv[1], "-c") && strcmp(argv[1], "--command")) == 0) {
if (argc > 2 && (strcmp(argv[1], "-c") && strcmp(argv[1], "--command")) == 0)
{
run_batch_mode(argv, argc);
} else if(argc > 1) {
}
else if (argc > 1)
{
print_info("Usage: %s [-c \"command\"]", argv[0]);
return EXIT_FAILURE;
} else {
}
else
{
run_interactive_mode();
}

Expand Down

0 comments on commit a7d4f05

Please sign in to comment.