diff --git a/src/alias.c b/src/alias.c index fb292c5..b334ceb 100644 --- a/src/alias.c +++ b/src/alias.c @@ -12,6 +12,7 @@ #include #include "alias.h" +#include "common.h" #include "history.h" #include "utils.h" @@ -69,6 +70,7 @@ char *get_alias(char *buffer, int i, alias_t *alias) return buffer; } +static bool replace_alias(char **buffer, alias_t *alias) { char *tmp_buff = *buffer; @@ -78,12 +80,24 @@ bool replace_alias(char **buffer, alias_t *alias) tmp_buff = get_alias(tmp_buff, i, alias); if (tmp_buff == NULL) return false; + if (strncmp(*buffer, tmp_buff, strlen(*buffer)) == 0) + return false; i = skip_to_next_token(tmp_buff, i); } *buffer = tmp_buff; return true; } +int parse_alias(char **buffer, alias_t *alias) +{ + size_t i = 0; + + for (; i < 1000 && replace_alias(buffer, alias); i++); + if (i > 900) + return fprintf(stderr, "Alias loop.\n"), RETURN_FAILURE; + return RETURN_SUCCESS; +} + alias_t init_alias(void) { alias_t alias; diff --git a/src/alias.h b/src/alias.h index 997f6f1..6a939da 100644 --- a/src/alias.h +++ b/src/alias.h @@ -16,6 +16,6 @@ typedef struct alias_s { } alias_t; void free_alias(alias_t *alias); -bool replace_alias(char **buffer, alias_t *alias); +int parse_alias(char **buffer, alias_t *alias); alias_t init_alias(void); #endif /* ALIAS*/ diff --git a/src/builtins/where.c b/src/builtins/where.c index 1cffa75..5e8a8a5 100644 --- a/src/builtins/where.c +++ b/src/builtins/where.c @@ -68,7 +68,7 @@ bool search_cmd(ef_t *ef, char *arg) if (!alias_path.str) return false; - replace_alias(&alias_path.str, ef->exec_ctx->alias); + parse_alias(&alias_path.str, ef->exec_ctx->alias); if (strcmp(arg, alias_path.str) != 0) dprintf(ef->out_fd, "%s is aliased to %s\n", arg, alias_path.str); search_builtins(ef, arg); diff --git a/src/builtins/which.c b/src/builtins/which.c index 264e8e6..e332df5 100644 --- a/src/builtins/which.c +++ b/src/builtins/which.c @@ -54,7 +54,7 @@ bool search_cmd(ef_t *ef, char *arg) return false; if (bin_path == NULL) return free(alias_path.str), NULL; - replace_alias(&alias_path.str, ef->exec_ctx->alias); + parse_alias(&alias_path.str, ef->exec_ctx->alias); if (strcmp(arg, alias_path.str) != 0) return dprintf(ef->out_fd, "%s:\t aliased to %s\n", arg, alias_path.str), true; diff --git a/src/update_command.c b/src/update_command.c index 4a4205a..591cb68 100644 --- a/src/update_command.c +++ b/src/update_command.c @@ -53,7 +53,10 @@ size_t update_command(char **buffer, if (parse_history(buffer, &buffer_len, buffer_sz, &exec_ctx->history_command) == 84) return RETURN_SUCCESS; - replace_alias(buffer, exec_ctx->alias); + if (parse_alias(buffer, exec_ctx->alias) == RETURN_FAILURE) { + exec_ctx->history->last_exit_code = RETURN_FAILURE; + return 0; + } exec_ctx->history_command = save_command(*buffer, exec_ctx->history_command); return buffer_len; diff --git a/validation_tests.py b/validation_tests.py index 6d45ed3..90274f8 100644 --- a/validation_tests.py +++ b/validation_tests.py @@ -180,6 +180,7 @@ TESTS = [ cmds=[ "ll\nalias ll ls\nll\n", "alias ls ls\nls\nalias\n", + "alias a b\nalias b a\na\n", "ll\n", ], depends_on=()