Merge branch 'master' of github.com:jzck/42sh
This commit is contained in:
commit
ac27fb4ce1
17 changed files with 75 additions and 110 deletions
|
|
@ -6,14 +6,14 @@
|
|||
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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
|
||||
|
||||
CC = gcc
|
||||
FLAGS = -Wall -Wextra -Werror -fvisibility=hidden
|
||||
FLAGS = -Wall -Wextra -Werror -g
|
||||
D_FLAGS = -g
|
||||
|
||||
DELTA = $$(echo "$$(tput cols)-47"|bc)
|
||||
|
|
|
|||
|
|
@ -13,10 +13,6 @@
|
|||
#ifndef 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_LD (1 << 1)
|
||||
# define READ_OPT_LE (1 << 2)
|
||||
|
|
@ -35,12 +31,12 @@ typedef struct s_readopt t_readopt;
|
|||
struct s_read
|
||||
{
|
||||
t_flag opts;
|
||||
char **names;
|
||||
char delim;
|
||||
int nchars;
|
||||
char *prompt;
|
||||
int timeout;
|
||||
int fd;
|
||||
char **names;
|
||||
char *input;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@
|
|||
struct s_data
|
||||
{
|
||||
t_flag opts;
|
||||
char **av_data;
|
||||
int fd;
|
||||
char **env;
|
||||
int argc;
|
||||
|
|
|
|||
|
|
@ -49,7 +49,6 @@ char/ft_isprint.c\
|
|||
char/ft_tolower.c\
|
||||
char/ft_toupper.c\
|
||||
cliopts/cliopts_get.c\
|
||||
cliopts/cliopts_getdata.c\
|
||||
cliopts/cliopts_has.c\
|
||||
color/ft_color_mk.c\
|
||||
color/ft_color_mkif.c\
|
||||
|
|
|
|||
|
|
@ -32,10 +32,10 @@ struct s_cliopts
|
|||
struct s_data_template
|
||||
{
|
||||
t_flag flag;
|
||||
char **av_data;
|
||||
};
|
||||
|
||||
int cliopts_get(char **av, t_cliopts opt_map[], void *data);
|
||||
char **cliopts_getdata(char **av);
|
||||
int cliopts_has(char **av, char c);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -58,7 +58,6 @@ static int cliopts_parse_short(char ***av, t_cliopts opt_map[], void *data)
|
|||
++(*av);
|
||||
if ((map->get)(av, data))
|
||||
return (ERR_SET(E_CO_MISS, *arg));
|
||||
break;
|
||||
}
|
||||
((t_data_template*)data)->flag |= map->flag_on;
|
||||
((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);
|
||||
return (0);
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
else
|
||||
return (0);
|
||||
break ;
|
||||
}
|
||||
((t_data_template*)data)->av_data = av;
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -16,7 +16,8 @@ int bt_read_getdelim(char ***av, t_read *data)
|
|||
{
|
||||
if (!av || !*av)
|
||||
return (1);
|
||||
data->delim = ***av;
|
||||
if (data)
|
||||
data->delim = ***av;
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
|
@ -24,17 +25,17 @@ int bt_read_getnchars(char ***av, t_read *data)
|
|||
{
|
||||
if (!av || !*av)
|
||||
return (1);
|
||||
data->nchars = ft_atoi(**av);
|
||||
if (data)
|
||||
data->nchars = ft_atoi(**av);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int bt_read_getprompt(char ***av, t_read *data)
|
||||
{
|
||||
DG("getting prompt");
|
||||
if (!av || !*av || !**av)
|
||||
return (1);
|
||||
data->prompt = **av;
|
||||
DG("got prompt [%s]", data->prompt);
|
||||
if (data)
|
||||
data->prompt = **av;
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
|
@ -42,7 +43,8 @@ int bt_read_gettimeout(char ***av, t_read *data)
|
|||
{
|
||||
if (!av || !*av)
|
||||
return (1);
|
||||
data->timeout = ft_atoi(**av);
|
||||
if (data)
|
||||
data->timeout = ft_atoi(**av);
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
|
@ -50,6 +52,7 @@ int bt_read_getfd(char ***av, t_read *data)
|
|||
{
|
||||
if (!av || !*av)
|
||||
return (1);
|
||||
data->fd = ft_atoi(**av);
|
||||
if (data)
|
||||
data->fd = ft_atoi(**av);
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,17 +27,13 @@ int bt_read_terminit(t_read *data)
|
|||
|
||||
(void)data;
|
||||
term = bt_read_term(1);
|
||||
term.c_lflag &= ~(ICANON | ECHO);
|
||||
if (data->opts & READ_OPT_LT)
|
||||
{
|
||||
term.c_cc[VTIME] = data->timeout * 10;
|
||||
term.c_cc[VMIN] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
term.c_cc[VTIME] = 0;
|
||||
term.c_cc[VMIN] = 1;
|
||||
}
|
||||
term.c_lflag = ECHO | ECHOE | ECHOK | ICANON;
|
||||
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[VMIN] = !data->timeout;
|
||||
term.c_cc[VEOL] = data->delim;
|
||||
if (tcsetattr(0, TCSANOW, &term) < 0)
|
||||
return (-1);
|
||||
return (0);
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ int builtin_export(
|
|||
ft_perror();
|
||||
if (data.flag & BT_EXPORT_LP)
|
||||
return (bt_export_print());
|
||||
av = cliopts_getdata((char**)av);
|
||||
av = data.av_data;
|
||||
while (*av)
|
||||
{
|
||||
if ((equal = ft_strchr(*av, '=')))
|
||||
|
|
|
|||
|
|
@ -38,18 +38,18 @@ int bt_read_init(t_read *data, char **av)
|
|||
data->fd = 0;
|
||||
data->timeout = 0;
|
||||
data->input = NULL;
|
||||
data->names = NULL;
|
||||
if ((cliopts_get(av, g_read_opts, data)))
|
||||
return(ft_perror());
|
||||
if (data->names)
|
||||
DG("%s,%s", data->names[0], data->names[1]);
|
||||
DG("opts=%b", data->opts);
|
||||
bt_read_terminit(data);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int bt_read_loop(t_read *data)
|
||||
{
|
||||
int i;
|
||||
int esc;
|
||||
int ret;
|
||||
char buf[2];
|
||||
|
||||
esc = 0;
|
||||
|
|
@ -58,16 +58,15 @@ int bt_read_loop(t_read *data)
|
|||
ft_printf(data->prompt);
|
||||
while (42)
|
||||
{
|
||||
if (read(data->fd, buf, 1) <= 0)
|
||||
return (1);
|
||||
buf[1] = 0;
|
||||
if (!(data->opts &READ_OPT_LS))
|
||||
ft_putchar(*buf);
|
||||
if ((ret = read(data->fd, buf, 1)) <= 0)
|
||||
return (ret);
|
||||
buf[ret] = 0;
|
||||
DG("read [%c]", *buf);
|
||||
if (!esc && *buf == data->delim)
|
||||
break ;
|
||||
esc = esc ? 0 : !(data->opts & READ_OPT_LR) && (*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("> ");
|
||||
if ((data->opts & READ_OPT_LN) && ++i >= data->nchars)
|
||||
break ;
|
||||
|
|
@ -80,25 +79,22 @@ int bt_read_assign(t_read *data)
|
|||
char *input;
|
||||
char **names;
|
||||
char *ifs;
|
||||
char *start;
|
||||
char *tok;
|
||||
|
||||
DG("assigning");
|
||||
input = data->input;
|
||||
names = data->names ? data->names : (char*[]){"REPLY", NULL};
|
||||
ifs = ft_getenv(data_singleton()->env, "IFS");
|
||||
start = input;
|
||||
while (*start && *names)
|
||||
names = *data->names ? data->names : (char*[]){"REPLY", NULL};
|
||||
ifs = ft_getenv(data_singleton()->local_var, "IFS");
|
||||
if (!names[1])
|
||||
ifs = NULL;
|
||||
tok = ft_strtok(input, ifs);
|
||||
while (*names)
|
||||
{
|
||||
if (!(names[1]) || !ifs)
|
||||
{
|
||||
builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL);
|
||||
break ;
|
||||
}
|
||||
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;
|
||||
DG("tok=%s", tok);
|
||||
DG("name=%s", *names);
|
||||
builtin_setenv("setenv", (char*[]){"setenv", *names, tok}, NULL);
|
||||
ifs = names[1] ? ifs : NULL;
|
||||
tok = ft_strtok(NULL, ifs);
|
||||
names++;
|
||||
}
|
||||
return (0);
|
||||
|
|
@ -114,10 +110,12 @@ int builtin_read(const char *path, char *const av[], char *const envp[])
|
|||
ret = 0;
|
||||
if (bt_read_init(&data, (char **)av))
|
||||
ret = 2;
|
||||
else if (bt_read_loop(&data))
|
||||
ret = 1;
|
||||
else if ((ret = bt_read_loop(&data)))
|
||||
;
|
||||
else if (bt_read_assign(&data))
|
||||
ret = 1;
|
||||
if (ret == -1)
|
||||
exit(1);
|
||||
if (ret != 0)
|
||||
bt_read_usage();
|
||||
if (ret != 2)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
if (len < 0)
|
||||
return ;
|
||||
if (c->cpath)
|
||||
ft_strdel(&c->cpath);
|
||||
c->cpath = ft_strndup(current_word, len);
|
||||
if (current_word[0] == '~')
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
pos = c->ircmd;
|
||||
if (pos >= ft_strlen(c->rcmd))
|
||||
pos = ft_strlen(c->rcmd) - (ft_strlen(data_singleton()->line.input) - pos);
|
||||
while (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)
|
||||
{
|
||||
int pos;
|
||||
size_t pos;
|
||||
|
||||
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] != ' ')
|
||||
--pos;
|
||||
if (c->rcmd[pos] == ' ')
|
||||
|
|
@ -57,6 +62,7 @@ int c_matching(t_data *s, t_comp *c)
|
|||
char *current_word;
|
||||
|
||||
current_word = c_current_words(c);
|
||||
DG("current_word = %s", current_word);
|
||||
if (ft_strchr(c->rcmd, '/'))
|
||||
c_seek_abs_path(c, current_word);
|
||||
else if (ft_strchr(c->rcmd, '$'))
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
if (key == 27 || key == 127 || key == 2117294875)
|
||||
{
|
||||
c_term_clear(c);
|
||||
c_clear(data_singleton());
|
||||
return (0);
|
||||
}
|
||||
if (key == 10 || key == 32)
|
||||
{
|
||||
ptr = c->lst;
|
||||
|
|
@ -119,10 +113,16 @@ int c_keypress(t_comp *c, long int key)
|
|||
c_updater(c, ptr->name);
|
||||
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);
|
||||
return (0);
|
||||
}
|
||||
else if (!ft_isprint(key))
|
||||
{
|
||||
c_term_clear(c);
|
||||
c_clear(data_singleton());
|
||||
return (0);
|
||||
}
|
||||
return ((c_rematch(c, key)) ? (0) : (1));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ int data_init(int ac, char **av)
|
|||
builtin_setenv(NULL, (char *[]){"setenv", "SHLVL", shlvl, 0}, NULL);
|
||||
ft_strdel(&shlvl);
|
||||
data->comp = NULL;
|
||||
data->opts = 0;
|
||||
data->opts = SH_INTERACTIVE | SH_OPTS_JOBC;
|
||||
exec_reset();
|
||||
data->lst_func = NULL;
|
||||
lexer_init(&data->lexer);
|
||||
|
|
|
|||
|
|
@ -28,19 +28,19 @@ int get_input_fd(char **av)
|
|||
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)
|
||||
{
|
||||
pipe(fds);
|
||||
file = *cliopts_getdata(av);
|
||||
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 = *cliopts_getdata(av)) && !stat(file, &buf))
|
||||
else if (file && !stat(file, &buf))
|
||||
{
|
||||
fd = -1;
|
||||
if (S_ISDIR(buf.st_mode))
|
||||
|
|
@ -86,13 +86,10 @@ int shell_init(int ac, char **av)
|
|||
|
||||
data = data_singleton();
|
||||
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))
|
||||
return (ft_perror());
|
||||
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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue