From b073baa06b363206a6a2b94fa9aaf96db9809802 Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 4 Jul 2023 15:54:19 +0200 Subject: [PATCH] add: alias --- src/alias/alias.c | 61 ++++++++++++++++++++++++++++++++ src/alias/alias.h | 14 ++++++++ src/builtin-exec/builtin.c | 28 ++++++--------- src/builtin-exec/builtin.h | 15 ++++++-- src/builtin-exec/builtin_alias.c | 49 +++++++++++++++++++++++++ src/config.h | 4 +++ src/data/data.h | 11 ++++++ src/exec/exec.c | 14 +++----- src/exec/exec.h | 6 ++-- src/input/input.c | 10 +++--- src/input/input.h | 3 +- src/main.c | 26 +++++++++----- src/parsing/parsing.c | 30 ++++++++++------ src/parsing/parsing.h | 5 ++- 14 files changed, 217 insertions(+), 59 deletions(-) create mode 100644 src/alias/alias.c create mode 100644 src/alias/alias.h create mode 100644 src/builtin-exec/builtin_alias.c create mode 100644 src/config.h create mode 100644 src/data/data.h diff --git a/src/alias/alias.c b/src/alias/alias.c new file mode 100644 index 0000000..999fc37 --- /dev/null +++ b/src/alias/alias.c @@ -0,0 +1,61 @@ +#include "../../lib/bozolib/bozolib.h" +#include "./alias.h" +#include + +lst** aliases_init() +{ + lst** aliases; + + aliases = malloc(sizeof(lst*)); + *aliases = NULL; + 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; +} diff --git a/src/alias/alias.h b/src/alias/alias.h new file mode 100644 index 0000000..8c73e54 --- /dev/null +++ b/src/alias/alias.h @@ -0,0 +1,14 @@ +#pragma once + +#include "../../lib/bozolib/bozolib.h" + +typedef struct s_alias +{ + char* key; + char* value; +} alias_t; + +lst** aliases_init(); +lst** aliases_save(lst** aliases); +char* get_alias(lst** aliases, const char* key); +int add_alias(lst** root, const char* key, const char* value); diff --git a/src/builtin-exec/builtin.c b/src/builtin-exec/builtin.c index 48914ae..4a9a772 100644 --- a/src/builtin-exec/builtin.c +++ b/src/builtin-exec/builtin.c @@ -1,18 +1,8 @@ -#include -#include -#include -#include -#include -#include "../cmd/cmd.h" -#include -#include "../env/env.h" -#include "../utils/utils.h" -#include "../../lib/bozolib/bozolib.h" -#include "../exec/exec.h" +#include "./builtin.h" char* builtin_path(const char* executable) { - if (strcmp(executable, "cd") == 0 || strcmp(executable, "where") == 0); + if (strcmp(executable, "cd") == 0 || strcmp(executable, "where") == 0 || strcmp(executable, "alias") == 0); else return NULL; return strdup(executable); @@ -22,12 +12,12 @@ int change_directory(char** args, lst** env) { char cwd[PATH_MAX]; char* oldpwd = get_env_variable(env, "PWD"); - if(len((void**)args)==1) + if (tablen((const void**)args)==1) { char* path = get_env_variable(env, "HOME"); chdir(path); } - else if (len((void**)args)>2) + else if (tablen((const void**)args)>2) { dprintf(2, "cd : Trop d'arguments!\n"); return 1; @@ -45,7 +35,7 @@ int change_directory(char** args, lst** env) int where(char** args, lst** env) { - if (len((void**)args)==1) + if (tablen((const void**)args)==1) { dprintf(2, "where : Il faut au moins 1 argument!\n"); return 1; @@ -65,18 +55,20 @@ int where(char** args, lst** env) return 0; } -int builtin_execute(cmd* input, lst** env) +int builtin_execute(cmd* input, data_t *data, int fd_in, int fd_out) { if (strcmp(input->executable, "cd") == 0) { - change_directory(input->args, env); + change_directory(input->args, data->env); return 0; } else if(strcmp(input->executable, "where") == 0) { - where(input->args, env); + where(input->args, data->env); return 0; } + else if (strcmp(input->executable, "alias") == 0) + return builtin_alias(fd_in, fd_out, data->aliases, input->args); else { return 1; diff --git a/src/builtin-exec/builtin.h b/src/builtin-exec/builtin.h index e5f15c6..bd8f3a2 100644 --- a/src/builtin-exec/builtin.h +++ b/src/builtin-exec/builtin.h @@ -1,9 +1,20 @@ #pragma once +#include +#include +#include + +#include "../env/env.h" #include "../cmd/cmd.h" +#include "../data/data.h" +#include "../alias/alias.h" +#include "../exec/exec.h" #include "../../lib/bozolib/bozolib.h" char* builtin_path(const char* executable); +int builtin_execute(cmd* input, data_t* data, int fd_in, int fd_out); + +int builtin_alias(int fd_in, int fd_out, lst** aliases, char** args); int change_directory(char** args, lst** env); -int where(char** args); -int builtin_execute(cmd* input, lst** env); +int where(char** args, lst** env); + diff --git a/src/builtin-exec/builtin_alias.c b/src/builtin-exec/builtin_alias.c new file mode 100644 index 0000000..efa6a2c --- /dev/null +++ b/src/builtin-exec/builtin_alias.c @@ -0,0 +1,49 @@ +#include "./builtin.h" +#include +#include +#include + +static 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 builtin_alias(int fd_in, int fd_out, lst** aliases, char **args) +{ + const char* equal; + char* result; + char* key; + (void)fd_in; + if (tablen((const void**) args) == 1) + print_aliases(aliases, fd_out); + for (size_t i = 1; args[i] != NULL; i++) + { + equal = strchr(args[i], '='); + if (equal == NULL) + { + result = get_alias(aliases, args[i]); + if (result != NULL) + dprintf(fd_out, "%s=%s\n", args[i], result); + } + else + { + key = strndup(args[i], equal - args[i]); + if (key == NULL) + return 1; + if (add_alias(aliases, key, equal + 1)) + { + free(key); + return 1; + } + free(key); + } + } + return 0; +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..be979f6 --- /dev/null +++ b/src/config.h @@ -0,0 +1,4 @@ +#pragma once + +#define ZZSH_HISTORY_PATH "~/.config/history" +#define ALIASES_SAVE_PATH "~/.config/aliases" diff --git a/src/data/data.h b/src/data/data.h new file mode 100644 index 0000000..37ef7cb --- /dev/null +++ b/src/data/data.h @@ -0,0 +1,11 @@ +#pragma once + +#include "../../lib/bozolib/bozolib.h" + +typedef struct s_data +{ + const char* computor_name; + int status_code; + lst** env; + lst** aliases; +} data_t; diff --git a/src/exec/exec.c b/src/exec/exec.c index c291681..ae09f17 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -9,13 +9,7 @@ #include "../utils/utils.h" #include "../../lib/bozolib/bozolib.h" #include "../builtin-exec/builtin.h" - -int len(void** list) -{ - int index; - for (index = 0; list[index]!=NULL; index++); - return index; -} +#include "../data/data.h" int execute(cmd* input, lst** env) { @@ -78,7 +72,7 @@ char* get_executable_path(const char* executable, lst** env) return NULL; } -int cmds_list_exec(lst** cmds, lst** env) +int cmds_list_exec(lst** cmds, data_t *data) { lst* current = *cmds; cmd* content; @@ -97,8 +91,8 @@ int cmds_list_exec(lst** cmds, lst** env) ; else if (content->executable == NULL) dprintf(2, "zzsh: command not found: %s\n", content->args[0]); - else if (builtin_execute(content, env) == 1) - execute(content, env); + else if (builtin_execute(content, data, fds[0], fds[1]) == 1) + execute(content, data->env); current = current->next; } return 0; diff --git a/src/exec/exec.h b/src/exec/exec.h index 6df14b1..7ee12df 100644 --- a/src/exec/exec.h +++ b/src/exec/exec.h @@ -2,9 +2,9 @@ #include "../cmd/cmd.h" #include "../../lib/bozolib/bozolib.h" +#include "../data/data.h" -int cmds_list_exec(lst** cmds, lst** env); +int cmds_list_exec(lst** cmds, data_t *data); int execute(cmd* input, lst** env); -int builtin_execute(cmd* input, lst** env); -int len(void** list); +int builtin_execute(cmd* input, data_t* data, int fd_int, int fd_out); char* get_executable_path(const char* executable, lst** env); diff --git a/src/input/input.c b/src/input/input.c index b49fb74..0dc6980 100644 --- a/src/input/input.c +++ b/src/input/input.c @@ -1,7 +1,7 @@ #include "./input.h" #include -static char* get_prompt(lst** env) +static char* get_prompt(data_t *data) { char* out; char cwd[PATH_MAX]; @@ -18,8 +18,8 @@ static char* get_prompt(lst** env) { *first_line_return = '\0'; } - char* user = get_env_variable(env, "USER"); - char* home = get_env_variable(env, "HOME"); + char* user = get_env_variable(data->env, "USER"); + char* home = get_env_variable(data->env, "HOME"); if (strncmp(home, cwd, strlen(home)) == 0) { strcpy(cwd, "~"); @@ -58,12 +58,12 @@ static void finisher(char** strs) } } -char *get_user_input(lst** env) +char *get_user_input(data_t *data) { char *prompt; char *input; - prompt = get_prompt(env); + prompt = get_prompt(data); if (prompt == NULL) return NULL; input = readline(prompt); diff --git a/src/input/input.h b/src/input/input.h index 58bdcc9..b940bf4 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -10,5 +10,6 @@ #include "../../lib/bozolib/bozolib.h" #include "../utils/utils.h" #include "../env/env.h" +#include "../data/data.h" -char *get_user_input(lst** env); +char *get_user_input(data_t *data); diff --git a/src/main.c b/src/main.c index b67beeb..0993289 100644 --- a/src/main.c +++ b/src/main.c @@ -2,9 +2,11 @@ #include "./input/input.h" #include #include +#include "alias/alias.h" #include "cmd/cmd.h" #include "./exec/exec.h" #include "./parsing/parsing.h" +#include "./data/data.h" void cmds_list_destroyer(lst*** cmds_list) { @@ -16,34 +18,40 @@ void cmds_list_destroyer(lst*** cmds_list) int main(int ac, char **av, char **env_str) { char *line; - lst** env; + data_t data; lst*** cmds_list; (void) av; (void) ac; - env = env_init((const char **) env_str); - if (env == NULL) + data.env = env_init((const char **) env_str); + if (data.env == NULL) return (1); - line = get_user_input(env); + data.aliases = aliases_init(); + if (data.aliases == NULL) + { + lst_clear(data.env, &env_del); + return 1; + } + line = get_user_input(&data); while (line != NULL) { - cmds_list = parsing(line, env); + cmds_list = parsing(line, &data); free(line); if (cmds_list != NULL) { for (size_t i = 0; cmds_list[i] != NULL; i++) { - if (cmds_list_exec(cmds_list[i], env)) + if (cmds_list_exec(cmds_list[i], &data)) { - lst_clear(env, &env_del); + lst_clear(data.env, &env_del); return 1; } } cmds_list_destroyer(cmds_list); } - line = get_user_input(env); + line = get_user_input(&data); } - lst_clear(env, &env_del); + lst_clear(data.env, &env_del); return (0); } diff --git a/src/parsing/parsing.c b/src/parsing/parsing.c index 56cdfc2..1600c22 100644 --- a/src/parsing/parsing.c +++ b/src/parsing/parsing.c @@ -1,14 +1,24 @@ #include "parsing.h" -char* parsing_executable(const char* executable, lst** env) + +char* parsing_executable(const char* executable, data_t *data) { + const char* tmp; if (executable == NULL) return NULL; - if (strchr("./", executable[0])) - return strdup(executable); - return (get_executable_path(executable, env)); + if (executable[0] == '\\') + tmp = executable + 1; + else + { + tmp = get_alias(data->aliases, executable); + if (tmp == NULL) + tmp = executable; + } + if (strchr("./", tmp[0])) + return strdup(tmp); + return (get_executable_path(tmp, data->env)); } -int parsing_cmd(char *str, cmd* command, lst** env) +int parsing_cmd(char *str, cmd* command, data_t *data) { if (get_redirections(str, command)) return 1; @@ -17,11 +27,11 @@ int parsing_cmd(char *str, cmd* command, lst** env) return 1; for (size_t i = 0; command->args[i]; i++) quote_remover(command->args[i]); - command->executable = parsing_executable(command->args[0], env); + command->executable = parsing_executable(command->args[0], data); return 0; } -lst **parsing_pipe(const char *str, lst** env) +lst **parsing_pipe(const char *str, data_t *data) { char** cmds_str; lst** cmds; @@ -39,7 +49,7 @@ lst **parsing_pipe(const char *str, lst** env) current = *cmds; for (size_t i = 0; cmds_str[i] != NULL; i++) { - if (parsing_cmd(cmds_str[i], current->content, env)) + if (parsing_cmd(cmds_str[i], current->content, data)) { tab_free((void**)cmds_str); return NULL; @@ -50,7 +60,7 @@ lst **parsing_pipe(const char *str, lst** env) return (cmds); } -lst*** parsing(const char *line, lst** env) +lst*** parsing(const char *line, data_t *data) { char** line_commas; lst*** tab; @@ -67,7 +77,7 @@ lst*** parsing(const char *line, lst** env) size_t i; for (i = 0; line_commas[i] != NULL; i++) { - tab[i] = parsing_pipe(line_commas[i], env); + tab[i] = parsing_pipe(line_commas[i], data); if (tab[i] == NULL) { tab_free((void**) line_commas); diff --git a/src/parsing/parsing.h b/src/parsing/parsing.h index 267bf58..a79ab8a 100644 --- a/src/parsing/parsing.h +++ b/src/parsing/parsing.h @@ -3,4 +3,7 @@ #include "../exec/exec.h" #include "../../lib/bozolib/bozolib.h" #include "../redirection/redirection.h" -lst*** parsing(const char* str, lst** env); +#include "../data/data.h" +#include "../alias/alias.h" + +lst*** parsing(const char* str, data_t *data);