summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-04-25 16:40:34 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-04-25 16:40:34 +0200
commite42601eb5acc07417b217dfed4e9ab8cbb91794f (patch)
tree76e7dacd2e8d93012fecf2c29fcb4d14e6d1baed /src
parent(fd)>qwe fd agregation redir works (diff)
download42-minishell-e42601eb5acc07417b217dfed4e9ab8cbb91794f.tar.gz
42-minishell-e42601eb5acc07417b217dfed4e9ab8cbb91794f.tar.bz2
42-minishell-e42601eb5acc07417b217dfed4e9ab8cbb91794f.tar.xz
42-minishell-e42601eb5acc07417b217dfed4e9ab8cbb91794f.tar.zst
42-minishell-e42601eb5acc07417b217dfed4e9ab8cbb91794f.zip
Stupid bash rule
Diffstat (limited to 'src')
-rw-r--r--src/ft_b_env.c2
-rw-r--r--src/ft_b_exit.c2
-rw-r--r--src/ft_b_export.c2
-rw-r--r--src/ft_d_define.h9
-rw-r--r--src/ft_e_externs_next.c4
-rw-r--r--src/ft_f_fail.c27
-rw-r--r--src/ft_f_fail.h13
-rw-r--r--src/ft_f_redir.c3
-rw-r--r--src/ft_m_argv.c40
-rw-r--r--src/ft_m_argv.h24
-rw-r--r--src/ft_m_funptr.c2
-rw-r--r--src/ft_p_line.c3
-rw-r--r--src/ft_s_destroy.c1
-rw-r--r--src/ft_s_init.c10
-rw-r--r--src/ft_s_struct.h1
-rw-r--r--src/minishell.c14
16 files changed, 119 insertions, 38 deletions
diff --git a/src/ft_b_env.c b/src/ft_b_env.c
index c602c13..14d05a2 100644
--- a/src/ft_b_env.c
+++ b/src/ft_b_env.c
@@ -23,7 +23,7 @@ uint8_t
if (args && args[0])
{
- ft_fail_no_options("env");
+ ft_fail_no_options("env", msh);
return (127);
}
ptr = msh->envp;
diff --git a/src/ft_b_exit.c b/src/ft_b_exit.c
index b9cb37b..ae421cf 100644
--- a/src/ft_b_exit.c
+++ b/src/ft_b_exit.c
@@ -28,7 +28,7 @@ uint8_t
if (argc > 1)
{
- ft_fail_too_many_args("exit");
+ ft_fail_too_many_args("exit", msh);
return (1);
}
if (argc == 1)
diff --git a/src/ft_b_export.c b/src/ft_b_export.c
index 767db08..fa2de45 100644
--- a/src/ft_b_export.c
+++ b/src/ft_b_export.c
@@ -66,7 +66,7 @@ uint8_t
next = 0;
if (!ft_check_valid_identifier(*ptr))
{
- ft_fail_identifier("export", *ptr);
+ ft_fail_identifier("export", *ptr, msh);
next = 1;
r = 1;
}
diff --git a/src/ft_d_define.h b/src/ft_d_define.h
index 3a10c58..acb1a29 100644
--- a/src/ft_d_define.h
+++ b/src/ft_d_define.h
@@ -19,16 +19,23 @@
** ====== CLASSICS ======
*/
+#define FT_DEFAULT_SHNAME "minishell"
#define FT_PS_ONE "minishell ~> "
#define FT_BUILTINS "echo|cd|pwd|export|unset|env|exit|type"
#define FT_BUILTINS_COUNT 8
#define FT_HISTFILE "minishell_history"
/*
+** ====== OPTIONS ======
+*/
+
+#define FT_INTERACT "-i"
+#define FT_COMMAND "-c"
+
+/*
** ====== FAIL MSG ======
*/
-#define FT_FAIL_ALLOC "failed to allocate memory"
#define FT_FAIL_NO_OPTIONS "no options required"
#define FT_FAIL_TOO_MANY_ARGS "too many arguments"
diff --git a/src/ft_e_externs_next.c b/src/ft_e_externs_next.c
index bf1bb38..ab5f150 100644
--- a/src/ft_e_externs_next.c
+++ b/src/ft_e_externs_next.c
@@ -33,7 +33,7 @@ static char
{
ft_lcom_clear(&msh->curr);
ft_s_destroy(msh);
- ft_fail_alloc();
+ ft_fail_alloc(msh);
}
ft_memcpy(fullpath, p_path, path_len);
*(fullpath + (path_len)) = '/';
@@ -98,7 +98,7 @@ char
{
ft_lcom_clear(&msh->curr);
ft_s_destroy(msh);
- ft_fail_alloc();
+ ft_fail_alloc(msh);
}
return (envpath);
}
diff --git a/src/ft_f_fail.c b/src/ft_f_fail.c
index b43281d..3f43bae 100644
--- a/src/ft_f_fail.c
+++ b/src/ft_f_fail.c
@@ -17,37 +17,42 @@
#include <errno.h>
#include "ft_d_define.h"
+#include "ft_s_struct.h"
static void
ft_write_fail(const char concern[],
- const char msg[])
+ const char msg[],
+ t_msh *msh)
{
- ft_dprintf(STDERR_FILENO, "minishell: %s: %s\n", concern, msg);
+ ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", msh->shname, concern, msg);
}
void
- ft_fail_no_options(const char concern[])
+ ft_fail_no_options(const char concern[],
+ t_msh *msh)
{
- ft_write_fail(concern, FT_FAIL_NO_OPTIONS);
+ ft_write_fail(concern, FT_FAIL_NO_OPTIONS, msh);
}
void
ft_fail_identifier(const char concern[],
- const char identifier[])
+ const char identifier[],
+ t_msh *msh)
{
- ft_dprintf(STDERR_FILENO,
- "minishell: %s: `%s': not a valid identifier\n", concern, identifier);
+ ft_dprintf(STDERR_FILENO, "%s: %s: `%s': not a valid identifier\n",
+ msh->shname, concern, identifier);
}
void
- ft_fail_too_many_args(const char concern[])
+ ft_fail_too_many_args(const char concern[],
+ t_msh *msh)
{
- ft_write_fail(concern, FT_FAIL_TOO_MANY_ARGS);
+ ft_write_fail(concern, FT_FAIL_TOO_MANY_ARGS, msh);
}
void
- ft_fail_alloc(void)
+ ft_fail_alloc(t_msh *msh)
{
- ft_write_fail("minishell", strerror(errno));
+ ft_dprintf(STDERR_FILENO, "%s: %s\n", msh->shname, strerror(errno));
exit(FT_RET_ALLOC);
}
diff --git a/src/ft_f_fail.h b/src/ft_f_fail.h
index 60f989d..5091810 100644
--- a/src/ft_f_fail.h
+++ b/src/ft_f_fail.h
@@ -13,10 +13,15 @@
#ifndef FT_F_FAIL_H
#define FT_F_FAIL_H
-void ft_fail_no_options(const char concern[]);
-void ft_fail_too_many_args(const char concern[]);
+#include "ft_s_struct.h"
+
+void ft_fail_no_options(const char concern[],
+ t_msh *msh);
+void ft_fail_too_many_args(const char concern[],
+ t_msh *msh);
void ft_fail_identifier(const char concern[],
- const char identifier[]);
-void ft_fail_alloc(void);
+ const char identifier[],
+ t_msh *msh);
+void ft_fail_alloc(t_msh *msh);
#endif
diff --git a/src/ft_f_redir.c b/src/ft_f_redir.c
index 997401a..2cd7d58 100644
--- a/src/ft_f_redir.c
+++ b/src/ft_f_redir.c
@@ -23,7 +23,8 @@ void
ft_f_redir(const char path[],
t_msh *msh)
{
- ft_dprintf(STDERR_FILENO, "minishell: %s: %s\n", path, strerror(errno));
+ ft_dprintf(STDERR_FILENO, "%s: %s: %s\n",
+ msh->shname, path, strerror(errno));
ft_lcom_clear(&msh->curr);
ft_s_destroy(msh);
exit(1);
diff --git a/src/ft_m_argv.c b/src/ft_m_argv.c
new file mode 100644
index 0000000..a4da580
--- /dev/null
+++ b/src/ft_m_argv.c
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_m_argv.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 <stdint.h>
+
+#include "ft_d_define.h"
+#include "ft_m_loop.h"
+#include "ft_s_struct.h"
+
+uint8_t
+ ft_m_argv(int argc,
+ const char *argv[],
+ t_msh *msh)
+{
+ int32_t i;
+
+ (void)argv;
+ if (argc == 1)
+ {
+ msh->ret = ft_m_loop(msh);
+ return (msh->ret);
+ }
+ i = 1;
+ while (i < argc)
+ {
+ /* if (ft_strncmp(*(argv + i), )) */
+ i++;
+ }
+ return (msh->ret);
+}
diff --git a/src/ft_m_argv.h b/src/ft_m_argv.h
new file mode 100644
index 0000000..2f3260d
--- /dev/null
+++ b/src/ft_m_argv.h
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_m_argv.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 FT_M_ARGV_H
+#define FT_M_ARGV_H
+
+#include <stdint.h>
+
+#include "ft_s_struct.h"
+
+uint8_t ft_m_argv(int argc,
+ const char *argv[],
+ t_msh *msh);
+
+#endif
diff --git a/src/ft_m_funptr.c b/src/ft_m_funptr.c
index 0a48d52..305dfc9 100644
--- a/src/ft_m_funptr.c
+++ b/src/ft_m_funptr.c
@@ -32,6 +32,6 @@ void
msh->bu_ptr[7] = ft_b_type;
if (!(msh->bu_ref = ft_split(FT_BUILTINS, '|')))
{
- ft_fail_alloc();
+ ft_fail_alloc(msh);
}
}
diff --git a/src/ft_p_line.c b/src/ft_p_line.c
index df31973..81e02d2 100644
--- a/src/ft_p_line.c
+++ b/src/ft_p_line.c
@@ -12,6 +12,7 @@
#include <libft.h>
#include <stdlib.h>
+
#include "ft_d_enum.h"
#include "ft_f_fail.h"
#include "ft_p_lcom.h"
@@ -37,6 +38,6 @@ void
}
if (ft_p_lcom(line, count, msh) < 0)
{
- ft_fail_alloc();
+ ft_fail_alloc(msh);
}
}
diff --git a/src/ft_s_destroy.c b/src/ft_s_destroy.c
index 7f495c4..f339e8d 100644
--- a/src/ft_s_destroy.c
+++ b/src/ft_s_destroy.c
@@ -18,6 +18,7 @@ void
{
ft_memdel((void*)&msh->ps_one);
ft_memdel((void*)&msh->cwd);
+ ft_memdel((void*)&msh->shname);
ft_delwords(msh->bu_ref);
ft_memdel((void*)&msh);
}
diff --git a/src/ft_s_init.c b/src/ft_s_init.c
index fb3ba6a..5de17c1 100644
--- a/src/ft_s_init.c
+++ b/src/ft_s_init.c
@@ -24,15 +24,11 @@ t_msh
t_msh *msh;
if (!(msh = (t_msh*)malloc(sizeof(t_msh))))
- {
return (NULL);
- }
- if (!(msh->ps_one =
- (char*)malloc((ft_strlen(FT_PS_ONE) + 1) * sizeof(char))))
- {
+ if (!(msh->ps_one = ft_strdup(FT_PS_ONE)))
+ return (NULL);
+ if (!(msh->shname = ft_strdup(FT_DEFAULT_SHNAME)))
return (NULL);
- }
- ft_strlcpy(msh->ps_one, FT_PS_ONE, ft_strlen(FT_PS_ONE) + 1);
msh->cwd = NULL;
msh->cwd = getcwd(NULL, 0);
/* TODO: handle getcwd failed */
diff --git a/src/ft_s_struct.h b/src/ft_s_struct.h
index 06674cc..38eeb43 100644
--- a/src/ft_s_struct.h
+++ b/src/ft_s_struct.h
@@ -42,6 +42,7 @@ typedef struct s_msh
char *ps_one;
char *cwd;
uint8_t ret;
+ char *shname;
char **bu_ref;
uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*);
struct s_lcom *curr;
diff --git a/src/minishell.c b/src/minishell.c
index 1b5f4da..1df809b 100644
--- a/src/minishell.c
+++ b/src/minishell.c
@@ -12,9 +12,11 @@
#include <libft.h>
#include <stdint.h>
+#include <string.h>
+#include <errno.h>
#include "ft_f_fail.h"
-#include "ft_m_loop.h"
+#include "ft_m_argv.h"
#include "ft_s_struct.h"
#include "ft_s_init.h"
#include "ft_s_destroy.h"
@@ -27,15 +29,13 @@ int
t_msh *msh;
int32_t ret;
- (void)argc;
- (void)argv;
- /* TODO: deal with args */
+ /* TODO: increment $SHLVL */
if (!(msh = ft_init_msh(envp)))
{
- ft_fail_alloc();
+ ft_dprintf(2, "%s\n", strerror(errno));
+ return (FT_RET_ALLOC);
}
- msh->ret = ft_m_loop(msh);
- ret = msh->ret;
+ ret = ft_m_argv(argc, argv, msh);
ft_s_destroy(msh);
return (ret);
}