Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facetint #77

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SOURCES = src/execute/execute_utils.c src/builtin/cd.c src/builtin/exit.c src/bu
src/builtin/env.c src/builtin/pwd.c src/builtin/builtin.c src/builtin/unset.c get_next_line/get_next_line.c \
src/expander/expander_nonvariables.c src/env/env_utils.c src/lexer/utils.c src/handler.c src/env/env.c\
src/lexer/lexer.c src/lexer/unquote.c src/lexer/lexer_utils.c src/parser.c src/execute/execute.c \
src/execute/error_message.c src/execute/dup2.c src/execute/heredoc.c src/expander/expander.c src/splitter.c src/lexer/syntax_analyzer.c src/signal.c $(MEMORY_ALLOCATOR_SOURCES)
src/lexer/lexer_error_message.c src/lexer/is_valid.c src/execute/error_message.c src/execute/dup2.c src/execute/heredoc.c src/expander/expander.c src/splitter.c src/lexer/syntax_analyzer.c src/signal.c $(MEMORY_ALLOCATOR_SOURCES)

MINISHELL_SOURCES = src/main.c $(SOURCES)
MINISHELL_OBJECTS = $(MINISHELL_SOURCES:.c=.o)
Expand Down
19 changes: 1 addition & 18 deletions src/handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:17:46 by facetint #+# #+# */
/* Updated: 2024/03/28 16:06:39 by facetint ### ########.fr */
/* Updated: 2024/03/29 17:19:29 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -102,16 +102,6 @@ void handle_file_redirections(t_command *cur)
}
}

void handle_invalid_input(t_token *lexical_data)
{
if (lexical_data)
{
ft_putstr_fd("minishell: syntax error near unexpected token\n" , 2);
*get_exit_status() = 258;
}
uninit_tokens(lexical_data);
}

void handle_input(char *input)
{
t_token *lexer_data;
Expand All @@ -129,10 +119,3 @@ void handle_input(char *input)
signal_type = PROMPT;
uninit_tokens(lexer_data);
}

void handle_memory_error(void)
{
ft_putstr_fd("Insufficent memory! Minishell aborting...", 2);
free_list(*get_global_env());
exit(1);
}
42 changes: 42 additions & 0 deletions src/lexer/lexer_error_message.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_error_message.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/29 17:18:11 by facetint #+# #+# */
/* Updated: 2024/03/29 17:24:20 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

#include "../../includes/minishell.h"
#include "../../libft/libft.h"
#include "../../includes/utils.h"
#include "../../includes/char_classification.h"
#include "../../memory-allocator/allocator.h"
#include "../../includes/env.h"

void unexpected_token_error(t_token *token)
{
if (token == NULL)
return ft_putstr_fd("syntax error occurred, null token found.\n", 2);
ft_putstr_fd("syntax error. lol.\n", 2);
}

void handle_invalid_input(t_token *lexical_data)
{
if (lexical_data)
{
ft_putstr_fd("minishell: syntax error near unexpected token\n" , 2);
*get_exit_status() = 258;
}
uninit_tokens(lexical_data);
}

void handle_memory_error(void)
{
ft_putstr_fd("Insufficent memory! Minishell aborting...", 2);
free_list(*get_global_env());
exit(1);
}
89 changes: 20 additions & 69 deletions src/lexer/syntax_analyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:18:35 by facetint #+# #+# */
/* Updated: 2024/03/28 16:58:07 by facetint ### ########.fr */
/* Updated: 2024/03/29 17:13:54 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,45 +15,45 @@
#include "../../includes/char_classification.h"
#include "../../includes/utils.h"

int is_word(t_token_type type)
int is_word(t_token_type type)
{
return type == UNQUOTED_WORD ||
type == SINGLE_QUOTED_WORD ||
type == DOUBLE_QUOTED_WORD;
return (type == UNQUOTED_WORD ||
type == SINGLE_QUOTED_WORD ||
type == DOUBLE_QUOTED_WORD);
}

int is_operator(t_token_type type)
int is_operator(t_token_type type)
{
return type == HEREDOC_REDIRECTION ||
type == INPUT_REDIRECTION ||
type == OUTPUT_REDIRECTION ||
type == APPEND_REDIRECTION ||
type == PIPE;
return (type == HEREDOC_REDIRECTION ||
type == INPUT_REDIRECTION ||
type == OUTPUT_REDIRECTION ||
type == APPEND_REDIRECTION ||
type == PIPE);
}

int is_there_lack_of_word(t_token *token)
int is_there_lack_of_word(t_token *token)
{
int wait_for_word;
int wait_for_word;

wait_for_word = 0;
while (token)
{
if (token->type == DELIMITER)
token = token->next;
if (wait_for_word && !is_word(token->type))
return 0;
return (0);
if (is_word(token->type))
wait_for_word = 0;
else if (is_operator(token->type))
wait_for_word = 1;
token = token->next;
}
return wait_for_word == 0;
return (wait_for_word == 0);
}

int validate_pipes(t_token *token)
{
int args;
int args;

args = 0;
while (token)
Expand All @@ -62,7 +62,7 @@ int validate_pipes(t_token *token)
{
args++;
token = token->next;
continue;
continue ;
}

if (is_operator(token->type) && token->type != PIPE)
Expand All @@ -71,66 +71,17 @@ int validate_pipes(t_token *token)
if (token && token->type == DELIMITER)
token = token->next;
if (!token || !is_word(token->type))
return 0;
return (0);
token = token->next;
continue;
continue ;
}

if (token->type == PIPE)
{
if (args == 0)
return 0;
args = 0;
}
token = token->next;
}
return 1;
}

int are_quotes_valid(t_token *token)
{
unsigned int length;

while (token)
{
if (token->type == SINGLE_QUOTED_WORD)
{
length = ft_strlen(token->value);
if (length <= 1
|| token->value[0] != SINGLE_QUOTE
|| token->value[length - 1] != SINGLE_QUOTE)
return (0);
}
else if (token->type == DOUBLE_QUOTED_WORD)
{
length = ft_strlen(token->value);
if (length <= 1
|| token->value[0] != DOUBLE_QUOTE
|| token->value[length - 1] != DOUBLE_QUOTE
|| is_escaped(token->value, length - 1))
return (0);
args = 0;
}
token = token->next;
}
return (1);
}

int are_tokens_valid(t_token *lexer_data)
{
while (lexer_data)
{
if (lexer_data->type == UNKNOWN)
return 0;
if (is_word(lexer_data->type) && lexer_data->value == NULL)
return 0;
lexer_data = lexer_data->next;
}
return 1;
}

int is_valid(t_token *lexer_data) {
return are_tokens_valid(lexer_data)
&& are_quotes_valid(lexer_data)
&& is_there_lack_of_word(lexer_data)
&& validate_pipes(lexer_data);
}
9 changes: 1 addition & 8 deletions src/lexer/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:18:44 by facetint #+# #+# */
/* Updated: 2024/03/28 16:53:49 by facetint ### ########.fr */
/* Updated: 2024/03/29 17:18:50 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -155,10 +155,3 @@ char *get_prompt()
prompt = ft_str_arr_join((char *[]){path, "$ "}, 2);
return prompt;*/
}

void unexpected_token_error(t_token *token)
{
if (token == NULL)
return ft_putstr_fd("syntax error occurred, null token found.\n", 2);
ft_putstr_fd("syntax error. lol.\n", 2);
}
Loading