fix: merge local changes

This commit is contained in:
Gu://em_ 2026-01-30 20:58:15 +01:00
commit f721e6db72
19 changed files with 568 additions and 104 deletions

View file

@ -137,10 +137,7 @@ static int exec_assignment(struct list *assignment_list, struct hash_map *vars)
struct ast_assignment *assignment =
ast_get_assignment(assignment_list->data);
char *key = strdup(assignment->name);
char *value = strdup(assignment->value);
hash_map_insert(vars, key, value, NULL);
set_var_copy(vars, assignment->name, assignment->value);
assignment_list = assignment_list->next;
}
return 0;
@ -148,7 +145,6 @@ static int exec_assignment(struct list *assignment_list, struct hash_map *vars)
int exec_ast_command(struct ast_command *command, struct hash_map *vars)
{
(void)vars;
exec_assignment(command->assignments, vars);
set_all_redir(command->redirections);

View file

@ -10,5 +10,4 @@ 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);
void unset_all_redir(struct list *redir_list);
#endif // EXECUTION_HELPERS_H

View file

@ -72,10 +72,8 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
tok->type = TOKEN_ELSE;
else if (strncmp(begin, "elif", size) == 0 && size == 4)
tok->type = TOKEN_ELIF;
else if (strncmp(begin, "&&", size) == 0 && size == 2)
tok->type = TOKEN_AND;
else if (strncmp(begin, "||", size) == 0 && size == 2)
tok->type = TOKEN_OR;
else if (strncmp(begin, "export", size) == 0 && size == 6)
tok->type = TOKEN_EXPORT;
// no keywords found.
if (tok->type == TOKEN_NULL)
@ -97,35 +95,44 @@ static void set_token_operator(struct token *tok, char *begin, ssize_t size)
{
if (tok->type != TOKEN_NULL)
return;
if (strncmp(begin, ">", size) == 0)
if (strncmp(begin, "&&", size) == 0 && size == 2)
{
tok->type = TOKEN_AND;
}
else if (strncmp(begin, "||", size) == 0 && size == 2)
{
tok->type = TOKEN_OR;
}
else if (strncmp(begin, ">", size) == 0 && size == 1)
{
tok->type = TOKEN_REDIR_RIGHT;
}
else if (strncmp(begin, "<", size) == 0)
else if (strncmp(begin, "<", size) == 0 && size == 1)
{
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;
}
else if (strncmp(begin, ">&", size) == 0)
else if (strncmp(begin, ">&", size) == 0 && size == 2)
{
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;
}
else if (strncmp(begin, "<&", size) == 0)
else if (strncmp(begin, "<&", size) == 0 && size == 2)
{
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;
}
else if (strncmp(begin, "|", size) == 0)
else if (strncmp(begin, "|", size) == 0 && size == 1)
{
tok->type = TOKEN_PIPE;
}
@ -231,9 +238,9 @@ struct token *new_token(char *begin, ssize_t size, struct token_info *info)
set_token_assignment(tok, begin, size);
else
{
set_token_keyword(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);
}
@ -286,10 +293,19 @@ ssize_t len_op_sepchar(char *stream, ssize_t i)
if (!is_special_char(stream, i))
return -1; // should never happen
if (stream[i] != '>' && stream[i] != '<')
return 1; // special character (cannot be operator)
// OR
if (stream[i] == '|' && stream[i + 1] == '|')
return 2;
// operator
// AND
if (stream[i] == '|' && stream[i + 1] == '|')
return 2;
// special chars
if (stream[i] != '>' && stream[i] != '<')
return 1;
// REDIRS
if (stream[i] == '<')
{

View file

@ -72,9 +72,10 @@ enum token_type
TOKEN_FOR,
TOKEN_WHILE,
TOKEN_UNTIL,
TOKEN_CASE,
TOKEN_EXPORT,
TOKEN_DO,
TOKEN_DONE,
TOKEN_CASE
TOKEN_DONE
};
struct token

View file

@ -89,9 +89,21 @@ static bool init_firsts_map(void)
return true;
}
static void add_first_redir(void)
{
add_first(RULE_REDIRECTION, TOKEN_IONUMBER);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_DOUBLE_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_PIPE);
}
// === Functions
bool grammar_init(void)
int grammar_init(void)
{
// Initialize the firsts map
bool success = init_firsts_map();
@ -128,15 +140,9 @@ bool grammar_init(void)
add_firsts(RULE_CASE_CLAUSE, first(RULE_CASE_ITEM));
// Redirection
add_first(RULE_REDIRECTION, TOKEN_IONUMBER);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_DOUBLE_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_PIPE);
add_first_redir();
// %RIP Matteo 30/01/2026
// %RAX Guillem 30/01/2026 hehe
// Element
add_first(RULE_ELEMENT, TOKEN_WORD);
@ -153,6 +159,7 @@ bool grammar_init(void)
// Simple command
add_firsts(RULE_SIMPLE_COMMAND, first(RULE_PREFIX));
add_first(RULE_SIMPLE_COMMAND, TOKEN_WORD);
add_first(RULE_SIMPLE_COMMAND, TOKEN_EXPORT);
// Funcdec
add_first(RULE_FUNCDEC, TOKEN_WORD);

