job_control first commit:

This commit is contained in:
Jack Halford 2016-12-10 17:47:36 +01:00
parent 4feaa64e08
commit 4cfeb62747
19 changed files with 193 additions and 39 deletions

View file

@ -42,11 +42,6 @@ $(D_OBJ)/%.o: $(D_SRC)/builtin/%.c includes/minishell.h
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
@echo "Compiling "$<"..." @echo "Compiling "$<"..."
$(D_OBJ)/%.o: $(D_SRC)/minishell-exec/%.c includes/minishell.h
@$(MKDIR) $(D_OBJ)
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
@echo "Compiling "$<"..."
$(D_OBJ)/%.o: $(D_SRC)/line-editing/%.c includes/line_editing.h $(D_OBJ)/%.o: $(D_SRC)/line-editing/%.c includes/line_editing.h
@$(MKDIR) $(D_OBJ) @$(MKDIR) $(D_OBJ)
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
@ -67,6 +62,11 @@ $(D_OBJ)/%.o: $(D_SRC)/exec/%.c includes/exec.h
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
@echo "Compiling "$<"..." @echo "Compiling "$<"..."
$(D_OBJ)/%.o: $(D_SRC)/job-control/%.c includes/job_control.h
@$(MKDIR) $(D_OBJ)
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
@echo "Compiling "$<"..."
libft/libft.a: libft/src/*/*.c libft/libft.a: libft/src/*/*.c
@echo "libft/libft.a" @echo "libft/libft.a"
@$(MAKE) -C libft 2>/dev/null @$(MAKE) -C libft 2>/dev/null

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2016/12/06 18:23:29 by jhalford ### ########.fr */ /* Updated: 2016/12/10 17:09:03 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,6 +30,7 @@ extern t_execfunc g_execfunc[];
int ft_exec(t_btree **ast, t_data *data); int ft_exec(t_btree **ast, t_data *data);
int exec_semi(t_btree **ast, t_data *data); int exec_semi(t_btree **ast, t_data *data);
int exec_ampersand(t_btree **ast, t_data *data);
int exec_or_if(t_btree **ast, t_data *data); int exec_or_if(t_btree **ast, t_data *data);
int exec_and_if(t_btree **ast, t_data *data); int exec_and_if(t_btree **ast, t_data *data);
int exec_pipe(t_btree **ast, t_data *data); int exec_pipe(t_btree **ast, t_data *data);

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_control.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:36:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef JOB_CONTROL_H
# define JOB_CONTROL_H
# include "minishell.h"
typedef struct s_job t_job;
struct s_job
{
int id;
pid_t pid;
char *command;
};
void job_new(t_data *data, char **av, pid_t pid);
void job_announce(t_job *job);
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2016/12/05 14:11:52 by jhalford ### ########.fr */ /* Updated: 2016/12/10 16:00:51 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2016/12/07 18:09:27 by jhalford ### ########.fr */ /* Updated: 2016/12/10 17:12:35 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,6 +20,7 @@
# include "lexer.h" # include "lexer.h"
# include "parser.h" # include "parser.h"
# include "exec.h" # include "exec.h"
# include "job_control.h"
# include <dirent.h> # include <dirent.h>
# include <sys/stat.h> # include <sys/stat.h>
@ -31,34 +32,42 @@ typedef long long t_type;
typedef struct s_line t_line; typedef struct s_line t_line;
typedef struct s_comp t_comp; typedef struct s_comp t_comp;
typedef struct s_exec t_exec; typedef struct s_exec t_exec;
typedef struct s_jobc t_jobc;
struct s_line struct s_line
{ {
t_dlist *history; t_dlist *history;
int input_pos; int input_pos;
t_list *qstack; t_list *qstack;
char *input; char *input;
}; };
struct s_comp struct s_comp
{ {
int a; int a;
}; };
struct s_exec struct s_exec
{ {
int fdin; int fdin;
int fdout; int fdout;
char *aol_status; int amp;
int aol_search; char *aol_status;
int aol_search;
};
struct s_jobc
{
t_list *list;
}; };
struct s_data struct s_data
{ {
char **env; char **env;
t_exec exec; t_line line;
t_line line; t_comp comp;
t_comp comp; t_exec exec;
t_jobc jobc;
}; };
typedef struct s_data t_data; typedef struct s_data t_data;
@ -67,7 +76,8 @@ typedef enum e_qstate t_qstate;
extern t_stof g_builtins[]; extern t_stof g_builtins[];
extern pid_t g_pid; extern pid_t g_pid;
void sig_handler(int signo); void sigint_handler(int signo);
void sigstop_handler(int signo);
int data_init(t_data *data); int data_init(t_data *data);
void data_exit(t_data *data); void data_exit(t_data *data);
void ft_cleanup(void); void ft_cleanup(void);

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_ampersand.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */
/* Updated: 2016/12/10 16:53:06 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_ampersand(t_btree **ast, t_data *data)
{
data->exec.amp = 1;
ft_exec(&(*ast)->left, data);
data->exec.amp = 0;
ft_exec(&(*ast)->right, data);
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */
/* Updated: 2016/12/05 12:14:37 by jhalford ### ########.fr */ /* Updated: 2016/12/10 17:19:57 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:18 by jhalford #+# #+# */ /* Created: 2016/11/27 21:13:18 by jhalford #+# #+# */
/* Updated: 2016/12/09 21:50:26 by jhalford ### ########.fr */ /* Updated: 2016/12/10 17:34:14 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,7 @@ int ft_cmd_process(char **argv, t_data *data)
if (ft_builtin(argv, data)) if (ft_builtin(argv, data))
return (0); return (0);
else if (ft_strchr(argv[0], '/')) else if (ft_strchr(argv[0], '/'))
execpath = argv[0]; execpath = ft_strdup(argv[0]);
else if (!(execpath = ft_findexec(ft_getenv(data->env, "PATH"), argv[0]))) else if (!(execpath = ft_findexec(ft_getenv(data->env, "PATH"), argv[0])))
{ {
ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, argv[0]); ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, argv[0]);
@ -54,7 +54,9 @@ int ft_cmd_exec(char *execpath, char **argv, t_data *data)
{ {
ft_strdel(&execpath); ft_strdel(&execpath);
g_pid = pid; g_pid = pid;
if (data->exec.fdout == STDOUT) if (data->exec.amp)
job_new(data, argv, pid);
else if (data->exec.fdout == STDOUT)
{ {
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
set_exitstatus(data, status); set_exitstatus(data, status);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */
/* Updated: 2016/12/05 13:37:46 by jhalford ### ########.fr */ /* Updated: 2016/12/10 17:22:42 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,7 @@ t_execfunc g_execfunc[] =
{TK_AND_IF, &exec_and_if}, {TK_AND_IF, &exec_and_if},
{TK_OR_IF, &exec_or_if}, {TK_OR_IF, &exec_or_if},
{TK_SEMI, &exec_semi}, {TK_SEMI, &exec_semi},
{TK_AMP, &exec_ampersand},
{TK_PIPE, &exec_pipe}, {TK_PIPE, &exec_pipe},
{TK_LESS, &exec_less}, {TK_LESS, &exec_less},
{TK_GREAT, &exec_great}, {TK_GREAT, &exec_great},
@ -31,9 +32,9 @@ int ft_exec(t_btree **ast, t_data *data)
int i; int i;
i = 0; i = 0;
item = (*ast)->item;
if (!*ast) if (!*ast)
return (0); return (0);
item = (*ast)->item;
while (g_execfunc[i].type) while (g_execfunc[i].type)
{ {
if (item->type == g_execfunc[i].type) if (item->type == g_execfunc[i].type)

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_announce.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 17:05:49 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:26:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
void job_announce(t_job *job)
{
ft_printf("[%i] %i\n", job->id, job->pid);
}

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 16:51:54 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:37:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
void job_new(t_data *data, char **av, pid_t pid, t_type type)
{
t_job job;
DG("got new job");
job.command = ft_sstrcat(av, ' ');
job.pid = pid;
job.id = 42;
ft_lstadd(&data->jobc.list, ft_lstnew(&job, sizeof(job)));
job_announce(data->jobc.list->content);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sigchld_handler.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 17:37:56 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:46:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void sigchld_handler(int signo)
{
(void)signo;
DG("got SIGCHLD");
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sigtstp_handler.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 15:14:53 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:46:16 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void sigtstp_handler(int signo)
{
(void)signo;
DG("got SIGTSTP");
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:14:12 by jhalford #+# #+# */ /* Created: 2016/12/01 12:14:12 by jhalford #+# #+# */
/* Updated: 2016/12/07 17:27:25 by jhalford ### ########.fr */ /* Updated: 2016/12/10 17:18:29 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:45:23 by jhalford #+# #+# */ /* Created: 2016/11/28 18:45:23 by jhalford #+# #+# */
/* Updated: 2016/12/09 14:48:01 by jhalford ### ########.fr */ /* Updated: 2016/12/10 17:18:25 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 13:35:03 by jhalford #+# #+# */ /* Created: 2016/12/03 13:35:03 by jhalford #+# #+# */
/* Updated: 2016/12/07 16:30:40 by jhalford ### ########.fr */ /* Updated: 2016/12/10 17:16:40 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* sig_handler.c :+: :+: :+: */ /* sigint_handler.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:43:22 by jhalford #+# #+# */ /* Created: 2016/12/10 15:14:47 by jhalford #+# #+# */
/* Updated: 2016/12/03 13:31:33 by jhalford ### ########.fr */ /* Updated: 2016/12/10 15:31:56 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,11 +14,12 @@
pid_t g_pid; pid_t g_pid;
void sig_handler(int signo) void sigint_handler(int signo)
{ {
(void)signo; (void)signo;
if (signo == SIGINT) if (signo == SIGINT)
{ {
DG("got SIGINT");
if (g_pid) if (g_pid)
kill(g_pid, SIGINT); kill(g_pid, SIGINT);
if (kill(g_pid, 0) == 0) if (kill(g_pid, 0) == 0)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2016/12/09 19:15:12 by jhalford ### ########.fr */ /* Updated: 2016/12/10 17:35:16 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,11 +26,18 @@ int data_init(t_data *data)
data->exec.fdout = STDOUT; data->exec.fdout = STDOUT;
data->exec.aol_status = NULL; data->exec.aol_status = NULL;
data->exec.aol_search = 0; data->exec.aol_search = 0;
data->exec.amp = 0;
data->jobc.list = NULL;
if (!(data->line.history = ft_dlstnew(NULL, 0))) if (!(data->line.history = ft_dlstnew(NULL, 0)))
return (-1); return (-1);
if ((term_name = ft_getenv(data->env, "TERM")) == NULL) if ((term_name = ft_getenv(data->env, "TERM")) == NULL)
return (-1); return (-1);
if (tgetent(NULL, term_name) != 1) if (tgetent(NULL, term_name) != 1)
return (-1); return (-1);
if (signal(SIGINT, sigint_handler) == SIG_ERR)
ft_dprintf(STDERR, "\ncan't catch SIGINT\n");
if (signal(SIGTSTP, sigtstp_handler) == SIG_ERR)
ft_dprintf(STDERR, "\ncan't catch SIGTSTP\n");
if (signal(SIGCHLD, sigchld_handler) == SIG_ERR)
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2016/12/09 22:15:07 by jhalford ### ########.fr */ /* Updated: 2016/12/10 17:16:19 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,8 +22,6 @@ int main(void)
ast = NULL; ast = NULL;
if (data_init(&data)) if (data_init(&data))
return (1); return (1);
if (signal(SIGINT, sig_handler) == SIG_ERR)
ft_dprintf(STDERR, "\ncan't catch SIGINT\n");
DG("{inv}{bol}{gre}start of shell"); DG("{inv}{bol}{gre}start of shell");
while (1) while (1)
{ {