Compare commits

...
Sign in to create a new pull request.

20 commits
dev ... lexer

Author SHA1 Message Date
matteo
79fb96e5e2 fix: ast_redir and ast_assignment accorded for every part 2026-01-29 18:53:41 +01:00
matteo
51158ec4f9 fix(lexer): merge conflicts 2026-01-29 18:53:37 +01:00
matteo
148db79788 fix(merge conflicts) 2026-01-29 18:52:51 +01:00
Jean
6948bbae63 fix(execution): Reimplemented the redirection logic 2026-01-29 18:51:07 +01:00
william.valenduc
3d26c0a897 fix(ast)!: assignment typo 2026-01-29 18:51:07 +01:00
william.valenduc
a0b723fb30 feat(execution): update $PWD and $OLD_PWD 2026-01-29 18:51:07 +01:00
matteo
dd8a7165e1 Revert "feat(ast_assignement): changed (name, value) to assignement"
This reverts commit ac77d79a60.
2026-01-29 12:19:21 +01:00
matteo
2884fa78d9 feat(ast_assignement): changed (name, value) to assignement 2026-01-29 12:19:21 +01:00
matteo
6da801675a feat(lexer): implementing assignements 2026-01-29 12:19:21 +01:00
matteo
46dde29c20 fix: small errors in exec and parser 2026-01-29 12:19:21 +01:00
matteo
d775ab6c1a feat(ast): add ast_assignements. Similar to how redirections are handled. 2026-01-29 12:19:21 +01:00
william.valenduc
308ac9ac8d feat(expansion): remove double quotes 2026-01-29 12:19:21 +01:00
Jean
ed64a1bc18 began to reimplement the new execution 2026-01-29 12:19:21 +01:00
matteo
050a1909f0 feat(parser): helper static function 2026-01-29 12:19:21 +01:00
matteo
967a78e1a4 fix(parser): small typos 2026-01-29 12:19:21 +01:00
matteo
efb0a6f148 style(parser): refactor parse_command 2026-01-29 12:19:21 +01:00
matteo
51fc8a3330 feat(parser): parse_command version with prefixes and elements 2026-01-29 12:19:21 +01:00
matteo
9d586402df fix: redirections types 2026-01-29 12:19:21 +01:00
Matteo Flebus
1e4b3fb0a8 feat(redirections): ast commands now have a field for the list of redirections + redirections implemented in parser 2026-01-29 12:19:21 +01:00
matteo
f1310b7b09 feat(lexer): parsing and_or operators 2026-01-28 16:42:40 +01:00
17 changed files with 439 additions and 130 deletions

View file

@ -14,8 +14,6 @@
#include "../utils/ast/ast.h" #include "../utils/ast/ast.h"
#include "../utils/hash_map/hash_map.h" #include "../utils/hash_map/hash_map.h"
// Refactored: delegates to helpers in execution_helpers.c // Refactored: delegates to helpers in execution_helpers.c
#include "execution_helpers.h" #include "execution_helpers.h"
@ -33,6 +31,7 @@ int execution(struct ast *ast, struct hash_map *vars)
struct ast_command *command = ast_get_command(ast); struct ast_command *command = ast_get_command(ast);
if (!expand(command, vars)) if (!expand(command, vars))
fprintf(stderr, "Error: Variable expansion failed\n"); fprintf(stderr, "Error: Variable expansion failed\n");
return exec_ast_command(command, vars); return exec_ast_command(command, vars);
} }
case AST_IF: case AST_IF:
@ -41,8 +40,6 @@ int execution(struct ast *ast, struct hash_map *vars)
return exec_ast_list(ast_get_list(ast), vars); return exec_ast_list(ast_get_list(ast), vars);
case AST_AND_OR: case AST_AND_OR:
return exec_ast_and_or(ast_get_and_or(ast), vars); return exec_ast_and_or(ast_get_and_or(ast), vars);
case AST_REDIR:
return exec_ast_redir(ast_get_redir(ast), vars);
default: default:
return 127; return 127;
} }

View file

