Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Achmuller de acosta authored and Sebastian Achmuller de acosta committed Feb 23, 2022
2 parents e9422d5 + 609ce92 commit 9ed4210
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ SRC_FILES = main.c lexer/lex_new_token.c \
built_in/pwd.c built_in/exit.c built_in/unset.c pipe/pipex.c pipe/check_cmd.c pipe/exec_built_in.c\
pipe/t_cmd_utils.c pipe/cmd_utils.c pipe/exec_cmd.c pipe/here_doc.c pipe/free_all.c\
signal_handling/sig_handle_interactive.c signal_handling/sig_handle_exec.c\
signal_handling/sig_handle_doc.c lexer/rm_quotes.c
signal_handling/sig_handle_doc.c lexer/rm_quotes.c pipe/open_files.c

OBJ_FILES = $(SRC_FILES:.c=.o)

Expand Down
8 changes: 5 additions & 3 deletions inc/pipex.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* pipex.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sachmull <sachmull@student.42.fr> +#+ +:+ +#+ */
/* By: oipadeol <oipadeol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/07 16:03:23 by oipadeol #+# #+# */
/* Updated: 2022/02/15 17:56:42 by sachmull ### ########.fr */
/* Updated: 2022/02/23 02:44:22 by oipadeol ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,6 +18,7 @@
# include <fcntl.h>
# include <stdio.h>
# include <dirent.h>
# include <sys/wait.h>
# include <errno.h>

typedef enum e_bool {
Expand Down Expand Up @@ -52,13 +53,14 @@ typedef struct s_input
int do_init(t_input *input);
int exec_cmds(t_input *input, t_cmd *cmds);
int built_in_cmd(t_input *input, t_cmd *cmd, int i);
int open_infile_outfile(t_cmd *cmd);
int check_cmd(t_input *input, t_cmd *cmd_attr, int i);
t_cmd *new_t_cmd(void);
void t_cmd_add_back(t_cmd **head, t_cmd *latest);
char **add_to_arr(char ***arr, char *s);
int here_doc(t_cmd *cmd, char *delimiter, int here_doc_id);
void free_all(t_input *input);
t_cmd *build_chain(t_lexer *l, t_input *input, t_token tok);
t_cmd *build_chain(t_lexer *l, t_input *input);
void close_fds(int fd[2]);

#endif
6 changes: 2 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sachmull <sachmull@student.42.fr> +#+ +:+ +#+ */
/* By: oipadeol <oipadeol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/04 16:17:20 by sachmull #+# #+# */
/* Updated: 2022/02/15 19:00:54 by sachmull ### ########.fr */
/* Updated: 2022/02/23 03:29:23 by oipadeol ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -44,8 +44,6 @@ int loop(t_shell_env *shell_env)
while (1)
{
line = readline("> ");
if (line == NULL)
;
if (ft_strlen(line) > 0)
add_history(line);
if (lex_valid_syntax(line))
Expand Down
4 changes: 2 additions & 2 deletions src/pipe/check_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* check_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sachmull <sachmull@student.42.fr> +#+ +:+ +#+ */
/* By: oipadeol <oipadeol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/05 11:23:44 by oipadeol #+# #+# */
/* Updated: 2022/02/13 18:11:31 by sachmull ### ########.fr */
/* Updated: 2022/02/23 01:56:39 by oipadeol ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
64 changes: 21 additions & 43 deletions src/pipe/cmd_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,12 @@
/* By: oipadeol <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/06 22:52:35 by oipadeol #+# #+# */
/* Updated: 2022/02/22 23:36:14 by oipadeol ### ########.fr */
/* Updated: 2022/02/23 03:04:00 by oipadeol ### ########.fr */
/* */
/* ************************************************************************** */

#include <pipex.h>

static char **init_arr(char ***arr, char *s)
{
*arr = malloc(sizeof(char *) * 2);
if (*arr == NULL)
return (NULL);
(*arr)[0] = s;
(*arr)[1] = NULL;
return (*arr);
}

