-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
40 lines (39 loc) · 1.45 KB
/
Copy pathmain.c
File metadata and controls
40 lines (39 loc) · 1.45 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "commands.h"
#include "utils.h"
int main() {
char input[256];
printf("Welcome to CLI Toolkit. Type 'exit' to quit.\n");
while (1) {
printf("> ");
if (fgets(input, sizeof(input), stdin) == NULL) break;
input[strcspn(input, "\n")] = 0; // remove newline
char* trimmed = trim(input);
if (strlen(trimmed) == 0) continue;
int count;
char** tokens = split_string(trimmed, &count);
if (strcmp(tokens[0], "hello") == 0) {
handle_hello();
} else if (strcmp(tokens[0], "add") == 0 && count >= 3) {
int a = atoi(tokens[1]);
int b = atoi(tokens[2]);
handle_add(a, b);
} else if (strcmp(tokens[0], "exit") == 0) {
handle_exit();
} else if (strcmp(tokens[0], "read") == 0 && count >= 2) {
handle_read_file(tokens[1]);
} else if (strcmp(tokens[0], "search") == 0 && count >= 3) {
handle_search_word(tokens[1], tokens[2]);
} else {
printf("Unknown command. Available: hello, add <a> <b>, read <file>, search <file> <word>, exit\n");
}
// Free tokens
for (int i = 0; i < count; i++) {
free(tokens[i]);
}
free(tokens);
}
return 0;
}