aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-16 16:19:22 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-16 16:19:22 +0200
commite439b71d807529734f04ce9d78b98c12022e7c72 (patch)
tree59125ee91455ac607aa064965073c0eed20b3573 /src
parent0a8f2cf987a55d6e1c4b210f0f99a1a1e4e4460a (diff)
downloadminishell-e439b71d807529734f04ce9d78b98c12022e7c72.tar.gz
minishell-e439b71d807529734f04ce9d78b98c12022e7c72.tar.bz2
minishell-e439b71d807529734f04ce9d78b98c12022e7c72.zip
Refactoring main, all setup code in setup.c, disabled -c and -l flags if not tested
Diffstat (limited to 'src')
-rw-r--r--src/debug.c28
-rw-r--r--src/error.c4
-rw-r--r--src/eval/redir.c6
-rw-r--r--src/main.c192
-rw-r--r--src/setup.c83
5 files changed, 185 insertions, 128 deletions
diff --git a/src/debug.c b/src/debug.c
index 8759252..3fe7784 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -1,16 +1,40 @@
-
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* debug.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/09/16 15:58:35 by charles #+# #+# */
+/* Updated: 2020/09/16 16:18:11 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
#include <stdio.h>
+
#include "lexer.h"
-void tok_lst_debug(t_tok_lst *tokens)
+void debug_tok_lst(t_tok_lst *tokens)
{
while (tokens != NULL)
{
+ // FIXME libft for safer correction
printf("[%#06x] |%s|%s\n", tokens->tag, tokens->content, tokens->tag & TAG_STICK ? " STICK" : "");
tokens = tokens->next;
}
}
+int debug_lexer(char *input)
+{
+ int status;
+ t_tok_lst *out;
+
+ status = lexer(input, &out);
+ if (status != 0)
+ return (status);
+ debug_tok_lst(out);
+ return (status);
+}
+
/* void token_debug(void *v) */
/* { */
/* t_token *t; */
diff --git a/src/error.c b/src/error.c
index 7da14d5..b5a422b 100644
--- a/src/error.c
+++ b/src/error.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 11:02:52 by charles #+# #+# */
-/* Updated: 2020/09/14 16:42:01 by charles ### ########.fr */
+/* Updated: 2020/09/16 15:42:42 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,7 +37,7 @@ void verrorf(const char *format, va_list ap)
char *str;
char c;
- ft_putstr_fd(g_basename, STDERR_FILENO);
+ ft_putstr_fd(g_progname, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
while (*format != '\0')
{
diff --git a/src/eval/redir.c b/src/eval/redir.c
index 6c3e45c..0d830e2 100644
--- a/src/eval/redir.c
+++ b/src/eval/redir.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/15 11:05:34 by charles #+# #+# */
-/* Updated: 2020/09/14 15:41:03 by charles ### ########.fr */
+/* Updated: 2020/09/16 16:17:09 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,8 @@
static int st_open_replace(char *filename, int *fd, int oflag)
{
+ if (fd == NULL)
+ return (EVAL_FATAL);
if (*fd != FD_NONE)
close(*fd);
if (oflag & O_CREAT)
@@ -34,6 +36,8 @@ static int st_open_replace_dispatch(char *filename, int fds[2], enum e_tok tag)
int *fd;
int oflag;
+ fd = NULL;
+ oflag = 0;
if (tag == TAG_REDIR_IN)
{
fd = &fds[FD_READ];
diff --git a/src/main.c b/src/main.c
index 9ce9bfe..b99bb97 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/09/15 20:34:09 by charles ### ########.fr */
+/* Updated: 2020/09/16 16:14:21 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,11 +20,7 @@
#include "parser.h"
#include "eval.h"
-/* void token_debug(void *v); */
-/* void token_put(void *v); */
-/* void print_level(int level); */
-/* void ast_print(int level, t_ast *ast); */
-void tok_lst_debug(t_tok_lst *tokens);
+int debug_lexer(char *input);
/*
** TODO
@@ -33,139 +29,89 @@ void tok_lst_debug(t_tok_lst *tokens);
** path tricks
*/
-bool env_set_default(t_env env, char *key, char *value)
+char *g_progname = "minishell";
+
+int execute(t_env env, char *input)
{
- if (env_search(env, key, NULL) != NULL)
- return (true);
- return (env_export(env, key, value) != NULL);
+ t_tok_lst *lexer_out;
+ int status;
+ t_parsed *parser_out;
+ int fds[2];
+
+ status = lexer(input, &lexer_out);
+ if (status != 0)
+ return (status);
+ parser_out = parse(lexer_out);
+ if (parser_out == NULL || parser_out->syntax_error)
+ return (1);
+ fds[0] = FD_NONE;
+ fds[1] = FD_NONE;
+ status = eval(fds, env, parser_out->ast, NULL, FD_NONE);
+ if (status == EVAL_FATAL)
+ exit(1);
+ g_last_status = status;
+ return (status);
}
-char *g_basename = "minishell";
-
-int main(int argc, char **argv, char **envp)
+int repl(t_env env)
{
- t_env env;
-
- env = env_from_array(envp);
-
- char buf[PATH_MAX] = {0};
- if (!(getcwd(buf, PATH_MAX)))
- return(1);
+ int ret;
+ char *line;
- char *shlvl_str = env_search(env, "SHLVL", NULL);
- if (shlvl_str != NULL)
+ print_prompt();
+ while ((ret = ft_getline(STDIN_FILENO, &line)) == FTGL_OK)
{
- shlvl_str = ft_itoa(ft_atoi(shlvl_str));
- env_export(env, "SHLVL", shlvl_str);
- free(shlvl_str);
+ if (*line == '\0')
+ {
+ print_prompt();
+ continue ;
+ }
+ if (execute(env, line) == EVAL_FATAL)
+ return (1);
+ print_prompt();
}
-
- if (!env_set_default(env, "PWD", buf) ||
- !env_set_default(env, "SHLVL", "0") ||
- !env_set_default(env, "PATH", "/sbin:"))
+ if (ret != FTGL_EOF)
return (1);
+ return (0);
+}
- /* char *path_str = env_search(env, "PATH"); */
- /* if (ft_strstr(path_str, "/sbin") == NULL) */
- /* { */
- /* char *value = ft_strjoin("/sbin:", path_str); */
- /* env_export(env, "PATH", value); */
- /* free(value); */
- /* } */
-
- char env_exec_path[PATH_MAX + 1];
- if (path_search(env, "env", env_exec_path, false) != 0)
- {
- ft_strcpy(env_exec_path, "/sbin/env");
- /* errorf("env: command not found\n"); */
- /* return (127); */
- }
- env_export(env, "_", env_exec_path);
+#ifndef MINISHELL_TEST
- g_last_status = 0;
- signal(SIGINT, signal_sigint);
- signal(SIGQUIT, signal_sigquit);
- signal(SIGTERM, signal_sigterm);
-
- char *last_slash = ft_strrchr(argv[0], '/');
- if (last_slash == NULL)
- g_basename = argv[0];
- else
- g_basename = last_slash + 1;
+int main(int argc, char **argv, char **envp)
+{
+ t_env env;
- if (argc == 3 && ft_strcmp(argv[1], "-l") == 0)
+ if (argc != 1)
{
- t_tok_lst *lex_out;
- int status = lexer(argv[2], &lex_out);
- if (status != 0)
- return (status);
- tok_lst_debug(lex_out);
- /* ft_lstiter((t_ftlst*)lex_out, token_debug); */
+ ft_putendl_fd("minishell doesn't accept any arguments", STDERR_FILENO);
+ return (1);
}
- else if (argc == 3 && ft_strcmp(argv[1], "-c") == 0) // put in MINISHELL_TEST
- {
- t_tok_lst *lex_out;
- int status = lexer(argv[2], &lex_out);
- if (status != 0)
- return (status);
-
- t_parsed *parser_out = parse(lex_out);
- if (parser_out == NULL || parser_out->syntax_error)
- return (1);
+ if ((env = env_from_array(envp)) == NULL)
+ return (1);
+ setup(argv[0], env);
+ g_last_status = 0;
+ repl(env);
+ ft_vecdestroy(env, free);
+ return (g_last_status);
+}
- /* ast_print(0, parser_out->ast); */
-
- /* printf("===cmd_argv===\n"); */
- /* ft_lstiter(parser_out->ast->cmd_argv, token_debug); */
- /* printf("===redirs===\n"); */
- /* ft_lstiter(parser_out->ast->redirs, token_debug); */
- int fds[2] = {FD_NONE, FD_NONE};
- status = eval(fds, env, parser_out->ast, NULL, FD_NONE);
- if (status == EVAL_FATAL)
- exit(1);
- g_last_status = status;
- exit(status);
- return (0);
- }
- else
- {
- int ret;
- char *line;
- int status;
+#else
- print_prompt();
- while ((ret = ft_getline(STDIN_FILENO, &line)) == FTGL_OK)
- {
- if (*line == '\0')
- {
- print_prompt();
- continue;
- }
- t_tok_lst *lex_out;
- status = lexer(line, &lex_out);
- if (status != 0)
- return (status);
-
- t_parsed *parser_out = parse(lex_out);
- if (parser_out == NULL)
- return (1);
- if (parser_out->syntax_error)
- {
- print_prompt();
- continue;
- }
-
- int fds[2] = {FD_NONE, FD_NONE};
- int status = eval(fds, env, parser_out->ast, NULL, FD_NONE);
- if (status == EVAL_FATAL)
- exit(1);
- g_last_status = status;
- print_prompt();
- }
- if (ret != FTGL_EOF)
- return (1);
- }
+int main(int argc, char **argv, char **envp)
+{
+ t_env env;
+ if ((env = env_from_array(envp)) == NULL)
+ return (1);
+ setup(argv[0], env);
+ g_last_status = 0;
+ if (argc == 3 && ft_strcmp(argv[1], "-l") == 0)
+ return (debug_lexer(argv[2]));
+ if (argc == 3 && ft_strcmp(argv[1], "-c") == 0)
+ return (execute(env, argv[2]));
+ repl(env);
ft_vecdestroy(env, free);
return (g_last_status);
}
+
+#endif
diff --git a/src/setup.c b/src/setup.c
new file mode 100644
index 0000000..a453e99
--- /dev/null
+++ b/src/setup.c
@@ -0,0 +1,83 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* setup.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/09/16 15:46:09 by charles #+# #+# */
+/* Updated: 2020/09/16 16:17:35 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+
+bool st_export_default(t_env env, char *key, char *value)
+{
+ if (env_search(env, key, NULL) != NULL)
+ return (true);
+ return (env_export(env, key, value) != NULL);
+}
+
+bool setup_env(t_env env)
+{
+ char buf[PATH_MAX + 1];
+
+ if (!(getcwd(buf, PATH_MAX)))
+ return (false);
+ if (!st_export_default(env, "PWD", buf) ||
+ !st_export_default(env, "SHLVL", "0") ||
+ !st_export_default(env, "PATH", "/sbin:"))
+ return (false);
+
+ /* char *path_str = env_search(env, "PATH"); */
+ /* if (ft_strstr(path_str, "/sbin") == NULL) */
+ /* { */
+ /* char *value = ft_strjoin("/sbin:", path_str); */
+ /* env_export(env, "PATH", value); */
+ /* free(value); */
+ /* } */
+
+ if (path_search(env, "env", buf, false) != 0)
+ {
+ ft_strcpy(buf, "/sbin/env");
+ /* errorf("env: command not found\n"); */
+ /* return (127); */
+ }
+ env_export(env, "_", buf);
+ return (true);
+}
+
+bool setup_shlvl(t_env env)
+{
+ char shlvl_str[64];
+ char *shlvl_value;
+
+ shlvl_value = env_search(env, "SHLVL", NULL);
+ if (shlvl_value == NULL)
+ return (false);
+ ft_itoa_cpy(shlvl_str, ft_atoi(shlvl_str));
+ return (env_export(env, "SHLVL", shlvl_str) != NULL);
+}
+
+void setup_progname(char *first_arg)
+{
+ char *last_slash;
+
+ if (first_arg == NULL)
+ return ;
+ last_slash = ft_strrchr(first_arg, '/');
+ if (last_slash == NULL)
+ g_progname = first_arg;
+ else
+ g_progname = last_slash + 1;
+}
+
+bool setup(char *first_arg, t_env env)
+{
+ signal(SIGINT, signal_sigint);
+ signal(SIGQUIT, signal_sigquit);
+ signal(SIGTERM, signal_sigterm);
+ setup_progname(first_arg);
+ return (setup_env(env) || setup_shlvl(env));
+}