fix: merge local changes
This commit is contained in:
commit
f721e6db72
19 changed files with 568 additions and 104 deletions
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue