aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-02-28 16:00:02 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-28 16:00:02 +0100
commit5dc7a4036025bd7c3f997f08e2d9b14338c0e528 (patch)
treedb45975de56f58a64bf6370d5a441b9299c6899d
parentcc1b57c96cc8c0fdd53e781b54a83bb9c743179a (diff)
downloadminishell-5dc7a4036025bd7c3f997f08e2d9b14338c0e528.tar.gz
minishell-5dc7a4036025bd7c3f997f08e2d9b14338c0e528.tar.bz2
minishell-5dc7a4036025bd7c3f997f08e2d9b14338c0e528.zip
Added eval.c draft
-rw-r--r--include/minishell.h10
-rw-r--r--include/ms_parse.h8
-rw-r--r--src/eval.c134
3 files changed, 146 insertions, 6 deletions
diff --git a/include/minishell.h b/include/minishell.h
index 5e27ffa..97293c2 100644
--- a/include/minishell.h
+++ b/include/minishell.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */
-/* Updated: 2020/02/28 12:34:21 by cacharle ### ########.fr */
+/* Updated: 2020/02/28 15:30:10 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,6 +30,8 @@
# include "ms_parse.h"
# define MS_PATH_KEY "PATH"
+# define MS_PIPE_WRITE 1
+# define MS_PIPE_READ 0
typedef struct
{
@@ -43,6 +45,12 @@ typedef struct
t_ftht *environment;
} t_state;
+typedef struct
+{
+ int pipe_in[2];
+ int pipe_out[2];
+} t_command_frame;
+
/*
** state.c
*/
diff --git a/include/ms_parse.h b/include/ms_parse.h
index 043f065..59c9ca8 100644
--- a/include/ms_parse.h
+++ b/include/ms_parse.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 09:00:00 by cacharle #+# #+# */
-/* Updated: 2020/02/28 12:28:59 by cacharle ### ########.fr */
+/* Updated: 2020/02/28 14:57:58 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -32,15 +32,15 @@ typedef struct
{
char *name;
char **argv;
- t_ftlst *redirections;
+ t_ftlst *redirections; // need to store them in reverse order
} t_command;
typedef enum
{
SEPARATOR_SEMICOLON,
SEPARATOR_PIPE,
- SEPARATOR_AND,
- SEPARATOR_OR,
+ // SEPARATOR_AND, // with parhenthesis => harder
+ // SEPARATOR_OR,
} t_separator;
typedef struct
diff --git a/src/eval.c b/src/eval.c
index 2956662..2b6cab6 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -1,8 +1,140 @@
#include "minishell.h"
+static int st_frame_init(t_command_frame *frame)
+{
+ /* if (pipe(frame->pipe_in) == -1) */
+ /* return (-1); */
+ /* if (pipe(frame->pipe_out) == -1) */
+ /* return (-1); */
+ frame->pipe_in[MS_PIPE_READ] = -1;
+ frame->pipe_in[MS_PIPE_WRITE] = -1;
+ frame->pipe_out[MS_PIPE_READ] = -1;
+ frame->pipe_out[MS_PIPE_WRITE] = -1;
+ return (0);
+}
+
+/*
+** Run child process with executable located by `path`, his arguments in `argv`
+** and the current environment variables in `envp`.
+** Returns the child process pid or -1 if an error occured when forking.
+*/
+
+static pid_t st_run_process(char *path, char **argv, char **envp, t_command_frame *frame)
+{
+ int i;
+ pid_t child_pid;
+
+ if (path == NULL || argv == NULL || envp == NULL || frame == NULL)
+ return (-1);
+ if ((child_pid = fork()) == -1)
+ return (-1);
+ if (child_pid == 0)
+ {
+ if (frame->pipe_in[MS_PIPE_READ] != -1)
+ dup2(STDIN_FILENO, frame->pipe_in[MS_PIPE_WRITE]);
+ if (frame->pipe_out[MS_PIPE_WRITE] != -1)
+ dup2(STDOUT_FILENO, frame->pipe_out[MS_PIPE_READ]);
+ if ((execve(path, argv, envp)) == -1)
+ exit(EXIT_FAILURE);
+ }
+ else
+ return (child_pid);
+}
+
+/*
+** Run a command.
+*/
+
+static int st_command_run(t_command *command, t_command_frame *frame)
+{
+ t_redirection *tmp;
+
+ if ((tmp = st_has_redirection_in(command)) != NULL)
+ {
+ if (pipe(frame->pipe_in) == -1)
+ return (-1);
+ if ((frame->pipe_in[MS_PIPE_READ] = open(tmp->filename, O_RDONLY)) < 0)
+ return (-1);
+ }
+ if ((tmp = st_has_redirection_out(command)) != NULL)
+ {
+ if (pipe(frame->pipe_out) == -1)
+ return (-1);
+ if ((frame->pipe_out[MS_PIPE_WRITE] = open(tmp->filename, O_WRONLY | O_CREAT)) < 0)
+ return (-1);
+ }
+ if ((child_pid = st_run_process(path, argv, envp, &frame)) == -1)
+ return (-1);
+ wait(&child_pid);
+ return (0);
+}
+
+/*
+** Run a standalone command.
+*/
+
+static int st_command_standalone_run(t_command *command)
+{
+ t_command_frame frame;
+
+ if (st_frame_init(&frame) == -1)
+ return (-1);
+ return (st_command_run(command, &frame));
+}
+
+/*
+** Run a piped command
+*/
+
+// in main loop, if previous was piped, init frame with previous pipe
+static int st_command_piped_run(t_command *command, int pipe_out[2])
+{
+ t_command_frame frame;
+
+ if (st_frame_init(&frame) == -1)
+ return (-1);
+ frame.pipe_out[MS_PIPE_WRITE] =
+
+}
+
+
+/*
+** Evaluate the commands in parsing according by what they are separated by.
+*/
+
int ms_eval(t_parsing *parsing)
{
- (void)parsing;
+ pid_t child_pid;
+ t_command_frame frame;
+
+ if (parsing == NULL)
+ return (-1);
+ if (ft_lstsize(parsing->commands) != ft_lstsize(parsing->separators) - 1)
+ return (-1);
+ while (parsing->separators != NULL)
+ {
+ if ((t_separator)parsing->separators->content == SEPARATOR_SEMICOLON)
+ {
+ }
+ else if ((t_separator)parsing->separators->content == SEPARATOR_PIPE)
+ {
+
+ // if has out redirection
+ //
+
+ // create pipe
+ // fork fst
+ // redirect stdout to pipe in
+ // execve
+
+ // fork snd
+ // redirect std to pipe out
+ // execve
+
+ }
+ ft_lstpop_front(&parsing->commands, free);
+ ft_lstpop_front(&parsing->separators, NULL);
+ }
return (0);
}