diff --git a/GRAMMAR b/GRAMMAR index e52fb9d22ad7ed0cf666ad3c3bfbe4a1c3751c5b..8239d7736dbf46ab87b3f931bb0ee2ba7133aa50 100644 --- a/GRAMMAR +++ b/GRAMMAR @@ -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 diff --git a/src/Makefile.am b/src/Makefile.am index c80bcdadb508f506837d35a275b0e933f459b837..8b1afb61106c3be7be43581fcfe4a2a13532d8e6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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= *~ '\#*' diff --git a/src/ast/ast.h b/src/ast/ast.h index 687b112a2f6a02fab7ee8329fdd9016e2cfd40c7..961e895d67648fe9080b49b3305732dacd395794 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -5,7 +5,7 @@ ** Login ** ** 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_ */ diff --git a/src/ast/ast_and.c b/src/ast/ast_and.c index 141610ede54b300000c3ef228018b3724b16542a..c9c33c7043f4c9fa8dcde9fe7a79537a7c95c7a2 100644 --- a/src/ast/ast_and.c +++ b/src/ast/ast_and.c @@ -5,28 +5,26 @@ ** Login ** ** 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); diff --git a/src/ast/ast_bang.c b/src/ast/ast_bang.c index 842ea29dbca345b8429e3c5193e8e72e9763dd74..45901dc31b83a6d4d472a774ed77882a63bbfbbf 100644 --- a/src/ast/ast_bang.c +++ b/src/ast/ast_bang.c @@ -5,28 +5,26 @@ ** Login ** ** 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); } diff --git a/src/ast/ast_case.c b/src/ast/ast_case.c index 11d20ef040560a92368bd10ee499e260a88547b6..65687308677222800a718d15a8bdd6df1c9ad7ee 100644 --- a/src/ast/ast_case.c +++ b/src/ast/ast_case.c @@ -5,32 +5,32 @@ ** Login ** ** 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) diff --git a/src/ast/ast_cmd.c b/src/ast/ast_cmd.c new file mode 100644 index 0000000000000000000000000000000000000000..024ae4a0a4bc345f3888ece9b65153abc00a020d --- /dev/null +++ b/src/ast/ast_cmd.c @@ -0,0 +1,63 @@ +/* +** ast_cmd.c for 42sh +** +** Made by Seblu +** Login +** +** 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); +} diff --git a/src/ast/ast_destruct.c b/src/ast/ast_destruct.c index ee55e2ec08021e21333fcefdf4da3f961d099104..2ffb82eb6b33e8994fc75b8aba28e81cf4fe0c09 100644 --- a/src/ast/ast_destruct.c +++ b/src/ast/ast_destruct.c @@ -5,14 +5,44 @@ ** Login ** ** 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); } diff --git a/src/ast/ast_for.c b/src/ast/ast_for.c index 3f0c86685f1f83dfe63c531bd5f03370ca4ceff9..9925f402fbb04a4fe4e79e765a07c64ac820deae 100644 --- a/src/ast/ast_for.c +++ b/src/ast/ast_for.c @@ -5,18 +5,18 @@ ** Login ** ** 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]); diff --git a/src/ast/ast_funcdec.c b/src/ast/ast_funcdec.c index 7b5f90184a61a84d2efadf7dab8ee764cea5c51b..003f6962d9de773ba1377ff51fcb2e72837bcd66 100644 --- a/src/ast/ast_funcdec.c +++ b/src/ast/ast_funcdec.c @@ -5,28 +5,26 @@ ** Login ** ** 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); diff --git a/src/ast/ast_if.c b/src/ast/ast_if.c index 4cdc90239d6adf69b80ff59fcfde0e6b17bbc1da..94ef8439d796fd2cbaf0d19fe73edf9a75c2f3fb 100644 --- a/src/ast/ast_if.c +++ b/src/ast/ast_if.c @@ -5,18 +5,18 @@ ** Login ** ** 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); diff --git a/src/ast/ast_or.c b/src/ast/ast_or.c index d48948db02a4df92cbe552515396fb3ea2d11e10..bffab815375ba2306fe3633c3e54ffe233d97c3d 100644 --- a/src/ast/ast_or.c +++ b/src/ast/ast_or.c @@ -5,28 +5,26 @@ ** Login ** ** 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); diff --git a/src/ast/ast_pipe.c b/src/ast/ast_pipe.c index 9c8a5e462e3942cfd5ddac5c4a2f5d2175ba55ab..d29a722b723dfad511e8d185452a2125d7f18004 100644 --- a/src/ast/ast_pipe.c +++ b/src/ast/ast_pipe.c @@ -5,28 +5,26 @@ ** Login ** ** 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); diff --git a/src/ast/ast_sep.c b/src/ast/ast_sep.c index 9b927b633f54ec485f632524f18fd334df4317e2..3a28f60a1e9ee4c43c2bdbfc07aee44f998579b3 100644 --- a/src/ast/ast_sep.c +++ b/src/ast/ast_sep.c @@ -5,28 +5,26 @@ ** Login ** ** 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); diff --git a/src/ast/ast_sepand.c b/src/ast/ast_sepand.c index 50b81b55af6f6cff273a9c9c34aa9bec342d25d7..27815a2911226b05412fabc32ec2dd35e82da935 100644 --- a/src/ast/ast_sepand.c +++ b/src/ast/ast_sepand.c @@ -5,28 +5,26 @@ ** Login ** ** 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); diff --git a/src/ast/ast_subshell.c b/src/ast/ast_subshell.c index e2c75469ffa88a28076529cb1196ba22eaa95035..81340676d6d8052e6c3e357b17c38cc117eb43d0 100644 --- a/src/ast/ast_subshell.c +++ b/src/ast/ast_subshell.c @@ -5,28 +5,26 @@ ** Login ** ** 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); } diff --git a/src/ast/ast_until.c b/src/ast/ast_until.c index f0a0968d27432cc1e6cbb22f7a8113bca567f40a..c1ef1fa60e741b610ee8eede7b0df2964d0ad838 100644 --- a/src/ast/ast_until.c +++ b/src/ast/ast_until.c @@ -5,28 +5,26 @@ ** Login ** ** 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); diff --git a/src/ast/ast_while.c b/src/ast/ast_while.c index e8c60eb36ddbb339488215d8252b540f6b0886e6..e86ccd1101af1ec6fc752de12f1191b2995b1e7f 100644 --- a/src/ast/ast_while.c +++ b/src/ast/ast_while.c @@ -5,28 +5,26 @@ ** Login ** ** 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); diff --git a/src/common/constant.h b/src/common/constant.h index 9b76b22a9faaddc92934dc62b990fa167bab26cc..c633510d12b50a8225db0834a449db96b9ad7fcf 100644 --- a/src/common/constant.h +++ b/src/common/constant.h @@ -5,24 +5,24 @@ ** Login ** ** 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 diff --git a/src/exec/exec.h b/src/exec/exec.h index f03d69dafb3a4174562a8ad14650fcbd97fbf42a..12122a6b5378f7348c4a4ca3c3a12988004661da 100644 --- a/src/exec/exec.h +++ b/src/exec/exec.h @@ -5,7 +5,7 @@ ** Login ** ** 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 # 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); */ diff --git a/src/exec/exec_node.c b/src/exec/exec_node.c index 6ea4db646a2126719ff50d5e725b1777c70eaad9..02439995af42ce1b6a1e31f4c88f44a8699c554f 100644 --- a/src/exec/exec_node.c +++ b/src/exec/exec_node.c @@ -5,7 +5,7 @@ ** Login ** ** 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; } diff --git a/src/alias/alias.c b/src/parser/alias.c similarity index 80% rename from src/alias/alias.c rename to src/parser/alias.c index 732813831a9f31510d2594f74ae11716c5b34136..14420efb0a4dbca40e16a4c09ef9bd991be8ae7c 100644 --- a/src/alias/alias.c +++ b/src/parser/alias.c @@ -5,7 +5,7 @@ ** Login ** ** 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 @@ -14,20 +14,20 @@ static size_t step = 5; -ts_aliases *alias_init(void) +s_aliases *alias_init(void) { - ts_aliases *new; + s_aliases *new; - secmalloc(new, sizeof (ts_aliases)); + secmalloc(new, sizeof (s_aliases)); new->size = step; new->pos = 0; new->db = NULL; return new; } -void alias_add(ts_aliases *aliases, const char *name, const char *value) +void alias_add(s_aliases *aliases, const char *name, const char *value) { - ts_alias *new; + s_alias *new; int freeplace = 0; size_t index; @@ -47,19 +47,19 @@ void alias_add(ts_aliases *aliases, const char *name, const char *value) return; } //create a new one - secmalloc(new, sizeof (ts_alias)); + secmalloc(new, sizeof (s_alias)); new->name = strdup(name); new->value = strdup(value); //add a new alias if not find if (aliases->pos >= aliases->size) { aliases->size += step; - secrealloc(aliases->db, aliases->db, aliases->size * sizeof (ts_alias)); + secrealloc(aliases->db, aliases->db, aliases->size * sizeof (s_alias)); } aliases->db[aliases->pos] = new; ++aliases->pos; } -int alias_remove(ts_aliases *aliases, const char *name) +int alias_remove(s_aliases *aliases, const char *name) { //find alias index for (register int i = 0; aliases && aliases->db[i]; ++i) diff --git a/src/alias/alias.h b/src/parser/alias.h similarity index 68% rename from src/alias/alias.h rename to src/parser/alias.h index 775328cbadd4747ffcedc252fdf429135576cc77..ab2f9f8b36b94ed5d88b72fb1b5d25583234aa19 100644 --- a/src/alias/alias.h +++ b/src/parser/alias.h @@ -5,7 +5,7 @@ ** Login ** ** Started on Wed Aug 23 00:32:09 2006 Seblu -** Last update Wed Aug 23 18:44:52 2006 Seblu +** Last update Mon Aug 28 23:07:38 2006 Seblu */ #ifndef ALIAS_H_ @@ -13,25 +13,25 @@ # include -typedef struct s_alias +typedef struct alias { char *name; char *value; -} ts_alias; +} s_alias; -typedef struct s_aliases +typedef struct aliases { size_t size; size_t pos; - ts_alias **db; -} ts_aliases; + s_alias **db; +} s_aliases; /*! ** Create a new aliases database ** ** @return new alias database */ -ts_aliases *alias_init(void); +s_aliases *alias_init(void); /*! ** Add an alias into an alias database @@ -40,7 +40,7 @@ ts_aliases *alias_init(void); ** @param name new alias name ** @param value new alias value */ -void alias_add(ts_aliases *aliases, const char *name, const char *value); +void alias_add(s_aliases *aliases, const char *name, const char *value); /*! ** Remove an alias into an alias database @@ -50,6 +50,6 @@ void alias_add(ts_aliases *aliases, const char *name, const char *value); ** ** @return true, if alias was found in database, else false */ -int alias_remove(ts_aliases *aliases, const char *name); +int alias_remove(s_aliases *aliases, const char *name); #endif diff --git a/src/readline/getln.c b/src/parser/getln.c similarity index 70% rename from src/readline/getln.c rename to src/parser/getln.c index 4d0e1ab5e73550f3a7a86c91ddf100ebfd0a3a7f..fad97785050bb9e5b8104888554f4ac334f6dbbc 100644 --- a/src/readline/getln.c +++ b/src/parser/getln.c @@ -5,13 +5,14 @@ ** Login ** ** Started on Wed Aug 2 01:25:01 2006 Seblu -** Last update Thu Aug 3 11:44:30 2006 Seblu +** Last update Mon Aug 28 22:55:18 2006 Seblu */ #include #include #include -#include "readline.h" +#include "../common/macro.h" +#include "getln.h" /*! ** Secure layer over strlen @@ -20,12 +21,7 @@ ** ** @return lenght of the string */ -static size_t sstrlen(const char *s) -{ - if (s == NULL) - return 0; - return strlen(s); -} +#define sstrlen(s) ((s) == NULL ? 0 : strlen(s)) /* ** Append a string to the buffer string @@ -44,37 +40,25 @@ static void buf_str(char **str, char *append, unsigned n) (*str)[ln + n] = 0; } - -/* -** Memory allocation of getln buffer -*/ -struct s_getln *getln_open(const int fd) +s_getln *getln_open(int fd) { - struct s_getln *new_buf; + s_getln *new_buf; - if ((new_buf = malloc(sizeof (struct s_getln))) == NULL) - exit(1); + secmalloc(new_buf, sizeof (s_getln)); new_buf->fd = fd; new_buf->size = 0; new_buf->offset = 0; return new_buf; } -/* -** Free a getln struct -*/ -void getln_close(struct s_getln *buf, const int closefd) +void getln_close(s_getln *buf, int closefd) { if (closefd) close(buf->fd); free(buf); } -/* -** Get next line in a file -** Return NULL when nothing to read -*/ -char *getln(struct s_getln *buf) +char *getln(s_getln *buf) { char *string = NULL; int i; diff --git a/src/parser/getln.h b/src/parser/getln.h new file mode 100644 index 0000000000000000000000000000000000000000..0670566b36dfcf0d5961c6e45f8020f96feae934 --- /dev/null +++ b/src/parser/getln.h @@ -0,0 +1,51 @@ +/* +** getln.h for 42sh +** +** Made by Seblu +** Login +** +** Started on Wed Aug 2 01:06:25 2006 Seblu +** Last update Mon Aug 28 22:56:28 2006 Seblu +*/ + +#ifndef GETLN_H_ +# define GETLN_H_ + +# define GETLN_BUF_SIZE 1024 + +typedef struct getln +{ + int fd; + char data[GETLN_BUF_SIZE]; + unsigned offset; + int size; +} s_getln; + +/*! +** Read a line from fd. This read is bufferised ! +** +** @param buf struct getln +** +** @return malloced readded line +*/ +char *getln(s_getln *buf); + +/*! +** Correctly close an getln struct and if @arg closefd is true +** close the fd of getln struct +** +** @param buf structure getln to close +** @param closefd if true (!0), close fd of getln struct +*/ +void getln_close(s_getln *buf, int closefd); + +/*! +** Create a getln struct associed with @arg fd +** +** @param fd fd associed with struct +** +** @return new struct +*/ +s_getln *getln_open(int fd); + +#endif diff --git a/src/parser/lexer.c b/src/parser/lexer.c index 0609bc0626e02d24c4940022a167ef5d4f1a9200..af2a1f74a14cddeeaa6c3e94ad5c1363ac5b17a7 100644 --- a/src/parser/lexer.c +++ b/src/parser/lexer.c @@ -5,7 +5,7 @@ ** Login ** ** Started on Sun Jul 30 04:36:53 2006 Seblu -** Last update Fri Aug 25 15:16:23 2006 Seblu +** Last update Tue Aug 29 00:29:37 2006 Seblu */ #include @@ -14,7 +14,7 @@ #include #include "parser.h" #include "../shell/shell.h" -#include "../readline/readline.h" +#include "getln.h" #include "../common/common.h" #include "../common/macro.h" @@ -33,7 +33,7 @@ */ // Order is very important for correct recognition ! -static const ts_token operators[] = +static const s_token operators[] = { {TOK_NEWLINE, "\n", 1}, {TOK_AND, "&&", 2}, @@ -106,7 +106,7 @@ static int is_quote_stop(const char *buf, size_t *buf_pos, const ts_quote *quote ** ** @return true if c is a separator */ -#define is_sep(c) ((c) == ' ' || (c) == '\t' || (c) == '\v') +#define is_sep(c) ((c) == ' ' || (c) == '\t') /*! ** Check if the buffer point to an operator. Il it's true and buf_pos is @@ -119,14 +119,14 @@ static int is_quote_stop(const char *buf, size_t *buf_pos, const ts_quote *quote ** ** @return true (!0) if find, else false (0) */ -static int is_operator(const char *buf, size_t *buf_pos, ts_token *token); +static int is_operator(const char *buf, size_t *buf_pos, s_token *token); /*! ** Read lexer's stream, and return the next token. ** ** @param lex lexer struct */ -static void lexer_eattoken(ts_lexer *lex); +static void lexer_eattoken(s_lexer *lex); /*! ** This function is only call when the end of a line occur in @@ -136,7 +136,7 @@ static void lexer_eattoken(ts_lexer *lex); ** ** @return 1 if can read a line, 0 if eof */ -static int lexer_eatline(ts_lexer *lexer); +static int lexer_eatline(s_lexer *lexer); /*! ** Cut a token in one or more line. @@ -144,7 +144,7 @@ static int lexer_eatline(ts_lexer *lexer); ** @param lexer lexer struct ** */ -static int lexer_cut(ts_lexer *lexer); +static int lexer_cut(s_lexer *lexer); /*! ** Correctly set a token. In first, it call macro token_free to @@ -154,7 +154,7 @@ static int lexer_cut(ts_lexer *lexer); ** @param id new token id ** @param s new token string */ -static void token_set(ts_token *token, te_tokenid id, const char *s); +static void token_set(s_token *token, e_tokenid id, const char *s); /* @@ -163,13 +163,12 @@ static void token_set(ts_token *token, te_tokenid id, const char *s); ** =========== */ -ts_lexer *lexer_init(FILE *fs) +s_lexer *lexer_init(int fd) { - ts_lexer *new; + s_lexer *new; - secmalloc(new, sizeof (ts_lexer)); - fflush(fs); - new->fs = fs; + secmalloc(new, sizeof (s_lexer)); + new->stream = getln_open(fd); new->buf = NULL; new->buf_size = new->buf_pos = 0; new->token.id = TOK_NONE; @@ -179,16 +178,16 @@ ts_lexer *lexer_init(FILE *fs) return new; } -ts_token lexer_lookahead(ts_lexer *lexer) +s_token lexer_lookahead(s_lexer *lexer) { if (lexer->token.id == TOK_NONE) lexer_eattoken(lexer); return lexer->token; } -ts_token lexer_gettoken(ts_lexer *lexer) +s_token lexer_gettoken(s_lexer *lexer) { - ts_token buf = { TOK_EOF, "EOF", 3 }; + s_token buf; if (lexer->token.id == TOK_NONE) lexer_eattoken(lexer); @@ -198,12 +197,31 @@ ts_token lexer_gettoken(ts_lexer *lexer) return buf; } -void lexer_heredocument(ts_lexer *lexer) +s_token lexer_getheredoc(s_lexer *lexer, const char *delim) { - lexer = lexer; + s_token token; + char *buf = NULL; + char *line; + + if (lexer->eof) { + token_set(&token, TOK_EOF, "EOF"); + return token; + } + show_prompt(PROMPT_PS2); + do { + line = getln(lexer->stream); + if (line == NULL) { + lexer->eof = 1; + break; + } + buf = strmerge(2, buf, line); + } + while (strcmp(line, delim)); + token_set(&token, TOK_WORD, buf); + return token; } -static void token_set(ts_token *token, te_tokenid id, const char *s) +static void token_set(s_token *token, e_tokenid id, const char *s) { if (token->id == TOK_WORD) free((char*) token->str); @@ -213,26 +231,21 @@ static void token_set(ts_token *token, te_tokenid id, const char *s) else token->len = 0; } -static void lexer_eattoken(ts_lexer *lexer) +static void lexer_eattoken(s_lexer *lexer) { - //if eof, set token EOF - if (lexer->eof) { - token_set(&lexer->token, TOK_EOF, "EOF"); - return; - } //if last char was read free buffer - if (lexer->buf_size > 0 && lexer->buf_pos == lexer->buf_size) { + if (lexer->buf && lexer->buf_pos == lexer->buf_size) { free(lexer->buf); lexer->buf = NULL; lexer->buf_size = 0; } //read a line if buf is empty - if (!lexer->buf_size && ((lexer->buf = readline(NULL)) != NULL)) { + if (!lexer->buf_size && !lexer->eof && (lexer->buf = getln(lexer->stream))) { lexer->buf_size = strlen(lexer->buf); lexer->buf_pos = 0; } //if eof is read, bye bye - if (lexer->buf == NULL) { + if (!lexer->buf) { lexer->eof = 1; token_set(&lexer->token, TOK_EOF, "EOF"); return; @@ -242,12 +255,12 @@ static void lexer_eattoken(ts_lexer *lexer) ;; //retry again } -static int lexer_eatline(ts_lexer *lexer) +static int lexer_eatline(s_lexer *lexer) { char *buf, *buf2; + assert(lexer->buf); buf = lexer->buf; - assert(buf); if (lexer->buf_size > 0 && buf[lexer->buf_size - 1] == '\n') buf[lexer->buf_size - 1] = 0; if (lexer->buf_size > 1 && buf[lexer->buf_size - 2] == '\\') @@ -255,7 +268,7 @@ static int lexer_eatline(ts_lexer *lexer) //show incomplet recognition prompt show_prompt(PROMPT_PS2); //retrieve a new line - if (!(buf2 = readline(NULL))) { + if (!(buf2 = getln(lexer->stream))) { lexer->eof = 1; return 0; } @@ -266,7 +279,7 @@ static int lexer_eatline(ts_lexer *lexer) return 1; } -static int lexer_cut(ts_lexer *lexer) +static int lexer_cut(s_lexer *lexer) { const char *buf = lexer->buf; size_t *buf_pos = &lexer->buf_pos, token_start, token_pos; @@ -313,7 +326,7 @@ static int lexer_cut(ts_lexer *lexer) return 1; } -static int is_operator(const char *buf, size_t *buf_pos, ts_token *token) +static int is_operator(const char *buf, size_t *buf_pos, s_token *token) { for (register int i = 0; operators[i].id != TOK_NONE; ++i) if (!strncmp(buf, operators[i].str, operators[i].len)) { diff --git a/src/parser/parser.c b/src/parser/parser.c index 654f734a3d8fcf63184652de8a05cb47259e8a4c..d60bb0e2ca9f95490642e81c18ac3d9ecaafc4a5 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -5,7 +5,7 @@ ** Login ** ** Started on Wed Aug 2 00:56:07 2006 Seblu -** Last update Fri Aug 25 15:13:58 2006 Seblu +** Last update Tue Aug 29 01:02:02 2006 Seblu */ #include @@ -15,7 +15,7 @@ #include "parser.h" #include "../common/macro.h" #include "../shell/shell.h" -#include "../readline/readline.h" +#include "getln.h" /* ** ============ @@ -46,7 +46,7 @@ /* }; */ -static ts_ast_node *regnode(ts_parser *parser, ts_ast_node *node); +static s_ast_node *regnode(s_parser *parser, s_ast_node *node); /*! ** Parse an input, following the Grammar rule input @@ -59,15 +59,15 @@ static ts_ast_node *regnode(ts_parser *parser, ts_ast_node *node); ** ** @return parent ast node, for execution */ -static ts_ast_node *parse_input(ts_parser *parser); +static s_ast_node *parse_input(s_parser *parser); -static ts_ast_node *parse_list(ts_parser *parser); +static s_ast_node *parse_list(s_parser *parser); -static ts_ast_node *parse_andor(ts_parser *parser); +static s_ast_node *parse_andor(s_parser *parser); -static ts_ast_node *parse_pipeline(ts_parser *parser); +static s_ast_node *parse_pipeline(s_parser *parser); -static ts_ast_node *parse_command(ts_parser *parser); +static s_ast_node *parse_command(s_parser *parser); /*! ** Notify a parse error @@ -75,7 +75,7 @@ static ts_ast_node *parse_command(ts_parser *parser); ** @param parser parser where error appear ** @param t token near of the error */ -static void parse_error(ts_parser *parser, ts_token t); +static void parse_error(s_parser *parser, s_token t); #if DEBUG_PARSER==1 # define debugmsg(msg) fprintf(stderr, "debug: %s\n", (msg)) @@ -89,33 +89,33 @@ static void parse_error(ts_parser *parser, ts_token t); ** =========== */ -ts_parser *parser_init(FILE *fs) +s_parser *parser_init(int fd) { - ts_parser *new; + s_parser *new; - secmalloc(new, sizeof (ts_parser)); - new->lexer = lexer_init(fs); + secmalloc(new, sizeof (s_parser)); + new->lexer = lexer_init(fd); new->error = 0; new->regnodes = NULL; new->regsize = new->regpos = 0; return new; } -static ts_ast_node *regnode(ts_parser *parser, ts_ast_node *node) +static s_ast_node *regnode(s_parser *parser, s_ast_node *node) { if (!node) return node; if (parser->regpos >= parser->regsize) { parser->regsize += 50; secrealloc(parser->regnodes, parser->regnodes, - parser->regsize * sizeof (ts_ast_node)); + parser->regsize * sizeof (s_ast_node)); } parser->regnodes[parser->regpos] = node; ++parser->regpos; return node; } -static void parse_error(ts_parser *parser, ts_token t) +static void parse_error(s_parser *parser, s_token t) { debugmsg("parse_error"); fprintf(stderr, "%s: syntax error near unexpected token `%s'\n", @@ -128,14 +128,14 @@ static void parse_error(ts_parser *parser, ts_token t) longjmp(parser->stack, 1); } -ts_ast_node *parse(ts_parser *parser) +s_ast_node *parse(s_parser *parser) { parser->regpos = 0; parser->error = 0; // prevent of too big register ast size if (parser->regsize >= 200) secrealloc(parser->regnodes, parser->regnodes, - (parser->regsize = 50) * sizeof (ts_ast_node)); + (parser->regsize = 50) * sizeof (s_ast_node)); if (setjmp(parser->stack)) return NULL; show_prompt(PROMPT_PS1); @@ -143,7 +143,7 @@ ts_ast_node *parse(ts_parser *parser) //test lexer mode while (1) { - ts_token tok = lexer_gettoken(parser->lexer); + s_token tok = lexer_gettoken(parser->lexer); if (tok.id == TOK_EOF) exit(69); printf("Returned token: %d [%s]\n", tok.id, @@ -155,10 +155,10 @@ ts_ast_node *parse(ts_parser *parser) return parse_input(parser); } -static ts_ast_node *parse_input(ts_parser *parser) +static s_ast_node *parse_input(s_parser *parser) { - ts_token token; - ts_ast_node *buf; + s_token token; + s_ast_node *buf; debugmsg("parse_input"); token = lexer_lookahead(parser->lexer); @@ -175,11 +175,11 @@ static ts_ast_node *parse_input(ts_parser *parser) return buf; } -static ts_ast_node *parse_list(ts_parser *parser) +static s_ast_node *parse_list(s_parser *parser) { - ts_token token; - ts_ast_node *lhs; - ts_ast_node *rhs; + s_token token; + s_ast_node *lhs; + s_ast_node *rhs; debugmsg("parse_list"); lhs = parse_andor(parser); @@ -195,11 +195,11 @@ static ts_ast_node *parse_list(ts_parser *parser) return lhs; } -static ts_ast_node *parse_andor(ts_parser *parser) +static s_ast_node *parse_andor(s_parser *parser) { - ts_token token; - ts_ast_node *lhs; - ts_ast_node *rhs; + s_token token; + s_ast_node *lhs; + s_ast_node *rhs; debugmsg("parse_andor"); lhs = parse_pipeline(parser); @@ -215,10 +215,10 @@ static ts_ast_node *parse_andor(ts_parser *parser) return lhs; } -static ts_ast_node *parse_pipeline(ts_parser *parser) +static s_ast_node *parse_pipeline(s_parser *parser) { - ts_token token; - ts_ast_node *lhs; + s_token token; + s_ast_node *lhs; int banged = 0; debugmsg("parse_pipeline"); @@ -231,9 +231,9 @@ static ts_ast_node *parse_pipeline(ts_parser *parser) return lhs; } -static ts_ast_node *parse_command(ts_parser *parser) +static s_ast_node *parse_command(s_parser *parser) { - ts_token token; + s_token token; token = lexer_lookahead(parser->lexer); @@ -241,97 +241,97 @@ static ts_ast_node *parse_command(ts_parser *parser) return NULL; } -/* static ts_ast_node *parse_simplecommand(ts_parser *parser) */ +/* static s_ast_node *parse_simplecommand(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_shellcommand(ts_parser *parser) */ +/* static s_ast_node *parse_shellcommand(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_funcdec(ts_parser *parser) */ +/* static s_ast_node *parse_funcdec(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_cmdprefix(ts_parser *parser) */ +/* static s_ast_node *parse_cmdprefix(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_redirection(ts_parser *parser) */ +/* static s_ast_node *parse_redirection(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_element(ts_parser *parser) */ +/* static s_ast_node *parse_element(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_compound_list(ts_parser *parser) */ +/* static s_ast_node *parse_compound_list(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_rulefor(ts_parser *parser) */ +/* static s_ast_node *parse_rulefor(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_rulewhile(ts_parser *parser) */ +/* static s_ast_node *parse_rulewhile(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_ruleuntil(ts_parser *parser) */ +/* static s_ast_node *parse_ruleuntil(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_rulecase(ts_parser *parser) */ +/* static s_ast_node *parse_rulecase(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_ruleif(ts_parser *parser) */ +/* static s_ast_node *parse_ruleif(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_elseclause(ts_parser *parser) */ +/* static s_ast_node *parse_elseclause(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_dogroup(ts_parser *parser) */ +/* static s_ast_node *parse_dogroup(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_caseclause(ts_parser *parser) */ +/* static s_ast_node *parse_caseclause(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ /* } */ -/* static ts_ast_node *parse_pattern(ts_parser *parser) */ +/* static s_ast_node *parse_pattern(s_parser *parser) */ /* { */ /* parser=parser; */ /* return NULL; */ diff --git a/src/parser/parser.h b/src/parser/parser.h index 2485a5143566f47488536db677baff4f0168466a..54fca506cb31871db35fd3323f44c830a1cd329a 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -5,7 +5,7 @@ ** Login ** ** Started on Wed Aug 2 00:49:50 2006 Seblu -** Last update Fri Aug 25 15:17:29 2006 Seblu +** Last update Tue Aug 29 00:24:16 2006 Seblu */ #include @@ -14,12 +14,13 @@ #ifndef PARSER_H_ # define PARSER_H_ +# include "getln.h" // Define is parser or lexer is run for DEBBUGING #define DEBUG_PARSER 1 #define DEBUG_LEXER 0 -typedef enum e_tokenid +typedef enum tokenid { //token free-context recognition (lexer time) TOK_NONE, @@ -60,35 +61,35 @@ typedef enum e_tokenid TOK_NUMBER, TOK_ASSIGNMENT, TOK_BANG - } te_tokenid; + } e_tokenid; -typedef struct s_token +typedef struct token { - te_tokenid id; + e_tokenid id; const char *str; size_t len; -} ts_token; +} s_token; -typedef struct s_lexer +typedef struct lexer { - ts_token token; + s_token token; FILE *fs; char eof; char *buf; size_t buf_size; //without \0 size_t buf_pos; -} ts_lexer; + s_getln *stream; +} s_lexer; -typedef struct s_parser +typedef struct parser { int error; - ts_lexer *lexer; + s_lexer *lexer; jmp_buf stack; - ts_ast_node **regnodes; + s_ast_node **regnodes; size_t regsize; size_t regpos; -} ts_parser; - +} s_parser; /* ** ============== @@ -103,7 +104,7 @@ typedef struct s_parser ** ** @return the new struct */ -ts_parser *parser_init(FILE *fs); +s_parser *parser_init(int fd); /*! ** Do a parse pass @@ -112,7 +113,7 @@ ts_parser *parser_init(FILE *fs); ** ** @return ast_to_execute */ -ts_ast_node *parse(ts_parser *parser); +s_ast_node *parse(s_parser *parser); /* ** ============= @@ -127,7 +128,7 @@ ts_ast_node *parse(ts_parser *parser); ** ** @return the new struct */ -ts_lexer *lexer_init(FILE *fs); +s_lexer *lexer_init(int fd); /*! ** Start a new lexical recognition @@ -138,7 +139,7 @@ ts_lexer *lexer_init(FILE *fs); ** ** @return return if lexer is ready to start or not */ -int lexer_start(ts_lexer *lexer); +int lexer_start(s_lexer *lexer); /*! ** Return the next token and destroy it @@ -148,7 +149,7 @@ int lexer_start(ts_lexer *lexer); ** ** @return the next token */ -ts_token lexer_gettoken(ts_lexer *lexer); +s_token lexer_gettoken(s_lexer *lexer); /*! ** Return the next token without destruction of it. @@ -158,13 +159,13 @@ ts_token lexer_gettoken(ts_lexer *lexer); ** ** @return the look ahead token */ -ts_token lexer_lookahead(ts_lexer *lexer); +s_token lexer_lookahead(s_lexer *lexer); /*! ** Parse input as a here-document (describe is XSI) ** ** @param lexer current lexer */ -void lexer_heredocument(ts_lexer *lexer); +s_token lexer_getheredoc(s_lexer *lexer, const char *delim); #endif diff --git a/src/readline/readline.c b/src/readline/readline.c deleted file mode 100644 index d7de272e9dca1bc83f036993f97850c302b447a1..0000000000000000000000000000000000000000 --- a/src/readline/readline.c +++ /dev/null @@ -1,23 +0,0 @@ -/* -** readline.c for 42sh -** -** Made by Seblu -** Login -** -** Started on Wed Aug 2 01:13:56 2006 Seblu -** Last update Fri Aug 25 07:52:48 2006 Seblu -*/ - -#include -#include -#include -#include "readline.h" - -struct s_getln getln_stdin; - -char *readline(const char *prompt) -{ - if (prompt && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO)) - write(STDERR_FILENO, prompt, strlen(prompt)); - return getln(&getln_stdin); -} diff --git a/src/readline/readline.h b/src/readline/readline.h deleted file mode 100644 index dadb8ed393f04ed8f86389f61c5b19c73870fc7b..0000000000000000000000000000000000000000 --- a/src/readline/readline.h +++ /dev/null @@ -1,29 +0,0 @@ -/* -** readline.h for 42sh -** -** Made by Seblu -** Login -** -** Started on Wed Aug 2 01:06:25 2006 Seblu -** Last update Wed Aug 2 17:57:38 2006 Seblu -*/ - -#ifndef READLINE_H_ -# define READLINE_H_ - -# define GETLN_BUF_SIZE 1024 - -struct s_getln -{ - int fd; - char data[GETLN_BUF_SIZE]; - unsigned offset; - int size; -}; - -char *readline(const char *prompt); -char *getln(struct s_getln *buf); -void getln_close(struct s_getln *buf, const int closefd); -struct s_getln *getln_open(const int fd); - -#endif diff --git a/src/option/option_parser.c b/src/shell/getoptions.c similarity index 85% rename from src/option/option_parser.c rename to src/shell/getoptions.c index 7376e5f44ac4d149f5f6fdcf0df5db0308154d1e..87d1727a59eba2c247e551a2610dcbfa722c3924 100644 --- a/src/option/option_parser.c +++ b/src/shell/getoptions.c @@ -1,11 +1,11 @@ /* -** option_parser.c for 42sh +** getoptions.c for 42sh ** ** Made by Seblu ** Login ** ** Started on Sun Jul 30 03:28:26 2006 Seblu -** Last update Wed Aug 23 18:39:19 2006 Seblu +** Last update Tue Aug 29 00:54:37 2006 Seblu */ #include @@ -19,7 +19,7 @@ ** @param argv program argv ** @param opt shell opt structure to set with options */ -void option_parser(ts_options *opt, int argc, char **argv) +void getoptions(s_options *opt, int argc, char **argv) { #if DEBUG_OPTION == 1 printf("* Option Parser\n"); diff --git a/src/option/option.c b/src/shell/option.c similarity index 77% rename from src/option/option.c rename to src/shell/option.c index 9c7b04650d0d181e4acae081b0fe4add2f673ccf..216c09d65139f7f5a0dfd73562a530050a44230e 100644 --- a/src/option/option.c +++ b/src/shell/option.c @@ -5,7 +5,7 @@ ** Login ** ** Started on Tue Mar 21 19:00:38 2006 Seblu -** Last update Fri Aug 25 03:49:21 2006 Seblu +** Last update Tue Aug 29 00:22:20 2006 Seblu */ /* @@ -35,16 +35,16 @@ static const char *opts_table[NBR_OPTION] = ** =========== */ -ts_options *option_init(void) +s_options *option_init(void) { - ts_options *new; + s_options *new; - secmalloc(new, sizeof (ts_options)); + secmalloc(new, sizeof (s_options)); new->command = NULL; return new; } -int option_set(ts_options *shopt, const char *name) +int option_set(s_options *shopt, const char *name) { register int i; @@ -57,7 +57,7 @@ int option_set(ts_options *shopt, const char *name) return 0; } -int option_unset(ts_options *shopt, const char *name) +int option_unset(s_options *shopt, const char *name) { register int i; @@ -70,7 +70,7 @@ int option_unset(ts_options *shopt, const char *name) return 0; } -int option_isset(const ts_options *shopt, const char *name) +int option_isset(const s_options *shopt, const char *name) { register int i; diff --git a/src/option/option.h b/src/shell/option.h similarity index 74% rename from src/option/option.h rename to src/shell/option.h index 3ee0af4888079b7721bcd07339997d9f90ef02fd..1444faa105f165d969c5593a83f10ef253793056 100644 --- a/src/option/option.h +++ b/src/shell/option.h @@ -5,7 +5,7 @@ ** Login ** ** Started on Tue Mar 21 18:50:03 2006 Seblu -** Last update Wed Aug 23 18:39:43 2006 Seblu +** Last update Tue Aug 29 00:54:50 2006 Seblu */ #ifndef OPTION_H_ @@ -15,11 +15,11 @@ #define DEBUG_OPTION 0 -typedef struct s_options +typedef struct options { signed char item[NBR_OPTION]; char *command; -} ts_options; +} s_options; /* ** ============== @@ -32,7 +32,7 @@ typedef struct s_options ** ** @return A new options structure */ -ts_options *option_init(void); +s_options *option_init(void); /*! ** Set a shell option @@ -42,7 +42,7 @@ ts_options *option_init(void); ** ** @return 0 on failure, else 1 */ -int option_set(ts_options *shopt, const char *name); +int option_set(s_options *shopt, const char *name); /*! ** Unset a shell option @@ -52,7 +52,7 @@ int option_set(ts_options *shopt, const char *name); ** ** @return 0 on failure, else 1 */ -int option_unset(ts_options *shopt, const char *name); +int option_unset(s_options *shopt, const char *name); /*! ** Tell if an option is set. if option nane is not set return -1 @@ -62,7 +62,7 @@ int option_unset(ts_options *shopt, const char *name); ** ** @return 0 if unset, 1 if set and -1 if not exist */ -int option_isset(const ts_options *shopt, const char *name); +int option_isset(const s_options *shopt, const char *name); /*! ** Return a list of know opt @@ -73,17 +73,17 @@ const char **opt_get(); /* ** ===================== -** FILE: option_parser.c +** FILE: getoption.c ** ===================== */ /*! -** Parse the command line +** Parse the command line and set options ** ** @param opt shell opt structure to set with options ** @param argc program argc ** @param argv program argv */ -void option_parser(ts_options *opt, int argc, char **argv); +void getoptions(s_options *opt, int argc, char **argv); #endif diff --git a/src/shell/shell_prompt.c b/src/shell/prompt.c similarity index 81% rename from src/shell/shell_prompt.c rename to src/shell/prompt.c index 01942861f9cc23492f5a93e3928bf3a383e2519a..ce1926b7f3f38c02e2c4626e68f155c58804b749 100644 --- a/src/shell/shell_prompt.c +++ b/src/shell/prompt.c @@ -1,11 +1,11 @@ /* -** shell_prompt.c for 42sh in /home/seblu +** prompt.c for 42sh in /home/seblu ** ** Made by Seblu ** Login ** ** Started on Sun Jul 30 02:27:59 2006 Seblu -** Last update Fri Aug 25 08:18:07 2006 Seblu +** Last update Tue Aug 29 00:47:41 2006 Seblu */ #include @@ -20,9 +20,9 @@ ** ** @return */ -const char *get_prompt(te_prompt pty) +const char *get_prompt(e_prompt pty) { - static char prompt[80]; + static char prompt[80]; //fixme if (pty == PROMPT_PS1) { strncpy(prompt, shell->name, 78); @@ -36,11 +36,12 @@ const char *get_prompt(te_prompt pty) } //todo gestion des variables ! -void show_prompt(te_prompt pty) +void show_prompt(e_prompt pty) { + char *prompt; + if (!isinteractive()) return; - char *prompt; switch (pty) { case PROMPT_PS1: prompt = "42sh "; diff --git a/src/shell/shell.h b/src/shell/shell.h index f1f77825aad8209b99d405b7390536824a3e81f5..9d1f4adf63120196d1a8e102f1436ae3e9c48838 100644 --- a/src/shell/shell.h +++ b/src/shell/shell.h @@ -5,7 +5,7 @@ ** Login ** ** Started on Sun Jul 16 20:03:53 2006 Seblu -** Last update Fri Aug 25 07:45:07 2006 Seblu +** Last update Tue Aug 29 00:51:36 2006 Seblu */ #ifndef SHELL_H_ @@ -14,32 +14,32 @@ # include # include # include "../common/constant.h" -# include "../alias/alias.h" -# include "../option/option.h" +# include "../parser/alias.h" +# include "option.h" -typedef struct s_shell +typedef struct shell { /* struct s_var *vars; */ /* struct s_func *funcs; */ /* struct s_history *history; */ - ts_aliases *aliases; - ts_options *options; + s_aliases *aliases; + s_options *options; char *name; int status; -} ts_shell; +} s_shell; -ts_shell *shell_init(const char *argv0); -void shell_destroy(ts_shell *sh); +s_shell *shell_init(const char *argv0); +void shell_destroy(s_shell *sh); -const char *get_prompt(te_prompt pty); +const char *get_prompt(e_prompt pty); /*! ** Show a prompt on stderr is current shell is a plug on a tty ** ** @param pty prompt type (PS1, PS2, PS4) */ -void show_prompt(te_prompt pty); +void show_prompt(e_prompt pty); -extern ts_shell *shell; +extern s_shell *shell; #endif /* !SHELL_H_ */ diff --git a/src/shell/shell_destroy.c b/src/shell/shell_destroy.c index ee6aea6ea01780c081564c0e1f333f4b434183fc..24220841c8609f1a856c7d68144058385bfcaabf 100644 --- a/src/shell/shell_destroy.c +++ b/src/shell/shell_destroy.c @@ -5,7 +5,7 @@ ** Login ** ** Started on Fri Apr 7 00:19:04 2006 Seblu -** Last update Sun Jul 30 03:16:45 2006 Seblu +** Last update Tue Aug 29 00:31:48 2006 Seblu */ #include @@ -19,7 +19,7 @@ ** @param sh structure to destroy ** */ -void shell_destroy(ts_shell *sh) +void shell_destroy(s_shell *sh) { assert(sh); /* var_delete(&sh->vars); */ diff --git a/src/shell/shell_entry.c b/src/shell/shell_entry.c index 0ff3968e90ce9695bdf3fc86e29dfaf3d1a79d4b..2146d9b063d795663f970f6a0a98269c7276629d 100644 --- a/src/shell/shell_entry.c +++ b/src/shell/shell_entry.c @@ -5,7 +5,7 @@ ** Login ** ** Started on Mon Apr 10 23:57:28 2006 Seblu -** Last update Fri Aug 25 12:24:50 2006 Seblu +** Last update Tue Aug 29 00:53:31 2006 Seblu */ #include @@ -19,7 +19,7 @@ /* ** Global shell structure */ -ts_shell *shell = NULL; +s_shell *shell = NULL; /*! ** Program entry point @@ -31,21 +31,21 @@ ts_shell *shell = NULL; */ int main(int argc, char *argv[]) { - ts_ast_node *ast; - ts_parser *parser; + s_ast_node *ast; + s_parser *parser; // shell initialization shell = shell_init(argv[0]); // parse argv - option_parser(shell->options, argc, argv); + getoptions(shell->options, argc, argv); // shell parser init - parser = parser_init(stdin); + parser = parser_init(STDIN_FILENO); // parse and execute stdin stream do { ast = parse(parser); if (!parser->error) - exec_ast(ast); + exec_node(ast); ast_destruct(ast); } while (!parser->lexer->eof); diff --git a/src/shell/shell_init.c b/src/shell/shell_init.c index b4ee3de0a7b49776ea73e782efdd4eefd364f7e2..ae3fe0f468084a80aec29ac26281445eb71f2abc 100644 --- a/src/shell/shell_init.c +++ b/src/shell/shell_init.c @@ -5,7 +5,7 @@ ** Login ** ** Started on Sun Jul 16 20:11:09 2006 Seblu -** Last update Fri Aug 25 03:50:14 2006 Seblu +** Last update Tue Aug 29 00:55:19 2006 Seblu */ #include @@ -15,7 +15,7 @@ #include #include #include "shell.h" -#include "../option/option.h" +#include "option.h" #include "../common/macro.h" #include "../common/common.h" @@ -28,11 +28,11 @@ ** ** @return the new shell structure */ -ts_shell *shell_init(const char *argv0) +s_shell *shell_init(const char *argv0) { - ts_shell *new; + s_shell *new; - secmalloc(new, sizeof (ts_shell)); + secmalloc(new, sizeof (s_shell)); /* new->vars = NULL; */ /* new->funcs = NULL; */ /* new->history = NULL; */