Skip to content

Commit

Permalink
Merge v0.7.1-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
frouioui committed May 24, 2018
1 parent 65bec1e commit 13b0c96
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 43 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ SRCS_TEST = $(PATH_TEST)/shell/check_args_test.c \
$(PATH_TEST)/shell/get_input_test.c \
$(PATH_TEST)/shell/modify_input_auto_test.c \
$(PATH_TEST)/shell/get_alias_test.c \
$(PATH_TEST)/shell/write_history_test.c \
$(PATH_TEST)/parsing/analyse_redirect_test.c \
$(PATH_TEST)/parsing/analyse_redirect_2_test.c \
$(PATH_TEST)/parsing/check_env_variable_test.c \
Expand Down
4 changes: 2 additions & 2 deletions include/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int destroy_shell(shell_t *);
int find_option_env(char **, char *);
int find_separator_env(char *);
unsigned int shell_loop(shell_t *);
unsigned int redirect_loop(shell_t *, char *);
unsigned int redirect_loop(shell_t *, char *, char *);
char **init_local(void);

/* --- basic init functions --- */
Expand All @@ -89,7 +89,7 @@ backup_t *initialisation_backup(char **);
char **init_paths(char **);

/* --- history function --- */
void write_command_history(command_line_t *, char **);
void write_command_history(char *, char **);

/* --- binding functions / redirection struct / macros --- */
binding_t *init_bindings(shell_t *shell);
Expand Down
4 changes: 2 additions & 2 deletions src/shell_loop/input/history_binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static char *up_history(shell_t *shell, char *input, int *i, int *hist_i)
new = get_line_history(shell, *hist_i);
if (new == NULL)
return (input);
for (int i = 0; i < (int)strlen(input); i++)
for (int a = 0; a < *i; a++)
my_putstr("\033[1D");
my_putstr(CLEAR_END_LINE);
write(0, new, strlen(new));
Expand All @@ -56,7 +56,7 @@ static char *down_history(shell_t *shell, char *input, int *i, int *hist_i)
new = get_line_history(shell, (*hist_i) - 1);
if (new == NULL)
return (input);
for (int i = 0; i < (int)strlen(input); i++)
for (int a = 0; a < *i; a++)
my_putstr("\033[1D");
my_putstr(CLEAR_END_LINE);
write(1, new, strlen(new));
Expand Down
19 changes: 10 additions & 9 deletions src/shell_loop/shell_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,32 @@ bool is_empty_input(char *user_input)
return (false);
}

