looks promising

This commit is contained in:
Jack Halford 2017-02-07 02:07:47 +01:00
parent bb52d35f4d
commit d7f5116eb0
13 changed files with 85 additions and 113 deletions

16
42sh/11
View file

@ -1,16 +0,0 @@
11
42sh
Makefile
Session.vim
a.out
debug
donovan_segaults_06-02
file
includes
libft
objs
out
pdf
script.sh
src
update_makefile.sh

17
42sh/9-
View file

@ -1,17 +0,0 @@
11
42sh
9-
Makefile
Session.vim
a.out
debug
donovan_segaults_06-02
file
includes
libft
objs
out
pdf
script.sh
src
update_makefile.sh

View file

@ -61,10 +61,10 @@ exec/exec_or_if.c\
exec/exec_pipe.c\ exec/exec_pipe.c\
exec/exec_redir.c\ exec/exec_redir.c\
exec/exec_semi.c\ exec/exec_semi.c\
exec/fd_is_valid.c\
exec/ft_exec.c\ exec/ft_exec.c\
exec/ft_findexec.c\ exec/ft_findexec.c\
exec/launch_process.c\ exec/launch_process.c\
exec/process_do_redirection.c\
exec/process_redirect.c\ exec/process_redirect.c\
exec/process_reset.c\ exec/process_reset.c\
exec/process_setexec.c\ exec/process_setexec.c\

View file

@ -1,12 +1,13 @@
11 4
42sh 42sh
9-
Makefile Makefile
Session.vim Session.vim
a.out
debug debug
donovan_segaults_06-02 donovan_segaults_06-02
file file
file1
file2
file3
includes includes
libft libft
objs objs

View file

@ -67,9 +67,16 @@ struct s_execmap
int (*f)(t_btree **ast); int (*f)(t_btree **ast);
}; };
struct s_redirmap
{
t_flag type;
int (*f)(t_redir *redir);
};
#include "minishell.h" #include "minishell.h"
extern t_execmap g_execmap[]; extern t_execmap g_execmap[];
extern t_redirmap g_redirmap[];
int ft_exec(t_btree **ast); int ft_exec(t_btree **ast);
@ -88,15 +95,15 @@ void process_setsig(void);
void process_free(void *content, size_t content_size); void process_free(void *content, size_t content_size);
void process_reset(void); void process_reset(void);
int process_redirect(t_process *p); int fd_is_valid(int fd);
int process_do_redirection(t_redir *redir);
void bad_fd(int fd); void bad_fd(int fd);
int redirect_great(t_redir *redir, int *fdold, int *fdnew); int process_redirect(t_process *p);
int redirect_less(t_redir *redir, int *fdold, int *fdnew); int redirect_great(t_redir *redir);
int redirect_dgreat(t_redir *redir, int *fdold, int *fdnew); int redirect_less(t_redir *redir);
int redirect_dless(t_redir *redir, int *fdold, int *fdnew); int redirect_dgreat(t_redir *redir);
int redirect_greatand(t_redir *redir, int *fdold, int *fdnew); int redirect_dless(t_redir *redir);
int redirect_lessand(t_redir *redir, int *fdold, int *fdnew); int redirect_greatand(t_redir *redir);
int redirect_lessand(t_redir *redir);
char *ft_findexec(char *path, char *file); char *ft_findexec(char *path, char *file);

View file

@ -32,6 +32,7 @@ typedef union u_word t_word;
typedef struct s_job t_job; typedef struct s_job t_job;
typedef struct s_execmap t_execmap; typedef struct s_execmap t_execmap;
typedef struct s_redirmap t_redirmap;
typedef struct s_process t_process; typedef struct s_process t_process;
typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]); typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]);

