diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-09-09 18:24:53 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-09-09 18:24:53 +0200 |
| commit | c8c72449733f064f86b8a7c0b1284b6196fff0e3 (patch) | |
| tree | e347b900dd39c7518c37564058494ab8a0a59e23 | |
| parent | 4238485a4e26a9d15541708bfc38bfede9bbe7d2 (diff) | |
| download | minishell-c8c72449733f064f86b8a7c0b1284b6196fff0e3.tar.gz minishell-c8c72449733f064f86b8a7c0b1284b6196fff0e3.tar.bz2 minishell-c8c72449733f064f86b8a7c0b1284b6196fff0e3.zip | |
Re Added cmd variable
| -rw-r--r-- | include/eval.h | 8 | ||||
| -rw-r--r-- | include/lexer.h | 3 | ||||
| -rw-r--r-- | src/env.c | 4 | ||||
| -rw-r--r-- | src/eval/cmd.c | 23 | ||||
| -rw-r--r-- | src/eval/variable.c | 42 | ||||
| -rw-r--r-- | src/lexer/tok_lst.c | 20 | ||||
| -rw-r--r-- | src/main.c | 5 |
7 files changed, 96 insertions, 9 deletions
diff --git a/include/eval.h b/include/eval.h index 9b5d00a..a23b249 100644 --- a/include/eval.h +++ b/include/eval.h @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:30 by charles #+# #+# */ -/* Updated: 2020/08/28 16:56:37 by charles ### ########.fr */ +/* Updated: 2020/09/09 17:27:21 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,4 +74,10 @@ bool exec_is_path(char *exec_name); bool exec_is_valid(char *exec_path); char *exec_search_path(t_path path, char *path_var, char *exec_name); +/* +** variable.c +*/ + +bool variable_extract(t_tok_lst **argv, t_env env, t_env env_local); + #endif diff --git a/include/lexer.h b/include/lexer.h index ba248a8..8d7a81f 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -6,7 +6,7 @@ /* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/19 10:51:26 by nahaddac #+# #+# */ -/* Updated: 2020/08/27 20:55:27 by charles ### ########.fr */ +/* Updated: 2020/09/09 17:50:01 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,6 +61,7 @@ t_tok_lst *tok_lst_push_front(t_tok_lst **tokens, t_tok_lst *pushed); void *tok_lst_destroy(t_tok_lst **tokens, void (*del)(void*)); t_tok_lst *tok_lst_last(t_tok_lst *tokens); t_tok_lst *tok_lst_uncons(t_tok_lst **tokens); +t_tok_lst *tok_lst_take_sticked(t_tok_lst **tokens); /* ** lexer.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */ -/* Updated: 2020/08/19 10:23:55 by charles ### ########.fr */ +/* Updated: 2020/09/09 16:56:39 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -105,7 +105,7 @@ char *env_search_first_match(t_env env, const char *haystack) while (ft_isalnum(haystack[len]) || haystack[len] == '_') len++; if (haystack[0] == '?') - return (ft_itoa(g_last_status_code)); // FIXME leak + return (ft_itoa(g_last_status_code)); // FIXME leak (static buffer) if (len == 0) return (NULL); i = -1; diff --git a/src/eval/cmd.c b/src/eval/cmd.c index ccb4cc2..5130a45 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/08/28 16:54:48 by charles ### ########.fr */ +/* Updated: 2020/09/09 17:40:54 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -94,9 +94,30 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) char **argv; if (!redir_extract(&ast->redirs, env, fds)) + { + ast->redirs = NULL; return (-1); + } + ast->redirs = NULL; if ((param.env_local = env_from_array((char*[]){NULL})) == NULL) return (-1); + variable_extract(&ast->cmd_argv, env, param.env_local); + + /* char **strs = preprocess(&start, env); */ + /* */ + /* if (env_export(env_local, id, strs[0]) == NULL) */ + /* return (-1); */ + /* if (ast->cmd_argv == NULL) // FIXME special env not passed to child processes */ + /* { */ + /* ft_vecpop(param.env_local, NULL); */ + /* if (ft_vecswallow_at(env, env->size - 1, param.env_local) == NULL) */ + /* { */ + /* ft_vecdestroy(param.env_local, free); */ + /* return (-1); */ + /* } */ + /* g_last_status_code = 0; */ + /* return (0); */ + /* } */ if ((argv = preprocess(&ast->cmd_argv, env)) == NULL) { diff --git a/src/eval/variable.c b/src/eval/variable.c new file mode 100644 index 0000000..2b6d7cf --- /dev/null +++ b/src/eval/variable.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* variable.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/09 17:12:14 by charles #+# #+# */ +/* Updated: 2020/09/09 18:22:43 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "eval.h" + +bool variable_extract(t_tok_lst **argv, t_env env, t_env env_local) +{ + char *key; + t_tok_lst *value_tokens; + char **strs; + + if (*argv == NULL || !((*argv)->tag & TAG_STR) + || !utils_start_with_valid_identifier((*argv)->content)) + return (true); + + key = (*argv)->content; + (*argv)->content = ft_strchr(key, '='); + *(*argv)->content = '\0'; + (*argv)->content++; + (*argv)->content = ft_strdup((*argv)->content); + + /* printf("|%s| |%s|\n", key, (*argv)->content); */ + /* if (*(*argv)->content == '\0') */ + /* { */ + /* ft_lstpop_front((t_ftlst**)argv, NULL); */ + /* return (true); */ + /* } */ + value_tokens = tok_lst_take_sticked(argv); + strs = preprocess(&value_tokens, env); + if (env_export(env_local, key, strs[0]) == NULL) + return (false); + return (variable_extract(argv, env, env_local)); +} diff --git a/src/lexer/tok_lst.c b/src/lexer/tok_lst.c index a746794..83f50bf 100644 --- a/src/lexer/tok_lst.c +++ b/src/lexer/tok_lst.c @@ -6,7 +6,7 @@ /* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/08/27 09:32:58 by charles #+# #+# */ -/* Updated: 2020/08/27 20:54:35 by charles ### ########.fr */ +/* Updated: 2020/09/09 18:10:26 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,3 +70,21 @@ t_tok_lst *tok_lst_uncons(t_tok_lst **tokens) poped->next = NULL; return (poped); } + +t_tok_lst *tok_lst_take_sticked(t_tok_lst **tokens) +{ + t_tok_lst *start; + t_tok_lst *curr; + + if (*tokens == NULL) + return (NULL); + start = *tokens; + curr = *tokens; + while (curr->tag & TAG_STICK && curr->tag & TAG_IS_STR) + curr = curr->next; + /* if (curr->tag & TAG_IS_STR) */ + /* curr = curr->next; */ + *tokens = curr->next; + curr->next = NULL; + return (start); +} @@ -6,7 +6,7 @@ /* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/09/09 14:20:41 by charles ### ########.fr */ +/* Updated: 2020/09/09 16:19:00 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,6 @@ void tok_lst_debug(t_tok_lst *tokens); ** $? ** concurrent pipeline ** cmd variable preprocess -** PATH with no permission, link and other file system fun stuff ** signal on whole line instead of single command ** env local to current minishell process */ @@ -115,7 +114,7 @@ int main(int argc, char **argv, char **envp) char *line; print_prompt(); - while ((ret = ft_getline(STDOUT_FILENO, &line)) == FTGL_OK) + while ((ret = ft_getline(STDIN_FILENO, &line)) == FTGL_OK) { if (*line == '\0') { |
