From c19bd35afdb45e49cebdfd96e7adb1e6fe477f0c Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Thu, 30 Jul 2020 20:30:58 +0200
Subject: Double exit no more

---
 src/b_cd.c            |  8 +++----
 src/b_env.c           |  2 +-
 src/b_exit.c          |  7 ++++--
 src/b_export.c        |  2 +-
 src/b_type.c          |  2 +-
 src/d_define.h        |  5 +++--
 src/e_builtins.c      | 61 ---------------------------------------------------
 src/e_externs.c       | 14 +++++++++---
 src/e_externs_next.c  |  4 ++--
 src/e_externs_pipes.c |  4 ++--
 src/f_chdir.c         |  2 +-
 src/f_chdir.h         |  2 +-
 src/f_com.c           | 23 +++++++++++++++++++
 src/f_com.h           | 20 +++++++++++++++++
 src/f_fail.c          |  8 +++----
 src/f_fail.h          |  9 ++++----
 src/m_funptr.c        |  2 +-
 src/m_loop.c          |  1 -
 src/p_line.c          |  2 +-
 src/u_vars.c          | 10 ++++-----
 20 files changed, 91 insertions(+), 97 deletions(-)
 create mode 100644 src/f_com.c
 create mode 100644 src/f_com.h

(limited to 'src')

diff --git a/src/b_cd.c b/src/b_cd.c
index c892f7e..f30e61f 100644
--- a/src/b_cd.c
+++ b/src/b_cd.c
@@ -28,7 +28,7 @@ static void
 	if (!(*path = ft_strdup(*args)))
 	{
 		s_destroy(msh);
-		fail_alloc(msh);
+		f_fail_alloc(msh);
 	}
 	if (!ft_strncmp("~/", *path, 2) || !ft_strncmp("~", *path, 2))
 	{
@@ -36,7 +36,7 @@ static void
 			"~", u_get_var_value("$HOME", msh))))
 		{
 			s_destroy(msh);
-			fail_alloc(msh);
+			f_fail_alloc(msh);
 		}
 	}
 }
@@ -50,7 +50,7 @@ uint8_t
 
 	if (argc >= 2)
 	{
-		fail_too_many_args("cd", msh);
+		f_fail_too_many_args("cd", msh);
 		return (1);
 	}
 	else if (argc == 0)
@@ -62,7 +62,7 @@ uint8_t
 		set_path(&path, args, msh);
 	if (chdir(path) != 0)
 	{
-		fail_chd("cd", path, msh);
+		f_fail_chd("cd", path, msh);
 		ft_memdel((void*)&path);
 		return (1);
 	}
diff --git a/src/b_env.c b/src/b_env.c
index 08adaf5..8f0a974 100644
--- a/src/b_env.c
+++ b/src/b_env.c
@@ -24,7 +24,7 @@ uint8_t
 
 	if (args && args[0])
 	{
-		fail_no_options("env", msh);
+		f_fail_no_options("env", msh);
 		return (127);
 	}
 	ptr = msh->envp;
diff --git a/src/b_exit.c b/src/b_exit.c
index 777bbb9..5f1c446 100644
--- a/src/b_exit.c
+++ b/src/b_exit.c
@@ -28,10 +28,9 @@ uint8_t
 	uint8_t			ret;
 	const uint64_t	argc = get_argc((const char**)args);
 
-	ret = 0;
 	if (argc > 1)
 	{
-		fail_too_many_args("exit", msh);
+		f_fail_too_many_args("exit", msh);
 		return (1);
 	}
 	if (argc == 1)
@@ -39,7 +38,11 @@ uint8_t
 		ret = ft_atoi(args[0]);
 		 /* TODO: non numeric args[0] */
 	}
+	else
+		ret = msh->ret;
 	ft_dprintf(STDERR_FILENO, "exit\n");
+	lcom_clear(&msh->curr);
+	s_destroy(msh);
 	exit(ret);
 	return (0);
 }
diff --git a/src/b_export.c b/src/b_export.c
index 95ccf40..9cb1c14 100644
--- a/src/b_export.c
+++ b/src/b_export.c
@@ -68,7 +68,7 @@ uint8_t
 		next = 0;
 		if (!check_valid_identifier(*ptr))
 		{
-			fail_identifier("export", *ptr, msh);
+			f_fail_identifier("export", *ptr, msh);
 			next = 1;
 			r = 1;
 		}
