Skip to content

Commit

Permalink
Norminette check (#34)
Browse files Browse the repository at this point in the history
* Fix norm errors

* Fix memory leaks

* Fix crash when there's no limiter for heredoc
  • Loading branch information
servettonga authored Jul 19, 2024
1 parent d8d1bbf commit 5f8d7c1
Show file tree
Hide file tree
Showing 36 changed files with 749 additions and 884 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ SRCS = $(SRC_DIR)/main.c \
$(SRC_DIR)/exec/execute_utils.c \
$(SRC_DIR)/parser/create_default_pipeline_node.c \
$(SRC_DIR)/parser/parser.c \
$(SRC_DIR)/parser/remove_cmd_arg.c \
$(SRC_DIR)/parser/parser_utils.c \
$(SRC_DIR)/parser/replace_vars.c \
$(SRC_DIR)/parser/replace_wildcards.c \
$(SRC_DIR)/parser/set_redirections.c \
Expand All @@ -54,6 +54,7 @@ OBJS = $(SRCS:.c=.o)
# Include files
INCS = $(INC_DIR)/environment.h \
$(INC_DIR)/execute.h \
$(INC_DIR)/messages.h \
$(INC_DIR)/minishell.h \
$(INC_DIR)/parser.h \
$(INC_DIR)/signals.h \
Expand Down
47 changes: 47 additions & 0 deletions include/messages.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* messages.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sehosaf <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 10:21:05 by sehosaf #+# #+# */
/* Updated: 2024/07/18 21:05:21 by sehosaf ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef MESSAGES_H
# define MESSAGES_H

# define PROMPT "\033[0;36mminishell\033[0m \033[0;32m> \033[0m"

// ** Error messages **
# define ERR_GENERIC "\033[0;31mError: \033[0m"
# define ERR_MALLOC "minishell: malloc failed"
# define ERR_PERM "Permission denied"

// ** Syntax error messages **
# define ERR_SNX_TOKEN "minishell: syntax error near unexpected token `"
# define ERR_SNX_EOF "minishell: syntax error: unexpected end of file "

// ** Built-in commands messages **
# define ERR_NO_HOME "minishell: cd: HOME not set "
# define ERR_NO_CD "minishell: cd: can't cd to "
# define ERR_RET_CWD "minishell: cd: error retrieving current directory "
# define ERR_MANY_ARGS "minishell: exit: too many arguments "
# define ERR_PWD "minishell: pwd: error "

// ** Execution messages **
# define ERR_IS_DIR "minishell: is a directory: "
# define ERR_FORK "minishell: fork failed "
# define ERR_EXEC "minishell: execve failed "
# define ERR_ENV "minishell: environment list is empty "
# define ERR_INPUT_FILE "minishell: input file error: "
# define ERR_OUTPUT_FILE "minishell: output file error: "
# define ERR_PIPE "minishell: pipe error: "

// ** Signals messages **
# define ERR_SIGINT "minishell: sigaction: SIGINT "
# define ERR_SIGQUIT "minishell: sigaction: SIGQUIT "

#endif
9 changes: 4 additions & 5 deletions include/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmoroz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 20:37:44 by sehosaf #+# #+# */
/* Updated: 2024/07/10 15:51:34 by dmoroz ### ########.fr */
/* Updated: 2024/07/17 14:23:21 by sehosaf ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -29,6 +29,7 @@
# include <stdbool.h>
# include <limits.h>
# include "libft.h"
# include "messages.h"

/*
t_command struct represent command which has to be executed
Expand Down Expand Up @@ -59,12 +60,12 @@ typedef enum e_connection
typedef struct s_command
{
char **args;
t_connection connection_type;
t_connection conn_type;
int is_heredoc;
char *limiter;
char *infile;
char *outfile;
int outfile_append_mode;
int ap_mode;
} t_command;

typedef struct s_pipeline
Expand Down Expand Up @@ -106,6 +107,4 @@ void free_pipeline(t_pipeline *p);
void interactive_signal_handlers(void);
void non_interactive_signal_handlers(void);

# define PROMPT "\033[0;36mminishell\033[0m \033[0;32m> \033[0m"

#endif
16 changes: 15 additions & 1 deletion include/parser.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sehosaf <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 09:26:08 by sehosaf #+# #+# */
/* Updated: 2024/07/18 17:19:48 by sehosaf ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PARSER_H
# define PARSER_H

Expand All @@ -7,9 +19,11 @@
char **split_line(char *line);
void split_tokens_per_command(t_pipeline *start, char **tokens);
t_pipeline *create_default_pipeline_node(void);
void set_redirections(t_pipeline *node);
int set_redirections(t_pipeline *node);
void remove_cmd_arg(t_pipeline *node, int ind);
void replace_vars(t_pipeline *node, t_shell *shell);
void replace_wildcards(t_pipeline *node);
char *get_var_name(char *dollar);
char *get_var_value(const char *name, t_shell *shell);

#endif
6 changes: 3 additions & 3 deletions libft/libft/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sehosaf <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 18:53:52 by dmoroz #+# #+# */
/* Updated: 2024/06/12 10:32:48 by sehosaf ### ########.fr */
/* Updated: 2024/07/17 20:42:46 by sehosaf ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -42,8 +42,8 @@ size_t ft_strlcat(char *dst, const char *src, size_t size);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
char *ft_strnstr(const char *str, const char *sub, size_t len);
int ft_strcmp(const char *s1, const char *s2);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_strcmp(const char *s1, const char *s2);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_atoi(const char *str);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
char *ft_substr(char const *s, unsigned int start, size_t len);
Expand Down
10 changes: 5 additions & 5 deletions src/builtin/cmd_cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sehosaf <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/10 20:37:41 by sehosaf #+# #+# */
/* Updated: 2024/06/19 21:03:35 by sehosaf ### ########.fr */
/* Updated: 2024/07/17 11:30:10 by sehosaf ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -31,7 +31,7 @@ int cmd_cd(t_shell *shell, char **args)
change_to = get_env_val(shell->env, "HOME");
if (change_to == NULL)
{
ft_putendl_fd("minishell: cd: HOME not set", 2);
ft_putendl_fd(ERR_NO_HOME, 2);
return (EXIT_FAILURE);
}
}
Expand All @@ -43,7 +43,7 @@ int cmd_cd(t_shell *shell, char **args)
old_pwd = getcwd(NULL, 0);
if (chdir(change_to) != 0)
{
ft_putstr_fd("minishell: cd: can't cd to ", 2);
ft_putstr_fd(ERR_NO_CD, 2);
ft_putendl_fd(change_to, 2);
return (free(old_pwd), EXIT_FAILURE);
}
Expand All @@ -52,7 +52,7 @@ int cmd_cd(t_shell *shell, char **args)

static bool is_home_keyword(const char *arg)
{
if (arg[0] == '~' && (arg[1] == '\0'))
if (arg[0] == '~' && arg[1] == '\0')
return (true);
else if (ft_strcmp(arg, "--") == 0)
return (true);
Expand All @@ -70,7 +70,7 @@ static int update_pwd_env(t_shell *shell, char *old_pwd)
new_pwd = getcwd(NULL, 0);
if (new_pwd == NULL)
{
ft_putendl_fd("minishell: cd: error retrieving current directory", 2);
ft_putendl_fd(ERR_RET_CWD, 2);
return (free(old_pwd), EXIT_FAILURE);
}
if (set_env_var(shell->env, "OLDPWD", old_pwd) == EXIT_FAILURE)
Expand Down
6 changes: 3 additions & 3 deletions src/builtin/cmd_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sehosaf <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/09 19:04:21 by sehosaf #+# #+# */
/* Updated: 2024/06/24 21:06:54 by sehosaf ### ########.fr */
/* Updated: 2024/07/17 11:32:13 by sehosaf ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -24,11 +24,11 @@ int cmd_exit(t_shell *shell, char **args, t_connection ct)
shell->exit_status = EXIT_SUCCESS;
else if (args[2])
{
ft_putendl_fd("minishell: exit: too many arguments", STDERR_FILENO);
ft_putendl_fd(ERR_MANY_ARGS, STDERR_FILENO);
return (EXIT_FAILURE);
}
if (is_number(args[1]))
shell->exit_status = ft_atol(args[1]);
shell->exit_status = (signed int)ft_atol(args[1]);
else if (args[1] && !is_number(args[1]))
{
ft_putstr_fd("minishell: exit: ", STDERR_FILENO);
Expand Down
4 changes: 2 additions & 2 deletions src/builtin/cmd_pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sehosaf <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/02 14:08:08 by sehosaf #+# #+# */
/* Updated: 2024/06/19 20:53:20 by sehosaf ### ########.fr */
/* Updated: 2024/07/17 11:33:39 by sehosaf ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,7 +22,7 @@ int cmd_pwd(void)

if (getcwd(cwd, PATH_MAX) == NULL)
{
ft_putendl_fd("minishell: pwd: error", STDERR_FILENO);
ft_putendl_fd(ERR_PWD, STDERR_FILENO);
return (EXIT_FAILURE);
}
return (ft_putendl_fd(cwd, STDOUT_FILENO), EXIT_SUCCESS);
Expand Down
37 changes: 17 additions & 20 deletions src/env/env_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* env_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmoroz <dmoroz@student.42warsaw.pl> +#+ +:+ +#+ */
/* By: sehosaf <sehosaf@student.42warsaw.pl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 21:34:34 by sehosaf #+# #+# */
/* Updated: 2024/07/08 16:12:38 by dmoroz ### ########.fr */
/* Updated: 2024/07/18 15:07:15 by sehosaf ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,25 +28,22 @@ int init_environment(t_shell *shell)
t_env *last_node;
int i;

if (shell && shell->env == NULL)
shell->env = NULL;
i = 0;
while (environ[i])
{
i = 0;
while (environ[i])
{
key_value = split_key_value(environ[i], '=');
new_node = create_env_node(key_value);
if (handle_errors(new_node, key_value) == EXIT_FAILURE)
return (EXIT_FAILURE);
if (i == 0)
shell->env = new_node;
else
last_node->next = new_node;
last_node = new_node;
i++;
}
return (EXIT_SUCCESS);
key_value = split_key_value(environ[i], '=');
new_node = create_env_node(key_value);
if (handle_errors(new_node, key_value) == EXIT_FAILURE)
return (EXIT_FAILURE);
if (i == 0)
shell->env = new_node;
else
last_node->next = new_node;
last_node = new_node;
i++;
}
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}

static int handle_errors(t_env *new_node, char **key_value)
Expand All @@ -61,7 +58,7 @@ static int handle_errors(t_env *new_node, char **key_value)
}
if (new_node == NULL)
{
perror("malloc");
perror(ERR_MALLOC);
ret = EXIT_FAILURE;
}
if (key_value)
Expand Down
4 changes: 2 additions & 2 deletions src/env/env_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sehosaf <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 12:42:49 by sehosaf #+# #+# */
/* Updated: 2024/06/19 21:14:07 by sehosaf ### ########.fr */
/* Updated: 2024/07/17 11:35:00 by sehosaf ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -57,7 +57,7 @@ char **split_key_value(const char *str, char c)

result = (char **)malloc(sizeof(char *) * 3);
if (!result)
return (perror("malloc"), NULL);
return (perror(ERR_MALLOC), NULL);
index = ft_strchr(str, c);
if (index)
{
Expand Down
8 changes: 4 additions & 4 deletions src/exec/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sehosaf <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 21:04:06 by sehosaf #+# #+# */
/* Updated: 2024/06/24 21:46:51 by sehosaf ### ########.fr */
/* Updated: 2024/07/17 12:32:00 by sehosaf ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,7 +39,7 @@ void execute(t_pipeline *pipeline, t_shell *shell)
while (cur != NULL)
{
if (!async
&& !should_execute(cur->cmd.connection_type, shell->exit_status))
&& !should_execute(cur->cmd.conn_type, shell->exit_status))
break ;
cmds[i] = find_type(shell, pipeline, cur);
if (!async)
Expand Down Expand Up @@ -77,13 +77,13 @@ static pid_t absolute_path(t_shell *shell, t_pipeline *p, t_pipeline *cur)

if (stat(cur->cmd.args[0], &st) == -1)
{
perror("minishell: ");
perror(ERR_GENERIC);
shell->exit_status = 127;
return (EXIT_FAILURE);
}
if (S_ISDIR(st.st_mode))
{
ft_putendl_fd("minishell: is a directory", 2);
ft_putendl_fd(ERR_IS_DIR, 2);
shell->exit_status = 126;
return (EXIT_FAILURE);
}
Expand Down
2 changes: 1 addition & 1 deletion src/exec/execute_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int execute_builtin(t_command command, t_shell *shell)
t_connection connection_type;

ret = EXIT_SUCCESS;
connection_type = command.connection_type;
connection_type = command.conn_type;
cmd_name = command.args[0];
if (ft_strcmp(cmd_name, "cd") == 0)
ret = cmd_cd(shell, command.args);
Expand Down
Loading

0 comments on commit 5f8d7c1

Please sign in to comment.