settings 1

This commit is contained in:
Ame 2017-05-29 16:19:42 +02:00
parent 99b847d43a
commit cd78d15d3f
9 changed files with 132 additions and 25 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
@ -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)

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

111
fish_shell/fish_settings.c Normal file
View File

@ -0,0 +1,111 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <string.h>
#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;
}

View File

@ -0,0 +1,6 @@
#include "fish_core.h"
Settings * getSettings();
//char* convertPS(char* );
void freeSettings(Settings *settings);
char* extractVariable(char* filename, char* var);

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
/* Custom types */
@ -37,6 +41,8 @@ typedef struct {
typedef struct {
char *PS1;
char *PS2;
struct passwd* user1;
} 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,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})

View File

@ -0,0 +1,3 @@
#include "gtest/gtest.h"
#include "../fish_shell/fish_types.h"
#include "../fish_shell/fish_settings.h"

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