Commit 454c92a6 authored by pêle-mêle's avatar pêle-mêle
Browse files

reorg + begin to manage write errors

parent 6bc99dc5
......@@ -3,6 +3,7 @@ NAME = minishell
SRC_DIR += src/
SRC_DIR += src/parsing/
SRC_DIR += src/execution/
SRC_DIR += src/builtins/
SRC += main.c
SRC += signal_handling.c
......@@ -15,16 +16,16 @@ SRC += parsing_vars_redirs.c
SRC += parsing_utils.c
SRC += exec_handler.c
SRC += launch_command.c
SRC += options.c
SRC += ft_echo.c
SRC += ft_pwd.c
SRC += ft_cd.c
SRC += var_utils.c
SRC += ft_export.c
SRC += ft_unset.c
SRC += ft_env.c
SRC += options.c
SRC += var_utils.c
SRC += ft_error.c
......@@ -35,7 +36,6 @@ INC_DIR = inc/
HDR += minishell.h
HDR += signals.h
HDR += parsing.h
HDR += pre_execution.h
HDR += execution.h
vpath %.h $(INC_DIR)
......
#ifndef PRE_EXECUTION_H
# define PRE_EXECUTION_H
/*
** main function
*/
char **prepare_args(t_command *command, char **env, int stat_loc);
#endif
......@@ -6,7 +6,7 @@
/* By: mvidal-a <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/04 12:29:06 by mvidal-a #+# #+# */
/* Updated: 2021/01/27 11:54:32 by mvidal-a ### ########.fr */
/* Updated: 2021/01/28 17:14:54 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
......@@ -22,6 +22,8 @@
# define TRUE 1
# define FALSE 0
# define WRITE_SUCCESS -2
typedef struct s_list
{
void *content;
......
......@@ -6,27 +6,32 @@
/* By: mvidal-a <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/07 18:10:47 by mvidal-a #+# #+# */
/* Updated: 2020/06/25 13:44:13 by mvidal-a ### ########.fr */
/* Updated: 2021/01/28 17:22:24 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** return value (inherited from write):
** nonnegative on success (nb of chars written without the newline)
** -1 + errno set on error
** return value:
** -2 on success (the total nb of bytes requested have been written)
** nonnegative if the nb of bytes written is smaller than requested
** -1 on error
*/
ssize_t ft_putendl_fd(const char *s, int fd)
{
ssize_t ret;
ssize_t ret_str;
ssize_t ret_nl;
ret = ft_putstr_fd(s, fd);
if (ret != -1)
ret_str = ft_putstr_fd(s, fd);
if (ret_str == WRITE_SUCCESS)
{
if (ft_putchar_fd('\n', fd) == -1)
ret = -1;
ret_nl = ft_putchar_fd('\n', fd);
if (ret_nl == -1)
ret_str = ERROR;
else if (ret_nl == 0)
ret_str = ft_strlen(s);
}
return (ret);
return (ret_str);
}
......@@ -6,7 +6,7 @@
/* By: mvidal-a <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/07 18:06:07 by mvidal-a #+# #+# */
/* Updated: 2020/06/25 13:38:25 by mvidal-a ### ########.fr */
/* Updated: 2021/01/28 17:14:12 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
......@@ -14,12 +14,21 @@
#include <unistd.h>
/*
** return value (inherited from write):
** nonnegative on success (nb of chars written)
** -1 + errno set on error
** return value:
** -2 on success (the total nb of bytes requested have been written)
** nonnegative if the nb of bytes written is smaller than requested
** -1 on error
*/
ssize_t ft_putstr_fd(const char *s, int fd)
ssize_t ft_putstr_fd(const char *s, int fd)
{
return (write(fd, s, ft_strlen(s)));
size_t len;
ssize_t ret;
len = ft_strlen(s);
ret = write(fd, s, len);
if (ret == (ssize_t)len)
return (WRITE_SUCCESS);
else
return (ret);
}
#include "execution.h"
// >>> echo
// error management: stop quand un write fail, ou alors on fait un seul write pour tout
// no nl: "This may also be achieved by appending `\c' to the end of the string" (man echo sur mac mais pas linux)
int ft_echo(char **args, t_xe *xe)
{
t_byte options;
......@@ -10,7 +7,10 @@ int ft_echo(char **args, t_xe *xe)
(void)xe;
options = shift_options(&args, ECHO_OPTIONS);
if (*args != NULL)
ft_putstr_fd(*args++, STDOUT_FILENO);
{
if (ft_putstr_fd(*args++, STDOUT_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
}
while (*args != NULL)
{
ft_putchar_fd(' ', STDOUT_FILENO);
......
......@@ -200,7 +200,7 @@ static int ft_export_checks(char **args, t_xe *xe)
}
}
else
putstr_stderr("export: Variable identifier (name) invalid\n"); // leaks si le write marche pas
printf("export: Variable identifier (name) invalid\n"); // ERROR CODE
args++;
}
return (SUCCESS);
......
......@@ -9,12 +9,11 @@ int ft_pwd(char **args, t_xe *xe)
buf = getcwd(NULL, 0);
if (buf == NULL)
{
putstr_stderr("pwd : erreur de détermination du répertoire actuel : getcwd : ne peut accéder aux répertoires parents : Aucun fichier ou dossier de ce type\n"); // leaks si le write marche pas
printf("pwd : erreur de détermination du répertoire actuel : getcwd : ne peut accéder aux répertoires parents : Aucun fichier ou dossier de ce type\n"); // ERROR CODE
xe->stat_loc = 1;
return (SUCCESS); // other code!
}
ft_putstr_fd(buf, STDOUT_FILENO);
ft_putchar_fd('\n', STDOUT_FILENO);
ft_putendl_fd(buf, STDOUT_FILENO);
free(buf);
xe->stat_loc = 0;
return (SUCCESS);
......
......@@ -39,7 +39,7 @@ int ft_unset(char **args, t_xe *xe)
}
else
{
putstr_stderr("unset: Variable identifier (name) invalid\n"); // leaks si le write marche pas
printf("unset: Variable identifier (name) invalid\n"); // ERROR CODE
xe->stat_loc = 1;
return (SUCCESS);
}
......
#include "minishell.h"
#include "signals.h"
#include "parsing.h"
#include "pre_execution.h"
#include "execution.h"
#include <sys/types.h> // waitpid
......
......@@ -204,7 +204,7 @@ void apply_redir(char *cur_arg, enum e_redir_op redir)
flags |= O_APPEND;
}
redir_fd = open(cur_arg, flags, mode);
if (redir_fd >= 0) // else error?
if (redir_fd >= 0) // else error
{
dup2(redir_fd, src_fd); // error
}
......
......@@ -3,15 +3,6 @@
#include "parsing.h"
#include <stdio.h> // perror
void putstr_stderr(const char *str)
{
if (ft_putstr_fd(str, STDERR_FILENO) == ERROR)
{
perror("Cannot write the error message on STDERR");
exit(EXIT_FAILURE);
}
}
int parsing_error(int err_code, t_xe *xe)
{
const char *err_msg[] = {
......@@ -24,8 +15,7 @@ int parsing_error(int err_code, t_xe *xe)
(void)err_msg;
ft_putstr_fd("minishell: syntax error: ", STDERR_FILENO);
ft_putstr_fd(err_msg[err_code - _PARSING_ERROR_ - 1], STDERR_FILENO);
ft_putchar_fd('\n', STDERR_FILENO);
ft_putendl_fd(err_msg[err_code - _PARSING_ERROR_ - 1], STDERR_FILENO);
xe->stat_loc = 2;
return (SUCCESS);
}
......@@ -40,8 +30,7 @@ int exec_error(int err_code, t_xe *xe)
(void)err_msg;
ft_putstr_fd("minishell: command error: ", STDERR_FILENO);
ft_putstr_fd(err_msg[err_code - _EXEC_ERROR_ - 1], STDERR_FILENO);
ft_putchar_fd('\n', STDERR_FILENO);
ft_putendl_fd(err_msg[err_code - _EXEC_ERROR_ - 1], STDERR_FILENO);
// have to set stat_loc as well !!!
if (err_code == HOME_NOT_SET)
xe->stat_loc = 1;
......@@ -65,7 +54,7 @@ static const char *err_msg(int err_code)
static int err_output(int err_code)
{
putstr_stderr("Error: ");
ft_putstr_fd("Error: ", STDERR_FILENO);
perror(err_msg(err_code - _ERRNO_MSG_));
return (SUCCESS);
}
......@@ -76,7 +65,7 @@ int clean_and_exit(int ret, t_xe *xe)
free_str_array(xe->env); // besoin de free?
free(xe);
if (ret == ARG_ERR)
putstr_stderr("Minishell takes no argument");
ft_putstr_fd("Minishell takes no argument", STDERR_FILENO);
if (ret != CHILD_EXIT)
if (isatty(STDIN_FILENO)) // temp pour le testeur
write(1, "exit\n", 5);
......@@ -94,7 +83,7 @@ int ft_error(int ret, t_xe *xe)
else if (ret > _EXIT_CODE_)
return (clean_and_exit(ret, xe));
else if (ret != SUCCESS) // temp
putstr_stderr("ERROR CODE ERROR (printed for debug)");// temp
ft_putstr_fd("ERROR CODE ERROR (printed for debug)", STDERR_FILENO);// temp
return (SUCCESS);
}
......
#include "minishell.h"
#include "signals.h"
#include "parsing.h"
#include "pre_execution.h"
#include "execution.h"
#include <sys/types.h> // waitpid
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment