Merge pull request #5 from Savapitech/history_T_A

[FIX] FREE AND divide shell loop
This commit is contained in:
savalet
2025-04-17 20:00:22 +02:00
committed by GitHub
5 changed files with 30 additions and 20 deletions

View File

@@ -30,7 +30,7 @@ typedef struct history_command_s {
} his_command_t;
typedef struct parsing_history_s {
char *name;
char const *name;
char *(*funct)(char *, his_variable_t *, his_command_t *);
} parsing_history_t;

View File

@@ -15,7 +15,7 @@
static char *get_arg(char *line, int x, int end_cmd)
{
char *tmp = malloc(sizeof(char) * (x - end_cmd) + 1);
char *tmp = malloc(sizeof(char) * ((x - end_cmd) + 1));
if (tmp != NULL) {
tmp = strncpy(tmp, &line[end_cmd], x - end_cmd);
@@ -31,7 +31,7 @@ his_command_t set_cmd(char *line, his_command_t cmd_struct)
while (line[x] != '\0' && !isblank(line[x]))
x++;
cmd_struct.command = malloc(sizeof(char) * x + 1);
cmd_struct.command = malloc((sizeof(char) * x + 1));
if (cmd_struct.command != NULL) {
cmd_struct.command = strncpy(cmd_struct.command, line, x);
cmd_struct.command[x] = '\0';

View File

@@ -76,6 +76,7 @@ int choose_id_or_last(his_variable_t *his_variable, int index_str, char *str)
if (his_variable->str == NULL)
return 3;
his_variable->id = atoi(his_variable->str + 1);
free(his_variable->str);
return (mode == 1) ? 3 : 4;
}

View File

@@ -55,31 +55,38 @@ void write_prompt(int is_a_tty)
WRITE_CONST(STDOUT_FILENO, SHELL_PROMPT);
}
/*
** Noeud de fonction
** Pour changer la commande
** passer en parametre
** si besoin
*/
static
bool change_shell_command(char **buffer, exec_ctx_t *exec_ctx,
size_t buffer_sz)
{
size_t buffer_len = 0;
char *tmp_buff = NULL;
if (getline(buffer, &buffer_sz, stdin) == -1)
return false;
tmp_buff = (*buffer);
buffer_len = update_command(&tmp_buff, &buffer_sz, exec_ctx);
if (buffer_len < 1 || !u_str_is_alnum(tmp_buff)) {
check_basic_error(tmp_buff);
free(tmp_buff);
return true;
}
U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer);
visitor(tmp_buff, exec_ctx);
free(tmp_buff);
return false;
}
static
int shell_loop(int is_a_tty, exec_ctx_t *exec_ctx)
{
char *buffer = NULL;
size_t buffer_sz = 0;
size_t buffer_len = 0;
while (true) {
write_prompt(is_a_tty);
if (getline(&buffer, &buffer_sz, stdin) == -1)
break;
buffer_len = update_command(&buffer, &buffer_sz, exec_ctx);
if (buffer_len < 1 || !u_str_is_alnum(buffer)) {
check_basic_error(buffer);
continue;
}
U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer);
visitor(buffer, exec_ctx);
free(buffer);
if (change_shell_command(&buffer, exec_ctx, buffer_sz) == false)
return exec_ctx->history->last_exit_code;
}
free(exec_ctx->history_command);
return (free(buffer), exec_ctx->history->last_exit_code);

View File

@@ -38,6 +38,8 @@ size_t update_command(char **buffer,
size_t buffer_len = 0;
buffer_len = u_strlen(*buffer);
if (buffer_len < 2)
return 1;
(*buffer)[buffer_len - 1] = '\0';
if (parse_history(buffer, &buffer_len,
buffer_sz, &exec_ctx->history_command) == 84)