View of xos/usr/xsh/free_cmd.c


XOS | Parent Directory | View | Download

/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
#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);
}