Merge branch 'master' of github.com:jzck/42sh

This commit is contained in:
wescande 2017-03-19 17:45:32 +01:00
commit ac27fb4ce1
17 changed files with 75 additions and 110 deletions

View file

@ -6,14 +6,14 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ # # By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 by wescande #+# #+# # # Created: 2016/08/29 21:32:58 by wescande #+# #+# #
# Updated: 2017/03/18 14:17:54 by gwojda ### ########.fr # # Updated: 2017/03/19 15:14:11 by gwojda ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
NAME = 42sh NAME = 42sh
CC = gcc CC = gcc
FLAGS = -Wall -Wextra -Werror -fvisibility=hidden FLAGS = -Wall -Wextra -Werror -g
D_FLAGS = -g D_FLAGS = -g
DELTA = $$(echo "$$(tput cols)-47"|bc) DELTA = $$(echo "$$(tput cols)-47"|bc)

View file

@ -13,10 +13,6 @@
#ifndef BUILTIN_READ_H #ifndef BUILTIN_READ_H
# define BUILTIN_READ_H # define BUILTIN_READ_H
/* # include "types.h" */
/* # include "builtin.h" */
/* # include "minishell.h" */
# define READ_OPT_LA (1 << 0) # define READ_OPT_LA (1 << 0)
# define READ_OPT_LD (1 << 1) # define READ_OPT_LD (1 << 1)
# define READ_OPT_LE (1 << 2) # define READ_OPT_LE (1 << 2)
@ -35,12 +31,12 @@ typedef struct s_readopt t_readopt;
struct s_read struct s_read
{ {
t_flag opts; t_flag opts;
char **names;
char delim; char delim;
int nchars; int nchars;
char *prompt; char *prompt;
int timeout; int timeout;
int fd; int fd;
char **names;
char *input; char *input;
}; };

View file

@ -41,6 +41,7 @@
struct s_data struct s_data
{ {
t_flag opts; t_flag opts;
char **av_data;
int fd; int fd;
char **env; char **env;
int argc; int argc;

View file

@ -49,7 +49,6 @@ char/ft_isprint.c\
char/ft_tolower.c\ char/ft_tolower.c\
char/ft_toupper.c\ char/ft_toupper.c\
cliopts/cliopts_get.c\ cliopts/cliopts_get.c\
cliopts/cliopts_getdata.c\
cliopts/cliopts_has.c\ cliopts/cliopts_has.c\
color/ft_color_mk.c\ color/ft_color_mk.c\
color/ft_color_mkif.c\ color/ft_color_mkif.c\

View file

@ -32,10 +32,10 @@ struct s_cliopts
struct s_data_template struct s_data_template
{ {
t_flag flag; t_flag flag;
char **av_data;
}; };
int cliopts_get(char **av, t_cliopts opt_map[], void *data); int cliopts_get(char **av, t_cliopts opt_map[], void *data);
char **cliopts_getdata(char **av);
int cliopts_has(char **av, char c); int cliopts_has(char **av, char c);
#endif #endif

View file

@ -58,7 +58,6 @@ static int cliopts_parse_short(char ***av, t_cliopts opt_map[], void *data)
++(*av); ++(*av);
if ((map->get)(av, data)) if ((map->get)(av, data))
return (ERR_SET(E_CO_MISS, *arg)); return (ERR_SET(E_CO_MISS, *arg));
break;
} }
((t_data_template*)data)->flag |= map->flag_on; ((t_data_template*)data)->flag |= map->flag_on;
((t_data_template*)data)->flag &= ~map->flag_off; ((t_data_template*)data)->flag &= ~map->flag_off;
@ -66,7 +65,6 @@ static int cliopts_parse_short(char ***av, t_cliopts opt_map[], void *data)
} }
++(*av); ++(*av);
return (0); return (0);
} }
static int cliopts_parse_long(char ***av, t_cliopts opt_map[], void *data) static int cliopts_parse_long(char ***av, t_cliopts opt_map[], void *data)
@ -109,7 +107,8 @@ int cliopts_get(char **av, t_cliopts opt_map[], void *data)
return (1); return (1);
} }
else else
return (0); break ;
} }
((t_data_template*)data)->av_data = av;
return (0); return (0);
} }

View file

