big refactoring in progress, will finish tomorrow

This commit is contained in:
Jack Halford 2016-12-13 17:59:58 +01:00
parent a948a65cbb
commit e3b344bbb9
33 changed files with 496 additions and 242 deletions

26
42sh/includes/builtin.h Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */
/* Updated: 2016/12/13 17:58:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUILTIN_H
# define BUILTIN_H
# include "types.h"
# include "libft.h"
t_execf *is_builtin(t_process *p);
int builtin_env(const char *path, char *const argv[], char *const envp[]);
int builtin_echo(const char *path, char *const argv[], char *const envp[]);
int builtin_cd(const char *path, char *const argv[], char *const envp[]);
int builtin_exit(const char *path, char *const argv[], char *const envp[]);
int builtin_setenv(const char *path, char *const argv[], char *const envp[]);
int builtin_unsetenv(const char *path, char *const argv[], char *const envp[]);
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2016/12/13 12:56:42 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:50:40 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,17 +17,30 @@
# define PIPE_WRITE 1
# include "libft.h"
# include "types.h"
# include "job_control.h"
typedef struct s_execfunc t_execfunc;
typedef long long t_type;
struct s_process
{
char **argv;
char *path;
t_execf *execf;
pid_t pid;
int fdin;
int fdout;
int status;
t_flag attributes;
};
struct s_exec
{
char *aol_status;
int aol_search;
t_job job;
t_process process;
};
struct s_execfunc
struct s_execmap
{
t_type type;
int (*f)(t_btree **ast);
@ -35,7 +48,7 @@ struct s_execfunc
#include "minishell.h"
extern t_execfunc g_execfunc[];
extern t_execmap g_execmap[];
int ft_exec(t_btree **ast);
@ -50,15 +63,18 @@ int exec_great(t_btree **ast);
int exec_dgreat(t_btree **ast);
int exec_command(t_btree **ast);
int launch_process(t_process *p);
int process_setexec(t_process *p);
int process_setgroup(t_process *p);
int process_redirect(t_process *p);
void fd_redirect(void);
void fd_reset(void);
int ft_cmd_process(char **argv);
int ft_cmd_exec(char *execpath, char **argv);
char *ft_findexec(char *path, char *file);
void ast_free(void *data, size_t content_size);
void set_exitstatus(int status);
void ast_free(void *data, size_t content_size);
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */
/* Updated: 2016/12/13 13:00:19 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:50:50 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,32 +16,24 @@
# include <sys/types.h>
# include <termios.h>
# include "libft.h"
# include "types.h"
# include "exec.h"
# define TYPE_BUILTIN
typedef struct s_job t_job;
typedef struct s_process t_process;
typedef struct s_jobc t_jobc;
# define PROCESS_COMPLETED 1 << 0
# define PROCESS_STOPED 1 << 1
# define PROCESS_BUILTIN 1 << 2
# define PROCESS_BINARY 1 << 3
# define PROCESS_SCRIPT 1 << 4
# define PROCESS_UNKNOWN 1 << 5
struct s_job
{
int id;
pid_t pgid;
char notified;
char *command;
int foreground;
t_list *first_process;
};
struct s_process
{
char **argv;
pid_t pid;
int fdin;
int fdout;
char completed;
char stopped;
int status;
struct termios tmodes;
};
struct s_jobc
@ -50,24 +42,20 @@ struct s_jobc
pid_t shell_pgid;
int current_id;
int rank[2];
t_job job;
t_process process;
struct termios shell_tmodes;
};
# include "minishell.h"
extern t_data *g_data;
void job_new(char **av, pid_t pid);
void job_announce(t_job *job);
void job_free(void *content, size_t content_size);
int job_cmp_pid(t_job *job, pid_t *pid);
int job_cmp_id(t_job *job, int *id);
int job_addprocess(t_process *p);
void job_update_id(void);
void job_print_change(t_job *job, int status);
void job_update_rank(void);
void job_new(char **av, pid_t pid);
void job_free(void *content, size_t content_size);
int job_cmp_pid(t_job *job, pid_t *pid);
int job_cmp_id(t_job *job, int *id);
int check_chlds(void);
void sigchld_handler(int signo);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2016/12/13 12:56:56 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:51:07 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,10 +16,12 @@
# include "libft.h"
# include "types.h"
# include "line_editing.h"
# include "lexer.h"
# include "parser.h"
# include "exec.h"
# include "builtin.h"
# include "job_control.h"
# include <dirent.h>
@ -77,13 +79,6 @@ void shell_exit(void);
int data_init(void);
void data_exit(void);
int ft_builtin(char **av);
int builtin_echo(char **av, t_data *data);
int builtin_cd(char **av, t_data *data);
int builtin_exit(char **av, t_data *data);
int builtin_setenv(char **av, t_data *data);
int builtin_unsetenv(char **av, t_data *data);
int builtin_env(char **av, t_data *data);
void ft_expand_dollar(char **av, char **env);
char *ft_findexec(char *path, char *file);

24
42sh/includes/types.h Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */
/* Updated: 2016/12/13 17:51:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TYPES_H
# define TYPES_H
typedef struct s_job t_job;
typedef struct s_jobc t_jobc;
typedef struct s_execmap t_execmap;
typedef struct s_process t_process;
typedef long long t_type;
typedef long long t_flag;
typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]);
#endif

@ -1 +1 @@
Subproject commit 2ba016bcad85d67ed6a18da54067eda8e3deca5b
Subproject commit c4890729647c61fbe8f4fb627d0fcc098d224e54

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */
/* Updated: 2016/12/03 11:58:14 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:56:40 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,23 +17,23 @@
#define HAS_CDOPT_L(x) (x & CD_OPT_L)
#define CDERR_1 "cd: no such file or directory: %s\n"
static char *builtin_cd_special(char **av, char **env)
static char *builtin_cd_special(char *const av[], char *const env[])
{
char *target;
if (!*av)
{
if (!(target = ft_getenv(env, "HOME")))
if (!(target = ft_getenv((char**)env, "HOME")))
return (NULL);
}
else if (ft_strcmp(*av, "-") == 0)
target = ft_getenv(env, "OLDPWD");
target = ft_getenv((char**)env, "OLDPWD");
else
target = *av;
return (target);
}
static int builtin_cd_opts(char **av, int *opts)
static int builtin_cd_opts(char *const av[], int *opts)
{
int i;
int j;
@ -60,7 +60,7 @@ static int builtin_cd_opts(char **av, int *opts)
return (i);
}
int builtin_cd(char **av, t_data *data)
int builtin_cd(const char *path, char *const av[], char *const envp[])
{
int i;
int opts;
@ -68,9 +68,9 @@ int builtin_cd(char **av, t_data *data)
opts = 0;
i = builtin_cd_opts(av, &opts);
if (!(target = builtin_cd_special(av + i, data->env)))
if (!(target = builtin_cd_special(av + i, envp)))
return (0);
builtin_setenv((char*[3]){"OLDPWD", getcwd(NULL, 0)}, data);
builtin_setenv(path, (char*[3]){"OLDPWD", getcwd(NULL, 0)}, envp);
if (chdir(target))
{
ft_printf(CDERR_1, target);
@ -78,6 +78,6 @@ int builtin_cd(char **av, t_data *data)
}
else if (target != av[i])
ft_printf("%s\n", target);
builtin_setenv((char*[3]){"PWD", getcwd(NULL, 0)}, data);
builtin_setenv(path, (char*[3]){"PWD", getcwd(NULL, 0)}, envp);
return (0);
}

View file

@ -6,15 +6,16 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:21:41 by jhalford #+# #+# */
/* Updated: 2016/11/28 14:22:02 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:58:14 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "builtin.h"
int builtin_echo(char **av, t_data *data)
int builtin_echo(const char *path, char *const av[], char *const envp[])
{
(void)data;
(void)envp;
(void)path;
av++;
while (*av)
{

View file

@ -6,33 +6,40 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */
/* Updated: 2016/12/12 18:03:37 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:56:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_env(char **av, t_data *data)
int builtin_env(const char *path, char *const argv[], char *const envp[])
{
int i;
char **env;
i = 1;
env = NULL;
if (!av[1])
{
ft_sstrprint(data->env, '\n');
ft_putchar('\n');
}
else
{
while (av[i] && ft_strchr(av[i], '='))
{
env = ft_sstradd(env, av[i]);
i++;
}
if (av[i])
ft_cmd_process(av + i);
}
(void)argv;
(void)envp;
(void)path;
return (0);
}
/* int builtin_env(char **av, t_data *data) */
/* { */
/* int i; */
/* char **env; */
/* i = 1; */
/* env = NULL; */
/* if (!av[1]) */
/* { */
/* ft_sstrprint(data->env, '\n'); */
/* ft_putchar('\n'); */
/* } */
/* else */
/* { */
/* while (av[i] && ft_strchr(av[i], '=')) */
/* { */
/* env = ft_sstradd(env, av[i]); */
/* i++; */
/* } */
/* if (av[i]) */
/* ft_cmd_process(av + i); */
/* } */
/* return (0); */
/* } */

