aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-10-10 08:11:59 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-10-10 08:11:59 +0200
commit25ca78b5a53c931b0bc388aef099974dbba4b6ff (patch)
tree64a78d39acd408cc3dc0acaaf4c304ed2e1227c6
parenta55e414f6709c6b22ff9805cd32a40fae9da2538 (diff)
downloadminishell-25ca78b5a53c931b0bc388aef099974dbba4b6ff.tar.gz
minishell-25ca78b5a53c931b0bc388aef099974dbba4b6ff.tar.bz2
minishell-25ca78b5a53c931b0bc388aef099974dbba4b6ff.zip
Norming debugging function, Enabling them only when MINISHELL_TEST is defined
-rw-r--r--Makefile4
-rw-r--r--include/minishell.h24
-rw-r--r--include/parser.h35
-rw-r--r--src/debug.c122
-rw-r--r--src/debug/lexer.c37
-rw-r--r--src/debug/parser.c112
-rw-r--r--src/setup.c4
7 files changed, 188 insertions, 150 deletions
diff --git a/Makefile b/Makefile
index 0e2168b..a2ffe86 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/02/03 04:14:24 by cacharle #+# #+# #
-# Updated: 2020/10/08 13:46:23 by cacharle ### ########.fr #
+# Updated: 2020/10/10 08:11:22 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -31,7 +31,7 @@ SRC = $(shell find $(SRCDIR) -type f -name "*.c")
OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
CC = gcc
-CCFLAGS = -g -I$(LIBFTDIR)/include -I$(INCLUDEDIR) -Wall -Wextra #-Werror
+CCFLAGS = -g -O2 -I$(LIBFTDIR)/include -I$(INCLUDEDIR) -Wall -Wextra #-Werror
LDFLAGS = -L$(LIBFTDIR) -lft
NAME = minishell
diff --git a/include/minishell.h b/include/minishell.h
index d205e29..4181c46 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/10/09 20:40:08 by charles ### ########.fr */
+/* Updated: 2020/10/10 08:08:21 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -60,8 +60,7 @@ typedef struct
{
int last_status;
char *progname;
- pid_t pids[STATE_PIDS_MAX_SIZE];
- size_t pids_len;
+ pid_t child_pid;
bool killed;
bool is_child;
} t_state;
@@ -126,6 +125,9 @@ int builtin_exit(char **argv, t_env env);
char **preprocess(t_tok_lst **tokens, t_env env);
int preprocess_filename(
t_tok_lst **tokens, t_env env, char **filename);
+size_t interpolate(
+ char *str, size_t i, t_tok_lst **curr_addr,
+ enum e_tok prev_tag, t_env env);
/*
** signal.c
@@ -149,7 +151,19 @@ bool utils_strisblank(char *str);
bool setup(char *first_arg, t_env env);
-size_t interpolate(
- char *str, size_t i, t_tok_lst **curr_addr, enum e_tok prev_tag, t_env env);
+
+/*
+** debug.c
+*/
+
+# ifdef MINISHELL_TEST
+
+# include "parser.h"
+
+int debug_lexer(char *input);
+int debug_parser(char *input);
+void ast_print(int level, t_ast *ast);
+
+# endif
#endif
diff --git a/include/parser.h b/include/parser.h
index bd4e5b0..4bf79df 100644
--- a/include/parser.h
+++ b/include/parser.h
@@ -6,12 +6,12 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 09:00:00 by cacharle #+# #+# */
-/* Updated: 2020/10/09 16:02:40 by cacharle ### ########.fr */
+/* Updated: 2020/10/10 07:37:55 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
-#ifndef PARSE_H
-# define PARSE_H
+#ifndef PARSER_H
+# define PARSER_H
/*
** \file parser.h
@@ -36,7 +36,7 @@
# include "lexer.h"
# include "error.h"
-struct s_ast;
+struct s_ast;
/*
** \brief Operation struct
@@ -45,12 +45,11 @@ struct s_ast;
** \param sep Type of separator
*/
-
typedef struct s_op
{
struct s_ast *left;
struct s_ast *right;
- enum e_tok sep;
+ enum e_tok sep;
} t_op;
/*
@@ -77,18 +76,18 @@ enum e_ast
** \param pipline List of commands in a pipeline
** \param parend_ast AST inside parenthesis
** \param redirs Redirections tokens
+** \note Norminette doesn't like unamed union
+** It breaks my heart to waste memory but
+** t_ast isn't heavily used anyway
*/
typedef struct s_ast
{
enum e_ast tag;
- union
- {
- t_op op;
- t_tok_lst *cmd_argv;
- t_ftlst *pipeline;
- struct s_ast *parent_ast;
- };
+ t_op op;
+ t_tok_lst *cmd_argv;
+ t_ftlst *pipeline;
+ struct s_ast *parent_ast;
t_tok_lst *redirs;
} t_ast;
@@ -112,8 +111,8 @@ typedef struct s_parsed
t_parsed *parsed_new(t_ast *ast, t_tok_lst *rest);
t_parsed *parsed_error(const char *format, ...);
-t_parsed *parsed_expected(void);
-t_parsed *parsed_unexpected(char *content);
+t_parsed *parsed_expected(void);
+t_parsed *parsed_unexpected(char *content);
void parsed_destroy(t_parsed *parsed);
/*
@@ -121,8 +120,8 @@ void parsed_destroy(t_parsed *parsed);
*/
t_parsed *parse(t_tok_lst *input);
-t_parsed *parse_op(t_tok_lst *input);
-t_parsed *parse_expr(t_tok_lst *input);
-t_parsed *parse_cmd(t_tok_lst *input);
+t_parsed *parse_op(t_tok_lst *input);
+t_parsed *parse_expr(t_tok_lst *input);
+t_parsed *parse_cmd(t_tok_lst *input);
#endif
diff --git a/src/debug.c b/src/debug.c
deleted file mode 100644
index a0e6770..0000000
--- a/src/debug.c
+++ /dev/null
@@ -1,122 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* debug.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/09/16 15:58:35 by charles #+# #+# */
-/* Updated: 2020/10/09 14:27:40 by cacharle ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#include "lexer.h"
-#include "parser.h"
-
-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;
- }
-}
-
-void debug_tok_lst_line(t_tok_lst *tokens)
-{
- while (tokens != NULL)
- {
- // FIXME libft for safer correction
- printf("|%s| ", tokens->content);
- 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 print_level(int level)
-{
- while (level-- > 0)
- printf(" ");
-}
-
-void ast_print(int level, t_ast *ast)
-{
- if (ast == NULL)
- return ;
- if (ast->tag == AST_PARENT)
- {
- print_level(level);
- printf("parent: redir [ ");
- debug_tok_lst_line(ast->redirs);
- printf(" ]\n");
- ast_print(level + 1, ast->parent_ast);
- }
- else if (ast->tag == AST_PIPELINE)
- {
- print_level(level);
- printf("pipeline: {\n");
- t_ftlst *curr = ast->pipeline;
- while (curr != NULL)
- {
- ast_print(level + 1, (t_ast*)curr->data);
- printf("\n");
- curr = curr->next;
- }
- print_level(level);
- printf(" }\n");
- }
- else if (ast->tag == AST_CMD)
- {
- print_level(level);
- printf("cmd: [ ");
- debug_tok_lst_line(ast->cmd_argv);
- printf(" ] redirs: [");
- debug_tok_lst_line(ast->redirs);
- printf(" ]");
- }
- else if (ast->tag == AST_OP) {
- printf("{\n");
- print_level(level);
- printf(" left:\n");
- ast_print(level + 1, ast->op.left);
- printf("\n");
- print_level(level);
- printf(" right:\n");
- ast_print(level + 1, ast->op.right);
- printf("\n");
- print_level(level);
- printf("}\n");
- }
-}
-
-int debug_parser(char *input)
-{
- int status;
- t_tok_lst *out;
-
- status = lexer(input, &out);
- if (status != 0)
- {
- printf("Lexer error\n");
- debug_tok_lst(out);
- return (status);
- }
- t_parsed *ret = parse(out);
- if (ret == NULL || ret->syntax_error)
- return (1);
- ast_print(0, ret->ast);
- return (status);
-}
diff --git a/src/debug/lexer.c b/src/debug/lexer.c
new file mode 100644
index 0000000..4d25662
--- /dev/null
+++ b/src/debug/lexer.c
@@ -0,0 +1,37 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* lexer.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/10/10 07:51:39 by cacharle #+# #+# */
+/* Updated: 2020/10/10 07:56:44 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+#include "lexer.h"
+
+#ifdef MINISHELL_TEST
+
+int debug_lexer(char *input)
+{
+ int status;
+ t_tok_lst *out;
+
+ status = lexer(input, &out);
+ if (status != 0)
+ return (status);
+ while (out != NULL)
+ {
+ ft_putnbr(out->tag);
+ ft_putchar(' ');
+ ft_putstr(out->content);
+ ft_putendl(out->tag & TAG_STICK ? " STICK" : "");
+ out = out->next;
+ }
+ return (status);
+}
+
+#endif
diff --git a/src/debug/parser.c b/src/debug/parser.c
new file mode 100644
index 0000000..4eda29a
--- /dev/null
+++ b/src/debug/parser.c
@@ -0,0 +1,112 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* parser.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/10/10 07:53:11 by cacharle #+# #+# */
+/* Updated: 2020/10/10 08:06:35 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "lexer.h"
+#include "minishell.h"
+#include "parser.h"
+
+#ifdef MINISHELL_TEST
+
+static void st_tok_lst_put(t_tok_lst *tokens)
+{
+ while (tokens != NULL)
+ {
+ ft_putchar('|');
+ ft_putstr(tokens->content);
+ ft_putstr("| ");
+ tokens = tokens->next;
+ }
+}
+
+static void st_print_level(int level)
+{
+ while (level-- > 0)
+ ft_putstr(" ");
+}
+
+void st_ast_print_parent_pipeline(int level, t_ast *ast)
+{
+ t_ftlst *curr;
+
+ if (ast->tag == AST_PARENT)
+ {
+ st_print_level(level);
+ ft_putstr("parent: redir [ ");
+ st_tok_lst_put(ast->redirs);
+ ft_putstr(" ]\n");
+ ast_print(level + 1, ast->parent_ast);
+ }
+ else if (ast->tag == AST_PIPELINE)
+ {
+ st_print_level(level);
+ ft_putstr("pipeline: {\n");
+ curr = ast->pipeline;
+ while (curr != NULL)
+ {
+ ast_print(level + 1, (t_ast *)curr->data);
+ ft_putstr("\n");
+ curr = curr->next;
+ }
+ st_print_level(level);
+ ft_putstr(" }\n");
+ }
+}
+
+void ast_print(int level, t_ast *ast)
+{
+ if (ast->tag == AST_PARENT || ast->tag == AST_PIPELINE)
+ st_ast_print_parent_pipeline(level, ast);
+ else if (ast->tag == AST_CMD)
+ {
+ st_print_level(level);
+ ft_putstr("cmd: [ ");
+ st_tok_lst_put(ast->cmd_argv);
+ ft_putstr(" ] redirs: [");
+ st_tok_lst_put(ast->redirs);
+ ft_putstr(" ]");
+ }
+ else if (ast->tag == AST_OP)
+ {
+ ft_putstr("{\n");
+ st_print_level(level);
+ ft_putstr(" left:\n");
+ ast_print(level + 1, ast->op.left);
+ ft_putstr("\n");
+ st_print_level(level);
+ ft_putstr(" right:\n");
+ ast_print(level + 1, ast->op.right);
+ ft_putstr("\n");
+ st_print_level(level);
+ ft_putstr("}\n");
+ }
+}
+
+int debug_parser(char *input)
+{
+ int status;
+ t_tok_lst *out;
+ t_parsed *ret;
+
+ status = lexer(input, &out);
+ if (status != 0)
+ {
+ ft_putendl("Lexer error\n");
+ return (status);
+ }
+ ret = parse(out);
+ if (ret == NULL || ret->syntax_error)
+ return (1);
+ ast_print(0, ret->ast);
+ return (status);
+}
+
+#endif
diff --git a/src/setup.c b/src/setup.c
index 844b9e4..2830e2d 100644
--- a/src/setup.c
+++ b/src/setup.c
@@ -6,7 +6,7 @@
/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/16 15:46:09 by charles #+# #+# */
-/* Updated: 2020/10/09 13:38:34 by cacharle ### ########.fr */
+/* Updated: 2020/10/10 08:08:35 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -66,7 +66,5 @@ bool setup(char *first_arg, t_env env)
signal(SIGQUIT, signal_sigquit);
signal(SIGTERM, signal_sigterm);
setup_progname(first_arg);
- ft_bzero(g_state.pids, STATE_PIDS_MAX_SIZE);
- g_state.pids_len = 0;
return (setup_env(env) || setup_shlvl(env));
}