diff --git a/cshell b/cshell index 9f7eeb1..b261d70 100755 Binary files a/cshell and b/cshell differ diff --git a/handleJSON.c b/eventually/handleJSON.c similarity index 92% rename from handleJSON.c rename to eventually/handleJSON.c index c35840d..4f8c0fe 100644 --- a/handleJSON.c +++ b/eventually/handleJSON.c @@ -1,4 +1,7 @@ -// This file exists because I hate myself too much to use cJSON +// I was gonna make a json parser from scratch but ive +// had my fill of writing lexers/parsers lately, +// might finish it eventually but for now itll be +// basic config files #include #include diff --git a/main.c b/src/cShell.c similarity index 87% rename from main.c rename to src/cShell.c index 2840528..ec3aaef 100644 --- a/main.c +++ b/src/cShell.c @@ -7,100 +7,11 @@ #include #include -#define INPUT_SIZE 1024 -#define VERSION 0.1f +#include "cShell.h" // TODO: research tab completion, very big quality of life feature -char *trimCWD(char *cwd) { - char *token = strtok(cwd, "/"); - char *trimmedCWD = NULL; - - while (token != NULL) { - trimmedCWD = token; - token = strtok(NULL, "/"); - } - - return trimmedCWD; -} - -void execute(char **args) { - pid_t pid = fork(); - if (pid == 0) { - execvp(args[0], args); - perror("execvp failed"); - exit(EXIT_FAILURE); - } else if (pid > 0) { - wait(NULL); - } else { - perror("fork failed"); - } -} - -void parse_input(char *input, char **args) { - char *token = strtok(input, " "); - int i = 0; - while (token != NULL) { - args[i++] = token; - token = strtok(NULL, " "); - } - args[i] = NULL; -} - -char* getCWD() { // current working directory - char *cwd = malloc(1024); - - if (cwd == NULL) { - perror("Malloc failed"); - return NULL; - } - - if (getcwd(cwd, 1024) != NULL) { - return cwd; - } else { - perror("Could not get current working directory"); - free(cwd); - return NULL; - } -} - -void listDirectorys(const char *path) { - struct dirent *entry; - DIR *dp = opendir(path); - - if (dp == NULL) { - perror("Directory read failed"); - return; - } - - while ((entry = readdir(dp))) { - // skips hidden entries - if (entry->d_name[0] == '.') { - continue; - } - printf("%s\n", entry->d_name); - } - - closedir(dp); - } - - void printClamShell() { - printf(" ████ ██████ \n"); - printf(" ████░░░░████░░░░░░██ \n"); - printf(" ██░░░░░░░░░░░░██░░░░░░██ \n"); - printf(" ██░░░░░░▒▒▒▒░░░░░░░░░░▒▒██\n"); - printf("██░░░░░░▒▒░░░░░░░░░░░░██▒▒██\n"); - printf("██░░░░▒▒░░░░░░░░░░░░░░░░██ \n"); - printf("██░░▒▒░░░░░░░░░░░░▒▒░░░░░░██\n"); - printf("██░░▒▒░░░░░░░░░░░░▒▒░░░░░░██\n"); - printf(" ██░░░░░░░░░░░░▒▒░░░░░░▒▒██\n"); - printf(" ██░░░░░░░░░░▒▒░░░░░░░░██ \n"); - printf(" ██░░░░▒▒▒▒░░░░░░░░▒▒██ \n"); - printf(" ████░░░░░░▒▒▒▒████ \n"); - printf(" ██████████ \n"); -} - -void handle_command(char **args, char **cwd, char **trimmedCWD) { +void handleCommand(char **args, char **cwd, char **trimmedCWD) { if (args[0] == NULL) { return; } @@ -117,6 +28,7 @@ void handle_command(char **args, char **cwd, char **trimmedCWD) { "\texit\n" "\thelp\n" "\tversion\n" + "\tgenerate_clam\n" "\tcd \n" "\tmkdir \n" "\trmdir \n" @@ -213,34 +125,99 @@ void handle_command(char **args, char **cwd, char **trimmedCWD) { } } + if (strcmp("generate_clam", args[0]) == 0) { + printClamShell(); + return; + } + // execute the external command execute(args); } -int main() { - char input[INPUT_SIZE]; - char *args[100]; - char user[100] = "defaultusr"; - char *cwd = getCWD(); - char *trimmedCWD = trimCWD(cwd); - - while (1) { - if (cwd != NULL) { - printf("[cShell-%s] /%s $> ", user, trimmedCWD); - } else { - printf("[cShell-%s] $> ", user); - } - - if (!fgets(input, sizeof(input), stdin)) { - break; - } - - input[strcspn(input, "\n")] = '\0'; - - parse_input(input, args); - - handle_command(args, &cwd, &trimmedCWD); - } - free(cwd); - return 0; +void printClamShell() { + printf(" ████ ██████ \n"); + printf(" ████░░░░████░░░░░░██ \n"); + printf(" ██░░░░░░░░░░░░██░░░░░░██ \n"); + printf(" ██░░░░░░▒▒▒▒░░░░░░░░░░▒▒██\n"); + printf("██░░░░░░▒▒░░░░░░░░░░░░██▒▒██\n"); + printf("██░░░░▒▒░░░░░░░░░░░░░░░░██ \n"); + printf("██░░▒▒░░░░░░░░░░░░▒▒░░░░░░██\n"); + printf("██░░▒▒░░░░░░░░░░░░▒▒░░░░░░██\n"); + printf(" ██░░░░░░░░░░░░▒▒░░░░░░▒▒██\n"); + printf(" ██░░░░░░░░░░▒▒░░░░░░░░██ \n"); + printf(" ██░░░░▒▒▒▒░░░░░░░░▒▒██ \n"); + printf(" ████░░░░░░▒▒▒▒████ \n"); + printf(" ██████████ \n"); } + +char *trimCWD(char *cwd) { + char *token = strtok(cwd, "/"); + char *trimmedCWD = NULL; + + while (token != NULL) { + trimmedCWD = token; + token = strtok(NULL, "/"); + } + + return trimmedCWD; +} + +void execute(char **args) { + pid_t pid = fork(); + if (pid == 0) { + execvp(args[0], args); + perror("execvp failed"); + exit(EXIT_FAILURE); + } else if (pid > 0) { + wait(NULL); + } else { + perror("fork failed"); + } +} + +void parseInput(char *input, char **args) { + char *token = strtok(input, " "); + int i = 0; + while (token != NULL) { + args[i++] = token; + token = strtok(NULL, " "); + } + args[i] = NULL; +} + +char* getCWD() { // current working directory + char *cwd = (char *)malloc(INPUT_SIZE); + + if (cwd == NULL) { + perror("Malloc failed"); + return NULL; + } + + if (getcwd(cwd, 1024) != NULL) { + return cwd; + } else { + perror("Could not get current working directory"); + free(cwd); + return NULL; + } +} + +void listDirectorys(const char *path) { + struct dirent *entry; + DIR *dp = opendir(path); + + if (dp == NULL) { + perror("Directory read failed"); + return; + } + + while ((entry = readdir(dp))) { + // skips hidden entries + if (entry->d_name[0] == '.') { + continue; + } + printf("%s\n", entry->d_name); + } + + closedir(dp); + } diff --git a/src/cShell.h b/src/cShell.h new file mode 100644 index 0000000..3ff3255 --- /dev/null +++ b/src/cShell.h @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef CSHELL_H +#define CSHELL_H +#define INPUT_SIZE 1024 +#define VERSION 0.1f + +void handleCommand(char **args, char **cwd, char **trimmedCWD); +void printClamShell(void); +char *trimCWD(char *cwd); +void execute(char **args); +void parseInput(char *input, char **args); +char* getCWD(void); +void listDirectorys(const char *path); + +#endif \ No newline at end of file diff --git a/src/history.c b/src/history.c new file mode 100644 index 0000000..48429d3 --- /dev/null +++ b/src/history.c @@ -0,0 +1,3 @@ +#include "history.h" + +// TODO: define functions in history.h \ No newline at end of file diff --git a/src/history.h b/src/history.h new file mode 100644 index 0000000..a5e8977 --- /dev/null +++ b/src/history.h @@ -0,0 +1,11 @@ +#ifndef HISTORY_H +#define HISTORY_H + +char* history[500]; + +void createHistoryDir() {}; +void initHistory() {}; +void writeHistory() {}; +void accessHistory() {}; + +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..b93a584 --- /dev/null +++ b/src/main.c @@ -0,0 +1,29 @@ +#include "cShell.h" + +int main() { + char input[INPUT_SIZE]; + char *args[100]; + char user[100] = "defaultusr"; + char *cwd = getCWD(); + char *trimmedCWD = trimCWD(cwd); + + while (1) { + if (cwd != NULL) { + printf("[cShell-%s] /%s $> ", user, trimmedCWD); + } else { + printf("[cShell-%s] $> ", user); + } + + if (!fgets(input, sizeof(input), stdin)) { + break; + } + + input[strcspn(input, "\n")] = '\0'; + + parseInput(input, args); + + handleCommand(args, &cwd, &trimmedCWD); + } + free(cwd); + return 0; +}