diff --git a/src/shell.c b/src/shell.c index 8138cff..67f6357 100644 --- a/src/shell.c +++ b/src/shell.c @@ -55,32 +55,37 @@ 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 +int 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 1; + 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 0; + } + U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer); + visitor(tmp_buff, exec_ctx); + free(tmp_buff); + return 0; +} + 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); - free(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) == 1) + return exec_ctx->history->last_exit_code; } free(exec_ctx->history_command); return (free(buffer), exec_ctx->history->last_exit_code); diff --git a/src/update_command.c b/src/update_command.c index 0f0ca4c..7dbd333 100644 --- a/src/update_command.c +++ b/src/update_command.c @@ -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)