diff --git a/src/b_type.c b/src/b_type.c
index ccaca43..00bad1a 100644
--- a/src/b_type.c
+++ b/src/b_type.c
@@ -57,7 +57,7 @@ static char
 			if (!(fullpath = ft_strdup(com)))
 			{
 				lcom_clear(&msh->curr);
-				fail_alloc(msh);
+				f_fail_alloc(msh);
 			}
 			return (fullpath);
 		}
diff --git a/src/d_define.h b/src/d_define.h
index 6f93751..b2f2f08 100644
--- a/src/d_define.h
+++ b/src/d_define.h
@@ -42,7 +42,8 @@
 ** ====== FAIL MSG ======
 */
 
-#define FT_FAIL_NO_OPTIONS		"no options required"
-#define FT_FAIL_TOO_MANY_ARGS	"too many arguments"
+#define FT_FAIL_COMMAND_NOT_FOUND	"command not found"
+#define FT_FAIL_NO_OPTIONS			"no options required"
+#define FT_FAIL_TOO_MANY_ARGS		"too many arguments"
 
 #endif
diff --git a/src/e_builtins.c b/src/e_builtins.c
index ca512a3..fe4f7e1 100644
--- a/src/e_builtins.c
+++ b/src/e_builtins.c
@@ -21,51 +21,6 @@
 #include "s_lcom.h"
 #include "s_struct.h"
 
-/* static void */
-/* 	e_builtin_child(const t_lcom *ptr, */
-/* 					uint8_t bu_id, */
-/* 					t_msh *msh) */
-/* { */
-/* 	int32_t	ret; */
-
-/* 	dup_redirs(ptr, msh); */
-/* 	ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); */
-/* 	lcom_clear(&msh->curr); */
-/* 	s_destroy(msh); */
-/* 	exit(ret); */
-/* } */
-
-/* static void */
-/* 	e_builtin_parent(pid_t pid, */
-/* 						const t_lcom *ptr, */
-/* 						uint8_t bu_id, */
-/* 						t_msh *msh) */
-/* { */
-/* 	int32_t	status; */
-/* 	int32_t	ret; */
-
-/* 	while (wait(&status) != pid) */
-/* 		; */
-/* 	ret = WEXITSTATUS(status); */
-/* 	if (bu_id != FT_ID_EXIT) */
-/* 		msh->ret = ret; */
-/* 	if (bu_id == FT_ID_CD && msh->ret == 0) */
-/* 	{ */
-/* 		msh->bu_ptr[bu_id](ptr->argv + 1, msh); */
-/* 		/\* TODO: export $PWD *\/ */
-/* 	} */
-/* 	else if (bu_id == FT_ID_EXIT && ret == 0) */
-/* 	{ */
-/* 		if (ptr->argv[1]) */
-/* 			ret = ft_atoi(ptr->argv[1]); */
-/* 		else */
-/* 			ret = msh->ret; */
-/* 		lcom_clear(&msh->curr); */
-/* 		s_destroy(msh); */
-/* 		exit(ret); */
-/* 	} */
-/* } */
-
 void
 	e_builtin(const t_lcom *ptr,
 				uint8_t bu_id,
@@ -77,20 +32,4 @@ void
 	ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh);
 	lcom_clear(&msh->curr);
 	msh->ret = ret;
-	/* pid_t	pid; */
-
-	/* /\* TODO: find a way to handle exit | bu_id = 6 *\/ */
-	/* e_builtin_child(ptr, bu_id, msh); */
-	/* if ((pid = fork()) == 0) */
-	/* { */
-	/* 	e_builtin_child(ptr, bu_id, msh); */
-	/* } */
-	/* else if (pid < 0) */
-	/* { */
-	/* 	/\* TODO: handle fork failed *\/ */
-	/* } */
-	/* else */
-	/* { */
-	/* 	e_builtin_parent(pid, ptr, bu_id, msh); */
-	/* } */
 }
diff --git a/src/e_externs.c b/src/e_externs.c
index d7c4854..3d55fa9 100644
--- a/src/e_externs.c
+++ b/src/e_externs.c
@@ -17,6 +17,7 @@
 #include <unistd.h>
 
 #include "e_externs_next.h"
+#include "f_fail.h"
 #include "m_redirs.h"
 #include "s_destroy.h"
 #include "s_lcom.h"