unsigned int redirect_loop(shell_t *shell, char *user_input)
unsigned int redirect_loop(shell_t *shell, char *input, char *copy_input)
{
if (is_empty_input(user_input) == true) {
user_input = apply_transformation(shell->bonus, user_input,
if (is_empty_input(input) == true && copy_input != NULL) {
input = apply_transformation(shell->bonus, input,
shell->paths);
shell->command_line = get_command_line(shell->bonus,
user_input, shell->env, shell->local);
input, shell->env, shell->local);
if (shell->command_line == NULL)
return (FAILURE);
shell->code = execute_command(shell, shell->command_line);
write_command_history(shell->command_line, shell->paths);
write_command_history(copy_input, shell->paths);
free_command(shell->command_line);
update_backup(shell);
free(user_input);
free(input);
free(copy_input);
}
return (SUCCESS);
}

unsigned int shell_loop(shell_t *shell)
{
char *user_input = NULL;
char *input = NULL;

while (shell->state == OK && display_prompt(shell) &&
(user_input = get_input(shell, 1, 0)) != NULL) {
if (redirect_loop(shell, user_input) == FAILURE)
(input = get_input(shell, 1, 0)) != NULL) {
if (redirect_loop(shell, input, strdup(input)) == FAILURE)
return (FAILURE);
}
return (SUCCESS);
Expand Down
11 changes: 8 additions & 3 deletions src/shell_loop/write_command_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
#include "shell.h"
#include "instruction.h"

void write_command_history(command_line_t *cmd, char **paths)
void write_command_history(char *cmd, char **paths)
{
int fd = 0;
char *path = NULL;

fd = open(paths[1], O_WRONLY | O_CREAT | O_APPEND,
if (paths == NULL)
path = ".history42";
else
path = paths[1];
fd = open(path, O_WRONLY | O_CREAT | O_APPEND,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd != -1) {
write(fd, cmd->full_command, strlen(cmd->full_command));
write(fd, cmd, strlen(cmd));
write(fd, "\n", 1);
close(fd);
}
Expand Down
27 changes: 9 additions & 18 deletions tests/execution/history_builtin_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ Test(history_built, display_full_history)
FILE *fp = NULL;

close(open(shell->paths[1], O_RDONLY | O_TRUNC));
write_command_history(get_command_line(true, "echo a > z",
shell->env, NULL), shell->paths);
write_command_history("echo a > z", shell->paths);
shell->command_line = get_command_line(true, "history > buf",
shell->env, NULL);
execute_command(shell, shell->command_line);
Expand All @@ -55,10 +54,8 @@ Test(history_built, display_full_history_2)
FILE *fp = NULL;

close(open(shell->paths[1], O_RDONLY | O_TRUNC));
write_command_history(get_command_line(true, "echo a > z",
shell->env, NULL), shell->paths);
write_command_history(get_command_line(true, "vald",
shell->env, NULL), shell->paths);
write_command_history("echo a > z", shell->paths);
write_command_history("vald", shell->paths);
shell->command_line = get_command_line(true, "history > buf2",
shell->env, NULL);
execute_command(shell, shell->command_line);
Expand All @@ -72,12 +69,9 @@ Test(history_built, display_2)
FILE *fp = NULL;

close(open(shell->paths[1], O_RDONLY | O_TRUNC));
write_command_history(get_command_line(true, "echo a > z",
shell->env, NULL), shell->paths);
write_command_history(get_command_line(true, "vald",
shell->env, NULL), shell->paths);
write_command_history(get_command_line(true, "valdgrind ; toto",
shell->env, NULL), shell->paths);
write_command_history("echo a > z", shell->paths);
write_command_history("vald", shell->paths);
write_command_history("valdgrind ; toto", shell->paths);
shell->command_line = get_command_line(true, "history 2 > buf2",
shell->env, NULL);
execute_command(shell, shell->command_line);
Expand All @@ -91,12 +85,9 @@ Test(history_built, display_too_big)
FILE *fp = NULL;

close(open(shell->paths[1], O_RDONLY | O_TRUNC));
write_command_history(get_command_line(true, "echo a > z",
shell->env, NULL), shell->paths);
write_command_history(get_command_line(true, "vald",
shell->env, NULL), shell->paths);
write_command_history(get_command_line(true, "valdgrind ; toto",
shell->env, NULL), shell->paths);
write_command_history("echo a > z", shell->paths);
write_command_history("vald", shell->paths);
write_command_history("valdgrind ; toto", shell->paths);
shell->command_line = get_command_line(true, "history 24 > buf2",
shell->env, NULL);
execute_command(shell, shell->command_line);
Expand Down
4 changes: 2 additions & 2 deletions tests/execution/setenv_crash_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Test(setenv_builtin, crash_test_1)
env[1] = NULL;
shell = initialisation_shell(1, NULL, env);
for (int i = 0; i < 11; i++)
redirect_loop(shell, cmd[i]);
redirect_loop(shell, cmd[i], strdup(cmd[i]));
}

Test(setenv_builtin, crash_test_2)
Expand All @@ -54,5 +54,5 @@ Test(setenv_builtin, crash_test_2)
env[1] = NULL;
shell = initialisation_shell(1, NULL, env);
for (int i = 0; i < 14; i++)
redirect_loop(shell, cmd[i]);
redirect_loop(shell, cmd[i], strdup(cmd[i]));
}
14 changes: 7 additions & 7 deletions tests/shell/write_history_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ Test(write_command_history, simple_command_1)
"PWD=/home/marvin"};
FILE *fp;

close(open(".history", O_RDWR | O_TRUNC));
close(open(".history42", O_RDWR | O_TRUNC));
for (int i = 0; i < 4; i++) {
env[i] = malloc(sizeof(char) * my_strlen(str[i]));
env[i] = my_strcpy(env[i], str[i]);
}
env[4] = NULL;
cmd = get_command_line(true, "ls -l", env, NULL);
write_command_history(true, cmd);
fp = fopen (".history","r");
write_command_history(cmd->full_command, NULL);
fp = fopen (".history42","r");
cr_assert_file_contents_eq_str(fp, "ls -l\n");
}

Expand All @@ -43,16 +43,16 @@ Test(write_command_history, simple_command_2)
"PWD=/home/marvin"};
FILE *fp;

close(open(".history", O_RDWR | O_TRUNC));
close(open(".history42", O_RDWR | O_TRUNC));
for (int i = 0; i < 4; i++) {
env[i] = malloc(sizeof(char) * my_strlen(str[i]));
env[i] = my_strcpy(env[i], str[i]);
}
env[4] = NULL;
cmd = get_command_line(true, "ls -l", env, NULL);
write_command_history(true, cmd);
write_command_history(cmd->full_command, NULL);
cmd = get_command_line(true, "env", env, NULL);
write_command_history(true, cmd);
fp = fopen (".history","r");
write_command_history(cmd->full_command, NULL);
fp = fopen (".history42","r");
cr_assert_file_contents_eq_str(fp, "ls -l\nenv\n");
}

0 comments on commit 13b0c96

Please sign in to comment.