aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-07-15 18:33:37 +0200
committerCharles <sircharlesaze@gmail.com>2020-07-15 18:33:37 +0200
commitbcc627a31a6652550be179d1602aae9ccd8db596 (patch)
treefe574ea5f0c693e2898e1f517a3b98e0d730fe4c
parent9c9e5ac17efca1cc22fd8cf69fb75a1e4701efe7 (diff)
downloadminishell-bcc627a31a6652550be179d1602aae9ccd8db596.tar.gz
minishell-bcc627a31a6652550be179d1602aae9ccd8db596.tar.bz2
minishell-bcc627a31a6652550be179d1602aae9ccd8db596.zip
Added cmd local/exported environment variable (WIP, still a few edge cases)
-rw-r--r--include/eval.h3
-rw-r--r--include/minishell.h3
m---------libft0
m---------minishell_test0
-rw-r--r--src/env.c2
-rw-r--r--src/eval/cmd.c41
-rw-r--r--src/main.c3
-rw-r--r--src/parse/parse_error.c6
-rw-r--r--src/utils.c16
9 files changed, 63 insertions, 11 deletions
diff --git a/include/eval.h b/include/eval.h
index 261e1a2..88142cd 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/07/14 09:35:17 by charles ### ########.fr */
+/* Updated: 2020/07/15 17:03:05 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,6 +35,7 @@ typedef struct
char *exec_path;
char **argv;
t_env env;
+ t_env env_local;
t_builtin_entry *builtin;
} t_fork_param_cmd;
diff --git a/include/minishell.h b/include/minishell.h
index de6e923..d2bc9e7 100644
--- a/include/minishell.h
+++ b/include/minishell.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */
-/* Updated: 2020/07/15 13:11:38 by charles ### ########.fr */
+/* Updated: 2020/07/15 17:00:03 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -151,5 +151,6 @@ int utils_directory_iter(
size_t utils_var_end(char *name);
bool utils_valid_identifier(char *name);
+bool utils_start_with_valid_identifier(char *name);
#endif
diff --git a/libft b/libft
-Subproject 13cf7ba8ba259c4c68c32021d4b96f7bf8de068
+Subproject 0fc1e473e3bae0e1df32228ff72b9ab654fd2ac
diff --git a/minishell_test b/minishell_test
-Subproject 9132220296cdf6ab29c570fe0534649cfcc1cd8
+Subproject 8a97914ac822703362c22bb6228905f3fbe13d9
diff --git a/src/env.c b/src/env.c
index 1ed5162..9620782 100644
--- a/src/env.c
+++ b/src/env.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */
-/* Updated: 2020/07/15 13:02:04 by charles ### ########.fr */
+/* Updated: 2020/07/15 17:30:53 by charles ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index 196d810..93a5b23 100644
--- a/src/eval/cmd.c
+++ b/src/eval/cmd.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 10:41:31 by charles #+# #+# */
-/* Updated: 2020/07/15 13:03:20 by charles ### ########.fr */
+/* Updated: 2020/07/15 18:16:27 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -54,6 +54,11 @@ int forked_cmd(void *void_param)
t_fork_param_cmd *param;
param = void_param;
+ if (ft_vecswallow_at(param->env, param->env->size - 1, param->env_local) == NULL)
+ {
+ ft_vecdestroy(param->env_local, free);
+ return (-1);
+ }
if (param->builtin != NULL)
return (param->builtin->func(param->argv, param->env));
else
@@ -71,12 +76,38 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
return (-1);
}
ast->redirs = NULL;
+ if ((param.env_local = env_from_array((char*[]){NULL})) == NULL)
+ return (-1);
+ while (ast->cmd_argv != NULL
+ && ((t_token*)ast->cmd_argv->data)->tag & TAG_IS_STR
+ && utils_start_with_valid_identifier(((t_token*)ast->cmd_argv->data)->content))
+ {
+ if (ft_vecpush(param.env_local, ((t_token*)ast->cmd_argv->data)->content) == NULL)
+ return (-1);
+ ft_lstpop_front(&ast->cmd_argv, NULL);
+ }
+ /* printf("[\n"); */
+ /* ft_veciter(param.env_local, ft_putendl); */
+ /* printf("]\n"); */
+ if (ast->cmd_argv == NULL)
+ {
+ /* printf("--\n"); */
+ if (ft_vecswallow_at(env, env->size - 1, param.env_local) == NULL)
+ {
+ /* printf("hyo\n"); */
+ ft_vecdestroy(param.env_local, free);
+ return (-1);
+ }
+ g_last_status_code = 0;
+ return (0);
+ }
if ((argv = preprocess(&ast->cmd_argv, env)) == NULL)
{
ast->cmd_argv = NULL;
return (-1);
}
+ /* puts(*argv); */
// can have no command (e.g `< file`)
if (argv[0] == NULL)
@@ -84,21 +115,27 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
param.builtin = builtin_search_func(argv[0]);
if (param.builtin == NULL)
{
+ // check env local for PATH
param.exec_path = exec_search_path(path, env_search(env, "PATH"), argv[0]);
if (param.exec_path == NULL)
{
+ g_last_status_code = 127;
error_eval_put(ERROR_CMD_NOT_FOUND, argv[0]);
ft_split_destroy(argv);
return (-1); // return error status
}
}
else// solved with pipeline
- return (param.builtin->func(argv, env));
+ {
+ g_last_status_code = param.builtin->func(argv, env);
+ return (g_last_status_code);
+ }
param.argv = argv;
param.env = env;
int ret = fork_wrap(fds, &param, &forked_cmd);
g_last_status_code = ret;
ft_split_destroy(argv);
+ ft_vecdestroy(param.env_local, free);
return (ret);
}
diff --git a/src/main.c b/src/main.c
index 8b6667c..9f4dd28 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/07/14 11:34:18 by charles ### ########.fr */
+/* Updated: 2020/07/15 15:30:40 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -68,7 +68,6 @@ void signal_sigterm(int signum)
** syntax error
** signal
** pipeline
-** cmd are path
** cmd variable
** interpolation order
** PATH with no permission, link and other file system fun stuff
diff --git a/src/parse/parse_error.c b/src/parse/parse_error.c
index 1116a88..6df86e2 100644
--- a/src/parse/parse_error.c
+++ b/src/parse/parse_error.c
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/18 15:09:48 by nahaddac #+# #+# */
-/* Updated: 2020/07/14 11:49:48 by nahaddac ### ########.fr */
+/* Updated: 2020/07/15 18:31:51 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -51,10 +51,10 @@ t_token *error_syntax_simple(t_ftlst *in)
{
t_token *tk;
size_t i;
- t_ftlst *first;
+ /* t_ftlst *first; */
tk = in->data;
- first = in;
+ /* first = in; */
if(tk->tag & TAG_IS_SEP || (tk->tag & TAG_IS_REDIR))
{
if (out_error_first(tk))
diff --git a/src/utils.c b/src/utils.c
index b7d4404..c4fbb19 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -6,7 +6,7 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */
-/* Updated: 2020/07/15 12:54:28 by charles ### ########.fr */
+/* Updated: 2020/07/15 17:57:09 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -76,3 +76,17 @@ bool utils_valid_identifier(char *name)
}
return (true);
}
+
+bool utils_start_with_valid_identifier(char *name)
+{
+ char *equal_ptr;
+ bool ret;
+
+ equal_ptr = ft_strchr(name, '=');
+ if (equal_ptr == NULL)
+ return (false);
+ *equal_ptr = '\0';
+ ret = utils_valid_identifier(name);
+ *equal_ptr = '=';
+ return (ret);
+}