summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-08-15 18:00:23 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-08-15 18:00:23 +0200
commit56ccad6b85a5f3f17eebaf4ba511e07ba760b050 (patch)
treeb7073ccf7712baf52ee5c7792130029495249d3f
parentNow deletes comments (diff)
download42-minishell-56ccad6b85a5f3f17eebaf4ba511e07ba760b050.tar.gz
42-minishell-56ccad6b85a5f3f17eebaf4ba511e07ba760b050.tar.bz2
42-minishell-56ccad6b85a5f3f17eebaf4ba511e07ba760b050.tar.xz
42-minishell-56ccad6b85a5f3f17eebaf4ba511e07ba760b050.tar.zst
42-minishell-56ccad6b85a5f3f17eebaf4ba511e07ba760b050.zip
Can read scripts, but need to handle fails
-rw-r--r--src/b_exit.c1
-rw-r--r--src/m_argv.c21
-rw-r--r--src/m_loop.c7
-rw-r--r--src/m_loop.h2
-rw-r--r--src/m_minishell.c4
-rw-r--r--src/p_line.c21
6 files changed, 42 insertions, 14 deletions
diff --git a/src/b_exit.c b/src/b_exit.c
index 19e55d9..cb8988f 100644
--- a/src/b_exit.c
+++ b/src/b_exit.c
@@ -40,6 +40,5 @@ uint8_t
}
else
ret = msh->ret;
- ft_dprintf(STDERR_FILENO, "exit\n");
return (ret);
}
diff --git a/src/m_argv.c b/src/m_argv.c
index 6a79bd7..1e23bae 100644
--- a/src/m_argv.c
+++ b/src/m_argv.c
@@ -12,6 +12,8 @@
#include <libft.h>
#include <stdint.h>
+#include <fcntl.h>
+#include <unistd.h>
#include "d_define.h"
#include "m_comm.h"
@@ -20,16 +22,16 @@
uint8_t
m_argv(int argc,
- char *const argv[],
- t_msh *msh)
+ char *const argv[],
+ t_msh *msh)
{
- /* TODO: better argv handling */
+ int32_t fd;
+
if (argc == 1)
{
- msh->ret = m_loop(msh);
- return (msh->ret);
+ msh->ret = m_loop(STDIN_FILENO, msh);
}
- if (!ft_strncmp(*(argv + 1), FT_OPT_COMMAND, 3))
+ else if (argc > 1 && ft_strncmp(*(argv + 1), FT_OPT_COMMAND, 3) == 0)
{
if (*(argv + 2) == NULL)
{
@@ -39,5 +41,12 @@ uint8_t
}
msh->ret = m_comm(*(argv + 2), msh);
}
+ else
+ {
+ if ((fd = open(*(argv + 1), O_RDONLY)) < 0)
+ f_open_file(msh);
+ msh->ret = m_loop(fd, msh);
+ close(fd);
+ }
return (msh->ret);
}
diff --git a/src/m_loop.c b/src/m_loop.c
index 140d009..84efddf 100644
--- a/src/m_loop.c
+++ b/src/m_loop.c
@@ -82,7 +82,7 @@ void
}
uint8_t
- m_loop(t_msh *msh)
+ m_loop(int32_t fd, t_msh *msh)
{
char *line;
char *quote;
@@ -91,8 +91,9 @@ uint8_t
gnl = 1;
while (gnl > 0)
{
- m_prompt(msh);
- gnl = get_next_line(STDIN_FILENO, &line);
+ if (fd == STDIN_FILENO)
+ m_prompt(msh);
+ gnl = get_next_line(fd, &line);
if (line[0] != '\0')
{
if (!(quote = ft_strchr(line, '\'')) && !(quote = ft_strchr(line, '\"')))
diff --git a/src/m_loop.h b/src/m_loop.h
index 1d665e3..3f95a5e 100644
--- a/src/m_loop.h
+++ b/src/m_loop.h
@@ -17,6 +17,6 @@
#include "s_struct.h"
-uint8_t m_loop(t_msh *msh);
+uint8_t m_loop(int32_t fd, t_msh *msh);
#endif
diff --git a/src/m_minishell.c b/src/m_minishell.c
index e88207b..a538041 100644
--- a/src/m_minishell.c
+++ b/src/m_minishell.c
@@ -30,13 +30,11 @@ int
int32_t ret;
t_msh *msh;
- /* TODO: handle general variables | $var */
- if (!(msh = init_msh(argv, envp)))
+ if ((msh = init_msh(argv, envp)) == NULL)
{
ft_dprintf(2, "%s\n", strerror(errno));
return (FT_RET_ALLOC);
}
- /* TODO: delet this */
ret = m_argv(argc, argv, msh);
s_destroy(msh);
return (ret);
diff --git a/src/p_line.c b/src/p_line.c
index af1ba64..8b13424 100644
--- a/src/p_line.c
+++ b/src/p_line.c
@@ -37,11 +37,32 @@ static void
}
}
+static t_bool
+ p_check_whitespaces_only(char line[])
+{
+ char *ptr;
+
+ ptr = line;
+ while (*ptr != '\0')
+ {
+ if (*ptr != ' ' && *ptr != '\t')
+ {
+ return (FALSE);
+ }
+ ptr++;
+ }
+ return (TRUE);
+}
+
void
p_line(char line[],
t_msh *msh)
{
p_delete_comments(line);
+ if (p_check_whitespaces_only(line) == TRUE)
+ {
+ return ;
+ }
if (p_lcom(line, msh) < 0)
{
f_alloc_and_destroy_msh(msh);