char **add_to_arr(char ***arr, char *s)
{
int i;
char **temp;

if (*arr == NULL)
return (init_arr(arr, s));
i = 0;
while ((*arr)[i])
i++;
temp = malloc(sizeof(char *) * (i + 2));
if (!temp)
return (NULL);
i = 0;
while ((*arr)[i++])
temp[i - 1] = (*arr)[i - 1];
temp[i - 1] = s;
temp[i] = NULL;
free(*arr);
*arr = temp;
return (*arr);
}

static void set_flags(t_cmd *cmd, t_token *tok)
{
if (tok->type == RE_IN)
Expand Down Expand Up @@ -86,30 +53,41 @@ static void build_cmd(t_input *ip, t_cmd *cmd, t_token *tok)
add_to_arr(&cmd->cmds, rm_quotes(expand_str(ip->envp, &tok->literal)));
}

t_cmd *build_chain(t_lexer *l, t_input *input, t_token tok)
static void builder(t_lexer *l, t_input *input, t_cmd **first_cmd)
{
t_cmd *first_cmd;
t_cmd *latest_cmd;
t_token tok;
t_token peek_tok;

first_cmd = new_t_cmd();
if (!first_cmd)
return (NULL);
latest_cmd = first_cmd;
tok = lex_next_token(l);
latest_cmd = *first_cmd;
while (tok.type != END && latest_cmd)
{
build_cmd(input, latest_cmd, &tok);
peek_tok = lex_next_token(l);
if (peek_tok.type == END || peek_tok.type == PIPE)
{
if (peek_tok.type == END)
return (first_cmd);
return (free(peek_tok.literal));
latest_cmd = new_t_cmd();
t_cmd_add_back(&first_cmd, latest_cmd);
while (peek_tok.type == PIPE)
t_cmd_add_back(first_cmd, latest_cmd);
if (peek_tok.type == PIPE)
free(peek_tok.literal);
if (peek_tok.type == PIPE)
peek_tok = lex_next_token(l);
}
tok = peek_tok;
}
return (free(tok.literal));
}

t_cmd *build_chain(t_lexer *l, t_input *input)
{
t_cmd *first_cmd;

first_cmd = new_t_cmd();
if (!first_cmd)
return (NULL);
builder(l, input, &first_cmd);
return (first_cmd);
}
6 changes: 4 additions & 2 deletions src/pipe/exec_built_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* exec_built_in.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sachmull <sachmull@student.42.fr> +#+ +:+ +#+ */
/* By: oipadeol <oipadeol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/12 00:44:29 by oipadeol #+# #+# */
/* Updated: 2022/02/15 17:54:47 by sachmull ### ########.fr */
/* Updated: 2022/02/23 00:39:44 by oipadeol ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -35,6 +35,8 @@ static void restore_fds(t_cmd *cmd)
{
dup2(cmd->re_in, STDIN_FILENO);
dup2(cmd->re_out, STDOUT_FILENO);
close(cmd->re_in);
close(cmd->re_out);
}

static int check_built_in(t_cmd *cmd)
Expand Down
39 changes: 6 additions & 33 deletions src/pipe/exec_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
/* ::: :::::::: */
/* exec_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sachmull <sachmull@student.42.fr> +#+ +:+ +#+ */
/* By: oipadeol <oipadeol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/06 22:56:01 by oipadeol #+# #+# */
/* Updated: 2022/02/15 17:51:58 by sachmull ### ########.fr */
/* Updated: 2022/02/23 01:02:43 by oipadeol ### ########.fr */
/* */
/* ************************************************************************** */

#include <pipex.h>

void close_fds(int fd[2])
{
close(fd[0]);
close(fd[1]);
if (fd[0] != -1)
close(fd[0]);
if (fd[1] != -1)
close(fd[1]);
}

int do_init(t_input *input)
Expand All @@ -38,35 +40,6 @@ int do_init(t_input *input)
return (0);
}

