Skip to content

Commit

Permalink
Merge pull request #75 from eguefif/test
Browse files Browse the repository at this point in the history
Finished prep test and heredoc and fixed bug
  • Loading branch information
eguefif authored Nov 23, 2023
2 parents 37ca584 + fa815d0 commit c0f747e
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NAME = minishell
CC = gcc
CFLAGS = -g -Wall -Werror -Wextra
CFLAGS = -g -Wall -Werror -Wextra# -fsanitize=address
INC = -Iincludes -I$(LIBFT_DIR)/includes -I$(READLINE_DIR)

LIB = -lft -L$(LIBFT_DIR) -lncurses -L$(READLINE_DIR) -lreadline
Expand All @@ -15,7 +15,7 @@ READLINE_SRC = $(LIB_DIR)/readline-8.2.tar.gz
READLINE = $(READLINE_DIR)/libreadline.a

_SRC_LIB_STATIC = parser.c executer.c cleaner.c lexer.c lexer_get_tokens.c error.c parser_get_commands.c parser_clean_commands1.c parser_clean_commands2.c lexer_count_tokens.c lexer_get_token_size.c parser_get_commands_populate.c \
utils.c executer_getpath.c environment.c signals.c builtin.c
utils.c executer_getpath.c environment.c signals.c builtin.c parser_clean_commands_stdin.c

