aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
parentcc1b57c96cc8c0fdd53e781b54a83bb9c743179a (diff)
downloadminishell-5dc7a4036025bd7c3f997f08e2d9b14338c0e528.tar.gz
minishell-5dc7a4036025bd7c3f997f08e2d9b14338c0e528.tar.bz2
minishell-5dc7a4036025bd7c3f997f08e2d9b14338c0e528.zip
Added eval.c draft
Diffstat (limited to 'src')
-rw-r--r--src/eval.c134
1 files changed, 133 insertions, 1 deletions
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);
}