aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-07-17 14:09:10 +0200
committerCharles <sircharlesaze@gmail.com>2020-07-18 08:57:19 +0200
commitbd8f652de51395fb26659f7a634e55bd46917b2e (patch)
treeb8e0d746e180ebfe716815540dc553a0487c8116 /src
parent134e5ca4fe0a1051fb7946874953608894959a13 (diff)
downloadminishell-bd8f652de51395fb26659f7a634e55bd46917b2e.tar.gz
minishell-bd8f652de51395fb26659f7a634e55bd46917b2e.tar.bz2
minishell-bd8f652de51395fb26659f7a634e55bd46917b2e.zip
Fixing exit error message and overflow detection
Diffstat (limited to 'src')
-rw-r--r--src/builtin/builtin.c16
-rw-r--r--src/builtin/exit.c27
-rw-r--r--src/eval/cmd.c6
-rw-r--r--src/lexer/lexer.c4
-rw-r--r--src/main.c14
-rw-r--r--src/path.c4
-rw-r--r--src/preprocess.c4
7 files changed, 44 insertions, 31 deletions
diff --git a/src/builtin/builtin.c b/src/builtin/builtin.c
index 9c5fe9e..08abcd8 100644
--- a/src/builtin/builtin.c
+++ b/src/builtin/builtin.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:11:01 by charles #+# #+# */
-/* Updated: 2020/07/14 11:06:37 by charles ### ########.fr */
+/* Updated: 2020/07/17 11:16:57 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,13 +22,13 @@
*/
static t_builtin_entry g_builtin_lookup[] = {
- {"echo", builtin_echo},
- {"cd", builtin_cd},
- {"pwd", builtin_pwd},
- {"export", builtin_export},
- {"unset", builtin_unset},
- {"env", builtin_env},
- {"exit", builtin_exit},
+ {"echo", builtin_echo, true},
+ {"cd", builtin_cd, false},
+ {"pwd", builtin_pwd, true},
+ {"export", builtin_export, false},
+ {"unset", builtin_unset, false},
+ {"env", builtin_env, false},
+ {"exit", builtin_exit, false},
};
t_builtin_entry *builtin_search_func(char *name)
diff --git a/src/builtin/exit.c b/src/builtin/exit.c
index 15271f6..860325e 100644
--- a/src/builtin/exit.c
+++ b/src/builtin/exit.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:10:16 by charles #+# #+# */
-/* Updated: 2020/07/13 15:35:44 by charles ### ########.fr */
+/* Updated: 2020/07/17 16:36:03 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,27 +19,32 @@
int builtin_exit(char **argv, t_env env)
{
- int status;
+ long status;
+ char *after;
(void)env;
if (argv[1] == NULL)
status = g_last_status_code;
- else if (argv[2] != NULL)
- {
- // replace with minishell error system
- ft_putendl_fd("minishell: exit: too many arguments", STDERR_FILENO);
- return 1;
- }
else
{
errno = 0;
- status = ft_atoi_strict(argv[1]);
- if (errno != 0)
+ status = ft_strtol(argv[1], &after, 10);
+ while (ft_isblank(*after))
+ after++;
+ if (*after != '\0' || errno == ERANGE)
{
// replace with minishell error system
- ft_putendl_fd("minishell: exit: ---argv[1]---: numeric argument required", STDERR_FILENO);
+ ft_putstr_fd("minishell: exit: ", STDERR_FILENO);
+ ft_putstr_fd(argv[1], STDERR_FILENO);
+ ft_putstr_fd(": numeric argument required\n", STDERR_FILENO);
return 2;
}
+ if (argv[2] != NULL)
+ {
+ // replace with minishell error system
+ ft_putendl_fd("minishell: exit: too many arguments", STDERR_FILENO);
+ return 1;
+ }
}
exit(status % 256);
return (0);
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index deb7df7..1953325 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/16 09:12:02 by charles ### ########.fr */
+/* Updated: 2020/07/17 11:43:59 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -88,7 +88,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
return (-1);
ft_lstpop_front(&ast->cmd_argv, (void (*)(void*))token_destroy);
}
- if (ast->cmd_argv == NULL)
+ 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)
@@ -123,7 +123,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
return (-1); // return error status
}
}
- else// solved with pipeline
+ else if (!param.builtin->forked)
{
g_last_status_code = param.builtin->func(argv, env);
return (g_last_status_code);
diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c
index 32b9eeb..a6a2755 100644
--- a/src/lexer/lexer.c
+++ b/src/lexer/lexer.c
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/16 08:18:25 by nahaddac #+# #+# */
-/* Updated: 2020/07/17 13:06:17 by nahaddac ### ########.fr */
+/* Updated: 2020/07/17 13:08:08 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,7 +24,7 @@ int len_is_not_sep(char *input)
i +=2;
if (input[i] == '\\')
i += len_is_not_sep(&input[i]);
-
+
}
if (lexer_sep(input[i]))
return(i);
diff --git a/src/main.c b/src/main.c
index fc55e3e..9687de4 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/16 09:19:58 by charles ### ########.fr */
+/* Updated: 2020/07/17 13:39:05 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,12 +29,15 @@ void ast_print(int level, t_ast *ast);
/*
** TODO
** $?
-** syntax error
-** signal
** pipeline
-** cmd variable
-** interpolation order
+** cmd variable preprocess
** PATH with no permission, link and other file system fun stuff
+** escape lexer
+** escape split preprocessing
+** signal on whole line instead of single command
+** parsing error
+** env local to current minishell process
+** exit
*/
char *g_basename = "minishell";
@@ -47,6 +50,7 @@ int main(int argc, char **argv, char **envp)
env = env_from_array(envp);
path = path_update(NULL, env_search(env, "PATH"));
+ g_last_status_code = 0;
signal(SIGINT, signal_sigint);
signal(SIGQUIT, signal_sigquit);
signal(SIGTERM, signal_sigterm);
diff --git a/src/path.c b/src/path.c
index 579fd81..1234998 100644
--- a/src/path.c
+++ b/src/path.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/27 15:51:01 by cacharle #+# #+# */
-/* Updated: 2020/07/15 12:11:48 by charles ### ########.fr */
+/* Updated: 2020/07/17 10:48:31 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -52,6 +52,8 @@ static int st_add_file(char *dirname, struct dirent *entry, void *path)
** \return The updated/created path hash table or NULL on error
*/
+// TODO check nullstring path == current directory
+// i.e ./ not needed before executable
t_path path_update(t_path path, char *path_var)
{
int i;
diff --git a/src/preprocess.c b/src/preprocess.c
index 13f297c..44558d4 100644
--- a/src/preprocess.c
+++ b/src/preprocess.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/03 08:58:49 by charles #+# #+# */
-/* Updated: 2020/07/13 11:08:18 by charles ### ########.fr */
+/* Updated: 2020/07/17 11:20:26 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -36,6 +36,8 @@ static char *st_iterpolate_env(char *str, enum e_token_tag tag, t_env env)
while (++i < dstr->length)
if (dstr->str[i] == '\\' && st_escapable(dstr->str[i + 1], tag))
ft_dstrerase(dstr, i, 1);
+ else if (dstr->str[i] == '$' && !ft_isalnum(dstr->str[i + 1]) && dstr->str[i + 1] != '_') // $ alone
+ continue;
else if (dstr->str[i] == '$')
{
if ((match = env_search_first_match(env, dstr->str + i + 1)) == NULL)