implement quote
This commit is contained in:
parent
0ea63d6d6c
commit
b0cd55b11c
5
Makefile
5
Makefile
@ -6,7 +6,7 @@
|
|||||||
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2022/03/06 12:50:24 by apommier #+# #+# #
|
# Created: 2022/03/06 12:50:24 by apommier #+# #+# #
|
||||||
# Updated: 2022/04/11 17:14:26 by apommier ### ########.fr #
|
# Updated: 2022/04/12 23:31:57 by apommier ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -27,8 +27,9 @@ SRCS = srcs/main.c\
|
|||||||
srcs/built_in/export2.c\
|
srcs/built_in/export2.c\
|
||||||
srcs/built_in/env.c\
|
srcs/built_in/env.c\
|
||||||
srcs/set_quote/split_with_quote.c\
|
srcs/set_quote/split_with_quote.c\
|
||||||
|
srcs/set_quote/set_quote.c\
|
||||||
srcs/built_in/choose_builtin.c
|
srcs/built_in/choose_builtin.c
|
||||||
# srcs/set_quote/set_quote.c\
|
|
||||||
|
|
||||||
|
|
||||||
OBJS = ${SRCS:.c=.o}
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
|
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
|
||||||
/* Updated: 2022/04/11 17:53:06 by apommier ### ########.fr */
|
/* Updated: 2022/04/12 23:42:20 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -78,6 +78,8 @@ void free_cmd(t_cmd *cmd);
|
|||||||
void exit_shell(t_cmd *cmd);
|
void exit_shell(t_cmd *cmd);
|
||||||
|
|
||||||
//set_quote.c
|
//set_quote.c
|
||||||
|
int is_in_quote(char *str, int index);
|
||||||
|
int is_quote_good(char *str);
|
||||||
char **ft_split_with_quote(char const *s, char c);
|
char **ft_split_with_quote(char const *s, char c);
|
||||||
|
|
||||||
//redirection.c set redirection and input good
|
//redirection.c set redirection and input good
|
||||||
@ -85,6 +87,9 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index);
|
|||||||
char next_space(char *str, int i);
|
char next_space(char *str, int i);
|
||||||
|
|
||||||
//uitls redirection
|
//uitls redirection
|
||||||
|
int parse_quote(t_cmd *cmd);
|
||||||
|
char *get_str(char *str, int start, int end);
|
||||||
|
char *cut_str(char *str, int start, int end);
|
||||||
int double_size(char **tab);
|
int double_size(char **tab);
|
||||||
void print_double_fd(char **tab, int fd);
|
void print_double_fd(char **tab, int fd);
|
||||||
void free_double(char **tab);
|
void free_double(char **tab);
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
||||||
/* Updated: 2022/04/11 17:47:41 by apommier ### ########.fr */
|
/* Updated: 2022/04/13 00:09:42 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -78,7 +78,6 @@ void print_prompt(char **path)
|
|||||||
}
|
}
|
||||||
if (ft_strlen(input) && next_space(input, 0) && input)
|
if (ft_strlen(input) && next_space(input, 0) && input)
|
||||||
{
|
{
|
||||||
|
|
||||||
cmd = set_cmd(input, path);
|
cmd = set_cmd(input, path);
|
||||||
if (cmd)
|
if (cmd)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
|
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
|
||||||
/* Updated: 2022/04/11 17:49:06 by apommier ### ########.fr */
|
/* Updated: 2022/04/13 01:28:18 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
t_s_cmd *set_s_cmd(char *line, t_cmd *cmd, int index)
|
t_s_cmd *set_s_cmd(char *line, int index)
|
||||||
{
|
{
|
||||||
t_s_cmd *s_cmd;
|
t_s_cmd *s_cmd;
|
||||||
char **split_line;
|
char **split_line;
|
||||||
@ -33,11 +33,17 @@ t_s_cmd *set_s_cmd(char *line, t_cmd *cmd, int index)
|
|||||||
//printf("before redirect\n");
|
//printf("before redirect\n");
|
||||||
line = set_redirection(s_cmd, line, index);//SET REDIRECTION
|
line = set_redirection(s_cmd, line, index);//SET REDIRECTION
|
||||||
split_line = ft_split_with_quote(line, ' ');
|
split_line = ft_split_with_quote(line, ' ');
|
||||||
|
//parse_quote(cmd);
|
||||||
//print_double_fd(split_line, 0);
|
//print_double_fd(split_line, 0);
|
||||||
if (!is_builtin(split_line[0]))
|
|
||||||
|
|
||||||
|
/*if (!is_builtin(split_line[0]))
|
||||||
s_cmd->cmd = ft_strdup(get_command(split_line, cmd->path));
|
s_cmd->cmd = ft_strdup(get_command(split_line, cmd->path));
|
||||||
else
|
else
|
||||||
s_cmd->cmd = ft_strdup(split_line[0]);
|
s_cmd->cmd = ft_strdup(split_line[0]);*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*if (!s_cmd->cmd)
|
/*if (!s_cmd->cmd)
|
||||||
{
|
{
|
||||||
free(line);
|
free(line);
|
||||||
@ -45,11 +51,8 @@ t_s_cmd *set_s_cmd(char *line, t_cmd *cmd, int index)
|
|||||||
free(s_cmd);
|
free(s_cmd);
|
||||||
return (0);
|
return (0);
|
||||||
}*/
|
}*/
|
||||||
if (s_cmd->cmd)
|
|
||||||
{
|
|
||||||
s_cmd->nb_args = double_size(split_line);
|
s_cmd->nb_args = double_size(split_line);
|
||||||
s_cmd->args = split_line;
|
s_cmd->args = split_line;
|
||||||
}
|
|
||||||
free(line);
|
free(line);
|
||||||
return (s_cmd);
|
return (s_cmd);
|
||||||
}
|
}
|
||||||
@ -61,7 +64,7 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while (cmds[i])
|
while (cmds[i])
|
||||||
{
|
{
|
||||||
cmd->s_cmds[i] = set_s_cmd(cmds[i], cmd, i);
|
cmd->s_cmds[i] = set_s_cmd(cmds[i], i);
|
||||||
if (!cmd->s_cmds[i])
|
if (!cmd->s_cmds[i])
|
||||||
{
|
{
|
||||||
printf("invalid command\n");
|
printf("invalid command\n");
|
||||||
@ -81,6 +84,8 @@ t_cmd *set_cmd(char *input, char **env)
|
|||||||
t_cmd *cmd;
|
t_cmd *cmd;
|
||||||
char **cmds;
|
char **cmds;
|
||||||
|
|
||||||
|
if (!is_quote_good(input))
|
||||||
|
return (0);
|
||||||
cmds = ft_split_with_quote(input, '|');
|
cmds = ft_split_with_quote(input, '|');
|
||||||
//print_double_fd(cmds, 1);
|
//print_double_fd(cmds, 1);
|
||||||
if (!cmds)
|
if (!cmds)
|
||||||
@ -96,12 +101,11 @@ t_cmd *set_cmd(char *input, char **env)
|
|||||||
cmd->path = get_path(env);
|
cmd->path = get_path(env);
|
||||||
cmd->env = env;
|
cmd->env = env;
|
||||||
//set_quote_and_var;
|
//set_quote_and_var;
|
||||||
|
|
||||||
//cmd->err_var = 0;
|
//cmd->err_var = 0;
|
||||||
//cmd->outfile = 0;
|
|
||||||
//cmd->infile = 0;
|
|
||||||
cmd->nb_s_cmd = double_size(cmds);
|
cmd->nb_s_cmd = double_size(cmds);
|
||||||
cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd
|
cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd
|
||||||
|
parse_quote(cmd);
|
||||||
|
//printf("after parse quote -%s-\n", cmd->s_cmds[0]->cmd);
|
||||||
free(cmds);
|
free(cmds);
|
||||||
if (cmd)
|
if (cmd)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -6,74 +6,219 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
|
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
|
||||||
/* Updated: 2022/04/11 16:30:09 by apommier ### ########.fr */
|
/* Updated: 2022/04/13 01:27:30 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../../includes/minishell.h"
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
int is_quote_even(char *str)
|
int is_in_quote(char *str, int index)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int open;
|
||||||
|
|
||||||
|
open = 0;
|
||||||
|
i = 0;
|
||||||
|
while (i < index)
|
||||||
|
{
|
||||||
|
if (str[i] == '\'' && open != 2)
|
||||||
|
{
|
||||||
|
if (!open)
|
||||||
|
open = 1;
|
||||||
|
else if (open == 1)
|
||||||
|
open = 0;
|
||||||
|
}
|
||||||
|
else if (str[i] == '"' && open != 1)
|
||||||
|
{
|
||||||
|
if (!open)
|
||||||
|
open = 2;
|
||||||
|
else if (open == 2)
|
||||||
|
open = 0;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (open);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_quote_good(char *str)
|
||||||
{
|
{
|
||||||
int simple_quote;
|
int simple_quote;
|
||||||
int double_quote;
|
int double_quote;
|
||||||
|
int open;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = -1;
|
i = 0;
|
||||||
|
open = 0;
|
||||||
simple_quote = 0;
|
simple_quote = 0;
|
||||||
double_quote = 0;
|
double_quote = 0;
|
||||||
while (str[i++])
|
while (str[i])
|
||||||
{
|
{
|
||||||
if (str[i] == 39)
|
if (str[i] == '\'' && open != 2)
|
||||||
|
{
|
||||||
|
if (!open)
|
||||||
|
open = 1;
|
||||||
|
else if (open == 1)
|
||||||
|
open = 0;
|
||||||
simple_quote++;
|
simple_quote++;
|
||||||
else if (str[i] == 34)
|
}
|
||||||
|
else if (str[i] == '"' && open != 1)
|
||||||
|
{
|
||||||
|
if (!open)
|
||||||
|
open = 2;
|
||||||
|
else if (open == 2)
|
||||||
|
open = 0;
|
||||||
double_quote++;
|
double_quote++;
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
if (simple_quote % 2 || double_quote % 2)
|
if (simple_quote % 2 || double_quote % 2)
|
||||||
|
{
|
||||||
|
printf("bad quote\n");
|
||||||
return (0);
|
return (0);
|
||||||
|
}
|
||||||
|
//printf("good_quote\n");
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_var(char *cmd)
|
char *del_char(char *str, int *index)
|
||||||
|
{
|
||||||
|
char *swap;
|
||||||
|
char *swap2;
|
||||||
|
|
||||||
|
swap = 0;
|
||||||
|
//printf("index= %d\n", *index);
|
||||||
|
//printf("before del char -%s-\n", str);
|
||||||
|
// if (ft_strlen(str) > 1)
|
||||||
|
swap = ft_strdup(str + *index + 1);
|
||||||
|
// else
|
||||||
|
// swap =
|
||||||
|
//printf("in del char after dup -%s-\n", swap);
|
||||||
|
// if (str)
|
||||||
|
str[*index] = 0;
|
||||||
|
swap2 = str;
|
||||||
|
str = ft_strjoin(str, swap);
|
||||||
|
//if (*index)
|
||||||
|
// (*index)--;
|
||||||
|
//printf("after del char -%s-\n", str);
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_var(char **env, char *var_name)
|
||||||
|
{
|
||||||
|
char *line;
|
||||||
|
char **split_line;
|
||||||
|
int index;
|
||||||
|
|
||||||
|
line = 0;
|
||||||
|
index = find_it(env, var_name);
|
||||||
|
if (index >= 0)
|
||||||
|
{
|
||||||
|
line = env[index];
|
||||||
|
split_line = ft_split(line, '=');
|
||||||
|
if (split_line[1])
|
||||||
|
line = ft_strdup(split_line[1]);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
free_double(split_line);
|
||||||
|
}
|
||||||
|
|
||||||
|
//printf("get_var line= -%s-\n", line);
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *change_var(t_cmd *big_cmd, char *cmd, int *index)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int simple_quote;
|
char *swap;
|
||||||
int double_quote;
|
char *swap2;
|
||||||
int first_open;
|
char *ret;
|
||||||
|
char *var;
|
||||||
|
|
||||||
|
i = *index + 1;
|
||||||
|
while (cmd[i] && (ft_isalnum(cmd[i]) || cmd[i] == '_'))
|
||||||
|
i++;
|
||||||
|
swap = ft_substr(cmd, *index + 1, i - *index);
|
||||||
|
//printf("swap= -%s-\n", swap);
|
||||||
|
var = get_var(big_cmd->env, swap);
|
||||||
|
swap2 = ft_strdup(cmd + i);
|
||||||
|
cmd[*index] = 0;
|
||||||
|
ret = ft_strjoin(var, 0);
|
||||||
|
free(var);
|
||||||
|
var = ret;
|
||||||
|
ret = ft_strjoin(ret, swap2);
|
||||||
|
//printf("change_var -%s-\n", ret);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *set_var(t_cmd *big_cmd, char *cmd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
//printf("set_var\n");
|
||||||
i = 0;
|
i = 0;
|
||||||
first_open = 0;
|
|
||||||
simple_quote = 0;
|
|
||||||
double_quote = 0;
|
|
||||||
while (cmd[i])
|
while (cmd[i])
|
||||||
{
|
{
|
||||||
|
//printf("char= -%c-\n", cmd[i]);
|
||||||
if (cmd[i] == '\'')
|
if (cmd[i] == '\'')
|
||||||
{
|
{
|
||||||
if (simple_quote)
|
cmd = del_char(cmd, &i);
|
||||||
simple_quote = 0;
|
//printf("i= %d char= -%c- str= -%s-\n", i, cmd[i], cmd);
|
||||||
else
|
if (cmd[i])
|
||||||
simple_quote = 1;
|
{
|
||||||
|
while (cmd[i] != '\'')
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
cmd = del_char(cmd, &i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (cmd[i] == '"')
|
else if (cmd[i] == '"')
|
||||||
{
|
{
|
||||||
if (double_quote)
|
cmd = del_char(cmd, &i);
|
||||||
double_quote = 0;
|
//printf("i= %d char= -%c-\n", i, cmd[i]);
|
||||||
else
|
if (cmd[i])
|
||||||
double_quote = 1;
|
{
|
||||||
}
|
while (cmd[i] != '"')
|
||||||
else if (cmd[i] == '$' && !simple_quote)
|
{
|
||||||
i = change_var(cmd, i);
|
if (cmd[i++] == '$')
|
||||||
i++;
|
cmd = change_var(big_cmd, cmd, &i);
|
||||||
}
|
}
|
||||||
|
cmd = del_char(cmd, &i);
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_quote(char **cmds)
|
//i++;
|
||||||
|
}
|
||||||
|
else if (cmd[i] == '$')
|
||||||
|
cmd = change_var(big_cmd, cmd, &i);
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
//printf("after all -%s-\n", cmd);
|
||||||
|
return (cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parse_quote(t_cmd *cmd)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (cmds[i])
|
while (cmd->s_cmds[i])
|
||||||
{
|
{
|
||||||
set_var(cmds[i]);
|
j = -1;
|
||||||
|
while (cmd->s_cmds[i]->args[++j])
|
||||||
|
{
|
||||||
|
cmd->s_cmds[i]->args[j] = set_var(cmd, cmd->s_cmds[i]->args[j]);
|
||||||
|
}
|
||||||
|
//printf("parse quote -%s-\n", cmd->s_cmds[i]->args[0]);
|
||||||
|
if (!is_builtin(cmd->s_cmds[i]->args[0]))
|
||||||
|
cmd->s_cmds[i]->cmd = ft_strdup(get_command(cmd->s_cmds[i]->args, cmd->path));
|
||||||
|
else
|
||||||
|
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]);
|
||||||
|
//free(cmd->s_cmds[i]->cmd);
|
||||||
|
//cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]);
|
||||||
|
//printf("parse quote -%s-\n", cmd->s_cmds[i]->cmd);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
|
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
|
||||||
/* Updated: 2022/04/10 18:13:00 by apommier ### ########.fr */
|
/* Updated: 2022/04/12 04:29:12 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -238,19 +238,25 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
|
|||||||
while (line[i])
|
while (line[i])
|
||||||
{
|
{
|
||||||
if(line[i] == '<')
|
if(line[i] == '<')
|
||||||
|
{
|
||||||
|
if (!is_in_quote(line, i))
|
||||||
{
|
{
|
||||||
line = ft_input(line, cmd, i);
|
line = ft_input(line, cmd, i);
|
||||||
if (cmd->in_type == 1)
|
if (cmd->in_type == 1)
|
||||||
wait_prompt(cmd, index);
|
wait_prompt(cmd, index);
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if(line[i] == '>')
|
else if(line[i] == '>')
|
||||||
|
{
|
||||||
|
if (!is_in_quote(line, i))
|
||||||
{
|
{
|
||||||
line = ft_output(line, cmd, i);
|
line = ft_output(line, cmd, i);
|
||||||
//if (cmd->in_type == 0)
|
//if (cmd->in_type == 0)
|
||||||
set_file(cmd->outfile);
|
set_file(cmd->outfile);
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user