mirror of
https://github.com/Savapitech/42sh.git
synced 2026-01-18 16:57:28 +01:00
Fix alias loop
Fix alias loop
This commit is contained in:
2
FIX.md
2
FIX.md
@@ -3,6 +3,6 @@
|
||||
- [ ] Up arrow that not get args
|
||||
- [ ] !x no args
|
||||
- [ ] Conditional jump/ invalid read with cd (PWD env var set)
|
||||
- [ ] Alias loop
|
||||
- [x] Alias loop
|
||||
- [ ] All AFL crashes
|
||||
- [ ] Fix CTRL+E and ->
|
||||
|
||||
1
Makefile
1
Makefile
@@ -18,6 +18,7 @@ LIB_NAME := libu.a
|
||||
SRC := $(wildcard src/*.c)
|
||||
SRC += $(wildcard src/builtins/*.c)
|
||||
SRC += $(wildcard src/builtins/expr/*.c)
|
||||
SRC += $(wildcard src/builtins/history/*.c)
|
||||
SRC += $(wildcard src/ast/*.c)
|
||||
SRC += $(wildcard src/utils/*.c)
|
||||
SRC += $(wildcard src/local/*.c)
|
||||
|
||||
11
src/alias.c
11
src/alias.c
@@ -12,7 +12,6 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alias.h"
|
||||
#include "common.h"
|
||||
#include "history.h"
|
||||
#include "utils.h"
|
||||
|
||||
@@ -70,7 +69,6 @@ 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;
|
||||
@@ -86,15 +84,6 @@ bool replace_alias(char **buffer, alias_t *alias)
|
||||
return true;
|
||||
}
|
||||
|
||||
int parse_alias(char **buffer, size_t *buffer_len, alias_t *alias)
|
||||
{
|
||||
bool need_to_replace = true;
|
||||
|
||||
while (need_to_replace)
|
||||
need_to_replace = replace_alias(buffer, alias);
|
||||
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);
|
||||
int parse_alias(char **buffer, size_t *buffer_len, alias_t *alias);
|
||||
bool replace_alias(char **buffer, alias_t *alias);
|
||||
alias_t init_alias(void);
|
||||
#endif /* ALIAS*/
|
||||
|
||||
@@ -16,21 +16,18 @@
|
||||
|
||||
void free_alias(alias_t *alias)
|
||||
{
|
||||
for (size_t i = 0; i != alias->size; i++){
|
||||
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)
|
||||
{
|
||||
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]);
|
||||
}
|
||||
for (size_t i = 0; i != alias->size; i++)
|
||||
printf("%s\t%s\n", alias->alias_array[i], alias->alias_to_replace[i]);
|
||||
return RETURN_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -47,16 +44,16 @@ int size_str_in_narray(int i, char **array)
|
||||
static
|
||||
char *array_nto_strdup(char **array, int i)
|
||||
{
|
||||
char *new_str = NULL;
|
||||
char *new_str = nullptr;
|
||||
int size_str = 0;
|
||||
int letter = 0;
|
||||
|
||||
if (len_array(array) < i)
|
||||
return NULL;
|
||||
return nullptr;
|
||||
size_str = size_str_in_narray(i, array);
|
||||
new_str = malloc(size_str + 1);
|
||||
if (new_str == NULL)
|
||||
return NULL;
|
||||
return nullptr;
|
||||
for (int j = i; array[j] != NULL; j++){
|
||||
for (int k = 0; array[j][k] != '\0'; k++){
|
||||
new_str[letter] = array[j][k];
|
||||
@@ -107,18 +104,13 @@ int add_alias(alias_t *alias, char **args)
|
||||
int builtins_alias(ef_t *ef, char **args)
|
||||
{
|
||||
alias_t *alias = ef->exec_ctx->alias;
|
||||
char *first_arg = args[1];
|
||||
|
||||
if (first_arg != NULL && strcmp(args[1], "-h") == 0){
|
||||
fprintf(stderr, "alias [cpy] [command]\n");
|
||||
return RETURN_FAILURE;
|
||||
}
|
||||
if (len_array(args) < 3){
|
||||
if (args[1] != NULL && strcmp(args[1], "-h") == 0)
|
||||
return fprintf(stderr, "alias [cpy] [command]\n"), RETURN_FAILURE;
|
||||
if (len_array(args) < 3) {
|
||||
builtins_display_alias(alias);
|
||||
return RETURN_SUCCESS;
|
||||
}
|
||||
alias->size++;
|
||||
if (add_alias(alias, args) == RETURN_FAILURE)
|
||||
return RETURN_FAILURE;
|
||||
return RETURN_SUCCESS;
|
||||
return add_alias(alias, args);
|
||||
}
|
||||
@@ -68,7 +68,7 @@ bool search_cmd(ef_t *ef, char *arg)
|
||||
|
||||
if (!alias_path.str)
|
||||
return false;
|
||||
parse_alias(&alias_path.str, &alias_path.sz, ef->exec_ctx->alias);
|
||||
replace_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;
|
||||
parse_alias(&alias_path.str, &alias_path.sz, ef->exec_ctx->alias);
|
||||
replace_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,7 @@ size_t update_command(char **buffer,
|
||||
if (parse_history(buffer, &buffer_len,
|
||||
buffer_sz, &exec_ctx->history_command) == 84)
|
||||
return RETURN_SUCCESS;
|
||||
parse_alias(buffer, &buffer_len, exec_ctx->alias);
|
||||
replace_alias(buffer, exec_ctx->alias);
|
||||
exec_ctx->history_command = save_command(*buffer,
|
||||
exec_ctx->history_command);
|
||||
return buffer_len;
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
** File description:
|
||||
** is_a_token
|
||||
*/
|
||||
|
||||
#include "string.h"
|
||||
#include "u_str.h"
|
||||
#include "ast.h"
|
||||
|
||||
bool is_a_token(char *str, int index_str)
|
||||
|
||||
@@ -179,6 +179,7 @@ TESTS = [
|
||||
name="alias",
|
||||
cmds=[
|
||||
"ll\nalias ll ls\nll\n",
|
||||
"alias ls ls\nls\nalias\n",
|
||||
"ll\n",
|
||||
],
|
||||
depends_on=()
|
||||
|
||||
Reference in New Issue
Block a user