Commit 89b38287 authored by abenoit's avatar abenoit
Browse files

merge

parents 9dac684d c9a0ae73
......@@ -11,7 +11,8 @@ SRC += check_syntax.c
SRC += parsing_handler.c
SRC += parsing_states1.c
SRC += parsing_states2.c
SRC += parse_utils.c
SRC += parsing_vars_redirs.c
SRC += parsing_utils.c
SRC += exec_handler.c
......
......@@ -14,7 +14,7 @@
# define S_APPEND 0x40
# define S_CMDSEP 0x80
enum e_state
enum e_state
{
SPACE,
LETTER,
......@@ -29,7 +29,7 @@ enum e_state
NB_STATES
};
enum e_parsing_error
enum e_parsing_error
{
SQUOTE_MISSING = 11,
DQUOTE_MISSING,
......@@ -40,7 +40,7 @@ enum e_parsing_error
NB_PARSING_ERRORS
};
typedef struct s_state_machine
typedef struct s_state_machine
{
char buf[BUF_SIZE];
size_t len;
......@@ -57,49 +57,58 @@ typedef struct s_state_machine
t_byte var_state;
t_byte quote_state;
uint8_t pad[5];
} t_state_machine;
} t_state_machine;
typedef char *(*t_parse)(t_state_machine *, char *);
/*
** check_syntax.c
*/
int parsing_error(int err_code, t_xe *xe);
int check_syntax(char *line);
int parsing_error(int err_code, t_xe *xe);
int check_syntax(char *line);
/*
** main functions
*/
t_command *parse_one_command(t_xe *xe);
int parse_input(char **line, char **env, int stat_loc, t_command *command);
t_command *parse_one_command(t_xe *xe);
int parse_input(char **line, char **env, int stat_loc,
t_command *command);
/*
** parsing states
*/
char *space(t_state_machine *machine, char *line);
char *letter(t_state_machine *machine, char *line);
char *backslash(t_state_machine *machine, char *line);
char *dollar(t_state_machine *machine, char *line);
char *single_quote(t_state_machine *machine, char *line);
char *double_quote(t_state_machine *machine, char *line);
char *quoted_backslash(t_state_machine *machine, char *line);
char *quoted_dollar(t_state_machine *machine, char *line);
char *angle_bracket(t_state_machine *machine, char *line);
char *space(t_state_machine *machine, char *line);
char *letter(t_state_machine *machine, char *line);
char *backslash(t_state_machine *machine, char *line);
char *dollar(t_state_machine *machine, char *line);
char *single_quote(t_state_machine *machine, char *line);
char *double_quote(t_state_machine *machine, char *line);
char *quoted_backslash(t_state_machine *machine, char *line);
char *quoted_dollar(t_state_machine *machine, char *line);
char *angle_bracket(t_state_machine *machine, char *line);
/*
** variables
*/
char *parse_variable(t_state_machine *machine, char *line);
int parse_exit_status(t_state_machine *machine);
char *parse_quoted_variable(t_state_machine *machine, char *line);
/*
** redirs
*/
char *new_redir_info(t_state_machine *machine, char *line);
/*
** utils
*/
int new_command(t_list **commands);
char *new_redir_info(t_state_machine *machine, char *line);
int add_variable(t_list **var_list, size_t start, size_t len, t_byte split_flag);
int parse_exit_status(t_state_machine *machine);
char *parse_quoted_variable(t_state_machine *machine, char *line);
char *parse_variable(t_state_machine *machine, char *line);
int add_arg(t_state_machine *machine);
int reset_buf(t_state_machine *machine);
int add_to_buf(t_state_machine *machine, char c);
int add_to_buf(t_state_machine *machine, char c);
int reset_buf(t_state_machine *machine);
int add_arg(t_state_machine *machine);
#endif
......@@ -6,7 +6,7 @@
# By: mvidal-a <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/12/23 13:17:34 by mvidal-a #+# #+# #
# Updated: 2021/01/26 12:41:03 by mvidal-a ### ########.fr #
# Updated: 2021/01/27 11:54:52 by mvidal-a ### ########.fr #
# #
# **************************************************************************** #
......@@ -74,6 +74,7 @@ SRCS_CUSTOM += ft_swap.c
SRCS_CUSTOM += strjoin_free.c
SRCS_CUSTOM += ft_varnamelen.c
SRCS_CUSTOM += blank_str.c
SRCS_CUSTOM += push_int_to_array.c
VPATH = part_1:part_2:bonus:custom
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_int_to_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/27 11:53:41 by mvidal-a #+# #+# */
/* Updated: 2021/01/27 11:53:57 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
int *push_int_to_array(int *array, size_t array_size, int nb)
{
int *new_array;
size_t i;
new_array = (int *)malloc(sizeof(int) * (array_size + 1));
if (new_array == NULL)
return (NULL);
i = 0;
while (i < array_size)
{
new_array[i] = array[i];
i++;
}
new_array[i] = nb;
free(array);
return (new_array);
}
......@@ -6,7 +6,7 @@
/* By: mvidal-a <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/04 12:29:06 by mvidal-a #+# #+# */
/* Updated: 2021/01/26 12:18:43 by mvidal-a ### ########.fr */
/* Updated: 2021/01/27 11:54:32 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
......@@ -118,5 +118,6 @@ char **push_str_to_array(char **array, char *str);
char **rm_str_from_array(char **array, size_t str_pos);
void sort_str_array(char **array);
int *push_int_to_array(int *array, size_t array_size, int nb);
#endif
......@@ -10,32 +10,44 @@ same for CTRL-\, after "^\Quit"
CTRL-D apres avoir ecrit un debut de cmd
echo > $unknown_var ; ls (redir ambigue mais il fait quand meme ls)
$unknown_var ; ls ; ls (premiere commande vide mais il fait quand meme ls)
echo > $unknown_var ; ls ; ls (redir ambigue mais il fait quand meme ls)
create file when redirecting output without doing anything ("> a")
<<<<<<< HEAD
=======
error when redir_in file does not exist ("< unknown_file")
commande "."
>>>>>>> c9a0ae73937d8cb518ce2bf2028d3f3c378803ec
compare with bash: "cat | exit"
"cat |"
then "ls"
check_syntax:
echo "'"
echo '\'
echo '"'
masaki:
add_to_buf malloc error!
masaki:
ajouter des errno = 0 ?
builtins: set stat_loc?
mkdir ok; cd ok; rmdir ../ok; pwd
cd: leaks in getcwd
......
#include <unistd.h>
#include <stdlib.h>
static int line_init(char **line, int offset, int ret)
{
if (!(*line = malloc((offset + 1) * sizeof(char))))
return (-1);
((*line)[offset]) = '\0';
return (ret);
}
static int recursive_get_line(char **line, int fd, int offset)
{
char buffer;
int ret;
ret = read(fd, &buffer, 1);
if (ret == 1)
{
if (buffer == '\n' || buffer == '\0')
return (line_init(line, offset, 1));
else
{
ret = recursive_get_line(line, fd, offset + 1);
if ((*line) != NULL)
((*line)[offset]) = buffer;
return (ret);
}
}
else if (ret == 0)
return (line_init(line, offset, 0));
return (-1);
}
int rec_gnl(int fd, char **line)
{
char buffer;
if (fd < 0 || !line || read(fd, &buffer, 0) < 0)
return (-1);
*line = NULL;
return (recursive_get_line(line, fd, 0));
}
......@@ -9,8 +9,8 @@
void child_setup(const int *fd, int fd_in)
{
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGINT, SIG_DFL); // error if SIG_ERR? (check errno)
signal(SIGQUIT, SIG_DFL); // error if SIG_ERR? (check errno)
close(fd[0]);// error
if (fd_in != STDIN_FILENO)
{
......
......@@ -19,8 +19,7 @@ int ft_cd(char **args, t_xe *xe)
path = ft_strdup(args[1]);
if (path == NULL)
return (SUCCESS); // err
errno = 0;
oldpwd = getcwd(NULL, 0);
oldpwd = getcwd(NULL, 0); // error
if (chdir(path) == ERROR)
{
perror("cd");
......
......@@ -20,6 +20,7 @@ static int search_path(DIR *dirp, char *name)
{
struct dirent *buf;
errno = 0; // to distinguish the NULL ret value: end of stream, or error
buf = readdir(dirp);
while (buf != NULL)
{
......@@ -27,6 +28,8 @@ static int search_path(DIR *dirp, char *name)
return (SUCCESS);
buf = readdir(dirp);
}
if (errno != 0)
;//error
return (FAILURE);
}
......@@ -100,8 +103,8 @@ static int launch_ext(char **args, t_xe *xe)
}
else
{
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGINT, SIG_IGN); // error if SIG_ERR? (check errno)
signal(SIGQUIT, SIG_IGN); // error if SIG_ERR? (check errno)
waitpid(pid, &xe->stat_loc, 0); // return value? error?
free_str_array(path);
if (WIFSIGNALED(xe->stat_loc)) // check this return value
......@@ -110,7 +113,7 @@ static int launch_ext(char **args, t_xe *xe)
}
else if (xe->stat_loc > 256)
xe->stat_loc = xe->stat_loc / 256;
signal_handler();
signal_handler(); // err
}
return (ret);
}
......
......@@ -83,6 +83,7 @@ int clean_and_exit(int ret, t_xe *xe)
free_str_array(xe->env); // besoin de free?
free(xe);
if (ret != CHILD_EXIT)
if (isatty(STDIN_FILENO)) // temp pour le testeur
write(1, "exit\n", 5);
exit(EXIT_SUCCESS);
}
......
......@@ -33,9 +33,17 @@ char *double_quote(t_state_machine *machine, char *line)
char *quoted_backslash(t_state_machine *machine, char *line)
{
if (!ft_isset(*line, "\\\"$"))
if (ft_isset(*line, "\\\"$"))
{
if (add_to_buf(machine, *line) == FAILURE)
return (NULL);
line++;
}
else
{
if (add_to_buf(machine, '\\') == FAILURE)
return (NULL);
}
machine->state = DOUBLE_QUOTE;
return (line);
}
......
#include "parsing.h"
int add_arg(t_state_machine *machine)
{
if (reset_buf(machine) == FAILURE)
return (FAILURE);
if (machine->cur_token_stack == &machine->redir_paths
&& ((machine->var_token_count >= 1 && machine->cur_arg[0] != '\0')
|| (machine->var_state == TRUE && machine->var_token_count == 0
&& machine->cur_arg[0] == '\0')))
{
machine->redir_types[0] = AMBIG;
}
else if (machine->quote_state == TRUE || machine->cur_arg[0] != '\0')
{
*machine->cur_token_stack = push_str_to_array(*machine->cur_token_stack,
machine->cur_arg);
if (*machine->cur_token_stack == NULL)
return (FAILURE);
machine->cur_arg = NULL;
}
machine->quote_state = FALSE;
return (SUCCESS);
}
int reset_buf(t_state_machine *machine)
{
if (machine->cur_arg == NULL)
machine->cur_arg = ft_strdup(machine->buf);
else if (machine->len != 0)
machine->cur_arg = strjoin_free(machine->cur_arg, machine->buf);
if (machine->cur_arg == NULL)
return (FAILURE);
if (machine->len != 0)
{
machine->len = 0;
ft_bzero(&machine->buf, BUF_SIZE);
}
return (SUCCESS);
}
int add_to_buf(t_state_machine *machine, char c)
{
if (machine->len == BUF_SIZE - 1)
{
if (reset_buf(machine) == FAILURE)
return (FAILURE);
}
machine->buf[machine->len] = c;
machine->len++;
return (SUCCESS);
}
void free_command(void *content)
{
t_command *command;
command = (t_command *)content;
free_str_array(command->args);
free_str_array(command->redir_paths);
free(command->redir_types);
free(command);
}
void free_commands(t_list **commands)
{
ft_lstclear(commands, free_command);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_vars_redirs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/27 12:14:56 by mvidal-a #+# #+# */
/* Updated: 2021/01/27 12:18:48 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
int *push_int_to_array(int *array, size_t array_size, int nb)
static int add_var_args(t_state_machine *machine, char *var_value)
{
int *new_array;
size_t i;
new_array = (int *)malloc(sizeof(int) * (array_size + 1));
if (new_array == NULL)
return (NULL);
i = 0;
while (i < array_size)
if (ft_isspace(*var_value) && machine->cur_arg[0] != '\0')
{
new_array[i] = array[i];
i++;
if (add_arg(machine) == FAILURE)
return (FAILURE);
machine->var_token_count++;
var_value++;
}
new_array[i] = nb;
free(array);
return (new_array);
while (ft_isspace(*var_value))
var_value++;
while (*var_value != '\0')
{
if (ft_isspace(*var_value))
{
if (add_arg(machine) == FAILURE)
return (FAILURE);
machine->var_token_count++;
if (blank_str(var_value) == TRUE)
break ;
while (ft_isspace(*var_value))
var_value++;
}
if (add_to_buf(machine, *var_value++) == FAILURE)
return (FAILURE);
}
return (SUCCESS);
}
char *new_redir_info(t_state_machine *machine, char *line)
char *parse_variable(t_state_machine *machine, char *line)
{
enum e_redir_op redir_type;
size_t var_name_len;
char *var_value;
if (*line == '<')
redir_type = FILEIN;
else if (line[1] == '>')
if (reset_buf(machine) == FAILURE)
return (NULL);
var_name_len = ft_varnamelen(line);
var_value = get_var_value(machine->env, line, var_name_len);
if (var_value == NULL)
return (NULL);
if (add_var_args(machine, var_value) == FAILURE)
{
redir_type = APPEND;
line++;
free(var_value);
return (NULL);
}
else
redir_type = FILEOUT;
machine->redir_types =
(enum e_redir_op *)push_int_to_array((int *)machine->redir_types,
ft_arraylen(machine->redir_paths), (int)redir_type);
free(var_value);
line += var_name_len;
machine->var_state = TRUE;
return (line);
}
int parse_exit_status(t_state_machine *machine)
int parse_exit_status(t_state_machine *machine)
{
char *value;
size_t i;
......@@ -60,7 +85,7 @@ int parse_exit_status(t_state_machine *machine)
return (SUCCESS);
}
char *parse_quoted_variable(t_state_machine *machine, char *line)
char *parse_quoted_variable(t_state_machine *machine, char *line)
{
size_t var_name_len;
char *var_value;
......@@ -85,127 +110,21 @@ char *parse_quoted_variable(t_state_machine *machine, char *line)
return (line);
}
//int add_var_arg(t_state_machine *machine)
//{
//}
char *parse_variable(t_state_machine *machine, char *line)
char *new_redir_info(t_state_machine *machine, char *line)
{
size_t var_name_len;
char *var_value;
size_t i;
enum e_redir_op redir_type;
if (reset_buf(machine) == FAILURE)
return (NULL);
var_name_len = ft_varnamelen(line);
var_value = get_var_value(machine->env, line, var_name_len);
if (var_value == NULL)
return (NULL);
i = 0;
if (ft_isspace(var_value[i]) && machine->cur_arg[0] != '\0')
{
if (add_arg(machine) == FAILURE)
{
free(var_value);
return (NULL);
}
machine->var_token_count++;
i++;
}
while (ft_isspace(var_value[i]))
i++;
while (var_value[i] != '\0')
if (*line == '<')
redir_type = FILEIN;
else if (line[1] == '>')
{
if (ft_isspace(var_value[i]))
{
if (add_arg(machine) == FAILURE)
{
free(var_value);
return (NULL);
}
machine->var_token_count++;
i++; // optional
if (blank_str(var_value + i) == TRUE)
break ;
while (ft_isspace(var_value[i]))
i++;
}
if (add_to_buf(machine, var_value[i]) == FAILURE)
{
free(var_value);
return (NULL);
}
i++;
redir_type = APPEND;
line++;
}
free(var_value);
line += var_name_len;
machine->var_state = TRUE;
else
redir_type = FILEOUT;
machine->redir_types =
(enum e_redir_op *)push_int_to_array((int *)machine->redir_types,
ft_arraylen(machine->redir_paths), (int)redir_type);
return (line);
}
int add_arg(t_state_machine *machine)
{
if (reset_buf(machine) == FAILURE)
return (FAILURE);
if (machine->cur_token_stack == &machine->redir_paths
&& ((machine->var_token_count >= 1 && machine->cur_arg[0] != '\0')