diff --git a/Makefile b/Makefile index 2fbf9f1..3e4bdf2 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # 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/env.c\ srcs/set_quote/split_with_quote.c\ + srcs/set_quote/set_quote.c\ srcs/built_in/choose_builtin.c -# srcs/set_quote/set_quote.c\ + OBJS = ${SRCS:.c=.o} diff --git a/includes/minishell.h b/includes/minishell.h index 018d18d..2564fa4 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* 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); //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); //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); //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); void print_double_fd(char **tab, int fd); void free_double(char **tab); diff --git a/srcs/main.c b/srcs/main.c index 285e7e6..12f67cf 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* 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) { - cmd = set_cmd(input, path); if (cmd) { diff --git a/srcs/set_cmd/set_cmd.c b/srcs/set_cmd/set_cmd.c index aa949e9..b478952 100644 --- a/srcs/set_cmd/set_cmd.c +++ b/srcs/set_cmd/set_cmd.c @@ -6,7 +6,7 @@ /* 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; char **split_line; @@ -33,11 +33,17 @@ t_s_cmd *set_s_cmd(char *line, t_cmd *cmd, int index) //printf("before redirect\n"); line = set_redirection(s_cmd, line, index);//SET REDIRECTION split_line = ft_split_with_quote(line, ' '); + //parse_quote(cmd); //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)); else - s_cmd->cmd = ft_strdup(split_line[0]); + s_cmd->cmd = ft_strdup(split_line[0]);*/ + + + /*if (!s_cmd->cmd) { free(line); @@ -45,11 +51,8 @@ t_s_cmd *set_s_cmd(char *line, t_cmd *cmd, int index) free(s_cmd); return (0); }*/ - if (s_cmd->cmd) - { s_cmd->nb_args = double_size(split_line); s_cmd->args = split_line; - } free(line); return (s_cmd); } @@ -61,7 +64,7 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds) i = 0; 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]) { printf("invalid command\n"); @@ -80,7 +83,9 @@ t_cmd *set_cmd(char *input, char **env) { t_cmd *cmd; char **cmds; - + + if (!is_quote_good(input)) + return (0); cmds = ft_split_with_quote(input, '|'); //print_double_fd(cmds, 1); if (!cmds) @@ -96,12 +101,11 @@ t_cmd *set_cmd(char *input, char **env) cmd->path = get_path(env); cmd->env = env; //set_quote_and_var; - //cmd->err_var = 0; - //cmd->outfile = 0; - //cmd->infile = 0; cmd->nb_s_cmd = double_size(cmds); 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); if (cmd) { diff --git a/srcs/set_quote/set_quote.c b/srcs/set_quote/set_quote.c index 6e784e8..97d3444 100644 --- a/srcs/set_quote/set_quote.c +++ b/srcs/set_quote/set_quote.c @@ -6,74 +6,219 @@ /* 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" -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 double_quote; + int open; int i; - i = -1; + i = 0; + open = 0; simple_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++; - else if (str[i] == 34) + } + else if (str[i] == '"' && open != 1) + { + if (!open) + open = 2; + else if (open == 2) + open = 0; double_quote++; + } + i++; } if (simple_quote % 2 || double_quote % 2) + { + printf("bad quote\n"); return (0); + } + //printf("good_quote\n"); 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; + char *swap; + char *swap2; + 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; - int simple_quote; - int double_quote; - int first_open; + //printf("set_var\n"); i = 0; - first_open = 0; - simple_quote = 0; - double_quote = 0; while (cmd[i]) { + //printf("char= -%c-\n", cmd[i]); if (cmd[i] == '\'') { - if (simple_quote) - simple_quote = 0; - else - simple_quote = 1; + cmd = del_char(cmd, &i); + //printf("i= %d char= -%c- str= -%s-\n", i, cmd[i], cmd); + if (cmd[i]) + { + while (cmd[i] != '\'') + { + i++; + } + cmd = del_char(cmd, &i); + } } else if (cmd[i] == '"') { - if (double_quote) - double_quote = 0; - else - double_quote = 1; + cmd = del_char(cmd, &i); + //printf("i= %d char= -%c-\n", i, cmd[i]); + if (cmd[i]) + { + while (cmd[i] != '"') + { + if (cmd[i++] == '$') + cmd = change_var(big_cmd, cmd, &i); + } + cmd = del_char(cmd, &i); + } + + //i++; } - else if (cmd[i] == '$' && !simple_quote) - i = change_var(cmd, i); - 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(char **cmds) +int parse_quote(t_cmd *cmd) { int i; + int j; 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++; } + return (0); } \ No newline at end of file diff --git a/srcs/set_redirection/redirection.c b/srcs/set_redirection/redirection.c index d5503e3..69a73ca 100644 --- a/srcs/set_redirection/redirection.c +++ b/srcs/set_redirection/redirection.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ @@ -239,17 +239,23 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index) { if(line[i] == '<') { - line = ft_input(line, cmd, i); - if (cmd->in_type == 1) - wait_prompt(cmd, index); - i = 0; + if (!is_in_quote(line, i)) + { + line = ft_input(line, cmd, i); + if (cmd->in_type == 1) + wait_prompt(cmd, index); + i = 0; + } } else if(line[i] == '>') { - line = ft_output(line, cmd, i); - //if (cmd->in_type == 0) + if (!is_in_quote(line, i)) + { + line = ft_output(line, cmd, i); + //if (cmd->in_type == 0) set_file(cmd->outfile); - i = 0; + i = 0; + } } else i++;