Commit a13320af authored by pêle-mêle's avatar pêle-mêle
Browse files

norm for parsing functions

parent b28b92d9
......@@ -6,9 +6,13 @@ SRC_DIR += src/execution/
SRC += main.c
SRC += signal_handling.c
SRC += parse_input.c
SRC += check_syntax.c
SRC += parsing_handler.c
SRC += parsing_states1.c
SRC += parsing_states2.c
SRC += parse_utils.c
SRC += exec_handler.c
SRC += launch_command.c
......
......@@ -68,11 +68,25 @@ int parsing_error(int err_code, t_xe *xe);
int check_syntax(char *line);
/*
** main function
** main functions
*/
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);
/*
** utils
......
......@@ -49,7 +49,11 @@ int parse_exit_status(t_state_machine *machine)
i = 0;
while (value[i] != '\0')
{
add_to_buf(machine, value[i]);
if (add_to_buf(machine, value[i]) == FAILURE)
{
free(value);
return (FAILURE);
}
i++;
}
free(value);
......@@ -69,7 +73,11 @@ char *parse_quoted_variable(t_state_machine *machine, char *line)
i = 0;
while (var_value[i] != '\0')
{
add_to_buf(machine, var_value[i]);
if (add_to_buf(machine, var_value[i]) == FAILURE)
{
free(var_value);
return (NULL);
}
i++;
}
free(var_value);
......@@ -77,6 +85,10 @@ 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)
{
size_t var_name_len;
......@@ -118,7 +130,11 @@ char *parse_variable(t_state_machine *machine, char *line)
while (ft_isspace(var_value[i]))
i++;
}
add_to_buf(machine, var_value[i]);
if (add_to_buf(machine, var_value[i]) == FAILURE)
{
free(var_value);
return (NULL);
}
i++;
}
free(var_value);
......
#include "parsing.h"
int parse_input(char **line, char **env, int stat_loc,
t_command *command)
{
static t_parse process[NB_STATES - 1] = {space, letter, backslash, dollar,
single_quote, double_quote, quoted_backslash, quoted_dollar,
angle_bracket};
t_state_machine machine;
ft_bzero(&machine, sizeof(machine));
machine.env = env;
machine.stat_loc = stat_loc;
machine.cur_token_stack = &machine.args;
while (machine.state != END)
{
*line = process[machine.state](&machine, *line);
if (*line == NULL)
break ;
}
free(machine.cur_arg);
command->pipe_flag = machine.pipe_flag;
command->redir_paths = machine.redir_paths;
command->redir_types = machine.redir_types;
command->args = machine.args;
if (*line == NULL)
return (FAILURE);
return (SUCCESS);
}
t_command *parse_one_command(t_xe *xe)
{
t_command *cur_command;
int ret;
cur_command = (t_command *)malloc(sizeof(t_command));
if (cur_command == NULL)
return (NULL);
ret = parse_input(&xe->line, xe->env, xe->stat_loc, cur_command);
if (ret == FAILURE)
{
free_command(cur_command);
return (NULL);
}
return (cur_command);
}
#include "parsing.h"
char *angle_bracket(t_state_machine *machine, char *line)
{
line = new_redir_info(machine, line);
if (line == NULL)
return (NULL);
line++;
machine->state = SPACE;
machine->cur_token_stack = &machine->redir_paths;
return (line);
}
char *quoted_dollar(t_state_machine *machine, char *line)
char *letter(t_state_machine *machine, char *line)
{
if (*line == '?')
if (ft_isspace(*line) || ft_isset(*line, "><;|") || *line == '\0')
{
if (parse_exit_status(machine) == FAILURE)
if (add_arg(machine) == FAILURE)
return (NULL);
line++;
machine->cur_token_stack = &machine->args;
machine->var_token_count = 0;
machine->var_state = FALSE;
machine->state = SPACE;
}
else if (ft_isalnum(*line) || *line == '_')
else
{
line = parse_quoted_variable(machine, line);
if (line == NULL)
if (*line == '\\')
machine->state = BACKSLASH;
else if (*line == '$')
machine->state = DOLLAR;
else if (*line == '\'')
machine->state = SINGLE_QUOTE;
else if (*line == '"')
machine->state = DOUBLE_QUOTE;
else if (add_to_buf(machine, *line) == FAILURE)
return (NULL);
line++;
}
else
add_to_buf(machine, '$');
machine->state = DOUBLE_QUOTE;
return (line);
}
char *quoted_backslash(t_state_machine *machine, char *line)
{
if (!ft_isset(*line, "\\\"$"))
add_to_buf(machine, '\\');
machine->state = DOUBLE_QUOTE;
return (line);
}
char *double_quote(t_state_machine *machine, char *line)
char *space(t_state_machine *machine, char *line)
{
if (*line == '"')
if (ft_isspace(*line))
line++;
else if (*line == '>' || *line == '<')
machine->state = ANGLE_BRACKET;
else if (*line == '|')
{
machine->state = LETTER;
machine->quote_state = TRUE;
machine->state = END;
machine->pipe_flag = TRUE;
line++;
}
else if (*line == ';')
{
machine->state = END;
line++;
}
else if (*line == '$')
machine->state = QUOTED_DOLLAR;
else if (*line == '\\')
machine->state = QUOTED_BACKSLASH;
else if (*line == '\0')
machine->state = END;
else
add_to_buf(machine, *line);
line++;
machine->state = LETTER;
return (line);
}
char *single_quote(t_state_machine *machine, char *line)
char *backslash(t_state_machine *machine, char *line)
{
while (*line != '\'')
{
add_to_buf(machine, *line);
line++;
}
if (add_to_buf(machine, *line) == FAILURE)
return (NULL);
line++;
machine->state = LETTER;
machine->quote_state = TRUE;
return (line);
}
......@@ -85,115 +77,13 @@ char *dollar(t_state_machine *machine, char *line)
line = parse_variable(machine, line);
if (line == NULL)
return (NULL);
machine->state = LETTER; // don't finish arg when empty var value
}
else
{
add_to_buf(machine, '$');
machine->state = LETTER;
}
return (line);
}
char *backslash(t_state_machine *machine, char *line)
{
add_to_buf(machine, *line);
line++;
machine->state = LETTER;
return (line);
}
char *space(t_state_machine *machine, char *line)
{
if (ft_isspace(*line))
line++;
else if (*line == '>' || *line == '<')
machine->state = ANGLE_BRACKET;
else if (*line == '|')
{
machine->state = END;
machine->pipe_flag = TRUE;
line++;
}
else if (*line == ';')
{
machine->state = END;
line++;
}
else if (*line == '\0')
machine->state = END;
else
machine->state = LETTER;
return (line);
}
char *letter(t_state_machine *machine, char *line)
{
if (ft_isspace(*line) || ft_isset(*line, "><;|") || *line == '\0')
{
if (add_arg(machine) == FAILURE)
if (add_to_buf(machine, '$') == FAILURE)
return (NULL);
machine->cur_token_stack = &machine->args;
machine->var_token_count = 0;
machine->var_state = FALSE;
machine->state = SPACE;
}
else
{
if (*line == '\\')
machine->state = BACKSLASH;
else if (*line == '$')
machine->state = DOLLAR;
else if (*line == '\'')
machine->state = SINGLE_QUOTE;
else if (*line == '"')
machine->state = DOUBLE_QUOTE;
else
add_to_buf(machine, *line);
line++;
machine->state = LETTER;
}
return (line);
}
int parse_input(char **line, char **env, int stat_loc, t_command *command)
{
static t_parse process[NB_STATES - 1] = {space, letter, backslash, dollar,
single_quote, double_quote, quoted_backslash, quoted_dollar, angle_bracket};
t_state_machine machine;
ft_bzero(&machine, sizeof(machine));
machine.env = env;
machine.stat_loc = stat_loc;
machine.cur_token_stack = &machine.args;
while (machine.state != END)
{
*line = process[machine.state](&machine, *line);
if (*line == NULL)
break ;
}
free(machine.cur_arg);
command->pipe_flag = machine.pipe_flag;
command->redir_paths = machine.redir_paths;
command->redir_types = machine.redir_types;
command->args = machine.args;
if (*line == NULL)
return (FAILURE);
return (SUCCESS);
}
t_command *parse_one_command(t_xe *xe)
{
t_command *cur_command;
int ret;
cur_command = (t_command *)malloc(sizeof(t_command));
if (cur_command == NULL)
return (NULL);
ret = parse_input(&xe->line, xe->env, xe->stat_loc, cur_command);
if (ret == FAILURE)
{
free_command(cur_command);
return (NULL);
}
return (cur_command);
}
#include "parsing.h"
char *single_quote(t_state_machine *machine, char *line)
{
while (*line != '\'')
{
if (add_to_buf(machine, *line) == FAILURE)
return (NULL);
line++;
}
line++;
machine->state = LETTER;
machine->quote_state = TRUE;
return (line);
}
char *double_quote(t_state_machine *machine, char *line)
{
if (*line == '"')
{
machine->state = LETTER;
machine->quote_state = TRUE;
}
else if (*line == '$')
machine->state = QUOTED_DOLLAR;
else if (*line == '\\')
machine->state = QUOTED_BACKSLASH;
else if (add_to_buf(machine, *line) == FAILURE)
return (NULL);
line++;
return (line);
}
char *quoted_backslash(t_state_machine *machine, char *line)
{
if (!ft_isset(*line, "\\\"$"))
if (add_to_buf(machine, '\\') == FAILURE)
return (NULL);
machine->state = DOUBLE_QUOTE;
return (line);
}
char *quoted_dollar(t_state_machine *machine, char *line)
{
if (*line == '?')
{
if (parse_exit_status(machine) == FAILURE)
return (NULL);
line++;
}
else if (ft_isalnum(*line) || *line == '_')
{
line = parse_quoted_variable(machine, line);
if (line == NULL)
return (NULL);
}
else if (add_to_buf(machine, '$') == FAILURE)
return (NULL);
machine->state = DOUBLE_QUOTE;
return (line);
}
char *angle_bracket(t_state_machine *machine, char *line)
{
line = new_redir_info(machine, line);
if (line == NULL)
return (NULL);
line++;
machine->state = SPACE;
machine->cur_token_stack = &machine->redir_paths;
return (line);
}
Supports Markdown
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