View file

@ -6,20 +6,21 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */
/* Updated: 2016/12/01 14:35:36 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:59:05 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_exit(char **av, t_data *data)
int builtin_exit(const char *path, char *const av[], char *const envp[])
{
int status;
(void)path;
if (av[1])
status = ft_atoi(av[1]);
else
status = ft_atoi(ft_getenv(data->env, "?"));
status = ft_atoi(ft_getenv((char**)envp, "?"));
exit(status);
return (0);
}

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin.c :+: :+: :+: */
/* is_builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:21:34 by jhalford #+# #+# */
/* Updated: 2016/12/12 17:56:11 by jhalford ### ########.fr */
/* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */
/* Updated: 2016/12/13 17:31:18 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,32 +22,13 @@ t_stof g_builtin[] = {
{NULL, NULL},
};
int ft_builtin(char **av)
t_execf *is_builtin(t_process *p)
{
int i;
int ret;
t_data *data;
i = -1;
data = data_singleton();
while (g_builtin[++i].name)
if (ft_strcmp(g_builtin[i].name, *av) == 0)
{
if (data->exec.fdout != STDOUT)
{
if (fork() == 0)
{
fd_redirect();
ret = (g_builtin[i].f)(av, data);
exit(ret);
}
}
else
{
ret = (g_builtin[i].f)(av, data);
set_exitstatus(ret);
}
return (1);
}
return (0);
if (ft_strcmp(g_builtin[i].name, p->argv[0]) == 0)
return (g_builtin[i].f);
return (NULL);
}

View file

@ -6,11 +6,11 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */
/* Updated: 2016/12/09 21:28:29 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:17:38 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "exec.h"
void ast_free(void *data, size_t content_size)
{

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */
/* Updated: 2016/12/12 18:03:03 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:19:19 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,9 +14,9 @@
int exec_ampersand(t_btree **ast)
{
data_singleton()->exec.foreground = 1;
data_singleton()->exec.job.foreground = 1;
ft_exec(&(*ast)->left);
data_singleton()->exec.foreground = 0;
data_singleton()->exec.job.foreground = 0;
ft_exec(&(*ast)->right);
btree_delone(ast, &ast_free);
return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */
/* Updated: 2016/12/13 12:35:12 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:41:09 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,11 +15,17 @@
int exec_command(t_btree **ast)
{
t_astnode *node;
t_process *process;
t_job *job;
node = (*ast)->item;
ft_strappend(&data->jobc.process.command, ft_sstrcat(node->data.sstr));
DG("gonna exec_command '%s'", data->joc.process.command);
ft_cmd_process(node->data.sstr);
process = &data_singleton()->exec.process;
job = &data_singleton()->exec.job;
process->argv = ft_sstrdup(node->data.sstr);
DG("gonna launch_process");
process_setexec(process);
launch_process(process);
job_addprocess(process);
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */
/* Updated: 2016/12/12 18:03:11 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:13:58 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,9 +19,9 @@ int exec_dgreat(t_btree **ast)
node = (*ast)->item;
fd = open(node->data.redir.word.word, O_WRONLY | O_APPEND | O_CREAT, 0644);
data_singleton()->exec.fdout = fd;
data_singleton()->exec.process.fdout = fd;
ft_exec(&(*ast)->left);
data_singleton()->exec.fdout = STDOUT;
data_singleton()->exec.process.fdout = STDOUT;
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */
/* Updated: 2016/12/12 18:03:23 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:14:19 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,9 +19,9 @@ int exec_great(t_btree **ast)
node = (*ast)->item;
fd = open(node->data.redir.word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644);
data_singleton()->exec.fdout = fd;
data_singleton()->exec.process.fdout = fd;
ft_exec(&(*ast)->left);
data_singleton()->exec.fdout = STDOUT;
data_singleton()->exec.process.fdout = STDOUT;
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */
/* Updated: 2016/12/13 13:00:20 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:14:46 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,16 +16,15 @@ int exec_less(t_btree **ast)
{
t_astnode *node;
int fd;
t_data *data;
node = (*ast)->item;
fd = open(node->data.redir.word.word, O_RDONLY);
data_singleton()->jobc.process.fdin = fd;
ft_strappend(&data->jobc.process.command, "<");
ft_strappend(&data->jobc.process.command, node->data.redir.word.word);
data_singleton()->exec.process.fdin = fd;
/* ft_strappend(&data->exec.process.command, "<"); */
/* ft_strappend(&data->exec.process.command, node->data.redir.word.word); */
ft_exec(&(*ast)->left);
data_singleton()->jobc.process.fdin = STDIN;
data->jobc.process.command = NULL;
data_singleton()->exec.process.fdin = STDIN;
/* data->exec.process.command = NULL; */
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */
/* Updated: 2016/12/13 12:31:15 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:15:22 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,17 +20,17 @@ int exec_pipe(t_btree **ast)
data = data_singleton();
pipe(fds);
DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]);
data->jobc.process.fdout = fds[PIPE_WRITE];
data->exec.process.fdout = fds[PIPE_WRITE];
ft_exec(&(*ast)->left);
if (data->jobc.process.fdout != STDOUT)
close(data->exec.fdout);
data->jobc.process.fdout = STDOUT;
data->jobc.process.fdin = fds[PIPE_READ];
if (data->exec.process.fdout != STDOUT)
close(data->exec.process.fdout);
data->exec.process.fdout = STDOUT;
data->exec.process.fdin = fds[PIPE_READ];
ft_exec(&(*ast)->right);
close(fds[PIPE_WRITE]);
close(fds[PIPE_READ]);
data->jobc.process.fdin = STDIN;
data->jobc.process.fdout = STDOUT;
data->exec.process.fdin = STDIN;
data->exec.process.fdout = STDOUT;
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -1,68 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:18 by jhalford #+# #+# */
/* Updated: 2016/12/13 12:56:18 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
extern pid_t g_pid;
int ft_cmd_process(char **argv)
{
char *execpath;
ft_expand_dollar(argv, data_singleton()->env);
if (ft_builtin(argv))
return (0);
else if (ft_strchr(argv[0], '/'))
execpath = ft_strdup(argv[0]);
else if (!(execpath = ft_findexec(ft_getenv(data_singleton()->env, "PATH"), argv[0])))
{
ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, argv[0]);
set_exitstatus(127);
return (-1);
}
return (ft_cmd_exec(execpath, argv));
}
int ft_cmd_exec(char *execpath, char **argv)
{
pid_t pid;
int status;
if (access(execpath, X_OK) == -1)
{
ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, argv[0]);
ft_strdel(&execpath);
return (-1);
}
if ((pid = fork()) == -1)
return (-1);
else if (pid == 0)
{
fd_redirect();
execve(execpath, argv, data_singleton()->env);
exit(42);
}
else
{
ft_strdel(&execpath);
g_pid = pid;
if (data_singleton()->exec.foreground)
job_new(argv, pid);
else if (data_singleton()->exec.fdout == STDOUT)
{
waitpid(pid, &status, 0);
set_exitstatus(status);
}
g_pid = 0;
}
return (0);
}

View file

@ -6,13 +6,13 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */
/* Updated: 2016/12/12 18:11:48 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:40:43 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
t_execfunc g_execfunc[] =
t_execmap g_execmap[] =
{
{TK_AND_IF, &exec_and_if},
{TK_OR_IF, &exec_or_if},
@ -35,10 +35,10 @@ int ft_exec(t_btree **ast)
if (!*ast)
return (0);
item = (*ast)->item;
while (g_execfunc[i].type)
while (g_execmap[i].type)
{
if (item->type == g_execfunc[i].type)
return ((*g_execfunc[i].f)(ast));
if (item->type == g_execmap[i].type)
return ((*g_execmap[i].f)(ast));
i++;
}
return (0);

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* launch_process.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */
/* Updated: 2016/12/13 17:50:38 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int launch_process(t_process *p)
{
t_exec *exec;
int pid;
exec = &data_singleton()->exec;
if (p->attributes & PROCESS_UNKNOWN)
ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, p->argv[0]);
if (p->attributes & PROCESS_BUILTIN && p->fdout != STDOUT)
set_exitstatus((*p->execf)(p->path, p->argv, data_singleton()->env));
else
{
if (p->attributes & (PROCESS_BINARY | PROCESS_SCRIPT)
&& access(p->path, X_OK) == -1)
{
ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, p->argv[0]);
return (-1);
}
pid = fork();
if (pid == 0)
{
process_setgroup(p);
process_redirect(p);
(*p->execf)(p->path, p->argv, data_singleton()->env);
exit(42);
}
else if (pid > 0)
p->pid = pid;
else if (pid == -1)
perror("fork");
if (p->fdout == STDOUT)
{
waitpid(pid, &p->status, 0);
set_exitstatus(p->status);
}
}
return (0);
}

View file

@ -1,30 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_redirect.c :+: :+: :+: */
/* process_redirect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */
/* Updated: 2016/12/13 12:13:24 by jhalford ### ########.fr */
/* Updated: 2016/12/13 17:49:09 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "exec.h"
void fd_redirect(void)
int process_redirect(t_process *p)
{
t_data *data;
data = data_singleton();
if (data->exec.fdin != STDIN)
if (p->fdin != STDIN)
{
dup2(data->exec.fdin, STDIN);
close(data->exec.fdin);
dup2(p->fdin, STDIN);
close(p->fdin);
}
if (data->exec.fdout != STDOUT)
if (p->fdout != STDOUT)
{
dup2(data->exec.fdout, STDOUT);
close(data->exec.fdout);
dup2(p->fdout, STDOUT);
close(p->fdout);
}
return (0);
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process_setexec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */
/* Updated: 2016/12/13 17:50:26 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int process_setexec(t_process *p)
{
if ((p->execf = is_builtin(p)))
p->attributes &= PROCESS_BUILTIN;
else if (ft_strchr(p->argv[0], '/'))
{
p->execf = &execve;
p->attributes &= PROCESS_SCRIPT;
p->path = ft_strdup(p->argv[0]);
}
else if (!(p->path = ft_findexec(ft_getenv(
data_singleton()->env, "PATH"), p->argv[0])))
{
p->execf = &execve;
p->attributes &= PROCESS_BINARY;
}
else
{
p->execf = NULL;
p->attributes &= PROCESS_UNKNOWN;
}
return (0);
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process_setgroup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:48:10 by jhalford #+# #+# */
/* Updated: 2016/12/13 17:48:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
int process_setgroup(t_process *p)
{
t_job *job;
job = data_singleton()->exec.job;
pid = getpid();
if (job->pgid == 0)
job->pgid = pid;
setpgid(pid, job->pgid);
if (job->foreground)
tcsetpgrp(STDIN_FILENO, job->pgid);
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_addprocess.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 13:54:51 by jhalford #+# #+# */
/* Updated: 2016/12/13 17:06:57 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
int job_addprocess(t_process *p)
{
t_jobc *jobc;
t_job *job;
jobc = &data_singleton()->jobc;
job = &data_singleton()->exec.job;
if (p->fdin == STDIN)
{
job->id = current_id;
job_update_id();
ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job)));
if (job->foreground)
put_job_in_foreground(job, 0);
else
put_job_in_background(job, 0);
}
if (p->fdout = STDOUT)
job_notify_new(first_job);
first_job = jobc->first_job->content;
ft_lstadd(first_job->process, ft_lstnew(p, sizeof(*p)));
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_is_completed.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 15:10:20 by jhalford #+# #+# */
/* Updated: 2016/12/13 15:24:25 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
int job_is_completed(t_job *job)
{
t_list *lst;
t_process *process;
lst = job->lst;
while (lst)
{
process = lst->content;
if (!(process->attributes & PROCESS_COMPLETED))
return (0);
lst = lst->next;
}
return (1);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_is_stopped.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 15:06:45 by jhalford #+# #+# */
/* Updated: 2016/12/13 15:24:26 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
int job_is_stopped(t_job *job)
{
t_list *lst;
t_process *process;
lst = job->lst;
while (lst)
{
process = lst->content;
if (!(process->attributes & (PROCESS_COMPLETED | PROCESS_STOPPED)))
return (0);
lst = lst->next;
}
return (1);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_notify_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 14:27:01 by jhalford #+# #+# */
/* Updated: 2016/12/13 14:58:23 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
void job_notify_new(t_job *job)
{
t_list *process;
process = job->first_process;
ft_printf("{mag}[%i]", job->id);
while (process)
{
ft_printf(" %i", ((t_process*)process->content)->pid);
process=process->next;
}
ft_printf("{eoc}\n");
}

View file

@ -1,18 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_announce.c :+: :+: :+: */
/* put_job_in_background.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 17:05:49 by jhalford #+# #+# */
/* Updated: 2016/12/12 12:54:09 by jhalford ### ########.fr */
/* Created: 2016/12/13 15:03:29 by jhalford #+# #+# */
/* Updated: 2016/12/13 15:04:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
void job_announce(t_job *job)
int put_job_in_background(t_job *job, int cont)
{
ft_printf("{mag}[%i] %i{eoc}\n", job->id, job->pid);
/* Send the job a continue signal, if necessary. */
if (cont)
if (kill (-j->pgid, SIGCONT) < 0)
perror ("kill (SIGCONT)");
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* put_job_in_foreground.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 14:58:36 by jhalford #+# #+# */
/* Updated: 2016/12/13 15:06:21 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
int put_job_in_foreground(t_job *job, int cont)
{
t_jobc *jobc;
/* Put the job into the foreground. */
tcsetpgrp(shell_terminal, job->pgid);
/* Send the job a continue signal, if necessary. */
if (cont)
{
tcsetattr (shell_terminal, TCSADRAIN, &job->tmodes);
if (kill(- job->pgid, SIGCONT) < 0)
perror("kill (SIGCONT)");
}
/* Wait for it to report. */
wait_for_job(j);
/* Put the shell back in the foreground. */
tcsetpgrp(shell_terminal, jobc->shell_pgid);
/* Restore the shells terminal modes. */
tcgetattr(shell_terminal, &job->tmodes);
tcsetattr(shell_terminal, TCSADRAIN, &jobc->shell_tmodes);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2016/12/12 17:50:03 by jhalford ### ########.fr */
/* Updated: 2016/12/13 13:38:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,6 +32,10 @@ int data_init(void)
data->jobc.current_id = 1;
data->jobc.rank[0] = 0;
data->jobc.rank[1] = 0;
data->jobc.job.id = 0;
data->jobc.job.pgid = 0;
data->jobc.job.notified = 0;
data->jobc.job.foreground = 0;
if (!(data->line.history = ft_dlstnew(NULL, 0)))
return (-1);
if ((term_name = ft_getenv(data->env, "TERM")) == NULL)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
/* Updated: 2016/12/12 18:11:50 by jhalford ### ########.fr */
/* Updated: 2016/12/13 15:14:35 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */