no more custom parsing, all is done w/ cliopts

This commit is contained in:
Jack Halford 2017-03-14 21:51:40 +01:00
parent bb9796f758
commit c6a23fb2ac
13 changed files with 88 additions and 130 deletions

View file

@ -270,7 +270,6 @@ main/data_singleton.c\
main/ft_putast.c\ main/ft_putast.c\
main/instruction_free.c\ main/instruction_free.c\
main/main.c\ main/main.c\
main/shell_get_opts.c\
main/shell_init.c\ main/shell_init.c\
parser/add_bang.c\ parser/add_bang.c\
parser/add_case.c\ parser/add_case.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2017/03/14 20:34:31 by jhalford ### ########.fr */ /* Updated: 2017/03/14 21:17:31 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/03/14 19:58:37 by jhalford ### ########.fr */ /* Updated: 2017/03/14 21:34:13 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,10 +34,10 @@
struct s_data struct s_data
{ {
t_flag opts;
char **env; char **env;
int argc; int argc;
char **argv; char **argv;
t_flag opts;
t_line line; t_line line;
t_comp *comp; t_comp *comp;
t_exec exec; t_exec exec;
@ -47,11 +47,9 @@ struct s_data
t_list *lst_func; t_list *lst_func;
}; };
void shell_get_opts(int ac, char **av); int shell_init(int ac, char **av);
char *shell_get_avdata();
void shell_init(int ac, char **av);
void shell_exit(void); void shell_exit(void);
int data_init(void); int data_init(int ac, char **av);
void data_exit(void); void data_exit(void);
int instruction_free(t_list **token, t_parser *parser, int instruction_free(t_list **token, t_parser *parser,

@ -1 +1 @@
Subproject commit 2713f7ab295d895824b22ac39524f85a7e5b7985 Subproject commit 938a355bac5f6d4c7941968d3ef0da0d984a9329

View file

@ -6,19 +6,19 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */ /* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */
/* Updated: 2017/03/14 21:10:37 by jhalford ### ########.fr */ /* Updated: 2017/03/14 21:39:12 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_cliopts opts[] = static t_cliopts export_opts[] =
{ {
{'p', NULL, BT_EXPORT_LP, 0, NULL}, {'p', NULL, BT_EXPORT_LP, 0, NULL},
{0, NULL, 0, 0, NULL}, {0, NULL, 0, 0, NULL},
}; };
int bt_export_print(void) int bt_export_print(void)
{ {
char **env; char **env;
char *equal; char *equal;
@ -35,7 +35,8 @@ int bt_export_print(void)
return (0); return (0);
} }
int builtin_export(const char *path, char *const av[], char *const envp[]) int builtin_export(
const char *path, char *const av[], char *const envp[])
{ {
char *equal; char *equal;
t_btexport data; t_btexport data;
@ -44,7 +45,8 @@ int builtin_export(const char *path, char *const av[], char *const envp[])
i = 0; i = 0;
(void)envp; (void)envp;
(void)path; (void)path;
if (cliopts_get((char**)av, opts, &data)) data.flag = 0;
if (cliopts_get((char**)av, export_opts, &data))
ft_perror(); ft_perror();
if (data.flag & BT_EXPORT_LP) if (data.flag & BT_EXPORT_LP)
return (bt_export_print()); return (bt_export_print());

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */
/* Updated: 2017/03/14 21:11:41 by jhalford ### ########.fr */ /* Updated: 2017/03/14 21:40:07 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,6 @@ int builtin_setenv(const char *path, char *const av[], char *const envp[])
} }
else else
{ {
DG("str3join(%s,%s,%s)", av[0], "=", av[1]);
str = ft_str3join(av[0], "=", av[1]); str = ft_str3join(av[0], "=", av[1]);
while ((*env) && (*env)[i]) while ((*env) && (*env)[i])
{ {

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:29:17 by jhalford #+# #+# */ /* Created: 2016/11/28 14:29:17 by jhalford #+# #+# */
/* Updated: 2017/03/14 21:08:12 by jhalford ### ########.fr */ /* Updated: 2017/03/14 21:13:35 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */
/* Updated: 2017/03/13 23:15:06 by jhalford ### ########.fr */ /* Updated: 2017/03/14 21:48:36 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -40,7 +40,7 @@ int plaunch_file(t_process *p)
} }
else if (S_ISDIR(p->data.cmd.stat->st_mode)) else if (S_ISDIR(p->data.cmd.stat->st_mode))
{ {
ft_dprintf(2, "{red}%s: %s: Is a directory{eoc}\n", SHELL_NAME, p->data.cmd.av[0]); ft_dprintf(2, "{red}%s: %s: is a directory{eoc}\n", SHELL_NAME, p->data.cmd.av[0]);
exit(126); exit(126);
} }
else if (access(p->data.cmd.path, X_OK) == -1) else if (access(p->data.cmd.path, X_OK) == -1)

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
/* Updated: 2017/03/13 14:43:28 by jhalford ### ########.fr */ /* Updated: 2017/03/14 21:34:46 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2017/03/13 23:48:04 by ariard ### ########.fr */ /* Updated: 2017/03/14 21:39:13 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,12 +14,14 @@
extern char **environ; extern char **environ;
int data_init(void) int data_init(int ac, char **av)
{ {
t_data *data; t_data *data;
char *term_name; char *term_name;
data = data_singleton(); data = data_singleton();
data->argc = ac;
data->argv = ft_sstrdup(av);
data->env = ft_sstrdup(environ); data->env = ft_sstrdup(environ);
data->local_var = NULL; data->local_var = NULL;
set_exitstatus(0, 1); set_exitstatus(0, 1);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/03/14 21:11:58 by jhalford ### ########.fr */ /* Updated: 2017/03/14 21:50:56 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,13 +31,11 @@ int handle_instruction(int fd)
if ((ret = readline(fd, get_lexer_stack(lexer) || if ((ret = readline(fd, get_lexer_stack(lexer) ||
parser.state == UNDEFINED || lexer.state == HEREDOC, &str))) parser.state == UNDEFINED || lexer.state == HEREDOC, &str)))
{ {
DG("readline trap");
if (ret == -1) if (ret == -1)
return (-1); return (-1);
return (parser.state == UNDEFINED ? error_eof(&token, return (parser.state == UNDEFINED ? error_eof(&token,
&parser, &ast) : 1); &parser, &ast) : 1);
} }
DG("INPUT STRING IS [%s]", str);
if (lexer.state == HEREDOC) if (lexer.state == HEREDOC)
{ {
ft_strappend(&lexer.str, (char[]){'\n', 0}); ft_strappend(&lexer.str, (char[]){'\n', 0});
@ -62,7 +60,6 @@ int handle_instruction(int fd)
if (parser.state == ERROR) if (parser.state == ERROR)
error_syntax(&token, &parser, &ast); error_syntax(&token, &parser, &ast);
lexer.state = data_singleton()->heredoc_queue ? HEREDOC : 0; lexer.state = data_singleton()->heredoc_queue ? HEREDOC : 0;
/* DG("lexer.state=%i", lexer.state); */
if (lexer.state) if (lexer.state)
continue; continue;
else if (parser.state == SUCCESS) else if (parser.state == SUCCESS)
@ -86,10 +83,11 @@ int handle_instruction(int fd)
int get_input_fd(char **av) int get_input_fd(char **av)
{ {
t_data *data; t_data *data;
char *file; char *file;
int fds[2]; int fds[2];
int fd; int fd;
struct stat buf;
data = data_singleton(); data = data_singleton();
if (SH_IS_INTERACTIVE(data->opts)) if (SH_IS_INTERACTIVE(data->opts))
@ -98,22 +96,23 @@ int get_input_fd(char **av)
{ {
pipe(fds); pipe(fds);
fd = fds[PIPE_READ]; fd = fds[PIPE_READ];
/* file = shell_get_avdata(); */
file = *cliopts_getdata(av); file = *cliopts_getdata(av);
write(fds[PIPE_WRITE], file, ft_strlen(file)); write(fds[PIPE_WRITE], file, ft_strlen(file));
close(fds[PIPE_WRITE]); close(fds[PIPE_WRITE]);
fcntl(fd, F_SETFD, FD_CLOEXEC); fcntl(fd, F_SETFD, FD_CLOEXEC);
return (fd); return (fd);
} }
/* else if ((file = shell_get_avdata())) */
else if ((file = *cliopts_getdata(av))) else if ((file = *cliopts_getdata(av)))
{ {
if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0) stat(file, &buf);
return (-1); fd = -1;
if (S_ISDIR(buf.st_mode))
ft_printf("{red}%s: %s: is a directory\n{eoc}", g_argv[0], file);
else if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0)
ft_printf("{red}%s: %s: No such file or directory\n{eoc}", g_argv[0], file);
return (fd); return (fd);
} }
else return (STDIN);
return (STDIN);
} }
int main(int ac, char **av) int main(int ac, char **av)
@ -123,16 +122,13 @@ int main(int ac, char **av)
g_argv = av; g_argv = av;
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
DG("{inv}{bol}{gre}start of shell{eoc}"); DG("{inv}{bol}{gre}start of shell{eoc}");
shell_init(ac, av); if (shell_init(ac, av))
if ((fd = get_input_fd(av)) < 0) return (1);
{ if ((fd = get_input_fd(av)) < 0)
ft_printf("{red}%s: %s: No such file or directory\n{eoc}", SHELL_NAME, *cliopts_getdata(av));
return (1); return (1);
}
DG("JOBC is %s, fd=[%i]", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd); DG("JOBC is %s, fd=[%i]", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd);
while (handle_instruction(fd) == 0) while (handle_instruction(fd) == 0)
; ;
DG("gonna exit");
builtin_exit(NULL, NULL, NULL); builtin_exit(NULL, NULL, NULL);
return (0); return (0);
} }