@ -12,6 +12,8 @@
#include "../expansion/expansion.h" #include "../expansion/expansion.h"
#include "../utils/ast/ast.h" #include "../utils/ast/ast.h"
#include "../utils/hash_map/hash_map.h" #include "../utils/hash_map/hash_map.h"
#include "../utils/lists/lists.h"
#include "../utils/vars/vars.h"
#include "execution.h" #include "execution.h"
static char **list_to_argv(struct list *command_list) static char **list_to_argv(struct list *command_list)
@ -36,43 +38,61 @@ static char **list_to_argv(struct list *command_list)
return argv; return argv;
} }
static int try_builtin(char **argv); static int try_builtin(char **argv, struct hash_map *vars);
int exec_ast_command(struct ast_command *command, struct hash_map *vars) int exec_ast_command(struct ast_command *command, struct hash_map *vars)
{ {
(void)vars; (void)vars;
set_all_redir(command->redirections);
if (!command || !(command->command)) if (!command || !(command->command))
{
return 1; return 1;
}
char **argv = list_to_argv(command->command); char **argv = list_to_argv(command->command);
if (!argv || !(argv[0])) if (!argv || !(argv[0]))
{ {
free(argv); free(argv);
unset_all_redir();
return 0; return 0;
} }
int builtin_ret = try_builtin(argv);
int builtin_ret = try_builtin(argv, vars);
if (builtin_ret != -1) if (builtin_ret != -1)
{ {
free(argv); free(argv);
unset_all_redir();
return builtin_ret; return builtin_ret;
} }
pid_t pid = fork(); pid_t pid = fork();
if (pid < 0) if (pid < 0)
{ {
perror("fork"); perror("fork");
free(argv); free(argv);
unset_all_redir();
return 1; return 1;
} }
if (pid == 0) if (pid == 0)
{ {
execvp(argv[0], argv); execvp(argv[0], argv);
perror("execvp"); perror("execvp");
unset_all_redir();
_exit(127); _exit(127);
} }
int status = 0; int status = 0;
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
free(argv); free(argv);
unset_all_redir();
if (WIFEXITED(status)) if (WIFEXITED(status))
{
return WEXITSTATUS(status); return WEXITSTATUS(status);
}
return 1; return 1;
} }
@ -121,7 +141,7 @@ static int get_fd_target(const struct ast_redir *redir)
if (redir->io_number != -1) if (redir->io_number != -1)
return redir->io_number; return redir->io_number;
if (redir->type == AST_REDIR_TYPE_LESS if (redir->type == AST_REDIR_TYPE_LESS
|| redir->type == AST_REDIR_TYPE_DLESS || redir->type == AST_REDIR_TYPE_LESSGREAT
|| redir->type == AST_REDIR_TYPE_LESSAND) || redir->type == AST_REDIR_TYPE_LESSAND)
return 0; return 0;
return 1; return 1;
@ -163,6 +183,72 @@ static int handle_and_restore_fd(int saved_fd, int fd_target)
return 0; return 0;
} }
static int set_all_redir(struct list *redir_list)
{
while (redir_list)
{
struct ast_redir *redir = (struct ast_redir *)redir_list->data;
int target_fd;
if (redir->io_number != -1)
{
target_fd = redir->io_number;
}
else
{
// assign target_fd depending on redir type
if (redir->type == AST_REDIR_TYPE_LESS
|| redir->type == AST_REDIR_TYPE_LESSGREAT
|| redir->type == AST_REDIR_TYPE_LESSAND)
{
target_fd = 0;
}
else
{
target_fd = 1;
}
}
redir->saved_fd = dup(target_fd);
int new_fd = -1;
int flags = 0;
int mode = 0644;
if (redir->type == AST_REDIR_TYPE_GREAT
|| redir->type == AST_REDIR_TYPE_CLOBBER
|| redir->type == AST_REDIR_TYPE_DGREAT
|| redir->type == AST_REDIR_TYPE_LESS)
{
new_fd = open_redir_file(redir, &flags, &mode);
if (new_fd == -1)
{
perror("open");
return -1;
}
}
else if (redir->type == AST_REDIR_TYPE_GREATAND
|| redir->type == AST_REDIR_TYPE_LESSAND)
{
new_fd = atoi(redir->filename);
}
if (dup2(new_fd, target_fd) == -1)
{
perror("dup2");
if (new_fd != -1)
{
close(new_fd);
}
return -1;
}
if (new_fd != -1)
{
close(new_fd);
}
redir_list = redir_list->next;
}
return 0;
}
/*
int exec_ast_redir(struct ast_redir *redir, struct hash_map *vars) int exec_ast_redir(struct ast_redir *redir, struct hash_map *vars)
{ {
int fd_target = get_fd_target(redir); int fd_target = get_fd_target(redir);
@ -206,7 +292,7 @@ int exec_ast_redir(struct ast_redir *redir, struct hash_map *vars)
int ret = execution(redir->child, vars); int ret = execution(redir->child, vars);
handle_and_restore_fd(saved_fd, fd_target); handle_and_restore_fd(saved_fd, fd_target);
return ret; return ret;
} } */
// --- Builtins --- // --- Builtins ---
@ -234,7 +320,6 @@ static int builtin_echo(char **argv)
return 0; return 0;
} }
static int builtin_true(char **argv) static int builtin_true(char **argv)
{ {
(void)argv; (void)argv;
@ -256,8 +341,7 @@ static int builtin_exit(char **argv)
return exit_val; return exit_val;
} }
static int builtin_cd(char **argv, struct hash_map *vars)
static int builtin_cd(char **argv)
{ {
const char *path = argv[1]; const char *path = argv[1];
if (!path) if (!path)
@ -269,22 +353,25 @@ static int builtin_cd(char **argv)
return 1; return 1;
} }
} }
// char *pwd = getcwd("", "");
char *pwd = get_var_or_env(vars, "PWD");
if (chdir(path) != 0) if (chdir(path) != 0)
{ {
perror("cd"); perror("cd");
return 1; return 1;
} }
set_var_copy(vars, "OLD_PWD", pwd);
set_var_copy(vars, "PWD", path);
return 0; return 0;
} }
/** /**
* @brief Tries to execute a builtin command if the command matches a builtin * @brief Tries to execute a builtin command if the command matches a builtin
* *
* @param argv Array of command arguments * @param argv Array of command arguments
* @return int Exit status of the builtin command, or -1 if not a builtin * @return int Exit status of the builtin command, or -1 if not a builtin
*/ */
static int try_builtin(char **argv) static int try_builtin(char **argv, struct hash_map *vars)
{ {
if (!argv || !argv[0]) if (!argv || !argv[0])
return 0; return 0;
@ -298,7 +385,7 @@ static int try_builtin(char **argv)
if (strcmp(argv[0], "exit") == 0) if (strcmp(argv[0], "exit") == 0)
return builtin_exit(argv); return builtin_exit(argv);
if (strcmp(argv[0], "cd") == 0) if (strcmp(argv[0], "cd") == 0)
return builtin_cd(argv); return builtin_cd(argv, vars);
return -1; return -1;
} }

