mirror of
https://github.com/Savapitech/42sh.git
synced 2026-01-18 16:57:28 +01:00
Fix alias loop and alias of alias
Fix alias loop
This commit is contained in:
14
src/alias.c
14
src/alias.c
@@ -12,6 +12,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#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;
|
||||
|
||||
@@ -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*/
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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=()
|
||||
|
||||
Reference in New Issue
Block a user