Commit b13062fe authored by Seblu's avatar Seblu
Browse files

suppression fichier shell_getopt.c (inutile)

ajout d'un script qui creer un fichier ast.png a partir du dernier dot ast generer
debut des ast printer
parser de commande
parent f3f51117
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -15,11 +15,13 @@ bin_PROGRAMS=42sh
		ast/ast_if.c			\
		ast/ast_or.c			\
		ast/ast_pipe.c			\
		ast/ast_print.c			\
		ast/ast_sep.c			\
		ast/ast_sepand.c		\
		ast/ast_subshell.c		\
		ast/ast_until.c			\
		ast/ast_while.c			\
		common/basename.c		\
		common/common.h			\
		common/constant.h		\
		common/isdigitstr.c		\
@@ -27,7 +29,6 @@ bin_PROGRAMS=42sh
		common/strmerge.c		\
		common/strvmerge.c		\
		common/strndup.c		\
		common/basename.c		\
		exec/exec.h			\
		exec/exec_node.c		\
		parser/alias.h			\
+52 −15
Original line number Diff line number Diff line
@@ -5,12 +5,13 @@
** Login   <seblu@epita.fr>
**
** Started on  Sun Jul 30 04:40:03 2006 Seblu
** Last update Tue Aug 29 00:35:06 2006 Seblu
** Last update Fri Sep  1 00:30:34 2006 Seblu
*/

#ifndef AST_H_
# define AST_H_

# include <stdio.h>
# include "../common/macro.h"

typedef struct ast_node  s_ast_node;
@@ -174,6 +175,23 @@ struct ast_node
  u_node_item		body;
};

/*!
** Print an ast to @arg filename file
**
** @param ast ast to print
** @param filename filename where ast is printed. if it's NULL
** random file name is generated.
*/
void		ast_print(s_ast_node *ast, const char *filename);

/*!
** Print an ast node
**
** @param ast ast node to add to file
** @param fs file stream where print ast
*/
void		ast_print_node(s_ast_node *ast, FILE *fs, unsigned int *node_id);

/*!
** Destroy node and all its childs
**
@@ -305,6 +323,25 @@ void ast_cmd_add_redir(s_ast_node *node,
				  int			fd,
				  char			*word);

/*!
** Add a arg vector to a cmd node
**
** @param node node where add
** @param argv new arg vector
*/
void		ast_cmd_add_argv(s_ast_node *node, char *argv);

/*!
** Add a prefix to a cmd node
**
** @param node node for addition
** @param assignment_word word to add
*/
void		ast_cmd_add_prefix(s_ast_node *node, char *assignment_word);


void		ast_cmd_print(s_ast_node *node, FILE *fs, unsigned int *node_id);

/*!
** Destruct a cmd node
**
+79 −7
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
** Login   <seblu@epita.fr>
**
** Started on  Fri Aug 18 22:13:51 2006 Seblu
** Last update Mon Aug 28 23:56:46 2006 Seblu
** Last update Fri Sep  1 00:44:22 2006 Seblu
*/

#include "ast.h"
@@ -42,18 +42,90 @@ void ast_cmd_add_redir(s_ast_node *node,
  *this = red;
}

void		ast_cmd_add_prefix(s_ast_node *node, char *assignment_word)
{
  if (node->type != T_CMD)
    return;
  size_t	size = 0;
  if (node->body.child_cmd.prefix)
    while (node->body.child_cmd.prefix[size])
      ++size;
  secrealloc(node->body.child_cmd.prefix, node->body.child_cmd.prefix,
	     (++size + 1) * sizeof (char *));
  node->body.child_cmd.prefix[size - 1] = assignment_word;
  node->body.child_cmd.prefix[size] = NULL;
}

void		ast_cmd_add_argv(s_ast_node *node, char *argv)
{
  if (node->type != T_CMD)
    return;
  size_t	size = 0;
  if (node->body.child_cmd.argv)
    while (node->body.child_cmd.argv[size])
      ++size;
  secrealloc(node->body.child_cmd.argv, node->body.child_cmd.argv,
	     (++size + 1) * sizeof (char *));
  node->body.child_cmd.argv[size - 1] = argv;
  node->body.child_cmd.argv[size] = NULL;
}

