2017-05-29 14:19:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <string.h>
|
2017-05-29 17:42:02 +00:00
|
|
|
#include "fish_settings.h"
|
2017-05-29 14:19:42 +00:00
|
|
|
#include "fish_core.h"
|
|
|
|
|
|
|
|
Settings *getSettings() {
|
|
|
|
Settings *s = (Settings*) malloc(sizeof(Settings));
|
|
|
|
if (s == NULL){
|
|
|
|
crash();
|
|
|
|
}
|
|
|
|
|
|
|
|
uid_t uid;
|
|
|
|
struct passwd *user;
|
2017-05-29 17:42:02 +00:00
|
|
|
char* filename = NULL;
|
2017-05-29 14:19:42 +00:00
|
|
|
uid = getuid();
|
|
|
|
user = getpwuid(uid);
|
|
|
|
|
2017-05-29 17:42:02 +00:00
|
|
|
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");
|
2017-05-29 17:09:19 +00:00
|
|
|
if (s->PS1 == NULL) s->PS1 = strdup("->");
|
2017-05-29 17:42:02 +00:00
|
|
|
s->PS2 = extractVariable(filename, (char*) "PS2");
|
2017-05-29 17:09:19 +00:00
|
|
|
if (s->PS2 == NULL) s->PS2 = strdup("->");
|
2017-05-29 17:42:02 +00:00
|
|
|
|
|
|
|
free(filename);
|
2017-05-29 14:19:42 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void freeSettings(Settings *settings){
|
|
|
|
if (settings != NULL){
|
2017-05-29 17:09:19 +00:00
|
|
|
settings->PS1[0] = '\0';
|
|
|
|
settings->PS2[0] = '\0';
|
2017-05-29 14:19:42 +00:00
|
|
|
free(settings->PS1);
|
|
|
|
free(settings->PS2);
|
|
|
|
free(settings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 11:29:19 +00:00
|
|
|
void printPS(char* PS, Settings* s){
|
|
|
|
int buf= FISH_BUFFER_SIZE;
|
|
|
|
int i = 0;
|
|
|
|
int slashed = 0;
|
|
|
|
char* path = NULL;
|
|
|
|
while (PS[i] != '\0'){
|
|
|
|
if(slashed){
|
|
|
|
switch (PS[i])
|
|
|
|
{
|
|
|
|
case 'u':
|
|
|
|
printf("%s",s->passwd->pw_name);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
path = (char*) malloc(sizeof(char)*buf);
|
|
|
|
while(getcwd(path,buf) == NULL){
|
|
|
|
buf+=FISH_BUFFER_SIZE;
|
|
|
|
free(path);
|
|
|
|
path = (char*) malloc(sizeof(char)*buf);
|
|
|
|
}
|
|
|
|
printf("%s",path);
|
|
|
|
free(path);
|
|
|
|
path = NULL;
|
|
|
|
break;
|
|
|
|
default:printf("%c",PS[i]);
|
|
|
|
|
|
|
|
}
|
|
|
|
slashed = 0;
|
|
|
|
}
|
|
|
|
else if(PS[i] == '\\'){
|
|
|
|
slashed = 1;
|
|
|
|
}else{
|
|
|
|
printf("%c",PS[i]);
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-29 14:19:42 +00:00
|
|
|
char* extractVariable(char* filename, char* var){
|
|
|
|
FILE *file = fopen ( filename, "r" );
|
2017-05-29 17:42:02 +00:00
|
|
|
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];
|
2017-05-29 14:19:42 +00:00
|
|
|
i++;
|
|
|
|
}
|
2017-05-29 17:42:02 +00:00
|
|
|
tmp[i-var_size-1]='\0';
|
|
|
|
}
|
2017-05-29 14:19:42 +00:00
|
|
|
}
|
2017-05-29 17:42:02 +00:00
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
perror ( filename ); /* why didn't the file open? */
|
|
|
|
}
|
2017-05-29 17:09:19 +00:00
|
|
|
return tmp;
|
2017-05-29 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-12 11:29:19 +00:00
|
|
|
|
|
|
|
|