From a36e2a9bc4dc30bbc33b650e1523c8ed088dbf3b Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 5 Jul 2023 21:12:04 +0200 Subject: [PATCH] add: env var iterpretation --- src/alias/:q | 142 ++++++++++++++++++++++++++++++++++++++++++ src/alias/alias.c | 7 ++- src/env/env.c | 73 +++++++++++++++++++++- src/env/env.h | 8 +++ src/env/envs.c | 1 - src/main.c | 3 +- src/parsing/parsing.c | 11 +++- src/parsing/parsing.h | 1 + 8 files changed, 236 insertions(+), 10 deletions(-) create mode 100644 src/alias/:q delete mode 100644 src/env/envs.c diff --git a/src/alias/:q b/src/alias/:q new file mode 100644 index 0000000..8ffeaaa --- /dev/null +++ b/src/alias/:q @@ -0,0 +1,142 @@ +#include "../../lib/bozolib/bozolib.h" +#include "./alias.h" +#include +#include +#include "../env/env.h" +#include +#include +#include "../utils/utils.h" +#include + +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; +} diff --git a/src/alias/alias.c b/src/alias/alias.c index fd91564..8ffeaaa 100644 --- a/src/alias/alias.c +++ b/src/alias/alias.c @@ -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) diff --git a/src/env/env.c b/src/env/env.c index 43cdc3a..a89806a 100644 --- a/src/env/env.c +++ b/src/env/env.c @@ -1,7 +1,4 @@ #include "env.h" -#include "../../lib/bozolib/bozolib.h" -#include -#include #include 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; +} diff --git a/src/env/env.h b/src/env/env.h index 9b1cc7d..af496c0 100644 --- a/src/env/env.h +++ b/src/env/env.h @@ -1,5 +1,12 @@ #pragma once + +#include +#include +#include + #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); diff --git a/src/env/envs.c b/src/env/envs.c deleted file mode 100644 index 8b13789..0000000 --- a/src/env/envs.c +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/main.c b/src/main.c index bb0e52b..d15495e 100644 --- a/src/main.c +++ b/src/main.c @@ -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); diff --git a/src/parsing/parsing.c b/src/parsing/parsing.c index 54eb1c4..7185329 100644 --- a/src/parsing/parsing.c +++ b/src/parsing/parsing.c @@ -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++) diff --git a/src/parsing/parsing.h b/src/parsing/parsing.h index a79ab8a..d646fd6 100644 --- a/src/parsing/parsing.h +++ b/src/parsing/parsing.h @@ -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);