@@ -72,8 +73,15 @@ void
 	{
 		fullpath = search_in_path(ptr->com, envpath, msh);
 		ft_delwords(envpath);
+		if (fullpath == NULL)
+		{
+			f_fail_command_not_found(ptr->com);
+			/* TODO: deal if not found etc */
+		}
+		else
+		{
+			exec_path(fullpath, ptr, msh);
+			ft_memdel((void*)&fullpath);
+		}
 	}
-	/* TODO: deal if not found etc */
-	exec_path(fullpath, ptr, msh);
-	ft_memdel((void*)&fullpath);
 }
diff --git a/src/e_externs_next.c b/src/e_externs_next.c
index ba3c2d5..e33405c 100644
--- a/src/e_externs_next.c
+++ b/src/e_externs_next.c
@@ -33,7 +33,7 @@ static char
 	{
 		lcom_clear(&msh->curr);
 		s_destroy(msh);
-		fail_alloc(msh);
+		f_fail_alloc(msh);
 	}
 	(void)ft_memcpy(fullpath, p_path, path_len);
 	*(fullpath + (path_len)) = '/';
@@ -97,7 +97,7 @@ char
 		{
 			lcom_clear(&msh->curr);
 			s_destroy(msh);
-			fail_alloc(msh);
+			f_fail_alloc(msh);
 		}
 		return (envpath);
 	}
diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c
index c8f8404..a5ee696 100644
--- a/src/e_externs_pipes.c
+++ b/src/e_externs_pipes.c
@@ -122,7 +122,7 @@ void
 	rptr = ptr;
 	pipes = e_get_pipes_count(head);
 	if (!(fullpath = (char **)malloc((pipes + 2) * sizeof(char *))))
-		fail_alloc(msh);
+		f_fail_alloc(msh);
 	fullpath[pipes + 1] = NULL;
 	i = 0;
 	while (rptr != NULL)