@ -1,32 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cliopts_getdata.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 20:35:15 by jhalford #+# #+# */
/* Updated: 2017/03/14 21:35:19 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "cliopts.h"
char **cliopts_getdata(char **av)
{
if (!av)
return (NULL);
av++;
while (*av)
{
if (ft_strcmp(*av, "--") == 0)
return (av + 1);
else if ((*av)[0] == '-' && (*av)[1] == '-')
av++;
else if ((*av)[0] == '-')
av++;
else
return (av);
}
return (av);
}

View file

@ -16,6 +16,7 @@ int bt_read_getdelim(char ***av, t_read *data)
{ {
if (!av || !*av) if (!av || !*av)
return (1); return (1);
if (data)
data->delim = ***av; data->delim = ***av;
return (0); return (0);
} }
@ -24,17 +25,17 @@ int bt_read_getnchars(char ***av, t_read *data)
{ {
if (!av || !*av) if (!av || !*av)
return (1); return (1);
if (data)
data->nchars = ft_atoi(**av); data->nchars = ft_atoi(**av);
return (0); return (0);
} }
int bt_read_getprompt(char ***av, t_read *data) int bt_read_getprompt(char ***av, t_read *data)
{ {
DG("getting prompt");
if (!av || !*av || !**av) if (!av || !*av || !**av)
return (1); return (1);
if (data)
data->prompt = **av; data->prompt = **av;
DG("got prompt [%s]", data->prompt);
return (0); return (0);
} }
@ -42,6 +43,7 @@ int bt_read_gettimeout(char ***av, t_read *data)
{ {
if (!av || !*av) if (!av || !*av)
return (1); return (1);
if (data)
data->timeout = ft_atoi(**av); data->timeout = ft_atoi(**av);
return (0); return (0);
} }
@ -50,6 +52,7 @@ int bt_read_getfd(char ***av, t_read *data)
{ {
if (!av || !*av) if (!av || !*av)
return (1); return (1);
if (data)
data->fd = ft_atoi(**av); data->fd = ft_atoi(**av);
return (0); return (0);
} }

View file

@ -27,17 +27,13 @@ int bt_read_terminit(t_read *data)
(void)data; (void)data;
term = bt_read_term(1); term = bt_read_term(1);
term.c_lflag &= ~(ICANON | ECHO); term.c_lflag = ECHO | ECHOE | ECHOK | ICANON;
if (data->opts & READ_OPT_LT) term.c_lflag &= data->timeout ? ~ICANON : ~0;
{ if (data->opts & READ_OPT_LS)
term.c_lflag &= ~ECHO;
term.c_cc[VTIME] = data->timeout * 10; term.c_cc[VTIME] = data->timeout * 10;
term.c_cc[VMIN] = 0; term.c_cc[VMIN] = !data->timeout;
} term.c_cc[VEOL] = data->delim;
else
{
term.c_cc[VTIME] = 0;
term.c_cc[VMIN] = 1;
}
if (tcsetattr(0, TCSANOW, &term) < 0) if (tcsetattr(0, TCSANOW, &term) < 0)
return (-1); return (-1);
return (0); return (0);

View file

@ -48,7 +48,7 @@ int builtin_export(
ft_perror(); ft_perror();
if (data.flag & BT_EXPORT_LP) if (data.flag & BT_EXPORT_LP)
return (bt_export_print()); return (bt_export_print());
av = cliopts_getdata((char**)av); av = data.av_data;
while (*av) while (*av)
{ {
if ((equal = ft_strchr(*av, '='))) if ((equal = ft_strchr(*av, '=')))

View file

@ -38,18 +38,18 @@ int bt_read_init(t_read *data, char **av)
data->fd = 0; data->fd = 0;
data->timeout = 0; data->timeout = 0;
data->input = NULL; data->input = NULL;
data->names = NULL;
if ((cliopts_get(av, g_read_opts, data))) if ((cliopts_get(av, g_read_opts, data)))
return(ft_perror()); return(ft_perror());
if (data->names) DG("opts=%b", data->opts);
DG("%s,%s", data->names[0], data->names[1]);
bt_read_terminit(data); bt_read_terminit(data);
return (0); return (0);
} }
int bt_read_loop(t_read *data) int bt_read_loop(t_read *data)
{ {
int i; int i;
int esc; int esc;
int ret;
char buf[2]; char buf[2];
esc = 0; esc = 0;
@ -58,16 +58,15 @@ int bt_read_loop(t_read *data)
ft_printf(data->prompt); ft_printf(data->prompt);
while (42) while (42)
{ {
if (read(data->fd, buf, 1) <= 0) if ((ret = read(data->fd, buf, 1)) <= 0)
return (1); return (ret);
buf[1] = 0; buf[ret] = 0;
if (!(data->opts &READ_OPT_LS)) DG("read [%c]", *buf);
ft_putchar(*buf);
if (!esc && *buf == data->delim) if (!esc && *buf == data->delim)
break ; break ;
esc = esc ? 0 : !(data->opts & READ_OPT_LR) && (*buf == '\\'); esc = esc ? 0 : !(data->opts & READ_OPT_LR) && (*buf == '\\');
ft_strappend(&data->input, buf); ft_strappend(&data->input, buf);
if (*buf == '\n' && !(data->opts & READ_OPT_LR)) if (*buf == '\n' && !(data->opts & (READ_OPT_LR | READ_OPT_LS)))
ft_putstr("> "); ft_putstr("> ");
if ((data->opts & READ_OPT_LN) && ++i >= data->nchars) if ((data->opts & READ_OPT_LN) && ++i >= data->nchars)
break ; break ;
@ -80,25 +79,22 @@ int bt_read_assign(t_read *data)
char *input; char *input;
char **names; char **names;
char *ifs; char *ifs;
char *start; char *tok;
DG("assigning");
input = data->input; input = data->input;
names = data->names ? data->names : (char*[]){"REPLY", NULL}; names = *data->names ? data->names : (char*[]){"REPLY", NULL};
ifs = ft_getenv(data_singleton()->env, "IFS"); ifs = ft_getenv(data_singleton()->local_var, "IFS");
start = input; if (!names[1])
while (*start && *names) ifs = NULL;
tok = ft_strtok(input, ifs);
while (*names)
{ {
if (!(names[1]) || !ifs) DG("tok=%s", tok);
{ DG("name=%s", *names);
builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL); builtin_setenv("setenv", (char*[]){"setenv", *names, tok}, NULL);
break ; ifs = names[1] ? ifs : NULL;
} tok = ft_strtok(NULL, ifs);
while (*input && !ft_strchr(ifs, *input))
input++;
while (input && ft_strchr(ifs, *input))
*(input++) = 0;
builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL);
start = input;
names++; names++;
} }
return (0); return (0);
@ -114,10 +110,12 @@ int builtin_read(const char *path, char *const av[], char *const envp[])
ret = 0; ret = 0;
if (bt_read_init(&data, (char **)av)) if (bt_read_init(&data, (char **)av))
ret = 2; ret = 2;
else if (bt_read_loop(&data)) else if ((ret = bt_read_loop(&data)))
ret = 1; ;
else if (bt_read_assign(&data)) else if (bt_read_assign(&data))
ret = 1; ret = 1;
if (ret == -1)
exit(1);
if (ret != 0) if (ret != 0)
bt_read_usage(); bt_read_usage();
if (ret != 2) if (ret != 2)

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/09 16:54:59 by gwojda #+# #+# */ /* Created: 2017/03/09 16:54:59 by gwojda #+# #+# */
/* Updated: 2017/03/18 14:04:24 by gwojda ### ########.fr */ /* Updated: 2017/03/19 15:22:06 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,6 +24,8 @@ void c_seek_abs_path(t_comp *c, char *current_word)
len = ft_strrchr(c->rcmd, '/') - current_word + 1; len = ft_strrchr(c->rcmd, '/') - current_word + 1;
if (len < 0) if (len < 0)
return ; return ;
if (c->cpath)
ft_strdel(&c->cpath);
c->cpath = ft_strndup(current_word, len); c->cpath = ft_strndup(current_word, len);
if (current_word[0] == '~') if (current_word[0] == '~')
{ {

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */ /* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/10/15 13:27:14 by alao #+# #+# */ /* Created: 2016/10/15 13:27:14 by alao #+# #+# */
/* Updated: 2017/03/16 10:20:44 by gwojda ### ########.fr */ /* Updated: 2017/03/19 15:34:38 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,6 +21,8 @@ static int c_chevron(t_comp *c)
size_t pos; size_t pos;
pos = c->ircmd; pos = c->ircmd;
if (pos >= ft_strlen(c->rcmd))
pos = ft_strlen(c->rcmd) - (ft_strlen(data_singleton()->line.input) - pos);
while (pos) while (pos)
{ {
if (c->rcmd[pos] == '<' || c->rcmd[pos] == '>') if (c->rcmd[pos] == '<' || c->rcmd[pos] == '>')
@ -38,9 +40,12 @@ static int c_chevron(t_comp *c)
static char *c_current_words(t_comp *c) static char *c_current_words(t_comp *c)
{ {
int pos; size_t pos;
pos = c->ircmd; pos = c->ircmd;
if (pos >= ft_strlen(c->rcmd))
pos = ft_strlen(c->rcmd) -
(ft_strlen(data_singleton()->line.input) - pos + 1);
while (pos && c->rcmd[pos] != ' ') while (pos && c->rcmd[pos] != ' ')
--pos; --pos;
if (c->rcmd[pos] == ' ') if (c->rcmd[pos] == ' ')
@ -57,6 +62,7 @@ int c_matching(t_data *s, t_comp *c)
char *current_word; char *current_word;
current_word = c_current_words(c); current_word = c_current_words(c);
DG("current_word = %s", current_word);
if (ft_strchr(c->rcmd, '/')) if (ft_strchr(c->rcmd, '/'))
c_seek_abs_path(c, current_word); c_seek_abs_path(c, current_word);
else if (ft_strchr(c->rcmd, '$')) else if (ft_strchr(c->rcmd, '$'))

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */ /* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 12:03:30 by alao #+# #+# */ /* Created: 2017/02/15 12:03:30 by alao #+# #+# */
/* Updated: 2017/03/17 12:03:26 by gwojda ### ########.fr */ /* Updated: 2017/03/19 15:28:50 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */ /* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/03 13:10:38 by alao #+# #+# */ /* Created: 2017/02/03 13:10:38 by alao #+# #+# */
/* Updated: 2017/03/17 16:48:49 by gwojda ### ########.fr */ /* Updated: 2017/03/19 15:18:55 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -105,12 +105,6 @@ int c_keypress(t_comp *c, long int key)
{ {
t_clst *ptr; t_clst *ptr;
if (key == 27 || key == 127 || key == 2117294875)
{
c_term_clear(c);
c_clear(data_singleton());
return (0);
}
if (key == 10 || key == 32) if (key == 10 || key == 32)
{ {
ptr = c->lst; ptr = c->lst;
@ -119,10 +113,16 @@ int c_keypress(t_comp *c, long int key)
c_updater(c, ptr->name); c_updater(c, ptr->name);
return (1); return (1);
} }
if (key == KP_U || key == KP_D || key == KP_L || key == KP_R) else if (key == KP_U || key == KP_D || key == KP_L || key == KP_R)
{ {
c_arrow(c, key); c_arrow(c, key);
return (0); return (0);
} }
else if (!ft_isprint(key))
{
c_term_clear(c);
c_clear(data_singleton());
return (0);
}
return ((c_rematch(c, key)) ? (0) : (1)); return ((c_rematch(c, key)) ? (0) : (1));
} }

View file

@ -35,7 +35,7 @@ int data_init(int ac, char **av)
builtin_setenv(NULL, (char *[]){"setenv", "SHLVL", shlvl, 0}, NULL); builtin_setenv(NULL, (char *[]){"setenv", "SHLVL", shlvl, 0}, NULL);
ft_strdel(&shlvl); ft_strdel(&shlvl);
data->comp = NULL; data->comp = NULL;
data->opts = 0; data->opts = SH_INTERACTIVE | SH_OPTS_JOBC;
exec_reset(); exec_reset();
data->lst_func = NULL; data->lst_func = NULL;
lexer_init(&data->lexer); lexer_init(&data->lexer);

View file

@ -28,19 +28,19 @@ int get_input_fd(char **av)
struct stat buf; struct stat buf;
data = data_singleton(); data = data_singleton();
file = *data->av_data;
if (SH_IS_INTERACTIVE(data->opts)) if (SH_IS_INTERACTIVE(data->opts))
return (STDIN); return (STDIN);
else if (data->opts & SH_OPTS_LC) else if (data->opts & SH_OPTS_LC)
{ {
pipe(fds); pipe(fds);
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]);
dup2_close(fds[PIPE_READ], (fd = 10)); dup2_close(fds[PIPE_READ], (fd = 10));
fcntl(fd, F_SETFD, FD_CLOEXEC); fcntl(fd, F_SETFD, FD_CLOEXEC);
return (fd); return (fd);
} }
else if ((file = *cliopts_getdata(av)) && !stat(file, &buf)) else if (file && !stat(file, &buf))
{ {
fd = -1; fd = -1;
if (S_ISDIR(buf.st_mode)) if (S_ISDIR(buf.st_mode))
@ -86,13 +86,10 @@ int shell_init(int ac, char **av)
data = data_singleton(); data = data_singleton();
data_init(ac, av); data_init(ac, av);
if (isatty(STDIN) && !*cliopts_getdata(av))
{
data->opts |= SH_INTERACTIVE;
data->opts |= SH_OPTS_JOBC;
}
if (cliopts_get(av, g_opts, data)) if (cliopts_get(av, g_opts, data))
return (ft_perror()); return (ft_perror());
if (!isatty(STDIN) || *data->av_data)
data->opts &= ~(SH_INTERACTIVE | SH_OPTS_JOBC);
data->fd = get_input_fd(av); data->fd = get_input_fd(av);
if (SH_IS_INTERACTIVE(data->opts)) if (SH_IS_INTERACTIVE(data->opts))
interactive_settings(); interactive_settings();