Commit 018ce926 authored by Seblu's avatar Seblu
Browse files

gestion du here-doc par le lexer

remplacement readline par getln
shell_prompt.c devient prompt.c
le repertoire option fusionne avec shell
mise a jour du nommage des struct,enum,typedef, union a la norme epita 2007
reorganisation des dossiers: readline et alias sont dans desormais dans parser
implementation de la fonction ast_destruct
ajout de la creation d'un noeud ast
correction grammaire pour ajout HEREDOC
parent faa7fb44
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -28,8 +28,8 @@ funcdec: ['function'] WORD '(' ')' ('\n')* shell_command (redirection)*
redirection:  [NUMBER] '>' WORD
		| [NUMBER] '<' WORD
		| [NUMBER] '>>' WORD
		| [NUMBER] '<<' WORD
		| [NUMBER] '<<-' WORD
		| [NUMBER] '<<' HEREDOC
		| [NUMBER] '<<-' HEREDOC
		| [NUMBER] '>&' WORD
		| [NUMBER] '<&' WORD
		| [NUMBER] '>|' WORD
+10 −11
Original line number Diff line number Diff line
@@ -4,9 +4,7 @@ bin_PROGRAMS=42sh
#42sh_LDADD  = parser/libparse.a ../check/leaktrack/libmem.a evalexpr/libevalexpr.a


42sh_SOURCES=	alias/alias.h			\
		alias/alias.c			\
		ast/ast.h			\
42sh_SOURCES=	ast/ast.h			\
		ast/ast_and.c			\
		ast/ast_bang.c			\
		ast/ast_case.c			\
@@ -31,20 +29,21 @@ bin_PROGRAMS=42sh
		common/strndup.c		\
		common/basename.c		\
		exec/exec.h			\
		exec/exec_ast.c			\
		option/option.h			\
		option/option.c			\
		option/option_parser.c		\
		exec/exec_node.c		\
		parser/alias.h			\
		parser/alias.c			\
		parser/getln.h			\
		parser/getln.c			\
		parser/parser.h			\
		parser/parser.c			\
		parser/lexer.c			\
		readline/readline.h		\
		readline/readline.c		\
		readline/getln.c		\
		shell/getoptions.c		\
		shell/option.h			\
		shell/option.c			\
		shell/shell.h			\
		shell/shell_entry.c		\
		shell/shell_init.c		\
		shell/shell_destroy.c		\
		shell/shell_prompt.c
		shell/prompt.c

CLEANFILES= *~ '\#*'
+118 −91
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
** Login   <seblu@epita.fr>
**
** Started on  Sun Jul 30 04:40:03 2006 Seblu
** Last update Fri Aug 25 03:47:41 2006 Seblu
** Last update Tue Aug 29 00:35:06 2006 Seblu
*/

#ifndef AST_H_
@@ -13,61 +13,61 @@

# include "../common/macro.h"

typedef struct s_ast_node  ts_ast_node;
typedef struct ast_node  s_ast_node;

/*
** If ast node
*/
typedef struct s_if_node
typedef struct if_node
{
  ts_ast_node		*cond;
  ts_ast_node		*cond_true;
  ts_ast_node		*cond_false;
} ts_if_node;
  s_ast_node		*cond;
  s_ast_node		*cond_true;
  s_ast_node		*cond_false;
} s_if_node;

/*
** For ast node
*/
typedef struct s_for_node
typedef struct for_node
{
  char			*varname;
  char			**values;
  ts_ast_node		*exec;
} ts_for_node;
  s_ast_node		*exec;
} s_for_node;

/*
** Case item (not an ast node)
*/
typedef struct s_case_item ts_case_item;
struct s_case_item
typedef struct case_item s_case_item;
struct case_item
{
  char			**pattern;
  ts_ast_node		*exec;
  ts_case_item		*next;
  s_ast_node		*exec;
  s_case_item		*next;
};

/*
** Case ast node
*/
typedef struct s_case_node
typedef struct case_node
{
  char			*word;
  ts_case_item		*items;
} ts_case_node;
  s_case_item		*items;
} s_case_node;

/*
** While ast node
*/
typedef struct s_while_node
typedef struct while_node
{
  ts_ast_node		*cond;
  ts_ast_node		*exec;
} ts_while_node;
  s_ast_node		*cond;
  s_ast_node		*exec;
} s_while_node;

