Skip to content

Commit

Permalink
fixed remove_quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptiste Renaudon authored and Baptiste Renaudon committed Jan 18, 2023
1 parent ac6fb32 commit a1a7ddc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 51 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ SRCS = add_cmd.c \
pipex_children.c \
print.c \
pwd.c \
remove_quotes.c \
signal.c \
split_cmds.c \
unset.c \
Expand Down
2 changes: 1 addition & 1 deletion find_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int find_quote(const char *s, char c)
i++;
if (s[i] == (char)c)
return (i + 1);
return (0);
return (1);
}

t_token *init_token(const char *str, int str_idx, int idx, t_label label)
Expand Down
1 change: 0 additions & 1 deletion pipex_children.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ void child(t_cmd *cmd, int *pipes, int *children_pid)
{
g_glob->exit_ret = 1;
cmd_split = ft_split_quotes(cmd->str, " \t");
cmd_split[0] = remove_quotes(cmd_split[0]);
cmd_split[0] = get_path(cmd_split[0], false);
exit_on_error(cmd, cmd_split, pipes, children_pid);
dup_and_close(cmd, pipes);
Expand Down
98 changes: 98 additions & 0 deletions remove_quotes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* remove_quotes.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/18 10:43:05 by brenaudo #+# #+# */
/* Updated: 2023/01/18 10:43:08 by brenaudo ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

int count_quotes(char *str)
{
int i;
int count;

i = 0;
count = 0;
while (i < (int)ft_strlen(str) && str[i])
{
if (str[i] == '"')
i += find_quote(str + i + 1, '"');
if (str[i] == '\'')
i += find_quote(str + i + 1, '\'');
if (str[i] == '"' || str[i] == '\'')
count += 2;
i++;
}
return (count);
}

static int count_quotes_type(char *str, char quote_type)
{
int count;
int i;

i = 0;
count = 0;
while (str && str[i])
{
if (str[i] == quote_type)
count++;
i++;
}
return (count);
}

static bool is_closing_quote(char *str, int i_init, char quote_type)
{
int i;

i = i_init + 1;
while (str && str[i] && str[i] != quote_type)
i++;
if (str && str[i] == quote_type)
return (true);
if (str && !str[i] && count_quotes_type(str, quote_type) % 2 == 0)
return (true);
return (false);
}

static void init_remove_quotes(int *i, int *j, int *double_q, int *single_q)
{
*double_q = 0;
*single_q = 0;
*i = 0;
*j = 0;
}

char *remove_quotes(char *str)
{
int i;
int j;
char *ret;
int double_q;
int single_q;

ret = ft_calloc(((ft_strlen(str) - count_quotes(str) + 1)), sizeof(char));
init_remove_quotes(&i, &j, &double_q, &single_q);
while (str[i])
{
if (str[i] == '"' && !single_q && is_closing_quote(str, i, '\"'))
double_q = !double_q;
else if (str[i] == '\'' && !double_q && is_closing_quote(str, i, '\''))
single_q = !single_q;
else if ((str[i] == '"' && single_q) || (str[i] == '\'' && double_q)
|| (str[i] == '"' && !is_closing_quote(str, i, '\"'))
|| (str[i] == '\'' && !is_closing_quote(str, i, '\''))
|| (str[i] != '"' && str[i] != '\''))
ret[j++] = str[i];
i++;
}
free(str);
return (ret);
}
49 changes: 0 additions & 49 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,52 +53,3 @@ char **envp_list_to_tab(void)
ret[i] = NULL;
return (ret);
}

int count_quotes(char *str)
{
int i;
int count;

i = 0;
count = 0;
while (i < (int)ft_strlen(str) && str[i])
{
if (str[i] == '"')
i += find_quote(str + i + 1, '"');
if (str[i] == '\'')
i += find_quote(str + i + 1, '\'');
if (str[i] == '"' || str[i] == '\'')
count += 2;
i++;
}
return (count);
}

char *remove_quotes(char *str)
{
int i;
int j;
char *ret;
int double_q;
int single_q;

ret = ft_calloc(((ft_strlen(str) - count_quotes(str) + 1)), sizeof(char));
double_q = 0;
single_q = 0;
i = 0;
j = 0;
while (str[i])
{
if (str[i] == '"' && !single_q)
double_q = !double_q;
else if (str[i] == '\'' && !double_q)
single_q = !single_q;
else if ((str[i] == '"' && single_q)
|| (str[i] == '\'' && double_q)
|| (str[i] != '"' && str[i] != '\''))
ret[j++] = str[i];
i++;
}
free(str);
return (ret);
}

0 comments on commit a1a7ddc

Please sign in to comment.