Skip to content

Commit

Permalink
Merge pull request #103 from ErwannLesech/dev
Browse files Browse the repository at this point in the history
Step 3
  • Loading branch information
ErwannLesech authored Jan 27, 2024
2 parents febb852 + 272077c commit 74692b4
Show file tree
Hide file tree
Showing 60 changed files with 1,822 additions and 312 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/clang_tidy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ for header_file in $(find "$root_dir/src" -type f -name '*.h'); do
tail -n 1 "$header_file"
fi

# Check if the header file has a newline at the end
test "$(tail -c 1 "$header_file" | wc -l)" -eq 0 && echo "no newline at EOF in file: $header_file"
done

exit 0
1 change: 1 addition & 0 deletions clean_repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ find . -name "*.a" -type f -delete
find . -name "*.o" -type f -delete
find . -name ".deps" -type d -exec rm -r {} +
find . -name ".dirstamp" -type f -delete
find . -name "*.gv" -type f -delete

./tests/testsuite.sh -clean
7 changes: 3 additions & 4 deletions src/ast/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct ast_node *ast_node_new(enum ast_type type)
struct ast_node *node = malloc(sizeof(struct ast_node));
node->type = type;
node->children = NULL;
node->value = NULL;
node->children_count = 0;
return node;
}
Expand Down Expand Up @@ -57,14 +58,12 @@ void ast_free(struct ast_node *node)
}
free(node->children);
}
if (node->type == AST_WORD || node->type == AST_WORD_ASSIGNMENT
|| node->type == AST_VARIABLE || node->type == AST_WORD_DOUBLE_QUOTE
|| node->type == AST_IONUMBER || node->type == AST_REDIRECTION)
if (node->value)
{
free(node->value);
}

free(node);
node = NULL;
}

char *ast_type_to_string(enum ast_type type)
Expand Down
3 changes: 2 additions & 1 deletion src/execute/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ lib_LIBRARIES = libexecute.a

libexecute_a_SOURCES = ast_eval.c ast_eval.h \
../parser/parser.h ../ast/ast.h \
utils/builtin.c utils/builtin.h \
utils/builtin.c utils/builtin_utils.c utils/builtin.h \
utils/builtin_path.c \
utils/pipeline.c utils/loop.c \
hash_map/hash_map.c hash_map/hash_map.h \
hash_map/hash.c utils/ast_variable.c \
Expand Down
12 changes: 9 additions & 3 deletions src/execute/ast_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "ast_eval.h"

#include <fnmatch.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
Expand Down Expand Up @@ -36,7 +37,10 @@ struct builtin_function
*/
struct builtin_function builtin[] = { { .name = "echo", .fun = echo_fun },
{ .name = "true", .fun = true_fun },
{ .name = "false", .fun = false_fun } };
{ .name = "false", .fun = false_fun },
{ .name = "export", .fun = export_fun },
{ .name = "cd", .fun = cd_fun },
{ .name = ".*", .fun = dot_fun } };

/**
* \brief Evaluate the while loop
Expand Down Expand Up @@ -116,9 +120,11 @@ int ast_eval_simple_command(struct ast_node *node)
return return_val;
}
char *command = handle_word(node->children[0]);
for (size_t i = 0; i < 3; i++)

for (size_t i = 0; i < sizeof(builtin) / sizeof(struct builtin_function);
i++)
{
if (strcmp(command, builtin[i].name) == 0)
if (fnmatch(builtin[i].name, command, 0) == 0)
{
int return_val = builtin[i].fun(node);
if (fd_redir != -1)
Expand Down
9 changes: 9 additions & 0 deletions src/execute/ast_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ struct exec_grammar
int (*func)(struct ast_node *);
};

int exec_cmd(struct ast_node *node);

/**
* \brief Evaluate the given AST
* \param node The AST to evaluate.
Expand Down Expand Up @@ -91,6 +93,13 @@ char *handle_word(struct ast_node *node);

int redir_manager(struct ast_node *ast, int *save_fd, int *fd_dup);

/**
* \brief Remove a node from the ast
* \param ast The ast
* \param index The index of the node to remove
*/
void remove_node(struct ast_node *ast, int index);

/**
* \brief Set variable
* \param key The key of the variable.
Expand Down
4 changes: 2 additions & 2 deletions src/execute/utils/ast_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ char *get_variable(char *key)
key++;
for (int i = 0; environment[i].name[0] != '\0'; i++)
{
//printf("key:%s\n", key);
// printf("key:%s\n", key);
if (strcmp(key, environment[i].name) == 0)
{
//printf("key:%s\n", key);
// printf("key:%s\n", key);
return environment[i].fun(key);
}
}
Expand Down
Loading

0 comments on commit 74692b4

Please sign in to comment.