Skip to content
ast_case.c 1.18 KiB
Newer Older
Seblu's avatar
Seblu committed
/*
** ast_for.c for 42sh
**
** Made by Seblu
** Login   <seblu@epita.fr>
**
** Started on  Thu Aug  3 02:41:37 2006 Seblu
Seblu's avatar
Seblu committed
** Last update Mon Aug 28 23:57:38 2006 Seblu
Seblu's avatar
Seblu committed
*/

#include "ast.h"

Seblu's avatar
Seblu committed
s_ast_node	*ast_case_create(char *word)
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  s_ast_node	*node;
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
  secmalloc(node, sizeof (s_ast_node));
Seblu's avatar
Seblu committed
  node->type = T_CASE;
  node->body.child_case.word = word;
  node->body.child_case.items = NULL;
  return node;
}

Seblu's avatar
Seblu committed
void		ast_case_add_item(s_ast_node	*node,
Seblu's avatar
Seblu committed
				  char		**pattern,
Seblu's avatar
Seblu committed
				  s_ast_node	*exec)
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  s_case_item	*item;
  s_case_item	**this;
Seblu's avatar
Seblu committed

  if (node->type != T_CASE)
    return;
Seblu's avatar
Seblu committed
  secmalloc(item, sizeof (s_case_item));
  item->pattern = pattern;
  item->exec = exec;
  item->next = NULL;
  for (this = &node->body.child_case.items; *this; *this = (*this)->next)
    ; //do nothing
  *this = item;
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
void		ast_case_destruct(s_ast_node *node)
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  s_case_item	*this, *buf;
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
  if (node->type != T_CASE)
    return;
  free(node->body.child_case.word);
  for (this = node->body.child_case.items; this; this = buf) {
    for (register int i = 0; this->pattern[i]; ++i)
      free(this->pattern[i]);
    free(this->pattern);
    ast_destruct(this->exec);
    buf = this->next;
    free(this);
  }
  free(node);
Seblu's avatar
Seblu committed
}