View file

@ -8,6 +8,5 @@ int exec_ast_command(struct ast_command *command, struct hash_map *vars);
int exec_ast_if(struct ast_if *if_node, struct hash_map *vars); int exec_ast_if(struct ast_if *if_node, struct hash_map *vars);
int exec_ast_list(struct ast_list *list_node, struct hash_map *vars); int exec_ast_list(struct ast_list *list_node, struct hash_map *vars);
int exec_ast_and_or(struct ast_and_or *ao_node, struct hash_map *vars); int exec_ast_and_or(struct ast_and_or *ao_node, struct hash_map *vars);
int exec_ast_redir(struct ast_redir *redir, struct hash_map *vars);
#endif // EXECUTION_HELPERS_H #endif // EXECUTION_HELPERS_H

View file

@ -135,7 +135,7 @@ bool expand(struct ast_command *command, const struct hash_map *vars)
{ {
if (str[i] == '\'') if (str[i] == '\'')
{ {
// remove quote // remove single quote
in_quotes = !in_quotes; in_quotes = !in_quotes;
memmove(str + i, str + i + 1, strlen(str + i + 1) + 1); memmove(str + i, str + i + 1, strlen(str + i + 1) + 1);
i--; i--;
@ -144,6 +144,12 @@ bool expand(struct ast_command *command, const struct hash_map *vars)
{ {
continue; // do nothing continue; // do nothing
} }
else if (str[i] == '\"')
{
// remove double quote
memmove(str + i, str + i + 1, strlen(str + i + 1) + 1);
i--;
}
else if (str[i] == '$' && str[i + 1] != 0 && !isspace(str[i + 1])) else if (str[i] == '$' && str[i + 1] != 0 && !isspace(str[i + 1]))
{ {
// variable expansion // variable expansion

View file

@ -78,21 +78,43 @@ static bool update_lexing_mode(char *stream, ssize_t i,
return *lexing_mode != mode_before_update; return *lexing_mode != mode_before_update;
} }
/* @brief: updates the flags only_digits and has_equal.
* according to the character at stream[i].
*/
static void update_flags(char *stream, ssize_t i, struct token_info *info)
{
if (stream[i] == '=' && !info->has_equal)
{
if (i == 0)
{
perror("Syntax error: word start with a '='");
return;
}
else
info->has_equal = true;
}
else if (!isdigit(stream[i]) && info->only_digits)
{
info->only_digits = false;
}
}
struct token *peek_token(struct lexer_context *ctx) struct token *peek_token(struct lexer_context *ctx)
{ {
stream_init(ctx);
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
struct token_info info = { true, 0 };
char *stream = ctx->end_previous_token;
ssize_t i = 0;
// we already created the upcoming token during the previous call to peek() // we already created the upcoming token during the previous call to peek()
if (ctx->current_token != NULL) if (ctx->current_token != NULL)
{ {
return ctx->current_token; return ctx->current_token;
} }
stream_init(ctx);
char *stream = ctx->end_previous_token;
ssize_t i = 0;
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
while (i < ctx->remaining_chars) while (i < ctx->remaining_chars)
{ {
// true if we didn't encounter a quote of any type at stream[i] // true if we didn't encounter a quote of any type at stream[i]
@ -100,6 +122,7 @@ struct token *peek_token(struct lexer_context *ctx)
if (!update_lexing_mode(stream, i, &lexing_mode) if (!update_lexing_mode(stream, i, &lexing_mode)
&& lexing_mode == LEXER_NORMAL) && lexing_mode == LEXER_NORMAL)
{ {
update_flags(stream, i, &info);
if (is_special_char(stream, i)) if (is_special_char(stream, i))
{ {
if (i == 0) // where we create spe_char token if (i == 0) // where we create spe_char token
@ -121,7 +144,7 @@ struct token *peek_token(struct lexer_context *ctx)
i++; i++;
} }
struct token *tok = new_token(stream, i, ctx->only_digits); struct token *tok = new_token(stream, i, &info);
// if token is comment, we don't want it // if token is comment, we don't want it
if (tok->type == TOKEN_COMMENT) if (tok->type == TOKEN_COMMENT)
@ -138,6 +161,14 @@ struct token *peek_token(struct lexer_context *ctx)
struct token *pop_token(struct lexer_context *ctx) struct token *pop_token(struct lexer_context *ctx)
{ {
stream_init(ctx);
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
struct token_info info = { true, 0 };
char *stream = ctx->end_previous_token;
ssize_t i = 0;
if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF) if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF)
{ {
// we reached end of input, frees all the token still allocated. // we reached end of input, frees all the token still allocated.
@ -145,12 +176,6 @@ struct token *pop_token(struct lexer_context *ctx)
free_token(&ctx->current_token); free_token(&ctx->current_token);
return NULL; return NULL;
} }
stream_init(ctx);
char *stream = ctx->end_previous_token;
ssize_t i = 0;
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
while (i < ctx->remaining_chars) while (i < ctx->remaining_chars)
{ {
@ -159,6 +184,7 @@ struct token *pop_token(struct lexer_context *ctx)
if (!update_lexing_mode(stream, i, &lexing_mode) if (!update_lexing_mode(stream, i, &lexing_mode)
&& lexing_mode == LEXER_NORMAL) && lexing_mode == LEXER_NORMAL)
{ {
update_flags(stream, i, &info);
if (is_special_char(stream, i)) if (is_special_char(stream, i))
{ {
if (i == 0) // where we create spe_char token if (i == 0) // where we create spe_char token
@ -184,7 +210,7 @@ struct token *pop_token(struct lexer_context *ctx)
// (this should never happen) // (this should never happen)
if (ctx->current_token == NULL) if (ctx->current_token == NULL)
{ {
ctx->current_token = new_token(stream, i, ctx->only_digits); ctx->current_token = new_token(stream, i, &info);
} }
save_state(stream, i, ctx); save_state(stream, i, ctx);

View file

@ -97,35 +97,43 @@ static void set_token_operator(struct token *tok, char *begin, ssize_t size)
{ {
if (tok->type != TOKEN_NULL) if (tok->type != TOKEN_NULL)
return; return;
if (strncmp(begin, ">", size) == 0) if (strncmp(begin, "||", size) == 0 && size == 2)
{
tok->type = TOKEN_OR;
}
else if (strncmp(begin, "&&", size) == 0 && size == 2)
{
tok->type = TOKEN_AND;
}
else if (strncmp(begin, ">", size) == 0 && size == 1)
{ {
tok->type = TOKEN_REDIR_RIGHT; tok->type = TOKEN_REDIR_RIGHT;
} }
else if (strncmp(begin, "<", size) == 0) else if (strncmp(begin, "<", size) == 0 && size == 1)
{ {
tok->type = TOKEN_REDIR_LEFT; tok->type = TOKEN_REDIR_LEFT;
} }
else if (strncmp(begin, ">>", size) == 0) else if (strncmp(begin, ">>", size) == 0 && size == 2)
{ {
tok->type = TOKEN_REDIR_DOUBLE_RIGHT; tok->type = TOKEN_REDIR_DOUBLE_RIGHT;
} }
else if (strncmp(begin, ">&", size) == 0) else if (strncmp(begin, ">&", size) == 0 && size == 2)
{ {
tok->type = TOKEN_REDIR_RIGHT_AMP; tok->type = TOKEN_REDIR_RIGHT_AMP;
} }
else if (strncmp(begin, ">|", size) == 0) else if (strncmp(begin, ">|", size) == 0 && size == 2)
{ {
tok->type = TOKEN_REDIR_RIGHT_PIPE; tok->type = TOKEN_REDIR_RIGHT_PIPE;
} }
else if (strncmp(begin, "<&", size) == 0) else if (strncmp(begin, "<&", size) == 0 && size == 2)
{ {
tok->type = TOKEN_REDIR_LEFT_AMP; tok->type = TOKEN_REDIR_LEFT_AMP;
} }
else if (strncmp(begin, "<>", size) == 0) else if (strncmp(begin, "<>", size) == 0 && size == 2)
{ {
tok->type = TOKEN_REDIR_LEFT_RIGHT; tok->type = TOKEN_REDIR_LEFT_RIGHT;
} }
else if (strncmp(begin, "|", size) == 0) else if (strncmp(begin, "|", size) == 0 && size == 1)
{ {
tok->type = TOKEN_PIPE; tok->type = TOKEN_PIPE;
} }
@ -146,6 +154,21 @@ static void set_token_word(struct token *tok, char *begin, ssize_t size)
} }
} }
/* @brief: Sets the token to an assignment_word
* Also allocates the data and fills it.
*/
static void set_token_assignment(struct token *tok, char *begin, ssize_t size)
{
if (tok->type == TOKEN_NULL && size != 0)
{
tok->type = TOKEN_ASSIGNMENT_WORD;
tok->data = calloc(size + 1, sizeof(char));
if (tok->data == NULL)
return;
strncpy(tok->data, begin, size);
}
}
/* @brief: Sets the token to an IO number /* @brief: Sets the token to an IO number
* Also allocates the data and fills it. * Also allocates the data and fills it.
*/ */
@ -204,19 +227,23 @@ bool is_special_char(char *stream, ssize_t i)
return strchr(special_chars, c) != NULL; return strchr(special_chars, c) != NULL;
} }
struct token *new_token(char *begin, ssize_t size, bool only_digits) struct token *new_token(char *begin, ssize_t size, struct token_info *info)
{ {
struct token *tok = calloc(1, sizeof(struct token)); struct token *tok = calloc(1, sizeof(struct token));
if (tok == NULL) if (tok == NULL)
return NULL; return NULL;
if (only_digits) if (info->only_digits)
set_token_ION(tok, begin, size); set_token_ION(tok, begin, size);
else if (info->has_equal)
set_token_operator(tok, begin, size); set_token_assignment(tok, begin, size);
set_token_spechar(tok, begin, size); else
set_token_keyword(tok, begin, size); {
set_token_word(tok, begin, size); set_token_operator(tok, begin, size);
set_token_spechar(tok, begin, size);
set_token_keyword(tok, begin, size);
set_token_word(tok, begin, size);
}
return tok; return tok;
} }
@ -267,6 +294,12 @@ ssize_t len_op_sepchar(char *stream, ssize_t i)
if (!is_special_char(stream, i)) if (!is_special_char(stream, i))
return -1; // should never happen return -1; // should never happen
if (stream[i] == '|' || stream[i + 1] == '|')
return 2; // OR ||
if (stream[i] == '&' && stream[i + 1] == '&')
return 2; // AND &&
if (stream[i] != '>' && stream[i] != '<') if (stream[i] != '>' && stream[i] != '<')
return 1; // special character (cannot be operator) return 1; // special character (cannot be operator)

View file

@ -10,10 +10,6 @@ struct lexer_context
char *end_previous_token; char *end_previous_token;
ssize_t remaining_chars; ssize_t remaining_chars;
// usefull to detect IO numbers.
// tells us if we only lexed digits in current token.
bool only_digits;
struct token *previous_token; struct token *previous_token;
struct token *current_token; struct token *current_token;
}; };
@ -34,10 +30,13 @@ enum token_type
// Blanks // Blanks
TOKEN_NULL = 0, TOKEN_NULL = 0,
TOKEN_EOF, TOKEN_EOF,
TOKEN_WORD,
TOKEN_NEWLINE, TOKEN_NEWLINE,
// SPecial characters // words
TOKEN_WORD,
TOKEN_ASSIGNMENT_WORD,
// Special characters
TOKEN_GRAVE, TOKEN_GRAVE,
TOKEN_SEMICOLON, TOKEN_SEMICOLON,
TOKEN_COMMENT, TOKEN_COMMENT,
@ -51,10 +50,6 @@ enum token_type
TOKEN_PIPE, TOKEN_PIPE,
TOKEN_NEGATION, TOKEN_NEGATION,
// TODO merge into one and use the data field
// (Too difficult to handle in the parser because of firsts)
// TOKEN_REDIRECTION
//
// Redirections // Redirections
TOKEN_REDIR_LEFT, TOKEN_REDIR_LEFT,
TOKEN_REDIR_RIGHT, TOKEN_REDIR_RIGHT,
@ -82,17 +77,29 @@ struct token
char *data; char *data;
}; };
// used to give info from lexing when creating a new token.
struct token_info
{
// usefull to detect IO numbers.
// tells us if we only lexed digits in current token.
bool only_digits;
// usefull to detect assignments, and syntax errors with '='.
bool has_equal;
};
/* @return: true if a special character from the grammar was found at stream[i], /* @return: true if a special character from the grammar was found at stream[i],
* false otherwise. * false otherwise.
*/ */
bool is_special_char(char *stream, ssize_t i); bool is_special_char(char *stream, ssize_t i);
/* @brief: return a newly allocated token, with the corresponding type. /* @brief: return a newly allocated token, with the type corresponding
* to the info given in arguments.
* The data contains [size] char, starting from [begin]. * The data contains [size] char, starting from [begin].
* *
* @return: NULL on error, a token otherwise. * @return: NULL on error, a token otherwise.
*/ */
struct token *new_token(char *begin, ssize_t size, bool only_digits); struct token *new_token(char *begin, ssize_t size, struct token_info *info);
/* @brief: frees the token given in argument /* @brief: frees the token given in argument
*/ */

View file

@ -106,15 +106,15 @@ bool grammar_init(void)
add_first(RULE_LIST, TOKEN_WORD); add_first(RULE_LIST, TOKEN_WORD);
add_first(RULE_LIST, TOKEN_IF); add_first(RULE_LIST, TOKEN_IF);
add_first(RULE_COMMAND, TOKEN_NEGATION); add_first(RULE_LIST, TOKEN_NEGATION);
add_first(RULE_AND_OR, TOKEN_WORD); add_first(RULE_AND_OR, TOKEN_WORD);
add_first(RULE_AND_OR, TOKEN_IF); add_first(RULE_AND_OR, TOKEN_IF);
add_first(RULE_COMMAND, TOKEN_NEGATION); add_first(RULE_AND_OR, TOKEN_NEGATION);
add_first(RULE_PIPELINE, TOKEN_WORD); add_first(RULE_PIPELINE, TOKEN_WORD);
add_first(RULE_PIPELINE, TOKEN_IF); add_first(RULE_PIPELINE, TOKEN_IF);
add_first(RULE_COMMAND, TOKEN_NEGATION); add_first(RULE_PIPELINE, TOKEN_NEGATION);
add_first(RULE_COMMAND, TOKEN_WORD); add_first(RULE_COMMAND, TOKEN_WORD);
add_first(RULE_COMMAND, TOKEN_IF); add_first(RULE_COMMAND, TOKEN_IF);

View file

@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "grammar.h"
#include "grammar_basic.h" #include "grammar_basic.h"
static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type) static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type)
@ -16,7 +17,16 @@ static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type)
return AST_REDIR_TYPE_LESS; return AST_REDIR_TYPE_LESS;
case TOKEN_REDIR_RIGHT: case TOKEN_REDIR_RIGHT:
return AST_REDIR_TYPE_GREAT; return AST_REDIR_TYPE_GREAT;
// TODO finish this case TOKEN_REDIR_DOUBLE_RIGHT:
return AST_REDIR_TYPE_DGREAT;
case TOKEN_REDIR_LEFT_RIGHT:
return AST_REDIR_TYPE_LESSGREAT;
case TOKEN_REDIR_LEFT_AMP:
return AST_REDIR_TYPE_LESSAND;
case TOKEN_REDIR_RIGHT_AMP:
return AST_REDIR_TYPE_GREATAND;
case TOKEN_REDIR_RIGHT_PIPE:
return AST_REDIR_TYPE_CLOBBER;
default: default:
return AST_REDIR_TYPE_NULL; return AST_REDIR_TYPE_NULL;
} }
@ -57,5 +67,17 @@ struct ast *parse_redirection(struct lexer_context *ctx)
struct ast *parse_prefix(struct lexer_context *ctx) struct ast *parse_prefix(struct lexer_context *ctx)
{ {
return parse_redirection(ctx); struct token *token = PEEK_TOKEN();
if (token->type == TOKEN_ASSIGNMENT_WORD)
{
token = POP_TOKEN();
return ast_create_assignment(token->data);
}
else if (is_first(*token, RULE_REDIRECTION))
return parse_redirection(ctx);
else
{
perror("Syntax error: expected a prefix (redirection or assignment)");
return NULL;
}
} }

View file

@ -152,14 +152,15 @@ struct ast *parse_pipeline(struct lexer_context *ctx)
struct ast *parse_command(struct lexer_context *ctx) struct ast *parse_command(struct lexer_context *ctx)
{ {
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
struct ast *result = NULL;
if (token->type == TOKEN_WORD) if (is_first(*token, RULE_SIMPLE_COMMAND))
{ {
return parse_simple_command(ctx); result = parse_simple_command(ctx);
} }
else if (token->type == TOKEN_IF) else if (is_first(*token, RULE_SHELL_COMMAND))
{ {
return parse_shell_command(ctx); parse_shell_command(ctx);
} }
else else
{ {
@ -168,64 +169,109 @@ struct ast *parse_command(struct lexer_context *ctx)
} }
} }
/* @brief: frees command_elements and redirections lists (helper func)
* @return: NULL
*/
static void *err_simple_command(struct list *command_elements,
struct list *redirections)
{
list_deep_destroy(command_elements);
list_deep_destroy(redirections);
return NULL;
}
struct ast *parse_simple_command(struct lexer_context *ctx) struct ast *parse_simple_command(struct lexer_context *ctx)
{ {
struct list *command_elements = NULL; struct list *command_elements = NULL;
struct list *redirections = NULL; // list of redirection ASTs
struct list *assignments = NULL;
bool has_prefix = false;
struct token *token = PEEK_TOKEN();
if (is_first(*token, RULE_PREFIX))
{
has_prefix = true;
while (is_first(*token, RULE_PREFIX))
{
struct ast *prefix = parse_prefix(ctx);
if (prefix == NULL)
{
return err_simple_command(command_elements, redirections);
}
if (prefix->type == AST_ASSIGNMENT)
{
assignments = list_append(assignments, prefix);
}
else if (prefix->type == AST_REDIR)
{
redirections = list_append(redirections, prefix);
}
token = PEEK_TOKEN();
}
}
// WORD
struct token *token = POP_TOKEN();
if (token->type != TOKEN_WORD) if (token->type != TOKEN_WORD)
{ {
perror("Expected a command but got a different token type"); if (!has_prefix)
return NULL; {
perror("Expected a command but got a different token type");
return err_simple_command(command_elements, redirections);
}
// else : only prefixes
} }
char *command = strdup(token->data); else
command_elements = list_append(command_elements, command);
token = PEEK_TOKEN();
// Eventual elements
while (is_first(*token, RULE_ELEMENT))
{ {
// Get element if (token->type == TOKEN_WORD)
struct ast *element = parse_element(ctx);
if (element == NULL)
{ {
list_deep_destroy(command_elements); char *command = strdup(token->data);
return NULL; command_elements = list_append(command_elements, command);
}
// Get element type POP_TOKEN();
if (ast_is_word(element)) token = PEEK_TOKEN();
{
struct ast_word *element_word = ast_get_word(element);
command_elements =
list_append(command_elements, element_word->word);
} }
else if (ast_is_redir(element)) // Eventual elements
while (is_first(*token, RULE_ELEMENT))
{ {
// TODO // Get element
perror("NOT IMPLEMENTED"); struct ast *element = parse_element(ctx);
return NULL; if (element == NULL)
} {
else return err_simple_command(command_elements, redirections);
{ }
perror("Internal error: unexpected return value from parse_element "
"in parse_simple_command");
list_deep_destroy(command_elements);
return NULL;
}
// Forward // Get element type
token = PEEK_TOKEN(); if (ast_is_word(element))
{
struct ast_word *element_word = ast_get_word(element);
// TODO test this fix for the memory leaks
char *word = strdup(element_word->word);
ast_free(&element);
command_elements = list_append(command_elements, word);
// end of fix
}
else if (ast_is_redir(element))
{
// append redirections to the list of redirections
redirections = list_append(redirections, element);
}
else
{
perror("Internal error: unexpected return value from "
"parse_element in parse_simple_command");
return err_simple_command(command_elements, redirections);
}
// Forward
token = PEEK_TOKEN();
}
} }
// Result struct ast *result =
struct ast *result = ast_create_command(command_elements); ast_create_command(command_elements, redirections, assignments);
if (result == NULL) if (result == NULL)
{ {
list_deep_destroy(command_elements); return err_simple_command(command_elements, redirections);
return NULL;
} }
return result; return result;
} }
@ -254,6 +300,18 @@ struct ast *parse_shell_command(struct lexer_context *ctx)
return parse_if_rule(ctx); return parse_if_rule(ctx);
} }
/* @brief: frees all the arguments. (helper func)
* @return: NULL.
*/
static void *err_if_rule(struct ast **cond, struct ast **then_clause,
struct ast **else_clause)
{
ast_free(cond);
ast_free(then_clause);
ast_free(else_clause);
return NULL;
}
struct ast *parse_if_rule(struct lexer_context *ctx) struct ast *parse_if_rule(struct lexer_context *ctx)
{ {
// If keyword // If keyword
@ -272,38 +330,34 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
token = POP_TOKEN(); token = POP_TOKEN();
if (token->type != TOKEN_THEN) if (token->type != TOKEN_THEN)
{ {
ast_free(&condition_content);
perror("Expected the 'then' keyword but token has different type"); perror("Expected the 'then' keyword but token has different type");
return NULL; return err_if_rule(&condition_content, NULL, NULL);
} }
// Then content // Then content
struct ast *then_content = parse_compound_list(ctx); struct ast *then_content = parse_compound_list(ctx);
if (then_content == NULL) if (then_content == NULL)
{ {
ast_free(&condition_content); return err_if_rule(&condition_content, &then_content, NULL);
ast_free(&then_content);
return NULL;
} }
struct ast *else_content = NULL;
// Eventual else/elif clause(s) // Eventual else/elif clause(s)
struct ast *else_content = parse_else_clause(ctx); if (is_first(*token, RULE_ELSE_CLAUSE))
if (else_content == NULL)
{ {
ast_free(&condition_content); else_content = parse_else_clause(ctx);
ast_free(&then_content); if (else_content == NULL)
return NULL; {
return err_if_rule(&condition_content, &then_content, NULL);
}
} }
// Fi keyword // Fi keyword
token = POP_TOKEN(); token = POP_TOKEN();
if (token->type != TOKEN_FI) if (token->type != TOKEN_FI)
{ {
ast_free(&condition_content);
ast_free(&then_content);
ast_free(&else_content);
perror("Expected the 'fi' keyword but token has different type"); perror("Expected the 'fi' keyword but token has different type");
return NULL; return err_if_rule(&condition_content, &then_content, &else_content);
} }
// Result // Result
@ -311,11 +365,8 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
ast_create_if(condition_content, then_content, else_content); ast_create_if(condition_content, then_content, else_content);
if (result == NULL) if (result == NULL)
{ {
ast_free(&condition_content);
ast_free(&then_content);
ast_free(&else_content);
perror("Internal error: could not create a new AST (AST_IF)"); perror("Internal error: could not create a new AST (AST_IF)");
return NULL; return err_if_rule(&condition_content, &then_content, &else_content);
} }
return result; return result;

View file

@ -2,6 +2,7 @@
#define AST_H #define AST_H
#include "ast_and_or.h" #include "ast_and_or.h"
#include "ast_assignment.h"
#include "ast_base.h" #include "ast_base.h"
#include "ast_command.h" #include "ast_command.h"
#include "ast_end.h" #include "ast_end.h"

View file

@ -0,0 +1,54 @@
#define _POSIX_C_SOURCE 200809L
#include "ast_assignment.h"
#include <stdlib.h>
bool ast_is_assignment(struct ast *node)
{
return node != NULL && node->type == AST_ASSIGNMENT;
}
struct ast_assignment *ast_get_assignment(struct ast *node)
{
if (node == NULL || node->type != AST_ASSIGNMENT)
return NULL;
return (struct ast_assignment *)node->data;
}
/* @brief: splits the assignement 'name=value' into 2 parts,
* and fills the fields of ast_assignment with it.
*/
static void init_assignments(struct ast_assignment *ast_assignment,
char *assignment)
{
if (assignment == NULL)
return;
char *split_pos = strchr(assignment, '=');
if (split_pos == NULL)
return;
*split_pos = '\0';
ast_assignment->name = strdup(assignment);
ast_assignment->value = strdup(split_pos + 1);
}
struct ast *ast_create_assignment(char *assignment)
{
struct ast_assignment *assignment_data =
calloc(1, sizeof(struct ast_assignment));
if (!assignment_data)
return NULL;
init_assignments(assignement_data);
return ast_create(AST_ASSIGNMENT, assignment_data);
}
void ast_free_assignment(struct ast_assignment *assignment_data)
{
if (assignment_data == NULL)
return;
free(assignment_data->name);
free(assignment_data->value);
free(assignment_data);
}

View file

@ -0,0 +1,17 @@
#ifndef AST_ASSIGNMENT_H
#define AST_ASSIGNMENT_H
#include "ast_base.h"
struct ast_assignment
{
char *name;
char *value;
};
bool ast_is_assignment(struct ast *node);
struct ast_assignment *ast_get_assignment(struct ast *node);
struct ast *ast_create_assignment(char *assignment);
void ast_free_assignment(struct ast_assignment *assignment_data);
#endif /* ! AST_ASSIGNMENT_H */

View file

@ -15,7 +15,8 @@ enum ast_type
AST_CMD, AST_CMD,
AST_WORD, AST_WORD,
AST_PIPE, AST_PIPE,
AST_NEG AST_NEG,
AST_ASSIGNMENT
}; };
struct ast struct ast

View file

@ -5,13 +5,15 @@
#include "../lists/lists.h" #include "../lists/lists.h"
struct ast *ast_create_command(struct list *command) struct ast *ast_create_command(struct list *command, struct list *redirections,
struct ast_list *assignments)
{ {
struct ast_command *command_data = malloc(sizeof(struct ast_command)); struct ast_command *command_data = malloc(sizeof(struct ast_command));
if (!command_data) if (!command_data)
return NULL; return NULL;
command_data->command = command; command_data->command = command;
command_data->redirections = redirections;
return ast_create(AST_CMD, command_data); return ast_create(AST_CMD, command_data);
} }
@ -33,5 +35,7 @@ void ast_free_command(struct ast_command *command_data)
if (command_data == NULL) if (command_data == NULL)
return; return;
list_deep_destroy(command_data->command); list_deep_destroy(command_data->command);
ast_list_deep_destroy(command_data->redirections);
ast_list_deep_destroy(command_data->assignments);
free(command_data); free(command_data);
} }