@@ -130,7 +130,7 @@ void
 		if (ft_ischarset("/.", rptr->one->com[0]))
 		{
 			if (!(fullpath[i] = ft_strdup(rptr->one->com)))
-				fail_alloc(msh);
+				f_fail_alloc(msh);
 		}
 		else if ((envpath = get_env_path(msh)) != NULL)
 		{
diff --git a/src/f_chdir.c b/src/f_chdir.c
index 6bb0497..4623c82 100644
--- a/src/f_chdir.c
+++ b/src/f_chdir.c
@@ -18,7 +18,7 @@
 #include "s_struct.h"
 
 void
-	fail_chd(const char concern[],
+	f_fail_chd(const char concern[],
 				const char path[],
 				t_msh *msh)
 {
diff --git a/src/f_chdir.h b/src/f_chdir.h
index be9773b..76cb737 100644
--- a/src/f_chdir.h
+++ b/src/f_chdir.h
@@ -15,7 +15,7 @@
 
 #include "s_struct.h"
 
-void	fail_chd(const char concern[],
+void	f_fail_chd(const char concern[],
 					const char pathp[],
 					t_msh *msh);
 
diff --git a/src/f_com.c b/src/f_com.c
new file mode 100644
index 0000000..9c4e02a
--- /dev/null
+++ b/src/f_com.c
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   f_com.c                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
+/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <unistd.h>
+
+#include "d_define.h"
+#include "s_struct.h"
+
+void
+	f_fail_command_not_found(const char command[])
+{
+	ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", "minishell", command, FT_FAIL_COMMAND_NOT_FOUND);
+}
diff --git a/src/f_com.h b/src/f_com.h
new file mode 100644
index 0000000..12023f6
--- /dev/null
+++ b/src/f_com.h
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   f_com.h                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
+/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef F_COM_H
+#define F_COM_H
+
+#include "s_struct.h"
+
+void	f_fail_command_not_found(const char command[]);
+
+#endif
diff --git a/src/f_fail.c b/src/f_fail.c
index aae00fb..a3a1df1 100644
--- a/src/f_fail.c
+++ b/src/f_fail.c
@@ -28,14 +28,14 @@ static void
 }
 
 void
-	fail_no_options(const char concern[],
+	f_fail_no_options(const char concern[],
 					t_msh *msh)
 {
 	write_fail(concern, FT_FAIL_NO_OPTIONS, msh);
 }
 
 void
-	fail_identifier(const char concern[],
+	f_fail_identifier(const char concern[],
 					const char identifier[],
 					t_msh *msh)
 {
@@ -44,14 +44,14 @@ void
 }
 
 void
-	fail_too_many_args(const char concern[],
+	f_fail_too_many_args(const char concern[],
 						t_msh *msh)
 {
 	write_fail(concern, FT_FAIL_TOO_MANY_ARGS, msh);
 }
 
 void
-	fail_alloc(t_msh *msh)
+	f_fail_alloc(t_msh *msh)
 {
 	ft_dprintf(STDERR_FILENO, "%s: %s\n", msh->shname, strerror(errno));
 	exit(FT_RET_ALLOC);
diff --git a/src/f_fail.h b/src/f_fail.h
index 9699eee..868fe75 100644
--- a/src/f_fail.h
+++ b/src/f_fail.h
@@ -13,18 +13,19 @@
 #ifndef F_FAIL_H
 #define F_FAIL_H
 
+#include "f_com.h"
 #include "f_chdir.h"
 #include "f_errno.h"
 #include "f_redir.h"
 #include "s_struct.h"
 
-void	fail_no_options(const char concern[],
+void	f_fail_no_options(const char concern[],
 						t_msh *msh);
-void	fail_too_many_args(const char concern[],
+void	f_fail_too_many_args(const char concern[],
 							t_msh *msh);
-void	fail_identifier(const char concern[],
+void	f_fail_identifier(const char concern[],
 						const char identifier[],
 						t_msh *msh);
-void	fail_alloc(t_msh *msh);
+void	f_fail_alloc(t_msh *msh);
 
 #endif
diff --git a/src/m_funptr.c b/src/m_funptr.c
index 5a02b2e..3bd0969 100644
--- a/src/m_funptr.c
+++ b/src/m_funptr.c
@@ -32,6 +32,6 @@ void
 	msh->bu_ptr[7] = b_type;
 	if (!(msh->bu_ref = ft_split(FT_BUILTINS, '|')))
 	{
-		fail_alloc(msh);
+		f_fail_alloc(msh);
 	}
 }
diff --git a/src/m_loop.c b/src/m_loop.c
index fe3fbcd..143c25d 100644
--- a/src/m_loop.c
+++ b/src/m_loop.c
@@ -114,7 +114,6 @@ uint8_t
 			/* TODO: (null): Bad address on "msh ~> echo a > asd; cat < asd" but not on "msh ~> echo a > asd; cat asd" */
 			/* TODO: GNL 25 leak on "msh ~> exit" | gl hf */
 			/* TODO: "msh ~> some command \": re GNL into ft_nrealloc */
-			/* TODO: the chad pipes | */
 			/* TODO: a histfile would be nice */
 		}
 		else
diff --git a/src/p_line.c b/src/p_line.c
index 4a58537..aafc900 100644
--- a/src/p_line.c
+++ b/src/p_line.c
@@ -45,6 +45,6 @@ void
 	if (p_lcom(line, count, msh) < 0)
 	{
 		s_destroy(msh);
-		fail_alloc(msh);
+		f_fail_alloc(msh);
 	}
 }
diff --git a/src/u_vars.c b/src/u_vars.c
index 377da77..a852d6f 100644
--- a/src/u_vars.c
+++ b/src/u_vars.c
@@ -30,7 +30,7 @@ static char
 	{
 		lcom_clear(&msh->curr);
 		s_destroy(msh);
-		fail_alloc(msh);
+		f_fail_alloc(msh);
 	}
 	ft_memcpy((char*)rvarname, (const char*)varname + 1,
 		ft_strlen(varname + 1));
@@ -51,7 +51,7 @@ static char
 		ft_memdel((void*)&rvarname);
 		lcom_clear(&msh->curr);
 		s_destroy(msh);
-		fail_alloc(msh);
+		f_fail_alloc(msh);
 	}
 	ft_memdel((void*)&rvarname);
 	return (varval);
@@ -95,7 +95,7 @@ static char
 		{
 			lcom_clear(&msh->curr);
 			s_destroy(msh);
-			fail_alloc(msh);
+			f_fail_alloc(msh);
 		}
 		return (varval);
 	}
@@ -105,7 +105,7 @@ static char
 		{
 			lcom_clear(&msh->curr);
 			s_destroy(msh);
-			fail_alloc(msh);
+			f_fail_alloc(msh);
 		}
 		return (varval);
 	}
@@ -131,7 +131,7 @@ static char
 		{
 			lcom_clear(&msh->curr);
 			s_destroy(msh);
-			fail_alloc(msh);
+			f_fail_alloc(msh);
 		}
 		return (varval);
 	}
-- 
cgit v1.2.3