Merge branch 'ame' into 'master'

Garbage

See merge request !10
This commit is contained in:
Antoine Bartuccio 2017-05-29 17:11:24 +00:00
commit 9c0c89365a
10 changed files with 142 additions and 28 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.7)
cmake_minimum_required(VERSION 3.5)
project(fish_shell)
# We need thread support
@ -47,12 +47,13 @@ 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(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)
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)

View File

@ -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;

View File

@ -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 */

View File

@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <string.h>
#include "fish_settings.h"
#include "fish_core.h"
Settings *getSettings() {
Settings *s = (Settings*) malloc(sizeof(Settings));
if (s == NULL){
crash();
}
uid_t uid;
struct passwd *user;
char* filename = NULL;
uid = getuid();
user = getpwuid(uid);
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");
if (s->PS1 == NULL) s->PS1 = strdup("->");
s->PS2 = extractVariable(filename, (char*) "PS2");
if (s->PS2 == NULL) s->PS2 = strdup("->");
free(filename);
return s;
}
void freeSettings(Settings *settings){
if (settings != NULL){
settings->PS1[0] = '\0';
settings->PS2[0] = '\0';
free(settings->PS1);
free(settings->PS2);
free(settings);
}
}
char* extractVariable(char* filename, char* var){
FILE *file = fopen ( filename, "r" );
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++;
}
tmp[i-var_size-1]='\0';
}
}
fclose(file);
return tmp;
}
else {
perror ( filename ); /* why didn't the file open? */
}
return tmp;
}

View File

@ -0,0 +1,14 @@
#ifndef FISH_FISH_SETTINGS_H
#define FISH_FISH_SETTINGS_H
#include "fish_core.h"
#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

View File

@ -5,6 +5,10 @@
#ifndef FISH_FISH_TYPES_H
#define FISH_FISH_TYPES_H
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#define EXIT_SIGNAL -100
#define ERROR_STRING "\n"
@ -39,6 +43,8 @@ typedef struct {
typedef struct {
char *PS1;
char *PS2;
struct passwd* passwd;
} Settings;

View File

@ -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,9 +11,9 @@ 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 FishSettingsTests.cpp )
add_executable(fish_tests ${SOURCE_FILES_TESTS})
target_link_libraries(fish_tests libgtest libgmock)
target_link_libraries(fish_tests libgtest libgmock pcre)

View File

@ -0,0 +1,27 @@
#include "gtest/gtest.h"
#include "../fish_shell/fish_types.h"
#include "../fish_shell/fish_settings.h"
TEST(free_settings_Test, freeSettings){
Settings *s = getSettings();
ASSERT_STREQ(s->PS1, "->");
freeSettings(s);
ASSERT_STRNE(s->PS1, "->");
}
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);
}

2
fish_shell_tests/fishrc Normal file
View File

@ -0,0 +1,2 @@
PS1=sli->
PS2=sony

View File

@ -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);