fix: merging all the code
This commit is contained in:
parent
2e9e98d343
commit
1eecb1bd42
12 changed files with 86 additions and 61 deletions
|
|
@ -12,14 +12,14 @@
|
|||
* @brief converts a linked list of command arguments to an argv array. Don't
|
||||
* forget to free the result
|
||||
*
|
||||
* @param cmd_list Linked list of command arguments
|
||||
* @param command_list Linked list of command arguments
|
||||
* @return char** Array of command arguments suitable for execvp. Terminated by
|
||||
* NULL
|
||||
*/
|
||||
static char **list_to_argv(struct list *cmd_list)
|
||||
static char **list_to_argv(struct list *command_list)
|
||||
{
|
||||
size_t len = 0;
|
||||
struct list *cur = cmd_list;
|
||||
struct list *cur = command_list;
|
||||
|
||||
while (cur)
|
||||
{
|
||||
|
|
@ -32,7 +32,7 @@ static char **list_to_argv(struct list *cmd_list)
|
|||
{
|
||||
return NULL;
|
||||
}
|
||||
cur = cmd_list;
|
||||
cur = command_list;
|
||||
|
||||
for (size_t i = 0; i < len; i++)
|
||||
{
|
||||
|
|
@ -45,19 +45,19 @@ static char **list_to_argv(struct list *cmd_list)
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief Executes a command represented by an ast_cmd structure
|
||||
* @brief Executes a command represented by an ast_command structure
|
||||
*
|
||||
* @param cmd The command to execute
|
||||
* @param command The command to execute
|
||||
* @return int The exit status of the command
|
||||
*/
|
||||
static int exec_command(struct ast_cmd *cmd)
|
||||
static int exec_command(struct ast_command *command)
|
||||
{
|
||||
if (!cmd || !(cmd->cmd))
|
||||
if (!command || !(command->command))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
char **argv = list_to_argv(cmd->cmd);
|
||||
char **argv = list_to_argv(command->command);
|
||||
|
||||
if (!argv || !(argv[0]))
|
||||
{
|
||||
|
|
@ -112,8 +112,8 @@ int execution(struct ast *ast)
|
|||
return 0;
|
||||
}
|
||||
case AST_CMD: {
|
||||
struct ast_cmd *cmd = ast_get_cmd(ast);
|
||||
return exec_command(cmd); // It's recursive
|
||||
struct ast_command *command = ast_get_command(ast);
|
||||
return exec_command(command); // It's recursive
|
||||
}
|
||||
case AST_IF: {
|
||||
struct ast_if *if_node = ast_get_if(ast);
|
||||
|
|
|
|||
|
|
@ -14,15 +14,15 @@
|
|||
// return iter - start;
|
||||
// }
|
||||
|
||||
struct ast_cmd *expand(struct ast_cmd *cmd)
|
||||
struct ast_command *expand(struct ast_command *command)
|
||||
{
|
||||
if (cmd == NULL)
|
||||
if (command == NULL)
|
||||
return NULL;
|
||||
|
||||
bool in_quotes = false;
|
||||
char *str;
|
||||
size_t len;
|
||||
struct list *l = cmd->cmd;
|
||||
struct list *l = command->command;
|
||||
|
||||
while (l != NULL)
|
||||
{
|
||||
|
|
@ -69,19 +69,19 @@ struct ast_cmd *expand(struct ast_cmd *cmd)
|
|||
|
||||
l = l->next;
|
||||
}
|
||||
return cmd;
|
||||
return command;
|
||||
}
|
||||
|
||||
// int main()
|
||||
// {
|
||||
// printf("Expansion module test\n");
|
||||
// struct ast_cmd ast_cmd;
|
||||
// struct ast_command ast_command;
|
||||
// // char str[] = "echo Hello $?";
|
||||
// char str[] = "echo Hello $AE86";
|
||||
// ast_cmd.cmd = list_append(NULL, str);
|
||||
// ast_command.command = list_append(NULL, str);
|
||||
|
||||
// struct ast_cmd *cmd2 = expand(&ast_cmd);
|
||||
// printf("cmd2: %s\n", (char *)cmd2->cmd->data);
|
||||
// struct ast_command *command2 = expand(&ast_command);
|
||||
// printf("command2: %s\n", (char *)command2->command->data);
|
||||
|
||||
// return 0;
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ int iob_init(struct iob_context *ctx)
|
|||
return 0;
|
||||
|
||||
case IOB_MODE_CMD:
|
||||
if (context.args != NULL)
|
||||
if (context.args == NULL)
|
||||
return IOB_ERROR_BAD_ARG;
|
||||
state = IOB_STATE_READY;
|
||||
return 0;
|
||||
|
|
@ -121,7 +121,7 @@ int iob_config_from_args(struct args_options *args, struct iob_context *ctx)
|
|||
|
||||
case INPUT_CMD:
|
||||
ctx->mode = IOB_MODE_CMD;
|
||||
ctx->args = NULL;
|
||||
ctx->args = (char *)args->input_source;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -2,25 +2,26 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
|
||||
void ast_free(struct ast *node)
|
||||
void ast_free(struct ast **node)
|
||||
{
|
||||
if (node == NULL)
|
||||
if (*node == NULL)
|
||||
return;
|
||||
// ast void does not need to be freed.
|
||||
if (ast_is_if(node))
|
||||
ast_free_if(ast_get_if(node));
|
||||
else if (ast_is_command(node))
|
||||
ast_free_command(ast_get_command(node));
|
||||
else if (ast_is_list(node))
|
||||
ast_free_list(ast_get_list(node));
|
||||
if (ast_is_if(*node))
|
||||
ast_free_if(ast_get_if(*node));
|
||||
else if (ast_is_command(*node))
|
||||
ast_free_command(ast_get_command(*node));
|
||||
else if (ast_is_list(*node))
|
||||
ast_free_list(ast_get_list(*node));
|
||||
|
||||
free(node);
|
||||
free(*node);
|
||||
*node = NULL;
|
||||
}
|
||||
|
||||
struct ast *ast_create(enum ast_type type, void *data)
|
||||
{
|
||||
struct ast *node = malloc(sizeof(struct ast));
|
||||
if (!node)
|
||||
if (node == NULL)
|
||||
return NULL;
|
||||
|
||||
node->type = type;
|
||||
|
|
|
|||
|
|
@ -7,15 +7,4 @@
|
|||
#include "utils/ast/ast_list.h"
|
||||
#include "utils/ast/ast_void.h"
|
||||
|
||||
/* @brief: returns an ast* with corresponding data and type.
|
||||
*
|
||||
* @note: this function should only be called by ast_create_[TYPE] functions.
|
||||
*/
|
||||
struct ast *ast_create(enum ast_type type, void *data);
|
||||
|
||||
/* @brief: frees the given ast. If ast is NULL, does nothing.
|
||||
*
|
||||
*/
|
||||
void ast_free(struct ast *node);
|
||||
|
||||
#endif /* ! AST_H */
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ enum ast_type
|
|||
AST_END,
|
||||
AST_LIST,
|
||||
AST_IF,
|
||||
AST_VOID,
|
||||
AST_CMD
|
||||
};
|
||||
|
||||
|
|
@ -24,4 +25,15 @@ struct ast
|
|||
void *data;
|
||||
};
|
||||
|
||||
/* @brief: returns an ast* with corresponding data and type.
|
||||
*
|
||||
* @note: this function should only be called by ast_create_[TYPE] functions.
|
||||
*/
|
||||
struct ast *ast_create(enum ast_type type, void *data);
|
||||
|
||||
/* @brief: frees the given ast. If ast is NULL, does nothing.
|
||||
*
|
||||
*/
|
||||
void ast_free(struct ast **node);
|
||||
|
||||
#endif /* ! AST_BASE_H */
|
||||
|
|
|
|||
|
|
@ -30,9 +30,10 @@ bool ast_is_command(struct ast *node)
|
|||
return node->type == AST_CMD;
|
||||
}
|
||||
|
||||
void ast_free_command(struct ast_command **command_data)
|
||||
void ast_free_command(struct ast_command *command_data)
|
||||
{
|
||||
list_deep_destroy((*command_data)->command);
|
||||
free(*command_data);
|
||||
*command_data = NULL;
|
||||
if (command_data == NULL)
|
||||
return;
|
||||
list_deep_destroy(command_data->command);
|
||||
free(command_data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,6 @@ struct ast *ast_create_command(struct list *command);
|
|||
/*
|
||||
* @brief: frees the given ast_command and sets the pointer to NULL.
|
||||
*/
|
||||
void ast_free_command(struct ast_command **command_data);
|
||||
void ast_free_command(struct ast_command *command_data);
|
||||
|
||||
#endif /* ! AST_COMMAND_H */
|
||||
|
|
|
|||
|
|
@ -31,15 +31,14 @@ bool ast_is_if(struct ast *node)
|
|||
return node->type == AST_IF;
|
||||
}
|
||||
|
||||
void ast_free_if(struct ast_if **if_data)
|
||||
void ast_free_if(struct ast_if *if_data)
|
||||
{
|
||||
if (if_data == NULL || *if_data == NULL)
|
||||
if (if_data == NULL)
|
||||
return;
|
||||
|
||||
ast_free((*if_data)->condition);
|
||||
ast_free((*if_data)->then_clause);
|
||||
ast_free((*if_data)->else_clause);
|
||||
ast_free(&if_data->condition);
|
||||
ast_free(&if_data->then_clause);
|
||||
ast_free(&if_data->else_clause);
|
||||
|
||||
free(*if_data);
|
||||
*if_data = NULL;
|
||||
free(if_data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,6 @@ struct ast *ast_create_if(struct ast *condition, struct ast *then_clause,
|
|||
/*
|
||||
* @brief: frees the given ast_if and sets the pointer to NULL.
|
||||
*/
|
||||
void ast_free_if(struct ast_if **if_data);
|
||||
void ast_free_if(struct ast_if *if_data);
|
||||
|
||||
#endif /* ! AST_IF_H */
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
#include "utils/ast/ast.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
struct ast *ast_create_list(struct list *list)
|
||||
{
|
||||
struct ast_list *ast_list = malloc(sizeof(struct ast_list));
|
||||
if (ast_list == NULL)
|
||||
return NULL;
|
||||
|
||||
ast_list->list = list;
|
||||
ast_list->children = list;
|
||||
|
||||
return ast_create(AST_LIST, ast_list);
|
||||
}
|
||||
|
|
@ -22,12 +24,25 @@ bool ast_is_list(struct ast *node)
|
|||
return node->type == AST_LIST;
|
||||
}
|
||||
|
||||
void ast_free_list(struct ast_list **ast_list)
|
||||
void ast_free_list(struct ast_list *ast_list)
|
||||
{
|
||||
if (*ast_list == NULL || ast_list == NULL)
|
||||
if (ast_list == NULL)
|
||||
return;
|
||||
|
||||
list_deep_destroy((*ast_list)->children);
|
||||
free(*ast_list);
|
||||
*ast_list = NULL;
|
||||
list_deep_destroy(ast_list->children);
|
||||
free(ast_list);
|
||||
}
|
||||
|
||||
void ast_list_deep_destroy(struct list *l)
|
||||
{
|
||||
struct list *elt = l;
|
||||
struct list *next_elt;
|
||||
while (elt != NULL)
|
||||
{
|
||||
next_elt = elt->next;
|
||||
|
||||
ast_free(elt->data);
|
||||
free(elt);
|
||||
elt = next_elt;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,14 @@ struct ast_list
|
|||
struct list *children; // A list of ASTs (ast*)
|
||||
};
|
||||
|
||||
/*
|
||||
** Release the memory used by the AST list and its AST children
|
||||
** Does nothing if `list` is `NULL`.
|
||||
*
|
||||
* @warning: this function should NEVER be used on a list containing
|
||||
* anything else than ASTs.
|
||||
*/
|
||||
void ast_list_deep_destroy(struct list *l);
|
||||
|
||||
/**
|
||||
* Creates a new AST node representing a list of ASTs
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue