add: env var iterpretation

This commit is contained in:
starnakin 2023-07-05 21:12:04 +02:00
parent e15216ce85
commit a36e2a9bc4
8 changed files with 236 additions and 10 deletions

142
src/alias/:q Normal file
View File

@ -0,0 +1,142 @@
#include "../../lib/bozolib/bozolib.h"
#include "./alias.h"
#include <stdlib.h>
#include <stdio.h>
#include "../env/env.h"
#include <unistd.h>
#include <fcntl.h>
#include "../utils/utils.h"
#include <string.h>
static char* get_zzsh_aliases(lst** env)
{
char* home;
ssize_t file_name_size;
char* file_path;
ssize_t size_file_path;
file_name_size = strlen("/.zzsh_aliases");
home = get_env_variable(env, "HOME");
size_file_path = strlen(home)+file_name_size;
file_path = malloc((size_file_path+1)*sizeof(char));
strcpy(file_path, home);
strcat(file_path, "/.zzsh_aliases");
return file_path;
}
lst** aliases_init(lst** env)
{
lst** aliases;
int fd;
char* file_path = get_zzsh_aliases(env);
fd = open(file_path, O_CREAT, 0644);
if (fd == -1)
dprintf(2, "Erreur lors la création du fichier .zzsh_aliases\n");
close(fd);
fd = open(file_path, O_RDONLY);
if (fd == -1)
dprintf(2, "Erreur lors de la lecture du fichier .zzsh_aliases\n");
int buff_size;
buff_size = lseek(fd, 0, SEEK_END);
close(fd);
fd = open(file_path, O_RDONLY);
char buffer[buff_size];
buffer[0] = '\0';
read(fd, buffer, buff_size);
char** assignation;
char** line = split_quoted_charset(buffer, "\n");
aliases = malloc(sizeof(lst*));
*aliases = NULL;
for(int i = 0; line[i]!=NULL; i++)
{
if (strchr(line[i], '=') != NULL)
{
assignation = split_quoted_charset(line[i], "=");
add_alias(aliases, assignation[0], assignation[1]);
}
}
return aliases;
}
int add_alias(lst** root, const char* key, const char* value)
{
lst* nouveau;
alias_t* alias;
alias = malloc(sizeof(alias_t));
if (alias == NULL)
return 1;
nouveau = malloc(sizeof(lst));
if (nouveau == NULL)
{
free(alias);
return 1;
}
alias->key = strdup(key);
if (alias->key == NULL)
{
free(alias->key);
free(alias);
return 1;
}
alias->value = strdup(value);
if (alias->key == NULL)
{
free(alias->key);
free(alias->value);
free(alias);
return 1;
}
nouveau->content = alias;
lst_addback(root, nouveau);
return 0;
}
char* get_alias(lst** aliases, const char* key)
{
lst* current = *aliases;
alias_t* content;
while (current != NULL)
{
content = current->content;
if (strcmp(key, content->key) == 0)
return content->value;
current = current->next;
}
return NULL;
}
void alias_del(void *ptr)
{
alias_t *alias = ptr;
free(alias->key);
free(alias->value);
free(alias);
}
void print_aliases(lst** aliases, int fd_out)
{
lst* current = *aliases;
alias_t* content;
while (current != NULL)
{
content = current->content;
dprintf(fd_out, "%s='%s'\n", content->key, content->value);
current = current->next;
}
}
int alias_save(lst** aliases, lst** env)
{
int fd;
char* file_path = get_zzsh_aliases(env);
fd = open(file_path, O_WRONLY);
if (fd == -1)
{
dprintf(2, "Une erreur est survenue lors de l'écriture des aliases\n");
return 1;
}
print_aliases(aliases, fd);
return 0;
}

View File

@ -40,11 +40,12 @@ lst** aliases_init(lst** env)
close(fd);
fd = open(file_path, O_RDONLY);
char buffer[buff_size];
buffer[0] = '\0';
read(fd, buffer, buff_size);
char** assignation;
char** line = split_quoted_charset(buffer, "\n");
aliases = malloc(sizeof(lst*));
*aliases = NULL;
aliases = malloc(sizeof(lst*));
*aliases = NULL;
for(int i = 0; line[i]!=NULL; i++)
{
if (strchr(line[i], '=') != NULL)
@ -53,7 +54,7 @@ lst** aliases_init(lst** env)
add_alias(aliases, assignation[0], assignation[1]);
}
}
return aliases;
return aliases;
}
int add_alias(lst** root, const char* key, const char* value)