void		ast_cmd_print(s_ast_node *node, FILE *fs, unsigned int *node_id)
{
  unsigned	cur_id = *node_id;

  if (node->type != T_CMD)
    return;
  fprintf(fs, "%u [label = \"Command\"];\n", *node_id);
  //prefix
  char **prefix = node->body.child_cmd.prefix;
  if (prefix && prefix[0]) {
    ++*node_id;
    fprintf(fs, "%u [label = \"", *node_id);
    for (int i = 0; prefix && prefix[i]; ++i) {
      fprintf(fs, "prefix[%d]=%s\\n", i, prefix[i]);
    }
    fprintf(fs, "\"];\n");
    fprintf(fs, "%u -> %u\n", cur_id, *node_id);
  }
  //arguments
  char **argv = node->body.child_cmd.argv;
  if (argv && argv[0]) {
    ++*node_id;
    fprintf(fs, "%u [label = \"", *node_id);
    for (int i = 0; argv && argv[i]; ++i)
      fprintf(fs, "argv[%d]=%s\\n", i, argv[i]);
    fprintf(fs, "\"];\n");
    fprintf(fs, "%u -> %u\n", cur_id, *node_id);
  }
  //redirs
  if (node->body.child_cmd.redirs) {
    int i = 0;
    ++*node_id;
    fprintf(fs, "%u [label = \"", *node_id);
    for (s_redir *this = node->body.child_cmd.redirs; this; this = this->next, ++i)
      fprintf(fs, "redirs[%d]: fd=%d, type=%d, word=%s\\n", i, this->fd, this->type, this->word);
    fprintf(fs, "\"];\n");
    fprintf(fs, "%u -> %u\n", cur_id, *node_id);
  }
}

void		ast_cmd_destruct(s_ast_node *node)
{
  s_redir	*this, *buf;

  if (node->type != T_CMD)
    return;
  if (node->body.child_cmd.argv) {
    for (register int i = 0; node->body.child_cmd.argv[i]; ++i)
      free(node->body.child_cmd.argv[i]);
    free(node->body.child_cmd.argv);
  }
  if (node->body.child_cmd.prefix) {
    for (register int i = 0; node->body.child_cmd.prefix[i]; ++i)
      free(node->body.child_cmd.prefix[i]);
  free(node->body.child_cmd.argv);
    free(node->body.child_cmd.prefix);
  }
  for (this = node->body.child_cmd.redirs; this; this = buf) {
    free(this->word);
    buf = this->next;

src/ast/ast_print.c

0 → 100644
+95 −0
Original line number Diff line number Diff line
/*
** ast_print.c for 42sh
**
** Made by Seblu
** Login   <seblu@epita.fr>
**
** Started on  Sat Mar 25 23:11:01 2006 Seblu
** Last update Fri Sep  1 00:31:28 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"

#define NODE_TYPE_COUNT 14

typedef void (*print_fct)(s_ast_node *, FILE *, unsigned int *);

struct		ast_print_switch
{
  e_node_type	type;
  print_fct	fct;
};

struct ast_print_switch print_table[NODE_TYPE_COUNT] =
  {
    {T_IF, NULL}, // ast_if_print},
    {T_FOR,  NULL}, //ast_for_print},
    {T_WHILE,  NULL}, //ast_while_print},
    {T_UNTIL,  NULL}, //ast_until_print},
    {T_CMD, ast_cmd_print},
    {T_AND,  NULL}, //ast_and_print},
    {T_OR,  NULL}, //ast_or_print},
    {T_SUBSHELL,  NULL}, //ast_subshell_print},
    {T_FUNCDEC,  NULL}, //ast_funcdec_print},
    {T_BANG,  NULL}, //ast_bang_print},
    {T_PIPE,  NULL}, //ast_pipe_print},
    {T_SEPAND,  NULL}, //ast_sepand_print},
    {T_SEP,  NULL}, //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;
}
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
** Login   <seblu@epita.fr>
**
** Started on  Thu Aug  3 02:41:37 2006 Seblu
** Last update Tue Aug 29 00:00:40 2006 Seblu
** Last update Wed Aug 30 00:36:03 2006 Seblu
*/

#include "ast.h"
Loading