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_redir.c\
exec/exec_semi.c\
exec/fd_is_valid.c\
exec/ft_exec.c\
exec/ft_findexec.c\
exec/launch_process.c\
exec/process_do_redirection.c\
exec/process_redirect.c\
exec/process_reset.c\
exec/process_setexec.c\

View file

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

View file

@ -67,9 +67,16 @@ struct s_execmap
int (*f)(t_btree **ast);
};
struct s_redirmap
{
t_flag type;
int (*f)(t_redir *redir);
};
#include "minishell.h"
extern t_execmap g_execmap[];
extern t_redirmap g_redirmap[];
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_reset(void);
int process_redirect(t_process *p);
int process_do_redirection(t_redir *redir);
int fd_is_valid(int fd);
void bad_fd(int fd);
int redirect_great(t_redir *redir, int *fdold, int *fdnew);
int redirect_less(t_redir *redir, int *fdold, int *fdnew);
int redirect_dgreat(t_redir *redir, int *fdold, int *fdnew);
int redirect_dless(t_redir *redir, int *fdold, int *fdnew);
int redirect_greatand(t_redir *redir, int *fdold, int *fdnew);
int redirect_lessand(t_redir *redir, int *fdold, int *fdnew);
int process_redirect(t_process *p);
int redirect_great(t_redir *redir);
int redirect_less(t_redir *redir);
int redirect_dgreat(t_redir *redir);
int redirect_dless(t_redir *redir);
int redirect_greatand(t_redir *redir);
int redirect_lessand(t_redir *redir);
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_execmap t_execmap;
typedef struct s_redirmap t_redirmap;
typedef struct s_process t_process;
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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@ -16,50 +16,3 @@ int fd_is_valid(int fd)
{
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"
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)
{
t_list *redirs;
t_redir *redir;
int i;
redirs = p->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;
}
if (p->toclose != STDIN)

View file

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

View file

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

View file

@ -10,11 +10,13 @@
/* */
/* ************************************************************************** */
#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)
bad_fd(redir->word.fd);
if (redir->close)
@ -24,8 +26,8 @@ int redirect_greatand(t_redir *redir, int *fdold, int *fdnew)
}
else
{
*fdnew = redir->n;
*fdold = redir->word.fd;
fdold = redir->word.fd;
fdnew = redir->n;
}
if (fd_is_valid(fdold))
{

View file

@ -12,15 +12,18 @@
#include "exec.h"
int redirect_less(t_redir *redir, int *fdold, int *fdnew)
int redirect_less(t_redir *redir)
{
*fdnew = redir->n;
if ((*fdold = open(redir->word.word, O_RDONLY)) < 0)
int fdold;
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",
SHELL_NAME, redir->word.word);
exit (1);
}
return (0);
dup2(fdold, fdnew);
return (0);
}

View file

@ -12,8 +12,11 @@
#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)
bad_fd(redir->word.fd);
if (redir->close)
@ -23,10 +26,10 @@ int redirect_lessand(t_redir *redir, int *fdold, int *fdnew)
}
else
{
*fdnew = redir->word.fd;
*fdold = redir->n;
fdold = redir->word.fd;
fdnew = redir->n;
}
if (fd_is_valid(fdnew))
if (fd_is_valid(fdold))
{
dup2(fdold, fdnew);
close(fdold);