fix ctrl_c in << heredoc

This commit is contained in:
kinou-p 2022-04-14 11:31:06 +02:00
parent 63ec3979b3
commit 3b8d055260
6 changed files with 84 additions and 32 deletions

View File

@ -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/12 23:42:20 by apommier ### ########.fr */ /* Updated: 2022/04/14 10:30:52 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -79,10 +79,14 @@ void free_cmd(t_cmd *cmd);
void exit_shell(t_cmd *cmd); void exit_shell(t_cmd *cmd);
//set_quote.c //set_quote.c
char *set_var(t_cmd *big_cmd, char *cmd);
int is_in_quote(char *str, int index); int is_in_quote(char *str, int index);
int is_quote_good(char *str); 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);
//signals
void crtl_c(int num);
//redirection.c set redirection and input good //redirection.c set redirection and input good
char *set_redirection(t_s_cmd *cmd, char *line, int index); 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);

View File

@ -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/13 00:09:42 by apommier ### ########.fr */ /* Updated: 2022/04/14 10:35:08 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,18 +14,12 @@
void crtl_c(int num) void crtl_c(int num)
{ {
int tmpin;
tmpin = dup(0);
num = 0; num = 0;
close(0);
dup2(tmpin, 0);
close(tmpin);
printf("\n"); printf("\n");
rl_replace_line("", 0); rl_replace_line("", 0);
rl_on_new_line(); rl_on_new_line();
rl_redisplay(); rl_redisplay();
//ft_putstr_fd("test\n", 0);
} }
void sig_quit(int num) void sig_quit(int num)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/02 18:51:31 by apommier #+# #+# */ /* Created: 2022/04/02 18:51:31 by apommier #+# #+# */
/* Updated: 2022/04/11 17:49:11 by apommier ### ########.fr */ /* Updated: 2022/04/14 11:27:29 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -57,8 +57,8 @@ int wait_exit(t_cmd *cmd)
else if (WIFSIGNALED(status)) else if (WIFSIGNALED(status))
{ {
cmd->err_var = WTERMSIG(status); cmd->err_var = WTERMSIG(status);
if (cmd->err_var == 2) //if (cmd->err_var == 2)
ft_putstr_fd("\b\b\b\b\b", 1); ft_putstr_fd("\b\b\b\b\b", 1);
} }
i++; i++;
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */ /* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
/* Updated: 2022/04/10 21:33:46 by apommier ### ########.fr */ /* Updated: 2022/04/13 21:25:05 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -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/13 01:28:18 by apommier ### ########.fr */ /* Updated: 2022/04/14 10:58:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,6 +32,8 @@ t_s_cmd *set_s_cmd(char *line, int index)
s_cmd->outfile = 0; s_cmd->outfile = 0;
//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
if (!line)
return (0);
split_line = ft_split_with_quote(line, ' '); split_line = ft_split_with_quote(line, ' ');
//parse_quote(cmd); //parse_quote(cmd);
//print_double_fd(split_line, 0); //print_double_fd(split_line, 0);
@ -67,7 +69,6 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds)
cmd->s_cmds[i] = set_s_cmd(cmds[i], 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");
free_cmd(cmd); free_cmd(cmd);
return (0); return (0);
} }
@ -104,6 +105,8 @@ t_cmd *set_cmd(char *input, char **env)
//cmd->err_var = 0; //cmd->err_var = 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
if (!cmd)
return (0);
parse_quote(cmd); parse_quote(cmd);
//printf("after parse quote -%s-\n", cmd->s_cmds[0]->cmd); //printf("after parse quote -%s-\n", cmd->s_cmds[0]->cmd);
free(cmds); free(cmds);

View File

@ -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/12 04:29:12 by apommier ### ########.fr */ /* Updated: 2022/04/14 11:28:30 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
char next_space(char *str, int i) char next_space(char *str, int i)
{ {
i++; //i++;
while (str[i] == ' ') while (str[i] == ' ')
i++; i++;
return (str[i]); return (str[i]);
@ -49,7 +49,7 @@ char *get_word(char *str, int start)
while (new[i] && new[i] != ' ' && new[i] != '>' && new[i] != '<') while (new[i] && new[i] != ' ' && new[i] != '>' && new[i] != '<')
i++; i++;
new[i] = 0; new[i] = 0;
printf("get word= %s\n", new); //printf("get word= %s\n", new);
return (new); return (new);
} }
@ -75,6 +75,7 @@ char *set_input(char *line, t_s_cmd *cmd, int index)
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i]) while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
i++; i++;
cmd->infile = get_word(line, word_index); cmd->infile = get_word(line, word_index);
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
line = cut_str(line, index, i); line = cut_str(line, index, i);
return (line); return (line);
} }
@ -95,6 +96,7 @@ char *set_output(char *line, t_s_cmd *cmd, int index)
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i]) while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
i++; i++;
cmd->outfile = get_word(line, index); cmd->outfile = get_word(line, index);
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
line = cut_str(line, index, i); line = cut_str(line, index, i);
return (line); return (line);
} }
@ -106,11 +108,11 @@ char *ft_input(char *line, t_s_cmd *cmd, int index)
char next; char next;
i = index; i = index;
next = next_space(line, i); next = next_space(line, i + 1);
if (line[i + 1] == '<') if (line[i + 1] == '<')
{ {
cmd->in_type = 1; cmd->in_type = 1;
next = next_space(line, i + 1); next = next_space(line, i + 2);
} }
else else
cmd->in_type = 0; cmd->in_type = 0;
@ -126,11 +128,11 @@ char *ft_output(char *line, t_s_cmd *cmd, int index)
char next; char next;
i = index; i = index;
next = next_space(line, i); next = next_space(line, i + 1);
if (line[i + 1] == '>') if (line[i + 1] == '>')
{ {
cmd->in_type = 1; cmd->in_type = 1;
next = next_space(line, i + 1); next = next_space(line, i + 2);
} }
else else
cmd->in_type = 0; cmd->in_type = 0;
@ -178,6 +180,11 @@ char **add_line(char **tab, char *line)
return(ret); return(ret);
} }
/*void del_heredoc()
{
}*/
char *set_heredoc(int index, char **in) char *set_heredoc(int index, char **in)
{ {
char *nbr_file; char *nbr_file;
@ -203,30 +210,71 @@ char *set_heredoc(int index, char **in)
return (file_name); return (file_name);
} }
void wait_prompt(t_s_cmd *cmd, int index) void sig_heredoc(int num)
{
num = 0;
struct sigaction base;
base.sa_handler = &crtl_c;
base.sa_flags = 0;
//printf("sig_heredoc\n");
if (sigaction(SIGINT, &base, 0) == -1)
{
printf("sigaction error2\n");
return ;
}
}
int wait_prompt(t_s_cmd *cmd, int index)
{ {
char *input; char *input;
int i;
char **history; char **history;
char *in;
char *dup;
struct sigaction test;
test.sa_handler = &sig_heredoc;
test.sa_flags = 0;
in = ft_strjoin(cmd->infile, "\n");
free(cmd->infile);
cmd->infile = 0;
if (sigaction(SIGINT, &test, 0) == -1)
{
printf("sigaction error\n");
exit(1);
}
//printf("wait_prompt\n");
history = 0; history = 0;
input = 0; input = 0;
while (ft_strcmp(input, cmd->infile)) i = 0;
while (i == 0 || (input && ft_strlen(input) && ft_strcmp(input, in)))
{ {
i = 1;
ft_putstr_fd("> ", 0); ft_putstr_fd("> ", 0);
input = get_next_line(0); input = get_next_line(0);
//input = readline(""); //input = readline("");
printf("input= %s\n", input); //printf("input= -%s-", input);
if (!input) if (!input)
return ; return (0);
input[ft_strlen(input) - 1] = 0; //input[ft_strlen(input) - 1] = 0;
if (ft_strcmp(input, cmd->infile)) if (ft_strcmp(input, in))
history = add_line(history, input); {
dup = ft_strdup(input);
dup[ft_strlen(input) - 1] = 0;
dup = set_var(cmd->big_cmd, dup);
history = add_line(history, dup);
}
} }
free(input); free(input);
free(cmd->infile); //free(cmd->infile);
cmd->infile = 0;//option? //cmd->infile = 0;//option?
cmd->infile = set_heredoc(index, history); cmd->infile = set_heredoc(index, history);
cmd->in_type = 0; cmd->in_type = 0;
return (1);
} }
char *set_redirection(t_s_cmd *cmd, char *line, int index) char *set_redirection(t_s_cmd *cmd, char *line, int index)
@ -243,7 +291,10 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
{ {
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); {
if (!wait_prompt(cmd, index))
return (0);
}
i = 0; i = 0;
} }
} }