Commit faa7fb44 authored by Seblu's avatar Seblu

modifications et ajouts des headers common

le lexer est ok, ne reste plus qu'a faire le mode here doc.


parent 89b761b4
......@@ -23,8 +23,9 @@ bin_PROGRAMS=42sh
ast/ast_until.c \
ast/ast_while.c \
common/common.h \
common/constant.h \
common/isdigitstr.c \
common/mem.h \
common/macro.h \
common/strmerge.c \
common/strvmerge.c \
common/strndup.c \
......
......@@ -5,12 +5,12 @@
** Login <seblu@epita.fr>
**
** Started on Wed Aug 23 00:39:17 2006 Seblu
** Last update Wed Aug 23 18:47:59 2006 Seblu
** Last update Fri Aug 25 03:44:03 2006 Seblu
*/
#include <string.h>
#include "alias.h"
#include "../common/mem.h"
#include "../common/macro.h"
static size_t step = 5;
......
......@@ -5,16 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Sun Jul 30 04:40:03 2006 Seblu
** Last update Fri Aug 18 22:38:00 2006 Seblu
** Last update Fri Aug 25 03:47:41 2006 Seblu
*/
#ifndef AST_H_
# define AST_H_
# include <sys/types.h>
# include <errno.h>
# include <stdlib.h>
# include <assert.h>
# include "../common/macro.h"
typedef struct s_ast_node ts_ast_node;
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:40:35 2006 Seblu
** Last update Fri Aug 25 03:45:10 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_and_create(ts_ast_node *lhs, ts_ast_node *rhs)
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:43:52 2006 Seblu
** Last update Fri Aug 25 03:45:26 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_bang_create(ts_ast_node *child)
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 22:32:35 2006 Seblu
** Last update Fri Aug 25 03:45:34 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_case_create(char *word)
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:38:08 2006 Seblu
** Last update Fri Aug 25 03:45:55 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_for_create(char *varname,
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:43:15 2006 Seblu
** Last update Fri Aug 25 03:46:54 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_funcdec_create(char *name, ts_ast_node *body)
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:37:47 2006 Seblu
** Last update Fri Aug 25 03:46:10 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_if_create(ts_ast_node *cond,
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:40:50 2006 Seblu
** Last update Fri Aug 25 03:46:27 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_or_create(ts_ast_node *lhs, ts_ast_node *rhs)
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:44:37 2006 Seblu
** Last update Fri Aug 25 03:46:33 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_pipe_create(ts_ast_node *lhs, ts_ast_node *rhs)
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:48:47 2006 Seblu
** Last update Fri Aug 25 03:46:42 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_sep_create(ts_ast_node *lhs, ts_ast_node *rhs)
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:49:02 2006 Seblu
** Last update Fri Aug 25 03:48:01 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_sepand_create(ts_ast_node *lhs, ts_ast_node *rhs)
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:42:28 2006 Seblu
** Last update Fri Aug 25 03:48:09 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_subshell_create(ts_ast_node *child)
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:39:25 2006 Seblu
** Last update Fri Aug 25 03:48:15 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_until_create(ts_ast_node *cond, ts_ast_node *exec)
......
......@@ -5,10 +5,9 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 20:39:02 2006 Seblu
** Last update Fri Aug 25 03:48:21 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_while_create(ts_ast_node *cond, ts_ast_node *exec)
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sun Jul 30 03:59:48 2006 Seblu
** Last update Tue Aug 22 22:41:29 2006 Seblu
** Last update Fri Aug 25 03:31:29 2006 Seblu
*/
#ifndef COMMON_H_
......
/*
** constant.h for 42sh
**
** Made by Seblu
** Login <Seblu@epita.fr>
**
** Started on Fri Aug 25 03:39:03 2006 Seblu
** Last update Fri Aug 25 07:34:15 2006 Seblu
*/
#ifndef CONSTANT_H_
# define CONSTANT_H_
typedef enum e_error
{
ERROR_PARSE = 258,
ERROR_FORK = 128,
ERROR_REDIR = 1,
ERROR_MEM = 42,
} te_error;
typedef enum e_prompt {
PROMPT_PS1 = 1,
PROMPT_PS2 = 2,
PROMPT_PS4 = 4
} te_prompt;
#endif
/*
** mem.h for 42sh
** macro.h for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
** Login <seblu@seblu.net>
**
** Started on Sun Jul 30 03:35:39 2006 Seblu
** Last update Sun Jul 30 03:42:49 2006 Seblu
** Started on Fri Aug 25 03:32:54 2006 Seblu
** Last update Fri Aug 25 03:42:43 2006 Seblu
*/
#ifndef MACRO_H_
# define MACRO_H_
#include <stdlib.h>
# include <unistd.h>
# include <stdlib.h>
# include "constant.h"
static const int MEM_ERROR = 42;
# define secmalloc(name, size) if (!(name = malloc(size))) exit(ERROR_MEM)
# define secrealloc(ret, name, size) if (!(ret = realloc(name, size))) exit(ERROR_MEM)
# define secstrdup(ret, str) if (!(ret = strdup(str))) exit(ERROR_MEM)
# define secmalloc(name, size) if (!(name = malloc(size))) exit(MEM_ERROR)
# define secrealloc(ret, name, size) if (!(ret = realloc(name, size))) exit(MEM_ERROR)
# define secstrdup(ret, str) if (!(ret = strdup(str))) exit(MEM_ERROR)
# define isinteractive() (isatty(STDOUT_FILENO) && isatty(STDERR_FILENO))
#endif
......@@ -5,12 +5,12 @@
** Login <seblu@epita.fr>
**
** Started on Tue May 16 21:23:02 2006 Seblu
** Last update Thu Aug 3 05:29:21 2006 Seblu
** Last update Fri Aug 25 03:48:39 2006 Seblu
*/
#include <stdarg.h>
#include <string.h>
#include "mem.h"
#include "macro.h"
char *strmerge(int n, const char *s1, ...)
{
......
......@@ -5,12 +5,12 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 05:56:28 2006 Seblu
** Last update Thu Aug 3 05:58:42 2006 Seblu
** Last update Fri Aug 25 03:49:08 2006 Seblu
*/
#include <string.h>
#include <stdlib.h>
#include "mem.h"
#include "macro.h"
char *strndup(const char *str, size_t n)
{
......
......@@ -5,11 +5,11 @@
** Login <seblu@epita.fr>
**
** Started on Sun Jul 30 04:03:35 2006 Seblu
** Last update Thu Aug 3 05:29:38 2006 Seblu
** Last update Fri Aug 25 03:48:47 2006 Seblu
*/
#include <string.h>
#include "mem.h"
#include "macro.h"
char *strvmerge(const char *const *vtable)
{
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Tue Mar 21 19:00:38 2006 Seblu
** Last update Wed Aug 23 18:37:38 2006 Seblu
** Last update Fri Aug 25 03:49:21 2006 Seblu
*/
/*
......@@ -17,7 +17,7 @@
#include <string.h>
#include "option.h"
#include "../shell/shell.h"
#include "../common/mem.h"
#include "../common/macro.h"
static const char *opts_table[NBR_OPTION] =
{
......
This diff is collapsed.
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Wed Aug 2 00:56:07 2006 Seblu
** Last update Tue Aug 22 19:10:23 2006 Seblu
** Last update Fri Aug 25 15:13:58 2006 Seblu
*/
#include <stdio.h>
......@@ -13,7 +13,7 @@
#include <unistd.h>
#include <setjmp.h>
#include "parser.h"
#include "../common/mem.h"
#include "../common/macro.h"
#include "../shell/shell.h"
#include "../readline/readline.h"
......@@ -23,10 +23,28 @@
** ============
*/
/*
** Define is parser is run for DEBBUGING
*/
#define DEBUG_PARSER 1
/* static ts_token keywords[] = */
/* { */
/* {TOK_IF, "if"}, */
/* {TOK_THEN, "then"}, */
/* {TOK_ELSE, "else"}, */
/* {TOK_FI, "fi"}, */
/* {TOK_ELIF, "elif"}, */
/* {TOK_DO, "do"}, */
/* {TOK_DONE, "done"}, */
/* {TOK_CASE, "case"}, */
/* {TOK_ESAC, "esac"}, */
/* {TOK_WHILE, "while"}, */
/* {TOK_UNTIL, "until"}, */
/* {TOK_FOR, "for"}, */
/* {TOK_IN, "in"}, */
/* {TOK_LBRACE, "{"}, */
/* {TOK_RBRACE, "}"}, */
/* {TOK_BANG, "!"}, */
/* {TOK_NONE, NULL} */
/* }; */
static ts_ast_node *regnode(ts_parser *parser, ts_ast_node *node);
......@@ -112,7 +130,6 @@ static void parse_error(ts_parser *parser, ts_token t)
ts_ast_node *parse(ts_parser *parser)
{
lexer_start(parser->lexer);
parser->regpos = 0;
parser->error = 0;
// prevent of too big register ast size
......@@ -121,18 +138,20 @@ ts_ast_node *parse(ts_parser *parser)
(parser->regsize = 50) * sizeof (ts_ast_node));
if (setjmp(parser->stack))
return NULL;
show_prompt(PROMPT_PS1);
#if DEBUG_LEXER == 1
//test lexer mode
while (1)
{
ts_token tok;
tok = lexer_gettoken(parser->lexer);
ts_token tok = lexer_gettoken(parser->lexer);
if (tok.id == TOK_EOF)
exit(42);
exit(69);
printf("Returned token: %d [%s]\n", tok.id,
(*tok.str == '\n') ? "\\n" : tok.str);
if (tok.id == TOK_NEWLINE)
lexer_start(parser->lexer);
printf("Returned token: %d [%s]\n", tok.id, tok.str);
show_prompt(PROMPT_PS1);
}
#endif
return parse_input(parser);
}
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Wed Aug 2 00:49:50 2006 Seblu
** Last update Wed Aug 23 02:33:38 2006 Seblu
** Last update Fri Aug 25 15:17:29 2006 Seblu
*/
#include <setjmp.h>
......@@ -14,29 +14,34 @@
#ifndef PARSER_H_
# define PARSER_H_
typedef enum e_token
// Define is parser or lexer is run for DEBBUGING
#define DEBUG_PARSER 1
#define DEBUG_LEXER 0
typedef enum e_tokenid
{
//token free-context recognition
//token free-context recognition (lexer time)
TOK_NONE,
TOK_NEWLINE,
TOK_NUMBER,
TOK_EOF,
TOK_AND,
TOK_OR,
TOK_DSEMI,
TOK_LESS,
TOK_GREAT,
TOK_DLESS,
TOK_DGREAT,
TOK_LESSAND,
TOK_GREATAND,
TOK_LESSGREAT,
TOK_DLESSDASH,
TOK_CLOBBER,
TOK_CONTEXT,
TOK_WORD,
//token context-sensitive recognition
TOK_ASSIGNMENT,
TOK_NEWLINE, // \n
TOK_EOF, // EOF
TOK_AND, // &&
TOK_SEPAND, // &
TOK_OR, // ||
TOK_PIPE, // |
TOK_DSEMI, // ;;
TOK_SEP, // ;
TOK_DLESSDASH, // <<-
TOK_DLESS, // <<
TOK_LESSGREAT, // <>
TOK_LESSAND, // <&
TOK_LESS, // <
TOK_DGREAT, // >>
TOK_GREATAND, // >&
TOK_CLOBBER, // >|
TOK_GREAT, // >
TOK_WORD, // all others
//token context-sensitive recognition (parser time)
TOK_IF,
TOK_THEN,
TOK_ELSE,
......@@ -52,30 +57,25 @@ typedef enum e_token
TOK_IN,
TOK_LBRACE,
TOK_RBRACE,
TOK_BANG,
TOK_SEP,
TOK_SEPAND,
TOK_NUMBER,
TOK_ASSIGNMENT,
TOK_BANG
} te_tokenid;
typedef struct s_token
{
te_tokenid id;
const char *str;
te_tokenid id;
const char *str;
size_t len;
} ts_token;
typedef enum e_lexer_status
{
LEXER_READY = 1,
LEXER_END = 2,
} te_lexer_status;
typedef struct s_lexer
{
te_lexer_status status;
ts_token token;
FILE *fs;
char eof;
char *buf;
size_t buf_size;
size_t buf_size; //without \0
size_t buf_pos;
} ts_lexer;
......@@ -90,14 +90,6 @@ typedef struct s_parser
} ts_parser;
/*!
** Free the string of a token if is a word (dynamically allocated)
**
** @param token a token struct type (not a pointer)
*/
#define token_free(token) if ((token).id == TOK_WORD && (token).str) \
free((char*)(token).str)
/*
** ==============
** FILE: parser.c
......@@ -168,4 +160,11 @@ ts_token lexer_gettoken(ts_lexer *lexer);
*/
ts_token lexer_lookahead(ts_lexer *lexer);
/*!
** Parse input as a here-document (describe is XSI)
**
** @param lexer current lexer
*/
void lexer_heredocument(ts_lexer *lexer);
#endif
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Wed Aug 2 01:13:56 2006 Seblu
** Last update Thu Aug 3 11:18:11 2006 Seblu
** Last update Fri Aug 25 07:52:48 2006 Seblu
*/
#include <string.h>
......@@ -17,7 +17,7 @@ struct s_getln getln_stdin;
char *readline(const char *prompt)
{
if (isatty(STDOUT_FILENO) && isatty(STDERR_FILENO))
if (prompt && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO))
write(STDERR_FILENO, prompt, strlen(prompt));
return getln(&getln_stdin);
}
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sun Jul 16 20:03:53 2006 Seblu
** Last update Wed Aug 23 18:38:11 2006 Seblu
** Last update Fri Aug 25 07:45:07 2006 Seblu
*/
#ifndef SHELL_H_
......@@ -13,19 +13,10 @@
# include <errno.h>
# include <stdlib.h>
# include "../common/constant.h"
# include "../alias/alias.h"
# include "../option/option.h"
static const int ERROR_PARSE = 258;
static const int ERROR_FORK = 128;
static const int ERROR_REDIR = 1;
typedef enum e_prompt_type {
TYPE_PS1 = 1,
TYPE_PS2 = 2,
TYPE_PS4 = 4
} te_prompt_type;
typedef struct s_shell
{
/* struct s_var *vars; */
......@@ -40,7 +31,14 @@ typedef struct s_shell
ts_shell *shell_init(const char *argv0);
void shell_destroy(ts_shell *sh);
const char *get_prompt(te_prompt_type pty);
const char *get_prompt(te_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);
extern ts_shell *shell;
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Mon Apr 10 23:57:28 2006 Seblu
** Last update Wed Aug 23 18:40:08 2006 Seblu
** Last update Fri Aug 25 12:24:50 2006 Seblu
*/
#include <stdio.h>
......@@ -14,6 +14,7 @@
#include "../ast/ast.h"
#include "../parser/parser.h"
#include "../exec/exec.h"
#include "../common/macro.h"
/*
** Global shell structure
......@@ -47,6 +48,8 @@ int main(int argc, char *argv[])
exec_ast(ast);
ast_destruct(ast);
}
while (parser->lexer->status != LEXER_END);
while (!parser->lexer->eof);
if (!isinteractive())
fprintf(stderr, "exit\n");
return shell->status;
}
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sun Jul 16 20:11:09 2006 Seblu
** Last update Wed Aug 23 18:41:10 2006 Seblu
** Last update Fri Aug 25 03:50:14 2006 Seblu
*/
#include <stdlib.h>
......@@ -16,7 +16,7 @@
#include <stdlib.h>
#include "shell.h"
#include "../option/option.h"
#include "../common/mem.h"
#include "../common/macro.h"
#include "../common/common.h"
......
......@@ -5,14 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Sun Jul 30 02:27:59 2006 Seblu
** Last update Sat Aug 19 01:44:07 2006 Seblu
** Last update Fri Aug 25 08:18:07 2006 Seblu
*/
#include <stdio.h>
#include <string.h>
#include "shell.h"
static char prompt[80];
#include "../common/macro.h"
/*!
** Return a prompt given by PS1, PS2, PS4 or the default if not set
......@@ -21,16 +20,39 @@ static char prompt[80];
**
** @return
*/
const char *get_prompt(te_prompt_type pty)
const char *get_prompt(te_prompt pty)
{
static char prompt[80];
//fixme
if (pty == TYPE_PS1) {
if (pty == PROMPT_PS1) {
strncpy(prompt, shell->name, 78);
strcpy(prompt + strlen(shell->name), "$ ");
}
else if (pty == TYPE_PS2)
else if (pty == PROMPT_PS2)
strcpy(prompt, "> ");
else if (pty == TYPE_PS4)
else if (pty == PROMPT_PS4)
return strcpy(prompt, "");
return prompt;
}
//todo gestion des variables !
void show_prompt(te_prompt pty)
{
if (!isinteractive())
return;
char *prompt;
switch (pty) {
case PROMPT_PS1:
prompt = "42sh ";
break;
case PROMPT_PS2:
prompt = "> ";
break;
case PROMPT_PS4:
prompt = "+";
break;
}
fflush(stderr);
fprintf(stderr, "%s", prompt);
fflush(stderr);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment