Skip to content
ast_print.c 2.15 KiB
Newer Older
/*
** ast_print.c for 42sh
**
** Made by Seblu
** Login   <seblu@epita.fr>
**
** Started on  Sat Mar 25 23:11:01 2006 Seblu
Seblu's avatar
Seblu committed
** Last update Tue Sep 26 17:47:33 2006 Seblu
*/

#include <string.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "ast.h"

Seblu's avatar
Seblu committed
enum { NODE_TYPE_COUNT = 14 };
Seblu's avatar
Seblu committed
typedef void (*f_print)(s_ast_node *, FILE *, unsigned int *);

struct		ast_print_switch
{
  e_node_type	type;
Seblu's avatar
Seblu committed
  f_print	fct;
};

struct ast_print_switch print_table[NODE_TYPE_COUNT] =
  {
    {T_IF, NULL}, // ast_if_print},
    {T_FOR,  NULL}, //ast_for_print},
Seblu's avatar
Seblu committed
    {T_WHILE, ast_while_print},
    {T_UNTIL, ast_until_print},
    {T_CMD, ast_cmd_print},
Seblu's avatar
Seblu committed
    {T_AND,  ast_and_print},
    {T_OR,  ast_or_print},
    {T_SUBSHELL,  ast_subshell_print},
    {T_FUNCDEC,  NULL}, //ast_funcdec_print},
Seblu's avatar
Seblu committed
    {T_BANG,  ast_bang_print},
    {T_PIPE,  ast_pipe_print},
    {T_SEPAND,  ast_sepand_print},
    {T_SEP, ast_sep_print},
    {T_CASE,  NULL} //ast_sepand_print}
  };

static char	*newastfilename(void);

void		ast_print(s_ast_node *ast, const char *filename)
{
  FILE		*fs;
  unsigned int	index;

  if (ast == NULL)
    return;
  //open file stream
  if (!filename)
    filename = newastfilename();
  if (!(fs = fopen(filename, "w")))
    return;
  //write dot header
  fprintf(fs, "digraph \"42sh-ast\" {\n");
  fprintf(fs, "node [fontname=Vera, color=lightblue2, style=filled];\n");
  //start ast node wrinting
  ast_print_node(ast, fs, &index);
  //write dot foot and close
  fprintf(fs, "}");
  fclose(fs);
}

void		ast_print_node(s_ast_node *ast, FILE *fs, unsigned int *node_id)
{
  for (register int i = 0; i < NODE_TYPE_COUNT; ++i)
    if (print_table[i].type == ast->type)
      (print_table[i].fct)(ast, fs, node_id);
}

static char	*newastfilename(void)
{

  static char	buf[PATH_MAX];
  time_t	st;
  struct tm	*t;
  struct stat	buf2;
  int		more = 0;

  st = time(NULL);
  t = localtime(&st);
  do
    snprintf(buf, 256, "/tmp/42sh-ast-%d-%d-%d-%d-%d-%d--%d.dot", 1900 + t->tm_year,
	     1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, more++);
  while (stat(buf, &buf2) != -1 && more < 50);
  return buf;
}