mirror of
https://github.com/Savapitech/42sh.git
synced 2026-03-18 21:50:35 +01:00
[ADD] Alias builtin
This commit is contained in:
@@ -15,6 +15,9 @@
|
||||
typedef struct alias_s{
|
||||
size_t size;
|
||||
char **alias_array;
|
||||
char **alias_to_replace;
|
||||
} alias_t;
|
||||
|
||||
void free_alias(alias_t *alias);
|
||||
|
||||
#endif /* ALIAS*/
|
||||
|
||||
@@ -10,25 +10,44 @@
|
||||
#include "env.h"
|
||||
#include "exec.h"
|
||||
#include "alias.h"
|
||||
#include <string.h>
|
||||
#include "utils.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void free_alias(alias_t *alias)
|
||||
{
|
||||
for (size_t i = 0; i != alias->size; i++){
|
||||
free(alias->alias_array[i]);
|
||||
free(alias->alias_to_replace[i]);
|
||||
}
|
||||
free(alias->alias_array);
|
||||
free(alias->alias_to_replace);
|
||||
return ;
|
||||
}
|
||||
|
||||
int builtins_display_alias(alias_t *alias)
|
||||
{
|
||||
printf("DISPLAY ALIAS\n");
|
||||
for (size_t i = 0; i != alias->size; i++){
|
||||
printf("|| Alias: %s || ", alias->alias_array[i]);
|
||||
printf("Command: %s ||\n", alias->alias_to_replace[i]);
|
||||
}
|
||||
return RETURN_SUCCESS;
|
||||
}
|
||||
|
||||
int builtins_alias(ef_t *ef, char **args)
|
||||
{
|
||||
alias_t alias; //= ef->builtin_handler->history_command;
|
||||
alias_t *alias = ef->builtin_handler->alias;
|
||||
char *first_arg = args[1];
|
||||
|
||||
alias.size = 0;
|
||||
alias.alias_array = NULL;
|
||||
if (first_arg != NULL && strcmp(args[1], "--display") == 0)
|
||||
return builtins_display_alias(&alias);
|
||||
|
||||
printf("ARG 1: %s\n", args[1]);
|
||||
return builtins_display_alias(alias);
|
||||
if (len_array(args) != 3)
|
||||
return RETURN_FAILURE;
|
||||
alias->alias_array[alias->size - 1] = strdup(args[1]);
|
||||
alias->alias_to_replace[alias->size - 1] = strdup(args[2]);
|
||||
printf("size alias %d\n",len_array(args));// alias->size);
|
||||
printf("J ACTIVE LA SAUVEGARDE DE L ALIAS\n");
|
||||
//return RETURN_FAILURE;
|
||||
return RETURN_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,11 +11,13 @@
|
||||
#include "env.h"
|
||||
#include "history.h"
|
||||
#include "shell.h"
|
||||
#include "alias.h"
|
||||
|
||||
typedef struct {
|
||||
env_t *env;
|
||||
history_t *history;
|
||||
his_command_t *history_command;
|
||||
alias_t *alias;
|
||||
} builtin_handler_t;
|
||||
|
||||
size_t update_command(char **buffer,
|
||||
|
||||
42
src/shell.c
42
src/shell.c
@@ -19,6 +19,7 @@
|
||||
#include "u_str.h"
|
||||
#include "history.h"
|
||||
#include "exec.h"
|
||||
#include "alias.h"
|
||||
|
||||
__attribute__((unused))
|
||||
static
|
||||
@@ -105,25 +106,52 @@ his_command_t *init_cmd_history(void)
|
||||
** l initalisation de builtin handler dans
|
||||
** une fonction pour l env l' history et les futurs builtins
|
||||
*/
|
||||
alias_t init_alias(void)
|
||||
{
|
||||
alias_t alias;
|
||||
|
||||
alias.size = 1;
|
||||
alias.alias_array = malloc(sizeof(char *) * alias.size);
|
||||
alias.alias_to_replace = malloc(sizeof(char *) * alias.size);
|
||||
return alias;
|
||||
}
|
||||
|
||||
static
|
||||
bool error_in_init(builtin_handler_t *builtin_handler)
|
||||
{
|
||||
if (!builtin_handler->history_command || !builtin_handler->env->env || !builtin_handler->alias->alias_array || !builtin_handler->alias->alias_to_replace){
|
||||
if (builtin_handler->history_command)
|
||||
free(builtin_handler->history_command);
|
||||
if (builtin_handler->env->env)
|
||||
free(builtin_handler->env->env);
|
||||
if (builtin_handler->alias->alias_array)
|
||||
free(builtin_handler->alias->alias_array);
|
||||
if (!builtin_handler->alias->alias_to_replace)
|
||||
free(builtin_handler->alias->alias_to_replace);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int shell(char **env_ptr)
|
||||
{
|
||||
alias_t alias = init_alias();
|
||||
env_t env = parse_env(env_ptr);
|
||||
history_t history = { .cmd_history = NULL, 0, .last_chdir = NULL};
|
||||
his_command_t *cmd_history = init_cmd_history();
|
||||
builtin_handler_t builtin_handler = {.env = &env,
|
||||
.history = &history, .history_command = cmd_history};
|
||||
.history = &history, .history_command = cmd_history,
|
||||
.alias = &alias};
|
||||
int shell_result;
|
||||
|
||||
if (!cmd_history || !env.env){
|
||||
if (cmd_history)
|
||||
free(cmd_history);
|
||||
if (env.env)
|
||||
free(env.env);
|
||||
if (error_in_init(&builtin_handler) == true){
|
||||
free_alias(builtin_handler.alias);
|
||||
return RETURN_FAILURE;
|
||||
}
|
||||
U_DEBUG_CALL(debug_env_entries, &env);
|
||||
signal(SIGINT, ignore_sigint);
|
||||
shell_result = shell_loop(isatty(STDIN_FILENO), &builtin_handler);
|
||||
free_env(&env);
|
||||
free_env(builtin_handler.env);
|
||||
free_alias(builtin_handler.alias);
|
||||
return shell_result;
|
||||
}
|
||||
|
||||
@@ -14,4 +14,5 @@
|
||||
char *strn_to_ndup(int start, int size, char *str);
|
||||
bool is_a_token(char *str, int index_str);
|
||||
char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy);
|
||||
int len_array(char **array);
|
||||
#endif /* UTILS_H */
|
||||
|
||||
17
src/utils/len_array.c
Normal file
17
src/utils/len_array.c
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2025
|
||||
** 42sh
|
||||
** File description:
|
||||
** len_array
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
int len_array(char **array)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while(array[i] != NULL)
|
||||
i++;
|
||||
return i;
|
||||
}
|
||||
Reference in New Issue
Block a user