int open_infile_outfile(t_cmd *cmd)
{
int i;
const int m = O_WRONLY | O_CREAT;

i = 0;
while (cmd->infile && cmd->infile[i++])
{
cmd->fd[0] = open(cmd->infile[i - 1], O_RDONLY);
if (cmd->fd[0] < 0)
perror(cmd->infile[i - 1]);
if (cmd->fd[0] < 0)
return (1);
}
i = 0;
while (cmd->outfile && cmd->outfile[i++])
{
if (*(cmd->outfile_type[i - 1]) == 'A')
cmd->fd[1] = open(cmd->outfile[i - 1], m | O_APPEND, 0666);
else
cmd->fd[1] = open(cmd->outfile[i - 1], m | O_TRUNC, 0666);
if (cmd->fd[1] < 0)
perror(cmd->outfile[i - 1]);
if (cmd->fd[1] < 0)
return (1);
}
return (0);
}

static void do_exec(t_input *input, t_cmd *cmd, int i)
{
int j;
Expand Down
4 changes: 2 additions & 2 deletions src/pipe/free_all.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: oipadeol <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/12 00:55:13 by oipadeol #+# #+# */
/* Updated: 2022/02/22 11:02:10 by oipadeol ### ########.fr */
/* Updated: 2022/02/23 02:06:30 by oipadeol ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,7 +38,7 @@ void free_all(t_input *input)
t_cmd *cmd;
t_cmd *temp;

if (!input)
if (input)
{
cmd = input->cmd_chain;
while (cmd)
Expand Down
3 changes: 2 additions & 1 deletion src/pipe/here_doc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sachmull <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/13 10:41:48 by oipadeol #+# #+# */
/* Updated: 2022/02/23 16:23:08 by sachmull ### ########.fr */
/* Updated: 2022/02/23 16:23:33 by sachmull ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -47,6 +47,7 @@ int here_doc(t_cmd *cmd, char *delimiter, int here_doc_id)
return (1);
}
read_to_fd(fd, delimiter);
close(fd);
add_to_arr(&cmd->infile, s);
return (0);
}
62 changes: 62 additions & 0 deletions src/pipe/open_files.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* open_files.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oipadeol <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/23 01:03:17 by oipadeol #+# #+# */
/* Updated: 2022/02/23 01:15:24 by oipadeol ### ########.fr */
/* */
/* ************************************************************************** */

#include <pipex.h>

static int open_all_infile(t_cmd *cmd)
{
int i;

i = 0;
while (cmd->infile && cmd->infile[i++])
{
if (cmd->fd[0] != -1)
close(cmd->fd[0]);
cmd->fd[0] = open(cmd->infile[i - 1], O_RDONLY);
if (cmd->fd[0] < 0)
perror(cmd->infile[i - 1]);
if (cmd->fd[0] < 0)
return (1);
}
return (0);
}

static int open_all_outfile(t_cmd *cmd)
{
int i;
const int m = O_WRONLY | O_CREAT;

i = 0;
while (cmd->outfile && cmd->outfile[i++])
{
if (cmd->fd[1] != -1)
close(cmd->fd[1]);
if (*(cmd->outfile_type[i - 1]) == 'A')
cmd->fd[1] = open(cmd->outfile[i - 1], m | O_APPEND, 0666);
else
cmd->fd[1] = open(cmd->outfile[i - 1], m | O_TRUNC, 0666);
if (cmd->fd[1] < 0)
perror(cmd->outfile[i - 1]);
if (cmd->fd[1] < 0)
return (1);
}
return (0);
}

int open_infile_outfile(t_cmd *cmd)
{
if (open_all_infile(cmd))
return (1);
if (open_all_outfile(cmd))
return (1);
return (0);
}
8 changes: 3 additions & 5 deletions src/pipe/pipex.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sachmull <sachmull@student.42.fr> +#+ +:+ +#+ */
/* By: oipadeol <oipadeol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/03 20:11:10 by oipadeol #+# #+# */
/* Updated: 2022/02/13 18:10:42 by sachmull ### ########.fr */
/* Updated: 2022/02/23 02:44:36 by oipadeol ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,15 +15,13 @@
int pipex(t_lexer *l)
{
t_input *input;
int i;
int exit_code;

errno = 0;
i = 0;
input = malloc(sizeof(t_input));
if (!input || do_init(input))
free_all(input);
input->cmd_chain = build_chain(l, input, lex_next_token(l));
input->cmd_chain = build_chain(l, input);
if (input->cmd_chain->cmds)
{
pipe(input->fd[0]);
Expand Down
Loading

0 comments on commit 9ed4210

Please sign in to comment.