View file

@ -7,6 +7,8 @@
struct ast_command struct ast_command
{ {
struct list *command; // A list of words (char*) struct list *command; // A list of words (char*)
struct ast_list *redirections; // A list of ASTs, all ast_redir
struct list *assignments; // A list of ASTs, all ast_assignment
}; };
/** /**
@ -23,7 +25,8 @@ struct ast_command *ast_get_command(struct ast *node);
/** /**
* Creates a new AST node representing a command. * Creates a new AST node representing a command.
*/ */
struct ast *ast_create_command(struct list *command); struct ast *ast_create_command(struct list *command, struct list *redirections,
struct list *assignments);
/* /*
* @brief: frees the given ast_command and sets the pointer to NULL. * @brief: frees the given ast_command and sets the pointer to NULL.

View file

@ -8,7 +8,7 @@ enum ast_redir_type
AST_REDIR_TYPE_NULL, AST_REDIR_TYPE_NULL,
AST_REDIR_TYPE_LESS, // < AST_REDIR_TYPE_LESS, // <
AST_REDIR_TYPE_GREAT, // > AST_REDIR_TYPE_GREAT, // >
AST_REDIR_TYPE_DLESS, // << AST_REDIR_TYPE_LESSGREAT, // <>
AST_REDIR_TYPE_DGREAT, // >> AST_REDIR_TYPE_DGREAT, // >>
AST_REDIR_TYPE_LESSAND, // <& AST_REDIR_TYPE_LESSAND, // <&
AST_REDIR_TYPE_GREATAND, // >& AST_REDIR_TYPE_GREATAND, // >&
@ -21,6 +21,7 @@ struct ast_redir
int io_number; // The FD being redirected (default -1 if not specified, int io_number; // The FD being redirected (default -1 if not specified,
// implies 0 or 1 based on type) // implies 0 or 1 based on type)
enum ast_redir_type type; enum ast_redir_type type;
int saved_fd; // To store the original FD for restoration (-1 before save)
}; };
bool ast_is_redir(struct ast *node); bool ast_is_redir(struct ast *node);