forked from Sirelaw/minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
144 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.