Commit 018ce926 authored by Seblu's avatar Seblu

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
......@@ -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
......
......@@ -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= *~ '\#*'
This diff is collapsed.
......@@ -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);
......
......@@ -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);
}
......
......@@ -5,32 +5,32 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:45:34 2006 Seblu
** Last update Mon Aug 28 23:57:38 2006 Seblu
*/
#include "ast.h"
ts_ast_node *ast_case_create(char *word)
s_ast_node *ast_case_create(char *word)
{
ts_ast_node *node;
s_ast_node *node;
secmalloc(node, sizeof (ts_ast_node));
secmalloc(node, sizeof (s_ast_node));
node->type = T_CASE;
node->body.child_case.word = word;
node->body.child_case.items = NULL;
return node;
}
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)
{
ts_case_item *item;
ts_case_item **this;
s_case_item *item;
s_case_item **this;
if (node->type != T_CASE)
return;
secmalloc(item, sizeof (ts_case_item));
secmalloc(item, sizeof (s_case_item));
item->pattern = pattern;
item->exec = exec;
item->next = NULL;
......@@ -39,14 +39,12 @@ void ast_case_add_item(ts_ast_node *node,
*this = item;
}
void ast_case_destruct(ts_ast_node *node)
void ast_case_destruct(s_ast_node *node)
{
ts_case_item *this, *buf;
s_case_item *this, *buf;
if (node->type != T_CASE) {
ast_destruct(node);
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)
......
/*
** ast_cmd.c for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Fri Aug 18 22:13:51 2006 Seblu
** Last update Mon Aug 28 23:56:46 2006 Seblu
*/
#include "ast.h"
s_ast_node *ast_cmd_create(void)
{
s_ast_node *node;
secmalloc(node, sizeof (s_ast_node));
node->type = T_CMD;
node->body.child_cmd.argv = NULL;
node->body.child_cmd.prefix = NULL;
node->body.child_cmd.redirs = NULL;
return node;
}
void ast_cmd_add_redir(s_ast_node *node,
e_redir_type type,
int fd,
char *word)
{
s_redir *red;
s_redir **this;
if (node->type != T_CMD)
return;
secmalloc(red, sizeof (s_redir));
red->type = type;
red->fd = fd;
red->word = word;
red->next = NULL;
for (this = &node->body.child_cmd.redirs; *this; *this = (*this)->next)
; //do nothing
*this = red;
}
void ast_cmd_destruct(s_ast_node *node)
{
s_redir *this, *buf;
if (node->type != T_CMD)
return;
for (register int i = 0; node->body.child_cmd.argv[i]; ++i)
free(node->body.child_cmd.argv[i]);
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;
free(this);
}
free(node);
}
......@@ -5,14 +5,44 @@
** Login <seblu@epita.fr>
**
** Started on Sat Mar 25 23:11:01 2006 Seblu
** Last update Thu Aug 3 07:00:17 2006 Seblu
** Last update Tue Aug 29 00:06:45 2006 Seblu
*/
#include "ast.h"
void ast_destruct(ts_ast_node *ast)
#define NODE_TYPE_COUNT 14
typedef void (*destruct_fct)(s_ast_node *);
struct ast_destruct_switch
{
e_node_type type;
destruct_fct fct;
};
struct ast_destruct_switch destruction_table[NODE_TYPE_COUNT] =
{
{T_IF, ast_if_destruct},
{T_FOR, ast_for_destruct},
{T_WHILE, ast_while_destruct},
{T_UNTIL, ast_until_destruct},
{T_CMD, ast_cmd_destruct},
{T_AND, ast_and_destruct},
{T_OR, ast_or_destruct},
{T_SUBSHELL, ast_subshell_destruct},
{T_FUNCDEC, ast_funcdec_destruct},
{T_BANG, ast_bang_destruct},
{T_PIPE, ast_pipe_destruct},
{T_SEPAND, ast_sepand_destruct},
{T_SEP, ast_sep_destruct},
{T_CASE, ast_sepand_destruct}
};
void ast_destruct(s_ast_node *ast)
{
if (ast == NULL)
return;
//fixme
for (register int i = 0; i < NODE_TYPE_COUNT; ++i)
if (destruction_table[i].type == ast->type)
(destruction_table[i].fct)(ast);
}
......@@ -5,18 +5,18 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:45:55 2006 Seblu
** Last update Mon Aug 28 23:58:56 2006 Seblu
*/
#include "ast.h"
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)
{
ts_ast_node *node;
s_ast_node *node;
secmalloc(node, sizeof (ts_ast_node));
secmalloc(node, sizeof (s_ast_node));
node->type = T_FOR;
node->body.child_for.varname = varname;
node->body.child_for.values = values;
......@@ -24,12 +24,10 @@ ts_ast_node *ast_for_create(char *varname,
return node;
}
void ast_for_destruct(ts_ast_node *node)
void ast_for_destruct(s_ast_node *node)
{
if (node->type != T_FOR) {
ast_destruct(node);
if (node->type != T_FOR)
return;
}
free(node->body.child_for.varname);
for (register int i = 0; node->body.child_for.values[i]; ++i)
free(node->body.child_for.values[i]);
......
......@@ -5,28 +5,26 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:46:54 2006 Seblu
** Last update Mon Aug 28 23:59:05 2006 Seblu
*/
#include "ast.h"
ts_ast_node *ast_funcdec_create(char *name, ts_ast_node *body)
s_ast_node *ast_funcdec_create(char *name, s_ast_node *body)
{
ts_ast_node *node;
s_ast_node *node;
secmalloc(node, sizeof (ts_ast_node));
secmalloc(node, sizeof (s_ast_node));
node->type = T_FUNCDEC;
node->body.child_funcdec.name = name;
node->body.child_funcdec.body = body;
return node;
}
void ast_funcdec_destruct(ts_ast_node *node)
void ast_funcdec_destruct(s_ast_node *node)
{
if (node->type != T_FUNCDEC) {
ast_destruct(node);
if (node->type != T_FUNCDEC)
return;
}
free(node->body.child_funcdec.name);
ast_destruct(node->body.child_funcdec.body);
free(node);
......
......@@ -5,18 +5,18 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:46:10 2006 Seblu
** Last update Mon Aug 28 23:59:17 2006 Seblu
*/
#include "ast.h"
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)
{
ts_ast_node *node;
s_ast_node *node;
secmalloc(node, sizeof (ts_ast_node));
secmalloc(node, sizeof (s_ast_node));
node->type = T_IF;
node->body.child_if.cond = cond;
node->body.child_if.cond_true = cond_true;
......@@ -24,12 +24,10 @@ ts_ast_node *ast_if_create(ts_ast_node *cond,
return node;
}
void ast_if_destruct(ts_ast_node *node)
void ast_if_destruct(s_ast_node *node)
{
if (node->type != T_IF) {
ast_destruct(node);
if (node->type != T_IF)
return;
}
ast_destruct(node->body.child_if.cond);
ast_destruct(node->body.child_if.cond_true);
ast_destruct(node->body.child_if.cond_false);
......
......@@ -5,28 +5,26 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:46:27 2006 Seblu
** Last update Mon Aug 28 23:59:55 2006 Seblu
*/
#include "ast.h"
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)
{
ts_ast_node *node;
s_ast_node *node;
secmalloc(node, sizeof (ts_ast_node));
secmalloc(node, sizeof (s_ast_node));
node->type = T_OR;
node->body.child_or.lhs = lhs;
node->body.child_or.rhs = rhs;
return node;
}
void ast_or_destruct(ts_ast_node *node)
void ast_or_destruct(s_ast_node *node)
{
if (node->type != T_OR) {
ast_destruct(node);
if (node->type != T_OR)
return;
}
ast_destruct(node->body.child_or.lhs);
ast_destruct(node->body.child_or.rhs);
free(node);
......
......@@ -5,28 +5,26 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:46:33 2006 Seblu
** Last update Tue Aug 29 00:00:31 2006 Seblu
*/
#include "ast.h"
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)
{
ts_ast_node *node;
s_ast_node *node;
secmalloc(node, sizeof (ts_ast_node));
secmalloc(node, sizeof (s_ast_node));
node->type = T_PIPE;
node->body.child_pipe.lhs = lhs;
node->body.child_pipe.rhs = rhs;
return node;
}
void ast_pipe_destruct(ts_ast_node *node)
void ast_pipe_destruct(s_ast_node *node)
{
if (node->type != T_PIPE) {
ast_destruct(node);
if (node->type != T_PIPE)
return;
}
ast_destruct(node->body.child_pipe.lhs);
ast_destruct(node->body.child_pipe.rhs);
free(node);
......
......@@ -5,28 +5,26 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:46:42 2006 Seblu
** Last update Tue Aug 29 00:00:40 2006 Seblu
*/
#include "ast.h"
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)
{
ts_ast_node *node;
s_ast_node *node;
secmalloc(node, sizeof (ts_ast_node));
secmalloc(node, sizeof (s_ast_node));
node->type = T_SEP;
node->body.child_sep.lhs = lhs;
node->body.child_sep.rhs = rhs;
return node;
}
void ast_sep_destruct(ts_ast_node *node)
void ast_sep_destruct(s_ast_node *node)
{
if (node->type != T_SEP) {
ast_destruct(node);
if (node->type != T_SEP)
return;
}
ast_destruct(node->body.child_sep.lhs);
ast_destruct(node->body.child_sep.rhs);
free(node);
......
......@@ -5,28 +5,26 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:48:01 2006 Seblu
** Last update Tue Aug 29 00:00:49 2006 Seblu
*/
#include "ast.h"
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)
{
ts_ast_node *node;
s_ast_node *node;
secmalloc(node, sizeof (ts_ast_node));
secmalloc(node, sizeof (s_ast_node));
node->type = T_SEPAND;
node->body.child_sepand.lhs = lhs;
node->body.child_sepand.rhs = rhs;
return node;
}
void ast_sepand_destruct(ts_ast_node *node)
void ast_sepand_destruct(s_ast_node *node)
{
if (node->type != T_SEPAND) {
ast_destruct(node);
if (node->type != T_SEPAND)
return;
}
ast_destruct(node->body.child_sepand.lhs);
ast_destruct(node->body.child_sepand.rhs);
free(node);
......
......@@ -5,28 +5,26 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:48:09 2006 Seblu
** Last update Tue Aug 29 00:07:12 2006 Seblu
*/
#include "ast.h"
ts_ast_node *ast_subshell_create(ts_ast_node *child)
s_ast_node *ast_subshell_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_SUBSHELL;
node->body.child_subshell.lhs = child;
node->body.child_subshell.rhs = NULL;
return node;
}
void ast_subshell_destruct(ts_ast_node *node)
void ast_subshell_destruct(s_ast_node *node)
{
if (node->type != T_SUBSHELL) {
ast_destruct(node);
if (node->type != T_SUBSHELL)
return;
}
ast_destruct(node->body.child_subshell.lhs);
free(node);
}
......
......@@ -5,28 +5,26 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:48:15 2006 Seblu
** Last update Tue Aug 29 00:01:11 2006 Seblu
*/
#include "ast.h"
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)
{
ts_ast_node *node;
s_ast_node *node;
secmalloc(node, sizeof (ts_ast_node));
secmalloc(node, sizeof (s_ast_node));
node->type = T_UNTIL;
node->body.child_until.cond = cond;
node->body.child_until.exec = exec;
return node;
}
void ast_until_destruct(ts_ast_node *node)
void ast_until_destruct(s_ast_node *node)
{
if (node->type != T_UNTIL) {
ast_destruct(node);
if (node->type != T_UNTIL)
return;
}
ast_destruct(node->body.child_until.cond);
ast_destruct(node->body.child_until.exec);
free(node);
......
......@@ -5,28 +5,26 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 25 03:48:21 2006 Seblu
** Last update Tue Aug 29 00:01:57 2006 Seblu
*/
#include "ast.h"
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)
{
ts_ast_node *node;
s_ast_node *node;
secmalloc(node, sizeof (ts_ast_node));
secmalloc(node, sizeof (s_ast_node));
node->type = T_WHILE;
node->body.child_while.cond = cond;
node->body.child_while.exec = exec;
return node;
}
void ast_while_destruct(ts_ast_node *node)
void ast_while_destruct(s_ast_node *node)
{
if (node->type != T_WHILE) {
ast_destruct(node);
if (node->type != T_WHILE)
return;
}
ast_destruct(node->body.child_while.cond);
ast_destruct(node->body.child_while.exec);
free(node);
......
......@@ -5,24 +5,24 @@
** Login <Seblu@epita.fr>
**
** Started on Fri Aug 25 03:39:03 2006 Seblu
** Last update Fri Aug 25 07:34:15 2006 Seblu
** Last update Tue Aug 29 00:31:01 2006 Seblu
*/
#ifndef CONSTANT_H_
# define CONSTANT_H_
typedef enum e_error
typedef enum error
{
ERROR_PARSE = 258,
ERROR_FORK = 128,
ERROR_REDIR = 1,
ERROR_MEM = 42,
} te_error;
} e_error;
typedef enum e_prompt {
typedef enum prompt {
PROMPT_PS1 = 1,
PROMPT_PS2 = 2,
PROMPT_PS4 = 4
} te_prompt;
} e_prompt;
#endif
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sun Mar 30 16:02:07 2006 Seblu
** Last update Thu Aug 3 01:20:18 2006 Seblu
** Last update Tue Aug 29 00:15:59 2006 Seblu
*/
#ifndef EXEC_H_
......@@ -15,7 +15,7 @@
# include <stdio.h>
# include "../ast/ast.h"
void exec_ast(ts_ast_node *node);
void exec_node(s_ast_node *node);
/* void exec_node(struct s_ast *node, struct s_42sh *sh); */
/* void exec_list(struct s_list *node, struct s_42sh *sh); */
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sat Mar 25 14:51:09 2006 Seblu
** Last update Sat Apr 15 09:12:38 2006 Seblu
** Last update Tue Aug 29 00:19:35 2006 Seblu
*/
#include "exec.h"
......@@ -16,7 +16,7 @@
** @param node node to execute
** @param sh shell struct
*/
void exec_ast(ts_ast_node *node)
void exec_node(s_ast_node *node)
{
node = node;
}
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Wed Aug 23 00:39:17 2006 Seblu
** Last update Fri Aug 25 03:44:03 2006 Seblu
** Last update Mon Aug 28 23:08:31 2006 Seblu
*/
#include <string.h>
......@@ -14,20 +14,20 @@
static size_t step = 5;
ts_aliases *alias_init(void