aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/minishell.h4
-rw-r--r--src/env.c6
-rw-r--r--src/eval/cmd.c9
-rw-r--r--src/eval/redir.c46
-rw-r--r--src/lexer/trim.c9
-rw-r--r--src/lexer/utils.c6
-rw-r--r--src/preprocess.c17
7 files changed, 50 insertions, 47 deletions
diff --git a/include/minishell.h b/include/minishell.h
index 7e755a3..95f299b 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/09/09 15:42:03 by charles ### ########.fr */
+/* Updated: 2020/09/14 15:36:04 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -111,7 +111,7 @@ int builtin_exit(char **argv, t_env env);
*/
char **preprocess(t_tok_lst **tokens, t_env env);
-char *preprocess_filename(t_tok_lst **tokens, t_env env);
+int preprocess_filename(t_tok_lst **tokens, t_env env, char **filename);
/*
** signal.c
diff --git a/src/env.c b/src/env.c
index 19ead42..42339c8 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/09/10 14:19:42 by charles ### ########.fr */
+/* Updated: 2020/09/14 16:00:15 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -91,6 +91,8 @@ int env_search_index(t_env env, char *key)
return (-1);
}
+static char g_status_buf[64] = {'\0'};
+
char *env_search_first_match(t_env env, const char *haystack)
{
size_t len;
@@ -105,7 +107,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)); // FIXME leak (static buffer)
+ return (ft_itoa_cpy(g_status_buf, g_last_status));
if (len == 0)
return (NULL);
i = -1;
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index 223725c..ff2c904 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/09/13 21:00:38 by charles ### ########.fr */
+/* Updated: 2020/09/14 15:50:15 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,8 +15,6 @@
pid_t g_child_pid = -1;
int g_last_status = 0;
-void token_debug(void *v);
-
int wrapped_cmd(void *void_param)
{
t_fork_param_cmd *param;
@@ -59,13 +57,8 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid)
if ((status = redir_extract(&ast->redirs, env, fds)) != 0)
return (status);
-
if ((argv = preprocess(&ast->cmd_argv, env)) == NULL)
- {
- ast->cmd_argv = NULL;
return (EVAL_FATAL);
- }
-
if (argv[0] == NULL)
return (0);
param.builtin = builtin_search_func(argv[0]);
diff --git a/src/eval/redir.c b/src/eval/redir.c
index 39e202d..6c3e45c 100644
--- a/src/eval/redir.c
+++ b/src/eval/redir.c
@@ -6,13 +6,13 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/15 11:05:34 by charles #+# #+# */
-/* Updated: 2020/09/10 20:25:59 by charles ### ########.fr */
+/* Updated: 2020/09/14 15:41:03 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "eval.h"
-static int st_open_replace(int *fd, char *filename, int oflag)
+static int st_open_replace(char *filename, int *fd, int oflag)
{
if (*fd != FD_NONE)
close(*fd);
@@ -29,14 +29,35 @@ static int st_open_replace(int *fd, char *filename, int oflag)
return (0);
}
-int redir_extract(
- t_tok_lst **redirs,
- t_env env,
- int fds[2])
+static int st_open_replace_dispatch(char *filename, int fds[2], enum e_tok tag)
+{
+ int *fd;
+ int oflag;
+
+ if (tag == TAG_REDIR_IN)
+ {
+ fd = &fds[FD_READ];
+ oflag = O_RDONLY;
+ }
+ else if (tag == TAG_REDIR_OUT)
+ {
+ fd = &fds[FD_WRITE];
+ oflag = O_WRONLY | O_CREAT | O_TRUNC;
+ }
+ else if (tag == TAG_REDIR_APPEND)
+ {
+ fd = &fds[FD_WRITE];
+ oflag = O_WRONLY | O_CREAT | O_APPEND;
+ }
+ return (st_open_replace(filename, fd, oflag));
+}
+
+int redir_extract(t_tok_lst **redirs, t_env env, int fds[2])
{
t_tok_lst *after;
t_tok_lst *curr;
char *filename;
+ int status;
if (*redirs == NULL)
return (0);
@@ -54,22 +75,17 @@ int redir_extract(
}
curr = curr->next;
}
- if ((filename = preprocess_filename(&(*redirs)->next, env)) == NULL)
+ if ((status = preprocess_filename(&(*redirs)->next, env, &filename)))
{
tok_lst_destroy(redirs, free);
tok_lst_destroy(&after, free);
- return (EVAL_FATAL);
+ return (status);
}
- if (((*redirs)->tag == TAG_REDIR_IN
- && st_open_replace(&fds[FD_READ], filename, O_RDONLY) != 0)
- || ((*redirs)->tag == TAG_REDIR_OUT
- && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC) != 0)
- || ((*redirs)->tag == TAG_REDIR_APPEND
- && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND) != 0))
+ if ((status = st_open_replace_dispatch(filename, fds, (*redirs)->tag)) != 0)
{
tok_lst_destroy(redirs, free);
tok_lst_destroy(&after, free);
- return (EVAL_FATAL);
+ return (status);
}
tok_lst_destroy(redirs, free);
free(filename);
diff --git a/src/lexer/trim.c b/src/lexer/trim.c
index 991cecc..24eb99f 100644
--- a/src/lexer/trim.c
+++ b/src/lexer/trim.c
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/16 08:18:36 by nahaddac #+# #+# */
-/* Updated: 2020/09/14 15:40:21 by nahaddac ### ########.fr */
+/* Updated: 2020/09/14 16:23:35 by nahaddac ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,12 +43,7 @@ int del_quote(char *str)
{
while (str[i++] != '\0')
{
- if (str[i] == '\\' && str[i + 1] == '\'')
- {
- i++;
- break;
- }
- else if (str[i] == '\\')
+ if (str[i] == '\\')
i += 2;
if (str[i] == '\'')
{
diff --git a/src/lexer/utils.c b/src/lexer/utils.c
index d440de4..b4846d6 100644
--- a/src/lexer/utils.c
+++ b/src/lexer/utils.c
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/16 08:18:15 by nahaddac #+# #+# */
-/* Updated: 2020/09/14 11:38:18 by nahaddac ### ########.fr */
+/* Updated: 2020/09/14 15:16:35 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -97,8 +97,8 @@ int quote_len(char *input, int i)
i++;
while (input[i] != quote_type && input[i] != '\0')
{
- if(input[i] == '\\')
- i+=2;
+ if (quote_type == '"' && input[i] == '\\')
+ i++;
i++;
}
while (ft_isblank(input[i + 1]))
diff --git a/src/preprocess.c b/src/preprocess.c
index a07e429..2af9b7e 100644
--- a/src/preprocess.c
+++ b/src/preprocess.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/03 08:58:49 by charles #+# #+# */
-/* Updated: 2020/09/09 13:15:16 by charles ### ########.fr */
+/* Updated: 2020/09/14 15:42:18 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -176,7 +176,7 @@ char **preprocess(t_tok_lst **tokens, t_env env)
while ((str = curr->content) != NULL && str[++i] != '\0')
{
if (escape(str + i, curr->tag))
- continue;
+ continue ;
if (str[i] == '$')
i = interpolate(str, i, &curr, prev_tag, env) - 1;
}
@@ -188,26 +188,23 @@ char **preprocess(t_tok_lst **tokens, t_env env)
return (st_tokens_to_argv(*tokens));
}
-// TODO malloc error vs shell error
-char *preprocess_filename(t_tok_lst **tokens, t_env env)
+int preprocess_filename(t_tok_lst **tokens, t_env env, char **filename)
{
char **strs;
- char *ret;
if ((strs = preprocess(tokens, env)) == NULL
|| strs[0] == NULL)
{
free(strs);
- return (NULL);
+ return (EVAL_FATAL);
}
if (strs[1] != NULL)
{
- /* save tokens */
errorf("%s: ambiguous redidrect\n", strs[1]);
ft_split_destroy(strs);
- return (NULL);
+ return (1);
}
- ret = strs[0];
+ *filename = ft_strdup(strs[0]);
free(strs);
- return (ft_strdup(ret));
+ return (*filename == NULL ? EVAL_FATAL : 0);
}