From cd78d15d3f01614219f11eee352e81cde8839057 Mon Sep 17 00:00:00 2001 From: Ame Date: Mon, 29 May 2017 16:19:42 +0200 Subject: [PATCH 1/4] settings 1 --- CMakeLists.txt | 4 +- fish_shell/fish_core.c | 17 ---- fish_shell/fish_core.h | 5 +- fish_shell/fish_settings.c | 111 +++++++++++++++++++++++++ fish_shell/fish_settings.h | 6 ++ fish_shell/fish_types.h | 6 ++ fish_shell_tests/CMakeLists.txt | 4 +- fish_shell_tests/FishSettingsTests.cpp | 3 + fish_shell_tests/main.cpp | 1 + 9 files changed, 132 insertions(+), 25 deletions(-) create mode 100644 fish_shell/fish_settings.c create mode 100644 fish_shell/fish_settings.h create mode 100644 fish_shell_tests/FishSettingsTests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d2f20ed..65dbe63 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.7) +cmake_minimum_required(VERSION 3.5) project(fish_shell) # We need thread support @@ -50,7 +50,7 @@ set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FALGS} -g") set(CMAKE_EXE_LINKER_FLAGS "-lpcre") -set(SOURCE_FILES fish_shell/main.c fish_shell/fish_types.h fish_shell/fish_core.h fish_shell/fish_core.c fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_globbing.c fish_shell/fish_globbing.h fish_shell/fish_utils.c fish_shell/fish_utils.h) +set(SOURCE_FILES fish_shell/main.c fish_shell/fish_types.h fish_shell/fish_core.h fish_shell/fish_core.c fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_globbing.c fish_shell/fish_globbing.h fish_shell/fish_utils.c fish_shell/fish_utils.h fish_settings.c fish_settings.h) add_executable(fish ${SOURCE_FILES}) #add_subdirectory(fish_shell) diff --git a/fish_shell/fish_core.c b/fish_shell/fish_core.c index b135254..7e62d8c 100644 --- a/fish_shell/fish_core.c +++ b/fish_shell/fish_core.c @@ -103,23 +103,6 @@ char *fishReadLine() { return NULL; } -Settings *getSettings() { - Settings *s = (Settings*) malloc(sizeof(Settings)); - if (s == NULL){ - crash(); - } else { - s->PS1 = strdup("\n~>"); - } - - return s; -} - -void freeSettings(Settings *settings){ - if (settings != NULL){ - free(settings->PS1); - free(settings); - } -} int fishLoad(WordArray *array) { pid_t pid; diff --git a/fish_shell/fish_core.h b/fish_shell/fish_core.h index 27abdc5..e602ac8 100644 --- a/fish_shell/fish_core.h +++ b/fish_shell/fish_core.h @@ -13,15 +13,12 @@ #include "fish_types.h" #include "fish_commands.h" #include "fish_utils.h" +#include "fish_settings.h" /* WordArray functions */ WordList * split(char *string, char *separator); // Tested -/* Settings functions */ - -Settings * getSettings(); -void freeSettings(Settings *settings); /* General purpose functions */ diff --git a/fish_shell/fish_settings.c b/fish_shell/fish_settings.c new file mode 100644 index 0000000..ab8fba3 --- /dev/null +++ b/fish_shell/fish_settings.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include "fish_core.h" + +Settings *getSettings() { + Settings *s = (Settings*) malloc(sizeof(Settings)); + if (s == NULL){ + crash(); + } + + uid_t uid; + struct passwd *user; + char* file = (char*)malloc(sizeof(char*)*50); + uid = getuid(); + user = getpwuid(uid); + + file = "\0"; + file = strcat(user->pw_dir, "/.fishrc"); + s->user1 = user; + s->PS1 = extractVariable(file, "PS1"); + s->PS2 = extractVariable(file,"PS2"); + return s; +} + +// char* convertPS(char* s){ +// if(s != NULL){ +// printf("Quelque chose à convertir\n"); +// int i=0; +// char* PS = (char*) malloc(sizeof(char*)* 20); +// PS[0] = '\0'; +// while(s[i] != '\0'){ +// if(s[i]=='\\'){ +// i++; +// switch (s[i]){ +// case '\\' : PS[i-1] = '\\'; +// case 'u' : PS[i-1] = '?'; +// default : PS[i] = s[i]; + +// } +// i++; +// }else{ +// PS[i] = s[i]; +// i++; + +// } +// printf("Prompt %i: %s ! %s traiter: %c donne: %c \n",i,PS, s, s[i-1], PS[i-1] ); + +// } +// return PS; +// PS = NULL; +// free(PS); +// }else{ +// printf("Void argument, try again Ame\n"); +// return strdup(">"); +// } +// } + +void freeSettings(Settings *settings){ + if (settings != NULL){ + free(settings->PS1); + free(settings->PS2); + free(settings); + } +} + +char* extractVariable(char* filename, char* var){ + FILE *file = fopen ( filename, "r" ); + if ( file != NULL ) + { + char line [ 128 ]; /* or other suitable maximum line size */ + + while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */ + { + printf("Ligne : %s", line); /* write the line */ + if (strncmp(line, var, strlen(var)-1) == 0) + { + int i=0; + char* tmp = (char*) malloc(sizeof(char*)* (strlen(line)-strlen(var))); + //tmp = "\0"; + while(var[i] != '\0') + { + i++; + } + i= i+1; + while(line[i] != '\n' && line[i] != '\0') + { + printf("ici: %s\n", tmp); + tmp[i-strlen(var)-1] = line[i]; + i++; + } + tmp[i-strlen(var)-1]='\0'; + printf("Result: %s\n",tmp); + return tmp; + free(tmp); + } + } + fclose ( file ); + printf("Variable doesn't exit\n"); + } + else + { + perror ( filename ); /* why didn't the file open? */ + } + return NULL; +} + + diff --git a/fish_shell/fish_settings.h b/fish_shell/fish_settings.h new file mode 100644 index 0000000..bc9240b --- /dev/null +++ b/fish_shell/fish_settings.h @@ -0,0 +1,6 @@ +#include "fish_core.h" + +Settings * getSettings(); +//char* convertPS(char* ); +void freeSettings(Settings *settings); +char* extractVariable(char* filename, char* var); \ No newline at end of file diff --git a/fish_shell/fish_types.h b/fish_shell/fish_types.h index cef1a74..a132e14 100644 --- a/fish_shell/fish_types.h +++ b/fish_shell/fish_types.h @@ -5,6 +5,10 @@ #ifndef FISH_FISH_TYPES_H #define FISH_FISH_TYPES_H +#include +#include +#include + #define EXIT_SIGNAL -100 /* Custom types */ @@ -37,6 +41,8 @@ typedef struct { typedef struct { char *PS1; + char *PS2; + struct passwd* user1; } Settings; diff --git a/fish_shell_tests/CMakeLists.txt b/fish_shell_tests/CMakeLists.txt index a634843..a670066 100644 --- a/fish_shell_tests/CMakeLists.txt +++ b/fish_shell_tests/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.7) +cmake_minimum_required(VERSION 3.5) project(fish_shell_tests) set(CMAKE_CXX_STANDARD 11) @@ -11,7 +11,7 @@ include_directories( "${source_dir}/googlemock/include" ) -set(SOURCE_FILES_TESTS main.cpp FishCoreTests.cpp FishUtilsTests.cpp) +set(SOURCE_FILES_TESTS main.cpp FishCoreTests.cpp FishUtilsTests.cpp FishSettings.cpp ) add_executable(fish_tests ${SOURCE_FILES_TESTS}) diff --git a/fish_shell_tests/FishSettingsTests.cpp b/fish_shell_tests/FishSettingsTests.cpp new file mode 100644 index 0000000..e363817 --- /dev/null +++ b/fish_shell_tests/FishSettingsTests.cpp @@ -0,0 +1,3 @@ +#include "gtest/gtest.h" +#include "../fish_shell/fish_types.h" +#include "../fish_shell/fish_settings.h" \ No newline at end of file diff --git a/fish_shell_tests/main.cpp b/fish_shell_tests/main.cpp index 8260e08..ab4b889 100644 --- a/fish_shell_tests/main.cpp +++ b/fish_shell_tests/main.cpp @@ -7,6 +7,7 @@ #include "../fish_shell/fish_utils.c" #include "../fish_shell/fish_commands.c" #include "../fish_shell/fish_globbing.c" +#include "../fish_shell/fish_settings.c" int main(int argc, char **argv){ ::testing::InitGoogleTest(&argc, argv); From 671ac281066852f3d7fb26c6f6619640871d034d Mon Sep 17 00:00:00 2001 From: Ame Date: Mon, 29 May 2017 17:18:08 +0200 Subject: [PATCH 2/4] Putain d'ubuntu de merde --- CMakeLists.txt | 5 +++-- fish_shell_tests/CMakeLists.txt | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 65dbe63..b2bf4ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,11 +48,12 @@ include_directories( set(CMAKE_C_STANDARD 99) set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FALGS} -g") -set(CMAKE_EXE_LINKER_FLAGS "-lpcre") -set(SOURCE_FILES fish_shell/main.c fish_shell/fish_types.h fish_shell/fish_core.h fish_shell/fish_core.c fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_globbing.c fish_shell/fish_globbing.h fish_shell/fish_utils.c fish_shell/fish_utils.h fish_settings.c fish_settings.h) +set(SOURCE_FILES fish_shell/main.c fish_shell/fish_types.h fish_shell/fish_core.h fish_shell/fish_core.c fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_globbing.c fish_shell/fish_globbing.h fish_shell/fish_utils.c fish_shell/fish_utils.h fish_shell/fish_settings.c fish_shell/fish_settings.h) add_executable(fish ${SOURCE_FILES}) +target_link_libraries(fish pcre) + #add_subdirectory(fish_shell) add_subdirectory(fish_shell_tests) diff --git a/fish_shell_tests/CMakeLists.txt b/fish_shell_tests/CMakeLists.txt index a670066..34532df 100644 --- a/fish_shell_tests/CMakeLists.txt +++ b/fish_shell_tests/CMakeLists.txt @@ -11,9 +11,9 @@ include_directories( "${source_dir}/googlemock/include" ) -set(SOURCE_FILES_TESTS main.cpp FishCoreTests.cpp FishUtilsTests.cpp FishSettings.cpp ) +set(SOURCE_FILES_TESTS main.cpp FishCoreTests.cpp FishUtilsTests.cpp FishSettingsTests.cpp ) add_executable(fish_tests ${SOURCE_FILES_TESTS}) -target_link_libraries(fish_tests libgtest libgmock) +target_link_libraries(fish_tests libgtest libgmock pcre) From d85f4db5ccd4914a63f4c131a48362c7eb1c16d6 Mon Sep 17 00:00:00 2001 From: Ame Date: Mon, 29 May 2017 19:42:02 +0200 Subject: [PATCH 3/4] TESTED+0Leak+Sli HAPPY --- CMakeLists.txt | 2 +- fish_shell/fish_settings.c | 114 +++++++++---------------- fish_shell/fish_settings.h | 15 +++- fish_shell/fish_types.h | 2 +- fish_shell_tests/FishSettingsTests.cpp | 27 +++++- 5 files changed, 80 insertions(+), 80 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b2bf4ad..5b70fdd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,7 +47,7 @@ include_directories( set(CMAKE_C_STANDARD 99) set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow") -set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FALGS} -g") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -g") set(SOURCE_FILES fish_shell/main.c fish_shell/fish_types.h fish_shell/fish_core.h fish_shell/fish_core.c fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_globbing.c fish_shell/fish_globbing.h fish_shell/fish_utils.c fish_shell/fish_utils.h fish_shell/fish_settings.c fish_shell/fish_settings.h) add_executable(fish ${SOURCE_FILES}) diff --git a/fish_shell/fish_settings.c b/fish_shell/fish_settings.c index ab8fba3..0eb5cab 100644 --- a/fish_shell/fish_settings.c +++ b/fish_shell/fish_settings.c @@ -4,6 +4,7 @@ #include #include #include +#include "fish_settings.h" #include "fish_core.h" Settings *getSettings() { @@ -14,51 +15,25 @@ Settings *getSettings() { uid_t uid; struct passwd *user; - char* file = (char*)malloc(sizeof(char*)*50); + char* filename = NULL; uid = getuid(); user = getpwuid(uid); - file = "\0"; - file = strcat(user->pw_dir, "/.fishrc"); - s->user1 = user; - s->PS1 = extractVariable(file, "PS1"); - s->PS2 = extractVariable(file,"PS2"); + filename = (char*) malloc(sizeof(char*)*(strlen(user->pw_dir) + FISH_RC_FILE_SIZE + 1)); + + if (filename == NULL) crash(); + + filename[0] = '\0'; + filename = strcat(filename, user->pw_dir); + filename = strcat(filename, (char*) FISH_RC_FILE); + s->passwd = user; + s->PS1 = extractVariable(filename, (char*) "PS1"); + s->PS2 = extractVariable(filename, (char*) "PS2"); + + free(filename); return s; } -// char* convertPS(char* s){ -// if(s != NULL){ -// printf("Quelque chose à convertir\n"); -// int i=0; -// char* PS = (char*) malloc(sizeof(char*)* 20); -// PS[0] = '\0'; -// while(s[i] != '\0'){ -// if(s[i]=='\\'){ -// i++; -// switch (s[i]){ -// case '\\' : PS[i-1] = '\\'; -// case 'u' : PS[i-1] = '?'; -// default : PS[i] = s[i]; - -// } -// i++; -// }else{ -// PS[i] = s[i]; -// i++; - -// } -// printf("Prompt %i: %s ! %s traiter: %c donne: %c \n",i,PS, s, s[i-1], PS[i-1] ); - -// } -// return PS; -// PS = NULL; -// free(PS); -// }else{ -// printf("Void argument, try again Ame\n"); -// return strdup(">"); -// } -// } - void freeSettings(Settings *settings){ if (settings != NULL){ free(settings->PS1); @@ -69,43 +44,36 @@ void freeSettings(Settings *settings){ char* extractVariable(char* filename, char* var){ FILE *file = fopen ( filename, "r" ); - if ( file != NULL ) - { - char line [ 128 ]; /* or other suitable maximum line size */ - - while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */ - { - printf("Ligne : %s", line); /* write the line */ - if (strncmp(line, var, strlen(var)-1) == 0) - { - int i=0; - char* tmp = (char*) malloc(sizeof(char*)* (strlen(line)-strlen(var))); - //tmp = "\0"; - while(var[i] != '\0') - { + int var_size = strlen(var); + char *tmp = NULL; + char line[FISH_BUFFER_SIZE]; + int i = 0; + + if ( file != NULL ){ + + while ( fgets ( line, FISH_BUFFER_SIZE, file ) != NULL ) { + + if (!strncmp(line, var, var_size)) { + + tmp = (char*) malloc(sizeof(char*)* (strlen(line)-var_size)); + if (tmp == NULL) crash(); + + i = var_size + 1; + while(line[i] != '\n' && line[i] != '\0'){ + tmp[i-var_size-1] = line[i]; i++; } - i= i+1; - while(line[i] != '\n' && line[i] != '\0') - { - printf("ici: %s\n", tmp); - tmp[i-strlen(var)-1] = line[i]; - i++; - } - tmp[i-strlen(var)-1]='\0'; - printf("Result: %s\n",tmp); - return tmp; - free(tmp); - } + tmp[i-var_size-1]='\0'; + } } - fclose ( file ); - printf("Variable doesn't exit\n"); - } - else - { - perror ( filename ); /* why didn't the file open? */ - } - return NULL; + + fclose(file); + return tmp; + } + else { + perror ( filename ); /* why didn't the file open? */ + } + return NULL; } diff --git a/fish_shell/fish_settings.h b/fish_shell/fish_settings.h index bc9240b..91b7792 100644 --- a/fish_shell/fish_settings.h +++ b/fish_shell/fish_settings.h @@ -1,6 +1,13 @@ +#ifndef FISH_FISH_SETTINGS_H +#define FISH_FISH_SETTINGS_H + #include "fish_core.h" -Settings * getSettings(); -//char* convertPS(char* ); -void freeSettings(Settings *settings); -char* extractVariable(char* filename, char* var); \ No newline at end of file +#define FISH_RC_FILE "/.fishrc" +#define FISH_RC_FILE_SIZE 8 + +Settings * getSettings(); //TESTEDssssss +void freeSettings(Settings *settings); //TESTED +char* extractVariable(char* filename, char* var);//TESTED + +#endif //FISH_FISH_SETTINGS_H \ No newline at end of file diff --git a/fish_shell/fish_types.h b/fish_shell/fish_types.h index a132e14..885a428 100644 --- a/fish_shell/fish_types.h +++ b/fish_shell/fish_types.h @@ -42,7 +42,7 @@ typedef struct { typedef struct { char *PS1; char *PS2; - struct passwd* user1; + struct passwd* passwd; } Settings; diff --git a/fish_shell_tests/FishSettingsTests.cpp b/fish_shell_tests/FishSettingsTests.cpp index e363817..70718d7 100644 --- a/fish_shell_tests/FishSettingsTests.cpp +++ b/fish_shell_tests/FishSettingsTests.cpp @@ -1,3 +1,28 @@ #include "gtest/gtest.h" #include "../fish_shell/fish_types.h" -#include "../fish_shell/fish_settings.h" \ No newline at end of file +#include "../fish_shell/fish_settings.h" + +TEST(free_settings_Test, freeSettings){ + Settings *s1 = getSettings(); + Settings *s2 = getSettings(); + + freeSettings(s1); + + ASSERT_STRNE(s1->PS1, s2->PS1); + + freeSettings(s2); +} + +TEST(extract_variable_Test, extractVariable){ + ASSERT_TRUE(extractVariable((char*) "P4T3", (char*) "PS1") == NULL); + ASSERT_STREQ(extractVariable((char*) "../fish_shell_tests/fishrc", (char*) "PS1"), "sli->"); + ASSERT_TRUE(extractVariable((char*) "../fish_shell_tests/fishrc", (char*) "P1ZZ4") == NULL); + +} + +TEST(get_settings_Test, getSettings){ + Settings* s = getSettings(); + ASSERT_FALSE(s ==NULL); + ASSERT_FALSE(s->passwd == NULL); + freeSettings(s); +} \ No newline at end of file From 9b3938894dde72e1d63d886d42d657aa5b071e71 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Mon, 29 May 2017 19:09:19 +0200 Subject: [PATCH 4/4] Victime boloss --- fish_shell/fish_settings.c | 6 +++++- fish_shell/fish_settings.h | 3 ++- fish_shell_tests/FishSettingsTests.cpp | 11 +++++------ fish_shell_tests/fishrc | 2 ++ 4 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 fish_shell_tests/fishrc diff --git a/fish_shell/fish_settings.c b/fish_shell/fish_settings.c index 0eb5cab..a39c3bd 100644 --- a/fish_shell/fish_settings.c +++ b/fish_shell/fish_settings.c @@ -28,7 +28,9 @@ Settings *getSettings() { filename = strcat(filename, (char*) FISH_RC_FILE); s->passwd = user; s->PS1 = extractVariable(filename, (char*) "PS1"); + if (s->PS1 == NULL) s->PS1 = strdup("->"); s->PS2 = extractVariable(filename, (char*) "PS2"); + if (s->PS2 == NULL) s->PS2 = strdup("->"); free(filename); return s; @@ -36,6 +38,8 @@ Settings *getSettings() { void freeSettings(Settings *settings){ if (settings != NULL){ + settings->PS1[0] = '\0'; + settings->PS2[0] = '\0'; free(settings->PS1); free(settings->PS2); free(settings); @@ -73,7 +77,7 @@ char* extractVariable(char* filename, char* var){ else { perror ( filename ); /* why didn't the file open? */ } - return NULL; + return tmp; } diff --git a/fish_shell/fish_settings.h b/fish_shell/fish_settings.h index 91b7792..eac5988 100644 --- a/fish_shell/fish_settings.h +++ b/fish_shell/fish_settings.h @@ -10,4 +10,5 @@ Settings * getSettings(); //TESTEDssssss void freeSettings(Settings *settings); //TESTED char* extractVariable(char* filename, char* var);//TESTED -#endif //FISH_FISH_SETTINGS_H \ No newline at end of file +#endif //FISH_FISH_SETTINGS_H + diff --git a/fish_shell_tests/FishSettingsTests.cpp b/fish_shell_tests/FishSettingsTests.cpp index 70718d7..b2c122a 100644 --- a/fish_shell_tests/FishSettingsTests.cpp +++ b/fish_shell_tests/FishSettingsTests.cpp @@ -3,14 +3,13 @@ #include "../fish_shell/fish_settings.h" TEST(free_settings_Test, freeSettings){ - Settings *s1 = getSettings(); - Settings *s2 = getSettings(); + Settings *s = getSettings(); - freeSettings(s1); + ASSERT_STREQ(s->PS1, "->"); - ASSERT_STRNE(s1->PS1, s2->PS1); + freeSettings(s); - freeSettings(s2); + ASSERT_STRNE(s->PS1, "->"); } TEST(extract_variable_Test, extractVariable){ @@ -25,4 +24,4 @@ TEST(get_settings_Test, getSettings){ ASSERT_FALSE(s ==NULL); ASSERT_FALSE(s->passwd == NULL); freeSettings(s); -} \ No newline at end of file +} diff --git a/fish_shell_tests/fishrc b/fish_shell_tests/fishrc new file mode 100644 index 0000000..dcc591a --- /dev/null +++ b/fish_shell_tests/fishrc @@ -0,0 +1,2 @@ +PS1=sli-> +PS2=sony