diff --git a/CMakeLists.txt b/CMakeLists.txt index 108974e..4c897d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,5 +4,5 @@ project(fish) set(CMAKE_C_STANDARD 99) set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra") -set(SOURCE_FILES main.c fish_lib/fish_lib.c fish_lib/fish_lib.h) +set(SOURCE_FILES main.c fish_shell/fish_core.c fish_shell/fish_core.h fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_types.h) add_executable(fish ${SOURCE_FILES}) \ No newline at end of file diff --git a/fish_shell/fish_commands.c b/fish_shell/fish_commands.c new file mode 100644 index 0000000..ab4057d --- /dev/null +++ b/fish_shell/fish_commands.c @@ -0,0 +1,64 @@ +// +// Created by Antoine Bartuccio on 14/05/2017. +// +#include +#include +#include "fish_core.h" + +/* Necessary global variables */ +char * builtinCommandsStr[] = { + "cd", + "help", + "exit" +}; + + +builtinCommand *builtinCommands[] = { + &fishCd, + &fishHelp, + &fishExit +}; + +char ** getBuiltinCommandsStr(){ + return builtinCommandsStr; +} + +builtinCommand **getBuiltinCommands(){ + return builtinCommands; +} + +int fishCd(WordArray *args) { + if (args->size < 2){ + fprintf(stderr, "fish: Où sont les arguments de ta commande \"cd\" connard ?!\n"); + } else { + if (chdir(args->words[1]) != 0){ + perror("fish"); + } + } + return 1; +} + +int fishHelp(WordArray *args) { + int i; + printf("Bartuccio Antoine, Amalvy Arthur, Yann Chevanton\n"); + printf("Tape tes putains de noms de programmes et tes arguments de merde et tabasse ENTER !\n"); + printf("Les commandes suivantes sont internes :\nls"); + for (i=0; i < getNbBuiltins(); i++){ + printf("\t%s\n", builtinCommandsStr[i]); + } + printf("Et sinon pour le reste, RTFM !"); + if (args->size > 0) + return 1; + return 1; +} + +int fishExit(WordArray *args) { + if (args->size != 1) + return 1; + else + return 0; +} + +int getNbBuiltins() { + return sizeof(builtinCommandsStr) / sizeof(char*); +} diff --git a/fish_shell/fish_commands.h b/fish_shell/fish_commands.h new file mode 100644 index 0000000..0039d1b --- /dev/null +++ b/fish_shell/fish_commands.h @@ -0,0 +1,24 @@ +// +// Created by Antoine Bartuccio on 14/05/2017. +// + +#ifndef FISH_FISH_COMMANDS_H +#define FISH_FISH_COMMANDS_H + +#include "fish_core.h" + +/* Getters */ + +char ** getBuiltinCommandsStr (); +builtinCommand **getBuiltinCommands(); +int getNbBuiltins(); + +/* Built in shell commands */ + +int fishCd(WordArray * args); + +int fishHelp(WordArray * args); + +int fishExit(WordArray * args); + +#endif //FISH_FISH_COMMANDS_H diff --git a/fish_lib/fish_lib.c b/fish_shell/fish_core.c similarity index 67% rename from fish_lib/fish_lib.c rename to fish_shell/fish_core.c index a81d076..66ddcad 100644 --- a/fish_lib/fish_lib.c +++ b/fish_shell/fish_core.c @@ -4,7 +4,8 @@ #include #include #include -#include "fish_lib.h" +#include +#include "fish_core.h" #define FISH_BUFFER_SIZE 1024 #define FISH_TOKENS " \t\r\n\a" @@ -12,24 +13,20 @@ void fishLoop(Settings * settings){ char * line = NULL; WordArray * splited = NULL; - int exited = 0; - int i; + int status = 1; - while (!exited) { + do { printf("%s", settings->PS1); line = fishReadLine(); line = fishExpand(line); splited = split(line, FISH_TOKENS); - for (i = 0; i < splited->size; i++) { - printf("%s\n", splited->words[i]); - if(!strcmp(splited->words[i], "exit")) - exited = 1; - } + + status = fishExecute(splited); freeWordArray(splited); free(line); - } + } while(status); } int countSeparators(char *string, char *separators) { @@ -101,11 +98,14 @@ char *fishReadLine() { while (1){ c = getchar(); - if (c == EOF || c == '\n'){ - line[position] = '\0'; - return line; - } else { - line[position] = (char) c; + switch (c){ + case '\n': + line[position] = '\0'; + return line; + case EOF: + exit(EXIT_SUCCESS); + default: + line[position] = (char) c; } position++; @@ -134,8 +134,48 @@ Settings *getSettings() { fprintf(stderr, "fish: Error allocating fucking settings"); exit(EXIT_FAILURE); } - s->PS1 = strdup("~>"); + s->PS1 = strdup("\n~>"); return s; } +int fishLoad(WordArray *array) { + pid_t pid; + int status; + + pid = fork(); + if (pid == 0){ + /* Executes only in the child process */ + if (execvp(array->words[0], array->words) == -1){ + /* Error during system call */ + perror("fish"); + } + exit(EXIT_FAILURE); + } else if (pid < 0){ + /* Fork failed */ + perror("fish"); + } else { + /* Handle parent process */ + + /* Wait for the child process to finish */ + do { + waitpid(pid, &status, WUNTRACED); + } while (!WIFEXITED(status) && !WIFSIGNALED(status)); + } + + return 1; +} + +int fishExecute(WordArray *array) { + int i; + if (array->size < 0) + return 1; + + for (i=0; i < getNbBuiltins(); i++){ + if (!strcmp(array->words[0], getBuiltinCommandsStr()[i])){ + return getBuiltinCommands()[i](array); + } + } + + return fishLoad(array); +} diff --git a/fish_lib/fish_lib.h b/fish_shell/fish_core.h similarity index 67% rename from fish_lib/fish_lib.h rename to fish_shell/fish_core.h index f940a7d..0522e2c 100644 --- a/fish_lib/fish_lib.h +++ b/fish_shell/fish_core.h @@ -2,17 +2,12 @@ // Created by Antoine Bartuccio on 11/05/2017. // -#ifndef FISH_FISH_LIB_H -#define FISH_FISH_LIB_H +#ifndef FISH_FISH_CORE_H +#define FISH_FISH_CORE_H -typedef struct { - char ** words; - int size; -} WordArray; -typedef struct { - char *PS1; -} Settings; +#include "fish_types.h" +#include "fish_commands.h" /* WordArray functions */ @@ -34,4 +29,8 @@ int countSeparators(char *string, char *separators); char * fishExpand(char* line); -#endif //FISH_FISH_LIB_H +int fishLoad(WordArray *array); + +int fishExecute(WordArray *array); + +#endif //FISH_FISH_CORE_H diff --git a/fish_shell/fish_types.h b/fish_shell/fish_types.h new file mode 100644 index 0000000..32402c2 --- /dev/null +++ b/fish_shell/fish_types.h @@ -0,0 +1,22 @@ +// +// Created by Antoine Bartuccio on 14/05/2017. +// + +#ifndef FISH_FISH_TYPES_H +#define FISH_FISH_TYPES_H + +/* Custom types */ + +typedef struct { + char ** words; + int size; +} WordArray; + +typedef struct { + char *PS1; +} Settings; + + +typedef int (builtinCommand) (WordArray*); + +#endif //FISH_FISH_TYPES_H diff --git a/main.c b/main.c index 4cfd21c..d0efe10 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,5 @@ #include -#include "fish_lib/fish_lib.h" +#include "fish_shell/fish_core.h" int main() { /* todo load config file */