/*
** Enumerate different type of redirection
*/
typedef enum e_redir_type
typedef enum redir_type
  {
    R_LESS,		/*  <   */
    R_LESSAND,		/*  <&  */
@@ -78,29 +78,29 @@ typedef enum e_redir_type
    R_CLOBBER,		/*  >|  */
    R_DLESS,		/*  <<  */
    R_DLESSDASH		/*  <<- */
  } te_redir_type;
  } e_redir_type;

/*
** Redirection ast node
*/
typedef struct s_redir ts_redir;
struct s_redir
typedef struct redir s_redir;
struct redir
{
  te_redir_type		type;
  e_redir_type		type;
  int			fd;
  char			*word;
  ts_redir		*next;
  s_redir		*next;
};

/*
** Command ast node
*/
typedef struct s_cmd_node
typedef struct cmd_node
{
  char			**argv;
  ts_redir		*redirs;
  s_redir		*redirs;
  char			**prefix;
} ts_cmd_node;
} s_cmd_node;

/*
** Binary ast node
@@ -108,28 +108,29 @@ typedef struct s_cmd_node
** T_PIPE, T_SEP* , T_AND, T_OR : binary operator
** T_BANG : unary operator but ts_bin_op with right pointer is always NULL
*/
typedef struct s_bin_node
typedef struct bin_node
{
  ts_ast_node		*lhs;
  ts_ast_node		*rhs;
} ts_bin_node;
  s_ast_node		*lhs;
  s_ast_node		*rhs;
} s_bin_node;

/*
** Funcdec node
*/
typedef struct s_funcdec_node
typedef struct funcdec_node
{
  char			*name;
  ts_ast_node		*body;
} ts_funcdec_node;
  s_ast_node		*body;
} s_funcdec_node;

/*
** Enumerate all node type
*/
typedef enum e_node_type
typedef enum node_type
  {
    T_IF,
    T_FOR,
    T_CASE,
    T_WHILE,
    T_UNTIL,
    T_CMD,
@@ -141,37 +142,36 @@ typedef enum e_node_type
    T_PIPE,
    T_SEPAND,
    T_SEP,
    T_CASE,
  } te_node_type;
  } e_node_type;

/*
** This is a type for a node item
*/
typedef union u_node_item
typedef union node_item
{
  ts_if_node		child_if;
  ts_for_node		child_for;
  ts_case_node		child_case;
  ts_while_node		child_while;
  ts_while_node		child_until;
  ts_cmd_node		child_cmd; //todo
  ts_bin_node		child_and;
  ts_bin_node		child_or;
  ts_bin_node		child_subshell;
  ts_funcdec_node	child_funcdec;
  ts_bin_node		child_bang;
  ts_bin_node		child_pipe;
  ts_bin_node		child_sep;
  ts_bin_node		child_sepand;
} tu_node_item;
  s_if_node		child_if;
  s_for_node		child_for;
  s_case_node		child_case;
  s_while_node		child_while;
  s_while_node		child_until;
  s_cmd_node		child_cmd;
  s_bin_node		child_and;
  s_bin_node		child_or;
  s_bin_node		child_subshell;
  s_funcdec_node	child_funcdec;
  s_bin_node		child_bang;
  s_bin_node		child_pipe;
  s_bin_node		child_sep;
  s_bin_node		child_sepand;
} u_node_item;

/*
** Generic ast node type
*/
struct s_ast_node
struct ast_node
{
  te_node_type		type;
  tu_node_item		body;
  e_node_type		type;
  u_node_item		body;
};

/*!
@@ -180,7 +180,7 @@ struct s_ast_node
** @param ast mother node to destroy
**
*/
void		ast_destruct(ts_ast_node *ast);
void		ast_destruct(s_ast_node *ast);

/*!
** Create an if ast node
@@ -191,16 +191,16 @@ void ast_destruct(ts_ast_node *ast);
**
** @return the node
*/
ts_ast_node	*ast_if_create(ts_ast_node *cond,
			       ts_ast_node *cond_true,
			       ts_ast_node *cond_false);
s_ast_node	*ast_if_create(s_ast_node *cond,
			       s_ast_node *cond_true,
			       s_ast_node *cond_false);

/*!
** Destruct an if ast node
**
** @param node node to destroy
*/
void		ast_if_destruct(ts_ast_node *node);
void		ast_if_destruct(s_ast_node *node);

