fix: merge local changes
This commit is contained in:
commit
f721e6db72
19 changed files with 568 additions and 104 deletions
|
|
@ -32,7 +32,7 @@ run_cmd "Running autoreconf" autoreconf --force --verbose --install
|
||||||
if [[ "$(uname)" == "Darwin" ]]; then
|
if [[ "$(uname)" == "Darwin" ]]; then
|
||||||
run_cmd "Configuring for MacOS" ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -I/opt/homebrew/include' LDFLAGS='-L/opt/homebrew/lib'
|
run_cmd "Configuring for MacOS" ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -I/opt/homebrew/include' LDFLAGS='-L/opt/homebrew/lib'
|
||||||
else
|
else
|
||||||
run_cmd "Configuring for Linux" ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g'
|
run_cmd "Configuring for Linux" ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g -fsanitize=address'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
run_cmd "Cleaning build" make clean
|
run_cmd "Cleaning build" make clean
|
||||||
|
|
|
||||||
|
|
@ -137,10 +137,7 @@ static int exec_assignment(struct list *assignment_list, struct hash_map *vars)
|
||||||
struct ast_assignment *assignment =
|
struct ast_assignment *assignment =
|
||||||
ast_get_assignment(assignment_list->data);
|
ast_get_assignment(assignment_list->data);
|
||||||
|
|
||||||
char *key = strdup(assignment->name);
|
set_var_copy(vars, assignment->name, assignment->value);
|
||||||
char *value = strdup(assignment->value);
|
|
||||||
hash_map_insert(vars, key, value, NULL);
|
|
||||||
|
|
||||||
assignment_list = assignment_list->next;
|
assignment_list = assignment_list->next;
|
||||||
}
|
}
|
||||||
return 0;
|
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)
|
int exec_ast_command(struct ast_command *command, struct hash_map *vars)
|
||||||
{
|
{
|
||||||
(void)vars;
|
|
||||||
exec_assignment(command->assignments, vars);
|
exec_assignment(command->assignments, vars);
|
||||||
set_all_redir(command->redirections);
|
set_all_redir(command->redirections);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
int exec_ast_and_or(struct ast_and_or *ao_node, struct hash_map *vars);
|
||||||
|
|
||||||
void unset_all_redir(struct list *redir_list);
|
void unset_all_redir(struct list *redir_list);
|
||||||
|
|
||||||
#endif // EXECUTION_HELPERS_H
|
#endif // EXECUTION_HELPERS_H
|
||||||
|
|
|
||||||
|
|
@ -72,10 +72,8 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
|
||||||
tok->type = TOKEN_ELSE;
|
tok->type = TOKEN_ELSE;
|
||||||
else if (strncmp(begin, "elif", size) == 0 && size == 4)
|
else if (strncmp(begin, "elif", size) == 0 && size == 4)
|
||||||
tok->type = TOKEN_ELIF;
|
tok->type = TOKEN_ELIF;
|
||||||
else if (strncmp(begin, "&&", size) == 0 && size == 2)
|
else if (strncmp(begin, "export", size) == 0 && size == 6)
|
||||||
tok->type = TOKEN_AND;
|
tok->type = TOKEN_EXPORT;
|
||||||
else if (strncmp(begin, "||", size) == 0 && size == 2)
|
|
||||||
tok->type = TOKEN_OR;
|
|
||||||
|
|
||||||
// no keywords found.
|
// no keywords found.
|
||||||
if (tok->type == TOKEN_NULL)
|
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)
|
if (tok->type != TOKEN_NULL)
|
||||||
return;
|
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;
|
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;
|
||||||
}
|
}
|
||||||
|
|
@ -231,9 +238,9 @@ struct token *new_token(char *begin, ssize_t size, struct token_info *info)
|
||||||
set_token_assignment(tok, begin, size);
|
set_token_assignment(tok, begin, size);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
set_token_keyword(tok, begin, size);
|
||||||
set_token_operator(tok, begin, size);
|
set_token_operator(tok, begin, size);
|
||||||
set_token_spechar(tok, begin, size);
|
set_token_spechar(tok, begin, size);
|
||||||
set_token_keyword(tok, begin, size);
|
|
||||||
set_token_word(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))
|
if (!is_special_char(stream, i))
|
||||||
return -1; // should never happen
|
return -1; // should never happen
|
||||||
|
|
||||||
if (stream[i] != '>' && stream[i] != '<')
|
// OR
|
||||||
return 1; // special character (cannot be operator)
|
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] == '<')
|
if (stream[i] == '<')
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -72,9 +72,10 @@ enum token_type
|
||||||
TOKEN_FOR,
|
TOKEN_FOR,
|
||||||
TOKEN_WHILE,
|
TOKEN_WHILE,
|
||||||
TOKEN_UNTIL,
|
TOKEN_UNTIL,
|
||||||
|
TOKEN_CASE,
|
||||||
|
TOKEN_EXPORT,
|
||||||
TOKEN_DO,
|
TOKEN_DO,
|
||||||
TOKEN_DONE,
|
TOKEN_DONE
|
||||||
TOKEN_CASE
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct token
|
struct token
|
||||||
|
|
|
||||||
|
|
@ -89,9 +89,21 @@ static bool init_firsts_map(void)
|
||||||
return true;
|
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
|
// === Functions
|
||||||
|
|
||||||
bool grammar_init(void)
|
int grammar_init(void)
|
||||||
{
|
{
|
||||||
// Initialize the firsts map
|
// Initialize the firsts map
|
||||||
bool success = init_firsts_map();
|
bool success = init_firsts_map();
|
||||||
|
|
@ -128,15 +140,9 @@ bool grammar_init(void)
|
||||||
add_firsts(RULE_CASE_CLAUSE, first(RULE_CASE_ITEM));
|
add_firsts(RULE_CASE_CLAUSE, first(RULE_CASE_ITEM));
|
||||||
|
|
||||||
// Redirection
|
// Redirection
|
||||||
add_first(RULE_REDIRECTION, TOKEN_IONUMBER);
|
add_first_redir();
|
||||||
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);
|
|
||||||
// %RIP Matteo 30/01/2026
|
// %RIP Matteo 30/01/2026
|
||||||
|
// %RAX Guillem 30/01/2026 hehe
|
||||||
|
|
||||||
// Element
|
// Element
|
||||||
add_first(RULE_ELEMENT, TOKEN_WORD);
|
add_first(RULE_ELEMENT, TOKEN_WORD);
|
||||||
|
|
@ -153,6 +159,7 @@ bool grammar_init(void)
|
||||||
// Simple command
|
// Simple command
|
||||||
add_firsts(RULE_SIMPLE_COMMAND, first(RULE_PREFIX));
|
add_firsts(RULE_SIMPLE_COMMAND, first(RULE_PREFIX));
|
||||||
add_first(RULE_SIMPLE_COMMAND, TOKEN_WORD);
|
add_first(RULE_SIMPLE_COMMAND, TOKEN_WORD);
|
||||||
|
add_first(RULE_SIMPLE_COMMAND, TOKEN_EXPORT);
|
||||||
|
|
||||||
// Funcdec
|
// Funcdec
|
||||||
add_first(RULE_FUNCDEC, TOKEN_WORD);
|
add_first(RULE_FUNCDEC, TOKEN_WORD);
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ struct firsts_list
|
||||||
* @return PARSER_INIT_SUCCESS on success PARSER_INIT_ERROR on error
|
* @return PARSER_INIT_SUCCESS on success PARSER_INIT_ERROR on error
|
||||||
* @warning Do not use outside the parser
|
* @warning Do not use outside the parser
|
||||||
*/
|
*/
|
||||||
bool grammar_init(void);
|
int grammar_init(void);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief Closes the grammar submodule
|
* @brief Closes the grammar submodule
|
||||||
|
|
|
||||||
|
|
@ -138,7 +138,7 @@ struct ast *parse_prefix(struct lexer_context *ctx)
|
||||||
if (token->type == TOKEN_ASSIGNMENT_WORD)
|
if (token->type == TOKEN_ASSIGNMENT_WORD)
|
||||||
{
|
{
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
return ast_create_assignment(token->data);
|
return ast_create_assignment(token->data, false);
|
||||||
}
|
}
|
||||||
else if (is_first(*token, RULE_REDIRECTION))
|
else if (is_first(*token, RULE_REDIRECTION))
|
||||||
return parse_redirection(ctx);
|
return parse_redirection(ctx);
|
||||||
|
|
|
||||||
|
|
@ -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)
|
/* @brief: frees all the arguments. (helper func)
|
||||||
* @return: NULL.
|
* @return: NULL.
|
||||||
*/
|
*/
|
||||||
|
|
@ -49,6 +38,46 @@ static void *err_if_rule(struct ast **cond, struct ast **then_clause,
|
||||||
return NULL;
|
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
|
// === Functions
|
||||||
|
|
||||||
struct ast *parse_list(struct lexer_context *ctx)
|
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);
|
struct ast *prefix = parse_prefix(ctx);
|
||||||
if (prefix == NULL)
|
if (prefix == NULL)
|
||||||
{
|
{
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_s_com(command_elements, redirections, assignments);
|
||||||
}
|
}
|
||||||
if (prefix->type == AST_ASSIGNMENT)
|
if (prefix->type == AST_ASSIGNMENT)
|
||||||
{
|
{
|
||||||
|
|
@ -240,23 +269,28 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
|
|
||||||
if (token->type != TOKEN_WORD)
|
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");
|
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);
|
||||||
}
|
}
|
||||||
// else : only prefixes
|
if (token->type == TOKEN_EXPORT)
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
if (token->type == TOKEN_WORD)
|
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 // TOKEN WORD
|
||||||
{
|
{
|
||||||
char *command = strdup(token->data);
|
char *command = strdup(token->data);
|
||||||
command_elements = list_append(command_elements, command);
|
command_elements = list_append(command_elements, command);
|
||||||
|
|
||||||
POP_TOKEN();
|
POP_TOKEN();
|
||||||
token = PEEK_TOKEN();
|
|
||||||
}
|
}
|
||||||
|
token = PEEK_TOKEN();
|
||||||
// Eventual elements
|
// Eventual elements
|
||||||
while (is_first(*token, RULE_ELEMENT))
|
while (is_first(*token, RULE_ELEMENT))
|
||||||
{
|
{
|
||||||
|
|
@ -264,7 +298,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
struct ast *element = parse_element(ctx);
|
struct ast *element = parse_element(ctx);
|
||||||
if (element == NULL)
|
if (element == NULL)
|
||||||
{
|
{
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_s_com(command_elements, redirections, assignments);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get element type
|
// Get element type
|
||||||
|
|
@ -287,19 +321,18 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
perror("Internal error: unexpected return value from "
|
perror("Internal error: unexpected return value from "
|
||||||
"parse_element in parse_simple_command");
|
"parse_element in parse_simple_command");
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_s_com(command_elements, redirections, assignments);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forward
|
// Forward
|
||||||
token = PEEK_TOKEN();
|
token = PEEK_TOKEN();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
struct ast *result =
|
struct ast *result =
|
||||||
ast_create_command(command_elements, redirections, assignments);
|
ast_create_command(command_elements, redirections, assignments);
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
{
|
{
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_s_com(command_elements, redirections, assignments);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -334,6 +367,10 @@ struct ast *parse_element(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
return parse_redirection(ctx);
|
return parse_redirection(ctx);
|
||||||
}
|
}
|
||||||
|
else if (token->type == TOKEN_EXPORT)
|
||||||
|
{
|
||||||
|
return parse_export(ctx);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
perror("Syntax error: unexpected token at parse_element");
|
perror("Syntax error: unexpected token at parse_element");
|
||||||
|
|
@ -531,12 +568,11 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
if (token->type != TOKEN_THEN)
|
if (token->type != TOKEN_THEN)
|
||||||
{
|
{
|
||||||
perror(
|
perror("Expected the 'then' keyword but got a different token "
|
||||||
"Expected the 'then' keyword but got a different token type");
|
"type");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then clause
|
|
||||||
struct ast *then_content = parse_compound_list(ctx);
|
struct ast *then_content = parse_compound_list(ctx);
|
||||||
if (then_content == NULL)
|
if (then_content == NULL)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -10,12 +10,12 @@ static enum parser_state state = PARSER_STATE_NOT_INITIALIZED;
|
||||||
|
|
||||||
// === Functions
|
// === Functions
|
||||||
|
|
||||||
bool parser_init(void)
|
int parser_init(void)
|
||||||
{
|
{
|
||||||
if (state == PARSER_STATE_READY)
|
if (state == PARSER_STATE_READY)
|
||||||
{
|
{
|
||||||
perror("Internal error: tried to initialize the parser module twice.");
|
perror("Internal error: tried to initialize the parser module twice.");
|
||||||
return NULL;
|
return false;
|
||||||
}
|
}
|
||||||
int success = grammar_init();
|
int success = grammar_init();
|
||||||
if (success == false)
|
if (success == false)
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ enum parser_state
|
||||||
*
|
*
|
||||||
* @return Returns false on error and true on success
|
* @return Returns false on error and true on success
|
||||||
*/
|
*/
|
||||||
bool parser_init(void);
|
int parser_init(void);
|
||||||
|
|
||||||
/* @brief Closes the parser module after use
|
/* @brief Closes the parser module after use
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,7 @@ int args_handler(int argc, char **argv, struct args_options *options,
|
||||||
}
|
}
|
||||||
|
|
||||||
args_in_var(vars, args_list);
|
args_in_var(vars, args_list);
|
||||||
list_destroy(args_list);
|
list_destroy(&args_list);
|
||||||
|
|
||||||
if (options->type == INPUT_UNDEFINED)
|
if (options->type == INPUT_UNDEFINED)
|
||||||
options->type = INPUT_STDIN;
|
options->type = INPUT_STDIN;
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ static void init_assignments(struct ast_assignment *ast_assignment,
|
||||||
ast_assignment->value = strdup(split_pos + 1);
|
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 =
|
struct ast_assignment *assignment_data =
|
||||||
calloc(1, sizeof(struct ast_assignment));
|
calloc(1, sizeof(struct ast_assignment));
|
||||||
|
|
@ -42,6 +42,7 @@ struct ast *ast_create_assignment(char *assignment)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
init_assignments(assignment_data, assignment);
|
init_assignments(assignment_data, assignment);
|
||||||
|
assignment_data->global = global;
|
||||||
return ast_create(AST_ASSIGNMENT, assignment_data);
|
return ast_create(AST_ASSIGNMENT, assignment_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,12 @@ struct ast_assignment
|
||||||
{
|
{
|
||||||
char *name;
|
char *name;
|
||||||
char *value;
|
char *value;
|
||||||
|
bool global;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool ast_is_assignment(struct ast *node);
|
bool ast_is_assignment(struct ast *node);
|
||||||
struct ast_assignment *ast_get_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);
|
void ast_free_assignment(struct ast_assignment *assignment_data);
|
||||||
|
|
||||||
#endif /* ! AST_ASSIGNMENT_H */
|
#endif /* ! AST_ASSIGNMENT_H */
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ void list_print(struct list *list);
|
||||||
** Release the memory used by the list.
|
** Release the memory used by the list.
|
||||||
** Does nothing if `list` is `NULL`.
|
** 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
|
** Release the memory used by the list and its content
|
||||||
|
|
|
||||||
|
|
@ -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;
|
struct list *next_elt;
|
||||||
while (elt != NULL)
|
while (elt != NULL)
|
||||||
{
|
{
|
||||||
|
|
@ -59,6 +59,7 @@ void list_destroy(struct list *list)
|
||||||
free(elt);
|
free(elt);
|
||||||
elt = next_elt;
|
elt = next_elt;
|
||||||
}
|
}
|
||||||
|
*list = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct list *list_append(struct list *list, void *value)
|
struct list *list_append(struct list *list, void *value)
|
||||||
|
|
|
||||||
17
tests/and_ors.sh
Executable file
17
tests/and_ors.sh
Executable file
|
|
@ -0,0 +1,17 @@
|
||||||
|
true
|
||||||
|
echo 'true =' $?
|
||||||
|
|
||||||
|
false
|
||||||
|
echo 'false =' $?
|
||||||
|
|
||||||
|
false && true
|
||||||
|
echo 'false && true =' $?
|
||||||
|
|
||||||
|
true && false
|
||||||
|
echo 'true && false =' $?
|
||||||
|
|
||||||
|
true || false
|
||||||
|
echo 'true || false =' $?
|
||||||
|
|
||||||
|
false || true
|
||||||
|
echo 'false || true =' $?
|
||||||
305
tests/unit/utils/lists.c
Normal file
305
tests/unit/utils/lists.c
Normal file
|
|
@ -0,0 +1,305 @@
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
|
||||||
|
#include "../../../src/utils/lists/lists.h"
|
||||||
|
|
||||||
|
#include <criterion/criterion.h>
|
||||||
|
#include <criterion/new/assert.h>
|
||||||
|
#include <criterion/redirect.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
TestSuite(lists);
|
||||||
|
|
||||||
|
Test(lists, append_empty)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_append(lst, (void *)1);
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)1);
|
||||||
|
cr_expect(lst->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 1);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, prepend_empty)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_prepend(lst, (void *)1);
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)1);
|
||||||
|
cr_expect(lst->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 1);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, insert_empty)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_insert(lst, (void *)1, 0);
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)1);
|
||||||
|
cr_expect(lst->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 1);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, insert_out_of_bounds)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_insert(lst, (void *)1, 5);
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)1);
|
||||||
|
cr_expect(lst->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 1);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, remove_out_of_bounds)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_append(lst, (void *)1);
|
||||||
|
lst = list_remove(lst, 5);
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)1);
|
||||||
|
cr_expect(lst->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 1);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, append_multiple)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_append(lst, (void *)1);
|
||||||
|
lst = list_append(lst, (void *)2);
|
||||||
|
lst = list_append(lst, (void *)3);
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)1);
|
||||||
|
cr_expect(lst->next->data == (void *)2);
|
||||||
|
cr_expect(lst->next->next->data == (void *)3);
|
||||||
|
cr_expect(lst->next->next->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 3);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, prepend_multiple)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_prepend(lst, (void *)1);
|
||||||
|
lst = list_prepend(lst, (void *)2);
|
||||||
|
lst = list_prepend(lst, (void *)3);
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)3);
|
||||||
|
cr_expect(lst->next->data == (void *)2);
|
||||||
|
cr_expect(lst->next->next->data == (void *)1);
|
||||||
|
cr_expect(lst->next->next->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 3);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, insert_multiple)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_insert(lst, (void *)1, 0);
|
||||||
|
lst = list_insert(lst, (void *)3, 1);
|
||||||
|
lst = list_insert(lst, (void *)2, 1);
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)1);
|
||||||
|
cr_expect(lst->next->data == (void *)2);
|
||||||
|
cr_expect(lst->next->next->data == (void *)3);
|
||||||
|
cr_expect(lst->next->next->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 3);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, append)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_prepend(lst, (void *)2);
|
||||||
|
lst = list_prepend(lst, (void *)1);
|
||||||
|
|
||||||
|
lst = list_append(lst, (void *)3);
|
||||||
|
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)1);
|
||||||
|
cr_expect(lst->next->data == (void *)2);
|
||||||
|
cr_expect(lst->next->next->data == (void *)3);
|
||||||
|
cr_expect(lst->next->next->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 3);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, prepend)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_append(lst, (void *)1);
|
||||||
|
lst = list_append(lst, (void *)2);
|
||||||
|
|
||||||
|
lst = list_prepend(lst, (void *)0);
|
||||||
|
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)0);
|
||||||
|
cr_expect(lst->next->data == (void *)1);
|
||||||
|
cr_expect(lst->next->next->data == (void *)2);
|
||||||
|
cr_expect(lst->next->next->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 3);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, insert)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_append(lst, (void *)1);
|
||||||
|
lst = list_append(lst, (void *)3);
|
||||||
|
|
||||||
|
lst = list_insert(lst, (void *)2, 1);
|
||||||
|
lst = list_insert(lst, (void *)0, 0);
|
||||||
|
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)0);
|
||||||
|
cr_expect(lst->next->data == (void *)1);
|
||||||
|
cr_expect(lst->next->next->data == (void *)2);
|
||||||
|
cr_expect(lst->next->next->next->data == (void *)3);
|
||||||
|
cr_expect(lst->next->next->next->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 4);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, remove)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_append(lst, (void *)1);
|
||||||
|
lst = list_append(lst, (void *)2);
|
||||||
|
lst = list_append(lst, (void *)3);
|
||||||
|
lst = list_append(lst, (void *)4);
|
||||||
|
|
||||||
|
lst = list_remove(lst, 1); // remove 2
|
||||||
|
lst = list_remove(lst, 2); // remove 4
|
||||||
|
lst = list_remove(lst, 0); // remove 1
|
||||||
|
|
||||||
|
cr_expect(lst != NULL);
|
||||||
|
cr_expect(lst->data == (void *)3);
|
||||||
|
cr_expect(lst->next == NULL);
|
||||||
|
cr_expect(list_length(lst) == 1);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, destroy_null)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, deep_destroy_null)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, deep_destroy)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_append(lst, strdup("string1"));
|
||||||
|
lst = list_append(lst, strdup("string2"));
|
||||||
|
lst = list_append(lst, strdup("string3"));
|
||||||
|
|
||||||
|
list_deep_destroy(lst);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, length_empty)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
cr_expect(list_length(lst) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, print_empty, .init = cr_redirect_stdout)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
list_print(lst);
|
||||||
|
cr_expect_stdout_eq_str("");
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, print_non_empty, .init = cr_redirect_stdout)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_append(lst, (void *)1);
|
||||||
|
lst = list_append(lst, (void *)2);
|
||||||
|
lst = list_append(lst, (void *)3);
|
||||||
|
|
||||||
|
list_print(lst);
|
||||||
|
fflush(stdout);
|
||||||
|
cr_expect_stdout_eq_str("0x1 0x2 0x3\n");
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, find_empty)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
cr_expect(list_find(lst, (void *)1) == -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, find_non_empty)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
lst = list_append(lst, (void *)1);
|
||||||
|
lst = list_append(lst, (void *)2);
|
||||||
|
lst = list_append(lst, (void *)3);
|
||||||
|
|
||||||
|
cr_expect(list_find(lst, (void *)1) == 0);
|
||||||
|
cr_expect(list_find(lst, (void *)2) == 1);
|
||||||
|
cr_expect(list_find(lst, (void *)3) == 2);
|
||||||
|
cr_expect(list_find(lst, (void *)4) == -1); // not found
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void fold_func(void *acc, void *data)
|
||||||
|
{
|
||||||
|
*(int *)acc += *(int *)data;
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(lists, fold)
|
||||||
|
{
|
||||||
|
struct list *lst = NULL;
|
||||||
|
int v1 = 10, v2 = 20, v3 = 30;
|
||||||
|
lst = list_append(lst, &v1);
|
||||||
|
lst = list_append(lst, &v2);
|
||||||
|
lst = list_append(lst, &v3);
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
list_fold(lst, &sum, fold_func);
|
||||||
|
cr_expect(sum == 60);
|
||||||
|
|
||||||
|
list_destroy(&lst);
|
||||||
|
cr_expect(lst == NULL);
|
||||||
|
}
|
||||||
84
tests/unit/utils/vars.c
Normal file
84
tests/unit/utils/vars.c
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
#include "../../../src/utils/vars/vars.h"
|
||||||
|
|
||||||
|
#include <criterion/criterion.h>
|
||||||
|
#include <criterion/new/assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "../../../src/utils/hash_map/hash_map.h"
|
||||||
|
#include "../../../src/utils/string_utils/string_utils.h"
|
||||||
|
|
||||||
|
TestSuite(utils_vars);
|
||||||
|
|
||||||
|
Test(utils_vars, init_free)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(utils_vars, get_defaults)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
cr_assert_str_eq(get_var(map, "?"), "0");
|
||||||
|
char int_str[11];
|
||||||
|
int_to_str((int)getpid(), int_str);
|
||||||
|
cr_assert_str_eq(get_var(map, "$"), int_str);
|
||||||
|
int_to_str((int)getuid(), int_str);
|
||||||
|
cr_assert_str_eq(get_var(map, "UID"), int_str);
|
||||||
|
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(utils_vars, set_vars)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
set_var_copy(map, "key1", "value1");
|
||||||
|
cr_assert_str_eq(get_var(map, "key1"), "value1");
|
||||||
|
set_var_copy(map, "key2", "value2");
|
||||||
|
cr_assert_str_eq(get_var(map, "key2"), "value2");
|
||||||
|
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(utils_vars, get_env_vars)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
cr_assert_eq(get_var_or_env(map, "ENV_TEST"), NULL);
|
||||||
|
setenv("ENV_TEST", "value1", 0);
|
||||||
|
cr_assert_str_eq(get_var_or_env(map, "ENV_TEST"), "value1");
|
||||||
|
setenv("ENV_TEST", "value2", 1);
|
||||||
|
cr_assert_str_eq(get_var_or_env(map, "ENV_TEST"), "value2");
|
||||||
|
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(utils_vars, set_vars_update)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
set_var_copy(map, "key", "value1");
|
||||||
|
cr_assert_str_eq(get_var(map, "key"), "value1");
|
||||||
|
set_var_copy(map, "key", "value2");
|
||||||
|
cr_assert_str_eq(get_var(map, "key"), "value2");
|
||||||
|
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(utils_vars, set_vars_int)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
set_var_int(map, "key1", 100);
|
||||||
|
cr_assert_str_eq(get_var(map, "key1"), "100");
|
||||||
|
set_var_int(map, "key1", 200);
|
||||||
|
cr_assert_str_eq(get_var(map, "key1"), "200");
|
||||||
|
set_var_int(map, "key2", 10);
|
||||||
|
cr_assert_str_eq(get_var(map, "key2"), "10");
|
||||||
|
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue