Skip to content

Commit

Permalink
norminette passed, final tests to do
Browse files Browse the repository at this point in the history
  • Loading branch information
¨Roman committed Nov 9, 2024
1 parent 3644cfe commit 1be3469
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
6 changes: 6 additions & 0 deletions inc/tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,11 @@ void final_quote_removal(int arg_count, t_ast *command_node);
char **ft_split_preserve_quotes(char *str, char delimiter);
char **ft_add_segment(char **result, char *start, size_t length, \
int *count);
char **initialize_result_array(int *count);
int toggle_quotes(int in_quotes, char current_char);
char **add_segment_on_delimiter(char **result, char **start, char \
*str, int *count);
char **finalize_result_array(char **result, int count);
char **ft_split_preserve_quotes(char *str, char delimiter);

#endif
Binary file removed minishell
Binary file not shown.
2 changes: 2 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ void main_loop(t_ms_data *data, t_loop_data *loop_data)
set_signals_interactive(data);
set_signals_noninteractive();
loop_data->input = readline("🌴 maxishell> ");
if (loop_data->input[0] == '\0')
continue ;
if (!loop_data->input || !ft_strncmp(loop_data->input, "exit", 4))
{
data->args = ft_split(loop_data->input, ' ');
Expand Down
26 changes: 6 additions & 20 deletions src/parser/loc_env_var_handler_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,38 +98,24 @@ char **ft_add_segment(char **result, char *start, size_t length, int *count)

char **ft_split_preserve_quotes(char *str, char delimiter)
{
char **result;
int count;
int in_quotes;
char *start;
char **temp;
char **result;

count = 0;
in_quotes = 0;
start = str;
result = malloc(sizeof(char *));
result = initialize_result_array(&count);
if (!result)
return (NULL);
while (*str)
{
if (*str == '"' || *str == '\'')
in_quotes = !in_quotes;
else if (*str == delimiter && !in_quotes)
{
result = ft_add_segment(result, start, str - start, &count);
start = str + 1;
}
in_quotes = toggle_quotes(in_quotes, *str);
if (*str == delimiter && !in_quotes)
result = add_segment_on_delimiter(result, &start, str, &count);
str++;
}
if (str != start)
result = ft_add_segment(result, start, str - start, &count);
temp = realloc(result, sizeof(char *) * (count + 1));
if (!temp)
{
free(result);
return (NULL);
}
result = temp;
result[count] = NULL;
return (result);
return (finalize_result_array(result, count));
}
54 changes: 54 additions & 0 deletions src/parser/loc_env_var_handler_utils_utils_utils_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* loc_env_var_handler_utils_utils_utils_uti :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rmikhayl <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/09 12:32:24 by rmikhayl #+# #+# */
/* Updated: 2024/11/09 12:32:26 by rmikhayl ### ########.fr */
/* */
/* ************************************************************************** */

#include "tokens.h"

char **initialize_result_array(int *count)
{
char **result;

*count = 0;
result = malloc(sizeof(char *));
if (!result)
return (NULL);
return (result);
}

int toggle_quotes(int in_quotes, char current_char)
{
if (current_char == '"' || current_char == '\'')
return (!in_quotes);
return (in_quotes);
}

char **add_segment_on_delimiter(char **result, char **start, \
char *str, int *count)
{
result = ft_add_segment(result, *start, str - *start, count);
*start = str + 1;
return (result);
}

char **finalize_result_array(char **result, int count)
{
char **temp;

temp = realloc(result, sizeof(char *) * (count + 1));
if (!temp)
{
free(result);
return (NULL);
}
result = temp;
result[count] = NULL;
return (result);
}

0 comments on commit 1be3469

Please sign in to comment.