modifs readscript + token newline

This commit is contained in:
ariard@student.42.fr 2017-01-24 01:27:25 +01:00
parent 830b04208e
commit 0431add79c
39 changed files with 128 additions and 174 deletions

View file

@ -72,11 +72,6 @@ $(D_OBJ)/%.o: $(D_SRC)/job-control/%.c includes/job_control.h
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
@echo "Compiling "$<"..."
$(D_OBJ)/%.o: $(D_SRC)/read-script/%.c includes/readscript.h
@$(MKDIR) $(D_OBJ)
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
@echo "Compiling "$<"..."
libft/libft.a: libft/src/*/*.c
@echo "libft/libft.a"
@$(MAKE) -C libft 2>/dev/null

View file

@ -6,7 +6,7 @@
/* By: sbenning <sbenning@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/09 02:05:22 by sbenning #+# #+# */
/* Updated: 2017/01/21 21:49:00 by ariard ### ########.fr */
/* Updated: 2017/01/23 21:45:15 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/01/22 23:30:39 by ariard ### ########.fr */
/* Updated: 2017/01/24 00:45:06 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,6 +36,10 @@ typedef long long t_type;
# define TK_DQ_WORD (1 << 16)
# define TK_COMMAND (1 << 17)
# define TK_SUBSHELL (1 << 18)
# define TK_NEWLINE (1 << 19)
# define TK_LOOP (1 << 20)
# define TK_B_LOOP (1 << 21)
# define TK_E_LOOP (1 << 22)
# define TK_WORD (TK_N_WORD | TK_Q_WORD | TK_DQ_WORD)
# define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20)
@ -44,6 +48,7 @@ typedef long long t_type;
enum e_lexstate
{
DEFAULT,
NEWLINE,
DELIM,
SEP,
WORD,
@ -89,6 +94,7 @@ int ft_is_delim(char c);
t_lexstate get_lexer_state(char *str);
int lexer_default(t_list **alst, char *str);
int lexer_newline(t_list **alst, char *str);
int lexer_delim(t_list **alst, char *str);
int lexer_sep(t_list **alst, char *str);
int lexer_word(t_list **alst, char *str);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:38:21 by jhalford #+# #+# */
/* Updated: 2017/01/10 12:46:19 by jhalford ### ########.fr */
/* Updated: 2017/01/23 21:45:02 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/01/22 23:21:39 by ariard ### ########.fr */
/* Updated: 2017/01/23 23:31:30 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,6 @@
# include "libft.h"
# include "readscript.h"
# include "types.h"
# include "lexer.h"
# include "parser.h"
@ -48,6 +47,14 @@ struct s_comp
# define SHELL_MSG_NOJOBC "no job-control"
struct s_script
{
char *buffer;
int q_size;
t_list *queue;
};
typedef struct s_script t_script;
struct s_data
{
@ -73,6 +80,7 @@ void data_exit(void);
int shell_single_command(char *command);
int read_script(char *file);
int shell_script(void);
void ft_expand_dollar(char **av, char **env);

View file

@ -1,42 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_script.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/21 22:50:22 by ariard #+# #+# */
/* Updated: 2017/01/22 23:02:55 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef READSCRIPT_H
# define READSCRIPT_H
/*
* input : destination buffer
* b_size : allocated size of the destination buffer
* used : acutal used size in the destination buffer
* q_size : number of elements in the queue
* queue : list of queued lines, FIFO
*/
struct s_script
{
char *buffer;
int b_size;
int q_size;
t_list *queue;
};
typedef struct s_script t_script;
/*
* Read_script setup/cleanup
*/
int ft_read_script(char *file);
int rs_shebang_check(char *file, t_script *script);
#endif

20
42sh/sample/20lines.sh Normal file
View file

@ -0,0 +1,20 @@
ls
pwd
mkdir
cd
ls
pwd
mkdir
cd
ls
pwd
mkdir
cd
ls
pwd
mkdir
cd
ls
pwd
mkdir
cd

2
42sh/sample/2lines.sh Normal file
View file

@ -0,0 +1,2 @@
ls pwd
yolo

View file

@ -1,4 +1,4 @@
#!/bin/minishell
#!/bin/bash
while [ 1 ]; do
sleep 1