_SRC = main.c $(_SRC_LIB_STATIC)
_OBJ = $(_SRC:.c=.o)
Expand Down
3 changes: 2 additions & 1 deletion includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maxpelle <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 18:40:07 by eguefif #+# #+# */
/* Updated: 2023/11/23 11:30:39 by eguefif ### ########.fr */
/* Updated: 2023/11/23 13:28:36 by eguefif ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -68,6 +68,7 @@ size_t get_count_var_env(char *token);
size_t get_env_var_len(char **env_var);
char **get_env_list(char *token, char **env);
size_t get_new_token_size(char *token, char **var_env);
char *clean_tokens_stdin(char *token);

int ms_execute(t_command *commnands, char ***env);
char *get_command_path(char *command, char **env);
Expand Down
12 changes: 9 additions & 3 deletions src/executer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maxpelle <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/13 12:16:43 by eguefif #+# #+# */
/* Updated: 2023/11/23 11:13:39 by eguefif ### ########.fr */
/* Updated: 2023/11/23 13:28:34 by eguefif ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -60,9 +60,15 @@ static int run(t_command *commands, char ***env)
return (1);
if (set_redirections(commands[i], pipe_fd,
commands[i + 1].last))
return (1);
{
ms_clean_commands(commands);
exit(0);
}
if (!commands[i].args[0])
return (0);
{
ms_clean_commands(commands);
exit (0);
}
ft_exit_nb(commands, handle_child(&commands[i], *env));
}
if (dup2(pipe_fd[0], 0) == -1)
Expand Down
2 changes: 1 addition & 1 deletion src/executer_getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: eguefif <[email protected].> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/20 18:40:07 by eguefif #+# #+# */
/* Updated: 2023/11/20 17:37:48 by eguefif ### ########.fr */
/* Updated: 2023/11/23 13:28:35 by eguefif ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
26 changes: 21 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maxpelle <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 18:42:06 by eguefif #+# #+# */
/* Updated: 2023/11/23 11:37:34 by eguefif ### ########.fr */
/* Updated: 2023/11/23 12:38:41 by eguefif ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -45,29 +45,45 @@ int main(int argc, char **argv, char **env)

char **non_interactive_mode(char **env)
{
t_command *commands;
char *line;
t_command *commands;
int retval;

line = get_next_line(0);
while (line)
{
retval = 0;
if (ms_init_signals() != 0)
break ;
if (check_valid_line_for_history(line))
{
commands = ms_parser(line, env);
if (commands->args[0] && ft_strcmp(commands->args[0], "exit") == 0)
{
if (commands->args[1])
retval = ft_atoi(commands->args[1]);
env = handle_exit_code(env, retval);
break ;
}
if (line)
free(line);
if (errno == ENOMEM)
break ;
if (commands)
{
retval = ms_execute(commands, &env);
env = handle_exit_code(env, retval);
}
line = get_next_line(0);
if (line)
free(line);
else
env = handle_exit_code(env, 258);
ms_clean_commands(commands);
}
else if (!line)
{
ft_printf("exit\n");
break ;
}
line = get_next_line(0);
}
return (env);
}
Expand Down
12 changes: 9 additions & 3 deletions src/parser_clean_commands1.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maxpelle <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 13:15:11 by maxpelle #+# #+# */
/* Updated: 2023/11/20 17:43:16 by eguefif ### ########.fr */
/* Updated: 2023/11/23 13:24:07 by eguefif ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -41,8 +41,14 @@ void parser_clean_commands(t_command *commands, char **env)
commands[i].args[j] = clean_tokens(commands[i].args[j], env);
}
if (commands[i].redirections.r_stdin)
commands[i].redirections.r_stdin
= clean_tokens(commands[i].redirections.r_stdin, env);
{
if (commands[i].redirections.heredoc > 0)
commands[i].redirections.r_stdin
= clean_tokens_stdin(commands[i].redirections.r_stdin);
else
commands[i].redirections.r_stdin
= clean_tokens(commands[i].redirections.r_stdin, env);
}
if (commands[i].redirections.r_stdout)
commands[i].redirections.r_stdout
= clean_tokens(commands[i].redirections.r_stdout, env);
Expand Down
2 changes: 1 addition & 1 deletion src/parser_clean_commands2.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maxpelle <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 14:59:51 by maxpelle #+# #+# */
/* Updated: 2023/11/20 17:44:15 by eguefif ### ########.fr */
/* Updated: 2023/11/23 12:59:52 by eguefif ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
60 changes: 60 additions & 0 deletions src/parser_clean_commands_stdin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_clean_commands_stdin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eguefif <[email protected].> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 13:07:15 by eguefif #+# #+# */
/* Updated: 2023/11/23 13:22:49 by eguefif ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

size_t get_quotes_number(char *token);

char *clean_tokens_stdin(char *token)
{
size_t size;
size_t i;
size_t j;
char *retval;

size = get_quotes_number(token);
retval = (char *) malloc(sizeof(char) * (size + 1));
if (!retval)
{
free(token);
return (0);
}
retval[size] = 0;
i = 0;
j = 0;
while (i < size)
{
if (!ft_strchr("\'\"", token[i]))
{
retval[j] = token[i];
j++;
}
i++;
}
return (retval);
}

size_t get_quotes_number(char *token)
{
size_t retval;
int i;

retval = 0;
i = 0;
while (token[i])
{
if (token[i] == '\'' || token[i] == '\"')
retval++;
i++;
}
return (retval);
}
9 changes: 7 additions & 2 deletions src/parser_get_commands_populate.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: eguefif <[email protected].> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 14:22:53 by eguefif #+# #+# */
/* Updated: 2023/11/17 14:45:58 by eguefif ### ########.fr */
/* Updated: 2023/11/23 12:51:53 by eguefif ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -86,7 +86,12 @@ static t_redirections populate_redirection(t_redirections redir,
if (token[0] == '<')
{
if (token[1] == '<')
redir.heredoc = 1;
{
if (ft_strchr(token, '\"') || ft_strchr(token, '\''))
redir.heredoc = 1;
else
redir.heredoc = 2;
}
if (redir.r_stdin)
free(redir.r_stdin);
redir.r_stdin = ft_strdup(file);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: eguefif <[email protected].> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/20 10:12:18 by eguefif #+# #+# */
/* Updated: 2023/11/20 17:27:02 by eguefif ### ########.fr */
/* Updated: 2023/11/23 13:28:34 by eguefif ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
9 changes: 0 additions & 9 deletions test/segfault_test/commands.tsv
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


:w
:
!
Expand Down Expand Up @@ -194,9 +192,6 @@ hola
hola Ctlr-\
cat (faire Ctlr-\ apres avoir fait plusieurs fois [ENTREE])
Ctlr-Z
sleep 3 | sleep 3 | sleep 3 (faire Ctlr-C une seconde apres)
sleep 3 | sleep 3 | sleep 3 (faire Ctlr-D une seconde apres)
sleep 3 | sleep 3 | sleep 3 (faire Ctlr-\ une seconde apres)
env
env hola
env hola que tal
Expand Down Expand Up @@ -537,10 +532,6 @@ hola
unset HOLA | cat -e
export HOLA | echo hola env | grep PROUT
export | echo hola
sleep 3 | sleep 3
time sleep 3 | sleep 3
sleep 3 | exit
exit | sleep 3
echo hola > a >>b echo que tal cat a | <b cat | cat > c | cat
pwd && ls
pwd || ls
Expand Down
2 changes: 1 addition & 1 deletion test/segfault_test/segfault.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ def test_segfault(command):
print("Error: timeout")

print(retval)
assert retval.returncode == 0
assert retval.returncode != -11

0 comments on commit c0f747e

Please sign in to comment.