/*!
** Create a for ast node
@@ -212,16 +212,16 @@ void ast_if_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_for_create(char		*varname,
s_ast_node	*ast_for_create(char		*varname,
				char		**values,
				ts_ast_node	*exec);
				s_ast_node	*exec);

/*!
** Destruct a for ast node
**
** @param node node to destroy
*/
void		ast_for_destruct(ts_ast_node *node);
void		ast_for_destruct(s_ast_node *node);

/*!
** Create a case ast node
@@ -230,7 +230,7 @@ void ast_for_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_case_create(char *word);
s_ast_node	*ast_case_create(char *word);

/*!
** Add a case item into a case node. An item is counpound of a set of word
@@ -240,16 +240,16 @@ ts_ast_node *ast_case_create(char *word);
** @param pattern list of word that can match to reference
** @param exec exec corresponding with match
*/
void		ast_case_add_item(ts_ast_node	*node,
void		ast_case_add_item(s_ast_node	*node,
				  char		**pattern,
				  ts_ast_node	*exec);
				  s_ast_node	*exec);

/*!
** Destruct a case ast node
**
** @param node node to destroy
*/
void		ast_case_destruct(ts_ast_node *node);
void		ast_case_destruct(s_ast_node *node);

/*!
** Create a while ast node
@@ -259,14 +259,14 @@ void ast_case_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_while_create(ts_ast_node *cond, ts_ast_node *exec);
s_ast_node	*ast_while_create(s_ast_node *cond, s_ast_node *exec);

/*!
** Destruct a while ast node
**
** @param node node to destroy
*/
void		ast_while_destruct(ts_ast_node *node);
void		ast_while_destruct(s_ast_node *node);

/*!
** Create a until ast node
@@ -276,14 +276,41 @@ void ast_while_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_until_create(ts_ast_node *cond, ts_ast_node *exec);
s_ast_node	*ast_until_create(s_ast_node *cond, s_ast_node *exec);

/*!
** Destruct a until ast node
**
** @param node node to destroy
*/
void		ast_until_destruct(ts_ast_node *node);
void		ast_until_destruct(s_ast_node *node);

/*!
** Create a cmd ast node
**
** @return the node
*/
s_ast_node	*ast_cmd_create(void);

/*!
** Add a redirection to a cmd node
**
** @param node node where add
** @param type type of redirection
** @param fd fd parameter of redirection
** @param word file or word parameter of redirection
*/
void		ast_cmd_add_redir(s_ast_node		*node,
				  e_redir_type		type,
				  int			fd,
				  char			*word);

/*!
** Destruct a cmd node
**
** @param node node to destroy
*/
void		ast_cmd_destruct(s_ast_node *node);

/*!
** Create an and (&&) ast node
@@ -293,14 +320,14 @@ void ast_until_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_and_create(ts_ast_node *lhs, ts_ast_node *rhs);
s_ast_node	*ast_and_create(s_ast_node *lhs, s_ast_node *rhs);

/*!
** Destruct an and (&&) node
**
** @param node node to destroy
*/
void		ast_and_destruct(ts_ast_node *node);
void		ast_and_destruct(s_ast_node *node);

/*!
** Create an or (||) ast node
@@ -310,14 +337,14 @@ void ast_and_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_or_create(ts_ast_node *lhs, ts_ast_node *rhs);
s_ast_node	*ast_or_create(s_ast_node *lhs, s_ast_node *rhs);

/*!
** Destruct an or (||) node
**
** @param node node to destroy
*/
void		ast_or_destruct(ts_ast_node *node);
void		ast_or_destruct(s_ast_node *node);

/*!
** Create a subshell ($()) ast node
@@ -326,14 +353,14 @@ void ast_or_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_subshell_create(ts_ast_node *child);
s_ast_node	*ast_subshell_create(s_ast_node *child);

/*!
** Destruct a subshell ($()) node
**
** @param node node to destroy
*/
void		ast_subshell_destruct(ts_ast_node *node);
void		ast_subshell_destruct(s_ast_node *node);

/*!
** Create a funcdec (function declaration) ast node
@@ -343,14 +370,14 @@ void ast_subshell_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_fundec_create(char *name, ts_ast_node *body);
s_ast_node	*ast_fundec_create(char *name, s_ast_node *body);

/*!
** Destruct a funcdec ast node
**
** @param node node to destroy
*/
void		ast_funcdec_destruct(ts_ast_node *node);
void		ast_funcdec_destruct(s_ast_node *node);