Binary file not shown.

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 16:11:11 by jhalford #+# #+# */
/* Updated: 2017/01/22 23:27:50 by ariard ### ########.fr */
/* Updated: 2017/01/22 23:33:04 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */
/* Updated: 2017/01/22 23:18:13 by ariard ### ########.fr */
/* Updated: 2017/01/24 01:25:40 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
int (*g_lexer[])(t_list **alst, char *str) =
{
&lexer_default,
&lexer_newline,
&lexer_delim,
&lexer_sep,
&lexer_word,
@ -32,7 +33,7 @@ int (*g_lexer[])(t_list **alst, char *str) =
int ft_is_delim(char c)
{
return (c == ' ' || c == '\t' || c == '\n');
return (c == ' ' || c == '\t');
}
int ft_tokenize(t_list **alst, char *str, t_lexstate state)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/10 13:45:46 by jhalford #+# #+# */
/* Updated: 2017/01/19 23:46:48 by ariard ### ########.fr */
/* Updated: 2017/01/24 00:55:05 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,9 @@
t_lexstate get_lexer_state(char *str)
{
if (ft_is_delim(*str))
if (*str == '\n')
return (NEWLINE);
else if (ft_is_delim(*str))
return (DELIM);
else if (*str == '$')
return (VAR);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:56:49 by jhalford #+# #+# */
/* Updated: 2017/01/19 22:06:43 by ariard ### ########.fr */
/* Updated: 2017/01/23 23:24:40 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */
/* Updated: 2017/01/19 23:58:36 by ariard ### ########.fr */
/* Updated: 2017/01/24 01:01:43 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:58:44 by jhalford #+# #+# */
/* Updated: 2017/01/11 15:45:53 by jhalford ### ########.fr */
/* Updated: 2017/01/24 00:32:55 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:06:35 by jhalford #+# #+# */
/* Updated: 2017/01/10 14:48:49 by jhalford ### ########.fr */
/* Updated: 2017/01/23 22:51:25 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:06:53 by jhalford #+# #+# */
/* Updated: 2017/01/10 14:27:51 by jhalford ### ########.fr */
/* Updated: 2017/01/24 00:45:30 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_newline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/23 23:19:46 by ariard #+# #+# */
/* Updated: 2017/01/24 01:25:21 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
int lexer_newline(t_list **alst, char *str)
{
t_token *token;
if (*alst)
{
token = (*alst)->content;
if (*token->data)
return (lexer_newline(&(*alst)->next, str));
}
else
{
token = token_init();
*alst = ft_lstnew(token, sizeof(*token));
}
token = (*alst)->content;
token->type = TK_NEWLINE;
return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */
/* Updated: 2017/01/10 14:29:46 by jhalford ### ########.fr */
/* Updated: 2017/01/23 22:54:33 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 16:29:57 by jhalford #+# #+# */
/* Updated: 2017/01/10 14:39:52 by jhalford ### ########.fr */
/* Updated: 2017/01/24 01:25:42 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */
/* Updated: 2017/01/11 15:38:03 by jhalford ### ########.fr */
/* Updated: 2017/01/24 01:02:50 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 16:46:27 by jhalford #+# #+# */
/* Updated: 2017/01/22 23:26:06 by ariard ### ########.fr */
/* Updated: 2017/01/22 23:33:08 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/11 17:18:42 by jhalford #+# #+# */
/* Updated: 2017/01/19 23:01:05 by ariard ### ########.fr */
/* Updated: 2017/01/24 00:30:33 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 15:30:25 by jhalford #+# #+# */
/* Updated: 2017/01/19 23:58:47 by ariard ### ########.fr */
/* Updated: 2017/01/24 01:07:41 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */
/* Updated: 2017/01/19 23:44:34 by ariard ### ########.fr */
/* Updated: 2017/01/24 01:25:27 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

Binary file not shown.

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2017/01/22 17:20:04 by ariard ### ########.fr */
/* Updated: 2017/01/23 22:37:43 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/01/22 23:09:43 by ariard ### ########.fr */
/* Updated: 2017/01/23 23:51:35 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -37,7 +37,6 @@ int shell_single_command(char *command)
/* /1* btree_apply_infix(ast, &ft_putast2); *1/ */
if (ft_exec(&ast))
return (1);
return (0);
}
@ -50,12 +49,9 @@ int main(int ac, char **av)
shell_single_command(shell_get_avdata());
return (0);
}
if (ft_read_script(av[1]))
{
if (data_singleton()->opts & SHELL_MODE_SCRIPT)
shell_script();
else
shell_single_command(data_singleton()->line.input);
if (data_singleton()->opts & SHELL_MODE_SCRIPT)
{
shell_script();
return (0);
}
while (1)

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/21 22:49:31 by ariard #+# #+# */
/* Updated: 2017/01/22 23:11:59 by ariard ### ########.fr */
/* Updated: 2017/01/24 00:20:01 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,15 +16,22 @@ static int rs_loop(char *file, t_script *script)
{
int fd;
script->queue = ft_memalloc(sizeof(t_list));
fd = open(file, O_RDONLY);
if ((fd = open(file, O_RDONLY)) == -1)
return (0);
while (get_next_line(fd, &script->buffer) > 0)
{
ft_lsteadd(&script->queue,
ft_lstnew(script->buffer, ft_strlen(script->buffer)));
ft_strdel(&script->buffer);
ft_lsteadd(&script->queue, ft_lstnew(ft_strdup("\n"), 1));
script->q_size += 2;
}
close(fd);
if (script->q_size > 0)
{
data_singleton()->opts &= ~SHELL_MODE_MASK;
data_singleton()->opts |= SHELL_MODE_SCRIPT;
}
return (0);
}
@ -32,12 +39,12 @@ static int rs_setup(t_script *script)
{
ft_bzero(script, sizeof(script));
script->buffer = NULL;
script->b_size = 0;
script->q_size = 0;
script->queue = NULL;
return (0);
}
int ft_read_script(char *file)
int read_script(char *file)
{
t_script *script;
@ -48,8 +55,6 @@ int ft_read_script(char *file)
return (-1);
if (rs_loop(file, script) < 0)
return (-1);
if (rs_shebang_check(file, script) < 0)
return (-1);
script->buffer = NULL;
DG("{inv}{bol}{gre}read of script{eoc} script detected");
return (1);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 14:04:48 by jhalford #+# #+# */
/* Updated: 2017/01/22 22:18:56 by ariard ### ########.fr */
/* Updated: 2017/01/23 23:36:11 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
/* Updated: 2017/01/22 21:58:53 by ariard ### ########.fr */
/* Updated: 2017/01/23 22:35:46 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,5 +41,6 @@ void shell_init(int ac, char **av)
tcsetpgrp(STDIN, *shell_pgid);
tcgetattr(STDIN, &data_singleton()->jobc.shell_tmodes);
}
read_script(av[1]);
shell_get_opts(ac, av);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/22 23:06:34 by ariard #+# #+# */
/* Updated: 2017/01/22 23:32:27 by ariard ### ########.fr */
/* Updated: 2017/01/24 01:25:38 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,24 +15,29 @@
int shell_script()
{
t_list *token;
t_list *head;
t_btree *ast;
t_list *tmp;
token = NULL;
head = NULL;
ast = NULL;
tmp = data_singleton()->script.queue;
while (tmp)
{
if (ft_tokenize(&token, tmp->content , DEFAULT))
return (1);
if (!token)
return (0);
if (ft_post_tokenize(&token, tmp->content))
return (1);
tmp = tmp->next;
ft_lst_merge(&head, token);
token = NULL;
}
if (!token)
return (0);
// if (ft_post_tokenize(&token, command))
// return (1);
DG("after post_tokenize");
token_print(token);
token_print(head);
return (0);
if (ft_parse(&ast, &token))
return (1);
btree_print(STDBUG, ast, &ft_putast);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2017/01/20 16:37:29 by ariard ### ########.fr */
/* Updated: 2017/01/23 23:15:05 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */
/* Updated: 2017/01/20 16:24:51 by ariard ### ########.fr */
/* Updated: 2017/01/23 23:17:04 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -1,78 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rs_shebang.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/22 21:42:27 by ariard #+# #+# */
/* Updated: 2017/01/22 23:11:57 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *rs_shebang_parse_interpreter(char *shebang)
{
int i;
i = ft_strlen(shebang) - 1;
while (shebang[i])
{
if (shebang[i] == '/')
return (&shebang[++i]);
i--;
}
return (NULL);
}
static int rs_valid_interpreter(char *interpreter)
{
if (ft_strncmp(interpreter, "bash", ft_strlen("bash")) == 0)
return (1);
if (ft_strncmp(interpreter, "csh", ft_strlen("csh")) == 0)
return (1);
if (ft_strncmp(interpreter, "ksh", ft_strlen("bash")) == 0)
return (1);
if (ft_strncmp(interpreter, "sh", ft_strlen("bash")) == 0)
return (1);
if (ft_strncmp(interpreter, "tcsh", ft_strlen("bash")) == 0)
return (1);
if (ft_strncmp(interpreter, "tcsh", ft_strlen("bash")) == 0)
return (1);
if (ft_strncmp(interpreter, "minishell", ft_strlen("minishell")) == 0)
return (1);
return (0);
}
static void rs_set_line_input(char *interpreter, char *file)
{
char *buffer;
buffer = ft_strnew(1024);
ft_strncpy(buffer, interpreter, ft_strlen(interpreter));
ft_strncat(buffer, " ", 1);
ft_strncat(buffer, file, ft_strlen(file));
data_singleton()->line.input = buffer;
data_singleton()->opts &= ~SHELL_MODE_MASK;
data_singleton()->opts |= SHELL_MODE_INPUT;
}
int rs_shebang_check(char *file, t_script *script)
{
char *interpreter;
if (!script || !script->queue || !script->queue->next)
return (0);
interpreter = rs_shebang_parse_interpreter(script->queue->next->content);
if (interpreter == NULL || rs_valid_interpreter(interpreter) == 0)
rs_set_line_input("sh", file);
else if (ft_strncmp(interpreter, "minishell", ft_strlen("minishell")) != 0)
rs_set_line_input(interpreter, file);
else if (ft_strncmp(interpreter, "minishell", ft_strlen("minishell")) == 0)
{
data_singleton()->opts &= ~SHELL_MODE_MASK;
data_singleton()->opts |= SHELL_MODE_SCRIPT;
}
return (0);
}