View file

@ -1,61 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* shell_get_opts.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 14:04:48 by jhalford #+# #+# */
/* Updated: 2017/03/06 16:52:34 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void shell_parse_long_opt(char *str)
{
if (ft_strcmp("no-jobcontrol", str) == 0)
data_singleton()->opts &= ~SH_OPTS_JOBC;
}
static void shell_parse_short_opt(char *str)
{
int i;
i = 0;
while (str[i])
{
if (*str == 'c')
{
data_singleton()->opts |= SH_OPTS_LC;
data_singleton()->opts &= ~SH_OPTS_JOBC;
data_singleton()->opts &= ~SH_INTERACTIVE;
}
i++;
}
}
void shell_get_opts(int ac, char **av)
{
int i;
i = 1;
if (isatty(STDIN) && !av[1])
{
data_singleton()->opts |= SH_INTERACTIVE;
data_singleton()->opts |= SH_OPTS_JOBC;
}
while (i < ac && av[i][0] == '-')
{
if (ft_strcmp(av[i], "--") == 0)
{
i++;
break ;
}
if (av[i][1] == '-')
shell_parse_long_opt(av[i] + 2);
else
shell_parse_short_opt(av[i] + 1);
i++;
}
}

View file

@ -6,43 +6,66 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
/* Updated: 2017/03/14 21:10:26 by jhalford ### ########.fr */ /* Updated: 2017/03/14 21:50:11 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void shell_init(int ac, char **av) static t_cliopts shell_opts[] =
{
{'c', NULL, SH_OPTS_LC, SH_OPTS_JOBC | SH_INTERACTIVE, NULL},
{-1, "no-jobcontrol", 0, SH_OPTS_JOBC, NULL},
{0, 0, 0, 0, 0},
};
static int interactive_settings(void)
{ {
int *shell_pgid; int *shell_pgid;
t_data *data; t_data *data;
DG("check");
data_init();
data = data_singleton(); data = data_singleton();
data->argc = ac; DG("interactive shell settings");
data->argv = ft_sstrdup(av); shell_pgid = &data->jobc.shell_pgid;
shell_get_opts(ac, av); while (tcgetpgrp(STDIN) != (*shell_pgid = getpgrp()))
if (SH_IS_INTERACTIVE(data->opts)) 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, SIG_DFL);
signal(SIGWINCH, sigwinch_resize);
*shell_pgid = getpid();
if (setpgid(*shell_pgid, *shell_pgid))
{ {
DG("interactive shell settings"); ft_dprintf(2, "Couldnt put the shell in it's own process group");
shell_pgid = &data->jobc.shell_pgid; exit (1);
while (tcgetpgrp(STDIN) != (*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, SIG_DFL);
signal(SIGWINCH, sigwinch_resize);
*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, *shell_pgid);
tcgetattr(STDIN, &data->jobc.shell_tmodes);
} }
tcsetpgrp(STDIN, *shell_pgid);
tcgetattr(STDIN, &data->jobc.shell_tmodes);
return (0);
}
int shell_init(int ac, char **av)
{
t_data *data;
data = data_singleton();
data_init(ac, av);
if (isatty(STDIN) && !*cliopts_getdata(av))
{
DG("interactive");
data->opts |= SH_INTERACTIVE;
data->opts |= SH_OPTS_JOBC;
}
else
DG("non interactive");
if (cliopts_get(av, shell_opts, data))
return (ft_perror());
if (SH_IS_INTERACTIVE(data->opts))
interactive_settings();
else
DG("not interactive");
return (0);
} }