73
src/env/env.c vendored
View File

@ -1,7 +1,4 @@
#include "env.h"
#include "../../lib/bozolib/bozolib.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
void env_del(void *ptr)
@ -174,3 +171,73 @@ char **get_env_str(lst** root)
tab[i] = NULL;
return tab;
}
static char* get_key(const char* key_start)
{
if (strncmp(key_start, "~", 1) == 0)
return strdup("~");
size_t i = 1;
while (key_start[i] != '\0' && strchr("\t $\"\'", key_start[i]) == NULL)
i++;
return (strndup(key_start, i));
}
static char* get_value(data_t* data, const char* key)
{
char *out;
if (strcmp(key, "~") == 0)
out = get_env_variable(data->env, "HOME");
else if (strcmp(key, "$?") == 0)
out = itoA(data->status_code);
else
out = get_env_variable(data->env, key + 1);
if (out == NULL)
out = "";
return out;
}
char* interpret_env_var(data_t *data, const char* str)
{
char symbols[3] = "$~";
char* out;
char* key;
char* value;
char* tmp2;
const char* tmp;
out = strdup(str);
if (out == NULL)
return NULL;
for (size_t i = 0; symbols[i] != '\0'; i++)
{
tmp = strchr(out,symbols[i]);
while (tmp != NULL && is_in_quote(tmp, str - tmp) == 1)
tmp = strchr(tmp, symbols[i]);
while (tmp != NULL)
{
if (tmp == NULL)
continue;
key = get_key(tmp);
if (key == NULL)
{
free(out);
return NULL;
}
value = get_value(data, key);
if (value == NULL)
{
free(key);
free(out);
return NULL;
}
tmp2 = str_replace(out, value, tmp - out, tmp - out + strlen(key));
free(key);
free(out);
out = tmp2;
tmp = strchr(out,symbols[i]);
while (tmp != NULL && is_in_quote(tmp, str - tmp) == 1)
tmp = strchr(tmp, symbols[i]);
}
}
return out;
}

8
src/env/env.h vendored
View File

@ -1,5 +1,12 @@
#pragma once
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../lib/bozolib/bozolib.h"
#include "../data/data.h"
#include "../utils/utils.h"
typedef struct s_env
{
@ -13,4 +20,5 @@ void env_del(void *ptr);
char *get_env_variable(lst** root, const char* key);
int add_env_variable(lst** root, const char *key, const char *value);
int edit_env_variable(lst** root, const char *key, const char *new_value);
char* interpret_env_var(data_t*, const char*);
char** get_env_str(lst** root);

1
src/env/envs.c vendored
View File

@ -1 +0,0 @@

View File

@ -35,6 +35,7 @@ int main(int ac, char **av, char **env_str)
lst_clear(data.env, &env_del);
return 1;
}
data.status_code = 0;
signal(SIGINT, ctrlc);
signal(SIGQUIT, SIG_IGN);
line = get_user_input(&data);
@ -56,7 +57,7 @@ int main(int ac, char **av, char **env_str)
}
line = get_user_input(&data);
}
alias_save(data.aliases, data.env);
alias_save(data.aliases, data.env);
lst_clear(data.aliases, &alias_del);
lst_clear(data.env, &env_del);
return (0);

View File

@ -14,9 +14,16 @@ int parsing_cmd(char *str, cmd_t* command, data_t *data)
char* tmp;
if (cmd_init(command))
return 1;
if (get_redirections(str, command))
tmp = interpret_env_var(data, str);
if (tmp == NULL)
return 1;
command->args = split_quoted_charset(str, "\t ");
if (get_redirections(tmp, command))
{
free(tmp);
return 1;
}
command->args = split_quoted_charset(tmp, "\t ");
free(tmp);
if (command->args == NULL)
return 1;
for (size_t i = 0; command->args[i] != NULL; i++)

View File

@ -5,5 +5,6 @@
#include "../redirection/redirection.h"
#include "../data/data.h"
#include "../alias/alias.h"
#include "../env/env.h"
lst*** parsing(const char* str, data_t *data);