View file

@ -64,7 +64,7 @@ struct firsts_list
* @return PARSER_INIT_SUCCESS on success PARSER_INIT_ERROR on error
* @warning Do not use outside the parser
*/
bool grammar_init(void);
int grammar_init(void);
/*
* @brief Closes the grammar submodule

View file

@ -138,7 +138,7 @@ struct ast *parse_prefix(struct lexer_context *ctx)
if (token->type == TOKEN_ASSIGNMENT_WORD)
{
token = POP_TOKEN();
return ast_create_assignment(token->data);
return ast_create_assignment(token->data, false);
}
else if (is_first(*token, RULE_REDIRECTION))
return parse_redirection(ctx);

View file

@ -26,17 +26,6 @@ static enum ast_and_or_type and_or_tok_to_ast(enum token_type tok_type)
}
}
/* @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;
}
/* @brief: frees all the arguments. (helper func)
* @return: NULL.
*/
@ -49,6 +38,46 @@ static void *err_if_rule(struct ast **cond, struct ast **then_clause,
return NULL;
}
/* @brief: frees command_elements and redirections lists (helper func)
* @return: NULL
*/
static void *err_s_com(struct list *command_elements, struct list *redirections,
struct list *assignments)
{
list_deep_destroy(command_elements);
list_deep_destroy(redirections);
list_deep_destroy(assignments);
return NULL;
}
/* @brief: used when export keyword is found, and expects an assignment after.
* @return: an ast_assignment with the field [global] set to true.
*/
static struct ast *parse_export(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
if (token->type != TOKEN_EXPORT)
{
fprintf(stderr, "expected the export keyword in parse_export");
return NULL;
}
// export
POP_TOKEN();
token = PEEK_TOKEN();
if (token->type != TOKEN_ASSIGNMENT_WORD)
{
fprintf(stderr, "in parser: export must be followed by 'x=y'");
return NULL;
}
// assignment
POP_TOKEN();
return ast_create_assignment(token->data, true);
}
// === Functions
struct ast *parse_list(struct lexer_context *ctx)
@ -224,7 +253,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
struct ast *prefix = parse_prefix(ctx);
if (prefix == NULL)
{
return err_simple_command(command_elements, redirections);
return err_s_com(command_elements, redirections, assignments);
}
if (prefix->type == AST_ASSIGNMENT)
{
@ -240,66 +269,70 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
if (token->type != TOKEN_WORD)
{
if (!has_prefix)
if (!has_prefix && token->type != TOKEN_EXPORT)
{
perror("Expected a command but got a different token type");
return err_simple_command(command_elements, redirections);
return err_s_com(command_elements, redirections, assignments);
}
if (token->type == TOKEN_EXPORT)
{
struct ast *assignment_export = parse_export(ctx);
if (assignment_export == NULL)
return err_s_com(command_elements, redirections, assignments);
assignments = list_append(assignments, assignment_export);
}
// else : only prefixes
}
else
else // TOKEN WORD
{
if (token->type == TOKEN_WORD)
char *command = strdup(token->data);
command_elements = list_append(command_elements, command);
POP_TOKEN();
}
token = PEEK_TOKEN();
// Eventual elements
while (is_first(*token, RULE_ELEMENT))
{
// Get element
struct ast *element = parse_element(ctx);
if (element == NULL)
{
char *command = strdup(token->data);
command_elements = list_append(command_elements, command);
POP_TOKEN();
token = PEEK_TOKEN();
return err_s_com(command_elements, redirections, assignments);
}
// Eventual elements
while (is_first(*token, RULE_ELEMENT))
// Get element type
if (ast_is_word(element))
{
// Get element
struct ast *element = parse_element(ctx);
if (element == NULL)
{
return err_simple_command(command_elements, redirections);
}
struct ast_word *element_word = ast_get_word(element);
// Get element type
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();
// 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_s_com(command_elements, redirections, assignments);
}
// Forward
token = PEEK_TOKEN();
}
struct ast *result =
ast_create_command(command_elements, redirections, assignments);
if (result == NULL)
{
return err_simple_command(command_elements, redirections);
return err_s_com(command_elements, redirections, assignments);
}
return result;
}
@ -334,6 +367,10 @@ struct ast *parse_element(struct lexer_context *ctx)
{
return parse_redirection(ctx);
}
else if (token->type == TOKEN_EXPORT)
{
return parse_export(ctx);
}
else
{
perror("Syntax error: unexpected token at parse_element");
@ -531,12 +568,11 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
token = POP_TOKEN();
if (token->type != TOKEN_THEN)
{
perror(
"Expected the 'then' keyword but got a different token type");
perror("Expected the 'then' keyword but got a different token "
"type");
return NULL;
}
// Then clause
struct ast *then_content = parse_compound_list(ctx);
if (then_content == NULL)
{

View file

@ -10,12 +10,12 @@ static enum parser_state state = PARSER_STATE_NOT_INITIALIZED;
// === Functions
bool parser_init(void)
int parser_init(void)
{
if (state == PARSER_STATE_READY)
{
perror("Internal error: tried to initialize the parser module twice.");
return NULL;
return false;
}
int success = grammar_init();
if (success == false)

View file

@ -18,7 +18,7 @@ enum parser_state
*
* @return Returns false on error and true on success
*/
bool parser_init(void);
int parser_init(void);
/* @brief Closes the parser module after use
*/

View file

@ -121,7 +121,7 @@ int args_handler(int argc, char **argv, struct args_options *options,
}
args_in_var(vars, args_list);
list_destroy(args_list);
list_destroy(&args_list);
if (options->type == INPUT_UNDEFINED)
options->type = INPUT_STDIN;

View file

@ -34,7 +34,7 @@ static void init_assignments(struct ast_assignment *ast_assignment,
ast_assignment->value = strdup(split_pos + 1);
}
struct ast *ast_create_assignment(char *assignment)
struct ast *ast_create_assignment(char *assignment, bool global)
{
struct ast_assignment *assignment_data =
calloc(1, sizeof(struct ast_assignment));
@ -42,6 +42,7 @@ struct ast *ast_create_assignment(char *assignment)
return NULL;
init_assignments(assignment_data, assignment);
assignment_data->global = global;
return ast_create(AST_ASSIGNMENT, assignment_data);
}

View file

@ -7,11 +7,12 @@ struct ast_assignment
{
char *name;
char *value;
bool global;
};
bool ast_is_assignment(struct ast *node);
struct ast_assignment *ast_get_assignment(struct ast *node);
struct ast *ast_create_assignment(char *assignment);
struct ast *ast_create_assignment(char *assignment, bool global);
void ast_free_assignment(struct ast_assignment *assignment_data);
#endif /* ! AST_ASSIGNMENT_H */

View file

@ -31,7 +31,7 @@ void list_print(struct list *list);
** Release the memory used by the list.
** Does nothing if `list` is `NULL`.
*/
void list_destroy(struct list *list);
void list_destroy(struct list **list);
/*
** Release the memory used by the list and its content

View file

@ -49,9 +49,9 @@ void list_print(struct list *list)
}
}
void list_destroy(struct list *list)
void list_destroy(struct list **list)
{
struct list *elt = list;
struct list *elt = *list;
struct list *next_elt;
while (elt != NULL)
{
@ -59,6 +59,7 @@ void list_destroy(struct list *list)
free(elt);
elt = next_elt;
}
*list = NULL;
}
struct list *list_append(struct list *list, void *value)