Commit 392117bf authored by Seblu's avatar Seblu

debut de readline

ajout du getln
correction du ast.h (voir si on peut faire mieux pour le forward de typedef)
premiere fonction du parser
parent 6ef731df
......@@ -10,14 +10,19 @@ bin_PROGRAMS=42sh
common/strmerge.c \
common/strvmerge.c \
lexer/lexer.h \
opt/opt.h \
opt/opt.c \
opt/opt_init.c \
opt/opt_parser.c \
parser/parser.h \
parser/parser.c \
readline/readline.h \
readline/readline.c \
readline/getln.c \
shell/shell.h \
shell/shell_entry.c \
shell/shell_init.c \
shell/shell_destroy.c \
shell/shell_prompt.c \
opt/opt.h \
opt/opt.c \
opt/opt_init.c \
opt/opt_parser.c
shell/shell_prompt.c
CLEANFILES= *~ '\#*'
This diff is collapsed.
/*
** parser.c for 42sh in /home/seblu/svn/42sh/src/parser
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Wed Aug 2 00:56:07 2006 Seblu
** Last update Wed Aug 2 17:47:01 2006 Seblu
*/
#include "parser.h"
/*!
** Parse a File stream, and return the ast
**
** @param fs file stream to parse
**
** @return ast from the file stream
*/
ts_ast_node *parse(FILE* fs)
{
int fd;
fd = fileno(fs);
return NULL;
}
/*
** parser.h for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Wed Aug 2 00:49:50 2006 Seblu
** Last update Wed Aug 2 17:47:18 2006 Seblu
*/
#include <stdio.h>
#include "../ast/ast.h"
#ifndef PARSER_H_
# define PARSER_H_
ts_ast_node *parse(FILE* fs);
#endif
/*
** getln.c for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Wed Aug 2 01:25:01 2006 Seblu
** Last update Wed Aug 2 01:38:22 2006 Seblu
*/
#include "readline.h"
/*!
** Secure layer over strlen
**
** @param s the string
**
** @return lenght of the string
*/
inline static size_t sstrlen(const char *s)
{
if (s == NULL)
return 0;
return strlen(s);
}
/*
** Append a string to the buffer string
*/
inline static void buf_str(char **str, char *append, unsigned n)
{
unsigned ln;
unsigned i;
unsigned j;
ln = len(*str);
if ((*str = realloc(*str, (ln + n + 1) * sizeof (char))) == NULL)
exit(1);
for (i = ln, j = 0; i < ln + n; i++, j++)
(*str)[i] = append[j];
(*str)[ln + n] = 0;
}
/*
** Memory allocation of getln buffer
*/
struct s_getln *getln_open(const int fd)
{
struct s_getln *new_buf;
if ((new_buf = malloc(sizeof (struct s_getln))) == NULL)
exit(1);
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)
{
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 *string = NULL;
int i;
do
{
for (i = buf->offset; i < buf->size; i++)
{
if (buf->data[i] == '\n')
{
buf_str(&(string), buf->data + buf->offset, i - buf->offset);
buf->offset = i + 1;
return string;
}
}
if (buf->size - buf->offset > 0)
buf_str(&(string), buf->data + buf->offset, buf->size - buf->offset);
buf->offset = 0;
buf->size = read(buf->fd, buf->data, GETLN_BUF_SIZE);
if (buf->size < 0)
buf->size = 0;
}
while (buf->size > 0);
return string;
}
/*
** readline.c for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Wed Aug 2 01:13:56 2006 Seblu
** Last update Wed Aug 2 17:58:03 2006 Seblu
*/
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "readline.h"
struct s_getln getln_stdin;
char *readline(const char *prompt)
{
if (isatty(STDOUT_FILENO) && isatty(STDERR_FILENO))
write(STDERR_FILENO, prompt, strlen(prompt));
return getln(&getln_stdin);
}
/*
** readline.h for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** 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
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