View file

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* process_do_redirection.c :+: :+: :+: */ /* fd_is_valid.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
@ -16,50 +16,3 @@ int fd_is_valid(int fd)
{ {
return (fcntl(fd, F_GETFD) != -1 || errno != EBADF); return (fcntl(fd, F_GETFD) != -1 || errno != EBADF);
} }
int process_do_redirection(t_redir *redir)
{
int fdold;
int fdnew;
if (redir->n > 9)
bad_fd(redir->n);
if (redir->type & TK_GREAT)
redirect_great(redir, &fdold, &fdnew);
else if (redir->type & TK_GREAT)
redirect_dgreat(redir, &fdold, &fdnew);
else if (redir->type & TK_LESS)
redirect_less(redir, &fdold, &fdnew);
else if (redir->type & TK_GREATAND)
{
if (redirect_greatand(redir, &fdold, &fdnew))
return (0);
}
else if (redir->type & TK_LESSAND)
{
if (redirect_lessand(redir, &fdold, &fdnew))
return (0);
}
else
exit(42);
DG("gonna redirect dup2(%i,%i)", fdold, fdnew);
if (fd_is_valid(fdnew))
{
if (fd_is_valid(fdold))
{
dup2(fdold, fdnew);
close(fdold);
}
else
bad_fd(fdold);
}
else
{
DG("[%i] is not a valid fd", fdnew);
if (fdnew <= 2)
close(fdnew);
else
bad_fd(fdnew);
}
return (0);
}

View file

@ -12,14 +12,42 @@
#include "exec.h" #include "exec.h"
t_redirmap g_redirmap[] =
{
{TK_LESS, redirect_less},
{TK_GREAT, redirect_great},
/* {TK_DLESS, redirect_dless}, */
{TK_DGREAT, redirect_dgreat},
{TK_LESSAND, redirect_lessand},
{TK_GREATAND, redirect_greatand},
{0, NULL},
};
int process_redirect(t_process *p) int process_redirect(t_process *p)
{ {
t_list *redirs; t_list *redirs;
t_redir *redir;
int i;
redirs = p->redirs; redirs = p->redirs;
while (redirs) while (redirs)
{ {
process_do_redirection(redirs->content); i = 0;
redir = redirs->content;
if (redir->n > 9)
{
bad_fd(redir->n);
exit(1);
}
while (g_redirmap[i].type)
{
if (g_redirmap[i].type == redir->type)
{
(g_redirmap[i].f)(redir);
break ;
}
i++;
}
redirs = redirs->next; redirs = redirs->next;
} }
if (p->toclose != STDIN) if (p->toclose != STDIN)

View file

@ -12,14 +12,18 @@
#include "exec.h" #include "exec.h"
int redirect_dgreat(t_redir *redir, int *fdold, int *fdnew) int redirect_dgreat(t_redir *redir)
{ {
*fdold = redir->n; int fdold;
if ((*fdnew = open(redir->word.word, int fdnew;
fdnew = redir->n;
if ((fdold = open(redir->word.word,
O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0)
{ {
DG("open errno=%i", errno); DG("open errno=%i", errno);
exit(1); exit(1);
} }
dup2(fdold, fdnew);
return (0); return (0);
} }

View file

@ -12,15 +12,18 @@
#include "exec.h" #include "exec.h"
int redirect_great(t_redir* redir, int *fdold, int *fdnew) int redirect_great(t_redir* redir)
{ {
*fdnew = redir->n; int fdold;
if ((*fdold = open(redir->word.word, int fdnew;
fdnew = redir->n;
if ((fdold = open(redir->word.word,
O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
{ {
DG("open errno=%i", errno); DG("open errno=%i", errno);
exit(1); exit(1);
} }
return (0);
dup2(fdold, fdnew); dup2(fdold, fdnew);
return (0);
} }

View file

@ -10,11 +10,13 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "exec.h" #include "exec.h"
int redirect_greatand(t_redir *redir, int *fdold, int *fdnew) int redirect_greatand(t_redir *redir)
{ {
int fdold;
int fdnew;
if (redir->word.fd > 9) if (redir->word.fd > 9)
bad_fd(redir->word.fd); bad_fd(redir->word.fd);
if (redir->close) if (redir->close)
@ -24,8 +26,8 @@ int redirect_greatand(t_redir *redir, int *fdold, int *fdnew)
} }
else else
{ {
*fdnew = redir->n; fdold = redir->word.fd;
*fdold = redir->word.fd; fdnew = redir->n;
} }
if (fd_is_valid(fdold)) if (fd_is_valid(fdold))
{ {

View file

@ -12,15 +12,18 @@
#include "exec.h" #include "exec.h"
int redirect_less(t_redir *redir, int *fdold, int *fdnew) int redirect_less(t_redir *redir)
{ {
*fdnew = redir->n; int fdold;
if ((*fdold = open(redir->word.word, O_RDONLY)) < 0) int fdnew;
fdnew = redir->n;
if ((fdold = open(redir->word.word, O_RDONLY)) < 0)
{ {
ft_dprintf(2, "{red}%s: no such file or directory: %s{eoc}\n", ft_dprintf(2, "{red}%s: no such file or directory: %s{eoc}\n",
SHELL_NAME, redir->word.word); SHELL_NAME, redir->word.word);
exit (1); exit (1);
} }
return (0);
dup2(fdold, fdnew); dup2(fdold, fdnew);
return (0);
} }

View file

@ -12,8 +12,11 @@
#include "exec.h" #include "exec.h"
int redirect_lessand(t_redir *redir, int *fdold, int *fdnew) int redirect_lessand(t_redir *redir)
{ {
int fdold;
int fdnew;
if (redir->word.fd > 9) if (redir->word.fd > 9)
bad_fd(redir->word.fd); bad_fd(redir->word.fd);
if (redir->close) if (redir->close)
@ -23,10 +26,10 @@ int redirect_lessand(t_redir *redir, int *fdold, int *fdnew)
} }
else else
{ {
*fdnew = redir->word.fd; fdold = redir->word.fd;
*fdold = redir->n; fdnew = redir->n;
} }
if (fd_is_valid(fdnew)) if (fd_is_valid(fdold))
{ {
dup2(fdold, fdnew); dup2(fdold, fdnew);
close(fdold); close(fdold);