#include "free_cmd.h"
#include "command.h"
#include <stdlib.h>
void free_argument_list(struct argument_struct *first_argument)
{
struct argument_struct *argument, *next_argument;
for (argument = first_argument; argument; argument = next_argument) {
next_argument = argument->next;
free_argument(argument);
}
}
void free_redirect_list(struct redirect_struct *first_redirect)
{
struct redirect_struct *redirect, *next_redirect;
for (redirect = first_redirect; redirect; redirect = next_redirect) {
next_redirect = redirect->next;
free_redirect(redirect);
}
}
void free_command_list(struct command_struct *first_command)
{
struct command_struct *command, *next_command;
for (command = first_command; command; command = next_command) {
next_command = command->next;
free_command(command);
}
}
void free_pipeline_list(struct pipeline_struct *first_pipeline)
{
struct pipeline_struct *pipeline, *next_pipeline;
for (pipeline = first_pipeline; pipeline; pipeline = next_pipeline) {
next_pipeline = pipeline->next;
free_pipeline(pipeline);
}
}
void free_and_or_list_list(struct and_or_list_struct *first_and_or_list)
{
struct and_or_list_struct *and_or_list, *next_and_or_list;
for (and_or_list = first_and_or_list; and_or_list; and_or_list = next_and_or_list) {
next_and_or_list = and_or_list->next;
free_and_or_list(and_or_list);
}
}
void free_argument(struct argument_struct *argument)
{
free(argument->word);
free(argument);
}
void free_redirect(struct redirect_struct *redirect)
{
free(redirect->filename);
free(redirect);
}
static void destroy_simple_command(struct simple_command_struct *simple_command)
{
if (simple_command->command_name) {
free(simple_command->command_name);
free_argument_list(simple_command->first_argument);
}
free_redirect_list(simple_command->first_redirect);
}
static void destroy_compound_command(struct compound_command_struct *compound_command)
{
switch (compound_command->type) {
case CC_SUBSHELL:
free_list(compound_command->list);
break;
}
free_redirect_list(compound_command->first_redirect);
}
void free_command(struct command_struct *command)
{
switch (command->type) {
case CT_SIMPLE:
destroy_simple_command(&command->simple_command);
break;
case CT_COMPOUND:
destroy_compound_command(&command->compound_command);
break;
}
free(command);
}
void free_pipeline(struct pipeline_struct *pipeline)
{
free_command_list(pipeline->first_command);
free(pipeline);
}
void free_and_or_list(struct and_or_list_struct *and_or_list)
{
free_pipeline_list(and_or_list->first_pipeline);
free(and_or_list);
}
void free_list(struct list_struct *list)
{
free_and_or_list_list(list->first_and_or_list);
free(list);
}