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/instruction_free.c\
main/main.c\
main/shell_get_opts.c\
main/shell_init.c\
parser/add_bang.c\
parser/add_case.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
{
t_flag opts;
char **env;
int argc;
char **argv;
t_flag opts;
t_line line;
t_comp *comp;
t_exec exec;
@ -47,11 +47,9 @@ struct s_data
t_list *lst_func;
};
void shell_get_opts(int ac, char **av);
char *shell_get_avdata();
void shell_init(int ac, char **av);
int shell_init(int ac, char **av);
void shell_exit(void);
int data_init(void);
int data_init(int ac, char **av);
void data_exit(void);
int instruction_free(t_list **token, t_parser *parser,

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

View file

@ -6,13 +6,13 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
t_cliopts opts[] =
static t_cliopts export_opts[] =
{
{'p', NULL, BT_EXPORT_LP, 0, NULL},
{0, NULL, 0, 0, NULL},
@ -35,7 +35,8 @@ int bt_export_print(void)
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;
t_btexport data;
@ -44,7 +45,8 @@ int builtin_export(const char *path, char *const av[], char *const envp[])
i = 0;
(void)envp;
(void)path;
if (cliopts_get((char**)av, opts, &data))
data.flag = 0;
if (cliopts_get((char**)av, export_opts, &data))
ft_perror();
if (data.flag & BT_EXPORT_LP)
return (bt_export_print());

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
{
DG("str3join(%s,%s,%s)", av[0], "=", av[1]);
str = ft_str3join(av[0], "=", av[1]);
while ((*env) && (*env)[i])
{

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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))
{
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);
}
else if (access(p->data.cmd.path, X_OK) == -1)

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
int data_init(void)
int data_init(int ac, char **av)
{
t_data *data;
char *term_name;
data = data_singleton();
data->argc = ac;
data->argv = ft_sstrdup(av);
data->env = ft_sstrdup(environ);
data->local_var = NULL;
set_exitstatus(0, 1);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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) ||
parser.state == UNDEFINED || lexer.state == HEREDOC, &str)))
{
DG("readline trap");
if (ret == -1)
return (-1);
return (parser.state == UNDEFINED ? error_eof(&token,
&parser, &ast) : 1);
}
DG("INPUT STRING IS [%s]", str);
if (lexer.state == HEREDOC)
{
ft_strappend(&lexer.str, (char[]){'\n', 0});
@ -62,7 +60,6 @@ int handle_instruction(int fd)
if (parser.state == ERROR)
error_syntax(&token, &parser, &ast);
lexer.state = data_singleton()->heredoc_queue ? HEREDOC : 0;
/* DG("lexer.state=%i", lexer.state); */
if (lexer.state)
continue;
else if (parser.state == SUCCESS)
@ -90,6 +87,7 @@ int get_input_fd(char **av)
char *file;
int fds[2];
int fd;
struct stat buf;
data = data_singleton();
if (SH_IS_INTERACTIVE(data->opts))
@ -98,21 +96,22 @@ int get_input_fd(char **av)
{
pipe(fds);
fd = fds[PIPE_READ];
/* file = shell_get_avdata(); */
file = *cliopts_getdata(av);
write(fds[PIPE_WRITE], file, ft_strlen(file));
close(fds[PIPE_WRITE]);
fcntl(fd, F_SETFD, FD_CLOEXEC);
return (fd);
}
/* else if ((file = shell_get_avdata())) */
else if ((file = *cliopts_getdata(av)))
{
if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0)
return (-1);
stat(file, &buf);
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);
}
else
return (STDIN);
}
@ -123,16 +122,13 @@ int main(int ac, char **av)
g_argv = av;
setlocale(LC_ALL, "");
DG("{inv}{bol}{gre}start of shell{eoc}");
shell_init(ac, av);
if ((fd = get_input_fd(av)) < 0)
{
ft_printf("{red}%s: %s: No such file or directory\n{eoc}", SHELL_NAME, *cliopts_getdata(av));
if (shell_init(ac, av))
return (1);
if ((fd = get_input_fd(av)) < 0)
return (1);
}
DG("JOBC is %s, fd=[%i]", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd);
while (handle_instruction(fd) == 0)
;
DG("gonna exit");
builtin_exit(NULL, NULL, NULL);
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,25 +6,25 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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;
t_data *data;
DG("check");
data_init();
data = data_singleton();
data->argc = ac;
data->argv = ft_sstrdup(av);
shell_get_opts(ac, av);
if (SH_IS_INTERACTIVE(data->opts))
{
DG("interactive shell settings");
shell_pgid = &data->jobc.shell_pgid;
while (tcgetpgrp(STDIN) != (*shell_pgid = getpgrp()))
@ -44,5 +44,28 @@ void shell_init(int ac, char **av)
}
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);
}