/*!
** Create a bang (!) ast node
@@ -359,14 +386,14 @@ void ast_funcdec_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_bang_create(ts_ast_node *child);
s_ast_node	*ast_bang_create(s_ast_node *child);

/*!
** Destruct a bang (!) node
**
** @param node node to destroy
*/
void		ast_bang_destruct(ts_ast_node *node);
void		ast_bang_destruct(s_ast_node *node);

/*!
** Create a pipe (|) ast node
@@ -376,14 +403,14 @@ void ast_bang_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_pipe_create(ts_ast_node *lhs, ts_ast_node *rhs);
s_ast_node	*ast_pipe_create(s_ast_node *lhs, s_ast_node *rhs);

/*!
** Destruct a pipe (|) node
**
** @param node node to destroy
*/
void		ast_pipe_destruct(ts_ast_node *node);
void		ast_pipe_destruct(s_ast_node *node);

/*!
** Create a separtor (;) ast node
@@ -393,14 +420,14 @@ void ast_pipe_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_sep_create(ts_ast_node *lhs, ts_ast_node *rhs);
s_ast_node	*ast_sep_create(s_ast_node *lhs, s_ast_node *rhs);

/*!
** Destruct a sep (;) node
**
** @param node node to destroy
*/
void		ast_sep_destruct(ts_ast_node *node);
void		ast_sep_destruct(s_ast_node *node);

/*!
** Create a sepand (&) ast node
@@ -410,13 +437,13 @@ void ast_sep_destruct(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node	*ast_sepand_create(ts_ast_node *lhs, ts_ast_node *rhs);
s_ast_node	*ast_sepand_create(s_ast_node *lhs, s_ast_node *rhs);

/*!
** Destruct a sepand (&) node
**
** @param node node to destroy
*/
void		ast_sepand_destruct(ts_ast_node *node);
void		ast_sepand_destruct(s_ast_node *node);

#endif /* !AST_H_ */
+6 −8
Original line number Diff line number Diff line
@@ -5,28 +5,26 @@
** Login   <seblu@epita.fr>
**
** Started on  Thu Aug  3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:45:10 2006 Seblu
** Last update Mon Aug 28 23:57:05 2006 Seblu
*/

#include "ast.h"

ts_ast_node	*ast_and_create(ts_ast_node *lhs, ts_ast_node *rhs)
s_ast_node	*ast_and_create(s_ast_node *lhs, s_ast_node *rhs)
{
  ts_ast_node	*node;
  s_ast_node	*node;

  secmalloc(node, sizeof (ts_ast_node));
  secmalloc(node, sizeof (s_ast_node));
  node->type = T_AND;
  node->body.child_and.lhs = lhs;
  node->body.child_and.rhs = rhs;
  return node;
}

void		ast_and_destruct(ts_ast_node *node)
void		ast_and_destruct(s_ast_node *node)
{
  if (node->type != T_AND) {
    ast_destruct(node);
  if (node->type != T_AND)
    return;
  }
  ast_destruct(node->body.child_and.lhs);
  ast_destruct(node->body.child_and.rhs);
  free(node);
+6 −8
Original line number Diff line number Diff line
@@ -5,28 +5,26 @@
** Login   <seblu@epita.fr>
**
** Started on  Thu Aug  3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:45:26 2006 Seblu
** Last update Mon Aug 28 23:57:20 2006 Seblu
*/

#include "ast.h"

ts_ast_node	*ast_bang_create(ts_ast_node *child)
s_ast_node	*ast_bang_create(s_ast_node *child)
{
  ts_ast_node	*node;
  s_ast_node	*node;

  secmalloc(node, sizeof (ts_ast_node));
  secmalloc(node, sizeof (s_ast_node));
  node->type = T_BANG;
  node->body.child_bang.lhs = child;
  node->body.child_bang.rhs = NULL;
  return node;
}

void		ast_bang_destruct(ts_ast_node *node)
void		ast_bang_destruct(s_ast_node *node)
{
  if (node->type != T_BANG) {
    ast_destruct(node);
  if (node->type != T_BANG)
    return;
  }
  ast_destruct(node->body.child_bang.lhs);
  free(node);
}
Loading