diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | include/minishell.h | 9 | ||||
| m--------- | libft | 0 | ||||
| -rw-r--r-- | src/builtin/env.c | 15 | ||||
| -rw-r--r-- | src/environment.c | 13 | ||||
| -rw-r--r-- | src/eval.c | 274 | ||||
| -rw-r--r-- | src/main.c | 25 | ||||
| -rw-r--r-- | src/parse.c | 1 | ||||
| -rw-r--r-- | src/state.c | 9 | ||||
| -rw-r--r-- | src/util.c | 1 |
10 files changed, 182 insertions, 167 deletions
@@ -31,7 +31,7 @@ SRC = $(SRCFILES) OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) CC = gcc -CCFLAGS = -I$(LIBFTDIR)/include -I$(INCLUDEDIR) -Wall -Wextra #-Werror +CCFLAGS = -g -I$(LIBFTDIR)/include -I$(INCLUDEDIR) -Wall -Wextra #-Werror LDFLAGS = -L$(LIBFTDIR) -lft NAME = minishell diff --git a/include/minishell.h b/include/minishell.h index 97293c2..4fbddfd 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -77,22 +77,19 @@ void ms_path_destroy(t_path *path); t_ftht *ms_environment_from_array(const char **envp); char **ms_environment_to_array(t_ftht *environment); -// probably bloat -// void ms_environment_destroy(t_ftht *environment); /* ** builtin*.c */ -typedef int t_status; - -typedef t_status (*t_builtin_func)(int argc, char **argv, char **envp); +typedef int (*t_builtin_func)(t_state *state); t_builtin_func ms_echo; t_builtin_func ms_cd; t_builtin_func ms_pwd; t_builtin_func ms_export; t_builtin_func ms_unset; -t_builtin_func ms_env; +// t_builtin_func ms_env; +int ms_env(t_state *state); t_builtin_func ms_exit; /* diff --git a/libft b/libft -Subproject 915f1b888cf9c05e4b61321f84ac045eacd8ddd +Subproject e792d0a3ff1c1da456c241530571263df0b887b diff --git a/src/builtin/env.c b/src/builtin/env.c index e69de29..ada0122 100644 --- a/src/builtin/env.c +++ b/src/builtin/env.c @@ -0,0 +1,15 @@ +#include "minishell.h" + +void st_print_env_variable(t_ftht_content *content) +{ + ft_putstr(content->key); + ft_putchar('='); + ft_putstr((char*)content->value); + ft_putchar('\n'); +} + +int ms_env(t_state *state) +{ + ft_htiter(state->environment, st_print_env_variable); + return (0); +} diff --git a/src/environment.c b/src/environment.c index a39b246..90cac86 100644 --- a/src/environment.c +++ b/src/environment.c @@ -21,7 +21,7 @@ t_ftht *ms_environment_from_array(const char **envp) char *key; char *value; /* int equal_pos; */ - + if (envp == NULL) return (NULL); if ((environment = ft_htnew(MS_ENVIRONMENT_HT_SIZE)) == NULL) @@ -32,12 +32,14 @@ t_ftht *ms_environment_from_array(const char **envp) // free stuff on error if ((value = ft_strchr(envp[i], '=')) == NULL) return (NULL); - if ((key = ft_strndup(envp[i], ft_strspn(envp[i], "="))) == NULL) + if ((key = ft_strndup(envp[i], ft_strcspn(envp[i], "="))) == NULL) return (NULL); - if ((value = ft_strdup(value)) == NULL) + /* printf("%s -- %d\n", envp[i], strcspn(envp[i], "=")); */ + if ((value = ft_strdup(value + 1)) == NULL) return (NULL); if (ft_htset(environment, key, value, ms_ht_del_str_entry) == NULL) return (NULL); + free(key); } return (environment); } @@ -49,8 +51,3 @@ char **ms_environment_to_array(t_ftht *environment) return (NULL); } - -/* void ms_environment_destroy(t_ftht *environment) */ -/* { */ -/* ft_htdestroy(environment, ms_ht_del_str_entry); */ -/* } */ @@ -1,140 +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) -{ - 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); -} +/* 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) */ +/* { */ +/* 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); */ +/* } */ @@ -12,22 +12,25 @@ #include "minishell.h" -int main(int argc, char **argv, char **envp) +int main(int argc, char **argv, const char **envp) { t_state state; - char *line; - int ret; + /* char *line; */ + /* int ret; */ + (void)argc; + (void)argv; if (ms_state_init(&state, envp) == -1) return (1); - - while ((ret = ft_next_line(STDIN_FILENO, &line)) == 1) - { - if (ms_eval(ms_parse(line)) == -1) - continue ; // and display error - free(line); - } - free(line); + ms_env(&state); + + /* while ((ret = ft_next_line(STDIN_FILENO, &line)) == 1) */ + /* { */ + /* if (ms_eval(ms_parse(line)) == -1) */ + /* continue ; // and display error */ + /* free(line); */ + /* } */ + /* free(line); */ ms_state_destroy(&state); return (0); } diff --git a/src/parse.c b/src/parse.c index b4bb994..5cdb066 100644 --- a/src/parse.c +++ b/src/parse.c @@ -2,5 +2,6 @@ t_parsing *ms_parse(char *input) { + (void)input; return NULL; } diff --git a/src/state.c b/src/state.c index 61d8d41..41c802b 100644 --- a/src/state.c +++ b/src/state.c @@ -14,11 +14,12 @@ int ms_state_init(t_state *state, const char **envp) { - if ((state->environment = ms_environment_from_array(envp)) == NULL) - return (-1); + state->environment = NULL; state->path = NULL; - if ((state->path = ms_path_update(state->path, ft_htget(state->environment, MS_PATH_KEY))) == NULL) + if ((state->environment = ms_environment_from_array(envp)) == NULL) return (-1); + /* if ((state->path = ms_path_update(state->path, ft_htget(state->environment, MS_PATH_KEY))) == NULL) */ + /* return (-1); */ return (0); } @@ -26,6 +27,6 @@ void ms_state_destroy(t_state *state) { if (state == NULL) return ; - ms_path_destroy(state->path); + /* ms_path_destroy(state->path); */ ft_htdestroy(state->environment, ms_ht_del_str_entry); } @@ -18,4 +18,5 @@ void ms_ht_del_str_entry(t_ftht_content *content) return ; free(content->key); free(content->value); + free(content); } |
