data_init and get_fd refactoring

This commit is contained in:
Jack Halford 2017-03-20 09:56:58 +01:00
parent e7310fae99
commit f482f78254
18 changed files with 117 additions and 60 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2017/03/16 20:24:46 by jhalford ### ########.fr */
/* Updated: 2017/03/20 09:46:31 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,9 +17,6 @@
# include "job_control.h"
# include <sys/stat.h>
# define PIPE_READ 0
# define PIPE_WRITE 1
# define PROCESS_COMPLETED (1 << 0)
# define PROCESS_SUSPENDED (1 << 1)
# define PROCESS_RUNNING (1 << 2)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/03/18 19:05:37 by ariard ### ########.fr */
/* Updated: 2017/03/20 09:37:28 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -42,6 +42,7 @@ struct s_data
{
t_flag opts;
char **av_data;
char *c_arg;
int fd;
char **env;
int argc;
@ -60,6 +61,7 @@ int shell_init(int ac, char **av);
void shell_exit(void);
int data_init(int ac, char **av);
void data_exit(void);
int get_c_arg(char ***av, t_data *data);
void content_free(void *data, size_t content_size);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:22:54 by jhalford #+# #+# */
/* Updated: 2017/03/14 18:07:10 by jhalford ### ########.fr */
/* Updated: 2017/03/20 09:23:06 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -42,6 +42,7 @@ struct s_fmt
char conversion;
int valid;
t_conv conv;
};
int ft_vdprintf(int fd, const char *format, va_list ap);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */
/* Updated: 2017/03/18 19:08:44 by ariard ### ########.fr */
/* Updated: 2017/03/20 09:37:38 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */
/* Updated: 2017/03/14 17:24:24 by jhalford ### ########.fr */
/* Updated: 2017/03/20 09:38:36 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,9 @@
# define STDERR STDERR_FILENO
# define STDBUG 3
# define PIPE_READ 0
# define PIPE_WRITE 1
# include <sys/types.h>
# include <sys/xattr.h>

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 20:04:04 by jhalford #+# #+# */
/* Updated: 2017/03/15 21:18:20 by jhalford ### ########.fr */
/* Updated: 2017/03/20 09:28:34 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:33:27 by jhalford #+# #+# */
/* Updated: 2017/03/17 21:33:37 by jhalford ### ########.fr */
/* Updated: 2017/03/20 09:22:14 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:33:24 by jhalford #+# #+# */
/* Updated: 2017/02/18 13:08:53 by jhalford ### ########.fr */
/* Updated: 2017/03/20 09:17:48 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,12 +6,25 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:33:32 by jhalford #+# #+# */
/* Updated: 2016/12/13 11:19:29 by jhalford ### ########.fr */
/* Updated: 2017/03/20 09:23:01 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
enum e_conversion =
{
CONV_D,
CONV_I,
CONV_U,
CONV_O,
CONV_x,
CONV_X,
CONV_B,
CONV_S,
CONV_C,
};
char *ft_transform(t_fmt *fmt, va_list ap)
{
char *ret;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/07 14:25:41 by jhalford #+# #+# */
/* Updated: 2017/03/07 14:27:58 by ariard ### ########.fr */
/* Updated: 2017/03/20 08:27:05 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/21 18:00:03 by jhalford #+# #+# */
/* Updated: 2017/03/15 21:15:40 by jhalford ### ########.fr */
/* Updated: 2017/03/20 08:41:18 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */
/* Updated: 2017/03/20 08:08:55 by jhalford ### ########.fr */
/* Updated: 2017/03/20 08:31:25 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -109,7 +109,7 @@ int builtin_read(const char *path, char *const av[], char *const envp[])
;
else if ((ret = bt_read_loop(&data)))
;
else if (bt_read_assign(&data))
else if (data.input && bt_read_assign(&data))
ret = 1;
if (ret == -1)
exit(1);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */
/* Updated: 2017/03/15 16:29:51 by wescande ### ########.fr */
/* Updated: 2017/03/20 08:24:10 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */
/* Updated: 2017/03/16 16:55:19 by jhalford ### ########.fr */
/* Updated: 2017/03/20 08:51:51 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
/* Updated: 2017/03/18 14:55:08 by gwojda ### ########.fr */
/* Updated: 2017/03/20 09:46:30 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,7 @@ int readline(int has_prompt, char **input)
{
int ret;
if (!has_prompt)
if (!has_prompt)
data_singleton()->line.prompt_size = 1;
if (!SH_IS_INTERACTIVE(data_singleton()->opts))
return ((ret = get_next_line(data_singleton()->fd, input)) >= 0 ? !ret : ret);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2017/03/19 16:49:29 by wescande ### ########.fr */
/* Updated: 2017/03/20 09:55:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,26 +14,49 @@
extern char **environ;
int data_init(int ac, char **av)
static int localenv_init(void)
{
t_data *data;
data = data_singleton();
data->local_var = NULL;
builtin_setenv(NULL, (char *[]){"local", "IFS", " \t\n", 0}, NULL);
builtin_setenv(NULL, (char *[]){"local", "PS2", " >", 0}, NULL);
return (0);
}
static int shlvl_inc(void)
{
t_data *data;
char *shlvl;
data = data_singleton();
if ((shlvl = ft_getenv(data->env, "SHLVL")))
{
if (!(shlvl = ft_itoa(ft_atoi(shlvl) + 1)))
return (-1);
}
else if (!(shlvl = ft_strdup("1")))
return (-1);
builtin_setenv(NULL, (char *[]){"setenv", "SHLVL", shlvl, 0}, NULL);
ft_strdel(&shlvl);
return (0);
}
int data_init(int ac, char **av)
{
t_data *data;
char *term_name;
char *shlvl;
data = data_singleton();
data->argc = ac;
data->argv = ft_sstrdup(av);
data->env = ft_sstrdup(environ);
data->local_var = NULL;
builtin_setenv(NULL, (char *[]){"local", "IFS", "\n ", 0}, NULL);
data->c_arg = NULL;
set_exitstatus(0, 1);
shlvl = ft_getenv(data->env, "SHLVL");
if (shlvl)
shlvl = ft_itoa(ft_atoi(shlvl) + 1);
else
shlvl = ft_strdup("1");
builtin_setenv(NULL, (char *[]){"setenv", "SHLVL", shlvl, 0}, NULL);
ft_strdel(&shlvl);
localenv_init();
if (shlvl_inc())
return (-1);
data->comp = NULL;
data->opts = SH_INTERACTIVE | SH_OPTS_JOBC;
exec_reset();

View file

@ -65,7 +65,7 @@ int main(int ac, char **av)
g_argv = av;
setlocale(LC_ALL, "");
DG("{inv}{bol}{gre}start of shell{eoc}");
if (shell_init(ac, av))
if (shell_init(ac, av) != 0)
return (1);
DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF");
token = NULL;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
/* Updated: 2017/03/19 16:50:42 by wescande ### ########.fr */
/* Updated: 2017/03/20 09:54:18 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,43 +14,47 @@
static t_cliopts g_opts[] =
{
{'c', NULL, SH_OPTS_LC, SH_OPTS_JOBC | SH_INTERACTIVE, NULL},
{'c', NULL, SH_OPTS_LC, SH_OPTS_JOBC | SH_INTERACTIVE, get_c_arg},
{-1, "no-jobcontrol", 0, SH_OPTS_JOBC, NULL},
{0, 0, 0, 0, 0},
};
int get_input_fd(char **av)
int get_c_arg(char ***av, t_data *data)
{
t_data *data;
if (!av || !*av)
return (1);
if (data)
data->c_arg = **av;
return (0);
}
static int get_input_fd(t_data *data)
{
static int fds[2] = {-1, STDIN};
char *file;
int fds[2];
int fd;
struct stat buf;
data = data_singleton();
file = *data->av_data;
if (SH_IS_INTERACTIVE(data->opts))
return (STDIN);
else if (data->opts & SH_OPTS_LC)
/* fds = (int[2]){-1, STDIN}; */
if (data->opts & SH_OPTS_LC)
{
DG("-c");
file = data->c_arg;
pipe(fds);
write(fds[PIPE_WRITE], file, ft_strlen(file));
close(fds[PIPE_WRITE]);
dup2_close(fds[PIPE_READ], (fd = 10));
fcntl(fd, F_SETFD, FD_CLOEXEC);
return (fd);
}
else if (file && !stat(file, &buf))
else if ((file = *data->av_data) && !stat(file, &buf))
{
fd = -1;
if (S_ISDIR(buf.st_mode))
ft_printf("{red}%s: %s: is a directory\n{eoc}", av[0], file);
else if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0)
ft_printf("{red}%s: %s: No such file or directory\n{eoc}", av[0], file);
if (fd > 0 && !dup2_close(fd, 10) && (fd = 10))
return (fd);
ft_printf("{red}%s: %s: is a directory\n{eoc}", data->argv[0], file);
else if ((fds[PIPE_READ] = open(file, O_RDONLY | O_CLOEXEC)) < 0)
ft_printf("{red}%s: %s: No such file or directory\n{eoc}",
data->argv[0], file);
}
return (STDIN);
fds[PIPE_WRITE] = fds[PIPE_READ] != -1 ?
fcntl(fds[PIPE_READ], F_DUPFD_CLOEXEC, 10) : STDIN;
close(fds[PIPE_READ]);
return (fds[PIPE_WRITE]);
}
static int interactive_settings(void)
@ -73,25 +77,39 @@ static int interactive_settings(void)
{
ft_dprintf(2,
"{red}Couldnt put the shell in it's own process group{eoc}\n");
exit(1);
return (-1);
}
tcsetpgrp(STDIN, *shell_pgid);
tcgetattr(STDIN, &data->jobc.shell_tmodes);
return (0);
}
static int usage(void)
{
ft_dprintf(2, "usage: 42sh [-c command | [<]script] [--no-jobcontrol]\n");
return (0);
}
int shell_init(int ac, char **av)
{
t_data *data;
data = data_singleton();
data_init(ac, av);
DG();
if (data_init(ac, av) < 0)
return (-1);
DG();
if (cliopts_get(av, g_opts, data))
{
usage();
return (ft_perror());
}
DG();
if (!isatty(STDIN) || *data->av_data)
data->opts &= ~(SH_INTERACTIVE | SH_OPTS_JOBC);
data->fd = get_input_fd(av);
if (SH_IS_INTERACTIVE(data->opts))
interactive_settings();
if ((data->fd = get_input_fd(data)) < 0)
return (-1);
if (SH_IS_INTERACTIVE(data->opts) && interactive_settings() < 0)
return (-1);
return (0);
}