From 65a033c5f65ab9bb49b2e6f0a9f1b99182a9b6a4 Mon Sep 17 00:00:00 2001 From: Matteo Flebus Date: Thu, 8 Jan 2026 18:34:08 +0100 Subject: [PATCH 1/7] feat(autotools): fucking autotools meme aurelien galere --- configure.ac | 9 ++++++++- src/Makefile.am | 22 +++++++++------------- src/lexer/Makefile.am | 2 +- src/parser/Makefile.am | 2 +- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/configure.ac b/configure.ac index 4a89a36..d5a6154 100644 --- a/configure.ac +++ b/configure.ac @@ -1,6 +1,9 @@ # Init the 42sh project AC_INIT([42sh], [1.0], [matteo.flebus@epita.fr]) +FLAGS="-std=c99 -pedantic -Werror -Wall -Wextra -Wvla" +AC_SUBST([FLAGS]) + # Setup Automake AM_INIT_AUTOMAKE([subdir-objects] [foreign]) @@ -21,10 +24,14 @@ AC_PROG_CC # List Makefiles in subdirectories AC_CONFIG_FILES([ - Makefile src/Makefile src/ast/Makefile src/parser/Makefile src/lexer/Makefile + src/io_backend/Makefile + src/execution/Makefile + src/expansion/Makefile + src/utils/Makefile ]) + # TODO add tests Makefile here AC_OUTPUT diff --git a/src/Makefile.am b/src/Makefile.am index 8ecd21f..3e0ecf7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,14 +1,13 @@ # define the subdirectories SUBDIRS = \ - ast \ parser \ lexer \ io_backend \ execution \ - expansin \ + expansion \ utils -bin_PROGRAMS = 42sh +bin_PROGRAMS = 42sh 42sh_asan 42sh_SOURCES = main.c @@ -17,7 +16,6 @@ bin_PROGRAMS = 42sh 42sh_CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla 42sh_LDADD = \ - ast/libast.a \ parser/libparser.a \ lexer/liblexer.a \ io_backend/libio_backend.a \ @@ -28,8 +26,6 @@ bin_PROGRAMS = 42sh ################################################# Test -bin_PROGRAMS = 42sh_asan - 42sh_asan_SOURCES = main.c 42sh_asan_CPPFLAGS = -I%D% @@ -37,10 +33,10 @@ bin_PROGRAMS = 42sh_asan 42sh_asan_CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla -g -fsanitize=address 42sh_asan_LDADD = \ - ast/libast.a \ - parser/libparser.a \ - lexer/liblexer.a \ - io_backend/libio_backend.a \ - expansion/libexpansion.a \ - execution/libexecution.a \ - utils/libutils.a + ast/lib_asan_ast.a \ + parser/lib_asan_parser.a \ + lexer/lib_asan_lexer.a \ + io_backend/lib_asan_io_backend.a \ + expansion/lib_asan_expansion.a \ + execution/lib_asan_execution.a \ + utils/lib_asan_utils.a diff --git a/src/lexer/Makefile.am b/src/lexer/Makefile.am index a113221..26f7797 100644 --- a/src/lexer/Makefile.am +++ b/src/lexer/Makefile.am @@ -1,4 +1,4 @@ -lib_LIBRARIES = liblexer.a +lib_LIBRARIES = liblexer.a lib_asan_lexer.a liblexer_a_SOURCES = \ lexer.c \ diff --git a/src/parser/Makefile.am b/src/parser/Makefile.am index 51c8cb6..4263c5a 100644 --- a/src/parser/Makefile.am +++ b/src/parser/Makefile.am @@ -6,6 +6,6 @@ libparser_a_SOURCES = \ libparser_a_CPPFLAGS = -I$(top_srcdir)/src -libparser_a_CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla +libparser_a_CFLAGS = $(FLAGS) noinst_LIBRARIES = libparser.a From b54488c17f912de99a17d279795f0219b097e65f Mon Sep 17 00:00:00 2001 From: Matteo Flebus Date: Fri, 9 Jan 2026 14:27:27 +0100 Subject: [PATCH 2/7] feat(autotools): explainations in README --- README.md | 14 +++++++++++--- configure.ac | 7 +++++-- src/Makefile.am | 32 ++++++++++++++------------------ src/ast/Makefile.am | 2 -- src/execution/Makefile.am | 2 -- src/expansion/Makefile.am | 2 -- src/io_backend/Makefile.am | 2 -- src/lexer/Makefile.am | 4 +--- src/parser/Makefile.am | 2 -- src/utils/Makefile.am | 11 ++++------- 10 files changed, 35 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 87fe6e2..bbab8c7 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,20 @@ TODO ### Build - -TODO +run this command: + autoreconf --force --verbose --install ### Test +run this command: + ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla' +then: + make -TODO +#### asan +run this command: + ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g -fsanitize=address' +then: + make check ## Authors diff --git a/configure.ac b/configure.ac index d5a6154..f48e3de 100644 --- a/configure.ac +++ b/configure.ac @@ -1,8 +1,8 @@ # Init the 42sh project AC_INIT([42sh], [1.0], [matteo.flebus@epita.fr]) -FLAGS="-std=c99 -pedantic -Werror -Wall -Wextra -Wvla" -AC_SUBST([FLAGS]) +# FLAGS="-std=c99 -pedantic -Werror -Wall -Wextra -Wvla" +# AC_SUBST([FLAGS]) # Setup Automake AM_INIT_AUTOMAKE([subdir-objects] [foreign]) @@ -32,6 +32,9 @@ AC_CONFIG_FILES([ src/execution/Makefile src/expansion/Makefile src/utils/Makefile + tests/Makefile + tests/unit/Makefile + tests/unit/utils/Makefile ]) # TODO add tests Makefile here AC_OUTPUT diff --git a/src/Makefile.am b/src/Makefile.am index 3e0ecf7..411f4cb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,14 +7,12 @@ SUBDIRS = \ expansion \ utils -bin_PROGRAMS = 42sh 42sh_asan +bin_PROGRAMS = 42sh 42sh_SOURCES = main.c 42sh_CPPFLAGS = -I%D% -42sh_CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla - 42sh_LDADD = \ parser/libparser.a \ lexer/liblexer.a \ @@ -25,18 +23,16 @@ bin_PROGRAMS = 42sh 42sh_asan ################################################# Test - -42sh_asan_SOURCES = main.c - -42sh_asan_CPPFLAGS = -I%D% - -42sh_asan_CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla -g -fsanitize=address - -42sh_asan_LDADD = \ - ast/lib_asan_ast.a \ - parser/lib_asan_parser.a \ - lexer/lib_asan_lexer.a \ - io_backend/lib_asan_io_backend.a \ - expansion/lib_asan_expansion.a \ - execution/lib_asan_execution.a \ - utils/lib_asan_utils.a +# +#42sh_asan_SOURCES = main.c +# +#42sh_asan_CPPFLAGS = -I%D% +# +#42sh_asan_LDADD = \ +# ast/lib_asan_ast.a \ +# parser/lib_asan_parser.a \ +# lexer/lib_asan_lexer.a \ +# io_backend/lib_asan_io_backend.a \ +# expansion/lib_asan_expansion.a \ +# execution/lib_asan_execution.a \ +# utils/lib_asan_utils.a diff --git a/src/ast/Makefile.am b/src/ast/Makefile.am index 9df484e..fa41e3d 100644 --- a/src/ast/Makefile.am +++ b/src/ast/Makefile.am @@ -6,6 +6,4 @@ libast_a_SOURCES = \ libast_a_CPPFLAGS = -I$(top_srcdir)/src -libast_a_CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla - noinst_LIBRARIES = libast.a diff --git a/src/execution/Makefile.am b/src/execution/Makefile.am index f83e3ba..5a71cbb 100644 --- a/src/execution/Makefile.am +++ b/src/execution/Makefile.am @@ -6,6 +6,4 @@ libexecution_a_SOURCES = \ libexecution_a_CPPFLAGS = -I$(top_srcdir)/src -libexecution_a_CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla - noinst_LIBRARIES = libexecution.a diff --git a/src/expansion/Makefile.am b/src/expansion/Makefile.am index 5074622..065f225 100644 --- a/src/expansion/Makefile.am +++ b/src/expansion/Makefile.am @@ -6,6 +6,4 @@ libexpansion_a_SOURCES = \ libexpansion_a_CPPFLAGS = -I$(top_srcdir)/src -libexpansion_a_CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla - noinst_LIBRARIES = libexpansion.a diff --git a/src/io_backend/Makefile.am b/src/io_backend/Makefile.am index 7f056b2..0cfadff 100644 --- a/src/io_backend/Makefile.am +++ b/src/io_backend/Makefile.am @@ -6,6 +6,4 @@ libio_backend_a_SOURCES = \ libio_backend_a_CPPFLAGS = -I$(top_srcdir)/src -libio_backend_a_CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla - noinst_LIBRARIES = libio_backend.a diff --git a/src/lexer/Makefile.am b/src/lexer/Makefile.am index 26f7797..dd11411 100644 --- a/src/lexer/Makefile.am +++ b/src/lexer/Makefile.am @@ -1,4 +1,4 @@ -lib_LIBRARIES = liblexer.a lib_asan_lexer.a +lib_LIBRARIES = liblexer.a liblexer_a_SOURCES = \ lexer.c \ @@ -6,6 +6,4 @@ liblexer_a_SOURCES = \ liblexer_a_CPPFLAGS = -I$(top_srcdir)/src -liblexer_a_CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla - noinst_LIBRARIES = liblexer.a diff --git a/src/parser/Makefile.am b/src/parser/Makefile.am index 4263c5a..dd13901 100644 --- a/src/parser/Makefile.am +++ b/src/parser/Makefile.am @@ -6,6 +6,4 @@ libparser_a_SOURCES = \ libparser_a_CPPFLAGS = -I$(top_srcdir)/src -libparser_a_CFLAGS = $(FLAGS) - noinst_LIBRARIES = libparser.a diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am index dba2e51..5b72df3 100644 --- a/src/utils/Makefile.am +++ b/src/utils/Makefile.am @@ -1,11 +1,8 @@ lib_LIBRARIES = libutils.a -# libutils_a_SOURCES = \ -# utils.c \ -# utils.h +libutils_a_SOURCES = \ + string_utils.c -# libutils_a_CPPFLAGS = -I$(top_srcdir)/src +libutils_a_CPPFLAGS = -I$(top_srcdir)/src -# libutils_a_CFLAGS = -std=c99 -pedantic -Werror -Wall -Wextra -Wvla - -# noinst_LIBRARIES = libutils.a +noinst_LIBRARIES = libutils.a From c341b62ec6ed1bffc93d5471ccabaa2e242ed2d5 Mon Sep 17 00:00:00 2001 From: Matteo Flebus Date: Fri, 9 Jan 2026 14:34:22 +0100 Subject: [PATCH 3/7] fix(configure.ac): commented Makefiles for testing --- configure.ac | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index f48e3de..f9684ed 100644 --- a/configure.ac +++ b/configure.ac @@ -32,9 +32,9 @@ AC_CONFIG_FILES([ src/execution/Makefile src/expansion/Makefile src/utils/Makefile - tests/Makefile - tests/unit/Makefile - tests/unit/utils/Makefile ]) # TODO add tests Makefile here + # tests/Makefile + # tests/unit/Makefile + # tests/unit/utils/Makefile AC_OUTPUT From 3e66d4b959b8801ea27340ddd7e56ceaea3ebaee Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Fri, 9 Jan 2026 16:05:41 +0100 Subject: [PATCH 4/7] refactor: moved ast module to utils --- src/{ => utils}/ast/Makefile.am | 0 src/{ => utils}/ast/ast.c | 0 src/{ => utils}/ast/ast.h | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename src/{ => utils}/ast/Makefile.am (100%) rename src/{ => utils}/ast/ast.c (100%) rename src/{ => utils}/ast/ast.h (100%) diff --git a/src/ast/Makefile.am b/src/utils/ast/Makefile.am similarity index 100% rename from src/ast/Makefile.am rename to src/utils/ast/Makefile.am diff --git a/src/ast/ast.c b/src/utils/ast/ast.c similarity index 100% rename from src/ast/ast.c rename to src/utils/ast/ast.c diff --git a/src/ast/ast.h b/src/utils/ast/ast.h similarity index 100% rename from src/ast/ast.h rename to src/utils/ast/ast.h From 62e44b8101fdac007b5bbd1c13020daa0f70c725 Mon Sep 17 00:00:00 2001 From: "william.valenduc" Date: Fri, 9 Jan 2026 17:21:23 +0000 Subject: [PATCH 5/7] fix: ast circular dep --- src/utils/ast/ast.h | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/utils/ast/ast.h b/src/utils/ast/ast.h index 5bfd8df..8e01551 100644 --- a/src/utils/ast/ast.h +++ b/src/utils/ast/ast.h @@ -1,7 +1,7 @@ #ifndef AST_H #define AST_H -#include "lists.h" +#include "../lists/lists.h" enum ast_type { @@ -10,28 +10,30 @@ enum ast_type AST_CMD }; -union ast_node +struct ast_cmd { - struct ast_if; - struct ast_cmd; + struct list *cmd; }; +union ast_node; + struct ast { enum ast_type type; - union ast_node data; + union ast_node *data; }; struct ast_if { - struct ast* condition; - struct ast* then_clause; - struct ast* else_clause; + struct ast *condition; + struct ast *then_clause; + struct ast *else_clause; }; -struct ast_cmd +union ast_node { - struct list* cmd; + struct ast_if *ast_if; + struct ast_cmd *ast_cmd; }; #endif /* ! AST_H */ From 807416940c25728e236041f0e2f30ee0a98f36fb Mon Sep 17 00:00:00 2001 From: "william.valenduc" Date: Fri, 9 Jan 2026 22:36:48 +0000 Subject: [PATCH 6/7] feat(ast): data as void* and basic helpers --- src/utils/ast/ast.c | 23 +++++++++++++++++++++++ src/utils/ast/ast.h | 23 +++++++++++------------ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/utils/ast/ast.c b/src/utils/ast/ast.c index e69de29..93fa402 100644 --- a/src/utils/ast/ast.c +++ b/src/utils/ast/ast.c @@ -0,0 +1,23 @@ +#include "ast.h" + +#include + +bool ast_is_if(struct ast *node) +{ + return node->type == AST_IF; +} + +bool ast_is_cmd(struct ast *node) +{ + return node->type == AST_CMD; +} + +struct ast_if *ast_get_if(struct ast *node) +{ + return (struct ast_if *)node->data; +} + +struct ast_cmd *ast_get_cmd(struct ast *node) +{ + return (struct ast_cmd *)node->data; +} diff --git a/src/utils/ast/ast.h b/src/utils/ast/ast.h index 8e01551..c68b143 100644 --- a/src/utils/ast/ast.h +++ b/src/utils/ast/ast.h @@ -5,7 +5,7 @@ enum ast_type { - AST_END = 0, + AST_END, AST_IF, AST_CMD }; @@ -15,14 +15,6 @@ struct ast_cmd struct list *cmd; }; -union ast_node; - -struct ast -{ - enum ast_type type; - union ast_node *data; -}; - struct ast_if { struct ast *condition; @@ -30,10 +22,17 @@ struct ast_if struct ast *else_clause; }; -union ast_node +struct ast { - struct ast_if *ast_if; - struct ast_cmd *ast_cmd; + enum ast_type type; + + /** + * Data associated with this AST node. It can be one of the following: + * - NULL (AST_END) + * - struct ast_if* (AST_IF) + * - struct ast_cmd* (AST_CMD) + */ + void *data; }; #endif /* ! AST_H */ From 13bed60c09e264d25df8a08c09ed0adfc719fa5c Mon Sep 17 00:00:00 2001 From: "william.valenduc" Date: Fri, 9 Jan 2026 22:50:09 +0000 Subject: [PATCH 7/7] feat(ast): ast_create_if, ast_create_cmd and header doc --- src/utils/ast/ast.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/utils/ast/ast.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/utils/ast/ast.c b/src/utils/ast/ast.c index 93fa402..40b42d7 100644 --- a/src/utils/ast/ast.c +++ b/src/utils/ast/ast.c @@ -1,23 +1,68 @@ #include "ast.h" +#include #include +#include bool ast_is_if(struct ast *node) { + assert(node != NULL); return node->type == AST_IF; } bool ast_is_cmd(struct ast *node) { + assert(node != NULL); return node->type == AST_CMD; } struct ast_if *ast_get_if(struct ast *node) { + assert(node != NULL); + assert(node->type == AST_IF); return (struct ast_if *)node->data; } struct ast_cmd *ast_get_cmd(struct ast *node) { + assert(node != NULL); + assert(node->type == AST_CMD); return (struct ast_cmd *)node->data; } + +static struct ast *ast_create(enum ast_type type, void *data) +{ + struct ast *node = malloc(sizeof(struct ast)); + if (!node) + return NULL; + + node->type = type; + node->data = data; + + return node; +} + +struct ast *ast_create_if(struct ast *condition, struct ast *then_clause, + struct ast *else_clause) +{ + struct ast_if *if_data = malloc(sizeof(struct ast_if)); + if (!if_data) + return NULL; + + if_data->condition = condition; + if_data->then_clause = then_clause; + if_data->else_clause = else_clause; + + return ast_create(AST_IF, if_data); +} + +struct ast *ast_create_cmd(struct list *cmd) +{ + struct ast_cmd *cmd_data = malloc(sizeof(struct ast_cmd)); + if (!cmd_data) + return NULL; + + cmd_data->cmd = cmd; + + return ast_create(AST_CMD, cmd_data); +} diff --git a/src/utils/ast/ast.h b/src/utils/ast/ast.h index c68b143..3247ba5 100644 --- a/src/utils/ast/ast.h +++ b/src/utils/ast/ast.h @@ -1,6 +1,8 @@ #ifndef AST_H #define AST_H +#include + #include "../lists/lists.h" enum ast_type @@ -35,4 +37,37 @@ struct ast void *data; }; +/** + * Checks if the given AST node is an if statement. + */ +bool ast_is_if(struct ast *node); + +/** + * Checks if the given AST node is a command. + */ +bool ast_is_cmd(struct ast *node); + +/** + * Retrieves the if statement data from the given AST node. + * Assumes that the node is of type AST_IF. + */ +struct ast_if *ast_get_if(struct ast *node); + +/** + * Retrieves the command data from the given AST node. + * Assumes that the node is of type AST_CMD. + */ +struct ast_cmd *ast_get_cmd(struct ast *node); + +/** + * Creates a new AST node representing an if statement. + */ +struct ast *ast_create_if(struct ast *condition, struct ast *then_clause, + struct ast *else_clause); + +/** + * Creates a new AST node representing a command. + */ +struct ast *ast_create_cmd(struct list *cmd); + #endif /* ! AST_H */