job control advances, still have to do ctrl-z, fg, bg, jobs

This commit is contained in:
Jack Halford 2016-12-12 18:13:29 +01:00
parent 0b3b3e2810
commit 6cfac2eaff
43 changed files with 459 additions and 182 deletions

View file

@ -6,49 +6,62 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:09:03 by jhalford ### ########.fr */
/* Updated: 2016/12/12 18:02:49 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef EXEC_H
# define EXEC_H
# include "minishell.h"
# define PIPE_READ 0
# define PIPE_WRITE 1
# include "libft.h"
typedef struct s_execfunc t_execfunc;
typedef long long t_type;
struct s_exec
{
int fdin;
int fdout;
int amp;
char *aol_status;
int aol_search;
};
struct s_execfunc
{
t_type type;
int (*f)(t_btree **ast, t_data *data);
int (*f)(t_btree **ast);
};
# include "minishell.h"
extern t_execfunc g_execfunc[];
int ft_exec(t_btree **ast, t_data *data);
int ft_exec(t_btree **ast);
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_and_if(t_btree **ast, t_data *data);
int exec_pipe(t_btree **ast, t_data *data);
int exec_semi(t_btree **ast);
int exec_ampersand(t_btree **ast);
int exec_or_if(t_btree **ast);
int exec_and_if(t_btree **ast);
int exec_pipe(t_btree **ast);
int exec_less(t_btree **ast, t_data *data);
int exec_great(t_btree **ast, t_data *data);
int exec_dgreat(t_btree **ast, t_data *data);
int exec_command(t_btree **ast, t_data *data);
int exec_less(t_btree **ast);
int exec_great(t_btree **ast);
int exec_dgreat(t_btree **ast);
int exec_command(t_btree **ast);
void fd_redirect(t_data *data);
void fd_reset(t_data *data);
void fd_redirect(void);
void fd_reset(void);
int ft_cmd_process(char **argv, t_data *data);
int ft_cmd_exec(char *execpath, char **argv, t_data *data);
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(t_data *data, int status);
void set_exitstatus(int status);
#endif

View file

@ -6,16 +6,19 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */
/* Updated: 2016/12/10 18:20:40 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:51:54 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef JOB_CONTROL_H
# define JOB_CONTROL_H
# include "minishell.h"
# include <sys/types.h>
# include <termios.h>
# include "libft.h"
typedef struct s_job t_job;
typedef struct s_jobc t_jobc;
struct s_job
{
@ -24,12 +27,29 @@ struct s_job
char *command;
};
struct s_jobc
{
t_list *list;
pid_t shell_pgid;
int current_id;
int rank[2];
struct termios shell_tmodes;
};
# include "minishell.h"
extern t_data *g_data;
void job_new(t_data *data, char **av, pid_t pid);
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);
void job_update_id(void);
void job_print_change(t_job *job, int status);
void job_update_rank(void);
int ft_cmppid(t_job *job, pid_t *pid);
int check_chlds(void);
void sigchld_handler(int signo);
void sigint_handler(int signo);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:38:21 by jhalford #+# #+# */
/* Updated: 2016/12/07 16:57:40 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:49:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:56:39 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:56:30 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,6 +33,13 @@ typedef struct s_line t_line;
typedef struct s_comp t_comp;
typedef struct s_exec t_exec;
typedef struct s_jobc t_jobc;
typedef enum e_mode t_mode;
enum e_mode
{
MODE_INPUT,
MODE_EXEC,
};
struct s_line
{
@ -47,23 +54,10 @@ struct s_comp
int a;
};
struct s_exec
{
int fdin;
int fdout;
int amp;
char *aol_status;
int aol_search;
};
struct s_jobc
{
t_list *list;
};
struct s_data
{
char **env;
t_mode mode;
t_line line;
t_comp comp;
t_exec exec;
@ -78,11 +72,12 @@ extern pid_t g_pid;
t_data *data_singleton();
int data_init(t_data *data);
void data_exit(t_data *data);
void ft_cleanup(void);
void shell_init(void);
void shell_exit(void);
int data_init(void);
void data_exit(void);
int ft_builtin(char **av, t_data *data);
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);

@ -1 +1 @@
Subproject commit 5fab2c76b033729a3b16a15f2b5ac63f2deaafc4
Subproject commit 6f46797917052ce5d9c4e85ce7aa7d63701d919c

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:21:34 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:17:21 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:56:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,12 +22,14 @@ t_stof g_builtin[] = {
{NULL, NULL},
};
int ft_builtin(char **av, t_data *data)
int ft_builtin(char **av)
{
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)
{
@ -35,7 +37,7 @@ int ft_builtin(char **av, t_data *data)
{
if (fork() == 0)
{
fd_redirect(data);
fd_redirect();
ret = (g_builtin[i].f)(av, data);
exit(ret);
}
@ -43,7 +45,7 @@ int ft_builtin(char **av, t_data *data)
else
{
ret = (g_builtin[i].f)(av, data);
set_exitstatus(data, ret);
set_exitstatus(ret);
}
return (1);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */
/* Updated: 2016/11/28 14:28:37 by jhalford ### ########.fr */
/* Updated: 2016/12/12 18:03:37 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,7 +32,7 @@ int builtin_env(char **av, t_data *data)
i++;
}
if (av[i])
ft_cmd_process(av + i, data);
ft_cmd_process(av + i);
}
return (0);
}

View file

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

View file

@ -6,27 +6,30 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 20:52:28 by jhalford #+# #+# */
/* Updated: 2016/12/05 12:17:33 by jhalford ### ########.fr */
/* Updated: 2016/12/12 18:01:06 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_and_if(t_btree **ast, t_data *data)
int exec_and_if(t_btree **ast)
{
t_data *data;
data = data_singleton();
if (data->exec.aol_status == NULL
|| (data->exec.aol_search == TK_AND_IF
&& *data->exec.aol_status == '0')
|| (data->exec.aol_search == TK_OR_IF
&& *data->exec.aol_status != '0'))
{
ft_exec(&(*ast)->left, data);
ft_exec(&(*ast)->left);
data->exec.aol_status = ft_getenv(data->env, "?");
}
data->exec.aol_search = TK_AND_IF;
if (*data->exec.aol_status == '0'
|| ((t_astnode*)(*ast)->right->item)->type != TK_COMMAND)
ft_exec(&(*ast)->right, data);
ft_exec(&(*ast)->right);
data->exec.aol_status = NULL;
data->exec.aol_search = 0;
btree_delone(ast, &ast_free);

View file

@ -6,18 +6,18 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */
/* Updated: 2016/12/05 14:06:34 by jhalford ### ########.fr */
/* Updated: 2016/12/12 18:01:19 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_command(t_btree **ast, t_data *data)
int exec_command(t_btree **ast)
{
t_astnode *node;
node = (*ast)->item;
ft_cmd_process(node->data.sstr, data);
ft_cmd_process(node->data.sstr);
btree_delone(ast, &ast_free);
return (0);
}

View file

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

View file

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

View file

@ -6,22 +6,22 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */
/* Updated: 2016/12/05 12:12:59 by jhalford ### ########.fr */
/* Updated: 2016/12/12 18:03:52 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_less(t_btree **ast, t_data *data)
int exec_less(t_btree **ast)
{
t_astnode *node;
int fd;
node = (*ast)->item;
fd = open(node->data.redir.word.word, O_RDONLY);
data->exec.fdin = fd;
ft_exec(&(*ast)->left, data);
data->exec.fdin = STDIN;
data_singleton()->exec.fdin = fd;
ft_exec(&(*ast)->left);
data_singleton()->exec.fdin = STDIN;
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,27 +6,30 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */
/* Updated: 2016/12/09 21:50:19 by jhalford ### ########.fr */
/* Updated: 2016/12/12 18:04:08 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_or_if(t_btree **ast, t_data *data)
int exec_or_if(t_btree **ast)
{
t_data *data;
data = data_singleton();
if (data->exec.aol_status == NULL
|| (data->exec.aol_search == TK_AND_IF
&& *data->exec.aol_status == '0')
|| (data->exec.aol_search == TK_OR_IF
&& *data->exec.aol_status != '0'))
{
ft_exec(&(*ast)->left, data);
ft_exec(&(*ast)->left);
data->exec.aol_status = ft_getenv(data->env, "?");
}
data->exec.aol_search = TK_OR_IF;
if (*data->exec.aol_status != '0'
|| ((t_astnode*)(*ast)->right->item)->type != TK_COMMAND)
ft_exec(&(*ast)->right, data);
ft_exec(&(*ast)->right);
data->exec.aol_status = NULL;
data->exec.aol_search = 0;
btree_delone(ast, &ast_free);

View file

@ -6,25 +6,27 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */
/* Updated: 2016/12/06 20:26:55 by jhalford ### ########.fr */
/* Updated: 2016/12/12 18:04:20 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_pipe(t_btree **ast, t_data *data)
int exec_pipe(t_btree **ast)
{
int fds[2];
t_data *data;
data = data_singleton();
pipe(fds);
DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]);
data->exec.fdout = fds[PIPE_WRITE];
ft_exec(&(*ast)->left, data);
ft_exec(&(*ast)->left);
if (data->exec.fdout != STDOUT)
close(data->exec.fdout);
data->exec.fdout = STDOUT;
data->exec.fdin = fds[PIPE_READ];
ft_exec(&(*ast)->right, data);
ft_exec(&(*ast)->right);
close(fds[PIPE_WRITE]);
close(fds[PIPE_READ]);
data->exec.fdin = STDIN;

View file

@ -6,16 +6,16 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:19:57 by jhalford ### ########.fr */
/* Updated: 2016/12/12 18:00:21 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_semi(t_btree **ast, t_data *data)
int exec_semi(t_btree **ast)
{
ft_exec(&(*ast)->left, data);
ft_exec(&(*ast)->right, data);
ft_exec(&(*ast)->left);
ft_exec(&(*ast)->right);
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,14 +6,17 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:24:08 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:54:45 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void fd_redirect(t_data *data)
void fd_redirect(void)
{
t_data *data;
data = data_singleton();
if (data->exec.fdin != STDIN)
{
dup2(data->exec.fdin, STDIN);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:18 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:34:14 by jhalford ### ########.fr */
/* Updated: 2016/12/12 18:11:05 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,25 +14,25 @@
extern pid_t g_pid;
int ft_cmd_process(char **argv, t_data *data)
int ft_cmd_process(char **argv)
{
char *execpath;
ft_expand_dollar(argv, data->env);
if (ft_builtin(argv, data))
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->env, "PATH"), 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(data, 127);
set_exitstatus(127);
return (-1);
}
return (ft_cmd_exec(execpath, argv, data));
return (ft_cmd_exec(execpath, argv));
}
int ft_cmd_exec(char *execpath, char **argv, t_data *data)
int ft_cmd_exec(char *execpath, char **argv)
{
pid_t pid;
int status;
@ -47,19 +47,20 @@ int ft_cmd_exec(char *execpath, char **argv, t_data *data)
return (-1);
else if (pid == 0)
{
fd_redirect(data);
execve(execpath, argv, data->env);
fd_redirect();
execve(execpath, argv, data_singleton()->env);
exit(42);
}
else
{
ft_strdel(&execpath);
g_pid = pid;
if (data->exec.amp)
job_new(data, argv, pid);
else if (data->exec.fdout == STDOUT)
if (data_singleton()->exec.amp)
job_new(argv, pid);
else if (data_singleton()->exec.fdout == STDOUT)
{
waitpid(pid, &status, 0);
set_exitstatus(data, status);
set_exitstatus(status);
}
g_pid = 0;
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:22:42 by jhalford ### ########.fr */
/* Updated: 2016/12/12 18:11:48 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,7 +26,7 @@ t_execfunc g_execfunc[] =
{0, 0},
};
int ft_exec(t_btree **ast, t_data *data)
int ft_exec(t_btree **ast)
{
t_astnode *item;
int i;
@ -38,7 +38,7 @@ int ft_exec(t_btree **ast, t_data *data)
while (g_execfunc[i].type)
{
if (item->type == g_execfunc[i].type)
return ((*g_execfunc[i].f)(ast, data));
return ((*g_execfunc[i].f)(ast));
i++;
}
return (0);

View file

@ -6,17 +6,17 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */
/* Updated: 2016/12/07 16:29:11 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:54:19 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void set_exitstatus(t_data *data, int status)
void set_exitstatus(int status)
{
char *astatus;
astatus = ft_itoa(status);
builtin_setenv((char*[3]){"?", astatus}, data);
builtin_setenv((char*[3]){"?", astatus}, data_singleton());
ft_strdel(&astatus);
}

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_chlds.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 12:28:40 by jhalford #+# #+# */
/* Updated: 2016/12/12 17:23:14 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int check_chlds()
{
int status;
pid_t pid;
t_job *job;
t_list **start;
t_list *list;
DG("gonna check childs");
start = &data_singleton()->jobc.list;
pid = waitpid(-1, &status, WNOHANG);
DG("waitpid = %i", pid);
list = *start ? ft_lst_find(*start, &pid, job_cmp_pid) : NULL;
if (list)
{
job = list->content;
if (job->id < data_singleton()->jobc.current_id)
data_singleton()->jobc.current_id = job->id;
job_print_change(job, status);
ft_lst_delif(start, list->content, ft_addrcmp, job_free);
job_update_rank();
return (1);
}
return (0);
}

View file

@ -1,6 +0,0 @@
#include "minishell.h"
int ft_cmppid(t_job *job, pid_t *pid)
{
return (job->pid - *pid);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 17:05:49 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:26:44 by jhalford ### ########.fr */
/* Updated: 2016/12/12 12:54:09 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,5 +14,5 @@
void job_announce(t_job *job)
{
ft_printf("[%i] %i\n", job->id, job->pid);
ft_printf("{mag}[%i] %i{eoc}\n", job->id, job->pid);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_cmp_id.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 13:38:13 by jhalford #+# #+# */
/* Updated: 2016/12/12 13:44:23 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int job_cmp_id(t_job *job, int *id)
{
return (job->id - *id);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_cmp_pid.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 13:00:21 by jhalford #+# #+# */
/* Updated: 2016/12/12 13:40:13 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int job_cmp_pid(t_job *job, pid_t *pid)
{
return (job->pid - *pid);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */
/* Updated: 2016/12/12 13:02:05 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
void job_free(void *content, size_t content_size)
{
t_job *job;
(void)content_size;
job = content;
ft_strdel(&job->command);
free(job);
}

View file

@ -6,21 +6,31 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 16:51:54 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:58:04 by jhalford ### ########.fr */
/* Updated: 2016/12/12 15:06:23 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
void job_new(t_data *data, char **av, pid_t pid)
void job_new(char **av, pid_t pid)
{
t_job job;
t_data *data;
data = data_singleton();
DG("got new job");
if (data->mode == MODE_INPUT)
DG("am in MODE_INPUT");
else if (data->mode == MODE_EXEC)
DG("am in MODE_EXEC");
job.command = ft_sstrcat(av, ' ');
DG("job command '%s'", job.command);
job.pid = pid;
job.id = 42;
job_update_id();
DG("id = %i", data->jobc.current_id);
job.id = data->jobc.current_id;
data->jobc.rank[1] = data->jobc.rank[0];
data->jobc.rank[0] = job.id;
ft_lstadd(&data->jobc.list, ft_lstnew(&job, sizeof(job)));
job_announce(data->jobc.list->content);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_print_change.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 15:04:03 by jhalford #+# #+# */
/* Updated: 2016/12/12 16:39:31 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void job_print_change(t_job *job, int status)
{
char rank;
rank = ' ';
if (job->id == data_singleton()->jobc.rank[0])
rank = '+';
else if (job->id == data_singleton()->jobc.rank[1])
rank = '-';
ft_printf("{mag}[%i] %c %i ", job->id, rank, job->pid);
if (status == 0)
ft_printf("{gre}done{mag}");
else if (status == 9)
ft_printf("{red}killed{mag}");
else
ft_printf("exit %i", status);
ft_printf("\t%s{eoc}\n", job->command);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_update_id.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 13:33:08 by jhalford #+# #+# */
/* Updated: 2016/12/12 17:27:59 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void job_update_id(void)
{
int *id;
t_jobc *jobc;
t_list *start;
jobc = &data_singleton()->jobc;
id = &jobc->current_id;
start = jobc->list;
while (ft_lst_find(start, id, job_cmp_id))
{
*id += 1;
DG("id = %i", *id);
}
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* job_shiftstatus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 13:05:10 by jhalford #+# #+# */
/* Updated: 2016/12/12 17:23:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "job_control.h"
void job_update_rank()
{
t_jobc *jobc;
t_job *job;
t_list *list;
jobc = &data_singleton()->jobc;
list = jobc->list;
if (list)
{
job = list->content;
jobc->rank[0] = job->id;
jobc->rank[1] = list->next ? ((t_job*)list->next->content)->id : 0;
}
else
{
jobc->rank[0] = 0;
jobc->rank[1] = 0;
}
DG("updated rank: %i,%i", jobc->rank[0], jobc->rank[1]);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 17:37:56 by jhalford #+# #+# */
/* Updated: 2016/12/10 18:25:01 by jhalford ### ########.fr */
/* Updated: 2016/12/12 16:49:07 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,28 +14,17 @@
void sigchld_handler(int signo)
{
int status;
pid_t pid;
t_job *job;
t_list *start;
t_list *list;
t_data *data;
(void)signo;
DG("got SIGCHLD");
start = data_singleton()->jobc.list;
pid = waitpid(-1, &status, WNOHANG);
DG("SIGCHLD pid=%i", pid);
list = start ? ft_lst_find(start, &pid, ft_cmppid) : NULL;
if (list)
data = data_singleton();
if (data->mode == MODE_INPUT)
{
job = list->content;
if (status == 0)
ft_printf("[%i] + %i done\t%s\n", job->id, pid, job->command);
else if (status == 9)
ft_printf("[%i] + %i killed\t%s\n", job->id, pid, job->command);
DG("got SIGCHLD in MODE_INPUT (asynchronos notification)");
check_chlds();
ft_putstr(SHELL_PROMPT);
ft_putstr(data->line.input);
}
else
ft_printf("[%i] + %i exit %i\t%s\n",
job->id, pid, status, job->command);
ft_prompt();
}
DG("got SIGCHLD in MODE_EXEC, will check before next prompt");
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 15:56:40 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:36:24 by jhalford ### ########.fr */
/* Updated: 2016/12/12 13:59:16 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,11 +6,11 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:14:12 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:18:29 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:42:04 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "line_editing.h"
#include "minishell.h"
t_stof g_keys[] = {
{FT_KEY_C_K, NULL},
@ -60,6 +60,7 @@ int ft_interactive_sh(t_data *data)
return (-1);
else if (ret == 2)
{
data->mode = MODE_EXEC;
ft_lstdel(&data->line.qstack, &ft_lst_cfree);
ft_set_termios(data, 0);
return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:44:24 by jhalford #+# #+# */
/* Updated: 2016/12/07 18:12:29 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:40:27 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,6 @@ int ft_key_ctrl_d(t_data *data, char *buf)
{
(void)data;
(void)buf;
data_exit(data);
ft_putendl("exit");
exit(0);
}

View file

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

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 14:22:34 by jhalford #+# #+# */
/* Updated: 2016/11/28 15:16:30 by jhalford ### ########.fr */
/* Updated: 2016/12/12 16:49:05 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,17 @@
int ft_prompt(void)
{
t_data *data;
data = data_singleton();
while (data->jobc.list)
{
/* usleep(500 * 1000); */
if (check_chlds())
continue ;
else
break ;
}
ft_putstr(SHELL_PROMPT);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:14:09 by jhalford #+# #+# */
/* Updated: 2016/12/07 14:20:59 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:35:27 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 13:35:03 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:16:40 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:35:15 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,8 +22,9 @@ int input_init(t_data *data)
data->line.input_pos = 0;
data->line.qstack = ft_lstnew(NULL, sizeof(t_qstate));
*((t_qstate*)data->line.qstack->content) = Q_NONE;
ft_prompt();
if (ft_set_termios(data, 1))
return (-1);
ft_prompt();
data->mode = MODE_INPUT;
return (0);
}

View file

@ -6,14 +6,17 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */
/* Updated: 2016/12/07 18:12:34 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:39:54 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void data_exit(t_data *data)
void data_exit(void)
{
t_data *data;
data = data_singleton();
ft_strdel(&data->line.input);
ft_dlstdel(&data->line.history, &ft_lst_bfree);
ft_lstdel(&data->line.qstack, &ft_lst_cfree);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:58:36 by jhalford ### ########.fr */
/* Updated: 2016/12/12 17:50:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,11 +14,12 @@
extern char **environ;
int data_init(t_data *data)
int data_init(void)
{
char *term_name;
t_data *data;
atexit(&ft_cleanup);
data = data_singleton();
data->line.input = NULL;
data->env = ft_sstrdup(environ);
data->line.history = NULL;
@ -28,17 +29,14 @@ int data_init(t_data *data)
data->exec.aol_search = 0;
data->exec.amp = 0;
data->jobc.list = NULL;
data->jobc.current_id = 1;
data->jobc.rank[0] = 0;
data->jobc.rank[1] = 0;
if (!(data->line.history = ft_dlstnew(NULL, 0)))
return (-1);
if ((term_name = ft_getenv(data->env, "TERM")) == NULL)
return (-1);
if (tgetent(NULL, term_name) != 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)
ft_dprintf(STDERR, "\ncan't catch SIGCHLD\n");
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2016/12/10 17:58:39 by jhalford ### ########.fr */
/* Updated: 2016/12/12 18:11:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,14 +16,12 @@ int main(void)
{
t_list *token;
t_btree *ast;
/* t_data data; */
t_data *data;
token = NULL;
ast = NULL;
data = data_singleton();
if (data_init(data))
return (1);
shell_init();
DG("{inv}{bol}{gre}start of shell");
while (1)
{
@ -41,7 +39,7 @@ int main(void)
btree_print(STDERR, ast, &ft_putast);
/* ft_dprintf(STDERR, "\n--- INFIX BREAKDOWN ---\n"); */
/* btree_apply_infix(ast, &ft_putast2); */
if (ft_exec(&ast, data))
if (ft_exec(&ast))
return (1);
}
return (0);

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cleanup.c :+: :+: :+: */
/* shell_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 14:42:42 by jhalford #+# #+# */
/* Updated: 2016/12/09 21:50:38 by jhalford ### ########.fr */
/* Created: 2016/12/12 17:37:04 by jhalford #+# #+# */
/* Updated: 2016/12/12 17:51:20 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,15 +17,10 @@ extern char PC;
extern char *UP;
extern char *BC;
void ft_cleanup(void)
void shell_exit(void)
{
struct termios term;
DG("cleanup. char * UP at %p", UP);
DG("cleanup. char * BC at %p", BC);
if (tcgetattr(0, &term) == -1)
return ;
term.c_lflag |= ICANON | ISIG | ECHO;
if (tcsetattr(0, TCSANOW, &term) == -1)
return ;
data_exit();
tcsetattr(0, TCSANOW, &data_singleton()->jobc.shell_tmodes);
}

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* shell_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
/* Updated: 2016/12/12 18:11:50 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void shell_init(void)
{
int *shell_pgid;
shell_pgid = &data_singleton()->jobc.shell_pgid;
data_init();
atexit(&shell_exit);
if (isatty(STDIN_FILENO))
{
while (tcgetpgrp(STDIN_FILENO) != (*shell_pgid = getpgrp()))
kill(*shell_pgid, SIGTTIN);
signal(SIGINT, sigint_handler);
signal(SIGQUIT, SIG_IGN);
signal(SIGTSTP, sigtstp_handler);
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGCHLD, sigchld_handler);
*shell_pgid = getpid();
if (setpgid(*shell_pgid, *shell_pgid))
{
ft_dprintf(2, "Couldnt put the shell in it's own process group");
exit (1);
}
tcsetpgrp(STDIN_FILENO, *shell_pgid);
tcgetattr(STDIN_FILENO, &data_singleton()->jobc.shell_tmodes);
}
}