From 9317f4409db95c90a24aa7f067a6ae8218b74d9c Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 5 Aug 2024 13:35:51 +0200 Subject: Create basic structure for ast evaluation --- src/interpreter.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/interpreter.c diff --git a/src/interpreter.c b/src/interpreter.c new file mode 100644 index 0000000..2a09e6d --- /dev/null +++ b/src/interpreter.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* interpreter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser type == PIPE_NODE) + return (eval_pipe(&node->content.pipe)); + else if (node->type == CMD_NODE) + return (eval_cmd(&node->content.cmd)); + else + { + panic(UNREACHABLE); + return (-1); + } +} + +static int eval_pipe(t_pipe *pipe) +{ + return (0); +} + +static int eval_cmd(t_cmd *cmd) +{ + return (0); +} -- cgit v1.2.3 From 06f7c2548f68ec2786eec97219b55cd095de450d Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Thu, 8 Aug 2024 17:10:25 +0200 Subject: Add data structure and prototypes for env --- include/env.h | 23 +++++++++++++++++++++++ include/minishell.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 include/env.h diff --git a/include/env.h b/include/env.h new file mode 100644 index 0000000..1ea6f2e --- /dev/null +++ b/include/env.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser # include -- cgit v1.2.3 From 29a8e10e63f7e4ed1b08c4984c6c01431d3ccb06 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 9 Aug 2024 19:08:18 +0200 Subject: first ideas of my understanding --- src/builtins_part_one.c | 173 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 src/builtins_part_one.c diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c new file mode 100644 index 0000000..d7ab540 --- /dev/null +++ b/src/builtins_part_one.c @@ -0,0 +1,173 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtins_part_one.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ +/* Updated: 2024/08/09 19:07:31 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int echo(char **av) +{ + int i; + int f; + + i = 1; + if (av[1][0] == '\0') + { + write(1, "\n", 1); + return (1); + } + if (ft_strcmp(av[1], "-n")) + { + i++; + f = 1; + } + while (av[i]) + { + write(1, &av[1], ft_strlen(av[i])); + i++; + } + if (f) + write(1, "\n", 1); + return (0); +} +// Und args wären bei +// echo Hello World +// dann +// echo, Hello und World + + +// Die Builtins haben bitte int als return type. 0 = Success, 1 = Error usw. +// Als Parameter kann ich dir die das hier geben: +// char **args +// Aufpassen: args[0] ist der Name des Commands. +// t_env *env +// Hier hast du alle Variablen drin. +// Schau dir dafür am besten env.h an. + + + +// 2-G-4:~ chuhlig$ echo -n hello +// hello2-G-4:~ chuhlig$ echo test -n hello +// test -n hello + + + +// minishell % echo -n "Das ist" +// echo -n " ein Beispiel" +// echo " für echo -n." + +// 2-G-4:~ chuhlig$ echo 'hello world' +// hello world +// 2-G-4:~ chuhlig$ echo hello'world' +// helloworld +// 2-G-4:~ chuhlig$ echo hello""world +// helloworld +// 2-G-4:~ chuhlig$ echo '' + +// 2-G-4:~ chuhlig$ echo "$PWD" +// /Users/chuhlig +// 2-G-4:~ chuhlig$ echo '$PWD' +// $PWD +// 2-G-4:~ chuhlig$ echo "aspas ->'" +// aspas ->' +// 2-G-4:~ chuhlig$ echo "aspas -> ' " +// aspas -> ' +// 2-G-4:~ chuhlig$ echo 'aspas ->"' +// aspas ->" +// 2-G-4:~ chuhlig$ echo 'aspas -> " ' +// aspas -> " +// 2-G-4:~ chuhlig$ echo "> >> < * ? [ ] | ; [ ] || && ( ) & # $ \ <<" +// > >> < * ? [ ] | ; [ ] || && ( ) & # $ \ << +// 2-G-4:~ chuhlig$ echo '> >> < * ? [ ] | ; [ ] || && ( ) & # $ \ <<' +// > >> < * ? [ ] | ; [ ] || && ( ) & # $ \ << +// 2-G-4:~ chuhlig$ echo "exit_code ->$? user ->$USER home -> $HOME" +// exit_code ->0 user ->chuhlig home -> /Users/chuhlig +// 2-G-4:~ chuhlig$ echo 'exit_code ->$? user ->$USER home -> $HOME' +// exit_code ->$? user ->$USER home -> $HOME +// 2-G-4:~ chuhlig$ echo "$" +// $ +// 2-G-4:~ chuhlig$ echo '$' +// $ +// 2-G-4:~ chuhlig$ echo $ +// $ + +// 2-G-4:~ chuhlig$ echo "test" -n hello +// test -n hello +// 2-G-4:~ chuhlig$ echo -n "test" -n hello +// test -n hello2-G-4:~ chuhlig$ echo -n "test" echo -n hello +// test echo -n hello2-G-4:~ chuhlig$ + + + +// 2-G-4:~ chuhlig$ echo -n "test1 test2" test3 +// test1 test2 test32-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo -n " test1 test2 " 'test3 ' +// test1 test2 test3 2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo -n test1test2 +// test1test22-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo test1 -n +// test1 -n +// 2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo "test1 -n" +// test1 -n +// 2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo -n -n test1 +// test12-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo -n -n -n -n -n test1 +// test12-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo - +// - +// 2-G-4:~ chuhlig$ echo -- +// -- +// 2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo " -nn " +// -nn +// 2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo "-n test1 -n test2" +// -n test1 -n test2 +// 2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo "test1 -n test2" +// test1 -n test2 +// 2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo ~42 +// ~42 +// 2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo -n -n -nasd +// -nasd2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo -n -n -n-nnnnn +// -n-nnnnn2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo -n -nnnnnnn -n -nnn -nnnnn -n-n +// -n-n2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo -n -nnnnnnn -n -nnn -nnnnn -n feel my pain +// feel my pain2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo -n -n -n-n +// -n-n2-G-4:~ chuhlig$ +// 2-G-4:~ chuhlig$ echo "'totally logical'" +// 'totally logical' +// 2-G-4:~ chuhlig$ echo 'totally logical' +// totally logical +// 2-G-4:~ chuhlig$ echo ''totally logical'' +// totally logical +// 2-G-4:~ chuhlig$ echo ""'totally logical'"" +// totally logical + +// 2-G-4:~ chuhlig$ eCho + +// 2-G-4:~ chuhlig$ eChO + +// 2-G-4:~ chuhlig$ eCHO + +// 2-G-4:~ chuhlig$ ECHO +int pwd_builtin(void) +{ + char cwd[1028]; + + getcwd(cwd, sizeof(cwd)); + printf("%s\n", cwd); + return (0); +} \ No newline at end of file -- cgit v1.2.3 From 8cbba6da72ddd04e358bdb893e700702f92adacd Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Sun, 11 Aug 2024 15:15:06 +0200 Subject: fixed |> fixed norm added new function --- include/token.h | 5 +++-- lib/libft/Makefile | 1 + lib/libft/ft_isspace.c | 20 ++++++++++++++++++++ lib/libft/libft.h | 3 ++- src/repl.c | 4 ++-- src/tokenizer.c | 17 +++++++++-------- 6 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 lib/libft/ft_isspace.c diff --git a/include/token.h b/include/token.h index d7ff9f9..80ace03 100644 --- a/include/token.h +++ b/include/token.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 13:27:18 by dkaiser #+# #+# */ -/* Updated: 2024/08/05 13:23:27 by chuhlig ### ########.fr */ +/* Updated: 2024/08/11 13:46:22 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,6 +45,7 @@ t_token *new_redir_token(int type, t_token *previous, t_token *next); void free_token(t_token *token); -void tokenizer(char *s, t_token **token_list); +void tokenizer(char *s, t_token **token_list, + char quote_check); #endif \ No newline at end of file diff --git a/lib/libft/Makefile b/lib/libft/Makefile index 6951c43..6f2950c 100644 --- a/lib/libft/Makefile +++ b/lib/libft/Makefile @@ -10,6 +10,7 @@ SRC = ft_atoi.c \ ft_isascii.c \ ft_isdigit.c \ ft_isprint.c \ + ft_isspace.c \ ft_itoa.c \ ft_memchr.c \ ft_memcmp.c \ diff --git a/lib/libft/ft_isspace.c b/lib/libft/ft_isspace.c new file mode 100644 index 0000000..63f21fa --- /dev/null +++ b/lib/libft/ft_isspace.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/11 13:59:45 by chuhlig #+# #+# */ +/* Updated: 2024/08/11 14:04:23 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isspace(char c) +{ + if (c == ' ' || c == '\t') + return (1); + return (0); +} diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 67fbb0f..abb739d 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/10 16:37:54 by dkaiser #+# #+# */ -/* Updated: 2024/08/05 13:55:13 by chuhlig ### ########.fr */ +/* Updated: 2024/08/11 14:01:57 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ int ft_isalpha(int c); int ft_isdigit(int c); int ft_isalnum(int c); int ft_isprint(int c); +int ft_isspace(char c); int ft_isascii(int c); int ft_strlen(const char *str); void *ft_memset(void *b, int c, size_t len); diff --git a/src/repl.c b/src/repl.c index 1fd7be7..fe9faf3 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2024/08/09 15:27:11 by chuhlig ### ########.fr */ +/* Updated: 2024/08/11 14:41:29 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,8 +27,8 @@ void repl(const char *prompt) return ; add_history(input); token_list = NULL; - tokenizer(input, &token_list); current = token_list; + tokenizer(input, &token_list, '\0'); while (current != NULL) { next = current->next; diff --git a/src/tokenizer.c b/src/tokenizer.c index 34685ac..a9a86ca 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/09 15:40:00 by chuhlig ### ########.fr */ +/* Updated: 2024/08/11 14:52:54 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ void print_token(t_token *token) } } -void conditional_print(char *string, int start_of_string, int i, +void snap_string_token(char *string, int start_of_string, int i, t_token **token_list) { char *line; @@ -64,7 +64,7 @@ void conditional_print(char *string, int start_of_string, int i, void handle_special_chars(char *s, int *i, int *start, t_token **token_list) { - conditional_print(s, *start, *i - 1, token_list); + snap_string_token(s, *start, *i - 1, token_list); if (s[*i] == '<' && s[*i + 1] == '<') *token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL); else if (s[*i] == '>' && s[*i + 1] == '>') @@ -78,14 +78,15 @@ void handle_special_chars(char *s, int *i, int *start, t_token **token_list) else if (s[*i] == '\n') *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); print_token(*token_list); - if (s[*i + 1] == '<' || s[*i + 1] == '>') + if (s[*i] == '<' && s[*i + 1] == '<') + (*i)++; + if (s[*i] == '>' && s[*i + 1] == '>') (*i)++; *start = *i + 1; } -void tokenizer(char *s, t_token **token_list) +void tokenizer(char *s, t_token **token_list, char quote_check) { - char quote_check; int pos; int i; int f; @@ -104,9 +105,9 @@ void tokenizer(char *s, t_token **token_list) f = 1; quote_check = s[i]; } - if ((!f && (s[i] == ' ' || s[i] == '\t')) || i == ft_strlen(s) - 1) + if ((!f && (ft_isspace(s[i + 1]))) || i == ft_strlen(s) - 1) { - conditional_print(s, pos, i, token_list); + snap_string_token(s, pos, i, token_list); pos = i + 1; } } -- cgit v1.2.3 From d9fcd5d84006f312dc417124c697341f2d94de6b Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Sun, 18 Aug 2024 04:30:20 +0200 Subject: 2nd push with unset export exit and cd --- src/builtins_part_one.c | 267 ++++++++++++++++++++++++------------------------ 1 file changed, 135 insertions(+), 132 deletions(-) diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index d7ab540..fc0fdab 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,16 +6,19 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2024/08/09 19:07:31 by chuhlig ### ########.fr */ +/* Updated: 2024/08/18 04:28:56 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ +#include "env.h" + int echo(char **av) { int i; int f; i = 1; + f = 1; if (av[1][0] == '\0') { write(1, "\n", 1); @@ -24,7 +27,7 @@ int echo(char **av) if (ft_strcmp(av[1], "-n")) { i++; - f = 1; + f = 0; } while (av[i]) { @@ -35,139 +38,139 @@ int echo(char **av) write(1, "\n", 1); return (0); } -// Und args wären bei -// echo Hello World -// dann -// echo, Hello und World - - -// Die Builtins haben bitte int als return type. 0 = Success, 1 = Error usw. -// Als Parameter kann ich dir die das hier geben: -// char **args -// Aufpassen: args[0] ist der Name des Commands. -// t_env *env -// Hier hast du alle Variablen drin. -// Schau dir dafür am besten env.h an. - - - -// 2-G-4:~ chuhlig$ echo -n hello -// hello2-G-4:~ chuhlig$ echo test -n hello -// test -n hello - - - -// minishell % echo -n "Das ist" -// echo -n " ein Beispiel" -// echo " für echo -n." - -// 2-G-4:~ chuhlig$ echo 'hello world' -// hello world -// 2-G-4:~ chuhlig$ echo hello'world' -// helloworld -// 2-G-4:~ chuhlig$ echo hello""world -// helloworld -// 2-G-4:~ chuhlig$ echo '' - -// 2-G-4:~ chuhlig$ echo "$PWD" -// /Users/chuhlig -// 2-G-4:~ chuhlig$ echo '$PWD' -// $PWD -// 2-G-4:~ chuhlig$ echo "aspas ->'" -// aspas ->' -// 2-G-4:~ chuhlig$ echo "aspas -> ' " -// aspas -> ' -// 2-G-4:~ chuhlig$ echo 'aspas ->"' -// aspas ->" -// 2-G-4:~ chuhlig$ echo 'aspas -> " ' -// aspas -> " -// 2-G-4:~ chuhlig$ echo "> >> < * ? [ ] | ; [ ] || && ( ) & # $ \ <<" -// > >> < * ? [ ] | ; [ ] || && ( ) & # $ \ << -// 2-G-4:~ chuhlig$ echo '> >> < * ? [ ] | ; [ ] || && ( ) & # $ \ <<' -// > >> < * ? [ ] | ; [ ] || && ( ) & # $ \ << -// 2-G-4:~ chuhlig$ echo "exit_code ->$? user ->$USER home -> $HOME" -// exit_code ->0 user ->chuhlig home -> /Users/chuhlig -// 2-G-4:~ chuhlig$ echo 'exit_code ->$? user ->$USER home -> $HOME' -// exit_code ->$? user ->$USER home -> $HOME -// 2-G-4:~ chuhlig$ echo "$" -// $ -// 2-G-4:~ chuhlig$ echo '$' -// $ -// 2-G-4:~ chuhlig$ echo $ -// $ - -// 2-G-4:~ chuhlig$ echo "test" -n hello -// test -n hello -// 2-G-4:~ chuhlig$ echo -n "test" -n hello -// test -n hello2-G-4:~ chuhlig$ echo -n "test" echo -n hello -// test echo -n hello2-G-4:~ chuhlig$ - - - -// 2-G-4:~ chuhlig$ echo -n "test1 test2" test3 -// test1 test2 test32-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo -n " test1 test2 " 'test3 ' -// test1 test2 test3 2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo -n test1test2 -// test1test22-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo test1 -n -// test1 -n -// 2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo "test1 -n" -// test1 -n -// 2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo -n -n test1 -// test12-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo -n -n -n -n -n test1 -// test12-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo - -// - -// 2-G-4:~ chuhlig$ echo -- -// -- -// 2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo " -nn " -// -nn -// 2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo "-n test1 -n test2" -// -n test1 -n test2 -// 2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo "test1 -n test2" -// test1 -n test2 -// 2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo ~42 -// ~42 -// 2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo -n -n -nasd -// -nasd2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo -n -n -n-nnnnn -// -n-nnnnn2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo -n -nnnnnnn -n -nnn -nnnnn -n-n -// -n-n2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo -n -nnnnnnn -n -nnn -nnnnn -n feel my pain -// feel my pain2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo -n -n -n-n -// -n-n2-G-4:~ chuhlig$ -// 2-G-4:~ chuhlig$ echo "'totally logical'" -// 'totally logical' -// 2-G-4:~ chuhlig$ echo 'totally logical' -// totally logical -// 2-G-4:~ chuhlig$ echo ''totally logical'' -// totally logical -// 2-G-4:~ chuhlig$ echo ""'totally logical'"" -// totally logical - -// 2-G-4:~ chuhlig$ eCho - -// 2-G-4:~ chuhlig$ eChO - -// 2-G-4:~ chuhlig$ eCHO - -// 2-G-4:~ chuhlig$ ECHO -int pwd_builtin(void) + +int pwd(void) { char cwd[1028]; getcwd(cwd, sizeof(cwd)); printf("%s\n", cwd); return (0); -} \ No newline at end of file +} + +int env(char **env) +{ + int i; + + i = 0; + while (env[i] != NULL) + { + printf("%s\n", env[i]); + i++; + } + return (0); +} + +int cd(char **av) +{ + + if(av[1] == NULL) + { + while (env[i] != NULL) // home command + { + if (ft_strncmp(env[i], "HOME=", 5) == 0) + break ; + i++; + } + // before chdir we need to update old pwd + // getcwd(cwd, sizeof(cwd)); + // str = ft_strjoin("PWD= or old pwd=", cwd); + // env[i] = str; + if(chdir(env[i] + 5) == -1) + return; + } + else + //update old pwd + // + while (env[i] != NULL) // home command + { + if (ft_strncmp(env[i], "old PWD=", 5) == 0) + break ; + i++; + } + str = ft_strjoin("old pwd=", cwd); + env[i] = str; + // + chdir(av[1] == -1); + return ; + // udpate pwd + while (env[i] != NULL) // home command + { + if (ft_strncmp(env[i], "PWD=", 5) == 0) + break ; + i++; + } + str = ft_strjoin("PWD=", cwd); + env[i] = str; +} + +// exit + + +int exit(char *av) +{ + freenode free toke free sequence stop repl free env + clear history +} +//joar + +//export + +int export(char **av, t_env **env) +{ + char *tmp; + if(tmp = ft_strchr(av[1])) + { + tmp = '\0'; + t_env *current; + current = *env; + while(current) + { + if (strcmp(current->name, av[1]) == 0) + { + free(current->value); + current->value = ft_strdup(tmp + 1); + break; + } + current = current->next; + } + if (!current) + { + current = malloc(sizeof(t_env)); + current->name = ft_strdup(av[1]); + current->value = ft_strdup(tmp + 1); + current->next = *env; + *env = current; + } + return (0); + } + else + syntax return 1; +} + +//unset + +int unset(char **av, t_env **env) +{ + t_env *current; + t_env *prev; + + current = env; + prev = NULL; + while (current) + { + if(ft_strcmp(current->name, av[1] == 0)) + break ; + prev = current; + current = current->next; + } + if(current) + { + if(prev) + prev->next = current->next; + else + *env = current->next + free(t_env); + } + return (0); +} -- cgit v1.2.3 From 8fb55b598b6a2f7565fe4a5f82186dc04ee6e7eb Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 29 Aug 2024 15:55:46 +0200 Subject: temporary here get envlst --- src/builtins_part_one.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index fc0fdab..6ca0caa 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2024/08/18 04:28:56 by chuhlig ### ########.fr */ +/* Updated: 2024/08/29 14:24:13 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -116,10 +116,11 @@ int exit(char *av) //export +// add a part to append it to list and maybe also check bc export pwd or home stuf int export(char **av, t_env **env) { char *tmp; - if(tmp = ft_strchr(av[1])) + if(tmp = ft_strchr(av[1], '=')) { tmp = '\0'; t_env *current; @@ -149,14 +150,17 @@ int export(char **av, t_env **env) } //unset - +//for unset as well check and for name part int unset(char **av, t_env **env) { t_env *current; t_env *prev; - + char *tmp; + current = env; prev = NULL; + tmp = ft_strchr(av[1], '='); + tmp = '\0'; while (current) { if(ft_strcmp(current->name, av[1] == 0)) @@ -174,3 +178,27 @@ int unset(char **av, t_env **env) } return (0); } + +void getenvlst(t_env **env, char **en) +{ + char *tmp; + int i; + int t; + t_env *current; + + t = 0; + i = 0; + while (en[i] != NULL) + { + tmp = ft_strchr(en[i], '='); + tmp = '\0'; + current = *env; + current = malloc(sizeof(t_env)); + current->name = ft_strdup(en[i]); + current->value = ft_strdup(tmp + 1); + current->next = *env; + *env = current; + i++; + } + return (0); +} \ No newline at end of file -- cgit v1.2.3 From 85ddabacb816a04fa86875d0aab6ec6337e132ed Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 29 Aug 2024 15:56:23 +0200 Subject: just date --- src/main.c | 2 +- src/tokenizer.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 8523b9e..596b611 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */ -/* Updated: 2024/07/18 16:44:14 by chuhlig ### ########.fr */ +/* Updated: 2024/08/26 19:55:57 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/tokenizer.c b/src/tokenizer.c index 34685ac..dfdffe1 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/09 15:40:00 by chuhlig ### ########.fr */ +/* Updated: 2024/08/29 15:01:48 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ -- cgit v1.2.3 From 5373e9e53accb20438d92a78224221734786d051 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 29 Aug 2024 15:57:14 +0200 Subject: added other token append to remove the error that its not at the start --- src/new_token.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/new_token.c b/src/new_token.c index 92ff421..6431c7d 100644 --- a/src/new_token.c +++ b/src/new_token.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* new_token.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 14:29:44 by dkaiser #+# #+# */ -/* Updated: 2024/06/28 14:59:34 by dkaiser ### ########.fr */ +/* Updated: 2024/08/29 15:30:52 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,3 +50,28 @@ t_token *new_redir_token(int type, t_token *previous, t_token *next) token->content.redir_type = type; return (token); } + +// void ft_append_token(int type, t_token **list)// but we need somewhere token/node head initialize with nul +// { +// t_token *node; +// t_token *last_node; + +// if (!list) +// return ; +// node = malloc(sizeof(t_token)); +// if (!node) +// return ; +// node->next = NULL; +// node->type = type; +// if (!*list) +// { +// *list = node; +// node->previous = NULL; +// } +// else +// { +// last_node = ft_lstlast(*list); +// last_node->next = node; +// node->previous = last_node; +// } +// } \ No newline at end of file -- cgit v1.2.3 From 801598045e52071af1effa3a28ddffb17671b08e Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Wed, 4 Sep 2024 17:53:41 +0200 Subject: update for builtins and now 2 envlist --- src/builtins_part_one.c | 79 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index 6ca0caa..10d8979 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,13 +6,13 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2024/08/29 14:24:13 by chuhlig ### ########.fr */ +/* Updated: 2024/09/04 17:52:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" -int echo(char **av) +int echo(char **av)//more or less done { int i; int f; @@ -39,29 +39,44 @@ int echo(char **av) return (0); } -int pwd(void) + +int pwd(t_env **env, char *av) //done { - char cwd[1028]; + t_env *current; + t_env *prev; + char *tmp; - getcwd(cwd, sizeof(cwd)); - printf("%s\n", cwd); + current = env; + prev = NULL; + while (current) + { + if(ft_strcmp(current->name, av == 0)) + break ; + prev = current; + current = current->next; + } + ft_printf("%s\n", current->value); return (0); } -int env(char **env) +int env(t_env **env)// here i need str env oder ich print es alles zusammen xD { - int i; + t_env *current; + t_env *prev; + char *tmp; - i = 0; - while (env[i] != NULL) + current = env; + prev = NULL; + while (current) { - printf("%s\n", env[i]); - i++; + ft_printf("%s\n", current->name); + prev = current; + current = current->next; } return (0); } -int cd(char **av) +int cd(t_env **env,char **av) // here also need to do the same again for envstrarr. dont like it { if(av[1] == NULL) @@ -116,8 +131,7 @@ int exit(char *av) //export -// add a part to append it to list and maybe also check bc export pwd or home stuf -int export(char **av, t_env **env) +int export(char **av, t_env **env) // here also need to do the same again for envstrarr. dont like it { char *tmp; if(tmp = ft_strchr(av[1], '=')) @@ -151,7 +165,7 @@ int export(char **av, t_env **env) //unset //for unset as well check and for name part -int unset(char **av, t_env **env) +int unset(char **av, t_env **env) // here also need to do the same again for envstrarr. dont like it { t_env *current; t_env *prev; @@ -174,19 +188,17 @@ int unset(char **av, t_env **env) prev->next = current->next; else *env = current->next - free(t_env); + free(t_env);//need function for this } return (0); } -void getenvlst(t_env **env, char **en) +void getenvlst(t_env **env, char **en)// seperated name and value { char *tmp; int i; - int t; t_env *current; - t = 0; i = 0; while (en[i] != NULL) { @@ -201,4 +213,29 @@ void getenvlst(t_env **env, char **en) i++; } return (0); -} \ No newline at end of file +} + +void getenvlststr(t_env **env, char **en)//without serparation +{ + int i; + size_t t; + t_env *current; + + t = 0; + i = 0; + while (en[i] != NULL) + { + current = *env; + current = malloc(sizeof(t_env)); + while(en[t] != '=') + t++; + current->name = ft_substr(en[i], 0, t); + current->value = ft_strdup(en[i]); + current->next = *env; + *env = current; + i++; + } + return (0); +} + +//should name the list different \ No newline at end of file -- cgit v1.2.3 From fd179cf97e176b00cd01763d82e89d9af6ed355c Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 13 Sep 2024 21:21:09 +0200 Subject: update builtins for env structure --- src/builtins_part_one.c | 141 ++++++++++++++++++++++++++++-------------------- 1 file changed, 82 insertions(+), 59 deletions(-) diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index 10d8979..e99bb90 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,13 +6,13 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2024/09/04 17:52:49 by chuhlig ### ########.fr */ +/* Updated: 2024/09/13 21:20:17 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" -int echo(char **av)//more or less done +int echo(char **av) { int i; int f; @@ -40,7 +40,7 @@ int echo(char **av)//more or less done } -int pwd(t_env **env, char *av) //done +int pwd(t_env **env, char *av) { t_env *current; t_env *prev; @@ -59,75 +59,98 @@ int pwd(t_env **env, char *av) //done return (0); } -int env(t_env **env)// here i need str env oder ich print es alles zusammen xD +int env(t_env **env) { t_env *current; t_env *prev; - char *tmp; current = env; prev = NULL; while (current) { - ft_printf("%s\n", current->name); + ft_printf("%s", current->name); + ft_printf("=%s\n", current->value); prev = current; current = current->next; } return (0); } -int cd(t_env **env,char **av) // here also need to do the same again for envstrarr. dont like it +void update_oldpwd(t_env **env) { + t_env *current; + t_env *prev; + char cwd[1028]; + char *tmp; - if(av[1] == NULL) - { - while (env[i] != NULL) // home command + while (current) // home command { - if (ft_strncmp(env[i], "HOME=", 5) == 0) + if (ft_strncmp(current->name, "OLDPWD", 6) == 0) break ; - i++; + prev = current; + current = current->next; } - // before chdir we need to update old pwd - // getcwd(cwd, sizeof(cwd)); - // str = ft_strjoin("PWD= or old pwd=", cwd); - // env[i] = str; - if(chdir(env[i] + 5) == -1) - return; + getcwd(cwd, sizeof(cwd)); + tmp = ft_strdup(cwd); + free(current->value); + current->value = tmp; +} + +void update_pwd(t_env **env) +{ + t_env *current; + t_env *prev; + char cwd[1028]; + char *tmp; + + while (current) + { + if (ft_strncmp(current->name, "PWD", 3) == 0) + break ; + prev = current; + current = current->next; } - else - //update old pwd - // - while (env[i] != NULL) // home command + getcwd(cwd, sizeof(cwd)); + tmp = ft_strdup(cwd); + free(current->value); + current->value = tmp; +} + +int cd(t_env **env,char **av) +{ + t_env *current; + t_env *prev; + t_env *pwd; + + current = env; + if(av[1] == NULL) + { + while (current) // home command { - if (ft_strncmp(env[i], "old PWD=", 5) == 0) - break ; - i++; + if (ft_strncmp(current->name, "HOME", 4) == 0) + break ; + prev = current; + current = current->next; } - str = ft_strjoin("old pwd=", cwd); - env[i] = str; - // - chdir(av[1] == -1); + if(chdir(current->value) == -1) return ; - // udpate pwd - while (env[i] != NULL) // home command + } + else { - if (ft_strncmp(env[i], "PWD=", 5) == 0) - break ; - i++; + update_oldpwd(&env); + if(chdir(av[1]) == -1) + return ; + update_pwd(&env); } - str = ft_strjoin("PWD=", cwd); - env[i] = str; } // exit - int exit(char *av) { freenode free toke free sequence stop repl free env clear history } -//joar //export @@ -215,27 +238,27 @@ void getenvlst(t_env **env, char **en)// seperated name and value return (0); } -void getenvlststr(t_env **env, char **en)//without serparation -{ - int i; - size_t t; - t_env *current; +// void getenvlststr(t_env **env, char **en)//without serparation +// { +// int i; +// size_t t; +// t_env *current; - t = 0; - i = 0; - while (en[i] != NULL) - { - current = *env; - current = malloc(sizeof(t_env)); - while(en[t] != '=') - t++; - current->name = ft_substr(en[i], 0, t); - current->value = ft_strdup(en[i]); - current->next = *env; - *env = current; - i++; - } - return (0); -} +// t = 0; +// i = 0; +// while (en[i] != NULL) +// { +// current = *env; +// current = malloc(sizeof(t_env)); +// while(en[t] != '=') +// t++; +// current->name = ft_substr(en[i], 0, t); +// current->value = ft_strdup(en[i]); +// current->next = *env; +// *env = current; +// i++; +// } +// return (0); +// } //should name the list different \ No newline at end of file -- cgit v1.2.3 From a4927cc09474bcd1cf7280392485146abce0a12c Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 13 Sep 2024 21:46:52 +0200 Subject: update for unset export and cd --- src/builtins_part_one.c | 61 ++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index e99bb90..42561e9 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2024/09/13 21:20:17 by chuhlig ### ########.fr */ +/* Updated: 2024/09/13 21:46:08 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -125,6 +125,7 @@ int cd(t_env **env,char **av) current = env; if(av[1] == NULL) { + update_oldpwd(&env); while (current) // home command { if (ft_strncmp(current->name, "HOME", 4) == 0) @@ -157,33 +158,39 @@ int exit(char *av) int export(char **av, t_env **env) // here also need to do the same again for envstrarr. dont like it { char *tmp; - if(tmp = ft_strchr(av[1], '=')) + t_env *current; + int i; + + i = i; + while(av[i]) { - tmp = '\0'; - t_env *current; - current = *env; - while(current) + if(tmp = ft_strchr(av[i], '=')) { - if (strcmp(current->name, av[1]) == 0) + tmp = '\0'; + current = *env; + while(current) { - free(current->value); - current->value = ft_strdup(tmp + 1); - break; - } - current = current->next; + if (strcmp(current->name, av[i]) == 0) + { + free(current->value); + current->value = ft_strdup(tmp + 1); + break; + } + current = current->next; + } + if (!current) + { + current = malloc(sizeof(t_env)); + current->name = ft_strdup(av[i]); + current->value = ft_strdup(tmp + 1); + current->next = *env; + *env = current; + } + return (0); } - if (!current) - { - current = malloc(sizeof(t_env)); - current->name = ft_strdup(av[1]); - current->value = ft_strdup(tmp + 1); - current->next = *env; - *env = current; - } - return (0); + i++; } - else - syntax return 1; + return 1; } //unset @@ -192,12 +199,9 @@ int unset(char **av, t_env **env) // here also need to do the same again for env { t_env *current; t_env *prev; - char *tmp; current = env; prev = NULL; - tmp = ft_strchr(av[1], '='); - tmp = '\0'; while (current) { if(ft_strcmp(current->name, av[1] == 0)) @@ -210,8 +214,9 @@ int unset(char **av, t_env **env) // here also need to do the same again for env if(prev) prev->next = current->next; else - *env = current->next - free(t_env);//need function for this + *env = current->next; + free(current->value); + free(current); } return (0); } -- cgit v1.2.3 From 9b1ddf7b3491f26d8b3ba9daeca7174aede9c781 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Tue, 17 Sep 2024 16:34:57 +0200 Subject: added remove for env list --- src/builtins_part_one.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index 42561e9..5a35236 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2024/09/13 21:46:08 by chuhlig ### ########.fr */ +/* Updated: 2024/09/17 16:23:15 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -155,7 +155,7 @@ int exit(char *av) //export -int export(char **av, t_env **env) // here also need to do the same again for envstrarr. dont like it +int export(char **av, t_env **env) { char *tmp; t_env *current; @@ -195,7 +195,7 @@ int export(char **av, t_env **env) // here also need to do the same again for en //unset //for unset as well check and for name part -int unset(char **av, t_env **env) // here also need to do the same again for envstrarr. dont like it +int unset(char **av, t_env **env) { t_env *current; t_env *prev; @@ -243,6 +243,24 @@ void getenvlst(t_env **env, char **en)// seperated name and value return (0); } + +void free_envlst(t_env **env) +{ + t_env *cur; + t_env *new; + + cur = *env; + while (cur) + { + new = cur->next; + free(cur->name); + free(cur->value); + free(cur); + cur = new; + } + +} + // void getenvlststr(t_env **env, char **en)//without serparation // { // int i; -- cgit v1.2.3 From 2b6dfc7368384655da2a843896539f443695b972 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 17 Oct 2024 14:30:23 +0200 Subject: removed env stuf to move it into env branch --- src/builtins_part_one.c | 41 +---------------------------------------- 1 file changed, 1 insertion(+), 40 deletions(-) diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index 5a35236..d817336 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2024/09/17 16:23:15 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 14:29:40 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -221,45 +221,6 @@ int unset(char **av, t_env **env) return (0); } -void getenvlst(t_env **env, char **en)// seperated name and value -{ - char *tmp; - int i; - t_env *current; - - i = 0; - while (en[i] != NULL) - { - tmp = ft_strchr(en[i], '='); - tmp = '\0'; - current = *env; - current = malloc(sizeof(t_env)); - current->name = ft_strdup(en[i]); - current->value = ft_strdup(tmp + 1); - current->next = *env; - *env = current; - i++; - } - return (0); -} - - -void free_envlst(t_env **env) -{ - t_env *cur; - t_env *new; - - cur = *env; - while (cur) - { - new = cur->next; - free(cur->name); - free(cur->value); - free(cur); - cur = new; - } - -} // void getenvlststr(t_env **env, char **en)//without serparation // { -- cgit v1.2.3 From a0016dffa6c5412d7c53b3d9040b837299c50159 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 17 Oct 2024 14:32:28 +0200 Subject: added env linked lis c file --- src/env.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/env.c diff --git a/src/env.c b/src/env.c new file mode 100644 index 0000000..2403329 --- /dev/null +++ b/src/env.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ +/* Updated: 2024/10/17 14:31:50 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "env.h" + +void getenvlst(t_env **env, char **en)// seperated name and value +{ + char *tmp; + int i; + t_env *current; + + i = 0; + while (en[i] != NULL) + { + tmp = ft_strchr(en[i], '='); + tmp = '\0'; + current = *env; + current = malloc(sizeof(t_env)); + current->name = ft_strdup(en[i]); + current->value = ft_strdup(tmp + 1); + current->next = *env; + *env = current; + i++; + } + return (0); +} + + +void free_envlst(t_env **env) +{ + t_env *cur; + t_env *new; + + cur = *env; + while (cur) + { + new = cur->next; + free(cur->name); + free(cur->value); + free(cur); + cur = new; + } + +} \ No newline at end of file -- cgit v1.2.3 From 9298e72de61af5311678c656c12fc177589671a7 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 17 Oct 2024 15:32:02 +0200 Subject: fixed normitte and fixed return issue --- src/env.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/env.c b/src/env.c index 2403329..8105bf4 100644 --- a/src/env.c +++ b/src/env.c @@ -6,17 +6,17 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ -/* Updated: 2024/10/17 14:31:50 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:18:44 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" -void getenvlst(t_env **env, char **en)// seperated name and value +void getenvlst(t_env **env, char **en) { char *tmp; int i; - t_env *current; + t_env *current; i = 0; while (en[i] != NULL) @@ -24,21 +24,19 @@ void getenvlst(t_env **env, char **en)// seperated name and value tmp = ft_strchr(en[i], '='); tmp = '\0'; current = *env; - current = malloc(sizeof(t_env)); - current->name = ft_strdup(en[i]); - current->value = ft_strdup(tmp + 1); - current->next = *env; - *env = current; + current = malloc(sizeof(t_env)); + current->name = ft_strdup(en[i]); + current->value = ft_strdup(tmp + 1); + current->next = *env; + *env = current; i++; } - return (0); } - void free_envlst(t_env **env) { t_env *cur; - t_env *new; + t_env *new; cur = *env; while (cur) @@ -49,5 +47,4 @@ void free_envlst(t_env **env) free(cur); cur = new; } - } \ No newline at end of file -- cgit v1.2.3 From e6e8051fbda53a75332e09f917e74d687f467a8a Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 17 Oct 2024 15:38:15 +0200 Subject: changed prototypes in header file --- include/env.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/include/env.h b/include/env.h index 12d8db0..5a65333 100644 --- a/include/env.h +++ b/include/env.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* env.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/09/13 16:26:16 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 15:37:32 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,9 +17,6 @@ typedef struct s_env struct s_env *next; } t_env; -char *env_get(t_env *env, char *name); -void env_export(t_env *env, char *name, char *value); -void env_unset(t_env *env, char *name); -char **env_to_strlst(t_env *env); -t_env **env_from_strlst(char **lst); +void getenvlst(t_env **env, char **en); +void free_envlst(t_env **env); -- cgit v1.2.3 From d05140c9c6bacce28b37124fbda48ea3a39ac486 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 17 Oct 2024 15:48:50 +0200 Subject: fixed *tmp and added libft in env header --- include/env.h | 4 +++- src/env.c | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/env.h b/include/env.h index 5a65333..e774002 100644 --- a/include/env.h +++ b/include/env.h @@ -6,10 +6,12 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:37:32 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:47:30 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" + typedef struct s_env { char *name; diff --git a/src/env.c b/src/env.c index 8105bf4..c71b2cb 100644 --- a/src/env.c +++ b/src/env.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ -/* Updated: 2024/10/17 15:18:44 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:47:11 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ void getenvlst(t_env **env, char **en) while (en[i] != NULL) { tmp = ft_strchr(en[i], '='); - tmp = '\0'; + *tmp = '\0'; current = *env; current = malloc(sizeof(t_env)); current->name = ft_strdup(en[i]); -- cgit v1.2.3 From e7faa6525a007e463c06a20e438a2253287011f1 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Thu, 17 Oct 2024 16:06:53 +0200 Subject: yes --- Makefile | 2 +- include/env.h | 6 ++++-- include/minishell.h | 6 ++++-- src/env.c | 19 ++++++++++++++++--- src/interpreter.c | 21 +++++++++++++-------- src/main.c | 10 +++++++--- src/repl.c | 7 +++++-- 7 files changed, 50 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 827c317..283db1a 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ HEADERS = -I include -I $(LIB_DIR)/libft VPATH := src SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ free_token.c new_node.c free_node.c tokenizer.c parser.c \ - parse_cmd.c collect_redirs.c print_ast.c + parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/env.h b/include/env.h index 5a65333..ee01181 100644 --- a/include/env.h +++ b/include/env.h @@ -6,10 +6,12 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:37:32 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:59:59 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" + typedef struct s_env { char *name; @@ -19,4 +21,4 @@ typedef struct s_env void getenvlst(t_env **env, char **en); void free_envlst(t_env **env); - +char *env_get(t_env *env, char *name); diff --git a/include/minishell.h b/include/minishell.h index 6997b15..2319e8b 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ -/* Updated: 2024/10/17 15:18:44 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:58:28 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" +#include "get_next_line.h" +#include "libft.h" void getenvlst(t_env **env, char **en) { @@ -22,7 +24,7 @@ void getenvlst(t_env **env, char **en) while (en[i] != NULL) { tmp = ft_strchr(en[i], '='); - tmp = '\0'; + *tmp = '\0'; current = *env; current = malloc(sizeof(t_env)); current->name = ft_strdup(en[i]); @@ -47,4 +49,15 @@ void free_envlst(t_env **env) free(cur); cur = new; } -} \ No newline at end of file +} + +char *env_get(t_env *env, char *name) +{ + while (env != NULL) + { + if (!ft_strncmp(env->name, name, ft_strlen(name))) + return (env->value); + env = env->next; + } + return (NULL); +} diff --git a/src/interpreter.c b/src/interpreter.c index 2a09e6d..abaeb47 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,21 +6,21 @@ /* By: dkaiser type == PIPE_NODE) - return (eval_pipe(&node->content.pipe)); + return (eval_pipe(&node->content.pipe, env)); else if (node->type == CMD_NODE) - return (eval_cmd(&node->content.cmd)); + return (eval_cmd(&node->content.cmd, env)); else { panic(UNREACHABLE); @@ -28,12 +28,17 @@ int eval(t_node *node) } } -static int eval_pipe(t_pipe *pipe) +static int eval_pipe(t_pipe *pipe, t_env *env) { + dbg("TODO: PIPE"); + eval_cmd(&pipe->left->content.cmd, env); + eval_cmd(&pipe->right->content.cmd, env); return (0); } -static int eval_cmd(t_cmd *cmd) +static int eval_cmd(t_cmd *cmd, t_env *env) { + printf("%s\n", cmd->args[0]); + printf("PATH=%s\n", env_get(env, "PATH")); return (0); } diff --git a/src/main.c b/src/main.c index 8523b9e..64bc312 100644 --- a/src/main.c +++ b/src/main.c @@ -6,15 +6,19 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */ -/* Updated: 2024/07/18 16:44:14 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:34:02 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "../include/minishell.h" -int main(void) +int main(int argc, char *argv[], char *envp[]) { + t_env *env; + if (!argc && !argv) + return (1); if (init()) return (1); - repl("Minishell $ "); + getenvlst(&env, envp); + repl("Minishell $ ", env); } diff --git a/src/repl.c b/src/repl.c index d590fec..931e25e 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,14 +6,14 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2024/09/13 16:26:35 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 15:25:48 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "../include/minishell.h" #include "token.h" -void repl(const char *prompt) +void repl(const char *prompt, t_env *env) { char *input; t_token *token_list; @@ -31,7 +31,10 @@ void repl(const char *prompt) tokenizer(input, &token_list, '\0'); lines = parse(token_list); if (lines) + { print_ast(lines->content); + eval(lines->content, env); + } free(input); } } -- cgit v1.2.3 From a3b571e8549f7f7b5a6ca2da449cc5d3aefa9084 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Thu, 17 Oct 2024 17:19:48 +0200 Subject: Add path handling --- Makefile | 3 ++- include/env.h | 8 ++++---- include/minishell.h | 5 +++-- src/interpreter.c | 5 ++--- src/main.c | 5 +++-- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 283db1a..3ae1359 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,8 @@ HEADERS = -I include -I $(LIB_DIR)/libft VPATH := src SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ free_token.c new_node.c free_node.c tokenizer.c parser.c \ - parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c + parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \ + get_cmd_path.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/env.h b/include/env.h index ee01181..f22ebdd 100644 --- a/include/env.h +++ b/include/env.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:59:59 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 17:01:27 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,6 @@ typedef struct s_env struct s_env *next; } t_env; -void getenvlst(t_env **env, char **en); -void free_envlst(t_env **env); -char *env_get(t_env *env, char *name); +void getenvlst(t_env **env, char **en); +void free_envlst(t_env **env); +char *env_get(t_env *env, char *name); diff --git a/include/minishell.h b/include/minishell.h index 2319e8b..c84fda3 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: dkaiser args[0]); - printf("PATH=%s\n", env_get(env, "PATH")); + printf("%s\n", get_cmd_path(cmd->args[0], env)); return (0); } diff --git a/src/main.c b/src/main.c index 64bc312..e87bc99 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:34:02 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 17:01:45 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,8 @@ int main(int argc, char *argv[], char *envp[]) { - t_env *env; + t_env *env; + if (!argc && !argv) return (1); if (init()) -- cgit v1.2.3 From 32e454ed265fc66dc45e43a3c68fbbe416fc16f9 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 21 Oct 2024 14:32:36 +0200 Subject: Add path handling for real now --- src/get_cmd_path.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/get_cmd_path.c diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c new file mode 100644 index 0000000..1ecf393 --- /dev/null +++ b/src/get_cmd_path.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_cmd_path.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 17:01:27 by dkaiser ### ########.fr */ +/* Updated: 2024/10/21 14:57:24 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,3 +22,4 @@ typedef struct s_env void getenvlst(t_env **env, char **en); void free_envlst(t_env **env); char *env_get(t_env *env, char *name); +char **env_to_strlst(t_env *env); diff --git a/include/minishell.h b/include/minishell.h index c84fda3..6282ea6 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ -/* Updated: 2024/10/17 15:58:28 by dkaiser ### ########.fr */ +/* Updated: 2024/10/21 15:07:51 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" #include "get_next_line.h" #include "libft.h" +#include void getenvlst(t_env **env, char **en) { @@ -51,7 +52,7 @@ void free_envlst(t_env **env) } } -char *env_get(t_env *env, char *name) +char *env_get(t_env *env, char *name) { while (env != NULL) { diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c new file mode 100644 index 0000000..3a58df9 --- /dev/null +++ b/src/env_to_strlst.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env_to_strlst.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser next; + } + result = malloc(sizeof(char *) * (size + 1)); + if (result == NULL) + return (NULL); + i = 0; + cur = env; + while (i < size) + { + result[i] = get_var_assign(cur); + cur = cur->next; + i++; + } + result[i] = NULL; + return (result); +} + +static char *get_var_assign(t_env *cur) +{ + char *left_side; + char *result; + + left_side = ft_strjoin(cur->name, "="); + if (left_side == NULL) + return (NULL); + result = ft_strjoin(left_side, cur->value); + free(left_side); + return (result); +} diff --git a/src/execute_cmd.c b/src/execute_cmd.c new file mode 100644 index 0000000..3730d4b --- /dev/null +++ b/src/execute_cmd.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* execute_cmd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser +#include +#include + +int execute_cmd(t_cmd *cmd, t_env *env) +{ + int result; + pid_t pid; + + pid = fork(); + if (pid < 0) + return (EXIT_FAILURE); + if (pid == 0) + { + result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); + exit(result); + } + else + { + // only wait if cmd is on rightmost side of pipe? + // so in cmd1 | cmd2 | cmd3 | cmd4 only wait for cmd4 + waitpid(pid, &result, 0); + } + return (result); +} diff --git a/src/interpreter.c b/src/interpreter.c index 0fa7ac1..1082644 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,11 +6,12 @@ /* By: dkaiser static int eval_pipe(t_pipe *pipe, t_env *env); static int eval_cmd(t_cmd *cmd, t_env *env); @@ -38,6 +39,13 @@ static int eval_pipe(t_pipe *pipe, t_env *env) static int eval_cmd(t_cmd *cmd, t_env *env) { - printf("%s\n", get_cmd_path(cmd->args[0], env)); + char *cmd_path; + + cmd_path = get_cmd_path(cmd->args[0], env); + if (cmd_path == NULL) + return (1); + free(cmd->args[0]); + cmd->args[0] = cmd_path; + execute_cmd(cmd, env); return (0); } diff --git a/src/main.c b/src/main.c index e87bc99..c3e0ec7 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 17:01:45 by dkaiser ### ########.fr */ +/* Updated: 2024/10/21 15:02:56 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ int main(int argc, char *argv[], char *envp[]) { t_env *env; + env = NULL; if (!argc && !argv) return (1); if (init()) -- cgit v1.2.3 From 0a5684e637cd6ec084575ddf9930cfde55d3cca7 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 22 Oct 2024 15:42:28 +0200 Subject: Add pipe basics TODO: Add actual piping --- src/execute_cmd.c | 21 ++++----------- src/interpreter.c | 79 +++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 3730d4b..33ba11c 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,7 +6,7 @@ /* By: dkaiser args[0], cmd->args, env_to_strlst(env)); - exit(result); - } - else - { - // only wait if cmd is on rightmost side of pipe? - // so in cmd1 | cmd2 | cmd3 | cmd4 only wait for cmd4 - waitpid(pid, &result, 0); - } + cmd_path = get_cmd_path(cmd->args[0], env); + cmd->args[0] = cmd_path; + result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); return (result); } diff --git a/src/interpreter.c b/src/interpreter.c index 1082644..f6757c4 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,46 +6,73 @@ /* By: dkaiser +#include +#include #include +#include -static int eval_pipe(t_pipe *pipe, t_env *env); -static int eval_cmd(t_cmd *cmd, t_env *env); +int eval_rec(t_node *node, t_env *env); int eval(t_node *node, t_env *env) { - if (node->type == PIPE_NODE) - return (eval_pipe(&node->content.pipe, env)); - else if (node->type == CMD_NODE) - return (eval_cmd(&node->content.cmd, env)); + pid_t pid; + int result; + + result = 0; + pid = fork(); + if (pid < 0) + { + return (EXIT_FAILURE); + } + if (pid == 0) + { + result = eval_rec(node, env); + exit(result); + } else { - panic(UNREACHABLE); - return (-1); + waitpid(pid, &result, 0); } + return (result); } -static int eval_pipe(t_pipe *pipe, t_env *env) -{ - dbg("TODO: PIPE"); - eval_cmd(&pipe->left->content.cmd, env); - eval_cmd(&pipe->right->content.cmd, env); - return (0); -} - -static int eval_cmd(t_cmd *cmd, t_env *env) +int eval_rec(t_node *node, t_env *env) { - char *cmd_path; + pid_t pid; + int result; - cmd_path = get_cmd_path(cmd->args[0], env); - if (cmd_path == NULL) - return (1); - free(cmd->args[0]); - cmd->args[0] = cmd_path; - execute_cmd(cmd, env); - return (0); + if (node->type == PIPE_NODE) + { + pid = fork(); + if (pid < 0) + { + return (EXIT_FAILURE); + } + if (pid == 0) + { + result = execute_cmd(&node->content.pipe.left->content.cmd, env); + exit(result); + } + else + { + result = eval(node->content.pipe.right, env); + } + } + else if (node->type == CMD_NODE) + { + result = execute_cmd(&node->content.cmd, env); + } + else + { + panic(UNREACHABLE); + return (EXIT_FAILURE); + } + return (result); } -- cgit v1.2.3 From 07522e059d2956014b8d97f39b72a90c6dc6a7c6 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 25 Oct 2024 12:51:07 +0200 Subject: Finish pipes I guess Somehow this worked at the first try. This'll have to be tested more thoroughly. --- src/interpreter.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/interpreter.c b/src/interpreter.c index f6757c4..e08d289 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: dkaiser #include -int eval_rec(t_node *node, t_env *env); +static int eval_rec(t_node *node, t_env *env, int in_fd); int eval(t_node *node, t_env *env) { @@ -33,7 +33,7 @@ int eval(t_node *node, t_env *env) } if (pid == 0) { - result = eval_rec(node, env); + result = eval_rec(node, env, STDIN_FILENO); exit(result); } else @@ -43,11 +43,15 @@ int eval(t_node *node, t_env *env) return (result); } -int eval_rec(t_node *node, t_env *env) +static int eval_rec(t_node *node, t_env *env, int in_fd) { pid_t pid; int result; + int p[2]; + result = pipe(p); + if (result == -1) + return (EXIT_FAILURE); if (node->type == PIPE_NODE) { pid = fork(); @@ -57,12 +61,17 @@ int eval_rec(t_node *node, t_env *env) } if (pid == 0) { + close(p[0]); + dup2(in_fd, STDIN_FILENO); + dup2(p[1], STDOUT_FILENO); result = execute_cmd(&node->content.pipe.left->content.cmd, env); exit(result); } else { - result = eval(node->content.pipe.right, env); + close(p[1]); + dup2(p[0], STDIN_FILENO); + result = eval_rec(node->content.pipe.right, env, p[0]); } } else if (node->type == CMD_NODE) -- cgit v1.2.3 From 15d8385f8ecf30e1ca74025b12fed7e45349b706 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 25 Oct 2024 13:44:13 +0200 Subject: Handle all redirections except APPEND TODO: Add APPEND handling TODO: Fix permissions --- src/execute_cmd.c | 34 +++++++++++++++++++++++++++++++++- src/interpreter.c | 4 +++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 33ba11c..fa7677f 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,22 +6,54 @@ /* By: dkaiser #include +#include +#include #include +#include int execute_cmd(t_cmd *cmd, t_env *env) { int result; char *cmd_path; + int fd; cmd_path = get_cmd_path(cmd->args[0], env); cmd->args[0] = cmd_path; + + if (cmd->redirs[0].type == INPUT_FILE) + { + fd = open(cmd->redirs[0].specifier, O_RDONLY); + if (fd < 0) + return (EXIT_FAILURE); + dup2(fd, STDIN_FILENO); + } + else if (cmd->redirs[0].type == INPUT_LIMITER) + { + dbg("INPUT_LIMITER"); + } + if (cmd->redirs[1].type == OUTPUT_APPEND) + { + dbg("OUTPUT_APPEND"); + fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND); + if (fd < 0) + return (EXIT_FAILURE); + dup2(fd, STDOUT_FILENO); + } + else if (cmd->redirs[1].type == OUTPUT_OVERRIDE) + { + fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC); + if (fd < 0) + return (EXIT_FAILURE); + dup2(fd, STDOUT_FILENO); + dbg("OUTPUT_OVERRIDE"); + } result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); return (result); } diff --git a/src/interpreter.c b/src/interpreter.c index e08d289..458f2af 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ -/* Updated: 2024/10/17 15:58:28 by dkaiser ### ########.fr */ +/* Updated: 2024/10/25 15:48:56 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ -- cgit v1.2.3 From 44378a79d5c6b742e8dbb03e5025b51a45d0e881 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 15:50:31 +0200 Subject: norm --- include/env.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/env.h b/include/env.h index ee01181..3bf6bd6 100644 --- a/include/env.h +++ b/include/env.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:59:59 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 17:08:01 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,4 +21,4 @@ typedef struct s_env void getenvlst(t_env **env, char **en); void free_envlst(t_env **env); -char *env_get(t_env *env, char *name); +char *env_get(t_env *env, char *name); -- cgit v1.2.3 From cecb3e3560c395f2355a068624a7f6ca3d6b5655 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 15:51:12 +0200 Subject: adjusted header for env --- include/minishell.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/minishell.h b/include/minishell.h index 2319e8b..7b6226e 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* minishell.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:25:58 by dkaiser ### ########.fr */ +/* Updated: 2024/10/22 16:52:26 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ int init(void); int init_signal_handling(void); -void repl(const char *prompt, t_env *env); +void repl(const char *prompt, t_env **env); t_list *parse(t_token *tokens); t_node *parse_cmd(t_token *tokens); @@ -37,5 +37,5 @@ t_redirection *collect_redirs(t_token **tokens); void print_ast(t_node *ast); -int eval(t_node *node, t_env *env); +int eval(t_node *node, t_env **env); #endif -- cgit v1.2.3 From 46003a3dcff9f8cdbc89f0c371785fe3715b0769 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 15:53:59 +0200 Subject: assigned null to en struct against segfault --- src/main.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 64bc312..3672fdd 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:34:02 by dkaiser ### ########.fr */ +/* Updated: 2024/10/25 15:53:10 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,13 @@ int main(int argc, char *argv[], char *envp[]) { - t_env *env; + t_env *env; + + env = NULL; if (!argc && !argv) return (1); if (init()) return (1); getenvlst(&env, envp); - repl("Minishell $ ", env); + repl("Minishell $ ", &env); } -- cgit v1.2.3 From 32cba376c211abe3c96f0d39b4d4eff65c104c0a Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 15:54:49 +0200 Subject: adjusted function prototype --- src/repl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/repl.c b/src/repl.c index 931e25e..9fae849 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,14 +6,14 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:25:48 by dkaiser ### ########.fr */ +/* Updated: 2024/10/22 17:05:14 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "../include/minishell.h" #include "token.h" -void repl(const char *prompt, t_env *env) +void repl(const char *prompt, t_env **env) { char *input; t_token *token_list; -- cgit v1.2.3 From ca4acea03cda19c2a0f0fd168d3c8fd418d71e04 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 15:56:05 +0200 Subject: adjuste input parameter for builtins --- src/interpreter.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/interpreter.c b/src/interpreter.c index abaeb47..13f10ed 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -3,22 +3,23 @@ /* ::: :::::::: */ /* interpreter.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/05 13:15:24 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 16:01:23 by dkaiser ### ########.fr */ +/* Updated: 2024/10/25 15:46:53 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include static int eval_pipe(t_pipe *pipe, t_env *env); -static int eval_cmd(t_cmd *cmd, t_env *env); +static int eval_cmd(t_cmd *cmd, t_env **env); -int eval(t_node *node, t_env *env) +int eval(t_node *node, t_env **env) { if (node->type == PIPE_NODE) - return (eval_pipe(&node->content.pipe, env)); + return (eval_pipe(&node->content.pipe, *env)); else if (node->type == CMD_NODE) return (eval_cmd(&node->content.cmd, env)); else @@ -31,14 +32,24 @@ int eval(t_node *node, t_env *env) static int eval_pipe(t_pipe *pipe, t_env *env) { dbg("TODO: PIPE"); - eval_cmd(&pipe->left->content.cmd, env); - eval_cmd(&pipe->right->content.cmd, env); + eval_cmd(&pipe->left->content.cmd, &env); + eval_cmd(&pipe->right->content.cmd, &env); return (0); } -static int eval_cmd(t_cmd *cmd, t_env *env) +static int eval_cmd(t_cmd *cmd, t_env **env) { - printf("%s\n", cmd->args[0]); - printf("PATH=%s\n", env_get(env, "PATH")); + if (ft_strncmp(cmd->args[0], "pwd", 4) == 0) + pwd(*env); + else if (ft_strncmp(cmd->args[0], "echo", 5) == 0) + echo(cmd->args); + else if (ft_strncmp(cmd->args[0], "env", 4) == 0) + ft_env(*env); + else if (ft_strncmp(cmd->args[0], "cd", 3) == 0) + cd(env, cmd->args); + else if (ft_strncmp(cmd->args[0], "unset", 6) == 0) + unset(cmd->args, env); + else if (ft_strncmp(cmd->args[0], "export", 7) == 0) + export(cmd->args, env); return (0); } -- cgit v1.2.3 From 00ad7429f223c85e99da6ffa8f7dade0c73c97b5 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 20:53:44 +0200 Subject: update for builtins and extra functions --- src/builtins_part_one.c | 180 +++++++++++------------------------------------- src/builtins_part_two.c | 82 ++++++++++++++++++++++ 2 files changed, 124 insertions(+), 138 deletions(-) create mode 100644 src/builtins_part_two.c diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index d817336..6b92d9c 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2024/10/17 14:29:40 by chuhlig ### ########.fr */ +/* Updated: 2024/10/25 20:52:36 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,6 @@ int echo(char **av) return (0); } - int pwd(t_env **env, char *av) { t_env *current; @@ -50,7 +49,7 @@ int pwd(t_env **env, char *av) prev = NULL; while (current) { - if(ft_strcmp(current->name, av == 0)) + if (ft_strcmp(current->name, av == 0)) break ; prev = current; current = current->next; @@ -76,85 +75,12 @@ int env(t_env **env) return (0); } -void update_oldpwd(t_env **env) -{ - t_env *current; - t_env *prev; - char cwd[1028]; - char *tmp; - - while (current) // home command - { - if (ft_strncmp(current->name, "OLDPWD", 6) == 0) - break ; - prev = current; - current = current->next; - } - getcwd(cwd, sizeof(cwd)); - tmp = ft_strdup(cwd); - free(current->value); - current->value = tmp; -} - -void update_pwd(t_env **env) -{ - t_env *current; - t_env *prev; - char cwd[1028]; - char *tmp; - - while (current) - { - if (ft_strncmp(current->name, "PWD", 3) == 0) - break ; - prev = current; - current = current->next; - } - getcwd(cwd, sizeof(cwd)); - tmp = ft_strdup(cwd); - free(current->value); - current->value = tmp; -} - -int cd(t_env **env,char **av) -{ - t_env *current; - t_env *prev; - t_env *pwd; - - current = env; - if(av[1] == NULL) - { - update_oldpwd(&env); - while (current) // home command - { - if (ft_strncmp(current->name, "HOME", 4) == 0) - break ; - prev = current; - current = current->next; - } - if(chdir(current->value) == -1) - return ; - } - else - { - update_oldpwd(&env); - if(chdir(av[1]) == -1) - return ; - update_pwd(&env); - } -} - -// exit - int exit(char *av) { - freenode free toke free sequence stop repl free env - clear history + freenode free toke free sequence stop repl free env; + clear history; } -//export - int export(char **av, t_env **env) { char *tmp; @@ -162,87 +88,65 @@ int export(char **av, t_env **env) int i; i = i; - while(av[i]) + while (av[i]) { - if(tmp = ft_strchr(av[i], '=')) + if (t_strchr(av[i], '=')) { + tmp = ft_strchr(av[i], '='); tmp = '\0'; current = *env; - while(current) + while (current) { - if (strcmp(current->name, av[i]) == 0) + if (ft_strcmp(current->name, tmp[i]) == 0) { - free(current->value); - current->value = ft_strdup(tmp + 1); - break; - } - current = current->next; + free(current->value); + current->value = ft_strdup(tmp + 1); + break ; + } + current = current->next; } if (!current) { - current = malloc(sizeof(t_env)); - current->name = ft_strdup(av[i]); - current->value = ft_strdup(tmp + 1); - current->next = *env; - *env = current; - } - return (0); + current = malloc(sizeof(t_env)); + current->name = ft_strdup(av[i]); + current->value = ft_strdup(tmp + 1); + current->next = *env; + *env = current; + } } i++; } - return 1; + return (1); } -//unset -//for unset as well check and for name part int unset(char **av, t_env **env) { t_env *current; t_env *prev; + int i; + i = 0; current = env; prev = NULL; - while (current) - { - if(ft_strcmp(current->name, av[1] == 0)) - break ; - prev = current; - current = current->next; - } - if(current) + while (av[i]) { - if(prev) - prev->next = current->next; - else - *env = current->next; - free(current->value); - free(current); + while (current) + { + if (ft_strcmp(current->name, av[i] == 0)) + break ; + prev = current; + current = current->next; + } + if (current) + { + if (prev) + prev->next = current->next; + else + *env = current->next; + free(current->value); + free(current); + } + i++; } return (0); -} - - -// void getenvlststr(t_env **env, char **en)//without serparation -// { -// int i; -// size_t t; -// t_env *current; - -// t = 0; -// i = 0; -// while (en[i] != NULL) -// { -// current = *env; -// current = malloc(sizeof(t_env)); -// while(en[t] != '=') -// t++; -// current->name = ft_substr(en[i], 0, t); -// current->value = ft_strdup(en[i]); -// current->next = *env; -// *env = current; -// i++; -// } -// return (0); -// } - -//should name the list different \ No newline at end of file +} \ No newline at end of file diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c new file mode 100644 index 0000000..94ef258 --- /dev/null +++ b/src/builtins_part_two.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtins_part_two.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/25 20:52:16 by chuhlig #+# #+# */ +/* Updated: 2024/10/25 20:52:46 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "env.h" + +void update_oldpwd(t_env **env) +{ + t_env *current; + t_env *prev; + char cwd[1028]; + char *tmp; + + while (current) + { + if (ft_strncmp(current->name, "OLDPWD", 6) == 0) + break ; + prev = current; + current = current->next; + } + getcwd(cwd, sizeof(cwd)); + tmp = ft_strdup(cwd); + free(current->value); + current->value = tmp; +} + +void update_pwd(t_env **env) +{ + t_env *current; + t_env *prev; + char cwd[1028]; + char *tmp; + + while (current) + { + if (ft_strncmp(current->name, "PWD", 3) == 0) + break ; + prev = current; + current = current->next; + } + getcwd(cwd, sizeof(cwd)); + tmp = ft_strdup(cwd); + free(current->value); + current->value = tmp; +} + +int cd(t_env **env, char **av) +{ + t_env *current; + t_env *prev; + t_env *pwd; + + current = env; + if (av[1] == NULL) + { + update_oldpwd(&env); + while (current) + { + if (ft_strncmp(current->name, "HOME", 4) == 0) + break ; + prev = current; + current = current->next; + } + if (chdir(current->value) == -1) + return ; + } + else + { + update_oldpwd(&env); + if (chdir(av[1]) == -1) + return ; + update_pwd(&env); + } +} \ No newline at end of file -- cgit v1.2.3 From 36b693a56d4f0d2841d7323ca7ae8df900c611dc Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 21:00:39 +0200 Subject: restored merge conflict error" " --- src/execute_cmd.c | 21 +++++++++++-- src/interpreter.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/src/execute_cmd.c b/src/execute_cmd.c index fa7677f..6386fc0 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* execute_cmd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/21 13:58:56 by dkaiser #+# #+# */ -/* Updated: 2024/10/25 13:31:16 by dkaiser ### ########.fr */ +/* Updated: 2024/10/25 20:59:22 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,3 +57,20 @@ int execute_cmd(t_cmd *cmd, t_env *env) result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); return (result); } + +static int eval_cmd(t_cmd *cmd, t_env **env) +{ + if (ft_strncmp(cmd->args[0], "pwd", 4) == 0) + pwd(*env); + else if (ft_strncmp(cmd->args[0], "echo", 5) == 0) + echo(cmd->args); + else if (ft_strncmp(cmd->args[0], "env", 4) == 0) + ft_env(*env); + else if (ft_strncmp(cmd->args[0], "cd", 3) == 0) + cd(env, cmd->args); + else if (ft_strncmp(cmd->args[0], "unset", 6) == 0) + unset(cmd->args, env); + else if (ft_strncmp(cmd->args[0], "export", 7) == 0) + export(cmd->args, env); + return (0); +} \ No newline at end of file diff --git a/src/interpreter.c b/src/interpreter.c index e69de29..c6a4a00 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* interpreter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/05 13:15:24 by dkaiser #+# #+# */ +/* Updated: 2024/10/25 20:59:16 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "debug_tools.h" +#include "minishell.h" +#include +#include +#include +#include +#include + +static int eval_rec(t_node *node, t_env *env, int in_fd); + +int eval(t_node *node, t_env *env) +{ + pid_t pid; + int result; + + if (node == NULL) + return (EXIT_FAILURE); + result = 0; + pid = fork(); + if (pid < 0) + { + return (EXIT_FAILURE); + } + if (pid == 0) + { + result = eval_rec(node, env, STDIN_FILENO); + exit(result); + } + else + { + waitpid(pid, &result, 0); + } + return (result); +} + +static int eval_rec(t_node *node, t_env *env, int in_fd) +{ + pid_t pid; + int result; + int p[2]; + + result = pipe(p); + if (result == -1) + return (EXIT_FAILURE); + if (node->type == PIPE_NODE) + { + pid = fork(); + if (pid < 0) + { + return (EXIT_FAILURE); + } + if (pid == 0) + { + close(p[0]); + dup2(in_fd, STDIN_FILENO); + dup2(p[1], STDOUT_FILENO); + result = execute_cmd(&node->content.pipe.left->content.cmd, env); + exit(result); + } + else + { + close(p[1]); + dup2(p[0], STDIN_FILENO); + result = eval_rec(node->content.pipe.right, env, p[0]); + } + } + else if (node->type == CMD_NODE) + { + result = execute_cmd(&node->content.cmd, env); + } + else + { + panic(UNREACHABLE); + return (EXIT_FAILURE); + } + return (result); +} + -- cgit v1.2.3